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/socketvar.h> 62 #include <sys/sockio.h> 63 #include <sys/isa_defs.h> 64 #include <sys/md5.h> 65 #include <sys/random.h> 66 #include <sys/sodirect.h> 67 #include <sys/uio.h> 68 #include <sys/systm.h> 69 #include <netinet/in.h> 70 #include <netinet/tcp.h> 71 #include <netinet/ip6.h> 72 #include <netinet/icmp6.h> 73 #include <net/if.h> 74 #include <net/route.h> 75 #include <inet/ipsec_impl.h> 76 77 #include <inet/common.h> 78 #include <inet/ip.h> 79 #include <inet/ip_impl.h> 80 #include <inet/ip6.h> 81 #include <inet/ip_ndp.h> 82 #include <inet/proto_set.h> 83 #include <inet/mib2.h> 84 #include <inet/nd.h> 85 #include <inet/optcom.h> 86 #include <inet/snmpcom.h> 87 #include <inet/kstatcom.h> 88 #include <inet/tcp.h> 89 #include <inet/tcp_impl.h> 90 #include <net/pfkeyv2.h> 91 #include <inet/ipsec_info.h> 92 #include <inet/ipdrop.h> 93 94 #include <inet/ipclassifier.h> 95 #include <inet/ip_ire.h> 96 #include <inet/ip_ftable.h> 97 #include <inet/ip_if.h> 98 #include <inet/ipp_common.h> 99 #include <inet/ip_netinfo.h> 100 #include <sys/squeue_impl.h> 101 #include <sys/squeue.h> 102 #include <inet/kssl/ksslapi.h> 103 #include <sys/tsol/label.h> 104 #include <sys/tsol/tnet.h> 105 #include <rpc/pmap_prot.h> 106 #include <sys/callo.h> 107 108 /* 109 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 110 * 111 * (Read the detailed design doc in PSARC case directory) 112 * 113 * The entire tcp state is contained in tcp_t and conn_t structure 114 * which are allocated in tandem using ipcl_conn_create() and passing 115 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 116 * the references on the tcp_t. The tcp_t structure is never compressed 117 * and packets always land on the correct TCP perimeter from the time 118 * eager is created till the time tcp_t dies (as such the old mentat 119 * TCP global queue is not used for detached state and no IPSEC checking 120 * is required). The global queue is still allocated to send out resets 121 * for connection which have no listeners and IP directly calls 122 * tcp_xmit_listeners_reset() which does any policy check. 123 * 124 * Protection and Synchronisation mechanism: 125 * 126 * The tcp data structure does not use any kind of lock for protecting 127 * its state but instead uses 'squeues' for mutual exclusion from various 128 * read and write side threads. To access a tcp member, the thread should 129 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS, 130 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller 131 * can pass any tcp function having prototype of edesc_t as argument 132 * (different from traditional STREAMs model where packets come in only 133 * designated entry points). The list of functions that can be directly 134 * called via squeue are listed before the usual function prototype. 135 * 136 * Referencing: 137 * 138 * TCP is MT-Hot and we use a reference based scheme to make sure that the 139 * tcp structure doesn't disappear when its needed. When the application 140 * creates an outgoing connection or accepts an incoming connection, we 141 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 142 * The IP reference is just a symbolic reference since ip_tcpclose() 143 * looks at tcp structure after tcp_close_output() returns which could 144 * have dropped the last TCP reference. So as long as the connection is 145 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 146 * conn_t. The classifier puts its own reference when the connection is 147 * inserted in listen or connected hash. Anytime a thread needs to enter 148 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 149 * on write side or by doing a classify on read side and then puts a 150 * reference on the conn before doing squeue_enter/tryenter/fill. For 151 * read side, the classifier itself puts the reference under fanout lock 152 * to make sure that tcp can't disappear before it gets processed. The 153 * squeue will drop this reference automatically so the called function 154 * doesn't have to do a DEC_REF. 155 * 156 * Opening a new connection: 157 * 158 * The outgoing connection open is pretty simple. tcp_open() does the 159 * work in creating the conn/tcp structure and initializing it. The 160 * squeue assignment is done based on the CPU the application 161 * is running on. So for outbound connections, processing is always done 162 * on application CPU which might be different from the incoming CPU 163 * being interrupted by the NIC. An optimal way would be to figure out 164 * the NIC <-> CPU binding at listen time, and assign the outgoing 165 * connection to the squeue attached to the CPU that will be interrupted 166 * for incoming packets (we know the NIC based on the bind IP address). 167 * This might seem like a problem if more data is going out but the 168 * fact is that in most cases the transmit is ACK driven transmit where 169 * the outgoing data normally sits on TCP's xmit queue waiting to be 170 * transmitted. 171 * 172 * Accepting a connection: 173 * 174 * This is a more interesting case because of various races involved in 175 * establishing a eager in its own perimeter. Read the meta comment on 176 * top of tcp_conn_request(). But briefly, the squeue is picked by 177 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 178 * 179 * Closing a connection: 180 * 181 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 182 * via squeue to do the close and mark the tcp as detached if the connection 183 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 184 * reference but tcp_close() drop IP's reference always. So if tcp was 185 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 186 * and 1 because it is in classifier's connected hash. This is the condition 187 * we use to determine that its OK to clean up the tcp outside of squeue 188 * when time wait expires (check the ref under fanout and conn_lock and 189 * if it is 2, remove it from fanout hash and kill it). 190 * 191 * Although close just drops the necessary references and marks the 192 * tcp_detached state, tcp_close needs to know the tcp_detached has been 193 * set (under squeue) before letting the STREAM go away (because a 194 * inbound packet might attempt to go up the STREAM while the close 195 * has happened and tcp_detached is not set). So a special lock and 196 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 197 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 198 * tcp_detached. 199 * 200 * Special provisions and fast paths: 201 * 202 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 203 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 204 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 205 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 206 * check to send packets directly to tcp_rput_data via squeue. Everyone 207 * else comes through tcp_input() on the read side. 208 * 209 * We also make special provisions for sockfs by marking tcp_issocket 210 * whenever we have only sockfs on top of TCP. This allows us to skip 211 * putting the tcp in acceptor hash since a sockfs listener can never 212 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 213 * since eager has already been allocated and the accept now happens 214 * on acceptor STREAM. There is a big blob of comment on top of 215 * tcp_conn_request explaining the new accept. When socket is POP'd, 216 * sockfs sends us an ioctl to mark the fact and we go back to old 217 * behaviour. Once tcp_issocket is unset, its never set for the 218 * life of that connection. 219 * 220 * In support of on-board asynchronous DMA hardware (e.g. Intel I/OAT) 221 * two consoldiation private KAPIs are used to enqueue M_DATA mblk_t's 222 * directly to the socket (sodirect) and start an asynchronous copyout 223 * to a user-land receive-side buffer (uioa) when a blocking socket read 224 * (e.g. read, recv, ...) is pending. 225 * 226 * This is accomplished when tcp_issocket is set and tcp_sodirect is not 227 * NULL so points to an sodirect_t and if marked enabled then we enqueue 228 * all mblk_t's directly to the socket. 229 * 230 * Further, if the sodirect_t sod_uioa and if marked enabled (due to a 231 * blocking socket read, e.g. user-land read, recv, ...) then an asynchronous 232 * copyout will be started directly to the user-land uio buffer. Also, as we 233 * have a pending read, TCP's push logic can take into account the number of 234 * bytes to be received and only awake the blocked read()er when the uioa_t 235 * byte count has been satisfied. 236 * 237 * IPsec notes : 238 * 239 * Since a packet is always executed on the correct TCP perimeter 240 * all IPsec processing is defered to IP including checking new 241 * connections and setting IPSEC policies for new connection. The 242 * only exception is tcp_xmit_listeners_reset() which is called 243 * directly from IP and needs to policy check to see if TH_RST 244 * can be sent out. 245 * 246 * PFHooks notes : 247 * 248 * For mdt case, one meta buffer contains multiple packets. Mblks for every 249 * packet are assembled and passed to the hooks. When packets are blocked, 250 * or boundary of any packet is changed, the mdt processing is stopped, and 251 * packets of the meta buffer are send to the IP path one by one. 252 */ 253 254 /* 255 * Values for squeue switch: 256 * 1: SQ_NODRAIN 257 * 2: SQ_PROCESS 258 * 3: SQ_FILL 259 */ 260 int tcp_squeue_wput = 2; /* /etc/systems */ 261 int tcp_squeue_flag; 262 263 /* 264 * Macros for sodirect: 265 * 266 * SOD_PTR_ENTER(tcp, sodp) - for the tcp_t pointer "tcp" set the 267 * sodirect_t pointer "sodp" to the socket/tcp shared sodirect_t 268 * if it exists and is enabled, else to NULL. Note, in the current 269 * sodirect implementation the sod_lockp must not be held across any 270 * STREAMS call (e.g. putnext) else a "recursive mutex_enter" PANIC 271 * will result as sod_lockp is the streamhead stdata.sd_lock. 272 * 273 * SOD_NOT_ENABLED(tcp) - return true if not a sodirect tcp_t or the 274 * sodirect_t isn't enabled, usefull for ASSERT()ing that a recieve 275 * side tcp code path dealing with a tcp_rcv_list or putnext() isn't 276 * being used when sodirect code paths should be. 277 */ 278 279 #define SOD_PTR_ENTER(tcp, sodp) \ 280 (sodp) = (tcp)->tcp_sodirect; \ 281 \ 282 if ((sodp) != NULL) { \ 283 mutex_enter((sodp)->sod_lockp); \ 284 if (!((sodp)->sod_state & SOD_ENABLED)) { \ 285 mutex_exit((sodp)->sod_lockp); \ 286 (sodp) = NULL; \ 287 } \ 288 } 289 290 #define SOD_NOT_ENABLED(tcp) \ 291 ((tcp)->tcp_sodirect == NULL || \ 292 !((tcp)->tcp_sodirect->sod_state & SOD_ENABLED)) 293 294 /* 295 * This controls how tiny a write must be before we try to copy it 296 * into the the mblk on the tail of the transmit queue. Not much 297 * speedup is observed for values larger than sixteen. Zero will 298 * disable the optimisation. 299 */ 300 int tcp_tx_pull_len = 16; 301 302 /* 303 * TCP Statistics. 304 * 305 * How TCP statistics work. 306 * 307 * There are two types of statistics invoked by two macros. 308 * 309 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 310 * supposed to be used in non MT-hot paths of the code. 311 * 312 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 313 * supposed to be used for DEBUG purposes and may be used on a hot path. 314 * 315 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 316 * (use "kstat tcp" to get them). 317 * 318 * There is also additional debugging facility that marks tcp_clean_death() 319 * instances and saves them in tcp_t structure. It is triggered by 320 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 321 * tcp_clean_death() calls that counts the number of times each tag was hit. It 322 * is triggered by TCP_CLD_COUNTERS define. 323 * 324 * How to add new counters. 325 * 326 * 1) Add a field in the tcp_stat structure describing your counter. 327 * 2) Add a line in the template in tcp_kstat2_init() with the name 328 * of the counter. 329 * 330 * IMPORTANT!! - make sure that both are in sync !! 331 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 332 * 333 * Please avoid using private counters which are not kstat-exported. 334 * 335 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 336 * in tcp_t structure. 337 * 338 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 339 */ 340 341 #ifndef TCP_DEBUG_COUNTER 342 #ifdef DEBUG 343 #define TCP_DEBUG_COUNTER 1 344 #else 345 #define TCP_DEBUG_COUNTER 0 346 #endif 347 #endif 348 349 #define TCP_CLD_COUNTERS 0 350 351 #define TCP_TAG_CLEAN_DEATH 1 352 #define TCP_MAX_CLEAN_DEATH_TAG 32 353 354 #ifdef lint 355 static int _lint_dummy_; 356 #endif 357 358 #if TCP_CLD_COUNTERS 359 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 360 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 361 #elif defined(lint) 362 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 363 #else 364 #define TCP_CLD_STAT(x) 365 #endif 366 367 #if TCP_DEBUG_COUNTER 368 #define TCP_DBGSTAT(tcps, x) \ 369 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 370 #define TCP_G_DBGSTAT(x) \ 371 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 372 #elif defined(lint) 373 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 374 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 375 #else 376 #define TCP_DBGSTAT(tcps, x) 377 #define TCP_G_DBGSTAT(x) 378 #endif 379 380 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 381 382 tcp_g_stat_t tcp_g_statistics; 383 kstat_t *tcp_g_kstat; 384 385 /* 386 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 387 * tcp write side. 388 */ 389 #define CALL_IP_WPUT(connp, q, mp) { \ 390 ASSERT(((q)->q_flag & QREADR) == 0); \ 391 TCP_DBGSTAT(connp->conn_netstack->netstack_tcp, tcp_ip_output); \ 392 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 393 } 394 395 /* Macros for timestamp comparisons */ 396 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 397 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 398 399 /* 400 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 401 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 402 * by adding three components: a time component which grows by 1 every 4096 403 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 404 * a per-connection component which grows by 125000 for every new connection; 405 * and an "extra" component that grows by a random amount centered 406 * approximately on 64000. This causes the the ISS generator to cycle every 407 * 4.89 hours if no TCP connections are made, and faster if connections are 408 * made. 409 * 410 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 411 * components: a time component which grows by 250000 every second; and 412 * a per-connection component which grows by 125000 for every new connections. 413 * 414 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 415 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 416 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 417 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 418 * password. 419 */ 420 #define ISS_INCR 250000 421 #define ISS_NSEC_SHT 12 422 423 static sin_t sin_null; /* Zero address for quick clears */ 424 static sin6_t sin6_null; /* Zero address for quick clears */ 425 426 /* 427 * This implementation follows the 4.3BSD interpretation of the urgent 428 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 429 * incompatible changes in protocols like telnet and rlogin. 430 */ 431 #define TCP_OLD_URP_INTERPRETATION 1 432 433 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 434 (TCP_IS_DETACHED(tcp) && \ 435 (!(tcp)->tcp_hard_binding)) 436 437 /* 438 * TCP reassembly macros. We hide starting and ending sequence numbers in 439 * b_next and b_prev of messages on the reassembly queue. The messages are 440 * chained using b_cont. These macros are used in tcp_reass() so we don't 441 * have to see the ugly casts and assignments. 442 */ 443 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 444 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 445 (mblk_t *)(uintptr_t)(u)) 446 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 447 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 448 (mblk_t *)(uintptr_t)(u)) 449 450 /* 451 * Implementation of TCP Timers. 452 * ============================= 453 * 454 * INTERFACE: 455 * 456 * There are two basic functions dealing with tcp timers: 457 * 458 * timeout_id_t tcp_timeout(connp, func, time) 459 * clock_t tcp_timeout_cancel(connp, timeout_id) 460 * TCP_TIMER_RESTART(tcp, intvl) 461 * 462 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 463 * after 'time' ticks passed. The function called by timeout() must adhere to 464 * the same restrictions as a driver soft interrupt handler - it must not sleep 465 * or call other functions that might sleep. The value returned is the opaque 466 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 467 * cancel the request. The call to tcp_timeout() may fail in which case it 468 * returns zero. This is different from the timeout(9F) function which never 469 * fails. 470 * 471 * The call-back function 'func' always receives 'connp' as its single 472 * argument. It is always executed in the squeue corresponding to the tcp 473 * structure. The tcp structure is guaranteed to be present at the time the 474 * call-back is called. 475 * 476 * NOTE: The call-back function 'func' is never called if tcp is in 477 * the TCPS_CLOSED state. 478 * 479 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 480 * request. locks acquired by the call-back routine should not be held across 481 * the call to tcp_timeout_cancel() or a deadlock may result. 482 * 483 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 484 * Otherwise, it returns an integer value greater than or equal to 0. In 485 * particular, if the call-back function is already placed on the squeue, it can 486 * not be canceled. 487 * 488 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 489 * within squeue context corresponding to the tcp instance. Since the 490 * call-back is also called via the same squeue, there are no race 491 * conditions described in untimeout(9F) manual page since all calls are 492 * strictly serialized. 493 * 494 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 495 * stored in tcp_timer_tid and starts a new one using 496 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 497 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 498 * field. 499 * 500 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 501 * call-back may still be called, so it is possible tcp_timer() will be 502 * called several times. This should not be a problem since tcp_timer() 503 * should always check the tcp instance state. 504 * 505 * 506 * IMPLEMENTATION: 507 * 508 * TCP timers are implemented using three-stage process. The call to 509 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 510 * when the timer expires. The tcp_timer_callback() arranges the call of the 511 * tcp_timer_handler() function via squeue corresponding to the tcp 512 * instance. The tcp_timer_handler() calls actual requested timeout call-back 513 * and passes tcp instance as an argument to it. Information is passed between 514 * stages using the tcp_timer_t structure which contains the connp pointer, the 515 * tcp call-back to call and the timeout id returned by the timeout(9F). 516 * 517 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 518 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 519 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 520 * returns the pointer to this mblk. 521 * 522 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 523 * looks like a normal mblk without actual dblk attached to it. 524 * 525 * To optimize performance each tcp instance holds a small cache of timer 526 * mblocks. In the current implementation it caches up to two timer mblocks per 527 * tcp instance. The cache is preserved over tcp frees and is only freed when 528 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 529 * timer processing happens on a corresponding squeue, the cache manipulation 530 * does not require any locks. Experiments show that majority of timer mblocks 531 * allocations are satisfied from the tcp cache and do not involve kmem calls. 532 * 533 * The tcp_timeout() places a refhold on the connp instance which guarantees 534 * that it will be present at the time the call-back function fires. The 535 * tcp_timer_handler() drops the reference after calling the call-back, so the 536 * call-back function does not need to manipulate the references explicitly. 537 */ 538 539 typedef struct tcp_timer_s { 540 conn_t *connp; 541 void (*tcpt_proc)(void *); 542 callout_id_t tcpt_tid; 543 } tcp_timer_t; 544 545 static kmem_cache_t *tcp_timercache; 546 kmem_cache_t *tcp_sack_info_cache; 547 kmem_cache_t *tcp_iphc_cache; 548 549 /* 550 * For scalability, we must not run a timer for every TCP connection 551 * in TIME_WAIT state. To see why, consider (for time wait interval of 552 * 4 minutes): 553 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 554 * 555 * This list is ordered by time, so you need only delete from the head 556 * until you get to entries which aren't old enough to delete yet. 557 * The list consists of only the detached TIME_WAIT connections. 558 * 559 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 560 * becomes detached TIME_WAIT (either by changing the state and already 561 * being detached or the other way around). This means that the TIME_WAIT 562 * state can be extended (up to doubled) if the connection doesn't become 563 * detached for a long time. 564 * 565 * The list manipulations (including tcp_time_wait_next/prev) 566 * are protected by the tcp_time_wait_lock. The content of the 567 * detached TIME_WAIT connections is protected by the normal perimeters. 568 * 569 * This list is per squeue and squeues are shared across the tcp_stack_t's. 570 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 571 * and conn_netstack. 572 * The tcp_t's that are added to tcp_free_list are disassociated and 573 * have NULL tcp_tcps and conn_netstack pointers. 574 */ 575 typedef struct tcp_squeue_priv_s { 576 kmutex_t tcp_time_wait_lock; 577 callout_id_t tcp_time_wait_tid; 578 tcp_t *tcp_time_wait_head; 579 tcp_t *tcp_time_wait_tail; 580 tcp_t *tcp_free_list; 581 uint_t tcp_free_list_cnt; 582 } tcp_squeue_priv_t; 583 584 /* 585 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 586 * Running it every 5 seconds seems to give the best results. 587 */ 588 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 589 590 /* 591 * To prevent memory hog, limit the number of entries in tcp_free_list 592 * to 1% of available memory / number of cpus 593 */ 594 uint_t tcp_free_list_max_cnt = 0; 595 596 #define TCP_XMIT_LOWATER 4096 597 #define TCP_XMIT_HIWATER 49152 598 #define TCP_RECV_LOWATER 2048 599 #define TCP_RECV_HIWATER 49152 600 601 /* 602 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 603 */ 604 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 605 606 #define TIDUSZ 4096 /* transport interface data unit size */ 607 608 /* 609 * Bind hash list size and has function. It has to be a power of 2 for 610 * hashing. 611 */ 612 #define TCP_BIND_FANOUT_SIZE 512 613 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 614 /* 615 * Size of listen and acceptor hash list. It has to be a power of 2 for 616 * hashing. 617 */ 618 #define TCP_FANOUT_SIZE 256 619 620 #ifdef _ILP32 621 #define TCP_ACCEPTOR_HASH(accid) \ 622 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 623 #else 624 #define TCP_ACCEPTOR_HASH(accid) \ 625 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 626 #endif /* _ILP32 */ 627 628 #define IP_ADDR_CACHE_SIZE 2048 629 #define IP_ADDR_CACHE_HASH(faddr) \ 630 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 631 632 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 633 #define TCP_HSP_HASH_SIZE 256 634 635 #define TCP_HSP_HASH(addr) \ 636 (((addr>>24) ^ (addr >>16) ^ \ 637 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 638 639 /* 640 * TCP options struct returned from tcp_parse_options. 641 */ 642 typedef struct tcp_opt_s { 643 uint32_t tcp_opt_mss; 644 uint32_t tcp_opt_wscale; 645 uint32_t tcp_opt_ts_val; 646 uint32_t tcp_opt_ts_ecr; 647 tcp_t *tcp; 648 } tcp_opt_t; 649 650 /* 651 * TCP option struct passing information b/w lisenter and eager. 652 */ 653 struct tcp_options { 654 uint_t to_flags; 655 ssize_t to_boundif; /* IPV6_BOUND_IF */ 656 sock_upper_handle_t to_handle; 657 }; 658 659 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 660 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 661 #define TCPOPT_UPPERHANDLE 0x00000004 /* set upper handle */ 662 663 /* 664 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 665 */ 666 667 #ifdef _BIG_ENDIAN 668 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 669 (TCPOPT_TSTAMP << 8) | 10) 670 #else 671 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 672 (TCPOPT_NOP << 8) | TCPOPT_NOP) 673 #endif 674 675 /* 676 * Flags returned from tcp_parse_options. 677 */ 678 #define TCP_OPT_MSS_PRESENT 1 679 #define TCP_OPT_WSCALE_PRESENT 2 680 #define TCP_OPT_TSTAMP_PRESENT 4 681 #define TCP_OPT_SACK_OK_PRESENT 8 682 #define TCP_OPT_SACK_PRESENT 16 683 684 /* TCP option length */ 685 #define TCPOPT_NOP_LEN 1 686 #define TCPOPT_MAXSEG_LEN 4 687 #define TCPOPT_WS_LEN 3 688 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 689 #define TCPOPT_TSTAMP_LEN 10 690 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 691 #define TCPOPT_SACK_OK_LEN 2 692 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 693 #define TCPOPT_REAL_SACK_LEN 4 694 #define TCPOPT_MAX_SACK_LEN 36 695 #define TCPOPT_HEADER_LEN 2 696 697 /* TCP cwnd burst factor. */ 698 #define TCP_CWND_INFINITE 65535 699 #define TCP_CWND_SS 3 700 #define TCP_CWND_NORMAL 5 701 702 /* Maximum TCP initial cwin (start/restart). */ 703 #define TCP_MAX_INIT_CWND 8 704 705 /* 706 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 707 * either tcp_slow_start_initial or tcp_slow_start_after idle 708 * depending on the caller. If the upper layer has not used the 709 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 710 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 711 * If the upper layer has changed set the tcp_init_cwnd, just use 712 * it to calculate the tcp_cwnd. 713 */ 714 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 715 { \ 716 if ((tcp)->tcp_init_cwnd == 0) { \ 717 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 718 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 719 } else { \ 720 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 721 } \ 722 tcp->tcp_cwnd_cnt = 0; \ 723 } 724 725 /* TCP Timer control structure */ 726 typedef struct tcpt_s { 727 pfv_t tcpt_pfv; /* The routine we are to call */ 728 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 729 } tcpt_t; 730 731 /* Host Specific Parameter structure */ 732 typedef struct tcp_hsp { 733 struct tcp_hsp *tcp_hsp_next; 734 in6_addr_t tcp_hsp_addr_v6; 735 in6_addr_t tcp_hsp_subnet_v6; 736 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 737 int32_t tcp_hsp_sendspace; 738 int32_t tcp_hsp_recvspace; 739 int32_t tcp_hsp_tstamp; 740 } tcp_hsp_t; 741 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 742 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 743 744 /* 745 * Functions called directly via squeue having a prototype of edesc_t. 746 */ 747 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 748 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 749 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 750 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 751 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 752 void tcp_input(void *arg, mblk_t *mp, void *arg2); 753 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 754 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 755 void tcp_output(void *arg, mblk_t *mp, void *arg2); 756 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 757 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 758 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 759 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 760 761 762 /* Prototype for TCP functions */ 763 static void tcp_random_init(void); 764 int tcp_random(void); 765 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 766 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 767 tcp_t *eager); 768 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 769 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 770 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 771 boolean_t user_specified); 772 static void tcp_closei_local(tcp_t *tcp); 773 static void tcp_close_detached(tcp_t *tcp); 774 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 775 mblk_t *idmp, mblk_t **defermp); 776 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 777 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 778 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 779 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 780 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 781 uint32_t scope_id, cred_t *cr, pid_t pid); 782 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 783 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 784 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 785 static char *tcp_display(tcp_t *tcp, char *, char); 786 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 787 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 788 static void tcp_eager_unlink(tcp_t *tcp); 789 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 790 int unixerr); 791 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 792 int tlierr, int unixerr); 793 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 794 cred_t *cr); 795 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 796 char *value, caddr_t cp, cred_t *cr); 797 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 798 char *value, caddr_t cp, cred_t *cr); 799 static int tcp_tpistate(tcp_t *tcp); 800 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 801 int caller_holds_lock); 802 static void tcp_bind_hash_remove(tcp_t *tcp); 803 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 804 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 805 static void tcp_acceptor_hash_remove(tcp_t *tcp); 806 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 807 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 808 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 809 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 810 void tcp_g_q_setup(tcp_stack_t *); 811 void tcp_g_q_create(tcp_stack_t *); 812 void tcp_g_q_destroy(tcp_stack_t *); 813 static int tcp_header_init_ipv4(tcp_t *tcp); 814 static int tcp_header_init_ipv6(tcp_t *tcp); 815 int tcp_init(tcp_t *tcp, queue_t *q); 816 static int tcp_init_values(tcp_t *tcp); 817 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 818 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 819 static void tcp_ip_notify(tcp_t *tcp); 820 static mblk_t *tcp_ire_mp(mblk_t **mpp); 821 static void tcp_iss_init(tcp_t *tcp); 822 static void tcp_keepalive_killer(void *arg); 823 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 824 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 825 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 826 int *do_disconnectp, int *t_errorp, int *sys_errorp); 827 static boolean_t tcp_allow_connopt_set(int level, int name); 828 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 829 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 830 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 831 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 832 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 833 mblk_t *mblk); 834 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 835 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 836 uchar_t *ptr, uint_t len); 837 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 838 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 839 tcp_stack_t *); 840 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 841 caddr_t cp, cred_t *cr); 842 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 843 caddr_t cp, cred_t *cr); 844 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 845 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 846 caddr_t cp, cred_t *cr); 847 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 848 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 849 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 850 static void tcp_reinit(tcp_t *tcp); 851 static void tcp_reinit_values(tcp_t *tcp); 852 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 853 tcp_t *thisstream, cred_t *cr); 854 855 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 856 static uint_t tcp_rcv_drain(tcp_t *tcp); 857 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 858 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 859 static void tcp_ss_rexmit(tcp_t *tcp); 860 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 861 static void tcp_process_options(tcp_t *, tcph_t *); 862 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 863 static void tcp_rsrv(queue_t *q); 864 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 865 static int tcp_snmp_state(tcp_t *tcp); 866 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 867 cred_t *cr); 868 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 869 cred_t *cr); 870 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 871 cred_t *cr); 872 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 873 cred_t *cr); 874 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 875 cred_t *cr); 876 static void tcp_timer(void *arg); 877 static void tcp_timer_callback(void *); 878 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 879 boolean_t random); 880 static in_port_t tcp_get_next_priv_port(const tcp_t *); 881 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 882 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 883 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 884 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 885 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 886 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 887 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 888 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 889 const int num_sack_blk, int *usable, uint_t *snxt, 890 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 891 const int mdt_thres); 892 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 893 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 894 const int num_sack_blk, int *usable, uint_t *snxt, 895 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 896 const int mdt_thres); 897 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 898 int num_sack_blk); 899 static void tcp_wsrv(queue_t *q); 900 static int tcp_xmit_end(tcp_t *tcp); 901 static void tcp_ack_timer(void *arg); 902 static mblk_t *tcp_ack_mp(tcp_t *tcp); 903 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 904 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 905 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 906 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 907 uint32_t ack, int ctl); 908 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 909 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 910 static int setmaxps(queue_t *q, int maxpsz); 911 static void tcp_set_rto(tcp_t *, time_t); 912 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 913 boolean_t, boolean_t); 914 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 915 boolean_t ipsec_mctl); 916 static int tcp_build_hdrs(tcp_t *); 917 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 918 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 919 tcph_t *tcph); 920 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 921 static mblk_t *tcp_mdt_info_mp(mblk_t *); 922 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 923 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 924 const boolean_t, const uint32_t, const uint32_t, 925 const uint32_t, const uint32_t, tcp_stack_t *); 926 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 927 const uint_t, const uint_t, boolean_t *); 928 static mblk_t *tcp_lso_info_mp(mblk_t *); 929 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 930 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 931 extern mblk_t *tcp_timermp_alloc(int); 932 extern void tcp_timermp_free(tcp_t *); 933 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 934 static void tcp_stop_lingering(tcp_t *tcp); 935 static void tcp_close_linger_timeout(void *arg); 936 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 937 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 938 static void tcp_stack_fini(netstackid_t stackid, void *arg); 939 static void *tcp_g_kstat_init(tcp_g_stat_t *); 940 static void tcp_g_kstat_fini(kstat_t *); 941 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 942 static void tcp_kstat_fini(netstackid_t, kstat_t *); 943 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 944 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 945 static int tcp_kstat_update(kstat_t *kp, int rw); 946 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 947 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 948 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 949 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 950 tcph_t *tcph, mblk_t *idmp); 951 static int tcp_squeue_switch(int); 952 953 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 954 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 955 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 956 static int tcp_tpi_close(queue_t *, int); 957 static int tcpclose_accept(queue_t *); 958 959 static void tcp_squeue_add(squeue_t *); 960 static boolean_t tcp_zcopy_check(tcp_t *); 961 static void tcp_zcopy_notify(tcp_t *); 962 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 963 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 964 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 965 966 extern void tcp_kssl_input(tcp_t *, mblk_t *); 967 968 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 969 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 970 971 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 972 sock_upper_handle_t, cred_t *); 973 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 974 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int); 975 static int tcp_do_listen(conn_t *, int, cred_t *); 976 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 977 cred_t *, pid_t); 978 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 979 boolean_t); 980 static int tcp_do_unbind(conn_t *); 981 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 982 boolean_t); 983 984 /* 985 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 986 * 987 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 988 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 989 * (defined in tcp.h) needs to be filled in and passed into the kernel 990 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 991 * structure contains the four-tuple of a TCP connection and a range of TCP 992 * states (specified by ac_start and ac_end). The use of wildcard addresses 993 * and ports is allowed. Connections with a matching four tuple and a state 994 * within the specified range will be aborted. The valid states for the 995 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 996 * inclusive. 997 * 998 * An application which has its connection aborted by this ioctl will receive 999 * an error that is dependent on the connection state at the time of the abort. 1000 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1001 * though a RST packet has been received. If the connection state is equal to 1002 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1003 * and all resources associated with the connection will be freed. 1004 */ 1005 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1006 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1007 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1008 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 1009 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1010 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1011 boolean_t, tcp_stack_t *); 1012 1013 static struct module_info tcp_rinfo = { 1014 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1015 }; 1016 1017 static struct module_info tcp_winfo = { 1018 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1019 }; 1020 1021 /* 1022 * Entry points for TCP as a device. The normal case which supports 1023 * the TCP functionality. 1024 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 1025 */ 1026 struct qinit tcp_rinitv4 = { 1027 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 1028 }; 1029 1030 struct qinit tcp_rinitv6 = { 1031 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 1032 }; 1033 1034 struct qinit tcp_winit = { 1035 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1036 }; 1037 1038 /* Initial entry point for TCP in socket mode. */ 1039 struct qinit tcp_sock_winit = { 1040 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1041 }; 1042 1043 /* TCP entry point during fallback */ 1044 struct qinit tcp_fallback_sock_winit = { 1045 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 1046 }; 1047 1048 /* 1049 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1050 * an accept. Avoid allocating data structures since eager has already 1051 * been created. 1052 */ 1053 struct qinit tcp_acceptor_rinit = { 1054 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1055 }; 1056 1057 struct qinit tcp_acceptor_winit = { 1058 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1059 }; 1060 1061 /* 1062 * Entry points for TCP loopback (read side only) 1063 * The open routine is only used for reopens, thus no need to 1064 * have a separate one for tcp_openv6. 1065 */ 1066 struct qinit tcp_loopback_rinit = { 1067 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 1068 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1069 }; 1070 1071 /* For AF_INET aka /dev/tcp */ 1072 struct streamtab tcpinfov4 = { 1073 &tcp_rinitv4, &tcp_winit 1074 }; 1075 1076 /* For AF_INET6 aka /dev/tcp6 */ 1077 struct streamtab tcpinfov6 = { 1078 &tcp_rinitv6, &tcp_winit 1079 }; 1080 1081 sock_downcalls_t sock_tcp_downcalls; 1082 1083 /* 1084 * Have to ensure that tcp_g_q_close is not done by an 1085 * interrupt thread. 1086 */ 1087 static taskq_t *tcp_taskq; 1088 1089 /* Setable only in /etc/system. Move to ndd? */ 1090 boolean_t tcp_icmp_source_quench = B_FALSE; 1091 1092 /* 1093 * Following assumes TPI alignment requirements stay along 32 bit 1094 * boundaries 1095 */ 1096 #define ROUNDUP32(x) \ 1097 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1098 1099 /* Template for response to info request. */ 1100 static struct T_info_ack tcp_g_t_info_ack = { 1101 T_INFO_ACK, /* PRIM_type */ 1102 0, /* TSDU_size */ 1103 T_INFINITE, /* ETSDU_size */ 1104 T_INVALID, /* CDATA_size */ 1105 T_INVALID, /* DDATA_size */ 1106 sizeof (sin_t), /* ADDR_size */ 1107 0, /* OPT_size - not initialized here */ 1108 TIDUSZ, /* TIDU_size */ 1109 T_COTS_ORD, /* SERV_type */ 1110 TCPS_IDLE, /* CURRENT_state */ 1111 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1112 }; 1113 1114 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1115 T_INFO_ACK, /* PRIM_type */ 1116 0, /* TSDU_size */ 1117 T_INFINITE, /* ETSDU_size */ 1118 T_INVALID, /* CDATA_size */ 1119 T_INVALID, /* DDATA_size */ 1120 sizeof (sin6_t), /* ADDR_size */ 1121 0, /* OPT_size - not initialized here */ 1122 TIDUSZ, /* TIDU_size */ 1123 T_COTS_ORD, /* SERV_type */ 1124 TCPS_IDLE, /* CURRENT_state */ 1125 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1126 }; 1127 1128 #define MS 1L 1129 #define SECONDS (1000 * MS) 1130 #define MINUTES (60 * SECONDS) 1131 #define HOURS (60 * MINUTES) 1132 #define DAYS (24 * HOURS) 1133 1134 #define PARAM_MAX (~(uint32_t)0) 1135 1136 /* Max size IP datagram is 64k - 1 */ 1137 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1138 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1139 /* Max of the above */ 1140 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1141 1142 /* Largest TCP port number */ 1143 #define TCP_MAX_PORT (64 * 1024 - 1) 1144 1145 /* 1146 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1147 * layer header. It has to be a multiple of 4. 1148 */ 1149 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1150 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1151 1152 /* 1153 * All of these are alterable, within the min/max values given, at run time. 1154 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1155 * per the TCP spec. 1156 */ 1157 /* BEGIN CSTYLED */ 1158 static tcpparam_t lcl_tcp_param_arr[] = { 1159 /*min max value name */ 1160 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1161 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1162 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1163 { 1, 1024, 1, "tcp_conn_req_min" }, 1164 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1165 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1166 { 0, 10, 0, "tcp_debug" }, 1167 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1168 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1169 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1170 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1171 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1172 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1173 { 1, 255, 64, "tcp_ipv4_ttl"}, 1174 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1175 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1176 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1177 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1178 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1179 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1180 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1181 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1182 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1183 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1184 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1185 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1186 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1187 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1188 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1189 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1190 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1191 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1192 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1193 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1194 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1195 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1196 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1197 /* 1198 * Question: What default value should I set for tcp_strong_iss? 1199 */ 1200 { 0, 2, 1, "tcp_strong_iss"}, 1201 { 0, 65536, 20, "tcp_rtt_updates"}, 1202 { 0, 1, 1, "tcp_wscale_always"}, 1203 { 0, 1, 0, "tcp_tstamp_always"}, 1204 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1205 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1206 { 0, 16, 2, "tcp_deferred_acks_max"}, 1207 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1208 { 1, 4, 4, "tcp_slow_start_initial"}, 1209 { 0, 2, 2, "tcp_sack_permitted"}, 1210 { 0, 1, 1, "tcp_compression_enabled"}, 1211 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1212 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1213 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1214 { 0, 1, 0, "tcp_rev_src_routes"}, 1215 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1216 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1217 { 0, 16, 8, "tcp_local_dacks_max"}, 1218 { 0, 2, 1, "tcp_ecn_permitted"}, 1219 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1220 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1221 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1222 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1223 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1224 }; 1225 /* END CSTYLED */ 1226 1227 /* 1228 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1229 * each header fragment in the header buffer. Each parameter value has 1230 * to be a multiple of 4 (32-bit aligned). 1231 */ 1232 static tcpparam_t lcl_tcp_mdt_head_param = 1233 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1234 static tcpparam_t lcl_tcp_mdt_tail_param = 1235 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1236 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1237 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1238 1239 /* 1240 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1241 * the maximum number of payload buffers associated per Multidata. 1242 */ 1243 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1244 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1245 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1246 1247 /* Round up the value to the nearest mss. */ 1248 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1249 1250 /* 1251 * Set ECN capable transport (ECT) code point in IP header. 1252 * 1253 * Note that there are 2 ECT code points '01' and '10', which are called 1254 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1255 * point ECT(0) for TCP as described in RFC 2481. 1256 */ 1257 #define SET_ECT(tcp, iph) \ 1258 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1259 /* We need to clear the code point first. */ \ 1260 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1261 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1262 } else { \ 1263 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1264 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1265 } 1266 1267 /* 1268 * The format argument to pass to tcp_display(). 1269 * DISP_PORT_ONLY means that the returned string has only port info. 1270 * DISP_ADDR_AND_PORT means that the returned string also contains the 1271 * remote and local IP address. 1272 */ 1273 #define DISP_PORT_ONLY 1 1274 #define DISP_ADDR_AND_PORT 2 1275 1276 #define NDD_TOO_QUICK_MSG \ 1277 "ndd get info rate too high for non-privileged users, try again " \ 1278 "later.\n" 1279 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1280 1281 #define IS_VMLOANED_MBLK(mp) \ 1282 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1283 1284 1285 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1286 boolean_t tcp_mdt_chain = B_TRUE; 1287 1288 /* 1289 * MDT threshold in the form of effective send MSS multiplier; we take 1290 * the MDT path if the amount of unsent data exceeds the threshold value 1291 * (default threshold is 1*SMSS). 1292 */ 1293 uint_t tcp_mdt_smss_threshold = 1; 1294 1295 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1296 1297 /* 1298 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1299 * tunable settable via NDD. Otherwise, the per-connection behavior is 1300 * determined dynamically during tcp_adapt_ire(), which is the default. 1301 */ 1302 boolean_t tcp_static_maxpsz = B_FALSE; 1303 1304 /* Setable in /etc/system */ 1305 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1306 uint32_t tcp_random_anon_port = 1; 1307 1308 /* 1309 * To reach to an eager in Q0 which can be dropped due to an incoming 1310 * new SYN request when Q0 is full, a new doubly linked list is 1311 * introduced. This list allows to select an eager from Q0 in O(1) time. 1312 * This is needed to avoid spending too much time walking through the 1313 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1314 * this new list has to be a member of Q0. 1315 * This list is headed by listener's tcp_t. When the list is empty, 1316 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1317 * of listener's tcp_t point to listener's tcp_t itself. 1318 * 1319 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1320 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1321 * These macros do not affect the eager's membership to Q0. 1322 */ 1323 1324 1325 #define MAKE_DROPPABLE(listener, eager) \ 1326 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1327 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1328 = (eager); \ 1329 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1330 (eager)->tcp_eager_next_drop_q0 = \ 1331 (listener)->tcp_eager_next_drop_q0; \ 1332 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1333 } 1334 1335 #define MAKE_UNDROPPABLE(eager) \ 1336 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1337 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1338 = (eager)->tcp_eager_prev_drop_q0; \ 1339 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1340 = (eager)->tcp_eager_next_drop_q0; \ 1341 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1342 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1343 } 1344 1345 /* 1346 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1347 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1348 * data, TCP will not respond with an ACK. RFC 793 requires that 1349 * TCP responds with an ACK for such a bogus ACK. By not following 1350 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1351 * an attacker successfully spoofs an acceptable segment to our 1352 * peer; or when our peer is "confused." 1353 */ 1354 uint32_t tcp_drop_ack_unsent_cnt = 10; 1355 1356 /* 1357 * Hook functions to enable cluster networking 1358 * On non-clustered systems these vectors must always be NULL. 1359 */ 1360 1361 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1362 sa_family_t addr_family, uint8_t *laddrp, 1363 in_port_t lport, void *args) = NULL; 1364 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1365 sa_family_t addr_family, uint8_t *laddrp, 1366 in_port_t lport, void *args) = NULL; 1367 1368 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1369 boolean_t is_outgoing, 1370 sa_family_t addr_family, 1371 uint8_t *laddrp, in_port_t lport, 1372 uint8_t *faddrp, in_port_t fport, 1373 void *args) = NULL; 1374 1375 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1376 sa_family_t addr_family, uint8_t *laddrp, 1377 in_port_t lport, uint8_t *faddrp, 1378 in_port_t fport, void *args) = NULL; 1379 1380 /* 1381 * The following are defined in ip.c 1382 */ 1383 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1384 sa_family_t addr_family, uint8_t *laddrp, 1385 void *args); 1386 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1387 sa_family_t addr_family, uint8_t *laddrp, 1388 uint8_t *faddrp, void *args); 1389 1390 1391 /* 1392 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1393 */ 1394 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1395 (err) = 0; \ 1396 if (cl_inet_connect2 != NULL) { \ 1397 /* \ 1398 * Running in cluster mode - register active connection \ 1399 * information \ 1400 */ \ 1401 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1402 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1403 (err) = (*cl_inet_connect2)( \ 1404 (connp)->conn_netstack->netstack_stackid,\ 1405 IPPROTO_TCP, is_outgoing, AF_INET, \ 1406 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1407 (in_port_t)(tcp)->tcp_lport, \ 1408 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1409 (in_port_t)(tcp)->tcp_fport, NULL); \ 1410 } \ 1411 } else { \ 1412 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1413 &(tcp)->tcp_ip6h->ip6_src)) { \ 1414 (err) = (*cl_inet_connect2)( \ 1415 (connp)->conn_netstack->netstack_stackid,\ 1416 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1417 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1418 (in_port_t)(tcp)->tcp_lport, \ 1419 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1420 (in_port_t)(tcp)->tcp_fport, NULL); \ 1421 } \ 1422 } \ 1423 } \ 1424 } 1425 1426 #define CL_INET_DISCONNECT(connp, tcp) { \ 1427 if (cl_inet_disconnect != NULL) { \ 1428 /* \ 1429 * Running in cluster mode - deregister active \ 1430 * connection information \ 1431 */ \ 1432 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1433 if ((tcp)->tcp_ip_src != 0) { \ 1434 (*cl_inet_disconnect)( \ 1435 (connp)->conn_netstack->netstack_stackid,\ 1436 IPPROTO_TCP, AF_INET, \ 1437 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1438 (in_port_t)(tcp)->tcp_lport, \ 1439 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1440 (in_port_t)(tcp)->tcp_fport, NULL); \ 1441 } \ 1442 } else { \ 1443 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1444 &(tcp)->tcp_ip_src_v6)) { \ 1445 (*cl_inet_disconnect)( \ 1446 (connp)->conn_netstack->netstack_stackid,\ 1447 IPPROTO_TCP, AF_INET6, \ 1448 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1449 (in_port_t)(tcp)->tcp_lport, \ 1450 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1451 (in_port_t)(tcp)->tcp_fport, NULL); \ 1452 } \ 1453 } \ 1454 } \ 1455 } 1456 1457 /* 1458 * Cluster networking hook for traversing current connection list. 1459 * This routine is used to extract the current list of live connections 1460 * which must continue to to be dispatched to this node. 1461 */ 1462 int cl_tcp_walk_list(netstackid_t stack_id, 1463 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1464 1465 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1466 void *arg, tcp_stack_t *tcps); 1467 1468 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1469 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1470 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1471 ip6_t *, ip6h, int, 0); 1472 1473 /* 1474 * Figure out the value of window scale opton. Note that the rwnd is 1475 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1476 * We cannot find the scale value and then do a round up of tcp_rwnd 1477 * because the scale value may not be correct after that. 1478 * 1479 * Set the compiler flag to make this function inline. 1480 */ 1481 static void 1482 tcp_set_ws_value(tcp_t *tcp) 1483 { 1484 int i; 1485 uint32_t rwnd = tcp->tcp_rwnd; 1486 1487 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1488 i++, rwnd >>= 1) 1489 ; 1490 tcp->tcp_rcv_ws = i; 1491 } 1492 1493 /* 1494 * Remove a connection from the list of detached TIME_WAIT connections. 1495 * It returns B_FALSE if it can't remove the connection from the list 1496 * as the connection has already been removed from the list due to an 1497 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1498 */ 1499 static boolean_t 1500 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1501 { 1502 boolean_t locked = B_FALSE; 1503 1504 if (tcp_time_wait == NULL) { 1505 tcp_time_wait = *((tcp_squeue_priv_t **) 1506 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1507 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1508 locked = B_TRUE; 1509 } else { 1510 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1511 } 1512 1513 if (tcp->tcp_time_wait_expire == 0) { 1514 ASSERT(tcp->tcp_time_wait_next == NULL); 1515 ASSERT(tcp->tcp_time_wait_prev == NULL); 1516 if (locked) 1517 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1518 return (B_FALSE); 1519 } 1520 ASSERT(TCP_IS_DETACHED(tcp)); 1521 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1522 1523 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1524 ASSERT(tcp->tcp_time_wait_prev == NULL); 1525 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1526 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1527 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1528 NULL; 1529 } else { 1530 tcp_time_wait->tcp_time_wait_tail = NULL; 1531 } 1532 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1533 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1534 ASSERT(tcp->tcp_time_wait_next == NULL); 1535 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1536 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1537 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1538 } else { 1539 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1540 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1541 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1542 tcp->tcp_time_wait_next; 1543 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1544 tcp->tcp_time_wait_prev; 1545 } 1546 tcp->tcp_time_wait_next = NULL; 1547 tcp->tcp_time_wait_prev = NULL; 1548 tcp->tcp_time_wait_expire = 0; 1549 1550 if (locked) 1551 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1552 return (B_TRUE); 1553 } 1554 1555 /* 1556 * Add a connection to the list of detached TIME_WAIT connections 1557 * and set its time to expire. 1558 */ 1559 static void 1560 tcp_time_wait_append(tcp_t *tcp) 1561 { 1562 tcp_stack_t *tcps = tcp->tcp_tcps; 1563 tcp_squeue_priv_t *tcp_time_wait = 1564 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1565 SQPRIVATE_TCP)); 1566 1567 tcp_timers_stop(tcp); 1568 1569 /* Freed above */ 1570 ASSERT(tcp->tcp_timer_tid == 0); 1571 ASSERT(tcp->tcp_ack_tid == 0); 1572 1573 /* must have happened at the time of detaching the tcp */ 1574 ASSERT(tcp->tcp_ptpahn == NULL); 1575 ASSERT(tcp->tcp_flow_stopped == 0); 1576 ASSERT(tcp->tcp_time_wait_next == NULL); 1577 ASSERT(tcp->tcp_time_wait_prev == NULL); 1578 ASSERT(tcp->tcp_time_wait_expire == NULL); 1579 ASSERT(tcp->tcp_listener == NULL); 1580 1581 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1582 /* 1583 * The value computed below in tcp->tcp_time_wait_expire may 1584 * appear negative or wrap around. That is ok since our 1585 * interest is only in the difference between the current lbolt 1586 * value and tcp->tcp_time_wait_expire. But the value should not 1587 * be zero, since it means the tcp is not in the TIME_WAIT list. 1588 * The corresponding comparison in tcp_time_wait_collector() uses 1589 * modular arithmetic. 1590 */ 1591 tcp->tcp_time_wait_expire += 1592 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1593 if (tcp->tcp_time_wait_expire == 0) 1594 tcp->tcp_time_wait_expire = 1; 1595 1596 ASSERT(TCP_IS_DETACHED(tcp)); 1597 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1598 ASSERT(tcp->tcp_time_wait_next == NULL); 1599 ASSERT(tcp->tcp_time_wait_prev == NULL); 1600 TCP_DBGSTAT(tcps, tcp_time_wait); 1601 1602 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1603 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1604 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1605 tcp_time_wait->tcp_time_wait_head = tcp; 1606 } else { 1607 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1608 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1609 TCPS_TIME_WAIT); 1610 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1611 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1612 } 1613 tcp_time_wait->tcp_time_wait_tail = tcp; 1614 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1615 } 1616 1617 /* ARGSUSED */ 1618 void 1619 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1620 { 1621 conn_t *connp = (conn_t *)arg; 1622 tcp_t *tcp = connp->conn_tcp; 1623 tcp_stack_t *tcps = tcp->tcp_tcps; 1624 1625 ASSERT(tcp != NULL); 1626 if (tcp->tcp_state == TCPS_CLOSED) { 1627 return; 1628 } 1629 1630 ASSERT((tcp->tcp_family == AF_INET && 1631 tcp->tcp_ipversion == IPV4_VERSION) || 1632 (tcp->tcp_family == AF_INET6 && 1633 (tcp->tcp_ipversion == IPV4_VERSION || 1634 tcp->tcp_ipversion == IPV6_VERSION))); 1635 ASSERT(!tcp->tcp_listener); 1636 1637 TCP_STAT(tcps, tcp_time_wait_reap); 1638 ASSERT(TCP_IS_DETACHED(tcp)); 1639 1640 /* 1641 * Because they have no upstream client to rebind or tcp_close() 1642 * them later, we axe the connection here and now. 1643 */ 1644 tcp_close_detached(tcp); 1645 } 1646 1647 /* 1648 * Remove cached/latched IPsec references. 1649 */ 1650 void 1651 tcp_ipsec_cleanup(tcp_t *tcp) 1652 { 1653 conn_t *connp = tcp->tcp_connp; 1654 1655 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1656 1657 if (connp->conn_latch != NULL) { 1658 IPLATCH_REFRELE(connp->conn_latch, 1659 connp->conn_netstack); 1660 connp->conn_latch = NULL; 1661 } 1662 if (connp->conn_policy != NULL) { 1663 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1664 connp->conn_policy = NULL; 1665 } 1666 } 1667 1668 /* 1669 * Cleaup before placing on free list. 1670 * Disassociate from the netstack/tcp_stack_t since the freelist 1671 * is per squeue and not per netstack. 1672 */ 1673 void 1674 tcp_cleanup(tcp_t *tcp) 1675 { 1676 mblk_t *mp; 1677 char *tcp_iphc; 1678 int tcp_iphc_len; 1679 int tcp_hdr_grown; 1680 tcp_sack_info_t *tcp_sack_info; 1681 conn_t *connp = tcp->tcp_connp; 1682 tcp_stack_t *tcps = tcp->tcp_tcps; 1683 netstack_t *ns = tcps->tcps_netstack; 1684 mblk_t *tcp_rsrv_mp; 1685 1686 tcp_bind_hash_remove(tcp); 1687 1688 /* Cleanup that which needs the netstack first */ 1689 tcp_ipsec_cleanup(tcp); 1690 1691 tcp_free(tcp); 1692 1693 /* Release any SSL context */ 1694 if (tcp->tcp_kssl_ent != NULL) { 1695 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1696 tcp->tcp_kssl_ent = NULL; 1697 } 1698 1699 if (tcp->tcp_kssl_ctx != NULL) { 1700 kssl_release_ctx(tcp->tcp_kssl_ctx); 1701 tcp->tcp_kssl_ctx = NULL; 1702 } 1703 tcp->tcp_kssl_pending = B_FALSE; 1704 1705 conn_delete_ire(connp, NULL); 1706 1707 /* 1708 * Since we will bzero the entire structure, we need to 1709 * remove it and reinsert it in global hash list. We 1710 * know the walkers can't get to this conn because we 1711 * had set CONDEMNED flag earlier and checked reference 1712 * under conn_lock so walker won't pick it and when we 1713 * go the ipcl_globalhash_remove() below, no walker 1714 * can get to it. 1715 */ 1716 ipcl_globalhash_remove(connp); 1717 1718 /* 1719 * Now it is safe to decrement the reference counts. 1720 * This might be the last reference on the netstack and TCPS 1721 * in which case it will cause the tcp_g_q_close and 1722 * the freeing of the IP Instance. 1723 */ 1724 connp->conn_netstack = NULL; 1725 netstack_rele(ns); 1726 ASSERT(tcps != NULL); 1727 tcp->tcp_tcps = NULL; 1728 TCPS_REFRELE(tcps); 1729 1730 /* Save some state */ 1731 mp = tcp->tcp_timercache; 1732 1733 tcp_sack_info = tcp->tcp_sack_info; 1734 tcp_iphc = tcp->tcp_iphc; 1735 tcp_iphc_len = tcp->tcp_iphc_len; 1736 tcp_hdr_grown = tcp->tcp_hdr_grown; 1737 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1738 1739 if (connp->conn_cred != NULL) { 1740 crfree(connp->conn_cred); 1741 connp->conn_cred = NULL; 1742 } 1743 if (connp->conn_peercred != NULL) { 1744 crfree(connp->conn_peercred); 1745 connp->conn_peercred = NULL; 1746 } 1747 ipcl_conn_cleanup(connp); 1748 connp->conn_flags = IPCL_TCPCONN; 1749 bzero(tcp, sizeof (tcp_t)); 1750 1751 /* restore the state */ 1752 tcp->tcp_timercache = mp; 1753 1754 tcp->tcp_sack_info = tcp_sack_info; 1755 tcp->tcp_iphc = tcp_iphc; 1756 tcp->tcp_iphc_len = tcp_iphc_len; 1757 tcp->tcp_hdr_grown = tcp_hdr_grown; 1758 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1759 1760 tcp->tcp_connp = connp; 1761 1762 ASSERT(connp->conn_tcp == tcp); 1763 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1764 connp->conn_state_flags = CONN_INCIPIENT; 1765 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1766 ASSERT(connp->conn_ref == 1); 1767 } 1768 1769 /* 1770 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1771 * is done forwards from the head. 1772 * This walks all stack instances since 1773 * tcp_time_wait remains global across all stacks. 1774 */ 1775 /* ARGSUSED */ 1776 void 1777 tcp_time_wait_collector(void *arg) 1778 { 1779 tcp_t *tcp; 1780 clock_t now; 1781 mblk_t *mp; 1782 conn_t *connp; 1783 kmutex_t *lock; 1784 boolean_t removed; 1785 1786 squeue_t *sqp = (squeue_t *)arg; 1787 tcp_squeue_priv_t *tcp_time_wait = 1788 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1789 1790 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1791 tcp_time_wait->tcp_time_wait_tid = 0; 1792 1793 if (tcp_time_wait->tcp_free_list != NULL && 1794 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1795 TCP_G_STAT(tcp_freelist_cleanup); 1796 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1797 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1798 tcp->tcp_time_wait_next = NULL; 1799 tcp_time_wait->tcp_free_list_cnt--; 1800 ASSERT(tcp->tcp_tcps == NULL); 1801 CONN_DEC_REF(tcp->tcp_connp); 1802 } 1803 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1804 } 1805 1806 /* 1807 * In order to reap time waits reliably, we should use a 1808 * source of time that is not adjustable by the user -- hence 1809 * the call to ddi_get_lbolt(). 1810 */ 1811 now = ddi_get_lbolt(); 1812 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1813 /* 1814 * Compare times using modular arithmetic, since 1815 * lbolt can wrapover. 1816 */ 1817 if ((now - tcp->tcp_time_wait_expire) < 0) { 1818 break; 1819 } 1820 1821 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1822 ASSERT(removed); 1823 1824 connp = tcp->tcp_connp; 1825 ASSERT(connp->conn_fanout != NULL); 1826 lock = &connp->conn_fanout->connf_lock; 1827 /* 1828 * This is essentially a TW reclaim fast path optimization for 1829 * performance where the timewait collector checks under the 1830 * fanout lock (so that no one else can get access to the 1831 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1832 * the classifier hash list. If ref count is indeed 2, we can 1833 * just remove the conn under the fanout lock and avoid 1834 * cleaning up the conn under the squeue, provided that 1835 * clustering callbacks are not enabled. If clustering is 1836 * enabled, we need to make the clustering callback before 1837 * setting the CONDEMNED flag and after dropping all locks and 1838 * so we forego this optimization and fall back to the slow 1839 * path. Also please see the comments in tcp_closei_local 1840 * regarding the refcnt logic. 1841 * 1842 * Since we are holding the tcp_time_wait_lock, its better 1843 * not to block on the fanout_lock because other connections 1844 * can't add themselves to time_wait list. So we do a 1845 * tryenter instead of mutex_enter. 1846 */ 1847 if (mutex_tryenter(lock)) { 1848 mutex_enter(&connp->conn_lock); 1849 if ((connp->conn_ref == 2) && 1850 (cl_inet_disconnect == NULL)) { 1851 ipcl_hash_remove_locked(connp, 1852 connp->conn_fanout); 1853 /* 1854 * Set the CONDEMNED flag now itself so that 1855 * the refcnt cannot increase due to any 1856 * walker. But we have still not cleaned up 1857 * conn_ire_cache. This is still ok since 1858 * we are going to clean it up in tcp_cleanup 1859 * immediately and any interface unplumb 1860 * thread will wait till the ire is blown away 1861 */ 1862 connp->conn_state_flags |= CONN_CONDEMNED; 1863 mutex_exit(lock); 1864 mutex_exit(&connp->conn_lock); 1865 if (tcp_time_wait->tcp_free_list_cnt < 1866 tcp_free_list_max_cnt) { 1867 /* Add to head of tcp_free_list */ 1868 mutex_exit( 1869 &tcp_time_wait->tcp_time_wait_lock); 1870 tcp_cleanup(tcp); 1871 ASSERT(connp->conn_latch == NULL); 1872 ASSERT(connp->conn_policy == NULL); 1873 ASSERT(tcp->tcp_tcps == NULL); 1874 ASSERT(connp->conn_netstack == NULL); 1875 1876 mutex_enter( 1877 &tcp_time_wait->tcp_time_wait_lock); 1878 tcp->tcp_time_wait_next = 1879 tcp_time_wait->tcp_free_list; 1880 tcp_time_wait->tcp_free_list = tcp; 1881 tcp_time_wait->tcp_free_list_cnt++; 1882 continue; 1883 } else { 1884 /* Do not add to tcp_free_list */ 1885 mutex_exit( 1886 &tcp_time_wait->tcp_time_wait_lock); 1887 tcp_bind_hash_remove(tcp); 1888 conn_delete_ire(tcp->tcp_connp, NULL); 1889 tcp_ipsec_cleanup(tcp); 1890 CONN_DEC_REF(tcp->tcp_connp); 1891 } 1892 } else { 1893 CONN_INC_REF_LOCKED(connp); 1894 mutex_exit(lock); 1895 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1896 mutex_exit(&connp->conn_lock); 1897 /* 1898 * We can reuse the closemp here since conn has 1899 * detached (otherwise we wouldn't even be in 1900 * time_wait list). tcp_closemp_used can safely 1901 * be changed without taking a lock as no other 1902 * thread can concurrently access it at this 1903 * point in the connection lifecycle. 1904 */ 1905 1906 if (tcp->tcp_closemp.b_prev == NULL) 1907 tcp->tcp_closemp_used = B_TRUE; 1908 else 1909 cmn_err(CE_PANIC, 1910 "tcp_timewait_collector: " 1911 "concurrent use of tcp_closemp: " 1912 "connp %p tcp %p\n", (void *)connp, 1913 (void *)tcp); 1914 1915 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1916 mp = &tcp->tcp_closemp; 1917 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1918 tcp_timewait_output, connp, 1919 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1920 } 1921 } else { 1922 mutex_enter(&connp->conn_lock); 1923 CONN_INC_REF_LOCKED(connp); 1924 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1925 mutex_exit(&connp->conn_lock); 1926 /* 1927 * We can reuse the closemp here since conn has 1928 * detached (otherwise we wouldn't even be in 1929 * time_wait list). tcp_closemp_used can safely 1930 * be changed without taking a lock as no other 1931 * thread can concurrently access it at this 1932 * point in the connection lifecycle. 1933 */ 1934 1935 if (tcp->tcp_closemp.b_prev == NULL) 1936 tcp->tcp_closemp_used = B_TRUE; 1937 else 1938 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1939 "concurrent use of tcp_closemp: " 1940 "connp %p tcp %p\n", (void *)connp, 1941 (void *)tcp); 1942 1943 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1944 mp = &tcp->tcp_closemp; 1945 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1946 tcp_timewait_output, connp, 1947 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1948 } 1949 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1950 } 1951 1952 if (tcp_time_wait->tcp_free_list != NULL) 1953 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1954 1955 tcp_time_wait->tcp_time_wait_tid = 1956 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1957 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1958 CALLOUT_FLAG_ROUNDUP); 1959 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1960 } 1961 1962 /* 1963 * Reply to a clients T_CONN_RES TPI message. This function 1964 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1965 * on the acceptor STREAM and processed in tcp_wput_accept(). 1966 * Read the block comment on top of tcp_conn_request(). 1967 */ 1968 static void 1969 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1970 { 1971 tcp_t *acceptor; 1972 tcp_t *eager; 1973 tcp_t *tcp; 1974 struct T_conn_res *tcr; 1975 t_uscalar_t acceptor_id; 1976 t_scalar_t seqnum; 1977 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1978 struct tcp_options *tcpopt; 1979 mblk_t *ok_mp; 1980 mblk_t *mp1; 1981 tcp_stack_t *tcps = listener->tcp_tcps; 1982 1983 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1984 tcp_err_ack(listener, mp, TPROTO, 0); 1985 return; 1986 } 1987 tcr = (struct T_conn_res *)mp->b_rptr; 1988 1989 /* 1990 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1991 * read side queue of the streams device underneath us i.e. the 1992 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1993 * look it up in the queue_hash. Under LP64 it sends down the 1994 * minor_t of the accepting endpoint. 1995 * 1996 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1997 * fanout hash lock is held. 1998 * This prevents any thread from entering the acceptor queue from 1999 * below (since it has not been hard bound yet i.e. any inbound 2000 * packets will arrive on the listener or default tcp queue and 2001 * go through tcp_lookup). 2002 * The CONN_INC_REF will prevent the acceptor from closing. 2003 * 2004 * XXX It is still possible for a tli application to send down data 2005 * on the accepting stream while another thread calls t_accept. 2006 * This should not be a problem for well-behaved applications since 2007 * the T_OK_ACK is sent after the queue swapping is completed. 2008 * 2009 * If the accepting fd is the same as the listening fd, avoid 2010 * queue hash lookup since that will return an eager listener in a 2011 * already established state. 2012 */ 2013 acceptor_id = tcr->ACCEPTOR_id; 2014 mutex_enter(&listener->tcp_eager_lock); 2015 if (listener->tcp_acceptor_id == acceptor_id) { 2016 eager = listener->tcp_eager_next_q; 2017 /* only count how many T_CONN_INDs so don't count q0 */ 2018 if ((listener->tcp_conn_req_cnt_q != 1) || 2019 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 2020 mutex_exit(&listener->tcp_eager_lock); 2021 tcp_err_ack(listener, mp, TBADF, 0); 2022 return; 2023 } 2024 if (listener->tcp_conn_req_cnt_q0 != 0) { 2025 /* Throw away all the eagers on q0. */ 2026 tcp_eager_cleanup(listener, 1); 2027 } 2028 if (listener->tcp_syn_defense) { 2029 listener->tcp_syn_defense = B_FALSE; 2030 if (listener->tcp_ip_addr_cache != NULL) { 2031 kmem_free(listener->tcp_ip_addr_cache, 2032 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 2033 listener->tcp_ip_addr_cache = NULL; 2034 } 2035 } 2036 /* 2037 * Transfer tcp_conn_req_max to the eager so that when 2038 * a disconnect occurs we can revert the endpoint to the 2039 * listen state. 2040 */ 2041 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2042 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2043 /* 2044 * Get a reference on the acceptor just like the 2045 * tcp_acceptor_hash_lookup below. 2046 */ 2047 acceptor = listener; 2048 CONN_INC_REF(acceptor->tcp_connp); 2049 } else { 2050 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 2051 if (acceptor == NULL) { 2052 if (listener->tcp_debug) { 2053 (void) strlog(TCP_MOD_ID, 0, 1, 2054 SL_ERROR|SL_TRACE, 2055 "tcp_accept: did not find acceptor 0x%x\n", 2056 acceptor_id); 2057 } 2058 mutex_exit(&listener->tcp_eager_lock); 2059 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2060 return; 2061 } 2062 /* 2063 * Verify acceptor state. The acceptable states for an acceptor 2064 * include TCPS_IDLE and TCPS_BOUND. 2065 */ 2066 switch (acceptor->tcp_state) { 2067 case TCPS_IDLE: 2068 /* FALLTHRU */ 2069 case TCPS_BOUND: 2070 break; 2071 default: 2072 CONN_DEC_REF(acceptor->tcp_connp); 2073 mutex_exit(&listener->tcp_eager_lock); 2074 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2075 return; 2076 } 2077 } 2078 2079 /* The listener must be in TCPS_LISTEN */ 2080 if (listener->tcp_state != TCPS_LISTEN) { 2081 CONN_DEC_REF(acceptor->tcp_connp); 2082 mutex_exit(&listener->tcp_eager_lock); 2083 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2084 return; 2085 } 2086 2087 /* 2088 * Rendezvous with an eager connection request packet hanging off 2089 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2090 * tcp structure when the connection packet arrived in 2091 * tcp_conn_request(). 2092 */ 2093 seqnum = tcr->SEQ_number; 2094 eager = listener; 2095 do { 2096 eager = eager->tcp_eager_next_q; 2097 if (eager == NULL) { 2098 CONN_DEC_REF(acceptor->tcp_connp); 2099 mutex_exit(&listener->tcp_eager_lock); 2100 tcp_err_ack(listener, mp, TBADSEQ, 0); 2101 return; 2102 } 2103 } while (eager->tcp_conn_req_seqnum != seqnum); 2104 mutex_exit(&listener->tcp_eager_lock); 2105 2106 /* 2107 * At this point, both acceptor and listener have 2 ref 2108 * that they begin with. Acceptor has one additional ref 2109 * we placed in lookup while listener has 3 additional 2110 * ref for being behind the squeue (tcp_accept() is 2111 * done on listener's squeue); being in classifier hash; 2112 * and eager's ref on listener. 2113 */ 2114 ASSERT(listener->tcp_connp->conn_ref >= 5); 2115 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2116 2117 /* 2118 * The eager at this point is set in its own squeue and 2119 * could easily have been killed (tcp_accept_finish will 2120 * deal with that) because of a TH_RST so we can only 2121 * ASSERT for a single ref. 2122 */ 2123 ASSERT(eager->tcp_connp->conn_ref >= 1); 2124 2125 /* Pre allocate the stroptions mblk also */ 2126 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2127 sizeof (struct T_conn_res)), BPRI_HI); 2128 if (opt_mp == NULL) { 2129 CONN_DEC_REF(acceptor->tcp_connp); 2130 CONN_DEC_REF(eager->tcp_connp); 2131 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2132 return; 2133 } 2134 DB_TYPE(opt_mp) = M_SETOPTS; 2135 opt_mp->b_wptr += sizeof (struct tcp_options); 2136 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2137 tcpopt->to_flags = 0; 2138 2139 /* 2140 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2141 * from listener to acceptor. 2142 */ 2143 if (listener->tcp_bound_if != 0) { 2144 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2145 tcpopt->to_boundif = listener->tcp_bound_if; 2146 } 2147 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2148 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2149 } 2150 2151 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2152 if ((mp1 = copymsg(mp)) == NULL) { 2153 CONN_DEC_REF(acceptor->tcp_connp); 2154 CONN_DEC_REF(eager->tcp_connp); 2155 freemsg(opt_mp); 2156 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2157 return; 2158 } 2159 2160 tcr = (struct T_conn_res *)mp1->b_rptr; 2161 2162 /* 2163 * This is an expanded version of mi_tpi_ok_ack_alloc() 2164 * which allocates a larger mblk and appends the new 2165 * local address to the ok_ack. The address is copied by 2166 * soaccept() for getsockname(). 2167 */ 2168 { 2169 int extra; 2170 2171 extra = (eager->tcp_family == AF_INET) ? 2172 sizeof (sin_t) : sizeof (sin6_t); 2173 2174 /* 2175 * Try to re-use mp, if possible. Otherwise, allocate 2176 * an mblk and return it as ok_mp. In any case, mp 2177 * is no longer usable upon return. 2178 */ 2179 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2180 CONN_DEC_REF(acceptor->tcp_connp); 2181 CONN_DEC_REF(eager->tcp_connp); 2182 freemsg(opt_mp); 2183 /* Original mp has been freed by now, so use mp1 */ 2184 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2185 return; 2186 } 2187 2188 mp = NULL; /* We should never use mp after this point */ 2189 2190 switch (extra) { 2191 case sizeof (sin_t): { 2192 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2193 2194 ok_mp->b_wptr += extra; 2195 sin->sin_family = AF_INET; 2196 sin->sin_port = eager->tcp_lport; 2197 sin->sin_addr.s_addr = 2198 eager->tcp_ipha->ipha_src; 2199 break; 2200 } 2201 case sizeof (sin6_t): { 2202 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2203 2204 ok_mp->b_wptr += extra; 2205 sin6->sin6_family = AF_INET6; 2206 sin6->sin6_port = eager->tcp_lport; 2207 if (eager->tcp_ipversion == IPV4_VERSION) { 2208 sin6->sin6_flowinfo = 0; 2209 IN6_IPADDR_TO_V4MAPPED( 2210 eager->tcp_ipha->ipha_src, 2211 &sin6->sin6_addr); 2212 } else { 2213 ASSERT(eager->tcp_ip6h != NULL); 2214 sin6->sin6_flowinfo = 2215 eager->tcp_ip6h->ip6_vcf & 2216 ~IPV6_VERS_AND_FLOW_MASK; 2217 sin6->sin6_addr = 2218 eager->tcp_ip6h->ip6_src; 2219 } 2220 sin6->sin6_scope_id = 0; 2221 sin6->__sin6_src_id = 0; 2222 break; 2223 } 2224 default: 2225 break; 2226 } 2227 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2228 } 2229 2230 /* 2231 * If there are no options we know that the T_CONN_RES will 2232 * succeed. However, we can't send the T_OK_ACK upstream until 2233 * the tcp_accept_swap is done since it would be dangerous to 2234 * let the application start using the new fd prior to the swap. 2235 */ 2236 tcp_accept_swap(listener, acceptor, eager); 2237 2238 /* 2239 * tcp_accept_swap unlinks eager from listener but does not drop 2240 * the eager's reference on the listener. 2241 */ 2242 ASSERT(eager->tcp_listener == NULL); 2243 ASSERT(listener->tcp_connp->conn_ref >= 5); 2244 2245 /* 2246 * The eager is now associated with its own queue. Insert in 2247 * the hash so that the connection can be reused for a future 2248 * T_CONN_RES. 2249 */ 2250 tcp_acceptor_hash_insert(acceptor_id, eager); 2251 2252 /* 2253 * We now do the processing of options with T_CONN_RES. 2254 * We delay till now since we wanted to have queue to pass to 2255 * option processing routines that points back to the right 2256 * instance structure which does not happen until after 2257 * tcp_accept_swap(). 2258 * 2259 * Note: 2260 * The sanity of the logic here assumes that whatever options 2261 * are appropriate to inherit from listner=>eager are done 2262 * before this point, and whatever were to be overridden (or not) 2263 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2264 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2265 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2266 * This may not be true at this point in time but can be fixed 2267 * independently. This option processing code starts with 2268 * the instantiated acceptor instance and the final queue at 2269 * this point. 2270 */ 2271 2272 if (tcr->OPT_length != 0) { 2273 /* Options to process */ 2274 int t_error = 0; 2275 int sys_error = 0; 2276 int do_disconnect = 0; 2277 2278 if (tcp_conprim_opt_process(eager, mp1, 2279 &do_disconnect, &t_error, &sys_error) < 0) { 2280 eager->tcp_accept_error = 1; 2281 if (do_disconnect) { 2282 /* 2283 * An option failed which does not allow 2284 * connection to be accepted. 2285 * 2286 * We allow T_CONN_RES to succeed and 2287 * put a T_DISCON_IND on the eager queue. 2288 */ 2289 ASSERT(t_error == 0 && sys_error == 0); 2290 eager->tcp_send_discon_ind = 1; 2291 } else { 2292 ASSERT(t_error != 0); 2293 freemsg(ok_mp); 2294 /* 2295 * Original mp was either freed or set 2296 * to ok_mp above, so use mp1 instead. 2297 */ 2298 tcp_err_ack(listener, mp1, t_error, sys_error); 2299 goto finish; 2300 } 2301 } 2302 /* 2303 * Most likely success in setting options (except if 2304 * eager->tcp_send_discon_ind set). 2305 * mp1 option buffer represented by OPT_length/offset 2306 * potentially modified and contains results of setting 2307 * options at this point 2308 */ 2309 } 2310 2311 /* We no longer need mp1, since all options processing has passed */ 2312 freemsg(mp1); 2313 2314 putnext(listener->tcp_rq, ok_mp); 2315 2316 mutex_enter(&listener->tcp_eager_lock); 2317 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2318 tcp_t *tail; 2319 mblk_t *conn_ind; 2320 2321 /* 2322 * This path should not be executed if listener and 2323 * acceptor streams are the same. 2324 */ 2325 ASSERT(listener != acceptor); 2326 2327 tcp = listener->tcp_eager_prev_q0; 2328 /* 2329 * listener->tcp_eager_prev_q0 points to the TAIL of the 2330 * deferred T_conn_ind queue. We need to get to the head of 2331 * the queue in order to send up T_conn_ind the same order as 2332 * how the 3WHS is completed. 2333 */ 2334 while (tcp != listener) { 2335 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2336 break; 2337 else 2338 tcp = tcp->tcp_eager_prev_q0; 2339 } 2340 ASSERT(tcp != listener); 2341 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2342 ASSERT(conn_ind != NULL); 2343 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2344 2345 /* Move from q0 to q */ 2346 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2347 listener->tcp_conn_req_cnt_q0--; 2348 listener->tcp_conn_req_cnt_q++; 2349 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2350 tcp->tcp_eager_prev_q0; 2351 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2352 tcp->tcp_eager_next_q0; 2353 tcp->tcp_eager_prev_q0 = NULL; 2354 tcp->tcp_eager_next_q0 = NULL; 2355 tcp->tcp_conn_def_q0 = B_FALSE; 2356 2357 /* Make sure the tcp isn't in the list of droppables */ 2358 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2359 tcp->tcp_eager_prev_drop_q0 == NULL); 2360 2361 /* 2362 * Insert at end of the queue because sockfs sends 2363 * down T_CONN_RES in chronological order. Leaving 2364 * the older conn indications at front of the queue 2365 * helps reducing search time. 2366 */ 2367 tail = listener->tcp_eager_last_q; 2368 if (tail != NULL) 2369 tail->tcp_eager_next_q = tcp; 2370 else 2371 listener->tcp_eager_next_q = tcp; 2372 listener->tcp_eager_last_q = tcp; 2373 tcp->tcp_eager_next_q = NULL; 2374 mutex_exit(&listener->tcp_eager_lock); 2375 putnext(tcp->tcp_rq, conn_ind); 2376 } else { 2377 mutex_exit(&listener->tcp_eager_lock); 2378 } 2379 2380 /* 2381 * Done with the acceptor - free it 2382 * 2383 * Note: from this point on, no access to listener should be made 2384 * as listener can be equal to acceptor. 2385 */ 2386 finish: 2387 ASSERT(acceptor->tcp_detached); 2388 ASSERT(tcps->tcps_g_q != NULL); 2389 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2390 acceptor->tcp_rq = tcps->tcps_g_q; 2391 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2392 (void) tcp_clean_death(acceptor, 0, 2); 2393 CONN_DEC_REF(acceptor->tcp_connp); 2394 2395 /* 2396 * In case we already received a FIN we have to make tcp_rput send 2397 * the ordrel_ind. This will also send up a window update if the window 2398 * has opened up. 2399 * 2400 * In the normal case of a successful connection acceptance 2401 * we give the O_T_BIND_REQ to the read side put procedure as an 2402 * indication that this was just accepted. This tells tcp_rput to 2403 * pass up any data queued in tcp_rcv_list. 2404 * 2405 * In the fringe case where options sent with T_CONN_RES failed and 2406 * we required, we would be indicating a T_DISCON_IND to blow 2407 * away this connection. 2408 */ 2409 2410 /* 2411 * XXX: we currently have a problem if XTI application closes the 2412 * acceptor stream in between. This problem exists in on10-gate also 2413 * and is well know but nothing can be done short of major rewrite 2414 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2415 * eager same squeue as listener (we can distinguish non socket 2416 * listeners at the time of handling a SYN in tcp_conn_request) 2417 * and do most of the work that tcp_accept_finish does here itself 2418 * and then get behind the acceptor squeue to access the acceptor 2419 * queue. 2420 */ 2421 /* 2422 * We already have a ref on tcp so no need to do one before squeue_enter 2423 */ 2424 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2425 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2426 } 2427 2428 /* 2429 * Swap information between the eager and acceptor for a TLI/XTI client. 2430 * The sockfs accept is done on the acceptor stream and control goes 2431 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2432 * called. In either case, both the eager and listener are in their own 2433 * perimeter (squeue) and the code has to deal with potential race. 2434 * 2435 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2436 */ 2437 static void 2438 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2439 { 2440 conn_t *econnp, *aconnp; 2441 2442 ASSERT(eager->tcp_rq == listener->tcp_rq); 2443 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2444 ASSERT(!eager->tcp_hard_bound); 2445 ASSERT(!TCP_IS_SOCKET(acceptor)); 2446 ASSERT(!TCP_IS_SOCKET(eager)); 2447 ASSERT(!TCP_IS_SOCKET(listener)); 2448 2449 acceptor->tcp_detached = B_TRUE; 2450 /* 2451 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2452 * the acceptor id. 2453 */ 2454 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2455 2456 /* remove eager from listen list... */ 2457 mutex_enter(&listener->tcp_eager_lock); 2458 tcp_eager_unlink(eager); 2459 ASSERT(eager->tcp_eager_next_q == NULL && 2460 eager->tcp_eager_last_q == NULL); 2461 ASSERT(eager->tcp_eager_next_q0 == NULL && 2462 eager->tcp_eager_prev_q0 == NULL); 2463 mutex_exit(&listener->tcp_eager_lock); 2464 eager->tcp_rq = acceptor->tcp_rq; 2465 eager->tcp_wq = acceptor->tcp_wq; 2466 2467 econnp = eager->tcp_connp; 2468 aconnp = acceptor->tcp_connp; 2469 2470 eager->tcp_rq->q_ptr = econnp; 2471 eager->tcp_wq->q_ptr = econnp; 2472 2473 /* 2474 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2475 * which might be a different squeue from our peer TCP instance. 2476 * For TCP Fusion, the peer expects that whenever tcp_detached is 2477 * clear, our TCP queues point to the acceptor's queues. Thus, use 2478 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2479 * above reach global visibility prior to the clearing of tcp_detached. 2480 */ 2481 membar_producer(); 2482 eager->tcp_detached = B_FALSE; 2483 2484 ASSERT(eager->tcp_ack_tid == 0); 2485 2486 econnp->conn_dev = aconnp->conn_dev; 2487 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2488 ASSERT(econnp->conn_minor_arena != NULL); 2489 if (eager->tcp_cred != NULL) 2490 crfree(eager->tcp_cred); 2491 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2492 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2493 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2494 2495 aconnp->conn_cred = NULL; 2496 2497 econnp->conn_zoneid = aconnp->conn_zoneid; 2498 econnp->conn_allzones = aconnp->conn_allzones; 2499 2500 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2501 aconnp->conn_mac_exempt = B_FALSE; 2502 2503 ASSERT(aconnp->conn_peercred == NULL); 2504 2505 /* Do the IPC initialization */ 2506 CONN_INC_REF(econnp); 2507 2508 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2509 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2510 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2511 2512 /* Done with old IPC. Drop its ref on its connp */ 2513 CONN_DEC_REF(aconnp); 2514 } 2515 2516 2517 /* 2518 * Adapt to the information, such as rtt and rtt_sd, provided from the 2519 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2520 * 2521 * Checks for multicast and broadcast destination address. 2522 * Returns zero on failure; non-zero if ok. 2523 * 2524 * Note that the MSS calculation here is based on the info given in 2525 * the IRE. We do not do any calculation based on TCP options. They 2526 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2527 * knows which options to use. 2528 * 2529 * Note on how TCP gets its parameters for a connection. 2530 * 2531 * When a tcp_t structure is allocated, it gets all the default parameters. 2532 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2533 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2534 * default. 2535 * 2536 * An incoming SYN with a multicast or broadcast destination address, is dropped 2537 * in 1 of 2 places. 2538 * 2539 * 1. If the packet was received over the wire it is dropped in 2540 * ip_rput_process_broadcast() 2541 * 2542 * 2. If the packet was received through internal IP loopback, i.e. the packet 2543 * was generated and received on the same machine, it is dropped in 2544 * ip_wput_local() 2545 * 2546 * An incoming SYN with a multicast or broadcast source address is always 2547 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2548 * reject an attempt to connect to a broadcast or multicast (destination) 2549 * address. 2550 */ 2551 static int 2552 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2553 { 2554 tcp_hsp_t *hsp; 2555 ire_t *ire; 2556 ire_t *sire = NULL; 2557 iulp_t *ire_uinfo = NULL; 2558 uint32_t mss_max; 2559 uint32_t mss; 2560 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2561 conn_t *connp = tcp->tcp_connp; 2562 boolean_t ire_cacheable = B_FALSE; 2563 zoneid_t zoneid = connp->conn_zoneid; 2564 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2565 MATCH_IRE_SECATTR; 2566 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2567 ill_t *ill = NULL; 2568 boolean_t incoming = (ire_mp == NULL); 2569 tcp_stack_t *tcps = tcp->tcp_tcps; 2570 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2571 2572 ASSERT(connp->conn_ire_cache == NULL); 2573 2574 if (tcp->tcp_ipversion == IPV4_VERSION) { 2575 2576 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2577 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2578 return (0); 2579 } 2580 /* 2581 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2582 * for the destination with the nexthop as gateway. 2583 * ire_ctable_lookup() is used because this particular 2584 * ire, if it exists, will be marked private. 2585 * If that is not available, use the interface ire 2586 * for the nexthop. 2587 * 2588 * TSol: tcp_update_label will detect label mismatches based 2589 * only on the destination's label, but that would not 2590 * detect label mismatches based on the security attributes 2591 * of routes or next hop gateway. Hence we need to pass the 2592 * label to ire_ftable_lookup below in order to locate the 2593 * right prefix (and/or) ire cache. Similarly we also need 2594 * pass the label to the ire_cache_lookup below to locate 2595 * the right ire that also matches on the label. 2596 */ 2597 if (tcp->tcp_connp->conn_nexthop_set) { 2598 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2599 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2600 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2601 ipst); 2602 if (ire == NULL) { 2603 ire = ire_ftable_lookup( 2604 tcp->tcp_connp->conn_nexthop_v4, 2605 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2606 tsl, match_flags, ipst); 2607 if (ire == NULL) 2608 return (0); 2609 } else { 2610 ire_uinfo = &ire->ire_uinfo; 2611 } 2612 } else { 2613 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2614 zoneid, tsl, ipst); 2615 if (ire != NULL) { 2616 ire_cacheable = B_TRUE; 2617 ire_uinfo = (ire_mp != NULL) ? 2618 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2619 &ire->ire_uinfo; 2620 2621 } else { 2622 if (ire_mp == NULL) { 2623 ire = ire_ftable_lookup( 2624 tcp->tcp_connp->conn_rem, 2625 0, 0, 0, NULL, &sire, zoneid, 0, 2626 tsl, (MATCH_IRE_RECURSIVE | 2627 MATCH_IRE_DEFAULT), ipst); 2628 if (ire == NULL) 2629 return (0); 2630 ire_uinfo = (sire != NULL) ? 2631 &sire->ire_uinfo : 2632 &ire->ire_uinfo; 2633 } else { 2634 ire = (ire_t *)ire_mp->b_rptr; 2635 ire_uinfo = 2636 &((ire_t *) 2637 ire_mp->b_rptr)->ire_uinfo; 2638 } 2639 } 2640 } 2641 ASSERT(ire != NULL); 2642 2643 if ((ire->ire_src_addr == INADDR_ANY) || 2644 (ire->ire_type & IRE_BROADCAST)) { 2645 /* 2646 * ire->ire_mp is non null when ire_mp passed in is used 2647 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2648 */ 2649 if (ire->ire_mp == NULL) 2650 ire_refrele(ire); 2651 if (sire != NULL) 2652 ire_refrele(sire); 2653 return (0); 2654 } 2655 2656 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2657 ipaddr_t src_addr; 2658 2659 /* 2660 * ip_bind_connected() has stored the correct source 2661 * address in conn_src. 2662 */ 2663 src_addr = tcp->tcp_connp->conn_src; 2664 tcp->tcp_ipha->ipha_src = src_addr; 2665 /* 2666 * Copy of the src addr. in tcp_t is needed 2667 * for the lookup funcs. 2668 */ 2669 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2670 } 2671 /* 2672 * Set the fragment bit so that IP will tell us if the MTU 2673 * should change. IP tells us the latest setting of 2674 * ip_path_mtu_discovery through ire_frag_flag. 2675 */ 2676 if (ipst->ips_ip_path_mtu_discovery) { 2677 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2678 htons(IPH_DF); 2679 } 2680 /* 2681 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2682 * for IP_NEXTHOP. No cache ire has been found for the 2683 * destination and we are working with the nexthop's 2684 * interface ire. Since we need to forward all packets 2685 * to the nexthop first, we "blindly" set tcp_localnet 2686 * to false, eventhough the destination may also be 2687 * onlink. 2688 */ 2689 if (ire_uinfo == NULL) 2690 tcp->tcp_localnet = 0; 2691 else 2692 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2693 } else { 2694 /* 2695 * For incoming connection ire_mp = NULL 2696 * For outgoing connection ire_mp != NULL 2697 * Technically we should check conn_incoming_ill 2698 * when ire_mp is NULL and conn_outgoing_ill when 2699 * ire_mp is non-NULL. But this is performance 2700 * critical path and for IPV*_BOUND_IF, outgoing 2701 * and incoming ill are always set to the same value. 2702 */ 2703 ill_t *dst_ill = NULL; 2704 ipif_t *dst_ipif = NULL; 2705 2706 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2707 2708 if (connp->conn_outgoing_ill != NULL) { 2709 /* Outgoing or incoming path */ 2710 int err; 2711 2712 dst_ill = conn_get_held_ill(connp, 2713 &connp->conn_outgoing_ill, &err); 2714 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2715 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2716 return (0); 2717 } 2718 match_flags |= MATCH_IRE_ILL; 2719 dst_ipif = dst_ill->ill_ipif; 2720 } 2721 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2722 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2723 2724 if (ire != NULL) { 2725 ire_cacheable = B_TRUE; 2726 ire_uinfo = (ire_mp != NULL) ? 2727 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2728 &ire->ire_uinfo; 2729 } else { 2730 if (ire_mp == NULL) { 2731 ire = ire_ftable_lookup_v6( 2732 &tcp->tcp_connp->conn_remv6, 2733 0, 0, 0, dst_ipif, &sire, zoneid, 2734 0, tsl, match_flags, ipst); 2735 if (ire == NULL) { 2736 if (dst_ill != NULL) 2737 ill_refrele(dst_ill); 2738 return (0); 2739 } 2740 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2741 &ire->ire_uinfo; 2742 } else { 2743 ire = (ire_t *)ire_mp->b_rptr; 2744 ire_uinfo = 2745 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2746 } 2747 } 2748 if (dst_ill != NULL) 2749 ill_refrele(dst_ill); 2750 2751 ASSERT(ire != NULL); 2752 ASSERT(ire_uinfo != NULL); 2753 2754 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2755 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2756 /* 2757 * ire->ire_mp is non null when ire_mp passed in is used 2758 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2759 */ 2760 if (ire->ire_mp == NULL) 2761 ire_refrele(ire); 2762 if (sire != NULL) 2763 ire_refrele(sire); 2764 return (0); 2765 } 2766 2767 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2768 in6_addr_t src_addr; 2769 2770 /* 2771 * ip_bind_connected_v6() has stored the correct source 2772 * address per IPv6 addr. selection policy in 2773 * conn_src_v6. 2774 */ 2775 src_addr = tcp->tcp_connp->conn_srcv6; 2776 2777 tcp->tcp_ip6h->ip6_src = src_addr; 2778 /* 2779 * Copy of the src addr. in tcp_t is needed 2780 * for the lookup funcs. 2781 */ 2782 tcp->tcp_ip_src_v6 = src_addr; 2783 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2784 &connp->conn_srcv6)); 2785 } 2786 tcp->tcp_localnet = 2787 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2788 } 2789 2790 /* 2791 * This allows applications to fail quickly when connections are made 2792 * to dead hosts. Hosts can be labeled dead by adding a reject route 2793 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2794 */ 2795 if ((ire->ire_flags & RTF_REJECT) && 2796 (ire->ire_flags & RTF_PRIVATE)) 2797 goto error; 2798 2799 /* 2800 * Make use of the cached rtt and rtt_sd values to calculate the 2801 * initial RTO. Note that they are already initialized in 2802 * tcp_init_values(). 2803 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2804 * IP_NEXTHOP, but instead are using the interface ire for the 2805 * nexthop, then we do not use the ire_uinfo from that ire to 2806 * do any initializations. 2807 */ 2808 if (ire_uinfo != NULL) { 2809 if (ire_uinfo->iulp_rtt != 0) { 2810 clock_t rto; 2811 2812 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2813 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2814 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2815 tcps->tcps_rexmit_interval_extra + 2816 (tcp->tcp_rtt_sa >> 5); 2817 2818 if (rto > tcps->tcps_rexmit_interval_max) { 2819 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2820 } else if (rto < tcps->tcps_rexmit_interval_min) { 2821 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2822 } else { 2823 tcp->tcp_rto = rto; 2824 } 2825 } 2826 if (ire_uinfo->iulp_ssthresh != 0) 2827 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2828 else 2829 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2830 if (ire_uinfo->iulp_spipe > 0) { 2831 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2832 tcps->tcps_max_buf); 2833 if (tcps->tcps_snd_lowat_fraction != 0) 2834 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2835 tcps->tcps_snd_lowat_fraction; 2836 (void) tcp_maxpsz_set(tcp, B_TRUE); 2837 } 2838 /* 2839 * Note that up till now, acceptor always inherits receive 2840 * window from the listener. But if there is a metrics 2841 * associated with a host, we should use that instead of 2842 * inheriting it from listener. Thus we need to pass this 2843 * info back to the caller. 2844 */ 2845 if (ire_uinfo->iulp_rpipe > 0) { 2846 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2847 tcps->tcps_max_buf); 2848 } 2849 2850 if (ire_uinfo->iulp_rtomax > 0) { 2851 tcp->tcp_second_timer_threshold = 2852 ire_uinfo->iulp_rtomax; 2853 } 2854 2855 /* 2856 * Use the metric option settings, iulp_tstamp_ok and 2857 * iulp_wscale_ok, only for active open. What this means 2858 * is that if the other side uses timestamp or window 2859 * scale option, TCP will also use those options. That 2860 * is for passive open. If the application sets a 2861 * large window, window scale is enabled regardless of 2862 * the value in iulp_wscale_ok. This is the behavior 2863 * since 2.6. So we keep it. 2864 * The only case left in passive open processing is the 2865 * check for SACK. 2866 * For ECN, it should probably be like SACK. But the 2867 * current value is binary, so we treat it like the other 2868 * cases. The metric only controls active open.For passive 2869 * open, the ndd param, tcp_ecn_permitted, controls the 2870 * behavior. 2871 */ 2872 if (!tcp_detached) { 2873 /* 2874 * The if check means that the following can only 2875 * be turned on by the metrics only IRE, but not off. 2876 */ 2877 if (ire_uinfo->iulp_tstamp_ok) 2878 tcp->tcp_snd_ts_ok = B_TRUE; 2879 if (ire_uinfo->iulp_wscale_ok) 2880 tcp->tcp_snd_ws_ok = B_TRUE; 2881 if (ire_uinfo->iulp_sack == 2) 2882 tcp->tcp_snd_sack_ok = B_TRUE; 2883 if (ire_uinfo->iulp_ecn_ok) 2884 tcp->tcp_ecn_ok = B_TRUE; 2885 } else { 2886 /* 2887 * Passive open. 2888 * 2889 * As above, the if check means that SACK can only be 2890 * turned on by the metric only IRE. 2891 */ 2892 if (ire_uinfo->iulp_sack > 0) { 2893 tcp->tcp_snd_sack_ok = B_TRUE; 2894 } 2895 } 2896 } 2897 2898 2899 /* 2900 * XXX: Note that currently, ire_max_frag can be as small as 68 2901 * because of PMTUd. So tcp_mss may go to negative if combined 2902 * length of all those options exceeds 28 bytes. But because 2903 * of the tcp_mss_min check below, we may not have a problem if 2904 * tcp_mss_min is of a reasonable value. The default is 1 so 2905 * the negative problem still exists. And the check defeats PMTUd. 2906 * In fact, if PMTUd finds that the MSS should be smaller than 2907 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2908 * value. 2909 * 2910 * We do not deal with that now. All those problems related to 2911 * PMTUd will be fixed later. 2912 */ 2913 ASSERT(ire->ire_max_frag != 0); 2914 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2915 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2916 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2917 mss = MIN(mss, IPV6_MIN_MTU); 2918 } 2919 } 2920 2921 /* Sanity check for MSS value. */ 2922 if (tcp->tcp_ipversion == IPV4_VERSION) 2923 mss_max = tcps->tcps_mss_max_ipv4; 2924 else 2925 mss_max = tcps->tcps_mss_max_ipv6; 2926 2927 if (tcp->tcp_ipversion == IPV6_VERSION && 2928 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2929 /* 2930 * After receiving an ICMPv6 "packet too big" message with a 2931 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2932 * will insert a 8-byte fragment header in every packet; we 2933 * reduce the MSS by that amount here. 2934 */ 2935 mss -= sizeof (ip6_frag_t); 2936 } 2937 2938 if (tcp->tcp_ipsec_overhead == 0) 2939 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2940 2941 mss -= tcp->tcp_ipsec_overhead; 2942 2943 if (mss < tcps->tcps_mss_min) 2944 mss = tcps->tcps_mss_min; 2945 if (mss > mss_max) 2946 mss = mss_max; 2947 2948 /* Note that this is the maximum MSS, excluding all options. */ 2949 tcp->tcp_mss = mss; 2950 2951 /* 2952 * Initialize the ISS here now that we have the full connection ID. 2953 * The RFC 1948 method of initial sequence number generation requires 2954 * knowledge of the full connection ID before setting the ISS. 2955 */ 2956 2957 tcp_iss_init(tcp); 2958 2959 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2960 tcp->tcp_loopback = B_TRUE; 2961 2962 if (tcp->tcp_ipversion == IPV4_VERSION) { 2963 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2964 } else { 2965 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2966 } 2967 2968 if (hsp != NULL) { 2969 /* Only modify if we're going to make them bigger */ 2970 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2971 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2972 if (tcps->tcps_snd_lowat_fraction != 0) 2973 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2974 tcps->tcps_snd_lowat_fraction; 2975 } 2976 2977 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2978 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2979 } 2980 2981 /* Copy timestamp flag only for active open */ 2982 if (!tcp_detached) 2983 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2984 } 2985 2986 if (sire != NULL) 2987 IRE_REFRELE(sire); 2988 2989 /* 2990 * If we got an IRE_CACHE and an ILL, go through their properties; 2991 * otherwise, this is deferred until later when we have an IRE_CACHE. 2992 */ 2993 if (tcp->tcp_loopback || 2994 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2995 /* 2996 * For incoming, see if this tcp may be MDT-capable. For 2997 * outgoing, this process has been taken care of through 2998 * tcp_rput_other. 2999 */ 3000 tcp_ire_ill_check(tcp, ire, ill, incoming); 3001 tcp->tcp_ire_ill_check_done = B_TRUE; 3002 } 3003 3004 mutex_enter(&connp->conn_lock); 3005 /* 3006 * Make sure that conn is not marked incipient 3007 * for incoming connections. A blind 3008 * removal of incipient flag is cheaper than 3009 * check and removal. 3010 */ 3011 connp->conn_state_flags &= ~CONN_INCIPIENT; 3012 3013 /* 3014 * Must not cache forwarding table routes 3015 * or recache an IRE after the conn_t has 3016 * had conn_ire_cache cleared and is flagged 3017 * unusable, (see the CONN_CACHE_IRE() macro). 3018 */ 3019 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 3020 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 3021 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3022 connp->conn_ire_cache = ire; 3023 IRE_UNTRACE_REF(ire); 3024 rw_exit(&ire->ire_bucket->irb_lock); 3025 mutex_exit(&connp->conn_lock); 3026 return (1); 3027 } 3028 rw_exit(&ire->ire_bucket->irb_lock); 3029 } 3030 mutex_exit(&connp->conn_lock); 3031 3032 if (ire->ire_mp == NULL) 3033 ire_refrele(ire); 3034 return (1); 3035 3036 error: 3037 if (ire->ire_mp == NULL) 3038 ire_refrele(ire); 3039 if (sire != NULL) 3040 ire_refrele(sire); 3041 return (0); 3042 } 3043 3044 static void 3045 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 3046 { 3047 int error; 3048 conn_t *connp = tcp->tcp_connp; 3049 struct sockaddr *sa; 3050 mblk_t *mp1; 3051 struct T_bind_req *tbr; 3052 int backlog; 3053 socklen_t len; 3054 sin_t *sin; 3055 sin6_t *sin6; 3056 3057 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3058 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3059 if (tcp->tcp_debug) { 3060 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3061 "tcp_tpi_bind: bad req, len %u", 3062 (uint_t)(mp->b_wptr - mp->b_rptr)); 3063 } 3064 tcp_err_ack(tcp, mp, TPROTO, 0); 3065 return; 3066 } 3067 /* Make sure the largest address fits */ 3068 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3069 if (mp1 == NULL) { 3070 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3071 return; 3072 } 3073 mp = mp1; 3074 tbr = (struct T_bind_req *)mp->b_rptr; 3075 3076 backlog = tbr->CONIND_number; 3077 len = tbr->ADDR_length; 3078 3079 switch (len) { 3080 case 0: /* request for a generic port */ 3081 tbr->ADDR_offset = sizeof (struct T_bind_req); 3082 if (tcp->tcp_family == AF_INET) { 3083 tbr->ADDR_length = sizeof (sin_t); 3084 sin = (sin_t *)&tbr[1]; 3085 *sin = sin_null; 3086 sin->sin_family = AF_INET; 3087 sa = (struct sockaddr *)sin; 3088 len = sizeof (sin_t); 3089 mp->b_wptr = (uchar_t *)&sin[1]; 3090 } else { 3091 ASSERT(tcp->tcp_family == AF_INET6); 3092 tbr->ADDR_length = sizeof (sin6_t); 3093 sin6 = (sin6_t *)&tbr[1]; 3094 *sin6 = sin6_null; 3095 sin6->sin6_family = AF_INET6; 3096 sa = (struct sockaddr *)sin6; 3097 len = sizeof (sin6_t); 3098 mp->b_wptr = (uchar_t *)&sin6[1]; 3099 } 3100 break; 3101 3102 case sizeof (sin_t): /* Complete IPv4 address */ 3103 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3104 sizeof (sin_t)); 3105 break; 3106 3107 case sizeof (sin6_t): /* Complete IPv6 address */ 3108 sa = (struct sockaddr *)mi_offset_param(mp, 3109 tbr->ADDR_offset, sizeof (sin6_t)); 3110 break; 3111 3112 default: 3113 if (tcp->tcp_debug) { 3114 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3115 "tcp_tpi_bind: bad address length, %d", 3116 tbr->ADDR_length); 3117 } 3118 tcp_err_ack(tcp, mp, TBADADDR, 0); 3119 return; 3120 } 3121 3122 error = tcp_bind_check(connp, sa, len, DB_CRED(mp), 3123 tbr->PRIM_type != O_T_BIND_REQ); 3124 if (error == 0) { 3125 if (tcp->tcp_family == AF_INET) { 3126 sin = (sin_t *)sa; 3127 sin->sin_port = tcp->tcp_lport; 3128 } else { 3129 sin6 = (sin6_t *)sa; 3130 sin6->sin6_port = tcp->tcp_lport; 3131 } 3132 3133 if (backlog > 0) { 3134 error = tcp_do_listen(connp, backlog, DB_CRED(mp)); 3135 } 3136 } 3137 done: 3138 if (error > 0) { 3139 tcp_err_ack(tcp, mp, TSYSERR, error); 3140 } else if (error < 0) { 3141 tcp_err_ack(tcp, mp, -error, 0); 3142 } else { 3143 mp->b_datap->db_type = M_PCPROTO; 3144 tbr->PRIM_type = T_BIND_ACK; 3145 putnext(tcp->tcp_rq, mp); 3146 } 3147 } 3148 3149 /* 3150 * If the "bind_to_req_port_only" parameter is set, if the requested port 3151 * number is available, return it, If not return 0 3152 * 3153 * If "bind_to_req_port_only" parameter is not set and 3154 * If the requested port number is available, return it. If not, return 3155 * the first anonymous port we happen across. If no anonymous ports are 3156 * available, return 0. addr is the requested local address, if any. 3157 * 3158 * In either case, when succeeding update the tcp_t to record the port number 3159 * and insert it in the bind hash table. 3160 * 3161 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3162 * without setting SO_REUSEADDR. This is needed so that they 3163 * can be viewed as two independent transport protocols. 3164 */ 3165 static in_port_t 3166 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3167 int reuseaddr, boolean_t quick_connect, 3168 boolean_t bind_to_req_port_only, boolean_t user_specified) 3169 { 3170 /* number of times we have run around the loop */ 3171 int count = 0; 3172 /* maximum number of times to run around the loop */ 3173 int loopmax; 3174 conn_t *connp = tcp->tcp_connp; 3175 zoneid_t zoneid = connp->conn_zoneid; 3176 tcp_stack_t *tcps = tcp->tcp_tcps; 3177 3178 /* 3179 * Lookup for free addresses is done in a loop and "loopmax" 3180 * influences how long we spin in the loop 3181 */ 3182 if (bind_to_req_port_only) { 3183 /* 3184 * If the requested port is busy, don't bother to look 3185 * for a new one. Setting loop maximum count to 1 has 3186 * that effect. 3187 */ 3188 loopmax = 1; 3189 } else { 3190 /* 3191 * If the requested port is busy, look for a free one 3192 * in the anonymous port range. 3193 * Set loopmax appropriately so that one does not look 3194 * forever in the case all of the anonymous ports are in use. 3195 */ 3196 if (tcp->tcp_anon_priv_bind) { 3197 /* 3198 * loopmax = 3199 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3200 */ 3201 loopmax = IPPORT_RESERVED - 3202 tcps->tcps_min_anonpriv_port; 3203 } else { 3204 loopmax = (tcps->tcps_largest_anon_port - 3205 tcps->tcps_smallest_anon_port + 1); 3206 } 3207 } 3208 do { 3209 uint16_t lport; 3210 tf_t *tbf; 3211 tcp_t *ltcp; 3212 conn_t *lconnp; 3213 3214 lport = htons(port); 3215 3216 /* 3217 * Ensure that the tcp_t is not currently in the bind hash. 3218 * Hold the lock on the hash bucket to ensure that 3219 * the duplicate check plus the insertion is an atomic 3220 * operation. 3221 * 3222 * This function does an inline lookup on the bind hash list 3223 * Make sure that we access only members of tcp_t 3224 * and that we don't look at tcp_tcp, since we are not 3225 * doing a CONN_INC_REF. 3226 */ 3227 tcp_bind_hash_remove(tcp); 3228 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3229 mutex_enter(&tbf->tf_lock); 3230 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3231 ltcp = ltcp->tcp_bind_hash) { 3232 if (lport == ltcp->tcp_lport) 3233 break; 3234 } 3235 3236 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3237 boolean_t not_socket; 3238 boolean_t exclbind; 3239 3240 lconnp = ltcp->tcp_connp; 3241 3242 /* 3243 * On a labeled system, we must treat bindings to ports 3244 * on shared IP addresses by sockets with MAC exemption 3245 * privilege as being in all zones, as there's 3246 * otherwise no way to identify the right receiver. 3247 */ 3248 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3249 IPCL_ZONE_MATCH(connp, 3250 ltcp->tcp_connp->conn_zoneid)) && 3251 !lconnp->conn_mac_exempt && 3252 !connp->conn_mac_exempt) 3253 continue; 3254 3255 /* 3256 * If TCP_EXCLBIND is set for either the bound or 3257 * binding endpoint, the semantics of bind 3258 * is changed according to the following. 3259 * 3260 * spec = specified address (v4 or v6) 3261 * unspec = unspecified address (v4 or v6) 3262 * A = specified addresses are different for endpoints 3263 * 3264 * bound bind to allowed 3265 * ------------------------------------- 3266 * unspec unspec no 3267 * unspec spec no 3268 * spec unspec no 3269 * spec spec yes if A 3270 * 3271 * For labeled systems, SO_MAC_EXEMPT behaves the same 3272 * as TCP_EXCLBIND, except that zoneid is ignored. 3273 * 3274 * Note: 3275 * 3276 * 1. Because of TLI semantics, an endpoint can go 3277 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3278 * TCPS_BOUND, depending on whether it is originally 3279 * a listener or not. That is why we need to check 3280 * for states greater than or equal to TCPS_BOUND 3281 * here. 3282 * 3283 * 2. Ideally, we should only check for state equals 3284 * to TCPS_LISTEN. And the following check should be 3285 * added. 3286 * 3287 * if (ltcp->tcp_state == TCPS_LISTEN || 3288 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3289 * ... 3290 * } 3291 * 3292 * The semantics will be changed to this. If the 3293 * endpoint on the list is in state not equal to 3294 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3295 * set, let the bind succeed. 3296 * 3297 * Because of (1), we cannot do that for TLI 3298 * endpoints. But we can do that for socket endpoints. 3299 * If in future, we can change this going back 3300 * semantics, we can use the above check for TLI also. 3301 */ 3302 not_socket = !(TCP_IS_SOCKET(ltcp) && 3303 TCP_IS_SOCKET(tcp)); 3304 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3305 3306 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3307 (exclbind && (not_socket || 3308 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3309 if (V6_OR_V4_INADDR_ANY( 3310 ltcp->tcp_bound_source_v6) || 3311 V6_OR_V4_INADDR_ANY(*laddr) || 3312 IN6_ARE_ADDR_EQUAL(laddr, 3313 <cp->tcp_bound_source_v6)) { 3314 break; 3315 } 3316 continue; 3317 } 3318 3319 /* 3320 * Check ipversion to allow IPv4 and IPv6 sockets to 3321 * have disjoint port number spaces, if *_EXCLBIND 3322 * is not set and only if the application binds to a 3323 * specific port. We use the same autoassigned port 3324 * number space for IPv4 and IPv6 sockets. 3325 */ 3326 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3327 bind_to_req_port_only) 3328 continue; 3329 3330 /* 3331 * Ideally, we should make sure that the source 3332 * address, remote address, and remote port in the 3333 * four tuple for this tcp-connection is unique. 3334 * However, trying to find out the local source 3335 * address would require too much code duplication 3336 * with IP, since IP needs needs to have that code 3337 * to support userland TCP implementations. 3338 */ 3339 if (quick_connect && 3340 (ltcp->tcp_state > TCPS_LISTEN) && 3341 ((tcp->tcp_fport != ltcp->tcp_fport) || 3342 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3343 <cp->tcp_remote_v6))) 3344 continue; 3345 3346 if (!reuseaddr) { 3347 /* 3348 * No socket option SO_REUSEADDR. 3349 * If existing port is bound to 3350 * a non-wildcard IP address 3351 * and the requesting stream is 3352 * bound to a distinct 3353 * different IP addresses 3354 * (non-wildcard, also), keep 3355 * going. 3356 */ 3357 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3358 !V6_OR_V4_INADDR_ANY( 3359 ltcp->tcp_bound_source_v6) && 3360 !IN6_ARE_ADDR_EQUAL(laddr, 3361 <cp->tcp_bound_source_v6)) 3362 continue; 3363 if (ltcp->tcp_state >= TCPS_BOUND) { 3364 /* 3365 * This port is being used and 3366 * its state is >= TCPS_BOUND, 3367 * so we can't bind to it. 3368 */ 3369 break; 3370 } 3371 } else { 3372 /* 3373 * socket option SO_REUSEADDR is set on the 3374 * binding tcp_t. 3375 * 3376 * If two streams are bound to 3377 * same IP address or both addr 3378 * and bound source are wildcards 3379 * (INADDR_ANY), we want to stop 3380 * searching. 3381 * We have found a match of IP source 3382 * address and source port, which is 3383 * refused regardless of the 3384 * SO_REUSEADDR setting, so we break. 3385 */ 3386 if (IN6_ARE_ADDR_EQUAL(laddr, 3387 <cp->tcp_bound_source_v6) && 3388 (ltcp->tcp_state == TCPS_LISTEN || 3389 ltcp->tcp_state == TCPS_BOUND)) 3390 break; 3391 } 3392 } 3393 if (ltcp != NULL) { 3394 /* The port number is busy */ 3395 mutex_exit(&tbf->tf_lock); 3396 } else { 3397 /* 3398 * This port is ours. Insert in fanout and mark as 3399 * bound to prevent others from getting the port 3400 * number. 3401 */ 3402 tcp->tcp_state = TCPS_BOUND; 3403 tcp->tcp_lport = htons(port); 3404 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3405 3406 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3407 tcp->tcp_lport)] == tbf); 3408 tcp_bind_hash_insert(tbf, tcp, 1); 3409 3410 mutex_exit(&tbf->tf_lock); 3411 3412 /* 3413 * We don't want tcp_next_port_to_try to "inherit" 3414 * a port number supplied by the user in a bind. 3415 */ 3416 if (user_specified) 3417 return (port); 3418 3419 /* 3420 * This is the only place where tcp_next_port_to_try 3421 * is updated. After the update, it may or may not 3422 * be in the valid range. 3423 */ 3424 if (!tcp->tcp_anon_priv_bind) 3425 tcps->tcps_next_port_to_try = port + 1; 3426 return (port); 3427 } 3428 3429 if (tcp->tcp_anon_priv_bind) { 3430 port = tcp_get_next_priv_port(tcp); 3431 } else { 3432 if (count == 0 && user_specified) { 3433 /* 3434 * We may have to return an anonymous port. So 3435 * get one to start with. 3436 */ 3437 port = 3438 tcp_update_next_port( 3439 tcps->tcps_next_port_to_try, 3440 tcp, B_TRUE); 3441 user_specified = B_FALSE; 3442 } else { 3443 port = tcp_update_next_port(port + 1, tcp, 3444 B_FALSE); 3445 } 3446 } 3447 if (port == 0) 3448 break; 3449 3450 /* 3451 * Don't let this loop run forever in the case where 3452 * all of the anonymous ports are in use. 3453 */ 3454 } while (++count < loopmax); 3455 return (0); 3456 } 3457 3458 /* 3459 * tcp_clean_death / tcp_close_detached must not be called more than once 3460 * on a tcp. Thus every function that potentially calls tcp_clean_death 3461 * must check for the tcp state before calling tcp_clean_death. 3462 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3463 * tcp_timer_handler, all check for the tcp state. 3464 */ 3465 /* ARGSUSED */ 3466 void 3467 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3468 { 3469 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3470 3471 freemsg(mp); 3472 if (tcp->tcp_state > TCPS_BOUND) 3473 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3474 ETIMEDOUT, 5); 3475 } 3476 3477 /* 3478 * We are dying for some reason. Try to do it gracefully. (May be called 3479 * as writer.) 3480 * 3481 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3482 * done by a service procedure). 3483 * TBD - Should the return value distinguish between the tcp_t being 3484 * freed and it being reinitialized? 3485 */ 3486 static int 3487 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3488 { 3489 mblk_t *mp; 3490 queue_t *q; 3491 conn_t *connp = tcp->tcp_connp; 3492 tcp_stack_t *tcps = tcp->tcp_tcps; 3493 sodirect_t *sodp; 3494 3495 TCP_CLD_STAT(tag); 3496 3497 #if TCP_TAG_CLEAN_DEATH 3498 tcp->tcp_cleandeathtag = tag; 3499 #endif 3500 3501 if (tcp->tcp_fused) 3502 tcp_unfuse(tcp); 3503 3504 if (tcp->tcp_linger_tid != 0 && 3505 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3506 tcp_stop_lingering(tcp); 3507 } 3508 3509 ASSERT(tcp != NULL); 3510 ASSERT((tcp->tcp_family == AF_INET && 3511 tcp->tcp_ipversion == IPV4_VERSION) || 3512 (tcp->tcp_family == AF_INET6 && 3513 (tcp->tcp_ipversion == IPV4_VERSION || 3514 tcp->tcp_ipversion == IPV6_VERSION))); 3515 3516 if (TCP_IS_DETACHED(tcp)) { 3517 if (tcp->tcp_hard_binding) { 3518 /* 3519 * Its an eager that we are dealing with. We close the 3520 * eager but in case a conn_ind has already gone to the 3521 * listener, let tcp_accept_finish() send a discon_ind 3522 * to the listener and drop the last reference. If the 3523 * listener doesn't even know about the eager i.e. the 3524 * conn_ind hasn't gone up, blow away the eager and drop 3525 * the last reference as well. If the conn_ind has gone 3526 * up, state should be BOUND. tcp_accept_finish 3527 * will figure out that the connection has received a 3528 * RST and will send a DISCON_IND to the application. 3529 */ 3530 tcp_closei_local(tcp); 3531 if (!tcp->tcp_tconnind_started) { 3532 CONN_DEC_REF(connp); 3533 } else { 3534 tcp->tcp_state = TCPS_BOUND; 3535 } 3536 } else { 3537 tcp_close_detached(tcp); 3538 } 3539 return (0); 3540 } 3541 3542 TCP_STAT(tcps, tcp_clean_death_nondetached); 3543 3544 /* If sodirect, not anymore */ 3545 SOD_PTR_ENTER(tcp, sodp); 3546 if (sodp != NULL) { 3547 tcp->tcp_sodirect = NULL; 3548 mutex_exit(sodp->sod_lockp); 3549 } 3550 3551 q = tcp->tcp_rq; 3552 3553 /* Trash all inbound data */ 3554 if (!IPCL_IS_NONSTR(connp)) { 3555 ASSERT(q != NULL); 3556 flushq(q, FLUSHALL); 3557 } 3558 3559 /* 3560 * If we are at least part way open and there is error 3561 * (err==0 implies no error) 3562 * notify our client by a T_DISCON_IND. 3563 */ 3564 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3565 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3566 !TCP_IS_SOCKET(tcp)) { 3567 /* 3568 * Send M_FLUSH according to TPI. Because sockets will 3569 * (and must) ignore FLUSHR we do that only for TPI 3570 * endpoints and sockets in STREAMS mode. 3571 */ 3572 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3573 } 3574 if (tcp->tcp_debug) { 3575 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3576 "tcp_clean_death: discon err %d", err); 3577 } 3578 if (IPCL_IS_NONSTR(connp)) { 3579 /* Direct socket, use upcall */ 3580 (*connp->conn_upcalls->su_disconnected)( 3581 connp->conn_upper_handle, tcp->tcp_connid, err); 3582 } else { 3583 mp = mi_tpi_discon_ind(NULL, err, 0); 3584 if (mp != NULL) { 3585 putnext(q, mp); 3586 } else { 3587 if (tcp->tcp_debug) { 3588 (void) strlog(TCP_MOD_ID, 0, 1, 3589 SL_ERROR|SL_TRACE, 3590 "tcp_clean_death, sending M_ERROR"); 3591 } 3592 (void) putnextctl1(q, M_ERROR, EPROTO); 3593 } 3594 } 3595 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3596 /* SYN_SENT or SYN_RCVD */ 3597 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3598 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3599 /* ESTABLISHED or CLOSE_WAIT */ 3600 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3601 } 3602 } 3603 3604 tcp_reinit(tcp); 3605 if (IPCL_IS_NONSTR(connp)) 3606 (void) tcp_do_unbind(connp); 3607 3608 return (-1); 3609 } 3610 3611 /* 3612 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3613 * to expire, stop the wait and finish the close. 3614 */ 3615 static void 3616 tcp_stop_lingering(tcp_t *tcp) 3617 { 3618 clock_t delta = 0; 3619 tcp_stack_t *tcps = tcp->tcp_tcps; 3620 3621 tcp->tcp_linger_tid = 0; 3622 if (tcp->tcp_state > TCPS_LISTEN) { 3623 tcp_acceptor_hash_remove(tcp); 3624 mutex_enter(&tcp->tcp_non_sq_lock); 3625 if (tcp->tcp_flow_stopped) { 3626 tcp_clrqfull(tcp); 3627 } 3628 mutex_exit(&tcp->tcp_non_sq_lock); 3629 3630 if (tcp->tcp_timer_tid != 0) { 3631 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3632 tcp->tcp_timer_tid = 0; 3633 } 3634 /* 3635 * Need to cancel those timers which will not be used when 3636 * TCP is detached. This has to be done before the tcp_wq 3637 * is set to the global queue. 3638 */ 3639 tcp_timers_stop(tcp); 3640 3641 tcp->tcp_detached = B_TRUE; 3642 ASSERT(tcps->tcps_g_q != NULL); 3643 tcp->tcp_rq = tcps->tcps_g_q; 3644 tcp->tcp_wq = WR(tcps->tcps_g_q); 3645 3646 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3647 tcp_time_wait_append(tcp); 3648 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3649 goto finish; 3650 } 3651 3652 /* 3653 * If delta is zero the timer event wasn't executed and was 3654 * successfully canceled. In this case we need to restart it 3655 * with the minimal delta possible. 3656 */ 3657 if (delta >= 0) { 3658 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3659 delta ? delta : 1); 3660 } 3661 } else { 3662 tcp_closei_local(tcp); 3663 CONN_DEC_REF(tcp->tcp_connp); 3664 } 3665 finish: 3666 /* Signal closing thread that it can complete close */ 3667 mutex_enter(&tcp->tcp_closelock); 3668 tcp->tcp_detached = B_TRUE; 3669 ASSERT(tcps->tcps_g_q != NULL); 3670 3671 tcp->tcp_rq = tcps->tcps_g_q; 3672 tcp->tcp_wq = WR(tcps->tcps_g_q); 3673 3674 tcp->tcp_closed = 1; 3675 cv_signal(&tcp->tcp_closecv); 3676 mutex_exit(&tcp->tcp_closelock); 3677 } 3678 3679 /* 3680 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3681 * expires. 3682 */ 3683 static void 3684 tcp_close_linger_timeout(void *arg) 3685 { 3686 conn_t *connp = (conn_t *)arg; 3687 tcp_t *tcp = connp->conn_tcp; 3688 3689 tcp->tcp_client_errno = ETIMEDOUT; 3690 tcp_stop_lingering(tcp); 3691 } 3692 3693 static void 3694 tcp_close_common(conn_t *connp, int flags) 3695 { 3696 tcp_t *tcp = connp->conn_tcp; 3697 mblk_t *mp = &tcp->tcp_closemp; 3698 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3699 mblk_t *bp; 3700 3701 ASSERT(connp->conn_ref >= 2); 3702 3703 /* 3704 * Mark the conn as closing. ill_pending_mp_add will not 3705 * add any mp to the pending mp list, after this conn has 3706 * started closing. Same for sq_pending_mp_add 3707 */ 3708 mutex_enter(&connp->conn_lock); 3709 connp->conn_state_flags |= CONN_CLOSING; 3710 if (connp->conn_oper_pending_ill != NULL) 3711 conn_ioctl_cleanup_reqd = B_TRUE; 3712 CONN_INC_REF_LOCKED(connp); 3713 mutex_exit(&connp->conn_lock); 3714 tcp->tcp_closeflags = (uint8_t)flags; 3715 ASSERT(connp->conn_ref >= 3); 3716 3717 /* 3718 * tcp_closemp_used is used below without any protection of a lock 3719 * as we don't expect any one else to use it concurrently at this 3720 * point otherwise it would be a major defect. 3721 */ 3722 3723 if (mp->b_prev == NULL) 3724 tcp->tcp_closemp_used = B_TRUE; 3725 else 3726 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3727 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3728 3729 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3730 3731 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3732 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3733 3734 mutex_enter(&tcp->tcp_closelock); 3735 while (!tcp->tcp_closed) { 3736 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3737 /* 3738 * The cv_wait_sig() was interrupted. We now do the 3739 * following: 3740 * 3741 * 1) If the endpoint was lingering, we allow this 3742 * to be interrupted by cancelling the linger timeout 3743 * and closing normally. 3744 * 3745 * 2) Revert to calling cv_wait() 3746 * 3747 * We revert to using cv_wait() to avoid an 3748 * infinite loop which can occur if the calling 3749 * thread is higher priority than the squeue worker 3750 * thread and is bound to the same cpu. 3751 */ 3752 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3753 mutex_exit(&tcp->tcp_closelock); 3754 /* Entering squeue, bump ref count. */ 3755 CONN_INC_REF(connp); 3756 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3757 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3758 tcp_linger_interrupted, connp, 3759 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3760 mutex_enter(&tcp->tcp_closelock); 3761 } 3762 break; 3763 } 3764 } 3765 while (!tcp->tcp_closed) 3766 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3767 mutex_exit(&tcp->tcp_closelock); 3768 3769 /* 3770 * In the case of listener streams that have eagers in the q or q0 3771 * we wait for the eagers to drop their reference to us. tcp_rq and 3772 * tcp_wq of the eagers point to our queues. By waiting for the 3773 * refcnt to drop to 1, we are sure that the eagers have cleaned 3774 * up their queue pointers and also dropped their references to us. 3775 */ 3776 if (tcp->tcp_wait_for_eagers) { 3777 mutex_enter(&connp->conn_lock); 3778 while (connp->conn_ref != 1) { 3779 cv_wait(&connp->conn_cv, &connp->conn_lock); 3780 } 3781 mutex_exit(&connp->conn_lock); 3782 } 3783 /* 3784 * ioctl cleanup. The mp is queued in the 3785 * ill_pending_mp or in the sq_pending_mp. 3786 */ 3787 if (conn_ioctl_cleanup_reqd) 3788 conn_ioctl_cleanup(connp); 3789 3790 tcp->tcp_cpid = -1; 3791 } 3792 3793 static int 3794 tcp_tpi_close(queue_t *q, int flags) 3795 { 3796 conn_t *connp; 3797 3798 ASSERT(WR(q)->q_next == NULL); 3799 3800 if (flags & SO_FALLBACK) { 3801 /* 3802 * stream is being closed while in fallback 3803 * simply free the resources that were allocated 3804 */ 3805 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3806 qprocsoff(q); 3807 goto done; 3808 } 3809 3810 connp = Q_TO_CONN(q); 3811 /* 3812 * We are being closed as /dev/tcp or /dev/tcp6. 3813 */ 3814 tcp_close_common(connp, flags); 3815 3816 qprocsoff(q); 3817 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3818 3819 /* 3820 * Drop IP's reference on the conn. This is the last reference 3821 * on the connp if the state was less than established. If the 3822 * connection has gone into timewait state, then we will have 3823 * one ref for the TCP and one more ref (total of two) for the 3824 * classifier connected hash list (a timewait connections stays 3825 * in connected hash till closed). 3826 * 3827 * We can't assert the references because there might be other 3828 * transient reference places because of some walkers or queued 3829 * packets in squeue for the timewait state. 3830 */ 3831 CONN_DEC_REF(connp); 3832 done: 3833 q->q_ptr = WR(q)->q_ptr = NULL; 3834 return (0); 3835 } 3836 3837 static int 3838 tcpclose_accept(queue_t *q) 3839 { 3840 vmem_t *minor_arena; 3841 dev_t conn_dev; 3842 3843 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3844 3845 /* 3846 * We had opened an acceptor STREAM for sockfs which is 3847 * now being closed due to some error. 3848 */ 3849 qprocsoff(q); 3850 3851 minor_arena = (vmem_t *)WR(q)->q_ptr; 3852 conn_dev = (dev_t)RD(q)->q_ptr; 3853 ASSERT(minor_arena != NULL); 3854 ASSERT(conn_dev != 0); 3855 inet_minor_free(minor_arena, conn_dev); 3856 q->q_ptr = WR(q)->q_ptr = NULL; 3857 return (0); 3858 } 3859 3860 /* 3861 * Called by tcp_close() routine via squeue when lingering is 3862 * interrupted by a signal. 3863 */ 3864 3865 /* ARGSUSED */ 3866 static void 3867 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3868 { 3869 conn_t *connp = (conn_t *)arg; 3870 tcp_t *tcp = connp->conn_tcp; 3871 3872 freeb(mp); 3873 if (tcp->tcp_linger_tid != 0 && 3874 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3875 tcp_stop_lingering(tcp); 3876 tcp->tcp_client_errno = EINTR; 3877 } 3878 } 3879 3880 /* 3881 * Called by streams close routine via squeues when our client blows off her 3882 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3883 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3884 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3885 * acked. 3886 * 3887 * NOTE: tcp_close potentially returns error when lingering. 3888 * However, the stream head currently does not pass these errors 3889 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3890 * errors to the application (from tsleep()) and not errors 3891 * like ECONNRESET caused by receiving a reset packet. 3892 */ 3893 3894 /* ARGSUSED */ 3895 static void 3896 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3897 { 3898 char *msg; 3899 conn_t *connp = (conn_t *)arg; 3900 tcp_t *tcp = connp->conn_tcp; 3901 clock_t delta = 0; 3902 tcp_stack_t *tcps = tcp->tcp_tcps; 3903 3904 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3905 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3906 3907 mutex_enter(&tcp->tcp_eager_lock); 3908 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3909 /* Cleanup for listener */ 3910 tcp_eager_cleanup(tcp, 0); 3911 tcp->tcp_wait_for_eagers = 1; 3912 } 3913 mutex_exit(&tcp->tcp_eager_lock); 3914 3915 connp->conn_mdt_ok = B_FALSE; 3916 tcp->tcp_mdt = B_FALSE; 3917 3918 connp->conn_lso_ok = B_FALSE; 3919 tcp->tcp_lso = B_FALSE; 3920 3921 msg = NULL; 3922 switch (tcp->tcp_state) { 3923 case TCPS_CLOSED: 3924 case TCPS_IDLE: 3925 case TCPS_BOUND: 3926 case TCPS_LISTEN: 3927 break; 3928 case TCPS_SYN_SENT: 3929 msg = "tcp_close, during connect"; 3930 break; 3931 case TCPS_SYN_RCVD: 3932 /* 3933 * Close during the connect 3-way handshake 3934 * but here there may or may not be pending data 3935 * already on queue. Process almost same as in 3936 * the ESTABLISHED state. 3937 */ 3938 /* FALLTHRU */ 3939 default: 3940 if (tcp->tcp_sodirect != NULL) { 3941 /* Ok, no more sodirect */ 3942 tcp->tcp_sodirect = NULL; 3943 } 3944 3945 if (tcp->tcp_fused) 3946 tcp_unfuse(tcp); 3947 3948 /* 3949 * If SO_LINGER has set a zero linger time, abort the 3950 * connection with a reset. 3951 */ 3952 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3953 msg = "tcp_close, zero lingertime"; 3954 break; 3955 } 3956 3957 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3958 /* 3959 * Abort connection if there is unread data queued. 3960 */ 3961 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3962 msg = "tcp_close, unread data"; 3963 break; 3964 } 3965 /* 3966 * tcp_hard_bound is now cleared thus all packets go through 3967 * tcp_lookup. This fact is used by tcp_detach below. 3968 * 3969 * We have done a qwait() above which could have possibly 3970 * drained more messages in turn causing transition to a 3971 * different state. Check whether we have to do the rest 3972 * of the processing or not. 3973 */ 3974 if (tcp->tcp_state <= TCPS_LISTEN) 3975 break; 3976 3977 /* 3978 * Transmit the FIN before detaching the tcp_t. 3979 * After tcp_detach returns this queue/perimeter 3980 * no longer owns the tcp_t thus others can modify it. 3981 */ 3982 (void) tcp_xmit_end(tcp); 3983 3984 /* 3985 * If lingering on close then wait until the fin is acked, 3986 * the SO_LINGER time passes, or a reset is sent/received. 3987 */ 3988 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3989 !(tcp->tcp_fin_acked) && 3990 tcp->tcp_state >= TCPS_ESTABLISHED) { 3991 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3992 tcp->tcp_client_errno = EWOULDBLOCK; 3993 } else if (tcp->tcp_client_errno == 0) { 3994 3995 ASSERT(tcp->tcp_linger_tid == 0); 3996 3997 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3998 tcp_close_linger_timeout, 3999 tcp->tcp_lingertime * hz); 4000 4001 /* tcp_close_linger_timeout will finish close */ 4002 if (tcp->tcp_linger_tid == 0) 4003 tcp->tcp_client_errno = ENOSR; 4004 else 4005 return; 4006 } 4007 4008 /* 4009 * Check if we need to detach or just close 4010 * the instance. 4011 */ 4012 if (tcp->tcp_state <= TCPS_LISTEN) 4013 break; 4014 } 4015 4016 /* 4017 * Make sure that no other thread will access the tcp_rq of 4018 * this instance (through lookups etc.) as tcp_rq will go 4019 * away shortly. 4020 */ 4021 tcp_acceptor_hash_remove(tcp); 4022 4023 mutex_enter(&tcp->tcp_non_sq_lock); 4024 if (tcp->tcp_flow_stopped) { 4025 tcp_clrqfull(tcp); 4026 } 4027 mutex_exit(&tcp->tcp_non_sq_lock); 4028 4029 if (tcp->tcp_timer_tid != 0) { 4030 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4031 tcp->tcp_timer_tid = 0; 4032 } 4033 /* 4034 * Need to cancel those timers which will not be used when 4035 * TCP is detached. This has to be done before the tcp_wq 4036 * is set to the global queue. 4037 */ 4038 tcp_timers_stop(tcp); 4039 4040 tcp->tcp_detached = B_TRUE; 4041 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4042 tcp_time_wait_append(tcp); 4043 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4044 ASSERT(connp->conn_ref >= 3); 4045 goto finish; 4046 } 4047 4048 /* 4049 * If delta is zero the timer event wasn't executed and was 4050 * successfully canceled. In this case we need to restart it 4051 * with the minimal delta possible. 4052 */ 4053 if (delta >= 0) 4054 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4055 delta ? delta : 1); 4056 4057 ASSERT(connp->conn_ref >= 3); 4058 goto finish; 4059 } 4060 4061 /* Detach did not complete. Still need to remove q from stream. */ 4062 if (msg) { 4063 if (tcp->tcp_state == TCPS_ESTABLISHED || 4064 tcp->tcp_state == TCPS_CLOSE_WAIT) 4065 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4066 if (tcp->tcp_state == TCPS_SYN_SENT || 4067 tcp->tcp_state == TCPS_SYN_RCVD) 4068 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4069 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4070 } 4071 4072 tcp_closei_local(tcp); 4073 CONN_DEC_REF(connp); 4074 ASSERT(connp->conn_ref >= 2); 4075 4076 finish: 4077 /* 4078 * Although packets are always processed on the correct 4079 * tcp's perimeter and access is serialized via squeue's, 4080 * IP still needs a queue when sending packets in time_wait 4081 * state so use WR(tcps_g_q) till ip_output() can be 4082 * changed to deal with just connp. For read side, we 4083 * could have set tcp_rq to NULL but there are some cases 4084 * in tcp_rput_data() from early days of this code which 4085 * do a putnext without checking if tcp is closed. Those 4086 * need to be identified before both tcp_rq and tcp_wq 4087 * can be set to NULL and tcps_g_q can disappear forever. 4088 */ 4089 mutex_enter(&tcp->tcp_closelock); 4090 /* 4091 * Don't change the queues in the case of a listener that has 4092 * eagers in its q or q0. It could surprise the eagers. 4093 * Instead wait for the eagers outside the squeue. 4094 */ 4095 if (!tcp->tcp_wait_for_eagers) { 4096 tcp->tcp_detached = B_TRUE; 4097 /* 4098 * When default queue is closing we set tcps_g_q to NULL 4099 * after the close is done. 4100 */ 4101 ASSERT(tcps->tcps_g_q != NULL); 4102 tcp->tcp_rq = tcps->tcps_g_q; 4103 tcp->tcp_wq = WR(tcps->tcps_g_q); 4104 } 4105 4106 /* Signal tcp_close() to finish closing. */ 4107 tcp->tcp_closed = 1; 4108 cv_signal(&tcp->tcp_closecv); 4109 mutex_exit(&tcp->tcp_closelock); 4110 } 4111 4112 4113 /* 4114 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4115 * Some stream heads get upset if they see these later on as anything but NULL. 4116 */ 4117 static void 4118 tcp_close_mpp(mblk_t **mpp) 4119 { 4120 mblk_t *mp; 4121 4122 if ((mp = *mpp) != NULL) { 4123 do { 4124 mp->b_next = NULL; 4125 mp->b_prev = NULL; 4126 } while ((mp = mp->b_cont) != NULL); 4127 4128 mp = *mpp; 4129 *mpp = NULL; 4130 freemsg(mp); 4131 } 4132 } 4133 4134 /* Do detached close. */ 4135 static void 4136 tcp_close_detached(tcp_t *tcp) 4137 { 4138 if (tcp->tcp_fused) 4139 tcp_unfuse(tcp); 4140 4141 /* 4142 * Clustering code serializes TCP disconnect callbacks and 4143 * cluster tcp list walks by blocking a TCP disconnect callback 4144 * if a cluster tcp list walk is in progress. This ensures 4145 * accurate accounting of TCPs in the cluster code even though 4146 * the TCP list walk itself is not atomic. 4147 */ 4148 tcp_closei_local(tcp); 4149 CONN_DEC_REF(tcp->tcp_connp); 4150 } 4151 4152 /* 4153 * Stop all TCP timers, and free the timer mblks if requested. 4154 */ 4155 void 4156 tcp_timers_stop(tcp_t *tcp) 4157 { 4158 if (tcp->tcp_timer_tid != 0) { 4159 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4160 tcp->tcp_timer_tid = 0; 4161 } 4162 if (tcp->tcp_ka_tid != 0) { 4163 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4164 tcp->tcp_ka_tid = 0; 4165 } 4166 if (tcp->tcp_ack_tid != 0) { 4167 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4168 tcp->tcp_ack_tid = 0; 4169 } 4170 if (tcp->tcp_push_tid != 0) { 4171 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4172 tcp->tcp_push_tid = 0; 4173 } 4174 } 4175 4176 /* 4177 * The tcp_t is going away. Remove it from all lists and set it 4178 * to TCPS_CLOSED. The freeing up of memory is deferred until 4179 * tcp_inactive. This is needed since a thread in tcp_rput might have 4180 * done a CONN_INC_REF on this structure before it was removed from the 4181 * hashes. 4182 */ 4183 static void 4184 tcp_closei_local(tcp_t *tcp) 4185 { 4186 ire_t *ire; 4187 conn_t *connp = tcp->tcp_connp; 4188 tcp_stack_t *tcps = tcp->tcp_tcps; 4189 4190 if (!TCP_IS_SOCKET(tcp)) 4191 tcp_acceptor_hash_remove(tcp); 4192 4193 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4194 tcp->tcp_ibsegs = 0; 4195 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4196 tcp->tcp_obsegs = 0; 4197 4198 /* 4199 * If we are an eager connection hanging off a listener that 4200 * hasn't formally accepted the connection yet, get off his 4201 * list and blow off any data that we have accumulated. 4202 */ 4203 if (tcp->tcp_listener != NULL) { 4204 tcp_t *listener = tcp->tcp_listener; 4205 mutex_enter(&listener->tcp_eager_lock); 4206 /* 4207 * tcp_tconnind_started == B_TRUE means that the 4208 * conn_ind has already gone to listener. At 4209 * this point, eager will be closed but we 4210 * leave it in listeners eager list so that 4211 * if listener decides to close without doing 4212 * accept, we can clean this up. In tcp_wput_accept 4213 * we take care of the case of accept on closed 4214 * eager. 4215 */ 4216 if (!tcp->tcp_tconnind_started) { 4217 tcp_eager_unlink(tcp); 4218 mutex_exit(&listener->tcp_eager_lock); 4219 /* 4220 * We don't want to have any pointers to the 4221 * listener queue, after we have released our 4222 * reference on the listener 4223 */ 4224 ASSERT(tcps->tcps_g_q != NULL); 4225 tcp->tcp_rq = tcps->tcps_g_q; 4226 tcp->tcp_wq = WR(tcps->tcps_g_q); 4227 CONN_DEC_REF(listener->tcp_connp); 4228 } else { 4229 mutex_exit(&listener->tcp_eager_lock); 4230 } 4231 } 4232 4233 /* Stop all the timers */ 4234 tcp_timers_stop(tcp); 4235 4236 if (tcp->tcp_state == TCPS_LISTEN) { 4237 if (tcp->tcp_ip_addr_cache) { 4238 kmem_free((void *)tcp->tcp_ip_addr_cache, 4239 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4240 tcp->tcp_ip_addr_cache = NULL; 4241 } 4242 } 4243 mutex_enter(&tcp->tcp_non_sq_lock); 4244 if (tcp->tcp_flow_stopped) 4245 tcp_clrqfull(tcp); 4246 mutex_exit(&tcp->tcp_non_sq_lock); 4247 4248 tcp_bind_hash_remove(tcp); 4249 /* 4250 * If the tcp_time_wait_collector (which runs outside the squeue) 4251 * is trying to remove this tcp from the time wait list, we will 4252 * block in tcp_time_wait_remove while trying to acquire the 4253 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4254 * requires the ipcl_hash_remove to be ordered after the 4255 * tcp_time_wait_remove for the refcnt checks to work correctly. 4256 */ 4257 if (tcp->tcp_state == TCPS_TIME_WAIT) 4258 (void) tcp_time_wait_remove(tcp, NULL); 4259 CL_INET_DISCONNECT(connp, tcp); 4260 ipcl_hash_remove(connp); 4261 4262 /* 4263 * Delete the cached ire in conn_ire_cache and also mark 4264 * the conn as CONDEMNED 4265 */ 4266 mutex_enter(&connp->conn_lock); 4267 connp->conn_state_flags |= CONN_CONDEMNED; 4268 ire = connp->conn_ire_cache; 4269 connp->conn_ire_cache = NULL; 4270 mutex_exit(&connp->conn_lock); 4271 if (ire != NULL) 4272 IRE_REFRELE_NOTR(ire); 4273 4274 /* Need to cleanup any pending ioctls */ 4275 ASSERT(tcp->tcp_time_wait_next == NULL); 4276 ASSERT(tcp->tcp_time_wait_prev == NULL); 4277 ASSERT(tcp->tcp_time_wait_expire == 0); 4278 tcp->tcp_state = TCPS_CLOSED; 4279 4280 /* Release any SSL context */ 4281 if (tcp->tcp_kssl_ent != NULL) { 4282 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4283 tcp->tcp_kssl_ent = NULL; 4284 } 4285 if (tcp->tcp_kssl_ctx != NULL) { 4286 kssl_release_ctx(tcp->tcp_kssl_ctx); 4287 tcp->tcp_kssl_ctx = NULL; 4288 } 4289 tcp->tcp_kssl_pending = B_FALSE; 4290 4291 tcp_ipsec_cleanup(tcp); 4292 } 4293 4294 /* 4295 * tcp is dying (called from ipcl_conn_destroy and error cases). 4296 * Free the tcp_t in either case. 4297 */ 4298 void 4299 tcp_free(tcp_t *tcp) 4300 { 4301 mblk_t *mp; 4302 ip6_pkt_t *ipp; 4303 4304 ASSERT(tcp != NULL); 4305 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4306 4307 tcp->tcp_rq = NULL; 4308 tcp->tcp_wq = NULL; 4309 4310 tcp_close_mpp(&tcp->tcp_xmit_head); 4311 tcp_close_mpp(&tcp->tcp_reass_head); 4312 if (tcp->tcp_rcv_list != NULL) { 4313 /* Free b_next chain */ 4314 tcp_close_mpp(&tcp->tcp_rcv_list); 4315 } 4316 if ((mp = tcp->tcp_urp_mp) != NULL) { 4317 freemsg(mp); 4318 } 4319 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4320 freemsg(mp); 4321 } 4322 4323 if (tcp->tcp_fused_sigurg_mp != NULL) { 4324 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4325 freeb(tcp->tcp_fused_sigurg_mp); 4326 tcp->tcp_fused_sigurg_mp = NULL; 4327 } 4328 4329 if (tcp->tcp_ordrel_mp != NULL) { 4330 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4331 freeb(tcp->tcp_ordrel_mp); 4332 tcp->tcp_ordrel_mp = NULL; 4333 } 4334 4335 if (tcp->tcp_ordrel_mp != NULL) { 4336 freeb(tcp->tcp_ordrel_mp); 4337 tcp->tcp_ordrel_mp = NULL; 4338 } 4339 4340 if (tcp->tcp_sack_info != NULL) { 4341 if (tcp->tcp_notsack_list != NULL) { 4342 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4343 } 4344 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4345 } 4346 4347 if (tcp->tcp_hopopts != NULL) { 4348 mi_free(tcp->tcp_hopopts); 4349 tcp->tcp_hopopts = NULL; 4350 tcp->tcp_hopoptslen = 0; 4351 } 4352 ASSERT(tcp->tcp_hopoptslen == 0); 4353 if (tcp->tcp_dstopts != NULL) { 4354 mi_free(tcp->tcp_dstopts); 4355 tcp->tcp_dstopts = NULL; 4356 tcp->tcp_dstoptslen = 0; 4357 } 4358 ASSERT(tcp->tcp_dstoptslen == 0); 4359 if (tcp->tcp_rtdstopts != NULL) { 4360 mi_free(tcp->tcp_rtdstopts); 4361 tcp->tcp_rtdstopts = NULL; 4362 tcp->tcp_rtdstoptslen = 0; 4363 } 4364 ASSERT(tcp->tcp_rtdstoptslen == 0); 4365 if (tcp->tcp_rthdr != NULL) { 4366 mi_free(tcp->tcp_rthdr); 4367 tcp->tcp_rthdr = NULL; 4368 tcp->tcp_rthdrlen = 0; 4369 } 4370 ASSERT(tcp->tcp_rthdrlen == 0); 4371 4372 ipp = &tcp->tcp_sticky_ipp; 4373 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4374 IPPF_RTHDR)) 4375 ip6_pkt_free(ipp); 4376 4377 /* 4378 * Free memory associated with the tcp/ip header template. 4379 */ 4380 4381 if (tcp->tcp_iphc != NULL) 4382 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4383 4384 /* 4385 * Following is really a blowing away a union. 4386 * It happens to have exactly two members of identical size 4387 * the following code is enough. 4388 */ 4389 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4390 } 4391 4392 4393 /* 4394 * Put a connection confirmation message upstream built from the 4395 * address information within 'iph' and 'tcph'. Report our success or failure. 4396 */ 4397 static boolean_t 4398 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4399 mblk_t **defermp) 4400 { 4401 sin_t sin; 4402 sin6_t sin6; 4403 mblk_t *mp; 4404 char *optp = NULL; 4405 int optlen = 0; 4406 cred_t *cr; 4407 4408 if (defermp != NULL) 4409 *defermp = NULL; 4410 4411 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4412 /* 4413 * Return in T_CONN_CON results of option negotiation through 4414 * the T_CONN_REQ. Note: If there is an real end-to-end option 4415 * negotiation, then what is received from remote end needs 4416 * to be taken into account but there is no such thing (yet?) 4417 * in our TCP/IP. 4418 * Note: We do not use mi_offset_param() here as 4419 * tcp_opts_conn_req contents do not directly come from 4420 * an application and are either generated in kernel or 4421 * from user input that was already verified. 4422 */ 4423 mp = tcp->tcp_conn.tcp_opts_conn_req; 4424 optp = (char *)(mp->b_rptr + 4425 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4426 optlen = (int) 4427 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4428 } 4429 4430 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4431 ipha_t *ipha = (ipha_t *)iphdr; 4432 4433 /* packet is IPv4 */ 4434 if (tcp->tcp_family == AF_INET) { 4435 sin = sin_null; 4436 sin.sin_addr.s_addr = ipha->ipha_src; 4437 sin.sin_port = *(uint16_t *)tcph->th_lport; 4438 sin.sin_family = AF_INET; 4439 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4440 (int)sizeof (sin_t), optp, optlen); 4441 } else { 4442 sin6 = sin6_null; 4443 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4444 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4445 sin6.sin6_family = AF_INET6; 4446 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4447 (int)sizeof (sin6_t), optp, optlen); 4448 4449 } 4450 } else { 4451 ip6_t *ip6h = (ip6_t *)iphdr; 4452 4453 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4454 ASSERT(tcp->tcp_family == AF_INET6); 4455 sin6 = sin6_null; 4456 sin6.sin6_addr = ip6h->ip6_src; 4457 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4458 sin6.sin6_family = AF_INET6; 4459 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4460 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4461 (int)sizeof (sin6_t), optp, optlen); 4462 } 4463 4464 if (!mp) 4465 return (B_FALSE); 4466 4467 if ((cr = DB_CRED(idmp)) != NULL) { 4468 mblk_setcred(mp, cr); 4469 DB_CPID(mp) = DB_CPID(idmp); 4470 } 4471 4472 if (defermp == NULL) { 4473 conn_t *connp = tcp->tcp_connp; 4474 if (IPCL_IS_NONSTR(connp)) { 4475 (*connp->conn_upcalls->su_connected) 4476 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4477 DB_CPID(mp)); 4478 freemsg(mp); 4479 } else { 4480 putnext(tcp->tcp_rq, mp); 4481 } 4482 } else { 4483 *defermp = mp; 4484 } 4485 4486 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4487 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4488 return (B_TRUE); 4489 } 4490 4491 /* 4492 * Defense for the SYN attack - 4493 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4494 * one from the list of droppable eagers. This list is a subset of q0. 4495 * see comments before the definition of MAKE_DROPPABLE(). 4496 * 2. Don't drop a SYN request before its first timeout. This gives every 4497 * request at least til the first timeout to complete its 3-way handshake. 4498 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4499 * requests currently on the queue that has timed out. This will be used 4500 * as an indicator of whether an attack is under way, so that appropriate 4501 * actions can be taken. (It's incremented in tcp_timer() and decremented 4502 * either when eager goes into ESTABLISHED, or gets freed up.) 4503 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4504 * # of timeout drops back to <= q0len/32 => SYN alert off 4505 */ 4506 static boolean_t 4507 tcp_drop_q0(tcp_t *tcp) 4508 { 4509 tcp_t *eager; 4510 mblk_t *mp; 4511 tcp_stack_t *tcps = tcp->tcp_tcps; 4512 4513 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4514 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4515 4516 /* Pick oldest eager from the list of droppable eagers */ 4517 eager = tcp->tcp_eager_prev_drop_q0; 4518 4519 /* If list is empty. return B_FALSE */ 4520 if (eager == tcp) { 4521 return (B_FALSE); 4522 } 4523 4524 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4525 if ((mp = allocb(0, BPRI_HI)) == NULL) 4526 return (B_FALSE); 4527 4528 /* 4529 * Take this eager out from the list of droppable eagers since we are 4530 * going to drop it. 4531 */ 4532 MAKE_UNDROPPABLE(eager); 4533 4534 if (tcp->tcp_debug) { 4535 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4536 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4537 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4538 tcp->tcp_conn_req_cnt_q0, 4539 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4540 } 4541 4542 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4543 4544 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4545 CONN_INC_REF(eager->tcp_connp); 4546 4547 /* Mark the IRE created for this SYN request temporary */ 4548 tcp_ip_ire_mark_advice(eager); 4549 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4550 tcp_clean_death_wrapper, eager->tcp_connp, 4551 SQ_FILL, SQTAG_TCP_DROP_Q0); 4552 4553 return (B_TRUE); 4554 } 4555 4556 int 4557 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4558 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4559 { 4560 tcp_t *ltcp = lconnp->conn_tcp; 4561 tcp_t *tcp = connp->conn_tcp; 4562 mblk_t *tpi_mp; 4563 ipha_t *ipha; 4564 ip6_t *ip6h; 4565 sin6_t sin6; 4566 in6_addr_t v6dst; 4567 int err; 4568 int ifindex = 0; 4569 cred_t *cr; 4570 tcp_stack_t *tcps = tcp->tcp_tcps; 4571 4572 if (ipvers == IPV4_VERSION) { 4573 ipha = (ipha_t *)mp->b_rptr; 4574 4575 connp->conn_send = ip_output; 4576 connp->conn_recv = tcp_input; 4577 4578 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4579 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4580 4581 sin6 = sin6_null; 4582 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4583 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4584 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4585 sin6.sin6_family = AF_INET6; 4586 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4587 lconnp->conn_zoneid, tcps->tcps_netstack); 4588 if (tcp->tcp_recvdstaddr) { 4589 sin6_t sin6d; 4590 4591 sin6d = sin6_null; 4592 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4593 &sin6d.sin6_addr); 4594 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4595 sin6d.sin6_family = AF_INET; 4596 tpi_mp = mi_tpi_extconn_ind(NULL, 4597 (char *)&sin6d, sizeof (sin6_t), 4598 (char *)&tcp, 4599 (t_scalar_t)sizeof (intptr_t), 4600 (char *)&sin6d, sizeof (sin6_t), 4601 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4602 } else { 4603 tpi_mp = mi_tpi_conn_ind(NULL, 4604 (char *)&sin6, sizeof (sin6_t), 4605 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4606 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4607 } 4608 } else { 4609 ip6h = (ip6_t *)mp->b_rptr; 4610 4611 connp->conn_send = ip_output_v6; 4612 connp->conn_recv = tcp_input; 4613 4614 connp->conn_srcv6 = ip6h->ip6_dst; 4615 connp->conn_remv6 = ip6h->ip6_src; 4616 4617 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4618 ifindex = (int)DB_CKSUMSTUFF(mp); 4619 DB_CKSUMSTUFF(mp) = 0; 4620 4621 sin6 = sin6_null; 4622 sin6.sin6_addr = ip6h->ip6_src; 4623 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4624 sin6.sin6_family = AF_INET6; 4625 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4626 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4627 lconnp->conn_zoneid, tcps->tcps_netstack); 4628 4629 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4630 /* Pass up the scope_id of remote addr */ 4631 sin6.sin6_scope_id = ifindex; 4632 } else { 4633 sin6.sin6_scope_id = 0; 4634 } 4635 if (tcp->tcp_recvdstaddr) { 4636 sin6_t sin6d; 4637 4638 sin6d = sin6_null; 4639 sin6.sin6_addr = ip6h->ip6_dst; 4640 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4641 sin6d.sin6_family = AF_INET; 4642 tpi_mp = mi_tpi_extconn_ind(NULL, 4643 (char *)&sin6d, sizeof (sin6_t), 4644 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4645 (char *)&sin6d, sizeof (sin6_t), 4646 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4647 } else { 4648 tpi_mp = mi_tpi_conn_ind(NULL, 4649 (char *)&sin6, sizeof (sin6_t), 4650 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4651 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4652 } 4653 } 4654 4655 if (tpi_mp == NULL) 4656 return (ENOMEM); 4657 4658 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4659 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4660 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4661 connp->conn_fully_bound = B_FALSE; 4662 4663 /* Inherit information from the "parent" */ 4664 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4665 tcp->tcp_family = ltcp->tcp_family; 4666 4667 tcp->tcp_wq = ltcp->tcp_wq; 4668 tcp->tcp_rq = ltcp->tcp_rq; 4669 4670 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4671 tcp->tcp_detached = B_TRUE; 4672 SOCK_CONNID_INIT(tcp->tcp_connid); 4673 if ((err = tcp_init_values(tcp)) != 0) { 4674 freemsg(tpi_mp); 4675 return (err); 4676 } 4677 4678 if (ipvers == IPV4_VERSION) { 4679 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4680 freemsg(tpi_mp); 4681 return (err); 4682 } 4683 ASSERT(tcp->tcp_ipha != NULL); 4684 } else { 4685 /* ifindex must be already set */ 4686 ASSERT(ifindex != 0); 4687 4688 if (ltcp->tcp_bound_if != 0) { 4689 /* 4690 * Set newtcp's bound_if equal to 4691 * listener's value. If ifindex is 4692 * not the same as ltcp->tcp_bound_if, 4693 * it must be a packet for the ipmp group 4694 * of interfaces 4695 */ 4696 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4697 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4698 tcp->tcp_bound_if = ifindex; 4699 } 4700 4701 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4702 tcp->tcp_recvifindex = 0; 4703 tcp->tcp_recvhops = 0xffffffffU; 4704 ASSERT(tcp->tcp_ip6h != NULL); 4705 } 4706 4707 tcp->tcp_lport = ltcp->tcp_lport; 4708 4709 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4710 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4711 /* 4712 * Listener had options of some sort; eager inherits. 4713 * Free up the eager template and allocate one 4714 * of the right size. 4715 */ 4716 if (tcp->tcp_hdr_grown) { 4717 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4718 } else { 4719 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4720 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4721 } 4722 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4723 KM_NOSLEEP); 4724 if (tcp->tcp_iphc == NULL) { 4725 tcp->tcp_iphc_len = 0; 4726 freemsg(tpi_mp); 4727 return (ENOMEM); 4728 } 4729 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4730 tcp->tcp_hdr_grown = B_TRUE; 4731 } 4732 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4733 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4734 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4735 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4736 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4737 4738 /* 4739 * Copy the IP+TCP header template from listener to eager 4740 */ 4741 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4742 if (tcp->tcp_ipversion == IPV6_VERSION) { 4743 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4744 IPPROTO_RAW) { 4745 tcp->tcp_ip6h = 4746 (ip6_t *)(tcp->tcp_iphc + 4747 sizeof (ip6i_t)); 4748 } else { 4749 tcp->tcp_ip6h = 4750 (ip6_t *)(tcp->tcp_iphc); 4751 } 4752 tcp->tcp_ipha = NULL; 4753 } else { 4754 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4755 tcp->tcp_ip6h = NULL; 4756 } 4757 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4758 tcp->tcp_ip_hdr_len); 4759 } else { 4760 /* 4761 * only valid case when ipversion of listener and 4762 * eager differ is when listener is IPv6 and 4763 * eager is IPv4. 4764 * Eager header template has been initialized to the 4765 * maximum v4 header sizes, which includes space for 4766 * TCP and IP options. 4767 */ 4768 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4769 (tcp->tcp_ipversion == IPV4_VERSION)); 4770 ASSERT(tcp->tcp_iphc_len >= 4771 TCP_MAX_COMBINED_HEADER_LENGTH); 4772 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4773 /* copy IP header fields individually */ 4774 tcp->tcp_ipha->ipha_ttl = 4775 ltcp->tcp_ip6h->ip6_hops; 4776 bcopy(ltcp->tcp_tcph->th_lport, 4777 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4778 } 4779 4780 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4781 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4782 sizeof (in_port_t)); 4783 4784 if (ltcp->tcp_lport == 0) { 4785 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4786 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4787 sizeof (in_port_t)); 4788 } 4789 4790 if (tcp->tcp_ipversion == IPV4_VERSION) { 4791 ASSERT(ipha != NULL); 4792 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4793 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4794 4795 /* Source routing option copyover (reverse it) */ 4796 if (tcps->tcps_rev_src_routes) 4797 tcp_opt_reverse(tcp, ipha); 4798 } else { 4799 ASSERT(ip6h != NULL); 4800 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4801 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4802 } 4803 4804 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4805 ASSERT(!tcp->tcp_tconnind_started); 4806 /* 4807 * If the SYN contains a credential, it's a loopback packet; attach 4808 * the credential to the TPI message. 4809 */ 4810 if ((cr = DB_CRED(idmp)) != NULL) { 4811 mblk_setcred(tpi_mp, cr); 4812 DB_CPID(tpi_mp) = DB_CPID(idmp); 4813 } 4814 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4815 4816 /* Inherit the listener's SSL protection state */ 4817 4818 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4819 kssl_hold_ent(tcp->tcp_kssl_ent); 4820 tcp->tcp_kssl_pending = B_TRUE; 4821 } 4822 4823 /* Inherit the listener's non-STREAMS flag */ 4824 if (IPCL_IS_NONSTR(lconnp)) { 4825 connp->conn_flags |= IPCL_NONSTR; 4826 connp->conn_upcalls = lconnp->conn_upcalls; 4827 } 4828 4829 return (0); 4830 } 4831 4832 4833 int 4834 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4835 tcph_t *tcph, mblk_t *idmp) 4836 { 4837 tcp_t *ltcp = lconnp->conn_tcp; 4838 tcp_t *tcp = connp->conn_tcp; 4839 sin_t sin; 4840 mblk_t *tpi_mp = NULL; 4841 int err; 4842 cred_t *cr; 4843 tcp_stack_t *tcps = tcp->tcp_tcps; 4844 4845 sin = sin_null; 4846 sin.sin_addr.s_addr = ipha->ipha_src; 4847 sin.sin_port = *(uint16_t *)tcph->th_lport; 4848 sin.sin_family = AF_INET; 4849 if (ltcp->tcp_recvdstaddr) { 4850 sin_t sind; 4851 4852 sind = sin_null; 4853 sind.sin_addr.s_addr = ipha->ipha_dst; 4854 sind.sin_port = *(uint16_t *)tcph->th_fport; 4855 sind.sin_family = AF_INET; 4856 tpi_mp = mi_tpi_extconn_ind(NULL, 4857 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4858 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4859 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4860 } else { 4861 tpi_mp = mi_tpi_conn_ind(NULL, 4862 (char *)&sin, sizeof (sin_t), 4863 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4864 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4865 } 4866 4867 if (tpi_mp == NULL) { 4868 return (ENOMEM); 4869 } 4870 4871 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4872 connp->conn_send = ip_output; 4873 connp->conn_recv = tcp_input; 4874 connp->conn_fully_bound = B_FALSE; 4875 4876 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4877 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4878 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4879 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4880 4881 /* Inherit information from the "parent" */ 4882 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4883 tcp->tcp_family = ltcp->tcp_family; 4884 tcp->tcp_wq = ltcp->tcp_wq; 4885 tcp->tcp_rq = ltcp->tcp_rq; 4886 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4887 tcp->tcp_detached = B_TRUE; 4888 SOCK_CONNID_INIT(tcp->tcp_connid); 4889 if ((err = tcp_init_values(tcp)) != 0) { 4890 freemsg(tpi_mp); 4891 return (err); 4892 } 4893 4894 /* 4895 * Let's make sure that eager tcp template has enough space to 4896 * copy IPv4 listener's tcp template. Since the conn_t structure is 4897 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4898 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4899 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4900 * extension headers or with ip6i_t struct). Note that bcopy() below 4901 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4902 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4903 */ 4904 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4905 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4906 4907 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4908 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4909 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4910 tcp->tcp_ttl = ltcp->tcp_ttl; 4911 tcp->tcp_tos = ltcp->tcp_tos; 4912 4913 /* Copy the IP+TCP header template from listener to eager */ 4914 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4915 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4916 tcp->tcp_ip6h = NULL; 4917 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4918 tcp->tcp_ip_hdr_len); 4919 4920 /* Initialize the IP addresses and Ports */ 4921 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4922 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4923 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4924 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4925 4926 /* Source routing option copyover (reverse it) */ 4927 if (tcps->tcps_rev_src_routes) 4928 tcp_opt_reverse(tcp, ipha); 4929 4930 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4931 ASSERT(!tcp->tcp_tconnind_started); 4932 4933 /* 4934 * If the SYN contains a credential, it's a loopback packet; attach 4935 * the credential to the TPI message. 4936 */ 4937 if ((cr = DB_CRED(idmp)) != NULL) { 4938 mblk_setcred(tpi_mp, cr); 4939 DB_CPID(tpi_mp) = DB_CPID(idmp); 4940 } 4941 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4942 4943 /* Inherit the listener's SSL protection state */ 4944 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4945 kssl_hold_ent(tcp->tcp_kssl_ent); 4946 tcp->tcp_kssl_pending = B_TRUE; 4947 } 4948 4949 /* Inherit the listener's non-STREAMS flag */ 4950 if (IPCL_IS_NONSTR(lconnp)) { 4951 connp->conn_flags |= IPCL_NONSTR; 4952 connp->conn_upcalls = lconnp->conn_upcalls; 4953 } 4954 4955 return (0); 4956 } 4957 4958 /* 4959 * sets up conn for ipsec. 4960 * if the first mblk is M_CTL it is consumed and mpp is updated. 4961 * in case of error mpp is freed. 4962 */ 4963 conn_t * 4964 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4965 { 4966 conn_t *connp = tcp->tcp_connp; 4967 conn_t *econnp; 4968 squeue_t *new_sqp; 4969 mblk_t *first_mp = *mpp; 4970 mblk_t *mp = *mpp; 4971 boolean_t mctl_present = B_FALSE; 4972 uint_t ipvers; 4973 4974 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4975 if (econnp == NULL) { 4976 freemsg(first_mp); 4977 return (NULL); 4978 } 4979 if (DB_TYPE(mp) == M_CTL) { 4980 if (mp->b_cont == NULL || 4981 mp->b_cont->b_datap->db_type != M_DATA) { 4982 freemsg(first_mp); 4983 return (NULL); 4984 } 4985 mp = mp->b_cont; 4986 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4987 freemsg(first_mp); 4988 return (NULL); 4989 } 4990 4991 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4992 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4993 mctl_present = B_TRUE; 4994 } else { 4995 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4996 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4997 } 4998 4999 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5000 DB_CKSUMSTART(mp) = 0; 5001 5002 ASSERT(OK_32PTR(mp->b_rptr)); 5003 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5004 if (ipvers == IPV4_VERSION) { 5005 uint16_t *up; 5006 uint32_t ports; 5007 ipha_t *ipha; 5008 5009 ipha = (ipha_t *)mp->b_rptr; 5010 up = (uint16_t *)((uchar_t *)ipha + 5011 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5012 ports = *(uint32_t *)up; 5013 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5014 ipha->ipha_dst, ipha->ipha_src, ports); 5015 } else { 5016 uint16_t *up; 5017 uint32_t ports; 5018 uint16_t ip_hdr_len; 5019 uint8_t *nexthdrp; 5020 ip6_t *ip6h; 5021 tcph_t *tcph; 5022 5023 ip6h = (ip6_t *)mp->b_rptr; 5024 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5025 ip_hdr_len = IPV6_HDR_LEN; 5026 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5027 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5028 CONN_DEC_REF(econnp); 5029 freemsg(first_mp); 5030 return (NULL); 5031 } 5032 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5033 up = (uint16_t *)tcph->th_lport; 5034 ports = *(uint32_t *)up; 5035 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5036 ip6h->ip6_dst, ip6h->ip6_src, ports); 5037 } 5038 5039 /* 5040 * The caller already ensured that there is a sqp present. 5041 */ 5042 econnp->conn_sqp = new_sqp; 5043 econnp->conn_initial_sqp = new_sqp; 5044 5045 if (connp->conn_policy != NULL) { 5046 ipsec_in_t *ii; 5047 ii = (ipsec_in_t *)(first_mp->b_rptr); 5048 ASSERT(ii->ipsec_in_policy == NULL); 5049 IPPH_REFHOLD(connp->conn_policy); 5050 ii->ipsec_in_policy = connp->conn_policy; 5051 5052 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5053 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5054 CONN_DEC_REF(econnp); 5055 freemsg(first_mp); 5056 return (NULL); 5057 } 5058 } 5059 5060 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5061 CONN_DEC_REF(econnp); 5062 freemsg(first_mp); 5063 return (NULL); 5064 } 5065 5066 /* 5067 * If we know we have some policy, pass the "IPSEC" 5068 * options size TCP uses this adjust the MSS. 5069 */ 5070 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5071 if (mctl_present) { 5072 freeb(first_mp); 5073 *mpp = mp; 5074 } 5075 5076 return (econnp); 5077 } 5078 5079 /* 5080 * tcp_get_conn/tcp_free_conn 5081 * 5082 * tcp_get_conn is used to get a clean tcp connection structure. 5083 * It tries to reuse the connections put on the freelist by the 5084 * time_wait_collector failing which it goes to kmem_cache. This 5085 * way has two benefits compared to just allocating from and 5086 * freeing to kmem_cache. 5087 * 1) The time_wait_collector can free (which includes the cleanup) 5088 * outside the squeue. So when the interrupt comes, we have a clean 5089 * connection sitting in the freelist. Obviously, this buys us 5090 * performance. 5091 * 5092 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5093 * has multiple disadvantages - tying up the squeue during alloc, and the 5094 * fact that IPSec policy initialization has to happen here which 5095 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5096 * But allocating the conn/tcp in IP land is also not the best since 5097 * we can't check the 'q' and 'q0' which are protected by squeue and 5098 * blindly allocate memory which might have to be freed here if we are 5099 * not allowed to accept the connection. By using the freelist and 5100 * putting the conn/tcp back in freelist, we don't pay a penalty for 5101 * allocating memory without checking 'q/q0' and freeing it if we can't 5102 * accept the connection. 5103 * 5104 * Care should be taken to put the conn back in the same squeue's freelist 5105 * from which it was allocated. Best results are obtained if conn is 5106 * allocated from listener's squeue and freed to the same. Time wait 5107 * collector will free up the freelist is the connection ends up sitting 5108 * there for too long. 5109 */ 5110 void * 5111 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5112 { 5113 tcp_t *tcp = NULL; 5114 conn_t *connp = NULL; 5115 squeue_t *sqp = (squeue_t *)arg; 5116 tcp_squeue_priv_t *tcp_time_wait; 5117 netstack_t *ns; 5118 5119 tcp_time_wait = 5120 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5121 5122 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5123 tcp = tcp_time_wait->tcp_free_list; 5124 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5125 if (tcp != NULL) { 5126 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5127 tcp_time_wait->tcp_free_list_cnt--; 5128 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5129 tcp->tcp_time_wait_next = NULL; 5130 connp = tcp->tcp_connp; 5131 connp->conn_flags |= IPCL_REUSED; 5132 5133 ASSERT(tcp->tcp_tcps == NULL); 5134 ASSERT(connp->conn_netstack == NULL); 5135 ASSERT(tcp->tcp_rsrv_mp != NULL); 5136 ns = tcps->tcps_netstack; 5137 netstack_hold(ns); 5138 connp->conn_netstack = ns; 5139 tcp->tcp_tcps = tcps; 5140 TCPS_REFHOLD(tcps); 5141 ipcl_globalhash_insert(connp); 5142 return ((void *)connp); 5143 } 5144 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5145 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5146 tcps->tcps_netstack)) == NULL) 5147 return (NULL); 5148 tcp = connp->conn_tcp; 5149 /* 5150 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5151 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5152 */ 5153 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5154 ipcl_conn_destroy(connp); 5155 return (NULL); 5156 } 5157 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5158 tcp->tcp_tcps = tcps; 5159 TCPS_REFHOLD(tcps); 5160 5161 return ((void *)connp); 5162 } 5163 5164 /* 5165 * Update the cached label for the given tcp_t. This should be called once per 5166 * connection, and before any packets are sent or tcp_process_options is 5167 * invoked. Returns B_FALSE if the correct label could not be constructed. 5168 */ 5169 static boolean_t 5170 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5171 { 5172 conn_t *connp = tcp->tcp_connp; 5173 5174 if (tcp->tcp_ipversion == IPV4_VERSION) { 5175 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5176 int added; 5177 5178 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5179 connp->conn_mac_exempt, 5180 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5181 return (B_FALSE); 5182 5183 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5184 if (added == -1) 5185 return (B_FALSE); 5186 tcp->tcp_hdr_len += added; 5187 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5188 tcp->tcp_ip_hdr_len += added; 5189 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5190 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5191 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5192 tcp->tcp_hdr_len); 5193 if (added == -1) 5194 return (B_FALSE); 5195 tcp->tcp_hdr_len += added; 5196 tcp->tcp_tcph = (tcph_t *) 5197 ((uchar_t *)tcp->tcp_tcph + added); 5198 tcp->tcp_ip_hdr_len += added; 5199 } 5200 } else { 5201 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5202 5203 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5204 connp->conn_mac_exempt, 5205 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5206 return (B_FALSE); 5207 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5208 &tcp->tcp_label_len, optbuf) != 0) 5209 return (B_FALSE); 5210 if (tcp_build_hdrs(tcp) != 0) 5211 return (B_FALSE); 5212 } 5213 5214 connp->conn_ulp_labeled = 1; 5215 5216 return (B_TRUE); 5217 } 5218 5219 /* BEGIN CSTYLED */ 5220 /* 5221 * 5222 * The sockfs ACCEPT path: 5223 * ======================= 5224 * 5225 * The eager is now established in its own perimeter as soon as SYN is 5226 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5227 * completes the accept processing on the acceptor STREAM. The sending 5228 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5229 * listener but a TLI/XTI listener completes the accept processing 5230 * on the listener perimeter. 5231 * 5232 * Common control flow for 3 way handshake: 5233 * ---------------------------------------- 5234 * 5235 * incoming SYN (listener perimeter) -> tcp_rput_data() 5236 * -> tcp_conn_request() 5237 * 5238 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5239 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5240 * 5241 * Sockfs ACCEPT Path: 5242 * ------------------- 5243 * 5244 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5245 * as STREAM entry point) 5246 * 5247 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5248 * 5249 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5250 * association (we are not behind eager's squeue but sockfs is protecting us 5251 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5252 * is changed to point at tcp_wput(). 5253 * 5254 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5255 * listener (done on listener's perimeter). 5256 * 5257 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5258 * accept. 5259 * 5260 * TLI/XTI client ACCEPT path: 5261 * --------------------------- 5262 * 5263 * soaccept() sends T_CONN_RES on the listener STREAM. 5264 * 5265 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5266 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5267 * 5268 * Locks: 5269 * ====== 5270 * 5271 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5272 * and listeners->tcp_eager_next_q. 5273 * 5274 * Referencing: 5275 * ============ 5276 * 5277 * 1) We start out in tcp_conn_request by eager placing a ref on 5278 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5279 * 5280 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5281 * doing so we place a ref on the eager. This ref is finally dropped at the 5282 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5283 * reference is dropped by the squeue framework. 5284 * 5285 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5286 * 5287 * The reference must be released by the same entity that added the reference 5288 * In the above scheme, the eager is the entity that adds and releases the 5289 * references. Note that tcp_accept_finish executes in the squeue of the eager 5290 * (albeit after it is attached to the acceptor stream). Though 1. executes 5291 * in the listener's squeue, the eager is nascent at this point and the 5292 * reference can be considered to have been added on behalf of the eager. 5293 * 5294 * Eager getting a Reset or listener closing: 5295 * ========================================== 5296 * 5297 * Once the listener and eager are linked, the listener never does the unlink. 5298 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5299 * a message on all eager perimeter. The eager then does the unlink, clears 5300 * any pointers to the listener's queue and drops the reference to the 5301 * listener. The listener waits in tcp_close outside the squeue until its 5302 * refcount has dropped to 1. This ensures that the listener has waited for 5303 * all eagers to clear their association with the listener. 5304 * 5305 * Similarly, if eager decides to go away, it can unlink itself and close. 5306 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5307 * the reference to eager is still valid because of the extra ref we put 5308 * in tcp_send_conn_ind. 5309 * 5310 * Listener can always locate the eager under the protection 5311 * of the listener->tcp_eager_lock, and then do a refhold 5312 * on the eager during the accept processing. 5313 * 5314 * The acceptor stream accesses the eager in the accept processing 5315 * based on the ref placed on eager before sending T_conn_ind. 5316 * The only entity that can negate this refhold is a listener close 5317 * which is mutually exclusive with an active acceptor stream. 5318 * 5319 * Eager's reference on the listener 5320 * =================================== 5321 * 5322 * If the accept happens (even on a closed eager) the eager drops its 5323 * reference on the listener at the start of tcp_accept_finish. If the 5324 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5325 * the reference is dropped in tcp_closei_local. If the listener closes, 5326 * the reference is dropped in tcp_eager_kill. In all cases the reference 5327 * is dropped while executing in the eager's context (squeue). 5328 */ 5329 /* END CSTYLED */ 5330 5331 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5332 5333 /* 5334 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5335 * tcp_rput_data will not see any SYN packets. 5336 */ 5337 /* ARGSUSED */ 5338 void 5339 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5340 { 5341 tcph_t *tcph; 5342 uint32_t seg_seq; 5343 tcp_t *eager; 5344 uint_t ipvers; 5345 ipha_t *ipha; 5346 ip6_t *ip6h; 5347 int err; 5348 conn_t *econnp = NULL; 5349 squeue_t *new_sqp; 5350 mblk_t *mp1; 5351 uint_t ip_hdr_len; 5352 conn_t *connp = (conn_t *)arg; 5353 tcp_t *tcp = connp->conn_tcp; 5354 cred_t *credp; 5355 tcp_stack_t *tcps = tcp->tcp_tcps; 5356 ip_stack_t *ipst; 5357 5358 if (tcp->tcp_state != TCPS_LISTEN) 5359 goto error2; 5360 5361 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5362 5363 mutex_enter(&tcp->tcp_eager_lock); 5364 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5365 mutex_exit(&tcp->tcp_eager_lock); 5366 TCP_STAT(tcps, tcp_listendrop); 5367 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5368 if (tcp->tcp_debug) { 5369 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5370 "tcp_conn_request: listen backlog (max=%d) " 5371 "overflow (%d pending) on %s", 5372 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5373 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5374 } 5375 goto error2; 5376 } 5377 5378 if (tcp->tcp_conn_req_cnt_q0 >= 5379 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5380 /* 5381 * Q0 is full. Drop a pending half-open req from the queue 5382 * to make room for the new SYN req. Also mark the time we 5383 * drop a SYN. 5384 * 5385 * A more aggressive defense against SYN attack will 5386 * be to set the "tcp_syn_defense" flag now. 5387 */ 5388 TCP_STAT(tcps, tcp_listendropq0); 5389 tcp->tcp_last_rcv_lbolt = lbolt64; 5390 if (!tcp_drop_q0(tcp)) { 5391 mutex_exit(&tcp->tcp_eager_lock); 5392 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5393 if (tcp->tcp_debug) { 5394 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5395 "tcp_conn_request: listen half-open queue " 5396 "(max=%d) full (%d pending) on %s", 5397 tcps->tcps_conn_req_max_q0, 5398 tcp->tcp_conn_req_cnt_q0, 5399 tcp_display(tcp, NULL, 5400 DISP_PORT_ONLY)); 5401 } 5402 goto error2; 5403 } 5404 } 5405 mutex_exit(&tcp->tcp_eager_lock); 5406 5407 /* 5408 * IP adds STRUIO_EAGER and ensures that the received packet is 5409 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5410 * link local address. If IPSec is enabled, db_struioflag has 5411 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5412 * otherwise an error case if neither of them is set. 5413 */ 5414 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5415 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5416 DB_CKSUMSTART(mp) = 0; 5417 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5418 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5419 if (econnp == NULL) 5420 goto error2; 5421 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5422 econnp->conn_sqp = new_sqp; 5423 econnp->conn_initial_sqp = new_sqp; 5424 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5425 /* 5426 * mp is updated in tcp_get_ipsec_conn(). 5427 */ 5428 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5429 if (econnp == NULL) { 5430 /* 5431 * mp freed by tcp_get_ipsec_conn. 5432 */ 5433 return; 5434 } 5435 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5436 } else { 5437 goto error2; 5438 } 5439 5440 ASSERT(DB_TYPE(mp) == M_DATA); 5441 5442 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5443 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5444 ASSERT(OK_32PTR(mp->b_rptr)); 5445 if (ipvers == IPV4_VERSION) { 5446 ipha = (ipha_t *)mp->b_rptr; 5447 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5448 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5449 } else { 5450 ip6h = (ip6_t *)mp->b_rptr; 5451 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5452 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5453 } 5454 5455 if (tcp->tcp_family == AF_INET) { 5456 ASSERT(ipvers == IPV4_VERSION); 5457 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5458 } else { 5459 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5460 } 5461 5462 if (err) 5463 goto error3; 5464 5465 eager = econnp->conn_tcp; 5466 5467 /* 5468 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that at close 5469 * time, we will always have that to send up. Otherwise, we need to do 5470 * special handling in case the allocation fails at that time. 5471 */ 5472 ASSERT(eager->tcp_ordrel_mp == NULL); 5473 if (!IPCL_IS_NONSTR(econnp) && 5474 (eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5475 goto error3; 5476 5477 /* Inherit various TCP parameters from the listener */ 5478 eager->tcp_naglim = tcp->tcp_naglim; 5479 eager->tcp_first_timer_threshold = 5480 tcp->tcp_first_timer_threshold; 5481 eager->tcp_second_timer_threshold = 5482 tcp->tcp_second_timer_threshold; 5483 5484 eager->tcp_first_ctimer_threshold = 5485 tcp->tcp_first_ctimer_threshold; 5486 eager->tcp_second_ctimer_threshold = 5487 tcp->tcp_second_ctimer_threshold; 5488 5489 /* 5490 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5491 * If it does not, the eager's receive window will be set to the 5492 * listener's receive window later in this function. 5493 */ 5494 eager->tcp_rwnd = 0; 5495 5496 /* 5497 * Inherit listener's tcp_init_cwnd. Need to do this before 5498 * calling tcp_process_options() where tcp_mss_set() is called 5499 * to set the initial cwnd. 5500 */ 5501 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5502 5503 /* 5504 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5505 * zone id before the accept is completed in tcp_wput_accept(). 5506 */ 5507 econnp->conn_zoneid = connp->conn_zoneid; 5508 econnp->conn_allzones = connp->conn_allzones; 5509 5510 /* Copy nexthop information from listener to eager */ 5511 if (connp->conn_nexthop_set) { 5512 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5513 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5514 } 5515 5516 /* 5517 * TSOL: tsol_input_proc() needs the eager's cred before the 5518 * eager is accepted 5519 */ 5520 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5521 crhold(credp); 5522 5523 /* 5524 * If the caller has the process-wide flag set, then default to MAC 5525 * exempt mode. This allows read-down to unlabeled hosts. 5526 */ 5527 if (getpflags(NET_MAC_AWARE, credp) != 0) 5528 econnp->conn_mac_exempt = B_TRUE; 5529 5530 if (is_system_labeled()) { 5531 cred_t *cr; 5532 5533 if (connp->conn_mlp_type != mlptSingle) { 5534 cr = econnp->conn_peercred = DB_CRED(mp); 5535 if (cr != NULL) 5536 crhold(cr); 5537 else 5538 cr = econnp->conn_cred; 5539 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5540 econnp, cred_t *, cr) 5541 } else { 5542 cr = econnp->conn_cred; 5543 DTRACE_PROBE2(syn_accept, conn_t *, 5544 econnp, cred_t *, cr) 5545 } 5546 5547 if (!tcp_update_label(eager, cr)) { 5548 DTRACE_PROBE3( 5549 tx__ip__log__error__connrequest__tcp, 5550 char *, "eager connp(1) label on SYN mp(2) failed", 5551 conn_t *, econnp, mblk_t *, mp); 5552 goto error3; 5553 } 5554 } 5555 5556 eager->tcp_hard_binding = B_TRUE; 5557 5558 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5559 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5560 5561 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5562 if (err != 0) { 5563 tcp_bind_hash_remove(eager); 5564 goto error3; 5565 } 5566 5567 /* 5568 * No need to check for multicast destination since ip will only pass 5569 * up multicasts to those that have expressed interest 5570 * TODO: what about rejecting broadcasts? 5571 * Also check that source is not a multicast or broadcast address. 5572 */ 5573 eager->tcp_state = TCPS_SYN_RCVD; 5574 5575 5576 /* 5577 * There should be no ire in the mp as we are being called after 5578 * receiving the SYN. 5579 */ 5580 ASSERT(tcp_ire_mp(&mp) == NULL); 5581 5582 /* 5583 * Adapt our mss, ttl, ... according to information provided in IRE. 5584 */ 5585 5586 if (tcp_adapt_ire(eager, NULL) == 0) { 5587 /* Undo the bind_hash_insert */ 5588 tcp_bind_hash_remove(eager); 5589 goto error3; 5590 } 5591 5592 /* Process all TCP options. */ 5593 tcp_process_options(eager, tcph); 5594 5595 /* Is the other end ECN capable? */ 5596 if (tcps->tcps_ecn_permitted >= 1 && 5597 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5598 eager->tcp_ecn_ok = B_TRUE; 5599 } 5600 5601 /* 5602 * listener->tcp_rq->q_hiwat should be the default window size or a 5603 * window size changed via SO_RCVBUF option. First round up the 5604 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5605 * scale option value if needed. Call tcp_rwnd_set() to finish the 5606 * setting. 5607 * 5608 * Note if there is a rpipe metric associated with the remote host, 5609 * we should not inherit receive window size from listener. 5610 */ 5611 eager->tcp_rwnd = MSS_ROUNDUP( 5612 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5613 eager->tcp_rwnd), eager->tcp_mss); 5614 if (eager->tcp_snd_ws_ok) 5615 tcp_set_ws_value(eager); 5616 /* 5617 * Note that this is the only place tcp_rwnd_set() is called for 5618 * accepting a connection. We need to call it here instead of 5619 * after the 3-way handshake because we need to tell the other 5620 * side our rwnd in the SYN-ACK segment. 5621 */ 5622 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5623 5624 /* 5625 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5626 * via soaccept()->soinheritoptions() which essentially applies 5627 * all the listener options to the new STREAM. The options that we 5628 * need to take care of are: 5629 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5630 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5631 * SO_SNDBUF, SO_RCVBUF. 5632 * 5633 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5634 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5635 * tcp_maxpsz_set() gets called later from 5636 * tcp_accept_finish(), the option takes effect. 5637 * 5638 */ 5639 /* Set the TCP options */ 5640 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5641 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5642 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5643 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5644 eager->tcp_oobinline = tcp->tcp_oobinline; 5645 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5646 eager->tcp_broadcast = tcp->tcp_broadcast; 5647 eager->tcp_useloopback = tcp->tcp_useloopback; 5648 eager->tcp_dontroute = tcp->tcp_dontroute; 5649 eager->tcp_debug = tcp->tcp_debug; 5650 eager->tcp_linger = tcp->tcp_linger; 5651 eager->tcp_lingertime = tcp->tcp_lingertime; 5652 if (tcp->tcp_ka_enabled) 5653 eager->tcp_ka_enabled = 1; 5654 5655 /* Set the IP options */ 5656 econnp->conn_broadcast = connp->conn_broadcast; 5657 econnp->conn_loopback = connp->conn_loopback; 5658 econnp->conn_dontroute = connp->conn_dontroute; 5659 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5660 5661 /* Put a ref on the listener for the eager. */ 5662 CONN_INC_REF(connp); 5663 mutex_enter(&tcp->tcp_eager_lock); 5664 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5665 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5666 tcp->tcp_eager_next_q0 = eager; 5667 eager->tcp_eager_prev_q0 = tcp; 5668 5669 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5670 eager->tcp_listener = tcp; 5671 eager->tcp_saved_listener = tcp; 5672 5673 /* 5674 * Tag this detached tcp vector for later retrieval 5675 * by our listener client in tcp_accept(). 5676 */ 5677 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5678 tcp->tcp_conn_req_cnt_q0++; 5679 if (++tcp->tcp_conn_req_seqnum == -1) { 5680 /* 5681 * -1 is "special" and defined in TPI as something 5682 * that should never be used in T_CONN_IND 5683 */ 5684 ++tcp->tcp_conn_req_seqnum; 5685 } 5686 mutex_exit(&tcp->tcp_eager_lock); 5687 5688 if (tcp->tcp_syn_defense) { 5689 /* Don't drop the SYN that comes from a good IP source */ 5690 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5691 if (addr_cache != NULL && eager->tcp_remote == 5692 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5693 eager->tcp_dontdrop = B_TRUE; 5694 } 5695 } 5696 5697 /* 5698 * We need to insert the eager in its own perimeter but as soon 5699 * as we do that, we expose the eager to the classifier and 5700 * should not touch any field outside the eager's perimeter. 5701 * So do all the work necessary before inserting the eager 5702 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5703 * will succeed but undo everything if it fails. 5704 */ 5705 seg_seq = ABE32_TO_U32(tcph->th_seq); 5706 eager->tcp_irs = seg_seq; 5707 eager->tcp_rack = seg_seq; 5708 eager->tcp_rnxt = seg_seq + 1; 5709 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5710 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5711 eager->tcp_state = TCPS_SYN_RCVD; 5712 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5713 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5714 if (mp1 == NULL) { 5715 /* 5716 * Increment the ref count as we are going to 5717 * enqueueing an mp in squeue 5718 */ 5719 CONN_INC_REF(econnp); 5720 goto error; 5721 } 5722 DB_CPID(mp1) = tcp->tcp_cpid; 5723 mblk_setcred(mp1, tcp->tcp_cred); 5724 eager->tcp_cpid = tcp->tcp_cpid; 5725 eager->tcp_open_time = lbolt64; 5726 5727 /* 5728 * We need to start the rto timer. In normal case, we start 5729 * the timer after sending the packet on the wire (or at 5730 * least believing that packet was sent by waiting for 5731 * CALL_IP_WPUT() to return). Since this is the first packet 5732 * being sent on the wire for the eager, our initial tcp_rto 5733 * is at least tcp_rexmit_interval_min which is a fairly 5734 * large value to allow the algorithm to adjust slowly to large 5735 * fluctuations of RTT during first few transmissions. 5736 * 5737 * Starting the timer first and then sending the packet in this 5738 * case shouldn't make much difference since tcp_rexmit_interval_min 5739 * is of the order of several 100ms and starting the timer 5740 * first and then sending the packet will result in difference 5741 * of few micro seconds. 5742 * 5743 * Without this optimization, we are forced to hold the fanout 5744 * lock across the ipcl_bind_insert() and sending the packet 5745 * so that we don't race against an incoming packet (maybe RST) 5746 * for this eager. 5747 * 5748 * It is necessary to acquire an extra reference on the eager 5749 * at this point and hold it until after tcp_send_data() to 5750 * ensure against an eager close race. 5751 */ 5752 5753 CONN_INC_REF(eager->tcp_connp); 5754 5755 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5756 5757 /* 5758 * Insert the eager in its own perimeter now. We are ready to deal 5759 * with any packets on eager. 5760 */ 5761 if (eager->tcp_ipversion == IPV4_VERSION) { 5762 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5763 goto error; 5764 } 5765 } else { 5766 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5767 goto error; 5768 } 5769 } 5770 5771 /* mark conn as fully-bound */ 5772 econnp->conn_fully_bound = B_TRUE; 5773 5774 /* Send the SYN-ACK */ 5775 tcp_send_data(eager, eager->tcp_wq, mp1); 5776 CONN_DEC_REF(eager->tcp_connp); 5777 freemsg(mp); 5778 5779 return; 5780 error: 5781 freemsg(mp1); 5782 eager->tcp_closemp_used = B_TRUE; 5783 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5784 mp1 = &eager->tcp_closemp; 5785 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5786 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5787 5788 /* 5789 * If a connection already exists, send the mp to that connections so 5790 * that it can be appropriately dealt with. 5791 */ 5792 ipst = tcps->tcps_netstack->netstack_ip; 5793 5794 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5795 if (!IPCL_IS_CONNECTED(econnp)) { 5796 /* 5797 * Something bad happened. ipcl_conn_insert() 5798 * failed because a connection already existed 5799 * in connected hash but we can't find it 5800 * anymore (someone blew it away). Just 5801 * free this message and hopefully remote 5802 * will retransmit at which time the SYN can be 5803 * treated as a new connection or dealth with 5804 * a TH_RST if a connection already exists. 5805 */ 5806 CONN_DEC_REF(econnp); 5807 freemsg(mp); 5808 } else { 5809 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5810 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5811 } 5812 } else { 5813 /* Nobody wants this packet */ 5814 freemsg(mp); 5815 } 5816 return; 5817 error3: 5818 CONN_DEC_REF(econnp); 5819 error2: 5820 freemsg(mp); 5821 } 5822 5823 /* 5824 * In an ideal case of vertical partition in NUMA architecture, its 5825 * beneficial to have the listener and all the incoming connections 5826 * tied to the same squeue. The other constraint is that incoming 5827 * connections should be tied to the squeue attached to interrupted 5828 * CPU for obvious locality reason so this leaves the listener to 5829 * be tied to the same squeue. Our only problem is that when listener 5830 * is binding, the CPU that will get interrupted by the NIC whose 5831 * IP address the listener is binding to is not even known. So 5832 * the code below allows us to change that binding at the time the 5833 * CPU is interrupted by virtue of incoming connection's squeue. 5834 * 5835 * This is usefull only in case of a listener bound to a specific IP 5836 * address. For other kind of listeners, they get bound the 5837 * very first time and there is no attempt to rebind them. 5838 */ 5839 void 5840 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5841 { 5842 conn_t *connp = (conn_t *)arg; 5843 squeue_t *sqp = (squeue_t *)arg2; 5844 squeue_t *new_sqp; 5845 uint32_t conn_flags; 5846 5847 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5848 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5849 } else { 5850 goto done; 5851 } 5852 5853 if (connp->conn_fanout == NULL) 5854 goto done; 5855 5856 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5857 mutex_enter(&connp->conn_fanout->connf_lock); 5858 mutex_enter(&connp->conn_lock); 5859 /* 5860 * No one from read or write side can access us now 5861 * except for already queued packets on this squeue. 5862 * But since we haven't changed the squeue yet, they 5863 * can't execute. If they are processed after we have 5864 * changed the squeue, they are sent back to the 5865 * correct squeue down below. 5866 * But a listner close can race with processing of 5867 * incoming SYN. If incoming SYN processing changes 5868 * the squeue then the listener close which is waiting 5869 * to enter the squeue would operate on the wrong 5870 * squeue. Hence we don't change the squeue here unless 5871 * the refcount is exactly the minimum refcount. The 5872 * minimum refcount of 4 is counted as - 1 each for 5873 * TCP and IP, 1 for being in the classifier hash, and 5874 * 1 for the mblk being processed. 5875 */ 5876 5877 if (connp->conn_ref != 4 || 5878 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5879 mutex_exit(&connp->conn_lock); 5880 mutex_exit(&connp->conn_fanout->connf_lock); 5881 goto done; 5882 } 5883 if (connp->conn_sqp != new_sqp) { 5884 while (connp->conn_sqp != new_sqp) 5885 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5886 } 5887 5888 do { 5889 conn_flags = connp->conn_flags; 5890 conn_flags |= IPCL_FULLY_BOUND; 5891 (void) cas32(&connp->conn_flags, connp->conn_flags, 5892 conn_flags); 5893 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5894 5895 mutex_exit(&connp->conn_fanout->connf_lock); 5896 mutex_exit(&connp->conn_lock); 5897 } 5898 5899 done: 5900 if (connp->conn_sqp != sqp) { 5901 CONN_INC_REF(connp); 5902 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5903 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5904 } else { 5905 tcp_conn_request(connp, mp, sqp); 5906 } 5907 } 5908 5909 /* 5910 * Successful connect request processing begins when our client passes 5911 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5912 * our T_OK_ACK reply message upstream. The control flow looks like this: 5913 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5914 * upstream <- tcp_rput() <- IP 5915 * After various error checks are completed, tcp_tpi_connect() lays 5916 * the target address and port into the composite header template, 5917 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5918 * request followed by an IRE request, and passes the three mblk message 5919 * down to IP looking like this: 5920 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5921 * Processing continues in tcp_rput() when we receive the following message: 5922 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5923 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5924 * to fire off the connection request, and then passes the T_OK_ACK mblk 5925 * upstream that we filled in below. There are, of course, numerous 5926 * error conditions along the way which truncate the processing described 5927 * above. 5928 */ 5929 static void 5930 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5931 { 5932 sin_t *sin; 5933 queue_t *q = tcp->tcp_wq; 5934 struct T_conn_req *tcr; 5935 struct sockaddr *sa; 5936 socklen_t len; 5937 int error; 5938 5939 tcr = (struct T_conn_req *)mp->b_rptr; 5940 5941 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5942 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5943 tcp_err_ack(tcp, mp, TPROTO, 0); 5944 return; 5945 } 5946 5947 /* 5948 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5949 * will always have that to send up. Otherwise, we need to do 5950 * special handling in case the allocation fails at that time. 5951 * If the end point is TPI, the tcp_t can be reused and the 5952 * tcp_ordrel_mp may be allocated already. 5953 */ 5954 if (tcp->tcp_ordrel_mp == NULL) { 5955 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5956 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5957 return; 5958 } 5959 } 5960 5961 /* 5962 * Determine packet type based on type of address passed in 5963 * the request should contain an IPv4 or IPv6 address. 5964 * Make sure that address family matches the type of 5965 * family of the the address passed down 5966 */ 5967 switch (tcr->DEST_length) { 5968 default: 5969 tcp_err_ack(tcp, mp, TBADADDR, 0); 5970 return; 5971 5972 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5973 /* 5974 * XXX: The check for valid DEST_length was not there 5975 * in earlier releases and some buggy 5976 * TLI apps (e.g Sybase) got away with not feeding 5977 * in sin_zero part of address. 5978 * We allow that bug to keep those buggy apps humming. 5979 * Test suites require the check on DEST_length. 5980 * We construct a new mblk with valid DEST_length 5981 * free the original so the rest of the code does 5982 * not have to keep track of this special shorter 5983 * length address case. 5984 */ 5985 mblk_t *nmp; 5986 struct T_conn_req *ntcr; 5987 sin_t *nsin; 5988 5989 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5990 tcr->OPT_length, BPRI_HI); 5991 if (nmp == NULL) { 5992 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5993 return; 5994 } 5995 ntcr = (struct T_conn_req *)nmp->b_rptr; 5996 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5997 ntcr->PRIM_type = T_CONN_REQ; 5998 ntcr->DEST_length = sizeof (sin_t); 5999 ntcr->DEST_offset = sizeof (struct T_conn_req); 6000 6001 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6002 *nsin = sin_null; 6003 /* Get pointer to shorter address to copy from original mp */ 6004 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6005 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6006 if (sin == NULL || !OK_32PTR((char *)sin)) { 6007 freemsg(nmp); 6008 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6009 return; 6010 } 6011 nsin->sin_family = sin->sin_family; 6012 nsin->sin_port = sin->sin_port; 6013 nsin->sin_addr = sin->sin_addr; 6014 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6015 nmp->b_wptr = (uchar_t *)&nsin[1]; 6016 if (tcr->OPT_length != 0) { 6017 ntcr->OPT_length = tcr->OPT_length; 6018 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6019 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6020 (uchar_t *)ntcr + ntcr->OPT_offset, 6021 tcr->OPT_length); 6022 nmp->b_wptr += tcr->OPT_length; 6023 } 6024 freemsg(mp); /* original mp freed */ 6025 mp = nmp; /* re-initialize original variables */ 6026 tcr = ntcr; 6027 } 6028 /* FALLTHRU */ 6029 6030 case sizeof (sin_t): 6031 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6032 sizeof (sin_t)); 6033 len = sizeof (sin_t); 6034 break; 6035 6036 case sizeof (sin6_t): 6037 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6038 sizeof (sin6_t)); 6039 len = sizeof (sin6_t); 6040 break; 6041 } 6042 6043 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6044 if (error != 0) { 6045 tcp_err_ack(tcp, mp, TSYSERR, error); 6046 return; 6047 } 6048 6049 /* 6050 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6051 * should key on their sequence number and cut them loose. 6052 */ 6053 6054 /* 6055 * If options passed in, feed it for verification and handling 6056 */ 6057 if (tcr->OPT_length != 0) { 6058 mblk_t *ok_mp; 6059 mblk_t *discon_mp; 6060 mblk_t *conn_opts_mp; 6061 int t_error, sys_error, do_disconnect; 6062 6063 conn_opts_mp = NULL; 6064 6065 if (tcp_conprim_opt_process(tcp, mp, 6066 &do_disconnect, &t_error, &sys_error) < 0) { 6067 if (do_disconnect) { 6068 ASSERT(t_error == 0 && sys_error == 0); 6069 discon_mp = mi_tpi_discon_ind(NULL, 6070 ECONNREFUSED, 0); 6071 if (!discon_mp) { 6072 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6073 TSYSERR, ENOMEM); 6074 return; 6075 } 6076 ok_mp = mi_tpi_ok_ack_alloc(mp); 6077 if (!ok_mp) { 6078 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6079 TSYSERR, ENOMEM); 6080 return; 6081 } 6082 qreply(q, ok_mp); 6083 qreply(q, discon_mp); /* no flush! */ 6084 } else { 6085 ASSERT(t_error != 0); 6086 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6087 sys_error); 6088 } 6089 return; 6090 } 6091 /* 6092 * Success in setting options, the mp option buffer represented 6093 * by OPT_length/offset has been potentially modified and 6094 * contains results of option processing. We copy it in 6095 * another mp to save it for potentially influencing returning 6096 * it in T_CONN_CONN. 6097 */ 6098 if (tcr->OPT_length != 0) { /* there are resulting options */ 6099 conn_opts_mp = copyb(mp); 6100 if (!conn_opts_mp) { 6101 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6102 TSYSERR, ENOMEM); 6103 return; 6104 } 6105 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6106 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6107 /* 6108 * Note: 6109 * These resulting option negotiation can include any 6110 * end-to-end negotiation options but there no such 6111 * thing (yet?) in our TCP/IP. 6112 */ 6113 } 6114 } 6115 6116 /* call the non-TPI version */ 6117 error = tcp_do_connect(tcp->tcp_connp, sa, len, DB_CRED(mp), 6118 DB_CPID(mp)); 6119 if (error < 0) { 6120 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6121 } else if (error > 0) { 6122 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6123 } else { 6124 mp = mi_tpi_ok_ack_alloc(mp); 6125 } 6126 6127 /* 6128 * Note: Code below is the "failure" case 6129 */ 6130 /* return error ack and blow away saved option results if any */ 6131 connect_failed: 6132 if (mp != NULL) 6133 putnext(tcp->tcp_rq, mp); 6134 else { 6135 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6136 TSYSERR, ENOMEM); 6137 } 6138 } 6139 6140 /* 6141 * Handle connect to IPv4 destinations, including connections for AF_INET6 6142 * sockets connecting to IPv4 mapped IPv6 destinations. 6143 */ 6144 static int 6145 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6146 uint_t srcid, cred_t *cr, pid_t pid) 6147 { 6148 tcph_t *tcph; 6149 mblk_t *mp; 6150 ipaddr_t dstaddr = *dstaddrp; 6151 int32_t oldstate; 6152 uint16_t lport; 6153 int error = 0; 6154 tcp_stack_t *tcps = tcp->tcp_tcps; 6155 6156 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6157 6158 /* Check for attempt to connect to INADDR_ANY */ 6159 if (dstaddr == INADDR_ANY) { 6160 /* 6161 * SunOS 4.x and 4.3 BSD allow an application 6162 * to connect a TCP socket to INADDR_ANY. 6163 * When they do this, the kernel picks the 6164 * address of one interface and uses it 6165 * instead. The kernel usually ends up 6166 * picking the address of the loopback 6167 * interface. This is an undocumented feature. 6168 * However, we provide the same thing here 6169 * in order to have source and binary 6170 * compatibility with SunOS 4.x. 6171 * Update the T_CONN_REQ (sin/sin6) since it is used to 6172 * generate the T_CONN_CON. 6173 */ 6174 dstaddr = htonl(INADDR_LOOPBACK); 6175 *dstaddrp = dstaddr; 6176 } 6177 6178 /* Handle __sin6_src_id if socket not bound to an IP address */ 6179 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6180 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6181 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6182 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6183 tcp->tcp_ipha->ipha_src); 6184 } 6185 6186 /* 6187 * Don't let an endpoint connect to itself. Note that 6188 * the test here does not catch the case where the 6189 * source IP addr was left unspecified by the user. In 6190 * this case, the source addr is set in tcp_adapt_ire() 6191 * using the reply to the T_BIND message that we send 6192 * down to IP here and the check is repeated in tcp_rput_other. 6193 */ 6194 if (dstaddr == tcp->tcp_ipha->ipha_src && 6195 dstport == tcp->tcp_lport) { 6196 error = -TBADADDR; 6197 goto failed; 6198 } 6199 6200 tcp->tcp_ipha->ipha_dst = dstaddr; 6201 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6202 6203 /* 6204 * Massage a source route if any putting the first hop 6205 * in iph_dst. Compute a starting value for the checksum which 6206 * takes into account that the original iph_dst should be 6207 * included in the checksum but that ip will include the 6208 * first hop in the source route in the tcp checksum. 6209 */ 6210 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6211 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6212 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6213 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6214 if ((int)tcp->tcp_sum < 0) 6215 tcp->tcp_sum--; 6216 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6217 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6218 (tcp->tcp_sum >> 16)); 6219 tcph = tcp->tcp_tcph; 6220 *(uint16_t *)tcph->th_fport = dstport; 6221 tcp->tcp_fport = dstport; 6222 6223 oldstate = tcp->tcp_state; 6224 /* 6225 * At this point the remote destination address and remote port fields 6226 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6227 * have to see which state tcp was in so we can take apropriate action. 6228 */ 6229 if (oldstate == TCPS_IDLE) { 6230 /* 6231 * We support a quick connect capability here, allowing 6232 * clients to transition directly from IDLE to SYN_SENT 6233 * tcp_bindi will pick an unused port, insert the connection 6234 * in the bind hash and transition to BOUND state. 6235 */ 6236 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6237 tcp, B_TRUE); 6238 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6239 B_FALSE, B_FALSE); 6240 if (lport == 0) { 6241 error = -TNOADDR; 6242 goto failed; 6243 } 6244 } 6245 tcp->tcp_state = TCPS_SYN_SENT; 6246 6247 mp = allocb(sizeof (ire_t), BPRI_HI); 6248 if (mp == NULL) { 6249 tcp->tcp_state = oldstate; 6250 error = ENOMEM; 6251 goto failed; 6252 } 6253 mp->b_wptr += sizeof (ire_t); 6254 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6255 tcp->tcp_hard_binding = 1; 6256 if (cr == NULL) { 6257 cr = tcp->tcp_cred; 6258 pid = tcp->tcp_cpid; 6259 } 6260 mblk_setcred(mp, cr); 6261 DB_CPID(mp) = pid; 6262 6263 /* 6264 * We need to make sure that the conn_recv is set to a non-null 6265 * value before we insert the conn_t into the classifier table. 6266 * This is to avoid a race with an incoming packet which does 6267 * an ipcl_classify(). 6268 */ 6269 tcp->tcp_connp->conn_recv = tcp_input; 6270 6271 if (tcp->tcp_family == AF_INET) { 6272 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6273 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6274 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE); 6275 } else { 6276 in6_addr_t v6src; 6277 if (tcp->tcp_ipversion == IPV4_VERSION) { 6278 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6279 } else { 6280 v6src = tcp->tcp_ip6h->ip6_src; 6281 } 6282 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6283 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6284 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE); 6285 } 6286 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6287 tcp->tcp_active_open = 1; 6288 6289 return (tcp_post_ip_bind(tcp, mp, error)); 6290 failed: 6291 /* return error ack and blow away saved option results if any */ 6292 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6293 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6294 return (error); 6295 } 6296 6297 /* 6298 * Handle connect to IPv6 destinations. 6299 */ 6300 static int 6301 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6302 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6303 { 6304 tcph_t *tcph; 6305 mblk_t *mp; 6306 ip6_rthdr_t *rth; 6307 int32_t oldstate; 6308 uint16_t lport; 6309 tcp_stack_t *tcps = tcp->tcp_tcps; 6310 int error = 0; 6311 conn_t *connp = tcp->tcp_connp; 6312 6313 ASSERT(tcp->tcp_family == AF_INET6); 6314 6315 /* 6316 * If we're here, it means that the destination address is a native 6317 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6318 * reason why it might not be IPv6 is if the socket was bound to an 6319 * IPv4-mapped IPv6 address. 6320 */ 6321 if (tcp->tcp_ipversion != IPV6_VERSION) { 6322 return (-TBADADDR); 6323 } 6324 6325 /* 6326 * Interpret a zero destination to mean loopback. 6327 * Update the T_CONN_REQ (sin/sin6) since it is used to 6328 * generate the T_CONN_CON. 6329 */ 6330 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6331 *dstaddrp = ipv6_loopback; 6332 } 6333 6334 /* Handle __sin6_src_id if socket not bound to an IP address */ 6335 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6336 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6337 connp->conn_zoneid, tcps->tcps_netstack); 6338 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6339 } 6340 6341 /* 6342 * Take care of the scope_id now and add ip6i_t 6343 * if ip6i_t is not already allocated through TCP 6344 * sticky options. At this point tcp_ip6h does not 6345 * have dst info, thus use dstaddrp. 6346 */ 6347 if (scope_id != 0 && 6348 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6349 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6350 ip6i_t *ip6i; 6351 6352 ipp->ipp_ifindex = scope_id; 6353 ip6i = (ip6i_t *)tcp->tcp_iphc; 6354 6355 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6356 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6357 /* Already allocated */ 6358 ip6i->ip6i_flags |= IP6I_IFINDEX; 6359 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6360 ipp->ipp_fields |= IPPF_SCOPE_ID; 6361 } else { 6362 int reterr; 6363 6364 ipp->ipp_fields |= IPPF_SCOPE_ID; 6365 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6366 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6367 reterr = tcp_build_hdrs(tcp); 6368 if (reterr != 0) 6369 goto failed; 6370 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6371 } 6372 } 6373 6374 /* 6375 * Don't let an endpoint connect to itself. Note that 6376 * the test here does not catch the case where the 6377 * source IP addr was left unspecified by the user. In 6378 * this case, the source addr is set in tcp_adapt_ire() 6379 * using the reply to the T_BIND message that we send 6380 * down to IP here and the check is repeated in tcp_rput_other. 6381 */ 6382 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6383 (dstport == tcp->tcp_lport)) { 6384 error = -TBADADDR; 6385 goto failed; 6386 } 6387 6388 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6389 tcp->tcp_remote_v6 = *dstaddrp; 6390 tcp->tcp_ip6h->ip6_vcf = 6391 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6392 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6393 6394 /* 6395 * Massage a routing header (if present) putting the first hop 6396 * in ip6_dst. Compute a starting value for the checksum which 6397 * takes into account that the original ip6_dst should be 6398 * included in the checksum but that ip will include the 6399 * first hop in the source route in the tcp checksum. 6400 */ 6401 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6402 if (rth != NULL) { 6403 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6404 tcps->tcps_netstack); 6405 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6406 (tcp->tcp_sum >> 16)); 6407 } else { 6408 tcp->tcp_sum = 0; 6409 } 6410 6411 tcph = tcp->tcp_tcph; 6412 *(uint16_t *)tcph->th_fport = dstport; 6413 tcp->tcp_fport = dstport; 6414 6415 oldstate = tcp->tcp_state; 6416 /* 6417 * At this point the remote destination address and remote port fields 6418 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6419 * have to see which state tcp was in so we can take apropriate action. 6420 */ 6421 if (oldstate == TCPS_IDLE) { 6422 /* 6423 * We support a quick connect capability here, allowing 6424 * clients to transition directly from IDLE to SYN_SENT 6425 * tcp_bindi will pick an unused port, insert the connection 6426 * in the bind hash and transition to BOUND state. 6427 */ 6428 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6429 tcp, B_TRUE); 6430 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6431 B_FALSE, B_FALSE); 6432 if (lport == 0) { 6433 error = -TNOADDR; 6434 goto failed; 6435 } 6436 } 6437 tcp->tcp_state = TCPS_SYN_SENT; 6438 6439 mp = allocb(sizeof (ire_t), BPRI_HI); 6440 if (mp != NULL) { 6441 in6_addr_t v6src; 6442 6443 mp->b_wptr += sizeof (ire_t); 6444 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6445 if (cr == NULL) { 6446 cr = tcp->tcp_cred; 6447 pid = tcp->tcp_cpid; 6448 } 6449 mblk_setcred(mp, cr); 6450 DB_CPID(mp) = pid; 6451 tcp->tcp_hard_binding = 1; 6452 6453 /* 6454 * We need to make sure that the conn_recv is set to a non-null 6455 * value before we insert the conn_t into the classifier table. 6456 * This is to avoid a race with an incoming packet which does 6457 * an ipcl_classify(). 6458 */ 6459 tcp->tcp_connp->conn_recv = tcp_input; 6460 6461 if (tcp->tcp_ipversion == IPV4_VERSION) { 6462 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6463 } else { 6464 v6src = tcp->tcp_ip6h->ip6_src; 6465 } 6466 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6467 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6468 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE); 6469 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6470 tcp->tcp_active_open = 1; 6471 6472 return (tcp_post_ip_bind(tcp, mp, error)); 6473 } 6474 /* Error case */ 6475 tcp->tcp_state = oldstate; 6476 error = ENOMEM; 6477 6478 failed: 6479 /* return error ack and blow away saved option results if any */ 6480 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6481 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6482 return (error); 6483 } 6484 6485 /* 6486 * We need a stream q for detached closing tcp connections 6487 * to use. Our client hereby indicates that this q is the 6488 * one to use. 6489 */ 6490 static void 6491 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6492 { 6493 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6494 queue_t *q = tcp->tcp_wq; 6495 tcp_stack_t *tcps = tcp->tcp_tcps; 6496 6497 #ifdef NS_DEBUG 6498 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6499 tcps->tcps_netstack->netstack_stackid); 6500 #endif 6501 mp->b_datap->db_type = M_IOCACK; 6502 iocp->ioc_count = 0; 6503 mutex_enter(&tcps->tcps_g_q_lock); 6504 if (tcps->tcps_g_q != NULL) { 6505 mutex_exit(&tcps->tcps_g_q_lock); 6506 iocp->ioc_error = EALREADY; 6507 } else { 6508 int error = 0; 6509 conn_t *connp = tcp->tcp_connp; 6510 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6511 6512 tcps->tcps_g_q = tcp->tcp_rq; 6513 mutex_exit(&tcps->tcps_g_q_lock); 6514 iocp->ioc_error = 0; 6515 iocp->ioc_rval = 0; 6516 /* 6517 * We are passing tcp_sticky_ipp as NULL 6518 * as it is not useful for tcp_default queue 6519 * 6520 * Set conn_recv just in case. 6521 */ 6522 tcp->tcp_connp->conn_recv = tcp_conn_request; 6523 6524 ASSERT(connp->conn_af_isv6); 6525 connp->conn_ulp = IPPROTO_TCP; 6526 6527 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6528 NULL || connp->conn_mac_exempt) { 6529 error = -TBADADDR; 6530 } else { 6531 connp->conn_srcv6 = ipv6_all_zeros; 6532 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6533 } 6534 6535 (void) tcp_post_ip_bind(tcp, NULL, error); 6536 } 6537 qreply(q, mp); 6538 } 6539 6540 static int 6541 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6542 { 6543 tcp_t *ltcp = NULL; 6544 conn_t *connp; 6545 tcp_stack_t *tcps = tcp->tcp_tcps; 6546 6547 /* 6548 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6549 * when the stream is in BOUND state. Do not send a reset, 6550 * since the destination IP address is not valid, and it can 6551 * be the initialized value of all zeros (broadcast address). 6552 * 6553 * XXX There won't be any pending bind request to IP. 6554 */ 6555 if (tcp->tcp_state <= TCPS_BOUND) { 6556 if (tcp->tcp_debug) { 6557 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6558 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6559 } 6560 return (TOUTSTATE); 6561 } 6562 6563 6564 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6565 6566 /* 6567 * According to TPI, for non-listeners, ignore seqnum 6568 * and disconnect. 6569 * Following interpretation of -1 seqnum is historical 6570 * and implied TPI ? (TPI only states that for T_CONN_IND, 6571 * a valid seqnum should not be -1). 6572 * 6573 * -1 means disconnect everything 6574 * regardless even on a listener. 6575 */ 6576 6577 int old_state = tcp->tcp_state; 6578 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6579 6580 /* 6581 * The connection can't be on the tcp_time_wait_head list 6582 * since it is not detached. 6583 */ 6584 ASSERT(tcp->tcp_time_wait_next == NULL); 6585 ASSERT(tcp->tcp_time_wait_prev == NULL); 6586 ASSERT(tcp->tcp_time_wait_expire == 0); 6587 ltcp = NULL; 6588 /* 6589 * If it used to be a listener, check to make sure no one else 6590 * has taken the port before switching back to LISTEN state. 6591 */ 6592 if (tcp->tcp_ipversion == IPV4_VERSION) { 6593 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6594 tcp->tcp_ipha->ipha_src, 6595 tcp->tcp_connp->conn_zoneid, ipst); 6596 if (connp != NULL) 6597 ltcp = connp->conn_tcp; 6598 } else { 6599 /* Allow tcp_bound_if listeners? */ 6600 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6601 &tcp->tcp_ip6h->ip6_src, 0, 6602 tcp->tcp_connp->conn_zoneid, ipst); 6603 if (connp != NULL) 6604 ltcp = connp->conn_tcp; 6605 } 6606 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6607 tcp->tcp_state = TCPS_LISTEN; 6608 } else if (old_state > TCPS_BOUND) { 6609 tcp->tcp_conn_req_max = 0; 6610 tcp->tcp_state = TCPS_BOUND; 6611 } 6612 if (ltcp != NULL) 6613 CONN_DEC_REF(ltcp->tcp_connp); 6614 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6615 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6616 } else if (old_state == TCPS_ESTABLISHED || 6617 old_state == TCPS_CLOSE_WAIT) { 6618 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6619 } 6620 6621 if (tcp->tcp_fused) 6622 tcp_unfuse(tcp); 6623 6624 mutex_enter(&tcp->tcp_eager_lock); 6625 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6626 (tcp->tcp_conn_req_cnt_q != 0)) { 6627 tcp_eager_cleanup(tcp, 0); 6628 } 6629 mutex_exit(&tcp->tcp_eager_lock); 6630 6631 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6632 tcp->tcp_rnxt, TH_RST | TH_ACK); 6633 6634 tcp_reinit(tcp); 6635 6636 return (0); 6637 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6638 return (TBADSEQ); 6639 } 6640 return (0); 6641 } 6642 6643 /* 6644 * Our client hereby directs us to reject the connection request 6645 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6646 * of sending the appropriate RST, not an ICMP error. 6647 */ 6648 static void 6649 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6650 { 6651 t_scalar_t seqnum; 6652 int error; 6653 6654 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6655 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6656 tcp_err_ack(tcp, mp, TPROTO, 0); 6657 return; 6658 } 6659 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6660 error = tcp_disconnect_common(tcp, seqnum); 6661 if (error != 0) 6662 tcp_err_ack(tcp, mp, error, 0); 6663 else { 6664 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6665 /* Send M_FLUSH according to TPI */ 6666 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6667 } 6668 mp = mi_tpi_ok_ack_alloc(mp); 6669 if (mp) 6670 putnext(tcp->tcp_rq, mp); 6671 } 6672 } 6673 6674 /* 6675 * Diagnostic routine used to return a string associated with the tcp state. 6676 * Note that if the caller does not supply a buffer, it will use an internal 6677 * static string. This means that if multiple threads call this function at 6678 * the same time, output can be corrupted... Note also that this function 6679 * does not check the size of the supplied buffer. The caller has to make 6680 * sure that it is big enough. 6681 */ 6682 static char * 6683 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6684 { 6685 char buf1[30]; 6686 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6687 char *buf; 6688 char *cp; 6689 in6_addr_t local, remote; 6690 char local_addrbuf[INET6_ADDRSTRLEN]; 6691 char remote_addrbuf[INET6_ADDRSTRLEN]; 6692 6693 if (sup_buf != NULL) 6694 buf = sup_buf; 6695 else 6696 buf = priv_buf; 6697 6698 if (tcp == NULL) 6699 return ("NULL_TCP"); 6700 switch (tcp->tcp_state) { 6701 case TCPS_CLOSED: 6702 cp = "TCP_CLOSED"; 6703 break; 6704 case TCPS_IDLE: 6705 cp = "TCP_IDLE"; 6706 break; 6707 case TCPS_BOUND: 6708 cp = "TCP_BOUND"; 6709 break; 6710 case TCPS_LISTEN: 6711 cp = "TCP_LISTEN"; 6712 break; 6713 case TCPS_SYN_SENT: 6714 cp = "TCP_SYN_SENT"; 6715 break; 6716 case TCPS_SYN_RCVD: 6717 cp = "TCP_SYN_RCVD"; 6718 break; 6719 case TCPS_ESTABLISHED: 6720 cp = "TCP_ESTABLISHED"; 6721 break; 6722 case TCPS_CLOSE_WAIT: 6723 cp = "TCP_CLOSE_WAIT"; 6724 break; 6725 case TCPS_FIN_WAIT_1: 6726 cp = "TCP_FIN_WAIT_1"; 6727 break; 6728 case TCPS_CLOSING: 6729 cp = "TCP_CLOSING"; 6730 break; 6731 case TCPS_LAST_ACK: 6732 cp = "TCP_LAST_ACK"; 6733 break; 6734 case TCPS_FIN_WAIT_2: 6735 cp = "TCP_FIN_WAIT_2"; 6736 break; 6737 case TCPS_TIME_WAIT: 6738 cp = "TCP_TIME_WAIT"; 6739 break; 6740 default: 6741 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6742 cp = buf1; 6743 break; 6744 } 6745 switch (format) { 6746 case DISP_ADDR_AND_PORT: 6747 if (tcp->tcp_ipversion == IPV4_VERSION) { 6748 /* 6749 * Note that we use the remote address in the tcp_b 6750 * structure. This means that it will print out 6751 * the real destination address, not the next hop's 6752 * address if source routing is used. 6753 */ 6754 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6755 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6756 6757 } else { 6758 local = tcp->tcp_ip_src_v6; 6759 remote = tcp->tcp_remote_v6; 6760 } 6761 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6762 sizeof (local_addrbuf)); 6763 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6764 sizeof (remote_addrbuf)); 6765 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6766 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6767 ntohs(tcp->tcp_fport), cp); 6768 break; 6769 case DISP_PORT_ONLY: 6770 default: 6771 (void) mi_sprintf(buf, "[%u, %u] %s", 6772 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6773 break; 6774 } 6775 6776 return (buf); 6777 } 6778 6779 /* 6780 * Called via squeue to get on to eager's perimeter. It sends a 6781 * TH_RST if eager is in the fanout table. The listener wants the 6782 * eager to disappear either by means of tcp_eager_blowoff() or 6783 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6784 * called (via squeue) if the eager cannot be inserted in the 6785 * fanout table in tcp_conn_request(). 6786 */ 6787 /* ARGSUSED */ 6788 void 6789 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6790 { 6791 conn_t *econnp = (conn_t *)arg; 6792 tcp_t *eager = econnp->conn_tcp; 6793 tcp_t *listener = eager->tcp_listener; 6794 tcp_stack_t *tcps = eager->tcp_tcps; 6795 6796 /* 6797 * We could be called because listener is closing. Since 6798 * the eager is using listener's queue's, its not safe. 6799 * Better use the default queue just to send the TH_RST 6800 * out. 6801 */ 6802 ASSERT(tcps->tcps_g_q != NULL); 6803 eager->tcp_rq = tcps->tcps_g_q; 6804 eager->tcp_wq = WR(tcps->tcps_g_q); 6805 6806 /* 6807 * An eager's conn_fanout will be NULL if it's a duplicate 6808 * for an existing 4-tuples in the conn fanout table. 6809 * We don't want to send an RST out in such case. 6810 */ 6811 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6812 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6813 eager, eager->tcp_snxt, 0, TH_RST); 6814 } 6815 6816 /* We are here because listener wants this eager gone */ 6817 if (listener != NULL) { 6818 mutex_enter(&listener->tcp_eager_lock); 6819 tcp_eager_unlink(eager); 6820 if (eager->tcp_tconnind_started) { 6821 /* 6822 * The eager has sent a conn_ind up to the 6823 * listener but listener decides to close 6824 * instead. We need to drop the extra ref 6825 * placed on eager in tcp_rput_data() before 6826 * sending the conn_ind to listener. 6827 */ 6828 CONN_DEC_REF(econnp); 6829 } 6830 mutex_exit(&listener->tcp_eager_lock); 6831 CONN_DEC_REF(listener->tcp_connp); 6832 } 6833 6834 if (eager->tcp_state > TCPS_BOUND) 6835 tcp_close_detached(eager); 6836 } 6837 6838 /* 6839 * Reset any eager connection hanging off this listener marked 6840 * with 'seqnum' and then reclaim it's resources. 6841 */ 6842 static boolean_t 6843 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6844 { 6845 tcp_t *eager; 6846 mblk_t *mp; 6847 tcp_stack_t *tcps = listener->tcp_tcps; 6848 6849 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6850 eager = listener; 6851 mutex_enter(&listener->tcp_eager_lock); 6852 do { 6853 eager = eager->tcp_eager_next_q; 6854 if (eager == NULL) { 6855 mutex_exit(&listener->tcp_eager_lock); 6856 return (B_FALSE); 6857 } 6858 } while (eager->tcp_conn_req_seqnum != seqnum); 6859 6860 if (eager->tcp_closemp_used) { 6861 mutex_exit(&listener->tcp_eager_lock); 6862 return (B_TRUE); 6863 } 6864 eager->tcp_closemp_used = B_TRUE; 6865 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6866 CONN_INC_REF(eager->tcp_connp); 6867 mutex_exit(&listener->tcp_eager_lock); 6868 mp = &eager->tcp_closemp; 6869 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6870 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6871 return (B_TRUE); 6872 } 6873 6874 /* 6875 * Reset any eager connection hanging off this listener 6876 * and then reclaim it's resources. 6877 */ 6878 static void 6879 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6880 { 6881 tcp_t *eager; 6882 mblk_t *mp; 6883 tcp_stack_t *tcps = listener->tcp_tcps; 6884 6885 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6886 6887 if (!q0_only) { 6888 /* First cleanup q */ 6889 TCP_STAT(tcps, tcp_eager_blowoff_q); 6890 eager = listener->tcp_eager_next_q; 6891 while (eager != NULL) { 6892 if (!eager->tcp_closemp_used) { 6893 eager->tcp_closemp_used = B_TRUE; 6894 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6895 CONN_INC_REF(eager->tcp_connp); 6896 mp = &eager->tcp_closemp; 6897 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6898 tcp_eager_kill, eager->tcp_connp, 6899 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6900 } 6901 eager = eager->tcp_eager_next_q; 6902 } 6903 } 6904 /* Then cleanup q0 */ 6905 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6906 eager = listener->tcp_eager_next_q0; 6907 while (eager != listener) { 6908 if (!eager->tcp_closemp_used) { 6909 eager->tcp_closemp_used = B_TRUE; 6910 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6911 CONN_INC_REF(eager->tcp_connp); 6912 mp = &eager->tcp_closemp; 6913 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6914 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6915 SQTAG_TCP_EAGER_CLEANUP_Q0); 6916 } 6917 eager = eager->tcp_eager_next_q0; 6918 } 6919 } 6920 6921 /* 6922 * If we are an eager connection hanging off a listener that hasn't 6923 * formally accepted the connection yet, get off his list and blow off 6924 * any data that we have accumulated. 6925 */ 6926 static void 6927 tcp_eager_unlink(tcp_t *tcp) 6928 { 6929 tcp_t *listener = tcp->tcp_listener; 6930 6931 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6932 ASSERT(listener != NULL); 6933 if (tcp->tcp_eager_next_q0 != NULL) { 6934 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6935 6936 /* Remove the eager tcp from q0 */ 6937 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6938 tcp->tcp_eager_prev_q0; 6939 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6940 tcp->tcp_eager_next_q0; 6941 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6942 listener->tcp_conn_req_cnt_q0--; 6943 6944 tcp->tcp_eager_next_q0 = NULL; 6945 tcp->tcp_eager_prev_q0 = NULL; 6946 6947 /* 6948 * Take the eager out, if it is in the list of droppable 6949 * eagers. 6950 */ 6951 MAKE_UNDROPPABLE(tcp); 6952 6953 if (tcp->tcp_syn_rcvd_timeout != 0) { 6954 /* we have timed out before */ 6955 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6956 listener->tcp_syn_rcvd_timeout--; 6957 } 6958 } else { 6959 tcp_t **tcpp = &listener->tcp_eager_next_q; 6960 tcp_t *prev = NULL; 6961 6962 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6963 if (tcpp[0] == tcp) { 6964 if (listener->tcp_eager_last_q == tcp) { 6965 /* 6966 * If we are unlinking the last 6967 * element on the list, adjust 6968 * tail pointer. Set tail pointer 6969 * to nil when list is empty. 6970 */ 6971 ASSERT(tcp->tcp_eager_next_q == NULL); 6972 if (listener->tcp_eager_last_q == 6973 listener->tcp_eager_next_q) { 6974 listener->tcp_eager_last_q = 6975 NULL; 6976 } else { 6977 /* 6978 * We won't get here if there 6979 * is only one eager in the 6980 * list. 6981 */ 6982 ASSERT(prev != NULL); 6983 listener->tcp_eager_last_q = 6984 prev; 6985 } 6986 } 6987 tcpp[0] = tcp->tcp_eager_next_q; 6988 tcp->tcp_eager_next_q = NULL; 6989 tcp->tcp_eager_last_q = NULL; 6990 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6991 listener->tcp_conn_req_cnt_q--; 6992 break; 6993 } 6994 prev = tcpp[0]; 6995 } 6996 } 6997 tcp->tcp_listener = NULL; 6998 } 6999 7000 /* Shorthand to generate and send TPI error acks to our client */ 7001 static void 7002 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7003 { 7004 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7005 putnext(tcp->tcp_rq, mp); 7006 } 7007 7008 /* Shorthand to generate and send TPI error acks to our client */ 7009 static void 7010 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7011 int t_error, int sys_error) 7012 { 7013 struct T_error_ack *teackp; 7014 7015 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7016 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7017 teackp = (struct T_error_ack *)mp->b_rptr; 7018 teackp->ERROR_prim = primitive; 7019 teackp->TLI_error = t_error; 7020 teackp->UNIX_error = sys_error; 7021 putnext(tcp->tcp_rq, mp); 7022 } 7023 } 7024 7025 /* 7026 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7027 * but instead the code relies on: 7028 * - the fact that the address of the array and its size never changes 7029 * - the atomic assignment of the elements of the array 7030 */ 7031 /* ARGSUSED */ 7032 static int 7033 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7034 { 7035 int i; 7036 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7037 7038 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7039 if (tcps->tcps_g_epriv_ports[i] != 0) 7040 (void) mi_mpprintf(mp, "%d ", 7041 tcps->tcps_g_epriv_ports[i]); 7042 } 7043 return (0); 7044 } 7045 7046 /* 7047 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7048 * threads from changing it at the same time. 7049 */ 7050 /* ARGSUSED */ 7051 static int 7052 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7053 cred_t *cr) 7054 { 7055 long new_value; 7056 int i; 7057 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7058 7059 /* 7060 * Fail the request if the new value does not lie within the 7061 * port number limits. 7062 */ 7063 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7064 new_value <= 0 || new_value >= 65536) { 7065 return (EINVAL); 7066 } 7067 7068 mutex_enter(&tcps->tcps_epriv_port_lock); 7069 /* Check if the value is already in the list */ 7070 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7071 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7072 mutex_exit(&tcps->tcps_epriv_port_lock); 7073 return (EEXIST); 7074 } 7075 } 7076 /* Find an empty slot */ 7077 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7078 if (tcps->tcps_g_epriv_ports[i] == 0) 7079 break; 7080 } 7081 if (i == tcps->tcps_g_num_epriv_ports) { 7082 mutex_exit(&tcps->tcps_epriv_port_lock); 7083 return (EOVERFLOW); 7084 } 7085 /* Set the new value */ 7086 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7087 mutex_exit(&tcps->tcps_epriv_port_lock); 7088 return (0); 7089 } 7090 7091 /* 7092 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7093 * threads from changing it at the same time. 7094 */ 7095 /* ARGSUSED */ 7096 static int 7097 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7098 cred_t *cr) 7099 { 7100 long new_value; 7101 int i; 7102 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7103 7104 /* 7105 * Fail the request if the new value does not lie within the 7106 * port number limits. 7107 */ 7108 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7109 new_value >= 65536) { 7110 return (EINVAL); 7111 } 7112 7113 mutex_enter(&tcps->tcps_epriv_port_lock); 7114 /* Check that the value is already in the list */ 7115 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7116 if (tcps->tcps_g_epriv_ports[i] == new_value) 7117 break; 7118 } 7119 if (i == tcps->tcps_g_num_epriv_ports) { 7120 mutex_exit(&tcps->tcps_epriv_port_lock); 7121 return (ESRCH); 7122 } 7123 /* Clear the value */ 7124 tcps->tcps_g_epriv_ports[i] = 0; 7125 mutex_exit(&tcps->tcps_epriv_port_lock); 7126 return (0); 7127 } 7128 7129 /* Return the TPI/TLI equivalent of our current tcp_state */ 7130 static int 7131 tcp_tpistate(tcp_t *tcp) 7132 { 7133 switch (tcp->tcp_state) { 7134 case TCPS_IDLE: 7135 return (TS_UNBND); 7136 case TCPS_LISTEN: 7137 /* 7138 * Return whether there are outstanding T_CONN_IND waiting 7139 * for the matching T_CONN_RES. Therefore don't count q0. 7140 */ 7141 if (tcp->tcp_conn_req_cnt_q > 0) 7142 return (TS_WRES_CIND); 7143 else 7144 return (TS_IDLE); 7145 case TCPS_BOUND: 7146 return (TS_IDLE); 7147 case TCPS_SYN_SENT: 7148 return (TS_WCON_CREQ); 7149 case TCPS_SYN_RCVD: 7150 /* 7151 * Note: assumption: this has to the active open SYN_RCVD. 7152 * The passive instance is detached in SYN_RCVD stage of 7153 * incoming connection processing so we cannot get request 7154 * for T_info_ack on it. 7155 */ 7156 return (TS_WACK_CRES); 7157 case TCPS_ESTABLISHED: 7158 return (TS_DATA_XFER); 7159 case TCPS_CLOSE_WAIT: 7160 return (TS_WREQ_ORDREL); 7161 case TCPS_FIN_WAIT_1: 7162 return (TS_WIND_ORDREL); 7163 case TCPS_FIN_WAIT_2: 7164 return (TS_WIND_ORDREL); 7165 7166 case TCPS_CLOSING: 7167 case TCPS_LAST_ACK: 7168 case TCPS_TIME_WAIT: 7169 case TCPS_CLOSED: 7170 /* 7171 * Following TS_WACK_DREQ7 is a rendition of "not 7172 * yet TS_IDLE" TPI state. There is no best match to any 7173 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7174 * choose a value chosen that will map to TLI/XTI level 7175 * state of TSTATECHNG (state is process of changing) which 7176 * captures what this dummy state represents. 7177 */ 7178 return (TS_WACK_DREQ7); 7179 default: 7180 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7181 tcp->tcp_state, tcp_display(tcp, NULL, 7182 DISP_PORT_ONLY)); 7183 return (TS_UNBND); 7184 } 7185 } 7186 7187 static void 7188 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7189 { 7190 tcp_stack_t *tcps = tcp->tcp_tcps; 7191 7192 if (tcp->tcp_family == AF_INET6) 7193 *tia = tcp_g_t_info_ack_v6; 7194 else 7195 *tia = tcp_g_t_info_ack; 7196 tia->CURRENT_state = tcp_tpistate(tcp); 7197 tia->OPT_size = tcp_max_optsize; 7198 if (tcp->tcp_mss == 0) { 7199 /* Not yet set - tcp_open does not set mss */ 7200 if (tcp->tcp_ipversion == IPV4_VERSION) 7201 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7202 else 7203 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7204 } else { 7205 tia->TIDU_size = tcp->tcp_mss; 7206 } 7207 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7208 } 7209 7210 static void 7211 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7212 t_uscalar_t cap_bits1) 7213 { 7214 tcap->CAP_bits1 = 0; 7215 7216 if (cap_bits1 & TC1_INFO) { 7217 tcp_copy_info(&tcap->INFO_ack, tcp); 7218 tcap->CAP_bits1 |= TC1_INFO; 7219 } 7220 7221 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7222 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7223 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7224 } 7225 7226 } 7227 7228 /* 7229 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7230 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7231 * tcp_g_t_info_ack. The current state of the stream is copied from 7232 * tcp_state. 7233 */ 7234 static void 7235 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7236 { 7237 t_uscalar_t cap_bits1; 7238 struct T_capability_ack *tcap; 7239 7240 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7241 freemsg(mp); 7242 return; 7243 } 7244 7245 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7246 7247 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7248 mp->b_datap->db_type, T_CAPABILITY_ACK); 7249 if (mp == NULL) 7250 return; 7251 7252 tcap = (struct T_capability_ack *)mp->b_rptr; 7253 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7254 7255 putnext(tcp->tcp_rq, mp); 7256 } 7257 7258 /* 7259 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7260 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7261 * The current state of the stream is copied from tcp_state. 7262 */ 7263 static void 7264 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7265 { 7266 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7267 T_INFO_ACK); 7268 if (!mp) { 7269 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7270 return; 7271 } 7272 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7273 putnext(tcp->tcp_rq, mp); 7274 } 7275 7276 /* Respond to the TPI addr request */ 7277 static void 7278 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7279 { 7280 sin_t *sin; 7281 mblk_t *ackmp; 7282 struct T_addr_ack *taa; 7283 7284 /* Make it large enough for worst case */ 7285 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7286 2 * sizeof (sin6_t), 1); 7287 if (ackmp == NULL) { 7288 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7289 return; 7290 } 7291 7292 if (tcp->tcp_ipversion == IPV6_VERSION) { 7293 tcp_addr_req_ipv6(tcp, ackmp); 7294 return; 7295 } 7296 taa = (struct T_addr_ack *)ackmp->b_rptr; 7297 7298 bzero(taa, sizeof (struct T_addr_ack)); 7299 ackmp->b_wptr = (uchar_t *)&taa[1]; 7300 7301 taa->PRIM_type = T_ADDR_ACK; 7302 ackmp->b_datap->db_type = M_PCPROTO; 7303 7304 /* 7305 * Note: Following code assumes 32 bit alignment of basic 7306 * data structures like sin_t and struct T_addr_ack. 7307 */ 7308 if (tcp->tcp_state >= TCPS_BOUND) { 7309 /* 7310 * Fill in local address 7311 */ 7312 taa->LOCADDR_length = sizeof (sin_t); 7313 taa->LOCADDR_offset = sizeof (*taa); 7314 7315 sin = (sin_t *)&taa[1]; 7316 7317 /* Fill zeroes and then intialize non-zero fields */ 7318 *sin = sin_null; 7319 7320 sin->sin_family = AF_INET; 7321 7322 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7323 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7324 7325 ackmp->b_wptr = (uchar_t *)&sin[1]; 7326 7327 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7328 /* 7329 * Fill in Remote address 7330 */ 7331 taa->REMADDR_length = sizeof (sin_t); 7332 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7333 taa->LOCADDR_length); 7334 7335 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7336 *sin = sin_null; 7337 sin->sin_family = AF_INET; 7338 sin->sin_addr.s_addr = tcp->tcp_remote; 7339 sin->sin_port = tcp->tcp_fport; 7340 7341 ackmp->b_wptr = (uchar_t *)&sin[1]; 7342 } 7343 } 7344 putnext(tcp->tcp_rq, ackmp); 7345 } 7346 7347 /* Assumes that tcp_addr_req gets enough space and alignment */ 7348 static void 7349 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7350 { 7351 sin6_t *sin6; 7352 struct T_addr_ack *taa; 7353 7354 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7355 ASSERT(OK_32PTR(ackmp->b_rptr)); 7356 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7357 2 * sizeof (sin6_t)); 7358 7359 taa = (struct T_addr_ack *)ackmp->b_rptr; 7360 7361 bzero(taa, sizeof (struct T_addr_ack)); 7362 ackmp->b_wptr = (uchar_t *)&taa[1]; 7363 7364 taa->PRIM_type = T_ADDR_ACK; 7365 ackmp->b_datap->db_type = M_PCPROTO; 7366 7367 /* 7368 * Note: Following code assumes 32 bit alignment of basic 7369 * data structures like sin6_t and struct T_addr_ack. 7370 */ 7371 if (tcp->tcp_state >= TCPS_BOUND) { 7372 /* 7373 * Fill in local address 7374 */ 7375 taa->LOCADDR_length = sizeof (sin6_t); 7376 taa->LOCADDR_offset = sizeof (*taa); 7377 7378 sin6 = (sin6_t *)&taa[1]; 7379 *sin6 = sin6_null; 7380 7381 sin6->sin6_family = AF_INET6; 7382 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7383 sin6->sin6_port = tcp->tcp_lport; 7384 7385 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7386 7387 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7388 /* 7389 * Fill in Remote address 7390 */ 7391 taa->REMADDR_length = sizeof (sin6_t); 7392 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7393 taa->LOCADDR_length); 7394 7395 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7396 *sin6 = sin6_null; 7397 sin6->sin6_family = AF_INET6; 7398 sin6->sin6_flowinfo = 7399 tcp->tcp_ip6h->ip6_vcf & 7400 ~IPV6_VERS_AND_FLOW_MASK; 7401 sin6->sin6_addr = tcp->tcp_remote_v6; 7402 sin6->sin6_port = tcp->tcp_fport; 7403 7404 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7405 } 7406 } 7407 putnext(tcp->tcp_rq, ackmp); 7408 } 7409 7410 /* 7411 * Handle reinitialization of a tcp structure. 7412 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7413 */ 7414 static void 7415 tcp_reinit(tcp_t *tcp) 7416 { 7417 mblk_t *mp; 7418 int err; 7419 tcp_stack_t *tcps = tcp->tcp_tcps; 7420 7421 TCP_STAT(tcps, tcp_reinit_calls); 7422 7423 /* tcp_reinit should never be called for detached tcp_t's */ 7424 ASSERT(tcp->tcp_listener == NULL); 7425 ASSERT((tcp->tcp_family == AF_INET && 7426 tcp->tcp_ipversion == IPV4_VERSION) || 7427 (tcp->tcp_family == AF_INET6 && 7428 (tcp->tcp_ipversion == IPV4_VERSION || 7429 tcp->tcp_ipversion == IPV6_VERSION))); 7430 7431 /* Cancel outstanding timers */ 7432 tcp_timers_stop(tcp); 7433 7434 /* 7435 * Reset everything in the state vector, after updating global 7436 * MIB data from instance counters. 7437 */ 7438 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7439 tcp->tcp_ibsegs = 0; 7440 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7441 tcp->tcp_obsegs = 0; 7442 7443 tcp_close_mpp(&tcp->tcp_xmit_head); 7444 if (tcp->tcp_snd_zcopy_aware) 7445 tcp_zcopy_notify(tcp); 7446 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7447 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7448 mutex_enter(&tcp->tcp_non_sq_lock); 7449 if (tcp->tcp_flow_stopped && 7450 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7451 tcp_clrqfull(tcp); 7452 } 7453 mutex_exit(&tcp->tcp_non_sq_lock); 7454 tcp_close_mpp(&tcp->tcp_reass_head); 7455 tcp->tcp_reass_tail = NULL; 7456 if (tcp->tcp_rcv_list != NULL) { 7457 /* Free b_next chain */ 7458 tcp_close_mpp(&tcp->tcp_rcv_list); 7459 tcp->tcp_rcv_last_head = NULL; 7460 tcp->tcp_rcv_last_tail = NULL; 7461 tcp->tcp_rcv_cnt = 0; 7462 } 7463 tcp->tcp_rcv_last_tail = NULL; 7464 7465 if ((mp = tcp->tcp_urp_mp) != NULL) { 7466 freemsg(mp); 7467 tcp->tcp_urp_mp = NULL; 7468 } 7469 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7470 freemsg(mp); 7471 tcp->tcp_urp_mark_mp = NULL; 7472 } 7473 if (tcp->tcp_fused_sigurg_mp != NULL) { 7474 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7475 freeb(tcp->tcp_fused_sigurg_mp); 7476 tcp->tcp_fused_sigurg_mp = NULL; 7477 } 7478 if (tcp->tcp_ordrel_mp != NULL) { 7479 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7480 freeb(tcp->tcp_ordrel_mp); 7481 tcp->tcp_ordrel_mp = NULL; 7482 } 7483 7484 /* 7485 * Following is a union with two members which are 7486 * identical types and size so the following cleanup 7487 * is enough. 7488 */ 7489 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7490 7491 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7492 7493 /* 7494 * The connection can't be on the tcp_time_wait_head list 7495 * since it is not detached. 7496 */ 7497 ASSERT(tcp->tcp_time_wait_next == NULL); 7498 ASSERT(tcp->tcp_time_wait_prev == NULL); 7499 ASSERT(tcp->tcp_time_wait_expire == 0); 7500 7501 if (tcp->tcp_kssl_pending) { 7502 tcp->tcp_kssl_pending = B_FALSE; 7503 7504 /* Don't reset if the initialized by bind. */ 7505 if (tcp->tcp_kssl_ent != NULL) { 7506 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7507 KSSL_NO_PROXY); 7508 } 7509 } 7510 if (tcp->tcp_kssl_ctx != NULL) { 7511 kssl_release_ctx(tcp->tcp_kssl_ctx); 7512 tcp->tcp_kssl_ctx = NULL; 7513 } 7514 7515 /* 7516 * Reset/preserve other values 7517 */ 7518 tcp_reinit_values(tcp); 7519 ipcl_hash_remove(tcp->tcp_connp); 7520 conn_delete_ire(tcp->tcp_connp, NULL); 7521 tcp_ipsec_cleanup(tcp); 7522 7523 if (tcp->tcp_conn_req_max != 0) { 7524 /* 7525 * This is the case when a TLI program uses the same 7526 * transport end point to accept a connection. This 7527 * makes the TCP both a listener and acceptor. When 7528 * this connection is closed, we need to set the state 7529 * back to TCPS_LISTEN. Make sure that the eager list 7530 * is reinitialized. 7531 * 7532 * Note that this stream is still bound to the four 7533 * tuples of the previous connection in IP. If a new 7534 * SYN with different foreign address comes in, IP will 7535 * not find it and will send it to the global queue. In 7536 * the global queue, TCP will do a tcp_lookup_listener() 7537 * to find this stream. This works because this stream 7538 * is only removed from connected hash. 7539 * 7540 */ 7541 tcp->tcp_state = TCPS_LISTEN; 7542 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7543 tcp->tcp_eager_next_drop_q0 = tcp; 7544 tcp->tcp_eager_prev_drop_q0 = tcp; 7545 tcp->tcp_connp->conn_recv = tcp_conn_request; 7546 if (tcp->tcp_family == AF_INET6) { 7547 ASSERT(tcp->tcp_connp->conn_af_isv6); 7548 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7549 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7550 } else { 7551 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7552 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7553 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7554 } 7555 } else { 7556 tcp->tcp_state = TCPS_BOUND; 7557 } 7558 7559 /* 7560 * Initialize to default values 7561 * Can't fail since enough header template space already allocated 7562 * at open(). 7563 */ 7564 err = tcp_init_values(tcp); 7565 ASSERT(err == 0); 7566 /* Restore state in tcp_tcph */ 7567 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7568 if (tcp->tcp_ipversion == IPV4_VERSION) 7569 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7570 else 7571 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7572 /* 7573 * Copy of the src addr. in tcp_t is needed in tcp_t 7574 * since the lookup funcs can only lookup on tcp_t 7575 */ 7576 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7577 7578 ASSERT(tcp->tcp_ptpbhn != NULL); 7579 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7580 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7581 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7582 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7583 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7584 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7585 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7586 } 7587 7588 /* 7589 * Force values to zero that need be zero. 7590 * Do not touch values asociated with the BOUND or LISTEN state 7591 * since the connection will end up in that state after the reinit. 7592 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7593 * structure! 7594 */ 7595 static void 7596 tcp_reinit_values(tcp) 7597 tcp_t *tcp; 7598 { 7599 tcp_stack_t *tcps = tcp->tcp_tcps; 7600 7601 #ifndef lint 7602 #define DONTCARE(x) 7603 #define PRESERVE(x) 7604 #else 7605 #define DONTCARE(x) ((x) = (x)) 7606 #define PRESERVE(x) ((x) = (x)) 7607 #endif /* lint */ 7608 7609 PRESERVE(tcp->tcp_bind_hash_port); 7610 PRESERVE(tcp->tcp_bind_hash); 7611 PRESERVE(tcp->tcp_ptpbhn); 7612 PRESERVE(tcp->tcp_acceptor_hash); 7613 PRESERVE(tcp->tcp_ptpahn); 7614 7615 /* Should be ASSERT NULL on these with new code! */ 7616 ASSERT(tcp->tcp_time_wait_next == NULL); 7617 ASSERT(tcp->tcp_time_wait_prev == NULL); 7618 ASSERT(tcp->tcp_time_wait_expire == 0); 7619 PRESERVE(tcp->tcp_state); 7620 PRESERVE(tcp->tcp_rq); 7621 PRESERVE(tcp->tcp_wq); 7622 7623 ASSERT(tcp->tcp_xmit_head == NULL); 7624 ASSERT(tcp->tcp_xmit_last == NULL); 7625 ASSERT(tcp->tcp_unsent == 0); 7626 ASSERT(tcp->tcp_xmit_tail == NULL); 7627 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7628 7629 tcp->tcp_snxt = 0; /* Displayed in mib */ 7630 tcp->tcp_suna = 0; /* Displayed in mib */ 7631 tcp->tcp_swnd = 0; 7632 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7633 7634 ASSERT(tcp->tcp_ibsegs == 0); 7635 ASSERT(tcp->tcp_obsegs == 0); 7636 7637 if (tcp->tcp_iphc != NULL) { 7638 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7639 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7640 } 7641 7642 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7643 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7644 DONTCARE(tcp->tcp_ipha); 7645 DONTCARE(tcp->tcp_ip6h); 7646 DONTCARE(tcp->tcp_ip_hdr_len); 7647 DONTCARE(tcp->tcp_tcph); 7648 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7649 tcp->tcp_valid_bits = 0; 7650 7651 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7652 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7653 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7654 tcp->tcp_last_rcv_lbolt = 0; 7655 7656 tcp->tcp_init_cwnd = 0; 7657 7658 tcp->tcp_urp_last_valid = 0; 7659 tcp->tcp_hard_binding = 0; 7660 tcp->tcp_hard_bound = 0; 7661 PRESERVE(tcp->tcp_cred); 7662 PRESERVE(tcp->tcp_cpid); 7663 PRESERVE(tcp->tcp_open_time); 7664 PRESERVE(tcp->tcp_exclbind); 7665 7666 tcp->tcp_fin_acked = 0; 7667 tcp->tcp_fin_rcvd = 0; 7668 tcp->tcp_fin_sent = 0; 7669 tcp->tcp_ordrel_done = 0; 7670 7671 tcp->tcp_debug = 0; 7672 tcp->tcp_dontroute = 0; 7673 tcp->tcp_broadcast = 0; 7674 7675 tcp->tcp_useloopback = 0; 7676 tcp->tcp_reuseaddr = 0; 7677 tcp->tcp_oobinline = 0; 7678 tcp->tcp_dgram_errind = 0; 7679 7680 tcp->tcp_detached = 0; 7681 tcp->tcp_bind_pending = 0; 7682 tcp->tcp_unbind_pending = 0; 7683 7684 tcp->tcp_snd_ws_ok = B_FALSE; 7685 tcp->tcp_snd_ts_ok = B_FALSE; 7686 tcp->tcp_linger = 0; 7687 tcp->tcp_ka_enabled = 0; 7688 tcp->tcp_zero_win_probe = 0; 7689 7690 tcp->tcp_loopback = 0; 7691 tcp->tcp_refuse = 0; 7692 tcp->tcp_localnet = 0; 7693 tcp->tcp_syn_defense = 0; 7694 tcp->tcp_set_timer = 0; 7695 7696 tcp->tcp_active_open = 0; 7697 tcp->tcp_rexmit = B_FALSE; 7698 tcp->tcp_xmit_zc_clean = B_FALSE; 7699 7700 tcp->tcp_snd_sack_ok = B_FALSE; 7701 PRESERVE(tcp->tcp_recvdstaddr); 7702 tcp->tcp_hwcksum = B_FALSE; 7703 7704 tcp->tcp_ire_ill_check_done = B_FALSE; 7705 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7706 7707 tcp->tcp_mdt = B_FALSE; 7708 tcp->tcp_mdt_hdr_head = 0; 7709 tcp->tcp_mdt_hdr_tail = 0; 7710 7711 tcp->tcp_conn_def_q0 = 0; 7712 tcp->tcp_ip_forward_progress = B_FALSE; 7713 tcp->tcp_anon_priv_bind = 0; 7714 tcp->tcp_ecn_ok = B_FALSE; 7715 7716 tcp->tcp_cwr = B_FALSE; 7717 tcp->tcp_ecn_echo_on = B_FALSE; 7718 7719 if (tcp->tcp_sack_info != NULL) { 7720 if (tcp->tcp_notsack_list != NULL) { 7721 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7722 } 7723 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7724 tcp->tcp_sack_info = NULL; 7725 } 7726 7727 tcp->tcp_rcv_ws = 0; 7728 tcp->tcp_snd_ws = 0; 7729 tcp->tcp_ts_recent = 0; 7730 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7731 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7732 tcp->tcp_if_mtu = 0; 7733 7734 ASSERT(tcp->tcp_reass_head == NULL); 7735 ASSERT(tcp->tcp_reass_tail == NULL); 7736 7737 tcp->tcp_cwnd_cnt = 0; 7738 7739 ASSERT(tcp->tcp_rcv_list == NULL); 7740 ASSERT(tcp->tcp_rcv_last_head == NULL); 7741 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7742 ASSERT(tcp->tcp_rcv_cnt == 0); 7743 7744 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7745 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7746 tcp->tcp_csuna = 0; 7747 7748 tcp->tcp_rto = 0; /* Displayed in MIB */ 7749 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7750 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7751 tcp->tcp_rtt_update = 0; 7752 7753 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7754 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7755 7756 tcp->tcp_rack = 0; /* Displayed in mib */ 7757 tcp->tcp_rack_cnt = 0; 7758 tcp->tcp_rack_cur_max = 0; 7759 tcp->tcp_rack_abs_max = 0; 7760 7761 tcp->tcp_max_swnd = 0; 7762 7763 ASSERT(tcp->tcp_listener == NULL); 7764 7765 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7766 7767 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7768 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7769 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7770 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7771 7772 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7773 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7774 PRESERVE(tcp->tcp_conn_req_max); 7775 PRESERVE(tcp->tcp_conn_req_seqnum); 7776 7777 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7778 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7779 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7780 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7781 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7782 7783 tcp->tcp_lingertime = 0; 7784 7785 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7786 ASSERT(tcp->tcp_urp_mp == NULL); 7787 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7788 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7789 7790 ASSERT(tcp->tcp_eager_next_q == NULL); 7791 ASSERT(tcp->tcp_eager_last_q == NULL); 7792 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7793 tcp->tcp_eager_prev_q0 == NULL) || 7794 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7795 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7796 7797 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7798 tcp->tcp_eager_prev_drop_q0 == NULL) || 7799 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7800 7801 tcp->tcp_client_errno = 0; 7802 7803 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7804 7805 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7806 7807 PRESERVE(tcp->tcp_bound_source_v6); 7808 tcp->tcp_last_sent_len = 0; 7809 tcp->tcp_dupack_cnt = 0; 7810 7811 tcp->tcp_fport = 0; /* Displayed in MIB */ 7812 PRESERVE(tcp->tcp_lport); 7813 7814 PRESERVE(tcp->tcp_acceptor_lockp); 7815 7816 ASSERT(tcp->tcp_ordrel_mp == NULL); 7817 PRESERVE(tcp->tcp_acceptor_id); 7818 DONTCARE(tcp->tcp_ipsec_overhead); 7819 7820 PRESERVE(tcp->tcp_family); 7821 if (tcp->tcp_family == AF_INET6) { 7822 tcp->tcp_ipversion = IPV6_VERSION; 7823 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7824 } else { 7825 tcp->tcp_ipversion = IPV4_VERSION; 7826 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7827 } 7828 7829 tcp->tcp_bound_if = 0; 7830 tcp->tcp_ipv6_recvancillary = 0; 7831 tcp->tcp_recvifindex = 0; 7832 tcp->tcp_recvhops = 0; 7833 tcp->tcp_closed = 0; 7834 tcp->tcp_cleandeathtag = 0; 7835 if (tcp->tcp_hopopts != NULL) { 7836 mi_free(tcp->tcp_hopopts); 7837 tcp->tcp_hopopts = NULL; 7838 tcp->tcp_hopoptslen = 0; 7839 } 7840 ASSERT(tcp->tcp_hopoptslen == 0); 7841 if (tcp->tcp_dstopts != NULL) { 7842 mi_free(tcp->tcp_dstopts); 7843 tcp->tcp_dstopts = NULL; 7844 tcp->tcp_dstoptslen = 0; 7845 } 7846 ASSERT(tcp->tcp_dstoptslen == 0); 7847 if (tcp->tcp_rtdstopts != NULL) { 7848 mi_free(tcp->tcp_rtdstopts); 7849 tcp->tcp_rtdstopts = NULL; 7850 tcp->tcp_rtdstoptslen = 0; 7851 } 7852 ASSERT(tcp->tcp_rtdstoptslen == 0); 7853 if (tcp->tcp_rthdr != NULL) { 7854 mi_free(tcp->tcp_rthdr); 7855 tcp->tcp_rthdr = NULL; 7856 tcp->tcp_rthdrlen = 0; 7857 } 7858 ASSERT(tcp->tcp_rthdrlen == 0); 7859 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7860 7861 /* Reset fusion-related fields */ 7862 tcp->tcp_fused = B_FALSE; 7863 tcp->tcp_unfusable = B_FALSE; 7864 tcp->tcp_fused_sigurg = B_FALSE; 7865 tcp->tcp_direct_sockfs = B_FALSE; 7866 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7867 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7868 tcp->tcp_loopback_peer = NULL; 7869 tcp->tcp_fuse_rcv_hiwater = 0; 7870 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7871 tcp->tcp_fuse_rcv_unread_cnt = 0; 7872 7873 tcp->tcp_lso = B_FALSE; 7874 7875 tcp->tcp_in_ack_unsent = 0; 7876 tcp->tcp_cork = B_FALSE; 7877 tcp->tcp_tconnind_started = B_FALSE; 7878 7879 PRESERVE(tcp->tcp_squeue_bytes); 7880 7881 ASSERT(tcp->tcp_kssl_ctx == NULL); 7882 ASSERT(!tcp->tcp_kssl_pending); 7883 PRESERVE(tcp->tcp_kssl_ent); 7884 7885 /* Sodirect */ 7886 tcp->tcp_sodirect = NULL; 7887 7888 tcp->tcp_closemp_used = B_FALSE; 7889 7890 PRESERVE(tcp->tcp_rsrv_mp); 7891 PRESERVE(tcp->tcp_rsrv_mp_lock); 7892 7893 #ifdef DEBUG 7894 DONTCARE(tcp->tcmp_stk[0]); 7895 #endif 7896 7897 PRESERVE(tcp->tcp_connid); 7898 7899 7900 #undef DONTCARE 7901 #undef PRESERVE 7902 } 7903 7904 /* 7905 * Allocate necessary resources and initialize state vector. 7906 * Guaranteed not to fail so that when an error is returned, 7907 * the caller doesn't need to do any additional cleanup. 7908 */ 7909 int 7910 tcp_init(tcp_t *tcp, queue_t *q) 7911 { 7912 int err; 7913 7914 tcp->tcp_rq = q; 7915 tcp->tcp_wq = WR(q); 7916 tcp->tcp_state = TCPS_IDLE; 7917 if ((err = tcp_init_values(tcp)) != 0) 7918 tcp_timers_stop(tcp); 7919 return (err); 7920 } 7921 7922 static int 7923 tcp_init_values(tcp_t *tcp) 7924 { 7925 int err; 7926 tcp_stack_t *tcps = tcp->tcp_tcps; 7927 7928 ASSERT((tcp->tcp_family == AF_INET && 7929 tcp->tcp_ipversion == IPV4_VERSION) || 7930 (tcp->tcp_family == AF_INET6 && 7931 (tcp->tcp_ipversion == IPV4_VERSION || 7932 tcp->tcp_ipversion == IPV6_VERSION))); 7933 7934 /* 7935 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7936 * will be close to tcp_rexmit_interval_initial. By doing this, we 7937 * allow the algorithm to adjust slowly to large fluctuations of RTT 7938 * during first few transmissions of a connection as seen in slow 7939 * links. 7940 */ 7941 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7942 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7943 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7944 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7945 tcps->tcps_conn_grace_period; 7946 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7947 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7948 tcp->tcp_timer_backoff = 0; 7949 tcp->tcp_ms_we_have_waited = 0; 7950 tcp->tcp_last_recv_time = lbolt; 7951 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7952 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7953 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7954 7955 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7956 7957 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7958 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7959 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7960 /* 7961 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7962 * passive open. 7963 */ 7964 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7965 7966 tcp->tcp_naglim = tcps->tcps_naglim_def; 7967 7968 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7969 7970 tcp->tcp_mdt_hdr_head = 0; 7971 tcp->tcp_mdt_hdr_tail = 0; 7972 7973 /* Reset fusion-related fields */ 7974 tcp->tcp_fused = B_FALSE; 7975 tcp->tcp_unfusable = B_FALSE; 7976 tcp->tcp_fused_sigurg = B_FALSE; 7977 tcp->tcp_direct_sockfs = B_FALSE; 7978 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7979 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7980 tcp->tcp_loopback_peer = NULL; 7981 tcp->tcp_fuse_rcv_hiwater = 0; 7982 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7983 tcp->tcp_fuse_rcv_unread_cnt = 0; 7984 7985 /* Sodirect */ 7986 tcp->tcp_sodirect = NULL; 7987 7988 /* Initialize the header template */ 7989 if (tcp->tcp_ipversion == IPV4_VERSION) { 7990 err = tcp_header_init_ipv4(tcp); 7991 } else { 7992 err = tcp_header_init_ipv6(tcp); 7993 } 7994 if (err) 7995 return (err); 7996 7997 /* 7998 * Init the window scale to the max so tcp_rwnd_set() won't pare 7999 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8000 */ 8001 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8002 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8003 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8004 8005 tcp->tcp_cork = B_FALSE; 8006 /* 8007 * Init the tcp_debug option. This value determines whether TCP 8008 * calls strlog() to print out debug messages. Doing this 8009 * initialization here means that this value is not inherited thru 8010 * tcp_reinit(). 8011 */ 8012 tcp->tcp_debug = tcps->tcps_dbg; 8013 8014 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8015 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8016 8017 return (0); 8018 } 8019 8020 /* 8021 * Initialize the IPv4 header. Loses any record of any IP options. 8022 */ 8023 static int 8024 tcp_header_init_ipv4(tcp_t *tcp) 8025 { 8026 tcph_t *tcph; 8027 uint32_t sum; 8028 conn_t *connp; 8029 tcp_stack_t *tcps = tcp->tcp_tcps; 8030 8031 /* 8032 * This is a simple initialization. If there's 8033 * already a template, it should never be too small, 8034 * so reuse it. Otherwise, allocate space for the new one. 8035 */ 8036 if (tcp->tcp_iphc == NULL) { 8037 ASSERT(tcp->tcp_iphc_len == 0); 8038 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8039 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8040 if (tcp->tcp_iphc == NULL) { 8041 tcp->tcp_iphc_len = 0; 8042 return (ENOMEM); 8043 } 8044 } 8045 8046 /* options are gone; may need a new label */ 8047 connp = tcp->tcp_connp; 8048 connp->conn_mlp_type = mlptSingle; 8049 connp->conn_ulp_labeled = !is_system_labeled(); 8050 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8051 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8052 tcp->tcp_ip6h = NULL; 8053 tcp->tcp_ipversion = IPV4_VERSION; 8054 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8055 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8056 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8057 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8058 tcp->tcp_ipha->ipha_version_and_hdr_length 8059 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8060 tcp->tcp_ipha->ipha_ident = 0; 8061 8062 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8063 tcp->tcp_tos = 0; 8064 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8065 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8066 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8067 8068 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8069 tcp->tcp_tcph = tcph; 8070 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8071 /* 8072 * IP wants our header length in the checksum field to 8073 * allow it to perform a single pseudo-header+checksum 8074 * calculation on behalf of TCP. 8075 * Include the adjustment for a source route once IP_OPTIONS is set. 8076 */ 8077 sum = sizeof (tcph_t) + tcp->tcp_sum; 8078 sum = (sum >> 16) + (sum & 0xFFFF); 8079 U16_TO_ABE16(sum, tcph->th_sum); 8080 return (0); 8081 } 8082 8083 /* 8084 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8085 */ 8086 static int 8087 tcp_header_init_ipv6(tcp_t *tcp) 8088 { 8089 tcph_t *tcph; 8090 uint32_t sum; 8091 conn_t *connp; 8092 tcp_stack_t *tcps = tcp->tcp_tcps; 8093 8094 /* 8095 * This is a simple initialization. If there's 8096 * already a template, it should never be too small, 8097 * so reuse it. Otherwise, allocate space for the new one. 8098 * Ensure that there is enough space to "downgrade" the tcp_t 8099 * to an IPv4 tcp_t. This requires having space for a full load 8100 * of IPv4 options, as well as a full load of TCP options 8101 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8102 * than a v6 header and a TCP header with a full load of TCP options 8103 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8104 * We want to avoid reallocation in the "downgraded" case when 8105 * processing outbound IPv4 options. 8106 */ 8107 if (tcp->tcp_iphc == NULL) { 8108 ASSERT(tcp->tcp_iphc_len == 0); 8109 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8110 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8111 if (tcp->tcp_iphc == NULL) { 8112 tcp->tcp_iphc_len = 0; 8113 return (ENOMEM); 8114 } 8115 } 8116 8117 /* options are gone; may need a new label */ 8118 connp = tcp->tcp_connp; 8119 connp->conn_mlp_type = mlptSingle; 8120 connp->conn_ulp_labeled = !is_system_labeled(); 8121 8122 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8123 tcp->tcp_ipversion = IPV6_VERSION; 8124 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8125 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8126 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8127 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8128 tcp->tcp_ipha = NULL; 8129 8130 /* Initialize the header template */ 8131 8132 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8133 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8134 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8135 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8136 8137 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8138 tcp->tcp_tcph = tcph; 8139 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8140 /* 8141 * IP wants our header length in the checksum field to 8142 * allow it to perform a single psuedo-header+checksum 8143 * calculation on behalf of TCP. 8144 * Include the adjustment for a source route when IPV6_RTHDR is set. 8145 */ 8146 sum = sizeof (tcph_t) + tcp->tcp_sum; 8147 sum = (sum >> 16) + (sum & 0xFFFF); 8148 U16_TO_ABE16(sum, tcph->th_sum); 8149 return (0); 8150 } 8151 8152 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8153 #define ICMP_MIN_TCP_HDR 8 8154 8155 /* 8156 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8157 * passed up by IP. The message is always received on the correct tcp_t. 8158 * Assumes that IP has pulled up everything up to and including the ICMP header. 8159 */ 8160 void 8161 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8162 { 8163 icmph_t *icmph; 8164 ipha_t *ipha; 8165 int iph_hdr_length; 8166 tcph_t *tcph; 8167 boolean_t ipsec_mctl = B_FALSE; 8168 boolean_t secure; 8169 mblk_t *first_mp = mp; 8170 int32_t new_mss; 8171 uint32_t ratio; 8172 size_t mp_size = MBLKL(mp); 8173 uint32_t seg_seq; 8174 tcp_stack_t *tcps = tcp->tcp_tcps; 8175 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8176 8177 /* Assume IP provides aligned packets - otherwise toss */ 8178 if (!OK_32PTR(mp->b_rptr)) { 8179 freemsg(mp); 8180 return; 8181 } 8182 8183 /* 8184 * Since ICMP errors are normal data marked with M_CTL when sent 8185 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8186 * packets starting with an ipsec_info_t, see ipsec_info.h. 8187 */ 8188 if ((mp_size == sizeof (ipsec_info_t)) && 8189 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8190 ASSERT(mp->b_cont != NULL); 8191 mp = mp->b_cont; 8192 /* IP should have done this */ 8193 ASSERT(OK_32PTR(mp->b_rptr)); 8194 mp_size = MBLKL(mp); 8195 ipsec_mctl = B_TRUE; 8196 } 8197 8198 /* 8199 * Verify that we have a complete outer IP header. If not, drop it. 8200 */ 8201 if (mp_size < sizeof (ipha_t)) { 8202 noticmpv4: 8203 freemsg(first_mp); 8204 return; 8205 } 8206 8207 ipha = (ipha_t *)mp->b_rptr; 8208 /* 8209 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8210 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8211 */ 8212 switch (IPH_HDR_VERSION(ipha)) { 8213 case IPV6_VERSION: 8214 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8215 return; 8216 case IPV4_VERSION: 8217 break; 8218 default: 8219 goto noticmpv4; 8220 } 8221 8222 /* Skip past the outer IP and ICMP headers */ 8223 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8224 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8225 /* 8226 * If we don't have the correct outer IP header length or if the ULP 8227 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8228 * send it upstream. 8229 */ 8230 if (iph_hdr_length < sizeof (ipha_t) || 8231 ipha->ipha_protocol != IPPROTO_ICMP || 8232 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8233 goto noticmpv4; 8234 } 8235 ipha = (ipha_t *)&icmph[1]; 8236 8237 /* Skip past the inner IP and find the ULP header */ 8238 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8239 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8240 /* 8241 * If we don't have the correct inner IP header length or if the ULP 8242 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8243 * bytes of TCP header, drop it. 8244 */ 8245 if (iph_hdr_length < sizeof (ipha_t) || 8246 ipha->ipha_protocol != IPPROTO_TCP || 8247 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8248 goto noticmpv4; 8249 } 8250 8251 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8252 if (ipsec_mctl) { 8253 secure = ipsec_in_is_secure(first_mp); 8254 } else { 8255 secure = B_FALSE; 8256 } 8257 if (secure) { 8258 /* 8259 * If we are willing to accept this in clear 8260 * we don't have to verify policy. 8261 */ 8262 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8263 if (!tcp_check_policy(tcp, first_mp, 8264 ipha, NULL, secure, ipsec_mctl)) { 8265 /* 8266 * tcp_check_policy called 8267 * ip_drop_packet() on failure. 8268 */ 8269 return; 8270 } 8271 } 8272 } 8273 } else if (ipsec_mctl) { 8274 /* 8275 * This is a hard_bound connection. IP has already 8276 * verified policy. We don't have to do it again. 8277 */ 8278 freeb(first_mp); 8279 first_mp = mp; 8280 ipsec_mctl = B_FALSE; 8281 } 8282 8283 seg_seq = ABE32_TO_U32(tcph->th_seq); 8284 /* 8285 * TCP SHOULD check that the TCP sequence number contained in 8286 * payload of the ICMP error message is within the range 8287 * SND.UNA <= SEG.SEQ < SND.NXT. 8288 */ 8289 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8290 /* 8291 * The ICMP message is bogus, just drop it. But if this is 8292 * an ICMP too big message, IP has already changed 8293 * the ire_max_frag to the bogus value. We need to change 8294 * it back. 8295 */ 8296 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8297 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8298 conn_t *connp = tcp->tcp_connp; 8299 ire_t *ire; 8300 int flag; 8301 8302 if (tcp->tcp_ipversion == IPV4_VERSION) { 8303 flag = tcp->tcp_ipha-> 8304 ipha_fragment_offset_and_flags; 8305 } else { 8306 flag = 0; 8307 } 8308 mutex_enter(&connp->conn_lock); 8309 if ((ire = connp->conn_ire_cache) != NULL) { 8310 mutex_enter(&ire->ire_lock); 8311 mutex_exit(&connp->conn_lock); 8312 ire->ire_max_frag = tcp->tcp_if_mtu; 8313 ire->ire_frag_flag |= flag; 8314 mutex_exit(&ire->ire_lock); 8315 } else { 8316 mutex_exit(&connp->conn_lock); 8317 } 8318 } 8319 goto noticmpv4; 8320 } 8321 8322 switch (icmph->icmph_type) { 8323 case ICMP_DEST_UNREACHABLE: 8324 switch (icmph->icmph_code) { 8325 case ICMP_FRAGMENTATION_NEEDED: 8326 /* 8327 * Reduce the MSS based on the new MTU. This will 8328 * eliminate any fragmentation locally. 8329 * N.B. There may well be some funny side-effects on 8330 * the local send policy and the remote receive policy. 8331 * Pending further research, we provide 8332 * tcp_ignore_path_mtu just in case this proves 8333 * disastrous somewhere. 8334 * 8335 * After updating the MSS, retransmit part of the 8336 * dropped segment using the new mss by calling 8337 * tcp_wput_data(). Need to adjust all those 8338 * params to make sure tcp_wput_data() work properly. 8339 */ 8340 if (tcps->tcps_ignore_path_mtu || 8341 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8342 break; 8343 8344 /* 8345 * Decrease the MSS by time stamp options 8346 * IP options and IPSEC options. tcp_hdr_len 8347 * includes time stamp option and IP option 8348 * length. Note that new_mss may be negative 8349 * if tcp_ipsec_overhead is large and the 8350 * icmph_du_mtu is the minimum value, which is 68. 8351 */ 8352 new_mss = ntohs(icmph->icmph_du_mtu) - 8353 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8354 8355 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8356 new_mss); 8357 8358 /* 8359 * Only update the MSS if the new one is 8360 * smaller than the previous one. This is 8361 * to avoid problems when getting multiple 8362 * ICMP errors for the same MTU. 8363 */ 8364 if (new_mss >= tcp->tcp_mss) 8365 break; 8366 8367 /* 8368 * Note that we are using the template header's DF 8369 * bit in the fast path sending. So we need to compare 8370 * the new mss with both tcps_mss_min and ip_pmtu_min. 8371 * And stop doing IPv4 PMTUd if new_mss is less than 8372 * MAX(tcps_mss_min, ip_pmtu_min). 8373 */ 8374 if (new_mss < tcps->tcps_mss_min || 8375 new_mss < ipst->ips_ip_pmtu_min) { 8376 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8377 0; 8378 } 8379 8380 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8381 ASSERT(ratio >= 1); 8382 tcp_mss_set(tcp, new_mss, B_TRUE); 8383 8384 /* 8385 * Make sure we have something to 8386 * send. 8387 */ 8388 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8389 (tcp->tcp_xmit_head != NULL)) { 8390 /* 8391 * Shrink tcp_cwnd in 8392 * proportion to the old MSS/new MSS. 8393 */ 8394 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8395 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8396 (tcp->tcp_unsent == 0)) { 8397 tcp->tcp_rexmit_max = tcp->tcp_fss; 8398 } else { 8399 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8400 } 8401 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8402 tcp->tcp_rexmit = B_TRUE; 8403 tcp->tcp_dupack_cnt = 0; 8404 tcp->tcp_snd_burst = TCP_CWND_SS; 8405 tcp_ss_rexmit(tcp); 8406 } 8407 break; 8408 case ICMP_PORT_UNREACHABLE: 8409 case ICMP_PROTOCOL_UNREACHABLE: 8410 switch (tcp->tcp_state) { 8411 case TCPS_SYN_SENT: 8412 case TCPS_SYN_RCVD: 8413 /* 8414 * ICMP can snipe away incipient 8415 * TCP connections as long as 8416 * seq number is same as initial 8417 * send seq number. 8418 */ 8419 if (seg_seq == tcp->tcp_iss) { 8420 (void) tcp_clean_death(tcp, 8421 ECONNREFUSED, 6); 8422 } 8423 break; 8424 } 8425 break; 8426 case ICMP_HOST_UNREACHABLE: 8427 case ICMP_NET_UNREACHABLE: 8428 /* Record the error in case we finally time out. */ 8429 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8430 tcp->tcp_client_errno = EHOSTUNREACH; 8431 else 8432 tcp->tcp_client_errno = ENETUNREACH; 8433 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8434 if (tcp->tcp_listener != NULL && 8435 tcp->tcp_listener->tcp_syn_defense) { 8436 /* 8437 * Ditch the half-open connection if we 8438 * suspect a SYN attack is under way. 8439 */ 8440 tcp_ip_ire_mark_advice(tcp); 8441 (void) tcp_clean_death(tcp, 8442 tcp->tcp_client_errno, 7); 8443 } 8444 } 8445 break; 8446 default: 8447 break; 8448 } 8449 break; 8450 case ICMP_SOURCE_QUENCH: { 8451 /* 8452 * use a global boolean to control 8453 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8454 * The default is false. 8455 */ 8456 if (tcp_icmp_source_quench) { 8457 /* 8458 * Reduce the sending rate as if we got a 8459 * retransmit timeout 8460 */ 8461 uint32_t npkt; 8462 8463 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8464 tcp->tcp_mss; 8465 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8466 tcp->tcp_cwnd = tcp->tcp_mss; 8467 tcp->tcp_cwnd_cnt = 0; 8468 } 8469 break; 8470 } 8471 } 8472 freemsg(first_mp); 8473 } 8474 8475 /* 8476 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8477 * error messages passed up by IP. 8478 * Assumes that IP has pulled up all the extension headers as well 8479 * as the ICMPv6 header. 8480 */ 8481 static void 8482 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8483 { 8484 icmp6_t *icmp6; 8485 ip6_t *ip6h; 8486 uint16_t iph_hdr_length; 8487 tcpha_t *tcpha; 8488 uint8_t *nexthdrp; 8489 uint32_t new_mss; 8490 uint32_t ratio; 8491 boolean_t secure; 8492 mblk_t *first_mp = mp; 8493 size_t mp_size; 8494 uint32_t seg_seq; 8495 tcp_stack_t *tcps = tcp->tcp_tcps; 8496 8497 /* 8498 * The caller has determined if this is an IPSEC_IN packet and 8499 * set ipsec_mctl appropriately (see tcp_icmp_error). 8500 */ 8501 if (ipsec_mctl) 8502 mp = mp->b_cont; 8503 8504 mp_size = MBLKL(mp); 8505 8506 /* 8507 * Verify that we have a complete IP header. If not, send it upstream. 8508 */ 8509 if (mp_size < sizeof (ip6_t)) { 8510 noticmpv6: 8511 freemsg(first_mp); 8512 return; 8513 } 8514 8515 /* 8516 * Verify this is an ICMPV6 packet, else send it upstream. 8517 */ 8518 ip6h = (ip6_t *)mp->b_rptr; 8519 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8520 iph_hdr_length = IPV6_HDR_LEN; 8521 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8522 &nexthdrp) || 8523 *nexthdrp != IPPROTO_ICMPV6) { 8524 goto noticmpv6; 8525 } 8526 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8527 ip6h = (ip6_t *)&icmp6[1]; 8528 /* 8529 * Verify if we have a complete ICMP and inner IP header. 8530 */ 8531 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8532 goto noticmpv6; 8533 8534 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8535 goto noticmpv6; 8536 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8537 /* 8538 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8539 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8540 * packet. 8541 */ 8542 if ((*nexthdrp != IPPROTO_TCP) || 8543 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8544 goto noticmpv6; 8545 } 8546 8547 /* 8548 * ICMP errors come on the right queue or come on 8549 * listener/global queue for detached connections and 8550 * get switched to the right queue. If it comes on the 8551 * right queue, policy check has already been done by IP 8552 * and thus free the first_mp without verifying the policy. 8553 * If it has come for a non-hard bound connection, we need 8554 * to verify policy as IP may not have done it. 8555 */ 8556 if (!tcp->tcp_hard_bound) { 8557 if (ipsec_mctl) { 8558 secure = ipsec_in_is_secure(first_mp); 8559 } else { 8560 secure = B_FALSE; 8561 } 8562 if (secure) { 8563 /* 8564 * If we are willing to accept this in clear 8565 * we don't have to verify policy. 8566 */ 8567 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8568 if (!tcp_check_policy(tcp, first_mp, 8569 NULL, ip6h, secure, ipsec_mctl)) { 8570 /* 8571 * tcp_check_policy called 8572 * ip_drop_packet() on failure. 8573 */ 8574 return; 8575 } 8576 } 8577 } 8578 } else if (ipsec_mctl) { 8579 /* 8580 * This is a hard_bound connection. IP has already 8581 * verified policy. We don't have to do it again. 8582 */ 8583 freeb(first_mp); 8584 first_mp = mp; 8585 ipsec_mctl = B_FALSE; 8586 } 8587 8588 seg_seq = ntohl(tcpha->tha_seq); 8589 /* 8590 * TCP SHOULD check that the TCP sequence number contained in 8591 * payload of the ICMP error message is within the range 8592 * SND.UNA <= SEG.SEQ < SND.NXT. 8593 */ 8594 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8595 /* 8596 * If the ICMP message is bogus, should we kill the 8597 * connection, or should we just drop the bogus ICMP 8598 * message? It would probably make more sense to just 8599 * drop the message so that if this one managed to get 8600 * in, the real connection should not suffer. 8601 */ 8602 goto noticmpv6; 8603 } 8604 8605 switch (icmp6->icmp6_type) { 8606 case ICMP6_PACKET_TOO_BIG: 8607 /* 8608 * Reduce the MSS based on the new MTU. This will 8609 * eliminate any fragmentation locally. 8610 * N.B. There may well be some funny side-effects on 8611 * the local send policy and the remote receive policy. 8612 * Pending further research, we provide 8613 * tcp_ignore_path_mtu just in case this proves 8614 * disastrous somewhere. 8615 * 8616 * After updating the MSS, retransmit part of the 8617 * dropped segment using the new mss by calling 8618 * tcp_wput_data(). Need to adjust all those 8619 * params to make sure tcp_wput_data() work properly. 8620 */ 8621 if (tcps->tcps_ignore_path_mtu) 8622 break; 8623 8624 /* 8625 * Decrease the MSS by time stamp options 8626 * IP options and IPSEC options. tcp_hdr_len 8627 * includes time stamp option and IP option 8628 * length. 8629 */ 8630 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8631 tcp->tcp_ipsec_overhead; 8632 8633 /* 8634 * Only update the MSS if the new one is 8635 * smaller than the previous one. This is 8636 * to avoid problems when getting multiple 8637 * ICMP errors for the same MTU. 8638 */ 8639 if (new_mss >= tcp->tcp_mss) 8640 break; 8641 8642 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8643 ASSERT(ratio >= 1); 8644 tcp_mss_set(tcp, new_mss, B_TRUE); 8645 8646 /* 8647 * Make sure we have something to 8648 * send. 8649 */ 8650 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8651 (tcp->tcp_xmit_head != NULL)) { 8652 /* 8653 * Shrink tcp_cwnd in 8654 * proportion to the old MSS/new MSS. 8655 */ 8656 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8657 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8658 (tcp->tcp_unsent == 0)) { 8659 tcp->tcp_rexmit_max = tcp->tcp_fss; 8660 } else { 8661 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8662 } 8663 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8664 tcp->tcp_rexmit = B_TRUE; 8665 tcp->tcp_dupack_cnt = 0; 8666 tcp->tcp_snd_burst = TCP_CWND_SS; 8667 tcp_ss_rexmit(tcp); 8668 } 8669 break; 8670 8671 case ICMP6_DST_UNREACH: 8672 switch (icmp6->icmp6_code) { 8673 case ICMP6_DST_UNREACH_NOPORT: 8674 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8675 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8676 (seg_seq == tcp->tcp_iss)) { 8677 (void) tcp_clean_death(tcp, 8678 ECONNREFUSED, 8); 8679 } 8680 break; 8681 8682 case ICMP6_DST_UNREACH_ADMIN: 8683 case ICMP6_DST_UNREACH_NOROUTE: 8684 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8685 case ICMP6_DST_UNREACH_ADDR: 8686 /* Record the error in case we finally time out. */ 8687 tcp->tcp_client_errno = EHOSTUNREACH; 8688 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8689 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8690 (seg_seq == tcp->tcp_iss)) { 8691 if (tcp->tcp_listener != NULL && 8692 tcp->tcp_listener->tcp_syn_defense) { 8693 /* 8694 * Ditch the half-open connection if we 8695 * suspect a SYN attack is under way. 8696 */ 8697 tcp_ip_ire_mark_advice(tcp); 8698 (void) tcp_clean_death(tcp, 8699 tcp->tcp_client_errno, 9); 8700 } 8701 } 8702 8703 8704 break; 8705 default: 8706 break; 8707 } 8708 break; 8709 8710 case ICMP6_PARAM_PROB: 8711 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8712 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8713 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8714 (uchar_t *)nexthdrp) { 8715 if (tcp->tcp_state == TCPS_SYN_SENT || 8716 tcp->tcp_state == TCPS_SYN_RCVD) { 8717 (void) tcp_clean_death(tcp, 8718 ECONNREFUSED, 10); 8719 } 8720 break; 8721 } 8722 break; 8723 8724 case ICMP6_TIME_EXCEEDED: 8725 default: 8726 break; 8727 } 8728 freemsg(first_mp); 8729 } 8730 8731 /* 8732 * Notify IP that we are having trouble with this connection. IP should 8733 * blow the IRE away and start over. 8734 */ 8735 static void 8736 tcp_ip_notify(tcp_t *tcp) 8737 { 8738 struct iocblk *iocp; 8739 ipid_t *ipid; 8740 mblk_t *mp; 8741 8742 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8743 if (tcp->tcp_ipversion == IPV6_VERSION) 8744 return; 8745 8746 mp = mkiocb(IP_IOCTL); 8747 if (mp == NULL) 8748 return; 8749 8750 iocp = (struct iocblk *)mp->b_rptr; 8751 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8752 8753 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8754 if (!mp->b_cont) { 8755 freeb(mp); 8756 return; 8757 } 8758 8759 ipid = (ipid_t *)mp->b_cont->b_rptr; 8760 mp->b_cont->b_wptr += iocp->ioc_count; 8761 bzero(ipid, sizeof (*ipid)); 8762 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8763 ipid->ipid_ire_type = IRE_CACHE; 8764 ipid->ipid_addr_offset = sizeof (ipid_t); 8765 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8766 /* 8767 * Note: in the case of source routing we want to blow away the 8768 * route to the first source route hop. 8769 */ 8770 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8771 sizeof (tcp->tcp_ipha->ipha_dst)); 8772 8773 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8774 } 8775 8776 /* Unlink and return any mblk that looks like it contains an ire */ 8777 static mblk_t * 8778 tcp_ire_mp(mblk_t **mpp) 8779 { 8780 mblk_t *mp = *mpp; 8781 mblk_t *prev_mp = NULL; 8782 8783 for (;;) { 8784 switch (DB_TYPE(mp)) { 8785 case IRE_DB_TYPE: 8786 case IRE_DB_REQ_TYPE: 8787 if (mp == *mpp) { 8788 *mpp = mp->b_cont; 8789 } else { 8790 prev_mp->b_cont = mp->b_cont; 8791 } 8792 mp->b_cont = NULL; 8793 return (mp); 8794 default: 8795 break; 8796 } 8797 prev_mp = mp; 8798 mp = mp->b_cont; 8799 if (mp == NULL) 8800 break; 8801 } 8802 return (mp); 8803 } 8804 8805 /* 8806 * Timer callback routine for keepalive probe. We do a fake resend of 8807 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8808 * check to see if we have heard anything from the other end for the last 8809 * RTO period. If we have, set the timer to expire for another 8810 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8811 * RTO << 1 and check again when it expires. Keep exponentially increasing 8812 * the timeout if we have not heard from the other side. If for more than 8813 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8814 * kill the connection unless the keepalive abort threshold is 0. In 8815 * that case, we will probe "forever." 8816 */ 8817 static void 8818 tcp_keepalive_killer(void *arg) 8819 { 8820 mblk_t *mp; 8821 conn_t *connp = (conn_t *)arg; 8822 tcp_t *tcp = connp->conn_tcp; 8823 int32_t firetime; 8824 int32_t idletime; 8825 int32_t ka_intrvl; 8826 tcp_stack_t *tcps = tcp->tcp_tcps; 8827 8828 tcp->tcp_ka_tid = 0; 8829 8830 if (tcp->tcp_fused) 8831 return; 8832 8833 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8834 ka_intrvl = tcp->tcp_ka_interval; 8835 8836 /* 8837 * Keepalive probe should only be sent if the application has not 8838 * done a close on the connection. 8839 */ 8840 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8841 return; 8842 } 8843 /* Timer fired too early, restart it. */ 8844 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8845 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8846 MSEC_TO_TICK(ka_intrvl)); 8847 return; 8848 } 8849 8850 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8851 /* 8852 * If we have not heard from the other side for a long 8853 * time, kill the connection unless the keepalive abort 8854 * threshold is 0. In that case, we will probe "forever." 8855 */ 8856 if (tcp->tcp_ka_abort_thres != 0 && 8857 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8858 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8859 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8860 tcp->tcp_client_errno : ETIMEDOUT, 11); 8861 return; 8862 } 8863 8864 if (tcp->tcp_snxt == tcp->tcp_suna && 8865 idletime >= ka_intrvl) { 8866 /* Fake resend of last ACKed byte. */ 8867 mblk_t *mp1 = allocb(1, BPRI_LO); 8868 8869 if (mp1 != NULL) { 8870 *mp1->b_wptr++ = '\0'; 8871 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8872 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8873 freeb(mp1); 8874 /* 8875 * if allocation failed, fall through to start the 8876 * timer back. 8877 */ 8878 if (mp != NULL) { 8879 tcp_send_data(tcp, tcp->tcp_wq, mp); 8880 BUMP_MIB(&tcps->tcps_mib, 8881 tcpTimKeepaliveProbe); 8882 if (tcp->tcp_ka_last_intrvl != 0) { 8883 int max; 8884 /* 8885 * We should probe again at least 8886 * in ka_intrvl, but not more than 8887 * tcp_rexmit_interval_max. 8888 */ 8889 max = tcps->tcps_rexmit_interval_max; 8890 firetime = MIN(ka_intrvl - 1, 8891 tcp->tcp_ka_last_intrvl << 1); 8892 if (firetime > max) 8893 firetime = max; 8894 } else { 8895 firetime = tcp->tcp_rto; 8896 } 8897 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8898 tcp_keepalive_killer, 8899 MSEC_TO_TICK(firetime)); 8900 tcp->tcp_ka_last_intrvl = firetime; 8901 return; 8902 } 8903 } 8904 } else { 8905 tcp->tcp_ka_last_intrvl = 0; 8906 } 8907 8908 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8909 if ((firetime = ka_intrvl - idletime) < 0) { 8910 firetime = ka_intrvl; 8911 } 8912 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8913 MSEC_TO_TICK(firetime)); 8914 } 8915 8916 int 8917 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8918 { 8919 queue_t *q = tcp->tcp_rq; 8920 int32_t mss = tcp->tcp_mss; 8921 int maxpsz; 8922 conn_t *connp = tcp->tcp_connp; 8923 8924 if (TCP_IS_DETACHED(tcp)) 8925 return (mss); 8926 if (tcp->tcp_fused) { 8927 maxpsz = tcp_fuse_maxpsz_set(tcp); 8928 mss = INFPSZ; 8929 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8930 /* 8931 * Set the sd_qn_maxpsz according to the socket send buffer 8932 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8933 * instruct the stream head to copyin user data into contiguous 8934 * kernel-allocated buffers without breaking it up into smaller 8935 * chunks. We round up the buffer size to the nearest SMSS. 8936 */ 8937 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8938 if (tcp->tcp_kssl_ctx == NULL) 8939 mss = INFPSZ; 8940 else 8941 mss = SSL3_MAX_RECORD_LEN; 8942 } else { 8943 /* 8944 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8945 * (and a multiple of the mss). This instructs the stream 8946 * head to break down larger than SMSS writes into SMSS- 8947 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8948 */ 8949 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8950 maxpsz = tcp->tcp_maxpsz * mss; 8951 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8952 maxpsz = tcp->tcp_xmit_hiwater/2; 8953 /* Round up to nearest mss */ 8954 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8955 } 8956 } 8957 8958 (void) proto_set_maxpsz(q, connp, maxpsz); 8959 if (!(IPCL_IS_NONSTR(connp))) { 8960 /* XXX do it in set_maxpsz()? */ 8961 tcp->tcp_wq->q_maxpsz = maxpsz; 8962 } 8963 8964 if (set_maxblk) 8965 (void) proto_set_tx_maxblk(q, connp, mss); 8966 return (mss); 8967 } 8968 8969 /* 8970 * Extract option values from a tcp header. We put any found values into the 8971 * tcpopt struct and return a bitmask saying which options were found. 8972 */ 8973 static int 8974 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8975 { 8976 uchar_t *endp; 8977 int len; 8978 uint32_t mss; 8979 uchar_t *up = (uchar_t *)tcph; 8980 int found = 0; 8981 int32_t sack_len; 8982 tcp_seq sack_begin, sack_end; 8983 tcp_t *tcp; 8984 8985 endp = up + TCP_HDR_LENGTH(tcph); 8986 up += TCP_MIN_HEADER_LENGTH; 8987 while (up < endp) { 8988 len = endp - up; 8989 switch (*up) { 8990 case TCPOPT_EOL: 8991 break; 8992 8993 case TCPOPT_NOP: 8994 up++; 8995 continue; 8996 8997 case TCPOPT_MAXSEG: 8998 if (len < TCPOPT_MAXSEG_LEN || 8999 up[1] != TCPOPT_MAXSEG_LEN) 9000 break; 9001 9002 mss = BE16_TO_U16(up+2); 9003 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9004 tcpopt->tcp_opt_mss = mss; 9005 found |= TCP_OPT_MSS_PRESENT; 9006 9007 up += TCPOPT_MAXSEG_LEN; 9008 continue; 9009 9010 case TCPOPT_WSCALE: 9011 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9012 break; 9013 9014 if (up[2] > TCP_MAX_WINSHIFT) 9015 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9016 else 9017 tcpopt->tcp_opt_wscale = up[2]; 9018 found |= TCP_OPT_WSCALE_PRESENT; 9019 9020 up += TCPOPT_WS_LEN; 9021 continue; 9022 9023 case TCPOPT_SACK_PERMITTED: 9024 if (len < TCPOPT_SACK_OK_LEN || 9025 up[1] != TCPOPT_SACK_OK_LEN) 9026 break; 9027 found |= TCP_OPT_SACK_OK_PRESENT; 9028 up += TCPOPT_SACK_OK_LEN; 9029 continue; 9030 9031 case TCPOPT_SACK: 9032 if (len <= 2 || up[1] <= 2 || len < up[1]) 9033 break; 9034 9035 /* If TCP is not interested in SACK blks... */ 9036 if ((tcp = tcpopt->tcp) == NULL) { 9037 up += up[1]; 9038 continue; 9039 } 9040 sack_len = up[1] - TCPOPT_HEADER_LEN; 9041 up += TCPOPT_HEADER_LEN; 9042 9043 /* 9044 * If the list is empty, allocate one and assume 9045 * nothing is sack'ed. 9046 */ 9047 ASSERT(tcp->tcp_sack_info != NULL); 9048 if (tcp->tcp_notsack_list == NULL) { 9049 tcp_notsack_update(&(tcp->tcp_notsack_list), 9050 tcp->tcp_suna, tcp->tcp_snxt, 9051 &(tcp->tcp_num_notsack_blk), 9052 &(tcp->tcp_cnt_notsack_list)); 9053 9054 /* 9055 * Make sure tcp_notsack_list is not NULL. 9056 * This happens when kmem_alloc(KM_NOSLEEP) 9057 * returns NULL. 9058 */ 9059 if (tcp->tcp_notsack_list == NULL) { 9060 up += sack_len; 9061 continue; 9062 } 9063 tcp->tcp_fack = tcp->tcp_suna; 9064 } 9065 9066 while (sack_len > 0) { 9067 if (up + 8 > endp) { 9068 up = endp; 9069 break; 9070 } 9071 sack_begin = BE32_TO_U32(up); 9072 up += 4; 9073 sack_end = BE32_TO_U32(up); 9074 up += 4; 9075 sack_len -= 8; 9076 /* 9077 * Bounds checking. Make sure the SACK 9078 * info is within tcp_suna and tcp_snxt. 9079 * If this SACK blk is out of bound, ignore 9080 * it but continue to parse the following 9081 * blks. 9082 */ 9083 if (SEQ_LEQ(sack_end, sack_begin) || 9084 SEQ_LT(sack_begin, tcp->tcp_suna) || 9085 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9086 continue; 9087 } 9088 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9089 sack_begin, sack_end, 9090 &(tcp->tcp_num_notsack_blk), 9091 &(tcp->tcp_cnt_notsack_list)); 9092 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9093 tcp->tcp_fack = sack_end; 9094 } 9095 } 9096 found |= TCP_OPT_SACK_PRESENT; 9097 continue; 9098 9099 case TCPOPT_TSTAMP: 9100 if (len < TCPOPT_TSTAMP_LEN || 9101 up[1] != TCPOPT_TSTAMP_LEN) 9102 break; 9103 9104 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9105 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9106 9107 found |= TCP_OPT_TSTAMP_PRESENT; 9108 9109 up += TCPOPT_TSTAMP_LEN; 9110 continue; 9111 9112 default: 9113 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9114 break; 9115 up += up[1]; 9116 continue; 9117 } 9118 break; 9119 } 9120 return (found); 9121 } 9122 9123 /* 9124 * Set the mss associated with a particular tcp based on its current value, 9125 * and a new one passed in. Observe minimums and maximums, and reset 9126 * other state variables that we want to view as multiples of mss. 9127 * 9128 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9129 * highwater marks etc. need to be initialized or adjusted. 9130 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9131 * packet arrives. 9132 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9133 * ICMP6_PACKET_TOO_BIG arrives. 9134 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9135 * to increase the MSS to use the extra bytes available. 9136 * 9137 * Callers except tcp_paws_check() ensure that they only reduce mss. 9138 */ 9139 static void 9140 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9141 { 9142 uint32_t mss_max; 9143 tcp_stack_t *tcps = tcp->tcp_tcps; 9144 9145 if (tcp->tcp_ipversion == IPV4_VERSION) 9146 mss_max = tcps->tcps_mss_max_ipv4; 9147 else 9148 mss_max = tcps->tcps_mss_max_ipv6; 9149 9150 if (mss < tcps->tcps_mss_min) 9151 mss = tcps->tcps_mss_min; 9152 if (mss > mss_max) 9153 mss = mss_max; 9154 /* 9155 * Unless naglim has been set by our client to 9156 * a non-mss value, force naglim to track mss. 9157 * This can help to aggregate small writes. 9158 */ 9159 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9160 tcp->tcp_naglim = mss; 9161 /* 9162 * TCP should be able to buffer at least 4 MSS data for obvious 9163 * performance reason. 9164 */ 9165 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9166 tcp->tcp_xmit_hiwater = mss << 2; 9167 9168 if (do_ss) { 9169 /* 9170 * Either the tcp_cwnd is as yet uninitialized, or mss is 9171 * changing due to a reduction in MTU, presumably as a 9172 * result of a new path component, reset cwnd to its 9173 * "initial" value, as a multiple of the new mss. 9174 */ 9175 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9176 } else { 9177 /* 9178 * Called by tcp_paws_check(), the mss increased 9179 * marginally to allow use of space previously taken 9180 * by the timestamp option. It would be inappropriate 9181 * to apply slow start or tcp_init_cwnd values to 9182 * tcp_cwnd, simply adjust to a multiple of the new mss. 9183 */ 9184 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9185 tcp->tcp_cwnd_cnt = 0; 9186 } 9187 tcp->tcp_mss = mss; 9188 (void) tcp_maxpsz_set(tcp, B_TRUE); 9189 } 9190 9191 /* For /dev/tcp aka AF_INET open */ 9192 static int 9193 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9194 { 9195 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9196 } 9197 9198 /* For /dev/tcp6 aka AF_INET6 open */ 9199 static int 9200 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9201 { 9202 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9203 } 9204 9205 static conn_t * 9206 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9207 boolean_t issocket, int *errorp) 9208 { 9209 tcp_t *tcp = NULL; 9210 conn_t *connp; 9211 int err; 9212 zoneid_t zoneid; 9213 tcp_stack_t *tcps; 9214 squeue_t *sqp; 9215 9216 ASSERT(errorp != NULL); 9217 /* 9218 * Find the proper zoneid and netstack. 9219 */ 9220 /* 9221 * Special case for install: miniroot needs to be able to 9222 * access files via NFS as though it were always in the 9223 * global zone. 9224 */ 9225 if (credp == kcred && nfs_global_client_only != 0) { 9226 zoneid = GLOBAL_ZONEID; 9227 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9228 netstack_tcp; 9229 ASSERT(tcps != NULL); 9230 } else { 9231 netstack_t *ns; 9232 9233 ns = netstack_find_by_cred(credp); 9234 ASSERT(ns != NULL); 9235 tcps = ns->netstack_tcp; 9236 ASSERT(tcps != NULL); 9237 9238 /* 9239 * For exclusive stacks we set the zoneid to zero 9240 * to make TCP operate as if in the global zone. 9241 */ 9242 if (tcps->tcps_netstack->netstack_stackid != 9243 GLOBAL_NETSTACKID) 9244 zoneid = GLOBAL_ZONEID; 9245 else 9246 zoneid = crgetzoneid(credp); 9247 } 9248 /* 9249 * For stackid zero this is done from strplumb.c, but 9250 * non-zero stackids are handled here. 9251 */ 9252 if (tcps->tcps_g_q == NULL && 9253 tcps->tcps_netstack->netstack_stackid != 9254 GLOBAL_NETSTACKID) { 9255 tcp_g_q_setup(tcps); 9256 } 9257 9258 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9259 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9260 /* 9261 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9262 * so we drop it by one. 9263 */ 9264 netstack_rele(tcps->tcps_netstack); 9265 if (connp == NULL) { 9266 *errorp = ENOSR; 9267 return (NULL); 9268 } 9269 connp->conn_sqp = sqp; 9270 connp->conn_initial_sqp = connp->conn_sqp; 9271 tcp = connp->conn_tcp; 9272 9273 if (isv6) { 9274 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9275 connp->conn_send = ip_output_v6; 9276 connp->conn_af_isv6 = B_TRUE; 9277 connp->conn_pkt_isv6 = B_TRUE; 9278 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9279 tcp->tcp_ipversion = IPV6_VERSION; 9280 tcp->tcp_family = AF_INET6; 9281 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9282 } else { 9283 connp->conn_flags |= IPCL_TCP4; 9284 connp->conn_send = ip_output; 9285 connp->conn_af_isv6 = B_FALSE; 9286 connp->conn_pkt_isv6 = B_FALSE; 9287 tcp->tcp_ipversion = IPV4_VERSION; 9288 tcp->tcp_family = AF_INET; 9289 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9290 } 9291 9292 /* 9293 * TCP keeps a copy of cred for cache locality reasons but 9294 * we put a reference only once. If connp->conn_cred 9295 * becomes invalid, tcp_cred should also be set to NULL. 9296 */ 9297 tcp->tcp_cred = connp->conn_cred = credp; 9298 crhold(connp->conn_cred); 9299 tcp->tcp_cpid = curproc->p_pid; 9300 tcp->tcp_open_time = lbolt64; 9301 connp->conn_zoneid = zoneid; 9302 connp->conn_mlp_type = mlptSingle; 9303 connp->conn_ulp_labeled = !is_system_labeled(); 9304 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9305 ASSERT(tcp->tcp_tcps == tcps); 9306 9307 /* 9308 * If the caller has the process-wide flag set, then default to MAC 9309 * exempt mode. This allows read-down to unlabeled hosts. 9310 */ 9311 if (getpflags(NET_MAC_AWARE, credp) != 0) 9312 connp->conn_mac_exempt = B_TRUE; 9313 9314 connp->conn_dev = NULL; 9315 if (issocket) { 9316 connp->conn_flags |= IPCL_SOCKET; 9317 tcp->tcp_issocket = 1; 9318 } 9319 9320 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9321 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9322 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9323 9324 /* Non-zero default values */ 9325 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9326 9327 if (q == NULL) { 9328 /* 9329 * Create a helper stream for non-STREAMS socket. 9330 */ 9331 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9332 if (err != 0) { 9333 ip1dbg(("tcp_create: create of IP helper stream " 9334 "failed\n")); 9335 CONN_DEC_REF(connp); 9336 *errorp = err; 9337 return (NULL); 9338 } 9339 q = connp->conn_rq; 9340 } else { 9341 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9342 } 9343 9344 SOCK_CONNID_INIT(tcp->tcp_connid); 9345 err = tcp_init(tcp, q); 9346 if (err != 0) { 9347 CONN_DEC_REF(connp); 9348 *errorp = err; 9349 return (NULL); 9350 } 9351 9352 return (connp); 9353 } 9354 9355 static int 9356 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9357 boolean_t isv6) 9358 { 9359 tcp_t *tcp = NULL; 9360 conn_t *connp = NULL; 9361 int err; 9362 vmem_t *minor_arena = NULL; 9363 dev_t conn_dev; 9364 boolean_t issocket; 9365 9366 if (q->q_ptr != NULL) 9367 return (0); 9368 9369 if (sflag == MODOPEN) 9370 return (EINVAL); 9371 9372 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9373 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9374 minor_arena = ip_minor_arena_la; 9375 } else { 9376 /* 9377 * Either minor numbers in the large arena were exhausted 9378 * or a non socket application is doing the open. 9379 * Try to allocate from the small arena. 9380 */ 9381 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9382 return (EBUSY); 9383 } 9384 minor_arena = ip_minor_arena_sa; 9385 } 9386 9387 ASSERT(minor_arena != NULL); 9388 9389 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9390 9391 if (flag & SO_FALLBACK) { 9392 /* 9393 * Non streams socket needs a stream to fallback to 9394 */ 9395 RD(q)->q_ptr = (void *)conn_dev; 9396 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9397 WR(q)->q_ptr = (void *)minor_arena; 9398 qprocson(q); 9399 return (0); 9400 } else if (flag & SO_ACCEPTOR) { 9401 q->q_qinfo = &tcp_acceptor_rinit; 9402 /* 9403 * the conn_dev and minor_arena will be subsequently used by 9404 * tcp_wput_accept() and tcpclose_accept() to figure out the 9405 * minor device number for this connection from the q_ptr. 9406 */ 9407 RD(q)->q_ptr = (void *)conn_dev; 9408 WR(q)->q_qinfo = &tcp_acceptor_winit; 9409 WR(q)->q_ptr = (void *)minor_arena; 9410 qprocson(q); 9411 return (0); 9412 } 9413 9414 issocket = flag & SO_SOCKSTR; 9415 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9416 9417 if (connp == NULL) { 9418 inet_minor_free(minor_arena, conn_dev); 9419 q->q_ptr = WR(q)->q_ptr = NULL; 9420 return (err); 9421 } 9422 9423 q->q_ptr = WR(q)->q_ptr = connp; 9424 9425 connp->conn_dev = conn_dev; 9426 connp->conn_minor_arena = minor_arena; 9427 9428 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9429 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9430 9431 if (issocket) { 9432 WR(q)->q_qinfo = &tcp_sock_winit; 9433 } else { 9434 tcp = connp->conn_tcp; 9435 #ifdef _ILP32 9436 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9437 #else 9438 tcp->tcp_acceptor_id = conn_dev; 9439 #endif /* _ILP32 */ 9440 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9441 } 9442 9443 /* 9444 * Put the ref for TCP. Ref for IP was already put 9445 * by ipcl_conn_create. Also Make the conn_t globally 9446 * visible to walkers 9447 */ 9448 mutex_enter(&connp->conn_lock); 9449 CONN_INC_REF_LOCKED(connp); 9450 ASSERT(connp->conn_ref == 2); 9451 connp->conn_state_flags &= ~CONN_INCIPIENT; 9452 mutex_exit(&connp->conn_lock); 9453 9454 qprocson(q); 9455 return (0); 9456 } 9457 9458 /* 9459 * Some TCP options can be "set" by requesting them in the option 9460 * buffer. This is needed for XTI feature test though we do not 9461 * allow it in general. We interpret that this mechanism is more 9462 * applicable to OSI protocols and need not be allowed in general. 9463 * This routine filters out options for which it is not allowed (most) 9464 * and lets through those (few) for which it is. [ The XTI interface 9465 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9466 * ever implemented will have to be allowed here ]. 9467 */ 9468 static boolean_t 9469 tcp_allow_connopt_set(int level, int name) 9470 { 9471 9472 switch (level) { 9473 case IPPROTO_TCP: 9474 switch (name) { 9475 case TCP_NODELAY: 9476 return (B_TRUE); 9477 default: 9478 return (B_FALSE); 9479 } 9480 /*NOTREACHED*/ 9481 default: 9482 return (B_FALSE); 9483 } 9484 /*NOTREACHED*/ 9485 } 9486 9487 /* 9488 * this routine gets default values of certain options whose default 9489 * values are maintained by protocol specific code 9490 */ 9491 /* ARGSUSED */ 9492 int 9493 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9494 { 9495 int32_t *i1 = (int32_t *)ptr; 9496 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9497 9498 switch (level) { 9499 case IPPROTO_TCP: 9500 switch (name) { 9501 case TCP_NOTIFY_THRESHOLD: 9502 *i1 = tcps->tcps_ip_notify_interval; 9503 break; 9504 case TCP_ABORT_THRESHOLD: 9505 *i1 = tcps->tcps_ip_abort_interval; 9506 break; 9507 case TCP_CONN_NOTIFY_THRESHOLD: 9508 *i1 = tcps->tcps_ip_notify_cinterval; 9509 break; 9510 case TCP_CONN_ABORT_THRESHOLD: 9511 *i1 = tcps->tcps_ip_abort_cinterval; 9512 break; 9513 default: 9514 return (-1); 9515 } 9516 break; 9517 case IPPROTO_IP: 9518 switch (name) { 9519 case IP_TTL: 9520 *i1 = tcps->tcps_ipv4_ttl; 9521 break; 9522 default: 9523 return (-1); 9524 } 9525 break; 9526 case IPPROTO_IPV6: 9527 switch (name) { 9528 case IPV6_UNICAST_HOPS: 9529 *i1 = tcps->tcps_ipv6_hoplimit; 9530 break; 9531 default: 9532 return (-1); 9533 } 9534 break; 9535 default: 9536 return (-1); 9537 } 9538 return (sizeof (int)); 9539 } 9540 9541 static int 9542 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9543 { 9544 int *i1 = (int *)ptr; 9545 tcp_t *tcp = connp->conn_tcp; 9546 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9547 9548 switch (level) { 9549 case SOL_SOCKET: 9550 switch (name) { 9551 case SO_LINGER: { 9552 struct linger *lgr = (struct linger *)ptr; 9553 9554 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9555 lgr->l_linger = tcp->tcp_lingertime; 9556 } 9557 return (sizeof (struct linger)); 9558 case SO_DEBUG: 9559 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9560 break; 9561 case SO_KEEPALIVE: 9562 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9563 break; 9564 case SO_DONTROUTE: 9565 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9566 break; 9567 case SO_USELOOPBACK: 9568 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9569 break; 9570 case SO_BROADCAST: 9571 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9572 break; 9573 case SO_REUSEADDR: 9574 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9575 break; 9576 case SO_OOBINLINE: 9577 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9578 break; 9579 case SO_DGRAM_ERRIND: 9580 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9581 break; 9582 case SO_TYPE: 9583 *i1 = SOCK_STREAM; 9584 break; 9585 case SO_SNDBUF: 9586 *i1 = tcp->tcp_xmit_hiwater; 9587 break; 9588 case SO_RCVBUF: 9589 *i1 = tcp->tcp_recv_hiwater; 9590 break; 9591 case SO_SND_COPYAVOID: 9592 *i1 = tcp->tcp_snd_zcopy_on ? 9593 SO_SND_COPYAVOID : 0; 9594 break; 9595 case SO_ALLZONES: 9596 *i1 = connp->conn_allzones ? 1 : 0; 9597 break; 9598 case SO_ANON_MLP: 9599 *i1 = connp->conn_anon_mlp; 9600 break; 9601 case SO_MAC_EXEMPT: 9602 *i1 = connp->conn_mac_exempt; 9603 break; 9604 case SO_EXCLBIND: 9605 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9606 break; 9607 case SO_PROTOTYPE: 9608 *i1 = IPPROTO_TCP; 9609 break; 9610 case SO_DOMAIN: 9611 *i1 = tcp->tcp_family; 9612 break; 9613 case SO_ACCEPTCONN: 9614 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9615 default: 9616 return (-1); 9617 } 9618 break; 9619 case IPPROTO_TCP: 9620 switch (name) { 9621 case TCP_NODELAY: 9622 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9623 break; 9624 case TCP_MAXSEG: 9625 *i1 = tcp->tcp_mss; 9626 break; 9627 case TCP_NOTIFY_THRESHOLD: 9628 *i1 = (int)tcp->tcp_first_timer_threshold; 9629 break; 9630 case TCP_ABORT_THRESHOLD: 9631 *i1 = tcp->tcp_second_timer_threshold; 9632 break; 9633 case TCP_CONN_NOTIFY_THRESHOLD: 9634 *i1 = tcp->tcp_first_ctimer_threshold; 9635 break; 9636 case TCP_CONN_ABORT_THRESHOLD: 9637 *i1 = tcp->tcp_second_ctimer_threshold; 9638 break; 9639 case TCP_RECVDSTADDR: 9640 *i1 = tcp->tcp_recvdstaddr; 9641 break; 9642 case TCP_ANONPRIVBIND: 9643 *i1 = tcp->tcp_anon_priv_bind; 9644 break; 9645 case TCP_EXCLBIND: 9646 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9647 break; 9648 case TCP_INIT_CWND: 9649 *i1 = tcp->tcp_init_cwnd; 9650 break; 9651 case TCP_KEEPALIVE_THRESHOLD: 9652 *i1 = tcp->tcp_ka_interval; 9653 break; 9654 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9655 *i1 = tcp->tcp_ka_abort_thres; 9656 break; 9657 case TCP_CORK: 9658 *i1 = tcp->tcp_cork; 9659 break; 9660 default: 9661 return (-1); 9662 } 9663 break; 9664 case IPPROTO_IP: 9665 if (tcp->tcp_family != AF_INET) 9666 return (-1); 9667 switch (name) { 9668 case IP_OPTIONS: 9669 case T_IP_OPTIONS: { 9670 /* 9671 * This is compatible with BSD in that in only return 9672 * the reverse source route with the final destination 9673 * as the last entry. The first 4 bytes of the option 9674 * will contain the final destination. 9675 */ 9676 int opt_len; 9677 9678 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9679 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9680 ASSERT(opt_len >= 0); 9681 /* Caller ensures enough space */ 9682 if (opt_len > 0) { 9683 /* 9684 * TODO: Do we have to handle getsockopt on an 9685 * initiator as well? 9686 */ 9687 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9688 } 9689 return (0); 9690 } 9691 case IP_TOS: 9692 case T_IP_TOS: 9693 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9694 break; 9695 case IP_TTL: 9696 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9697 break; 9698 case IP_NEXTHOP: 9699 /* Handled at IP level */ 9700 return (-EINVAL); 9701 default: 9702 return (-1); 9703 } 9704 break; 9705 case IPPROTO_IPV6: 9706 /* 9707 * IPPROTO_IPV6 options are only supported for sockets 9708 * that are using IPv6 on the wire. 9709 */ 9710 if (tcp->tcp_ipversion != IPV6_VERSION) { 9711 return (-1); 9712 } 9713 switch (name) { 9714 case IPV6_UNICAST_HOPS: 9715 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9716 break; /* goto sizeof (int) option return */ 9717 case IPV6_BOUND_IF: 9718 /* Zero if not set */ 9719 *i1 = tcp->tcp_bound_if; 9720 break; /* goto sizeof (int) option return */ 9721 case IPV6_RECVPKTINFO: 9722 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9723 *i1 = 1; 9724 else 9725 *i1 = 0; 9726 break; /* goto sizeof (int) option return */ 9727 case IPV6_RECVTCLASS: 9728 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9729 *i1 = 1; 9730 else 9731 *i1 = 0; 9732 break; /* goto sizeof (int) option return */ 9733 case IPV6_RECVHOPLIMIT: 9734 if (tcp->tcp_ipv6_recvancillary & 9735 TCP_IPV6_RECVHOPLIMIT) 9736 *i1 = 1; 9737 else 9738 *i1 = 0; 9739 break; /* goto sizeof (int) option return */ 9740 case IPV6_RECVHOPOPTS: 9741 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9742 *i1 = 1; 9743 else 9744 *i1 = 0; 9745 break; /* goto sizeof (int) option return */ 9746 case IPV6_RECVDSTOPTS: 9747 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9748 *i1 = 1; 9749 else 9750 *i1 = 0; 9751 break; /* goto sizeof (int) option return */ 9752 case _OLD_IPV6_RECVDSTOPTS: 9753 if (tcp->tcp_ipv6_recvancillary & 9754 TCP_OLD_IPV6_RECVDSTOPTS) 9755 *i1 = 1; 9756 else 9757 *i1 = 0; 9758 break; /* goto sizeof (int) option return */ 9759 case IPV6_RECVRTHDR: 9760 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9761 *i1 = 1; 9762 else 9763 *i1 = 0; 9764 break; /* goto sizeof (int) option return */ 9765 case IPV6_RECVRTHDRDSTOPTS: 9766 if (tcp->tcp_ipv6_recvancillary & 9767 TCP_IPV6_RECVRTDSTOPTS) 9768 *i1 = 1; 9769 else 9770 *i1 = 0; 9771 break; /* goto sizeof (int) option return */ 9772 case IPV6_PKTINFO: { 9773 /* XXX assumes that caller has room for max size! */ 9774 struct in6_pktinfo *pkti; 9775 9776 pkti = (struct in6_pktinfo *)ptr; 9777 if (ipp->ipp_fields & IPPF_IFINDEX) 9778 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9779 else 9780 pkti->ipi6_ifindex = 0; 9781 if (ipp->ipp_fields & IPPF_ADDR) 9782 pkti->ipi6_addr = ipp->ipp_addr; 9783 else 9784 pkti->ipi6_addr = ipv6_all_zeros; 9785 return (sizeof (struct in6_pktinfo)); 9786 } 9787 case IPV6_TCLASS: 9788 if (ipp->ipp_fields & IPPF_TCLASS) 9789 *i1 = ipp->ipp_tclass; 9790 else 9791 *i1 = IPV6_FLOW_TCLASS( 9792 IPV6_DEFAULT_VERS_AND_FLOW); 9793 break; /* goto sizeof (int) option return */ 9794 case IPV6_NEXTHOP: { 9795 sin6_t *sin6 = (sin6_t *)ptr; 9796 9797 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9798 return (0); 9799 *sin6 = sin6_null; 9800 sin6->sin6_family = AF_INET6; 9801 sin6->sin6_addr = ipp->ipp_nexthop; 9802 return (sizeof (sin6_t)); 9803 } 9804 case IPV6_HOPOPTS: 9805 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9806 return (0); 9807 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9808 return (0); 9809 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9810 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9811 if (tcp->tcp_label_len > 0) { 9812 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9813 ptr[1] = (ipp->ipp_hopoptslen - 9814 tcp->tcp_label_len + 7) / 8 - 1; 9815 } 9816 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9817 case IPV6_RTHDRDSTOPTS: 9818 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9819 return (0); 9820 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9821 return (ipp->ipp_rtdstoptslen); 9822 case IPV6_RTHDR: 9823 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9824 return (0); 9825 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9826 return (ipp->ipp_rthdrlen); 9827 case IPV6_DSTOPTS: 9828 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9829 return (0); 9830 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9831 return (ipp->ipp_dstoptslen); 9832 case IPV6_SRC_PREFERENCES: 9833 return (ip6_get_src_preferences(connp, 9834 (uint32_t *)ptr)); 9835 case IPV6_PATHMTU: { 9836 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9837 9838 if (tcp->tcp_state < TCPS_ESTABLISHED) 9839 return (-1); 9840 9841 return (ip_fill_mtuinfo(&connp->conn_remv6, 9842 connp->conn_fport, mtuinfo, 9843 connp->conn_netstack)); 9844 } 9845 default: 9846 return (-1); 9847 } 9848 break; 9849 default: 9850 return (-1); 9851 } 9852 return (sizeof (int)); 9853 } 9854 9855 /* 9856 * TCP routine to get the values of options. 9857 */ 9858 int 9859 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9860 { 9861 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9862 } 9863 9864 /* returns UNIX error, the optlen is a value-result arg */ 9865 int 9866 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9867 void *optvalp, socklen_t *optlen, cred_t *cr) 9868 { 9869 conn_t *connp = (conn_t *)proto_handle; 9870 squeue_t *sqp = connp->conn_sqp; 9871 int error; 9872 t_uscalar_t max_optbuf_len; 9873 void *optvalp_buf; 9874 int len; 9875 9876 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9877 tcp_opt_obj.odb_opt_des_arr, 9878 tcp_opt_obj.odb_opt_arr_cnt, 9879 tcp_opt_obj.odb_topmost_tpiprovider, 9880 B_FALSE, B_TRUE, cr); 9881 if (error != 0) { 9882 if (error < 0) { 9883 error = proto_tlitosyserr(-error); 9884 } 9885 return (error); 9886 } 9887 9888 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9889 9890 error = squeue_synch_enter(sqp, connp, 0); 9891 if (error == ENOMEM) { 9892 return (ENOMEM); 9893 } 9894 9895 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9896 squeue_synch_exit(sqp, connp); 9897 9898 if (len < 0) { 9899 /* 9900 * Pass on to IP 9901 */ 9902 kmem_free(optvalp_buf, max_optbuf_len); 9903 return (ip_get_options(connp, level, option_name, 9904 optvalp, optlen, cr)); 9905 } else { 9906 /* 9907 * update optlen and copy option value 9908 */ 9909 t_uscalar_t size = MIN(len, *optlen); 9910 bcopy(optvalp_buf, optvalp, size); 9911 bcopy(&size, optlen, sizeof (size)); 9912 9913 kmem_free(optvalp_buf, max_optbuf_len); 9914 return (0); 9915 } 9916 } 9917 9918 /* 9919 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9920 * Parameters are assumed to be verified by the caller. 9921 */ 9922 /* ARGSUSED */ 9923 int 9924 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9925 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9926 void *thisdg_attrs, cred_t *cr) 9927 { 9928 tcp_t *tcp = connp->conn_tcp; 9929 int *i1 = (int *)invalp; 9930 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9931 boolean_t checkonly; 9932 int reterr; 9933 tcp_stack_t *tcps = tcp->tcp_tcps; 9934 9935 switch (optset_context) { 9936 case SETFN_OPTCOM_CHECKONLY: 9937 checkonly = B_TRUE; 9938 /* 9939 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9940 * inlen != 0 implies value supplied and 9941 * we have to "pretend" to set it. 9942 * inlen == 0 implies that there is no 9943 * value part in T_CHECK request and just validation 9944 * done elsewhere should be enough, we just return here. 9945 */ 9946 if (inlen == 0) { 9947 *outlenp = 0; 9948 return (0); 9949 } 9950 break; 9951 case SETFN_OPTCOM_NEGOTIATE: 9952 checkonly = B_FALSE; 9953 break; 9954 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9955 case SETFN_CONN_NEGOTIATE: 9956 checkonly = B_FALSE; 9957 /* 9958 * Negotiating local and "association-related" options 9959 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9960 * primitives is allowed by XTI, but we choose 9961 * to not implement this style negotiation for Internet 9962 * protocols (We interpret it is a must for OSI world but 9963 * optional for Internet protocols) for all options. 9964 * [ Will do only for the few options that enable test 9965 * suites that our XTI implementation of this feature 9966 * works for transports that do allow it ] 9967 */ 9968 if (!tcp_allow_connopt_set(level, name)) { 9969 *outlenp = 0; 9970 return (EINVAL); 9971 } 9972 break; 9973 default: 9974 /* 9975 * We should never get here 9976 */ 9977 *outlenp = 0; 9978 return (EINVAL); 9979 } 9980 9981 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9982 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9983 9984 /* 9985 * For TCP, we should have no ancillary data sent down 9986 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9987 * has to be zero. 9988 */ 9989 ASSERT(thisdg_attrs == NULL); 9990 9991 /* 9992 * For fixed length options, no sanity check 9993 * of passed in length is done. It is assumed *_optcom_req() 9994 * routines do the right thing. 9995 */ 9996 switch (level) { 9997 case SOL_SOCKET: 9998 switch (name) { 9999 case SO_LINGER: { 10000 struct linger *lgr = (struct linger *)invalp; 10001 10002 if (!checkonly) { 10003 if (lgr->l_onoff) { 10004 tcp->tcp_linger = 1; 10005 tcp->tcp_lingertime = lgr->l_linger; 10006 } else { 10007 tcp->tcp_linger = 0; 10008 tcp->tcp_lingertime = 0; 10009 } 10010 /* struct copy */ 10011 *(struct linger *)outvalp = *lgr; 10012 } else { 10013 if (!lgr->l_onoff) { 10014 ((struct linger *) 10015 outvalp)->l_onoff = 0; 10016 ((struct linger *) 10017 outvalp)->l_linger = 0; 10018 } else { 10019 /* struct copy */ 10020 *(struct linger *)outvalp = *lgr; 10021 } 10022 } 10023 *outlenp = sizeof (struct linger); 10024 return (0); 10025 } 10026 case SO_DEBUG: 10027 if (!checkonly) 10028 tcp->tcp_debug = onoff; 10029 break; 10030 case SO_KEEPALIVE: 10031 if (checkonly) { 10032 /* check only case */ 10033 break; 10034 } 10035 10036 if (!onoff) { 10037 if (tcp->tcp_ka_enabled) { 10038 if (tcp->tcp_ka_tid != 0) { 10039 (void) TCP_TIMER_CANCEL(tcp, 10040 tcp->tcp_ka_tid); 10041 tcp->tcp_ka_tid = 0; 10042 } 10043 tcp->tcp_ka_enabled = 0; 10044 } 10045 break; 10046 } 10047 if (!tcp->tcp_ka_enabled) { 10048 /* Crank up the keepalive timer */ 10049 tcp->tcp_ka_last_intrvl = 0; 10050 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10051 tcp_keepalive_killer, 10052 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10053 tcp->tcp_ka_enabled = 1; 10054 } 10055 break; 10056 case SO_DONTROUTE: 10057 /* 10058 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10059 * only of interest to IP. We track them here only so 10060 * that we can report their current value. 10061 */ 10062 if (!checkonly) { 10063 tcp->tcp_dontroute = onoff; 10064 tcp->tcp_connp->conn_dontroute = onoff; 10065 } 10066 break; 10067 case SO_USELOOPBACK: 10068 if (!checkonly) { 10069 tcp->tcp_useloopback = onoff; 10070 tcp->tcp_connp->conn_loopback = onoff; 10071 } 10072 break; 10073 case SO_BROADCAST: 10074 if (!checkonly) { 10075 tcp->tcp_broadcast = onoff; 10076 tcp->tcp_connp->conn_broadcast = onoff; 10077 } 10078 break; 10079 case SO_REUSEADDR: 10080 if (!checkonly) { 10081 tcp->tcp_reuseaddr = onoff; 10082 tcp->tcp_connp->conn_reuseaddr = onoff; 10083 } 10084 break; 10085 case SO_OOBINLINE: 10086 if (!checkonly) { 10087 tcp->tcp_oobinline = onoff; 10088 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10089 proto_set_rx_oob_opt(connp, onoff); 10090 } 10091 break; 10092 case SO_DGRAM_ERRIND: 10093 if (!checkonly) 10094 tcp->tcp_dgram_errind = onoff; 10095 break; 10096 case SO_SNDBUF: { 10097 if (*i1 > tcps->tcps_max_buf) { 10098 *outlenp = 0; 10099 return (ENOBUFS); 10100 } 10101 if (checkonly) 10102 break; 10103 10104 tcp->tcp_xmit_hiwater = *i1; 10105 if (tcps->tcps_snd_lowat_fraction != 0) 10106 tcp->tcp_xmit_lowater = 10107 tcp->tcp_xmit_hiwater / 10108 tcps->tcps_snd_lowat_fraction; 10109 (void) tcp_maxpsz_set(tcp, B_TRUE); 10110 /* 10111 * If we are flow-controlled, recheck the condition. 10112 * There are apps that increase SO_SNDBUF size when 10113 * flow-controlled (EWOULDBLOCK), and expect the flow 10114 * control condition to be lifted right away. 10115 */ 10116 mutex_enter(&tcp->tcp_non_sq_lock); 10117 if (tcp->tcp_flow_stopped && 10118 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10119 tcp_clrqfull(tcp); 10120 } 10121 mutex_exit(&tcp->tcp_non_sq_lock); 10122 break; 10123 } 10124 case SO_RCVBUF: 10125 if (*i1 > tcps->tcps_max_buf) { 10126 *outlenp = 0; 10127 return (ENOBUFS); 10128 } 10129 /* Silently ignore zero */ 10130 if (!checkonly && *i1 != 0) { 10131 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10132 (void) tcp_rwnd_set(tcp, *i1); 10133 } 10134 /* 10135 * XXX should we return the rwnd here 10136 * and tcp_opt_get ? 10137 */ 10138 break; 10139 case SO_SND_COPYAVOID: 10140 if (!checkonly) { 10141 /* we only allow enable at most once for now */ 10142 if (tcp->tcp_loopback || 10143 (tcp->tcp_kssl_ctx != NULL) || 10144 (!tcp->tcp_snd_zcopy_aware && 10145 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10146 *outlenp = 0; 10147 return (EOPNOTSUPP); 10148 } 10149 tcp->tcp_snd_zcopy_aware = 1; 10150 } 10151 break; 10152 case SO_ALLZONES: 10153 /* Pass option along to IP level for handling */ 10154 return (-EINVAL); 10155 case SO_ANON_MLP: 10156 /* Pass option along to IP level for handling */ 10157 return (-EINVAL); 10158 case SO_MAC_EXEMPT: 10159 /* Pass option along to IP level for handling */ 10160 return (-EINVAL); 10161 case SO_EXCLBIND: 10162 if (!checkonly) 10163 tcp->tcp_exclbind = onoff; 10164 break; 10165 default: 10166 *outlenp = 0; 10167 return (EINVAL); 10168 } 10169 break; 10170 case IPPROTO_TCP: 10171 switch (name) { 10172 case TCP_NODELAY: 10173 if (!checkonly) 10174 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10175 break; 10176 case TCP_NOTIFY_THRESHOLD: 10177 if (!checkonly) 10178 tcp->tcp_first_timer_threshold = *i1; 10179 break; 10180 case TCP_ABORT_THRESHOLD: 10181 if (!checkonly) 10182 tcp->tcp_second_timer_threshold = *i1; 10183 break; 10184 case TCP_CONN_NOTIFY_THRESHOLD: 10185 if (!checkonly) 10186 tcp->tcp_first_ctimer_threshold = *i1; 10187 break; 10188 case TCP_CONN_ABORT_THRESHOLD: 10189 if (!checkonly) 10190 tcp->tcp_second_ctimer_threshold = *i1; 10191 break; 10192 case TCP_RECVDSTADDR: 10193 if (tcp->tcp_state > TCPS_LISTEN) 10194 return (EOPNOTSUPP); 10195 if (!checkonly) 10196 tcp->tcp_recvdstaddr = onoff; 10197 break; 10198 case TCP_ANONPRIVBIND: 10199 if ((reterr = secpolicy_net_privaddr(cr, 0, 10200 IPPROTO_TCP)) != 0) { 10201 *outlenp = 0; 10202 return (reterr); 10203 } 10204 if (!checkonly) { 10205 tcp->tcp_anon_priv_bind = onoff; 10206 } 10207 break; 10208 case TCP_EXCLBIND: 10209 if (!checkonly) 10210 tcp->tcp_exclbind = onoff; 10211 break; /* goto sizeof (int) option return */ 10212 case TCP_INIT_CWND: { 10213 uint32_t init_cwnd = *((uint32_t *)invalp); 10214 10215 if (checkonly) 10216 break; 10217 10218 /* 10219 * Only allow socket with network configuration 10220 * privilege to set the initial cwnd to be larger 10221 * than allowed by RFC 3390. 10222 */ 10223 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10224 tcp->tcp_init_cwnd = init_cwnd; 10225 break; 10226 } 10227 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10228 *outlenp = 0; 10229 return (reterr); 10230 } 10231 if (init_cwnd > TCP_MAX_INIT_CWND) { 10232 *outlenp = 0; 10233 return (EINVAL); 10234 } 10235 tcp->tcp_init_cwnd = init_cwnd; 10236 break; 10237 } 10238 case TCP_KEEPALIVE_THRESHOLD: 10239 if (checkonly) 10240 break; 10241 10242 if (*i1 < tcps->tcps_keepalive_interval_low || 10243 *i1 > tcps->tcps_keepalive_interval_high) { 10244 *outlenp = 0; 10245 return (EINVAL); 10246 } 10247 if (*i1 != tcp->tcp_ka_interval) { 10248 tcp->tcp_ka_interval = *i1; 10249 /* 10250 * Check if we need to restart the 10251 * keepalive timer. 10252 */ 10253 if (tcp->tcp_ka_tid != 0) { 10254 ASSERT(tcp->tcp_ka_enabled); 10255 (void) TCP_TIMER_CANCEL(tcp, 10256 tcp->tcp_ka_tid); 10257 tcp->tcp_ka_last_intrvl = 0; 10258 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10259 tcp_keepalive_killer, 10260 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10261 } 10262 } 10263 break; 10264 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10265 if (!checkonly) { 10266 if (*i1 < 10267 tcps->tcps_keepalive_abort_interval_low || 10268 *i1 > 10269 tcps->tcps_keepalive_abort_interval_high) { 10270 *outlenp = 0; 10271 return (EINVAL); 10272 } 10273 tcp->tcp_ka_abort_thres = *i1; 10274 } 10275 break; 10276 case TCP_CORK: 10277 if (!checkonly) { 10278 /* 10279 * if tcp->tcp_cork was set and is now 10280 * being unset, we have to make sure that 10281 * the remaining data gets sent out. Also 10282 * unset tcp->tcp_cork so that tcp_wput_data() 10283 * can send data even if it is less than mss 10284 */ 10285 if (tcp->tcp_cork && onoff == 0 && 10286 tcp->tcp_unsent > 0) { 10287 tcp->tcp_cork = B_FALSE; 10288 tcp_wput_data(tcp, NULL, B_FALSE); 10289 } 10290 tcp->tcp_cork = onoff; 10291 } 10292 break; 10293 default: 10294 *outlenp = 0; 10295 return (EINVAL); 10296 } 10297 break; 10298 case IPPROTO_IP: 10299 if (tcp->tcp_family != AF_INET) { 10300 *outlenp = 0; 10301 return (ENOPROTOOPT); 10302 } 10303 switch (name) { 10304 case IP_OPTIONS: 10305 case T_IP_OPTIONS: 10306 reterr = tcp_opt_set_header(tcp, checkonly, 10307 invalp, inlen); 10308 if (reterr) { 10309 *outlenp = 0; 10310 return (reterr); 10311 } 10312 /* OK return - copy input buffer into output buffer */ 10313 if (invalp != outvalp) { 10314 /* don't trust bcopy for identical src/dst */ 10315 bcopy(invalp, outvalp, inlen); 10316 } 10317 *outlenp = inlen; 10318 return (0); 10319 case IP_TOS: 10320 case T_IP_TOS: 10321 if (!checkonly) { 10322 tcp->tcp_ipha->ipha_type_of_service = 10323 (uchar_t)*i1; 10324 tcp->tcp_tos = (uchar_t)*i1; 10325 } 10326 break; 10327 case IP_TTL: 10328 if (!checkonly) { 10329 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10330 tcp->tcp_ttl = (uchar_t)*i1; 10331 } 10332 break; 10333 case IP_BOUND_IF: 10334 case IP_NEXTHOP: 10335 /* Handled at the IP level */ 10336 return (-EINVAL); 10337 case IP_SEC_OPT: 10338 /* 10339 * We should not allow policy setting after 10340 * we start listening for connections. 10341 */ 10342 if (tcp->tcp_state == TCPS_LISTEN) { 10343 return (EINVAL); 10344 } else { 10345 /* Handled at the IP level */ 10346 return (-EINVAL); 10347 } 10348 default: 10349 *outlenp = 0; 10350 return (EINVAL); 10351 } 10352 break; 10353 case IPPROTO_IPV6: { 10354 ip6_pkt_t *ipp; 10355 10356 /* 10357 * IPPROTO_IPV6 options are only supported for sockets 10358 * that are using IPv6 on the wire. 10359 */ 10360 if (tcp->tcp_ipversion != IPV6_VERSION) { 10361 *outlenp = 0; 10362 return (ENOPROTOOPT); 10363 } 10364 /* 10365 * Only sticky options; no ancillary data 10366 */ 10367 ipp = &tcp->tcp_sticky_ipp; 10368 10369 switch (name) { 10370 case IPV6_UNICAST_HOPS: 10371 /* -1 means use default */ 10372 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10373 *outlenp = 0; 10374 return (EINVAL); 10375 } 10376 if (!checkonly) { 10377 if (*i1 == -1) { 10378 tcp->tcp_ip6h->ip6_hops = 10379 ipp->ipp_unicast_hops = 10380 (uint8_t)tcps->tcps_ipv6_hoplimit; 10381 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10382 /* Pass modified value to IP. */ 10383 *i1 = tcp->tcp_ip6h->ip6_hops; 10384 } else { 10385 tcp->tcp_ip6h->ip6_hops = 10386 ipp->ipp_unicast_hops = 10387 (uint8_t)*i1; 10388 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10389 } 10390 reterr = tcp_build_hdrs(tcp); 10391 if (reterr != 0) 10392 return (reterr); 10393 } 10394 break; 10395 case IPV6_BOUND_IF: 10396 if (!checkonly) { 10397 tcp->tcp_bound_if = *i1; 10398 PASS_OPT_TO_IP(connp); 10399 } 10400 break; 10401 /* 10402 * Set boolean switches for ancillary data delivery 10403 */ 10404 case IPV6_RECVPKTINFO: 10405 if (!checkonly) { 10406 if (onoff) 10407 tcp->tcp_ipv6_recvancillary |= 10408 TCP_IPV6_RECVPKTINFO; 10409 else 10410 tcp->tcp_ipv6_recvancillary &= 10411 ~TCP_IPV6_RECVPKTINFO; 10412 /* Force it to be sent up with the next msg */ 10413 tcp->tcp_recvifindex = 0; 10414 PASS_OPT_TO_IP(connp); 10415 } 10416 break; 10417 case IPV6_RECVTCLASS: 10418 if (!checkonly) { 10419 if (onoff) 10420 tcp->tcp_ipv6_recvancillary |= 10421 TCP_IPV6_RECVTCLASS; 10422 else 10423 tcp->tcp_ipv6_recvancillary &= 10424 ~TCP_IPV6_RECVTCLASS; 10425 PASS_OPT_TO_IP(connp); 10426 } 10427 break; 10428 case IPV6_RECVHOPLIMIT: 10429 if (!checkonly) { 10430 if (onoff) 10431 tcp->tcp_ipv6_recvancillary |= 10432 TCP_IPV6_RECVHOPLIMIT; 10433 else 10434 tcp->tcp_ipv6_recvancillary &= 10435 ~TCP_IPV6_RECVHOPLIMIT; 10436 /* Force it to be sent up with the next msg */ 10437 tcp->tcp_recvhops = 0xffffffffU; 10438 PASS_OPT_TO_IP(connp); 10439 } 10440 break; 10441 case IPV6_RECVHOPOPTS: 10442 if (!checkonly) { 10443 if (onoff) 10444 tcp->tcp_ipv6_recvancillary |= 10445 TCP_IPV6_RECVHOPOPTS; 10446 else 10447 tcp->tcp_ipv6_recvancillary &= 10448 ~TCP_IPV6_RECVHOPOPTS; 10449 PASS_OPT_TO_IP(connp); 10450 } 10451 break; 10452 case IPV6_RECVDSTOPTS: 10453 if (!checkonly) { 10454 if (onoff) 10455 tcp->tcp_ipv6_recvancillary |= 10456 TCP_IPV6_RECVDSTOPTS; 10457 else 10458 tcp->tcp_ipv6_recvancillary &= 10459 ~TCP_IPV6_RECVDSTOPTS; 10460 PASS_OPT_TO_IP(connp); 10461 } 10462 break; 10463 case _OLD_IPV6_RECVDSTOPTS: 10464 if (!checkonly) { 10465 if (onoff) 10466 tcp->tcp_ipv6_recvancillary |= 10467 TCP_OLD_IPV6_RECVDSTOPTS; 10468 else 10469 tcp->tcp_ipv6_recvancillary &= 10470 ~TCP_OLD_IPV6_RECVDSTOPTS; 10471 } 10472 break; 10473 case IPV6_RECVRTHDR: 10474 if (!checkonly) { 10475 if (onoff) 10476 tcp->tcp_ipv6_recvancillary |= 10477 TCP_IPV6_RECVRTHDR; 10478 else 10479 tcp->tcp_ipv6_recvancillary &= 10480 ~TCP_IPV6_RECVRTHDR; 10481 PASS_OPT_TO_IP(connp); 10482 } 10483 break; 10484 case IPV6_RECVRTHDRDSTOPTS: 10485 if (!checkonly) { 10486 if (onoff) 10487 tcp->tcp_ipv6_recvancillary |= 10488 TCP_IPV6_RECVRTDSTOPTS; 10489 else 10490 tcp->tcp_ipv6_recvancillary &= 10491 ~TCP_IPV6_RECVRTDSTOPTS; 10492 PASS_OPT_TO_IP(connp); 10493 } 10494 break; 10495 case IPV6_PKTINFO: 10496 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10497 return (EINVAL); 10498 if (checkonly) 10499 break; 10500 10501 if (inlen == 0) { 10502 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10503 } else { 10504 struct in6_pktinfo *pkti; 10505 10506 pkti = (struct in6_pktinfo *)invalp; 10507 /* 10508 * RFC 3542 states that ipi6_addr must be 10509 * the unspecified address when setting the 10510 * IPV6_PKTINFO sticky socket option on a 10511 * TCP socket. 10512 */ 10513 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10514 return (EINVAL); 10515 /* 10516 * IP will validate the source address and 10517 * interface index. 10518 */ 10519 reterr = ip_set_options(tcp->tcp_connp, level, 10520 name, invalp, inlen, cr); 10521 if (reterr != 0) 10522 return (reterr); 10523 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10524 ipp->ipp_addr = pkti->ipi6_addr; 10525 if (ipp->ipp_ifindex != 0) 10526 ipp->ipp_fields |= IPPF_IFINDEX; 10527 else 10528 ipp->ipp_fields &= ~IPPF_IFINDEX; 10529 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10530 ipp->ipp_fields |= IPPF_ADDR; 10531 else 10532 ipp->ipp_fields &= ~IPPF_ADDR; 10533 } 10534 reterr = tcp_build_hdrs(tcp); 10535 if (reterr != 0) 10536 return (reterr); 10537 PASS_OPT_TO_IP(connp); 10538 break; 10539 case IPV6_TCLASS: 10540 if (inlen != 0 && inlen != sizeof (int)) 10541 return (EINVAL); 10542 if (checkonly) 10543 break; 10544 10545 if (inlen == 0) { 10546 ipp->ipp_fields &= ~IPPF_TCLASS; 10547 } else { 10548 if (*i1 > 255 || *i1 < -1) 10549 return (EINVAL); 10550 if (*i1 == -1) { 10551 ipp->ipp_tclass = 0; 10552 *i1 = 0; 10553 } else { 10554 ipp->ipp_tclass = *i1; 10555 } 10556 ipp->ipp_fields |= IPPF_TCLASS; 10557 } 10558 reterr = tcp_build_hdrs(tcp); 10559 if (reterr != 0) 10560 return (reterr); 10561 break; 10562 case IPV6_NEXTHOP: 10563 /* 10564 * IP will verify that the nexthop is reachable 10565 * and fail for sticky options. 10566 */ 10567 if (inlen != 0 && inlen != sizeof (sin6_t)) 10568 return (EINVAL); 10569 if (checkonly) 10570 break; 10571 10572 if (inlen == 0) { 10573 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10574 } else { 10575 sin6_t *sin6 = (sin6_t *)invalp; 10576 10577 if (sin6->sin6_family != AF_INET6) 10578 return (EAFNOSUPPORT); 10579 if (IN6_IS_ADDR_V4MAPPED( 10580 &sin6->sin6_addr)) 10581 return (EADDRNOTAVAIL); 10582 ipp->ipp_nexthop = sin6->sin6_addr; 10583 if (!IN6_IS_ADDR_UNSPECIFIED( 10584 &ipp->ipp_nexthop)) 10585 ipp->ipp_fields |= IPPF_NEXTHOP; 10586 else 10587 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10588 } 10589 reterr = tcp_build_hdrs(tcp); 10590 if (reterr != 0) 10591 return (reterr); 10592 PASS_OPT_TO_IP(connp); 10593 break; 10594 case IPV6_HOPOPTS: { 10595 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10596 10597 /* 10598 * Sanity checks - minimum size, size a multiple of 10599 * eight bytes, and matching size passed in. 10600 */ 10601 if (inlen != 0 && 10602 inlen != (8 * (hopts->ip6h_len + 1))) 10603 return (EINVAL); 10604 10605 if (checkonly) 10606 break; 10607 10608 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10609 (uchar_t **)&ipp->ipp_hopopts, 10610 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10611 if (reterr != 0) 10612 return (reterr); 10613 if (ipp->ipp_hopoptslen == 0) 10614 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10615 else 10616 ipp->ipp_fields |= IPPF_HOPOPTS; 10617 reterr = tcp_build_hdrs(tcp); 10618 if (reterr != 0) 10619 return (reterr); 10620 break; 10621 } 10622 case IPV6_RTHDRDSTOPTS: { 10623 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10624 10625 /* 10626 * Sanity checks - minimum size, size a multiple of 10627 * eight bytes, and matching size passed in. 10628 */ 10629 if (inlen != 0 && 10630 inlen != (8 * (dopts->ip6d_len + 1))) 10631 return (EINVAL); 10632 10633 if (checkonly) 10634 break; 10635 10636 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10637 (uchar_t **)&ipp->ipp_rtdstopts, 10638 &ipp->ipp_rtdstoptslen, 0); 10639 if (reterr != 0) 10640 return (reterr); 10641 if (ipp->ipp_rtdstoptslen == 0) 10642 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10643 else 10644 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10645 reterr = tcp_build_hdrs(tcp); 10646 if (reterr != 0) 10647 return (reterr); 10648 break; 10649 } 10650 case IPV6_DSTOPTS: { 10651 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10652 10653 /* 10654 * Sanity checks - minimum size, size a multiple of 10655 * eight bytes, and matching size passed in. 10656 */ 10657 if (inlen != 0 && 10658 inlen != (8 * (dopts->ip6d_len + 1))) 10659 return (EINVAL); 10660 10661 if (checkonly) 10662 break; 10663 10664 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10665 (uchar_t **)&ipp->ipp_dstopts, 10666 &ipp->ipp_dstoptslen, 0); 10667 if (reterr != 0) 10668 return (reterr); 10669 if (ipp->ipp_dstoptslen == 0) 10670 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10671 else 10672 ipp->ipp_fields |= IPPF_DSTOPTS; 10673 reterr = tcp_build_hdrs(tcp); 10674 if (reterr != 0) 10675 return (reterr); 10676 break; 10677 } 10678 case IPV6_RTHDR: { 10679 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10680 10681 /* 10682 * Sanity checks - minimum size, size a multiple of 10683 * eight bytes, and matching size passed in. 10684 */ 10685 if (inlen != 0 && 10686 inlen != (8 * (rt->ip6r_len + 1))) 10687 return (EINVAL); 10688 10689 if (checkonly) 10690 break; 10691 10692 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10693 (uchar_t **)&ipp->ipp_rthdr, 10694 &ipp->ipp_rthdrlen, 0); 10695 if (reterr != 0) 10696 return (reterr); 10697 if (ipp->ipp_rthdrlen == 0) 10698 ipp->ipp_fields &= ~IPPF_RTHDR; 10699 else 10700 ipp->ipp_fields |= IPPF_RTHDR; 10701 reterr = tcp_build_hdrs(tcp); 10702 if (reterr != 0) 10703 return (reterr); 10704 break; 10705 } 10706 case IPV6_V6ONLY: 10707 if (!checkonly) { 10708 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10709 } 10710 break; 10711 case IPV6_USE_MIN_MTU: 10712 if (inlen != sizeof (int)) 10713 return (EINVAL); 10714 10715 if (*i1 < -1 || *i1 > 1) 10716 return (EINVAL); 10717 10718 if (checkonly) 10719 break; 10720 10721 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10722 ipp->ipp_use_min_mtu = *i1; 10723 break; 10724 case IPV6_BOUND_PIF: 10725 /* Handled at the IP level */ 10726 return (-EINVAL); 10727 case IPV6_SEC_OPT: 10728 /* 10729 * We should not allow policy setting after 10730 * we start listening for connections. 10731 */ 10732 if (tcp->tcp_state == TCPS_LISTEN) { 10733 return (EINVAL); 10734 } else { 10735 /* Handled at the IP level */ 10736 return (-EINVAL); 10737 } 10738 case IPV6_SRC_PREFERENCES: 10739 if (inlen != sizeof (uint32_t)) 10740 return (EINVAL); 10741 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10742 *(uint32_t *)invalp); 10743 if (reterr != 0) { 10744 *outlenp = 0; 10745 return (reterr); 10746 } 10747 break; 10748 default: 10749 *outlenp = 0; 10750 return (EINVAL); 10751 } 10752 break; 10753 } /* end IPPROTO_IPV6 */ 10754 default: 10755 *outlenp = 0; 10756 return (EINVAL); 10757 } 10758 /* 10759 * Common case of OK return with outval same as inval 10760 */ 10761 if (invalp != outvalp) { 10762 /* don't trust bcopy for identical src/dst */ 10763 (void) bcopy(invalp, outvalp, inlen); 10764 } 10765 *outlenp = inlen; 10766 return (0); 10767 } 10768 10769 /* ARGSUSED */ 10770 int 10771 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10772 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10773 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10774 { 10775 conn_t *connp = Q_TO_CONN(q); 10776 10777 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10778 outlenp, outvalp, thisdg_attrs, cr)); 10779 } 10780 10781 int 10782 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10783 const void *optvalp, socklen_t optlen, cred_t *cr) 10784 { 10785 conn_t *connp = (conn_t *)proto_handle; 10786 squeue_t *sqp = connp->conn_sqp; 10787 int error; 10788 10789 /* 10790 * Entering the squeue synchronously can result in a context switch, 10791 * which can cause a rather sever performance degradation. So we try to 10792 * handle whatever options we can without entering the squeue. 10793 */ 10794 if (level == IPPROTO_TCP) { 10795 switch (option_name) { 10796 case TCP_NODELAY: 10797 if (optlen != sizeof (int32_t)) 10798 return (EINVAL); 10799 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10800 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10801 connp->conn_tcp->tcp_mss; 10802 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10803 return (0); 10804 default: 10805 break; 10806 } 10807 } 10808 10809 error = squeue_synch_enter(sqp, connp, 0); 10810 if (error == ENOMEM) { 10811 return (ENOMEM); 10812 } 10813 10814 error = proto_opt_check(level, option_name, optlen, NULL, 10815 tcp_opt_obj.odb_opt_des_arr, 10816 tcp_opt_obj.odb_opt_arr_cnt, 10817 tcp_opt_obj.odb_topmost_tpiprovider, 10818 B_TRUE, B_FALSE, cr); 10819 10820 if (error != 0) { 10821 if (error < 0) { 10822 error = proto_tlitosyserr(-error); 10823 } 10824 squeue_synch_exit(sqp, connp); 10825 return (error); 10826 } 10827 10828 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10829 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10830 NULL, cr); 10831 squeue_synch_exit(sqp, connp); 10832 10833 if (error < 0) { 10834 /* 10835 * Pass on to ip 10836 */ 10837 error = ip_set_options(connp, level, option_name, optvalp, 10838 optlen, cr); 10839 } 10840 return (error); 10841 } 10842 10843 /* 10844 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10845 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10846 * headers, and the maximum size tcp header (to avoid reallocation 10847 * on the fly for additional tcp options). 10848 * Returns failure if can't allocate memory. 10849 */ 10850 static int 10851 tcp_build_hdrs(tcp_t *tcp) 10852 { 10853 char *hdrs; 10854 uint_t hdrs_len; 10855 ip6i_t *ip6i; 10856 char buf[TCP_MAX_HDR_LENGTH]; 10857 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10858 in6_addr_t src, dst; 10859 tcp_stack_t *tcps = tcp->tcp_tcps; 10860 conn_t *connp = tcp->tcp_connp; 10861 10862 /* 10863 * save the existing tcp header and source/dest IP addresses 10864 */ 10865 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10866 src = tcp->tcp_ip6h->ip6_src; 10867 dst = tcp->tcp_ip6h->ip6_dst; 10868 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10869 ASSERT(hdrs_len != 0); 10870 if (hdrs_len > tcp->tcp_iphc_len) { 10871 /* Need to reallocate */ 10872 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10873 if (hdrs == NULL) 10874 return (ENOMEM); 10875 if (tcp->tcp_iphc != NULL) { 10876 if (tcp->tcp_hdr_grown) { 10877 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10878 } else { 10879 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10880 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10881 } 10882 tcp->tcp_iphc_len = 0; 10883 } 10884 ASSERT(tcp->tcp_iphc_len == 0); 10885 tcp->tcp_iphc = hdrs; 10886 tcp->tcp_iphc_len = hdrs_len; 10887 tcp->tcp_hdr_grown = B_TRUE; 10888 } 10889 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10890 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10891 10892 /* Set header fields not in ipp */ 10893 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10894 ip6i = (ip6i_t *)tcp->tcp_iphc; 10895 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10896 } else { 10897 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10898 } 10899 /* 10900 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10901 * 10902 * tcp->tcp_tcp_hdr_len doesn't change here. 10903 */ 10904 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10905 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10906 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10907 10908 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10909 10910 tcp->tcp_ip6h->ip6_src = src; 10911 tcp->tcp_ip6h->ip6_dst = dst; 10912 10913 /* 10914 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10915 * the default value for TCP. 10916 */ 10917 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10918 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10919 10920 /* 10921 * If we're setting extension headers after a connection 10922 * has been established, and if we have a routing header 10923 * among the extension headers, call ip_massage_options_v6 to 10924 * manipulate the routing header/ip6_dst set the checksum 10925 * difference in the tcp header template. 10926 * (This happens in tcp_connect_ipv6 if the routing header 10927 * is set prior to the connect.) 10928 * Set the tcp_sum to zero first in case we've cleared a 10929 * routing header or don't have one at all. 10930 */ 10931 tcp->tcp_sum = 0; 10932 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10933 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10934 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10935 (uint8_t *)tcp->tcp_tcph); 10936 if (rth != NULL) { 10937 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10938 rth, tcps->tcps_netstack); 10939 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10940 (tcp->tcp_sum >> 16)); 10941 } 10942 } 10943 10944 /* Try to get everything in a single mblk */ 10945 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10946 hdrs_len + tcps->tcps_wroff_xtra); 10947 return (0); 10948 } 10949 10950 /* 10951 * Transfer any source route option from ipha to buf/dst in reversed form. 10952 */ 10953 static int 10954 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10955 { 10956 ipoptp_t opts; 10957 uchar_t *opt; 10958 uint8_t optval; 10959 uint8_t optlen; 10960 uint32_t len = 0; 10961 10962 for (optval = ipoptp_first(&opts, ipha); 10963 optval != IPOPT_EOL; 10964 optval = ipoptp_next(&opts)) { 10965 opt = opts.ipoptp_cur; 10966 optlen = opts.ipoptp_len; 10967 switch (optval) { 10968 int off1, off2; 10969 case IPOPT_SSRR: 10970 case IPOPT_LSRR: 10971 10972 /* Reverse source route */ 10973 /* 10974 * First entry should be the next to last one in the 10975 * current source route (the last entry is our 10976 * address.) 10977 * The last entry should be the final destination. 10978 */ 10979 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10980 buf[IPOPT_OLEN] = (uint8_t)optlen; 10981 off1 = IPOPT_MINOFF_SR - 1; 10982 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10983 if (off2 < 0) { 10984 /* No entries in source route */ 10985 break; 10986 } 10987 bcopy(opt + off2, dst, IP_ADDR_LEN); 10988 /* 10989 * Note: use src since ipha has not had its src 10990 * and dst reversed (it is in the state it was 10991 * received. 10992 */ 10993 bcopy(&ipha->ipha_src, buf + off2, 10994 IP_ADDR_LEN); 10995 off2 -= IP_ADDR_LEN; 10996 10997 while (off2 > 0) { 10998 bcopy(opt + off2, buf + off1, 10999 IP_ADDR_LEN); 11000 off1 += IP_ADDR_LEN; 11001 off2 -= IP_ADDR_LEN; 11002 } 11003 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11004 buf += optlen; 11005 len += optlen; 11006 break; 11007 } 11008 } 11009 done: 11010 /* Pad the resulting options */ 11011 while (len & 0x3) { 11012 *buf++ = IPOPT_EOL; 11013 len++; 11014 } 11015 return (len); 11016 } 11017 11018 11019 /* 11020 * Extract and revert a source route from ipha (if any) 11021 * and then update the relevant fields in both tcp_t and the standard header. 11022 */ 11023 static void 11024 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11025 { 11026 char buf[TCP_MAX_HDR_LENGTH]; 11027 uint_t tcph_len; 11028 int len; 11029 11030 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11031 len = IPH_HDR_LENGTH(ipha); 11032 if (len == IP_SIMPLE_HDR_LENGTH) 11033 /* Nothing to do */ 11034 return; 11035 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11036 (len & 0x3)) 11037 return; 11038 11039 tcph_len = tcp->tcp_tcp_hdr_len; 11040 bcopy(tcp->tcp_tcph, buf, tcph_len); 11041 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11042 (tcp->tcp_ipha->ipha_dst & 0xffff); 11043 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11044 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11045 len += IP_SIMPLE_HDR_LENGTH; 11046 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11047 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11048 if ((int)tcp->tcp_sum < 0) 11049 tcp->tcp_sum--; 11050 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11051 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11052 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11053 bcopy(buf, tcp->tcp_tcph, tcph_len); 11054 tcp->tcp_ip_hdr_len = len; 11055 tcp->tcp_ipha->ipha_version_and_hdr_length = 11056 (IP_VERSION << 4) | (len >> 2); 11057 len += tcph_len; 11058 tcp->tcp_hdr_len = len; 11059 } 11060 11061 /* 11062 * Copy the standard header into its new location, 11063 * lay in the new options and then update the relevant 11064 * fields in both tcp_t and the standard header. 11065 */ 11066 static int 11067 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11068 { 11069 uint_t tcph_len; 11070 uint8_t *ip_optp; 11071 tcph_t *new_tcph; 11072 tcp_stack_t *tcps = tcp->tcp_tcps; 11073 conn_t *connp = tcp->tcp_connp; 11074 11075 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11076 return (EINVAL); 11077 11078 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11079 return (EINVAL); 11080 11081 if (checkonly) { 11082 /* 11083 * do not really set, just pretend to - T_CHECK 11084 */ 11085 return (0); 11086 } 11087 11088 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11089 if (tcp->tcp_label_len > 0) { 11090 int padlen; 11091 uint8_t opt; 11092 11093 /* convert list termination to no-ops */ 11094 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11095 ip_optp += ip_optp[IPOPT_OLEN]; 11096 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11097 while (--padlen >= 0) 11098 *ip_optp++ = opt; 11099 } 11100 tcph_len = tcp->tcp_tcp_hdr_len; 11101 new_tcph = (tcph_t *)(ip_optp + len); 11102 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11103 tcp->tcp_tcph = new_tcph; 11104 bcopy(ptr, ip_optp, len); 11105 11106 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11107 11108 tcp->tcp_ip_hdr_len = len; 11109 tcp->tcp_ipha->ipha_version_and_hdr_length = 11110 (IP_VERSION << 4) | (len >> 2); 11111 tcp->tcp_hdr_len = len + tcph_len; 11112 if (!TCP_IS_DETACHED(tcp)) { 11113 /* Always allocate room for all options. */ 11114 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11115 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11116 } 11117 return (0); 11118 } 11119 11120 /* Get callback routine passed to nd_load by tcp_param_register */ 11121 /* ARGSUSED */ 11122 static int 11123 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11124 { 11125 tcpparam_t *tcppa = (tcpparam_t *)cp; 11126 11127 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11128 return (0); 11129 } 11130 11131 /* 11132 * Walk through the param array specified registering each element with the 11133 * named dispatch handler. 11134 */ 11135 static boolean_t 11136 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11137 { 11138 for (; cnt-- > 0; tcppa++) { 11139 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11140 if (!nd_load(ndp, tcppa->tcp_param_name, 11141 tcp_param_get, tcp_param_set, 11142 (caddr_t)tcppa)) { 11143 nd_free(ndp); 11144 return (B_FALSE); 11145 } 11146 } 11147 } 11148 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11149 KM_SLEEP); 11150 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11151 sizeof (tcpparam_t)); 11152 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11153 tcp_param_get, tcp_param_set_aligned, 11154 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11155 nd_free(ndp); 11156 return (B_FALSE); 11157 } 11158 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11159 KM_SLEEP); 11160 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11161 sizeof (tcpparam_t)); 11162 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11163 tcp_param_get, tcp_param_set_aligned, 11164 (caddr_t)tcps->tcps_mdt_head_param)) { 11165 nd_free(ndp); 11166 return (B_FALSE); 11167 } 11168 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11169 KM_SLEEP); 11170 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11171 sizeof (tcpparam_t)); 11172 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11173 tcp_param_get, tcp_param_set_aligned, 11174 (caddr_t)tcps->tcps_mdt_tail_param)) { 11175 nd_free(ndp); 11176 return (B_FALSE); 11177 } 11178 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11179 KM_SLEEP); 11180 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11181 sizeof (tcpparam_t)); 11182 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11183 tcp_param_get, tcp_param_set_aligned, 11184 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11185 nd_free(ndp); 11186 return (B_FALSE); 11187 } 11188 if (!nd_load(ndp, "tcp_extra_priv_ports", 11189 tcp_extra_priv_ports_get, NULL, NULL)) { 11190 nd_free(ndp); 11191 return (B_FALSE); 11192 } 11193 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11194 NULL, tcp_extra_priv_ports_add, NULL)) { 11195 nd_free(ndp); 11196 return (B_FALSE); 11197 } 11198 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11199 NULL, tcp_extra_priv_ports_del, NULL)) { 11200 nd_free(ndp); 11201 return (B_FALSE); 11202 } 11203 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11204 NULL)) { 11205 nd_free(ndp); 11206 return (B_FALSE); 11207 } 11208 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11209 NULL, NULL)) { 11210 nd_free(ndp); 11211 return (B_FALSE); 11212 } 11213 if (!nd_load(ndp, "tcp_listen_hash", 11214 tcp_listen_hash_report, NULL, NULL)) { 11215 nd_free(ndp); 11216 return (B_FALSE); 11217 } 11218 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11219 NULL, NULL)) { 11220 nd_free(ndp); 11221 return (B_FALSE); 11222 } 11223 if (!nd_load(ndp, "tcp_acceptor_hash", 11224 tcp_acceptor_hash_report, NULL, NULL)) { 11225 nd_free(ndp); 11226 return (B_FALSE); 11227 } 11228 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11229 tcp_1948_phrase_set, NULL)) { 11230 nd_free(ndp); 11231 return (B_FALSE); 11232 } 11233 /* 11234 * Dummy ndd variables - only to convey obsolescence information 11235 * through printing of their name (no get or set routines) 11236 * XXX Remove in future releases ? 11237 */ 11238 if (!nd_load(ndp, 11239 "tcp_close_wait_interval(obsoleted - " 11240 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11241 nd_free(ndp); 11242 return (B_FALSE); 11243 } 11244 return (B_TRUE); 11245 } 11246 11247 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11248 /* ARGSUSED */ 11249 static int 11250 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11251 cred_t *cr) 11252 { 11253 long new_value; 11254 tcpparam_t *tcppa = (tcpparam_t *)cp; 11255 11256 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11257 new_value < tcppa->tcp_param_min || 11258 new_value > tcppa->tcp_param_max) { 11259 return (EINVAL); 11260 } 11261 /* 11262 * Need to make sure new_value is a multiple of 4. If it is not, 11263 * round it up. For future 64 bit requirement, we actually make it 11264 * a multiple of 8. 11265 */ 11266 if (new_value & 0x7) { 11267 new_value = (new_value & ~0x7) + 0x8; 11268 } 11269 tcppa->tcp_param_val = new_value; 11270 return (0); 11271 } 11272 11273 /* Set callback routine passed to nd_load by tcp_param_register */ 11274 /* ARGSUSED */ 11275 static int 11276 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11277 { 11278 long new_value; 11279 tcpparam_t *tcppa = (tcpparam_t *)cp; 11280 11281 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11282 new_value < tcppa->tcp_param_min || 11283 new_value > tcppa->tcp_param_max) { 11284 return (EINVAL); 11285 } 11286 tcppa->tcp_param_val = new_value; 11287 return (0); 11288 } 11289 11290 /* 11291 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11292 * is filled, return as much as we can. The message passed in may be 11293 * multi-part, chained using b_cont. "start" is the starting sequence 11294 * number for this piece. 11295 */ 11296 static mblk_t * 11297 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11298 { 11299 uint32_t end; 11300 mblk_t *mp1; 11301 mblk_t *mp2; 11302 mblk_t *next_mp; 11303 uint32_t u1; 11304 tcp_stack_t *tcps = tcp->tcp_tcps; 11305 11306 /* Walk through all the new pieces. */ 11307 do { 11308 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11309 (uintptr_t)INT_MAX); 11310 end = start + (int)(mp->b_wptr - mp->b_rptr); 11311 next_mp = mp->b_cont; 11312 if (start == end) { 11313 /* Empty. Blast it. */ 11314 freeb(mp); 11315 continue; 11316 } 11317 mp->b_cont = NULL; 11318 TCP_REASS_SET_SEQ(mp, start); 11319 TCP_REASS_SET_END(mp, end); 11320 mp1 = tcp->tcp_reass_tail; 11321 if (!mp1) { 11322 tcp->tcp_reass_tail = mp; 11323 tcp->tcp_reass_head = mp; 11324 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11325 UPDATE_MIB(&tcps->tcps_mib, 11326 tcpInDataUnorderBytes, end - start); 11327 continue; 11328 } 11329 /* New stuff completely beyond tail? */ 11330 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11331 /* Link it on end. */ 11332 mp1->b_cont = mp; 11333 tcp->tcp_reass_tail = mp; 11334 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11335 UPDATE_MIB(&tcps->tcps_mib, 11336 tcpInDataUnorderBytes, end - start); 11337 continue; 11338 } 11339 mp1 = tcp->tcp_reass_head; 11340 u1 = TCP_REASS_SEQ(mp1); 11341 /* New stuff at the front? */ 11342 if (SEQ_LT(start, u1)) { 11343 /* Yes... Check for overlap. */ 11344 mp->b_cont = mp1; 11345 tcp->tcp_reass_head = mp; 11346 tcp_reass_elim_overlap(tcp, mp); 11347 continue; 11348 } 11349 /* 11350 * The new piece fits somewhere between the head and tail. 11351 * We find our slot, where mp1 precedes us and mp2 trails. 11352 */ 11353 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11354 u1 = TCP_REASS_SEQ(mp2); 11355 if (SEQ_LEQ(start, u1)) 11356 break; 11357 } 11358 /* Link ourselves in */ 11359 mp->b_cont = mp2; 11360 mp1->b_cont = mp; 11361 11362 /* Trim overlap with following mblk(s) first */ 11363 tcp_reass_elim_overlap(tcp, mp); 11364 11365 /* Trim overlap with preceding mblk */ 11366 tcp_reass_elim_overlap(tcp, mp1); 11367 11368 } while (start = end, mp = next_mp); 11369 mp1 = tcp->tcp_reass_head; 11370 /* Anything ready to go? */ 11371 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11372 return (NULL); 11373 /* Eat what we can off the queue */ 11374 for (;;) { 11375 mp = mp1->b_cont; 11376 end = TCP_REASS_END(mp1); 11377 TCP_REASS_SET_SEQ(mp1, 0); 11378 TCP_REASS_SET_END(mp1, 0); 11379 if (!mp) { 11380 tcp->tcp_reass_tail = NULL; 11381 break; 11382 } 11383 if (end != TCP_REASS_SEQ(mp)) { 11384 mp1->b_cont = NULL; 11385 break; 11386 } 11387 mp1 = mp; 11388 } 11389 mp1 = tcp->tcp_reass_head; 11390 tcp->tcp_reass_head = mp; 11391 return (mp1); 11392 } 11393 11394 /* Eliminate any overlap that mp may have over later mblks */ 11395 static void 11396 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11397 { 11398 uint32_t end; 11399 mblk_t *mp1; 11400 uint32_t u1; 11401 tcp_stack_t *tcps = tcp->tcp_tcps; 11402 11403 end = TCP_REASS_END(mp); 11404 while ((mp1 = mp->b_cont) != NULL) { 11405 u1 = TCP_REASS_SEQ(mp1); 11406 if (!SEQ_GT(end, u1)) 11407 break; 11408 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11409 mp->b_wptr -= end - u1; 11410 TCP_REASS_SET_END(mp, u1); 11411 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11412 UPDATE_MIB(&tcps->tcps_mib, 11413 tcpInDataPartDupBytes, end - u1); 11414 break; 11415 } 11416 mp->b_cont = mp1->b_cont; 11417 TCP_REASS_SET_SEQ(mp1, 0); 11418 TCP_REASS_SET_END(mp1, 0); 11419 freeb(mp1); 11420 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11421 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11422 } 11423 if (!mp1) 11424 tcp->tcp_reass_tail = mp; 11425 } 11426 11427 static uint_t 11428 tcp_rwnd_reopen(tcp_t *tcp) 11429 { 11430 uint_t ret = 0; 11431 uint_t thwin; 11432 11433 /* Learn the latest rwnd information that we sent to the other side. */ 11434 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11435 << tcp->tcp_rcv_ws; 11436 /* This is peer's calculated send window (our receive window). */ 11437 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11438 /* 11439 * Increase the receive window to max. But we need to do receiver 11440 * SWS avoidance. This means that we need to check the increase of 11441 * of receive window is at least 1 MSS. 11442 */ 11443 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11444 /* 11445 * If the window that the other side knows is less than max 11446 * deferred acks segments, send an update immediately. 11447 */ 11448 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11449 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11450 ret = TH_ACK_NEEDED; 11451 } 11452 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11453 } 11454 return (ret); 11455 } 11456 11457 /* 11458 * Send up all messages queued on tcp_rcv_list. 11459 */ 11460 static uint_t 11461 tcp_rcv_drain(tcp_t *tcp) 11462 { 11463 mblk_t *mp; 11464 uint_t ret = 0; 11465 #ifdef DEBUG 11466 uint_t cnt = 0; 11467 #endif 11468 queue_t *q = tcp->tcp_rq; 11469 11470 /* Can't drain on an eager connection */ 11471 if (tcp->tcp_listener != NULL) 11472 return (ret); 11473 11474 /* Can't be a non-STREAMS connection or sodirect enabled */ 11475 ASSERT((!IPCL_IS_NONSTR(tcp->tcp_connp)) && SOD_NOT_ENABLED(tcp)); 11476 11477 /* No need for the push timer now. */ 11478 if (tcp->tcp_push_tid != 0) { 11479 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11480 tcp->tcp_push_tid = 0; 11481 } 11482 11483 /* 11484 * Handle two cases here: we are currently fused or we were 11485 * previously fused and have some urgent data to be delivered 11486 * upstream. The latter happens because we either ran out of 11487 * memory or were detached and therefore sending the SIGURG was 11488 * deferred until this point. In either case we pass control 11489 * over to tcp_fuse_rcv_drain() since it may need to complete 11490 * some work. 11491 */ 11492 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11493 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11494 tcp->tcp_fused_sigurg_mp != NULL); 11495 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11496 &tcp->tcp_fused_sigurg_mp)) 11497 return (ret); 11498 } 11499 11500 while ((mp = tcp->tcp_rcv_list) != NULL) { 11501 tcp->tcp_rcv_list = mp->b_next; 11502 mp->b_next = NULL; 11503 #ifdef DEBUG 11504 cnt += msgdsize(mp); 11505 #endif 11506 /* Does this need SSL processing first? */ 11507 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11508 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11509 mblk_t *, mp); 11510 tcp_kssl_input(tcp, mp); 11511 continue; 11512 } 11513 putnext(q, mp); 11514 } 11515 #ifdef DEBUG 11516 ASSERT(cnt == tcp->tcp_rcv_cnt); 11517 #endif 11518 tcp->tcp_rcv_last_head = NULL; 11519 tcp->tcp_rcv_last_tail = NULL; 11520 tcp->tcp_rcv_cnt = 0; 11521 11522 if (canputnext(q)) 11523 return (tcp_rwnd_reopen(tcp)); 11524 11525 return (ret); 11526 } 11527 11528 /* 11529 * Queue data on tcp_rcv_list which is a b_next chain. 11530 * tcp_rcv_last_head/tail is the last element of this chain. 11531 * Each element of the chain is a b_cont chain. 11532 * 11533 * M_DATA messages are added to the current element. 11534 * Other messages are added as new (b_next) elements. 11535 */ 11536 void 11537 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11538 { 11539 ASSERT(seg_len == msgdsize(mp)); 11540 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11541 11542 if (tcp->tcp_rcv_list == NULL) { 11543 ASSERT(tcp->tcp_rcv_last_head == NULL); 11544 tcp->tcp_rcv_list = mp; 11545 tcp->tcp_rcv_last_head = mp; 11546 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11547 tcp->tcp_rcv_last_tail->b_cont = mp; 11548 } else { 11549 tcp->tcp_rcv_last_head->b_next = mp; 11550 tcp->tcp_rcv_last_head = mp; 11551 } 11552 11553 while (mp->b_cont) 11554 mp = mp->b_cont; 11555 11556 tcp->tcp_rcv_last_tail = mp; 11557 tcp->tcp_rcv_cnt += seg_len; 11558 tcp->tcp_rwnd -= seg_len; 11559 } 11560 11561 /* 11562 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11563 * above, in addition when uioa is enabled schedule an asynchronous uio 11564 * prior to enqueuing. They implement the combinhed semantics of the 11565 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11566 * canputnext(), i.e. flow-control with backenable. 11567 * 11568 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11569 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11570 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11571 * 11572 * Must be called with sodp->sod_lockp held and will return with the lock 11573 * released. 11574 */ 11575 static uint_t 11576 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11577 { 11578 queue_t *q = tcp->tcp_rq; 11579 uint_t thwin; 11580 tcp_stack_t *tcps = tcp->tcp_tcps; 11581 uint_t ret = 0; 11582 11583 /* Can't be an eager connection */ 11584 ASSERT(tcp->tcp_listener == NULL); 11585 11586 /* Caller must have lock held */ 11587 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11588 11589 /* Sodirect mode so must not be a tcp_rcv_list */ 11590 ASSERT(tcp->tcp_rcv_list == NULL); 11591 11592 if (SOD_QFULL(sodp)) { 11593 /* Q is full, mark Q for need backenable */ 11594 SOD_QSETBE(sodp); 11595 } 11596 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11597 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11598 << tcp->tcp_rcv_ws; 11599 /* This is peer's calculated send window (our available rwnd). */ 11600 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11601 /* 11602 * Increase the receive window to max. But we need to do receiver 11603 * SWS avoidance. This means that we need to check the increase of 11604 * of receive window is at least 1 MSS. 11605 */ 11606 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11607 /* 11608 * If the window that the other side knows is less than max 11609 * deferred acks segments, send an update immediately. 11610 */ 11611 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11612 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11613 ret = TH_ACK_NEEDED; 11614 } 11615 tcp->tcp_rwnd = q->q_hiwat; 11616 } 11617 11618 if (!SOD_QEMPTY(sodp)) { 11619 /* Wakeup to socket */ 11620 sodp->sod_state &= SOD_WAKE_CLR; 11621 sodp->sod_state |= SOD_WAKE_DONE; 11622 (sodp->sod_wakeup)(sodp); 11623 /* wakeup() does the mutex_ext() */ 11624 } else { 11625 /* Q is empty, no need to wake */ 11626 sodp->sod_state &= SOD_WAKE_CLR; 11627 sodp->sod_state |= SOD_WAKE_NOT; 11628 mutex_exit(sodp->sod_lockp); 11629 } 11630 11631 /* No need for the push timer now. */ 11632 if (tcp->tcp_push_tid != 0) { 11633 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11634 tcp->tcp_push_tid = 0; 11635 } 11636 11637 return (ret); 11638 } 11639 11640 /* 11641 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11642 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11643 * to the user-land buffer and flag the mblk_t as such. 11644 * 11645 * Also, handle tcp_rwnd. 11646 */ 11647 uint_t 11648 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11649 { 11650 uioa_t *uioap = &sodp->sod_uioa; 11651 boolean_t qfull; 11652 uint_t thwin; 11653 11654 /* Can't be an eager connection */ 11655 ASSERT(tcp->tcp_listener == NULL); 11656 11657 /* Caller must have lock held */ 11658 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11659 11660 /* Sodirect mode so must not be a tcp_rcv_list */ 11661 ASSERT(tcp->tcp_rcv_list == NULL); 11662 11663 /* Passed in segment length must be equal to mblk_t chain data size */ 11664 ASSERT(seg_len == msgdsize(mp)); 11665 11666 if (DB_TYPE(mp) != M_DATA) { 11667 /* Only process M_DATA mblk_t's */ 11668 goto enq; 11669 } 11670 if (uioap->uioa_state & UIOA_ENABLED) { 11671 /* Uioa is enabled */ 11672 mblk_t *mp1 = mp; 11673 mblk_t *lmp = NULL; 11674 11675 if (seg_len > uioap->uio_resid) { 11676 /* 11677 * There isn't enough uio space for the mblk_t chain 11678 * so disable uioa such that this and any additional 11679 * mblk_t data is handled by the socket and schedule 11680 * the socket for wakeup to finish this uioa. 11681 */ 11682 uioap->uioa_state &= UIOA_CLR; 11683 uioap->uioa_state |= UIOA_FINI; 11684 if (sodp->sod_state & SOD_WAKE_NOT) { 11685 sodp->sod_state &= SOD_WAKE_CLR; 11686 sodp->sod_state |= SOD_WAKE_NEED; 11687 } 11688 goto enq; 11689 } 11690 do { 11691 uint32_t len = MBLKL(mp1); 11692 11693 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11694 /* Scheduled, mark dblk_t as such */ 11695 DB_FLAGS(mp1) |= DBLK_UIOA; 11696 } else { 11697 /* Error, turn off async processing */ 11698 uioap->uioa_state &= UIOA_CLR; 11699 uioap->uioa_state |= UIOA_FINI; 11700 break; 11701 } 11702 lmp = mp1; 11703 } while ((mp1 = mp1->b_cont) != NULL); 11704 11705 if (mp1 != NULL || uioap->uio_resid == 0) { 11706 /* 11707 * Not all mblk_t(s) uioamoved (error) or all uio 11708 * space has been consumed so schedule the socket 11709 * for wakeup to finish this uio. 11710 */ 11711 sodp->sod_state &= SOD_WAKE_CLR; 11712 sodp->sod_state |= SOD_WAKE_NEED; 11713 11714 /* Break the mblk chain if neccessary. */ 11715 if (mp1 != NULL && lmp != NULL) { 11716 mp->b_next = mp1; 11717 lmp->b_cont = NULL; 11718 } 11719 } 11720 } else if (uioap->uioa_state & UIOA_FINI) { 11721 /* 11722 * Post UIO_ENABLED waiting for socket to finish processing 11723 * so just enqueue and update tcp_rwnd. 11724 */ 11725 if (SOD_QFULL(sodp)) 11726 tcp->tcp_rwnd -= seg_len; 11727 } else if (sodp->sod_want > 0) { 11728 /* 11729 * Uioa isn't enabled but sodirect has a pending read(). 11730 */ 11731 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11732 if (sodp->sod_state & SOD_WAKE_NOT) { 11733 /* Schedule socket for wakeup */ 11734 sodp->sod_state &= SOD_WAKE_CLR; 11735 sodp->sod_state |= SOD_WAKE_NEED; 11736 } 11737 tcp->tcp_rwnd -= seg_len; 11738 } 11739 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11740 /* 11741 * No pending sodirect read() so used the default 11742 * TCP push logic to guess that a push is needed. 11743 */ 11744 if (sodp->sod_state & SOD_WAKE_NOT) { 11745 /* Schedule socket for wakeup */ 11746 sodp->sod_state &= SOD_WAKE_CLR; 11747 sodp->sod_state |= SOD_WAKE_NEED; 11748 } 11749 tcp->tcp_rwnd -= seg_len; 11750 } else { 11751 /* Just update tcp_rwnd */ 11752 tcp->tcp_rwnd -= seg_len; 11753 } 11754 enq: 11755 qfull = SOD_QFULL(sodp); 11756 11757 (sodp->sod_enqueue)(sodp, mp); 11758 11759 if (! qfull && SOD_QFULL(sodp)) { 11760 /* Wasn't QFULL, now QFULL, need back-enable */ 11761 SOD_QSETBE(sodp); 11762 } 11763 11764 /* 11765 * Check to see if remote avail swnd < mss due to delayed ACK, 11766 * first get advertised rwnd. 11767 */ 11768 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11769 /* Minus delayed ACK count */ 11770 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11771 if (thwin < tcp->tcp_mss) { 11772 /* Remote avail swnd < mss, need ACK now */ 11773 return (TH_ACK_NEEDED); 11774 } 11775 11776 return (0); 11777 } 11778 11779 /* 11780 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11781 * 11782 * This is the default entry function into TCP on the read side. TCP is 11783 * always entered via squeue i.e. using squeue's for mutual exclusion. 11784 * When classifier does a lookup to find the tcp, it also puts a reference 11785 * on the conn structure associated so the tcp is guaranteed to exist 11786 * when we come here. We still need to check the state because it might 11787 * as well has been closed. The squeue processing function i.e. squeue_enter, 11788 * is responsible for doing the CONN_DEC_REF. 11789 * 11790 * Apart from the default entry point, IP also sends packets directly to 11791 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11792 * connections. 11793 */ 11794 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11795 void 11796 tcp_input(void *arg, mblk_t *mp, void *arg2) 11797 { 11798 conn_t *connp = (conn_t *)arg; 11799 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11800 11801 /* arg2 is the sqp */ 11802 ASSERT(arg2 != NULL); 11803 ASSERT(mp != NULL); 11804 11805 /* 11806 * Don't accept any input on a closed tcp as this TCP logically does 11807 * not exist on the system. Don't proceed further with this TCP. 11808 * For eg. this packet could trigger another close of this tcp 11809 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11810 * tcp_clean_death / tcp_closei_local must be called at most once 11811 * on a TCP. In this case we need to refeed the packet into the 11812 * classifier and figure out where the packet should go. Need to 11813 * preserve the recv_ill somehow. Until we figure that out, for 11814 * now just drop the packet if we can't classify the packet. 11815 */ 11816 if (tcp->tcp_state == TCPS_CLOSED || 11817 tcp->tcp_state == TCPS_BOUND) { 11818 conn_t *new_connp; 11819 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11820 11821 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11822 if (new_connp != NULL) { 11823 tcp_reinput(new_connp, mp, arg2); 11824 return; 11825 } 11826 /* We failed to classify. For now just drop the packet */ 11827 freemsg(mp); 11828 return; 11829 } 11830 11831 if (DB_TYPE(mp) != M_DATA) { 11832 tcp_rput_common(tcp, mp); 11833 return; 11834 } 11835 11836 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11837 squeue_t *final_sqp; 11838 11839 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11840 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11841 DB_CKSUMSTART(mp) = 0; 11842 if (tcp->tcp_state == TCPS_SYN_SENT && 11843 connp->conn_final_sqp == NULL && 11844 tcp_outbound_squeue_switch) { 11845 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11846 connp->conn_final_sqp = final_sqp; 11847 if (connp->conn_final_sqp != connp->conn_sqp) { 11848 CONN_INC_REF(connp); 11849 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11850 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11851 tcp_rput_data, connp, ip_squeue_flag, 11852 SQTAG_CONNECT_FINISH); 11853 return; 11854 } 11855 } 11856 } 11857 tcp_rput_data(connp, mp, arg2); 11858 } 11859 11860 /* 11861 * The read side put procedure. 11862 * The packets passed up by ip are assume to be aligned according to 11863 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11864 */ 11865 static void 11866 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11867 { 11868 /* 11869 * tcp_rput_data() does not expect M_CTL except for the case 11870 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11871 * type. Need to make sure that any other M_CTLs don't make 11872 * it to tcp_rput_data since it is not expecting any and doesn't 11873 * check for it. 11874 */ 11875 if (DB_TYPE(mp) == M_CTL) { 11876 switch (*(uint32_t *)(mp->b_rptr)) { 11877 case TCP_IOC_ABORT_CONN: 11878 /* 11879 * Handle connection abort request. 11880 */ 11881 tcp_ioctl_abort_handler(tcp, mp); 11882 return; 11883 case IPSEC_IN: 11884 /* 11885 * Only secure icmp arrive in TCP and they 11886 * don't go through data path. 11887 */ 11888 tcp_icmp_error(tcp, mp); 11889 return; 11890 case IN_PKTINFO: 11891 /* 11892 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11893 * sockets that are receiving IPv4 traffic. tcp 11894 */ 11895 ASSERT(tcp->tcp_family == AF_INET6); 11896 ASSERT(tcp->tcp_ipv6_recvancillary & 11897 TCP_IPV6_RECVPKTINFO); 11898 tcp_rput_data(tcp->tcp_connp, mp, 11899 tcp->tcp_connp->conn_sqp); 11900 return; 11901 case MDT_IOC_INFO_UPDATE: 11902 /* 11903 * Handle Multidata information update; the 11904 * following routine will free the message. 11905 */ 11906 if (tcp->tcp_connp->conn_mdt_ok) { 11907 tcp_mdt_update(tcp, 11908 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11909 B_FALSE); 11910 } 11911 freemsg(mp); 11912 return; 11913 case LSO_IOC_INFO_UPDATE: 11914 /* 11915 * Handle LSO information update; the following 11916 * routine will free the message. 11917 */ 11918 if (tcp->tcp_connp->conn_lso_ok) { 11919 tcp_lso_update(tcp, 11920 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11921 } 11922 freemsg(mp); 11923 return; 11924 default: 11925 /* 11926 * tcp_icmp_err() will process the M_CTL packets. 11927 * Non-ICMP packets, if any, will be discarded in 11928 * tcp_icmp_err(). We will process the ICMP packet 11929 * even if we are TCP_IS_DETACHED_NONEAGER as the 11930 * incoming ICMP packet may result in changing 11931 * the tcp_mss, which we would need if we have 11932 * packets to retransmit. 11933 */ 11934 tcp_icmp_error(tcp, mp); 11935 return; 11936 } 11937 } 11938 11939 /* No point processing the message if tcp is already closed */ 11940 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11941 freemsg(mp); 11942 return; 11943 } 11944 11945 tcp_rput_other(tcp, mp); 11946 } 11947 11948 11949 /* The minimum of smoothed mean deviation in RTO calculation. */ 11950 #define TCP_SD_MIN 400 11951 11952 /* 11953 * Set RTO for this connection. The formula is from Jacobson and Karels' 11954 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11955 * are the same as those in Appendix A.2 of that paper. 11956 * 11957 * m = new measurement 11958 * sa = smoothed RTT average (8 * average estimates). 11959 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11960 */ 11961 static void 11962 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11963 { 11964 long m = TICK_TO_MSEC(rtt); 11965 clock_t sa = tcp->tcp_rtt_sa; 11966 clock_t sv = tcp->tcp_rtt_sd; 11967 clock_t rto; 11968 tcp_stack_t *tcps = tcp->tcp_tcps; 11969 11970 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11971 tcp->tcp_rtt_update++; 11972 11973 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11974 if (sa != 0) { 11975 /* 11976 * Update average estimator: 11977 * new rtt = 7/8 old rtt + 1/8 Error 11978 */ 11979 11980 /* m is now Error in estimate. */ 11981 m -= sa >> 3; 11982 if ((sa += m) <= 0) { 11983 /* 11984 * Don't allow the smoothed average to be negative. 11985 * We use 0 to denote reinitialization of the 11986 * variables. 11987 */ 11988 sa = 1; 11989 } 11990 11991 /* 11992 * Update deviation estimator: 11993 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11994 */ 11995 if (m < 0) 11996 m = -m; 11997 m -= sv >> 2; 11998 sv += m; 11999 } else { 12000 /* 12001 * This follows BSD's implementation. So the reinitialized 12002 * RTO is 3 * m. We cannot go less than 2 because if the 12003 * link is bandwidth dominated, doubling the window size 12004 * during slow start means doubling the RTT. We want to be 12005 * more conservative when we reinitialize our estimates. 3 12006 * is just a convenient number. 12007 */ 12008 sa = m << 3; 12009 sv = m << 1; 12010 } 12011 if (sv < TCP_SD_MIN) { 12012 /* 12013 * We do not know that if sa captures the delay ACK 12014 * effect as in a long train of segments, a receiver 12015 * does not delay its ACKs. So set the minimum of sv 12016 * to be TCP_SD_MIN, which is default to 400 ms, twice 12017 * of BSD DATO. That means the minimum of mean 12018 * deviation is 100 ms. 12019 * 12020 */ 12021 sv = TCP_SD_MIN; 12022 } 12023 tcp->tcp_rtt_sa = sa; 12024 tcp->tcp_rtt_sd = sv; 12025 /* 12026 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12027 * 12028 * Add tcp_rexmit_interval extra in case of extreme environment 12029 * where the algorithm fails to work. The default value of 12030 * tcp_rexmit_interval_extra should be 0. 12031 * 12032 * As we use a finer grained clock than BSD and update 12033 * RTO for every ACKs, add in another .25 of RTT to the 12034 * deviation of RTO to accomodate burstiness of 1/4 of 12035 * window size. 12036 */ 12037 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12038 12039 if (rto > tcps->tcps_rexmit_interval_max) { 12040 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12041 } else if (rto < tcps->tcps_rexmit_interval_min) { 12042 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12043 } else { 12044 tcp->tcp_rto = rto; 12045 } 12046 12047 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12048 tcp->tcp_timer_backoff = 0; 12049 } 12050 12051 /* 12052 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12053 * send queue which starts at the given seq. no. 12054 * 12055 * Parameters: 12056 * tcp_t *tcp: the tcp instance pointer. 12057 * uint32_t seq: the starting seq. no of the requested segment. 12058 * int32_t *off: after the execution, *off will be the offset to 12059 * the returned mblk which points to the requested seq no. 12060 * It is the caller's responsibility to send in a non-null off. 12061 * 12062 * Return: 12063 * A mblk_t pointer pointing to the requested segment in send queue. 12064 */ 12065 static mblk_t * 12066 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12067 { 12068 int32_t cnt; 12069 mblk_t *mp; 12070 12071 /* Defensive coding. Make sure we don't send incorrect data. */ 12072 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12073 return (NULL); 12074 12075 cnt = seq - tcp->tcp_suna; 12076 mp = tcp->tcp_xmit_head; 12077 while (cnt > 0 && mp != NULL) { 12078 cnt -= mp->b_wptr - mp->b_rptr; 12079 if (cnt < 0) { 12080 cnt += mp->b_wptr - mp->b_rptr; 12081 break; 12082 } 12083 mp = mp->b_cont; 12084 } 12085 ASSERT(mp != NULL); 12086 *off = cnt; 12087 return (mp); 12088 } 12089 12090 /* 12091 * This function handles all retransmissions if SACK is enabled for this 12092 * connection. First it calculates how many segments can be retransmitted 12093 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12094 * segments. A segment is eligible if sack_cnt for that segment is greater 12095 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12096 * all eligible segments, it checks to see if TCP can send some new segments 12097 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12098 * 12099 * Parameters: 12100 * tcp_t *tcp: the tcp structure of the connection. 12101 * uint_t *flags: in return, appropriate value will be set for 12102 * tcp_rput_data(). 12103 */ 12104 static void 12105 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12106 { 12107 notsack_blk_t *notsack_blk; 12108 int32_t usable_swnd; 12109 int32_t mss; 12110 uint32_t seg_len; 12111 mblk_t *xmit_mp; 12112 tcp_stack_t *tcps = tcp->tcp_tcps; 12113 12114 ASSERT(tcp->tcp_sack_info != NULL); 12115 ASSERT(tcp->tcp_notsack_list != NULL); 12116 ASSERT(tcp->tcp_rexmit == B_FALSE); 12117 12118 /* Defensive coding in case there is a bug... */ 12119 if (tcp->tcp_notsack_list == NULL) { 12120 return; 12121 } 12122 notsack_blk = tcp->tcp_notsack_list; 12123 mss = tcp->tcp_mss; 12124 12125 /* 12126 * Limit the num of outstanding data in the network to be 12127 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12128 */ 12129 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12130 12131 /* At least retransmit 1 MSS of data. */ 12132 if (usable_swnd <= 0) { 12133 usable_swnd = mss; 12134 } 12135 12136 /* Make sure no new RTT samples will be taken. */ 12137 tcp->tcp_csuna = tcp->tcp_snxt; 12138 12139 notsack_blk = tcp->tcp_notsack_list; 12140 while (usable_swnd > 0) { 12141 mblk_t *snxt_mp, *tmp_mp; 12142 tcp_seq begin = tcp->tcp_sack_snxt; 12143 tcp_seq end; 12144 int32_t off; 12145 12146 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12147 if (SEQ_GT(notsack_blk->end, begin) && 12148 (notsack_blk->sack_cnt >= 12149 tcps->tcps_dupack_fast_retransmit)) { 12150 end = notsack_blk->end; 12151 if (SEQ_LT(begin, notsack_blk->begin)) { 12152 begin = notsack_blk->begin; 12153 } 12154 break; 12155 } 12156 } 12157 /* 12158 * All holes are filled. Manipulate tcp_cwnd to send more 12159 * if we can. Note that after the SACK recovery, tcp_cwnd is 12160 * set to tcp_cwnd_ssthresh. 12161 */ 12162 if (notsack_blk == NULL) { 12163 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12164 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12165 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12166 ASSERT(tcp->tcp_cwnd > 0); 12167 return; 12168 } else { 12169 usable_swnd = usable_swnd / mss; 12170 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12171 MAX(usable_swnd * mss, mss); 12172 *flags |= TH_XMIT_NEEDED; 12173 return; 12174 } 12175 } 12176 12177 /* 12178 * Note that we may send more than usable_swnd allows here 12179 * because of round off, but no more than 1 MSS of data. 12180 */ 12181 seg_len = end - begin; 12182 if (seg_len > mss) 12183 seg_len = mss; 12184 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12185 ASSERT(snxt_mp != NULL); 12186 /* This should not happen. Defensive coding again... */ 12187 if (snxt_mp == NULL) { 12188 return; 12189 } 12190 12191 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12192 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12193 if (xmit_mp == NULL) 12194 return; 12195 12196 usable_swnd -= seg_len; 12197 tcp->tcp_pipe += seg_len; 12198 tcp->tcp_sack_snxt = begin + seg_len; 12199 12200 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12201 12202 /* 12203 * Update the send timestamp to avoid false retransmission. 12204 */ 12205 snxt_mp->b_prev = (mblk_t *)lbolt; 12206 12207 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12208 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12209 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12210 /* 12211 * Update tcp_rexmit_max to extend this SACK recovery phase. 12212 * This happens when new data sent during fast recovery is 12213 * also lost. If TCP retransmits those new data, it needs 12214 * to extend SACK recover phase to avoid starting another 12215 * fast retransmit/recovery unnecessarily. 12216 */ 12217 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12218 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12219 } 12220 } 12221 } 12222 12223 /* 12224 * This function handles policy checking at TCP level for non-hard_bound/ 12225 * detached connections. 12226 */ 12227 static boolean_t 12228 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12229 boolean_t secure, boolean_t mctl_present) 12230 { 12231 ipsec_latch_t *ipl = NULL; 12232 ipsec_action_t *act = NULL; 12233 mblk_t *data_mp; 12234 ipsec_in_t *ii; 12235 const char *reason; 12236 kstat_named_t *counter; 12237 tcp_stack_t *tcps = tcp->tcp_tcps; 12238 ipsec_stack_t *ipss; 12239 ip_stack_t *ipst; 12240 12241 ASSERT(mctl_present || !secure); 12242 12243 ASSERT((ipha == NULL && ip6h != NULL) || 12244 (ip6h == NULL && ipha != NULL)); 12245 12246 /* 12247 * We don't necessarily have an ipsec_in_act action to verify 12248 * policy because of assymetrical policy where we have only 12249 * outbound policy and no inbound policy (possible with global 12250 * policy). 12251 */ 12252 if (!secure) { 12253 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12254 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12255 return (B_TRUE); 12256 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12257 "tcp_check_policy", ipha, ip6h, secure, 12258 tcps->tcps_netstack); 12259 ipss = tcps->tcps_netstack->netstack_ipsec; 12260 12261 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12262 DROPPER(ipss, ipds_tcp_clear), 12263 &tcps->tcps_dropper); 12264 return (B_FALSE); 12265 } 12266 12267 /* 12268 * We have a secure packet. 12269 */ 12270 if (act == NULL) { 12271 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12272 "tcp_check_policy", ipha, ip6h, secure, 12273 tcps->tcps_netstack); 12274 ipss = tcps->tcps_netstack->netstack_ipsec; 12275 12276 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12277 DROPPER(ipss, ipds_tcp_secure), 12278 &tcps->tcps_dropper); 12279 return (B_FALSE); 12280 } 12281 12282 /* 12283 * XXX This whole routine is currently incorrect. ipl should 12284 * be set to the latch pointer, but is currently not set, so 12285 * we initialize it to NULL to avoid picking up random garbage. 12286 */ 12287 if (ipl == NULL) 12288 return (B_TRUE); 12289 12290 data_mp = first_mp->b_cont; 12291 12292 ii = (ipsec_in_t *)first_mp->b_rptr; 12293 12294 ipst = tcps->tcps_netstack->netstack_ip; 12295 12296 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12297 &counter, tcp->tcp_connp)) { 12298 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12299 return (B_TRUE); 12300 } 12301 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12302 "tcp inbound policy mismatch: %s, packet dropped\n", 12303 reason); 12304 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12305 12306 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12307 &tcps->tcps_dropper); 12308 return (B_FALSE); 12309 } 12310 12311 /* 12312 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12313 * retransmission after a timeout. 12314 * 12315 * To limit the number of duplicate segments, we limit the number of segment 12316 * to be sent in one time to tcp_snd_burst, the burst variable. 12317 */ 12318 static void 12319 tcp_ss_rexmit(tcp_t *tcp) 12320 { 12321 uint32_t snxt; 12322 uint32_t smax; 12323 int32_t win; 12324 int32_t mss; 12325 int32_t off; 12326 int32_t burst = tcp->tcp_snd_burst; 12327 mblk_t *snxt_mp; 12328 tcp_stack_t *tcps = tcp->tcp_tcps; 12329 12330 /* 12331 * Note that tcp_rexmit can be set even though TCP has retransmitted 12332 * all unack'ed segments. 12333 */ 12334 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12335 smax = tcp->tcp_rexmit_max; 12336 snxt = tcp->tcp_rexmit_nxt; 12337 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12338 snxt = tcp->tcp_suna; 12339 } 12340 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12341 win -= snxt - tcp->tcp_suna; 12342 mss = tcp->tcp_mss; 12343 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12344 12345 while (SEQ_LT(snxt, smax) && (win > 0) && 12346 (burst > 0) && (snxt_mp != NULL)) { 12347 mblk_t *xmit_mp; 12348 mblk_t *old_snxt_mp = snxt_mp; 12349 uint32_t cnt = mss; 12350 12351 if (win < cnt) { 12352 cnt = win; 12353 } 12354 if (SEQ_GT(snxt + cnt, smax)) { 12355 cnt = smax - snxt; 12356 } 12357 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12358 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12359 if (xmit_mp == NULL) 12360 return; 12361 12362 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12363 12364 snxt += cnt; 12365 win -= cnt; 12366 /* 12367 * Update the send timestamp to avoid false 12368 * retransmission. 12369 */ 12370 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12371 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12372 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12373 12374 tcp->tcp_rexmit_nxt = snxt; 12375 burst--; 12376 } 12377 /* 12378 * If we have transmitted all we have at the time 12379 * we started the retranmission, we can leave 12380 * the rest of the job to tcp_wput_data(). But we 12381 * need to check the send window first. If the 12382 * win is not 0, go on with tcp_wput_data(). 12383 */ 12384 if (SEQ_LT(snxt, smax) || win == 0) { 12385 return; 12386 } 12387 } 12388 /* Only call tcp_wput_data() if there is data to be sent. */ 12389 if (tcp->tcp_unsent) { 12390 tcp_wput_data(tcp, NULL, B_FALSE); 12391 } 12392 } 12393 12394 /* 12395 * Process all TCP option in SYN segment. Note that this function should 12396 * be called after tcp_adapt_ire() is called so that the necessary info 12397 * from IRE is already set in the tcp structure. 12398 * 12399 * This function sets up the correct tcp_mss value according to the 12400 * MSS option value and our header size. It also sets up the window scale 12401 * and timestamp values, and initialize SACK info blocks. But it does not 12402 * change receive window size after setting the tcp_mss value. The caller 12403 * should do the appropriate change. 12404 */ 12405 void 12406 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12407 { 12408 int options; 12409 tcp_opt_t tcpopt; 12410 uint32_t mss_max; 12411 char *tmp_tcph; 12412 tcp_stack_t *tcps = tcp->tcp_tcps; 12413 12414 tcpopt.tcp = NULL; 12415 options = tcp_parse_options(tcph, &tcpopt); 12416 12417 /* 12418 * Process MSS option. Note that MSS option value does not account 12419 * for IP or TCP options. This means that it is equal to MTU - minimum 12420 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12421 * IPv6. 12422 */ 12423 if (!(options & TCP_OPT_MSS_PRESENT)) { 12424 if (tcp->tcp_ipversion == IPV4_VERSION) 12425 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12426 else 12427 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12428 } else { 12429 if (tcp->tcp_ipversion == IPV4_VERSION) 12430 mss_max = tcps->tcps_mss_max_ipv4; 12431 else 12432 mss_max = tcps->tcps_mss_max_ipv6; 12433 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12434 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12435 else if (tcpopt.tcp_opt_mss > mss_max) 12436 tcpopt.tcp_opt_mss = mss_max; 12437 } 12438 12439 /* Process Window Scale option. */ 12440 if (options & TCP_OPT_WSCALE_PRESENT) { 12441 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12442 tcp->tcp_snd_ws_ok = B_TRUE; 12443 } else { 12444 tcp->tcp_snd_ws = B_FALSE; 12445 tcp->tcp_snd_ws_ok = B_FALSE; 12446 tcp->tcp_rcv_ws = B_FALSE; 12447 } 12448 12449 /* Process Timestamp option. */ 12450 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12451 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12452 tmp_tcph = (char *)tcp->tcp_tcph; 12453 12454 tcp->tcp_snd_ts_ok = B_TRUE; 12455 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12456 tcp->tcp_last_rcv_lbolt = lbolt64; 12457 ASSERT(OK_32PTR(tmp_tcph)); 12458 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12459 12460 /* Fill in our template header with basic timestamp option. */ 12461 tmp_tcph += tcp->tcp_tcp_hdr_len; 12462 tmp_tcph[0] = TCPOPT_NOP; 12463 tmp_tcph[1] = TCPOPT_NOP; 12464 tmp_tcph[2] = TCPOPT_TSTAMP; 12465 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12466 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12467 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12468 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12469 } else { 12470 tcp->tcp_snd_ts_ok = B_FALSE; 12471 } 12472 12473 /* 12474 * Process SACK options. If SACK is enabled for this connection, 12475 * then allocate the SACK info structure. Note the following ways 12476 * when tcp_snd_sack_ok is set to true. 12477 * 12478 * For active connection: in tcp_adapt_ire() called in 12479 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12480 * is checked. 12481 * 12482 * For passive connection: in tcp_adapt_ire() called in 12483 * tcp_accept_comm(). 12484 * 12485 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12486 * That check makes sure that if we did not send a SACK OK option, 12487 * we will not enable SACK for this connection even though the other 12488 * side sends us SACK OK option. For active connection, the SACK 12489 * info structure has already been allocated. So we need to free 12490 * it if SACK is disabled. 12491 */ 12492 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12493 (tcp->tcp_snd_sack_ok || 12494 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12495 /* This should be true only in the passive case. */ 12496 if (tcp->tcp_sack_info == NULL) { 12497 ASSERT(TCP_IS_DETACHED(tcp)); 12498 tcp->tcp_sack_info = 12499 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12500 } 12501 if (tcp->tcp_sack_info == NULL) { 12502 tcp->tcp_snd_sack_ok = B_FALSE; 12503 } else { 12504 tcp->tcp_snd_sack_ok = B_TRUE; 12505 if (tcp->tcp_snd_ts_ok) { 12506 tcp->tcp_max_sack_blk = 3; 12507 } else { 12508 tcp->tcp_max_sack_blk = 4; 12509 } 12510 } 12511 } else { 12512 /* 12513 * Resetting tcp_snd_sack_ok to B_FALSE so that 12514 * no SACK info will be used for this 12515 * connection. This assumes that SACK usage 12516 * permission is negotiated. This may need 12517 * to be changed once this is clarified. 12518 */ 12519 if (tcp->tcp_sack_info != NULL) { 12520 ASSERT(tcp->tcp_notsack_list == NULL); 12521 kmem_cache_free(tcp_sack_info_cache, 12522 tcp->tcp_sack_info); 12523 tcp->tcp_sack_info = NULL; 12524 } 12525 tcp->tcp_snd_sack_ok = B_FALSE; 12526 } 12527 12528 /* 12529 * Now we know the exact TCP/IP header length, subtract 12530 * that from tcp_mss to get our side's MSS. 12531 */ 12532 tcp->tcp_mss -= tcp->tcp_hdr_len; 12533 /* 12534 * Here we assume that the other side's header size will be equal to 12535 * our header size. We calculate the real MSS accordingly. Need to 12536 * take into additional stuffs IPsec puts in. 12537 * 12538 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12539 */ 12540 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12541 ((tcp->tcp_ipversion == IPV4_VERSION ? 12542 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12543 12544 /* 12545 * Set MSS to the smaller one of both ends of the connection. 12546 * We should not have called tcp_mss_set() before, but our 12547 * side of the MSS should have been set to a proper value 12548 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12549 * STREAM head parameters properly. 12550 * 12551 * If we have a larger-than-16-bit window but the other side 12552 * didn't want to do window scale, tcp_rwnd_set() will take 12553 * care of that. 12554 */ 12555 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12556 } 12557 12558 /* 12559 * Sends the T_CONN_IND to the listener. The caller calls this 12560 * functions via squeue to get inside the listener's perimeter 12561 * once the 3 way hand shake is done a T_CONN_IND needs to be 12562 * sent. As an optimization, the caller can call this directly 12563 * if listener's perimeter is same as eager's. 12564 */ 12565 /* ARGSUSED */ 12566 void 12567 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12568 { 12569 conn_t *lconnp = (conn_t *)arg; 12570 tcp_t *listener = lconnp->conn_tcp; 12571 tcp_t *tcp; 12572 struct T_conn_ind *conn_ind; 12573 ipaddr_t *addr_cache; 12574 boolean_t need_send_conn_ind = B_FALSE; 12575 tcp_stack_t *tcps = listener->tcp_tcps; 12576 12577 /* retrieve the eager */ 12578 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12579 ASSERT(conn_ind->OPT_offset != 0 && 12580 conn_ind->OPT_length == sizeof (intptr_t)); 12581 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12582 conn_ind->OPT_length); 12583 12584 /* 12585 * TLI/XTI applications will get confused by 12586 * sending eager as an option since it violates 12587 * the option semantics. So remove the eager as 12588 * option since TLI/XTI app doesn't need it anyway. 12589 */ 12590 if (!TCP_IS_SOCKET(listener)) { 12591 conn_ind->OPT_length = 0; 12592 conn_ind->OPT_offset = 0; 12593 } 12594 if (listener->tcp_state == TCPS_CLOSED || 12595 TCP_IS_DETACHED(listener)) { 12596 /* 12597 * If listener has closed, it would have caused a 12598 * a cleanup/blowoff to happen for the eager. We 12599 * just need to return. 12600 */ 12601 freemsg(mp); 12602 return; 12603 } 12604 12605 12606 /* 12607 * if the conn_req_q is full defer passing up the 12608 * T_CONN_IND until space is availabe after t_accept() 12609 * processing 12610 */ 12611 mutex_enter(&listener->tcp_eager_lock); 12612 12613 /* 12614 * Take the eager out, if it is in the list of droppable eagers 12615 * as we are here because the 3W handshake is over. 12616 */ 12617 MAKE_UNDROPPABLE(tcp); 12618 12619 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12620 tcp_t *tail; 12621 12622 /* 12623 * The eager already has an extra ref put in tcp_rput_data 12624 * so that it stays till accept comes back even though it 12625 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12626 */ 12627 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12628 listener->tcp_conn_req_cnt_q0--; 12629 listener->tcp_conn_req_cnt_q++; 12630 12631 /* Move from SYN_RCVD to ESTABLISHED list */ 12632 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12633 tcp->tcp_eager_prev_q0; 12634 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12635 tcp->tcp_eager_next_q0; 12636 tcp->tcp_eager_prev_q0 = NULL; 12637 tcp->tcp_eager_next_q0 = NULL; 12638 12639 /* 12640 * Insert at end of the queue because sockfs 12641 * sends down T_CONN_RES in chronological 12642 * order. Leaving the older conn indications 12643 * at front of the queue helps reducing search 12644 * time. 12645 */ 12646 tail = listener->tcp_eager_last_q; 12647 if (tail != NULL) 12648 tail->tcp_eager_next_q = tcp; 12649 else 12650 listener->tcp_eager_next_q = tcp; 12651 listener->tcp_eager_last_q = tcp; 12652 tcp->tcp_eager_next_q = NULL; 12653 /* 12654 * Delay sending up the T_conn_ind until we are 12655 * done with the eager. Once we have have sent up 12656 * the T_conn_ind, the accept can potentially complete 12657 * any time and release the refhold we have on the eager. 12658 */ 12659 need_send_conn_ind = B_TRUE; 12660 } else { 12661 /* 12662 * Defer connection on q0 and set deferred 12663 * connection bit true 12664 */ 12665 tcp->tcp_conn_def_q0 = B_TRUE; 12666 12667 /* take tcp out of q0 ... */ 12668 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12669 tcp->tcp_eager_next_q0; 12670 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12671 tcp->tcp_eager_prev_q0; 12672 12673 /* ... and place it at the end of q0 */ 12674 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12675 tcp->tcp_eager_next_q0 = listener; 12676 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12677 listener->tcp_eager_prev_q0 = tcp; 12678 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12679 } 12680 12681 /* we have timed out before */ 12682 if (tcp->tcp_syn_rcvd_timeout != 0) { 12683 tcp->tcp_syn_rcvd_timeout = 0; 12684 listener->tcp_syn_rcvd_timeout--; 12685 if (listener->tcp_syn_defense && 12686 listener->tcp_syn_rcvd_timeout <= 12687 (tcps->tcps_conn_req_max_q0 >> 5) && 12688 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12689 listener->tcp_last_rcv_lbolt)) { 12690 /* 12691 * Turn off the defense mode if we 12692 * believe the SYN attack is over. 12693 */ 12694 listener->tcp_syn_defense = B_FALSE; 12695 if (listener->tcp_ip_addr_cache) { 12696 kmem_free((void *)listener->tcp_ip_addr_cache, 12697 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12698 listener->tcp_ip_addr_cache = NULL; 12699 } 12700 } 12701 } 12702 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12703 if (addr_cache != NULL) { 12704 /* 12705 * We have finished a 3-way handshake with this 12706 * remote host. This proves the IP addr is good. 12707 * Cache it! 12708 */ 12709 addr_cache[IP_ADDR_CACHE_HASH( 12710 tcp->tcp_remote)] = tcp->tcp_remote; 12711 } 12712 mutex_exit(&listener->tcp_eager_lock); 12713 if (need_send_conn_ind) { 12714 if (IPCL_IS_NONSTR(lconnp)) { 12715 ASSERT(tcp->tcp_listener == listener); 12716 ASSERT(tcp->tcp_saved_listener == listener); 12717 if ((*lconnp->conn_upcalls->su_newconn) 12718 (lconnp->conn_upper_handle, 12719 (sock_lower_handle_t)tcp->tcp_connp, 12720 &sock_tcp_downcalls, DB_CRED(mp), DB_CPID(mp), 12721 &tcp->tcp_connp->conn_upcalls) != NULL) { 12722 /* 12723 * Keep the message around 12724 * in case of fallback 12725 */ 12726 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12727 } else { 12728 freemsg(mp); 12729 } 12730 } else { 12731 putnext(listener->tcp_rq, mp); 12732 } 12733 } 12734 } 12735 12736 mblk_t * 12737 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12738 uint_t *ifindexp, ip6_pkt_t *ippp) 12739 { 12740 ip_pktinfo_t *pinfo; 12741 ip6_t *ip6h; 12742 uchar_t *rptr; 12743 mblk_t *first_mp = mp; 12744 boolean_t mctl_present = B_FALSE; 12745 uint_t ifindex = 0; 12746 ip6_pkt_t ipp; 12747 uint_t ipvers; 12748 uint_t ip_hdr_len; 12749 tcp_stack_t *tcps = tcp->tcp_tcps; 12750 12751 rptr = mp->b_rptr; 12752 ASSERT(OK_32PTR(rptr)); 12753 ASSERT(tcp != NULL); 12754 ipp.ipp_fields = 0; 12755 12756 switch DB_TYPE(mp) { 12757 case M_CTL: 12758 mp = mp->b_cont; 12759 if (mp == NULL) { 12760 freemsg(first_mp); 12761 return (NULL); 12762 } 12763 if (DB_TYPE(mp) != M_DATA) { 12764 freemsg(first_mp); 12765 return (NULL); 12766 } 12767 mctl_present = B_TRUE; 12768 break; 12769 case M_DATA: 12770 break; 12771 default: 12772 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12773 freemsg(mp); 12774 return (NULL); 12775 } 12776 ipvers = IPH_HDR_VERSION(rptr); 12777 if (ipvers == IPV4_VERSION) { 12778 if (tcp == NULL) { 12779 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12780 goto done; 12781 } 12782 12783 ipp.ipp_fields |= IPPF_HOPLIMIT; 12784 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12785 12786 /* 12787 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12788 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12789 */ 12790 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12791 mctl_present) { 12792 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12793 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12794 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12795 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12796 ipp.ipp_fields |= IPPF_IFINDEX; 12797 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12798 ifindex = pinfo->ip_pkt_ifindex; 12799 } 12800 freeb(first_mp); 12801 mctl_present = B_FALSE; 12802 } 12803 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12804 } else { 12805 ip6h = (ip6_t *)rptr; 12806 12807 ASSERT(ipvers == IPV6_VERSION); 12808 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12809 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12810 ipp.ipp_hoplimit = ip6h->ip6_hops; 12811 12812 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12813 uint8_t nexthdrp; 12814 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12815 12816 /* Look for ifindex information */ 12817 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12818 ip6i_t *ip6i = (ip6i_t *)ip6h; 12819 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12820 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12821 freemsg(first_mp); 12822 return (NULL); 12823 } 12824 12825 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12826 ASSERT(ip6i->ip6i_ifindex != 0); 12827 ipp.ipp_fields |= IPPF_IFINDEX; 12828 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12829 ifindex = ip6i->ip6i_ifindex; 12830 } 12831 rptr = (uchar_t *)&ip6i[1]; 12832 mp->b_rptr = rptr; 12833 if (rptr == mp->b_wptr) { 12834 mblk_t *mp1; 12835 mp1 = mp->b_cont; 12836 freeb(mp); 12837 mp = mp1; 12838 rptr = mp->b_rptr; 12839 } 12840 if (MBLKL(mp) < IPV6_HDR_LEN + 12841 sizeof (tcph_t)) { 12842 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12843 freemsg(first_mp); 12844 return (NULL); 12845 } 12846 ip6h = (ip6_t *)rptr; 12847 } 12848 12849 /* 12850 * Find any potentially interesting extension headers 12851 * as well as the length of the IPv6 + extension 12852 * headers. 12853 */ 12854 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12855 /* Verify if this is a TCP packet */ 12856 if (nexthdrp != IPPROTO_TCP) { 12857 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12858 freemsg(first_mp); 12859 return (NULL); 12860 } 12861 } else { 12862 ip_hdr_len = IPV6_HDR_LEN; 12863 } 12864 } 12865 12866 done: 12867 if (ipversp != NULL) 12868 *ipversp = ipvers; 12869 if (ip_hdr_lenp != NULL) 12870 *ip_hdr_lenp = ip_hdr_len; 12871 if (ippp != NULL) 12872 *ippp = ipp; 12873 if (ifindexp != NULL) 12874 *ifindexp = ifindex; 12875 if (mctl_present) { 12876 freeb(first_mp); 12877 } 12878 return (mp); 12879 } 12880 12881 /* 12882 * Handle M_DATA messages from IP. Its called directly from IP via 12883 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12884 * in this path. 12885 * 12886 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12887 * v4 and v6), we are called through tcp_input() and a M_CTL can 12888 * be present for options but tcp_find_pktinfo() deals with it. We 12889 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12890 * 12891 * The first argument is always the connp/tcp to which the mp belongs. 12892 * There are no exceptions to this rule. The caller has already put 12893 * a reference on this connp/tcp and once tcp_rput_data() returns, 12894 * the squeue will do the refrele. 12895 * 12896 * The TH_SYN for the listener directly go to tcp_conn_request via 12897 * squeue. 12898 * 12899 * sqp: NULL = recursive, sqp != NULL means called from squeue 12900 */ 12901 void 12902 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12903 { 12904 int32_t bytes_acked; 12905 int32_t gap; 12906 mblk_t *mp1; 12907 uint_t flags; 12908 uint32_t new_swnd = 0; 12909 uchar_t *iphdr; 12910 uchar_t *rptr; 12911 int32_t rgap; 12912 uint32_t seg_ack; 12913 int seg_len; 12914 uint_t ip_hdr_len; 12915 uint32_t seg_seq; 12916 tcph_t *tcph; 12917 int urp; 12918 tcp_opt_t tcpopt; 12919 uint_t ipvers; 12920 ip6_pkt_t ipp; 12921 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12922 uint32_t cwnd; 12923 uint32_t add; 12924 int npkt; 12925 int mss; 12926 conn_t *connp = (conn_t *)arg; 12927 squeue_t *sqp = (squeue_t *)arg2; 12928 tcp_t *tcp = connp->conn_tcp; 12929 tcp_stack_t *tcps = tcp->tcp_tcps; 12930 12931 /* 12932 * RST from fused tcp loopback peer should trigger an unfuse. 12933 */ 12934 if (tcp->tcp_fused) { 12935 TCP_STAT(tcps, tcp_fusion_aborted); 12936 tcp_unfuse(tcp); 12937 } 12938 12939 iphdr = mp->b_rptr; 12940 rptr = mp->b_rptr; 12941 ASSERT(OK_32PTR(rptr)); 12942 12943 /* 12944 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12945 * processing here. For rest call tcp_find_pktinfo to fill up the 12946 * necessary information. 12947 */ 12948 if (IPCL_IS_TCP4(connp)) { 12949 ipvers = IPV4_VERSION; 12950 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12951 } else { 12952 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12953 NULL, &ipp); 12954 if (mp == NULL) { 12955 TCP_STAT(tcps, tcp_rput_v6_error); 12956 return; 12957 } 12958 iphdr = mp->b_rptr; 12959 rptr = mp->b_rptr; 12960 } 12961 ASSERT(DB_TYPE(mp) == M_DATA); 12962 ASSERT(mp->b_next == NULL); 12963 12964 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12965 seg_seq = ABE32_TO_U32(tcph->th_seq); 12966 seg_ack = ABE32_TO_U32(tcph->th_ack); 12967 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12968 seg_len = (int)(mp->b_wptr - rptr) - 12969 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12970 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12971 do { 12972 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12973 (uintptr_t)INT_MAX); 12974 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12975 } while ((mp1 = mp1->b_cont) != NULL && 12976 mp1->b_datap->db_type == M_DATA); 12977 } 12978 12979 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12980 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12981 seg_len, tcph); 12982 return; 12983 } 12984 12985 if (sqp != NULL) { 12986 /* 12987 * This is the correct place to update tcp_last_recv_time. Note 12988 * that it is also updated for tcp structure that belongs to 12989 * global and listener queues which do not really need updating. 12990 * But that should not cause any harm. And it is updated for 12991 * all kinds of incoming segments, not only for data segments. 12992 */ 12993 tcp->tcp_last_recv_time = lbolt; 12994 } 12995 12996 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12997 12998 BUMP_LOCAL(tcp->tcp_ibsegs); 12999 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13000 13001 if ((flags & TH_URG) && sqp != NULL) { 13002 /* 13003 * TCP can't handle urgent pointers that arrive before 13004 * the connection has been accept()ed since it can't 13005 * buffer OOB data. Discard segment if this happens. 13006 * 13007 * We can't just rely on a non-null tcp_listener to indicate 13008 * that the accept() has completed since unlinking of the 13009 * eager and completion of the accept are not atomic. 13010 * tcp_detached, when it is not set (B_FALSE) indicates 13011 * that the accept() has completed. 13012 * 13013 * Nor can it reassemble urgent pointers, so discard 13014 * if it's not the next segment expected. 13015 * 13016 * Otherwise, collapse chain into one mblk (discard if 13017 * that fails). This makes sure the headers, retransmitted 13018 * data, and new data all are in the same mblk. 13019 */ 13020 ASSERT(mp != NULL); 13021 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13022 freemsg(mp); 13023 return; 13024 } 13025 /* Update pointers into message */ 13026 iphdr = rptr = mp->b_rptr; 13027 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13028 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13029 /* 13030 * Since we can't handle any data with this urgent 13031 * pointer that is out of sequence, we expunge 13032 * the data. This allows us to still register 13033 * the urgent mark and generate the M_PCSIG, 13034 * which we can do. 13035 */ 13036 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13037 seg_len = 0; 13038 } 13039 } 13040 13041 switch (tcp->tcp_state) { 13042 case TCPS_SYN_SENT: 13043 if (flags & TH_ACK) { 13044 /* 13045 * Note that our stack cannot send data before a 13046 * connection is established, therefore the 13047 * following check is valid. Otherwise, it has 13048 * to be changed. 13049 */ 13050 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13051 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13052 freemsg(mp); 13053 if (flags & TH_RST) 13054 return; 13055 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13056 tcp, seg_ack, 0, TH_RST); 13057 return; 13058 } 13059 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13060 } 13061 if (flags & TH_RST) { 13062 freemsg(mp); 13063 if (flags & TH_ACK) 13064 (void) tcp_clean_death(tcp, 13065 ECONNREFUSED, 13); 13066 return; 13067 } 13068 if (!(flags & TH_SYN)) { 13069 freemsg(mp); 13070 return; 13071 } 13072 13073 /* Process all TCP options. */ 13074 tcp_process_options(tcp, tcph); 13075 /* 13076 * The following changes our rwnd to be a multiple of the 13077 * MIN(peer MSS, our MSS) for performance reason. 13078 */ 13079 (void) tcp_rwnd_set(tcp, 13080 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 13081 13082 /* Is the other end ECN capable? */ 13083 if (tcp->tcp_ecn_ok) { 13084 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13085 tcp->tcp_ecn_ok = B_FALSE; 13086 } 13087 } 13088 /* 13089 * Clear ECN flags because it may interfere with later 13090 * processing. 13091 */ 13092 flags &= ~(TH_ECE|TH_CWR); 13093 13094 tcp->tcp_irs = seg_seq; 13095 tcp->tcp_rack = seg_seq; 13096 tcp->tcp_rnxt = seg_seq + 1; 13097 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13098 if (!TCP_IS_DETACHED(tcp)) { 13099 /* Allocate room for SACK options if needed. */ 13100 if (tcp->tcp_snd_sack_ok) { 13101 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13102 tcp->tcp_hdr_len + 13103 TCPOPT_MAX_SACK_LEN + 13104 (tcp->tcp_loopback ? 0 : 13105 tcps->tcps_wroff_xtra)); 13106 } else { 13107 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13108 tcp->tcp_hdr_len + 13109 (tcp->tcp_loopback ? 0 : 13110 tcps->tcps_wroff_xtra)); 13111 } 13112 } 13113 if (flags & TH_ACK) { 13114 /* 13115 * If we can't get the confirmation upstream, pretend 13116 * we didn't even see this one. 13117 * 13118 * XXX: how can we pretend we didn't see it if we 13119 * have updated rnxt et. al. 13120 * 13121 * For loopback we defer sending up the T_CONN_CON 13122 * until after some checks below. 13123 */ 13124 mp1 = NULL; 13125 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13126 tcp->tcp_loopback ? &mp1 : NULL)) { 13127 freemsg(mp); 13128 return; 13129 } 13130 /* SYN was acked - making progress */ 13131 if (tcp->tcp_ipversion == IPV6_VERSION) 13132 tcp->tcp_ip_forward_progress = B_TRUE; 13133 13134 /* One for the SYN */ 13135 tcp->tcp_suna = tcp->tcp_iss + 1; 13136 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13137 tcp->tcp_state = TCPS_ESTABLISHED; 13138 13139 /* 13140 * If SYN was retransmitted, need to reset all 13141 * retransmission info. This is because this 13142 * segment will be treated as a dup ACK. 13143 */ 13144 if (tcp->tcp_rexmit) { 13145 tcp->tcp_rexmit = B_FALSE; 13146 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13147 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13148 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13149 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13150 tcp->tcp_ms_we_have_waited = 0; 13151 13152 /* 13153 * Set tcp_cwnd back to 1 MSS, per 13154 * recommendation from 13155 * draft-floyd-incr-init-win-01.txt, 13156 * Increasing TCP's Initial Window. 13157 */ 13158 tcp->tcp_cwnd = tcp->tcp_mss; 13159 } 13160 13161 tcp->tcp_swl1 = seg_seq; 13162 tcp->tcp_swl2 = seg_ack; 13163 13164 new_swnd = BE16_TO_U16(tcph->th_win); 13165 tcp->tcp_swnd = new_swnd; 13166 if (new_swnd > tcp->tcp_max_swnd) 13167 tcp->tcp_max_swnd = new_swnd; 13168 13169 /* 13170 * Always send the three-way handshake ack immediately 13171 * in order to make the connection complete as soon as 13172 * possible on the accepting host. 13173 */ 13174 flags |= TH_ACK_NEEDED; 13175 13176 /* 13177 * Special case for loopback. At this point we have 13178 * received SYN-ACK from the remote endpoint. In 13179 * order to ensure that both endpoints reach the 13180 * fused state prior to any data exchange, the final 13181 * ACK needs to be sent before we indicate T_CONN_CON 13182 * to the module upstream. 13183 */ 13184 if (tcp->tcp_loopback) { 13185 mblk_t *ack_mp; 13186 13187 ASSERT(!tcp->tcp_unfusable); 13188 ASSERT(mp1 != NULL); 13189 /* 13190 * For loopback, we always get a pure SYN-ACK 13191 * and only need to send back the final ACK 13192 * with no data (this is because the other 13193 * tcp is ours and we don't do T/TCP). This 13194 * final ACK triggers the passive side to 13195 * perform fusion in ESTABLISHED state. 13196 */ 13197 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13198 if (tcp->tcp_ack_tid != 0) { 13199 (void) TCP_TIMER_CANCEL(tcp, 13200 tcp->tcp_ack_tid); 13201 tcp->tcp_ack_tid = 0; 13202 } 13203 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13204 BUMP_LOCAL(tcp->tcp_obsegs); 13205 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13206 13207 if (!IPCL_IS_NONSTR(connp)) { 13208 /* Send up T_CONN_CON */ 13209 putnext(tcp->tcp_rq, mp1); 13210 } else { 13211 (*connp->conn_upcalls-> 13212 su_connected) 13213 (connp->conn_upper_handle, 13214 tcp->tcp_connid, 13215 DB_CRED(mp1), 13216 DB_CPID(mp1)); 13217 freemsg(mp1); 13218 } 13219 13220 freemsg(mp); 13221 return; 13222 } 13223 /* 13224 * Forget fusion; we need to handle more 13225 * complex cases below. Send the deferred 13226 * T_CONN_CON message upstream and proceed 13227 * as usual. Mark this tcp as not capable 13228 * of fusion. 13229 */ 13230 TCP_STAT(tcps, tcp_fusion_unfusable); 13231 tcp->tcp_unfusable = B_TRUE; 13232 if (!IPCL_IS_NONSTR(connp)) { 13233 putnext(tcp->tcp_rq, mp1); 13234 } else { 13235 (*connp->conn_upcalls->su_connected) 13236 (connp->conn_upper_handle, 13237 tcp->tcp_connid, DB_CRED(mp1), 13238 DB_CPID(mp1)); 13239 freemsg(mp1); 13240 } 13241 } 13242 13243 /* 13244 * Check to see if there is data to be sent. If 13245 * yes, set the transmit flag. Then check to see 13246 * if received data processing needs to be done. 13247 * If not, go straight to xmit_check. This short 13248 * cut is OK as we don't support T/TCP. 13249 */ 13250 if (tcp->tcp_unsent) 13251 flags |= TH_XMIT_NEEDED; 13252 13253 if (seg_len == 0 && !(flags & TH_URG)) { 13254 freemsg(mp); 13255 goto xmit_check; 13256 } 13257 13258 flags &= ~TH_SYN; 13259 seg_seq++; 13260 break; 13261 } 13262 tcp->tcp_state = TCPS_SYN_RCVD; 13263 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13264 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13265 if (mp1) { 13266 DB_CPID(mp1) = tcp->tcp_cpid; 13267 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13268 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13269 } 13270 freemsg(mp); 13271 return; 13272 case TCPS_SYN_RCVD: 13273 if (flags & TH_ACK) { 13274 /* 13275 * In this state, a SYN|ACK packet is either bogus 13276 * because the other side must be ACKing our SYN which 13277 * indicates it has seen the ACK for their SYN and 13278 * shouldn't retransmit it or we're crossing SYNs 13279 * on active open. 13280 */ 13281 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13282 freemsg(mp); 13283 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13284 tcp, seg_ack, 0, TH_RST); 13285 return; 13286 } 13287 /* 13288 * NOTE: RFC 793 pg. 72 says this should be 13289 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13290 * but that would mean we have an ack that ignored 13291 * our SYN. 13292 */ 13293 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13294 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13295 freemsg(mp); 13296 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13297 tcp, seg_ack, 0, TH_RST); 13298 return; 13299 } 13300 } 13301 break; 13302 case TCPS_LISTEN: 13303 /* 13304 * Only a TLI listener can come through this path when a 13305 * acceptor is going back to be a listener and a packet 13306 * for the acceptor hits the classifier. For a socket 13307 * listener, this can never happen because a listener 13308 * can never accept connection on itself and hence a 13309 * socket acceptor can not go back to being a listener. 13310 */ 13311 ASSERT(!TCP_IS_SOCKET(tcp)); 13312 /*FALLTHRU*/ 13313 case TCPS_CLOSED: 13314 case TCPS_BOUND: { 13315 conn_t *new_connp; 13316 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13317 13318 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13319 if (new_connp != NULL) { 13320 tcp_reinput(new_connp, mp, connp->conn_sqp); 13321 return; 13322 } 13323 /* We failed to classify. For now just drop the packet */ 13324 freemsg(mp); 13325 return; 13326 } 13327 case TCPS_IDLE: 13328 /* 13329 * Handle the case where the tcp_clean_death() has happened 13330 * on a connection (application hasn't closed yet) but a packet 13331 * was already queued on squeue before tcp_clean_death() 13332 * was processed. Calling tcp_clean_death() twice on same 13333 * connection can result in weird behaviour. 13334 */ 13335 freemsg(mp); 13336 return; 13337 default: 13338 break; 13339 } 13340 13341 /* 13342 * Already on the correct queue/perimeter. 13343 * If this is a detached connection and not an eager 13344 * connection hanging off a listener then new data 13345 * (past the FIN) will cause a reset. 13346 * We do a special check here where it 13347 * is out of the main line, rather than check 13348 * if we are detached every time we see new 13349 * data down below. 13350 */ 13351 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13352 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13353 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13354 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13355 13356 freemsg(mp); 13357 /* 13358 * This could be an SSL closure alert. We're detached so just 13359 * acknowledge it this last time. 13360 */ 13361 if (tcp->tcp_kssl_ctx != NULL) { 13362 kssl_release_ctx(tcp->tcp_kssl_ctx); 13363 tcp->tcp_kssl_ctx = NULL; 13364 13365 tcp->tcp_rnxt += seg_len; 13366 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13367 flags |= TH_ACK_NEEDED; 13368 goto ack_check; 13369 } 13370 13371 tcp_xmit_ctl("new data when detached", tcp, 13372 tcp->tcp_snxt, 0, TH_RST); 13373 (void) tcp_clean_death(tcp, EPROTO, 12); 13374 return; 13375 } 13376 13377 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13378 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13379 new_swnd = BE16_TO_U16(tcph->th_win) << 13380 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13381 13382 if (tcp->tcp_snd_ts_ok) { 13383 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13384 /* 13385 * This segment is not acceptable. 13386 * Drop it and send back an ACK. 13387 */ 13388 freemsg(mp); 13389 flags |= TH_ACK_NEEDED; 13390 goto ack_check; 13391 } 13392 } else if (tcp->tcp_snd_sack_ok) { 13393 ASSERT(tcp->tcp_sack_info != NULL); 13394 tcpopt.tcp = tcp; 13395 /* 13396 * SACK info in already updated in tcp_parse_options. Ignore 13397 * all other TCP options... 13398 */ 13399 (void) tcp_parse_options(tcph, &tcpopt); 13400 } 13401 try_again:; 13402 mss = tcp->tcp_mss; 13403 gap = seg_seq - tcp->tcp_rnxt; 13404 rgap = tcp->tcp_rwnd - (gap + seg_len); 13405 /* 13406 * gap is the amount of sequence space between what we expect to see 13407 * and what we got for seg_seq. A positive value for gap means 13408 * something got lost. A negative value means we got some old stuff. 13409 */ 13410 if (gap < 0) { 13411 /* Old stuff present. Is the SYN in there? */ 13412 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13413 (seg_len != 0)) { 13414 flags &= ~TH_SYN; 13415 seg_seq++; 13416 urp--; 13417 /* Recompute the gaps after noting the SYN. */ 13418 goto try_again; 13419 } 13420 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13421 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13422 (seg_len > -gap ? -gap : seg_len)); 13423 /* Remove the old stuff from seg_len. */ 13424 seg_len += gap; 13425 /* 13426 * Anything left? 13427 * Make sure to check for unack'd FIN when rest of data 13428 * has been previously ack'd. 13429 */ 13430 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13431 /* 13432 * Resets are only valid if they lie within our offered 13433 * window. If the RST bit is set, we just ignore this 13434 * segment. 13435 */ 13436 if (flags & TH_RST) { 13437 freemsg(mp); 13438 return; 13439 } 13440 13441 /* 13442 * The arriving of dup data packets indicate that we 13443 * may have postponed an ack for too long, or the other 13444 * side's RTT estimate is out of shape. Start acking 13445 * more often. 13446 */ 13447 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13448 tcp->tcp_rack_cnt >= 1 && 13449 tcp->tcp_rack_abs_max > 2) { 13450 tcp->tcp_rack_abs_max--; 13451 } 13452 tcp->tcp_rack_cur_max = 1; 13453 13454 /* 13455 * This segment is "unacceptable". None of its 13456 * sequence space lies within our advertized window. 13457 * 13458 * Adjust seg_len to the original value for tracing. 13459 */ 13460 seg_len -= gap; 13461 if (tcp->tcp_debug) { 13462 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13463 "tcp_rput: unacceptable, gap %d, rgap %d, " 13464 "flags 0x%x, seg_seq %u, seg_ack %u, " 13465 "seg_len %d, rnxt %u, snxt %u, %s", 13466 gap, rgap, flags, seg_seq, seg_ack, 13467 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13468 tcp_display(tcp, NULL, 13469 DISP_ADDR_AND_PORT)); 13470 } 13471 13472 /* 13473 * Arrange to send an ACK in response to the 13474 * unacceptable segment per RFC 793 page 69. There 13475 * is only one small difference between ours and the 13476 * acceptability test in the RFC - we accept ACK-only 13477 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13478 * will be generated. 13479 * 13480 * Note that we have to ACK an ACK-only packet at least 13481 * for stacks that send 0-length keep-alives with 13482 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13483 * section 4.2.3.6. As long as we don't ever generate 13484 * an unacceptable packet in response to an incoming 13485 * packet that is unacceptable, it should not cause 13486 * "ACK wars". 13487 */ 13488 flags |= TH_ACK_NEEDED; 13489 13490 /* 13491 * Continue processing this segment in order to use the 13492 * ACK information it contains, but skip all other 13493 * sequence-number processing. Processing the ACK 13494 * information is necessary in order to 13495 * re-synchronize connections that may have lost 13496 * synchronization. 13497 * 13498 * We clear seg_len and flag fields related to 13499 * sequence number processing as they are not 13500 * to be trusted for an unacceptable segment. 13501 */ 13502 seg_len = 0; 13503 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13504 goto process_ack; 13505 } 13506 13507 /* Fix seg_seq, and chew the gap off the front. */ 13508 seg_seq = tcp->tcp_rnxt; 13509 urp += gap; 13510 do { 13511 mblk_t *mp2; 13512 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13513 (uintptr_t)UINT_MAX); 13514 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13515 if (gap > 0) { 13516 mp->b_rptr = mp->b_wptr - gap; 13517 break; 13518 } 13519 mp2 = mp; 13520 mp = mp->b_cont; 13521 freeb(mp2); 13522 } while (gap < 0); 13523 /* 13524 * If the urgent data has already been acknowledged, we 13525 * should ignore TH_URG below 13526 */ 13527 if (urp < 0) 13528 flags &= ~TH_URG; 13529 } 13530 /* 13531 * rgap is the amount of stuff received out of window. A negative 13532 * value is the amount out of window. 13533 */ 13534 if (rgap < 0) { 13535 mblk_t *mp2; 13536 13537 if (tcp->tcp_rwnd == 0) { 13538 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13539 } else { 13540 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13541 UPDATE_MIB(&tcps->tcps_mib, 13542 tcpInDataPastWinBytes, -rgap); 13543 } 13544 13545 /* 13546 * seg_len does not include the FIN, so if more than 13547 * just the FIN is out of window, we act like we don't 13548 * see it. (If just the FIN is out of window, rgap 13549 * will be zero and we will go ahead and acknowledge 13550 * the FIN.) 13551 */ 13552 flags &= ~TH_FIN; 13553 13554 /* Fix seg_len and make sure there is something left. */ 13555 seg_len += rgap; 13556 if (seg_len <= 0) { 13557 /* 13558 * Resets are only valid if they lie within our offered 13559 * window. If the RST bit is set, we just ignore this 13560 * segment. 13561 */ 13562 if (flags & TH_RST) { 13563 freemsg(mp); 13564 return; 13565 } 13566 13567 /* Per RFC 793, we need to send back an ACK. */ 13568 flags |= TH_ACK_NEEDED; 13569 13570 /* 13571 * Send SIGURG as soon as possible i.e. even 13572 * if the TH_URG was delivered in a window probe 13573 * packet (which will be unacceptable). 13574 * 13575 * We generate a signal if none has been generated 13576 * for this connection or if this is a new urgent 13577 * byte. Also send a zero-length "unmarked" message 13578 * to inform SIOCATMARK that this is not the mark. 13579 * 13580 * tcp_urp_last_valid is cleared when the T_exdata_ind 13581 * is sent up. This plus the check for old data 13582 * (gap >= 0) handles the wraparound of the sequence 13583 * number space without having to always track the 13584 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13585 * this max in its rcv_up variable). 13586 * 13587 * This prevents duplicate SIGURGS due to a "late" 13588 * zero-window probe when the T_EXDATA_IND has already 13589 * been sent up. 13590 */ 13591 if ((flags & TH_URG) && 13592 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13593 tcp->tcp_urp_last))) { 13594 if (IPCL_IS_NONSTR(connp)) { 13595 if (!TCP_IS_DETACHED(tcp)) { 13596 (*connp->conn_upcalls-> 13597 su_signal_oob) 13598 (connp->conn_upper_handle, 13599 urp); 13600 } 13601 } else { 13602 mp1 = allocb(0, BPRI_MED); 13603 if (mp1 == NULL) { 13604 freemsg(mp); 13605 return; 13606 } 13607 if (!TCP_IS_DETACHED(tcp) && 13608 !putnextctl1(tcp->tcp_rq, 13609 M_PCSIG, SIGURG)) { 13610 /* Try again on the rexmit. */ 13611 freemsg(mp1); 13612 freemsg(mp); 13613 return; 13614 } 13615 /* 13616 * If the next byte would be the mark 13617 * then mark with MARKNEXT else mark 13618 * with NOTMARKNEXT. 13619 */ 13620 if (gap == 0 && urp == 0) 13621 mp1->b_flag |= MSGMARKNEXT; 13622 else 13623 mp1->b_flag |= MSGNOTMARKNEXT; 13624 freemsg(tcp->tcp_urp_mark_mp); 13625 tcp->tcp_urp_mark_mp = mp1; 13626 flags |= TH_SEND_URP_MARK; 13627 } 13628 tcp->tcp_urp_last_valid = B_TRUE; 13629 tcp->tcp_urp_last = urp + seg_seq; 13630 } 13631 /* 13632 * If this is a zero window probe, continue to 13633 * process the ACK part. But we need to set seg_len 13634 * to 0 to avoid data processing. Otherwise just 13635 * drop the segment and send back an ACK. 13636 */ 13637 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13638 flags &= ~(TH_SYN | TH_URG); 13639 seg_len = 0; 13640 goto process_ack; 13641 } else { 13642 freemsg(mp); 13643 goto ack_check; 13644 } 13645 } 13646 /* Pitch out of window stuff off the end. */ 13647 rgap = seg_len; 13648 mp2 = mp; 13649 do { 13650 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13651 (uintptr_t)INT_MAX); 13652 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13653 if (rgap < 0) { 13654 mp2->b_wptr += rgap; 13655 if ((mp1 = mp2->b_cont) != NULL) { 13656 mp2->b_cont = NULL; 13657 freemsg(mp1); 13658 } 13659 break; 13660 } 13661 } while ((mp2 = mp2->b_cont) != NULL); 13662 } 13663 ok:; 13664 /* 13665 * TCP should check ECN info for segments inside the window only. 13666 * Therefore the check should be done here. 13667 */ 13668 if (tcp->tcp_ecn_ok) { 13669 if (flags & TH_CWR) { 13670 tcp->tcp_ecn_echo_on = B_FALSE; 13671 } 13672 /* 13673 * Note that both ECN_CE and CWR can be set in the 13674 * same segment. In this case, we once again turn 13675 * on ECN_ECHO. 13676 */ 13677 if (tcp->tcp_ipversion == IPV4_VERSION) { 13678 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13679 13680 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13681 tcp->tcp_ecn_echo_on = B_TRUE; 13682 } 13683 } else { 13684 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13685 13686 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13687 htonl(IPH_ECN_CE << 20)) { 13688 tcp->tcp_ecn_echo_on = B_TRUE; 13689 } 13690 } 13691 } 13692 13693 /* 13694 * Check whether we can update tcp_ts_recent. This test is 13695 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13696 * Extensions for High Performance: An Update", Internet Draft. 13697 */ 13698 if (tcp->tcp_snd_ts_ok && 13699 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13700 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13701 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13702 tcp->tcp_last_rcv_lbolt = lbolt64; 13703 } 13704 13705 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13706 /* 13707 * FIN in an out of order segment. We record this in 13708 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13709 * Clear the FIN so that any check on FIN flag will fail. 13710 * Remember that FIN also counts in the sequence number 13711 * space. So we need to ack out of order FIN only segments. 13712 */ 13713 if (flags & TH_FIN) { 13714 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13715 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13716 flags &= ~TH_FIN; 13717 flags |= TH_ACK_NEEDED; 13718 } 13719 if (seg_len > 0) { 13720 /* Fill in the SACK blk list. */ 13721 if (tcp->tcp_snd_sack_ok) { 13722 ASSERT(tcp->tcp_sack_info != NULL); 13723 tcp_sack_insert(tcp->tcp_sack_list, 13724 seg_seq, seg_seq + seg_len, 13725 &(tcp->tcp_num_sack_blk)); 13726 } 13727 13728 /* 13729 * Attempt reassembly and see if we have something 13730 * ready to go. 13731 */ 13732 mp = tcp_reass(tcp, mp, seg_seq); 13733 /* Always ack out of order packets */ 13734 flags |= TH_ACK_NEEDED | TH_PUSH; 13735 if (mp) { 13736 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13737 (uintptr_t)INT_MAX); 13738 seg_len = mp->b_cont ? msgdsize(mp) : 13739 (int)(mp->b_wptr - mp->b_rptr); 13740 seg_seq = tcp->tcp_rnxt; 13741 /* 13742 * A gap is filled and the seq num and len 13743 * of the gap match that of a previously 13744 * received FIN, put the FIN flag back in. 13745 */ 13746 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13747 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13748 flags |= TH_FIN; 13749 tcp->tcp_valid_bits &= 13750 ~TCP_OFO_FIN_VALID; 13751 } 13752 } else { 13753 /* 13754 * Keep going even with NULL mp. 13755 * There may be a useful ACK or something else 13756 * we don't want to miss. 13757 * 13758 * But TCP should not perform fast retransmit 13759 * because of the ack number. TCP uses 13760 * seg_len == 0 to determine if it is a pure 13761 * ACK. And this is not a pure ACK. 13762 */ 13763 seg_len = 0; 13764 ofo_seg = B_TRUE; 13765 } 13766 } 13767 } else if (seg_len > 0) { 13768 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13769 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13770 /* 13771 * If an out of order FIN was received before, and the seq 13772 * num and len of the new segment match that of the FIN, 13773 * put the FIN flag back in. 13774 */ 13775 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13776 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13777 flags |= TH_FIN; 13778 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13779 } 13780 } 13781 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13782 if (flags & TH_RST) { 13783 freemsg(mp); 13784 switch (tcp->tcp_state) { 13785 case TCPS_SYN_RCVD: 13786 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13787 break; 13788 case TCPS_ESTABLISHED: 13789 case TCPS_FIN_WAIT_1: 13790 case TCPS_FIN_WAIT_2: 13791 case TCPS_CLOSE_WAIT: 13792 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13793 break; 13794 case TCPS_CLOSING: 13795 case TCPS_LAST_ACK: 13796 (void) tcp_clean_death(tcp, 0, 16); 13797 break; 13798 default: 13799 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13800 (void) tcp_clean_death(tcp, ENXIO, 17); 13801 break; 13802 } 13803 return; 13804 } 13805 if (flags & TH_SYN) { 13806 /* 13807 * See RFC 793, Page 71 13808 * 13809 * The seq number must be in the window as it should 13810 * be "fixed" above. If it is outside window, it should 13811 * be already rejected. Note that we allow seg_seq to be 13812 * rnxt + rwnd because we want to accept 0 window probe. 13813 */ 13814 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13815 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13816 freemsg(mp); 13817 /* 13818 * If the ACK flag is not set, just use our snxt as the 13819 * seq number of the RST segment. 13820 */ 13821 if (!(flags & TH_ACK)) { 13822 seg_ack = tcp->tcp_snxt; 13823 } 13824 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13825 TH_RST|TH_ACK); 13826 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13827 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13828 return; 13829 } 13830 /* 13831 * urp could be -1 when the urp field in the packet is 0 13832 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13833 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13834 */ 13835 if (flags & TH_URG && urp >= 0) { 13836 if (!tcp->tcp_urp_last_valid || 13837 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13838 if (IPCL_IS_NONSTR(connp)) { 13839 if (!TCP_IS_DETACHED(tcp)) { 13840 (*connp->conn_upcalls->su_signal_oob) 13841 (connp->conn_upper_handle, urp); 13842 } 13843 } else { 13844 /* 13845 * If we haven't generated the signal yet for 13846 * this urgent pointer value, do it now. Also, 13847 * send up a zero-length M_DATA indicating 13848 * whether or not this is the mark. The latter 13849 * is not needed when a T_EXDATA_IND is sent up. 13850 * However, if there are allocation failures 13851 * this code relies on the sender retransmitting 13852 * and the socket code for determining the mark 13853 * should not block waiting for the peer to 13854 * transmit. Thus, for simplicity we always 13855 * send up the mark indication. 13856 */ 13857 mp1 = allocb(0, BPRI_MED); 13858 if (mp1 == NULL) { 13859 freemsg(mp); 13860 return; 13861 } 13862 if (!TCP_IS_DETACHED(tcp) && 13863 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13864 SIGURG)) { 13865 /* Try again on the rexmit. */ 13866 freemsg(mp1); 13867 freemsg(mp); 13868 return; 13869 } 13870 /* 13871 * Mark with NOTMARKNEXT for now. 13872 * The code below will change this to MARKNEXT 13873 * if we are at the mark. 13874 * 13875 * If there are allocation failures (e.g. in 13876 * dupmsg below) the next time tcp_rput_data 13877 * sees the urgent segment it will send up the 13878 * MSGMARKNEXT message. 13879 */ 13880 mp1->b_flag |= MSGNOTMARKNEXT; 13881 freemsg(tcp->tcp_urp_mark_mp); 13882 tcp->tcp_urp_mark_mp = mp1; 13883 flags |= TH_SEND_URP_MARK; 13884 #ifdef DEBUG 13885 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13886 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13887 "last %x, %s", 13888 seg_seq, urp, tcp->tcp_urp_last, 13889 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13890 #endif /* DEBUG */ 13891 } 13892 tcp->tcp_urp_last_valid = B_TRUE; 13893 tcp->tcp_urp_last = urp + seg_seq; 13894 } else if (tcp->tcp_urp_mark_mp != NULL) { 13895 /* 13896 * An allocation failure prevented the previous 13897 * tcp_rput_data from sending up the allocated 13898 * MSG*MARKNEXT message - send it up this time 13899 * around. 13900 */ 13901 flags |= TH_SEND_URP_MARK; 13902 } 13903 13904 /* 13905 * If the urgent byte is in this segment, make sure that it is 13906 * all by itself. This makes it much easier to deal with the 13907 * possibility of an allocation failure on the T_exdata_ind. 13908 * Note that seg_len is the number of bytes in the segment, and 13909 * urp is the offset into the segment of the urgent byte. 13910 * urp < seg_len means that the urgent byte is in this segment. 13911 */ 13912 if (urp < seg_len) { 13913 if (seg_len != 1) { 13914 uint32_t tmp_rnxt; 13915 /* 13916 * Break it up and feed it back in. 13917 * Re-attach the IP header. 13918 */ 13919 mp->b_rptr = iphdr; 13920 if (urp > 0) { 13921 /* 13922 * There is stuff before the urgent 13923 * byte. 13924 */ 13925 mp1 = dupmsg(mp); 13926 if (!mp1) { 13927 /* 13928 * Trim from urgent byte on. 13929 * The rest will come back. 13930 */ 13931 (void) adjmsg(mp, 13932 urp - seg_len); 13933 tcp_rput_data(connp, 13934 mp, NULL); 13935 return; 13936 } 13937 (void) adjmsg(mp1, urp - seg_len); 13938 /* Feed this piece back in. */ 13939 tmp_rnxt = tcp->tcp_rnxt; 13940 tcp_rput_data(connp, mp1, NULL); 13941 /* 13942 * If the data passed back in was not 13943 * processed (ie: bad ACK) sending 13944 * the remainder back in will cause a 13945 * loop. In this case, drop the 13946 * packet and let the sender try 13947 * sending a good packet. 13948 */ 13949 if (tmp_rnxt == tcp->tcp_rnxt) { 13950 freemsg(mp); 13951 return; 13952 } 13953 } 13954 if (urp != seg_len - 1) { 13955 uint32_t tmp_rnxt; 13956 /* 13957 * There is stuff after the urgent 13958 * byte. 13959 */ 13960 mp1 = dupmsg(mp); 13961 if (!mp1) { 13962 /* 13963 * Trim everything beyond the 13964 * urgent byte. The rest will 13965 * come back. 13966 */ 13967 (void) adjmsg(mp, 13968 urp + 1 - seg_len); 13969 tcp_rput_data(connp, 13970 mp, NULL); 13971 return; 13972 } 13973 (void) adjmsg(mp1, urp + 1 - seg_len); 13974 tmp_rnxt = tcp->tcp_rnxt; 13975 tcp_rput_data(connp, mp1, NULL); 13976 /* 13977 * If the data passed back in was not 13978 * processed (ie: bad ACK) sending 13979 * the remainder back in will cause a 13980 * loop. In this case, drop the 13981 * packet and let the sender try 13982 * sending a good packet. 13983 */ 13984 if (tmp_rnxt == tcp->tcp_rnxt) { 13985 freemsg(mp); 13986 return; 13987 } 13988 } 13989 tcp_rput_data(connp, mp, NULL); 13990 return; 13991 } 13992 /* 13993 * This segment contains only the urgent byte. We 13994 * have to allocate the T_exdata_ind, if we can. 13995 */ 13996 if (IPCL_IS_NONSTR(connp)) { 13997 int error; 13998 13999 (*connp->conn_upcalls->su_recv) 14000 (connp->conn_upper_handle, mp, seg_len, 14001 MSG_OOB, &error, NULL); 14002 mp = NULL; 14003 goto update_ack; 14004 } else if (!tcp->tcp_urp_mp) { 14005 struct T_exdata_ind *tei; 14006 mp1 = allocb(sizeof (struct T_exdata_ind), 14007 BPRI_MED); 14008 if (!mp1) { 14009 /* 14010 * Sigh... It'll be back. 14011 * Generate any MSG*MARK message now. 14012 */ 14013 freemsg(mp); 14014 seg_len = 0; 14015 if (flags & TH_SEND_URP_MARK) { 14016 14017 14018 ASSERT(tcp->tcp_urp_mark_mp); 14019 tcp->tcp_urp_mark_mp->b_flag &= 14020 ~MSGNOTMARKNEXT; 14021 tcp->tcp_urp_mark_mp->b_flag |= 14022 MSGMARKNEXT; 14023 } 14024 goto ack_check; 14025 } 14026 mp1->b_datap->db_type = M_PROTO; 14027 tei = (struct T_exdata_ind *)mp1->b_rptr; 14028 tei->PRIM_type = T_EXDATA_IND; 14029 tei->MORE_flag = 0; 14030 mp1->b_wptr = (uchar_t *)&tei[1]; 14031 tcp->tcp_urp_mp = mp1; 14032 #ifdef DEBUG 14033 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14034 "tcp_rput: allocated exdata_ind %s", 14035 tcp_display(tcp, NULL, 14036 DISP_PORT_ONLY)); 14037 #endif /* DEBUG */ 14038 /* 14039 * There is no need to send a separate MSG*MARK 14040 * message since the T_EXDATA_IND will be sent 14041 * now. 14042 */ 14043 flags &= ~TH_SEND_URP_MARK; 14044 freemsg(tcp->tcp_urp_mark_mp); 14045 tcp->tcp_urp_mark_mp = NULL; 14046 } 14047 /* 14048 * Now we are all set. On the next putnext upstream, 14049 * tcp_urp_mp will be non-NULL and will get prepended 14050 * to what has to be this piece containing the urgent 14051 * byte. If for any reason we abort this segment below, 14052 * if it comes back, we will have this ready, or it 14053 * will get blown off in close. 14054 */ 14055 } else if (urp == seg_len) { 14056 /* 14057 * The urgent byte is the next byte after this sequence 14058 * number. If there is data it is marked with 14059 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14060 * since it is not needed. Otherwise, if the code 14061 * above just allocated a zero-length tcp_urp_mark_mp 14062 * message, that message is tagged with MSGMARKNEXT. 14063 * Sending up these MSGMARKNEXT messages makes 14064 * SIOCATMARK work correctly even though 14065 * the T_EXDATA_IND will not be sent up until the 14066 * urgent byte arrives. 14067 */ 14068 if (seg_len != 0) { 14069 flags |= TH_MARKNEXT_NEEDED; 14070 freemsg(tcp->tcp_urp_mark_mp); 14071 tcp->tcp_urp_mark_mp = NULL; 14072 flags &= ~TH_SEND_URP_MARK; 14073 } else if (tcp->tcp_urp_mark_mp != NULL) { 14074 flags |= TH_SEND_URP_MARK; 14075 tcp->tcp_urp_mark_mp->b_flag &= 14076 ~MSGNOTMARKNEXT; 14077 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14078 } 14079 #ifdef DEBUG 14080 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14081 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14082 seg_len, flags, 14083 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14084 #endif /* DEBUG */ 14085 } 14086 #ifdef DEBUG 14087 else { 14088 /* Data left until we hit mark */ 14089 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14090 "tcp_rput: URP %d bytes left, %s", 14091 urp - seg_len, tcp_display(tcp, NULL, 14092 DISP_PORT_ONLY)); 14093 } 14094 #endif /* DEBUG */ 14095 } 14096 14097 process_ack: 14098 if (!(flags & TH_ACK)) { 14099 freemsg(mp); 14100 goto xmit_check; 14101 } 14102 } 14103 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14104 14105 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14106 tcp->tcp_ip_forward_progress = B_TRUE; 14107 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14108 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14109 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14110 /* 3-way handshake complete - pass up the T_CONN_IND */ 14111 tcp_t *listener = tcp->tcp_listener; 14112 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14113 14114 tcp->tcp_tconnind_started = B_TRUE; 14115 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14116 /* 14117 * We are here means eager is fine but it can 14118 * get a TH_RST at any point between now and till 14119 * accept completes and disappear. We need to 14120 * ensure that reference to eager is valid after 14121 * we get out of eager's perimeter. So we do 14122 * an extra refhold. 14123 */ 14124 CONN_INC_REF(connp); 14125 14126 /* 14127 * The listener also exists because of the refhold 14128 * done in tcp_conn_request. Its possible that it 14129 * might have closed. We will check that once we 14130 * get inside listeners context. 14131 */ 14132 CONN_INC_REF(listener->tcp_connp); 14133 if (listener->tcp_connp->conn_sqp == 14134 connp->conn_sqp) { 14135 /* 14136 * We optimize by not calling an SQUEUE_ENTER 14137 * on the listener since we know that the 14138 * listener and eager squeues are the same. 14139 * We are able to make this check safely only 14140 * because neither the eager nor the listener 14141 * can change its squeue. Only an active connect 14142 * can change its squeue 14143 */ 14144 tcp_send_conn_ind(listener->tcp_connp, mp, 14145 listener->tcp_connp->conn_sqp); 14146 CONN_DEC_REF(listener->tcp_connp); 14147 } else if (!tcp->tcp_loopback) { 14148 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14149 mp, tcp_send_conn_ind, 14150 listener->tcp_connp, SQ_FILL, 14151 SQTAG_TCP_CONN_IND); 14152 } else { 14153 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14154 mp, tcp_send_conn_ind, 14155 listener->tcp_connp, SQ_PROCESS, 14156 SQTAG_TCP_CONN_IND); 14157 } 14158 } 14159 14160 if (tcp->tcp_active_open) { 14161 /* 14162 * We are seeing the final ack in the three way 14163 * hand shake of a active open'ed connection 14164 * so we must send up a T_CONN_CON 14165 */ 14166 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14167 freemsg(mp); 14168 return; 14169 } 14170 /* 14171 * Don't fuse the loopback endpoints for 14172 * simultaneous active opens. 14173 */ 14174 if (tcp->tcp_loopback) { 14175 TCP_STAT(tcps, tcp_fusion_unfusable); 14176 tcp->tcp_unfusable = B_TRUE; 14177 } 14178 } 14179 14180 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14181 bytes_acked--; 14182 /* SYN was acked - making progress */ 14183 if (tcp->tcp_ipversion == IPV6_VERSION) 14184 tcp->tcp_ip_forward_progress = B_TRUE; 14185 14186 /* 14187 * If SYN was retransmitted, need to reset all 14188 * retransmission info as this segment will be 14189 * treated as a dup ACK. 14190 */ 14191 if (tcp->tcp_rexmit) { 14192 tcp->tcp_rexmit = B_FALSE; 14193 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14194 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14195 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14196 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14197 tcp->tcp_ms_we_have_waited = 0; 14198 tcp->tcp_cwnd = mss; 14199 } 14200 14201 /* 14202 * We set the send window to zero here. 14203 * This is needed if there is data to be 14204 * processed already on the queue. 14205 * Later (at swnd_update label), the 14206 * "new_swnd > tcp_swnd" condition is satisfied 14207 * the XMIT_NEEDED flag is set in the current 14208 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14209 * called if there is already data on queue in 14210 * this state. 14211 */ 14212 tcp->tcp_swnd = 0; 14213 14214 if (new_swnd > tcp->tcp_max_swnd) 14215 tcp->tcp_max_swnd = new_swnd; 14216 tcp->tcp_swl1 = seg_seq; 14217 tcp->tcp_swl2 = seg_ack; 14218 tcp->tcp_state = TCPS_ESTABLISHED; 14219 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14220 14221 /* Fuse when both sides are in ESTABLISHED state */ 14222 if (tcp->tcp_loopback && do_tcp_fusion) 14223 tcp_fuse(tcp, iphdr, tcph); 14224 14225 } 14226 /* This code follows 4.4BSD-Lite2 mostly. */ 14227 if (bytes_acked < 0) 14228 goto est; 14229 14230 /* 14231 * If TCP is ECN capable and the congestion experience bit is 14232 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14233 * done once per window (or more loosely, per RTT). 14234 */ 14235 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14236 tcp->tcp_cwr = B_FALSE; 14237 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14238 if (!tcp->tcp_cwr) { 14239 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14240 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14241 tcp->tcp_cwnd = npkt * mss; 14242 /* 14243 * If the cwnd is 0, use the timer to clock out 14244 * new segments. This is required by the ECN spec. 14245 */ 14246 if (npkt == 0) { 14247 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14248 /* 14249 * This makes sure that when the ACK comes 14250 * back, we will increase tcp_cwnd by 1 MSS. 14251 */ 14252 tcp->tcp_cwnd_cnt = 0; 14253 } 14254 tcp->tcp_cwr = B_TRUE; 14255 /* 14256 * This marks the end of the current window of in 14257 * flight data. That is why we don't use 14258 * tcp_suna + tcp_swnd. Only data in flight can 14259 * provide ECN info. 14260 */ 14261 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14262 tcp->tcp_ecn_cwr_sent = B_FALSE; 14263 } 14264 } 14265 14266 mp1 = tcp->tcp_xmit_head; 14267 if (bytes_acked == 0) { 14268 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14269 int dupack_cnt; 14270 14271 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14272 /* 14273 * Fast retransmit. When we have seen exactly three 14274 * identical ACKs while we have unacked data 14275 * outstanding we take it as a hint that our peer 14276 * dropped something. 14277 * 14278 * If TCP is retransmitting, don't do fast retransmit. 14279 */ 14280 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14281 ! tcp->tcp_rexmit) { 14282 /* Do Limited Transmit */ 14283 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14284 tcps->tcps_dupack_fast_retransmit) { 14285 /* 14286 * RFC 3042 14287 * 14288 * What we need to do is temporarily 14289 * increase tcp_cwnd so that new 14290 * data can be sent if it is allowed 14291 * by the receive window (tcp_rwnd). 14292 * tcp_wput_data() will take care of 14293 * the rest. 14294 * 14295 * If the connection is SACK capable, 14296 * only do limited xmit when there 14297 * is SACK info. 14298 * 14299 * Note how tcp_cwnd is incremented. 14300 * The first dup ACK will increase 14301 * it by 1 MSS. The second dup ACK 14302 * will increase it by 2 MSS. This 14303 * means that only 1 new segment will 14304 * be sent for each dup ACK. 14305 */ 14306 if (tcp->tcp_unsent > 0 && 14307 (!tcp->tcp_snd_sack_ok || 14308 (tcp->tcp_snd_sack_ok && 14309 tcp->tcp_notsack_list != NULL))) { 14310 tcp->tcp_cwnd += mss << 14311 (tcp->tcp_dupack_cnt - 1); 14312 flags |= TH_LIMIT_XMIT; 14313 } 14314 } else if (dupack_cnt == 14315 tcps->tcps_dupack_fast_retransmit) { 14316 14317 /* 14318 * If we have reduced tcp_ssthresh 14319 * because of ECN, do not reduce it again 14320 * unless it is already one window of data 14321 * away. After one window of data, tcp_cwr 14322 * should then be cleared. Note that 14323 * for non ECN capable connection, tcp_cwr 14324 * should always be false. 14325 * 14326 * Adjust cwnd since the duplicate 14327 * ack indicates that a packet was 14328 * dropped (due to congestion.) 14329 */ 14330 if (!tcp->tcp_cwr) { 14331 npkt = ((tcp->tcp_snxt - 14332 tcp->tcp_suna) >> 1) / mss; 14333 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14334 mss; 14335 tcp->tcp_cwnd = (npkt + 14336 tcp->tcp_dupack_cnt) * mss; 14337 } 14338 if (tcp->tcp_ecn_ok) { 14339 tcp->tcp_cwr = B_TRUE; 14340 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14341 tcp->tcp_ecn_cwr_sent = B_FALSE; 14342 } 14343 14344 /* 14345 * We do Hoe's algorithm. Refer to her 14346 * paper "Improving the Start-up Behavior 14347 * of a Congestion Control Scheme for TCP," 14348 * appeared in SIGCOMM'96. 14349 * 14350 * Save highest seq no we have sent so far. 14351 * Be careful about the invisible FIN byte. 14352 */ 14353 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14354 (tcp->tcp_unsent == 0)) { 14355 tcp->tcp_rexmit_max = tcp->tcp_fss; 14356 } else { 14357 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14358 } 14359 14360 /* 14361 * Do not allow bursty traffic during. 14362 * fast recovery. Refer to Fall and Floyd's 14363 * paper "Simulation-based Comparisons of 14364 * Tahoe, Reno and SACK TCP" (in CCR?) 14365 * This is a best current practise. 14366 */ 14367 tcp->tcp_snd_burst = TCP_CWND_SS; 14368 14369 /* 14370 * For SACK: 14371 * Calculate tcp_pipe, which is the 14372 * estimated number of bytes in 14373 * network. 14374 * 14375 * tcp_fack is the highest sack'ed seq num 14376 * TCP has received. 14377 * 14378 * tcp_pipe is explained in the above quoted 14379 * Fall and Floyd's paper. tcp_fack is 14380 * explained in Mathis and Mahdavi's 14381 * "Forward Acknowledgment: Refining TCP 14382 * Congestion Control" in SIGCOMM '96. 14383 */ 14384 if (tcp->tcp_snd_sack_ok) { 14385 ASSERT(tcp->tcp_sack_info != NULL); 14386 if (tcp->tcp_notsack_list != NULL) { 14387 tcp->tcp_pipe = tcp->tcp_snxt - 14388 tcp->tcp_fack; 14389 tcp->tcp_sack_snxt = seg_ack; 14390 flags |= TH_NEED_SACK_REXMIT; 14391 } else { 14392 /* 14393 * Always initialize tcp_pipe 14394 * even though we don't have 14395 * any SACK info. If later 14396 * we get SACK info and 14397 * tcp_pipe is not initialized, 14398 * funny things will happen. 14399 */ 14400 tcp->tcp_pipe = 14401 tcp->tcp_cwnd_ssthresh; 14402 } 14403 } else { 14404 flags |= TH_REXMIT_NEEDED; 14405 } /* tcp_snd_sack_ok */ 14406 14407 } else { 14408 /* 14409 * Here we perform congestion 14410 * avoidance, but NOT slow start. 14411 * This is known as the Fast 14412 * Recovery Algorithm. 14413 */ 14414 if (tcp->tcp_snd_sack_ok && 14415 tcp->tcp_notsack_list != NULL) { 14416 flags |= TH_NEED_SACK_REXMIT; 14417 tcp->tcp_pipe -= mss; 14418 if (tcp->tcp_pipe < 0) 14419 tcp->tcp_pipe = 0; 14420 } else { 14421 /* 14422 * We know that one more packet has 14423 * left the pipe thus we can update 14424 * cwnd. 14425 */ 14426 cwnd = tcp->tcp_cwnd + mss; 14427 if (cwnd > tcp->tcp_cwnd_max) 14428 cwnd = tcp->tcp_cwnd_max; 14429 tcp->tcp_cwnd = cwnd; 14430 if (tcp->tcp_unsent > 0) 14431 flags |= TH_XMIT_NEEDED; 14432 } 14433 } 14434 } 14435 } else if (tcp->tcp_zero_win_probe) { 14436 /* 14437 * If the window has opened, need to arrange 14438 * to send additional data. 14439 */ 14440 if (new_swnd != 0) { 14441 /* tcp_suna != tcp_snxt */ 14442 /* Packet contains a window update */ 14443 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14444 tcp->tcp_zero_win_probe = 0; 14445 tcp->tcp_timer_backoff = 0; 14446 tcp->tcp_ms_we_have_waited = 0; 14447 14448 /* 14449 * Transmit starting with tcp_suna since 14450 * the one byte probe is not ack'ed. 14451 * If TCP has sent more than one identical 14452 * probe, tcp_rexmit will be set. That means 14453 * tcp_ss_rexmit() will send out the one 14454 * byte along with new data. Otherwise, 14455 * fake the retransmission. 14456 */ 14457 flags |= TH_XMIT_NEEDED; 14458 if (!tcp->tcp_rexmit) { 14459 tcp->tcp_rexmit = B_TRUE; 14460 tcp->tcp_dupack_cnt = 0; 14461 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14462 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14463 } 14464 } 14465 } 14466 goto swnd_update; 14467 } 14468 14469 /* 14470 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14471 * If the ACK value acks something that we have not yet sent, it might 14472 * be an old duplicate segment. Send an ACK to re-synchronize the 14473 * other side. 14474 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14475 * state is handled above, so we can always just drop the segment and 14476 * send an ACK here. 14477 * 14478 * Should we send ACKs in response to ACK only segments? 14479 */ 14480 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14481 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14482 /* drop the received segment */ 14483 freemsg(mp); 14484 14485 /* 14486 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14487 * greater than 0, check if the number of such 14488 * bogus ACks is greater than that count. If yes, 14489 * don't send back any ACK. This prevents TCP from 14490 * getting into an ACK storm if somehow an attacker 14491 * successfully spoofs an acceptable segment to our 14492 * peer. 14493 */ 14494 if (tcp_drop_ack_unsent_cnt > 0 && 14495 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14496 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14497 return; 14498 } 14499 mp = tcp_ack_mp(tcp); 14500 if (mp != NULL) { 14501 BUMP_LOCAL(tcp->tcp_obsegs); 14502 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14503 tcp_send_data(tcp, tcp->tcp_wq, mp); 14504 } 14505 return; 14506 } 14507 14508 /* 14509 * TCP gets a new ACK, update the notsack'ed list to delete those 14510 * blocks that are covered by this ACK. 14511 */ 14512 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14513 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14514 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14515 } 14516 14517 /* 14518 * If we got an ACK after fast retransmit, check to see 14519 * if it is a partial ACK. If it is not and the congestion 14520 * window was inflated to account for the other side's 14521 * cached packets, retract it. If it is, do Hoe's algorithm. 14522 */ 14523 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14524 ASSERT(tcp->tcp_rexmit == B_FALSE); 14525 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14526 tcp->tcp_dupack_cnt = 0; 14527 /* 14528 * Restore the orig tcp_cwnd_ssthresh after 14529 * fast retransmit phase. 14530 */ 14531 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14532 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14533 } 14534 tcp->tcp_rexmit_max = seg_ack; 14535 tcp->tcp_cwnd_cnt = 0; 14536 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14537 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14538 14539 /* 14540 * Remove all notsack info to avoid confusion with 14541 * the next fast retrasnmit/recovery phase. 14542 */ 14543 if (tcp->tcp_snd_sack_ok && 14544 tcp->tcp_notsack_list != NULL) { 14545 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14546 } 14547 } else { 14548 if (tcp->tcp_snd_sack_ok && 14549 tcp->tcp_notsack_list != NULL) { 14550 flags |= TH_NEED_SACK_REXMIT; 14551 tcp->tcp_pipe -= mss; 14552 if (tcp->tcp_pipe < 0) 14553 tcp->tcp_pipe = 0; 14554 } else { 14555 /* 14556 * Hoe's algorithm: 14557 * 14558 * Retransmit the unack'ed segment and 14559 * restart fast recovery. Note that we 14560 * need to scale back tcp_cwnd to the 14561 * original value when we started fast 14562 * recovery. This is to prevent overly 14563 * aggressive behaviour in sending new 14564 * segments. 14565 */ 14566 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14567 tcps->tcps_dupack_fast_retransmit * mss; 14568 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14569 flags |= TH_REXMIT_NEEDED; 14570 } 14571 } 14572 } else { 14573 tcp->tcp_dupack_cnt = 0; 14574 if (tcp->tcp_rexmit) { 14575 /* 14576 * TCP is retranmitting. If the ACK ack's all 14577 * outstanding data, update tcp_rexmit_max and 14578 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14579 * to the correct value. 14580 * 14581 * Note that SEQ_LEQ() is used. This is to avoid 14582 * unnecessary fast retransmit caused by dup ACKs 14583 * received when TCP does slow start retransmission 14584 * after a time out. During this phase, TCP may 14585 * send out segments which are already received. 14586 * This causes dup ACKs to be sent back. 14587 */ 14588 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14589 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14590 tcp->tcp_rexmit_nxt = seg_ack; 14591 } 14592 if (seg_ack != tcp->tcp_rexmit_max) { 14593 flags |= TH_XMIT_NEEDED; 14594 } 14595 } else { 14596 tcp->tcp_rexmit = B_FALSE; 14597 tcp->tcp_xmit_zc_clean = B_FALSE; 14598 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14599 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14600 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14601 } 14602 tcp->tcp_ms_we_have_waited = 0; 14603 } 14604 } 14605 14606 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14607 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14608 tcp->tcp_suna = seg_ack; 14609 if (tcp->tcp_zero_win_probe != 0) { 14610 tcp->tcp_zero_win_probe = 0; 14611 tcp->tcp_timer_backoff = 0; 14612 } 14613 14614 /* 14615 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14616 * Note that it cannot be the SYN being ack'ed. The code flow 14617 * will not reach here. 14618 */ 14619 if (mp1 == NULL) { 14620 goto fin_acked; 14621 } 14622 14623 /* 14624 * Update the congestion window. 14625 * 14626 * If TCP is not ECN capable or TCP is ECN capable but the 14627 * congestion experience bit is not set, increase the tcp_cwnd as 14628 * usual. 14629 */ 14630 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14631 cwnd = tcp->tcp_cwnd; 14632 add = mss; 14633 14634 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14635 /* 14636 * This is to prevent an increase of less than 1 MSS of 14637 * tcp_cwnd. With partial increase, tcp_wput_data() 14638 * may send out tinygrams in order to preserve mblk 14639 * boundaries. 14640 * 14641 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14642 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14643 * increased by 1 MSS for every RTTs. 14644 */ 14645 if (tcp->tcp_cwnd_cnt <= 0) { 14646 tcp->tcp_cwnd_cnt = cwnd + add; 14647 } else { 14648 tcp->tcp_cwnd_cnt -= add; 14649 add = 0; 14650 } 14651 } 14652 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14653 } 14654 14655 /* See if the latest urgent data has been acknowledged */ 14656 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14657 SEQ_GT(seg_ack, tcp->tcp_urg)) 14658 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14659 14660 /* Can we update the RTT estimates? */ 14661 if (tcp->tcp_snd_ts_ok) { 14662 /* Ignore zero timestamp echo-reply. */ 14663 if (tcpopt.tcp_opt_ts_ecr != 0) { 14664 tcp_set_rto(tcp, (int32_t)lbolt - 14665 (int32_t)tcpopt.tcp_opt_ts_ecr); 14666 } 14667 14668 /* If needed, restart the timer. */ 14669 if (tcp->tcp_set_timer == 1) { 14670 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14671 tcp->tcp_set_timer = 0; 14672 } 14673 /* 14674 * Update tcp_csuna in case the other side stops sending 14675 * us timestamps. 14676 */ 14677 tcp->tcp_csuna = tcp->tcp_snxt; 14678 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14679 /* 14680 * An ACK sequence we haven't seen before, so get the RTT 14681 * and update the RTO. But first check if the timestamp is 14682 * valid to use. 14683 */ 14684 if ((mp1->b_next != NULL) && 14685 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14686 tcp_set_rto(tcp, (int32_t)lbolt - 14687 (int32_t)(intptr_t)mp1->b_prev); 14688 else 14689 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14690 14691 /* Remeber the last sequence to be ACKed */ 14692 tcp->tcp_csuna = seg_ack; 14693 if (tcp->tcp_set_timer == 1) { 14694 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14695 tcp->tcp_set_timer = 0; 14696 } 14697 } else { 14698 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14699 } 14700 14701 /* Eat acknowledged bytes off the xmit queue. */ 14702 for (;;) { 14703 mblk_t *mp2; 14704 uchar_t *wptr; 14705 14706 wptr = mp1->b_wptr; 14707 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14708 bytes_acked -= (int)(wptr - mp1->b_rptr); 14709 if (bytes_acked < 0) { 14710 mp1->b_rptr = wptr + bytes_acked; 14711 /* 14712 * Set a new timestamp if all the bytes timed by the 14713 * old timestamp have been ack'ed. 14714 */ 14715 if (SEQ_GT(seg_ack, 14716 (uint32_t)(uintptr_t)(mp1->b_next))) { 14717 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14718 mp1->b_next = NULL; 14719 } 14720 break; 14721 } 14722 mp1->b_next = NULL; 14723 mp1->b_prev = NULL; 14724 mp2 = mp1; 14725 mp1 = mp1->b_cont; 14726 14727 /* 14728 * This notification is required for some zero-copy 14729 * clients to maintain a copy semantic. After the data 14730 * is ack'ed, client is safe to modify or reuse the buffer. 14731 */ 14732 if (tcp->tcp_snd_zcopy_aware && 14733 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14734 tcp_zcopy_notify(tcp); 14735 freeb(mp2); 14736 if (bytes_acked == 0) { 14737 if (mp1 == NULL) { 14738 /* Everything is ack'ed, clear the tail. */ 14739 tcp->tcp_xmit_tail = NULL; 14740 /* 14741 * Cancel the timer unless we are still 14742 * waiting for an ACK for the FIN packet. 14743 */ 14744 if (tcp->tcp_timer_tid != 0 && 14745 tcp->tcp_snxt == tcp->tcp_suna) { 14746 (void) TCP_TIMER_CANCEL(tcp, 14747 tcp->tcp_timer_tid); 14748 tcp->tcp_timer_tid = 0; 14749 } 14750 goto pre_swnd_update; 14751 } 14752 if (mp2 != tcp->tcp_xmit_tail) 14753 break; 14754 tcp->tcp_xmit_tail = mp1; 14755 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14756 (uintptr_t)INT_MAX); 14757 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14758 mp1->b_rptr); 14759 break; 14760 } 14761 if (mp1 == NULL) { 14762 /* 14763 * More was acked but there is nothing more 14764 * outstanding. This means that the FIN was 14765 * just acked or that we're talking to a clown. 14766 */ 14767 fin_acked: 14768 ASSERT(tcp->tcp_fin_sent); 14769 tcp->tcp_xmit_tail = NULL; 14770 if (tcp->tcp_fin_sent) { 14771 /* FIN was acked - making progress */ 14772 if (tcp->tcp_ipversion == IPV6_VERSION && 14773 !tcp->tcp_fin_acked) 14774 tcp->tcp_ip_forward_progress = B_TRUE; 14775 tcp->tcp_fin_acked = B_TRUE; 14776 if (tcp->tcp_linger_tid != 0 && 14777 TCP_TIMER_CANCEL(tcp, 14778 tcp->tcp_linger_tid) >= 0) { 14779 tcp_stop_lingering(tcp); 14780 freemsg(mp); 14781 mp = NULL; 14782 } 14783 } else { 14784 /* 14785 * We should never get here because 14786 * we have already checked that the 14787 * number of bytes ack'ed should be 14788 * smaller than or equal to what we 14789 * have sent so far (it is the 14790 * acceptability check of the ACK). 14791 * We can only get here if the send 14792 * queue is corrupted. 14793 * 14794 * Terminate the connection and 14795 * panic the system. It is better 14796 * for us to panic instead of 14797 * continuing to avoid other disaster. 14798 */ 14799 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14800 tcp->tcp_rnxt, TH_RST|TH_ACK); 14801 panic("Memory corruption " 14802 "detected for connection %s.", 14803 tcp_display(tcp, NULL, 14804 DISP_ADDR_AND_PORT)); 14805 /*NOTREACHED*/ 14806 } 14807 goto pre_swnd_update; 14808 } 14809 ASSERT(mp2 != tcp->tcp_xmit_tail); 14810 } 14811 if (tcp->tcp_unsent) { 14812 flags |= TH_XMIT_NEEDED; 14813 } 14814 pre_swnd_update: 14815 tcp->tcp_xmit_head = mp1; 14816 swnd_update: 14817 /* 14818 * The following check is different from most other implementations. 14819 * For bi-directional transfer, when segments are dropped, the 14820 * "normal" check will not accept a window update in those 14821 * retransmitted segemnts. Failing to do that, TCP may send out 14822 * segments which are outside receiver's window. As TCP accepts 14823 * the ack in those retransmitted segments, if the window update in 14824 * the same segment is not accepted, TCP will incorrectly calculates 14825 * that it can send more segments. This can create a deadlock 14826 * with the receiver if its window becomes zero. 14827 */ 14828 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14829 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14830 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14831 /* 14832 * The criteria for update is: 14833 * 14834 * 1. the segment acknowledges some data. Or 14835 * 2. the segment is new, i.e. it has a higher seq num. Or 14836 * 3. the segment is not old and the advertised window is 14837 * larger than the previous advertised window. 14838 */ 14839 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14840 flags |= TH_XMIT_NEEDED; 14841 tcp->tcp_swnd = new_swnd; 14842 if (new_swnd > tcp->tcp_max_swnd) 14843 tcp->tcp_max_swnd = new_swnd; 14844 tcp->tcp_swl1 = seg_seq; 14845 tcp->tcp_swl2 = seg_ack; 14846 } 14847 est: 14848 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14849 14850 switch (tcp->tcp_state) { 14851 case TCPS_FIN_WAIT_1: 14852 if (tcp->tcp_fin_acked) { 14853 tcp->tcp_state = TCPS_FIN_WAIT_2; 14854 /* 14855 * We implement the non-standard BSD/SunOS 14856 * FIN_WAIT_2 flushing algorithm. 14857 * If there is no user attached to this 14858 * TCP endpoint, then this TCP struct 14859 * could hang around forever in FIN_WAIT_2 14860 * state if the peer forgets to send us 14861 * a FIN. To prevent this, we wait only 14862 * 2*MSL (a convenient time value) for 14863 * the FIN to arrive. If it doesn't show up, 14864 * we flush the TCP endpoint. This algorithm, 14865 * though a violation of RFC-793, has worked 14866 * for over 10 years in BSD systems. 14867 * Note: SunOS 4.x waits 675 seconds before 14868 * flushing the FIN_WAIT_2 connection. 14869 */ 14870 TCP_TIMER_RESTART(tcp, 14871 tcps->tcps_fin_wait_2_flush_interval); 14872 } 14873 break; 14874 case TCPS_FIN_WAIT_2: 14875 break; /* Shutdown hook? */ 14876 case TCPS_LAST_ACK: 14877 freemsg(mp); 14878 if (tcp->tcp_fin_acked) { 14879 (void) tcp_clean_death(tcp, 0, 19); 14880 return; 14881 } 14882 goto xmit_check; 14883 case TCPS_CLOSING: 14884 if (tcp->tcp_fin_acked) { 14885 tcp->tcp_state = TCPS_TIME_WAIT; 14886 /* 14887 * Unconditionally clear the exclusive binding 14888 * bit so this TIME-WAIT connection won't 14889 * interfere with new ones. 14890 */ 14891 tcp->tcp_exclbind = 0; 14892 if (!TCP_IS_DETACHED(tcp)) { 14893 TCP_TIMER_RESTART(tcp, 14894 tcps->tcps_time_wait_interval); 14895 } else { 14896 tcp_time_wait_append(tcp); 14897 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14898 } 14899 } 14900 /*FALLTHRU*/ 14901 case TCPS_CLOSE_WAIT: 14902 freemsg(mp); 14903 goto xmit_check; 14904 default: 14905 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14906 break; 14907 } 14908 } 14909 if (flags & TH_FIN) { 14910 /* Make sure we ack the fin */ 14911 flags |= TH_ACK_NEEDED; 14912 if (!tcp->tcp_fin_rcvd) { 14913 tcp->tcp_fin_rcvd = B_TRUE; 14914 tcp->tcp_rnxt++; 14915 tcph = tcp->tcp_tcph; 14916 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14917 14918 /* 14919 * Generate the ordrel_ind at the end unless we 14920 * are an eager guy. 14921 * In the eager case tcp_rsrv will do this when run 14922 * after tcp_accept is done. 14923 */ 14924 if (tcp->tcp_listener == NULL && 14925 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14926 flags |= TH_ORDREL_NEEDED; 14927 switch (tcp->tcp_state) { 14928 case TCPS_SYN_RCVD: 14929 case TCPS_ESTABLISHED: 14930 tcp->tcp_state = TCPS_CLOSE_WAIT; 14931 /* Keepalive? */ 14932 break; 14933 case TCPS_FIN_WAIT_1: 14934 if (!tcp->tcp_fin_acked) { 14935 tcp->tcp_state = TCPS_CLOSING; 14936 break; 14937 } 14938 /* FALLTHRU */ 14939 case TCPS_FIN_WAIT_2: 14940 tcp->tcp_state = TCPS_TIME_WAIT; 14941 /* 14942 * Unconditionally clear the exclusive binding 14943 * bit so this TIME-WAIT connection won't 14944 * interfere with new ones. 14945 */ 14946 tcp->tcp_exclbind = 0; 14947 if (!TCP_IS_DETACHED(tcp)) { 14948 TCP_TIMER_RESTART(tcp, 14949 tcps->tcps_time_wait_interval); 14950 } else { 14951 tcp_time_wait_append(tcp); 14952 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14953 } 14954 if (seg_len) { 14955 /* 14956 * implies data piggybacked on FIN. 14957 * break to handle data. 14958 */ 14959 break; 14960 } 14961 freemsg(mp); 14962 goto ack_check; 14963 } 14964 } 14965 } 14966 if (mp == NULL) 14967 goto xmit_check; 14968 if (seg_len == 0) { 14969 freemsg(mp); 14970 goto xmit_check; 14971 } 14972 if (mp->b_rptr == mp->b_wptr) { 14973 /* 14974 * The header has been consumed, so we remove the 14975 * zero-length mblk here. 14976 */ 14977 mp1 = mp; 14978 mp = mp->b_cont; 14979 freeb(mp1); 14980 } 14981 update_ack: 14982 tcph = tcp->tcp_tcph; 14983 tcp->tcp_rack_cnt++; 14984 { 14985 uint32_t cur_max; 14986 14987 cur_max = tcp->tcp_rack_cur_max; 14988 if (tcp->tcp_rack_cnt >= cur_max) { 14989 /* 14990 * We have more unacked data than we should - send 14991 * an ACK now. 14992 */ 14993 flags |= TH_ACK_NEEDED; 14994 cur_max++; 14995 if (cur_max > tcp->tcp_rack_abs_max) 14996 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14997 else 14998 tcp->tcp_rack_cur_max = cur_max; 14999 } else if (TCP_IS_DETACHED(tcp)) { 15000 /* We don't have an ACK timer for detached TCP. */ 15001 flags |= TH_ACK_NEEDED; 15002 } else if (seg_len < mss) { 15003 /* 15004 * If we get a segment that is less than an mss, and we 15005 * already have unacknowledged data, and the amount 15006 * unacknowledged is not a multiple of mss, then we 15007 * better generate an ACK now. Otherwise, this may be 15008 * the tail piece of a transaction, and we would rather 15009 * wait for the response. 15010 */ 15011 uint32_t udif; 15012 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15013 (uintptr_t)INT_MAX); 15014 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15015 if (udif && (udif % mss)) 15016 flags |= TH_ACK_NEEDED; 15017 else 15018 flags |= TH_ACK_TIMER_NEEDED; 15019 } else { 15020 /* Start delayed ack timer */ 15021 flags |= TH_ACK_TIMER_NEEDED; 15022 } 15023 } 15024 tcp->tcp_rnxt += seg_len; 15025 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15026 15027 if (mp == NULL) 15028 goto xmit_check; 15029 15030 /* Update SACK list */ 15031 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15032 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15033 &(tcp->tcp_num_sack_blk)); 15034 } 15035 15036 if (tcp->tcp_urp_mp) { 15037 tcp->tcp_urp_mp->b_cont = mp; 15038 mp = tcp->tcp_urp_mp; 15039 tcp->tcp_urp_mp = NULL; 15040 /* Ready for a new signal. */ 15041 tcp->tcp_urp_last_valid = B_FALSE; 15042 #ifdef DEBUG 15043 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15044 "tcp_rput: sending exdata_ind %s", 15045 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15046 #endif /* DEBUG */ 15047 } 15048 15049 /* 15050 * Check for ancillary data changes compared to last segment. 15051 */ 15052 if (tcp->tcp_ipv6_recvancillary != 0) { 15053 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15054 ASSERT(mp != NULL); 15055 } 15056 15057 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15058 /* 15059 * Side queue inbound data until the accept happens. 15060 * tcp_accept/tcp_rput drains this when the accept happens. 15061 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15062 * T_EXDATA_IND) it is queued on b_next. 15063 * XXX Make urgent data use this. Requires: 15064 * Removing tcp_listener check for TH_URG 15065 * Making M_PCPROTO and MARK messages skip the eager case 15066 */ 15067 15068 if (tcp->tcp_kssl_pending) { 15069 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15070 mblk_t *, mp); 15071 tcp_kssl_input(tcp, mp); 15072 } else { 15073 tcp_rcv_enqueue(tcp, mp, seg_len); 15074 } 15075 } else { 15076 sodirect_t *sodp = tcp->tcp_sodirect; 15077 15078 /* 15079 * If an sodirect connection and an enabled sodirect_t then 15080 * sodp will be set to point to the tcp_t/sonode_t shared 15081 * sodirect_t and the sodirect_t's lock will be held. 15082 */ 15083 if (sodp != NULL) { 15084 mutex_enter(sodp->sod_lockp); 15085 if (!(sodp->sod_state & SOD_ENABLED) || 15086 (tcp->tcp_kssl_ctx != NULL && 15087 DB_TYPE(mp) == M_DATA)) { 15088 sodp = NULL; 15089 } 15090 mutex_exit(sodp->sod_lockp); 15091 } 15092 if (mp->b_datap->db_type != M_DATA || 15093 (flags & TH_MARKNEXT_NEEDED)) { 15094 if (IPCL_IS_NONSTR(connp)) { 15095 int error; 15096 15097 if ((*connp->conn_upcalls->su_recv) 15098 (connp->conn_upper_handle, mp, 15099 seg_len, 0, &error, NULL) <= 0) { 15100 if (error == ENOSPC) { 15101 tcp->tcp_rwnd -= seg_len; 15102 } else if (error == EOPNOTSUPP) { 15103 tcp_rcv_enqueue(tcp, mp, 15104 seg_len); 15105 } 15106 } 15107 } else if (sodp != NULL) { 15108 mutex_enter(sodp->sod_lockp); 15109 SOD_UIOAFINI(sodp); 15110 if (!SOD_QEMPTY(sodp) && 15111 (sodp->sod_state & SOD_WAKE_NOT)) { 15112 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15113 /* sod_wakeup() did the mutex_exit() */ 15114 } else { 15115 mutex_exit(sodp->sod_lockp); 15116 } 15117 } else if (tcp->tcp_rcv_list != NULL) { 15118 flags |= tcp_rcv_drain(tcp); 15119 } 15120 ASSERT(tcp->tcp_rcv_list == NULL || 15121 tcp->tcp_fused_sigurg); 15122 15123 if (flags & TH_MARKNEXT_NEEDED) { 15124 #ifdef DEBUG 15125 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15126 "tcp_rput: sending MSGMARKNEXT %s", 15127 tcp_display(tcp, NULL, 15128 DISP_PORT_ONLY)); 15129 #endif /* DEBUG */ 15130 mp->b_flag |= MSGMARKNEXT; 15131 flags &= ~TH_MARKNEXT_NEEDED; 15132 } 15133 15134 /* Does this need SSL processing first? */ 15135 if ((tcp->tcp_kssl_ctx != NULL) && 15136 (DB_TYPE(mp) == M_DATA)) { 15137 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15138 mblk_t *, mp); 15139 tcp_kssl_input(tcp, mp); 15140 } else if (!IPCL_IS_NONSTR(connp)) { 15141 /* Already handled non-STREAMS case. */ 15142 putnext(tcp->tcp_rq, mp); 15143 if (!canputnext(tcp->tcp_rq)) 15144 tcp->tcp_rwnd -= seg_len; 15145 } 15146 } else if ((tcp->tcp_kssl_ctx != NULL) && 15147 (DB_TYPE(mp) == M_DATA)) { 15148 /* Does this need SSL processing first? */ 15149 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15150 tcp_kssl_input(tcp, mp); 15151 } else if (IPCL_IS_NONSTR(connp)) { 15152 /* Non-STREAMS socket */ 15153 boolean_t push = flags & (TH_PUSH|TH_FIN); 15154 int error; 15155 15156 if ((*connp->conn_upcalls->su_recv)( 15157 connp->conn_upper_handle, 15158 mp, seg_len, 0, &error, &push) <= 0) { 15159 if (error == ENOSPC) { 15160 tcp->tcp_rwnd -= seg_len; 15161 } else if (error == EOPNOTSUPP) { 15162 tcp_rcv_enqueue(tcp, mp, seg_len); 15163 } 15164 } else if (push) { 15165 /* 15166 * PUSH bit set and sockfs is not 15167 * flow controlled 15168 */ 15169 flags |= tcp_rwnd_reopen(tcp); 15170 } 15171 } else if (sodp != NULL) { 15172 /* 15173 * Sodirect so all mblk_t's are queued on the 15174 * socket directly, check for wakeup of blocked 15175 * reader (if any), and last if flow-controled. 15176 */ 15177 mutex_enter(sodp->sod_lockp); 15178 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15179 if ((sodp->sod_state & SOD_WAKE_NEED) || 15180 (flags & (TH_PUSH|TH_FIN))) { 15181 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15182 /* sod_wakeup() did the mutex_exit() */ 15183 } else { 15184 if (SOD_QFULL(sodp)) { 15185 /* Q is full, need backenable */ 15186 SOD_QSETBE(sodp); 15187 } 15188 mutex_exit(sodp->sod_lockp); 15189 } 15190 } else if ((flags & (TH_PUSH|TH_FIN)) || 15191 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15192 if (tcp->tcp_rcv_list != NULL) { 15193 /* 15194 * Enqueue the new segment first and then 15195 * call tcp_rcv_drain() to send all data 15196 * up. The other way to do this is to 15197 * send all queued data up and then call 15198 * putnext() to send the new segment up. 15199 * This way can remove the else part later 15200 * on. 15201 * 15202 * We don't do this to avoid one more call to 15203 * canputnext() as tcp_rcv_drain() needs to 15204 * call canputnext(). 15205 */ 15206 tcp_rcv_enqueue(tcp, mp, seg_len); 15207 flags |= tcp_rcv_drain(tcp); 15208 } else { 15209 putnext(tcp->tcp_rq, mp); 15210 if (!canputnext(tcp->tcp_rq)) 15211 tcp->tcp_rwnd -= seg_len; 15212 } 15213 } else { 15214 /* 15215 * Enqueue all packets when processing an mblk 15216 * from the co queue and also enqueue normal packets. 15217 * For packets which belong to SSL stream do SSL 15218 * processing first. 15219 */ 15220 tcp_rcv_enqueue(tcp, mp, seg_len); 15221 } 15222 /* 15223 * Make sure the timer is running if we have data waiting 15224 * for a push bit. This provides resiliency against 15225 * implementations that do not correctly generate push bits. 15226 * 15227 * Note, for sodirect if Q isn't empty and there's not a 15228 * pending wakeup then we need a timer. Also note that sodp 15229 * is assumed to be still valid after exit()ing the sod_lockp 15230 * above and while the SOD state can change it can only change 15231 * such that the Q is empty now even though data was added 15232 * above. 15233 */ 15234 if (!IPCL_IS_NONSTR(connp) && 15235 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15236 (sodp->sod_state & SOD_WAKE_NOT)) || 15237 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15238 tcp->tcp_push_tid == 0) { 15239 /* 15240 * The connection may be closed at this point, so don't 15241 * do anything for a detached tcp. 15242 */ 15243 if (!TCP_IS_DETACHED(tcp)) 15244 tcp->tcp_push_tid = TCP_TIMER(tcp, 15245 tcp_push_timer, 15246 MSEC_TO_TICK( 15247 tcps->tcps_push_timer_interval)); 15248 } 15249 } 15250 15251 xmit_check: 15252 /* Is there anything left to do? */ 15253 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15254 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15255 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15256 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15257 goto done; 15258 15259 /* Any transmit work to do and a non-zero window? */ 15260 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15261 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15262 if (flags & TH_REXMIT_NEEDED) { 15263 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15264 15265 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15266 if (snd_size > mss) 15267 snd_size = mss; 15268 if (snd_size > tcp->tcp_swnd) 15269 snd_size = tcp->tcp_swnd; 15270 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15271 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15272 B_TRUE); 15273 15274 if (mp1 != NULL) { 15275 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15276 tcp->tcp_csuna = tcp->tcp_snxt; 15277 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15278 UPDATE_MIB(&tcps->tcps_mib, 15279 tcpRetransBytes, snd_size); 15280 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15281 } 15282 } 15283 if (flags & TH_NEED_SACK_REXMIT) { 15284 tcp_sack_rxmit(tcp, &flags); 15285 } 15286 /* 15287 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15288 * out new segment. Note that tcp_rexmit should not be 15289 * set, otherwise TH_LIMIT_XMIT should not be set. 15290 */ 15291 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15292 if (!tcp->tcp_rexmit) { 15293 tcp_wput_data(tcp, NULL, B_FALSE); 15294 } else { 15295 tcp_ss_rexmit(tcp); 15296 } 15297 } 15298 /* 15299 * Adjust tcp_cwnd back to normal value after sending 15300 * new data segments. 15301 */ 15302 if (flags & TH_LIMIT_XMIT) { 15303 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15304 /* 15305 * This will restart the timer. Restarting the 15306 * timer is used to avoid a timeout before the 15307 * limited transmitted segment's ACK gets back. 15308 */ 15309 if (tcp->tcp_xmit_head != NULL) 15310 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15311 } 15312 15313 /* Anything more to do? */ 15314 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15315 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15316 goto done; 15317 } 15318 ack_check: 15319 if (flags & TH_SEND_URP_MARK) { 15320 ASSERT(tcp->tcp_urp_mark_mp); 15321 ASSERT(!IPCL_IS_NONSTR(connp)); 15322 /* 15323 * Send up any queued data and then send the mark message 15324 */ 15325 sodirect_t *sodp; 15326 15327 SOD_PTR_ENTER(tcp, sodp); 15328 15329 mp1 = tcp->tcp_urp_mark_mp; 15330 tcp->tcp_urp_mark_mp = NULL; 15331 if (sodp != NULL) { 15332 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15333 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15334 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15335 } 15336 ASSERT(tcp->tcp_rcv_list == NULL); 15337 15338 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15339 /* sod_wakeup() does the mutex_exit() */ 15340 } else if (tcp->tcp_rcv_list != NULL) { 15341 flags |= tcp_rcv_drain(tcp); 15342 15343 ASSERT(tcp->tcp_rcv_list == NULL || 15344 tcp->tcp_fused_sigurg); 15345 15346 } 15347 putnext(tcp->tcp_rq, mp1); 15348 #ifdef DEBUG 15349 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15350 "tcp_rput: sending zero-length %s %s", 15351 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15352 "MSGNOTMARKNEXT"), 15353 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15354 #endif /* DEBUG */ 15355 flags &= ~TH_SEND_URP_MARK; 15356 } 15357 if (flags & TH_ACK_NEEDED) { 15358 /* 15359 * Time to send an ack for some reason. 15360 */ 15361 mp1 = tcp_ack_mp(tcp); 15362 15363 if (mp1 != NULL) { 15364 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15365 BUMP_LOCAL(tcp->tcp_obsegs); 15366 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15367 } 15368 if (tcp->tcp_ack_tid != 0) { 15369 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15370 tcp->tcp_ack_tid = 0; 15371 } 15372 } 15373 if (flags & TH_ACK_TIMER_NEEDED) { 15374 /* 15375 * Arrange for deferred ACK or push wait timeout. 15376 * Start timer if it is not already running. 15377 */ 15378 if (tcp->tcp_ack_tid == 0) { 15379 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15380 MSEC_TO_TICK(tcp->tcp_localnet ? 15381 (clock_t)tcps->tcps_local_dack_interval : 15382 (clock_t)tcps->tcps_deferred_ack_interval)); 15383 } 15384 } 15385 if (flags & TH_ORDREL_NEEDED) { 15386 /* 15387 * Send up the ordrel_ind unless we are an eager guy. 15388 * In the eager case tcp_rsrv will do this when run 15389 * after tcp_accept is done. 15390 */ 15391 sodirect_t *sodp; 15392 15393 ASSERT(tcp->tcp_listener == NULL); 15394 15395 if (IPCL_IS_NONSTR(connp)) { 15396 ASSERT(tcp->tcp_ordrel_mp == NULL); 15397 tcp->tcp_ordrel_done = B_TRUE; 15398 (*connp->conn_upcalls->su_opctl) 15399 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15400 goto done; 15401 } 15402 15403 SOD_PTR_ENTER(tcp, sodp); 15404 if (sodp != NULL) { 15405 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15406 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15407 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15408 } 15409 /* No more sodirect */ 15410 tcp->tcp_sodirect = NULL; 15411 if (!SOD_QEMPTY(sodp)) { 15412 /* Mblk(s) to process, notify */ 15413 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15414 /* sod_wakeup() does the mutex_exit() */ 15415 } else { 15416 /* Nothing to process */ 15417 mutex_exit(sodp->sod_lockp); 15418 } 15419 } else if (tcp->tcp_rcv_list != NULL) { 15420 /* 15421 * Push any mblk(s) enqueued from co processing. 15422 */ 15423 flags |= tcp_rcv_drain(tcp); 15424 15425 ASSERT(tcp->tcp_rcv_list == NULL || 15426 tcp->tcp_fused_sigurg); 15427 } 15428 15429 mp1 = tcp->tcp_ordrel_mp; 15430 tcp->tcp_ordrel_mp = NULL; 15431 tcp->tcp_ordrel_done = B_TRUE; 15432 putnext(tcp->tcp_rq, mp1); 15433 } 15434 done: 15435 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15436 } 15437 15438 /* 15439 * This function does PAWS protection check. Returns B_TRUE if the 15440 * segment passes the PAWS test, else returns B_FALSE. 15441 */ 15442 boolean_t 15443 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15444 { 15445 uint8_t flags; 15446 int options; 15447 uint8_t *up; 15448 15449 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15450 /* 15451 * If timestamp option is aligned nicely, get values inline, 15452 * otherwise call general routine to parse. Only do that 15453 * if timestamp is the only option. 15454 */ 15455 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15456 TCPOPT_REAL_TS_LEN && 15457 OK_32PTR((up = ((uint8_t *)tcph) + 15458 TCP_MIN_HEADER_LENGTH)) && 15459 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15460 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15461 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15462 15463 options = TCP_OPT_TSTAMP_PRESENT; 15464 } else { 15465 if (tcp->tcp_snd_sack_ok) { 15466 tcpoptp->tcp = tcp; 15467 } else { 15468 tcpoptp->tcp = NULL; 15469 } 15470 options = tcp_parse_options(tcph, tcpoptp); 15471 } 15472 15473 if (options & TCP_OPT_TSTAMP_PRESENT) { 15474 /* 15475 * Do PAWS per RFC 1323 section 4.2. Accept RST 15476 * regardless of the timestamp, page 18 RFC 1323.bis. 15477 */ 15478 if ((flags & TH_RST) == 0 && 15479 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15480 tcp->tcp_ts_recent)) { 15481 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15482 PAWS_TIMEOUT)) { 15483 /* This segment is not acceptable. */ 15484 return (B_FALSE); 15485 } else { 15486 /* 15487 * Connection has been idle for 15488 * too long. Reset the timestamp 15489 * and assume the segment is valid. 15490 */ 15491 tcp->tcp_ts_recent = 15492 tcpoptp->tcp_opt_ts_val; 15493 } 15494 } 15495 } else { 15496 /* 15497 * If we don't get a timestamp on every packet, we 15498 * figure we can't really trust 'em, so we stop sending 15499 * and parsing them. 15500 */ 15501 tcp->tcp_snd_ts_ok = B_FALSE; 15502 15503 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15504 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15505 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15506 /* 15507 * Adjust the tcp_mss accordingly. We also need to 15508 * adjust tcp_cwnd here in accordance with the new mss. 15509 * But we avoid doing a slow start here so as to not 15510 * to lose on the transfer rate built up so far. 15511 */ 15512 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15513 if (tcp->tcp_snd_sack_ok) { 15514 ASSERT(tcp->tcp_sack_info != NULL); 15515 tcp->tcp_max_sack_blk = 4; 15516 } 15517 } 15518 return (B_TRUE); 15519 } 15520 15521 /* 15522 * Attach ancillary data to a received TCP segments for the 15523 * ancillary pieces requested by the application that are 15524 * different than they were in the previous data segment. 15525 * 15526 * Save the "current" values once memory allocation is ok so that 15527 * when memory allocation fails we can just wait for the next data segment. 15528 */ 15529 static mblk_t * 15530 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15531 { 15532 struct T_optdata_ind *todi; 15533 int optlen; 15534 uchar_t *optptr; 15535 struct T_opthdr *toh; 15536 uint_t addflag; /* Which pieces to add */ 15537 mblk_t *mp1; 15538 15539 optlen = 0; 15540 addflag = 0; 15541 /* If app asked for pktinfo and the index has changed ... */ 15542 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15543 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15544 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15545 optlen += sizeof (struct T_opthdr) + 15546 sizeof (struct in6_pktinfo); 15547 addflag |= TCP_IPV6_RECVPKTINFO; 15548 } 15549 /* If app asked for hoplimit and it has changed ... */ 15550 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15551 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15552 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15553 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15554 addflag |= TCP_IPV6_RECVHOPLIMIT; 15555 } 15556 /* If app asked for tclass and it has changed ... */ 15557 if ((ipp->ipp_fields & IPPF_TCLASS) && 15558 ipp->ipp_tclass != tcp->tcp_recvtclass && 15559 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15560 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15561 addflag |= TCP_IPV6_RECVTCLASS; 15562 } 15563 /* 15564 * If app asked for hopbyhop headers and it has changed ... 15565 * For security labels, note that (1) security labels can't change on 15566 * a connected socket at all, (2) we're connected to at most one peer, 15567 * (3) if anything changes, then it must be some other extra option. 15568 */ 15569 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15570 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15571 (ipp->ipp_fields & IPPF_HOPOPTS), 15572 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15573 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15574 tcp->tcp_label_len; 15575 addflag |= TCP_IPV6_RECVHOPOPTS; 15576 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15577 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15578 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15579 return (mp); 15580 } 15581 /* If app asked for dst headers before routing headers ... */ 15582 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15583 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15584 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15585 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15586 optlen += sizeof (struct T_opthdr) + 15587 ipp->ipp_rtdstoptslen; 15588 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15589 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15590 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15591 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15592 return (mp); 15593 } 15594 /* If app asked for routing headers and it has changed ... */ 15595 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15596 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15597 (ipp->ipp_fields & IPPF_RTHDR), 15598 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15599 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15600 addflag |= TCP_IPV6_RECVRTHDR; 15601 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15602 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15603 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15604 return (mp); 15605 } 15606 /* If app asked for dest headers and it has changed ... */ 15607 if ((tcp->tcp_ipv6_recvancillary & 15608 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15609 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15610 (ipp->ipp_fields & IPPF_DSTOPTS), 15611 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15612 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15613 addflag |= TCP_IPV6_RECVDSTOPTS; 15614 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15615 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15616 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15617 return (mp); 15618 } 15619 15620 if (optlen == 0) { 15621 /* Nothing to add */ 15622 return (mp); 15623 } 15624 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15625 if (mp1 == NULL) { 15626 /* 15627 * Defer sending ancillary data until the next TCP segment 15628 * arrives. 15629 */ 15630 return (mp); 15631 } 15632 mp1->b_cont = mp; 15633 mp = mp1; 15634 mp->b_wptr += sizeof (*todi) + optlen; 15635 mp->b_datap->db_type = M_PROTO; 15636 todi = (struct T_optdata_ind *)mp->b_rptr; 15637 todi->PRIM_type = T_OPTDATA_IND; 15638 todi->DATA_flag = 1; /* MORE data */ 15639 todi->OPT_length = optlen; 15640 todi->OPT_offset = sizeof (*todi); 15641 optptr = (uchar_t *)&todi[1]; 15642 /* 15643 * If app asked for pktinfo and the index has changed ... 15644 * Note that the local address never changes for the connection. 15645 */ 15646 if (addflag & TCP_IPV6_RECVPKTINFO) { 15647 struct in6_pktinfo *pkti; 15648 15649 toh = (struct T_opthdr *)optptr; 15650 toh->level = IPPROTO_IPV6; 15651 toh->name = IPV6_PKTINFO; 15652 toh->len = sizeof (*toh) + sizeof (*pkti); 15653 toh->status = 0; 15654 optptr += sizeof (*toh); 15655 pkti = (struct in6_pktinfo *)optptr; 15656 if (tcp->tcp_ipversion == IPV6_VERSION) 15657 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15658 else 15659 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15660 &pkti->ipi6_addr); 15661 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15662 optptr += sizeof (*pkti); 15663 ASSERT(OK_32PTR(optptr)); 15664 /* Save as "last" value */ 15665 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15666 } 15667 /* If app asked for hoplimit and it has changed ... */ 15668 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15669 toh = (struct T_opthdr *)optptr; 15670 toh->level = IPPROTO_IPV6; 15671 toh->name = IPV6_HOPLIMIT; 15672 toh->len = sizeof (*toh) + sizeof (uint_t); 15673 toh->status = 0; 15674 optptr += sizeof (*toh); 15675 *(uint_t *)optptr = ipp->ipp_hoplimit; 15676 optptr += sizeof (uint_t); 15677 ASSERT(OK_32PTR(optptr)); 15678 /* Save as "last" value */ 15679 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15680 } 15681 /* If app asked for tclass and it has changed ... */ 15682 if (addflag & TCP_IPV6_RECVTCLASS) { 15683 toh = (struct T_opthdr *)optptr; 15684 toh->level = IPPROTO_IPV6; 15685 toh->name = IPV6_TCLASS; 15686 toh->len = sizeof (*toh) + sizeof (uint_t); 15687 toh->status = 0; 15688 optptr += sizeof (*toh); 15689 *(uint_t *)optptr = ipp->ipp_tclass; 15690 optptr += sizeof (uint_t); 15691 ASSERT(OK_32PTR(optptr)); 15692 /* Save as "last" value */ 15693 tcp->tcp_recvtclass = ipp->ipp_tclass; 15694 } 15695 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15696 toh = (struct T_opthdr *)optptr; 15697 toh->level = IPPROTO_IPV6; 15698 toh->name = IPV6_HOPOPTS; 15699 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15700 tcp->tcp_label_len; 15701 toh->status = 0; 15702 optptr += sizeof (*toh); 15703 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15704 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15705 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15706 ASSERT(OK_32PTR(optptr)); 15707 /* Save as last value */ 15708 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15709 (ipp->ipp_fields & IPPF_HOPOPTS), 15710 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15711 } 15712 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15713 toh = (struct T_opthdr *)optptr; 15714 toh->level = IPPROTO_IPV6; 15715 toh->name = IPV6_RTHDRDSTOPTS; 15716 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15717 toh->status = 0; 15718 optptr += sizeof (*toh); 15719 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15720 optptr += ipp->ipp_rtdstoptslen; 15721 ASSERT(OK_32PTR(optptr)); 15722 /* Save as last value */ 15723 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15724 &tcp->tcp_rtdstoptslen, 15725 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15726 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15727 } 15728 if (addflag & TCP_IPV6_RECVRTHDR) { 15729 toh = (struct T_opthdr *)optptr; 15730 toh->level = IPPROTO_IPV6; 15731 toh->name = IPV6_RTHDR; 15732 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15733 toh->status = 0; 15734 optptr += sizeof (*toh); 15735 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15736 optptr += ipp->ipp_rthdrlen; 15737 ASSERT(OK_32PTR(optptr)); 15738 /* Save as last value */ 15739 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15740 (ipp->ipp_fields & IPPF_RTHDR), 15741 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15742 } 15743 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15744 toh = (struct T_opthdr *)optptr; 15745 toh->level = IPPROTO_IPV6; 15746 toh->name = IPV6_DSTOPTS; 15747 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15748 toh->status = 0; 15749 optptr += sizeof (*toh); 15750 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15751 optptr += ipp->ipp_dstoptslen; 15752 ASSERT(OK_32PTR(optptr)); 15753 /* Save as last value */ 15754 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15755 (ipp->ipp_fields & IPPF_DSTOPTS), 15756 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15757 } 15758 ASSERT(optptr == mp->b_wptr); 15759 return (mp); 15760 } 15761 15762 /* 15763 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15764 * or a "bad" IRE detected by tcp_adapt_ire. 15765 * We can't tell if the failure was due to the laddr or the faddr 15766 * thus we clear out all addresses and ports. 15767 */ 15768 static void 15769 tcp_tpi_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15770 { 15771 queue_t *q = tcp->tcp_rq; 15772 tcph_t *tcph; 15773 struct T_error_ack *tea; 15774 conn_t *connp = tcp->tcp_connp; 15775 15776 15777 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15778 15779 if (mp->b_cont) { 15780 freemsg(mp->b_cont); 15781 mp->b_cont = NULL; 15782 } 15783 tea = (struct T_error_ack *)mp->b_rptr; 15784 switch (tea->PRIM_type) { 15785 case T_BIND_ACK: 15786 /* 15787 * Need to unbind with classifier since we were just told that 15788 * our bind succeeded. 15789 */ 15790 tcp->tcp_hard_bound = B_FALSE; 15791 tcp->tcp_hard_binding = B_FALSE; 15792 15793 ipcl_hash_remove(connp); 15794 /* Reuse the mblk if possible */ 15795 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15796 sizeof (*tea)); 15797 mp->b_rptr = mp->b_datap->db_base; 15798 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15799 tea = (struct T_error_ack *)mp->b_rptr; 15800 tea->PRIM_type = T_ERROR_ACK; 15801 tea->TLI_error = TSYSERR; 15802 tea->UNIX_error = error; 15803 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15804 tea->ERROR_prim = T_CONN_REQ; 15805 } else { 15806 tea->ERROR_prim = O_T_BIND_REQ; 15807 } 15808 break; 15809 15810 case T_ERROR_ACK: 15811 if (tcp->tcp_state >= TCPS_SYN_SENT) 15812 tea->ERROR_prim = T_CONN_REQ; 15813 break; 15814 default: 15815 panic("tcp_tpi_bind_failed: unexpected TPI type"); 15816 /*NOTREACHED*/ 15817 } 15818 15819 tcp->tcp_state = TCPS_IDLE; 15820 if (tcp->tcp_ipversion == IPV4_VERSION) 15821 tcp->tcp_ipha->ipha_src = 0; 15822 else 15823 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15824 /* 15825 * Copy of the src addr. in tcp_t is needed since 15826 * the lookup funcs. can only look at tcp_t 15827 */ 15828 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15829 15830 tcph = tcp->tcp_tcph; 15831 tcph->th_lport[0] = 0; 15832 tcph->th_lport[1] = 0; 15833 tcp_bind_hash_remove(tcp); 15834 bzero(&connp->u_port, sizeof (connp->u_port)); 15835 /* blow away saved option results if any */ 15836 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15837 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15838 15839 conn_delete_ire(tcp->tcp_connp, NULL); 15840 putnext(q, mp); 15841 } 15842 15843 /* 15844 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15845 * messages. 15846 */ 15847 void 15848 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15849 { 15850 uchar_t *rptr = mp->b_rptr; 15851 queue_t *q = tcp->tcp_rq; 15852 struct T_error_ack *tea; 15853 15854 switch (mp->b_datap->db_type) { 15855 case M_PROTO: 15856 case M_PCPROTO: 15857 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15858 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15859 break; 15860 tea = (struct T_error_ack *)rptr; 15861 switch (tea->PRIM_type) { 15862 case T_BIND_ACK: 15863 /* 15864 * AF_INET socket should not be here. 15865 */ 15866 ASSERT(tcp->tcp_family != AF_INET && 15867 tcp->tcp_family != AF_INET6); 15868 (void) tcp_post_ip_bind(tcp, mp->b_cont, 0); 15869 return; 15870 case T_ERROR_ACK: 15871 if (tcp->tcp_debug) { 15872 (void) strlog(TCP_MOD_ID, 0, 1, 15873 SL_TRACE|SL_ERROR, 15874 "tcp_rput_other: case T_ERROR_ACK, " 15875 "ERROR_prim == %d", 15876 tea->ERROR_prim); 15877 } 15878 switch (tea->ERROR_prim) { 15879 case O_T_BIND_REQ: 15880 case T_BIND_REQ: 15881 ASSERT(tcp->tcp_family != AF_INET); 15882 tcp_tpi_bind_failed(tcp, mp, 15883 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15884 ENETUNREACH : EADDRNOTAVAIL)); 15885 return; 15886 case T_SVR4_OPTMGMT_REQ: 15887 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15888 /* T_OPTMGMT_REQ generated by TCP */ 15889 printf("T_SVR4_OPTMGMT_REQ failed " 15890 "%d/%d - dropped (cnt %d)\n", 15891 tea->TLI_error, tea->UNIX_error, 15892 tcp->tcp_drop_opt_ack_cnt); 15893 freemsg(mp); 15894 tcp->tcp_drop_opt_ack_cnt--; 15895 return; 15896 } 15897 break; 15898 } 15899 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15900 tcp->tcp_drop_opt_ack_cnt > 0) { 15901 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15902 "- dropped (cnt %d)\n", 15903 tea->TLI_error, tea->UNIX_error, 15904 tcp->tcp_drop_opt_ack_cnt); 15905 freemsg(mp); 15906 tcp->tcp_drop_opt_ack_cnt--; 15907 return; 15908 } 15909 break; 15910 case T_OPTMGMT_ACK: 15911 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15912 /* T_OPTMGMT_REQ generated by TCP */ 15913 freemsg(mp); 15914 tcp->tcp_drop_opt_ack_cnt--; 15915 return; 15916 } 15917 break; 15918 default: 15919 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15920 break; 15921 } 15922 break; 15923 case M_FLUSH: 15924 if (*rptr & FLUSHR) 15925 flushq(q, FLUSHDATA); 15926 break; 15927 default: 15928 /* M_CTL will be directly sent to tcp_icmp_error() */ 15929 ASSERT(DB_TYPE(mp) != M_CTL); 15930 break; 15931 } 15932 /* 15933 * Make sure we set this bit before sending the ACK for 15934 * bind. Otherwise accept could possibly run and free 15935 * this tcp struct. 15936 */ 15937 ASSERT(q != NULL); 15938 putnext(q, mp); 15939 } 15940 15941 /* ARGSUSED */ 15942 static void 15943 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15944 { 15945 conn_t *connp = (conn_t *)arg; 15946 tcp_t *tcp = connp->conn_tcp; 15947 queue_t *q = tcp->tcp_rq; 15948 uint_t thwin; 15949 tcp_stack_t *tcps = tcp->tcp_tcps; 15950 sodirect_t *sodp; 15951 boolean_t fc; 15952 15953 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15954 tcp->tcp_rsrv_mp = mp; 15955 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15956 15957 TCP_STAT(tcps, tcp_rsrv_calls); 15958 15959 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15960 return; 15961 } 15962 15963 if (tcp->tcp_fused) { 15964 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15965 15966 ASSERT(tcp->tcp_fused); 15967 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15968 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15969 ASSERT(!TCP_IS_DETACHED(tcp)); 15970 ASSERT(tcp->tcp_connp->conn_sqp == 15971 peer_tcp->tcp_connp->conn_sqp); 15972 15973 /* 15974 * Normally we would not get backenabled in synchronous 15975 * streams mode, but in case this happens, we need to plug 15976 * synchronous streams during our drain to prevent a race 15977 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15978 */ 15979 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15980 if (tcp->tcp_rcv_list != NULL) 15981 (void) tcp_rcv_drain(tcp); 15982 15983 if (peer_tcp > tcp) { 15984 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15985 mutex_enter(&tcp->tcp_non_sq_lock); 15986 } else { 15987 mutex_enter(&tcp->tcp_non_sq_lock); 15988 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15989 } 15990 15991 if (peer_tcp->tcp_flow_stopped && 15992 (TCP_UNSENT_BYTES(peer_tcp) <= 15993 peer_tcp->tcp_xmit_lowater)) { 15994 tcp_clrqfull(peer_tcp); 15995 } 15996 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15997 mutex_exit(&tcp->tcp_non_sq_lock); 15998 15999 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 16000 TCP_STAT(tcps, tcp_fusion_backenabled); 16001 return; 16002 } 16003 16004 SOD_PTR_ENTER(tcp, sodp); 16005 if (sodp != NULL) { 16006 /* An sodirect connection */ 16007 if (SOD_QFULL(sodp)) { 16008 /* Flow-controlled, need another back-enable */ 16009 fc = B_TRUE; 16010 SOD_QSETBE(sodp); 16011 } else { 16012 /* Not flow-controlled */ 16013 fc = B_FALSE; 16014 } 16015 mutex_exit(sodp->sod_lockp); 16016 } else if (canputnext(q)) { 16017 /* STREAMS, not flow-controlled */ 16018 fc = B_FALSE; 16019 } else { 16020 /* STREAMS, flow-controlled */ 16021 fc = B_TRUE; 16022 } 16023 if (!fc) { 16024 /* Not flow-controlled, open rwnd */ 16025 tcp->tcp_rwnd = q->q_hiwat; 16026 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16027 << tcp->tcp_rcv_ws; 16028 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16029 /* 16030 * Send back a window update immediately if TCP is above 16031 * ESTABLISHED state and the increase of the rcv window 16032 * that the other side knows is at least 1 MSS after flow 16033 * control is lifted. 16034 */ 16035 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16036 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16037 tcp_xmit_ctl(NULL, tcp, 16038 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16039 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16040 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16041 } 16042 } 16043 } 16044 16045 /* 16046 * The read side service routine is called mostly when we get back-enabled as a 16047 * result of flow control relief. Since we don't actually queue anything in 16048 * TCP, we have no data to send out of here. What we do is clear the receive 16049 * window, and send out a window update. 16050 */ 16051 static void 16052 tcp_rsrv(queue_t *q) 16053 { 16054 conn_t *connp = Q_TO_CONN(q); 16055 tcp_t *tcp = connp->conn_tcp; 16056 mblk_t *mp; 16057 tcp_stack_t *tcps = tcp->tcp_tcps; 16058 16059 /* No code does a putq on the read side */ 16060 ASSERT(q->q_first == NULL); 16061 16062 /* Nothing to do for the default queue */ 16063 if (q == tcps->tcps_g_q) { 16064 return; 16065 } 16066 16067 /* 16068 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16069 * been run. So just return. 16070 */ 16071 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16072 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16073 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16074 return; 16075 } 16076 tcp->tcp_rsrv_mp = NULL; 16077 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16078 16079 CONN_INC_REF(connp); 16080 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16081 SQ_PROCESS, SQTAG_TCP_RSRV); 16082 } 16083 16084 /* 16085 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16086 * We do not allow the receive window to shrink. After setting rwnd, 16087 * set the flow control hiwat of the stream. 16088 * 16089 * This function is called in 2 cases: 16090 * 16091 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16092 * connection (passive open) and in tcp_rput_data() for active connect. 16093 * This is called after tcp_mss_set() when the desired MSS value is known. 16094 * This makes sure that our window size is a mutiple of the other side's 16095 * MSS. 16096 * 2) Handling SO_RCVBUF option. 16097 * 16098 * It is ASSUMED that the requested size is a multiple of the current MSS. 16099 * 16100 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16101 * user requests so. 16102 */ 16103 static int 16104 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16105 { 16106 uint32_t mss = tcp->tcp_mss; 16107 uint32_t old_max_rwnd; 16108 uint32_t max_transmittable_rwnd; 16109 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16110 tcp_stack_t *tcps = tcp->tcp_tcps; 16111 16112 if (tcp->tcp_fused) { 16113 size_t sth_hiwat; 16114 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16115 16116 ASSERT(peer_tcp != NULL); 16117 /* 16118 * Record the stream head's high water mark for 16119 * this endpoint; this is used for flow-control 16120 * purposes in tcp_fuse_output(). 16121 */ 16122 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16123 if (!tcp_detached) { 16124 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16125 sth_hiwat); 16126 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16127 conn_t *connp = tcp->tcp_connp; 16128 struct sock_proto_props sopp; 16129 16130 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16131 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16132 16133 (*connp->conn_upcalls->su_set_proto_props) 16134 (connp->conn_upper_handle, &sopp); 16135 } 16136 } 16137 16138 /* 16139 * In the fusion case, the maxpsz stream head value of 16140 * our peer is set according to its send buffer size 16141 * and our receive buffer size; since the latter may 16142 * have changed we need to update the peer's maxpsz. 16143 */ 16144 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16145 return (rwnd); 16146 } 16147 16148 if (tcp_detached) { 16149 old_max_rwnd = tcp->tcp_rwnd; 16150 } else { 16151 old_max_rwnd = tcp->tcp_recv_hiwater; 16152 } 16153 16154 /* 16155 * Insist on a receive window that is at least 16156 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16157 * funny TCP interactions of Nagle algorithm, SWS avoidance 16158 * and delayed acknowledgement. 16159 */ 16160 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16161 16162 /* 16163 * If window size info has already been exchanged, TCP should not 16164 * shrink the window. Shrinking window is doable if done carefully. 16165 * We may add that support later. But so far there is not a real 16166 * need to do that. 16167 */ 16168 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16169 /* MSS may have changed, do a round up again. */ 16170 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16171 } 16172 16173 /* 16174 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16175 * can be applied even before the window scale option is decided. 16176 */ 16177 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16178 if (rwnd > max_transmittable_rwnd) { 16179 rwnd = max_transmittable_rwnd - 16180 (max_transmittable_rwnd % mss); 16181 if (rwnd < mss) 16182 rwnd = max_transmittable_rwnd; 16183 /* 16184 * If we're over the limit we may have to back down tcp_rwnd. 16185 * The increment below won't work for us. So we set all three 16186 * here and the increment below will have no effect. 16187 */ 16188 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16189 } 16190 if (tcp->tcp_localnet) { 16191 tcp->tcp_rack_abs_max = 16192 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16193 } else { 16194 /* 16195 * For a remote host on a different subnet (through a router), 16196 * we ack every other packet to be conforming to RFC1122. 16197 * tcp_deferred_acks_max is default to 2. 16198 */ 16199 tcp->tcp_rack_abs_max = 16200 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16201 } 16202 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16203 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16204 else 16205 tcp->tcp_rack_cur_max = 0; 16206 /* 16207 * Increment the current rwnd by the amount the maximum grew (we 16208 * can not overwrite it since we might be in the middle of a 16209 * connection.) 16210 */ 16211 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16212 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16213 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16214 tcp->tcp_cwnd_max = rwnd; 16215 16216 if (tcp_detached) 16217 return (rwnd); 16218 /* 16219 * We set the maximum receive window into rq->q_hiwat if it is 16220 * a STREAMS socket. 16221 * This is not actually used for flow control. 16222 */ 16223 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16224 tcp->tcp_rq->q_hiwat = rwnd; 16225 tcp->tcp_recv_hiwater = rwnd; 16226 /* 16227 * Set the STREAM head high water mark. This doesn't have to be 16228 * here, since we are simply using default values, but we would 16229 * prefer to choose these values algorithmically, with a likely 16230 * relationship to rwnd. 16231 */ 16232 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16233 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16234 return (rwnd); 16235 } 16236 16237 /* 16238 * Return SNMP stuff in buffer in mpdata. 16239 */ 16240 mblk_t * 16241 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16242 { 16243 mblk_t *mpdata; 16244 mblk_t *mp_conn_ctl = NULL; 16245 mblk_t *mp_conn_tail; 16246 mblk_t *mp_attr_ctl = NULL; 16247 mblk_t *mp_attr_tail; 16248 mblk_t *mp6_conn_ctl = NULL; 16249 mblk_t *mp6_conn_tail; 16250 mblk_t *mp6_attr_ctl = NULL; 16251 mblk_t *mp6_attr_tail; 16252 struct opthdr *optp; 16253 mib2_tcpConnEntry_t tce; 16254 mib2_tcp6ConnEntry_t tce6; 16255 mib2_transportMLPEntry_t mlp; 16256 connf_t *connfp; 16257 int i; 16258 boolean_t ispriv; 16259 zoneid_t zoneid; 16260 int v4_conn_idx; 16261 int v6_conn_idx; 16262 conn_t *connp = Q_TO_CONN(q); 16263 tcp_stack_t *tcps; 16264 ip_stack_t *ipst; 16265 mblk_t *mp2ctl; 16266 16267 /* 16268 * make a copy of the original message 16269 */ 16270 mp2ctl = copymsg(mpctl); 16271 16272 if (mpctl == NULL || 16273 (mpdata = mpctl->b_cont) == NULL || 16274 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16275 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16276 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16277 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16278 freemsg(mp_conn_ctl); 16279 freemsg(mp_attr_ctl); 16280 freemsg(mp6_conn_ctl); 16281 freemsg(mp6_attr_ctl); 16282 freemsg(mpctl); 16283 freemsg(mp2ctl); 16284 return (NULL); 16285 } 16286 16287 ipst = connp->conn_netstack->netstack_ip; 16288 tcps = connp->conn_netstack->netstack_tcp; 16289 16290 /* build table of connections -- need count in fixed part */ 16291 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16292 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16293 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16294 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16295 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16296 16297 ispriv = 16298 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16299 zoneid = Q_TO_CONN(q)->conn_zoneid; 16300 16301 v4_conn_idx = v6_conn_idx = 0; 16302 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16303 16304 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16305 ipst = tcps->tcps_netstack->netstack_ip; 16306 16307 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16308 16309 connp = NULL; 16310 16311 while ((connp = 16312 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16313 tcp_t *tcp; 16314 boolean_t needattr; 16315 16316 if (connp->conn_zoneid != zoneid) 16317 continue; /* not in this zone */ 16318 16319 tcp = connp->conn_tcp; 16320 UPDATE_MIB(&tcps->tcps_mib, 16321 tcpHCInSegs, tcp->tcp_ibsegs); 16322 tcp->tcp_ibsegs = 0; 16323 UPDATE_MIB(&tcps->tcps_mib, 16324 tcpHCOutSegs, tcp->tcp_obsegs); 16325 tcp->tcp_obsegs = 0; 16326 16327 tce6.tcp6ConnState = tce.tcpConnState = 16328 tcp_snmp_state(tcp); 16329 if (tce.tcpConnState == MIB2_TCP_established || 16330 tce.tcpConnState == MIB2_TCP_closeWait) 16331 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16332 16333 needattr = B_FALSE; 16334 bzero(&mlp, sizeof (mlp)); 16335 if (connp->conn_mlp_type != mlptSingle) { 16336 if (connp->conn_mlp_type == mlptShared || 16337 connp->conn_mlp_type == mlptBoth) 16338 mlp.tme_flags |= MIB2_TMEF_SHARED; 16339 if (connp->conn_mlp_type == mlptPrivate || 16340 connp->conn_mlp_type == mlptBoth) 16341 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16342 needattr = B_TRUE; 16343 } 16344 if (connp->conn_peercred != NULL) { 16345 ts_label_t *tsl; 16346 16347 tsl = crgetlabel(connp->conn_peercred); 16348 mlp.tme_doi = label2doi(tsl); 16349 mlp.tme_label = *label2bslabel(tsl); 16350 needattr = B_TRUE; 16351 } 16352 16353 /* Create a message to report on IPv6 entries */ 16354 if (tcp->tcp_ipversion == IPV6_VERSION) { 16355 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16356 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16357 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16358 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16359 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16360 /* Don't want just anybody seeing these... */ 16361 if (ispriv) { 16362 tce6.tcp6ConnEntryInfo.ce_snxt = 16363 tcp->tcp_snxt; 16364 tce6.tcp6ConnEntryInfo.ce_suna = 16365 tcp->tcp_suna; 16366 tce6.tcp6ConnEntryInfo.ce_rnxt = 16367 tcp->tcp_rnxt; 16368 tce6.tcp6ConnEntryInfo.ce_rack = 16369 tcp->tcp_rack; 16370 } else { 16371 /* 16372 * Netstat, unfortunately, uses this to 16373 * get send/receive queue sizes. How to fix? 16374 * Why not compute the difference only? 16375 */ 16376 tce6.tcp6ConnEntryInfo.ce_snxt = 16377 tcp->tcp_snxt - tcp->tcp_suna; 16378 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16379 tce6.tcp6ConnEntryInfo.ce_rnxt = 16380 tcp->tcp_rnxt - tcp->tcp_rack; 16381 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16382 } 16383 16384 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16385 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16386 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16387 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16388 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16389 16390 tce6.tcp6ConnCreationProcess = 16391 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16392 tcp->tcp_cpid; 16393 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16394 16395 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16396 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16397 16398 mlp.tme_connidx = v6_conn_idx++; 16399 if (needattr) 16400 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16401 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16402 } 16403 /* 16404 * Create an IPv4 table entry for IPv4 entries and also 16405 * for IPv6 entries which are bound to in6addr_any 16406 * but don't have IPV6_V6ONLY set. 16407 * (i.e. anything an IPv4 peer could connect to) 16408 */ 16409 if (tcp->tcp_ipversion == IPV4_VERSION || 16410 (tcp->tcp_state <= TCPS_LISTEN && 16411 !tcp->tcp_connp->conn_ipv6_v6only && 16412 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16413 if (tcp->tcp_ipversion == IPV6_VERSION) { 16414 tce.tcpConnRemAddress = INADDR_ANY; 16415 tce.tcpConnLocalAddress = INADDR_ANY; 16416 } else { 16417 tce.tcpConnRemAddress = 16418 tcp->tcp_remote; 16419 tce.tcpConnLocalAddress = 16420 tcp->tcp_ip_src; 16421 } 16422 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16423 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16424 /* Don't want just anybody seeing these... */ 16425 if (ispriv) { 16426 tce.tcpConnEntryInfo.ce_snxt = 16427 tcp->tcp_snxt; 16428 tce.tcpConnEntryInfo.ce_suna = 16429 tcp->tcp_suna; 16430 tce.tcpConnEntryInfo.ce_rnxt = 16431 tcp->tcp_rnxt; 16432 tce.tcpConnEntryInfo.ce_rack = 16433 tcp->tcp_rack; 16434 } else { 16435 /* 16436 * Netstat, unfortunately, uses this to 16437 * get send/receive queue sizes. How 16438 * to fix? 16439 * Why not compute the difference only? 16440 */ 16441 tce.tcpConnEntryInfo.ce_snxt = 16442 tcp->tcp_snxt - tcp->tcp_suna; 16443 tce.tcpConnEntryInfo.ce_suna = 0; 16444 tce.tcpConnEntryInfo.ce_rnxt = 16445 tcp->tcp_rnxt - tcp->tcp_rack; 16446 tce.tcpConnEntryInfo.ce_rack = 0; 16447 } 16448 16449 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16450 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16451 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16452 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16453 tce.tcpConnEntryInfo.ce_state = 16454 tcp->tcp_state; 16455 16456 tce.tcpConnCreationProcess = 16457 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16458 tcp->tcp_cpid; 16459 tce.tcpConnCreationTime = tcp->tcp_open_time; 16460 16461 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16462 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16463 16464 mlp.tme_connidx = v4_conn_idx++; 16465 if (needattr) 16466 (void) snmp_append_data2( 16467 mp_attr_ctl->b_cont, 16468 &mp_attr_tail, (char *)&mlp, 16469 sizeof (mlp)); 16470 } 16471 } 16472 } 16473 16474 /* fixed length structure for IPv4 and IPv6 counters */ 16475 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16476 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16477 sizeof (mib2_tcp6ConnEntry_t)); 16478 /* synchronize 32- and 64-bit counters */ 16479 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16480 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16481 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16482 optp->level = MIB2_TCP; 16483 optp->name = 0; 16484 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16485 sizeof (tcps->tcps_mib)); 16486 optp->len = msgdsize(mpdata); 16487 qreply(q, mpctl); 16488 16489 /* table of connections... */ 16490 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16491 sizeof (struct T_optmgmt_ack)]; 16492 optp->level = MIB2_TCP; 16493 optp->name = MIB2_TCP_CONN; 16494 optp->len = msgdsize(mp_conn_ctl->b_cont); 16495 qreply(q, mp_conn_ctl); 16496 16497 /* table of MLP attributes... */ 16498 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16499 sizeof (struct T_optmgmt_ack)]; 16500 optp->level = MIB2_TCP; 16501 optp->name = EXPER_XPORT_MLP; 16502 optp->len = msgdsize(mp_attr_ctl->b_cont); 16503 if (optp->len == 0) 16504 freemsg(mp_attr_ctl); 16505 else 16506 qreply(q, mp_attr_ctl); 16507 16508 /* table of IPv6 connections... */ 16509 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16510 sizeof (struct T_optmgmt_ack)]; 16511 optp->level = MIB2_TCP6; 16512 optp->name = MIB2_TCP6_CONN; 16513 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16514 qreply(q, mp6_conn_ctl); 16515 16516 /* table of IPv6 MLP attributes... */ 16517 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16518 sizeof (struct T_optmgmt_ack)]; 16519 optp->level = MIB2_TCP6; 16520 optp->name = EXPER_XPORT_MLP; 16521 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16522 if (optp->len == 0) 16523 freemsg(mp6_attr_ctl); 16524 else 16525 qreply(q, mp6_attr_ctl); 16526 return (mp2ctl); 16527 } 16528 16529 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16530 /* ARGSUSED */ 16531 int 16532 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16533 { 16534 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16535 16536 switch (level) { 16537 case MIB2_TCP: 16538 switch (name) { 16539 case 13: 16540 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16541 return (0); 16542 /* TODO: delete entry defined by tce */ 16543 return (1); 16544 default: 16545 return (0); 16546 } 16547 default: 16548 return (1); 16549 } 16550 } 16551 16552 /* Translate TCP state to MIB2 TCP state. */ 16553 static int 16554 tcp_snmp_state(tcp_t *tcp) 16555 { 16556 if (tcp == NULL) 16557 return (0); 16558 16559 switch (tcp->tcp_state) { 16560 case TCPS_CLOSED: 16561 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16562 case TCPS_BOUND: 16563 return (MIB2_TCP_closed); 16564 case TCPS_LISTEN: 16565 return (MIB2_TCP_listen); 16566 case TCPS_SYN_SENT: 16567 return (MIB2_TCP_synSent); 16568 case TCPS_SYN_RCVD: 16569 return (MIB2_TCP_synReceived); 16570 case TCPS_ESTABLISHED: 16571 return (MIB2_TCP_established); 16572 case TCPS_CLOSE_WAIT: 16573 return (MIB2_TCP_closeWait); 16574 case TCPS_FIN_WAIT_1: 16575 return (MIB2_TCP_finWait1); 16576 case TCPS_CLOSING: 16577 return (MIB2_TCP_closing); 16578 case TCPS_LAST_ACK: 16579 return (MIB2_TCP_lastAck); 16580 case TCPS_FIN_WAIT_2: 16581 return (MIB2_TCP_finWait2); 16582 case TCPS_TIME_WAIT: 16583 return (MIB2_TCP_timeWait); 16584 default: 16585 return (0); 16586 } 16587 } 16588 16589 static char tcp_report_header[] = 16590 "TCP " MI_COL_HDRPAD_STR 16591 "zone dest snxt suna " 16592 "swnd rnxt rack rwnd rto mss w sw rw t " 16593 "recent [lport,fport] state"; 16594 16595 /* 16596 * TCP status report triggered via the Named Dispatch mechanism. 16597 */ 16598 /* ARGSUSED */ 16599 static void 16600 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16601 cred_t *cr) 16602 { 16603 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16604 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16605 char cflag; 16606 in6_addr_t v6dst; 16607 char buf[80]; 16608 uint_t print_len, buf_len; 16609 16610 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16611 if (buf_len <= 0) 16612 return; 16613 16614 if (hashval >= 0) 16615 (void) sprintf(hash, "%03d ", hashval); 16616 else 16617 hash[0] = '\0'; 16618 16619 /* 16620 * Note that we use the remote address in the tcp_b structure. 16621 * This means that it will print out the real destination address, 16622 * not the next hop's address if source routing is used. This 16623 * avoid the confusion on the output because user may not 16624 * know that source routing is used for a connection. 16625 */ 16626 if (tcp->tcp_ipversion == IPV4_VERSION) { 16627 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16628 } else { 16629 v6dst = tcp->tcp_remote_v6; 16630 } 16631 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16632 /* 16633 * the ispriv checks are so that normal users cannot determine 16634 * sequence number information using NDD. 16635 */ 16636 16637 if (TCP_IS_DETACHED(tcp)) 16638 cflag = '*'; 16639 else 16640 cflag = ' '; 16641 print_len = snprintf((char *)mp->b_wptr, buf_len, 16642 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16643 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16644 hash, 16645 (void *)tcp, 16646 tcp->tcp_connp->conn_zoneid, 16647 addrbuf, 16648 (ispriv) ? tcp->tcp_snxt : 0, 16649 (ispriv) ? tcp->tcp_suna : 0, 16650 tcp->tcp_swnd, 16651 (ispriv) ? tcp->tcp_rnxt : 0, 16652 (ispriv) ? tcp->tcp_rack : 0, 16653 tcp->tcp_rwnd, 16654 tcp->tcp_rto, 16655 tcp->tcp_mss, 16656 tcp->tcp_snd_ws_ok, 16657 tcp->tcp_snd_ws, 16658 tcp->tcp_rcv_ws, 16659 tcp->tcp_snd_ts_ok, 16660 tcp->tcp_ts_recent, 16661 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16662 if (print_len < buf_len) { 16663 ((mblk_t *)mp)->b_wptr += print_len; 16664 } else { 16665 ((mblk_t *)mp)->b_wptr += buf_len; 16666 } 16667 } 16668 16669 /* 16670 * TCP status report (for listeners only) triggered via the Named Dispatch 16671 * mechanism. 16672 */ 16673 /* ARGSUSED */ 16674 static void 16675 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16676 { 16677 char addrbuf[INET6_ADDRSTRLEN]; 16678 in6_addr_t v6dst; 16679 uint_t print_len, buf_len; 16680 16681 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16682 if (buf_len <= 0) 16683 return; 16684 16685 if (tcp->tcp_ipversion == IPV4_VERSION) { 16686 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16687 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16688 } else { 16689 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16690 addrbuf, sizeof (addrbuf)); 16691 } 16692 print_len = snprintf((char *)mp->b_wptr, buf_len, 16693 "%03d " 16694 MI_COL_PTRFMT_STR 16695 "%d %s %05u %08u %d/%d/%d%c\n", 16696 hashval, (void *)tcp, 16697 tcp->tcp_connp->conn_zoneid, 16698 addrbuf, 16699 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16700 tcp->tcp_conn_req_seqnum, 16701 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16702 tcp->tcp_conn_req_max, 16703 tcp->tcp_syn_defense ? '*' : ' '); 16704 if (print_len < buf_len) { 16705 ((mblk_t *)mp)->b_wptr += print_len; 16706 } else { 16707 ((mblk_t *)mp)->b_wptr += buf_len; 16708 } 16709 } 16710 16711 /* TCP status report triggered via the Named Dispatch mechanism. */ 16712 /* ARGSUSED */ 16713 static int 16714 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16715 { 16716 tcp_t *tcp; 16717 int i; 16718 conn_t *connp; 16719 connf_t *connfp; 16720 zoneid_t zoneid; 16721 tcp_stack_t *tcps; 16722 ip_stack_t *ipst; 16723 16724 zoneid = Q_TO_CONN(q)->conn_zoneid; 16725 tcps = Q_TO_TCP(q)->tcp_tcps; 16726 16727 /* 16728 * Because of the ndd constraint, at most we can have 64K buffer 16729 * to put in all TCP info. So to be more efficient, just 16730 * allocate a 64K buffer here, assuming we need that large buffer. 16731 * This may be a problem as any user can read tcp_status. Therefore 16732 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16733 * This should be OK as normal users should not do this too often. 16734 */ 16735 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16736 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16737 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16738 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16739 return (0); 16740 } 16741 } 16742 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16743 /* The following may work even if we cannot get a large buf. */ 16744 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16745 return (0); 16746 } 16747 16748 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16749 16750 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16751 16752 ipst = tcps->tcps_netstack->netstack_ip; 16753 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16754 16755 connp = NULL; 16756 16757 while ((connp = 16758 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16759 tcp = connp->conn_tcp; 16760 if (zoneid != GLOBAL_ZONEID && 16761 zoneid != connp->conn_zoneid) 16762 continue; 16763 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16764 cr); 16765 } 16766 16767 } 16768 16769 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16770 return (0); 16771 } 16772 16773 /* TCP status report triggered via the Named Dispatch mechanism. */ 16774 /* ARGSUSED */ 16775 static int 16776 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16777 { 16778 tf_t *tbf; 16779 tcp_t *tcp, *ltcp; 16780 int i; 16781 zoneid_t zoneid; 16782 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16783 16784 zoneid = Q_TO_CONN(q)->conn_zoneid; 16785 16786 /* Refer to comments in tcp_status_report(). */ 16787 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16788 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16789 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16790 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16791 return (0); 16792 } 16793 } 16794 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16795 /* The following may work even if we cannot get a large buf. */ 16796 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16797 return (0); 16798 } 16799 16800 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16801 16802 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16803 tbf = &tcps->tcps_bind_fanout[i]; 16804 mutex_enter(&tbf->tf_lock); 16805 for (ltcp = tbf->tf_tcp; ltcp != NULL; 16806 ltcp = ltcp->tcp_bind_hash) { 16807 for (tcp = ltcp; tcp != NULL; 16808 tcp = tcp->tcp_bind_hash_port) { 16809 if (zoneid != GLOBAL_ZONEID && 16810 zoneid != tcp->tcp_connp->conn_zoneid) 16811 continue; 16812 CONN_INC_REF(tcp->tcp_connp); 16813 tcp_report_item(mp->b_cont, tcp, i, 16814 Q_TO_TCP(q), cr); 16815 CONN_DEC_REF(tcp->tcp_connp); 16816 } 16817 } 16818 mutex_exit(&tbf->tf_lock); 16819 } 16820 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16821 return (0); 16822 } 16823 16824 /* TCP status report triggered via the Named Dispatch mechanism. */ 16825 /* ARGSUSED */ 16826 static int 16827 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16828 { 16829 connf_t *connfp; 16830 conn_t *connp; 16831 tcp_t *tcp; 16832 int i; 16833 zoneid_t zoneid; 16834 tcp_stack_t *tcps; 16835 ip_stack_t *ipst; 16836 16837 zoneid = Q_TO_CONN(q)->conn_zoneid; 16838 tcps = Q_TO_TCP(q)->tcp_tcps; 16839 16840 /* Refer to comments in tcp_status_report(). */ 16841 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16842 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16843 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16844 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16845 return (0); 16846 } 16847 } 16848 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16849 /* The following may work even if we cannot get a large buf. */ 16850 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16851 return (0); 16852 } 16853 16854 (void) mi_mpprintf(mp, 16855 " TCP " MI_COL_HDRPAD_STR 16856 "zone IP addr port seqnum backlog (q0/q/max)"); 16857 16858 ipst = tcps->tcps_netstack->netstack_ip; 16859 16860 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16861 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16862 connp = NULL; 16863 while ((connp = 16864 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16865 tcp = connp->conn_tcp; 16866 if (zoneid != GLOBAL_ZONEID && 16867 zoneid != connp->conn_zoneid) 16868 continue; 16869 tcp_report_listener(mp->b_cont, tcp, i); 16870 } 16871 } 16872 16873 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16874 return (0); 16875 } 16876 16877 /* TCP status report triggered via the Named Dispatch mechanism. */ 16878 /* ARGSUSED */ 16879 static int 16880 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16881 { 16882 connf_t *connfp; 16883 conn_t *connp; 16884 tcp_t *tcp; 16885 int i; 16886 zoneid_t zoneid; 16887 tcp_stack_t *tcps; 16888 ip_stack_t *ipst; 16889 16890 zoneid = Q_TO_CONN(q)->conn_zoneid; 16891 tcps = Q_TO_TCP(q)->tcp_tcps; 16892 ipst = tcps->tcps_netstack->netstack_ip; 16893 16894 /* Refer to comments in tcp_status_report(). */ 16895 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16896 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16897 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16898 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16899 return (0); 16900 } 16901 } 16902 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16903 /* The following may work even if we cannot get a large buf. */ 16904 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16905 return (0); 16906 } 16907 16908 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16909 ipst->ips_ipcl_conn_fanout_size); 16910 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16911 16912 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16913 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16914 connp = NULL; 16915 while ((connp = 16916 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16917 tcp = connp->conn_tcp; 16918 if (zoneid != GLOBAL_ZONEID && 16919 zoneid != connp->conn_zoneid) 16920 continue; 16921 tcp_report_item(mp->b_cont, tcp, i, 16922 Q_TO_TCP(q), cr); 16923 } 16924 } 16925 16926 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16927 return (0); 16928 } 16929 16930 /* TCP status report triggered via the Named Dispatch mechanism. */ 16931 /* ARGSUSED */ 16932 static int 16933 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16934 { 16935 tf_t *tf; 16936 tcp_t *tcp; 16937 int i; 16938 zoneid_t zoneid; 16939 tcp_stack_t *tcps; 16940 16941 zoneid = Q_TO_CONN(q)->conn_zoneid; 16942 tcps = Q_TO_TCP(q)->tcp_tcps; 16943 16944 /* Refer to comments in tcp_status_report(). */ 16945 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16946 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16947 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16948 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16949 return (0); 16950 } 16951 } 16952 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16953 /* The following may work even if we cannot get a large buf. */ 16954 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16955 return (0); 16956 } 16957 16958 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16959 16960 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16961 tf = &tcps->tcps_acceptor_fanout[i]; 16962 mutex_enter(&tf->tf_lock); 16963 for (tcp = tf->tf_tcp; tcp != NULL; 16964 tcp = tcp->tcp_acceptor_hash) { 16965 if (zoneid != GLOBAL_ZONEID && 16966 zoneid != tcp->tcp_connp->conn_zoneid) 16967 continue; 16968 tcp_report_item(mp->b_cont, tcp, i, 16969 Q_TO_TCP(q), cr); 16970 } 16971 mutex_exit(&tf->tf_lock); 16972 } 16973 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16974 return (0); 16975 } 16976 16977 /* 16978 * tcp_timer is the timer service routine. It handles the retransmission, 16979 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16980 * from the state of the tcp instance what kind of action needs to be done 16981 * at the time it is called. 16982 */ 16983 static void 16984 tcp_timer(void *arg) 16985 { 16986 mblk_t *mp; 16987 clock_t first_threshold; 16988 clock_t second_threshold; 16989 clock_t ms; 16990 uint32_t mss; 16991 conn_t *connp = (conn_t *)arg; 16992 tcp_t *tcp = connp->conn_tcp; 16993 tcp_stack_t *tcps = tcp->tcp_tcps; 16994 16995 tcp->tcp_timer_tid = 0; 16996 16997 if (tcp->tcp_fused) 16998 return; 16999 17000 first_threshold = tcp->tcp_first_timer_threshold; 17001 second_threshold = tcp->tcp_second_timer_threshold; 17002 switch (tcp->tcp_state) { 17003 case TCPS_IDLE: 17004 case TCPS_BOUND: 17005 case TCPS_LISTEN: 17006 return; 17007 case TCPS_SYN_RCVD: { 17008 tcp_t *listener = tcp->tcp_listener; 17009 17010 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 17011 ASSERT(tcp->tcp_rq == listener->tcp_rq); 17012 /* it's our first timeout */ 17013 tcp->tcp_syn_rcvd_timeout = 1; 17014 mutex_enter(&listener->tcp_eager_lock); 17015 listener->tcp_syn_rcvd_timeout++; 17016 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 17017 /* 17018 * Make this eager available for drop if we 17019 * need to drop one to accomodate a new 17020 * incoming SYN request. 17021 */ 17022 MAKE_DROPPABLE(listener, tcp); 17023 } 17024 if (!listener->tcp_syn_defense && 17025 (listener->tcp_syn_rcvd_timeout > 17026 (tcps->tcps_conn_req_max_q0 >> 2)) && 17027 (tcps->tcps_conn_req_max_q0 > 200)) { 17028 /* We may be under attack. Put on a defense. */ 17029 listener->tcp_syn_defense = B_TRUE; 17030 cmn_err(CE_WARN, "High TCP connect timeout " 17031 "rate! System (port %d) may be under a " 17032 "SYN flood attack!", 17033 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17034 17035 listener->tcp_ip_addr_cache = kmem_zalloc( 17036 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17037 KM_NOSLEEP); 17038 } 17039 mutex_exit(&listener->tcp_eager_lock); 17040 } else if (listener != NULL) { 17041 mutex_enter(&listener->tcp_eager_lock); 17042 tcp->tcp_syn_rcvd_timeout++; 17043 if (tcp->tcp_syn_rcvd_timeout > 1 && 17044 !tcp->tcp_closemp_used) { 17045 /* 17046 * This is our second timeout. Put the tcp in 17047 * the list of droppable eagers to allow it to 17048 * be dropped, if needed. We don't check 17049 * whether tcp_dontdrop is set or not to 17050 * protect ourselve from a SYN attack where a 17051 * remote host can spoof itself as one of the 17052 * good IP source and continue to hold 17053 * resources too long. 17054 */ 17055 MAKE_DROPPABLE(listener, tcp); 17056 } 17057 mutex_exit(&listener->tcp_eager_lock); 17058 } 17059 } 17060 /* FALLTHRU */ 17061 case TCPS_SYN_SENT: 17062 first_threshold = tcp->tcp_first_ctimer_threshold; 17063 second_threshold = tcp->tcp_second_ctimer_threshold; 17064 break; 17065 case TCPS_ESTABLISHED: 17066 case TCPS_FIN_WAIT_1: 17067 case TCPS_CLOSING: 17068 case TCPS_CLOSE_WAIT: 17069 case TCPS_LAST_ACK: 17070 /* If we have data to rexmit */ 17071 if (tcp->tcp_suna != tcp->tcp_snxt) { 17072 clock_t time_to_wait; 17073 17074 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17075 if (!tcp->tcp_xmit_head) 17076 break; 17077 time_to_wait = lbolt - 17078 (clock_t)tcp->tcp_xmit_head->b_prev; 17079 time_to_wait = tcp->tcp_rto - 17080 TICK_TO_MSEC(time_to_wait); 17081 /* 17082 * If the timer fires too early, 1 clock tick earlier, 17083 * restart the timer. 17084 */ 17085 if (time_to_wait > msec_per_tick) { 17086 TCP_STAT(tcps, tcp_timer_fire_early); 17087 TCP_TIMER_RESTART(tcp, time_to_wait); 17088 return; 17089 } 17090 /* 17091 * When we probe zero windows, we force the swnd open. 17092 * If our peer acks with a closed window swnd will be 17093 * set to zero by tcp_rput(). As long as we are 17094 * receiving acks tcp_rput will 17095 * reset 'tcp_ms_we_have_waited' so as not to trip the 17096 * first and second interval actions. NOTE: the timer 17097 * interval is allowed to continue its exponential 17098 * backoff. 17099 */ 17100 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17101 if (tcp->tcp_debug) { 17102 (void) strlog(TCP_MOD_ID, 0, 1, 17103 SL_TRACE, "tcp_timer: zero win"); 17104 } 17105 } else { 17106 /* 17107 * After retransmission, we need to do 17108 * slow start. Set the ssthresh to one 17109 * half of current effective window and 17110 * cwnd to one MSS. Also reset 17111 * tcp_cwnd_cnt. 17112 * 17113 * Note that if tcp_ssthresh is reduced because 17114 * of ECN, do not reduce it again unless it is 17115 * already one window of data away (tcp_cwr 17116 * should then be cleared) or this is a 17117 * timeout for a retransmitted segment. 17118 */ 17119 uint32_t npkt; 17120 17121 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17122 npkt = ((tcp->tcp_timer_backoff ? 17123 tcp->tcp_cwnd_ssthresh : 17124 tcp->tcp_snxt - 17125 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17126 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17127 tcp->tcp_mss; 17128 } 17129 tcp->tcp_cwnd = tcp->tcp_mss; 17130 tcp->tcp_cwnd_cnt = 0; 17131 if (tcp->tcp_ecn_ok) { 17132 tcp->tcp_cwr = B_TRUE; 17133 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17134 tcp->tcp_ecn_cwr_sent = B_FALSE; 17135 } 17136 } 17137 break; 17138 } 17139 /* 17140 * We have something to send yet we cannot send. The 17141 * reason can be: 17142 * 17143 * 1. Zero send window: we need to do zero window probe. 17144 * 2. Zero cwnd: because of ECN, we need to "clock out 17145 * segments. 17146 * 3. SWS avoidance: receiver may have shrunk window, 17147 * reset our knowledge. 17148 * 17149 * Note that condition 2 can happen with either 1 or 17150 * 3. But 1 and 3 are exclusive. 17151 */ 17152 if (tcp->tcp_unsent != 0) { 17153 if (tcp->tcp_cwnd == 0) { 17154 /* 17155 * Set tcp_cwnd to 1 MSS so that a 17156 * new segment can be sent out. We 17157 * are "clocking out" new data when 17158 * the network is really congested. 17159 */ 17160 ASSERT(tcp->tcp_ecn_ok); 17161 tcp->tcp_cwnd = tcp->tcp_mss; 17162 } 17163 if (tcp->tcp_swnd == 0) { 17164 /* Extend window for zero window probe */ 17165 tcp->tcp_swnd++; 17166 tcp->tcp_zero_win_probe = B_TRUE; 17167 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17168 } else { 17169 /* 17170 * Handle timeout from sender SWS avoidance. 17171 * Reset our knowledge of the max send window 17172 * since the receiver might have reduced its 17173 * receive buffer. Avoid setting tcp_max_swnd 17174 * to one since that will essentially disable 17175 * the SWS checks. 17176 * 17177 * Note that since we don't have a SWS 17178 * state variable, if the timeout is set 17179 * for ECN but not for SWS, this 17180 * code will also be executed. This is 17181 * fine as tcp_max_swnd is updated 17182 * constantly and it will not affect 17183 * anything. 17184 */ 17185 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17186 } 17187 tcp_wput_data(tcp, NULL, B_FALSE); 17188 return; 17189 } 17190 /* Is there a FIN that needs to be to re retransmitted? */ 17191 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17192 !tcp->tcp_fin_acked) 17193 break; 17194 /* Nothing to do, return without restarting timer. */ 17195 TCP_STAT(tcps, tcp_timer_fire_miss); 17196 return; 17197 case TCPS_FIN_WAIT_2: 17198 /* 17199 * User closed the TCP endpoint and peer ACK'ed our FIN. 17200 * We waited some time for for peer's FIN, but it hasn't 17201 * arrived. We flush the connection now to avoid 17202 * case where the peer has rebooted. 17203 */ 17204 if (TCP_IS_DETACHED(tcp)) { 17205 (void) tcp_clean_death(tcp, 0, 23); 17206 } else { 17207 TCP_TIMER_RESTART(tcp, 17208 tcps->tcps_fin_wait_2_flush_interval); 17209 } 17210 return; 17211 case TCPS_TIME_WAIT: 17212 (void) tcp_clean_death(tcp, 0, 24); 17213 return; 17214 default: 17215 if (tcp->tcp_debug) { 17216 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17217 "tcp_timer: strange state (%d) %s", 17218 tcp->tcp_state, tcp_display(tcp, NULL, 17219 DISP_PORT_ONLY)); 17220 } 17221 return; 17222 } 17223 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17224 /* 17225 * For zero window probe, we need to send indefinitely, 17226 * unless we have not heard from the other side for some 17227 * time... 17228 */ 17229 if ((tcp->tcp_zero_win_probe == 0) || 17230 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17231 second_threshold)) { 17232 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17233 /* 17234 * If TCP is in SYN_RCVD state, send back a 17235 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17236 * should be zero in TCPS_SYN_RCVD state. 17237 */ 17238 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17239 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17240 "in SYN_RCVD", 17241 tcp, tcp->tcp_snxt, 17242 tcp->tcp_rnxt, TH_RST | TH_ACK); 17243 } 17244 (void) tcp_clean_death(tcp, 17245 tcp->tcp_client_errno ? 17246 tcp->tcp_client_errno : ETIMEDOUT, 25); 17247 return; 17248 } else { 17249 /* 17250 * Set tcp_ms_we_have_waited to second_threshold 17251 * so that in next timeout, we will do the above 17252 * check (lbolt - tcp_last_recv_time). This is 17253 * also to avoid overflow. 17254 * 17255 * We don't need to decrement tcp_timer_backoff 17256 * to avoid overflow because it will be decremented 17257 * later if new timeout value is greater than 17258 * tcp_rexmit_interval_max. In the case when 17259 * tcp_rexmit_interval_max is greater than 17260 * second_threshold, it means that we will wait 17261 * longer than second_threshold to send the next 17262 * window probe. 17263 */ 17264 tcp->tcp_ms_we_have_waited = second_threshold; 17265 } 17266 } else if (ms > first_threshold) { 17267 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17268 tcp->tcp_xmit_head != NULL) { 17269 tcp->tcp_xmit_head = 17270 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17271 } 17272 /* 17273 * We have been retransmitting for too long... The RTT 17274 * we calculated is probably incorrect. Reinitialize it. 17275 * Need to compensate for 0 tcp_rtt_sa. Reset 17276 * tcp_rtt_update so that we won't accidentally cache a 17277 * bad value. But only do this if this is not a zero 17278 * window probe. 17279 */ 17280 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17281 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17282 (tcp->tcp_rtt_sa >> 5); 17283 tcp->tcp_rtt_sa = 0; 17284 tcp_ip_notify(tcp); 17285 tcp->tcp_rtt_update = 0; 17286 } 17287 } 17288 tcp->tcp_timer_backoff++; 17289 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17290 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17291 tcps->tcps_rexmit_interval_min) { 17292 /* 17293 * This means the original RTO is tcp_rexmit_interval_min. 17294 * So we will use tcp_rexmit_interval_min as the RTO value 17295 * and do the backoff. 17296 */ 17297 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17298 } else { 17299 ms <<= tcp->tcp_timer_backoff; 17300 } 17301 if (ms > tcps->tcps_rexmit_interval_max) { 17302 ms = tcps->tcps_rexmit_interval_max; 17303 /* 17304 * ms is at max, decrement tcp_timer_backoff to avoid 17305 * overflow. 17306 */ 17307 tcp->tcp_timer_backoff--; 17308 } 17309 tcp->tcp_ms_we_have_waited += ms; 17310 if (tcp->tcp_zero_win_probe == 0) { 17311 tcp->tcp_rto = ms; 17312 } 17313 TCP_TIMER_RESTART(tcp, ms); 17314 /* 17315 * This is after a timeout and tcp_rto is backed off. Set 17316 * tcp_set_timer to 1 so that next time RTO is updated, we will 17317 * restart the timer with a correct value. 17318 */ 17319 tcp->tcp_set_timer = 1; 17320 mss = tcp->tcp_snxt - tcp->tcp_suna; 17321 if (mss > tcp->tcp_mss) 17322 mss = tcp->tcp_mss; 17323 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17324 mss = tcp->tcp_swnd; 17325 17326 if ((mp = tcp->tcp_xmit_head) != NULL) 17327 mp->b_prev = (mblk_t *)lbolt; 17328 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17329 B_TRUE); 17330 17331 /* 17332 * When slow start after retransmission begins, start with 17333 * this seq no. tcp_rexmit_max marks the end of special slow 17334 * start phase. tcp_snd_burst controls how many segments 17335 * can be sent because of an ack. 17336 */ 17337 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17338 tcp->tcp_snd_burst = TCP_CWND_SS; 17339 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17340 (tcp->tcp_unsent == 0)) { 17341 tcp->tcp_rexmit_max = tcp->tcp_fss; 17342 } else { 17343 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17344 } 17345 tcp->tcp_rexmit = B_TRUE; 17346 tcp->tcp_dupack_cnt = 0; 17347 17348 /* 17349 * Remove all rexmit SACK blk to start from fresh. 17350 */ 17351 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17352 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17353 tcp->tcp_num_notsack_blk = 0; 17354 tcp->tcp_cnt_notsack_list = 0; 17355 } 17356 if (mp == NULL) { 17357 return; 17358 } 17359 /* Attach credentials to retransmitted initial SYNs. */ 17360 if (tcp->tcp_state == TCPS_SYN_SENT) { 17361 mblk_setcred(mp, tcp->tcp_cred); 17362 DB_CPID(mp) = tcp->tcp_cpid; 17363 } 17364 17365 tcp->tcp_csuna = tcp->tcp_snxt; 17366 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17367 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17368 tcp_send_data(tcp, tcp->tcp_wq, mp); 17369 17370 } 17371 17372 static int 17373 tcp_do_unbind(conn_t *connp) 17374 { 17375 tcp_t *tcp = connp->conn_tcp; 17376 int error = 0; 17377 17378 switch (tcp->tcp_state) { 17379 case TCPS_BOUND: 17380 case TCPS_LISTEN: 17381 break; 17382 default: 17383 return (-TOUTSTATE); 17384 } 17385 17386 /* 17387 * Need to clean up all the eagers since after the unbind, segments 17388 * will no longer be delivered to this listener stream. 17389 */ 17390 mutex_enter(&tcp->tcp_eager_lock); 17391 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17392 tcp_eager_cleanup(tcp, 0); 17393 } 17394 mutex_exit(&tcp->tcp_eager_lock); 17395 17396 if (tcp->tcp_ipversion == IPV4_VERSION) { 17397 tcp->tcp_ipha->ipha_src = 0; 17398 } else { 17399 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17400 } 17401 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17402 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17403 tcp_bind_hash_remove(tcp); 17404 tcp->tcp_state = TCPS_IDLE; 17405 tcp->tcp_mdt = B_FALSE; 17406 17407 connp = tcp->tcp_connp; 17408 connp->conn_mdt_ok = B_FALSE; 17409 ipcl_hash_remove(connp); 17410 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17411 17412 return (error); 17413 } 17414 17415 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17416 static void 17417 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 17418 { 17419 int error = tcp_do_unbind(tcp->tcp_connp); 17420 17421 if (error > 0) { 17422 tcp_err_ack(tcp, mp, TSYSERR, error); 17423 } else if (error < 0) { 17424 tcp_err_ack(tcp, mp, -error, 0); 17425 } else { 17426 /* Send M_FLUSH according to TPI */ 17427 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17428 17429 mp = mi_tpi_ok_ack_alloc(mp); 17430 putnext(tcp->tcp_rq, mp); 17431 } 17432 } 17433 17434 /* 17435 * Don't let port fall into the privileged range. 17436 * Since the extra privileged ports can be arbitrary we also 17437 * ensure that we exclude those from consideration. 17438 * tcp_g_epriv_ports is not sorted thus we loop over it until 17439 * there are no changes. 17440 * 17441 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17442 * but instead the code relies on: 17443 * - the fact that the address of the array and its size never changes 17444 * - the atomic assignment of the elements of the array 17445 * 17446 * Returns 0 if there are no more ports available. 17447 * 17448 * TS note: skip multilevel ports. 17449 */ 17450 static in_port_t 17451 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17452 { 17453 int i; 17454 boolean_t restart = B_FALSE; 17455 tcp_stack_t *tcps = tcp->tcp_tcps; 17456 17457 if (random && tcp_random_anon_port != 0) { 17458 (void) random_get_pseudo_bytes((uint8_t *)&port, 17459 sizeof (in_port_t)); 17460 /* 17461 * Unless changed by a sys admin, the smallest anon port 17462 * is 32768 and the largest anon port is 65535. It is 17463 * very likely (50%) for the random port to be smaller 17464 * than the smallest anon port. When that happens, 17465 * add port % (anon port range) to the smallest anon 17466 * port to get the random port. It should fall into the 17467 * valid anon port range. 17468 */ 17469 if (port < tcps->tcps_smallest_anon_port) { 17470 port = tcps->tcps_smallest_anon_port + 17471 port % (tcps->tcps_largest_anon_port - 17472 tcps->tcps_smallest_anon_port); 17473 } 17474 } 17475 17476 retry: 17477 if (port < tcps->tcps_smallest_anon_port) 17478 port = (in_port_t)tcps->tcps_smallest_anon_port; 17479 17480 if (port > tcps->tcps_largest_anon_port) { 17481 if (restart) 17482 return (0); 17483 restart = B_TRUE; 17484 port = (in_port_t)tcps->tcps_smallest_anon_port; 17485 } 17486 17487 if (port < tcps->tcps_smallest_nonpriv_port) 17488 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17489 17490 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17491 if (port == tcps->tcps_g_epriv_ports[i]) { 17492 port++; 17493 /* 17494 * Make sure whether the port is in the 17495 * valid range. 17496 */ 17497 goto retry; 17498 } 17499 } 17500 if (is_system_labeled() && 17501 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17502 IPPROTO_TCP, B_TRUE)) != 0) { 17503 port = i; 17504 goto retry; 17505 } 17506 return (port); 17507 } 17508 17509 /* 17510 * Return the next anonymous port in the privileged port range for 17511 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17512 * downwards. This is the same behavior as documented in the userland 17513 * library call rresvport(3N). 17514 * 17515 * TS note: skip multilevel ports. 17516 */ 17517 static in_port_t 17518 tcp_get_next_priv_port(const tcp_t *tcp) 17519 { 17520 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17521 in_port_t nextport; 17522 boolean_t restart = B_FALSE; 17523 tcp_stack_t *tcps = tcp->tcp_tcps; 17524 retry: 17525 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17526 next_priv_port >= IPPORT_RESERVED) { 17527 next_priv_port = IPPORT_RESERVED - 1; 17528 if (restart) 17529 return (0); 17530 restart = B_TRUE; 17531 } 17532 if (is_system_labeled() && 17533 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17534 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17535 next_priv_port = nextport; 17536 goto retry; 17537 } 17538 return (next_priv_port--); 17539 } 17540 17541 /* The write side r/w procedure. */ 17542 17543 #if CCS_STATS 17544 struct { 17545 struct { 17546 int64_t count, bytes; 17547 } tot, hit; 17548 } wrw_stats; 17549 #endif 17550 17551 /* 17552 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17553 * messages. 17554 */ 17555 /* ARGSUSED */ 17556 static void 17557 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17558 { 17559 conn_t *connp = (conn_t *)arg; 17560 tcp_t *tcp = connp->conn_tcp; 17561 queue_t *q = tcp->tcp_wq; 17562 17563 ASSERT(DB_TYPE(mp) != M_IOCTL); 17564 /* 17565 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17566 * Once the close starts, streamhead and sockfs will not let any data 17567 * packets come down (close ensures that there are no threads using the 17568 * queue and no new threads will come down) but since qprocsoff() 17569 * hasn't happened yet, a M_FLUSH or some non data message might 17570 * get reflected back (in response to our own FLUSHRW) and get 17571 * processed after tcp_close() is done. The conn would still be valid 17572 * because a ref would have added but we need to check the state 17573 * before actually processing the packet. 17574 */ 17575 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17576 freemsg(mp); 17577 return; 17578 } 17579 17580 switch (DB_TYPE(mp)) { 17581 case M_IOCDATA: 17582 tcp_wput_iocdata(tcp, mp); 17583 break; 17584 case M_FLUSH: 17585 tcp_wput_flush(tcp, mp); 17586 break; 17587 default: 17588 CALL_IP_WPUT(connp, q, mp); 17589 break; 17590 } 17591 } 17592 17593 /* 17594 * The TCP fast path write put procedure. 17595 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17596 */ 17597 /* ARGSUSED */ 17598 void 17599 tcp_output(void *arg, mblk_t *mp, void *arg2) 17600 { 17601 int len; 17602 int hdrlen; 17603 int plen; 17604 mblk_t *mp1; 17605 uchar_t *rptr; 17606 uint32_t snxt; 17607 tcph_t *tcph; 17608 struct datab *db; 17609 uint32_t suna; 17610 uint32_t mss; 17611 ipaddr_t *dst; 17612 ipaddr_t *src; 17613 uint32_t sum; 17614 int usable; 17615 conn_t *connp = (conn_t *)arg; 17616 tcp_t *tcp = connp->conn_tcp; 17617 uint32_t msize; 17618 tcp_stack_t *tcps = tcp->tcp_tcps; 17619 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17620 17621 /* 17622 * Try and ASSERT the minimum possible references on the 17623 * conn early enough. Since we are executing on write side, 17624 * the connection is obviously not detached and that means 17625 * there is a ref each for TCP and IP. Since we are behind 17626 * the squeue, the minimum references needed are 3. If the 17627 * conn is in classifier hash list, there should be an 17628 * extra ref for that (we check both the possibilities). 17629 */ 17630 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17631 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17632 17633 ASSERT(DB_TYPE(mp) == M_DATA); 17634 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17635 17636 mutex_enter(&tcp->tcp_non_sq_lock); 17637 tcp->tcp_squeue_bytes -= msize; 17638 mutex_exit(&tcp->tcp_non_sq_lock); 17639 17640 /* Check to see if this connection wants to be re-fused. */ 17641 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17642 if (tcp->tcp_ipversion == IPV4_VERSION) { 17643 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17644 &tcp->tcp_saved_tcph); 17645 } else { 17646 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17647 &tcp->tcp_saved_tcph); 17648 } 17649 } 17650 /* Bypass tcp protocol for fused tcp loopback */ 17651 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17652 return; 17653 17654 mss = tcp->tcp_mss; 17655 if (tcp->tcp_xmit_zc_clean) 17656 mp = tcp_zcopy_backoff(tcp, mp, 0); 17657 17658 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17659 len = (int)(mp->b_wptr - mp->b_rptr); 17660 17661 /* 17662 * Criteria for fast path: 17663 * 17664 * 1. no unsent data 17665 * 2. single mblk in request 17666 * 3. connection established 17667 * 4. data in mblk 17668 * 5. len <= mss 17669 * 6. no tcp_valid bits 17670 */ 17671 if ((tcp->tcp_unsent != 0) || 17672 (tcp->tcp_cork) || 17673 (mp->b_cont != NULL) || 17674 (tcp->tcp_state != TCPS_ESTABLISHED) || 17675 (len == 0) || 17676 (len > mss) || 17677 (tcp->tcp_valid_bits != 0)) { 17678 tcp_wput_data(tcp, mp, B_FALSE); 17679 return; 17680 } 17681 17682 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17683 ASSERT(tcp->tcp_fin_sent == 0); 17684 17685 /* queue new packet onto retransmission queue */ 17686 if (tcp->tcp_xmit_head == NULL) { 17687 tcp->tcp_xmit_head = mp; 17688 } else { 17689 tcp->tcp_xmit_last->b_cont = mp; 17690 } 17691 tcp->tcp_xmit_last = mp; 17692 tcp->tcp_xmit_tail = mp; 17693 17694 /* find out how much we can send */ 17695 /* BEGIN CSTYLED */ 17696 /* 17697 * un-acked usable 17698 * |--------------|-----------------| 17699 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17700 */ 17701 /* END CSTYLED */ 17702 17703 /* start sending from tcp_snxt */ 17704 snxt = tcp->tcp_snxt; 17705 17706 /* 17707 * Check to see if this connection has been idled for some 17708 * time and no ACK is expected. If it is, we need to slow 17709 * start again to get back the connection's "self-clock" as 17710 * described in VJ's paper. 17711 * 17712 * Refer to the comment in tcp_mss_set() for the calculation 17713 * of tcp_cwnd after idle. 17714 */ 17715 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17716 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17717 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17718 } 17719 17720 usable = tcp->tcp_swnd; /* tcp window size */ 17721 if (usable > tcp->tcp_cwnd) 17722 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17723 usable -= snxt; /* subtract stuff already sent */ 17724 suna = tcp->tcp_suna; 17725 usable += suna; 17726 /* usable can be < 0 if the congestion window is smaller */ 17727 if (len > usable) { 17728 /* Can't send complete M_DATA in one shot */ 17729 goto slow; 17730 } 17731 17732 mutex_enter(&tcp->tcp_non_sq_lock); 17733 if (tcp->tcp_flow_stopped && 17734 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17735 tcp_clrqfull(tcp); 17736 } 17737 mutex_exit(&tcp->tcp_non_sq_lock); 17738 17739 /* 17740 * determine if anything to send (Nagle). 17741 * 17742 * 1. len < tcp_mss (i.e. small) 17743 * 2. unacknowledged data present 17744 * 3. len < nagle limit 17745 * 4. last packet sent < nagle limit (previous packet sent) 17746 */ 17747 if ((len < mss) && (snxt != suna) && 17748 (len < (int)tcp->tcp_naglim) && 17749 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17750 /* 17751 * This was the first unsent packet and normally 17752 * mss < xmit_hiwater so there is no need to worry 17753 * about flow control. The next packet will go 17754 * through the flow control check in tcp_wput_data(). 17755 */ 17756 /* leftover work from above */ 17757 tcp->tcp_unsent = len; 17758 tcp->tcp_xmit_tail_unsent = len; 17759 17760 return; 17761 } 17762 17763 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17764 17765 if (snxt == suna) { 17766 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17767 } 17768 17769 /* we have always sent something */ 17770 tcp->tcp_rack_cnt = 0; 17771 17772 tcp->tcp_snxt = snxt + len; 17773 tcp->tcp_rack = tcp->tcp_rnxt; 17774 17775 if ((mp1 = dupb(mp)) == 0) 17776 goto no_memory; 17777 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17778 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17779 17780 /* adjust tcp header information */ 17781 tcph = tcp->tcp_tcph; 17782 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17783 17784 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17785 sum = (sum >> 16) + (sum & 0xFFFF); 17786 U16_TO_ABE16(sum, tcph->th_sum); 17787 17788 U32_TO_ABE32(snxt, tcph->th_seq); 17789 17790 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17791 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17792 BUMP_LOCAL(tcp->tcp_obsegs); 17793 17794 /* Update the latest receive window size in TCP header. */ 17795 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17796 tcph->th_win); 17797 17798 tcp->tcp_last_sent_len = (ushort_t)len; 17799 17800 plen = len + tcp->tcp_hdr_len; 17801 17802 if (tcp->tcp_ipversion == IPV4_VERSION) { 17803 tcp->tcp_ipha->ipha_length = htons(plen); 17804 } else { 17805 tcp->tcp_ip6h->ip6_plen = htons(plen - 17806 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17807 } 17808 17809 /* see if we need to allocate a mblk for the headers */ 17810 hdrlen = tcp->tcp_hdr_len; 17811 rptr = mp1->b_rptr - hdrlen; 17812 db = mp1->b_datap; 17813 if ((db->db_ref != 2) || rptr < db->db_base || 17814 (!OK_32PTR(rptr))) { 17815 /* NOTE: we assume allocb returns an OK_32PTR */ 17816 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17817 tcps->tcps_wroff_xtra, BPRI_MED); 17818 if (!mp) { 17819 freemsg(mp1); 17820 goto no_memory; 17821 } 17822 mp->b_cont = mp1; 17823 mp1 = mp; 17824 /* Leave room for Link Level header */ 17825 /* hdrlen = tcp->tcp_hdr_len; */ 17826 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17827 mp1->b_wptr = &rptr[hdrlen]; 17828 } 17829 mp1->b_rptr = rptr; 17830 17831 /* Fill in the timestamp option. */ 17832 if (tcp->tcp_snd_ts_ok) { 17833 U32_TO_BE32((uint32_t)lbolt, 17834 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17835 U32_TO_BE32(tcp->tcp_ts_recent, 17836 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17837 } else { 17838 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17839 } 17840 17841 /* copy header into outgoing packet */ 17842 dst = (ipaddr_t *)rptr; 17843 src = (ipaddr_t *)tcp->tcp_iphc; 17844 dst[0] = src[0]; 17845 dst[1] = src[1]; 17846 dst[2] = src[2]; 17847 dst[3] = src[3]; 17848 dst[4] = src[4]; 17849 dst[5] = src[5]; 17850 dst[6] = src[6]; 17851 dst[7] = src[7]; 17852 dst[8] = src[8]; 17853 dst[9] = src[9]; 17854 if (hdrlen -= 40) { 17855 hdrlen >>= 2; 17856 dst += 10; 17857 src += 10; 17858 do { 17859 *dst++ = *src++; 17860 } while (--hdrlen); 17861 } 17862 17863 /* 17864 * Set the ECN info in the TCP header. Note that this 17865 * is not the template header. 17866 */ 17867 if (tcp->tcp_ecn_ok) { 17868 SET_ECT(tcp, rptr); 17869 17870 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17871 if (tcp->tcp_ecn_echo_on) 17872 tcph->th_flags[0] |= TH_ECE; 17873 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17874 tcph->th_flags[0] |= TH_CWR; 17875 tcp->tcp_ecn_cwr_sent = B_TRUE; 17876 } 17877 } 17878 17879 if (tcp->tcp_ip_forward_progress) { 17880 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17881 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17882 tcp->tcp_ip_forward_progress = B_FALSE; 17883 } 17884 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17885 return; 17886 17887 /* 17888 * If we ran out of memory, we pretend to have sent the packet 17889 * and that it was lost on the wire. 17890 */ 17891 no_memory: 17892 return; 17893 17894 slow: 17895 /* leftover work from above */ 17896 tcp->tcp_unsent = len; 17897 tcp->tcp_xmit_tail_unsent = len; 17898 tcp_wput_data(tcp, NULL, B_FALSE); 17899 } 17900 17901 /* ARGSUSED */ 17902 void 17903 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17904 { 17905 conn_t *connp = (conn_t *)arg; 17906 tcp_t *tcp = connp->conn_tcp; 17907 queue_t *q = tcp->tcp_rq; 17908 struct tcp_options *tcpopt; 17909 tcp_stack_t *tcps = tcp->tcp_tcps; 17910 17911 /* socket options */ 17912 uint_t sopp_flags; 17913 ssize_t sopp_rxhiwat; 17914 ssize_t sopp_maxblk; 17915 ushort_t sopp_wroff; 17916 ushort_t sopp_tail; 17917 ushort_t sopp_copyopt; 17918 17919 tcpopt = (struct tcp_options *)mp->b_rptr; 17920 17921 /* 17922 * Drop the eager's ref on the listener, that was placed when 17923 * this eager began life in tcp_conn_request. 17924 */ 17925 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17926 if (IPCL_IS_NONSTR(connp)) { 17927 /* Safe to free conn_ind message */ 17928 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17929 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17930 17931 /* The listener tells us which upper handle to use */ 17932 ASSERT(tcpopt->to_flags & TCPOPT_UPPERHANDLE); 17933 connp->conn_upper_handle = tcpopt->to_handle; 17934 } 17935 17936 tcp->tcp_detached = B_FALSE; 17937 17938 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17939 /* 17940 * Someone blewoff the eager before we could finish 17941 * the accept. 17942 * 17943 * The only reason eager exists it because we put in 17944 * a ref on it when conn ind went up. We need to send 17945 * a disconnect indication up while the last reference 17946 * on the eager will be dropped by the squeue when we 17947 * return. 17948 */ 17949 ASSERT(tcp->tcp_listener == NULL); 17950 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17951 if (IPCL_IS_NONSTR(connp)) { 17952 ASSERT(tcp->tcp_issocket); 17953 (*connp->conn_upcalls->su_disconnected)( 17954 connp->conn_upper_handle, tcp->tcp_connid, 17955 ECONNREFUSED); 17956 freemsg(mp); 17957 } else { 17958 struct T_discon_ind *tdi; 17959 17960 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17961 /* 17962 * Let us reuse the incoming mblk to avoid 17963 * memory allocation failure problems. We know 17964 * that the size of the incoming mblk i.e. 17965 * stroptions is greater than sizeof 17966 * T_discon_ind. So the reallocb below can't 17967 * fail. 17968 */ 17969 freemsg(mp->b_cont); 17970 mp->b_cont = NULL; 17971 ASSERT(DB_REF(mp) == 1); 17972 mp = reallocb(mp, sizeof (struct T_discon_ind), 17973 B_FALSE); 17974 ASSERT(mp != NULL); 17975 DB_TYPE(mp) = M_PROTO; 17976 ((union T_primitives *)mp->b_rptr)->type = 17977 T_DISCON_IND; 17978 tdi = (struct T_discon_ind *)mp->b_rptr; 17979 if (tcp->tcp_issocket) { 17980 tdi->DISCON_reason = ECONNREFUSED; 17981 tdi->SEQ_number = 0; 17982 } else { 17983 tdi->DISCON_reason = ENOPROTOOPT; 17984 tdi->SEQ_number = 17985 tcp->tcp_conn_req_seqnum; 17986 } 17987 mp->b_wptr = mp->b_rptr + 17988 sizeof (struct T_discon_ind); 17989 putnext(q, mp); 17990 return; 17991 } 17992 } 17993 if (tcp->tcp_hard_binding) { 17994 tcp->tcp_hard_binding = B_FALSE; 17995 tcp->tcp_hard_bound = B_TRUE; 17996 } 17997 return; 17998 } 17999 18000 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 18001 int boundif = tcpopt->to_boundif; 18002 uint_t len = sizeof (int); 18003 18004 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 18005 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 18006 (uchar_t *)&boundif, NULL, tcp->tcp_cred); 18007 } 18008 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 18009 uint_t on = 1; 18010 uint_t len = sizeof (uint_t); 18011 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 18012 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 18013 (uchar_t *)&on, NULL, tcp->tcp_cred); 18014 } 18015 18016 /* 18017 * For a loopback connection with tcp_direct_sockfs on, note that 18018 * we don't have to protect tcp_rcv_list yet because synchronous 18019 * streams has not yet been enabled and tcp_fuse_rrw() cannot 18020 * possibly race with us. 18021 */ 18022 18023 /* 18024 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18025 * properly. This is the first time we know of the acceptor' 18026 * queue. So we do it here. 18027 * 18028 * XXX 18029 */ 18030 if (tcp->tcp_rcv_list == NULL) { 18031 /* 18032 * Recv queue is empty, tcp_rwnd should not have changed. 18033 * That means it should be equal to the listener's tcp_rwnd. 18034 */ 18035 if (!IPCL_IS_NONSTR(connp)) 18036 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18037 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 18038 } else { 18039 #ifdef DEBUG 18040 mblk_t *tmp; 18041 mblk_t *mp1; 18042 uint_t cnt = 0; 18043 18044 mp1 = tcp->tcp_rcv_list; 18045 while ((tmp = mp1) != NULL) { 18046 mp1 = tmp->b_next; 18047 cnt += msgdsize(tmp); 18048 } 18049 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18050 #endif 18051 /* There is some data, add them back to get the max. */ 18052 if (!IPCL_IS_NONSTR(connp)) 18053 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18054 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18055 } 18056 /* 18057 * This is the first time we run on the correct 18058 * queue after tcp_accept. So fix all the q parameters 18059 * here. 18060 */ 18061 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 18062 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18063 18064 /* 18065 * Record the stream head's high water mark for this endpoint; 18066 * this is used for flow-control purposes. 18067 */ 18068 sopp_rxhiwat = tcp->tcp_fused ? 18069 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 18070 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 18071 18072 /* 18073 * Determine what write offset value to use depending on SACK and 18074 * whether the endpoint is fused or not. 18075 */ 18076 if (tcp->tcp_fused) { 18077 ASSERT(tcp->tcp_loopback); 18078 ASSERT(tcp->tcp_loopback_peer != NULL); 18079 /* 18080 * For fused tcp loopback, set the stream head's write 18081 * offset value to zero since we won't be needing any room 18082 * for TCP/IP headers. This would also improve performance 18083 * since it would reduce the amount of work done by kmem. 18084 * Non-fused tcp loopback case is handled separately below. 18085 */ 18086 sopp_wroff = 0; 18087 /* 18088 * Update the peer's transmit parameters according to 18089 * our recently calculated high water mark value. 18090 */ 18091 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18092 } else if (tcp->tcp_snd_sack_ok) { 18093 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18094 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18095 } else { 18096 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18097 tcps->tcps_wroff_xtra); 18098 } 18099 18100 /* 18101 * If this is endpoint is handling SSL, then reserve extra 18102 * offset and space at the end. 18103 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18104 * overriding the previous setting. The extra cost of signing and 18105 * encrypting multiple MSS-size records (12 of them with Ethernet), 18106 * instead of a single contiguous one by the stream head 18107 * largely outweighs the statistical reduction of ACKs, when 18108 * applicable. The peer will also save on decryption and verification 18109 * costs. 18110 */ 18111 if (tcp->tcp_kssl_ctx != NULL) { 18112 sopp_wroff += SSL3_WROFFSET; 18113 18114 sopp_flags |= SOCKOPT_TAIL; 18115 sopp_tail = SSL3_MAX_TAIL_LEN; 18116 18117 sopp_flags |= SOCKOPT_ZCOPY; 18118 sopp_copyopt = ZCVMUNSAFE; 18119 18120 sopp_maxblk = SSL3_MAX_RECORD_LEN; 18121 } 18122 18123 /* Send the options up */ 18124 if (IPCL_IS_NONSTR(connp)) { 18125 struct sock_proto_props sopp; 18126 18127 sopp.sopp_flags = sopp_flags; 18128 sopp.sopp_wroff = sopp_wroff; 18129 sopp.sopp_maxblk = sopp_maxblk; 18130 sopp.sopp_rxhiwat = sopp_rxhiwat; 18131 if (sopp_flags & SOCKOPT_TAIL) { 18132 ASSERT(tcp->tcp_kssl_ctx != NULL); 18133 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 18134 sopp.sopp_tail = sopp_tail; 18135 sopp.sopp_zcopyflag = sopp_copyopt; 18136 } 18137 (*connp->conn_upcalls->su_set_proto_props) 18138 (connp->conn_upper_handle, &sopp); 18139 } else { 18140 struct stroptions *stropt; 18141 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18142 if (stropt_mp == NULL) { 18143 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 18144 return; 18145 } 18146 DB_TYPE(stropt_mp) = M_SETOPTS; 18147 stropt = (struct stroptions *)stropt_mp->b_rptr; 18148 stropt_mp->b_wptr += sizeof (struct stroptions); 18149 stropt = (struct stroptions *)stropt_mp->b_rptr; 18150 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 18151 stropt->so_hiwat = sopp_rxhiwat; 18152 stropt->so_wroff = sopp_wroff; 18153 stropt->so_maxblk = sopp_maxblk; 18154 18155 if (sopp_flags & SOCKOPT_TAIL) { 18156 ASSERT(tcp->tcp_kssl_ctx != NULL); 18157 18158 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 18159 stropt->so_tail = sopp_tail; 18160 stropt->so_copyopt = sopp_copyopt; 18161 } 18162 18163 /* Send the options up */ 18164 putnext(q, stropt_mp); 18165 } 18166 18167 freemsg(mp); 18168 /* 18169 * Pass up any data and/or a fin that has been received. 18170 * 18171 * Adjust receive window in case it had decreased 18172 * (because there is data <=> tcp_rcv_list != NULL) 18173 * while the connection was detached. Note that 18174 * in case the eager was flow-controlled, w/o this 18175 * code, the rwnd may never open up again! 18176 */ 18177 if (tcp->tcp_rcv_list != NULL) { 18178 if (IPCL_IS_NONSTR(connp)) { 18179 mblk_t *mp; 18180 int space_left; 18181 int error; 18182 boolean_t push = B_TRUE; 18183 18184 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 18185 (connp->conn_upper_handle, NULL, 0, 0, &error, 18186 &push) >= 0) { 18187 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 18188 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18189 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18190 tcp_xmit_ctl(NULL, 18191 tcp, (tcp->tcp_swnd == 0) ? 18192 tcp->tcp_suna : tcp->tcp_snxt, 18193 tcp->tcp_rnxt, TH_ACK); 18194 } 18195 } 18196 while ((mp = tcp->tcp_rcv_list) != NULL) { 18197 push = B_TRUE; 18198 tcp->tcp_rcv_list = mp->b_next; 18199 mp->b_next = NULL; 18200 space_left = (*connp->conn_upcalls->su_recv) 18201 (connp->conn_upper_handle, mp, msgdsize(mp), 18202 0, &error, &push); 18203 if (space_left < 0) { 18204 /* 18205 * At this point the eager is not 18206 * visible to anyone, so fallback 18207 * can not happen. 18208 */ 18209 ASSERT(error != EOPNOTSUPP); 18210 } 18211 } 18212 tcp->tcp_rcv_last_head = NULL; 18213 tcp->tcp_rcv_last_tail = NULL; 18214 tcp->tcp_rcv_cnt = 0; 18215 } else { 18216 /* We drain directly in case of fused tcp loopback */ 18217 sodirect_t *sodp; 18218 18219 if (!tcp->tcp_fused && canputnext(q)) { 18220 tcp->tcp_rwnd = q->q_hiwat; 18221 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18222 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18223 tcp_xmit_ctl(NULL, 18224 tcp, (tcp->tcp_swnd == 0) ? 18225 tcp->tcp_suna : tcp->tcp_snxt, 18226 tcp->tcp_rnxt, TH_ACK); 18227 } 18228 } 18229 18230 SOD_PTR_ENTER(tcp, sodp); 18231 if (sodp != NULL) { 18232 /* Sodirect, move from rcv_list */ 18233 ASSERT(!tcp->tcp_fused); 18234 while ((mp = tcp->tcp_rcv_list) != NULL) { 18235 tcp->tcp_rcv_list = mp->b_next; 18236 mp->b_next = NULL; 18237 (void) tcp_rcv_sod_enqueue(tcp, sodp, 18238 mp, msgdsize(mp)); 18239 } 18240 tcp->tcp_rcv_last_head = NULL; 18241 tcp->tcp_rcv_last_tail = NULL; 18242 tcp->tcp_rcv_cnt = 0; 18243 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18244 /* sod_wakeup() did the mutex_exit() */ 18245 } else { 18246 /* Not sodirect, drain */ 18247 (void) tcp_rcv_drain(tcp); 18248 } 18249 } 18250 18251 /* 18252 * For fused tcp loopback, back-enable peer endpoint 18253 * if it's currently flow-controlled. 18254 */ 18255 if (tcp->tcp_fused) { 18256 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18257 18258 ASSERT(peer_tcp != NULL); 18259 ASSERT(peer_tcp->tcp_fused); 18260 /* 18261 * In order to change the peer's tcp_flow_stopped, 18262 * we need to take locks for both end points. The 18263 * highest address is taken first. 18264 */ 18265 if (peer_tcp > tcp) { 18266 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18267 mutex_enter(&tcp->tcp_non_sq_lock); 18268 } else { 18269 mutex_enter(&tcp->tcp_non_sq_lock); 18270 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18271 } 18272 if (peer_tcp->tcp_flow_stopped) { 18273 tcp_clrqfull(peer_tcp); 18274 TCP_STAT(tcps, tcp_fusion_backenabled); 18275 } 18276 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18277 mutex_exit(&tcp->tcp_non_sq_lock); 18278 } 18279 } 18280 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18281 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18282 tcp->tcp_ordrel_done = B_TRUE; 18283 if (IPCL_IS_NONSTR(connp)) { 18284 ASSERT(tcp->tcp_ordrel_mp == NULL); 18285 (*connp->conn_upcalls->su_opctl)( 18286 connp->conn_upper_handle, 18287 SOCK_OPCTL_SHUT_RECV, 0); 18288 } else { 18289 mp = tcp->tcp_ordrel_mp; 18290 tcp->tcp_ordrel_mp = NULL; 18291 putnext(q, mp); 18292 } 18293 } 18294 if (tcp->tcp_hard_binding) { 18295 tcp->tcp_hard_binding = B_FALSE; 18296 tcp->tcp_hard_bound = B_TRUE; 18297 } 18298 18299 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 18300 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 18301 tcp->tcp_loopback_peer != NULL && 18302 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 18303 tcp_fuse_syncstr_enable_pair(tcp); 18304 } 18305 18306 if (tcp->tcp_ka_enabled) { 18307 tcp->tcp_ka_last_intrvl = 0; 18308 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18309 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18310 } 18311 18312 /* 18313 * At this point, eager is fully established and will 18314 * have the following references - 18315 * 18316 * 2 references for connection to exist (1 for TCP and 1 for IP). 18317 * 1 reference for the squeue which will be dropped by the squeue as 18318 * soon as this function returns. 18319 * There will be 1 additonal reference for being in classifier 18320 * hash list provided something bad hasn't happened. 18321 */ 18322 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18323 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18324 } 18325 18326 /* 18327 * The function called through squeue to get behind listener's perimeter to 18328 * send a deffered conn_ind. 18329 */ 18330 /* ARGSUSED */ 18331 void 18332 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18333 { 18334 conn_t *connp = (conn_t *)arg; 18335 tcp_t *listener = connp->conn_tcp; 18336 struct T_conn_ind *conn_ind; 18337 tcp_t *tcp; 18338 18339 if (listener->tcp_state == TCPS_CLOSED || 18340 TCP_IS_DETACHED(listener)) { 18341 /* 18342 * If listener has closed, it would have caused a 18343 * a cleanup/blowoff to happen for the eager. 18344 */ 18345 18346 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18347 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18348 conn_ind->OPT_length); 18349 /* 18350 * We need to drop the ref on eager that was put 18351 * tcp_rput_data() before trying to send the conn_ind 18352 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18353 * and tcp_wput_accept() is sending this deferred conn_ind but 18354 * listener is closed so we drop the ref. 18355 */ 18356 CONN_DEC_REF(tcp->tcp_connp); 18357 freemsg(mp); 18358 return; 18359 } 18360 if (IPCL_IS_NONSTR(connp)) { 18361 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18362 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18363 conn_ind->OPT_length); 18364 18365 if ((*connp->conn_upcalls->su_newconn) 18366 (connp->conn_upper_handle, 18367 (sock_lower_handle_t)tcp->tcp_connp, 18368 &sock_tcp_downcalls, DB_CRED(mp), DB_CPID(mp), 18369 &tcp->tcp_connp->conn_upcalls) != NULL) { 18370 /* Keep the message around in case of fallback */ 18371 tcp->tcp_conn.tcp_eager_conn_ind = mp; 18372 } else { 18373 freemsg(mp); 18374 } 18375 } else { 18376 putnext(listener->tcp_rq, mp); 18377 } 18378 } 18379 18380 /* ARGSUSED */ 18381 static int 18382 tcp_accept_common(conn_t *lconnp, conn_t *econnp, 18383 sock_upper_handle_t sock_handle, cred_t *cr) 18384 { 18385 tcp_t *listener, *eager; 18386 mblk_t *opt_mp; 18387 struct tcp_options *tcpopt; 18388 18389 listener = lconnp->conn_tcp; 18390 ASSERT(listener->tcp_state == TCPS_LISTEN); 18391 eager = econnp->conn_tcp; 18392 ASSERT(eager->tcp_listener != NULL); 18393 18394 ASSERT(eager->tcp_rq != NULL); 18395 18396 /* If tcp_fused and sodirect enabled disable it */ 18397 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18398 /* Fused, disable sodirect */ 18399 mutex_enter(eager->tcp_sodirect->sod_lockp); 18400 SOD_DISABLE(eager->tcp_sodirect); 18401 mutex_exit(eager->tcp_sodirect->sod_lockp); 18402 eager->tcp_sodirect = NULL; 18403 } 18404 18405 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 18406 if (opt_mp == NULL) { 18407 return (-TPROTO); 18408 } 18409 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 18410 eager->tcp_issocket = B_TRUE; 18411 18412 econnp->conn_upcalls = lconnp->conn_upcalls; 18413 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18414 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18415 ASSERT(econnp->conn_netstack == 18416 listener->tcp_connp->conn_netstack); 18417 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18418 18419 /* Put the ref for IP */ 18420 CONN_INC_REF(econnp); 18421 18422 /* 18423 * We should have minimum of 3 references on the conn 18424 * at this point. One each for TCP and IP and one for 18425 * the T_conn_ind that was sent up when the 3-way handshake 18426 * completed. In the normal case we would also have another 18427 * reference (making a total of 4) for the conn being in the 18428 * classifier hash list. However the eager could have received 18429 * an RST subsequently and tcp_closei_local could have removed 18430 * the eager from the classifier hash list, hence we can't 18431 * assert that reference. 18432 */ 18433 ASSERT(econnp->conn_ref >= 3); 18434 18435 opt_mp->b_datap->db_type = M_SETOPTS; 18436 opt_mp->b_wptr += sizeof (struct tcp_options); 18437 18438 /* 18439 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18440 * from listener to acceptor. In case of non-STREAMS sockets, 18441 * we also need to pass the upper handle along. 18442 */ 18443 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 18444 tcpopt->to_flags = 0; 18445 18446 if (IPCL_IS_NONSTR(econnp)) { 18447 ASSERT(sock_handle != NULL); 18448 tcpopt->to_flags |= TCPOPT_UPPERHANDLE; 18449 tcpopt->to_handle = sock_handle; 18450 } 18451 if (listener->tcp_bound_if != 0) { 18452 tcpopt->to_flags |= TCPOPT_BOUNDIF; 18453 tcpopt->to_boundif = listener->tcp_bound_if; 18454 } 18455 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18456 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 18457 } 18458 18459 mutex_enter(&listener->tcp_eager_lock); 18460 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18461 18462 tcp_t *tail; 18463 tcp_t *tcp; 18464 mblk_t *mp1; 18465 18466 tcp = listener->tcp_eager_prev_q0; 18467 /* 18468 * listener->tcp_eager_prev_q0 points to the TAIL of the 18469 * deferred T_conn_ind queue. We need to get to the head 18470 * of the queue in order to send up T_conn_ind the same 18471 * order as how the 3WHS is completed. 18472 */ 18473 while (tcp != listener) { 18474 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18475 !tcp->tcp_kssl_pending) 18476 break; 18477 else 18478 tcp = tcp->tcp_eager_prev_q0; 18479 } 18480 /* None of the pending eagers can be sent up now */ 18481 if (tcp == listener) 18482 goto no_more_eagers; 18483 18484 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18485 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18486 /* Move from q0 to q */ 18487 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18488 listener->tcp_conn_req_cnt_q0--; 18489 listener->tcp_conn_req_cnt_q++; 18490 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18491 tcp->tcp_eager_prev_q0; 18492 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18493 tcp->tcp_eager_next_q0; 18494 tcp->tcp_eager_prev_q0 = NULL; 18495 tcp->tcp_eager_next_q0 = NULL; 18496 tcp->tcp_conn_def_q0 = B_FALSE; 18497 18498 /* Make sure the tcp isn't in the list of droppables */ 18499 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18500 tcp->tcp_eager_prev_drop_q0 == NULL); 18501 18502 /* 18503 * Insert at end of the queue because sockfs sends 18504 * down T_CONN_RES in chronological order. Leaving 18505 * the older conn indications at front of the queue 18506 * helps reducing search time. 18507 */ 18508 tail = listener->tcp_eager_last_q; 18509 if (tail != NULL) { 18510 tail->tcp_eager_next_q = tcp; 18511 } else { 18512 listener->tcp_eager_next_q = tcp; 18513 } 18514 listener->tcp_eager_last_q = tcp; 18515 tcp->tcp_eager_next_q = NULL; 18516 18517 /* Need to get inside the listener perimeter */ 18518 CONN_INC_REF(listener->tcp_connp); 18519 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18520 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18521 SQTAG_TCP_SEND_PENDING); 18522 } 18523 no_more_eagers: 18524 tcp_eager_unlink(eager); 18525 mutex_exit(&listener->tcp_eager_lock); 18526 18527 /* 18528 * At this point, the eager is detached from the listener 18529 * but we still have an extra refs on eager (apart from the 18530 * usual tcp references). The ref was placed in tcp_rput_data 18531 * before sending the conn_ind in tcp_send_conn_ind. 18532 * The ref will be dropped in tcp_accept_finish(). 18533 */ 18534 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18535 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18536 return (0); 18537 } 18538 18539 int 18540 tcp_accept(sock_lower_handle_t lproto_handle, 18541 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18542 cred_t *cr) 18543 { 18544 conn_t *lconnp, *econnp; 18545 tcp_t *listener, *eager; 18546 tcp_stack_t *tcps; 18547 18548 lconnp = (conn_t *)lproto_handle; 18549 listener = lconnp->conn_tcp; 18550 ASSERT(listener->tcp_state == TCPS_LISTEN); 18551 econnp = (conn_t *)eproto_handle; 18552 eager = econnp->conn_tcp; 18553 ASSERT(eager->tcp_listener != NULL); 18554 tcps = eager->tcp_tcps; 18555 18556 ASSERT(IPCL_IS_NONSTR(econnp)); 18557 /* 18558 * Create helper stream if it is a non-TPI TCP connection. 18559 */ 18560 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18561 ip1dbg(("tcp_accept: create of IP helper stream" 18562 " failed\n")); 18563 return (EPROTO); 18564 } 18565 eager->tcp_rq = econnp->conn_rq; 18566 eager->tcp_wq = econnp->conn_wq; 18567 18568 ASSERT(eager->tcp_rq != NULL); 18569 18570 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18571 return (tcp_accept_common(lconnp, econnp, sock_handle, cr)); 18572 } 18573 18574 18575 /* 18576 * This is the STREAMS entry point for T_CONN_RES coming down on 18577 * Acceptor STREAM when sockfs listener does accept processing. 18578 * Read the block comment on top of tcp_conn_request(). 18579 */ 18580 void 18581 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18582 { 18583 queue_t *rq = RD(q); 18584 struct T_conn_res *conn_res; 18585 tcp_t *eager; 18586 tcp_t *listener; 18587 struct T_ok_ack *ok; 18588 t_scalar_t PRIM_type; 18589 conn_t *econnp; 18590 18591 ASSERT(DB_TYPE(mp) == M_PROTO); 18592 18593 conn_res = (struct T_conn_res *)mp->b_rptr; 18594 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18595 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18596 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18597 if (mp != NULL) 18598 putnext(rq, mp); 18599 return; 18600 } 18601 switch (conn_res->PRIM_type) { 18602 case O_T_CONN_RES: 18603 case T_CONN_RES: 18604 /* 18605 * We pass up an err ack if allocb fails. This will 18606 * cause sockfs to issue a T_DISCON_REQ which will cause 18607 * tcp_eager_blowoff to be called. sockfs will then call 18608 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18609 * we need to do the allocb up here because we have to 18610 * make sure rq->q_qinfo->qi_qclose still points to the 18611 * correct function (tcpclose_accept) in case allocb 18612 * fails. 18613 */ 18614 bcopy(mp->b_rptr + conn_res->OPT_offset, 18615 &eager, conn_res->OPT_length); 18616 PRIM_type = conn_res->PRIM_type; 18617 mp->b_datap->db_type = M_PCPROTO; 18618 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18619 ok = (struct T_ok_ack *)mp->b_rptr; 18620 ok->PRIM_type = T_OK_ACK; 18621 ok->CORRECT_prim = PRIM_type; 18622 econnp = eager->tcp_connp; 18623 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18624 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18625 eager->tcp_rq = rq; 18626 eager->tcp_wq = q; 18627 rq->q_ptr = econnp; 18628 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18629 q->q_ptr = econnp; 18630 q->q_qinfo = &tcp_winit; 18631 listener = eager->tcp_listener; 18632 18633 /* 18634 * TCP is _D_SODIRECT and sockfs is directly above so 18635 * save shared sodirect_t pointer (if any). 18636 */ 18637 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18638 if (tcp_accept_common(listener->tcp_connp, 18639 econnp, NULL, CRED()) < 0) { 18640 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18641 if (mp != NULL) 18642 putnext(rq, mp); 18643 return; 18644 } 18645 18646 /* 18647 * Send the new local address also up to sockfs. There 18648 * should already be enough space in the mp that came 18649 * down from soaccept(). 18650 */ 18651 if (eager->tcp_family == AF_INET) { 18652 sin_t *sin; 18653 18654 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18655 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18656 sin = (sin_t *)mp->b_wptr; 18657 mp->b_wptr += sizeof (sin_t); 18658 sin->sin_family = AF_INET; 18659 sin->sin_port = eager->tcp_lport; 18660 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18661 } else { 18662 sin6_t *sin6; 18663 18664 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18665 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18666 sin6 = (sin6_t *)mp->b_wptr; 18667 mp->b_wptr += sizeof (sin6_t); 18668 sin6->sin6_family = AF_INET6; 18669 sin6->sin6_port = eager->tcp_lport; 18670 if (eager->tcp_ipversion == IPV4_VERSION) { 18671 sin6->sin6_flowinfo = 0; 18672 IN6_IPADDR_TO_V4MAPPED( 18673 eager->tcp_ipha->ipha_src, 18674 &sin6->sin6_addr); 18675 } else { 18676 ASSERT(eager->tcp_ip6h != NULL); 18677 sin6->sin6_flowinfo = 18678 eager->tcp_ip6h->ip6_vcf & 18679 ~IPV6_VERS_AND_FLOW_MASK; 18680 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18681 } 18682 sin6->sin6_scope_id = 0; 18683 sin6->__sin6_src_id = 0; 18684 } 18685 18686 putnext(rq, mp); 18687 return; 18688 default: 18689 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18690 if (mp != NULL) 18691 putnext(rq, mp); 18692 return; 18693 } 18694 } 18695 18696 static int 18697 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18698 { 18699 sin_t *sin = (sin_t *)sa; 18700 sin6_t *sin6 = (sin6_t *)sa; 18701 18702 switch (tcp->tcp_family) { 18703 case AF_INET: 18704 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18705 18706 if (*salenp < sizeof (sin_t)) 18707 return (EINVAL); 18708 18709 *sin = sin_null; 18710 sin->sin_family = AF_INET; 18711 sin->sin_port = tcp->tcp_lport; 18712 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18713 break; 18714 18715 case AF_INET6: 18716 if (*salenp < sizeof (sin6_t)) 18717 return (EINVAL); 18718 18719 *sin6 = sin6_null; 18720 sin6->sin6_family = AF_INET6; 18721 sin6->sin6_port = tcp->tcp_lport; 18722 if (tcp->tcp_ipversion == IPV4_VERSION) { 18723 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18724 &sin6->sin6_addr); 18725 } else { 18726 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18727 } 18728 break; 18729 } 18730 18731 return (0); 18732 } 18733 18734 static int 18735 i_tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18736 { 18737 sin_t *sin = (sin_t *)sa; 18738 sin6_t *sin6 = (sin6_t *)sa; 18739 18740 if (tcp->tcp_state < TCPS_SYN_RCVD) 18741 return (ENOTCONN); 18742 18743 switch (tcp->tcp_family) { 18744 case AF_INET: 18745 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18746 18747 if (*salenp < sizeof (sin_t)) 18748 return (EINVAL); 18749 18750 *sin = sin_null; 18751 sin->sin_family = AF_INET; 18752 sin->sin_port = tcp->tcp_fport; 18753 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18754 sin->sin_addr.s_addr); 18755 *salenp = sizeof (sin_t); 18756 break; 18757 18758 case AF_INET6: 18759 if (*salenp < sizeof (sin6_t)) 18760 return (EINVAL); 18761 18762 *sin6 = sin6_null; 18763 sin6->sin6_family = AF_INET6; 18764 sin6->sin6_port = tcp->tcp_fport; 18765 sin6->sin6_addr = tcp->tcp_remote_v6; 18766 if (tcp->tcp_ipversion == IPV6_VERSION) { 18767 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18768 ~IPV6_VERS_AND_FLOW_MASK; 18769 } 18770 *salenp = sizeof (sin6_t); 18771 break; 18772 } 18773 18774 return (0); 18775 } 18776 18777 /* 18778 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18779 */ 18780 static void 18781 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18782 { 18783 void *data; 18784 mblk_t *datamp = mp->b_cont; 18785 tcp_t *tcp = Q_TO_TCP(q); 18786 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18787 18788 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18789 cmdp->cb_error = EPROTO; 18790 qreply(q, mp); 18791 return; 18792 } 18793 18794 data = datamp->b_rptr; 18795 18796 switch (cmdp->cb_cmd) { 18797 case TI_GETPEERNAME: 18798 cmdp->cb_error = i_tcp_getpeername(tcp, data, &cmdp->cb_len); 18799 break; 18800 case TI_GETMYNAME: 18801 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18802 break; 18803 default: 18804 cmdp->cb_error = EINVAL; 18805 break; 18806 } 18807 18808 qreply(q, mp); 18809 } 18810 18811 void 18812 tcp_wput(queue_t *q, mblk_t *mp) 18813 { 18814 conn_t *connp = Q_TO_CONN(q); 18815 tcp_t *tcp; 18816 void (*output_proc)(); 18817 t_scalar_t type; 18818 uchar_t *rptr; 18819 struct iocblk *iocp; 18820 size_t size; 18821 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18822 18823 ASSERT(connp->conn_ref >= 2); 18824 18825 switch (DB_TYPE(mp)) { 18826 case M_DATA: 18827 tcp = connp->conn_tcp; 18828 ASSERT(tcp != NULL); 18829 18830 size = msgdsize(mp); 18831 18832 mutex_enter(&tcp->tcp_non_sq_lock); 18833 tcp->tcp_squeue_bytes += size; 18834 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18835 tcp_setqfull(tcp); 18836 } 18837 mutex_exit(&tcp->tcp_non_sq_lock); 18838 18839 if (DB_CRED(mp) == NULL && is_system_labeled()) 18840 msg_setcredpid(mp, CONN_CRED(connp), curproc->p_pid); 18841 18842 CONN_INC_REF(connp); 18843 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18844 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18845 return; 18846 18847 case M_CMD: 18848 tcp_wput_cmdblk(q, mp); 18849 return; 18850 18851 case M_PROTO: 18852 case M_PCPROTO: 18853 /* 18854 * if it is a snmp message, don't get behind the squeue 18855 */ 18856 tcp = connp->conn_tcp; 18857 rptr = mp->b_rptr; 18858 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18859 type = ((union T_primitives *)rptr)->type; 18860 } else { 18861 if (tcp->tcp_debug) { 18862 (void) strlog(TCP_MOD_ID, 0, 1, 18863 SL_ERROR|SL_TRACE, 18864 "tcp_wput_proto, dropping one..."); 18865 } 18866 freemsg(mp); 18867 return; 18868 } 18869 if (type == T_SVR4_OPTMGMT_REQ) { 18870 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18871 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18872 cr)) { 18873 /* 18874 * This was a SNMP request 18875 */ 18876 return; 18877 } else { 18878 output_proc = tcp_wput_proto; 18879 } 18880 } else { 18881 output_proc = tcp_wput_proto; 18882 } 18883 break; 18884 case M_IOCTL: 18885 /* 18886 * Most ioctls can be processed right away without going via 18887 * squeues - process them right here. Those that do require 18888 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18889 * are processed by tcp_wput_ioctl(). 18890 */ 18891 iocp = (struct iocblk *)mp->b_rptr; 18892 tcp = connp->conn_tcp; 18893 18894 switch (iocp->ioc_cmd) { 18895 case TCP_IOC_ABORT_CONN: 18896 tcp_ioctl_abort_conn(q, mp); 18897 return; 18898 case TI_GETPEERNAME: 18899 case TI_GETMYNAME: 18900 mi_copyin(q, mp, NULL, 18901 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18902 return; 18903 case ND_SET: 18904 /* nd_getset does the necessary checks */ 18905 case ND_GET: 18906 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18907 CALL_IP_WPUT(connp, q, mp); 18908 return; 18909 } 18910 qreply(q, mp); 18911 return; 18912 case TCP_IOC_DEFAULT_Q: 18913 /* 18914 * Wants to be the default wq. Check the credentials 18915 * first, the rest is executed via squeue. 18916 */ 18917 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18918 iocp->ioc_error = EPERM; 18919 iocp->ioc_count = 0; 18920 mp->b_datap->db_type = M_IOCACK; 18921 qreply(q, mp); 18922 return; 18923 } 18924 output_proc = tcp_wput_ioctl; 18925 break; 18926 default: 18927 output_proc = tcp_wput_ioctl; 18928 break; 18929 } 18930 break; 18931 default: 18932 output_proc = tcp_wput_nondata; 18933 break; 18934 } 18935 18936 CONN_INC_REF(connp); 18937 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18938 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18939 } 18940 18941 /* 18942 * Initial STREAMS write side put() procedure for sockets. It tries to 18943 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18944 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18945 * are handled by tcp_wput() as usual. 18946 * 18947 * All further messages will also be handled by tcp_wput() because we cannot 18948 * be sure that the above short cut is safe later. 18949 */ 18950 static void 18951 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18952 { 18953 conn_t *connp = Q_TO_CONN(wq); 18954 tcp_t *tcp = connp->conn_tcp; 18955 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18956 18957 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18958 wq->q_qinfo = &tcp_winit; 18959 18960 ASSERT(IPCL_IS_TCP(connp)); 18961 ASSERT(TCP_IS_SOCKET(tcp)); 18962 18963 if (DB_TYPE(mp) == M_PCPROTO && 18964 MBLKL(mp) == sizeof (struct T_capability_req) && 18965 car->PRIM_type == T_CAPABILITY_REQ) { 18966 tcp_capability_req(tcp, mp); 18967 return; 18968 } 18969 18970 tcp_wput(wq, mp); 18971 } 18972 18973 /* ARGSUSED */ 18974 static void 18975 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18976 { 18977 #ifdef DEBUG 18978 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18979 #endif 18980 freemsg(mp); 18981 } 18982 18983 static boolean_t 18984 tcp_zcopy_check(tcp_t *tcp) 18985 { 18986 conn_t *connp = tcp->tcp_connp; 18987 ire_t *ire; 18988 boolean_t zc_enabled = B_FALSE; 18989 tcp_stack_t *tcps = tcp->tcp_tcps; 18990 18991 if (do_tcpzcopy == 2) 18992 zc_enabled = B_TRUE; 18993 else if (tcp->tcp_ipversion == IPV4_VERSION && 18994 IPCL_IS_CONNECTED(connp) && 18995 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18996 connp->conn_dontroute == 0 && 18997 !connp->conn_nexthop_set && 18998 connp->conn_outgoing_ill == NULL && 18999 connp->conn_nofailover_ill == NULL && 19000 do_tcpzcopy == 1) { 19001 /* 19002 * the checks above closely resemble the fast path checks 19003 * in tcp_send_data(). 19004 */ 19005 mutex_enter(&connp->conn_lock); 19006 ire = connp->conn_ire_cache; 19007 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19008 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19009 IRE_REFHOLD(ire); 19010 if (ire->ire_stq != NULL) { 19011 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19012 19013 zc_enabled = ill && (ill->ill_capabilities & 19014 ILL_CAPAB_ZEROCOPY) && 19015 (ill->ill_zerocopy_capab-> 19016 ill_zerocopy_flags != 0); 19017 } 19018 IRE_REFRELE(ire); 19019 } 19020 mutex_exit(&connp->conn_lock); 19021 } 19022 tcp->tcp_snd_zcopy_on = zc_enabled; 19023 if (!TCP_IS_DETACHED(tcp)) { 19024 if (zc_enabled) { 19025 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19026 ZCVMSAFE); 19027 TCP_STAT(tcps, tcp_zcopy_on); 19028 } else { 19029 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19030 ZCVMUNSAFE); 19031 TCP_STAT(tcps, tcp_zcopy_off); 19032 } 19033 } 19034 return (zc_enabled); 19035 } 19036 19037 static mblk_t * 19038 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19039 { 19040 tcp_stack_t *tcps = tcp->tcp_tcps; 19041 19042 if (do_tcpzcopy == 2) 19043 return (bp); 19044 else if (tcp->tcp_snd_zcopy_on) { 19045 tcp->tcp_snd_zcopy_on = B_FALSE; 19046 if (!TCP_IS_DETACHED(tcp)) { 19047 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 19048 ZCVMUNSAFE); 19049 TCP_STAT(tcps, tcp_zcopy_disable); 19050 } 19051 } 19052 return (tcp_zcopy_backoff(tcp, bp, 0)); 19053 } 19054 19055 /* 19056 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19057 * the original desballoca'ed segmapped mblk. 19058 */ 19059 static mblk_t * 19060 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19061 { 19062 mblk_t *head, *tail, *nbp; 19063 tcp_stack_t *tcps = tcp->tcp_tcps; 19064 19065 if (IS_VMLOANED_MBLK(bp)) { 19066 TCP_STAT(tcps, tcp_zcopy_backoff); 19067 if ((head = copyb(bp)) == NULL) { 19068 /* fail to backoff; leave it for the next backoff */ 19069 tcp->tcp_xmit_zc_clean = B_FALSE; 19070 return (bp); 19071 } 19072 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19073 if (fix_xmitlist) 19074 tcp_zcopy_notify(tcp); 19075 else 19076 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19077 } 19078 nbp = bp->b_cont; 19079 if (fix_xmitlist) { 19080 head->b_prev = bp->b_prev; 19081 head->b_next = bp->b_next; 19082 if (tcp->tcp_xmit_tail == bp) 19083 tcp->tcp_xmit_tail = head; 19084 } 19085 bp->b_next = NULL; 19086 bp->b_prev = NULL; 19087 freeb(bp); 19088 } else { 19089 head = bp; 19090 nbp = bp->b_cont; 19091 } 19092 tail = head; 19093 while (nbp) { 19094 if (IS_VMLOANED_MBLK(nbp)) { 19095 TCP_STAT(tcps, tcp_zcopy_backoff); 19096 if ((tail->b_cont = copyb(nbp)) == NULL) { 19097 tcp->tcp_xmit_zc_clean = B_FALSE; 19098 tail->b_cont = nbp; 19099 return (head); 19100 } 19101 tail = tail->b_cont; 19102 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19103 if (fix_xmitlist) 19104 tcp_zcopy_notify(tcp); 19105 else 19106 tail->b_datap->db_struioflag |= 19107 STRUIO_ZCNOTIFY; 19108 } 19109 bp = nbp; 19110 nbp = nbp->b_cont; 19111 if (fix_xmitlist) { 19112 tail->b_prev = bp->b_prev; 19113 tail->b_next = bp->b_next; 19114 if (tcp->tcp_xmit_tail == bp) 19115 tcp->tcp_xmit_tail = tail; 19116 } 19117 bp->b_next = NULL; 19118 bp->b_prev = NULL; 19119 freeb(bp); 19120 } else { 19121 tail->b_cont = nbp; 19122 tail = nbp; 19123 nbp = nbp->b_cont; 19124 } 19125 } 19126 if (fix_xmitlist) { 19127 tcp->tcp_xmit_last = tail; 19128 tcp->tcp_xmit_zc_clean = B_TRUE; 19129 } 19130 return (head); 19131 } 19132 19133 static void 19134 tcp_zcopy_notify(tcp_t *tcp) 19135 { 19136 struct stdata *stp; 19137 conn_t *connp; 19138 19139 if (tcp->tcp_detached) 19140 return; 19141 connp = tcp->tcp_connp; 19142 if (IPCL_IS_NONSTR(connp)) { 19143 (*connp->conn_upcalls->su_zcopy_notify) 19144 (connp->conn_upper_handle); 19145 return; 19146 } 19147 stp = STREAM(tcp->tcp_rq); 19148 mutex_enter(&stp->sd_lock); 19149 stp->sd_flag |= STZCNOTIFY; 19150 cv_broadcast(&stp->sd_zcopy_wait); 19151 mutex_exit(&stp->sd_lock); 19152 } 19153 19154 static boolean_t 19155 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19156 { 19157 ire_t *ire; 19158 conn_t *connp = tcp->tcp_connp; 19159 tcp_stack_t *tcps = tcp->tcp_tcps; 19160 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19161 19162 mutex_enter(&connp->conn_lock); 19163 ire = connp->conn_ire_cache; 19164 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19165 19166 if ((ire != NULL) && 19167 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19168 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19169 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19170 IRE_REFHOLD(ire); 19171 mutex_exit(&connp->conn_lock); 19172 } else { 19173 boolean_t cached = B_FALSE; 19174 ts_label_t *tsl; 19175 19176 /* force a recheck later on */ 19177 tcp->tcp_ire_ill_check_done = B_FALSE; 19178 19179 TCP_DBGSTAT(tcps, tcp_ire_null1); 19180 connp->conn_ire_cache = NULL; 19181 mutex_exit(&connp->conn_lock); 19182 19183 if (ire != NULL) 19184 IRE_REFRELE_NOTR(ire); 19185 19186 tsl = crgetlabel(CONN_CRED(connp)); 19187 ire = (dst ? 19188 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19189 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19190 connp->conn_zoneid, tsl, ipst)); 19191 19192 if (ire == NULL) { 19193 TCP_STAT(tcps, tcp_ire_null); 19194 return (B_FALSE); 19195 } 19196 19197 IRE_REFHOLD_NOTR(ire); 19198 19199 mutex_enter(&connp->conn_lock); 19200 if (CONN_CACHE_IRE(connp)) { 19201 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19202 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19203 TCP_CHECK_IREINFO(tcp, ire); 19204 connp->conn_ire_cache = ire; 19205 cached = B_TRUE; 19206 } 19207 rw_exit(&ire->ire_bucket->irb_lock); 19208 } 19209 mutex_exit(&connp->conn_lock); 19210 19211 /* 19212 * We can continue to use the ire but since it was 19213 * not cached, we should drop the extra reference. 19214 */ 19215 if (!cached) 19216 IRE_REFRELE_NOTR(ire); 19217 19218 /* 19219 * Rampart note: no need to select a new label here, since 19220 * labels are not allowed to change during the life of a TCP 19221 * connection. 19222 */ 19223 } 19224 19225 *irep = ire; 19226 19227 return (B_TRUE); 19228 } 19229 19230 /* 19231 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19232 * 19233 * 0 = success; 19234 * 1 = failed to find ire and ill. 19235 */ 19236 static boolean_t 19237 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19238 { 19239 ipha_t *ipha; 19240 ipaddr_t dst; 19241 ire_t *ire; 19242 ill_t *ill; 19243 conn_t *connp = tcp->tcp_connp; 19244 mblk_t *ire_fp_mp; 19245 tcp_stack_t *tcps = tcp->tcp_tcps; 19246 19247 if (mp != NULL) 19248 ipha = (ipha_t *)mp->b_rptr; 19249 else 19250 ipha = tcp->tcp_ipha; 19251 dst = ipha->ipha_dst; 19252 19253 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19254 return (B_FALSE); 19255 19256 if ((ire->ire_flags & RTF_MULTIRT) || 19257 (ire->ire_stq == NULL) || 19258 (ire->ire_nce == NULL) || 19259 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19260 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19261 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19262 TCP_STAT(tcps, tcp_ip_ire_send); 19263 IRE_REFRELE(ire); 19264 return (B_FALSE); 19265 } 19266 19267 ill = ire_to_ill(ire); 19268 if (connp->conn_outgoing_ill != NULL) { 19269 ill_t *conn_outgoing_ill = NULL; 19270 /* 19271 * Choose a good ill in the group to send the packets on. 19272 */ 19273 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 19274 ill = ire_to_ill(ire); 19275 } 19276 ASSERT(ill != NULL); 19277 19278 if (!tcp->tcp_ire_ill_check_done) { 19279 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19280 tcp->tcp_ire_ill_check_done = B_TRUE; 19281 } 19282 19283 *irep = ire; 19284 *illp = ill; 19285 19286 return (B_TRUE); 19287 } 19288 19289 static void 19290 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19291 { 19292 ipha_t *ipha; 19293 ipaddr_t src; 19294 ipaddr_t dst; 19295 uint32_t cksum; 19296 ire_t *ire; 19297 uint16_t *up; 19298 ill_t *ill; 19299 conn_t *connp = tcp->tcp_connp; 19300 uint32_t hcksum_txflags = 0; 19301 mblk_t *ire_fp_mp; 19302 uint_t ire_fp_mp_len; 19303 tcp_stack_t *tcps = tcp->tcp_tcps; 19304 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19305 19306 ASSERT(DB_TYPE(mp) == M_DATA); 19307 19308 if (is_system_labeled() && DB_CRED(mp) == NULL) 19309 mblk_setcred(mp, CONN_CRED(tcp->tcp_connp)); 19310 19311 ipha = (ipha_t *)mp->b_rptr; 19312 src = ipha->ipha_src; 19313 dst = ipha->ipha_dst; 19314 19315 ASSERT(q != NULL); 19316 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19317 19318 /* 19319 * Drop off fast path for IPv6 and also if options are present or 19320 * we need to resolve a TS label. 19321 */ 19322 if (tcp->tcp_ipversion != IPV4_VERSION || 19323 !IPCL_IS_CONNECTED(connp) || 19324 !CONN_IS_LSO_MD_FASTPATH(connp) || 19325 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19326 !connp->conn_ulp_labeled || 19327 ipha->ipha_ident == IP_HDR_INCLUDED || 19328 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19329 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19330 if (tcp->tcp_snd_zcopy_aware) 19331 mp = tcp_zcopy_disable(tcp, mp); 19332 TCP_STAT(tcps, tcp_ip_send); 19333 CALL_IP_WPUT(connp, q, mp); 19334 return; 19335 } 19336 19337 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19338 if (tcp->tcp_snd_zcopy_aware) 19339 mp = tcp_zcopy_backoff(tcp, mp, 0); 19340 CALL_IP_WPUT(connp, q, mp); 19341 return; 19342 } 19343 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19344 ire_fp_mp_len = MBLKL(ire_fp_mp); 19345 19346 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19347 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19348 #ifndef _BIG_ENDIAN 19349 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19350 #endif 19351 19352 /* 19353 * Check to see if we need to re-enable LSO/MDT for this connection 19354 * because it was previously disabled due to changes in the ill; 19355 * note that by doing it here, this re-enabling only applies when 19356 * the packet is not dispatched through CALL_IP_WPUT(). 19357 * 19358 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19359 * case, since that's how we ended up here. For IPv6, we do the 19360 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19361 */ 19362 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19363 /* 19364 * Restore LSO for this connection, so that next time around 19365 * it is eligible to go through tcp_lsosend() path again. 19366 */ 19367 TCP_STAT(tcps, tcp_lso_enabled); 19368 tcp->tcp_lso = B_TRUE; 19369 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19370 "interface %s\n", (void *)connp, ill->ill_name)); 19371 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19372 /* 19373 * Restore MDT for this connection, so that next time around 19374 * it is eligible to go through tcp_multisend() path again. 19375 */ 19376 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19377 tcp->tcp_mdt = B_TRUE; 19378 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19379 "interface %s\n", (void *)connp, ill->ill_name)); 19380 } 19381 19382 if (tcp->tcp_snd_zcopy_aware) { 19383 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19384 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19385 mp = tcp_zcopy_disable(tcp, mp); 19386 /* 19387 * we shouldn't need to reset ipha as the mp containing 19388 * ipha should never be a zero-copy mp. 19389 */ 19390 } 19391 19392 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19393 ASSERT(ill->ill_hcksum_capab != NULL); 19394 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19395 } 19396 19397 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19398 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19399 19400 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19401 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19402 19403 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19404 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19405 19406 /* Software checksum? */ 19407 if (DB_CKSUMFLAGS(mp) == 0) { 19408 TCP_STAT(tcps, tcp_out_sw_cksum); 19409 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19410 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19411 } 19412 19413 /* Calculate IP header checksum if hardware isn't capable */ 19414 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19415 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19416 ((uint16_t *)ipha)[4]); 19417 } 19418 19419 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19420 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19421 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19422 19423 UPDATE_OB_PKT_COUNT(ire); 19424 ire->ire_last_used_time = lbolt; 19425 19426 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19427 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19428 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19429 ntohs(ipha->ipha_length)); 19430 19431 DTRACE_PROBE4(ip4__physical__out__start, 19432 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 19433 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19434 ipst->ips_ipv4firewall_physical_out, 19435 NULL, ill, ipha, mp, mp, 0, ipst); 19436 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19437 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 19438 19439 if (mp != NULL) { 19440 if (ipst->ips_ipobs_enabled) { 19441 zoneid_t szone; 19442 19443 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 19444 ipst, ALL_ZONES); 19445 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 19446 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 19447 } 19448 19449 ILL_SEND_TX(ill, ire, connp, mp, 0); 19450 } 19451 19452 IRE_REFRELE(ire); 19453 } 19454 19455 /* 19456 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19457 * if the receiver shrinks the window, i.e. moves the right window to the 19458 * left, the we should not send new data, but should retransmit normally the 19459 * old unacked data between suna and suna + swnd. We might has sent data 19460 * that is now outside the new window, pretend that we didn't send it. 19461 */ 19462 static void 19463 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19464 { 19465 uint32_t snxt = tcp->tcp_snxt; 19466 mblk_t *xmit_tail; 19467 int32_t offset; 19468 19469 ASSERT(shrunk_count > 0); 19470 19471 /* Pretend we didn't send the data outside the window */ 19472 snxt -= shrunk_count; 19473 19474 /* Get the mblk and the offset in it per the shrunk window */ 19475 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19476 19477 ASSERT(xmit_tail != NULL); 19478 19479 /* Reset all the values per the now shrunk window */ 19480 tcp->tcp_snxt = snxt; 19481 tcp->tcp_xmit_tail = xmit_tail; 19482 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19483 offset; 19484 tcp->tcp_unsent += shrunk_count; 19485 19486 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19487 /* 19488 * Make sure the timer is running so that we will probe a zero 19489 * window. 19490 */ 19491 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19492 } 19493 19494 19495 /* 19496 * The TCP normal data output path. 19497 * NOTE: the logic of the fast path is duplicated from this function. 19498 */ 19499 static void 19500 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19501 { 19502 int len; 19503 mblk_t *local_time; 19504 mblk_t *mp1; 19505 uint32_t snxt; 19506 int tail_unsent; 19507 int tcpstate; 19508 int usable = 0; 19509 mblk_t *xmit_tail; 19510 queue_t *q = tcp->tcp_wq; 19511 int32_t mss; 19512 int32_t num_sack_blk = 0; 19513 int32_t tcp_hdr_len; 19514 int32_t tcp_tcp_hdr_len; 19515 int mdt_thres; 19516 int rc; 19517 tcp_stack_t *tcps = tcp->tcp_tcps; 19518 ip_stack_t *ipst; 19519 19520 tcpstate = tcp->tcp_state; 19521 if (mp == NULL) { 19522 /* 19523 * tcp_wput_data() with NULL mp should only be called when 19524 * there is unsent data. 19525 */ 19526 ASSERT(tcp->tcp_unsent > 0); 19527 /* Really tacky... but we need this for detached closes. */ 19528 len = tcp->tcp_unsent; 19529 goto data_null; 19530 } 19531 19532 #if CCS_STATS 19533 wrw_stats.tot.count++; 19534 wrw_stats.tot.bytes += msgdsize(mp); 19535 #endif 19536 ASSERT(mp->b_datap->db_type == M_DATA); 19537 /* 19538 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19539 * or before a connection attempt has begun. 19540 */ 19541 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19542 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19543 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19544 #ifdef DEBUG 19545 cmn_err(CE_WARN, 19546 "tcp_wput_data: data after ordrel, %s", 19547 tcp_display(tcp, NULL, 19548 DISP_ADDR_AND_PORT)); 19549 #else 19550 if (tcp->tcp_debug) { 19551 (void) strlog(TCP_MOD_ID, 0, 1, 19552 SL_TRACE|SL_ERROR, 19553 "tcp_wput_data: data after ordrel, %s\n", 19554 tcp_display(tcp, NULL, 19555 DISP_ADDR_AND_PORT)); 19556 } 19557 #endif /* DEBUG */ 19558 } 19559 if (tcp->tcp_snd_zcopy_aware && 19560 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19561 tcp_zcopy_notify(tcp); 19562 freemsg(mp); 19563 mutex_enter(&tcp->tcp_non_sq_lock); 19564 if (tcp->tcp_flow_stopped && 19565 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19566 tcp_clrqfull(tcp); 19567 } 19568 mutex_exit(&tcp->tcp_non_sq_lock); 19569 return; 19570 } 19571 19572 /* Strip empties */ 19573 for (;;) { 19574 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19575 (uintptr_t)INT_MAX); 19576 len = (int)(mp->b_wptr - mp->b_rptr); 19577 if (len > 0) 19578 break; 19579 mp1 = mp; 19580 mp = mp->b_cont; 19581 freeb(mp1); 19582 if (!mp) { 19583 return; 19584 } 19585 } 19586 19587 /* If we are the first on the list ... */ 19588 if (tcp->tcp_xmit_head == NULL) { 19589 tcp->tcp_xmit_head = mp; 19590 tcp->tcp_xmit_tail = mp; 19591 tcp->tcp_xmit_tail_unsent = len; 19592 } else { 19593 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19594 struct datab *dp; 19595 19596 mp1 = tcp->tcp_xmit_last; 19597 if (len < tcp_tx_pull_len && 19598 (dp = mp1->b_datap)->db_ref == 1 && 19599 dp->db_lim - mp1->b_wptr >= len) { 19600 ASSERT(len > 0); 19601 ASSERT(!mp1->b_cont); 19602 if (len == 1) { 19603 *mp1->b_wptr++ = *mp->b_rptr; 19604 } else { 19605 bcopy(mp->b_rptr, mp1->b_wptr, len); 19606 mp1->b_wptr += len; 19607 } 19608 if (mp1 == tcp->tcp_xmit_tail) 19609 tcp->tcp_xmit_tail_unsent += len; 19610 mp1->b_cont = mp->b_cont; 19611 if (tcp->tcp_snd_zcopy_aware && 19612 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19613 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19614 freeb(mp); 19615 mp = mp1; 19616 } else { 19617 tcp->tcp_xmit_last->b_cont = mp; 19618 } 19619 len += tcp->tcp_unsent; 19620 } 19621 19622 /* Tack on however many more positive length mblks we have */ 19623 if ((mp1 = mp->b_cont) != NULL) { 19624 do { 19625 int tlen; 19626 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19627 (uintptr_t)INT_MAX); 19628 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19629 if (tlen <= 0) { 19630 mp->b_cont = mp1->b_cont; 19631 freeb(mp1); 19632 } else { 19633 len += tlen; 19634 mp = mp1; 19635 } 19636 } while ((mp1 = mp->b_cont) != NULL); 19637 } 19638 tcp->tcp_xmit_last = mp; 19639 tcp->tcp_unsent = len; 19640 19641 if (urgent) 19642 usable = 1; 19643 19644 data_null: 19645 snxt = tcp->tcp_snxt; 19646 xmit_tail = tcp->tcp_xmit_tail; 19647 tail_unsent = tcp->tcp_xmit_tail_unsent; 19648 19649 /* 19650 * Note that tcp_mss has been adjusted to take into account the 19651 * timestamp option if applicable. Because SACK options do not 19652 * appear in every TCP segments and they are of variable lengths, 19653 * they cannot be included in tcp_mss. Thus we need to calculate 19654 * the actual segment length when we need to send a segment which 19655 * includes SACK options. 19656 */ 19657 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19658 int32_t opt_len; 19659 19660 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19661 tcp->tcp_num_sack_blk); 19662 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19663 2 + TCPOPT_HEADER_LEN; 19664 mss = tcp->tcp_mss - opt_len; 19665 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19666 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19667 } else { 19668 mss = tcp->tcp_mss; 19669 tcp_hdr_len = tcp->tcp_hdr_len; 19670 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19671 } 19672 19673 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19674 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19675 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19676 } 19677 if (tcpstate == TCPS_SYN_RCVD) { 19678 /* 19679 * The three-way connection establishment handshake is not 19680 * complete yet. We want to queue the data for transmission 19681 * after entering ESTABLISHED state (RFC793). A jump to 19682 * "done" label effectively leaves data on the queue. 19683 */ 19684 goto done; 19685 } else { 19686 int usable_r; 19687 19688 /* 19689 * In the special case when cwnd is zero, which can only 19690 * happen if the connection is ECN capable, return now. 19691 * New segments is sent using tcp_timer(). The timer 19692 * is set in tcp_rput_data(). 19693 */ 19694 if (tcp->tcp_cwnd == 0) { 19695 /* 19696 * Note that tcp_cwnd is 0 before 3-way handshake is 19697 * finished. 19698 */ 19699 ASSERT(tcp->tcp_ecn_ok || 19700 tcp->tcp_state < TCPS_ESTABLISHED); 19701 return; 19702 } 19703 19704 /* NOTE: trouble if xmitting while SYN not acked? */ 19705 usable_r = snxt - tcp->tcp_suna; 19706 usable_r = tcp->tcp_swnd - usable_r; 19707 19708 /* 19709 * Check if the receiver has shrunk the window. If 19710 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19711 * cannot be set as there is unsent data, so FIN cannot 19712 * be sent out. Otherwise, we need to take into account 19713 * of FIN as it consumes an "invisible" sequence number. 19714 */ 19715 ASSERT(tcp->tcp_fin_sent == 0); 19716 if (usable_r < 0) { 19717 /* 19718 * The receiver has shrunk the window and we have sent 19719 * -usable_r date beyond the window, re-adjust. 19720 * 19721 * If TCP window scaling is enabled, there can be 19722 * round down error as the advertised receive window 19723 * is actually right shifted n bits. This means that 19724 * the lower n bits info is wiped out. It will look 19725 * like the window is shrunk. Do a check here to 19726 * see if the shrunk amount is actually within the 19727 * error in window calculation. If it is, just 19728 * return. Note that this check is inside the 19729 * shrunk window check. This makes sure that even 19730 * though tcp_process_shrunk_swnd() is not called, 19731 * we will stop further processing. 19732 */ 19733 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19734 tcp_process_shrunk_swnd(tcp, -usable_r); 19735 } 19736 return; 19737 } 19738 19739 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19740 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19741 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19742 19743 /* usable = MIN(usable, unsent) */ 19744 if (usable_r > len) 19745 usable_r = len; 19746 19747 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19748 if (usable_r > 0) { 19749 usable = usable_r; 19750 } else { 19751 /* Bypass all other unnecessary processing. */ 19752 goto done; 19753 } 19754 } 19755 19756 local_time = (mblk_t *)lbolt; 19757 19758 /* 19759 * "Our" Nagle Algorithm. This is not the same as in the old 19760 * BSD. This is more in line with the true intent of Nagle. 19761 * 19762 * The conditions are: 19763 * 1. The amount of unsent data (or amount of data which can be 19764 * sent, whichever is smaller) is less than Nagle limit. 19765 * 2. The last sent size is also less than Nagle limit. 19766 * 3. There is unack'ed data. 19767 * 4. Urgent pointer is not set. Send urgent data ignoring the 19768 * Nagle algorithm. This reduces the probability that urgent 19769 * bytes get "merged" together. 19770 * 5. The app has not closed the connection. This eliminates the 19771 * wait time of the receiving side waiting for the last piece of 19772 * (small) data. 19773 * 19774 * If all are satisified, exit without sending anything. Note 19775 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19776 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19777 * 4095). 19778 */ 19779 if (usable < (int)tcp->tcp_naglim && 19780 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19781 snxt != tcp->tcp_suna && 19782 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19783 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19784 goto done; 19785 } 19786 19787 if (tcp->tcp_cork) { 19788 /* 19789 * if the tcp->tcp_cork option is set, then we have to force 19790 * TCP not to send partial segment (smaller than MSS bytes). 19791 * We are calculating the usable now based on full mss and 19792 * will save the rest of remaining data for later. 19793 */ 19794 if (usable < mss) 19795 goto done; 19796 usable = (usable / mss) * mss; 19797 } 19798 19799 /* Update the latest receive window size in TCP header. */ 19800 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19801 tcp->tcp_tcph->th_win); 19802 19803 /* 19804 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19805 * 19806 * 1. Simple TCP/IP{v4,v6} (no options). 19807 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19808 * 3. If the TCP connection is in ESTABLISHED state. 19809 * 4. The TCP is not detached. 19810 * 19811 * If any of the above conditions have changed during the 19812 * connection, stop using LSO/MDT and restore the stream head 19813 * parameters accordingly. 19814 */ 19815 ipst = tcps->tcps_netstack->netstack_ip; 19816 19817 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19818 ((tcp->tcp_ipversion == IPV4_VERSION && 19819 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19820 (tcp->tcp_ipversion == IPV6_VERSION && 19821 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19822 tcp->tcp_state != TCPS_ESTABLISHED || 19823 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19824 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19825 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19826 if (tcp->tcp_lso) { 19827 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19828 tcp->tcp_lso = B_FALSE; 19829 } else { 19830 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19831 tcp->tcp_mdt = B_FALSE; 19832 } 19833 19834 /* Anything other than detached is considered pathological */ 19835 if (!TCP_IS_DETACHED(tcp)) { 19836 if (tcp->tcp_lso) 19837 TCP_STAT(tcps, tcp_lso_disabled); 19838 else 19839 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19840 (void) tcp_maxpsz_set(tcp, B_TRUE); 19841 } 19842 } 19843 19844 /* Use MDT if sendable amount is greater than the threshold */ 19845 if (tcp->tcp_mdt && 19846 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19847 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19848 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19849 (tcp->tcp_valid_bits == 0 || 19850 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19851 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19852 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19853 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19854 local_time, mdt_thres); 19855 } else { 19856 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19857 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19858 local_time, INT_MAX); 19859 } 19860 19861 /* Pretend that all we were trying to send really got sent */ 19862 if (rc < 0 && tail_unsent < 0) { 19863 do { 19864 xmit_tail = xmit_tail->b_cont; 19865 xmit_tail->b_prev = local_time; 19866 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19867 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19868 tail_unsent += (int)(xmit_tail->b_wptr - 19869 xmit_tail->b_rptr); 19870 } while (tail_unsent < 0); 19871 } 19872 done:; 19873 tcp->tcp_xmit_tail = xmit_tail; 19874 tcp->tcp_xmit_tail_unsent = tail_unsent; 19875 len = tcp->tcp_snxt - snxt; 19876 if (len) { 19877 /* 19878 * If new data was sent, need to update the notsack 19879 * list, which is, afterall, data blocks that have 19880 * not been sack'ed by the receiver. New data is 19881 * not sack'ed. 19882 */ 19883 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19884 /* len is a negative value. */ 19885 tcp->tcp_pipe -= len; 19886 tcp_notsack_update(&(tcp->tcp_notsack_list), 19887 tcp->tcp_snxt, snxt, 19888 &(tcp->tcp_num_notsack_blk), 19889 &(tcp->tcp_cnt_notsack_list)); 19890 } 19891 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19892 tcp->tcp_rack = tcp->tcp_rnxt; 19893 tcp->tcp_rack_cnt = 0; 19894 if ((snxt + len) == tcp->tcp_suna) { 19895 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19896 } 19897 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19898 /* 19899 * Didn't send anything. Make sure the timer is running 19900 * so that we will probe a zero window. 19901 */ 19902 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19903 } 19904 /* Note that len is the amount we just sent but with a negative sign */ 19905 tcp->tcp_unsent += len; 19906 mutex_enter(&tcp->tcp_non_sq_lock); 19907 if (tcp->tcp_flow_stopped) { 19908 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19909 tcp_clrqfull(tcp); 19910 } 19911 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19912 tcp_setqfull(tcp); 19913 } 19914 mutex_exit(&tcp->tcp_non_sq_lock); 19915 } 19916 19917 /* 19918 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19919 * outgoing TCP header with the template header, as well as other 19920 * options such as time-stamp, ECN and/or SACK. 19921 */ 19922 static void 19923 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19924 { 19925 tcph_t *tcp_tmpl, *tcp_h; 19926 uint32_t *dst, *src; 19927 int hdrlen; 19928 19929 ASSERT(OK_32PTR(rptr)); 19930 19931 /* Template header */ 19932 tcp_tmpl = tcp->tcp_tcph; 19933 19934 /* Header of outgoing packet */ 19935 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19936 19937 /* dst and src are opaque 32-bit fields, used for copying */ 19938 dst = (uint32_t *)rptr; 19939 src = (uint32_t *)tcp->tcp_iphc; 19940 hdrlen = tcp->tcp_hdr_len; 19941 19942 /* Fill time-stamp option if needed */ 19943 if (tcp->tcp_snd_ts_ok) { 19944 U32_TO_BE32((uint32_t)now, 19945 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19946 U32_TO_BE32(tcp->tcp_ts_recent, 19947 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19948 } else { 19949 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19950 } 19951 19952 /* 19953 * Copy the template header; is this really more efficient than 19954 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19955 * but perhaps not for other scenarios. 19956 */ 19957 dst[0] = src[0]; 19958 dst[1] = src[1]; 19959 dst[2] = src[2]; 19960 dst[3] = src[3]; 19961 dst[4] = src[4]; 19962 dst[5] = src[5]; 19963 dst[6] = src[6]; 19964 dst[7] = src[7]; 19965 dst[8] = src[8]; 19966 dst[9] = src[9]; 19967 if (hdrlen -= 40) { 19968 hdrlen >>= 2; 19969 dst += 10; 19970 src += 10; 19971 do { 19972 *dst++ = *src++; 19973 } while (--hdrlen); 19974 } 19975 19976 /* 19977 * Set the ECN info in the TCP header if it is not a zero 19978 * window probe. Zero window probe is only sent in 19979 * tcp_wput_data() and tcp_timer(). 19980 */ 19981 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19982 SET_ECT(tcp, rptr); 19983 19984 if (tcp->tcp_ecn_echo_on) 19985 tcp_h->th_flags[0] |= TH_ECE; 19986 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19987 tcp_h->th_flags[0] |= TH_CWR; 19988 tcp->tcp_ecn_cwr_sent = B_TRUE; 19989 } 19990 } 19991 19992 /* Fill in SACK options */ 19993 if (num_sack_blk > 0) { 19994 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19995 sack_blk_t *tmp; 19996 int32_t i; 19997 19998 wptr[0] = TCPOPT_NOP; 19999 wptr[1] = TCPOPT_NOP; 20000 wptr[2] = TCPOPT_SACK; 20001 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 20002 sizeof (sack_blk_t); 20003 wptr += TCPOPT_REAL_SACK_LEN; 20004 20005 tmp = tcp->tcp_sack_list; 20006 for (i = 0; i < num_sack_blk; i++) { 20007 U32_TO_BE32(tmp[i].begin, wptr); 20008 wptr += sizeof (tcp_seq); 20009 U32_TO_BE32(tmp[i].end, wptr); 20010 wptr += sizeof (tcp_seq); 20011 } 20012 tcp_h->th_offset_and_rsrvd[0] += 20013 ((num_sack_blk * 2 + 1) << 4); 20014 } 20015 } 20016 20017 /* 20018 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20019 * the destination address and SAP attribute, and if necessary, the 20020 * hardware checksum offload attribute to a Multidata message. 20021 */ 20022 static int 20023 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20024 const uint32_t start, const uint32_t stuff, const uint32_t end, 20025 const uint32_t flags, tcp_stack_t *tcps) 20026 { 20027 /* Add global destination address & SAP attribute */ 20028 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20029 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20030 "destination address+SAP\n")); 20031 20032 if (dlmp != NULL) 20033 TCP_STAT(tcps, tcp_mdt_allocfail); 20034 return (-1); 20035 } 20036 20037 /* Add global hwcksum attribute */ 20038 if (hwcksum && 20039 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20040 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20041 "checksum attribute\n")); 20042 20043 TCP_STAT(tcps, tcp_mdt_allocfail); 20044 return (-1); 20045 } 20046 20047 return (0); 20048 } 20049 20050 /* 20051 * Smaller and private version of pdescinfo_t used specifically for TCP, 20052 * which allows for only two payload spans per packet. 20053 */ 20054 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20055 20056 /* 20057 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20058 * scheme, and returns one the following: 20059 * 20060 * -1 = failed allocation. 20061 * 0 = success; burst count reached, or usable send window is too small, 20062 * and that we'd rather wait until later before sending again. 20063 */ 20064 static int 20065 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20066 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20067 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20068 const int mdt_thres) 20069 { 20070 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20071 multidata_t *mmd; 20072 uint_t obsegs, obbytes, hdr_frag_sz; 20073 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20074 int num_burst_seg, max_pld; 20075 pdesc_t *pkt; 20076 tcp_pdescinfo_t tcp_pkt_info; 20077 pdescinfo_t *pkt_info; 20078 int pbuf_idx, pbuf_idx_nxt; 20079 int seg_len, len, spill, af; 20080 boolean_t add_buffer, zcopy, clusterwide; 20081 boolean_t rconfirm = B_FALSE; 20082 boolean_t done = B_FALSE; 20083 uint32_t cksum; 20084 uint32_t hwcksum_flags; 20085 ire_t *ire = NULL; 20086 ill_t *ill; 20087 ipha_t *ipha; 20088 ip6_t *ip6h; 20089 ipaddr_t src, dst; 20090 ill_zerocopy_capab_t *zc_cap = NULL; 20091 uint16_t *up; 20092 int err; 20093 conn_t *connp; 20094 tcp_stack_t *tcps = tcp->tcp_tcps; 20095 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20096 int usable_mmd, tail_unsent_mmd; 20097 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20098 mblk_t *xmit_tail_mmd; 20099 netstackid_t stack_id; 20100 20101 #ifdef _BIG_ENDIAN 20102 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20103 #else 20104 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20105 #endif 20106 20107 #define PREP_NEW_MULTIDATA() { \ 20108 mmd = NULL; \ 20109 md_mp = md_hbuf = NULL; \ 20110 cur_hdr_off = 0; \ 20111 max_pld = tcp->tcp_mdt_max_pld; \ 20112 pbuf_idx = pbuf_idx_nxt = -1; \ 20113 add_buffer = B_TRUE; \ 20114 zcopy = B_FALSE; \ 20115 } 20116 20117 #define PREP_NEW_PBUF() { \ 20118 md_pbuf = md_pbuf_nxt = NULL; \ 20119 pbuf_idx = pbuf_idx_nxt = -1; \ 20120 cur_pld_off = 0; \ 20121 first_snxt = *snxt; \ 20122 ASSERT(*tail_unsent > 0); \ 20123 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20124 } 20125 20126 ASSERT(mdt_thres >= mss); 20127 ASSERT(*usable > 0 && *usable > mdt_thres); 20128 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20129 ASSERT(!TCP_IS_DETACHED(tcp)); 20130 ASSERT(tcp->tcp_valid_bits == 0 || 20131 tcp->tcp_valid_bits == TCP_FSS_VALID); 20132 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20133 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20134 (tcp->tcp_ipversion == IPV6_VERSION && 20135 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20136 20137 connp = tcp->tcp_connp; 20138 ASSERT(connp != NULL); 20139 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20140 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20141 20142 stack_id = connp->conn_netstack->netstack_stackid; 20143 20144 usable_mmd = tail_unsent_mmd = 0; 20145 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20146 xmit_tail_mmd = NULL; 20147 /* 20148 * Note that tcp will only declare at most 2 payload spans per 20149 * packet, which is much lower than the maximum allowable number 20150 * of packet spans per Multidata. For this reason, we use the 20151 * privately declared and smaller descriptor info structure, in 20152 * order to save some stack space. 20153 */ 20154 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20155 20156 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20157 if (af == AF_INET) { 20158 dst = tcp->tcp_ipha->ipha_dst; 20159 src = tcp->tcp_ipha->ipha_src; 20160 ASSERT(!CLASSD(dst)); 20161 } 20162 ASSERT(af == AF_INET || 20163 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20164 20165 obsegs = obbytes = 0; 20166 num_burst_seg = tcp->tcp_snd_burst; 20167 md_mp_head = NULL; 20168 PREP_NEW_MULTIDATA(); 20169 20170 /* 20171 * Before we go on further, make sure there is an IRE that we can 20172 * use, and that the ILL supports MDT. Otherwise, there's no point 20173 * in proceeding any further, and we should just hand everything 20174 * off to the legacy path. 20175 */ 20176 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20177 goto legacy_send_no_md; 20178 20179 ASSERT(ire != NULL); 20180 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20181 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20182 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20183 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20184 /* 20185 * If we do support loopback for MDT (which requires modifications 20186 * to the receiving paths), the following assertions should go away, 20187 * and we would be sending the Multidata to loopback conn later on. 20188 */ 20189 ASSERT(!IRE_IS_LOCAL(ire)); 20190 ASSERT(ire->ire_stq != NULL); 20191 20192 ill = ire_to_ill(ire); 20193 ASSERT(ill != NULL); 20194 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20195 20196 if (!tcp->tcp_ire_ill_check_done) { 20197 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20198 tcp->tcp_ire_ill_check_done = B_TRUE; 20199 } 20200 20201 /* 20202 * If the underlying interface conditions have changed, or if the 20203 * new interface does not support MDT, go back to legacy path. 20204 */ 20205 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20206 /* don't go through this path anymore for this connection */ 20207 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20208 tcp->tcp_mdt = B_FALSE; 20209 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20210 "interface %s\n", (void *)connp, ill->ill_name)); 20211 /* IRE will be released prior to returning */ 20212 goto legacy_send_no_md; 20213 } 20214 20215 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20216 zc_cap = ill->ill_zerocopy_capab; 20217 20218 /* 20219 * Check if we can take tcp fast-path. Note that "incomplete" 20220 * ire's (where the link-layer for next hop is not resolved 20221 * or where the fast-path header in nce_fp_mp is not available 20222 * yet) are sent down the legacy (slow) path. 20223 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20224 */ 20225 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20226 /* IRE will be released prior to returning */ 20227 goto legacy_send_no_md; 20228 } 20229 20230 /* go to legacy path if interface doesn't support zerocopy */ 20231 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20232 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20233 /* IRE will be released prior to returning */ 20234 goto legacy_send_no_md; 20235 } 20236 20237 /* does the interface support hardware checksum offload? */ 20238 hwcksum_flags = 0; 20239 if (ILL_HCKSUM_CAPABLE(ill) && 20240 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20241 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20242 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20243 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20244 HCKSUM_IPHDRCKSUM) 20245 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20246 20247 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20248 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20249 hwcksum_flags |= HCK_FULLCKSUM; 20250 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20251 HCKSUM_INET_PARTIAL) 20252 hwcksum_flags |= HCK_PARTIALCKSUM; 20253 } 20254 20255 /* 20256 * Each header fragment consists of the leading extra space, 20257 * followed by the TCP/IP header, and the trailing extra space. 20258 * We make sure that each header fragment begins on a 32-bit 20259 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20260 * aligned in tcp_mdt_update). 20261 */ 20262 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20263 tcp->tcp_mdt_hdr_tail), 4); 20264 20265 /* are we starting from the beginning of data block? */ 20266 if (*tail_unsent == 0) { 20267 *xmit_tail = (*xmit_tail)->b_cont; 20268 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20269 *tail_unsent = (int)MBLKL(*xmit_tail); 20270 } 20271 20272 /* 20273 * Here we create one or more Multidata messages, each made up of 20274 * one header buffer and up to N payload buffers. This entire 20275 * operation is done within two loops: 20276 * 20277 * The outer loop mostly deals with creating the Multidata message, 20278 * as well as the header buffer that gets added to it. It also 20279 * links the Multidata messages together such that all of them can 20280 * be sent down to the lower layer in a single putnext call; this 20281 * linking behavior depends on the tcp_mdt_chain tunable. 20282 * 20283 * The inner loop takes an existing Multidata message, and adds 20284 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20285 * packetizes those buffers by filling up the corresponding header 20286 * buffer fragments with the proper IP and TCP headers, and by 20287 * describing the layout of each packet in the packet descriptors 20288 * that get added to the Multidata. 20289 */ 20290 do { 20291 /* 20292 * If usable send window is too small, or data blocks in 20293 * transmit list are smaller than our threshold (i.e. app 20294 * performs large writes followed by small ones), we hand 20295 * off the control over to the legacy path. Note that we'll 20296 * get back the control once it encounters a large block. 20297 */ 20298 if (*usable < mss || (*tail_unsent <= mdt_thres && 20299 (*xmit_tail)->b_cont != NULL && 20300 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20301 /* send down what we've got so far */ 20302 if (md_mp_head != NULL) { 20303 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20304 obsegs, obbytes, &rconfirm); 20305 } 20306 /* 20307 * Pass control over to tcp_send(), but tell it to 20308 * return to us once a large-size transmission is 20309 * possible. 20310 */ 20311 TCP_STAT(tcps, tcp_mdt_legacy_small); 20312 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20313 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20314 tail_unsent, xmit_tail, local_time, 20315 mdt_thres)) <= 0) { 20316 /* burst count reached, or alloc failed */ 20317 IRE_REFRELE(ire); 20318 return (err); 20319 } 20320 20321 /* tcp_send() may have sent everything, so check */ 20322 if (*usable <= 0) { 20323 IRE_REFRELE(ire); 20324 return (0); 20325 } 20326 20327 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20328 /* 20329 * We may have delivered the Multidata, so make sure 20330 * to re-initialize before the next round. 20331 */ 20332 md_mp_head = NULL; 20333 obsegs = obbytes = 0; 20334 num_burst_seg = tcp->tcp_snd_burst; 20335 PREP_NEW_MULTIDATA(); 20336 20337 /* are we starting from the beginning of data block? */ 20338 if (*tail_unsent == 0) { 20339 *xmit_tail = (*xmit_tail)->b_cont; 20340 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20341 (uintptr_t)INT_MAX); 20342 *tail_unsent = (int)MBLKL(*xmit_tail); 20343 } 20344 } 20345 /* 20346 * Record current values for parameters we may need to pass 20347 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20348 * each iteration of the outer loop (each multidata message 20349 * creation). If we have a failure in the inner loop, we send 20350 * any complete multidata messages we have before reverting 20351 * to using the traditional non-md path. 20352 */ 20353 snxt_mmd = *snxt; 20354 usable_mmd = *usable; 20355 xmit_tail_mmd = *xmit_tail; 20356 tail_unsent_mmd = *tail_unsent; 20357 obsegs_mmd = obsegs; 20358 obbytes_mmd = obbytes; 20359 20360 /* 20361 * max_pld limits the number of mblks in tcp's transmit 20362 * queue that can be added to a Multidata message. Once 20363 * this counter reaches zero, no more additional mblks 20364 * can be added to it. What happens afterwards depends 20365 * on whether or not we are set to chain the Multidata 20366 * messages. If we are to link them together, reset 20367 * max_pld to its original value (tcp_mdt_max_pld) and 20368 * prepare to create a new Multidata message which will 20369 * get linked to md_mp_head. Else, leave it alone and 20370 * let the inner loop break on its own. 20371 */ 20372 if (tcp_mdt_chain && max_pld == 0) 20373 PREP_NEW_MULTIDATA(); 20374 20375 /* adding a payload buffer; re-initialize values */ 20376 if (add_buffer) 20377 PREP_NEW_PBUF(); 20378 20379 /* 20380 * If we don't have a Multidata, either because we just 20381 * (re)entered this outer loop, or after we branched off 20382 * to tcp_send above, setup the Multidata and header 20383 * buffer to be used. 20384 */ 20385 if (md_mp == NULL) { 20386 int md_hbuflen; 20387 uint32_t start, stuff; 20388 20389 /* 20390 * Calculate Multidata header buffer size large enough 20391 * to hold all of the headers that can possibly be 20392 * sent at this moment. We'd rather over-estimate 20393 * the size than running out of space; this is okay 20394 * since this buffer is small anyway. 20395 */ 20396 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20397 20398 /* 20399 * Start and stuff offset for partial hardware 20400 * checksum offload; these are currently for IPv4. 20401 * For full checksum offload, they are set to zero. 20402 */ 20403 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20404 if (af == AF_INET) { 20405 start = IP_SIMPLE_HDR_LENGTH; 20406 stuff = IP_SIMPLE_HDR_LENGTH + 20407 TCP_CHECKSUM_OFFSET; 20408 } else { 20409 start = IPV6_HDR_LEN; 20410 stuff = IPV6_HDR_LEN + 20411 TCP_CHECKSUM_OFFSET; 20412 } 20413 } else { 20414 start = stuff = 0; 20415 } 20416 20417 /* 20418 * Create the header buffer, Multidata, as well as 20419 * any necessary attributes (destination address, 20420 * SAP and hardware checksum offload) that should 20421 * be associated with the Multidata message. 20422 */ 20423 ASSERT(cur_hdr_off == 0); 20424 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20425 ((md_hbuf->b_wptr += md_hbuflen), 20426 (mmd = mmd_alloc(md_hbuf, &md_mp, 20427 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20428 /* fastpath mblk */ 20429 ire->ire_nce->nce_res_mp, 20430 /* hardware checksum enabled */ 20431 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20432 /* hardware checksum offsets */ 20433 start, stuff, 0, 20434 /* hardware checksum flag */ 20435 hwcksum_flags, tcps) != 0)) { 20436 legacy_send: 20437 /* 20438 * We arrive here from a failure within the 20439 * inner (packetizer) loop or we fail one of 20440 * the conditionals above. We restore the 20441 * previously checkpointed values for: 20442 * xmit_tail 20443 * usable 20444 * tail_unsent 20445 * snxt 20446 * obbytes 20447 * obsegs 20448 * We should then be able to dispatch any 20449 * complete multidata before reverting to the 20450 * traditional path with consistent parameters 20451 * (the inner loop updates these as it 20452 * iterates). 20453 */ 20454 *xmit_tail = xmit_tail_mmd; 20455 *usable = usable_mmd; 20456 *tail_unsent = tail_unsent_mmd; 20457 *snxt = snxt_mmd; 20458 obbytes = obbytes_mmd; 20459 obsegs = obsegs_mmd; 20460 if (md_mp != NULL) { 20461 /* Unlink message from the chain */ 20462 if (md_mp_head != NULL) { 20463 err = (intptr_t)rmvb(md_mp_head, 20464 md_mp); 20465 /* 20466 * We can't assert that rmvb 20467 * did not return -1, since we 20468 * may get here before linkb 20469 * happens. We do, however, 20470 * check if we just removed the 20471 * only element in the list. 20472 */ 20473 if (err == 0) 20474 md_mp_head = NULL; 20475 } 20476 /* md_hbuf gets freed automatically */ 20477 TCP_STAT(tcps, tcp_mdt_discarded); 20478 freeb(md_mp); 20479 } else { 20480 /* Either allocb or mmd_alloc failed */ 20481 TCP_STAT(tcps, tcp_mdt_allocfail); 20482 if (md_hbuf != NULL) 20483 freeb(md_hbuf); 20484 } 20485 20486 /* send down what we've got so far */ 20487 if (md_mp_head != NULL) { 20488 tcp_multisend_data(tcp, ire, ill, 20489 md_mp_head, obsegs, obbytes, 20490 &rconfirm); 20491 } 20492 legacy_send_no_md: 20493 if (ire != NULL) 20494 IRE_REFRELE(ire); 20495 /* 20496 * Too bad; let the legacy path handle this. 20497 * We specify INT_MAX for the threshold, since 20498 * we gave up with the Multidata processings 20499 * and let the old path have it all. 20500 */ 20501 TCP_STAT(tcps, tcp_mdt_legacy_all); 20502 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20503 tcp_tcp_hdr_len, num_sack_blk, usable, 20504 snxt, tail_unsent, xmit_tail, local_time, 20505 INT_MAX)); 20506 } 20507 20508 /* link to any existing ones, if applicable */ 20509 TCP_STAT(tcps, tcp_mdt_allocd); 20510 if (md_mp_head == NULL) { 20511 md_mp_head = md_mp; 20512 } else if (tcp_mdt_chain) { 20513 TCP_STAT(tcps, tcp_mdt_linked); 20514 linkb(md_mp_head, md_mp); 20515 } 20516 } 20517 20518 ASSERT(md_mp_head != NULL); 20519 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20520 ASSERT(md_mp != NULL && mmd != NULL); 20521 ASSERT(md_hbuf != NULL); 20522 20523 /* 20524 * Packetize the transmittable portion of the data block; 20525 * each data block is essentially added to the Multidata 20526 * as a payload buffer. We also deal with adding more 20527 * than one payload buffers, which happens when the remaining 20528 * packetized portion of the current payload buffer is less 20529 * than MSS, while the next data block in transmit queue 20530 * has enough data to make up for one. This "spillover" 20531 * case essentially creates a split-packet, where portions 20532 * of the packet's payload fragments may span across two 20533 * virtually discontiguous address blocks. 20534 */ 20535 seg_len = mss; 20536 do { 20537 len = seg_len; 20538 20539 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20540 ipha = NULL; 20541 ip6h = NULL; 20542 20543 ASSERT(len > 0); 20544 ASSERT(max_pld >= 0); 20545 ASSERT(!add_buffer || cur_pld_off == 0); 20546 20547 /* 20548 * First time around for this payload buffer; note 20549 * in the case of a spillover, the following has 20550 * been done prior to adding the split-packet 20551 * descriptor to Multidata, and we don't want to 20552 * repeat the process. 20553 */ 20554 if (add_buffer) { 20555 ASSERT(mmd != NULL); 20556 ASSERT(md_pbuf == NULL); 20557 ASSERT(md_pbuf_nxt == NULL); 20558 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20559 20560 /* 20561 * Have we reached the limit? We'd get to 20562 * this case when we're not chaining the 20563 * Multidata messages together, and since 20564 * we're done, terminate this loop. 20565 */ 20566 if (max_pld == 0) 20567 break; /* done */ 20568 20569 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20570 TCP_STAT(tcps, tcp_mdt_allocfail); 20571 goto legacy_send; /* out_of_mem */ 20572 } 20573 20574 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20575 zc_cap != NULL) { 20576 if (!ip_md_zcopy_attr(mmd, NULL, 20577 zc_cap->ill_zerocopy_flags)) { 20578 freeb(md_pbuf); 20579 TCP_STAT(tcps, 20580 tcp_mdt_allocfail); 20581 /* out_of_mem */ 20582 goto legacy_send; 20583 } 20584 zcopy = B_TRUE; 20585 } 20586 20587 md_pbuf->b_rptr += base_pld_off; 20588 20589 /* 20590 * Add a payload buffer to the Multidata; this 20591 * operation must not fail, or otherwise our 20592 * logic in this routine is broken. There 20593 * is no memory allocation done by the 20594 * routine, so any returned failure simply 20595 * tells us that we've done something wrong. 20596 * 20597 * A failure tells us that either we're adding 20598 * the same payload buffer more than once, or 20599 * we're trying to add more buffers than 20600 * allowed (max_pld calculation is wrong). 20601 * None of the above cases should happen, and 20602 * we panic because either there's horrible 20603 * heap corruption, and/or programming mistake. 20604 */ 20605 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20606 if (pbuf_idx < 0) { 20607 cmn_err(CE_PANIC, "tcp_multisend: " 20608 "payload buffer logic error " 20609 "detected for tcp %p mmd %p " 20610 "pbuf %p (%d)\n", 20611 (void *)tcp, (void *)mmd, 20612 (void *)md_pbuf, pbuf_idx); 20613 } 20614 20615 ASSERT(max_pld > 0); 20616 --max_pld; 20617 add_buffer = B_FALSE; 20618 } 20619 20620 ASSERT(md_mp_head != NULL); 20621 ASSERT(md_pbuf != NULL); 20622 ASSERT(md_pbuf_nxt == NULL); 20623 ASSERT(pbuf_idx != -1); 20624 ASSERT(pbuf_idx_nxt == -1); 20625 ASSERT(*usable > 0); 20626 20627 /* 20628 * We spillover to the next payload buffer only 20629 * if all of the following is true: 20630 * 20631 * 1. There is not enough data on the current 20632 * payload buffer to make up `len', 20633 * 2. We are allowed to send `len', 20634 * 3. The next payload buffer length is large 20635 * enough to accomodate `spill'. 20636 */ 20637 if ((spill = len - *tail_unsent) > 0 && 20638 *usable >= len && 20639 MBLKL((*xmit_tail)->b_cont) >= spill && 20640 max_pld > 0) { 20641 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20642 if (md_pbuf_nxt == NULL) { 20643 TCP_STAT(tcps, tcp_mdt_allocfail); 20644 goto legacy_send; /* out_of_mem */ 20645 } 20646 20647 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20648 zc_cap != NULL) { 20649 if (!ip_md_zcopy_attr(mmd, NULL, 20650 zc_cap->ill_zerocopy_flags)) { 20651 freeb(md_pbuf_nxt); 20652 TCP_STAT(tcps, 20653 tcp_mdt_allocfail); 20654 /* out_of_mem */ 20655 goto legacy_send; 20656 } 20657 zcopy = B_TRUE; 20658 } 20659 20660 /* 20661 * See comments above on the first call to 20662 * mmd_addpldbuf for explanation on the panic. 20663 */ 20664 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20665 if (pbuf_idx_nxt < 0) { 20666 panic("tcp_multisend: " 20667 "next payload buffer logic error " 20668 "detected for tcp %p mmd %p " 20669 "pbuf %p (%d)\n", 20670 (void *)tcp, (void *)mmd, 20671 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20672 } 20673 20674 ASSERT(max_pld > 0); 20675 --max_pld; 20676 } else if (spill > 0) { 20677 /* 20678 * If there's a spillover, but the following 20679 * xmit_tail couldn't give us enough octets 20680 * to reach "len", then stop the current 20681 * Multidata creation and let the legacy 20682 * tcp_send() path take over. We don't want 20683 * to send the tiny segment as part of this 20684 * Multidata for performance reasons; instead, 20685 * we let the legacy path deal with grouping 20686 * it with the subsequent small mblks. 20687 */ 20688 if (*usable >= len && 20689 MBLKL((*xmit_tail)->b_cont) < spill) { 20690 max_pld = 0; 20691 break; /* done */ 20692 } 20693 20694 /* 20695 * We can't spillover, and we are near 20696 * the end of the current payload buffer, 20697 * so send what's left. 20698 */ 20699 ASSERT(*tail_unsent > 0); 20700 len = *tail_unsent; 20701 } 20702 20703 /* tail_unsent is negated if there is a spillover */ 20704 *tail_unsent -= len; 20705 *usable -= len; 20706 ASSERT(*usable >= 0); 20707 20708 if (*usable < mss) 20709 seg_len = *usable; 20710 /* 20711 * Sender SWS avoidance; see comments in tcp_send(); 20712 * everything else is the same, except that we only 20713 * do this here if there is no more data to be sent 20714 * following the current xmit_tail. We don't check 20715 * for 1-byte urgent data because we shouldn't get 20716 * here if TCP_URG_VALID is set. 20717 */ 20718 if (*usable > 0 && *usable < mss && 20719 ((md_pbuf_nxt == NULL && 20720 (*xmit_tail)->b_cont == NULL) || 20721 (md_pbuf_nxt != NULL && 20722 (*xmit_tail)->b_cont->b_cont == NULL)) && 20723 seg_len < (tcp->tcp_max_swnd >> 1) && 20724 (tcp->tcp_unsent - 20725 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20726 !tcp->tcp_zero_win_probe) { 20727 if ((*snxt + len) == tcp->tcp_snxt && 20728 (*snxt + len) == tcp->tcp_suna) { 20729 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20730 } 20731 done = B_TRUE; 20732 } 20733 20734 /* 20735 * Prime pump for IP's checksumming on our behalf; 20736 * include the adjustment for a source route if any. 20737 * Do this only for software/partial hardware checksum 20738 * offload, as this field gets zeroed out later for 20739 * the full hardware checksum offload case. 20740 */ 20741 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20742 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20743 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20744 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20745 } 20746 20747 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20748 *snxt += len; 20749 20750 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20751 /* 20752 * We set the PUSH bit only if TCP has no more buffered 20753 * data to be transmitted (or if sender SWS avoidance 20754 * takes place), as opposed to setting it for every 20755 * last packet in the burst. 20756 */ 20757 if (done || 20758 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20759 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20760 20761 /* 20762 * Set FIN bit if this is our last segment; snxt 20763 * already includes its length, and it will not 20764 * be adjusted after this point. 20765 */ 20766 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20767 *snxt == tcp->tcp_fss) { 20768 if (!tcp->tcp_fin_acked) { 20769 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20770 BUMP_MIB(&tcps->tcps_mib, 20771 tcpOutControl); 20772 } 20773 if (!tcp->tcp_fin_sent) { 20774 tcp->tcp_fin_sent = B_TRUE; 20775 /* 20776 * tcp state must be ESTABLISHED 20777 * in order for us to get here in 20778 * the first place. 20779 */ 20780 tcp->tcp_state = TCPS_FIN_WAIT_1; 20781 20782 /* 20783 * Upon returning from this routine, 20784 * tcp_wput_data() will set tcp_snxt 20785 * to be equal to snxt + tcp_fin_sent. 20786 * This is essentially the same as 20787 * setting it to tcp_fss + 1. 20788 */ 20789 } 20790 } 20791 20792 tcp->tcp_last_sent_len = (ushort_t)len; 20793 20794 len += tcp_hdr_len; 20795 if (tcp->tcp_ipversion == IPV4_VERSION) 20796 tcp->tcp_ipha->ipha_length = htons(len); 20797 else 20798 tcp->tcp_ip6h->ip6_plen = htons(len - 20799 ((char *)&tcp->tcp_ip6h[1] - 20800 tcp->tcp_iphc)); 20801 20802 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20803 20804 /* setup header fragment */ 20805 PDESC_HDR_ADD(pkt_info, 20806 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20807 tcp->tcp_mdt_hdr_head, /* head room */ 20808 tcp_hdr_len, /* len */ 20809 tcp->tcp_mdt_hdr_tail); /* tail room */ 20810 20811 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20812 hdr_frag_sz); 20813 ASSERT(MBLKIN(md_hbuf, 20814 (pkt_info->hdr_base - md_hbuf->b_rptr), 20815 PDESC_HDRSIZE(pkt_info))); 20816 20817 /* setup first payload fragment */ 20818 PDESC_PLD_INIT(pkt_info); 20819 PDESC_PLD_SPAN_ADD(pkt_info, 20820 pbuf_idx, /* index */ 20821 md_pbuf->b_rptr + cur_pld_off, /* start */ 20822 tcp->tcp_last_sent_len); /* len */ 20823 20824 /* create a split-packet in case of a spillover */ 20825 if (md_pbuf_nxt != NULL) { 20826 ASSERT(spill > 0); 20827 ASSERT(pbuf_idx_nxt > pbuf_idx); 20828 ASSERT(!add_buffer); 20829 20830 md_pbuf = md_pbuf_nxt; 20831 md_pbuf_nxt = NULL; 20832 pbuf_idx = pbuf_idx_nxt; 20833 pbuf_idx_nxt = -1; 20834 cur_pld_off = spill; 20835 20836 /* trim out first payload fragment */ 20837 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20838 20839 /* setup second payload fragment */ 20840 PDESC_PLD_SPAN_ADD(pkt_info, 20841 pbuf_idx, /* index */ 20842 md_pbuf->b_rptr, /* start */ 20843 spill); /* len */ 20844 20845 if ((*xmit_tail)->b_next == NULL) { 20846 /* 20847 * Store the lbolt used for RTT 20848 * estimation. We can only record one 20849 * timestamp per mblk so we do it when 20850 * we reach the end of the payload 20851 * buffer. Also we only take a new 20852 * timestamp sample when the previous 20853 * timed data from the same mblk has 20854 * been ack'ed. 20855 */ 20856 (*xmit_tail)->b_prev = local_time; 20857 (*xmit_tail)->b_next = 20858 (mblk_t *)(uintptr_t)first_snxt; 20859 } 20860 20861 first_snxt = *snxt - spill; 20862 20863 /* 20864 * Advance xmit_tail; usable could be 0 by 20865 * the time we got here, but we made sure 20866 * above that we would only spillover to 20867 * the next data block if usable includes 20868 * the spilled-over amount prior to the 20869 * subtraction. Therefore, we are sure 20870 * that xmit_tail->b_cont can't be NULL. 20871 */ 20872 ASSERT((*xmit_tail)->b_cont != NULL); 20873 *xmit_tail = (*xmit_tail)->b_cont; 20874 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20875 (uintptr_t)INT_MAX); 20876 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20877 } else { 20878 cur_pld_off += tcp->tcp_last_sent_len; 20879 } 20880 20881 /* 20882 * Fill in the header using the template header, and 20883 * add options such as time-stamp, ECN and/or SACK, 20884 * as needed. 20885 */ 20886 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20887 (clock_t)local_time, num_sack_blk); 20888 20889 /* take care of some IP header businesses */ 20890 if (af == AF_INET) { 20891 ipha = (ipha_t *)pkt_info->hdr_rptr; 20892 20893 ASSERT(OK_32PTR((uchar_t *)ipha)); 20894 ASSERT(PDESC_HDRL(pkt_info) >= 20895 IP_SIMPLE_HDR_LENGTH); 20896 ASSERT(ipha->ipha_version_and_hdr_length == 20897 IP_SIMPLE_HDR_VERSION); 20898 20899 /* 20900 * Assign ident value for current packet; see 20901 * related comments in ip_wput_ire() about the 20902 * contract private interface with clustering 20903 * group. 20904 */ 20905 clusterwide = B_FALSE; 20906 if (cl_inet_ipident != NULL) { 20907 ASSERT(cl_inet_isclusterwide != NULL); 20908 if ((*cl_inet_isclusterwide)(stack_id, 20909 IPPROTO_IP, AF_INET, 20910 (uint8_t *)(uintptr_t)src, NULL)) { 20911 ipha->ipha_ident = 20912 (*cl_inet_ipident)(stack_id, 20913 IPPROTO_IP, AF_INET, 20914 (uint8_t *)(uintptr_t)src, 20915 (uint8_t *)(uintptr_t)dst, 20916 NULL); 20917 clusterwide = B_TRUE; 20918 } 20919 } 20920 20921 if (!clusterwide) { 20922 ipha->ipha_ident = (uint16_t) 20923 atomic_add_32_nv( 20924 &ire->ire_ident, 1); 20925 } 20926 #ifndef _BIG_ENDIAN 20927 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20928 (ipha->ipha_ident >> 8); 20929 #endif 20930 } else { 20931 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20932 20933 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20934 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20935 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20936 ASSERT(PDESC_HDRL(pkt_info) >= 20937 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20938 TCP_CHECKSUM_SIZE)); 20939 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20940 20941 if (tcp->tcp_ip_forward_progress) { 20942 rconfirm = B_TRUE; 20943 tcp->tcp_ip_forward_progress = B_FALSE; 20944 } 20945 } 20946 20947 /* at least one payload span, and at most two */ 20948 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20949 20950 /* add the packet descriptor to Multidata */ 20951 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20952 KM_NOSLEEP)) == NULL) { 20953 /* 20954 * Any failure other than ENOMEM indicates 20955 * that we have passed in invalid pkt_info 20956 * or parameters to mmd_addpdesc, which must 20957 * not happen. 20958 * 20959 * EINVAL is a result of failure on boundary 20960 * checks against the pkt_info contents. It 20961 * should not happen, and we panic because 20962 * either there's horrible heap corruption, 20963 * and/or programming mistake. 20964 */ 20965 if (err != ENOMEM) { 20966 cmn_err(CE_PANIC, "tcp_multisend: " 20967 "pdesc logic error detected for " 20968 "tcp %p mmd %p pinfo %p (%d)\n", 20969 (void *)tcp, (void *)mmd, 20970 (void *)pkt_info, err); 20971 } 20972 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20973 goto legacy_send; /* out_of_mem */ 20974 } 20975 ASSERT(pkt != NULL); 20976 20977 /* calculate IP header and TCP checksums */ 20978 if (af == AF_INET) { 20979 /* calculate pseudo-header checksum */ 20980 cksum = (dst >> 16) + (dst & 0xFFFF) + 20981 (src >> 16) + (src & 0xFFFF); 20982 20983 /* offset for TCP header checksum */ 20984 up = IPH_TCPH_CHECKSUMP(ipha, 20985 IP_SIMPLE_HDR_LENGTH); 20986 } else { 20987 up = (uint16_t *)&ip6h->ip6_src; 20988 20989 /* calculate pseudo-header checksum */ 20990 cksum = up[0] + up[1] + up[2] + up[3] + 20991 up[4] + up[5] + up[6] + up[7] + 20992 up[8] + up[9] + up[10] + up[11] + 20993 up[12] + up[13] + up[14] + up[15]; 20994 20995 /* Fold the initial sum */ 20996 cksum = (cksum & 0xffff) + (cksum >> 16); 20997 20998 up = (uint16_t *)(((uchar_t *)ip6h) + 20999 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 21000 } 21001 21002 if (hwcksum_flags & HCK_FULLCKSUM) { 21003 /* clear checksum field for hardware */ 21004 *up = 0; 21005 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21006 uint32_t sum; 21007 21008 /* pseudo-header checksumming */ 21009 sum = *up + cksum + IP_TCP_CSUM_COMP; 21010 sum = (sum & 0xFFFF) + (sum >> 16); 21011 *up = (sum & 0xFFFF) + (sum >> 16); 21012 } else { 21013 /* software checksumming */ 21014 TCP_STAT(tcps, tcp_out_sw_cksum); 21015 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21016 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21017 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21018 cksum + IP_TCP_CSUM_COMP); 21019 if (*up == 0) 21020 *up = 0xFFFF; 21021 } 21022 21023 /* IPv4 header checksum */ 21024 if (af == AF_INET) { 21025 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21026 ipha->ipha_hdr_checksum = 0; 21027 } else { 21028 IP_HDR_CKSUM(ipha, cksum, 21029 ((uint32_t *)ipha)[0], 21030 ((uint16_t *)ipha)[4]); 21031 } 21032 } 21033 21034 if (af == AF_INET && 21035 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21036 af == AF_INET6 && 21037 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21038 mblk_t *mp, *mp1; 21039 uchar_t *hdr_rptr, *hdr_wptr; 21040 uchar_t *pld_rptr, *pld_wptr; 21041 21042 /* 21043 * We reconstruct a pseudo packet for the hooks 21044 * framework using mmd_transform_link(). 21045 * If it is a split packet we pullup the 21046 * payload. FW_HOOKS expects a pkt comprising 21047 * of two mblks: a header and the payload. 21048 */ 21049 if ((mp = mmd_transform_link(pkt)) == NULL) { 21050 TCP_STAT(tcps, tcp_mdt_allocfail); 21051 goto legacy_send; 21052 } 21053 21054 if (pkt_info->pld_cnt > 1) { 21055 /* split payload, more than one pld */ 21056 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21057 NULL) { 21058 freemsg(mp); 21059 TCP_STAT(tcps, 21060 tcp_mdt_allocfail); 21061 goto legacy_send; 21062 } 21063 freemsg(mp->b_cont); 21064 mp->b_cont = mp1; 21065 } else { 21066 mp1 = mp->b_cont; 21067 } 21068 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21069 21070 /* 21071 * Remember the message offsets. This is so we 21072 * can detect changes when we return from the 21073 * FW_HOOKS callbacks. 21074 */ 21075 hdr_rptr = mp->b_rptr; 21076 hdr_wptr = mp->b_wptr; 21077 pld_rptr = mp->b_cont->b_rptr; 21078 pld_wptr = mp->b_cont->b_wptr; 21079 21080 if (af == AF_INET) { 21081 DTRACE_PROBE4( 21082 ip4__physical__out__start, 21083 ill_t *, NULL, 21084 ill_t *, ill, 21085 ipha_t *, ipha, 21086 mblk_t *, mp); 21087 FW_HOOKS( 21088 ipst->ips_ip4_physical_out_event, 21089 ipst->ips_ipv4firewall_physical_out, 21090 NULL, ill, ipha, mp, mp, 0, ipst); 21091 DTRACE_PROBE1( 21092 ip4__physical__out__end, 21093 mblk_t *, mp); 21094 } else { 21095 DTRACE_PROBE4( 21096 ip6__physical__out_start, 21097 ill_t *, NULL, 21098 ill_t *, ill, 21099 ip6_t *, ip6h, 21100 mblk_t *, mp); 21101 FW_HOOKS6( 21102 ipst->ips_ip6_physical_out_event, 21103 ipst->ips_ipv6firewall_physical_out, 21104 NULL, ill, ip6h, mp, mp, 0, ipst); 21105 DTRACE_PROBE1( 21106 ip6__physical__out__end, 21107 mblk_t *, mp); 21108 } 21109 21110 if (mp == NULL || 21111 (mp1 = mp->b_cont) == NULL || 21112 mp->b_rptr != hdr_rptr || 21113 mp->b_wptr != hdr_wptr || 21114 mp1->b_rptr != pld_rptr || 21115 mp1->b_wptr != pld_wptr || 21116 mp1->b_cont != NULL) { 21117 /* 21118 * We abandon multidata processing and 21119 * return to the normal path, either 21120 * when a packet is blocked, or when 21121 * the boundaries of header buffer or 21122 * payload buffer have been changed by 21123 * FW_HOOKS[6]. 21124 */ 21125 if (mp != NULL) 21126 freemsg(mp); 21127 goto legacy_send; 21128 } 21129 /* Finished with the pseudo packet */ 21130 freemsg(mp); 21131 } 21132 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21133 ill, ipha, ip6h); 21134 /* advance header offset */ 21135 cur_hdr_off += hdr_frag_sz; 21136 21137 obbytes += tcp->tcp_last_sent_len; 21138 ++obsegs; 21139 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21140 *tail_unsent > 0); 21141 21142 if ((*xmit_tail)->b_next == NULL) { 21143 /* 21144 * Store the lbolt used for RTT estimation. We can only 21145 * record one timestamp per mblk so we do it when we 21146 * reach the end of the payload buffer. Also we only 21147 * take a new timestamp sample when the previous timed 21148 * data from the same mblk has been ack'ed. 21149 */ 21150 (*xmit_tail)->b_prev = local_time; 21151 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21152 } 21153 21154 ASSERT(*tail_unsent >= 0); 21155 if (*tail_unsent > 0) { 21156 /* 21157 * We got here because we broke out of the above 21158 * loop due to of one of the following cases: 21159 * 21160 * 1. len < adjusted MSS (i.e. small), 21161 * 2. Sender SWS avoidance, 21162 * 3. max_pld is zero. 21163 * 21164 * We are done for this Multidata, so trim our 21165 * last payload buffer (if any) accordingly. 21166 */ 21167 if (md_pbuf != NULL) 21168 md_pbuf->b_wptr -= *tail_unsent; 21169 } else if (*usable > 0) { 21170 *xmit_tail = (*xmit_tail)->b_cont; 21171 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21172 (uintptr_t)INT_MAX); 21173 *tail_unsent = (int)MBLKL(*xmit_tail); 21174 add_buffer = B_TRUE; 21175 } 21176 } while (!done && *usable > 0 && num_burst_seg > 0 && 21177 (tcp_mdt_chain || max_pld > 0)); 21178 21179 if (md_mp_head != NULL) { 21180 /* send everything down */ 21181 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21182 &rconfirm); 21183 } 21184 21185 #undef PREP_NEW_MULTIDATA 21186 #undef PREP_NEW_PBUF 21187 #undef IPVER 21188 21189 IRE_REFRELE(ire); 21190 return (0); 21191 } 21192 21193 /* 21194 * A wrapper function for sending one or more Multidata messages down to 21195 * the module below ip; this routine does not release the reference of the 21196 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21197 */ 21198 static void 21199 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21200 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21201 { 21202 uint64_t delta; 21203 nce_t *nce; 21204 tcp_stack_t *tcps = tcp->tcp_tcps; 21205 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21206 21207 ASSERT(ire != NULL && ill != NULL); 21208 ASSERT(ire->ire_stq != NULL); 21209 ASSERT(md_mp_head != NULL); 21210 ASSERT(rconfirm != NULL); 21211 21212 /* adjust MIBs and IRE timestamp */ 21213 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21214 tcp->tcp_obsegs += obsegs; 21215 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21216 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21217 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21218 21219 if (tcp->tcp_ipversion == IPV4_VERSION) { 21220 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21221 } else { 21222 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21223 } 21224 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21225 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21226 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21227 21228 ire->ire_ob_pkt_count += obsegs; 21229 if (ire->ire_ipif != NULL) 21230 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21231 ire->ire_last_used_time = lbolt; 21232 21233 if (ipst->ips_ipobs_enabled) { 21234 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21235 pdesc_t *dl_pkt; 21236 pdescinfo_t pinfo; 21237 mblk_t *nmp; 21238 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21239 21240 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21241 (dl_pkt != NULL); 21242 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21243 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21244 continue; 21245 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21246 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21247 freemsg(nmp); 21248 } 21249 } 21250 21251 /* send it down */ 21252 putnext(ire->ire_stq, md_mp_head); 21253 21254 /* we're done for TCP/IPv4 */ 21255 if (tcp->tcp_ipversion == IPV4_VERSION) 21256 return; 21257 21258 nce = ire->ire_nce; 21259 21260 ASSERT(nce != NULL); 21261 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21262 ASSERT(nce->nce_state != ND_INCOMPLETE); 21263 21264 /* reachability confirmation? */ 21265 if (*rconfirm) { 21266 nce->nce_last = TICK_TO_MSEC(lbolt64); 21267 if (nce->nce_state != ND_REACHABLE) { 21268 mutex_enter(&nce->nce_lock); 21269 nce->nce_state = ND_REACHABLE; 21270 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21271 mutex_exit(&nce->nce_lock); 21272 (void) untimeout(nce->nce_timeout_id); 21273 if (ip_debug > 2) { 21274 /* ip1dbg */ 21275 pr_addr_dbg("tcp_multisend_data: state " 21276 "for %s changed to REACHABLE\n", 21277 AF_INET6, &ire->ire_addr_v6); 21278 } 21279 } 21280 /* reset transport reachability confirmation */ 21281 *rconfirm = B_FALSE; 21282 } 21283 21284 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21285 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21286 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21287 21288 if (delta > (uint64_t)ill->ill_reachable_time) { 21289 mutex_enter(&nce->nce_lock); 21290 switch (nce->nce_state) { 21291 case ND_REACHABLE: 21292 case ND_STALE: 21293 /* 21294 * ND_REACHABLE is identical to ND_STALE in this 21295 * specific case. If reachable time has expired for 21296 * this neighbor (delta is greater than reachable 21297 * time), conceptually, the neighbor cache is no 21298 * longer in REACHABLE state, but already in STALE 21299 * state. So the correct transition here is to 21300 * ND_DELAY. 21301 */ 21302 nce->nce_state = ND_DELAY; 21303 mutex_exit(&nce->nce_lock); 21304 NDP_RESTART_TIMER(nce, 21305 ipst->ips_delay_first_probe_time); 21306 if (ip_debug > 3) { 21307 /* ip2dbg */ 21308 pr_addr_dbg("tcp_multisend_data: state " 21309 "for %s changed to DELAY\n", 21310 AF_INET6, &ire->ire_addr_v6); 21311 } 21312 break; 21313 case ND_DELAY: 21314 case ND_PROBE: 21315 mutex_exit(&nce->nce_lock); 21316 /* Timers have already started */ 21317 break; 21318 case ND_UNREACHABLE: 21319 /* 21320 * ndp timer has detected that this nce is 21321 * unreachable and initiated deleting this nce 21322 * and all its associated IREs. This is a race 21323 * where we found the ire before it was deleted 21324 * and have just sent out a packet using this 21325 * unreachable nce. 21326 */ 21327 mutex_exit(&nce->nce_lock); 21328 break; 21329 default: 21330 ASSERT(0); 21331 } 21332 } 21333 } 21334 21335 /* 21336 * Derived from tcp_send_data(). 21337 */ 21338 static void 21339 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21340 int num_lso_seg) 21341 { 21342 ipha_t *ipha; 21343 mblk_t *ire_fp_mp; 21344 uint_t ire_fp_mp_len; 21345 uint32_t hcksum_txflags = 0; 21346 ipaddr_t src; 21347 ipaddr_t dst; 21348 uint32_t cksum; 21349 uint16_t *up; 21350 tcp_stack_t *tcps = tcp->tcp_tcps; 21351 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21352 21353 ASSERT(DB_TYPE(mp) == M_DATA); 21354 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21355 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21356 ASSERT(tcp->tcp_connp != NULL); 21357 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21358 21359 ipha = (ipha_t *)mp->b_rptr; 21360 src = ipha->ipha_src; 21361 dst = ipha->ipha_dst; 21362 21363 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21364 21365 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21366 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21367 num_lso_seg); 21368 #ifndef _BIG_ENDIAN 21369 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21370 #endif 21371 if (tcp->tcp_snd_zcopy_aware) { 21372 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21373 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21374 mp = tcp_zcopy_disable(tcp, mp); 21375 } 21376 21377 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21378 ASSERT(ill->ill_hcksum_capab != NULL); 21379 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21380 } 21381 21382 /* 21383 * Since the TCP checksum should be recalculated by h/w, we can just 21384 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21385 * pseudo-header checksum for HCK_PARTIALCKSUM. 21386 * The partial pseudo-header excludes TCP length, that was calculated 21387 * in tcp_send(), so to zero *up before further processing. 21388 */ 21389 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21390 21391 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21392 *up = 0; 21393 21394 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21395 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21396 21397 /* 21398 * Append LSO flags and mss to the mp. 21399 */ 21400 lso_info_set(mp, mss, HW_LSO); 21401 21402 ipha->ipha_fragment_offset_and_flags |= 21403 (uint32_t)htons(ire->ire_frag_flag); 21404 21405 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21406 ire_fp_mp_len = MBLKL(ire_fp_mp); 21407 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21408 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21409 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21410 21411 UPDATE_OB_PKT_COUNT(ire); 21412 ire->ire_last_used_time = lbolt; 21413 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21414 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21415 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21416 ntohs(ipha->ipha_length)); 21417 21418 DTRACE_PROBE4(ip4__physical__out__start, 21419 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 21420 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21421 ipst->ips_ipv4firewall_physical_out, NULL, 21422 ill, ipha, mp, mp, 0, ipst); 21423 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21424 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 21425 21426 if (mp != NULL) { 21427 if (ipst->ips_ipobs_enabled) { 21428 zoneid_t szone; 21429 21430 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 21431 ipst, ALL_ZONES); 21432 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21433 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 21434 } 21435 21436 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0); 21437 } 21438 } 21439 21440 /* 21441 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21442 * scheme, and returns one of the following: 21443 * 21444 * -1 = failed allocation. 21445 * 0 = success; burst count reached, or usable send window is too small, 21446 * and that we'd rather wait until later before sending again. 21447 * 1 = success; we are called from tcp_multisend(), and both usable send 21448 * window and tail_unsent are greater than the MDT threshold, and thus 21449 * Multidata Transmit should be used instead. 21450 */ 21451 static int 21452 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21453 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21454 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21455 const int mdt_thres) 21456 { 21457 int num_burst_seg = tcp->tcp_snd_burst; 21458 ire_t *ire = NULL; 21459 ill_t *ill = NULL; 21460 mblk_t *ire_fp_mp = NULL; 21461 uint_t ire_fp_mp_len = 0; 21462 int num_lso_seg = 1; 21463 uint_t lso_usable; 21464 boolean_t do_lso_send = B_FALSE; 21465 tcp_stack_t *tcps = tcp->tcp_tcps; 21466 21467 /* 21468 * Check LSO capability before any further work. And the similar check 21469 * need to be done in for(;;) loop. 21470 * LSO will be deployed when therer is more than one mss of available 21471 * data and a burst transmission is allowed. 21472 */ 21473 if (tcp->tcp_lso && 21474 (tcp->tcp_valid_bits == 0 || 21475 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21476 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21477 /* 21478 * Try to find usable IRE/ILL and do basic check to the ILL. 21479 */ 21480 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21481 /* 21482 * Enable LSO with this transmission. 21483 * Since IRE has been hold in 21484 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21485 * should be called before return. 21486 */ 21487 do_lso_send = B_TRUE; 21488 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21489 ire_fp_mp_len = MBLKL(ire_fp_mp); 21490 /* Round up to multiple of 4 */ 21491 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21492 } else { 21493 do_lso_send = B_FALSE; 21494 ill = NULL; 21495 } 21496 } 21497 21498 for (;;) { 21499 struct datab *db; 21500 tcph_t *tcph; 21501 uint32_t sum; 21502 mblk_t *mp, *mp1; 21503 uchar_t *rptr; 21504 int len; 21505 21506 /* 21507 * If we're called by tcp_multisend(), and the amount of 21508 * sendable data as well as the size of current xmit_tail 21509 * is beyond the MDT threshold, return to the caller and 21510 * let the large data transmit be done using MDT. 21511 */ 21512 if (*usable > 0 && *usable > mdt_thres && 21513 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21514 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21515 ASSERT(tcp->tcp_mdt); 21516 return (1); /* success; do large send */ 21517 } 21518 21519 if (num_burst_seg == 0) 21520 break; /* success; burst count reached */ 21521 21522 /* 21523 * Calculate the maximum payload length we can send in *one* 21524 * time. 21525 */ 21526 if (do_lso_send) { 21527 /* 21528 * Check whether need to do LSO any more. 21529 */ 21530 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21531 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21532 lso_usable = MIN(lso_usable, 21533 num_burst_seg * mss); 21534 21535 num_lso_seg = lso_usable / mss; 21536 if (lso_usable % mss) { 21537 num_lso_seg++; 21538 tcp->tcp_last_sent_len = (ushort_t) 21539 (lso_usable % mss); 21540 } else { 21541 tcp->tcp_last_sent_len = (ushort_t)mss; 21542 } 21543 } else { 21544 do_lso_send = B_FALSE; 21545 num_lso_seg = 1; 21546 lso_usable = mss; 21547 } 21548 } 21549 21550 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21551 21552 /* 21553 * Adjust num_burst_seg here. 21554 */ 21555 num_burst_seg -= num_lso_seg; 21556 21557 len = mss; 21558 if (len > *usable) { 21559 ASSERT(do_lso_send == B_FALSE); 21560 21561 len = *usable; 21562 if (len <= 0) { 21563 /* Terminate the loop */ 21564 break; /* success; too small */ 21565 } 21566 /* 21567 * Sender silly-window avoidance. 21568 * Ignore this if we are going to send a 21569 * zero window probe out. 21570 * 21571 * TODO: force data into microscopic window? 21572 * ==> (!pushed || (unsent > usable)) 21573 */ 21574 if (len < (tcp->tcp_max_swnd >> 1) && 21575 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21576 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21577 len == 1) && (! tcp->tcp_zero_win_probe)) { 21578 /* 21579 * If the retransmit timer is not running 21580 * we start it so that we will retransmit 21581 * in the case when the the receiver has 21582 * decremented the window. 21583 */ 21584 if (*snxt == tcp->tcp_snxt && 21585 *snxt == tcp->tcp_suna) { 21586 /* 21587 * We are not supposed to send 21588 * anything. So let's wait a little 21589 * bit longer before breaking SWS 21590 * avoidance. 21591 * 21592 * What should the value be? 21593 * Suggestion: MAX(init rexmit time, 21594 * tcp->tcp_rto) 21595 */ 21596 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21597 } 21598 break; /* success; too small */ 21599 } 21600 } 21601 21602 tcph = tcp->tcp_tcph; 21603 21604 /* 21605 * The reason to adjust len here is that we need to set flags 21606 * and calculate checksum. 21607 */ 21608 if (do_lso_send) 21609 len = lso_usable; 21610 21611 *usable -= len; /* Approximate - can be adjusted later */ 21612 if (*usable > 0) 21613 tcph->th_flags[0] = TH_ACK; 21614 else 21615 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21616 21617 /* 21618 * Prime pump for IP's checksumming on our behalf 21619 * Include the adjustment for a source route if any. 21620 */ 21621 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21622 sum = (sum >> 16) + (sum & 0xFFFF); 21623 U16_TO_ABE16(sum, tcph->th_sum); 21624 21625 U32_TO_ABE32(*snxt, tcph->th_seq); 21626 21627 /* 21628 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21629 * set. For the case when TCP_FSS_VALID is the only valid 21630 * bit (normal active close), branch off only when we think 21631 * that the FIN flag needs to be set. Note for this case, 21632 * that (snxt + len) may not reflect the actual seg_len, 21633 * as len may be further reduced in tcp_xmit_mp(). If len 21634 * gets modified, we will end up here again. 21635 */ 21636 if (tcp->tcp_valid_bits != 0 && 21637 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21638 ((*snxt + len) == tcp->tcp_fss))) { 21639 uchar_t *prev_rptr; 21640 uint32_t prev_snxt = tcp->tcp_snxt; 21641 21642 if (*tail_unsent == 0) { 21643 ASSERT((*xmit_tail)->b_cont != NULL); 21644 *xmit_tail = (*xmit_tail)->b_cont; 21645 prev_rptr = (*xmit_tail)->b_rptr; 21646 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21647 (*xmit_tail)->b_rptr); 21648 } else { 21649 prev_rptr = (*xmit_tail)->b_rptr; 21650 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21651 *tail_unsent; 21652 } 21653 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21654 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21655 /* Restore tcp_snxt so we get amount sent right. */ 21656 tcp->tcp_snxt = prev_snxt; 21657 if (prev_rptr == (*xmit_tail)->b_rptr) { 21658 /* 21659 * If the previous timestamp is still in use, 21660 * don't stomp on it. 21661 */ 21662 if ((*xmit_tail)->b_next == NULL) { 21663 (*xmit_tail)->b_prev = local_time; 21664 (*xmit_tail)->b_next = 21665 (mblk_t *)(uintptr_t)(*snxt); 21666 } 21667 } else 21668 (*xmit_tail)->b_rptr = prev_rptr; 21669 21670 if (mp == NULL) { 21671 if (ire != NULL) 21672 IRE_REFRELE(ire); 21673 return (-1); 21674 } 21675 mp1 = mp->b_cont; 21676 21677 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21678 tcp->tcp_last_sent_len = (ushort_t)len; 21679 while (mp1->b_cont) { 21680 *xmit_tail = (*xmit_tail)->b_cont; 21681 (*xmit_tail)->b_prev = local_time; 21682 (*xmit_tail)->b_next = 21683 (mblk_t *)(uintptr_t)(*snxt); 21684 mp1 = mp1->b_cont; 21685 } 21686 *snxt += len; 21687 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21688 BUMP_LOCAL(tcp->tcp_obsegs); 21689 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21690 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21691 tcp_send_data(tcp, q, mp); 21692 continue; 21693 } 21694 21695 *snxt += len; /* Adjust later if we don't send all of len */ 21696 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21697 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21698 21699 if (*tail_unsent) { 21700 /* Are the bytes above us in flight? */ 21701 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21702 if (rptr != (*xmit_tail)->b_rptr) { 21703 *tail_unsent -= len; 21704 if (len <= mss) /* LSO is unusable */ 21705 tcp->tcp_last_sent_len = (ushort_t)len; 21706 len += tcp_hdr_len; 21707 if (tcp->tcp_ipversion == IPV4_VERSION) 21708 tcp->tcp_ipha->ipha_length = htons(len); 21709 else 21710 tcp->tcp_ip6h->ip6_plen = 21711 htons(len - 21712 ((char *)&tcp->tcp_ip6h[1] - 21713 tcp->tcp_iphc)); 21714 mp = dupb(*xmit_tail); 21715 if (mp == NULL) { 21716 if (ire != NULL) 21717 IRE_REFRELE(ire); 21718 return (-1); /* out_of_mem */ 21719 } 21720 mp->b_rptr = rptr; 21721 /* 21722 * If the old timestamp is no longer in use, 21723 * sample a new timestamp now. 21724 */ 21725 if ((*xmit_tail)->b_next == NULL) { 21726 (*xmit_tail)->b_prev = local_time; 21727 (*xmit_tail)->b_next = 21728 (mblk_t *)(uintptr_t)(*snxt-len); 21729 } 21730 goto must_alloc; 21731 } 21732 } else { 21733 *xmit_tail = (*xmit_tail)->b_cont; 21734 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21735 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21736 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21737 (*xmit_tail)->b_rptr); 21738 } 21739 21740 (*xmit_tail)->b_prev = local_time; 21741 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21742 21743 *tail_unsent -= len; 21744 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21745 tcp->tcp_last_sent_len = (ushort_t)len; 21746 21747 len += tcp_hdr_len; 21748 if (tcp->tcp_ipversion == IPV4_VERSION) 21749 tcp->tcp_ipha->ipha_length = htons(len); 21750 else 21751 tcp->tcp_ip6h->ip6_plen = htons(len - 21752 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21753 21754 mp = dupb(*xmit_tail); 21755 if (mp == NULL) { 21756 if (ire != NULL) 21757 IRE_REFRELE(ire); 21758 return (-1); /* out_of_mem */ 21759 } 21760 21761 len = tcp_hdr_len; 21762 /* 21763 * There are four reasons to allocate a new hdr mblk: 21764 * 1) The bytes above us are in use by another packet 21765 * 2) We don't have good alignment 21766 * 3) The mblk is being shared 21767 * 4) We don't have enough room for a header 21768 */ 21769 rptr = mp->b_rptr - len; 21770 if (!OK_32PTR(rptr) || 21771 ((db = mp->b_datap), db->db_ref != 2) || 21772 rptr < db->db_base + ire_fp_mp_len) { 21773 /* NOTE: we assume allocb returns an OK_32PTR */ 21774 21775 must_alloc:; 21776 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21777 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21778 if (mp1 == NULL) { 21779 freemsg(mp); 21780 if (ire != NULL) 21781 IRE_REFRELE(ire); 21782 return (-1); /* out_of_mem */ 21783 } 21784 mp1->b_cont = mp; 21785 mp = mp1; 21786 /* Leave room for Link Level header */ 21787 len = tcp_hdr_len; 21788 rptr = 21789 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21790 mp->b_wptr = &rptr[len]; 21791 } 21792 21793 /* 21794 * Fill in the header using the template header, and add 21795 * options such as time-stamp, ECN and/or SACK, as needed. 21796 */ 21797 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21798 21799 mp->b_rptr = rptr; 21800 21801 if (*tail_unsent) { 21802 int spill = *tail_unsent; 21803 21804 mp1 = mp->b_cont; 21805 if (mp1 == NULL) 21806 mp1 = mp; 21807 21808 /* 21809 * If we're a little short, tack on more mblks until 21810 * there is no more spillover. 21811 */ 21812 while (spill < 0) { 21813 mblk_t *nmp; 21814 int nmpsz; 21815 21816 nmp = (*xmit_tail)->b_cont; 21817 nmpsz = MBLKL(nmp); 21818 21819 /* 21820 * Excess data in mblk; can we split it? 21821 * If MDT is enabled for the connection, 21822 * keep on splitting as this is a transient 21823 * send path. 21824 */ 21825 if (!do_lso_send && !tcp->tcp_mdt && 21826 (spill + nmpsz > 0)) { 21827 /* 21828 * Don't split if stream head was 21829 * told to break up larger writes 21830 * into smaller ones. 21831 */ 21832 if (tcp->tcp_maxpsz > 0) 21833 break; 21834 21835 /* 21836 * Next mblk is less than SMSS/2 21837 * rounded up to nearest 64-byte; 21838 * let it get sent as part of the 21839 * next segment. 21840 */ 21841 if (tcp->tcp_localnet && 21842 !tcp->tcp_cork && 21843 (nmpsz < roundup((mss >> 1), 64))) 21844 break; 21845 } 21846 21847 *xmit_tail = nmp; 21848 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21849 /* Stash for rtt use later */ 21850 (*xmit_tail)->b_prev = local_time; 21851 (*xmit_tail)->b_next = 21852 (mblk_t *)(uintptr_t)(*snxt - len); 21853 mp1->b_cont = dupb(*xmit_tail); 21854 mp1 = mp1->b_cont; 21855 21856 spill += nmpsz; 21857 if (mp1 == NULL) { 21858 *tail_unsent = spill; 21859 freemsg(mp); 21860 if (ire != NULL) 21861 IRE_REFRELE(ire); 21862 return (-1); /* out_of_mem */ 21863 } 21864 } 21865 21866 /* Trim back any surplus on the last mblk */ 21867 if (spill >= 0) { 21868 mp1->b_wptr -= spill; 21869 *tail_unsent = spill; 21870 } else { 21871 /* 21872 * We did not send everything we could in 21873 * order to remain within the b_cont limit. 21874 */ 21875 *usable -= spill; 21876 *snxt += spill; 21877 tcp->tcp_last_sent_len += spill; 21878 UPDATE_MIB(&tcps->tcps_mib, 21879 tcpOutDataBytes, spill); 21880 /* 21881 * Adjust the checksum 21882 */ 21883 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21884 sum += spill; 21885 sum = (sum >> 16) + (sum & 0xFFFF); 21886 U16_TO_ABE16(sum, tcph->th_sum); 21887 if (tcp->tcp_ipversion == IPV4_VERSION) { 21888 sum = ntohs( 21889 ((ipha_t *)rptr)->ipha_length) + 21890 spill; 21891 ((ipha_t *)rptr)->ipha_length = 21892 htons(sum); 21893 } else { 21894 sum = ntohs( 21895 ((ip6_t *)rptr)->ip6_plen) + 21896 spill; 21897 ((ip6_t *)rptr)->ip6_plen = 21898 htons(sum); 21899 } 21900 *tail_unsent = 0; 21901 } 21902 } 21903 if (tcp->tcp_ip_forward_progress) { 21904 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21905 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21906 tcp->tcp_ip_forward_progress = B_FALSE; 21907 } 21908 21909 if (do_lso_send) { 21910 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21911 num_lso_seg); 21912 tcp->tcp_obsegs += num_lso_seg; 21913 21914 TCP_STAT(tcps, tcp_lso_times); 21915 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21916 } else { 21917 tcp_send_data(tcp, q, mp); 21918 BUMP_LOCAL(tcp->tcp_obsegs); 21919 } 21920 } 21921 21922 if (ire != NULL) 21923 IRE_REFRELE(ire); 21924 return (0); 21925 } 21926 21927 /* Unlink and return any mblk that looks like it contains a MDT info */ 21928 static mblk_t * 21929 tcp_mdt_info_mp(mblk_t *mp) 21930 { 21931 mblk_t *prev_mp; 21932 21933 for (;;) { 21934 prev_mp = mp; 21935 /* no more to process? */ 21936 if ((mp = mp->b_cont) == NULL) 21937 break; 21938 21939 switch (DB_TYPE(mp)) { 21940 case M_CTL: 21941 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21942 continue; 21943 ASSERT(prev_mp != NULL); 21944 prev_mp->b_cont = mp->b_cont; 21945 mp->b_cont = NULL; 21946 return (mp); 21947 default: 21948 break; 21949 } 21950 } 21951 return (mp); 21952 } 21953 21954 /* MDT info update routine, called when IP notifies us about MDT */ 21955 static void 21956 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21957 { 21958 boolean_t prev_state; 21959 tcp_stack_t *tcps = tcp->tcp_tcps; 21960 21961 /* 21962 * IP is telling us to abort MDT on this connection? We know 21963 * this because the capability is only turned off when IP 21964 * encounters some pathological cases, e.g. link-layer change 21965 * where the new driver doesn't support MDT, or in situation 21966 * where MDT usage on the link-layer has been switched off. 21967 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21968 * if the link-layer doesn't support MDT, and if it does, it 21969 * will indicate that the feature is to be turned on. 21970 */ 21971 prev_state = tcp->tcp_mdt; 21972 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21973 if (!tcp->tcp_mdt && !first) { 21974 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21975 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21976 (void *)tcp->tcp_connp)); 21977 } 21978 21979 /* 21980 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21981 * so disable MDT otherwise. The checks are done here 21982 * and in tcp_wput_data(). 21983 */ 21984 if (tcp->tcp_mdt && 21985 (tcp->tcp_ipversion == IPV4_VERSION && 21986 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21987 (tcp->tcp_ipversion == IPV6_VERSION && 21988 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21989 tcp->tcp_mdt = B_FALSE; 21990 21991 if (tcp->tcp_mdt) { 21992 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21993 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21994 "version (%d), expected version is %d", 21995 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21996 tcp->tcp_mdt = B_FALSE; 21997 return; 21998 } 21999 22000 /* 22001 * We need the driver to be able to handle at least three 22002 * spans per packet in order for tcp MDT to be utilized. 22003 * The first is for the header portion, while the rest are 22004 * needed to handle a packet that straddles across two 22005 * virtually non-contiguous buffers; a typical tcp packet 22006 * therefore consists of only two spans. Note that we take 22007 * a zero as "don't care". 22008 */ 22009 if (mdt_capab->ill_mdt_span_limit > 0 && 22010 mdt_capab->ill_mdt_span_limit < 3) { 22011 tcp->tcp_mdt = B_FALSE; 22012 return; 22013 } 22014 22015 /* a zero means driver wants default value */ 22016 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22017 tcps->tcps_mdt_max_pbufs); 22018 if (tcp->tcp_mdt_max_pld == 0) 22019 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22020 22021 /* ensure 32-bit alignment */ 22022 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22023 mdt_capab->ill_mdt_hdr_head), 4); 22024 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22025 mdt_capab->ill_mdt_hdr_tail), 4); 22026 22027 if (!first && !prev_state) { 22028 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22029 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22030 (void *)tcp->tcp_connp)); 22031 } 22032 } 22033 } 22034 22035 /* Unlink and return any mblk that looks like it contains a LSO info */ 22036 static mblk_t * 22037 tcp_lso_info_mp(mblk_t *mp) 22038 { 22039 mblk_t *prev_mp; 22040 22041 for (;;) { 22042 prev_mp = mp; 22043 /* no more to process? */ 22044 if ((mp = mp->b_cont) == NULL) 22045 break; 22046 22047 switch (DB_TYPE(mp)) { 22048 case M_CTL: 22049 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22050 continue; 22051 ASSERT(prev_mp != NULL); 22052 prev_mp->b_cont = mp->b_cont; 22053 mp->b_cont = NULL; 22054 return (mp); 22055 default: 22056 break; 22057 } 22058 } 22059 22060 return (mp); 22061 } 22062 22063 /* LSO info update routine, called when IP notifies us about LSO */ 22064 static void 22065 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22066 { 22067 tcp_stack_t *tcps = tcp->tcp_tcps; 22068 22069 /* 22070 * IP is telling us to abort LSO on this connection? We know 22071 * this because the capability is only turned off when IP 22072 * encounters some pathological cases, e.g. link-layer change 22073 * where the new NIC/driver doesn't support LSO, or in situation 22074 * where LSO usage on the link-layer has been switched off. 22075 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22076 * if the link-layer doesn't support LSO, and if it does, it 22077 * will indicate that the feature is to be turned on. 22078 */ 22079 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22080 TCP_STAT(tcps, tcp_lso_enabled); 22081 22082 /* 22083 * We currently only support LSO on simple TCP/IPv4, 22084 * so disable LSO otherwise. The checks are done here 22085 * and in tcp_wput_data(). 22086 */ 22087 if (tcp->tcp_lso && 22088 (tcp->tcp_ipversion == IPV4_VERSION && 22089 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22090 (tcp->tcp_ipversion == IPV6_VERSION)) { 22091 tcp->tcp_lso = B_FALSE; 22092 TCP_STAT(tcps, tcp_lso_disabled); 22093 } else { 22094 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22095 lso_capab->ill_lso_max); 22096 } 22097 } 22098 22099 static void 22100 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22101 { 22102 conn_t *connp = tcp->tcp_connp; 22103 tcp_stack_t *tcps = tcp->tcp_tcps; 22104 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22105 22106 ASSERT(ire != NULL); 22107 22108 /* 22109 * We may be in the fastpath here, and although we essentially do 22110 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22111 * we try to keep things as brief as possible. After all, these 22112 * are only best-effort checks, and we do more thorough ones prior 22113 * to calling tcp_send()/tcp_multisend(). 22114 */ 22115 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22116 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22117 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22118 !(ire->ire_flags & RTF_MULTIRT) && 22119 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22120 CONN_IS_LSO_MD_FASTPATH(connp)) { 22121 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22122 /* Cache the result */ 22123 connp->conn_lso_ok = B_TRUE; 22124 22125 ASSERT(ill->ill_lso_capab != NULL); 22126 if (!ill->ill_lso_capab->ill_lso_on) { 22127 ill->ill_lso_capab->ill_lso_on = 1; 22128 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22129 "LSO for interface %s\n", (void *)connp, 22130 ill->ill_name)); 22131 } 22132 tcp_lso_update(tcp, ill->ill_lso_capab); 22133 } else if (ipst->ips_ip_multidata_outbound && 22134 ILL_MDT_CAPABLE(ill)) { 22135 /* Cache the result */ 22136 connp->conn_mdt_ok = B_TRUE; 22137 22138 ASSERT(ill->ill_mdt_capab != NULL); 22139 if (!ill->ill_mdt_capab->ill_mdt_on) { 22140 ill->ill_mdt_capab->ill_mdt_on = 1; 22141 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22142 "MDT for interface %s\n", (void *)connp, 22143 ill->ill_name)); 22144 } 22145 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22146 } 22147 } 22148 22149 /* 22150 * The goal is to reduce the number of generated tcp segments by 22151 * setting the maxpsz multiplier to 0; this will have an affect on 22152 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22153 * into each packet, up to SMSS bytes. Doing this reduces the number 22154 * of outbound segments and incoming ACKs, thus allowing for better 22155 * network and system performance. In contrast the legacy behavior 22156 * may result in sending less than SMSS size, because the last mblk 22157 * for some packets may have more data than needed to make up SMSS, 22158 * and the legacy code refused to "split" it. 22159 * 22160 * We apply the new behavior on following situations: 22161 * 22162 * 1) Loopback connections, 22163 * 2) Connections in which the remote peer is not on local subnet, 22164 * 3) Local subnet connections over the bge interface (see below). 22165 * 22166 * Ideally, we would like this behavior to apply for interfaces other 22167 * than bge. However, doing so would negatively impact drivers which 22168 * perform dynamic mapping and unmapping of DMA resources, which are 22169 * increased by setting the maxpsz multiplier to 0 (more mblks per 22170 * packet will be generated by tcp). The bge driver does not suffer 22171 * from this, as it copies the mblks into pre-mapped buffers, and 22172 * therefore does not require more I/O resources than before. 22173 * 22174 * Otherwise, this behavior is present on all network interfaces when 22175 * the destination endpoint is non-local, since reducing the number 22176 * of packets in general is good for the network. 22177 * 22178 * TODO We need to remove this hard-coded conditional for bge once 22179 * a better "self-tuning" mechanism, or a way to comprehend 22180 * the driver transmit strategy is devised. Until the solution 22181 * is found and well understood, we live with this hack. 22182 */ 22183 if (!tcp_static_maxpsz && 22184 (tcp->tcp_loopback || !tcp->tcp_localnet || 22185 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22186 /* override the default value */ 22187 tcp->tcp_maxpsz = 0; 22188 22189 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22190 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22191 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22192 } 22193 22194 /* set the stream head parameters accordingly */ 22195 (void) tcp_maxpsz_set(tcp, B_TRUE); 22196 } 22197 22198 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22199 static void 22200 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22201 { 22202 uchar_t fval = *mp->b_rptr; 22203 mblk_t *tail; 22204 queue_t *q = tcp->tcp_wq; 22205 22206 /* TODO: How should flush interact with urgent data? */ 22207 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22208 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22209 /* 22210 * Flush only data that has not yet been put on the wire. If 22211 * we flush data that we have already transmitted, life, as we 22212 * know it, may come to an end. 22213 */ 22214 tail = tcp->tcp_xmit_tail; 22215 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22216 tcp->tcp_xmit_tail_unsent = 0; 22217 tcp->tcp_unsent = 0; 22218 if (tail->b_wptr != tail->b_rptr) 22219 tail = tail->b_cont; 22220 if (tail) { 22221 mblk_t **excess = &tcp->tcp_xmit_head; 22222 for (;;) { 22223 mblk_t *mp1 = *excess; 22224 if (mp1 == tail) 22225 break; 22226 tcp->tcp_xmit_tail = mp1; 22227 tcp->tcp_xmit_last = mp1; 22228 excess = &mp1->b_cont; 22229 } 22230 *excess = NULL; 22231 tcp_close_mpp(&tail); 22232 if (tcp->tcp_snd_zcopy_aware) 22233 tcp_zcopy_notify(tcp); 22234 } 22235 /* 22236 * We have no unsent data, so unsent must be less than 22237 * tcp_xmit_lowater, so re-enable flow. 22238 */ 22239 mutex_enter(&tcp->tcp_non_sq_lock); 22240 if (tcp->tcp_flow_stopped) { 22241 tcp_clrqfull(tcp); 22242 } 22243 mutex_exit(&tcp->tcp_non_sq_lock); 22244 } 22245 /* 22246 * TODO: you can't just flush these, you have to increase rwnd for one 22247 * thing. For another, how should urgent data interact? 22248 */ 22249 if (fval & FLUSHR) { 22250 *mp->b_rptr = fval & ~FLUSHW; 22251 /* XXX */ 22252 qreply(q, mp); 22253 return; 22254 } 22255 freemsg(mp); 22256 } 22257 22258 /* 22259 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22260 * messages. 22261 */ 22262 static void 22263 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22264 { 22265 mblk_t *mp1; 22266 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22267 STRUCT_HANDLE(strbuf, sb); 22268 queue_t *q = tcp->tcp_wq; 22269 int error; 22270 uint_t addrlen; 22271 22272 /* Make sure it is one of ours. */ 22273 switch (iocp->ioc_cmd) { 22274 case TI_GETMYNAME: 22275 case TI_GETPEERNAME: 22276 break; 22277 default: 22278 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22279 return; 22280 } 22281 switch (mi_copy_state(q, mp, &mp1)) { 22282 case -1: 22283 return; 22284 case MI_COPY_CASE(MI_COPY_IN, 1): 22285 break; 22286 case MI_COPY_CASE(MI_COPY_OUT, 1): 22287 /* Copy out the strbuf. */ 22288 mi_copyout(q, mp); 22289 return; 22290 case MI_COPY_CASE(MI_COPY_OUT, 2): 22291 /* All done. */ 22292 mi_copy_done(q, mp, 0); 22293 return; 22294 default: 22295 mi_copy_done(q, mp, EPROTO); 22296 return; 22297 } 22298 /* Check alignment of the strbuf */ 22299 if (!OK_32PTR(mp1->b_rptr)) { 22300 mi_copy_done(q, mp, EINVAL); 22301 return; 22302 } 22303 22304 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22305 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22306 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22307 mi_copy_done(q, mp, EINVAL); 22308 return; 22309 } 22310 22311 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22312 if (mp1 == NULL) 22313 return; 22314 22315 switch (iocp->ioc_cmd) { 22316 case TI_GETMYNAME: 22317 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22318 break; 22319 case TI_GETPEERNAME: 22320 error = i_tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22321 break; 22322 } 22323 22324 if (error != 0) { 22325 mi_copy_done(q, mp, error); 22326 } else { 22327 mp1->b_wptr += addrlen; 22328 STRUCT_FSET(sb, len, addrlen); 22329 22330 /* Copy out the address */ 22331 mi_copyout(q, mp); 22332 } 22333 } 22334 22335 static void 22336 tcp_disable_direct_sockfs(tcp_t *tcp) 22337 { 22338 #ifdef _ILP32 22339 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 22340 #else 22341 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22342 #endif 22343 /* 22344 * Insert this socket into the acceptor hash. 22345 * We might need it for T_CONN_RES message 22346 */ 22347 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22348 22349 if (tcp->tcp_fused) { 22350 /* 22351 * This is a fused loopback tcp; disable 22352 * read-side synchronous streams interface 22353 * and drain any queued data. It is okay 22354 * to do this for non-synchronous streams 22355 * fused tcp as well. 22356 */ 22357 tcp_fuse_disable_pair(tcp, B_FALSE); 22358 } 22359 tcp->tcp_issocket = B_FALSE; 22360 tcp->tcp_sodirect = NULL; 22361 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 22362 } 22363 22364 /* 22365 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22366 * messages. 22367 */ 22368 /* ARGSUSED */ 22369 static void 22370 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22371 { 22372 conn_t *connp = (conn_t *)arg; 22373 tcp_t *tcp = connp->conn_tcp; 22374 queue_t *q = tcp->tcp_wq; 22375 struct iocblk *iocp; 22376 22377 ASSERT(DB_TYPE(mp) == M_IOCTL); 22378 /* 22379 * Try and ASSERT the minimum possible references on the 22380 * conn early enough. Since we are executing on write side, 22381 * the connection is obviously not detached and that means 22382 * there is a ref each for TCP and IP. Since we are behind 22383 * the squeue, the minimum references needed are 3. If the 22384 * conn is in classifier hash list, there should be an 22385 * extra ref for that (we check both the possibilities). 22386 */ 22387 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22388 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22389 22390 iocp = (struct iocblk *)mp->b_rptr; 22391 switch (iocp->ioc_cmd) { 22392 case TCP_IOC_DEFAULT_Q: 22393 /* Wants to be the default wq. */ 22394 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22395 iocp->ioc_error = EPERM; 22396 iocp->ioc_count = 0; 22397 mp->b_datap->db_type = M_IOCACK; 22398 qreply(q, mp); 22399 return; 22400 } 22401 tcp_def_q_set(tcp, mp); 22402 return; 22403 case _SIOCSOCKFALLBACK: 22404 /* 22405 * Either sockmod is about to be popped and the socket 22406 * would now be treated as a plain stream, or a module 22407 * is about to be pushed so we could no longer use read- 22408 * side synchronous streams for fused loopback tcp. 22409 * Drain any queued data and disable direct sockfs 22410 * interface from now on. 22411 */ 22412 if (!tcp->tcp_issocket) { 22413 DB_TYPE(mp) = M_IOCNAK; 22414 iocp->ioc_error = EINVAL; 22415 } else { 22416 tcp_disable_direct_sockfs(tcp); 22417 DB_TYPE(mp) = M_IOCACK; 22418 iocp->ioc_error = 0; 22419 } 22420 iocp->ioc_count = 0; 22421 iocp->ioc_rval = 0; 22422 qreply(q, mp); 22423 return; 22424 } 22425 CALL_IP_WPUT(connp, q, mp); 22426 } 22427 22428 /* 22429 * This routine is called by tcp_wput() to handle all TPI requests. 22430 */ 22431 /* ARGSUSED */ 22432 static void 22433 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22434 { 22435 conn_t *connp = (conn_t *)arg; 22436 tcp_t *tcp = connp->conn_tcp; 22437 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22438 uchar_t *rptr; 22439 t_scalar_t type; 22440 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22441 22442 /* 22443 * Try and ASSERT the minimum possible references on the 22444 * conn early enough. Since we are executing on write side, 22445 * the connection is obviously not detached and that means 22446 * there is a ref each for TCP and IP. Since we are behind 22447 * the squeue, the minimum references needed are 3. If the 22448 * conn is in classifier hash list, there should be an 22449 * extra ref for that (we check both the possibilities). 22450 */ 22451 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22452 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22453 22454 rptr = mp->b_rptr; 22455 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22456 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22457 type = ((union T_primitives *)rptr)->type; 22458 if (type == T_EXDATA_REQ) { 22459 tcp_output_urgent(connp, mp->b_cont, arg2); 22460 freeb(mp); 22461 } else if (type != T_DATA_REQ) { 22462 goto non_urgent_data; 22463 } else { 22464 /* TODO: options, flags, ... from user */ 22465 /* Set length to zero for reclamation below */ 22466 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22467 freeb(mp); 22468 } 22469 return; 22470 } else { 22471 if (tcp->tcp_debug) { 22472 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22473 "tcp_wput_proto, dropping one..."); 22474 } 22475 freemsg(mp); 22476 return; 22477 } 22478 22479 non_urgent_data: 22480 22481 switch ((int)tprim->type) { 22482 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22483 /* 22484 * save the kssl_ent_t from the next block, and convert this 22485 * back to a normal bind_req. 22486 */ 22487 if (mp->b_cont != NULL) { 22488 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22489 22490 if (tcp->tcp_kssl_ent != NULL) { 22491 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22492 KSSL_NO_PROXY); 22493 tcp->tcp_kssl_ent = NULL; 22494 } 22495 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22496 sizeof (kssl_ent_t)); 22497 kssl_hold_ent(tcp->tcp_kssl_ent); 22498 freemsg(mp->b_cont); 22499 mp->b_cont = NULL; 22500 } 22501 tprim->type = T_BIND_REQ; 22502 22503 /* FALLTHROUGH */ 22504 case O_T_BIND_REQ: /* bind request */ 22505 case T_BIND_REQ: /* new semantics bind request */ 22506 tcp_tpi_bind(tcp, mp); 22507 break; 22508 case T_UNBIND_REQ: /* unbind request */ 22509 tcp_tpi_unbind(tcp, mp); 22510 break; 22511 case O_T_CONN_RES: /* old connection response XXX */ 22512 case T_CONN_RES: /* connection response */ 22513 tcp_tli_accept(tcp, mp); 22514 break; 22515 case T_CONN_REQ: /* connection request */ 22516 tcp_tpi_connect(tcp, mp); 22517 break; 22518 case T_DISCON_REQ: /* disconnect request */ 22519 tcp_disconnect(tcp, mp); 22520 break; 22521 case T_CAPABILITY_REQ: 22522 tcp_capability_req(tcp, mp); /* capability request */ 22523 break; 22524 case T_INFO_REQ: /* information request */ 22525 tcp_info_req(tcp, mp); 22526 break; 22527 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22528 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, 22529 &tcp_opt_obj, B_TRUE); 22530 break; 22531 case T_OPTMGMT_REQ: 22532 /* 22533 * Note: no support for snmpcom_req() through new 22534 * T_OPTMGMT_REQ. See comments in ip.c 22535 */ 22536 /* Only IP is allowed to return meaningful value */ 22537 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22538 B_TRUE); 22539 break; 22540 22541 case T_UNITDATA_REQ: /* unitdata request */ 22542 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22543 break; 22544 case T_ORDREL_REQ: /* orderly release req */ 22545 freemsg(mp); 22546 22547 if (tcp->tcp_fused) 22548 tcp_unfuse(tcp); 22549 22550 if (tcp_xmit_end(tcp) != 0) { 22551 /* 22552 * We were crossing FINs and got a reset from 22553 * the other side. Just ignore it. 22554 */ 22555 if (tcp->tcp_debug) { 22556 (void) strlog(TCP_MOD_ID, 0, 1, 22557 SL_ERROR|SL_TRACE, 22558 "tcp_wput_proto, T_ORDREL_REQ out of " 22559 "state %s", 22560 tcp_display(tcp, NULL, 22561 DISP_ADDR_AND_PORT)); 22562 } 22563 } 22564 break; 22565 case T_ADDR_REQ: 22566 tcp_addr_req(tcp, mp); 22567 break; 22568 default: 22569 if (tcp->tcp_debug) { 22570 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22571 "tcp_wput_proto, bogus TPI msg, type %d", 22572 tprim->type); 22573 } 22574 /* 22575 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22576 * to recover. 22577 */ 22578 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22579 break; 22580 } 22581 } 22582 22583 /* 22584 * The TCP write service routine should never be called... 22585 */ 22586 /* ARGSUSED */ 22587 static void 22588 tcp_wsrv(queue_t *q) 22589 { 22590 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22591 22592 TCP_STAT(tcps, tcp_wsrv_called); 22593 } 22594 22595 /* Non overlapping byte exchanger */ 22596 static void 22597 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22598 { 22599 uchar_t uch; 22600 22601 while (len-- > 0) { 22602 uch = a[len]; 22603 a[len] = b[len]; 22604 b[len] = uch; 22605 } 22606 } 22607 22608 /* 22609 * Send out a control packet on the tcp connection specified. This routine 22610 * is typically called where we need a simple ACK or RST generated. 22611 */ 22612 static void 22613 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22614 { 22615 uchar_t *rptr; 22616 tcph_t *tcph; 22617 ipha_t *ipha = NULL; 22618 ip6_t *ip6h = NULL; 22619 uint32_t sum; 22620 int tcp_hdr_len; 22621 int tcp_ip_hdr_len; 22622 mblk_t *mp; 22623 tcp_stack_t *tcps = tcp->tcp_tcps; 22624 22625 /* 22626 * Save sum for use in source route later. 22627 */ 22628 ASSERT(tcp != NULL); 22629 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22630 tcp_hdr_len = tcp->tcp_hdr_len; 22631 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22632 22633 /* If a text string is passed in with the request, pass it to strlog. */ 22634 if (str != NULL && tcp->tcp_debug) { 22635 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22636 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22637 str, seq, ack, ctl); 22638 } 22639 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22640 BPRI_MED); 22641 if (mp == NULL) { 22642 return; 22643 } 22644 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22645 mp->b_rptr = rptr; 22646 mp->b_wptr = &rptr[tcp_hdr_len]; 22647 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22648 22649 if (tcp->tcp_ipversion == IPV4_VERSION) { 22650 ipha = (ipha_t *)rptr; 22651 ipha->ipha_length = htons(tcp_hdr_len); 22652 } else { 22653 ip6h = (ip6_t *)rptr; 22654 ASSERT(tcp != NULL); 22655 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22656 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22657 } 22658 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22659 tcph->th_flags[0] = (uint8_t)ctl; 22660 if (ctl & TH_RST) { 22661 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22662 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22663 /* 22664 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22665 */ 22666 if (tcp->tcp_snd_ts_ok && 22667 tcp->tcp_state > TCPS_SYN_SENT) { 22668 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22669 *(mp->b_wptr) = TCPOPT_EOL; 22670 if (tcp->tcp_ipversion == IPV4_VERSION) { 22671 ipha->ipha_length = htons(tcp_hdr_len - 22672 TCPOPT_REAL_TS_LEN); 22673 } else { 22674 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22675 TCPOPT_REAL_TS_LEN); 22676 } 22677 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22678 sum -= TCPOPT_REAL_TS_LEN; 22679 } 22680 } 22681 if (ctl & TH_ACK) { 22682 if (tcp->tcp_snd_ts_ok) { 22683 U32_TO_BE32(lbolt, 22684 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22685 U32_TO_BE32(tcp->tcp_ts_recent, 22686 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22687 } 22688 22689 /* Update the latest receive window size in TCP header. */ 22690 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22691 tcph->th_win); 22692 tcp->tcp_rack = ack; 22693 tcp->tcp_rack_cnt = 0; 22694 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22695 } 22696 BUMP_LOCAL(tcp->tcp_obsegs); 22697 U32_TO_BE32(seq, tcph->th_seq); 22698 U32_TO_BE32(ack, tcph->th_ack); 22699 /* 22700 * Include the adjustment for a source route if any. 22701 */ 22702 sum = (sum >> 16) + (sum & 0xFFFF); 22703 U16_TO_BE16(sum, tcph->th_sum); 22704 tcp_send_data(tcp, tcp->tcp_wq, mp); 22705 } 22706 22707 /* 22708 * If this routine returns B_TRUE, TCP can generate a RST in response 22709 * to a segment. If it returns B_FALSE, TCP should not respond. 22710 */ 22711 static boolean_t 22712 tcp_send_rst_chk(tcp_stack_t *tcps) 22713 { 22714 clock_t now; 22715 22716 /* 22717 * TCP needs to protect itself from generating too many RSTs. 22718 * This can be a DoS attack by sending us random segments 22719 * soliciting RSTs. 22720 * 22721 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22722 * in each 1 second interval. In this way, TCP still generate 22723 * RSTs in normal cases but when under attack, the impact is 22724 * limited. 22725 */ 22726 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22727 now = lbolt; 22728 /* lbolt can wrap around. */ 22729 if ((tcps->tcps_last_rst_intrvl > now) || 22730 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22731 1*SECONDS)) { 22732 tcps->tcps_last_rst_intrvl = now; 22733 tcps->tcps_rst_cnt = 1; 22734 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22735 return (B_FALSE); 22736 } 22737 } 22738 return (B_TRUE); 22739 } 22740 22741 /* 22742 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22743 */ 22744 static void 22745 tcp_ip_ire_mark_advice(tcp_t *tcp) 22746 { 22747 mblk_t *mp; 22748 ipic_t *ipic; 22749 22750 if (tcp->tcp_ipversion == IPV4_VERSION) { 22751 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22752 &ipic); 22753 } else { 22754 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22755 &ipic); 22756 } 22757 if (mp == NULL) 22758 return; 22759 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22760 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22761 } 22762 22763 /* 22764 * Return an IP advice ioctl mblk and set ipic to be the pointer 22765 * to the advice structure. 22766 */ 22767 static mblk_t * 22768 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22769 { 22770 struct iocblk *ioc; 22771 mblk_t *mp, *mp1; 22772 22773 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22774 if (mp == NULL) 22775 return (NULL); 22776 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22777 *ipic = (ipic_t *)mp->b_rptr; 22778 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22779 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22780 22781 bcopy(addr, *ipic + 1, addr_len); 22782 22783 (*ipic)->ipic_addr_length = addr_len; 22784 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22785 22786 mp1 = mkiocb(IP_IOCTL); 22787 if (mp1 == NULL) { 22788 freemsg(mp); 22789 return (NULL); 22790 } 22791 mp1->b_cont = mp; 22792 ioc = (struct iocblk *)mp1->b_rptr; 22793 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22794 22795 return (mp1); 22796 } 22797 22798 /* 22799 * Generate a reset based on an inbound packet, connp is set by caller 22800 * when RST is in response to an unexpected inbound packet for which 22801 * there is active tcp state in the system. 22802 * 22803 * IPSEC NOTE : Try to send the reply with the same protection as it came 22804 * in. We still have the ipsec_mp that the packet was attached to. Thus 22805 * the packet will go out at the same level of protection as it came in by 22806 * converting the IPSEC_IN to IPSEC_OUT. 22807 */ 22808 static void 22809 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22810 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22811 tcp_stack_t *tcps, conn_t *connp) 22812 { 22813 ipha_t *ipha = NULL; 22814 ip6_t *ip6h = NULL; 22815 ushort_t len; 22816 tcph_t *tcph; 22817 int i; 22818 mblk_t *ipsec_mp; 22819 boolean_t mctl_present; 22820 ipic_t *ipic; 22821 ipaddr_t v4addr; 22822 in6_addr_t v6addr; 22823 int addr_len; 22824 void *addr; 22825 queue_t *q = tcps->tcps_g_q; 22826 tcp_t *tcp; 22827 cred_t *cr; 22828 mblk_t *nmp; 22829 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22830 22831 if (tcps->tcps_g_q == NULL) { 22832 /* 22833 * For non-zero stackids the default queue isn't created 22834 * until the first open, thus there can be a need to send 22835 * a reset before then. But we can't do that, hence we just 22836 * drop the packet. Later during boot, when the default queue 22837 * has been setup, a retransmitted packet from the peer 22838 * will result in a reset. 22839 */ 22840 ASSERT(tcps->tcps_netstack->netstack_stackid != 22841 GLOBAL_NETSTACKID); 22842 freemsg(mp); 22843 return; 22844 } 22845 22846 if (connp != NULL) 22847 tcp = connp->conn_tcp; 22848 else 22849 tcp = Q_TO_TCP(q); 22850 22851 if (!tcp_send_rst_chk(tcps)) { 22852 tcps->tcps_rst_unsent++; 22853 freemsg(mp); 22854 return; 22855 } 22856 22857 if (mp->b_datap->db_type == M_CTL) { 22858 ipsec_mp = mp; 22859 mp = mp->b_cont; 22860 mctl_present = B_TRUE; 22861 } else { 22862 ipsec_mp = mp; 22863 mctl_present = B_FALSE; 22864 } 22865 22866 if (str && q && tcps->tcps_dbg) { 22867 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22868 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22869 "flags 0x%x", 22870 str, seq, ack, ctl); 22871 } 22872 if (mp->b_datap->db_ref != 1) { 22873 mblk_t *mp1 = copyb(mp); 22874 freemsg(mp); 22875 mp = mp1; 22876 if (!mp) { 22877 if (mctl_present) 22878 freeb(ipsec_mp); 22879 return; 22880 } else { 22881 if (mctl_present) { 22882 ipsec_mp->b_cont = mp; 22883 } else { 22884 ipsec_mp = mp; 22885 } 22886 } 22887 } else if (mp->b_cont) { 22888 freemsg(mp->b_cont); 22889 mp->b_cont = NULL; 22890 } 22891 /* 22892 * We skip reversing source route here. 22893 * (for now we replace all IP options with EOL) 22894 */ 22895 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22896 ipha = (ipha_t *)mp->b_rptr; 22897 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22898 mp->b_rptr[i] = IPOPT_EOL; 22899 /* 22900 * Make sure that src address isn't flagrantly invalid. 22901 * Not all broadcast address checking for the src address 22902 * is possible, since we don't know the netmask of the src 22903 * addr. No check for destination address is done, since 22904 * IP will not pass up a packet with a broadcast dest 22905 * address to TCP. Similar checks are done below for IPv6. 22906 */ 22907 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22908 CLASSD(ipha->ipha_src)) { 22909 freemsg(ipsec_mp); 22910 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22911 return; 22912 } 22913 } else { 22914 ip6h = (ip6_t *)mp->b_rptr; 22915 22916 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22917 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22918 freemsg(ipsec_mp); 22919 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22920 return; 22921 } 22922 22923 /* Remove any extension headers assuming partial overlay */ 22924 if (ip_hdr_len > IPV6_HDR_LEN) { 22925 uint8_t *to; 22926 22927 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22928 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22929 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22930 ip_hdr_len = IPV6_HDR_LEN; 22931 ip6h = (ip6_t *)mp->b_rptr; 22932 ip6h->ip6_nxt = IPPROTO_TCP; 22933 } 22934 } 22935 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22936 if (tcph->th_flags[0] & TH_RST) { 22937 freemsg(ipsec_mp); 22938 return; 22939 } 22940 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22941 len = ip_hdr_len + sizeof (tcph_t); 22942 mp->b_wptr = &mp->b_rptr[len]; 22943 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22944 ipha->ipha_length = htons(len); 22945 /* Swap addresses */ 22946 v4addr = ipha->ipha_src; 22947 ipha->ipha_src = ipha->ipha_dst; 22948 ipha->ipha_dst = v4addr; 22949 ipha->ipha_ident = 0; 22950 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22951 addr_len = IP_ADDR_LEN; 22952 addr = &v4addr; 22953 } else { 22954 /* No ip6i_t in this case */ 22955 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22956 /* Swap addresses */ 22957 v6addr = ip6h->ip6_src; 22958 ip6h->ip6_src = ip6h->ip6_dst; 22959 ip6h->ip6_dst = v6addr; 22960 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22961 addr_len = IPV6_ADDR_LEN; 22962 addr = &v6addr; 22963 } 22964 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22965 U32_TO_BE32(ack, tcph->th_ack); 22966 U32_TO_BE32(seq, tcph->th_seq); 22967 U16_TO_BE16(0, tcph->th_win); 22968 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22969 tcph->th_flags[0] = (uint8_t)ctl; 22970 if (ctl & TH_RST) { 22971 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22972 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22973 } 22974 22975 /* IP trusts us to set up labels when required. */ 22976 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22977 crgetlabel(cr) != NULL) { 22978 int err; 22979 22980 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22981 err = tsol_check_label(cr, &mp, 22982 tcp->tcp_connp->conn_mac_exempt, 22983 tcps->tcps_netstack->netstack_ip); 22984 else 22985 err = tsol_check_label_v6(cr, &mp, 22986 tcp->tcp_connp->conn_mac_exempt, 22987 tcps->tcps_netstack->netstack_ip); 22988 if (mctl_present) 22989 ipsec_mp->b_cont = mp; 22990 else 22991 ipsec_mp = mp; 22992 if (err != 0) { 22993 freemsg(ipsec_mp); 22994 return; 22995 } 22996 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22997 ipha = (ipha_t *)mp->b_rptr; 22998 } else { 22999 ip6h = (ip6_t *)mp->b_rptr; 23000 } 23001 } 23002 23003 if (mctl_present) { 23004 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23005 23006 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23007 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23008 return; 23009 } 23010 } 23011 if (zoneid == ALL_ZONES) 23012 zoneid = GLOBAL_ZONEID; 23013 23014 /* Add the zoneid so ip_output routes it properly */ 23015 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23016 freemsg(ipsec_mp); 23017 return; 23018 } 23019 ipsec_mp = nmp; 23020 23021 /* 23022 * NOTE: one might consider tracing a TCP packet here, but 23023 * this function has no active TCP state and no tcp structure 23024 * that has a trace buffer. If we traced here, we would have 23025 * to keep a local trace buffer in tcp_record_trace(). 23026 * 23027 * TSol note: The mblk that contains the incoming packet was 23028 * reused by tcp_xmit_listener_reset, so it already contains 23029 * the right credentials and we don't need to call mblk_setcred. 23030 * Also the conn's cred is not right since it is associated 23031 * with tcps_g_q. 23032 */ 23033 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23034 23035 /* 23036 * Tell IP to mark the IRE used for this destination temporary. 23037 * This way, we can limit our exposure to DoS attack because IP 23038 * creates an IRE for each destination. If there are too many, 23039 * the time to do any routing lookup will be extremely long. And 23040 * the lookup can be in interrupt context. 23041 * 23042 * Note that in normal circumstances, this marking should not 23043 * affect anything. It would be nice if only 1 message is 23044 * needed to inform IP that the IRE created for this RST should 23045 * not be added to the cache table. But there is currently 23046 * not such communication mechanism between TCP and IP. So 23047 * the best we can do now is to send the advice ioctl to IP 23048 * to mark the IRE temporary. 23049 */ 23050 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23051 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23052 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23053 } 23054 } 23055 23056 /* 23057 * Initiate closedown sequence on an active connection. (May be called as 23058 * writer.) Return value zero for OK return, non-zero for error return. 23059 */ 23060 static int 23061 tcp_xmit_end(tcp_t *tcp) 23062 { 23063 ipic_t *ipic; 23064 mblk_t *mp; 23065 tcp_stack_t *tcps = tcp->tcp_tcps; 23066 23067 if (tcp->tcp_state < TCPS_SYN_RCVD || 23068 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23069 /* 23070 * Invalid state, only states TCPS_SYN_RCVD, 23071 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23072 */ 23073 return (-1); 23074 } 23075 23076 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23077 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23078 /* 23079 * If there is nothing more unsent, send the FIN now. 23080 * Otherwise, it will go out with the last segment. 23081 */ 23082 if (tcp->tcp_unsent == 0) { 23083 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23084 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23085 23086 if (mp) { 23087 tcp_send_data(tcp, tcp->tcp_wq, mp); 23088 } else { 23089 /* 23090 * Couldn't allocate msg. Pretend we got it out. 23091 * Wait for rexmit timeout. 23092 */ 23093 tcp->tcp_snxt = tcp->tcp_fss + 1; 23094 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23095 } 23096 23097 /* 23098 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23099 * changed. 23100 */ 23101 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23102 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23103 } 23104 } else { 23105 /* 23106 * If tcp->tcp_cork is set, then the data will not get sent, 23107 * so we have to check that and unset it first. 23108 */ 23109 if (tcp->tcp_cork) 23110 tcp->tcp_cork = B_FALSE; 23111 tcp_wput_data(tcp, NULL, B_FALSE); 23112 } 23113 23114 /* 23115 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23116 * is 0, don't update the cache. 23117 */ 23118 if (tcps->tcps_rtt_updates == 0 || 23119 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23120 return (0); 23121 23122 /* 23123 * NOTE: should not update if source routes i.e. if tcp_remote if 23124 * different from the destination. 23125 */ 23126 if (tcp->tcp_ipversion == IPV4_VERSION) { 23127 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23128 return (0); 23129 } 23130 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23131 &ipic); 23132 } else { 23133 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23134 &tcp->tcp_ip6h->ip6_dst))) { 23135 return (0); 23136 } 23137 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23138 &ipic); 23139 } 23140 23141 /* Record route attributes in the IRE for use by future connections. */ 23142 if (mp == NULL) 23143 return (0); 23144 23145 /* 23146 * We do not have a good algorithm to update ssthresh at this time. 23147 * So don't do any update. 23148 */ 23149 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23150 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23151 23152 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23153 23154 return (0); 23155 } 23156 23157 /* 23158 * Generate a "no listener here" RST in response to an "unknown" segment. 23159 * connp is set by caller when RST is in response to an unexpected 23160 * inbound packet for which there is active tcp state in the system. 23161 * Note that we are reusing the incoming mp to construct the outgoing RST. 23162 */ 23163 void 23164 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23165 tcp_stack_t *tcps, conn_t *connp) 23166 { 23167 uchar_t *rptr; 23168 uint32_t seg_len; 23169 tcph_t *tcph; 23170 uint32_t seg_seq; 23171 uint32_t seg_ack; 23172 uint_t flags; 23173 mblk_t *ipsec_mp; 23174 ipha_t *ipha; 23175 ip6_t *ip6h; 23176 boolean_t mctl_present = B_FALSE; 23177 boolean_t check = B_TRUE; 23178 boolean_t policy_present; 23179 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23180 23181 TCP_STAT(tcps, tcp_no_listener); 23182 23183 ipsec_mp = mp; 23184 23185 if (mp->b_datap->db_type == M_CTL) { 23186 ipsec_in_t *ii; 23187 23188 mctl_present = B_TRUE; 23189 mp = mp->b_cont; 23190 23191 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23192 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23193 if (ii->ipsec_in_dont_check) { 23194 check = B_FALSE; 23195 if (!ii->ipsec_in_secure) { 23196 freeb(ipsec_mp); 23197 mctl_present = B_FALSE; 23198 ipsec_mp = mp; 23199 } 23200 } 23201 } 23202 23203 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23204 policy_present = ipss->ipsec_inbound_v4_policy_present; 23205 ipha = (ipha_t *)mp->b_rptr; 23206 ip6h = NULL; 23207 } else { 23208 policy_present = ipss->ipsec_inbound_v6_policy_present; 23209 ipha = NULL; 23210 ip6h = (ip6_t *)mp->b_rptr; 23211 } 23212 23213 if (check && policy_present) { 23214 /* 23215 * The conn_t parameter is NULL because we already know 23216 * nobody's home. 23217 */ 23218 ipsec_mp = ipsec_check_global_policy( 23219 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23220 tcps->tcps_netstack); 23221 if (ipsec_mp == NULL) 23222 return; 23223 } 23224 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23225 DTRACE_PROBE2( 23226 tx__ip__log__error__nolistener__tcp, 23227 char *, "Could not reply with RST to mp(1)", 23228 mblk_t *, mp); 23229 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23230 freemsg(ipsec_mp); 23231 return; 23232 } 23233 23234 rptr = mp->b_rptr; 23235 23236 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23237 seg_seq = BE32_TO_U32(tcph->th_seq); 23238 seg_ack = BE32_TO_U32(tcph->th_ack); 23239 flags = tcph->th_flags[0]; 23240 23241 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23242 if (flags & TH_RST) { 23243 freemsg(ipsec_mp); 23244 } else if (flags & TH_ACK) { 23245 tcp_xmit_early_reset("no tcp, reset", 23246 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23247 connp); 23248 } else { 23249 if (flags & TH_SYN) { 23250 seg_len++; 23251 } else { 23252 /* 23253 * Here we violate the RFC. Note that a normal 23254 * TCP will never send a segment without the ACK 23255 * flag, except for RST or SYN segment. This 23256 * segment is neither. Just drop it on the 23257 * floor. 23258 */ 23259 freemsg(ipsec_mp); 23260 tcps->tcps_rst_unsent++; 23261 return; 23262 } 23263 23264 tcp_xmit_early_reset("no tcp, reset/ack", 23265 ipsec_mp, 0, seg_seq + seg_len, 23266 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23267 } 23268 } 23269 23270 /* 23271 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23272 * ip and tcp header ready to pass down to IP. If the mp passed in is 23273 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23274 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23275 * otherwise it will dup partial mblks.) 23276 * Otherwise, an appropriate ACK packet will be generated. This 23277 * routine is not usually called to send new data for the first time. It 23278 * is mostly called out of the timer for retransmits, and to generate ACKs. 23279 * 23280 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23281 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23282 * of the original mblk chain will be returned in *offset and *end_mp. 23283 */ 23284 mblk_t * 23285 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23286 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23287 boolean_t rexmit) 23288 { 23289 int data_length; 23290 int32_t off = 0; 23291 uint_t flags; 23292 mblk_t *mp1; 23293 mblk_t *mp2; 23294 uchar_t *rptr; 23295 tcph_t *tcph; 23296 int32_t num_sack_blk = 0; 23297 int32_t sack_opt_len = 0; 23298 tcp_stack_t *tcps = tcp->tcp_tcps; 23299 23300 /* Allocate for our maximum TCP header + link-level */ 23301 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23302 tcps->tcps_wroff_xtra, BPRI_MED); 23303 if (!mp1) 23304 return (NULL); 23305 data_length = 0; 23306 23307 /* 23308 * Note that tcp_mss has been adjusted to take into account the 23309 * timestamp option if applicable. Because SACK options do not 23310 * appear in every TCP segments and they are of variable lengths, 23311 * they cannot be included in tcp_mss. Thus we need to calculate 23312 * the actual segment length when we need to send a segment which 23313 * includes SACK options. 23314 */ 23315 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23316 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23317 tcp->tcp_num_sack_blk); 23318 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23319 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23320 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23321 max_to_send -= sack_opt_len; 23322 } 23323 23324 if (offset != NULL) { 23325 off = *offset; 23326 /* We use offset as an indicator that end_mp is not NULL. */ 23327 *end_mp = NULL; 23328 } 23329 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23330 /* This could be faster with cooperation from downstream */ 23331 if (mp2 != mp1 && !sendall && 23332 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23333 max_to_send) 23334 /* 23335 * Don't send the next mblk since the whole mblk 23336 * does not fit. 23337 */ 23338 break; 23339 mp2->b_cont = dupb(mp); 23340 mp2 = mp2->b_cont; 23341 if (!mp2) { 23342 freemsg(mp1); 23343 return (NULL); 23344 } 23345 mp2->b_rptr += off; 23346 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23347 (uintptr_t)INT_MAX); 23348 23349 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23350 if (data_length > max_to_send) { 23351 mp2->b_wptr -= data_length - max_to_send; 23352 data_length = max_to_send; 23353 off = mp2->b_wptr - mp->b_rptr; 23354 break; 23355 } else { 23356 off = 0; 23357 } 23358 } 23359 if (offset != NULL) { 23360 *offset = off; 23361 *end_mp = mp; 23362 } 23363 if (seg_len != NULL) { 23364 *seg_len = data_length; 23365 } 23366 23367 /* Update the latest receive window size in TCP header. */ 23368 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23369 tcp->tcp_tcph->th_win); 23370 23371 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23372 mp1->b_rptr = rptr; 23373 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23374 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23375 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23376 U32_TO_ABE32(seq, tcph->th_seq); 23377 23378 /* 23379 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23380 * that this function was called from tcp_wput_data. Thus, when called 23381 * to retransmit data the setting of the PUSH bit may appear some 23382 * what random in that it might get set when it should not. This 23383 * should not pose any performance issues. 23384 */ 23385 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23386 tcp->tcp_unsent == data_length)) { 23387 flags = TH_ACK | TH_PUSH; 23388 } else { 23389 flags = TH_ACK; 23390 } 23391 23392 if (tcp->tcp_ecn_ok) { 23393 if (tcp->tcp_ecn_echo_on) 23394 flags |= TH_ECE; 23395 23396 /* 23397 * Only set ECT bit and ECN_CWR if a segment contains new data. 23398 * There is no TCP flow control for non-data segments, and 23399 * only data segment is transmitted reliably. 23400 */ 23401 if (data_length > 0 && !rexmit) { 23402 SET_ECT(tcp, rptr); 23403 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23404 flags |= TH_CWR; 23405 tcp->tcp_ecn_cwr_sent = B_TRUE; 23406 } 23407 } 23408 } 23409 23410 if (tcp->tcp_valid_bits) { 23411 uint32_t u1; 23412 23413 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23414 seq == tcp->tcp_iss) { 23415 uchar_t *wptr; 23416 23417 /* 23418 * If TCP_ISS_VALID and the seq number is tcp_iss, 23419 * TCP can only be in SYN-SENT, SYN-RCVD or 23420 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23421 * our SYN is not ack'ed but the app closes this 23422 * TCP connection. 23423 */ 23424 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23425 tcp->tcp_state == TCPS_SYN_RCVD || 23426 tcp->tcp_state == TCPS_FIN_WAIT_1); 23427 23428 /* 23429 * Tack on the MSS option. It is always needed 23430 * for both active and passive open. 23431 * 23432 * MSS option value should be interface MTU - MIN 23433 * TCP/IP header according to RFC 793 as it means 23434 * the maximum segment size TCP can receive. But 23435 * to get around some broken middle boxes/end hosts 23436 * out there, we allow the option value to be the 23437 * same as the MSS option size on the peer side. 23438 * In this way, the other side will not send 23439 * anything larger than they can receive. 23440 * 23441 * Note that for SYN_SENT state, the ndd param 23442 * tcp_use_smss_as_mss_opt has no effect as we 23443 * don't know the peer's MSS option value. So 23444 * the only case we need to take care of is in 23445 * SYN_RCVD state, which is done later. 23446 */ 23447 wptr = mp1->b_wptr; 23448 wptr[0] = TCPOPT_MAXSEG; 23449 wptr[1] = TCPOPT_MAXSEG_LEN; 23450 wptr += 2; 23451 u1 = tcp->tcp_if_mtu - 23452 (tcp->tcp_ipversion == IPV4_VERSION ? 23453 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23454 TCP_MIN_HEADER_LENGTH; 23455 U16_TO_BE16(u1, wptr); 23456 mp1->b_wptr = wptr + 2; 23457 /* Update the offset to cover the additional word */ 23458 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23459 23460 /* 23461 * Note that the following way of filling in 23462 * TCP options are not optimal. Some NOPs can 23463 * be saved. But there is no need at this time 23464 * to optimize it. When it is needed, we will 23465 * do it. 23466 */ 23467 switch (tcp->tcp_state) { 23468 case TCPS_SYN_SENT: 23469 flags = TH_SYN; 23470 23471 if (tcp->tcp_snd_ts_ok) { 23472 uint32_t llbolt = (uint32_t)lbolt; 23473 23474 wptr = mp1->b_wptr; 23475 wptr[0] = TCPOPT_NOP; 23476 wptr[1] = TCPOPT_NOP; 23477 wptr[2] = TCPOPT_TSTAMP; 23478 wptr[3] = TCPOPT_TSTAMP_LEN; 23479 wptr += 4; 23480 U32_TO_BE32(llbolt, wptr); 23481 wptr += 4; 23482 ASSERT(tcp->tcp_ts_recent == 0); 23483 U32_TO_BE32(0L, wptr); 23484 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23485 tcph->th_offset_and_rsrvd[0] += 23486 (3 << 4); 23487 } 23488 23489 /* 23490 * Set up all the bits to tell other side 23491 * we are ECN capable. 23492 */ 23493 if (tcp->tcp_ecn_ok) { 23494 flags |= (TH_ECE | TH_CWR); 23495 } 23496 break; 23497 case TCPS_SYN_RCVD: 23498 flags |= TH_SYN; 23499 23500 /* 23501 * Reset the MSS option value to be SMSS 23502 * We should probably add back the bytes 23503 * for timestamp option and IPsec. We 23504 * don't do that as this is a workaround 23505 * for broken middle boxes/end hosts, it 23506 * is better for us to be more cautious. 23507 * They may not take these things into 23508 * account in their SMSS calculation. Thus 23509 * the peer's calculated SMSS may be smaller 23510 * than what it can be. This should be OK. 23511 */ 23512 if (tcps->tcps_use_smss_as_mss_opt) { 23513 u1 = tcp->tcp_mss; 23514 U16_TO_BE16(u1, wptr); 23515 } 23516 23517 /* 23518 * If the other side is ECN capable, reply 23519 * that we are also ECN capable. 23520 */ 23521 if (tcp->tcp_ecn_ok) 23522 flags |= TH_ECE; 23523 break; 23524 default: 23525 /* 23526 * The above ASSERT() makes sure that this 23527 * must be FIN-WAIT-1 state. Our SYN has 23528 * not been ack'ed so retransmit it. 23529 */ 23530 flags |= TH_SYN; 23531 break; 23532 } 23533 23534 if (tcp->tcp_snd_ws_ok) { 23535 wptr = mp1->b_wptr; 23536 wptr[0] = TCPOPT_NOP; 23537 wptr[1] = TCPOPT_WSCALE; 23538 wptr[2] = TCPOPT_WS_LEN; 23539 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23540 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23541 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23542 } 23543 23544 if (tcp->tcp_snd_sack_ok) { 23545 wptr = mp1->b_wptr; 23546 wptr[0] = TCPOPT_NOP; 23547 wptr[1] = TCPOPT_NOP; 23548 wptr[2] = TCPOPT_SACK_PERMITTED; 23549 wptr[3] = TCPOPT_SACK_OK_LEN; 23550 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23551 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23552 } 23553 23554 /* allocb() of adequate mblk assures space */ 23555 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23556 (uintptr_t)INT_MAX); 23557 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23558 /* 23559 * Get IP set to checksum on our behalf 23560 * Include the adjustment for a source route if any. 23561 */ 23562 u1 += tcp->tcp_sum; 23563 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23564 U16_TO_BE16(u1, tcph->th_sum); 23565 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23566 } 23567 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23568 (seq + data_length) == tcp->tcp_fss) { 23569 if (!tcp->tcp_fin_acked) { 23570 flags |= TH_FIN; 23571 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23572 } 23573 if (!tcp->tcp_fin_sent) { 23574 tcp->tcp_fin_sent = B_TRUE; 23575 switch (tcp->tcp_state) { 23576 case TCPS_SYN_RCVD: 23577 case TCPS_ESTABLISHED: 23578 tcp->tcp_state = TCPS_FIN_WAIT_1; 23579 break; 23580 case TCPS_CLOSE_WAIT: 23581 tcp->tcp_state = TCPS_LAST_ACK; 23582 break; 23583 } 23584 if (tcp->tcp_suna == tcp->tcp_snxt) 23585 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23586 tcp->tcp_snxt = tcp->tcp_fss + 1; 23587 } 23588 } 23589 /* 23590 * Note the trick here. u1 is unsigned. When tcp_urg 23591 * is smaller than seq, u1 will become a very huge value. 23592 * So the comparison will fail. Also note that tcp_urp 23593 * should be positive, see RFC 793 page 17. 23594 */ 23595 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23596 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23597 u1 < (uint32_t)(64 * 1024)) { 23598 flags |= TH_URG; 23599 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23600 U32_TO_ABE16(u1, tcph->th_urp); 23601 } 23602 } 23603 tcph->th_flags[0] = (uchar_t)flags; 23604 tcp->tcp_rack = tcp->tcp_rnxt; 23605 tcp->tcp_rack_cnt = 0; 23606 23607 if (tcp->tcp_snd_ts_ok) { 23608 if (tcp->tcp_state != TCPS_SYN_SENT) { 23609 uint32_t llbolt = (uint32_t)lbolt; 23610 23611 U32_TO_BE32(llbolt, 23612 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23613 U32_TO_BE32(tcp->tcp_ts_recent, 23614 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23615 } 23616 } 23617 23618 if (num_sack_blk > 0) { 23619 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23620 sack_blk_t *tmp; 23621 int32_t i; 23622 23623 wptr[0] = TCPOPT_NOP; 23624 wptr[1] = TCPOPT_NOP; 23625 wptr[2] = TCPOPT_SACK; 23626 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23627 sizeof (sack_blk_t); 23628 wptr += TCPOPT_REAL_SACK_LEN; 23629 23630 tmp = tcp->tcp_sack_list; 23631 for (i = 0; i < num_sack_blk; i++) { 23632 U32_TO_BE32(tmp[i].begin, wptr); 23633 wptr += sizeof (tcp_seq); 23634 U32_TO_BE32(tmp[i].end, wptr); 23635 wptr += sizeof (tcp_seq); 23636 } 23637 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23638 } 23639 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23640 data_length += (int)(mp1->b_wptr - rptr); 23641 if (tcp->tcp_ipversion == IPV4_VERSION) { 23642 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23643 } else { 23644 ip6_t *ip6 = (ip6_t *)(rptr + 23645 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23646 sizeof (ip6i_t) : 0)); 23647 23648 ip6->ip6_plen = htons(data_length - 23649 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23650 } 23651 23652 /* 23653 * Prime pump for IP 23654 * Include the adjustment for a source route if any. 23655 */ 23656 data_length -= tcp->tcp_ip_hdr_len; 23657 data_length += tcp->tcp_sum; 23658 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23659 U16_TO_ABE16(data_length, tcph->th_sum); 23660 if (tcp->tcp_ip_forward_progress) { 23661 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23662 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23663 tcp->tcp_ip_forward_progress = B_FALSE; 23664 } 23665 return (mp1); 23666 } 23667 23668 /* This function handles the push timeout. */ 23669 void 23670 tcp_push_timer(void *arg) 23671 { 23672 conn_t *connp = (conn_t *)arg; 23673 tcp_t *tcp = connp->conn_tcp; 23674 uint_t flags; 23675 sodirect_t *sodp; 23676 23677 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23678 23679 ASSERT(tcp->tcp_listener == NULL); 23680 23681 ASSERT(!IPCL_IS_NONSTR(connp)); 23682 23683 /* 23684 * We need to plug synchronous streams during our drain to prevent 23685 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23686 */ 23687 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23688 tcp->tcp_push_tid = 0; 23689 23690 SOD_PTR_ENTER(tcp, sodp); 23691 if (sodp != NULL) { 23692 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23693 /* sod_wakeup() does the mutex_exit() */ 23694 } else if (tcp->tcp_rcv_list != NULL) { 23695 flags = tcp_rcv_drain(tcp); 23696 } 23697 if (flags == TH_ACK_NEEDED) 23698 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23699 23700 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23701 } 23702 23703 /* 23704 * This function handles delayed ACK timeout. 23705 */ 23706 static void 23707 tcp_ack_timer(void *arg) 23708 { 23709 conn_t *connp = (conn_t *)arg; 23710 tcp_t *tcp = connp->conn_tcp; 23711 mblk_t *mp; 23712 tcp_stack_t *tcps = tcp->tcp_tcps; 23713 23714 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23715 23716 tcp->tcp_ack_tid = 0; 23717 23718 if (tcp->tcp_fused) 23719 return; 23720 23721 /* 23722 * Do not send ACK if there is no outstanding unack'ed data. 23723 */ 23724 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23725 return; 23726 } 23727 23728 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23729 /* 23730 * Make sure we don't allow deferred ACKs to result in 23731 * timer-based ACKing. If we have held off an ACK 23732 * when there was more than an mss here, and the timer 23733 * goes off, we have to worry about the possibility 23734 * that the sender isn't doing slow-start, or is out 23735 * of step with us for some other reason. We fall 23736 * permanently back in the direction of 23737 * ACK-every-other-packet as suggested in RFC 1122. 23738 */ 23739 if (tcp->tcp_rack_abs_max > 2) 23740 tcp->tcp_rack_abs_max--; 23741 tcp->tcp_rack_cur_max = 2; 23742 } 23743 mp = tcp_ack_mp(tcp); 23744 23745 if (mp != NULL) { 23746 BUMP_LOCAL(tcp->tcp_obsegs); 23747 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23748 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23749 tcp_send_data(tcp, tcp->tcp_wq, mp); 23750 } 23751 } 23752 23753 23754 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23755 static mblk_t * 23756 tcp_ack_mp(tcp_t *tcp) 23757 { 23758 uint32_t seq_no; 23759 tcp_stack_t *tcps = tcp->tcp_tcps; 23760 23761 /* 23762 * There are a few cases to be considered while setting the sequence no. 23763 * Essentially, we can come here while processing an unacceptable pkt 23764 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23765 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23766 * If we are here for a zero window probe, stick with suna. In all 23767 * other cases, we check if suna + swnd encompasses snxt and set 23768 * the sequence number to snxt, if so. If snxt falls outside the 23769 * window (the receiver probably shrunk its window), we will go with 23770 * suna + swnd, otherwise the sequence no will be unacceptable to the 23771 * receiver. 23772 */ 23773 if (tcp->tcp_zero_win_probe) { 23774 seq_no = tcp->tcp_suna; 23775 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23776 ASSERT(tcp->tcp_swnd == 0); 23777 seq_no = tcp->tcp_snxt; 23778 } else { 23779 seq_no = SEQ_GT(tcp->tcp_snxt, 23780 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23781 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23782 } 23783 23784 if (tcp->tcp_valid_bits) { 23785 /* 23786 * For the complex case where we have to send some 23787 * controls (FIN or SYN), let tcp_xmit_mp do it. 23788 */ 23789 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23790 NULL, B_FALSE)); 23791 } else { 23792 /* Generate a simple ACK */ 23793 int data_length; 23794 uchar_t *rptr; 23795 tcph_t *tcph; 23796 mblk_t *mp1; 23797 int32_t tcp_hdr_len; 23798 int32_t tcp_tcp_hdr_len; 23799 int32_t num_sack_blk = 0; 23800 int32_t sack_opt_len; 23801 23802 /* 23803 * Allocate space for TCP + IP headers 23804 * and link-level header 23805 */ 23806 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23807 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23808 tcp->tcp_num_sack_blk); 23809 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23810 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23811 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23812 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23813 } else { 23814 tcp_hdr_len = tcp->tcp_hdr_len; 23815 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23816 } 23817 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23818 if (!mp1) 23819 return (NULL); 23820 23821 /* Update the latest receive window size in TCP header. */ 23822 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23823 tcp->tcp_tcph->th_win); 23824 /* copy in prototype TCP + IP header */ 23825 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23826 mp1->b_rptr = rptr; 23827 mp1->b_wptr = rptr + tcp_hdr_len; 23828 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23829 23830 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23831 23832 /* Set the TCP sequence number. */ 23833 U32_TO_ABE32(seq_no, tcph->th_seq); 23834 23835 /* Set up the TCP flag field. */ 23836 tcph->th_flags[0] = (uchar_t)TH_ACK; 23837 if (tcp->tcp_ecn_echo_on) 23838 tcph->th_flags[0] |= TH_ECE; 23839 23840 tcp->tcp_rack = tcp->tcp_rnxt; 23841 tcp->tcp_rack_cnt = 0; 23842 23843 /* fill in timestamp option if in use */ 23844 if (tcp->tcp_snd_ts_ok) { 23845 uint32_t llbolt = (uint32_t)lbolt; 23846 23847 U32_TO_BE32(llbolt, 23848 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23849 U32_TO_BE32(tcp->tcp_ts_recent, 23850 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23851 } 23852 23853 /* Fill in SACK options */ 23854 if (num_sack_blk > 0) { 23855 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23856 sack_blk_t *tmp; 23857 int32_t i; 23858 23859 wptr[0] = TCPOPT_NOP; 23860 wptr[1] = TCPOPT_NOP; 23861 wptr[2] = TCPOPT_SACK; 23862 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23863 sizeof (sack_blk_t); 23864 wptr += TCPOPT_REAL_SACK_LEN; 23865 23866 tmp = tcp->tcp_sack_list; 23867 for (i = 0; i < num_sack_blk; i++) { 23868 U32_TO_BE32(tmp[i].begin, wptr); 23869 wptr += sizeof (tcp_seq); 23870 U32_TO_BE32(tmp[i].end, wptr); 23871 wptr += sizeof (tcp_seq); 23872 } 23873 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23874 << 4); 23875 } 23876 23877 if (tcp->tcp_ipversion == IPV4_VERSION) { 23878 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23879 } else { 23880 /* Check for ip6i_t header in sticky hdrs */ 23881 ip6_t *ip6 = (ip6_t *)(rptr + 23882 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23883 sizeof (ip6i_t) : 0)); 23884 23885 ip6->ip6_plen = htons(tcp_hdr_len - 23886 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23887 } 23888 23889 /* 23890 * Prime pump for checksum calculation in IP. Include the 23891 * adjustment for a source route if any. 23892 */ 23893 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23894 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23895 U16_TO_ABE16(data_length, tcph->th_sum); 23896 23897 if (tcp->tcp_ip_forward_progress) { 23898 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23899 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23900 tcp->tcp_ip_forward_progress = B_FALSE; 23901 } 23902 return (mp1); 23903 } 23904 } 23905 23906 /* 23907 * Hash list insertion routine for tcp_t structures. Each hash bucket 23908 * contains a list of tcp_t entries, and each entry is bound to a unique 23909 * port. If there are multiple tcp_t's that are bound to the same port, then 23910 * one of them will be linked into the hash bucket list, and the rest will 23911 * hang off of that one entry. For each port, entries bound to a specific IP 23912 * address will be inserted before those those bound to INADDR_ANY. 23913 */ 23914 static void 23915 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23916 { 23917 tcp_t **tcpp; 23918 tcp_t *tcpnext; 23919 tcp_t *tcphash; 23920 23921 if (tcp->tcp_ptpbhn != NULL) { 23922 ASSERT(!caller_holds_lock); 23923 tcp_bind_hash_remove(tcp); 23924 } 23925 tcpp = &tbf->tf_tcp; 23926 if (!caller_holds_lock) { 23927 mutex_enter(&tbf->tf_lock); 23928 } else { 23929 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23930 } 23931 tcphash = tcpp[0]; 23932 tcpnext = NULL; 23933 if (tcphash != NULL) { 23934 /* Look for an entry using the same port */ 23935 while ((tcphash = tcpp[0]) != NULL && 23936 tcp->tcp_lport != tcphash->tcp_lport) 23937 tcpp = &(tcphash->tcp_bind_hash); 23938 23939 /* The port was not found, just add to the end */ 23940 if (tcphash == NULL) 23941 goto insert; 23942 23943 /* 23944 * OK, there already exists an entry bound to the 23945 * same port. 23946 * 23947 * If the new tcp bound to the INADDR_ANY address 23948 * and the first one in the list is not bound to 23949 * INADDR_ANY we skip all entries until we find the 23950 * first one bound to INADDR_ANY. 23951 * This makes sure that applications binding to a 23952 * specific address get preference over those binding to 23953 * INADDR_ANY. 23954 */ 23955 tcpnext = tcphash; 23956 tcphash = NULL; 23957 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23958 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23959 while ((tcpnext = tcpp[0]) != NULL && 23960 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23961 tcpp = &(tcpnext->tcp_bind_hash_port); 23962 23963 if (tcpnext) { 23964 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23965 tcphash = tcpnext->tcp_bind_hash; 23966 if (tcphash != NULL) { 23967 tcphash->tcp_ptpbhn = 23968 &(tcp->tcp_bind_hash); 23969 tcpnext->tcp_bind_hash = NULL; 23970 } 23971 } 23972 } else { 23973 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23974 tcphash = tcpnext->tcp_bind_hash; 23975 if (tcphash != NULL) { 23976 tcphash->tcp_ptpbhn = 23977 &(tcp->tcp_bind_hash); 23978 tcpnext->tcp_bind_hash = NULL; 23979 } 23980 } 23981 } 23982 insert: 23983 tcp->tcp_bind_hash_port = tcpnext; 23984 tcp->tcp_bind_hash = tcphash; 23985 tcp->tcp_ptpbhn = tcpp; 23986 tcpp[0] = tcp; 23987 if (!caller_holds_lock) 23988 mutex_exit(&tbf->tf_lock); 23989 } 23990 23991 /* 23992 * Hash list removal routine for tcp_t structures. 23993 */ 23994 static void 23995 tcp_bind_hash_remove(tcp_t *tcp) 23996 { 23997 tcp_t *tcpnext; 23998 kmutex_t *lockp; 23999 tcp_stack_t *tcps = tcp->tcp_tcps; 24000 24001 if (tcp->tcp_ptpbhn == NULL) 24002 return; 24003 24004 /* 24005 * Extract the lock pointer in case there are concurrent 24006 * hash_remove's for this instance. 24007 */ 24008 ASSERT(tcp->tcp_lport != 0); 24009 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24010 24011 ASSERT(lockp != NULL); 24012 mutex_enter(lockp); 24013 if (tcp->tcp_ptpbhn) { 24014 tcpnext = tcp->tcp_bind_hash_port; 24015 if (tcpnext != NULL) { 24016 tcp->tcp_bind_hash_port = NULL; 24017 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24018 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 24019 if (tcpnext->tcp_bind_hash != NULL) { 24020 tcpnext->tcp_bind_hash->tcp_ptpbhn = 24021 &(tcpnext->tcp_bind_hash); 24022 tcp->tcp_bind_hash = NULL; 24023 } 24024 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 24025 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24026 tcp->tcp_bind_hash = NULL; 24027 } 24028 *tcp->tcp_ptpbhn = tcpnext; 24029 tcp->tcp_ptpbhn = NULL; 24030 } 24031 mutex_exit(lockp); 24032 } 24033 24034 24035 /* 24036 * Hash list lookup routine for tcp_t structures. 24037 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24038 */ 24039 static tcp_t * 24040 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24041 { 24042 tf_t *tf; 24043 tcp_t *tcp; 24044 24045 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24046 mutex_enter(&tf->tf_lock); 24047 for (tcp = tf->tf_tcp; tcp != NULL; 24048 tcp = tcp->tcp_acceptor_hash) { 24049 if (tcp->tcp_acceptor_id == id) { 24050 CONN_INC_REF(tcp->tcp_connp); 24051 mutex_exit(&tf->tf_lock); 24052 return (tcp); 24053 } 24054 } 24055 mutex_exit(&tf->tf_lock); 24056 return (NULL); 24057 } 24058 24059 24060 /* 24061 * Hash list insertion routine for tcp_t structures. 24062 */ 24063 void 24064 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24065 { 24066 tf_t *tf; 24067 tcp_t **tcpp; 24068 tcp_t *tcpnext; 24069 tcp_stack_t *tcps = tcp->tcp_tcps; 24070 24071 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24072 24073 if (tcp->tcp_ptpahn != NULL) 24074 tcp_acceptor_hash_remove(tcp); 24075 tcpp = &tf->tf_tcp; 24076 mutex_enter(&tf->tf_lock); 24077 tcpnext = tcpp[0]; 24078 if (tcpnext) 24079 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24080 tcp->tcp_acceptor_hash = tcpnext; 24081 tcp->tcp_ptpahn = tcpp; 24082 tcpp[0] = tcp; 24083 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24084 mutex_exit(&tf->tf_lock); 24085 } 24086 24087 /* 24088 * Hash list removal routine for tcp_t structures. 24089 */ 24090 static void 24091 tcp_acceptor_hash_remove(tcp_t *tcp) 24092 { 24093 tcp_t *tcpnext; 24094 kmutex_t *lockp; 24095 24096 /* 24097 * Extract the lock pointer in case there are concurrent 24098 * hash_remove's for this instance. 24099 */ 24100 lockp = tcp->tcp_acceptor_lockp; 24101 24102 if (tcp->tcp_ptpahn == NULL) 24103 return; 24104 24105 ASSERT(lockp != NULL); 24106 mutex_enter(lockp); 24107 if (tcp->tcp_ptpahn) { 24108 tcpnext = tcp->tcp_acceptor_hash; 24109 if (tcpnext) { 24110 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24111 tcp->tcp_acceptor_hash = NULL; 24112 } 24113 *tcp->tcp_ptpahn = tcpnext; 24114 tcp->tcp_ptpahn = NULL; 24115 } 24116 mutex_exit(lockp); 24117 tcp->tcp_acceptor_lockp = NULL; 24118 } 24119 24120 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24121 24122 static ipaddr_t netmasks[] = { 24123 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24124 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24125 }; 24126 24127 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24128 24129 /* 24130 * XXX This routine should go away and instead we should use the metrics 24131 * associated with the routes to determine the default sndspace and rcvspace. 24132 */ 24133 static tcp_hsp_t * 24134 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24135 { 24136 tcp_hsp_t *hsp = NULL; 24137 24138 /* Quick check without acquiring the lock. */ 24139 if (tcps->tcps_hsp_hash == NULL) 24140 return (NULL); 24141 24142 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24143 24144 /* This routine finds the best-matching HSP for address addr. */ 24145 24146 if (tcps->tcps_hsp_hash) { 24147 int i; 24148 ipaddr_t srchaddr; 24149 tcp_hsp_t *hsp_net; 24150 24151 /* We do three passes: host, network, and subnet. */ 24152 24153 srchaddr = addr; 24154 24155 for (i = 1; i <= 3; i++) { 24156 /* Look for exact match on srchaddr */ 24157 24158 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24159 while (hsp) { 24160 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24161 hsp->tcp_hsp_addr == srchaddr) 24162 break; 24163 hsp = hsp->tcp_hsp_next; 24164 } 24165 ASSERT(hsp == NULL || 24166 hsp->tcp_hsp_vers == IPV4_VERSION); 24167 24168 /* 24169 * If this is the first pass: 24170 * If we found a match, great, return it. 24171 * If not, search for the network on the second pass. 24172 */ 24173 24174 if (i == 1) 24175 if (hsp) 24176 break; 24177 else 24178 { 24179 srchaddr = addr & netmask(addr); 24180 continue; 24181 } 24182 24183 /* 24184 * If this is the second pass: 24185 * If we found a match, but there's a subnet mask, 24186 * save the match but try again using the subnet 24187 * mask on the third pass. 24188 * Otherwise, return whatever we found. 24189 */ 24190 24191 if (i == 2) { 24192 if (hsp && hsp->tcp_hsp_subnet) { 24193 hsp_net = hsp; 24194 srchaddr = addr & hsp->tcp_hsp_subnet; 24195 continue; 24196 } else { 24197 break; 24198 } 24199 } 24200 24201 /* 24202 * This must be the third pass. If we didn't find 24203 * anything, return the saved network HSP instead. 24204 */ 24205 24206 if (!hsp) 24207 hsp = hsp_net; 24208 } 24209 } 24210 24211 rw_exit(&tcps->tcps_hsp_lock); 24212 return (hsp); 24213 } 24214 24215 /* 24216 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24217 * match lookup. 24218 */ 24219 static tcp_hsp_t * 24220 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24221 { 24222 tcp_hsp_t *hsp = NULL; 24223 24224 /* Quick check without acquiring the lock. */ 24225 if (tcps->tcps_hsp_hash == NULL) 24226 return (NULL); 24227 24228 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24229 24230 /* This routine finds the best-matching HSP for address addr. */ 24231 24232 if (tcps->tcps_hsp_hash) { 24233 int i; 24234 in6_addr_t v6srchaddr; 24235 tcp_hsp_t *hsp_net; 24236 24237 /* We do three passes: host, network, and subnet. */ 24238 24239 v6srchaddr = *v6addr; 24240 24241 for (i = 1; i <= 3; i++) { 24242 /* Look for exact match on srchaddr */ 24243 24244 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24245 V4_PART_OF_V6(v6srchaddr))]; 24246 while (hsp) { 24247 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24248 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24249 &v6srchaddr)) 24250 break; 24251 hsp = hsp->tcp_hsp_next; 24252 } 24253 24254 /* 24255 * If this is the first pass: 24256 * If we found a match, great, return it. 24257 * If not, search for the network on the second pass. 24258 */ 24259 24260 if (i == 1) 24261 if (hsp) 24262 break; 24263 else { 24264 /* Assume a 64 bit mask */ 24265 v6srchaddr.s6_addr32[0] = 24266 v6addr->s6_addr32[0]; 24267 v6srchaddr.s6_addr32[1] = 24268 v6addr->s6_addr32[1]; 24269 v6srchaddr.s6_addr32[2] = 0; 24270 v6srchaddr.s6_addr32[3] = 0; 24271 continue; 24272 } 24273 24274 /* 24275 * If this is the second pass: 24276 * If we found a match, but there's a subnet mask, 24277 * save the match but try again using the subnet 24278 * mask on the third pass. 24279 * Otherwise, return whatever we found. 24280 */ 24281 24282 if (i == 2) { 24283 ASSERT(hsp == NULL || 24284 hsp->tcp_hsp_vers == IPV6_VERSION); 24285 if (hsp && 24286 !IN6_IS_ADDR_UNSPECIFIED( 24287 &hsp->tcp_hsp_subnet_v6)) { 24288 hsp_net = hsp; 24289 V6_MASK_COPY(*v6addr, 24290 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24291 continue; 24292 } else { 24293 break; 24294 } 24295 } 24296 24297 /* 24298 * This must be the third pass. If we didn't find 24299 * anything, return the saved network HSP instead. 24300 */ 24301 24302 if (!hsp) 24303 hsp = hsp_net; 24304 } 24305 } 24306 24307 rw_exit(&tcps->tcps_hsp_lock); 24308 return (hsp); 24309 } 24310 24311 /* 24312 * Type three generator adapted from the random() function in 4.4 BSD: 24313 */ 24314 24315 /* 24316 * Copyright (c) 1983, 1993 24317 * The Regents of the University of California. All rights reserved. 24318 * 24319 * Redistribution and use in source and binary forms, with or without 24320 * modification, are permitted provided that the following conditions 24321 * are met: 24322 * 1. Redistributions of source code must retain the above copyright 24323 * notice, this list of conditions and the following disclaimer. 24324 * 2. Redistributions in binary form must reproduce the above copyright 24325 * notice, this list of conditions and the following disclaimer in the 24326 * documentation and/or other materials provided with the distribution. 24327 * 3. All advertising materials mentioning features or use of this software 24328 * must display the following acknowledgement: 24329 * This product includes software developed by the University of 24330 * California, Berkeley and its contributors. 24331 * 4. Neither the name of the University nor the names of its contributors 24332 * may be used to endorse or promote products derived from this software 24333 * without specific prior written permission. 24334 * 24335 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24336 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24337 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24338 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24339 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24340 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24341 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24342 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24343 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24344 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24345 * SUCH DAMAGE. 24346 */ 24347 24348 /* Type 3 -- x**31 + x**3 + 1 */ 24349 #define DEG_3 31 24350 #define SEP_3 3 24351 24352 24353 /* Protected by tcp_random_lock */ 24354 static int tcp_randtbl[DEG_3 + 1]; 24355 24356 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24357 static int *tcp_random_rptr = &tcp_randtbl[1]; 24358 24359 static int *tcp_random_state = &tcp_randtbl[1]; 24360 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24361 24362 kmutex_t tcp_random_lock; 24363 24364 void 24365 tcp_random_init(void) 24366 { 24367 int i; 24368 hrtime_t hrt; 24369 time_t wallclock; 24370 uint64_t result; 24371 24372 /* 24373 * Use high-res timer and current time for seed. Gethrtime() returns 24374 * a longlong, which may contain resolution down to nanoseconds. 24375 * The current time will either be a 32-bit or a 64-bit quantity. 24376 * XOR the two together in a 64-bit result variable. 24377 * Convert the result to a 32-bit value by multiplying the high-order 24378 * 32-bits by the low-order 32-bits. 24379 */ 24380 24381 hrt = gethrtime(); 24382 (void) drv_getparm(TIME, &wallclock); 24383 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24384 mutex_enter(&tcp_random_lock); 24385 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24386 (result & 0xffffffff); 24387 24388 for (i = 1; i < DEG_3; i++) 24389 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24390 + 12345; 24391 tcp_random_fptr = &tcp_random_state[SEP_3]; 24392 tcp_random_rptr = &tcp_random_state[0]; 24393 mutex_exit(&tcp_random_lock); 24394 for (i = 0; i < 10 * DEG_3; i++) 24395 (void) tcp_random(); 24396 } 24397 24398 /* 24399 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24400 * This range is selected to be approximately centered on TCP_ISS / 2, 24401 * and easy to compute. We get this value by generating a 32-bit random 24402 * number, selecting out the high-order 17 bits, and then adding one so 24403 * that we never return zero. 24404 */ 24405 int 24406 tcp_random(void) 24407 { 24408 int i; 24409 24410 mutex_enter(&tcp_random_lock); 24411 *tcp_random_fptr += *tcp_random_rptr; 24412 24413 /* 24414 * The high-order bits are more random than the low-order bits, 24415 * so we select out the high-order 17 bits and add one so that 24416 * we never return zero. 24417 */ 24418 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24419 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24420 tcp_random_fptr = tcp_random_state; 24421 ++tcp_random_rptr; 24422 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24423 tcp_random_rptr = tcp_random_state; 24424 24425 mutex_exit(&tcp_random_lock); 24426 return (i); 24427 } 24428 24429 static int 24430 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24431 int *t_errorp, int *sys_errorp) 24432 { 24433 int error; 24434 int is_absreq_failure; 24435 t_scalar_t *opt_lenp; 24436 t_scalar_t opt_offset; 24437 int prim_type; 24438 struct T_conn_req *tcreqp; 24439 struct T_conn_res *tcresp; 24440 cred_t *cr; 24441 24442 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24443 24444 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24445 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24446 prim_type == T_CONN_RES); 24447 24448 switch (prim_type) { 24449 case T_CONN_REQ: 24450 tcreqp = (struct T_conn_req *)mp->b_rptr; 24451 opt_offset = tcreqp->OPT_offset; 24452 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24453 break; 24454 case O_T_CONN_RES: 24455 case T_CONN_RES: 24456 tcresp = (struct T_conn_res *)mp->b_rptr; 24457 opt_offset = tcresp->OPT_offset; 24458 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24459 break; 24460 } 24461 24462 *t_errorp = 0; 24463 *sys_errorp = 0; 24464 *do_disconnectp = 0; 24465 24466 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24467 opt_offset, cr, &tcp_opt_obj, 24468 NULL, &is_absreq_failure); 24469 24470 switch (error) { 24471 case 0: /* no error */ 24472 ASSERT(is_absreq_failure == 0); 24473 return (0); 24474 case ENOPROTOOPT: 24475 *t_errorp = TBADOPT; 24476 break; 24477 case EACCES: 24478 *t_errorp = TACCES; 24479 break; 24480 default: 24481 *t_errorp = TSYSERR; *sys_errorp = error; 24482 break; 24483 } 24484 if (is_absreq_failure != 0) { 24485 /* 24486 * The connection request should get the local ack 24487 * T_OK_ACK and then a T_DISCON_IND. 24488 */ 24489 *do_disconnectp = 1; 24490 } 24491 return (-1); 24492 } 24493 24494 /* 24495 * Split this function out so that if the secret changes, I'm okay. 24496 * 24497 * Initialize the tcp_iss_cookie and tcp_iss_key. 24498 */ 24499 24500 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24501 24502 static void 24503 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24504 { 24505 struct { 24506 int32_t current_time; 24507 uint32_t randnum; 24508 uint16_t pad; 24509 uint8_t ether[6]; 24510 uint8_t passwd[PASSWD_SIZE]; 24511 } tcp_iss_cookie; 24512 time_t t; 24513 24514 /* 24515 * Start with the current absolute time. 24516 */ 24517 (void) drv_getparm(TIME, &t); 24518 tcp_iss_cookie.current_time = t; 24519 24520 /* 24521 * XXX - Need a more random number per RFC 1750, not this crap. 24522 * OTOH, if what follows is pretty random, then I'm in better shape. 24523 */ 24524 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24525 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24526 24527 /* 24528 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24529 * as a good template. 24530 */ 24531 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24532 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24533 24534 /* 24535 * The pass-phrase. Normally this is supplied by user-called NDD. 24536 */ 24537 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24538 24539 /* 24540 * See 4010593 if this section becomes a problem again, 24541 * but the local ethernet address is useful here. 24542 */ 24543 (void) localetheraddr(NULL, 24544 (struct ether_addr *)&tcp_iss_cookie.ether); 24545 24546 /* 24547 * Hash 'em all together. The MD5Final is called per-connection. 24548 */ 24549 mutex_enter(&tcps->tcps_iss_key_lock); 24550 MD5Init(&tcps->tcps_iss_key); 24551 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24552 sizeof (tcp_iss_cookie)); 24553 mutex_exit(&tcps->tcps_iss_key_lock); 24554 } 24555 24556 /* 24557 * Set the RFC 1948 pass phrase 24558 */ 24559 /* ARGSUSED */ 24560 static int 24561 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24562 cred_t *cr) 24563 { 24564 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24565 24566 /* 24567 * Basically, value contains a new pass phrase. Pass it along! 24568 */ 24569 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24570 return (0); 24571 } 24572 24573 /* ARGSUSED */ 24574 static int 24575 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24576 { 24577 bzero(buf, sizeof (tcp_sack_info_t)); 24578 return (0); 24579 } 24580 24581 /* ARGSUSED */ 24582 static int 24583 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24584 { 24585 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24586 return (0); 24587 } 24588 24589 /* 24590 * Make sure we wait until the default queue is setup, yet allow 24591 * tcp_g_q_create() to open a TCP stream. 24592 * We need to allow tcp_g_q_create() do do an open 24593 * of tcp, hence we compare curhread. 24594 * All others have to wait until the tcps_g_q has been 24595 * setup. 24596 */ 24597 void 24598 tcp_g_q_setup(tcp_stack_t *tcps) 24599 { 24600 mutex_enter(&tcps->tcps_g_q_lock); 24601 if (tcps->tcps_g_q != NULL) { 24602 mutex_exit(&tcps->tcps_g_q_lock); 24603 return; 24604 } 24605 if (tcps->tcps_g_q_creator == NULL) { 24606 /* This thread will set it up */ 24607 tcps->tcps_g_q_creator = curthread; 24608 mutex_exit(&tcps->tcps_g_q_lock); 24609 tcp_g_q_create(tcps); 24610 mutex_enter(&tcps->tcps_g_q_lock); 24611 ASSERT(tcps->tcps_g_q_creator == curthread); 24612 tcps->tcps_g_q_creator = NULL; 24613 cv_signal(&tcps->tcps_g_q_cv); 24614 ASSERT(tcps->tcps_g_q != NULL); 24615 mutex_exit(&tcps->tcps_g_q_lock); 24616 return; 24617 } 24618 /* Everybody but the creator has to wait */ 24619 if (tcps->tcps_g_q_creator != curthread) { 24620 while (tcps->tcps_g_q == NULL) 24621 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24622 } 24623 mutex_exit(&tcps->tcps_g_q_lock); 24624 } 24625 24626 #define IP "ip" 24627 24628 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24629 24630 /* 24631 * Create a default tcp queue here instead of in strplumb 24632 */ 24633 void 24634 tcp_g_q_create(tcp_stack_t *tcps) 24635 { 24636 int error; 24637 ldi_handle_t lh = NULL; 24638 ldi_ident_t li = NULL; 24639 int rval; 24640 cred_t *cr; 24641 major_t IP_MAJ; 24642 24643 #ifdef NS_DEBUG 24644 (void) printf("tcp_g_q_create()\n"); 24645 #endif 24646 24647 IP_MAJ = ddi_name_to_major(IP); 24648 24649 ASSERT(tcps->tcps_g_q_creator == curthread); 24650 24651 error = ldi_ident_from_major(IP_MAJ, &li); 24652 if (error) { 24653 #ifdef DEBUG 24654 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24655 error); 24656 #endif 24657 return; 24658 } 24659 24660 cr = zone_get_kcred(netstackid_to_zoneid( 24661 tcps->tcps_netstack->netstack_stackid)); 24662 ASSERT(cr != NULL); 24663 /* 24664 * We set the tcp default queue to IPv6 because IPv4 falls 24665 * back to IPv6 when it can't find a client, but 24666 * IPv6 does not fall back to IPv4. 24667 */ 24668 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24669 if (error) { 24670 #ifdef DEBUG 24671 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24672 error); 24673 #endif 24674 goto out; 24675 } 24676 24677 /* 24678 * This ioctl causes the tcp framework to cache a pointer to 24679 * this stream, so we don't want to close the stream after 24680 * this operation. 24681 * Use the kernel credentials that are for the zone we're in. 24682 */ 24683 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24684 (intptr_t)0, FKIOCTL, cr, &rval); 24685 if (error) { 24686 #ifdef DEBUG 24687 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24688 "error %d\n", error); 24689 #endif 24690 goto out; 24691 } 24692 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24693 lh = NULL; 24694 out: 24695 /* Close layered handles */ 24696 if (li) 24697 ldi_ident_release(li); 24698 /* Keep cred around until _inactive needs it */ 24699 tcps->tcps_g_q_cr = cr; 24700 } 24701 24702 /* 24703 * We keep tcp_g_q set until all other tcp_t's in the zone 24704 * has gone away, and then when tcp_g_q_inactive() is called 24705 * we clear it. 24706 */ 24707 void 24708 tcp_g_q_destroy(tcp_stack_t *tcps) 24709 { 24710 #ifdef NS_DEBUG 24711 (void) printf("tcp_g_q_destroy()for stack %d\n", 24712 tcps->tcps_netstack->netstack_stackid); 24713 #endif 24714 24715 if (tcps->tcps_g_q == NULL) { 24716 return; /* Nothing to cleanup */ 24717 } 24718 /* 24719 * Drop reference corresponding to the default queue. 24720 * This reference was added from tcp_open when the default queue 24721 * was created, hence we compensate for this extra drop in 24722 * tcp_g_q_close. If the refcnt drops to zero here it means 24723 * the default queue was the last one to be open, in which 24724 * case, then tcp_g_q_inactive will be 24725 * called as a result of the refrele. 24726 */ 24727 TCPS_REFRELE(tcps); 24728 } 24729 24730 /* 24731 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24732 * Run by tcp_q_q_inactive using a taskq. 24733 */ 24734 static void 24735 tcp_g_q_close(void *arg) 24736 { 24737 tcp_stack_t *tcps = arg; 24738 int error; 24739 ldi_handle_t lh = NULL; 24740 ldi_ident_t li = NULL; 24741 cred_t *cr; 24742 major_t IP_MAJ; 24743 24744 IP_MAJ = ddi_name_to_major(IP); 24745 24746 #ifdef NS_DEBUG 24747 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24748 tcps->tcps_netstack->netstack_stackid, 24749 tcps->tcps_netstack->netstack_refcnt); 24750 #endif 24751 lh = tcps->tcps_g_q_lh; 24752 if (lh == NULL) 24753 return; /* Nothing to cleanup */ 24754 24755 ASSERT(tcps->tcps_refcnt == 1); 24756 ASSERT(tcps->tcps_g_q != NULL); 24757 24758 error = ldi_ident_from_major(IP_MAJ, &li); 24759 if (error) { 24760 #ifdef DEBUG 24761 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24762 error); 24763 #endif 24764 return; 24765 } 24766 24767 cr = tcps->tcps_g_q_cr; 24768 tcps->tcps_g_q_cr = NULL; 24769 ASSERT(cr != NULL); 24770 24771 /* 24772 * Make sure we can break the recursion when tcp_close decrements 24773 * the reference count causing g_q_inactive to be called again. 24774 */ 24775 tcps->tcps_g_q_lh = NULL; 24776 24777 /* close the default queue */ 24778 (void) ldi_close(lh, FREAD|FWRITE, cr); 24779 /* 24780 * At this point in time tcps and the rest of netstack_t might 24781 * have been deleted. 24782 */ 24783 tcps = NULL; 24784 24785 /* Close layered handles */ 24786 ldi_ident_release(li); 24787 crfree(cr); 24788 } 24789 24790 /* 24791 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24792 * 24793 * Have to ensure that the ldi routines are not used by an 24794 * interrupt thread by using a taskq. 24795 */ 24796 void 24797 tcp_g_q_inactive(tcp_stack_t *tcps) 24798 { 24799 if (tcps->tcps_g_q_lh == NULL) 24800 return; /* Nothing to cleanup */ 24801 24802 ASSERT(tcps->tcps_refcnt == 0); 24803 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24804 24805 if (servicing_interrupt()) { 24806 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24807 (void *) tcps, TQ_SLEEP); 24808 } else { 24809 tcp_g_q_close(tcps); 24810 } 24811 } 24812 24813 /* 24814 * Called by IP when IP is loaded into the kernel 24815 */ 24816 void 24817 tcp_ddi_g_init(void) 24818 { 24819 tcp_timercache = kmem_cache_create("tcp_timercache", 24820 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24821 NULL, NULL, NULL, NULL, NULL, 0); 24822 24823 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24824 sizeof (tcp_sack_info_t), 0, 24825 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24826 24827 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24828 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24829 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24830 24831 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24832 24833 /* Initialize the random number generator */ 24834 tcp_random_init(); 24835 24836 /* A single callback independently of how many netstacks we have */ 24837 ip_squeue_init(tcp_squeue_add); 24838 24839 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24840 24841 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24842 TASKQ_PREPOPULATE); 24843 24844 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24845 24846 /* 24847 * We want to be informed each time a stack is created or 24848 * destroyed in the kernel, so we can maintain the 24849 * set of tcp_stack_t's. 24850 */ 24851 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24852 tcp_stack_fini); 24853 } 24854 24855 24856 #define INET_NAME "ip" 24857 24858 /* 24859 * Initialize the TCP stack instance. 24860 */ 24861 static void * 24862 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24863 { 24864 tcp_stack_t *tcps; 24865 tcpparam_t *pa; 24866 int i; 24867 int error = 0; 24868 major_t major; 24869 24870 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24871 tcps->tcps_netstack = ns; 24872 24873 /* Initialize locks */ 24874 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24875 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24876 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24877 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24878 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24879 24880 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24881 tcps->tcps_g_epriv_ports[0] = 2049; 24882 tcps->tcps_g_epriv_ports[1] = 4045; 24883 tcps->tcps_min_anonpriv_port = 512; 24884 24885 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24886 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24887 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24888 TCP_FANOUT_SIZE, KM_SLEEP); 24889 24890 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24891 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24892 MUTEX_DEFAULT, NULL); 24893 } 24894 24895 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24896 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24897 MUTEX_DEFAULT, NULL); 24898 } 24899 24900 /* TCP's IPsec code calls the packet dropper. */ 24901 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24902 24903 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24904 tcps->tcps_params = pa; 24905 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24906 24907 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24908 A_CNT(lcl_tcp_param_arr), tcps); 24909 24910 /* 24911 * Note: To really walk the device tree you need the devinfo 24912 * pointer to your device which is only available after probe/attach. 24913 * The following is safe only because it uses ddi_root_node() 24914 */ 24915 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24916 tcp_opt_obj.odb_opt_arr_cnt); 24917 24918 /* 24919 * Initialize RFC 1948 secret values. This will probably be reset once 24920 * by the boot scripts. 24921 * 24922 * Use NULL name, as the name is caught by the new lockstats. 24923 * 24924 * Initialize with some random, non-guessable string, like the global 24925 * T_INFO_ACK. 24926 */ 24927 24928 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24929 sizeof (tcp_g_t_info_ack), tcps); 24930 24931 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24932 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24933 24934 major = mod_name_to_major(INET_NAME); 24935 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24936 ASSERT(error == 0); 24937 return (tcps); 24938 } 24939 24940 /* 24941 * Called when the IP module is about to be unloaded. 24942 */ 24943 void 24944 tcp_ddi_g_destroy(void) 24945 { 24946 tcp_g_kstat_fini(tcp_g_kstat); 24947 tcp_g_kstat = NULL; 24948 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24949 24950 mutex_destroy(&tcp_random_lock); 24951 24952 kmem_cache_destroy(tcp_timercache); 24953 kmem_cache_destroy(tcp_sack_info_cache); 24954 kmem_cache_destroy(tcp_iphc_cache); 24955 24956 netstack_unregister(NS_TCP); 24957 taskq_destroy(tcp_taskq); 24958 } 24959 24960 /* 24961 * Shut down the TCP stack instance. 24962 */ 24963 /* ARGSUSED */ 24964 static void 24965 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24966 { 24967 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24968 24969 tcp_g_q_destroy(tcps); 24970 } 24971 24972 /* 24973 * Free the TCP stack instance. 24974 */ 24975 static void 24976 tcp_stack_fini(netstackid_t stackid, void *arg) 24977 { 24978 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24979 int i; 24980 24981 nd_free(&tcps->tcps_g_nd); 24982 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24983 tcps->tcps_params = NULL; 24984 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24985 tcps->tcps_wroff_xtra_param = NULL; 24986 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24987 tcps->tcps_mdt_head_param = NULL; 24988 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24989 tcps->tcps_mdt_tail_param = NULL; 24990 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 24991 tcps->tcps_mdt_max_pbufs_param = NULL; 24992 24993 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24994 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 24995 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 24996 } 24997 24998 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24999 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25000 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25001 } 25002 25003 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25004 tcps->tcps_bind_fanout = NULL; 25005 25006 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25007 tcps->tcps_acceptor_fanout = NULL; 25008 25009 mutex_destroy(&tcps->tcps_iss_key_lock); 25010 rw_destroy(&tcps->tcps_hsp_lock); 25011 mutex_destroy(&tcps->tcps_g_q_lock); 25012 cv_destroy(&tcps->tcps_g_q_cv); 25013 mutex_destroy(&tcps->tcps_epriv_port_lock); 25014 25015 ip_drop_unregister(&tcps->tcps_dropper); 25016 25017 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25018 tcps->tcps_kstat = NULL; 25019 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25020 25021 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25022 tcps->tcps_mibkp = NULL; 25023 25024 ldi_ident_release(tcps->tcps_ldi_ident); 25025 kmem_free(tcps, sizeof (*tcps)); 25026 } 25027 25028 /* 25029 * Generate ISS, taking into account NDD changes may happen halfway through. 25030 * (If the iss is not zero, set it.) 25031 */ 25032 25033 static void 25034 tcp_iss_init(tcp_t *tcp) 25035 { 25036 MD5_CTX context; 25037 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25038 uint32_t answer[4]; 25039 tcp_stack_t *tcps = tcp->tcp_tcps; 25040 25041 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25042 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25043 switch (tcps->tcps_strong_iss) { 25044 case 2: 25045 mutex_enter(&tcps->tcps_iss_key_lock); 25046 context = tcps->tcps_iss_key; 25047 mutex_exit(&tcps->tcps_iss_key_lock); 25048 arg.ports = tcp->tcp_ports; 25049 if (tcp->tcp_ipversion == IPV4_VERSION) { 25050 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25051 &arg.src); 25052 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25053 &arg.dst); 25054 } else { 25055 arg.src = tcp->tcp_ip6h->ip6_src; 25056 arg.dst = tcp->tcp_ip6h->ip6_dst; 25057 } 25058 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25059 MD5Final((uchar_t *)answer, &context); 25060 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25061 /* 25062 * Now that we've hashed into a unique per-connection sequence 25063 * space, add a random increment per strong_iss == 1. So I 25064 * guess we'll have to... 25065 */ 25066 /* FALLTHRU */ 25067 case 1: 25068 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25069 break; 25070 default: 25071 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25072 break; 25073 } 25074 tcp->tcp_valid_bits = TCP_ISS_VALID; 25075 tcp->tcp_fss = tcp->tcp_iss - 1; 25076 tcp->tcp_suna = tcp->tcp_iss; 25077 tcp->tcp_snxt = tcp->tcp_iss + 1; 25078 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25079 tcp->tcp_csuna = tcp->tcp_snxt; 25080 } 25081 25082 /* 25083 * Exported routine for extracting active tcp connection status. 25084 * 25085 * This is used by the Solaris Cluster Networking software to 25086 * gather a list of connections that need to be forwarded to 25087 * specific nodes in the cluster when configuration changes occur. 25088 * 25089 * The callback is invoked for each tcp_t structure from all netstacks, 25090 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 25091 * from the netstack with the specified stack_id. Returning 25092 * non-zero from the callback routine terminates the search. 25093 */ 25094 int 25095 cl_tcp_walk_list(netstackid_t stack_id, 25096 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 25097 { 25098 netstack_handle_t nh; 25099 netstack_t *ns; 25100 int ret = 0; 25101 25102 if (stack_id >= 0) { 25103 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 25104 return (EINVAL); 25105 25106 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25107 ns->netstack_tcp); 25108 netstack_rele(ns); 25109 return (ret); 25110 } 25111 25112 netstack_next_init(&nh); 25113 while ((ns = netstack_next(&nh)) != NULL) { 25114 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25115 ns->netstack_tcp); 25116 netstack_rele(ns); 25117 } 25118 netstack_next_fini(&nh); 25119 return (ret); 25120 } 25121 25122 static int 25123 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25124 tcp_stack_t *tcps) 25125 { 25126 tcp_t *tcp; 25127 cl_tcp_info_t cl_tcpi; 25128 connf_t *connfp; 25129 conn_t *connp; 25130 int i; 25131 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25132 25133 ASSERT(callback != NULL); 25134 25135 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25136 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25137 connp = NULL; 25138 25139 while ((connp = 25140 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25141 25142 tcp = connp->conn_tcp; 25143 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25144 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25145 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25146 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25147 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25148 /* 25149 * The macros tcp_laddr and tcp_faddr give the IPv4 25150 * addresses. They are copied implicitly below as 25151 * mapped addresses. 25152 */ 25153 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25154 if (tcp->tcp_ipversion == IPV4_VERSION) { 25155 cl_tcpi.cl_tcpi_faddr = 25156 tcp->tcp_ipha->ipha_dst; 25157 } else { 25158 cl_tcpi.cl_tcpi_faddr_v6 = 25159 tcp->tcp_ip6h->ip6_dst; 25160 } 25161 25162 /* 25163 * If the callback returns non-zero 25164 * we terminate the traversal. 25165 */ 25166 if ((*callback)(&cl_tcpi, arg) != 0) { 25167 CONN_DEC_REF(tcp->tcp_connp); 25168 return (1); 25169 } 25170 } 25171 } 25172 25173 return (0); 25174 } 25175 25176 /* 25177 * Macros used for accessing the different types of sockaddr 25178 * structures inside a tcp_ioc_abort_conn_t. 25179 */ 25180 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25181 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25182 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25183 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25184 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25185 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25186 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25187 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25188 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25189 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25190 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25191 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25192 25193 /* 25194 * Return the correct error code to mimic the behavior 25195 * of a connection reset. 25196 */ 25197 #define TCP_AC_GET_ERRCODE(state, err) { \ 25198 switch ((state)) { \ 25199 case TCPS_SYN_SENT: \ 25200 case TCPS_SYN_RCVD: \ 25201 (err) = ECONNREFUSED; \ 25202 break; \ 25203 case TCPS_ESTABLISHED: \ 25204 case TCPS_FIN_WAIT_1: \ 25205 case TCPS_FIN_WAIT_2: \ 25206 case TCPS_CLOSE_WAIT: \ 25207 (err) = ECONNRESET; \ 25208 break; \ 25209 case TCPS_CLOSING: \ 25210 case TCPS_LAST_ACK: \ 25211 case TCPS_TIME_WAIT: \ 25212 (err) = 0; \ 25213 break; \ 25214 default: \ 25215 (err) = ENXIO; \ 25216 } \ 25217 } 25218 25219 /* 25220 * Check if a tcp structure matches the info in acp. 25221 */ 25222 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25223 (((acp)->ac_local.ss_family == AF_INET) ? \ 25224 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25225 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25226 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25227 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25228 (TCP_AC_V4LPORT((acp)) == 0 || \ 25229 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25230 (TCP_AC_V4RPORT((acp)) == 0 || \ 25231 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25232 (acp)->ac_start <= (tcp)->tcp_state && \ 25233 (acp)->ac_end >= (tcp)->tcp_state) : \ 25234 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25235 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25236 &(tcp)->tcp_ip_src_v6)) && \ 25237 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25238 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25239 &(tcp)->tcp_remote_v6)) && \ 25240 (TCP_AC_V6LPORT((acp)) == 0 || \ 25241 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25242 (TCP_AC_V6RPORT((acp)) == 0 || \ 25243 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25244 (acp)->ac_start <= (tcp)->tcp_state && \ 25245 (acp)->ac_end >= (tcp)->tcp_state)) 25246 25247 #define TCP_AC_MATCH(acp, tcp) \ 25248 (((acp)->ac_zoneid == ALL_ZONES || \ 25249 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25250 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25251 25252 /* 25253 * Build a message containing a tcp_ioc_abort_conn_t structure 25254 * which is filled in with information from acp and tp. 25255 */ 25256 static mblk_t * 25257 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25258 { 25259 mblk_t *mp; 25260 tcp_ioc_abort_conn_t *tacp; 25261 25262 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25263 if (mp == NULL) 25264 return (NULL); 25265 25266 mp->b_datap->db_type = M_CTL; 25267 25268 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25269 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25270 sizeof (uint32_t)); 25271 25272 tacp->ac_start = acp->ac_start; 25273 tacp->ac_end = acp->ac_end; 25274 tacp->ac_zoneid = acp->ac_zoneid; 25275 25276 if (acp->ac_local.ss_family == AF_INET) { 25277 tacp->ac_local.ss_family = AF_INET; 25278 tacp->ac_remote.ss_family = AF_INET; 25279 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25280 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25281 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25282 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25283 } else { 25284 tacp->ac_local.ss_family = AF_INET6; 25285 tacp->ac_remote.ss_family = AF_INET6; 25286 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25287 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25288 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25289 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25290 } 25291 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25292 return (mp); 25293 } 25294 25295 /* 25296 * Print a tcp_ioc_abort_conn_t structure. 25297 */ 25298 static void 25299 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25300 { 25301 char lbuf[128]; 25302 char rbuf[128]; 25303 sa_family_t af; 25304 in_port_t lport, rport; 25305 ushort_t logflags; 25306 25307 af = acp->ac_local.ss_family; 25308 25309 if (af == AF_INET) { 25310 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25311 lbuf, 128); 25312 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25313 rbuf, 128); 25314 lport = ntohs(TCP_AC_V4LPORT(acp)); 25315 rport = ntohs(TCP_AC_V4RPORT(acp)); 25316 } else { 25317 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25318 lbuf, 128); 25319 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25320 rbuf, 128); 25321 lport = ntohs(TCP_AC_V6LPORT(acp)); 25322 rport = ntohs(TCP_AC_V6RPORT(acp)); 25323 } 25324 25325 logflags = SL_TRACE | SL_NOTE; 25326 /* 25327 * Don't print this message to the console if the operation was done 25328 * to a non-global zone. 25329 */ 25330 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25331 logflags |= SL_CONSOLE; 25332 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25333 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25334 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25335 acp->ac_start, acp->ac_end); 25336 } 25337 25338 /* 25339 * Called inside tcp_rput when a message built using 25340 * tcp_ioctl_abort_build_msg is put into a queue. 25341 * Note that when we get here there is no wildcard in acp any more. 25342 */ 25343 static void 25344 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25345 { 25346 tcp_ioc_abort_conn_t *acp; 25347 25348 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25349 if (tcp->tcp_state <= acp->ac_end) { 25350 /* 25351 * If we get here, we are already on the correct 25352 * squeue. This ioctl follows the following path 25353 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25354 * ->tcp_ioctl_abort->squeue_enter (if on a 25355 * different squeue) 25356 */ 25357 int errcode; 25358 25359 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25360 (void) tcp_clean_death(tcp, errcode, 26); 25361 } 25362 freemsg(mp); 25363 } 25364 25365 /* 25366 * Abort all matching connections on a hash chain. 25367 */ 25368 static int 25369 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25370 boolean_t exact, tcp_stack_t *tcps) 25371 { 25372 int nmatch, err = 0; 25373 tcp_t *tcp; 25374 MBLKP mp, last, listhead = NULL; 25375 conn_t *tconnp; 25376 connf_t *connfp; 25377 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25378 25379 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25380 25381 startover: 25382 nmatch = 0; 25383 25384 mutex_enter(&connfp->connf_lock); 25385 for (tconnp = connfp->connf_head; tconnp != NULL; 25386 tconnp = tconnp->conn_next) { 25387 tcp = tconnp->conn_tcp; 25388 if (TCP_AC_MATCH(acp, tcp)) { 25389 CONN_INC_REF(tcp->tcp_connp); 25390 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25391 if (mp == NULL) { 25392 err = ENOMEM; 25393 CONN_DEC_REF(tcp->tcp_connp); 25394 break; 25395 } 25396 mp->b_prev = (mblk_t *)tcp; 25397 25398 if (listhead == NULL) { 25399 listhead = mp; 25400 last = mp; 25401 } else { 25402 last->b_next = mp; 25403 last = mp; 25404 } 25405 nmatch++; 25406 if (exact) 25407 break; 25408 } 25409 25410 /* Avoid holding lock for too long. */ 25411 if (nmatch >= 500) 25412 break; 25413 } 25414 mutex_exit(&connfp->connf_lock); 25415 25416 /* Pass mp into the correct tcp */ 25417 while ((mp = listhead) != NULL) { 25418 listhead = listhead->b_next; 25419 tcp = (tcp_t *)mp->b_prev; 25420 mp->b_next = mp->b_prev = NULL; 25421 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 25422 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 25423 } 25424 25425 *count += nmatch; 25426 if (nmatch >= 500 && err == 0) 25427 goto startover; 25428 return (err); 25429 } 25430 25431 /* 25432 * Abort all connections that matches the attributes specified in acp. 25433 */ 25434 static int 25435 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25436 { 25437 sa_family_t af; 25438 uint32_t ports; 25439 uint16_t *pports; 25440 int err = 0, count = 0; 25441 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25442 int index = -1; 25443 ushort_t logflags; 25444 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25445 25446 af = acp->ac_local.ss_family; 25447 25448 if (af == AF_INET) { 25449 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25450 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25451 pports = (uint16_t *)&ports; 25452 pports[1] = TCP_AC_V4LPORT(acp); 25453 pports[0] = TCP_AC_V4RPORT(acp); 25454 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25455 } 25456 } else { 25457 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25458 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25459 pports = (uint16_t *)&ports; 25460 pports[1] = TCP_AC_V6LPORT(acp); 25461 pports[0] = TCP_AC_V6RPORT(acp); 25462 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25463 } 25464 } 25465 25466 /* 25467 * For cases where remote addr, local port, and remote port are non- 25468 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25469 */ 25470 if (index != -1) { 25471 err = tcp_ioctl_abort_bucket(acp, index, 25472 &count, exact, tcps); 25473 } else { 25474 /* 25475 * loop through all entries for wildcard case 25476 */ 25477 for (index = 0; 25478 index < ipst->ips_ipcl_conn_fanout_size; 25479 index++) { 25480 err = tcp_ioctl_abort_bucket(acp, index, 25481 &count, exact, tcps); 25482 if (err != 0) 25483 break; 25484 } 25485 } 25486 25487 logflags = SL_TRACE | SL_NOTE; 25488 /* 25489 * Don't print this message to the console if the operation was done 25490 * to a non-global zone. 25491 */ 25492 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25493 logflags |= SL_CONSOLE; 25494 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25495 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25496 if (err == 0 && count == 0) 25497 err = ENOENT; 25498 return (err); 25499 } 25500 25501 /* 25502 * Process the TCP_IOC_ABORT_CONN ioctl request. 25503 */ 25504 static void 25505 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25506 { 25507 int err; 25508 IOCP iocp; 25509 MBLKP mp1; 25510 sa_family_t laf, raf; 25511 tcp_ioc_abort_conn_t *acp; 25512 zone_t *zptr; 25513 conn_t *connp = Q_TO_CONN(q); 25514 zoneid_t zoneid = connp->conn_zoneid; 25515 tcp_t *tcp = connp->conn_tcp; 25516 tcp_stack_t *tcps = tcp->tcp_tcps; 25517 25518 iocp = (IOCP)mp->b_rptr; 25519 25520 if ((mp1 = mp->b_cont) == NULL || 25521 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25522 err = EINVAL; 25523 goto out; 25524 } 25525 25526 /* check permissions */ 25527 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25528 err = EPERM; 25529 goto out; 25530 } 25531 25532 if (mp1->b_cont != NULL) { 25533 freemsg(mp1->b_cont); 25534 mp1->b_cont = NULL; 25535 } 25536 25537 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25538 laf = acp->ac_local.ss_family; 25539 raf = acp->ac_remote.ss_family; 25540 25541 /* check that a zone with the supplied zoneid exists */ 25542 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25543 zptr = zone_find_by_id(zoneid); 25544 if (zptr != NULL) { 25545 zone_rele(zptr); 25546 } else { 25547 err = EINVAL; 25548 goto out; 25549 } 25550 } 25551 25552 /* 25553 * For exclusive stacks we set the zoneid to zero 25554 * to make TCP operate as if in the global zone. 25555 */ 25556 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25557 acp->ac_zoneid = GLOBAL_ZONEID; 25558 25559 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25560 acp->ac_start > acp->ac_end || laf != raf || 25561 (laf != AF_INET && laf != AF_INET6)) { 25562 err = EINVAL; 25563 goto out; 25564 } 25565 25566 tcp_ioctl_abort_dump(acp); 25567 err = tcp_ioctl_abort(acp, tcps); 25568 25569 out: 25570 if (mp1 != NULL) { 25571 freemsg(mp1); 25572 mp->b_cont = NULL; 25573 } 25574 25575 if (err != 0) 25576 miocnak(q, mp, 0, err); 25577 else 25578 miocack(q, mp, 0, 0); 25579 } 25580 25581 /* 25582 * tcp_time_wait_processing() handles processing of incoming packets when 25583 * the tcp is in the TIME_WAIT state. 25584 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25585 * on the time wait list. 25586 */ 25587 void 25588 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25589 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25590 { 25591 int32_t bytes_acked; 25592 int32_t gap; 25593 int32_t rgap; 25594 tcp_opt_t tcpopt; 25595 uint_t flags; 25596 uint32_t new_swnd = 0; 25597 conn_t *connp; 25598 tcp_stack_t *tcps = tcp->tcp_tcps; 25599 25600 BUMP_LOCAL(tcp->tcp_ibsegs); 25601 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25602 25603 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25604 new_swnd = BE16_TO_U16(tcph->th_win) << 25605 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25606 if (tcp->tcp_snd_ts_ok) { 25607 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25608 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25609 tcp->tcp_rnxt, TH_ACK); 25610 goto done; 25611 } 25612 } 25613 gap = seg_seq - tcp->tcp_rnxt; 25614 rgap = tcp->tcp_rwnd - (gap + seg_len); 25615 if (gap < 0) { 25616 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25617 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25618 (seg_len > -gap ? -gap : seg_len)); 25619 seg_len += gap; 25620 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25621 if (flags & TH_RST) { 25622 goto done; 25623 } 25624 if ((flags & TH_FIN) && seg_len == -1) { 25625 /* 25626 * When TCP receives a duplicate FIN in 25627 * TIME_WAIT state, restart the 2 MSL timer. 25628 * See page 73 in RFC 793. Make sure this TCP 25629 * is already on the TIME_WAIT list. If not, 25630 * just restart the timer. 25631 */ 25632 if (TCP_IS_DETACHED(tcp)) { 25633 if (tcp_time_wait_remove(tcp, NULL) == 25634 B_TRUE) { 25635 tcp_time_wait_append(tcp); 25636 TCP_DBGSTAT(tcps, 25637 tcp_rput_time_wait); 25638 } 25639 } else { 25640 ASSERT(tcp != NULL); 25641 TCP_TIMER_RESTART(tcp, 25642 tcps->tcps_time_wait_interval); 25643 } 25644 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25645 tcp->tcp_rnxt, TH_ACK); 25646 goto done; 25647 } 25648 flags |= TH_ACK_NEEDED; 25649 seg_len = 0; 25650 goto process_ack; 25651 } 25652 25653 /* Fix seg_seq, and chew the gap off the front. */ 25654 seg_seq = tcp->tcp_rnxt; 25655 } 25656 25657 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25658 /* 25659 * Make sure that when we accept the connection, pick 25660 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25661 * old connection. 25662 * 25663 * The next ISS generated is equal to tcp_iss_incr_extra 25664 * + ISS_INCR/2 + other components depending on the 25665 * value of tcp_strong_iss. We pre-calculate the new 25666 * ISS here and compare with tcp_snxt to determine if 25667 * we need to make adjustment to tcp_iss_incr_extra. 25668 * 25669 * The above calculation is ugly and is a 25670 * waste of CPU cycles... 25671 */ 25672 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25673 int32_t adj; 25674 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25675 25676 switch (tcps->tcps_strong_iss) { 25677 case 2: { 25678 /* Add time and MD5 components. */ 25679 uint32_t answer[4]; 25680 struct { 25681 uint32_t ports; 25682 in6_addr_t src; 25683 in6_addr_t dst; 25684 } arg; 25685 MD5_CTX context; 25686 25687 mutex_enter(&tcps->tcps_iss_key_lock); 25688 context = tcps->tcps_iss_key; 25689 mutex_exit(&tcps->tcps_iss_key_lock); 25690 arg.ports = tcp->tcp_ports; 25691 /* We use MAPPED addresses in tcp_iss_init */ 25692 arg.src = tcp->tcp_ip_src_v6; 25693 if (tcp->tcp_ipversion == IPV4_VERSION) { 25694 IN6_IPADDR_TO_V4MAPPED( 25695 tcp->tcp_ipha->ipha_dst, 25696 &arg.dst); 25697 } else { 25698 arg.dst = 25699 tcp->tcp_ip6h->ip6_dst; 25700 } 25701 MD5Update(&context, (uchar_t *)&arg, 25702 sizeof (arg)); 25703 MD5Final((uchar_t *)answer, &context); 25704 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25705 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25706 break; 25707 } 25708 case 1: 25709 /* Add time component and min random (i.e. 1). */ 25710 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25711 break; 25712 default: 25713 /* Add only time component. */ 25714 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25715 break; 25716 } 25717 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25718 /* 25719 * New ISS not guaranteed to be ISS_INCR/2 25720 * ahead of the current tcp_snxt, so add the 25721 * difference to tcp_iss_incr_extra. 25722 */ 25723 tcps->tcps_iss_incr_extra += adj; 25724 } 25725 /* 25726 * If tcp_clean_death() can not perform the task now, 25727 * drop the SYN packet and let the other side re-xmit. 25728 * Otherwise pass the SYN packet back in, since the 25729 * old tcp state has been cleaned up or freed. 25730 */ 25731 if (tcp_clean_death(tcp, 0, 27) == -1) 25732 goto done; 25733 /* 25734 * We will come back to tcp_rput_data 25735 * on the global queue. Packets destined 25736 * for the global queue will be checked 25737 * with global policy. But the policy for 25738 * this packet has already been checked as 25739 * this was destined for the detached 25740 * connection. We need to bypass policy 25741 * check this time by attaching a dummy 25742 * ipsec_in with ipsec_in_dont_check set. 25743 */ 25744 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25745 if (connp != NULL) { 25746 TCP_STAT(tcps, tcp_time_wait_syn_success); 25747 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25748 return; 25749 } 25750 goto done; 25751 } 25752 25753 /* 25754 * rgap is the amount of stuff received out of window. A negative 25755 * value is the amount out of window. 25756 */ 25757 if (rgap < 0) { 25758 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25759 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25760 /* Fix seg_len and make sure there is something left. */ 25761 seg_len += rgap; 25762 if (seg_len <= 0) { 25763 if (flags & TH_RST) { 25764 goto done; 25765 } 25766 flags |= TH_ACK_NEEDED; 25767 seg_len = 0; 25768 goto process_ack; 25769 } 25770 } 25771 /* 25772 * Check whether we can update tcp_ts_recent. This test is 25773 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25774 * Extensions for High Performance: An Update", Internet Draft. 25775 */ 25776 if (tcp->tcp_snd_ts_ok && 25777 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25778 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25779 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25780 tcp->tcp_last_rcv_lbolt = lbolt64; 25781 } 25782 25783 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25784 /* Always ack out of order packets */ 25785 flags |= TH_ACK_NEEDED; 25786 seg_len = 0; 25787 } else if (seg_len > 0) { 25788 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25789 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25790 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25791 } 25792 if (flags & TH_RST) { 25793 (void) tcp_clean_death(tcp, 0, 28); 25794 goto done; 25795 } 25796 if (flags & TH_SYN) { 25797 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25798 TH_RST|TH_ACK); 25799 /* 25800 * Do not delete the TCP structure if it is in 25801 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25802 */ 25803 goto done; 25804 } 25805 process_ack: 25806 if (flags & TH_ACK) { 25807 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25808 if (bytes_acked <= 0) { 25809 if (bytes_acked == 0 && seg_len == 0 && 25810 new_swnd == tcp->tcp_swnd) 25811 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25812 } else { 25813 /* Acks something not sent */ 25814 flags |= TH_ACK_NEEDED; 25815 } 25816 } 25817 if (flags & TH_ACK_NEEDED) { 25818 /* 25819 * Time to send an ack for some reason. 25820 */ 25821 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25822 tcp->tcp_rnxt, TH_ACK); 25823 } 25824 done: 25825 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25826 DB_CKSUMSTART(mp) = 0; 25827 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25828 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25829 } 25830 freemsg(mp); 25831 } 25832 25833 /* 25834 * TCP Timers Implementation. 25835 */ 25836 timeout_id_t 25837 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25838 { 25839 mblk_t *mp; 25840 tcp_timer_t *tcpt; 25841 tcp_t *tcp = connp->conn_tcp; 25842 25843 ASSERT(connp->conn_sqp != NULL); 25844 25845 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25846 25847 if (tcp->tcp_timercache == NULL) { 25848 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25849 } else { 25850 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25851 mp = tcp->tcp_timercache; 25852 tcp->tcp_timercache = mp->b_next; 25853 mp->b_next = NULL; 25854 ASSERT(mp->b_wptr == NULL); 25855 } 25856 25857 CONN_INC_REF(connp); 25858 tcpt = (tcp_timer_t *)mp->b_rptr; 25859 tcpt->connp = connp; 25860 tcpt->tcpt_proc = f; 25861 /* 25862 * TCP timers are normal timeouts. Plus, they do not require more than 25863 * a 10 millisecond resolution. By choosing a coarser resolution and by 25864 * rounding up the expiration to the next resolution boundary, we can 25865 * batch timers in the callout subsystem to make TCP timers more 25866 * efficient. The roundup also protects short timers from expiring too 25867 * early before they have a chance to be cancelled. 25868 */ 25869 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25870 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25871 25872 return ((timeout_id_t)mp); 25873 } 25874 25875 static void 25876 tcp_timer_callback(void *arg) 25877 { 25878 mblk_t *mp = (mblk_t *)arg; 25879 tcp_timer_t *tcpt; 25880 conn_t *connp; 25881 25882 tcpt = (tcp_timer_t *)mp->b_rptr; 25883 connp = tcpt->connp; 25884 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25885 SQ_FILL, SQTAG_TCP_TIMER); 25886 } 25887 25888 static void 25889 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25890 { 25891 tcp_timer_t *tcpt; 25892 conn_t *connp = (conn_t *)arg; 25893 tcp_t *tcp = connp->conn_tcp; 25894 25895 tcpt = (tcp_timer_t *)mp->b_rptr; 25896 ASSERT(connp == tcpt->connp); 25897 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25898 25899 /* 25900 * If the TCP has reached the closed state, don't proceed any 25901 * further. This TCP logically does not exist on the system. 25902 * tcpt_proc could for example access queues, that have already 25903 * been qprocoff'ed off. Also see comments at the start of tcp_input 25904 */ 25905 if (tcp->tcp_state != TCPS_CLOSED) { 25906 (*tcpt->tcpt_proc)(connp); 25907 } else { 25908 tcp->tcp_timer_tid = 0; 25909 } 25910 tcp_timer_free(connp->conn_tcp, mp); 25911 } 25912 25913 /* 25914 * There is potential race with untimeout and the handler firing at the same 25915 * time. The mblock may be freed by the handler while we are trying to use 25916 * it. But since both should execute on the same squeue, this race should not 25917 * occur. 25918 */ 25919 clock_t 25920 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25921 { 25922 mblk_t *mp = (mblk_t *)id; 25923 tcp_timer_t *tcpt; 25924 clock_t delta; 25925 25926 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25927 25928 if (mp == NULL) 25929 return (-1); 25930 25931 tcpt = (tcp_timer_t *)mp->b_rptr; 25932 ASSERT(tcpt->connp == connp); 25933 25934 delta = untimeout_default(tcpt->tcpt_tid, 0); 25935 25936 if (delta >= 0) { 25937 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25938 tcp_timer_free(connp->conn_tcp, mp); 25939 CONN_DEC_REF(connp); 25940 } 25941 25942 return (delta); 25943 } 25944 25945 /* 25946 * Allocate space for the timer event. The allocation looks like mblk, but it is 25947 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25948 * 25949 * Dealing with failures: If we can't allocate from the timer cache we try 25950 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25951 * points to b_rptr. 25952 * If we can't allocate anything using allocb_tryhard(), we perform a last 25953 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25954 * save the actual allocation size in b_datap. 25955 */ 25956 mblk_t * 25957 tcp_timermp_alloc(int kmflags) 25958 { 25959 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25960 kmflags & ~KM_PANIC); 25961 25962 if (mp != NULL) { 25963 mp->b_next = mp->b_prev = NULL; 25964 mp->b_rptr = (uchar_t *)(&mp[1]); 25965 mp->b_wptr = NULL; 25966 mp->b_datap = NULL; 25967 mp->b_queue = NULL; 25968 mp->b_cont = NULL; 25969 } else if (kmflags & KM_PANIC) { 25970 /* 25971 * Failed to allocate memory for the timer. Try allocating from 25972 * dblock caches. 25973 */ 25974 /* ipclassifier calls this from a constructor - hence no tcps */ 25975 TCP_G_STAT(tcp_timermp_allocfail); 25976 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25977 if (mp == NULL) { 25978 size_t size = 0; 25979 /* 25980 * Memory is really low. Try tryhard allocation. 25981 * 25982 * ipclassifier calls this from a constructor - 25983 * hence no tcps 25984 */ 25985 TCP_G_STAT(tcp_timermp_allocdblfail); 25986 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25987 sizeof (tcp_timer_t), &size, kmflags); 25988 mp->b_rptr = (uchar_t *)(&mp[1]); 25989 mp->b_next = mp->b_prev = NULL; 25990 mp->b_wptr = (uchar_t *)-1; 25991 mp->b_datap = (dblk_t *)size; 25992 mp->b_queue = NULL; 25993 mp->b_cont = NULL; 25994 } 25995 ASSERT(mp->b_wptr != NULL); 25996 } 25997 /* ipclassifier calls this from a constructor - hence no tcps */ 25998 TCP_G_DBGSTAT(tcp_timermp_alloced); 25999 26000 return (mp); 26001 } 26002 26003 /* 26004 * Free per-tcp timer cache. 26005 * It can only contain entries from tcp_timercache. 26006 */ 26007 void 26008 tcp_timermp_free(tcp_t *tcp) 26009 { 26010 mblk_t *mp; 26011 26012 while ((mp = tcp->tcp_timercache) != NULL) { 26013 ASSERT(mp->b_wptr == NULL); 26014 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26015 kmem_cache_free(tcp_timercache, mp); 26016 } 26017 } 26018 26019 /* 26020 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26021 * events there already (currently at most two events are cached). 26022 * If the event is not allocated from the timer cache, free it right away. 26023 */ 26024 static void 26025 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26026 { 26027 mblk_t *mp1 = tcp->tcp_timercache; 26028 26029 if (mp->b_wptr != NULL) { 26030 /* 26031 * This allocation is not from a timer cache, free it right 26032 * away. 26033 */ 26034 if (mp->b_wptr != (uchar_t *)-1) 26035 freeb(mp); 26036 else 26037 kmem_free(mp, (size_t)mp->b_datap); 26038 } else if (mp1 == NULL || mp1->b_next == NULL) { 26039 /* Cache this timer block for future allocations */ 26040 mp->b_rptr = (uchar_t *)(&mp[1]); 26041 mp->b_next = mp1; 26042 tcp->tcp_timercache = mp; 26043 } else { 26044 kmem_cache_free(tcp_timercache, mp); 26045 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 26046 } 26047 } 26048 26049 /* 26050 * End of TCP Timers implementation. 26051 */ 26052 26053 /* 26054 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26055 * on the specified backing STREAMS q. Note, the caller may make the 26056 * decision to call based on the tcp_t.tcp_flow_stopped value which 26057 * when check outside the q's lock is only an advisory check ... 26058 */ 26059 void 26060 tcp_setqfull(tcp_t *tcp) 26061 { 26062 tcp_stack_t *tcps = tcp->tcp_tcps; 26063 conn_t *connp = tcp->tcp_connp; 26064 26065 if (tcp->tcp_closed) 26066 return; 26067 26068 if (IPCL_IS_NONSTR(connp)) { 26069 (*connp->conn_upcalls->su_txq_full) 26070 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 26071 tcp->tcp_flow_stopped = B_TRUE; 26072 } else { 26073 queue_t *q = tcp->tcp_wq; 26074 26075 if (!(q->q_flag & QFULL)) { 26076 mutex_enter(QLOCK(q)); 26077 if (!(q->q_flag & QFULL)) { 26078 /* still need to set QFULL */ 26079 q->q_flag |= QFULL; 26080 tcp->tcp_flow_stopped = B_TRUE; 26081 mutex_exit(QLOCK(q)); 26082 TCP_STAT(tcps, tcp_flwctl_on); 26083 } else { 26084 mutex_exit(QLOCK(q)); 26085 } 26086 } 26087 } 26088 } 26089 26090 void 26091 tcp_clrqfull(tcp_t *tcp) 26092 { 26093 conn_t *connp = tcp->tcp_connp; 26094 26095 if (tcp->tcp_closed) 26096 return; 26097 26098 if (IPCL_IS_NONSTR(connp)) { 26099 (*connp->conn_upcalls->su_txq_full) 26100 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 26101 tcp->tcp_flow_stopped = B_FALSE; 26102 } else { 26103 queue_t *q = tcp->tcp_wq; 26104 26105 if (q->q_flag & QFULL) { 26106 mutex_enter(QLOCK(q)); 26107 if (q->q_flag & QFULL) { 26108 q->q_flag &= ~QFULL; 26109 tcp->tcp_flow_stopped = B_FALSE; 26110 mutex_exit(QLOCK(q)); 26111 if (q->q_flag & QWANTW) 26112 qbackenable(q, 0); 26113 } else { 26114 mutex_exit(QLOCK(q)); 26115 } 26116 } 26117 } 26118 } 26119 26120 /* 26121 * kstats related to squeues i.e. not per IP instance 26122 */ 26123 static void * 26124 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26125 { 26126 kstat_t *ksp; 26127 26128 tcp_g_stat_t template = { 26129 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26130 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26131 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26132 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26133 }; 26134 26135 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26136 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26137 KSTAT_FLAG_VIRTUAL); 26138 26139 if (ksp == NULL) 26140 return (NULL); 26141 26142 bcopy(&template, tcp_g_statp, sizeof (template)); 26143 ksp->ks_data = (void *)tcp_g_statp; 26144 26145 kstat_install(ksp); 26146 return (ksp); 26147 } 26148 26149 static void 26150 tcp_g_kstat_fini(kstat_t *ksp) 26151 { 26152 if (ksp != NULL) { 26153 kstat_delete(ksp); 26154 } 26155 } 26156 26157 26158 static void * 26159 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26160 { 26161 kstat_t *ksp; 26162 26163 tcp_stat_t template = { 26164 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26165 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26166 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26167 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26168 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26169 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26170 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26171 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26172 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26173 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26174 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26175 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26176 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26177 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26178 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26179 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26180 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26181 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26182 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26183 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26184 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26185 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26186 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26187 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26188 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26189 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26190 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26191 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26192 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26193 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26194 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26195 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26196 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26197 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26198 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26199 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26200 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26201 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26202 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26203 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26204 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26205 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26206 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26207 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26208 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26209 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26210 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26211 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26212 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26213 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26214 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26215 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26216 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26217 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26218 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26219 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26220 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26221 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26222 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26223 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26224 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26225 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26226 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26227 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26228 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26229 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26230 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26231 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26232 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26233 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26234 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26235 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26236 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26237 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26238 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26239 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26240 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26241 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26242 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26243 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26244 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26245 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26246 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26247 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26248 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26249 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26250 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26251 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26252 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26253 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26254 }; 26255 26256 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26257 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26258 KSTAT_FLAG_VIRTUAL, stackid); 26259 26260 if (ksp == NULL) 26261 return (NULL); 26262 26263 bcopy(&template, tcps_statisticsp, sizeof (template)); 26264 ksp->ks_data = (void *)tcps_statisticsp; 26265 ksp->ks_private = (void *)(uintptr_t)stackid; 26266 26267 kstat_install(ksp); 26268 return (ksp); 26269 } 26270 26271 static void 26272 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26273 { 26274 if (ksp != NULL) { 26275 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26276 kstat_delete_netstack(ksp, stackid); 26277 } 26278 } 26279 26280 /* 26281 * TCP Kstats implementation 26282 */ 26283 static void * 26284 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26285 { 26286 kstat_t *ksp; 26287 26288 tcp_named_kstat_t template = { 26289 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26290 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26291 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26292 { "maxConn", KSTAT_DATA_INT32, 0 }, 26293 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26294 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26295 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26296 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26297 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26298 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26299 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26300 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26301 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26302 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26303 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26304 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26305 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26306 { "outAck", KSTAT_DATA_UINT32, 0 }, 26307 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26308 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26309 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26310 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26311 { "outControl", KSTAT_DATA_UINT32, 0 }, 26312 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26313 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26314 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26315 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26316 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26317 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26318 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26319 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26320 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26321 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26322 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26323 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26324 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26325 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26326 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26327 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26328 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26329 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26330 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26331 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26332 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26333 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26334 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26335 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26336 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26337 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26338 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26339 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26340 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26341 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26342 }; 26343 26344 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26345 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26346 26347 if (ksp == NULL) 26348 return (NULL); 26349 26350 template.rtoAlgorithm.value.ui32 = 4; 26351 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26352 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26353 template.maxConn.value.i32 = -1; 26354 26355 bcopy(&template, ksp->ks_data, sizeof (template)); 26356 ksp->ks_update = tcp_kstat_update; 26357 ksp->ks_private = (void *)(uintptr_t)stackid; 26358 26359 kstat_install(ksp); 26360 return (ksp); 26361 } 26362 26363 static void 26364 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26365 { 26366 if (ksp != NULL) { 26367 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26368 kstat_delete_netstack(ksp, stackid); 26369 } 26370 } 26371 26372 static int 26373 tcp_kstat_update(kstat_t *kp, int rw) 26374 { 26375 tcp_named_kstat_t *tcpkp; 26376 tcp_t *tcp; 26377 connf_t *connfp; 26378 conn_t *connp; 26379 int i; 26380 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26381 netstack_t *ns; 26382 tcp_stack_t *tcps; 26383 ip_stack_t *ipst; 26384 26385 if ((kp == NULL) || (kp->ks_data == NULL)) 26386 return (EIO); 26387 26388 if (rw == KSTAT_WRITE) 26389 return (EACCES); 26390 26391 ns = netstack_find_by_stackid(stackid); 26392 if (ns == NULL) 26393 return (-1); 26394 tcps = ns->netstack_tcp; 26395 if (tcps == NULL) { 26396 netstack_rele(ns); 26397 return (-1); 26398 } 26399 26400 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26401 26402 tcpkp->currEstab.value.ui32 = 0; 26403 26404 ipst = ns->netstack_ip; 26405 26406 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26407 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26408 connp = NULL; 26409 while ((connp = 26410 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26411 tcp = connp->conn_tcp; 26412 switch (tcp_snmp_state(tcp)) { 26413 case MIB2_TCP_established: 26414 case MIB2_TCP_closeWait: 26415 tcpkp->currEstab.value.ui32++; 26416 break; 26417 } 26418 } 26419 } 26420 26421 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26422 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26423 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26424 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26425 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26426 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26427 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26428 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26429 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26430 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26431 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26432 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26433 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26434 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26435 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26436 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26437 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26438 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26439 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26440 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26441 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26442 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26443 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26444 tcpkp->inDataInorderSegs.value.ui32 = 26445 tcps->tcps_mib.tcpInDataInorderSegs; 26446 tcpkp->inDataInorderBytes.value.ui32 = 26447 tcps->tcps_mib.tcpInDataInorderBytes; 26448 tcpkp->inDataUnorderSegs.value.ui32 = 26449 tcps->tcps_mib.tcpInDataUnorderSegs; 26450 tcpkp->inDataUnorderBytes.value.ui32 = 26451 tcps->tcps_mib.tcpInDataUnorderBytes; 26452 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26453 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26454 tcpkp->inDataPartDupSegs.value.ui32 = 26455 tcps->tcps_mib.tcpInDataPartDupSegs; 26456 tcpkp->inDataPartDupBytes.value.ui32 = 26457 tcps->tcps_mib.tcpInDataPartDupBytes; 26458 tcpkp->inDataPastWinSegs.value.ui32 = 26459 tcps->tcps_mib.tcpInDataPastWinSegs; 26460 tcpkp->inDataPastWinBytes.value.ui32 = 26461 tcps->tcps_mib.tcpInDataPastWinBytes; 26462 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26463 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26464 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26465 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26466 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26467 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26468 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26469 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26470 tcpkp->timKeepaliveProbe.value.ui32 = 26471 tcps->tcps_mib.tcpTimKeepaliveProbe; 26472 tcpkp->timKeepaliveDrop.value.ui32 = 26473 tcps->tcps_mib.tcpTimKeepaliveDrop; 26474 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26475 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26476 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26477 tcpkp->outSackRetransSegs.value.ui32 = 26478 tcps->tcps_mib.tcpOutSackRetransSegs; 26479 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26480 26481 netstack_rele(ns); 26482 return (0); 26483 } 26484 26485 void 26486 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26487 { 26488 uint16_t hdr_len; 26489 ipha_t *ipha; 26490 uint8_t *nexthdrp; 26491 tcph_t *tcph; 26492 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26493 26494 /* Already has an eager */ 26495 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26496 TCP_STAT(tcps, tcp_reinput_syn); 26497 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26498 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 26499 return; 26500 } 26501 26502 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26503 case IPV4_VERSION: 26504 ipha = (ipha_t *)mp->b_rptr; 26505 hdr_len = IPH_HDR_LENGTH(ipha); 26506 break; 26507 case IPV6_VERSION: 26508 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26509 &hdr_len, &nexthdrp)) { 26510 CONN_DEC_REF(connp); 26511 freemsg(mp); 26512 return; 26513 } 26514 break; 26515 } 26516 26517 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26518 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26519 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26520 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26521 } 26522 26523 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26524 SQ_FILL, SQTAG_TCP_REINPUT); 26525 } 26526 26527 static int 26528 tcp_squeue_switch(int val) 26529 { 26530 int rval = SQ_FILL; 26531 26532 switch (val) { 26533 case 1: 26534 rval = SQ_NODRAIN; 26535 break; 26536 case 2: 26537 rval = SQ_PROCESS; 26538 break; 26539 default: 26540 break; 26541 } 26542 return (rval); 26543 } 26544 26545 /* 26546 * This is called once for each squeue - globally for all stack 26547 * instances. 26548 */ 26549 static void 26550 tcp_squeue_add(squeue_t *sqp) 26551 { 26552 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26553 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26554 26555 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26556 tcp_time_wait->tcp_time_wait_tid = 26557 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26558 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26559 CALLOUT_FLAG_ROUNDUP); 26560 if (tcp_free_list_max_cnt == 0) { 26561 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26562 max_ncpus : boot_max_ncpus); 26563 26564 /* 26565 * Limit number of entries to 1% of availble memory / tcp_ncpus 26566 */ 26567 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26568 (tcp_ncpus * sizeof (tcp_t) * 100); 26569 } 26570 tcp_time_wait->tcp_free_list_cnt = 0; 26571 } 26572 26573 static int 26574 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error) 26575 { 26576 mblk_t *ire_mp = NULL; 26577 mblk_t *syn_mp; 26578 mblk_t *mdti; 26579 mblk_t *lsoi; 26580 int retval; 26581 tcph_t *tcph; 26582 uint32_t mss; 26583 queue_t *q = tcp->tcp_rq; 26584 conn_t *connp = tcp->tcp_connp; 26585 tcp_stack_t *tcps = tcp->tcp_tcps; 26586 26587 if (error == 0) { 26588 /* 26589 * Adapt Multidata information, if any. The 26590 * following tcp_mdt_update routine will free 26591 * the message. 26592 */ 26593 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26594 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26595 b_rptr)->mdt_capab, B_TRUE); 26596 freemsg(mdti); 26597 } 26598 26599 /* 26600 * Check to update LSO information with tcp, and 26601 * tcp_lso_update routine will free the message. 26602 */ 26603 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26604 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26605 b_rptr)->lso_capab); 26606 freemsg(lsoi); 26607 } 26608 26609 /* Get the IRE, if we had requested for it */ 26610 if (mp != NULL) 26611 ire_mp = tcp_ire_mp(&mp); 26612 26613 if (tcp->tcp_hard_binding) { 26614 tcp->tcp_hard_binding = B_FALSE; 26615 tcp->tcp_hard_bound = B_TRUE; 26616 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26617 if (retval != 0) { 26618 error = EADDRINUSE; 26619 goto bind_failed; 26620 } 26621 } else { 26622 if (ire_mp != NULL) 26623 freeb(ire_mp); 26624 goto after_syn_sent; 26625 } 26626 26627 retval = tcp_adapt_ire(tcp, ire_mp); 26628 if (ire_mp != NULL) 26629 freeb(ire_mp); 26630 if (retval == 0) { 26631 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26632 ENETUNREACH : EADDRNOTAVAIL); 26633 goto ipcl_rm; 26634 } 26635 /* 26636 * Don't let an endpoint connect to itself. 26637 * Also checked in tcp_connect() but that 26638 * check can't handle the case when the 26639 * local IP address is INADDR_ANY. 26640 */ 26641 if (tcp->tcp_ipversion == IPV4_VERSION) { 26642 if ((tcp->tcp_ipha->ipha_dst == 26643 tcp->tcp_ipha->ipha_src) && 26644 (BE16_EQL(tcp->tcp_tcph->th_lport, 26645 tcp->tcp_tcph->th_fport))) { 26646 error = EADDRNOTAVAIL; 26647 goto ipcl_rm; 26648 } 26649 } else { 26650 if (IN6_ARE_ADDR_EQUAL( 26651 &tcp->tcp_ip6h->ip6_dst, 26652 &tcp->tcp_ip6h->ip6_src) && 26653 (BE16_EQL(tcp->tcp_tcph->th_lport, 26654 tcp->tcp_tcph->th_fport))) { 26655 error = EADDRNOTAVAIL; 26656 goto ipcl_rm; 26657 } 26658 } 26659 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26660 /* 26661 * This should not be possible! Just for 26662 * defensive coding... 26663 */ 26664 if (tcp->tcp_state != TCPS_SYN_SENT) 26665 goto after_syn_sent; 26666 26667 if (is_system_labeled() && 26668 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26669 error = EHOSTUNREACH; 26670 goto ipcl_rm; 26671 } 26672 26673 /* 26674 * tcp_adapt_ire() does not adjust 26675 * for TCP/IP header length. 26676 */ 26677 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26678 26679 /* 26680 * Just make sure our rwnd is at 26681 * least tcp_recv_hiwat_mss * MSS 26682 * large, and round up to the nearest 26683 * MSS. 26684 * 26685 * We do the round up here because 26686 * we need to get the interface 26687 * MTU first before we can do the 26688 * round up. 26689 */ 26690 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26691 tcps->tcps_recv_hiwat_minmss * mss); 26692 if (!IPCL_IS_NONSTR(connp)) 26693 q->q_hiwat = tcp->tcp_rwnd; 26694 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26695 tcp_set_ws_value(tcp); 26696 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26697 tcp->tcp_tcph->th_win); 26698 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26699 tcp->tcp_snd_ws_ok = B_TRUE; 26700 26701 /* 26702 * Set tcp_snd_ts_ok to true 26703 * so that tcp_xmit_mp will 26704 * include the timestamp 26705 * option in the SYN segment. 26706 */ 26707 if (tcps->tcps_tstamp_always || 26708 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26709 tcp->tcp_snd_ts_ok = B_TRUE; 26710 } 26711 26712 /* 26713 * tcp_snd_sack_ok can be set in 26714 * tcp_adapt_ire() if the sack metric 26715 * is set. So check it here also. 26716 */ 26717 if (tcps->tcps_sack_permitted == 2 || 26718 tcp->tcp_snd_sack_ok) { 26719 if (tcp->tcp_sack_info == NULL) { 26720 tcp->tcp_sack_info = 26721 kmem_cache_alloc(tcp_sack_info_cache, 26722 KM_SLEEP); 26723 } 26724 tcp->tcp_snd_sack_ok = B_TRUE; 26725 } 26726 26727 /* 26728 * Should we use ECN? Note that the current 26729 * default value (SunOS 5.9) of tcp_ecn_permitted 26730 * is 1. The reason for doing this is that there 26731 * are equipments out there that will drop ECN 26732 * enabled IP packets. Setting it to 1 avoids 26733 * compatibility problems. 26734 */ 26735 if (tcps->tcps_ecn_permitted == 2) 26736 tcp->tcp_ecn_ok = B_TRUE; 26737 26738 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26739 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26740 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26741 if (syn_mp) { 26742 cred_t *cr; 26743 pid_t pid; 26744 26745 /* 26746 * Obtain the credential from the 26747 * thread calling connect(). 26748 * If none can be found, default to 26749 * the creator of the socket. 26750 */ 26751 if (mp == NULL || 26752 (cr = DB_CRED(mp)) == NULL) { 26753 cr = tcp->tcp_cred; 26754 pid = tcp->tcp_cpid; 26755 } else { 26756 pid = DB_CPID(mp); 26757 } 26758 26759 mblk_setcred(syn_mp, cr); 26760 DB_CPID(syn_mp) = pid; 26761 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26762 } 26763 after_syn_sent: 26764 /* 26765 * A trailer mblk indicates a waiting client upstream. 26766 * We complete here the processing begun in 26767 * either tcp_bind() or tcp_connect() by passing 26768 * upstream the reply message they supplied. 26769 */ 26770 if (mp != NULL) { 26771 ASSERT(mp->b_cont == NULL); 26772 freeb(mp); 26773 } 26774 return (error); 26775 } else { 26776 /* error */ 26777 if (tcp->tcp_debug) { 26778 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26779 "tcp_post_ip_bind: error == %d", error); 26780 } 26781 if (mp != NULL) { 26782 freeb(mp); 26783 } 26784 } 26785 26786 ipcl_rm: 26787 /* 26788 * Need to unbind with classifier since we were just 26789 * told that our bind succeeded. a.k.a error == 0 at the entry. 26790 */ 26791 tcp->tcp_hard_bound = B_FALSE; 26792 tcp->tcp_hard_binding = B_FALSE; 26793 26794 ipcl_hash_remove(connp); 26795 26796 bind_failed: 26797 tcp->tcp_state = TCPS_IDLE; 26798 if (tcp->tcp_ipversion == IPV4_VERSION) 26799 tcp->tcp_ipha->ipha_src = 0; 26800 else 26801 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26802 /* 26803 * Copy of the src addr. in tcp_t is needed since 26804 * the lookup funcs. can only look at tcp_t 26805 */ 26806 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26807 26808 tcph = tcp->tcp_tcph; 26809 tcph->th_lport[0] = 0; 26810 tcph->th_lport[1] = 0; 26811 tcp_bind_hash_remove(tcp); 26812 bzero(&connp->u_port, sizeof (connp->u_port)); 26813 /* blow away saved option results if any */ 26814 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26815 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26816 26817 conn_delete_ire(tcp->tcp_connp, NULL); 26818 26819 return (error); 26820 } 26821 26822 static int 26823 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26824 boolean_t bind_to_req_port_only, cred_t *cr) 26825 { 26826 in_port_t mlp_port; 26827 mlp_type_t addrtype, mlptype; 26828 boolean_t user_specified; 26829 in_port_t allocated_port; 26830 in_port_t requested_port = *requested_port_ptr; 26831 conn_t *connp; 26832 zone_t *zone; 26833 tcp_stack_t *tcps = tcp->tcp_tcps; 26834 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26835 26836 /* 26837 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26838 */ 26839 if (cr == NULL) 26840 cr = tcp->tcp_cred; 26841 /* 26842 * Get a valid port (within the anonymous range and should not 26843 * be a privileged one) to use if the user has not given a port. 26844 * If multiple threads are here, they may all start with 26845 * with the same initial port. But, it should be fine as long as 26846 * tcp_bindi will ensure that no two threads will be assigned 26847 * the same port. 26848 * 26849 * NOTE: XXX If a privileged process asks for an anonymous port, we 26850 * still check for ports only in the range > tcp_smallest_non_priv_port, 26851 * unless TCP_ANONPRIVBIND option is set. 26852 */ 26853 mlptype = mlptSingle; 26854 mlp_port = requested_port; 26855 if (requested_port == 0) { 26856 requested_port = tcp->tcp_anon_priv_bind ? 26857 tcp_get_next_priv_port(tcp) : 26858 tcp_update_next_port(tcps->tcps_next_port_to_try, 26859 tcp, B_TRUE); 26860 if (requested_port == 0) { 26861 return (-TNOADDR); 26862 } 26863 user_specified = B_FALSE; 26864 26865 /* 26866 * If the user went through one of the RPC interfaces to create 26867 * this socket and RPC is MLP in this zone, then give him an 26868 * anonymous MLP. 26869 */ 26870 connp = tcp->tcp_connp; 26871 if (connp->conn_anon_mlp && is_system_labeled()) { 26872 zone = crgetzone(cr); 26873 addrtype = tsol_mlp_addr_type(zone->zone_id, 26874 IPV6_VERSION, &v6addr, 26875 tcps->tcps_netstack->netstack_ip); 26876 if (addrtype == mlptSingle) { 26877 return (-TNOADDR); 26878 } 26879 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26880 PMAPPORT, addrtype); 26881 mlp_port = PMAPPORT; 26882 } 26883 } else { 26884 int i; 26885 boolean_t priv = B_FALSE; 26886 26887 /* 26888 * If the requested_port is in the well-known privileged range, 26889 * verify that the stream was opened by a privileged user. 26890 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26891 * but instead the code relies on: 26892 * - the fact that the address of the array and its size never 26893 * changes 26894 * - the atomic assignment of the elements of the array 26895 */ 26896 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26897 priv = B_TRUE; 26898 } else { 26899 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26900 if (requested_port == 26901 tcps->tcps_g_epriv_ports[i]) { 26902 priv = B_TRUE; 26903 break; 26904 } 26905 } 26906 } 26907 if (priv) { 26908 if (secpolicy_net_privaddr(cr, requested_port, 26909 IPPROTO_TCP) != 0) { 26910 if (tcp->tcp_debug) { 26911 (void) strlog(TCP_MOD_ID, 0, 1, 26912 SL_ERROR|SL_TRACE, 26913 "tcp_bind: no priv for port %d", 26914 requested_port); 26915 } 26916 return (-TACCES); 26917 } 26918 } 26919 user_specified = B_TRUE; 26920 26921 connp = tcp->tcp_connp; 26922 if (is_system_labeled()) { 26923 zone = crgetzone(cr); 26924 addrtype = tsol_mlp_addr_type(zone->zone_id, 26925 IPV6_VERSION, &v6addr, 26926 tcps->tcps_netstack->netstack_ip); 26927 if (addrtype == mlptSingle) { 26928 return (-TNOADDR); 26929 } 26930 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26931 requested_port, addrtype); 26932 } 26933 } 26934 26935 if (mlptype != mlptSingle) { 26936 if (secpolicy_net_bindmlp(cr) != 0) { 26937 if (tcp->tcp_debug) { 26938 (void) strlog(TCP_MOD_ID, 0, 1, 26939 SL_ERROR|SL_TRACE, 26940 "tcp_bind: no priv for multilevel port %d", 26941 requested_port); 26942 } 26943 return (-TACCES); 26944 } 26945 26946 /* 26947 * If we're specifically binding a shared IP address and the 26948 * port is MLP on shared addresses, then check to see if this 26949 * zone actually owns the MLP. Reject if not. 26950 */ 26951 if (mlptype == mlptShared && addrtype == mlptShared) { 26952 /* 26953 * No need to handle exclusive-stack zones since 26954 * ALL_ZONES only applies to the shared stack. 26955 */ 26956 zoneid_t mlpzone; 26957 26958 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26959 htons(mlp_port)); 26960 if (connp->conn_zoneid != mlpzone) { 26961 if (tcp->tcp_debug) { 26962 (void) strlog(TCP_MOD_ID, 0, 1, 26963 SL_ERROR|SL_TRACE, 26964 "tcp_bind: attempt to bind port " 26965 "%d on shared addr in zone %d " 26966 "(should be %d)", 26967 mlp_port, connp->conn_zoneid, 26968 mlpzone); 26969 } 26970 return (-TACCES); 26971 } 26972 } 26973 26974 if (!user_specified) { 26975 int err; 26976 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26977 requested_port, B_TRUE); 26978 if (err != 0) { 26979 if (tcp->tcp_debug) { 26980 (void) strlog(TCP_MOD_ID, 0, 1, 26981 SL_ERROR|SL_TRACE, 26982 "tcp_bind: cannot establish anon " 26983 "MLP for port %d", 26984 requested_port); 26985 } 26986 return (err); 26987 } 26988 connp->conn_anon_port = B_TRUE; 26989 } 26990 connp->conn_mlp_type = mlptype; 26991 } 26992 26993 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26994 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26995 26996 if (allocated_port == 0) { 26997 connp->conn_mlp_type = mlptSingle; 26998 if (connp->conn_anon_port) { 26999 connp->conn_anon_port = B_FALSE; 27000 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27001 requested_port, B_FALSE); 27002 } 27003 if (bind_to_req_port_only) { 27004 if (tcp->tcp_debug) { 27005 (void) strlog(TCP_MOD_ID, 0, 1, 27006 SL_ERROR|SL_TRACE, 27007 "tcp_bind: requested addr busy"); 27008 } 27009 return (-TADDRBUSY); 27010 } else { 27011 /* If we are out of ports, fail the bind. */ 27012 if (tcp->tcp_debug) { 27013 (void) strlog(TCP_MOD_ID, 0, 1, 27014 SL_ERROR|SL_TRACE, 27015 "tcp_bind: out of ports?"); 27016 } 27017 return (-TNOADDR); 27018 } 27019 } 27020 27021 /* Pass the allocated port back */ 27022 *requested_port_ptr = allocated_port; 27023 return (0); 27024 } 27025 27026 static int 27027 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27028 boolean_t bind_to_req_port_only) 27029 { 27030 tcp_t *tcp = connp->conn_tcp; 27031 27032 sin_t *sin; 27033 sin6_t *sin6; 27034 sin6_t sin6addr; 27035 in_port_t requested_port; 27036 ipaddr_t v4addr; 27037 in6_addr_t v6addr; 27038 uint_t origipversion; 27039 int error = 0; 27040 27041 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 27042 27043 if (tcp->tcp_state == TCPS_BOUND) { 27044 return (0); 27045 } else if (tcp->tcp_state > TCPS_BOUND) { 27046 if (tcp->tcp_debug) { 27047 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27048 "tcp_bind: bad state, %d", tcp->tcp_state); 27049 } 27050 return (-TOUTSTATE); 27051 } 27052 origipversion = tcp->tcp_ipversion; 27053 27054 if (sa != NULL && !OK_32PTR((char *)sa)) { 27055 if (tcp->tcp_debug) { 27056 (void) strlog(TCP_MOD_ID, 0, 1, 27057 SL_ERROR|SL_TRACE, 27058 "tcp_bind: bad address parameter, " 27059 "address %p, len %d", 27060 (void *)sa, len); 27061 } 27062 return (-TPROTO); 27063 } 27064 27065 switch (len) { 27066 case 0: /* request for a generic port */ 27067 if (tcp->tcp_family == AF_INET) { 27068 sin = (sin_t *)&sin6addr; 27069 *sin = sin_null; 27070 sin->sin_family = AF_INET; 27071 tcp->tcp_ipversion = IPV4_VERSION; 27072 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 27073 } else { 27074 ASSERT(tcp->tcp_family == AF_INET6); 27075 sin6 = (sin6_t *)&sin6addr; 27076 *sin6 = sin6_null; 27077 sin6->sin6_family = AF_INET6; 27078 tcp->tcp_ipversion = IPV6_VERSION; 27079 V6_SET_ZERO(v6addr); 27080 } 27081 requested_port = 0; 27082 break; 27083 27084 case sizeof (sin_t): /* Complete IPv4 address */ 27085 sin = (sin_t *)sa; 27086 /* 27087 * With sockets sockfs will accept bogus sin_family in 27088 * bind() and replace it with the family used in the socket 27089 * call. 27090 */ 27091 if (sin->sin_family != AF_INET || 27092 tcp->tcp_family != AF_INET) { 27093 return (EAFNOSUPPORT); 27094 } 27095 requested_port = ntohs(sin->sin_port); 27096 tcp->tcp_ipversion = IPV4_VERSION; 27097 v4addr = sin->sin_addr.s_addr; 27098 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 27099 break; 27100 27101 case sizeof (sin6_t): /* Complete IPv6 address */ 27102 sin6 = (sin6_t *)sa; 27103 if (sin6->sin6_family != AF_INET6 || 27104 tcp->tcp_family != AF_INET6) { 27105 return (EAFNOSUPPORT); 27106 } 27107 requested_port = ntohs(sin6->sin6_port); 27108 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 27109 IPV4_VERSION : IPV6_VERSION; 27110 v6addr = sin6->sin6_addr; 27111 break; 27112 27113 default: 27114 if (tcp->tcp_debug) { 27115 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27116 "tcp_bind: bad address length, %d", len); 27117 } 27118 return (EAFNOSUPPORT); 27119 /* return (-TBADADDR); */ 27120 } 27121 27122 tcp->tcp_bound_source_v6 = v6addr; 27123 27124 /* Check for change in ipversion */ 27125 if (origipversion != tcp->tcp_ipversion) { 27126 ASSERT(tcp->tcp_family == AF_INET6); 27127 error = tcp->tcp_ipversion == IPV6_VERSION ? 27128 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 27129 if (error) { 27130 return (ENOMEM); 27131 } 27132 } 27133 27134 /* 27135 * Initialize family specific fields. Copy of the src addr. 27136 * in tcp_t is needed for the lookup funcs. 27137 */ 27138 if (tcp->tcp_ipversion == IPV6_VERSION) { 27139 tcp->tcp_ip6h->ip6_src = v6addr; 27140 } else { 27141 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 27142 } 27143 tcp->tcp_ip_src_v6 = v6addr; 27144 27145 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 27146 27147 error = tcp_bind_select_lport(tcp, &requested_port, 27148 bind_to_req_port_only, cr); 27149 27150 return (error); 27151 } 27152 27153 /* 27154 * Return unix error is tli error is TSYSERR, otherwise return a negative 27155 * tli error. 27156 */ 27157 int 27158 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27159 boolean_t bind_to_req_port_only) 27160 { 27161 int error; 27162 tcp_t *tcp = connp->conn_tcp; 27163 27164 if (tcp->tcp_state >= TCPS_BOUND) { 27165 if (tcp->tcp_debug) { 27166 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27167 "tcp_bind: bad state, %d", tcp->tcp_state); 27168 } 27169 return (-TOUTSTATE); 27170 } 27171 27172 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 27173 if (error != 0) 27174 return (error); 27175 27176 ASSERT(tcp->tcp_state == TCPS_BOUND); 27177 27178 tcp->tcp_conn_req_max = 0; 27179 27180 /* 27181 * We need to make sure that the conn_recv is set to a non-null 27182 * value before we insert the conn into the classifier table. 27183 * This is to avoid a race with an incoming packet which does an 27184 * ipcl_classify(). 27185 */ 27186 connp->conn_recv = tcp_conn_request; 27187 27188 if (tcp->tcp_family == AF_INET6) { 27189 ASSERT(tcp->tcp_connp->conn_af_isv6); 27190 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27191 &tcp->tcp_bound_source_v6, 0, B_FALSE); 27192 } else { 27193 ASSERT(!tcp->tcp_connp->conn_af_isv6); 27194 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 27195 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 27196 } 27197 return (tcp_post_ip_bind(tcp, NULL, error)); 27198 } 27199 27200 int 27201 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 27202 socklen_t len, cred_t *cr) 27203 { 27204 int error; 27205 conn_t *connp = (conn_t *)proto_handle; 27206 squeue_t *sqp = connp->conn_sqp; 27207 27208 ASSERT(sqp != NULL); 27209 27210 error = squeue_synch_enter(sqp, connp, 0); 27211 if (error != 0) { 27212 /* failed to enter */ 27213 return (ENOSR); 27214 } 27215 27216 /* binding to a NULL address really means unbind */ 27217 if (sa == NULL) { 27218 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 27219 error = tcp_do_unbind(connp); 27220 else 27221 error = EINVAL; 27222 } else { 27223 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 27224 } 27225 27226 squeue_synch_exit(sqp, connp); 27227 27228 if (error < 0) { 27229 if (error == -TOUTSTATE) 27230 error = EINVAL; 27231 else 27232 error = proto_tlitosyserr(-error); 27233 } 27234 27235 return (error); 27236 } 27237 27238 /* 27239 * If the return value from this function is positive, it's a UNIX error. 27240 * Otherwise, if it's negative, then the absolute value is a TLI error. 27241 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 27242 */ 27243 int 27244 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 27245 cred_t *cr, pid_t pid) 27246 { 27247 tcp_t *tcp = connp->conn_tcp; 27248 sin_t *sin = (sin_t *)sa; 27249 sin6_t *sin6 = (sin6_t *)sa; 27250 ipaddr_t *dstaddrp; 27251 in_port_t dstport; 27252 uint_t srcid; 27253 int error = 0; 27254 27255 switch (len) { 27256 default: 27257 /* 27258 * Should never happen 27259 */ 27260 return (EINVAL); 27261 27262 case sizeof (sin_t): 27263 sin = (sin_t *)sa; 27264 if (sin->sin_port == 0) { 27265 return (-TBADADDR); 27266 } 27267 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 27268 return (EAFNOSUPPORT); 27269 } 27270 break; 27271 27272 case sizeof (sin6_t): 27273 sin6 = (sin6_t *)sa; 27274 if (sin6->sin6_port == 0) { 27275 return (-TBADADDR); 27276 } 27277 break; 27278 } 27279 /* 27280 * If we're connecting to an IPv4-mapped IPv6 address, we need to 27281 * make sure that the template IP header in the tcp structure is an 27282 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 27283 * need to this before we call tcp_bindi() so that the port lookup 27284 * code will look for ports in the correct port space (IPv4 and 27285 * IPv6 have separate port spaces). 27286 */ 27287 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 27288 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27289 int err = 0; 27290 27291 err = tcp_header_init_ipv4(tcp); 27292 if (err != 0) { 27293 error = ENOMEM; 27294 goto connect_failed; 27295 } 27296 if (tcp->tcp_lport != 0) 27297 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 27298 } 27299 27300 switch (tcp->tcp_state) { 27301 case TCPS_LISTEN: 27302 /* 27303 * Listening sockets are not allowed to issue connect(). 27304 */ 27305 if (IPCL_IS_NONSTR(connp)) 27306 return (EOPNOTSUPP); 27307 /* FALLTHRU */ 27308 case TCPS_IDLE: 27309 /* 27310 * We support quick connect, refer to comments in 27311 * tcp_connect_*() 27312 */ 27313 /* FALLTHRU */ 27314 case TCPS_BOUND: 27315 /* 27316 * We must bump the generation before the operation start. 27317 * This is done to ensure that any upcall made later on sends 27318 * up the right generation to the socket. 27319 */ 27320 SOCK_CONNID_BUMP(tcp->tcp_connid); 27321 27322 if (tcp->tcp_family == AF_INET6) { 27323 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27324 return (tcp_connect_ipv6(tcp, 27325 &sin6->sin6_addr, 27326 sin6->sin6_port, sin6->sin6_flowinfo, 27327 sin6->__sin6_src_id, sin6->sin6_scope_id, 27328 cr, pid)); 27329 } 27330 /* 27331 * Destination adress is mapped IPv6 address. 27332 * Source bound address should be unspecified or 27333 * IPv6 mapped address as well. 27334 */ 27335 if (!IN6_IS_ADDR_UNSPECIFIED( 27336 &tcp->tcp_bound_source_v6) && 27337 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 27338 return (EADDRNOTAVAIL); 27339 } 27340 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 27341 dstport = sin6->sin6_port; 27342 srcid = sin6->__sin6_src_id; 27343 } else { 27344 dstaddrp = &sin->sin_addr.s_addr; 27345 dstport = sin->sin_port; 27346 srcid = 0; 27347 } 27348 27349 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 27350 pid); 27351 break; 27352 default: 27353 return (-TOUTSTATE); 27354 } 27355 /* 27356 * Note: Code below is the "failure" case 27357 */ 27358 connect_failed: 27359 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 27360 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 27361 return (error); 27362 } 27363 27364 int 27365 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 27366 socklen_t len, sock_connid_t *id, cred_t *cr) 27367 { 27368 conn_t *connp = (conn_t *)proto_handle; 27369 tcp_t *tcp = connp->conn_tcp; 27370 squeue_t *sqp = connp->conn_sqp; 27371 int error; 27372 27373 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 27374 if (error != 0) { 27375 return (error); 27376 } 27377 27378 error = squeue_synch_enter(sqp, connp, 0); 27379 if (error != 0) { 27380 /* failed to enter */ 27381 return (ENOSR); 27382 } 27383 27384 /* 27385 * TCP supports quick connect, so no need to do an implicit bind 27386 */ 27387 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 27388 if (error == 0) { 27389 *id = connp->conn_tcp->tcp_connid; 27390 } else if (error < 0) { 27391 if (error == -TOUTSTATE) { 27392 switch (connp->conn_tcp->tcp_state) { 27393 case TCPS_SYN_SENT: 27394 error = EALREADY; 27395 break; 27396 case TCPS_ESTABLISHED: 27397 error = EISCONN; 27398 break; 27399 case TCPS_LISTEN: 27400 error = EOPNOTSUPP; 27401 break; 27402 default: 27403 error = EINVAL; 27404 break; 27405 } 27406 } else { 27407 error = proto_tlitosyserr(-error); 27408 } 27409 } 27410 done: 27411 squeue_synch_exit(sqp, connp); 27412 27413 return ((error == 0) ? EINPROGRESS : error); 27414 } 27415 27416 /* ARGSUSED */ 27417 sock_lower_handle_t 27418 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 27419 uint_t *smodep, int *errorp, int flags, cred_t *credp) 27420 { 27421 conn_t *connp; 27422 boolean_t isv6 = family == AF_INET6; 27423 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 27424 (proto != 0 && proto != IPPROTO_TCP)) { 27425 *errorp = EPROTONOSUPPORT; 27426 return (NULL); 27427 } 27428 27429 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 27430 if (connp == NULL) { 27431 return (NULL); 27432 } 27433 27434 /* 27435 * Put the ref for TCP. Ref for IP was already put 27436 * by ipcl_conn_create. Also Make the conn_t globally 27437 * visible to walkers 27438 */ 27439 mutex_enter(&connp->conn_lock); 27440 CONN_INC_REF_LOCKED(connp); 27441 ASSERT(connp->conn_ref == 2); 27442 connp->conn_state_flags &= ~CONN_INCIPIENT; 27443 27444 connp->conn_flags |= IPCL_NONSTR; 27445 mutex_exit(&connp->conn_lock); 27446 27447 ASSERT(errorp != NULL); 27448 *errorp = 0; 27449 *sock_downcalls = &sock_tcp_downcalls; 27450 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 27451 SM_SENDFILESUPP; 27452 27453 return ((sock_lower_handle_t)connp); 27454 } 27455 27456 /* ARGSUSED */ 27457 void 27458 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 27459 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 27460 { 27461 conn_t *connp = (conn_t *)proto_handle; 27462 struct sock_proto_props sopp; 27463 27464 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 27465 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 27466 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 27467 27468 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 27469 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 27470 sopp.sopp_maxpsz = INFPSZ; 27471 sopp.sopp_maxblk = INFPSZ; 27472 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 27473 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 27474 sopp.sopp_maxaddrlen = sizeof (sin6_t); 27475 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 27476 tcp_rinfo.mi_minpsz; 27477 27478 connp->conn_upcalls = sock_upcalls; 27479 connp->conn_upper_handle = sock_handle; 27480 27481 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 27482 } 27483 27484 /* ARGSUSED */ 27485 int 27486 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 27487 { 27488 conn_t *connp = (conn_t *)proto_handle; 27489 27490 tcp_close_common(connp, flags); 27491 27492 ip_close_helper_stream(connp); 27493 27494 /* 27495 * Drop IP's reference on the conn. This is the last reference 27496 * on the connp if the state was less than established. If the 27497 * connection has gone into timewait state, then we will have 27498 * one ref for the TCP and one more ref (total of two) for the 27499 * classifier connected hash list (a timewait connections stays 27500 * in connected hash till closed). 27501 * 27502 * We can't assert the references because there might be other 27503 * transient reference places because of some walkers or queued 27504 * packets in squeue for the timewait state. 27505 */ 27506 CONN_DEC_REF(connp); 27507 return (0); 27508 } 27509 27510 /* ARGSUSED */ 27511 int 27512 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 27513 cred_t *cr) 27514 { 27515 tcp_t *tcp; 27516 uint32_t msize; 27517 conn_t *connp = (conn_t *)proto_handle; 27518 int32_t tcpstate; 27519 27520 ASSERT(connp->conn_ref >= 2); 27521 27522 if (msg->msg_controllen != 0) { 27523 return (EOPNOTSUPP); 27524 27525 } 27526 switch (DB_TYPE(mp)) { 27527 case M_DATA: 27528 tcp = connp->conn_tcp; 27529 ASSERT(tcp != NULL); 27530 27531 tcpstate = tcp->tcp_state; 27532 if (tcpstate < TCPS_ESTABLISHED) { 27533 freemsg(mp); 27534 return (ENOTCONN); 27535 } else if (tcpstate > TCPS_CLOSE_WAIT) { 27536 freemsg(mp); 27537 return (EPIPE); 27538 } 27539 27540 if (is_system_labeled()) 27541 msg_setcredpid(mp, cr, curproc->p_pid); 27542 27543 /* XXX pass the size down and to the squeue */ 27544 msize = msgdsize(mp); 27545 27546 mutex_enter(&tcp->tcp_non_sq_lock); 27547 tcp->tcp_squeue_bytes += msize; 27548 /* 27549 * Squeue Flow Control 27550 */ 27551 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 27552 tcp_setqfull(tcp); 27553 } 27554 mutex_exit(&tcp->tcp_non_sq_lock); 27555 27556 /* 27557 * The application may pass in an address in the msghdr, but 27558 * we ignore the address on connection-oriented sockets. 27559 * Just like BSD this code does not generate an error for 27560 * TCP (a CONNREQUIRED socket) when sending to an address 27561 * passed in with sendto/sendmsg. Instead the data is 27562 * delivered on the connection as if no address had been 27563 * supplied. 27564 */ 27565 CONN_INC_REF(connp); 27566 27567 if (msg != NULL && msg->msg_flags & MSG_OOB) { 27568 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 27569 tcp_output_urgent, connp, tcp_squeue_flag, 27570 SQTAG_TCP_OUTPUT); 27571 } else { 27572 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 27573 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 27574 } 27575 27576 return (0); 27577 27578 default: 27579 ASSERT(0); 27580 } 27581 27582 freemsg(mp); 27583 return (0); 27584 } 27585 27586 /* ARGSUSED */ 27587 void 27588 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 27589 { 27590 int len; 27591 uint32_t msize; 27592 conn_t *connp = (conn_t *)arg; 27593 tcp_t *tcp = connp->conn_tcp; 27594 27595 msize = msgdsize(mp); 27596 27597 len = msize - 1; 27598 if (len < 0) { 27599 freemsg(mp); 27600 return; 27601 } 27602 27603 /* 27604 * Try to force urgent data out on the wire. 27605 * Even if we have unsent data this will 27606 * at least send the urgent flag. 27607 * XXX does not handle more flag correctly. 27608 */ 27609 len += tcp->tcp_unsent; 27610 len += tcp->tcp_snxt; 27611 tcp->tcp_urg = len; 27612 tcp->tcp_valid_bits |= TCP_URG_VALID; 27613 27614 /* Bypass tcp protocol for fused tcp loopback */ 27615 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27616 return; 27617 tcp_wput_data(tcp, mp, B_TRUE); 27618 } 27619 27620 /* ARGSUSED */ 27621 int 27622 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27623 socklen_t *addrlen, cred_t *cr) 27624 { 27625 sin_t *sin; 27626 sin6_t *sin6; 27627 conn_t *connp = (conn_t *)proto_handle; 27628 tcp_t *tcp = connp->conn_tcp; 27629 27630 ASSERT(tcp != NULL); 27631 if (tcp->tcp_state < TCPS_SYN_RCVD) 27632 return (ENOTCONN); 27633 27634 addr->sa_family = tcp->tcp_family; 27635 switch (tcp->tcp_family) { 27636 case AF_INET: 27637 if (*addrlen < sizeof (sin_t)) 27638 return (EINVAL); 27639 27640 sin = (sin_t *)addr; 27641 *sin = sin_null; 27642 sin->sin_family = AF_INET; 27643 if (tcp->tcp_ipversion == IPV4_VERSION) { 27644 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 27645 sin->sin_addr.s_addr); 27646 } 27647 sin->sin_port = tcp->tcp_fport; 27648 *addrlen = sizeof (struct sockaddr_in); 27649 break; 27650 case AF_INET6: 27651 sin6 = (sin6_t *)addr; 27652 *sin6 = sin6_null; 27653 sin6->sin6_family = AF_INET6; 27654 27655 if (*addrlen < sizeof (struct sockaddr_in6)) 27656 return (EINVAL); 27657 27658 if (tcp->tcp_ipversion == IPV6_VERSION) { 27659 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 27660 ~IPV6_VERS_AND_FLOW_MASK; 27661 } 27662 27663 sin6->sin6_addr = tcp->tcp_remote_v6; 27664 sin6->sin6_port = tcp->tcp_fport; 27665 *addrlen = sizeof (struct sockaddr_in6); 27666 break; 27667 } 27668 return (0); 27669 } 27670 27671 /* ARGSUSED */ 27672 int 27673 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27674 socklen_t *addrlenp, cred_t *cr) 27675 { 27676 sin_t *sin; 27677 sin6_t *sin6; 27678 conn_t *connp = (conn_t *)proto_handle; 27679 tcp_t *tcp = connp->conn_tcp; 27680 27681 switch (tcp->tcp_family) { 27682 case AF_INET: 27683 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 27684 if (*addrlenp < sizeof (sin_t)) 27685 return (EINVAL); 27686 sin = (sin_t *)addr; 27687 *sin = sin_null; 27688 sin->sin_family = AF_INET; 27689 *addrlenp = sizeof (sin_t); 27690 if (tcp->tcp_state >= TCPS_BOUND) { 27691 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 27692 sin->sin_port = tcp->tcp_lport; 27693 } 27694 break; 27695 27696 case AF_INET6: 27697 if (*addrlenp < sizeof (sin6_t)) 27698 return (EINVAL); 27699 sin6 = (sin6_t *)addr; 27700 *sin6 = sin6_null; 27701 sin6->sin6_family = AF_INET6; 27702 *addrlenp = sizeof (sin6_t); 27703 if (tcp->tcp_state >= TCPS_BOUND) { 27704 sin6->sin6_port = tcp->tcp_lport; 27705 if (tcp->tcp_ipversion == IPV4_VERSION) { 27706 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 27707 &sin6->sin6_addr); 27708 } else { 27709 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 27710 } 27711 } 27712 break; 27713 } 27714 return (0); 27715 } 27716 27717 /* 27718 * tcp_fallback 27719 * 27720 * A direct socket is falling back to using STREAMS. Hanging 27721 * off of the queue is a temporary tcp_t, which was created using 27722 * tcp_open(). The tcp_open() was called as part of the regular 27723 * sockfs create path, i.e., the SO_SOCKSTR flag is passed down, 27724 * and therefore the temporary tcp_t is marked to be a socket 27725 * (i.e., IPCL_SOCKET, tcp_issocket). So the optimizations 27726 * introduced by FireEngine will be used. 27727 * 27728 * The tcp_t associated with the socket falling back will 27729 * still be marked as a socket, although the direct socket flag 27730 * (IPCL_NONSTR) is removed. A fall back to true TPI semantics 27731 * will not take place until a _SIOCSOCKFALLBACK ioctl is issued. 27732 * 27733 * If the above mentioned behavior, i.e., the tmp tcp_t is created 27734 * as a STREAMS/TPI endpoint, then we will need to do more work here. 27735 * Such as inserting the direct socket into the acceptor hash. 27736 */ 27737 void 27738 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27739 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27740 { 27741 tcp_t *tcp, *eager; 27742 conn_t *connp = (conn_t *)proto_handle; 27743 int error; 27744 struct T_capability_ack tca; 27745 struct sockaddr_in6 laddr, faddr; 27746 socklen_t laddrlen, faddrlen; 27747 short opts; 27748 struct stroptions *stropt; 27749 mblk_t *stropt_mp; 27750 mblk_t *mp; 27751 mblk_t *conn_ind_head = NULL; 27752 mblk_t *conn_ind_tail = NULL; 27753 mblk_t *ordrel_mp; 27754 mblk_t *fused_sigurp_mp; 27755 27756 tcp = connp->conn_tcp; 27757 /* 27758 * No support for acceptor fallback 27759 */ 27760 ASSERT(q->q_qinfo != &tcp_acceptor_rinit); 27761 27762 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL); 27763 27764 /* Pre-allocate the T_ordrel_ind mblk. */ 27765 ASSERT(tcp->tcp_ordrel_mp == NULL); 27766 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27767 STR_NOSIG, NULL); 27768 ordrel_mp->b_datap->db_type = M_PROTO; 27769 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27770 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27771 27772 /* Pre-allocate the M_PCSIG anyway */ 27773 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27774 27775 /* 27776 * Enter the squeue so that no new packets can come in 27777 */ 27778 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27779 if (error != 0) { 27780 /* failed to enter, free all the pre-allocated messages. */ 27781 freeb(stropt_mp); 27782 freeb(ordrel_mp); 27783 freeb(fused_sigurp_mp); 27784 return; 27785 } 27786 27787 /* Disable I/OAT during fallback */ 27788 tcp->tcp_sodirect = NULL; 27789 27790 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27791 connp->conn_minor_arena = WR(q)->q_ptr; 27792 27793 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27794 27795 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27796 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27797 27798 WR(q)->q_qinfo = &tcp_sock_winit; 27799 27800 if (!direct_sockfs) 27801 tcp_disable_direct_sockfs(tcp); 27802 27803 /* 27804 * free the helper stream 27805 */ 27806 ip_close_helper_stream(connp); 27807 27808 /* 27809 * Notify the STREAM head about options 27810 */ 27811 DB_TYPE(stropt_mp) = M_SETOPTS; 27812 stropt = (struct stroptions *)stropt_mp->b_rptr; 27813 stropt_mp->b_wptr += sizeof (struct stroptions); 27814 stropt = (struct stroptions *)stropt_mp->b_rptr; 27815 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27816 27817 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27818 tcp->tcp_tcps->tcps_wroff_xtra); 27819 if (tcp->tcp_snd_sack_ok) 27820 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27821 stropt->so_hiwat = tcp->tcp_fused ? 27822 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27823 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27824 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27825 27826 putnext(RD(q), stropt_mp); 27827 27828 /* 27829 * Collect the information needed to sync with the sonode 27830 */ 27831 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27832 27833 laddrlen = faddrlen = sizeof (sin6_t); 27834 (void) tcp_getsockname(proto_handle, (struct sockaddr *)&laddr, 27835 &laddrlen, CRED()); 27836 error = tcp_getpeername(proto_handle, (struct sockaddr *)&faddr, 27837 &faddrlen, CRED()); 27838 if (error != 0) 27839 faddrlen = 0; 27840 27841 opts = 0; 27842 if (tcp->tcp_oobinline) 27843 opts |= SO_OOBINLINE; 27844 if (tcp->tcp_dontroute) 27845 opts |= SO_DONTROUTE; 27846 27847 /* 27848 * Notify the socket that the protocol is now quiescent, 27849 * and it's therefore safe move data from the socket 27850 * to the stream head. 27851 */ 27852 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27853 (struct sockaddr *)&laddr, laddrlen, 27854 (struct sockaddr *)&faddr, faddrlen, opts); 27855 27856 while ((mp = tcp->tcp_rcv_list) != NULL) { 27857 tcp->tcp_rcv_list = mp->b_next; 27858 mp->b_next = NULL; 27859 putnext(q, mp); 27860 } 27861 tcp->tcp_rcv_last_head = NULL; 27862 tcp->tcp_rcv_last_tail = NULL; 27863 tcp->tcp_rcv_cnt = 0; 27864 27865 /* 27866 * No longer a direct socket 27867 */ 27868 connp->conn_flags &= ~IPCL_NONSTR; 27869 27870 tcp->tcp_ordrel_mp = ordrel_mp; 27871 27872 if (tcp->tcp_fused) { 27873 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27874 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27875 } else { 27876 freeb(fused_sigurp_mp); 27877 } 27878 27879 /* 27880 * Send T_CONN_IND messages for all ESTABLISHED connections. 27881 */ 27882 mutex_enter(&tcp->tcp_eager_lock); 27883 for (eager = tcp->tcp_eager_next_q; eager != NULL; 27884 eager = eager->tcp_eager_next_q) { 27885 mp = eager->tcp_conn.tcp_eager_conn_ind; 27886 27887 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27888 ASSERT(mp != NULL); 27889 /* 27890 * TLI/XTI applications will get confused by 27891 * sending eager as an option since it violates 27892 * the option semantics. So remove the eager as 27893 * option since TLI/XTI app doesn't need it anyway. 27894 */ 27895 if (!TCP_IS_SOCKET(tcp)) { 27896 struct T_conn_ind *conn_ind; 27897 27898 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27899 conn_ind->OPT_length = 0; 27900 conn_ind->OPT_offset = 0; 27901 } 27902 if (conn_ind_head == NULL) { 27903 conn_ind_head = mp; 27904 } else { 27905 conn_ind_tail->b_next = mp; 27906 } 27907 conn_ind_tail = mp; 27908 } 27909 mutex_exit(&tcp->tcp_eager_lock); 27910 27911 mp = conn_ind_head; 27912 while (mp != NULL) { 27913 mblk_t *nmp = mp->b_next; 27914 mp->b_next = NULL; 27915 27916 putnext(tcp->tcp_rq, mp); 27917 mp = nmp; 27918 } 27919 27920 /* 27921 * There should be atleast two ref's (IP + TCP) 27922 */ 27923 ASSERT(connp->conn_ref >= 2); 27924 squeue_synch_exit(connp->conn_sqp, connp); 27925 } 27926 27927 /* ARGSUSED */ 27928 static void 27929 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27930 { 27931 conn_t *connp = (conn_t *)arg; 27932 tcp_t *tcp = connp->conn_tcp; 27933 27934 freemsg(mp); 27935 27936 if (tcp->tcp_fused) 27937 tcp_unfuse(tcp); 27938 27939 if (tcp_xmit_end(tcp) != 0) { 27940 /* 27941 * We were crossing FINs and got a reset from 27942 * the other side. Just ignore it. 27943 */ 27944 if (tcp->tcp_debug) { 27945 (void) strlog(TCP_MOD_ID, 0, 1, 27946 SL_ERROR|SL_TRACE, 27947 "tcp_shutdown_output() out of state %s", 27948 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27949 } 27950 } 27951 } 27952 27953 /* ARGSUSED */ 27954 int 27955 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27956 { 27957 conn_t *connp = (conn_t *)proto_handle; 27958 tcp_t *tcp = connp->conn_tcp; 27959 27960 /* 27961 * X/Open requires that we check the connected state. 27962 */ 27963 if (tcp->tcp_state < TCPS_SYN_SENT) 27964 return (ENOTCONN); 27965 27966 /* shutdown the send side */ 27967 if (how != SHUT_RD) { 27968 mblk_t *bp; 27969 27970 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27971 CONN_INC_REF(connp); 27972 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27973 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27974 27975 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27976 SOCK_OPCTL_SHUT_SEND, 0); 27977 } 27978 27979 /* shutdown the recv side */ 27980 if (how != SHUT_WR) 27981 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27982 SOCK_OPCTL_SHUT_RECV, 0); 27983 27984 return (0); 27985 } 27986 27987 /* 27988 * SOP_LISTEN() calls into tcp_listen(). 27989 */ 27990 /* ARGSUSED */ 27991 int 27992 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27993 { 27994 conn_t *connp = (conn_t *)proto_handle; 27995 int error; 27996 squeue_t *sqp = connp->conn_sqp; 27997 27998 error = squeue_synch_enter(sqp, connp, 0); 27999 if (error != 0) { 28000 /* failed to enter */ 28001 return (ENOBUFS); 28002 } 28003 28004 error = tcp_do_listen(connp, backlog, cr); 28005 if (error == 0) { 28006 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 28007 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 28008 } else if (error < 0) { 28009 if (error == -TOUTSTATE) 28010 error = EINVAL; 28011 else 28012 error = proto_tlitosyserr(-error); 28013 } 28014 squeue_synch_exit(sqp, connp); 28015 return (error); 28016 } 28017 28018 static int 28019 tcp_do_listen(conn_t *connp, int backlog, cred_t *cr) 28020 { 28021 tcp_t *tcp = connp->conn_tcp; 28022 sin_t *sin; 28023 sin6_t *sin6; 28024 int error = 0; 28025 tcp_stack_t *tcps = tcp->tcp_tcps; 28026 28027 if (tcp->tcp_state >= TCPS_BOUND) { 28028 if ((tcp->tcp_state == TCPS_BOUND || 28029 tcp->tcp_state == TCPS_LISTEN) && 28030 backlog > 0) { 28031 /* 28032 * Handle listen() increasing backlog. 28033 * This is more "liberal" then what the TPI spec 28034 * requires but is needed to avoid a t_unbind 28035 * when handling listen() since the port number 28036 * might be "stolen" between the unbind and bind. 28037 */ 28038 goto do_listen; 28039 } 28040 if (tcp->tcp_debug) { 28041 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 28042 "tcp_bind: bad state, %d", tcp->tcp_state); 28043 } 28044 return (-TOUTSTATE); 28045 } else { 28046 int32_t len; 28047 sin6_t addr; 28048 28049 /* Do an implicit bind: Request for a generic port. */ 28050 if (tcp->tcp_family == AF_INET) { 28051 len = sizeof (sin_t); 28052 sin = (sin_t *)&addr; 28053 *sin = sin_null; 28054 sin->sin_family = AF_INET; 28055 tcp->tcp_ipversion = IPV4_VERSION; 28056 } else { 28057 ASSERT(tcp->tcp_family == AF_INET6); 28058 len = sizeof (sin6_t); 28059 sin6 = (sin6_t *)&addr; 28060 *sin6 = sin6_null; 28061 sin6->sin6_family = AF_INET6; 28062 tcp->tcp_ipversion = IPV6_VERSION; 28063 } 28064 28065 error = tcp_bind_check(connp, (struct sockaddr *)&addr, len, 28066 cr, B_FALSE); 28067 if (error) 28068 return (error); 28069 /* Fall through and do the fanout insertion */ 28070 } 28071 28072 do_listen: 28073 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 28074 tcp->tcp_conn_req_max = backlog; 28075 if (tcp->tcp_conn_req_max) { 28076 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 28077 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 28078 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 28079 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 28080 /* 28081 * If this is a listener, do not reset the eager list 28082 * and other stuffs. Note that we don't check if the 28083 * existing eager list meets the new tcp_conn_req_max 28084 * requirement. 28085 */ 28086 if (tcp->tcp_state != TCPS_LISTEN) { 28087 tcp->tcp_state = TCPS_LISTEN; 28088 /* Initialize the chain. Don't need the eager_lock */ 28089 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 28090 tcp->tcp_eager_next_drop_q0 = tcp; 28091 tcp->tcp_eager_prev_drop_q0 = tcp; 28092 tcp->tcp_second_ctimer_threshold = 28093 tcps->tcps_ip_abort_linterval; 28094 } 28095 } 28096 28097 /* 28098 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 28099 * processing continues in tcp_rput_other(). 28100 * 28101 * We need to make sure that the conn_recv is set to a non-null 28102 * value before we insert the conn into the classifier table. 28103 * This is to avoid a race with an incoming packet which does an 28104 * ipcl_classify(). 28105 */ 28106 connp->conn_recv = tcp_conn_request; 28107 if (tcp->tcp_family == AF_INET) { 28108 error = ip_proto_bind_laddr_v4(connp, NULL, 28109 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 28110 } else { 28111 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 28112 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 28113 } 28114 return (tcp_post_ip_bind(tcp, NULL, error)); 28115 } 28116 28117 void 28118 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 28119 { 28120 conn_t *connp = (conn_t *)proto_handle; 28121 tcp_t *tcp = connp->conn_tcp; 28122 tcp_stack_t *tcps = tcp->tcp_tcps; 28123 uint_t thwin; 28124 28125 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 28126 28127 /* Flow control condition has been removed. */ 28128 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 28129 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 28130 << tcp->tcp_rcv_ws; 28131 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 28132 /* 28133 * Send back a window update immediately if TCP is above 28134 * ESTABLISHED state and the increase of the rcv window 28135 * that the other side knows is at least 1 MSS after flow 28136 * control is lifted. 28137 */ 28138 if (tcp->tcp_state >= TCPS_ESTABLISHED && 28139 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 28140 tcp_xmit_ctl(NULL, tcp, 28141 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 28142 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 28143 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 28144 } 28145 28146 squeue_synch_exit(connp->conn_sqp, connp); 28147 } 28148 28149 /* ARGSUSED */ 28150 int 28151 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 28152 int mode, int32_t *rvalp, cred_t *cr) 28153 { 28154 conn_t *connp = (conn_t *)proto_handle; 28155 int error; 28156 28157 switch (cmd) { 28158 case ND_SET: 28159 case ND_GET: 28160 case TCP_IOC_DEFAULT_Q: 28161 case _SIOCSOCKFALLBACK: 28162 case TCP_IOC_ABORT_CONN: 28163 case TI_GETPEERNAME: 28164 case TI_GETMYNAME: 28165 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 28166 cmd)); 28167 error = EINVAL; 28168 break; 28169 default: 28170 /* 28171 * Pass on to IP using helper stream 28172 */ 28173 error = ldi_ioctl( 28174 connp->conn_helper_info->ip_helper_stream_handle, 28175 cmd, arg, mode, cr, rvalp); 28176 break; 28177 } 28178 return (error); 28179 } 28180 28181 sock_downcalls_t sock_tcp_downcalls = { 28182 tcp_activate, 28183 tcp_accept, 28184 tcp_bind, 28185 tcp_listen, 28186 tcp_connect, 28187 tcp_getpeername, 28188 tcp_getsockname, 28189 tcp_getsockopt, 28190 tcp_setsockopt, 28191 tcp_sendmsg, 28192 NULL, 28193 NULL, 28194 NULL, 28195 tcp_shutdown, 28196 tcp_clr_flowctrl, 28197 tcp_ioctl, 28198 tcp_close, 28199 }; 28200