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 mutex_enter(&tcp->tcp_eager_lock); 4199 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4200 /* Cleanup for listener */ 4201 tcp_eager_cleanup(tcp, 0); 4202 tcp->tcp_wait_for_eagers = 1; 4203 } 4204 mutex_exit(&tcp->tcp_eager_lock); 4205 4206 connp->conn_mdt_ok = B_FALSE; 4207 tcp->tcp_mdt = B_FALSE; 4208 4209 connp->conn_lso_ok = B_FALSE; 4210 tcp->tcp_lso = B_FALSE; 4211 4212 msg = NULL; 4213 switch (tcp->tcp_state) { 4214 case TCPS_CLOSED: 4215 case TCPS_IDLE: 4216 case TCPS_BOUND: 4217 case TCPS_LISTEN: 4218 break; 4219 case TCPS_SYN_SENT: 4220 msg = "tcp_close, during connect"; 4221 break; 4222 case TCPS_SYN_RCVD: 4223 /* 4224 * Close during the connect 3-way handshake 4225 * but here there may or may not be pending data 4226 * already on queue. Process almost same as in 4227 * the ESTABLISHED state. 4228 */ 4229 /* FALLTHRU */ 4230 default: 4231 if (tcp->tcp_sodirect != NULL) { 4232 /* Ok, no more sodirect */ 4233 tcp->tcp_sodirect = NULL; 4234 } 4235 4236 if (tcp->tcp_fused) 4237 tcp_unfuse(tcp); 4238 4239 /* 4240 * If SO_LINGER has set a zero linger time, abort the 4241 * connection with a reset. 4242 */ 4243 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4244 msg = "tcp_close, zero lingertime"; 4245 break; 4246 } 4247 4248 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4249 /* 4250 * Abort connection if there is unread data queued. 4251 */ 4252 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4253 msg = "tcp_close, unread data"; 4254 break; 4255 } 4256 /* 4257 * tcp_hard_bound is now cleared thus all packets go through 4258 * tcp_lookup. This fact is used by tcp_detach below. 4259 * 4260 * We have done a qwait() above which could have possibly 4261 * drained more messages in turn causing transition to a 4262 * different state. Check whether we have to do the rest 4263 * of the processing or not. 4264 */ 4265 if (tcp->tcp_state <= TCPS_LISTEN) 4266 break; 4267 4268 /* 4269 * Transmit the FIN before detaching the tcp_t. 4270 * After tcp_detach returns this queue/perimeter 4271 * no longer owns the tcp_t thus others can modify it. 4272 */ 4273 (void) tcp_xmit_end(tcp); 4274 4275 /* 4276 * If lingering on close then wait until the fin is acked, 4277 * the SO_LINGER time passes, or a reset is sent/received. 4278 */ 4279 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4280 !(tcp->tcp_fin_acked) && 4281 tcp->tcp_state >= TCPS_ESTABLISHED) { 4282 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4283 tcp->tcp_client_errno = EWOULDBLOCK; 4284 } else if (tcp->tcp_client_errno == 0) { 4285 4286 ASSERT(tcp->tcp_linger_tid == 0); 4287 4288 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4289 tcp_close_linger_timeout, 4290 tcp->tcp_lingertime * hz); 4291 4292 /* tcp_close_linger_timeout will finish close */ 4293 if (tcp->tcp_linger_tid == 0) 4294 tcp->tcp_client_errno = ENOSR; 4295 else 4296 return; 4297 } 4298 4299 /* 4300 * Check if we need to detach or just close 4301 * the instance. 4302 */ 4303 if (tcp->tcp_state <= TCPS_LISTEN) 4304 break; 4305 } 4306 4307 /* 4308 * Make sure that no other thread will access the tcp_rq of 4309 * this instance (through lookups etc.) as tcp_rq will go 4310 * away shortly. 4311 */ 4312 tcp_acceptor_hash_remove(tcp); 4313 4314 mutex_enter(&tcp->tcp_non_sq_lock); 4315 if (tcp->tcp_flow_stopped) { 4316 tcp_clrqfull(tcp); 4317 } 4318 mutex_exit(&tcp->tcp_non_sq_lock); 4319 4320 if (tcp->tcp_timer_tid != 0) { 4321 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4322 tcp->tcp_timer_tid = 0; 4323 } 4324 /* 4325 * Need to cancel those timers which will not be used when 4326 * TCP is detached. This has to be done before the tcp_wq 4327 * is set to the global queue. 4328 */ 4329 tcp_timers_stop(tcp); 4330 4331 tcp->tcp_detached = B_TRUE; 4332 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4333 tcp_time_wait_append(tcp); 4334 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4335 ASSERT(connp->conn_ref >= 3); 4336 goto finish; 4337 } 4338 4339 /* 4340 * If delta is zero the timer event wasn't executed and was 4341 * successfully canceled. In this case we need to restart it 4342 * with the minimal delta possible. 4343 */ 4344 if (delta >= 0) 4345 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4346 delta ? delta : 1); 4347 4348 ASSERT(connp->conn_ref >= 3); 4349 goto finish; 4350 } 4351 4352 /* Detach did not complete. Still need to remove q from stream. */ 4353 if (msg) { 4354 if (tcp->tcp_state == TCPS_ESTABLISHED || 4355 tcp->tcp_state == TCPS_CLOSE_WAIT) 4356 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4357 if (tcp->tcp_state == TCPS_SYN_SENT || 4358 tcp->tcp_state == TCPS_SYN_RCVD) 4359 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4360 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4361 } 4362 4363 tcp_closei_local(tcp); 4364 CONN_DEC_REF(connp); 4365 ASSERT(connp->conn_ref >= 2); 4366 4367 finish: 4368 /* 4369 * Although packets are always processed on the correct 4370 * tcp's perimeter and access is serialized via squeue's, 4371 * IP still needs a queue when sending packets in time_wait 4372 * state so use WR(tcps_g_q) till ip_output() can be 4373 * changed to deal with just connp. For read side, we 4374 * could have set tcp_rq to NULL but there are some cases 4375 * in tcp_rput_data() from early days of this code which 4376 * do a putnext without checking if tcp is closed. Those 4377 * need to be identified before both tcp_rq and tcp_wq 4378 * can be set to NULL and tcps_g_q can disappear forever. 4379 */ 4380 mutex_enter(&tcp->tcp_closelock); 4381 /* 4382 * Don't change the queues in the case of a listener that has 4383 * eagers in its q or q0. It could surprise the eagers. 4384 * Instead wait for the eagers outside the squeue. 4385 */ 4386 if (!tcp->tcp_wait_for_eagers) { 4387 tcp->tcp_detached = B_TRUE; 4388 /* 4389 * When default queue is closing we set tcps_g_q to NULL 4390 * after the close is done. 4391 */ 4392 ASSERT(tcps->tcps_g_q != NULL); 4393 tcp->tcp_rq = tcps->tcps_g_q; 4394 tcp->tcp_wq = WR(tcps->tcps_g_q); 4395 } 4396 4397 /* Signal tcp_close() to finish closing. */ 4398 tcp->tcp_closed = 1; 4399 cv_signal(&tcp->tcp_closecv); 4400 mutex_exit(&tcp->tcp_closelock); 4401 } 4402 4403 4404 /* 4405 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4406 * Some stream heads get upset if they see these later on as anything but NULL. 4407 */ 4408 static void 4409 tcp_close_mpp(mblk_t **mpp) 4410 { 4411 mblk_t *mp; 4412 4413 if ((mp = *mpp) != NULL) { 4414 do { 4415 mp->b_next = NULL; 4416 mp->b_prev = NULL; 4417 } while ((mp = mp->b_cont) != NULL); 4418 4419 mp = *mpp; 4420 *mpp = NULL; 4421 freemsg(mp); 4422 } 4423 } 4424 4425 /* Do detached close. */ 4426 static void 4427 tcp_close_detached(tcp_t *tcp) 4428 { 4429 if (tcp->tcp_fused) 4430 tcp_unfuse(tcp); 4431 4432 /* 4433 * Clustering code serializes TCP disconnect callbacks and 4434 * cluster tcp list walks by blocking a TCP disconnect callback 4435 * if a cluster tcp list walk is in progress. This ensures 4436 * accurate accounting of TCPs in the cluster code even though 4437 * the TCP list walk itself is not atomic. 4438 */ 4439 tcp_closei_local(tcp); 4440 CONN_DEC_REF(tcp->tcp_connp); 4441 } 4442 4443 /* 4444 * Stop all TCP timers, and free the timer mblks if requested. 4445 */ 4446 void 4447 tcp_timers_stop(tcp_t *tcp) 4448 { 4449 if (tcp->tcp_timer_tid != 0) { 4450 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4451 tcp->tcp_timer_tid = 0; 4452 } 4453 if (tcp->tcp_ka_tid != 0) { 4454 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4455 tcp->tcp_ka_tid = 0; 4456 } 4457 if (tcp->tcp_ack_tid != 0) { 4458 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4459 tcp->tcp_ack_tid = 0; 4460 } 4461 if (tcp->tcp_push_tid != 0) { 4462 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4463 tcp->tcp_push_tid = 0; 4464 } 4465 } 4466 4467 /* 4468 * The tcp_t is going away. Remove it from all lists and set it 4469 * to TCPS_CLOSED. The freeing up of memory is deferred until 4470 * tcp_inactive. This is needed since a thread in tcp_rput might have 4471 * done a CONN_INC_REF on this structure before it was removed from the 4472 * hashes. 4473 */ 4474 static void 4475 tcp_closei_local(tcp_t *tcp) 4476 { 4477 ire_t *ire; 4478 conn_t *connp = tcp->tcp_connp; 4479 tcp_stack_t *tcps = tcp->tcp_tcps; 4480 4481 if (!TCP_IS_SOCKET(tcp)) 4482 tcp_acceptor_hash_remove(tcp); 4483 4484 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4485 tcp->tcp_ibsegs = 0; 4486 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4487 tcp->tcp_obsegs = 0; 4488 4489 /* 4490 * If we are an eager connection hanging off a listener that 4491 * hasn't formally accepted the connection yet, get off his 4492 * list and blow off any data that we have accumulated. 4493 */ 4494 if (tcp->tcp_listener != NULL) { 4495 tcp_t *listener = tcp->tcp_listener; 4496 mutex_enter(&listener->tcp_eager_lock); 4497 /* 4498 * tcp_tconnind_started == B_TRUE means that the 4499 * conn_ind has already gone to listener. At 4500 * this point, eager will be closed but we 4501 * leave it in listeners eager list so that 4502 * if listener decides to close without doing 4503 * accept, we can clean this up. In tcp_wput_accept 4504 * we take care of the case of accept on closed 4505 * eager. 4506 */ 4507 if (!tcp->tcp_tconnind_started) { 4508 tcp_eager_unlink(tcp); 4509 mutex_exit(&listener->tcp_eager_lock); 4510 /* 4511 * We don't want to have any pointers to the 4512 * listener queue, after we have released our 4513 * reference on the listener 4514 */ 4515 ASSERT(tcps->tcps_g_q != NULL); 4516 tcp->tcp_rq = tcps->tcps_g_q; 4517 tcp->tcp_wq = WR(tcps->tcps_g_q); 4518 CONN_DEC_REF(listener->tcp_connp); 4519 } else { 4520 mutex_exit(&listener->tcp_eager_lock); 4521 } 4522 } 4523 4524 /* Stop all the timers */ 4525 tcp_timers_stop(tcp); 4526 4527 if (tcp->tcp_state == TCPS_LISTEN) { 4528 if (tcp->tcp_ip_addr_cache) { 4529 kmem_free((void *)tcp->tcp_ip_addr_cache, 4530 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4531 tcp->tcp_ip_addr_cache = NULL; 4532 } 4533 } 4534 mutex_enter(&tcp->tcp_non_sq_lock); 4535 if (tcp->tcp_flow_stopped) 4536 tcp_clrqfull(tcp); 4537 mutex_exit(&tcp->tcp_non_sq_lock); 4538 4539 tcp_bind_hash_remove(tcp); 4540 /* 4541 * If the tcp_time_wait_collector (which runs outside the squeue) 4542 * is trying to remove this tcp from the time wait list, we will 4543 * block in tcp_time_wait_remove while trying to acquire the 4544 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4545 * requires the ipcl_hash_remove to be ordered after the 4546 * tcp_time_wait_remove for the refcnt checks to work correctly. 4547 */ 4548 if (tcp->tcp_state == TCPS_TIME_WAIT) 4549 (void) tcp_time_wait_remove(tcp, NULL); 4550 CL_INET_DISCONNECT(tcp); 4551 ipcl_hash_remove(connp); 4552 4553 /* 4554 * Delete the cached ire in conn_ire_cache and also mark 4555 * the conn as CONDEMNED 4556 */ 4557 mutex_enter(&connp->conn_lock); 4558 connp->conn_state_flags |= CONN_CONDEMNED; 4559 ire = connp->conn_ire_cache; 4560 connp->conn_ire_cache = NULL; 4561 mutex_exit(&connp->conn_lock); 4562 if (ire != NULL) 4563 IRE_REFRELE_NOTR(ire); 4564 4565 /* Need to cleanup any pending ioctls */ 4566 ASSERT(tcp->tcp_time_wait_next == NULL); 4567 ASSERT(tcp->tcp_time_wait_prev == NULL); 4568 ASSERT(tcp->tcp_time_wait_expire == 0); 4569 tcp->tcp_state = TCPS_CLOSED; 4570 4571 /* Release any SSL context */ 4572 if (tcp->tcp_kssl_ent != NULL) { 4573 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4574 tcp->tcp_kssl_ent = NULL; 4575 } 4576 if (tcp->tcp_kssl_ctx != NULL) { 4577 kssl_release_ctx(tcp->tcp_kssl_ctx); 4578 tcp->tcp_kssl_ctx = NULL; 4579 } 4580 tcp->tcp_kssl_pending = B_FALSE; 4581 4582 tcp_ipsec_cleanup(tcp); 4583 } 4584 4585 /* 4586 * tcp is dying (called from ipcl_conn_destroy and error cases). 4587 * Free the tcp_t in either case. 4588 */ 4589 void 4590 tcp_free(tcp_t *tcp) 4591 { 4592 mblk_t *mp; 4593 ip6_pkt_t *ipp; 4594 4595 ASSERT(tcp != NULL); 4596 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4597 4598 tcp->tcp_rq = NULL; 4599 tcp->tcp_wq = NULL; 4600 4601 tcp_close_mpp(&tcp->tcp_xmit_head); 4602 tcp_close_mpp(&tcp->tcp_reass_head); 4603 if (tcp->tcp_rcv_list != NULL) { 4604 /* Free b_next chain */ 4605 tcp_close_mpp(&tcp->tcp_rcv_list); 4606 } 4607 if ((mp = tcp->tcp_urp_mp) != NULL) { 4608 freemsg(mp); 4609 } 4610 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4611 freemsg(mp); 4612 } 4613 4614 if (tcp->tcp_fused_sigurg_mp != NULL) { 4615 freeb(tcp->tcp_fused_sigurg_mp); 4616 tcp->tcp_fused_sigurg_mp = NULL; 4617 } 4618 4619 if (tcp->tcp_sack_info != NULL) { 4620 if (tcp->tcp_notsack_list != NULL) { 4621 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4622 } 4623 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4624 } 4625 4626 if (tcp->tcp_hopopts != NULL) { 4627 mi_free(tcp->tcp_hopopts); 4628 tcp->tcp_hopopts = NULL; 4629 tcp->tcp_hopoptslen = 0; 4630 } 4631 ASSERT(tcp->tcp_hopoptslen == 0); 4632 if (tcp->tcp_dstopts != NULL) { 4633 mi_free(tcp->tcp_dstopts); 4634 tcp->tcp_dstopts = NULL; 4635 tcp->tcp_dstoptslen = 0; 4636 } 4637 ASSERT(tcp->tcp_dstoptslen == 0); 4638 if (tcp->tcp_rtdstopts != NULL) { 4639 mi_free(tcp->tcp_rtdstopts); 4640 tcp->tcp_rtdstopts = NULL; 4641 tcp->tcp_rtdstoptslen = 0; 4642 } 4643 ASSERT(tcp->tcp_rtdstoptslen == 0); 4644 if (tcp->tcp_rthdr != NULL) { 4645 mi_free(tcp->tcp_rthdr); 4646 tcp->tcp_rthdr = NULL; 4647 tcp->tcp_rthdrlen = 0; 4648 } 4649 ASSERT(tcp->tcp_rthdrlen == 0); 4650 4651 ipp = &tcp->tcp_sticky_ipp; 4652 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4653 IPPF_RTHDR)) 4654 ip6_pkt_free(ipp); 4655 4656 /* 4657 * Free memory associated with the tcp/ip header template. 4658 */ 4659 4660 if (tcp->tcp_iphc != NULL) 4661 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4662 4663 /* 4664 * Following is really a blowing away a union. 4665 * It happens to have exactly two members of identical size 4666 * the following code is enough. 4667 */ 4668 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4669 } 4670 4671 4672 /* 4673 * Put a connection confirmation message upstream built from the 4674 * address information within 'iph' and 'tcph'. Report our success or failure. 4675 */ 4676 static boolean_t 4677 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4678 mblk_t **defermp) 4679 { 4680 sin_t sin; 4681 sin6_t sin6; 4682 mblk_t *mp; 4683 char *optp = NULL; 4684 int optlen = 0; 4685 cred_t *cr; 4686 4687 if (defermp != NULL) 4688 *defermp = NULL; 4689 4690 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4691 /* 4692 * Return in T_CONN_CON results of option negotiation through 4693 * the T_CONN_REQ. Note: If there is an real end-to-end option 4694 * negotiation, then what is received from remote end needs 4695 * to be taken into account but there is no such thing (yet?) 4696 * in our TCP/IP. 4697 * Note: We do not use mi_offset_param() here as 4698 * tcp_opts_conn_req contents do not directly come from 4699 * an application and are either generated in kernel or 4700 * from user input that was already verified. 4701 */ 4702 mp = tcp->tcp_conn.tcp_opts_conn_req; 4703 optp = (char *)(mp->b_rptr + 4704 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4705 optlen = (int) 4706 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4707 } 4708 4709 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4710 ipha_t *ipha = (ipha_t *)iphdr; 4711 4712 /* packet is IPv4 */ 4713 if (tcp->tcp_family == AF_INET) { 4714 sin = sin_null; 4715 sin.sin_addr.s_addr = ipha->ipha_src; 4716 sin.sin_port = *(uint16_t *)tcph->th_lport; 4717 sin.sin_family = AF_INET; 4718 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4719 (int)sizeof (sin_t), optp, optlen); 4720 } else { 4721 sin6 = sin6_null; 4722 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4723 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4724 sin6.sin6_family = AF_INET6; 4725 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4726 (int)sizeof (sin6_t), optp, optlen); 4727 4728 } 4729 } else { 4730 ip6_t *ip6h = (ip6_t *)iphdr; 4731 4732 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4733 ASSERT(tcp->tcp_family == AF_INET6); 4734 sin6 = sin6_null; 4735 sin6.sin6_addr = ip6h->ip6_src; 4736 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4737 sin6.sin6_family = AF_INET6; 4738 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4739 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4740 (int)sizeof (sin6_t), optp, optlen); 4741 } 4742 4743 if (!mp) 4744 return (B_FALSE); 4745 4746 if ((cr = DB_CRED(idmp)) != NULL) { 4747 mblk_setcred(mp, cr); 4748 DB_CPID(mp) = DB_CPID(idmp); 4749 } 4750 4751 if (defermp == NULL) 4752 putnext(tcp->tcp_rq, mp); 4753 else 4754 *defermp = mp; 4755 4756 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4757 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4758 return (B_TRUE); 4759 } 4760 4761 /* 4762 * Defense for the SYN attack - 4763 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4764 * one from the list of droppable eagers. This list is a subset of q0. 4765 * see comments before the definition of MAKE_DROPPABLE(). 4766 * 2. Don't drop a SYN request before its first timeout. This gives every 4767 * request at least til the first timeout to complete its 3-way handshake. 4768 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4769 * requests currently on the queue that has timed out. This will be used 4770 * as an indicator of whether an attack is under way, so that appropriate 4771 * actions can be taken. (It's incremented in tcp_timer() and decremented 4772 * either when eager goes into ESTABLISHED, or gets freed up.) 4773 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4774 * # of timeout drops back to <= q0len/32 => SYN alert off 4775 */ 4776 static boolean_t 4777 tcp_drop_q0(tcp_t *tcp) 4778 { 4779 tcp_t *eager; 4780 mblk_t *mp; 4781 tcp_stack_t *tcps = tcp->tcp_tcps; 4782 4783 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4784 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4785 4786 /* Pick oldest eager from the list of droppable eagers */ 4787 eager = tcp->tcp_eager_prev_drop_q0; 4788 4789 /* If list is empty. return B_FALSE */ 4790 if (eager == tcp) { 4791 return (B_FALSE); 4792 } 4793 4794 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4795 if ((mp = allocb(0, BPRI_HI)) == NULL) 4796 return (B_FALSE); 4797 4798 /* 4799 * Take this eager out from the list of droppable eagers since we are 4800 * going to drop it. 4801 */ 4802 MAKE_UNDROPPABLE(eager); 4803 4804 if (tcp->tcp_debug) { 4805 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4806 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4807 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4808 tcp->tcp_conn_req_cnt_q0, 4809 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4810 } 4811 4812 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4813 4814 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4815 CONN_INC_REF(eager->tcp_connp); 4816 4817 /* Mark the IRE created for this SYN request temporary */ 4818 tcp_ip_ire_mark_advice(eager); 4819 squeue_fill(eager->tcp_connp->conn_sqp, mp, 4820 tcp_clean_death_wrapper, eager->tcp_connp, SQTAG_TCP_DROP_Q0); 4821 4822 return (B_TRUE); 4823 } 4824 4825 int 4826 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4827 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4828 { 4829 tcp_t *ltcp = lconnp->conn_tcp; 4830 tcp_t *tcp = connp->conn_tcp; 4831 mblk_t *tpi_mp; 4832 ipha_t *ipha; 4833 ip6_t *ip6h; 4834 sin6_t sin6; 4835 in6_addr_t v6dst; 4836 int err; 4837 int ifindex = 0; 4838 cred_t *cr; 4839 tcp_stack_t *tcps = tcp->tcp_tcps; 4840 4841 if (ipvers == IPV4_VERSION) { 4842 ipha = (ipha_t *)mp->b_rptr; 4843 4844 connp->conn_send = ip_output; 4845 connp->conn_recv = tcp_input; 4846 4847 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4848 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4849 4850 sin6 = sin6_null; 4851 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4852 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4853 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4854 sin6.sin6_family = AF_INET6; 4855 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4856 lconnp->conn_zoneid, tcps->tcps_netstack); 4857 if (tcp->tcp_recvdstaddr) { 4858 sin6_t sin6d; 4859 4860 sin6d = sin6_null; 4861 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4862 &sin6d.sin6_addr); 4863 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4864 sin6d.sin6_family = AF_INET; 4865 tpi_mp = mi_tpi_extconn_ind(NULL, 4866 (char *)&sin6d, sizeof (sin6_t), 4867 (char *)&tcp, 4868 (t_scalar_t)sizeof (intptr_t), 4869 (char *)&sin6d, sizeof (sin6_t), 4870 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4871 } else { 4872 tpi_mp = mi_tpi_conn_ind(NULL, 4873 (char *)&sin6, sizeof (sin6_t), 4874 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4875 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4876 } 4877 } else { 4878 ip6h = (ip6_t *)mp->b_rptr; 4879 4880 connp->conn_send = ip_output_v6; 4881 connp->conn_recv = tcp_input; 4882 4883 connp->conn_srcv6 = ip6h->ip6_dst; 4884 connp->conn_remv6 = ip6h->ip6_src; 4885 4886 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4887 ifindex = (int)DB_CKSUMSTUFF(mp); 4888 DB_CKSUMSTUFF(mp) = 0; 4889 4890 sin6 = sin6_null; 4891 sin6.sin6_addr = ip6h->ip6_src; 4892 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4893 sin6.sin6_family = AF_INET6; 4894 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4895 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4896 lconnp->conn_zoneid, tcps->tcps_netstack); 4897 4898 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4899 /* Pass up the scope_id of remote addr */ 4900 sin6.sin6_scope_id = ifindex; 4901 } else { 4902 sin6.sin6_scope_id = 0; 4903 } 4904 if (tcp->tcp_recvdstaddr) { 4905 sin6_t sin6d; 4906 4907 sin6d = sin6_null; 4908 sin6.sin6_addr = ip6h->ip6_dst; 4909 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4910 sin6d.sin6_family = AF_INET; 4911 tpi_mp = mi_tpi_extconn_ind(NULL, 4912 (char *)&sin6d, sizeof (sin6_t), 4913 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4914 (char *)&sin6d, sizeof (sin6_t), 4915 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4916 } else { 4917 tpi_mp = mi_tpi_conn_ind(NULL, 4918 (char *)&sin6, sizeof (sin6_t), 4919 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4920 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4921 } 4922 } 4923 4924 if (tpi_mp == NULL) 4925 return (ENOMEM); 4926 4927 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4928 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4929 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4930 connp->conn_fully_bound = B_FALSE; 4931 4932 /* Inherit information from the "parent" */ 4933 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4934 tcp->tcp_family = ltcp->tcp_family; 4935 tcp->tcp_wq = ltcp->tcp_wq; 4936 tcp->tcp_rq = ltcp->tcp_rq; 4937 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4938 tcp->tcp_detached = B_TRUE; 4939 if ((err = tcp_init_values(tcp)) != 0) { 4940 freemsg(tpi_mp); 4941 return (err); 4942 } 4943 4944 if (ipvers == IPV4_VERSION) { 4945 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4946 freemsg(tpi_mp); 4947 return (err); 4948 } 4949 ASSERT(tcp->tcp_ipha != NULL); 4950 } else { 4951 /* ifindex must be already set */ 4952 ASSERT(ifindex != 0); 4953 4954 if (ltcp->tcp_bound_if != 0) { 4955 /* 4956 * Set newtcp's bound_if equal to 4957 * listener's value. If ifindex is 4958 * not the same as ltcp->tcp_bound_if, 4959 * it must be a packet for the ipmp group 4960 * of interfaces 4961 */ 4962 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4963 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4964 tcp->tcp_bound_if = ifindex; 4965 } 4966 4967 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4968 tcp->tcp_recvifindex = 0; 4969 tcp->tcp_recvhops = 0xffffffffU; 4970 ASSERT(tcp->tcp_ip6h != NULL); 4971 } 4972 4973 tcp->tcp_lport = ltcp->tcp_lport; 4974 4975 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4976 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4977 /* 4978 * Listener had options of some sort; eager inherits. 4979 * Free up the eager template and allocate one 4980 * of the right size. 4981 */ 4982 if (tcp->tcp_hdr_grown) { 4983 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4984 } else { 4985 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4986 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4987 } 4988 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4989 KM_NOSLEEP); 4990 if (tcp->tcp_iphc == NULL) { 4991 tcp->tcp_iphc_len = 0; 4992 freemsg(tpi_mp); 4993 return (ENOMEM); 4994 } 4995 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4996 tcp->tcp_hdr_grown = B_TRUE; 4997 } 4998 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4999 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5000 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5001 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 5002 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 5003 5004 /* 5005 * Copy the IP+TCP header template from listener to eager 5006 */ 5007 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5008 if (tcp->tcp_ipversion == IPV6_VERSION) { 5009 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 5010 IPPROTO_RAW) { 5011 tcp->tcp_ip6h = 5012 (ip6_t *)(tcp->tcp_iphc + 5013 sizeof (ip6i_t)); 5014 } else { 5015 tcp->tcp_ip6h = 5016 (ip6_t *)(tcp->tcp_iphc); 5017 } 5018 tcp->tcp_ipha = NULL; 5019 } else { 5020 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5021 tcp->tcp_ip6h = NULL; 5022 } 5023 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5024 tcp->tcp_ip_hdr_len); 5025 } else { 5026 /* 5027 * only valid case when ipversion of listener and 5028 * eager differ is when listener is IPv6 and 5029 * eager is IPv4. 5030 * Eager header template has been initialized to the 5031 * maximum v4 header sizes, which includes space for 5032 * TCP and IP options. 5033 */ 5034 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 5035 (tcp->tcp_ipversion == IPV4_VERSION)); 5036 ASSERT(tcp->tcp_iphc_len >= 5037 TCP_MAX_COMBINED_HEADER_LENGTH); 5038 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5039 /* copy IP header fields individually */ 5040 tcp->tcp_ipha->ipha_ttl = 5041 ltcp->tcp_ip6h->ip6_hops; 5042 bcopy(ltcp->tcp_tcph->th_lport, 5043 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 5044 } 5045 5046 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5047 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 5048 sizeof (in_port_t)); 5049 5050 if (ltcp->tcp_lport == 0) { 5051 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 5052 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 5053 sizeof (in_port_t)); 5054 } 5055 5056 if (tcp->tcp_ipversion == IPV4_VERSION) { 5057 ASSERT(ipha != NULL); 5058 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5059 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5060 5061 /* Source routing option copyover (reverse it) */ 5062 if (tcps->tcps_rev_src_routes) 5063 tcp_opt_reverse(tcp, ipha); 5064 } else { 5065 ASSERT(ip6h != NULL); 5066 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 5067 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 5068 } 5069 5070 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5071 ASSERT(!tcp->tcp_tconnind_started); 5072 /* 5073 * If the SYN contains a credential, it's a loopback packet; attach 5074 * the credential to the TPI message. 5075 */ 5076 if ((cr = DB_CRED(idmp)) != NULL) { 5077 mblk_setcred(tpi_mp, cr); 5078 DB_CPID(tpi_mp) = DB_CPID(idmp); 5079 } 5080 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5081 5082 /* Inherit the listener's SSL protection state */ 5083 5084 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5085 kssl_hold_ent(tcp->tcp_kssl_ent); 5086 tcp->tcp_kssl_pending = B_TRUE; 5087 } 5088 5089 return (0); 5090 } 5091 5092 5093 int 5094 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 5095 tcph_t *tcph, mblk_t *idmp) 5096 { 5097 tcp_t *ltcp = lconnp->conn_tcp; 5098 tcp_t *tcp = connp->conn_tcp; 5099 sin_t sin; 5100 mblk_t *tpi_mp = NULL; 5101 int err; 5102 cred_t *cr; 5103 tcp_stack_t *tcps = tcp->tcp_tcps; 5104 5105 sin = sin_null; 5106 sin.sin_addr.s_addr = ipha->ipha_src; 5107 sin.sin_port = *(uint16_t *)tcph->th_lport; 5108 sin.sin_family = AF_INET; 5109 if (ltcp->tcp_recvdstaddr) { 5110 sin_t sind; 5111 5112 sind = sin_null; 5113 sind.sin_addr.s_addr = ipha->ipha_dst; 5114 sind.sin_port = *(uint16_t *)tcph->th_fport; 5115 sind.sin_family = AF_INET; 5116 tpi_mp = mi_tpi_extconn_ind(NULL, 5117 (char *)&sind, sizeof (sin_t), (char *)&tcp, 5118 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 5119 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5120 } else { 5121 tpi_mp = mi_tpi_conn_ind(NULL, 5122 (char *)&sin, sizeof (sin_t), 5123 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 5124 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 5125 } 5126 5127 if (tpi_mp == NULL) { 5128 return (ENOMEM); 5129 } 5130 5131 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 5132 connp->conn_send = ip_output; 5133 connp->conn_recv = tcp_input; 5134 connp->conn_fully_bound = B_FALSE; 5135 5136 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 5137 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 5138 connp->conn_fport = *(uint16_t *)tcph->th_lport; 5139 connp->conn_lport = *(uint16_t *)tcph->th_fport; 5140 5141 /* Inherit information from the "parent" */ 5142 tcp->tcp_ipversion = ltcp->tcp_ipversion; 5143 tcp->tcp_family = ltcp->tcp_family; 5144 tcp->tcp_wq = ltcp->tcp_wq; 5145 tcp->tcp_rq = ltcp->tcp_rq; 5146 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 5147 tcp->tcp_detached = B_TRUE; 5148 if ((err = tcp_init_values(tcp)) != 0) { 5149 freemsg(tpi_mp); 5150 return (err); 5151 } 5152 5153 /* 5154 * Let's make sure that eager tcp template has enough space to 5155 * copy IPv4 listener's tcp template. Since the conn_t structure is 5156 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 5157 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 5158 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 5159 * extension headers or with ip6i_t struct). Note that bcopy() below 5160 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 5161 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 5162 */ 5163 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5164 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5165 5166 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5167 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5168 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5169 tcp->tcp_ttl = ltcp->tcp_ttl; 5170 tcp->tcp_tos = ltcp->tcp_tos; 5171 5172 /* Copy the IP+TCP header template from listener to eager */ 5173 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5174 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5175 tcp->tcp_ip6h = NULL; 5176 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5177 tcp->tcp_ip_hdr_len); 5178 5179 /* Initialize the IP addresses and Ports */ 5180 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5181 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5182 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5183 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5184 5185 /* Source routing option copyover (reverse it) */ 5186 if (tcps->tcps_rev_src_routes) 5187 tcp_opt_reverse(tcp, ipha); 5188 5189 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5190 ASSERT(!tcp->tcp_tconnind_started); 5191 5192 /* 5193 * If the SYN contains a credential, it's a loopback packet; attach 5194 * the credential to the TPI message. 5195 */ 5196 if ((cr = DB_CRED(idmp)) != NULL) { 5197 mblk_setcred(tpi_mp, cr); 5198 DB_CPID(tpi_mp) = DB_CPID(idmp); 5199 } 5200 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5201 5202 /* Inherit the listener's SSL protection state */ 5203 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5204 kssl_hold_ent(tcp->tcp_kssl_ent); 5205 tcp->tcp_kssl_pending = B_TRUE; 5206 } 5207 5208 return (0); 5209 } 5210 5211 /* 5212 * sets up conn for ipsec. 5213 * if the first mblk is M_CTL it is consumed and mpp is updated. 5214 * in case of error mpp is freed. 5215 */ 5216 conn_t * 5217 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5218 { 5219 conn_t *connp = tcp->tcp_connp; 5220 conn_t *econnp; 5221 squeue_t *new_sqp; 5222 mblk_t *first_mp = *mpp; 5223 mblk_t *mp = *mpp; 5224 boolean_t mctl_present = B_FALSE; 5225 uint_t ipvers; 5226 5227 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 5228 if (econnp == NULL) { 5229 freemsg(first_mp); 5230 return (NULL); 5231 } 5232 if (DB_TYPE(mp) == M_CTL) { 5233 if (mp->b_cont == NULL || 5234 mp->b_cont->b_datap->db_type != M_DATA) { 5235 freemsg(first_mp); 5236 return (NULL); 5237 } 5238 mp = mp->b_cont; 5239 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5240 freemsg(first_mp); 5241 return (NULL); 5242 } 5243 5244 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5245 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5246 mctl_present = B_TRUE; 5247 } else { 5248 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5249 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5250 } 5251 5252 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5253 DB_CKSUMSTART(mp) = 0; 5254 5255 ASSERT(OK_32PTR(mp->b_rptr)); 5256 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5257 if (ipvers == IPV4_VERSION) { 5258 uint16_t *up; 5259 uint32_t ports; 5260 ipha_t *ipha; 5261 5262 ipha = (ipha_t *)mp->b_rptr; 5263 up = (uint16_t *)((uchar_t *)ipha + 5264 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5265 ports = *(uint32_t *)up; 5266 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5267 ipha->ipha_dst, ipha->ipha_src, ports); 5268 } else { 5269 uint16_t *up; 5270 uint32_t ports; 5271 uint16_t ip_hdr_len; 5272 uint8_t *nexthdrp; 5273 ip6_t *ip6h; 5274 tcph_t *tcph; 5275 5276 ip6h = (ip6_t *)mp->b_rptr; 5277 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5278 ip_hdr_len = IPV6_HDR_LEN; 5279 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5280 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5281 CONN_DEC_REF(econnp); 5282 freemsg(first_mp); 5283 return (NULL); 5284 } 5285 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5286 up = (uint16_t *)tcph->th_lport; 5287 ports = *(uint32_t *)up; 5288 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5289 ip6h->ip6_dst, ip6h->ip6_src, ports); 5290 } 5291 5292 /* 5293 * The caller already ensured that there is a sqp present. 5294 */ 5295 econnp->conn_sqp = new_sqp; 5296 5297 if (connp->conn_policy != NULL) { 5298 ipsec_in_t *ii; 5299 ii = (ipsec_in_t *)(first_mp->b_rptr); 5300 ASSERT(ii->ipsec_in_policy == NULL); 5301 IPPH_REFHOLD(connp->conn_policy); 5302 ii->ipsec_in_policy = connp->conn_policy; 5303 5304 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5305 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5306 CONN_DEC_REF(econnp); 5307 freemsg(first_mp); 5308 return (NULL); 5309 } 5310 } 5311 5312 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5313 CONN_DEC_REF(econnp); 5314 freemsg(first_mp); 5315 return (NULL); 5316 } 5317 5318 /* 5319 * If we know we have some policy, pass the "IPSEC" 5320 * options size TCP uses this adjust the MSS. 5321 */ 5322 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5323 if (mctl_present) { 5324 freeb(first_mp); 5325 *mpp = mp; 5326 } 5327 5328 return (econnp); 5329 } 5330 5331 /* 5332 * tcp_get_conn/tcp_free_conn 5333 * 5334 * tcp_get_conn is used to get a clean tcp connection structure. 5335 * It tries to reuse the connections put on the freelist by the 5336 * time_wait_collector failing which it goes to kmem_cache. This 5337 * way has two benefits compared to just allocating from and 5338 * freeing to kmem_cache. 5339 * 1) The time_wait_collector can free (which includes the cleanup) 5340 * outside the squeue. So when the interrupt comes, we have a clean 5341 * connection sitting in the freelist. Obviously, this buys us 5342 * performance. 5343 * 5344 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5345 * has multiple disadvantages - tying up the squeue during alloc, and the 5346 * fact that IPSec policy initialization has to happen here which 5347 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5348 * But allocating the conn/tcp in IP land is also not the best since 5349 * we can't check the 'q' and 'q0' which are protected by squeue and 5350 * blindly allocate memory which might have to be freed here if we are 5351 * not allowed to accept the connection. By using the freelist and 5352 * putting the conn/tcp back in freelist, we don't pay a penalty for 5353 * allocating memory without checking 'q/q0' and freeing it if we can't 5354 * accept the connection. 5355 * 5356 * Care should be taken to put the conn back in the same squeue's freelist 5357 * from which it was allocated. Best results are obtained if conn is 5358 * allocated from listener's squeue and freed to the same. Time wait 5359 * collector will free up the freelist is the connection ends up sitting 5360 * there for too long. 5361 */ 5362 void * 5363 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5364 { 5365 tcp_t *tcp = NULL; 5366 conn_t *connp = NULL; 5367 squeue_t *sqp = (squeue_t *)arg; 5368 tcp_squeue_priv_t *tcp_time_wait; 5369 netstack_t *ns; 5370 5371 tcp_time_wait = 5372 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5373 5374 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5375 tcp = tcp_time_wait->tcp_free_list; 5376 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5377 if (tcp != NULL) { 5378 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5379 tcp_time_wait->tcp_free_list_cnt--; 5380 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5381 tcp->tcp_time_wait_next = NULL; 5382 connp = tcp->tcp_connp; 5383 connp->conn_flags |= IPCL_REUSED; 5384 5385 ASSERT(tcp->tcp_tcps == NULL); 5386 ASSERT(connp->conn_netstack == NULL); 5387 ASSERT(tcp->tcp_rsrv_mp != NULL); 5388 ns = tcps->tcps_netstack; 5389 netstack_hold(ns); 5390 connp->conn_netstack = ns; 5391 tcp->tcp_tcps = tcps; 5392 TCPS_REFHOLD(tcps); 5393 ipcl_globalhash_insert(connp); 5394 return ((void *)connp); 5395 } 5396 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5397 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5398 tcps->tcps_netstack)) == NULL) 5399 return (NULL); 5400 tcp = connp->conn_tcp; 5401 /* 5402 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5403 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5404 */ 5405 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5406 ipcl_conn_destroy(connp); 5407 return (NULL); 5408 } 5409 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5410 tcp->tcp_tcps = tcps; 5411 TCPS_REFHOLD(tcps); 5412 5413 return ((void *)connp); 5414 } 5415 5416 /* 5417 * Update the cached label for the given tcp_t. This should be called once per 5418 * connection, and before any packets are sent or tcp_process_options is 5419 * invoked. Returns B_FALSE if the correct label could not be constructed. 5420 */ 5421 static boolean_t 5422 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5423 { 5424 conn_t *connp = tcp->tcp_connp; 5425 5426 if (tcp->tcp_ipversion == IPV4_VERSION) { 5427 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5428 int added; 5429 5430 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5431 connp->conn_mac_exempt, 5432 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5433 return (B_FALSE); 5434 5435 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5436 if (added == -1) 5437 return (B_FALSE); 5438 tcp->tcp_hdr_len += added; 5439 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5440 tcp->tcp_ip_hdr_len += added; 5441 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5442 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5443 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5444 tcp->tcp_hdr_len); 5445 if (added == -1) 5446 return (B_FALSE); 5447 tcp->tcp_hdr_len += added; 5448 tcp->tcp_tcph = (tcph_t *) 5449 ((uchar_t *)tcp->tcp_tcph + added); 5450 tcp->tcp_ip_hdr_len += added; 5451 } 5452 } else { 5453 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5454 5455 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5456 connp->conn_mac_exempt, 5457 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5458 return (B_FALSE); 5459 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5460 &tcp->tcp_label_len, optbuf) != 0) 5461 return (B_FALSE); 5462 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5463 return (B_FALSE); 5464 } 5465 5466 connp->conn_ulp_labeled = 1; 5467 5468 return (B_TRUE); 5469 } 5470 5471 /* BEGIN CSTYLED */ 5472 /* 5473 * 5474 * The sockfs ACCEPT path: 5475 * ======================= 5476 * 5477 * The eager is now established in its own perimeter as soon as SYN is 5478 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5479 * completes the accept processing on the acceptor STREAM. The sending 5480 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5481 * listener but a TLI/XTI listener completes the accept processing 5482 * on the listener perimeter. 5483 * 5484 * Common control flow for 3 way handshake: 5485 * ---------------------------------------- 5486 * 5487 * incoming SYN (listener perimeter) -> tcp_rput_data() 5488 * -> tcp_conn_request() 5489 * 5490 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5491 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5492 * 5493 * Sockfs ACCEPT Path: 5494 * ------------------- 5495 * 5496 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5497 * as STREAM entry point) 5498 * 5499 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5500 * 5501 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5502 * association (we are not behind eager's squeue but sockfs is protecting us 5503 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5504 * is changed to point at tcp_wput(). 5505 * 5506 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5507 * listener (done on listener's perimeter). 5508 * 5509 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5510 * accept. 5511 * 5512 * TLI/XTI client ACCEPT path: 5513 * --------------------------- 5514 * 5515 * soaccept() sends T_CONN_RES on the listener STREAM. 5516 * 5517 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5518 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5519 * 5520 * Locks: 5521 * ====== 5522 * 5523 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5524 * and listeners->tcp_eager_next_q. 5525 * 5526 * Referencing: 5527 * ============ 5528 * 5529 * 1) We start out in tcp_conn_request by eager placing a ref on 5530 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5531 * 5532 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5533 * doing so we place a ref on the eager. This ref is finally dropped at the 5534 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5535 * reference is dropped by the squeue framework. 5536 * 5537 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5538 * 5539 * The reference must be released by the same entity that added the reference 5540 * In the above scheme, the eager is the entity that adds and releases the 5541 * references. Note that tcp_accept_finish executes in the squeue of the eager 5542 * (albeit after it is attached to the acceptor stream). Though 1. executes 5543 * in the listener's squeue, the eager is nascent at this point and the 5544 * reference can be considered to have been added on behalf of the eager. 5545 * 5546 * Eager getting a Reset or listener closing: 5547 * ========================================== 5548 * 5549 * Once the listener and eager are linked, the listener never does the unlink. 5550 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5551 * a message on all eager perimeter. The eager then does the unlink, clears 5552 * any pointers to the listener's queue and drops the reference to the 5553 * listener. The listener waits in tcp_close outside the squeue until its 5554 * refcount has dropped to 1. This ensures that the listener has waited for 5555 * all eagers to clear their association with the listener. 5556 * 5557 * Similarly, if eager decides to go away, it can unlink itself and close. 5558 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5559 * the reference to eager is still valid because of the extra ref we put 5560 * in tcp_send_conn_ind. 5561 * 5562 * Listener can always locate the eager under the protection 5563 * of the listener->tcp_eager_lock, and then do a refhold 5564 * on the eager during the accept processing. 5565 * 5566 * The acceptor stream accesses the eager in the accept processing 5567 * based on the ref placed on eager before sending T_conn_ind. 5568 * The only entity that can negate this refhold is a listener close 5569 * which is mutually exclusive with an active acceptor stream. 5570 * 5571 * Eager's reference on the listener 5572 * =================================== 5573 * 5574 * If the accept happens (even on a closed eager) the eager drops its 5575 * reference on the listener at the start of tcp_accept_finish. If the 5576 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5577 * the reference is dropped in tcp_closei_local. If the listener closes, 5578 * the reference is dropped in tcp_eager_kill. In all cases the reference 5579 * is dropped while executing in the eager's context (squeue). 5580 */ 5581 /* END CSTYLED */ 5582 5583 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5584 5585 /* 5586 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5587 * tcp_rput_data will not see any SYN packets. 5588 */ 5589 /* ARGSUSED */ 5590 void 5591 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5592 { 5593 tcph_t *tcph; 5594 uint32_t seg_seq; 5595 tcp_t *eager; 5596 uint_t ipvers; 5597 ipha_t *ipha; 5598 ip6_t *ip6h; 5599 int err; 5600 conn_t *econnp = NULL; 5601 squeue_t *new_sqp; 5602 mblk_t *mp1; 5603 uint_t ip_hdr_len; 5604 conn_t *connp = (conn_t *)arg; 5605 tcp_t *tcp = connp->conn_tcp; 5606 cred_t *credp; 5607 tcp_stack_t *tcps = tcp->tcp_tcps; 5608 ip_stack_t *ipst; 5609 5610 if (tcp->tcp_state != TCPS_LISTEN) 5611 goto error2; 5612 5613 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5614 5615 mutex_enter(&tcp->tcp_eager_lock); 5616 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5617 mutex_exit(&tcp->tcp_eager_lock); 5618 TCP_STAT(tcps, tcp_listendrop); 5619 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5620 if (tcp->tcp_debug) { 5621 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5622 "tcp_conn_request: listen backlog (max=%d) " 5623 "overflow (%d pending) on %s", 5624 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5625 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5626 } 5627 goto error2; 5628 } 5629 5630 if (tcp->tcp_conn_req_cnt_q0 >= 5631 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5632 /* 5633 * Q0 is full. Drop a pending half-open req from the queue 5634 * to make room for the new SYN req. Also mark the time we 5635 * drop a SYN. 5636 * 5637 * A more aggressive defense against SYN attack will 5638 * be to set the "tcp_syn_defense" flag now. 5639 */ 5640 TCP_STAT(tcps, tcp_listendropq0); 5641 tcp->tcp_last_rcv_lbolt = lbolt64; 5642 if (!tcp_drop_q0(tcp)) { 5643 mutex_exit(&tcp->tcp_eager_lock); 5644 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5645 if (tcp->tcp_debug) { 5646 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5647 "tcp_conn_request: listen half-open queue " 5648 "(max=%d) full (%d pending) on %s", 5649 tcps->tcps_conn_req_max_q0, 5650 tcp->tcp_conn_req_cnt_q0, 5651 tcp_display(tcp, NULL, 5652 DISP_PORT_ONLY)); 5653 } 5654 goto error2; 5655 } 5656 } 5657 mutex_exit(&tcp->tcp_eager_lock); 5658 5659 /* 5660 * IP adds STRUIO_EAGER and ensures that the received packet is 5661 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5662 * link local address. If IPSec is enabled, db_struioflag has 5663 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5664 * otherwise an error case if neither of them is set. 5665 */ 5666 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5667 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5668 DB_CKSUMSTART(mp) = 0; 5669 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5670 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5671 if (econnp == NULL) 5672 goto error2; 5673 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5674 econnp->conn_sqp = new_sqp; 5675 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5676 /* 5677 * mp is updated in tcp_get_ipsec_conn(). 5678 */ 5679 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5680 if (econnp == NULL) { 5681 /* 5682 * mp freed by tcp_get_ipsec_conn. 5683 */ 5684 return; 5685 } 5686 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5687 } else { 5688 goto error2; 5689 } 5690 5691 ASSERT(DB_TYPE(mp) == M_DATA); 5692 5693 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5694 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5695 ASSERT(OK_32PTR(mp->b_rptr)); 5696 if (ipvers == IPV4_VERSION) { 5697 ipha = (ipha_t *)mp->b_rptr; 5698 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5699 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5700 } else { 5701 ip6h = (ip6_t *)mp->b_rptr; 5702 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5703 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5704 } 5705 5706 if (tcp->tcp_family == AF_INET) { 5707 ASSERT(ipvers == IPV4_VERSION); 5708 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5709 } else { 5710 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5711 } 5712 5713 if (err) 5714 goto error3; 5715 5716 eager = econnp->conn_tcp; 5717 5718 /* 5719 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5720 * will always have that to send up. Otherwise, we need to do 5721 * special handling in case the allocation fails at that time. 5722 */ 5723 ASSERT(eager->tcp_ordrel_mp == NULL); 5724 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5725 goto error3; 5726 5727 /* Inherit various TCP parameters from the listener */ 5728 eager->tcp_naglim = tcp->tcp_naglim; 5729 eager->tcp_first_timer_threshold = 5730 tcp->tcp_first_timer_threshold; 5731 eager->tcp_second_timer_threshold = 5732 tcp->tcp_second_timer_threshold; 5733 5734 eager->tcp_first_ctimer_threshold = 5735 tcp->tcp_first_ctimer_threshold; 5736 eager->tcp_second_ctimer_threshold = 5737 tcp->tcp_second_ctimer_threshold; 5738 5739 /* 5740 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5741 * If it does not, the eager's receive window will be set to the 5742 * listener's receive window later in this function. 5743 */ 5744 eager->tcp_rwnd = 0; 5745 5746 /* 5747 * Inherit listener's tcp_init_cwnd. Need to do this before 5748 * calling tcp_process_options() where tcp_mss_set() is called 5749 * to set the initial cwnd. 5750 */ 5751 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5752 5753 /* 5754 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5755 * zone id before the accept is completed in tcp_wput_accept(). 5756 */ 5757 econnp->conn_zoneid = connp->conn_zoneid; 5758 econnp->conn_allzones = connp->conn_allzones; 5759 5760 /* Copy nexthop information from listener to eager */ 5761 if (connp->conn_nexthop_set) { 5762 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5763 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5764 } 5765 5766 /* 5767 * TSOL: tsol_input_proc() needs the eager's cred before the 5768 * eager is accepted 5769 */ 5770 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5771 crhold(credp); 5772 5773 /* 5774 * If the caller has the process-wide flag set, then default to MAC 5775 * exempt mode. This allows read-down to unlabeled hosts. 5776 */ 5777 if (getpflags(NET_MAC_AWARE, credp) != 0) 5778 econnp->conn_mac_exempt = B_TRUE; 5779 5780 if (is_system_labeled()) { 5781 cred_t *cr; 5782 5783 if (connp->conn_mlp_type != mlptSingle) { 5784 cr = econnp->conn_peercred = DB_CRED(mp); 5785 if (cr != NULL) 5786 crhold(cr); 5787 else 5788 cr = econnp->conn_cred; 5789 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5790 econnp, cred_t *, cr) 5791 } else { 5792 cr = econnp->conn_cred; 5793 DTRACE_PROBE2(syn_accept, conn_t *, 5794 econnp, cred_t *, cr) 5795 } 5796 5797 if (!tcp_update_label(eager, cr)) { 5798 DTRACE_PROBE3( 5799 tx__ip__log__error__connrequest__tcp, 5800 char *, "eager connp(1) label on SYN mp(2) failed", 5801 conn_t *, econnp, mblk_t *, mp); 5802 goto error3; 5803 } 5804 } 5805 5806 eager->tcp_hard_binding = B_TRUE; 5807 5808 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5809 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5810 5811 CL_INET_CONNECT(eager); 5812 5813 /* 5814 * No need to check for multicast destination since ip will only pass 5815 * up multicasts to those that have expressed interest 5816 * TODO: what about rejecting broadcasts? 5817 * Also check that source is not a multicast or broadcast address. 5818 */ 5819 eager->tcp_state = TCPS_SYN_RCVD; 5820 5821 5822 /* 5823 * There should be no ire in the mp as we are being called after 5824 * receiving the SYN. 5825 */ 5826 ASSERT(tcp_ire_mp(mp) == NULL); 5827 5828 /* 5829 * Adapt our mss, ttl, ... according to information provided in IRE. 5830 */ 5831 5832 if (tcp_adapt_ire(eager, NULL) == 0) { 5833 /* Undo the bind_hash_insert */ 5834 tcp_bind_hash_remove(eager); 5835 goto error3; 5836 } 5837 5838 /* Process all TCP options. */ 5839 tcp_process_options(eager, tcph); 5840 5841 /* Is the other end ECN capable? */ 5842 if (tcps->tcps_ecn_permitted >= 1 && 5843 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5844 eager->tcp_ecn_ok = B_TRUE; 5845 } 5846 5847 /* 5848 * listener->tcp_rq->q_hiwat should be the default window size or a 5849 * window size changed via SO_RCVBUF option. First round up the 5850 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5851 * scale option value if needed. Call tcp_rwnd_set() to finish the 5852 * setting. 5853 * 5854 * Note if there is a rpipe metric associated with the remote host, 5855 * we should not inherit receive window size from listener. 5856 */ 5857 eager->tcp_rwnd = MSS_ROUNDUP( 5858 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5859 eager->tcp_rwnd), eager->tcp_mss); 5860 if (eager->tcp_snd_ws_ok) 5861 tcp_set_ws_value(eager); 5862 /* 5863 * Note that this is the only place tcp_rwnd_set() is called for 5864 * accepting a connection. We need to call it here instead of 5865 * after the 3-way handshake because we need to tell the other 5866 * side our rwnd in the SYN-ACK segment. 5867 */ 5868 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5869 5870 /* 5871 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5872 * via soaccept()->soinheritoptions() which essentially applies 5873 * all the listener options to the new STREAM. The options that we 5874 * need to take care of are: 5875 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5876 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5877 * SO_SNDBUF, SO_RCVBUF. 5878 * 5879 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5880 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5881 * tcp_maxpsz_set() gets called later from 5882 * tcp_accept_finish(), the option takes effect. 5883 * 5884 */ 5885 /* Set the TCP options */ 5886 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5887 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5888 eager->tcp_oobinline = tcp->tcp_oobinline; 5889 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5890 eager->tcp_broadcast = tcp->tcp_broadcast; 5891 eager->tcp_useloopback = tcp->tcp_useloopback; 5892 eager->tcp_dontroute = tcp->tcp_dontroute; 5893 eager->tcp_linger = tcp->tcp_linger; 5894 eager->tcp_lingertime = tcp->tcp_lingertime; 5895 if (tcp->tcp_ka_enabled) 5896 eager->tcp_ka_enabled = 1; 5897 5898 /* Set the IP options */ 5899 econnp->conn_broadcast = connp->conn_broadcast; 5900 econnp->conn_loopback = connp->conn_loopback; 5901 econnp->conn_dontroute = connp->conn_dontroute; 5902 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5903 5904 /* Put a ref on the listener for the eager. */ 5905 CONN_INC_REF(connp); 5906 mutex_enter(&tcp->tcp_eager_lock); 5907 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5908 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5909 tcp->tcp_eager_next_q0 = eager; 5910 eager->tcp_eager_prev_q0 = tcp; 5911 5912 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5913 eager->tcp_listener = tcp; 5914 eager->tcp_saved_listener = tcp; 5915 5916 /* 5917 * Tag this detached tcp vector for later retrieval 5918 * by our listener client in tcp_accept(). 5919 */ 5920 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5921 tcp->tcp_conn_req_cnt_q0++; 5922 if (++tcp->tcp_conn_req_seqnum == -1) { 5923 /* 5924 * -1 is "special" and defined in TPI as something 5925 * that should never be used in T_CONN_IND 5926 */ 5927 ++tcp->tcp_conn_req_seqnum; 5928 } 5929 mutex_exit(&tcp->tcp_eager_lock); 5930 5931 if (tcp->tcp_syn_defense) { 5932 /* Don't drop the SYN that comes from a good IP source */ 5933 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5934 if (addr_cache != NULL && eager->tcp_remote == 5935 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5936 eager->tcp_dontdrop = B_TRUE; 5937 } 5938 } 5939 5940 /* 5941 * We need to insert the eager in its own perimeter but as soon 5942 * as we do that, we expose the eager to the classifier and 5943 * should not touch any field outside the eager's perimeter. 5944 * So do all the work necessary before inserting the eager 5945 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5946 * will succeed but undo everything if it fails. 5947 */ 5948 seg_seq = ABE32_TO_U32(tcph->th_seq); 5949 eager->tcp_irs = seg_seq; 5950 eager->tcp_rack = seg_seq; 5951 eager->tcp_rnxt = seg_seq + 1; 5952 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5953 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5954 eager->tcp_state = TCPS_SYN_RCVD; 5955 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5956 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5957 if (mp1 == NULL) { 5958 /* 5959 * Increment the ref count as we are going to 5960 * enqueueing an mp in squeue 5961 */ 5962 CONN_INC_REF(econnp); 5963 goto error; 5964 } 5965 DB_CPID(mp1) = tcp->tcp_cpid; 5966 eager->tcp_cpid = tcp->tcp_cpid; 5967 eager->tcp_open_time = lbolt64; 5968 5969 /* 5970 * We need to start the rto timer. In normal case, we start 5971 * the timer after sending the packet on the wire (or at 5972 * least believing that packet was sent by waiting for 5973 * CALL_IP_WPUT() to return). Since this is the first packet 5974 * being sent on the wire for the eager, our initial tcp_rto 5975 * is at least tcp_rexmit_interval_min which is a fairly 5976 * large value to allow the algorithm to adjust slowly to large 5977 * fluctuations of RTT during first few transmissions. 5978 * 5979 * Starting the timer first and then sending the packet in this 5980 * case shouldn't make much difference since tcp_rexmit_interval_min 5981 * is of the order of several 100ms and starting the timer 5982 * first and then sending the packet will result in difference 5983 * of few micro seconds. 5984 * 5985 * Without this optimization, we are forced to hold the fanout 5986 * lock across the ipcl_bind_insert() and sending the packet 5987 * so that we don't race against an incoming packet (maybe RST) 5988 * for this eager. 5989 * 5990 * It is necessary to acquire an extra reference on the eager 5991 * at this point and hold it until after tcp_send_data() to 5992 * ensure against an eager close race. 5993 */ 5994 5995 CONN_INC_REF(eager->tcp_connp); 5996 5997 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5998 5999 /* 6000 * Insert the eager in its own perimeter now. We are ready to deal 6001 * with any packets on eager. 6002 */ 6003 if (eager->tcp_ipversion == IPV4_VERSION) { 6004 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 6005 goto error; 6006 } 6007 } else { 6008 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 6009 goto error; 6010 } 6011 } 6012 6013 /* mark conn as fully-bound */ 6014 econnp->conn_fully_bound = B_TRUE; 6015 6016 /* Send the SYN-ACK */ 6017 tcp_send_data(eager, eager->tcp_wq, mp1); 6018 CONN_DEC_REF(eager->tcp_connp); 6019 freemsg(mp); 6020 6021 return; 6022 error: 6023 freemsg(mp1); 6024 eager->tcp_closemp_used = B_TRUE; 6025 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6026 squeue_fill(econnp->conn_sqp, &eager->tcp_closemp, tcp_eager_kill, 6027 econnp, SQTAG_TCP_CONN_REQ_2); 6028 6029 /* 6030 * If a connection already exists, send the mp to that connections so 6031 * that it can be appropriately dealt with. 6032 */ 6033 ipst = tcps->tcps_netstack->netstack_ip; 6034 6035 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 6036 if (!IPCL_IS_CONNECTED(econnp)) { 6037 /* 6038 * Something bad happened. ipcl_conn_insert() 6039 * failed because a connection already existed 6040 * in connected hash but we can't find it 6041 * anymore (someone blew it away). Just 6042 * free this message and hopefully remote 6043 * will retransmit at which time the SYN can be 6044 * treated as a new connection or dealth with 6045 * a TH_RST if a connection already exists. 6046 */ 6047 CONN_DEC_REF(econnp); 6048 freemsg(mp); 6049 } else { 6050 squeue_fill(econnp->conn_sqp, mp, tcp_input, 6051 econnp, SQTAG_TCP_CONN_REQ_1); 6052 } 6053 } else { 6054 /* Nobody wants this packet */ 6055 freemsg(mp); 6056 } 6057 return; 6058 error3: 6059 CONN_DEC_REF(econnp); 6060 error2: 6061 freemsg(mp); 6062 } 6063 6064 /* 6065 * In an ideal case of vertical partition in NUMA architecture, its 6066 * beneficial to have the listener and all the incoming connections 6067 * tied to the same squeue. The other constraint is that incoming 6068 * connections should be tied to the squeue attached to interrupted 6069 * CPU for obvious locality reason so this leaves the listener to 6070 * be tied to the same squeue. Our only problem is that when listener 6071 * is binding, the CPU that will get interrupted by the NIC whose 6072 * IP address the listener is binding to is not even known. So 6073 * the code below allows us to change that binding at the time the 6074 * CPU is interrupted by virtue of incoming connection's squeue. 6075 * 6076 * This is usefull only in case of a listener bound to a specific IP 6077 * address. For other kind of listeners, they get bound the 6078 * very first time and there is no attempt to rebind them. 6079 */ 6080 void 6081 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 6082 { 6083 conn_t *connp = (conn_t *)arg; 6084 squeue_t *sqp = (squeue_t *)arg2; 6085 squeue_t *new_sqp; 6086 uint32_t conn_flags; 6087 6088 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 6089 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 6090 } else { 6091 goto done; 6092 } 6093 6094 if (connp->conn_fanout == NULL) 6095 goto done; 6096 6097 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 6098 mutex_enter(&connp->conn_fanout->connf_lock); 6099 mutex_enter(&connp->conn_lock); 6100 /* 6101 * No one from read or write side can access us now 6102 * except for already queued packets on this squeue. 6103 * But since we haven't changed the squeue yet, they 6104 * can't execute. If they are processed after we have 6105 * changed the squeue, they are sent back to the 6106 * correct squeue down below. 6107 * But a listner close can race with processing of 6108 * incoming SYN. If incoming SYN processing changes 6109 * the squeue then the listener close which is waiting 6110 * to enter the squeue would operate on the wrong 6111 * squeue. Hence we don't change the squeue here unless 6112 * the refcount is exactly the minimum refcount. The 6113 * minimum refcount of 4 is counted as - 1 each for 6114 * TCP and IP, 1 for being in the classifier hash, and 6115 * 1 for the mblk being processed. 6116 */ 6117 6118 if (connp->conn_ref != 4 || 6119 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 6120 mutex_exit(&connp->conn_lock); 6121 mutex_exit(&connp->conn_fanout->connf_lock); 6122 goto done; 6123 } 6124 if (connp->conn_sqp != new_sqp) { 6125 while (connp->conn_sqp != new_sqp) 6126 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 6127 } 6128 6129 do { 6130 conn_flags = connp->conn_flags; 6131 conn_flags |= IPCL_FULLY_BOUND; 6132 (void) cas32(&connp->conn_flags, connp->conn_flags, 6133 conn_flags); 6134 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 6135 6136 mutex_exit(&connp->conn_fanout->connf_lock); 6137 mutex_exit(&connp->conn_lock); 6138 } 6139 6140 done: 6141 if (connp->conn_sqp != sqp) { 6142 CONN_INC_REF(connp); 6143 squeue_fill(connp->conn_sqp, mp, 6144 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 6145 } else { 6146 tcp_conn_request(connp, mp, sqp); 6147 } 6148 } 6149 6150 /* 6151 * Successful connect request processing begins when our client passes 6152 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 6153 * our T_OK_ACK reply message upstream. The control flow looks like this: 6154 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 6155 * upstream <- tcp_rput() <- IP 6156 * After various error checks are completed, tcp_connect() lays 6157 * the target address and port into the composite header template, 6158 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 6159 * request followed by an IRE request, and passes the three mblk message 6160 * down to IP looking like this: 6161 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 6162 * Processing continues in tcp_rput() when we receive the following message: 6163 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 6164 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 6165 * to fire off the connection request, and then passes the T_OK_ACK mblk 6166 * upstream that we filled in below. There are, of course, numerous 6167 * error conditions along the way which truncate the processing described 6168 * above. 6169 */ 6170 static void 6171 tcp_connect(tcp_t *tcp, mblk_t *mp) 6172 { 6173 sin_t *sin; 6174 sin6_t *sin6; 6175 queue_t *q = tcp->tcp_wq; 6176 struct T_conn_req *tcr; 6177 ipaddr_t *dstaddrp; 6178 in_port_t dstport; 6179 uint_t srcid; 6180 6181 tcr = (struct T_conn_req *)mp->b_rptr; 6182 6183 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6184 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 6185 tcp_err_ack(tcp, mp, TPROTO, 0); 6186 return; 6187 } 6188 6189 /* 6190 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 6191 * will always have that to send up. Otherwise, we need to do 6192 * special handling in case the allocation fails at that time. 6193 */ 6194 ASSERT(tcp->tcp_ordrel_mp == NULL); 6195 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 6196 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6197 return; 6198 } 6199 6200 /* 6201 * Determine packet type based on type of address passed in 6202 * the request should contain an IPv4 or IPv6 address. 6203 * Make sure that address family matches the type of 6204 * family of the the address passed down 6205 */ 6206 switch (tcr->DEST_length) { 6207 default: 6208 tcp_err_ack(tcp, mp, TBADADDR, 0); 6209 return; 6210 6211 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 6212 /* 6213 * XXX: The check for valid DEST_length was not there 6214 * in earlier releases and some buggy 6215 * TLI apps (e.g Sybase) got away with not feeding 6216 * in sin_zero part of address. 6217 * We allow that bug to keep those buggy apps humming. 6218 * Test suites require the check on DEST_length. 6219 * We construct a new mblk with valid DEST_length 6220 * free the original so the rest of the code does 6221 * not have to keep track of this special shorter 6222 * length address case. 6223 */ 6224 mblk_t *nmp; 6225 struct T_conn_req *ntcr; 6226 sin_t *nsin; 6227 6228 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6229 tcr->OPT_length, BPRI_HI); 6230 if (nmp == NULL) { 6231 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6232 return; 6233 } 6234 ntcr = (struct T_conn_req *)nmp->b_rptr; 6235 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6236 ntcr->PRIM_type = T_CONN_REQ; 6237 ntcr->DEST_length = sizeof (sin_t); 6238 ntcr->DEST_offset = sizeof (struct T_conn_req); 6239 6240 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6241 *nsin = sin_null; 6242 /* Get pointer to shorter address to copy from original mp */ 6243 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6244 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6245 if (sin == NULL || !OK_32PTR((char *)sin)) { 6246 freemsg(nmp); 6247 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6248 return; 6249 } 6250 nsin->sin_family = sin->sin_family; 6251 nsin->sin_port = sin->sin_port; 6252 nsin->sin_addr = sin->sin_addr; 6253 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6254 nmp->b_wptr = (uchar_t *)&nsin[1]; 6255 if (tcr->OPT_length != 0) { 6256 ntcr->OPT_length = tcr->OPT_length; 6257 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6258 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6259 (uchar_t *)ntcr + ntcr->OPT_offset, 6260 tcr->OPT_length); 6261 nmp->b_wptr += tcr->OPT_length; 6262 } 6263 freemsg(mp); /* original mp freed */ 6264 mp = nmp; /* re-initialize original variables */ 6265 tcr = ntcr; 6266 } 6267 /* FALLTHRU */ 6268 6269 case sizeof (sin_t): 6270 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6271 sizeof (sin_t)); 6272 if (sin == NULL || !OK_32PTR((char *)sin)) { 6273 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6274 return; 6275 } 6276 if (tcp->tcp_family != AF_INET || 6277 sin->sin_family != AF_INET) { 6278 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6279 return; 6280 } 6281 if (sin->sin_port == 0) { 6282 tcp_err_ack(tcp, mp, TBADADDR, 0); 6283 return; 6284 } 6285 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6286 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6287 return; 6288 } 6289 6290 break; 6291 6292 case sizeof (sin6_t): 6293 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6294 sizeof (sin6_t)); 6295 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6296 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6297 return; 6298 } 6299 if (tcp->tcp_family != AF_INET6 || 6300 sin6->sin6_family != AF_INET6) { 6301 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6302 return; 6303 } 6304 if (sin6->sin6_port == 0) { 6305 tcp_err_ack(tcp, mp, TBADADDR, 0); 6306 return; 6307 } 6308 break; 6309 } 6310 /* 6311 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6312 * should key on their sequence number and cut them loose. 6313 */ 6314 6315 /* 6316 * If options passed in, feed it for verification and handling 6317 */ 6318 if (tcr->OPT_length != 0) { 6319 mblk_t *ok_mp; 6320 mblk_t *discon_mp; 6321 mblk_t *conn_opts_mp; 6322 int t_error, sys_error, do_disconnect; 6323 6324 conn_opts_mp = NULL; 6325 6326 if (tcp_conprim_opt_process(tcp, mp, 6327 &do_disconnect, &t_error, &sys_error) < 0) { 6328 if (do_disconnect) { 6329 ASSERT(t_error == 0 && sys_error == 0); 6330 discon_mp = mi_tpi_discon_ind(NULL, 6331 ECONNREFUSED, 0); 6332 if (!discon_mp) { 6333 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6334 TSYSERR, ENOMEM); 6335 return; 6336 } 6337 ok_mp = mi_tpi_ok_ack_alloc(mp); 6338 if (!ok_mp) { 6339 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6340 TSYSERR, ENOMEM); 6341 return; 6342 } 6343 qreply(q, ok_mp); 6344 qreply(q, discon_mp); /* no flush! */ 6345 } else { 6346 ASSERT(t_error != 0); 6347 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6348 sys_error); 6349 } 6350 return; 6351 } 6352 /* 6353 * Success in setting options, the mp option buffer represented 6354 * by OPT_length/offset has been potentially modified and 6355 * contains results of option processing. We copy it in 6356 * another mp to save it for potentially influencing returning 6357 * it in T_CONN_CONN. 6358 */ 6359 if (tcr->OPT_length != 0) { /* there are resulting options */ 6360 conn_opts_mp = copyb(mp); 6361 if (!conn_opts_mp) { 6362 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6363 TSYSERR, ENOMEM); 6364 return; 6365 } 6366 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6367 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6368 /* 6369 * Note: 6370 * These resulting option negotiation can include any 6371 * end-to-end negotiation options but there no such 6372 * thing (yet?) in our TCP/IP. 6373 */ 6374 } 6375 } 6376 6377 /* 6378 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6379 * make sure that the template IP header in the tcp structure is an 6380 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6381 * need to this before we call tcp_bindi() so that the port lookup 6382 * code will look for ports in the correct port space (IPv4 and 6383 * IPv6 have separate port spaces). 6384 */ 6385 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6386 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6387 int err = 0; 6388 6389 err = tcp_header_init_ipv4(tcp); 6390 if (err != 0) { 6391 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6392 goto connect_failed; 6393 } 6394 if (tcp->tcp_lport != 0) 6395 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6396 } 6397 6398 if (tcp->tcp_issocket) { 6399 /* 6400 * TCP is _D_SODIRECT and sockfs is directly above so save 6401 * the shared sonode sodirect_t pointer (if any) to enable 6402 * TCP sodirect. 6403 */ 6404 tcp->tcp_sodirect = SOD_QTOSODP(tcp->tcp_rq); 6405 } 6406 6407 switch (tcp->tcp_state) { 6408 case TCPS_IDLE: 6409 /* 6410 * We support quick connect, refer to comments in 6411 * tcp_connect_*() 6412 */ 6413 /* FALLTHRU */ 6414 case TCPS_BOUND: 6415 case TCPS_LISTEN: 6416 if (tcp->tcp_family == AF_INET6) { 6417 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6418 tcp_connect_ipv6(tcp, mp, 6419 &sin6->sin6_addr, 6420 sin6->sin6_port, sin6->sin6_flowinfo, 6421 sin6->__sin6_src_id, sin6->sin6_scope_id); 6422 return; 6423 } 6424 /* 6425 * Destination adress is mapped IPv6 address. 6426 * Source bound address should be unspecified or 6427 * IPv6 mapped address as well. 6428 */ 6429 if (!IN6_IS_ADDR_UNSPECIFIED( 6430 &tcp->tcp_bound_source_v6) && 6431 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6432 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6433 EADDRNOTAVAIL); 6434 break; 6435 } 6436 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6437 dstport = sin6->sin6_port; 6438 srcid = sin6->__sin6_src_id; 6439 } else { 6440 dstaddrp = &sin->sin_addr.s_addr; 6441 dstport = sin->sin_port; 6442 srcid = 0; 6443 } 6444 6445 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6446 return; 6447 default: 6448 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6449 break; 6450 } 6451 /* 6452 * Note: Code below is the "failure" case 6453 */ 6454 /* return error ack and blow away saved option results if any */ 6455 connect_failed: 6456 if (mp != NULL) 6457 putnext(tcp->tcp_rq, mp); 6458 else { 6459 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6460 TSYSERR, ENOMEM); 6461 } 6462 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6463 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6464 } 6465 6466 /* 6467 * Handle connect to IPv4 destinations, including connections for AF_INET6 6468 * sockets connecting to IPv4 mapped IPv6 destinations. 6469 */ 6470 static void 6471 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6472 uint_t srcid) 6473 { 6474 tcph_t *tcph; 6475 mblk_t *mp1; 6476 ipaddr_t dstaddr = *dstaddrp; 6477 int32_t oldstate; 6478 uint16_t lport; 6479 tcp_stack_t *tcps = tcp->tcp_tcps; 6480 6481 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6482 6483 /* Check for attempt to connect to INADDR_ANY */ 6484 if (dstaddr == INADDR_ANY) { 6485 /* 6486 * SunOS 4.x and 4.3 BSD allow an application 6487 * to connect a TCP socket to INADDR_ANY. 6488 * When they do this, the kernel picks the 6489 * address of one interface and uses it 6490 * instead. The kernel usually ends up 6491 * picking the address of the loopback 6492 * interface. This is an undocumented feature. 6493 * However, we provide the same thing here 6494 * in order to have source and binary 6495 * compatibility with SunOS 4.x. 6496 * Update the T_CONN_REQ (sin/sin6) since it is used to 6497 * generate the T_CONN_CON. 6498 */ 6499 dstaddr = htonl(INADDR_LOOPBACK); 6500 *dstaddrp = dstaddr; 6501 } 6502 6503 /* Handle __sin6_src_id if socket not bound to an IP address */ 6504 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6505 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6506 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6507 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6508 tcp->tcp_ipha->ipha_src); 6509 } 6510 6511 /* 6512 * Don't let an endpoint connect to itself. Note that 6513 * the test here does not catch the case where the 6514 * source IP addr was left unspecified by the user. In 6515 * this case, the source addr is set in tcp_adapt_ire() 6516 * using the reply to the T_BIND message that we send 6517 * down to IP here and the check is repeated in tcp_rput_other. 6518 */ 6519 if (dstaddr == tcp->tcp_ipha->ipha_src && 6520 dstport == tcp->tcp_lport) { 6521 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6522 goto failed; 6523 } 6524 6525 tcp->tcp_ipha->ipha_dst = dstaddr; 6526 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6527 6528 /* 6529 * Massage a source route if any putting the first hop 6530 * in iph_dst. Compute a starting value for the checksum which 6531 * takes into account that the original iph_dst should be 6532 * included in the checksum but that ip will include the 6533 * first hop in the source route in the tcp checksum. 6534 */ 6535 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6536 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6537 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6538 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6539 if ((int)tcp->tcp_sum < 0) 6540 tcp->tcp_sum--; 6541 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6542 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6543 (tcp->tcp_sum >> 16)); 6544 tcph = tcp->tcp_tcph; 6545 *(uint16_t *)tcph->th_fport = dstport; 6546 tcp->tcp_fport = dstport; 6547 6548 oldstate = tcp->tcp_state; 6549 /* 6550 * At this point the remote destination address and remote port fields 6551 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6552 * have to see which state tcp was in so we can take apropriate action. 6553 */ 6554 if (oldstate == TCPS_IDLE) { 6555 /* 6556 * We support a quick connect capability here, allowing 6557 * clients to transition directly from IDLE to SYN_SENT 6558 * tcp_bindi will pick an unused port, insert the connection 6559 * in the bind hash and transition to BOUND state. 6560 */ 6561 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6562 tcp, B_TRUE); 6563 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6564 B_FALSE, B_FALSE); 6565 if (lport == 0) { 6566 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6567 goto failed; 6568 } 6569 } 6570 tcp->tcp_state = TCPS_SYN_SENT; 6571 6572 /* 6573 * TODO: allow data with connect requests 6574 * by unlinking M_DATA trailers here and 6575 * linking them in behind the T_OK_ACK mblk. 6576 * The tcp_rput() bind ack handler would then 6577 * feed them to tcp_wput_data() rather than call 6578 * tcp_timer(). 6579 */ 6580 mp = mi_tpi_ok_ack_alloc(mp); 6581 if (!mp) { 6582 tcp->tcp_state = oldstate; 6583 goto failed; 6584 } 6585 if (tcp->tcp_family == AF_INET) { 6586 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6587 sizeof (ipa_conn_t)); 6588 } else { 6589 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6590 sizeof (ipa6_conn_t)); 6591 } 6592 if (mp1) { 6593 /* 6594 * We need to make sure that the conn_recv is set to a non-null 6595 * value before we insert the conn_t into the classifier table. 6596 * This is to avoid a race with an incoming packet which does 6597 * an ipcl_classify(). 6598 */ 6599 tcp->tcp_connp->conn_recv = tcp_input; 6600 6601 /* Hang onto the T_OK_ACK for later. */ 6602 linkb(mp1, mp); 6603 mblk_setcred(mp1, tcp->tcp_cred); 6604 if (tcp->tcp_family == AF_INET) 6605 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6606 else { 6607 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6608 &tcp->tcp_sticky_ipp); 6609 } 6610 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6611 tcp->tcp_active_open = 1; 6612 /* 6613 * If the bind cannot complete immediately 6614 * IP will arrange to call tcp_rput_other 6615 * when the bind completes. 6616 */ 6617 if (mp1 != NULL) 6618 tcp_rput_other(tcp, mp1); 6619 return; 6620 } 6621 /* Error case */ 6622 tcp->tcp_state = oldstate; 6623 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6624 6625 failed: 6626 /* return error ack and blow away saved option results if any */ 6627 if (mp != NULL) 6628 putnext(tcp->tcp_rq, mp); 6629 else { 6630 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6631 TSYSERR, ENOMEM); 6632 } 6633 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6634 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6635 6636 } 6637 6638 /* 6639 * Handle connect to IPv6 destinations. 6640 */ 6641 static void 6642 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6643 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6644 { 6645 tcph_t *tcph; 6646 mblk_t *mp1; 6647 ip6_rthdr_t *rth; 6648 int32_t oldstate; 6649 uint16_t lport; 6650 tcp_stack_t *tcps = tcp->tcp_tcps; 6651 6652 ASSERT(tcp->tcp_family == AF_INET6); 6653 6654 /* 6655 * If we're here, it means that the destination address is a native 6656 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6657 * reason why it might not be IPv6 is if the socket was bound to an 6658 * IPv4-mapped IPv6 address. 6659 */ 6660 if (tcp->tcp_ipversion != IPV6_VERSION) { 6661 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6662 goto failed; 6663 } 6664 6665 /* 6666 * Interpret a zero destination to mean loopback. 6667 * Update the T_CONN_REQ (sin/sin6) since it is used to 6668 * generate the T_CONN_CON. 6669 */ 6670 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6671 *dstaddrp = ipv6_loopback; 6672 } 6673 6674 /* Handle __sin6_src_id if socket not bound to an IP address */ 6675 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6676 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6677 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6678 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6679 } 6680 6681 /* 6682 * Take care of the scope_id now and add ip6i_t 6683 * if ip6i_t is not already allocated through TCP 6684 * sticky options. At this point tcp_ip6h does not 6685 * have dst info, thus use dstaddrp. 6686 */ 6687 if (scope_id != 0 && 6688 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6689 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6690 ip6i_t *ip6i; 6691 6692 ipp->ipp_ifindex = scope_id; 6693 ip6i = (ip6i_t *)tcp->tcp_iphc; 6694 6695 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6696 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6697 /* Already allocated */ 6698 ip6i->ip6i_flags |= IP6I_IFINDEX; 6699 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6700 ipp->ipp_fields |= IPPF_SCOPE_ID; 6701 } else { 6702 int reterr; 6703 6704 ipp->ipp_fields |= IPPF_SCOPE_ID; 6705 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6706 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6707 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6708 if (reterr != 0) 6709 goto failed; 6710 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6711 } 6712 } 6713 6714 /* 6715 * Don't let an endpoint connect to itself. Note that 6716 * the test here does not catch the case where the 6717 * source IP addr was left unspecified by the user. In 6718 * this case, the source addr is set in tcp_adapt_ire() 6719 * using the reply to the T_BIND message that we send 6720 * down to IP here and the check is repeated in tcp_rput_other. 6721 */ 6722 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6723 (dstport == tcp->tcp_lport)) { 6724 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6725 goto failed; 6726 } 6727 6728 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6729 tcp->tcp_remote_v6 = *dstaddrp; 6730 tcp->tcp_ip6h->ip6_vcf = 6731 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6732 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6733 6734 6735 /* 6736 * Massage a routing header (if present) putting the first hop 6737 * in ip6_dst. Compute a starting value for the checksum which 6738 * takes into account that the original ip6_dst should be 6739 * included in the checksum but that ip will include the 6740 * first hop in the source route in the tcp checksum. 6741 */ 6742 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6743 if (rth != NULL) { 6744 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6745 tcps->tcps_netstack); 6746 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6747 (tcp->tcp_sum >> 16)); 6748 } else { 6749 tcp->tcp_sum = 0; 6750 } 6751 6752 tcph = tcp->tcp_tcph; 6753 *(uint16_t *)tcph->th_fport = dstport; 6754 tcp->tcp_fport = dstport; 6755 6756 oldstate = tcp->tcp_state; 6757 /* 6758 * At this point the remote destination address and remote port fields 6759 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6760 * have to see which state tcp was in so we can take apropriate action. 6761 */ 6762 if (oldstate == TCPS_IDLE) { 6763 /* 6764 * We support a quick connect capability here, allowing 6765 * clients to transition directly from IDLE to SYN_SENT 6766 * tcp_bindi will pick an unused port, insert the connection 6767 * in the bind hash and transition to BOUND state. 6768 */ 6769 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6770 tcp, B_TRUE); 6771 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6772 B_FALSE, B_FALSE); 6773 if (lport == 0) { 6774 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6775 goto failed; 6776 } 6777 } 6778 tcp->tcp_state = TCPS_SYN_SENT; 6779 /* 6780 * TODO: allow data with connect requests 6781 * by unlinking M_DATA trailers here and 6782 * linking them in behind the T_OK_ACK mblk. 6783 * The tcp_rput() bind ack handler would then 6784 * feed them to tcp_wput_data() rather than call 6785 * tcp_timer(). 6786 */ 6787 mp = mi_tpi_ok_ack_alloc(mp); 6788 if (!mp) { 6789 tcp->tcp_state = oldstate; 6790 goto failed; 6791 } 6792 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6793 if (mp1) { 6794 /* 6795 * We need to make sure that the conn_recv is set to a non-null 6796 * value before we insert the conn_t into the classifier table. 6797 * This is to avoid a race with an incoming packet which does 6798 * an ipcl_classify(). 6799 */ 6800 tcp->tcp_connp->conn_recv = tcp_input; 6801 6802 /* Hang onto the T_OK_ACK for later. */ 6803 linkb(mp1, mp); 6804 mblk_setcred(mp1, tcp->tcp_cred); 6805 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6806 &tcp->tcp_sticky_ipp); 6807 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6808 tcp->tcp_active_open = 1; 6809 /* ip_bind_v6() may return ACK or ERROR */ 6810 if (mp1 != NULL) 6811 tcp_rput_other(tcp, mp1); 6812 return; 6813 } 6814 /* Error case */ 6815 tcp->tcp_state = oldstate; 6816 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6817 6818 failed: 6819 /* return error ack and blow away saved option results if any */ 6820 if (mp != NULL) 6821 putnext(tcp->tcp_rq, mp); 6822 else { 6823 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6824 TSYSERR, ENOMEM); 6825 } 6826 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6827 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6828 } 6829 6830 /* 6831 * We need a stream q for detached closing tcp connections 6832 * to use. Our client hereby indicates that this q is the 6833 * one to use. 6834 */ 6835 static void 6836 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6837 { 6838 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6839 queue_t *q = tcp->tcp_wq; 6840 tcp_stack_t *tcps = tcp->tcp_tcps; 6841 6842 #ifdef NS_DEBUG 6843 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6844 tcps->tcps_netstack->netstack_stackid); 6845 #endif 6846 mp->b_datap->db_type = M_IOCACK; 6847 iocp->ioc_count = 0; 6848 mutex_enter(&tcps->tcps_g_q_lock); 6849 if (tcps->tcps_g_q != NULL) { 6850 mutex_exit(&tcps->tcps_g_q_lock); 6851 iocp->ioc_error = EALREADY; 6852 } else { 6853 mblk_t *mp1; 6854 6855 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6856 if (mp1 == NULL) { 6857 mutex_exit(&tcps->tcps_g_q_lock); 6858 iocp->ioc_error = ENOMEM; 6859 } else { 6860 tcps->tcps_g_q = tcp->tcp_rq; 6861 mutex_exit(&tcps->tcps_g_q_lock); 6862 iocp->ioc_error = 0; 6863 iocp->ioc_rval = 0; 6864 /* 6865 * We are passing tcp_sticky_ipp as NULL 6866 * as it is not useful for tcp_default queue 6867 * 6868 * Set conn_recv just in case. 6869 */ 6870 tcp->tcp_connp->conn_recv = tcp_conn_request; 6871 6872 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6873 if (mp1 != NULL) 6874 tcp_rput_other(tcp, mp1); 6875 } 6876 } 6877 qreply(q, mp); 6878 } 6879 6880 /* 6881 * Our client hereby directs us to reject the connection request 6882 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6883 * of sending the appropriate RST, not an ICMP error. 6884 */ 6885 static void 6886 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6887 { 6888 tcp_t *ltcp = NULL; 6889 t_scalar_t seqnum; 6890 conn_t *connp; 6891 tcp_stack_t *tcps = tcp->tcp_tcps; 6892 6893 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6894 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6895 tcp_err_ack(tcp, mp, TPROTO, 0); 6896 return; 6897 } 6898 6899 /* 6900 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6901 * when the stream is in BOUND state. Do not send a reset, 6902 * since the destination IP address is not valid, and it can 6903 * be the initialized value of all zeros (broadcast address). 6904 * 6905 * If TCP has sent down a bind request to IP and has not 6906 * received the reply, reject the request. Otherwise, TCP 6907 * will be confused. 6908 */ 6909 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6910 if (tcp->tcp_debug) { 6911 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6912 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6913 } 6914 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6915 return; 6916 } 6917 6918 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6919 6920 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6921 6922 /* 6923 * According to TPI, for non-listeners, ignore seqnum 6924 * and disconnect. 6925 * Following interpretation of -1 seqnum is historical 6926 * and implied TPI ? (TPI only states that for T_CONN_IND, 6927 * a valid seqnum should not be -1). 6928 * 6929 * -1 means disconnect everything 6930 * regardless even on a listener. 6931 */ 6932 6933 int old_state = tcp->tcp_state; 6934 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6935 6936 /* 6937 * The connection can't be on the tcp_time_wait_head list 6938 * since it is not detached. 6939 */ 6940 ASSERT(tcp->tcp_time_wait_next == NULL); 6941 ASSERT(tcp->tcp_time_wait_prev == NULL); 6942 ASSERT(tcp->tcp_time_wait_expire == 0); 6943 ltcp = NULL; 6944 /* 6945 * If it used to be a listener, check to make sure no one else 6946 * has taken the port before switching back to LISTEN state. 6947 */ 6948 if (tcp->tcp_ipversion == IPV4_VERSION) { 6949 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6950 tcp->tcp_ipha->ipha_src, 6951 tcp->tcp_connp->conn_zoneid, ipst); 6952 if (connp != NULL) 6953 ltcp = connp->conn_tcp; 6954 } else { 6955 /* Allow tcp_bound_if listeners? */ 6956 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6957 &tcp->tcp_ip6h->ip6_src, 0, 6958 tcp->tcp_connp->conn_zoneid, ipst); 6959 if (connp != NULL) 6960 ltcp = connp->conn_tcp; 6961 } 6962 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6963 tcp->tcp_state = TCPS_LISTEN; 6964 } else if (old_state > TCPS_BOUND) { 6965 tcp->tcp_conn_req_max = 0; 6966 tcp->tcp_state = TCPS_BOUND; 6967 } 6968 if (ltcp != NULL) 6969 CONN_DEC_REF(ltcp->tcp_connp); 6970 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6971 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6972 } else if (old_state == TCPS_ESTABLISHED || 6973 old_state == TCPS_CLOSE_WAIT) { 6974 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6975 } 6976 6977 if (tcp->tcp_fused) 6978 tcp_unfuse(tcp); 6979 6980 mutex_enter(&tcp->tcp_eager_lock); 6981 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6982 (tcp->tcp_conn_req_cnt_q != 0)) { 6983 tcp_eager_cleanup(tcp, 0); 6984 } 6985 mutex_exit(&tcp->tcp_eager_lock); 6986 6987 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6988 tcp->tcp_rnxt, TH_RST | TH_ACK); 6989 6990 tcp_reinit(tcp); 6991 6992 if (old_state >= TCPS_ESTABLISHED) { 6993 /* Send M_FLUSH according to TPI */ 6994 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6995 } 6996 mp = mi_tpi_ok_ack_alloc(mp); 6997 if (mp) 6998 putnext(tcp->tcp_rq, mp); 6999 return; 7000 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 7001 tcp_err_ack(tcp, mp, TBADSEQ, 0); 7002 return; 7003 } 7004 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 7005 /* Send M_FLUSH according to TPI */ 7006 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 7007 } 7008 mp = mi_tpi_ok_ack_alloc(mp); 7009 if (mp) 7010 putnext(tcp->tcp_rq, mp); 7011 } 7012 7013 /* 7014 * Diagnostic routine used to return a string associated with the tcp state. 7015 * Note that if the caller does not supply a buffer, it will use an internal 7016 * static string. This means that if multiple threads call this function at 7017 * the same time, output can be corrupted... Note also that this function 7018 * does not check the size of the supplied buffer. The caller has to make 7019 * sure that it is big enough. 7020 */ 7021 static char * 7022 tcp_display(tcp_t *tcp, char *sup_buf, char format) 7023 { 7024 char buf1[30]; 7025 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 7026 char *buf; 7027 char *cp; 7028 in6_addr_t local, remote; 7029 char local_addrbuf[INET6_ADDRSTRLEN]; 7030 char remote_addrbuf[INET6_ADDRSTRLEN]; 7031 7032 if (sup_buf != NULL) 7033 buf = sup_buf; 7034 else 7035 buf = priv_buf; 7036 7037 if (tcp == NULL) 7038 return ("NULL_TCP"); 7039 switch (tcp->tcp_state) { 7040 case TCPS_CLOSED: 7041 cp = "TCP_CLOSED"; 7042 break; 7043 case TCPS_IDLE: 7044 cp = "TCP_IDLE"; 7045 break; 7046 case TCPS_BOUND: 7047 cp = "TCP_BOUND"; 7048 break; 7049 case TCPS_LISTEN: 7050 cp = "TCP_LISTEN"; 7051 break; 7052 case TCPS_SYN_SENT: 7053 cp = "TCP_SYN_SENT"; 7054 break; 7055 case TCPS_SYN_RCVD: 7056 cp = "TCP_SYN_RCVD"; 7057 break; 7058 case TCPS_ESTABLISHED: 7059 cp = "TCP_ESTABLISHED"; 7060 break; 7061 case TCPS_CLOSE_WAIT: 7062 cp = "TCP_CLOSE_WAIT"; 7063 break; 7064 case TCPS_FIN_WAIT_1: 7065 cp = "TCP_FIN_WAIT_1"; 7066 break; 7067 case TCPS_CLOSING: 7068 cp = "TCP_CLOSING"; 7069 break; 7070 case TCPS_LAST_ACK: 7071 cp = "TCP_LAST_ACK"; 7072 break; 7073 case TCPS_FIN_WAIT_2: 7074 cp = "TCP_FIN_WAIT_2"; 7075 break; 7076 case TCPS_TIME_WAIT: 7077 cp = "TCP_TIME_WAIT"; 7078 break; 7079 default: 7080 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 7081 cp = buf1; 7082 break; 7083 } 7084 switch (format) { 7085 case DISP_ADDR_AND_PORT: 7086 if (tcp->tcp_ipversion == IPV4_VERSION) { 7087 /* 7088 * Note that we use the remote address in the tcp_b 7089 * structure. This means that it will print out 7090 * the real destination address, not the next hop's 7091 * address if source routing is used. 7092 */ 7093 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 7094 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 7095 7096 } else { 7097 local = tcp->tcp_ip_src_v6; 7098 remote = tcp->tcp_remote_v6; 7099 } 7100 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 7101 sizeof (local_addrbuf)); 7102 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 7103 sizeof (remote_addrbuf)); 7104 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 7105 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 7106 ntohs(tcp->tcp_fport), cp); 7107 break; 7108 case DISP_PORT_ONLY: 7109 default: 7110 (void) mi_sprintf(buf, "[%u, %u] %s", 7111 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 7112 break; 7113 } 7114 7115 return (buf); 7116 } 7117 7118 /* 7119 * Called via squeue to get on to eager's perimeter. It sends a 7120 * TH_RST if eager is in the fanout table. The listener wants the 7121 * eager to disappear either by means of tcp_eager_blowoff() or 7122 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 7123 * called (via squeue) if the eager cannot be inserted in the 7124 * fanout table in tcp_conn_request(). 7125 */ 7126 /* ARGSUSED */ 7127 void 7128 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 7129 { 7130 conn_t *econnp = (conn_t *)arg; 7131 tcp_t *eager = econnp->conn_tcp; 7132 tcp_t *listener = eager->tcp_listener; 7133 tcp_stack_t *tcps = eager->tcp_tcps; 7134 7135 /* 7136 * We could be called because listener is closing. Since 7137 * the eager is using listener's queue's, its not safe. 7138 * Better use the default queue just to send the TH_RST 7139 * out. 7140 */ 7141 ASSERT(tcps->tcps_g_q != NULL); 7142 eager->tcp_rq = tcps->tcps_g_q; 7143 eager->tcp_wq = WR(tcps->tcps_g_q); 7144 7145 /* 7146 * An eager's conn_fanout will be NULL if it's a duplicate 7147 * for an existing 4-tuples in the conn fanout table. 7148 * We don't want to send an RST out in such case. 7149 */ 7150 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 7151 tcp_xmit_ctl("tcp_eager_kill, can't wait", 7152 eager, eager->tcp_snxt, 0, TH_RST); 7153 } 7154 7155 /* We are here because listener wants this eager gone */ 7156 if (listener != NULL) { 7157 mutex_enter(&listener->tcp_eager_lock); 7158 tcp_eager_unlink(eager); 7159 if (eager->tcp_tconnind_started) { 7160 /* 7161 * The eager has sent a conn_ind up to the 7162 * listener but listener decides to close 7163 * instead. We need to drop the extra ref 7164 * placed on eager in tcp_rput_data() before 7165 * sending the conn_ind to listener. 7166 */ 7167 CONN_DEC_REF(econnp); 7168 } 7169 mutex_exit(&listener->tcp_eager_lock); 7170 CONN_DEC_REF(listener->tcp_connp); 7171 } 7172 7173 if (eager->tcp_state > TCPS_BOUND) 7174 tcp_close_detached(eager); 7175 } 7176 7177 /* 7178 * Reset any eager connection hanging off this listener marked 7179 * with 'seqnum' and then reclaim it's resources. 7180 */ 7181 static boolean_t 7182 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 7183 { 7184 tcp_t *eager; 7185 mblk_t *mp; 7186 tcp_stack_t *tcps = listener->tcp_tcps; 7187 7188 TCP_STAT(tcps, tcp_eager_blowoff_calls); 7189 eager = listener; 7190 mutex_enter(&listener->tcp_eager_lock); 7191 do { 7192 eager = eager->tcp_eager_next_q; 7193 if (eager == NULL) { 7194 mutex_exit(&listener->tcp_eager_lock); 7195 return (B_FALSE); 7196 } 7197 } while (eager->tcp_conn_req_seqnum != seqnum); 7198 7199 if (eager->tcp_closemp_used) { 7200 mutex_exit(&listener->tcp_eager_lock); 7201 return (B_TRUE); 7202 } 7203 eager->tcp_closemp_used = B_TRUE; 7204 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7205 CONN_INC_REF(eager->tcp_connp); 7206 mutex_exit(&listener->tcp_eager_lock); 7207 mp = &eager->tcp_closemp; 7208 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 7209 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 7210 return (B_TRUE); 7211 } 7212 7213 /* 7214 * Reset any eager connection hanging off this listener 7215 * and then reclaim it's resources. 7216 */ 7217 static void 7218 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 7219 { 7220 tcp_t *eager; 7221 mblk_t *mp; 7222 tcp_stack_t *tcps = listener->tcp_tcps; 7223 7224 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7225 7226 if (!q0_only) { 7227 /* First cleanup q */ 7228 TCP_STAT(tcps, tcp_eager_blowoff_q); 7229 eager = listener->tcp_eager_next_q; 7230 while (eager != NULL) { 7231 if (!eager->tcp_closemp_used) { 7232 eager->tcp_closemp_used = B_TRUE; 7233 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7234 CONN_INC_REF(eager->tcp_connp); 7235 mp = &eager->tcp_closemp; 7236 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7237 tcp_eager_kill, eager->tcp_connp, 7238 SQTAG_TCP_EAGER_CLEANUP); 7239 } 7240 eager = eager->tcp_eager_next_q; 7241 } 7242 } 7243 /* Then cleanup q0 */ 7244 TCP_STAT(tcps, tcp_eager_blowoff_q0); 7245 eager = listener->tcp_eager_next_q0; 7246 while (eager != listener) { 7247 if (!eager->tcp_closemp_used) { 7248 eager->tcp_closemp_used = B_TRUE; 7249 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 7250 CONN_INC_REF(eager->tcp_connp); 7251 mp = &eager->tcp_closemp; 7252 squeue_fill(eager->tcp_connp->conn_sqp, mp, 7253 tcp_eager_kill, eager->tcp_connp, 7254 SQTAG_TCP_EAGER_CLEANUP_Q0); 7255 } 7256 eager = eager->tcp_eager_next_q0; 7257 } 7258 } 7259 7260 /* 7261 * If we are an eager connection hanging off a listener that hasn't 7262 * formally accepted the connection yet, get off his list and blow off 7263 * any data that we have accumulated. 7264 */ 7265 static void 7266 tcp_eager_unlink(tcp_t *tcp) 7267 { 7268 tcp_t *listener = tcp->tcp_listener; 7269 7270 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 7271 ASSERT(listener != NULL); 7272 if (tcp->tcp_eager_next_q0 != NULL) { 7273 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 7274 7275 /* Remove the eager tcp from q0 */ 7276 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 7277 tcp->tcp_eager_prev_q0; 7278 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 7279 tcp->tcp_eager_next_q0; 7280 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 7281 listener->tcp_conn_req_cnt_q0--; 7282 7283 tcp->tcp_eager_next_q0 = NULL; 7284 tcp->tcp_eager_prev_q0 = NULL; 7285 7286 /* 7287 * Take the eager out, if it is in the list of droppable 7288 * eagers. 7289 */ 7290 MAKE_UNDROPPABLE(tcp); 7291 7292 if (tcp->tcp_syn_rcvd_timeout != 0) { 7293 /* we have timed out before */ 7294 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7295 listener->tcp_syn_rcvd_timeout--; 7296 } 7297 } else { 7298 tcp_t **tcpp = &listener->tcp_eager_next_q; 7299 tcp_t *prev = NULL; 7300 7301 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7302 if (tcpp[0] == tcp) { 7303 if (listener->tcp_eager_last_q == tcp) { 7304 /* 7305 * If we are unlinking the last 7306 * element on the list, adjust 7307 * tail pointer. Set tail pointer 7308 * to nil when list is empty. 7309 */ 7310 ASSERT(tcp->tcp_eager_next_q == NULL); 7311 if (listener->tcp_eager_last_q == 7312 listener->tcp_eager_next_q) { 7313 listener->tcp_eager_last_q = 7314 NULL; 7315 } else { 7316 /* 7317 * We won't get here if there 7318 * is only one eager in the 7319 * list. 7320 */ 7321 ASSERT(prev != NULL); 7322 listener->tcp_eager_last_q = 7323 prev; 7324 } 7325 } 7326 tcpp[0] = tcp->tcp_eager_next_q; 7327 tcp->tcp_eager_next_q = NULL; 7328 tcp->tcp_eager_last_q = NULL; 7329 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7330 listener->tcp_conn_req_cnt_q--; 7331 break; 7332 } 7333 prev = tcpp[0]; 7334 } 7335 } 7336 tcp->tcp_listener = NULL; 7337 } 7338 7339 /* Shorthand to generate and send TPI error acks to our client */ 7340 static void 7341 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7342 { 7343 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7344 putnext(tcp->tcp_rq, mp); 7345 } 7346 7347 /* Shorthand to generate and send TPI error acks to our client */ 7348 static void 7349 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7350 int t_error, int sys_error) 7351 { 7352 struct T_error_ack *teackp; 7353 7354 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7355 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7356 teackp = (struct T_error_ack *)mp->b_rptr; 7357 teackp->ERROR_prim = primitive; 7358 teackp->TLI_error = t_error; 7359 teackp->UNIX_error = sys_error; 7360 putnext(tcp->tcp_rq, mp); 7361 } 7362 } 7363 7364 /* 7365 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7366 * but instead the code relies on: 7367 * - the fact that the address of the array and its size never changes 7368 * - the atomic assignment of the elements of the array 7369 */ 7370 /* ARGSUSED */ 7371 static int 7372 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7373 { 7374 int i; 7375 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7376 7377 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7378 if (tcps->tcps_g_epriv_ports[i] != 0) 7379 (void) mi_mpprintf(mp, "%d ", 7380 tcps->tcps_g_epriv_ports[i]); 7381 } 7382 return (0); 7383 } 7384 7385 /* 7386 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7387 * threads from changing it at the same time. 7388 */ 7389 /* ARGSUSED */ 7390 static int 7391 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7392 cred_t *cr) 7393 { 7394 long new_value; 7395 int i; 7396 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7397 7398 /* 7399 * Fail the request if the new value does not lie within the 7400 * port number limits. 7401 */ 7402 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7403 new_value <= 0 || new_value >= 65536) { 7404 return (EINVAL); 7405 } 7406 7407 mutex_enter(&tcps->tcps_epriv_port_lock); 7408 /* Check if the value is already in the list */ 7409 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7410 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7411 mutex_exit(&tcps->tcps_epriv_port_lock); 7412 return (EEXIST); 7413 } 7414 } 7415 /* Find an empty slot */ 7416 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7417 if (tcps->tcps_g_epriv_ports[i] == 0) 7418 break; 7419 } 7420 if (i == tcps->tcps_g_num_epriv_ports) { 7421 mutex_exit(&tcps->tcps_epriv_port_lock); 7422 return (EOVERFLOW); 7423 } 7424 /* Set the new value */ 7425 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7426 mutex_exit(&tcps->tcps_epriv_port_lock); 7427 return (0); 7428 } 7429 7430 /* 7431 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7432 * threads from changing it at the same time. 7433 */ 7434 /* ARGSUSED */ 7435 static int 7436 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7437 cred_t *cr) 7438 { 7439 long new_value; 7440 int i; 7441 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7442 7443 /* 7444 * Fail the request if the new value does not lie within the 7445 * port number limits. 7446 */ 7447 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7448 new_value >= 65536) { 7449 return (EINVAL); 7450 } 7451 7452 mutex_enter(&tcps->tcps_epriv_port_lock); 7453 /* Check that the value is already in the list */ 7454 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7455 if (tcps->tcps_g_epriv_ports[i] == new_value) 7456 break; 7457 } 7458 if (i == tcps->tcps_g_num_epriv_ports) { 7459 mutex_exit(&tcps->tcps_epriv_port_lock); 7460 return (ESRCH); 7461 } 7462 /* Clear the value */ 7463 tcps->tcps_g_epriv_ports[i] = 0; 7464 mutex_exit(&tcps->tcps_epriv_port_lock); 7465 return (0); 7466 } 7467 7468 /* Return the TPI/TLI equivalent of our current tcp_state */ 7469 static int 7470 tcp_tpistate(tcp_t *tcp) 7471 { 7472 switch (tcp->tcp_state) { 7473 case TCPS_IDLE: 7474 return (TS_UNBND); 7475 case TCPS_LISTEN: 7476 /* 7477 * Return whether there are outstanding T_CONN_IND waiting 7478 * for the matching T_CONN_RES. Therefore don't count q0. 7479 */ 7480 if (tcp->tcp_conn_req_cnt_q > 0) 7481 return (TS_WRES_CIND); 7482 else 7483 return (TS_IDLE); 7484 case TCPS_BOUND: 7485 return (TS_IDLE); 7486 case TCPS_SYN_SENT: 7487 return (TS_WCON_CREQ); 7488 case TCPS_SYN_RCVD: 7489 /* 7490 * Note: assumption: this has to the active open SYN_RCVD. 7491 * The passive instance is detached in SYN_RCVD stage of 7492 * incoming connection processing so we cannot get request 7493 * for T_info_ack on it. 7494 */ 7495 return (TS_WACK_CRES); 7496 case TCPS_ESTABLISHED: 7497 return (TS_DATA_XFER); 7498 case TCPS_CLOSE_WAIT: 7499 return (TS_WREQ_ORDREL); 7500 case TCPS_FIN_WAIT_1: 7501 return (TS_WIND_ORDREL); 7502 case TCPS_FIN_WAIT_2: 7503 return (TS_WIND_ORDREL); 7504 7505 case TCPS_CLOSING: 7506 case TCPS_LAST_ACK: 7507 case TCPS_TIME_WAIT: 7508 case TCPS_CLOSED: 7509 /* 7510 * Following TS_WACK_DREQ7 is a rendition of "not 7511 * yet TS_IDLE" TPI state. There is no best match to any 7512 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7513 * choose a value chosen that will map to TLI/XTI level 7514 * state of TSTATECHNG (state is process of changing) which 7515 * captures what this dummy state represents. 7516 */ 7517 return (TS_WACK_DREQ7); 7518 default: 7519 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7520 tcp->tcp_state, tcp_display(tcp, NULL, 7521 DISP_PORT_ONLY)); 7522 return (TS_UNBND); 7523 } 7524 } 7525 7526 static void 7527 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7528 { 7529 tcp_stack_t *tcps = tcp->tcp_tcps; 7530 7531 if (tcp->tcp_family == AF_INET6) 7532 *tia = tcp_g_t_info_ack_v6; 7533 else 7534 *tia = tcp_g_t_info_ack; 7535 tia->CURRENT_state = tcp_tpistate(tcp); 7536 tia->OPT_size = tcp_max_optsize; 7537 if (tcp->tcp_mss == 0) { 7538 /* Not yet set - tcp_open does not set mss */ 7539 if (tcp->tcp_ipversion == IPV4_VERSION) 7540 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7541 else 7542 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7543 } else { 7544 tia->TIDU_size = tcp->tcp_mss; 7545 } 7546 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7547 } 7548 7549 /* 7550 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7551 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7552 * tcp_g_t_info_ack. The current state of the stream is copied from 7553 * tcp_state. 7554 */ 7555 static void 7556 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7557 { 7558 t_uscalar_t cap_bits1; 7559 struct T_capability_ack *tcap; 7560 7561 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7562 freemsg(mp); 7563 return; 7564 } 7565 7566 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7567 7568 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7569 mp->b_datap->db_type, T_CAPABILITY_ACK); 7570 if (mp == NULL) 7571 return; 7572 7573 tcap = (struct T_capability_ack *)mp->b_rptr; 7574 tcap->CAP_bits1 = 0; 7575 7576 if (cap_bits1 & TC1_INFO) { 7577 tcp_copy_info(&tcap->INFO_ack, tcp); 7578 tcap->CAP_bits1 |= TC1_INFO; 7579 } 7580 7581 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7582 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7583 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7584 } 7585 7586 putnext(tcp->tcp_rq, mp); 7587 } 7588 7589 /* 7590 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7591 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7592 * The current state of the stream is copied from tcp_state. 7593 */ 7594 static void 7595 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7596 { 7597 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7598 T_INFO_ACK); 7599 if (!mp) { 7600 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7601 return; 7602 } 7603 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7604 putnext(tcp->tcp_rq, mp); 7605 } 7606 7607 /* Respond to the TPI addr request */ 7608 static void 7609 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7610 { 7611 sin_t *sin; 7612 mblk_t *ackmp; 7613 struct T_addr_ack *taa; 7614 7615 /* Make it large enough for worst case */ 7616 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7617 2 * sizeof (sin6_t), 1); 7618 if (ackmp == NULL) { 7619 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7620 return; 7621 } 7622 7623 if (tcp->tcp_ipversion == IPV6_VERSION) { 7624 tcp_addr_req_ipv6(tcp, ackmp); 7625 return; 7626 } 7627 taa = (struct T_addr_ack *)ackmp->b_rptr; 7628 7629 bzero(taa, sizeof (struct T_addr_ack)); 7630 ackmp->b_wptr = (uchar_t *)&taa[1]; 7631 7632 taa->PRIM_type = T_ADDR_ACK; 7633 ackmp->b_datap->db_type = M_PCPROTO; 7634 7635 /* 7636 * Note: Following code assumes 32 bit alignment of basic 7637 * data structures like sin_t and struct T_addr_ack. 7638 */ 7639 if (tcp->tcp_state >= TCPS_BOUND) { 7640 /* 7641 * Fill in local address 7642 */ 7643 taa->LOCADDR_length = sizeof (sin_t); 7644 taa->LOCADDR_offset = sizeof (*taa); 7645 7646 sin = (sin_t *)&taa[1]; 7647 7648 /* Fill zeroes and then intialize non-zero fields */ 7649 *sin = sin_null; 7650 7651 sin->sin_family = AF_INET; 7652 7653 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7654 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7655 7656 ackmp->b_wptr = (uchar_t *)&sin[1]; 7657 7658 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7659 /* 7660 * Fill in Remote address 7661 */ 7662 taa->REMADDR_length = sizeof (sin_t); 7663 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7664 taa->LOCADDR_length); 7665 7666 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7667 *sin = sin_null; 7668 sin->sin_family = AF_INET; 7669 sin->sin_addr.s_addr = tcp->tcp_remote; 7670 sin->sin_port = tcp->tcp_fport; 7671 7672 ackmp->b_wptr = (uchar_t *)&sin[1]; 7673 } 7674 } 7675 putnext(tcp->tcp_rq, ackmp); 7676 } 7677 7678 /* Assumes that tcp_addr_req gets enough space and alignment */ 7679 static void 7680 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7681 { 7682 sin6_t *sin6; 7683 struct T_addr_ack *taa; 7684 7685 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7686 ASSERT(OK_32PTR(ackmp->b_rptr)); 7687 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7688 2 * sizeof (sin6_t)); 7689 7690 taa = (struct T_addr_ack *)ackmp->b_rptr; 7691 7692 bzero(taa, sizeof (struct T_addr_ack)); 7693 ackmp->b_wptr = (uchar_t *)&taa[1]; 7694 7695 taa->PRIM_type = T_ADDR_ACK; 7696 ackmp->b_datap->db_type = M_PCPROTO; 7697 7698 /* 7699 * Note: Following code assumes 32 bit alignment of basic 7700 * data structures like sin6_t and struct T_addr_ack. 7701 */ 7702 if (tcp->tcp_state >= TCPS_BOUND) { 7703 /* 7704 * Fill in local address 7705 */ 7706 taa->LOCADDR_length = sizeof (sin6_t); 7707 taa->LOCADDR_offset = sizeof (*taa); 7708 7709 sin6 = (sin6_t *)&taa[1]; 7710 *sin6 = sin6_null; 7711 7712 sin6->sin6_family = AF_INET6; 7713 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7714 sin6->sin6_port = tcp->tcp_lport; 7715 7716 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7717 7718 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7719 /* 7720 * Fill in Remote address 7721 */ 7722 taa->REMADDR_length = sizeof (sin6_t); 7723 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7724 taa->LOCADDR_length); 7725 7726 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7727 *sin6 = sin6_null; 7728 sin6->sin6_family = AF_INET6; 7729 sin6->sin6_flowinfo = 7730 tcp->tcp_ip6h->ip6_vcf & 7731 ~IPV6_VERS_AND_FLOW_MASK; 7732 sin6->sin6_addr = tcp->tcp_remote_v6; 7733 sin6->sin6_port = tcp->tcp_fport; 7734 7735 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7736 } 7737 } 7738 putnext(tcp->tcp_rq, ackmp); 7739 } 7740 7741 /* 7742 * Handle reinitialization of a tcp structure. 7743 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7744 */ 7745 static void 7746 tcp_reinit(tcp_t *tcp) 7747 { 7748 mblk_t *mp; 7749 int err; 7750 tcp_stack_t *tcps = tcp->tcp_tcps; 7751 7752 TCP_STAT(tcps, tcp_reinit_calls); 7753 7754 /* tcp_reinit should never be called for detached tcp_t's */ 7755 ASSERT(tcp->tcp_listener == NULL); 7756 ASSERT((tcp->tcp_family == AF_INET && 7757 tcp->tcp_ipversion == IPV4_VERSION) || 7758 (tcp->tcp_family == AF_INET6 && 7759 (tcp->tcp_ipversion == IPV4_VERSION || 7760 tcp->tcp_ipversion == IPV6_VERSION))); 7761 7762 /* Cancel outstanding timers */ 7763 tcp_timers_stop(tcp); 7764 7765 /* 7766 * Reset everything in the state vector, after updating global 7767 * MIB data from instance counters. 7768 */ 7769 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7770 tcp->tcp_ibsegs = 0; 7771 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7772 tcp->tcp_obsegs = 0; 7773 7774 tcp_close_mpp(&tcp->tcp_xmit_head); 7775 if (tcp->tcp_snd_zcopy_aware) 7776 tcp_zcopy_notify(tcp); 7777 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7778 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7779 mutex_enter(&tcp->tcp_non_sq_lock); 7780 if (tcp->tcp_flow_stopped && 7781 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7782 tcp_clrqfull(tcp); 7783 } 7784 mutex_exit(&tcp->tcp_non_sq_lock); 7785 tcp_close_mpp(&tcp->tcp_reass_head); 7786 tcp->tcp_reass_tail = NULL; 7787 if (tcp->tcp_rcv_list != NULL) { 7788 /* Free b_next chain */ 7789 tcp_close_mpp(&tcp->tcp_rcv_list); 7790 tcp->tcp_rcv_last_head = NULL; 7791 tcp->tcp_rcv_last_tail = NULL; 7792 tcp->tcp_rcv_cnt = 0; 7793 } 7794 tcp->tcp_rcv_last_tail = NULL; 7795 7796 if ((mp = tcp->tcp_urp_mp) != NULL) { 7797 freemsg(mp); 7798 tcp->tcp_urp_mp = NULL; 7799 } 7800 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7801 freemsg(mp); 7802 tcp->tcp_urp_mark_mp = NULL; 7803 } 7804 if (tcp->tcp_fused_sigurg_mp != NULL) { 7805 freeb(tcp->tcp_fused_sigurg_mp); 7806 tcp->tcp_fused_sigurg_mp = NULL; 7807 } 7808 if (tcp->tcp_ordrel_mp != NULL) { 7809 freeb(tcp->tcp_ordrel_mp); 7810 tcp->tcp_ordrel_mp = NULL; 7811 } 7812 7813 /* 7814 * Following is a union with two members which are 7815 * identical types and size so the following cleanup 7816 * is enough. 7817 */ 7818 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7819 7820 CL_INET_DISCONNECT(tcp); 7821 7822 /* 7823 * The connection can't be on the tcp_time_wait_head list 7824 * since it is not detached. 7825 */ 7826 ASSERT(tcp->tcp_time_wait_next == NULL); 7827 ASSERT(tcp->tcp_time_wait_prev == NULL); 7828 ASSERT(tcp->tcp_time_wait_expire == 0); 7829 7830 if (tcp->tcp_kssl_pending) { 7831 tcp->tcp_kssl_pending = B_FALSE; 7832 7833 /* Don't reset if the initialized by bind. */ 7834 if (tcp->tcp_kssl_ent != NULL) { 7835 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7836 KSSL_NO_PROXY); 7837 } 7838 } 7839 if (tcp->tcp_kssl_ctx != NULL) { 7840 kssl_release_ctx(tcp->tcp_kssl_ctx); 7841 tcp->tcp_kssl_ctx = NULL; 7842 } 7843 7844 /* 7845 * Reset/preserve other values 7846 */ 7847 tcp_reinit_values(tcp); 7848 ipcl_hash_remove(tcp->tcp_connp); 7849 conn_delete_ire(tcp->tcp_connp, NULL); 7850 tcp_ipsec_cleanup(tcp); 7851 7852 if (tcp->tcp_conn_req_max != 0) { 7853 /* 7854 * This is the case when a TLI program uses the same 7855 * transport end point to accept a connection. This 7856 * makes the TCP both a listener and acceptor. When 7857 * this connection is closed, we need to set the state 7858 * back to TCPS_LISTEN. Make sure that the eager list 7859 * is reinitialized. 7860 * 7861 * Note that this stream is still bound to the four 7862 * tuples of the previous connection in IP. If a new 7863 * SYN with different foreign address comes in, IP will 7864 * not find it and will send it to the global queue. In 7865 * the global queue, TCP will do a tcp_lookup_listener() 7866 * to find this stream. This works because this stream 7867 * is only removed from connected hash. 7868 * 7869 */ 7870 tcp->tcp_state = TCPS_LISTEN; 7871 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7872 tcp->tcp_eager_next_drop_q0 = tcp; 7873 tcp->tcp_eager_prev_drop_q0 = tcp; 7874 tcp->tcp_connp->conn_recv = tcp_conn_request; 7875 if (tcp->tcp_family == AF_INET6) { 7876 ASSERT(tcp->tcp_connp->conn_af_isv6); 7877 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7878 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7879 } else { 7880 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7881 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7882 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7883 } 7884 } else { 7885 tcp->tcp_state = TCPS_BOUND; 7886 } 7887 7888 /* 7889 * Initialize to default values 7890 * Can't fail since enough header template space already allocated 7891 * at open(). 7892 */ 7893 err = tcp_init_values(tcp); 7894 ASSERT(err == 0); 7895 /* Restore state in tcp_tcph */ 7896 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7897 if (tcp->tcp_ipversion == IPV4_VERSION) 7898 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7899 else 7900 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7901 /* 7902 * Copy of the src addr. in tcp_t is needed in tcp_t 7903 * since the lookup funcs can only lookup on tcp_t 7904 */ 7905 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7906 7907 ASSERT(tcp->tcp_ptpbhn != NULL); 7908 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7909 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7910 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7911 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7912 } 7913 7914 /* 7915 * Force values to zero that need be zero. 7916 * Do not touch values asociated with the BOUND or LISTEN state 7917 * since the connection will end up in that state after the reinit. 7918 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7919 * structure! 7920 */ 7921 static void 7922 tcp_reinit_values(tcp) 7923 tcp_t *tcp; 7924 { 7925 tcp_stack_t *tcps = tcp->tcp_tcps; 7926 7927 #ifndef lint 7928 #define DONTCARE(x) 7929 #define PRESERVE(x) 7930 #else 7931 #define DONTCARE(x) ((x) = (x)) 7932 #define PRESERVE(x) ((x) = (x)) 7933 #endif /* lint */ 7934 7935 PRESERVE(tcp->tcp_bind_hash); 7936 PRESERVE(tcp->tcp_ptpbhn); 7937 PRESERVE(tcp->tcp_acceptor_hash); 7938 PRESERVE(tcp->tcp_ptpahn); 7939 7940 /* Should be ASSERT NULL on these with new code! */ 7941 ASSERT(tcp->tcp_time_wait_next == NULL); 7942 ASSERT(tcp->tcp_time_wait_prev == NULL); 7943 ASSERT(tcp->tcp_time_wait_expire == 0); 7944 PRESERVE(tcp->tcp_state); 7945 PRESERVE(tcp->tcp_rq); 7946 PRESERVE(tcp->tcp_wq); 7947 7948 ASSERT(tcp->tcp_xmit_head == NULL); 7949 ASSERT(tcp->tcp_xmit_last == NULL); 7950 ASSERT(tcp->tcp_unsent == 0); 7951 ASSERT(tcp->tcp_xmit_tail == NULL); 7952 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7953 7954 tcp->tcp_snxt = 0; /* Displayed in mib */ 7955 tcp->tcp_suna = 0; /* Displayed in mib */ 7956 tcp->tcp_swnd = 0; 7957 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7958 7959 ASSERT(tcp->tcp_ibsegs == 0); 7960 ASSERT(tcp->tcp_obsegs == 0); 7961 7962 if (tcp->tcp_iphc != NULL) { 7963 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7964 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7965 } 7966 7967 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7968 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7969 DONTCARE(tcp->tcp_ipha); 7970 DONTCARE(tcp->tcp_ip6h); 7971 DONTCARE(tcp->tcp_ip_hdr_len); 7972 DONTCARE(tcp->tcp_tcph); 7973 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7974 tcp->tcp_valid_bits = 0; 7975 7976 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7977 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7978 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7979 tcp->tcp_last_rcv_lbolt = 0; 7980 7981 tcp->tcp_init_cwnd = 0; 7982 7983 tcp->tcp_urp_last_valid = 0; 7984 tcp->tcp_hard_binding = 0; 7985 tcp->tcp_hard_bound = 0; 7986 PRESERVE(tcp->tcp_cred); 7987 PRESERVE(tcp->tcp_cpid); 7988 PRESERVE(tcp->tcp_open_time); 7989 PRESERVE(tcp->tcp_exclbind); 7990 7991 tcp->tcp_fin_acked = 0; 7992 tcp->tcp_fin_rcvd = 0; 7993 tcp->tcp_fin_sent = 0; 7994 tcp->tcp_ordrel_done = 0; 7995 7996 tcp->tcp_debug = 0; 7997 tcp->tcp_dontroute = 0; 7998 tcp->tcp_broadcast = 0; 7999 8000 tcp->tcp_useloopback = 0; 8001 tcp->tcp_reuseaddr = 0; 8002 tcp->tcp_oobinline = 0; 8003 tcp->tcp_dgram_errind = 0; 8004 8005 tcp->tcp_detached = 0; 8006 tcp->tcp_bind_pending = 0; 8007 tcp->tcp_unbind_pending = 0; 8008 8009 tcp->tcp_snd_ws_ok = B_FALSE; 8010 tcp->tcp_snd_ts_ok = B_FALSE; 8011 tcp->tcp_linger = 0; 8012 tcp->tcp_ka_enabled = 0; 8013 tcp->tcp_zero_win_probe = 0; 8014 8015 tcp->tcp_loopback = 0; 8016 tcp->tcp_refuse = 0; 8017 tcp->tcp_localnet = 0; 8018 tcp->tcp_syn_defense = 0; 8019 tcp->tcp_set_timer = 0; 8020 8021 tcp->tcp_active_open = 0; 8022 tcp->tcp_rexmit = B_FALSE; 8023 tcp->tcp_xmit_zc_clean = B_FALSE; 8024 8025 tcp->tcp_snd_sack_ok = B_FALSE; 8026 PRESERVE(tcp->tcp_recvdstaddr); 8027 tcp->tcp_hwcksum = B_FALSE; 8028 8029 tcp->tcp_ire_ill_check_done = B_FALSE; 8030 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 8031 8032 tcp->tcp_mdt = B_FALSE; 8033 tcp->tcp_mdt_hdr_head = 0; 8034 tcp->tcp_mdt_hdr_tail = 0; 8035 8036 tcp->tcp_conn_def_q0 = 0; 8037 tcp->tcp_ip_forward_progress = B_FALSE; 8038 tcp->tcp_anon_priv_bind = 0; 8039 tcp->tcp_ecn_ok = B_FALSE; 8040 8041 tcp->tcp_cwr = B_FALSE; 8042 tcp->tcp_ecn_echo_on = B_FALSE; 8043 8044 if (tcp->tcp_sack_info != NULL) { 8045 if (tcp->tcp_notsack_list != NULL) { 8046 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 8047 } 8048 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 8049 tcp->tcp_sack_info = NULL; 8050 } 8051 8052 tcp->tcp_rcv_ws = 0; 8053 tcp->tcp_snd_ws = 0; 8054 tcp->tcp_ts_recent = 0; 8055 tcp->tcp_rnxt = 0; /* Displayed in mib */ 8056 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 8057 tcp->tcp_if_mtu = 0; 8058 8059 ASSERT(tcp->tcp_reass_head == NULL); 8060 ASSERT(tcp->tcp_reass_tail == NULL); 8061 8062 tcp->tcp_cwnd_cnt = 0; 8063 8064 ASSERT(tcp->tcp_rcv_list == NULL); 8065 ASSERT(tcp->tcp_rcv_last_head == NULL); 8066 ASSERT(tcp->tcp_rcv_last_tail == NULL); 8067 ASSERT(tcp->tcp_rcv_cnt == 0); 8068 8069 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 8070 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 8071 tcp->tcp_csuna = 0; 8072 8073 tcp->tcp_rto = 0; /* Displayed in MIB */ 8074 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 8075 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 8076 tcp->tcp_rtt_update = 0; 8077 8078 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8079 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 8080 8081 tcp->tcp_rack = 0; /* Displayed in mib */ 8082 tcp->tcp_rack_cnt = 0; 8083 tcp->tcp_rack_cur_max = 0; 8084 tcp->tcp_rack_abs_max = 0; 8085 8086 tcp->tcp_max_swnd = 0; 8087 8088 ASSERT(tcp->tcp_listener == NULL); 8089 8090 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 8091 8092 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 8093 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 8094 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 8095 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 8096 8097 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 8098 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 8099 PRESERVE(tcp->tcp_conn_req_max); 8100 PRESERVE(tcp->tcp_conn_req_seqnum); 8101 8102 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 8103 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 8104 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 8105 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 8106 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 8107 8108 tcp->tcp_lingertime = 0; 8109 8110 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 8111 ASSERT(tcp->tcp_urp_mp == NULL); 8112 ASSERT(tcp->tcp_urp_mark_mp == NULL); 8113 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 8114 8115 ASSERT(tcp->tcp_eager_next_q == NULL); 8116 ASSERT(tcp->tcp_eager_last_q == NULL); 8117 ASSERT((tcp->tcp_eager_next_q0 == NULL && 8118 tcp->tcp_eager_prev_q0 == NULL) || 8119 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 8120 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 8121 8122 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 8123 tcp->tcp_eager_prev_drop_q0 == NULL) || 8124 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 8125 8126 tcp->tcp_client_errno = 0; 8127 8128 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 8129 8130 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 8131 8132 PRESERVE(tcp->tcp_bound_source_v6); 8133 tcp->tcp_last_sent_len = 0; 8134 tcp->tcp_dupack_cnt = 0; 8135 8136 tcp->tcp_fport = 0; /* Displayed in MIB */ 8137 PRESERVE(tcp->tcp_lport); 8138 8139 PRESERVE(tcp->tcp_acceptor_lockp); 8140 8141 ASSERT(tcp->tcp_ordrel_mp == NULL); 8142 PRESERVE(tcp->tcp_acceptor_id); 8143 DONTCARE(tcp->tcp_ipsec_overhead); 8144 8145 PRESERVE(tcp->tcp_family); 8146 if (tcp->tcp_family == AF_INET6) { 8147 tcp->tcp_ipversion = IPV6_VERSION; 8148 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 8149 } else { 8150 tcp->tcp_ipversion = IPV4_VERSION; 8151 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 8152 } 8153 8154 tcp->tcp_bound_if = 0; 8155 tcp->tcp_ipv6_recvancillary = 0; 8156 tcp->tcp_recvifindex = 0; 8157 tcp->tcp_recvhops = 0; 8158 tcp->tcp_closed = 0; 8159 tcp->tcp_cleandeathtag = 0; 8160 if (tcp->tcp_hopopts != NULL) { 8161 mi_free(tcp->tcp_hopopts); 8162 tcp->tcp_hopopts = NULL; 8163 tcp->tcp_hopoptslen = 0; 8164 } 8165 ASSERT(tcp->tcp_hopoptslen == 0); 8166 if (tcp->tcp_dstopts != NULL) { 8167 mi_free(tcp->tcp_dstopts); 8168 tcp->tcp_dstopts = NULL; 8169 tcp->tcp_dstoptslen = 0; 8170 } 8171 ASSERT(tcp->tcp_dstoptslen == 0); 8172 if (tcp->tcp_rtdstopts != NULL) { 8173 mi_free(tcp->tcp_rtdstopts); 8174 tcp->tcp_rtdstopts = NULL; 8175 tcp->tcp_rtdstoptslen = 0; 8176 } 8177 ASSERT(tcp->tcp_rtdstoptslen == 0); 8178 if (tcp->tcp_rthdr != NULL) { 8179 mi_free(tcp->tcp_rthdr); 8180 tcp->tcp_rthdr = NULL; 8181 tcp->tcp_rthdrlen = 0; 8182 } 8183 ASSERT(tcp->tcp_rthdrlen == 0); 8184 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 8185 8186 /* Reset fusion-related fields */ 8187 tcp->tcp_fused = B_FALSE; 8188 tcp->tcp_unfusable = B_FALSE; 8189 tcp->tcp_fused_sigurg = B_FALSE; 8190 tcp->tcp_direct_sockfs = B_FALSE; 8191 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8192 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8193 tcp->tcp_loopback_peer = NULL; 8194 tcp->tcp_fuse_rcv_hiwater = 0; 8195 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8196 tcp->tcp_fuse_rcv_unread_cnt = 0; 8197 8198 tcp->tcp_lso = B_FALSE; 8199 8200 tcp->tcp_in_ack_unsent = 0; 8201 tcp->tcp_cork = B_FALSE; 8202 tcp->tcp_tconnind_started = B_FALSE; 8203 8204 PRESERVE(tcp->tcp_squeue_bytes); 8205 8206 ASSERT(tcp->tcp_kssl_ctx == NULL); 8207 ASSERT(!tcp->tcp_kssl_pending); 8208 PRESERVE(tcp->tcp_kssl_ent); 8209 8210 /* Sodirect */ 8211 tcp->tcp_sodirect = NULL; 8212 8213 tcp->tcp_closemp_used = B_FALSE; 8214 8215 PRESERVE(tcp->tcp_rsrv_mp); 8216 PRESERVE(tcp->tcp_rsrv_mp_lock); 8217 8218 #ifdef DEBUG 8219 DONTCARE(tcp->tcmp_stk[0]); 8220 #endif 8221 8222 8223 #undef DONTCARE 8224 #undef PRESERVE 8225 } 8226 8227 /* 8228 * Allocate necessary resources and initialize state vector. 8229 * Guaranteed not to fail so that when an error is returned, 8230 * the caller doesn't need to do any additional cleanup. 8231 */ 8232 int 8233 tcp_init(tcp_t *tcp, queue_t *q) 8234 { 8235 int err; 8236 8237 tcp->tcp_rq = q; 8238 tcp->tcp_wq = WR(q); 8239 tcp->tcp_state = TCPS_IDLE; 8240 if ((err = tcp_init_values(tcp)) != 0) 8241 tcp_timers_stop(tcp); 8242 return (err); 8243 } 8244 8245 static int 8246 tcp_init_values(tcp_t *tcp) 8247 { 8248 int err; 8249 tcp_stack_t *tcps = tcp->tcp_tcps; 8250 8251 ASSERT((tcp->tcp_family == AF_INET && 8252 tcp->tcp_ipversion == IPV4_VERSION) || 8253 (tcp->tcp_family == AF_INET6 && 8254 (tcp->tcp_ipversion == IPV4_VERSION || 8255 tcp->tcp_ipversion == IPV6_VERSION))); 8256 8257 /* 8258 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 8259 * will be close to tcp_rexmit_interval_initial. By doing this, we 8260 * allow the algorithm to adjust slowly to large fluctuations of RTT 8261 * during first few transmissions of a connection as seen in slow 8262 * links. 8263 */ 8264 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 8265 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 8266 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 8267 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 8268 tcps->tcps_conn_grace_period; 8269 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 8270 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 8271 tcp->tcp_timer_backoff = 0; 8272 tcp->tcp_ms_we_have_waited = 0; 8273 tcp->tcp_last_recv_time = lbolt; 8274 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 8275 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 8276 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 8277 8278 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 8279 8280 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 8281 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 8282 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 8283 /* 8284 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 8285 * passive open. 8286 */ 8287 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 8288 8289 tcp->tcp_naglim = tcps->tcps_naglim_def; 8290 8291 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 8292 8293 tcp->tcp_mdt_hdr_head = 0; 8294 tcp->tcp_mdt_hdr_tail = 0; 8295 8296 /* Reset fusion-related fields */ 8297 tcp->tcp_fused = B_FALSE; 8298 tcp->tcp_unfusable = B_FALSE; 8299 tcp->tcp_fused_sigurg = B_FALSE; 8300 tcp->tcp_direct_sockfs = B_FALSE; 8301 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 8302 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 8303 tcp->tcp_loopback_peer = NULL; 8304 tcp->tcp_fuse_rcv_hiwater = 0; 8305 tcp->tcp_fuse_rcv_unread_hiwater = 0; 8306 tcp->tcp_fuse_rcv_unread_cnt = 0; 8307 8308 /* Sodirect */ 8309 tcp->tcp_sodirect = NULL; 8310 8311 /* Initialize the header template */ 8312 if (tcp->tcp_ipversion == IPV4_VERSION) { 8313 err = tcp_header_init_ipv4(tcp); 8314 } else { 8315 err = tcp_header_init_ipv6(tcp); 8316 } 8317 if (err) 8318 return (err); 8319 8320 /* 8321 * Init the window scale to the max so tcp_rwnd_set() won't pare 8322 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8323 */ 8324 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8325 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8326 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8327 8328 tcp->tcp_cork = B_FALSE; 8329 /* 8330 * Init the tcp_debug option. This value determines whether TCP 8331 * calls strlog() to print out debug messages. Doing this 8332 * initialization here means that this value is not inherited thru 8333 * tcp_reinit(). 8334 */ 8335 tcp->tcp_debug = tcps->tcps_dbg; 8336 8337 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8338 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8339 8340 return (0); 8341 } 8342 8343 /* 8344 * Initialize the IPv4 header. Loses any record of any IP options. 8345 */ 8346 static int 8347 tcp_header_init_ipv4(tcp_t *tcp) 8348 { 8349 tcph_t *tcph; 8350 uint32_t sum; 8351 conn_t *connp; 8352 tcp_stack_t *tcps = tcp->tcp_tcps; 8353 8354 /* 8355 * This is a simple initialization. If there's 8356 * already a template, it should never be too small, 8357 * so reuse it. Otherwise, allocate space for the new one. 8358 */ 8359 if (tcp->tcp_iphc == NULL) { 8360 ASSERT(tcp->tcp_iphc_len == 0); 8361 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8362 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8363 if (tcp->tcp_iphc == NULL) { 8364 tcp->tcp_iphc_len = 0; 8365 return (ENOMEM); 8366 } 8367 } 8368 8369 /* options are gone; may need a new label */ 8370 connp = tcp->tcp_connp; 8371 connp->conn_mlp_type = mlptSingle; 8372 connp->conn_ulp_labeled = !is_system_labeled(); 8373 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8374 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8375 tcp->tcp_ip6h = NULL; 8376 tcp->tcp_ipversion = IPV4_VERSION; 8377 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8378 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8379 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8380 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8381 tcp->tcp_ipha->ipha_version_and_hdr_length 8382 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8383 tcp->tcp_ipha->ipha_ident = 0; 8384 8385 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8386 tcp->tcp_tos = 0; 8387 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8388 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8389 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8390 8391 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8392 tcp->tcp_tcph = tcph; 8393 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8394 /* 8395 * IP wants our header length in the checksum field to 8396 * allow it to perform a single pseudo-header+checksum 8397 * calculation on behalf of TCP. 8398 * Include the adjustment for a source route once IP_OPTIONS is set. 8399 */ 8400 sum = sizeof (tcph_t) + tcp->tcp_sum; 8401 sum = (sum >> 16) + (sum & 0xFFFF); 8402 U16_TO_ABE16(sum, tcph->th_sum); 8403 return (0); 8404 } 8405 8406 /* 8407 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8408 */ 8409 static int 8410 tcp_header_init_ipv6(tcp_t *tcp) 8411 { 8412 tcph_t *tcph; 8413 uint32_t sum; 8414 conn_t *connp; 8415 tcp_stack_t *tcps = tcp->tcp_tcps; 8416 8417 /* 8418 * This is a simple initialization. If there's 8419 * already a template, it should never be too small, 8420 * so reuse it. Otherwise, allocate space for the new one. 8421 * Ensure that there is enough space to "downgrade" the tcp_t 8422 * to an IPv4 tcp_t. This requires having space for a full load 8423 * of IPv4 options, as well as a full load of TCP options 8424 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8425 * than a v6 header and a TCP header with a full load of TCP options 8426 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8427 * We want to avoid reallocation in the "downgraded" case when 8428 * processing outbound IPv4 options. 8429 */ 8430 if (tcp->tcp_iphc == NULL) { 8431 ASSERT(tcp->tcp_iphc_len == 0); 8432 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8433 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8434 if (tcp->tcp_iphc == NULL) { 8435 tcp->tcp_iphc_len = 0; 8436 return (ENOMEM); 8437 } 8438 } 8439 8440 /* options are gone; may need a new label */ 8441 connp = tcp->tcp_connp; 8442 connp->conn_mlp_type = mlptSingle; 8443 connp->conn_ulp_labeled = !is_system_labeled(); 8444 8445 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8446 tcp->tcp_ipversion = IPV6_VERSION; 8447 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8448 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8449 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8450 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8451 tcp->tcp_ipha = NULL; 8452 8453 /* Initialize the header template */ 8454 8455 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8456 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8457 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8458 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8459 8460 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8461 tcp->tcp_tcph = tcph; 8462 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8463 /* 8464 * IP wants our header length in the checksum field to 8465 * allow it to perform a single psuedo-header+checksum 8466 * calculation on behalf of TCP. 8467 * Include the adjustment for a source route when IPV6_RTHDR is set. 8468 */ 8469 sum = sizeof (tcph_t) + tcp->tcp_sum; 8470 sum = (sum >> 16) + (sum & 0xFFFF); 8471 U16_TO_ABE16(sum, tcph->th_sum); 8472 return (0); 8473 } 8474 8475 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8476 #define ICMP_MIN_TCP_HDR 8 8477 8478 /* 8479 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8480 * passed up by IP. The message is always received on the correct tcp_t. 8481 * Assumes that IP has pulled up everything up to and including the ICMP header. 8482 */ 8483 void 8484 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8485 { 8486 icmph_t *icmph; 8487 ipha_t *ipha; 8488 int iph_hdr_length; 8489 tcph_t *tcph; 8490 boolean_t ipsec_mctl = B_FALSE; 8491 boolean_t secure; 8492 mblk_t *first_mp = mp; 8493 uint32_t new_mss; 8494 uint32_t ratio; 8495 size_t mp_size = MBLKL(mp); 8496 uint32_t seg_seq; 8497 tcp_stack_t *tcps = tcp->tcp_tcps; 8498 8499 /* Assume IP provides aligned packets - otherwise toss */ 8500 if (!OK_32PTR(mp->b_rptr)) { 8501 freemsg(mp); 8502 return; 8503 } 8504 8505 /* 8506 * Since ICMP errors are normal data marked with M_CTL when sent 8507 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8508 * packets starting with an ipsec_info_t, see ipsec_info.h. 8509 */ 8510 if ((mp_size == sizeof (ipsec_info_t)) && 8511 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8512 ASSERT(mp->b_cont != NULL); 8513 mp = mp->b_cont; 8514 /* IP should have done this */ 8515 ASSERT(OK_32PTR(mp->b_rptr)); 8516 mp_size = MBLKL(mp); 8517 ipsec_mctl = B_TRUE; 8518 } 8519 8520 /* 8521 * Verify that we have a complete outer IP header. If not, drop it. 8522 */ 8523 if (mp_size < sizeof (ipha_t)) { 8524 noticmpv4: 8525 freemsg(first_mp); 8526 return; 8527 } 8528 8529 ipha = (ipha_t *)mp->b_rptr; 8530 /* 8531 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8532 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8533 */ 8534 switch (IPH_HDR_VERSION(ipha)) { 8535 case IPV6_VERSION: 8536 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8537 return; 8538 case IPV4_VERSION: 8539 break; 8540 default: 8541 goto noticmpv4; 8542 } 8543 8544 /* Skip past the outer IP and ICMP headers */ 8545 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8546 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8547 /* 8548 * If we don't have the correct outer IP header length or if the ULP 8549 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8550 * send it upstream. 8551 */ 8552 if (iph_hdr_length < sizeof (ipha_t) || 8553 ipha->ipha_protocol != IPPROTO_ICMP || 8554 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8555 goto noticmpv4; 8556 } 8557 ipha = (ipha_t *)&icmph[1]; 8558 8559 /* Skip past the inner IP and find the ULP header */ 8560 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8561 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8562 /* 8563 * If we don't have the correct inner IP header length or if the ULP 8564 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8565 * bytes of TCP header, drop it. 8566 */ 8567 if (iph_hdr_length < sizeof (ipha_t) || 8568 ipha->ipha_protocol != IPPROTO_TCP || 8569 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8570 goto noticmpv4; 8571 } 8572 8573 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8574 if (ipsec_mctl) { 8575 secure = ipsec_in_is_secure(first_mp); 8576 } else { 8577 secure = B_FALSE; 8578 } 8579 if (secure) { 8580 /* 8581 * If we are willing to accept this in clear 8582 * we don't have to verify policy. 8583 */ 8584 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8585 if (!tcp_check_policy(tcp, first_mp, 8586 ipha, NULL, secure, ipsec_mctl)) { 8587 /* 8588 * tcp_check_policy called 8589 * ip_drop_packet() on failure. 8590 */ 8591 return; 8592 } 8593 } 8594 } 8595 } else if (ipsec_mctl) { 8596 /* 8597 * This is a hard_bound connection. IP has already 8598 * verified policy. We don't have to do it again. 8599 */ 8600 freeb(first_mp); 8601 first_mp = mp; 8602 ipsec_mctl = B_FALSE; 8603 } 8604 8605 seg_seq = ABE32_TO_U32(tcph->th_seq); 8606 /* 8607 * TCP SHOULD check that the TCP sequence number contained in 8608 * payload of the ICMP error message is within the range 8609 * SND.UNA <= SEG.SEQ < SND.NXT. 8610 */ 8611 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8612 /* 8613 * If the ICMP message is bogus, should we kill the 8614 * connection, or should we just drop the bogus ICMP 8615 * message? It would probably make more sense to just 8616 * drop the message so that if this one managed to get 8617 * in, the real connection should not suffer. 8618 */ 8619 goto noticmpv4; 8620 } 8621 8622 switch (icmph->icmph_type) { 8623 case ICMP_DEST_UNREACHABLE: 8624 switch (icmph->icmph_code) { 8625 case ICMP_FRAGMENTATION_NEEDED: 8626 /* 8627 * Reduce the MSS based on the new MTU. This will 8628 * eliminate any fragmentation locally. 8629 * N.B. There may well be some funny side-effects on 8630 * the local send policy and the remote receive policy. 8631 * Pending further research, we provide 8632 * tcp_ignore_path_mtu just in case this proves 8633 * disastrous somewhere. 8634 * 8635 * After updating the MSS, retransmit part of the 8636 * dropped segment using the new mss by calling 8637 * tcp_wput_data(). Need to adjust all those 8638 * params to make sure tcp_wput_data() work properly. 8639 */ 8640 if (tcps->tcps_ignore_path_mtu) 8641 break; 8642 8643 /* 8644 * Decrease the MSS by time stamp options 8645 * IP options and IPSEC options. tcp_hdr_len 8646 * includes time stamp option and IP option 8647 * length. 8648 */ 8649 8650 new_mss = ntohs(icmph->icmph_du_mtu) - 8651 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8652 8653 /* 8654 * Only update the MSS if the new one is 8655 * smaller than the previous one. This is 8656 * to avoid problems when getting multiple 8657 * ICMP errors for the same MTU. 8658 */ 8659 if (new_mss >= tcp->tcp_mss) 8660 break; 8661 8662 /* 8663 * Stop doing PMTU if new_mss is less than 68 8664 * or less than tcp_mss_min. 8665 * The value 68 comes from rfc 1191. 8666 */ 8667 if (new_mss < MAX(68, tcps->tcps_mss_min)) 8668 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8669 0; 8670 8671 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8672 ASSERT(ratio >= 1); 8673 tcp_mss_set(tcp, new_mss, B_TRUE); 8674 8675 /* 8676 * Make sure we have something to 8677 * send. 8678 */ 8679 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8680 (tcp->tcp_xmit_head != NULL)) { 8681 /* 8682 * Shrink tcp_cwnd in 8683 * proportion to the old MSS/new MSS. 8684 */ 8685 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8686 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8687 (tcp->tcp_unsent == 0)) { 8688 tcp->tcp_rexmit_max = tcp->tcp_fss; 8689 } else { 8690 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8691 } 8692 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8693 tcp->tcp_rexmit = B_TRUE; 8694 tcp->tcp_dupack_cnt = 0; 8695 tcp->tcp_snd_burst = TCP_CWND_SS; 8696 tcp_ss_rexmit(tcp); 8697 } 8698 break; 8699 case ICMP_PORT_UNREACHABLE: 8700 case ICMP_PROTOCOL_UNREACHABLE: 8701 switch (tcp->tcp_state) { 8702 case TCPS_SYN_SENT: 8703 case TCPS_SYN_RCVD: 8704 /* 8705 * ICMP can snipe away incipient 8706 * TCP connections as long as 8707 * seq number is same as initial 8708 * send seq number. 8709 */ 8710 if (seg_seq == tcp->tcp_iss) { 8711 (void) tcp_clean_death(tcp, 8712 ECONNREFUSED, 6); 8713 } 8714 break; 8715 } 8716 break; 8717 case ICMP_HOST_UNREACHABLE: 8718 case ICMP_NET_UNREACHABLE: 8719 /* Record the error in case we finally time out. */ 8720 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8721 tcp->tcp_client_errno = EHOSTUNREACH; 8722 else 8723 tcp->tcp_client_errno = ENETUNREACH; 8724 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8725 if (tcp->tcp_listener != NULL && 8726 tcp->tcp_listener->tcp_syn_defense) { 8727 /* 8728 * Ditch the half-open connection if we 8729 * suspect a SYN attack is under way. 8730 */ 8731 tcp_ip_ire_mark_advice(tcp); 8732 (void) tcp_clean_death(tcp, 8733 tcp->tcp_client_errno, 7); 8734 } 8735 } 8736 break; 8737 default: 8738 break; 8739 } 8740 break; 8741 case ICMP_SOURCE_QUENCH: { 8742 /* 8743 * use a global boolean to control 8744 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8745 * The default is false. 8746 */ 8747 if (tcp_icmp_source_quench) { 8748 /* 8749 * Reduce the sending rate as if we got a 8750 * retransmit timeout 8751 */ 8752 uint32_t npkt; 8753 8754 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8755 tcp->tcp_mss; 8756 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8757 tcp->tcp_cwnd = tcp->tcp_mss; 8758 tcp->tcp_cwnd_cnt = 0; 8759 } 8760 break; 8761 } 8762 } 8763 freemsg(first_mp); 8764 } 8765 8766 /* 8767 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8768 * error messages passed up by IP. 8769 * Assumes that IP has pulled up all the extension headers as well 8770 * as the ICMPv6 header. 8771 */ 8772 static void 8773 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8774 { 8775 icmp6_t *icmp6; 8776 ip6_t *ip6h; 8777 uint16_t iph_hdr_length; 8778 tcpha_t *tcpha; 8779 uint8_t *nexthdrp; 8780 uint32_t new_mss; 8781 uint32_t ratio; 8782 boolean_t secure; 8783 mblk_t *first_mp = mp; 8784 size_t mp_size; 8785 uint32_t seg_seq; 8786 tcp_stack_t *tcps = tcp->tcp_tcps; 8787 8788 /* 8789 * The caller has determined if this is an IPSEC_IN packet and 8790 * set ipsec_mctl appropriately (see tcp_icmp_error). 8791 */ 8792 if (ipsec_mctl) 8793 mp = mp->b_cont; 8794 8795 mp_size = MBLKL(mp); 8796 8797 /* 8798 * Verify that we have a complete IP header. If not, send it upstream. 8799 */ 8800 if (mp_size < sizeof (ip6_t)) { 8801 noticmpv6: 8802 freemsg(first_mp); 8803 return; 8804 } 8805 8806 /* 8807 * Verify this is an ICMPV6 packet, else send it upstream. 8808 */ 8809 ip6h = (ip6_t *)mp->b_rptr; 8810 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8811 iph_hdr_length = IPV6_HDR_LEN; 8812 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8813 &nexthdrp) || 8814 *nexthdrp != IPPROTO_ICMPV6) { 8815 goto noticmpv6; 8816 } 8817 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8818 ip6h = (ip6_t *)&icmp6[1]; 8819 /* 8820 * Verify if we have a complete ICMP and inner IP header. 8821 */ 8822 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8823 goto noticmpv6; 8824 8825 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8826 goto noticmpv6; 8827 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8828 /* 8829 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8830 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8831 * packet. 8832 */ 8833 if ((*nexthdrp != IPPROTO_TCP) || 8834 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8835 goto noticmpv6; 8836 } 8837 8838 /* 8839 * ICMP errors come on the right queue or come on 8840 * listener/global queue for detached connections and 8841 * get switched to the right queue. If it comes on the 8842 * right queue, policy check has already been done by IP 8843 * and thus free the first_mp without verifying the policy. 8844 * If it has come for a non-hard bound connection, we need 8845 * to verify policy as IP may not have done it. 8846 */ 8847 if (!tcp->tcp_hard_bound) { 8848 if (ipsec_mctl) { 8849 secure = ipsec_in_is_secure(first_mp); 8850 } else { 8851 secure = B_FALSE; 8852 } 8853 if (secure) { 8854 /* 8855 * If we are willing to accept this in clear 8856 * we don't have to verify policy. 8857 */ 8858 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8859 if (!tcp_check_policy(tcp, first_mp, 8860 NULL, ip6h, secure, ipsec_mctl)) { 8861 /* 8862 * tcp_check_policy called 8863 * ip_drop_packet() on failure. 8864 */ 8865 return; 8866 } 8867 } 8868 } 8869 } else if (ipsec_mctl) { 8870 /* 8871 * This is a hard_bound connection. IP has already 8872 * verified policy. We don't have to do it again. 8873 */ 8874 freeb(first_mp); 8875 first_mp = mp; 8876 ipsec_mctl = B_FALSE; 8877 } 8878 8879 seg_seq = ntohl(tcpha->tha_seq); 8880 /* 8881 * TCP SHOULD check that the TCP sequence number contained in 8882 * payload of the ICMP error message is within the range 8883 * SND.UNA <= SEG.SEQ < SND.NXT. 8884 */ 8885 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8886 /* 8887 * If the ICMP message is bogus, should we kill the 8888 * connection, or should we just drop the bogus ICMP 8889 * message? It would probably make more sense to just 8890 * drop the message so that if this one managed to get 8891 * in, the real connection should not suffer. 8892 */ 8893 goto noticmpv6; 8894 } 8895 8896 switch (icmp6->icmp6_type) { 8897 case ICMP6_PACKET_TOO_BIG: 8898 /* 8899 * Reduce the MSS based on the new MTU. This will 8900 * eliminate any fragmentation locally. 8901 * N.B. There may well be some funny side-effects on 8902 * the local send policy and the remote receive policy. 8903 * Pending further research, we provide 8904 * tcp_ignore_path_mtu just in case this proves 8905 * disastrous somewhere. 8906 * 8907 * After updating the MSS, retransmit part of the 8908 * dropped segment using the new mss by calling 8909 * tcp_wput_data(). Need to adjust all those 8910 * params to make sure tcp_wput_data() work properly. 8911 */ 8912 if (tcps->tcps_ignore_path_mtu) 8913 break; 8914 8915 /* 8916 * Decrease the MSS by time stamp options 8917 * IP options and IPSEC options. tcp_hdr_len 8918 * includes time stamp option and IP option 8919 * length. 8920 */ 8921 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8922 tcp->tcp_ipsec_overhead; 8923 8924 /* 8925 * Only update the MSS if the new one is 8926 * smaller than the previous one. This is 8927 * to avoid problems when getting multiple 8928 * ICMP errors for the same MTU. 8929 */ 8930 if (new_mss >= tcp->tcp_mss) 8931 break; 8932 8933 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8934 ASSERT(ratio >= 1); 8935 tcp_mss_set(tcp, new_mss, B_TRUE); 8936 8937 /* 8938 * Make sure we have something to 8939 * send. 8940 */ 8941 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8942 (tcp->tcp_xmit_head != NULL)) { 8943 /* 8944 * Shrink tcp_cwnd in 8945 * proportion to the old MSS/new MSS. 8946 */ 8947 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8948 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8949 (tcp->tcp_unsent == 0)) { 8950 tcp->tcp_rexmit_max = tcp->tcp_fss; 8951 } else { 8952 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8953 } 8954 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8955 tcp->tcp_rexmit = B_TRUE; 8956 tcp->tcp_dupack_cnt = 0; 8957 tcp->tcp_snd_burst = TCP_CWND_SS; 8958 tcp_ss_rexmit(tcp); 8959 } 8960 break; 8961 8962 case ICMP6_DST_UNREACH: 8963 switch (icmp6->icmp6_code) { 8964 case ICMP6_DST_UNREACH_NOPORT: 8965 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8966 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8967 (seg_seq == tcp->tcp_iss)) { 8968 (void) tcp_clean_death(tcp, 8969 ECONNREFUSED, 8); 8970 } 8971 break; 8972 8973 case ICMP6_DST_UNREACH_ADMIN: 8974 case ICMP6_DST_UNREACH_NOROUTE: 8975 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8976 case ICMP6_DST_UNREACH_ADDR: 8977 /* Record the error in case we finally time out. */ 8978 tcp->tcp_client_errno = EHOSTUNREACH; 8979 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8980 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8981 (seg_seq == tcp->tcp_iss)) { 8982 if (tcp->tcp_listener != NULL && 8983 tcp->tcp_listener->tcp_syn_defense) { 8984 /* 8985 * Ditch the half-open connection if we 8986 * suspect a SYN attack is under way. 8987 */ 8988 tcp_ip_ire_mark_advice(tcp); 8989 (void) tcp_clean_death(tcp, 8990 tcp->tcp_client_errno, 9); 8991 } 8992 } 8993 8994 8995 break; 8996 default: 8997 break; 8998 } 8999 break; 9000 9001 case ICMP6_PARAM_PROB: 9002 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 9003 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 9004 (uchar_t *)ip6h + icmp6->icmp6_pptr == 9005 (uchar_t *)nexthdrp) { 9006 if (tcp->tcp_state == TCPS_SYN_SENT || 9007 tcp->tcp_state == TCPS_SYN_RCVD) { 9008 (void) tcp_clean_death(tcp, 9009 ECONNREFUSED, 10); 9010 } 9011 break; 9012 } 9013 break; 9014 9015 case ICMP6_TIME_EXCEEDED: 9016 default: 9017 break; 9018 } 9019 freemsg(first_mp); 9020 } 9021 9022 /* 9023 * IP recognizes seven kinds of bind requests: 9024 * 9025 * - A zero-length address binds only to the protocol number. 9026 * 9027 * - A 4-byte address is treated as a request to 9028 * validate that the address is a valid local IPv4 9029 * address, appropriate for an application to bind to. 9030 * IP does the verification, but does not make any note 9031 * of the address at this time. 9032 * 9033 * - A 16-byte address contains is treated as a request 9034 * to validate a local IPv6 address, as the 4-byte 9035 * address case above. 9036 * 9037 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 9038 * use it for the inbound fanout of packets. 9039 * 9040 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 9041 * use it for the inbound fanout of packets. 9042 * 9043 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 9044 * information consisting of local and remote addresses 9045 * and ports. In this case, the addresses are both 9046 * validated as appropriate for this operation, and, if 9047 * so, the information is retained for use in the 9048 * inbound fanout. 9049 * 9050 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 9051 * fanout information, like the 12-byte case above. 9052 * 9053 * IP will also fill in the IRE request mblk with information 9054 * regarding our peer. In all cases, we notify IP of our protocol 9055 * type by appending a single protocol byte to the bind request. 9056 */ 9057 static mblk_t * 9058 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 9059 { 9060 char *cp; 9061 mblk_t *mp; 9062 struct T_bind_req *tbr; 9063 ipa_conn_t *ac; 9064 ipa6_conn_t *ac6; 9065 sin_t *sin; 9066 sin6_t *sin6; 9067 9068 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 9069 ASSERT((tcp->tcp_family == AF_INET && 9070 tcp->tcp_ipversion == IPV4_VERSION) || 9071 (tcp->tcp_family == AF_INET6 && 9072 (tcp->tcp_ipversion == IPV4_VERSION || 9073 tcp->tcp_ipversion == IPV6_VERSION))); 9074 9075 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 9076 if (!mp) 9077 return (mp); 9078 mp->b_datap->db_type = M_PROTO; 9079 tbr = (struct T_bind_req *)mp->b_rptr; 9080 tbr->PRIM_type = bind_prim; 9081 tbr->ADDR_offset = sizeof (*tbr); 9082 tbr->CONIND_number = 0; 9083 tbr->ADDR_length = addr_length; 9084 cp = (char *)&tbr[1]; 9085 switch (addr_length) { 9086 case sizeof (ipa_conn_t): 9087 ASSERT(tcp->tcp_family == AF_INET); 9088 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9089 9090 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9091 if (mp->b_cont == NULL) { 9092 freemsg(mp); 9093 return (NULL); 9094 } 9095 mp->b_cont->b_wptr += sizeof (ire_t); 9096 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9097 9098 /* cp known to be 32 bit aligned */ 9099 ac = (ipa_conn_t *)cp; 9100 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 9101 ac->ac_faddr = tcp->tcp_remote; 9102 ac->ac_fport = tcp->tcp_fport; 9103 ac->ac_lport = tcp->tcp_lport; 9104 tcp->tcp_hard_binding = 1; 9105 break; 9106 9107 case sizeof (ipa6_conn_t): 9108 ASSERT(tcp->tcp_family == AF_INET6); 9109 9110 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 9111 if (mp->b_cont == NULL) { 9112 freemsg(mp); 9113 return (NULL); 9114 } 9115 mp->b_cont->b_wptr += sizeof (ire_t); 9116 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 9117 9118 /* cp known to be 32 bit aligned */ 9119 ac6 = (ipa6_conn_t *)cp; 9120 if (tcp->tcp_ipversion == IPV4_VERSION) { 9121 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 9122 &ac6->ac6_laddr); 9123 } else { 9124 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 9125 } 9126 ac6->ac6_faddr = tcp->tcp_remote_v6; 9127 ac6->ac6_fport = tcp->tcp_fport; 9128 ac6->ac6_lport = tcp->tcp_lport; 9129 tcp->tcp_hard_binding = 1; 9130 break; 9131 9132 case sizeof (sin_t): 9133 /* 9134 * NOTE: IPV6_ADDR_LEN also has same size. 9135 * Use family to discriminate. 9136 */ 9137 if (tcp->tcp_family == AF_INET) { 9138 sin = (sin_t *)cp; 9139 9140 *sin = sin_null; 9141 sin->sin_family = AF_INET; 9142 sin->sin_addr.s_addr = tcp->tcp_bound_source; 9143 sin->sin_port = tcp->tcp_lport; 9144 break; 9145 } else { 9146 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 9147 } 9148 break; 9149 9150 case sizeof (sin6_t): 9151 ASSERT(tcp->tcp_family == AF_INET6); 9152 sin6 = (sin6_t *)cp; 9153 9154 *sin6 = sin6_null; 9155 sin6->sin6_family = AF_INET6; 9156 sin6->sin6_addr = tcp->tcp_bound_source_v6; 9157 sin6->sin6_port = tcp->tcp_lport; 9158 break; 9159 9160 case IP_ADDR_LEN: 9161 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 9162 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 9163 break; 9164 9165 } 9166 /* Add protocol number to end */ 9167 cp[addr_length] = (char)IPPROTO_TCP; 9168 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 9169 return (mp); 9170 } 9171 9172 /* 9173 * Notify IP that we are having trouble with this connection. IP should 9174 * blow the IRE away and start over. 9175 */ 9176 static void 9177 tcp_ip_notify(tcp_t *tcp) 9178 { 9179 struct iocblk *iocp; 9180 ipid_t *ipid; 9181 mblk_t *mp; 9182 9183 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 9184 if (tcp->tcp_ipversion == IPV6_VERSION) 9185 return; 9186 9187 mp = mkiocb(IP_IOCTL); 9188 if (mp == NULL) 9189 return; 9190 9191 iocp = (struct iocblk *)mp->b_rptr; 9192 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 9193 9194 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 9195 if (!mp->b_cont) { 9196 freeb(mp); 9197 return; 9198 } 9199 9200 ipid = (ipid_t *)mp->b_cont->b_rptr; 9201 mp->b_cont->b_wptr += iocp->ioc_count; 9202 bzero(ipid, sizeof (*ipid)); 9203 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 9204 ipid->ipid_ire_type = IRE_CACHE; 9205 ipid->ipid_addr_offset = sizeof (ipid_t); 9206 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 9207 /* 9208 * Note: in the case of source routing we want to blow away the 9209 * route to the first source route hop. 9210 */ 9211 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 9212 sizeof (tcp->tcp_ipha->ipha_dst)); 9213 9214 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 9215 } 9216 9217 /* Unlink and return any mblk that looks like it contains an ire */ 9218 static mblk_t * 9219 tcp_ire_mp(mblk_t *mp) 9220 { 9221 mblk_t *prev_mp; 9222 9223 for (;;) { 9224 prev_mp = mp; 9225 mp = mp->b_cont; 9226 if (mp == NULL) 9227 break; 9228 switch (DB_TYPE(mp)) { 9229 case IRE_DB_TYPE: 9230 case IRE_DB_REQ_TYPE: 9231 if (prev_mp != NULL) 9232 prev_mp->b_cont = mp->b_cont; 9233 mp->b_cont = NULL; 9234 return (mp); 9235 default: 9236 break; 9237 } 9238 } 9239 return (mp); 9240 } 9241 9242 /* 9243 * Timer callback routine for keepalive probe. We do a fake resend of 9244 * last ACKed byte. Then set a timer using RTO. When the timer expires, 9245 * check to see if we have heard anything from the other end for the last 9246 * RTO period. If we have, set the timer to expire for another 9247 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 9248 * RTO << 1 and check again when it expires. Keep exponentially increasing 9249 * the timeout if we have not heard from the other side. If for more than 9250 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 9251 * kill the connection unless the keepalive abort threshold is 0. In 9252 * that case, we will probe "forever." 9253 */ 9254 static void 9255 tcp_keepalive_killer(void *arg) 9256 { 9257 mblk_t *mp; 9258 conn_t *connp = (conn_t *)arg; 9259 tcp_t *tcp = connp->conn_tcp; 9260 int32_t firetime; 9261 int32_t idletime; 9262 int32_t ka_intrvl; 9263 tcp_stack_t *tcps = tcp->tcp_tcps; 9264 9265 tcp->tcp_ka_tid = 0; 9266 9267 if (tcp->tcp_fused) 9268 return; 9269 9270 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 9271 ka_intrvl = tcp->tcp_ka_interval; 9272 9273 /* 9274 * Keepalive probe should only be sent if the application has not 9275 * done a close on the connection. 9276 */ 9277 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 9278 return; 9279 } 9280 /* Timer fired too early, restart it. */ 9281 if (tcp->tcp_state < TCPS_ESTABLISHED) { 9282 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9283 MSEC_TO_TICK(ka_intrvl)); 9284 return; 9285 } 9286 9287 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 9288 /* 9289 * If we have not heard from the other side for a long 9290 * time, kill the connection unless the keepalive abort 9291 * threshold is 0. In that case, we will probe "forever." 9292 */ 9293 if (tcp->tcp_ka_abort_thres != 0 && 9294 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 9295 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 9296 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 9297 tcp->tcp_client_errno : ETIMEDOUT, 11); 9298 return; 9299 } 9300 9301 if (tcp->tcp_snxt == tcp->tcp_suna && 9302 idletime >= ka_intrvl) { 9303 /* Fake resend of last ACKed byte. */ 9304 mblk_t *mp1 = allocb(1, BPRI_LO); 9305 9306 if (mp1 != NULL) { 9307 *mp1->b_wptr++ = '\0'; 9308 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 9309 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 9310 freeb(mp1); 9311 /* 9312 * if allocation failed, fall through to start the 9313 * timer back. 9314 */ 9315 if (mp != NULL) { 9316 tcp_send_data(tcp, tcp->tcp_wq, mp); 9317 BUMP_MIB(&tcps->tcps_mib, 9318 tcpTimKeepaliveProbe); 9319 if (tcp->tcp_ka_last_intrvl != 0) { 9320 int max; 9321 /* 9322 * We should probe again at least 9323 * in ka_intrvl, but not more than 9324 * tcp_rexmit_interval_max. 9325 */ 9326 max = tcps->tcps_rexmit_interval_max; 9327 firetime = MIN(ka_intrvl - 1, 9328 tcp->tcp_ka_last_intrvl << 1); 9329 if (firetime > max) 9330 firetime = max; 9331 } else { 9332 firetime = tcp->tcp_rto; 9333 } 9334 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9335 tcp_keepalive_killer, 9336 MSEC_TO_TICK(firetime)); 9337 tcp->tcp_ka_last_intrvl = firetime; 9338 return; 9339 } 9340 } 9341 } else { 9342 tcp->tcp_ka_last_intrvl = 0; 9343 } 9344 9345 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9346 if ((firetime = ka_intrvl - idletime) < 0) { 9347 firetime = ka_intrvl; 9348 } 9349 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9350 MSEC_TO_TICK(firetime)); 9351 } 9352 9353 int 9354 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9355 { 9356 queue_t *q = tcp->tcp_rq; 9357 int32_t mss = tcp->tcp_mss; 9358 int maxpsz; 9359 9360 if (TCP_IS_DETACHED(tcp)) 9361 return (mss); 9362 9363 if (tcp->tcp_fused) { 9364 maxpsz = tcp_fuse_maxpsz_set(tcp); 9365 mss = INFPSZ; 9366 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 9367 /* 9368 * Set the sd_qn_maxpsz according to the socket send buffer 9369 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9370 * instruct the stream head to copyin user data into contiguous 9371 * kernel-allocated buffers without breaking it up into smaller 9372 * chunks. We round up the buffer size to the nearest SMSS. 9373 */ 9374 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9375 if (tcp->tcp_kssl_ctx == NULL) 9376 mss = INFPSZ; 9377 else 9378 mss = SSL3_MAX_RECORD_LEN; 9379 } else { 9380 /* 9381 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9382 * (and a multiple of the mss). This instructs the stream 9383 * head to break down larger than SMSS writes into SMSS- 9384 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9385 */ 9386 maxpsz = tcp->tcp_maxpsz * mss; 9387 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9388 maxpsz = tcp->tcp_xmit_hiwater/2; 9389 /* Round up to nearest mss */ 9390 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9391 } 9392 } 9393 (void) setmaxps(q, maxpsz); 9394 tcp->tcp_wq->q_maxpsz = maxpsz; 9395 9396 if (set_maxblk) 9397 (void) mi_set_sth_maxblk(q, mss); 9398 9399 return (mss); 9400 } 9401 9402 /* 9403 * Extract option values from a tcp header. We put any found values into the 9404 * tcpopt struct and return a bitmask saying which options were found. 9405 */ 9406 static int 9407 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9408 { 9409 uchar_t *endp; 9410 int len; 9411 uint32_t mss; 9412 uchar_t *up = (uchar_t *)tcph; 9413 int found = 0; 9414 int32_t sack_len; 9415 tcp_seq sack_begin, sack_end; 9416 tcp_t *tcp; 9417 9418 endp = up + TCP_HDR_LENGTH(tcph); 9419 up += TCP_MIN_HEADER_LENGTH; 9420 while (up < endp) { 9421 len = endp - up; 9422 switch (*up) { 9423 case TCPOPT_EOL: 9424 break; 9425 9426 case TCPOPT_NOP: 9427 up++; 9428 continue; 9429 9430 case TCPOPT_MAXSEG: 9431 if (len < TCPOPT_MAXSEG_LEN || 9432 up[1] != TCPOPT_MAXSEG_LEN) 9433 break; 9434 9435 mss = BE16_TO_U16(up+2); 9436 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9437 tcpopt->tcp_opt_mss = mss; 9438 found |= TCP_OPT_MSS_PRESENT; 9439 9440 up += TCPOPT_MAXSEG_LEN; 9441 continue; 9442 9443 case TCPOPT_WSCALE: 9444 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9445 break; 9446 9447 if (up[2] > TCP_MAX_WINSHIFT) 9448 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9449 else 9450 tcpopt->tcp_opt_wscale = up[2]; 9451 found |= TCP_OPT_WSCALE_PRESENT; 9452 9453 up += TCPOPT_WS_LEN; 9454 continue; 9455 9456 case TCPOPT_SACK_PERMITTED: 9457 if (len < TCPOPT_SACK_OK_LEN || 9458 up[1] != TCPOPT_SACK_OK_LEN) 9459 break; 9460 found |= TCP_OPT_SACK_OK_PRESENT; 9461 up += TCPOPT_SACK_OK_LEN; 9462 continue; 9463 9464 case TCPOPT_SACK: 9465 if (len <= 2 || up[1] <= 2 || len < up[1]) 9466 break; 9467 9468 /* If TCP is not interested in SACK blks... */ 9469 if ((tcp = tcpopt->tcp) == NULL) { 9470 up += up[1]; 9471 continue; 9472 } 9473 sack_len = up[1] - TCPOPT_HEADER_LEN; 9474 up += TCPOPT_HEADER_LEN; 9475 9476 /* 9477 * If the list is empty, allocate one and assume 9478 * nothing is sack'ed. 9479 */ 9480 ASSERT(tcp->tcp_sack_info != NULL); 9481 if (tcp->tcp_notsack_list == NULL) { 9482 tcp_notsack_update(&(tcp->tcp_notsack_list), 9483 tcp->tcp_suna, tcp->tcp_snxt, 9484 &(tcp->tcp_num_notsack_blk), 9485 &(tcp->tcp_cnt_notsack_list)); 9486 9487 /* 9488 * Make sure tcp_notsack_list is not NULL. 9489 * This happens when kmem_alloc(KM_NOSLEEP) 9490 * returns NULL. 9491 */ 9492 if (tcp->tcp_notsack_list == NULL) { 9493 up += sack_len; 9494 continue; 9495 } 9496 tcp->tcp_fack = tcp->tcp_suna; 9497 } 9498 9499 while (sack_len > 0) { 9500 if (up + 8 > endp) { 9501 up = endp; 9502 break; 9503 } 9504 sack_begin = BE32_TO_U32(up); 9505 up += 4; 9506 sack_end = BE32_TO_U32(up); 9507 up += 4; 9508 sack_len -= 8; 9509 /* 9510 * Bounds checking. Make sure the SACK 9511 * info is within tcp_suna and tcp_snxt. 9512 * If this SACK blk is out of bound, ignore 9513 * it but continue to parse the following 9514 * blks. 9515 */ 9516 if (SEQ_LEQ(sack_end, sack_begin) || 9517 SEQ_LT(sack_begin, tcp->tcp_suna) || 9518 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9519 continue; 9520 } 9521 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9522 sack_begin, sack_end, 9523 &(tcp->tcp_num_notsack_blk), 9524 &(tcp->tcp_cnt_notsack_list)); 9525 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9526 tcp->tcp_fack = sack_end; 9527 } 9528 } 9529 found |= TCP_OPT_SACK_PRESENT; 9530 continue; 9531 9532 case TCPOPT_TSTAMP: 9533 if (len < TCPOPT_TSTAMP_LEN || 9534 up[1] != TCPOPT_TSTAMP_LEN) 9535 break; 9536 9537 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9538 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9539 9540 found |= TCP_OPT_TSTAMP_PRESENT; 9541 9542 up += TCPOPT_TSTAMP_LEN; 9543 continue; 9544 9545 default: 9546 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9547 break; 9548 up += up[1]; 9549 continue; 9550 } 9551 break; 9552 } 9553 return (found); 9554 } 9555 9556 /* 9557 * Set the mss associated with a particular tcp based on its current value, 9558 * and a new one passed in. Observe minimums and maximums, and reset 9559 * other state variables that we want to view as multiples of mss. 9560 * 9561 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9562 * highwater marks etc. need to be initialized or adjusted. 9563 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9564 * packet arrives. 9565 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9566 * ICMP6_PACKET_TOO_BIG arrives. 9567 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9568 * to increase the MSS to use the extra bytes available. 9569 * 9570 * Callers except tcp_paws_check() ensure that they only reduce mss. 9571 */ 9572 static void 9573 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9574 { 9575 uint32_t mss_max; 9576 tcp_stack_t *tcps = tcp->tcp_tcps; 9577 9578 if (tcp->tcp_ipversion == IPV4_VERSION) 9579 mss_max = tcps->tcps_mss_max_ipv4; 9580 else 9581 mss_max = tcps->tcps_mss_max_ipv6; 9582 9583 if (mss < tcps->tcps_mss_min) 9584 mss = tcps->tcps_mss_min; 9585 if (mss > mss_max) 9586 mss = mss_max; 9587 /* 9588 * Unless naglim has been set by our client to 9589 * a non-mss value, force naglim to track mss. 9590 * This can help to aggregate small writes. 9591 */ 9592 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9593 tcp->tcp_naglim = mss; 9594 /* 9595 * TCP should be able to buffer at least 4 MSS data for obvious 9596 * performance reason. 9597 */ 9598 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9599 tcp->tcp_xmit_hiwater = mss << 2; 9600 9601 if (do_ss) { 9602 /* 9603 * Either the tcp_cwnd is as yet uninitialized, or mss is 9604 * changing due to a reduction in MTU, presumably as a 9605 * result of a new path component, reset cwnd to its 9606 * "initial" value, as a multiple of the new mss. 9607 */ 9608 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9609 } else { 9610 /* 9611 * Called by tcp_paws_check(), the mss increased 9612 * marginally to allow use of space previously taken 9613 * by the timestamp option. It would be inappropriate 9614 * to apply slow start or tcp_init_cwnd values to 9615 * tcp_cwnd, simply adjust to a multiple of the new mss. 9616 */ 9617 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9618 tcp->tcp_cwnd_cnt = 0; 9619 } 9620 tcp->tcp_mss = mss; 9621 (void) tcp_maxpsz_set(tcp, B_TRUE); 9622 } 9623 9624 /* For /dev/tcp aka AF_INET open */ 9625 static int 9626 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9627 { 9628 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9629 } 9630 9631 /* For /dev/tcp6 aka AF_INET6 open */ 9632 static int 9633 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9634 { 9635 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9636 } 9637 9638 static int 9639 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9640 boolean_t isv6) 9641 { 9642 tcp_t *tcp = NULL; 9643 conn_t *connp; 9644 int err; 9645 vmem_t *minor_arena = NULL; 9646 dev_t conn_dev; 9647 zoneid_t zoneid; 9648 tcp_stack_t *tcps = NULL; 9649 9650 if (q->q_ptr != NULL) 9651 return (0); 9652 9653 if (sflag == MODOPEN) 9654 return (EINVAL); 9655 9656 if (!(flag & SO_ACCEPTOR)) { 9657 /* 9658 * Special case for install: miniroot needs to be able to 9659 * access files via NFS as though it were always in the 9660 * global zone. 9661 */ 9662 if (credp == kcred && nfs_global_client_only != 0) { 9663 zoneid = GLOBAL_ZONEID; 9664 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9665 netstack_tcp; 9666 ASSERT(tcps != NULL); 9667 } else { 9668 netstack_t *ns; 9669 9670 ns = netstack_find_by_cred(credp); 9671 ASSERT(ns != NULL); 9672 tcps = ns->netstack_tcp; 9673 ASSERT(tcps != NULL); 9674 9675 /* 9676 * For exclusive stacks we set the zoneid to zero 9677 * to make TCP operate as if in the global zone. 9678 */ 9679 if (tcps->tcps_netstack->netstack_stackid != 9680 GLOBAL_NETSTACKID) 9681 zoneid = GLOBAL_ZONEID; 9682 else 9683 zoneid = crgetzoneid(credp); 9684 } 9685 /* 9686 * For stackid zero this is done from strplumb.c, but 9687 * non-zero stackids are handled here. 9688 */ 9689 if (tcps->tcps_g_q == NULL && 9690 tcps->tcps_netstack->netstack_stackid != 9691 GLOBAL_NETSTACKID) { 9692 tcp_g_q_setup(tcps); 9693 } 9694 } 9695 9696 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9697 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9698 minor_arena = ip_minor_arena_la; 9699 } else { 9700 /* 9701 * Either minor numbers in the large arena were exhausted 9702 * or a non socket application is doing the open. 9703 * Try to allocate from the small arena. 9704 */ 9705 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9706 if (tcps != NULL) 9707 netstack_rele(tcps->tcps_netstack); 9708 return (EBUSY); 9709 } 9710 minor_arena = ip_minor_arena_sa; 9711 } 9712 ASSERT(minor_arena != NULL); 9713 9714 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9715 9716 if (flag & SO_ACCEPTOR) { 9717 /* No netstack_find_by_cred, hence no netstack_rele needed */ 9718 ASSERT(tcps == NULL); 9719 q->q_qinfo = &tcp_acceptor_rinit; 9720 /* 9721 * the conn_dev and minor_arena will be subsequently used by 9722 * tcp_wput_accept() and tcpclose_accept() to figure out the 9723 * minor device number for this connection from the q_ptr. 9724 */ 9725 RD(q)->q_ptr = (void *)conn_dev; 9726 WR(q)->q_qinfo = &tcp_acceptor_winit; 9727 WR(q)->q_ptr = (void *)minor_arena; 9728 qprocson(q); 9729 return (0); 9730 } 9731 9732 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt), tcps); 9733 /* 9734 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9735 * so we drop it by one. 9736 */ 9737 netstack_rele(tcps->tcps_netstack); 9738 if (connp == NULL) { 9739 inet_minor_free(minor_arena, conn_dev); 9740 q->q_ptr = NULL; 9741 return (ENOSR); 9742 } 9743 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9744 tcp = connp->conn_tcp; 9745 9746 q->q_ptr = WR(q)->q_ptr = connp; 9747 if (isv6) { 9748 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9749 connp->conn_send = ip_output_v6; 9750 connp->conn_af_isv6 = B_TRUE; 9751 connp->conn_pkt_isv6 = B_TRUE; 9752 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9753 tcp->tcp_ipversion = IPV6_VERSION; 9754 tcp->tcp_family = AF_INET6; 9755 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9756 } else { 9757 connp->conn_flags |= IPCL_TCP4; 9758 connp->conn_send = ip_output; 9759 connp->conn_af_isv6 = B_FALSE; 9760 connp->conn_pkt_isv6 = B_FALSE; 9761 tcp->tcp_ipversion = IPV4_VERSION; 9762 tcp->tcp_family = AF_INET; 9763 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9764 } 9765 9766 /* 9767 * TCP keeps a copy of cred for cache locality reasons but 9768 * we put a reference only once. If connp->conn_cred 9769 * becomes invalid, tcp_cred should also be set to NULL. 9770 */ 9771 tcp->tcp_cred = connp->conn_cred = credp; 9772 crhold(connp->conn_cred); 9773 tcp->tcp_cpid = curproc->p_pid; 9774 tcp->tcp_open_time = lbolt64; 9775 connp->conn_zoneid = zoneid; 9776 connp->conn_mlp_type = mlptSingle; 9777 connp->conn_ulp_labeled = !is_system_labeled(); 9778 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9779 ASSERT(tcp->tcp_tcps == tcps); 9780 9781 /* 9782 * If the caller has the process-wide flag set, then default to MAC 9783 * exempt mode. This allows read-down to unlabeled hosts. 9784 */ 9785 if (getpflags(NET_MAC_AWARE, credp) != 0) 9786 connp->conn_mac_exempt = B_TRUE; 9787 9788 connp->conn_dev = conn_dev; 9789 connp->conn_minor_arena = minor_arena; 9790 9791 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9792 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9793 9794 if (flag & SO_SOCKSTR) { 9795 /* 9796 * No need to insert a socket in tcp acceptor hash. 9797 * If it was a socket acceptor stream, we dealt with 9798 * it above. A socket listener can never accept a 9799 * connection and doesn't need acceptor_id. 9800 */ 9801 connp->conn_flags |= IPCL_SOCKET; 9802 tcp->tcp_issocket = 1; 9803 WR(q)->q_qinfo = &tcp_sock_winit; 9804 } else { 9805 #ifdef _ILP32 9806 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9807 #else 9808 tcp->tcp_acceptor_id = conn_dev; 9809 #endif /* _ILP32 */ 9810 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9811 } 9812 9813 err = tcp_init(tcp, q); 9814 if (err != 0) { 9815 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 9816 tcp_acceptor_hash_remove(tcp); 9817 CONN_DEC_REF(connp); 9818 q->q_ptr = WR(q)->q_ptr = NULL; 9819 return (err); 9820 } 9821 9822 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9823 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9824 9825 /* Non-zero default values */ 9826 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9827 /* 9828 * Put the ref for TCP. Ref for IP was already put 9829 * by ipcl_conn_create. Also Make the conn_t globally 9830 * visible to walkers 9831 */ 9832 mutex_enter(&connp->conn_lock); 9833 CONN_INC_REF_LOCKED(connp); 9834 ASSERT(connp->conn_ref == 2); 9835 connp->conn_state_flags &= ~CONN_INCIPIENT; 9836 mutex_exit(&connp->conn_lock); 9837 9838 qprocson(q); 9839 return (0); 9840 } 9841 9842 /* 9843 * Some TCP options can be "set" by requesting them in the option 9844 * buffer. This is needed for XTI feature test though we do not 9845 * allow it in general. We interpret that this mechanism is more 9846 * applicable to OSI protocols and need not be allowed in general. 9847 * This routine filters out options for which it is not allowed (most) 9848 * and lets through those (few) for which it is. [ The XTI interface 9849 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9850 * ever implemented will have to be allowed here ]. 9851 */ 9852 static boolean_t 9853 tcp_allow_connopt_set(int level, int name) 9854 { 9855 9856 switch (level) { 9857 case IPPROTO_TCP: 9858 switch (name) { 9859 case TCP_NODELAY: 9860 return (B_TRUE); 9861 default: 9862 return (B_FALSE); 9863 } 9864 /*NOTREACHED*/ 9865 default: 9866 return (B_FALSE); 9867 } 9868 /*NOTREACHED*/ 9869 } 9870 9871 /* 9872 * This routine gets default values of certain options whose default 9873 * values are maintained by protocol specific code 9874 */ 9875 /* ARGSUSED */ 9876 int 9877 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9878 { 9879 int32_t *i1 = (int32_t *)ptr; 9880 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9881 9882 switch (level) { 9883 case IPPROTO_TCP: 9884 switch (name) { 9885 case TCP_NOTIFY_THRESHOLD: 9886 *i1 = tcps->tcps_ip_notify_interval; 9887 break; 9888 case TCP_ABORT_THRESHOLD: 9889 *i1 = tcps->tcps_ip_abort_interval; 9890 break; 9891 case TCP_CONN_NOTIFY_THRESHOLD: 9892 *i1 = tcps->tcps_ip_notify_cinterval; 9893 break; 9894 case TCP_CONN_ABORT_THRESHOLD: 9895 *i1 = tcps->tcps_ip_abort_cinterval; 9896 break; 9897 default: 9898 return (-1); 9899 } 9900 break; 9901 case IPPROTO_IP: 9902 switch (name) { 9903 case IP_TTL: 9904 *i1 = tcps->tcps_ipv4_ttl; 9905 break; 9906 default: 9907 return (-1); 9908 } 9909 break; 9910 case IPPROTO_IPV6: 9911 switch (name) { 9912 case IPV6_UNICAST_HOPS: 9913 *i1 = tcps->tcps_ipv6_hoplimit; 9914 break; 9915 default: 9916 return (-1); 9917 } 9918 break; 9919 default: 9920 return (-1); 9921 } 9922 return (sizeof (int)); 9923 } 9924 9925 9926 /* 9927 * TCP routine to get the values of options. 9928 */ 9929 int 9930 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9931 { 9932 int *i1 = (int *)ptr; 9933 conn_t *connp = Q_TO_CONN(q); 9934 tcp_t *tcp = connp->conn_tcp; 9935 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9936 9937 switch (level) { 9938 case SOL_SOCKET: 9939 switch (name) { 9940 case SO_LINGER: { 9941 struct linger *lgr = (struct linger *)ptr; 9942 9943 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9944 lgr->l_linger = tcp->tcp_lingertime; 9945 } 9946 return (sizeof (struct linger)); 9947 case SO_DEBUG: 9948 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9949 break; 9950 case SO_KEEPALIVE: 9951 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9952 break; 9953 case SO_DONTROUTE: 9954 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9955 break; 9956 case SO_USELOOPBACK: 9957 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9958 break; 9959 case SO_BROADCAST: 9960 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9961 break; 9962 case SO_REUSEADDR: 9963 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9964 break; 9965 case SO_OOBINLINE: 9966 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9967 break; 9968 case SO_DGRAM_ERRIND: 9969 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9970 break; 9971 case SO_TYPE: 9972 *i1 = SOCK_STREAM; 9973 break; 9974 case SO_SNDBUF: 9975 *i1 = tcp->tcp_xmit_hiwater; 9976 break; 9977 case SO_RCVBUF: 9978 *i1 = RD(q)->q_hiwat; 9979 break; 9980 case SO_SND_COPYAVOID: 9981 *i1 = tcp->tcp_snd_zcopy_on ? 9982 SO_SND_COPYAVOID : 0; 9983 break; 9984 case SO_ALLZONES: 9985 *i1 = connp->conn_allzones ? 1 : 0; 9986 break; 9987 case SO_ANON_MLP: 9988 *i1 = connp->conn_anon_mlp; 9989 break; 9990 case SO_MAC_EXEMPT: 9991 *i1 = connp->conn_mac_exempt; 9992 break; 9993 case SO_EXCLBIND: 9994 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9995 break; 9996 case SO_PROTOTYPE: 9997 *i1 = IPPROTO_TCP; 9998 break; 9999 case SO_DOMAIN: 10000 *i1 = tcp->tcp_family; 10001 break; 10002 default: 10003 return (-1); 10004 } 10005 break; 10006 case IPPROTO_TCP: 10007 switch (name) { 10008 case TCP_NODELAY: 10009 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 10010 break; 10011 case TCP_MAXSEG: 10012 *i1 = tcp->tcp_mss; 10013 break; 10014 case TCP_NOTIFY_THRESHOLD: 10015 *i1 = (int)tcp->tcp_first_timer_threshold; 10016 break; 10017 case TCP_ABORT_THRESHOLD: 10018 *i1 = tcp->tcp_second_timer_threshold; 10019 break; 10020 case TCP_CONN_NOTIFY_THRESHOLD: 10021 *i1 = tcp->tcp_first_ctimer_threshold; 10022 break; 10023 case TCP_CONN_ABORT_THRESHOLD: 10024 *i1 = tcp->tcp_second_ctimer_threshold; 10025 break; 10026 case TCP_RECVDSTADDR: 10027 *i1 = tcp->tcp_recvdstaddr; 10028 break; 10029 case TCP_ANONPRIVBIND: 10030 *i1 = tcp->tcp_anon_priv_bind; 10031 break; 10032 case TCP_EXCLBIND: 10033 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 10034 break; 10035 case TCP_INIT_CWND: 10036 *i1 = tcp->tcp_init_cwnd; 10037 break; 10038 case TCP_KEEPALIVE_THRESHOLD: 10039 *i1 = tcp->tcp_ka_interval; 10040 break; 10041 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10042 *i1 = tcp->tcp_ka_abort_thres; 10043 break; 10044 case TCP_CORK: 10045 *i1 = tcp->tcp_cork; 10046 break; 10047 default: 10048 return (-1); 10049 } 10050 break; 10051 case IPPROTO_IP: 10052 if (tcp->tcp_family != AF_INET) 10053 return (-1); 10054 switch (name) { 10055 case IP_OPTIONS: 10056 case T_IP_OPTIONS: { 10057 /* 10058 * This is compatible with BSD in that in only return 10059 * the reverse source route with the final destination 10060 * as the last entry. The first 4 bytes of the option 10061 * will contain the final destination. 10062 */ 10063 int opt_len; 10064 10065 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 10066 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 10067 ASSERT(opt_len >= 0); 10068 /* Caller ensures enough space */ 10069 if (opt_len > 0) { 10070 /* 10071 * TODO: Do we have to handle getsockopt on an 10072 * initiator as well? 10073 */ 10074 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 10075 } 10076 return (0); 10077 } 10078 case IP_TOS: 10079 case T_IP_TOS: 10080 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 10081 break; 10082 case IP_TTL: 10083 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 10084 break; 10085 case IP_NEXTHOP: 10086 /* Handled at IP level */ 10087 return (-EINVAL); 10088 default: 10089 return (-1); 10090 } 10091 break; 10092 case IPPROTO_IPV6: 10093 /* 10094 * IPPROTO_IPV6 options are only supported for sockets 10095 * that are using IPv6 on the wire. 10096 */ 10097 if (tcp->tcp_ipversion != IPV6_VERSION) { 10098 return (-1); 10099 } 10100 switch (name) { 10101 case IPV6_UNICAST_HOPS: 10102 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 10103 break; /* goto sizeof (int) option return */ 10104 case IPV6_BOUND_IF: 10105 /* Zero if not set */ 10106 *i1 = tcp->tcp_bound_if; 10107 break; /* goto sizeof (int) option return */ 10108 case IPV6_RECVPKTINFO: 10109 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 10110 *i1 = 1; 10111 else 10112 *i1 = 0; 10113 break; /* goto sizeof (int) option return */ 10114 case IPV6_RECVTCLASS: 10115 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 10116 *i1 = 1; 10117 else 10118 *i1 = 0; 10119 break; /* goto sizeof (int) option return */ 10120 case IPV6_RECVHOPLIMIT: 10121 if (tcp->tcp_ipv6_recvancillary & 10122 TCP_IPV6_RECVHOPLIMIT) 10123 *i1 = 1; 10124 else 10125 *i1 = 0; 10126 break; /* goto sizeof (int) option return */ 10127 case IPV6_RECVHOPOPTS: 10128 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 10129 *i1 = 1; 10130 else 10131 *i1 = 0; 10132 break; /* goto sizeof (int) option return */ 10133 case IPV6_RECVDSTOPTS: 10134 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 10135 *i1 = 1; 10136 else 10137 *i1 = 0; 10138 break; /* goto sizeof (int) option return */ 10139 case _OLD_IPV6_RECVDSTOPTS: 10140 if (tcp->tcp_ipv6_recvancillary & 10141 TCP_OLD_IPV6_RECVDSTOPTS) 10142 *i1 = 1; 10143 else 10144 *i1 = 0; 10145 break; /* goto sizeof (int) option return */ 10146 case IPV6_RECVRTHDR: 10147 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 10148 *i1 = 1; 10149 else 10150 *i1 = 0; 10151 break; /* goto sizeof (int) option return */ 10152 case IPV6_RECVRTHDRDSTOPTS: 10153 if (tcp->tcp_ipv6_recvancillary & 10154 TCP_IPV6_RECVRTDSTOPTS) 10155 *i1 = 1; 10156 else 10157 *i1 = 0; 10158 break; /* goto sizeof (int) option return */ 10159 case IPV6_PKTINFO: { 10160 /* XXX assumes that caller has room for max size! */ 10161 struct in6_pktinfo *pkti; 10162 10163 pkti = (struct in6_pktinfo *)ptr; 10164 if (ipp->ipp_fields & IPPF_IFINDEX) 10165 pkti->ipi6_ifindex = ipp->ipp_ifindex; 10166 else 10167 pkti->ipi6_ifindex = 0; 10168 if (ipp->ipp_fields & IPPF_ADDR) 10169 pkti->ipi6_addr = ipp->ipp_addr; 10170 else 10171 pkti->ipi6_addr = ipv6_all_zeros; 10172 return (sizeof (struct in6_pktinfo)); 10173 } 10174 case IPV6_TCLASS: 10175 if (ipp->ipp_fields & IPPF_TCLASS) 10176 *i1 = ipp->ipp_tclass; 10177 else 10178 *i1 = IPV6_FLOW_TCLASS( 10179 IPV6_DEFAULT_VERS_AND_FLOW); 10180 break; /* goto sizeof (int) option return */ 10181 case IPV6_NEXTHOP: { 10182 sin6_t *sin6 = (sin6_t *)ptr; 10183 10184 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 10185 return (0); 10186 *sin6 = sin6_null; 10187 sin6->sin6_family = AF_INET6; 10188 sin6->sin6_addr = ipp->ipp_nexthop; 10189 return (sizeof (sin6_t)); 10190 } 10191 case IPV6_HOPOPTS: 10192 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 10193 return (0); 10194 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 10195 return (0); 10196 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 10197 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 10198 if (tcp->tcp_label_len > 0) { 10199 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 10200 ptr[1] = (ipp->ipp_hopoptslen - 10201 tcp->tcp_label_len + 7) / 8 - 1; 10202 } 10203 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 10204 case IPV6_RTHDRDSTOPTS: 10205 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 10206 return (0); 10207 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 10208 return (ipp->ipp_rtdstoptslen); 10209 case IPV6_RTHDR: 10210 if (!(ipp->ipp_fields & IPPF_RTHDR)) 10211 return (0); 10212 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 10213 return (ipp->ipp_rthdrlen); 10214 case IPV6_DSTOPTS: 10215 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 10216 return (0); 10217 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 10218 return (ipp->ipp_dstoptslen); 10219 case IPV6_SRC_PREFERENCES: 10220 return (ip6_get_src_preferences(connp, 10221 (uint32_t *)ptr)); 10222 case IPV6_PATHMTU: { 10223 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 10224 10225 if (tcp->tcp_state < TCPS_ESTABLISHED) 10226 return (-1); 10227 10228 return (ip_fill_mtuinfo(&connp->conn_remv6, 10229 connp->conn_fport, mtuinfo, 10230 connp->conn_netstack)); 10231 } 10232 default: 10233 return (-1); 10234 } 10235 break; 10236 default: 10237 return (-1); 10238 } 10239 return (sizeof (int)); 10240 } 10241 10242 /* 10243 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 10244 * Parameters are assumed to be verified by the caller. 10245 */ 10246 /* ARGSUSED */ 10247 int 10248 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10249 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10250 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10251 { 10252 conn_t *connp = Q_TO_CONN(q); 10253 tcp_t *tcp = connp->conn_tcp; 10254 int *i1 = (int *)invalp; 10255 boolean_t onoff = (*i1 == 0) ? 0 : 1; 10256 boolean_t checkonly; 10257 int reterr; 10258 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 10259 10260 switch (optset_context) { 10261 case SETFN_OPTCOM_CHECKONLY: 10262 checkonly = B_TRUE; 10263 /* 10264 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 10265 * inlen != 0 implies value supplied and 10266 * we have to "pretend" to set it. 10267 * inlen == 0 implies that there is no 10268 * value part in T_CHECK request and just validation 10269 * done elsewhere should be enough, we just return here. 10270 */ 10271 if (inlen == 0) { 10272 *outlenp = 0; 10273 return (0); 10274 } 10275 break; 10276 case SETFN_OPTCOM_NEGOTIATE: 10277 checkonly = B_FALSE; 10278 break; 10279 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 10280 case SETFN_CONN_NEGOTIATE: 10281 checkonly = B_FALSE; 10282 /* 10283 * Negotiating local and "association-related" options 10284 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 10285 * primitives is allowed by XTI, but we choose 10286 * to not implement this style negotiation for Internet 10287 * protocols (We interpret it is a must for OSI world but 10288 * optional for Internet protocols) for all options. 10289 * [ Will do only for the few options that enable test 10290 * suites that our XTI implementation of this feature 10291 * works for transports that do allow it ] 10292 */ 10293 if (!tcp_allow_connopt_set(level, name)) { 10294 *outlenp = 0; 10295 return (EINVAL); 10296 } 10297 break; 10298 default: 10299 /* 10300 * We should never get here 10301 */ 10302 *outlenp = 0; 10303 return (EINVAL); 10304 } 10305 10306 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 10307 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 10308 10309 /* 10310 * For TCP, we should have no ancillary data sent down 10311 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10312 * has to be zero. 10313 */ 10314 ASSERT(thisdg_attrs == NULL); 10315 10316 /* 10317 * For fixed length options, no sanity check 10318 * of passed in length is done. It is assumed *_optcom_req() 10319 * routines do the right thing. 10320 */ 10321 10322 switch (level) { 10323 case SOL_SOCKET: 10324 switch (name) { 10325 case SO_LINGER: { 10326 struct linger *lgr = (struct linger *)invalp; 10327 10328 if (!checkonly) { 10329 if (lgr->l_onoff) { 10330 tcp->tcp_linger = 1; 10331 tcp->tcp_lingertime = lgr->l_linger; 10332 } else { 10333 tcp->tcp_linger = 0; 10334 tcp->tcp_lingertime = 0; 10335 } 10336 /* struct copy */ 10337 *(struct linger *)outvalp = *lgr; 10338 } else { 10339 if (!lgr->l_onoff) { 10340 ((struct linger *) 10341 outvalp)->l_onoff = 0; 10342 ((struct linger *) 10343 outvalp)->l_linger = 0; 10344 } else { 10345 /* struct copy */ 10346 *(struct linger *)outvalp = *lgr; 10347 } 10348 } 10349 *outlenp = sizeof (struct linger); 10350 return (0); 10351 } 10352 case SO_DEBUG: 10353 if (!checkonly) 10354 tcp->tcp_debug = onoff; 10355 break; 10356 case SO_KEEPALIVE: 10357 if (checkonly) { 10358 /* T_CHECK case */ 10359 break; 10360 } 10361 10362 if (!onoff) { 10363 if (tcp->tcp_ka_enabled) { 10364 if (tcp->tcp_ka_tid != 0) { 10365 (void) TCP_TIMER_CANCEL(tcp, 10366 tcp->tcp_ka_tid); 10367 tcp->tcp_ka_tid = 0; 10368 } 10369 tcp->tcp_ka_enabled = 0; 10370 } 10371 break; 10372 } 10373 if (!tcp->tcp_ka_enabled) { 10374 /* Crank up the keepalive timer */ 10375 tcp->tcp_ka_last_intrvl = 0; 10376 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10377 tcp_keepalive_killer, 10378 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10379 tcp->tcp_ka_enabled = 1; 10380 } 10381 break; 10382 case SO_DONTROUTE: 10383 /* 10384 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10385 * only of interest to IP. We track them here only so 10386 * that we can report their current value. 10387 */ 10388 if (!checkonly) { 10389 tcp->tcp_dontroute = onoff; 10390 tcp->tcp_connp->conn_dontroute = onoff; 10391 } 10392 break; 10393 case SO_USELOOPBACK: 10394 if (!checkonly) { 10395 tcp->tcp_useloopback = onoff; 10396 tcp->tcp_connp->conn_loopback = onoff; 10397 } 10398 break; 10399 case SO_BROADCAST: 10400 if (!checkonly) { 10401 tcp->tcp_broadcast = onoff; 10402 tcp->tcp_connp->conn_broadcast = onoff; 10403 } 10404 break; 10405 case SO_REUSEADDR: 10406 if (!checkonly) { 10407 tcp->tcp_reuseaddr = onoff; 10408 tcp->tcp_connp->conn_reuseaddr = onoff; 10409 } 10410 break; 10411 case SO_OOBINLINE: 10412 if (!checkonly) 10413 tcp->tcp_oobinline = onoff; 10414 break; 10415 case SO_DGRAM_ERRIND: 10416 if (!checkonly) 10417 tcp->tcp_dgram_errind = onoff; 10418 break; 10419 case SO_SNDBUF: { 10420 if (*i1 > tcps->tcps_max_buf) { 10421 *outlenp = 0; 10422 return (ENOBUFS); 10423 } 10424 if (checkonly) 10425 break; 10426 10427 tcp->tcp_xmit_hiwater = *i1; 10428 if (tcps->tcps_snd_lowat_fraction != 0) 10429 tcp->tcp_xmit_lowater = 10430 tcp->tcp_xmit_hiwater / 10431 tcps->tcps_snd_lowat_fraction; 10432 (void) tcp_maxpsz_set(tcp, B_TRUE); 10433 /* 10434 * If we are flow-controlled, recheck the condition. 10435 * There are apps that increase SO_SNDBUF size when 10436 * flow-controlled (EWOULDBLOCK), and expect the flow 10437 * control condition to be lifted right away. 10438 */ 10439 mutex_enter(&tcp->tcp_non_sq_lock); 10440 if (tcp->tcp_flow_stopped && 10441 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10442 tcp_clrqfull(tcp); 10443 } 10444 mutex_exit(&tcp->tcp_non_sq_lock); 10445 break; 10446 } 10447 case SO_RCVBUF: 10448 if (*i1 > tcps->tcps_max_buf) { 10449 *outlenp = 0; 10450 return (ENOBUFS); 10451 } 10452 /* Silently ignore zero */ 10453 if (!checkonly && *i1 != 0) { 10454 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10455 (void) tcp_rwnd_set(tcp, *i1); 10456 } 10457 /* 10458 * XXX should we return the rwnd here 10459 * and tcp_opt_get ? 10460 */ 10461 break; 10462 case SO_SND_COPYAVOID: 10463 if (!checkonly) { 10464 /* we only allow enable at most once for now */ 10465 if (tcp->tcp_loopback || 10466 (tcp->tcp_kssl_ctx != NULL) || 10467 (!tcp->tcp_snd_zcopy_aware && 10468 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10469 *outlenp = 0; 10470 return (EOPNOTSUPP); 10471 } 10472 tcp->tcp_snd_zcopy_aware = 1; 10473 } 10474 break; 10475 case SO_ALLZONES: 10476 /* Pass option along to IP level for handling */ 10477 return (-EINVAL); 10478 case SO_ANON_MLP: 10479 /* Pass option along to IP level for handling */ 10480 return (-EINVAL); 10481 case SO_MAC_EXEMPT: 10482 /* Pass option along to IP level for handling */ 10483 return (-EINVAL); 10484 case SO_EXCLBIND: 10485 if (!checkonly) 10486 tcp->tcp_exclbind = onoff; 10487 break; 10488 default: 10489 *outlenp = 0; 10490 return (EINVAL); 10491 } 10492 break; 10493 case IPPROTO_TCP: 10494 switch (name) { 10495 case TCP_NODELAY: 10496 if (!checkonly) 10497 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10498 break; 10499 case TCP_NOTIFY_THRESHOLD: 10500 if (!checkonly) 10501 tcp->tcp_first_timer_threshold = *i1; 10502 break; 10503 case TCP_ABORT_THRESHOLD: 10504 if (!checkonly) 10505 tcp->tcp_second_timer_threshold = *i1; 10506 break; 10507 case TCP_CONN_NOTIFY_THRESHOLD: 10508 if (!checkonly) 10509 tcp->tcp_first_ctimer_threshold = *i1; 10510 break; 10511 case TCP_CONN_ABORT_THRESHOLD: 10512 if (!checkonly) 10513 tcp->tcp_second_ctimer_threshold = *i1; 10514 break; 10515 case TCP_RECVDSTADDR: 10516 if (tcp->tcp_state > TCPS_LISTEN) 10517 return (EOPNOTSUPP); 10518 if (!checkonly) 10519 tcp->tcp_recvdstaddr = onoff; 10520 break; 10521 case TCP_ANONPRIVBIND: 10522 if ((reterr = secpolicy_net_privaddr(cr, 0, 10523 IPPROTO_TCP)) != 0) { 10524 *outlenp = 0; 10525 return (reterr); 10526 } 10527 if (!checkonly) { 10528 tcp->tcp_anon_priv_bind = onoff; 10529 } 10530 break; 10531 case TCP_EXCLBIND: 10532 if (!checkonly) 10533 tcp->tcp_exclbind = onoff; 10534 break; /* goto sizeof (int) option return */ 10535 case TCP_INIT_CWND: { 10536 uint32_t init_cwnd = *((uint32_t *)invalp); 10537 10538 if (checkonly) 10539 break; 10540 10541 /* 10542 * Only allow socket with network configuration 10543 * privilege to set the initial cwnd to be larger 10544 * than allowed by RFC 3390. 10545 */ 10546 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10547 tcp->tcp_init_cwnd = init_cwnd; 10548 break; 10549 } 10550 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10551 *outlenp = 0; 10552 return (reterr); 10553 } 10554 if (init_cwnd > TCP_MAX_INIT_CWND) { 10555 *outlenp = 0; 10556 return (EINVAL); 10557 } 10558 tcp->tcp_init_cwnd = init_cwnd; 10559 break; 10560 } 10561 case TCP_KEEPALIVE_THRESHOLD: 10562 if (checkonly) 10563 break; 10564 10565 if (*i1 < tcps->tcps_keepalive_interval_low || 10566 *i1 > tcps->tcps_keepalive_interval_high) { 10567 *outlenp = 0; 10568 return (EINVAL); 10569 } 10570 if (*i1 != tcp->tcp_ka_interval) { 10571 tcp->tcp_ka_interval = *i1; 10572 /* 10573 * Check if we need to restart the 10574 * keepalive timer. 10575 */ 10576 if (tcp->tcp_ka_tid != 0) { 10577 ASSERT(tcp->tcp_ka_enabled); 10578 (void) TCP_TIMER_CANCEL(tcp, 10579 tcp->tcp_ka_tid); 10580 tcp->tcp_ka_last_intrvl = 0; 10581 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10582 tcp_keepalive_killer, 10583 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10584 } 10585 } 10586 break; 10587 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10588 if (!checkonly) { 10589 if (*i1 < 10590 tcps->tcps_keepalive_abort_interval_low || 10591 *i1 > 10592 tcps->tcps_keepalive_abort_interval_high) { 10593 *outlenp = 0; 10594 return (EINVAL); 10595 } 10596 tcp->tcp_ka_abort_thres = *i1; 10597 } 10598 break; 10599 case TCP_CORK: 10600 if (!checkonly) { 10601 /* 10602 * if tcp->tcp_cork was set and is now 10603 * being unset, we have to make sure that 10604 * the remaining data gets sent out. Also 10605 * unset tcp->tcp_cork so that tcp_wput_data() 10606 * can send data even if it is less than mss 10607 */ 10608 if (tcp->tcp_cork && onoff == 0 && 10609 tcp->tcp_unsent > 0) { 10610 tcp->tcp_cork = B_FALSE; 10611 tcp_wput_data(tcp, NULL, B_FALSE); 10612 } 10613 tcp->tcp_cork = onoff; 10614 } 10615 break; 10616 default: 10617 *outlenp = 0; 10618 return (EINVAL); 10619 } 10620 break; 10621 case IPPROTO_IP: 10622 if (tcp->tcp_family != AF_INET) { 10623 *outlenp = 0; 10624 return (ENOPROTOOPT); 10625 } 10626 switch (name) { 10627 case IP_OPTIONS: 10628 case T_IP_OPTIONS: 10629 reterr = tcp_opt_set_header(tcp, checkonly, 10630 invalp, inlen); 10631 if (reterr) { 10632 *outlenp = 0; 10633 return (reterr); 10634 } 10635 /* OK return - copy input buffer into output buffer */ 10636 if (invalp != outvalp) { 10637 /* don't trust bcopy for identical src/dst */ 10638 bcopy(invalp, outvalp, inlen); 10639 } 10640 *outlenp = inlen; 10641 return (0); 10642 case IP_TOS: 10643 case T_IP_TOS: 10644 if (!checkonly) { 10645 tcp->tcp_ipha->ipha_type_of_service = 10646 (uchar_t)*i1; 10647 tcp->tcp_tos = (uchar_t)*i1; 10648 } 10649 break; 10650 case IP_TTL: 10651 if (!checkonly) { 10652 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10653 tcp->tcp_ttl = (uchar_t)*i1; 10654 } 10655 break; 10656 case IP_BOUND_IF: 10657 case IP_NEXTHOP: 10658 /* Handled at the IP level */ 10659 return (-EINVAL); 10660 case IP_SEC_OPT: 10661 /* 10662 * We should not allow policy setting after 10663 * we start listening for connections. 10664 */ 10665 if (tcp->tcp_state == TCPS_LISTEN) { 10666 return (EINVAL); 10667 } else { 10668 /* Handled at the IP level */ 10669 return (-EINVAL); 10670 } 10671 default: 10672 *outlenp = 0; 10673 return (EINVAL); 10674 } 10675 break; 10676 case IPPROTO_IPV6: { 10677 ip6_pkt_t *ipp; 10678 10679 /* 10680 * IPPROTO_IPV6 options are only supported for sockets 10681 * that are using IPv6 on the wire. 10682 */ 10683 if (tcp->tcp_ipversion != IPV6_VERSION) { 10684 *outlenp = 0; 10685 return (ENOPROTOOPT); 10686 } 10687 /* 10688 * Only sticky options; no ancillary data 10689 */ 10690 ASSERT(thisdg_attrs == NULL); 10691 ipp = &tcp->tcp_sticky_ipp; 10692 10693 switch (name) { 10694 case IPV6_UNICAST_HOPS: 10695 /* -1 means use default */ 10696 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10697 *outlenp = 0; 10698 return (EINVAL); 10699 } 10700 if (!checkonly) { 10701 if (*i1 == -1) { 10702 tcp->tcp_ip6h->ip6_hops = 10703 ipp->ipp_unicast_hops = 10704 (uint8_t)tcps->tcps_ipv6_hoplimit; 10705 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10706 /* Pass modified value to IP. */ 10707 *i1 = tcp->tcp_ip6h->ip6_hops; 10708 } else { 10709 tcp->tcp_ip6h->ip6_hops = 10710 ipp->ipp_unicast_hops = 10711 (uint8_t)*i1; 10712 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10713 } 10714 reterr = tcp_build_hdrs(q, tcp); 10715 if (reterr != 0) 10716 return (reterr); 10717 } 10718 break; 10719 case IPV6_BOUND_IF: 10720 if (!checkonly) { 10721 int error = 0; 10722 10723 tcp->tcp_bound_if = *i1; 10724 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10725 B_TRUE, checkonly, level, name, mblk); 10726 if (error != 0) { 10727 *outlenp = 0; 10728 return (error); 10729 } 10730 } 10731 break; 10732 /* 10733 * Set boolean switches for ancillary data delivery 10734 */ 10735 case IPV6_RECVPKTINFO: 10736 if (!checkonly) { 10737 if (onoff) 10738 tcp->tcp_ipv6_recvancillary |= 10739 TCP_IPV6_RECVPKTINFO; 10740 else 10741 tcp->tcp_ipv6_recvancillary &= 10742 ~TCP_IPV6_RECVPKTINFO; 10743 /* Force it to be sent up with the next msg */ 10744 tcp->tcp_recvifindex = 0; 10745 } 10746 break; 10747 case IPV6_RECVTCLASS: 10748 if (!checkonly) { 10749 if (onoff) 10750 tcp->tcp_ipv6_recvancillary |= 10751 TCP_IPV6_RECVTCLASS; 10752 else 10753 tcp->tcp_ipv6_recvancillary &= 10754 ~TCP_IPV6_RECVTCLASS; 10755 } 10756 break; 10757 case IPV6_RECVHOPLIMIT: 10758 if (!checkonly) { 10759 if (onoff) 10760 tcp->tcp_ipv6_recvancillary |= 10761 TCP_IPV6_RECVHOPLIMIT; 10762 else 10763 tcp->tcp_ipv6_recvancillary &= 10764 ~TCP_IPV6_RECVHOPLIMIT; 10765 /* Force it to be sent up with the next msg */ 10766 tcp->tcp_recvhops = 0xffffffffU; 10767 } 10768 break; 10769 case IPV6_RECVHOPOPTS: 10770 if (!checkonly) { 10771 if (onoff) 10772 tcp->tcp_ipv6_recvancillary |= 10773 TCP_IPV6_RECVHOPOPTS; 10774 else 10775 tcp->tcp_ipv6_recvancillary &= 10776 ~TCP_IPV6_RECVHOPOPTS; 10777 } 10778 break; 10779 case IPV6_RECVDSTOPTS: 10780 if (!checkonly) { 10781 if (onoff) 10782 tcp->tcp_ipv6_recvancillary |= 10783 TCP_IPV6_RECVDSTOPTS; 10784 else 10785 tcp->tcp_ipv6_recvancillary &= 10786 ~TCP_IPV6_RECVDSTOPTS; 10787 } 10788 break; 10789 case _OLD_IPV6_RECVDSTOPTS: 10790 if (!checkonly) { 10791 if (onoff) 10792 tcp->tcp_ipv6_recvancillary |= 10793 TCP_OLD_IPV6_RECVDSTOPTS; 10794 else 10795 tcp->tcp_ipv6_recvancillary &= 10796 ~TCP_OLD_IPV6_RECVDSTOPTS; 10797 } 10798 break; 10799 case IPV6_RECVRTHDR: 10800 if (!checkonly) { 10801 if (onoff) 10802 tcp->tcp_ipv6_recvancillary |= 10803 TCP_IPV6_RECVRTHDR; 10804 else 10805 tcp->tcp_ipv6_recvancillary &= 10806 ~TCP_IPV6_RECVRTHDR; 10807 } 10808 break; 10809 case IPV6_RECVRTHDRDSTOPTS: 10810 if (!checkonly) { 10811 if (onoff) 10812 tcp->tcp_ipv6_recvancillary |= 10813 TCP_IPV6_RECVRTDSTOPTS; 10814 else 10815 tcp->tcp_ipv6_recvancillary &= 10816 ~TCP_IPV6_RECVRTDSTOPTS; 10817 } 10818 break; 10819 case IPV6_PKTINFO: 10820 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10821 return (EINVAL); 10822 if (checkonly) 10823 break; 10824 10825 if (inlen == 0) { 10826 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10827 } else { 10828 struct in6_pktinfo *pkti; 10829 10830 pkti = (struct in6_pktinfo *)invalp; 10831 /* 10832 * RFC 3542 states that ipi6_addr must be 10833 * the unspecified address when setting the 10834 * IPV6_PKTINFO sticky socket option on a 10835 * TCP socket. 10836 */ 10837 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10838 return (EINVAL); 10839 /* 10840 * ip6_set_pktinfo() validates the source 10841 * address and interface index. 10842 */ 10843 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10844 pkti, mblk); 10845 if (reterr != 0) 10846 return (reterr); 10847 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10848 ipp->ipp_addr = pkti->ipi6_addr; 10849 if (ipp->ipp_ifindex != 0) 10850 ipp->ipp_fields |= IPPF_IFINDEX; 10851 else 10852 ipp->ipp_fields &= ~IPPF_IFINDEX; 10853 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10854 ipp->ipp_fields |= IPPF_ADDR; 10855 else 10856 ipp->ipp_fields &= ~IPPF_ADDR; 10857 } 10858 reterr = tcp_build_hdrs(q, tcp); 10859 if (reterr != 0) 10860 return (reterr); 10861 break; 10862 case IPV6_TCLASS: 10863 if (inlen != 0 && inlen != sizeof (int)) 10864 return (EINVAL); 10865 if (checkonly) 10866 break; 10867 10868 if (inlen == 0) { 10869 ipp->ipp_fields &= ~IPPF_TCLASS; 10870 } else { 10871 if (*i1 > 255 || *i1 < -1) 10872 return (EINVAL); 10873 if (*i1 == -1) { 10874 ipp->ipp_tclass = 0; 10875 *i1 = 0; 10876 } else { 10877 ipp->ipp_tclass = *i1; 10878 } 10879 ipp->ipp_fields |= IPPF_TCLASS; 10880 } 10881 reterr = tcp_build_hdrs(q, tcp); 10882 if (reterr != 0) 10883 return (reterr); 10884 break; 10885 case IPV6_NEXTHOP: 10886 /* 10887 * IP will verify that the nexthop is reachable 10888 * and fail for sticky options. 10889 */ 10890 if (inlen != 0 && inlen != sizeof (sin6_t)) 10891 return (EINVAL); 10892 if (checkonly) 10893 break; 10894 10895 if (inlen == 0) { 10896 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10897 } else { 10898 sin6_t *sin6 = (sin6_t *)invalp; 10899 10900 if (sin6->sin6_family != AF_INET6) 10901 return (EAFNOSUPPORT); 10902 if (IN6_IS_ADDR_V4MAPPED( 10903 &sin6->sin6_addr)) 10904 return (EADDRNOTAVAIL); 10905 ipp->ipp_nexthop = sin6->sin6_addr; 10906 if (!IN6_IS_ADDR_UNSPECIFIED( 10907 &ipp->ipp_nexthop)) 10908 ipp->ipp_fields |= IPPF_NEXTHOP; 10909 else 10910 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10911 } 10912 reterr = tcp_build_hdrs(q, tcp); 10913 if (reterr != 0) 10914 return (reterr); 10915 break; 10916 case IPV6_HOPOPTS: { 10917 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10918 10919 /* 10920 * Sanity checks - minimum size, size a multiple of 10921 * eight bytes, and matching size passed in. 10922 */ 10923 if (inlen != 0 && 10924 inlen != (8 * (hopts->ip6h_len + 1))) 10925 return (EINVAL); 10926 10927 if (checkonly) 10928 break; 10929 10930 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10931 (uchar_t **)&ipp->ipp_hopopts, 10932 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10933 if (reterr != 0) 10934 return (reterr); 10935 if (ipp->ipp_hopoptslen == 0) 10936 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10937 else 10938 ipp->ipp_fields |= IPPF_HOPOPTS; 10939 reterr = tcp_build_hdrs(q, tcp); 10940 if (reterr != 0) 10941 return (reterr); 10942 break; 10943 } 10944 case IPV6_RTHDRDSTOPTS: { 10945 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10946 10947 /* 10948 * Sanity checks - minimum size, size a multiple of 10949 * eight bytes, and matching size passed in. 10950 */ 10951 if (inlen != 0 && 10952 inlen != (8 * (dopts->ip6d_len + 1))) 10953 return (EINVAL); 10954 10955 if (checkonly) 10956 break; 10957 10958 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10959 (uchar_t **)&ipp->ipp_rtdstopts, 10960 &ipp->ipp_rtdstoptslen, 0); 10961 if (reterr != 0) 10962 return (reterr); 10963 if (ipp->ipp_rtdstoptslen == 0) 10964 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10965 else 10966 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10967 reterr = tcp_build_hdrs(q, tcp); 10968 if (reterr != 0) 10969 return (reterr); 10970 break; 10971 } 10972 case IPV6_DSTOPTS: { 10973 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10974 10975 /* 10976 * Sanity checks - minimum size, size a multiple of 10977 * eight bytes, and matching size passed in. 10978 */ 10979 if (inlen != 0 && 10980 inlen != (8 * (dopts->ip6d_len + 1))) 10981 return (EINVAL); 10982 10983 if (checkonly) 10984 break; 10985 10986 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10987 (uchar_t **)&ipp->ipp_dstopts, 10988 &ipp->ipp_dstoptslen, 0); 10989 if (reterr != 0) 10990 return (reterr); 10991 if (ipp->ipp_dstoptslen == 0) 10992 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10993 else 10994 ipp->ipp_fields |= IPPF_DSTOPTS; 10995 reterr = tcp_build_hdrs(q, tcp); 10996 if (reterr != 0) 10997 return (reterr); 10998 break; 10999 } 11000 case IPV6_RTHDR: { 11001 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 11002 11003 /* 11004 * Sanity checks - minimum size, size a multiple of 11005 * eight bytes, and matching size passed in. 11006 */ 11007 if (inlen != 0 && 11008 inlen != (8 * (rt->ip6r_len + 1))) 11009 return (EINVAL); 11010 11011 if (checkonly) 11012 break; 11013 11014 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 11015 (uchar_t **)&ipp->ipp_rthdr, 11016 &ipp->ipp_rthdrlen, 0); 11017 if (reterr != 0) 11018 return (reterr); 11019 if (ipp->ipp_rthdrlen == 0) 11020 ipp->ipp_fields &= ~IPPF_RTHDR; 11021 else 11022 ipp->ipp_fields |= IPPF_RTHDR; 11023 reterr = tcp_build_hdrs(q, tcp); 11024 if (reterr != 0) 11025 return (reterr); 11026 break; 11027 } 11028 case IPV6_V6ONLY: 11029 if (!checkonly) 11030 tcp->tcp_connp->conn_ipv6_v6only = onoff; 11031 break; 11032 case IPV6_USE_MIN_MTU: 11033 if (inlen != sizeof (int)) 11034 return (EINVAL); 11035 11036 if (*i1 < -1 || *i1 > 1) 11037 return (EINVAL); 11038 11039 if (checkonly) 11040 break; 11041 11042 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 11043 ipp->ipp_use_min_mtu = *i1; 11044 break; 11045 case IPV6_BOUND_PIF: 11046 /* Handled at the IP level */ 11047 return (-EINVAL); 11048 case IPV6_SEC_OPT: 11049 /* 11050 * We should not allow policy setting after 11051 * we start listening for connections. 11052 */ 11053 if (tcp->tcp_state == TCPS_LISTEN) { 11054 return (EINVAL); 11055 } else { 11056 /* Handled at the IP level */ 11057 return (-EINVAL); 11058 } 11059 case IPV6_SRC_PREFERENCES: 11060 if (inlen != sizeof (uint32_t)) 11061 return (EINVAL); 11062 reterr = ip6_set_src_preferences(tcp->tcp_connp, 11063 *(uint32_t *)invalp); 11064 if (reterr != 0) { 11065 *outlenp = 0; 11066 return (reterr); 11067 } 11068 break; 11069 default: 11070 *outlenp = 0; 11071 return (EINVAL); 11072 } 11073 break; 11074 } /* end IPPROTO_IPV6 */ 11075 default: 11076 *outlenp = 0; 11077 return (EINVAL); 11078 } 11079 /* 11080 * Common case of OK return with outval same as inval 11081 */ 11082 if (invalp != outvalp) { 11083 /* don't trust bcopy for identical src/dst */ 11084 (void) bcopy(invalp, outvalp, inlen); 11085 } 11086 *outlenp = inlen; 11087 return (0); 11088 } 11089 11090 /* 11091 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 11092 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 11093 * headers, and the maximum size tcp header (to avoid reallocation 11094 * on the fly for additional tcp options). 11095 * Returns failure if can't allocate memory. 11096 */ 11097 static int 11098 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 11099 { 11100 char *hdrs; 11101 uint_t hdrs_len; 11102 ip6i_t *ip6i; 11103 char buf[TCP_MAX_HDR_LENGTH]; 11104 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 11105 in6_addr_t src, dst; 11106 tcp_stack_t *tcps = tcp->tcp_tcps; 11107 11108 /* 11109 * save the existing tcp header and source/dest IP addresses 11110 */ 11111 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 11112 src = tcp->tcp_ip6h->ip6_src; 11113 dst = tcp->tcp_ip6h->ip6_dst; 11114 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 11115 ASSERT(hdrs_len != 0); 11116 if (hdrs_len > tcp->tcp_iphc_len) { 11117 /* Need to reallocate */ 11118 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 11119 if (hdrs == NULL) 11120 return (ENOMEM); 11121 if (tcp->tcp_iphc != NULL) { 11122 if (tcp->tcp_hdr_grown) { 11123 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 11124 } else { 11125 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 11126 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 11127 } 11128 tcp->tcp_iphc_len = 0; 11129 } 11130 ASSERT(tcp->tcp_iphc_len == 0); 11131 tcp->tcp_iphc = hdrs; 11132 tcp->tcp_iphc_len = hdrs_len; 11133 tcp->tcp_hdr_grown = B_TRUE; 11134 } 11135 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 11136 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 11137 11138 /* Set header fields not in ipp */ 11139 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 11140 ip6i = (ip6i_t *)tcp->tcp_iphc; 11141 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 11142 } else { 11143 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 11144 } 11145 /* 11146 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 11147 * 11148 * tcp->tcp_tcp_hdr_len doesn't change here. 11149 */ 11150 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 11151 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 11152 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 11153 11154 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 11155 11156 tcp->tcp_ip6h->ip6_src = src; 11157 tcp->tcp_ip6h->ip6_dst = dst; 11158 11159 /* 11160 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 11161 * the default value for TCP. 11162 */ 11163 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 11164 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 11165 11166 /* 11167 * If we're setting extension headers after a connection 11168 * has been established, and if we have a routing header 11169 * among the extension headers, call ip_massage_options_v6 to 11170 * manipulate the routing header/ip6_dst set the checksum 11171 * difference in the tcp header template. 11172 * (This happens in tcp_connect_ipv6 if the routing header 11173 * is set prior to the connect.) 11174 * Set the tcp_sum to zero first in case we've cleared a 11175 * routing header or don't have one at all. 11176 */ 11177 tcp->tcp_sum = 0; 11178 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 11179 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 11180 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 11181 (uint8_t *)tcp->tcp_tcph); 11182 if (rth != NULL) { 11183 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 11184 rth, tcps->tcps_netstack); 11185 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 11186 (tcp->tcp_sum >> 16)); 11187 } 11188 } 11189 11190 /* Try to get everything in a single mblk */ 11191 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcps->tcps_wroff_xtra); 11192 return (0); 11193 } 11194 11195 /* 11196 * Transfer any source route option from ipha to buf/dst in reversed form. 11197 */ 11198 static int 11199 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 11200 { 11201 ipoptp_t opts; 11202 uchar_t *opt; 11203 uint8_t optval; 11204 uint8_t optlen; 11205 uint32_t len = 0; 11206 11207 for (optval = ipoptp_first(&opts, ipha); 11208 optval != IPOPT_EOL; 11209 optval = ipoptp_next(&opts)) { 11210 opt = opts.ipoptp_cur; 11211 optlen = opts.ipoptp_len; 11212 switch (optval) { 11213 int off1, off2; 11214 case IPOPT_SSRR: 11215 case IPOPT_LSRR: 11216 11217 /* Reverse source route */ 11218 /* 11219 * First entry should be the next to last one in the 11220 * current source route (the last entry is our 11221 * address.) 11222 * The last entry should be the final destination. 11223 */ 11224 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11225 buf[IPOPT_OLEN] = (uint8_t)optlen; 11226 off1 = IPOPT_MINOFF_SR - 1; 11227 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11228 if (off2 < 0) { 11229 /* No entries in source route */ 11230 break; 11231 } 11232 bcopy(opt + off2, dst, IP_ADDR_LEN); 11233 /* 11234 * Note: use src since ipha has not had its src 11235 * and dst reversed (it is in the state it was 11236 * received. 11237 */ 11238 bcopy(&ipha->ipha_src, buf + off2, 11239 IP_ADDR_LEN); 11240 off2 -= IP_ADDR_LEN; 11241 11242 while (off2 > 0) { 11243 bcopy(opt + off2, buf + off1, 11244 IP_ADDR_LEN); 11245 off1 += IP_ADDR_LEN; 11246 off2 -= IP_ADDR_LEN; 11247 } 11248 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11249 buf += optlen; 11250 len += optlen; 11251 break; 11252 } 11253 } 11254 done: 11255 /* Pad the resulting options */ 11256 while (len & 0x3) { 11257 *buf++ = IPOPT_EOL; 11258 len++; 11259 } 11260 return (len); 11261 } 11262 11263 11264 /* 11265 * Extract and revert a source route from ipha (if any) 11266 * and then update the relevant fields in both tcp_t and the standard header. 11267 */ 11268 static void 11269 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11270 { 11271 char buf[TCP_MAX_HDR_LENGTH]; 11272 uint_t tcph_len; 11273 int len; 11274 11275 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11276 len = IPH_HDR_LENGTH(ipha); 11277 if (len == IP_SIMPLE_HDR_LENGTH) 11278 /* Nothing to do */ 11279 return; 11280 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11281 (len & 0x3)) 11282 return; 11283 11284 tcph_len = tcp->tcp_tcp_hdr_len; 11285 bcopy(tcp->tcp_tcph, buf, tcph_len); 11286 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11287 (tcp->tcp_ipha->ipha_dst & 0xffff); 11288 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11289 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11290 len += IP_SIMPLE_HDR_LENGTH; 11291 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11292 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11293 if ((int)tcp->tcp_sum < 0) 11294 tcp->tcp_sum--; 11295 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11296 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11297 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11298 bcopy(buf, tcp->tcp_tcph, tcph_len); 11299 tcp->tcp_ip_hdr_len = len; 11300 tcp->tcp_ipha->ipha_version_and_hdr_length = 11301 (IP_VERSION << 4) | (len >> 2); 11302 len += tcph_len; 11303 tcp->tcp_hdr_len = len; 11304 } 11305 11306 /* 11307 * Copy the standard header into its new location, 11308 * lay in the new options and then update the relevant 11309 * fields in both tcp_t and the standard header. 11310 */ 11311 static int 11312 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11313 { 11314 uint_t tcph_len; 11315 uint8_t *ip_optp; 11316 tcph_t *new_tcph; 11317 tcp_stack_t *tcps = tcp->tcp_tcps; 11318 11319 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11320 return (EINVAL); 11321 11322 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11323 return (EINVAL); 11324 11325 if (checkonly) { 11326 /* 11327 * do not really set, just pretend to - T_CHECK 11328 */ 11329 return (0); 11330 } 11331 11332 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11333 if (tcp->tcp_label_len > 0) { 11334 int padlen; 11335 uint8_t opt; 11336 11337 /* convert list termination to no-ops */ 11338 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11339 ip_optp += ip_optp[IPOPT_OLEN]; 11340 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11341 while (--padlen >= 0) 11342 *ip_optp++ = opt; 11343 } 11344 tcph_len = tcp->tcp_tcp_hdr_len; 11345 new_tcph = (tcph_t *)(ip_optp + len); 11346 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11347 tcp->tcp_tcph = new_tcph; 11348 bcopy(ptr, ip_optp, len); 11349 11350 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11351 11352 tcp->tcp_ip_hdr_len = len; 11353 tcp->tcp_ipha->ipha_version_and_hdr_length = 11354 (IP_VERSION << 4) | (len >> 2); 11355 tcp->tcp_hdr_len = len + tcph_len; 11356 if (!TCP_IS_DETACHED(tcp)) { 11357 /* Always allocate room for all options. */ 11358 (void) mi_set_sth_wroff(tcp->tcp_rq, 11359 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11360 } 11361 return (0); 11362 } 11363 11364 /* Get callback routine passed to nd_load by tcp_param_register */ 11365 /* ARGSUSED */ 11366 static int 11367 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11368 { 11369 tcpparam_t *tcppa = (tcpparam_t *)cp; 11370 11371 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11372 return (0); 11373 } 11374 11375 /* 11376 * Walk through the param array specified registering each element with the 11377 * named dispatch handler. 11378 */ 11379 static boolean_t 11380 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11381 { 11382 for (; cnt-- > 0; tcppa++) { 11383 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11384 if (!nd_load(ndp, tcppa->tcp_param_name, 11385 tcp_param_get, tcp_param_set, 11386 (caddr_t)tcppa)) { 11387 nd_free(ndp); 11388 return (B_FALSE); 11389 } 11390 } 11391 } 11392 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11393 KM_SLEEP); 11394 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11395 sizeof (tcpparam_t)); 11396 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11397 tcp_param_get, tcp_param_set_aligned, 11398 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11399 nd_free(ndp); 11400 return (B_FALSE); 11401 } 11402 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11403 KM_SLEEP); 11404 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11405 sizeof (tcpparam_t)); 11406 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11407 tcp_param_get, tcp_param_set_aligned, 11408 (caddr_t)tcps->tcps_mdt_head_param)) { 11409 nd_free(ndp); 11410 return (B_FALSE); 11411 } 11412 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11413 KM_SLEEP); 11414 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11415 sizeof (tcpparam_t)); 11416 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11417 tcp_param_get, tcp_param_set_aligned, 11418 (caddr_t)tcps->tcps_mdt_tail_param)) { 11419 nd_free(ndp); 11420 return (B_FALSE); 11421 } 11422 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11423 KM_SLEEP); 11424 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11425 sizeof (tcpparam_t)); 11426 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11427 tcp_param_get, tcp_param_set_aligned, 11428 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11429 nd_free(ndp); 11430 return (B_FALSE); 11431 } 11432 if (!nd_load(ndp, "tcp_extra_priv_ports", 11433 tcp_extra_priv_ports_get, NULL, NULL)) { 11434 nd_free(ndp); 11435 return (B_FALSE); 11436 } 11437 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11438 NULL, tcp_extra_priv_ports_add, NULL)) { 11439 nd_free(ndp); 11440 return (B_FALSE); 11441 } 11442 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11443 NULL, tcp_extra_priv_ports_del, NULL)) { 11444 nd_free(ndp); 11445 return (B_FALSE); 11446 } 11447 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11448 NULL)) { 11449 nd_free(ndp); 11450 return (B_FALSE); 11451 } 11452 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11453 NULL, NULL)) { 11454 nd_free(ndp); 11455 return (B_FALSE); 11456 } 11457 if (!nd_load(ndp, "tcp_listen_hash", 11458 tcp_listen_hash_report, NULL, NULL)) { 11459 nd_free(ndp); 11460 return (B_FALSE); 11461 } 11462 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11463 NULL, NULL)) { 11464 nd_free(ndp); 11465 return (B_FALSE); 11466 } 11467 if (!nd_load(ndp, "tcp_acceptor_hash", 11468 tcp_acceptor_hash_report, NULL, NULL)) { 11469 nd_free(ndp); 11470 return (B_FALSE); 11471 } 11472 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11473 tcp_1948_phrase_set, NULL)) { 11474 nd_free(ndp); 11475 return (B_FALSE); 11476 } 11477 /* 11478 * Dummy ndd variables - only to convey obsolescence information 11479 * through printing of their name (no get or set routines) 11480 * XXX Remove in future releases ? 11481 */ 11482 if (!nd_load(ndp, 11483 "tcp_close_wait_interval(obsoleted - " 11484 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11485 nd_free(ndp); 11486 return (B_FALSE); 11487 } 11488 return (B_TRUE); 11489 } 11490 11491 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11492 /* ARGSUSED */ 11493 static int 11494 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11495 cred_t *cr) 11496 { 11497 long new_value; 11498 tcpparam_t *tcppa = (tcpparam_t *)cp; 11499 11500 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11501 new_value < tcppa->tcp_param_min || 11502 new_value > tcppa->tcp_param_max) { 11503 return (EINVAL); 11504 } 11505 /* 11506 * Need to make sure new_value is a multiple of 4. If it is not, 11507 * round it up. For future 64 bit requirement, we actually make it 11508 * a multiple of 8. 11509 */ 11510 if (new_value & 0x7) { 11511 new_value = (new_value & ~0x7) + 0x8; 11512 } 11513 tcppa->tcp_param_val = new_value; 11514 return (0); 11515 } 11516 11517 /* Set callback routine passed to nd_load by tcp_param_register */ 11518 /* ARGSUSED */ 11519 static int 11520 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11521 { 11522 long new_value; 11523 tcpparam_t *tcppa = (tcpparam_t *)cp; 11524 11525 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11526 new_value < tcppa->tcp_param_min || 11527 new_value > tcppa->tcp_param_max) { 11528 return (EINVAL); 11529 } 11530 tcppa->tcp_param_val = new_value; 11531 return (0); 11532 } 11533 11534 /* 11535 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11536 * is filled, return as much as we can. The message passed in may be 11537 * multi-part, chained using b_cont. "start" is the starting sequence 11538 * number for this piece. 11539 */ 11540 static mblk_t * 11541 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11542 { 11543 uint32_t end; 11544 mblk_t *mp1; 11545 mblk_t *mp2; 11546 mblk_t *next_mp; 11547 uint32_t u1; 11548 tcp_stack_t *tcps = tcp->tcp_tcps; 11549 11550 /* Walk through all the new pieces. */ 11551 do { 11552 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11553 (uintptr_t)INT_MAX); 11554 end = start + (int)(mp->b_wptr - mp->b_rptr); 11555 next_mp = mp->b_cont; 11556 if (start == end) { 11557 /* Empty. Blast it. */ 11558 freeb(mp); 11559 continue; 11560 } 11561 mp->b_cont = NULL; 11562 TCP_REASS_SET_SEQ(mp, start); 11563 TCP_REASS_SET_END(mp, end); 11564 mp1 = tcp->tcp_reass_tail; 11565 if (!mp1) { 11566 tcp->tcp_reass_tail = mp; 11567 tcp->tcp_reass_head = mp; 11568 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11569 UPDATE_MIB(&tcps->tcps_mib, 11570 tcpInDataUnorderBytes, end - start); 11571 continue; 11572 } 11573 /* New stuff completely beyond tail? */ 11574 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11575 /* Link it on end. */ 11576 mp1->b_cont = mp; 11577 tcp->tcp_reass_tail = mp; 11578 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11579 UPDATE_MIB(&tcps->tcps_mib, 11580 tcpInDataUnorderBytes, end - start); 11581 continue; 11582 } 11583 mp1 = tcp->tcp_reass_head; 11584 u1 = TCP_REASS_SEQ(mp1); 11585 /* New stuff at the front? */ 11586 if (SEQ_LT(start, u1)) { 11587 /* Yes... Check for overlap. */ 11588 mp->b_cont = mp1; 11589 tcp->tcp_reass_head = mp; 11590 tcp_reass_elim_overlap(tcp, mp); 11591 continue; 11592 } 11593 /* 11594 * The new piece fits somewhere between the head and tail. 11595 * We find our slot, where mp1 precedes us and mp2 trails. 11596 */ 11597 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11598 u1 = TCP_REASS_SEQ(mp2); 11599 if (SEQ_LEQ(start, u1)) 11600 break; 11601 } 11602 /* Link ourselves in */ 11603 mp->b_cont = mp2; 11604 mp1->b_cont = mp; 11605 11606 /* Trim overlap with following mblk(s) first */ 11607 tcp_reass_elim_overlap(tcp, mp); 11608 11609 /* Trim overlap with preceding mblk */ 11610 tcp_reass_elim_overlap(tcp, mp1); 11611 11612 } while (start = end, mp = next_mp); 11613 mp1 = tcp->tcp_reass_head; 11614 /* Anything ready to go? */ 11615 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11616 return (NULL); 11617 /* Eat what we can off the queue */ 11618 for (;;) { 11619 mp = mp1->b_cont; 11620 end = TCP_REASS_END(mp1); 11621 TCP_REASS_SET_SEQ(mp1, 0); 11622 TCP_REASS_SET_END(mp1, 0); 11623 if (!mp) { 11624 tcp->tcp_reass_tail = NULL; 11625 break; 11626 } 11627 if (end != TCP_REASS_SEQ(mp)) { 11628 mp1->b_cont = NULL; 11629 break; 11630 } 11631 mp1 = mp; 11632 } 11633 mp1 = tcp->tcp_reass_head; 11634 tcp->tcp_reass_head = mp; 11635 return (mp1); 11636 } 11637 11638 /* Eliminate any overlap that mp may have over later mblks */ 11639 static void 11640 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11641 { 11642 uint32_t end; 11643 mblk_t *mp1; 11644 uint32_t u1; 11645 tcp_stack_t *tcps = tcp->tcp_tcps; 11646 11647 end = TCP_REASS_END(mp); 11648 while ((mp1 = mp->b_cont) != NULL) { 11649 u1 = TCP_REASS_SEQ(mp1); 11650 if (!SEQ_GT(end, u1)) 11651 break; 11652 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11653 mp->b_wptr -= end - u1; 11654 TCP_REASS_SET_END(mp, u1); 11655 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11656 UPDATE_MIB(&tcps->tcps_mib, 11657 tcpInDataPartDupBytes, end - u1); 11658 break; 11659 } 11660 mp->b_cont = mp1->b_cont; 11661 TCP_REASS_SET_SEQ(mp1, 0); 11662 TCP_REASS_SET_END(mp1, 0); 11663 freeb(mp1); 11664 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11665 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11666 } 11667 if (!mp1) 11668 tcp->tcp_reass_tail = mp; 11669 } 11670 11671 /* 11672 * Send up all messages queued on tcp_rcv_list. 11673 */ 11674 static uint_t 11675 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11676 { 11677 mblk_t *mp; 11678 uint_t ret = 0; 11679 uint_t thwin; 11680 #ifdef DEBUG 11681 uint_t cnt = 0; 11682 #endif 11683 tcp_stack_t *tcps = tcp->tcp_tcps; 11684 11685 /* Can't drain on an eager connection */ 11686 if (tcp->tcp_listener != NULL) 11687 return (ret); 11688 11689 /* Can't be sodirect enabled */ 11690 ASSERT(SOD_NOT_ENABLED(tcp)); 11691 11692 /* No need for the push timer now. */ 11693 if (tcp->tcp_push_tid != 0) { 11694 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11695 tcp->tcp_push_tid = 0; 11696 } 11697 11698 /* 11699 * Handle two cases here: we are currently fused or we were 11700 * previously fused and have some urgent data to be delivered 11701 * upstream. The latter happens because we either ran out of 11702 * memory or were detached and therefore sending the SIGURG was 11703 * deferred until this point. In either case we pass control 11704 * over to tcp_fuse_rcv_drain() since it may need to complete 11705 * some work. 11706 */ 11707 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11708 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11709 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11710 &tcp->tcp_fused_sigurg_mp)) 11711 return (ret); 11712 } 11713 11714 while ((mp = tcp->tcp_rcv_list) != NULL) { 11715 tcp->tcp_rcv_list = mp->b_next; 11716 mp->b_next = NULL; 11717 #ifdef DEBUG 11718 cnt += msgdsize(mp); 11719 #endif 11720 /* Does this need SSL processing first? */ 11721 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11722 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11723 mblk_t *, mp); 11724 tcp_kssl_input(tcp, mp); 11725 continue; 11726 } 11727 putnext(q, mp); 11728 } 11729 ASSERT(cnt == tcp->tcp_rcv_cnt); 11730 tcp->tcp_rcv_last_head = NULL; 11731 tcp->tcp_rcv_last_tail = NULL; 11732 tcp->tcp_rcv_cnt = 0; 11733 11734 /* Learn the latest rwnd information that we sent to the other side. */ 11735 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11736 << tcp->tcp_rcv_ws; 11737 /* This is peer's calculated send window (our receive window). */ 11738 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11739 /* 11740 * Increase the receive window to max. But we need to do receiver 11741 * SWS avoidance. This means that we need to check the increase of 11742 * of receive window is at least 1 MSS. 11743 */ 11744 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11745 /* 11746 * If the window that the other side knows is less than max 11747 * deferred acks segments, send an update immediately. 11748 */ 11749 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11750 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11751 ret = TH_ACK_NEEDED; 11752 } 11753 tcp->tcp_rwnd = q->q_hiwat; 11754 } 11755 return (ret); 11756 } 11757 11758 /* 11759 * Queue data on tcp_rcv_list which is a b_next chain. 11760 * tcp_rcv_last_head/tail is the last element of this chain. 11761 * Each element of the chain is a b_cont chain. 11762 * 11763 * M_DATA messages are added to the current element. 11764 * Other messages are added as new (b_next) elements. 11765 */ 11766 void 11767 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11768 { 11769 ASSERT(seg_len == msgdsize(mp)); 11770 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11771 11772 if (tcp->tcp_rcv_list == NULL) { 11773 ASSERT(tcp->tcp_rcv_last_head == NULL); 11774 tcp->tcp_rcv_list = mp; 11775 tcp->tcp_rcv_last_head = mp; 11776 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11777 tcp->tcp_rcv_last_tail->b_cont = mp; 11778 } else { 11779 tcp->tcp_rcv_last_head->b_next = mp; 11780 tcp->tcp_rcv_last_head = mp; 11781 } 11782 11783 while (mp->b_cont) 11784 mp = mp->b_cont; 11785 11786 tcp->tcp_rcv_last_tail = mp; 11787 tcp->tcp_rcv_cnt += seg_len; 11788 tcp->tcp_rwnd -= seg_len; 11789 } 11790 11791 /* 11792 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11793 * above, in addition when uioa is enabled schedule an asynchronous uio 11794 * prior to enqueuing. They implement the combinhed semantics of the 11795 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11796 * canputnext(), i.e. flow-control with backenable. 11797 * 11798 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11799 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11800 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11801 * 11802 * Must be called with sodp->sod_lockp held and will return with the lock 11803 * released. 11804 */ 11805 static uint_t 11806 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11807 { 11808 queue_t *q = tcp->tcp_rq; 11809 uint_t thwin; 11810 tcp_stack_t *tcps = tcp->tcp_tcps; 11811 uint_t ret = 0; 11812 11813 /* Can't be an eager connection */ 11814 ASSERT(tcp->tcp_listener == NULL); 11815 11816 /* Caller must have lock held */ 11817 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11818 11819 /* Sodirect mode so must not be a tcp_rcv_list */ 11820 ASSERT(tcp->tcp_rcv_list == NULL); 11821 11822 if (SOD_QFULL(sodp)) { 11823 /* Q is full, mark Q for need backenable */ 11824 SOD_QSETBE(sodp); 11825 } 11826 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11827 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11828 << tcp->tcp_rcv_ws; 11829 /* This is peer's calculated send window (our available rwnd). */ 11830 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11831 /* 11832 * Increase the receive window to max. But we need to do receiver 11833 * SWS avoidance. This means that we need to check the increase of 11834 * of receive window is at least 1 MSS. 11835 */ 11836 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11837 /* 11838 * If the window that the other side knows is less than max 11839 * deferred acks segments, send an update immediately. 11840 */ 11841 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11842 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11843 ret = TH_ACK_NEEDED; 11844 } 11845 tcp->tcp_rwnd = q->q_hiwat; 11846 } 11847 11848 if (!SOD_QEMPTY(sodp)) { 11849 /* Wakeup to socket */ 11850 sodp->sod_state &= SOD_WAKE_CLR; 11851 sodp->sod_state |= SOD_WAKE_DONE; 11852 (sodp->sod_wakeup)(sodp); 11853 /* wakeup() does the mutex_ext() */ 11854 } else { 11855 /* Q is empty, no need to wake */ 11856 sodp->sod_state &= SOD_WAKE_CLR; 11857 sodp->sod_state |= SOD_WAKE_NOT; 11858 mutex_exit(sodp->sod_lockp); 11859 } 11860 11861 /* No need for the push timer now. */ 11862 if (tcp->tcp_push_tid != 0) { 11863 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11864 tcp->tcp_push_tid = 0; 11865 } 11866 11867 return (ret); 11868 } 11869 11870 /* 11871 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11872 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11873 * to the user-land buffer and flag the mblk_t as such. 11874 * 11875 * Also, handle tcp_rwnd. 11876 */ 11877 uint_t 11878 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11879 { 11880 uioa_t *uioap = &sodp->sod_uioa; 11881 boolean_t qfull; 11882 uint_t thwin; 11883 11884 /* Can't be an eager connection */ 11885 ASSERT(tcp->tcp_listener == NULL); 11886 11887 /* Caller must have lock held */ 11888 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11889 11890 /* Sodirect mode so must not be a tcp_rcv_list */ 11891 ASSERT(tcp->tcp_rcv_list == NULL); 11892 11893 /* Passed in segment length must be equal to mblk_t chain data size */ 11894 ASSERT(seg_len == msgdsize(mp)); 11895 11896 if (DB_TYPE(mp) != M_DATA) { 11897 /* Only process M_DATA mblk_t's */ 11898 goto enq; 11899 } 11900 if (uioap->uioa_state & UIOA_ENABLED) { 11901 /* Uioa is enabled */ 11902 mblk_t *mp1 = mp; 11903 mblk_t *lmp = NULL; 11904 11905 if (seg_len > uioap->uio_resid) { 11906 /* 11907 * There isn't enough uio space for the mblk_t chain 11908 * so disable uioa such that this and any additional 11909 * mblk_t data is handled by the socket and schedule 11910 * the socket for wakeup to finish this uioa. 11911 */ 11912 uioap->uioa_state &= UIOA_CLR; 11913 uioap->uioa_state |= UIOA_FINI; 11914 if (sodp->sod_state & SOD_WAKE_NOT) { 11915 sodp->sod_state &= SOD_WAKE_CLR; 11916 sodp->sod_state |= SOD_WAKE_NEED; 11917 } 11918 goto enq; 11919 } 11920 do { 11921 uint32_t len = MBLKL(mp1); 11922 11923 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11924 /* Scheduled, mark dblk_t as such */ 11925 DB_FLAGS(mp1) |= DBLK_UIOA; 11926 } else { 11927 /* Error, turn off async processing */ 11928 uioap->uioa_state &= UIOA_CLR; 11929 uioap->uioa_state |= UIOA_FINI; 11930 break; 11931 } 11932 lmp = mp1; 11933 } while ((mp1 = mp1->b_cont) != NULL); 11934 11935 if (mp1 != NULL || uioap->uio_resid == 0) { 11936 /* 11937 * Not all mblk_t(s) uioamoved (error) or all uio 11938 * space has been consumed so schedule the socket 11939 * for wakeup to finish this uio. 11940 */ 11941 sodp->sod_state &= SOD_WAKE_CLR; 11942 sodp->sod_state |= SOD_WAKE_NEED; 11943 11944 /* Break the mblk chain if neccessary. */ 11945 if (mp1 != NULL && lmp != NULL) { 11946 mp->b_next = mp1; 11947 lmp->b_cont = NULL; 11948 } 11949 } 11950 } else if (uioap->uioa_state & UIOA_FINI) { 11951 /* 11952 * Post UIO_ENABLED waiting for socket to finish processing 11953 * so just enqueue and update tcp_rwnd. 11954 */ 11955 if (SOD_QFULL(sodp)) 11956 tcp->tcp_rwnd -= seg_len; 11957 } else if (sodp->sod_want > 0) { 11958 /* 11959 * Uioa isn't enabled but sodirect has a pending read(). 11960 */ 11961 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11962 if (sodp->sod_state & SOD_WAKE_NOT) { 11963 /* Schedule socket for wakeup */ 11964 sodp->sod_state &= SOD_WAKE_CLR; 11965 sodp->sod_state |= SOD_WAKE_NEED; 11966 } 11967 tcp->tcp_rwnd -= seg_len; 11968 } 11969 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11970 /* 11971 * No pending sodirect read() so used the default 11972 * TCP push logic to guess that a push is needed. 11973 */ 11974 if (sodp->sod_state & SOD_WAKE_NOT) { 11975 /* Schedule socket for wakeup */ 11976 sodp->sod_state &= SOD_WAKE_CLR; 11977 sodp->sod_state |= SOD_WAKE_NEED; 11978 } 11979 tcp->tcp_rwnd -= seg_len; 11980 } else { 11981 /* Just update tcp_rwnd */ 11982 tcp->tcp_rwnd -= seg_len; 11983 } 11984 enq: 11985 qfull = SOD_QFULL(sodp); 11986 11987 (sodp->sod_enqueue)(sodp, mp); 11988 11989 if (! qfull && SOD_QFULL(sodp)) { 11990 /* Wasn't QFULL, now QFULL, need back-enable */ 11991 SOD_QSETBE(sodp); 11992 } 11993 11994 /* 11995 * Check to see if remote avail swnd < mss due to delayed ACK, 11996 * first get advertised rwnd. 11997 */ 11998 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11999 /* Minus delayed ACK count */ 12000 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 12001 if (thwin < tcp->tcp_mss) { 12002 /* Remote avail swnd < mss, need ACK now */ 12003 return (TH_ACK_NEEDED); 12004 } 12005 12006 return (0); 12007 } 12008 12009 /* 12010 * DEFAULT TCP ENTRY POINT via squeue on READ side. 12011 * 12012 * This is the default entry function into TCP on the read side. TCP is 12013 * always entered via squeue i.e. using squeue's for mutual exclusion. 12014 * When classifier does a lookup to find the tcp, it also puts a reference 12015 * on the conn structure associated so the tcp is guaranteed to exist 12016 * when we come here. We still need to check the state because it might 12017 * as well has been closed. The squeue processing function i.e. squeue_enter, 12018 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 12019 * CONN_DEC_REF. 12020 * 12021 * Apart from the default entry point, IP also sends packets directly to 12022 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 12023 * connections. 12024 */ 12025 void 12026 tcp_input(void *arg, mblk_t *mp, void *arg2) 12027 { 12028 conn_t *connp = (conn_t *)arg; 12029 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 12030 12031 /* arg2 is the sqp */ 12032 ASSERT(arg2 != NULL); 12033 ASSERT(mp != NULL); 12034 12035 /* 12036 * Don't accept any input on a closed tcp as this TCP logically does 12037 * not exist on the system. Don't proceed further with this TCP. 12038 * For eg. this packet could trigger another close of this tcp 12039 * which would be disastrous for tcp_refcnt. tcp_close_detached / 12040 * tcp_clean_death / tcp_closei_local must be called at most once 12041 * on a TCP. In this case we need to refeed the packet into the 12042 * classifier and figure out where the packet should go. Need to 12043 * preserve the recv_ill somehow. Until we figure that out, for 12044 * now just drop the packet if we can't classify the packet. 12045 */ 12046 if (tcp->tcp_state == TCPS_CLOSED || 12047 tcp->tcp_state == TCPS_BOUND) { 12048 conn_t *new_connp; 12049 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 12050 12051 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 12052 if (new_connp != NULL) { 12053 tcp_reinput(new_connp, mp, arg2); 12054 return; 12055 } 12056 /* We failed to classify. For now just drop the packet */ 12057 freemsg(mp); 12058 return; 12059 } 12060 12061 if (DB_TYPE(mp) == M_DATA) 12062 tcp_rput_data(connp, mp, arg2); 12063 else 12064 tcp_rput_common(tcp, mp); 12065 } 12066 12067 /* 12068 * The read side put procedure. 12069 * The packets passed up by ip are assume to be aligned according to 12070 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 12071 */ 12072 static void 12073 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 12074 { 12075 /* 12076 * tcp_rput_data() does not expect M_CTL except for the case 12077 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 12078 * type. Need to make sure that any other M_CTLs don't make 12079 * it to tcp_rput_data since it is not expecting any and doesn't 12080 * check for it. 12081 */ 12082 if (DB_TYPE(mp) == M_CTL) { 12083 switch (*(uint32_t *)(mp->b_rptr)) { 12084 case TCP_IOC_ABORT_CONN: 12085 /* 12086 * Handle connection abort request. 12087 */ 12088 tcp_ioctl_abort_handler(tcp, mp); 12089 return; 12090 case IPSEC_IN: 12091 /* 12092 * Only secure icmp arrive in TCP and they 12093 * don't go through data path. 12094 */ 12095 tcp_icmp_error(tcp, mp); 12096 return; 12097 case IN_PKTINFO: 12098 /* 12099 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 12100 * sockets that are receiving IPv4 traffic. tcp 12101 */ 12102 ASSERT(tcp->tcp_family == AF_INET6); 12103 ASSERT(tcp->tcp_ipv6_recvancillary & 12104 TCP_IPV6_RECVPKTINFO); 12105 tcp_rput_data(tcp->tcp_connp, mp, 12106 tcp->tcp_connp->conn_sqp); 12107 return; 12108 case MDT_IOC_INFO_UPDATE: 12109 /* 12110 * Handle Multidata information update; the 12111 * following routine will free the message. 12112 */ 12113 if (tcp->tcp_connp->conn_mdt_ok) { 12114 tcp_mdt_update(tcp, 12115 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 12116 B_FALSE); 12117 } 12118 freemsg(mp); 12119 return; 12120 case LSO_IOC_INFO_UPDATE: 12121 /* 12122 * Handle LSO information update; the following 12123 * routine will free the message. 12124 */ 12125 if (tcp->tcp_connp->conn_lso_ok) { 12126 tcp_lso_update(tcp, 12127 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 12128 } 12129 freemsg(mp); 12130 return; 12131 default: 12132 /* 12133 * tcp_icmp_err() will process the M_CTL packets. 12134 * Non-ICMP packets, if any, will be discarded in 12135 * tcp_icmp_err(). We will process the ICMP packet 12136 * even if we are TCP_IS_DETACHED_NONEAGER as the 12137 * incoming ICMP packet may result in changing 12138 * the tcp_mss, which we would need if we have 12139 * packets to retransmit. 12140 */ 12141 tcp_icmp_error(tcp, mp); 12142 return; 12143 } 12144 } 12145 12146 /* No point processing the message if tcp is already closed */ 12147 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 12148 freemsg(mp); 12149 return; 12150 } 12151 12152 tcp_rput_other(tcp, mp); 12153 } 12154 12155 12156 /* The minimum of smoothed mean deviation in RTO calculation. */ 12157 #define TCP_SD_MIN 400 12158 12159 /* 12160 * Set RTO for this connection. The formula is from Jacobson and Karels' 12161 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 12162 * are the same as those in Appendix A.2 of that paper. 12163 * 12164 * m = new measurement 12165 * sa = smoothed RTT average (8 * average estimates). 12166 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 12167 */ 12168 static void 12169 tcp_set_rto(tcp_t *tcp, clock_t rtt) 12170 { 12171 long m = TICK_TO_MSEC(rtt); 12172 clock_t sa = tcp->tcp_rtt_sa; 12173 clock_t sv = tcp->tcp_rtt_sd; 12174 clock_t rto; 12175 tcp_stack_t *tcps = tcp->tcp_tcps; 12176 12177 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 12178 tcp->tcp_rtt_update++; 12179 12180 /* tcp_rtt_sa is not 0 means this is a new sample. */ 12181 if (sa != 0) { 12182 /* 12183 * Update average estimator: 12184 * new rtt = 7/8 old rtt + 1/8 Error 12185 */ 12186 12187 /* m is now Error in estimate. */ 12188 m -= sa >> 3; 12189 if ((sa += m) <= 0) { 12190 /* 12191 * Don't allow the smoothed average to be negative. 12192 * We use 0 to denote reinitialization of the 12193 * variables. 12194 */ 12195 sa = 1; 12196 } 12197 12198 /* 12199 * Update deviation estimator: 12200 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12201 */ 12202 if (m < 0) 12203 m = -m; 12204 m -= sv >> 2; 12205 sv += m; 12206 } else { 12207 /* 12208 * This follows BSD's implementation. So the reinitialized 12209 * RTO is 3 * m. We cannot go less than 2 because if the 12210 * link is bandwidth dominated, doubling the window size 12211 * during slow start means doubling the RTT. We want to be 12212 * more conservative when we reinitialize our estimates. 3 12213 * is just a convenient number. 12214 */ 12215 sa = m << 3; 12216 sv = m << 1; 12217 } 12218 if (sv < TCP_SD_MIN) { 12219 /* 12220 * We do not know that if sa captures the delay ACK 12221 * effect as in a long train of segments, a receiver 12222 * does not delay its ACKs. So set the minimum of sv 12223 * to be TCP_SD_MIN, which is default to 400 ms, twice 12224 * of BSD DATO. That means the minimum of mean 12225 * deviation is 100 ms. 12226 * 12227 */ 12228 sv = TCP_SD_MIN; 12229 } 12230 tcp->tcp_rtt_sa = sa; 12231 tcp->tcp_rtt_sd = sv; 12232 /* 12233 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12234 * 12235 * Add tcp_rexmit_interval extra in case of extreme environment 12236 * where the algorithm fails to work. The default value of 12237 * tcp_rexmit_interval_extra should be 0. 12238 * 12239 * As we use a finer grained clock than BSD and update 12240 * RTO for every ACKs, add in another .25 of RTT to the 12241 * deviation of RTO to accomodate burstiness of 1/4 of 12242 * window size. 12243 */ 12244 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12245 12246 if (rto > tcps->tcps_rexmit_interval_max) { 12247 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12248 } else if (rto < tcps->tcps_rexmit_interval_min) { 12249 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12250 } else { 12251 tcp->tcp_rto = rto; 12252 } 12253 12254 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12255 tcp->tcp_timer_backoff = 0; 12256 } 12257 12258 /* 12259 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12260 * send queue which starts at the given seq. no. 12261 * 12262 * Parameters: 12263 * tcp_t *tcp: the tcp instance pointer. 12264 * uint32_t seq: the starting seq. no of the requested segment. 12265 * int32_t *off: after the execution, *off will be the offset to 12266 * the returned mblk which points to the requested seq no. 12267 * It is the caller's responsibility to send in a non-null off. 12268 * 12269 * Return: 12270 * A mblk_t pointer pointing to the requested segment in send queue. 12271 */ 12272 static mblk_t * 12273 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12274 { 12275 int32_t cnt; 12276 mblk_t *mp; 12277 12278 /* Defensive coding. Make sure we don't send incorrect data. */ 12279 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12280 return (NULL); 12281 12282 cnt = seq - tcp->tcp_suna; 12283 mp = tcp->tcp_xmit_head; 12284 while (cnt > 0 && mp != NULL) { 12285 cnt -= mp->b_wptr - mp->b_rptr; 12286 if (cnt < 0) { 12287 cnt += mp->b_wptr - mp->b_rptr; 12288 break; 12289 } 12290 mp = mp->b_cont; 12291 } 12292 ASSERT(mp != NULL); 12293 *off = cnt; 12294 return (mp); 12295 } 12296 12297 /* 12298 * This function handles all retransmissions if SACK is enabled for this 12299 * connection. First it calculates how many segments can be retransmitted 12300 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12301 * segments. A segment is eligible if sack_cnt for that segment is greater 12302 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12303 * all eligible segments, it checks to see if TCP can send some new segments 12304 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12305 * 12306 * Parameters: 12307 * tcp_t *tcp: the tcp structure of the connection. 12308 * uint_t *flags: in return, appropriate value will be set for 12309 * tcp_rput_data(). 12310 */ 12311 static void 12312 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12313 { 12314 notsack_blk_t *notsack_blk; 12315 int32_t usable_swnd; 12316 int32_t mss; 12317 uint32_t seg_len; 12318 mblk_t *xmit_mp; 12319 tcp_stack_t *tcps = tcp->tcp_tcps; 12320 12321 ASSERT(tcp->tcp_sack_info != NULL); 12322 ASSERT(tcp->tcp_notsack_list != NULL); 12323 ASSERT(tcp->tcp_rexmit == B_FALSE); 12324 12325 /* Defensive coding in case there is a bug... */ 12326 if (tcp->tcp_notsack_list == NULL) { 12327 return; 12328 } 12329 notsack_blk = tcp->tcp_notsack_list; 12330 mss = tcp->tcp_mss; 12331 12332 /* 12333 * Limit the num of outstanding data in the network to be 12334 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12335 */ 12336 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12337 12338 /* At least retransmit 1 MSS of data. */ 12339 if (usable_swnd <= 0) { 12340 usable_swnd = mss; 12341 } 12342 12343 /* Make sure no new RTT samples will be taken. */ 12344 tcp->tcp_csuna = tcp->tcp_snxt; 12345 12346 notsack_blk = tcp->tcp_notsack_list; 12347 while (usable_swnd > 0) { 12348 mblk_t *snxt_mp, *tmp_mp; 12349 tcp_seq begin = tcp->tcp_sack_snxt; 12350 tcp_seq end; 12351 int32_t off; 12352 12353 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12354 if (SEQ_GT(notsack_blk->end, begin) && 12355 (notsack_blk->sack_cnt >= 12356 tcps->tcps_dupack_fast_retransmit)) { 12357 end = notsack_blk->end; 12358 if (SEQ_LT(begin, notsack_blk->begin)) { 12359 begin = notsack_blk->begin; 12360 } 12361 break; 12362 } 12363 } 12364 /* 12365 * All holes are filled. Manipulate tcp_cwnd to send more 12366 * if we can. Note that after the SACK recovery, tcp_cwnd is 12367 * set to tcp_cwnd_ssthresh. 12368 */ 12369 if (notsack_blk == NULL) { 12370 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12371 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12372 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12373 ASSERT(tcp->tcp_cwnd > 0); 12374 return; 12375 } else { 12376 usable_swnd = usable_swnd / mss; 12377 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12378 MAX(usable_swnd * mss, mss); 12379 *flags |= TH_XMIT_NEEDED; 12380 return; 12381 } 12382 } 12383 12384 /* 12385 * Note that we may send more than usable_swnd allows here 12386 * because of round off, but no more than 1 MSS of data. 12387 */ 12388 seg_len = end - begin; 12389 if (seg_len > mss) 12390 seg_len = mss; 12391 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12392 ASSERT(snxt_mp != NULL); 12393 /* This should not happen. Defensive coding again... */ 12394 if (snxt_mp == NULL) { 12395 return; 12396 } 12397 12398 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12399 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12400 if (xmit_mp == NULL) 12401 return; 12402 12403 usable_swnd -= seg_len; 12404 tcp->tcp_pipe += seg_len; 12405 tcp->tcp_sack_snxt = begin + seg_len; 12406 12407 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12408 12409 /* 12410 * Update the send timestamp to avoid false retransmission. 12411 */ 12412 snxt_mp->b_prev = (mblk_t *)lbolt; 12413 12414 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12415 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12416 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12417 /* 12418 * Update tcp_rexmit_max to extend this SACK recovery phase. 12419 * This happens when new data sent during fast recovery is 12420 * also lost. If TCP retransmits those new data, it needs 12421 * to extend SACK recover phase to avoid starting another 12422 * fast retransmit/recovery unnecessarily. 12423 */ 12424 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12425 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12426 } 12427 } 12428 } 12429 12430 /* 12431 * This function handles policy checking at TCP level for non-hard_bound/ 12432 * detached connections. 12433 */ 12434 static boolean_t 12435 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12436 boolean_t secure, boolean_t mctl_present) 12437 { 12438 ipsec_latch_t *ipl = NULL; 12439 ipsec_action_t *act = NULL; 12440 mblk_t *data_mp; 12441 ipsec_in_t *ii; 12442 const char *reason; 12443 kstat_named_t *counter; 12444 tcp_stack_t *tcps = tcp->tcp_tcps; 12445 ipsec_stack_t *ipss; 12446 ip_stack_t *ipst; 12447 12448 ASSERT(mctl_present || !secure); 12449 12450 ASSERT((ipha == NULL && ip6h != NULL) || 12451 (ip6h == NULL && ipha != NULL)); 12452 12453 /* 12454 * We don't necessarily have an ipsec_in_act action to verify 12455 * policy because of assymetrical policy where we have only 12456 * outbound policy and no inbound policy (possible with global 12457 * policy). 12458 */ 12459 if (!secure) { 12460 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12461 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12462 return (B_TRUE); 12463 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12464 "tcp_check_policy", ipha, ip6h, secure, 12465 tcps->tcps_netstack); 12466 ipss = tcps->tcps_netstack->netstack_ipsec; 12467 12468 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12469 DROPPER(ipss, ipds_tcp_clear), 12470 &tcps->tcps_dropper); 12471 return (B_FALSE); 12472 } 12473 12474 /* 12475 * We have a secure packet. 12476 */ 12477 if (act == NULL) { 12478 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12479 "tcp_check_policy", ipha, ip6h, secure, 12480 tcps->tcps_netstack); 12481 ipss = tcps->tcps_netstack->netstack_ipsec; 12482 12483 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12484 DROPPER(ipss, ipds_tcp_secure), 12485 &tcps->tcps_dropper); 12486 return (B_FALSE); 12487 } 12488 12489 /* 12490 * XXX This whole routine is currently incorrect. ipl should 12491 * be set to the latch pointer, but is currently not set, so 12492 * we initialize it to NULL to avoid picking up random garbage. 12493 */ 12494 if (ipl == NULL) 12495 return (B_TRUE); 12496 12497 data_mp = first_mp->b_cont; 12498 12499 ii = (ipsec_in_t *)first_mp->b_rptr; 12500 12501 ipst = tcps->tcps_netstack->netstack_ip; 12502 12503 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12504 &counter, tcp->tcp_connp)) { 12505 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12506 return (B_TRUE); 12507 } 12508 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12509 "tcp inbound policy mismatch: %s, packet dropped\n", 12510 reason); 12511 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12512 12513 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12514 &tcps->tcps_dropper); 12515 return (B_FALSE); 12516 } 12517 12518 /* 12519 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12520 * retransmission after a timeout. 12521 * 12522 * To limit the number of duplicate segments, we limit the number of segment 12523 * to be sent in one time to tcp_snd_burst, the burst variable. 12524 */ 12525 static void 12526 tcp_ss_rexmit(tcp_t *tcp) 12527 { 12528 uint32_t snxt; 12529 uint32_t smax; 12530 int32_t win; 12531 int32_t mss; 12532 int32_t off; 12533 int32_t burst = tcp->tcp_snd_burst; 12534 mblk_t *snxt_mp; 12535 tcp_stack_t *tcps = tcp->tcp_tcps; 12536 12537 /* 12538 * Note that tcp_rexmit can be set even though TCP has retransmitted 12539 * all unack'ed segments. 12540 */ 12541 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12542 smax = tcp->tcp_rexmit_max; 12543 snxt = tcp->tcp_rexmit_nxt; 12544 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12545 snxt = tcp->tcp_suna; 12546 } 12547 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12548 win -= snxt - tcp->tcp_suna; 12549 mss = tcp->tcp_mss; 12550 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12551 12552 while (SEQ_LT(snxt, smax) && (win > 0) && 12553 (burst > 0) && (snxt_mp != NULL)) { 12554 mblk_t *xmit_mp; 12555 mblk_t *old_snxt_mp = snxt_mp; 12556 uint32_t cnt = mss; 12557 12558 if (win < cnt) { 12559 cnt = win; 12560 } 12561 if (SEQ_GT(snxt + cnt, smax)) { 12562 cnt = smax - snxt; 12563 } 12564 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12565 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12566 if (xmit_mp == NULL) 12567 return; 12568 12569 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12570 12571 snxt += cnt; 12572 win -= cnt; 12573 /* 12574 * Update the send timestamp to avoid false 12575 * retransmission. 12576 */ 12577 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12578 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12579 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12580 12581 tcp->tcp_rexmit_nxt = snxt; 12582 burst--; 12583 } 12584 /* 12585 * If we have transmitted all we have at the time 12586 * we started the retranmission, we can leave 12587 * the rest of the job to tcp_wput_data(). But we 12588 * need to check the send window first. If the 12589 * win is not 0, go on with tcp_wput_data(). 12590 */ 12591 if (SEQ_LT(snxt, smax) || win == 0) { 12592 return; 12593 } 12594 } 12595 /* Only call tcp_wput_data() if there is data to be sent. */ 12596 if (tcp->tcp_unsent) { 12597 tcp_wput_data(tcp, NULL, B_FALSE); 12598 } 12599 } 12600 12601 /* 12602 * Process all TCP option in SYN segment. Note that this function should 12603 * be called after tcp_adapt_ire() is called so that the necessary info 12604 * from IRE is already set in the tcp structure. 12605 * 12606 * This function sets up the correct tcp_mss value according to the 12607 * MSS option value and our header size. It also sets up the window scale 12608 * and timestamp values, and initialize SACK info blocks. But it does not 12609 * change receive window size after setting the tcp_mss value. The caller 12610 * should do the appropriate change. 12611 */ 12612 void 12613 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12614 { 12615 int options; 12616 tcp_opt_t tcpopt; 12617 uint32_t mss_max; 12618 char *tmp_tcph; 12619 tcp_stack_t *tcps = tcp->tcp_tcps; 12620 12621 tcpopt.tcp = NULL; 12622 options = tcp_parse_options(tcph, &tcpopt); 12623 12624 /* 12625 * Process MSS option. Note that MSS option value does not account 12626 * for IP or TCP options. This means that it is equal to MTU - minimum 12627 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12628 * IPv6. 12629 */ 12630 if (!(options & TCP_OPT_MSS_PRESENT)) { 12631 if (tcp->tcp_ipversion == IPV4_VERSION) 12632 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12633 else 12634 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12635 } else { 12636 if (tcp->tcp_ipversion == IPV4_VERSION) 12637 mss_max = tcps->tcps_mss_max_ipv4; 12638 else 12639 mss_max = tcps->tcps_mss_max_ipv6; 12640 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12641 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12642 else if (tcpopt.tcp_opt_mss > mss_max) 12643 tcpopt.tcp_opt_mss = mss_max; 12644 } 12645 12646 /* Process Window Scale option. */ 12647 if (options & TCP_OPT_WSCALE_PRESENT) { 12648 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12649 tcp->tcp_snd_ws_ok = B_TRUE; 12650 } else { 12651 tcp->tcp_snd_ws = B_FALSE; 12652 tcp->tcp_snd_ws_ok = B_FALSE; 12653 tcp->tcp_rcv_ws = B_FALSE; 12654 } 12655 12656 /* Process Timestamp option. */ 12657 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12658 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12659 tmp_tcph = (char *)tcp->tcp_tcph; 12660 12661 tcp->tcp_snd_ts_ok = B_TRUE; 12662 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12663 tcp->tcp_last_rcv_lbolt = lbolt64; 12664 ASSERT(OK_32PTR(tmp_tcph)); 12665 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12666 12667 /* Fill in our template header with basic timestamp option. */ 12668 tmp_tcph += tcp->tcp_tcp_hdr_len; 12669 tmp_tcph[0] = TCPOPT_NOP; 12670 tmp_tcph[1] = TCPOPT_NOP; 12671 tmp_tcph[2] = TCPOPT_TSTAMP; 12672 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12673 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12674 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12675 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12676 } else { 12677 tcp->tcp_snd_ts_ok = B_FALSE; 12678 } 12679 12680 /* 12681 * Process SACK options. If SACK is enabled for this connection, 12682 * then allocate the SACK info structure. Note the following ways 12683 * when tcp_snd_sack_ok is set to true. 12684 * 12685 * For active connection: in tcp_adapt_ire() called in 12686 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12687 * is checked. 12688 * 12689 * For passive connection: in tcp_adapt_ire() called in 12690 * tcp_accept_comm(). 12691 * 12692 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12693 * That check makes sure that if we did not send a SACK OK option, 12694 * we will not enable SACK for this connection even though the other 12695 * side sends us SACK OK option. For active connection, the SACK 12696 * info structure has already been allocated. So we need to free 12697 * it if SACK is disabled. 12698 */ 12699 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12700 (tcp->tcp_snd_sack_ok || 12701 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12702 /* This should be true only in the passive case. */ 12703 if (tcp->tcp_sack_info == NULL) { 12704 ASSERT(TCP_IS_DETACHED(tcp)); 12705 tcp->tcp_sack_info = 12706 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12707 } 12708 if (tcp->tcp_sack_info == NULL) { 12709 tcp->tcp_snd_sack_ok = B_FALSE; 12710 } else { 12711 tcp->tcp_snd_sack_ok = B_TRUE; 12712 if (tcp->tcp_snd_ts_ok) { 12713 tcp->tcp_max_sack_blk = 3; 12714 } else { 12715 tcp->tcp_max_sack_blk = 4; 12716 } 12717 } 12718 } else { 12719 /* 12720 * Resetting tcp_snd_sack_ok to B_FALSE so that 12721 * no SACK info will be used for this 12722 * connection. This assumes that SACK usage 12723 * permission is negotiated. This may need 12724 * to be changed once this is clarified. 12725 */ 12726 if (tcp->tcp_sack_info != NULL) { 12727 ASSERT(tcp->tcp_notsack_list == NULL); 12728 kmem_cache_free(tcp_sack_info_cache, 12729 tcp->tcp_sack_info); 12730 tcp->tcp_sack_info = NULL; 12731 } 12732 tcp->tcp_snd_sack_ok = B_FALSE; 12733 } 12734 12735 /* 12736 * Now we know the exact TCP/IP header length, subtract 12737 * that from tcp_mss to get our side's MSS. 12738 */ 12739 tcp->tcp_mss -= tcp->tcp_hdr_len; 12740 /* 12741 * Here we assume that the other side's header size will be equal to 12742 * our header size. We calculate the real MSS accordingly. Need to 12743 * take into additional stuffs IPsec puts in. 12744 * 12745 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12746 */ 12747 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12748 ((tcp->tcp_ipversion == IPV4_VERSION ? 12749 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12750 12751 /* 12752 * Set MSS to the smaller one of both ends of the connection. 12753 * We should not have called tcp_mss_set() before, but our 12754 * side of the MSS should have been set to a proper value 12755 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12756 * STREAM head parameters properly. 12757 * 12758 * If we have a larger-than-16-bit window but the other side 12759 * didn't want to do window scale, tcp_rwnd_set() will take 12760 * care of that. 12761 */ 12762 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12763 } 12764 12765 /* 12766 * Sends the T_CONN_IND to the listener. The caller calls this 12767 * functions via squeue to get inside the listener's perimeter 12768 * once the 3 way hand shake is done a T_CONN_IND needs to be 12769 * sent. As an optimization, the caller can call this directly 12770 * if listener's perimeter is same as eager's. 12771 */ 12772 /* ARGSUSED */ 12773 void 12774 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12775 { 12776 conn_t *lconnp = (conn_t *)arg; 12777 tcp_t *listener = lconnp->conn_tcp; 12778 tcp_t *tcp; 12779 struct T_conn_ind *conn_ind; 12780 ipaddr_t *addr_cache; 12781 boolean_t need_send_conn_ind = B_FALSE; 12782 tcp_stack_t *tcps = listener->tcp_tcps; 12783 12784 /* retrieve the eager */ 12785 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12786 ASSERT(conn_ind->OPT_offset != 0 && 12787 conn_ind->OPT_length == sizeof (intptr_t)); 12788 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12789 conn_ind->OPT_length); 12790 12791 /* 12792 * TLI/XTI applications will get confused by 12793 * sending eager as an option since it violates 12794 * the option semantics. So remove the eager as 12795 * option since TLI/XTI app doesn't need it anyway. 12796 */ 12797 if (!TCP_IS_SOCKET(listener)) { 12798 conn_ind->OPT_length = 0; 12799 conn_ind->OPT_offset = 0; 12800 } 12801 if (listener->tcp_state == TCPS_CLOSED || 12802 TCP_IS_DETACHED(listener)) { 12803 /* 12804 * If listener has closed, it would have caused a 12805 * a cleanup/blowoff to happen for the eager. We 12806 * just need to return. 12807 */ 12808 freemsg(mp); 12809 return; 12810 } 12811 12812 12813 /* 12814 * if the conn_req_q is full defer passing up the 12815 * T_CONN_IND until space is availabe after t_accept() 12816 * processing 12817 */ 12818 mutex_enter(&listener->tcp_eager_lock); 12819 12820 /* 12821 * Take the eager out, if it is in the list of droppable eagers 12822 * as we are here because the 3W handshake is over. 12823 */ 12824 MAKE_UNDROPPABLE(tcp); 12825 12826 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12827 tcp_t *tail; 12828 12829 /* 12830 * The eager already has an extra ref put in tcp_rput_data 12831 * so that it stays till accept comes back even though it 12832 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12833 */ 12834 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12835 listener->tcp_conn_req_cnt_q0--; 12836 listener->tcp_conn_req_cnt_q++; 12837 12838 /* Move from SYN_RCVD to ESTABLISHED list */ 12839 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12840 tcp->tcp_eager_prev_q0; 12841 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12842 tcp->tcp_eager_next_q0; 12843 tcp->tcp_eager_prev_q0 = NULL; 12844 tcp->tcp_eager_next_q0 = NULL; 12845 12846 /* 12847 * Insert at end of the queue because sockfs 12848 * sends down T_CONN_RES in chronological 12849 * order. Leaving the older conn indications 12850 * at front of the queue helps reducing search 12851 * time. 12852 */ 12853 tail = listener->tcp_eager_last_q; 12854 if (tail != NULL) 12855 tail->tcp_eager_next_q = tcp; 12856 else 12857 listener->tcp_eager_next_q = tcp; 12858 listener->tcp_eager_last_q = tcp; 12859 tcp->tcp_eager_next_q = NULL; 12860 /* 12861 * Delay sending up the T_conn_ind until we are 12862 * done with the eager. Once we have have sent up 12863 * the T_conn_ind, the accept can potentially complete 12864 * any time and release the refhold we have on the eager. 12865 */ 12866 need_send_conn_ind = B_TRUE; 12867 } else { 12868 /* 12869 * Defer connection on q0 and set deferred 12870 * connection bit true 12871 */ 12872 tcp->tcp_conn_def_q0 = B_TRUE; 12873 12874 /* take tcp out of q0 ... */ 12875 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12876 tcp->tcp_eager_next_q0; 12877 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12878 tcp->tcp_eager_prev_q0; 12879 12880 /* ... and place it at the end of q0 */ 12881 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12882 tcp->tcp_eager_next_q0 = listener; 12883 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12884 listener->tcp_eager_prev_q0 = tcp; 12885 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12886 } 12887 12888 /* we have timed out before */ 12889 if (tcp->tcp_syn_rcvd_timeout != 0) { 12890 tcp->tcp_syn_rcvd_timeout = 0; 12891 listener->tcp_syn_rcvd_timeout--; 12892 if (listener->tcp_syn_defense && 12893 listener->tcp_syn_rcvd_timeout <= 12894 (tcps->tcps_conn_req_max_q0 >> 5) && 12895 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12896 listener->tcp_last_rcv_lbolt)) { 12897 /* 12898 * Turn off the defense mode if we 12899 * believe the SYN attack is over. 12900 */ 12901 listener->tcp_syn_defense = B_FALSE; 12902 if (listener->tcp_ip_addr_cache) { 12903 kmem_free((void *)listener->tcp_ip_addr_cache, 12904 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12905 listener->tcp_ip_addr_cache = NULL; 12906 } 12907 } 12908 } 12909 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12910 if (addr_cache != NULL) { 12911 /* 12912 * We have finished a 3-way handshake with this 12913 * remote host. This proves the IP addr is good. 12914 * Cache it! 12915 */ 12916 addr_cache[IP_ADDR_CACHE_HASH( 12917 tcp->tcp_remote)] = tcp->tcp_remote; 12918 } 12919 mutex_exit(&listener->tcp_eager_lock); 12920 if (need_send_conn_ind) 12921 putnext(listener->tcp_rq, mp); 12922 } 12923 12924 mblk_t * 12925 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12926 uint_t *ifindexp, ip6_pkt_t *ippp) 12927 { 12928 ip_pktinfo_t *pinfo; 12929 ip6_t *ip6h; 12930 uchar_t *rptr; 12931 mblk_t *first_mp = mp; 12932 boolean_t mctl_present = B_FALSE; 12933 uint_t ifindex = 0; 12934 ip6_pkt_t ipp; 12935 uint_t ipvers; 12936 uint_t ip_hdr_len; 12937 tcp_stack_t *tcps = tcp->tcp_tcps; 12938 12939 rptr = mp->b_rptr; 12940 ASSERT(OK_32PTR(rptr)); 12941 ASSERT(tcp != NULL); 12942 ipp.ipp_fields = 0; 12943 12944 switch DB_TYPE(mp) { 12945 case M_CTL: 12946 mp = mp->b_cont; 12947 if (mp == NULL) { 12948 freemsg(first_mp); 12949 return (NULL); 12950 } 12951 if (DB_TYPE(mp) != M_DATA) { 12952 freemsg(first_mp); 12953 return (NULL); 12954 } 12955 mctl_present = B_TRUE; 12956 break; 12957 case M_DATA: 12958 break; 12959 default: 12960 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12961 freemsg(mp); 12962 return (NULL); 12963 } 12964 ipvers = IPH_HDR_VERSION(rptr); 12965 if (ipvers == IPV4_VERSION) { 12966 if (tcp == NULL) { 12967 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12968 goto done; 12969 } 12970 12971 ipp.ipp_fields |= IPPF_HOPLIMIT; 12972 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12973 12974 /* 12975 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12976 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12977 */ 12978 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12979 mctl_present) { 12980 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12981 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12982 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12983 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12984 ipp.ipp_fields |= IPPF_IFINDEX; 12985 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12986 ifindex = pinfo->ip_pkt_ifindex; 12987 } 12988 freeb(first_mp); 12989 mctl_present = B_FALSE; 12990 } 12991 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12992 } else { 12993 ip6h = (ip6_t *)rptr; 12994 12995 ASSERT(ipvers == IPV6_VERSION); 12996 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12997 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12998 ipp.ipp_hoplimit = ip6h->ip6_hops; 12999 13000 if (ip6h->ip6_nxt != IPPROTO_TCP) { 13001 uint8_t nexthdrp; 13002 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13003 13004 /* Look for ifindex information */ 13005 if (ip6h->ip6_nxt == IPPROTO_RAW) { 13006 ip6i_t *ip6i = (ip6i_t *)ip6h; 13007 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 13008 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13009 freemsg(first_mp); 13010 return (NULL); 13011 } 13012 13013 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 13014 ASSERT(ip6i->ip6i_ifindex != 0); 13015 ipp.ipp_fields |= IPPF_IFINDEX; 13016 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 13017 ifindex = ip6i->ip6i_ifindex; 13018 } 13019 rptr = (uchar_t *)&ip6i[1]; 13020 mp->b_rptr = rptr; 13021 if (rptr == mp->b_wptr) { 13022 mblk_t *mp1; 13023 mp1 = mp->b_cont; 13024 freeb(mp); 13025 mp = mp1; 13026 rptr = mp->b_rptr; 13027 } 13028 if (MBLKL(mp) < IPV6_HDR_LEN + 13029 sizeof (tcph_t)) { 13030 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13031 freemsg(first_mp); 13032 return (NULL); 13033 } 13034 ip6h = (ip6_t *)rptr; 13035 } 13036 13037 /* 13038 * Find any potentially interesting extension headers 13039 * as well as the length of the IPv6 + extension 13040 * headers. 13041 */ 13042 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 13043 /* Verify if this is a TCP packet */ 13044 if (nexthdrp != IPPROTO_TCP) { 13045 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 13046 freemsg(first_mp); 13047 return (NULL); 13048 } 13049 } else { 13050 ip_hdr_len = IPV6_HDR_LEN; 13051 } 13052 } 13053 13054 done: 13055 if (ipversp != NULL) 13056 *ipversp = ipvers; 13057 if (ip_hdr_lenp != NULL) 13058 *ip_hdr_lenp = ip_hdr_len; 13059 if (ippp != NULL) 13060 *ippp = ipp; 13061 if (ifindexp != NULL) 13062 *ifindexp = ifindex; 13063 if (mctl_present) { 13064 freeb(first_mp); 13065 } 13066 return (mp); 13067 } 13068 13069 /* 13070 * Handle M_DATA messages from IP. Its called directly from IP via 13071 * squeue for AF_INET type sockets fast path. No M_CTL are expected 13072 * in this path. 13073 * 13074 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 13075 * v4 and v6), we are called through tcp_input() and a M_CTL can 13076 * be present for options but tcp_find_pktinfo() deals with it. We 13077 * only expect M_DATA packets after tcp_find_pktinfo() is done. 13078 * 13079 * The first argument is always the connp/tcp to which the mp belongs. 13080 * There are no exceptions to this rule. The caller has already put 13081 * a reference on this connp/tcp and once tcp_rput_data() returns, 13082 * the squeue will do the refrele. 13083 * 13084 * The TH_SYN for the listener directly go to tcp_conn_request via 13085 * squeue. 13086 * 13087 * sqp: NULL = recursive, sqp != NULL means called from squeue 13088 */ 13089 void 13090 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 13091 { 13092 int32_t bytes_acked; 13093 int32_t gap; 13094 mblk_t *mp1; 13095 uint_t flags; 13096 uint32_t new_swnd = 0; 13097 uchar_t *iphdr; 13098 uchar_t *rptr; 13099 int32_t rgap; 13100 uint32_t seg_ack; 13101 int seg_len; 13102 uint_t ip_hdr_len; 13103 uint32_t seg_seq; 13104 tcph_t *tcph; 13105 int urp; 13106 tcp_opt_t tcpopt; 13107 uint_t ipvers; 13108 ip6_pkt_t ipp; 13109 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 13110 uint32_t cwnd; 13111 uint32_t add; 13112 int npkt; 13113 int mss; 13114 conn_t *connp = (conn_t *)arg; 13115 squeue_t *sqp = (squeue_t *)arg2; 13116 tcp_t *tcp = connp->conn_tcp; 13117 tcp_stack_t *tcps = tcp->tcp_tcps; 13118 13119 /* 13120 * RST from fused tcp loopback peer should trigger an unfuse. 13121 */ 13122 if (tcp->tcp_fused) { 13123 TCP_STAT(tcps, tcp_fusion_aborted); 13124 tcp_unfuse(tcp); 13125 } 13126 13127 iphdr = mp->b_rptr; 13128 rptr = mp->b_rptr; 13129 ASSERT(OK_32PTR(rptr)); 13130 13131 /* 13132 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 13133 * processing here. For rest call tcp_find_pktinfo to fill up the 13134 * necessary information. 13135 */ 13136 if (IPCL_IS_TCP4(connp)) { 13137 ipvers = IPV4_VERSION; 13138 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13139 } else { 13140 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13141 NULL, &ipp); 13142 if (mp == NULL) { 13143 TCP_STAT(tcps, tcp_rput_v6_error); 13144 return; 13145 } 13146 iphdr = mp->b_rptr; 13147 rptr = mp->b_rptr; 13148 } 13149 ASSERT(DB_TYPE(mp) == M_DATA); 13150 13151 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13152 seg_seq = ABE32_TO_U32(tcph->th_seq); 13153 seg_ack = ABE32_TO_U32(tcph->th_ack); 13154 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13155 seg_len = (int)(mp->b_wptr - rptr) - 13156 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13157 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13158 do { 13159 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13160 (uintptr_t)INT_MAX); 13161 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13162 } while ((mp1 = mp1->b_cont) != NULL && 13163 mp1->b_datap->db_type == M_DATA); 13164 } 13165 13166 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13167 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13168 seg_len, tcph); 13169 return; 13170 } 13171 13172 if (sqp != NULL) { 13173 /* 13174 * This is the correct place to update tcp_last_recv_time. Note 13175 * that it is also updated for tcp structure that belongs to 13176 * global and listener queues which do not really need updating. 13177 * But that should not cause any harm. And it is updated for 13178 * all kinds of incoming segments, not only for data segments. 13179 */ 13180 tcp->tcp_last_recv_time = lbolt; 13181 } 13182 13183 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13184 13185 BUMP_LOCAL(tcp->tcp_ibsegs); 13186 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13187 13188 if ((flags & TH_URG) && sqp != NULL) { 13189 /* 13190 * TCP can't handle urgent pointers that arrive before 13191 * the connection has been accept()ed since it can't 13192 * buffer OOB data. Discard segment if this happens. 13193 * 13194 * We can't just rely on a non-null tcp_listener to indicate 13195 * that the accept() has completed since unlinking of the 13196 * eager and completion of the accept are not atomic. 13197 * tcp_detached, when it is not set (B_FALSE) indicates 13198 * that the accept() has completed. 13199 * 13200 * Nor can it reassemble urgent pointers, so discard 13201 * if it's not the next segment expected. 13202 * 13203 * Otherwise, collapse chain into one mblk (discard if 13204 * that fails). This makes sure the headers, retransmitted 13205 * data, and new data all are in the same mblk. 13206 */ 13207 ASSERT(mp != NULL); 13208 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13209 freemsg(mp); 13210 return; 13211 } 13212 /* Update pointers into message */ 13213 iphdr = rptr = mp->b_rptr; 13214 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13215 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13216 /* 13217 * Since we can't handle any data with this urgent 13218 * pointer that is out of sequence, we expunge 13219 * the data. This allows us to still register 13220 * the urgent mark and generate the M_PCSIG, 13221 * which we can do. 13222 */ 13223 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13224 seg_len = 0; 13225 } 13226 } 13227 13228 switch (tcp->tcp_state) { 13229 case TCPS_SYN_SENT: 13230 if (flags & TH_ACK) { 13231 /* 13232 * Note that our stack cannot send data before a 13233 * connection is established, therefore the 13234 * following check is valid. Otherwise, it has 13235 * to be changed. 13236 */ 13237 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13238 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13239 freemsg(mp); 13240 if (flags & TH_RST) 13241 return; 13242 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13243 tcp, seg_ack, 0, TH_RST); 13244 return; 13245 } 13246 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13247 } 13248 if (flags & TH_RST) { 13249 freemsg(mp); 13250 if (flags & TH_ACK) 13251 (void) tcp_clean_death(tcp, 13252 ECONNREFUSED, 13); 13253 return; 13254 } 13255 if (!(flags & TH_SYN)) { 13256 freemsg(mp); 13257 return; 13258 } 13259 13260 /* Process all TCP options. */ 13261 tcp_process_options(tcp, tcph); 13262 /* 13263 * The following changes our rwnd to be a multiple of the 13264 * MIN(peer MSS, our MSS) for performance reason. 13265 */ 13266 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 13267 tcp->tcp_mss)); 13268 13269 /* Is the other end ECN capable? */ 13270 if (tcp->tcp_ecn_ok) { 13271 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13272 tcp->tcp_ecn_ok = B_FALSE; 13273 } 13274 } 13275 /* 13276 * Clear ECN flags because it may interfere with later 13277 * processing. 13278 */ 13279 flags &= ~(TH_ECE|TH_CWR); 13280 13281 tcp->tcp_irs = seg_seq; 13282 tcp->tcp_rack = seg_seq; 13283 tcp->tcp_rnxt = seg_seq + 1; 13284 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13285 if (!TCP_IS_DETACHED(tcp)) { 13286 /* Allocate room for SACK options if needed. */ 13287 if (tcp->tcp_snd_sack_ok) { 13288 (void) mi_set_sth_wroff(tcp->tcp_rq, 13289 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 13290 (tcp->tcp_loopback ? 0 : 13291 tcps->tcps_wroff_xtra)); 13292 } else { 13293 (void) mi_set_sth_wroff(tcp->tcp_rq, 13294 tcp->tcp_hdr_len + 13295 (tcp->tcp_loopback ? 0 : 13296 tcps->tcps_wroff_xtra)); 13297 } 13298 } 13299 if (flags & TH_ACK) { 13300 /* 13301 * If we can't get the confirmation upstream, pretend 13302 * we didn't even see this one. 13303 * 13304 * XXX: how can we pretend we didn't see it if we 13305 * have updated rnxt et. al. 13306 * 13307 * For loopback we defer sending up the T_CONN_CON 13308 * until after some checks below. 13309 */ 13310 mp1 = NULL; 13311 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13312 tcp->tcp_loopback ? &mp1 : NULL)) { 13313 freemsg(mp); 13314 return; 13315 } 13316 /* SYN was acked - making progress */ 13317 if (tcp->tcp_ipversion == IPV6_VERSION) 13318 tcp->tcp_ip_forward_progress = B_TRUE; 13319 13320 /* One for the SYN */ 13321 tcp->tcp_suna = tcp->tcp_iss + 1; 13322 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13323 tcp->tcp_state = TCPS_ESTABLISHED; 13324 13325 /* 13326 * If SYN was retransmitted, need to reset all 13327 * retransmission info. This is because this 13328 * segment will be treated as a dup ACK. 13329 */ 13330 if (tcp->tcp_rexmit) { 13331 tcp->tcp_rexmit = B_FALSE; 13332 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13333 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13334 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13335 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13336 tcp->tcp_ms_we_have_waited = 0; 13337 13338 /* 13339 * Set tcp_cwnd back to 1 MSS, per 13340 * recommendation from 13341 * draft-floyd-incr-init-win-01.txt, 13342 * Increasing TCP's Initial Window. 13343 */ 13344 tcp->tcp_cwnd = tcp->tcp_mss; 13345 } 13346 13347 tcp->tcp_swl1 = seg_seq; 13348 tcp->tcp_swl2 = seg_ack; 13349 13350 new_swnd = BE16_TO_U16(tcph->th_win); 13351 tcp->tcp_swnd = new_swnd; 13352 if (new_swnd > tcp->tcp_max_swnd) 13353 tcp->tcp_max_swnd = new_swnd; 13354 13355 /* 13356 * Always send the three-way handshake ack immediately 13357 * in order to make the connection complete as soon as 13358 * possible on the accepting host. 13359 */ 13360 flags |= TH_ACK_NEEDED; 13361 13362 /* 13363 * Special case for loopback. At this point we have 13364 * received SYN-ACK from the remote endpoint. In 13365 * order to ensure that both endpoints reach the 13366 * fused state prior to any data exchange, the final 13367 * ACK needs to be sent before we indicate T_CONN_CON 13368 * to the module upstream. 13369 */ 13370 if (tcp->tcp_loopback) { 13371 mblk_t *ack_mp; 13372 13373 ASSERT(!tcp->tcp_unfusable); 13374 ASSERT(mp1 != NULL); 13375 /* 13376 * For loopback, we always get a pure SYN-ACK 13377 * and only need to send back the final ACK 13378 * with no data (this is because the other 13379 * tcp is ours and we don't do T/TCP). This 13380 * final ACK triggers the passive side to 13381 * perform fusion in ESTABLISHED state. 13382 */ 13383 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13384 if (tcp->tcp_ack_tid != 0) { 13385 (void) TCP_TIMER_CANCEL(tcp, 13386 tcp->tcp_ack_tid); 13387 tcp->tcp_ack_tid = 0; 13388 } 13389 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13390 BUMP_LOCAL(tcp->tcp_obsegs); 13391 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13392 13393 /* Send up T_CONN_CON */ 13394 putnext(tcp->tcp_rq, mp1); 13395 13396 freemsg(mp); 13397 return; 13398 } 13399 /* 13400 * Forget fusion; we need to handle more 13401 * complex cases below. Send the deferred 13402 * T_CONN_CON message upstream and proceed 13403 * as usual. Mark this tcp as not capable 13404 * of fusion. 13405 */ 13406 TCP_STAT(tcps, tcp_fusion_unfusable); 13407 tcp->tcp_unfusable = B_TRUE; 13408 putnext(tcp->tcp_rq, mp1); 13409 } 13410 13411 /* 13412 * Check to see if there is data to be sent. If 13413 * yes, set the transmit flag. Then check to see 13414 * if received data processing needs to be done. 13415 * If not, go straight to xmit_check. This short 13416 * cut is OK as we don't support T/TCP. 13417 */ 13418 if (tcp->tcp_unsent) 13419 flags |= TH_XMIT_NEEDED; 13420 13421 if (seg_len == 0 && !(flags & TH_URG)) { 13422 freemsg(mp); 13423 goto xmit_check; 13424 } 13425 13426 flags &= ~TH_SYN; 13427 seg_seq++; 13428 break; 13429 } 13430 tcp->tcp_state = TCPS_SYN_RCVD; 13431 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13432 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13433 if (mp1) { 13434 DB_CPID(mp1) = tcp->tcp_cpid; 13435 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13436 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13437 } 13438 freemsg(mp); 13439 return; 13440 case TCPS_SYN_RCVD: 13441 if (flags & TH_ACK) { 13442 /* 13443 * In this state, a SYN|ACK packet is either bogus 13444 * because the other side must be ACKing our SYN which 13445 * indicates it has seen the ACK for their SYN and 13446 * shouldn't retransmit it or we're crossing SYNs 13447 * on active open. 13448 */ 13449 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13450 freemsg(mp); 13451 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13452 tcp, seg_ack, 0, TH_RST); 13453 return; 13454 } 13455 /* 13456 * NOTE: RFC 793 pg. 72 says this should be 13457 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13458 * but that would mean we have an ack that ignored 13459 * our SYN. 13460 */ 13461 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13462 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13463 freemsg(mp); 13464 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13465 tcp, seg_ack, 0, TH_RST); 13466 return; 13467 } 13468 } 13469 break; 13470 case TCPS_LISTEN: 13471 /* 13472 * Only a TLI listener can come through this path when a 13473 * acceptor is going back to be a listener and a packet 13474 * for the acceptor hits the classifier. For a socket 13475 * listener, this can never happen because a listener 13476 * can never accept connection on itself and hence a 13477 * socket acceptor can not go back to being a listener. 13478 */ 13479 ASSERT(!TCP_IS_SOCKET(tcp)); 13480 /*FALLTHRU*/ 13481 case TCPS_CLOSED: 13482 case TCPS_BOUND: { 13483 conn_t *new_connp; 13484 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13485 13486 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13487 if (new_connp != NULL) { 13488 tcp_reinput(new_connp, mp, connp->conn_sqp); 13489 return; 13490 } 13491 /* We failed to classify. For now just drop the packet */ 13492 freemsg(mp); 13493 return; 13494 } 13495 case TCPS_IDLE: 13496 /* 13497 * Handle the case where the tcp_clean_death() has happened 13498 * on a connection (application hasn't closed yet) but a packet 13499 * was already queued on squeue before tcp_clean_death() 13500 * was processed. Calling tcp_clean_death() twice on same 13501 * connection can result in weird behaviour. 13502 */ 13503 freemsg(mp); 13504 return; 13505 default: 13506 break; 13507 } 13508 13509 /* 13510 * Already on the correct queue/perimeter. 13511 * If this is a detached connection and not an eager 13512 * connection hanging off a listener then new data 13513 * (past the FIN) will cause a reset. 13514 * We do a special check here where it 13515 * is out of the main line, rather than check 13516 * if we are detached every time we see new 13517 * data down below. 13518 */ 13519 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13520 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13521 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13522 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13523 13524 freemsg(mp); 13525 /* 13526 * This could be an SSL closure alert. We're detached so just 13527 * acknowledge it this last time. 13528 */ 13529 if (tcp->tcp_kssl_ctx != NULL) { 13530 kssl_release_ctx(tcp->tcp_kssl_ctx); 13531 tcp->tcp_kssl_ctx = NULL; 13532 13533 tcp->tcp_rnxt += seg_len; 13534 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13535 flags |= TH_ACK_NEEDED; 13536 goto ack_check; 13537 } 13538 13539 tcp_xmit_ctl("new data when detached", tcp, 13540 tcp->tcp_snxt, 0, TH_RST); 13541 (void) tcp_clean_death(tcp, EPROTO, 12); 13542 return; 13543 } 13544 13545 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13546 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13547 new_swnd = BE16_TO_U16(tcph->th_win) << 13548 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13549 13550 if (tcp->tcp_snd_ts_ok) { 13551 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13552 /* 13553 * This segment is not acceptable. 13554 * Drop it and send back an ACK. 13555 */ 13556 freemsg(mp); 13557 flags |= TH_ACK_NEEDED; 13558 goto ack_check; 13559 } 13560 } else if (tcp->tcp_snd_sack_ok) { 13561 ASSERT(tcp->tcp_sack_info != NULL); 13562 tcpopt.tcp = tcp; 13563 /* 13564 * SACK info in already updated in tcp_parse_options. Ignore 13565 * all other TCP options... 13566 */ 13567 (void) tcp_parse_options(tcph, &tcpopt); 13568 } 13569 try_again:; 13570 mss = tcp->tcp_mss; 13571 gap = seg_seq - tcp->tcp_rnxt; 13572 rgap = tcp->tcp_rwnd - (gap + seg_len); 13573 /* 13574 * gap is the amount of sequence space between what we expect to see 13575 * and what we got for seg_seq. A positive value for gap means 13576 * something got lost. A negative value means we got some old stuff. 13577 */ 13578 if (gap < 0) { 13579 /* Old stuff present. Is the SYN in there? */ 13580 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13581 (seg_len != 0)) { 13582 flags &= ~TH_SYN; 13583 seg_seq++; 13584 urp--; 13585 /* Recompute the gaps after noting the SYN. */ 13586 goto try_again; 13587 } 13588 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13589 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13590 (seg_len > -gap ? -gap : seg_len)); 13591 /* Remove the old stuff from seg_len. */ 13592 seg_len += gap; 13593 /* 13594 * Anything left? 13595 * Make sure to check for unack'd FIN when rest of data 13596 * has been previously ack'd. 13597 */ 13598 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13599 /* 13600 * Resets are only valid if they lie within our offered 13601 * window. If the RST bit is set, we just ignore this 13602 * segment. 13603 */ 13604 if (flags & TH_RST) { 13605 freemsg(mp); 13606 return; 13607 } 13608 13609 /* 13610 * The arriving of dup data packets indicate that we 13611 * may have postponed an ack for too long, or the other 13612 * side's RTT estimate is out of shape. Start acking 13613 * more often. 13614 */ 13615 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13616 tcp->tcp_rack_cnt >= 1 && 13617 tcp->tcp_rack_abs_max > 2) { 13618 tcp->tcp_rack_abs_max--; 13619 } 13620 tcp->tcp_rack_cur_max = 1; 13621 13622 /* 13623 * This segment is "unacceptable". None of its 13624 * sequence space lies within our advertized window. 13625 * 13626 * Adjust seg_len to the original value for tracing. 13627 */ 13628 seg_len -= gap; 13629 if (tcp->tcp_debug) { 13630 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13631 "tcp_rput: unacceptable, gap %d, rgap %d, " 13632 "flags 0x%x, seg_seq %u, seg_ack %u, " 13633 "seg_len %d, rnxt %u, snxt %u, %s", 13634 gap, rgap, flags, seg_seq, seg_ack, 13635 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13636 tcp_display(tcp, NULL, 13637 DISP_ADDR_AND_PORT)); 13638 } 13639 13640 /* 13641 * Arrange to send an ACK in response to the 13642 * unacceptable segment per RFC 793 page 69. There 13643 * is only one small difference between ours and the 13644 * acceptability test in the RFC - we accept ACK-only 13645 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13646 * will be generated. 13647 * 13648 * Note that we have to ACK an ACK-only packet at least 13649 * for stacks that send 0-length keep-alives with 13650 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13651 * section 4.2.3.6. As long as we don't ever generate 13652 * an unacceptable packet in response to an incoming 13653 * packet that is unacceptable, it should not cause 13654 * "ACK wars". 13655 */ 13656 flags |= TH_ACK_NEEDED; 13657 13658 /* 13659 * Continue processing this segment in order to use the 13660 * ACK information it contains, but skip all other 13661 * sequence-number processing. Processing the ACK 13662 * information is necessary in order to 13663 * re-synchronize connections that may have lost 13664 * synchronization. 13665 * 13666 * We clear seg_len and flag fields related to 13667 * sequence number processing as they are not 13668 * to be trusted for an unacceptable segment. 13669 */ 13670 seg_len = 0; 13671 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13672 goto process_ack; 13673 } 13674 13675 /* Fix seg_seq, and chew the gap off the front. */ 13676 seg_seq = tcp->tcp_rnxt; 13677 urp += gap; 13678 do { 13679 mblk_t *mp2; 13680 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13681 (uintptr_t)UINT_MAX); 13682 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13683 if (gap > 0) { 13684 mp->b_rptr = mp->b_wptr - gap; 13685 break; 13686 } 13687 mp2 = mp; 13688 mp = mp->b_cont; 13689 freeb(mp2); 13690 } while (gap < 0); 13691 /* 13692 * If the urgent data has already been acknowledged, we 13693 * should ignore TH_URG below 13694 */ 13695 if (urp < 0) 13696 flags &= ~TH_URG; 13697 } 13698 /* 13699 * rgap is the amount of stuff received out of window. A negative 13700 * value is the amount out of window. 13701 */ 13702 if (rgap < 0) { 13703 mblk_t *mp2; 13704 13705 if (tcp->tcp_rwnd == 0) { 13706 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13707 } else { 13708 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13709 UPDATE_MIB(&tcps->tcps_mib, 13710 tcpInDataPastWinBytes, -rgap); 13711 } 13712 13713 /* 13714 * seg_len does not include the FIN, so if more than 13715 * just the FIN is out of window, we act like we don't 13716 * see it. (If just the FIN is out of window, rgap 13717 * will be zero and we will go ahead and acknowledge 13718 * the FIN.) 13719 */ 13720 flags &= ~TH_FIN; 13721 13722 /* Fix seg_len and make sure there is something left. */ 13723 seg_len += rgap; 13724 if (seg_len <= 0) { 13725 /* 13726 * Resets are only valid if they lie within our offered 13727 * window. If the RST bit is set, we just ignore this 13728 * segment. 13729 */ 13730 if (flags & TH_RST) { 13731 freemsg(mp); 13732 return; 13733 } 13734 13735 /* Per RFC 793, we need to send back an ACK. */ 13736 flags |= TH_ACK_NEEDED; 13737 13738 /* 13739 * Send SIGURG as soon as possible i.e. even 13740 * if the TH_URG was delivered in a window probe 13741 * packet (which will be unacceptable). 13742 * 13743 * We generate a signal if none has been generated 13744 * for this connection or if this is a new urgent 13745 * byte. Also send a zero-length "unmarked" message 13746 * to inform SIOCATMARK that this is not the mark. 13747 * 13748 * tcp_urp_last_valid is cleared when the T_exdata_ind 13749 * is sent up. This plus the check for old data 13750 * (gap >= 0) handles the wraparound of the sequence 13751 * number space without having to always track the 13752 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13753 * this max in its rcv_up variable). 13754 * 13755 * This prevents duplicate SIGURGS due to a "late" 13756 * zero-window probe when the T_EXDATA_IND has already 13757 * been sent up. 13758 */ 13759 if ((flags & TH_URG) && 13760 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13761 tcp->tcp_urp_last))) { 13762 mp1 = allocb(0, BPRI_MED); 13763 if (mp1 == NULL) { 13764 freemsg(mp); 13765 return; 13766 } 13767 if (!TCP_IS_DETACHED(tcp) && 13768 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13769 SIGURG)) { 13770 /* Try again on the rexmit. */ 13771 freemsg(mp1); 13772 freemsg(mp); 13773 return; 13774 } 13775 /* 13776 * If the next byte would be the mark 13777 * then mark with MARKNEXT else mark 13778 * with NOTMARKNEXT. 13779 */ 13780 if (gap == 0 && urp == 0) 13781 mp1->b_flag |= MSGMARKNEXT; 13782 else 13783 mp1->b_flag |= MSGNOTMARKNEXT; 13784 freemsg(tcp->tcp_urp_mark_mp); 13785 tcp->tcp_urp_mark_mp = mp1; 13786 flags |= TH_SEND_URP_MARK; 13787 tcp->tcp_urp_last_valid = B_TRUE; 13788 tcp->tcp_urp_last = urp + seg_seq; 13789 } 13790 /* 13791 * If this is a zero window probe, continue to 13792 * process the ACK part. But we need to set seg_len 13793 * to 0 to avoid data processing. Otherwise just 13794 * drop the segment and send back an ACK. 13795 */ 13796 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13797 flags &= ~(TH_SYN | TH_URG); 13798 seg_len = 0; 13799 goto process_ack; 13800 } else { 13801 freemsg(mp); 13802 goto ack_check; 13803 } 13804 } 13805 /* Pitch out of window stuff off the end. */ 13806 rgap = seg_len; 13807 mp2 = mp; 13808 do { 13809 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13810 (uintptr_t)INT_MAX); 13811 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13812 if (rgap < 0) { 13813 mp2->b_wptr += rgap; 13814 if ((mp1 = mp2->b_cont) != NULL) { 13815 mp2->b_cont = NULL; 13816 freemsg(mp1); 13817 } 13818 break; 13819 } 13820 } while ((mp2 = mp2->b_cont) != NULL); 13821 } 13822 ok:; 13823 /* 13824 * TCP should check ECN info for segments inside the window only. 13825 * Therefore the check should be done here. 13826 */ 13827 if (tcp->tcp_ecn_ok) { 13828 if (flags & TH_CWR) { 13829 tcp->tcp_ecn_echo_on = B_FALSE; 13830 } 13831 /* 13832 * Note that both ECN_CE and CWR can be set in the 13833 * same segment. In this case, we once again turn 13834 * on ECN_ECHO. 13835 */ 13836 if (tcp->tcp_ipversion == IPV4_VERSION) { 13837 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13838 13839 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13840 tcp->tcp_ecn_echo_on = B_TRUE; 13841 } 13842 } else { 13843 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13844 13845 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13846 htonl(IPH_ECN_CE << 20)) { 13847 tcp->tcp_ecn_echo_on = B_TRUE; 13848 } 13849 } 13850 } 13851 13852 /* 13853 * Check whether we can update tcp_ts_recent. This test is 13854 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13855 * Extensions for High Performance: An Update", Internet Draft. 13856 */ 13857 if (tcp->tcp_snd_ts_ok && 13858 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13859 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13860 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13861 tcp->tcp_last_rcv_lbolt = lbolt64; 13862 } 13863 13864 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13865 /* 13866 * FIN in an out of order segment. We record this in 13867 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13868 * Clear the FIN so that any check on FIN flag will fail. 13869 * Remember that FIN also counts in the sequence number 13870 * space. So we need to ack out of order FIN only segments. 13871 */ 13872 if (flags & TH_FIN) { 13873 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13874 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13875 flags &= ~TH_FIN; 13876 flags |= TH_ACK_NEEDED; 13877 } 13878 if (seg_len > 0) { 13879 /* Fill in the SACK blk list. */ 13880 if (tcp->tcp_snd_sack_ok) { 13881 ASSERT(tcp->tcp_sack_info != NULL); 13882 tcp_sack_insert(tcp->tcp_sack_list, 13883 seg_seq, seg_seq + seg_len, 13884 &(tcp->tcp_num_sack_blk)); 13885 } 13886 13887 /* 13888 * Attempt reassembly and see if we have something 13889 * ready to go. 13890 */ 13891 mp = tcp_reass(tcp, mp, seg_seq); 13892 /* Always ack out of order packets */ 13893 flags |= TH_ACK_NEEDED | TH_PUSH; 13894 if (mp) { 13895 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13896 (uintptr_t)INT_MAX); 13897 seg_len = mp->b_cont ? msgdsize(mp) : 13898 (int)(mp->b_wptr - mp->b_rptr); 13899 seg_seq = tcp->tcp_rnxt; 13900 /* 13901 * A gap is filled and the seq num and len 13902 * of the gap match that of a previously 13903 * received FIN, put the FIN flag back in. 13904 */ 13905 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13906 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13907 flags |= TH_FIN; 13908 tcp->tcp_valid_bits &= 13909 ~TCP_OFO_FIN_VALID; 13910 } 13911 } else { 13912 /* 13913 * Keep going even with NULL mp. 13914 * There may be a useful ACK or something else 13915 * we don't want to miss. 13916 * 13917 * But TCP should not perform fast retransmit 13918 * because of the ack number. TCP uses 13919 * seg_len == 0 to determine if it is a pure 13920 * ACK. And this is not a pure ACK. 13921 */ 13922 seg_len = 0; 13923 ofo_seg = B_TRUE; 13924 } 13925 } 13926 } else if (seg_len > 0) { 13927 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13928 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13929 /* 13930 * If an out of order FIN was received before, and the seq 13931 * num and len of the new segment match that of the FIN, 13932 * put the FIN flag back in. 13933 */ 13934 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13935 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13936 flags |= TH_FIN; 13937 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13938 } 13939 } 13940 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13941 if (flags & TH_RST) { 13942 freemsg(mp); 13943 switch (tcp->tcp_state) { 13944 case TCPS_SYN_RCVD: 13945 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13946 break; 13947 case TCPS_ESTABLISHED: 13948 case TCPS_FIN_WAIT_1: 13949 case TCPS_FIN_WAIT_2: 13950 case TCPS_CLOSE_WAIT: 13951 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13952 break; 13953 case TCPS_CLOSING: 13954 case TCPS_LAST_ACK: 13955 (void) tcp_clean_death(tcp, 0, 16); 13956 break; 13957 default: 13958 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13959 (void) tcp_clean_death(tcp, ENXIO, 17); 13960 break; 13961 } 13962 return; 13963 } 13964 if (flags & TH_SYN) { 13965 /* 13966 * See RFC 793, Page 71 13967 * 13968 * The seq number must be in the window as it should 13969 * be "fixed" above. If it is outside window, it should 13970 * be already rejected. Note that we allow seg_seq to be 13971 * rnxt + rwnd because we want to accept 0 window probe. 13972 */ 13973 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13974 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13975 freemsg(mp); 13976 /* 13977 * If the ACK flag is not set, just use our snxt as the 13978 * seq number of the RST segment. 13979 */ 13980 if (!(flags & TH_ACK)) { 13981 seg_ack = tcp->tcp_snxt; 13982 } 13983 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13984 TH_RST|TH_ACK); 13985 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13986 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13987 return; 13988 } 13989 /* 13990 * urp could be -1 when the urp field in the packet is 0 13991 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13992 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13993 */ 13994 if (flags & TH_URG && urp >= 0) { 13995 if (!tcp->tcp_urp_last_valid || 13996 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13997 /* 13998 * If we haven't generated the signal yet for this 13999 * urgent pointer value, do it now. Also, send up a 14000 * zero-length M_DATA indicating whether or not this is 14001 * the mark. The latter is not needed when a 14002 * T_EXDATA_IND is sent up. However, if there are 14003 * allocation failures this code relies on the sender 14004 * retransmitting and the socket code for determining 14005 * the mark should not block waiting for the peer to 14006 * transmit. Thus, for simplicity we always send up the 14007 * mark indication. 14008 */ 14009 mp1 = allocb(0, BPRI_MED); 14010 if (mp1 == NULL) { 14011 freemsg(mp); 14012 return; 14013 } 14014 if (!TCP_IS_DETACHED(tcp) && 14015 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 14016 /* Try again on the rexmit. */ 14017 freemsg(mp1); 14018 freemsg(mp); 14019 return; 14020 } 14021 /* 14022 * Mark with NOTMARKNEXT for now. 14023 * The code below will change this to MARKNEXT 14024 * if we are at the mark. 14025 * 14026 * If there are allocation failures (e.g. in dupmsg 14027 * below) the next time tcp_rput_data sees the urgent 14028 * segment it will send up the MSG*MARKNEXT message. 14029 */ 14030 mp1->b_flag |= MSGNOTMARKNEXT; 14031 freemsg(tcp->tcp_urp_mark_mp); 14032 tcp->tcp_urp_mark_mp = mp1; 14033 flags |= TH_SEND_URP_MARK; 14034 #ifdef DEBUG 14035 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14036 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 14037 "last %x, %s", 14038 seg_seq, urp, tcp->tcp_urp_last, 14039 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14040 #endif /* DEBUG */ 14041 tcp->tcp_urp_last_valid = B_TRUE; 14042 tcp->tcp_urp_last = urp + seg_seq; 14043 } else if (tcp->tcp_urp_mark_mp != NULL) { 14044 /* 14045 * An allocation failure prevented the previous 14046 * tcp_rput_data from sending up the allocated 14047 * MSG*MARKNEXT message - send it up this time 14048 * around. 14049 */ 14050 flags |= TH_SEND_URP_MARK; 14051 } 14052 14053 /* 14054 * If the urgent byte is in this segment, make sure that it is 14055 * all by itself. This makes it much easier to deal with the 14056 * possibility of an allocation failure on the T_exdata_ind. 14057 * Note that seg_len is the number of bytes in the segment, and 14058 * urp is the offset into the segment of the urgent byte. 14059 * urp < seg_len means that the urgent byte is in this segment. 14060 */ 14061 if (urp < seg_len) { 14062 if (seg_len != 1) { 14063 uint32_t tmp_rnxt; 14064 /* 14065 * Break it up and feed it back in. 14066 * Re-attach the IP header. 14067 */ 14068 mp->b_rptr = iphdr; 14069 if (urp > 0) { 14070 /* 14071 * There is stuff before the urgent 14072 * byte. 14073 */ 14074 mp1 = dupmsg(mp); 14075 if (!mp1) { 14076 /* 14077 * Trim from urgent byte on. 14078 * The rest will come back. 14079 */ 14080 (void) adjmsg(mp, 14081 urp - seg_len); 14082 tcp_rput_data(connp, 14083 mp, NULL); 14084 return; 14085 } 14086 (void) adjmsg(mp1, urp - seg_len); 14087 /* Feed this piece back in. */ 14088 tmp_rnxt = tcp->tcp_rnxt; 14089 tcp_rput_data(connp, mp1, NULL); 14090 /* 14091 * If the data passed back in was not 14092 * processed (ie: bad ACK) sending 14093 * the remainder back in will cause a 14094 * loop. In this case, drop the 14095 * packet and let the sender try 14096 * sending a good packet. 14097 */ 14098 if (tmp_rnxt == tcp->tcp_rnxt) { 14099 freemsg(mp); 14100 return; 14101 } 14102 } 14103 if (urp != seg_len - 1) { 14104 uint32_t tmp_rnxt; 14105 /* 14106 * There is stuff after the urgent 14107 * byte. 14108 */ 14109 mp1 = dupmsg(mp); 14110 if (!mp1) { 14111 /* 14112 * Trim everything beyond the 14113 * urgent byte. The rest will 14114 * come back. 14115 */ 14116 (void) adjmsg(mp, 14117 urp + 1 - seg_len); 14118 tcp_rput_data(connp, 14119 mp, NULL); 14120 return; 14121 } 14122 (void) adjmsg(mp1, urp + 1 - seg_len); 14123 tmp_rnxt = tcp->tcp_rnxt; 14124 tcp_rput_data(connp, mp1, NULL); 14125 /* 14126 * If the data passed back in was not 14127 * processed (ie: bad ACK) sending 14128 * the remainder back in will cause a 14129 * loop. In this case, drop the 14130 * packet and let the sender try 14131 * sending a good packet. 14132 */ 14133 if (tmp_rnxt == tcp->tcp_rnxt) { 14134 freemsg(mp); 14135 return; 14136 } 14137 } 14138 tcp_rput_data(connp, mp, NULL); 14139 return; 14140 } 14141 /* 14142 * This segment contains only the urgent byte. We 14143 * have to allocate the T_exdata_ind, if we can. 14144 */ 14145 if (!tcp->tcp_urp_mp) { 14146 struct T_exdata_ind *tei; 14147 mp1 = allocb(sizeof (struct T_exdata_ind), 14148 BPRI_MED); 14149 if (!mp1) { 14150 /* 14151 * Sigh... It'll be back. 14152 * Generate any MSG*MARK message now. 14153 */ 14154 freemsg(mp); 14155 seg_len = 0; 14156 if (flags & TH_SEND_URP_MARK) { 14157 14158 14159 ASSERT(tcp->tcp_urp_mark_mp); 14160 tcp->tcp_urp_mark_mp->b_flag &= 14161 ~MSGNOTMARKNEXT; 14162 tcp->tcp_urp_mark_mp->b_flag |= 14163 MSGMARKNEXT; 14164 } 14165 goto ack_check; 14166 } 14167 mp1->b_datap->db_type = M_PROTO; 14168 tei = (struct T_exdata_ind *)mp1->b_rptr; 14169 tei->PRIM_type = T_EXDATA_IND; 14170 tei->MORE_flag = 0; 14171 mp1->b_wptr = (uchar_t *)&tei[1]; 14172 tcp->tcp_urp_mp = mp1; 14173 #ifdef DEBUG 14174 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14175 "tcp_rput: allocated exdata_ind %s", 14176 tcp_display(tcp, NULL, 14177 DISP_PORT_ONLY)); 14178 #endif /* DEBUG */ 14179 /* 14180 * There is no need to send a separate MSG*MARK 14181 * message since the T_EXDATA_IND will be sent 14182 * now. 14183 */ 14184 flags &= ~TH_SEND_URP_MARK; 14185 freemsg(tcp->tcp_urp_mark_mp); 14186 tcp->tcp_urp_mark_mp = NULL; 14187 } 14188 /* 14189 * Now we are all set. On the next putnext upstream, 14190 * tcp_urp_mp will be non-NULL and will get prepended 14191 * to what has to be this piece containing the urgent 14192 * byte. If for any reason we abort this segment below, 14193 * if it comes back, we will have this ready, or it 14194 * will get blown off in close. 14195 */ 14196 } else if (urp == seg_len) { 14197 /* 14198 * The urgent byte is the next byte after this sequence 14199 * number. If there is data it is marked with 14200 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14201 * since it is not needed. Otherwise, if the code 14202 * above just allocated a zero-length tcp_urp_mark_mp 14203 * message, that message is tagged with MSGMARKNEXT. 14204 * Sending up these MSGMARKNEXT messages makes 14205 * SIOCATMARK work correctly even though 14206 * the T_EXDATA_IND will not be sent up until the 14207 * urgent byte arrives. 14208 */ 14209 if (seg_len != 0) { 14210 flags |= TH_MARKNEXT_NEEDED; 14211 freemsg(tcp->tcp_urp_mark_mp); 14212 tcp->tcp_urp_mark_mp = NULL; 14213 flags &= ~TH_SEND_URP_MARK; 14214 } else if (tcp->tcp_urp_mark_mp != NULL) { 14215 flags |= TH_SEND_URP_MARK; 14216 tcp->tcp_urp_mark_mp->b_flag &= 14217 ~MSGNOTMARKNEXT; 14218 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14219 } 14220 #ifdef DEBUG 14221 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14222 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14223 seg_len, flags, 14224 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14225 #endif /* DEBUG */ 14226 } else { 14227 /* Data left until we hit mark */ 14228 #ifdef DEBUG 14229 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14230 "tcp_rput: URP %d bytes left, %s", 14231 urp - seg_len, tcp_display(tcp, NULL, 14232 DISP_PORT_ONLY)); 14233 #endif /* DEBUG */ 14234 } 14235 } 14236 14237 process_ack: 14238 if (!(flags & TH_ACK)) { 14239 freemsg(mp); 14240 goto xmit_check; 14241 } 14242 } 14243 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14244 14245 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14246 tcp->tcp_ip_forward_progress = B_TRUE; 14247 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14248 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14249 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14250 /* 3-way handshake complete - pass up the T_CONN_IND */ 14251 tcp_t *listener = tcp->tcp_listener; 14252 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14253 14254 tcp->tcp_tconnind_started = B_TRUE; 14255 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14256 /* 14257 * We are here means eager is fine but it can 14258 * get a TH_RST at any point between now and till 14259 * accept completes and disappear. We need to 14260 * ensure that reference to eager is valid after 14261 * we get out of eager's perimeter. So we do 14262 * an extra refhold. 14263 */ 14264 CONN_INC_REF(connp); 14265 14266 /* 14267 * The listener also exists because of the refhold 14268 * done in tcp_conn_request. Its possible that it 14269 * might have closed. We will check that once we 14270 * get inside listeners context. 14271 */ 14272 CONN_INC_REF(listener->tcp_connp); 14273 if (listener->tcp_connp->conn_sqp == 14274 connp->conn_sqp) { 14275 tcp_send_conn_ind(listener->tcp_connp, mp, 14276 listener->tcp_connp->conn_sqp); 14277 CONN_DEC_REF(listener->tcp_connp); 14278 } else if (!tcp->tcp_loopback) { 14279 squeue_fill(listener->tcp_connp->conn_sqp, mp, 14280 tcp_send_conn_ind, 14281 listener->tcp_connp, SQTAG_TCP_CONN_IND); 14282 } else { 14283 squeue_enter(listener->tcp_connp->conn_sqp, mp, 14284 tcp_send_conn_ind, listener->tcp_connp, 14285 SQTAG_TCP_CONN_IND); 14286 } 14287 } 14288 14289 if (tcp->tcp_active_open) { 14290 /* 14291 * We are seeing the final ack in the three way 14292 * hand shake of a active open'ed connection 14293 * so we must send up a T_CONN_CON 14294 */ 14295 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14296 freemsg(mp); 14297 return; 14298 } 14299 /* 14300 * Don't fuse the loopback endpoints for 14301 * simultaneous active opens. 14302 */ 14303 if (tcp->tcp_loopback) { 14304 TCP_STAT(tcps, tcp_fusion_unfusable); 14305 tcp->tcp_unfusable = B_TRUE; 14306 } 14307 } 14308 14309 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14310 bytes_acked--; 14311 /* SYN was acked - making progress */ 14312 if (tcp->tcp_ipversion == IPV6_VERSION) 14313 tcp->tcp_ip_forward_progress = B_TRUE; 14314 14315 /* 14316 * If SYN was retransmitted, need to reset all 14317 * retransmission info as this segment will be 14318 * treated as a dup ACK. 14319 */ 14320 if (tcp->tcp_rexmit) { 14321 tcp->tcp_rexmit = B_FALSE; 14322 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14323 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14324 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14325 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14326 tcp->tcp_ms_we_have_waited = 0; 14327 tcp->tcp_cwnd = mss; 14328 } 14329 14330 /* 14331 * We set the send window to zero here. 14332 * This is needed if there is data to be 14333 * processed already on the queue. 14334 * Later (at swnd_update label), the 14335 * "new_swnd > tcp_swnd" condition is satisfied 14336 * the XMIT_NEEDED flag is set in the current 14337 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14338 * called if there is already data on queue in 14339 * this state. 14340 */ 14341 tcp->tcp_swnd = 0; 14342 14343 if (new_swnd > tcp->tcp_max_swnd) 14344 tcp->tcp_max_swnd = new_swnd; 14345 tcp->tcp_swl1 = seg_seq; 14346 tcp->tcp_swl2 = seg_ack; 14347 tcp->tcp_state = TCPS_ESTABLISHED; 14348 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14349 14350 /* Fuse when both sides are in ESTABLISHED state */ 14351 if (tcp->tcp_loopback && do_tcp_fusion) 14352 tcp_fuse(tcp, iphdr, tcph); 14353 14354 } 14355 /* This code follows 4.4BSD-Lite2 mostly. */ 14356 if (bytes_acked < 0) 14357 goto est; 14358 14359 /* 14360 * If TCP is ECN capable and the congestion experience bit is 14361 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14362 * done once per window (or more loosely, per RTT). 14363 */ 14364 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14365 tcp->tcp_cwr = B_FALSE; 14366 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14367 if (!tcp->tcp_cwr) { 14368 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14369 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14370 tcp->tcp_cwnd = npkt * mss; 14371 /* 14372 * If the cwnd is 0, use the timer to clock out 14373 * new segments. This is required by the ECN spec. 14374 */ 14375 if (npkt == 0) { 14376 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14377 /* 14378 * This makes sure that when the ACK comes 14379 * back, we will increase tcp_cwnd by 1 MSS. 14380 */ 14381 tcp->tcp_cwnd_cnt = 0; 14382 } 14383 tcp->tcp_cwr = B_TRUE; 14384 /* 14385 * This marks the end of the current window of in 14386 * flight data. That is why we don't use 14387 * tcp_suna + tcp_swnd. Only data in flight can 14388 * provide ECN info. 14389 */ 14390 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14391 tcp->tcp_ecn_cwr_sent = B_FALSE; 14392 } 14393 } 14394 14395 mp1 = tcp->tcp_xmit_head; 14396 if (bytes_acked == 0) { 14397 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14398 int dupack_cnt; 14399 14400 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14401 /* 14402 * Fast retransmit. When we have seen exactly three 14403 * identical ACKs while we have unacked data 14404 * outstanding we take it as a hint that our peer 14405 * dropped something. 14406 * 14407 * If TCP is retransmitting, don't do fast retransmit. 14408 */ 14409 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14410 ! tcp->tcp_rexmit) { 14411 /* Do Limited Transmit */ 14412 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14413 tcps->tcps_dupack_fast_retransmit) { 14414 /* 14415 * RFC 3042 14416 * 14417 * What we need to do is temporarily 14418 * increase tcp_cwnd so that new 14419 * data can be sent if it is allowed 14420 * by the receive window (tcp_rwnd). 14421 * tcp_wput_data() will take care of 14422 * the rest. 14423 * 14424 * If the connection is SACK capable, 14425 * only do limited xmit when there 14426 * is SACK info. 14427 * 14428 * Note how tcp_cwnd is incremented. 14429 * The first dup ACK will increase 14430 * it by 1 MSS. The second dup ACK 14431 * will increase it by 2 MSS. This 14432 * means that only 1 new segment will 14433 * be sent for each dup ACK. 14434 */ 14435 if (tcp->tcp_unsent > 0 && 14436 (!tcp->tcp_snd_sack_ok || 14437 (tcp->tcp_snd_sack_ok && 14438 tcp->tcp_notsack_list != NULL))) { 14439 tcp->tcp_cwnd += mss << 14440 (tcp->tcp_dupack_cnt - 1); 14441 flags |= TH_LIMIT_XMIT; 14442 } 14443 } else if (dupack_cnt == 14444 tcps->tcps_dupack_fast_retransmit) { 14445 14446 /* 14447 * If we have reduced tcp_ssthresh 14448 * because of ECN, do not reduce it again 14449 * unless it is already one window of data 14450 * away. After one window of data, tcp_cwr 14451 * should then be cleared. Note that 14452 * for non ECN capable connection, tcp_cwr 14453 * should always be false. 14454 * 14455 * Adjust cwnd since the duplicate 14456 * ack indicates that a packet was 14457 * dropped (due to congestion.) 14458 */ 14459 if (!tcp->tcp_cwr) { 14460 npkt = ((tcp->tcp_snxt - 14461 tcp->tcp_suna) >> 1) / mss; 14462 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14463 mss; 14464 tcp->tcp_cwnd = (npkt + 14465 tcp->tcp_dupack_cnt) * mss; 14466 } 14467 if (tcp->tcp_ecn_ok) { 14468 tcp->tcp_cwr = B_TRUE; 14469 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14470 tcp->tcp_ecn_cwr_sent = B_FALSE; 14471 } 14472 14473 /* 14474 * We do Hoe's algorithm. Refer to her 14475 * paper "Improving the Start-up Behavior 14476 * of a Congestion Control Scheme for TCP," 14477 * appeared in SIGCOMM'96. 14478 * 14479 * Save highest seq no we have sent so far. 14480 * Be careful about the invisible FIN byte. 14481 */ 14482 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14483 (tcp->tcp_unsent == 0)) { 14484 tcp->tcp_rexmit_max = tcp->tcp_fss; 14485 } else { 14486 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14487 } 14488 14489 /* 14490 * Do not allow bursty traffic during. 14491 * fast recovery. Refer to Fall and Floyd's 14492 * paper "Simulation-based Comparisons of 14493 * Tahoe, Reno and SACK TCP" (in CCR?) 14494 * This is a best current practise. 14495 */ 14496 tcp->tcp_snd_burst = TCP_CWND_SS; 14497 14498 /* 14499 * For SACK: 14500 * Calculate tcp_pipe, which is the 14501 * estimated number of bytes in 14502 * network. 14503 * 14504 * tcp_fack is the highest sack'ed seq num 14505 * TCP has received. 14506 * 14507 * tcp_pipe is explained in the above quoted 14508 * Fall and Floyd's paper. tcp_fack is 14509 * explained in Mathis and Mahdavi's 14510 * "Forward Acknowledgment: Refining TCP 14511 * Congestion Control" in SIGCOMM '96. 14512 */ 14513 if (tcp->tcp_snd_sack_ok) { 14514 ASSERT(tcp->tcp_sack_info != NULL); 14515 if (tcp->tcp_notsack_list != NULL) { 14516 tcp->tcp_pipe = tcp->tcp_snxt - 14517 tcp->tcp_fack; 14518 tcp->tcp_sack_snxt = seg_ack; 14519 flags |= TH_NEED_SACK_REXMIT; 14520 } else { 14521 /* 14522 * Always initialize tcp_pipe 14523 * even though we don't have 14524 * any SACK info. If later 14525 * we get SACK info and 14526 * tcp_pipe is not initialized, 14527 * funny things will happen. 14528 */ 14529 tcp->tcp_pipe = 14530 tcp->tcp_cwnd_ssthresh; 14531 } 14532 } else { 14533 flags |= TH_REXMIT_NEEDED; 14534 } /* tcp_snd_sack_ok */ 14535 14536 } else { 14537 /* 14538 * Here we perform congestion 14539 * avoidance, but NOT slow start. 14540 * This is known as the Fast 14541 * Recovery Algorithm. 14542 */ 14543 if (tcp->tcp_snd_sack_ok && 14544 tcp->tcp_notsack_list != NULL) { 14545 flags |= TH_NEED_SACK_REXMIT; 14546 tcp->tcp_pipe -= mss; 14547 if (tcp->tcp_pipe < 0) 14548 tcp->tcp_pipe = 0; 14549 } else { 14550 /* 14551 * We know that one more packet has 14552 * left the pipe thus we can update 14553 * cwnd. 14554 */ 14555 cwnd = tcp->tcp_cwnd + mss; 14556 if (cwnd > tcp->tcp_cwnd_max) 14557 cwnd = tcp->tcp_cwnd_max; 14558 tcp->tcp_cwnd = cwnd; 14559 if (tcp->tcp_unsent > 0) 14560 flags |= TH_XMIT_NEEDED; 14561 } 14562 } 14563 } 14564 } else if (tcp->tcp_zero_win_probe) { 14565 /* 14566 * If the window has opened, need to arrange 14567 * to send additional data. 14568 */ 14569 if (new_swnd != 0) { 14570 /* tcp_suna != tcp_snxt */ 14571 /* Packet contains a window update */ 14572 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14573 tcp->tcp_zero_win_probe = 0; 14574 tcp->tcp_timer_backoff = 0; 14575 tcp->tcp_ms_we_have_waited = 0; 14576 14577 /* 14578 * Transmit starting with tcp_suna since 14579 * the one byte probe is not ack'ed. 14580 * If TCP has sent more than one identical 14581 * probe, tcp_rexmit will be set. That means 14582 * tcp_ss_rexmit() will send out the one 14583 * byte along with new data. Otherwise, 14584 * fake the retransmission. 14585 */ 14586 flags |= TH_XMIT_NEEDED; 14587 if (!tcp->tcp_rexmit) { 14588 tcp->tcp_rexmit = B_TRUE; 14589 tcp->tcp_dupack_cnt = 0; 14590 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14591 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14592 } 14593 } 14594 } 14595 goto swnd_update; 14596 } 14597 14598 /* 14599 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14600 * If the ACK value acks something that we have not yet sent, it might 14601 * be an old duplicate segment. Send an ACK to re-synchronize the 14602 * other side. 14603 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14604 * state is handled above, so we can always just drop the segment and 14605 * send an ACK here. 14606 * 14607 * Should we send ACKs in response to ACK only segments? 14608 */ 14609 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14610 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14611 /* drop the received segment */ 14612 freemsg(mp); 14613 14614 /* 14615 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14616 * greater than 0, check if the number of such 14617 * bogus ACks is greater than that count. If yes, 14618 * don't send back any ACK. This prevents TCP from 14619 * getting into an ACK storm if somehow an attacker 14620 * successfully spoofs an acceptable segment to our 14621 * peer. 14622 */ 14623 if (tcp_drop_ack_unsent_cnt > 0 && 14624 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14625 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14626 return; 14627 } 14628 mp = tcp_ack_mp(tcp); 14629 if (mp != NULL) { 14630 BUMP_LOCAL(tcp->tcp_obsegs); 14631 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14632 tcp_send_data(tcp, tcp->tcp_wq, mp); 14633 } 14634 return; 14635 } 14636 14637 /* 14638 * TCP gets a new ACK, update the notsack'ed list to delete those 14639 * blocks that are covered by this ACK. 14640 */ 14641 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14642 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14643 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14644 } 14645 14646 /* 14647 * If we got an ACK after fast retransmit, check to see 14648 * if it is a partial ACK. If it is not and the congestion 14649 * window was inflated to account for the other side's 14650 * cached packets, retract it. If it is, do Hoe's algorithm. 14651 */ 14652 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14653 ASSERT(tcp->tcp_rexmit == B_FALSE); 14654 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14655 tcp->tcp_dupack_cnt = 0; 14656 /* 14657 * Restore the orig tcp_cwnd_ssthresh after 14658 * fast retransmit phase. 14659 */ 14660 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14661 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14662 } 14663 tcp->tcp_rexmit_max = seg_ack; 14664 tcp->tcp_cwnd_cnt = 0; 14665 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14666 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14667 14668 /* 14669 * Remove all notsack info to avoid confusion with 14670 * the next fast retrasnmit/recovery phase. 14671 */ 14672 if (tcp->tcp_snd_sack_ok && 14673 tcp->tcp_notsack_list != NULL) { 14674 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14675 } 14676 } else { 14677 if (tcp->tcp_snd_sack_ok && 14678 tcp->tcp_notsack_list != NULL) { 14679 flags |= TH_NEED_SACK_REXMIT; 14680 tcp->tcp_pipe -= mss; 14681 if (tcp->tcp_pipe < 0) 14682 tcp->tcp_pipe = 0; 14683 } else { 14684 /* 14685 * Hoe's algorithm: 14686 * 14687 * Retransmit the unack'ed segment and 14688 * restart fast recovery. Note that we 14689 * need to scale back tcp_cwnd to the 14690 * original value when we started fast 14691 * recovery. This is to prevent overly 14692 * aggressive behaviour in sending new 14693 * segments. 14694 */ 14695 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14696 tcps->tcps_dupack_fast_retransmit * mss; 14697 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14698 flags |= TH_REXMIT_NEEDED; 14699 } 14700 } 14701 } else { 14702 tcp->tcp_dupack_cnt = 0; 14703 if (tcp->tcp_rexmit) { 14704 /* 14705 * TCP is retranmitting. If the ACK ack's all 14706 * outstanding data, update tcp_rexmit_max and 14707 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14708 * to the correct value. 14709 * 14710 * Note that SEQ_LEQ() is used. This is to avoid 14711 * unnecessary fast retransmit caused by dup ACKs 14712 * received when TCP does slow start retransmission 14713 * after a time out. During this phase, TCP may 14714 * send out segments which are already received. 14715 * This causes dup ACKs to be sent back. 14716 */ 14717 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14718 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14719 tcp->tcp_rexmit_nxt = seg_ack; 14720 } 14721 if (seg_ack != tcp->tcp_rexmit_max) { 14722 flags |= TH_XMIT_NEEDED; 14723 } 14724 } else { 14725 tcp->tcp_rexmit = B_FALSE; 14726 tcp->tcp_xmit_zc_clean = B_FALSE; 14727 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14728 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14729 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14730 } 14731 tcp->tcp_ms_we_have_waited = 0; 14732 } 14733 } 14734 14735 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14736 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14737 tcp->tcp_suna = seg_ack; 14738 if (tcp->tcp_zero_win_probe != 0) { 14739 tcp->tcp_zero_win_probe = 0; 14740 tcp->tcp_timer_backoff = 0; 14741 } 14742 14743 /* 14744 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14745 * Note that it cannot be the SYN being ack'ed. The code flow 14746 * will not reach here. 14747 */ 14748 if (mp1 == NULL) { 14749 goto fin_acked; 14750 } 14751 14752 /* 14753 * Update the congestion window. 14754 * 14755 * If TCP is not ECN capable or TCP is ECN capable but the 14756 * congestion experience bit is not set, increase the tcp_cwnd as 14757 * usual. 14758 */ 14759 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14760 cwnd = tcp->tcp_cwnd; 14761 add = mss; 14762 14763 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14764 /* 14765 * This is to prevent an increase of less than 1 MSS of 14766 * tcp_cwnd. With partial increase, tcp_wput_data() 14767 * may send out tinygrams in order to preserve mblk 14768 * boundaries. 14769 * 14770 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14771 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14772 * increased by 1 MSS for every RTTs. 14773 */ 14774 if (tcp->tcp_cwnd_cnt <= 0) { 14775 tcp->tcp_cwnd_cnt = cwnd + add; 14776 } else { 14777 tcp->tcp_cwnd_cnt -= add; 14778 add = 0; 14779 } 14780 } 14781 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14782 } 14783 14784 /* See if the latest urgent data has been acknowledged */ 14785 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14786 SEQ_GT(seg_ack, tcp->tcp_urg)) 14787 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14788 14789 /* Can we update the RTT estimates? */ 14790 if (tcp->tcp_snd_ts_ok) { 14791 /* Ignore zero timestamp echo-reply. */ 14792 if (tcpopt.tcp_opt_ts_ecr != 0) { 14793 tcp_set_rto(tcp, (int32_t)lbolt - 14794 (int32_t)tcpopt.tcp_opt_ts_ecr); 14795 } 14796 14797 /* If needed, restart the timer. */ 14798 if (tcp->tcp_set_timer == 1) { 14799 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14800 tcp->tcp_set_timer = 0; 14801 } 14802 /* 14803 * Update tcp_csuna in case the other side stops sending 14804 * us timestamps. 14805 */ 14806 tcp->tcp_csuna = tcp->tcp_snxt; 14807 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14808 /* 14809 * An ACK sequence we haven't seen before, so get the RTT 14810 * and update the RTO. But first check if the timestamp is 14811 * valid to use. 14812 */ 14813 if ((mp1->b_next != NULL) && 14814 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14815 tcp_set_rto(tcp, (int32_t)lbolt - 14816 (int32_t)(intptr_t)mp1->b_prev); 14817 else 14818 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14819 14820 /* Remeber the last sequence to be ACKed */ 14821 tcp->tcp_csuna = seg_ack; 14822 if (tcp->tcp_set_timer == 1) { 14823 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14824 tcp->tcp_set_timer = 0; 14825 } 14826 } else { 14827 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14828 } 14829 14830 /* Eat acknowledged bytes off the xmit queue. */ 14831 for (;;) { 14832 mblk_t *mp2; 14833 uchar_t *wptr; 14834 14835 wptr = mp1->b_wptr; 14836 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14837 bytes_acked -= (int)(wptr - mp1->b_rptr); 14838 if (bytes_acked < 0) { 14839 mp1->b_rptr = wptr + bytes_acked; 14840 /* 14841 * Set a new timestamp if all the bytes timed by the 14842 * old timestamp have been ack'ed. 14843 */ 14844 if (SEQ_GT(seg_ack, 14845 (uint32_t)(uintptr_t)(mp1->b_next))) { 14846 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14847 mp1->b_next = NULL; 14848 } 14849 break; 14850 } 14851 mp1->b_next = NULL; 14852 mp1->b_prev = NULL; 14853 mp2 = mp1; 14854 mp1 = mp1->b_cont; 14855 14856 /* 14857 * This notification is required for some zero-copy 14858 * clients to maintain a copy semantic. After the data 14859 * is ack'ed, client is safe to modify or reuse the buffer. 14860 */ 14861 if (tcp->tcp_snd_zcopy_aware && 14862 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14863 tcp_zcopy_notify(tcp); 14864 freeb(mp2); 14865 if (bytes_acked == 0) { 14866 if (mp1 == NULL) { 14867 /* Everything is ack'ed, clear the tail. */ 14868 tcp->tcp_xmit_tail = NULL; 14869 /* 14870 * Cancel the timer unless we are still 14871 * waiting for an ACK for the FIN packet. 14872 */ 14873 if (tcp->tcp_timer_tid != 0 && 14874 tcp->tcp_snxt == tcp->tcp_suna) { 14875 (void) TCP_TIMER_CANCEL(tcp, 14876 tcp->tcp_timer_tid); 14877 tcp->tcp_timer_tid = 0; 14878 } 14879 goto pre_swnd_update; 14880 } 14881 if (mp2 != tcp->tcp_xmit_tail) 14882 break; 14883 tcp->tcp_xmit_tail = mp1; 14884 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14885 (uintptr_t)INT_MAX); 14886 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14887 mp1->b_rptr); 14888 break; 14889 } 14890 if (mp1 == NULL) { 14891 /* 14892 * More was acked but there is nothing more 14893 * outstanding. This means that the FIN was 14894 * just acked or that we're talking to a clown. 14895 */ 14896 fin_acked: 14897 ASSERT(tcp->tcp_fin_sent); 14898 tcp->tcp_xmit_tail = NULL; 14899 if (tcp->tcp_fin_sent) { 14900 /* FIN was acked - making progress */ 14901 if (tcp->tcp_ipversion == IPV6_VERSION && 14902 !tcp->tcp_fin_acked) 14903 tcp->tcp_ip_forward_progress = B_TRUE; 14904 tcp->tcp_fin_acked = B_TRUE; 14905 if (tcp->tcp_linger_tid != 0 && 14906 TCP_TIMER_CANCEL(tcp, 14907 tcp->tcp_linger_tid) >= 0) { 14908 tcp_stop_lingering(tcp); 14909 freemsg(mp); 14910 mp = NULL; 14911 } 14912 } else { 14913 /* 14914 * We should never get here because 14915 * we have already checked that the 14916 * number of bytes ack'ed should be 14917 * smaller than or equal to what we 14918 * have sent so far (it is the 14919 * acceptability check of the ACK). 14920 * We can only get here if the send 14921 * queue is corrupted. 14922 * 14923 * Terminate the connection and 14924 * panic the system. It is better 14925 * for us to panic instead of 14926 * continuing to avoid other disaster. 14927 */ 14928 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14929 tcp->tcp_rnxt, TH_RST|TH_ACK); 14930 panic("Memory corruption " 14931 "detected for connection %s.", 14932 tcp_display(tcp, NULL, 14933 DISP_ADDR_AND_PORT)); 14934 /*NOTREACHED*/ 14935 } 14936 goto pre_swnd_update; 14937 } 14938 ASSERT(mp2 != tcp->tcp_xmit_tail); 14939 } 14940 if (tcp->tcp_unsent) { 14941 flags |= TH_XMIT_NEEDED; 14942 } 14943 pre_swnd_update: 14944 tcp->tcp_xmit_head = mp1; 14945 swnd_update: 14946 /* 14947 * The following check is different from most other implementations. 14948 * For bi-directional transfer, when segments are dropped, the 14949 * "normal" check will not accept a window update in those 14950 * retransmitted segemnts. Failing to do that, TCP may send out 14951 * segments which are outside receiver's window. As TCP accepts 14952 * the ack in those retransmitted segments, if the window update in 14953 * the same segment is not accepted, TCP will incorrectly calculates 14954 * that it can send more segments. This can create a deadlock 14955 * with the receiver if its window becomes zero. 14956 */ 14957 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14958 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14959 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14960 /* 14961 * The criteria for update is: 14962 * 14963 * 1. the segment acknowledges some data. Or 14964 * 2. the segment is new, i.e. it has a higher seq num. Or 14965 * 3. the segment is not old and the advertised window is 14966 * larger than the previous advertised window. 14967 */ 14968 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14969 flags |= TH_XMIT_NEEDED; 14970 tcp->tcp_swnd = new_swnd; 14971 if (new_swnd > tcp->tcp_max_swnd) 14972 tcp->tcp_max_swnd = new_swnd; 14973 tcp->tcp_swl1 = seg_seq; 14974 tcp->tcp_swl2 = seg_ack; 14975 } 14976 est: 14977 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14978 14979 switch (tcp->tcp_state) { 14980 case TCPS_FIN_WAIT_1: 14981 if (tcp->tcp_fin_acked) { 14982 tcp->tcp_state = TCPS_FIN_WAIT_2; 14983 /* 14984 * We implement the non-standard BSD/SunOS 14985 * FIN_WAIT_2 flushing algorithm. 14986 * If there is no user attached to this 14987 * TCP endpoint, then this TCP struct 14988 * could hang around forever in FIN_WAIT_2 14989 * state if the peer forgets to send us 14990 * a FIN. To prevent this, we wait only 14991 * 2*MSL (a convenient time value) for 14992 * the FIN to arrive. If it doesn't show up, 14993 * we flush the TCP endpoint. This algorithm, 14994 * though a violation of RFC-793, has worked 14995 * for over 10 years in BSD systems. 14996 * Note: SunOS 4.x waits 675 seconds before 14997 * flushing the FIN_WAIT_2 connection. 14998 */ 14999 TCP_TIMER_RESTART(tcp, 15000 tcps->tcps_fin_wait_2_flush_interval); 15001 } 15002 break; 15003 case TCPS_FIN_WAIT_2: 15004 break; /* Shutdown hook? */ 15005 case TCPS_LAST_ACK: 15006 freemsg(mp); 15007 if (tcp->tcp_fin_acked) { 15008 (void) tcp_clean_death(tcp, 0, 19); 15009 return; 15010 } 15011 goto xmit_check; 15012 case TCPS_CLOSING: 15013 if (tcp->tcp_fin_acked) { 15014 tcp->tcp_state = TCPS_TIME_WAIT; 15015 /* 15016 * Unconditionally clear the exclusive binding 15017 * bit so this TIME-WAIT connection won't 15018 * interfere with new ones. 15019 */ 15020 tcp->tcp_exclbind = 0; 15021 if (!TCP_IS_DETACHED(tcp)) { 15022 TCP_TIMER_RESTART(tcp, 15023 tcps->tcps_time_wait_interval); 15024 } else { 15025 tcp_time_wait_append(tcp); 15026 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15027 } 15028 } 15029 /*FALLTHRU*/ 15030 case TCPS_CLOSE_WAIT: 15031 freemsg(mp); 15032 goto xmit_check; 15033 default: 15034 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 15035 break; 15036 } 15037 } 15038 if (flags & TH_FIN) { 15039 /* Make sure we ack the fin */ 15040 flags |= TH_ACK_NEEDED; 15041 if (!tcp->tcp_fin_rcvd) { 15042 tcp->tcp_fin_rcvd = B_TRUE; 15043 tcp->tcp_rnxt++; 15044 tcph = tcp->tcp_tcph; 15045 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15046 15047 /* 15048 * Generate the ordrel_ind at the end unless we 15049 * are an eager guy. 15050 * In the eager case tcp_rsrv will do this when run 15051 * after tcp_accept is done. 15052 */ 15053 if (tcp->tcp_listener == NULL && 15054 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 15055 flags |= TH_ORDREL_NEEDED; 15056 switch (tcp->tcp_state) { 15057 case TCPS_SYN_RCVD: 15058 case TCPS_ESTABLISHED: 15059 tcp->tcp_state = TCPS_CLOSE_WAIT; 15060 /* Keepalive? */ 15061 break; 15062 case TCPS_FIN_WAIT_1: 15063 if (!tcp->tcp_fin_acked) { 15064 tcp->tcp_state = TCPS_CLOSING; 15065 break; 15066 } 15067 /* FALLTHRU */ 15068 case TCPS_FIN_WAIT_2: 15069 tcp->tcp_state = TCPS_TIME_WAIT; 15070 /* 15071 * Unconditionally clear the exclusive binding 15072 * bit so this TIME-WAIT connection won't 15073 * interfere with new ones. 15074 */ 15075 tcp->tcp_exclbind = 0; 15076 if (!TCP_IS_DETACHED(tcp)) { 15077 TCP_TIMER_RESTART(tcp, 15078 tcps->tcps_time_wait_interval); 15079 } else { 15080 tcp_time_wait_append(tcp); 15081 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15082 } 15083 if (seg_len) { 15084 /* 15085 * implies data piggybacked on FIN. 15086 * break to handle data. 15087 */ 15088 break; 15089 } 15090 freemsg(mp); 15091 goto ack_check; 15092 } 15093 } 15094 } 15095 if (mp == NULL) 15096 goto xmit_check; 15097 if (seg_len == 0) { 15098 freemsg(mp); 15099 goto xmit_check; 15100 } 15101 if (mp->b_rptr == mp->b_wptr) { 15102 /* 15103 * The header has been consumed, so we remove the 15104 * zero-length mblk here. 15105 */ 15106 mp1 = mp; 15107 mp = mp->b_cont; 15108 freeb(mp1); 15109 } 15110 tcph = tcp->tcp_tcph; 15111 tcp->tcp_rack_cnt++; 15112 { 15113 uint32_t cur_max; 15114 15115 cur_max = tcp->tcp_rack_cur_max; 15116 if (tcp->tcp_rack_cnt >= cur_max) { 15117 /* 15118 * We have more unacked data than we should - send 15119 * an ACK now. 15120 */ 15121 flags |= TH_ACK_NEEDED; 15122 cur_max++; 15123 if (cur_max > tcp->tcp_rack_abs_max) 15124 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15125 else 15126 tcp->tcp_rack_cur_max = cur_max; 15127 } else if (TCP_IS_DETACHED(tcp)) { 15128 /* We don't have an ACK timer for detached TCP. */ 15129 flags |= TH_ACK_NEEDED; 15130 } else if (seg_len < mss) { 15131 /* 15132 * If we get a segment that is less than an mss, and we 15133 * already have unacknowledged data, and the amount 15134 * unacknowledged is not a multiple of mss, then we 15135 * better generate an ACK now. Otherwise, this may be 15136 * the tail piece of a transaction, and we would rather 15137 * wait for the response. 15138 */ 15139 uint32_t udif; 15140 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15141 (uintptr_t)INT_MAX); 15142 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15143 if (udif && (udif % mss)) 15144 flags |= TH_ACK_NEEDED; 15145 else 15146 flags |= TH_ACK_TIMER_NEEDED; 15147 } else { 15148 /* Start delayed ack timer */ 15149 flags |= TH_ACK_TIMER_NEEDED; 15150 } 15151 } 15152 tcp->tcp_rnxt += seg_len; 15153 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15154 15155 /* Update SACK list */ 15156 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15157 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15158 &(tcp->tcp_num_sack_blk)); 15159 } 15160 15161 if (tcp->tcp_urp_mp) { 15162 tcp->tcp_urp_mp->b_cont = mp; 15163 mp = tcp->tcp_urp_mp; 15164 tcp->tcp_urp_mp = NULL; 15165 /* Ready for a new signal. */ 15166 tcp->tcp_urp_last_valid = B_FALSE; 15167 #ifdef DEBUG 15168 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15169 "tcp_rput: sending exdata_ind %s", 15170 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15171 #endif /* DEBUG */ 15172 } 15173 15174 /* 15175 * Check for ancillary data changes compared to last segment. 15176 */ 15177 if (tcp->tcp_ipv6_recvancillary != 0) { 15178 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15179 ASSERT(mp != NULL); 15180 } 15181 15182 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15183 /* 15184 * Side queue inbound data until the accept happens. 15185 * tcp_accept/tcp_rput drains this when the accept happens. 15186 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15187 * T_EXDATA_IND) it is queued on b_next. 15188 * XXX Make urgent data use this. Requires: 15189 * Removing tcp_listener check for TH_URG 15190 * Making M_PCPROTO and MARK messages skip the eager case 15191 */ 15192 15193 if (tcp->tcp_kssl_pending) { 15194 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15195 mblk_t *, mp); 15196 tcp_kssl_input(tcp, mp); 15197 } else { 15198 tcp_rcv_enqueue(tcp, mp, seg_len); 15199 } 15200 } else { 15201 sodirect_t *sodp = tcp->tcp_sodirect; 15202 15203 /* 15204 * If an sodirect connection and an enabled sodirect_t then 15205 * sodp will be set to point to the tcp_t/sonode_t shared 15206 * sodirect_t and the sodirect_t's lock will be held. 15207 */ 15208 if (sodp != NULL) { 15209 mutex_enter(sodp->sod_lockp); 15210 if (!(sodp->sod_state & SOD_ENABLED) || 15211 (tcp->tcp_kssl_ctx != NULL && 15212 DB_TYPE(mp) == M_DATA)) { 15213 mutex_exit(sodp->sod_lockp); 15214 sodp = NULL; 15215 } 15216 } 15217 if (mp->b_datap->db_type != M_DATA || 15218 (flags & TH_MARKNEXT_NEEDED)) { 15219 if (sodp != NULL) { 15220 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15221 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15222 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15223 } 15224 if (!SOD_QEMPTY(sodp) && 15225 (sodp->sod_state & SOD_WAKE_NOT)) { 15226 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15227 /* sod_wakeup() did the mutex_exit() */ 15228 } else { 15229 mutex_exit(sodp->sod_lockp); 15230 } 15231 } else if (tcp->tcp_rcv_list != NULL) { 15232 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15233 } 15234 ASSERT(tcp->tcp_rcv_list == NULL || 15235 tcp->tcp_fused_sigurg); 15236 15237 if (flags & TH_MARKNEXT_NEEDED) { 15238 #ifdef DEBUG 15239 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15240 "tcp_rput: sending MSGMARKNEXT %s", 15241 tcp_display(tcp, NULL, 15242 DISP_PORT_ONLY)); 15243 #endif /* DEBUG */ 15244 mp->b_flag |= MSGMARKNEXT; 15245 flags &= ~TH_MARKNEXT_NEEDED; 15246 } 15247 15248 /* Does this need SSL processing first? */ 15249 if ((tcp->tcp_kssl_ctx != NULL) && 15250 (DB_TYPE(mp) == M_DATA)) { 15251 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15252 mblk_t *, mp); 15253 tcp_kssl_input(tcp, mp); 15254 } else { 15255 putnext(tcp->tcp_rq, mp); 15256 if (!canputnext(tcp->tcp_rq)) 15257 tcp->tcp_rwnd -= seg_len; 15258 } 15259 } else if ((tcp->tcp_kssl_ctx != NULL) && 15260 (DB_TYPE(mp) == M_DATA)) { 15261 /* Do SSL processing first */ 15262 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, 15263 mblk_t *, mp); 15264 tcp_kssl_input(tcp, mp); 15265 } else if (sodp != NULL) { 15266 /* 15267 * Sodirect so all mblk_t's are queued on the 15268 * socket directly, check for wakeup of blocked 15269 * reader (if any), and last if flow-controled. 15270 */ 15271 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15272 if ((sodp->sod_state & SOD_WAKE_NEED) || 15273 (flags & (TH_PUSH|TH_FIN))) { 15274 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15275 /* sod_wakeup() did the mutex_exit() */ 15276 } else { 15277 if (SOD_QFULL(sodp)) { 15278 /* Q is full, need backenable */ 15279 SOD_QSETBE(sodp); 15280 } 15281 mutex_exit(sodp->sod_lockp); 15282 } 15283 } else if ((flags & (TH_PUSH|TH_FIN)) || 15284 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 15285 if (tcp->tcp_rcv_list != NULL) { 15286 /* 15287 * Enqueue the new segment first and then 15288 * call tcp_rcv_drain() to send all data 15289 * up. The other way to do this is to 15290 * send all queued data up and then call 15291 * putnext() to send the new segment up. 15292 * This way can remove the else part later 15293 * on. 15294 * 15295 * We don't this to avoid one more call to 15296 * canputnext() as tcp_rcv_drain() needs to 15297 * call canputnext(). 15298 */ 15299 tcp_rcv_enqueue(tcp, mp, seg_len); 15300 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15301 } else { 15302 putnext(tcp->tcp_rq, mp); 15303 if (!canputnext(tcp->tcp_rq)) 15304 tcp->tcp_rwnd -= seg_len; 15305 } 15306 } else { 15307 /* 15308 * Enqueue all packets when processing an mblk 15309 * from the co queue and also enqueue normal packets. 15310 */ 15311 tcp_rcv_enqueue(tcp, mp, seg_len); 15312 } 15313 /* 15314 * Make sure the timer is running if we have data waiting 15315 * for a push bit. This provides resiliency against 15316 * implementations that do not correctly generate push bits. 15317 * 15318 * Note, for sodirect if Q isn't empty and there's not a 15319 * pending wakeup then we need a timer. Also note that sodp 15320 * is assumed to be still valid after exit()ing the sod_lockp 15321 * above and while the SOD state can change it can only change 15322 * such that the Q is empty now even though data was added 15323 * above. 15324 */ 15325 if (((sodp != NULL && !SOD_QEMPTY(sodp) && 15326 (sodp->sod_state & SOD_WAKE_NOT)) || 15327 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15328 tcp->tcp_push_tid == 0) { 15329 /* 15330 * The connection may be closed at this point, so don't 15331 * do anything for a detached tcp. 15332 */ 15333 if (!TCP_IS_DETACHED(tcp)) 15334 tcp->tcp_push_tid = TCP_TIMER(tcp, 15335 tcp_push_timer, 15336 MSEC_TO_TICK( 15337 tcps->tcps_push_timer_interval)); 15338 } 15339 } 15340 15341 xmit_check: 15342 /* Is there anything left to do? */ 15343 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15344 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15345 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15346 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15347 goto done; 15348 15349 /* Any transmit work to do and a non-zero window? */ 15350 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15351 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15352 if (flags & TH_REXMIT_NEEDED) { 15353 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15354 15355 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15356 if (snd_size > mss) 15357 snd_size = mss; 15358 if (snd_size > tcp->tcp_swnd) 15359 snd_size = tcp->tcp_swnd; 15360 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15361 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15362 B_TRUE); 15363 15364 if (mp1 != NULL) { 15365 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15366 tcp->tcp_csuna = tcp->tcp_snxt; 15367 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15368 UPDATE_MIB(&tcps->tcps_mib, 15369 tcpRetransBytes, snd_size); 15370 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15371 } 15372 } 15373 if (flags & TH_NEED_SACK_REXMIT) { 15374 tcp_sack_rxmit(tcp, &flags); 15375 } 15376 /* 15377 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15378 * out new segment. Note that tcp_rexmit should not be 15379 * set, otherwise TH_LIMIT_XMIT should not be set. 15380 */ 15381 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15382 if (!tcp->tcp_rexmit) { 15383 tcp_wput_data(tcp, NULL, B_FALSE); 15384 } else { 15385 tcp_ss_rexmit(tcp); 15386 } 15387 } 15388 /* 15389 * Adjust tcp_cwnd back to normal value after sending 15390 * new data segments. 15391 */ 15392 if (flags & TH_LIMIT_XMIT) { 15393 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15394 /* 15395 * This will restart the timer. Restarting the 15396 * timer is used to avoid a timeout before the 15397 * limited transmitted segment's ACK gets back. 15398 */ 15399 if (tcp->tcp_xmit_head != NULL) 15400 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15401 } 15402 15403 /* Anything more to do? */ 15404 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15405 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15406 goto done; 15407 } 15408 ack_check: 15409 if (flags & TH_SEND_URP_MARK) { 15410 ASSERT(tcp->tcp_urp_mark_mp); 15411 /* 15412 * Send up any queued data and then send the mark message 15413 */ 15414 sodirect_t *sodp; 15415 15416 SOD_PTR_ENTER(tcp, sodp); 15417 15418 mp1 = tcp->tcp_urp_mark_mp; 15419 tcp->tcp_urp_mark_mp = NULL; 15420 if (sodp != NULL) { 15421 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15422 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15423 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15424 } 15425 ASSERT(tcp->tcp_rcv_list == NULL); 15426 15427 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15428 /* sod_wakeup() does the mutex_exit() */ 15429 } else if (tcp->tcp_rcv_list != NULL) { 15430 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15431 15432 ASSERT(tcp->tcp_rcv_list == NULL || 15433 tcp->tcp_fused_sigurg); 15434 15435 } 15436 putnext(tcp->tcp_rq, mp1); 15437 #ifdef DEBUG 15438 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15439 "tcp_rput: sending zero-length %s %s", 15440 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15441 "MSGNOTMARKNEXT"), 15442 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15443 #endif /* DEBUG */ 15444 flags &= ~TH_SEND_URP_MARK; 15445 } 15446 if (flags & TH_ACK_NEEDED) { 15447 /* 15448 * Time to send an ack for some reason. 15449 */ 15450 mp1 = tcp_ack_mp(tcp); 15451 15452 if (mp1 != NULL) { 15453 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15454 BUMP_LOCAL(tcp->tcp_obsegs); 15455 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15456 } 15457 if (tcp->tcp_ack_tid != 0) { 15458 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15459 tcp->tcp_ack_tid = 0; 15460 } 15461 } 15462 if (flags & TH_ACK_TIMER_NEEDED) { 15463 /* 15464 * Arrange for deferred ACK or push wait timeout. 15465 * Start timer if it is not already running. 15466 */ 15467 if (tcp->tcp_ack_tid == 0) { 15468 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15469 MSEC_TO_TICK(tcp->tcp_localnet ? 15470 (clock_t)tcps->tcps_local_dack_interval : 15471 (clock_t)tcps->tcps_deferred_ack_interval)); 15472 } 15473 } 15474 if (flags & TH_ORDREL_NEEDED) { 15475 /* 15476 * Send up the ordrel_ind unless we are an eager guy. 15477 * In the eager case tcp_rsrv will do this when run 15478 * after tcp_accept is done. 15479 */ 15480 sodirect_t *sodp; 15481 15482 ASSERT(tcp->tcp_listener == NULL); 15483 15484 SOD_PTR_ENTER(tcp, sodp); 15485 if (sodp != NULL) { 15486 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15487 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15488 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15489 } 15490 /* No more sodirect */ 15491 tcp->tcp_sodirect = NULL; 15492 if (!SOD_QEMPTY(sodp)) { 15493 /* Mblk(s) to process, notify */ 15494 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15495 /* sod_wakeup() does the mutex_exit() */ 15496 } else { 15497 /* Nothing to process */ 15498 mutex_exit(sodp->sod_lockp); 15499 } 15500 } else if (tcp->tcp_rcv_list != NULL) { 15501 /* 15502 * Push any mblk(s) enqueued from co processing. 15503 */ 15504 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 15505 15506 ASSERT(tcp->tcp_rcv_list == NULL || 15507 tcp->tcp_fused_sigurg); 15508 } 15509 15510 mp1 = tcp->tcp_ordrel_mp; 15511 tcp->tcp_ordrel_mp = NULL; 15512 tcp->tcp_ordrel_done = B_TRUE; 15513 putnext(tcp->tcp_rq, mp1); 15514 } 15515 done: 15516 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15517 } 15518 15519 /* 15520 * This function does PAWS protection check. Returns B_TRUE if the 15521 * segment passes the PAWS test, else returns B_FALSE. 15522 */ 15523 boolean_t 15524 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15525 { 15526 uint8_t flags; 15527 int options; 15528 uint8_t *up; 15529 15530 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15531 /* 15532 * If timestamp option is aligned nicely, get values inline, 15533 * otherwise call general routine to parse. Only do that 15534 * if timestamp is the only option. 15535 */ 15536 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15537 TCPOPT_REAL_TS_LEN && 15538 OK_32PTR((up = ((uint8_t *)tcph) + 15539 TCP_MIN_HEADER_LENGTH)) && 15540 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15541 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15542 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15543 15544 options = TCP_OPT_TSTAMP_PRESENT; 15545 } else { 15546 if (tcp->tcp_snd_sack_ok) { 15547 tcpoptp->tcp = tcp; 15548 } else { 15549 tcpoptp->tcp = NULL; 15550 } 15551 options = tcp_parse_options(tcph, tcpoptp); 15552 } 15553 15554 if (options & TCP_OPT_TSTAMP_PRESENT) { 15555 /* 15556 * Do PAWS per RFC 1323 section 4.2. Accept RST 15557 * regardless of the timestamp, page 18 RFC 1323.bis. 15558 */ 15559 if ((flags & TH_RST) == 0 && 15560 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15561 tcp->tcp_ts_recent)) { 15562 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15563 PAWS_TIMEOUT)) { 15564 /* This segment is not acceptable. */ 15565 return (B_FALSE); 15566 } else { 15567 /* 15568 * Connection has been idle for 15569 * too long. Reset the timestamp 15570 * and assume the segment is valid. 15571 */ 15572 tcp->tcp_ts_recent = 15573 tcpoptp->tcp_opt_ts_val; 15574 } 15575 } 15576 } else { 15577 /* 15578 * If we don't get a timestamp on every packet, we 15579 * figure we can't really trust 'em, so we stop sending 15580 * and parsing them. 15581 */ 15582 tcp->tcp_snd_ts_ok = B_FALSE; 15583 15584 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15585 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15586 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15587 /* 15588 * Adjust the tcp_mss accordingly. We also need to 15589 * adjust tcp_cwnd here in accordance with the new mss. 15590 * But we avoid doing a slow start here so as to not 15591 * to lose on the transfer rate built up so far. 15592 */ 15593 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15594 if (tcp->tcp_snd_sack_ok) { 15595 ASSERT(tcp->tcp_sack_info != NULL); 15596 tcp->tcp_max_sack_blk = 4; 15597 } 15598 } 15599 return (B_TRUE); 15600 } 15601 15602 /* 15603 * Attach ancillary data to a received TCP segments for the 15604 * ancillary pieces requested by the application that are 15605 * different than they were in the previous data segment. 15606 * 15607 * Save the "current" values once memory allocation is ok so that 15608 * when memory allocation fails we can just wait for the next data segment. 15609 */ 15610 static mblk_t * 15611 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15612 { 15613 struct T_optdata_ind *todi; 15614 int optlen; 15615 uchar_t *optptr; 15616 struct T_opthdr *toh; 15617 uint_t addflag; /* Which pieces to add */ 15618 mblk_t *mp1; 15619 15620 optlen = 0; 15621 addflag = 0; 15622 /* If app asked for pktinfo and the index has changed ... */ 15623 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15624 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15625 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15626 optlen += sizeof (struct T_opthdr) + 15627 sizeof (struct in6_pktinfo); 15628 addflag |= TCP_IPV6_RECVPKTINFO; 15629 } 15630 /* If app asked for hoplimit and it has changed ... */ 15631 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15632 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15633 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15634 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15635 addflag |= TCP_IPV6_RECVHOPLIMIT; 15636 } 15637 /* If app asked for tclass and it has changed ... */ 15638 if ((ipp->ipp_fields & IPPF_TCLASS) && 15639 ipp->ipp_tclass != tcp->tcp_recvtclass && 15640 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15641 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15642 addflag |= TCP_IPV6_RECVTCLASS; 15643 } 15644 /* 15645 * If app asked for hopbyhop headers and it has changed ... 15646 * For security labels, note that (1) security labels can't change on 15647 * a connected socket at all, (2) we're connected to at most one peer, 15648 * (3) if anything changes, then it must be some other extra option. 15649 */ 15650 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15651 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15652 (ipp->ipp_fields & IPPF_HOPOPTS), 15653 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15654 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15655 tcp->tcp_label_len; 15656 addflag |= TCP_IPV6_RECVHOPOPTS; 15657 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15658 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15659 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15660 return (mp); 15661 } 15662 /* If app asked for dst headers before routing headers ... */ 15663 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15664 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15665 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15666 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15667 optlen += sizeof (struct T_opthdr) + 15668 ipp->ipp_rtdstoptslen; 15669 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15670 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15671 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15672 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15673 return (mp); 15674 } 15675 /* If app asked for routing headers and it has changed ... */ 15676 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15677 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15678 (ipp->ipp_fields & IPPF_RTHDR), 15679 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15680 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15681 addflag |= TCP_IPV6_RECVRTHDR; 15682 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15683 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15684 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15685 return (mp); 15686 } 15687 /* If app asked for dest headers and it has changed ... */ 15688 if ((tcp->tcp_ipv6_recvancillary & 15689 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15690 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15691 (ipp->ipp_fields & IPPF_DSTOPTS), 15692 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15693 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15694 addflag |= TCP_IPV6_RECVDSTOPTS; 15695 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15696 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15697 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15698 return (mp); 15699 } 15700 15701 if (optlen == 0) { 15702 /* Nothing to add */ 15703 return (mp); 15704 } 15705 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15706 if (mp1 == NULL) { 15707 /* 15708 * Defer sending ancillary data until the next TCP segment 15709 * arrives. 15710 */ 15711 return (mp); 15712 } 15713 mp1->b_cont = mp; 15714 mp = mp1; 15715 mp->b_wptr += sizeof (*todi) + optlen; 15716 mp->b_datap->db_type = M_PROTO; 15717 todi = (struct T_optdata_ind *)mp->b_rptr; 15718 todi->PRIM_type = T_OPTDATA_IND; 15719 todi->DATA_flag = 1; /* MORE data */ 15720 todi->OPT_length = optlen; 15721 todi->OPT_offset = sizeof (*todi); 15722 optptr = (uchar_t *)&todi[1]; 15723 /* 15724 * If app asked for pktinfo and the index has changed ... 15725 * Note that the local address never changes for the connection. 15726 */ 15727 if (addflag & TCP_IPV6_RECVPKTINFO) { 15728 struct in6_pktinfo *pkti; 15729 15730 toh = (struct T_opthdr *)optptr; 15731 toh->level = IPPROTO_IPV6; 15732 toh->name = IPV6_PKTINFO; 15733 toh->len = sizeof (*toh) + sizeof (*pkti); 15734 toh->status = 0; 15735 optptr += sizeof (*toh); 15736 pkti = (struct in6_pktinfo *)optptr; 15737 if (tcp->tcp_ipversion == IPV6_VERSION) 15738 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15739 else 15740 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15741 &pkti->ipi6_addr); 15742 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15743 optptr += sizeof (*pkti); 15744 ASSERT(OK_32PTR(optptr)); 15745 /* Save as "last" value */ 15746 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15747 } 15748 /* If app asked for hoplimit and it has changed ... */ 15749 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15750 toh = (struct T_opthdr *)optptr; 15751 toh->level = IPPROTO_IPV6; 15752 toh->name = IPV6_HOPLIMIT; 15753 toh->len = sizeof (*toh) + sizeof (uint_t); 15754 toh->status = 0; 15755 optptr += sizeof (*toh); 15756 *(uint_t *)optptr = ipp->ipp_hoplimit; 15757 optptr += sizeof (uint_t); 15758 ASSERT(OK_32PTR(optptr)); 15759 /* Save as "last" value */ 15760 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15761 } 15762 /* If app asked for tclass and it has changed ... */ 15763 if (addflag & TCP_IPV6_RECVTCLASS) { 15764 toh = (struct T_opthdr *)optptr; 15765 toh->level = IPPROTO_IPV6; 15766 toh->name = IPV6_TCLASS; 15767 toh->len = sizeof (*toh) + sizeof (uint_t); 15768 toh->status = 0; 15769 optptr += sizeof (*toh); 15770 *(uint_t *)optptr = ipp->ipp_tclass; 15771 optptr += sizeof (uint_t); 15772 ASSERT(OK_32PTR(optptr)); 15773 /* Save as "last" value */ 15774 tcp->tcp_recvtclass = ipp->ipp_tclass; 15775 } 15776 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15777 toh = (struct T_opthdr *)optptr; 15778 toh->level = IPPROTO_IPV6; 15779 toh->name = IPV6_HOPOPTS; 15780 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15781 tcp->tcp_label_len; 15782 toh->status = 0; 15783 optptr += sizeof (*toh); 15784 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15785 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15786 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15787 ASSERT(OK_32PTR(optptr)); 15788 /* Save as last value */ 15789 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15790 (ipp->ipp_fields & IPPF_HOPOPTS), 15791 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15792 } 15793 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15794 toh = (struct T_opthdr *)optptr; 15795 toh->level = IPPROTO_IPV6; 15796 toh->name = IPV6_RTHDRDSTOPTS; 15797 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15798 toh->status = 0; 15799 optptr += sizeof (*toh); 15800 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15801 optptr += ipp->ipp_rtdstoptslen; 15802 ASSERT(OK_32PTR(optptr)); 15803 /* Save as last value */ 15804 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15805 &tcp->tcp_rtdstoptslen, 15806 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15807 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15808 } 15809 if (addflag & TCP_IPV6_RECVRTHDR) { 15810 toh = (struct T_opthdr *)optptr; 15811 toh->level = IPPROTO_IPV6; 15812 toh->name = IPV6_RTHDR; 15813 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15814 toh->status = 0; 15815 optptr += sizeof (*toh); 15816 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15817 optptr += ipp->ipp_rthdrlen; 15818 ASSERT(OK_32PTR(optptr)); 15819 /* Save as last value */ 15820 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15821 (ipp->ipp_fields & IPPF_RTHDR), 15822 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15823 } 15824 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15825 toh = (struct T_opthdr *)optptr; 15826 toh->level = IPPROTO_IPV6; 15827 toh->name = IPV6_DSTOPTS; 15828 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15829 toh->status = 0; 15830 optptr += sizeof (*toh); 15831 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15832 optptr += ipp->ipp_dstoptslen; 15833 ASSERT(OK_32PTR(optptr)); 15834 /* Save as last value */ 15835 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15836 (ipp->ipp_fields & IPPF_DSTOPTS), 15837 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15838 } 15839 ASSERT(optptr == mp->b_wptr); 15840 return (mp); 15841 } 15842 15843 15844 /* 15845 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15846 * or a "bad" IRE detected by tcp_adapt_ire. 15847 * We can't tell if the failure was due to the laddr or the faddr 15848 * thus we clear out all addresses and ports. 15849 */ 15850 static void 15851 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15852 { 15853 queue_t *q = tcp->tcp_rq; 15854 tcph_t *tcph; 15855 struct T_error_ack *tea; 15856 conn_t *connp = tcp->tcp_connp; 15857 15858 15859 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15860 15861 if (mp->b_cont) { 15862 freemsg(mp->b_cont); 15863 mp->b_cont = NULL; 15864 } 15865 tea = (struct T_error_ack *)mp->b_rptr; 15866 switch (tea->PRIM_type) { 15867 case T_BIND_ACK: 15868 /* 15869 * Need to unbind with classifier since we were just told that 15870 * our bind succeeded. 15871 */ 15872 tcp->tcp_hard_bound = B_FALSE; 15873 tcp->tcp_hard_binding = B_FALSE; 15874 15875 ipcl_hash_remove(connp); 15876 /* Reuse the mblk if possible */ 15877 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15878 sizeof (*tea)); 15879 mp->b_rptr = mp->b_datap->db_base; 15880 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15881 tea = (struct T_error_ack *)mp->b_rptr; 15882 tea->PRIM_type = T_ERROR_ACK; 15883 tea->TLI_error = TSYSERR; 15884 tea->UNIX_error = error; 15885 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15886 tea->ERROR_prim = T_CONN_REQ; 15887 } else { 15888 tea->ERROR_prim = O_T_BIND_REQ; 15889 } 15890 break; 15891 15892 case T_ERROR_ACK: 15893 if (tcp->tcp_state >= TCPS_SYN_SENT) 15894 tea->ERROR_prim = T_CONN_REQ; 15895 break; 15896 default: 15897 panic("tcp_bind_failed: unexpected TPI type"); 15898 /*NOTREACHED*/ 15899 } 15900 15901 tcp->tcp_state = TCPS_IDLE; 15902 if (tcp->tcp_ipversion == IPV4_VERSION) 15903 tcp->tcp_ipha->ipha_src = 0; 15904 else 15905 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15906 /* 15907 * Copy of the src addr. in tcp_t is needed since 15908 * the lookup funcs. can only look at tcp_t 15909 */ 15910 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15911 15912 tcph = tcp->tcp_tcph; 15913 tcph->th_lport[0] = 0; 15914 tcph->th_lport[1] = 0; 15915 tcp_bind_hash_remove(tcp); 15916 bzero(&connp->u_port, sizeof (connp->u_port)); 15917 /* blow away saved option results if any */ 15918 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15919 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15920 15921 conn_delete_ire(tcp->tcp_connp, NULL); 15922 putnext(q, mp); 15923 } 15924 15925 /* 15926 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15927 * messages. 15928 */ 15929 void 15930 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15931 { 15932 mblk_t *mp1; 15933 uchar_t *rptr = mp->b_rptr; 15934 queue_t *q = tcp->tcp_rq; 15935 struct T_error_ack *tea; 15936 uint32_t mss; 15937 mblk_t *syn_mp; 15938 mblk_t *mdti; 15939 mblk_t *lsoi; 15940 int retval; 15941 mblk_t *ire_mp; 15942 tcp_stack_t *tcps = tcp->tcp_tcps; 15943 15944 switch (mp->b_datap->db_type) { 15945 case M_PROTO: 15946 case M_PCPROTO: 15947 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15948 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15949 break; 15950 tea = (struct T_error_ack *)rptr; 15951 switch (tea->PRIM_type) { 15952 case T_BIND_ACK: 15953 /* 15954 * Adapt Multidata information, if any. The 15955 * following tcp_mdt_update routine will free 15956 * the message. 15957 */ 15958 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15959 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15960 b_rptr)->mdt_capab, B_TRUE); 15961 freemsg(mdti); 15962 } 15963 15964 /* 15965 * Check to update LSO information with tcp, and 15966 * tcp_lso_update routine will free the message. 15967 */ 15968 if ((lsoi = tcp_lso_info_mp(mp)) != NULL) { 15969 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 15970 b_rptr)->lso_capab); 15971 freemsg(lsoi); 15972 } 15973 15974 /* Get the IRE, if we had requested for it */ 15975 ire_mp = tcp_ire_mp(mp); 15976 15977 if (tcp->tcp_hard_binding) { 15978 tcp->tcp_hard_binding = B_FALSE; 15979 tcp->tcp_hard_bound = B_TRUE; 15980 CL_INET_CONNECT(tcp); 15981 } else { 15982 if (ire_mp != NULL) 15983 freeb(ire_mp); 15984 goto after_syn_sent; 15985 } 15986 15987 retval = tcp_adapt_ire(tcp, ire_mp); 15988 if (ire_mp != NULL) 15989 freeb(ire_mp); 15990 if (retval == 0) { 15991 tcp_bind_failed(tcp, mp, 15992 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15993 ENETUNREACH : EADDRNOTAVAIL)); 15994 return; 15995 } 15996 /* 15997 * Don't let an endpoint connect to itself. 15998 * Also checked in tcp_connect() but that 15999 * check can't handle the case when the 16000 * local IP address is INADDR_ANY. 16001 */ 16002 if (tcp->tcp_ipversion == IPV4_VERSION) { 16003 if ((tcp->tcp_ipha->ipha_dst == 16004 tcp->tcp_ipha->ipha_src) && 16005 (BE16_EQL(tcp->tcp_tcph->th_lport, 16006 tcp->tcp_tcph->th_fport))) { 16007 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16008 return; 16009 } 16010 } else { 16011 if (IN6_ARE_ADDR_EQUAL( 16012 &tcp->tcp_ip6h->ip6_dst, 16013 &tcp->tcp_ip6h->ip6_src) && 16014 (BE16_EQL(tcp->tcp_tcph->th_lport, 16015 tcp->tcp_tcph->th_fport))) { 16016 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 16017 return; 16018 } 16019 } 16020 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 16021 /* 16022 * This should not be possible! Just for 16023 * defensive coding... 16024 */ 16025 if (tcp->tcp_state != TCPS_SYN_SENT) 16026 goto after_syn_sent; 16027 16028 if (is_system_labeled() && 16029 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 16030 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 16031 return; 16032 } 16033 16034 ASSERT(q == tcp->tcp_rq); 16035 /* 16036 * tcp_adapt_ire() does not adjust 16037 * for TCP/IP header length. 16038 */ 16039 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 16040 16041 /* 16042 * Just make sure our rwnd is at 16043 * least tcp_recv_hiwat_mss * MSS 16044 * large, and round up to the nearest 16045 * MSS. 16046 * 16047 * We do the round up here because 16048 * we need to get the interface 16049 * MTU first before we can do the 16050 * round up. 16051 */ 16052 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 16053 tcps->tcps_recv_hiwat_minmss * mss); 16054 q->q_hiwat = tcp->tcp_rwnd; 16055 tcp_set_ws_value(tcp); 16056 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 16057 tcp->tcp_tcph->th_win); 16058 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 16059 tcp->tcp_snd_ws_ok = B_TRUE; 16060 16061 /* 16062 * Set tcp_snd_ts_ok to true 16063 * so that tcp_xmit_mp will 16064 * include the timestamp 16065 * option in the SYN segment. 16066 */ 16067 if (tcps->tcps_tstamp_always || 16068 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 16069 tcp->tcp_snd_ts_ok = B_TRUE; 16070 } 16071 16072 /* 16073 * tcp_snd_sack_ok can be set in 16074 * tcp_adapt_ire() if the sack metric 16075 * is set. So check it here also. 16076 */ 16077 if (tcps->tcps_sack_permitted == 2 || 16078 tcp->tcp_snd_sack_ok) { 16079 if (tcp->tcp_sack_info == NULL) { 16080 tcp->tcp_sack_info = 16081 kmem_cache_alloc( 16082 tcp_sack_info_cache, 16083 KM_SLEEP); 16084 } 16085 tcp->tcp_snd_sack_ok = B_TRUE; 16086 } 16087 16088 /* 16089 * Should we use ECN? Note that the current 16090 * default value (SunOS 5.9) of tcp_ecn_permitted 16091 * is 1. The reason for doing this is that there 16092 * are equipments out there that will drop ECN 16093 * enabled IP packets. Setting it to 1 avoids 16094 * compatibility problems. 16095 */ 16096 if (tcps->tcps_ecn_permitted == 2) 16097 tcp->tcp_ecn_ok = B_TRUE; 16098 16099 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16100 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 16101 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 16102 if (syn_mp) { 16103 cred_t *cr; 16104 pid_t pid; 16105 16106 /* 16107 * Obtain the credential from the 16108 * thread calling connect(); the credential 16109 * lives on in the second mblk which 16110 * originated from T_CONN_REQ and is echoed 16111 * with the T_BIND_ACK from ip. If none 16112 * can be found, default to the creator 16113 * of the socket. 16114 */ 16115 if (mp->b_cont == NULL || 16116 (cr = DB_CRED(mp->b_cont)) == NULL) { 16117 cr = tcp->tcp_cred; 16118 pid = tcp->tcp_cpid; 16119 } else { 16120 pid = DB_CPID(mp->b_cont); 16121 } 16122 mblk_setcred(syn_mp, cr); 16123 DB_CPID(syn_mp) = pid; 16124 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 16125 } 16126 after_syn_sent: 16127 /* 16128 * A trailer mblk indicates a waiting client upstream. 16129 * We complete here the processing begun in 16130 * either tcp_bind() or tcp_connect() by passing 16131 * upstream the reply message they supplied. 16132 */ 16133 mp1 = mp; 16134 mp = mp->b_cont; 16135 freeb(mp1); 16136 if (mp) 16137 break; 16138 return; 16139 case T_ERROR_ACK: 16140 if (tcp->tcp_debug) { 16141 (void) strlog(TCP_MOD_ID, 0, 1, 16142 SL_TRACE|SL_ERROR, 16143 "tcp_rput_other: case T_ERROR_ACK, " 16144 "ERROR_prim == %d", 16145 tea->ERROR_prim); 16146 } 16147 switch (tea->ERROR_prim) { 16148 case O_T_BIND_REQ: 16149 case T_BIND_REQ: 16150 tcp_bind_failed(tcp, mp, 16151 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 16152 ENETUNREACH : EADDRNOTAVAIL)); 16153 return; 16154 case T_UNBIND_REQ: 16155 tcp->tcp_hard_binding = B_FALSE; 16156 tcp->tcp_hard_bound = B_FALSE; 16157 if (mp->b_cont) { 16158 freemsg(mp->b_cont); 16159 mp->b_cont = NULL; 16160 } 16161 if (tcp->tcp_unbind_pending) 16162 tcp->tcp_unbind_pending = 0; 16163 else { 16164 /* From tcp_ip_unbind() - free */ 16165 freemsg(mp); 16166 return; 16167 } 16168 break; 16169 case T_SVR4_OPTMGMT_REQ: 16170 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16171 /* T_OPTMGMT_REQ generated by TCP */ 16172 printf("T_SVR4_OPTMGMT_REQ failed " 16173 "%d/%d - dropped (cnt %d)\n", 16174 tea->TLI_error, tea->UNIX_error, 16175 tcp->tcp_drop_opt_ack_cnt); 16176 freemsg(mp); 16177 tcp->tcp_drop_opt_ack_cnt--; 16178 return; 16179 } 16180 break; 16181 } 16182 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 16183 tcp->tcp_drop_opt_ack_cnt > 0) { 16184 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 16185 "- dropped (cnt %d)\n", 16186 tea->TLI_error, tea->UNIX_error, 16187 tcp->tcp_drop_opt_ack_cnt); 16188 freemsg(mp); 16189 tcp->tcp_drop_opt_ack_cnt--; 16190 return; 16191 } 16192 break; 16193 case T_OPTMGMT_ACK: 16194 if (tcp->tcp_drop_opt_ack_cnt > 0) { 16195 /* T_OPTMGMT_REQ generated by TCP */ 16196 freemsg(mp); 16197 tcp->tcp_drop_opt_ack_cnt--; 16198 return; 16199 } 16200 break; 16201 default: 16202 break; 16203 } 16204 break; 16205 case M_FLUSH: 16206 if (*rptr & FLUSHR) 16207 flushq(q, FLUSHDATA); 16208 break; 16209 default: 16210 /* M_CTL will be directly sent to tcp_icmp_error() */ 16211 ASSERT(DB_TYPE(mp) != M_CTL); 16212 break; 16213 } 16214 /* 16215 * Make sure we set this bit before sending the ACK for 16216 * bind. Otherwise accept could possibly run and free 16217 * this tcp struct. 16218 */ 16219 putnext(q, mp); 16220 } 16221 16222 /* ARGSUSED */ 16223 static void 16224 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 16225 { 16226 conn_t *connp = (conn_t *)arg; 16227 tcp_t *tcp = connp->conn_tcp; 16228 queue_t *q = tcp->tcp_rq; 16229 uint_t thwin; 16230 tcp_stack_t *tcps = tcp->tcp_tcps; 16231 sodirect_t *sodp; 16232 boolean_t fc; 16233 16234 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16235 tcp->tcp_rsrv_mp = mp; 16236 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16237 16238 TCP_STAT(tcps, tcp_rsrv_calls); 16239 16240 if (TCP_IS_DETACHED(tcp) || q == NULL) { 16241 return; 16242 } 16243 16244 if (tcp->tcp_fused) { 16245 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16246 16247 ASSERT(tcp->tcp_fused); 16248 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 16249 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 16250 ASSERT(!TCP_IS_DETACHED(tcp)); 16251 ASSERT(tcp->tcp_connp->conn_sqp == 16252 peer_tcp->tcp_connp->conn_sqp); 16253 16254 /* 16255 * Normally we would not get backenabled in synchronous 16256 * streams mode, but in case this happens, we need to plug 16257 * synchronous streams during our drain to prevent a race 16258 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 16259 */ 16260 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 16261 if (tcp->tcp_rcv_list != NULL) 16262 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 16263 16264 if (peer_tcp > tcp) { 16265 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16266 mutex_enter(&tcp->tcp_non_sq_lock); 16267 } else { 16268 mutex_enter(&tcp->tcp_non_sq_lock); 16269 mutex_enter(&peer_tcp->tcp_non_sq_lock); 16270 } 16271 16272 if (peer_tcp->tcp_flow_stopped && 16273 (TCP_UNSENT_BYTES(peer_tcp) <= 16274 peer_tcp->tcp_xmit_lowater)) { 16275 tcp_clrqfull(peer_tcp); 16276 } 16277 mutex_exit(&peer_tcp->tcp_non_sq_lock); 16278 mutex_exit(&tcp->tcp_non_sq_lock); 16279 16280 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16281 TCP_STAT(tcps, tcp_fusion_backenabled); 16282 return; 16283 } 16284 16285 SOD_PTR_ENTER(tcp, sodp); 16286 if (sodp != NULL) { 16287 /* An sodirect connection */ 16288 if (SOD_QFULL(sodp)) { 16289 /* Flow-controlled, need another back-enable */ 16290 fc = B_TRUE; 16291 SOD_QSETBE(sodp); 16292 } else { 16293 /* Not flow-controlled */ 16294 fc = B_FALSE; 16295 } 16296 mutex_exit(sodp->sod_lockp); 16297 } else if (canputnext(q)) { 16298 /* STREAMS, not flow-controlled */ 16299 fc = B_FALSE; 16300 } else { 16301 /* STREAMS, flow-controlled */ 16302 fc = B_TRUE; 16303 } 16304 if (!fc) { 16305 /* Not flow-controlled, open rwnd */ 16306 tcp->tcp_rwnd = q->q_hiwat; 16307 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16308 << tcp->tcp_rcv_ws; 16309 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16310 /* 16311 * Send back a window update immediately if TCP is above 16312 * ESTABLISHED state and the increase of the rcv window 16313 * that the other side knows is at least 1 MSS after flow 16314 * control is lifted. 16315 */ 16316 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16317 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16318 tcp_xmit_ctl(NULL, tcp, 16319 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16320 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16321 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16322 } 16323 } 16324 } 16325 16326 /* 16327 * The read side service routine is called mostly when we get back-enabled as a 16328 * result of flow control relief. Since we don't actually queue anything in 16329 * TCP, we have no data to send out of here. What we do is clear the receive 16330 * window, and send out a window update. 16331 */ 16332 static void 16333 tcp_rsrv(queue_t *q) 16334 { 16335 conn_t *connp = Q_TO_CONN(q); 16336 tcp_t *tcp = connp->conn_tcp; 16337 mblk_t *mp; 16338 tcp_stack_t *tcps = tcp->tcp_tcps; 16339 16340 /* No code does a putq on the read side */ 16341 ASSERT(q->q_first == NULL); 16342 16343 /* Nothing to do for the default queue */ 16344 if (q == tcps->tcps_g_q) { 16345 return; 16346 } 16347 16348 /* 16349 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16350 * been run. So just return. 16351 */ 16352 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16353 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16354 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16355 return; 16356 } 16357 tcp->tcp_rsrv_mp = NULL; 16358 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16359 16360 CONN_INC_REF(connp); 16361 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16362 SQTAG_TCP_RSRV); 16363 } 16364 16365 /* 16366 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16367 * We do not allow the receive window to shrink. After setting rwnd, 16368 * set the flow control hiwat of the stream. 16369 * 16370 * This function is called in 2 cases: 16371 * 16372 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16373 * connection (passive open) and in tcp_rput_data() for active connect. 16374 * This is called after tcp_mss_set() when the desired MSS value is known. 16375 * This makes sure that our window size is a mutiple of the other side's 16376 * MSS. 16377 * 2) Handling SO_RCVBUF option. 16378 * 16379 * It is ASSUMED that the requested size is a multiple of the current MSS. 16380 * 16381 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16382 * user requests so. 16383 */ 16384 static int 16385 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16386 { 16387 uint32_t mss = tcp->tcp_mss; 16388 uint32_t old_max_rwnd; 16389 uint32_t max_transmittable_rwnd; 16390 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16391 tcp_stack_t *tcps = tcp->tcp_tcps; 16392 16393 if (tcp->tcp_fused) { 16394 size_t sth_hiwat; 16395 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16396 16397 ASSERT(peer_tcp != NULL); 16398 /* 16399 * Record the stream head's high water mark for 16400 * this endpoint; this is used for flow-control 16401 * purposes in tcp_fuse_output(). 16402 */ 16403 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16404 if (!tcp_detached) 16405 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 16406 16407 /* 16408 * In the fusion case, the maxpsz stream head value of 16409 * our peer is set according to its send buffer size 16410 * and our receive buffer size; since the latter may 16411 * have changed we need to update the peer's maxpsz. 16412 */ 16413 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16414 return (rwnd); 16415 } 16416 16417 if (tcp_detached) 16418 old_max_rwnd = tcp->tcp_rwnd; 16419 else 16420 old_max_rwnd = tcp->tcp_rq->q_hiwat; 16421 16422 /* 16423 * Insist on a receive window that is at least 16424 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16425 * funny TCP interactions of Nagle algorithm, SWS avoidance 16426 * and delayed acknowledgement. 16427 */ 16428 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16429 16430 /* 16431 * If window size info has already been exchanged, TCP should not 16432 * shrink the window. Shrinking window is doable if done carefully. 16433 * We may add that support later. But so far there is not a real 16434 * need to do that. 16435 */ 16436 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16437 /* MSS may have changed, do a round up again. */ 16438 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16439 } 16440 16441 /* 16442 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16443 * can be applied even before the window scale option is decided. 16444 */ 16445 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16446 if (rwnd > max_transmittable_rwnd) { 16447 rwnd = max_transmittable_rwnd - 16448 (max_transmittable_rwnd % mss); 16449 if (rwnd < mss) 16450 rwnd = max_transmittable_rwnd; 16451 /* 16452 * If we're over the limit we may have to back down tcp_rwnd. 16453 * The increment below won't work for us. So we set all three 16454 * here and the increment below will have no effect. 16455 */ 16456 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16457 } 16458 if (tcp->tcp_localnet) { 16459 tcp->tcp_rack_abs_max = 16460 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16461 } else { 16462 /* 16463 * For a remote host on a different subnet (through a router), 16464 * we ack every other packet to be conforming to RFC1122. 16465 * tcp_deferred_acks_max is default to 2. 16466 */ 16467 tcp->tcp_rack_abs_max = 16468 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16469 } 16470 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16471 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16472 else 16473 tcp->tcp_rack_cur_max = 0; 16474 /* 16475 * Increment the current rwnd by the amount the maximum grew (we 16476 * can not overwrite it since we might be in the middle of a 16477 * connection.) 16478 */ 16479 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16480 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16481 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16482 tcp->tcp_cwnd_max = rwnd; 16483 16484 if (tcp_detached) 16485 return (rwnd); 16486 /* 16487 * We set the maximum receive window into rq->q_hiwat. 16488 * This is not actually used for flow control. 16489 */ 16490 tcp->tcp_rq->q_hiwat = rwnd; 16491 /* 16492 * Set the Stream head high water mark. This doesn't have to be 16493 * here, since we are simply using default values, but we would 16494 * prefer to choose these values algorithmically, with a likely 16495 * relationship to rwnd. 16496 */ 16497 (void) mi_set_sth_hiwat(tcp->tcp_rq, 16498 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16499 return (rwnd); 16500 } 16501 16502 /* 16503 * Return SNMP stuff in buffer in mpdata. 16504 */ 16505 mblk_t * 16506 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16507 { 16508 mblk_t *mpdata; 16509 mblk_t *mp_conn_ctl = NULL; 16510 mblk_t *mp_conn_tail; 16511 mblk_t *mp_attr_ctl = NULL; 16512 mblk_t *mp_attr_tail; 16513 mblk_t *mp6_conn_ctl = NULL; 16514 mblk_t *mp6_conn_tail; 16515 mblk_t *mp6_attr_ctl = NULL; 16516 mblk_t *mp6_attr_tail; 16517 struct opthdr *optp; 16518 mib2_tcpConnEntry_t tce; 16519 mib2_tcp6ConnEntry_t tce6; 16520 mib2_transportMLPEntry_t mlp; 16521 connf_t *connfp; 16522 int i; 16523 boolean_t ispriv; 16524 zoneid_t zoneid; 16525 int v4_conn_idx; 16526 int v6_conn_idx; 16527 conn_t *connp = Q_TO_CONN(q); 16528 tcp_stack_t *tcps; 16529 ip_stack_t *ipst; 16530 mblk_t *mp2ctl; 16531 16532 /* 16533 * make a copy of the original message 16534 */ 16535 mp2ctl = copymsg(mpctl); 16536 16537 if (mpctl == NULL || 16538 (mpdata = mpctl->b_cont) == NULL || 16539 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16540 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16541 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16542 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16543 freemsg(mp_conn_ctl); 16544 freemsg(mp_attr_ctl); 16545 freemsg(mp6_conn_ctl); 16546 freemsg(mp6_attr_ctl); 16547 freemsg(mpctl); 16548 freemsg(mp2ctl); 16549 return (NULL); 16550 } 16551 16552 ipst = connp->conn_netstack->netstack_ip; 16553 tcps = connp->conn_netstack->netstack_tcp; 16554 16555 /* build table of connections -- need count in fixed part */ 16556 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16557 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16558 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16559 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16560 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16561 16562 ispriv = 16563 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16564 zoneid = Q_TO_CONN(q)->conn_zoneid; 16565 16566 v4_conn_idx = v6_conn_idx = 0; 16567 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16568 16569 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16570 ipst = tcps->tcps_netstack->netstack_ip; 16571 16572 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16573 16574 connp = NULL; 16575 16576 while ((connp = 16577 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16578 tcp_t *tcp; 16579 boolean_t needattr; 16580 16581 if (connp->conn_zoneid != zoneid) 16582 continue; /* not in this zone */ 16583 16584 tcp = connp->conn_tcp; 16585 UPDATE_MIB(&tcps->tcps_mib, 16586 tcpHCInSegs, tcp->tcp_ibsegs); 16587 tcp->tcp_ibsegs = 0; 16588 UPDATE_MIB(&tcps->tcps_mib, 16589 tcpHCOutSegs, tcp->tcp_obsegs); 16590 tcp->tcp_obsegs = 0; 16591 16592 tce6.tcp6ConnState = tce.tcpConnState = 16593 tcp_snmp_state(tcp); 16594 if (tce.tcpConnState == MIB2_TCP_established || 16595 tce.tcpConnState == MIB2_TCP_closeWait) 16596 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16597 16598 needattr = B_FALSE; 16599 bzero(&mlp, sizeof (mlp)); 16600 if (connp->conn_mlp_type != mlptSingle) { 16601 if (connp->conn_mlp_type == mlptShared || 16602 connp->conn_mlp_type == mlptBoth) 16603 mlp.tme_flags |= MIB2_TMEF_SHARED; 16604 if (connp->conn_mlp_type == mlptPrivate || 16605 connp->conn_mlp_type == mlptBoth) 16606 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16607 needattr = B_TRUE; 16608 } 16609 if (connp->conn_peercred != NULL) { 16610 ts_label_t *tsl; 16611 16612 tsl = crgetlabel(connp->conn_peercred); 16613 mlp.tme_doi = label2doi(tsl); 16614 mlp.tme_label = *label2bslabel(tsl); 16615 needattr = B_TRUE; 16616 } 16617 16618 /* Create a message to report on IPv6 entries */ 16619 if (tcp->tcp_ipversion == IPV6_VERSION) { 16620 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16621 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16622 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16623 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16624 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16625 /* Don't want just anybody seeing these... */ 16626 if (ispriv) { 16627 tce6.tcp6ConnEntryInfo.ce_snxt = 16628 tcp->tcp_snxt; 16629 tce6.tcp6ConnEntryInfo.ce_suna = 16630 tcp->tcp_suna; 16631 tce6.tcp6ConnEntryInfo.ce_rnxt = 16632 tcp->tcp_rnxt; 16633 tce6.tcp6ConnEntryInfo.ce_rack = 16634 tcp->tcp_rack; 16635 } else { 16636 /* 16637 * Netstat, unfortunately, uses this to 16638 * get send/receive queue sizes. How to fix? 16639 * Why not compute the difference only? 16640 */ 16641 tce6.tcp6ConnEntryInfo.ce_snxt = 16642 tcp->tcp_snxt - tcp->tcp_suna; 16643 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16644 tce6.tcp6ConnEntryInfo.ce_rnxt = 16645 tcp->tcp_rnxt - tcp->tcp_rack; 16646 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16647 } 16648 16649 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16650 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16651 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16652 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16653 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16654 16655 tce6.tcp6ConnCreationProcess = 16656 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16657 tcp->tcp_cpid; 16658 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16659 16660 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16661 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16662 16663 mlp.tme_connidx = v6_conn_idx++; 16664 if (needattr) 16665 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16666 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16667 } 16668 /* 16669 * Create an IPv4 table entry for IPv4 entries and also 16670 * for IPv6 entries which are bound to in6addr_any 16671 * but don't have IPV6_V6ONLY set. 16672 * (i.e. anything an IPv4 peer could connect to) 16673 */ 16674 if (tcp->tcp_ipversion == IPV4_VERSION || 16675 (tcp->tcp_state <= TCPS_LISTEN && 16676 !tcp->tcp_connp->conn_ipv6_v6only && 16677 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16678 if (tcp->tcp_ipversion == IPV6_VERSION) { 16679 tce.tcpConnRemAddress = INADDR_ANY; 16680 tce.tcpConnLocalAddress = INADDR_ANY; 16681 } else { 16682 tce.tcpConnRemAddress = 16683 tcp->tcp_remote; 16684 tce.tcpConnLocalAddress = 16685 tcp->tcp_ip_src; 16686 } 16687 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16688 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16689 /* Don't want just anybody seeing these... */ 16690 if (ispriv) { 16691 tce.tcpConnEntryInfo.ce_snxt = 16692 tcp->tcp_snxt; 16693 tce.tcpConnEntryInfo.ce_suna = 16694 tcp->tcp_suna; 16695 tce.tcpConnEntryInfo.ce_rnxt = 16696 tcp->tcp_rnxt; 16697 tce.tcpConnEntryInfo.ce_rack = 16698 tcp->tcp_rack; 16699 } else { 16700 /* 16701 * Netstat, unfortunately, uses this to 16702 * get send/receive queue sizes. How 16703 * to fix? 16704 * Why not compute the difference only? 16705 */ 16706 tce.tcpConnEntryInfo.ce_snxt = 16707 tcp->tcp_snxt - tcp->tcp_suna; 16708 tce.tcpConnEntryInfo.ce_suna = 0; 16709 tce.tcpConnEntryInfo.ce_rnxt = 16710 tcp->tcp_rnxt - tcp->tcp_rack; 16711 tce.tcpConnEntryInfo.ce_rack = 0; 16712 } 16713 16714 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16715 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16716 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16717 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16718 tce.tcpConnEntryInfo.ce_state = 16719 tcp->tcp_state; 16720 16721 tce.tcpConnCreationProcess = 16722 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16723 tcp->tcp_cpid; 16724 tce.tcpConnCreationTime = tcp->tcp_open_time; 16725 16726 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16727 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16728 16729 mlp.tme_connidx = v4_conn_idx++; 16730 if (needattr) 16731 (void) snmp_append_data2( 16732 mp_attr_ctl->b_cont, 16733 &mp_attr_tail, (char *)&mlp, 16734 sizeof (mlp)); 16735 } 16736 } 16737 } 16738 16739 /* fixed length structure for IPv4 and IPv6 counters */ 16740 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16741 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16742 sizeof (mib2_tcp6ConnEntry_t)); 16743 /* synchronize 32- and 64-bit counters */ 16744 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16745 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16746 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16747 optp->level = MIB2_TCP; 16748 optp->name = 0; 16749 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16750 sizeof (tcps->tcps_mib)); 16751 optp->len = msgdsize(mpdata); 16752 qreply(q, mpctl); 16753 16754 /* table of connections... */ 16755 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16756 sizeof (struct T_optmgmt_ack)]; 16757 optp->level = MIB2_TCP; 16758 optp->name = MIB2_TCP_CONN; 16759 optp->len = msgdsize(mp_conn_ctl->b_cont); 16760 qreply(q, mp_conn_ctl); 16761 16762 /* table of MLP attributes... */ 16763 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16764 sizeof (struct T_optmgmt_ack)]; 16765 optp->level = MIB2_TCP; 16766 optp->name = EXPER_XPORT_MLP; 16767 optp->len = msgdsize(mp_attr_ctl->b_cont); 16768 if (optp->len == 0) 16769 freemsg(mp_attr_ctl); 16770 else 16771 qreply(q, mp_attr_ctl); 16772 16773 /* table of IPv6 connections... */ 16774 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16775 sizeof (struct T_optmgmt_ack)]; 16776 optp->level = MIB2_TCP6; 16777 optp->name = MIB2_TCP6_CONN; 16778 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16779 qreply(q, mp6_conn_ctl); 16780 16781 /* table of IPv6 MLP attributes... */ 16782 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16783 sizeof (struct T_optmgmt_ack)]; 16784 optp->level = MIB2_TCP6; 16785 optp->name = EXPER_XPORT_MLP; 16786 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16787 if (optp->len == 0) 16788 freemsg(mp6_attr_ctl); 16789 else 16790 qreply(q, mp6_attr_ctl); 16791 return (mp2ctl); 16792 } 16793 16794 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16795 /* ARGSUSED */ 16796 int 16797 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16798 { 16799 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16800 16801 switch (level) { 16802 case MIB2_TCP: 16803 switch (name) { 16804 case 13: 16805 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16806 return (0); 16807 /* TODO: delete entry defined by tce */ 16808 return (1); 16809 default: 16810 return (0); 16811 } 16812 default: 16813 return (1); 16814 } 16815 } 16816 16817 /* Translate TCP state to MIB2 TCP state. */ 16818 static int 16819 tcp_snmp_state(tcp_t *tcp) 16820 { 16821 if (tcp == NULL) 16822 return (0); 16823 16824 switch (tcp->tcp_state) { 16825 case TCPS_CLOSED: 16826 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16827 case TCPS_BOUND: 16828 return (MIB2_TCP_closed); 16829 case TCPS_LISTEN: 16830 return (MIB2_TCP_listen); 16831 case TCPS_SYN_SENT: 16832 return (MIB2_TCP_synSent); 16833 case TCPS_SYN_RCVD: 16834 return (MIB2_TCP_synReceived); 16835 case TCPS_ESTABLISHED: 16836 return (MIB2_TCP_established); 16837 case TCPS_CLOSE_WAIT: 16838 return (MIB2_TCP_closeWait); 16839 case TCPS_FIN_WAIT_1: 16840 return (MIB2_TCP_finWait1); 16841 case TCPS_CLOSING: 16842 return (MIB2_TCP_closing); 16843 case TCPS_LAST_ACK: 16844 return (MIB2_TCP_lastAck); 16845 case TCPS_FIN_WAIT_2: 16846 return (MIB2_TCP_finWait2); 16847 case TCPS_TIME_WAIT: 16848 return (MIB2_TCP_timeWait); 16849 default: 16850 return (0); 16851 } 16852 } 16853 16854 static char tcp_report_header[] = 16855 "TCP " MI_COL_HDRPAD_STR 16856 "zone dest snxt suna " 16857 "swnd rnxt rack rwnd rto mss w sw rw t " 16858 "recent [lport,fport] state"; 16859 16860 /* 16861 * TCP status report triggered via the Named Dispatch mechanism. 16862 */ 16863 /* ARGSUSED */ 16864 static void 16865 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16866 cred_t *cr) 16867 { 16868 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16869 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16870 char cflag; 16871 in6_addr_t v6dst; 16872 char buf[80]; 16873 uint_t print_len, buf_len; 16874 16875 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16876 if (buf_len <= 0) 16877 return; 16878 16879 if (hashval >= 0) 16880 (void) sprintf(hash, "%03d ", hashval); 16881 else 16882 hash[0] = '\0'; 16883 16884 /* 16885 * Note that we use the remote address in the tcp_b structure. 16886 * This means that it will print out the real destination address, 16887 * not the next hop's address if source routing is used. This 16888 * avoid the confusion on the output because user may not 16889 * know that source routing is used for a connection. 16890 */ 16891 if (tcp->tcp_ipversion == IPV4_VERSION) { 16892 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16893 } else { 16894 v6dst = tcp->tcp_remote_v6; 16895 } 16896 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16897 /* 16898 * the ispriv checks are so that normal users cannot determine 16899 * sequence number information using NDD. 16900 */ 16901 16902 if (TCP_IS_DETACHED(tcp)) 16903 cflag = '*'; 16904 else 16905 cflag = ' '; 16906 print_len = snprintf((char *)mp->b_wptr, buf_len, 16907 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16908 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16909 hash, 16910 (void *)tcp, 16911 tcp->tcp_connp->conn_zoneid, 16912 addrbuf, 16913 (ispriv) ? tcp->tcp_snxt : 0, 16914 (ispriv) ? tcp->tcp_suna : 0, 16915 tcp->tcp_swnd, 16916 (ispriv) ? tcp->tcp_rnxt : 0, 16917 (ispriv) ? tcp->tcp_rack : 0, 16918 tcp->tcp_rwnd, 16919 tcp->tcp_rto, 16920 tcp->tcp_mss, 16921 tcp->tcp_snd_ws_ok, 16922 tcp->tcp_snd_ws, 16923 tcp->tcp_rcv_ws, 16924 tcp->tcp_snd_ts_ok, 16925 tcp->tcp_ts_recent, 16926 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16927 if (print_len < buf_len) { 16928 ((mblk_t *)mp)->b_wptr += print_len; 16929 } else { 16930 ((mblk_t *)mp)->b_wptr += buf_len; 16931 } 16932 } 16933 16934 /* 16935 * TCP status report (for listeners only) triggered via the Named Dispatch 16936 * mechanism. 16937 */ 16938 /* ARGSUSED */ 16939 static void 16940 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16941 { 16942 char addrbuf[INET6_ADDRSTRLEN]; 16943 in6_addr_t v6dst; 16944 uint_t print_len, buf_len; 16945 16946 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16947 if (buf_len <= 0) 16948 return; 16949 16950 if (tcp->tcp_ipversion == IPV4_VERSION) { 16951 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16952 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16953 } else { 16954 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16955 addrbuf, sizeof (addrbuf)); 16956 } 16957 print_len = snprintf((char *)mp->b_wptr, buf_len, 16958 "%03d " 16959 MI_COL_PTRFMT_STR 16960 "%d %s %05u %08u %d/%d/%d%c\n", 16961 hashval, (void *)tcp, 16962 tcp->tcp_connp->conn_zoneid, 16963 addrbuf, 16964 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16965 tcp->tcp_conn_req_seqnum, 16966 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16967 tcp->tcp_conn_req_max, 16968 tcp->tcp_syn_defense ? '*' : ' '); 16969 if (print_len < buf_len) { 16970 ((mblk_t *)mp)->b_wptr += print_len; 16971 } else { 16972 ((mblk_t *)mp)->b_wptr += buf_len; 16973 } 16974 } 16975 16976 /* TCP status report triggered via the Named Dispatch mechanism. */ 16977 /* ARGSUSED */ 16978 static int 16979 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16980 { 16981 tcp_t *tcp; 16982 int i; 16983 conn_t *connp; 16984 connf_t *connfp; 16985 zoneid_t zoneid; 16986 tcp_stack_t *tcps; 16987 ip_stack_t *ipst; 16988 16989 zoneid = Q_TO_CONN(q)->conn_zoneid; 16990 tcps = Q_TO_TCP(q)->tcp_tcps; 16991 16992 /* 16993 * Because of the ndd constraint, at most we can have 64K buffer 16994 * to put in all TCP info. So to be more efficient, just 16995 * allocate a 64K buffer here, assuming we need that large buffer. 16996 * This may be a problem as any user can read tcp_status. Therefore 16997 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16998 * This should be OK as normal users should not do this too often. 16999 */ 17000 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17001 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17002 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17003 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17004 return (0); 17005 } 17006 } 17007 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17008 /* The following may work even if we cannot get a large buf. */ 17009 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17010 return (0); 17011 } 17012 17013 (void) mi_mpprintf(mp, "%s", tcp_report_header); 17014 17015 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 17016 17017 ipst = tcps->tcps_netstack->netstack_ip; 17018 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 17019 17020 connp = NULL; 17021 17022 while ((connp = 17023 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17024 tcp = connp->conn_tcp; 17025 if (zoneid != GLOBAL_ZONEID && 17026 zoneid != connp->conn_zoneid) 17027 continue; 17028 tcp_report_item(mp->b_cont, tcp, -1, tcp, 17029 cr); 17030 } 17031 17032 } 17033 17034 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17035 return (0); 17036 } 17037 17038 /* TCP status report triggered via the Named Dispatch mechanism. */ 17039 /* ARGSUSED */ 17040 static int 17041 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17042 { 17043 tf_t *tbf; 17044 tcp_t *tcp; 17045 int i; 17046 zoneid_t zoneid; 17047 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17048 17049 zoneid = Q_TO_CONN(q)->conn_zoneid; 17050 17051 /* Refer to comments in tcp_status_report(). */ 17052 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17053 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17054 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17055 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17056 return (0); 17057 } 17058 } 17059 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17060 /* The following may work even if we cannot get a large buf. */ 17061 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17062 return (0); 17063 } 17064 17065 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17066 17067 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 17068 tbf = &tcps->tcps_bind_fanout[i]; 17069 mutex_enter(&tbf->tf_lock); 17070 for (tcp = tbf->tf_tcp; tcp != NULL; 17071 tcp = tcp->tcp_bind_hash) { 17072 if (zoneid != GLOBAL_ZONEID && 17073 zoneid != tcp->tcp_connp->conn_zoneid) 17074 continue; 17075 CONN_INC_REF(tcp->tcp_connp); 17076 tcp_report_item(mp->b_cont, tcp, i, 17077 Q_TO_TCP(q), cr); 17078 CONN_DEC_REF(tcp->tcp_connp); 17079 } 17080 mutex_exit(&tbf->tf_lock); 17081 } 17082 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17083 return (0); 17084 } 17085 17086 /* TCP status report triggered via the Named Dispatch mechanism. */ 17087 /* ARGSUSED */ 17088 static int 17089 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17090 { 17091 connf_t *connfp; 17092 conn_t *connp; 17093 tcp_t *tcp; 17094 int i; 17095 zoneid_t zoneid; 17096 tcp_stack_t *tcps; 17097 ip_stack_t *ipst; 17098 17099 zoneid = Q_TO_CONN(q)->conn_zoneid; 17100 tcps = Q_TO_TCP(q)->tcp_tcps; 17101 17102 /* Refer to comments in tcp_status_report(). */ 17103 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17104 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17105 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17106 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17107 return (0); 17108 } 17109 } 17110 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17111 /* The following may work even if we cannot get a large buf. */ 17112 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17113 return (0); 17114 } 17115 17116 (void) mi_mpprintf(mp, 17117 " TCP " MI_COL_HDRPAD_STR 17118 "zone IP addr port seqnum backlog (q0/q/max)"); 17119 17120 ipst = tcps->tcps_netstack->netstack_ip; 17121 17122 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 17123 connfp = &ipst->ips_ipcl_bind_fanout[i]; 17124 connp = NULL; 17125 while ((connp = 17126 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17127 tcp = connp->conn_tcp; 17128 if (zoneid != GLOBAL_ZONEID && 17129 zoneid != connp->conn_zoneid) 17130 continue; 17131 tcp_report_listener(mp->b_cont, tcp, i); 17132 } 17133 } 17134 17135 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17136 return (0); 17137 } 17138 17139 /* TCP status report triggered via the Named Dispatch mechanism. */ 17140 /* ARGSUSED */ 17141 static int 17142 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17143 { 17144 connf_t *connfp; 17145 conn_t *connp; 17146 tcp_t *tcp; 17147 int i; 17148 zoneid_t zoneid; 17149 tcp_stack_t *tcps; 17150 ip_stack_t *ipst; 17151 17152 zoneid = Q_TO_CONN(q)->conn_zoneid; 17153 tcps = Q_TO_TCP(q)->tcp_tcps; 17154 ipst = tcps->tcps_netstack->netstack_ip; 17155 17156 /* Refer to comments in tcp_status_report(). */ 17157 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17158 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17159 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17160 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17161 return (0); 17162 } 17163 } 17164 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17165 /* The following may work even if we cannot get a large buf. */ 17166 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17167 return (0); 17168 } 17169 17170 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 17171 ipst->ips_ipcl_conn_fanout_size); 17172 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17173 17174 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 17175 connfp = &ipst->ips_ipcl_conn_fanout[i]; 17176 connp = NULL; 17177 while ((connp = 17178 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 17179 tcp = connp->conn_tcp; 17180 if (zoneid != GLOBAL_ZONEID && 17181 zoneid != connp->conn_zoneid) 17182 continue; 17183 tcp_report_item(mp->b_cont, tcp, i, 17184 Q_TO_TCP(q), cr); 17185 } 17186 } 17187 17188 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17189 return (0); 17190 } 17191 17192 /* TCP status report triggered via the Named Dispatch mechanism. */ 17193 /* ARGSUSED */ 17194 static int 17195 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 17196 { 17197 tf_t *tf; 17198 tcp_t *tcp; 17199 int i; 17200 zoneid_t zoneid; 17201 tcp_stack_t *tcps; 17202 17203 zoneid = Q_TO_CONN(q)->conn_zoneid; 17204 tcps = Q_TO_TCP(q)->tcp_tcps; 17205 17206 /* Refer to comments in tcp_status_report(). */ 17207 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 17208 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 17209 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 17210 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 17211 return (0); 17212 } 17213 } 17214 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 17215 /* The following may work even if we cannot get a large buf. */ 17216 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 17217 return (0); 17218 } 17219 17220 (void) mi_mpprintf(mp, " %s", tcp_report_header); 17221 17222 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 17223 tf = &tcps->tcps_acceptor_fanout[i]; 17224 mutex_enter(&tf->tf_lock); 17225 for (tcp = tf->tf_tcp; tcp != NULL; 17226 tcp = tcp->tcp_acceptor_hash) { 17227 if (zoneid != GLOBAL_ZONEID && 17228 zoneid != tcp->tcp_connp->conn_zoneid) 17229 continue; 17230 tcp_report_item(mp->b_cont, tcp, i, 17231 Q_TO_TCP(q), cr); 17232 } 17233 mutex_exit(&tf->tf_lock); 17234 } 17235 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 17236 return (0); 17237 } 17238 17239 /* 17240 * tcp_timer is the timer service routine. It handles the retransmission, 17241 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 17242 * from the state of the tcp instance what kind of action needs to be done 17243 * at the time it is called. 17244 */ 17245 static void 17246 tcp_timer(void *arg) 17247 { 17248 mblk_t *mp; 17249 clock_t first_threshold; 17250 clock_t second_threshold; 17251 clock_t ms; 17252 uint32_t mss; 17253 conn_t *connp = (conn_t *)arg; 17254 tcp_t *tcp = connp->conn_tcp; 17255 tcp_stack_t *tcps = tcp->tcp_tcps; 17256 17257 tcp->tcp_timer_tid = 0; 17258 17259 if (tcp->tcp_fused) 17260 return; 17261 17262 first_threshold = tcp->tcp_first_timer_threshold; 17263 second_threshold = tcp->tcp_second_timer_threshold; 17264 switch (tcp->tcp_state) { 17265 case TCPS_IDLE: 17266 case TCPS_BOUND: 17267 case TCPS_LISTEN: 17268 return; 17269 case TCPS_SYN_RCVD: { 17270 tcp_t *listener = tcp->tcp_listener; 17271 17272 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17273 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17274 /* it's our first timeout */ 17275 tcp->tcp_syn_rcvd_timeout = 1; 17276 mutex_enter(&listener->tcp_eager_lock); 17277 listener->tcp_syn_rcvd_timeout++; 17278 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17279 /* 17280 * Make this eager available for drop if we 17281 * need to drop one to accomodate a new 17282 * incoming SYN request. 17283 */ 17284 MAKE_DROPPABLE(listener, tcp); 17285 } 17286 if (!listener->tcp_syn_defense && 17287 (listener->tcp_syn_rcvd_timeout > 17288 (tcps->tcps_conn_req_max_q0 >> 2)) && 17289 (tcps->tcps_conn_req_max_q0 > 200)) { 17290 /* We may be under attack. Put on a defense. */ 17291 listener->tcp_syn_defense = B_TRUE; 17292 cmn_err(CE_WARN, "High TCP connect timeout " 17293 "rate! System (port %d) may be under a " 17294 "SYN flood attack!", 17295 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17296 17297 listener->tcp_ip_addr_cache = kmem_zalloc( 17298 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17299 KM_NOSLEEP); 17300 } 17301 mutex_exit(&listener->tcp_eager_lock); 17302 } else if (listener != NULL) { 17303 mutex_enter(&listener->tcp_eager_lock); 17304 tcp->tcp_syn_rcvd_timeout++; 17305 if (tcp->tcp_syn_rcvd_timeout > 1 && 17306 !tcp->tcp_closemp_used) { 17307 /* 17308 * This is our second timeout. Put the tcp in 17309 * the list of droppable eagers to allow it to 17310 * be dropped, if needed. We don't check 17311 * whether tcp_dontdrop is set or not to 17312 * protect ourselve from a SYN attack where a 17313 * remote host can spoof itself as one of the 17314 * good IP source and continue to hold 17315 * resources too long. 17316 */ 17317 MAKE_DROPPABLE(listener, tcp); 17318 } 17319 mutex_exit(&listener->tcp_eager_lock); 17320 } 17321 } 17322 /* FALLTHRU */ 17323 case TCPS_SYN_SENT: 17324 first_threshold = tcp->tcp_first_ctimer_threshold; 17325 second_threshold = tcp->tcp_second_ctimer_threshold; 17326 break; 17327 case TCPS_ESTABLISHED: 17328 case TCPS_FIN_WAIT_1: 17329 case TCPS_CLOSING: 17330 case TCPS_CLOSE_WAIT: 17331 case TCPS_LAST_ACK: 17332 /* If we have data to rexmit */ 17333 if (tcp->tcp_suna != tcp->tcp_snxt) { 17334 clock_t time_to_wait; 17335 17336 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17337 if (!tcp->tcp_xmit_head) 17338 break; 17339 time_to_wait = lbolt - 17340 (clock_t)tcp->tcp_xmit_head->b_prev; 17341 time_to_wait = tcp->tcp_rto - 17342 TICK_TO_MSEC(time_to_wait); 17343 /* 17344 * If the timer fires too early, 1 clock tick earlier, 17345 * restart the timer. 17346 */ 17347 if (time_to_wait > msec_per_tick) { 17348 TCP_STAT(tcps, tcp_timer_fire_early); 17349 TCP_TIMER_RESTART(tcp, time_to_wait); 17350 return; 17351 } 17352 /* 17353 * When we probe zero windows, we force the swnd open. 17354 * If our peer acks with a closed window swnd will be 17355 * set to zero by tcp_rput(). As long as we are 17356 * receiving acks tcp_rput will 17357 * reset 'tcp_ms_we_have_waited' so as not to trip the 17358 * first and second interval actions. NOTE: the timer 17359 * interval is allowed to continue its exponential 17360 * backoff. 17361 */ 17362 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17363 if (tcp->tcp_debug) { 17364 (void) strlog(TCP_MOD_ID, 0, 1, 17365 SL_TRACE, "tcp_timer: zero win"); 17366 } 17367 } else { 17368 /* 17369 * After retransmission, we need to do 17370 * slow start. Set the ssthresh to one 17371 * half of current effective window and 17372 * cwnd to one MSS. Also reset 17373 * tcp_cwnd_cnt. 17374 * 17375 * Note that if tcp_ssthresh is reduced because 17376 * of ECN, do not reduce it again unless it is 17377 * already one window of data away (tcp_cwr 17378 * should then be cleared) or this is a 17379 * timeout for a retransmitted segment. 17380 */ 17381 uint32_t npkt; 17382 17383 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17384 npkt = ((tcp->tcp_timer_backoff ? 17385 tcp->tcp_cwnd_ssthresh : 17386 tcp->tcp_snxt - 17387 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17388 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17389 tcp->tcp_mss; 17390 } 17391 tcp->tcp_cwnd = tcp->tcp_mss; 17392 tcp->tcp_cwnd_cnt = 0; 17393 if (tcp->tcp_ecn_ok) { 17394 tcp->tcp_cwr = B_TRUE; 17395 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17396 tcp->tcp_ecn_cwr_sent = B_FALSE; 17397 } 17398 } 17399 break; 17400 } 17401 /* 17402 * We have something to send yet we cannot send. The 17403 * reason can be: 17404 * 17405 * 1. Zero send window: we need to do zero window probe. 17406 * 2. Zero cwnd: because of ECN, we need to "clock out 17407 * segments. 17408 * 3. SWS avoidance: receiver may have shrunk window, 17409 * reset our knowledge. 17410 * 17411 * Note that condition 2 can happen with either 1 or 17412 * 3. But 1 and 3 are exclusive. 17413 */ 17414 if (tcp->tcp_unsent != 0) { 17415 if (tcp->tcp_cwnd == 0) { 17416 /* 17417 * Set tcp_cwnd to 1 MSS so that a 17418 * new segment can be sent out. We 17419 * are "clocking out" new data when 17420 * the network is really congested. 17421 */ 17422 ASSERT(tcp->tcp_ecn_ok); 17423 tcp->tcp_cwnd = tcp->tcp_mss; 17424 } 17425 if (tcp->tcp_swnd == 0) { 17426 /* Extend window for zero window probe */ 17427 tcp->tcp_swnd++; 17428 tcp->tcp_zero_win_probe = B_TRUE; 17429 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17430 } else { 17431 /* 17432 * Handle timeout from sender SWS avoidance. 17433 * Reset our knowledge of the max send window 17434 * since the receiver might have reduced its 17435 * receive buffer. Avoid setting tcp_max_swnd 17436 * to one since that will essentially disable 17437 * the SWS checks. 17438 * 17439 * Note that since we don't have a SWS 17440 * state variable, if the timeout is set 17441 * for ECN but not for SWS, this 17442 * code will also be executed. This is 17443 * fine as tcp_max_swnd is updated 17444 * constantly and it will not affect 17445 * anything. 17446 */ 17447 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17448 } 17449 tcp_wput_data(tcp, NULL, B_FALSE); 17450 return; 17451 } 17452 /* Is there a FIN that needs to be to re retransmitted? */ 17453 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17454 !tcp->tcp_fin_acked) 17455 break; 17456 /* Nothing to do, return without restarting timer. */ 17457 TCP_STAT(tcps, tcp_timer_fire_miss); 17458 return; 17459 case TCPS_FIN_WAIT_2: 17460 /* 17461 * User closed the TCP endpoint and peer ACK'ed our FIN. 17462 * We waited some time for for peer's FIN, but it hasn't 17463 * arrived. We flush the connection now to avoid 17464 * case where the peer has rebooted. 17465 */ 17466 if (TCP_IS_DETACHED(tcp)) { 17467 (void) tcp_clean_death(tcp, 0, 23); 17468 } else { 17469 TCP_TIMER_RESTART(tcp, 17470 tcps->tcps_fin_wait_2_flush_interval); 17471 } 17472 return; 17473 case TCPS_TIME_WAIT: 17474 (void) tcp_clean_death(tcp, 0, 24); 17475 return; 17476 default: 17477 if (tcp->tcp_debug) { 17478 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17479 "tcp_timer: strange state (%d) %s", 17480 tcp->tcp_state, tcp_display(tcp, NULL, 17481 DISP_PORT_ONLY)); 17482 } 17483 return; 17484 } 17485 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17486 /* 17487 * For zero window probe, we need to send indefinitely, 17488 * unless we have not heard from the other side for some 17489 * time... 17490 */ 17491 if ((tcp->tcp_zero_win_probe == 0) || 17492 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17493 second_threshold)) { 17494 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17495 /* 17496 * If TCP is in SYN_RCVD state, send back a 17497 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17498 * should be zero in TCPS_SYN_RCVD state. 17499 */ 17500 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17501 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17502 "in SYN_RCVD", 17503 tcp, tcp->tcp_snxt, 17504 tcp->tcp_rnxt, TH_RST | TH_ACK); 17505 } 17506 (void) tcp_clean_death(tcp, 17507 tcp->tcp_client_errno ? 17508 tcp->tcp_client_errno : ETIMEDOUT, 25); 17509 return; 17510 } else { 17511 /* 17512 * Set tcp_ms_we_have_waited to second_threshold 17513 * so that in next timeout, we will do the above 17514 * check (lbolt - tcp_last_recv_time). This is 17515 * also to avoid overflow. 17516 * 17517 * We don't need to decrement tcp_timer_backoff 17518 * to avoid overflow because it will be decremented 17519 * later if new timeout value is greater than 17520 * tcp_rexmit_interval_max. In the case when 17521 * tcp_rexmit_interval_max is greater than 17522 * second_threshold, it means that we will wait 17523 * longer than second_threshold to send the next 17524 * window probe. 17525 */ 17526 tcp->tcp_ms_we_have_waited = second_threshold; 17527 } 17528 } else if (ms > first_threshold) { 17529 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17530 tcp->tcp_xmit_head != NULL) { 17531 tcp->tcp_xmit_head = 17532 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17533 } 17534 /* 17535 * We have been retransmitting for too long... The RTT 17536 * we calculated is probably incorrect. Reinitialize it. 17537 * Need to compensate for 0 tcp_rtt_sa. Reset 17538 * tcp_rtt_update so that we won't accidentally cache a 17539 * bad value. But only do this if this is not a zero 17540 * window probe. 17541 */ 17542 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17543 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17544 (tcp->tcp_rtt_sa >> 5); 17545 tcp->tcp_rtt_sa = 0; 17546 tcp_ip_notify(tcp); 17547 tcp->tcp_rtt_update = 0; 17548 } 17549 } 17550 tcp->tcp_timer_backoff++; 17551 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17552 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17553 tcps->tcps_rexmit_interval_min) { 17554 /* 17555 * This means the original RTO is tcp_rexmit_interval_min. 17556 * So we will use tcp_rexmit_interval_min as the RTO value 17557 * and do the backoff. 17558 */ 17559 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17560 } else { 17561 ms <<= tcp->tcp_timer_backoff; 17562 } 17563 if (ms > tcps->tcps_rexmit_interval_max) { 17564 ms = tcps->tcps_rexmit_interval_max; 17565 /* 17566 * ms is at max, decrement tcp_timer_backoff to avoid 17567 * overflow. 17568 */ 17569 tcp->tcp_timer_backoff--; 17570 } 17571 tcp->tcp_ms_we_have_waited += ms; 17572 if (tcp->tcp_zero_win_probe == 0) { 17573 tcp->tcp_rto = ms; 17574 } 17575 TCP_TIMER_RESTART(tcp, ms); 17576 /* 17577 * This is after a timeout and tcp_rto is backed off. Set 17578 * tcp_set_timer to 1 so that next time RTO is updated, we will 17579 * restart the timer with a correct value. 17580 */ 17581 tcp->tcp_set_timer = 1; 17582 mss = tcp->tcp_snxt - tcp->tcp_suna; 17583 if (mss > tcp->tcp_mss) 17584 mss = tcp->tcp_mss; 17585 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17586 mss = tcp->tcp_swnd; 17587 17588 if ((mp = tcp->tcp_xmit_head) != NULL) 17589 mp->b_prev = (mblk_t *)lbolt; 17590 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17591 B_TRUE); 17592 17593 /* 17594 * When slow start after retransmission begins, start with 17595 * this seq no. tcp_rexmit_max marks the end of special slow 17596 * start phase. tcp_snd_burst controls how many segments 17597 * can be sent because of an ack. 17598 */ 17599 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17600 tcp->tcp_snd_burst = TCP_CWND_SS; 17601 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17602 (tcp->tcp_unsent == 0)) { 17603 tcp->tcp_rexmit_max = tcp->tcp_fss; 17604 } else { 17605 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17606 } 17607 tcp->tcp_rexmit = B_TRUE; 17608 tcp->tcp_dupack_cnt = 0; 17609 17610 /* 17611 * Remove all rexmit SACK blk to start from fresh. 17612 */ 17613 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17614 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17615 tcp->tcp_num_notsack_blk = 0; 17616 tcp->tcp_cnt_notsack_list = 0; 17617 } 17618 if (mp == NULL) { 17619 return; 17620 } 17621 /* Attach credentials to retransmitted initial SYNs. */ 17622 if (tcp->tcp_state == TCPS_SYN_SENT) { 17623 mblk_setcred(mp, tcp->tcp_cred); 17624 DB_CPID(mp) = tcp->tcp_cpid; 17625 } 17626 17627 tcp->tcp_csuna = tcp->tcp_snxt; 17628 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17629 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17630 tcp_send_data(tcp, tcp->tcp_wq, mp); 17631 17632 } 17633 17634 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17635 static void 17636 tcp_unbind(tcp_t *tcp, mblk_t *mp) 17637 { 17638 conn_t *connp; 17639 17640 switch (tcp->tcp_state) { 17641 case TCPS_BOUND: 17642 case TCPS_LISTEN: 17643 break; 17644 default: 17645 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 17646 return; 17647 } 17648 17649 /* 17650 * Need to clean up all the eagers since after the unbind, segments 17651 * will no longer be delivered to this listener stream. 17652 */ 17653 mutex_enter(&tcp->tcp_eager_lock); 17654 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17655 tcp_eager_cleanup(tcp, 0); 17656 } 17657 mutex_exit(&tcp->tcp_eager_lock); 17658 17659 if (tcp->tcp_ipversion == IPV4_VERSION) { 17660 tcp->tcp_ipha->ipha_src = 0; 17661 } else { 17662 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17663 } 17664 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17665 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17666 tcp_bind_hash_remove(tcp); 17667 tcp->tcp_state = TCPS_IDLE; 17668 tcp->tcp_mdt = B_FALSE; 17669 /* Send M_FLUSH according to TPI */ 17670 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17671 connp = tcp->tcp_connp; 17672 connp->conn_mdt_ok = B_FALSE; 17673 ipcl_hash_remove(connp); 17674 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17675 mp = mi_tpi_ok_ack_alloc(mp); 17676 putnext(tcp->tcp_rq, mp); 17677 } 17678 17679 /* 17680 * Don't let port fall into the privileged range. 17681 * Since the extra privileged ports can be arbitrary we also 17682 * ensure that we exclude those from consideration. 17683 * tcp_g_epriv_ports is not sorted thus we loop over it until 17684 * there are no changes. 17685 * 17686 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17687 * but instead the code relies on: 17688 * - the fact that the address of the array and its size never changes 17689 * - the atomic assignment of the elements of the array 17690 * 17691 * Returns 0 if there are no more ports available. 17692 * 17693 * TS note: skip multilevel ports. 17694 */ 17695 static in_port_t 17696 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17697 { 17698 int i; 17699 boolean_t restart = B_FALSE; 17700 tcp_stack_t *tcps = tcp->tcp_tcps; 17701 17702 if (random && tcp_random_anon_port != 0) { 17703 (void) random_get_pseudo_bytes((uint8_t *)&port, 17704 sizeof (in_port_t)); 17705 /* 17706 * Unless changed by a sys admin, the smallest anon port 17707 * is 32768 and the largest anon port is 65535. It is 17708 * very likely (50%) for the random port to be smaller 17709 * than the smallest anon port. When that happens, 17710 * add port % (anon port range) to the smallest anon 17711 * port to get the random port. It should fall into the 17712 * valid anon port range. 17713 */ 17714 if (port < tcps->tcps_smallest_anon_port) { 17715 port = tcps->tcps_smallest_anon_port + 17716 port % (tcps->tcps_largest_anon_port - 17717 tcps->tcps_smallest_anon_port); 17718 } 17719 } 17720 17721 retry: 17722 if (port < tcps->tcps_smallest_anon_port) 17723 port = (in_port_t)tcps->tcps_smallest_anon_port; 17724 17725 if (port > tcps->tcps_largest_anon_port) { 17726 if (restart) 17727 return (0); 17728 restart = B_TRUE; 17729 port = (in_port_t)tcps->tcps_smallest_anon_port; 17730 } 17731 17732 if (port < tcps->tcps_smallest_nonpriv_port) 17733 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17734 17735 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17736 if (port == tcps->tcps_g_epriv_ports[i]) { 17737 port++; 17738 /* 17739 * Make sure whether the port is in the 17740 * valid range. 17741 */ 17742 goto retry; 17743 } 17744 } 17745 if (is_system_labeled() && 17746 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17747 IPPROTO_TCP, B_TRUE)) != 0) { 17748 port = i; 17749 goto retry; 17750 } 17751 return (port); 17752 } 17753 17754 /* 17755 * Return the next anonymous port in the privileged port range for 17756 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17757 * downwards. This is the same behavior as documented in the userland 17758 * library call rresvport(3N). 17759 * 17760 * TS note: skip multilevel ports. 17761 */ 17762 static in_port_t 17763 tcp_get_next_priv_port(const tcp_t *tcp) 17764 { 17765 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17766 in_port_t nextport; 17767 boolean_t restart = B_FALSE; 17768 tcp_stack_t *tcps = tcp->tcp_tcps; 17769 retry: 17770 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17771 next_priv_port >= IPPORT_RESERVED) { 17772 next_priv_port = IPPORT_RESERVED - 1; 17773 if (restart) 17774 return (0); 17775 restart = B_TRUE; 17776 } 17777 if (is_system_labeled() && 17778 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17779 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17780 next_priv_port = nextport; 17781 goto retry; 17782 } 17783 return (next_priv_port--); 17784 } 17785 17786 /* The write side r/w procedure. */ 17787 17788 #if CCS_STATS 17789 struct { 17790 struct { 17791 int64_t count, bytes; 17792 } tot, hit; 17793 } wrw_stats; 17794 #endif 17795 17796 /* 17797 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17798 * messages. 17799 */ 17800 /* ARGSUSED */ 17801 static void 17802 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17803 { 17804 conn_t *connp = (conn_t *)arg; 17805 tcp_t *tcp = connp->conn_tcp; 17806 queue_t *q = tcp->tcp_wq; 17807 17808 ASSERT(DB_TYPE(mp) != M_IOCTL); 17809 /* 17810 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17811 * Once the close starts, streamhead and sockfs will not let any data 17812 * packets come down (close ensures that there are no threads using the 17813 * queue and no new threads will come down) but since qprocsoff() 17814 * hasn't happened yet, a M_FLUSH or some non data message might 17815 * get reflected back (in response to our own FLUSHRW) and get 17816 * processed after tcp_close() is done. The conn would still be valid 17817 * because a ref would have added but we need to check the state 17818 * before actually processing the packet. 17819 */ 17820 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17821 freemsg(mp); 17822 return; 17823 } 17824 17825 switch (DB_TYPE(mp)) { 17826 case M_IOCDATA: 17827 tcp_wput_iocdata(tcp, mp); 17828 break; 17829 case M_FLUSH: 17830 tcp_wput_flush(tcp, mp); 17831 break; 17832 default: 17833 CALL_IP_WPUT(connp, q, mp); 17834 break; 17835 } 17836 } 17837 17838 /* 17839 * The TCP fast path write put procedure. 17840 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17841 */ 17842 /* ARGSUSED */ 17843 void 17844 tcp_output(void *arg, mblk_t *mp, void *arg2) 17845 { 17846 int len; 17847 int hdrlen; 17848 int plen; 17849 mblk_t *mp1; 17850 uchar_t *rptr; 17851 uint32_t snxt; 17852 tcph_t *tcph; 17853 struct datab *db; 17854 uint32_t suna; 17855 uint32_t mss; 17856 ipaddr_t *dst; 17857 ipaddr_t *src; 17858 uint32_t sum; 17859 int usable; 17860 conn_t *connp = (conn_t *)arg; 17861 tcp_t *tcp = connp->conn_tcp; 17862 uint32_t msize; 17863 tcp_stack_t *tcps = tcp->tcp_tcps; 17864 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17865 17866 /* 17867 * Try and ASSERT the minimum possible references on the 17868 * conn early enough. Since we are executing on write side, 17869 * the connection is obviously not detached and that means 17870 * there is a ref each for TCP and IP. Since we are behind 17871 * the squeue, the minimum references needed are 3. If the 17872 * conn is in classifier hash list, there should be an 17873 * extra ref for that (we check both the possibilities). 17874 */ 17875 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17876 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17877 17878 ASSERT(DB_TYPE(mp) == M_DATA); 17879 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17880 17881 mutex_enter(&tcp->tcp_non_sq_lock); 17882 tcp->tcp_squeue_bytes -= msize; 17883 mutex_exit(&tcp->tcp_non_sq_lock); 17884 17885 /* Check to see if this connection wants to be re-fused. */ 17886 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17887 if (tcp->tcp_ipversion == IPV4_VERSION) { 17888 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17889 &tcp->tcp_saved_tcph); 17890 } else { 17891 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17892 &tcp->tcp_saved_tcph); 17893 } 17894 } 17895 /* Bypass tcp protocol for fused tcp loopback */ 17896 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17897 return; 17898 17899 mss = tcp->tcp_mss; 17900 if (tcp->tcp_xmit_zc_clean) 17901 mp = tcp_zcopy_backoff(tcp, mp, 0); 17902 17903 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17904 len = (int)(mp->b_wptr - mp->b_rptr); 17905 17906 /* 17907 * Criteria for fast path: 17908 * 17909 * 1. no unsent data 17910 * 2. single mblk in request 17911 * 3. connection established 17912 * 4. data in mblk 17913 * 5. len <= mss 17914 * 6. no tcp_valid bits 17915 */ 17916 if ((tcp->tcp_unsent != 0) || 17917 (tcp->tcp_cork) || 17918 (mp->b_cont != NULL) || 17919 (tcp->tcp_state != TCPS_ESTABLISHED) || 17920 (len == 0) || 17921 (len > mss) || 17922 (tcp->tcp_valid_bits != 0)) { 17923 tcp_wput_data(tcp, mp, B_FALSE); 17924 return; 17925 } 17926 17927 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17928 ASSERT(tcp->tcp_fin_sent == 0); 17929 17930 /* queue new packet onto retransmission queue */ 17931 if (tcp->tcp_xmit_head == NULL) { 17932 tcp->tcp_xmit_head = mp; 17933 } else { 17934 tcp->tcp_xmit_last->b_cont = mp; 17935 } 17936 tcp->tcp_xmit_last = mp; 17937 tcp->tcp_xmit_tail = mp; 17938 17939 /* find out how much we can send */ 17940 /* BEGIN CSTYLED */ 17941 /* 17942 * un-acked usable 17943 * |--------------|-----------------| 17944 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17945 */ 17946 /* END CSTYLED */ 17947 17948 /* start sending from tcp_snxt */ 17949 snxt = tcp->tcp_snxt; 17950 17951 /* 17952 * Check to see if this connection has been idled for some 17953 * time and no ACK is expected. If it is, we need to slow 17954 * start again to get back the connection's "self-clock" as 17955 * described in VJ's paper. 17956 * 17957 * Refer to the comment in tcp_mss_set() for the calculation 17958 * of tcp_cwnd after idle. 17959 */ 17960 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17961 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17962 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17963 } 17964 17965 usable = tcp->tcp_swnd; /* tcp window size */ 17966 if (usable > tcp->tcp_cwnd) 17967 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17968 usable -= snxt; /* subtract stuff already sent */ 17969 suna = tcp->tcp_suna; 17970 usable += suna; 17971 /* usable can be < 0 if the congestion window is smaller */ 17972 if (len > usable) { 17973 /* Can't send complete M_DATA in one shot */ 17974 goto slow; 17975 } 17976 17977 mutex_enter(&tcp->tcp_non_sq_lock); 17978 if (tcp->tcp_flow_stopped && 17979 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17980 tcp_clrqfull(tcp); 17981 } 17982 mutex_exit(&tcp->tcp_non_sq_lock); 17983 17984 /* 17985 * determine if anything to send (Nagle). 17986 * 17987 * 1. len < tcp_mss (i.e. small) 17988 * 2. unacknowledged data present 17989 * 3. len < nagle limit 17990 * 4. last packet sent < nagle limit (previous packet sent) 17991 */ 17992 if ((len < mss) && (snxt != suna) && 17993 (len < (int)tcp->tcp_naglim) && 17994 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17995 /* 17996 * This was the first unsent packet and normally 17997 * mss < xmit_hiwater so there is no need to worry 17998 * about flow control. The next packet will go 17999 * through the flow control check in tcp_wput_data(). 18000 */ 18001 /* leftover work from above */ 18002 tcp->tcp_unsent = len; 18003 tcp->tcp_xmit_tail_unsent = len; 18004 18005 return; 18006 } 18007 18008 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 18009 18010 if (snxt == suna) { 18011 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18012 } 18013 18014 /* we have always sent something */ 18015 tcp->tcp_rack_cnt = 0; 18016 18017 tcp->tcp_snxt = snxt + len; 18018 tcp->tcp_rack = tcp->tcp_rnxt; 18019 18020 if ((mp1 = dupb(mp)) == 0) 18021 goto no_memory; 18022 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 18023 mp->b_next = (mblk_t *)(uintptr_t)snxt; 18024 18025 /* adjust tcp header information */ 18026 tcph = tcp->tcp_tcph; 18027 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 18028 18029 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 18030 sum = (sum >> 16) + (sum & 0xFFFF); 18031 U16_TO_ABE16(sum, tcph->th_sum); 18032 18033 U32_TO_ABE32(snxt, tcph->th_seq); 18034 18035 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 18036 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 18037 BUMP_LOCAL(tcp->tcp_obsegs); 18038 18039 /* Update the latest receive window size in TCP header. */ 18040 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18041 tcph->th_win); 18042 18043 tcp->tcp_last_sent_len = (ushort_t)len; 18044 18045 plen = len + tcp->tcp_hdr_len; 18046 18047 if (tcp->tcp_ipversion == IPV4_VERSION) { 18048 tcp->tcp_ipha->ipha_length = htons(plen); 18049 } else { 18050 tcp->tcp_ip6h->ip6_plen = htons(plen - 18051 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 18052 } 18053 18054 /* see if we need to allocate a mblk for the headers */ 18055 hdrlen = tcp->tcp_hdr_len; 18056 rptr = mp1->b_rptr - hdrlen; 18057 db = mp1->b_datap; 18058 if ((db->db_ref != 2) || rptr < db->db_base || 18059 (!OK_32PTR(rptr))) { 18060 /* NOTE: we assume allocb returns an OK_32PTR */ 18061 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 18062 tcps->tcps_wroff_xtra, BPRI_MED); 18063 if (!mp) { 18064 freemsg(mp1); 18065 goto no_memory; 18066 } 18067 mp->b_cont = mp1; 18068 mp1 = mp; 18069 /* Leave room for Link Level header */ 18070 /* hdrlen = tcp->tcp_hdr_len; */ 18071 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 18072 mp1->b_wptr = &rptr[hdrlen]; 18073 } 18074 mp1->b_rptr = rptr; 18075 18076 /* Fill in the timestamp option. */ 18077 if (tcp->tcp_snd_ts_ok) { 18078 U32_TO_BE32((uint32_t)lbolt, 18079 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 18080 U32_TO_BE32(tcp->tcp_ts_recent, 18081 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 18082 } else { 18083 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18084 } 18085 18086 /* copy header into outgoing packet */ 18087 dst = (ipaddr_t *)rptr; 18088 src = (ipaddr_t *)tcp->tcp_iphc; 18089 dst[0] = src[0]; 18090 dst[1] = src[1]; 18091 dst[2] = src[2]; 18092 dst[3] = src[3]; 18093 dst[4] = src[4]; 18094 dst[5] = src[5]; 18095 dst[6] = src[6]; 18096 dst[7] = src[7]; 18097 dst[8] = src[8]; 18098 dst[9] = src[9]; 18099 if (hdrlen -= 40) { 18100 hdrlen >>= 2; 18101 dst += 10; 18102 src += 10; 18103 do { 18104 *dst++ = *src++; 18105 } while (--hdrlen); 18106 } 18107 18108 /* 18109 * Set the ECN info in the TCP header. Note that this 18110 * is not the template header. 18111 */ 18112 if (tcp->tcp_ecn_ok) { 18113 SET_ECT(tcp, rptr); 18114 18115 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18116 if (tcp->tcp_ecn_echo_on) 18117 tcph->th_flags[0] |= TH_ECE; 18118 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18119 tcph->th_flags[0] |= TH_CWR; 18120 tcp->tcp_ecn_cwr_sent = B_TRUE; 18121 } 18122 } 18123 18124 if (tcp->tcp_ip_forward_progress) { 18125 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 18126 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 18127 tcp->tcp_ip_forward_progress = B_FALSE; 18128 } 18129 tcp_send_data(tcp, tcp->tcp_wq, mp1); 18130 return; 18131 18132 /* 18133 * If we ran out of memory, we pretend to have sent the packet 18134 * and that it was lost on the wire. 18135 */ 18136 no_memory: 18137 return; 18138 18139 slow: 18140 /* leftover work from above */ 18141 tcp->tcp_unsent = len; 18142 tcp->tcp_xmit_tail_unsent = len; 18143 tcp_wput_data(tcp, NULL, B_FALSE); 18144 } 18145 18146 /* 18147 * The function called through squeue to get behind eager's perimeter to 18148 * finish the accept processing. 18149 */ 18150 /* ARGSUSED */ 18151 void 18152 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 18153 { 18154 conn_t *connp = (conn_t *)arg; 18155 tcp_t *tcp = connp->conn_tcp; 18156 queue_t *q = tcp->tcp_rq; 18157 mblk_t *mp1; 18158 mblk_t *stropt_mp = mp; 18159 struct stroptions *stropt; 18160 uint_t thwin; 18161 tcp_stack_t *tcps = tcp->tcp_tcps; 18162 18163 /* 18164 * Drop the eager's ref on the listener, that was placed when 18165 * this eager began life in tcp_conn_request. 18166 */ 18167 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 18168 18169 tcp->tcp_detached = B_FALSE; 18170 18171 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 18172 /* 18173 * Someone blewoff the eager before we could finish 18174 * the accept. 18175 * 18176 * The only reason eager exists it because we put in 18177 * a ref on it when conn ind went up. We need to send 18178 * a disconnect indication up while the last reference 18179 * on the eager will be dropped by the squeue when we 18180 * return. 18181 */ 18182 ASSERT(tcp->tcp_listener == NULL); 18183 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 18184 struct T_discon_ind *tdi; 18185 18186 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 18187 /* 18188 * Let us reuse the incoming mblk to avoid memory 18189 * allocation failure problems. We know that the 18190 * size of the incoming mblk i.e. stroptions is greater 18191 * than sizeof T_discon_ind. So the reallocb below 18192 * can't fail. 18193 */ 18194 freemsg(mp->b_cont); 18195 mp->b_cont = NULL; 18196 ASSERT(DB_REF(mp) == 1); 18197 mp = reallocb(mp, sizeof (struct T_discon_ind), 18198 B_FALSE); 18199 ASSERT(mp != NULL); 18200 DB_TYPE(mp) = M_PROTO; 18201 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 18202 tdi = (struct T_discon_ind *)mp->b_rptr; 18203 if (tcp->tcp_issocket) { 18204 tdi->DISCON_reason = ECONNREFUSED; 18205 tdi->SEQ_number = 0; 18206 } else { 18207 tdi->DISCON_reason = ENOPROTOOPT; 18208 tdi->SEQ_number = 18209 tcp->tcp_conn_req_seqnum; 18210 } 18211 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 18212 putnext(q, mp); 18213 } else { 18214 freemsg(mp); 18215 } 18216 if (tcp->tcp_hard_binding) { 18217 tcp->tcp_hard_binding = B_FALSE; 18218 tcp->tcp_hard_bound = B_TRUE; 18219 } 18220 return; 18221 } 18222 18223 mp1 = stropt_mp->b_cont; 18224 stropt_mp->b_cont = NULL; 18225 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 18226 stropt = (struct stroptions *)stropt_mp->b_rptr; 18227 18228 while (mp1 != NULL) { 18229 mp = mp1; 18230 mp1 = mp1->b_cont; 18231 mp->b_cont = NULL; 18232 tcp->tcp_drop_opt_ack_cnt++; 18233 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 18234 } 18235 mp = NULL; 18236 18237 /* 18238 * For a loopback connection with tcp_direct_sockfs on, note that 18239 * we don't have to protect tcp_rcv_list yet because synchronous 18240 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18241 * possibly race with us. 18242 */ 18243 18244 /* 18245 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18246 * properly. This is the first time we know of the acceptor' 18247 * queue. So we do it here. 18248 */ 18249 if (tcp->tcp_rcv_list == NULL) { 18250 /* 18251 * Recv queue is empty, tcp_rwnd should not have changed. 18252 * That means it should be equal to the listener's tcp_rwnd. 18253 */ 18254 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18255 } else { 18256 #ifdef DEBUG 18257 uint_t cnt = 0; 18258 18259 mp1 = tcp->tcp_rcv_list; 18260 while ((mp = mp1) != NULL) { 18261 mp1 = mp->b_next; 18262 cnt += msgdsize(mp); 18263 } 18264 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18265 #endif 18266 /* There is some data, add them back to get the max. */ 18267 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18268 } 18269 /* 18270 * This is the first time we run on the correct 18271 * queue after tcp_accept. So fix all the q parameters 18272 * here. 18273 */ 18274 stropt->so_flags = SO_HIWAT | SO_MAXBLK | SO_WROFF; 18275 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18276 18277 /* 18278 * Record the stream head's high water mark for this endpoint; 18279 * this is used for flow-control purposes. 18280 */ 18281 stropt->so_hiwat = tcp->tcp_fused ? 18282 tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat) : 18283 MAX(q->q_hiwat, tcps->tcps_sth_rcv_hiwat); 18284 18285 /* 18286 * Determine what write offset value to use depending on SACK and 18287 * whether the endpoint is fused or not. 18288 */ 18289 if (tcp->tcp_fused) { 18290 ASSERT(tcp->tcp_loopback); 18291 ASSERT(tcp->tcp_loopback_peer != NULL); 18292 /* 18293 * For fused tcp loopback, set the stream head's write 18294 * offset value to zero since we won't be needing any room 18295 * for TCP/IP headers. This would also improve performance 18296 * since it would reduce the amount of work done by kmem. 18297 * Non-fused tcp loopback case is handled separately below. 18298 */ 18299 stropt->so_wroff = 0; 18300 /* 18301 * Update the peer's transmit parameters according to 18302 * our recently calculated high water mark value. 18303 */ 18304 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18305 } else if (tcp->tcp_snd_sack_ok) { 18306 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18307 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18308 } else { 18309 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18310 tcps->tcps_wroff_xtra); 18311 } 18312 18313 /* 18314 * If this is endpoint is handling SSL, then reserve extra 18315 * offset and space at the end. 18316 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18317 * overriding the previous setting. The extra cost of signing and 18318 * encrypting multiple MSS-size records (12 of them with Ethernet), 18319 * instead of a single contiguous one by the stream head 18320 * largely outweighs the statistical reduction of ACKs, when 18321 * applicable. The peer will also save on decryption and verification 18322 * costs. 18323 */ 18324 if (tcp->tcp_kssl_ctx != NULL) { 18325 stropt->so_wroff += SSL3_WROFFSET; 18326 18327 stropt->so_flags |= SO_TAIL; 18328 stropt->so_tail = SSL3_MAX_TAIL_LEN; 18329 18330 stropt->so_flags |= SO_COPYOPT; 18331 stropt->so_copyopt = ZCVMUNSAFE; 18332 18333 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 18334 } 18335 18336 /* Send the options up */ 18337 putnext(q, stropt_mp); 18338 18339 /* 18340 * Pass up any data and/or a fin that has been received. 18341 * 18342 * Adjust receive window in case it had decreased 18343 * (because there is data <=> tcp_rcv_list != NULL) 18344 * while the connection was detached. Note that 18345 * in case the eager was flow-controlled, w/o this 18346 * code, the rwnd may never open up again! 18347 */ 18348 if (tcp->tcp_rcv_list != NULL) { 18349 /* We drain directly in case of fused tcp loopback */ 18350 sodirect_t *sodp; 18351 18352 if (!tcp->tcp_fused && canputnext(q)) { 18353 tcp->tcp_rwnd = q->q_hiwat; 18354 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 18355 << tcp->tcp_rcv_ws; 18356 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 18357 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18358 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 18359 tcp_xmit_ctl(NULL, 18360 tcp, (tcp->tcp_swnd == 0) ? 18361 tcp->tcp_suna : tcp->tcp_snxt, 18362 tcp->tcp_rnxt, TH_ACK); 18363 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 18364 } 18365 18366 } 18367 18368 SOD_PTR_ENTER(tcp, sodp); 18369 if (sodp != NULL) { 18370 /* Sodirect, move from rcv_list */ 18371 ASSERT(!tcp->tcp_fused); 18372 while ((mp = tcp->tcp_rcv_list) != NULL) { 18373 tcp->tcp_rcv_list = mp->b_next; 18374 mp->b_next = NULL; 18375 (void) tcp_rcv_sod_enqueue(tcp, sodp, mp, 18376 msgdsize(mp)); 18377 } 18378 tcp->tcp_rcv_last_head = NULL; 18379 tcp->tcp_rcv_last_tail = NULL; 18380 tcp->tcp_rcv_cnt = 0; 18381 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18382 /* sod_wakeup() did the mutex_exit() */ 18383 } else { 18384 /* Not sodirect, drain */ 18385 (void) tcp_rcv_drain(q, tcp); 18386 } 18387 18388 /* 18389 * For fused tcp loopback, back-enable peer endpoint 18390 * if it's currently flow-controlled. 18391 */ 18392 if (tcp->tcp_fused) { 18393 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18394 18395 ASSERT(peer_tcp != NULL); 18396 ASSERT(peer_tcp->tcp_fused); 18397 /* 18398 * In order to change the peer's tcp_flow_stopped, 18399 * we need to take locks for both end points. The 18400 * highest address is taken first. 18401 */ 18402 if (peer_tcp > tcp) { 18403 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18404 mutex_enter(&tcp->tcp_non_sq_lock); 18405 } else { 18406 mutex_enter(&tcp->tcp_non_sq_lock); 18407 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18408 } 18409 if (peer_tcp->tcp_flow_stopped) { 18410 tcp_clrqfull(peer_tcp); 18411 TCP_STAT(tcps, tcp_fusion_backenabled); 18412 } 18413 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18414 mutex_exit(&tcp->tcp_non_sq_lock); 18415 } 18416 } 18417 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18418 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18419 mp = tcp->tcp_ordrel_mp; 18420 tcp->tcp_ordrel_mp = NULL; 18421 tcp->tcp_ordrel_done = B_TRUE; 18422 putnext(q, mp); 18423 } 18424 if (tcp->tcp_hard_binding) { 18425 tcp->tcp_hard_binding = B_FALSE; 18426 tcp->tcp_hard_bound = B_TRUE; 18427 } 18428 18429 /* We can enable synchronous streams now */ 18430 if (tcp->tcp_fused) { 18431 tcp_fuse_syncstr_enable_pair(tcp); 18432 } 18433 18434 if (tcp->tcp_ka_enabled) { 18435 tcp->tcp_ka_last_intrvl = 0; 18436 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18437 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18438 } 18439 18440 /* 18441 * At this point, eager is fully established and will 18442 * have the following references - 18443 * 18444 * 2 references for connection to exist (1 for TCP and 1 for IP). 18445 * 1 reference for the squeue which will be dropped by the squeue as 18446 * soon as this function returns. 18447 * There will be 1 additonal reference for being in classifier 18448 * hash list provided something bad hasn't happened. 18449 */ 18450 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18451 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18452 } 18453 18454 /* 18455 * The function called through squeue to get behind listener's perimeter to 18456 * send a deffered conn_ind. 18457 */ 18458 /* ARGSUSED */ 18459 void 18460 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18461 { 18462 conn_t *connp = (conn_t *)arg; 18463 tcp_t *listener = connp->conn_tcp; 18464 18465 if (listener->tcp_state == TCPS_CLOSED || 18466 TCP_IS_DETACHED(listener)) { 18467 /* 18468 * If listener has closed, it would have caused a 18469 * a cleanup/blowoff to happen for the eager. 18470 */ 18471 tcp_t *tcp; 18472 struct T_conn_ind *conn_ind; 18473 18474 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18475 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18476 conn_ind->OPT_length); 18477 /* 18478 * We need to drop the ref on eager that was put 18479 * tcp_rput_data() before trying to send the conn_ind 18480 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18481 * and tcp_wput_accept() is sending this deferred conn_ind but 18482 * listener is closed so we drop the ref. 18483 */ 18484 CONN_DEC_REF(tcp->tcp_connp); 18485 freemsg(mp); 18486 return; 18487 } 18488 putnext(listener->tcp_rq, mp); 18489 } 18490 18491 18492 /* 18493 * This is the STREAMS entry point for T_CONN_RES coming down on 18494 * Acceptor STREAM when sockfs listener does accept processing. 18495 * Read the block comment on top of tcp_conn_request(). 18496 */ 18497 void 18498 tcp_wput_accept(queue_t *q, mblk_t *mp) 18499 { 18500 queue_t *rq = RD(q); 18501 struct T_conn_res *conn_res; 18502 tcp_t *eager; 18503 tcp_t *listener; 18504 struct T_ok_ack *ok; 18505 t_scalar_t PRIM_type; 18506 mblk_t *opt_mp; 18507 conn_t *econnp; 18508 18509 ASSERT(DB_TYPE(mp) == M_PROTO); 18510 18511 conn_res = (struct T_conn_res *)mp->b_rptr; 18512 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18513 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18514 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18515 if (mp != NULL) 18516 putnext(rq, mp); 18517 return; 18518 } 18519 switch (conn_res->PRIM_type) { 18520 case O_T_CONN_RES: 18521 case T_CONN_RES: 18522 /* 18523 * We pass up an err ack if allocb fails. This will 18524 * cause sockfs to issue a T_DISCON_REQ which will cause 18525 * tcp_eager_blowoff to be called. sockfs will then call 18526 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18527 * we need to do the allocb up here because we have to 18528 * make sure rq->q_qinfo->qi_qclose still points to the 18529 * correct function (tcpclose_accept) in case allocb 18530 * fails. 18531 */ 18532 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18533 if (opt_mp == NULL) { 18534 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18535 if (mp != NULL) 18536 putnext(rq, mp); 18537 return; 18538 } 18539 18540 bcopy(mp->b_rptr + conn_res->OPT_offset, 18541 &eager, conn_res->OPT_length); 18542 PRIM_type = conn_res->PRIM_type; 18543 mp->b_datap->db_type = M_PCPROTO; 18544 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18545 ok = (struct T_ok_ack *)mp->b_rptr; 18546 ok->PRIM_type = T_OK_ACK; 18547 ok->CORRECT_prim = PRIM_type; 18548 econnp = eager->tcp_connp; 18549 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18550 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18551 eager->tcp_rq = rq; 18552 eager->tcp_wq = q; 18553 rq->q_ptr = econnp; 18554 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18555 q->q_ptr = econnp; 18556 q->q_qinfo = &tcp_winit; 18557 listener = eager->tcp_listener; 18558 eager->tcp_issocket = B_TRUE; 18559 18560 /* 18561 * TCP is _D_SODIRECT and sockfs is directly above so 18562 * save shared sodirect_t pointer (if any). 18563 * 18564 * If tcp_fused and sodirect enabled disable it. 18565 */ 18566 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18567 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18568 /* Fused, disable sodirect */ 18569 mutex_enter(eager->tcp_sodirect->sod_lockp); 18570 SOD_DISABLE(eager->tcp_sodirect); 18571 mutex_exit(eager->tcp_sodirect->sod_lockp); 18572 eager->tcp_sodirect = NULL; 18573 } 18574 18575 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18576 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18577 ASSERT(econnp->conn_netstack == 18578 listener->tcp_connp->conn_netstack); 18579 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18580 18581 /* Put the ref for IP */ 18582 CONN_INC_REF(econnp); 18583 18584 /* 18585 * We should have minimum of 3 references on the conn 18586 * at this point. One each for TCP and IP and one for 18587 * the T_conn_ind that was sent up when the 3-way handshake 18588 * completed. In the normal case we would also have another 18589 * reference (making a total of 4) for the conn being in the 18590 * classifier hash list. However the eager could have received 18591 * an RST subsequently and tcp_closei_local could have removed 18592 * the eager from the classifier hash list, hence we can't 18593 * assert that reference. 18594 */ 18595 ASSERT(econnp->conn_ref >= 3); 18596 18597 /* 18598 * Send the new local address also up to sockfs. There 18599 * should already be enough space in the mp that came 18600 * down from soaccept(). 18601 */ 18602 if (eager->tcp_family == AF_INET) { 18603 sin_t *sin; 18604 18605 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18606 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18607 sin = (sin_t *)mp->b_wptr; 18608 mp->b_wptr += sizeof (sin_t); 18609 sin->sin_family = AF_INET; 18610 sin->sin_port = eager->tcp_lport; 18611 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18612 } else { 18613 sin6_t *sin6; 18614 18615 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18616 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18617 sin6 = (sin6_t *)mp->b_wptr; 18618 mp->b_wptr += sizeof (sin6_t); 18619 sin6->sin6_family = AF_INET6; 18620 sin6->sin6_port = eager->tcp_lport; 18621 if (eager->tcp_ipversion == IPV4_VERSION) { 18622 sin6->sin6_flowinfo = 0; 18623 IN6_IPADDR_TO_V4MAPPED( 18624 eager->tcp_ipha->ipha_src, 18625 &sin6->sin6_addr); 18626 } else { 18627 ASSERT(eager->tcp_ip6h != NULL); 18628 sin6->sin6_flowinfo = 18629 eager->tcp_ip6h->ip6_vcf & 18630 ~IPV6_VERS_AND_FLOW_MASK; 18631 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18632 } 18633 sin6->sin6_scope_id = 0; 18634 sin6->__sin6_src_id = 0; 18635 } 18636 18637 putnext(rq, mp); 18638 18639 opt_mp->b_datap->db_type = M_SETOPTS; 18640 opt_mp->b_wptr += sizeof (struct stroptions); 18641 18642 /* 18643 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18644 * from listener to acceptor. The message is chained on the 18645 * bind_mp which tcp_rput_other will send down to IP. 18646 */ 18647 if (listener->tcp_bound_if != 0) { 18648 /* allocate optmgmt req */ 18649 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18650 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 18651 sizeof (int)); 18652 if (mp != NULL) 18653 linkb(opt_mp, mp); 18654 } 18655 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18656 uint_t on = 1; 18657 18658 /* allocate optmgmt req */ 18659 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 18660 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 18661 if (mp != NULL) 18662 linkb(opt_mp, mp); 18663 } 18664 18665 18666 mutex_enter(&listener->tcp_eager_lock); 18667 18668 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18669 18670 tcp_t *tail; 18671 tcp_t *tcp; 18672 mblk_t *mp1; 18673 18674 tcp = listener->tcp_eager_prev_q0; 18675 /* 18676 * listener->tcp_eager_prev_q0 points to the TAIL of the 18677 * deferred T_conn_ind queue. We need to get to the head 18678 * of the queue in order to send up T_conn_ind the same 18679 * order as how the 3WHS is completed. 18680 */ 18681 while (tcp != listener) { 18682 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18683 !tcp->tcp_kssl_pending) 18684 break; 18685 else 18686 tcp = tcp->tcp_eager_prev_q0; 18687 } 18688 /* None of the pending eagers can be sent up now */ 18689 if (tcp == listener) 18690 goto no_more_eagers; 18691 18692 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18693 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18694 /* Move from q0 to q */ 18695 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18696 listener->tcp_conn_req_cnt_q0--; 18697 listener->tcp_conn_req_cnt_q++; 18698 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18699 tcp->tcp_eager_prev_q0; 18700 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18701 tcp->tcp_eager_next_q0; 18702 tcp->tcp_eager_prev_q0 = NULL; 18703 tcp->tcp_eager_next_q0 = NULL; 18704 tcp->tcp_conn_def_q0 = B_FALSE; 18705 18706 /* Make sure the tcp isn't in the list of droppables */ 18707 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18708 tcp->tcp_eager_prev_drop_q0 == NULL); 18709 18710 /* 18711 * Insert at end of the queue because sockfs sends 18712 * down T_CONN_RES in chronological order. Leaving 18713 * the older conn indications at front of the queue 18714 * helps reducing search time. 18715 */ 18716 tail = listener->tcp_eager_last_q; 18717 if (tail != NULL) { 18718 tail->tcp_eager_next_q = tcp; 18719 } else { 18720 listener->tcp_eager_next_q = tcp; 18721 } 18722 listener->tcp_eager_last_q = tcp; 18723 tcp->tcp_eager_next_q = NULL; 18724 18725 /* Need to get inside the listener perimeter */ 18726 CONN_INC_REF(listener->tcp_connp); 18727 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 18728 tcp_send_pending, listener->tcp_connp, 18729 SQTAG_TCP_SEND_PENDING); 18730 } 18731 no_more_eagers: 18732 tcp_eager_unlink(eager); 18733 mutex_exit(&listener->tcp_eager_lock); 18734 18735 /* 18736 * At this point, the eager is detached from the listener 18737 * but we still have an extra refs on eager (apart from the 18738 * usual tcp references). The ref was placed in tcp_rput_data 18739 * before sending the conn_ind in tcp_send_conn_ind. 18740 * The ref will be dropped in tcp_accept_finish(). 18741 */ 18742 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 18743 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 18744 return; 18745 default: 18746 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18747 if (mp != NULL) 18748 putnext(rq, mp); 18749 return; 18750 } 18751 } 18752 18753 static int 18754 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18755 { 18756 sin_t *sin = (sin_t *)sa; 18757 sin6_t *sin6 = (sin6_t *)sa; 18758 18759 switch (tcp->tcp_family) { 18760 case AF_INET: 18761 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18762 18763 if (*salenp < sizeof (sin_t)) 18764 return (EINVAL); 18765 18766 *sin = sin_null; 18767 sin->sin_family = AF_INET; 18768 sin->sin_port = tcp->tcp_lport; 18769 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18770 break; 18771 18772 case AF_INET6: 18773 if (*salenp < sizeof (sin6_t)) 18774 return (EINVAL); 18775 18776 *sin6 = sin6_null; 18777 sin6->sin6_family = AF_INET6; 18778 sin6->sin6_port = tcp->tcp_lport; 18779 if (tcp->tcp_ipversion == IPV4_VERSION) { 18780 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18781 &sin6->sin6_addr); 18782 } else { 18783 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18784 } 18785 break; 18786 } 18787 18788 return (0); 18789 } 18790 18791 static int 18792 tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18793 { 18794 sin_t *sin = (sin_t *)sa; 18795 sin6_t *sin6 = (sin6_t *)sa; 18796 18797 if (tcp->tcp_state < TCPS_SYN_RCVD) 18798 return (ENOTCONN); 18799 18800 switch (tcp->tcp_family) { 18801 case AF_INET: 18802 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18803 18804 if (*salenp < sizeof (sin_t)) 18805 return (EINVAL); 18806 18807 *sin = sin_null; 18808 sin->sin_family = AF_INET; 18809 sin->sin_port = tcp->tcp_fport; 18810 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18811 sin->sin_addr.s_addr); 18812 break; 18813 18814 case AF_INET6: 18815 if (*salenp < sizeof (sin6_t)) 18816 return (EINVAL); 18817 18818 *sin6 = sin6_null; 18819 sin6->sin6_family = AF_INET6; 18820 sin6->sin6_port = tcp->tcp_fport; 18821 sin6->sin6_addr = tcp->tcp_remote_v6; 18822 if (tcp->tcp_ipversion == IPV6_VERSION) { 18823 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18824 ~IPV6_VERS_AND_FLOW_MASK; 18825 } 18826 break; 18827 } 18828 18829 return (0); 18830 } 18831 18832 /* 18833 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18834 */ 18835 static void 18836 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18837 { 18838 void *data; 18839 mblk_t *datamp = mp->b_cont; 18840 tcp_t *tcp = Q_TO_TCP(q); 18841 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18842 18843 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18844 cmdp->cb_error = EPROTO; 18845 qreply(q, mp); 18846 return; 18847 } 18848 18849 data = datamp->b_rptr; 18850 18851 switch (cmdp->cb_cmd) { 18852 case TI_GETPEERNAME: 18853 cmdp->cb_error = tcp_getpeername(tcp, data, &cmdp->cb_len); 18854 break; 18855 case TI_GETMYNAME: 18856 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18857 break; 18858 default: 18859 cmdp->cb_error = EINVAL; 18860 break; 18861 } 18862 18863 qreply(q, mp); 18864 } 18865 18866 void 18867 tcp_wput(queue_t *q, mblk_t *mp) 18868 { 18869 conn_t *connp = Q_TO_CONN(q); 18870 tcp_t *tcp; 18871 void (*output_proc)(); 18872 t_scalar_t type; 18873 uchar_t *rptr; 18874 struct iocblk *iocp; 18875 uint32_t msize; 18876 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18877 18878 ASSERT(connp->conn_ref >= 2); 18879 18880 switch (DB_TYPE(mp)) { 18881 case M_DATA: 18882 tcp = connp->conn_tcp; 18883 ASSERT(tcp != NULL); 18884 18885 msize = msgdsize(mp); 18886 18887 mutex_enter(&tcp->tcp_non_sq_lock); 18888 tcp->tcp_squeue_bytes += msize; 18889 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18890 tcp_setqfull(tcp); 18891 } 18892 mutex_exit(&tcp->tcp_non_sq_lock); 18893 18894 CONN_INC_REF(connp); 18895 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18896 tcp_output, connp, SQTAG_TCP_OUTPUT); 18897 return; 18898 18899 case M_CMD: 18900 tcp_wput_cmdblk(q, mp); 18901 return; 18902 18903 case M_PROTO: 18904 case M_PCPROTO: 18905 /* 18906 * if it is a snmp message, don't get behind the squeue 18907 */ 18908 tcp = connp->conn_tcp; 18909 rptr = mp->b_rptr; 18910 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18911 type = ((union T_primitives *)rptr)->type; 18912 } else { 18913 if (tcp->tcp_debug) { 18914 (void) strlog(TCP_MOD_ID, 0, 1, 18915 SL_ERROR|SL_TRACE, 18916 "tcp_wput_proto, dropping one..."); 18917 } 18918 freemsg(mp); 18919 return; 18920 } 18921 if (type == T_SVR4_OPTMGMT_REQ) { 18922 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18923 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18924 cr)) { 18925 /* 18926 * This was a SNMP request 18927 */ 18928 return; 18929 } else { 18930 output_proc = tcp_wput_proto; 18931 } 18932 } else { 18933 output_proc = tcp_wput_proto; 18934 } 18935 break; 18936 case M_IOCTL: 18937 /* 18938 * Most ioctls can be processed right away without going via 18939 * squeues - process them right here. Those that do require 18940 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18941 * are processed by tcp_wput_ioctl(). 18942 */ 18943 iocp = (struct iocblk *)mp->b_rptr; 18944 tcp = connp->conn_tcp; 18945 18946 switch (iocp->ioc_cmd) { 18947 case TCP_IOC_ABORT_CONN: 18948 tcp_ioctl_abort_conn(q, mp); 18949 return; 18950 case TI_GETPEERNAME: 18951 case TI_GETMYNAME: 18952 mi_copyin(q, mp, NULL, 18953 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18954 return; 18955 case ND_SET: 18956 /* nd_getset does the necessary checks */ 18957 case ND_GET: 18958 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18959 CALL_IP_WPUT(connp, q, mp); 18960 return; 18961 } 18962 qreply(q, mp); 18963 return; 18964 case TCP_IOC_DEFAULT_Q: 18965 /* 18966 * Wants to be the default wq. Check the credentials 18967 * first, the rest is executed via squeue. 18968 */ 18969 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18970 iocp->ioc_error = EPERM; 18971 iocp->ioc_count = 0; 18972 mp->b_datap->db_type = M_IOCACK; 18973 qreply(q, mp); 18974 return; 18975 } 18976 output_proc = tcp_wput_ioctl; 18977 break; 18978 default: 18979 output_proc = tcp_wput_ioctl; 18980 break; 18981 } 18982 break; 18983 default: 18984 output_proc = tcp_wput_nondata; 18985 break; 18986 } 18987 18988 CONN_INC_REF(connp); 18989 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 18990 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 18991 } 18992 18993 /* 18994 * Initial STREAMS write side put() procedure for sockets. It tries to 18995 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18996 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18997 * are handled by tcp_wput() as usual. 18998 * 18999 * All further messages will also be handled by tcp_wput() because we cannot 19000 * be sure that the above short cut is safe later. 19001 */ 19002 static void 19003 tcp_wput_sock(queue_t *wq, mblk_t *mp) 19004 { 19005 conn_t *connp = Q_TO_CONN(wq); 19006 tcp_t *tcp = connp->conn_tcp; 19007 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 19008 19009 ASSERT(wq->q_qinfo == &tcp_sock_winit); 19010 wq->q_qinfo = &tcp_winit; 19011 19012 ASSERT(IPCL_IS_TCP(connp)); 19013 ASSERT(TCP_IS_SOCKET(tcp)); 19014 19015 if (DB_TYPE(mp) == M_PCPROTO && 19016 MBLKL(mp) == sizeof (struct T_capability_req) && 19017 car->PRIM_type == T_CAPABILITY_REQ) { 19018 tcp_capability_req(tcp, mp); 19019 return; 19020 } 19021 19022 tcp_wput(wq, mp); 19023 } 19024 19025 static boolean_t 19026 tcp_zcopy_check(tcp_t *tcp) 19027 { 19028 conn_t *connp = tcp->tcp_connp; 19029 ire_t *ire; 19030 boolean_t zc_enabled = B_FALSE; 19031 tcp_stack_t *tcps = tcp->tcp_tcps; 19032 19033 if (do_tcpzcopy == 2) 19034 zc_enabled = B_TRUE; 19035 else if (tcp->tcp_ipversion == IPV4_VERSION && 19036 IPCL_IS_CONNECTED(connp) && 19037 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 19038 connp->conn_dontroute == 0 && 19039 !connp->conn_nexthop_set && 19040 connp->conn_outgoing_ill == NULL && 19041 connp->conn_nofailover_ill == NULL && 19042 do_tcpzcopy == 1) { 19043 /* 19044 * the checks above closely resemble the fast path checks 19045 * in tcp_send_data(). 19046 */ 19047 mutex_enter(&connp->conn_lock); 19048 ire = connp->conn_ire_cache; 19049 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19050 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19051 IRE_REFHOLD(ire); 19052 if (ire->ire_stq != NULL) { 19053 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19054 19055 zc_enabled = ill && (ill->ill_capabilities & 19056 ILL_CAPAB_ZEROCOPY) && 19057 (ill->ill_zerocopy_capab-> 19058 ill_zerocopy_flags != 0); 19059 } 19060 IRE_REFRELE(ire); 19061 } 19062 mutex_exit(&connp->conn_lock); 19063 } 19064 tcp->tcp_snd_zcopy_on = zc_enabled; 19065 if (!TCP_IS_DETACHED(tcp)) { 19066 if (zc_enabled) { 19067 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 19068 TCP_STAT(tcps, tcp_zcopy_on); 19069 } else { 19070 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19071 TCP_STAT(tcps, tcp_zcopy_off); 19072 } 19073 } 19074 return (zc_enabled); 19075 } 19076 19077 static mblk_t * 19078 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19079 { 19080 tcp_stack_t *tcps = tcp->tcp_tcps; 19081 19082 if (do_tcpzcopy == 2) 19083 return (bp); 19084 else if (tcp->tcp_snd_zcopy_on) { 19085 tcp->tcp_snd_zcopy_on = B_FALSE; 19086 if (!TCP_IS_DETACHED(tcp)) { 19087 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 19088 TCP_STAT(tcps, tcp_zcopy_disable); 19089 } 19090 } 19091 return (tcp_zcopy_backoff(tcp, bp, 0)); 19092 } 19093 19094 /* 19095 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19096 * the original desballoca'ed segmapped mblk. 19097 */ 19098 static mblk_t * 19099 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19100 { 19101 mblk_t *head, *tail, *nbp; 19102 tcp_stack_t *tcps = tcp->tcp_tcps; 19103 19104 if (IS_VMLOANED_MBLK(bp)) { 19105 TCP_STAT(tcps, tcp_zcopy_backoff); 19106 if ((head = copyb(bp)) == NULL) { 19107 /* fail to backoff; leave it for the next backoff */ 19108 tcp->tcp_xmit_zc_clean = B_FALSE; 19109 return (bp); 19110 } 19111 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19112 if (fix_xmitlist) 19113 tcp_zcopy_notify(tcp); 19114 else 19115 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19116 } 19117 nbp = bp->b_cont; 19118 if (fix_xmitlist) { 19119 head->b_prev = bp->b_prev; 19120 head->b_next = bp->b_next; 19121 if (tcp->tcp_xmit_tail == bp) 19122 tcp->tcp_xmit_tail = head; 19123 } 19124 bp->b_next = NULL; 19125 bp->b_prev = NULL; 19126 freeb(bp); 19127 } else { 19128 head = bp; 19129 nbp = bp->b_cont; 19130 } 19131 tail = head; 19132 while (nbp) { 19133 if (IS_VMLOANED_MBLK(nbp)) { 19134 TCP_STAT(tcps, tcp_zcopy_backoff); 19135 if ((tail->b_cont = copyb(nbp)) == NULL) { 19136 tcp->tcp_xmit_zc_clean = B_FALSE; 19137 tail->b_cont = nbp; 19138 return (head); 19139 } 19140 tail = tail->b_cont; 19141 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19142 if (fix_xmitlist) 19143 tcp_zcopy_notify(tcp); 19144 else 19145 tail->b_datap->db_struioflag |= 19146 STRUIO_ZCNOTIFY; 19147 } 19148 bp = nbp; 19149 nbp = nbp->b_cont; 19150 if (fix_xmitlist) { 19151 tail->b_prev = bp->b_prev; 19152 tail->b_next = bp->b_next; 19153 if (tcp->tcp_xmit_tail == bp) 19154 tcp->tcp_xmit_tail = tail; 19155 } 19156 bp->b_next = NULL; 19157 bp->b_prev = NULL; 19158 freeb(bp); 19159 } else { 19160 tail->b_cont = nbp; 19161 tail = nbp; 19162 nbp = nbp->b_cont; 19163 } 19164 } 19165 if (fix_xmitlist) { 19166 tcp->tcp_xmit_last = tail; 19167 tcp->tcp_xmit_zc_clean = B_TRUE; 19168 } 19169 return (head); 19170 } 19171 19172 static void 19173 tcp_zcopy_notify(tcp_t *tcp) 19174 { 19175 struct stdata *stp; 19176 19177 if (tcp->tcp_detached) 19178 return; 19179 stp = STREAM(tcp->tcp_rq); 19180 mutex_enter(&stp->sd_lock); 19181 stp->sd_flag |= STZCNOTIFY; 19182 cv_broadcast(&stp->sd_zcopy_wait); 19183 mutex_exit(&stp->sd_lock); 19184 } 19185 19186 static boolean_t 19187 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19188 { 19189 ire_t *ire; 19190 conn_t *connp = tcp->tcp_connp; 19191 tcp_stack_t *tcps = tcp->tcp_tcps; 19192 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19193 19194 mutex_enter(&connp->conn_lock); 19195 ire = connp->conn_ire_cache; 19196 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19197 19198 if ((ire != NULL) && 19199 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19200 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19201 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19202 IRE_REFHOLD(ire); 19203 mutex_exit(&connp->conn_lock); 19204 } else { 19205 boolean_t cached = B_FALSE; 19206 ts_label_t *tsl; 19207 19208 /* force a recheck later on */ 19209 tcp->tcp_ire_ill_check_done = B_FALSE; 19210 19211 TCP_DBGSTAT(tcps, tcp_ire_null1); 19212 connp->conn_ire_cache = NULL; 19213 mutex_exit(&connp->conn_lock); 19214 19215 if (ire != NULL) 19216 IRE_REFRELE_NOTR(ire); 19217 19218 tsl = crgetlabel(CONN_CRED(connp)); 19219 ire = (dst ? 19220 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19221 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19222 connp->conn_zoneid, tsl, ipst)); 19223 19224 if (ire == NULL) { 19225 TCP_STAT(tcps, tcp_ire_null); 19226 return (B_FALSE); 19227 } 19228 19229 IRE_REFHOLD_NOTR(ire); 19230 19231 mutex_enter(&connp->conn_lock); 19232 if (CONN_CACHE_IRE(connp)) { 19233 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19234 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19235 TCP_CHECK_IREINFO(tcp, ire); 19236 connp->conn_ire_cache = ire; 19237 cached = B_TRUE; 19238 } 19239 rw_exit(&ire->ire_bucket->irb_lock); 19240 } 19241 mutex_exit(&connp->conn_lock); 19242 19243 /* 19244 * We can continue to use the ire but since it was 19245 * not cached, we should drop the extra reference. 19246 */ 19247 if (!cached) 19248 IRE_REFRELE_NOTR(ire); 19249 19250 /* 19251 * Rampart note: no need to select a new label here, since 19252 * labels are not allowed to change during the life of a TCP 19253 * connection. 19254 */ 19255 } 19256 19257 *irep = ire; 19258 19259 return (B_TRUE); 19260 } 19261 19262 /* 19263 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19264 * 19265 * 0 = success; 19266 * 1 = failed to find ire and ill. 19267 */ 19268 static boolean_t 19269 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19270 { 19271 ipha_t *ipha; 19272 ipaddr_t dst; 19273 ire_t *ire; 19274 ill_t *ill; 19275 conn_t *connp = tcp->tcp_connp; 19276 mblk_t *ire_fp_mp; 19277 tcp_stack_t *tcps = tcp->tcp_tcps; 19278 19279 if (mp != NULL) 19280 ipha = (ipha_t *)mp->b_rptr; 19281 else 19282 ipha = tcp->tcp_ipha; 19283 dst = ipha->ipha_dst; 19284 19285 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19286 return (B_FALSE); 19287 19288 if ((ire->ire_flags & RTF_MULTIRT) || 19289 (ire->ire_stq == NULL) || 19290 (ire->ire_nce == NULL) || 19291 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19292 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19293 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19294 TCP_STAT(tcps, tcp_ip_ire_send); 19295 IRE_REFRELE(ire); 19296 return (B_FALSE); 19297 } 19298 19299 ill = ire_to_ill(ire); 19300 if (connp->conn_outgoing_ill != NULL) { 19301 ill_t *conn_outgoing_ill = NULL; 19302 /* 19303 * Choose a good ill in the group to send the packets on. 19304 */ 19305 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19306 ill = ire_to_ill(ire); 19307 } 19308 ASSERT(ill != NULL); 19309 19310 if (!tcp->tcp_ire_ill_check_done) { 19311 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19312 tcp->tcp_ire_ill_check_done = B_TRUE; 19313 } 19314 19315 *irep = ire; 19316 *illp = ill; 19317 19318 return (B_TRUE); 19319 } 19320 19321 static void 19322 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19323 { 19324 ipha_t *ipha; 19325 ipaddr_t src; 19326 ipaddr_t dst; 19327 uint32_t cksum; 19328 ire_t *ire; 19329 uint16_t *up; 19330 ill_t *ill; 19331 conn_t *connp = tcp->tcp_connp; 19332 uint32_t hcksum_txflags = 0; 19333 mblk_t *ire_fp_mp; 19334 uint_t ire_fp_mp_len; 19335 tcp_stack_t *tcps = tcp->tcp_tcps; 19336 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19337 19338 ASSERT(DB_TYPE(mp) == M_DATA); 19339 19340 if (DB_CRED(mp) == NULL) 19341 mblk_setcred(mp, CONN_CRED(connp)); 19342 19343 ipha = (ipha_t *)mp->b_rptr; 19344 src = ipha->ipha_src; 19345 dst = ipha->ipha_dst; 19346 19347 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19348 19349 /* 19350 * Drop off fast path for IPv6 and also if options are present or 19351 * we need to resolve a TS label. 19352 */ 19353 if (tcp->tcp_ipversion != IPV4_VERSION || 19354 !IPCL_IS_CONNECTED(connp) || 19355 !CONN_IS_LSO_MD_FASTPATH(connp) || 19356 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19357 !connp->conn_ulp_labeled || 19358 ipha->ipha_ident == IP_HDR_INCLUDED || 19359 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19360 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19361 if (tcp->tcp_snd_zcopy_aware) 19362 mp = tcp_zcopy_disable(tcp, mp); 19363 TCP_STAT(tcps, tcp_ip_send); 19364 CALL_IP_WPUT(connp, q, mp); 19365 return; 19366 } 19367 19368 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19369 if (tcp->tcp_snd_zcopy_aware) 19370 mp = tcp_zcopy_backoff(tcp, mp, 0); 19371 CALL_IP_WPUT(connp, q, mp); 19372 return; 19373 } 19374 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19375 ire_fp_mp_len = MBLKL(ire_fp_mp); 19376 19377 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19378 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19379 #ifndef _BIG_ENDIAN 19380 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19381 #endif 19382 19383 /* 19384 * Check to see if we need to re-enable LSO/MDT for this connection 19385 * because it was previously disabled due to changes in the ill; 19386 * note that by doing it here, this re-enabling only applies when 19387 * the packet is not dispatched through CALL_IP_WPUT(). 19388 * 19389 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19390 * case, since that's how we ended up here. For IPv6, we do the 19391 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19392 */ 19393 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19394 /* 19395 * Restore LSO for this connection, so that next time around 19396 * it is eligible to go through tcp_lsosend() path again. 19397 */ 19398 TCP_STAT(tcps, tcp_lso_enabled); 19399 tcp->tcp_lso = B_TRUE; 19400 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19401 "interface %s\n", (void *)connp, ill->ill_name)); 19402 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19403 /* 19404 * Restore MDT for this connection, so that next time around 19405 * it is eligible to go through tcp_multisend() path again. 19406 */ 19407 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19408 tcp->tcp_mdt = B_TRUE; 19409 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19410 "interface %s\n", (void *)connp, ill->ill_name)); 19411 } 19412 19413 if (tcp->tcp_snd_zcopy_aware) { 19414 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19415 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19416 mp = tcp_zcopy_disable(tcp, mp); 19417 /* 19418 * we shouldn't need to reset ipha as the mp containing 19419 * ipha should never be a zero-copy mp. 19420 */ 19421 } 19422 19423 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19424 ASSERT(ill->ill_hcksum_capab != NULL); 19425 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19426 } 19427 19428 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19429 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19430 19431 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19432 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19433 19434 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19435 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19436 19437 /* Software checksum? */ 19438 if (DB_CKSUMFLAGS(mp) == 0) { 19439 TCP_STAT(tcps, tcp_out_sw_cksum); 19440 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19441 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19442 } 19443 19444 ipha->ipha_fragment_offset_and_flags |= 19445 (uint32_t)htons(ire->ire_frag_flag); 19446 19447 /* Calculate IP header checksum if hardware isn't capable */ 19448 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19449 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19450 ((uint16_t *)ipha)[4]); 19451 } 19452 19453 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19454 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19455 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19456 19457 UPDATE_OB_PKT_COUNT(ire); 19458 ire->ire_last_used_time = lbolt; 19459 19460 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19461 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19462 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19463 ntohs(ipha->ipha_length)); 19464 19465 if (ILL_DLS_CAPABLE(ill)) { 19466 /* 19467 * Send the packet directly to DLD, where it may be queued 19468 * depending on the availability of transmit resources at 19469 * the media layer. 19470 */ 19471 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 19472 } else { 19473 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 19474 DTRACE_PROBE4(ip4__physical__out__start, 19475 ill_t *, NULL, ill_t *, out_ill, 19476 ipha_t *, ipha, mblk_t *, mp); 19477 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19478 ipst->ips_ipv4firewall_physical_out, 19479 NULL, out_ill, ipha, mp, mp, 0, ipst); 19480 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19481 19482 if (mp != NULL) { 19483 if (ipst->ips_ipobs_enabled) { 19484 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, 19485 IP_REAL_ZONEID(connp->conn_zoneid, ipst), 19486 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, 19487 ipst); 19488 } 19489 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 19490 putnext(ire->ire_stq, mp); 19491 } 19492 } 19493 IRE_REFRELE(ire); 19494 } 19495 19496 /* 19497 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19498 * if the receiver shrinks the window, i.e. moves the right window to the 19499 * left, the we should not send new data, but should retransmit normally the 19500 * old unacked data between suna and suna + swnd. We might has sent data 19501 * that is now outside the new window, pretend that we didn't send it. 19502 */ 19503 static void 19504 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19505 { 19506 uint32_t snxt = tcp->tcp_snxt; 19507 mblk_t *xmit_tail; 19508 int32_t offset; 19509 19510 ASSERT(shrunk_count > 0); 19511 19512 /* Pretend we didn't send the data outside the window */ 19513 snxt -= shrunk_count; 19514 19515 /* Get the mblk and the offset in it per the shrunk window */ 19516 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19517 19518 ASSERT(xmit_tail != NULL); 19519 19520 /* Reset all the values per the now shrunk window */ 19521 tcp->tcp_snxt = snxt; 19522 tcp->tcp_xmit_tail = xmit_tail; 19523 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19524 offset; 19525 tcp->tcp_unsent += shrunk_count; 19526 19527 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19528 /* 19529 * Make sure the timer is running so that we will probe a zero 19530 * window. 19531 */ 19532 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19533 } 19534 19535 19536 /* 19537 * The TCP normal data output path. 19538 * NOTE: the logic of the fast path is duplicated from this function. 19539 */ 19540 static void 19541 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19542 { 19543 int len; 19544 mblk_t *local_time; 19545 mblk_t *mp1; 19546 uint32_t snxt; 19547 int tail_unsent; 19548 int tcpstate; 19549 int usable = 0; 19550 mblk_t *xmit_tail; 19551 queue_t *q = tcp->tcp_wq; 19552 int32_t mss; 19553 int32_t num_sack_blk = 0; 19554 int32_t tcp_hdr_len; 19555 int32_t tcp_tcp_hdr_len; 19556 int mdt_thres; 19557 int rc; 19558 tcp_stack_t *tcps = tcp->tcp_tcps; 19559 ip_stack_t *ipst; 19560 19561 tcpstate = tcp->tcp_state; 19562 if (mp == NULL) { 19563 /* 19564 * tcp_wput_data() with NULL mp should only be called when 19565 * there is unsent data. 19566 */ 19567 ASSERT(tcp->tcp_unsent > 0); 19568 /* Really tacky... but we need this for detached closes. */ 19569 len = tcp->tcp_unsent; 19570 goto data_null; 19571 } 19572 19573 #if CCS_STATS 19574 wrw_stats.tot.count++; 19575 wrw_stats.tot.bytes += msgdsize(mp); 19576 #endif 19577 ASSERT(mp->b_datap->db_type == M_DATA); 19578 /* 19579 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19580 * or before a connection attempt has begun. 19581 */ 19582 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19583 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19584 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19585 #ifdef DEBUG 19586 cmn_err(CE_WARN, 19587 "tcp_wput_data: data after ordrel, %s", 19588 tcp_display(tcp, NULL, 19589 DISP_ADDR_AND_PORT)); 19590 #else 19591 if (tcp->tcp_debug) { 19592 (void) strlog(TCP_MOD_ID, 0, 1, 19593 SL_TRACE|SL_ERROR, 19594 "tcp_wput_data: data after ordrel, %s\n", 19595 tcp_display(tcp, NULL, 19596 DISP_ADDR_AND_PORT)); 19597 } 19598 #endif /* DEBUG */ 19599 } 19600 if (tcp->tcp_snd_zcopy_aware && 19601 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19602 tcp_zcopy_notify(tcp); 19603 freemsg(mp); 19604 mutex_enter(&tcp->tcp_non_sq_lock); 19605 if (tcp->tcp_flow_stopped && 19606 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19607 tcp_clrqfull(tcp); 19608 } 19609 mutex_exit(&tcp->tcp_non_sq_lock); 19610 return; 19611 } 19612 19613 /* Strip empties */ 19614 for (;;) { 19615 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19616 (uintptr_t)INT_MAX); 19617 len = (int)(mp->b_wptr - mp->b_rptr); 19618 if (len > 0) 19619 break; 19620 mp1 = mp; 19621 mp = mp->b_cont; 19622 freeb(mp1); 19623 if (!mp) { 19624 return; 19625 } 19626 } 19627 19628 /* If we are the first on the list ... */ 19629 if (tcp->tcp_xmit_head == NULL) { 19630 tcp->tcp_xmit_head = mp; 19631 tcp->tcp_xmit_tail = mp; 19632 tcp->tcp_xmit_tail_unsent = len; 19633 } else { 19634 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19635 struct datab *dp; 19636 19637 mp1 = tcp->tcp_xmit_last; 19638 if (len < tcp_tx_pull_len && 19639 (dp = mp1->b_datap)->db_ref == 1 && 19640 dp->db_lim - mp1->b_wptr >= len) { 19641 ASSERT(len > 0); 19642 ASSERT(!mp1->b_cont); 19643 if (len == 1) { 19644 *mp1->b_wptr++ = *mp->b_rptr; 19645 } else { 19646 bcopy(mp->b_rptr, mp1->b_wptr, len); 19647 mp1->b_wptr += len; 19648 } 19649 if (mp1 == tcp->tcp_xmit_tail) 19650 tcp->tcp_xmit_tail_unsent += len; 19651 mp1->b_cont = mp->b_cont; 19652 if (tcp->tcp_snd_zcopy_aware && 19653 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19654 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19655 freeb(mp); 19656 mp = mp1; 19657 } else { 19658 tcp->tcp_xmit_last->b_cont = mp; 19659 } 19660 len += tcp->tcp_unsent; 19661 } 19662 19663 /* Tack on however many more positive length mblks we have */ 19664 if ((mp1 = mp->b_cont) != NULL) { 19665 do { 19666 int tlen; 19667 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19668 (uintptr_t)INT_MAX); 19669 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19670 if (tlen <= 0) { 19671 mp->b_cont = mp1->b_cont; 19672 freeb(mp1); 19673 } else { 19674 len += tlen; 19675 mp = mp1; 19676 } 19677 } while ((mp1 = mp->b_cont) != NULL); 19678 } 19679 tcp->tcp_xmit_last = mp; 19680 tcp->tcp_unsent = len; 19681 19682 if (urgent) 19683 usable = 1; 19684 19685 data_null: 19686 snxt = tcp->tcp_snxt; 19687 xmit_tail = tcp->tcp_xmit_tail; 19688 tail_unsent = tcp->tcp_xmit_tail_unsent; 19689 19690 /* 19691 * Note that tcp_mss has been adjusted to take into account the 19692 * timestamp option if applicable. Because SACK options do not 19693 * appear in every TCP segments and they are of variable lengths, 19694 * they cannot be included in tcp_mss. Thus we need to calculate 19695 * the actual segment length when we need to send a segment which 19696 * includes SACK options. 19697 */ 19698 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19699 int32_t opt_len; 19700 19701 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19702 tcp->tcp_num_sack_blk); 19703 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19704 2 + TCPOPT_HEADER_LEN; 19705 mss = tcp->tcp_mss - opt_len; 19706 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19707 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19708 } else { 19709 mss = tcp->tcp_mss; 19710 tcp_hdr_len = tcp->tcp_hdr_len; 19711 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19712 } 19713 19714 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19715 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19716 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19717 } 19718 if (tcpstate == TCPS_SYN_RCVD) { 19719 /* 19720 * The three-way connection establishment handshake is not 19721 * complete yet. We want to queue the data for transmission 19722 * after entering ESTABLISHED state (RFC793). A jump to 19723 * "done" label effectively leaves data on the queue. 19724 */ 19725 goto done; 19726 } else { 19727 int usable_r; 19728 19729 /* 19730 * In the special case when cwnd is zero, which can only 19731 * happen if the connection is ECN capable, return now. 19732 * New segments is sent using tcp_timer(). The timer 19733 * is set in tcp_rput_data(). 19734 */ 19735 if (tcp->tcp_cwnd == 0) { 19736 /* 19737 * Note that tcp_cwnd is 0 before 3-way handshake is 19738 * finished. 19739 */ 19740 ASSERT(tcp->tcp_ecn_ok || 19741 tcp->tcp_state < TCPS_ESTABLISHED); 19742 return; 19743 } 19744 19745 /* NOTE: trouble if xmitting while SYN not acked? */ 19746 usable_r = snxt - tcp->tcp_suna; 19747 usable_r = tcp->tcp_swnd - usable_r; 19748 19749 /* 19750 * Check if the receiver has shrunk the window. If 19751 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19752 * cannot be set as there is unsent data, so FIN cannot 19753 * be sent out. Otherwise, we need to take into account 19754 * of FIN as it consumes an "invisible" sequence number. 19755 */ 19756 ASSERT(tcp->tcp_fin_sent == 0); 19757 if (usable_r < 0) { 19758 /* 19759 * The receiver has shrunk the window and we have sent 19760 * -usable_r date beyond the window, re-adjust. 19761 * 19762 * If TCP window scaling is enabled, there can be 19763 * round down error as the advertised receive window 19764 * is actually right shifted n bits. This means that 19765 * the lower n bits info is wiped out. It will look 19766 * like the window is shrunk. Do a check here to 19767 * see if the shrunk amount is actually within the 19768 * error in window calculation. If it is, just 19769 * return. Note that this check is inside the 19770 * shrunk window check. This makes sure that even 19771 * though tcp_process_shrunk_swnd() is not called, 19772 * we will stop further processing. 19773 */ 19774 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19775 tcp_process_shrunk_swnd(tcp, -usable_r); 19776 } 19777 return; 19778 } 19779 19780 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19781 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19782 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19783 19784 /* usable = MIN(usable, unsent) */ 19785 if (usable_r > len) 19786 usable_r = len; 19787 19788 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19789 if (usable_r > 0) { 19790 usable = usable_r; 19791 } else { 19792 /* Bypass all other unnecessary processing. */ 19793 goto done; 19794 } 19795 } 19796 19797 local_time = (mblk_t *)lbolt; 19798 19799 /* 19800 * "Our" Nagle Algorithm. This is not the same as in the old 19801 * BSD. This is more in line with the true intent of Nagle. 19802 * 19803 * The conditions are: 19804 * 1. The amount of unsent data (or amount of data which can be 19805 * sent, whichever is smaller) is less than Nagle limit. 19806 * 2. The last sent size is also less than Nagle limit. 19807 * 3. There is unack'ed data. 19808 * 4. Urgent pointer is not set. Send urgent data ignoring the 19809 * Nagle algorithm. This reduces the probability that urgent 19810 * bytes get "merged" together. 19811 * 5. The app has not closed the connection. This eliminates the 19812 * wait time of the receiving side waiting for the last piece of 19813 * (small) data. 19814 * 19815 * If all are satisified, exit without sending anything. Note 19816 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19817 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19818 * 4095). 19819 */ 19820 if (usable < (int)tcp->tcp_naglim && 19821 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19822 snxt != tcp->tcp_suna && 19823 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19824 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19825 goto done; 19826 } 19827 19828 if (tcp->tcp_cork) { 19829 /* 19830 * if the tcp->tcp_cork option is set, then we have to force 19831 * TCP not to send partial segment (smaller than MSS bytes). 19832 * We are calculating the usable now based on full mss and 19833 * will save the rest of remaining data for later. 19834 */ 19835 if (usable < mss) 19836 goto done; 19837 usable = (usable / mss) * mss; 19838 } 19839 19840 /* Update the latest receive window size in TCP header. */ 19841 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19842 tcp->tcp_tcph->th_win); 19843 19844 /* 19845 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19846 * 19847 * 1. Simple TCP/IP{v4,v6} (no options). 19848 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19849 * 3. If the TCP connection is in ESTABLISHED state. 19850 * 4. The TCP is not detached. 19851 * 19852 * If any of the above conditions have changed during the 19853 * connection, stop using LSO/MDT and restore the stream head 19854 * parameters accordingly. 19855 */ 19856 ipst = tcps->tcps_netstack->netstack_ip; 19857 19858 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19859 ((tcp->tcp_ipversion == IPV4_VERSION && 19860 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19861 (tcp->tcp_ipversion == IPV6_VERSION && 19862 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19863 tcp->tcp_state != TCPS_ESTABLISHED || 19864 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19865 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19866 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19867 if (tcp->tcp_lso) { 19868 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19869 tcp->tcp_lso = B_FALSE; 19870 } else { 19871 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19872 tcp->tcp_mdt = B_FALSE; 19873 } 19874 19875 /* Anything other than detached is considered pathological */ 19876 if (!TCP_IS_DETACHED(tcp)) { 19877 if (tcp->tcp_lso) 19878 TCP_STAT(tcps, tcp_lso_disabled); 19879 else 19880 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19881 (void) tcp_maxpsz_set(tcp, B_TRUE); 19882 } 19883 } 19884 19885 /* Use MDT if sendable amount is greater than the threshold */ 19886 if (tcp->tcp_mdt && 19887 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19888 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19889 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19890 (tcp->tcp_valid_bits == 0 || 19891 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19892 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19893 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19894 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19895 local_time, mdt_thres); 19896 } else { 19897 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19898 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19899 local_time, INT_MAX); 19900 } 19901 19902 /* Pretend that all we were trying to send really got sent */ 19903 if (rc < 0 && tail_unsent < 0) { 19904 do { 19905 xmit_tail = xmit_tail->b_cont; 19906 xmit_tail->b_prev = local_time; 19907 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19908 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19909 tail_unsent += (int)(xmit_tail->b_wptr - 19910 xmit_tail->b_rptr); 19911 } while (tail_unsent < 0); 19912 } 19913 done:; 19914 tcp->tcp_xmit_tail = xmit_tail; 19915 tcp->tcp_xmit_tail_unsent = tail_unsent; 19916 len = tcp->tcp_snxt - snxt; 19917 if (len) { 19918 /* 19919 * If new data was sent, need to update the notsack 19920 * list, which is, afterall, data blocks that have 19921 * not been sack'ed by the receiver. New data is 19922 * not sack'ed. 19923 */ 19924 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19925 /* len is a negative value. */ 19926 tcp->tcp_pipe -= len; 19927 tcp_notsack_update(&(tcp->tcp_notsack_list), 19928 tcp->tcp_snxt, snxt, 19929 &(tcp->tcp_num_notsack_blk), 19930 &(tcp->tcp_cnt_notsack_list)); 19931 } 19932 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19933 tcp->tcp_rack = tcp->tcp_rnxt; 19934 tcp->tcp_rack_cnt = 0; 19935 if ((snxt + len) == tcp->tcp_suna) { 19936 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19937 } 19938 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19939 /* 19940 * Didn't send anything. Make sure the timer is running 19941 * so that we will probe a zero window. 19942 */ 19943 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19944 } 19945 /* Note that len is the amount we just sent but with a negative sign */ 19946 tcp->tcp_unsent += len; 19947 mutex_enter(&tcp->tcp_non_sq_lock); 19948 if (tcp->tcp_flow_stopped) { 19949 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19950 tcp_clrqfull(tcp); 19951 } 19952 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19953 tcp_setqfull(tcp); 19954 } 19955 mutex_exit(&tcp->tcp_non_sq_lock); 19956 } 19957 19958 /* 19959 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19960 * outgoing TCP header with the template header, as well as other 19961 * options such as time-stamp, ECN and/or SACK. 19962 */ 19963 static void 19964 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19965 { 19966 tcph_t *tcp_tmpl, *tcp_h; 19967 uint32_t *dst, *src; 19968 int hdrlen; 19969 19970 ASSERT(OK_32PTR(rptr)); 19971 19972 /* Template header */ 19973 tcp_tmpl = tcp->tcp_tcph; 19974 19975 /* Header of outgoing packet */ 19976 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19977 19978 /* dst and src are opaque 32-bit fields, used for copying */ 19979 dst = (uint32_t *)rptr; 19980 src = (uint32_t *)tcp->tcp_iphc; 19981 hdrlen = tcp->tcp_hdr_len; 19982 19983 /* Fill time-stamp option if needed */ 19984 if (tcp->tcp_snd_ts_ok) { 19985 U32_TO_BE32((uint32_t)now, 19986 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19987 U32_TO_BE32(tcp->tcp_ts_recent, 19988 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19989 } else { 19990 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19991 } 19992 19993 /* 19994 * Copy the template header; is this really more efficient than 19995 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19996 * but perhaps not for other scenarios. 19997 */ 19998 dst[0] = src[0]; 19999 dst[1] = src[1]; 20000 dst[2] = src[2]; 20001 dst[3] = src[3]; 20002 dst[4] = src[4]; 20003 dst[5] = src[5]; 20004 dst[6] = src[6]; 20005 dst[7] = src[7]; 20006 dst[8] = src[8]; 20007 dst[9] = src[9]; 20008 if (hdrlen -= 40) { 20009 hdrlen >>= 2; 20010 dst += 10; 20011 src += 10; 20012 do { 20013 *dst++ = *src++; 20014 } while (--hdrlen); 20015 } 20016 20017 /* 20018 * Set the ECN info in the TCP header if it is not a zero 20019 * window probe. Zero window probe is only sent in 20020 * tcp_wput_data() and tcp_timer(). 20021 */ 20022 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 20023 SET_ECT(tcp, rptr); 20024 20025 if (tcp->tcp_ecn_echo_on) 20026 tcp_h->th_flags[0] |= TH_ECE; 20027 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 20028 tcp_h->th_flags[0] |= TH_CWR; 20029 tcp->tcp_ecn_cwr_sent = B_TRUE; 20030 } 20031 } 20032 20033 /* Fill in SACK options */ 20034 if (num_sack_blk > 0) { 20035 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 20036 sack_blk_t *tmp; 20037 int32_t i; 20038 20039 wptr[0] = TCPOPT_NOP; 20040 wptr[1] = TCPOPT_NOP; 20041 wptr[2] = TCPOPT_SACK; 20042 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20043 sizeof (sack_blk_t); 20044 wptr += TCPOPT_REAL_SACK_LEN; 20045 20046 tmp = tcp->tcp_sack_list; 20047 for (i = 0; i < num_sack_blk; i++) { 20048 U32_TO_BE32(tmp[i].begin, wptr); 20049 wptr += sizeof (tcp_seq); 20050 U32_TO_BE32(tmp[i].end, wptr); 20051 wptr += sizeof (tcp_seq); 20052 } 20053 tcp_h->th_offset_and_rsrvd[0] += 20054 ((num_sack_blk * 2 + 1) << 4); 20055 } 20056 } 20057 20058 /* 20059 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20060 * the destination address and SAP attribute, and if necessary, the 20061 * hardware checksum offload attribute to a Multidata message. 20062 */ 20063 static int 20064 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20065 const uint32_t start, const uint32_t stuff, const uint32_t end, 20066 const uint32_t flags, tcp_stack_t *tcps) 20067 { 20068 /* Add global destination address & SAP attribute */ 20069 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20070 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20071 "destination address+SAP\n")); 20072 20073 if (dlmp != NULL) 20074 TCP_STAT(tcps, tcp_mdt_allocfail); 20075 return (-1); 20076 } 20077 20078 /* Add global hwcksum attribute */ 20079 if (hwcksum && 20080 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20081 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20082 "checksum attribute\n")); 20083 20084 TCP_STAT(tcps, tcp_mdt_allocfail); 20085 return (-1); 20086 } 20087 20088 return (0); 20089 } 20090 20091 /* 20092 * Smaller and private version of pdescinfo_t used specifically for TCP, 20093 * which allows for only two payload spans per packet. 20094 */ 20095 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20096 20097 /* 20098 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20099 * scheme, and returns one the following: 20100 * 20101 * -1 = failed allocation. 20102 * 0 = success; burst count reached, or usable send window is too small, 20103 * and that we'd rather wait until later before sending again. 20104 */ 20105 static int 20106 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20107 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20108 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20109 const int mdt_thres) 20110 { 20111 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20112 multidata_t *mmd; 20113 uint_t obsegs, obbytes, hdr_frag_sz; 20114 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20115 int num_burst_seg, max_pld; 20116 pdesc_t *pkt; 20117 tcp_pdescinfo_t tcp_pkt_info; 20118 pdescinfo_t *pkt_info; 20119 int pbuf_idx, pbuf_idx_nxt; 20120 int seg_len, len, spill, af; 20121 boolean_t add_buffer, zcopy, clusterwide; 20122 boolean_t rconfirm = B_FALSE; 20123 boolean_t done = B_FALSE; 20124 uint32_t cksum; 20125 uint32_t hwcksum_flags; 20126 ire_t *ire = NULL; 20127 ill_t *ill; 20128 ipha_t *ipha; 20129 ip6_t *ip6h; 20130 ipaddr_t src, dst; 20131 ill_zerocopy_capab_t *zc_cap = NULL; 20132 uint16_t *up; 20133 int err; 20134 conn_t *connp; 20135 tcp_stack_t *tcps = tcp->tcp_tcps; 20136 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20137 int usable_mmd, tail_unsent_mmd; 20138 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20139 mblk_t *xmit_tail_mmd; 20140 20141 #ifdef _BIG_ENDIAN 20142 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20143 #else 20144 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20145 #endif 20146 20147 #define PREP_NEW_MULTIDATA() { \ 20148 mmd = NULL; \ 20149 md_mp = md_hbuf = NULL; \ 20150 cur_hdr_off = 0; \ 20151 max_pld = tcp->tcp_mdt_max_pld; \ 20152 pbuf_idx = pbuf_idx_nxt = -1; \ 20153 add_buffer = B_TRUE; \ 20154 zcopy = B_FALSE; \ 20155 } 20156 20157 #define PREP_NEW_PBUF() { \ 20158 md_pbuf = md_pbuf_nxt = NULL; \ 20159 pbuf_idx = pbuf_idx_nxt = -1; \ 20160 cur_pld_off = 0; \ 20161 first_snxt = *snxt; \ 20162 ASSERT(*tail_unsent > 0); \ 20163 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20164 } 20165 20166 ASSERT(mdt_thres >= mss); 20167 ASSERT(*usable > 0 && *usable > mdt_thres); 20168 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20169 ASSERT(!TCP_IS_DETACHED(tcp)); 20170 ASSERT(tcp->tcp_valid_bits == 0 || 20171 tcp->tcp_valid_bits == TCP_FSS_VALID); 20172 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20173 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20174 (tcp->tcp_ipversion == IPV6_VERSION && 20175 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20176 20177 connp = tcp->tcp_connp; 20178 ASSERT(connp != NULL); 20179 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20180 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20181 20182 usable_mmd = tail_unsent_mmd = 0; 20183 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20184 xmit_tail_mmd = NULL; 20185 /* 20186 * Note that tcp will only declare at most 2 payload spans per 20187 * packet, which is much lower than the maximum allowable number 20188 * of packet spans per Multidata. For this reason, we use the 20189 * privately declared and smaller descriptor info structure, in 20190 * order to save some stack space. 20191 */ 20192 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20193 20194 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20195 if (af == AF_INET) { 20196 dst = tcp->tcp_ipha->ipha_dst; 20197 src = tcp->tcp_ipha->ipha_src; 20198 ASSERT(!CLASSD(dst)); 20199 } 20200 ASSERT(af == AF_INET || 20201 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20202 20203 obsegs = obbytes = 0; 20204 num_burst_seg = tcp->tcp_snd_burst; 20205 md_mp_head = NULL; 20206 PREP_NEW_MULTIDATA(); 20207 20208 /* 20209 * Before we go on further, make sure there is an IRE that we can 20210 * use, and that the ILL supports MDT. Otherwise, there's no point 20211 * in proceeding any further, and we should just hand everything 20212 * off to the legacy path. 20213 */ 20214 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20215 goto legacy_send_no_md; 20216 20217 ASSERT(ire != NULL); 20218 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20219 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20220 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20221 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20222 /* 20223 * If we do support loopback for MDT (which requires modifications 20224 * to the receiving paths), the following assertions should go away, 20225 * and we would be sending the Multidata to loopback conn later on. 20226 */ 20227 ASSERT(!IRE_IS_LOCAL(ire)); 20228 ASSERT(ire->ire_stq != NULL); 20229 20230 ill = ire_to_ill(ire); 20231 ASSERT(ill != NULL); 20232 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20233 20234 if (!tcp->tcp_ire_ill_check_done) { 20235 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20236 tcp->tcp_ire_ill_check_done = B_TRUE; 20237 } 20238 20239 /* 20240 * If the underlying interface conditions have changed, or if the 20241 * new interface does not support MDT, go back to legacy path. 20242 */ 20243 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20244 /* don't go through this path anymore for this connection */ 20245 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20246 tcp->tcp_mdt = B_FALSE; 20247 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20248 "interface %s\n", (void *)connp, ill->ill_name)); 20249 /* IRE will be released prior to returning */ 20250 goto legacy_send_no_md; 20251 } 20252 20253 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20254 zc_cap = ill->ill_zerocopy_capab; 20255 20256 /* 20257 * Check if we can take tcp fast-path. Note that "incomplete" 20258 * ire's (where the link-layer for next hop is not resolved 20259 * or where the fast-path header in nce_fp_mp is not available 20260 * yet) are sent down the legacy (slow) path. 20261 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20262 */ 20263 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20264 /* IRE will be released prior to returning */ 20265 goto legacy_send_no_md; 20266 } 20267 20268 /* go to legacy path if interface doesn't support zerocopy */ 20269 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20270 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20271 /* IRE will be released prior to returning */ 20272 goto legacy_send_no_md; 20273 } 20274 20275 /* does the interface support hardware checksum offload? */ 20276 hwcksum_flags = 0; 20277 if (ILL_HCKSUM_CAPABLE(ill) && 20278 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20279 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20280 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20281 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20282 HCKSUM_IPHDRCKSUM) 20283 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20284 20285 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20286 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20287 hwcksum_flags |= HCK_FULLCKSUM; 20288 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20289 HCKSUM_INET_PARTIAL) 20290 hwcksum_flags |= HCK_PARTIALCKSUM; 20291 } 20292 20293 /* 20294 * Each header fragment consists of the leading extra space, 20295 * followed by the TCP/IP header, and the trailing extra space. 20296 * We make sure that each header fragment begins on a 32-bit 20297 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20298 * aligned in tcp_mdt_update). 20299 */ 20300 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20301 tcp->tcp_mdt_hdr_tail), 4); 20302 20303 /* are we starting from the beginning of data block? */ 20304 if (*tail_unsent == 0) { 20305 *xmit_tail = (*xmit_tail)->b_cont; 20306 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20307 *tail_unsent = (int)MBLKL(*xmit_tail); 20308 } 20309 20310 /* 20311 * Here we create one or more Multidata messages, each made up of 20312 * one header buffer and up to N payload buffers. This entire 20313 * operation is done within two loops: 20314 * 20315 * The outer loop mostly deals with creating the Multidata message, 20316 * as well as the header buffer that gets added to it. It also 20317 * links the Multidata messages together such that all of them can 20318 * be sent down to the lower layer in a single putnext call; this 20319 * linking behavior depends on the tcp_mdt_chain tunable. 20320 * 20321 * The inner loop takes an existing Multidata message, and adds 20322 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20323 * packetizes those buffers by filling up the corresponding header 20324 * buffer fragments with the proper IP and TCP headers, and by 20325 * describing the layout of each packet in the packet descriptors 20326 * that get added to the Multidata. 20327 */ 20328 do { 20329 /* 20330 * If usable send window is too small, or data blocks in 20331 * transmit list are smaller than our threshold (i.e. app 20332 * performs large writes followed by small ones), we hand 20333 * off the control over to the legacy path. Note that we'll 20334 * get back the control once it encounters a large block. 20335 */ 20336 if (*usable < mss || (*tail_unsent <= mdt_thres && 20337 (*xmit_tail)->b_cont != NULL && 20338 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20339 /* send down what we've got so far */ 20340 if (md_mp_head != NULL) { 20341 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20342 obsegs, obbytes, &rconfirm); 20343 } 20344 /* 20345 * Pass control over to tcp_send(), but tell it to 20346 * return to us once a large-size transmission is 20347 * possible. 20348 */ 20349 TCP_STAT(tcps, tcp_mdt_legacy_small); 20350 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20351 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20352 tail_unsent, xmit_tail, local_time, 20353 mdt_thres)) <= 0) { 20354 /* burst count reached, or alloc failed */ 20355 IRE_REFRELE(ire); 20356 return (err); 20357 } 20358 20359 /* tcp_send() may have sent everything, so check */ 20360 if (*usable <= 0) { 20361 IRE_REFRELE(ire); 20362 return (0); 20363 } 20364 20365 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20366 /* 20367 * We may have delivered the Multidata, so make sure 20368 * to re-initialize before the next round. 20369 */ 20370 md_mp_head = NULL; 20371 obsegs = obbytes = 0; 20372 num_burst_seg = tcp->tcp_snd_burst; 20373 PREP_NEW_MULTIDATA(); 20374 20375 /* are we starting from the beginning of data block? */ 20376 if (*tail_unsent == 0) { 20377 *xmit_tail = (*xmit_tail)->b_cont; 20378 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20379 (uintptr_t)INT_MAX); 20380 *tail_unsent = (int)MBLKL(*xmit_tail); 20381 } 20382 } 20383 /* 20384 * Record current values for parameters we may need to pass 20385 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20386 * each iteration of the outer loop (each multidata message 20387 * creation). If we have a failure in the inner loop, we send 20388 * any complete multidata messages we have before reverting 20389 * to using the traditional non-md path. 20390 */ 20391 snxt_mmd = *snxt; 20392 usable_mmd = *usable; 20393 xmit_tail_mmd = *xmit_tail; 20394 tail_unsent_mmd = *tail_unsent; 20395 obsegs_mmd = obsegs; 20396 obbytes_mmd = obbytes; 20397 20398 /* 20399 * max_pld limits the number of mblks in tcp's transmit 20400 * queue that can be added to a Multidata message. Once 20401 * this counter reaches zero, no more additional mblks 20402 * can be added to it. What happens afterwards depends 20403 * on whether or not we are set to chain the Multidata 20404 * messages. If we are to link them together, reset 20405 * max_pld to its original value (tcp_mdt_max_pld) and 20406 * prepare to create a new Multidata message which will 20407 * get linked to md_mp_head. Else, leave it alone and 20408 * let the inner loop break on its own. 20409 */ 20410 if (tcp_mdt_chain && max_pld == 0) 20411 PREP_NEW_MULTIDATA(); 20412 20413 /* adding a payload buffer; re-initialize values */ 20414 if (add_buffer) 20415 PREP_NEW_PBUF(); 20416 20417 /* 20418 * If we don't have a Multidata, either because we just 20419 * (re)entered this outer loop, or after we branched off 20420 * to tcp_send above, setup the Multidata and header 20421 * buffer to be used. 20422 */ 20423 if (md_mp == NULL) { 20424 int md_hbuflen; 20425 uint32_t start, stuff; 20426 20427 /* 20428 * Calculate Multidata header buffer size large enough 20429 * to hold all of the headers that can possibly be 20430 * sent at this moment. We'd rather over-estimate 20431 * the size than running out of space; this is okay 20432 * since this buffer is small anyway. 20433 */ 20434 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20435 20436 /* 20437 * Start and stuff offset for partial hardware 20438 * checksum offload; these are currently for IPv4. 20439 * For full checksum offload, they are set to zero. 20440 */ 20441 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20442 if (af == AF_INET) { 20443 start = IP_SIMPLE_HDR_LENGTH; 20444 stuff = IP_SIMPLE_HDR_LENGTH + 20445 TCP_CHECKSUM_OFFSET; 20446 } else { 20447 start = IPV6_HDR_LEN; 20448 stuff = IPV6_HDR_LEN + 20449 TCP_CHECKSUM_OFFSET; 20450 } 20451 } else { 20452 start = stuff = 0; 20453 } 20454 20455 /* 20456 * Create the header buffer, Multidata, as well as 20457 * any necessary attributes (destination address, 20458 * SAP and hardware checksum offload) that should 20459 * be associated with the Multidata message. 20460 */ 20461 ASSERT(cur_hdr_off == 0); 20462 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20463 ((md_hbuf->b_wptr += md_hbuflen), 20464 (mmd = mmd_alloc(md_hbuf, &md_mp, 20465 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20466 /* fastpath mblk */ 20467 ire->ire_nce->nce_res_mp, 20468 /* hardware checksum enabled */ 20469 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20470 /* hardware checksum offsets */ 20471 start, stuff, 0, 20472 /* hardware checksum flag */ 20473 hwcksum_flags, tcps) != 0)) { 20474 legacy_send: 20475 /* 20476 * We arrive here from a failure within the 20477 * inner (packetizer) loop or we fail one of 20478 * the conditionals above. We restore the 20479 * previously checkpointed values for: 20480 * xmit_tail 20481 * usable 20482 * tail_unsent 20483 * snxt 20484 * obbytes 20485 * obsegs 20486 * We should then be able to dispatch any 20487 * complete multidata before reverting to the 20488 * traditional path with consistent parameters 20489 * (the inner loop updates these as it 20490 * iterates). 20491 */ 20492 *xmit_tail = xmit_tail_mmd; 20493 *usable = usable_mmd; 20494 *tail_unsent = tail_unsent_mmd; 20495 *snxt = snxt_mmd; 20496 obbytes = obbytes_mmd; 20497 obsegs = obsegs_mmd; 20498 if (md_mp != NULL) { 20499 /* Unlink message from the chain */ 20500 if (md_mp_head != NULL) { 20501 err = (intptr_t)rmvb(md_mp_head, 20502 md_mp); 20503 /* 20504 * We can't assert that rmvb 20505 * did not return -1, since we 20506 * may get here before linkb 20507 * happens. We do, however, 20508 * check if we just removed the 20509 * only element in the list. 20510 */ 20511 if (err == 0) 20512 md_mp_head = NULL; 20513 } 20514 /* md_hbuf gets freed automatically */ 20515 TCP_STAT(tcps, tcp_mdt_discarded); 20516 freeb(md_mp); 20517 } else { 20518 /* Either allocb or mmd_alloc failed */ 20519 TCP_STAT(tcps, tcp_mdt_allocfail); 20520 if (md_hbuf != NULL) 20521 freeb(md_hbuf); 20522 } 20523 20524 /* send down what we've got so far */ 20525 if (md_mp_head != NULL) { 20526 tcp_multisend_data(tcp, ire, ill, 20527 md_mp_head, obsegs, obbytes, 20528 &rconfirm); 20529 } 20530 legacy_send_no_md: 20531 if (ire != NULL) 20532 IRE_REFRELE(ire); 20533 /* 20534 * Too bad; let the legacy path handle this. 20535 * We specify INT_MAX for the threshold, since 20536 * we gave up with the Multidata processings 20537 * and let the old path have it all. 20538 */ 20539 TCP_STAT(tcps, tcp_mdt_legacy_all); 20540 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20541 tcp_tcp_hdr_len, num_sack_blk, usable, 20542 snxt, tail_unsent, xmit_tail, local_time, 20543 INT_MAX)); 20544 } 20545 20546 /* link to any existing ones, if applicable */ 20547 TCP_STAT(tcps, tcp_mdt_allocd); 20548 if (md_mp_head == NULL) { 20549 md_mp_head = md_mp; 20550 } else if (tcp_mdt_chain) { 20551 TCP_STAT(tcps, tcp_mdt_linked); 20552 linkb(md_mp_head, md_mp); 20553 } 20554 } 20555 20556 ASSERT(md_mp_head != NULL); 20557 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20558 ASSERT(md_mp != NULL && mmd != NULL); 20559 ASSERT(md_hbuf != NULL); 20560 20561 /* 20562 * Packetize the transmittable portion of the data block; 20563 * each data block is essentially added to the Multidata 20564 * as a payload buffer. We also deal with adding more 20565 * than one payload buffers, which happens when the remaining 20566 * packetized portion of the current payload buffer is less 20567 * than MSS, while the next data block in transmit queue 20568 * has enough data to make up for one. This "spillover" 20569 * case essentially creates a split-packet, where portions 20570 * of the packet's payload fragments may span across two 20571 * virtually discontiguous address blocks. 20572 */ 20573 seg_len = mss; 20574 do { 20575 len = seg_len; 20576 20577 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20578 ipha = NULL; 20579 ip6h = NULL; 20580 20581 ASSERT(len > 0); 20582 ASSERT(max_pld >= 0); 20583 ASSERT(!add_buffer || cur_pld_off == 0); 20584 20585 /* 20586 * First time around for this payload buffer; note 20587 * in the case of a spillover, the following has 20588 * been done prior to adding the split-packet 20589 * descriptor to Multidata, and we don't want to 20590 * repeat the process. 20591 */ 20592 if (add_buffer) { 20593 ASSERT(mmd != NULL); 20594 ASSERT(md_pbuf == NULL); 20595 ASSERT(md_pbuf_nxt == NULL); 20596 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20597 20598 /* 20599 * Have we reached the limit? We'd get to 20600 * this case when we're not chaining the 20601 * Multidata messages together, and since 20602 * we're done, terminate this loop. 20603 */ 20604 if (max_pld == 0) 20605 break; /* done */ 20606 20607 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20608 TCP_STAT(tcps, tcp_mdt_allocfail); 20609 goto legacy_send; /* out_of_mem */ 20610 } 20611 20612 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20613 zc_cap != NULL) { 20614 if (!ip_md_zcopy_attr(mmd, NULL, 20615 zc_cap->ill_zerocopy_flags)) { 20616 freeb(md_pbuf); 20617 TCP_STAT(tcps, 20618 tcp_mdt_allocfail); 20619 /* out_of_mem */ 20620 goto legacy_send; 20621 } 20622 zcopy = B_TRUE; 20623 } 20624 20625 md_pbuf->b_rptr += base_pld_off; 20626 20627 /* 20628 * Add a payload buffer to the Multidata; this 20629 * operation must not fail, or otherwise our 20630 * logic in this routine is broken. There 20631 * is no memory allocation done by the 20632 * routine, so any returned failure simply 20633 * tells us that we've done something wrong. 20634 * 20635 * A failure tells us that either we're adding 20636 * the same payload buffer more than once, or 20637 * we're trying to add more buffers than 20638 * allowed (max_pld calculation is wrong). 20639 * None of the above cases should happen, and 20640 * we panic because either there's horrible 20641 * heap corruption, and/or programming mistake. 20642 */ 20643 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20644 if (pbuf_idx < 0) { 20645 cmn_err(CE_PANIC, "tcp_multisend: " 20646 "payload buffer logic error " 20647 "detected for tcp %p mmd %p " 20648 "pbuf %p (%d)\n", 20649 (void *)tcp, (void *)mmd, 20650 (void *)md_pbuf, pbuf_idx); 20651 } 20652 20653 ASSERT(max_pld > 0); 20654 --max_pld; 20655 add_buffer = B_FALSE; 20656 } 20657 20658 ASSERT(md_mp_head != NULL); 20659 ASSERT(md_pbuf != NULL); 20660 ASSERT(md_pbuf_nxt == NULL); 20661 ASSERT(pbuf_idx != -1); 20662 ASSERT(pbuf_idx_nxt == -1); 20663 ASSERT(*usable > 0); 20664 20665 /* 20666 * We spillover to the next payload buffer only 20667 * if all of the following is true: 20668 * 20669 * 1. There is not enough data on the current 20670 * payload buffer to make up `len', 20671 * 2. We are allowed to send `len', 20672 * 3. The next payload buffer length is large 20673 * enough to accomodate `spill'. 20674 */ 20675 if ((spill = len - *tail_unsent) > 0 && 20676 *usable >= len && 20677 MBLKL((*xmit_tail)->b_cont) >= spill && 20678 max_pld > 0) { 20679 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20680 if (md_pbuf_nxt == NULL) { 20681 TCP_STAT(tcps, tcp_mdt_allocfail); 20682 goto legacy_send; /* out_of_mem */ 20683 } 20684 20685 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20686 zc_cap != NULL) { 20687 if (!ip_md_zcopy_attr(mmd, NULL, 20688 zc_cap->ill_zerocopy_flags)) { 20689 freeb(md_pbuf_nxt); 20690 TCP_STAT(tcps, 20691 tcp_mdt_allocfail); 20692 /* out_of_mem */ 20693 goto legacy_send; 20694 } 20695 zcopy = B_TRUE; 20696 } 20697 20698 /* 20699 * See comments above on the first call to 20700 * mmd_addpldbuf for explanation on the panic. 20701 */ 20702 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20703 if (pbuf_idx_nxt < 0) { 20704 panic("tcp_multisend: " 20705 "next payload buffer logic error " 20706 "detected for tcp %p mmd %p " 20707 "pbuf %p (%d)\n", 20708 (void *)tcp, (void *)mmd, 20709 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20710 } 20711 20712 ASSERT(max_pld > 0); 20713 --max_pld; 20714 } else if (spill > 0) { 20715 /* 20716 * If there's a spillover, but the following 20717 * xmit_tail couldn't give us enough octets 20718 * to reach "len", then stop the current 20719 * Multidata creation and let the legacy 20720 * tcp_send() path take over. We don't want 20721 * to send the tiny segment as part of this 20722 * Multidata for performance reasons; instead, 20723 * we let the legacy path deal with grouping 20724 * it with the subsequent small mblks. 20725 */ 20726 if (*usable >= len && 20727 MBLKL((*xmit_tail)->b_cont) < spill) { 20728 max_pld = 0; 20729 break; /* done */ 20730 } 20731 20732 /* 20733 * We can't spillover, and we are near 20734 * the end of the current payload buffer, 20735 * so send what's left. 20736 */ 20737 ASSERT(*tail_unsent > 0); 20738 len = *tail_unsent; 20739 } 20740 20741 /* tail_unsent is negated if there is a spillover */ 20742 *tail_unsent -= len; 20743 *usable -= len; 20744 ASSERT(*usable >= 0); 20745 20746 if (*usable < mss) 20747 seg_len = *usable; 20748 /* 20749 * Sender SWS avoidance; see comments in tcp_send(); 20750 * everything else is the same, except that we only 20751 * do this here if there is no more data to be sent 20752 * following the current xmit_tail. We don't check 20753 * for 1-byte urgent data because we shouldn't get 20754 * here if TCP_URG_VALID is set. 20755 */ 20756 if (*usable > 0 && *usable < mss && 20757 ((md_pbuf_nxt == NULL && 20758 (*xmit_tail)->b_cont == NULL) || 20759 (md_pbuf_nxt != NULL && 20760 (*xmit_tail)->b_cont->b_cont == NULL)) && 20761 seg_len < (tcp->tcp_max_swnd >> 1) && 20762 (tcp->tcp_unsent - 20763 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20764 !tcp->tcp_zero_win_probe) { 20765 if ((*snxt + len) == tcp->tcp_snxt && 20766 (*snxt + len) == tcp->tcp_suna) { 20767 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20768 } 20769 done = B_TRUE; 20770 } 20771 20772 /* 20773 * Prime pump for IP's checksumming on our behalf; 20774 * include the adjustment for a source route if any. 20775 * Do this only for software/partial hardware checksum 20776 * offload, as this field gets zeroed out later for 20777 * the full hardware checksum offload case. 20778 */ 20779 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20780 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20781 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20782 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20783 } 20784 20785 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20786 *snxt += len; 20787 20788 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20789 /* 20790 * We set the PUSH bit only if TCP has no more buffered 20791 * data to be transmitted (or if sender SWS avoidance 20792 * takes place), as opposed to setting it for every 20793 * last packet in the burst. 20794 */ 20795 if (done || 20796 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20797 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20798 20799 /* 20800 * Set FIN bit if this is our last segment; snxt 20801 * already includes its length, and it will not 20802 * be adjusted after this point. 20803 */ 20804 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20805 *snxt == tcp->tcp_fss) { 20806 if (!tcp->tcp_fin_acked) { 20807 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20808 BUMP_MIB(&tcps->tcps_mib, 20809 tcpOutControl); 20810 } 20811 if (!tcp->tcp_fin_sent) { 20812 tcp->tcp_fin_sent = B_TRUE; 20813 /* 20814 * tcp state must be ESTABLISHED 20815 * in order for us to get here in 20816 * the first place. 20817 */ 20818 tcp->tcp_state = TCPS_FIN_WAIT_1; 20819 20820 /* 20821 * Upon returning from this routine, 20822 * tcp_wput_data() will set tcp_snxt 20823 * to be equal to snxt + tcp_fin_sent. 20824 * This is essentially the same as 20825 * setting it to tcp_fss + 1. 20826 */ 20827 } 20828 } 20829 20830 tcp->tcp_last_sent_len = (ushort_t)len; 20831 20832 len += tcp_hdr_len; 20833 if (tcp->tcp_ipversion == IPV4_VERSION) 20834 tcp->tcp_ipha->ipha_length = htons(len); 20835 else 20836 tcp->tcp_ip6h->ip6_plen = htons(len - 20837 ((char *)&tcp->tcp_ip6h[1] - 20838 tcp->tcp_iphc)); 20839 20840 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20841 20842 /* setup header fragment */ 20843 PDESC_HDR_ADD(pkt_info, 20844 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20845 tcp->tcp_mdt_hdr_head, /* head room */ 20846 tcp_hdr_len, /* len */ 20847 tcp->tcp_mdt_hdr_tail); /* tail room */ 20848 20849 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20850 hdr_frag_sz); 20851 ASSERT(MBLKIN(md_hbuf, 20852 (pkt_info->hdr_base - md_hbuf->b_rptr), 20853 PDESC_HDRSIZE(pkt_info))); 20854 20855 /* setup first payload fragment */ 20856 PDESC_PLD_INIT(pkt_info); 20857 PDESC_PLD_SPAN_ADD(pkt_info, 20858 pbuf_idx, /* index */ 20859 md_pbuf->b_rptr + cur_pld_off, /* start */ 20860 tcp->tcp_last_sent_len); /* len */ 20861 20862 /* create a split-packet in case of a spillover */ 20863 if (md_pbuf_nxt != NULL) { 20864 ASSERT(spill > 0); 20865 ASSERT(pbuf_idx_nxt > pbuf_idx); 20866 ASSERT(!add_buffer); 20867 20868 md_pbuf = md_pbuf_nxt; 20869 md_pbuf_nxt = NULL; 20870 pbuf_idx = pbuf_idx_nxt; 20871 pbuf_idx_nxt = -1; 20872 cur_pld_off = spill; 20873 20874 /* trim out first payload fragment */ 20875 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20876 20877 /* setup second payload fragment */ 20878 PDESC_PLD_SPAN_ADD(pkt_info, 20879 pbuf_idx, /* index */ 20880 md_pbuf->b_rptr, /* start */ 20881 spill); /* len */ 20882 20883 if ((*xmit_tail)->b_next == NULL) { 20884 /* 20885 * Store the lbolt used for RTT 20886 * estimation. We can only record one 20887 * timestamp per mblk so we do it when 20888 * we reach the end of the payload 20889 * buffer. Also we only take a new 20890 * timestamp sample when the previous 20891 * timed data from the same mblk has 20892 * been ack'ed. 20893 */ 20894 (*xmit_tail)->b_prev = local_time; 20895 (*xmit_tail)->b_next = 20896 (mblk_t *)(uintptr_t)first_snxt; 20897 } 20898 20899 first_snxt = *snxt - spill; 20900 20901 /* 20902 * Advance xmit_tail; usable could be 0 by 20903 * the time we got here, but we made sure 20904 * above that we would only spillover to 20905 * the next data block if usable includes 20906 * the spilled-over amount prior to the 20907 * subtraction. Therefore, we are sure 20908 * that xmit_tail->b_cont can't be NULL. 20909 */ 20910 ASSERT((*xmit_tail)->b_cont != NULL); 20911 *xmit_tail = (*xmit_tail)->b_cont; 20912 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20913 (uintptr_t)INT_MAX); 20914 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20915 } else { 20916 cur_pld_off += tcp->tcp_last_sent_len; 20917 } 20918 20919 /* 20920 * Fill in the header using the template header, and 20921 * add options such as time-stamp, ECN and/or SACK, 20922 * as needed. 20923 */ 20924 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20925 (clock_t)local_time, num_sack_blk); 20926 20927 /* take care of some IP header businesses */ 20928 if (af == AF_INET) { 20929 ipha = (ipha_t *)pkt_info->hdr_rptr; 20930 20931 ASSERT(OK_32PTR((uchar_t *)ipha)); 20932 ASSERT(PDESC_HDRL(pkt_info) >= 20933 IP_SIMPLE_HDR_LENGTH); 20934 ASSERT(ipha->ipha_version_and_hdr_length == 20935 IP_SIMPLE_HDR_VERSION); 20936 20937 /* 20938 * Assign ident value for current packet; see 20939 * related comments in ip_wput_ire() about the 20940 * contract private interface with clustering 20941 * group. 20942 */ 20943 clusterwide = B_FALSE; 20944 if (cl_inet_ipident != NULL) { 20945 ASSERT(cl_inet_isclusterwide != NULL); 20946 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 20947 AF_INET, 20948 (uint8_t *)(uintptr_t)src)) { 20949 ipha->ipha_ident = 20950 (*cl_inet_ipident) 20951 (IPPROTO_IP, AF_INET, 20952 (uint8_t *)(uintptr_t)src, 20953 (uint8_t *)(uintptr_t)dst); 20954 clusterwide = B_TRUE; 20955 } 20956 } 20957 20958 if (!clusterwide) { 20959 ipha->ipha_ident = (uint16_t) 20960 atomic_add_32_nv( 20961 &ire->ire_ident, 1); 20962 } 20963 #ifndef _BIG_ENDIAN 20964 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20965 (ipha->ipha_ident >> 8); 20966 #endif 20967 } else { 20968 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20969 20970 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20971 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20972 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20973 ASSERT(PDESC_HDRL(pkt_info) >= 20974 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20975 TCP_CHECKSUM_SIZE)); 20976 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20977 20978 if (tcp->tcp_ip_forward_progress) { 20979 rconfirm = B_TRUE; 20980 tcp->tcp_ip_forward_progress = B_FALSE; 20981 } 20982 } 20983 20984 /* at least one payload span, and at most two */ 20985 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20986 20987 /* add the packet descriptor to Multidata */ 20988 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20989 KM_NOSLEEP)) == NULL) { 20990 /* 20991 * Any failure other than ENOMEM indicates 20992 * that we have passed in invalid pkt_info 20993 * or parameters to mmd_addpdesc, which must 20994 * not happen. 20995 * 20996 * EINVAL is a result of failure on boundary 20997 * checks against the pkt_info contents. It 20998 * should not happen, and we panic because 20999 * either there's horrible heap corruption, 21000 * and/or programming mistake. 21001 */ 21002 if (err != ENOMEM) { 21003 cmn_err(CE_PANIC, "tcp_multisend: " 21004 "pdesc logic error detected for " 21005 "tcp %p mmd %p pinfo %p (%d)\n", 21006 (void *)tcp, (void *)mmd, 21007 (void *)pkt_info, err); 21008 } 21009 TCP_STAT(tcps, tcp_mdt_addpdescfail); 21010 goto legacy_send; /* out_of_mem */ 21011 } 21012 ASSERT(pkt != NULL); 21013 21014 /* calculate IP header and TCP checksums */ 21015 if (af == AF_INET) { 21016 /* calculate pseudo-header checksum */ 21017 cksum = (dst >> 16) + (dst & 0xFFFF) + 21018 (src >> 16) + (src & 0xFFFF); 21019 21020 /* offset for TCP header checksum */ 21021 up = IPH_TCPH_CHECKSUMP(ipha, 21022 IP_SIMPLE_HDR_LENGTH); 21023 } else { 21024 up = (uint16_t *)&ip6h->ip6_src; 21025 21026 /* calculate pseudo-header checksum */ 21027 cksum = up[0] + up[1] + up[2] + up[3] + 21028 up[4] + up[5] + up[6] + up[7] + 21029 up[8] + up[9] + up[10] + up[11] + 21030 up[12] + up[13] + up[14] + up[15]; 21031 21032 /* Fold the initial sum */ 21033 cksum = (cksum & 0xffff) + (cksum >> 16); 21034 21035 up = (uint16_t *)(((uchar_t *)ip6h) + 21036 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21037 } 21038 21039 if (hwcksum_flags & HCK_FULLCKSUM) { 21040 /* clear checksum field for hardware */ 21041 *up = 0; 21042 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21043 uint32_t sum; 21044 21045 /* pseudo-header checksumming */ 21046 sum = *up + cksum + IP_TCP_CSUM_COMP; 21047 sum = (sum & 0xFFFF) + (sum >> 16); 21048 *up = (sum & 0xFFFF) + (sum >> 16); 21049 } else { 21050 /* software checksumming */ 21051 TCP_STAT(tcps, tcp_out_sw_cksum); 21052 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21053 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21054 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21055 cksum + IP_TCP_CSUM_COMP); 21056 if (*up == 0) 21057 *up = 0xFFFF; 21058 } 21059 21060 /* IPv4 header checksum */ 21061 if (af == AF_INET) { 21062 ipha->ipha_fragment_offset_and_flags |= 21063 (uint32_t)htons(ire->ire_frag_flag); 21064 21065 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21066 ipha->ipha_hdr_checksum = 0; 21067 } else { 21068 IP_HDR_CKSUM(ipha, cksum, 21069 ((uint32_t *)ipha)[0], 21070 ((uint16_t *)ipha)[4]); 21071 } 21072 } 21073 21074 if (af == AF_INET && 21075 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21076 af == AF_INET6 && 21077 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21078 mblk_t *mp, *mp1; 21079 uchar_t *hdr_rptr, *hdr_wptr; 21080 uchar_t *pld_rptr, *pld_wptr; 21081 21082 /* 21083 * We reconstruct a pseudo packet for the hooks 21084 * framework using mmd_transform_link(). 21085 * If it is a split packet we pullup the 21086 * payload. FW_HOOKS expects a pkt comprising 21087 * of two mblks: a header and the payload. 21088 */ 21089 if ((mp = mmd_transform_link(pkt)) == NULL) { 21090 TCP_STAT(tcps, tcp_mdt_allocfail); 21091 goto legacy_send; 21092 } 21093 21094 if (pkt_info->pld_cnt > 1) { 21095 /* split payload, more than one pld */ 21096 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21097 NULL) { 21098 freemsg(mp); 21099 TCP_STAT(tcps, 21100 tcp_mdt_allocfail); 21101 goto legacy_send; 21102 } 21103 freemsg(mp->b_cont); 21104 mp->b_cont = mp1; 21105 } else { 21106 mp1 = mp->b_cont; 21107 } 21108 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21109 21110 /* 21111 * Remember the message offsets. This is so we 21112 * can detect changes when we return from the 21113 * FW_HOOKS callbacks. 21114 */ 21115 hdr_rptr = mp->b_rptr; 21116 hdr_wptr = mp->b_wptr; 21117 pld_rptr = mp->b_cont->b_rptr; 21118 pld_wptr = mp->b_cont->b_wptr; 21119 21120 if (af == AF_INET) { 21121 DTRACE_PROBE4( 21122 ip4__physical__out__start, 21123 ill_t *, NULL, 21124 ill_t *, ill, 21125 ipha_t *, ipha, 21126 mblk_t *, mp); 21127 FW_HOOKS( 21128 ipst->ips_ip4_physical_out_event, 21129 ipst->ips_ipv4firewall_physical_out, 21130 NULL, ill, ipha, mp, mp, 0, ipst); 21131 DTRACE_PROBE1( 21132 ip4__physical__out__end, 21133 mblk_t *, mp); 21134 } else { 21135 DTRACE_PROBE4( 21136 ip6__physical__out_start, 21137 ill_t *, NULL, 21138 ill_t *, ill, 21139 ip6_t *, ip6h, 21140 mblk_t *, mp); 21141 FW_HOOKS6( 21142 ipst->ips_ip6_physical_out_event, 21143 ipst->ips_ipv6firewall_physical_out, 21144 NULL, ill, ip6h, mp, mp, 0, ipst); 21145 DTRACE_PROBE1( 21146 ip6__physical__out__end, 21147 mblk_t *, mp); 21148 } 21149 21150 if (mp == NULL || 21151 (mp1 = mp->b_cont) == NULL || 21152 mp->b_rptr != hdr_rptr || 21153 mp->b_wptr != hdr_wptr || 21154 mp1->b_rptr != pld_rptr || 21155 mp1->b_wptr != pld_wptr || 21156 mp1->b_cont != NULL) { 21157 /* 21158 * We abandon multidata processing and 21159 * return to the normal path, either 21160 * when a packet is blocked, or when 21161 * the boundaries of header buffer or 21162 * payload buffer have been changed by 21163 * FW_HOOKS[6]. 21164 */ 21165 if (mp != NULL) 21166 freemsg(mp); 21167 goto legacy_send; 21168 } 21169 /* Finished with the pseudo packet */ 21170 freemsg(mp); 21171 } 21172 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21173 ill, ipha, ip6h); 21174 /* advance header offset */ 21175 cur_hdr_off += hdr_frag_sz; 21176 21177 obbytes += tcp->tcp_last_sent_len; 21178 ++obsegs; 21179 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21180 *tail_unsent > 0); 21181 21182 if ((*xmit_tail)->b_next == NULL) { 21183 /* 21184 * Store the lbolt used for RTT estimation. We can only 21185 * record one timestamp per mblk so we do it when we 21186 * reach the end of the payload buffer. Also we only 21187 * take a new timestamp sample when the previous timed 21188 * data from the same mblk has been ack'ed. 21189 */ 21190 (*xmit_tail)->b_prev = local_time; 21191 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21192 } 21193 21194 ASSERT(*tail_unsent >= 0); 21195 if (*tail_unsent > 0) { 21196 /* 21197 * We got here because we broke out of the above 21198 * loop due to of one of the following cases: 21199 * 21200 * 1. len < adjusted MSS (i.e. small), 21201 * 2. Sender SWS avoidance, 21202 * 3. max_pld is zero. 21203 * 21204 * We are done for this Multidata, so trim our 21205 * last payload buffer (if any) accordingly. 21206 */ 21207 if (md_pbuf != NULL) 21208 md_pbuf->b_wptr -= *tail_unsent; 21209 } else if (*usable > 0) { 21210 *xmit_tail = (*xmit_tail)->b_cont; 21211 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21212 (uintptr_t)INT_MAX); 21213 *tail_unsent = (int)MBLKL(*xmit_tail); 21214 add_buffer = B_TRUE; 21215 } 21216 } while (!done && *usable > 0 && num_burst_seg > 0 && 21217 (tcp_mdt_chain || max_pld > 0)); 21218 21219 if (md_mp_head != NULL) { 21220 /* send everything down */ 21221 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21222 &rconfirm); 21223 } 21224 21225 #undef PREP_NEW_MULTIDATA 21226 #undef PREP_NEW_PBUF 21227 #undef IPVER 21228 21229 IRE_REFRELE(ire); 21230 return (0); 21231 } 21232 21233 /* 21234 * A wrapper function for sending one or more Multidata messages down to 21235 * the module below ip; this routine does not release the reference of the 21236 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21237 */ 21238 static void 21239 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21240 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21241 { 21242 uint64_t delta; 21243 nce_t *nce; 21244 tcp_stack_t *tcps = tcp->tcp_tcps; 21245 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21246 21247 ASSERT(ire != NULL && ill != NULL); 21248 ASSERT(ire->ire_stq != NULL); 21249 ASSERT(md_mp_head != NULL); 21250 ASSERT(rconfirm != NULL); 21251 21252 /* adjust MIBs and IRE timestamp */ 21253 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21254 tcp->tcp_obsegs += obsegs; 21255 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21256 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21257 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21258 21259 if (tcp->tcp_ipversion == IPV4_VERSION) { 21260 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21261 } else { 21262 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21263 } 21264 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21265 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21266 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21267 21268 ire->ire_ob_pkt_count += obsegs; 21269 if (ire->ire_ipif != NULL) 21270 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21271 ire->ire_last_used_time = lbolt; 21272 21273 if (ipst->ips_ipobs_enabled) { 21274 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21275 pdesc_t *dl_pkt; 21276 pdescinfo_t pinfo; 21277 mblk_t *nmp; 21278 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21279 21280 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21281 (dl_pkt != NULL); 21282 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21283 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21284 continue; 21285 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21286 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21287 freemsg(nmp); 21288 } 21289 } 21290 21291 /* send it down */ 21292 if (ILL_DLS_CAPABLE(ill)) { 21293 ill_dls_capab_t *ill_dls = ill->ill_dls_capab; 21294 ill_dls->ill_tx(ill_dls->ill_tx_handle, md_mp_head); 21295 } else { 21296 putnext(ire->ire_stq, md_mp_head); 21297 } 21298 21299 /* we're done for TCP/IPv4 */ 21300 if (tcp->tcp_ipversion == IPV4_VERSION) 21301 return; 21302 21303 nce = ire->ire_nce; 21304 21305 ASSERT(nce != NULL); 21306 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21307 ASSERT(nce->nce_state != ND_INCOMPLETE); 21308 21309 /* reachability confirmation? */ 21310 if (*rconfirm) { 21311 nce->nce_last = TICK_TO_MSEC(lbolt64); 21312 if (nce->nce_state != ND_REACHABLE) { 21313 mutex_enter(&nce->nce_lock); 21314 nce->nce_state = ND_REACHABLE; 21315 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21316 mutex_exit(&nce->nce_lock); 21317 (void) untimeout(nce->nce_timeout_id); 21318 if (ip_debug > 2) { 21319 /* ip1dbg */ 21320 pr_addr_dbg("tcp_multisend_data: state " 21321 "for %s changed to REACHABLE\n", 21322 AF_INET6, &ire->ire_addr_v6); 21323 } 21324 } 21325 /* reset transport reachability confirmation */ 21326 *rconfirm = B_FALSE; 21327 } 21328 21329 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21330 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21331 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21332 21333 if (delta > (uint64_t)ill->ill_reachable_time) { 21334 mutex_enter(&nce->nce_lock); 21335 switch (nce->nce_state) { 21336 case ND_REACHABLE: 21337 case ND_STALE: 21338 /* 21339 * ND_REACHABLE is identical to ND_STALE in this 21340 * specific case. If reachable time has expired for 21341 * this neighbor (delta is greater than reachable 21342 * time), conceptually, the neighbor cache is no 21343 * longer in REACHABLE state, but already in STALE 21344 * state. So the correct transition here is to 21345 * ND_DELAY. 21346 */ 21347 nce->nce_state = ND_DELAY; 21348 mutex_exit(&nce->nce_lock); 21349 NDP_RESTART_TIMER(nce, 21350 ipst->ips_delay_first_probe_time); 21351 if (ip_debug > 3) { 21352 /* ip2dbg */ 21353 pr_addr_dbg("tcp_multisend_data: state " 21354 "for %s changed to DELAY\n", 21355 AF_INET6, &ire->ire_addr_v6); 21356 } 21357 break; 21358 case ND_DELAY: 21359 case ND_PROBE: 21360 mutex_exit(&nce->nce_lock); 21361 /* Timers have already started */ 21362 break; 21363 case ND_UNREACHABLE: 21364 /* 21365 * ndp timer has detected that this nce is 21366 * unreachable and initiated deleting this nce 21367 * and all its associated IREs. This is a race 21368 * where we found the ire before it was deleted 21369 * and have just sent out a packet using this 21370 * unreachable nce. 21371 */ 21372 mutex_exit(&nce->nce_lock); 21373 break; 21374 default: 21375 ASSERT(0); 21376 } 21377 } 21378 } 21379 21380 /* 21381 * Derived from tcp_send_data(). 21382 */ 21383 static void 21384 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21385 int num_lso_seg) 21386 { 21387 ipha_t *ipha; 21388 mblk_t *ire_fp_mp; 21389 uint_t ire_fp_mp_len; 21390 uint32_t hcksum_txflags = 0; 21391 ipaddr_t src; 21392 ipaddr_t dst; 21393 uint32_t cksum; 21394 uint16_t *up; 21395 tcp_stack_t *tcps = tcp->tcp_tcps; 21396 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21397 21398 ASSERT(DB_TYPE(mp) == M_DATA); 21399 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21400 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21401 ASSERT(tcp->tcp_connp != NULL); 21402 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21403 21404 ipha = (ipha_t *)mp->b_rptr; 21405 src = ipha->ipha_src; 21406 dst = ipha->ipha_dst; 21407 21408 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21409 21410 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21411 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21412 num_lso_seg); 21413 #ifndef _BIG_ENDIAN 21414 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21415 #endif 21416 if (tcp->tcp_snd_zcopy_aware) { 21417 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21418 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21419 mp = tcp_zcopy_disable(tcp, mp); 21420 } 21421 21422 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21423 ASSERT(ill->ill_hcksum_capab != NULL); 21424 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21425 } 21426 21427 /* 21428 * Since the TCP checksum should be recalculated by h/w, we can just 21429 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21430 * pseudo-header checksum for HCK_PARTIALCKSUM. 21431 * The partial pseudo-header excludes TCP length, that was calculated 21432 * in tcp_send(), so to zero *up before further processing. 21433 */ 21434 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21435 21436 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21437 *up = 0; 21438 21439 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21440 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21441 21442 /* 21443 * Append LSO flag to DB_LSOFLAGS(mp) and set the mss to DB_LSOMSS(mp). 21444 */ 21445 DB_LSOFLAGS(mp) |= HW_LSO; 21446 DB_LSOMSS(mp) = mss; 21447 21448 ipha->ipha_fragment_offset_and_flags |= 21449 (uint32_t)htons(ire->ire_frag_flag); 21450 21451 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21452 ire_fp_mp_len = MBLKL(ire_fp_mp); 21453 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21454 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21455 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21456 21457 UPDATE_OB_PKT_COUNT(ire); 21458 ire->ire_last_used_time = lbolt; 21459 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21460 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21461 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21462 ntohs(ipha->ipha_length)); 21463 21464 if (ILL_DLS_CAPABLE(ill)) { 21465 /* 21466 * Send the packet directly to DLD, where it may be queued 21467 * depending on the availability of transmit resources at 21468 * the media layer. 21469 */ 21470 IP_DLS_ILL_TX(ill, ipha, mp, ipst, ire_fp_mp_len); 21471 } else { 21472 ill_t *out_ill = (ill_t *)ire->ire_stq->q_ptr; 21473 DTRACE_PROBE4(ip4__physical__out__start, 21474 ill_t *, NULL, ill_t *, out_ill, 21475 ipha_t *, ipha, mblk_t *, mp); 21476 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21477 ipst->ips_ipv4firewall_physical_out, 21478 NULL, out_ill, ipha, mp, mp, 0, ipst); 21479 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21480 21481 if (mp != NULL) { 21482 if (ipst->ips_ipobs_enabled) { 21483 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21484 21485 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21486 ALL_ZONES, ill, tcp->tcp_ipversion, 21487 ire_fp_mp_len, ipst); 21488 } 21489 DTRACE_IP_FASTPATH(mp, ipha, out_ill, ipha, NULL); 21490 putnext(ire->ire_stq, mp); 21491 } 21492 } 21493 } 21494 21495 /* 21496 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21497 * scheme, and returns one of the following: 21498 * 21499 * -1 = failed allocation. 21500 * 0 = success; burst count reached, or usable send window is too small, 21501 * and that we'd rather wait until later before sending again. 21502 * 1 = success; we are called from tcp_multisend(), and both usable send 21503 * window and tail_unsent are greater than the MDT threshold, and thus 21504 * Multidata Transmit should be used instead. 21505 */ 21506 static int 21507 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21508 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21509 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21510 const int mdt_thres) 21511 { 21512 int num_burst_seg = tcp->tcp_snd_burst; 21513 ire_t *ire = NULL; 21514 ill_t *ill = NULL; 21515 mblk_t *ire_fp_mp = NULL; 21516 uint_t ire_fp_mp_len = 0; 21517 int num_lso_seg = 1; 21518 uint_t lso_usable; 21519 boolean_t do_lso_send = B_FALSE; 21520 tcp_stack_t *tcps = tcp->tcp_tcps; 21521 21522 /* 21523 * Check LSO capability before any further work. And the similar check 21524 * need to be done in for(;;) loop. 21525 * LSO will be deployed when therer is more than one mss of available 21526 * data and a burst transmission is allowed. 21527 */ 21528 if (tcp->tcp_lso && 21529 (tcp->tcp_valid_bits == 0 || 21530 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21531 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21532 /* 21533 * Try to find usable IRE/ILL and do basic check to the ILL. 21534 */ 21535 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21536 /* 21537 * Enable LSO with this transmission. 21538 * Since IRE has been hold in 21539 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21540 * should be called before return. 21541 */ 21542 do_lso_send = B_TRUE; 21543 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21544 ire_fp_mp_len = MBLKL(ire_fp_mp); 21545 /* Round up to multiple of 4 */ 21546 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21547 } else { 21548 do_lso_send = B_FALSE; 21549 ill = NULL; 21550 } 21551 } 21552 21553 for (;;) { 21554 struct datab *db; 21555 tcph_t *tcph; 21556 uint32_t sum; 21557 mblk_t *mp, *mp1; 21558 uchar_t *rptr; 21559 int len; 21560 21561 /* 21562 * If we're called by tcp_multisend(), and the amount of 21563 * sendable data as well as the size of current xmit_tail 21564 * is beyond the MDT threshold, return to the caller and 21565 * let the large data transmit be done using MDT. 21566 */ 21567 if (*usable > 0 && *usable > mdt_thres && 21568 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21569 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21570 ASSERT(tcp->tcp_mdt); 21571 return (1); /* success; do large send */ 21572 } 21573 21574 if (num_burst_seg == 0) 21575 break; /* success; burst count reached */ 21576 21577 /* 21578 * Calculate the maximum payload length we can send in *one* 21579 * time. 21580 */ 21581 if (do_lso_send) { 21582 /* 21583 * Check whether need to do LSO any more. 21584 */ 21585 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21586 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21587 lso_usable = MIN(lso_usable, 21588 num_burst_seg * mss); 21589 21590 num_lso_seg = lso_usable / mss; 21591 if (lso_usable % mss) { 21592 num_lso_seg++; 21593 tcp->tcp_last_sent_len = (ushort_t) 21594 (lso_usable % mss); 21595 } else { 21596 tcp->tcp_last_sent_len = (ushort_t)mss; 21597 } 21598 } else { 21599 do_lso_send = B_FALSE; 21600 num_lso_seg = 1; 21601 lso_usable = mss; 21602 } 21603 } 21604 21605 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21606 21607 /* 21608 * Adjust num_burst_seg here. 21609 */ 21610 num_burst_seg -= num_lso_seg; 21611 21612 len = mss; 21613 if (len > *usable) { 21614 ASSERT(do_lso_send == B_FALSE); 21615 21616 len = *usable; 21617 if (len <= 0) { 21618 /* Terminate the loop */ 21619 break; /* success; too small */ 21620 } 21621 /* 21622 * Sender silly-window avoidance. 21623 * Ignore this if we are going to send a 21624 * zero window probe out. 21625 * 21626 * TODO: force data into microscopic window? 21627 * ==> (!pushed || (unsent > usable)) 21628 */ 21629 if (len < (tcp->tcp_max_swnd >> 1) && 21630 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21631 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21632 len == 1) && (! tcp->tcp_zero_win_probe)) { 21633 /* 21634 * If the retransmit timer is not running 21635 * we start it so that we will retransmit 21636 * in the case when the the receiver has 21637 * decremented the window. 21638 */ 21639 if (*snxt == tcp->tcp_snxt && 21640 *snxt == tcp->tcp_suna) { 21641 /* 21642 * We are not supposed to send 21643 * anything. So let's wait a little 21644 * bit longer before breaking SWS 21645 * avoidance. 21646 * 21647 * What should the value be? 21648 * Suggestion: MAX(init rexmit time, 21649 * tcp->tcp_rto) 21650 */ 21651 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21652 } 21653 break; /* success; too small */ 21654 } 21655 } 21656 21657 tcph = tcp->tcp_tcph; 21658 21659 /* 21660 * The reason to adjust len here is that we need to set flags 21661 * and calculate checksum. 21662 */ 21663 if (do_lso_send) 21664 len = lso_usable; 21665 21666 *usable -= len; /* Approximate - can be adjusted later */ 21667 if (*usable > 0) 21668 tcph->th_flags[0] = TH_ACK; 21669 else 21670 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21671 21672 /* 21673 * Prime pump for IP's checksumming on our behalf 21674 * Include the adjustment for a source route if any. 21675 */ 21676 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21677 sum = (sum >> 16) + (sum & 0xFFFF); 21678 U16_TO_ABE16(sum, tcph->th_sum); 21679 21680 U32_TO_ABE32(*snxt, tcph->th_seq); 21681 21682 /* 21683 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21684 * set. For the case when TCP_FSS_VALID is the only valid 21685 * bit (normal active close), branch off only when we think 21686 * that the FIN flag needs to be set. Note for this case, 21687 * that (snxt + len) may not reflect the actual seg_len, 21688 * as len may be further reduced in tcp_xmit_mp(). If len 21689 * gets modified, we will end up here again. 21690 */ 21691 if (tcp->tcp_valid_bits != 0 && 21692 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21693 ((*snxt + len) == tcp->tcp_fss))) { 21694 uchar_t *prev_rptr; 21695 uint32_t prev_snxt = tcp->tcp_snxt; 21696 21697 if (*tail_unsent == 0) { 21698 ASSERT((*xmit_tail)->b_cont != NULL); 21699 *xmit_tail = (*xmit_tail)->b_cont; 21700 prev_rptr = (*xmit_tail)->b_rptr; 21701 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21702 (*xmit_tail)->b_rptr); 21703 } else { 21704 prev_rptr = (*xmit_tail)->b_rptr; 21705 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21706 *tail_unsent; 21707 } 21708 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21709 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21710 /* Restore tcp_snxt so we get amount sent right. */ 21711 tcp->tcp_snxt = prev_snxt; 21712 if (prev_rptr == (*xmit_tail)->b_rptr) { 21713 /* 21714 * If the previous timestamp is still in use, 21715 * don't stomp on it. 21716 */ 21717 if ((*xmit_tail)->b_next == NULL) { 21718 (*xmit_tail)->b_prev = local_time; 21719 (*xmit_tail)->b_next = 21720 (mblk_t *)(uintptr_t)(*snxt); 21721 } 21722 } else 21723 (*xmit_tail)->b_rptr = prev_rptr; 21724 21725 if (mp == NULL) { 21726 if (ire != NULL) 21727 IRE_REFRELE(ire); 21728 return (-1); 21729 } 21730 mp1 = mp->b_cont; 21731 21732 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21733 tcp->tcp_last_sent_len = (ushort_t)len; 21734 while (mp1->b_cont) { 21735 *xmit_tail = (*xmit_tail)->b_cont; 21736 (*xmit_tail)->b_prev = local_time; 21737 (*xmit_tail)->b_next = 21738 (mblk_t *)(uintptr_t)(*snxt); 21739 mp1 = mp1->b_cont; 21740 } 21741 *snxt += len; 21742 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21743 BUMP_LOCAL(tcp->tcp_obsegs); 21744 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21745 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21746 tcp_send_data(tcp, q, mp); 21747 continue; 21748 } 21749 21750 *snxt += len; /* Adjust later if we don't send all of len */ 21751 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21752 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21753 21754 if (*tail_unsent) { 21755 /* Are the bytes above us in flight? */ 21756 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21757 if (rptr != (*xmit_tail)->b_rptr) { 21758 *tail_unsent -= len; 21759 if (len <= mss) /* LSO is unusable */ 21760 tcp->tcp_last_sent_len = (ushort_t)len; 21761 len += tcp_hdr_len; 21762 if (tcp->tcp_ipversion == IPV4_VERSION) 21763 tcp->tcp_ipha->ipha_length = htons(len); 21764 else 21765 tcp->tcp_ip6h->ip6_plen = 21766 htons(len - 21767 ((char *)&tcp->tcp_ip6h[1] - 21768 tcp->tcp_iphc)); 21769 mp = dupb(*xmit_tail); 21770 if (mp == NULL) { 21771 if (ire != NULL) 21772 IRE_REFRELE(ire); 21773 return (-1); /* out_of_mem */ 21774 } 21775 mp->b_rptr = rptr; 21776 /* 21777 * If the old timestamp is no longer in use, 21778 * sample a new timestamp now. 21779 */ 21780 if ((*xmit_tail)->b_next == NULL) { 21781 (*xmit_tail)->b_prev = local_time; 21782 (*xmit_tail)->b_next = 21783 (mblk_t *)(uintptr_t)(*snxt-len); 21784 } 21785 goto must_alloc; 21786 } 21787 } else { 21788 *xmit_tail = (*xmit_tail)->b_cont; 21789 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21790 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21791 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21792 (*xmit_tail)->b_rptr); 21793 } 21794 21795 (*xmit_tail)->b_prev = local_time; 21796 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21797 21798 *tail_unsent -= len; 21799 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21800 tcp->tcp_last_sent_len = (ushort_t)len; 21801 21802 len += tcp_hdr_len; 21803 if (tcp->tcp_ipversion == IPV4_VERSION) 21804 tcp->tcp_ipha->ipha_length = htons(len); 21805 else 21806 tcp->tcp_ip6h->ip6_plen = htons(len - 21807 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21808 21809 mp = dupb(*xmit_tail); 21810 if (mp == NULL) { 21811 if (ire != NULL) 21812 IRE_REFRELE(ire); 21813 return (-1); /* out_of_mem */ 21814 } 21815 21816 len = tcp_hdr_len; 21817 /* 21818 * There are four reasons to allocate a new hdr mblk: 21819 * 1) The bytes above us are in use by another packet 21820 * 2) We don't have good alignment 21821 * 3) The mblk is being shared 21822 * 4) We don't have enough room for a header 21823 */ 21824 rptr = mp->b_rptr - len; 21825 if (!OK_32PTR(rptr) || 21826 ((db = mp->b_datap), db->db_ref != 2) || 21827 rptr < db->db_base + ire_fp_mp_len) { 21828 /* NOTE: we assume allocb returns an OK_32PTR */ 21829 21830 must_alloc:; 21831 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21832 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21833 if (mp1 == NULL) { 21834 freemsg(mp); 21835 if (ire != NULL) 21836 IRE_REFRELE(ire); 21837 return (-1); /* out_of_mem */ 21838 } 21839 mp1->b_cont = mp; 21840 mp = mp1; 21841 /* Leave room for Link Level header */ 21842 len = tcp_hdr_len; 21843 rptr = 21844 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21845 mp->b_wptr = &rptr[len]; 21846 } 21847 21848 /* 21849 * Fill in the header using the template header, and add 21850 * options such as time-stamp, ECN and/or SACK, as needed. 21851 */ 21852 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21853 21854 mp->b_rptr = rptr; 21855 21856 if (*tail_unsent) { 21857 int spill = *tail_unsent; 21858 21859 mp1 = mp->b_cont; 21860 if (mp1 == NULL) 21861 mp1 = mp; 21862 21863 /* 21864 * If we're a little short, tack on more mblks until 21865 * there is no more spillover. 21866 */ 21867 while (spill < 0) { 21868 mblk_t *nmp; 21869 int nmpsz; 21870 21871 nmp = (*xmit_tail)->b_cont; 21872 nmpsz = MBLKL(nmp); 21873 21874 /* 21875 * Excess data in mblk; can we split it? 21876 * If MDT is enabled for the connection, 21877 * keep on splitting as this is a transient 21878 * send path. 21879 */ 21880 if (!do_lso_send && !tcp->tcp_mdt && 21881 (spill + nmpsz > 0)) { 21882 /* 21883 * Don't split if stream head was 21884 * told to break up larger writes 21885 * into smaller ones. 21886 */ 21887 if (tcp->tcp_maxpsz > 0) 21888 break; 21889 21890 /* 21891 * Next mblk is less than SMSS/2 21892 * rounded up to nearest 64-byte; 21893 * let it get sent as part of the 21894 * next segment. 21895 */ 21896 if (tcp->tcp_localnet && 21897 !tcp->tcp_cork && 21898 (nmpsz < roundup((mss >> 1), 64))) 21899 break; 21900 } 21901 21902 *xmit_tail = nmp; 21903 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21904 /* Stash for rtt use later */ 21905 (*xmit_tail)->b_prev = local_time; 21906 (*xmit_tail)->b_next = 21907 (mblk_t *)(uintptr_t)(*snxt - len); 21908 mp1->b_cont = dupb(*xmit_tail); 21909 mp1 = mp1->b_cont; 21910 21911 spill += nmpsz; 21912 if (mp1 == NULL) { 21913 *tail_unsent = spill; 21914 freemsg(mp); 21915 if (ire != NULL) 21916 IRE_REFRELE(ire); 21917 return (-1); /* out_of_mem */ 21918 } 21919 } 21920 21921 /* Trim back any surplus on the last mblk */ 21922 if (spill >= 0) { 21923 mp1->b_wptr -= spill; 21924 *tail_unsent = spill; 21925 } else { 21926 /* 21927 * We did not send everything we could in 21928 * order to remain within the b_cont limit. 21929 */ 21930 *usable -= spill; 21931 *snxt += spill; 21932 tcp->tcp_last_sent_len += spill; 21933 UPDATE_MIB(&tcps->tcps_mib, 21934 tcpOutDataBytes, spill); 21935 /* 21936 * Adjust the checksum 21937 */ 21938 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21939 sum += spill; 21940 sum = (sum >> 16) + (sum & 0xFFFF); 21941 U16_TO_ABE16(sum, tcph->th_sum); 21942 if (tcp->tcp_ipversion == IPV4_VERSION) { 21943 sum = ntohs( 21944 ((ipha_t *)rptr)->ipha_length) + 21945 spill; 21946 ((ipha_t *)rptr)->ipha_length = 21947 htons(sum); 21948 } else { 21949 sum = ntohs( 21950 ((ip6_t *)rptr)->ip6_plen) + 21951 spill; 21952 ((ip6_t *)rptr)->ip6_plen = 21953 htons(sum); 21954 } 21955 *tail_unsent = 0; 21956 } 21957 } 21958 if (tcp->tcp_ip_forward_progress) { 21959 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21960 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21961 tcp->tcp_ip_forward_progress = B_FALSE; 21962 } 21963 21964 if (do_lso_send) { 21965 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21966 num_lso_seg); 21967 tcp->tcp_obsegs += num_lso_seg; 21968 21969 TCP_STAT(tcps, tcp_lso_times); 21970 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21971 } else { 21972 tcp_send_data(tcp, q, mp); 21973 BUMP_LOCAL(tcp->tcp_obsegs); 21974 } 21975 } 21976 21977 if (ire != NULL) 21978 IRE_REFRELE(ire); 21979 return (0); 21980 } 21981 21982 /* Unlink and return any mblk that looks like it contains a MDT info */ 21983 static mblk_t * 21984 tcp_mdt_info_mp(mblk_t *mp) 21985 { 21986 mblk_t *prev_mp; 21987 21988 for (;;) { 21989 prev_mp = mp; 21990 /* no more to process? */ 21991 if ((mp = mp->b_cont) == NULL) 21992 break; 21993 21994 switch (DB_TYPE(mp)) { 21995 case M_CTL: 21996 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21997 continue; 21998 ASSERT(prev_mp != NULL); 21999 prev_mp->b_cont = mp->b_cont; 22000 mp->b_cont = NULL; 22001 return (mp); 22002 default: 22003 break; 22004 } 22005 } 22006 return (mp); 22007 } 22008 22009 /* MDT info update routine, called when IP notifies us about MDT */ 22010 static void 22011 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 22012 { 22013 boolean_t prev_state; 22014 tcp_stack_t *tcps = tcp->tcp_tcps; 22015 22016 /* 22017 * IP is telling us to abort MDT on this connection? We know 22018 * this because the capability is only turned off when IP 22019 * encounters some pathological cases, e.g. link-layer change 22020 * where the new driver doesn't support MDT, or in situation 22021 * where MDT usage on the link-layer has been switched off. 22022 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 22023 * if the link-layer doesn't support MDT, and if it does, it 22024 * will indicate that the feature is to be turned on. 22025 */ 22026 prev_state = tcp->tcp_mdt; 22027 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 22028 if (!tcp->tcp_mdt && !first) { 22029 TCP_STAT(tcps, tcp_mdt_conn_halted3); 22030 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 22031 (void *)tcp->tcp_connp)); 22032 } 22033 22034 /* 22035 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 22036 * so disable MDT otherwise. The checks are done here 22037 * and in tcp_wput_data(). 22038 */ 22039 if (tcp->tcp_mdt && 22040 (tcp->tcp_ipversion == IPV4_VERSION && 22041 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22042 (tcp->tcp_ipversion == IPV6_VERSION && 22043 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 22044 tcp->tcp_mdt = B_FALSE; 22045 22046 if (tcp->tcp_mdt) { 22047 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 22048 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 22049 "version (%d), expected version is %d", 22050 mdt_capab->ill_mdt_version, MDT_VERSION_2); 22051 tcp->tcp_mdt = B_FALSE; 22052 return; 22053 } 22054 22055 /* 22056 * We need the driver to be able to handle at least three 22057 * spans per packet in order for tcp MDT to be utilized. 22058 * The first is for the header portion, while the rest are 22059 * needed to handle a packet that straddles across two 22060 * virtually non-contiguous buffers; a typical tcp packet 22061 * therefore consists of only two spans. Note that we take 22062 * a zero as "don't care". 22063 */ 22064 if (mdt_capab->ill_mdt_span_limit > 0 && 22065 mdt_capab->ill_mdt_span_limit < 3) { 22066 tcp->tcp_mdt = B_FALSE; 22067 return; 22068 } 22069 22070 /* a zero means driver wants default value */ 22071 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22072 tcps->tcps_mdt_max_pbufs); 22073 if (tcp->tcp_mdt_max_pld == 0) 22074 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22075 22076 /* ensure 32-bit alignment */ 22077 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22078 mdt_capab->ill_mdt_hdr_head), 4); 22079 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22080 mdt_capab->ill_mdt_hdr_tail), 4); 22081 22082 if (!first && !prev_state) { 22083 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22084 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22085 (void *)tcp->tcp_connp)); 22086 } 22087 } 22088 } 22089 22090 /* Unlink and return any mblk that looks like it contains a LSO info */ 22091 static mblk_t * 22092 tcp_lso_info_mp(mblk_t *mp) 22093 { 22094 mblk_t *prev_mp; 22095 22096 for (;;) { 22097 prev_mp = mp; 22098 /* no more to process? */ 22099 if ((mp = mp->b_cont) == NULL) 22100 break; 22101 22102 switch (DB_TYPE(mp)) { 22103 case M_CTL: 22104 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22105 continue; 22106 ASSERT(prev_mp != NULL); 22107 prev_mp->b_cont = mp->b_cont; 22108 mp->b_cont = NULL; 22109 return (mp); 22110 default: 22111 break; 22112 } 22113 } 22114 22115 return (mp); 22116 } 22117 22118 /* LSO info update routine, called when IP notifies us about LSO */ 22119 static void 22120 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22121 { 22122 tcp_stack_t *tcps = tcp->tcp_tcps; 22123 22124 /* 22125 * IP is telling us to abort LSO on this connection? We know 22126 * this because the capability is only turned off when IP 22127 * encounters some pathological cases, e.g. link-layer change 22128 * where the new NIC/driver doesn't support LSO, or in situation 22129 * where LSO usage on the link-layer has been switched off. 22130 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22131 * if the link-layer doesn't support LSO, and if it does, it 22132 * will indicate that the feature is to be turned on. 22133 */ 22134 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22135 TCP_STAT(tcps, tcp_lso_enabled); 22136 22137 /* 22138 * We currently only support LSO on simple TCP/IPv4, 22139 * so disable LSO otherwise. The checks are done here 22140 * and in tcp_wput_data(). 22141 */ 22142 if (tcp->tcp_lso && 22143 (tcp->tcp_ipversion == IPV4_VERSION && 22144 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22145 (tcp->tcp_ipversion == IPV6_VERSION)) { 22146 tcp->tcp_lso = B_FALSE; 22147 TCP_STAT(tcps, tcp_lso_disabled); 22148 } else { 22149 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22150 lso_capab->ill_lso_max); 22151 } 22152 } 22153 22154 static void 22155 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22156 { 22157 conn_t *connp = tcp->tcp_connp; 22158 tcp_stack_t *tcps = tcp->tcp_tcps; 22159 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22160 22161 ASSERT(ire != NULL); 22162 22163 /* 22164 * We may be in the fastpath here, and although we essentially do 22165 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22166 * we try to keep things as brief as possible. After all, these 22167 * are only best-effort checks, and we do more thorough ones prior 22168 * to calling tcp_send()/tcp_multisend(). 22169 */ 22170 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22171 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22172 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22173 !(ire->ire_flags & RTF_MULTIRT) && 22174 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22175 CONN_IS_LSO_MD_FASTPATH(connp)) { 22176 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22177 /* Cache the result */ 22178 connp->conn_lso_ok = B_TRUE; 22179 22180 ASSERT(ill->ill_lso_capab != NULL); 22181 if (!ill->ill_lso_capab->ill_lso_on) { 22182 ill->ill_lso_capab->ill_lso_on = 1; 22183 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22184 "LSO for interface %s\n", (void *)connp, 22185 ill->ill_name)); 22186 } 22187 tcp_lso_update(tcp, ill->ill_lso_capab); 22188 } else if (ipst->ips_ip_multidata_outbound && 22189 ILL_MDT_CAPABLE(ill)) { 22190 /* Cache the result */ 22191 connp->conn_mdt_ok = B_TRUE; 22192 22193 ASSERT(ill->ill_mdt_capab != NULL); 22194 if (!ill->ill_mdt_capab->ill_mdt_on) { 22195 ill->ill_mdt_capab->ill_mdt_on = 1; 22196 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22197 "MDT for interface %s\n", (void *)connp, 22198 ill->ill_name)); 22199 } 22200 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22201 } 22202 } 22203 22204 /* 22205 * The goal is to reduce the number of generated tcp segments by 22206 * setting the maxpsz multiplier to 0; this will have an affect on 22207 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22208 * into each packet, up to SMSS bytes. Doing this reduces the number 22209 * of outbound segments and incoming ACKs, thus allowing for better 22210 * network and system performance. In contrast the legacy behavior 22211 * may result in sending less than SMSS size, because the last mblk 22212 * for some packets may have more data than needed to make up SMSS, 22213 * and the legacy code refused to "split" it. 22214 * 22215 * We apply the new behavior on following situations: 22216 * 22217 * 1) Loopback connections, 22218 * 2) Connections in which the remote peer is not on local subnet, 22219 * 3) Local subnet connections over the bge interface (see below). 22220 * 22221 * Ideally, we would like this behavior to apply for interfaces other 22222 * than bge. However, doing so would negatively impact drivers which 22223 * perform dynamic mapping and unmapping of DMA resources, which are 22224 * increased by setting the maxpsz multiplier to 0 (more mblks per 22225 * packet will be generated by tcp). The bge driver does not suffer 22226 * from this, as it copies the mblks into pre-mapped buffers, and 22227 * therefore does not require more I/O resources than before. 22228 * 22229 * Otherwise, this behavior is present on all network interfaces when 22230 * the destination endpoint is non-local, since reducing the number 22231 * of packets in general is good for the network. 22232 * 22233 * TODO We need to remove this hard-coded conditional for bge once 22234 * a better "self-tuning" mechanism, or a way to comprehend 22235 * the driver transmit strategy is devised. Until the solution 22236 * is found and well understood, we live with this hack. 22237 */ 22238 if (!tcp_static_maxpsz && 22239 (tcp->tcp_loopback || !tcp->tcp_localnet || 22240 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22241 /* override the default value */ 22242 tcp->tcp_maxpsz = 0; 22243 22244 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22245 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22246 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22247 } 22248 22249 /* set the stream head parameters accordingly */ 22250 (void) tcp_maxpsz_set(tcp, B_TRUE); 22251 } 22252 22253 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22254 static void 22255 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22256 { 22257 uchar_t fval = *mp->b_rptr; 22258 mblk_t *tail; 22259 queue_t *q = tcp->tcp_wq; 22260 22261 /* TODO: How should flush interact with urgent data? */ 22262 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22263 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22264 /* 22265 * Flush only data that has not yet been put on the wire. If 22266 * we flush data that we have already transmitted, life, as we 22267 * know it, may come to an end. 22268 */ 22269 tail = tcp->tcp_xmit_tail; 22270 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22271 tcp->tcp_xmit_tail_unsent = 0; 22272 tcp->tcp_unsent = 0; 22273 if (tail->b_wptr != tail->b_rptr) 22274 tail = tail->b_cont; 22275 if (tail) { 22276 mblk_t **excess = &tcp->tcp_xmit_head; 22277 for (;;) { 22278 mblk_t *mp1 = *excess; 22279 if (mp1 == tail) 22280 break; 22281 tcp->tcp_xmit_tail = mp1; 22282 tcp->tcp_xmit_last = mp1; 22283 excess = &mp1->b_cont; 22284 } 22285 *excess = NULL; 22286 tcp_close_mpp(&tail); 22287 if (tcp->tcp_snd_zcopy_aware) 22288 tcp_zcopy_notify(tcp); 22289 } 22290 /* 22291 * We have no unsent data, so unsent must be less than 22292 * tcp_xmit_lowater, so re-enable flow. 22293 */ 22294 mutex_enter(&tcp->tcp_non_sq_lock); 22295 if (tcp->tcp_flow_stopped) { 22296 tcp_clrqfull(tcp); 22297 } 22298 mutex_exit(&tcp->tcp_non_sq_lock); 22299 } 22300 /* 22301 * TODO: you can't just flush these, you have to increase rwnd for one 22302 * thing. For another, how should urgent data interact? 22303 */ 22304 if (fval & FLUSHR) { 22305 *mp->b_rptr = fval & ~FLUSHW; 22306 /* XXX */ 22307 qreply(q, mp); 22308 return; 22309 } 22310 freemsg(mp); 22311 } 22312 22313 /* 22314 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22315 * messages. 22316 */ 22317 static void 22318 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22319 { 22320 mblk_t *mp1; 22321 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22322 STRUCT_HANDLE(strbuf, sb); 22323 queue_t *q = tcp->tcp_wq; 22324 int error; 22325 uint_t addrlen; 22326 22327 /* Make sure it is one of ours. */ 22328 switch (iocp->ioc_cmd) { 22329 case TI_GETMYNAME: 22330 case TI_GETPEERNAME: 22331 break; 22332 default: 22333 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22334 return; 22335 } 22336 switch (mi_copy_state(q, mp, &mp1)) { 22337 case -1: 22338 return; 22339 case MI_COPY_CASE(MI_COPY_IN, 1): 22340 break; 22341 case MI_COPY_CASE(MI_COPY_OUT, 1): 22342 /* Copy out the strbuf. */ 22343 mi_copyout(q, mp); 22344 return; 22345 case MI_COPY_CASE(MI_COPY_OUT, 2): 22346 /* All done. */ 22347 mi_copy_done(q, mp, 0); 22348 return; 22349 default: 22350 mi_copy_done(q, mp, EPROTO); 22351 return; 22352 } 22353 /* Check alignment of the strbuf */ 22354 if (!OK_32PTR(mp1->b_rptr)) { 22355 mi_copy_done(q, mp, EINVAL); 22356 return; 22357 } 22358 22359 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22360 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22361 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22362 mi_copy_done(q, mp, EINVAL); 22363 return; 22364 } 22365 22366 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22367 if (mp1 == NULL) 22368 return; 22369 22370 switch (iocp->ioc_cmd) { 22371 case TI_GETMYNAME: 22372 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22373 break; 22374 case TI_GETPEERNAME: 22375 error = tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22376 break; 22377 } 22378 22379 if (error != 0) { 22380 mi_copy_done(q, mp, error); 22381 } else { 22382 mp1->b_wptr += addrlen; 22383 STRUCT_FSET(sb, len, addrlen); 22384 22385 /* Copy out the address */ 22386 mi_copyout(q, mp); 22387 } 22388 } 22389 22390 /* 22391 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22392 * messages. 22393 */ 22394 /* ARGSUSED */ 22395 static void 22396 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22397 { 22398 conn_t *connp = (conn_t *)arg; 22399 tcp_t *tcp = connp->conn_tcp; 22400 queue_t *q = tcp->tcp_wq; 22401 struct iocblk *iocp; 22402 tcp_stack_t *tcps = tcp->tcp_tcps; 22403 22404 ASSERT(DB_TYPE(mp) == M_IOCTL); 22405 /* 22406 * Try and ASSERT the minimum possible references on the 22407 * conn early enough. Since we are executing on write side, 22408 * the connection is obviously not detached and that means 22409 * there is a ref each for TCP and IP. Since we are behind 22410 * the squeue, the minimum references needed are 3. If the 22411 * conn is in classifier hash list, there should be an 22412 * extra ref for that (we check both the possibilities). 22413 */ 22414 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22415 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22416 22417 iocp = (struct iocblk *)mp->b_rptr; 22418 switch (iocp->ioc_cmd) { 22419 case TCP_IOC_DEFAULT_Q: 22420 /* Wants to be the default wq. */ 22421 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22422 iocp->ioc_error = EPERM; 22423 iocp->ioc_count = 0; 22424 mp->b_datap->db_type = M_IOCACK; 22425 qreply(q, mp); 22426 return; 22427 } 22428 tcp_def_q_set(tcp, mp); 22429 return; 22430 case _SIOCSOCKFALLBACK: 22431 /* 22432 * Either sockmod is about to be popped and the socket 22433 * would now be treated as a plain stream, or a module 22434 * is about to be pushed so we could no longer use read- 22435 * side synchronous streams for fused loopback tcp. 22436 * Drain any queued data and disable direct sockfs 22437 * interface from now on. 22438 */ 22439 if (!tcp->tcp_issocket) { 22440 DB_TYPE(mp) = M_IOCNAK; 22441 iocp->ioc_error = EINVAL; 22442 } else { 22443 #ifdef _ILP32 22444 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 22445 #else 22446 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22447 #endif 22448 /* 22449 * Insert this socket into the acceptor hash. 22450 * We might need it for T_CONN_RES message 22451 */ 22452 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22453 22454 if (tcp->tcp_fused) { 22455 /* 22456 * This is a fused loopback tcp; disable 22457 * read-side synchronous streams interface 22458 * and drain any queued data. It is okay 22459 * to do this for non-synchronous streams 22460 * fused tcp as well. 22461 */ 22462 tcp_fuse_disable_pair(tcp, B_FALSE); 22463 } 22464 tcp->tcp_issocket = B_FALSE; 22465 tcp->tcp_sodirect = NULL; 22466 TCP_STAT(tcps, tcp_sock_fallback); 22467 22468 DB_TYPE(mp) = M_IOCACK; 22469 iocp->ioc_error = 0; 22470 } 22471 iocp->ioc_count = 0; 22472 iocp->ioc_rval = 0; 22473 qreply(q, mp); 22474 return; 22475 } 22476 CALL_IP_WPUT(connp, q, mp); 22477 } 22478 22479 /* 22480 * This routine is called by tcp_wput() to handle all TPI requests. 22481 */ 22482 /* ARGSUSED */ 22483 static void 22484 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22485 { 22486 conn_t *connp = (conn_t *)arg; 22487 tcp_t *tcp = connp->conn_tcp; 22488 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22489 uchar_t *rptr; 22490 t_scalar_t type; 22491 int len; 22492 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22493 22494 /* 22495 * Try and ASSERT the minimum possible references on the 22496 * conn early enough. Since we are executing on write side, 22497 * the connection is obviously not detached and that means 22498 * there is a ref each for TCP and IP. Since we are behind 22499 * the squeue, the minimum references needed are 3. If the 22500 * conn is in classifier hash list, there should be an 22501 * extra ref for that (we check both the possibilities). 22502 */ 22503 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22504 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22505 22506 rptr = mp->b_rptr; 22507 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22508 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22509 type = ((union T_primitives *)rptr)->type; 22510 if (type == T_EXDATA_REQ) { 22511 uint32_t msize = msgdsize(mp->b_cont); 22512 22513 len = msize - 1; 22514 if (len < 0) { 22515 freemsg(mp); 22516 return; 22517 } 22518 /* 22519 * Try to force urgent data out on the wire. 22520 * Even if we have unsent data this will 22521 * at least send the urgent flag. 22522 * XXX does not handle more flag correctly. 22523 */ 22524 len += tcp->tcp_unsent; 22525 len += tcp->tcp_snxt; 22526 tcp->tcp_urg = len; 22527 tcp->tcp_valid_bits |= TCP_URG_VALID; 22528 22529 /* Bypass tcp protocol for fused tcp loopback */ 22530 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 22531 return; 22532 } else if (type != T_DATA_REQ) { 22533 goto non_urgent_data; 22534 } 22535 /* TODO: options, flags, ... from user */ 22536 /* Set length to zero for reclamation below */ 22537 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22538 freeb(mp); 22539 return; 22540 } else { 22541 if (tcp->tcp_debug) { 22542 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22543 "tcp_wput_proto, dropping one..."); 22544 } 22545 freemsg(mp); 22546 return; 22547 } 22548 22549 non_urgent_data: 22550 22551 switch ((int)tprim->type) { 22552 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22553 /* 22554 * save the kssl_ent_t from the next block, and convert this 22555 * back to a normal bind_req. 22556 */ 22557 if (mp->b_cont != NULL) { 22558 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22559 22560 if (tcp->tcp_kssl_ent != NULL) { 22561 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22562 KSSL_NO_PROXY); 22563 tcp->tcp_kssl_ent = NULL; 22564 } 22565 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22566 sizeof (kssl_ent_t)); 22567 kssl_hold_ent(tcp->tcp_kssl_ent); 22568 freemsg(mp->b_cont); 22569 mp->b_cont = NULL; 22570 } 22571 tprim->type = T_BIND_REQ; 22572 22573 /* FALLTHROUGH */ 22574 case O_T_BIND_REQ: /* bind request */ 22575 case T_BIND_REQ: /* new semantics bind request */ 22576 tcp_bind(tcp, mp); 22577 break; 22578 case T_UNBIND_REQ: /* unbind request */ 22579 tcp_unbind(tcp, mp); 22580 break; 22581 case O_T_CONN_RES: /* old connection response XXX */ 22582 case T_CONN_RES: /* connection response */ 22583 tcp_accept(tcp, mp); 22584 break; 22585 case T_CONN_REQ: /* connection request */ 22586 tcp_connect(tcp, mp); 22587 break; 22588 case T_DISCON_REQ: /* disconnect request */ 22589 tcp_disconnect(tcp, mp); 22590 break; 22591 case T_CAPABILITY_REQ: 22592 tcp_capability_req(tcp, mp); /* capability request */ 22593 break; 22594 case T_INFO_REQ: /* information request */ 22595 tcp_info_req(tcp, mp); 22596 break; 22597 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22598 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22599 &tcp_opt_obj, B_TRUE); 22600 break; 22601 case T_OPTMGMT_REQ: 22602 /* 22603 * Note: no support for snmpcom_req() through new 22604 * T_OPTMGMT_REQ. See comments in ip.c 22605 */ 22606 /* Only IP is allowed to return meaningful value */ 22607 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22608 B_TRUE); 22609 break; 22610 22611 case T_UNITDATA_REQ: /* unitdata request */ 22612 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22613 break; 22614 case T_ORDREL_REQ: /* orderly release req */ 22615 freemsg(mp); 22616 22617 if (tcp->tcp_fused) 22618 tcp_unfuse(tcp); 22619 22620 if (tcp_xmit_end(tcp) != 0) { 22621 /* 22622 * We were crossing FINs and got a reset from 22623 * the other side. Just ignore it. 22624 */ 22625 if (tcp->tcp_debug) { 22626 (void) strlog(TCP_MOD_ID, 0, 1, 22627 SL_ERROR|SL_TRACE, 22628 "tcp_wput_proto, T_ORDREL_REQ out of " 22629 "state %s", 22630 tcp_display(tcp, NULL, 22631 DISP_ADDR_AND_PORT)); 22632 } 22633 } 22634 break; 22635 case T_ADDR_REQ: 22636 tcp_addr_req(tcp, mp); 22637 break; 22638 default: 22639 if (tcp->tcp_debug) { 22640 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22641 "tcp_wput_proto, bogus TPI msg, type %d", 22642 tprim->type); 22643 } 22644 /* 22645 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22646 * to recover. 22647 */ 22648 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22649 break; 22650 } 22651 } 22652 22653 /* 22654 * The TCP write service routine should never be called... 22655 */ 22656 /* ARGSUSED */ 22657 static void 22658 tcp_wsrv(queue_t *q) 22659 { 22660 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22661 22662 TCP_STAT(tcps, tcp_wsrv_called); 22663 } 22664 22665 /* Non overlapping byte exchanger */ 22666 static void 22667 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22668 { 22669 uchar_t uch; 22670 22671 while (len-- > 0) { 22672 uch = a[len]; 22673 a[len] = b[len]; 22674 b[len] = uch; 22675 } 22676 } 22677 22678 /* 22679 * Send out a control packet on the tcp connection specified. This routine 22680 * is typically called where we need a simple ACK or RST generated. 22681 */ 22682 static void 22683 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22684 { 22685 uchar_t *rptr; 22686 tcph_t *tcph; 22687 ipha_t *ipha = NULL; 22688 ip6_t *ip6h = NULL; 22689 uint32_t sum; 22690 int tcp_hdr_len; 22691 int tcp_ip_hdr_len; 22692 mblk_t *mp; 22693 tcp_stack_t *tcps = tcp->tcp_tcps; 22694 22695 /* 22696 * Save sum for use in source route later. 22697 */ 22698 ASSERT(tcp != NULL); 22699 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22700 tcp_hdr_len = tcp->tcp_hdr_len; 22701 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22702 22703 /* If a text string is passed in with the request, pass it to strlog. */ 22704 if (str != NULL && tcp->tcp_debug) { 22705 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22706 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22707 str, seq, ack, ctl); 22708 } 22709 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22710 BPRI_MED); 22711 if (mp == NULL) { 22712 return; 22713 } 22714 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22715 mp->b_rptr = rptr; 22716 mp->b_wptr = &rptr[tcp_hdr_len]; 22717 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22718 22719 if (tcp->tcp_ipversion == IPV4_VERSION) { 22720 ipha = (ipha_t *)rptr; 22721 ipha->ipha_length = htons(tcp_hdr_len); 22722 } else { 22723 ip6h = (ip6_t *)rptr; 22724 ASSERT(tcp != NULL); 22725 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22726 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22727 } 22728 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22729 tcph->th_flags[0] = (uint8_t)ctl; 22730 if (ctl & TH_RST) { 22731 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22732 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22733 /* 22734 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22735 */ 22736 if (tcp->tcp_snd_ts_ok && 22737 tcp->tcp_state > TCPS_SYN_SENT) { 22738 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22739 *(mp->b_wptr) = TCPOPT_EOL; 22740 if (tcp->tcp_ipversion == IPV4_VERSION) { 22741 ipha->ipha_length = htons(tcp_hdr_len - 22742 TCPOPT_REAL_TS_LEN); 22743 } else { 22744 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22745 TCPOPT_REAL_TS_LEN); 22746 } 22747 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22748 sum -= TCPOPT_REAL_TS_LEN; 22749 } 22750 } 22751 if (ctl & TH_ACK) { 22752 if (tcp->tcp_snd_ts_ok) { 22753 U32_TO_BE32(lbolt, 22754 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22755 U32_TO_BE32(tcp->tcp_ts_recent, 22756 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22757 } 22758 22759 /* Update the latest receive window size in TCP header. */ 22760 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22761 tcph->th_win); 22762 tcp->tcp_rack = ack; 22763 tcp->tcp_rack_cnt = 0; 22764 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22765 } 22766 BUMP_LOCAL(tcp->tcp_obsegs); 22767 U32_TO_BE32(seq, tcph->th_seq); 22768 U32_TO_BE32(ack, tcph->th_ack); 22769 /* 22770 * Include the adjustment for a source route if any. 22771 */ 22772 sum = (sum >> 16) + (sum & 0xFFFF); 22773 U16_TO_BE16(sum, tcph->th_sum); 22774 tcp_send_data(tcp, tcp->tcp_wq, mp); 22775 } 22776 22777 /* 22778 * If this routine returns B_TRUE, TCP can generate a RST in response 22779 * to a segment. If it returns B_FALSE, TCP should not respond. 22780 */ 22781 static boolean_t 22782 tcp_send_rst_chk(tcp_stack_t *tcps) 22783 { 22784 clock_t now; 22785 22786 /* 22787 * TCP needs to protect itself from generating too many RSTs. 22788 * This can be a DoS attack by sending us random segments 22789 * soliciting RSTs. 22790 * 22791 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22792 * in each 1 second interval. In this way, TCP still generate 22793 * RSTs in normal cases but when under attack, the impact is 22794 * limited. 22795 */ 22796 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22797 now = lbolt; 22798 /* lbolt can wrap around. */ 22799 if ((tcps->tcps_last_rst_intrvl > now) || 22800 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22801 1*SECONDS)) { 22802 tcps->tcps_last_rst_intrvl = now; 22803 tcps->tcps_rst_cnt = 1; 22804 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22805 return (B_FALSE); 22806 } 22807 } 22808 return (B_TRUE); 22809 } 22810 22811 /* 22812 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22813 */ 22814 static void 22815 tcp_ip_ire_mark_advice(tcp_t *tcp) 22816 { 22817 mblk_t *mp; 22818 ipic_t *ipic; 22819 22820 if (tcp->tcp_ipversion == IPV4_VERSION) { 22821 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22822 &ipic); 22823 } else { 22824 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22825 &ipic); 22826 } 22827 if (mp == NULL) 22828 return; 22829 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22830 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22831 } 22832 22833 /* 22834 * Return an IP advice ioctl mblk and set ipic to be the pointer 22835 * to the advice structure. 22836 */ 22837 static mblk_t * 22838 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22839 { 22840 struct iocblk *ioc; 22841 mblk_t *mp, *mp1; 22842 22843 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22844 if (mp == NULL) 22845 return (NULL); 22846 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22847 *ipic = (ipic_t *)mp->b_rptr; 22848 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22849 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22850 22851 bcopy(addr, *ipic + 1, addr_len); 22852 22853 (*ipic)->ipic_addr_length = addr_len; 22854 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22855 22856 mp1 = mkiocb(IP_IOCTL); 22857 if (mp1 == NULL) { 22858 freemsg(mp); 22859 return (NULL); 22860 } 22861 mp1->b_cont = mp; 22862 ioc = (struct iocblk *)mp1->b_rptr; 22863 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22864 22865 return (mp1); 22866 } 22867 22868 /* 22869 * Generate a reset based on an inbound packet, connp is set by caller 22870 * when RST is in response to an unexpected inbound packet for which 22871 * there is active tcp state in the system. 22872 * 22873 * IPSEC NOTE : Try to send the reply with the same protection as it came 22874 * in. We still have the ipsec_mp that the packet was attached to. Thus 22875 * the packet will go out at the same level of protection as it came in by 22876 * converting the IPSEC_IN to IPSEC_OUT. 22877 */ 22878 static void 22879 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22880 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22881 tcp_stack_t *tcps, conn_t *connp) 22882 { 22883 ipha_t *ipha = NULL; 22884 ip6_t *ip6h = NULL; 22885 ushort_t len; 22886 tcph_t *tcph; 22887 int i; 22888 mblk_t *ipsec_mp; 22889 boolean_t mctl_present; 22890 ipic_t *ipic; 22891 ipaddr_t v4addr; 22892 in6_addr_t v6addr; 22893 int addr_len; 22894 void *addr; 22895 queue_t *q = tcps->tcps_g_q; 22896 tcp_t *tcp; 22897 cred_t *cr; 22898 mblk_t *nmp; 22899 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22900 22901 if (tcps->tcps_g_q == NULL) { 22902 /* 22903 * For non-zero stackids the default queue isn't created 22904 * until the first open, thus there can be a need to send 22905 * a reset before then. But we can't do that, hence we just 22906 * drop the packet. Later during boot, when the default queue 22907 * has been setup, a retransmitted packet from the peer 22908 * will result in a reset. 22909 */ 22910 ASSERT(tcps->tcps_netstack->netstack_stackid != 22911 GLOBAL_NETSTACKID); 22912 freemsg(mp); 22913 return; 22914 } 22915 22916 if (connp != NULL) 22917 tcp = connp->conn_tcp; 22918 else 22919 tcp = Q_TO_TCP(q); 22920 22921 if (!tcp_send_rst_chk(tcps)) { 22922 tcps->tcps_rst_unsent++; 22923 freemsg(mp); 22924 return; 22925 } 22926 22927 if (mp->b_datap->db_type == M_CTL) { 22928 ipsec_mp = mp; 22929 mp = mp->b_cont; 22930 mctl_present = B_TRUE; 22931 } else { 22932 ipsec_mp = mp; 22933 mctl_present = B_FALSE; 22934 } 22935 22936 if (str && q && tcps->tcps_dbg) { 22937 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22938 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22939 "flags 0x%x", 22940 str, seq, ack, ctl); 22941 } 22942 if (mp->b_datap->db_ref != 1) { 22943 mblk_t *mp1 = copyb(mp); 22944 freemsg(mp); 22945 mp = mp1; 22946 if (!mp) { 22947 if (mctl_present) 22948 freeb(ipsec_mp); 22949 return; 22950 } else { 22951 if (mctl_present) { 22952 ipsec_mp->b_cont = mp; 22953 } else { 22954 ipsec_mp = mp; 22955 } 22956 } 22957 } else if (mp->b_cont) { 22958 freemsg(mp->b_cont); 22959 mp->b_cont = NULL; 22960 } 22961 /* 22962 * We skip reversing source route here. 22963 * (for now we replace all IP options with EOL) 22964 */ 22965 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22966 ipha = (ipha_t *)mp->b_rptr; 22967 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22968 mp->b_rptr[i] = IPOPT_EOL; 22969 /* 22970 * Make sure that src address isn't flagrantly invalid. 22971 * Not all broadcast address checking for the src address 22972 * is possible, since we don't know the netmask of the src 22973 * addr. No check for destination address is done, since 22974 * IP will not pass up a packet with a broadcast dest 22975 * address to TCP. Similar checks are done below for IPv6. 22976 */ 22977 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22978 CLASSD(ipha->ipha_src)) { 22979 freemsg(ipsec_mp); 22980 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22981 return; 22982 } 22983 } else { 22984 ip6h = (ip6_t *)mp->b_rptr; 22985 22986 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22987 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22988 freemsg(ipsec_mp); 22989 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22990 return; 22991 } 22992 22993 /* Remove any extension headers assuming partial overlay */ 22994 if (ip_hdr_len > IPV6_HDR_LEN) { 22995 uint8_t *to; 22996 22997 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22998 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22999 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 23000 ip_hdr_len = IPV6_HDR_LEN; 23001 ip6h = (ip6_t *)mp->b_rptr; 23002 ip6h->ip6_nxt = IPPROTO_TCP; 23003 } 23004 } 23005 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 23006 if (tcph->th_flags[0] & TH_RST) { 23007 freemsg(ipsec_mp); 23008 return; 23009 } 23010 tcph->th_offset_and_rsrvd[0] = (5 << 4); 23011 len = ip_hdr_len + sizeof (tcph_t); 23012 mp->b_wptr = &mp->b_rptr[len]; 23013 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23014 ipha->ipha_length = htons(len); 23015 /* Swap addresses */ 23016 v4addr = ipha->ipha_src; 23017 ipha->ipha_src = ipha->ipha_dst; 23018 ipha->ipha_dst = v4addr; 23019 ipha->ipha_ident = 0; 23020 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 23021 addr_len = IP_ADDR_LEN; 23022 addr = &v4addr; 23023 } else { 23024 /* No ip6i_t in this case */ 23025 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 23026 /* Swap addresses */ 23027 v6addr = ip6h->ip6_src; 23028 ip6h->ip6_src = ip6h->ip6_dst; 23029 ip6h->ip6_dst = v6addr; 23030 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 23031 addr_len = IPV6_ADDR_LEN; 23032 addr = &v6addr; 23033 } 23034 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 23035 U32_TO_BE32(ack, tcph->th_ack); 23036 U32_TO_BE32(seq, tcph->th_seq); 23037 U16_TO_BE16(0, tcph->th_win); 23038 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 23039 tcph->th_flags[0] = (uint8_t)ctl; 23040 if (ctl & TH_RST) { 23041 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 23042 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23043 } 23044 23045 /* IP trusts us to set up labels when required. */ 23046 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 23047 crgetlabel(cr) != NULL) { 23048 int err; 23049 23050 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23051 err = tsol_check_label(cr, &mp, 23052 tcp->tcp_connp->conn_mac_exempt, 23053 tcps->tcps_netstack->netstack_ip); 23054 else 23055 err = tsol_check_label_v6(cr, &mp, 23056 tcp->tcp_connp->conn_mac_exempt, 23057 tcps->tcps_netstack->netstack_ip); 23058 if (mctl_present) 23059 ipsec_mp->b_cont = mp; 23060 else 23061 ipsec_mp = mp; 23062 if (err != 0) { 23063 freemsg(ipsec_mp); 23064 return; 23065 } 23066 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23067 ipha = (ipha_t *)mp->b_rptr; 23068 } else { 23069 ip6h = (ip6_t *)mp->b_rptr; 23070 } 23071 } 23072 23073 if (mctl_present) { 23074 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23075 23076 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23077 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23078 return; 23079 } 23080 } 23081 if (zoneid == ALL_ZONES) 23082 zoneid = GLOBAL_ZONEID; 23083 23084 /* Add the zoneid so ip_output routes it properly */ 23085 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23086 freemsg(ipsec_mp); 23087 return; 23088 } 23089 ipsec_mp = nmp; 23090 23091 /* 23092 * NOTE: one might consider tracing a TCP packet here, but 23093 * this function has no active TCP state and no tcp structure 23094 * that has a trace buffer. If we traced here, we would have 23095 * to keep a local trace buffer in tcp_record_trace(). 23096 * 23097 * TSol note: The mblk that contains the incoming packet was 23098 * reused by tcp_xmit_listener_reset, so it already contains 23099 * the right credentials and we don't need to call mblk_setcred. 23100 * Also the conn's cred is not right since it is associated 23101 * with tcps_g_q. 23102 */ 23103 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23104 23105 /* 23106 * Tell IP to mark the IRE used for this destination temporary. 23107 * This way, we can limit our exposure to DoS attack because IP 23108 * creates an IRE for each destination. If there are too many, 23109 * the time to do any routing lookup will be extremely long. And 23110 * the lookup can be in interrupt context. 23111 * 23112 * Note that in normal circumstances, this marking should not 23113 * affect anything. It would be nice if only 1 message is 23114 * needed to inform IP that the IRE created for this RST should 23115 * not be added to the cache table. But there is currently 23116 * not such communication mechanism between TCP and IP. So 23117 * the best we can do now is to send the advice ioctl to IP 23118 * to mark the IRE temporary. 23119 */ 23120 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23121 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23122 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23123 } 23124 } 23125 23126 /* 23127 * Initiate closedown sequence on an active connection. (May be called as 23128 * writer.) Return value zero for OK return, non-zero for error return. 23129 */ 23130 static int 23131 tcp_xmit_end(tcp_t *tcp) 23132 { 23133 ipic_t *ipic; 23134 mblk_t *mp; 23135 tcp_stack_t *tcps = tcp->tcp_tcps; 23136 23137 if (tcp->tcp_state < TCPS_SYN_RCVD || 23138 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23139 /* 23140 * Invalid state, only states TCPS_SYN_RCVD, 23141 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23142 */ 23143 return (-1); 23144 } 23145 23146 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23147 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23148 /* 23149 * If there is nothing more unsent, send the FIN now. 23150 * Otherwise, it will go out with the last segment. 23151 */ 23152 if (tcp->tcp_unsent == 0) { 23153 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23154 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23155 23156 if (mp) { 23157 tcp_send_data(tcp, tcp->tcp_wq, mp); 23158 } else { 23159 /* 23160 * Couldn't allocate msg. Pretend we got it out. 23161 * Wait for rexmit timeout. 23162 */ 23163 tcp->tcp_snxt = tcp->tcp_fss + 1; 23164 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23165 } 23166 23167 /* 23168 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23169 * changed. 23170 */ 23171 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23172 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23173 } 23174 } else { 23175 /* 23176 * If tcp->tcp_cork is set, then the data will not get sent, 23177 * so we have to check that and unset it first. 23178 */ 23179 if (tcp->tcp_cork) 23180 tcp->tcp_cork = B_FALSE; 23181 tcp_wput_data(tcp, NULL, B_FALSE); 23182 } 23183 23184 /* 23185 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23186 * is 0, don't update the cache. 23187 */ 23188 if (tcps->tcps_rtt_updates == 0 || 23189 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23190 return (0); 23191 23192 /* 23193 * NOTE: should not update if source routes i.e. if tcp_remote if 23194 * different from the destination. 23195 */ 23196 if (tcp->tcp_ipversion == IPV4_VERSION) { 23197 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23198 return (0); 23199 } 23200 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23201 &ipic); 23202 } else { 23203 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23204 &tcp->tcp_ip6h->ip6_dst))) { 23205 return (0); 23206 } 23207 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23208 &ipic); 23209 } 23210 23211 /* Record route attributes in the IRE for use by future connections. */ 23212 if (mp == NULL) 23213 return (0); 23214 23215 /* 23216 * We do not have a good algorithm to update ssthresh at this time. 23217 * So don't do any update. 23218 */ 23219 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23220 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23221 23222 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23223 return (0); 23224 } 23225 23226 /* 23227 * Generate a "no listener here" RST in response to an "unknown" segment. 23228 * connp is set by caller when RST is in response to an unexpected 23229 * inbound packet for which there is active tcp state in the system. 23230 * Note that we are reusing the incoming mp to construct the outgoing RST. 23231 */ 23232 void 23233 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23234 tcp_stack_t *tcps, conn_t *connp) 23235 { 23236 uchar_t *rptr; 23237 uint32_t seg_len; 23238 tcph_t *tcph; 23239 uint32_t seg_seq; 23240 uint32_t seg_ack; 23241 uint_t flags; 23242 mblk_t *ipsec_mp; 23243 ipha_t *ipha; 23244 ip6_t *ip6h; 23245 boolean_t mctl_present = B_FALSE; 23246 boolean_t check = B_TRUE; 23247 boolean_t policy_present; 23248 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23249 23250 TCP_STAT(tcps, tcp_no_listener); 23251 23252 ipsec_mp = mp; 23253 23254 if (mp->b_datap->db_type == M_CTL) { 23255 ipsec_in_t *ii; 23256 23257 mctl_present = B_TRUE; 23258 mp = mp->b_cont; 23259 23260 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23261 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23262 if (ii->ipsec_in_dont_check) { 23263 check = B_FALSE; 23264 if (!ii->ipsec_in_secure) { 23265 freeb(ipsec_mp); 23266 mctl_present = B_FALSE; 23267 ipsec_mp = mp; 23268 } 23269 } 23270 } 23271 23272 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23273 policy_present = ipss->ipsec_inbound_v4_policy_present; 23274 ipha = (ipha_t *)mp->b_rptr; 23275 ip6h = NULL; 23276 } else { 23277 policy_present = ipss->ipsec_inbound_v6_policy_present; 23278 ipha = NULL; 23279 ip6h = (ip6_t *)mp->b_rptr; 23280 } 23281 23282 if (check && policy_present) { 23283 /* 23284 * The conn_t parameter is NULL because we already know 23285 * nobody's home. 23286 */ 23287 ipsec_mp = ipsec_check_global_policy( 23288 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23289 tcps->tcps_netstack); 23290 if (ipsec_mp == NULL) 23291 return; 23292 } 23293 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23294 DTRACE_PROBE2( 23295 tx__ip__log__error__nolistener__tcp, 23296 char *, "Could not reply with RST to mp(1)", 23297 mblk_t *, mp); 23298 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23299 freemsg(ipsec_mp); 23300 return; 23301 } 23302 23303 rptr = mp->b_rptr; 23304 23305 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23306 seg_seq = BE32_TO_U32(tcph->th_seq); 23307 seg_ack = BE32_TO_U32(tcph->th_ack); 23308 flags = tcph->th_flags[0]; 23309 23310 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23311 if (flags & TH_RST) { 23312 freemsg(ipsec_mp); 23313 } else if (flags & TH_ACK) { 23314 tcp_xmit_early_reset("no tcp, reset", 23315 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23316 connp); 23317 } else { 23318 if (flags & TH_SYN) { 23319 seg_len++; 23320 } else { 23321 /* 23322 * Here we violate the RFC. Note that a normal 23323 * TCP will never send a segment without the ACK 23324 * flag, except for RST or SYN segment. This 23325 * segment is neither. Just drop it on the 23326 * floor. 23327 */ 23328 freemsg(ipsec_mp); 23329 tcps->tcps_rst_unsent++; 23330 return; 23331 } 23332 23333 tcp_xmit_early_reset("no tcp, reset/ack", 23334 ipsec_mp, 0, seg_seq + seg_len, 23335 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23336 } 23337 } 23338 23339 /* 23340 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23341 * ip and tcp header ready to pass down to IP. If the mp passed in is 23342 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23343 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23344 * otherwise it will dup partial mblks.) 23345 * Otherwise, an appropriate ACK packet will be generated. This 23346 * routine is not usually called to send new data for the first time. It 23347 * is mostly called out of the timer for retransmits, and to generate ACKs. 23348 * 23349 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23350 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23351 * of the original mblk chain will be returned in *offset and *end_mp. 23352 */ 23353 mblk_t * 23354 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23355 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23356 boolean_t rexmit) 23357 { 23358 int data_length; 23359 int32_t off = 0; 23360 uint_t flags; 23361 mblk_t *mp1; 23362 mblk_t *mp2; 23363 uchar_t *rptr; 23364 tcph_t *tcph; 23365 int32_t num_sack_blk = 0; 23366 int32_t sack_opt_len = 0; 23367 tcp_stack_t *tcps = tcp->tcp_tcps; 23368 23369 /* Allocate for our maximum TCP header + link-level */ 23370 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23371 tcps->tcps_wroff_xtra, BPRI_MED); 23372 if (!mp1) 23373 return (NULL); 23374 data_length = 0; 23375 23376 /* 23377 * Note that tcp_mss has been adjusted to take into account the 23378 * timestamp option if applicable. Because SACK options do not 23379 * appear in every TCP segments and they are of variable lengths, 23380 * they cannot be included in tcp_mss. Thus we need to calculate 23381 * the actual segment length when we need to send a segment which 23382 * includes SACK options. 23383 */ 23384 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23385 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23386 tcp->tcp_num_sack_blk); 23387 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23388 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23389 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23390 max_to_send -= sack_opt_len; 23391 } 23392 23393 if (offset != NULL) { 23394 off = *offset; 23395 /* We use offset as an indicator that end_mp is not NULL. */ 23396 *end_mp = NULL; 23397 } 23398 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23399 /* This could be faster with cooperation from downstream */ 23400 if (mp2 != mp1 && !sendall && 23401 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23402 max_to_send) 23403 /* 23404 * Don't send the next mblk since the whole mblk 23405 * does not fit. 23406 */ 23407 break; 23408 mp2->b_cont = dupb(mp); 23409 mp2 = mp2->b_cont; 23410 if (!mp2) { 23411 freemsg(mp1); 23412 return (NULL); 23413 } 23414 mp2->b_rptr += off; 23415 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23416 (uintptr_t)INT_MAX); 23417 23418 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23419 if (data_length > max_to_send) { 23420 mp2->b_wptr -= data_length - max_to_send; 23421 data_length = max_to_send; 23422 off = mp2->b_wptr - mp->b_rptr; 23423 break; 23424 } else { 23425 off = 0; 23426 } 23427 } 23428 if (offset != NULL) { 23429 *offset = off; 23430 *end_mp = mp; 23431 } 23432 if (seg_len != NULL) { 23433 *seg_len = data_length; 23434 } 23435 23436 /* Update the latest receive window size in TCP header. */ 23437 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23438 tcp->tcp_tcph->th_win); 23439 23440 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23441 mp1->b_rptr = rptr; 23442 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23443 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23444 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23445 U32_TO_ABE32(seq, tcph->th_seq); 23446 23447 /* 23448 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23449 * that this function was called from tcp_wput_data. Thus, when called 23450 * to retransmit data the setting of the PUSH bit may appear some 23451 * what random in that it might get set when it should not. This 23452 * should not pose any performance issues. 23453 */ 23454 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23455 tcp->tcp_unsent == data_length)) { 23456 flags = TH_ACK | TH_PUSH; 23457 } else { 23458 flags = TH_ACK; 23459 } 23460 23461 if (tcp->tcp_ecn_ok) { 23462 if (tcp->tcp_ecn_echo_on) 23463 flags |= TH_ECE; 23464 23465 /* 23466 * Only set ECT bit and ECN_CWR if a segment contains new data. 23467 * There is no TCP flow control for non-data segments, and 23468 * only data segment is transmitted reliably. 23469 */ 23470 if (data_length > 0 && !rexmit) { 23471 SET_ECT(tcp, rptr); 23472 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23473 flags |= TH_CWR; 23474 tcp->tcp_ecn_cwr_sent = B_TRUE; 23475 } 23476 } 23477 } 23478 23479 if (tcp->tcp_valid_bits) { 23480 uint32_t u1; 23481 23482 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23483 seq == tcp->tcp_iss) { 23484 uchar_t *wptr; 23485 23486 /* 23487 * If TCP_ISS_VALID and the seq number is tcp_iss, 23488 * TCP can only be in SYN-SENT, SYN-RCVD or 23489 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23490 * our SYN is not ack'ed but the app closes this 23491 * TCP connection. 23492 */ 23493 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23494 tcp->tcp_state == TCPS_SYN_RCVD || 23495 tcp->tcp_state == TCPS_FIN_WAIT_1); 23496 23497 /* 23498 * Tack on the MSS option. It is always needed 23499 * for both active and passive open. 23500 * 23501 * MSS option value should be interface MTU - MIN 23502 * TCP/IP header according to RFC 793 as it means 23503 * the maximum segment size TCP can receive. But 23504 * to get around some broken middle boxes/end hosts 23505 * out there, we allow the option value to be the 23506 * same as the MSS option size on the peer side. 23507 * In this way, the other side will not send 23508 * anything larger than they can receive. 23509 * 23510 * Note that for SYN_SENT state, the ndd param 23511 * tcp_use_smss_as_mss_opt has no effect as we 23512 * don't know the peer's MSS option value. So 23513 * the only case we need to take care of is in 23514 * SYN_RCVD state, which is done later. 23515 */ 23516 wptr = mp1->b_wptr; 23517 wptr[0] = TCPOPT_MAXSEG; 23518 wptr[1] = TCPOPT_MAXSEG_LEN; 23519 wptr += 2; 23520 u1 = tcp->tcp_if_mtu - 23521 (tcp->tcp_ipversion == IPV4_VERSION ? 23522 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23523 TCP_MIN_HEADER_LENGTH; 23524 U16_TO_BE16(u1, wptr); 23525 mp1->b_wptr = wptr + 2; 23526 /* Update the offset to cover the additional word */ 23527 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23528 23529 /* 23530 * Note that the following way of filling in 23531 * TCP options are not optimal. Some NOPs can 23532 * be saved. But there is no need at this time 23533 * to optimize it. When it is needed, we will 23534 * do it. 23535 */ 23536 switch (tcp->tcp_state) { 23537 case TCPS_SYN_SENT: 23538 flags = TH_SYN; 23539 23540 if (tcp->tcp_snd_ts_ok) { 23541 uint32_t llbolt = (uint32_t)lbolt; 23542 23543 wptr = mp1->b_wptr; 23544 wptr[0] = TCPOPT_NOP; 23545 wptr[1] = TCPOPT_NOP; 23546 wptr[2] = TCPOPT_TSTAMP; 23547 wptr[3] = TCPOPT_TSTAMP_LEN; 23548 wptr += 4; 23549 U32_TO_BE32(llbolt, wptr); 23550 wptr += 4; 23551 ASSERT(tcp->tcp_ts_recent == 0); 23552 U32_TO_BE32(0L, wptr); 23553 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23554 tcph->th_offset_and_rsrvd[0] += 23555 (3 << 4); 23556 } 23557 23558 /* 23559 * Set up all the bits to tell other side 23560 * we are ECN capable. 23561 */ 23562 if (tcp->tcp_ecn_ok) { 23563 flags |= (TH_ECE | TH_CWR); 23564 } 23565 break; 23566 case TCPS_SYN_RCVD: 23567 flags |= TH_SYN; 23568 23569 /* 23570 * Reset the MSS option value to be SMSS 23571 * We should probably add back the bytes 23572 * for timestamp option and IPsec. We 23573 * don't do that as this is a workaround 23574 * for broken middle boxes/end hosts, it 23575 * is better for us to be more cautious. 23576 * They may not take these things into 23577 * account in their SMSS calculation. Thus 23578 * the peer's calculated SMSS may be smaller 23579 * than what it can be. This should be OK. 23580 */ 23581 if (tcps->tcps_use_smss_as_mss_opt) { 23582 u1 = tcp->tcp_mss; 23583 U16_TO_BE16(u1, wptr); 23584 } 23585 23586 /* 23587 * If the other side is ECN capable, reply 23588 * that we are also ECN capable. 23589 */ 23590 if (tcp->tcp_ecn_ok) 23591 flags |= TH_ECE; 23592 break; 23593 default: 23594 /* 23595 * The above ASSERT() makes sure that this 23596 * must be FIN-WAIT-1 state. Our SYN has 23597 * not been ack'ed so retransmit it. 23598 */ 23599 flags |= TH_SYN; 23600 break; 23601 } 23602 23603 if (tcp->tcp_snd_ws_ok) { 23604 wptr = mp1->b_wptr; 23605 wptr[0] = TCPOPT_NOP; 23606 wptr[1] = TCPOPT_WSCALE; 23607 wptr[2] = TCPOPT_WS_LEN; 23608 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23609 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23610 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23611 } 23612 23613 if (tcp->tcp_snd_sack_ok) { 23614 wptr = mp1->b_wptr; 23615 wptr[0] = TCPOPT_NOP; 23616 wptr[1] = TCPOPT_NOP; 23617 wptr[2] = TCPOPT_SACK_PERMITTED; 23618 wptr[3] = TCPOPT_SACK_OK_LEN; 23619 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23620 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23621 } 23622 23623 /* allocb() of adequate mblk assures space */ 23624 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23625 (uintptr_t)INT_MAX); 23626 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23627 /* 23628 * Get IP set to checksum on our behalf 23629 * Include the adjustment for a source route if any. 23630 */ 23631 u1 += tcp->tcp_sum; 23632 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23633 U16_TO_BE16(u1, tcph->th_sum); 23634 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23635 } 23636 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23637 (seq + data_length) == tcp->tcp_fss) { 23638 if (!tcp->tcp_fin_acked) { 23639 flags |= TH_FIN; 23640 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23641 } 23642 if (!tcp->tcp_fin_sent) { 23643 tcp->tcp_fin_sent = B_TRUE; 23644 switch (tcp->tcp_state) { 23645 case TCPS_SYN_RCVD: 23646 case TCPS_ESTABLISHED: 23647 tcp->tcp_state = TCPS_FIN_WAIT_1; 23648 break; 23649 case TCPS_CLOSE_WAIT: 23650 tcp->tcp_state = TCPS_LAST_ACK; 23651 break; 23652 } 23653 if (tcp->tcp_suna == tcp->tcp_snxt) 23654 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23655 tcp->tcp_snxt = tcp->tcp_fss + 1; 23656 } 23657 } 23658 /* 23659 * Note the trick here. u1 is unsigned. When tcp_urg 23660 * is smaller than seq, u1 will become a very huge value. 23661 * So the comparison will fail. Also note that tcp_urp 23662 * should be positive, see RFC 793 page 17. 23663 */ 23664 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23665 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23666 u1 < (uint32_t)(64 * 1024)) { 23667 flags |= TH_URG; 23668 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23669 U32_TO_ABE16(u1, tcph->th_urp); 23670 } 23671 } 23672 tcph->th_flags[0] = (uchar_t)flags; 23673 tcp->tcp_rack = tcp->tcp_rnxt; 23674 tcp->tcp_rack_cnt = 0; 23675 23676 if (tcp->tcp_snd_ts_ok) { 23677 if (tcp->tcp_state != TCPS_SYN_SENT) { 23678 uint32_t llbolt = (uint32_t)lbolt; 23679 23680 U32_TO_BE32(llbolt, 23681 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23682 U32_TO_BE32(tcp->tcp_ts_recent, 23683 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23684 } 23685 } 23686 23687 if (num_sack_blk > 0) { 23688 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23689 sack_blk_t *tmp; 23690 int32_t i; 23691 23692 wptr[0] = TCPOPT_NOP; 23693 wptr[1] = TCPOPT_NOP; 23694 wptr[2] = TCPOPT_SACK; 23695 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23696 sizeof (sack_blk_t); 23697 wptr += TCPOPT_REAL_SACK_LEN; 23698 23699 tmp = tcp->tcp_sack_list; 23700 for (i = 0; i < num_sack_blk; i++) { 23701 U32_TO_BE32(tmp[i].begin, wptr); 23702 wptr += sizeof (tcp_seq); 23703 U32_TO_BE32(tmp[i].end, wptr); 23704 wptr += sizeof (tcp_seq); 23705 } 23706 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23707 } 23708 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23709 data_length += (int)(mp1->b_wptr - rptr); 23710 if (tcp->tcp_ipversion == IPV4_VERSION) { 23711 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23712 } else { 23713 ip6_t *ip6 = (ip6_t *)(rptr + 23714 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23715 sizeof (ip6i_t) : 0)); 23716 23717 ip6->ip6_plen = htons(data_length - 23718 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23719 } 23720 23721 /* 23722 * Prime pump for IP 23723 * Include the adjustment for a source route if any. 23724 */ 23725 data_length -= tcp->tcp_ip_hdr_len; 23726 data_length += tcp->tcp_sum; 23727 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23728 U16_TO_ABE16(data_length, tcph->th_sum); 23729 if (tcp->tcp_ip_forward_progress) { 23730 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23731 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23732 tcp->tcp_ip_forward_progress = B_FALSE; 23733 } 23734 return (mp1); 23735 } 23736 23737 /* This function handles the push timeout. */ 23738 void 23739 tcp_push_timer(void *arg) 23740 { 23741 conn_t *connp = (conn_t *)arg; 23742 tcp_t *tcp = connp->conn_tcp; 23743 tcp_stack_t *tcps = tcp->tcp_tcps; 23744 uint_t flags; 23745 sodirect_t *sodp; 23746 23747 TCP_DBGSTAT(tcps, tcp_push_timer_cnt); 23748 23749 ASSERT(tcp->tcp_listener == NULL); 23750 23751 /* 23752 * We need to plug synchronous streams during our drain to prevent 23753 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23754 */ 23755 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23756 tcp->tcp_push_tid = 0; 23757 23758 SOD_PTR_ENTER(tcp, sodp); 23759 if (sodp != NULL) { 23760 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23761 /* sod_wakeup() does the mutex_exit() */ 23762 } else if (tcp->tcp_rcv_list != NULL) { 23763 flags = tcp_rcv_drain(tcp->tcp_rq, tcp); 23764 } 23765 if (flags == TH_ACK_NEEDED) 23766 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23767 23768 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23769 } 23770 23771 /* 23772 * This function handles delayed ACK timeout. 23773 */ 23774 static void 23775 tcp_ack_timer(void *arg) 23776 { 23777 conn_t *connp = (conn_t *)arg; 23778 tcp_t *tcp = connp->conn_tcp; 23779 mblk_t *mp; 23780 tcp_stack_t *tcps = tcp->tcp_tcps; 23781 23782 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23783 23784 tcp->tcp_ack_tid = 0; 23785 23786 if (tcp->tcp_fused) 23787 return; 23788 23789 /* 23790 * Do not send ACK if there is no outstanding unack'ed data. 23791 */ 23792 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23793 return; 23794 } 23795 23796 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23797 /* 23798 * Make sure we don't allow deferred ACKs to result in 23799 * timer-based ACKing. If we have held off an ACK 23800 * when there was more than an mss here, and the timer 23801 * goes off, we have to worry about the possibility 23802 * that the sender isn't doing slow-start, or is out 23803 * of step with us for some other reason. We fall 23804 * permanently back in the direction of 23805 * ACK-every-other-packet as suggested in RFC 1122. 23806 */ 23807 if (tcp->tcp_rack_abs_max > 2) 23808 tcp->tcp_rack_abs_max--; 23809 tcp->tcp_rack_cur_max = 2; 23810 } 23811 mp = tcp_ack_mp(tcp); 23812 23813 if (mp != NULL) { 23814 BUMP_LOCAL(tcp->tcp_obsegs); 23815 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23816 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23817 tcp_send_data(tcp, tcp->tcp_wq, mp); 23818 } 23819 } 23820 23821 23822 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23823 static mblk_t * 23824 tcp_ack_mp(tcp_t *tcp) 23825 { 23826 uint32_t seq_no; 23827 tcp_stack_t *tcps = tcp->tcp_tcps; 23828 23829 /* 23830 * There are a few cases to be considered while setting the sequence no. 23831 * Essentially, we can come here while processing an unacceptable pkt 23832 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23833 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23834 * If we are here for a zero window probe, stick with suna. In all 23835 * other cases, we check if suna + swnd encompasses snxt and set 23836 * the sequence number to snxt, if so. If snxt falls outside the 23837 * window (the receiver probably shrunk its window), we will go with 23838 * suna + swnd, otherwise the sequence no will be unacceptable to the 23839 * receiver. 23840 */ 23841 if (tcp->tcp_zero_win_probe) { 23842 seq_no = tcp->tcp_suna; 23843 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23844 ASSERT(tcp->tcp_swnd == 0); 23845 seq_no = tcp->tcp_snxt; 23846 } else { 23847 seq_no = SEQ_GT(tcp->tcp_snxt, 23848 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23849 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23850 } 23851 23852 if (tcp->tcp_valid_bits) { 23853 /* 23854 * For the complex case where we have to send some 23855 * controls (FIN or SYN), let tcp_xmit_mp do it. 23856 */ 23857 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23858 NULL, B_FALSE)); 23859 } else { 23860 /* Generate a simple ACK */ 23861 int data_length; 23862 uchar_t *rptr; 23863 tcph_t *tcph; 23864 mblk_t *mp1; 23865 int32_t tcp_hdr_len; 23866 int32_t tcp_tcp_hdr_len; 23867 int32_t num_sack_blk = 0; 23868 int32_t sack_opt_len; 23869 23870 /* 23871 * Allocate space for TCP + IP headers 23872 * and link-level header 23873 */ 23874 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23875 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23876 tcp->tcp_num_sack_blk); 23877 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23878 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23879 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23880 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23881 } else { 23882 tcp_hdr_len = tcp->tcp_hdr_len; 23883 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23884 } 23885 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23886 if (!mp1) 23887 return (NULL); 23888 23889 /* Update the latest receive window size in TCP header. */ 23890 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23891 tcp->tcp_tcph->th_win); 23892 /* copy in prototype TCP + IP header */ 23893 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23894 mp1->b_rptr = rptr; 23895 mp1->b_wptr = rptr + tcp_hdr_len; 23896 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23897 23898 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23899 23900 /* Set the TCP sequence number. */ 23901 U32_TO_ABE32(seq_no, tcph->th_seq); 23902 23903 /* Set up the TCP flag field. */ 23904 tcph->th_flags[0] = (uchar_t)TH_ACK; 23905 if (tcp->tcp_ecn_echo_on) 23906 tcph->th_flags[0] |= TH_ECE; 23907 23908 tcp->tcp_rack = tcp->tcp_rnxt; 23909 tcp->tcp_rack_cnt = 0; 23910 23911 /* fill in timestamp option if in use */ 23912 if (tcp->tcp_snd_ts_ok) { 23913 uint32_t llbolt = (uint32_t)lbolt; 23914 23915 U32_TO_BE32(llbolt, 23916 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23917 U32_TO_BE32(tcp->tcp_ts_recent, 23918 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23919 } 23920 23921 /* Fill in SACK options */ 23922 if (num_sack_blk > 0) { 23923 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23924 sack_blk_t *tmp; 23925 int32_t i; 23926 23927 wptr[0] = TCPOPT_NOP; 23928 wptr[1] = TCPOPT_NOP; 23929 wptr[2] = TCPOPT_SACK; 23930 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23931 sizeof (sack_blk_t); 23932 wptr += TCPOPT_REAL_SACK_LEN; 23933 23934 tmp = tcp->tcp_sack_list; 23935 for (i = 0; i < num_sack_blk; i++) { 23936 U32_TO_BE32(tmp[i].begin, wptr); 23937 wptr += sizeof (tcp_seq); 23938 U32_TO_BE32(tmp[i].end, wptr); 23939 wptr += sizeof (tcp_seq); 23940 } 23941 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23942 << 4); 23943 } 23944 23945 if (tcp->tcp_ipversion == IPV4_VERSION) { 23946 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23947 } else { 23948 /* Check for ip6i_t header in sticky hdrs */ 23949 ip6_t *ip6 = (ip6_t *)(rptr + 23950 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23951 sizeof (ip6i_t) : 0)); 23952 23953 ip6->ip6_plen = htons(tcp_hdr_len - 23954 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23955 } 23956 23957 /* 23958 * Prime pump for checksum calculation in IP. Include the 23959 * adjustment for a source route if any. 23960 */ 23961 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23962 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23963 U16_TO_ABE16(data_length, tcph->th_sum); 23964 23965 if (tcp->tcp_ip_forward_progress) { 23966 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23967 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23968 tcp->tcp_ip_forward_progress = B_FALSE; 23969 } 23970 return (mp1); 23971 } 23972 } 23973 23974 /* 23975 * Hash list insertion routine for tcp_t structures. 23976 * Inserts entries with the ones bound to a specific IP address first 23977 * followed by those bound to INADDR_ANY. 23978 */ 23979 static void 23980 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23981 { 23982 tcp_t **tcpp; 23983 tcp_t *tcpnext; 23984 23985 if (tcp->tcp_ptpbhn != NULL) { 23986 ASSERT(!caller_holds_lock); 23987 tcp_bind_hash_remove(tcp); 23988 } 23989 tcpp = &tbf->tf_tcp; 23990 if (!caller_holds_lock) { 23991 mutex_enter(&tbf->tf_lock); 23992 } else { 23993 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23994 } 23995 tcpnext = tcpp[0]; 23996 if (tcpnext) { 23997 /* 23998 * If the new tcp bound to the INADDR_ANY address 23999 * and the first one in the list is not bound to 24000 * INADDR_ANY we skip all entries until we find the 24001 * first one bound to INADDR_ANY. 24002 * This makes sure that applications binding to a 24003 * specific address get preference over those binding to 24004 * INADDR_ANY. 24005 */ 24006 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 24007 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 24008 while ((tcpnext = tcpp[0]) != NULL && 24009 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24010 tcpp = &(tcpnext->tcp_bind_hash); 24011 if (tcpnext) 24012 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24013 } else 24014 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 24015 } 24016 tcp->tcp_bind_hash = tcpnext; 24017 tcp->tcp_ptpbhn = tcpp; 24018 tcpp[0] = tcp; 24019 if (!caller_holds_lock) 24020 mutex_exit(&tbf->tf_lock); 24021 } 24022 24023 /* 24024 * Hash list removal routine for tcp_t structures. 24025 */ 24026 static void 24027 tcp_bind_hash_remove(tcp_t *tcp) 24028 { 24029 tcp_t *tcpnext; 24030 kmutex_t *lockp; 24031 tcp_stack_t *tcps = tcp->tcp_tcps; 24032 24033 if (tcp->tcp_ptpbhn == NULL) 24034 return; 24035 24036 /* 24037 * Extract the lock pointer in case there are concurrent 24038 * hash_remove's for this instance. 24039 */ 24040 ASSERT(tcp->tcp_lport != 0); 24041 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24042 24043 ASSERT(lockp != NULL); 24044 mutex_enter(lockp); 24045 if (tcp->tcp_ptpbhn) { 24046 tcpnext = tcp->tcp_bind_hash; 24047 if (tcpnext) { 24048 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24049 tcp->tcp_bind_hash = NULL; 24050 } 24051 *tcp->tcp_ptpbhn = tcpnext; 24052 tcp->tcp_ptpbhn = NULL; 24053 } 24054 mutex_exit(lockp); 24055 } 24056 24057 24058 /* 24059 * Hash list lookup routine for tcp_t structures. 24060 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24061 */ 24062 static tcp_t * 24063 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24064 { 24065 tf_t *tf; 24066 tcp_t *tcp; 24067 24068 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24069 mutex_enter(&tf->tf_lock); 24070 for (tcp = tf->tf_tcp; tcp != NULL; 24071 tcp = tcp->tcp_acceptor_hash) { 24072 if (tcp->tcp_acceptor_id == id) { 24073 CONN_INC_REF(tcp->tcp_connp); 24074 mutex_exit(&tf->tf_lock); 24075 return (tcp); 24076 } 24077 } 24078 mutex_exit(&tf->tf_lock); 24079 return (NULL); 24080 } 24081 24082 24083 /* 24084 * Hash list insertion routine for tcp_t structures. 24085 */ 24086 void 24087 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24088 { 24089 tf_t *tf; 24090 tcp_t **tcpp; 24091 tcp_t *tcpnext; 24092 tcp_stack_t *tcps = tcp->tcp_tcps; 24093 24094 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24095 24096 if (tcp->tcp_ptpahn != NULL) 24097 tcp_acceptor_hash_remove(tcp); 24098 tcpp = &tf->tf_tcp; 24099 mutex_enter(&tf->tf_lock); 24100 tcpnext = tcpp[0]; 24101 if (tcpnext) 24102 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24103 tcp->tcp_acceptor_hash = tcpnext; 24104 tcp->tcp_ptpahn = tcpp; 24105 tcpp[0] = tcp; 24106 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24107 mutex_exit(&tf->tf_lock); 24108 } 24109 24110 /* 24111 * Hash list removal routine for tcp_t structures. 24112 */ 24113 static void 24114 tcp_acceptor_hash_remove(tcp_t *tcp) 24115 { 24116 tcp_t *tcpnext; 24117 kmutex_t *lockp; 24118 24119 /* 24120 * Extract the lock pointer in case there are concurrent 24121 * hash_remove's for this instance. 24122 */ 24123 lockp = tcp->tcp_acceptor_lockp; 24124 24125 if (tcp->tcp_ptpahn == NULL) 24126 return; 24127 24128 ASSERT(lockp != NULL); 24129 mutex_enter(lockp); 24130 if (tcp->tcp_ptpahn) { 24131 tcpnext = tcp->tcp_acceptor_hash; 24132 if (tcpnext) { 24133 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24134 tcp->tcp_acceptor_hash = NULL; 24135 } 24136 *tcp->tcp_ptpahn = tcpnext; 24137 tcp->tcp_ptpahn = NULL; 24138 } 24139 mutex_exit(lockp); 24140 tcp->tcp_acceptor_lockp = NULL; 24141 } 24142 24143 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24144 24145 static ipaddr_t netmasks[] = { 24146 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24147 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24148 }; 24149 24150 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24151 24152 /* 24153 * XXX This routine should go away and instead we should use the metrics 24154 * associated with the routes to determine the default sndspace and rcvspace. 24155 */ 24156 static tcp_hsp_t * 24157 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24158 { 24159 tcp_hsp_t *hsp = NULL; 24160 24161 /* Quick check without acquiring the lock. */ 24162 if (tcps->tcps_hsp_hash == NULL) 24163 return (NULL); 24164 24165 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24166 24167 /* This routine finds the best-matching HSP for address addr. */ 24168 24169 if (tcps->tcps_hsp_hash) { 24170 int i; 24171 ipaddr_t srchaddr; 24172 tcp_hsp_t *hsp_net; 24173 24174 /* We do three passes: host, network, and subnet. */ 24175 24176 srchaddr = addr; 24177 24178 for (i = 1; i <= 3; i++) { 24179 /* Look for exact match on srchaddr */ 24180 24181 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24182 while (hsp) { 24183 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24184 hsp->tcp_hsp_addr == srchaddr) 24185 break; 24186 hsp = hsp->tcp_hsp_next; 24187 } 24188 ASSERT(hsp == NULL || 24189 hsp->tcp_hsp_vers == IPV4_VERSION); 24190 24191 /* 24192 * If this is the first pass: 24193 * If we found a match, great, return it. 24194 * If not, search for the network on the second pass. 24195 */ 24196 24197 if (i == 1) 24198 if (hsp) 24199 break; 24200 else 24201 { 24202 srchaddr = addr & netmask(addr); 24203 continue; 24204 } 24205 24206 /* 24207 * If this is the second pass: 24208 * If we found a match, but there's a subnet mask, 24209 * save the match but try again using the subnet 24210 * mask on the third pass. 24211 * Otherwise, return whatever we found. 24212 */ 24213 24214 if (i == 2) { 24215 if (hsp && hsp->tcp_hsp_subnet) { 24216 hsp_net = hsp; 24217 srchaddr = addr & hsp->tcp_hsp_subnet; 24218 continue; 24219 } else { 24220 break; 24221 } 24222 } 24223 24224 /* 24225 * This must be the third pass. If we didn't find 24226 * anything, return the saved network HSP instead. 24227 */ 24228 24229 if (!hsp) 24230 hsp = hsp_net; 24231 } 24232 } 24233 24234 rw_exit(&tcps->tcps_hsp_lock); 24235 return (hsp); 24236 } 24237 24238 /* 24239 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24240 * match lookup. 24241 */ 24242 static tcp_hsp_t * 24243 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24244 { 24245 tcp_hsp_t *hsp = NULL; 24246 24247 /* Quick check without acquiring the lock. */ 24248 if (tcps->tcps_hsp_hash == NULL) 24249 return (NULL); 24250 24251 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24252 24253 /* This routine finds the best-matching HSP for address addr. */ 24254 24255 if (tcps->tcps_hsp_hash) { 24256 int i; 24257 in6_addr_t v6srchaddr; 24258 tcp_hsp_t *hsp_net; 24259 24260 /* We do three passes: host, network, and subnet. */ 24261 24262 v6srchaddr = *v6addr; 24263 24264 for (i = 1; i <= 3; i++) { 24265 /* Look for exact match on srchaddr */ 24266 24267 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24268 V4_PART_OF_V6(v6srchaddr))]; 24269 while (hsp) { 24270 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24271 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24272 &v6srchaddr)) 24273 break; 24274 hsp = hsp->tcp_hsp_next; 24275 } 24276 24277 /* 24278 * If this is the first pass: 24279 * If we found a match, great, return it. 24280 * If not, search for the network on the second pass. 24281 */ 24282 24283 if (i == 1) 24284 if (hsp) 24285 break; 24286 else { 24287 /* Assume a 64 bit mask */ 24288 v6srchaddr.s6_addr32[0] = 24289 v6addr->s6_addr32[0]; 24290 v6srchaddr.s6_addr32[1] = 24291 v6addr->s6_addr32[1]; 24292 v6srchaddr.s6_addr32[2] = 0; 24293 v6srchaddr.s6_addr32[3] = 0; 24294 continue; 24295 } 24296 24297 /* 24298 * If this is the second pass: 24299 * If we found a match, but there's a subnet mask, 24300 * save the match but try again using the subnet 24301 * mask on the third pass. 24302 * Otherwise, return whatever we found. 24303 */ 24304 24305 if (i == 2) { 24306 ASSERT(hsp == NULL || 24307 hsp->tcp_hsp_vers == IPV6_VERSION); 24308 if (hsp && 24309 !IN6_IS_ADDR_UNSPECIFIED( 24310 &hsp->tcp_hsp_subnet_v6)) { 24311 hsp_net = hsp; 24312 V6_MASK_COPY(*v6addr, 24313 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24314 continue; 24315 } else { 24316 break; 24317 } 24318 } 24319 24320 /* 24321 * This must be the third pass. If we didn't find 24322 * anything, return the saved network HSP instead. 24323 */ 24324 24325 if (!hsp) 24326 hsp = hsp_net; 24327 } 24328 } 24329 24330 rw_exit(&tcps->tcps_hsp_lock); 24331 return (hsp); 24332 } 24333 24334 /* 24335 * Type three generator adapted from the random() function in 4.4 BSD: 24336 */ 24337 24338 /* 24339 * Copyright (c) 1983, 1993 24340 * The Regents of the University of California. All rights reserved. 24341 * 24342 * Redistribution and use in source and binary forms, with or without 24343 * modification, are permitted provided that the following conditions 24344 * are met: 24345 * 1. Redistributions of source code must retain the above copyright 24346 * notice, this list of conditions and the following disclaimer. 24347 * 2. Redistributions in binary form must reproduce the above copyright 24348 * notice, this list of conditions and the following disclaimer in the 24349 * documentation and/or other materials provided with the distribution. 24350 * 3. All advertising materials mentioning features or use of this software 24351 * must display the following acknowledgement: 24352 * This product includes software developed by the University of 24353 * California, Berkeley and its contributors. 24354 * 4. Neither the name of the University nor the names of its contributors 24355 * may be used to endorse or promote products derived from this software 24356 * without specific prior written permission. 24357 * 24358 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24359 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24360 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24361 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24362 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24363 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24364 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24365 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24366 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24367 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24368 * SUCH DAMAGE. 24369 */ 24370 24371 /* Type 3 -- x**31 + x**3 + 1 */ 24372 #define DEG_3 31 24373 #define SEP_3 3 24374 24375 24376 /* Protected by tcp_random_lock */ 24377 static int tcp_randtbl[DEG_3 + 1]; 24378 24379 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24380 static int *tcp_random_rptr = &tcp_randtbl[1]; 24381 24382 static int *tcp_random_state = &tcp_randtbl[1]; 24383 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24384 24385 kmutex_t tcp_random_lock; 24386 24387 void 24388 tcp_random_init(void) 24389 { 24390 int i; 24391 hrtime_t hrt; 24392 time_t wallclock; 24393 uint64_t result; 24394 24395 /* 24396 * Use high-res timer and current time for seed. Gethrtime() returns 24397 * a longlong, which may contain resolution down to nanoseconds. 24398 * The current time will either be a 32-bit or a 64-bit quantity. 24399 * XOR the two together in a 64-bit result variable. 24400 * Convert the result to a 32-bit value by multiplying the high-order 24401 * 32-bits by the low-order 32-bits. 24402 */ 24403 24404 hrt = gethrtime(); 24405 (void) drv_getparm(TIME, &wallclock); 24406 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24407 mutex_enter(&tcp_random_lock); 24408 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24409 (result & 0xffffffff); 24410 24411 for (i = 1; i < DEG_3; i++) 24412 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24413 + 12345; 24414 tcp_random_fptr = &tcp_random_state[SEP_3]; 24415 tcp_random_rptr = &tcp_random_state[0]; 24416 mutex_exit(&tcp_random_lock); 24417 for (i = 0; i < 10 * DEG_3; i++) 24418 (void) tcp_random(); 24419 } 24420 24421 /* 24422 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24423 * This range is selected to be approximately centered on TCP_ISS / 2, 24424 * and easy to compute. We get this value by generating a 32-bit random 24425 * number, selecting out the high-order 17 bits, and then adding one so 24426 * that we never return zero. 24427 */ 24428 int 24429 tcp_random(void) 24430 { 24431 int i; 24432 24433 mutex_enter(&tcp_random_lock); 24434 *tcp_random_fptr += *tcp_random_rptr; 24435 24436 /* 24437 * The high-order bits are more random than the low-order bits, 24438 * so we select out the high-order 17 bits and add one so that 24439 * we never return zero. 24440 */ 24441 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24442 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24443 tcp_random_fptr = tcp_random_state; 24444 ++tcp_random_rptr; 24445 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24446 tcp_random_rptr = tcp_random_state; 24447 24448 mutex_exit(&tcp_random_lock); 24449 return (i); 24450 } 24451 24452 /* 24453 * XXX This will go away when TPI is extended to send 24454 * info reqs to sockfs/timod ..... 24455 * Given a queue, set the max packet size for the write 24456 * side of the queue below stream head. This value is 24457 * cached on the stream head. 24458 * Returns 1 on success, 0 otherwise. 24459 */ 24460 static int 24461 setmaxps(queue_t *q, int maxpsz) 24462 { 24463 struct stdata *stp; 24464 queue_t *wq; 24465 stp = STREAM(q); 24466 24467 /* 24468 * At this point change of a queue parameter is not allowed 24469 * when a multiplexor is sitting on top. 24470 */ 24471 if (stp->sd_flag & STPLEX) 24472 return (0); 24473 24474 claimstr(stp->sd_wrq); 24475 wq = stp->sd_wrq->q_next; 24476 ASSERT(wq != NULL); 24477 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 24478 releasestr(stp->sd_wrq); 24479 return (1); 24480 } 24481 24482 static int 24483 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24484 int *t_errorp, int *sys_errorp) 24485 { 24486 int error; 24487 int is_absreq_failure; 24488 t_scalar_t *opt_lenp; 24489 t_scalar_t opt_offset; 24490 int prim_type; 24491 struct T_conn_req *tcreqp; 24492 struct T_conn_res *tcresp; 24493 cred_t *cr; 24494 24495 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24496 24497 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24498 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24499 prim_type == T_CONN_RES); 24500 24501 switch (prim_type) { 24502 case T_CONN_REQ: 24503 tcreqp = (struct T_conn_req *)mp->b_rptr; 24504 opt_offset = tcreqp->OPT_offset; 24505 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24506 break; 24507 case O_T_CONN_RES: 24508 case T_CONN_RES: 24509 tcresp = (struct T_conn_res *)mp->b_rptr; 24510 opt_offset = tcresp->OPT_offset; 24511 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24512 break; 24513 } 24514 24515 *t_errorp = 0; 24516 *sys_errorp = 0; 24517 *do_disconnectp = 0; 24518 24519 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24520 opt_offset, cr, &tcp_opt_obj, 24521 NULL, &is_absreq_failure); 24522 24523 switch (error) { 24524 case 0: /* no error */ 24525 ASSERT(is_absreq_failure == 0); 24526 return (0); 24527 case ENOPROTOOPT: 24528 *t_errorp = TBADOPT; 24529 break; 24530 case EACCES: 24531 *t_errorp = TACCES; 24532 break; 24533 default: 24534 *t_errorp = TSYSERR; *sys_errorp = error; 24535 break; 24536 } 24537 if (is_absreq_failure != 0) { 24538 /* 24539 * The connection request should get the local ack 24540 * T_OK_ACK and then a T_DISCON_IND. 24541 */ 24542 *do_disconnectp = 1; 24543 } 24544 return (-1); 24545 } 24546 24547 /* 24548 * Split this function out so that if the secret changes, I'm okay. 24549 * 24550 * Initialize the tcp_iss_cookie and tcp_iss_key. 24551 */ 24552 24553 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24554 24555 static void 24556 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24557 { 24558 struct { 24559 int32_t current_time; 24560 uint32_t randnum; 24561 uint16_t pad; 24562 uint8_t ether[6]; 24563 uint8_t passwd[PASSWD_SIZE]; 24564 } tcp_iss_cookie; 24565 time_t t; 24566 24567 /* 24568 * Start with the current absolute time. 24569 */ 24570 (void) drv_getparm(TIME, &t); 24571 tcp_iss_cookie.current_time = t; 24572 24573 /* 24574 * XXX - Need a more random number per RFC 1750, not this crap. 24575 * OTOH, if what follows is pretty random, then I'm in better shape. 24576 */ 24577 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24578 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24579 24580 /* 24581 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24582 * as a good template. 24583 */ 24584 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24585 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24586 24587 /* 24588 * The pass-phrase. Normally this is supplied by user-called NDD. 24589 */ 24590 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24591 24592 /* 24593 * See 4010593 if this section becomes a problem again, 24594 * but the local ethernet address is useful here. 24595 */ 24596 (void) localetheraddr(NULL, 24597 (struct ether_addr *)&tcp_iss_cookie.ether); 24598 24599 /* 24600 * Hash 'em all together. The MD5Final is called per-connection. 24601 */ 24602 mutex_enter(&tcps->tcps_iss_key_lock); 24603 MD5Init(&tcps->tcps_iss_key); 24604 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24605 sizeof (tcp_iss_cookie)); 24606 mutex_exit(&tcps->tcps_iss_key_lock); 24607 } 24608 24609 /* 24610 * Set the RFC 1948 pass phrase 24611 */ 24612 /* ARGSUSED */ 24613 static int 24614 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24615 cred_t *cr) 24616 { 24617 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24618 24619 /* 24620 * Basically, value contains a new pass phrase. Pass it along! 24621 */ 24622 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24623 return (0); 24624 } 24625 24626 /* ARGSUSED */ 24627 static int 24628 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24629 { 24630 bzero(buf, sizeof (tcp_sack_info_t)); 24631 return (0); 24632 } 24633 24634 /* ARGSUSED */ 24635 static int 24636 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24637 { 24638 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24639 return (0); 24640 } 24641 24642 /* 24643 * Make sure we wait until the default queue is setup, yet allow 24644 * tcp_g_q_create() to open a TCP stream. 24645 * We need to allow tcp_g_q_create() do do an open 24646 * of tcp, hence we compare curhread. 24647 * All others have to wait until the tcps_g_q has been 24648 * setup. 24649 */ 24650 void 24651 tcp_g_q_setup(tcp_stack_t *tcps) 24652 { 24653 mutex_enter(&tcps->tcps_g_q_lock); 24654 if (tcps->tcps_g_q != NULL) { 24655 mutex_exit(&tcps->tcps_g_q_lock); 24656 return; 24657 } 24658 if (tcps->tcps_g_q_creator == NULL) { 24659 /* This thread will set it up */ 24660 tcps->tcps_g_q_creator = curthread; 24661 mutex_exit(&tcps->tcps_g_q_lock); 24662 tcp_g_q_create(tcps); 24663 mutex_enter(&tcps->tcps_g_q_lock); 24664 ASSERT(tcps->tcps_g_q_creator == curthread); 24665 tcps->tcps_g_q_creator = NULL; 24666 cv_signal(&tcps->tcps_g_q_cv); 24667 ASSERT(tcps->tcps_g_q != NULL); 24668 mutex_exit(&tcps->tcps_g_q_lock); 24669 return; 24670 } 24671 /* Everybody but the creator has to wait */ 24672 if (tcps->tcps_g_q_creator != curthread) { 24673 while (tcps->tcps_g_q == NULL) 24674 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24675 } 24676 mutex_exit(&tcps->tcps_g_q_lock); 24677 } 24678 24679 #define IP "ip" 24680 24681 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24682 24683 /* 24684 * Create a default tcp queue here instead of in strplumb 24685 */ 24686 void 24687 tcp_g_q_create(tcp_stack_t *tcps) 24688 { 24689 int error; 24690 ldi_handle_t lh = NULL; 24691 ldi_ident_t li = NULL; 24692 int rval; 24693 cred_t *cr; 24694 major_t IP_MAJ; 24695 24696 #ifdef NS_DEBUG 24697 (void) printf("tcp_g_q_create()\n"); 24698 #endif 24699 24700 IP_MAJ = ddi_name_to_major(IP); 24701 24702 ASSERT(tcps->tcps_g_q_creator == curthread); 24703 24704 error = ldi_ident_from_major(IP_MAJ, &li); 24705 if (error) { 24706 #ifdef DEBUG 24707 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24708 error); 24709 #endif 24710 return; 24711 } 24712 24713 cr = zone_get_kcred(netstackid_to_zoneid( 24714 tcps->tcps_netstack->netstack_stackid)); 24715 ASSERT(cr != NULL); 24716 /* 24717 * We set the tcp default queue to IPv6 because IPv4 falls 24718 * back to IPv6 when it can't find a client, but 24719 * IPv6 does not fall back to IPv4. 24720 */ 24721 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24722 if (error) { 24723 #ifdef DEBUG 24724 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24725 error); 24726 #endif 24727 goto out; 24728 } 24729 24730 /* 24731 * This ioctl causes the tcp framework to cache a pointer to 24732 * this stream, so we don't want to close the stream after 24733 * this operation. 24734 * Use the kernel credentials that are for the zone we're in. 24735 */ 24736 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24737 (intptr_t)0, FKIOCTL, cr, &rval); 24738 if (error) { 24739 #ifdef DEBUG 24740 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24741 "error %d\n", error); 24742 #endif 24743 goto out; 24744 } 24745 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24746 lh = NULL; 24747 out: 24748 /* Close layered handles */ 24749 if (li) 24750 ldi_ident_release(li); 24751 /* Keep cred around until _inactive needs it */ 24752 tcps->tcps_g_q_cr = cr; 24753 } 24754 24755 /* 24756 * We keep tcp_g_q set until all other tcp_t's in the zone 24757 * has gone away, and then when tcp_g_q_inactive() is called 24758 * we clear it. 24759 */ 24760 void 24761 tcp_g_q_destroy(tcp_stack_t *tcps) 24762 { 24763 #ifdef NS_DEBUG 24764 (void) printf("tcp_g_q_destroy()for stack %d\n", 24765 tcps->tcps_netstack->netstack_stackid); 24766 #endif 24767 24768 if (tcps->tcps_g_q == NULL) { 24769 return; /* Nothing to cleanup */ 24770 } 24771 /* 24772 * Drop reference corresponding to the default queue. 24773 * This reference was added from tcp_open when the default queue 24774 * was created, hence we compensate for this extra drop in 24775 * tcp_g_q_close. If the refcnt drops to zero here it means 24776 * the default queue was the last one to be open, in which 24777 * case, then tcp_g_q_inactive will be 24778 * called as a result of the refrele. 24779 */ 24780 TCPS_REFRELE(tcps); 24781 } 24782 24783 /* 24784 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24785 * Run by tcp_q_q_inactive using a taskq. 24786 */ 24787 static void 24788 tcp_g_q_close(void *arg) 24789 { 24790 tcp_stack_t *tcps = arg; 24791 int error; 24792 ldi_handle_t lh = NULL; 24793 ldi_ident_t li = NULL; 24794 cred_t *cr; 24795 major_t IP_MAJ; 24796 24797 IP_MAJ = ddi_name_to_major(IP); 24798 24799 #ifdef NS_DEBUG 24800 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24801 tcps->tcps_netstack->netstack_stackid, 24802 tcps->tcps_netstack->netstack_refcnt); 24803 #endif 24804 lh = tcps->tcps_g_q_lh; 24805 if (lh == NULL) 24806 return; /* Nothing to cleanup */ 24807 24808 ASSERT(tcps->tcps_refcnt == 1); 24809 ASSERT(tcps->tcps_g_q != NULL); 24810 24811 error = ldi_ident_from_major(IP_MAJ, &li); 24812 if (error) { 24813 #ifdef DEBUG 24814 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24815 error); 24816 #endif 24817 return; 24818 } 24819 24820 cr = tcps->tcps_g_q_cr; 24821 tcps->tcps_g_q_cr = NULL; 24822 ASSERT(cr != NULL); 24823 24824 /* 24825 * Make sure we can break the recursion when tcp_close decrements 24826 * the reference count causing g_q_inactive to be called again. 24827 */ 24828 tcps->tcps_g_q_lh = NULL; 24829 24830 /* close the default queue */ 24831 (void) ldi_close(lh, FREAD|FWRITE, cr); 24832 /* 24833 * At this point in time tcps and the rest of netstack_t might 24834 * have been deleted. 24835 */ 24836 tcps = NULL; 24837 24838 /* Close layered handles */ 24839 ldi_ident_release(li); 24840 crfree(cr); 24841 } 24842 24843 /* 24844 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24845 * 24846 * Have to ensure that the ldi routines are not used by an 24847 * interrupt thread by using a taskq. 24848 */ 24849 void 24850 tcp_g_q_inactive(tcp_stack_t *tcps) 24851 { 24852 if (tcps->tcps_g_q_lh == NULL) 24853 return; /* Nothing to cleanup */ 24854 24855 ASSERT(tcps->tcps_refcnt == 0); 24856 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24857 24858 if (servicing_interrupt()) { 24859 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24860 (void *) tcps, TQ_SLEEP); 24861 } else { 24862 tcp_g_q_close(tcps); 24863 } 24864 } 24865 24866 /* 24867 * Called by IP when IP is loaded into the kernel 24868 */ 24869 void 24870 tcp_ddi_g_init(void) 24871 { 24872 tcp_timercache = kmem_cache_create("tcp_timercache", 24873 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24874 NULL, NULL, NULL, NULL, NULL, 0); 24875 24876 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24877 sizeof (tcp_sack_info_t), 0, 24878 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24879 24880 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24881 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24882 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24883 24884 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24885 24886 /* Initialize the random number generator */ 24887 tcp_random_init(); 24888 24889 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 24890 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 24891 24892 /* A single callback independently of how many netstacks we have */ 24893 ip_squeue_init(tcp_squeue_add); 24894 24895 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24896 24897 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24898 TASKQ_PREPOPULATE); 24899 24900 /* 24901 * We want to be informed each time a stack is created or 24902 * destroyed in the kernel, so we can maintain the 24903 * set of tcp_stack_t's. 24904 */ 24905 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24906 tcp_stack_fini); 24907 } 24908 24909 24910 /* 24911 * Initialize the TCP stack instance. 24912 */ 24913 static void * 24914 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24915 { 24916 tcp_stack_t *tcps; 24917 tcpparam_t *pa; 24918 int i; 24919 24920 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24921 tcps->tcps_netstack = ns; 24922 24923 /* Initialize locks */ 24924 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24925 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24926 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24927 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24928 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24929 24930 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24931 tcps->tcps_g_epriv_ports[0] = 2049; 24932 tcps->tcps_g_epriv_ports[1] = 4045; 24933 tcps->tcps_min_anonpriv_port = 512; 24934 24935 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24936 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24937 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24938 TCP_FANOUT_SIZE, KM_SLEEP); 24939 24940 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24941 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24942 MUTEX_DEFAULT, NULL); 24943 } 24944 24945 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24946 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24947 MUTEX_DEFAULT, NULL); 24948 } 24949 24950 /* TCP's IPsec code calls the packet dropper. */ 24951 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24952 24953 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24954 tcps->tcps_params = pa; 24955 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24956 24957 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24958 A_CNT(lcl_tcp_param_arr), tcps); 24959 24960 /* 24961 * Note: To really walk the device tree you need the devinfo 24962 * pointer to your device which is only available after probe/attach. 24963 * The following is safe only because it uses ddi_root_node() 24964 */ 24965 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24966 tcp_opt_obj.odb_opt_arr_cnt); 24967 24968 /* 24969 * Initialize RFC 1948 secret values. This will probably be reset once 24970 * by the boot scripts. 24971 * 24972 * Use NULL name, as the name is caught by the new lockstats. 24973 * 24974 * Initialize with some random, non-guessable string, like the global 24975 * T_INFO_ACK. 24976 */ 24977 24978 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24979 sizeof (tcp_g_t_info_ack), tcps); 24980 24981 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24982 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24983 24984 return (tcps); 24985 } 24986 24987 /* 24988 * Called when the IP module is about to be unloaded. 24989 */ 24990 void 24991 tcp_ddi_g_destroy(void) 24992 { 24993 tcp_g_kstat_fini(tcp_g_kstat); 24994 tcp_g_kstat = NULL; 24995 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24996 24997 mutex_destroy(&tcp_random_lock); 24998 24999 kmem_cache_destroy(tcp_timercache); 25000 kmem_cache_destroy(tcp_sack_info_cache); 25001 kmem_cache_destroy(tcp_iphc_cache); 25002 25003 netstack_unregister(NS_TCP); 25004 taskq_destroy(tcp_taskq); 25005 } 25006 25007 /* 25008 * Shut down the TCP stack instance. 25009 */ 25010 /* ARGSUSED */ 25011 static void 25012 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25013 { 25014 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25015 25016 tcp_g_q_destroy(tcps); 25017 } 25018 25019 /* 25020 * Free the TCP stack instance. 25021 */ 25022 static void 25023 tcp_stack_fini(netstackid_t stackid, void *arg) 25024 { 25025 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25026 int i; 25027 25028 nd_free(&tcps->tcps_g_nd); 25029 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25030 tcps->tcps_params = NULL; 25031 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25032 tcps->tcps_wroff_xtra_param = NULL; 25033 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25034 tcps->tcps_mdt_head_param = NULL; 25035 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25036 tcps->tcps_mdt_tail_param = NULL; 25037 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25038 tcps->tcps_mdt_max_pbufs_param = NULL; 25039 25040 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25041 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25042 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25043 } 25044 25045 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25046 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25047 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25048 } 25049 25050 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25051 tcps->tcps_bind_fanout = NULL; 25052 25053 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25054 tcps->tcps_acceptor_fanout = NULL; 25055 25056 mutex_destroy(&tcps->tcps_iss_key_lock); 25057 rw_destroy(&tcps->tcps_hsp_lock); 25058 mutex_destroy(&tcps->tcps_g_q_lock); 25059 cv_destroy(&tcps->tcps_g_q_cv); 25060 mutex_destroy(&tcps->tcps_epriv_port_lock); 25061 25062 ip_drop_unregister(&tcps->tcps_dropper); 25063 25064 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25065 tcps->tcps_kstat = NULL; 25066 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25067 25068 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25069 tcps->tcps_mibkp = NULL; 25070 25071 kmem_free(tcps, sizeof (*tcps)); 25072 } 25073 25074 /* 25075 * Generate ISS, taking into account NDD changes may happen halfway through. 25076 * (If the iss is not zero, set it.) 25077 */ 25078 25079 static void 25080 tcp_iss_init(tcp_t *tcp) 25081 { 25082 MD5_CTX context; 25083 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25084 uint32_t answer[4]; 25085 tcp_stack_t *tcps = tcp->tcp_tcps; 25086 25087 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25088 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25089 switch (tcps->tcps_strong_iss) { 25090 case 2: 25091 mutex_enter(&tcps->tcps_iss_key_lock); 25092 context = tcps->tcps_iss_key; 25093 mutex_exit(&tcps->tcps_iss_key_lock); 25094 arg.ports = tcp->tcp_ports; 25095 if (tcp->tcp_ipversion == IPV4_VERSION) { 25096 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25097 &arg.src); 25098 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25099 &arg.dst); 25100 } else { 25101 arg.src = tcp->tcp_ip6h->ip6_src; 25102 arg.dst = tcp->tcp_ip6h->ip6_dst; 25103 } 25104 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25105 MD5Final((uchar_t *)answer, &context); 25106 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25107 /* 25108 * Now that we've hashed into a unique per-connection sequence 25109 * space, add a random increment per strong_iss == 1. So I 25110 * guess we'll have to... 25111 */ 25112 /* FALLTHRU */ 25113 case 1: 25114 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25115 break; 25116 default: 25117 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25118 break; 25119 } 25120 tcp->tcp_valid_bits = TCP_ISS_VALID; 25121 tcp->tcp_fss = tcp->tcp_iss - 1; 25122 tcp->tcp_suna = tcp->tcp_iss; 25123 tcp->tcp_snxt = tcp->tcp_iss + 1; 25124 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25125 tcp->tcp_csuna = tcp->tcp_snxt; 25126 } 25127 25128 /* 25129 * Exported routine for extracting active tcp connection status. 25130 * 25131 * This is used by the Solaris Cluster Networking software to 25132 * gather a list of connections that need to be forwarded to 25133 * specific nodes in the cluster when configuration changes occur. 25134 * 25135 * The callback is invoked for each tcp_t structure. Returning 25136 * non-zero from the callback routine terminates the search. 25137 */ 25138 int 25139 cl_tcp_walk_list(int (*cl_callback)(cl_tcp_info_t *, void *), 25140 void *arg) 25141 { 25142 netstack_handle_t nh; 25143 netstack_t *ns; 25144 int ret = 0; 25145 25146 netstack_next_init(&nh); 25147 while ((ns = netstack_next(&nh)) != NULL) { 25148 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25149 ns->netstack_tcp); 25150 netstack_rele(ns); 25151 } 25152 netstack_next_fini(&nh); 25153 return (ret); 25154 } 25155 25156 static int 25157 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25158 tcp_stack_t *tcps) 25159 { 25160 tcp_t *tcp; 25161 cl_tcp_info_t cl_tcpi; 25162 connf_t *connfp; 25163 conn_t *connp; 25164 int i; 25165 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25166 25167 ASSERT(callback != NULL); 25168 25169 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25170 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25171 connp = NULL; 25172 25173 while ((connp = 25174 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25175 25176 tcp = connp->conn_tcp; 25177 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25178 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25179 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25180 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25181 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25182 /* 25183 * The macros tcp_laddr and tcp_faddr give the IPv4 25184 * addresses. They are copied implicitly below as 25185 * mapped addresses. 25186 */ 25187 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25188 if (tcp->tcp_ipversion == IPV4_VERSION) { 25189 cl_tcpi.cl_tcpi_faddr = 25190 tcp->tcp_ipha->ipha_dst; 25191 } else { 25192 cl_tcpi.cl_tcpi_faddr_v6 = 25193 tcp->tcp_ip6h->ip6_dst; 25194 } 25195 25196 /* 25197 * If the callback returns non-zero 25198 * we terminate the traversal. 25199 */ 25200 if ((*callback)(&cl_tcpi, arg) != 0) { 25201 CONN_DEC_REF(tcp->tcp_connp); 25202 return (1); 25203 } 25204 } 25205 } 25206 25207 return (0); 25208 } 25209 25210 /* 25211 * Macros used for accessing the different types of sockaddr 25212 * structures inside a tcp_ioc_abort_conn_t. 25213 */ 25214 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25215 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25216 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25217 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25218 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25219 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25220 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25221 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25222 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25223 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25224 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25225 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25226 25227 /* 25228 * Return the correct error code to mimic the behavior 25229 * of a connection reset. 25230 */ 25231 #define TCP_AC_GET_ERRCODE(state, err) { \ 25232 switch ((state)) { \ 25233 case TCPS_SYN_SENT: \ 25234 case TCPS_SYN_RCVD: \ 25235 (err) = ECONNREFUSED; \ 25236 break; \ 25237 case TCPS_ESTABLISHED: \ 25238 case TCPS_FIN_WAIT_1: \ 25239 case TCPS_FIN_WAIT_2: \ 25240 case TCPS_CLOSE_WAIT: \ 25241 (err) = ECONNRESET; \ 25242 break; \ 25243 case TCPS_CLOSING: \ 25244 case TCPS_LAST_ACK: \ 25245 case TCPS_TIME_WAIT: \ 25246 (err) = 0; \ 25247 break; \ 25248 default: \ 25249 (err) = ENXIO; \ 25250 } \ 25251 } 25252 25253 /* 25254 * Check if a tcp structure matches the info in acp. 25255 */ 25256 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25257 (((acp)->ac_local.ss_family == AF_INET) ? \ 25258 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25259 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25260 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25261 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25262 (TCP_AC_V4LPORT((acp)) == 0 || \ 25263 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25264 (TCP_AC_V4RPORT((acp)) == 0 || \ 25265 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25266 (acp)->ac_start <= (tcp)->tcp_state && \ 25267 (acp)->ac_end >= (tcp)->tcp_state) : \ 25268 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25269 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25270 &(tcp)->tcp_ip_src_v6)) && \ 25271 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25272 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25273 &(tcp)->tcp_remote_v6)) && \ 25274 (TCP_AC_V6LPORT((acp)) == 0 || \ 25275 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25276 (TCP_AC_V6RPORT((acp)) == 0 || \ 25277 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25278 (acp)->ac_start <= (tcp)->tcp_state && \ 25279 (acp)->ac_end >= (tcp)->tcp_state)) 25280 25281 #define TCP_AC_MATCH(acp, tcp) \ 25282 (((acp)->ac_zoneid == ALL_ZONES || \ 25283 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25284 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25285 25286 /* 25287 * Build a message containing a tcp_ioc_abort_conn_t structure 25288 * which is filled in with information from acp and tp. 25289 */ 25290 static mblk_t * 25291 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25292 { 25293 mblk_t *mp; 25294 tcp_ioc_abort_conn_t *tacp; 25295 25296 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25297 if (mp == NULL) 25298 return (NULL); 25299 25300 mp->b_datap->db_type = M_CTL; 25301 25302 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25303 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25304 sizeof (uint32_t)); 25305 25306 tacp->ac_start = acp->ac_start; 25307 tacp->ac_end = acp->ac_end; 25308 tacp->ac_zoneid = acp->ac_zoneid; 25309 25310 if (acp->ac_local.ss_family == AF_INET) { 25311 tacp->ac_local.ss_family = AF_INET; 25312 tacp->ac_remote.ss_family = AF_INET; 25313 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25314 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25315 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25316 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25317 } else { 25318 tacp->ac_local.ss_family = AF_INET6; 25319 tacp->ac_remote.ss_family = AF_INET6; 25320 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25321 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25322 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25323 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25324 } 25325 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25326 return (mp); 25327 } 25328 25329 /* 25330 * Print a tcp_ioc_abort_conn_t structure. 25331 */ 25332 static void 25333 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25334 { 25335 char lbuf[128]; 25336 char rbuf[128]; 25337 sa_family_t af; 25338 in_port_t lport, rport; 25339 ushort_t logflags; 25340 25341 af = acp->ac_local.ss_family; 25342 25343 if (af == AF_INET) { 25344 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25345 lbuf, 128); 25346 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25347 rbuf, 128); 25348 lport = ntohs(TCP_AC_V4LPORT(acp)); 25349 rport = ntohs(TCP_AC_V4RPORT(acp)); 25350 } else { 25351 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25352 lbuf, 128); 25353 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25354 rbuf, 128); 25355 lport = ntohs(TCP_AC_V6LPORT(acp)); 25356 rport = ntohs(TCP_AC_V6RPORT(acp)); 25357 } 25358 25359 logflags = SL_TRACE | SL_NOTE; 25360 /* 25361 * Don't print this message to the console if the operation was done 25362 * to a non-global zone. 25363 */ 25364 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25365 logflags |= SL_CONSOLE; 25366 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25367 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25368 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25369 acp->ac_start, acp->ac_end); 25370 } 25371 25372 /* 25373 * Called inside tcp_rput when a message built using 25374 * tcp_ioctl_abort_build_msg is put into a queue. 25375 * Note that when we get here there is no wildcard in acp any more. 25376 */ 25377 static void 25378 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25379 { 25380 tcp_ioc_abort_conn_t *acp; 25381 25382 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25383 if (tcp->tcp_state <= acp->ac_end) { 25384 /* 25385 * If we get here, we are already on the correct 25386 * squeue. This ioctl follows the following path 25387 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25388 * ->tcp_ioctl_abort->squeue_fill (if on a 25389 * different squeue) 25390 */ 25391 int errcode; 25392 25393 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25394 (void) tcp_clean_death(tcp, errcode, 26); 25395 } 25396 freemsg(mp); 25397 } 25398 25399 /* 25400 * Abort all matching connections on a hash chain. 25401 */ 25402 static int 25403 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25404 boolean_t exact, tcp_stack_t *tcps) 25405 { 25406 int nmatch, err = 0; 25407 tcp_t *tcp; 25408 MBLKP mp, last, listhead = NULL; 25409 conn_t *tconnp; 25410 connf_t *connfp; 25411 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25412 25413 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25414 25415 startover: 25416 nmatch = 0; 25417 25418 mutex_enter(&connfp->connf_lock); 25419 for (tconnp = connfp->connf_head; tconnp != NULL; 25420 tconnp = tconnp->conn_next) { 25421 tcp = tconnp->conn_tcp; 25422 if (TCP_AC_MATCH(acp, tcp)) { 25423 CONN_INC_REF(tcp->tcp_connp); 25424 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25425 if (mp == NULL) { 25426 err = ENOMEM; 25427 CONN_DEC_REF(tcp->tcp_connp); 25428 break; 25429 } 25430 mp->b_prev = (mblk_t *)tcp; 25431 25432 if (listhead == NULL) { 25433 listhead = mp; 25434 last = mp; 25435 } else { 25436 last->b_next = mp; 25437 last = mp; 25438 } 25439 nmatch++; 25440 if (exact) 25441 break; 25442 } 25443 25444 /* Avoid holding lock for too long. */ 25445 if (nmatch >= 500) 25446 break; 25447 } 25448 mutex_exit(&connfp->connf_lock); 25449 25450 /* Pass mp into the correct tcp */ 25451 while ((mp = listhead) != NULL) { 25452 listhead = listhead->b_next; 25453 tcp = (tcp_t *)mp->b_prev; 25454 mp->b_next = mp->b_prev = NULL; 25455 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 25456 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 25457 } 25458 25459 *count += nmatch; 25460 if (nmatch >= 500 && err == 0) 25461 goto startover; 25462 return (err); 25463 } 25464 25465 /* 25466 * Abort all connections that matches the attributes specified in acp. 25467 */ 25468 static int 25469 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25470 { 25471 sa_family_t af; 25472 uint32_t ports; 25473 uint16_t *pports; 25474 int err = 0, count = 0; 25475 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25476 int index = -1; 25477 ushort_t logflags; 25478 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25479 25480 af = acp->ac_local.ss_family; 25481 25482 if (af == AF_INET) { 25483 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25484 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25485 pports = (uint16_t *)&ports; 25486 pports[1] = TCP_AC_V4LPORT(acp); 25487 pports[0] = TCP_AC_V4RPORT(acp); 25488 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25489 } 25490 } else { 25491 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25492 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25493 pports = (uint16_t *)&ports; 25494 pports[1] = TCP_AC_V6LPORT(acp); 25495 pports[0] = TCP_AC_V6RPORT(acp); 25496 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25497 } 25498 } 25499 25500 /* 25501 * For cases where remote addr, local port, and remote port are non- 25502 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25503 */ 25504 if (index != -1) { 25505 err = tcp_ioctl_abort_bucket(acp, index, 25506 &count, exact, tcps); 25507 } else { 25508 /* 25509 * loop through all entries for wildcard case 25510 */ 25511 for (index = 0; 25512 index < ipst->ips_ipcl_conn_fanout_size; 25513 index++) { 25514 err = tcp_ioctl_abort_bucket(acp, index, 25515 &count, exact, tcps); 25516 if (err != 0) 25517 break; 25518 } 25519 } 25520 25521 logflags = SL_TRACE | SL_NOTE; 25522 /* 25523 * Don't print this message to the console if the operation was done 25524 * to a non-global zone. 25525 */ 25526 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25527 logflags |= SL_CONSOLE; 25528 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25529 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25530 if (err == 0 && count == 0) 25531 err = ENOENT; 25532 return (err); 25533 } 25534 25535 /* 25536 * Process the TCP_IOC_ABORT_CONN ioctl request. 25537 */ 25538 static void 25539 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25540 { 25541 int err; 25542 IOCP iocp; 25543 MBLKP mp1; 25544 sa_family_t laf, raf; 25545 tcp_ioc_abort_conn_t *acp; 25546 zone_t *zptr; 25547 conn_t *connp = Q_TO_CONN(q); 25548 zoneid_t zoneid = connp->conn_zoneid; 25549 tcp_t *tcp = connp->conn_tcp; 25550 tcp_stack_t *tcps = tcp->tcp_tcps; 25551 25552 iocp = (IOCP)mp->b_rptr; 25553 25554 if ((mp1 = mp->b_cont) == NULL || 25555 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25556 err = EINVAL; 25557 goto out; 25558 } 25559 25560 /* check permissions */ 25561 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25562 err = EPERM; 25563 goto out; 25564 } 25565 25566 if (mp1->b_cont != NULL) { 25567 freemsg(mp1->b_cont); 25568 mp1->b_cont = NULL; 25569 } 25570 25571 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25572 laf = acp->ac_local.ss_family; 25573 raf = acp->ac_remote.ss_family; 25574 25575 /* check that a zone with the supplied zoneid exists */ 25576 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25577 zptr = zone_find_by_id(zoneid); 25578 if (zptr != NULL) { 25579 zone_rele(zptr); 25580 } else { 25581 err = EINVAL; 25582 goto out; 25583 } 25584 } 25585 25586 /* 25587 * For exclusive stacks we set the zoneid to zero 25588 * to make TCP operate as if in the global zone. 25589 */ 25590 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25591 acp->ac_zoneid = GLOBAL_ZONEID; 25592 25593 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25594 acp->ac_start > acp->ac_end || laf != raf || 25595 (laf != AF_INET && laf != AF_INET6)) { 25596 err = EINVAL; 25597 goto out; 25598 } 25599 25600 tcp_ioctl_abort_dump(acp); 25601 err = tcp_ioctl_abort(acp, tcps); 25602 25603 out: 25604 if (mp1 != NULL) { 25605 freemsg(mp1); 25606 mp->b_cont = NULL; 25607 } 25608 25609 if (err != 0) 25610 miocnak(q, mp, 0, err); 25611 else 25612 miocack(q, mp, 0, 0); 25613 } 25614 25615 /* 25616 * tcp_time_wait_processing() handles processing of incoming packets when 25617 * the tcp is in the TIME_WAIT state. 25618 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25619 * on the time wait list. 25620 */ 25621 void 25622 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25623 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25624 { 25625 int32_t bytes_acked; 25626 int32_t gap; 25627 int32_t rgap; 25628 tcp_opt_t tcpopt; 25629 uint_t flags; 25630 uint32_t new_swnd = 0; 25631 conn_t *connp; 25632 tcp_stack_t *tcps = tcp->tcp_tcps; 25633 25634 BUMP_LOCAL(tcp->tcp_ibsegs); 25635 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25636 25637 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25638 new_swnd = BE16_TO_U16(tcph->th_win) << 25639 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25640 if (tcp->tcp_snd_ts_ok) { 25641 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25642 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25643 tcp->tcp_rnxt, TH_ACK); 25644 goto done; 25645 } 25646 } 25647 gap = seg_seq - tcp->tcp_rnxt; 25648 rgap = tcp->tcp_rwnd - (gap + seg_len); 25649 if (gap < 0) { 25650 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25651 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25652 (seg_len > -gap ? -gap : seg_len)); 25653 seg_len += gap; 25654 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25655 if (flags & TH_RST) { 25656 goto done; 25657 } 25658 if ((flags & TH_FIN) && seg_len == -1) { 25659 /* 25660 * When TCP receives a duplicate FIN in 25661 * TIME_WAIT state, restart the 2 MSL timer. 25662 * See page 73 in RFC 793. Make sure this TCP 25663 * is already on the TIME_WAIT list. If not, 25664 * just restart the timer. 25665 */ 25666 if (TCP_IS_DETACHED(tcp)) { 25667 if (tcp_time_wait_remove(tcp, NULL) == 25668 B_TRUE) { 25669 tcp_time_wait_append(tcp); 25670 TCP_DBGSTAT(tcps, 25671 tcp_rput_time_wait); 25672 } 25673 } else { 25674 ASSERT(tcp != NULL); 25675 TCP_TIMER_RESTART(tcp, 25676 tcps->tcps_time_wait_interval); 25677 } 25678 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25679 tcp->tcp_rnxt, TH_ACK); 25680 goto done; 25681 } 25682 flags |= TH_ACK_NEEDED; 25683 seg_len = 0; 25684 goto process_ack; 25685 } 25686 25687 /* Fix seg_seq, and chew the gap off the front. */ 25688 seg_seq = tcp->tcp_rnxt; 25689 } 25690 25691 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25692 /* 25693 * Make sure that when we accept the connection, pick 25694 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25695 * old connection. 25696 * 25697 * The next ISS generated is equal to tcp_iss_incr_extra 25698 * + ISS_INCR/2 + other components depending on the 25699 * value of tcp_strong_iss. We pre-calculate the new 25700 * ISS here and compare with tcp_snxt to determine if 25701 * we need to make adjustment to tcp_iss_incr_extra. 25702 * 25703 * The above calculation is ugly and is a 25704 * waste of CPU cycles... 25705 */ 25706 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25707 int32_t adj; 25708 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25709 25710 switch (tcps->tcps_strong_iss) { 25711 case 2: { 25712 /* Add time and MD5 components. */ 25713 uint32_t answer[4]; 25714 struct { 25715 uint32_t ports; 25716 in6_addr_t src; 25717 in6_addr_t dst; 25718 } arg; 25719 MD5_CTX context; 25720 25721 mutex_enter(&tcps->tcps_iss_key_lock); 25722 context = tcps->tcps_iss_key; 25723 mutex_exit(&tcps->tcps_iss_key_lock); 25724 arg.ports = tcp->tcp_ports; 25725 /* We use MAPPED addresses in tcp_iss_init */ 25726 arg.src = tcp->tcp_ip_src_v6; 25727 if (tcp->tcp_ipversion == IPV4_VERSION) { 25728 IN6_IPADDR_TO_V4MAPPED( 25729 tcp->tcp_ipha->ipha_dst, 25730 &arg.dst); 25731 } else { 25732 arg.dst = 25733 tcp->tcp_ip6h->ip6_dst; 25734 } 25735 MD5Update(&context, (uchar_t *)&arg, 25736 sizeof (arg)); 25737 MD5Final((uchar_t *)answer, &context); 25738 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25739 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25740 break; 25741 } 25742 case 1: 25743 /* Add time component and min random (i.e. 1). */ 25744 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25745 break; 25746 default: 25747 /* Add only time component. */ 25748 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25749 break; 25750 } 25751 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25752 /* 25753 * New ISS not guaranteed to be ISS_INCR/2 25754 * ahead of the current tcp_snxt, so add the 25755 * difference to tcp_iss_incr_extra. 25756 */ 25757 tcps->tcps_iss_incr_extra += adj; 25758 } 25759 /* 25760 * If tcp_clean_death() can not perform the task now, 25761 * drop the SYN packet and let the other side re-xmit. 25762 * Otherwise pass the SYN packet back in, since the 25763 * old tcp state has been cleaned up or freed. 25764 */ 25765 if (tcp_clean_death(tcp, 0, 27) == -1) 25766 goto done; 25767 /* 25768 * We will come back to tcp_rput_data 25769 * on the global queue. Packets destined 25770 * for the global queue will be checked 25771 * with global policy. But the policy for 25772 * this packet has already been checked as 25773 * this was destined for the detached 25774 * connection. We need to bypass policy 25775 * check this time by attaching a dummy 25776 * ipsec_in with ipsec_in_dont_check set. 25777 */ 25778 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25779 if (connp != NULL) { 25780 TCP_STAT(tcps, tcp_time_wait_syn_success); 25781 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25782 return; 25783 } 25784 goto done; 25785 } 25786 25787 /* 25788 * rgap is the amount of stuff received out of window. A negative 25789 * value is the amount out of window. 25790 */ 25791 if (rgap < 0) { 25792 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25793 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25794 /* Fix seg_len and make sure there is something left. */ 25795 seg_len += rgap; 25796 if (seg_len <= 0) { 25797 if (flags & TH_RST) { 25798 goto done; 25799 } 25800 flags |= TH_ACK_NEEDED; 25801 seg_len = 0; 25802 goto process_ack; 25803 } 25804 } 25805 /* 25806 * Check whether we can update tcp_ts_recent. This test is 25807 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25808 * Extensions for High Performance: An Update", Internet Draft. 25809 */ 25810 if (tcp->tcp_snd_ts_ok && 25811 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25812 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25813 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25814 tcp->tcp_last_rcv_lbolt = lbolt64; 25815 } 25816 25817 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25818 /* Always ack out of order packets */ 25819 flags |= TH_ACK_NEEDED; 25820 seg_len = 0; 25821 } else if (seg_len > 0) { 25822 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25823 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25824 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25825 } 25826 if (flags & TH_RST) { 25827 (void) tcp_clean_death(tcp, 0, 28); 25828 goto done; 25829 } 25830 if (flags & TH_SYN) { 25831 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25832 TH_RST|TH_ACK); 25833 /* 25834 * Do not delete the TCP structure if it is in 25835 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25836 */ 25837 goto done; 25838 } 25839 process_ack: 25840 if (flags & TH_ACK) { 25841 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25842 if (bytes_acked <= 0) { 25843 if (bytes_acked == 0 && seg_len == 0 && 25844 new_swnd == tcp->tcp_swnd) 25845 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25846 } else { 25847 /* Acks something not sent */ 25848 flags |= TH_ACK_NEEDED; 25849 } 25850 } 25851 if (flags & TH_ACK_NEEDED) { 25852 /* 25853 * Time to send an ack for some reason. 25854 */ 25855 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25856 tcp->tcp_rnxt, TH_ACK); 25857 } 25858 done: 25859 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25860 DB_CKSUMSTART(mp) = 0; 25861 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25862 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25863 } 25864 freemsg(mp); 25865 } 25866 25867 /* 25868 * Allocate a T_SVR4_OPTMGMT_REQ. 25869 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 25870 * that tcp_rput_other can drop the acks. 25871 */ 25872 static mblk_t * 25873 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 25874 { 25875 mblk_t *mp; 25876 struct T_optmgmt_req *tor; 25877 struct opthdr *oh; 25878 uint_t size; 25879 char *optptr; 25880 25881 size = sizeof (*tor) + sizeof (*oh) + optlen; 25882 mp = allocb(size, BPRI_MED); 25883 if (mp == NULL) 25884 return (NULL); 25885 25886 mp->b_wptr += size; 25887 mp->b_datap->db_type = M_PROTO; 25888 tor = (struct T_optmgmt_req *)mp->b_rptr; 25889 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 25890 tor->MGMT_flags = T_NEGOTIATE; 25891 tor->OPT_length = sizeof (*oh) + optlen; 25892 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 25893 25894 oh = (struct opthdr *)&tor[1]; 25895 oh->level = level; 25896 oh->name = cmd; 25897 oh->len = optlen; 25898 if (optlen != 0) { 25899 optptr = (char *)&oh[1]; 25900 bcopy(opt, optptr, optlen); 25901 } 25902 return (mp); 25903 } 25904 25905 /* 25906 * TCP Timers Implementation. 25907 */ 25908 timeout_id_t 25909 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25910 { 25911 mblk_t *mp; 25912 tcp_timer_t *tcpt; 25913 tcp_t *tcp = connp->conn_tcp; 25914 tcp_stack_t *tcps = tcp->tcp_tcps; 25915 25916 ASSERT(connp->conn_sqp != NULL); 25917 25918 TCP_DBGSTAT(tcps, tcp_timeout_calls); 25919 25920 if (tcp->tcp_timercache == NULL) { 25921 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25922 } else { 25923 TCP_DBGSTAT(tcps, tcp_timeout_cached_alloc); 25924 mp = tcp->tcp_timercache; 25925 tcp->tcp_timercache = mp->b_next; 25926 mp->b_next = NULL; 25927 ASSERT(mp->b_wptr == NULL); 25928 } 25929 25930 CONN_INC_REF(connp); 25931 tcpt = (tcp_timer_t *)mp->b_rptr; 25932 tcpt->connp = connp; 25933 tcpt->tcpt_proc = f; 25934 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 25935 return ((timeout_id_t)mp); 25936 } 25937 25938 static void 25939 tcp_timer_callback(void *arg) 25940 { 25941 mblk_t *mp = (mblk_t *)arg; 25942 tcp_timer_t *tcpt; 25943 conn_t *connp; 25944 25945 tcpt = (tcp_timer_t *)mp->b_rptr; 25946 connp = tcpt->connp; 25947 squeue_fill(connp->conn_sqp, mp, 25948 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 25949 } 25950 25951 static void 25952 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25953 { 25954 tcp_timer_t *tcpt; 25955 conn_t *connp = (conn_t *)arg; 25956 tcp_t *tcp = connp->conn_tcp; 25957 25958 tcpt = (tcp_timer_t *)mp->b_rptr; 25959 ASSERT(connp == tcpt->connp); 25960 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25961 25962 /* 25963 * If the TCP has reached the closed state, don't proceed any 25964 * further. This TCP logically does not exist on the system. 25965 * tcpt_proc could for example access queues, that have already 25966 * been qprocoff'ed off. Also see comments at the start of tcp_input 25967 */ 25968 if (tcp->tcp_state != TCPS_CLOSED) { 25969 (*tcpt->tcpt_proc)(connp); 25970 } else { 25971 tcp->tcp_timer_tid = 0; 25972 } 25973 tcp_timer_free(connp->conn_tcp, mp); 25974 } 25975 25976 /* 25977 * There is potential race with untimeout and the handler firing at the same 25978 * time. The mblock may be freed by the handler while we are trying to use 25979 * it. But since both should execute on the same squeue, this race should not 25980 * occur. 25981 */ 25982 clock_t 25983 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25984 { 25985 mblk_t *mp = (mblk_t *)id; 25986 tcp_timer_t *tcpt; 25987 clock_t delta; 25988 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25989 25990 TCP_DBGSTAT(tcps, tcp_timeout_cancel_reqs); 25991 25992 if (mp == NULL) 25993 return (-1); 25994 25995 tcpt = (tcp_timer_t *)mp->b_rptr; 25996 ASSERT(tcpt->connp == connp); 25997 25998 delta = untimeout(tcpt->tcpt_tid); 25999 26000 if (delta >= 0) { 26001 TCP_DBGSTAT(tcps, tcp_timeout_canceled); 26002 tcp_timer_free(connp->conn_tcp, mp); 26003 CONN_DEC_REF(connp); 26004 } 26005 26006 return (delta); 26007 } 26008 26009 /* 26010 * Allocate space for the timer event. The allocation looks like mblk, but it is 26011 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26012 * 26013 * Dealing with failures: If we can't allocate from the timer cache we try 26014 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26015 * points to b_rptr. 26016 * If we can't allocate anything using allocb_tryhard(), we perform a last 26017 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26018 * save the actual allocation size in b_datap. 26019 */ 26020 mblk_t * 26021 tcp_timermp_alloc(int kmflags) 26022 { 26023 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26024 kmflags & ~KM_PANIC); 26025 26026 if (mp != NULL) { 26027 mp->b_next = mp->b_prev = NULL; 26028 mp->b_rptr = (uchar_t *)(&mp[1]); 26029 mp->b_wptr = NULL; 26030 mp->b_datap = NULL; 26031 mp->b_queue = NULL; 26032 mp->b_cont = NULL; 26033 } else if (kmflags & KM_PANIC) { 26034 /* 26035 * Failed to allocate memory for the timer. Try allocating from 26036 * dblock caches. 26037 */ 26038 /* ipclassifier calls this from a constructor - hence no tcps */ 26039 TCP_G_STAT(tcp_timermp_allocfail); 26040 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26041 if (mp == NULL) { 26042 size_t size = 0; 26043 /* 26044 * Memory is really low. Try tryhard allocation. 26045 * 26046 * ipclassifier calls this from a constructor - 26047 * hence no tcps 26048 */ 26049 TCP_G_STAT(tcp_timermp_allocdblfail); 26050 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26051 sizeof (tcp_timer_t), &size, kmflags); 26052 mp->b_rptr = (uchar_t *)(&mp[1]); 26053 mp->b_next = mp->b_prev = NULL; 26054 mp->b_wptr = (uchar_t *)-1; 26055 mp->b_datap = (dblk_t *)size; 26056 mp->b_queue = NULL; 26057 mp->b_cont = NULL; 26058 } 26059 ASSERT(mp->b_wptr != NULL); 26060 } 26061 /* ipclassifier calls this from a constructor - hence no tcps */ 26062 TCP_G_DBGSTAT(tcp_timermp_alloced); 26063 26064 return (mp); 26065 } 26066 26067 /* 26068 * Free per-tcp timer cache. 26069 * It can only contain entries from tcp_timercache. 26070 */ 26071 void 26072 tcp_timermp_free(tcp_t *tcp) 26073 { 26074 mblk_t *mp; 26075 26076 while ((mp = tcp->tcp_timercache) != NULL) { 26077 ASSERT(mp->b_wptr == NULL); 26078 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26079 kmem_cache_free(tcp_timercache, mp); 26080 } 26081 } 26082 26083 /* 26084 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26085 * events there already (currently at most two events are cached). 26086 * If the event is not allocated from the timer cache, free it right away. 26087 */ 26088 static void 26089 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26090 { 26091 mblk_t *mp1 = tcp->tcp_timercache; 26092 tcp_stack_t *tcps = tcp->tcp_tcps; 26093 26094 if (mp->b_wptr != NULL) { 26095 /* 26096 * This allocation is not from a timer cache, free it right 26097 * away. 26098 */ 26099 if (mp->b_wptr != (uchar_t *)-1) 26100 freeb(mp); 26101 else 26102 kmem_free(mp, (size_t)mp->b_datap); 26103 } else if (mp1 == NULL || mp1->b_next == NULL) { 26104 /* Cache this timer block for future allocations */ 26105 mp->b_rptr = (uchar_t *)(&mp[1]); 26106 mp->b_next = mp1; 26107 tcp->tcp_timercache = mp; 26108 } else { 26109 kmem_cache_free(tcp_timercache, mp); 26110 TCP_DBGSTAT(tcps, tcp_timermp_freed); 26111 } 26112 } 26113 26114 /* 26115 * End of TCP Timers implementation. 26116 */ 26117 26118 /* 26119 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26120 * on the specified backing STREAMS q. Note, the caller may make the 26121 * decision to call based on the tcp_t.tcp_flow_stopped value which 26122 * when check outside the q's lock is only an advisory check ... 26123 */ 26124 26125 void 26126 tcp_setqfull(tcp_t *tcp) 26127 { 26128 queue_t *q = tcp->tcp_wq; 26129 tcp_stack_t *tcps = tcp->tcp_tcps; 26130 26131 if (!(q->q_flag & QFULL)) { 26132 mutex_enter(QLOCK(q)); 26133 if (!(q->q_flag & QFULL)) { 26134 /* still need to set QFULL */ 26135 q->q_flag |= QFULL; 26136 tcp->tcp_flow_stopped = B_TRUE; 26137 mutex_exit(QLOCK(q)); 26138 TCP_STAT(tcps, tcp_flwctl_on); 26139 } else { 26140 mutex_exit(QLOCK(q)); 26141 } 26142 } 26143 } 26144 26145 void 26146 tcp_clrqfull(tcp_t *tcp) 26147 { 26148 queue_t *q = tcp->tcp_wq; 26149 26150 if (q->q_flag & QFULL) { 26151 mutex_enter(QLOCK(q)); 26152 if (q->q_flag & QFULL) { 26153 q->q_flag &= ~QFULL; 26154 tcp->tcp_flow_stopped = B_FALSE; 26155 mutex_exit(QLOCK(q)); 26156 if (q->q_flag & QWANTW) 26157 qbackenable(q, 0); 26158 } else { 26159 mutex_exit(QLOCK(q)); 26160 } 26161 } 26162 } 26163 26164 26165 /* 26166 * kstats related to squeues i.e. not per IP instance 26167 */ 26168 static void * 26169 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26170 { 26171 kstat_t *ksp; 26172 26173 tcp_g_stat_t template = { 26174 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26175 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26176 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26177 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26178 }; 26179 26180 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26181 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26182 KSTAT_FLAG_VIRTUAL); 26183 26184 if (ksp == NULL) 26185 return (NULL); 26186 26187 bcopy(&template, tcp_g_statp, sizeof (template)); 26188 ksp->ks_data = (void *)tcp_g_statp; 26189 26190 kstat_install(ksp); 26191 return (ksp); 26192 } 26193 26194 static void 26195 tcp_g_kstat_fini(kstat_t *ksp) 26196 { 26197 if (ksp != NULL) { 26198 kstat_delete(ksp); 26199 } 26200 } 26201 26202 26203 static void * 26204 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26205 { 26206 kstat_t *ksp; 26207 26208 tcp_stat_t template = { 26209 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26210 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26211 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26212 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26213 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26214 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26215 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26216 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26217 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26218 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26219 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26220 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26221 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26222 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26223 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26224 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26225 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26226 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26227 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26228 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26229 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26230 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26231 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26232 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26233 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26234 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26235 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26236 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26237 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26238 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26239 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26240 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26241 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26242 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26243 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26244 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26245 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26246 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26247 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26248 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26249 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26250 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26251 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26252 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26253 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26254 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26255 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26256 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26257 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26258 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26259 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26260 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26261 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26262 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26263 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26264 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26265 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26266 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26267 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26268 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26269 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26270 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26271 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26272 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26273 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26274 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26275 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26276 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26277 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26278 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26279 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26280 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26281 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26282 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26283 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26284 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26285 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26286 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26287 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26288 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26289 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26290 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26291 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26292 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26293 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26294 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26295 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26296 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26297 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26298 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26299 }; 26300 26301 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26302 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26303 KSTAT_FLAG_VIRTUAL, stackid); 26304 26305 if (ksp == NULL) 26306 return (NULL); 26307 26308 bcopy(&template, tcps_statisticsp, sizeof (template)); 26309 ksp->ks_data = (void *)tcps_statisticsp; 26310 ksp->ks_private = (void *)(uintptr_t)stackid; 26311 26312 kstat_install(ksp); 26313 return (ksp); 26314 } 26315 26316 static void 26317 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26318 { 26319 if (ksp != NULL) { 26320 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26321 kstat_delete_netstack(ksp, stackid); 26322 } 26323 } 26324 26325 /* 26326 * TCP Kstats implementation 26327 */ 26328 static void * 26329 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26330 { 26331 kstat_t *ksp; 26332 26333 tcp_named_kstat_t template = { 26334 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26335 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26336 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26337 { "maxConn", KSTAT_DATA_INT32, 0 }, 26338 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26339 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26340 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26341 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26342 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26343 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26344 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26345 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26346 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26347 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26348 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26349 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26350 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26351 { "outAck", KSTAT_DATA_UINT32, 0 }, 26352 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26353 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26354 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26355 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26356 { "outControl", KSTAT_DATA_UINT32, 0 }, 26357 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26358 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26359 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26360 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26361 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26362 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26363 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26364 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26365 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26366 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26367 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26368 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26369 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26370 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26371 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26372 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26373 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26374 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26375 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26376 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26377 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26378 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26379 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26380 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26381 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26382 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26383 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26384 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26385 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26386 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26387 }; 26388 26389 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26390 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26391 26392 if (ksp == NULL) 26393 return (NULL); 26394 26395 template.rtoAlgorithm.value.ui32 = 4; 26396 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26397 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26398 template.maxConn.value.i32 = -1; 26399 26400 bcopy(&template, ksp->ks_data, sizeof (template)); 26401 ksp->ks_update = tcp_kstat_update; 26402 ksp->ks_private = (void *)(uintptr_t)stackid; 26403 26404 kstat_install(ksp); 26405 return (ksp); 26406 } 26407 26408 static void 26409 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26410 { 26411 if (ksp != NULL) { 26412 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26413 kstat_delete_netstack(ksp, stackid); 26414 } 26415 } 26416 26417 static int 26418 tcp_kstat_update(kstat_t *kp, int rw) 26419 { 26420 tcp_named_kstat_t *tcpkp; 26421 tcp_t *tcp; 26422 connf_t *connfp; 26423 conn_t *connp; 26424 int i; 26425 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26426 netstack_t *ns; 26427 tcp_stack_t *tcps; 26428 ip_stack_t *ipst; 26429 26430 if ((kp == NULL) || (kp->ks_data == NULL)) 26431 return (EIO); 26432 26433 if (rw == KSTAT_WRITE) 26434 return (EACCES); 26435 26436 ns = netstack_find_by_stackid(stackid); 26437 if (ns == NULL) 26438 return (-1); 26439 tcps = ns->netstack_tcp; 26440 if (tcps == NULL) { 26441 netstack_rele(ns); 26442 return (-1); 26443 } 26444 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26445 26446 tcpkp->currEstab.value.ui32 = 0; 26447 26448 ipst = ns->netstack_ip; 26449 26450 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26451 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26452 connp = NULL; 26453 while ((connp = 26454 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26455 tcp = connp->conn_tcp; 26456 switch (tcp_snmp_state(tcp)) { 26457 case MIB2_TCP_established: 26458 case MIB2_TCP_closeWait: 26459 tcpkp->currEstab.value.ui32++; 26460 break; 26461 } 26462 } 26463 } 26464 26465 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26466 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26467 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26468 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26469 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26470 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26471 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26472 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26473 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26474 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26475 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26476 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26477 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26478 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26479 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26480 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26481 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26482 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26483 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26484 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26485 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26486 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26487 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26488 tcpkp->inDataInorderSegs.value.ui32 = 26489 tcps->tcps_mib.tcpInDataInorderSegs; 26490 tcpkp->inDataInorderBytes.value.ui32 = 26491 tcps->tcps_mib.tcpInDataInorderBytes; 26492 tcpkp->inDataUnorderSegs.value.ui32 = 26493 tcps->tcps_mib.tcpInDataUnorderSegs; 26494 tcpkp->inDataUnorderBytes.value.ui32 = 26495 tcps->tcps_mib.tcpInDataUnorderBytes; 26496 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26497 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26498 tcpkp->inDataPartDupSegs.value.ui32 = 26499 tcps->tcps_mib.tcpInDataPartDupSegs; 26500 tcpkp->inDataPartDupBytes.value.ui32 = 26501 tcps->tcps_mib.tcpInDataPartDupBytes; 26502 tcpkp->inDataPastWinSegs.value.ui32 = 26503 tcps->tcps_mib.tcpInDataPastWinSegs; 26504 tcpkp->inDataPastWinBytes.value.ui32 = 26505 tcps->tcps_mib.tcpInDataPastWinBytes; 26506 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26507 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26508 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26509 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26510 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26511 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26512 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26513 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26514 tcpkp->timKeepaliveProbe.value.ui32 = 26515 tcps->tcps_mib.tcpTimKeepaliveProbe; 26516 tcpkp->timKeepaliveDrop.value.ui32 = 26517 tcps->tcps_mib.tcpTimKeepaliveDrop; 26518 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26519 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26520 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26521 tcpkp->outSackRetransSegs.value.ui32 = 26522 tcps->tcps_mib.tcpOutSackRetransSegs; 26523 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26524 26525 netstack_rele(ns); 26526 return (0); 26527 } 26528 26529 void 26530 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26531 { 26532 uint16_t hdr_len; 26533 ipha_t *ipha; 26534 uint8_t *nexthdrp; 26535 tcph_t *tcph; 26536 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26537 26538 /* Already has an eager */ 26539 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26540 TCP_STAT(tcps, tcp_reinput_syn); 26541 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 26542 connp, SQTAG_TCP_REINPUT_EAGER); 26543 return; 26544 } 26545 26546 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26547 case IPV4_VERSION: 26548 ipha = (ipha_t *)mp->b_rptr; 26549 hdr_len = IPH_HDR_LENGTH(ipha); 26550 break; 26551 case IPV6_VERSION: 26552 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26553 &hdr_len, &nexthdrp)) { 26554 CONN_DEC_REF(connp); 26555 freemsg(mp); 26556 return; 26557 } 26558 break; 26559 } 26560 26561 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26562 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26563 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26564 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26565 } 26566 26567 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 26568 SQTAG_TCP_REINPUT); 26569 } 26570 26571 static squeue_func_t 26572 tcp_squeue_switch(int val) 26573 { 26574 squeue_func_t rval = squeue_fill; 26575 26576 switch (val) { 26577 case 1: 26578 rval = squeue_enter_nodrain; 26579 break; 26580 case 2: 26581 rval = squeue_enter; 26582 break; 26583 default: 26584 break; 26585 } 26586 return (rval); 26587 } 26588 26589 /* 26590 * This is called once for each squeue - globally for all stack 26591 * instances. 26592 */ 26593 static void 26594 tcp_squeue_add(squeue_t *sqp) 26595 { 26596 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26597 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26598 26599 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26600 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 26601 sqp, TCP_TIME_WAIT_DELAY); 26602 if (tcp_free_list_max_cnt == 0) { 26603 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26604 max_ncpus : boot_max_ncpus); 26605 26606 /* 26607 * Limit number of entries to 1% of availble memory / tcp_ncpus 26608 */ 26609 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26610 (tcp_ncpus * sizeof (tcp_t) * 100); 26611 } 26612 tcp_time_wait->tcp_free_list_cnt = 0; 26613 } 26614