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_refuse = 0; 8023 tcp->tcp_localnet = 0; 8024 tcp->tcp_syn_defense = 0; 8025 tcp->tcp_set_timer = 0; 8026 8027 tcp->tcp_active_open = 0; 8028 tcp->tcp_rexmit = B_FALSE; 8029 tcp->tcp_xmit_zc_clean = B_FALSE; 8030 8031 tcp->tcp_snd_sack_ok = B_FALSE; 8032 PRESERVE(tcp->tcp_recvdstaddr); 8033 tcp->tcp_hwcksum = B_FALSE; 8034 8035 tcp->tcp_ire_ill_check_done = B_FALSE; 8036 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 8037 8038 tcp->tcp_mdt = B_FALSE; 8039 tcp->tcp_mdt_hdr_head = 0; 8040 tcp->tcp_mdt_hdr_tail = 0; 8041 8042 tcp->tcp_conn_def_q0 = 0; 8043 tcp->tcp_ip_forward_progress = B_FALSE; 8044 tcp->tcp_anon_priv_bind = 0; 8045 tcp->tcp_ecn_ok = B_FALSE; 8046 8047 tcp->tcp_cwr = B_FALSE; 8048 tcp->tcp_ecn_echo_on = B_FALSE; 8049 8050 if (tcp->tcp_sack_info != NULL) { 8051 if (tcp->tcp_notsack_list != NULL) { 8052 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8053 } 8054 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8055 tcp->tcp_sack_info = NULL; 8056 } 8057 8058 tcp->tcp_rcv_ws = 0; 8059 tcp->tcp_snd_ws = 0; 8060 tcp->tcp_ts_recent = 0; 8061 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8062 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8063 tcp->tcp_if_mtu = 0; 8064 8065 ASSERT(tcp->tcp_reass_head == NULL); 8066 ASSERT(tcp->tcp_reass_tail == NULL); 8067 8068 tcp->tcp_cwnd_cnt = 0; 8069 8070 ASSERT(tcp->tcp_rcv_list == NULL); 8071 ASSERT(tcp->tcp_rcv_last_head == NULL); 8072 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8073 ASSERT(tcp->tcp_rcv_cnt == 0); 8074 8075 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8076 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8077 tcp->tcp_csuna = 0; 8078 8079 tcp->tcp_rto = 0; /* Displayed in MIB */ 8080 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8081 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8082 tcp->tcp_rtt_update = 0; 8083 8084 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8085 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8086 8087 tcp->tcp_rack = 0; /* Displayed in mib */ 8088 tcp->tcp_rack_cnt = 0; 8089 tcp->tcp_rack_cur_max = 0; 8090 tcp->tcp_rack_abs_max = 0; 8091 8092 tcp->tcp_max_swnd = 0; 8093 8094 ASSERT(tcp->tcp_listener == NULL); 8095 8096 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8097 8098 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8099 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8100 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8101 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8102 8103 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8104 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8105 PRESERVE(tcp->tcp_conn_req_max); 8106 PRESERVE(tcp->tcp_conn_req_seqnum); 8107 8108 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8109 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8110 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8111 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8112 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8113 8114 tcp->tcp_lingertime = 0; 8115 8116 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8117 ASSERT(tcp->tcp_urp_mp == NULL); 8118 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8119 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8120 8121 ASSERT(tcp->tcp_eager_next_q == NULL); 8122 ASSERT(tcp->tcp_eager_last_q == NULL); 8123 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8124 tcp->tcp_eager_prev_q0 == NULL) || 8125 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8126 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8127 8128 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8129 tcp->tcp_eager_prev_drop_q0 == NULL) || 8130 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8131 8132 tcp->tcp_client_errno = 0; 8133 8134 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8135 8136 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8137 8138 PRESERVE(tcp->tcp_bound_source_v6); 8139 tcp->tcp_last_sent_len = 0; 8140 tcp->tcp_dupack_cnt = 0; 8141 8142 tcp->tcp_fport = 0; /* Displayed in MIB */ 8143 PRESERVE(tcp->tcp_lport); 8144 8145 PRESERVE(tcp->tcp_acceptor_lockp); 8146 8147 ASSERT(tcp->tcp_ordrel_mp == NULL); 8148 PRESERVE(tcp->tcp_acceptor_id); 8149 DONTCARE(tcp->tcp_ipsec_overhead); 8150 8151 PRESERVE(tcp->tcp_family); 8152 if (tcp->tcp_family == AF_INET6) { 8153 tcp->tcp_ipversion = IPV6_VERSION; 8154 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8155 } else { 8156 tcp->tcp_ipversion = IPV4_VERSION; 8157 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8158 } 8159 8160 tcp->tcp_bound_if = 0; 8161 tcp->tcp_ipv6_recvancillary = 0; 8162 tcp->tcp_recvifindex = 0; 8163 tcp->tcp_recvhops = 0; 8164 tcp->tcp_closed = 0; 8165 tcp->tcp_cleandeathtag = 0; 8166 if (tcp->tcp_hopopts != NULL) { 8167 mi_free(tcp->tcp_hopopts); 8168 tcp->tcp_hopopts = NULL; 8169 tcp->tcp_hopoptslen = 0; 8170 } 8171 ASSERT(tcp->tcp_hopoptslen == 0); 8172 if (tcp->tcp_dstopts != NULL) { 8173 mi_free(tcp->tcp_dstopts); 8174 tcp->tcp_dstopts = NULL; 8175 tcp->tcp_dstoptslen = 0; 8176 } 8177 ASSERT(tcp->tcp_dstoptslen == 0); 8178 if (tcp->tcp_rtdstopts != NULL) { 8179 mi_free(tcp->tcp_rtdstopts); 8180 tcp->tcp_rtdstopts = NULL; 8181 tcp->tcp_rtdstoptslen = 0; 8182 } 8183 ASSERT(tcp->tcp_rtdstoptslen == 0); 8184 if (tcp->tcp_rthdr != NULL) { 8185 mi_free(tcp->tcp_rthdr); 8186 tcp->tcp_rthdr = NULL; 8187 tcp->tcp_rthdrlen = 0; 8188 } 8189 ASSERT(tcp->tcp_rthdrlen == 0); 8190 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8191 8192 /* Reset fusion-related fields */ 8193 tcp->tcp_fused = B_FALSE; 8194 tcp->tcp_unfusable = B_FALSE; 8195 tcp->tcp_fused_sigurg = B_FALSE; 8196 tcp->tcp_direct_sockfs = B_FALSE; 8197 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8198 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8199 tcp->tcp_loopback_peer = NULL; 8200 tcp->tcp_fuse_rcv_hiwater = 0; 8201 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8202 tcp->tcp_fuse_rcv_unread_cnt = 0; 8203 8204 tcp->tcp_lso = B_FALSE; 8205 8206 tcp->tcp_in_ack_unsent = 0; 8207 tcp->tcp_cork = B_FALSE; 8208 tcp->tcp_tconnind_started = B_FALSE; 8209 8210 PRESERVE(tcp->tcp_squeue_bytes); 8211 8212 ASSERT(tcp->tcp_kssl_ctx == NULL); 8213 ASSERT(!tcp->tcp_kssl_pending); 8214 PRESERVE(tcp->tcp_kssl_ent); 8215 8216 /* Sodirect */ 8217 tcp->tcp_sodirect = NULL; 8218 8219 tcp->tcp_closemp_used = B_FALSE; 8220 8221 PRESERVE(tcp->tcp_rsrv_mp); 8222 PRESERVE(tcp->tcp_rsrv_mp_lock); 8223 8224 #ifdef DEBUG 8225 DONTCARE(tcp->tcmp_stk[0]); 8226 #endif 8227 8228 8229 #undef DONTCARE 8230 #undef PRESERVE 8231 } 8232 8233 /* 8234 * Allocate necessary resources and initialize state vector. 8235 * Guaranteed not to fail so that when an error is returned, 8236 * the caller doesn't need to do any additional cleanup. 8237 */ 8238 int 8239 tcp_init(tcp_t *tcp, queue_t *q) 8240 { 8241 int err; 8242 8243 tcp->tcp_rq = q; 8244 tcp->tcp_wq = WR(q); 8245 tcp->tcp_state = TCPS_IDLE; 8246 if ((err = tcp_init_values(tcp)) != 0) 8247 tcp_timers_stop(tcp); 8248 return (err); 8249 } 8250 8251 static int 8252 tcp_init_values(tcp_t *tcp) 8253 { 8254 int err; 8255 tcp_stack_t *tcps = tcp->tcp_tcps; 8256 8257 ASSERT((tcp->tcp_family == AF_INET && 8258 tcp->tcp_ipversion == IPV4_VERSION) || 8259 (tcp->tcp_family == AF_INET6 && 8260 (tcp->tcp_ipversion == IPV4_VERSION || 8261 tcp->tcp_ipversion == IPV6_VERSION))); 8262 8263 /* 8264 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8265 * will be close to tcp_rexmit_interval_initial. By doing this, we 8266 * allow the algorithm to adjust slowly to large fluctuations of RTT 8267 * during first few transmissions of a connection as seen in slow 8268 * links. 8269 */ 8270 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8271 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8272 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8273 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8274 tcps->tcps_conn_grace_period; 8275 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8276 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8277 tcp->tcp_timer_backoff = 0; 8278 tcp->tcp_ms_we_have_waited = 0; 8279 tcp->tcp_last_recv_time = lbolt; 8280 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8281 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8282 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8283 8284 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8285 8286 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8287 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8288 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8289 /* 8290 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8291 * passive open. 8292 */ 8293 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8294 8295 tcp->tcp_naglim = tcps->tcps_naglim_def; 8296 8297 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8298 8299 tcp->tcp_mdt_hdr_head = 0; 8300 tcp->tcp_mdt_hdr_tail = 0; 8301 8302 /* Reset fusion-related fields */ 8303 tcp->tcp_fused = B_FALSE; 8304 tcp->tcp_unfusable = B_FALSE; 8305 tcp->tcp_fused_sigurg = B_FALSE; 8306 tcp->tcp_direct_sockfs = B_FALSE; 8307 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8308 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8309 tcp->tcp_loopback_peer = NULL; 8310 tcp->tcp_fuse_rcv_hiwater = 0; 8311 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8312 tcp->tcp_fuse_rcv_unread_cnt = 0; 8313 8314 /* Sodirect */ 8315 tcp->tcp_sodirect = NULL; 8316 8317 /* Initialize the header template */ 8318 if (tcp->tcp_ipversion == IPV4_VERSION) { 8319 err = tcp_header_init_ipv4(tcp); 8320 } else { 8321 err = tcp_header_init_ipv6(tcp); 8322 } 8323 if (err) 8324 return (err); 8325 8326 /* 8327 * Init the window scale to the max so tcp_rwnd_set() won't pare 8328 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8329 */ 8330 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8331 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8332 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8333 8334 tcp->tcp_cork = B_FALSE; 8335 /* 8336 * Init the tcp_debug option. This value determines whether TCP 8337 * calls strlog() to print out debug messages. Doing this 8338 * initialization here means that this value is not inherited thru 8339 * tcp_reinit(). 8340 */ 8341 tcp->tcp_debug = tcps->tcps_dbg; 8342 8343 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8344 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8345 8346 return (0); 8347 } 8348 8349 /* 8350 * Initialize the IPv4 header. Loses any record of any IP options. 8351 */ 8352 static int 8353 tcp_header_init_ipv4(tcp_t *tcp) 8354 { 8355 tcph_t *tcph; 8356 uint32_t sum; 8357 conn_t *connp; 8358 tcp_stack_t *tcps = tcp->tcp_tcps; 8359 8360 /* 8361 * This is a simple initialization. If there's 8362 * already a template, it should never be too small, 8363 * so reuse it. Otherwise, allocate space for the new one. 8364 */ 8365 if (tcp->tcp_iphc == NULL) { 8366 ASSERT(tcp->tcp_iphc_len == 0); 8367 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8368 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8369 if (tcp->tcp_iphc == NULL) { 8370 tcp->tcp_iphc_len = 0; 8371 return (ENOMEM); 8372 } 8373 } 8374 8375 /* options are gone; may need a new label */ 8376 connp = tcp->tcp_connp; 8377 connp->conn_mlp_type = mlptSingle; 8378 connp->conn_ulp_labeled = !is_system_labeled(); 8379 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8380 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8381 tcp->tcp_ip6h = NULL; 8382 tcp->tcp_ipversion = IPV4_VERSION; 8383 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8384 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8385 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8386 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8387 tcp->tcp_ipha->ipha_version_and_hdr_length 8388 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8389 tcp->tcp_ipha->ipha_ident = 0; 8390 8391 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8392 tcp->tcp_tos = 0; 8393 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8394 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8395 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8396 8397 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8398 tcp->tcp_tcph = tcph; 8399 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8400 /* 8401 * IP wants our header length in the checksum field to 8402 * allow it to perform a single pseudo-header+checksum 8403 * calculation on behalf of TCP. 8404 * Include the adjustment for a source route once IP_OPTIONS is set. 8405 */ 8406 sum = sizeof (tcph_t) + tcp->tcp_sum; 8407 sum = (sum >> 16) + (sum & 0xFFFF); 8408 U16_TO_ABE16(sum, tcph->th_sum); 8409 return (0); 8410 } 8411 8412 /* 8413 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8414 */ 8415 static int 8416 tcp_header_init_ipv6(tcp_t *tcp) 8417 { 8418 tcph_t *tcph; 8419 uint32_t sum; 8420 conn_t *connp; 8421 tcp_stack_t *tcps = tcp->tcp_tcps; 8422 8423 /* 8424 * This is a simple initialization. If there's 8425 * already a template, it should never be too small, 8426 * so reuse it. Otherwise, allocate space for the new one. 8427 * Ensure that there is enough space to "downgrade" the tcp_t 8428 * to an IPv4 tcp_t. This requires having space for a full load 8429 * of IPv4 options, as well as a full load of TCP options 8430 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8431 * than a v6 header and a TCP header with a full load of TCP options 8432 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8433 * We want to avoid reallocation in the "downgraded" case when 8434 * processing outbound IPv4 options. 8435 */ 8436 if (tcp->tcp_iphc == NULL) { 8437 ASSERT(tcp->tcp_iphc_len == 0); 8438 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8439 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8440 if (tcp->tcp_iphc == NULL) { 8441 tcp->tcp_iphc_len = 0; 8442 return (ENOMEM); 8443 } 8444 } 8445 8446 /* options are gone; may need a new label */ 8447 connp = tcp->tcp_connp; 8448 connp->conn_mlp_type = mlptSingle; 8449 connp->conn_ulp_labeled = !is_system_labeled(); 8450 8451 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8452 tcp->tcp_ipversion = IPV6_VERSION; 8453 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8454 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8455 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8456 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8457 tcp->tcp_ipha = NULL; 8458 8459 /* Initialize the header template */ 8460 8461 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8462 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8463 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8464 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8465 8466 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8467 tcp->tcp_tcph = tcph; 8468 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8469 /* 8470 * IP wants our header length in the checksum field to 8471 * allow it to perform a single psuedo-header+checksum 8472 * calculation on behalf of TCP. 8473 * Include the adjustment for a source route when IPV6_RTHDR is set. 8474 */ 8475 sum = sizeof (tcph_t) + tcp->tcp_sum; 8476 sum = (sum >> 16) + (sum & 0xFFFF); 8477 U16_TO_ABE16(sum, tcph->th_sum); 8478 return (0); 8479 } 8480 8481 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8482 #define ICMP_MIN_TCP_HDR 8 8483 8484 /* 8485 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8486 * passed up by IP. The message is always received on the correct tcp_t. 8487 * Assumes that IP has pulled up everything up to and including the ICMP header. 8488 */ 8489 void 8490 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8491 { 8492 icmph_t *icmph; 8493 ipha_t *ipha; 8494 int iph_hdr_length; 8495 tcph_t *tcph; 8496 boolean_t ipsec_mctl = B_FALSE; 8497 boolean_t secure; 8498 mblk_t *first_mp = mp; 8499 uint32_t new_mss; 8500 uint32_t ratio; 8501 size_t mp_size = MBLKL(mp); 8502 uint32_t seg_seq; 8503 tcp_stack_t *tcps = tcp->tcp_tcps; 8504 8505 /* Assume IP provides aligned packets - otherwise toss */ 8506 if (!OK_32PTR(mp->b_rptr)) { 8507 freemsg(mp); 8508 return; 8509 } 8510 8511 /* 8512 * Since ICMP errors are normal data marked with M_CTL when sent 8513 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8514 * packets starting with an ipsec_info_t, see ipsec_info.h. 8515 */ 8516 if ((mp_size == sizeof (ipsec_info_t)) && 8517 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8518 ASSERT(mp->b_cont != NULL); 8519 mp = mp->b_cont; 8520 /* IP should have done this */ 8521 ASSERT(OK_32PTR(mp->b_rptr)); 8522 mp_size = MBLKL(mp); 8523 ipsec_mctl = B_TRUE; 8524 } 8525 8526 /* 8527 * Verify that we have a complete outer IP header. If not, drop it. 8528 */ 8529 if (mp_size < sizeof (ipha_t)) { 8530 noticmpv4: 8531 freemsg(first_mp); 8532 return; 8533 } 8534 8535 ipha = (ipha_t *)mp->b_rptr; 8536 /* 8537 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8538 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8539 */ 8540 switch (IPH_HDR_VERSION(ipha)) { 8541 case IPV6_VERSION: 8542 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8543 return; 8544 case IPV4_VERSION: 8545 break; 8546 default: 8547 goto noticmpv4; 8548 } 8549 8550 /* Skip past the outer IP and ICMP headers */ 8551 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8552 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8553 /* 8554 * If we don't have the correct outer IP header length or if the ULP 8555 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8556 * send it upstream. 8557 */ 8558 if (iph_hdr_length < sizeof (ipha_t) || 8559 ipha->ipha_protocol != IPPROTO_ICMP || 8560 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8561 goto noticmpv4; 8562 } 8563 ipha = (ipha_t *)&icmph[1]; 8564 8565 /* Skip past the inner IP and find the ULP header */ 8566 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8567 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8568 /* 8569 * If we don't have the correct inner IP header length or if the ULP 8570 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8571 * bytes of TCP header, drop it. 8572 */ 8573 if (iph_hdr_length < sizeof (ipha_t) || 8574 ipha->ipha_protocol != IPPROTO_TCP || 8575 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8576 goto noticmpv4; 8577 } 8578 8579 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8580 if (ipsec_mctl) { 8581 secure = ipsec_in_is_secure(first_mp); 8582 } else { 8583 secure = B_FALSE; 8584 } 8585 if (secure) { 8586 /* 8587 * If we are willing to accept this in clear 8588 * we don't have to verify policy. 8589 */ 8590 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8591 if (!tcp_check_policy(tcp, first_mp, 8592 ipha, NULL, secure, ipsec_mctl)) { 8593 /* 8594 * tcp_check_policy called 8595 * ip_drop_packet() on failure. 8596 */ 8597 return; 8598 } 8599 } 8600 } 8601 } else if (ipsec_mctl) { 8602 /* 8603 * This is a hard_bound connection. IP has already 8604 * verified policy. We don't have to do it again. 8605 */ 8606 freeb(first_mp); 8607 first_mp = mp; 8608 ipsec_mctl = B_FALSE; 8609 } 8610 8611 seg_seq = ABE32_TO_U32(tcph->th_seq); 8612 /* 8613 * TCP SHOULD check that the TCP sequence number contained in 8614 * payload of the ICMP error message is within the range 8615 * SND.UNA <= SEG.SEQ < SND.NXT. 8616 */ 8617 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8618 /* 8619 * If the ICMP message is bogus, should we kill the 8620 * connection, or should we just drop the bogus ICMP 8621 * message? It would probably make more sense to just 8622 * drop the message so that if this one managed to get 8623 * in, the real connection should not suffer. 8624 */ 8625 goto noticmpv4; 8626 } 8627 8628 switch (icmph->icmph_type) { 8629 case ICMP_DEST_UNREACHABLE: 8630 switch (icmph->icmph_code) { 8631 case ICMP_FRAGMENTATION_NEEDED: 8632 /* 8633 * Reduce the MSS based on the new MTU. This will 8634 * eliminate any fragmentation locally. 8635 * N.B. There may well be some funny side-effects on 8636 * the local send policy and the remote receive policy. 8637 * Pending further research, we provide 8638 * tcp_ignore_path_mtu just in case this proves 8639 * disastrous somewhere. 8640 * 8641 * After updating the MSS, retransmit part of the 8642 * dropped segment using the new mss by calling 8643 * tcp_wput_data(). Need to adjust all those 8644 * params to make sure tcp_wput_data() work properly. 8645 */ 8646 if (tcps->tcps_ignore_path_mtu) 8647 break; 8648 8649 /* 8650 * Decrease the MSS by time stamp options 8651 * IP options and IPSEC options. tcp_hdr_len 8652 * includes time stamp option and IP option 8653 * length. 8654 */ 8655 8656 new_mss = ntohs(icmph->icmph_du_mtu) - 8657 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8658 8659 /* 8660 * Only update the MSS if the new one is 8661 * smaller than the previous one. This is 8662 * to avoid problems when getting multiple 8663 * ICMP errors for the same MTU. 8664 */ 8665 if (new_mss >= tcp->tcp_mss) 8666 break; 8667 8668 /* 8669 * Stop doing PMTU if new_mss is less than 68 8670 * or less than tcp_mss_min. 8671 * The value 68 comes from rfc 1191. 8672 */ 8673 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8674 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8675 0; 8676 8677 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8678 ASSERT(ratio >= 1); 8679 tcp_mss_set(tcp, new_mss, B_TRUE); 8680 8681 /* 8682 * Make sure we have something to 8683 * send. 8684 */ 8685 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8686 (tcp->tcp_xmit_head != NULL)) { 8687 /* 8688 * Shrink tcp_cwnd in 8689 * proportion to the old MSS/new MSS. 8690 */ 8691 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8692 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8693 (tcp->tcp_unsent == 0)) { 8694 tcp->tcp_rexmit_max = tcp->tcp_fss; 8695 } else { 8696 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8697 } 8698 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8699 tcp->tcp_rexmit = B_TRUE; 8700 tcp->tcp_dupack_cnt = 0; 8701 tcp->tcp_snd_burst = TCP_CWND_SS; 8702 tcp_ss_rexmit(tcp); 8703 } 8704 break; 8705 case ICMP_PORT_UNREACHABLE: 8706 case ICMP_PROTOCOL_UNREACHABLE: 8707 switch (tcp->tcp_state) { 8708 case TCPS_SYN_SENT: 8709 case TCPS_SYN_RCVD: 8710 /* 8711 * ICMP can snipe away incipient 8712 * TCP connections as long as 8713 * seq number is same as initial 8714 * send seq number. 8715 */ 8716 if (seg_seq == tcp->tcp_iss) { 8717 (void) tcp_clean_death(tcp, 8718 ECONNREFUSED, 6); 8719 } 8720 break; 8721 } 8722 break; 8723 case ICMP_HOST_UNREACHABLE: 8724 case ICMP_NET_UNREACHABLE: 8725 /* Record the error in case we finally time out. */ 8726 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8727 tcp->tcp_client_errno = EHOSTUNREACH; 8728 else 8729 tcp->tcp_client_errno = ENETUNREACH; 8730 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8731 if (tcp->tcp_listener != NULL && 8732 tcp->tcp_listener->tcp_syn_defense) { 8733 /* 8734 * Ditch the half-open connection if we 8735 * suspect a SYN attack is under way. 8736 */ 8737 tcp_ip_ire_mark_advice(tcp); 8738 (void) tcp_clean_death(tcp, 8739 tcp->tcp_client_errno, 7); 8740 } 8741 } 8742 break; 8743 default: 8744 break; 8745 } 8746 break; 8747 case ICMP_SOURCE_QUENCH: { 8748 /* 8749 * use a global boolean to control 8750 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8751 * The default is false. 8752 */ 8753 if (tcp_icmp_source_quench) { 8754 /* 8755 * Reduce the sending rate as if we got a 8756 * retransmit timeout 8757 */ 8758 uint32_t npkt; 8759 8760 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8761 tcp->tcp_mss; 8762 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8763 tcp->tcp_cwnd = tcp->tcp_mss; 8764 tcp->tcp_cwnd_cnt = 0; 8765 } 8766 break; 8767 } 8768 } 8769 freemsg(first_mp); 8770 } 8771 8772 /* 8773 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8774 * error messages passed up by IP. 8775 * Assumes that IP has pulled up all the extension headers as well 8776 * as the ICMPv6 header. 8777 */ 8778 static void 8779 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8780 { 8781 icmp6_t *icmp6; 8782 ip6_t *ip6h; 8783 uint16_t iph_hdr_length; 8784 tcpha_t *tcpha; 8785 uint8_t *nexthdrp; 8786 uint32_t new_mss; 8787 uint32_t ratio; 8788 boolean_t secure; 8789 mblk_t *first_mp = mp; 8790 size_t mp_size; 8791 uint32_t seg_seq; 8792 tcp_stack_t *tcps = tcp->tcp_tcps; 8793 8794 /* 8795 * The caller has determined if this is an IPSEC_IN packet and 8796 * set ipsec_mctl appropriately (see tcp_icmp_error). 8797 */ 8798 if (ipsec_mctl) 8799 mp = mp->b_cont; 8800 8801 mp_size = MBLKL(mp); 8802 8803 /* 8804 * Verify that we have a complete IP header. If not, send it upstream. 8805 */ 8806 if (mp_size < sizeof (ip6_t)) { 8807 noticmpv6: 8808 freemsg(first_mp); 8809 return; 8810 } 8811 8812 /* 8813 * Verify this is an ICMPV6 packet, else send it upstream. 8814 */ 8815 ip6h = (ip6_t *)mp->b_rptr; 8816 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8817 iph_hdr_length = IPV6_HDR_LEN; 8818 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8819 &nexthdrp) || 8820 *nexthdrp != IPPROTO_ICMPV6) { 8821 goto noticmpv6; 8822 } 8823 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8824 ip6h = (ip6_t *)&icmp6[1]; 8825 /* 8826 * Verify if we have a complete ICMP and inner IP header. 8827 */ 8828 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8829 goto noticmpv6; 8830 8831 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8832 goto noticmpv6; 8833 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8834 /* 8835 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8836 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8837 * packet. 8838 */ 8839 if ((*nexthdrp != IPPROTO_TCP) || 8840 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8841 goto noticmpv6; 8842 } 8843 8844 /* 8845 * ICMP errors come on the right queue or come on 8846 * listener/global queue for detached connections and 8847 * get switched to the right queue. If it comes on the 8848 * right queue, policy check has already been done by IP 8849 * and thus free the first_mp without verifying the policy. 8850 * If it has come for a non-hard bound connection, we need 8851 * to verify policy as IP may not have done it. 8852 */ 8853 if (!tcp->tcp_hard_bound) { 8854 if (ipsec_mctl) { 8855 secure = ipsec_in_is_secure(first_mp); 8856 } else { 8857 secure = B_FALSE; 8858 } 8859 if (secure) { 8860 /* 8861 * If we are willing to accept this in clear 8862 * we don't have to verify policy. 8863 */ 8864 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8865 if (!tcp_check_policy(tcp, first_mp, 8866 NULL, ip6h, secure, ipsec_mctl)) { 8867 /* 8868 * tcp_check_policy called 8869 * ip_drop_packet() on failure. 8870 */ 8871 return; 8872 } 8873 } 8874 } 8875 } else if (ipsec_mctl) { 8876 /* 8877 * This is a hard_bound connection. IP has already 8878 * verified policy. We don't have to do it again. 8879 */ 8880 freeb(first_mp); 8881 first_mp = mp; 8882 ipsec_mctl = B_FALSE; 8883 } 8884 8885 seg_seq = ntohl(tcpha->tha_seq); 8886 /* 8887 * TCP SHOULD check that the TCP sequence number contained in 8888 * payload of the ICMP error message is within the range 8889 * SND.UNA <= SEG.SEQ < SND.NXT. 8890 */ 8891 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8892 /* 8893 * If the ICMP message is bogus, should we kill the 8894 * connection, or should we just drop the bogus ICMP 8895 * message? It would probably make more sense to just 8896 * drop the message so that if this one managed to get 8897 * in, the real connection should not suffer. 8898 */ 8899 goto noticmpv6; 8900 } 8901 8902 switch (icmp6->icmp6_type) { 8903 case ICMP6_PACKET_TOO_BIG: 8904 /* 8905 * Reduce the MSS based on the new MTU. This will 8906 * eliminate any fragmentation locally. 8907 * N.B. There may well be some funny side-effects on 8908 * the local send policy and the remote receive policy. 8909 * Pending further research, we provide 8910 * tcp_ignore_path_mtu just in case this proves 8911 * disastrous somewhere. 8912 * 8913 * After updating the MSS, retransmit part of the 8914 * dropped segment using the new mss by calling 8915 * tcp_wput_data(). Need to adjust all those 8916 * params to make sure tcp_wput_data() work properly. 8917 */ 8918 if (tcps->tcps_ignore_path_mtu) 8919 break; 8920 8921 /* 8922 * Decrease the MSS by time stamp options 8923 * IP options and IPSEC options. tcp_hdr_len 8924 * includes time stamp option and IP option 8925 * length. 8926 */ 8927 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8928 tcp->tcp_ipsec_overhead; 8929 8930 /* 8931 * Only update the MSS if the new one is 8932 * smaller than the previous one. This is 8933 * to avoid problems when getting multiple 8934 * ICMP errors for the same MTU. 8935 */ 8936 if (new_mss >= tcp->tcp_mss) 8937 break; 8938 8939 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8940 ASSERT(ratio >= 1); 8941 tcp_mss_set(tcp, new_mss, B_TRUE); 8942 8943 /* 8944 * Make sure we have something to 8945 * send. 8946 */ 8947 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8948 (tcp->tcp_xmit_head != NULL)) { 8949 /* 8950 * Shrink tcp_cwnd in 8951 * proportion to the old MSS/new MSS. 8952 */ 8953 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8954 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8955 (tcp->tcp_unsent == 0)) { 8956 tcp->tcp_rexmit_max = tcp->tcp_fss; 8957 } else { 8958 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8959 } 8960 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8961 tcp->tcp_rexmit = B_TRUE; 8962 tcp->tcp_dupack_cnt = 0; 8963 tcp->tcp_snd_burst = TCP_CWND_SS; 8964 tcp_ss_rexmit(tcp); 8965 } 8966 break; 8967 8968 case ICMP6_DST_UNREACH: 8969 switch (icmp6->icmp6_code) { 8970 case ICMP6_DST_UNREACH_NOPORT: 8971 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8972 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8973 (seg_seq == tcp->tcp_iss)) { 8974 (void) tcp_clean_death(tcp, 8975 ECONNREFUSED, 8); 8976 } 8977 break; 8978 8979 case ICMP6_DST_UNREACH_ADMIN: 8980 case ICMP6_DST_UNREACH_NOROUTE: 8981 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8982 case ICMP6_DST_UNREACH_ADDR: 8983 /* Record the error in case we finally time out. */ 8984 tcp->tcp_client_errno = EHOSTUNREACH; 8985 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8986 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8987 (seg_seq == tcp->tcp_iss)) { 8988 if (tcp->tcp_listener != NULL && 8989 tcp->tcp_listener->tcp_syn_defense) { 8990 /* 8991 * Ditch the half-open connection if we 8992 * suspect a SYN attack is under way. 8993 */ 8994 tcp_ip_ire_mark_advice(tcp); 8995 (void) tcp_clean_death(tcp, 8996 tcp->tcp_client_errno, 9); 8997 } 8998 } 8999 9000 9001 break; 9002 default: 9003 break; 9004 } 9005 break; 9006 9007 case ICMP6_PARAM_PROB: 9008 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 9009 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 9010 (uchar_t *)ip6h + icmp6->icmp6_pptr == 9011 (uchar_t *)nexthdrp) { 9012 if (tcp->tcp_state == TCPS_SYN_SENT || 9013 tcp->tcp_state == TCPS_SYN_RCVD) { 9014 (void) tcp_clean_death(tcp, 9015 ECONNREFUSED, 10); 9016 } 9017 break; 9018 } 9019 break; 9020 9021 case ICMP6_TIME_EXCEEDED: 9022 default: 9023 break; 9024 } 9025 freemsg(first_mp); 9026 } 9027 9028 /* 9029 * IP recognizes seven kinds of bind requests: 9030 * 9031 * - A zero-length address binds only to the protocol number. 9032 * 9033 * - A 4-byte address is treated as a request to 9034 * validate that the address is a valid local IPv4 9035 * address, appropriate for an application to bind to. 9036 * IP does the verification, but does not make any note 9037 * of the address at this time. 9038 * 9039 * - A 16-byte address contains is treated as a request 9040 * to validate a local IPv6 address, as the 4-byte 9041 * address case above. 9042 * 9043 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9044 * use it for the inbound fanout of packets. 9045 * 9046 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9047 * use it for the inbound fanout of packets. 9048 * 9049 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9050 * information consisting of local and remote addresses 9051 * and ports. In this case, the addresses are both 9052 * validated as appropriate for this operation, and, if 9053 * so, the information is retained for use in the 9054 * inbound fanout. 9055 * 9056 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9057 * fanout information, like the 12-byte case above. 9058 * 9059 * IP will also fill in the IRE request mblk with information 9060 * regarding our peer. In all cases, we notify IP of our protocol 9061 * type by appending a single protocol byte to the bind request. 9062 */ 9063 static mblk_t * 9064 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9065 { 9066 char *cp; 9067 mblk_t *mp; 9068 struct T_bind_req *tbr; 9069 ipa_conn_t *ac; 9070 ipa6_conn_t *ac6; 9071 sin_t *sin; 9072 sin6_t *sin6; 9073 9074 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9075 ASSERT((tcp->tcp_family == AF_INET && 9076 tcp->tcp_ipversion == IPV4_VERSION) || 9077 (tcp->tcp_family == AF_INET6 && 9078 (tcp->tcp_ipversion == IPV4_VERSION || 9079 tcp->tcp_ipversion == IPV6_VERSION))); 9080 9081 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9082 if (!mp) 9083 return (mp); 9084 mp->b_datap->db_type = M_PROTO; 9085 tbr = (struct T_bind_req *)mp->b_rptr; 9086 tbr->PRIM_type = bind_prim; 9087 tbr->ADDR_offset = sizeof (*tbr); 9088 tbr->CONIND_number = 0; 9089 tbr->ADDR_length = addr_length; 9090 cp = (char *)&tbr[1]; 9091 switch (addr_length) { 9092 case sizeof (ipa_conn_t): 9093 ASSERT(tcp->tcp_family == AF_INET); 9094 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9095 9096 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9097 if (mp->b_cont == NULL) { 9098 freemsg(mp); 9099 return (NULL); 9100 } 9101 mp->b_cont->b_wptr += sizeof (ire_t); 9102 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9103 9104 /* cp known to be 32 bit aligned */ 9105 ac = (ipa_conn_t *)cp; 9106 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9107 ac->ac_faddr = tcp->tcp_remote; 9108 ac->ac_fport = tcp->tcp_fport; 9109 ac->ac_lport = tcp->tcp_lport; 9110 tcp->tcp_hard_binding = 1; 9111 break; 9112 9113 case sizeof (ipa6_conn_t): 9114 ASSERT(tcp->tcp_family == AF_INET6); 9115 9116 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9117 if (mp->b_cont == NULL) { 9118 freemsg(mp); 9119 return (NULL); 9120 } 9121 mp->b_cont->b_wptr += sizeof (ire_t); 9122 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9123 9124 /* cp known to be 32 bit aligned */ 9125 ac6 = (ipa6_conn_t *)cp; 9126 if (tcp->tcp_ipversion == IPV4_VERSION) { 9127 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9128 &ac6->ac6_laddr); 9129 } else { 9130 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9131 } 9132 ac6->ac6_faddr = tcp->tcp_remote_v6; 9133 ac6->ac6_fport = tcp->tcp_fport; 9134 ac6->ac6_lport = tcp->tcp_lport; 9135 tcp->tcp_hard_binding = 1; 9136 break; 9137 9138 case sizeof (sin_t): 9139 /* 9140 * NOTE: IPV6_ADDR_LEN also has same size. 9141 * Use family to discriminate. 9142 */ 9143 if (tcp->tcp_family == AF_INET) { 9144 sin = (sin_t *)cp; 9145 9146 *sin = sin_null; 9147 sin->sin_family = AF_INET; 9148 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9149 sin->sin_port = tcp->tcp_lport; 9150 break; 9151 } else { 9152 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9153 } 9154 break; 9155 9156 case sizeof (sin6_t): 9157 ASSERT(tcp->tcp_family == AF_INET6); 9158 sin6 = (sin6_t *)cp; 9159 9160 *sin6 = sin6_null; 9161 sin6->sin6_family = AF_INET6; 9162 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9163 sin6->sin6_port = tcp->tcp_lport; 9164 break; 9165 9166 case IP_ADDR_LEN: 9167 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9168 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9169 break; 9170 9171 } 9172 /* Add protocol number to end */ 9173 cp[addr_length] = (char)IPPROTO_TCP; 9174 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9175 return (mp); 9176 } 9177 9178 /* 9179 * Notify IP that we are having trouble with this connection. IP should 9180 * blow the IRE away and start over. 9181 */ 9182 static void 9183 tcp_ip_notify(tcp_t *tcp) 9184 { 9185 struct iocblk *iocp; 9186 ipid_t *ipid; 9187 mblk_t *mp; 9188 9189 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9190 if (tcp->tcp_ipversion == IPV6_VERSION) 9191 return; 9192 9193 mp = mkiocb(IP_IOCTL); 9194 if (mp == NULL) 9195 return; 9196 9197 iocp = (struct iocblk *)mp->b_rptr; 9198 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9199 9200 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9201 if (!mp->b_cont) { 9202 freeb(mp); 9203 return; 9204 } 9205 9206 ipid = (ipid_t *)mp->b_cont->b_rptr; 9207 mp->b_cont->b_wptr += iocp->ioc_count; 9208 bzero(ipid, sizeof (*ipid)); 9209 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9210 ipid->ipid_ire_type = IRE_CACHE; 9211 ipid->ipid_addr_offset = sizeof (ipid_t); 9212 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9213 /* 9214 * Note: in the case of source routing we want to blow away the 9215 * route to the first source route hop. 9216 */ 9217 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9218 sizeof (tcp->tcp_ipha->ipha_dst)); 9219 9220 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9221 } 9222 9223 /* Unlink and return any mblk that looks like it contains an ire */ 9224 static mblk_t * 9225 tcp_ire_mp(mblk_t *mp) 9226 { 9227 mblk_t *prev_mp; 9228 9229 for (;;) { 9230 prev_mp = mp; 9231 mp = mp->b_cont; 9232 if (mp == NULL) 9233 break; 9234 switch (DB_TYPE(mp)) { 9235 case IRE_DB_TYPE: 9236 case IRE_DB_REQ_TYPE: 9237 if (prev_mp != NULL) 9238 prev_mp->b_cont = mp->b_cont; 9239 mp->b_cont = NULL; 9240 return (mp); 9241 default: 9242 break; 9243 } 9244 } 9245 return (mp); 9246 } 9247 9248 /* 9249 * Timer callback routine for keepalive probe. We do a fake resend of 9250 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9251 * check to see if we have heard anything from the other end for the last 9252 * RTO period. If we have, set the timer to expire for another 9253 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9254 * RTO << 1 and check again when it expires. Keep exponentially increasing 9255 * the timeout if we have not heard from the other side. If for more than 9256 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9257 * kill the connection unless the keepalive abort threshold is 0. In 9258 * that case, we will probe "forever." 9259 */ 9260 static void 9261 tcp_keepalive_killer(void *arg) 9262 { 9263 mblk_t *mp; 9264 conn_t *connp = (conn_t *)arg; 9265 tcp_t *tcp = connp->conn_tcp; 9266 int32_t firetime; 9267 int32_t idletime; 9268 int32_t ka_intrvl; 9269 tcp_stack_t *tcps = tcp->tcp_tcps; 9270 9271 tcp->tcp_ka_tid = 0; 9272 9273 if (tcp->tcp_fused) 9274 return; 9275 9276 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9277 ka_intrvl = tcp->tcp_ka_interval; 9278 9279 /* 9280 * Keepalive probe should only be sent if the application has not 9281 * done a close on the connection. 9282 */ 9283 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9284 return; 9285 } 9286 /* Timer fired too early, restart it. */ 9287 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9288 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9289 MSEC_TO_TICK(ka_intrvl)); 9290 return; 9291 } 9292 9293 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9294 /* 9295 * If we have not heard from the other side for a long 9296 * time, kill the connection unless the keepalive abort 9297 * threshold is 0. In that case, we will probe "forever." 9298 */ 9299 if (tcp->tcp_ka_abort_thres != 0 && 9300 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9301 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9302 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9303 tcp->tcp_client_errno : ETIMEDOUT, 11); 9304 return; 9305 } 9306 9307 if (tcp->tcp_snxt == tcp->tcp_suna && 9308 idletime >= ka_intrvl) { 9309 /* Fake resend of last ACKed byte. */ 9310 mblk_t *mp1 = allocb(1, BPRI_LO); 9311 9312 if (mp1 != NULL) { 9313 *mp1->b_wptr++ = '\0'; 9314 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9315 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9316 freeb(mp1); 9317 /* 9318 * if allocation failed, fall through to start the 9319 * timer back. 9320 */ 9321 if (mp != NULL) { 9322 tcp_send_data(tcp, tcp->tcp_wq, mp); 9323 BUMP_MIB(&tcps->tcps_mib, 9324 tcpTimKeepaliveProbe); 9325 if (tcp->tcp_ka_last_intrvl != 0) { 9326 int max; 9327 /* 9328 * We should probe again at least 9329 * in ka_intrvl, but not more than 9330 * tcp_rexmit_interval_max. 9331 */ 9332 max = tcps->tcps_rexmit_interval_max; 9333 firetime = MIN(ka_intrvl - 1, 9334 tcp->tcp_ka_last_intrvl << 1); 9335 if (firetime > max) 9336 firetime = max; 9337 } else { 9338 firetime = tcp->tcp_rto; 9339 } 9340 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9341 tcp_keepalive_killer, 9342 MSEC_TO_TICK(firetime)); 9343 tcp->tcp_ka_last_intrvl = firetime; 9344 return; 9345 } 9346 } 9347 } else { 9348 tcp->tcp_ka_last_intrvl = 0; 9349 } 9350 9351 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9352 if ((firetime = ka_intrvl - idletime) < 0) { 9353 firetime = ka_intrvl; 9354 } 9355 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9356 MSEC_TO_TICK(firetime)); 9357 } 9358 9359 int 9360 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9361 { 9362 queue_t *q = tcp->tcp_rq; 9363 int32_t mss = tcp->tcp_mss; 9364 int maxpsz; 9365 9366 if (TCP_IS_DETACHED(tcp)) 9367 return (mss); 9368 9369 if (tcp->tcp_fused) { 9370 maxpsz = tcp_fuse_maxpsz_set(tcp); 9371 mss = INFPSZ; 9372 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9373 /* 9374 * Set the sd_qn_maxpsz according to the socket send buffer 9375 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9376 * instruct the stream head to copyin user data into contiguous 9377 * kernel-allocated buffers without breaking it up into smaller 9378 * chunks. We round up the buffer size to the nearest SMSS. 9379 */ 9380 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9381 if (tcp->tcp_kssl_ctx == NULL) 9382 mss = INFPSZ; 9383 else 9384 mss = SSL3_MAX_RECORD_LEN; 9385 } else { 9386 /* 9387 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9388 * (and a multiple of the mss). This instructs the stream 9389 * head to break down larger than SMSS writes into SMSS- 9390 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9391 */ 9392 maxpsz = tcp->tcp_maxpsz * mss; 9393 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9394 maxpsz = tcp->tcp_xmit_hiwater/2; 9395 /* Round up to nearest mss */ 9396 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9397 } 9398 } 9399 (void) setmaxps(q, maxpsz); 9400 tcp->tcp_wq->q_maxpsz = maxpsz; 9401 9402 if (set_maxblk) 9403 (void) mi_set_sth_maxblk(q, mss); 9404 9405 return (mss); 9406 } 9407 9408 /* 9409 * Extract option values from a tcp header. We put any found values into the 9410 * tcpopt struct and return a bitmask saying which options were found. 9411 */ 9412 static int 9413 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9414 { 9415 uchar_t *endp; 9416 int len; 9417 uint32_t mss; 9418 uchar_t *up = (uchar_t *)tcph; 9419 int found = 0; 9420 int32_t sack_len; 9421 tcp_seq sack_begin, sack_end; 9422 tcp_t *tcp; 9423 9424 endp = up + TCP_HDR_LENGTH(tcph); 9425 up += TCP_MIN_HEADER_LENGTH; 9426 while (up < endp) { 9427 len = endp - up; 9428 switch (*up) { 9429 case TCPOPT_EOL: 9430 break; 9431 9432 case TCPOPT_NOP: 9433 up++; 9434 continue; 9435 9436 case TCPOPT_MAXSEG: 9437 if (len < TCPOPT_MAXSEG_LEN || 9438 up[1] != TCPOPT_MAXSEG_LEN) 9439 break; 9440 9441 mss = BE16_TO_U16(up+2); 9442 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9443 tcpopt->tcp_opt_mss = mss; 9444 found |= TCP_OPT_MSS_PRESENT; 9445 9446 up += TCPOPT_MAXSEG_LEN; 9447 continue; 9448 9449 case TCPOPT_WSCALE: 9450 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9451 break; 9452 9453 if (up[2] > TCP_MAX_WINSHIFT) 9454 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9455 else 9456 tcpopt->tcp_opt_wscale = up[2]; 9457 found |= TCP_OPT_WSCALE_PRESENT; 9458 9459 up += TCPOPT_WS_LEN; 9460 continue; 9461 9462 case TCPOPT_SACK_PERMITTED: 9463 if (len < TCPOPT_SACK_OK_LEN || 9464 up[1] != TCPOPT_SACK_OK_LEN) 9465 break; 9466 found |= TCP_OPT_SACK_OK_PRESENT; 9467 up += TCPOPT_SACK_OK_LEN; 9468 continue; 9469 9470 case TCPOPT_SACK: 9471 if (len <= 2 || up[1] <= 2 || len < up[1]) 9472 break; 9473 9474 /* If TCP is not interested in SACK blks... */ 9475 if ((tcp = tcpopt->tcp) == NULL) { 9476 up += up[1]; 9477 continue; 9478 } 9479 sack_len = up[1] - TCPOPT_HEADER_LEN; 9480 up += TCPOPT_HEADER_LEN; 9481 9482 /* 9483 * If the list is empty, allocate one and assume 9484 * nothing is sack'ed. 9485 */ 9486 ASSERT(tcp->tcp_sack_info != NULL); 9487 if (tcp->tcp_notsack_list == NULL) { 9488 tcp_notsack_update(&(tcp->tcp_notsack_list), 9489 tcp->tcp_suna, tcp->tcp_snxt, 9490 &(tcp->tcp_num_notsack_blk), 9491 &(tcp->tcp_cnt_notsack_list)); 9492 9493 /* 9494 * Make sure tcp_notsack_list is not NULL. 9495 * This happens when kmem_alloc(KM_NOSLEEP) 9496 * returns NULL. 9497 */ 9498 if (tcp->tcp_notsack_list == NULL) { 9499 up += sack_len; 9500 continue; 9501 } 9502 tcp->tcp_fack = tcp->tcp_suna; 9503 } 9504 9505 while (sack_len > 0) { 9506 if (up + 8 > endp) { 9507 up = endp; 9508 break; 9509 } 9510 sack_begin = BE32_TO_U32(up); 9511 up += 4; 9512 sack_end = BE32_TO_U32(up); 9513 up += 4; 9514 sack_len -= 8; 9515 /* 9516 * Bounds checking. Make sure the SACK 9517 * info is within tcp_suna and tcp_snxt. 9518 * If this SACK blk is out of bound, ignore 9519 * it but continue to parse the following 9520 * blks. 9521 */ 9522 if (SEQ_LEQ(sack_end, sack_begin) || 9523 SEQ_LT(sack_begin, tcp->tcp_suna) || 9524 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9525 continue; 9526 } 9527 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9528 sack_begin, sack_end, 9529 &(tcp->tcp_num_notsack_blk), 9530 &(tcp->tcp_cnt_notsack_list)); 9531 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9532 tcp->tcp_fack = sack_end; 9533 } 9534 } 9535 found |= TCP_OPT_SACK_PRESENT; 9536 continue; 9537 9538 case TCPOPT_TSTAMP: 9539 if (len < TCPOPT_TSTAMP_LEN || 9540 up[1] != TCPOPT_TSTAMP_LEN) 9541 break; 9542 9543 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9544 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9545 9546 found |= TCP_OPT_TSTAMP_PRESENT; 9547 9548 up += TCPOPT_TSTAMP_LEN; 9549 continue; 9550 9551 default: 9552 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9553 break; 9554 up += up[1]; 9555 continue; 9556 } 9557 break; 9558 } 9559 return (found); 9560 } 9561 9562 /* 9563 * Set the mss associated with a particular tcp based on its current value, 9564 * and a new one passed in. Observe minimums and maximums, and reset 9565 * other state variables that we want to view as multiples of mss. 9566 * 9567 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9568 * highwater marks etc. need to be initialized or adjusted. 9569 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9570 * packet arrives. 9571 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9572 * ICMP6_PACKET_TOO_BIG arrives. 9573 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9574 * to increase the MSS to use the extra bytes available. 9575 * 9576 * Callers except tcp_paws_check() ensure that they only reduce mss. 9577 */ 9578 static void 9579 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9580 { 9581 uint32_t mss_max; 9582 tcp_stack_t *tcps = tcp->tcp_tcps; 9583 9584 if (tcp->tcp_ipversion == IPV4_VERSION) 9585 mss_max = tcps->tcps_mss_max_ipv4; 9586 else 9587 mss_max = tcps->tcps_mss_max_ipv6; 9588 9589 if (mss < tcps->tcps_mss_min) 9590 mss = tcps->tcps_mss_min; 9591 if (mss > mss_max) 9592 mss = mss_max; 9593 /* 9594 * Unless naglim has been set by our client to 9595 * a non-mss value, force naglim to track mss. 9596 * This can help to aggregate small writes. 9597 */ 9598 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9599 tcp->tcp_naglim = mss; 9600 /* 9601 * TCP should be able to buffer at least 4 MSS data for obvious 9602 * performance reason. 9603 */ 9604 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9605 tcp->tcp_xmit_hiwater = mss << 2; 9606 9607 if (do_ss) { 9608 /* 9609 * Either the tcp_cwnd is as yet uninitialized, or mss is 9610 * changing due to a reduction in MTU, presumably as a 9611 * result of a new path component, reset cwnd to its 9612 * "initial" value, as a multiple of the new mss. 9613 */ 9614 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9615 } else { 9616 /* 9617 * Called by tcp_paws_check(), the mss increased 9618 * marginally to allow use of space previously taken 9619 * by the timestamp option. It would be inappropriate 9620 * to apply slow start or tcp_init_cwnd values to 9621 * tcp_cwnd, simply adjust to a multiple of the new mss. 9622 */ 9623 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9624 tcp->tcp_cwnd_cnt = 0; 9625 } 9626 tcp->tcp_mss = mss; 9627 (void) tcp_maxpsz_set(tcp, B_TRUE); 9628 } 9629 9630 /* For /dev/tcp aka AF_INET open */ 9631 static int 9632 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9633 { 9634 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9635 } 9636 9637 /* For /dev/tcp6 aka AF_INET6 open */ 9638 static int 9639 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9640 { 9641 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9642 } 9643 9644 static int 9645 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9646 boolean_t isv6) 9647 { 9648 tcp_t *tcp = NULL; 9649 conn_t *connp; 9650 int err; 9651 vmem_t *minor_arena = NULL; 9652 dev_t conn_dev; 9653 zoneid_t zoneid; 9654 tcp_stack_t *tcps = NULL; 9655 9656 if (q->q_ptr != NULL) 9657 return (0); 9658 9659 if (sflag == MODOPEN) 9660 return (EINVAL); 9661 9662 if (!(flag & SO_ACCEPTOR)) { 9663 /* 9664 * Special case for install: miniroot needs to be able to 9665 * access files via NFS as though it were always in the 9666 * global zone. 9667 */ 9668 if (credp == kcred && nfs_global_client_only != 0) { 9669 zoneid = GLOBAL_ZONEID; 9670 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9671 netstack_tcp; 9672 ASSERT(tcps != NULL); 9673 } else { 9674 netstack_t *ns; 9675 9676 ns = netstack_find_by_cred(credp); 9677 ASSERT(ns != NULL); 9678 tcps = ns->netstack_tcp; 9679 ASSERT(tcps != NULL); 9680 9681 /* 9682 * For exclusive stacks we set the zoneid to zero 9683 * to make TCP operate as if in the global zone. 9684 */ 9685 if (tcps->tcps_netstack->netstack_stackid != 9686 GLOBAL_NETSTACKID) 9687 zoneid = GLOBAL_ZONEID; 9688 else 9689 zoneid = crgetzoneid(credp); 9690 } 9691 /* 9692 * For stackid zero this is done from strplumb.c, but 9693 * non-zero stackids are handled here. 9694 */ 9695 if (tcps->tcps_g_q == NULL && 9696 tcps->tcps_netstack->netstack_stackid != 9697 GLOBAL_NETSTACKID) { 9698 tcp_g_q_setup(tcps); 9699 } 9700 } 9701 9702 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9703 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9704 minor_arena = ip_minor_arena_la; 9705 } else { 9706 /* 9707 * Either minor numbers in the large arena were exhausted 9708 * or a non socket application is doing the open. 9709 * Try to allocate from the small arena. 9710 */ 9711 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9712 if (tcps != NULL) 9713 netstack_rele(tcps->tcps_netstack); 9714 return (EBUSY); 9715 } 9716 minor_arena = ip_minor_arena_sa; 9717 } 9718 ASSERT(minor_arena != NULL); 9719 9720 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9721 9722 if (flag & SO_ACCEPTOR) { 9723 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9724 ASSERT(tcps == NULL); 9725 q->q_qinfo = &tcp_acceptor_rinit; 9726 /* 9727 * the conn_dev and minor_arena will be subsequently used by 9728 * tcp_wput_accept() and tcpclose_accept() to figure out the 9729 * minor device number for this connection from the q_ptr. 9730 */ 9731 RD(q)->q_ptr = (void *)conn_dev; 9732 WR(q)->q_qinfo = &tcp_acceptor_winit; 9733 WR(q)->q_ptr = (void *)minor_arena; 9734 qprocson(q); 9735 return (0); 9736 } 9737 9738 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9739 /* 9740 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9741 * so we drop it by one. 9742 */ 9743 netstack_rele(tcps->tcps_netstack); 9744 if (connp == NULL) { 9745 inet_minor_free(minor_arena, conn_dev); 9746 q->q_ptr = NULL; 9747 return (ENOSR); 9748 } 9749 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9750 tcp = connp->conn_tcp; 9751 9752 q->q_ptr = WR(q)->q_ptr = connp; 9753 if (isv6) { 9754 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9755 connp->conn_send = ip_output_v6; 9756 connp->conn_af_isv6 = B_TRUE; 9757 connp->conn_pkt_isv6 = B_TRUE; 9758 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9759 tcp->tcp_ipversion = IPV6_VERSION; 9760 tcp->tcp_family = AF_INET6; 9761 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9762 } else { 9763 connp->conn_flags |= IPCL_TCP4; 9764 connp->conn_send = ip_output; 9765 connp->conn_af_isv6 = B_FALSE; 9766 connp->conn_pkt_isv6 = B_FALSE; 9767 tcp->tcp_ipversion = IPV4_VERSION; 9768 tcp->tcp_family = AF_INET; 9769 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9770 } 9771 9772 /* 9773 * TCP keeps a copy of cred for cache locality reasons but 9774 * we put a reference only once. If connp->conn_cred 9775 * becomes invalid, tcp_cred should also be set to NULL. 9776 */ 9777 tcp->tcp_cred = connp->conn_cred = credp; 9778 crhold(connp->conn_cred); 9779 tcp->tcp_cpid = curproc->p_pid; 9780 tcp->tcp_open_time = lbolt64; 9781 connp->conn_zoneid = zoneid; 9782 connp->conn_mlp_type = mlptSingle; 9783 connp->conn_ulp_labeled = !is_system_labeled(); 9784 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9785 ASSERT(tcp->tcp_tcps == tcps); 9786 9787 /* 9788 * If the caller has the process-wide flag set, then default to MAC 9789 * exempt mode. This allows read-down to unlabeled hosts. 9790 */ 9791 if (getpflags(NET_MAC_AWARE, credp) != 0) 9792 connp->conn_mac_exempt = B_TRUE; 9793 9794 connp->conn_dev = conn_dev; 9795 connp->conn_minor_arena = minor_arena; 9796 9797 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9798 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9799 9800 if (flag & SO_SOCKSTR) { 9801 /* 9802 * No need to insert a socket in tcp acceptor hash. 9803 * If it was a socket acceptor stream, we dealt with 9804 * it above. A socket listener can never accept a 9805 * connection and doesn't need acceptor_id. 9806 */ 9807 connp->conn_flags |= IPCL_SOCKET; 9808 tcp->tcp_issocket = 1; 9809 WR(q)->q_qinfo = &tcp_sock_winit; 9810 } else { 9811 #ifdef _ILP32 9812 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9813 #else 9814 tcp->tcp_acceptor_id = conn_dev; 9815 #endif /* _ILP32 */ 9816 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9817 } 9818 9819 err = tcp_init(tcp, q); 9820 if (err != 0) { 9821 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 9822 tcp_acceptor_hash_remove(tcp); 9823 CONN_DEC_REF(connp); 9824 q->q_ptr = WR(q)->q_ptr = NULL; 9825 return (err); 9826 } 9827 9828 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9829 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9830 9831 /* Non-zero default values */ 9832 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9833 /* 9834 * Put the ref for TCP. Ref for IP was already put 9835 * by ipcl_conn_create. Also Make the conn_t globally 9836 * visible to walkers 9837 */ 9838 mutex_enter(&connp->conn_lock); 9839 CONN_INC_REF_LOCKED(connp); 9840 ASSERT(connp->conn_ref == 2); 9841 connp->conn_state_flags &= ~CONN_INCIPIENT; 9842 mutex_exit(&connp->conn_lock); 9843 9844 qprocson(q); 9845 return (0); 9846 } 9847 9848 /* 9849 * Some TCP options can be "set" by requesting them in the option 9850 * buffer. This is needed for XTI feature test though we do not 9851 * allow it in general. We interpret that this mechanism is more 9852 * applicable to OSI protocols and need not be allowed in general. 9853 * This routine filters out options for which it is not allowed (most) 9854 * and lets through those (few) for which it is. [ The XTI interface 9855 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9856 * ever implemented will have to be allowed here ]. 9857 */ 9858 static boolean_t 9859 tcp_allow_connopt_set(int level, int name) 9860 { 9861 9862 switch (level) { 9863 case IPPROTO_TCP: 9864 switch (name) { 9865 case TCP_NODELAY: 9866 return (B_TRUE); 9867 default: 9868 return (B_FALSE); 9869 } 9870 /*NOTREACHED*/ 9871 default: 9872 return (B_FALSE); 9873 } 9874 /*NOTREACHED*/ 9875 } 9876 9877 /* 9878 * This routine gets default values of certain options whose default 9879 * values are maintained by protocol specific code 9880 */ 9881 /* ARGSUSED */ 9882 int 9883 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9884 { 9885 int32_t *i1 = (int32_t *)ptr; 9886 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9887 9888 switch (level) { 9889 case IPPROTO_TCP: 9890 switch (name) { 9891 case TCP_NOTIFY_THRESHOLD: 9892 *i1 = tcps->tcps_ip_notify_interval; 9893 break; 9894 case TCP_ABORT_THRESHOLD: 9895 *i1 = tcps->tcps_ip_abort_interval; 9896 break; 9897 case TCP_CONN_NOTIFY_THRESHOLD: 9898 *i1 = tcps->tcps_ip_notify_cinterval; 9899 break; 9900 case TCP_CONN_ABORT_THRESHOLD: 9901 *i1 = tcps->tcps_ip_abort_cinterval; 9902 break; 9903 default: 9904 return (-1); 9905 } 9906 break; 9907 case IPPROTO_IP: 9908 switch (name) { 9909 case IP_TTL: 9910 *i1 = tcps->tcps_ipv4_ttl; 9911 break; 9912 default: 9913 return (-1); 9914 } 9915 break; 9916 case IPPROTO_IPV6: 9917 switch (name) { 9918 case IPV6_UNICAST_HOPS: 9919 *i1 = tcps->tcps_ipv6_hoplimit; 9920 break; 9921 default: 9922 return (-1); 9923 } 9924 break; 9925 default: 9926 return (-1); 9927 } 9928 return (sizeof (int)); 9929 } 9930 9931 9932 /* 9933 * TCP routine to get the values of options. 9934 */ 9935 int 9936 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9937 { 9938 int *i1 = (int *)ptr; 9939 conn_t *connp = Q_TO_CONN(q); 9940 tcp_t *tcp = connp->conn_tcp; 9941 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9942 9943 switch (level) { 9944 case SOL_SOCKET: 9945 switch (name) { 9946 case SO_LINGER: { 9947 struct linger *lgr = (struct linger *)ptr; 9948 9949 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9950 lgr->l_linger = tcp->tcp_lingertime; 9951 } 9952 return (sizeof (struct linger)); 9953 case SO_DEBUG: 9954 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9955 break; 9956 case SO_KEEPALIVE: 9957 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9958 break; 9959 case SO_DONTROUTE: 9960 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9961 break; 9962 case SO_USELOOPBACK: 9963 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9964 break; 9965 case SO_BROADCAST: 9966 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9967 break; 9968 case SO_REUSEADDR: 9969 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9970 break; 9971 case SO_OOBINLINE: 9972 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9973 break; 9974 case SO_DGRAM_ERRIND: 9975 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9976 break; 9977 case SO_TYPE: 9978 *i1 = SOCK_STREAM; 9979 break; 9980 case SO_SNDBUF: 9981 *i1 = tcp->tcp_xmit_hiwater; 9982 break; 9983 case SO_RCVBUF: 9984 *i1 = RD(q)->q_hiwat; 9985 break; 9986 case SO_SND_COPYAVOID: 9987 *i1 = tcp->tcp_snd_zcopy_on ? 9988 SO_SND_COPYAVOID : 0; 9989 break; 9990 case SO_ALLZONES: 9991 *i1 = connp->conn_allzones ? 1 : 0; 9992 break; 9993 case SO_ANON_MLP: 9994 *i1 = connp->conn_anon_mlp; 9995 break; 9996 case SO_MAC_EXEMPT: 9997 *i1 = connp->conn_mac_exempt; 9998 break; 9999 case SO_EXCLBIND: 10000 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 10001 break; 10002 case SO_PROTOTYPE: 10003 *i1 = IPPROTO_TCP; 10004 break; 10005 case SO_DOMAIN: 10006 *i1 = tcp->tcp_family; 10007 break; 10008 default: 10009 return (-1); 10010 } 10011 break; 10012 case IPPROTO_TCP: 10013 switch (name) { 10014 case TCP_NODELAY: 10015 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 10016 break; 10017 case TCP_MAXSEG: 10018 *i1 = tcp->tcp_mss; 10019 break; 10020 case TCP_NOTIFY_THRESHOLD: 10021 *i1 = (int)tcp->tcp_first_timer_threshold; 10022 break; 10023 case TCP_ABORT_THRESHOLD: 10024 *i1 = tcp->tcp_second_timer_threshold; 10025 break; 10026 case TCP_CONN_NOTIFY_THRESHOLD: 10027 *i1 = tcp->tcp_first_ctimer_threshold; 10028 break; 10029 case TCP_CONN_ABORT_THRESHOLD: 10030 *i1 = tcp->tcp_second_ctimer_threshold; 10031 break; 10032 case TCP_RECVDSTADDR: 10033 *i1 = tcp->tcp_recvdstaddr; 10034 break; 10035 case TCP_ANONPRIVBIND: 10036 *i1 = tcp->tcp_anon_priv_bind; 10037 break; 10038 case TCP_EXCLBIND: 10039 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 10040 break; 10041 case TCP_INIT_CWND: 10042 *i1 = tcp->tcp_init_cwnd; 10043 break; 10044 case TCP_KEEPALIVE_THRESHOLD: 10045 *i1 = tcp->tcp_ka_interval; 10046 break; 10047 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10048 *i1 = tcp->tcp_ka_abort_thres; 10049 break; 10050 case TCP_CORK: 10051 *i1 = tcp->tcp_cork; 10052 break; 10053 default: 10054 return (-1); 10055 } 10056 break; 10057 case IPPROTO_IP: 10058 if (tcp->tcp_family != AF_INET) 10059 return (-1); 10060 switch (name) { 10061 case IP_OPTIONS: 10062 case T_IP_OPTIONS: { 10063 /* 10064 * This is compatible with BSD in that in only return 10065 * the reverse source route with the final destination 10066 * as the last entry. The first 4 bytes of the option 10067 * will contain the final destination. 10068 */ 10069 int opt_len; 10070 10071 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10072 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10073 ASSERT(opt_len >= 0); 10074 /* Caller ensures enough space */ 10075 if (opt_len > 0) { 10076 /* 10077 * TODO: Do we have to handle getsockopt on an 10078 * initiator as well? 10079 */ 10080 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10081 } 10082 return (0); 10083 } 10084 case IP_TOS: 10085 case T_IP_TOS: 10086 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10087 break; 10088 case IP_TTL: 10089 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10090 break; 10091 case IP_NEXTHOP: 10092 /* Handled at IP level */ 10093 return (-EINVAL); 10094 default: 10095 return (-1); 10096 } 10097 break; 10098 case IPPROTO_IPV6: 10099 /* 10100 * IPPROTO_IPV6 options are only supported for sockets 10101 * that are using IPv6 on the wire. 10102 */ 10103 if (tcp->tcp_ipversion != IPV6_VERSION) { 10104 return (-1); 10105 } 10106 switch (name) { 10107 case IPV6_UNICAST_HOPS: 10108 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10109 break; /* goto sizeof (int) option return */ 10110 case IPV6_BOUND_IF: 10111 /* Zero if not set */ 10112 *i1 = tcp->tcp_bound_if; 10113 break; /* goto sizeof (int) option return */ 10114 case IPV6_RECVPKTINFO: 10115 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10116 *i1 = 1; 10117 else 10118 *i1 = 0; 10119 break; /* goto sizeof (int) option return */ 10120 case IPV6_RECVTCLASS: 10121 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10122 *i1 = 1; 10123 else 10124 *i1 = 0; 10125 break; /* goto sizeof (int) option return */ 10126 case IPV6_RECVHOPLIMIT: 10127 if (tcp->tcp_ipv6_recvancillary & 10128 TCP_IPV6_RECVHOPLIMIT) 10129 *i1 = 1; 10130 else 10131 *i1 = 0; 10132 break; /* goto sizeof (int) option return */ 10133 case IPV6_RECVHOPOPTS: 10134 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10135 *i1 = 1; 10136 else 10137 *i1 = 0; 10138 break; /* goto sizeof (int) option return */ 10139 case IPV6_RECVDSTOPTS: 10140 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10141 *i1 = 1; 10142 else 10143 *i1 = 0; 10144 break; /* goto sizeof (int) option return */ 10145 case _OLD_IPV6_RECVDSTOPTS: 10146 if (tcp->tcp_ipv6_recvancillary & 10147 TCP_OLD_IPV6_RECVDSTOPTS) 10148 *i1 = 1; 10149 else 10150 *i1 = 0; 10151 break; /* goto sizeof (int) option return */ 10152 case IPV6_RECVRTHDR: 10153 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10154 *i1 = 1; 10155 else 10156 *i1 = 0; 10157 break; /* goto sizeof (int) option return */ 10158 case IPV6_RECVRTHDRDSTOPTS: 10159 if (tcp->tcp_ipv6_recvancillary & 10160 TCP_IPV6_RECVRTDSTOPTS) 10161 *i1 = 1; 10162 else 10163 *i1 = 0; 10164 break; /* goto sizeof (int) option return */ 10165 case IPV6_PKTINFO: { 10166 /* XXX assumes that caller has room for max size! */ 10167 struct in6_pktinfo *pkti; 10168 10169 pkti = (struct in6_pktinfo *)ptr; 10170 if (ipp->ipp_fields & IPPF_IFINDEX) 10171 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10172 else 10173 pkti->ipi6_ifindex = 0; 10174 if (ipp->ipp_fields & IPPF_ADDR) 10175 pkti->ipi6_addr = ipp->ipp_addr; 10176 else 10177 pkti->ipi6_addr = ipv6_all_zeros; 10178 return (sizeof (struct in6_pktinfo)); 10179 } 10180 case IPV6_TCLASS: 10181 if (ipp->ipp_fields & IPPF_TCLASS) 10182 *i1 = ipp->ipp_tclass; 10183 else 10184 *i1 = IPV6_FLOW_TCLASS( 10185 IPV6_DEFAULT_VERS_AND_FLOW); 10186 break; /* goto sizeof (int) option return */ 10187 case IPV6_NEXTHOP: { 10188 sin6_t *sin6 = (sin6_t *)ptr; 10189 10190 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10191 return (0); 10192 *sin6 = sin6_null; 10193 sin6->sin6_family = AF_INET6; 10194 sin6->sin6_addr = ipp->ipp_nexthop; 10195 return (sizeof (sin6_t)); 10196 } 10197 case IPV6_HOPOPTS: 10198 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10199 return (0); 10200 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10201 return (0); 10202 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10203 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10204 if (tcp->tcp_label_len > 0) { 10205 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10206 ptr[1] = (ipp->ipp_hopoptslen - 10207 tcp->tcp_label_len + 7) / 8 - 1; 10208 } 10209 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10210 case IPV6_RTHDRDSTOPTS: 10211 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10212 return (0); 10213 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10214 return (ipp->ipp_rtdstoptslen); 10215 case IPV6_RTHDR: 10216 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10217 return (0); 10218 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10219 return (ipp->ipp_rthdrlen); 10220 case IPV6_DSTOPTS: 10221 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10222 return (0); 10223 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10224 return (ipp->ipp_dstoptslen); 10225 case IPV6_SRC_PREFERENCES: 10226 return (ip6_get_src_preferences(connp, 10227 (uint32_t *)ptr)); 10228 case IPV6_PATHMTU: { 10229 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10230 10231 if (tcp->tcp_state < TCPS_ESTABLISHED) 10232 return (-1); 10233 10234 return (ip_fill_mtuinfo(&connp->conn_remv6, 10235 connp->conn_fport, mtuinfo, 10236 connp->conn_netstack)); 10237 } 10238 default: 10239 return (-1); 10240 } 10241 break; 10242 default: 10243 return (-1); 10244 } 10245 return (sizeof (int)); 10246 } 10247 10248 /* 10249 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10250 * Parameters are assumed to be verified by the caller. 10251 */ 10252 /* ARGSUSED */ 10253 int 10254 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10255 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10256 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10257 { 10258 conn_t *connp = Q_TO_CONN(q); 10259 tcp_t *tcp = connp->conn_tcp; 10260 int *i1 = (int *)invalp; 10261 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10262 boolean_t checkonly; 10263 int reterr; 10264 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10265 10266 switch (optset_context) { 10267 case SETFN_OPTCOM_CHECKONLY: 10268 checkonly = B_TRUE; 10269 /* 10270 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10271 * inlen != 0 implies value supplied and 10272 * we have to "pretend" to set it. 10273 * inlen == 0 implies that there is no 10274 * value part in T_CHECK request and just validation 10275 * done elsewhere should be enough, we just return here. 10276 */ 10277 if (inlen == 0) { 10278 *outlenp = 0; 10279 return (0); 10280 } 10281 break; 10282 case SETFN_OPTCOM_NEGOTIATE: 10283 checkonly = B_FALSE; 10284 break; 10285 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10286 case SETFN_CONN_NEGOTIATE: 10287 checkonly = B_FALSE; 10288 /* 10289 * Negotiating local and "association-related" options 10290 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10291 * primitives is allowed by XTI, but we choose 10292 * to not implement this style negotiation for Internet 10293 * protocols (We interpret it is a must for OSI world but 10294 * optional for Internet protocols) for all options. 10295 * [ Will do only for the few options that enable test 10296 * suites that our XTI implementation of this feature 10297 * works for transports that do allow it ] 10298 */ 10299 if (!tcp_allow_connopt_set(level, name)) { 10300 *outlenp = 0; 10301 return (EINVAL); 10302 } 10303 break; 10304 default: 10305 /* 10306 * We should never get here 10307 */ 10308 *outlenp = 0; 10309 return (EINVAL); 10310 } 10311 10312 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10313 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10314 10315 /* 10316 * For TCP, we should have no ancillary data sent down 10317 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10318 * has to be zero. 10319 */ 10320 ASSERT(thisdg_attrs == NULL); 10321 10322 /* 10323 * For fixed length options, no sanity check 10324 * of passed in length is done. It is assumed *_optcom_req() 10325 * routines do the right thing. 10326 */ 10327 10328 switch (level) { 10329 case SOL_SOCKET: 10330 switch (name) { 10331 case SO_LINGER: { 10332 struct linger *lgr = (struct linger *)invalp; 10333 10334 if (!checkonly) { 10335 if (lgr->l_onoff) { 10336 tcp->tcp_linger = 1; 10337 tcp->tcp_lingertime = lgr->l_linger; 10338 } else { 10339 tcp->tcp_linger = 0; 10340 tcp->tcp_lingertime = 0; 10341 } 10342 /* struct copy */ 10343 *(struct linger *)outvalp = *lgr; 10344 } else { 10345 if (!lgr->l_onoff) { 10346 ((struct linger *) 10347 outvalp)->l_onoff = 0; 10348 ((struct linger *) 10349 outvalp)->l_linger = 0; 10350 } else { 10351 /* struct copy */ 10352 *(struct linger *)outvalp = *lgr; 10353 } 10354 } 10355 *outlenp = sizeof (struct linger); 10356 return (0); 10357 } 10358 case SO_DEBUG: 10359 if (!checkonly) 10360 tcp->tcp_debug = onoff; 10361 break; 10362 case SO_KEEPALIVE: 10363 if (checkonly) { 10364 /* T_CHECK case */ 10365 break; 10366 } 10367 10368 if (!onoff) { 10369 if (tcp->tcp_ka_enabled) { 10370 if (tcp->tcp_ka_tid != 0) { 10371 (void) TCP_TIMER_CANCEL(tcp, 10372 tcp->tcp_ka_tid); 10373 tcp->tcp_ka_tid = 0; 10374 } 10375 tcp->tcp_ka_enabled = 0; 10376 } 10377 break; 10378 } 10379 if (!tcp->tcp_ka_enabled) { 10380 /* Crank up the keepalive timer */ 10381 tcp->tcp_ka_last_intrvl = 0; 10382 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10383 tcp_keepalive_killer, 10384 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10385 tcp->tcp_ka_enabled = 1; 10386 } 10387 break; 10388 case SO_DONTROUTE: 10389 /* 10390 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10391 * only of interest to IP. We track them here only so 10392 * that we can report their current value. 10393 */ 10394 if (!checkonly) { 10395 tcp->tcp_dontroute = onoff; 10396 tcp->tcp_connp->conn_dontroute = onoff; 10397 } 10398 break; 10399 case SO_USELOOPBACK: 10400 if (!checkonly) { 10401 tcp->tcp_useloopback = onoff; 10402 tcp->tcp_connp->conn_loopback = onoff; 10403 } 10404 break; 10405 case SO_BROADCAST: 10406 if (!checkonly) { 10407 tcp->tcp_broadcast = onoff; 10408 tcp->tcp_connp->conn_broadcast = onoff; 10409 } 10410 break; 10411 case SO_REUSEADDR: 10412 if (!checkonly) { 10413 tcp->tcp_reuseaddr = onoff; 10414 tcp->tcp_connp->conn_reuseaddr = onoff; 10415 } 10416 break; 10417 case SO_OOBINLINE: 10418 if (!checkonly) 10419 tcp->tcp_oobinline = onoff; 10420 break; 10421 case SO_DGRAM_ERRIND: 10422 if (!checkonly) 10423 tcp->tcp_dgram_errind = onoff; 10424 break; 10425 case SO_SNDBUF: { 10426 if (*i1 > tcps->tcps_max_buf) { 10427 *outlenp = 0; 10428 return (ENOBUFS); 10429 } 10430 if (checkonly) 10431 break; 10432 10433 tcp->tcp_xmit_hiwater = *i1; 10434 if (tcps->tcps_snd_lowat_fraction != 0) 10435 tcp->tcp_xmit_lowater = 10436 tcp->tcp_xmit_hiwater / 10437 tcps->tcps_snd_lowat_fraction; 10438 (void) tcp_maxpsz_set(tcp, B_TRUE); 10439 /* 10440 * If we are flow-controlled, recheck the condition. 10441 * There are apps that increase SO_SNDBUF size when 10442 * flow-controlled (EWOULDBLOCK), and expect the flow 10443 * control condition to be lifted right away. 10444 */ 10445 mutex_enter(&tcp->tcp_non_sq_lock); 10446 if (tcp->tcp_flow_stopped && 10447 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10448 tcp_clrqfull(tcp); 10449 } 10450 mutex_exit(&tcp->tcp_non_sq_lock); 10451 break; 10452 } 10453 case SO_RCVBUF: 10454 if (*i1 > tcps->tcps_max_buf) { 10455 *outlenp = 0; 10456 return (ENOBUFS); 10457 } 10458 /* Silently ignore zero */ 10459 if (!checkonly && *i1 != 0) { 10460 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10461 (void) tcp_rwnd_set(tcp, *i1); 10462 } 10463 /* 10464 * XXX should we return the rwnd here 10465 * and tcp_opt_get ? 10466 */ 10467 break; 10468 case SO_SND_COPYAVOID: 10469 if (!checkonly) { 10470 /* we only allow enable at most once for now */ 10471 if (tcp->tcp_loopback || 10472 (tcp->tcp_kssl_ctx != NULL) || 10473 (!tcp->tcp_snd_zcopy_aware && 10474 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10475 *outlenp = 0; 10476 return (EOPNOTSUPP); 10477 } 10478 tcp->tcp_snd_zcopy_aware = 1; 10479 } 10480 break; 10481 case SO_ALLZONES: 10482 /* Pass option along to IP level for handling */ 10483 return (-EINVAL); 10484 case SO_ANON_MLP: 10485 /* Pass option along to IP level for handling */ 10486 return (-EINVAL); 10487 case SO_MAC_EXEMPT: 10488 /* Pass option along to IP level for handling */ 10489 return (-EINVAL); 10490 case SO_EXCLBIND: 10491 if (!checkonly) 10492 tcp->tcp_exclbind = onoff; 10493 break; 10494 default: 10495 *outlenp = 0; 10496 return (EINVAL); 10497 } 10498 break; 10499 case IPPROTO_TCP: 10500 switch (name) { 10501 case TCP_NODELAY: 10502 if (!checkonly) 10503 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10504 break; 10505 case TCP_NOTIFY_THRESHOLD: 10506 if (!checkonly) 10507 tcp->tcp_first_timer_threshold = *i1; 10508 break; 10509 case TCP_ABORT_THRESHOLD: 10510 if (!checkonly) 10511 tcp->tcp_second_timer_threshold = *i1; 10512 break; 10513 case TCP_CONN_NOTIFY_THRESHOLD: 10514 if (!checkonly) 10515 tcp->tcp_first_ctimer_threshold = *i1; 10516 break; 10517 case TCP_CONN_ABORT_THRESHOLD: 10518 if (!checkonly) 10519 tcp->tcp_second_ctimer_threshold = *i1; 10520 break; 10521 case TCP_RECVDSTADDR: 10522 if (tcp->tcp_state > TCPS_LISTEN) 10523 return (EOPNOTSUPP); 10524 if (!checkonly) 10525 tcp->tcp_recvdstaddr = onoff; 10526 break; 10527 case TCP_ANONPRIVBIND: 10528 if ((reterr = secpolicy_net_privaddr(cr, 0, 10529 IPPROTO_TCP)) != 0) { 10530 *outlenp = 0; 10531 return (reterr); 10532 } 10533 if (!checkonly) { 10534 tcp->tcp_anon_priv_bind = onoff; 10535 } 10536 break; 10537 case TCP_EXCLBIND: 10538 if (!checkonly) 10539 tcp->tcp_exclbind = onoff; 10540 break; /* goto sizeof (int) option return */ 10541 case TCP_INIT_CWND: { 10542 uint32_t init_cwnd = *((uint32_t *)invalp); 10543 10544 if (checkonly) 10545 break; 10546 10547 /* 10548 * Only allow socket with network configuration 10549 * privilege to set the initial cwnd to be larger 10550 * than allowed by RFC 3390. 10551 */ 10552 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10553 tcp->tcp_init_cwnd = init_cwnd; 10554 break; 10555 } 10556 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10557 *outlenp = 0; 10558 return (reterr); 10559 } 10560 if (init_cwnd > TCP_MAX_INIT_CWND) { 10561 *outlenp = 0; 10562 return (EINVAL); 10563 } 10564 tcp->tcp_init_cwnd = init_cwnd; 10565 break; 10566 } 10567 case TCP_KEEPALIVE_THRESHOLD: 10568 if (checkonly) 10569 break; 10570 10571 if (*i1 < tcps->tcps_keepalive_interval_low || 10572 *i1 > tcps->tcps_keepalive_interval_high) { 10573 *outlenp = 0; 10574 return (EINVAL); 10575 } 10576 if (*i1 != tcp->tcp_ka_interval) { 10577 tcp->tcp_ka_interval = *i1; 10578 /* 10579 * Check if we need to restart the 10580 * keepalive timer. 10581 */ 10582 if (tcp->tcp_ka_tid != 0) { 10583 ASSERT(tcp->tcp_ka_enabled); 10584 (void) TCP_TIMER_CANCEL(tcp, 10585 tcp->tcp_ka_tid); 10586 tcp->tcp_ka_last_intrvl = 0; 10587 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10588 tcp_keepalive_killer, 10589 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10590 } 10591 } 10592 break; 10593 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10594 if (!checkonly) { 10595 if (*i1 < 10596 tcps->tcps_keepalive_abort_interval_low || 10597 *i1 > 10598 tcps->tcps_keepalive_abort_interval_high) { 10599 *outlenp = 0; 10600 return (EINVAL); 10601 } 10602 tcp->tcp_ka_abort_thres = *i1; 10603 } 10604 break; 10605 case TCP_CORK: 10606 if (!checkonly) { 10607 /* 10608 * if tcp->tcp_cork was set and is now 10609 * being unset, we have to make sure that 10610 * the remaining data gets sent out. Also 10611 * unset tcp->tcp_cork so that tcp_wput_data() 10612 * can send data even if it is less than mss 10613 */ 10614 if (tcp->tcp_cork && onoff == 0 && 10615 tcp->tcp_unsent > 0) { 10616 tcp->tcp_cork = B_FALSE; 10617 tcp_wput_data(tcp, NULL, B_FALSE); 10618 } 10619 tcp->tcp_cork = onoff; 10620 } 10621 break; 10622 default: 10623 *outlenp = 0; 10624 return (EINVAL); 10625 } 10626 break; 10627 case IPPROTO_IP: 10628 if (tcp->tcp_family != AF_INET) { 10629 *outlenp = 0; 10630 return (ENOPROTOOPT); 10631 } 10632 switch (name) { 10633 case IP_OPTIONS: 10634 case T_IP_OPTIONS: 10635 reterr = tcp_opt_set_header(tcp, checkonly, 10636 invalp, inlen); 10637 if (reterr) { 10638 *outlenp = 0; 10639 return (reterr); 10640 } 10641 /* OK return - copy input buffer into output buffer */ 10642 if (invalp != outvalp) { 10643 /* don't trust bcopy for identical src/dst */ 10644 bcopy(invalp, outvalp, inlen); 10645 } 10646 *outlenp = inlen; 10647 return (0); 10648 case IP_TOS: 10649 case T_IP_TOS: 10650 if (!checkonly) { 10651 tcp->tcp_ipha->ipha_type_of_service = 10652 (uchar_t)*i1; 10653 tcp->tcp_tos = (uchar_t)*i1; 10654 } 10655 break; 10656 case IP_TTL: 10657 if (!checkonly) { 10658 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10659 tcp->tcp_ttl = (uchar_t)*i1; 10660 } 10661 break; 10662 case IP_BOUND_IF: 10663 case IP_NEXTHOP: 10664 /* Handled at the IP level */ 10665 return (-EINVAL); 10666 case IP_SEC_OPT: 10667 /* 10668 * We should not allow policy setting after 10669 * we start listening for connections. 10670 */ 10671 if (tcp->tcp_state == TCPS_LISTEN) { 10672 return (EINVAL); 10673 } else { 10674 /* Handled at the IP level */ 10675 return (-EINVAL); 10676 } 10677 default: 10678 *outlenp = 0; 10679 return (EINVAL); 10680 } 10681 break; 10682 case IPPROTO_IPV6: { 10683 ip6_pkt_t *ipp; 10684 10685 /* 10686 * IPPROTO_IPV6 options are only supported for sockets 10687 * that are using IPv6 on the wire. 10688 */ 10689 if (tcp->tcp_ipversion != IPV6_VERSION) { 10690 *outlenp = 0; 10691 return (ENOPROTOOPT); 10692 } 10693 /* 10694 * Only sticky options; no ancillary data 10695 */ 10696 ASSERT(thisdg_attrs == NULL); 10697 ipp = &tcp->tcp_sticky_ipp; 10698 10699 switch (name) { 10700 case IPV6_UNICAST_HOPS: 10701 /* -1 means use default */ 10702 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10703 *outlenp = 0; 10704 return (EINVAL); 10705 } 10706 if (!checkonly) { 10707 if (*i1 == -1) { 10708 tcp->tcp_ip6h->ip6_hops = 10709 ipp->ipp_unicast_hops = 10710 (uint8_t)tcps->tcps_ipv6_hoplimit; 10711 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10712 /* Pass modified value to IP. */ 10713 *i1 = tcp->tcp_ip6h->ip6_hops; 10714 } else { 10715 tcp->tcp_ip6h->ip6_hops = 10716 ipp->ipp_unicast_hops = 10717 (uint8_t)*i1; 10718 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10719 } 10720 reterr = tcp_build_hdrs(q, tcp); 10721 if (reterr != 0) 10722 return (reterr); 10723 } 10724 break; 10725 case IPV6_BOUND_IF: 10726 if (!checkonly) { 10727 int error = 0; 10728 10729 tcp->tcp_bound_if = *i1; 10730 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10731 B_TRUE, checkonly, level, name, mblk); 10732 if (error != 0) { 10733 *outlenp = 0; 10734 return (error); 10735 } 10736 } 10737 break; 10738 /* 10739 * Set boolean switches for ancillary data delivery 10740 */ 10741 case IPV6_RECVPKTINFO: 10742 if (!checkonly) { 10743 if (onoff) 10744 tcp->tcp_ipv6_recvancillary |= 10745 TCP_IPV6_RECVPKTINFO; 10746 else 10747 tcp->tcp_ipv6_recvancillary &= 10748 ~TCP_IPV6_RECVPKTINFO; 10749 /* Force it to be sent up with the next msg */ 10750 tcp->tcp_recvifindex = 0; 10751 } 10752 break; 10753 case IPV6_RECVTCLASS: 10754 if (!checkonly) { 10755 if (onoff) 10756 tcp->tcp_ipv6_recvancillary |= 10757 TCP_IPV6_RECVTCLASS; 10758 else 10759 tcp->tcp_ipv6_recvancillary &= 10760 ~TCP_IPV6_RECVTCLASS; 10761 } 10762 break; 10763 case IPV6_RECVHOPLIMIT: 10764 if (!checkonly) { 10765 if (onoff) 10766 tcp->tcp_ipv6_recvancillary |= 10767 TCP_IPV6_RECVHOPLIMIT; 10768 else 10769 tcp->tcp_ipv6_recvancillary &= 10770 ~TCP_IPV6_RECVHOPLIMIT; 10771 /* Force it to be sent up with the next msg */ 10772 tcp->tcp_recvhops = 0xffffffffU; 10773 } 10774 break; 10775 case IPV6_RECVHOPOPTS: 10776 if (!checkonly) { 10777 if (onoff) 10778 tcp->tcp_ipv6_recvancillary |= 10779 TCP_IPV6_RECVHOPOPTS; 10780 else 10781 tcp->tcp_ipv6_recvancillary &= 10782 ~TCP_IPV6_RECVHOPOPTS; 10783 } 10784 break; 10785 case IPV6_RECVDSTOPTS: 10786 if (!checkonly) { 10787 if (onoff) 10788 tcp->tcp_ipv6_recvancillary |= 10789 TCP_IPV6_RECVDSTOPTS; 10790 else 10791 tcp->tcp_ipv6_recvancillary &= 10792 ~TCP_IPV6_RECVDSTOPTS; 10793 } 10794 break; 10795 case _OLD_IPV6_RECVDSTOPTS: 10796 if (!checkonly) { 10797 if (onoff) 10798 tcp->tcp_ipv6_recvancillary |= 10799 TCP_OLD_IPV6_RECVDSTOPTS; 10800 else 10801 tcp->tcp_ipv6_recvancillary &= 10802 ~TCP_OLD_IPV6_RECVDSTOPTS; 10803 } 10804 break; 10805 case IPV6_RECVRTHDR: 10806 if (!checkonly) { 10807 if (onoff) 10808 tcp->tcp_ipv6_recvancillary |= 10809 TCP_IPV6_RECVRTHDR; 10810 else 10811 tcp->tcp_ipv6_recvancillary &= 10812 ~TCP_IPV6_RECVRTHDR; 10813 } 10814 break; 10815 case IPV6_RECVRTHDRDSTOPTS: 10816 if (!checkonly) { 10817 if (onoff) 10818 tcp->tcp_ipv6_recvancillary |= 10819 TCP_IPV6_RECVRTDSTOPTS; 10820 else 10821 tcp->tcp_ipv6_recvancillary &= 10822 ~TCP_IPV6_RECVRTDSTOPTS; 10823 } 10824 break; 10825 case IPV6_PKTINFO: 10826 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10827 return (EINVAL); 10828 if (checkonly) 10829 break; 10830 10831 if (inlen == 0) { 10832 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10833 } else { 10834 struct in6_pktinfo *pkti; 10835 10836 pkti = (struct in6_pktinfo *)invalp; 10837 /* 10838 * RFC 3542 states that ipi6_addr must be 10839 * the unspecified address when setting the 10840 * IPV6_PKTINFO sticky socket option on a 10841 * TCP socket. 10842 */ 10843 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10844 return (EINVAL); 10845 /* 10846 * ip6_set_pktinfo() validates the source 10847 * address and interface index. 10848 */ 10849 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10850 pkti, mblk); 10851 if (reterr != 0) 10852 return (reterr); 10853 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10854 ipp->ipp_addr = pkti->ipi6_addr; 10855 if (ipp->ipp_ifindex != 0) 10856 ipp->ipp_fields |= IPPF_IFINDEX; 10857 else 10858 ipp->ipp_fields &= ~IPPF_IFINDEX; 10859 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10860 ipp->ipp_fields |= IPPF_ADDR; 10861 else 10862 ipp->ipp_fields &= ~IPPF_ADDR; 10863 } 10864 reterr = tcp_build_hdrs(q, tcp); 10865 if (reterr != 0) 10866 return (reterr); 10867 break; 10868 case IPV6_TCLASS: 10869 if (inlen != 0 && inlen != sizeof (int)) 10870 return (EINVAL); 10871 if (checkonly) 10872 break; 10873 10874 if (inlen == 0) { 10875 ipp->ipp_fields &= ~IPPF_TCLASS; 10876 } else { 10877 if (*i1 > 255 || *i1 < -1) 10878 return (EINVAL); 10879 if (*i1 == -1) { 10880 ipp->ipp_tclass = 0; 10881 *i1 = 0; 10882 } else { 10883 ipp->ipp_tclass = *i1; 10884 } 10885 ipp->ipp_fields |= IPPF_TCLASS; 10886 } 10887 reterr = tcp_build_hdrs(q, tcp); 10888 if (reterr != 0) 10889 return (reterr); 10890 break; 10891 case IPV6_NEXTHOP: 10892 /* 10893 * IP will verify that the nexthop is reachable 10894 * and fail for sticky options. 10895 */ 10896 if (inlen != 0 && inlen != sizeof (sin6_t)) 10897 return (EINVAL); 10898 if (checkonly) 10899 break; 10900 10901 if (inlen == 0) { 10902 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10903 } else { 10904 sin6_t *sin6 = (sin6_t *)invalp; 10905 10906 if (sin6->sin6_family != AF_INET6) 10907 return (EAFNOSUPPORT); 10908 if (IN6_IS_ADDR_V4MAPPED( 10909 &sin6->sin6_addr)) 10910 return (EADDRNOTAVAIL); 10911 ipp->ipp_nexthop = sin6->sin6_addr; 10912 if (!IN6_IS_ADDR_UNSPECIFIED( 10913 &ipp->ipp_nexthop)) 10914 ipp->ipp_fields |= IPPF_NEXTHOP; 10915 else 10916 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10917 } 10918 reterr = tcp_build_hdrs(q, tcp); 10919 if (reterr != 0) 10920 return (reterr); 10921 break; 10922 case IPV6_HOPOPTS: { 10923 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10924 10925 /* 10926 * Sanity checks - minimum size, size a multiple of 10927 * eight bytes, and matching size passed in. 10928 */ 10929 if (inlen != 0 && 10930 inlen != (8 * (hopts->ip6h_len + 1))) 10931 return (EINVAL); 10932 10933 if (checkonly) 10934 break; 10935 10936 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10937 (uchar_t **)&ipp->ipp_hopopts, 10938 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10939 if (reterr != 0) 10940 return (reterr); 10941 if (ipp->ipp_hopoptslen == 0) 10942 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10943 else 10944 ipp->ipp_fields |= IPPF_HOPOPTS; 10945 reterr = tcp_build_hdrs(q, tcp); 10946 if (reterr != 0) 10947 return (reterr); 10948 break; 10949 } 10950 case IPV6_RTHDRDSTOPTS: { 10951 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10952 10953 /* 10954 * Sanity checks - minimum size, size a multiple of 10955 * eight bytes, and matching size passed in. 10956 */ 10957 if (inlen != 0 && 10958 inlen != (8 * (dopts->ip6d_len + 1))) 10959 return (EINVAL); 10960 10961 if (checkonly) 10962 break; 10963 10964 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10965 (uchar_t **)&ipp->ipp_rtdstopts, 10966 &ipp->ipp_rtdstoptslen, 0); 10967 if (reterr != 0) 10968 return (reterr); 10969 if (ipp->ipp_rtdstoptslen == 0) 10970 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10971 else 10972 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10973 reterr = tcp_build_hdrs(q, tcp); 10974 if (reterr != 0) 10975 return (reterr); 10976 break; 10977 } 10978 case IPV6_DSTOPTS: { 10979 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10980 10981 /* 10982 * Sanity checks - minimum size, size a multiple of 10983 * eight bytes, and matching size passed in. 10984 */ 10985 if (inlen != 0 && 10986 inlen != (8 * (dopts->ip6d_len + 1))) 10987 return (EINVAL); 10988 10989 if (checkonly) 10990 break; 10991 10992 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10993 (uchar_t **)&ipp->ipp_dstopts, 10994 &ipp->ipp_dstoptslen, 0); 10995 if (reterr != 0) 10996 return (reterr); 10997 if (ipp->ipp_dstoptslen == 0) 10998 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10999 else 11000 ipp->ipp_fields |= IPPF_DSTOPTS; 11001 reterr = tcp_build_hdrs(q, tcp); 11002 if (reterr != 0) 11003 return (reterr); 11004 break; 11005 } 11006 case IPV6_RTHDR: { 11007 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 11008 11009 /* 11010 * Sanity checks - minimum size, size a multiple of 11011 * eight bytes, and matching size passed in. 11012 */ 11013 if (inlen != 0 && 11014 inlen != (8 * (rt->ip6r_len + 1))) 11015 return (EINVAL); 11016 11017 if (checkonly) 11018 break; 11019 11020 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11021 (uchar_t **)&ipp->ipp_rthdr, 11022 &ipp->ipp_rthdrlen, 0); 11023 if (reterr != 0) 11024 return (reterr); 11025 if (ipp->ipp_rthdrlen == 0) 11026 ipp->ipp_fields &= ~IPPF_RTHDR; 11027 else 11028 ipp->ipp_fields |= IPPF_RTHDR; 11029 reterr = tcp_build_hdrs(q, tcp); 11030 if (reterr != 0) 11031 return (reterr); 11032 break; 11033 } 11034 case IPV6_V6ONLY: 11035 if (!checkonly) 11036 tcp->tcp_connp->conn_ipv6_v6only = onoff; 11037 break; 11038 case IPV6_USE_MIN_MTU: 11039 if (inlen != sizeof (int)) 11040 return (EINVAL); 11041 11042 if (*i1 < -1 || *i1 > 1) 11043 return (EINVAL); 11044 11045 if (checkonly) 11046 break; 11047 11048 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11049 ipp->ipp_use_min_mtu = *i1; 11050 break; 11051 case IPV6_BOUND_PIF: 11052 /* Handled at the IP level */ 11053 return (-EINVAL); 11054 case IPV6_SEC_OPT: 11055 /* 11056 * We should not allow policy setting after 11057 * we start listening for connections. 11058 */ 11059 if (tcp->tcp_state == TCPS_LISTEN) { 11060 return (EINVAL); 11061 } else { 11062 /* Handled at the IP level */ 11063 return (-EINVAL); 11064 } 11065 case IPV6_SRC_PREFERENCES: 11066 if (inlen != sizeof (uint32_t)) 11067 return (EINVAL); 11068 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11069 *(uint32_t *)invalp); 11070 if (reterr != 0) { 11071 *outlenp = 0; 11072 return (reterr); 11073 } 11074 break; 11075 default: 11076 *outlenp = 0; 11077 return (EINVAL); 11078 } 11079 break; 11080 } /* end IPPROTO_IPV6 */ 11081 default: 11082 *outlenp = 0; 11083 return (EINVAL); 11084 } 11085 /* 11086 * Common case of OK return with outval same as inval 11087 */ 11088 if (invalp != outvalp) { 11089 /* don't trust bcopy for identical src/dst */ 11090 (void) bcopy(invalp, outvalp, inlen); 11091 } 11092 *outlenp = inlen; 11093 return (0); 11094 } 11095 11096 /* 11097 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11098 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11099 * headers, and the maximum size tcp header (to avoid reallocation 11100 * on the fly for additional tcp options). 11101 * Returns failure if can't allocate memory. 11102 */ 11103 static int 11104 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11105 { 11106 char *hdrs; 11107 uint_t hdrs_len; 11108 ip6i_t *ip6i; 11109 char buf[TCP_MAX_HDR_LENGTH]; 11110 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11111 in6_addr_t src, dst; 11112 tcp_stack_t *tcps = tcp->tcp_tcps; 11113 11114 /* 11115 * save the existing tcp header and source/dest IP addresses 11116 */ 11117 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11118 src = tcp->tcp_ip6h->ip6_src; 11119 dst = tcp->tcp_ip6h->ip6_dst; 11120 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11121 ASSERT(hdrs_len != 0); 11122 if (hdrs_len > tcp->tcp_iphc_len) { 11123 /* Need to reallocate */ 11124 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11125 if (hdrs == NULL) 11126 return (ENOMEM); 11127 if (tcp->tcp_iphc != NULL) { 11128 if (tcp->tcp_hdr_grown) { 11129 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11130 } else { 11131 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11132 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11133 } 11134 tcp->tcp_iphc_len = 0; 11135 } 11136 ASSERT(tcp->tcp_iphc_len == 0); 11137 tcp->tcp_iphc = hdrs; 11138 tcp->tcp_iphc_len = hdrs_len; 11139 tcp->tcp_hdr_grown = B_TRUE; 11140 } 11141 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11142 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11143 11144 /* Set header fields not in ipp */ 11145 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11146 ip6i = (ip6i_t *)tcp->tcp_iphc; 11147 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11148 } else { 11149 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11150 } 11151 /* 11152 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11153 * 11154 * tcp->tcp_tcp_hdr_len doesn't change here. 11155 */ 11156 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11157 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11158 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11159 11160 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11161 11162 tcp->tcp_ip6h->ip6_src = src; 11163 tcp->tcp_ip6h->ip6_dst = dst; 11164 11165 /* 11166 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11167 * the default value for TCP. 11168 */ 11169 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11170 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11171 11172 /* 11173 * If we're setting extension headers after a connection 11174 * has been established, and if we have a routing header 11175 * among the extension headers, call ip_massage_options_v6 to 11176 * manipulate the routing header/ip6_dst set the checksum 11177 * difference in the tcp header template. 11178 * (This happens in tcp_connect_ipv6 if the routing header 11179 * is set prior to the connect.) 11180 * Set the tcp_sum to zero first in case we've cleared a 11181 * routing header or don't have one at all. 11182 */ 11183 tcp->tcp_sum = 0; 11184 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11185 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11186 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11187 (uint8_t *)tcp->tcp_tcph); 11188 if (rth != NULL) { 11189 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11190 rth, tcps->tcps_netstack); 11191 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11192 (tcp->tcp_sum >> 16)); 11193 } 11194 } 11195 11196 /* Try to get everything in a single mblk */ 11197 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11198 return (0); 11199 } 11200 11201 /* 11202 * Transfer any source route option from ipha to buf/dst in reversed form. 11203 */ 11204 static int 11205 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11206 { 11207 ipoptp_t opts; 11208 uchar_t *opt; 11209 uint8_t optval; 11210 uint8_t optlen; 11211 uint32_t len = 0; 11212 11213 for (optval = ipoptp_first(&opts, ipha); 11214 optval != IPOPT_EOL; 11215 optval = ipoptp_next(&opts)) { 11216 opt = opts.ipoptp_cur; 11217 optlen = opts.ipoptp_len; 11218 switch (optval) { 11219 int off1, off2; 11220 case IPOPT_SSRR: 11221 case IPOPT_LSRR: 11222 11223 /* Reverse source route */ 11224 /* 11225 * First entry should be the next to last one in the 11226 * current source route (the last entry is our 11227 * address.) 11228 * The last entry should be the final destination. 11229 */ 11230 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11231 buf[IPOPT_OLEN] = (uint8_t)optlen; 11232 off1 = IPOPT_MINOFF_SR - 1; 11233 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11234 if (off2 < 0) { 11235 /* No entries in source route */ 11236 break; 11237 } 11238 bcopy(opt + off2, dst, IP_ADDR_LEN); 11239 /* 11240 * Note: use src since ipha has not had its src 11241 * and dst reversed (it is in the state it was 11242 * received. 11243 */ 11244 bcopy(&ipha->ipha_src, buf + off2, 11245 IP_ADDR_LEN); 11246 off2 -= IP_ADDR_LEN; 11247 11248 while (off2 > 0) { 11249 bcopy(opt + off2, buf + off1, 11250 IP_ADDR_LEN); 11251 off1 += IP_ADDR_LEN; 11252 off2 -= IP_ADDR_LEN; 11253 } 11254 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11255 buf += optlen; 11256 len += optlen; 11257 break; 11258 } 11259 } 11260 done: 11261 /* Pad the resulting options */ 11262 while (len & 0x3) { 11263 *buf++ = IPOPT_EOL; 11264 len++; 11265 } 11266 return (len); 11267 } 11268 11269 11270 /* 11271 * Extract and revert a source route from ipha (if any) 11272 * and then update the relevant fields in both tcp_t and the standard header. 11273 */ 11274 static void 11275 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11276 { 11277 char buf[TCP_MAX_HDR_LENGTH]; 11278 uint_t tcph_len; 11279 int len; 11280 11281 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11282 len = IPH_HDR_LENGTH(ipha); 11283 if (len == IP_SIMPLE_HDR_LENGTH) 11284 /* Nothing to do */ 11285 return; 11286 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11287 (len & 0x3)) 11288 return; 11289 11290 tcph_len = tcp->tcp_tcp_hdr_len; 11291 bcopy(tcp->tcp_tcph, buf, tcph_len); 11292 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11293 (tcp->tcp_ipha->ipha_dst & 0xffff); 11294 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11295 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11296 len += IP_SIMPLE_HDR_LENGTH; 11297 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11298 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11299 if ((int)tcp->tcp_sum < 0) 11300 tcp->tcp_sum--; 11301 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11302 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11303 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11304 bcopy(buf, tcp->tcp_tcph, tcph_len); 11305 tcp->tcp_ip_hdr_len = len; 11306 tcp->tcp_ipha->ipha_version_and_hdr_length = 11307 (IP_VERSION << 4) | (len >> 2); 11308 len += tcph_len; 11309 tcp->tcp_hdr_len = len; 11310 } 11311 11312 /* 11313 * Copy the standard header into its new location, 11314 * lay in the new options and then update the relevant 11315 * fields in both tcp_t and the standard header. 11316 */ 11317 static int 11318 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11319 { 11320 uint_t tcph_len; 11321 uint8_t *ip_optp; 11322 tcph_t *new_tcph; 11323 tcp_stack_t *tcps = tcp->tcp_tcps; 11324 11325 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11326 return (EINVAL); 11327 11328 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11329 return (EINVAL); 11330 11331 if (checkonly) { 11332 /* 11333 * do not really set, just pretend to - T_CHECK 11334 */ 11335 return (0); 11336 } 11337 11338 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11339 if (tcp->tcp_label_len > 0) { 11340 int padlen; 11341 uint8_t opt; 11342 11343 /* convert list termination to no-ops */ 11344 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11345 ip_optp += ip_optp[IPOPT_OLEN]; 11346 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11347 while (--padlen >= 0) 11348 *ip_optp++ = opt; 11349 } 11350 tcph_len = tcp->tcp_tcp_hdr_len; 11351 new_tcph = (tcph_t *)(ip_optp + len); 11352 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11353 tcp->tcp_tcph = new_tcph; 11354 bcopy(ptr, ip_optp, len); 11355 11356 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11357 11358 tcp->tcp_ip_hdr_len = len; 11359 tcp->tcp_ipha->ipha_version_and_hdr_length = 11360 (IP_VERSION << 4) | (len >> 2); 11361 tcp->tcp_hdr_len = len + tcph_len; 11362 if (!TCP_IS_DETACHED(tcp)) { 11363 /* Always allocate room for all options. */ 11364 (void) mi_set_sth_wroff(tcp->tcp_rq, 11365 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11366 } 11367 return (0); 11368 } 11369 11370 /* Get callback routine passed to nd_load by tcp_param_register */ 11371 /* ARGSUSED */ 11372 static int 11373 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11374 { 11375 tcpparam_t *tcppa = (tcpparam_t *)cp; 11376 11377 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11378 return (0); 11379 } 11380 11381 /* 11382 * Walk through the param array specified registering each element with the 11383 * named dispatch handler. 11384 */ 11385 static boolean_t 11386 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11387 { 11388 for (; cnt-- > 0; tcppa++) { 11389 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11390 if (!nd_load(ndp, tcppa->tcp_param_name, 11391 tcp_param_get, tcp_param_set, 11392 (caddr_t)tcppa)) { 11393 nd_free(ndp); 11394 return (B_FALSE); 11395 } 11396 } 11397 } 11398 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11399 KM_SLEEP); 11400 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11401 sizeof (tcpparam_t)); 11402 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11403 tcp_param_get, tcp_param_set_aligned, 11404 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11405 nd_free(ndp); 11406 return (B_FALSE); 11407 } 11408 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11409 KM_SLEEP); 11410 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11411 sizeof (tcpparam_t)); 11412 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11413 tcp_param_get, tcp_param_set_aligned, 11414 (caddr_t)tcps->tcps_mdt_head_param)) { 11415 nd_free(ndp); 11416 return (B_FALSE); 11417 } 11418 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11419 KM_SLEEP); 11420 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11421 sizeof (tcpparam_t)); 11422 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11423 tcp_param_get, tcp_param_set_aligned, 11424 (caddr_t)tcps->tcps_mdt_tail_param)) { 11425 nd_free(ndp); 11426 return (B_FALSE); 11427 } 11428 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11429 KM_SLEEP); 11430 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11431 sizeof (tcpparam_t)); 11432 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11433 tcp_param_get, tcp_param_set_aligned, 11434 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11435 nd_free(ndp); 11436 return (B_FALSE); 11437 } 11438 if (!nd_load(ndp, "tcp_extra_priv_ports", 11439 tcp_extra_priv_ports_get, NULL, NULL)) { 11440 nd_free(ndp); 11441 return (B_FALSE); 11442 } 11443 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11444 NULL, tcp_extra_priv_ports_add, NULL)) { 11445 nd_free(ndp); 11446 return (B_FALSE); 11447 } 11448 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11449 NULL, tcp_extra_priv_ports_del, NULL)) { 11450 nd_free(ndp); 11451 return (B_FALSE); 11452 } 11453 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11454 NULL)) { 11455 nd_free(ndp); 11456 return (B_FALSE); 11457 } 11458 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11459 NULL, NULL)) { 11460 nd_free(ndp); 11461 return (B_FALSE); 11462 } 11463 if (!nd_load(ndp, "tcp_listen_hash", 11464 tcp_listen_hash_report, NULL, NULL)) { 11465 nd_free(ndp); 11466 return (B_FALSE); 11467 } 11468 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11469 NULL, NULL)) { 11470 nd_free(ndp); 11471 return (B_FALSE); 11472 } 11473 if (!nd_load(ndp, "tcp_acceptor_hash", 11474 tcp_acceptor_hash_report, NULL, NULL)) { 11475 nd_free(ndp); 11476 return (B_FALSE); 11477 } 11478 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11479 tcp_1948_phrase_set, NULL)) { 11480 nd_free(ndp); 11481 return (B_FALSE); 11482 } 11483 /* 11484 * Dummy ndd variables - only to convey obsolescence information 11485 * through printing of their name (no get or set routines) 11486 * XXX Remove in future releases ? 11487 */ 11488 if (!nd_load(ndp, 11489 "tcp_close_wait_interval(obsoleted - " 11490 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11491 nd_free(ndp); 11492 return (B_FALSE); 11493 } 11494 return (B_TRUE); 11495 } 11496 11497 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11498 /* ARGSUSED */ 11499 static int 11500 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11501 cred_t *cr) 11502 { 11503 long new_value; 11504 tcpparam_t *tcppa = (tcpparam_t *)cp; 11505 11506 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11507 new_value < tcppa->tcp_param_min || 11508 new_value > tcppa->tcp_param_max) { 11509 return (EINVAL); 11510 } 11511 /* 11512 * Need to make sure new_value is a multiple of 4. If it is not, 11513 * round it up. For future 64 bit requirement, we actually make it 11514 * a multiple of 8. 11515 */ 11516 if (new_value & 0x7) { 11517 new_value = (new_value & ~0x7) + 0x8; 11518 } 11519 tcppa->tcp_param_val = new_value; 11520 return (0); 11521 } 11522 11523 /* Set callback routine passed to nd_load by tcp_param_register */ 11524 /* ARGSUSED */ 11525 static int 11526 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11527 { 11528 long new_value; 11529 tcpparam_t *tcppa = (tcpparam_t *)cp; 11530 11531 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11532 new_value < tcppa->tcp_param_min || 11533 new_value > tcppa->tcp_param_max) { 11534 return (EINVAL); 11535 } 11536 tcppa->tcp_param_val = new_value; 11537 return (0); 11538 } 11539 11540 /* 11541 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11542 * is filled, return as much as we can. The message passed in may be 11543 * multi-part, chained using b_cont. "start" is the starting sequence 11544 * number for this piece. 11545 */ 11546 static mblk_t * 11547 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11548 { 11549 uint32_t end; 11550 mblk_t *mp1; 11551 mblk_t *mp2; 11552 mblk_t *next_mp; 11553 uint32_t u1; 11554 tcp_stack_t *tcps = tcp->tcp_tcps; 11555 11556 /* Walk through all the new pieces. */ 11557 do { 11558 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11559 (uintptr_t)INT_MAX); 11560 end = start + (int)(mp->b_wptr - mp->b_rptr); 11561 next_mp = mp->b_cont; 11562 if (start == end) { 11563 /* Empty. Blast it. */ 11564 freeb(mp); 11565 continue; 11566 } 11567 mp->b_cont = NULL; 11568 TCP_REASS_SET_SEQ(mp, start); 11569 TCP_REASS_SET_END(mp, end); 11570 mp1 = tcp->tcp_reass_tail; 11571 if (!mp1) { 11572 tcp->tcp_reass_tail = mp; 11573 tcp->tcp_reass_head = mp; 11574 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11575 UPDATE_MIB(&tcps->tcps_mib, 11576 tcpInDataUnorderBytes, end - start); 11577 continue; 11578 } 11579 /* New stuff completely beyond tail? */ 11580 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11581 /* Link it on end. */ 11582 mp1->b_cont = mp; 11583 tcp->tcp_reass_tail = mp; 11584 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11585 UPDATE_MIB(&tcps->tcps_mib, 11586 tcpInDataUnorderBytes, end - start); 11587 continue; 11588 } 11589 mp1 = tcp->tcp_reass_head; 11590 u1 = TCP_REASS_SEQ(mp1); 11591 /* New stuff at the front? */ 11592 if (SEQ_LT(start, u1)) { 11593 /* Yes... Check for overlap. */ 11594 mp->b_cont = mp1; 11595 tcp->tcp_reass_head = mp; 11596 tcp_reass_elim_overlap(tcp, mp); 11597 continue; 11598 } 11599 /* 11600 * The new piece fits somewhere between the head and tail. 11601 * We find our slot, where mp1 precedes us and mp2 trails. 11602 */ 11603 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11604 u1 = TCP_REASS_SEQ(mp2); 11605 if (SEQ_LEQ(start, u1)) 11606 break; 11607 } 11608 /* Link ourselves in */ 11609 mp->b_cont = mp2; 11610 mp1->b_cont = mp; 11611 11612 /* Trim overlap with following mblk(s) first */ 11613 tcp_reass_elim_overlap(tcp, mp); 11614 11615 /* Trim overlap with preceding mblk */ 11616 tcp_reass_elim_overlap(tcp, mp1); 11617 11618 } while (start = end, mp = next_mp); 11619 mp1 = tcp->tcp_reass_head; 11620 /* Anything ready to go? */ 11621 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11622 return (NULL); 11623 /* Eat what we can off the queue */ 11624 for (;;) { 11625 mp = mp1->b_cont; 11626 end = TCP_REASS_END(mp1); 11627 TCP_REASS_SET_SEQ(mp1, 0); 11628 TCP_REASS_SET_END(mp1, 0); 11629 if (!mp) { 11630 tcp->tcp_reass_tail = NULL; 11631 break; 11632 } 11633 if (end != TCP_REASS_SEQ(mp)) { 11634 mp1->b_cont = NULL; 11635 break; 11636 } 11637 mp1 = mp; 11638 } 11639 mp1 = tcp->tcp_reass_head; 11640 tcp->tcp_reass_head = mp; 11641 return (mp1); 11642 } 11643 11644 /* Eliminate any overlap that mp may have over later mblks */ 11645 static void 11646 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11647 { 11648 uint32_t end; 11649 mblk_t *mp1; 11650 uint32_t u1; 11651 tcp_stack_t *tcps = tcp->tcp_tcps; 11652 11653 end = TCP_REASS_END(mp); 11654 while ((mp1 = mp->b_cont) != NULL) { 11655 u1 = TCP_REASS_SEQ(mp1); 11656 if (!SEQ_GT(end, u1)) 11657 break; 11658 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11659 mp->b_wptr -= end - u1; 11660 TCP_REASS_SET_END(mp, u1); 11661 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11662 UPDATE_MIB(&tcps->tcps_mib, 11663 tcpInDataPartDupBytes, end - u1); 11664 break; 11665 } 11666 mp->b_cont = mp1->b_cont; 11667 TCP_REASS_SET_SEQ(mp1, 0); 11668 TCP_REASS_SET_END(mp1, 0); 11669 freeb(mp1); 11670 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11671 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11672 } 11673 if (!mp1) 11674 tcp->tcp_reass_tail = mp; 11675 } 11676 11677 /* 11678 * Send up all messages queued on tcp_rcv_list. 11679 */ 11680 static uint_t 11681 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11682 { 11683 mblk_t *mp; 11684 uint_t ret = 0; 11685 uint_t thwin; 11686 #ifdef DEBUG 11687 uint_t cnt = 0; 11688 #endif 11689 tcp_stack_t *tcps = tcp->tcp_tcps; 11690 11691 /* Can't drain on an eager connection */ 11692 if (tcp->tcp_listener != NULL) 11693 return (ret); 11694 11695 /* Can't be sodirect enabled */ 11696 ASSERT(SOD_NOT_ENABLED(tcp)); 11697 11698 /* No need for the push timer now. */ 11699 if (tcp->tcp_push_tid != 0) { 11700 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11701 tcp->tcp_push_tid = 0; 11702 } 11703 11704 /* 11705 * Handle two cases here: we are currently fused or we were 11706 * previously fused and have some urgent data to be delivered 11707 * upstream. The latter happens because we either ran out of 11708 * memory or were detached and therefore sending the SIGURG was 11709 * deferred until this point. In either case we pass control 11710 * over to tcp_fuse_rcv_drain() since it may need to complete 11711 * some work. 11712 */ 11713 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11714 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11715 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11716 &tcp->tcp_fused_sigurg_mp)) 11717 return (ret); 11718 } 11719 11720 while ((mp = tcp->tcp_rcv_list) != NULL) { 11721 tcp->tcp_rcv_list = mp->b_next; 11722 mp->b_next = NULL; 11723 #ifdef DEBUG 11724 cnt += msgdsize(mp); 11725 #endif 11726 /* Does this need SSL processing first? */ 11727 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11728 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11729 mblk_t *, mp); 11730 tcp_kssl_input(tcp, mp); 11731 continue; 11732 } 11733 putnext(q, mp); 11734 } 11735 ASSERT(cnt == tcp->tcp_rcv_cnt); 11736 tcp->tcp_rcv_last_head = NULL; 11737 tcp->tcp_rcv_last_tail = NULL; 11738 tcp->tcp_rcv_cnt = 0; 11739 11740 /* Learn the latest rwnd information that we sent to the other side. */ 11741 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11742 << tcp->tcp_rcv_ws; 11743 /* This is peer's calculated send window (our receive window). */ 11744 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11745 /* 11746 * Increase the receive window to max. But we need to do receiver 11747 * SWS avoidance. This means that we need to check the increase of 11748 * of receive window is at least 1 MSS. 11749 */ 11750 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11751 /* 11752 * If the window that the other side knows is less than max 11753 * deferred acks segments, send an update immediately. 11754 */ 11755 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11756 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11757 ret = TH_ACK_NEEDED; 11758 } 11759 tcp->tcp_rwnd = q->q_hiwat; 11760 } 11761 return (ret); 11762 } 11763 11764 /* 11765 * Queue data on tcp_rcv_list which is a b_next chain. 11766 * tcp_rcv_last_head/tail is the last element of this chain. 11767 * Each element of the chain is a b_cont chain. 11768 * 11769 * M_DATA messages are added to the current element. 11770 * Other messages are added as new (b_next) elements. 11771 */ 11772 void 11773 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11774 { 11775 ASSERT(seg_len == msgdsize(mp)); 11776 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11777 11778 if (tcp->tcp_rcv_list == NULL) { 11779 ASSERT(tcp->tcp_rcv_last_head == NULL); 11780 tcp->tcp_rcv_list = mp; 11781 tcp->tcp_rcv_last_head = mp; 11782 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11783 tcp->tcp_rcv_last_tail->b_cont = mp; 11784 } else { 11785 tcp->tcp_rcv_last_head->b_next = mp; 11786 tcp->tcp_rcv_last_head = mp; 11787 } 11788 11789 while (mp->b_cont) 11790 mp = mp->b_cont; 11791 11792 tcp->tcp_rcv_last_tail = mp; 11793 tcp->tcp_rcv_cnt += seg_len; 11794 tcp->tcp_rwnd -= seg_len; 11795 } 11796 11797 /* 11798 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11799 * above, in addition when uioa is enabled schedule an asynchronous uio 11800 * prior to enqueuing. They implement the combinhed semantics of the 11801 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11802 * canputnext(), i.e. flow-control with backenable. 11803 * 11804 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11805 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11806 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11807 * 11808 * Must be called with sodp->sod_lockp held and will return with the lock 11809 * released. 11810 */ 11811 static uint_t 11812 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11813 { 11814 queue_t *q = tcp->tcp_rq; 11815 uint_t thwin; 11816 tcp_stack_t *tcps = tcp->tcp_tcps; 11817 uint_t ret = 0; 11818 11819 /* Can't be an eager connection */ 11820 ASSERT(tcp->tcp_listener == NULL); 11821 11822 /* Caller must have lock held */ 11823 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11824 11825 /* Sodirect mode so must not be a tcp_rcv_list */ 11826 ASSERT(tcp->tcp_rcv_list == NULL); 11827 11828 if (SOD_QFULL(sodp)) { 11829 /* Q is full, mark Q for need backenable */ 11830 SOD_QSETBE(sodp); 11831 } 11832 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11833 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11834 << tcp->tcp_rcv_ws; 11835 /* This is peer's calculated send window (our available rwnd). */ 11836 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11837 /* 11838 * Increase the receive window to max. But we need to do receiver 11839 * SWS avoidance. This means that we need to check the increase of 11840 * of receive window is at least 1 MSS. 11841 */ 11842 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11843 /* 11844 * If the window that the other side knows is less than max 11845 * deferred acks segments, send an update immediately. 11846 */ 11847 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11848 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11849 ret = TH_ACK_NEEDED; 11850 } 11851 tcp->tcp_rwnd = q->q_hiwat; 11852 } 11853 11854 if (!SOD_QEMPTY(sodp)) { 11855 /* Wakeup to socket */ 11856 sodp->sod_state &= SOD_WAKE_CLR; 11857 sodp->sod_state |= SOD_WAKE_DONE; 11858 (sodp->sod_wakeup)(sodp); 11859 /* wakeup() does the mutex_ext() */ 11860 } else { 11861 /* Q is empty, no need to wake */ 11862 sodp->sod_state &= SOD_WAKE_CLR; 11863 sodp->sod_state |= SOD_WAKE_NOT; 11864 mutex_exit(sodp->sod_lockp); 11865 } 11866 11867 /* No need for the push timer now. */ 11868 if (tcp->tcp_push_tid != 0) { 11869 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11870 tcp->tcp_push_tid = 0; 11871 } 11872 11873 return (ret); 11874 } 11875 11876 /* 11877 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11878 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11879 * to the user-land buffer and flag the mblk_t as such. 11880 * 11881 * Also, handle tcp_rwnd. 11882 */ 11883 uint_t 11884 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11885 { 11886 uioa_t *uioap = &sodp->sod_uioa; 11887 boolean_t qfull; 11888 uint_t thwin; 11889 11890 /* Can't be an eager connection */ 11891 ASSERT(tcp->tcp_listener == NULL); 11892 11893 /* Caller must have lock held */ 11894 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11895 11896 /* Sodirect mode so must not be a tcp_rcv_list */ 11897 ASSERT(tcp->tcp_rcv_list == NULL); 11898 11899 /* Passed in segment length must be equal to mblk_t chain data size */ 11900 ASSERT(seg_len == msgdsize(mp)); 11901 11902 if (DB_TYPE(mp) != M_DATA) { 11903 /* Only process M_DATA mblk_t's */ 11904 goto enq; 11905 } 11906 if (uioap->uioa_state & UIOA_ENABLED) { 11907 /* Uioa is enabled */ 11908 mblk_t *mp1 = mp; 11909 mblk_t *lmp = NULL; 11910 11911 if (seg_len > uioap->uio_resid) { 11912 /* 11913 * There isn't enough uio space for the mblk_t chain 11914 * so disable uioa such that this and any additional 11915 * mblk_t data is handled by the socket and schedule 11916 * the socket for wakeup to finish this uioa. 11917 */ 11918 uioap->uioa_state &= UIOA_CLR; 11919 uioap->uioa_state |= UIOA_FINI; 11920 if (sodp->sod_state & SOD_WAKE_NOT) { 11921 sodp->sod_state &= SOD_WAKE_CLR; 11922 sodp->sod_state |= SOD_WAKE_NEED; 11923 } 11924 goto enq; 11925 } 11926 do { 11927 uint32_t len = MBLKL(mp1); 11928 11929 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11930 /* Scheduled, mark dblk_t as such */ 11931 DB_FLAGS(mp1) |= DBLK_UIOA; 11932 } else { 11933 /* Error, turn off async processing */ 11934 uioap->uioa_state &= UIOA_CLR; 11935 uioap->uioa_state |= UIOA_FINI; 11936 break; 11937 } 11938 lmp = mp1; 11939 } while ((mp1 = mp1->b_cont) != NULL); 11940 11941 if (mp1 != NULL || uioap->uio_resid == 0) { 11942 /* 11943 * Not all mblk_t(s) uioamoved (error) or all uio 11944 * space has been consumed so schedule the socket 11945 * for wakeup to finish this uio. 11946 */ 11947 sodp->sod_state &= SOD_WAKE_CLR; 11948 sodp->sod_state |= SOD_WAKE_NEED; 11949 11950 /* Break the mblk chain if neccessary. */ 11951 if (mp1 != NULL && lmp != NULL) { 11952 mp->b_next = mp1; 11953 lmp->b_cont = NULL; 11954 } 11955 } 11956 } else if (uioap->uioa_state & UIOA_FINI) { 11957 /* 11958 * Post UIO_ENABLED waiting for socket to finish processing 11959 * so just enqueue and update tcp_rwnd. 11960 */ 11961 if (SOD_QFULL(sodp)) 11962 tcp->tcp_rwnd -= seg_len; 11963 } else if (sodp->sod_want > 0) { 11964 /* 11965 * Uioa isn't enabled but sodirect has a pending read(). 11966 */ 11967 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11968 if (sodp->sod_state & SOD_WAKE_NOT) { 11969 /* Schedule socket for wakeup */ 11970 sodp->sod_state &= SOD_WAKE_CLR; 11971 sodp->sod_state |= SOD_WAKE_NEED; 11972 } 11973 tcp->tcp_rwnd -= seg_len; 11974 } 11975 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11976 /* 11977 * No pending sodirect read() so used the default 11978 * TCP push logic to guess that a push is needed. 11979 */ 11980 if (sodp->sod_state & SOD_WAKE_NOT) { 11981 /* Schedule socket for wakeup */ 11982 sodp->sod_state &= SOD_WAKE_CLR; 11983 sodp->sod_state |= SOD_WAKE_NEED; 11984 } 11985 tcp->tcp_rwnd -= seg_len; 11986 } else { 11987 /* Just update tcp_rwnd */ 11988 tcp->tcp_rwnd -= seg_len; 11989 } 11990 enq: 11991 qfull = SOD_QFULL(sodp); 11992 11993 (sodp->sod_enqueue)(sodp, mp); 11994 11995 if (! qfull && SOD_QFULL(sodp)) { 11996 /* Wasn't QFULL, now QFULL, need back-enable */ 11997 SOD_QSETBE(sodp); 11998 } 11999 12000 /* 12001 * Check to see if remote avail swnd < mss due to delayed ACK, 12002 * first get advertised rwnd. 12003 */ 12004 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 12005 /* Minus delayed ACK count */ 12006 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 12007 if (thwin < tcp->tcp_mss) { 12008 /* Remote avail swnd < mss, need ACK now */ 12009 return (TH_ACK_NEEDED); 12010 } 12011 12012 return (0); 12013 } 12014 12015 /* 12016 * DEFAULT TCP ENTRY POINT via squeue on READ side. 12017 * 12018 * This is the default entry function into TCP on the read side. TCP is 12019 * always entered via squeue i.e. using squeue's for mutual exclusion. 12020 * When classifier does a lookup to find the tcp, it also puts a reference 12021 * on the conn structure associated so the tcp is guaranteed to exist 12022 * when we come here. We still need to check the state because it might 12023 * as well has been closed. The squeue processing function i.e. squeue_enter, 12024 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 12025 * CONN_DEC_REF. 12026 * 12027 * Apart from the default entry point, IP also sends packets directly to 12028 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 12029 * connections. 12030 */ 12031 void 12032 tcp_input(void *arg, mblk_t *mp, void *arg2) 12033 { 12034 conn_t *connp = (conn_t *)arg; 12035 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 12036 12037 /* arg2 is the sqp */ 12038 ASSERT(arg2 != NULL); 12039 ASSERT(mp != NULL); 12040 12041 /* 12042 * Don't accept any input on a closed tcp as this TCP logically does 12043 * not exist on the system. Don't proceed further with this TCP. 12044 * For eg. this packet could trigger another close of this tcp 12045 * which would be disastrous for tcp_refcnt. tcp_close_detached / 12046 * tcp_clean_death / tcp_closei_local must be called at most once 12047 * on a TCP. In this case we need to refeed the packet into the 12048 * classifier and figure out where the packet should go. Need to 12049 * preserve the recv_ill somehow. Until we figure that out, for 12050 * now just drop the packet if we can't classify the packet. 12051 */ 12052 if (tcp->tcp_state == TCPS_CLOSED || 12053 tcp->tcp_state == TCPS_BOUND) { 12054 conn_t *new_connp; 12055 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 12056 12057 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 12058 if (new_connp != NULL) { 12059 tcp_reinput(new_connp, mp, arg2); 12060 return; 12061 } 12062 /* We failed to classify. For now just drop the packet */ 12063 freemsg(mp); 12064 return; 12065 } 12066 12067 if (DB_TYPE(mp) == M_DATA) 12068 tcp_rput_data(connp, mp, arg2); 12069 else 12070 tcp_rput_common(tcp, mp); 12071 } 12072 12073 /* 12074 * The read side put procedure. 12075 * The packets passed up by ip are assume to be aligned according to 12076 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 12077 */ 12078 static void 12079 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 12080 { 12081 /* 12082 * tcp_rput_data() does not expect M_CTL except for the case 12083 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 12084 * type. Need to make sure that any other M_CTLs don't make 12085 * it to tcp_rput_data since it is not expecting any and doesn't 12086 * check for it. 12087 */ 12088 if (DB_TYPE(mp) == M_CTL) { 12089 switch (*(uint32_t *)(mp->b_rptr)) { 12090 case TCP_IOC_ABORT_CONN: 12091 /* 12092 * Handle connection abort request. 12093 */ 12094 tcp_ioctl_abort_handler(tcp, mp); 12095 return; 12096 case IPSEC_IN: 12097 /* 12098 * Only secure icmp arrive in TCP and they 12099 * don't go through data path. 12100 */ 12101 tcp_icmp_error(tcp, mp); 12102 return; 12103 case IN_PKTINFO: 12104 /* 12105 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 12106 * sockets that are receiving IPv4 traffic. tcp 12107 */ 12108 ASSERT(tcp->tcp_family == AF_INET6); 12109 ASSERT(tcp->tcp_ipv6_recvancillary & 12110 TCP_IPV6_RECVPKTINFO); 12111 tcp_rput_data(tcp->tcp_connp, mp, 12112 tcp->tcp_connp->conn_sqp); 12113 return; 12114 case MDT_IOC_INFO_UPDATE: 12115 /* 12116 * Handle Multidata information update; the 12117 * following routine will free the message. 12118 */ 12119 if (tcp->tcp_connp->conn_mdt_ok) { 12120 tcp_mdt_update(tcp, 12121 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 12122 B_FALSE); 12123 } 12124 freemsg(mp); 12125 return; 12126 case LSO_IOC_INFO_UPDATE: 12127 /* 12128 * Handle LSO information update; the following 12129 * routine will free the message. 12130 */ 12131 if (tcp->tcp_connp->conn_lso_ok) { 12132 tcp_lso_update(tcp, 12133 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 12134 } 12135 freemsg(mp); 12136 return; 12137 default: 12138 /* 12139 * tcp_icmp_err() will process the M_CTL packets. 12140 * Non-ICMP packets, if any, will be discarded in 12141 * tcp_icmp_err(). We will process the ICMP packet 12142 * even if we are TCP_IS_DETACHED_NONEAGER as the 12143 * incoming ICMP packet may result in changing 12144 * the tcp_mss, which we would need if we have 12145 * packets to retransmit. 12146 */ 12147 tcp_icmp_error(tcp, mp); 12148 return; 12149 } 12150 } 12151 12152 /* No point processing the message if tcp is already closed */ 12153 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 12154 freemsg(mp); 12155 return; 12156 } 12157 12158 tcp_rput_other(tcp, mp); 12159 } 12160 12161 12162 /* The minimum of smoothed mean deviation in RTO calculation. */ 12163 #define TCP_SD_MIN 400 12164 12165 /* 12166 * Set RTO for this connection. The formula is from Jacobson and Karels' 12167 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 12168 * are the same as those in Appendix A.2 of that paper. 12169 * 12170 * m = new measurement 12171 * sa = smoothed RTT average (8 * average estimates). 12172 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 12173 */ 12174 static void 12175 tcp_set_rto(tcp_t *tcp, clock_t rtt) 12176 { 12177 long m = TICK_TO_MSEC(rtt); 12178 clock_t sa = tcp->tcp_rtt_sa; 12179 clock_t sv = tcp->tcp_rtt_sd; 12180 clock_t rto; 12181 tcp_stack_t *tcps = tcp->tcp_tcps; 12182 12183 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 12184 tcp->tcp_rtt_update++; 12185 12186 /* tcp_rtt_sa is not 0 means this is a new sample. */ 12187 if (sa != 0) { 12188 /* 12189 * Update average estimator: 12190 * new rtt = 7/8 old rtt + 1/8 Error 12191 */ 12192 12193 /* m is now Error in estimate. */ 12194 m -= sa >> 3; 12195 if ((sa += m) <= 0) { 12196 /* 12197 * Don't allow the smoothed average to be negative. 12198 * We use 0 to denote reinitialization of the 12199 * variables. 12200 */ 12201 sa = 1; 12202 } 12203 12204 /* 12205 * Update deviation estimator: 12206 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12207 */ 12208 if (m < 0) 12209 m = -m; 12210 m -= sv >> 2; 12211 sv += m; 12212 } else { 12213 /* 12214 * This follows BSD's implementation. So the reinitialized 12215 * RTO is 3 * m. We cannot go less than 2 because if the 12216 * link is bandwidth dominated, doubling the window size 12217 * during slow start means doubling the RTT. We want to be 12218 * more conservative when we reinitialize our estimates. 3 12219 * is just a convenient number. 12220 */ 12221 sa = m << 3; 12222 sv = m << 1; 12223 } 12224 if (sv < TCP_SD_MIN) { 12225 /* 12226 * We do not know that if sa captures the delay ACK 12227 * effect as in a long train of segments, a receiver 12228 * does not delay its ACKs. So set the minimum of sv 12229 * to be TCP_SD_MIN, which is default to 400 ms, twice 12230 * of BSD DATO. That means the minimum of mean 12231 * deviation is 100 ms. 12232 * 12233 */ 12234 sv = TCP_SD_MIN; 12235 } 12236 tcp->tcp_rtt_sa = sa; 12237 tcp->tcp_rtt_sd = sv; 12238 /* 12239 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12240 * 12241 * Add tcp_rexmit_interval extra in case of extreme environment 12242 * where the algorithm fails to work. The default value of 12243 * tcp_rexmit_interval_extra should be 0. 12244 * 12245 * As we use a finer grained clock than BSD and update 12246 * RTO for every ACKs, add in another .25 of RTT to the 12247 * deviation of RTO to accomodate burstiness of 1/4 of 12248 * window size. 12249 */ 12250 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12251 12252 if (rto > tcps->tcps_rexmit_interval_max) { 12253 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12254 } else if (rto < tcps->tcps_rexmit_interval_min) { 12255 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12256 } else { 12257 tcp->tcp_rto = rto; 12258 } 12259 12260 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12261 tcp->tcp_timer_backoff = 0; 12262 } 12263 12264 /* 12265 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12266 * send queue which starts at the given seq. no. 12267 * 12268 * Parameters: 12269 * tcp_t *tcp: the tcp instance pointer. 12270 * uint32_t seq: the starting seq. no of the requested segment. 12271 * int32_t *off: after the execution, *off will be the offset to 12272 * the returned mblk which points to the requested seq no. 12273 * It is the caller's responsibility to send in a non-null off. 12274 * 12275 * Return: 12276 * A mblk_t pointer pointing to the requested segment in send queue. 12277 */ 12278 static mblk_t * 12279 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12280 { 12281 int32_t cnt; 12282 mblk_t *mp; 12283 12284 /* Defensive coding. Make sure we don't send incorrect data. */ 12285 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12286 return (NULL); 12287 12288 cnt = seq - tcp->tcp_suna; 12289 mp = tcp->tcp_xmit_head; 12290 while (cnt > 0 && mp != NULL) { 12291 cnt -= mp->b_wptr - mp->b_rptr; 12292 if (cnt < 0) { 12293 cnt += mp->b_wptr - mp->b_rptr; 12294 break; 12295 } 12296 mp = mp->b_cont; 12297 } 12298 ASSERT(mp != NULL); 12299 *off = cnt; 12300 return (mp); 12301 } 12302 12303 /* 12304 * This function handles all retransmissions if SACK is enabled for this 12305 * connection. First it calculates how many segments can be retransmitted 12306 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12307 * segments. A segment is eligible if sack_cnt for that segment is greater 12308 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12309 * all eligible segments, it checks to see if TCP can send some new segments 12310 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12311 * 12312 * Parameters: 12313 * tcp_t *tcp: the tcp structure of the connection. 12314 * uint_t *flags: in return, appropriate value will be set for 12315 * tcp_rput_data(). 12316 */ 12317 static void 12318 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12319 { 12320 notsack_blk_t *notsack_blk; 12321 int32_t usable_swnd; 12322 int32_t mss; 12323 uint32_t seg_len; 12324 mblk_t *xmit_mp; 12325 tcp_stack_t *tcps = tcp->tcp_tcps; 12326 12327 ASSERT(tcp->tcp_sack_info != NULL); 12328 ASSERT(tcp->tcp_notsack_list != NULL); 12329 ASSERT(tcp->tcp_rexmit == B_FALSE); 12330 12331 /* Defensive coding in case there is a bug... */ 12332 if (tcp->tcp_notsack_list == NULL) { 12333 return; 12334 } 12335 notsack_blk = tcp->tcp_notsack_list; 12336 mss = tcp->tcp_mss; 12337 12338 /* 12339 * Limit the num of outstanding data in the network to be 12340 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12341 */ 12342 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12343 12344 /* At least retransmit 1 MSS of data. */ 12345 if (usable_swnd <= 0) { 12346 usable_swnd = mss; 12347 } 12348 12349 /* Make sure no new RTT samples will be taken. */ 12350 tcp->tcp_csuna = tcp->tcp_snxt; 12351 12352 notsack_blk = tcp->tcp_notsack_list; 12353 while (usable_swnd > 0) { 12354 mblk_t *snxt_mp, *tmp_mp; 12355 tcp_seq begin = tcp->tcp_sack_snxt; 12356 tcp_seq end; 12357 int32_t off; 12358 12359 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12360 if (SEQ_GT(notsack_blk->end, begin) && 12361 (notsack_blk->sack_cnt >= 12362 tcps->tcps_dupack_fast_retransmit)) { 12363 end = notsack_blk->end; 12364 if (SEQ_LT(begin, notsack_blk->begin)) { 12365 begin = notsack_blk->begin; 12366 } 12367 break; 12368 } 12369 } 12370 /* 12371 * All holes are filled. Manipulate tcp_cwnd to send more 12372 * if we can. Note that after the SACK recovery, tcp_cwnd is 12373 * set to tcp_cwnd_ssthresh. 12374 */ 12375 if (notsack_blk == NULL) { 12376 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12377 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12378 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12379 ASSERT(tcp->tcp_cwnd > 0); 12380 return; 12381 } else { 12382 usable_swnd = usable_swnd / mss; 12383 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12384 MAX(usable_swnd * mss, mss); 12385 *flags |= TH_XMIT_NEEDED; 12386 return; 12387 } 12388 } 12389 12390 /* 12391 * Note that we may send more than usable_swnd allows here 12392 * because of round off, but no more than 1 MSS of data. 12393 */ 12394 seg_len = end - begin; 12395 if (seg_len > mss) 12396 seg_len = mss; 12397 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12398 ASSERT(snxt_mp != NULL); 12399 /* This should not happen. Defensive coding again... */ 12400 if (snxt_mp == NULL) { 12401 return; 12402 } 12403 12404 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12405 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12406 if (xmit_mp == NULL) 12407 return; 12408 12409 usable_swnd -= seg_len; 12410 tcp->tcp_pipe += seg_len; 12411 tcp->tcp_sack_snxt = begin + seg_len; 12412 12413 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12414 12415 /* 12416 * Update the send timestamp to avoid false retransmission. 12417 */ 12418 snxt_mp->b_prev = (mblk_t *)lbolt; 12419 12420 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12421 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12422 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12423 /* 12424 * Update tcp_rexmit_max to extend this SACK recovery phase. 12425 * This happens when new data sent during fast recovery is 12426 * also lost. If TCP retransmits those new data, it needs 12427 * to extend SACK recover phase to avoid starting another 12428 * fast retransmit/recovery unnecessarily. 12429 */ 12430 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12431 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12432 } 12433 } 12434 } 12435 12436 /* 12437 * This function handles policy checking at TCP level for non-hard_bound/ 12438 * detached connections. 12439 */ 12440 static boolean_t 12441 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12442 boolean_t secure, boolean_t mctl_present) 12443 { 12444 ipsec_latch_t *ipl = NULL; 12445 ipsec_action_t *act = NULL; 12446 mblk_t *data_mp; 12447 ipsec_in_t *ii; 12448 const char *reason; 12449 kstat_named_t *counter; 12450 tcp_stack_t *tcps = tcp->tcp_tcps; 12451 ipsec_stack_t *ipss; 12452 ip_stack_t *ipst; 12453 12454 ASSERT(mctl_present || !secure); 12455 12456 ASSERT((ipha == NULL && ip6h != NULL) || 12457 (ip6h == NULL && ipha != NULL)); 12458 12459 /* 12460 * We don't necessarily have an ipsec_in_act action to verify 12461 * policy because of assymetrical policy where we have only 12462 * outbound policy and no inbound policy (possible with global 12463 * policy). 12464 */ 12465 if (!secure) { 12466 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12467 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12468 return (B_TRUE); 12469 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12470 "tcp_check_policy", ipha, ip6h, secure, 12471 tcps->tcps_netstack); 12472 ipss = tcps->tcps_netstack->netstack_ipsec; 12473 12474 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12475 DROPPER(ipss, ipds_tcp_clear), 12476 &tcps->tcps_dropper); 12477 return (B_FALSE); 12478 } 12479 12480 /* 12481 * We have a secure packet. 12482 */ 12483 if (act == NULL) { 12484 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12485 "tcp_check_policy", ipha, ip6h, secure, 12486 tcps->tcps_netstack); 12487 ipss = tcps->tcps_netstack->netstack_ipsec; 12488 12489 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12490 DROPPER(ipss, ipds_tcp_secure), 12491 &tcps->tcps_dropper); 12492 return (B_FALSE); 12493 } 12494 12495 /* 12496 * XXX This whole routine is currently incorrect. ipl should 12497 * be set to the latch pointer, but is currently not set, so 12498 * we initialize it to NULL to avoid picking up random garbage. 12499 */ 12500 if (ipl == NULL) 12501 return (B_TRUE); 12502 12503 data_mp = first_mp->b_cont; 12504 12505 ii = (ipsec_in_t *)first_mp->b_rptr; 12506 12507 ipst = tcps->tcps_netstack->netstack_ip; 12508 12509 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12510 &counter, tcp->tcp_connp)) { 12511 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12512 return (B_TRUE); 12513 } 12514 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12515 "tcp inbound policy mismatch: %s, packet dropped\n", 12516 reason); 12517 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12518 12519 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12520 &tcps->tcps_dropper); 12521 return (B_FALSE); 12522 } 12523 12524 /* 12525 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12526 * retransmission after a timeout. 12527 * 12528 * To limit the number of duplicate segments, we limit the number of segment 12529 * to be sent in one time to tcp_snd_burst, the burst variable. 12530 */ 12531 static void 12532 tcp_ss_rexmit(tcp_t *tcp) 12533 { 12534 uint32_t snxt; 12535 uint32_t smax; 12536 int32_t win; 12537 int32_t mss; 12538 int32_t off; 12539 int32_t burst = tcp->tcp_snd_burst; 12540 mblk_t *snxt_mp; 12541 tcp_stack_t *tcps = tcp->tcp_tcps; 12542 12543 /* 12544 * Note that tcp_rexmit can be set even though TCP has retransmitted 12545 * all unack'ed segments. 12546 */ 12547 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12548 smax = tcp->tcp_rexmit_max; 12549 snxt = tcp->tcp_rexmit_nxt; 12550 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12551 snxt = tcp->tcp_suna; 12552 } 12553 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12554 win -= snxt - tcp->tcp_suna; 12555 mss = tcp->tcp_mss; 12556 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12557 12558 while (SEQ_LT(snxt, smax) && (win > 0) && 12559 (burst > 0) && (snxt_mp != NULL)) { 12560 mblk_t *xmit_mp; 12561 mblk_t *old_snxt_mp = snxt_mp; 12562 uint32_t cnt = mss; 12563 12564 if (win < cnt) { 12565 cnt = win; 12566 } 12567 if (SEQ_GT(snxt + cnt, smax)) { 12568 cnt = smax - snxt; 12569 } 12570 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12571 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12572 if (xmit_mp == NULL) 12573 return; 12574 12575 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12576 12577 snxt += cnt; 12578 win -= cnt; 12579 /* 12580 * Update the send timestamp to avoid false 12581 * retransmission. 12582 */ 12583 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12584 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12585 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12586 12587 tcp->tcp_rexmit_nxt = snxt; 12588 burst--; 12589 } 12590 /* 12591 * If we have transmitted all we have at the time 12592 * we started the retranmission, we can leave 12593 * the rest of the job to tcp_wput_data(). But we 12594 * need to check the send window first. If the 12595 * win is not 0, go on with tcp_wput_data(). 12596 */ 12597 if (SEQ_LT(snxt, smax) || win == 0) { 12598 return; 12599 } 12600 } 12601 /* Only call tcp_wput_data() if there is data to be sent. */ 12602 if (tcp->tcp_unsent) { 12603 tcp_wput_data(tcp, NULL, B_FALSE); 12604 } 12605 } 12606 12607 /* 12608 * Process all TCP option in SYN segment. Note that this function should 12609 * be called after tcp_adapt_ire() is called so that the necessary info 12610 * from IRE is already set in the tcp structure. 12611 * 12612 * This function sets up the correct tcp_mss value according to the 12613 * MSS option value and our header size. It also sets up the window scale 12614 * and timestamp values, and initialize SACK info blocks. But it does not 12615 * change receive window size after setting the tcp_mss value. The caller 12616 * should do the appropriate change. 12617 */ 12618 void 12619 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12620 { 12621 int options; 12622 tcp_opt_t tcpopt; 12623 uint32_t mss_max; 12624 char *tmp_tcph; 12625 tcp_stack_t *tcps = tcp->tcp_tcps; 12626 12627 tcpopt.tcp = NULL; 12628 options = tcp_parse_options(tcph, &tcpopt); 12629 12630 /* 12631 * Process MSS option. Note that MSS option value does not account 12632 * for IP or TCP options. This means that it is equal to MTU - minimum 12633 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12634 * IPv6. 12635 */ 12636 if (!(options & TCP_OPT_MSS_PRESENT)) { 12637 if (tcp->tcp_ipversion == IPV4_VERSION) 12638 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12639 else 12640 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12641 } else { 12642 if (tcp->tcp_ipversion == IPV4_VERSION) 12643 mss_max = tcps->tcps_mss_max_ipv4; 12644 else 12645 mss_max = tcps->tcps_mss_max_ipv6; 12646 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12647 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12648 else if (tcpopt.tcp_opt_mss > mss_max) 12649 tcpopt.tcp_opt_mss = mss_max; 12650 } 12651 12652 /* Process Window Scale option. */ 12653 if (options & TCP_OPT_WSCALE_PRESENT) { 12654 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12655 tcp->tcp_snd_ws_ok = B_TRUE; 12656 } else { 12657 tcp->tcp_snd_ws = B_FALSE; 12658 tcp->tcp_snd_ws_ok = B_FALSE; 12659 tcp->tcp_rcv_ws = B_FALSE; 12660 } 12661 12662 /* Process Timestamp option. */ 12663 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12664 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12665 tmp_tcph = (char *)tcp->tcp_tcph; 12666 12667 tcp->tcp_snd_ts_ok = B_TRUE; 12668 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12669 tcp->tcp_last_rcv_lbolt = lbolt64; 12670 ASSERT(OK_32PTR(tmp_tcph)); 12671 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12672 12673 /* Fill in our template header with basic timestamp option. */ 12674 tmp_tcph += tcp->tcp_tcp_hdr_len; 12675 tmp_tcph[0] = TCPOPT_NOP; 12676 tmp_tcph[1] = TCPOPT_NOP; 12677 tmp_tcph[2] = TCPOPT_TSTAMP; 12678 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12679 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12680 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12681 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12682 } else { 12683 tcp->tcp_snd_ts_ok = B_FALSE; 12684 } 12685 12686 /* 12687 * Process SACK options. If SACK is enabled for this connection, 12688 * then allocate the SACK info structure. Note the following ways 12689 * when tcp_snd_sack_ok is set to true. 12690 * 12691 * For active connection: in tcp_adapt_ire() called in 12692 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12693 * is checked. 12694 * 12695 * For passive connection: in tcp_adapt_ire() called in 12696 * tcp_accept_comm(). 12697 * 12698 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12699 * That check makes sure that if we did not send a SACK OK option, 12700 * we will not enable SACK for this connection even though the other 12701 * side sends us SACK OK option. For active connection, the SACK 12702 * info structure has already been allocated. So we need to free 12703 * it if SACK is disabled. 12704 */ 12705 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12706 (tcp->tcp_snd_sack_ok || 12707 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12708 /* This should be true only in the passive case. */ 12709 if (tcp->tcp_sack_info == NULL) { 12710 ASSERT(TCP_IS_DETACHED(tcp)); 12711 tcp->tcp_sack_info = 12712 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12713 } 12714 if (tcp->tcp_sack_info == NULL) { 12715 tcp->tcp_snd_sack_ok = B_FALSE; 12716 } else { 12717 tcp->tcp_snd_sack_ok = B_TRUE; 12718 if (tcp->tcp_snd_ts_ok) { 12719 tcp->tcp_max_sack_blk = 3; 12720 } else { 12721 tcp->tcp_max_sack_blk = 4; 12722 } 12723 } 12724 } else { 12725 /* 12726 * Resetting tcp_snd_sack_ok to B_FALSE so that 12727 * no SACK info will be used for this 12728 * connection. This assumes that SACK usage 12729 * permission is negotiated. This may need 12730 * to be changed once this is clarified. 12731 */ 12732 if (tcp->tcp_sack_info != NULL) { 12733 ASSERT(tcp->tcp_notsack_list == NULL); 12734 kmem_cache_free(tcp_sack_info_cache, 12735 tcp->tcp_sack_info); 12736 tcp->tcp_sack_info = NULL; 12737 } 12738 tcp->tcp_snd_sack_ok = B_FALSE; 12739 } 12740 12741 /* 12742 * Now we know the exact TCP/IP header length, subtract 12743 * that from tcp_mss to get our side's MSS. 12744 */ 12745 tcp->tcp_mss -= tcp->tcp_hdr_len; 12746 /* 12747 * Here we assume that the other side's header size will be equal to 12748 * our header size. We calculate the real MSS accordingly. Need to 12749 * take into additional stuffs IPsec puts in. 12750 * 12751 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12752 */ 12753 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12754 ((tcp->tcp_ipversion == IPV4_VERSION ? 12755 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12756 12757 /* 12758 * Set MSS to the smaller one of both ends of the connection. 12759 * We should not have called tcp_mss_set() before, but our 12760 * side of the MSS should have been set to a proper value 12761 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12762 * STREAM head parameters properly. 12763 * 12764 * If we have a larger-than-16-bit window but the other side 12765 * didn't want to do window scale, tcp_rwnd_set() will take 12766 * care of that. 12767 */ 12768 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12769 } 12770 12771 /* 12772 * Sends the T_CONN_IND to the listener. The caller calls this 12773 * functions via squeue to get inside the listener's perimeter 12774 * once the 3 way hand shake is done a T_CONN_IND needs to be 12775 * sent. As an optimization, the caller can call this directly 12776 * if listener's perimeter is same as eager's. 12777 */ 12778 /* ARGSUSED */ 12779 void 12780 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12781 { 12782 conn_t *lconnp = (conn_t *)arg; 12783 tcp_t *listener = lconnp->conn_tcp; 12784 tcp_t *tcp; 12785 struct T_conn_ind *conn_ind; 12786 ipaddr_t *addr_cache; 12787 boolean_t need_send_conn_ind = B_FALSE; 12788 tcp_stack_t *tcps = listener->tcp_tcps; 12789 12790 /* retrieve the eager */ 12791 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12792 ASSERT(conn_ind->OPT_offset != 0 && 12793 conn_ind->OPT_length == sizeof (intptr_t)); 12794 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12795 conn_ind->OPT_length); 12796 12797 /* 12798 * TLI/XTI applications will get confused by 12799 * sending eager as an option since it violates 12800 * the option semantics. So remove the eager as 12801 * option since TLI/XTI app doesn't need it anyway. 12802 */ 12803 if (!TCP_IS_SOCKET(listener)) { 12804 conn_ind->OPT_length = 0; 12805 conn_ind->OPT_offset = 0; 12806 } 12807 if (listener->tcp_state == TCPS_CLOSED || 12808 TCP_IS_DETACHED(listener)) { 12809 /* 12810 * If listener has closed, it would have caused a 12811 * a cleanup/blowoff to happen for the eager. We 12812 * just need to return. 12813 */ 12814 freemsg(mp); 12815 return; 12816 } 12817 12818 12819 /* 12820 * if the conn_req_q is full defer passing up the 12821 * T_CONN_IND until space is availabe after t_accept() 12822 * processing 12823 */ 12824 mutex_enter(&listener->tcp_eager_lock); 12825 12826 /* 12827 * Take the eager out, if it is in the list of droppable eagers 12828 * as we are here because the 3W handshake is over. 12829 */ 12830 MAKE_UNDROPPABLE(tcp); 12831 12832 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12833 tcp_t *tail; 12834 12835 /* 12836 * The eager already has an extra ref put in tcp_rput_data 12837 * so that it stays till accept comes back even though it 12838 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12839 */ 12840 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12841 listener->tcp_conn_req_cnt_q0--; 12842 listener->tcp_conn_req_cnt_q++; 12843 12844 /* Move from SYN_RCVD to ESTABLISHED list */ 12845 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12846 tcp->tcp_eager_prev_q0; 12847 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12848 tcp->tcp_eager_next_q0; 12849 tcp->tcp_eager_prev_q0 = NULL; 12850 tcp->tcp_eager_next_q0 = NULL; 12851 12852 /* 12853 * Insert at end of the queue because sockfs 12854 * sends down T_CONN_RES in chronological 12855 * order. Leaving the older conn indications 12856 * at front of the queue helps reducing search 12857 * time. 12858 */ 12859 tail = listener->tcp_eager_last_q; 12860 if (tail != NULL) 12861 tail->tcp_eager_next_q = tcp; 12862 else 12863 listener->tcp_eager_next_q = tcp; 12864 listener->tcp_eager_last_q = tcp; 12865 tcp->tcp_eager_next_q = NULL; 12866 /* 12867 * Delay sending up the T_conn_ind until we are 12868 * done with the eager. Once we have have sent up 12869 * the T_conn_ind, the accept can potentially complete 12870 * any time and release the refhold we have on the eager. 12871 */ 12872 need_send_conn_ind = B_TRUE; 12873 } else { 12874 /* 12875 * Defer connection on q0 and set deferred 12876 * connection bit true 12877 */ 12878 tcp->tcp_conn_def_q0 = B_TRUE; 12879 12880 /* take tcp out of q0 ... */ 12881 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12882 tcp->tcp_eager_next_q0; 12883 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12884 tcp->tcp_eager_prev_q0; 12885 12886 /* ... and place it at the end of q0 */ 12887 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12888 tcp->tcp_eager_next_q0 = listener; 12889 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12890 listener->tcp_eager_prev_q0 = tcp; 12891 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12892 } 12893 12894 /* we have timed out before */ 12895 if (tcp->tcp_syn_rcvd_timeout != 0) { 12896 tcp->tcp_syn_rcvd_timeout = 0; 12897 listener->tcp_syn_rcvd_timeout--; 12898 if (listener->tcp_syn_defense && 12899 listener->tcp_syn_rcvd_timeout <= 12900 (tcps->tcps_conn_req_max_q0 >> 5) && 12901 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12902 listener->tcp_last_rcv_lbolt)) { 12903 /* 12904 * Turn off the defense mode if we 12905 * believe the SYN attack is over. 12906 */ 12907 listener->tcp_syn_defense = B_FALSE; 12908 if (listener->tcp_ip_addr_cache) { 12909 kmem_free((void *)listener->tcp_ip_addr_cache, 12910 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12911 listener->tcp_ip_addr_cache = NULL; 12912 } 12913 } 12914 } 12915 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12916 if (addr_cache != NULL) { 12917 /* 12918 * We have finished a 3-way handshake with this 12919 * remote host. This proves the IP addr is good. 12920 * Cache it! 12921 */ 12922 addr_cache[IP_ADDR_CACHE_HASH( 12923 tcp->tcp_remote)] = tcp->tcp_remote; 12924 } 12925 mutex_exit(&listener->tcp_eager_lock); 12926 if (need_send_conn_ind) 12927 putnext(listener->tcp_rq, mp); 12928 } 12929 12930 mblk_t * 12931 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12932 uint_t *ifindexp, ip6_pkt_t *ippp) 12933 { 12934 ip_pktinfo_t *pinfo; 12935 ip6_t *ip6h; 12936 uchar_t *rptr; 12937 mblk_t *first_mp = mp; 12938 boolean_t mctl_present = B_FALSE; 12939 uint_t ifindex = 0; 12940 ip6_pkt_t ipp; 12941 uint_t ipvers; 12942 uint_t ip_hdr_len; 12943 tcp_stack_t *tcps = tcp->tcp_tcps; 12944 12945 rptr = mp->b_rptr; 12946 ASSERT(OK_32PTR(rptr)); 12947 ASSERT(tcp != NULL); 12948 ipp.ipp_fields = 0; 12949 12950 switch DB_TYPE(mp) { 12951 case M_CTL: 12952 mp = mp->b_cont; 12953 if (mp == NULL) { 12954 freemsg(first_mp); 12955 return (NULL); 12956 } 12957 if (DB_TYPE(mp) != M_DATA) { 12958 freemsg(first_mp); 12959 return (NULL); 12960 } 12961 mctl_present = B_TRUE; 12962 break; 12963 case M_DATA: 12964 break; 12965 default: 12966 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12967 freemsg(mp); 12968 return (NULL); 12969 } 12970 ipvers = IPH_HDR_VERSION(rptr); 12971 if (ipvers == IPV4_VERSION) { 12972 if (tcp == NULL) { 12973 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12974 goto done; 12975 } 12976 12977 ipp.ipp_fields |= IPPF_HOPLIMIT; 12978 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12979 12980 /* 12981 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12982 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12983 */ 12984 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12985 mctl_present) { 12986 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12987 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12988 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12989 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12990 ipp.ipp_fields |= IPPF_IFINDEX; 12991 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12992 ifindex = pinfo->ip_pkt_ifindex; 12993 } 12994 freeb(first_mp); 12995 mctl_present = B_FALSE; 12996 } 12997 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12998 } else { 12999 ip6h = (ip6_t *)rptr; 13000 13001 ASSERT(ipvers == IPV6_VERSION); 13002 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 13003 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 13004 ipp.ipp_hoplimit = ip6h->ip6_hops; 13005 13006 if (ip6h->ip6_nxt != IPPROTO_TCP) { 13007 uint8_t nexthdrp; 13008 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13009 13010 /* Look for ifindex information */ 13011 if (ip6h->ip6_nxt == IPPROTO_RAW) { 13012 ip6i_t *ip6i = (ip6i_t *)ip6h; 13013 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 13014 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13015 freemsg(first_mp); 13016 return (NULL); 13017 } 13018 13019 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 13020 ASSERT(ip6i->ip6i_ifindex != 0); 13021 ipp.ipp_fields |= IPPF_IFINDEX; 13022 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 13023 ifindex = ip6i->ip6i_ifindex; 13024 } 13025 rptr = (uchar_t *)&ip6i[1]; 13026 mp->b_rptr = rptr; 13027 if (rptr == mp->b_wptr) { 13028 mblk_t *mp1; 13029 mp1 = mp->b_cont; 13030 freeb(mp); 13031 mp = mp1; 13032 rptr = mp->b_rptr; 13033 } 13034 if (MBLKL(mp) < IPV6_HDR_LEN + 13035 sizeof (tcph_t)) { 13036 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13037 freemsg(first_mp); 13038 return (NULL); 13039 } 13040 ip6h = (ip6_t *)rptr; 13041 } 13042 13043 /* 13044 * Find any potentially interesting extension headers 13045 * as well as the length of the IPv6 + extension 13046 * headers. 13047 */ 13048 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 13049 /* Verify if this is a TCP packet */ 13050 if (nexthdrp != IPPROTO_TCP) { 13051 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13052 freemsg(first_mp); 13053 return (NULL); 13054 } 13055 } else { 13056 ip_hdr_len = IPV6_HDR_LEN; 13057 } 13058 } 13059 13060 done: 13061 if (ipversp != NULL) 13062 *ipversp = ipvers; 13063 if (ip_hdr_lenp != NULL) 13064 *ip_hdr_lenp = ip_hdr_len; 13065 if (ippp != NULL) 13066 *ippp = ipp; 13067 if (ifindexp != NULL) 13068 *ifindexp = ifindex; 13069 if (mctl_present) { 13070 freeb(first_mp); 13071 } 13072 return (mp); 13073 } 13074 13075 /* 13076 * Handle M_DATA messages from IP. Its called directly from IP via 13077 * squeue for AF_INET type sockets fast path. No M_CTL are expected 13078 * in this path. 13079 * 13080 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 13081 * v4 and v6), we are called through tcp_input() and a M_CTL can 13082 * be present for options but tcp_find_pktinfo() deals with it. We 13083 * only expect M_DATA packets after tcp_find_pktinfo() is done. 13084 * 13085 * The first argument is always the connp/tcp to which the mp belongs. 13086 * There are no exceptions to this rule. The caller has already put 13087 * a reference on this connp/tcp and once tcp_rput_data() returns, 13088 * the squeue will do the refrele. 13089 * 13090 * The TH_SYN for the listener directly go to tcp_conn_request via 13091 * squeue. 13092 * 13093 * sqp: NULL = recursive, sqp != NULL means called from squeue 13094 */ 13095 void 13096 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 13097 { 13098 int32_t bytes_acked; 13099 int32_t gap; 13100 mblk_t *mp1; 13101 uint_t flags; 13102 uint32_t new_swnd = 0; 13103 uchar_t *iphdr; 13104 uchar_t *rptr; 13105 int32_t rgap; 13106 uint32_t seg_ack; 13107 int seg_len; 13108 uint_t ip_hdr_len; 13109 uint32_t seg_seq; 13110 tcph_t *tcph; 13111 int urp; 13112 tcp_opt_t tcpopt; 13113 uint_t ipvers; 13114 ip6_pkt_t ipp; 13115 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 13116 uint32_t cwnd; 13117 uint32_t add; 13118 int npkt; 13119 int mss; 13120 conn_t *connp = (conn_t *)arg; 13121 squeue_t *sqp = (squeue_t *)arg2; 13122 tcp_t *tcp = connp->conn_tcp; 13123 tcp_stack_t *tcps = tcp->tcp_tcps; 13124 13125 /* 13126 * RST from fused tcp loopback peer should trigger an unfuse. 13127 */ 13128 if (tcp->tcp_fused) { 13129 TCP_STAT(tcps, tcp_fusion_aborted); 13130 tcp_unfuse(tcp); 13131 } 13132 13133 iphdr = mp->b_rptr; 13134 rptr = mp->b_rptr; 13135 ASSERT(OK_32PTR(rptr)); 13136 13137 /* 13138 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 13139 * processing here. For rest call tcp_find_pktinfo to fill up the 13140 * necessary information. 13141 */ 13142 if (IPCL_IS_TCP4(connp)) { 13143 ipvers = IPV4_VERSION; 13144 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13145 } else { 13146 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13147 NULL, &ipp); 13148 if (mp == NULL) { 13149 TCP_STAT(tcps, tcp_rput_v6_error); 13150 return; 13151 } 13152 iphdr = mp->b_rptr; 13153 rptr = mp->b_rptr; 13154 } 13155 ASSERT(DB_TYPE(mp) == M_DATA); 13156 13157 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13158 seg_seq = ABE32_TO_U32(tcph->th_seq); 13159 seg_ack = ABE32_TO_U32(tcph->th_ack); 13160 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13161 seg_len = (int)(mp->b_wptr - rptr) - 13162 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13163 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13164 do { 13165 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13166 (uintptr_t)INT_MAX); 13167 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13168 } while ((mp1 = mp1->b_cont) != NULL && 13169 mp1->b_datap->db_type == M_DATA); 13170 } 13171 13172 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13173 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13174 seg_len, tcph); 13175 return; 13176 } 13177 13178 if (sqp != NULL) { 13179 /* 13180 * This is the correct place to update tcp_last_recv_time. Note 13181 * that it is also updated for tcp structure that belongs to 13182 * global and listener queues which do not really need updating. 13183 * But that should not cause any harm. And it is updated for 13184 * all kinds of incoming segments, not only for data segments. 13185 */ 13186 tcp->tcp_last_recv_time = lbolt; 13187 } 13188 13189 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13190 13191 BUMP_LOCAL(tcp->tcp_ibsegs); 13192 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13193 13194 if ((flags & TH_URG) && sqp != NULL) { 13195 /* 13196 * TCP can't handle urgent pointers that arrive before 13197 * the connection has been accept()ed since it can't 13198 * buffer OOB data. Discard segment if this happens. 13199 * 13200 * We can't just rely on a non-null tcp_listener to indicate 13201 * that the accept() has completed since unlinking of the 13202 * eager and completion of the accept are not atomic. 13203 * tcp_detached, when it is not set (B_FALSE) indicates 13204 * that the accept() has completed. 13205 * 13206 * Nor can it reassemble urgent pointers, so discard 13207 * if it's not the next segment expected. 13208 * 13209 * Otherwise, collapse chain into one mblk (discard if 13210 * that fails). This makes sure the headers, retransmitted 13211 * data, and new data all are in the same mblk. 13212 */ 13213 ASSERT(mp != NULL); 13214 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13215 freemsg(mp); 13216 return; 13217 } 13218 /* Update pointers into message */ 13219 iphdr = rptr = mp->b_rptr; 13220 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13221 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13222 /* 13223 * Since we can't handle any data with this urgent 13224 * pointer that is out of sequence, we expunge 13225 * the data. This allows us to still register 13226 * the urgent mark and generate the M_PCSIG, 13227 * which we can do. 13228 */ 13229 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13230 seg_len = 0; 13231 } 13232 } 13233 13234 switch (tcp->tcp_state) { 13235 case TCPS_SYN_SENT: 13236 if (flags & TH_ACK) { 13237 /* 13238 * Note that our stack cannot send data before a 13239 * connection is established, therefore the 13240 * following check is valid. Otherwise, it has 13241 * to be changed. 13242 */ 13243 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13244 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13245 freemsg(mp); 13246 if (flags & TH_RST) 13247 return; 13248 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13249 tcp, seg_ack, 0, TH_RST); 13250 return; 13251 } 13252 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13253 } 13254 if (flags & TH_RST) { 13255 freemsg(mp); 13256 if (flags & TH_ACK) 13257 (void) tcp_clean_death(tcp, 13258 ECONNREFUSED, 13); 13259 return; 13260 } 13261 if (!(flags & TH_SYN)) { 13262 freemsg(mp); 13263 return; 13264 } 13265 13266 /* Process all TCP options. */ 13267 tcp_process_options(tcp, tcph); 13268 /* 13269 * The following changes our rwnd to be a multiple of the 13270 * MIN(peer MSS, our MSS) for performance reason. 13271 */ 13272 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13273 tcp->tcp_mss)); 13274 13275 /* Is the other end ECN capable? */ 13276 if (tcp->tcp_ecn_ok) { 13277 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13278 tcp->tcp_ecn_ok = B_FALSE; 13279 } 13280 } 13281 /* 13282 * Clear ECN flags because it may interfere with later 13283 * processing. 13284 */ 13285 flags &= ~(TH_ECE|TH_CWR); 13286 13287 tcp->tcp_irs = seg_seq; 13288 tcp->tcp_rack = seg_seq; 13289 tcp->tcp_rnxt = seg_seq + 1; 13290 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13291 if (!TCP_IS_DETACHED(tcp)) { 13292 /* Allocate room for SACK options if needed. */ 13293 if (tcp->tcp_snd_sack_ok) { 13294 (void) mi_set_sth_wroff(tcp->tcp_rq, 13295 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13296 (tcp->tcp_loopback ? 0 : 13297 tcps->tcps_wroff_xtra)); 13298 } else { 13299 (void) mi_set_sth_wroff(tcp->tcp_rq, 13300 tcp->tcp_hdr_len + 13301 (tcp->tcp_loopback ? 0 : 13302 tcps->tcps_wroff_xtra)); 13303 } 13304 } 13305 if (flags & TH_ACK) { 13306 /* 13307 * If we can't get the confirmation upstream, pretend 13308 * we didn't even see this one. 13309 * 13310 * XXX: how can we pretend we didn't see it if we 13311 * have updated rnxt et. al. 13312 * 13313 * For loopback we defer sending up the T_CONN_CON 13314 * until after some checks below. 13315 */ 13316 mp1 = NULL; 13317 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13318 tcp->tcp_loopback ? &mp1 : NULL)) { 13319 freemsg(mp); 13320 return; 13321 } 13322 /* SYN was acked - making progress */ 13323 if (tcp->tcp_ipversion == IPV6_VERSION) 13324 tcp->tcp_ip_forward_progress = B_TRUE; 13325 13326 /* One for the SYN */ 13327 tcp->tcp_suna = tcp->tcp_iss + 1; 13328 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13329 tcp->tcp_state = TCPS_ESTABLISHED; 13330 13331 /* 13332 * If SYN was retransmitted, need to reset all 13333 * retransmission info. This is because this 13334 * segment will be treated as a dup ACK. 13335 */ 13336 if (tcp->tcp_rexmit) { 13337 tcp->tcp_rexmit = B_FALSE; 13338 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13339 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13340 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13341 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13342 tcp->tcp_ms_we_have_waited = 0; 13343 13344 /* 13345 * Set tcp_cwnd back to 1 MSS, per 13346 * recommendation from 13347 * draft-floyd-incr-init-win-01.txt, 13348 * Increasing TCP's Initial Window. 13349 */ 13350 tcp->tcp_cwnd = tcp->tcp_mss; 13351 } 13352 13353 tcp->tcp_swl1 = seg_seq; 13354 tcp->tcp_swl2 = seg_ack; 13355 13356 new_swnd = BE16_TO_U16(tcph->th_win); 13357 tcp->tcp_swnd = new_swnd; 13358 if (new_swnd > tcp->tcp_max_swnd) 13359 tcp->tcp_max_swnd = new_swnd; 13360 13361 /* 13362 * Always send the three-way handshake ack immediately 13363 * in order to make the connection complete as soon as 13364 * possible on the accepting host. 13365 */ 13366 flags |= TH_ACK_NEEDED; 13367 13368 /* 13369 * Special case for loopback. At this point we have 13370 * received SYN-ACK from the remote endpoint. In 13371 * order to ensure that both endpoints reach the 13372 * fused state prior to any data exchange, the final 13373 * ACK needs to be sent before we indicate T_CONN_CON 13374 * to the module upstream. 13375 */ 13376 if (tcp->tcp_loopback) { 13377 mblk_t *ack_mp; 13378 13379 ASSERT(!tcp->tcp_unfusable); 13380 ASSERT(mp1 != NULL); 13381 /* 13382 * For loopback, we always get a pure SYN-ACK 13383 * and only need to send back the final ACK 13384 * with no data (this is because the other 13385 * tcp is ours and we don't do T/TCP). This 13386 * final ACK triggers the passive side to 13387 * perform fusion in ESTABLISHED state. 13388 */ 13389 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13390 if (tcp->tcp_ack_tid != 0) { 13391 (void) TCP_TIMER_CANCEL(tcp, 13392 tcp->tcp_ack_tid); 13393 tcp->tcp_ack_tid = 0; 13394 } 13395 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13396 BUMP_LOCAL(tcp->tcp_obsegs); 13397 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13398 13399 /* Send up T_CONN_CON */ 13400 putnext(tcp->tcp_rq, mp1); 13401 13402 freemsg(mp); 13403 return; 13404 } 13405 /* 13406 * Forget fusion; we need to handle more 13407 * complex cases below. Send the deferred 13408 * T_CONN_CON message upstream and proceed 13409 * as usual. Mark this tcp as not capable 13410 * of fusion. 13411 */ 13412 TCP_STAT(tcps, tcp_fusion_unfusable); 13413 tcp->tcp_unfusable = B_TRUE; 13414 putnext(tcp->tcp_rq, mp1); 13415 } 13416 13417 /* 13418 * Check to see if there is data to be sent. If 13419 * yes, set the transmit flag. Then check to see 13420 * if received data processing needs to be done. 13421 * If not, go straight to xmit_check. This short 13422 * cut is OK as we don't support T/TCP. 13423 */ 13424 if (tcp->tcp_unsent) 13425 flags |= TH_XMIT_NEEDED; 13426 13427 if (seg_len == 0 && !(flags & TH_URG)) { 13428 freemsg(mp); 13429 goto xmit_check; 13430 } 13431 13432 flags &= ~TH_SYN; 13433 seg_seq++; 13434 break; 13435 } 13436 tcp->tcp_state = TCPS_SYN_RCVD; 13437 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13438 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13439 if (mp1) { 13440 DB_CPID(mp1) = tcp->tcp_cpid; 13441 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13442 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13443 } 13444 freemsg(mp); 13445 return; 13446 case TCPS_SYN_RCVD: 13447 if (flags & TH_ACK) { 13448 /* 13449 * In this state, a SYN|ACK packet is either bogus 13450 * because the other side must be ACKing our SYN which 13451 * indicates it has seen the ACK for their SYN and 13452 * shouldn't retransmit it or we're crossing SYNs 13453 * on active open. 13454 */ 13455 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13456 freemsg(mp); 13457 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13458 tcp, seg_ack, 0, TH_RST); 13459 return; 13460 } 13461 /* 13462 * NOTE: RFC 793 pg. 72 says this should be 13463 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13464 * but that would mean we have an ack that ignored 13465 * our SYN. 13466 */ 13467 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13468 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13469 freemsg(mp); 13470 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13471 tcp, seg_ack, 0, TH_RST); 13472 return; 13473 } 13474 } 13475 break; 13476 case TCPS_LISTEN: 13477 /* 13478 * Only a TLI listener can come through this path when a 13479 * acceptor is going back to be a listener and a packet 13480 * for the acceptor hits the classifier. For a socket 13481 * listener, this can never happen because a listener 13482 * can never accept connection on itself and hence a 13483 * socket acceptor can not go back to being a listener. 13484 */ 13485 ASSERT(!TCP_IS_SOCKET(tcp)); 13486 /*FALLTHRU*/ 13487 case TCPS_CLOSED: 13488 case TCPS_BOUND: { 13489 conn_t *new_connp; 13490 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13491 13492 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13493 if (new_connp != NULL) { 13494 tcp_reinput(new_connp, mp, connp->conn_sqp); 13495 return; 13496 } 13497 /* We failed to classify. For now just drop the packet */ 13498 freemsg(mp); 13499 return; 13500 } 13501 case TCPS_IDLE: 13502 /* 13503 * Handle the case where the tcp_clean_death() has happened 13504 * on a connection (application hasn't closed yet) but a packet 13505 * was already queued on squeue before tcp_clean_death() 13506 * was processed. Calling tcp_clean_death() twice on same 13507 * connection can result in weird behaviour. 13508 */ 13509 freemsg(mp); 13510 return; 13511 default: 13512 break; 13513 } 13514 13515 /* 13516 * Already on the correct queue/perimeter. 13517 * If this is a detached connection and not an eager 13518 * connection hanging off a listener then new data 13519 * (past the FIN) will cause a reset. 13520 * We do a special check here where it 13521 * is out of the main line, rather than check 13522 * if we are detached every time we see new 13523 * data down below. 13524 */ 13525 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13526 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13527 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13528 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13529 13530 freemsg(mp); 13531 /* 13532 * This could be an SSL closure alert. We're detached so just 13533 * acknowledge it this last time. 13534 */ 13535 if (tcp->tcp_kssl_ctx != NULL) { 13536 kssl_release_ctx(tcp->tcp_kssl_ctx); 13537 tcp->tcp_kssl_ctx = NULL; 13538 13539 tcp->tcp_rnxt += seg_len; 13540 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13541 flags |= TH_ACK_NEEDED; 13542 goto ack_check; 13543 } 13544 13545 tcp_xmit_ctl("new data when detached", tcp, 13546 tcp->tcp_snxt, 0, TH_RST); 13547 (void) tcp_clean_death(tcp, EPROTO, 12); 13548 return; 13549 } 13550 13551 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13552 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13553 new_swnd = BE16_TO_U16(tcph->th_win) << 13554 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13555 13556 if (tcp->tcp_snd_ts_ok) { 13557 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13558 /* 13559 * This segment is not acceptable. 13560 * Drop it and send back an ACK. 13561 */ 13562 freemsg(mp); 13563 flags |= TH_ACK_NEEDED; 13564 goto ack_check; 13565 } 13566 } else if (tcp->tcp_snd_sack_ok) { 13567 ASSERT(tcp->tcp_sack_info != NULL); 13568 tcpopt.tcp = tcp; 13569 /* 13570 * SACK info in already updated in tcp_parse_options. Ignore 13571 * all other TCP options... 13572 */ 13573 (void) tcp_parse_options(tcph, &tcpopt); 13574 } 13575 try_again:; 13576 mss = tcp->tcp_mss; 13577 gap = seg_seq - tcp->tcp_rnxt; 13578 rgap = tcp->tcp_rwnd - (gap + seg_len); 13579 /* 13580 * gap is the amount of sequence space between what we expect to see 13581 * and what we got for seg_seq. A positive value for gap means 13582 * something got lost. A negative value means we got some old stuff. 13583 */ 13584 if (gap < 0) { 13585 /* Old stuff present. Is the SYN in there? */ 13586 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13587 (seg_len != 0)) { 13588 flags &= ~TH_SYN; 13589 seg_seq++; 13590 urp--; 13591 /* Recompute the gaps after noting the SYN. */ 13592 goto try_again; 13593 } 13594 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13595 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13596 (seg_len > -gap ? -gap : seg_len)); 13597 /* Remove the old stuff from seg_len. */ 13598 seg_len += gap; 13599 /* 13600 * Anything left? 13601 * Make sure to check for unack'd FIN when rest of data 13602 * has been previously ack'd. 13603 */ 13604 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13605 /* 13606 * Resets are only valid if they lie within our offered 13607 * window. If the RST bit is set, we just ignore this 13608 * segment. 13609 */ 13610 if (flags & TH_RST) { 13611 freemsg(mp); 13612 return; 13613 } 13614 13615 /* 13616 * The arriving of dup data packets indicate that we 13617 * may have postponed an ack for too long, or the other 13618 * side's RTT estimate is out of shape. Start acking 13619 * more often. 13620 */ 13621 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13622 tcp->tcp_rack_cnt >= 1 && 13623 tcp->tcp_rack_abs_max > 2) { 13624 tcp->tcp_rack_abs_max--; 13625 } 13626 tcp->tcp_rack_cur_max = 1; 13627 13628 /* 13629 * This segment is "unacceptable". None of its 13630 * sequence space lies within our advertized window. 13631 * 13632 * Adjust seg_len to the original value for tracing. 13633 */ 13634 seg_len -= gap; 13635 if (tcp->tcp_debug) { 13636 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13637 "tcp_rput: unacceptable, gap %d, rgap %d, " 13638 "flags 0x%x, seg_seq %u, seg_ack %u, " 13639 "seg_len %d, rnxt %u, snxt %u, %s", 13640 gap, rgap, flags, seg_seq, seg_ack, 13641 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13642 tcp_display(tcp, NULL, 13643 DISP_ADDR_AND_PORT)); 13644 } 13645 13646 /* 13647 * Arrange to send an ACK in response to the 13648 * unacceptable segment per RFC 793 page 69. There 13649 * is only one small difference between ours and the 13650 * acceptability test in the RFC - we accept ACK-only 13651 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13652 * will be generated. 13653 * 13654 * Note that we have to ACK an ACK-only packet at least 13655 * for stacks that send 0-length keep-alives with 13656 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13657 * section 4.2.3.6. As long as we don't ever generate 13658 * an unacceptable packet in response to an incoming 13659 * packet that is unacceptable, it should not cause 13660 * "ACK wars". 13661 */ 13662 flags |= TH_ACK_NEEDED; 13663 13664 /* 13665 * Continue processing this segment in order to use the 13666 * ACK information it contains, but skip all other 13667 * sequence-number processing. Processing the ACK 13668 * information is necessary in order to 13669 * re-synchronize connections that may have lost 13670 * synchronization. 13671 * 13672 * We clear seg_len and flag fields related to 13673 * sequence number processing as they are not 13674 * to be trusted for an unacceptable segment. 13675 */ 13676 seg_len = 0; 13677 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13678 goto process_ack; 13679 } 13680 13681 /* Fix seg_seq, and chew the gap off the front. */ 13682 seg_seq = tcp->tcp_rnxt; 13683 urp += gap; 13684 do { 13685 mblk_t *mp2; 13686 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13687 (uintptr_t)UINT_MAX); 13688 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13689 if (gap > 0) { 13690 mp->b_rptr = mp->b_wptr - gap; 13691 break; 13692 } 13693 mp2 = mp; 13694 mp = mp->b_cont; 13695 freeb(mp2); 13696 } while (gap < 0); 13697 /* 13698 * If the urgent data has already been acknowledged, we 13699 * should ignore TH_URG below 13700 */ 13701 if (urp < 0) 13702 flags &= ~TH_URG; 13703 } 13704 /* 13705 * rgap is the amount of stuff received out of window. A negative 13706 * value is the amount out of window. 13707 */ 13708 if (rgap < 0) { 13709 mblk_t *mp2; 13710 13711 if (tcp->tcp_rwnd == 0) { 13712 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13713 } else { 13714 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13715 UPDATE_MIB(&tcps->tcps_mib, 13716 tcpInDataPastWinBytes, -rgap); 13717 } 13718 13719 /* 13720 * seg_len does not include the FIN, so if more than 13721 * just the FIN is out of window, we act like we don't 13722 * see it. (If just the FIN is out of window, rgap 13723 * will be zero and we will go ahead and acknowledge 13724 * the FIN.) 13725 */ 13726 flags &= ~TH_FIN; 13727 13728 /* Fix seg_len and make sure there is something left. */ 13729 seg_len += rgap; 13730 if (seg_len <= 0) { 13731 /* 13732 * Resets are only valid if they lie within our offered 13733 * window. If the RST bit is set, we just ignore this 13734 * segment. 13735 */ 13736 if (flags & TH_RST) { 13737 freemsg(mp); 13738 return; 13739 } 13740 13741 /* Per RFC 793, we need to send back an ACK. */ 13742 flags |= TH_ACK_NEEDED; 13743 13744 /* 13745 * Send SIGURG as soon as possible i.e. even 13746 * if the TH_URG was delivered in a window probe 13747 * packet (which will be unacceptable). 13748 * 13749 * We generate a signal if none has been generated 13750 * for this connection or if this is a new urgent 13751 * byte. Also send a zero-length "unmarked" message 13752 * to inform SIOCATMARK that this is not the mark. 13753 * 13754 * tcp_urp_last_valid is cleared when the T_exdata_ind 13755 * is sent up. This plus the check for old data 13756 * (gap >= 0) handles the wraparound of the sequence 13757 * number space without having to always track the 13758 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13759 * this max in its rcv_up variable). 13760 * 13761 * This prevents duplicate SIGURGS due to a "late" 13762 * zero-window probe when the T_EXDATA_IND has already 13763 * been sent up. 13764 */ 13765 if ((flags & TH_URG) && 13766 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13767 tcp->tcp_urp_last))) { 13768 mp1 = allocb(0, BPRI_MED); 13769 if (mp1 == NULL) { 13770 freemsg(mp); 13771 return; 13772 } 13773 if (!TCP_IS_DETACHED(tcp) && 13774 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13775 SIGURG)) { 13776 /* Try again on the rexmit. */ 13777 freemsg(mp1); 13778 freemsg(mp); 13779 return; 13780 } 13781 /* 13782 * If the next byte would be the mark 13783 * then mark with MARKNEXT else mark 13784 * with NOTMARKNEXT. 13785 */ 13786 if (gap == 0 && urp == 0) 13787 mp1->b_flag |= MSGMARKNEXT; 13788 else 13789 mp1->b_flag |= MSGNOTMARKNEXT; 13790 freemsg(tcp->tcp_urp_mark_mp); 13791 tcp->tcp_urp_mark_mp = mp1; 13792 flags |= TH_SEND_URP_MARK; 13793 tcp->tcp_urp_last_valid = B_TRUE; 13794 tcp->tcp_urp_last = urp + seg_seq; 13795 } 13796 /* 13797 * If this is a zero window probe, continue to 13798 * process the ACK part. But we need to set seg_len 13799 * to 0 to avoid data processing. Otherwise just 13800 * drop the segment and send back an ACK. 13801 */ 13802 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13803 flags &= ~(TH_SYN | TH_URG); 13804 seg_len = 0; 13805 goto process_ack; 13806 } else { 13807 freemsg(mp); 13808 goto ack_check; 13809 } 13810 } 13811 /* Pitch out of window stuff off the end. */ 13812 rgap = seg_len; 13813 mp2 = mp; 13814 do { 13815 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13816 (uintptr_t)INT_MAX); 13817 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13818 if (rgap < 0) { 13819 mp2->b_wptr += rgap; 13820 if ((mp1 = mp2->b_cont) != NULL) { 13821 mp2->b_cont = NULL; 13822 freemsg(mp1); 13823 } 13824 break; 13825 } 13826 } while ((mp2 = mp2->b_cont) != NULL); 13827 } 13828 ok:; 13829 /* 13830 * TCP should check ECN info for segments inside the window only. 13831 * Therefore the check should be done here. 13832 */ 13833 if (tcp->tcp_ecn_ok) { 13834 if (flags & TH_CWR) { 13835 tcp->tcp_ecn_echo_on = B_FALSE; 13836 } 13837 /* 13838 * Note that both ECN_CE and CWR can be set in the 13839 * same segment. In this case, we once again turn 13840 * on ECN_ECHO. 13841 */ 13842 if (tcp->tcp_ipversion == IPV4_VERSION) { 13843 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13844 13845 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13846 tcp->tcp_ecn_echo_on = B_TRUE; 13847 } 13848 } else { 13849 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13850 13851 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13852 htonl(IPH_ECN_CE << 20)) { 13853 tcp->tcp_ecn_echo_on = B_TRUE; 13854 } 13855 } 13856 } 13857 13858 /* 13859 * Check whether we can update tcp_ts_recent. This test is 13860 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13861 * Extensions for High Performance: An Update", Internet Draft. 13862 */ 13863 if (tcp->tcp_snd_ts_ok && 13864 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13865 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13866 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13867 tcp->tcp_last_rcv_lbolt = lbolt64; 13868 } 13869 13870 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13871 /* 13872 * FIN in an out of order segment. We record this in 13873 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13874 * Clear the FIN so that any check on FIN flag will fail. 13875 * Remember that FIN also counts in the sequence number 13876 * space. So we need to ack out of order FIN only segments. 13877 */ 13878 if (flags & TH_FIN) { 13879 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13880 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13881 flags &= ~TH_FIN; 13882 flags |= TH_ACK_NEEDED; 13883 } 13884 if (seg_len > 0) { 13885 /* Fill in the SACK blk list. */ 13886 if (tcp->tcp_snd_sack_ok) { 13887 ASSERT(tcp->tcp_sack_info != NULL); 13888 tcp_sack_insert(tcp->tcp_sack_list, 13889 seg_seq, seg_seq + seg_len, 13890 &(tcp->tcp_num_sack_blk)); 13891 } 13892 13893 /* 13894 * Attempt reassembly and see if we have something 13895 * ready to go. 13896 */ 13897 mp = tcp_reass(tcp, mp, seg_seq); 13898 /* Always ack out of order packets */ 13899 flags |= TH_ACK_NEEDED | TH_PUSH; 13900 if (mp) { 13901 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13902 (uintptr_t)INT_MAX); 13903 seg_len = mp->b_cont ? msgdsize(mp) : 13904 (int)(mp->b_wptr - mp->b_rptr); 13905 seg_seq = tcp->tcp_rnxt; 13906 /* 13907 * A gap is filled and the seq num and len 13908 * of the gap match that of a previously 13909 * received FIN, put the FIN flag back in. 13910 */ 13911 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13912 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13913 flags |= TH_FIN; 13914 tcp->tcp_valid_bits &= 13915 ~TCP_OFO_FIN_VALID; 13916 } 13917 } else { 13918 /* 13919 * Keep going even with NULL mp. 13920 * There may be a useful ACK or something else 13921 * we don't want to miss. 13922 * 13923 * But TCP should not perform fast retransmit 13924 * because of the ack number. TCP uses 13925 * seg_len == 0 to determine if it is a pure 13926 * ACK. And this is not a pure ACK. 13927 */ 13928 seg_len = 0; 13929 ofo_seg = B_TRUE; 13930 } 13931 } 13932 } else if (seg_len > 0) { 13933 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13934 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13935 /* 13936 * If an out of order FIN was received before, and the seq 13937 * num and len of the new segment match that of the FIN, 13938 * put the FIN flag back in. 13939 */ 13940 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13941 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13942 flags |= TH_FIN; 13943 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13944 } 13945 } 13946 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13947 if (flags & TH_RST) { 13948 freemsg(mp); 13949 switch (tcp->tcp_state) { 13950 case TCPS_SYN_RCVD: 13951 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13952 break; 13953 case TCPS_ESTABLISHED: 13954 case TCPS_FIN_WAIT_1: 13955 case TCPS_FIN_WAIT_2: 13956 case TCPS_CLOSE_WAIT: 13957 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13958 break; 13959 case TCPS_CLOSING: 13960 case TCPS_LAST_ACK: 13961 (void) tcp_clean_death(tcp, 0, 16); 13962 break; 13963 default: 13964 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13965 (void) tcp_clean_death(tcp, ENXIO, 17); 13966 break; 13967 } 13968 return; 13969 } 13970 if (flags & TH_SYN) { 13971 /* 13972 * See RFC 793, Page 71 13973 * 13974 * The seq number must be in the window as it should 13975 * be "fixed" above. If it is outside window, it should 13976 * be already rejected. Note that we allow seg_seq to be 13977 * rnxt + rwnd because we want to accept 0 window probe. 13978 */ 13979 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13980 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13981 freemsg(mp); 13982 /* 13983 * If the ACK flag is not set, just use our snxt as the 13984 * seq number of the RST segment. 13985 */ 13986 if (!(flags & TH_ACK)) { 13987 seg_ack = tcp->tcp_snxt; 13988 } 13989 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13990 TH_RST|TH_ACK); 13991 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13992 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13993 return; 13994 } 13995 /* 13996 * urp could be -1 when the urp field in the packet is 0 13997 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13998 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13999 */ 14000 if (flags & TH_URG && urp >= 0) { 14001 if (!tcp->tcp_urp_last_valid || 14002 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 14003 /* 14004 * If we haven't generated the signal yet for this 14005 * urgent pointer value, do it now. Also, send up a 14006 * zero-length M_DATA indicating whether or not this is 14007 * the mark. The latter is not needed when a 14008 * T_EXDATA_IND is sent up. However, if there are 14009 * allocation failures this code relies on the sender 14010 * retransmitting and the socket code for determining 14011 * the mark should not block waiting for the peer to 14012 * transmit. Thus, for simplicity we always send up the 14013 * mark indication. 14014 */ 14015 mp1 = allocb(0, BPRI_MED); 14016 if (mp1 == NULL) { 14017 freemsg(mp); 14018 return; 14019 } 14020 if (!TCP_IS_DETACHED(tcp) && 14021 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 14022 /* Try again on the rexmit. */ 14023 freemsg(mp1); 14024 freemsg(mp); 14025 return; 14026 } 14027 /* 14028 * Mark with NOTMARKNEXT for now. 14029 * The code below will change this to MARKNEXT 14030 * if we are at the mark. 14031 * 14032 * If there are allocation failures (e.g. in dupmsg 14033 * below) the next time tcp_rput_data sees the urgent 14034 * segment it will send up the MSG*MARKNEXT message. 14035 */ 14036 mp1->b_flag |= MSGNOTMARKNEXT; 14037 freemsg(tcp->tcp_urp_mark_mp); 14038 tcp->tcp_urp_mark_mp = mp1; 14039 flags |= TH_SEND_URP_MARK; 14040 #ifdef DEBUG 14041 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14042 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 14043 "last %x, %s", 14044 seg_seq, urp, tcp->tcp_urp_last, 14045 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14046 #endif /* DEBUG */ 14047 tcp->tcp_urp_last_valid = B_TRUE; 14048 tcp->tcp_urp_last = urp + seg_seq; 14049 } else if (tcp->tcp_urp_mark_mp != NULL) { 14050 /* 14051 * An allocation failure prevented the previous 14052 * tcp_rput_data from sending up the allocated 14053 * MSG*MARKNEXT message - send it up this time 14054 * around. 14055 */ 14056 flags |= TH_SEND_URP_MARK; 14057 } 14058 14059 /* 14060 * If the urgent byte is in this segment, make sure that it is 14061 * all by itself. This makes it much easier to deal with the 14062 * possibility of an allocation failure on the T_exdata_ind. 14063 * Note that seg_len is the number of bytes in the segment, and 14064 * urp is the offset into the segment of the urgent byte. 14065 * urp < seg_len means that the urgent byte is in this segment. 14066 */ 14067 if (urp < seg_len) { 14068 if (seg_len != 1) { 14069 uint32_t tmp_rnxt; 14070 /* 14071 * Break it up and feed it back in. 14072 * Re-attach the IP header. 14073 */ 14074 mp->b_rptr = iphdr; 14075 if (urp > 0) { 14076 /* 14077 * There is stuff before the urgent 14078 * byte. 14079 */ 14080 mp1 = dupmsg(mp); 14081 if (!mp1) { 14082 /* 14083 * Trim from urgent byte on. 14084 * The rest will come back. 14085 */ 14086 (void) adjmsg(mp, 14087 urp - seg_len); 14088 tcp_rput_data(connp, 14089 mp, NULL); 14090 return; 14091 } 14092 (void) adjmsg(mp1, urp - seg_len); 14093 /* Feed this piece back in. */ 14094 tmp_rnxt = tcp->tcp_rnxt; 14095 tcp_rput_data(connp, mp1, NULL); 14096 /* 14097 * If the data passed back in was not 14098 * processed (ie: bad ACK) sending 14099 * the remainder back in will cause a 14100 * loop. In this case, drop the 14101 * packet and let the sender try 14102 * sending a good packet. 14103 */ 14104 if (tmp_rnxt == tcp->tcp_rnxt) { 14105 freemsg(mp); 14106 return; 14107 } 14108 } 14109 if (urp != seg_len - 1) { 14110 uint32_t tmp_rnxt; 14111 /* 14112 * There is stuff after the urgent 14113 * byte. 14114 */ 14115 mp1 = dupmsg(mp); 14116 if (!mp1) { 14117 /* 14118 * Trim everything beyond the 14119 * urgent byte. The rest will 14120 * come back. 14121 */ 14122 (void) adjmsg(mp, 14123 urp + 1 - seg_len); 14124 tcp_rput_data(connp, 14125 mp, NULL); 14126 return; 14127 } 14128 (void) adjmsg(mp1, urp + 1 - seg_len); 14129 tmp_rnxt = tcp->tcp_rnxt; 14130 tcp_rput_data(connp, mp1, NULL); 14131 /* 14132 * If the data passed back in was not 14133 * processed (ie: bad ACK) sending 14134 * the remainder back in will cause a 14135 * loop. In this case, drop the 14136 * packet and let the sender try 14137 * sending a good packet. 14138 */ 14139 if (tmp_rnxt == tcp->tcp_rnxt) { 14140 freemsg(mp); 14141 return; 14142 } 14143 } 14144 tcp_rput_data(connp, mp, NULL); 14145 return; 14146 } 14147 /* 14148 * This segment contains only the urgent byte. We 14149 * have to allocate the T_exdata_ind, if we can. 14150 */ 14151 if (!tcp->tcp_urp_mp) { 14152 struct T_exdata_ind *tei; 14153 mp1 = allocb(sizeof (struct T_exdata_ind), 14154 BPRI_MED); 14155 if (!mp1) { 14156 /* 14157 * Sigh... It'll be back. 14158 * Generate any MSG*MARK message now. 14159 */ 14160 freemsg(mp); 14161 seg_len = 0; 14162 if (flags & TH_SEND_URP_MARK) { 14163 14164 14165 ASSERT(tcp->tcp_urp_mark_mp); 14166 tcp->tcp_urp_mark_mp->b_flag &= 14167 ~MSGNOTMARKNEXT; 14168 tcp->tcp_urp_mark_mp->b_flag |= 14169 MSGMARKNEXT; 14170 } 14171 goto ack_check; 14172 } 14173 mp1->b_datap->db_type = M_PROTO; 14174 tei = (struct T_exdata_ind *)mp1->b_rptr; 14175 tei->PRIM_type = T_EXDATA_IND; 14176 tei->MORE_flag = 0; 14177 mp1->b_wptr = (uchar_t *)&tei[1]; 14178 tcp->tcp_urp_mp = mp1; 14179 #ifdef DEBUG 14180 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14181 "tcp_rput: allocated exdata_ind %s", 14182 tcp_display(tcp, NULL, 14183 DISP_PORT_ONLY)); 14184 #endif /* DEBUG */ 14185 /* 14186 * There is no need to send a separate MSG*MARK 14187 * message since the T_EXDATA_IND will be sent 14188 * now. 14189 */ 14190 flags &= ~TH_SEND_URP_MARK; 14191 freemsg(tcp->tcp_urp_mark_mp); 14192 tcp->tcp_urp_mark_mp = NULL; 14193 } 14194 /* 14195 * Now we are all set. On the next putnext upstream, 14196 * tcp_urp_mp will be non-NULL and will get prepended 14197 * to what has to be this piece containing the urgent 14198 * byte. If for any reason we abort this segment below, 14199 * if it comes back, we will have this ready, or it 14200 * will get blown off in close. 14201 */ 14202 } else if (urp == seg_len) { 14203 /* 14204 * The urgent byte is the next byte after this sequence 14205 * number. If there is data it is marked with 14206 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14207 * since it is not needed. Otherwise, if the code 14208 * above just allocated a zero-length tcp_urp_mark_mp 14209 * message, that message is tagged with MSGMARKNEXT. 14210 * Sending up these MSGMARKNEXT messages makes 14211 * SIOCATMARK work correctly even though 14212 * the T_EXDATA_IND will not be sent up until the 14213 * urgent byte arrives. 14214 */ 14215 if (seg_len != 0) { 14216 flags |= TH_MARKNEXT_NEEDED; 14217 freemsg(tcp->tcp_urp_mark_mp); 14218 tcp->tcp_urp_mark_mp = NULL; 14219 flags &= ~TH_SEND_URP_MARK; 14220 } else if (tcp->tcp_urp_mark_mp != NULL) { 14221 flags |= TH_SEND_URP_MARK; 14222 tcp->tcp_urp_mark_mp->b_flag &= 14223 ~MSGNOTMARKNEXT; 14224 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14225 } 14226 #ifdef DEBUG 14227 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14228 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14229 seg_len, flags, 14230 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14231 #endif /* DEBUG */ 14232 } else { 14233 /* Data left until we hit mark */ 14234 #ifdef DEBUG 14235 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14236 "tcp_rput: URP %d bytes left, %s", 14237 urp - seg_len, tcp_display(tcp, NULL, 14238 DISP_PORT_ONLY)); 14239 #endif /* DEBUG */ 14240 } 14241 } 14242 14243 process_ack: 14244 if (!(flags & TH_ACK)) { 14245 freemsg(mp); 14246 goto xmit_check; 14247 } 14248 } 14249 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14250 14251 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14252 tcp->tcp_ip_forward_progress = B_TRUE; 14253 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14254 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14255 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14256 /* 3-way handshake complete - pass up the T_CONN_IND */ 14257 tcp_t *listener = tcp->tcp_listener; 14258 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14259 14260 tcp->tcp_tconnind_started = B_TRUE; 14261 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14262 /* 14263 * We are here means eager is fine but it can 14264 * get a TH_RST at any point between now and till 14265 * accept completes and disappear. We need to 14266 * ensure that reference to eager is valid after 14267 * we get out of eager's perimeter. So we do 14268 * an extra refhold. 14269 */ 14270 CONN_INC_REF(connp); 14271 14272 /* 14273 * The listener also exists because of the refhold 14274 * done in tcp_conn_request. Its possible that it 14275 * might have closed. We will check that once we 14276 * get inside listeners context. 14277 */ 14278 CONN_INC_REF(listener->tcp_connp); 14279 if (listener->tcp_connp->conn_sqp == 14280 connp->conn_sqp) { 14281 tcp_send_conn_ind(listener->tcp_connp, mp, 14282 listener->tcp_connp->conn_sqp); 14283 CONN_DEC_REF(listener->tcp_connp); 14284 } else if (!tcp->tcp_loopback) { 14285 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14286 tcp_send_conn_ind, 14287 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14288 } else { 14289 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14290 tcp_send_conn_ind, listener->tcp_connp, 14291 SQTAG_TCP_CONN_IND); 14292 } 14293 } 14294 14295 if (tcp->tcp_active_open) { 14296 /* 14297 * We are seeing the final ack in the three way 14298 * hand shake of a active open'ed connection 14299 * so we must send up a T_CONN_CON 14300 */ 14301 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14302 freemsg(mp); 14303 return; 14304 } 14305 /* 14306 * Don't fuse the loopback endpoints for 14307 * simultaneous active opens. 14308 */ 14309 if (tcp->tcp_loopback) { 14310 TCP_STAT(tcps, tcp_fusion_unfusable); 14311 tcp->tcp_unfusable = B_TRUE; 14312 } 14313 } 14314 14315 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14316 bytes_acked--; 14317 /* SYN was acked - making progress */ 14318 if (tcp->tcp_ipversion == IPV6_VERSION) 14319 tcp->tcp_ip_forward_progress = B_TRUE; 14320 14321 /* 14322 * If SYN was retransmitted, need to reset all 14323 * retransmission info as this segment will be 14324 * treated as a dup ACK. 14325 */ 14326 if (tcp->tcp_rexmit) { 14327 tcp->tcp_rexmit = B_FALSE; 14328 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14329 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14330 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14331 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14332 tcp->tcp_ms_we_have_waited = 0; 14333 tcp->tcp_cwnd = mss; 14334 } 14335 14336 /* 14337 * We set the send window to zero here. 14338 * This is needed if there is data to be 14339 * processed already on the queue. 14340 * Later (at swnd_update label), the 14341 * "new_swnd > tcp_swnd" condition is satisfied 14342 * the XMIT_NEEDED flag is set in the current 14343 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14344 * called if there is already data on queue in 14345 * this state. 14346 */ 14347 tcp->tcp_swnd = 0; 14348 14349 if (new_swnd > tcp->tcp_max_swnd) 14350 tcp->tcp_max_swnd = new_swnd; 14351 tcp->tcp_swl1 = seg_seq; 14352 tcp->tcp_swl2 = seg_ack; 14353 tcp->tcp_state = TCPS_ESTABLISHED; 14354 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14355 14356 /* Fuse when both sides are in ESTABLISHED state */ 14357 if (tcp->tcp_loopback && do_tcp_fusion) 14358 tcp_fuse(tcp, iphdr, tcph); 14359 14360 } 14361 /* This code follows 4.4BSD-Lite2 mostly. */ 14362 if (bytes_acked < 0) 14363 goto est; 14364 14365 /* 14366 * If TCP is ECN capable and the congestion experience bit is 14367 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14368 * done once per window (or more loosely, per RTT). 14369 */ 14370 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14371 tcp->tcp_cwr = B_FALSE; 14372 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14373 if (!tcp->tcp_cwr) { 14374 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14375 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14376 tcp->tcp_cwnd = npkt * mss; 14377 /* 14378 * If the cwnd is 0, use the timer to clock out 14379 * new segments. This is required by the ECN spec. 14380 */ 14381 if (npkt == 0) { 14382 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14383 /* 14384 * This makes sure that when the ACK comes 14385 * back, we will increase tcp_cwnd by 1 MSS. 14386 */ 14387 tcp->tcp_cwnd_cnt = 0; 14388 } 14389 tcp->tcp_cwr = B_TRUE; 14390 /* 14391 * This marks the end of the current window of in 14392 * flight data. That is why we don't use 14393 * tcp_suna + tcp_swnd. Only data in flight can 14394 * provide ECN info. 14395 */ 14396 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14397 tcp->tcp_ecn_cwr_sent = B_FALSE; 14398 } 14399 } 14400 14401 mp1 = tcp->tcp_xmit_head; 14402 if (bytes_acked == 0) { 14403 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14404 int dupack_cnt; 14405 14406 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14407 /* 14408 * Fast retransmit. When we have seen exactly three 14409 * identical ACKs while we have unacked data 14410 * outstanding we take it as a hint that our peer 14411 * dropped something. 14412 * 14413 * If TCP is retransmitting, don't do fast retransmit. 14414 */ 14415 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14416 ! tcp->tcp_rexmit) { 14417 /* Do Limited Transmit */ 14418 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14419 tcps->tcps_dupack_fast_retransmit) { 14420 /* 14421 * RFC 3042 14422 * 14423 * What we need to do is temporarily 14424 * increase tcp_cwnd so that new 14425 * data can be sent if it is allowed 14426 * by the receive window (tcp_rwnd). 14427 * tcp_wput_data() will take care of 14428 * the rest. 14429 * 14430 * If the connection is SACK capable, 14431 * only do limited xmit when there 14432 * is SACK info. 14433 * 14434 * Note how tcp_cwnd is incremented. 14435 * The first dup ACK will increase 14436 * it by 1 MSS. The second dup ACK 14437 * will increase it by 2 MSS. This 14438 * means that only 1 new segment will 14439 * be sent for each dup ACK. 14440 */ 14441 if (tcp->tcp_unsent > 0 && 14442 (!tcp->tcp_snd_sack_ok || 14443 (tcp->tcp_snd_sack_ok && 14444 tcp->tcp_notsack_list != NULL))) { 14445 tcp->tcp_cwnd += mss << 14446 (tcp->tcp_dupack_cnt - 1); 14447 flags |= TH_LIMIT_XMIT; 14448 } 14449 } else if (dupack_cnt == 14450 tcps->tcps_dupack_fast_retransmit) { 14451 14452 /* 14453 * If we have reduced tcp_ssthresh 14454 * because of ECN, do not reduce it again 14455 * unless it is already one window of data 14456 * away. After one window of data, tcp_cwr 14457 * should then be cleared. Note that 14458 * for non ECN capable connection, tcp_cwr 14459 * should always be false. 14460 * 14461 * Adjust cwnd since the duplicate 14462 * ack indicates that a packet was 14463 * dropped (due to congestion.) 14464 */ 14465 if (!tcp->tcp_cwr) { 14466 npkt = ((tcp->tcp_snxt - 14467 tcp->tcp_suna) >> 1) / mss; 14468 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14469 mss; 14470 tcp->tcp_cwnd = (npkt + 14471 tcp->tcp_dupack_cnt) * mss; 14472 } 14473 if (tcp->tcp_ecn_ok) { 14474 tcp->tcp_cwr = B_TRUE; 14475 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14476 tcp->tcp_ecn_cwr_sent = B_FALSE; 14477 } 14478 14479 /* 14480 * We do Hoe's algorithm. Refer to her 14481 * paper "Improving the Start-up Behavior 14482 * of a Congestion Control Scheme for TCP," 14483 * appeared in SIGCOMM'96. 14484 * 14485 * Save highest seq no we have sent so far. 14486 * Be careful about the invisible FIN byte. 14487 */ 14488 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14489 (tcp->tcp_unsent == 0)) { 14490 tcp->tcp_rexmit_max = tcp->tcp_fss; 14491 } else { 14492 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14493 } 14494 14495 /* 14496 * Do not allow bursty traffic during. 14497 * fast recovery. Refer to Fall and Floyd's 14498 * paper "Simulation-based Comparisons of 14499 * Tahoe, Reno and SACK TCP" (in CCR?) 14500 * This is a best current practise. 14501 */ 14502 tcp->tcp_snd_burst = TCP_CWND_SS; 14503 14504 /* 14505 * For SACK: 14506 * Calculate tcp_pipe, which is the 14507 * estimated number of bytes in 14508 * network. 14509 * 14510 * tcp_fack is the highest sack'ed seq num 14511 * TCP has received. 14512 * 14513 * tcp_pipe is explained in the above quoted 14514 * Fall and Floyd's paper. tcp_fack is 14515 * explained in Mathis and Mahdavi's 14516 * "Forward Acknowledgment: Refining TCP 14517 * Congestion Control" in SIGCOMM '96. 14518 */ 14519 if (tcp->tcp_snd_sack_ok) { 14520 ASSERT(tcp->tcp_sack_info != NULL); 14521 if (tcp->tcp_notsack_list != NULL) { 14522 tcp->tcp_pipe = tcp->tcp_snxt - 14523 tcp->tcp_fack; 14524 tcp->tcp_sack_snxt = seg_ack; 14525 flags |= TH_NEED_SACK_REXMIT; 14526 } else { 14527 /* 14528 * Always initialize tcp_pipe 14529 * even though we don't have 14530 * any SACK info. If later 14531 * we get SACK info and 14532 * tcp_pipe is not initialized, 14533 * funny things will happen. 14534 */ 14535 tcp->tcp_pipe = 14536 tcp->tcp_cwnd_ssthresh; 14537 } 14538 } else { 14539 flags |= TH_REXMIT_NEEDED; 14540 } /* tcp_snd_sack_ok */ 14541 14542 } else { 14543 /* 14544 * Here we perform congestion 14545 * avoidance, but NOT slow start. 14546 * This is known as the Fast 14547 * Recovery Algorithm. 14548 */ 14549 if (tcp->tcp_snd_sack_ok && 14550 tcp->tcp_notsack_list != NULL) { 14551 flags |= TH_NEED_SACK_REXMIT; 14552 tcp->tcp_pipe -= mss; 14553 if (tcp->tcp_pipe < 0) 14554 tcp->tcp_pipe = 0; 14555 } else { 14556 /* 14557 * We know that one more packet has 14558 * left the pipe thus we can update 14559 * cwnd. 14560 */ 14561 cwnd = tcp->tcp_cwnd + mss; 14562 if (cwnd > tcp->tcp_cwnd_max) 14563 cwnd = tcp->tcp_cwnd_max; 14564 tcp->tcp_cwnd = cwnd; 14565 if (tcp->tcp_unsent > 0) 14566 flags |= TH_XMIT_NEEDED; 14567 } 14568 } 14569 } 14570 } else if (tcp->tcp_zero_win_probe) { 14571 /* 14572 * If the window has opened, need to arrange 14573 * to send additional data. 14574 */ 14575 if (new_swnd != 0) { 14576 /* tcp_suna != tcp_snxt */ 14577 /* Packet contains a window update */ 14578 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14579 tcp->tcp_zero_win_probe = 0; 14580 tcp->tcp_timer_backoff = 0; 14581 tcp->tcp_ms_we_have_waited = 0; 14582 14583 /* 14584 * Transmit starting with tcp_suna since 14585 * the one byte probe is not ack'ed. 14586 * If TCP has sent more than one identical 14587 * probe, tcp_rexmit will be set. That means 14588 * tcp_ss_rexmit() will send out the one 14589 * byte along with new data. Otherwise, 14590 * fake the retransmission. 14591 */ 14592 flags |= TH_XMIT_NEEDED; 14593 if (!tcp->tcp_rexmit) { 14594 tcp->tcp_rexmit = B_TRUE; 14595 tcp->tcp_dupack_cnt = 0; 14596 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14597 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14598 } 14599 } 14600 } 14601 goto swnd_update; 14602 } 14603 14604 /* 14605 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14606 * If the ACK value acks something that we have not yet sent, it might 14607 * be an old duplicate segment. Send an ACK to re-synchronize the 14608 * other side. 14609 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14610 * state is handled above, so we can always just drop the segment and 14611 * send an ACK here. 14612 * 14613 * Should we send ACKs in response to ACK only segments? 14614 */ 14615 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14616 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14617 /* drop the received segment */ 14618 freemsg(mp); 14619 14620 /* 14621 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14622 * greater than 0, check if the number of such 14623 * bogus ACks is greater than that count. If yes, 14624 * don't send back any ACK. This prevents TCP from 14625 * getting into an ACK storm if somehow an attacker 14626 * successfully spoofs an acceptable segment to our 14627 * peer. 14628 */ 14629 if (tcp_drop_ack_unsent_cnt > 0 && 14630 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14631 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14632 return; 14633 } 14634 mp = tcp_ack_mp(tcp); 14635 if (mp != NULL) { 14636 BUMP_LOCAL(tcp->tcp_obsegs); 14637 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14638 tcp_send_data(tcp, tcp->tcp_wq, mp); 14639 } 14640 return; 14641 } 14642 14643 /* 14644 * TCP gets a new ACK, update the notsack'ed list to delete those 14645 * blocks that are covered by this ACK. 14646 */ 14647 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14648 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14649 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14650 } 14651 14652 /* 14653 * If we got an ACK after fast retransmit, check to see 14654 * if it is a partial ACK. If it is not and the congestion 14655 * window was inflated to account for the other side's 14656 * cached packets, retract it. If it is, do Hoe's algorithm. 14657 */ 14658 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14659 ASSERT(tcp->tcp_rexmit == B_FALSE); 14660 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14661 tcp->tcp_dupack_cnt = 0; 14662 /* 14663 * Restore the orig tcp_cwnd_ssthresh after 14664 * fast retransmit phase. 14665 */ 14666 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14667 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14668 } 14669 tcp->tcp_rexmit_max = seg_ack; 14670 tcp->tcp_cwnd_cnt = 0; 14671 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14672 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14673 14674 /* 14675 * Remove all notsack info to avoid confusion with 14676 * the next fast retrasnmit/recovery phase. 14677 */ 14678 if (tcp->tcp_snd_sack_ok && 14679 tcp->tcp_notsack_list != NULL) { 14680 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14681 } 14682 } else { 14683 if (tcp->tcp_snd_sack_ok && 14684 tcp->tcp_notsack_list != NULL) { 14685 flags |= TH_NEED_SACK_REXMIT; 14686 tcp->tcp_pipe -= mss; 14687 if (tcp->tcp_pipe < 0) 14688 tcp->tcp_pipe = 0; 14689 } else { 14690 /* 14691 * Hoe's algorithm: 14692 * 14693 * Retransmit the unack'ed segment and 14694 * restart fast recovery. Note that we 14695 * need to scale back tcp_cwnd to the 14696 * original value when we started fast 14697 * recovery. This is to prevent overly 14698 * aggressive behaviour in sending new 14699 * segments. 14700 */ 14701 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14702 tcps->tcps_dupack_fast_retransmit * mss; 14703 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14704 flags |= TH_REXMIT_NEEDED; 14705 } 14706 } 14707 } else { 14708 tcp->tcp_dupack_cnt = 0; 14709 if (tcp->tcp_rexmit) { 14710 /* 14711 * TCP is retranmitting. If the ACK ack's all 14712 * outstanding data, update tcp_rexmit_max and 14713 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14714 * to the correct value. 14715 * 14716 * Note that SEQ_LEQ() is used. This is to avoid 14717 * unnecessary fast retransmit caused by dup ACKs 14718 * received when TCP does slow start retransmission 14719 * after a time out. During this phase, TCP may 14720 * send out segments which are already received. 14721 * This causes dup ACKs to be sent back. 14722 */ 14723 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14724 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14725 tcp->tcp_rexmit_nxt = seg_ack; 14726 } 14727 if (seg_ack != tcp->tcp_rexmit_max) { 14728 flags |= TH_XMIT_NEEDED; 14729 } 14730 } else { 14731 tcp->tcp_rexmit = B_FALSE; 14732 tcp->tcp_xmit_zc_clean = B_FALSE; 14733 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14734 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14735 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14736 } 14737 tcp->tcp_ms_we_have_waited = 0; 14738 } 14739 } 14740 14741 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14742 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14743 tcp->tcp_suna = seg_ack; 14744 if (tcp->tcp_zero_win_probe != 0) { 14745 tcp->tcp_zero_win_probe = 0; 14746 tcp->tcp_timer_backoff = 0; 14747 } 14748 14749 /* 14750 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14751 * Note that it cannot be the SYN being ack'ed. The code flow 14752 * will not reach here. 14753 */ 14754 if (mp1 == NULL) { 14755 goto fin_acked; 14756 } 14757 14758 /* 14759 * Update the congestion window. 14760 * 14761 * If TCP is not ECN capable or TCP is ECN capable but the 14762 * congestion experience bit is not set, increase the tcp_cwnd as 14763 * usual. 14764 */ 14765 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14766 cwnd = tcp->tcp_cwnd; 14767 add = mss; 14768 14769 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14770 /* 14771 * This is to prevent an increase of less than 1 MSS of 14772 * tcp_cwnd. With partial increase, tcp_wput_data() 14773 * may send out tinygrams in order to preserve mblk 14774 * boundaries. 14775 * 14776 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14777 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14778 * increased by 1 MSS for every RTTs. 14779 */ 14780 if (tcp->tcp_cwnd_cnt <= 0) { 14781 tcp->tcp_cwnd_cnt = cwnd + add; 14782 } else { 14783 tcp->tcp_cwnd_cnt -= add; 14784 add = 0; 14785 } 14786 } 14787 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14788 } 14789 14790 /* See if the latest urgent data has been acknowledged */ 14791 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14792 SEQ_GT(seg_ack, tcp->tcp_urg)) 14793 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14794 14795 /* Can we update the RTT estimates? */ 14796 if (tcp->tcp_snd_ts_ok) { 14797 /* Ignore zero timestamp echo-reply. */ 14798 if (tcpopt.tcp_opt_ts_ecr != 0) { 14799 tcp_set_rto(tcp, (int32_t)lbolt - 14800 (int32_t)tcpopt.tcp_opt_ts_ecr); 14801 } 14802 14803 /* If needed, restart the timer. */ 14804 if (tcp->tcp_set_timer == 1) { 14805 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14806 tcp->tcp_set_timer = 0; 14807 } 14808 /* 14809 * Update tcp_csuna in case the other side stops sending 14810 * us timestamps. 14811 */ 14812 tcp->tcp_csuna = tcp->tcp_snxt; 14813 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14814 /* 14815 * An ACK sequence we haven't seen before, so get the RTT 14816 * and update the RTO. But first check if the timestamp is 14817 * valid to use. 14818 */ 14819 if ((mp1->b_next != NULL) && 14820 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14821 tcp_set_rto(tcp, (int32_t)lbolt - 14822 (int32_t)(intptr_t)mp1->b_prev); 14823 else 14824 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14825 14826 /* Remeber the last sequence to be ACKed */ 14827 tcp->tcp_csuna = seg_ack; 14828 if (tcp->tcp_set_timer == 1) { 14829 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14830 tcp->tcp_set_timer = 0; 14831 } 14832 } else { 14833 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14834 } 14835 14836 /* Eat acknowledged bytes off the xmit queue. */ 14837 for (;;) { 14838 mblk_t *mp2; 14839 uchar_t *wptr; 14840 14841 wptr = mp1->b_wptr; 14842 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14843 bytes_acked -= (int)(wptr - mp1->b_rptr); 14844 if (bytes_acked < 0) { 14845 mp1->b_rptr = wptr + bytes_acked; 14846 /* 14847 * Set a new timestamp if all the bytes timed by the 14848 * old timestamp have been ack'ed. 14849 */ 14850 if (SEQ_GT(seg_ack, 14851 (uint32_t)(uintptr_t)(mp1->b_next))) { 14852 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14853 mp1->b_next = NULL; 14854 } 14855 break; 14856 } 14857 mp1->b_next = NULL; 14858 mp1->b_prev = NULL; 14859 mp2 = mp1; 14860 mp1 = mp1->b_cont; 14861 14862 /* 14863 * This notification is required for some zero-copy 14864 * clients to maintain a copy semantic. After the data 14865 * is ack'ed, client is safe to modify or reuse the buffer. 14866 */ 14867 if (tcp->tcp_snd_zcopy_aware && 14868 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14869 tcp_zcopy_notify(tcp); 14870 freeb(mp2); 14871 if (bytes_acked == 0) { 14872 if (mp1 == NULL) { 14873 /* Everything is ack'ed, clear the tail. */ 14874 tcp->tcp_xmit_tail = NULL; 14875 /* 14876 * Cancel the timer unless we are still 14877 * waiting for an ACK for the FIN packet. 14878 */ 14879 if (tcp->tcp_timer_tid != 0 && 14880 tcp->tcp_snxt == tcp->tcp_suna) { 14881 (void) TCP_TIMER_CANCEL(tcp, 14882 tcp->tcp_timer_tid); 14883 tcp->tcp_timer_tid = 0; 14884 } 14885 goto pre_swnd_update; 14886 } 14887 if (mp2 != tcp->tcp_xmit_tail) 14888 break; 14889 tcp->tcp_xmit_tail = mp1; 14890 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14891 (uintptr_t)INT_MAX); 14892 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14893 mp1->b_rptr); 14894 break; 14895 } 14896 if (mp1 == NULL) { 14897 /* 14898 * More was acked but there is nothing more 14899 * outstanding. This means that the FIN was 14900 * just acked or that we're talking to a clown. 14901 */ 14902 fin_acked: 14903 ASSERT(tcp->tcp_fin_sent); 14904 tcp->tcp_xmit_tail = NULL; 14905 if (tcp->tcp_fin_sent) { 14906 /* FIN was acked - making progress */ 14907 if (tcp->tcp_ipversion == IPV6_VERSION && 14908 !tcp->tcp_fin_acked) 14909 tcp->tcp_ip_forward_progress = B_TRUE; 14910 tcp->tcp_fin_acked = B_TRUE; 14911 if (tcp->tcp_linger_tid != 0 && 14912 TCP_TIMER_CANCEL(tcp, 14913 tcp->tcp_linger_tid) >= 0) { 14914 tcp_stop_lingering(tcp); 14915 freemsg(mp); 14916 mp = NULL; 14917 } 14918 } else { 14919 /* 14920 * We should never get here because 14921 * we have already checked that the 14922 * number of bytes ack'ed should be 14923 * smaller than or equal to what we 14924 * have sent so far (it is the 14925 * acceptability check of the ACK). 14926 * We can only get here if the send 14927 * queue is corrupted. 14928 * 14929 * Terminate the connection and 14930 * panic the system. It is better 14931 * for us to panic instead of 14932 * continuing to avoid other disaster. 14933 */ 14934 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14935 tcp->tcp_rnxt, TH_RST|TH_ACK); 14936 panic("Memory corruption " 14937 "detected for connection %s.", 14938 tcp_display(tcp, NULL, 14939 DISP_ADDR_AND_PORT)); 14940 /*NOTREACHED*/ 14941 } 14942 goto pre_swnd_update; 14943 } 14944 ASSERT(mp2 != tcp->tcp_xmit_tail); 14945 } 14946 if (tcp->tcp_unsent) { 14947 flags |= TH_XMIT_NEEDED; 14948 } 14949 pre_swnd_update: 14950 tcp->tcp_xmit_head = mp1; 14951 swnd_update: 14952 /* 14953 * The following check is different from most other implementations. 14954 * For bi-directional transfer, when segments are dropped, the 14955 * "normal" check will not accept a window update in those 14956 * retransmitted segemnts. Failing to do that, TCP may send out 14957 * segments which are outside receiver's window. As TCP accepts 14958 * the ack in those retransmitted segments, if the window update in 14959 * the same segment is not accepted, TCP will incorrectly calculates 14960 * that it can send more segments. This can create a deadlock 14961 * with the receiver if its window becomes zero. 14962 */ 14963 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14964 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14965 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14966 /* 14967 * The criteria for update is: 14968 * 14969 * 1. the segment acknowledges some data. Or 14970 * 2. the segment is new, i.e. it has a higher seq num. Or 14971 * 3. the segment is not old and the advertised window is 14972 * larger than the previous advertised window. 14973 */ 14974 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14975 flags |= TH_XMIT_NEEDED; 14976 tcp->tcp_swnd = new_swnd; 14977 if (new_swnd > tcp->tcp_max_swnd) 14978 tcp->tcp_max_swnd = new_swnd; 14979 tcp->tcp_swl1 = seg_seq; 14980 tcp->tcp_swl2 = seg_ack; 14981 } 14982 est: 14983 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14984 14985 switch (tcp->tcp_state) { 14986 case TCPS_FIN_WAIT_1: 14987 if (tcp->tcp_fin_acked) { 14988 tcp->tcp_state = TCPS_FIN_WAIT_2; 14989 /* 14990 * We implement the non-standard BSD/SunOS 14991 * FIN_WAIT_2 flushing algorithm. 14992 * If there is no user attached to this 14993 * TCP endpoint, then this TCP struct 14994 * could hang around forever in FIN_WAIT_2 14995 * state if the peer forgets to send us 14996 * a FIN. To prevent this, we wait only 14997 * 2*MSL (a convenient time value) for 14998 * the FIN to arrive. If it doesn't show up, 14999 * we flush the TCP endpoint. This algorithm, 15000 * though a violation of RFC-793, has worked 15001 * for over 10 years in BSD systems. 15002 * Note: SunOS 4.x waits 675 seconds before 15003 * flushing the FIN_WAIT_2 connection. 15004 */ 15005 TCP_TIMER_RESTART(tcp, 15006 tcps->tcps_fin_wait_2_flush_interval); 15007 } 15008 break; 15009 case TCPS_FIN_WAIT_2: 15010 break; /* Shutdown hook? */ 15011 case TCPS_LAST_ACK: 15012 freemsg(mp); 15013 if (tcp->tcp_fin_acked) { 15014 (void) tcp_clean_death(tcp, 0, 19); 15015 return; 15016 } 15017 goto xmit_check; 15018 case TCPS_CLOSING: 15019 if (tcp->tcp_fin_acked) { 15020 tcp->tcp_state = TCPS_TIME_WAIT; 15021 /* 15022 * Unconditionally clear the exclusive binding 15023 * bit so this TIME-WAIT connection won't 15024 * interfere with new ones. 15025 */ 15026 tcp->tcp_exclbind = 0; 15027 if (!TCP_IS_DETACHED(tcp)) { 15028 TCP_TIMER_RESTART(tcp, 15029 tcps->tcps_time_wait_interval); 15030 } else { 15031 tcp_time_wait_append(tcp); 15032 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15033 } 15034 } 15035 /*FALLTHRU*/ 15036 case TCPS_CLOSE_WAIT: 15037 freemsg(mp); 15038 goto xmit_check; 15039 default: 15040 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 15041 break; 15042 } 15043 } 15044 if (flags & TH_FIN) { 15045 /* Make sure we ack the fin */ 15046 flags |= TH_ACK_NEEDED; 15047 if (!tcp->tcp_fin_rcvd) { 15048 tcp->tcp_fin_rcvd = B_TRUE; 15049 tcp->tcp_rnxt++; 15050 tcph = tcp->tcp_tcph; 15051 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15052 15053 /* 15054 * Generate the ordrel_ind at the end unless we 15055 * are an eager guy. 15056 * In the eager case tcp_rsrv will do this when run 15057 * after tcp_accept is done. 15058 */ 15059 if (tcp->tcp_listener == NULL && 15060 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 15061 flags |= TH_ORDREL_NEEDED; 15062 switch (tcp->tcp_state) { 15063 case TCPS_SYN_RCVD: 15064 case TCPS_ESTABLISHED: 15065 tcp->tcp_state = TCPS_CLOSE_WAIT; 15066 /* Keepalive? */ 15067 break; 15068 case TCPS_FIN_WAIT_1: 15069 if (!tcp->tcp_fin_acked) { 15070 tcp->tcp_state = TCPS_CLOSING; 15071 break; 15072 } 15073 /* FALLTHRU */ 15074 case TCPS_FIN_WAIT_2: 15075 tcp->tcp_state = TCPS_TIME_WAIT; 15076 /* 15077 * Unconditionally clear the exclusive binding 15078 * bit so this TIME-WAIT connection won't 15079 * interfere with new ones. 15080 */ 15081 tcp->tcp_exclbind = 0; 15082 if (!TCP_IS_DETACHED(tcp)) { 15083 TCP_TIMER_RESTART(tcp, 15084 tcps->tcps_time_wait_interval); 15085 } else { 15086 tcp_time_wait_append(tcp); 15087 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15088 } 15089 if (seg_len) { 15090 /* 15091 * implies data piggybacked on FIN. 15092 * break to handle data. 15093 */ 15094 break; 15095 } 15096 freemsg(mp); 15097 goto ack_check; 15098 } 15099 } 15100 } 15101 if (mp == NULL) 15102 goto xmit_check; 15103 if (seg_len == 0) { 15104 freemsg(mp); 15105 goto xmit_check; 15106 } 15107 if (mp->b_rptr == mp->b_wptr) { 15108 /* 15109 * The header has been consumed, so we remove the 15110 * zero-length mblk here. 15111 */ 15112 mp1 = mp; 15113 mp = mp->b_cont; 15114 freeb(mp1); 15115 } 15116 tcph = tcp->tcp_tcph; 15117 tcp->tcp_rack_cnt++; 15118 { 15119 uint32_t cur_max; 15120 15121 cur_max = tcp->tcp_rack_cur_max; 15122 if (tcp->tcp_rack_cnt >= cur_max) { 15123 /* 15124 * We have more unacked data than we should - send 15125 * an ACK now. 15126 */ 15127 flags |= TH_ACK_NEEDED; 15128 cur_max++; 15129 if (cur_max > tcp->tcp_rack_abs_max) 15130 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15131 else 15132 tcp->tcp_rack_cur_max = cur_max; 15133 } else if (TCP_IS_DETACHED(tcp)) { 15134 /* We don't have an ACK timer for detached TCP. */ 15135 flags |= TH_ACK_NEEDED; 15136 } else if (seg_len < mss) { 15137 /* 15138 * If we get a segment that is less than an mss, and we 15139 * already have unacknowledged data, and the amount 15140 * unacknowledged is not a multiple of mss, then we 15141 * better generate an ACK now. Otherwise, this may be 15142 * the tail piece of a transaction, and we would rather 15143 * wait for the response. 15144 */ 15145 uint32_t udif; 15146 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15147 (uintptr_t)INT_MAX); 15148 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15149 if (udif && (udif % mss)) 15150 flags |= TH_ACK_NEEDED; 15151 else 15152 flags |= TH_ACK_TIMER_NEEDED; 15153 } else { 15154 /* Start delayed ack timer */ 15155 flags |= TH_ACK_TIMER_NEEDED; 15156 } 15157 } 15158 tcp->tcp_rnxt += seg_len; 15159 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15160 15161 /* Update SACK list */ 15162 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15163 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15164 &(tcp->tcp_num_sack_blk)); 15165 } 15166 15167 if (tcp->tcp_urp_mp) { 15168 tcp->tcp_urp_mp->b_cont = mp; 15169 mp = tcp->tcp_urp_mp; 15170 tcp->tcp_urp_mp = NULL; 15171 /* Ready for a new signal. */ 15172 tcp->tcp_urp_last_valid = B_FALSE; 15173 #ifdef DEBUG 15174 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15175 "tcp_rput: sending exdata_ind %s", 15176 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15177 #endif /* DEBUG */ 15178 } 15179 15180 /* 15181 * Check for ancillary data changes compared to last segment. 15182 */ 15183 if (tcp->tcp_ipv6_recvancillary != 0) { 15184 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15185 ASSERT(mp != NULL); 15186 } 15187 15188 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15189 /* 15190 * Side queue inbound data until the accept happens. 15191 * tcp_accept/tcp_rput drains this when the accept happens. 15192 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15193 * T_EXDATA_IND) it is queued on b_next. 15194 * XXX Make urgent data use this. Requires: 15195 * Removing tcp_listener check for TH_URG 15196 * Making M_PCPROTO and MARK messages skip the eager case 15197 */ 15198 15199 if (tcp->tcp_kssl_pending) { 15200 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15201 mblk_t *, mp); 15202 tcp_kssl_input(tcp, mp); 15203 } else { 15204 tcp_rcv_enqueue(tcp, mp, seg_len); 15205 } 15206 } else { 15207 sodirect_t *sodp = tcp->tcp_sodirect; 15208 15209 /* 15210 * If an sodirect connection and an enabled sodirect_t then 15211 * sodp will be set to point to the tcp_t/sonode_t shared 15212 * sodirect_t and the sodirect_t's lock will be held. 15213 */ 15214 if (sodp != NULL) { 15215 mutex_enter(sodp->sod_lockp); 15216 if (!(sodp->sod_state & SOD_ENABLED) || 15217 (tcp->tcp_kssl_ctx != NULL && 15218 DB_TYPE(mp) == M_DATA)) { 15219 mutex_exit(sodp->sod_lockp); 15220 sodp = NULL; 15221 } 15222 } 15223 if (mp->b_datap->db_type != M_DATA || 15224 (flags & TH_MARKNEXT_NEEDED)) { 15225 if (sodp != NULL) { 15226 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15227 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15228 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15229 } 15230 if (!SOD_QEMPTY(sodp) && 15231 (sodp->sod_state & SOD_WAKE_NOT)) { 15232 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15233 /* sod_wakeup() did the mutex_exit() */ 15234 } else { 15235 mutex_exit(sodp->sod_lockp); 15236 } 15237 } else if (tcp->tcp_rcv_list != NULL) { 15238 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15239 } 15240 ASSERT(tcp->tcp_rcv_list == NULL || 15241 tcp->tcp_fused_sigurg); 15242 15243 if (flags & TH_MARKNEXT_NEEDED) { 15244 #ifdef DEBUG 15245 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15246 "tcp_rput: sending MSGMARKNEXT %s", 15247 tcp_display(tcp, NULL, 15248 DISP_PORT_ONLY)); 15249 #endif /* DEBUG */ 15250 mp->b_flag |= MSGMARKNEXT; 15251 flags &= ~TH_MARKNEXT_NEEDED; 15252 } 15253 15254 /* Does this need SSL processing first? */ 15255 if ((tcp->tcp_kssl_ctx != NULL) && 15256 (DB_TYPE(mp) == M_DATA)) { 15257 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15258 mblk_t *, mp); 15259 tcp_kssl_input(tcp, mp); 15260 } else { 15261 putnext(tcp->tcp_rq, mp); 15262 if (!canputnext(tcp->tcp_rq)) 15263 tcp->tcp_rwnd -= seg_len; 15264 } 15265 } else if ((tcp->tcp_kssl_ctx != NULL) && 15266 (DB_TYPE(mp) == M_DATA)) { 15267 /* Do SSL processing first */ 15268 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, 15269 mblk_t *, mp); 15270 tcp_kssl_input(tcp, mp); 15271 } else if (sodp != NULL) { 15272 /* 15273 * Sodirect so all mblk_t's are queued on the 15274 * socket directly, check for wakeup of blocked 15275 * reader (if any), and last if flow-controled. 15276 */ 15277 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15278 if ((sodp->sod_state & SOD_WAKE_NEED) || 15279 (flags & (TH_PUSH|TH_FIN))) { 15280 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15281 /* sod_wakeup() did the mutex_exit() */ 15282 } else { 15283 if (SOD_QFULL(sodp)) { 15284 /* Q is full, need backenable */ 15285 SOD_QSETBE(sodp); 15286 } 15287 mutex_exit(sodp->sod_lockp); 15288 } 15289 } else if ((flags & (TH_PUSH|TH_FIN)) || 15290 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 15291 if (tcp->tcp_rcv_list != NULL) { 15292 /* 15293 * Enqueue the new segment first and then 15294 * call tcp_rcv_drain() to send all data 15295 * up. The other way to do this is to 15296 * send all queued data up and then call 15297 * putnext() to send the new segment up. 15298 * This way can remove the else part later 15299 * on. 15300 * 15301 * We don't this to avoid one more call to 15302 * canputnext() as tcp_rcv_drain() needs to 15303 * call canputnext(). 15304 */ 15305 tcp_rcv_enqueue(tcp, mp, seg_len); 15306 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15307 } else { 15308 putnext(tcp->tcp_rq, mp); 15309 if (!canputnext(tcp->tcp_rq)) 15310 tcp->tcp_rwnd -= seg_len; 15311 } 15312 } else { 15313 /* 15314 * Enqueue all packets when processing an mblk 15315 * from the co queue and also enqueue normal packets. 15316 */ 15317 tcp_rcv_enqueue(tcp, mp, seg_len); 15318 } 15319 /* 15320 * Make sure the timer is running if we have data waiting 15321 * for a push bit. This provides resiliency against 15322 * implementations that do not correctly generate push bits. 15323 * 15324 * Note, for sodirect if Q isn't empty and there's not a 15325 * pending wakeup then we need a timer. Also note that sodp 15326 * is assumed to be still valid after exit()ing the sod_lockp 15327 * above and while the SOD state can change it can only change 15328 * such that the Q is empty now even though data was added 15329 * above. 15330 */ 15331 if (((sodp != NULL && !SOD_QEMPTY(sodp) && 15332 (sodp->sod_state & SOD_WAKE_NOT)) || 15333 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15334 tcp->tcp_push_tid == 0) { 15335 /* 15336 * The connection may be closed at this point, so don't 15337 * do anything for a detached tcp. 15338 */ 15339 if (!TCP_IS_DETACHED(tcp)) 15340 tcp->tcp_push_tid = TCP_TIMER(tcp, 15341 tcp_push_timer, 15342 MSEC_TO_TICK( 15343 tcps->tcps_push_timer_interval)); 15344 } 15345 } 15346 15347 xmit_check: 15348 /* Is there anything left to do? */ 15349 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15350 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15351 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15352 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15353 goto done; 15354 15355 /* Any transmit work to do and a non-zero window? */ 15356 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15357 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15358 if (flags & TH_REXMIT_NEEDED) { 15359 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15360 15361 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15362 if (snd_size > mss) 15363 snd_size = mss; 15364 if (snd_size > tcp->tcp_swnd) 15365 snd_size = tcp->tcp_swnd; 15366 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15367 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15368 B_TRUE); 15369 15370 if (mp1 != NULL) { 15371 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15372 tcp->tcp_csuna = tcp->tcp_snxt; 15373 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15374 UPDATE_MIB(&tcps->tcps_mib, 15375 tcpRetransBytes, snd_size); 15376 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15377 } 15378 } 15379 if (flags & TH_NEED_SACK_REXMIT) { 15380 tcp_sack_rxmit(tcp, &flags); 15381 } 15382 /* 15383 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15384 * out new segment. Note that tcp_rexmit should not be 15385 * set, otherwise TH_LIMIT_XMIT should not be set. 15386 */ 15387 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15388 if (!tcp->tcp_rexmit) { 15389 tcp_wput_data(tcp, NULL, B_FALSE); 15390 } else { 15391 tcp_ss_rexmit(tcp); 15392 } 15393 } 15394 /* 15395 * Adjust tcp_cwnd back to normal value after sending 15396 * new data segments. 15397 */ 15398 if (flags & TH_LIMIT_XMIT) { 15399 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15400 /* 15401 * This will restart the timer. Restarting the 15402 * timer is used to avoid a timeout before the 15403 * limited transmitted segment's ACK gets back. 15404 */ 15405 if (tcp->tcp_xmit_head != NULL) 15406 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15407 } 15408 15409 /* Anything more to do? */ 15410 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15411 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15412 goto done; 15413 } 15414 ack_check: 15415 if (flags & TH_SEND_URP_MARK) { 15416 ASSERT(tcp->tcp_urp_mark_mp); 15417 /* 15418 * Send up any queued data and then send the mark message 15419 */ 15420 sodirect_t *sodp; 15421 15422 SOD_PTR_ENTER(tcp, sodp); 15423 15424 mp1 = tcp->tcp_urp_mark_mp; 15425 tcp->tcp_urp_mark_mp = NULL; 15426 if (sodp != NULL) { 15427 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15428 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15429 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15430 } 15431 ASSERT(tcp->tcp_rcv_list == NULL); 15432 15433 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15434 /* sod_wakeup() does the mutex_exit() */ 15435 } else if (tcp->tcp_rcv_list != NULL) { 15436 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15437 15438 ASSERT(tcp->tcp_rcv_list == NULL || 15439 tcp->tcp_fused_sigurg); 15440 15441 } 15442 putnext(tcp->tcp_rq, mp1); 15443 #ifdef DEBUG 15444 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15445 "tcp_rput: sending zero-length %s %s", 15446 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15447 "MSGNOTMARKNEXT"), 15448 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15449 #endif /* DEBUG */ 15450 flags &= ~TH_SEND_URP_MARK; 15451 } 15452 if (flags & TH_ACK_NEEDED) { 15453 /* 15454 * Time to send an ack for some reason. 15455 */ 15456 mp1 = tcp_ack_mp(tcp); 15457 15458 if (mp1 != NULL) { 15459 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15460 BUMP_LOCAL(tcp->tcp_obsegs); 15461 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15462 } 15463 if (tcp->tcp_ack_tid != 0) { 15464 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15465 tcp->tcp_ack_tid = 0; 15466 } 15467 } 15468 if (flags & TH_ACK_TIMER_NEEDED) { 15469 /* 15470 * Arrange for deferred ACK or push wait timeout. 15471 * Start timer if it is not already running. 15472 */ 15473 if (tcp->tcp_ack_tid == 0) { 15474 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15475 MSEC_TO_TICK(tcp->tcp_localnet ? 15476 (clock_t)tcps->tcps_local_dack_interval : 15477 (clock_t)tcps->tcps_deferred_ack_interval)); 15478 } 15479 } 15480 if (flags & TH_ORDREL_NEEDED) { 15481 /* 15482 * Send up the ordrel_ind unless we are an eager guy. 15483 * In the eager case tcp_rsrv will do this when run 15484 * after tcp_accept is done. 15485 */ 15486 sodirect_t *sodp; 15487 15488 ASSERT(tcp->tcp_listener == NULL); 15489 15490 SOD_PTR_ENTER(tcp, sodp); 15491 if (sodp != NULL) { 15492 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15493 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15494 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15495 } 15496 /* No more sodirect */ 15497 tcp->tcp_sodirect = NULL; 15498 if (!SOD_QEMPTY(sodp)) { 15499 /* Mblk(s) to process, notify */ 15500 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15501 /* sod_wakeup() does the mutex_exit() */ 15502 } else { 15503 /* Nothing to process */ 15504 mutex_exit(sodp->sod_lockp); 15505 } 15506 } else if (tcp->tcp_rcv_list != NULL) { 15507 /* 15508 * Push any mblk(s) enqueued from co processing. 15509 */ 15510 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15511 15512 ASSERT(tcp->tcp_rcv_list == NULL || 15513 tcp->tcp_fused_sigurg); 15514 } 15515 15516 mp1 = tcp->tcp_ordrel_mp; 15517 tcp->tcp_ordrel_mp = NULL; 15518 tcp->tcp_ordrel_done = B_TRUE; 15519 putnext(tcp->tcp_rq, mp1); 15520 } 15521 done: 15522 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15523 } 15524 15525 /* 15526 * This function does PAWS protection check. Returns B_TRUE if the 15527 * segment passes the PAWS test, else returns B_FALSE. 15528 */ 15529 boolean_t 15530 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15531 { 15532 uint8_t flags; 15533 int options; 15534 uint8_t *up; 15535 15536 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15537 /* 15538 * If timestamp option is aligned nicely, get values inline, 15539 * otherwise call general routine to parse. Only do that 15540 * if timestamp is the only option. 15541 */ 15542 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15543 TCPOPT_REAL_TS_LEN && 15544 OK_32PTR((up = ((uint8_t *)tcph) + 15545 TCP_MIN_HEADER_LENGTH)) && 15546 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15547 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15548 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15549 15550 options = TCP_OPT_TSTAMP_PRESENT; 15551 } else { 15552 if (tcp->tcp_snd_sack_ok) { 15553 tcpoptp->tcp = tcp; 15554 } else { 15555 tcpoptp->tcp = NULL; 15556 } 15557 options = tcp_parse_options(tcph, tcpoptp); 15558 } 15559 15560 if (options & TCP_OPT_TSTAMP_PRESENT) { 15561 /* 15562 * Do PAWS per RFC 1323 section 4.2. Accept RST 15563 * regardless of the timestamp, page 18 RFC 1323.bis. 15564 */ 15565 if ((flags & TH_RST) == 0 && 15566 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15567 tcp->tcp_ts_recent)) { 15568 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15569 PAWS_TIMEOUT)) { 15570 /* This segment is not acceptable. */ 15571 return (B_FALSE); 15572 } else { 15573 /* 15574 * Connection has been idle for 15575 * too long. Reset the timestamp 15576 * and assume the segment is valid. 15577 */ 15578 tcp->tcp_ts_recent = 15579 tcpoptp->tcp_opt_ts_val; 15580 } 15581 } 15582 } else { 15583 /* 15584 * If we don't get a timestamp on every packet, we 15585 * figure we can't really trust 'em, so we stop sending 15586 * and parsing them. 15587 */ 15588 tcp->tcp_snd_ts_ok = B_FALSE; 15589 15590 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15591 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15592 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15593 /* 15594 * Adjust the tcp_mss accordingly. We also need to 15595 * adjust tcp_cwnd here in accordance with the new mss. 15596 * But we avoid doing a slow start here so as to not 15597 * to lose on the transfer rate built up so far. 15598 */ 15599 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15600 if (tcp->tcp_snd_sack_ok) { 15601 ASSERT(tcp->tcp_sack_info != NULL); 15602 tcp->tcp_max_sack_blk = 4; 15603 } 15604 } 15605 return (B_TRUE); 15606 } 15607 15608 /* 15609 * Attach ancillary data to a received TCP segments for the 15610 * ancillary pieces requested by the application that are 15611 * different than they were in the previous data segment. 15612 * 15613 * Save the "current" values once memory allocation is ok so that 15614 * when memory allocation fails we can just wait for the next data segment. 15615 */ 15616 static mblk_t * 15617 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15618 { 15619 struct T_optdata_ind *todi; 15620 int optlen; 15621 uchar_t *optptr; 15622 struct T_opthdr *toh; 15623 uint_t addflag; /* Which pieces to add */ 15624 mblk_t *mp1; 15625 15626 optlen = 0; 15627 addflag = 0; 15628 /* If app asked for pktinfo and the index has changed ... */ 15629 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15630 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15631 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15632 optlen += sizeof (struct T_opthdr) + 15633 sizeof (struct in6_pktinfo); 15634 addflag |= TCP_IPV6_RECVPKTINFO; 15635 } 15636 /* If app asked for hoplimit and it has changed ... */ 15637 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15638 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15639 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15640 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15641 addflag |= TCP_IPV6_RECVHOPLIMIT; 15642 } 15643 /* If app asked for tclass and it has changed ... */ 15644 if ((ipp->ipp_fields & IPPF_TCLASS) && 15645 ipp->ipp_tclass != tcp->tcp_recvtclass && 15646 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15647 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15648 addflag |= TCP_IPV6_RECVTCLASS; 15649 } 15650 /* 15651 * If app asked for hopbyhop headers and it has changed ... 15652 * For security labels, note that (1) security labels can't change on 15653 * a connected socket at all, (2) we're connected to at most one peer, 15654 * (3) if anything changes, then it must be some other extra option. 15655 */ 15656 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15657 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15658 (ipp->ipp_fields & IPPF_HOPOPTS), 15659 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15660 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15661 tcp->tcp_label_len; 15662 addflag |= TCP_IPV6_RECVHOPOPTS; 15663 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15664 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15665 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15666 return (mp); 15667 } 15668 /* If app asked for dst headers before routing headers ... */ 15669 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15670 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15671 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15672 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15673 optlen += sizeof (struct T_opthdr) + 15674 ipp->ipp_rtdstoptslen; 15675 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15676 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15677 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15678 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15679 return (mp); 15680 } 15681 /* If app asked for routing headers and it has changed ... */ 15682 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15683 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15684 (ipp->ipp_fields & IPPF_RTHDR), 15685 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15686 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15687 addflag |= TCP_IPV6_RECVRTHDR; 15688 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15689 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15690 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15691 return (mp); 15692 } 15693 /* If app asked for dest headers and it has changed ... */ 15694 if ((tcp->tcp_ipv6_recvancillary & 15695 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15696 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15697 (ipp->ipp_fields & IPPF_DSTOPTS), 15698 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15699 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15700 addflag |= TCP_IPV6_RECVDSTOPTS; 15701 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15702 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15703 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15704 return (mp); 15705 } 15706 15707 if (optlen == 0) { 15708 /* Nothing to add */ 15709 return (mp); 15710 } 15711 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15712 if (mp1 == NULL) { 15713 /* 15714 * Defer sending ancillary data until the next TCP segment 15715 * arrives. 15716 */ 15717 return (mp); 15718 } 15719 mp1->b_cont = mp; 15720 mp = mp1; 15721 mp->b_wptr += sizeof (*todi) + optlen; 15722 mp->b_datap->db_type = M_PROTO; 15723 todi = (struct T_optdata_ind *)mp->b_rptr; 15724 todi->PRIM_type = T_OPTDATA_IND; 15725 todi->DATA_flag = 1; /* MORE data */ 15726 todi->OPT_length = optlen; 15727 todi->OPT_offset = sizeof (*todi); 15728 optptr = (uchar_t *)&todi[1]; 15729 /* 15730 * If app asked for pktinfo and the index has changed ... 15731 * Note that the local address never changes for the connection. 15732 */ 15733 if (addflag & TCP_IPV6_RECVPKTINFO) { 15734 struct in6_pktinfo *pkti; 15735 15736 toh = (struct T_opthdr *)optptr; 15737 toh->level = IPPROTO_IPV6; 15738 toh->name = IPV6_PKTINFO; 15739 toh->len = sizeof (*toh) + sizeof (*pkti); 15740 toh->status = 0; 15741 optptr += sizeof (*toh); 15742 pkti = (struct in6_pktinfo *)optptr; 15743 if (tcp->tcp_ipversion == IPV6_VERSION) 15744 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15745 else 15746 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15747 &pkti->ipi6_addr); 15748 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15749 optptr += sizeof (*pkti); 15750 ASSERT(OK_32PTR(optptr)); 15751 /* Save as "last" value */ 15752 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15753 } 15754 /* If app asked for hoplimit and it has changed ... */ 15755 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15756 toh = (struct T_opthdr *)optptr; 15757 toh->level = IPPROTO_IPV6; 15758 toh->name = IPV6_HOPLIMIT; 15759 toh->len = sizeof (*toh) + sizeof (uint_t); 15760 toh->status = 0; 15761 optptr += sizeof (*toh); 15762 *(uint_t *)optptr = ipp->ipp_hoplimit; 15763 optptr += sizeof (uint_t); 15764 ASSERT(OK_32PTR(optptr)); 15765 /* Save as "last" value */ 15766 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15767 } 15768 /* If app asked for tclass and it has changed ... */ 15769 if (addflag & TCP_IPV6_RECVTCLASS) { 15770 toh = (struct T_opthdr *)optptr; 15771 toh->level = IPPROTO_IPV6; 15772 toh->name = IPV6_TCLASS; 15773 toh->len = sizeof (*toh) + sizeof (uint_t); 15774 toh->status = 0; 15775 optptr += sizeof (*toh); 15776 *(uint_t *)optptr = ipp->ipp_tclass; 15777 optptr += sizeof (uint_t); 15778 ASSERT(OK_32PTR(optptr)); 15779 /* Save as "last" value */ 15780 tcp->tcp_recvtclass = ipp->ipp_tclass; 15781 } 15782 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15783 toh = (struct T_opthdr *)optptr; 15784 toh->level = IPPROTO_IPV6; 15785 toh->name = IPV6_HOPOPTS; 15786 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15787 tcp->tcp_label_len; 15788 toh->status = 0; 15789 optptr += sizeof (*toh); 15790 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15791 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15792 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15793 ASSERT(OK_32PTR(optptr)); 15794 /* Save as last value */ 15795 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15796 (ipp->ipp_fields & IPPF_HOPOPTS), 15797 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15798 } 15799 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15800 toh = (struct T_opthdr *)optptr; 15801 toh->level = IPPROTO_IPV6; 15802 toh->name = IPV6_RTHDRDSTOPTS; 15803 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15804 toh->status = 0; 15805 optptr += sizeof (*toh); 15806 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15807 optptr += ipp->ipp_rtdstoptslen; 15808 ASSERT(OK_32PTR(optptr)); 15809 /* Save as last value */ 15810 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15811 &tcp->tcp_rtdstoptslen, 15812 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15813 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15814 } 15815 if (addflag & TCP_IPV6_RECVRTHDR) { 15816 toh = (struct T_opthdr *)optptr; 15817 toh->level = IPPROTO_IPV6; 15818 toh->name = IPV6_RTHDR; 15819 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15820 toh->status = 0; 15821 optptr += sizeof (*toh); 15822 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15823 optptr += ipp->ipp_rthdrlen; 15824 ASSERT(OK_32PTR(optptr)); 15825 /* Save as last value */ 15826 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15827 (ipp->ipp_fields & IPPF_RTHDR), 15828 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15829 } 15830 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15831 toh = (struct T_opthdr *)optptr; 15832 toh->level = IPPROTO_IPV6; 15833 toh->name = IPV6_DSTOPTS; 15834 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15835 toh->status = 0; 15836 optptr += sizeof (*toh); 15837 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15838 optptr += ipp->ipp_dstoptslen; 15839 ASSERT(OK_32PTR(optptr)); 15840 /* Save as last value */ 15841 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15842 (ipp->ipp_fields & IPPF_DSTOPTS), 15843 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15844 } 15845 ASSERT(optptr == mp->b_wptr); 15846 return (mp); 15847 } 15848 15849 15850 /* 15851 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15852 * or a "bad" IRE detected by tcp_adapt_ire. 15853 * We can't tell if the failure was due to the laddr or the faddr 15854 * thus we clear out all addresses and ports. 15855 */ 15856 static void 15857 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15858 { 15859 queue_t *q = tcp->tcp_rq; 15860 tcph_t *tcph; 15861 struct T_error_ack *tea; 15862 conn_t *connp = tcp->tcp_connp; 15863 15864 15865 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15866 15867 if (mp->b_cont) { 15868 freemsg(mp->b_cont); 15869 mp->b_cont = NULL; 15870 } 15871 tea = (struct T_error_ack *)mp->b_rptr; 15872 switch (tea->PRIM_type) { 15873 case T_BIND_ACK: 15874 /* 15875 * Need to unbind with classifier since we were just told that 15876 * our bind succeeded. 15877 */ 15878 tcp->tcp_hard_bound = B_FALSE; 15879 tcp->tcp_hard_binding = B_FALSE; 15880 15881 ipcl_hash_remove(connp); 15882 /* Reuse the mblk if possible */ 15883 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15884 sizeof (*tea)); 15885 mp->b_rptr = mp->b_datap->db_base; 15886 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15887 tea = (struct T_error_ack *)mp->b_rptr; 15888 tea->PRIM_type = T_ERROR_ACK; 15889 tea->TLI_error = TSYSERR; 15890 tea->UNIX_error = error; 15891 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15892 tea->ERROR_prim = T_CONN_REQ; 15893 } else { 15894 tea->ERROR_prim = O_T_BIND_REQ; 15895 } 15896 break; 15897 15898 case T_ERROR_ACK: 15899 if (tcp->tcp_state >= TCPS_SYN_SENT) 15900 tea->ERROR_prim = T_CONN_REQ; 15901 break; 15902 default: 15903 panic("tcp_bind_failed: unexpected TPI type"); 15904 /*NOTREACHED*/ 15905 } 15906 15907 tcp->tcp_state = TCPS_IDLE; 15908 if (tcp->tcp_ipversion == IPV4_VERSION) 15909 tcp->tcp_ipha->ipha_src = 0; 15910 else 15911 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15912 /* 15913 * Copy of the src addr. in tcp_t is needed since 15914 * the lookup funcs. can only look at tcp_t 15915 */ 15916 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15917 15918 tcph = tcp->tcp_tcph; 15919 tcph->th_lport[0] = 0; 15920 tcph->th_lport[1] = 0; 15921 tcp_bind_hash_remove(tcp); 15922 bzero(&connp->u_port, sizeof (connp->u_port)); 15923 /* blow away saved option results if any */ 15924 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15925 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15926 15927 conn_delete_ire(tcp->tcp_connp, NULL); 15928 putnext(q, mp); 15929 } 15930 15931 /* 15932 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15933 * messages. 15934 */ 15935 void 15936 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15937 { 15938 mblk_t *mp1; 15939 uchar_t *rptr = mp->b_rptr; 15940 queue_t *q = tcp->tcp_rq; 15941 struct T_error_ack *tea; 15942 uint32_t mss; 15943 mblk_t *syn_mp; 15944 mblk_t *mdti; 15945 mblk_t *lsoi; 15946 int retval; 15947 mblk_t *ire_mp; 15948 tcp_stack_t *tcps = tcp->tcp_tcps; 15949 15950 switch (mp->b_datap->db_type) { 15951 case M_PROTO: 15952 case M_PCPROTO: 15953 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15954 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15955 break; 15956 tea = (struct T_error_ack *)rptr; 15957 switch (tea->PRIM_type) { 15958 case T_BIND_ACK: 15959 /* 15960 * Adapt Multidata information, if any. The 15961 * following tcp_mdt_update routine will free 15962 * the message. 15963 */ 15964 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15965 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15966 b_rptr)->mdt_capab, B_TRUE); 15967 freemsg(mdti); 15968 } 15969 15970 /* 15971 * Check to update LSO information with tcp, and 15972 * tcp_lso_update routine will free the message. 15973 */ 15974 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15975 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15976 b_rptr)->lso_capab); 15977 freemsg(lsoi); 15978 } 15979 15980 /* Get the IRE, if we had requested for it */ 15981 ire_mp = tcp_ire_mp(mp); 15982 15983 if (tcp->tcp_hard_binding) { 15984 tcp->tcp_hard_binding = B_FALSE; 15985 tcp->tcp_hard_bound = B_TRUE; 15986 CL_INET_CONNECT(tcp); 15987 } else { 15988 if (ire_mp != NULL) 15989 freeb(ire_mp); 15990 goto after_syn_sent; 15991 } 15992 15993 retval = tcp_adapt_ire(tcp, ire_mp); 15994 if (ire_mp != NULL) 15995 freeb(ire_mp); 15996 if (retval == 0) { 15997 tcp_bind_failed(tcp, mp, 15998 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15999 ENETUNREACH : EADDRNOTAVAIL)); 16000 return; 16001 } 16002 /* 16003 * Don't let an endpoint connect to itself. 16004 * Also checked in tcp_connect() but that 16005 * check can't handle the case when the 16006 * local IP address is INADDR_ANY. 16007 */ 16008 if (tcp->tcp_ipversion == IPV4_VERSION) { 16009 if ((tcp->tcp_ipha->ipha_dst == 16010 tcp->tcp_ipha->ipha_src) && 16011 (BE16_EQL(tcp->tcp_tcph->th_lport, 16012 tcp->tcp_tcph->th_fport))) { 16013 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16014 return; 16015 } 16016 } else { 16017 if (IN6_ARE_ADDR_EQUAL( 16018 &tcp->tcp_ip6h->ip6_dst, 16019 &tcp->tcp_ip6h->ip6_src) && 16020 (BE16_EQL(tcp->tcp_tcph->th_lport, 16021 tcp->tcp_tcph->th_fport))) { 16022 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16023 return; 16024 } 16025 } 16026 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 16027 /* 16028 * This should not be possible! Just for 16029 * defensive coding... 16030 */ 16031 if (tcp->tcp_state != TCPS_SYN_SENT) 16032 goto after_syn_sent; 16033 16034 if (is_system_labeled() && 16035 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 16036 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 16037 return; 16038 } 16039 16040 ASSERT(q == tcp->tcp_rq); 16041 /* 16042 * tcp_adapt_ire() does not adjust 16043 * for TCP/IP header length. 16044 */ 16045 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 16046 16047 /* 16048 * Just make sure our rwnd is at 16049 * least tcp_recv_hiwat_mss * MSS 16050 * large, and round up to the nearest 16051 * MSS. 16052 * 16053 * We do the round up here because 16054 * we need to get the interface 16055 * MTU first before we can do the 16056 * round up. 16057 */ 16058 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 16059 tcps->tcps_recv_hiwat_minmss * mss); 16060 q->q_hiwat = tcp->tcp_rwnd; 16061 tcp_set_ws_value(tcp); 16062 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 16063 tcp->tcp_tcph->th_win); 16064 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 16065 tcp->tcp_snd_ws_ok = B_TRUE; 16066 16067 /* 16068 * Set tcp_snd_ts_ok to true 16069 * so that tcp_xmit_mp will 16070 * include the timestamp 16071 * option in the SYN segment. 16072 */ 16073 if (tcps->tcps_tstamp_always || 16074 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 16075 tcp->tcp_snd_ts_ok = B_TRUE; 16076 } 16077 16078 /* 16079 * tcp_snd_sack_ok can be set in 16080 * tcp_adapt_ire() if the sack metric 16081 * is set. So check it here also. 16082 */ 16083 if (tcps->tcps_sack_permitted == 2 || 16084 tcp->tcp_snd_sack_ok) { 16085 if (tcp->tcp_sack_info == NULL) { 16086 tcp->tcp_sack_info = 16087 kmem_cache_alloc( 16088 tcp_sack_info_cache, 16089 KM_SLEEP); 16090 } 16091 tcp->tcp_snd_sack_ok = B_TRUE; 16092 } 16093 16094 /* 16095 * Should we use ECN? Note that the current 16096 * default value (SunOS 5.9) of tcp_ecn_permitted 16097 * is 1. The reason for doing this is that there 16098 * are equipments out there that will drop ECN 16099 * enabled IP packets. Setting it to 1 avoids 16100 * compatibility problems. 16101 */ 16102 if (tcps->tcps_ecn_permitted == 2) 16103 tcp->tcp_ecn_ok = B_TRUE; 16104 16105 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16106 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 16107 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 16108 if (syn_mp) { 16109 cred_t *cr; 16110 pid_t pid; 16111 16112 /* 16113 * Obtain the credential from the 16114 * thread calling connect(); the credential 16115 * lives on in the second mblk which 16116 * originated from T_CONN_REQ and is echoed 16117 * with the T_BIND_ACK from ip. If none 16118 * can be found, default to the creator 16119 * of the socket. 16120 */ 16121 if (mp->b_cont == NULL || 16122 (cr = DB_CRED(mp->b_cont)) == NULL) { 16123 cr = tcp->tcp_cred; 16124 pid = tcp->tcp_cpid; 16125 } else { 16126 pid = DB_CPID(mp->b_cont); 16127 } 16128 mblk_setcred(syn_mp, cr); 16129 DB_CPID(syn_mp) = pid; 16130 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 16131 } 16132 after_syn_sent: 16133 /* 16134 * A trailer mblk indicates a waiting client upstream. 16135 * We complete here the processing begun in 16136 * either tcp_bind() or tcp_connect() by passing 16137 * upstream the reply message they supplied. 16138 */ 16139 mp1 = mp; 16140 mp = mp->b_cont; 16141 freeb(mp1); 16142 if (mp) 16143 break; 16144 return; 16145 case T_ERROR_ACK: 16146 if (tcp->tcp_debug) { 16147 (void) strlog(TCP_MOD_ID, 0, 1, 16148 SL_TRACE|SL_ERROR, 16149 "tcp_rput_other: case T_ERROR_ACK, " 16150 "ERROR_prim == %d", 16151 tea->ERROR_prim); 16152 } 16153 switch (tea->ERROR_prim) { 16154 case O_T_BIND_REQ: 16155 case T_BIND_REQ: 16156 tcp_bind_failed(tcp, mp, 16157 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16158 ENETUNREACH : EADDRNOTAVAIL)); 16159 return; 16160 case T_UNBIND_REQ: 16161 tcp->tcp_hard_binding = B_FALSE; 16162 tcp->tcp_hard_bound = B_FALSE; 16163 if (mp->b_cont) { 16164 freemsg(mp->b_cont); 16165 mp->b_cont = NULL; 16166 } 16167 if (tcp->tcp_unbind_pending) 16168 tcp->tcp_unbind_pending = 0; 16169 else { 16170 /* From tcp_ip_unbind() - free */ 16171 freemsg(mp); 16172 return; 16173 } 16174 break; 16175 case T_SVR4_OPTMGMT_REQ: 16176 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16177 /* T_OPTMGMT_REQ generated by TCP */ 16178 printf("T_SVR4_OPTMGMT_REQ failed " 16179 "%d/%d - dropped (cnt %d)\n", 16180 tea->TLI_error, tea->UNIX_error, 16181 tcp->tcp_drop_opt_ack_cnt); 16182 freemsg(mp); 16183 tcp->tcp_drop_opt_ack_cnt--; 16184 return; 16185 } 16186 break; 16187 } 16188 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 16189 tcp->tcp_drop_opt_ack_cnt > 0) { 16190 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 16191 "- dropped (cnt %d)\n", 16192 tea->TLI_error, tea->UNIX_error, 16193 tcp->tcp_drop_opt_ack_cnt); 16194 freemsg(mp); 16195 tcp->tcp_drop_opt_ack_cnt--; 16196 return; 16197 } 16198 break; 16199 case T_OPTMGMT_ACK: 16200 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16201 /* T_OPTMGMT_REQ generated by TCP */ 16202 freemsg(mp); 16203 tcp->tcp_drop_opt_ack_cnt--; 16204 return; 16205 } 16206 break; 16207 default: 16208 break; 16209 } 16210 break; 16211 case M_FLUSH: 16212 if (*rptr & FLUSHR) 16213 flushq(q, FLUSHDATA); 16214 break; 16215 default: 16216 /* M_CTL will be directly sent to tcp_icmp_error() */ 16217 ASSERT(DB_TYPE(mp) != M_CTL); 16218 break; 16219 } 16220 /* 16221 * Make sure we set this bit before sending the ACK for 16222 * bind. Otherwise accept could possibly run and free 16223 * this tcp struct. 16224 */ 16225 putnext(q, mp); 16226 } 16227 16228 /* ARGSUSED */ 16229 static void 16230 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 16231 { 16232 conn_t *connp = (conn_t *)arg; 16233 tcp_t *tcp = connp->conn_tcp; 16234 queue_t *q = tcp->tcp_rq; 16235 uint_t thwin; 16236 tcp_stack_t *tcps = tcp->tcp_tcps; 16237 sodirect_t *sodp; 16238 boolean_t fc; 16239 16240 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16241 tcp->tcp_rsrv_mp = mp; 16242 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16243 16244 TCP_STAT(tcps, tcp_rsrv_calls); 16245 16246 if (TCP_IS_DETACHED(tcp) || q == NULL) { 16247 return; 16248 } 16249 16250 if (tcp->tcp_fused) { 16251 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16252 16253 ASSERT(tcp->tcp_fused); 16254 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 16255 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 16256 ASSERT(!TCP_IS_DETACHED(tcp)); 16257 ASSERT(tcp->tcp_connp->conn_sqp == 16258 peer_tcp->tcp_connp->conn_sqp); 16259 16260 /* 16261 * Normally we would not get backenabled in synchronous 16262 * streams mode, but in case this happens, we need to plug 16263 * synchronous streams during our drain to prevent a race 16264 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 16265 */ 16266 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 16267 if (tcp->tcp_rcv_list != NULL) 16268 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16269 16270 if (peer_tcp > tcp) { 16271 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16272 mutex_enter(&tcp->tcp_non_sq_lock); 16273 } else { 16274 mutex_enter(&tcp->tcp_non_sq_lock); 16275 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16276 } 16277 16278 if (peer_tcp->tcp_flow_stopped && 16279 (TCP_UNSENT_BYTES(peer_tcp) <= 16280 peer_tcp->tcp_xmit_lowater)) { 16281 tcp_clrqfull(peer_tcp); 16282 } 16283 mutex_exit(&peer_tcp->tcp_non_sq_lock); 16284 mutex_exit(&tcp->tcp_non_sq_lock); 16285 16286 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16287 TCP_STAT(tcps, tcp_fusion_backenabled); 16288 return; 16289 } 16290 16291 SOD_PTR_ENTER(tcp, sodp); 16292 if (sodp != NULL) { 16293 /* An sodirect connection */ 16294 if (SOD_QFULL(sodp)) { 16295 /* Flow-controlled, need another back-enable */ 16296 fc = B_TRUE; 16297 SOD_QSETBE(sodp); 16298 } else { 16299 /* Not flow-controlled */ 16300 fc = B_FALSE; 16301 } 16302 mutex_exit(sodp->sod_lockp); 16303 } else if (canputnext(q)) { 16304 /* STREAMS, not flow-controlled */ 16305 fc = B_FALSE; 16306 } else { 16307 /* STREAMS, flow-controlled */ 16308 fc = B_TRUE; 16309 } 16310 if (!fc) { 16311 /* Not flow-controlled, open rwnd */ 16312 tcp->tcp_rwnd = q->q_hiwat; 16313 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16314 << tcp->tcp_rcv_ws; 16315 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16316 /* 16317 * Send back a window update immediately if TCP is above 16318 * ESTABLISHED state and the increase of the rcv window 16319 * that the other side knows is at least 1 MSS after flow 16320 * control is lifted. 16321 */ 16322 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16323 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16324 tcp_xmit_ctl(NULL, tcp, 16325 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16326 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16327 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16328 } 16329 } 16330 } 16331 16332 /* 16333 * The read side service routine is called mostly when we get back-enabled as a 16334 * result of flow control relief. Since we don't actually queue anything in 16335 * TCP, we have no data to send out of here. What we do is clear the receive 16336 * window, and send out a window update. 16337 */ 16338 static void 16339 tcp_rsrv(queue_t *q) 16340 { 16341 conn_t *connp = Q_TO_CONN(q); 16342 tcp_t *tcp = connp->conn_tcp; 16343 mblk_t *mp; 16344 tcp_stack_t *tcps = tcp->tcp_tcps; 16345 16346 /* No code does a putq on the read side */ 16347 ASSERT(q->q_first == NULL); 16348 16349 /* Nothing to do for the default queue */ 16350 if (q == tcps->tcps_g_q) { 16351 return; 16352 } 16353 16354 /* 16355 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16356 * been run. So just return. 16357 */ 16358 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16359 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16360 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16361 return; 16362 } 16363 tcp->tcp_rsrv_mp = NULL; 16364 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16365 16366 CONN_INC_REF(connp); 16367 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16368 SQTAG_TCP_RSRV); 16369 } 16370 16371 /* 16372 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16373 * We do not allow the receive window to shrink. After setting rwnd, 16374 * set the flow control hiwat of the stream. 16375 * 16376 * This function is called in 2 cases: 16377 * 16378 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16379 * connection (passive open) and in tcp_rput_data() for active connect. 16380 * This is called after tcp_mss_set() when the desired MSS value is known. 16381 * This makes sure that our window size is a mutiple of the other side's 16382 * MSS. 16383 * 2) Handling SO_RCVBUF option. 16384 * 16385 * It is ASSUMED that the requested size is a multiple of the current MSS. 16386 * 16387 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16388 * user requests so. 16389 */ 16390 static int 16391 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16392 { 16393 uint32_t mss = tcp->tcp_mss; 16394 uint32_t old_max_rwnd; 16395 uint32_t max_transmittable_rwnd; 16396 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16397 tcp_stack_t *tcps = tcp->tcp_tcps; 16398 16399 if (tcp->tcp_fused) { 16400 size_t sth_hiwat; 16401 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16402 16403 ASSERT(peer_tcp != NULL); 16404 /* 16405 * Record the stream head's high water mark for 16406 * this endpoint; this is used for flow-control 16407 * purposes in tcp_fuse_output(). 16408 */ 16409 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16410 if (!tcp_detached) 16411 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16412 16413 /* 16414 * In the fusion case, the maxpsz stream head value of 16415 * our peer is set according to its send buffer size 16416 * and our receive buffer size; since the latter may 16417 * have changed we need to update the peer's maxpsz. 16418 */ 16419 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16420 return (rwnd); 16421 } 16422 16423 if (tcp_detached) 16424 old_max_rwnd = tcp->tcp_rwnd; 16425 else 16426 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16427 16428 /* 16429 * Insist on a receive window that is at least 16430 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16431 * funny TCP interactions of Nagle algorithm, SWS avoidance 16432 * and delayed acknowledgement. 16433 */ 16434 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16435 16436 /* 16437 * If window size info has already been exchanged, TCP should not 16438 * shrink the window. Shrinking window is doable if done carefully. 16439 * We may add that support later. But so far there is not a real 16440 * need to do that. 16441 */ 16442 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16443 /* MSS may have changed, do a round up again. */ 16444 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16445 } 16446 16447 /* 16448 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16449 * can be applied even before the window scale option is decided. 16450 */ 16451 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16452 if (rwnd > max_transmittable_rwnd) { 16453 rwnd = max_transmittable_rwnd - 16454 (max_transmittable_rwnd % mss); 16455 if (rwnd < mss) 16456 rwnd = max_transmittable_rwnd; 16457 /* 16458 * If we're over the limit we may have to back down tcp_rwnd. 16459 * The increment below won't work for us. So we set all three 16460 * here and the increment below will have no effect. 16461 */ 16462 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16463 } 16464 if (tcp->tcp_localnet) { 16465 tcp->tcp_rack_abs_max = 16466 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16467 } else { 16468 /* 16469 * For a remote host on a different subnet (through a router), 16470 * we ack every other packet to be conforming to RFC1122. 16471 * tcp_deferred_acks_max is default to 2. 16472 */ 16473 tcp->tcp_rack_abs_max = 16474 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16475 } 16476 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16477 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16478 else 16479 tcp->tcp_rack_cur_max = 0; 16480 /* 16481 * Increment the current rwnd by the amount the maximum grew (we 16482 * can not overwrite it since we might be in the middle of a 16483 * connection.) 16484 */ 16485 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16486 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16487 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16488 tcp->tcp_cwnd_max = rwnd; 16489 16490 if (tcp_detached) 16491 return (rwnd); 16492 /* 16493 * We set the maximum receive window into rq->q_hiwat. 16494 * This is not actually used for flow control. 16495 */ 16496 tcp->tcp_rq->q_hiwat = rwnd; 16497 /* 16498 * Set the Stream head high water mark. This doesn't have to be 16499 * here, since we are simply using default values, but we would 16500 * prefer to choose these values algorithmically, with a likely 16501 * relationship to rwnd. 16502 */ 16503 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16504 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16505 return (rwnd); 16506 } 16507 16508 /* 16509 * Return SNMP stuff in buffer in mpdata. 16510 */ 16511 mblk_t * 16512 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16513 { 16514 mblk_t *mpdata; 16515 mblk_t *mp_conn_ctl = NULL; 16516 mblk_t *mp_conn_tail; 16517 mblk_t *mp_attr_ctl = NULL; 16518 mblk_t *mp_attr_tail; 16519 mblk_t *mp6_conn_ctl = NULL; 16520 mblk_t *mp6_conn_tail; 16521 mblk_t *mp6_attr_ctl = NULL; 16522 mblk_t *mp6_attr_tail; 16523 struct opthdr *optp; 16524 mib2_tcpConnEntry_t tce; 16525 mib2_tcp6ConnEntry_t tce6; 16526 mib2_transportMLPEntry_t mlp; 16527 connf_t *connfp; 16528 int i; 16529 boolean_t ispriv; 16530 zoneid_t zoneid; 16531 int v4_conn_idx; 16532 int v6_conn_idx; 16533 conn_t *connp = Q_TO_CONN(q); 16534 tcp_stack_t *tcps; 16535 ip_stack_t *ipst; 16536 mblk_t *mp2ctl; 16537 16538 /* 16539 * make a copy of the original message 16540 */ 16541 mp2ctl = copymsg(mpctl); 16542 16543 if (mpctl == NULL || 16544 (mpdata = mpctl->b_cont) == NULL || 16545 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16546 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16547 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16548 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16549 freemsg(mp_conn_ctl); 16550 freemsg(mp_attr_ctl); 16551 freemsg(mp6_conn_ctl); 16552 freemsg(mp6_attr_ctl); 16553 freemsg(mpctl); 16554 freemsg(mp2ctl); 16555 return (NULL); 16556 } 16557 16558 ipst = connp->conn_netstack->netstack_ip; 16559 tcps = connp->conn_netstack->netstack_tcp; 16560 16561 /* build table of connections -- need count in fixed part */ 16562 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16563 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16564 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16565 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16566 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16567 16568 ispriv = 16569 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16570 zoneid = Q_TO_CONN(q)->conn_zoneid; 16571 16572 v4_conn_idx = v6_conn_idx = 0; 16573 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16574 16575 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16576 ipst = tcps->tcps_netstack->netstack_ip; 16577 16578 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16579 16580 connp = NULL; 16581 16582 while ((connp = 16583 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16584 tcp_t *tcp; 16585 boolean_t needattr; 16586 16587 if (connp->conn_zoneid != zoneid) 16588 continue; /* not in this zone */ 16589 16590 tcp = connp->conn_tcp; 16591 UPDATE_MIB(&tcps->tcps_mib, 16592 tcpHCInSegs, tcp->tcp_ibsegs); 16593 tcp->tcp_ibsegs = 0; 16594 UPDATE_MIB(&tcps->tcps_mib, 16595 tcpHCOutSegs, tcp->tcp_obsegs); 16596 tcp->tcp_obsegs = 0; 16597 16598 tce6.tcp6ConnState = tce.tcpConnState = 16599 tcp_snmp_state(tcp); 16600 if (tce.tcpConnState == MIB2_TCP_established || 16601 tce.tcpConnState == MIB2_TCP_closeWait) 16602 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16603 16604 needattr = B_FALSE; 16605 bzero(&mlp, sizeof (mlp)); 16606 if (connp->conn_mlp_type != mlptSingle) { 16607 if (connp->conn_mlp_type == mlptShared || 16608 connp->conn_mlp_type == mlptBoth) 16609 mlp.tme_flags |= MIB2_TMEF_SHARED; 16610 if (connp->conn_mlp_type == mlptPrivate || 16611 connp->conn_mlp_type == mlptBoth) 16612 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16613 needattr = B_TRUE; 16614 } 16615 if (connp->conn_peercred != NULL) { 16616 ts_label_t *tsl; 16617 16618 tsl = crgetlabel(connp->conn_peercred); 16619 mlp.tme_doi = label2doi(tsl); 16620 mlp.tme_label = *label2bslabel(tsl); 16621 needattr = B_TRUE; 16622 } 16623 16624 /* Create a message to report on IPv6 entries */ 16625 if (tcp->tcp_ipversion == IPV6_VERSION) { 16626 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16627 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16628 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16629 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16630 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16631 /* Don't want just anybody seeing these... */ 16632 if (ispriv) { 16633 tce6.tcp6ConnEntryInfo.ce_snxt = 16634 tcp->tcp_snxt; 16635 tce6.tcp6ConnEntryInfo.ce_suna = 16636 tcp->tcp_suna; 16637 tce6.tcp6ConnEntryInfo.ce_rnxt = 16638 tcp->tcp_rnxt; 16639 tce6.tcp6ConnEntryInfo.ce_rack = 16640 tcp->tcp_rack; 16641 } else { 16642 /* 16643 * Netstat, unfortunately, uses this to 16644 * get send/receive queue sizes. How to fix? 16645 * Why not compute the difference only? 16646 */ 16647 tce6.tcp6ConnEntryInfo.ce_snxt = 16648 tcp->tcp_snxt - tcp->tcp_suna; 16649 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16650 tce6.tcp6ConnEntryInfo.ce_rnxt = 16651 tcp->tcp_rnxt - tcp->tcp_rack; 16652 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16653 } 16654 16655 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16656 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16657 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16658 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16659 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16660 16661 tce6.tcp6ConnCreationProcess = 16662 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16663 tcp->tcp_cpid; 16664 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16665 16666 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16667 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16668 16669 mlp.tme_connidx = v6_conn_idx++; 16670 if (needattr) 16671 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16672 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16673 } 16674 /* 16675 * Create an IPv4 table entry for IPv4 entries and also 16676 * for IPv6 entries which are bound to in6addr_any 16677 * but don't have IPV6_V6ONLY set. 16678 * (i.e. anything an IPv4 peer could connect to) 16679 */ 16680 if (tcp->tcp_ipversion == IPV4_VERSION || 16681 (tcp->tcp_state <= TCPS_LISTEN && 16682 !tcp->tcp_connp->conn_ipv6_v6only && 16683 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16684 if (tcp->tcp_ipversion == IPV6_VERSION) { 16685 tce.tcpConnRemAddress = INADDR_ANY; 16686 tce.tcpConnLocalAddress = INADDR_ANY; 16687 } else { 16688 tce.tcpConnRemAddress = 16689 tcp->tcp_remote; 16690 tce.tcpConnLocalAddress = 16691 tcp->tcp_ip_src; 16692 } 16693 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16694 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16695 /* Don't want just anybody seeing these... */ 16696 if (ispriv) { 16697 tce.tcpConnEntryInfo.ce_snxt = 16698 tcp->tcp_snxt; 16699 tce.tcpConnEntryInfo.ce_suna = 16700 tcp->tcp_suna; 16701 tce.tcpConnEntryInfo.ce_rnxt = 16702 tcp->tcp_rnxt; 16703 tce.tcpConnEntryInfo.ce_rack = 16704 tcp->tcp_rack; 16705 } else { 16706 /* 16707 * Netstat, unfortunately, uses this to 16708 * get send/receive queue sizes. How 16709 * to fix? 16710 * Why not compute the difference only? 16711 */ 16712 tce.tcpConnEntryInfo.ce_snxt = 16713 tcp->tcp_snxt - tcp->tcp_suna; 16714 tce.tcpConnEntryInfo.ce_suna = 0; 16715 tce.tcpConnEntryInfo.ce_rnxt = 16716 tcp->tcp_rnxt - tcp->tcp_rack; 16717 tce.tcpConnEntryInfo.ce_rack = 0; 16718 } 16719 16720 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16721 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16722 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16723 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16724 tce.tcpConnEntryInfo.ce_state = 16725 tcp->tcp_state; 16726 16727 tce.tcpConnCreationProcess = 16728 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16729 tcp->tcp_cpid; 16730 tce.tcpConnCreationTime = tcp->tcp_open_time; 16731 16732 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16733 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16734 16735 mlp.tme_connidx = v4_conn_idx++; 16736 if (needattr) 16737 (void) snmp_append_data2( 16738 mp_attr_ctl->b_cont, 16739 &mp_attr_tail, (char *)&mlp, 16740 sizeof (mlp)); 16741 } 16742 } 16743 } 16744 16745 /* fixed length structure for IPv4 and IPv6 counters */ 16746 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16747 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16748 sizeof (mib2_tcp6ConnEntry_t)); 16749 /* synchronize 32- and 64-bit counters */ 16750 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16751 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16752 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16753 optp->level = MIB2_TCP; 16754 optp->name = 0; 16755 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16756 sizeof (tcps->tcps_mib)); 16757 optp->len = msgdsize(mpdata); 16758 qreply(q, mpctl); 16759 16760 /* table of connections... */ 16761 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16762 sizeof (struct T_optmgmt_ack)]; 16763 optp->level = MIB2_TCP; 16764 optp->name = MIB2_TCP_CONN; 16765 optp->len = msgdsize(mp_conn_ctl->b_cont); 16766 qreply(q, mp_conn_ctl); 16767 16768 /* table of MLP attributes... */ 16769 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16770 sizeof (struct T_optmgmt_ack)]; 16771 optp->level = MIB2_TCP; 16772 optp->name = EXPER_XPORT_MLP; 16773 optp->len = msgdsize(mp_attr_ctl->b_cont); 16774 if (optp->len == 0) 16775 freemsg(mp_attr_ctl); 16776 else 16777 qreply(q, mp_attr_ctl); 16778 16779 /* table of IPv6 connections... */ 16780 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16781 sizeof (struct T_optmgmt_ack)]; 16782 optp->level = MIB2_TCP6; 16783 optp->name = MIB2_TCP6_CONN; 16784 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16785 qreply(q, mp6_conn_ctl); 16786 16787 /* table of IPv6 MLP attributes... */ 16788 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16789 sizeof (struct T_optmgmt_ack)]; 16790 optp->level = MIB2_TCP6; 16791 optp->name = EXPER_XPORT_MLP; 16792 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16793 if (optp->len == 0) 16794 freemsg(mp6_attr_ctl); 16795 else 16796 qreply(q, mp6_attr_ctl); 16797 return (mp2ctl); 16798 } 16799 16800 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16801 /* ARGSUSED */ 16802 int 16803 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16804 { 16805 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16806 16807 switch (level) { 16808 case MIB2_TCP: 16809 switch (name) { 16810 case 13: 16811 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16812 return (0); 16813 /* TODO: delete entry defined by tce */ 16814 return (1); 16815 default: 16816 return (0); 16817 } 16818 default: 16819 return (1); 16820 } 16821 } 16822 16823 /* Translate TCP state to MIB2 TCP state. */ 16824 static int 16825 tcp_snmp_state(tcp_t *tcp) 16826 { 16827 if (tcp == NULL) 16828 return (0); 16829 16830 switch (tcp->tcp_state) { 16831 case TCPS_CLOSED: 16832 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16833 case TCPS_BOUND: 16834 return (MIB2_TCP_closed); 16835 case TCPS_LISTEN: 16836 return (MIB2_TCP_listen); 16837 case TCPS_SYN_SENT: 16838 return (MIB2_TCP_synSent); 16839 case TCPS_SYN_RCVD: 16840 return (MIB2_TCP_synReceived); 16841 case TCPS_ESTABLISHED: 16842 return (MIB2_TCP_established); 16843 case TCPS_CLOSE_WAIT: 16844 return (MIB2_TCP_closeWait); 16845 case TCPS_FIN_WAIT_1: 16846 return (MIB2_TCP_finWait1); 16847 case TCPS_CLOSING: 16848 return (MIB2_TCP_closing); 16849 case TCPS_LAST_ACK: 16850 return (MIB2_TCP_lastAck); 16851 case TCPS_FIN_WAIT_2: 16852 return (MIB2_TCP_finWait2); 16853 case TCPS_TIME_WAIT: 16854 return (MIB2_TCP_timeWait); 16855 default: 16856 return (0); 16857 } 16858 } 16859 16860 static char tcp_report_header[] = 16861 "TCP " MI_COL_HDRPAD_STR 16862 "zone dest snxt suna " 16863 "swnd rnxt rack rwnd rto mss w sw rw t " 16864 "recent [lport,fport] state"; 16865 16866 /* 16867 * TCP status report triggered via the Named Dispatch mechanism. 16868 */ 16869 /* ARGSUSED */ 16870 static void 16871 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16872 cred_t *cr) 16873 { 16874 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16875 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16876 char cflag; 16877 in6_addr_t v6dst; 16878 char buf[80]; 16879 uint_t print_len, buf_len; 16880 16881 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16882 if (buf_len <= 0) 16883 return; 16884 16885 if (hashval >= 0) 16886 (void) sprintf(hash, "%03d ", hashval); 16887 else 16888 hash[0] = '\0'; 16889 16890 /* 16891 * Note that we use the remote address in the tcp_b structure. 16892 * This means that it will print out the real destination address, 16893 * not the next hop's address if source routing is used. This 16894 * avoid the confusion on the output because user may not 16895 * know that source routing is used for a connection. 16896 */ 16897 if (tcp->tcp_ipversion == IPV4_VERSION) { 16898 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16899 } else { 16900 v6dst = tcp->tcp_remote_v6; 16901 } 16902 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16903 /* 16904 * the ispriv checks are so that normal users cannot determine 16905 * sequence number information using NDD. 16906 */ 16907 16908 if (TCP_IS_DETACHED(tcp)) 16909 cflag = '*'; 16910 else 16911 cflag = ' '; 16912 print_len = snprintf((char *)mp->b_wptr, buf_len, 16913 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16914 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16915 hash, 16916 (void *)tcp, 16917 tcp->tcp_connp->conn_zoneid, 16918 addrbuf, 16919 (ispriv) ? tcp->tcp_snxt : 0, 16920 (ispriv) ? tcp->tcp_suna : 0, 16921 tcp->tcp_swnd, 16922 (ispriv) ? tcp->tcp_rnxt : 0, 16923 (ispriv) ? tcp->tcp_rack : 0, 16924 tcp->tcp_rwnd, 16925 tcp->tcp_rto, 16926 tcp->tcp_mss, 16927 tcp->tcp_snd_ws_ok, 16928 tcp->tcp_snd_ws, 16929 tcp->tcp_rcv_ws, 16930 tcp->tcp_snd_ts_ok, 16931 tcp->tcp_ts_recent, 16932 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16933 if (print_len < buf_len) { 16934 ((mblk_t *)mp)->b_wptr += print_len; 16935 } else { 16936 ((mblk_t *)mp)->b_wptr += buf_len; 16937 } 16938 } 16939 16940 /* 16941 * TCP status report (for listeners only) triggered via the Named Dispatch 16942 * mechanism. 16943 */ 16944 /* ARGSUSED */ 16945 static void 16946 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16947 { 16948 char addrbuf[INET6_ADDRSTRLEN]; 16949 in6_addr_t v6dst; 16950 uint_t print_len, buf_len; 16951 16952 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16953 if (buf_len <= 0) 16954 return; 16955 16956 if (tcp->tcp_ipversion == IPV4_VERSION) { 16957 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16958 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16959 } else { 16960 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16961 addrbuf, sizeof (addrbuf)); 16962 } 16963 print_len = snprintf((char *)mp->b_wptr, buf_len, 16964 "%03d " 16965 MI_COL_PTRFMT_STR 16966 "%d %s %05u %08u %d/%d/%d%c\n", 16967 hashval, (void *)tcp, 16968 tcp->tcp_connp->conn_zoneid, 16969 addrbuf, 16970 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16971 tcp->tcp_conn_req_seqnum, 16972 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16973 tcp->tcp_conn_req_max, 16974 tcp->tcp_syn_defense ? '*' : ' '); 16975 if (print_len < buf_len) { 16976 ((mblk_t *)mp)->b_wptr += print_len; 16977 } else { 16978 ((mblk_t *)mp)->b_wptr += buf_len; 16979 } 16980 } 16981 16982 /* TCP status report triggered via the Named Dispatch mechanism. */ 16983 /* ARGSUSED */ 16984 static int 16985 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16986 { 16987 tcp_t *tcp; 16988 int i; 16989 conn_t *connp; 16990 connf_t *connfp; 16991 zoneid_t zoneid; 16992 tcp_stack_t *tcps; 16993 ip_stack_t *ipst; 16994 16995 zoneid = Q_TO_CONN(q)->conn_zoneid; 16996 tcps = Q_TO_TCP(q)->tcp_tcps; 16997 16998 /* 16999 * Because of the ndd constraint, at most we can have 64K buffer 17000 * to put in all TCP info. So to be more efficient, just 17001 * allocate a 64K buffer here, assuming we need that large buffer. 17002 * This may be a problem as any user can read tcp_status. Therefore 17003 * we limit the rate of doing this using tcp_ndd_get_info_interval. 17004 * This should be OK as normal users should not do this too often. 17005 */ 17006 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17007 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17008 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17009 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17010 return (0); 17011 } 17012 } 17013 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17014 /* The following may work even if we cannot get a large buf. */ 17015 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17016 return (0); 17017 } 17018 17019 (void) mi_mpprintf(mp, "%s", tcp_report_header); 17020 17021 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 17022 17023 ipst = tcps->tcps_netstack->netstack_ip; 17024 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 17025 17026 connp = NULL; 17027 17028 while ((connp = 17029 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17030 tcp = connp->conn_tcp; 17031 if (zoneid != GLOBAL_ZONEID && 17032 zoneid != connp->conn_zoneid) 17033 continue; 17034 tcp_report_item(mp->b_cont, tcp, -1, tcp, 17035 cr); 17036 } 17037 17038 } 17039 17040 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17041 return (0); 17042 } 17043 17044 /* TCP status report triggered via the Named Dispatch mechanism. */ 17045 /* ARGSUSED */ 17046 static int 17047 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17048 { 17049 tf_t *tbf; 17050 tcp_t *tcp; 17051 int i; 17052 zoneid_t zoneid; 17053 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17054 17055 zoneid = Q_TO_CONN(q)->conn_zoneid; 17056 17057 /* Refer to comments in tcp_status_report(). */ 17058 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17059 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17060 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17061 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17062 return (0); 17063 } 17064 } 17065 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17066 /* The following may work even if we cannot get a large buf. */ 17067 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17068 return (0); 17069 } 17070 17071 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17072 17073 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 17074 tbf = &tcps->tcps_bind_fanout[i]; 17075 mutex_enter(&tbf->tf_lock); 17076 for (tcp = tbf->tf_tcp; tcp != NULL; 17077 tcp = tcp->tcp_bind_hash) { 17078 if (zoneid != GLOBAL_ZONEID && 17079 zoneid != tcp->tcp_connp->conn_zoneid) 17080 continue; 17081 CONN_INC_REF(tcp->tcp_connp); 17082 tcp_report_item(mp->b_cont, tcp, i, 17083 Q_TO_TCP(q), cr); 17084 CONN_DEC_REF(tcp->tcp_connp); 17085 } 17086 mutex_exit(&tbf->tf_lock); 17087 } 17088 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17089 return (0); 17090 } 17091 17092 /* TCP status report triggered via the Named Dispatch mechanism. */ 17093 /* ARGSUSED */ 17094 static int 17095 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17096 { 17097 connf_t *connfp; 17098 conn_t *connp; 17099 tcp_t *tcp; 17100 int i; 17101 zoneid_t zoneid; 17102 tcp_stack_t *tcps; 17103 ip_stack_t *ipst; 17104 17105 zoneid = Q_TO_CONN(q)->conn_zoneid; 17106 tcps = Q_TO_TCP(q)->tcp_tcps; 17107 17108 /* Refer to comments in tcp_status_report(). */ 17109 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17110 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17111 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17112 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17113 return (0); 17114 } 17115 } 17116 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17117 /* The following may work even if we cannot get a large buf. */ 17118 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17119 return (0); 17120 } 17121 17122 (void) mi_mpprintf(mp, 17123 " TCP " MI_COL_HDRPAD_STR 17124 "zone IP addr port seqnum backlog (q0/q/max)"); 17125 17126 ipst = tcps->tcps_netstack->netstack_ip; 17127 17128 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 17129 connfp = &ipst->ips_ipcl_bind_fanout[i]; 17130 connp = NULL; 17131 while ((connp = 17132 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17133 tcp = connp->conn_tcp; 17134 if (zoneid != GLOBAL_ZONEID && 17135 zoneid != connp->conn_zoneid) 17136 continue; 17137 tcp_report_listener(mp->b_cont, tcp, i); 17138 } 17139 } 17140 17141 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17142 return (0); 17143 } 17144 17145 /* TCP status report triggered via the Named Dispatch mechanism. */ 17146 /* ARGSUSED */ 17147 static int 17148 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17149 { 17150 connf_t *connfp; 17151 conn_t *connp; 17152 tcp_t *tcp; 17153 int i; 17154 zoneid_t zoneid; 17155 tcp_stack_t *tcps; 17156 ip_stack_t *ipst; 17157 17158 zoneid = Q_TO_CONN(q)->conn_zoneid; 17159 tcps = Q_TO_TCP(q)->tcp_tcps; 17160 ipst = tcps->tcps_netstack->netstack_ip; 17161 17162 /* Refer to comments in tcp_status_report(). */ 17163 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17164 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17165 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17166 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17167 return (0); 17168 } 17169 } 17170 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17171 /* The following may work even if we cannot get a large buf. */ 17172 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17173 return (0); 17174 } 17175 17176 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 17177 ipst->ips_ipcl_conn_fanout_size); 17178 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17179 17180 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 17181 connfp = &ipst->ips_ipcl_conn_fanout[i]; 17182 connp = NULL; 17183 while ((connp = 17184 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17185 tcp = connp->conn_tcp; 17186 if (zoneid != GLOBAL_ZONEID && 17187 zoneid != connp->conn_zoneid) 17188 continue; 17189 tcp_report_item(mp->b_cont, tcp, i, 17190 Q_TO_TCP(q), cr); 17191 } 17192 } 17193 17194 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17195 return (0); 17196 } 17197 17198 /* TCP status report triggered via the Named Dispatch mechanism. */ 17199 /* ARGSUSED */ 17200 static int 17201 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17202 { 17203 tf_t *tf; 17204 tcp_t *tcp; 17205 int i; 17206 zoneid_t zoneid; 17207 tcp_stack_t *tcps; 17208 17209 zoneid = Q_TO_CONN(q)->conn_zoneid; 17210 tcps = Q_TO_TCP(q)->tcp_tcps; 17211 17212 /* Refer to comments in tcp_status_report(). */ 17213 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17214 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17215 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17216 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17217 return (0); 17218 } 17219 } 17220 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17221 /* The following may work even if we cannot get a large buf. */ 17222 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17223 return (0); 17224 } 17225 17226 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17227 17228 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 17229 tf = &tcps->tcps_acceptor_fanout[i]; 17230 mutex_enter(&tf->tf_lock); 17231 for (tcp = tf->tf_tcp; tcp != NULL; 17232 tcp = tcp->tcp_acceptor_hash) { 17233 if (zoneid != GLOBAL_ZONEID && 17234 zoneid != tcp->tcp_connp->conn_zoneid) 17235 continue; 17236 tcp_report_item(mp->b_cont, tcp, i, 17237 Q_TO_TCP(q), cr); 17238 } 17239 mutex_exit(&tf->tf_lock); 17240 } 17241 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17242 return (0); 17243 } 17244 17245 /* 17246 * tcp_timer is the timer service routine. It handles the retransmission, 17247 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 17248 * from the state of the tcp instance what kind of action needs to be done 17249 * at the time it is called. 17250 */ 17251 static void 17252 tcp_timer(void *arg) 17253 { 17254 mblk_t *mp; 17255 clock_t first_threshold; 17256 clock_t second_threshold; 17257 clock_t ms; 17258 uint32_t mss; 17259 conn_t *connp = (conn_t *)arg; 17260 tcp_t *tcp = connp->conn_tcp; 17261 tcp_stack_t *tcps = tcp->tcp_tcps; 17262 17263 tcp->tcp_timer_tid = 0; 17264 17265 if (tcp->tcp_fused) 17266 return; 17267 17268 first_threshold = tcp->tcp_first_timer_threshold; 17269 second_threshold = tcp->tcp_second_timer_threshold; 17270 switch (tcp->tcp_state) { 17271 case TCPS_IDLE: 17272 case TCPS_BOUND: 17273 case TCPS_LISTEN: 17274 return; 17275 case TCPS_SYN_RCVD: { 17276 tcp_t *listener = tcp->tcp_listener; 17277 17278 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17279 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17280 /* it's our first timeout */ 17281 tcp->tcp_syn_rcvd_timeout = 1; 17282 mutex_enter(&listener->tcp_eager_lock); 17283 listener->tcp_syn_rcvd_timeout++; 17284 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17285 /* 17286 * Make this eager available for drop if we 17287 * need to drop one to accomodate a new 17288 * incoming SYN request. 17289 */ 17290 MAKE_DROPPABLE(listener, tcp); 17291 } 17292 if (!listener->tcp_syn_defense && 17293 (listener->tcp_syn_rcvd_timeout > 17294 (tcps->tcps_conn_req_max_q0 >> 2)) && 17295 (tcps->tcps_conn_req_max_q0 > 200)) { 17296 /* We may be under attack. Put on a defense. */ 17297 listener->tcp_syn_defense = B_TRUE; 17298 cmn_err(CE_WARN, "High TCP connect timeout " 17299 "rate! System (port %d) may be under a " 17300 "SYN flood attack!", 17301 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17302 17303 listener->tcp_ip_addr_cache = kmem_zalloc( 17304 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17305 KM_NOSLEEP); 17306 } 17307 mutex_exit(&listener->tcp_eager_lock); 17308 } else if (listener != NULL) { 17309 mutex_enter(&listener->tcp_eager_lock); 17310 tcp->tcp_syn_rcvd_timeout++; 17311 if (tcp->tcp_syn_rcvd_timeout > 1 && 17312 !tcp->tcp_closemp_used) { 17313 /* 17314 * This is our second timeout. Put the tcp in 17315 * the list of droppable eagers to allow it to 17316 * be dropped, if needed. We don't check 17317 * whether tcp_dontdrop is set or not to 17318 * protect ourselve from a SYN attack where a 17319 * remote host can spoof itself as one of the 17320 * good IP source and continue to hold 17321 * resources too long. 17322 */ 17323 MAKE_DROPPABLE(listener, tcp); 17324 } 17325 mutex_exit(&listener->tcp_eager_lock); 17326 } 17327 } 17328 /* FALLTHRU */ 17329 case TCPS_SYN_SENT: 17330 first_threshold = tcp->tcp_first_ctimer_threshold; 17331 second_threshold = tcp->tcp_second_ctimer_threshold; 17332 break; 17333 case TCPS_ESTABLISHED: 17334 case TCPS_FIN_WAIT_1: 17335 case TCPS_CLOSING: 17336 case TCPS_CLOSE_WAIT: 17337 case TCPS_LAST_ACK: 17338 /* If we have data to rexmit */ 17339 if (tcp->tcp_suna != tcp->tcp_snxt) { 17340 clock_t time_to_wait; 17341 17342 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17343 if (!tcp->tcp_xmit_head) 17344 break; 17345 time_to_wait = lbolt - 17346 (clock_t)tcp->tcp_xmit_head->b_prev; 17347 time_to_wait = tcp->tcp_rto - 17348 TICK_TO_MSEC(time_to_wait); 17349 /* 17350 * If the timer fires too early, 1 clock tick earlier, 17351 * restart the timer. 17352 */ 17353 if (time_to_wait > msec_per_tick) { 17354 TCP_STAT(tcps, tcp_timer_fire_early); 17355 TCP_TIMER_RESTART(tcp, time_to_wait); 17356 return; 17357 } 17358 /* 17359 * When we probe zero windows, we force the swnd open. 17360 * If our peer acks with a closed window swnd will be 17361 * set to zero by tcp_rput(). As long as we are 17362 * receiving acks tcp_rput will 17363 * reset 'tcp_ms_we_have_waited' so as not to trip the 17364 * first and second interval actions. NOTE: the timer 17365 * interval is allowed to continue its exponential 17366 * backoff. 17367 */ 17368 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17369 if (tcp->tcp_debug) { 17370 (void) strlog(TCP_MOD_ID, 0, 1, 17371 SL_TRACE, "tcp_timer: zero win"); 17372 } 17373 } else { 17374 /* 17375 * After retransmission, we need to do 17376 * slow start. Set the ssthresh to one 17377 * half of current effective window and 17378 * cwnd to one MSS. Also reset 17379 * tcp_cwnd_cnt. 17380 * 17381 * Note that if tcp_ssthresh is reduced because 17382 * of ECN, do not reduce it again unless it is 17383 * already one window of data away (tcp_cwr 17384 * should then be cleared) or this is a 17385 * timeout for a retransmitted segment. 17386 */ 17387 uint32_t npkt; 17388 17389 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17390 npkt = ((tcp->tcp_timer_backoff ? 17391 tcp->tcp_cwnd_ssthresh : 17392 tcp->tcp_snxt - 17393 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17394 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17395 tcp->tcp_mss; 17396 } 17397 tcp->tcp_cwnd = tcp->tcp_mss; 17398 tcp->tcp_cwnd_cnt = 0; 17399 if (tcp->tcp_ecn_ok) { 17400 tcp->tcp_cwr = B_TRUE; 17401 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17402 tcp->tcp_ecn_cwr_sent = B_FALSE; 17403 } 17404 } 17405 break; 17406 } 17407 /* 17408 * We have something to send yet we cannot send. The 17409 * reason can be: 17410 * 17411 * 1. Zero send window: we need to do zero window probe. 17412 * 2. Zero cwnd: because of ECN, we need to "clock out 17413 * segments. 17414 * 3. SWS avoidance: receiver may have shrunk window, 17415 * reset our knowledge. 17416 * 17417 * Note that condition 2 can happen with either 1 or 17418 * 3. But 1 and 3 are exclusive. 17419 */ 17420 if (tcp->tcp_unsent != 0) { 17421 if (tcp->tcp_cwnd == 0) { 17422 /* 17423 * Set tcp_cwnd to 1 MSS so that a 17424 * new segment can be sent out. We 17425 * are "clocking out" new data when 17426 * the network is really congested. 17427 */ 17428 ASSERT(tcp->tcp_ecn_ok); 17429 tcp->tcp_cwnd = tcp->tcp_mss; 17430 } 17431 if (tcp->tcp_swnd == 0) { 17432 /* Extend window for zero window probe */ 17433 tcp->tcp_swnd++; 17434 tcp->tcp_zero_win_probe = B_TRUE; 17435 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17436 } else { 17437 /* 17438 * Handle timeout from sender SWS avoidance. 17439 * Reset our knowledge of the max send window 17440 * since the receiver might have reduced its 17441 * receive buffer. Avoid setting tcp_max_swnd 17442 * to one since that will essentially disable 17443 * the SWS checks. 17444 * 17445 * Note that since we don't have a SWS 17446 * state variable, if the timeout is set 17447 * for ECN but not for SWS, this 17448 * code will also be executed. This is 17449 * fine as tcp_max_swnd is updated 17450 * constantly and it will not affect 17451 * anything. 17452 */ 17453 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17454 } 17455 tcp_wput_data(tcp, NULL, B_FALSE); 17456 return; 17457 } 17458 /* Is there a FIN that needs to be to re retransmitted? */ 17459 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17460 !tcp->tcp_fin_acked) 17461 break; 17462 /* Nothing to do, return without restarting timer. */ 17463 TCP_STAT(tcps, tcp_timer_fire_miss); 17464 return; 17465 case TCPS_FIN_WAIT_2: 17466 /* 17467 * User closed the TCP endpoint and peer ACK'ed our FIN. 17468 * We waited some time for for peer's FIN, but it hasn't 17469 * arrived. We flush the connection now to avoid 17470 * case where the peer has rebooted. 17471 */ 17472 if (TCP_IS_DETACHED(tcp)) { 17473 (void) tcp_clean_death(tcp, 0, 23); 17474 } else { 17475 TCP_TIMER_RESTART(tcp, 17476 tcps->tcps_fin_wait_2_flush_interval); 17477 } 17478 return; 17479 case TCPS_TIME_WAIT: 17480 (void) tcp_clean_death(tcp, 0, 24); 17481 return; 17482 default: 17483 if (tcp->tcp_debug) { 17484 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17485 "tcp_timer: strange state (%d) %s", 17486 tcp->tcp_state, tcp_display(tcp, NULL, 17487 DISP_PORT_ONLY)); 17488 } 17489 return; 17490 } 17491 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17492 /* 17493 * For zero window probe, we need to send indefinitely, 17494 * unless we have not heard from the other side for some 17495 * time... 17496 */ 17497 if ((tcp->tcp_zero_win_probe == 0) || 17498 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17499 second_threshold)) { 17500 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17501 /* 17502 * If TCP is in SYN_RCVD state, send back a 17503 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17504 * should be zero in TCPS_SYN_RCVD state. 17505 */ 17506 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17507 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17508 "in SYN_RCVD", 17509 tcp, tcp->tcp_snxt, 17510 tcp->tcp_rnxt, TH_RST | TH_ACK); 17511 } 17512 (void) tcp_clean_death(tcp, 17513 tcp->tcp_client_errno ? 17514 tcp->tcp_client_errno : ETIMEDOUT, 25); 17515 return; 17516 } else { 17517 /* 17518 * Set tcp_ms_we_have_waited to second_threshold 17519 * so that in next timeout, we will do the above 17520 * check (lbolt - tcp_last_recv_time). This is 17521 * also to avoid overflow. 17522 * 17523 * We don't need to decrement tcp_timer_backoff 17524 * to avoid overflow because it will be decremented 17525 * later if new timeout value is greater than 17526 * tcp_rexmit_interval_max. In the case when 17527 * tcp_rexmit_interval_max is greater than 17528 * second_threshold, it means that we will wait 17529 * longer than second_threshold to send the next 17530 * window probe. 17531 */ 17532 tcp->tcp_ms_we_have_waited = second_threshold; 17533 } 17534 } else if (ms > first_threshold) { 17535 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17536 tcp->tcp_xmit_head != NULL) { 17537 tcp->tcp_xmit_head = 17538 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17539 } 17540 /* 17541 * We have been retransmitting for too long... The RTT 17542 * we calculated is probably incorrect. Reinitialize it. 17543 * Need to compensate for 0 tcp_rtt_sa. Reset 17544 * tcp_rtt_update so that we won't accidentally cache a 17545 * bad value. But only do this if this is not a zero 17546 * window probe. 17547 */ 17548 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17549 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17550 (tcp->tcp_rtt_sa >> 5); 17551 tcp->tcp_rtt_sa = 0; 17552 tcp_ip_notify(tcp); 17553 tcp->tcp_rtt_update = 0; 17554 } 17555 } 17556 tcp->tcp_timer_backoff++; 17557 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17558 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17559 tcps->tcps_rexmit_interval_min) { 17560 /* 17561 * This means the original RTO is tcp_rexmit_interval_min. 17562 * So we will use tcp_rexmit_interval_min as the RTO value 17563 * and do the backoff. 17564 */ 17565 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17566 } else { 17567 ms <<= tcp->tcp_timer_backoff; 17568 } 17569 if (ms > tcps->tcps_rexmit_interval_max) { 17570 ms = tcps->tcps_rexmit_interval_max; 17571 /* 17572 * ms is at max, decrement tcp_timer_backoff to avoid 17573 * overflow. 17574 */ 17575 tcp->tcp_timer_backoff--; 17576 } 17577 tcp->tcp_ms_we_have_waited += ms; 17578 if (tcp->tcp_zero_win_probe == 0) { 17579 tcp->tcp_rto = ms; 17580 } 17581 TCP_TIMER_RESTART(tcp, ms); 17582 /* 17583 * This is after a timeout and tcp_rto is backed off. Set 17584 * tcp_set_timer to 1 so that next time RTO is updated, we will 17585 * restart the timer with a correct value. 17586 */ 17587 tcp->tcp_set_timer = 1; 17588 mss = tcp->tcp_snxt - tcp->tcp_suna; 17589 if (mss > tcp->tcp_mss) 17590 mss = tcp->tcp_mss; 17591 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17592 mss = tcp->tcp_swnd; 17593 17594 if ((mp = tcp->tcp_xmit_head) != NULL) 17595 mp->b_prev = (mblk_t *)lbolt; 17596 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17597 B_TRUE); 17598 17599 /* 17600 * When slow start after retransmission begins, start with 17601 * this seq no. tcp_rexmit_max marks the end of special slow 17602 * start phase. tcp_snd_burst controls how many segments 17603 * can be sent because of an ack. 17604 */ 17605 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17606 tcp->tcp_snd_burst = TCP_CWND_SS; 17607 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17608 (tcp->tcp_unsent == 0)) { 17609 tcp->tcp_rexmit_max = tcp->tcp_fss; 17610 } else { 17611 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17612 } 17613 tcp->tcp_rexmit = B_TRUE; 17614 tcp->tcp_dupack_cnt = 0; 17615 17616 /* 17617 * Remove all rexmit SACK blk to start from fresh. 17618 */ 17619 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17620 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17621 tcp->tcp_num_notsack_blk = 0; 17622 tcp->tcp_cnt_notsack_list = 0; 17623 } 17624 if (mp == NULL) { 17625 return; 17626 } 17627 /* Attach credentials to retransmitted initial SYNs. */ 17628 if (tcp->tcp_state == TCPS_SYN_SENT) { 17629 mblk_setcred(mp, tcp->tcp_cred); 17630 DB_CPID(mp) = tcp->tcp_cpid; 17631 } 17632 17633 tcp->tcp_csuna = tcp->tcp_snxt; 17634 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17635 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17636 tcp_send_data(tcp, tcp->tcp_wq, mp); 17637 17638 } 17639 17640 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17641 static void 17642 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17643 { 17644 conn_t *connp; 17645 17646 switch (tcp->tcp_state) { 17647 case TCPS_BOUND: 17648 case TCPS_LISTEN: 17649 break; 17650 default: 17651 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17652 return; 17653 } 17654 17655 /* 17656 * Need to clean up all the eagers since after the unbind, segments 17657 * will no longer be delivered to this listener stream. 17658 */ 17659 mutex_enter(&tcp->tcp_eager_lock); 17660 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17661 tcp_eager_cleanup(tcp, 0); 17662 } 17663 mutex_exit(&tcp->tcp_eager_lock); 17664 17665 if (tcp->tcp_ipversion == IPV4_VERSION) { 17666 tcp->tcp_ipha->ipha_src = 0; 17667 } else { 17668 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17669 } 17670 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17671 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17672 tcp_bind_hash_remove(tcp); 17673 tcp->tcp_state = TCPS_IDLE; 17674 tcp->tcp_mdt = B_FALSE; 17675 /* Send M_FLUSH according to TPI */ 17676 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17677 connp = tcp->tcp_connp; 17678 connp->conn_mdt_ok = B_FALSE; 17679 ipcl_hash_remove(connp); 17680 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17681 mp = mi_tpi_ok_ack_alloc(mp); 17682 putnext(tcp->tcp_rq, mp); 17683 } 17684 17685 /* 17686 * Don't let port fall into the privileged range. 17687 * Since the extra privileged ports can be arbitrary we also 17688 * ensure that we exclude those from consideration. 17689 * tcp_g_epriv_ports is not sorted thus we loop over it until 17690 * there are no changes. 17691 * 17692 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17693 * but instead the code relies on: 17694 * - the fact that the address of the array and its size never changes 17695 * - the atomic assignment of the elements of the array 17696 * 17697 * Returns 0 if there are no more ports available. 17698 * 17699 * TS note: skip multilevel ports. 17700 */ 17701 static in_port_t 17702 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17703 { 17704 int i; 17705 boolean_t restart = B_FALSE; 17706 tcp_stack_t *tcps = tcp->tcp_tcps; 17707 17708 if (random && tcp_random_anon_port != 0) { 17709 (void) random_get_pseudo_bytes((uint8_t *)&port, 17710 sizeof (in_port_t)); 17711 /* 17712 * Unless changed by a sys admin, the smallest anon port 17713 * is 32768 and the largest anon port is 65535. It is 17714 * very likely (50%) for the random port to be smaller 17715 * than the smallest anon port. When that happens, 17716 * add port % (anon port range) to the smallest anon 17717 * port to get the random port. It should fall into the 17718 * valid anon port range. 17719 */ 17720 if (port < tcps->tcps_smallest_anon_port) { 17721 port = tcps->tcps_smallest_anon_port + 17722 port % (tcps->tcps_largest_anon_port - 17723 tcps->tcps_smallest_anon_port); 17724 } 17725 } 17726 17727 retry: 17728 if (port < tcps->tcps_smallest_anon_port) 17729 port = (in_port_t)tcps->tcps_smallest_anon_port; 17730 17731 if (port > tcps->tcps_largest_anon_port) { 17732 if (restart) 17733 return (0); 17734 restart = B_TRUE; 17735 port = (in_port_t)tcps->tcps_smallest_anon_port; 17736 } 17737 17738 if (port < tcps->tcps_smallest_nonpriv_port) 17739 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17740 17741 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17742 if (port == tcps->tcps_g_epriv_ports[i]) { 17743 port++; 17744 /* 17745 * Make sure whether the port is in the 17746 * valid range. 17747 */ 17748 goto retry; 17749 } 17750 } 17751 if (is_system_labeled() && 17752 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17753 IPPROTO_TCP, B_TRUE)) != 0) { 17754 port = i; 17755 goto retry; 17756 } 17757 return (port); 17758 } 17759 17760 /* 17761 * Return the next anonymous port in the privileged port range for 17762 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17763 * downwards. This is the same behavior as documented in the userland 17764 * library call rresvport(3N). 17765 * 17766 * TS note: skip multilevel ports. 17767 */ 17768 static in_port_t 17769 tcp_get_next_priv_port(const tcp_t *tcp) 17770 { 17771 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17772 in_port_t nextport; 17773 boolean_t restart = B_FALSE; 17774 tcp_stack_t *tcps = tcp->tcp_tcps; 17775 retry: 17776 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17777 next_priv_port >= IPPORT_RESERVED) { 17778 next_priv_port = IPPORT_RESERVED - 1; 17779 if (restart) 17780 return (0); 17781 restart = B_TRUE; 17782 } 17783 if (is_system_labeled() && 17784 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17785 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17786 next_priv_port = nextport; 17787 goto retry; 17788 } 17789 return (next_priv_port--); 17790 } 17791 17792 /* The write side r/w procedure. */ 17793 17794 #if CCS_STATS 17795 struct { 17796 struct { 17797 int64_t count, bytes; 17798 } tot, hit; 17799 } wrw_stats; 17800 #endif 17801 17802 /* 17803 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17804 * messages. 17805 */ 17806 /* ARGSUSED */ 17807 static void 17808 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17809 { 17810 conn_t *connp = (conn_t *)arg; 17811 tcp_t *tcp = connp->conn_tcp; 17812 queue_t *q = tcp->tcp_wq; 17813 17814 ASSERT(DB_TYPE(mp) != M_IOCTL); 17815 /* 17816 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17817 * Once the close starts, streamhead and sockfs will not let any data 17818 * packets come down (close ensures that there are no threads using the 17819 * queue and no new threads will come down) but since qprocsoff() 17820 * hasn't happened yet, a M_FLUSH or some non data message might 17821 * get reflected back (in response to our own FLUSHRW) and get 17822 * processed after tcp_close() is done. The conn would still be valid 17823 * because a ref would have added but we need to check the state 17824 * before actually processing the packet. 17825 */ 17826 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17827 freemsg(mp); 17828 return; 17829 } 17830 17831 switch (DB_TYPE(mp)) { 17832 case M_IOCDATA: 17833 tcp_wput_iocdata(tcp, mp); 17834 break; 17835 case M_FLUSH: 17836 tcp_wput_flush(tcp, mp); 17837 break; 17838 default: 17839 CALL_IP_WPUT(connp, q, mp); 17840 break; 17841 } 17842 } 17843 17844 /* 17845 * The TCP fast path write put procedure. 17846 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17847 */ 17848 /* ARGSUSED */ 17849 void 17850 tcp_output(void *arg, mblk_t *mp, void *arg2) 17851 { 17852 int len; 17853 int hdrlen; 17854 int plen; 17855 mblk_t *mp1; 17856 uchar_t *rptr; 17857 uint32_t snxt; 17858 tcph_t *tcph; 17859 struct datab *db; 17860 uint32_t suna; 17861 uint32_t mss; 17862 ipaddr_t *dst; 17863 ipaddr_t *src; 17864 uint32_t sum; 17865 int usable; 17866 conn_t *connp = (conn_t *)arg; 17867 tcp_t *tcp = connp->conn_tcp; 17868 uint32_t msize; 17869 tcp_stack_t *tcps = tcp->tcp_tcps; 17870 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17871 17872 /* 17873 * Try and ASSERT the minimum possible references on the 17874 * conn early enough. Since we are executing on write side, 17875 * the connection is obviously not detached and that means 17876 * there is a ref each for TCP and IP. Since we are behind 17877 * the squeue, the minimum references needed are 3. If the 17878 * conn is in classifier hash list, there should be an 17879 * extra ref for that (we check both the possibilities). 17880 */ 17881 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17882 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17883 17884 ASSERT(DB_TYPE(mp) == M_DATA); 17885 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17886 17887 mutex_enter(&tcp->tcp_non_sq_lock); 17888 tcp->tcp_squeue_bytes -= msize; 17889 mutex_exit(&tcp->tcp_non_sq_lock); 17890 17891 /* Check to see if this connection wants to be re-fused. */ 17892 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17893 if (tcp->tcp_ipversion == IPV4_VERSION) { 17894 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17895 &tcp->tcp_saved_tcph); 17896 } else { 17897 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17898 &tcp->tcp_saved_tcph); 17899 } 17900 } 17901 /* Bypass tcp protocol for fused tcp loopback */ 17902 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17903 return; 17904 17905 mss = tcp->tcp_mss; 17906 if (tcp->tcp_xmit_zc_clean) 17907 mp = tcp_zcopy_backoff(tcp, mp, 0); 17908 17909 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17910 len = (int)(mp->b_wptr - mp->b_rptr); 17911 17912 /* 17913 * Criteria for fast path: 17914 * 17915 * 1. no unsent data 17916 * 2. single mblk in request 17917 * 3. connection established 17918 * 4. data in mblk 17919 * 5. len <= mss 17920 * 6. no tcp_valid bits 17921 */ 17922 if ((tcp->tcp_unsent != 0) || 17923 (tcp->tcp_cork) || 17924 (mp->b_cont != NULL) || 17925 (tcp->tcp_state != TCPS_ESTABLISHED) || 17926 (len == 0) || 17927 (len > mss) || 17928 (tcp->tcp_valid_bits != 0)) { 17929 tcp_wput_data(tcp, mp, B_FALSE); 17930 return; 17931 } 17932 17933 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17934 ASSERT(tcp->tcp_fin_sent == 0); 17935 17936 /* queue new packet onto retransmission queue */ 17937 if (tcp->tcp_xmit_head == NULL) { 17938 tcp->tcp_xmit_head = mp; 17939 } else { 17940 tcp->tcp_xmit_last->b_cont = mp; 17941 } 17942 tcp->tcp_xmit_last = mp; 17943 tcp->tcp_xmit_tail = mp; 17944 17945 /* find out how much we can send */ 17946 /* BEGIN CSTYLED */ 17947 /* 17948 * un-acked usable 17949 * |--------------|-----------------| 17950 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17951 */ 17952 /* END CSTYLED */ 17953 17954 /* start sending from tcp_snxt */ 17955 snxt = tcp->tcp_snxt; 17956 17957 /* 17958 * Check to see if this connection has been idled for some 17959 * time and no ACK is expected. If it is, we need to slow 17960 * start again to get back the connection's "self-clock" as 17961 * described in VJ's paper. 17962 * 17963 * Refer to the comment in tcp_mss_set() for the calculation 17964 * of tcp_cwnd after idle. 17965 */ 17966 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17967 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17968 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17969 } 17970 17971 usable = tcp->tcp_swnd; /* tcp window size */ 17972 if (usable > tcp->tcp_cwnd) 17973 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17974 usable -= snxt; /* subtract stuff already sent */ 17975 suna = tcp->tcp_suna; 17976 usable += suna; 17977 /* usable can be < 0 if the congestion window is smaller */ 17978 if (len > usable) { 17979 /* Can't send complete M_DATA in one shot */ 17980 goto slow; 17981 } 17982 17983 mutex_enter(&tcp->tcp_non_sq_lock); 17984 if (tcp->tcp_flow_stopped && 17985 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17986 tcp_clrqfull(tcp); 17987 } 17988 mutex_exit(&tcp->tcp_non_sq_lock); 17989 17990 /* 17991 * determine if anything to send (Nagle). 17992 * 17993 * 1. len < tcp_mss (i.e. small) 17994 * 2. unacknowledged data present 17995 * 3. len < nagle limit 17996 * 4. last packet sent < nagle limit (previous packet sent) 17997 */ 17998 if ((len < mss) && (snxt != suna) && 17999 (len < (int)tcp->tcp_naglim) && 18000 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 18001 /* 18002 * This was the first unsent packet and normally 18003 * mss < xmit_hiwater so there is no need to worry 18004 * about flow control. The next packet will go 18005 * through the flow control check in tcp_wput_data(). 18006 */ 18007 /* leftover work from above */ 18008 tcp->tcp_unsent = len; 18009 tcp->tcp_xmit_tail_unsent = len; 18010 18011 return; 18012 } 18013 18014 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 18015 18016 if (snxt == suna) { 18017 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18018 } 18019 18020 /* we have always sent something */ 18021 tcp->tcp_rack_cnt = 0; 18022 18023 tcp->tcp_snxt = snxt + len; 18024 tcp->tcp_rack = tcp->tcp_rnxt; 18025 18026 if ((mp1 = dupb(mp)) == 0) 18027 goto no_memory; 18028 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 18029 mp->b_next = (mblk_t *)(uintptr_t)snxt; 18030 18031 /* adjust tcp header information */ 18032 tcph = tcp->tcp_tcph; 18033 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 18034 18035 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 18036 sum = (sum >> 16) + (sum & 0xFFFF); 18037 U16_TO_ABE16(sum, tcph->th_sum); 18038 18039 U32_TO_ABE32(snxt, tcph->th_seq); 18040 18041 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 18042 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 18043 BUMP_LOCAL(tcp->tcp_obsegs); 18044 18045 /* Update the latest receive window size in TCP header. */ 18046 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18047 tcph->th_win); 18048 18049 tcp->tcp_last_sent_len = (ushort_t)len; 18050 18051 plen = len + tcp->tcp_hdr_len; 18052 18053 if (tcp->tcp_ipversion == IPV4_VERSION) { 18054 tcp->tcp_ipha->ipha_length = htons(plen); 18055 } else { 18056 tcp->tcp_ip6h->ip6_plen = htons(plen - 18057 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 18058 } 18059 18060 /* see if we need to allocate a mblk for the headers */ 18061 hdrlen = tcp->tcp_hdr_len; 18062 rptr = mp1->b_rptr - hdrlen; 18063 db = mp1->b_datap; 18064 if ((db->db_ref != 2) || rptr < db->db_base || 18065 (!OK_32PTR(rptr))) { 18066 /* NOTE: we assume allocb returns an OK_32PTR */ 18067 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 18068 tcps->tcps_wroff_xtra, BPRI_MED); 18069 if (!mp) { 18070 freemsg(mp1); 18071 goto no_memory; 18072 } 18073 mp->b_cont = mp1; 18074 mp1 = mp; 18075 /* Leave room for Link Level header */ 18076 /* hdrlen = tcp->tcp_hdr_len; */ 18077 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 18078 mp1->b_wptr = &rptr[hdrlen]; 18079 } 18080 mp1->b_rptr = rptr; 18081 18082 /* Fill in the timestamp option. */ 18083 if (tcp->tcp_snd_ts_ok) { 18084 U32_TO_BE32((uint32_t)lbolt, 18085 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 18086 U32_TO_BE32(tcp->tcp_ts_recent, 18087 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 18088 } else { 18089 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18090 } 18091 18092 /* copy header into outgoing packet */ 18093 dst = (ipaddr_t *)rptr; 18094 src = (ipaddr_t *)tcp->tcp_iphc; 18095 dst[0] = src[0]; 18096 dst[1] = src[1]; 18097 dst[2] = src[2]; 18098 dst[3] = src[3]; 18099 dst[4] = src[4]; 18100 dst[5] = src[5]; 18101 dst[6] = src[6]; 18102 dst[7] = src[7]; 18103 dst[8] = src[8]; 18104 dst[9] = src[9]; 18105 if (hdrlen -= 40) { 18106 hdrlen >>= 2; 18107 dst += 10; 18108 src += 10; 18109 do { 18110 *dst++ = *src++; 18111 } while (--hdrlen); 18112 } 18113 18114 /* 18115 * Set the ECN info in the TCP header. Note that this 18116 * is not the template header. 18117 */ 18118 if (tcp->tcp_ecn_ok) { 18119 SET_ECT(tcp, rptr); 18120 18121 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18122 if (tcp->tcp_ecn_echo_on) 18123 tcph->th_flags[0] |= TH_ECE; 18124 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18125 tcph->th_flags[0] |= TH_CWR; 18126 tcp->tcp_ecn_cwr_sent = B_TRUE; 18127 } 18128 } 18129 18130 if (tcp->tcp_ip_forward_progress) { 18131 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 18132 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 18133 tcp->tcp_ip_forward_progress = B_FALSE; 18134 } 18135 tcp_send_data(tcp, tcp->tcp_wq, mp1); 18136 return; 18137 18138 /* 18139 * If we ran out of memory, we pretend to have sent the packet 18140 * and that it was lost on the wire. 18141 */ 18142 no_memory: 18143 return; 18144 18145 slow: 18146 /* leftover work from above */ 18147 tcp->tcp_unsent = len; 18148 tcp->tcp_xmit_tail_unsent = len; 18149 tcp_wput_data(tcp, NULL, B_FALSE); 18150 } 18151 18152 /* 18153 * The function called through squeue to get behind eager's perimeter to 18154 * finish the accept processing. 18155 */ 18156 /* ARGSUSED */ 18157 void 18158 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 18159 { 18160 conn_t *connp = (conn_t *)arg; 18161 tcp_t *tcp = connp->conn_tcp; 18162 queue_t *q = tcp->tcp_rq; 18163 mblk_t *mp1; 18164 mblk_t *stropt_mp = mp; 18165 struct stroptions *stropt; 18166 uint_t thwin; 18167 tcp_stack_t *tcps = tcp->tcp_tcps; 18168 18169 /* 18170 * Drop the eager's ref on the listener, that was placed when 18171 * this eager began life in tcp_conn_request. 18172 */ 18173 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 18174 18175 tcp->tcp_detached = B_FALSE; 18176 18177 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 18178 /* 18179 * Someone blewoff the eager before we could finish 18180 * the accept. 18181 * 18182 * The only reason eager exists it because we put in 18183 * a ref on it when conn ind went up. We need to send 18184 * a disconnect indication up while the last reference 18185 * on the eager will be dropped by the squeue when we 18186 * return. 18187 */ 18188 ASSERT(tcp->tcp_listener == NULL); 18189 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 18190 struct T_discon_ind *tdi; 18191 18192 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 18193 /* 18194 * Let us reuse the incoming mblk to avoid memory 18195 * allocation failure problems. We know that the 18196 * size of the incoming mblk i.e. stroptions is greater 18197 * than sizeof T_discon_ind. So the reallocb below 18198 * can't fail. 18199 */ 18200 freemsg(mp->b_cont); 18201 mp->b_cont = NULL; 18202 ASSERT(DB_REF(mp) == 1); 18203 mp = reallocb(mp, sizeof (struct T_discon_ind), 18204 B_FALSE); 18205 ASSERT(mp != NULL); 18206 DB_TYPE(mp) = M_PROTO; 18207 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 18208 tdi = (struct T_discon_ind *)mp->b_rptr; 18209 if (tcp->tcp_issocket) { 18210 tdi->DISCON_reason = ECONNREFUSED; 18211 tdi->SEQ_number = 0; 18212 } else { 18213 tdi->DISCON_reason = ENOPROTOOPT; 18214 tdi->SEQ_number = 18215 tcp->tcp_conn_req_seqnum; 18216 } 18217 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 18218 putnext(q, mp); 18219 } else { 18220 freemsg(mp); 18221 } 18222 if (tcp->tcp_hard_binding) { 18223 tcp->tcp_hard_binding = B_FALSE; 18224 tcp->tcp_hard_bound = B_TRUE; 18225 } 18226 return; 18227 } 18228 18229 mp1 = stropt_mp->b_cont; 18230 stropt_mp->b_cont = NULL; 18231 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 18232 stropt = (struct stroptions *)stropt_mp->b_rptr; 18233 18234 while (mp1 != NULL) { 18235 mp = mp1; 18236 mp1 = mp1->b_cont; 18237 mp->b_cont = NULL; 18238 tcp->tcp_drop_opt_ack_cnt++; 18239 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 18240 } 18241 mp = NULL; 18242 18243 /* 18244 * For a loopback connection with tcp_direct_sockfs on, note that 18245 * we don't have to protect tcp_rcv_list yet because synchronous 18246 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18247 * possibly race with us. 18248 */ 18249 18250 /* 18251 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18252 * properly. This is the first time we know of the acceptor' 18253 * queue. So we do it here. 18254 */ 18255 if (tcp->tcp_rcv_list == NULL) { 18256 /* 18257 * Recv queue is empty, tcp_rwnd should not have changed. 18258 * That means it should be equal to the listener's tcp_rwnd. 18259 */ 18260 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18261 } else { 18262 #ifdef DEBUG 18263 uint_t cnt = 0; 18264 18265 mp1 = tcp->tcp_rcv_list; 18266 while ((mp = mp1) != NULL) { 18267 mp1 = mp->b_next; 18268 cnt += msgdsize(mp); 18269 } 18270 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18271 #endif 18272 /* There is some data, add them back to get the max. */ 18273 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18274 } 18275 /* 18276 * This is the first time we run on the correct 18277 * queue after tcp_accept. So fix all the q parameters 18278 * here. 18279 */ 18280 stropt->so_flags = SO_HIWAT | SO_MAXBLK | SO_WROFF; 18281 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18282 18283 /* 18284 * Record the stream head's high water mark for this endpoint; 18285 * this is used for flow-control purposes. 18286 */ 18287 stropt->so_hiwat = tcp->tcp_fused ? 18288 tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat) : 18289 MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 18290 18291 /* 18292 * Determine what write offset value to use depending on SACK and 18293 * whether the endpoint is fused or not. 18294 */ 18295 if (tcp->tcp_fused) { 18296 ASSERT(tcp->tcp_loopback); 18297 ASSERT(tcp->tcp_loopback_peer != NULL); 18298 /* 18299 * For fused tcp loopback, set the stream head's write 18300 * offset value to zero since we won't be needing any room 18301 * for TCP/IP headers. This would also improve performance 18302 * since it would reduce the amount of work done by kmem. 18303 * Non-fused tcp loopback case is handled separately below. 18304 */ 18305 stropt->so_wroff = 0; 18306 /* 18307 * Update the peer's transmit parameters according to 18308 * our recently calculated high water mark value. 18309 */ 18310 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18311 } else if (tcp->tcp_snd_sack_ok) { 18312 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18313 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18314 } else { 18315 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18316 tcps->tcps_wroff_xtra); 18317 } 18318 18319 /* 18320 * If this is endpoint is handling SSL, then reserve extra 18321 * offset and space at the end. 18322 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18323 * overriding the previous setting. The extra cost of signing and 18324 * encrypting multiple MSS-size records (12 of them with Ethernet), 18325 * instead of a single contiguous one by the stream head 18326 * largely outweighs the statistical reduction of ACKs, when 18327 * applicable. The peer will also save on decryption and verification 18328 * costs. 18329 */ 18330 if (tcp->tcp_kssl_ctx != NULL) { 18331 stropt->so_wroff += SSL3_WROFFSET; 18332 18333 stropt->so_flags |= SO_TAIL; 18334 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18335 18336 stropt->so_flags |= SO_COPYOPT; 18337 stropt->so_copyopt = ZCVMUNSAFE; 18338 18339 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18340 } 18341 18342 /* Send the options up */ 18343 putnext(q, stropt_mp); 18344 18345 /* 18346 * Pass up any data and/or a fin that has been received. 18347 * 18348 * Adjust receive window in case it had decreased 18349 * (because there is data <=> tcp_rcv_list != NULL) 18350 * while the connection was detached. Note that 18351 * in case the eager was flow-controlled, w/o this 18352 * code, the rwnd may never open up again! 18353 */ 18354 if (tcp->tcp_rcv_list != NULL) { 18355 /* We drain directly in case of fused tcp loopback */ 18356 sodirect_t *sodp; 18357 18358 if (!tcp->tcp_fused && canputnext(q)) { 18359 tcp->tcp_rwnd = q->q_hiwat; 18360 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18361 << tcp->tcp_rcv_ws; 18362 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18363 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18364 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18365 tcp_xmit_ctl(NULL, 18366 tcp, (tcp->tcp_swnd == 0) ? 18367 tcp->tcp_suna : tcp->tcp_snxt, 18368 tcp->tcp_rnxt, TH_ACK); 18369 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18370 } 18371 18372 } 18373 18374 SOD_PTR_ENTER(tcp, sodp); 18375 if (sodp != NULL) { 18376 /* Sodirect, move from rcv_list */ 18377 ASSERT(!tcp->tcp_fused); 18378 while ((mp = tcp->tcp_rcv_list) != NULL) { 18379 tcp->tcp_rcv_list = mp->b_next; 18380 mp->b_next = NULL; 18381 (void) tcp_rcv_sod_enqueue(tcp, sodp, mp, 18382 msgdsize(mp)); 18383 } 18384 tcp->tcp_rcv_last_head = NULL; 18385 tcp->tcp_rcv_last_tail = NULL; 18386 tcp->tcp_rcv_cnt = 0; 18387 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18388 /* sod_wakeup() did the mutex_exit() */ 18389 } else { 18390 /* Not sodirect, drain */ 18391 (void) tcp_rcv_drain(q, tcp); 18392 } 18393 18394 /* 18395 * For fused tcp loopback, back-enable peer endpoint 18396 * if it's currently flow-controlled. 18397 */ 18398 if (tcp->tcp_fused) { 18399 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18400 18401 ASSERT(peer_tcp != NULL); 18402 ASSERT(peer_tcp->tcp_fused); 18403 /* 18404 * In order to change the peer's tcp_flow_stopped, 18405 * we need to take locks for both end points. The 18406 * highest address is taken first. 18407 */ 18408 if (peer_tcp > tcp) { 18409 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18410 mutex_enter(&tcp->tcp_non_sq_lock); 18411 } else { 18412 mutex_enter(&tcp->tcp_non_sq_lock); 18413 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18414 } 18415 if (peer_tcp->tcp_flow_stopped) { 18416 tcp_clrqfull(peer_tcp); 18417 TCP_STAT(tcps, tcp_fusion_backenabled); 18418 } 18419 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18420 mutex_exit(&tcp->tcp_non_sq_lock); 18421 } 18422 } 18423 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18424 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18425 mp = tcp->tcp_ordrel_mp; 18426 tcp->tcp_ordrel_mp = NULL; 18427 tcp->tcp_ordrel_done = B_TRUE; 18428 putnext(q, mp); 18429 } 18430 if (tcp->tcp_hard_binding) { 18431 tcp->tcp_hard_binding = B_FALSE; 18432 tcp->tcp_hard_bound = B_TRUE; 18433 } 18434 18435 /* We can enable synchronous streams now */ 18436 if (tcp->tcp_fused) { 18437 tcp_fuse_syncstr_enable_pair(tcp); 18438 } 18439 18440 if (tcp->tcp_ka_enabled) { 18441 tcp->tcp_ka_last_intrvl = 0; 18442 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18443 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18444 } 18445 18446 /* 18447 * At this point, eager is fully established and will 18448 * have the following references - 18449 * 18450 * 2 references for connection to exist (1 for TCP and 1 for IP). 18451 * 1 reference for the squeue which will be dropped by the squeue as 18452 * soon as this function returns. 18453 * There will be 1 additonal reference for being in classifier 18454 * hash list provided something bad hasn't happened. 18455 */ 18456 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18457 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18458 } 18459 18460 /* 18461 * The function called through squeue to get behind listener's perimeter to 18462 * send a deffered conn_ind. 18463 */ 18464 /* ARGSUSED */ 18465 void 18466 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18467 { 18468 conn_t *connp = (conn_t *)arg; 18469 tcp_t *listener = connp->conn_tcp; 18470 18471 if (listener->tcp_state == TCPS_CLOSED || 18472 TCP_IS_DETACHED(listener)) { 18473 /* 18474 * If listener has closed, it would have caused a 18475 * a cleanup/blowoff to happen for the eager. 18476 */ 18477 tcp_t *tcp; 18478 struct T_conn_ind *conn_ind; 18479 18480 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18481 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18482 conn_ind->OPT_length); 18483 /* 18484 * We need to drop the ref on eager that was put 18485 * tcp_rput_data() before trying to send the conn_ind 18486 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18487 * and tcp_wput_accept() is sending this deferred conn_ind but 18488 * listener is closed so we drop the ref. 18489 */ 18490 CONN_DEC_REF(tcp->tcp_connp); 18491 freemsg(mp); 18492 return; 18493 } 18494 putnext(listener->tcp_rq, mp); 18495 } 18496 18497 18498 /* 18499 * This is the STREAMS entry point for T_CONN_RES coming down on 18500 * Acceptor STREAM when sockfs listener does accept processing. 18501 * Read the block comment on top of tcp_conn_request(). 18502 */ 18503 void 18504 tcp_wput_accept(queue_t *q, mblk_t *mp) 18505 { 18506 queue_t *rq = RD(q); 18507 struct T_conn_res *conn_res; 18508 tcp_t *eager; 18509 tcp_t *listener; 18510 struct T_ok_ack *ok; 18511 t_scalar_t PRIM_type; 18512 mblk_t *opt_mp; 18513 conn_t *econnp; 18514 18515 ASSERT(DB_TYPE(mp) == M_PROTO); 18516 18517 conn_res = (struct T_conn_res *)mp->b_rptr; 18518 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18519 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18520 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18521 if (mp != NULL) 18522 putnext(rq, mp); 18523 return; 18524 } 18525 switch (conn_res->PRIM_type) { 18526 case O_T_CONN_RES: 18527 case T_CONN_RES: 18528 /* 18529 * We pass up an err ack if allocb fails. This will 18530 * cause sockfs to issue a T_DISCON_REQ which will cause 18531 * tcp_eager_blowoff to be called. sockfs will then call 18532 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18533 * we need to do the allocb up here because we have to 18534 * make sure rq->q_qinfo->qi_qclose still points to the 18535 * correct function (tcpclose_accept) in case allocb 18536 * fails. 18537 */ 18538 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18539 if (opt_mp == NULL) { 18540 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18541 if (mp != NULL) 18542 putnext(rq, mp); 18543 return; 18544 } 18545 18546 bcopy(mp->b_rptr + conn_res->OPT_offset, 18547 &eager, conn_res->OPT_length); 18548 PRIM_type = conn_res->PRIM_type; 18549 mp->b_datap->db_type = M_PCPROTO; 18550 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18551 ok = (struct T_ok_ack *)mp->b_rptr; 18552 ok->PRIM_type = T_OK_ACK; 18553 ok->CORRECT_prim = PRIM_type; 18554 econnp = eager->tcp_connp; 18555 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18556 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18557 eager->tcp_rq = rq; 18558 eager->tcp_wq = q; 18559 rq->q_ptr = econnp; 18560 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18561 q->q_ptr = econnp; 18562 q->q_qinfo = &tcp_winit; 18563 listener = eager->tcp_listener; 18564 eager->tcp_issocket = B_TRUE; 18565 18566 /* 18567 * TCP is _D_SODIRECT and sockfs is directly above so 18568 * save shared sodirect_t pointer (if any). 18569 * 18570 * If tcp_fused and sodirect enabled disable it. 18571 */ 18572 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18573 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18574 /* Fused, disable sodirect */ 18575 mutex_enter(eager->tcp_sodirect->sod_lockp); 18576 SOD_DISABLE(eager->tcp_sodirect); 18577 mutex_exit(eager->tcp_sodirect->sod_lockp); 18578 eager->tcp_sodirect = NULL; 18579 } 18580 18581 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18582 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18583 ASSERT(econnp->conn_netstack == 18584 listener->tcp_connp->conn_netstack); 18585 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18586 18587 /* Put the ref for IP */ 18588 CONN_INC_REF(econnp); 18589 18590 /* 18591 * We should have minimum of 3 references on the conn 18592 * at this point. One each for TCP and IP and one for 18593 * the T_conn_ind that was sent up when the 3-way handshake 18594 * completed. In the normal case we would also have another 18595 * reference (making a total of 4) for the conn being in the 18596 * classifier hash list. However the eager could have received 18597 * an RST subsequently and tcp_closei_local could have removed 18598 * the eager from the classifier hash list, hence we can't 18599 * assert that reference. 18600 */ 18601 ASSERT(econnp->conn_ref >= 3); 18602 18603 /* 18604 * Send the new local address also up to sockfs. There 18605 * should already be enough space in the mp that came 18606 * down from soaccept(). 18607 */ 18608 if (eager->tcp_family == AF_INET) { 18609 sin_t *sin; 18610 18611 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18612 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18613 sin = (sin_t *)mp->b_wptr; 18614 mp->b_wptr += sizeof (sin_t); 18615 sin->sin_family = AF_INET; 18616 sin->sin_port = eager->tcp_lport; 18617 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18618 } else { 18619 sin6_t *sin6; 18620 18621 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18622 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18623 sin6 = (sin6_t *)mp->b_wptr; 18624 mp->b_wptr += sizeof (sin6_t); 18625 sin6->sin6_family = AF_INET6; 18626 sin6->sin6_port = eager->tcp_lport; 18627 if (eager->tcp_ipversion == IPV4_VERSION) { 18628 sin6->sin6_flowinfo = 0; 18629 IN6_IPADDR_TO_V4MAPPED( 18630 eager->tcp_ipha->ipha_src, 18631 &sin6->sin6_addr); 18632 } else { 18633 ASSERT(eager->tcp_ip6h != NULL); 18634 sin6->sin6_flowinfo = 18635 eager->tcp_ip6h->ip6_vcf & 18636 ~IPV6_VERS_AND_FLOW_MASK; 18637 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18638 } 18639 sin6->sin6_scope_id = 0; 18640 sin6->__sin6_src_id = 0; 18641 } 18642 18643 putnext(rq, mp); 18644 18645 opt_mp->b_datap->db_type = M_SETOPTS; 18646 opt_mp->b_wptr += sizeof (struct stroptions); 18647 18648 /* 18649 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18650 * from listener to acceptor. The message is chained on the 18651 * bind_mp which tcp_rput_other will send down to IP. 18652 */ 18653 if (listener->tcp_bound_if != 0) { 18654 /* allocate optmgmt req */ 18655 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18656 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18657 sizeof (int)); 18658 if (mp != NULL) 18659 linkb(opt_mp, mp); 18660 } 18661 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18662 uint_t on = 1; 18663 18664 /* allocate optmgmt req */ 18665 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18666 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18667 if (mp != NULL) 18668 linkb(opt_mp, mp); 18669 } 18670 18671 18672 mutex_enter(&listener->tcp_eager_lock); 18673 18674 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18675 18676 tcp_t *tail; 18677 tcp_t *tcp; 18678 mblk_t *mp1; 18679 18680 tcp = listener->tcp_eager_prev_q0; 18681 /* 18682 * listener->tcp_eager_prev_q0 points to the TAIL of the 18683 * deferred T_conn_ind queue. We need to get to the head 18684 * of the queue in order to send up T_conn_ind the same 18685 * order as how the 3WHS is completed. 18686 */ 18687 while (tcp != listener) { 18688 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18689 !tcp->tcp_kssl_pending) 18690 break; 18691 else 18692 tcp = tcp->tcp_eager_prev_q0; 18693 } 18694 /* None of the pending eagers can be sent up now */ 18695 if (tcp == listener) 18696 goto no_more_eagers; 18697 18698 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18699 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18700 /* Move from q0 to q */ 18701 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18702 listener->tcp_conn_req_cnt_q0--; 18703 listener->tcp_conn_req_cnt_q++; 18704 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18705 tcp->tcp_eager_prev_q0; 18706 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18707 tcp->tcp_eager_next_q0; 18708 tcp->tcp_eager_prev_q0 = NULL; 18709 tcp->tcp_eager_next_q0 = NULL; 18710 tcp->tcp_conn_def_q0 = B_FALSE; 18711 18712 /* Make sure the tcp isn't in the list of droppables */ 18713 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18714 tcp->tcp_eager_prev_drop_q0 == NULL); 18715 18716 /* 18717 * Insert at end of the queue because sockfs sends 18718 * down T_CONN_RES in chronological order. Leaving 18719 * the older conn indications at front of the queue 18720 * helps reducing search time. 18721 */ 18722 tail = listener->tcp_eager_last_q; 18723 if (tail != NULL) { 18724 tail->tcp_eager_next_q = tcp; 18725 } else { 18726 listener->tcp_eager_next_q = tcp; 18727 } 18728 listener->tcp_eager_last_q = tcp; 18729 tcp->tcp_eager_next_q = NULL; 18730 18731 /* Need to get inside the listener perimeter */ 18732 CONN_INC_REF(listener->tcp_connp); 18733 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18734 tcp_send_pending, listener->tcp_connp, 18735 SQTAG_TCP_SEND_PENDING); 18736 } 18737 no_more_eagers: 18738 tcp_eager_unlink(eager); 18739 mutex_exit(&listener->tcp_eager_lock); 18740 18741 /* 18742 * At this point, the eager is detached from the listener 18743 * but we still have an extra refs on eager (apart from the 18744 * usual tcp references). The ref was placed in tcp_rput_data 18745 * before sending the conn_ind in tcp_send_conn_ind. 18746 * The ref will be dropped in tcp_accept_finish(). 18747 */ 18748 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18749 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18750 return; 18751 default: 18752 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18753 if (mp != NULL) 18754 putnext(rq, mp); 18755 return; 18756 } 18757 } 18758 18759 static int 18760 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18761 { 18762 sin_t *sin = (sin_t *)sa; 18763 sin6_t *sin6 = (sin6_t *)sa; 18764 18765 switch (tcp->tcp_family) { 18766 case AF_INET: 18767 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18768 18769 if (*salenp < sizeof (sin_t)) 18770 return (EINVAL); 18771 18772 *sin = sin_null; 18773 sin->sin_family = AF_INET; 18774 sin->sin_port = tcp->tcp_lport; 18775 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18776 break; 18777 18778 case AF_INET6: 18779 if (*salenp < sizeof (sin6_t)) 18780 return (EINVAL); 18781 18782 *sin6 = sin6_null; 18783 sin6->sin6_family = AF_INET6; 18784 sin6->sin6_port = tcp->tcp_lport; 18785 if (tcp->tcp_ipversion == IPV4_VERSION) { 18786 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18787 &sin6->sin6_addr); 18788 } else { 18789 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18790 } 18791 break; 18792 } 18793 18794 return (0); 18795 } 18796 18797 static int 18798 tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18799 { 18800 sin_t *sin = (sin_t *)sa; 18801 sin6_t *sin6 = (sin6_t *)sa; 18802 18803 if (tcp->tcp_state < TCPS_SYN_RCVD) 18804 return (ENOTCONN); 18805 18806 switch (tcp->tcp_family) { 18807 case AF_INET: 18808 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18809 18810 if (*salenp < sizeof (sin_t)) 18811 return (EINVAL); 18812 18813 *sin = sin_null; 18814 sin->sin_family = AF_INET; 18815 sin->sin_port = tcp->tcp_fport; 18816 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18817 sin->sin_addr.s_addr); 18818 break; 18819 18820 case AF_INET6: 18821 if (*salenp < sizeof (sin6_t)) 18822 return (EINVAL); 18823 18824 *sin6 = sin6_null; 18825 sin6->sin6_family = AF_INET6; 18826 sin6->sin6_port = tcp->tcp_fport; 18827 sin6->sin6_addr = tcp->tcp_remote_v6; 18828 if (tcp->tcp_ipversion == IPV6_VERSION) { 18829 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18830 ~IPV6_VERS_AND_FLOW_MASK; 18831 } 18832 break; 18833 } 18834 18835 return (0); 18836 } 18837 18838 /* 18839 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18840 */ 18841 static void 18842 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18843 { 18844 void *data; 18845 mblk_t *datamp = mp->b_cont; 18846 tcp_t *tcp = Q_TO_TCP(q); 18847 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18848 18849 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18850 cmdp->cb_error = EPROTO; 18851 qreply(q, mp); 18852 return; 18853 } 18854 18855 data = datamp->b_rptr; 18856 18857 switch (cmdp->cb_cmd) { 18858 case TI_GETPEERNAME: 18859 cmdp->cb_error = tcp_getpeername(tcp, data, &cmdp->cb_len); 18860 break; 18861 case TI_GETMYNAME: 18862 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18863 break; 18864 default: 18865 cmdp->cb_error = EINVAL; 18866 break; 18867 } 18868 18869 qreply(q, mp); 18870 } 18871 18872 void 18873 tcp_wput(queue_t *q, mblk_t *mp) 18874 { 18875 conn_t *connp = Q_TO_CONN(q); 18876 tcp_t *tcp; 18877 void (*output_proc)(); 18878 t_scalar_t type; 18879 uchar_t *rptr; 18880 struct iocblk *iocp; 18881 uint32_t msize; 18882 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18883 18884 ASSERT(connp->conn_ref >= 2); 18885 18886 switch (DB_TYPE(mp)) { 18887 case M_DATA: 18888 tcp = connp->conn_tcp; 18889 ASSERT(tcp != NULL); 18890 18891 msize = msgdsize(mp); 18892 18893 mutex_enter(&tcp->tcp_non_sq_lock); 18894 tcp->tcp_squeue_bytes += msize; 18895 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18896 tcp_setqfull(tcp); 18897 } 18898 mutex_exit(&tcp->tcp_non_sq_lock); 18899 18900 CONN_INC_REF(connp); 18901 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18902 tcp_output, connp, SQTAG_TCP_OUTPUT); 18903 return; 18904 18905 case M_CMD: 18906 tcp_wput_cmdblk(q, mp); 18907 return; 18908 18909 case M_PROTO: 18910 case M_PCPROTO: 18911 /* 18912 * if it is a snmp message, don't get behind the squeue 18913 */ 18914 tcp = connp->conn_tcp; 18915 rptr = mp->b_rptr; 18916 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18917 type = ((union T_primitives *)rptr)->type; 18918 } else { 18919 if (tcp->tcp_debug) { 18920 (void) strlog(TCP_MOD_ID, 0, 1, 18921 SL_ERROR|SL_TRACE, 18922 "tcp_wput_proto, dropping one..."); 18923 } 18924 freemsg(mp); 18925 return; 18926 } 18927 if (type == T_SVR4_OPTMGMT_REQ) { 18928 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18929 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18930 cr)) { 18931 /* 18932 * This was a SNMP request 18933 */ 18934 return; 18935 } else { 18936 output_proc = tcp_wput_proto; 18937 } 18938 } else { 18939 output_proc = tcp_wput_proto; 18940 } 18941 break; 18942 case M_IOCTL: 18943 /* 18944 * Most ioctls can be processed right away without going via 18945 * squeues - process them right here. Those that do require 18946 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18947 * are processed by tcp_wput_ioctl(). 18948 */ 18949 iocp = (struct iocblk *)mp->b_rptr; 18950 tcp = connp->conn_tcp; 18951 18952 switch (iocp->ioc_cmd) { 18953 case TCP_IOC_ABORT_CONN: 18954 tcp_ioctl_abort_conn(q, mp); 18955 return; 18956 case TI_GETPEERNAME: 18957 case TI_GETMYNAME: 18958 mi_copyin(q, mp, NULL, 18959 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18960 return; 18961 case ND_SET: 18962 /* nd_getset does the necessary checks */ 18963 case ND_GET: 18964 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18965 CALL_IP_WPUT(connp, q, mp); 18966 return; 18967 } 18968 qreply(q, mp); 18969 return; 18970 case TCP_IOC_DEFAULT_Q: 18971 /* 18972 * Wants to be the default wq. Check the credentials 18973 * first, the rest is executed via squeue. 18974 */ 18975 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18976 iocp->ioc_error = EPERM; 18977 iocp->ioc_count = 0; 18978 mp->b_datap->db_type = M_IOCACK; 18979 qreply(q, mp); 18980 return; 18981 } 18982 output_proc = tcp_wput_ioctl; 18983 break; 18984 default: 18985 output_proc = tcp_wput_ioctl; 18986 break; 18987 } 18988 break; 18989 default: 18990 output_proc = tcp_wput_nondata; 18991 break; 18992 } 18993 18994 CONN_INC_REF(connp); 18995 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18996 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18997 } 18998 18999 /* 19000 * Initial STREAMS write side put() procedure for sockets. It tries to 19001 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 19002 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 19003 * are handled by tcp_wput() as usual. 19004 * 19005 * All further messages will also be handled by tcp_wput() because we cannot 19006 * be sure that the above short cut is safe later. 19007 */ 19008 static void 19009 tcp_wput_sock(queue_t *wq, mblk_t *mp) 19010 { 19011 conn_t *connp = Q_TO_CONN(wq); 19012 tcp_t *tcp = connp->conn_tcp; 19013 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 19014 19015 ASSERT(wq->q_qinfo == &tcp_sock_winit); 19016 wq->q_qinfo = &tcp_winit; 19017 19018 ASSERT(IPCL_IS_TCP(connp)); 19019 ASSERT(TCP_IS_SOCKET(tcp)); 19020 19021 if (DB_TYPE(mp) == M_PCPROTO && 19022 MBLKL(mp) == sizeof (struct T_capability_req) && 19023 car->PRIM_type == T_CAPABILITY_REQ) { 19024 tcp_capability_req(tcp, mp); 19025 return; 19026 } 19027 19028 tcp_wput(wq, mp); 19029 } 19030 19031 static boolean_t 19032 tcp_zcopy_check(tcp_t *tcp) 19033 { 19034 conn_t *connp = tcp->tcp_connp; 19035 ire_t *ire; 19036 boolean_t zc_enabled = B_FALSE; 19037 tcp_stack_t *tcps = tcp->tcp_tcps; 19038 19039 if (do_tcpzcopy == 2) 19040 zc_enabled = B_TRUE; 19041 else if (tcp->tcp_ipversion == IPV4_VERSION && 19042 IPCL_IS_CONNECTED(connp) && 19043 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 19044 connp->conn_dontroute == 0 && 19045 !connp->conn_nexthop_set && 19046 connp->conn_outgoing_ill == NULL && 19047 connp->conn_nofailover_ill == NULL && 19048 do_tcpzcopy == 1) { 19049 /* 19050 * the checks above closely resemble the fast path checks 19051 * in tcp_send_data(). 19052 */ 19053 mutex_enter(&connp->conn_lock); 19054 ire = connp->conn_ire_cache; 19055 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19056 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19057 IRE_REFHOLD(ire); 19058 if (ire->ire_stq != NULL) { 19059 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19060 19061 zc_enabled = ill && (ill->ill_capabilities & 19062 ILL_CAPAB_ZEROCOPY) && 19063 (ill->ill_zerocopy_capab-> 19064 ill_zerocopy_flags != 0); 19065 } 19066 IRE_REFRELE(ire); 19067 } 19068 mutex_exit(&connp->conn_lock); 19069 } 19070 tcp->tcp_snd_zcopy_on = zc_enabled; 19071 if (!TCP_IS_DETACHED(tcp)) { 19072 if (zc_enabled) { 19073 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 19074 TCP_STAT(tcps, tcp_zcopy_on); 19075 } else { 19076 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19077 TCP_STAT(tcps, tcp_zcopy_off); 19078 } 19079 } 19080 return (zc_enabled); 19081 } 19082 19083 static mblk_t * 19084 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19085 { 19086 tcp_stack_t *tcps = tcp->tcp_tcps; 19087 19088 if (do_tcpzcopy == 2) 19089 return (bp); 19090 else if (tcp->tcp_snd_zcopy_on) { 19091 tcp->tcp_snd_zcopy_on = B_FALSE; 19092 if (!TCP_IS_DETACHED(tcp)) { 19093 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19094 TCP_STAT(tcps, tcp_zcopy_disable); 19095 } 19096 } 19097 return (tcp_zcopy_backoff(tcp, bp, 0)); 19098 } 19099 19100 /* 19101 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19102 * the original desballoca'ed segmapped mblk. 19103 */ 19104 static mblk_t * 19105 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19106 { 19107 mblk_t *head, *tail, *nbp; 19108 tcp_stack_t *tcps = tcp->tcp_tcps; 19109 19110 if (IS_VMLOANED_MBLK(bp)) { 19111 TCP_STAT(tcps, tcp_zcopy_backoff); 19112 if ((head = copyb(bp)) == NULL) { 19113 /* fail to backoff; leave it for the next backoff */ 19114 tcp->tcp_xmit_zc_clean = B_FALSE; 19115 return (bp); 19116 } 19117 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19118 if (fix_xmitlist) 19119 tcp_zcopy_notify(tcp); 19120 else 19121 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19122 } 19123 nbp = bp->b_cont; 19124 if (fix_xmitlist) { 19125 head->b_prev = bp->b_prev; 19126 head->b_next = bp->b_next; 19127 if (tcp->tcp_xmit_tail == bp) 19128 tcp->tcp_xmit_tail = head; 19129 } 19130 bp->b_next = NULL; 19131 bp->b_prev = NULL; 19132 freeb(bp); 19133 } else { 19134 head = bp; 19135 nbp = bp->b_cont; 19136 } 19137 tail = head; 19138 while (nbp) { 19139 if (IS_VMLOANED_MBLK(nbp)) { 19140 TCP_STAT(tcps, tcp_zcopy_backoff); 19141 if ((tail->b_cont = copyb(nbp)) == NULL) { 19142 tcp->tcp_xmit_zc_clean = B_FALSE; 19143 tail->b_cont = nbp; 19144 return (head); 19145 } 19146 tail = tail->b_cont; 19147 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19148 if (fix_xmitlist) 19149 tcp_zcopy_notify(tcp); 19150 else 19151 tail->b_datap->db_struioflag |= 19152 STRUIO_ZCNOTIFY; 19153 } 19154 bp = nbp; 19155 nbp = nbp->b_cont; 19156 if (fix_xmitlist) { 19157 tail->b_prev = bp->b_prev; 19158 tail->b_next = bp->b_next; 19159 if (tcp->tcp_xmit_tail == bp) 19160 tcp->tcp_xmit_tail = tail; 19161 } 19162 bp->b_next = NULL; 19163 bp->b_prev = NULL; 19164 freeb(bp); 19165 } else { 19166 tail->b_cont = nbp; 19167 tail = nbp; 19168 nbp = nbp->b_cont; 19169 } 19170 } 19171 if (fix_xmitlist) { 19172 tcp->tcp_xmit_last = tail; 19173 tcp->tcp_xmit_zc_clean = B_TRUE; 19174 } 19175 return (head); 19176 } 19177 19178 static void 19179 tcp_zcopy_notify(tcp_t *tcp) 19180 { 19181 struct stdata *stp; 19182 19183 if (tcp->tcp_detached) 19184 return; 19185 stp = STREAM(tcp->tcp_rq); 19186 mutex_enter(&stp->sd_lock); 19187 stp->sd_flag |= STZCNOTIFY; 19188 cv_broadcast(&stp->sd_zcopy_wait); 19189 mutex_exit(&stp->sd_lock); 19190 } 19191 19192 static boolean_t 19193 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19194 { 19195 ire_t *ire; 19196 conn_t *connp = tcp->tcp_connp; 19197 tcp_stack_t *tcps = tcp->tcp_tcps; 19198 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19199 19200 mutex_enter(&connp->conn_lock); 19201 ire = connp->conn_ire_cache; 19202 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19203 19204 if ((ire != NULL) && 19205 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19206 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19207 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19208 IRE_REFHOLD(ire); 19209 mutex_exit(&connp->conn_lock); 19210 } else { 19211 boolean_t cached = B_FALSE; 19212 ts_label_t *tsl; 19213 19214 /* force a recheck later on */ 19215 tcp->tcp_ire_ill_check_done = B_FALSE; 19216 19217 TCP_DBGSTAT(tcps, tcp_ire_null1); 19218 connp->conn_ire_cache = NULL; 19219 mutex_exit(&connp->conn_lock); 19220 19221 if (ire != NULL) 19222 IRE_REFRELE_NOTR(ire); 19223 19224 tsl = crgetlabel(CONN_CRED(connp)); 19225 ire = (dst ? 19226 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19227 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19228 connp->conn_zoneid, tsl, ipst)); 19229 19230 if (ire == NULL) { 19231 TCP_STAT(tcps, tcp_ire_null); 19232 return (B_FALSE); 19233 } 19234 19235 IRE_REFHOLD_NOTR(ire); 19236 19237 mutex_enter(&connp->conn_lock); 19238 if (CONN_CACHE_IRE(connp)) { 19239 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19240 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19241 TCP_CHECK_IREINFO(tcp, ire); 19242 connp->conn_ire_cache = ire; 19243 cached = B_TRUE; 19244 } 19245 rw_exit(&ire->ire_bucket->irb_lock); 19246 } 19247 mutex_exit(&connp->conn_lock); 19248 19249 /* 19250 * We can continue to use the ire but since it was 19251 * not cached, we should drop the extra reference. 19252 */ 19253 if (!cached) 19254 IRE_REFRELE_NOTR(ire); 19255 19256 /* 19257 * Rampart note: no need to select a new label here, since 19258 * labels are not allowed to change during the life of a TCP 19259 * connection. 19260 */ 19261 } 19262 19263 *irep = ire; 19264 19265 return (B_TRUE); 19266 } 19267 19268 /* 19269 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19270 * 19271 * 0 = success; 19272 * 1 = failed to find ire and ill. 19273 */ 19274 static boolean_t 19275 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19276 { 19277 ipha_t *ipha; 19278 ipaddr_t dst; 19279 ire_t *ire; 19280 ill_t *ill; 19281 conn_t *connp = tcp->tcp_connp; 19282 mblk_t *ire_fp_mp; 19283 tcp_stack_t *tcps = tcp->tcp_tcps; 19284 19285 if (mp != NULL) 19286 ipha = (ipha_t *)mp->b_rptr; 19287 else 19288 ipha = tcp->tcp_ipha; 19289 dst = ipha->ipha_dst; 19290 19291 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19292 return (B_FALSE); 19293 19294 if ((ire->ire_flags & RTF_MULTIRT) || 19295 (ire->ire_stq == NULL) || 19296 (ire->ire_nce == NULL) || 19297 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19298 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19299 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19300 TCP_STAT(tcps, tcp_ip_ire_send); 19301 IRE_REFRELE(ire); 19302 return (B_FALSE); 19303 } 19304 19305 ill = ire_to_ill(ire); 19306 if (connp->conn_outgoing_ill != NULL) { 19307 ill_t *conn_outgoing_ill = NULL; 19308 /* 19309 * Choose a good ill in the group to send the packets on. 19310 */ 19311 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19312 ill = ire_to_ill(ire); 19313 } 19314 ASSERT(ill != NULL); 19315 19316 if (!tcp->tcp_ire_ill_check_done) { 19317 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19318 tcp->tcp_ire_ill_check_done = B_TRUE; 19319 } 19320 19321 *irep = ire; 19322 *illp = ill; 19323 19324 return (B_TRUE); 19325 } 19326 19327 static void 19328 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19329 { 19330 ipha_t *ipha; 19331 ipaddr_t src; 19332 ipaddr_t dst; 19333 uint32_t cksum; 19334 ire_t *ire; 19335 uint16_t *up; 19336 ill_t *ill; 19337 conn_t *connp = tcp->tcp_connp; 19338 uint32_t hcksum_txflags = 0; 19339 mblk_t *ire_fp_mp; 19340 uint_t ire_fp_mp_len; 19341 tcp_stack_t *tcps = tcp->tcp_tcps; 19342 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19343 19344 ASSERT(DB_TYPE(mp) == M_DATA); 19345 19346 if (DB_CRED(mp) == NULL) 19347 mblk_setcred(mp, CONN_CRED(connp)); 19348 19349 ipha = (ipha_t *)mp->b_rptr; 19350 src = ipha->ipha_src; 19351 dst = ipha->ipha_dst; 19352 19353 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19354 19355 /* 19356 * Drop off fast path for IPv6 and also if options are present or 19357 * we need to resolve a TS label. 19358 */ 19359 if (tcp->tcp_ipversion != IPV4_VERSION || 19360 !IPCL_IS_CONNECTED(connp) || 19361 !CONN_IS_LSO_MD_FASTPATH(connp) || 19362 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19363 !connp->conn_ulp_labeled || 19364 ipha->ipha_ident == IP_HDR_INCLUDED || 19365 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19366 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19367 if (tcp->tcp_snd_zcopy_aware) 19368 mp = tcp_zcopy_disable(tcp, mp); 19369 TCP_STAT(tcps, tcp_ip_send); 19370 CALL_IP_WPUT(connp, q, mp); 19371 return; 19372 } 19373 19374 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19375 if (tcp->tcp_snd_zcopy_aware) 19376 mp = tcp_zcopy_backoff(tcp, mp, 0); 19377 CALL_IP_WPUT(connp, q, mp); 19378 return; 19379 } 19380 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19381 ire_fp_mp_len = MBLKL(ire_fp_mp); 19382 19383 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19384 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19385 #ifndef _BIG_ENDIAN 19386 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19387 #endif 19388 19389 /* 19390 * Check to see if we need to re-enable LSO/MDT for this connection 19391 * because it was previously disabled due to changes in the ill; 19392 * note that by doing it here, this re-enabling only applies when 19393 * the packet is not dispatched through CALL_IP_WPUT(). 19394 * 19395 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19396 * case, since that's how we ended up here. For IPv6, we do the 19397 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19398 */ 19399 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19400 /* 19401 * Restore LSO for this connection, so that next time around 19402 * it is eligible to go through tcp_lsosend() path again. 19403 */ 19404 TCP_STAT(tcps, tcp_lso_enabled); 19405 tcp->tcp_lso = B_TRUE; 19406 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19407 "interface %s\n", (void *)connp, ill->ill_name)); 19408 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19409 /* 19410 * Restore MDT for this connection, so that next time around 19411 * it is eligible to go through tcp_multisend() path again. 19412 */ 19413 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19414 tcp->tcp_mdt = B_TRUE; 19415 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19416 "interface %s\n", (void *)connp, ill->ill_name)); 19417 } 19418 19419 if (tcp->tcp_snd_zcopy_aware) { 19420 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19421 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19422 mp = tcp_zcopy_disable(tcp, mp); 19423 /* 19424 * we shouldn't need to reset ipha as the mp containing 19425 * ipha should never be a zero-copy mp. 19426 */ 19427 } 19428 19429 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19430 ASSERT(ill->ill_hcksum_capab != NULL); 19431 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19432 } 19433 19434 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19435 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19436 19437 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19438 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19439 19440 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19441 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19442 19443 /* Software checksum? */ 19444 if (DB_CKSUMFLAGS(mp) == 0) { 19445 TCP_STAT(tcps, tcp_out_sw_cksum); 19446 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19447 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19448 } 19449 19450 ipha->ipha_fragment_offset_and_flags |= 19451 (uint32_t)htons(ire->ire_frag_flag); 19452 19453 /* Calculate IP header checksum if hardware isn't capable */ 19454 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19455 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19456 ((uint16_t *)ipha)[4]); 19457 } 19458 19459 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19460 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19461 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19462 19463 UPDATE_OB_PKT_COUNT(ire); 19464 ire->ire_last_used_time = lbolt; 19465 19466 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19467 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19468 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19469 ntohs(ipha->ipha_length)); 19470 19471 if (ILL_DLS_CAPABLE(ill)) { 19472 /* 19473 * Send the packet directly to DLD, where it may be queued 19474 * depending on the availability of transmit resources at 19475 * the media layer. 19476 */ 19477 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 19478 } else { 19479 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19480 DTRACE_PROBE4(ip4__physical__out__start, 19481 ill_t *, NULL, ill_t *, out_ill, 19482 ipha_t *, ipha, mblk_t *, mp); 19483 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19484 ipst->ips_ipv4firewall_physical_out, 19485 NULL, out_ill, ipha, mp, mp, 0, ipst); 19486 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19487 19488 if (mp != NULL) { 19489 if (ipst->ips_ipobs_enabled) { 19490 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, 19491 IP_REAL_ZONEID(connp->conn_zoneid, ipst), 19492 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, 19493 ipst); 19494 } 19495 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 19496 putnext(ire->ire_stq, mp); 19497 } 19498 } 19499 IRE_REFRELE(ire); 19500 } 19501 19502 /* 19503 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19504 * if the receiver shrinks the window, i.e. moves the right window to the 19505 * left, the we should not send new data, but should retransmit normally the 19506 * old unacked data between suna and suna + swnd. We might has sent data 19507 * that is now outside the new window, pretend that we didn't send it. 19508 */ 19509 static void 19510 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19511 { 19512 uint32_t snxt = tcp->tcp_snxt; 19513 mblk_t *xmit_tail; 19514 int32_t offset; 19515 19516 ASSERT(shrunk_count > 0); 19517 19518 /* Pretend we didn't send the data outside the window */ 19519 snxt -= shrunk_count; 19520 19521 /* Get the mblk and the offset in it per the shrunk window */ 19522 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19523 19524 ASSERT(xmit_tail != NULL); 19525 19526 /* Reset all the values per the now shrunk window */ 19527 tcp->tcp_snxt = snxt; 19528 tcp->tcp_xmit_tail = xmit_tail; 19529 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19530 offset; 19531 tcp->tcp_unsent += shrunk_count; 19532 19533 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19534 /* 19535 * Make sure the timer is running so that we will probe a zero 19536 * window. 19537 */ 19538 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19539 } 19540 19541 19542 /* 19543 * The TCP normal data output path. 19544 * NOTE: the logic of the fast path is duplicated from this function. 19545 */ 19546 static void 19547 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19548 { 19549 int len; 19550 mblk_t *local_time; 19551 mblk_t *mp1; 19552 uint32_t snxt; 19553 int tail_unsent; 19554 int tcpstate; 19555 int usable = 0; 19556 mblk_t *xmit_tail; 19557 queue_t *q = tcp->tcp_wq; 19558 int32_t mss; 19559 int32_t num_sack_blk = 0; 19560 int32_t tcp_hdr_len; 19561 int32_t tcp_tcp_hdr_len; 19562 int mdt_thres; 19563 int rc; 19564 tcp_stack_t *tcps = tcp->tcp_tcps; 19565 ip_stack_t *ipst; 19566 19567 tcpstate = tcp->tcp_state; 19568 if (mp == NULL) { 19569 /* 19570 * tcp_wput_data() with NULL mp should only be called when 19571 * there is unsent data. 19572 */ 19573 ASSERT(tcp->tcp_unsent > 0); 19574 /* Really tacky... but we need this for detached closes. */ 19575 len = tcp->tcp_unsent; 19576 goto data_null; 19577 } 19578 19579 #if CCS_STATS 19580 wrw_stats.tot.count++; 19581 wrw_stats.tot.bytes += msgdsize(mp); 19582 #endif 19583 ASSERT(mp->b_datap->db_type == M_DATA); 19584 /* 19585 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19586 * or before a connection attempt has begun. 19587 */ 19588 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19589 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19590 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19591 #ifdef DEBUG 19592 cmn_err(CE_WARN, 19593 "tcp_wput_data: data after ordrel, %s", 19594 tcp_display(tcp, NULL, 19595 DISP_ADDR_AND_PORT)); 19596 #else 19597 if (tcp->tcp_debug) { 19598 (void) strlog(TCP_MOD_ID, 0, 1, 19599 SL_TRACE|SL_ERROR, 19600 "tcp_wput_data: data after ordrel, %s\n", 19601 tcp_display(tcp, NULL, 19602 DISP_ADDR_AND_PORT)); 19603 } 19604 #endif /* DEBUG */ 19605 } 19606 if (tcp->tcp_snd_zcopy_aware && 19607 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19608 tcp_zcopy_notify(tcp); 19609 freemsg(mp); 19610 mutex_enter(&tcp->tcp_non_sq_lock); 19611 if (tcp->tcp_flow_stopped && 19612 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19613 tcp_clrqfull(tcp); 19614 } 19615 mutex_exit(&tcp->tcp_non_sq_lock); 19616 return; 19617 } 19618 19619 /* Strip empties */ 19620 for (;;) { 19621 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19622 (uintptr_t)INT_MAX); 19623 len = (int)(mp->b_wptr - mp->b_rptr); 19624 if (len > 0) 19625 break; 19626 mp1 = mp; 19627 mp = mp->b_cont; 19628 freeb(mp1); 19629 if (!mp) { 19630 return; 19631 } 19632 } 19633 19634 /* If we are the first on the list ... */ 19635 if (tcp->tcp_xmit_head == NULL) { 19636 tcp->tcp_xmit_head = mp; 19637 tcp->tcp_xmit_tail = mp; 19638 tcp->tcp_xmit_tail_unsent = len; 19639 } else { 19640 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19641 struct datab *dp; 19642 19643 mp1 = tcp->tcp_xmit_last; 19644 if (len < tcp_tx_pull_len && 19645 (dp = mp1->b_datap)->db_ref == 1 && 19646 dp->db_lim - mp1->b_wptr >= len) { 19647 ASSERT(len > 0); 19648 ASSERT(!mp1->b_cont); 19649 if (len == 1) { 19650 *mp1->b_wptr++ = *mp->b_rptr; 19651 } else { 19652 bcopy(mp->b_rptr, mp1->b_wptr, len); 19653 mp1->b_wptr += len; 19654 } 19655 if (mp1 == tcp->tcp_xmit_tail) 19656 tcp->tcp_xmit_tail_unsent += len; 19657 mp1->b_cont = mp->b_cont; 19658 if (tcp->tcp_snd_zcopy_aware && 19659 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19660 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19661 freeb(mp); 19662 mp = mp1; 19663 } else { 19664 tcp->tcp_xmit_last->b_cont = mp; 19665 } 19666 len += tcp->tcp_unsent; 19667 } 19668 19669 /* Tack on however many more positive length mblks we have */ 19670 if ((mp1 = mp->b_cont) != NULL) { 19671 do { 19672 int tlen; 19673 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19674 (uintptr_t)INT_MAX); 19675 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19676 if (tlen <= 0) { 19677 mp->b_cont = mp1->b_cont; 19678 freeb(mp1); 19679 } else { 19680 len += tlen; 19681 mp = mp1; 19682 } 19683 } while ((mp1 = mp->b_cont) != NULL); 19684 } 19685 tcp->tcp_xmit_last = mp; 19686 tcp->tcp_unsent = len; 19687 19688 if (urgent) 19689 usable = 1; 19690 19691 data_null: 19692 snxt = tcp->tcp_snxt; 19693 xmit_tail = tcp->tcp_xmit_tail; 19694 tail_unsent = tcp->tcp_xmit_tail_unsent; 19695 19696 /* 19697 * Note that tcp_mss has been adjusted to take into account the 19698 * timestamp option if applicable. Because SACK options do not 19699 * appear in every TCP segments and they are of variable lengths, 19700 * they cannot be included in tcp_mss. Thus we need to calculate 19701 * the actual segment length when we need to send a segment which 19702 * includes SACK options. 19703 */ 19704 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19705 int32_t opt_len; 19706 19707 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19708 tcp->tcp_num_sack_blk); 19709 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19710 2 + TCPOPT_HEADER_LEN; 19711 mss = tcp->tcp_mss - opt_len; 19712 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19713 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19714 } else { 19715 mss = tcp->tcp_mss; 19716 tcp_hdr_len = tcp->tcp_hdr_len; 19717 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19718 } 19719 19720 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19721 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19722 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19723 } 19724 if (tcpstate == TCPS_SYN_RCVD) { 19725 /* 19726 * The three-way connection establishment handshake is not 19727 * complete yet. We want to queue the data for transmission 19728 * after entering ESTABLISHED state (RFC793). A jump to 19729 * "done" label effectively leaves data on the queue. 19730 */ 19731 goto done; 19732 } else { 19733 int usable_r; 19734 19735 /* 19736 * In the special case when cwnd is zero, which can only 19737 * happen if the connection is ECN capable, return now. 19738 * New segments is sent using tcp_timer(). The timer 19739 * is set in tcp_rput_data(). 19740 */ 19741 if (tcp->tcp_cwnd == 0) { 19742 /* 19743 * Note that tcp_cwnd is 0 before 3-way handshake is 19744 * finished. 19745 */ 19746 ASSERT(tcp->tcp_ecn_ok || 19747 tcp->tcp_state < TCPS_ESTABLISHED); 19748 return; 19749 } 19750 19751 /* NOTE: trouble if xmitting while SYN not acked? */ 19752 usable_r = snxt - tcp->tcp_suna; 19753 usable_r = tcp->tcp_swnd - usable_r; 19754 19755 /* 19756 * Check if the receiver has shrunk the window. If 19757 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19758 * cannot be set as there is unsent data, so FIN cannot 19759 * be sent out. Otherwise, we need to take into account 19760 * of FIN as it consumes an "invisible" sequence number. 19761 */ 19762 ASSERT(tcp->tcp_fin_sent == 0); 19763 if (usable_r < 0) { 19764 /* 19765 * The receiver has shrunk the window and we have sent 19766 * -usable_r date beyond the window, re-adjust. 19767 * 19768 * If TCP window scaling is enabled, there can be 19769 * round down error as the advertised receive window 19770 * is actually right shifted n bits. This means that 19771 * the lower n bits info is wiped out. It will look 19772 * like the window is shrunk. Do a check here to 19773 * see if the shrunk amount is actually within the 19774 * error in window calculation. If it is, just 19775 * return. Note that this check is inside the 19776 * shrunk window check. This makes sure that even 19777 * though tcp_process_shrunk_swnd() is not called, 19778 * we will stop further processing. 19779 */ 19780 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19781 tcp_process_shrunk_swnd(tcp, -usable_r); 19782 } 19783 return; 19784 } 19785 19786 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19787 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19788 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19789 19790 /* usable = MIN(usable, unsent) */ 19791 if (usable_r > len) 19792 usable_r = len; 19793 19794 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19795 if (usable_r > 0) { 19796 usable = usable_r; 19797 } else { 19798 /* Bypass all other unnecessary processing. */ 19799 goto done; 19800 } 19801 } 19802 19803 local_time = (mblk_t *)lbolt; 19804 19805 /* 19806 * "Our" Nagle Algorithm. This is not the same as in the old 19807 * BSD. This is more in line with the true intent of Nagle. 19808 * 19809 * The conditions are: 19810 * 1. The amount of unsent data (or amount of data which can be 19811 * sent, whichever is smaller) is less than Nagle limit. 19812 * 2. The last sent size is also less than Nagle limit. 19813 * 3. There is unack'ed data. 19814 * 4. Urgent pointer is not set. Send urgent data ignoring the 19815 * Nagle algorithm. This reduces the probability that urgent 19816 * bytes get "merged" together. 19817 * 5. The app has not closed the connection. This eliminates the 19818 * wait time of the receiving side waiting for the last piece of 19819 * (small) data. 19820 * 19821 * If all are satisified, exit without sending anything. Note 19822 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19823 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19824 * 4095). 19825 */ 19826 if (usable < (int)tcp->tcp_naglim && 19827 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19828 snxt != tcp->tcp_suna && 19829 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19830 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19831 goto done; 19832 } 19833 19834 if (tcp->tcp_cork) { 19835 /* 19836 * if the tcp->tcp_cork option is set, then we have to force 19837 * TCP not to send partial segment (smaller than MSS bytes). 19838 * We are calculating the usable now based on full mss and 19839 * will save the rest of remaining data for later. 19840 */ 19841 if (usable < mss) 19842 goto done; 19843 usable = (usable / mss) * mss; 19844 } 19845 19846 /* Update the latest receive window size in TCP header. */ 19847 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19848 tcp->tcp_tcph->th_win); 19849 19850 /* 19851 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19852 * 19853 * 1. Simple TCP/IP{v4,v6} (no options). 19854 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19855 * 3. If the TCP connection is in ESTABLISHED state. 19856 * 4. The TCP is not detached. 19857 * 19858 * If any of the above conditions have changed during the 19859 * connection, stop using LSO/MDT and restore the stream head 19860 * parameters accordingly. 19861 */ 19862 ipst = tcps->tcps_netstack->netstack_ip; 19863 19864 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19865 ((tcp->tcp_ipversion == IPV4_VERSION && 19866 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19867 (tcp->tcp_ipversion == IPV6_VERSION && 19868 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19869 tcp->tcp_state != TCPS_ESTABLISHED || 19870 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19871 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19872 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19873 if (tcp->tcp_lso) { 19874 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19875 tcp->tcp_lso = B_FALSE; 19876 } else { 19877 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19878 tcp->tcp_mdt = B_FALSE; 19879 } 19880 19881 /* Anything other than detached is considered pathological */ 19882 if (!TCP_IS_DETACHED(tcp)) { 19883 if (tcp->tcp_lso) 19884 TCP_STAT(tcps, tcp_lso_disabled); 19885 else 19886 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19887 (void) tcp_maxpsz_set(tcp, B_TRUE); 19888 } 19889 } 19890 19891 /* Use MDT if sendable amount is greater than the threshold */ 19892 if (tcp->tcp_mdt && 19893 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19894 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19895 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19896 (tcp->tcp_valid_bits == 0 || 19897 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19898 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19899 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19900 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19901 local_time, mdt_thres); 19902 } else { 19903 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19904 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19905 local_time, INT_MAX); 19906 } 19907 19908 /* Pretend that all we were trying to send really got sent */ 19909 if (rc < 0 && tail_unsent < 0) { 19910 do { 19911 xmit_tail = xmit_tail->b_cont; 19912 xmit_tail->b_prev = local_time; 19913 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19914 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19915 tail_unsent += (int)(xmit_tail->b_wptr - 19916 xmit_tail->b_rptr); 19917 } while (tail_unsent < 0); 19918 } 19919 done:; 19920 tcp->tcp_xmit_tail = xmit_tail; 19921 tcp->tcp_xmit_tail_unsent = tail_unsent; 19922 len = tcp->tcp_snxt - snxt; 19923 if (len) { 19924 /* 19925 * If new data was sent, need to update the notsack 19926 * list, which is, afterall, data blocks that have 19927 * not been sack'ed by the receiver. New data is 19928 * not sack'ed. 19929 */ 19930 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19931 /* len is a negative value. */ 19932 tcp->tcp_pipe -= len; 19933 tcp_notsack_update(&(tcp->tcp_notsack_list), 19934 tcp->tcp_snxt, snxt, 19935 &(tcp->tcp_num_notsack_blk), 19936 &(tcp->tcp_cnt_notsack_list)); 19937 } 19938 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19939 tcp->tcp_rack = tcp->tcp_rnxt; 19940 tcp->tcp_rack_cnt = 0; 19941 if ((snxt + len) == tcp->tcp_suna) { 19942 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19943 } 19944 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19945 /* 19946 * Didn't send anything. Make sure the timer is running 19947 * so that we will probe a zero window. 19948 */ 19949 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19950 } 19951 /* Note that len is the amount we just sent but with a negative sign */ 19952 tcp->tcp_unsent += len; 19953 mutex_enter(&tcp->tcp_non_sq_lock); 19954 if (tcp->tcp_flow_stopped) { 19955 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19956 tcp_clrqfull(tcp); 19957 } 19958 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19959 tcp_setqfull(tcp); 19960 } 19961 mutex_exit(&tcp->tcp_non_sq_lock); 19962 } 19963 19964 /* 19965 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19966 * outgoing TCP header with the template header, as well as other 19967 * options such as time-stamp, ECN and/or SACK. 19968 */ 19969 static void 19970 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19971 { 19972 tcph_t *tcp_tmpl, *tcp_h; 19973 uint32_t *dst, *src; 19974 int hdrlen; 19975 19976 ASSERT(OK_32PTR(rptr)); 19977 19978 /* Template header */ 19979 tcp_tmpl = tcp->tcp_tcph; 19980 19981 /* Header of outgoing packet */ 19982 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19983 19984 /* dst and src are opaque 32-bit fields, used for copying */ 19985 dst = (uint32_t *)rptr; 19986 src = (uint32_t *)tcp->tcp_iphc; 19987 hdrlen = tcp->tcp_hdr_len; 19988 19989 /* Fill time-stamp option if needed */ 19990 if (tcp->tcp_snd_ts_ok) { 19991 U32_TO_BE32((uint32_t)now, 19992 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19993 U32_TO_BE32(tcp->tcp_ts_recent, 19994 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19995 } else { 19996 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19997 } 19998 19999 /* 20000 * Copy the template header; is this really more efficient than 20001 * calling bcopy()? For simple IPv4/TCP, it may be the case, 20002 * but perhaps not for other scenarios. 20003 */ 20004 dst[0] = src[0]; 20005 dst[1] = src[1]; 20006 dst[2] = src[2]; 20007 dst[3] = src[3]; 20008 dst[4] = src[4]; 20009 dst[5] = src[5]; 20010 dst[6] = src[6]; 20011 dst[7] = src[7]; 20012 dst[8] = src[8]; 20013 dst[9] = src[9]; 20014 if (hdrlen -= 40) { 20015 hdrlen >>= 2; 20016 dst += 10; 20017 src += 10; 20018 do { 20019 *dst++ = *src++; 20020 } while (--hdrlen); 20021 } 20022 20023 /* 20024 * Set the ECN info in the TCP header if it is not a zero 20025 * window probe. Zero window probe is only sent in 20026 * tcp_wput_data() and tcp_timer(). 20027 */ 20028 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 20029 SET_ECT(tcp, rptr); 20030 20031 if (tcp->tcp_ecn_echo_on) 20032 tcp_h->th_flags[0] |= TH_ECE; 20033 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 20034 tcp_h->th_flags[0] |= TH_CWR; 20035 tcp->tcp_ecn_cwr_sent = B_TRUE; 20036 } 20037 } 20038 20039 /* Fill in SACK options */ 20040 if (num_sack_blk > 0) { 20041 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 20042 sack_blk_t *tmp; 20043 int32_t i; 20044 20045 wptr[0] = TCPOPT_NOP; 20046 wptr[1] = TCPOPT_NOP; 20047 wptr[2] = TCPOPT_SACK; 20048 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20049 sizeof (sack_blk_t); 20050 wptr += TCPOPT_REAL_SACK_LEN; 20051 20052 tmp = tcp->tcp_sack_list; 20053 for (i = 0; i < num_sack_blk; i++) { 20054 U32_TO_BE32(tmp[i].begin, wptr); 20055 wptr += sizeof (tcp_seq); 20056 U32_TO_BE32(tmp[i].end, wptr); 20057 wptr += sizeof (tcp_seq); 20058 } 20059 tcp_h->th_offset_and_rsrvd[0] += 20060 ((num_sack_blk * 2 + 1) << 4); 20061 } 20062 } 20063 20064 /* 20065 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20066 * the destination address and SAP attribute, and if necessary, the 20067 * hardware checksum offload attribute to a Multidata message. 20068 */ 20069 static int 20070 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20071 const uint32_t start, const uint32_t stuff, const uint32_t end, 20072 const uint32_t flags, tcp_stack_t *tcps) 20073 { 20074 /* Add global destination address & SAP attribute */ 20075 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20076 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20077 "destination address+SAP\n")); 20078 20079 if (dlmp != NULL) 20080 TCP_STAT(tcps, tcp_mdt_allocfail); 20081 return (-1); 20082 } 20083 20084 /* Add global hwcksum attribute */ 20085 if (hwcksum && 20086 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20087 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20088 "checksum attribute\n")); 20089 20090 TCP_STAT(tcps, tcp_mdt_allocfail); 20091 return (-1); 20092 } 20093 20094 return (0); 20095 } 20096 20097 /* 20098 * Smaller and private version of pdescinfo_t used specifically for TCP, 20099 * which allows for only two payload spans per packet. 20100 */ 20101 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20102 20103 /* 20104 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20105 * scheme, and returns one the following: 20106 * 20107 * -1 = failed allocation. 20108 * 0 = success; burst count reached, or usable send window is too small, 20109 * and that we'd rather wait until later before sending again. 20110 */ 20111 static int 20112 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20113 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20114 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20115 const int mdt_thres) 20116 { 20117 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20118 multidata_t *mmd; 20119 uint_t obsegs, obbytes, hdr_frag_sz; 20120 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20121 int num_burst_seg, max_pld; 20122 pdesc_t *pkt; 20123 tcp_pdescinfo_t tcp_pkt_info; 20124 pdescinfo_t *pkt_info; 20125 int pbuf_idx, pbuf_idx_nxt; 20126 int seg_len, len, spill, af; 20127 boolean_t add_buffer, zcopy, clusterwide; 20128 boolean_t rconfirm = B_FALSE; 20129 boolean_t done = B_FALSE; 20130 uint32_t cksum; 20131 uint32_t hwcksum_flags; 20132 ire_t *ire = NULL; 20133 ill_t *ill; 20134 ipha_t *ipha; 20135 ip6_t *ip6h; 20136 ipaddr_t src, dst; 20137 ill_zerocopy_capab_t *zc_cap = NULL; 20138 uint16_t *up; 20139 int err; 20140 conn_t *connp; 20141 tcp_stack_t *tcps = tcp->tcp_tcps; 20142 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20143 int usable_mmd, tail_unsent_mmd; 20144 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20145 mblk_t *xmit_tail_mmd; 20146 20147 #ifdef _BIG_ENDIAN 20148 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20149 #else 20150 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20151 #endif 20152 20153 #define PREP_NEW_MULTIDATA() { \ 20154 mmd = NULL; \ 20155 md_mp = md_hbuf = NULL; \ 20156 cur_hdr_off = 0; \ 20157 max_pld = tcp->tcp_mdt_max_pld; \ 20158 pbuf_idx = pbuf_idx_nxt = -1; \ 20159 add_buffer = B_TRUE; \ 20160 zcopy = B_FALSE; \ 20161 } 20162 20163 #define PREP_NEW_PBUF() { \ 20164 md_pbuf = md_pbuf_nxt = NULL; \ 20165 pbuf_idx = pbuf_idx_nxt = -1; \ 20166 cur_pld_off = 0; \ 20167 first_snxt = *snxt; \ 20168 ASSERT(*tail_unsent > 0); \ 20169 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20170 } 20171 20172 ASSERT(mdt_thres >= mss); 20173 ASSERT(*usable > 0 && *usable > mdt_thres); 20174 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20175 ASSERT(!TCP_IS_DETACHED(tcp)); 20176 ASSERT(tcp->tcp_valid_bits == 0 || 20177 tcp->tcp_valid_bits == TCP_FSS_VALID); 20178 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20179 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20180 (tcp->tcp_ipversion == IPV6_VERSION && 20181 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20182 20183 connp = tcp->tcp_connp; 20184 ASSERT(connp != NULL); 20185 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20186 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20187 20188 usable_mmd = tail_unsent_mmd = 0; 20189 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20190 xmit_tail_mmd = NULL; 20191 /* 20192 * Note that tcp will only declare at most 2 payload spans per 20193 * packet, which is much lower than the maximum allowable number 20194 * of packet spans per Multidata. For this reason, we use the 20195 * privately declared and smaller descriptor info structure, in 20196 * order to save some stack space. 20197 */ 20198 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20199 20200 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20201 if (af == AF_INET) { 20202 dst = tcp->tcp_ipha->ipha_dst; 20203 src = tcp->tcp_ipha->ipha_src; 20204 ASSERT(!CLASSD(dst)); 20205 } 20206 ASSERT(af == AF_INET || 20207 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20208 20209 obsegs = obbytes = 0; 20210 num_burst_seg = tcp->tcp_snd_burst; 20211 md_mp_head = NULL; 20212 PREP_NEW_MULTIDATA(); 20213 20214 /* 20215 * Before we go on further, make sure there is an IRE that we can 20216 * use, and that the ILL supports MDT. Otherwise, there's no point 20217 * in proceeding any further, and we should just hand everything 20218 * off to the legacy path. 20219 */ 20220 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20221 goto legacy_send_no_md; 20222 20223 ASSERT(ire != NULL); 20224 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20225 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20226 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20227 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20228 /* 20229 * If we do support loopback for MDT (which requires modifications 20230 * to the receiving paths), the following assertions should go away, 20231 * and we would be sending the Multidata to loopback conn later on. 20232 */ 20233 ASSERT(!IRE_IS_LOCAL(ire)); 20234 ASSERT(ire->ire_stq != NULL); 20235 20236 ill = ire_to_ill(ire); 20237 ASSERT(ill != NULL); 20238 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20239 20240 if (!tcp->tcp_ire_ill_check_done) { 20241 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20242 tcp->tcp_ire_ill_check_done = B_TRUE; 20243 } 20244 20245 /* 20246 * If the underlying interface conditions have changed, or if the 20247 * new interface does not support MDT, go back to legacy path. 20248 */ 20249 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20250 /* don't go through this path anymore for this connection */ 20251 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20252 tcp->tcp_mdt = B_FALSE; 20253 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20254 "interface %s\n", (void *)connp, ill->ill_name)); 20255 /* IRE will be released prior to returning */ 20256 goto legacy_send_no_md; 20257 } 20258 20259 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20260 zc_cap = ill->ill_zerocopy_capab; 20261 20262 /* 20263 * Check if we can take tcp fast-path. Note that "incomplete" 20264 * ire's (where the link-layer for next hop is not resolved 20265 * or where the fast-path header in nce_fp_mp is not available 20266 * yet) are sent down the legacy (slow) path. 20267 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20268 */ 20269 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20270 /* IRE will be released prior to returning */ 20271 goto legacy_send_no_md; 20272 } 20273 20274 /* go to legacy path if interface doesn't support zerocopy */ 20275 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20276 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20277 /* IRE will be released prior to returning */ 20278 goto legacy_send_no_md; 20279 } 20280 20281 /* does the interface support hardware checksum offload? */ 20282 hwcksum_flags = 0; 20283 if (ILL_HCKSUM_CAPABLE(ill) && 20284 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20285 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20286 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20287 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20288 HCKSUM_IPHDRCKSUM) 20289 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20290 20291 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20292 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20293 hwcksum_flags |= HCK_FULLCKSUM; 20294 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20295 HCKSUM_INET_PARTIAL) 20296 hwcksum_flags |= HCK_PARTIALCKSUM; 20297 } 20298 20299 /* 20300 * Each header fragment consists of the leading extra space, 20301 * followed by the TCP/IP header, and the trailing extra space. 20302 * We make sure that each header fragment begins on a 32-bit 20303 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20304 * aligned in tcp_mdt_update). 20305 */ 20306 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20307 tcp->tcp_mdt_hdr_tail), 4); 20308 20309 /* are we starting from the beginning of data block? */ 20310 if (*tail_unsent == 0) { 20311 *xmit_tail = (*xmit_tail)->b_cont; 20312 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20313 *tail_unsent = (int)MBLKL(*xmit_tail); 20314 } 20315 20316 /* 20317 * Here we create one or more Multidata messages, each made up of 20318 * one header buffer and up to N payload buffers. This entire 20319 * operation is done within two loops: 20320 * 20321 * The outer loop mostly deals with creating the Multidata message, 20322 * as well as the header buffer that gets added to it. It also 20323 * links the Multidata messages together such that all of them can 20324 * be sent down to the lower layer in a single putnext call; this 20325 * linking behavior depends on the tcp_mdt_chain tunable. 20326 * 20327 * The inner loop takes an existing Multidata message, and adds 20328 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20329 * packetizes those buffers by filling up the corresponding header 20330 * buffer fragments with the proper IP and TCP headers, and by 20331 * describing the layout of each packet in the packet descriptors 20332 * that get added to the Multidata. 20333 */ 20334 do { 20335 /* 20336 * If usable send window is too small, or data blocks in 20337 * transmit list are smaller than our threshold (i.e. app 20338 * performs large writes followed by small ones), we hand 20339 * off the control over to the legacy path. Note that we'll 20340 * get back the control once it encounters a large block. 20341 */ 20342 if (*usable < mss || (*tail_unsent <= mdt_thres && 20343 (*xmit_tail)->b_cont != NULL && 20344 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20345 /* send down what we've got so far */ 20346 if (md_mp_head != NULL) { 20347 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20348 obsegs, obbytes, &rconfirm); 20349 } 20350 /* 20351 * Pass control over to tcp_send(), but tell it to 20352 * return to us once a large-size transmission is 20353 * possible. 20354 */ 20355 TCP_STAT(tcps, tcp_mdt_legacy_small); 20356 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20357 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20358 tail_unsent, xmit_tail, local_time, 20359 mdt_thres)) <= 0) { 20360 /* burst count reached, or alloc failed */ 20361 IRE_REFRELE(ire); 20362 return (err); 20363 } 20364 20365 /* tcp_send() may have sent everything, so check */ 20366 if (*usable <= 0) { 20367 IRE_REFRELE(ire); 20368 return (0); 20369 } 20370 20371 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20372 /* 20373 * We may have delivered the Multidata, so make sure 20374 * to re-initialize before the next round. 20375 */ 20376 md_mp_head = NULL; 20377 obsegs = obbytes = 0; 20378 num_burst_seg = tcp->tcp_snd_burst; 20379 PREP_NEW_MULTIDATA(); 20380 20381 /* are we starting from the beginning of data block? */ 20382 if (*tail_unsent == 0) { 20383 *xmit_tail = (*xmit_tail)->b_cont; 20384 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20385 (uintptr_t)INT_MAX); 20386 *tail_unsent = (int)MBLKL(*xmit_tail); 20387 } 20388 } 20389 /* 20390 * Record current values for parameters we may need to pass 20391 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20392 * each iteration of the outer loop (each multidata message 20393 * creation). If we have a failure in the inner loop, we send 20394 * any complete multidata messages we have before reverting 20395 * to using the traditional non-md path. 20396 */ 20397 snxt_mmd = *snxt; 20398 usable_mmd = *usable; 20399 xmit_tail_mmd = *xmit_tail; 20400 tail_unsent_mmd = *tail_unsent; 20401 obsegs_mmd = obsegs; 20402 obbytes_mmd = obbytes; 20403 20404 /* 20405 * max_pld limits the number of mblks in tcp's transmit 20406 * queue that can be added to a Multidata message. Once 20407 * this counter reaches zero, no more additional mblks 20408 * can be added to it. What happens afterwards depends 20409 * on whether or not we are set to chain the Multidata 20410 * messages. If we are to link them together, reset 20411 * max_pld to its original value (tcp_mdt_max_pld) and 20412 * prepare to create a new Multidata message which will 20413 * get linked to md_mp_head. Else, leave it alone and 20414 * let the inner loop break on its own. 20415 */ 20416 if (tcp_mdt_chain && max_pld == 0) 20417 PREP_NEW_MULTIDATA(); 20418 20419 /* adding a payload buffer; re-initialize values */ 20420 if (add_buffer) 20421 PREP_NEW_PBUF(); 20422 20423 /* 20424 * If we don't have a Multidata, either because we just 20425 * (re)entered this outer loop, or after we branched off 20426 * to tcp_send above, setup the Multidata and header 20427 * buffer to be used. 20428 */ 20429 if (md_mp == NULL) { 20430 int md_hbuflen; 20431 uint32_t start, stuff; 20432 20433 /* 20434 * Calculate Multidata header buffer size large enough 20435 * to hold all of the headers that can possibly be 20436 * sent at this moment. We'd rather over-estimate 20437 * the size than running out of space; this is okay 20438 * since this buffer is small anyway. 20439 */ 20440 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20441 20442 /* 20443 * Start and stuff offset for partial hardware 20444 * checksum offload; these are currently for IPv4. 20445 * For full checksum offload, they are set to zero. 20446 */ 20447 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20448 if (af == AF_INET) { 20449 start = IP_SIMPLE_HDR_LENGTH; 20450 stuff = IP_SIMPLE_HDR_LENGTH + 20451 TCP_CHECKSUM_OFFSET; 20452 } else { 20453 start = IPV6_HDR_LEN; 20454 stuff = IPV6_HDR_LEN + 20455 TCP_CHECKSUM_OFFSET; 20456 } 20457 } else { 20458 start = stuff = 0; 20459 } 20460 20461 /* 20462 * Create the header buffer, Multidata, as well as 20463 * any necessary attributes (destination address, 20464 * SAP and hardware checksum offload) that should 20465 * be associated with the Multidata message. 20466 */ 20467 ASSERT(cur_hdr_off == 0); 20468 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20469 ((md_hbuf->b_wptr += md_hbuflen), 20470 (mmd = mmd_alloc(md_hbuf, &md_mp, 20471 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20472 /* fastpath mblk */ 20473 ire->ire_nce->nce_res_mp, 20474 /* hardware checksum enabled */ 20475 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20476 /* hardware checksum offsets */ 20477 start, stuff, 0, 20478 /* hardware checksum flag */ 20479 hwcksum_flags, tcps) != 0)) { 20480 legacy_send: 20481 /* 20482 * We arrive here from a failure within the 20483 * inner (packetizer) loop or we fail one of 20484 * the conditionals above. We restore the 20485 * previously checkpointed values for: 20486 * xmit_tail 20487 * usable 20488 * tail_unsent 20489 * snxt 20490 * obbytes 20491 * obsegs 20492 * We should then be able to dispatch any 20493 * complete multidata before reverting to the 20494 * traditional path with consistent parameters 20495 * (the inner loop updates these as it 20496 * iterates). 20497 */ 20498 *xmit_tail = xmit_tail_mmd; 20499 *usable = usable_mmd; 20500 *tail_unsent = tail_unsent_mmd; 20501 *snxt = snxt_mmd; 20502 obbytes = obbytes_mmd; 20503 obsegs = obsegs_mmd; 20504 if (md_mp != NULL) { 20505 /* Unlink message from the chain */ 20506 if (md_mp_head != NULL) { 20507 err = (intptr_t)rmvb(md_mp_head, 20508 md_mp); 20509 /* 20510 * We can't assert that rmvb 20511 * did not return -1, since we 20512 * may get here before linkb 20513 * happens. We do, however, 20514 * check if we just removed the 20515 * only element in the list. 20516 */ 20517 if (err == 0) 20518 md_mp_head = NULL; 20519 } 20520 /* md_hbuf gets freed automatically */ 20521 TCP_STAT(tcps, tcp_mdt_discarded); 20522 freeb(md_mp); 20523 } else { 20524 /* Either allocb or mmd_alloc failed */ 20525 TCP_STAT(tcps, tcp_mdt_allocfail); 20526 if (md_hbuf != NULL) 20527 freeb(md_hbuf); 20528 } 20529 20530 /* send down what we've got so far */ 20531 if (md_mp_head != NULL) { 20532 tcp_multisend_data(tcp, ire, ill, 20533 md_mp_head, obsegs, obbytes, 20534 &rconfirm); 20535 } 20536 legacy_send_no_md: 20537 if (ire != NULL) 20538 IRE_REFRELE(ire); 20539 /* 20540 * Too bad; let the legacy path handle this. 20541 * We specify INT_MAX for the threshold, since 20542 * we gave up with the Multidata processings 20543 * and let the old path have it all. 20544 */ 20545 TCP_STAT(tcps, tcp_mdt_legacy_all); 20546 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20547 tcp_tcp_hdr_len, num_sack_blk, usable, 20548 snxt, tail_unsent, xmit_tail, local_time, 20549 INT_MAX)); 20550 } 20551 20552 /* link to any existing ones, if applicable */ 20553 TCP_STAT(tcps, tcp_mdt_allocd); 20554 if (md_mp_head == NULL) { 20555 md_mp_head = md_mp; 20556 } else if (tcp_mdt_chain) { 20557 TCP_STAT(tcps, tcp_mdt_linked); 20558 linkb(md_mp_head, md_mp); 20559 } 20560 } 20561 20562 ASSERT(md_mp_head != NULL); 20563 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20564 ASSERT(md_mp != NULL && mmd != NULL); 20565 ASSERT(md_hbuf != NULL); 20566 20567 /* 20568 * Packetize the transmittable portion of the data block; 20569 * each data block is essentially added to the Multidata 20570 * as a payload buffer. We also deal with adding more 20571 * than one payload buffers, which happens when the remaining 20572 * packetized portion of the current payload buffer is less 20573 * than MSS, while the next data block in transmit queue 20574 * has enough data to make up for one. This "spillover" 20575 * case essentially creates a split-packet, where portions 20576 * of the packet's payload fragments may span across two 20577 * virtually discontiguous address blocks. 20578 */ 20579 seg_len = mss; 20580 do { 20581 len = seg_len; 20582 20583 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20584 ipha = NULL; 20585 ip6h = NULL; 20586 20587 ASSERT(len > 0); 20588 ASSERT(max_pld >= 0); 20589 ASSERT(!add_buffer || cur_pld_off == 0); 20590 20591 /* 20592 * First time around for this payload buffer; note 20593 * in the case of a spillover, the following has 20594 * been done prior to adding the split-packet 20595 * descriptor to Multidata, and we don't want to 20596 * repeat the process. 20597 */ 20598 if (add_buffer) { 20599 ASSERT(mmd != NULL); 20600 ASSERT(md_pbuf == NULL); 20601 ASSERT(md_pbuf_nxt == NULL); 20602 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20603 20604 /* 20605 * Have we reached the limit? We'd get to 20606 * this case when we're not chaining the 20607 * Multidata messages together, and since 20608 * we're done, terminate this loop. 20609 */ 20610 if (max_pld == 0) 20611 break; /* done */ 20612 20613 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20614 TCP_STAT(tcps, tcp_mdt_allocfail); 20615 goto legacy_send; /* out_of_mem */ 20616 } 20617 20618 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20619 zc_cap != NULL) { 20620 if (!ip_md_zcopy_attr(mmd, NULL, 20621 zc_cap->ill_zerocopy_flags)) { 20622 freeb(md_pbuf); 20623 TCP_STAT(tcps, 20624 tcp_mdt_allocfail); 20625 /* out_of_mem */ 20626 goto legacy_send; 20627 } 20628 zcopy = B_TRUE; 20629 } 20630 20631 md_pbuf->b_rptr += base_pld_off; 20632 20633 /* 20634 * Add a payload buffer to the Multidata; this 20635 * operation must not fail, or otherwise our 20636 * logic in this routine is broken. There 20637 * is no memory allocation done by the 20638 * routine, so any returned failure simply 20639 * tells us that we've done something wrong. 20640 * 20641 * A failure tells us that either we're adding 20642 * the same payload buffer more than once, or 20643 * we're trying to add more buffers than 20644 * allowed (max_pld calculation is wrong). 20645 * None of the above cases should happen, and 20646 * we panic because either there's horrible 20647 * heap corruption, and/or programming mistake. 20648 */ 20649 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20650 if (pbuf_idx < 0) { 20651 cmn_err(CE_PANIC, "tcp_multisend: " 20652 "payload buffer logic error " 20653 "detected for tcp %p mmd %p " 20654 "pbuf %p (%d)\n", 20655 (void *)tcp, (void *)mmd, 20656 (void *)md_pbuf, pbuf_idx); 20657 } 20658 20659 ASSERT(max_pld > 0); 20660 --max_pld; 20661 add_buffer = B_FALSE; 20662 } 20663 20664 ASSERT(md_mp_head != NULL); 20665 ASSERT(md_pbuf != NULL); 20666 ASSERT(md_pbuf_nxt == NULL); 20667 ASSERT(pbuf_idx != -1); 20668 ASSERT(pbuf_idx_nxt == -1); 20669 ASSERT(*usable > 0); 20670 20671 /* 20672 * We spillover to the next payload buffer only 20673 * if all of the following is true: 20674 * 20675 * 1. There is not enough data on the current 20676 * payload buffer to make up `len', 20677 * 2. We are allowed to send `len', 20678 * 3. The next payload buffer length is large 20679 * enough to accomodate `spill'. 20680 */ 20681 if ((spill = len - *tail_unsent) > 0 && 20682 *usable >= len && 20683 MBLKL((*xmit_tail)->b_cont) >= spill && 20684 max_pld > 0) { 20685 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20686 if (md_pbuf_nxt == NULL) { 20687 TCP_STAT(tcps, tcp_mdt_allocfail); 20688 goto legacy_send; /* out_of_mem */ 20689 } 20690 20691 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20692 zc_cap != NULL) { 20693 if (!ip_md_zcopy_attr(mmd, NULL, 20694 zc_cap->ill_zerocopy_flags)) { 20695 freeb(md_pbuf_nxt); 20696 TCP_STAT(tcps, 20697 tcp_mdt_allocfail); 20698 /* out_of_mem */ 20699 goto legacy_send; 20700 } 20701 zcopy = B_TRUE; 20702 } 20703 20704 /* 20705 * See comments above on the first call to 20706 * mmd_addpldbuf for explanation on the panic. 20707 */ 20708 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20709 if (pbuf_idx_nxt < 0) { 20710 panic("tcp_multisend: " 20711 "next payload buffer logic error " 20712 "detected for tcp %p mmd %p " 20713 "pbuf %p (%d)\n", 20714 (void *)tcp, (void *)mmd, 20715 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20716 } 20717 20718 ASSERT(max_pld > 0); 20719 --max_pld; 20720 } else if (spill > 0) { 20721 /* 20722 * If there's a spillover, but the following 20723 * xmit_tail couldn't give us enough octets 20724 * to reach "len", then stop the current 20725 * Multidata creation and let the legacy 20726 * tcp_send() path take over. We don't want 20727 * to send the tiny segment as part of this 20728 * Multidata for performance reasons; instead, 20729 * we let the legacy path deal with grouping 20730 * it with the subsequent small mblks. 20731 */ 20732 if (*usable >= len && 20733 MBLKL((*xmit_tail)->b_cont) < spill) { 20734 max_pld = 0; 20735 break; /* done */ 20736 } 20737 20738 /* 20739 * We can't spillover, and we are near 20740 * the end of the current payload buffer, 20741 * so send what's left. 20742 */ 20743 ASSERT(*tail_unsent > 0); 20744 len = *tail_unsent; 20745 } 20746 20747 /* tail_unsent is negated if there is a spillover */ 20748 *tail_unsent -= len; 20749 *usable -= len; 20750 ASSERT(*usable >= 0); 20751 20752 if (*usable < mss) 20753 seg_len = *usable; 20754 /* 20755 * Sender SWS avoidance; see comments in tcp_send(); 20756 * everything else is the same, except that we only 20757 * do this here if there is no more data to be sent 20758 * following the current xmit_tail. We don't check 20759 * for 1-byte urgent data because we shouldn't get 20760 * here if TCP_URG_VALID is set. 20761 */ 20762 if (*usable > 0 && *usable < mss && 20763 ((md_pbuf_nxt == NULL && 20764 (*xmit_tail)->b_cont == NULL) || 20765 (md_pbuf_nxt != NULL && 20766 (*xmit_tail)->b_cont->b_cont == NULL)) && 20767 seg_len < (tcp->tcp_max_swnd >> 1) && 20768 (tcp->tcp_unsent - 20769 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20770 !tcp->tcp_zero_win_probe) { 20771 if ((*snxt + len) == tcp->tcp_snxt && 20772 (*snxt + len) == tcp->tcp_suna) { 20773 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20774 } 20775 done = B_TRUE; 20776 } 20777 20778 /* 20779 * Prime pump for IP's checksumming on our behalf; 20780 * include the adjustment for a source route if any. 20781 * Do this only for software/partial hardware checksum 20782 * offload, as this field gets zeroed out later for 20783 * the full hardware checksum offload case. 20784 */ 20785 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20786 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20787 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20788 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20789 } 20790 20791 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20792 *snxt += len; 20793 20794 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20795 /* 20796 * We set the PUSH bit only if TCP has no more buffered 20797 * data to be transmitted (or if sender SWS avoidance 20798 * takes place), as opposed to setting it for every 20799 * last packet in the burst. 20800 */ 20801 if (done || 20802 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20803 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20804 20805 /* 20806 * Set FIN bit if this is our last segment; snxt 20807 * already includes its length, and it will not 20808 * be adjusted after this point. 20809 */ 20810 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20811 *snxt == tcp->tcp_fss) { 20812 if (!tcp->tcp_fin_acked) { 20813 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20814 BUMP_MIB(&tcps->tcps_mib, 20815 tcpOutControl); 20816 } 20817 if (!tcp->tcp_fin_sent) { 20818 tcp->tcp_fin_sent = B_TRUE; 20819 /* 20820 * tcp state must be ESTABLISHED 20821 * in order for us to get here in 20822 * the first place. 20823 */ 20824 tcp->tcp_state = TCPS_FIN_WAIT_1; 20825 20826 /* 20827 * Upon returning from this routine, 20828 * tcp_wput_data() will set tcp_snxt 20829 * to be equal to snxt + tcp_fin_sent. 20830 * This is essentially the same as 20831 * setting it to tcp_fss + 1. 20832 */ 20833 } 20834 } 20835 20836 tcp->tcp_last_sent_len = (ushort_t)len; 20837 20838 len += tcp_hdr_len; 20839 if (tcp->tcp_ipversion == IPV4_VERSION) 20840 tcp->tcp_ipha->ipha_length = htons(len); 20841 else 20842 tcp->tcp_ip6h->ip6_plen = htons(len - 20843 ((char *)&tcp->tcp_ip6h[1] - 20844 tcp->tcp_iphc)); 20845 20846 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20847 20848 /* setup header fragment */ 20849 PDESC_HDR_ADD(pkt_info, 20850 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20851 tcp->tcp_mdt_hdr_head, /* head room */ 20852 tcp_hdr_len, /* len */ 20853 tcp->tcp_mdt_hdr_tail); /* tail room */ 20854 20855 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20856 hdr_frag_sz); 20857 ASSERT(MBLKIN(md_hbuf, 20858 (pkt_info->hdr_base - md_hbuf->b_rptr), 20859 PDESC_HDRSIZE(pkt_info))); 20860 20861 /* setup first payload fragment */ 20862 PDESC_PLD_INIT(pkt_info); 20863 PDESC_PLD_SPAN_ADD(pkt_info, 20864 pbuf_idx, /* index */ 20865 md_pbuf->b_rptr + cur_pld_off, /* start */ 20866 tcp->tcp_last_sent_len); /* len */ 20867 20868 /* create a split-packet in case of a spillover */ 20869 if (md_pbuf_nxt != NULL) { 20870 ASSERT(spill > 0); 20871 ASSERT(pbuf_idx_nxt > pbuf_idx); 20872 ASSERT(!add_buffer); 20873 20874 md_pbuf = md_pbuf_nxt; 20875 md_pbuf_nxt = NULL; 20876 pbuf_idx = pbuf_idx_nxt; 20877 pbuf_idx_nxt = -1; 20878 cur_pld_off = spill; 20879 20880 /* trim out first payload fragment */ 20881 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20882 20883 /* setup second payload fragment */ 20884 PDESC_PLD_SPAN_ADD(pkt_info, 20885 pbuf_idx, /* index */ 20886 md_pbuf->b_rptr, /* start */ 20887 spill); /* len */ 20888 20889 if ((*xmit_tail)->b_next == NULL) { 20890 /* 20891 * Store the lbolt used for RTT 20892 * estimation. We can only record one 20893 * timestamp per mblk so we do it when 20894 * we reach the end of the payload 20895 * buffer. Also we only take a new 20896 * timestamp sample when the previous 20897 * timed data from the same mblk has 20898 * been ack'ed. 20899 */ 20900 (*xmit_tail)->b_prev = local_time; 20901 (*xmit_tail)->b_next = 20902 (mblk_t *)(uintptr_t)first_snxt; 20903 } 20904 20905 first_snxt = *snxt - spill; 20906 20907 /* 20908 * Advance xmit_tail; usable could be 0 by 20909 * the time we got here, but we made sure 20910 * above that we would only spillover to 20911 * the next data block if usable includes 20912 * the spilled-over amount prior to the 20913 * subtraction. Therefore, we are sure 20914 * that xmit_tail->b_cont can't be NULL. 20915 */ 20916 ASSERT((*xmit_tail)->b_cont != NULL); 20917 *xmit_tail = (*xmit_tail)->b_cont; 20918 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20919 (uintptr_t)INT_MAX); 20920 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20921 } else { 20922 cur_pld_off += tcp->tcp_last_sent_len; 20923 } 20924 20925 /* 20926 * Fill in the header using the template header, and 20927 * add options such as time-stamp, ECN and/or SACK, 20928 * as needed. 20929 */ 20930 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20931 (clock_t)local_time, num_sack_blk); 20932 20933 /* take care of some IP header businesses */ 20934 if (af == AF_INET) { 20935 ipha = (ipha_t *)pkt_info->hdr_rptr; 20936 20937 ASSERT(OK_32PTR((uchar_t *)ipha)); 20938 ASSERT(PDESC_HDRL(pkt_info) >= 20939 IP_SIMPLE_HDR_LENGTH); 20940 ASSERT(ipha->ipha_version_and_hdr_length == 20941 IP_SIMPLE_HDR_VERSION); 20942 20943 /* 20944 * Assign ident value for current packet; see 20945 * related comments in ip_wput_ire() about the 20946 * contract private interface with clustering 20947 * group. 20948 */ 20949 clusterwide = B_FALSE; 20950 if (cl_inet_ipident != NULL) { 20951 ASSERT(cl_inet_isclusterwide != NULL); 20952 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20953 AF_INET, 20954 (uint8_t *)(uintptr_t)src)) { 20955 ipha->ipha_ident = 20956 (*cl_inet_ipident) 20957 (IPPROTO_IP, AF_INET, 20958 (uint8_t *)(uintptr_t)src, 20959 (uint8_t *)(uintptr_t)dst); 20960 clusterwide = B_TRUE; 20961 } 20962 } 20963 20964 if (!clusterwide) { 20965 ipha->ipha_ident = (uint16_t) 20966 atomic_add_32_nv( 20967 &ire->ire_ident, 1); 20968 } 20969 #ifndef _BIG_ENDIAN 20970 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20971 (ipha->ipha_ident >> 8); 20972 #endif 20973 } else { 20974 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20975 20976 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20977 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20978 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20979 ASSERT(PDESC_HDRL(pkt_info) >= 20980 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20981 TCP_CHECKSUM_SIZE)); 20982 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20983 20984 if (tcp->tcp_ip_forward_progress) { 20985 rconfirm = B_TRUE; 20986 tcp->tcp_ip_forward_progress = B_FALSE; 20987 } 20988 } 20989 20990 /* at least one payload span, and at most two */ 20991 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20992 20993 /* add the packet descriptor to Multidata */ 20994 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20995 KM_NOSLEEP)) == NULL) { 20996 /* 20997 * Any failure other than ENOMEM indicates 20998 * that we have passed in invalid pkt_info 20999 * or parameters to mmd_addpdesc, which must 21000 * not happen. 21001 * 21002 * EINVAL is a result of failure on boundary 21003 * checks against the pkt_info contents. It 21004 * should not happen, and we panic because 21005 * either there's horrible heap corruption, 21006 * and/or programming mistake. 21007 */ 21008 if (err != ENOMEM) { 21009 cmn_err(CE_PANIC, "tcp_multisend: " 21010 "pdesc logic error detected for " 21011 "tcp %p mmd %p pinfo %p (%d)\n", 21012 (void *)tcp, (void *)mmd, 21013 (void *)pkt_info, err); 21014 } 21015 TCP_STAT(tcps, tcp_mdt_addpdescfail); 21016 goto legacy_send; /* out_of_mem */ 21017 } 21018 ASSERT(pkt != NULL); 21019 21020 /* calculate IP header and TCP checksums */ 21021 if (af == AF_INET) { 21022 /* calculate pseudo-header checksum */ 21023 cksum = (dst >> 16) + (dst & 0xFFFF) + 21024 (src >> 16) + (src & 0xFFFF); 21025 21026 /* offset for TCP header checksum */ 21027 up = IPH_TCPH_CHECKSUMP(ipha, 21028 IP_SIMPLE_HDR_LENGTH); 21029 } else { 21030 up = (uint16_t *)&ip6h->ip6_src; 21031 21032 /* calculate pseudo-header checksum */ 21033 cksum = up[0] + up[1] + up[2] + up[3] + 21034 up[4] + up[5] + up[6] + up[7] + 21035 up[8] + up[9] + up[10] + up[11] + 21036 up[12] + up[13] + up[14] + up[15]; 21037 21038 /* Fold the initial sum */ 21039 cksum = (cksum & 0xffff) + (cksum >> 16); 21040 21041 up = (uint16_t *)(((uchar_t *)ip6h) + 21042 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21043 } 21044 21045 if (hwcksum_flags & HCK_FULLCKSUM) { 21046 /* clear checksum field for hardware */ 21047 *up = 0; 21048 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21049 uint32_t sum; 21050 21051 /* pseudo-header checksumming */ 21052 sum = *up + cksum + IP_TCP_CSUM_COMP; 21053 sum = (sum & 0xFFFF) + (sum >> 16); 21054 *up = (sum & 0xFFFF) + (sum >> 16); 21055 } else { 21056 /* software checksumming */ 21057 TCP_STAT(tcps, tcp_out_sw_cksum); 21058 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21059 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21060 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21061 cksum + IP_TCP_CSUM_COMP); 21062 if (*up == 0) 21063 *up = 0xFFFF; 21064 } 21065 21066 /* IPv4 header checksum */ 21067 if (af == AF_INET) { 21068 ipha->ipha_fragment_offset_and_flags |= 21069 (uint32_t)htons(ire->ire_frag_flag); 21070 21071 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21072 ipha->ipha_hdr_checksum = 0; 21073 } else { 21074 IP_HDR_CKSUM(ipha, cksum, 21075 ((uint32_t *)ipha)[0], 21076 ((uint16_t *)ipha)[4]); 21077 } 21078 } 21079 21080 if (af == AF_INET && 21081 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21082 af == AF_INET6 && 21083 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21084 mblk_t *mp, *mp1; 21085 uchar_t *hdr_rptr, *hdr_wptr; 21086 uchar_t *pld_rptr, *pld_wptr; 21087 21088 /* 21089 * We reconstruct a pseudo packet for the hooks 21090 * framework using mmd_transform_link(). 21091 * If it is a split packet we pullup the 21092 * payload. FW_HOOKS expects a pkt comprising 21093 * of two mblks: a header and the payload. 21094 */ 21095 if ((mp = mmd_transform_link(pkt)) == NULL) { 21096 TCP_STAT(tcps, tcp_mdt_allocfail); 21097 goto legacy_send; 21098 } 21099 21100 if (pkt_info->pld_cnt > 1) { 21101 /* split payload, more than one pld */ 21102 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21103 NULL) { 21104 freemsg(mp); 21105 TCP_STAT(tcps, 21106 tcp_mdt_allocfail); 21107 goto legacy_send; 21108 } 21109 freemsg(mp->b_cont); 21110 mp->b_cont = mp1; 21111 } else { 21112 mp1 = mp->b_cont; 21113 } 21114 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21115 21116 /* 21117 * Remember the message offsets. This is so we 21118 * can detect changes when we return from the 21119 * FW_HOOKS callbacks. 21120 */ 21121 hdr_rptr = mp->b_rptr; 21122 hdr_wptr = mp->b_wptr; 21123 pld_rptr = mp->b_cont->b_rptr; 21124 pld_wptr = mp->b_cont->b_wptr; 21125 21126 if (af == AF_INET) { 21127 DTRACE_PROBE4( 21128 ip4__physical__out__start, 21129 ill_t *, NULL, 21130 ill_t *, ill, 21131 ipha_t *, ipha, 21132 mblk_t *, mp); 21133 FW_HOOKS( 21134 ipst->ips_ip4_physical_out_event, 21135 ipst->ips_ipv4firewall_physical_out, 21136 NULL, ill, ipha, mp, mp, 0, ipst); 21137 DTRACE_PROBE1( 21138 ip4__physical__out__end, 21139 mblk_t *, mp); 21140 } else { 21141 DTRACE_PROBE4( 21142 ip6__physical__out_start, 21143 ill_t *, NULL, 21144 ill_t *, ill, 21145 ip6_t *, ip6h, 21146 mblk_t *, mp); 21147 FW_HOOKS6( 21148 ipst->ips_ip6_physical_out_event, 21149 ipst->ips_ipv6firewall_physical_out, 21150 NULL, ill, ip6h, mp, mp, 0, ipst); 21151 DTRACE_PROBE1( 21152 ip6__physical__out__end, 21153 mblk_t *, mp); 21154 } 21155 21156 if (mp == NULL || 21157 (mp1 = mp->b_cont) == NULL || 21158 mp->b_rptr != hdr_rptr || 21159 mp->b_wptr != hdr_wptr || 21160 mp1->b_rptr != pld_rptr || 21161 mp1->b_wptr != pld_wptr || 21162 mp1->b_cont != NULL) { 21163 /* 21164 * We abandon multidata processing and 21165 * return to the normal path, either 21166 * when a packet is blocked, or when 21167 * the boundaries of header buffer or 21168 * payload buffer have been changed by 21169 * FW_HOOKS[6]. 21170 */ 21171 if (mp != NULL) 21172 freemsg(mp); 21173 goto legacy_send; 21174 } 21175 /* Finished with the pseudo packet */ 21176 freemsg(mp); 21177 } 21178 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21179 ill, ipha, ip6h); 21180 /* advance header offset */ 21181 cur_hdr_off += hdr_frag_sz; 21182 21183 obbytes += tcp->tcp_last_sent_len; 21184 ++obsegs; 21185 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21186 *tail_unsent > 0); 21187 21188 if ((*xmit_tail)->b_next == NULL) { 21189 /* 21190 * Store the lbolt used for RTT estimation. We can only 21191 * record one timestamp per mblk so we do it when we 21192 * reach the end of the payload buffer. Also we only 21193 * take a new timestamp sample when the previous timed 21194 * data from the same mblk has been ack'ed. 21195 */ 21196 (*xmit_tail)->b_prev = local_time; 21197 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21198 } 21199 21200 ASSERT(*tail_unsent >= 0); 21201 if (*tail_unsent > 0) { 21202 /* 21203 * We got here because we broke out of the above 21204 * loop due to of one of the following cases: 21205 * 21206 * 1. len < adjusted MSS (i.e. small), 21207 * 2. Sender SWS avoidance, 21208 * 3. max_pld is zero. 21209 * 21210 * We are done for this Multidata, so trim our 21211 * last payload buffer (if any) accordingly. 21212 */ 21213 if (md_pbuf != NULL) 21214 md_pbuf->b_wptr -= *tail_unsent; 21215 } else if (*usable > 0) { 21216 *xmit_tail = (*xmit_tail)->b_cont; 21217 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21218 (uintptr_t)INT_MAX); 21219 *tail_unsent = (int)MBLKL(*xmit_tail); 21220 add_buffer = B_TRUE; 21221 } 21222 } while (!done && *usable > 0 && num_burst_seg > 0 && 21223 (tcp_mdt_chain || max_pld > 0)); 21224 21225 if (md_mp_head != NULL) { 21226 /* send everything down */ 21227 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21228 &rconfirm); 21229 } 21230 21231 #undef PREP_NEW_MULTIDATA 21232 #undef PREP_NEW_PBUF 21233 #undef IPVER 21234 21235 IRE_REFRELE(ire); 21236 return (0); 21237 } 21238 21239 /* 21240 * A wrapper function for sending one or more Multidata messages down to 21241 * the module below ip; this routine does not release the reference of the 21242 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21243 */ 21244 static void 21245 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21246 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21247 { 21248 uint64_t delta; 21249 nce_t *nce; 21250 tcp_stack_t *tcps = tcp->tcp_tcps; 21251 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21252 21253 ASSERT(ire != NULL && ill != NULL); 21254 ASSERT(ire->ire_stq != NULL); 21255 ASSERT(md_mp_head != NULL); 21256 ASSERT(rconfirm != NULL); 21257 21258 /* adjust MIBs and IRE timestamp */ 21259 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21260 tcp->tcp_obsegs += obsegs; 21261 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21262 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21263 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21264 21265 if (tcp->tcp_ipversion == IPV4_VERSION) { 21266 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21267 } else { 21268 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21269 } 21270 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21271 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21272 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21273 21274 ire->ire_ob_pkt_count += obsegs; 21275 if (ire->ire_ipif != NULL) 21276 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21277 ire->ire_last_used_time = lbolt; 21278 21279 if (ipst->ips_ipobs_enabled) { 21280 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21281 pdesc_t *dl_pkt; 21282 pdescinfo_t pinfo; 21283 mblk_t *nmp; 21284 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21285 21286 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21287 (dl_pkt != NULL); 21288 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21289 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21290 continue; 21291 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21292 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21293 freemsg(nmp); 21294 } 21295 } 21296 21297 /* send it down */ 21298 if (ILL_DLS_CAPABLE(ill)) { 21299 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 21300 ill_dls->ill_tx(ill_dls->ill_tx_handle, md_mp_head); 21301 } else { 21302 putnext(ire->ire_stq, md_mp_head); 21303 } 21304 21305 /* we're done for TCP/IPv4 */ 21306 if (tcp->tcp_ipversion == IPV4_VERSION) 21307 return; 21308 21309 nce = ire->ire_nce; 21310 21311 ASSERT(nce != NULL); 21312 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21313 ASSERT(nce->nce_state != ND_INCOMPLETE); 21314 21315 /* reachability confirmation? */ 21316 if (*rconfirm) { 21317 nce->nce_last = TICK_TO_MSEC(lbolt64); 21318 if (nce->nce_state != ND_REACHABLE) { 21319 mutex_enter(&nce->nce_lock); 21320 nce->nce_state = ND_REACHABLE; 21321 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21322 mutex_exit(&nce->nce_lock); 21323 (void) untimeout(nce->nce_timeout_id); 21324 if (ip_debug > 2) { 21325 /* ip1dbg */ 21326 pr_addr_dbg("tcp_multisend_data: state " 21327 "for %s changed to REACHABLE\n", 21328 AF_INET6, &ire->ire_addr_v6); 21329 } 21330 } 21331 /* reset transport reachability confirmation */ 21332 *rconfirm = B_FALSE; 21333 } 21334 21335 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21336 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21337 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21338 21339 if (delta > (uint64_t)ill->ill_reachable_time) { 21340 mutex_enter(&nce->nce_lock); 21341 switch (nce->nce_state) { 21342 case ND_REACHABLE: 21343 case ND_STALE: 21344 /* 21345 * ND_REACHABLE is identical to ND_STALE in this 21346 * specific case. If reachable time has expired for 21347 * this neighbor (delta is greater than reachable 21348 * time), conceptually, the neighbor cache is no 21349 * longer in REACHABLE state, but already in STALE 21350 * state. So the correct transition here is to 21351 * ND_DELAY. 21352 */ 21353 nce->nce_state = ND_DELAY; 21354 mutex_exit(&nce->nce_lock); 21355 NDP_RESTART_TIMER(nce, 21356 ipst->ips_delay_first_probe_time); 21357 if (ip_debug > 3) { 21358 /* ip2dbg */ 21359 pr_addr_dbg("tcp_multisend_data: state " 21360 "for %s changed to DELAY\n", 21361 AF_INET6, &ire->ire_addr_v6); 21362 } 21363 break; 21364 case ND_DELAY: 21365 case ND_PROBE: 21366 mutex_exit(&nce->nce_lock); 21367 /* Timers have already started */ 21368 break; 21369 case ND_UNREACHABLE: 21370 /* 21371 * ndp timer has detected that this nce is 21372 * unreachable and initiated deleting this nce 21373 * and all its associated IREs. This is a race 21374 * where we found the ire before it was deleted 21375 * and have just sent out a packet using this 21376 * unreachable nce. 21377 */ 21378 mutex_exit(&nce->nce_lock); 21379 break; 21380 default: 21381 ASSERT(0); 21382 } 21383 } 21384 } 21385 21386 /* 21387 * Derived from tcp_send_data(). 21388 */ 21389 static void 21390 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21391 int num_lso_seg) 21392 { 21393 ipha_t *ipha; 21394 mblk_t *ire_fp_mp; 21395 uint_t ire_fp_mp_len; 21396 uint32_t hcksum_txflags = 0; 21397 ipaddr_t src; 21398 ipaddr_t dst; 21399 uint32_t cksum; 21400 uint16_t *up; 21401 tcp_stack_t *tcps = tcp->tcp_tcps; 21402 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21403 21404 ASSERT(DB_TYPE(mp) == M_DATA); 21405 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21406 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21407 ASSERT(tcp->tcp_connp != NULL); 21408 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21409 21410 ipha = (ipha_t *)mp->b_rptr; 21411 src = ipha->ipha_src; 21412 dst = ipha->ipha_dst; 21413 21414 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21415 21416 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21417 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21418 num_lso_seg); 21419 #ifndef _BIG_ENDIAN 21420 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21421 #endif 21422 if (tcp->tcp_snd_zcopy_aware) { 21423 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21424 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21425 mp = tcp_zcopy_disable(tcp, mp); 21426 } 21427 21428 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21429 ASSERT(ill->ill_hcksum_capab != NULL); 21430 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21431 } 21432 21433 /* 21434 * Since the TCP checksum should be recalculated by h/w, we can just 21435 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21436 * pseudo-header checksum for HCK_PARTIALCKSUM. 21437 * The partial pseudo-header excludes TCP length, that was calculated 21438 * in tcp_send(), so to zero *up before further processing. 21439 */ 21440 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21441 21442 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21443 *up = 0; 21444 21445 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21446 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21447 21448 /* 21449 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 21450 */ 21451 DB_LSOFLAGS(mp) |= HW_LSO; 21452 DB_LSOMSS(mp) = mss; 21453 21454 ipha->ipha_fragment_offset_and_flags |= 21455 (uint32_t)htons(ire->ire_frag_flag); 21456 21457 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21458 ire_fp_mp_len = MBLKL(ire_fp_mp); 21459 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21460 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21461 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21462 21463 UPDATE_OB_PKT_COUNT(ire); 21464 ire->ire_last_used_time = lbolt; 21465 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21466 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21467 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21468 ntohs(ipha->ipha_length)); 21469 21470 if (ILL_DLS_CAPABLE(ill)) { 21471 /* 21472 * Send the packet directly to DLD, where it may be queued 21473 * depending on the availability of transmit resources at 21474 * the media layer. 21475 */ 21476 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 21477 } else { 21478 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21479 DTRACE_PROBE4(ip4__physical__out__start, 21480 ill_t *, NULL, ill_t *, out_ill, 21481 ipha_t *, ipha, mblk_t *, mp); 21482 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21483 ipst->ips_ipv4firewall_physical_out, 21484 NULL, out_ill, ipha, mp, mp, 0, ipst); 21485 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21486 21487 if (mp != NULL) { 21488 if (ipst->ips_ipobs_enabled) { 21489 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21490 21491 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21492 ALL_ZONES, ill, tcp->tcp_ipversion, 21493 ire_fp_mp_len, ipst); 21494 } 21495 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 21496 putnext(ire->ire_stq, mp); 21497 } 21498 } 21499 } 21500 21501 /* 21502 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21503 * scheme, and returns one of the following: 21504 * 21505 * -1 = failed allocation. 21506 * 0 = success; burst count reached, or usable send window is too small, 21507 * and that we'd rather wait until later before sending again. 21508 * 1 = success; we are called from tcp_multisend(), and both usable send 21509 * window and tail_unsent are greater than the MDT threshold, and thus 21510 * Multidata Transmit should be used instead. 21511 */ 21512 static int 21513 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21514 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21515 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21516 const int mdt_thres) 21517 { 21518 int num_burst_seg = tcp->tcp_snd_burst; 21519 ire_t *ire = NULL; 21520 ill_t *ill = NULL; 21521 mblk_t *ire_fp_mp = NULL; 21522 uint_t ire_fp_mp_len = 0; 21523 int num_lso_seg = 1; 21524 uint_t lso_usable; 21525 boolean_t do_lso_send = B_FALSE; 21526 tcp_stack_t *tcps = tcp->tcp_tcps; 21527 21528 /* 21529 * Check LSO capability before any further work. And the similar check 21530 * need to be done in for(;;) loop. 21531 * LSO will be deployed when therer is more than one mss of available 21532 * data and a burst transmission is allowed. 21533 */ 21534 if (tcp->tcp_lso && 21535 (tcp->tcp_valid_bits == 0 || 21536 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21537 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21538 /* 21539 * Try to find usable IRE/ILL and do basic check to the ILL. 21540 */ 21541 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21542 /* 21543 * Enable LSO with this transmission. 21544 * Since IRE has been hold in 21545 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21546 * should be called before return. 21547 */ 21548 do_lso_send = B_TRUE; 21549 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21550 ire_fp_mp_len = MBLKL(ire_fp_mp); 21551 /* Round up to multiple of 4 */ 21552 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21553 } else { 21554 do_lso_send = B_FALSE; 21555 ill = NULL; 21556 } 21557 } 21558 21559 for (;;) { 21560 struct datab *db; 21561 tcph_t *tcph; 21562 uint32_t sum; 21563 mblk_t *mp, *mp1; 21564 uchar_t *rptr; 21565 int len; 21566 21567 /* 21568 * If we're called by tcp_multisend(), and the amount of 21569 * sendable data as well as the size of current xmit_tail 21570 * is beyond the MDT threshold, return to the caller and 21571 * let the large data transmit be done using MDT. 21572 */ 21573 if (*usable > 0 && *usable > mdt_thres && 21574 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21575 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21576 ASSERT(tcp->tcp_mdt); 21577 return (1); /* success; do large send */ 21578 } 21579 21580 if (num_burst_seg == 0) 21581 break; /* success; burst count reached */ 21582 21583 /* 21584 * Calculate the maximum payload length we can send in *one* 21585 * time. 21586 */ 21587 if (do_lso_send) { 21588 /* 21589 * Check whether need to do LSO any more. 21590 */ 21591 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21592 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21593 lso_usable = MIN(lso_usable, 21594 num_burst_seg * mss); 21595 21596 num_lso_seg = lso_usable / mss; 21597 if (lso_usable % mss) { 21598 num_lso_seg++; 21599 tcp->tcp_last_sent_len = (ushort_t) 21600 (lso_usable % mss); 21601 } else { 21602 tcp->tcp_last_sent_len = (ushort_t)mss; 21603 } 21604 } else { 21605 do_lso_send = B_FALSE; 21606 num_lso_seg = 1; 21607 lso_usable = mss; 21608 } 21609 } 21610 21611 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21612 21613 /* 21614 * Adjust num_burst_seg here. 21615 */ 21616 num_burst_seg -= num_lso_seg; 21617 21618 len = mss; 21619 if (len > *usable) { 21620 ASSERT(do_lso_send == B_FALSE); 21621 21622 len = *usable; 21623 if (len <= 0) { 21624 /* Terminate the loop */ 21625 break; /* success; too small */ 21626 } 21627 /* 21628 * Sender silly-window avoidance. 21629 * Ignore this if we are going to send a 21630 * zero window probe out. 21631 * 21632 * TODO: force data into microscopic window? 21633 * ==> (!pushed || (unsent > usable)) 21634 */ 21635 if (len < (tcp->tcp_max_swnd >> 1) && 21636 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21637 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21638 len == 1) && (! tcp->tcp_zero_win_probe)) { 21639 /* 21640 * If the retransmit timer is not running 21641 * we start it so that we will retransmit 21642 * in the case when the the receiver has 21643 * decremented the window. 21644 */ 21645 if (*snxt == tcp->tcp_snxt && 21646 *snxt == tcp->tcp_suna) { 21647 /* 21648 * We are not supposed to send 21649 * anything. So let's wait a little 21650 * bit longer before breaking SWS 21651 * avoidance. 21652 * 21653 * What should the value be? 21654 * Suggestion: MAX(init rexmit time, 21655 * tcp->tcp_rto) 21656 */ 21657 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21658 } 21659 break; /* success; too small */ 21660 } 21661 } 21662 21663 tcph = tcp->tcp_tcph; 21664 21665 /* 21666 * The reason to adjust len here is that we need to set flags 21667 * and calculate checksum. 21668 */ 21669 if (do_lso_send) 21670 len = lso_usable; 21671 21672 *usable -= len; /* Approximate - can be adjusted later */ 21673 if (*usable > 0) 21674 tcph->th_flags[0] = TH_ACK; 21675 else 21676 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21677 21678 /* 21679 * Prime pump for IP's checksumming on our behalf 21680 * Include the adjustment for a source route if any. 21681 */ 21682 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21683 sum = (sum >> 16) + (sum & 0xFFFF); 21684 U16_TO_ABE16(sum, tcph->th_sum); 21685 21686 U32_TO_ABE32(*snxt, tcph->th_seq); 21687 21688 /* 21689 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21690 * set. For the case when TCP_FSS_VALID is the only valid 21691 * bit (normal active close), branch off only when we think 21692 * that the FIN flag needs to be set. Note for this case, 21693 * that (snxt + len) may not reflect the actual seg_len, 21694 * as len may be further reduced in tcp_xmit_mp(). If len 21695 * gets modified, we will end up here again. 21696 */ 21697 if (tcp->tcp_valid_bits != 0 && 21698 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21699 ((*snxt + len) == tcp->tcp_fss))) { 21700 uchar_t *prev_rptr; 21701 uint32_t prev_snxt = tcp->tcp_snxt; 21702 21703 if (*tail_unsent == 0) { 21704 ASSERT((*xmit_tail)->b_cont != NULL); 21705 *xmit_tail = (*xmit_tail)->b_cont; 21706 prev_rptr = (*xmit_tail)->b_rptr; 21707 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21708 (*xmit_tail)->b_rptr); 21709 } else { 21710 prev_rptr = (*xmit_tail)->b_rptr; 21711 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21712 *tail_unsent; 21713 } 21714 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21715 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21716 /* Restore tcp_snxt so we get amount sent right. */ 21717 tcp->tcp_snxt = prev_snxt; 21718 if (prev_rptr == (*xmit_tail)->b_rptr) { 21719 /* 21720 * If the previous timestamp is still in use, 21721 * don't stomp on it. 21722 */ 21723 if ((*xmit_tail)->b_next == NULL) { 21724 (*xmit_tail)->b_prev = local_time; 21725 (*xmit_tail)->b_next = 21726 (mblk_t *)(uintptr_t)(*snxt); 21727 } 21728 } else 21729 (*xmit_tail)->b_rptr = prev_rptr; 21730 21731 if (mp == NULL) { 21732 if (ire != NULL) 21733 IRE_REFRELE(ire); 21734 return (-1); 21735 } 21736 mp1 = mp->b_cont; 21737 21738 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21739 tcp->tcp_last_sent_len = (ushort_t)len; 21740 while (mp1->b_cont) { 21741 *xmit_tail = (*xmit_tail)->b_cont; 21742 (*xmit_tail)->b_prev = local_time; 21743 (*xmit_tail)->b_next = 21744 (mblk_t *)(uintptr_t)(*snxt); 21745 mp1 = mp1->b_cont; 21746 } 21747 *snxt += len; 21748 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21749 BUMP_LOCAL(tcp->tcp_obsegs); 21750 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21751 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21752 tcp_send_data(tcp, q, mp); 21753 continue; 21754 } 21755 21756 *snxt += len; /* Adjust later if we don't send all of len */ 21757 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21758 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21759 21760 if (*tail_unsent) { 21761 /* Are the bytes above us in flight? */ 21762 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21763 if (rptr != (*xmit_tail)->b_rptr) { 21764 *tail_unsent -= len; 21765 if (len <= mss) /* LSO is unusable */ 21766 tcp->tcp_last_sent_len = (ushort_t)len; 21767 len += tcp_hdr_len; 21768 if (tcp->tcp_ipversion == IPV4_VERSION) 21769 tcp->tcp_ipha->ipha_length = htons(len); 21770 else 21771 tcp->tcp_ip6h->ip6_plen = 21772 htons(len - 21773 ((char *)&tcp->tcp_ip6h[1] - 21774 tcp->tcp_iphc)); 21775 mp = dupb(*xmit_tail); 21776 if (mp == NULL) { 21777 if (ire != NULL) 21778 IRE_REFRELE(ire); 21779 return (-1); /* out_of_mem */ 21780 } 21781 mp->b_rptr = rptr; 21782 /* 21783 * If the old timestamp is no longer in use, 21784 * sample a new timestamp now. 21785 */ 21786 if ((*xmit_tail)->b_next == NULL) { 21787 (*xmit_tail)->b_prev = local_time; 21788 (*xmit_tail)->b_next = 21789 (mblk_t *)(uintptr_t)(*snxt-len); 21790 } 21791 goto must_alloc; 21792 } 21793 } else { 21794 *xmit_tail = (*xmit_tail)->b_cont; 21795 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21796 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21797 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21798 (*xmit_tail)->b_rptr); 21799 } 21800 21801 (*xmit_tail)->b_prev = local_time; 21802 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21803 21804 *tail_unsent -= len; 21805 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21806 tcp->tcp_last_sent_len = (ushort_t)len; 21807 21808 len += tcp_hdr_len; 21809 if (tcp->tcp_ipversion == IPV4_VERSION) 21810 tcp->tcp_ipha->ipha_length = htons(len); 21811 else 21812 tcp->tcp_ip6h->ip6_plen = htons(len - 21813 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21814 21815 mp = dupb(*xmit_tail); 21816 if (mp == NULL) { 21817 if (ire != NULL) 21818 IRE_REFRELE(ire); 21819 return (-1); /* out_of_mem */ 21820 } 21821 21822 len = tcp_hdr_len; 21823 /* 21824 * There are four reasons to allocate a new hdr mblk: 21825 * 1) The bytes above us are in use by another packet 21826 * 2) We don't have good alignment 21827 * 3) The mblk is being shared 21828 * 4) We don't have enough room for a header 21829 */ 21830 rptr = mp->b_rptr - len; 21831 if (!OK_32PTR(rptr) || 21832 ((db = mp->b_datap), db->db_ref != 2) || 21833 rptr < db->db_base + ire_fp_mp_len) { 21834 /* NOTE: we assume allocb returns an OK_32PTR */ 21835 21836 must_alloc:; 21837 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21838 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21839 if (mp1 == NULL) { 21840 freemsg(mp); 21841 if (ire != NULL) 21842 IRE_REFRELE(ire); 21843 return (-1); /* out_of_mem */ 21844 } 21845 mp1->b_cont = mp; 21846 mp = mp1; 21847 /* Leave room for Link Level header */ 21848 len = tcp_hdr_len; 21849 rptr = 21850 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21851 mp->b_wptr = &rptr[len]; 21852 } 21853 21854 /* 21855 * Fill in the header using the template header, and add 21856 * options such as time-stamp, ECN and/or SACK, as needed. 21857 */ 21858 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21859 21860 mp->b_rptr = rptr; 21861 21862 if (*tail_unsent) { 21863 int spill = *tail_unsent; 21864 21865 mp1 = mp->b_cont; 21866 if (mp1 == NULL) 21867 mp1 = mp; 21868 21869 /* 21870 * If we're a little short, tack on more mblks until 21871 * there is no more spillover. 21872 */ 21873 while (spill < 0) { 21874 mblk_t *nmp; 21875 int nmpsz; 21876 21877 nmp = (*xmit_tail)->b_cont; 21878 nmpsz = MBLKL(nmp); 21879 21880 /* 21881 * Excess data in mblk; can we split it? 21882 * If MDT is enabled for the connection, 21883 * keep on splitting as this is a transient 21884 * send path. 21885 */ 21886 if (!do_lso_send && !tcp->tcp_mdt && 21887 (spill + nmpsz > 0)) { 21888 /* 21889 * Don't split if stream head was 21890 * told to break up larger writes 21891 * into smaller ones. 21892 */ 21893 if (tcp->tcp_maxpsz > 0) 21894 break; 21895 21896 /* 21897 * Next mblk is less than SMSS/2 21898 * rounded up to nearest 64-byte; 21899 * let it get sent as part of the 21900 * next segment. 21901 */ 21902 if (tcp->tcp_localnet && 21903 !tcp->tcp_cork && 21904 (nmpsz < roundup((mss >> 1), 64))) 21905 break; 21906 } 21907 21908 *xmit_tail = nmp; 21909 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21910 /* Stash for rtt use later */ 21911 (*xmit_tail)->b_prev = local_time; 21912 (*xmit_tail)->b_next = 21913 (mblk_t *)(uintptr_t)(*snxt - len); 21914 mp1->b_cont = dupb(*xmit_tail); 21915 mp1 = mp1->b_cont; 21916 21917 spill += nmpsz; 21918 if (mp1 == NULL) { 21919 *tail_unsent = spill; 21920 freemsg(mp); 21921 if (ire != NULL) 21922 IRE_REFRELE(ire); 21923 return (-1); /* out_of_mem */ 21924 } 21925 } 21926 21927 /* Trim back any surplus on the last mblk */ 21928 if (spill >= 0) { 21929 mp1->b_wptr -= spill; 21930 *tail_unsent = spill; 21931 } else { 21932 /* 21933 * We did not send everything we could in 21934 * order to remain within the b_cont limit. 21935 */ 21936 *usable -= spill; 21937 *snxt += spill; 21938 tcp->tcp_last_sent_len += spill; 21939 UPDATE_MIB(&tcps->tcps_mib, 21940 tcpOutDataBytes, spill); 21941 /* 21942 * Adjust the checksum 21943 */ 21944 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21945 sum += spill; 21946 sum = (sum >> 16) + (sum & 0xFFFF); 21947 U16_TO_ABE16(sum, tcph->th_sum); 21948 if (tcp->tcp_ipversion == IPV4_VERSION) { 21949 sum = ntohs( 21950 ((ipha_t *)rptr)->ipha_length) + 21951 spill; 21952 ((ipha_t *)rptr)->ipha_length = 21953 htons(sum); 21954 } else { 21955 sum = ntohs( 21956 ((ip6_t *)rptr)->ip6_plen) + 21957 spill; 21958 ((ip6_t *)rptr)->ip6_plen = 21959 htons(sum); 21960 } 21961 *tail_unsent = 0; 21962 } 21963 } 21964 if (tcp->tcp_ip_forward_progress) { 21965 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21966 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21967 tcp->tcp_ip_forward_progress = B_FALSE; 21968 } 21969 21970 if (do_lso_send) { 21971 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21972 num_lso_seg); 21973 tcp->tcp_obsegs += num_lso_seg; 21974 21975 TCP_STAT(tcps, tcp_lso_times); 21976 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21977 } else { 21978 tcp_send_data(tcp, q, mp); 21979 BUMP_LOCAL(tcp->tcp_obsegs); 21980 } 21981 } 21982 21983 if (ire != NULL) 21984 IRE_REFRELE(ire); 21985 return (0); 21986 } 21987 21988 /* Unlink and return any mblk that looks like it contains a MDT info */ 21989 static mblk_t * 21990 tcp_mdt_info_mp(mblk_t *mp) 21991 { 21992 mblk_t *prev_mp; 21993 21994 for (;;) { 21995 prev_mp = mp; 21996 /* no more to process? */ 21997 if ((mp = mp->b_cont) == NULL) 21998 break; 21999 22000 switch (DB_TYPE(mp)) { 22001 case M_CTL: 22002 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 22003 continue; 22004 ASSERT(prev_mp != NULL); 22005 prev_mp->b_cont = mp->b_cont; 22006 mp->b_cont = NULL; 22007 return (mp); 22008 default: 22009 break; 22010 } 22011 } 22012 return (mp); 22013 } 22014 22015 /* MDT info update routine, called when IP notifies us about MDT */ 22016 static void 22017 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 22018 { 22019 boolean_t prev_state; 22020 tcp_stack_t *tcps = tcp->tcp_tcps; 22021 22022 /* 22023 * IP is telling us to abort MDT on this connection? We know 22024 * this because the capability is only turned off when IP 22025 * encounters some pathological cases, e.g. link-layer change 22026 * where the new driver doesn't support MDT, or in situation 22027 * where MDT usage on the link-layer has been switched off. 22028 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 22029 * if the link-layer doesn't support MDT, and if it does, it 22030 * will indicate that the feature is to be turned on. 22031 */ 22032 prev_state = tcp->tcp_mdt; 22033 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 22034 if (!tcp->tcp_mdt && !first) { 22035 TCP_STAT(tcps, tcp_mdt_conn_halted3); 22036 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 22037 (void *)tcp->tcp_connp)); 22038 } 22039 22040 /* 22041 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 22042 * so disable MDT otherwise. The checks are done here 22043 * and in tcp_wput_data(). 22044 */ 22045 if (tcp->tcp_mdt && 22046 (tcp->tcp_ipversion == IPV4_VERSION && 22047 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22048 (tcp->tcp_ipversion == IPV6_VERSION && 22049 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 22050 tcp->tcp_mdt = B_FALSE; 22051 22052 if (tcp->tcp_mdt) { 22053 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 22054 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 22055 "version (%d), expected version is %d", 22056 mdt_capab->ill_mdt_version, MDT_VERSION_2); 22057 tcp->tcp_mdt = B_FALSE; 22058 return; 22059 } 22060 22061 /* 22062 * We need the driver to be able to handle at least three 22063 * spans per packet in order for tcp MDT to be utilized. 22064 * The first is for the header portion, while the rest are 22065 * needed to handle a packet that straddles across two 22066 * virtually non-contiguous buffers; a typical tcp packet 22067 * therefore consists of only two spans. Note that we take 22068 * a zero as "don't care". 22069 */ 22070 if (mdt_capab->ill_mdt_span_limit > 0 && 22071 mdt_capab->ill_mdt_span_limit < 3) { 22072 tcp->tcp_mdt = B_FALSE; 22073 return; 22074 } 22075 22076 /* a zero means driver wants default value */ 22077 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22078 tcps->tcps_mdt_max_pbufs); 22079 if (tcp->tcp_mdt_max_pld == 0) 22080 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22081 22082 /* ensure 32-bit alignment */ 22083 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22084 mdt_capab->ill_mdt_hdr_head), 4); 22085 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22086 mdt_capab->ill_mdt_hdr_tail), 4); 22087 22088 if (!first && !prev_state) { 22089 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22090 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22091 (void *)tcp->tcp_connp)); 22092 } 22093 } 22094 } 22095 22096 /* Unlink and return any mblk that looks like it contains a LSO info */ 22097 static mblk_t * 22098 tcp_lso_info_mp(mblk_t *mp) 22099 { 22100 mblk_t *prev_mp; 22101 22102 for (;;) { 22103 prev_mp = mp; 22104 /* no more to process? */ 22105 if ((mp = mp->b_cont) == NULL) 22106 break; 22107 22108 switch (DB_TYPE(mp)) { 22109 case M_CTL: 22110 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22111 continue; 22112 ASSERT(prev_mp != NULL); 22113 prev_mp->b_cont = mp->b_cont; 22114 mp->b_cont = NULL; 22115 return (mp); 22116 default: 22117 break; 22118 } 22119 } 22120 22121 return (mp); 22122 } 22123 22124 /* LSO info update routine, called when IP notifies us about LSO */ 22125 static void 22126 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22127 { 22128 tcp_stack_t *tcps = tcp->tcp_tcps; 22129 22130 /* 22131 * IP is telling us to abort LSO on this connection? We know 22132 * this because the capability is only turned off when IP 22133 * encounters some pathological cases, e.g. link-layer change 22134 * where the new NIC/driver doesn't support LSO, or in situation 22135 * where LSO usage on the link-layer has been switched off. 22136 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22137 * if the link-layer doesn't support LSO, and if it does, it 22138 * will indicate that the feature is to be turned on. 22139 */ 22140 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22141 TCP_STAT(tcps, tcp_lso_enabled); 22142 22143 /* 22144 * We currently only support LSO on simple TCP/IPv4, 22145 * so disable LSO otherwise. The checks are done here 22146 * and in tcp_wput_data(). 22147 */ 22148 if (tcp->tcp_lso && 22149 (tcp->tcp_ipversion == IPV4_VERSION && 22150 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22151 (tcp->tcp_ipversion == IPV6_VERSION)) { 22152 tcp->tcp_lso = B_FALSE; 22153 TCP_STAT(tcps, tcp_lso_disabled); 22154 } else { 22155 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22156 lso_capab->ill_lso_max); 22157 } 22158 } 22159 22160 static void 22161 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22162 { 22163 conn_t *connp = tcp->tcp_connp; 22164 tcp_stack_t *tcps = tcp->tcp_tcps; 22165 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22166 22167 ASSERT(ire != NULL); 22168 22169 /* 22170 * We may be in the fastpath here, and although we essentially do 22171 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22172 * we try to keep things as brief as possible. After all, these 22173 * are only best-effort checks, and we do more thorough ones prior 22174 * to calling tcp_send()/tcp_multisend(). 22175 */ 22176 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22177 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22178 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22179 !(ire->ire_flags & RTF_MULTIRT) && 22180 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22181 CONN_IS_LSO_MD_FASTPATH(connp)) { 22182 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22183 /* Cache the result */ 22184 connp->conn_lso_ok = B_TRUE; 22185 22186 ASSERT(ill->ill_lso_capab != NULL); 22187 if (!ill->ill_lso_capab->ill_lso_on) { 22188 ill->ill_lso_capab->ill_lso_on = 1; 22189 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22190 "LSO for interface %s\n", (void *)connp, 22191 ill->ill_name)); 22192 } 22193 tcp_lso_update(tcp, ill->ill_lso_capab); 22194 } else if (ipst->ips_ip_multidata_outbound && 22195 ILL_MDT_CAPABLE(ill)) { 22196 /* Cache the result */ 22197 connp->conn_mdt_ok = B_TRUE; 22198 22199 ASSERT(ill->ill_mdt_capab != NULL); 22200 if (!ill->ill_mdt_capab->ill_mdt_on) { 22201 ill->ill_mdt_capab->ill_mdt_on = 1; 22202 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22203 "MDT for interface %s\n", (void *)connp, 22204 ill->ill_name)); 22205 } 22206 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22207 } 22208 } 22209 22210 /* 22211 * The goal is to reduce the number of generated tcp segments by 22212 * setting the maxpsz multiplier to 0; this will have an affect on 22213 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22214 * into each packet, up to SMSS bytes. Doing this reduces the number 22215 * of outbound segments and incoming ACKs, thus allowing for better 22216 * network and system performance. In contrast the legacy behavior 22217 * may result in sending less than SMSS size, because the last mblk 22218 * for some packets may have more data than needed to make up SMSS, 22219 * and the legacy code refused to "split" it. 22220 * 22221 * We apply the new behavior on following situations: 22222 * 22223 * 1) Loopback connections, 22224 * 2) Connections in which the remote peer is not on local subnet, 22225 * 3) Local subnet connections over the bge interface (see below). 22226 * 22227 * Ideally, we would like this behavior to apply for interfaces other 22228 * than bge. However, doing so would negatively impact drivers which 22229 * perform dynamic mapping and unmapping of DMA resources, which are 22230 * increased by setting the maxpsz multiplier to 0 (more mblks per 22231 * packet will be generated by tcp). The bge driver does not suffer 22232 * from this, as it copies the mblks into pre-mapped buffers, and 22233 * therefore does not require more I/O resources than before. 22234 * 22235 * Otherwise, this behavior is present on all network interfaces when 22236 * the destination endpoint is non-local, since reducing the number 22237 * of packets in general is good for the network. 22238 * 22239 * TODO We need to remove this hard-coded conditional for bge once 22240 * a better "self-tuning" mechanism, or a way to comprehend 22241 * the driver transmit strategy is devised. Until the solution 22242 * is found and well understood, we live with this hack. 22243 */ 22244 if (!tcp_static_maxpsz && 22245 (tcp->tcp_loopback || !tcp->tcp_localnet || 22246 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22247 /* override the default value */ 22248 tcp->tcp_maxpsz = 0; 22249 22250 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22251 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22252 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22253 } 22254 22255 /* set the stream head parameters accordingly */ 22256 (void) tcp_maxpsz_set(tcp, B_TRUE); 22257 } 22258 22259 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22260 static void 22261 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22262 { 22263 uchar_t fval = *mp->b_rptr; 22264 mblk_t *tail; 22265 queue_t *q = tcp->tcp_wq; 22266 22267 /* TODO: How should flush interact with urgent data? */ 22268 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22269 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22270 /* 22271 * Flush only data that has not yet been put on the wire. If 22272 * we flush data that we have already transmitted, life, as we 22273 * know it, may come to an end. 22274 */ 22275 tail = tcp->tcp_xmit_tail; 22276 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22277 tcp->tcp_xmit_tail_unsent = 0; 22278 tcp->tcp_unsent = 0; 22279 if (tail->b_wptr != tail->b_rptr) 22280 tail = tail->b_cont; 22281 if (tail) { 22282 mblk_t **excess = &tcp->tcp_xmit_head; 22283 for (;;) { 22284 mblk_t *mp1 = *excess; 22285 if (mp1 == tail) 22286 break; 22287 tcp->tcp_xmit_tail = mp1; 22288 tcp->tcp_xmit_last = mp1; 22289 excess = &mp1->b_cont; 22290 } 22291 *excess = NULL; 22292 tcp_close_mpp(&tail); 22293 if (tcp->tcp_snd_zcopy_aware) 22294 tcp_zcopy_notify(tcp); 22295 } 22296 /* 22297 * We have no unsent data, so unsent must be less than 22298 * tcp_xmit_lowater, so re-enable flow. 22299 */ 22300 mutex_enter(&tcp->tcp_non_sq_lock); 22301 if (tcp->tcp_flow_stopped) { 22302 tcp_clrqfull(tcp); 22303 } 22304 mutex_exit(&tcp->tcp_non_sq_lock); 22305 } 22306 /* 22307 * TODO: you can't just flush these, you have to increase rwnd for one 22308 * thing. For another, how should urgent data interact? 22309 */ 22310 if (fval & FLUSHR) { 22311 *mp->b_rptr = fval & ~FLUSHW; 22312 /* XXX */ 22313 qreply(q, mp); 22314 return; 22315 } 22316 freemsg(mp); 22317 } 22318 22319 /* 22320 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22321 * messages. 22322 */ 22323 static void 22324 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22325 { 22326 mblk_t *mp1; 22327 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22328 STRUCT_HANDLE(strbuf, sb); 22329 queue_t *q = tcp->tcp_wq; 22330 int error; 22331 uint_t addrlen; 22332 22333 /* Make sure it is one of ours. */ 22334 switch (iocp->ioc_cmd) { 22335 case TI_GETMYNAME: 22336 case TI_GETPEERNAME: 22337 break; 22338 default: 22339 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22340 return; 22341 } 22342 switch (mi_copy_state(q, mp, &mp1)) { 22343 case -1: 22344 return; 22345 case MI_COPY_CASE(MI_COPY_IN, 1): 22346 break; 22347 case MI_COPY_CASE(MI_COPY_OUT, 1): 22348 /* Copy out the strbuf. */ 22349 mi_copyout(q, mp); 22350 return; 22351 case MI_COPY_CASE(MI_COPY_OUT, 2): 22352 /* All done. */ 22353 mi_copy_done(q, mp, 0); 22354 return; 22355 default: 22356 mi_copy_done(q, mp, EPROTO); 22357 return; 22358 } 22359 /* Check alignment of the strbuf */ 22360 if (!OK_32PTR(mp1->b_rptr)) { 22361 mi_copy_done(q, mp, EINVAL); 22362 return; 22363 } 22364 22365 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22366 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22367 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22368 mi_copy_done(q, mp, EINVAL); 22369 return; 22370 } 22371 22372 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22373 if (mp1 == NULL) 22374 return; 22375 22376 switch (iocp->ioc_cmd) { 22377 case TI_GETMYNAME: 22378 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22379 break; 22380 case TI_GETPEERNAME: 22381 error = tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22382 break; 22383 } 22384 22385 if (error != 0) { 22386 mi_copy_done(q, mp, error); 22387 } else { 22388 mp1->b_wptr += addrlen; 22389 STRUCT_FSET(sb, len, addrlen); 22390 22391 /* Copy out the address */ 22392 mi_copyout(q, mp); 22393 } 22394 } 22395 22396 /* 22397 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22398 * messages. 22399 */ 22400 /* ARGSUSED */ 22401 static void 22402 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22403 { 22404 conn_t *connp = (conn_t *)arg; 22405 tcp_t *tcp = connp->conn_tcp; 22406 queue_t *q = tcp->tcp_wq; 22407 struct iocblk *iocp; 22408 tcp_stack_t *tcps = tcp->tcp_tcps; 22409 22410 ASSERT(DB_TYPE(mp) == M_IOCTL); 22411 /* 22412 * Try and ASSERT the minimum possible references on the 22413 * conn early enough. Since we are executing on write side, 22414 * the connection is obviously not detached and that means 22415 * there is a ref each for TCP and IP. Since we are behind 22416 * the squeue, the minimum references needed are 3. If the 22417 * conn is in classifier hash list, there should be an 22418 * extra ref for that (we check both the possibilities). 22419 */ 22420 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22421 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22422 22423 iocp = (struct iocblk *)mp->b_rptr; 22424 switch (iocp->ioc_cmd) { 22425 case TCP_IOC_DEFAULT_Q: 22426 /* Wants to be the default wq. */ 22427 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22428 iocp->ioc_error = EPERM; 22429 iocp->ioc_count = 0; 22430 mp->b_datap->db_type = M_IOCACK; 22431 qreply(q, mp); 22432 return; 22433 } 22434 tcp_def_q_set(tcp, mp); 22435 return; 22436 case _SIOCSOCKFALLBACK: 22437 /* 22438 * Either sockmod is about to be popped and the socket 22439 * would now be treated as a plain stream, or a module 22440 * is about to be pushed so we could no longer use read- 22441 * side synchronous streams for fused loopback tcp. 22442 * Drain any queued data and disable direct sockfs 22443 * interface from now on. 22444 */ 22445 if (!tcp->tcp_issocket) { 22446 DB_TYPE(mp) = M_IOCNAK; 22447 iocp->ioc_error = EINVAL; 22448 } else { 22449 #ifdef _ILP32 22450 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22451 #else 22452 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22453 #endif 22454 /* 22455 * Insert this socket into the acceptor hash. 22456 * We might need it for T_CONN_RES message 22457 */ 22458 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22459 22460 if (tcp->tcp_fused) { 22461 /* 22462 * This is a fused loopback tcp; disable 22463 * read-side synchronous streams interface 22464 * and drain any queued data. It is okay 22465 * to do this for non-synchronous streams 22466 * fused tcp as well. 22467 */ 22468 tcp_fuse_disable_pair(tcp, B_FALSE); 22469 } 22470 tcp->tcp_issocket = B_FALSE; 22471 tcp->tcp_sodirect = NULL; 22472 TCP_STAT(tcps, tcp_sock_fallback); 22473 22474 DB_TYPE(mp) = M_IOCACK; 22475 iocp->ioc_error = 0; 22476 } 22477 iocp->ioc_count = 0; 22478 iocp->ioc_rval = 0; 22479 qreply(q, mp); 22480 return; 22481 } 22482 CALL_IP_WPUT(connp, q, mp); 22483 } 22484 22485 /* 22486 * This routine is called by tcp_wput() to handle all TPI requests. 22487 */ 22488 /* ARGSUSED */ 22489 static void 22490 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22491 { 22492 conn_t *connp = (conn_t *)arg; 22493 tcp_t *tcp = connp->conn_tcp; 22494 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22495 uchar_t *rptr; 22496 t_scalar_t type; 22497 int len; 22498 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22499 22500 /* 22501 * Try and ASSERT the minimum possible references on the 22502 * conn early enough. Since we are executing on write side, 22503 * the connection is obviously not detached and that means 22504 * there is a ref each for TCP and IP. Since we are behind 22505 * the squeue, the minimum references needed are 3. If the 22506 * conn is in classifier hash list, there should be an 22507 * extra ref for that (we check both the possibilities). 22508 */ 22509 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22510 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22511 22512 rptr = mp->b_rptr; 22513 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22514 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22515 type = ((union T_primitives *)rptr)->type; 22516 if (type == T_EXDATA_REQ) { 22517 uint32_t msize = msgdsize(mp->b_cont); 22518 22519 len = msize - 1; 22520 if (len < 0) { 22521 freemsg(mp); 22522 return; 22523 } 22524 /* 22525 * Try to force urgent data out on the wire. 22526 * Even if we have unsent data this will 22527 * at least send the urgent flag. 22528 * XXX does not handle more flag correctly. 22529 */ 22530 len += tcp->tcp_unsent; 22531 len += tcp->tcp_snxt; 22532 tcp->tcp_urg = len; 22533 tcp->tcp_valid_bits |= TCP_URG_VALID; 22534 22535 /* Bypass tcp protocol for fused tcp loopback */ 22536 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22537 return; 22538 } else if (type != T_DATA_REQ) { 22539 goto non_urgent_data; 22540 } 22541 /* TODO: options, flags, ... from user */ 22542 /* Set length to zero for reclamation below */ 22543 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22544 freeb(mp); 22545 return; 22546 } else { 22547 if (tcp->tcp_debug) { 22548 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22549 "tcp_wput_proto, dropping one..."); 22550 } 22551 freemsg(mp); 22552 return; 22553 } 22554 22555 non_urgent_data: 22556 22557 switch ((int)tprim->type) { 22558 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22559 /* 22560 * save the kssl_ent_t from the next block, and convert this 22561 * back to a normal bind_req. 22562 */ 22563 if (mp->b_cont != NULL) { 22564 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22565 22566 if (tcp->tcp_kssl_ent != NULL) { 22567 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22568 KSSL_NO_PROXY); 22569 tcp->tcp_kssl_ent = NULL; 22570 } 22571 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22572 sizeof (kssl_ent_t)); 22573 kssl_hold_ent(tcp->tcp_kssl_ent); 22574 freemsg(mp->b_cont); 22575 mp->b_cont = NULL; 22576 } 22577 tprim->type = T_BIND_REQ; 22578 22579 /* FALLTHROUGH */ 22580 case O_T_BIND_REQ: /* bind request */ 22581 case T_BIND_REQ: /* new semantics bind request */ 22582 tcp_bind(tcp, mp); 22583 break; 22584 case T_UNBIND_REQ: /* unbind request */ 22585 tcp_unbind(tcp, mp); 22586 break; 22587 case O_T_CONN_RES: /* old connection response XXX */ 22588 case T_CONN_RES: /* connection response */ 22589 tcp_accept(tcp, mp); 22590 break; 22591 case T_CONN_REQ: /* connection request */ 22592 tcp_connect(tcp, mp); 22593 break; 22594 case T_DISCON_REQ: /* disconnect request */ 22595 tcp_disconnect(tcp, mp); 22596 break; 22597 case T_CAPABILITY_REQ: 22598 tcp_capability_req(tcp, mp); /* capability request */ 22599 break; 22600 case T_INFO_REQ: /* information request */ 22601 tcp_info_req(tcp, mp); 22602 break; 22603 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22604 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22605 &tcp_opt_obj, B_TRUE); 22606 break; 22607 case T_OPTMGMT_REQ: 22608 /* 22609 * Note: no support for snmpcom_req() through new 22610 * T_OPTMGMT_REQ. See comments in ip.c 22611 */ 22612 /* Only IP is allowed to return meaningful value */ 22613 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22614 B_TRUE); 22615 break; 22616 22617 case T_UNITDATA_REQ: /* unitdata request */ 22618 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22619 break; 22620 case T_ORDREL_REQ: /* orderly release req */ 22621 freemsg(mp); 22622 22623 if (tcp->tcp_fused) 22624 tcp_unfuse(tcp); 22625 22626 if (tcp_xmit_end(tcp) != 0) { 22627 /* 22628 * We were crossing FINs and got a reset from 22629 * the other side. Just ignore it. 22630 */ 22631 if (tcp->tcp_debug) { 22632 (void) strlog(TCP_MOD_ID, 0, 1, 22633 SL_ERROR|SL_TRACE, 22634 "tcp_wput_proto, T_ORDREL_REQ out of " 22635 "state %s", 22636 tcp_display(tcp, NULL, 22637 DISP_ADDR_AND_PORT)); 22638 } 22639 } 22640 break; 22641 case T_ADDR_REQ: 22642 tcp_addr_req(tcp, mp); 22643 break; 22644 default: 22645 if (tcp->tcp_debug) { 22646 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22647 "tcp_wput_proto, bogus TPI msg, type %d", 22648 tprim->type); 22649 } 22650 /* 22651 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22652 * to recover. 22653 */ 22654 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22655 break; 22656 } 22657 } 22658 22659 /* 22660 * The TCP write service routine should never be called... 22661 */ 22662 /* ARGSUSED */ 22663 static void 22664 tcp_wsrv(queue_t *q) 22665 { 22666 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22667 22668 TCP_STAT(tcps, tcp_wsrv_called); 22669 } 22670 22671 /* Non overlapping byte exchanger */ 22672 static void 22673 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22674 { 22675 uchar_t uch; 22676 22677 while (len-- > 0) { 22678 uch = a[len]; 22679 a[len] = b[len]; 22680 b[len] = uch; 22681 } 22682 } 22683 22684 /* 22685 * Send out a control packet on the tcp connection specified. This routine 22686 * is typically called where we need a simple ACK or RST generated. 22687 */ 22688 static void 22689 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22690 { 22691 uchar_t *rptr; 22692 tcph_t *tcph; 22693 ipha_t *ipha = NULL; 22694 ip6_t *ip6h = NULL; 22695 uint32_t sum; 22696 int tcp_hdr_len; 22697 int tcp_ip_hdr_len; 22698 mblk_t *mp; 22699 tcp_stack_t *tcps = tcp->tcp_tcps; 22700 22701 /* 22702 * Save sum for use in source route later. 22703 */ 22704 ASSERT(tcp != NULL); 22705 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22706 tcp_hdr_len = tcp->tcp_hdr_len; 22707 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22708 22709 /* If a text string is passed in with the request, pass it to strlog. */ 22710 if (str != NULL && tcp->tcp_debug) { 22711 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22712 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22713 str, seq, ack, ctl); 22714 } 22715 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22716 BPRI_MED); 22717 if (mp == NULL) { 22718 return; 22719 } 22720 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22721 mp->b_rptr = rptr; 22722 mp->b_wptr = &rptr[tcp_hdr_len]; 22723 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22724 22725 if (tcp->tcp_ipversion == IPV4_VERSION) { 22726 ipha = (ipha_t *)rptr; 22727 ipha->ipha_length = htons(tcp_hdr_len); 22728 } else { 22729 ip6h = (ip6_t *)rptr; 22730 ASSERT(tcp != NULL); 22731 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22732 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22733 } 22734 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22735 tcph->th_flags[0] = (uint8_t)ctl; 22736 if (ctl & TH_RST) { 22737 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22738 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22739 /* 22740 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22741 */ 22742 if (tcp->tcp_snd_ts_ok && 22743 tcp->tcp_state > TCPS_SYN_SENT) { 22744 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22745 *(mp->b_wptr) = TCPOPT_EOL; 22746 if (tcp->tcp_ipversion == IPV4_VERSION) { 22747 ipha->ipha_length = htons(tcp_hdr_len - 22748 TCPOPT_REAL_TS_LEN); 22749 } else { 22750 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22751 TCPOPT_REAL_TS_LEN); 22752 } 22753 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22754 sum -= TCPOPT_REAL_TS_LEN; 22755 } 22756 } 22757 if (ctl & TH_ACK) { 22758 if (tcp->tcp_snd_ts_ok) { 22759 U32_TO_BE32(lbolt, 22760 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22761 U32_TO_BE32(tcp->tcp_ts_recent, 22762 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22763 } 22764 22765 /* Update the latest receive window size in TCP header. */ 22766 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22767 tcph->th_win); 22768 tcp->tcp_rack = ack; 22769 tcp->tcp_rack_cnt = 0; 22770 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22771 } 22772 BUMP_LOCAL(tcp->tcp_obsegs); 22773 U32_TO_BE32(seq, tcph->th_seq); 22774 U32_TO_BE32(ack, tcph->th_ack); 22775 /* 22776 * Include the adjustment for a source route if any. 22777 */ 22778 sum = (sum >> 16) + (sum & 0xFFFF); 22779 U16_TO_BE16(sum, tcph->th_sum); 22780 tcp_send_data(tcp, tcp->tcp_wq, mp); 22781 } 22782 22783 /* 22784 * If this routine returns B_TRUE, TCP can generate a RST in response 22785 * to a segment. If it returns B_FALSE, TCP should not respond. 22786 */ 22787 static boolean_t 22788 tcp_send_rst_chk(tcp_stack_t *tcps) 22789 { 22790 clock_t now; 22791 22792 /* 22793 * TCP needs to protect itself from generating too many RSTs. 22794 * This can be a DoS attack by sending us random segments 22795 * soliciting RSTs. 22796 * 22797 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22798 * in each 1 second interval. In this way, TCP still generate 22799 * RSTs in normal cases but when under attack, the impact is 22800 * limited. 22801 */ 22802 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22803 now = lbolt; 22804 /* lbolt can wrap around. */ 22805 if ((tcps->tcps_last_rst_intrvl > now) || 22806 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22807 1*SECONDS)) { 22808 tcps->tcps_last_rst_intrvl = now; 22809 tcps->tcps_rst_cnt = 1; 22810 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22811 return (B_FALSE); 22812 } 22813 } 22814 return (B_TRUE); 22815 } 22816 22817 /* 22818 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22819 */ 22820 static void 22821 tcp_ip_ire_mark_advice(tcp_t *tcp) 22822 { 22823 mblk_t *mp; 22824 ipic_t *ipic; 22825 22826 if (tcp->tcp_ipversion == IPV4_VERSION) { 22827 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22828 &ipic); 22829 } else { 22830 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22831 &ipic); 22832 } 22833 if (mp == NULL) 22834 return; 22835 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22836 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22837 } 22838 22839 /* 22840 * Return an IP advice ioctl mblk and set ipic to be the pointer 22841 * to the advice structure. 22842 */ 22843 static mblk_t * 22844 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22845 { 22846 struct iocblk *ioc; 22847 mblk_t *mp, *mp1; 22848 22849 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22850 if (mp == NULL) 22851 return (NULL); 22852 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22853 *ipic = (ipic_t *)mp->b_rptr; 22854 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22855 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22856 22857 bcopy(addr, *ipic + 1, addr_len); 22858 22859 (*ipic)->ipic_addr_length = addr_len; 22860 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22861 22862 mp1 = mkiocb(IP_IOCTL); 22863 if (mp1 == NULL) { 22864 freemsg(mp); 22865 return (NULL); 22866 } 22867 mp1->b_cont = mp; 22868 ioc = (struct iocblk *)mp1->b_rptr; 22869 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22870 22871 return (mp1); 22872 } 22873 22874 /* 22875 * Generate a reset based on an inbound packet, connp is set by caller 22876 * when RST is in response to an unexpected inbound packet for which 22877 * there is active tcp state in the system. 22878 * 22879 * IPSEC NOTE : Try to send the reply with the same protection as it came 22880 * in. We still have the ipsec_mp that the packet was attached to. Thus 22881 * the packet will go out at the same level of protection as it came in by 22882 * converting the IPSEC_IN to IPSEC_OUT. 22883 */ 22884 static void 22885 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22886 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22887 tcp_stack_t *tcps, conn_t *connp) 22888 { 22889 ipha_t *ipha = NULL; 22890 ip6_t *ip6h = NULL; 22891 ushort_t len; 22892 tcph_t *tcph; 22893 int i; 22894 mblk_t *ipsec_mp; 22895 boolean_t mctl_present; 22896 ipic_t *ipic; 22897 ipaddr_t v4addr; 22898 in6_addr_t v6addr; 22899 int addr_len; 22900 void *addr; 22901 queue_t *q = tcps->tcps_g_q; 22902 tcp_t *tcp; 22903 cred_t *cr; 22904 mblk_t *nmp; 22905 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22906 22907 if (tcps->tcps_g_q == NULL) { 22908 /* 22909 * For non-zero stackids the default queue isn't created 22910 * until the first open, thus there can be a need to send 22911 * a reset before then. But we can't do that, hence we just 22912 * drop the packet. Later during boot, when the default queue 22913 * has been setup, a retransmitted packet from the peer 22914 * will result in a reset. 22915 */ 22916 ASSERT(tcps->tcps_netstack->netstack_stackid != 22917 GLOBAL_NETSTACKID); 22918 freemsg(mp); 22919 return; 22920 } 22921 22922 if (connp != NULL) 22923 tcp = connp->conn_tcp; 22924 else 22925 tcp = Q_TO_TCP(q); 22926 22927 if (!tcp_send_rst_chk(tcps)) { 22928 tcps->tcps_rst_unsent++; 22929 freemsg(mp); 22930 return; 22931 } 22932 22933 if (mp->b_datap->db_type == M_CTL) { 22934 ipsec_mp = mp; 22935 mp = mp->b_cont; 22936 mctl_present = B_TRUE; 22937 } else { 22938 ipsec_mp = mp; 22939 mctl_present = B_FALSE; 22940 } 22941 22942 if (str && q && tcps->tcps_dbg) { 22943 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22944 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22945 "flags 0x%x", 22946 str, seq, ack, ctl); 22947 } 22948 if (mp->b_datap->db_ref != 1) { 22949 mblk_t *mp1 = copyb(mp); 22950 freemsg(mp); 22951 mp = mp1; 22952 if (!mp) { 22953 if (mctl_present) 22954 freeb(ipsec_mp); 22955 return; 22956 } else { 22957 if (mctl_present) { 22958 ipsec_mp->b_cont = mp; 22959 } else { 22960 ipsec_mp = mp; 22961 } 22962 } 22963 } else if (mp->b_cont) { 22964 freemsg(mp->b_cont); 22965 mp->b_cont = NULL; 22966 } 22967 /* 22968 * We skip reversing source route here. 22969 * (for now we replace all IP options with EOL) 22970 */ 22971 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22972 ipha = (ipha_t *)mp->b_rptr; 22973 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22974 mp->b_rptr[i] = IPOPT_EOL; 22975 /* 22976 * Make sure that src address isn't flagrantly invalid. 22977 * Not all broadcast address checking for the src address 22978 * is possible, since we don't know the netmask of the src 22979 * addr. No check for destination address is done, since 22980 * IP will not pass up a packet with a broadcast dest 22981 * address to TCP. Similar checks are done below for IPv6. 22982 */ 22983 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22984 CLASSD(ipha->ipha_src)) { 22985 freemsg(ipsec_mp); 22986 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22987 return; 22988 } 22989 } else { 22990 ip6h = (ip6_t *)mp->b_rptr; 22991 22992 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22993 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22994 freemsg(ipsec_mp); 22995 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22996 return; 22997 } 22998 22999 /* Remove any extension headers assuming partial overlay */ 23000 if (ip_hdr_len > IPV6_HDR_LEN) { 23001 uint8_t *to; 23002 23003 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 23004 ovbcopy(ip6h, to, IPV6_HDR_LEN); 23005 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 23006 ip_hdr_len = IPV6_HDR_LEN; 23007 ip6h = (ip6_t *)mp->b_rptr; 23008 ip6h->ip6_nxt = IPPROTO_TCP; 23009 } 23010 } 23011 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 23012 if (tcph->th_flags[0] & TH_RST) { 23013 freemsg(ipsec_mp); 23014 return; 23015 } 23016 tcph->th_offset_and_rsrvd[0] = (5 << 4); 23017 len = ip_hdr_len + sizeof (tcph_t); 23018 mp->b_wptr = &mp->b_rptr[len]; 23019 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23020 ipha->ipha_length = htons(len); 23021 /* Swap addresses */ 23022 v4addr = ipha->ipha_src; 23023 ipha->ipha_src = ipha->ipha_dst; 23024 ipha->ipha_dst = v4addr; 23025 ipha->ipha_ident = 0; 23026 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 23027 addr_len = IP_ADDR_LEN; 23028 addr = &v4addr; 23029 } else { 23030 /* No ip6i_t in this case */ 23031 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 23032 /* Swap addresses */ 23033 v6addr = ip6h->ip6_src; 23034 ip6h->ip6_src = ip6h->ip6_dst; 23035 ip6h->ip6_dst = v6addr; 23036 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 23037 addr_len = IPV6_ADDR_LEN; 23038 addr = &v6addr; 23039 } 23040 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 23041 U32_TO_BE32(ack, tcph->th_ack); 23042 U32_TO_BE32(seq, tcph->th_seq); 23043 U16_TO_BE16(0, tcph->th_win); 23044 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 23045 tcph->th_flags[0] = (uint8_t)ctl; 23046 if (ctl & TH_RST) { 23047 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 23048 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23049 } 23050 23051 /* IP trusts us to set up labels when required. */ 23052 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 23053 crgetlabel(cr) != NULL) { 23054 int err; 23055 23056 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23057 err = tsol_check_label(cr, &mp, 23058 tcp->tcp_connp->conn_mac_exempt, 23059 tcps->tcps_netstack->netstack_ip); 23060 else 23061 err = tsol_check_label_v6(cr, &mp, 23062 tcp->tcp_connp->conn_mac_exempt, 23063 tcps->tcps_netstack->netstack_ip); 23064 if (mctl_present) 23065 ipsec_mp->b_cont = mp; 23066 else 23067 ipsec_mp = mp; 23068 if (err != 0) { 23069 freemsg(ipsec_mp); 23070 return; 23071 } 23072 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23073 ipha = (ipha_t *)mp->b_rptr; 23074 } else { 23075 ip6h = (ip6_t *)mp->b_rptr; 23076 } 23077 } 23078 23079 if (mctl_present) { 23080 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23081 23082 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23083 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23084 return; 23085 } 23086 } 23087 if (zoneid == ALL_ZONES) 23088 zoneid = GLOBAL_ZONEID; 23089 23090 /* Add the zoneid so ip_output routes it properly */ 23091 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23092 freemsg(ipsec_mp); 23093 return; 23094 } 23095 ipsec_mp = nmp; 23096 23097 /* 23098 * NOTE: one might consider tracing a TCP packet here, but 23099 * this function has no active TCP state and no tcp structure 23100 * that has a trace buffer. If we traced here, we would have 23101 * to keep a local trace buffer in tcp_record_trace(). 23102 * 23103 * TSol note: The mblk that contains the incoming packet was 23104 * reused by tcp_xmit_listener_reset, so it already contains 23105 * the right credentials and we don't need to call mblk_setcred. 23106 * Also the conn's cred is not right since it is associated 23107 * with tcps_g_q. 23108 */ 23109 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23110 23111 /* 23112 * Tell IP to mark the IRE used for this destination temporary. 23113 * This way, we can limit our exposure to DoS attack because IP 23114 * creates an IRE for each destination. If there are too many, 23115 * the time to do any routing lookup will be extremely long. And 23116 * the lookup can be in interrupt context. 23117 * 23118 * Note that in normal circumstances, this marking should not 23119 * affect anything. It would be nice if only 1 message is 23120 * needed to inform IP that the IRE created for this RST should 23121 * not be added to the cache table. But there is currently 23122 * not such communication mechanism between TCP and IP. So 23123 * the best we can do now is to send the advice ioctl to IP 23124 * to mark the IRE temporary. 23125 */ 23126 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23127 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23128 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23129 } 23130 } 23131 23132 /* 23133 * Initiate closedown sequence on an active connection. (May be called as 23134 * writer.) Return value zero for OK return, non-zero for error return. 23135 */ 23136 static int 23137 tcp_xmit_end(tcp_t *tcp) 23138 { 23139 ipic_t *ipic; 23140 mblk_t *mp; 23141 tcp_stack_t *tcps = tcp->tcp_tcps; 23142 23143 if (tcp->tcp_state < TCPS_SYN_RCVD || 23144 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23145 /* 23146 * Invalid state, only states TCPS_SYN_RCVD, 23147 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23148 */ 23149 return (-1); 23150 } 23151 23152 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23153 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23154 /* 23155 * If there is nothing more unsent, send the FIN now. 23156 * Otherwise, it will go out with the last segment. 23157 */ 23158 if (tcp->tcp_unsent == 0) { 23159 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23160 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23161 23162 if (mp) { 23163 tcp_send_data(tcp, tcp->tcp_wq, mp); 23164 } else { 23165 /* 23166 * Couldn't allocate msg. Pretend we got it out. 23167 * Wait for rexmit timeout. 23168 */ 23169 tcp->tcp_snxt = tcp->tcp_fss + 1; 23170 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23171 } 23172 23173 /* 23174 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23175 * changed. 23176 */ 23177 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23178 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23179 } 23180 } else { 23181 /* 23182 * If tcp->tcp_cork is set, then the data will not get sent, 23183 * so we have to check that and unset it first. 23184 */ 23185 if (tcp->tcp_cork) 23186 tcp->tcp_cork = B_FALSE; 23187 tcp_wput_data(tcp, NULL, B_FALSE); 23188 } 23189 23190 /* 23191 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23192 * is 0, don't update the cache. 23193 */ 23194 if (tcps->tcps_rtt_updates == 0 || 23195 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23196 return (0); 23197 23198 /* 23199 * NOTE: should not update if source routes i.e. if tcp_remote if 23200 * different from the destination. 23201 */ 23202 if (tcp->tcp_ipversion == IPV4_VERSION) { 23203 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23204 return (0); 23205 } 23206 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23207 &ipic); 23208 } else { 23209 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23210 &tcp->tcp_ip6h->ip6_dst))) { 23211 return (0); 23212 } 23213 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23214 &ipic); 23215 } 23216 23217 /* Record route attributes in the IRE for use by future connections. */ 23218 if (mp == NULL) 23219 return (0); 23220 23221 /* 23222 * We do not have a good algorithm to update ssthresh at this time. 23223 * So don't do any update. 23224 */ 23225 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23226 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23227 23228 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23229 return (0); 23230 } 23231 23232 /* 23233 * Generate a "no listener here" RST in response to an "unknown" segment. 23234 * connp is set by caller when RST is in response to an unexpected 23235 * inbound packet for which there is active tcp state in the system. 23236 * Note that we are reusing the incoming mp to construct the outgoing RST. 23237 */ 23238 void 23239 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23240 tcp_stack_t *tcps, conn_t *connp) 23241 { 23242 uchar_t *rptr; 23243 uint32_t seg_len; 23244 tcph_t *tcph; 23245 uint32_t seg_seq; 23246 uint32_t seg_ack; 23247 uint_t flags; 23248 mblk_t *ipsec_mp; 23249 ipha_t *ipha; 23250 ip6_t *ip6h; 23251 boolean_t mctl_present = B_FALSE; 23252 boolean_t check = B_TRUE; 23253 boolean_t policy_present; 23254 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23255 23256 TCP_STAT(tcps, tcp_no_listener); 23257 23258 ipsec_mp = mp; 23259 23260 if (mp->b_datap->db_type == M_CTL) { 23261 ipsec_in_t *ii; 23262 23263 mctl_present = B_TRUE; 23264 mp = mp->b_cont; 23265 23266 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23267 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23268 if (ii->ipsec_in_dont_check) { 23269 check = B_FALSE; 23270 if (!ii->ipsec_in_secure) { 23271 freeb(ipsec_mp); 23272 mctl_present = B_FALSE; 23273 ipsec_mp = mp; 23274 } 23275 } 23276 } 23277 23278 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23279 policy_present = ipss->ipsec_inbound_v4_policy_present; 23280 ipha = (ipha_t *)mp->b_rptr; 23281 ip6h = NULL; 23282 } else { 23283 policy_present = ipss->ipsec_inbound_v6_policy_present; 23284 ipha = NULL; 23285 ip6h = (ip6_t *)mp->b_rptr; 23286 } 23287 23288 if (check && policy_present) { 23289 /* 23290 * The conn_t parameter is NULL because we already know 23291 * nobody's home. 23292 */ 23293 ipsec_mp = ipsec_check_global_policy( 23294 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23295 tcps->tcps_netstack); 23296 if (ipsec_mp == NULL) 23297 return; 23298 } 23299 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23300 DTRACE_PROBE2( 23301 tx__ip__log__error__nolistener__tcp, 23302 char *, "Could not reply with RST to mp(1)", 23303 mblk_t *, mp); 23304 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23305 freemsg(ipsec_mp); 23306 return; 23307 } 23308 23309 rptr = mp->b_rptr; 23310 23311 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23312 seg_seq = BE32_TO_U32(tcph->th_seq); 23313 seg_ack = BE32_TO_U32(tcph->th_ack); 23314 flags = tcph->th_flags[0]; 23315 23316 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23317 if (flags & TH_RST) { 23318 freemsg(ipsec_mp); 23319 } else if (flags & TH_ACK) { 23320 tcp_xmit_early_reset("no tcp, reset", 23321 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23322 connp); 23323 } else { 23324 if (flags & TH_SYN) { 23325 seg_len++; 23326 } else { 23327 /* 23328 * Here we violate the RFC. Note that a normal 23329 * TCP will never send a segment without the ACK 23330 * flag, except for RST or SYN segment. This 23331 * segment is neither. Just drop it on the 23332 * floor. 23333 */ 23334 freemsg(ipsec_mp); 23335 tcps->tcps_rst_unsent++; 23336 return; 23337 } 23338 23339 tcp_xmit_early_reset("no tcp, reset/ack", 23340 ipsec_mp, 0, seg_seq + seg_len, 23341 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23342 } 23343 } 23344 23345 /* 23346 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23347 * ip and tcp header ready to pass down to IP. If the mp passed in is 23348 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23349 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23350 * otherwise it will dup partial mblks.) 23351 * Otherwise, an appropriate ACK packet will be generated. This 23352 * routine is not usually called to send new data for the first time. It 23353 * is mostly called out of the timer for retransmits, and to generate ACKs. 23354 * 23355 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23356 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23357 * of the original mblk chain will be returned in *offset and *end_mp. 23358 */ 23359 mblk_t * 23360 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23361 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23362 boolean_t rexmit) 23363 { 23364 int data_length; 23365 int32_t off = 0; 23366 uint_t flags; 23367 mblk_t *mp1; 23368 mblk_t *mp2; 23369 uchar_t *rptr; 23370 tcph_t *tcph; 23371 int32_t num_sack_blk = 0; 23372 int32_t sack_opt_len = 0; 23373 tcp_stack_t *tcps = tcp->tcp_tcps; 23374 23375 /* Allocate for our maximum TCP header + link-level */ 23376 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23377 tcps->tcps_wroff_xtra, BPRI_MED); 23378 if (!mp1) 23379 return (NULL); 23380 data_length = 0; 23381 23382 /* 23383 * Note that tcp_mss has been adjusted to take into account the 23384 * timestamp option if applicable. Because SACK options do not 23385 * appear in every TCP segments and they are of variable lengths, 23386 * they cannot be included in tcp_mss. Thus we need to calculate 23387 * the actual segment length when we need to send a segment which 23388 * includes SACK options. 23389 */ 23390 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23391 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23392 tcp->tcp_num_sack_blk); 23393 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23394 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23395 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23396 max_to_send -= sack_opt_len; 23397 } 23398 23399 if (offset != NULL) { 23400 off = *offset; 23401 /* We use offset as an indicator that end_mp is not NULL. */ 23402 *end_mp = NULL; 23403 } 23404 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23405 /* This could be faster with cooperation from downstream */ 23406 if (mp2 != mp1 && !sendall && 23407 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23408 max_to_send) 23409 /* 23410 * Don't send the next mblk since the whole mblk 23411 * does not fit. 23412 */ 23413 break; 23414 mp2->b_cont = dupb(mp); 23415 mp2 = mp2->b_cont; 23416 if (!mp2) { 23417 freemsg(mp1); 23418 return (NULL); 23419 } 23420 mp2->b_rptr += off; 23421 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23422 (uintptr_t)INT_MAX); 23423 23424 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23425 if (data_length > max_to_send) { 23426 mp2->b_wptr -= data_length - max_to_send; 23427 data_length = max_to_send; 23428 off = mp2->b_wptr - mp->b_rptr; 23429 break; 23430 } else { 23431 off = 0; 23432 } 23433 } 23434 if (offset != NULL) { 23435 *offset = off; 23436 *end_mp = mp; 23437 } 23438 if (seg_len != NULL) { 23439 *seg_len = data_length; 23440 } 23441 23442 /* Update the latest receive window size in TCP header. */ 23443 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23444 tcp->tcp_tcph->th_win); 23445 23446 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23447 mp1->b_rptr = rptr; 23448 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23449 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23450 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23451 U32_TO_ABE32(seq, tcph->th_seq); 23452 23453 /* 23454 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23455 * that this function was called from tcp_wput_data. Thus, when called 23456 * to retransmit data the setting of the PUSH bit may appear some 23457 * what random in that it might get set when it should not. This 23458 * should not pose any performance issues. 23459 */ 23460 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23461 tcp->tcp_unsent == data_length)) { 23462 flags = TH_ACK | TH_PUSH; 23463 } else { 23464 flags = TH_ACK; 23465 } 23466 23467 if (tcp->tcp_ecn_ok) { 23468 if (tcp->tcp_ecn_echo_on) 23469 flags |= TH_ECE; 23470 23471 /* 23472 * Only set ECT bit and ECN_CWR if a segment contains new data. 23473 * There is no TCP flow control for non-data segments, and 23474 * only data segment is transmitted reliably. 23475 */ 23476 if (data_length > 0 && !rexmit) { 23477 SET_ECT(tcp, rptr); 23478 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23479 flags |= TH_CWR; 23480 tcp->tcp_ecn_cwr_sent = B_TRUE; 23481 } 23482 } 23483 } 23484 23485 if (tcp->tcp_valid_bits) { 23486 uint32_t u1; 23487 23488 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23489 seq == tcp->tcp_iss) { 23490 uchar_t *wptr; 23491 23492 /* 23493 * If TCP_ISS_VALID and the seq number is tcp_iss, 23494 * TCP can only be in SYN-SENT, SYN-RCVD or 23495 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23496 * our SYN is not ack'ed but the app closes this 23497 * TCP connection. 23498 */ 23499 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23500 tcp->tcp_state == TCPS_SYN_RCVD || 23501 tcp->tcp_state == TCPS_FIN_WAIT_1); 23502 23503 /* 23504 * Tack on the MSS option. It is always needed 23505 * for both active and passive open. 23506 * 23507 * MSS option value should be interface MTU - MIN 23508 * TCP/IP header according to RFC 793 as it means 23509 * the maximum segment size TCP can receive. But 23510 * to get around some broken middle boxes/end hosts 23511 * out there, we allow the option value to be the 23512 * same as the MSS option size on the peer side. 23513 * In this way, the other side will not send 23514 * anything larger than they can receive. 23515 * 23516 * Note that for SYN_SENT state, the ndd param 23517 * tcp_use_smss_as_mss_opt has no effect as we 23518 * don't know the peer's MSS option value. So 23519 * the only case we need to take care of is in 23520 * SYN_RCVD state, which is done later. 23521 */ 23522 wptr = mp1->b_wptr; 23523 wptr[0] = TCPOPT_MAXSEG; 23524 wptr[1] = TCPOPT_MAXSEG_LEN; 23525 wptr += 2; 23526 u1 = tcp->tcp_if_mtu - 23527 (tcp->tcp_ipversion == IPV4_VERSION ? 23528 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23529 TCP_MIN_HEADER_LENGTH; 23530 U16_TO_BE16(u1, wptr); 23531 mp1->b_wptr = wptr + 2; 23532 /* Update the offset to cover the additional word */ 23533 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23534 23535 /* 23536 * Note that the following way of filling in 23537 * TCP options are not optimal. Some NOPs can 23538 * be saved. But there is no need at this time 23539 * to optimize it. When it is needed, we will 23540 * do it. 23541 */ 23542 switch (tcp->tcp_state) { 23543 case TCPS_SYN_SENT: 23544 flags = TH_SYN; 23545 23546 if (tcp->tcp_snd_ts_ok) { 23547 uint32_t llbolt = (uint32_t)lbolt; 23548 23549 wptr = mp1->b_wptr; 23550 wptr[0] = TCPOPT_NOP; 23551 wptr[1] = TCPOPT_NOP; 23552 wptr[2] = TCPOPT_TSTAMP; 23553 wptr[3] = TCPOPT_TSTAMP_LEN; 23554 wptr += 4; 23555 U32_TO_BE32(llbolt, wptr); 23556 wptr += 4; 23557 ASSERT(tcp->tcp_ts_recent == 0); 23558 U32_TO_BE32(0L, wptr); 23559 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23560 tcph->th_offset_and_rsrvd[0] += 23561 (3 << 4); 23562 } 23563 23564 /* 23565 * Set up all the bits to tell other side 23566 * we are ECN capable. 23567 */ 23568 if (tcp->tcp_ecn_ok) { 23569 flags |= (TH_ECE | TH_CWR); 23570 } 23571 break; 23572 case TCPS_SYN_RCVD: 23573 flags |= TH_SYN; 23574 23575 /* 23576 * Reset the MSS option value to be SMSS 23577 * We should probably add back the bytes 23578 * for timestamp option and IPsec. We 23579 * don't do that as this is a workaround 23580 * for broken middle boxes/end hosts, it 23581 * is better for us to be more cautious. 23582 * They may not take these things into 23583 * account in their SMSS calculation. Thus 23584 * the peer's calculated SMSS may be smaller 23585 * than what it can be. This should be OK. 23586 */ 23587 if (tcps->tcps_use_smss_as_mss_opt) { 23588 u1 = tcp->tcp_mss; 23589 U16_TO_BE16(u1, wptr); 23590 } 23591 23592 /* 23593 * If the other side is ECN capable, reply 23594 * that we are also ECN capable. 23595 */ 23596 if (tcp->tcp_ecn_ok) 23597 flags |= TH_ECE; 23598 break; 23599 default: 23600 /* 23601 * The above ASSERT() makes sure that this 23602 * must be FIN-WAIT-1 state. Our SYN has 23603 * not been ack'ed so retransmit it. 23604 */ 23605 flags |= TH_SYN; 23606 break; 23607 } 23608 23609 if (tcp->tcp_snd_ws_ok) { 23610 wptr = mp1->b_wptr; 23611 wptr[0] = TCPOPT_NOP; 23612 wptr[1] = TCPOPT_WSCALE; 23613 wptr[2] = TCPOPT_WS_LEN; 23614 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23615 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23616 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23617 } 23618 23619 if (tcp->tcp_snd_sack_ok) { 23620 wptr = mp1->b_wptr; 23621 wptr[0] = TCPOPT_NOP; 23622 wptr[1] = TCPOPT_NOP; 23623 wptr[2] = TCPOPT_SACK_PERMITTED; 23624 wptr[3] = TCPOPT_SACK_OK_LEN; 23625 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23626 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23627 } 23628 23629 /* allocb() of adequate mblk assures space */ 23630 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23631 (uintptr_t)INT_MAX); 23632 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23633 /* 23634 * Get IP set to checksum on our behalf 23635 * Include the adjustment for a source route if any. 23636 */ 23637 u1 += tcp->tcp_sum; 23638 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23639 U16_TO_BE16(u1, tcph->th_sum); 23640 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23641 } 23642 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23643 (seq + data_length) == tcp->tcp_fss) { 23644 if (!tcp->tcp_fin_acked) { 23645 flags |= TH_FIN; 23646 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23647 } 23648 if (!tcp->tcp_fin_sent) { 23649 tcp->tcp_fin_sent = B_TRUE; 23650 switch (tcp->tcp_state) { 23651 case TCPS_SYN_RCVD: 23652 case TCPS_ESTABLISHED: 23653 tcp->tcp_state = TCPS_FIN_WAIT_1; 23654 break; 23655 case TCPS_CLOSE_WAIT: 23656 tcp->tcp_state = TCPS_LAST_ACK; 23657 break; 23658 } 23659 if (tcp->tcp_suna == tcp->tcp_snxt) 23660 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23661 tcp->tcp_snxt = tcp->tcp_fss + 1; 23662 } 23663 } 23664 /* 23665 * Note the trick here. u1 is unsigned. When tcp_urg 23666 * is smaller than seq, u1 will become a very huge value. 23667 * So the comparison will fail. Also note that tcp_urp 23668 * should be positive, see RFC 793 page 17. 23669 */ 23670 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23671 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23672 u1 < (uint32_t)(64 * 1024)) { 23673 flags |= TH_URG; 23674 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23675 U32_TO_ABE16(u1, tcph->th_urp); 23676 } 23677 } 23678 tcph->th_flags[0] = (uchar_t)flags; 23679 tcp->tcp_rack = tcp->tcp_rnxt; 23680 tcp->tcp_rack_cnt = 0; 23681 23682 if (tcp->tcp_snd_ts_ok) { 23683 if (tcp->tcp_state != TCPS_SYN_SENT) { 23684 uint32_t llbolt = (uint32_t)lbolt; 23685 23686 U32_TO_BE32(llbolt, 23687 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23688 U32_TO_BE32(tcp->tcp_ts_recent, 23689 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23690 } 23691 } 23692 23693 if (num_sack_blk > 0) { 23694 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23695 sack_blk_t *tmp; 23696 int32_t i; 23697 23698 wptr[0] = TCPOPT_NOP; 23699 wptr[1] = TCPOPT_NOP; 23700 wptr[2] = TCPOPT_SACK; 23701 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23702 sizeof (sack_blk_t); 23703 wptr += TCPOPT_REAL_SACK_LEN; 23704 23705 tmp = tcp->tcp_sack_list; 23706 for (i = 0; i < num_sack_blk; i++) { 23707 U32_TO_BE32(tmp[i].begin, wptr); 23708 wptr += sizeof (tcp_seq); 23709 U32_TO_BE32(tmp[i].end, wptr); 23710 wptr += sizeof (tcp_seq); 23711 } 23712 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23713 } 23714 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23715 data_length += (int)(mp1->b_wptr - rptr); 23716 if (tcp->tcp_ipversion == IPV4_VERSION) { 23717 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23718 } else { 23719 ip6_t *ip6 = (ip6_t *)(rptr + 23720 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23721 sizeof (ip6i_t) : 0)); 23722 23723 ip6->ip6_plen = htons(data_length - 23724 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23725 } 23726 23727 /* 23728 * Prime pump for IP 23729 * Include the adjustment for a source route if any. 23730 */ 23731 data_length -= tcp->tcp_ip_hdr_len; 23732 data_length += tcp->tcp_sum; 23733 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23734 U16_TO_ABE16(data_length, tcph->th_sum); 23735 if (tcp->tcp_ip_forward_progress) { 23736 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23737 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23738 tcp->tcp_ip_forward_progress = B_FALSE; 23739 } 23740 return (mp1); 23741 } 23742 23743 /* This function handles the push timeout. */ 23744 void 23745 tcp_push_timer(void *arg) 23746 { 23747 conn_t *connp = (conn_t *)arg; 23748 tcp_t *tcp = connp->conn_tcp; 23749 tcp_stack_t *tcps = tcp->tcp_tcps; 23750 uint_t flags; 23751 sodirect_t *sodp; 23752 23753 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23754 23755 ASSERT(tcp->tcp_listener == NULL); 23756 23757 /* 23758 * We need to plug synchronous streams during our drain to prevent 23759 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23760 */ 23761 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23762 tcp->tcp_push_tid = 0; 23763 23764 SOD_PTR_ENTER(tcp, sodp); 23765 if (sodp != NULL) { 23766 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23767 /* sod_wakeup() does the mutex_exit() */ 23768 } else if (tcp->tcp_rcv_list != NULL) { 23769 flags = tcp_rcv_drain(tcp->tcp_rq, tcp); 23770 } 23771 if (flags == TH_ACK_NEEDED) 23772 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23773 23774 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23775 } 23776 23777 /* 23778 * This function handles delayed ACK timeout. 23779 */ 23780 static void 23781 tcp_ack_timer(void *arg) 23782 { 23783 conn_t *connp = (conn_t *)arg; 23784 tcp_t *tcp = connp->conn_tcp; 23785 mblk_t *mp; 23786 tcp_stack_t *tcps = tcp->tcp_tcps; 23787 23788 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23789 23790 tcp->tcp_ack_tid = 0; 23791 23792 if (tcp->tcp_fused) 23793 return; 23794 23795 /* 23796 * Do not send ACK if there is no outstanding unack'ed data. 23797 */ 23798 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23799 return; 23800 } 23801 23802 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23803 /* 23804 * Make sure we don't allow deferred ACKs to result in 23805 * timer-based ACKing. If we have held off an ACK 23806 * when there was more than an mss here, and the timer 23807 * goes off, we have to worry about the possibility 23808 * that the sender isn't doing slow-start, or is out 23809 * of step with us for some other reason. We fall 23810 * permanently back in the direction of 23811 * ACK-every-other-packet as suggested in RFC 1122. 23812 */ 23813 if (tcp->tcp_rack_abs_max > 2) 23814 tcp->tcp_rack_abs_max--; 23815 tcp->tcp_rack_cur_max = 2; 23816 } 23817 mp = tcp_ack_mp(tcp); 23818 23819 if (mp != NULL) { 23820 BUMP_LOCAL(tcp->tcp_obsegs); 23821 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23822 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23823 tcp_send_data(tcp, tcp->tcp_wq, mp); 23824 } 23825 } 23826 23827 23828 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23829 static mblk_t * 23830 tcp_ack_mp(tcp_t *tcp) 23831 { 23832 uint32_t seq_no; 23833 tcp_stack_t *tcps = tcp->tcp_tcps; 23834 23835 /* 23836 * There are a few cases to be considered while setting the sequence no. 23837 * Essentially, we can come here while processing an unacceptable pkt 23838 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23839 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23840 * If we are here for a zero window probe, stick with suna. In all 23841 * other cases, we check if suna + swnd encompasses snxt and set 23842 * the sequence number to snxt, if so. If snxt falls outside the 23843 * window (the receiver probably shrunk its window), we will go with 23844 * suna + swnd, otherwise the sequence no will be unacceptable to the 23845 * receiver. 23846 */ 23847 if (tcp->tcp_zero_win_probe) { 23848 seq_no = tcp->tcp_suna; 23849 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23850 ASSERT(tcp->tcp_swnd == 0); 23851 seq_no = tcp->tcp_snxt; 23852 } else { 23853 seq_no = SEQ_GT(tcp->tcp_snxt, 23854 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23855 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23856 } 23857 23858 if (tcp->tcp_valid_bits) { 23859 /* 23860 * For the complex case where we have to send some 23861 * controls (FIN or SYN), let tcp_xmit_mp do it. 23862 */ 23863 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23864 NULL, B_FALSE)); 23865 } else { 23866 /* Generate a simple ACK */ 23867 int data_length; 23868 uchar_t *rptr; 23869 tcph_t *tcph; 23870 mblk_t *mp1; 23871 int32_t tcp_hdr_len; 23872 int32_t tcp_tcp_hdr_len; 23873 int32_t num_sack_blk = 0; 23874 int32_t sack_opt_len; 23875 23876 /* 23877 * Allocate space for TCP + IP headers 23878 * and link-level header 23879 */ 23880 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23881 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23882 tcp->tcp_num_sack_blk); 23883 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23884 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23885 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23886 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23887 } else { 23888 tcp_hdr_len = tcp->tcp_hdr_len; 23889 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23890 } 23891 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23892 if (!mp1) 23893 return (NULL); 23894 23895 /* Update the latest receive window size in TCP header. */ 23896 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23897 tcp->tcp_tcph->th_win); 23898 /* copy in prototype TCP + IP header */ 23899 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23900 mp1->b_rptr = rptr; 23901 mp1->b_wptr = rptr + tcp_hdr_len; 23902 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23903 23904 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23905 23906 /* Set the TCP sequence number. */ 23907 U32_TO_ABE32(seq_no, tcph->th_seq); 23908 23909 /* Set up the TCP flag field. */ 23910 tcph->th_flags[0] = (uchar_t)TH_ACK; 23911 if (tcp->tcp_ecn_echo_on) 23912 tcph->th_flags[0] |= TH_ECE; 23913 23914 tcp->tcp_rack = tcp->tcp_rnxt; 23915 tcp->tcp_rack_cnt = 0; 23916 23917 /* fill in timestamp option if in use */ 23918 if (tcp->tcp_snd_ts_ok) { 23919 uint32_t llbolt = (uint32_t)lbolt; 23920 23921 U32_TO_BE32(llbolt, 23922 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23923 U32_TO_BE32(tcp->tcp_ts_recent, 23924 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23925 } 23926 23927 /* Fill in SACK options */ 23928 if (num_sack_blk > 0) { 23929 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23930 sack_blk_t *tmp; 23931 int32_t i; 23932 23933 wptr[0] = TCPOPT_NOP; 23934 wptr[1] = TCPOPT_NOP; 23935 wptr[2] = TCPOPT_SACK; 23936 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23937 sizeof (sack_blk_t); 23938 wptr += TCPOPT_REAL_SACK_LEN; 23939 23940 tmp = tcp->tcp_sack_list; 23941 for (i = 0; i < num_sack_blk; i++) { 23942 U32_TO_BE32(tmp[i].begin, wptr); 23943 wptr += sizeof (tcp_seq); 23944 U32_TO_BE32(tmp[i].end, wptr); 23945 wptr += sizeof (tcp_seq); 23946 } 23947 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23948 << 4); 23949 } 23950 23951 if (tcp->tcp_ipversion == IPV4_VERSION) { 23952 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23953 } else { 23954 /* Check for ip6i_t header in sticky hdrs */ 23955 ip6_t *ip6 = (ip6_t *)(rptr + 23956 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23957 sizeof (ip6i_t) : 0)); 23958 23959 ip6->ip6_plen = htons(tcp_hdr_len - 23960 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23961 } 23962 23963 /* 23964 * Prime pump for checksum calculation in IP. Include the 23965 * adjustment for a source route if any. 23966 */ 23967 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23968 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23969 U16_TO_ABE16(data_length, tcph->th_sum); 23970 23971 if (tcp->tcp_ip_forward_progress) { 23972 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23973 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23974 tcp->tcp_ip_forward_progress = B_FALSE; 23975 } 23976 return (mp1); 23977 } 23978 } 23979 23980 /* 23981 * Hash list insertion routine for tcp_t structures. 23982 * Inserts entries with the ones bound to a specific IP address first 23983 * followed by those bound to INADDR_ANY. 23984 */ 23985 static void 23986 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23987 { 23988 tcp_t **tcpp; 23989 tcp_t *tcpnext; 23990 23991 if (tcp->tcp_ptpbhn != NULL) { 23992 ASSERT(!caller_holds_lock); 23993 tcp_bind_hash_remove(tcp); 23994 } 23995 tcpp = &tbf->tf_tcp; 23996 if (!caller_holds_lock) { 23997 mutex_enter(&tbf->tf_lock); 23998 } else { 23999 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 24000 } 24001 tcpnext = tcpp[0]; 24002 if (tcpnext) { 24003 /* 24004 * If the new tcp bound to the INADDR_ANY address 24005 * and the first one in the list is not bound to 24006 * INADDR_ANY we skip all entries until we find the 24007 * first one bound to INADDR_ANY. 24008 * This makes sure that applications binding to a 24009 * specific address get preference over those binding to 24010 * INADDR_ANY. 24011 */ 24012 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 24013 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 24014 while ((tcpnext = tcpp[0]) != NULL && 24015 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24016 tcpp = &(tcpnext->tcp_bind_hash); 24017 if (tcpnext) 24018 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24019 } else 24020 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24021 } 24022 tcp->tcp_bind_hash = tcpnext; 24023 tcp->tcp_ptpbhn = tcpp; 24024 tcpp[0] = tcp; 24025 if (!caller_holds_lock) 24026 mutex_exit(&tbf->tf_lock); 24027 } 24028 24029 /* 24030 * Hash list removal routine for tcp_t structures. 24031 */ 24032 static void 24033 tcp_bind_hash_remove(tcp_t *tcp) 24034 { 24035 tcp_t *tcpnext; 24036 kmutex_t *lockp; 24037 tcp_stack_t *tcps = tcp->tcp_tcps; 24038 24039 if (tcp->tcp_ptpbhn == NULL) 24040 return; 24041 24042 /* 24043 * Extract the lock pointer in case there are concurrent 24044 * hash_remove's for this instance. 24045 */ 24046 ASSERT(tcp->tcp_lport != 0); 24047 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24048 24049 ASSERT(lockp != NULL); 24050 mutex_enter(lockp); 24051 if (tcp->tcp_ptpbhn) { 24052 tcpnext = tcp->tcp_bind_hash; 24053 if (tcpnext) { 24054 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24055 tcp->tcp_bind_hash = NULL; 24056 } 24057 *tcp->tcp_ptpbhn = tcpnext; 24058 tcp->tcp_ptpbhn = NULL; 24059 } 24060 mutex_exit(lockp); 24061 } 24062 24063 24064 /* 24065 * Hash list lookup routine for tcp_t structures. 24066 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24067 */ 24068 static tcp_t * 24069 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24070 { 24071 tf_t *tf; 24072 tcp_t *tcp; 24073 24074 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24075 mutex_enter(&tf->tf_lock); 24076 for (tcp = tf->tf_tcp; tcp != NULL; 24077 tcp = tcp->tcp_acceptor_hash) { 24078 if (tcp->tcp_acceptor_id == id) { 24079 CONN_INC_REF(tcp->tcp_connp); 24080 mutex_exit(&tf->tf_lock); 24081 return (tcp); 24082 } 24083 } 24084 mutex_exit(&tf->tf_lock); 24085 return (NULL); 24086 } 24087 24088 24089 /* 24090 * Hash list insertion routine for tcp_t structures. 24091 */ 24092 void 24093 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24094 { 24095 tf_t *tf; 24096 tcp_t **tcpp; 24097 tcp_t *tcpnext; 24098 tcp_stack_t *tcps = tcp->tcp_tcps; 24099 24100 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24101 24102 if (tcp->tcp_ptpahn != NULL) 24103 tcp_acceptor_hash_remove(tcp); 24104 tcpp = &tf->tf_tcp; 24105 mutex_enter(&tf->tf_lock); 24106 tcpnext = tcpp[0]; 24107 if (tcpnext) 24108 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24109 tcp->tcp_acceptor_hash = tcpnext; 24110 tcp->tcp_ptpahn = tcpp; 24111 tcpp[0] = tcp; 24112 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24113 mutex_exit(&tf->tf_lock); 24114 } 24115 24116 /* 24117 * Hash list removal routine for tcp_t structures. 24118 */ 24119 static void 24120 tcp_acceptor_hash_remove(tcp_t *tcp) 24121 { 24122 tcp_t *tcpnext; 24123 kmutex_t *lockp; 24124 24125 /* 24126 * Extract the lock pointer in case there are concurrent 24127 * hash_remove's for this instance. 24128 */ 24129 lockp = tcp->tcp_acceptor_lockp; 24130 24131 if (tcp->tcp_ptpahn == NULL) 24132 return; 24133 24134 ASSERT(lockp != NULL); 24135 mutex_enter(lockp); 24136 if (tcp->tcp_ptpahn) { 24137 tcpnext = tcp->tcp_acceptor_hash; 24138 if (tcpnext) { 24139 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24140 tcp->tcp_acceptor_hash = NULL; 24141 } 24142 *tcp->tcp_ptpahn = tcpnext; 24143 tcp->tcp_ptpahn = NULL; 24144 } 24145 mutex_exit(lockp); 24146 tcp->tcp_acceptor_lockp = NULL; 24147 } 24148 24149 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24150 24151 static ipaddr_t netmasks[] = { 24152 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24153 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24154 }; 24155 24156 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24157 24158 /* 24159 * XXX This routine should go away and instead we should use the metrics 24160 * associated with the routes to determine the default sndspace and rcvspace. 24161 */ 24162 static tcp_hsp_t * 24163 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24164 { 24165 tcp_hsp_t *hsp = NULL; 24166 24167 /* Quick check without acquiring the lock. */ 24168 if (tcps->tcps_hsp_hash == NULL) 24169 return (NULL); 24170 24171 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24172 24173 /* This routine finds the best-matching HSP for address addr. */ 24174 24175 if (tcps->tcps_hsp_hash) { 24176 int i; 24177 ipaddr_t srchaddr; 24178 tcp_hsp_t *hsp_net; 24179 24180 /* We do three passes: host, network, and subnet. */ 24181 24182 srchaddr = addr; 24183 24184 for (i = 1; i <= 3; i++) { 24185 /* Look for exact match on srchaddr */ 24186 24187 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24188 while (hsp) { 24189 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24190 hsp->tcp_hsp_addr == srchaddr) 24191 break; 24192 hsp = hsp->tcp_hsp_next; 24193 } 24194 ASSERT(hsp == NULL || 24195 hsp->tcp_hsp_vers == IPV4_VERSION); 24196 24197 /* 24198 * If this is the first pass: 24199 * If we found a match, great, return it. 24200 * If not, search for the network on the second pass. 24201 */ 24202 24203 if (i == 1) 24204 if (hsp) 24205 break; 24206 else 24207 { 24208 srchaddr = addr & netmask(addr); 24209 continue; 24210 } 24211 24212 /* 24213 * If this is the second pass: 24214 * If we found a match, but there's a subnet mask, 24215 * save the match but try again using the subnet 24216 * mask on the third pass. 24217 * Otherwise, return whatever we found. 24218 */ 24219 24220 if (i == 2) { 24221 if (hsp && hsp->tcp_hsp_subnet) { 24222 hsp_net = hsp; 24223 srchaddr = addr & hsp->tcp_hsp_subnet; 24224 continue; 24225 } else { 24226 break; 24227 } 24228 } 24229 24230 /* 24231 * This must be the third pass. If we didn't find 24232 * anything, return the saved network HSP instead. 24233 */ 24234 24235 if (!hsp) 24236 hsp = hsp_net; 24237 } 24238 } 24239 24240 rw_exit(&tcps->tcps_hsp_lock); 24241 return (hsp); 24242 } 24243 24244 /* 24245 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24246 * match lookup. 24247 */ 24248 static tcp_hsp_t * 24249 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24250 { 24251 tcp_hsp_t *hsp = NULL; 24252 24253 /* Quick check without acquiring the lock. */ 24254 if (tcps->tcps_hsp_hash == NULL) 24255 return (NULL); 24256 24257 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24258 24259 /* This routine finds the best-matching HSP for address addr. */ 24260 24261 if (tcps->tcps_hsp_hash) { 24262 int i; 24263 in6_addr_t v6srchaddr; 24264 tcp_hsp_t *hsp_net; 24265 24266 /* We do three passes: host, network, and subnet. */ 24267 24268 v6srchaddr = *v6addr; 24269 24270 for (i = 1; i <= 3; i++) { 24271 /* Look for exact match on srchaddr */ 24272 24273 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24274 V4_PART_OF_V6(v6srchaddr))]; 24275 while (hsp) { 24276 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24277 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24278 &v6srchaddr)) 24279 break; 24280 hsp = hsp->tcp_hsp_next; 24281 } 24282 24283 /* 24284 * If this is the first pass: 24285 * If we found a match, great, return it. 24286 * If not, search for the network on the second pass. 24287 */ 24288 24289 if (i == 1) 24290 if (hsp) 24291 break; 24292 else { 24293 /* Assume a 64 bit mask */ 24294 v6srchaddr.s6_addr32[0] = 24295 v6addr->s6_addr32[0]; 24296 v6srchaddr.s6_addr32[1] = 24297 v6addr->s6_addr32[1]; 24298 v6srchaddr.s6_addr32[2] = 0; 24299 v6srchaddr.s6_addr32[3] = 0; 24300 continue; 24301 } 24302 24303 /* 24304 * If this is the second pass: 24305 * If we found a match, but there's a subnet mask, 24306 * save the match but try again using the subnet 24307 * mask on the third pass. 24308 * Otherwise, return whatever we found. 24309 */ 24310 24311 if (i == 2) { 24312 ASSERT(hsp == NULL || 24313 hsp->tcp_hsp_vers == IPV6_VERSION); 24314 if (hsp && 24315 !IN6_IS_ADDR_UNSPECIFIED( 24316 &hsp->tcp_hsp_subnet_v6)) { 24317 hsp_net = hsp; 24318 V6_MASK_COPY(*v6addr, 24319 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24320 continue; 24321 } else { 24322 break; 24323 } 24324 } 24325 24326 /* 24327 * This must be the third pass. If we didn't find 24328 * anything, return the saved network HSP instead. 24329 */ 24330 24331 if (!hsp) 24332 hsp = hsp_net; 24333 } 24334 } 24335 24336 rw_exit(&tcps->tcps_hsp_lock); 24337 return (hsp); 24338 } 24339 24340 /* 24341 * Type three generator adapted from the random() function in 4.4 BSD: 24342 */ 24343 24344 /* 24345 * Copyright (c) 1983, 1993 24346 * The Regents of the University of California. All rights reserved. 24347 * 24348 * Redistribution and use in source and binary forms, with or without 24349 * modification, are permitted provided that the following conditions 24350 * are met: 24351 * 1. Redistributions of source code must retain the above copyright 24352 * notice, this list of conditions and the following disclaimer. 24353 * 2. Redistributions in binary form must reproduce the above copyright 24354 * notice, this list of conditions and the following disclaimer in the 24355 * documentation and/or other materials provided with the distribution. 24356 * 3. All advertising materials mentioning features or use of this software 24357 * must display the following acknowledgement: 24358 * This product includes software developed by the University of 24359 * California, Berkeley and its contributors. 24360 * 4. Neither the name of the University nor the names of its contributors 24361 * may be used to endorse or promote products derived from this software 24362 * without specific prior written permission. 24363 * 24364 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24365 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24366 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24367 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24368 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24369 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24370 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24371 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24372 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24373 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24374 * SUCH DAMAGE. 24375 */ 24376 24377 /* Type 3 -- x**31 + x**3 + 1 */ 24378 #define DEG_3 31 24379 #define SEP_3 3 24380 24381 24382 /* Protected by tcp_random_lock */ 24383 static int tcp_randtbl[DEG_3 + 1]; 24384 24385 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24386 static int *tcp_random_rptr = &tcp_randtbl[1]; 24387 24388 static int *tcp_random_state = &tcp_randtbl[1]; 24389 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24390 24391 kmutex_t tcp_random_lock; 24392 24393 void 24394 tcp_random_init(void) 24395 { 24396 int i; 24397 hrtime_t hrt; 24398 time_t wallclock; 24399 uint64_t result; 24400 24401 /* 24402 * Use high-res timer and current time for seed. Gethrtime() returns 24403 * a longlong, which may contain resolution down to nanoseconds. 24404 * The current time will either be a 32-bit or a 64-bit quantity. 24405 * XOR the two together in a 64-bit result variable. 24406 * Convert the result to a 32-bit value by multiplying the high-order 24407 * 32-bits by the low-order 32-bits. 24408 */ 24409 24410 hrt = gethrtime(); 24411 (void) drv_getparm(TIME, &wallclock); 24412 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24413 mutex_enter(&tcp_random_lock); 24414 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24415 (result & 0xffffffff); 24416 24417 for (i = 1; i < DEG_3; i++) 24418 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24419 + 12345; 24420 tcp_random_fptr = &tcp_random_state[SEP_3]; 24421 tcp_random_rptr = &tcp_random_state[0]; 24422 mutex_exit(&tcp_random_lock); 24423 for (i = 0; i < 10 * DEG_3; i++) 24424 (void) tcp_random(); 24425 } 24426 24427 /* 24428 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24429 * This range is selected to be approximately centered on TCP_ISS / 2, 24430 * and easy to compute. We get this value by generating a 32-bit random 24431 * number, selecting out the high-order 17 bits, and then adding one so 24432 * that we never return zero. 24433 */ 24434 int 24435 tcp_random(void) 24436 { 24437 int i; 24438 24439 mutex_enter(&tcp_random_lock); 24440 *tcp_random_fptr += *tcp_random_rptr; 24441 24442 /* 24443 * The high-order bits are more random than the low-order bits, 24444 * so we select out the high-order 17 bits and add one so that 24445 * we never return zero. 24446 */ 24447 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24448 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24449 tcp_random_fptr = tcp_random_state; 24450 ++tcp_random_rptr; 24451 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24452 tcp_random_rptr = tcp_random_state; 24453 24454 mutex_exit(&tcp_random_lock); 24455 return (i); 24456 } 24457 24458 /* 24459 * XXX This will go away when TPI is extended to send 24460 * info reqs to sockfs/timod ..... 24461 * Given a queue, set the max packet size for the write 24462 * side of the queue below stream head. This value is 24463 * cached on the stream head. 24464 * Returns 1 on success, 0 otherwise. 24465 */ 24466 static int 24467 setmaxps(queue_t *q, int maxpsz) 24468 { 24469 struct stdata *stp; 24470 queue_t *wq; 24471 stp = STREAM(q); 24472 24473 /* 24474 * At this point change of a queue parameter is not allowed 24475 * when a multiplexor is sitting on top. 24476 */ 24477 if (stp->sd_flag & STPLEX) 24478 return (0); 24479 24480 claimstr(stp->sd_wrq); 24481 wq = stp->sd_wrq->q_next; 24482 ASSERT(wq != NULL); 24483 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24484 releasestr(stp->sd_wrq); 24485 return (1); 24486 } 24487 24488 static int 24489 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24490 int *t_errorp, int *sys_errorp) 24491 { 24492 int error; 24493 int is_absreq_failure; 24494 t_scalar_t *opt_lenp; 24495 t_scalar_t opt_offset; 24496 int prim_type; 24497 struct T_conn_req *tcreqp; 24498 struct T_conn_res *tcresp; 24499 cred_t *cr; 24500 24501 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24502 24503 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24504 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24505 prim_type == T_CONN_RES); 24506 24507 switch (prim_type) { 24508 case T_CONN_REQ: 24509 tcreqp = (struct T_conn_req *)mp->b_rptr; 24510 opt_offset = tcreqp->OPT_offset; 24511 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24512 break; 24513 case O_T_CONN_RES: 24514 case T_CONN_RES: 24515 tcresp = (struct T_conn_res *)mp->b_rptr; 24516 opt_offset = tcresp->OPT_offset; 24517 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24518 break; 24519 } 24520 24521 *t_errorp = 0; 24522 *sys_errorp = 0; 24523 *do_disconnectp = 0; 24524 24525 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24526 opt_offset, cr, &tcp_opt_obj, 24527 NULL, &is_absreq_failure); 24528 24529 switch (error) { 24530 case 0: /* no error */ 24531 ASSERT(is_absreq_failure == 0); 24532 return (0); 24533 case ENOPROTOOPT: 24534 *t_errorp = TBADOPT; 24535 break; 24536 case EACCES: 24537 *t_errorp = TACCES; 24538 break; 24539 default: 24540 *t_errorp = TSYSERR; *sys_errorp = error; 24541 break; 24542 } 24543 if (is_absreq_failure != 0) { 24544 /* 24545 * The connection request should get the local ack 24546 * T_OK_ACK and then a T_DISCON_IND. 24547 */ 24548 *do_disconnectp = 1; 24549 } 24550 return (-1); 24551 } 24552 24553 /* 24554 * Split this function out so that if the secret changes, I'm okay. 24555 * 24556 * Initialize the tcp_iss_cookie and tcp_iss_key. 24557 */ 24558 24559 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24560 24561 static void 24562 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24563 { 24564 struct { 24565 int32_t current_time; 24566 uint32_t randnum; 24567 uint16_t pad; 24568 uint8_t ether[6]; 24569 uint8_t passwd[PASSWD_SIZE]; 24570 } tcp_iss_cookie; 24571 time_t t; 24572 24573 /* 24574 * Start with the current absolute time. 24575 */ 24576 (void) drv_getparm(TIME, &t); 24577 tcp_iss_cookie.current_time = t; 24578 24579 /* 24580 * XXX - Need a more random number per RFC 1750, not this crap. 24581 * OTOH, if what follows is pretty random, then I'm in better shape. 24582 */ 24583 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24584 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24585 24586 /* 24587 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24588 * as a good template. 24589 */ 24590 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24591 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24592 24593 /* 24594 * The pass-phrase. Normally this is supplied by user-called NDD. 24595 */ 24596 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24597 24598 /* 24599 * See 4010593 if this section becomes a problem again, 24600 * but the local ethernet address is useful here. 24601 */ 24602 (void) localetheraddr(NULL, 24603 (struct ether_addr *)&tcp_iss_cookie.ether); 24604 24605 /* 24606 * Hash 'em all together. The MD5Final is called per-connection. 24607 */ 24608 mutex_enter(&tcps->tcps_iss_key_lock); 24609 MD5Init(&tcps->tcps_iss_key); 24610 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24611 sizeof (tcp_iss_cookie)); 24612 mutex_exit(&tcps->tcps_iss_key_lock); 24613 } 24614 24615 /* 24616 * Set the RFC 1948 pass phrase 24617 */ 24618 /* ARGSUSED */ 24619 static int 24620 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24621 cred_t *cr) 24622 { 24623 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24624 24625 /* 24626 * Basically, value contains a new pass phrase. Pass it along! 24627 */ 24628 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24629 return (0); 24630 } 24631 24632 /* ARGSUSED */ 24633 static int 24634 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24635 { 24636 bzero(buf, sizeof (tcp_sack_info_t)); 24637 return (0); 24638 } 24639 24640 /* ARGSUSED */ 24641 static int 24642 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24643 { 24644 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24645 return (0); 24646 } 24647 24648 /* 24649 * Make sure we wait until the default queue is setup, yet allow 24650 * tcp_g_q_create() to open a TCP stream. 24651 * We need to allow tcp_g_q_create() do do an open 24652 * of tcp, hence we compare curhread. 24653 * All others have to wait until the tcps_g_q has been 24654 * setup. 24655 */ 24656 void 24657 tcp_g_q_setup(tcp_stack_t *tcps) 24658 { 24659 mutex_enter(&tcps->tcps_g_q_lock); 24660 if (tcps->tcps_g_q != NULL) { 24661 mutex_exit(&tcps->tcps_g_q_lock); 24662 return; 24663 } 24664 if (tcps->tcps_g_q_creator == NULL) { 24665 /* This thread will set it up */ 24666 tcps->tcps_g_q_creator = curthread; 24667 mutex_exit(&tcps->tcps_g_q_lock); 24668 tcp_g_q_create(tcps); 24669 mutex_enter(&tcps->tcps_g_q_lock); 24670 ASSERT(tcps->tcps_g_q_creator == curthread); 24671 tcps->tcps_g_q_creator = NULL; 24672 cv_signal(&tcps->tcps_g_q_cv); 24673 ASSERT(tcps->tcps_g_q != NULL); 24674 mutex_exit(&tcps->tcps_g_q_lock); 24675 return; 24676 } 24677 /* Everybody but the creator has to wait */ 24678 if (tcps->tcps_g_q_creator != curthread) { 24679 while (tcps->tcps_g_q == NULL) 24680 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24681 } 24682 mutex_exit(&tcps->tcps_g_q_lock); 24683 } 24684 24685 #define IP "ip" 24686 24687 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24688 24689 /* 24690 * Create a default tcp queue here instead of in strplumb 24691 */ 24692 void 24693 tcp_g_q_create(tcp_stack_t *tcps) 24694 { 24695 int error; 24696 ldi_handle_t lh = NULL; 24697 ldi_ident_t li = NULL; 24698 int rval; 24699 cred_t *cr; 24700 major_t IP_MAJ; 24701 24702 #ifdef NS_DEBUG 24703 (void) printf("tcp_g_q_create()\n"); 24704 #endif 24705 24706 IP_MAJ = ddi_name_to_major(IP); 24707 24708 ASSERT(tcps->tcps_g_q_creator == curthread); 24709 24710 error = ldi_ident_from_major(IP_MAJ, &li); 24711 if (error) { 24712 #ifdef DEBUG 24713 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24714 error); 24715 #endif 24716 return; 24717 } 24718 24719 cr = zone_get_kcred(netstackid_to_zoneid( 24720 tcps->tcps_netstack->netstack_stackid)); 24721 ASSERT(cr != NULL); 24722 /* 24723 * We set the tcp default queue to IPv6 because IPv4 falls 24724 * back to IPv6 when it can't find a client, but 24725 * IPv6 does not fall back to IPv4. 24726 */ 24727 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24728 if (error) { 24729 #ifdef DEBUG 24730 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24731 error); 24732 #endif 24733 goto out; 24734 } 24735 24736 /* 24737 * This ioctl causes the tcp framework to cache a pointer to 24738 * this stream, so we don't want to close the stream after 24739 * this operation. 24740 * Use the kernel credentials that are for the zone we're in. 24741 */ 24742 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24743 (intptr_t)0, FKIOCTL, cr, &rval); 24744 if (error) { 24745 #ifdef DEBUG 24746 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24747 "error %d\n", error); 24748 #endif 24749 goto out; 24750 } 24751 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24752 lh = NULL; 24753 out: 24754 /* Close layered handles */ 24755 if (li) 24756 ldi_ident_release(li); 24757 /* Keep cred around until _inactive needs it */ 24758 tcps->tcps_g_q_cr = cr; 24759 } 24760 24761 /* 24762 * We keep tcp_g_q set until all other tcp_t's in the zone 24763 * has gone away, and then when tcp_g_q_inactive() is called 24764 * we clear it. 24765 */ 24766 void 24767 tcp_g_q_destroy(tcp_stack_t *tcps) 24768 { 24769 #ifdef NS_DEBUG 24770 (void) printf("tcp_g_q_destroy()for stack %d\n", 24771 tcps->tcps_netstack->netstack_stackid); 24772 #endif 24773 24774 if (tcps->tcps_g_q == NULL) { 24775 return; /* Nothing to cleanup */ 24776 } 24777 /* 24778 * Drop reference corresponding to the default queue. 24779 * This reference was added from tcp_open when the default queue 24780 * was created, hence we compensate for this extra drop in 24781 * tcp_g_q_close. If the refcnt drops to zero here it means 24782 * the default queue was the last one to be open, in which 24783 * case, then tcp_g_q_inactive will be 24784 * called as a result of the refrele. 24785 */ 24786 TCPS_REFRELE(tcps); 24787 } 24788 24789 /* 24790 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24791 * Run by tcp_q_q_inactive using a taskq. 24792 */ 24793 static void 24794 tcp_g_q_close(void *arg) 24795 { 24796 tcp_stack_t *tcps = arg; 24797 int error; 24798 ldi_handle_t lh = NULL; 24799 ldi_ident_t li = NULL; 24800 cred_t *cr; 24801 major_t IP_MAJ; 24802 24803 IP_MAJ = ddi_name_to_major(IP); 24804 24805 #ifdef NS_DEBUG 24806 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24807 tcps->tcps_netstack->netstack_stackid, 24808 tcps->tcps_netstack->netstack_refcnt); 24809 #endif 24810 lh = tcps->tcps_g_q_lh; 24811 if (lh == NULL) 24812 return; /* Nothing to cleanup */ 24813 24814 ASSERT(tcps->tcps_refcnt == 1); 24815 ASSERT(tcps->tcps_g_q != NULL); 24816 24817 error = ldi_ident_from_major(IP_MAJ, &li); 24818 if (error) { 24819 #ifdef DEBUG 24820 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24821 error); 24822 #endif 24823 return; 24824 } 24825 24826 cr = tcps->tcps_g_q_cr; 24827 tcps->tcps_g_q_cr = NULL; 24828 ASSERT(cr != NULL); 24829 24830 /* 24831 * Make sure we can break the recursion when tcp_close decrements 24832 * the reference count causing g_q_inactive to be called again. 24833 */ 24834 tcps->tcps_g_q_lh = NULL; 24835 24836 /* close the default queue */ 24837 (void) ldi_close(lh, FREAD|FWRITE, cr); 24838 /* 24839 * At this point in time tcps and the rest of netstack_t might 24840 * have been deleted. 24841 */ 24842 tcps = NULL; 24843 24844 /* Close layered handles */ 24845 ldi_ident_release(li); 24846 crfree(cr); 24847 } 24848 24849 /* 24850 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24851 * 24852 * Have to ensure that the ldi routines are not used by an 24853 * interrupt thread by using a taskq. 24854 */ 24855 void 24856 tcp_g_q_inactive(tcp_stack_t *tcps) 24857 { 24858 if (tcps->tcps_g_q_lh == NULL) 24859 return; /* Nothing to cleanup */ 24860 24861 ASSERT(tcps->tcps_refcnt == 0); 24862 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24863 24864 if (servicing_interrupt()) { 24865 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24866 (void *) tcps, TQ_SLEEP); 24867 } else { 24868 tcp_g_q_close(tcps); 24869 } 24870 } 24871 24872 /* 24873 * Called by IP when IP is loaded into the kernel 24874 */ 24875 void 24876 tcp_ddi_g_init(void) 24877 { 24878 tcp_timercache = kmem_cache_create("tcp_timercache", 24879 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24880 NULL, NULL, NULL, NULL, NULL, 0); 24881 24882 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24883 sizeof (tcp_sack_info_t), 0, 24884 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24885 24886 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24887 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24888 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24889 24890 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24891 24892 /* Initialize the random number generator */ 24893 tcp_random_init(); 24894 24895 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24896 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24897 24898 /* A single callback independently of how many netstacks we have */ 24899 ip_squeue_init(tcp_squeue_add); 24900 24901 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24902 24903 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24904 TASKQ_PREPOPULATE); 24905 24906 /* 24907 * We want to be informed each time a stack is created or 24908 * destroyed in the kernel, so we can maintain the 24909 * set of tcp_stack_t's. 24910 */ 24911 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24912 tcp_stack_fini); 24913 } 24914 24915 24916 /* 24917 * Initialize the TCP stack instance. 24918 */ 24919 static void * 24920 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24921 { 24922 tcp_stack_t *tcps; 24923 tcpparam_t *pa; 24924 int i; 24925 24926 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24927 tcps->tcps_netstack = ns; 24928 24929 /* Initialize locks */ 24930 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24931 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24932 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24933 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24934 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24935 24936 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24937 tcps->tcps_g_epriv_ports[0] = 2049; 24938 tcps->tcps_g_epriv_ports[1] = 4045; 24939 tcps->tcps_min_anonpriv_port = 512; 24940 24941 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24942 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24943 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24944 TCP_FANOUT_SIZE, KM_SLEEP); 24945 24946 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24947 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24948 MUTEX_DEFAULT, NULL); 24949 } 24950 24951 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24952 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24953 MUTEX_DEFAULT, NULL); 24954 } 24955 24956 /* TCP's IPsec code calls the packet dropper. */ 24957 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24958 24959 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24960 tcps->tcps_params = pa; 24961 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24962 24963 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24964 A_CNT(lcl_tcp_param_arr), tcps); 24965 24966 /* 24967 * Note: To really walk the device tree you need the devinfo 24968 * pointer to your device which is only available after probe/attach. 24969 * The following is safe only because it uses ddi_root_node() 24970 */ 24971 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24972 tcp_opt_obj.odb_opt_arr_cnt); 24973 24974 /* 24975 * Initialize RFC 1948 secret values. This will probably be reset once 24976 * by the boot scripts. 24977 * 24978 * Use NULL name, as the name is caught by the new lockstats. 24979 * 24980 * Initialize with some random, non-guessable string, like the global 24981 * T_INFO_ACK. 24982 */ 24983 24984 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24985 sizeof (tcp_g_t_info_ack), tcps); 24986 24987 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24988 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24989 24990 return (tcps); 24991 } 24992 24993 /* 24994 * Called when the IP module is about to be unloaded. 24995 */ 24996 void 24997 tcp_ddi_g_destroy(void) 24998 { 24999 tcp_g_kstat_fini(tcp_g_kstat); 25000 tcp_g_kstat = NULL; 25001 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25002 25003 mutex_destroy(&tcp_random_lock); 25004 25005 kmem_cache_destroy(tcp_timercache); 25006 kmem_cache_destroy(tcp_sack_info_cache); 25007 kmem_cache_destroy(tcp_iphc_cache); 25008 25009 netstack_unregister(NS_TCP); 25010 taskq_destroy(tcp_taskq); 25011 } 25012 25013 /* 25014 * Shut down the TCP stack instance. 25015 */ 25016 /* ARGSUSED */ 25017 static void 25018 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25019 { 25020 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25021 25022 tcp_g_q_destroy(tcps); 25023 } 25024 25025 /* 25026 * Free the TCP stack instance. 25027 */ 25028 static void 25029 tcp_stack_fini(netstackid_t stackid, void *arg) 25030 { 25031 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25032 int i; 25033 25034 nd_free(&tcps->tcps_g_nd); 25035 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25036 tcps->tcps_params = NULL; 25037 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25038 tcps->tcps_wroff_xtra_param = NULL; 25039 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25040 tcps->tcps_mdt_head_param = NULL; 25041 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25042 tcps->tcps_mdt_tail_param = NULL; 25043 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25044 tcps->tcps_mdt_max_pbufs_param = NULL; 25045 25046 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25047 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25048 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25049 } 25050 25051 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25052 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25053 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25054 } 25055 25056 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25057 tcps->tcps_bind_fanout = NULL; 25058 25059 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25060 tcps->tcps_acceptor_fanout = NULL; 25061 25062 mutex_destroy(&tcps->tcps_iss_key_lock); 25063 rw_destroy(&tcps->tcps_hsp_lock); 25064 mutex_destroy(&tcps->tcps_g_q_lock); 25065 cv_destroy(&tcps->tcps_g_q_cv); 25066 mutex_destroy(&tcps->tcps_epriv_port_lock); 25067 25068 ip_drop_unregister(&tcps->tcps_dropper); 25069 25070 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25071 tcps->tcps_kstat = NULL; 25072 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25073 25074 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25075 tcps->tcps_mibkp = NULL; 25076 25077 kmem_free(tcps, sizeof (*tcps)); 25078 } 25079 25080 /* 25081 * Generate ISS, taking into account NDD changes may happen halfway through. 25082 * (If the iss is not zero, set it.) 25083 */ 25084 25085 static void 25086 tcp_iss_init(tcp_t *tcp) 25087 { 25088 MD5_CTX context; 25089 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25090 uint32_t answer[4]; 25091 tcp_stack_t *tcps = tcp->tcp_tcps; 25092 25093 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25094 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25095 switch (tcps->tcps_strong_iss) { 25096 case 2: 25097 mutex_enter(&tcps->tcps_iss_key_lock); 25098 context = tcps->tcps_iss_key; 25099 mutex_exit(&tcps->tcps_iss_key_lock); 25100 arg.ports = tcp->tcp_ports; 25101 if (tcp->tcp_ipversion == IPV4_VERSION) { 25102 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25103 &arg.src); 25104 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25105 &arg.dst); 25106 } else { 25107 arg.src = tcp->tcp_ip6h->ip6_src; 25108 arg.dst = tcp->tcp_ip6h->ip6_dst; 25109 } 25110 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25111 MD5Final((uchar_t *)answer, &context); 25112 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25113 /* 25114 * Now that we've hashed into a unique per-connection sequence 25115 * space, add a random increment per strong_iss == 1. So I 25116 * guess we'll have to... 25117 */ 25118 /* FALLTHRU */ 25119 case 1: 25120 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25121 break; 25122 default: 25123 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25124 break; 25125 } 25126 tcp->tcp_valid_bits = TCP_ISS_VALID; 25127 tcp->tcp_fss = tcp->tcp_iss - 1; 25128 tcp->tcp_suna = tcp->tcp_iss; 25129 tcp->tcp_snxt = tcp->tcp_iss + 1; 25130 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25131 tcp->tcp_csuna = tcp->tcp_snxt; 25132 } 25133 25134 /* 25135 * Exported routine for extracting active tcp connection status. 25136 * 25137 * This is used by the Solaris Cluster Networking software to 25138 * gather a list of connections that need to be forwarded to 25139 * specific nodes in the cluster when configuration changes occur. 25140 * 25141 * The callback is invoked for each tcp_t structure. Returning 25142 * non-zero from the callback routine terminates the search. 25143 */ 25144 int 25145 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25146 void *arg) 25147 { 25148 netstack_handle_t nh; 25149 netstack_t *ns; 25150 int ret = 0; 25151 25152 netstack_next_init(&nh); 25153 while ((ns = netstack_next(&nh)) != NULL) { 25154 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25155 ns->netstack_tcp); 25156 netstack_rele(ns); 25157 } 25158 netstack_next_fini(&nh); 25159 return (ret); 25160 } 25161 25162 static int 25163 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25164 tcp_stack_t *tcps) 25165 { 25166 tcp_t *tcp; 25167 cl_tcp_info_t cl_tcpi; 25168 connf_t *connfp; 25169 conn_t *connp; 25170 int i; 25171 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25172 25173 ASSERT(callback != NULL); 25174 25175 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25176 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25177 connp = NULL; 25178 25179 while ((connp = 25180 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25181 25182 tcp = connp->conn_tcp; 25183 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25184 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25185 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25186 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25187 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25188 /* 25189 * The macros tcp_laddr and tcp_faddr give the IPv4 25190 * addresses. They are copied implicitly below as 25191 * mapped addresses. 25192 */ 25193 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25194 if (tcp->tcp_ipversion == IPV4_VERSION) { 25195 cl_tcpi.cl_tcpi_faddr = 25196 tcp->tcp_ipha->ipha_dst; 25197 } else { 25198 cl_tcpi.cl_tcpi_faddr_v6 = 25199 tcp->tcp_ip6h->ip6_dst; 25200 } 25201 25202 /* 25203 * If the callback returns non-zero 25204 * we terminate the traversal. 25205 */ 25206 if ((*callback)(&cl_tcpi, arg) != 0) { 25207 CONN_DEC_REF(tcp->tcp_connp); 25208 return (1); 25209 } 25210 } 25211 } 25212 25213 return (0); 25214 } 25215 25216 /* 25217 * Macros used for accessing the different types of sockaddr 25218 * structures inside a tcp_ioc_abort_conn_t. 25219 */ 25220 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25221 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25222 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25223 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25224 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25225 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25226 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25227 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25228 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25229 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25230 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25231 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25232 25233 /* 25234 * Return the correct error code to mimic the behavior 25235 * of a connection reset. 25236 */ 25237 #define TCP_AC_GET_ERRCODE(state, err) { \ 25238 switch ((state)) { \ 25239 case TCPS_SYN_SENT: \ 25240 case TCPS_SYN_RCVD: \ 25241 (err) = ECONNREFUSED; \ 25242 break; \ 25243 case TCPS_ESTABLISHED: \ 25244 case TCPS_FIN_WAIT_1: \ 25245 case TCPS_FIN_WAIT_2: \ 25246 case TCPS_CLOSE_WAIT: \ 25247 (err) = ECONNRESET; \ 25248 break; \ 25249 case TCPS_CLOSING: \ 25250 case TCPS_LAST_ACK: \ 25251 case TCPS_TIME_WAIT: \ 25252 (err) = 0; \ 25253 break; \ 25254 default: \ 25255 (err) = ENXIO; \ 25256 } \ 25257 } 25258 25259 /* 25260 * Check if a tcp structure matches the info in acp. 25261 */ 25262 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25263 (((acp)->ac_local.ss_family == AF_INET) ? \ 25264 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25265 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25266 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25267 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25268 (TCP_AC_V4LPORT((acp)) == 0 || \ 25269 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25270 (TCP_AC_V4RPORT((acp)) == 0 || \ 25271 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25272 (acp)->ac_start <= (tcp)->tcp_state && \ 25273 (acp)->ac_end >= (tcp)->tcp_state) : \ 25274 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25275 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25276 &(tcp)->tcp_ip_src_v6)) && \ 25277 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25278 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25279 &(tcp)->tcp_remote_v6)) && \ 25280 (TCP_AC_V6LPORT((acp)) == 0 || \ 25281 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25282 (TCP_AC_V6RPORT((acp)) == 0 || \ 25283 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25284 (acp)->ac_start <= (tcp)->tcp_state && \ 25285 (acp)->ac_end >= (tcp)->tcp_state)) 25286 25287 #define TCP_AC_MATCH(acp, tcp) \ 25288 (((acp)->ac_zoneid == ALL_ZONES || \ 25289 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25290 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25291 25292 /* 25293 * Build a message containing a tcp_ioc_abort_conn_t structure 25294 * which is filled in with information from acp and tp. 25295 */ 25296 static mblk_t * 25297 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25298 { 25299 mblk_t *mp; 25300 tcp_ioc_abort_conn_t *tacp; 25301 25302 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25303 if (mp == NULL) 25304 return (NULL); 25305 25306 mp->b_datap->db_type = M_CTL; 25307 25308 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25309 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25310 sizeof (uint32_t)); 25311 25312 tacp->ac_start = acp->ac_start; 25313 tacp->ac_end = acp->ac_end; 25314 tacp->ac_zoneid = acp->ac_zoneid; 25315 25316 if (acp->ac_local.ss_family == AF_INET) { 25317 tacp->ac_local.ss_family = AF_INET; 25318 tacp->ac_remote.ss_family = AF_INET; 25319 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25320 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25321 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25322 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25323 } else { 25324 tacp->ac_local.ss_family = AF_INET6; 25325 tacp->ac_remote.ss_family = AF_INET6; 25326 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25327 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25328 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25329 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25330 } 25331 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25332 return (mp); 25333 } 25334 25335 /* 25336 * Print a tcp_ioc_abort_conn_t structure. 25337 */ 25338 static void 25339 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25340 { 25341 char lbuf[128]; 25342 char rbuf[128]; 25343 sa_family_t af; 25344 in_port_t lport, rport; 25345 ushort_t logflags; 25346 25347 af = acp->ac_local.ss_family; 25348 25349 if (af == AF_INET) { 25350 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25351 lbuf, 128); 25352 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25353 rbuf, 128); 25354 lport = ntohs(TCP_AC_V4LPORT(acp)); 25355 rport = ntohs(TCP_AC_V4RPORT(acp)); 25356 } else { 25357 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25358 lbuf, 128); 25359 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25360 rbuf, 128); 25361 lport = ntohs(TCP_AC_V6LPORT(acp)); 25362 rport = ntohs(TCP_AC_V6RPORT(acp)); 25363 } 25364 25365 logflags = SL_TRACE | SL_NOTE; 25366 /* 25367 * Don't print this message to the console if the operation was done 25368 * to a non-global zone. 25369 */ 25370 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25371 logflags |= SL_CONSOLE; 25372 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25373 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25374 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25375 acp->ac_start, acp->ac_end); 25376 } 25377 25378 /* 25379 * Called inside tcp_rput when a message built using 25380 * tcp_ioctl_abort_build_msg is put into a queue. 25381 * Note that when we get here there is no wildcard in acp any more. 25382 */ 25383 static void 25384 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25385 { 25386 tcp_ioc_abort_conn_t *acp; 25387 25388 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25389 if (tcp->tcp_state <= acp->ac_end) { 25390 /* 25391 * If we get here, we are already on the correct 25392 * squeue. This ioctl follows the following path 25393 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25394 * ->tcp_ioctl_abort->squeue_fill (if on a 25395 * different squeue) 25396 */ 25397 int errcode; 25398 25399 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25400 (void) tcp_clean_death(tcp, errcode, 26); 25401 } 25402 freemsg(mp); 25403 } 25404 25405 /* 25406 * Abort all matching connections on a hash chain. 25407 */ 25408 static int 25409 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25410 boolean_t exact, tcp_stack_t *tcps) 25411 { 25412 int nmatch, err = 0; 25413 tcp_t *tcp; 25414 MBLKP mp, last, listhead = NULL; 25415 conn_t *tconnp; 25416 connf_t *connfp; 25417 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25418 25419 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25420 25421 startover: 25422 nmatch = 0; 25423 25424 mutex_enter(&connfp->connf_lock); 25425 for (tconnp = connfp->connf_head; tconnp != NULL; 25426 tconnp = tconnp->conn_next) { 25427 tcp = tconnp->conn_tcp; 25428 if (TCP_AC_MATCH(acp, tcp)) { 25429 CONN_INC_REF(tcp->tcp_connp); 25430 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25431 if (mp == NULL) { 25432 err = ENOMEM; 25433 CONN_DEC_REF(tcp->tcp_connp); 25434 break; 25435 } 25436 mp->b_prev = (mblk_t *)tcp; 25437 25438 if (listhead == NULL) { 25439 listhead = mp; 25440 last = mp; 25441 } else { 25442 last->b_next = mp; 25443 last = mp; 25444 } 25445 nmatch++; 25446 if (exact) 25447 break; 25448 } 25449 25450 /* Avoid holding lock for too long. */ 25451 if (nmatch >= 500) 25452 break; 25453 } 25454 mutex_exit(&connfp->connf_lock); 25455 25456 /* Pass mp into the correct tcp */ 25457 while ((mp = listhead) != NULL) { 25458 listhead = listhead->b_next; 25459 tcp = (tcp_t *)mp->b_prev; 25460 mp->b_next = mp->b_prev = NULL; 25461 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25462 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25463 } 25464 25465 *count += nmatch; 25466 if (nmatch >= 500 && err == 0) 25467 goto startover; 25468 return (err); 25469 } 25470 25471 /* 25472 * Abort all connections that matches the attributes specified in acp. 25473 */ 25474 static int 25475 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25476 { 25477 sa_family_t af; 25478 uint32_t ports; 25479 uint16_t *pports; 25480 int err = 0, count = 0; 25481 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25482 int index = -1; 25483 ushort_t logflags; 25484 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25485 25486 af = acp->ac_local.ss_family; 25487 25488 if (af == AF_INET) { 25489 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25490 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25491 pports = (uint16_t *)&ports; 25492 pports[1] = TCP_AC_V4LPORT(acp); 25493 pports[0] = TCP_AC_V4RPORT(acp); 25494 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25495 } 25496 } else { 25497 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25498 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25499 pports = (uint16_t *)&ports; 25500 pports[1] = TCP_AC_V6LPORT(acp); 25501 pports[0] = TCP_AC_V6RPORT(acp); 25502 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25503 } 25504 } 25505 25506 /* 25507 * For cases where remote addr, local port, and remote port are non- 25508 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25509 */ 25510 if (index != -1) { 25511 err = tcp_ioctl_abort_bucket(acp, index, 25512 &count, exact, tcps); 25513 } else { 25514 /* 25515 * loop through all entries for wildcard case 25516 */ 25517 for (index = 0; 25518 index < ipst->ips_ipcl_conn_fanout_size; 25519 index++) { 25520 err = tcp_ioctl_abort_bucket(acp, index, 25521 &count, exact, tcps); 25522 if (err != 0) 25523 break; 25524 } 25525 } 25526 25527 logflags = SL_TRACE | SL_NOTE; 25528 /* 25529 * Don't print this message to the console if the operation was done 25530 * to a non-global zone. 25531 */ 25532 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25533 logflags |= SL_CONSOLE; 25534 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25535 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25536 if (err == 0 && count == 0) 25537 err = ENOENT; 25538 return (err); 25539 } 25540 25541 /* 25542 * Process the TCP_IOC_ABORT_CONN ioctl request. 25543 */ 25544 static void 25545 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25546 { 25547 int err; 25548 IOCP iocp; 25549 MBLKP mp1; 25550 sa_family_t laf, raf; 25551 tcp_ioc_abort_conn_t *acp; 25552 zone_t *zptr; 25553 conn_t *connp = Q_TO_CONN(q); 25554 zoneid_t zoneid = connp->conn_zoneid; 25555 tcp_t *tcp = connp->conn_tcp; 25556 tcp_stack_t *tcps = tcp->tcp_tcps; 25557 25558 iocp = (IOCP)mp->b_rptr; 25559 25560 if ((mp1 = mp->b_cont) == NULL || 25561 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25562 err = EINVAL; 25563 goto out; 25564 } 25565 25566 /* check permissions */ 25567 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25568 err = EPERM; 25569 goto out; 25570 } 25571 25572 if (mp1->b_cont != NULL) { 25573 freemsg(mp1->b_cont); 25574 mp1->b_cont = NULL; 25575 } 25576 25577 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25578 laf = acp->ac_local.ss_family; 25579 raf = acp->ac_remote.ss_family; 25580 25581 /* check that a zone with the supplied zoneid exists */ 25582 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25583 zptr = zone_find_by_id(zoneid); 25584 if (zptr != NULL) { 25585 zone_rele(zptr); 25586 } else { 25587 err = EINVAL; 25588 goto out; 25589 } 25590 } 25591 25592 /* 25593 * For exclusive stacks we set the zoneid to zero 25594 * to make TCP operate as if in the global zone. 25595 */ 25596 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25597 acp->ac_zoneid = GLOBAL_ZONEID; 25598 25599 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25600 acp->ac_start > acp->ac_end || laf != raf || 25601 (laf != AF_INET && laf != AF_INET6)) { 25602 err = EINVAL; 25603 goto out; 25604 } 25605 25606 tcp_ioctl_abort_dump(acp); 25607 err = tcp_ioctl_abort(acp, tcps); 25608 25609 out: 25610 if (mp1 != NULL) { 25611 freemsg(mp1); 25612 mp->b_cont = NULL; 25613 } 25614 25615 if (err != 0) 25616 miocnak(q, mp, 0, err); 25617 else 25618 miocack(q, mp, 0, 0); 25619 } 25620 25621 /* 25622 * tcp_time_wait_processing() handles processing of incoming packets when 25623 * the tcp is in the TIME_WAIT state. 25624 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25625 * on the time wait list. 25626 */ 25627 void 25628 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25629 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25630 { 25631 int32_t bytes_acked; 25632 int32_t gap; 25633 int32_t rgap; 25634 tcp_opt_t tcpopt; 25635 uint_t flags; 25636 uint32_t new_swnd = 0; 25637 conn_t *connp; 25638 tcp_stack_t *tcps = tcp->tcp_tcps; 25639 25640 BUMP_LOCAL(tcp->tcp_ibsegs); 25641 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25642 25643 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25644 new_swnd = BE16_TO_U16(tcph->th_win) << 25645 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25646 if (tcp->tcp_snd_ts_ok) { 25647 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25648 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25649 tcp->tcp_rnxt, TH_ACK); 25650 goto done; 25651 } 25652 } 25653 gap = seg_seq - tcp->tcp_rnxt; 25654 rgap = tcp->tcp_rwnd - (gap + seg_len); 25655 if (gap < 0) { 25656 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25657 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25658 (seg_len > -gap ? -gap : seg_len)); 25659 seg_len += gap; 25660 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25661 if (flags & TH_RST) { 25662 goto done; 25663 } 25664 if ((flags & TH_FIN) && seg_len == -1) { 25665 /* 25666 * When TCP receives a duplicate FIN in 25667 * TIME_WAIT state, restart the 2 MSL timer. 25668 * See page 73 in RFC 793. Make sure this TCP 25669 * is already on the TIME_WAIT list. If not, 25670 * just restart the timer. 25671 */ 25672 if (TCP_IS_DETACHED(tcp)) { 25673 if (tcp_time_wait_remove(tcp, NULL) == 25674 B_TRUE) { 25675 tcp_time_wait_append(tcp); 25676 TCP_DBGSTAT(tcps, 25677 tcp_rput_time_wait); 25678 } 25679 } else { 25680 ASSERT(tcp != NULL); 25681 TCP_TIMER_RESTART(tcp, 25682 tcps->tcps_time_wait_interval); 25683 } 25684 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25685 tcp->tcp_rnxt, TH_ACK); 25686 goto done; 25687 } 25688 flags |= TH_ACK_NEEDED; 25689 seg_len = 0; 25690 goto process_ack; 25691 } 25692 25693 /* Fix seg_seq, and chew the gap off the front. */ 25694 seg_seq = tcp->tcp_rnxt; 25695 } 25696 25697 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25698 /* 25699 * Make sure that when we accept the connection, pick 25700 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25701 * old connection. 25702 * 25703 * The next ISS generated is equal to tcp_iss_incr_extra 25704 * + ISS_INCR/2 + other components depending on the 25705 * value of tcp_strong_iss. We pre-calculate the new 25706 * ISS here and compare with tcp_snxt to determine if 25707 * we need to make adjustment to tcp_iss_incr_extra. 25708 * 25709 * The above calculation is ugly and is a 25710 * waste of CPU cycles... 25711 */ 25712 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25713 int32_t adj; 25714 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25715 25716 switch (tcps->tcps_strong_iss) { 25717 case 2: { 25718 /* Add time and MD5 components. */ 25719 uint32_t answer[4]; 25720 struct { 25721 uint32_t ports; 25722 in6_addr_t src; 25723 in6_addr_t dst; 25724 } arg; 25725 MD5_CTX context; 25726 25727 mutex_enter(&tcps->tcps_iss_key_lock); 25728 context = tcps->tcps_iss_key; 25729 mutex_exit(&tcps->tcps_iss_key_lock); 25730 arg.ports = tcp->tcp_ports; 25731 /* We use MAPPED addresses in tcp_iss_init */ 25732 arg.src = tcp->tcp_ip_src_v6; 25733 if (tcp->tcp_ipversion == IPV4_VERSION) { 25734 IN6_IPADDR_TO_V4MAPPED( 25735 tcp->tcp_ipha->ipha_dst, 25736 &arg.dst); 25737 } else { 25738 arg.dst = 25739 tcp->tcp_ip6h->ip6_dst; 25740 } 25741 MD5Update(&context, (uchar_t *)&arg, 25742 sizeof (arg)); 25743 MD5Final((uchar_t *)answer, &context); 25744 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25745 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25746 break; 25747 } 25748 case 1: 25749 /* Add time component and min random (i.e. 1). */ 25750 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25751 break; 25752 default: 25753 /* Add only time component. */ 25754 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25755 break; 25756 } 25757 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25758 /* 25759 * New ISS not guaranteed to be ISS_INCR/2 25760 * ahead of the current tcp_snxt, so add the 25761 * difference to tcp_iss_incr_extra. 25762 */ 25763 tcps->tcps_iss_incr_extra += adj; 25764 } 25765 /* 25766 * If tcp_clean_death() can not perform the task now, 25767 * drop the SYN packet and let the other side re-xmit. 25768 * Otherwise pass the SYN packet back in, since the 25769 * old tcp state has been cleaned up or freed. 25770 */ 25771 if (tcp_clean_death(tcp, 0, 27) == -1) 25772 goto done; 25773 /* 25774 * We will come back to tcp_rput_data 25775 * on the global queue. Packets destined 25776 * for the global queue will be checked 25777 * with global policy. But the policy for 25778 * this packet has already been checked as 25779 * this was destined for the detached 25780 * connection. We need to bypass policy 25781 * check this time by attaching a dummy 25782 * ipsec_in with ipsec_in_dont_check set. 25783 */ 25784 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25785 if (connp != NULL) { 25786 TCP_STAT(tcps, tcp_time_wait_syn_success); 25787 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25788 return; 25789 } 25790 goto done; 25791 } 25792 25793 /* 25794 * rgap is the amount of stuff received out of window. A negative 25795 * value is the amount out of window. 25796 */ 25797 if (rgap < 0) { 25798 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25799 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25800 /* Fix seg_len and make sure there is something left. */ 25801 seg_len += rgap; 25802 if (seg_len <= 0) { 25803 if (flags & TH_RST) { 25804 goto done; 25805 } 25806 flags |= TH_ACK_NEEDED; 25807 seg_len = 0; 25808 goto process_ack; 25809 } 25810 } 25811 /* 25812 * Check whether we can update tcp_ts_recent. This test is 25813 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25814 * Extensions for High Performance: An Update", Internet Draft. 25815 */ 25816 if (tcp->tcp_snd_ts_ok && 25817 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25818 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25819 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25820 tcp->tcp_last_rcv_lbolt = lbolt64; 25821 } 25822 25823 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25824 /* Always ack out of order packets */ 25825 flags |= TH_ACK_NEEDED; 25826 seg_len = 0; 25827 } else if (seg_len > 0) { 25828 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25829 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25830 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25831 } 25832 if (flags & TH_RST) { 25833 (void) tcp_clean_death(tcp, 0, 28); 25834 goto done; 25835 } 25836 if (flags & TH_SYN) { 25837 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25838 TH_RST|TH_ACK); 25839 /* 25840 * Do not delete the TCP structure if it is in 25841 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25842 */ 25843 goto done; 25844 } 25845 process_ack: 25846 if (flags & TH_ACK) { 25847 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25848 if (bytes_acked <= 0) { 25849 if (bytes_acked == 0 && seg_len == 0 && 25850 new_swnd == tcp->tcp_swnd) 25851 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25852 } else { 25853 /* Acks something not sent */ 25854 flags |= TH_ACK_NEEDED; 25855 } 25856 } 25857 if (flags & TH_ACK_NEEDED) { 25858 /* 25859 * Time to send an ack for some reason. 25860 */ 25861 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25862 tcp->tcp_rnxt, TH_ACK); 25863 } 25864 done: 25865 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25866 DB_CKSUMSTART(mp) = 0; 25867 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25868 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25869 } 25870 freemsg(mp); 25871 } 25872 25873 /* 25874 * Allocate a T_SVR4_OPTMGMT_REQ. 25875 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25876 * that tcp_rput_other can drop the acks. 25877 */ 25878 static mblk_t * 25879 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25880 { 25881 mblk_t *mp; 25882 struct T_optmgmt_req *tor; 25883 struct opthdr *oh; 25884 uint_t size; 25885 char *optptr; 25886 25887 size = sizeof (*tor) + sizeof (*oh) + optlen; 25888 mp = allocb(size, BPRI_MED); 25889 if (mp == NULL) 25890 return (NULL); 25891 25892 mp->b_wptr += size; 25893 mp->b_datap->db_type = M_PROTO; 25894 tor = (struct T_optmgmt_req *)mp->b_rptr; 25895 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25896 tor->MGMT_flags = T_NEGOTIATE; 25897 tor->OPT_length = sizeof (*oh) + optlen; 25898 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25899 25900 oh = (struct opthdr *)&tor[1]; 25901 oh->level = level; 25902 oh->name = cmd; 25903 oh->len = optlen; 25904 if (optlen != 0) { 25905 optptr = (char *)&oh[1]; 25906 bcopy(opt, optptr, optlen); 25907 } 25908 return (mp); 25909 } 25910 25911 /* 25912 * TCP Timers Implementation. 25913 */ 25914 timeout_id_t 25915 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25916 { 25917 mblk_t *mp; 25918 tcp_timer_t *tcpt; 25919 tcp_t *tcp = connp->conn_tcp; 25920 tcp_stack_t *tcps = tcp->tcp_tcps; 25921 25922 ASSERT(connp->conn_sqp != NULL); 25923 25924 TCP_DBGSTAT(tcps, tcp_timeout_calls); 25925 25926 if (tcp->tcp_timercache == NULL) { 25927 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25928 } else { 25929 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 25930 mp = tcp->tcp_timercache; 25931 tcp->tcp_timercache = mp->b_next; 25932 mp->b_next = NULL; 25933 ASSERT(mp->b_wptr == NULL); 25934 } 25935 25936 CONN_INC_REF(connp); 25937 tcpt = (tcp_timer_t *)mp->b_rptr; 25938 tcpt->connp = connp; 25939 tcpt->tcpt_proc = f; 25940 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 25941 return ((timeout_id_t)mp); 25942 } 25943 25944 static void 25945 tcp_timer_callback(void *arg) 25946 { 25947 mblk_t *mp = (mblk_t *)arg; 25948 tcp_timer_t *tcpt; 25949 conn_t *connp; 25950 25951 tcpt = (tcp_timer_t *)mp->b_rptr; 25952 connp = tcpt->connp; 25953 squeue_fill(connp->conn_sqp, mp, 25954 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25955 } 25956 25957 static void 25958 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25959 { 25960 tcp_timer_t *tcpt; 25961 conn_t *connp = (conn_t *)arg; 25962 tcp_t *tcp = connp->conn_tcp; 25963 25964 tcpt = (tcp_timer_t *)mp->b_rptr; 25965 ASSERT(connp == tcpt->connp); 25966 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25967 25968 /* 25969 * If the TCP has reached the closed state, don't proceed any 25970 * further. This TCP logically does not exist on the system. 25971 * tcpt_proc could for example access queues, that have already 25972 * been qprocoff'ed off. Also see comments at the start of tcp_input 25973 */ 25974 if (tcp->tcp_state != TCPS_CLOSED) { 25975 (*tcpt->tcpt_proc)(connp); 25976 } else { 25977 tcp->tcp_timer_tid = 0; 25978 } 25979 tcp_timer_free(connp->conn_tcp, mp); 25980 } 25981 25982 /* 25983 * There is potential race with untimeout and the handler firing at the same 25984 * time. The mblock may be freed by the handler while we are trying to use 25985 * it. But since both should execute on the same squeue, this race should not 25986 * occur. 25987 */ 25988 clock_t 25989 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25990 { 25991 mblk_t *mp = (mblk_t *)id; 25992 tcp_timer_t *tcpt; 25993 clock_t delta; 25994 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25995 25996 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 25997 25998 if (mp == NULL) 25999 return (-1); 26000 26001 tcpt = (tcp_timer_t *)mp->b_rptr; 26002 ASSERT(tcpt->connp == connp); 26003 26004 delta = untimeout(tcpt->tcpt_tid); 26005 26006 if (delta >= 0) { 26007 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26008 tcp_timer_free(connp->conn_tcp, mp); 26009 CONN_DEC_REF(connp); 26010 } 26011 26012 return (delta); 26013 } 26014 26015 /* 26016 * Allocate space for the timer event. The allocation looks like mblk, but it is 26017 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26018 * 26019 * Dealing with failures: If we can't allocate from the timer cache we try 26020 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26021 * points to b_rptr. 26022 * If we can't allocate anything using allocb_tryhard(), we perform a last 26023 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26024 * save the actual allocation size in b_datap. 26025 */ 26026 mblk_t * 26027 tcp_timermp_alloc(int kmflags) 26028 { 26029 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26030 kmflags & ~KM_PANIC); 26031 26032 if (mp != NULL) { 26033 mp->b_next = mp->b_prev = NULL; 26034 mp->b_rptr = (uchar_t *)(&mp[1]); 26035 mp->b_wptr = NULL; 26036 mp->b_datap = NULL; 26037 mp->b_queue = NULL; 26038 mp->b_cont = NULL; 26039 } else if (kmflags & KM_PANIC) { 26040 /* 26041 * Failed to allocate memory for the timer. Try allocating from 26042 * dblock caches. 26043 */ 26044 /* ipclassifier calls this from a constructor - hence no tcps */ 26045 TCP_G_STAT(tcp_timermp_allocfail); 26046 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26047 if (mp == NULL) { 26048 size_t size = 0; 26049 /* 26050 * Memory is really low. Try tryhard allocation. 26051 * 26052 * ipclassifier calls this from a constructor - 26053 * hence no tcps 26054 */ 26055 TCP_G_STAT(tcp_timermp_allocdblfail); 26056 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26057 sizeof (tcp_timer_t), &size, kmflags); 26058 mp->b_rptr = (uchar_t *)(&mp[1]); 26059 mp->b_next = mp->b_prev = NULL; 26060 mp->b_wptr = (uchar_t *)-1; 26061 mp->b_datap = (dblk_t *)size; 26062 mp->b_queue = NULL; 26063 mp->b_cont = NULL; 26064 } 26065 ASSERT(mp->b_wptr != NULL); 26066 } 26067 /* ipclassifier calls this from a constructor - hence no tcps */ 26068 TCP_G_DBGSTAT(tcp_timermp_alloced); 26069 26070 return (mp); 26071 } 26072 26073 /* 26074 * Free per-tcp timer cache. 26075 * It can only contain entries from tcp_timercache. 26076 */ 26077 void 26078 tcp_timermp_free(tcp_t *tcp) 26079 { 26080 mblk_t *mp; 26081 26082 while ((mp = tcp->tcp_timercache) != NULL) { 26083 ASSERT(mp->b_wptr == NULL); 26084 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26085 kmem_cache_free(tcp_timercache, mp); 26086 } 26087 } 26088 26089 /* 26090 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26091 * events there already (currently at most two events are cached). 26092 * If the event is not allocated from the timer cache, free it right away. 26093 */ 26094 static void 26095 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26096 { 26097 mblk_t *mp1 = tcp->tcp_timercache; 26098 tcp_stack_t *tcps = tcp->tcp_tcps; 26099 26100 if (mp->b_wptr != NULL) { 26101 /* 26102 * This allocation is not from a timer cache, free it right 26103 * away. 26104 */ 26105 if (mp->b_wptr != (uchar_t *)-1) 26106 freeb(mp); 26107 else 26108 kmem_free(mp, (size_t)mp->b_datap); 26109 } else if (mp1 == NULL || mp1->b_next == NULL) { 26110 /* Cache this timer block for future allocations */ 26111 mp->b_rptr = (uchar_t *)(&mp[1]); 26112 mp->b_next = mp1; 26113 tcp->tcp_timercache = mp; 26114 } else { 26115 kmem_cache_free(tcp_timercache, mp); 26116 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26117 } 26118 } 26119 26120 /* 26121 * End of TCP Timers implementation. 26122 */ 26123 26124 /* 26125 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26126 * on the specified backing STREAMS q. Note, the caller may make the 26127 * decision to call based on the tcp_t.tcp_flow_stopped value which 26128 * when check outside the q's lock is only an advisory check ... 26129 */ 26130 26131 void 26132 tcp_setqfull(tcp_t *tcp) 26133 { 26134 queue_t *q = tcp->tcp_wq; 26135 tcp_stack_t *tcps = tcp->tcp_tcps; 26136 26137 if (!(q->q_flag & QFULL)) { 26138 mutex_enter(QLOCK(q)); 26139 if (!(q->q_flag & QFULL)) { 26140 /* still need to set QFULL */ 26141 q->q_flag |= QFULL; 26142 tcp->tcp_flow_stopped = B_TRUE; 26143 mutex_exit(QLOCK(q)); 26144 TCP_STAT(tcps, tcp_flwctl_on); 26145 } else { 26146 mutex_exit(QLOCK(q)); 26147 } 26148 } 26149 } 26150 26151 void 26152 tcp_clrqfull(tcp_t *tcp) 26153 { 26154 queue_t *q = tcp->tcp_wq; 26155 26156 if (q->q_flag & QFULL) { 26157 mutex_enter(QLOCK(q)); 26158 if (q->q_flag & QFULL) { 26159 q->q_flag &= ~QFULL; 26160 tcp->tcp_flow_stopped = B_FALSE; 26161 mutex_exit(QLOCK(q)); 26162 if (q->q_flag & QWANTW) 26163 qbackenable(q, 0); 26164 } else { 26165 mutex_exit(QLOCK(q)); 26166 } 26167 } 26168 } 26169 26170 26171 /* 26172 * kstats related to squeues i.e. not per IP instance 26173 */ 26174 static void * 26175 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26176 { 26177 kstat_t *ksp; 26178 26179 tcp_g_stat_t template = { 26180 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26181 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26182 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26183 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26184 }; 26185 26186 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26187 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26188 KSTAT_FLAG_VIRTUAL); 26189 26190 if (ksp == NULL) 26191 return (NULL); 26192 26193 bcopy(&template, tcp_g_statp, sizeof (template)); 26194 ksp->ks_data = (void *)tcp_g_statp; 26195 26196 kstat_install(ksp); 26197 return (ksp); 26198 } 26199 26200 static void 26201 tcp_g_kstat_fini(kstat_t *ksp) 26202 { 26203 if (ksp != NULL) { 26204 kstat_delete(ksp); 26205 } 26206 } 26207 26208 26209 static void * 26210 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26211 { 26212 kstat_t *ksp; 26213 26214 tcp_stat_t template = { 26215 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26216 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26217 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26218 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26219 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26220 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26221 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26222 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26223 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26224 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26225 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26226 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26227 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26228 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26229 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26230 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26231 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26232 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26233 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26234 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26235 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26236 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26237 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26238 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26239 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26240 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26241 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26242 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26243 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26244 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26245 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26246 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26247 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26248 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26249 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26250 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26251 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26252 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26253 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26254 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26255 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26256 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26257 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26258 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26259 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26260 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26261 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26262 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26263 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26264 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26265 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26266 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26267 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26268 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26269 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26270 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26271 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26272 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26273 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26274 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26275 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26276 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26277 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26278 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26279 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26280 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26281 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26282 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26283 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26284 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26285 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26286 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26287 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26288 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26289 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26290 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26291 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26292 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26293 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26294 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26295 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26296 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26297 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26298 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26299 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26300 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26301 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26302 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26303 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26304 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26305 }; 26306 26307 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26308 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26309 KSTAT_FLAG_VIRTUAL, stackid); 26310 26311 if (ksp == NULL) 26312 return (NULL); 26313 26314 bcopy(&template, tcps_statisticsp, sizeof (template)); 26315 ksp->ks_data = (void *)tcps_statisticsp; 26316 ksp->ks_private = (void *)(uintptr_t)stackid; 26317 26318 kstat_install(ksp); 26319 return (ksp); 26320 } 26321 26322 static void 26323 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26324 { 26325 if (ksp != NULL) { 26326 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26327 kstat_delete_netstack(ksp, stackid); 26328 } 26329 } 26330 26331 /* 26332 * TCP Kstats implementation 26333 */ 26334 static void * 26335 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26336 { 26337 kstat_t *ksp; 26338 26339 tcp_named_kstat_t template = { 26340 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26341 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26342 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26343 { "maxConn", KSTAT_DATA_INT32, 0 }, 26344 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26345 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26346 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26347 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26348 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26349 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26350 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26351 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26352 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26353 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26354 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26355 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26356 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26357 { "outAck", KSTAT_DATA_UINT32, 0 }, 26358 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26359 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26360 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26361 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26362 { "outControl", KSTAT_DATA_UINT32, 0 }, 26363 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26364 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26365 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26366 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26367 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26368 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26369 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26370 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26371 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26372 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26373 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26374 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26375 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26376 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26377 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26378 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26379 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26380 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26381 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26382 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26383 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26384 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26385 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26386 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26387 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26388 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26389 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26390 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26391 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26392 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26393 }; 26394 26395 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26396 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26397 26398 if (ksp == NULL) 26399 return (NULL); 26400 26401 template.rtoAlgorithm.value.ui32 = 4; 26402 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26403 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26404 template.maxConn.value.i32 = -1; 26405 26406 bcopy(&template, ksp->ks_data, sizeof (template)); 26407 ksp->ks_update = tcp_kstat_update; 26408 ksp->ks_private = (void *)(uintptr_t)stackid; 26409 26410 kstat_install(ksp); 26411 return (ksp); 26412 } 26413 26414 static void 26415 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26416 { 26417 if (ksp != NULL) { 26418 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26419 kstat_delete_netstack(ksp, stackid); 26420 } 26421 } 26422 26423 static int 26424 tcp_kstat_update(kstat_t *kp, int rw) 26425 { 26426 tcp_named_kstat_t *tcpkp; 26427 tcp_t *tcp; 26428 connf_t *connfp; 26429 conn_t *connp; 26430 int i; 26431 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26432 netstack_t *ns; 26433 tcp_stack_t *tcps; 26434 ip_stack_t *ipst; 26435 26436 if ((kp == NULL) || (kp->ks_data == NULL)) 26437 return (EIO); 26438 26439 if (rw == KSTAT_WRITE) 26440 return (EACCES); 26441 26442 ns = netstack_find_by_stackid(stackid); 26443 if (ns == NULL) 26444 return (-1); 26445 tcps = ns->netstack_tcp; 26446 if (tcps == NULL) { 26447 netstack_rele(ns); 26448 return (-1); 26449 } 26450 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26451 26452 tcpkp->currEstab.value.ui32 = 0; 26453 26454 ipst = ns->netstack_ip; 26455 26456 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26457 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26458 connp = NULL; 26459 while ((connp = 26460 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26461 tcp = connp->conn_tcp; 26462 switch (tcp_snmp_state(tcp)) { 26463 case MIB2_TCP_established: 26464 case MIB2_TCP_closeWait: 26465 tcpkp->currEstab.value.ui32++; 26466 break; 26467 } 26468 } 26469 } 26470 26471 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26472 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26473 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26474 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26475 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26476 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26477 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26478 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26479 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26480 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26481 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26482 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26483 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26484 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26485 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26486 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26487 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26488 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26489 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26490 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26491 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26492 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26493 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26494 tcpkp->inDataInorderSegs.value.ui32 = 26495 tcps->tcps_mib.tcpInDataInorderSegs; 26496 tcpkp->inDataInorderBytes.value.ui32 = 26497 tcps->tcps_mib.tcpInDataInorderBytes; 26498 tcpkp->inDataUnorderSegs.value.ui32 = 26499 tcps->tcps_mib.tcpInDataUnorderSegs; 26500 tcpkp->inDataUnorderBytes.value.ui32 = 26501 tcps->tcps_mib.tcpInDataUnorderBytes; 26502 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26503 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26504 tcpkp->inDataPartDupSegs.value.ui32 = 26505 tcps->tcps_mib.tcpInDataPartDupSegs; 26506 tcpkp->inDataPartDupBytes.value.ui32 = 26507 tcps->tcps_mib.tcpInDataPartDupBytes; 26508 tcpkp->inDataPastWinSegs.value.ui32 = 26509 tcps->tcps_mib.tcpInDataPastWinSegs; 26510 tcpkp->inDataPastWinBytes.value.ui32 = 26511 tcps->tcps_mib.tcpInDataPastWinBytes; 26512 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26513 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26514 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26515 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26516 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26517 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26518 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26519 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26520 tcpkp->timKeepaliveProbe.value.ui32 = 26521 tcps->tcps_mib.tcpTimKeepaliveProbe; 26522 tcpkp->timKeepaliveDrop.value.ui32 = 26523 tcps->tcps_mib.tcpTimKeepaliveDrop; 26524 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26525 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26526 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26527 tcpkp->outSackRetransSegs.value.ui32 = 26528 tcps->tcps_mib.tcpOutSackRetransSegs; 26529 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26530 26531 netstack_rele(ns); 26532 return (0); 26533 } 26534 26535 void 26536 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26537 { 26538 uint16_t hdr_len; 26539 ipha_t *ipha; 26540 uint8_t *nexthdrp; 26541 tcph_t *tcph; 26542 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26543 26544 /* Already has an eager */ 26545 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26546 TCP_STAT(tcps, tcp_reinput_syn); 26547 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26548 connp, SQTAG_TCP_REINPUT_EAGER); 26549 return; 26550 } 26551 26552 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26553 case IPV4_VERSION: 26554 ipha = (ipha_t *)mp->b_rptr; 26555 hdr_len = IPH_HDR_LENGTH(ipha); 26556 break; 26557 case IPV6_VERSION: 26558 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26559 &hdr_len, &nexthdrp)) { 26560 CONN_DEC_REF(connp); 26561 freemsg(mp); 26562 return; 26563 } 26564 break; 26565 } 26566 26567 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26568 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26569 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26570 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26571 } 26572 26573 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26574 SQTAG_TCP_REINPUT); 26575 } 26576 26577 static squeue_func_t 26578 tcp_squeue_switch(int val) 26579 { 26580 squeue_func_t rval = squeue_fill; 26581 26582 switch (val) { 26583 case 1: 26584 rval = squeue_enter_nodrain; 26585 break; 26586 case 2: 26587 rval = squeue_enter; 26588 break; 26589 default: 26590 break; 26591 } 26592 return (rval); 26593 } 26594 26595 /* 26596 * This is called once for each squeue - globally for all stack 26597 * instances. 26598 */ 26599 static void 26600 tcp_squeue_add(squeue_t *sqp) 26601 { 26602 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26603 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26604 26605 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26606 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26607 sqp, TCP_TIME_WAIT_DELAY); 26608 if (tcp_free_list_max_cnt == 0) { 26609 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26610 max_ncpus : boot_max_ncpus); 26611 26612 /* 26613 * Limit number of entries to 1% of availble memory / tcp_ncpus 26614 */ 26615 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26616 (tcp_ncpus * sizeof (tcp_t) * 100); 26617 } 26618 tcp_time_wait->tcp_free_list_cnt = 0; 26619 } 26620