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 2009 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 #define _SUN_TPI_VERSION 2 35 #include <sys/tihdr.h> 36 #include <sys/timod.h> 37 #include <sys/ddi.h> 38 #include <sys/sunddi.h> 39 #include <sys/suntpi.h> 40 #include <sys/xti_inet.h> 41 #include <sys/cmn_err.h> 42 #include <sys/debug.h> 43 #include <sys/sdt.h> 44 #include <sys/vtrace.h> 45 #include <sys/kmem.h> 46 #include <sys/ethernet.h> 47 #include <sys/cpuvar.h> 48 #include <sys/dlpi.h> 49 #include <sys/multidata.h> 50 #include <sys/multidata_impl.h> 51 #include <sys/pattr.h> 52 #include <sys/policy.h> 53 #include <sys/priv.h> 54 #include <sys/zone.h> 55 #include <sys/sunldi.h> 56 57 #include <sys/errno.h> 58 #include <sys/signal.h> 59 #include <sys/socket.h> 60 #include <sys/socketvar.h> 61 #include <sys/sockio.h> 62 #include <sys/isa_defs.h> 63 #include <sys/md5.h> 64 #include <sys/random.h> 65 #include <sys/sodirect.h> 66 #include <sys/uio.h> 67 #include <sys/systm.h> 68 #include <netinet/in.h> 69 #include <netinet/tcp.h> 70 #include <netinet/ip6.h> 71 #include <netinet/icmp6.h> 72 #include <net/if.h> 73 #include <net/route.h> 74 #include <inet/ipsec_impl.h> 75 76 #include <inet/common.h> 77 #include <inet/ip.h> 78 #include <inet/ip_impl.h> 79 #include <inet/ip6.h> 80 #include <inet/ip_ndp.h> 81 #include <inet/proto_set.h> 82 #include <inet/mib2.h> 83 #include <inet/nd.h> 84 #include <inet/optcom.h> 85 #include <inet/snmpcom.h> 86 #include <inet/kstatcom.h> 87 #include <inet/tcp.h> 88 #include <inet/tcp_impl.h> 89 #include <inet/udp_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 }; 657 658 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 659 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 660 661 /* 662 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 663 */ 664 665 #ifdef _BIG_ENDIAN 666 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 667 (TCPOPT_TSTAMP << 8) | 10) 668 #else 669 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 670 (TCPOPT_NOP << 8) | TCPOPT_NOP) 671 #endif 672 673 /* 674 * Flags returned from tcp_parse_options. 675 */ 676 #define TCP_OPT_MSS_PRESENT 1 677 #define TCP_OPT_WSCALE_PRESENT 2 678 #define TCP_OPT_TSTAMP_PRESENT 4 679 #define TCP_OPT_SACK_OK_PRESENT 8 680 #define TCP_OPT_SACK_PRESENT 16 681 682 /* TCP option length */ 683 #define TCPOPT_NOP_LEN 1 684 #define TCPOPT_MAXSEG_LEN 4 685 #define TCPOPT_WS_LEN 3 686 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 687 #define TCPOPT_TSTAMP_LEN 10 688 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 689 #define TCPOPT_SACK_OK_LEN 2 690 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 691 #define TCPOPT_REAL_SACK_LEN 4 692 #define TCPOPT_MAX_SACK_LEN 36 693 #define TCPOPT_HEADER_LEN 2 694 695 /* TCP cwnd burst factor. */ 696 #define TCP_CWND_INFINITE 65535 697 #define TCP_CWND_SS 3 698 #define TCP_CWND_NORMAL 5 699 700 /* Maximum TCP initial cwin (start/restart). */ 701 #define TCP_MAX_INIT_CWND 8 702 703 /* 704 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 705 * either tcp_slow_start_initial or tcp_slow_start_after idle 706 * depending on the caller. If the upper layer has not used the 707 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 708 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 709 * If the upper layer has changed set the tcp_init_cwnd, just use 710 * it to calculate the tcp_cwnd. 711 */ 712 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 713 { \ 714 if ((tcp)->tcp_init_cwnd == 0) { \ 715 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 716 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 717 } else { \ 718 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 719 } \ 720 tcp->tcp_cwnd_cnt = 0; \ 721 } 722 723 /* TCP Timer control structure */ 724 typedef struct tcpt_s { 725 pfv_t tcpt_pfv; /* The routine we are to call */ 726 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 727 } tcpt_t; 728 729 /* Host Specific Parameter structure */ 730 typedef struct tcp_hsp { 731 struct tcp_hsp *tcp_hsp_next; 732 in6_addr_t tcp_hsp_addr_v6; 733 in6_addr_t tcp_hsp_subnet_v6; 734 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 735 int32_t tcp_hsp_sendspace; 736 int32_t tcp_hsp_recvspace; 737 int32_t tcp_hsp_tstamp; 738 } tcp_hsp_t; 739 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 740 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 741 742 /* 743 * Functions called directly via squeue having a prototype of edesc_t. 744 */ 745 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 746 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 747 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 748 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 749 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 750 void tcp_input(void *arg, mblk_t *mp, void *arg2); 751 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 752 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 753 void tcp_output(void *arg, mblk_t *mp, void *arg2); 754 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 755 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 756 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 757 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 758 759 760 /* Prototype for TCP functions */ 761 static void tcp_random_init(void); 762 int tcp_random(void); 763 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 764 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 765 tcp_t *eager); 766 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 767 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 768 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 769 boolean_t user_specified); 770 static void tcp_closei_local(tcp_t *tcp); 771 static void tcp_close_detached(tcp_t *tcp); 772 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 773 mblk_t *idmp, mblk_t **defermp); 774 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 775 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 776 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 777 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 778 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 779 uint32_t scope_id, cred_t *cr, pid_t pid); 780 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 781 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 782 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 783 static char *tcp_display(tcp_t *tcp, char *, char); 784 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 785 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 786 static void tcp_eager_unlink(tcp_t *tcp); 787 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 788 int unixerr); 789 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 790 int tlierr, int unixerr); 791 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 792 cred_t *cr); 793 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 794 char *value, caddr_t cp, cred_t *cr); 795 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 796 char *value, caddr_t cp, cred_t *cr); 797 static int tcp_tpistate(tcp_t *tcp); 798 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 799 int caller_holds_lock); 800 static void tcp_bind_hash_remove(tcp_t *tcp); 801 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 802 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 803 static void tcp_acceptor_hash_remove(tcp_t *tcp); 804 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 805 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 806 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 807 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 808 void tcp_g_q_setup(tcp_stack_t *); 809 void tcp_g_q_create(tcp_stack_t *); 810 void tcp_g_q_destroy(tcp_stack_t *); 811 static int tcp_header_init_ipv4(tcp_t *tcp); 812 static int tcp_header_init_ipv6(tcp_t *tcp); 813 int tcp_init(tcp_t *tcp, queue_t *q); 814 static int tcp_init_values(tcp_t *tcp); 815 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 816 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 817 static void tcp_ip_notify(tcp_t *tcp); 818 static mblk_t *tcp_ire_mp(mblk_t **mpp); 819 static void tcp_iss_init(tcp_t *tcp); 820 static void tcp_keepalive_killer(void *arg); 821 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 822 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 823 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 824 int *do_disconnectp, int *t_errorp, int *sys_errorp); 825 static boolean_t tcp_allow_connopt_set(int level, int name); 826 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 827 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 828 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 829 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 830 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 831 mblk_t *mblk); 832 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 833 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 834 uchar_t *ptr, uint_t len); 835 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 836 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 837 tcp_stack_t *); 838 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 839 caddr_t cp, cred_t *cr); 840 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 841 caddr_t cp, cred_t *cr); 842 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 843 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 844 caddr_t cp, cred_t *cr); 845 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 846 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 847 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 848 static void tcp_reinit(tcp_t *tcp); 849 static void tcp_reinit_values(tcp_t *tcp); 850 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 851 tcp_t *thisstream, cred_t *cr); 852 853 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 854 static uint_t tcp_rcv_drain(tcp_t *tcp); 855 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 856 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 857 static void tcp_ss_rexmit(tcp_t *tcp); 858 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 859 static void tcp_process_options(tcp_t *, tcph_t *); 860 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 861 static void tcp_rsrv(queue_t *q); 862 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 863 static int tcp_snmp_state(tcp_t *tcp); 864 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 865 cred_t *cr); 866 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 867 cred_t *cr); 868 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 869 cred_t *cr); 870 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 871 cred_t *cr); 872 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 873 cred_t *cr); 874 static void tcp_timer(void *arg); 875 static void tcp_timer_callback(void *); 876 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 877 boolean_t random); 878 static in_port_t tcp_get_next_priv_port(const tcp_t *); 879 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 880 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 881 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 882 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 883 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 884 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 885 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 886 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 887 const int num_sack_blk, int *usable, uint_t *snxt, 888 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 889 const int mdt_thres); 890 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 891 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 892 const int num_sack_blk, int *usable, uint_t *snxt, 893 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 894 const int mdt_thres); 895 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 896 int num_sack_blk); 897 static void tcp_wsrv(queue_t *q); 898 static int tcp_xmit_end(tcp_t *tcp); 899 static void tcp_ack_timer(void *arg); 900 static mblk_t *tcp_ack_mp(tcp_t *tcp); 901 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 902 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 903 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 904 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 905 uint32_t ack, int ctl); 906 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 907 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 908 static int setmaxps(queue_t *q, int maxpsz); 909 static void tcp_set_rto(tcp_t *, time_t); 910 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 911 boolean_t, boolean_t); 912 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 913 boolean_t ipsec_mctl); 914 static int tcp_build_hdrs(tcp_t *); 915 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 916 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 917 tcph_t *tcph); 918 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 919 static mblk_t *tcp_mdt_info_mp(mblk_t *); 920 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 921 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 922 const boolean_t, const uint32_t, const uint32_t, 923 const uint32_t, const uint32_t, tcp_stack_t *); 924 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 925 const uint_t, const uint_t, boolean_t *); 926 static mblk_t *tcp_lso_info_mp(mblk_t *); 927 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 928 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 929 extern mblk_t *tcp_timermp_alloc(int); 930 extern void tcp_timermp_free(tcp_t *); 931 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 932 static void tcp_stop_lingering(tcp_t *tcp); 933 static void tcp_close_linger_timeout(void *arg); 934 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 935 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 936 static void tcp_stack_fini(netstackid_t stackid, void *arg); 937 static void *tcp_g_kstat_init(tcp_g_stat_t *); 938 static void tcp_g_kstat_fini(kstat_t *); 939 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 940 static void tcp_kstat_fini(netstackid_t, kstat_t *); 941 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 942 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 943 static int tcp_kstat_update(kstat_t *kp, int rw); 944 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 945 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 946 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 947 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 948 tcph_t *tcph, mblk_t *idmp); 949 static int tcp_squeue_switch(int); 950 951 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 952 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 953 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 954 static int tcp_tpi_close(queue_t *, int); 955 static int tcpclose_accept(queue_t *); 956 957 static void tcp_squeue_add(squeue_t *); 958 static boolean_t tcp_zcopy_check(tcp_t *); 959 static void tcp_zcopy_notify(tcp_t *); 960 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 961 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 962 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 963 964 extern void tcp_kssl_input(tcp_t *, mblk_t *); 965 966 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 967 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 968 969 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 970 sock_upper_handle_t, cred_t *); 971 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 972 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 973 static int tcp_do_listen(conn_t *, int, cred_t *); 974 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 975 cred_t *, pid_t); 976 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 977 boolean_t); 978 static int tcp_do_unbind(conn_t *); 979 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 980 boolean_t); 981 982 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_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 cred_t *cr; 3057 3058 /* 3059 * All Solaris components should pass a db_credp 3060 * for this TPI message, hence we ASSERT. 3061 * But in case there is some other M_PROTO that looks 3062 * like a TPI message sent by some other kernel 3063 * component, we check and return an error. 3064 */ 3065 cr = msg_getcred(mp, NULL); 3066 ASSERT(cr != NULL); 3067 if (cr == NULL) { 3068 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3069 return; 3070 } 3071 3072 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3073 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3074 if (tcp->tcp_debug) { 3075 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3076 "tcp_tpi_bind: bad req, len %u", 3077 (uint_t)(mp->b_wptr - mp->b_rptr)); 3078 } 3079 tcp_err_ack(tcp, mp, TPROTO, 0); 3080 return; 3081 } 3082 /* Make sure the largest address fits */ 3083 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3084 if (mp1 == NULL) { 3085 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3086 return; 3087 } 3088 mp = mp1; 3089 tbr = (struct T_bind_req *)mp->b_rptr; 3090 3091 backlog = tbr->CONIND_number; 3092 len = tbr->ADDR_length; 3093 3094 switch (len) { 3095 case 0: /* request for a generic port */ 3096 tbr->ADDR_offset = sizeof (struct T_bind_req); 3097 if (tcp->tcp_family == AF_INET) { 3098 tbr->ADDR_length = sizeof (sin_t); 3099 sin = (sin_t *)&tbr[1]; 3100 *sin = sin_null; 3101 sin->sin_family = AF_INET; 3102 sa = (struct sockaddr *)sin; 3103 len = sizeof (sin_t); 3104 mp->b_wptr = (uchar_t *)&sin[1]; 3105 } else { 3106 ASSERT(tcp->tcp_family == AF_INET6); 3107 tbr->ADDR_length = sizeof (sin6_t); 3108 sin6 = (sin6_t *)&tbr[1]; 3109 *sin6 = sin6_null; 3110 sin6->sin6_family = AF_INET6; 3111 sa = (struct sockaddr *)sin6; 3112 len = sizeof (sin6_t); 3113 mp->b_wptr = (uchar_t *)&sin6[1]; 3114 } 3115 break; 3116 3117 case sizeof (sin_t): /* Complete IPv4 address */ 3118 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3119 sizeof (sin_t)); 3120 break; 3121 3122 case sizeof (sin6_t): /* Complete IPv6 address */ 3123 sa = (struct sockaddr *)mi_offset_param(mp, 3124 tbr->ADDR_offset, sizeof (sin6_t)); 3125 break; 3126 3127 default: 3128 if (tcp->tcp_debug) { 3129 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3130 "tcp_tpi_bind: bad address length, %d", 3131 tbr->ADDR_length); 3132 } 3133 tcp_err_ack(tcp, mp, TBADADDR, 0); 3134 return; 3135 } 3136 3137 error = tcp_bind_check(connp, sa, len, cr, 3138 tbr->PRIM_type != O_T_BIND_REQ); 3139 if (error == 0) { 3140 if (tcp->tcp_family == AF_INET) { 3141 sin = (sin_t *)sa; 3142 sin->sin_port = tcp->tcp_lport; 3143 } else { 3144 sin6 = (sin6_t *)sa; 3145 sin6->sin6_port = tcp->tcp_lport; 3146 } 3147 3148 if (backlog > 0) { 3149 error = tcp_do_listen(connp, backlog, cr); 3150 } 3151 } 3152 done: 3153 if (error > 0) { 3154 tcp_err_ack(tcp, mp, TSYSERR, error); 3155 } else if (error < 0) { 3156 tcp_err_ack(tcp, mp, -error, 0); 3157 } else { 3158 mp->b_datap->db_type = M_PCPROTO; 3159 tbr->PRIM_type = T_BIND_ACK; 3160 putnext(tcp->tcp_rq, mp); 3161 } 3162 } 3163 3164 /* 3165 * If the "bind_to_req_port_only" parameter is set, if the requested port 3166 * number is available, return it, If not return 0 3167 * 3168 * If "bind_to_req_port_only" parameter is not set and 3169 * If the requested port number is available, return it. If not, return 3170 * the first anonymous port we happen across. If no anonymous ports are 3171 * available, return 0. addr is the requested local address, if any. 3172 * 3173 * In either case, when succeeding update the tcp_t to record the port number 3174 * and insert it in the bind hash table. 3175 * 3176 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3177 * without setting SO_REUSEADDR. This is needed so that they 3178 * can be viewed as two independent transport protocols. 3179 */ 3180 static in_port_t 3181 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3182 int reuseaddr, boolean_t quick_connect, 3183 boolean_t bind_to_req_port_only, boolean_t user_specified) 3184 { 3185 /* number of times we have run around the loop */ 3186 int count = 0; 3187 /* maximum number of times to run around the loop */ 3188 int loopmax; 3189 conn_t *connp = tcp->tcp_connp; 3190 zoneid_t zoneid = connp->conn_zoneid; 3191 tcp_stack_t *tcps = tcp->tcp_tcps; 3192 3193 /* 3194 * Lookup for free addresses is done in a loop and "loopmax" 3195 * influences how long we spin in the loop 3196 */ 3197 if (bind_to_req_port_only) { 3198 /* 3199 * If the requested port is busy, don't bother to look 3200 * for a new one. Setting loop maximum count to 1 has 3201 * that effect. 3202 */ 3203 loopmax = 1; 3204 } else { 3205 /* 3206 * If the requested port is busy, look for a free one 3207 * in the anonymous port range. 3208 * Set loopmax appropriately so that one does not look 3209 * forever in the case all of the anonymous ports are in use. 3210 */ 3211 if (tcp->tcp_anon_priv_bind) { 3212 /* 3213 * loopmax = 3214 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3215 */ 3216 loopmax = IPPORT_RESERVED - 3217 tcps->tcps_min_anonpriv_port; 3218 } else { 3219 loopmax = (tcps->tcps_largest_anon_port - 3220 tcps->tcps_smallest_anon_port + 1); 3221 } 3222 } 3223 do { 3224 uint16_t lport; 3225 tf_t *tbf; 3226 tcp_t *ltcp; 3227 conn_t *lconnp; 3228 3229 lport = htons(port); 3230 3231 /* 3232 * Ensure that the tcp_t is not currently in the bind hash. 3233 * Hold the lock on the hash bucket to ensure that 3234 * the duplicate check plus the insertion is an atomic 3235 * operation. 3236 * 3237 * This function does an inline lookup on the bind hash list 3238 * Make sure that we access only members of tcp_t 3239 * and that we don't look at tcp_tcp, since we are not 3240 * doing a CONN_INC_REF. 3241 */ 3242 tcp_bind_hash_remove(tcp); 3243 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3244 mutex_enter(&tbf->tf_lock); 3245 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3246 ltcp = ltcp->tcp_bind_hash) { 3247 if (lport == ltcp->tcp_lport) 3248 break; 3249 } 3250 3251 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3252 boolean_t not_socket; 3253 boolean_t exclbind; 3254 3255 lconnp = ltcp->tcp_connp; 3256 3257 /* 3258 * On a labeled system, we must treat bindings to ports 3259 * on shared IP addresses by sockets with MAC exemption 3260 * privilege as being in all zones, as there's 3261 * otherwise no way to identify the right receiver. 3262 */ 3263 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3264 IPCL_ZONE_MATCH(connp, 3265 ltcp->tcp_connp->conn_zoneid)) && 3266 !lconnp->conn_mac_exempt && 3267 !connp->conn_mac_exempt) 3268 continue; 3269 3270 /* 3271 * If TCP_EXCLBIND is set for either the bound or 3272 * binding endpoint, the semantics of bind 3273 * is changed according to the following. 3274 * 3275 * spec = specified address (v4 or v6) 3276 * unspec = unspecified address (v4 or v6) 3277 * A = specified addresses are different for endpoints 3278 * 3279 * bound bind to allowed 3280 * ------------------------------------- 3281 * unspec unspec no 3282 * unspec spec no 3283 * spec unspec no 3284 * spec spec yes if A 3285 * 3286 * For labeled systems, SO_MAC_EXEMPT behaves the same 3287 * as TCP_EXCLBIND, except that zoneid is ignored. 3288 * 3289 * Note: 3290 * 3291 * 1. Because of TLI semantics, an endpoint can go 3292 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3293 * TCPS_BOUND, depending on whether it is originally 3294 * a listener or not. That is why we need to check 3295 * for states greater than or equal to TCPS_BOUND 3296 * here. 3297 * 3298 * 2. Ideally, we should only check for state equals 3299 * to TCPS_LISTEN. And the following check should be 3300 * added. 3301 * 3302 * if (ltcp->tcp_state == TCPS_LISTEN || 3303 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3304 * ... 3305 * } 3306 * 3307 * The semantics will be changed to this. If the 3308 * endpoint on the list is in state not equal to 3309 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3310 * set, let the bind succeed. 3311 * 3312 * Because of (1), we cannot do that for TLI 3313 * endpoints. But we can do that for socket endpoints. 3314 * If in future, we can change this going back 3315 * semantics, we can use the above check for TLI also. 3316 */ 3317 not_socket = !(TCP_IS_SOCKET(ltcp) && 3318 TCP_IS_SOCKET(tcp)); 3319 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3320 3321 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3322 (exclbind && (not_socket || 3323 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3324 if (V6_OR_V4_INADDR_ANY( 3325 ltcp->tcp_bound_source_v6) || 3326 V6_OR_V4_INADDR_ANY(*laddr) || 3327 IN6_ARE_ADDR_EQUAL(laddr, 3328 <cp->tcp_bound_source_v6)) { 3329 break; 3330 } 3331 continue; 3332 } 3333 3334 /* 3335 * Check ipversion to allow IPv4 and IPv6 sockets to 3336 * have disjoint port number spaces, if *_EXCLBIND 3337 * is not set and only if the application binds to a 3338 * specific port. We use the same autoassigned port 3339 * number space for IPv4 and IPv6 sockets. 3340 */ 3341 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3342 bind_to_req_port_only) 3343 continue; 3344 3345 /* 3346 * Ideally, we should make sure that the source 3347 * address, remote address, and remote port in the 3348 * four tuple for this tcp-connection is unique. 3349 * However, trying to find out the local source 3350 * address would require too much code duplication 3351 * with IP, since IP needs needs to have that code 3352 * to support userland TCP implementations. 3353 */ 3354 if (quick_connect && 3355 (ltcp->tcp_state > TCPS_LISTEN) && 3356 ((tcp->tcp_fport != ltcp->tcp_fport) || 3357 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3358 <cp->tcp_remote_v6))) 3359 continue; 3360 3361 if (!reuseaddr) { 3362 /* 3363 * No socket option SO_REUSEADDR. 3364 * If existing port is bound to 3365 * a non-wildcard IP address 3366 * and the requesting stream is 3367 * bound to a distinct 3368 * different IP addresses 3369 * (non-wildcard, also), keep 3370 * going. 3371 */ 3372 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3373 !V6_OR_V4_INADDR_ANY( 3374 ltcp->tcp_bound_source_v6) && 3375 !IN6_ARE_ADDR_EQUAL(laddr, 3376 <cp->tcp_bound_source_v6)) 3377 continue; 3378 if (ltcp->tcp_state >= TCPS_BOUND) { 3379 /* 3380 * This port is being used and 3381 * its state is >= TCPS_BOUND, 3382 * so we can't bind to it. 3383 */ 3384 break; 3385 } 3386 } else { 3387 /* 3388 * socket option SO_REUSEADDR is set on the 3389 * binding tcp_t. 3390 * 3391 * If two streams are bound to 3392 * same IP address or both addr 3393 * and bound source are wildcards 3394 * (INADDR_ANY), we want to stop 3395 * searching. 3396 * We have found a match of IP source 3397 * address and source port, which is 3398 * refused regardless of the 3399 * SO_REUSEADDR setting, so we break. 3400 */ 3401 if (IN6_ARE_ADDR_EQUAL(laddr, 3402 <cp->tcp_bound_source_v6) && 3403 (ltcp->tcp_state == TCPS_LISTEN || 3404 ltcp->tcp_state == TCPS_BOUND)) 3405 break; 3406 } 3407 } 3408 if (ltcp != NULL) { 3409 /* The port number is busy */ 3410 mutex_exit(&tbf->tf_lock); 3411 } else { 3412 /* 3413 * This port is ours. Insert in fanout and mark as 3414 * bound to prevent others from getting the port 3415 * number. 3416 */ 3417 tcp->tcp_state = TCPS_BOUND; 3418 tcp->tcp_lport = htons(port); 3419 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3420 3421 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3422 tcp->tcp_lport)] == tbf); 3423 tcp_bind_hash_insert(tbf, tcp, 1); 3424 3425 mutex_exit(&tbf->tf_lock); 3426 3427 /* 3428 * We don't want tcp_next_port_to_try to "inherit" 3429 * a port number supplied by the user in a bind. 3430 */ 3431 if (user_specified) 3432 return (port); 3433 3434 /* 3435 * This is the only place where tcp_next_port_to_try 3436 * is updated. After the update, it may or may not 3437 * be in the valid range. 3438 */ 3439 if (!tcp->tcp_anon_priv_bind) 3440 tcps->tcps_next_port_to_try = port + 1; 3441 return (port); 3442 } 3443 3444 if (tcp->tcp_anon_priv_bind) { 3445 port = tcp_get_next_priv_port(tcp); 3446 } else { 3447 if (count == 0 && user_specified) { 3448 /* 3449 * We may have to return an anonymous port. So 3450 * get one to start with. 3451 */ 3452 port = 3453 tcp_update_next_port( 3454 tcps->tcps_next_port_to_try, 3455 tcp, B_TRUE); 3456 user_specified = B_FALSE; 3457 } else { 3458 port = tcp_update_next_port(port + 1, tcp, 3459 B_FALSE); 3460 } 3461 } 3462 if (port == 0) 3463 break; 3464 3465 /* 3466 * Don't let this loop run forever in the case where 3467 * all of the anonymous ports are in use. 3468 */ 3469 } while (++count < loopmax); 3470 return (0); 3471 } 3472 3473 /* 3474 * tcp_clean_death / tcp_close_detached must not be called more than once 3475 * on a tcp. Thus every function that potentially calls tcp_clean_death 3476 * must check for the tcp state before calling tcp_clean_death. 3477 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3478 * tcp_timer_handler, all check for the tcp state. 3479 */ 3480 /* ARGSUSED */ 3481 void 3482 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3483 { 3484 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3485 3486 freemsg(mp); 3487 if (tcp->tcp_state > TCPS_BOUND) 3488 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3489 ETIMEDOUT, 5); 3490 } 3491 3492 /* 3493 * We are dying for some reason. Try to do it gracefully. (May be called 3494 * as writer.) 3495 * 3496 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3497 * done by a service procedure). 3498 * TBD - Should the return value distinguish between the tcp_t being 3499 * freed and it being reinitialized? 3500 */ 3501 static int 3502 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3503 { 3504 mblk_t *mp; 3505 queue_t *q; 3506 conn_t *connp = tcp->tcp_connp; 3507 tcp_stack_t *tcps = tcp->tcp_tcps; 3508 sodirect_t *sodp; 3509 3510 TCP_CLD_STAT(tag); 3511 3512 #if TCP_TAG_CLEAN_DEATH 3513 tcp->tcp_cleandeathtag = tag; 3514 #endif 3515 3516 if (tcp->tcp_fused) 3517 tcp_unfuse(tcp); 3518 3519 if (tcp->tcp_linger_tid != 0 && 3520 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3521 tcp_stop_lingering(tcp); 3522 } 3523 3524 ASSERT(tcp != NULL); 3525 ASSERT((tcp->tcp_family == AF_INET && 3526 tcp->tcp_ipversion == IPV4_VERSION) || 3527 (tcp->tcp_family == AF_INET6 && 3528 (tcp->tcp_ipversion == IPV4_VERSION || 3529 tcp->tcp_ipversion == IPV6_VERSION))); 3530 3531 if (TCP_IS_DETACHED(tcp)) { 3532 if (tcp->tcp_hard_binding) { 3533 /* 3534 * Its an eager that we are dealing with. We close the 3535 * eager but in case a conn_ind has already gone to the 3536 * listener, let tcp_accept_finish() send a discon_ind 3537 * to the listener and drop the last reference. If the 3538 * listener doesn't even know about the eager i.e. the 3539 * conn_ind hasn't gone up, blow away the eager and drop 3540 * the last reference as well. If the conn_ind has gone 3541 * up, state should be BOUND. tcp_accept_finish 3542 * will figure out that the connection has received a 3543 * RST and will send a DISCON_IND to the application. 3544 */ 3545 tcp_closei_local(tcp); 3546 if (!tcp->tcp_tconnind_started) { 3547 CONN_DEC_REF(connp); 3548 } else { 3549 tcp->tcp_state = TCPS_BOUND; 3550 } 3551 } else { 3552 tcp_close_detached(tcp); 3553 } 3554 return (0); 3555 } 3556 3557 TCP_STAT(tcps, tcp_clean_death_nondetached); 3558 3559 /* If sodirect, not anymore */ 3560 SOD_PTR_ENTER(tcp, sodp); 3561 if (sodp != NULL) { 3562 tcp->tcp_sodirect = NULL; 3563 mutex_exit(sodp->sod_lockp); 3564 } 3565 3566 q = tcp->tcp_rq; 3567 3568 /* Trash all inbound data */ 3569 if (!IPCL_IS_NONSTR(connp)) { 3570 ASSERT(q != NULL); 3571 flushq(q, FLUSHALL); 3572 } 3573 3574 /* 3575 * If we are at least part way open and there is error 3576 * (err==0 implies no error) 3577 * notify our client by a T_DISCON_IND. 3578 */ 3579 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3580 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3581 !TCP_IS_SOCKET(tcp)) { 3582 /* 3583 * Send M_FLUSH according to TPI. Because sockets will 3584 * (and must) ignore FLUSHR we do that only for TPI 3585 * endpoints and sockets in STREAMS mode. 3586 */ 3587 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3588 } 3589 if (tcp->tcp_debug) { 3590 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3591 "tcp_clean_death: discon err %d", err); 3592 } 3593 if (IPCL_IS_NONSTR(connp)) { 3594 /* Direct socket, use upcall */ 3595 (*connp->conn_upcalls->su_disconnected)( 3596 connp->conn_upper_handle, tcp->tcp_connid, err); 3597 } else { 3598 mp = mi_tpi_discon_ind(NULL, err, 0); 3599 if (mp != NULL) { 3600 putnext(q, mp); 3601 } else { 3602 if (tcp->tcp_debug) { 3603 (void) strlog(TCP_MOD_ID, 0, 1, 3604 SL_ERROR|SL_TRACE, 3605 "tcp_clean_death, sending M_ERROR"); 3606 } 3607 (void) putnextctl1(q, M_ERROR, EPROTO); 3608 } 3609 } 3610 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3611 /* SYN_SENT or SYN_RCVD */ 3612 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3613 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3614 /* ESTABLISHED or CLOSE_WAIT */ 3615 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3616 } 3617 } 3618 3619 tcp_reinit(tcp); 3620 if (IPCL_IS_NONSTR(connp)) 3621 (void) tcp_do_unbind(connp); 3622 3623 return (-1); 3624 } 3625 3626 /* 3627 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3628 * to expire, stop the wait and finish the close. 3629 */ 3630 static void 3631 tcp_stop_lingering(tcp_t *tcp) 3632 { 3633 clock_t delta = 0; 3634 tcp_stack_t *tcps = tcp->tcp_tcps; 3635 3636 tcp->tcp_linger_tid = 0; 3637 if (tcp->tcp_state > TCPS_LISTEN) { 3638 tcp_acceptor_hash_remove(tcp); 3639 mutex_enter(&tcp->tcp_non_sq_lock); 3640 if (tcp->tcp_flow_stopped) { 3641 tcp_clrqfull(tcp); 3642 } 3643 mutex_exit(&tcp->tcp_non_sq_lock); 3644 3645 if (tcp->tcp_timer_tid != 0) { 3646 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3647 tcp->tcp_timer_tid = 0; 3648 } 3649 /* 3650 * Need to cancel those timers which will not be used when 3651 * TCP is detached. This has to be done before the tcp_wq 3652 * is set to the global queue. 3653 */ 3654 tcp_timers_stop(tcp); 3655 3656 tcp->tcp_detached = B_TRUE; 3657 ASSERT(tcps->tcps_g_q != NULL); 3658 tcp->tcp_rq = tcps->tcps_g_q; 3659 tcp->tcp_wq = WR(tcps->tcps_g_q); 3660 3661 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3662 tcp_time_wait_append(tcp); 3663 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3664 goto finish; 3665 } 3666 3667 /* 3668 * If delta is zero the timer event wasn't executed and was 3669 * successfully canceled. In this case we need to restart it 3670 * with the minimal delta possible. 3671 */ 3672 if (delta >= 0) { 3673 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3674 delta ? delta : 1); 3675 } 3676 } else { 3677 tcp_closei_local(tcp); 3678 CONN_DEC_REF(tcp->tcp_connp); 3679 } 3680 finish: 3681 /* Signal closing thread that it can complete close */ 3682 mutex_enter(&tcp->tcp_closelock); 3683 tcp->tcp_detached = B_TRUE; 3684 ASSERT(tcps->tcps_g_q != NULL); 3685 3686 tcp->tcp_rq = tcps->tcps_g_q; 3687 tcp->tcp_wq = WR(tcps->tcps_g_q); 3688 3689 tcp->tcp_closed = 1; 3690 cv_signal(&tcp->tcp_closecv); 3691 mutex_exit(&tcp->tcp_closelock); 3692 } 3693 3694 /* 3695 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3696 * expires. 3697 */ 3698 static void 3699 tcp_close_linger_timeout(void *arg) 3700 { 3701 conn_t *connp = (conn_t *)arg; 3702 tcp_t *tcp = connp->conn_tcp; 3703 3704 tcp->tcp_client_errno = ETIMEDOUT; 3705 tcp_stop_lingering(tcp); 3706 } 3707 3708 static void 3709 tcp_close_common(conn_t *connp, int flags) 3710 { 3711 tcp_t *tcp = connp->conn_tcp; 3712 mblk_t *mp = &tcp->tcp_closemp; 3713 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3714 mblk_t *bp; 3715 3716 ASSERT(connp->conn_ref >= 2); 3717 3718 /* 3719 * Mark the conn as closing. ill_pending_mp_add will not 3720 * add any mp to the pending mp list, after this conn has 3721 * started closing. Same for sq_pending_mp_add 3722 */ 3723 mutex_enter(&connp->conn_lock); 3724 connp->conn_state_flags |= CONN_CLOSING; 3725 if (connp->conn_oper_pending_ill != NULL) 3726 conn_ioctl_cleanup_reqd = B_TRUE; 3727 CONN_INC_REF_LOCKED(connp); 3728 mutex_exit(&connp->conn_lock); 3729 tcp->tcp_closeflags = (uint8_t)flags; 3730 ASSERT(connp->conn_ref >= 3); 3731 3732 /* 3733 * tcp_closemp_used is used below without any protection of a lock 3734 * as we don't expect any one else to use it concurrently at this 3735 * point otherwise it would be a major defect. 3736 */ 3737 3738 if (mp->b_prev == NULL) 3739 tcp->tcp_closemp_used = B_TRUE; 3740 else 3741 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3742 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3743 3744 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3745 3746 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3747 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3748 3749 mutex_enter(&tcp->tcp_closelock); 3750 while (!tcp->tcp_closed) { 3751 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3752 /* 3753 * The cv_wait_sig() was interrupted. We now do the 3754 * following: 3755 * 3756 * 1) If the endpoint was lingering, we allow this 3757 * to be interrupted by cancelling the linger timeout 3758 * and closing normally. 3759 * 3760 * 2) Revert to calling cv_wait() 3761 * 3762 * We revert to using cv_wait() to avoid an 3763 * infinite loop which can occur if the calling 3764 * thread is higher priority than the squeue worker 3765 * thread and is bound to the same cpu. 3766 */ 3767 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3768 mutex_exit(&tcp->tcp_closelock); 3769 /* Entering squeue, bump ref count. */ 3770 CONN_INC_REF(connp); 3771 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3772 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3773 tcp_linger_interrupted, connp, 3774 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3775 mutex_enter(&tcp->tcp_closelock); 3776 } 3777 break; 3778 } 3779 } 3780 while (!tcp->tcp_closed) 3781 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3782 mutex_exit(&tcp->tcp_closelock); 3783 3784 /* 3785 * In the case of listener streams that have eagers in the q or q0 3786 * we wait for the eagers to drop their reference to us. tcp_rq and 3787 * tcp_wq of the eagers point to our queues. By waiting for the 3788 * refcnt to drop to 1, we are sure that the eagers have cleaned 3789 * up their queue pointers and also dropped their references to us. 3790 */ 3791 if (tcp->tcp_wait_for_eagers) { 3792 mutex_enter(&connp->conn_lock); 3793 while (connp->conn_ref != 1) { 3794 cv_wait(&connp->conn_cv, &connp->conn_lock); 3795 } 3796 mutex_exit(&connp->conn_lock); 3797 } 3798 /* 3799 * ioctl cleanup. The mp is queued in the 3800 * ill_pending_mp or in the sq_pending_mp. 3801 */ 3802 if (conn_ioctl_cleanup_reqd) 3803 conn_ioctl_cleanup(connp); 3804 3805 tcp->tcp_cpid = -1; 3806 } 3807 3808 static int 3809 tcp_tpi_close(queue_t *q, int flags) 3810 { 3811 conn_t *connp; 3812 3813 ASSERT(WR(q)->q_next == NULL); 3814 3815 if (flags & SO_FALLBACK) { 3816 /* 3817 * stream is being closed while in fallback 3818 * simply free the resources that were allocated 3819 */ 3820 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3821 qprocsoff(q); 3822 goto done; 3823 } 3824 3825 connp = Q_TO_CONN(q); 3826 /* 3827 * We are being closed as /dev/tcp or /dev/tcp6. 3828 */ 3829 tcp_close_common(connp, flags); 3830 3831 qprocsoff(q); 3832 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3833 3834 /* 3835 * Drop IP's reference on the conn. This is the last reference 3836 * on the connp if the state was less than established. If the 3837 * connection has gone into timewait state, then we will have 3838 * one ref for the TCP and one more ref (total of two) for the 3839 * classifier connected hash list (a timewait connections stays 3840 * in connected hash till closed). 3841 * 3842 * We can't assert the references because there might be other 3843 * transient reference places because of some walkers or queued 3844 * packets in squeue for the timewait state. 3845 */ 3846 CONN_DEC_REF(connp); 3847 done: 3848 q->q_ptr = WR(q)->q_ptr = NULL; 3849 return (0); 3850 } 3851 3852 static int 3853 tcpclose_accept(queue_t *q) 3854 { 3855 vmem_t *minor_arena; 3856 dev_t conn_dev; 3857 3858 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3859 3860 /* 3861 * We had opened an acceptor STREAM for sockfs which is 3862 * now being closed due to some error. 3863 */ 3864 qprocsoff(q); 3865 3866 minor_arena = (vmem_t *)WR(q)->q_ptr; 3867 conn_dev = (dev_t)RD(q)->q_ptr; 3868 ASSERT(minor_arena != NULL); 3869 ASSERT(conn_dev != 0); 3870 inet_minor_free(minor_arena, conn_dev); 3871 q->q_ptr = WR(q)->q_ptr = NULL; 3872 return (0); 3873 } 3874 3875 /* 3876 * Called by tcp_close() routine via squeue when lingering is 3877 * interrupted by a signal. 3878 */ 3879 3880 /* ARGSUSED */ 3881 static void 3882 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3883 { 3884 conn_t *connp = (conn_t *)arg; 3885 tcp_t *tcp = connp->conn_tcp; 3886 3887 freeb(mp); 3888 if (tcp->tcp_linger_tid != 0 && 3889 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3890 tcp_stop_lingering(tcp); 3891 tcp->tcp_client_errno = EINTR; 3892 } 3893 } 3894 3895 /* 3896 * Called by streams close routine via squeues when our client blows off her 3897 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3898 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3899 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3900 * acked. 3901 * 3902 * NOTE: tcp_close potentially returns error when lingering. 3903 * However, the stream head currently does not pass these errors 3904 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3905 * errors to the application (from tsleep()) and not errors 3906 * like ECONNRESET caused by receiving a reset packet. 3907 */ 3908 3909 /* ARGSUSED */ 3910 static void 3911 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3912 { 3913 char *msg; 3914 conn_t *connp = (conn_t *)arg; 3915 tcp_t *tcp = connp->conn_tcp; 3916 clock_t delta = 0; 3917 tcp_stack_t *tcps = tcp->tcp_tcps; 3918 3919 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3920 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3921 3922 mutex_enter(&tcp->tcp_eager_lock); 3923 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3924 /* Cleanup for listener */ 3925 tcp_eager_cleanup(tcp, 0); 3926 tcp->tcp_wait_for_eagers = 1; 3927 } 3928 mutex_exit(&tcp->tcp_eager_lock); 3929 3930 connp->conn_mdt_ok = B_FALSE; 3931 tcp->tcp_mdt = B_FALSE; 3932 3933 connp->conn_lso_ok = B_FALSE; 3934 tcp->tcp_lso = B_FALSE; 3935 3936 msg = NULL; 3937 switch (tcp->tcp_state) { 3938 case TCPS_CLOSED: 3939 case TCPS_IDLE: 3940 case TCPS_BOUND: 3941 case TCPS_LISTEN: 3942 break; 3943 case TCPS_SYN_SENT: 3944 msg = "tcp_close, during connect"; 3945 break; 3946 case TCPS_SYN_RCVD: 3947 /* 3948 * Close during the connect 3-way handshake 3949 * but here there may or may not be pending data 3950 * already on queue. Process almost same as in 3951 * the ESTABLISHED state. 3952 */ 3953 /* FALLTHRU */ 3954 default: 3955 if (tcp->tcp_sodirect != NULL) { 3956 /* Ok, no more sodirect */ 3957 tcp->tcp_sodirect = NULL; 3958 } 3959 3960 if (tcp->tcp_fused) 3961 tcp_unfuse(tcp); 3962 3963 /* 3964 * If SO_LINGER has set a zero linger time, abort the 3965 * connection with a reset. 3966 */ 3967 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3968 msg = "tcp_close, zero lingertime"; 3969 break; 3970 } 3971 3972 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3973 /* 3974 * Abort connection if there is unread data queued. 3975 */ 3976 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3977 msg = "tcp_close, unread data"; 3978 break; 3979 } 3980 /* 3981 * tcp_hard_bound is now cleared thus all packets go through 3982 * tcp_lookup. This fact is used by tcp_detach below. 3983 * 3984 * We have done a qwait() above which could have possibly 3985 * drained more messages in turn causing transition to a 3986 * different state. Check whether we have to do the rest 3987 * of the processing or not. 3988 */ 3989 if (tcp->tcp_state <= TCPS_LISTEN) 3990 break; 3991 3992 /* 3993 * Transmit the FIN before detaching the tcp_t. 3994 * After tcp_detach returns this queue/perimeter 3995 * no longer owns the tcp_t thus others can modify it. 3996 */ 3997 (void) tcp_xmit_end(tcp); 3998 3999 /* 4000 * If lingering on close then wait until the fin is acked, 4001 * the SO_LINGER time passes, or a reset is sent/received. 4002 */ 4003 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4004 !(tcp->tcp_fin_acked) && 4005 tcp->tcp_state >= TCPS_ESTABLISHED) { 4006 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4007 tcp->tcp_client_errno = EWOULDBLOCK; 4008 } else if (tcp->tcp_client_errno == 0) { 4009 4010 ASSERT(tcp->tcp_linger_tid == 0); 4011 4012 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4013 tcp_close_linger_timeout, 4014 tcp->tcp_lingertime * hz); 4015 4016 /* tcp_close_linger_timeout will finish close */ 4017 if (tcp->tcp_linger_tid == 0) 4018 tcp->tcp_client_errno = ENOSR; 4019 else 4020 return; 4021 } 4022 4023 /* 4024 * Check if we need to detach or just close 4025 * the instance. 4026 */ 4027 if (tcp->tcp_state <= TCPS_LISTEN) 4028 break; 4029 } 4030 4031 /* 4032 * Make sure that no other thread will access the tcp_rq of 4033 * this instance (through lookups etc.) as tcp_rq will go 4034 * away shortly. 4035 */ 4036 tcp_acceptor_hash_remove(tcp); 4037 4038 mutex_enter(&tcp->tcp_non_sq_lock); 4039 if (tcp->tcp_flow_stopped) { 4040 tcp_clrqfull(tcp); 4041 } 4042 mutex_exit(&tcp->tcp_non_sq_lock); 4043 4044 if (tcp->tcp_timer_tid != 0) { 4045 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4046 tcp->tcp_timer_tid = 0; 4047 } 4048 /* 4049 * Need to cancel those timers which will not be used when 4050 * TCP is detached. This has to be done before the tcp_wq 4051 * is set to the global queue. 4052 */ 4053 tcp_timers_stop(tcp); 4054 4055 tcp->tcp_detached = B_TRUE; 4056 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4057 tcp_time_wait_append(tcp); 4058 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4059 ASSERT(connp->conn_ref >= 3); 4060 goto finish; 4061 } 4062 4063 /* 4064 * If delta is zero the timer event wasn't executed and was 4065 * successfully canceled. In this case we need to restart it 4066 * with the minimal delta possible. 4067 */ 4068 if (delta >= 0) 4069 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4070 delta ? delta : 1); 4071 4072 ASSERT(connp->conn_ref >= 3); 4073 goto finish; 4074 } 4075 4076 /* Detach did not complete. Still need to remove q from stream. */ 4077 if (msg) { 4078 if (tcp->tcp_state == TCPS_ESTABLISHED || 4079 tcp->tcp_state == TCPS_CLOSE_WAIT) 4080 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4081 if (tcp->tcp_state == TCPS_SYN_SENT || 4082 tcp->tcp_state == TCPS_SYN_RCVD) 4083 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4084 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4085 } 4086 4087 tcp_closei_local(tcp); 4088 CONN_DEC_REF(connp); 4089 ASSERT(connp->conn_ref >= 2); 4090 4091 finish: 4092 /* 4093 * Although packets are always processed on the correct 4094 * tcp's perimeter and access is serialized via squeue's, 4095 * IP still needs a queue when sending packets in time_wait 4096 * state so use WR(tcps_g_q) till ip_output() can be 4097 * changed to deal with just connp. For read side, we 4098 * could have set tcp_rq to NULL but there are some cases 4099 * in tcp_rput_data() from early days of this code which 4100 * do a putnext without checking if tcp is closed. Those 4101 * need to be identified before both tcp_rq and tcp_wq 4102 * can be set to NULL and tcps_g_q can disappear forever. 4103 */ 4104 mutex_enter(&tcp->tcp_closelock); 4105 /* 4106 * Don't change the queues in the case of a listener that has 4107 * eagers in its q or q0. It could surprise the eagers. 4108 * Instead wait for the eagers outside the squeue. 4109 */ 4110 if (!tcp->tcp_wait_for_eagers) { 4111 tcp->tcp_detached = B_TRUE; 4112 /* 4113 * When default queue is closing we set tcps_g_q to NULL 4114 * after the close is done. 4115 */ 4116 ASSERT(tcps->tcps_g_q != NULL); 4117 tcp->tcp_rq = tcps->tcps_g_q; 4118 tcp->tcp_wq = WR(tcps->tcps_g_q); 4119 } 4120 4121 /* Signal tcp_close() to finish closing. */ 4122 tcp->tcp_closed = 1; 4123 cv_signal(&tcp->tcp_closecv); 4124 mutex_exit(&tcp->tcp_closelock); 4125 } 4126 4127 4128 /* 4129 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4130 * Some stream heads get upset if they see these later on as anything but NULL. 4131 */ 4132 static void 4133 tcp_close_mpp(mblk_t **mpp) 4134 { 4135 mblk_t *mp; 4136 4137 if ((mp = *mpp) != NULL) { 4138 do { 4139 mp->b_next = NULL; 4140 mp->b_prev = NULL; 4141 } while ((mp = mp->b_cont) != NULL); 4142 4143 mp = *mpp; 4144 *mpp = NULL; 4145 freemsg(mp); 4146 } 4147 } 4148 4149 /* Do detached close. */ 4150 static void 4151 tcp_close_detached(tcp_t *tcp) 4152 { 4153 if (tcp->tcp_fused) 4154 tcp_unfuse(tcp); 4155 4156 /* 4157 * Clustering code serializes TCP disconnect callbacks and 4158 * cluster tcp list walks by blocking a TCP disconnect callback 4159 * if a cluster tcp list walk is in progress. This ensures 4160 * accurate accounting of TCPs in the cluster code even though 4161 * the TCP list walk itself is not atomic. 4162 */ 4163 tcp_closei_local(tcp); 4164 CONN_DEC_REF(tcp->tcp_connp); 4165 } 4166 4167 /* 4168 * Stop all TCP timers, and free the timer mblks if requested. 4169 */ 4170 void 4171 tcp_timers_stop(tcp_t *tcp) 4172 { 4173 if (tcp->tcp_timer_tid != 0) { 4174 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4175 tcp->tcp_timer_tid = 0; 4176 } 4177 if (tcp->tcp_ka_tid != 0) { 4178 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4179 tcp->tcp_ka_tid = 0; 4180 } 4181 if (tcp->tcp_ack_tid != 0) { 4182 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4183 tcp->tcp_ack_tid = 0; 4184 } 4185 if (tcp->tcp_push_tid != 0) { 4186 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4187 tcp->tcp_push_tid = 0; 4188 } 4189 } 4190 4191 /* 4192 * The tcp_t is going away. Remove it from all lists and set it 4193 * to TCPS_CLOSED. The freeing up of memory is deferred until 4194 * tcp_inactive. This is needed since a thread in tcp_rput might have 4195 * done a CONN_INC_REF on this structure before it was removed from the 4196 * hashes. 4197 */ 4198 static void 4199 tcp_closei_local(tcp_t *tcp) 4200 { 4201 ire_t *ire; 4202 conn_t *connp = tcp->tcp_connp; 4203 tcp_stack_t *tcps = tcp->tcp_tcps; 4204 4205 if (!TCP_IS_SOCKET(tcp)) 4206 tcp_acceptor_hash_remove(tcp); 4207 4208 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4209 tcp->tcp_ibsegs = 0; 4210 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4211 tcp->tcp_obsegs = 0; 4212 4213 /* 4214 * If we are an eager connection hanging off a listener that 4215 * hasn't formally accepted the connection yet, get off his 4216 * list and blow off any data that we have accumulated. 4217 */ 4218 if (tcp->tcp_listener != NULL) { 4219 tcp_t *listener = tcp->tcp_listener; 4220 mutex_enter(&listener->tcp_eager_lock); 4221 /* 4222 * tcp_tconnind_started == B_TRUE means that the 4223 * conn_ind has already gone to listener. At 4224 * this point, eager will be closed but we 4225 * leave it in listeners eager list so that 4226 * if listener decides to close without doing 4227 * accept, we can clean this up. In tcp_wput_accept 4228 * we take care of the case of accept on closed 4229 * eager. 4230 */ 4231 if (!tcp->tcp_tconnind_started) { 4232 tcp_eager_unlink(tcp); 4233 mutex_exit(&listener->tcp_eager_lock); 4234 /* 4235 * We don't want to have any pointers to the 4236 * listener queue, after we have released our 4237 * reference on the listener 4238 */ 4239 ASSERT(tcps->tcps_g_q != NULL); 4240 tcp->tcp_rq = tcps->tcps_g_q; 4241 tcp->tcp_wq = WR(tcps->tcps_g_q); 4242 CONN_DEC_REF(listener->tcp_connp); 4243 } else { 4244 mutex_exit(&listener->tcp_eager_lock); 4245 } 4246 } 4247 4248 /* Stop all the timers */ 4249 tcp_timers_stop(tcp); 4250 4251 if (tcp->tcp_state == TCPS_LISTEN) { 4252 if (tcp->tcp_ip_addr_cache) { 4253 kmem_free((void *)tcp->tcp_ip_addr_cache, 4254 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4255 tcp->tcp_ip_addr_cache = NULL; 4256 } 4257 } 4258 mutex_enter(&tcp->tcp_non_sq_lock); 4259 if (tcp->tcp_flow_stopped) 4260 tcp_clrqfull(tcp); 4261 mutex_exit(&tcp->tcp_non_sq_lock); 4262 4263 tcp_bind_hash_remove(tcp); 4264 /* 4265 * If the tcp_time_wait_collector (which runs outside the squeue) 4266 * is trying to remove this tcp from the time wait list, we will 4267 * block in tcp_time_wait_remove while trying to acquire the 4268 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4269 * requires the ipcl_hash_remove to be ordered after the 4270 * tcp_time_wait_remove for the refcnt checks to work correctly. 4271 */ 4272 if (tcp->tcp_state == TCPS_TIME_WAIT) 4273 (void) tcp_time_wait_remove(tcp, NULL); 4274 CL_INET_DISCONNECT(connp, tcp); 4275 ipcl_hash_remove(connp); 4276 4277 /* 4278 * Delete the cached ire in conn_ire_cache and also mark 4279 * the conn as CONDEMNED 4280 */ 4281 mutex_enter(&connp->conn_lock); 4282 connp->conn_state_flags |= CONN_CONDEMNED; 4283 ire = connp->conn_ire_cache; 4284 connp->conn_ire_cache = NULL; 4285 mutex_exit(&connp->conn_lock); 4286 if (ire != NULL) 4287 IRE_REFRELE_NOTR(ire); 4288 4289 /* Need to cleanup any pending ioctls */ 4290 ASSERT(tcp->tcp_time_wait_next == NULL); 4291 ASSERT(tcp->tcp_time_wait_prev == NULL); 4292 ASSERT(tcp->tcp_time_wait_expire == 0); 4293 tcp->tcp_state = TCPS_CLOSED; 4294 4295 /* Release any SSL context */ 4296 if (tcp->tcp_kssl_ent != NULL) { 4297 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4298 tcp->tcp_kssl_ent = NULL; 4299 } 4300 if (tcp->tcp_kssl_ctx != NULL) { 4301 kssl_release_ctx(tcp->tcp_kssl_ctx); 4302 tcp->tcp_kssl_ctx = NULL; 4303 } 4304 tcp->tcp_kssl_pending = B_FALSE; 4305 4306 tcp_ipsec_cleanup(tcp); 4307 } 4308 4309 /* 4310 * tcp is dying (called from ipcl_conn_destroy and error cases). 4311 * Free the tcp_t in either case. 4312 */ 4313 void 4314 tcp_free(tcp_t *tcp) 4315 { 4316 mblk_t *mp; 4317 ip6_pkt_t *ipp; 4318 4319 ASSERT(tcp != NULL); 4320 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4321 4322 tcp->tcp_rq = NULL; 4323 tcp->tcp_wq = NULL; 4324 4325 tcp_close_mpp(&tcp->tcp_xmit_head); 4326 tcp_close_mpp(&tcp->tcp_reass_head); 4327 if (tcp->tcp_rcv_list != NULL) { 4328 /* Free b_next chain */ 4329 tcp_close_mpp(&tcp->tcp_rcv_list); 4330 } 4331 if ((mp = tcp->tcp_urp_mp) != NULL) { 4332 freemsg(mp); 4333 } 4334 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4335 freemsg(mp); 4336 } 4337 4338 if (tcp->tcp_fused_sigurg_mp != NULL) { 4339 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4340 freeb(tcp->tcp_fused_sigurg_mp); 4341 tcp->tcp_fused_sigurg_mp = NULL; 4342 } 4343 4344 if (tcp->tcp_ordrel_mp != NULL) { 4345 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4346 freeb(tcp->tcp_ordrel_mp); 4347 tcp->tcp_ordrel_mp = NULL; 4348 } 4349 4350 if (tcp->tcp_sack_info != NULL) { 4351 if (tcp->tcp_notsack_list != NULL) { 4352 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4353 } 4354 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4355 } 4356 4357 if (tcp->tcp_hopopts != NULL) { 4358 mi_free(tcp->tcp_hopopts); 4359 tcp->tcp_hopopts = NULL; 4360 tcp->tcp_hopoptslen = 0; 4361 } 4362 ASSERT(tcp->tcp_hopoptslen == 0); 4363 if (tcp->tcp_dstopts != NULL) { 4364 mi_free(tcp->tcp_dstopts); 4365 tcp->tcp_dstopts = NULL; 4366 tcp->tcp_dstoptslen = 0; 4367 } 4368 ASSERT(tcp->tcp_dstoptslen == 0); 4369 if (tcp->tcp_rtdstopts != NULL) { 4370 mi_free(tcp->tcp_rtdstopts); 4371 tcp->tcp_rtdstopts = NULL; 4372 tcp->tcp_rtdstoptslen = 0; 4373 } 4374 ASSERT(tcp->tcp_rtdstoptslen == 0); 4375 if (tcp->tcp_rthdr != NULL) { 4376 mi_free(tcp->tcp_rthdr); 4377 tcp->tcp_rthdr = NULL; 4378 tcp->tcp_rthdrlen = 0; 4379 } 4380 ASSERT(tcp->tcp_rthdrlen == 0); 4381 4382 ipp = &tcp->tcp_sticky_ipp; 4383 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4384 IPPF_RTHDR)) 4385 ip6_pkt_free(ipp); 4386 4387 /* 4388 * Free memory associated with the tcp/ip header template. 4389 */ 4390 4391 if (tcp->tcp_iphc != NULL) 4392 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4393 4394 /* 4395 * Following is really a blowing away a union. 4396 * It happens to have exactly two members of identical size 4397 * the following code is enough. 4398 */ 4399 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4400 } 4401 4402 4403 /* 4404 * Put a connection confirmation message upstream built from the 4405 * address information within 'iph' and 'tcph'. Report our success or failure. 4406 */ 4407 static boolean_t 4408 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4409 mblk_t **defermp) 4410 { 4411 sin_t sin; 4412 sin6_t sin6; 4413 mblk_t *mp; 4414 char *optp = NULL; 4415 int optlen = 0; 4416 4417 if (defermp != NULL) 4418 *defermp = NULL; 4419 4420 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4421 /* 4422 * Return in T_CONN_CON results of option negotiation through 4423 * the T_CONN_REQ. Note: If there is an real end-to-end option 4424 * negotiation, then what is received from remote end needs 4425 * to be taken into account but there is no such thing (yet?) 4426 * in our TCP/IP. 4427 * Note: We do not use mi_offset_param() here as 4428 * tcp_opts_conn_req contents do not directly come from 4429 * an application and are either generated in kernel or 4430 * from user input that was already verified. 4431 */ 4432 mp = tcp->tcp_conn.tcp_opts_conn_req; 4433 optp = (char *)(mp->b_rptr + 4434 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4435 optlen = (int) 4436 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4437 } 4438 4439 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4440 ipha_t *ipha = (ipha_t *)iphdr; 4441 4442 /* packet is IPv4 */ 4443 if (tcp->tcp_family == AF_INET) { 4444 sin = sin_null; 4445 sin.sin_addr.s_addr = ipha->ipha_src; 4446 sin.sin_port = *(uint16_t *)tcph->th_lport; 4447 sin.sin_family = AF_INET; 4448 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4449 (int)sizeof (sin_t), optp, optlen); 4450 } else { 4451 sin6 = sin6_null; 4452 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4453 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4454 sin6.sin6_family = AF_INET6; 4455 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4456 (int)sizeof (sin6_t), optp, optlen); 4457 4458 } 4459 } else { 4460 ip6_t *ip6h = (ip6_t *)iphdr; 4461 4462 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4463 ASSERT(tcp->tcp_family == AF_INET6); 4464 sin6 = sin6_null; 4465 sin6.sin6_addr = ip6h->ip6_src; 4466 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4467 sin6.sin6_family = AF_INET6; 4468 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4469 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4470 (int)sizeof (sin6_t), optp, optlen); 4471 } 4472 4473 if (!mp) 4474 return (B_FALSE); 4475 4476 mblk_copycred(mp, idmp); 4477 4478 if (defermp == NULL) { 4479 conn_t *connp = tcp->tcp_connp; 4480 if (IPCL_IS_NONSTR(connp)) { 4481 cred_t *cr; 4482 pid_t cpid; 4483 4484 cr = msg_getcred(mp, &cpid); 4485 (*connp->conn_upcalls->su_connected) 4486 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4487 cpid); 4488 freemsg(mp); 4489 } else { 4490 putnext(tcp->tcp_rq, mp); 4491 } 4492 } else { 4493 *defermp = mp; 4494 } 4495 4496 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4497 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4498 return (B_TRUE); 4499 } 4500 4501 /* 4502 * Defense for the SYN attack - 4503 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4504 * one from the list of droppable eagers. This list is a subset of q0. 4505 * see comments before the definition of MAKE_DROPPABLE(). 4506 * 2. Don't drop a SYN request before its first timeout. This gives every 4507 * request at least til the first timeout to complete its 3-way handshake. 4508 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4509 * requests currently on the queue that has timed out. This will be used 4510 * as an indicator of whether an attack is under way, so that appropriate 4511 * actions can be taken. (It's incremented in tcp_timer() and decremented 4512 * either when eager goes into ESTABLISHED, or gets freed up.) 4513 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4514 * # of timeout drops back to <= q0len/32 => SYN alert off 4515 */ 4516 static boolean_t 4517 tcp_drop_q0(tcp_t *tcp) 4518 { 4519 tcp_t *eager; 4520 mblk_t *mp; 4521 tcp_stack_t *tcps = tcp->tcp_tcps; 4522 4523 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4524 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4525 4526 /* Pick oldest eager from the list of droppable eagers */ 4527 eager = tcp->tcp_eager_prev_drop_q0; 4528 4529 /* If list is empty. return B_FALSE */ 4530 if (eager == tcp) { 4531 return (B_FALSE); 4532 } 4533 4534 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4535 if ((mp = allocb(0, BPRI_HI)) == NULL) 4536 return (B_FALSE); 4537 4538 /* 4539 * Take this eager out from the list of droppable eagers since we are 4540 * going to drop it. 4541 */ 4542 MAKE_UNDROPPABLE(eager); 4543 4544 if (tcp->tcp_debug) { 4545 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4546 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4547 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4548 tcp->tcp_conn_req_cnt_q0, 4549 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4550 } 4551 4552 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4553 4554 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4555 CONN_INC_REF(eager->tcp_connp); 4556 4557 /* Mark the IRE created for this SYN request temporary */ 4558 tcp_ip_ire_mark_advice(eager); 4559 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4560 tcp_clean_death_wrapper, eager->tcp_connp, 4561 SQ_FILL, SQTAG_TCP_DROP_Q0); 4562 4563 return (B_TRUE); 4564 } 4565 4566 int 4567 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4568 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4569 { 4570 tcp_t *ltcp = lconnp->conn_tcp; 4571 tcp_t *tcp = connp->conn_tcp; 4572 mblk_t *tpi_mp; 4573 ipha_t *ipha; 4574 ip6_t *ip6h; 4575 sin6_t sin6; 4576 in6_addr_t v6dst; 4577 int err; 4578 int ifindex = 0; 4579 tcp_stack_t *tcps = tcp->tcp_tcps; 4580 4581 if (ipvers == IPV4_VERSION) { 4582 ipha = (ipha_t *)mp->b_rptr; 4583 4584 connp->conn_send = ip_output; 4585 connp->conn_recv = tcp_input; 4586 4587 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4588 &connp->conn_bound_source_v6); 4589 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4590 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4591 4592 sin6 = sin6_null; 4593 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4594 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4595 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4596 sin6.sin6_family = AF_INET6; 4597 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4598 lconnp->conn_zoneid, tcps->tcps_netstack); 4599 if (tcp->tcp_recvdstaddr) { 4600 sin6_t sin6d; 4601 4602 sin6d = sin6_null; 4603 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4604 &sin6d.sin6_addr); 4605 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4606 sin6d.sin6_family = AF_INET; 4607 tpi_mp = mi_tpi_extconn_ind(NULL, 4608 (char *)&sin6d, sizeof (sin6_t), 4609 (char *)&tcp, 4610 (t_scalar_t)sizeof (intptr_t), 4611 (char *)&sin6d, sizeof (sin6_t), 4612 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4613 } else { 4614 tpi_mp = mi_tpi_conn_ind(NULL, 4615 (char *)&sin6, sizeof (sin6_t), 4616 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4617 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4618 } 4619 } else { 4620 ip6h = (ip6_t *)mp->b_rptr; 4621 4622 connp->conn_send = ip_output_v6; 4623 connp->conn_recv = tcp_input; 4624 4625 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4626 connp->conn_srcv6 = ip6h->ip6_dst; 4627 connp->conn_remv6 = ip6h->ip6_src; 4628 4629 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4630 ifindex = (int)DB_CKSUMSTUFF(mp); 4631 DB_CKSUMSTUFF(mp) = 0; 4632 4633 sin6 = sin6_null; 4634 sin6.sin6_addr = ip6h->ip6_src; 4635 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4636 sin6.sin6_family = AF_INET6; 4637 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4638 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4639 lconnp->conn_zoneid, tcps->tcps_netstack); 4640 4641 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4642 /* Pass up the scope_id of remote addr */ 4643 sin6.sin6_scope_id = ifindex; 4644 } else { 4645 sin6.sin6_scope_id = 0; 4646 } 4647 if (tcp->tcp_recvdstaddr) { 4648 sin6_t sin6d; 4649 4650 sin6d = sin6_null; 4651 sin6.sin6_addr = ip6h->ip6_dst; 4652 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4653 sin6d.sin6_family = AF_INET; 4654 tpi_mp = mi_tpi_extconn_ind(NULL, 4655 (char *)&sin6d, sizeof (sin6_t), 4656 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4657 (char *)&sin6d, sizeof (sin6_t), 4658 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4659 } else { 4660 tpi_mp = mi_tpi_conn_ind(NULL, 4661 (char *)&sin6, sizeof (sin6_t), 4662 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4663 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4664 } 4665 } 4666 4667 if (tpi_mp == NULL) 4668 return (ENOMEM); 4669 4670 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4671 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4672 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4673 connp->conn_fully_bound = B_FALSE; 4674 4675 /* Inherit information from the "parent" */ 4676 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4677 tcp->tcp_family = ltcp->tcp_family; 4678 4679 tcp->tcp_wq = ltcp->tcp_wq; 4680 tcp->tcp_rq = ltcp->tcp_rq; 4681 4682 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4683 tcp->tcp_detached = B_TRUE; 4684 SOCK_CONNID_INIT(tcp->tcp_connid); 4685 if ((err = tcp_init_values(tcp)) != 0) { 4686 freemsg(tpi_mp); 4687 return (err); 4688 } 4689 4690 if (ipvers == IPV4_VERSION) { 4691 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4692 freemsg(tpi_mp); 4693 return (err); 4694 } 4695 ASSERT(tcp->tcp_ipha != NULL); 4696 } else { 4697 /* ifindex must be already set */ 4698 ASSERT(ifindex != 0); 4699 4700 if (ltcp->tcp_bound_if != 0) 4701 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4702 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4703 tcp->tcp_bound_if = ifindex; 4704 4705 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4706 tcp->tcp_recvifindex = 0; 4707 tcp->tcp_recvhops = 0xffffffffU; 4708 ASSERT(tcp->tcp_ip6h != NULL); 4709 } 4710 4711 tcp->tcp_lport = ltcp->tcp_lport; 4712 4713 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4714 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4715 /* 4716 * Listener had options of some sort; eager inherits. 4717 * Free up the eager template and allocate one 4718 * of the right size. 4719 */ 4720 if (tcp->tcp_hdr_grown) { 4721 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4722 } else { 4723 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4724 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4725 } 4726 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4727 KM_NOSLEEP); 4728 if (tcp->tcp_iphc == NULL) { 4729 tcp->tcp_iphc_len = 0; 4730 freemsg(tpi_mp); 4731 return (ENOMEM); 4732 } 4733 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4734 tcp->tcp_hdr_grown = B_TRUE; 4735 } 4736 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4737 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4738 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4739 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4740 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4741 4742 /* 4743 * Copy the IP+TCP header template from listener to eager 4744 */ 4745 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4746 if (tcp->tcp_ipversion == IPV6_VERSION) { 4747 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4748 IPPROTO_RAW) { 4749 tcp->tcp_ip6h = 4750 (ip6_t *)(tcp->tcp_iphc + 4751 sizeof (ip6i_t)); 4752 } else { 4753 tcp->tcp_ip6h = 4754 (ip6_t *)(tcp->tcp_iphc); 4755 } 4756 tcp->tcp_ipha = NULL; 4757 } else { 4758 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4759 tcp->tcp_ip6h = NULL; 4760 } 4761 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4762 tcp->tcp_ip_hdr_len); 4763 } else { 4764 /* 4765 * only valid case when ipversion of listener and 4766 * eager differ is when listener is IPv6 and 4767 * eager is IPv4. 4768 * Eager header template has been initialized to the 4769 * maximum v4 header sizes, which includes space for 4770 * TCP and IP options. 4771 */ 4772 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4773 (tcp->tcp_ipversion == IPV4_VERSION)); 4774 ASSERT(tcp->tcp_iphc_len >= 4775 TCP_MAX_COMBINED_HEADER_LENGTH); 4776 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4777 /* copy IP header fields individually */ 4778 tcp->tcp_ipha->ipha_ttl = 4779 ltcp->tcp_ip6h->ip6_hops; 4780 bcopy(ltcp->tcp_tcph->th_lport, 4781 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4782 } 4783 4784 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4785 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4786 sizeof (in_port_t)); 4787 4788 if (ltcp->tcp_lport == 0) { 4789 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4790 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4791 sizeof (in_port_t)); 4792 } 4793 4794 if (tcp->tcp_ipversion == IPV4_VERSION) { 4795 ASSERT(ipha != NULL); 4796 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4797 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4798 4799 /* Source routing option copyover (reverse it) */ 4800 if (tcps->tcps_rev_src_routes) 4801 tcp_opt_reverse(tcp, ipha); 4802 } else { 4803 ASSERT(ip6h != NULL); 4804 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4805 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4806 } 4807 4808 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4809 ASSERT(!tcp->tcp_tconnind_started); 4810 /* 4811 * If the SYN contains a credential, it's a loopback packet; attach 4812 * the credential to the TPI message. 4813 */ 4814 mblk_copycred(tpi_mp, idmp); 4815 4816 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4817 4818 /* Inherit the listener's SSL protection state */ 4819 4820 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4821 kssl_hold_ent(tcp->tcp_kssl_ent); 4822 tcp->tcp_kssl_pending = B_TRUE; 4823 } 4824 4825 /* Inherit the listener's non-STREAMS flag */ 4826 if (IPCL_IS_NONSTR(lconnp)) { 4827 connp->conn_flags |= IPCL_NONSTR; 4828 } 4829 4830 return (0); 4831 } 4832 4833 4834 int 4835 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4836 tcph_t *tcph, mblk_t *idmp) 4837 { 4838 tcp_t *ltcp = lconnp->conn_tcp; 4839 tcp_t *tcp = connp->conn_tcp; 4840 sin_t sin; 4841 mblk_t *tpi_mp = NULL; 4842 int err; 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_bound_source_v6); 4877 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4878 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4879 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4880 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4881 4882 /* Inherit information from the "parent" */ 4883 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4884 tcp->tcp_family = ltcp->tcp_family; 4885 tcp->tcp_wq = ltcp->tcp_wq; 4886 tcp->tcp_rq = ltcp->tcp_rq; 4887 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4888 tcp->tcp_detached = B_TRUE; 4889 SOCK_CONNID_INIT(tcp->tcp_connid); 4890 if ((err = tcp_init_values(tcp)) != 0) { 4891 freemsg(tpi_mp); 4892 return (err); 4893 } 4894 4895 /* 4896 * Let's make sure that eager tcp template has enough space to 4897 * copy IPv4 listener's tcp template. Since the conn_t structure is 4898 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4899 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4900 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4901 * extension headers or with ip6i_t struct). Note that bcopy() below 4902 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4903 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4904 */ 4905 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4906 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4907 4908 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4909 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4910 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4911 tcp->tcp_ttl = ltcp->tcp_ttl; 4912 tcp->tcp_tos = ltcp->tcp_tos; 4913 4914 /* Copy the IP+TCP header template from listener to eager */ 4915 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4916 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4917 tcp->tcp_ip6h = NULL; 4918 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4919 tcp->tcp_ip_hdr_len); 4920 4921 /* Initialize the IP addresses and Ports */ 4922 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4923 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4924 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4925 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4926 4927 /* Source routing option copyover (reverse it) */ 4928 if (tcps->tcps_rev_src_routes) 4929 tcp_opt_reverse(tcp, ipha); 4930 4931 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4932 ASSERT(!tcp->tcp_tconnind_started); 4933 4934 /* 4935 * If the SYN contains a credential, it's a loopback packet; attach 4936 * the credential to the TPI message. 4937 */ 4938 mblk_copycred(tpi_mp, idmp); 4939 4940 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4941 4942 /* Inherit the listener's SSL protection state */ 4943 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4944 kssl_hold_ent(tcp->tcp_kssl_ent); 4945 tcp->tcp_kssl_pending = B_TRUE; 4946 } 4947 4948 /* Inherit the listener's non-STREAMS flag */ 4949 if (IPCL_IS_NONSTR(lconnp)) { 4950 connp->conn_flags |= IPCL_NONSTR; 4951 } 4952 4953 return (0); 4954 } 4955 4956 /* 4957 * sets up conn for ipsec. 4958 * if the first mblk is M_CTL it is consumed and mpp is updated. 4959 * in case of error mpp is freed. 4960 */ 4961 conn_t * 4962 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4963 { 4964 conn_t *connp = tcp->tcp_connp; 4965 conn_t *econnp; 4966 squeue_t *new_sqp; 4967 mblk_t *first_mp = *mpp; 4968 mblk_t *mp = *mpp; 4969 boolean_t mctl_present = B_FALSE; 4970 uint_t ipvers; 4971 4972 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4973 if (econnp == NULL) { 4974 freemsg(first_mp); 4975 return (NULL); 4976 } 4977 if (DB_TYPE(mp) == M_CTL) { 4978 if (mp->b_cont == NULL || 4979 mp->b_cont->b_datap->db_type != M_DATA) { 4980 freemsg(first_mp); 4981 return (NULL); 4982 } 4983 mp = mp->b_cont; 4984 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4985 freemsg(first_mp); 4986 return (NULL); 4987 } 4988 4989 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4990 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4991 mctl_present = B_TRUE; 4992 } else { 4993 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4994 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4995 } 4996 4997 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4998 DB_CKSUMSTART(mp) = 0; 4999 5000 ASSERT(OK_32PTR(mp->b_rptr)); 5001 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5002 if (ipvers == IPV4_VERSION) { 5003 uint16_t *up; 5004 uint32_t ports; 5005 ipha_t *ipha; 5006 5007 ipha = (ipha_t *)mp->b_rptr; 5008 up = (uint16_t *)((uchar_t *)ipha + 5009 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5010 ports = *(uint32_t *)up; 5011 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5012 ipha->ipha_dst, ipha->ipha_src, ports); 5013 } else { 5014 uint16_t *up; 5015 uint32_t ports; 5016 uint16_t ip_hdr_len; 5017 uint8_t *nexthdrp; 5018 ip6_t *ip6h; 5019 tcph_t *tcph; 5020 5021 ip6h = (ip6_t *)mp->b_rptr; 5022 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5023 ip_hdr_len = IPV6_HDR_LEN; 5024 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5025 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5026 CONN_DEC_REF(econnp); 5027 freemsg(first_mp); 5028 return (NULL); 5029 } 5030 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5031 up = (uint16_t *)tcph->th_lport; 5032 ports = *(uint32_t *)up; 5033 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5034 ip6h->ip6_dst, ip6h->ip6_src, ports); 5035 } 5036 5037 /* 5038 * The caller already ensured that there is a sqp present. 5039 */ 5040 econnp->conn_sqp = new_sqp; 5041 econnp->conn_initial_sqp = new_sqp; 5042 5043 if (connp->conn_policy != NULL) { 5044 ipsec_in_t *ii; 5045 ii = (ipsec_in_t *)(first_mp->b_rptr); 5046 ASSERT(ii->ipsec_in_policy == NULL); 5047 IPPH_REFHOLD(connp->conn_policy); 5048 ii->ipsec_in_policy = connp->conn_policy; 5049 5050 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5051 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5052 CONN_DEC_REF(econnp); 5053 freemsg(first_mp); 5054 return (NULL); 5055 } 5056 } 5057 5058 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5059 CONN_DEC_REF(econnp); 5060 freemsg(first_mp); 5061 return (NULL); 5062 } 5063 5064 /* 5065 * If we know we have some policy, pass the "IPSEC" 5066 * options size TCP uses this adjust the MSS. 5067 */ 5068 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5069 if (mctl_present) { 5070 freeb(first_mp); 5071 *mpp = mp; 5072 } 5073 5074 return (econnp); 5075 } 5076 5077 /* 5078 * tcp_get_conn/tcp_free_conn 5079 * 5080 * tcp_get_conn is used to get a clean tcp connection structure. 5081 * It tries to reuse the connections put on the freelist by the 5082 * time_wait_collector failing which it goes to kmem_cache. This 5083 * way has two benefits compared to just allocating from and 5084 * freeing to kmem_cache. 5085 * 1) The time_wait_collector can free (which includes the cleanup) 5086 * outside the squeue. So when the interrupt comes, we have a clean 5087 * connection sitting in the freelist. Obviously, this buys us 5088 * performance. 5089 * 5090 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5091 * has multiple disadvantages - tying up the squeue during alloc, and the 5092 * fact that IPSec policy initialization has to happen here which 5093 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5094 * But allocating the conn/tcp in IP land is also not the best since 5095 * we can't check the 'q' and 'q0' which are protected by squeue and 5096 * blindly allocate memory which might have to be freed here if we are 5097 * not allowed to accept the connection. By using the freelist and 5098 * putting the conn/tcp back in freelist, we don't pay a penalty for 5099 * allocating memory without checking 'q/q0' and freeing it if we can't 5100 * accept the connection. 5101 * 5102 * Care should be taken to put the conn back in the same squeue's freelist 5103 * from which it was allocated. Best results are obtained if conn is 5104 * allocated from listener's squeue and freed to the same. Time wait 5105 * collector will free up the freelist is the connection ends up sitting 5106 * there for too long. 5107 */ 5108 void * 5109 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5110 { 5111 tcp_t *tcp = NULL; 5112 conn_t *connp = NULL; 5113 squeue_t *sqp = (squeue_t *)arg; 5114 tcp_squeue_priv_t *tcp_time_wait; 5115 netstack_t *ns; 5116 5117 tcp_time_wait = 5118 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5119 5120 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5121 tcp = tcp_time_wait->tcp_free_list; 5122 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5123 if (tcp != NULL) { 5124 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5125 tcp_time_wait->tcp_free_list_cnt--; 5126 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5127 tcp->tcp_time_wait_next = NULL; 5128 connp = tcp->tcp_connp; 5129 connp->conn_flags |= IPCL_REUSED; 5130 5131 ASSERT(tcp->tcp_tcps == NULL); 5132 ASSERT(connp->conn_netstack == NULL); 5133 ASSERT(tcp->tcp_rsrv_mp != NULL); 5134 ns = tcps->tcps_netstack; 5135 netstack_hold(ns); 5136 connp->conn_netstack = ns; 5137 tcp->tcp_tcps = tcps; 5138 TCPS_REFHOLD(tcps); 5139 ipcl_globalhash_insert(connp); 5140 return ((void *)connp); 5141 } 5142 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5143 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5144 tcps->tcps_netstack)) == NULL) 5145 return (NULL); 5146 tcp = connp->conn_tcp; 5147 /* 5148 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5149 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5150 */ 5151 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5152 ipcl_conn_destroy(connp); 5153 return (NULL); 5154 } 5155 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5156 tcp->tcp_tcps = tcps; 5157 TCPS_REFHOLD(tcps); 5158 5159 return ((void *)connp); 5160 } 5161 5162 /* 5163 * Update the cached label for the given tcp_t. This should be called once per 5164 * connection, and before any packets are sent or tcp_process_options is 5165 * invoked. Returns B_FALSE if the correct label could not be constructed. 5166 */ 5167 static boolean_t 5168 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5169 { 5170 conn_t *connp = tcp->tcp_connp; 5171 5172 if (tcp->tcp_ipversion == IPV4_VERSION) { 5173 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5174 int added; 5175 5176 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5177 connp->conn_mac_exempt, 5178 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5179 return (B_FALSE); 5180 5181 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5182 if (added == -1) 5183 return (B_FALSE); 5184 tcp->tcp_hdr_len += added; 5185 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5186 tcp->tcp_ip_hdr_len += added; 5187 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5188 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5189 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5190 tcp->tcp_hdr_len); 5191 if (added == -1) 5192 return (B_FALSE); 5193 tcp->tcp_hdr_len += added; 5194 tcp->tcp_tcph = (tcph_t *) 5195 ((uchar_t *)tcp->tcp_tcph + added); 5196 tcp->tcp_ip_hdr_len += added; 5197 } 5198 } else { 5199 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5200 5201 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5202 connp->conn_mac_exempt, 5203 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5204 return (B_FALSE); 5205 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5206 &tcp->tcp_label_len, optbuf) != 0) 5207 return (B_FALSE); 5208 if (tcp_build_hdrs(tcp) != 0) 5209 return (B_FALSE); 5210 } 5211 5212 connp->conn_ulp_labeled = 1; 5213 5214 return (B_TRUE); 5215 } 5216 5217 /* BEGIN CSTYLED */ 5218 /* 5219 * 5220 * The sockfs ACCEPT path: 5221 * ======================= 5222 * 5223 * The eager is now established in its own perimeter as soon as SYN is 5224 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5225 * completes the accept processing on the acceptor STREAM. The sending 5226 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5227 * listener but a TLI/XTI listener completes the accept processing 5228 * on the listener perimeter. 5229 * 5230 * Common control flow for 3 way handshake: 5231 * ---------------------------------------- 5232 * 5233 * incoming SYN (listener perimeter) -> tcp_rput_data() 5234 * -> tcp_conn_request() 5235 * 5236 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5237 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5238 * 5239 * Sockfs ACCEPT Path: 5240 * ------------------- 5241 * 5242 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5243 * as STREAM entry point) 5244 * 5245 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5246 * 5247 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5248 * association (we are not behind eager's squeue but sockfs is protecting us 5249 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5250 * is changed to point at tcp_wput(). 5251 * 5252 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5253 * listener (done on listener's perimeter). 5254 * 5255 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5256 * accept. 5257 * 5258 * TLI/XTI client ACCEPT path: 5259 * --------------------------- 5260 * 5261 * soaccept() sends T_CONN_RES on the listener STREAM. 5262 * 5263 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5264 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5265 * 5266 * Locks: 5267 * ====== 5268 * 5269 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5270 * and listeners->tcp_eager_next_q. 5271 * 5272 * Referencing: 5273 * ============ 5274 * 5275 * 1) We start out in tcp_conn_request by eager placing a ref on 5276 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5277 * 5278 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5279 * doing so we place a ref on the eager. This ref is finally dropped at the 5280 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5281 * reference is dropped by the squeue framework. 5282 * 5283 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5284 * 5285 * The reference must be released by the same entity that added the reference 5286 * In the above scheme, the eager is the entity that adds and releases the 5287 * references. Note that tcp_accept_finish executes in the squeue of the eager 5288 * (albeit after it is attached to the acceptor stream). Though 1. executes 5289 * in the listener's squeue, the eager is nascent at this point and the 5290 * reference can be considered to have been added on behalf of the eager. 5291 * 5292 * Eager getting a Reset or listener closing: 5293 * ========================================== 5294 * 5295 * Once the listener and eager are linked, the listener never does the unlink. 5296 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5297 * a message on all eager perimeter. The eager then does the unlink, clears 5298 * any pointers to the listener's queue and drops the reference to the 5299 * listener. The listener waits in tcp_close outside the squeue until its 5300 * refcount has dropped to 1. This ensures that the listener has waited for 5301 * all eagers to clear their association with the listener. 5302 * 5303 * Similarly, if eager decides to go away, it can unlink itself and close. 5304 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5305 * the reference to eager is still valid because of the extra ref we put 5306 * in tcp_send_conn_ind. 5307 * 5308 * Listener can always locate the eager under the protection 5309 * of the listener->tcp_eager_lock, and then do a refhold 5310 * on the eager during the accept processing. 5311 * 5312 * The acceptor stream accesses the eager in the accept processing 5313 * based on the ref placed on eager before sending T_conn_ind. 5314 * The only entity that can negate this refhold is a listener close 5315 * which is mutually exclusive with an active acceptor stream. 5316 * 5317 * Eager's reference on the listener 5318 * =================================== 5319 * 5320 * If the accept happens (even on a closed eager) the eager drops its 5321 * reference on the listener at the start of tcp_accept_finish. If the 5322 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5323 * the reference is dropped in tcp_closei_local. If the listener closes, 5324 * the reference is dropped in tcp_eager_kill. In all cases the reference 5325 * is dropped while executing in the eager's context (squeue). 5326 */ 5327 /* END CSTYLED */ 5328 5329 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5330 5331 /* 5332 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5333 * tcp_rput_data will not see any SYN packets. 5334 */ 5335 /* ARGSUSED */ 5336 void 5337 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5338 { 5339 tcph_t *tcph; 5340 uint32_t seg_seq; 5341 tcp_t *eager; 5342 uint_t ipvers; 5343 ipha_t *ipha; 5344 ip6_t *ip6h; 5345 int err; 5346 conn_t *econnp = NULL; 5347 squeue_t *new_sqp; 5348 mblk_t *mp1; 5349 uint_t ip_hdr_len; 5350 conn_t *connp = (conn_t *)arg; 5351 tcp_t *tcp = connp->conn_tcp; 5352 cred_t *credp; 5353 tcp_stack_t *tcps = tcp->tcp_tcps; 5354 ip_stack_t *ipst; 5355 5356 if (tcp->tcp_state != TCPS_LISTEN) 5357 goto error2; 5358 5359 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5360 5361 mutex_enter(&tcp->tcp_eager_lock); 5362 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5363 mutex_exit(&tcp->tcp_eager_lock); 5364 TCP_STAT(tcps, tcp_listendrop); 5365 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5366 if (tcp->tcp_debug) { 5367 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5368 "tcp_conn_request: listen backlog (max=%d) " 5369 "overflow (%d pending) on %s", 5370 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5371 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5372 } 5373 goto error2; 5374 } 5375 5376 if (tcp->tcp_conn_req_cnt_q0 >= 5377 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5378 /* 5379 * Q0 is full. Drop a pending half-open req from the queue 5380 * to make room for the new SYN req. Also mark the time we 5381 * drop a SYN. 5382 * 5383 * A more aggressive defense against SYN attack will 5384 * be to set the "tcp_syn_defense" flag now. 5385 */ 5386 TCP_STAT(tcps, tcp_listendropq0); 5387 tcp->tcp_last_rcv_lbolt = lbolt64; 5388 if (!tcp_drop_q0(tcp)) { 5389 mutex_exit(&tcp->tcp_eager_lock); 5390 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5391 if (tcp->tcp_debug) { 5392 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5393 "tcp_conn_request: listen half-open queue " 5394 "(max=%d) full (%d pending) on %s", 5395 tcps->tcps_conn_req_max_q0, 5396 tcp->tcp_conn_req_cnt_q0, 5397 tcp_display(tcp, NULL, 5398 DISP_PORT_ONLY)); 5399 } 5400 goto error2; 5401 } 5402 } 5403 mutex_exit(&tcp->tcp_eager_lock); 5404 5405 /* 5406 * IP adds STRUIO_EAGER and ensures that the received packet is 5407 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5408 * link local address. If IPSec is enabled, db_struioflag has 5409 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5410 * otherwise an error case if neither of them is set. 5411 */ 5412 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5413 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5414 DB_CKSUMSTART(mp) = 0; 5415 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5416 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5417 if (econnp == NULL) 5418 goto error2; 5419 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5420 econnp->conn_sqp = new_sqp; 5421 econnp->conn_initial_sqp = new_sqp; 5422 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5423 /* 5424 * mp is updated in tcp_get_ipsec_conn(). 5425 */ 5426 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5427 if (econnp == NULL) { 5428 /* 5429 * mp freed by tcp_get_ipsec_conn. 5430 */ 5431 return; 5432 } 5433 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5434 } else { 5435 goto error2; 5436 } 5437 5438 ASSERT(DB_TYPE(mp) == M_DATA); 5439 5440 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5441 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5442 ASSERT(OK_32PTR(mp->b_rptr)); 5443 if (ipvers == IPV4_VERSION) { 5444 ipha = (ipha_t *)mp->b_rptr; 5445 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5446 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5447 } else { 5448 ip6h = (ip6_t *)mp->b_rptr; 5449 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5450 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5451 } 5452 5453 if (tcp->tcp_family == AF_INET) { 5454 ASSERT(ipvers == IPV4_VERSION); 5455 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5456 } else { 5457 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5458 } 5459 5460 if (err) 5461 goto error3; 5462 5463 eager = econnp->conn_tcp; 5464 5465 /* 5466 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that at close 5467 * time, we will always have that to send up. Otherwise, we need to do 5468 * special handling in case the allocation fails at that time. 5469 */ 5470 ASSERT(eager->tcp_ordrel_mp == NULL); 5471 if (!IPCL_IS_NONSTR(econnp) && 5472 (eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5473 goto error3; 5474 5475 /* Inherit various TCP parameters from the listener */ 5476 eager->tcp_naglim = tcp->tcp_naglim; 5477 eager->tcp_first_timer_threshold = 5478 tcp->tcp_first_timer_threshold; 5479 eager->tcp_second_timer_threshold = 5480 tcp->tcp_second_timer_threshold; 5481 5482 eager->tcp_first_ctimer_threshold = 5483 tcp->tcp_first_ctimer_threshold; 5484 eager->tcp_second_ctimer_threshold = 5485 tcp->tcp_second_ctimer_threshold; 5486 5487 /* 5488 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5489 * If it does not, the eager's receive window will be set to the 5490 * listener's receive window later in this function. 5491 */ 5492 eager->tcp_rwnd = 0; 5493 5494 /* 5495 * Inherit listener's tcp_init_cwnd. Need to do this before 5496 * calling tcp_process_options() where tcp_mss_set() is called 5497 * to set the initial cwnd. 5498 */ 5499 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5500 5501 /* 5502 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5503 * zone id before the accept is completed in tcp_wput_accept(). 5504 */ 5505 econnp->conn_zoneid = connp->conn_zoneid; 5506 econnp->conn_allzones = connp->conn_allzones; 5507 5508 /* Copy nexthop information from listener to eager */ 5509 if (connp->conn_nexthop_set) { 5510 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5511 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5512 } 5513 5514 /* 5515 * TSOL: tsol_input_proc() needs the eager's cred before the 5516 * eager is accepted 5517 */ 5518 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5519 crhold(credp); 5520 5521 /* 5522 * If the caller has the process-wide flag set, then default to MAC 5523 * exempt mode. This allows read-down to unlabeled hosts. 5524 */ 5525 if (getpflags(NET_MAC_AWARE, credp) != 0) 5526 econnp->conn_mac_exempt = B_TRUE; 5527 5528 if (is_system_labeled()) { 5529 cred_t *cr; 5530 5531 if (connp->conn_mlp_type != mlptSingle) { 5532 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5533 if (cr != NULL) 5534 crhold(cr); 5535 else 5536 cr = econnp->conn_cred; 5537 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5538 econnp, cred_t *, cr) 5539 } else { 5540 cr = econnp->conn_cred; 5541 DTRACE_PROBE2(syn_accept, conn_t *, 5542 econnp, cred_t *, cr) 5543 } 5544 5545 if (!tcp_update_label(eager, cr)) { 5546 DTRACE_PROBE3( 5547 tx__ip__log__error__connrequest__tcp, 5548 char *, "eager connp(1) label on SYN mp(2) failed", 5549 conn_t *, econnp, mblk_t *, mp); 5550 goto error3; 5551 } 5552 } 5553 5554 eager->tcp_hard_binding = B_TRUE; 5555 5556 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5557 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5558 5559 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5560 if (err != 0) { 5561 tcp_bind_hash_remove(eager); 5562 goto error3; 5563 } 5564 5565 /* 5566 * No need to check for multicast destination since ip will only pass 5567 * up multicasts to those that have expressed interest 5568 * TODO: what about rejecting broadcasts? 5569 * Also check that source is not a multicast or broadcast address. 5570 */ 5571 eager->tcp_state = TCPS_SYN_RCVD; 5572 5573 5574 /* 5575 * There should be no ire in the mp as we are being called after 5576 * receiving the SYN. 5577 */ 5578 ASSERT(tcp_ire_mp(&mp) == NULL); 5579 5580 /* 5581 * Adapt our mss, ttl, ... according to information provided in IRE. 5582 */ 5583 5584 if (tcp_adapt_ire(eager, NULL) == 0) { 5585 /* Undo the bind_hash_insert */ 5586 tcp_bind_hash_remove(eager); 5587 goto error3; 5588 } 5589 5590 /* Process all TCP options. */ 5591 tcp_process_options(eager, tcph); 5592 5593 /* Is the other end ECN capable? */ 5594 if (tcps->tcps_ecn_permitted >= 1 && 5595 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5596 eager->tcp_ecn_ok = B_TRUE; 5597 } 5598 5599 /* 5600 * listener->tcp_rq->q_hiwat should be the default window size or a 5601 * window size changed via SO_RCVBUF option. First round up the 5602 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5603 * scale option value if needed. Call tcp_rwnd_set() to finish the 5604 * setting. 5605 * 5606 * Note if there is a rpipe metric associated with the remote host, 5607 * we should not inherit receive window size from listener. 5608 */ 5609 eager->tcp_rwnd = MSS_ROUNDUP( 5610 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5611 eager->tcp_rwnd), eager->tcp_mss); 5612 if (eager->tcp_snd_ws_ok) 5613 tcp_set_ws_value(eager); 5614 /* 5615 * Note that this is the only place tcp_rwnd_set() is called for 5616 * accepting a connection. We need to call it here instead of 5617 * after the 3-way handshake because we need to tell the other 5618 * side our rwnd in the SYN-ACK segment. 5619 */ 5620 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5621 5622 /* 5623 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5624 * via soaccept()->soinheritoptions() which essentially applies 5625 * all the listener options to the new STREAM. The options that we 5626 * need to take care of are: 5627 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5628 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5629 * SO_SNDBUF, SO_RCVBUF. 5630 * 5631 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5632 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5633 * tcp_maxpsz_set() gets called later from 5634 * tcp_accept_finish(), the option takes effect. 5635 * 5636 */ 5637 /* Set the TCP options */ 5638 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5639 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5640 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5641 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5642 eager->tcp_oobinline = tcp->tcp_oobinline; 5643 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5644 eager->tcp_broadcast = tcp->tcp_broadcast; 5645 eager->tcp_useloopback = tcp->tcp_useloopback; 5646 eager->tcp_dontroute = tcp->tcp_dontroute; 5647 eager->tcp_debug = tcp->tcp_debug; 5648 eager->tcp_linger = tcp->tcp_linger; 5649 eager->tcp_lingertime = tcp->tcp_lingertime; 5650 if (tcp->tcp_ka_enabled) 5651 eager->tcp_ka_enabled = 1; 5652 5653 /* Set the IP options */ 5654 econnp->conn_broadcast = connp->conn_broadcast; 5655 econnp->conn_loopback = connp->conn_loopback; 5656 econnp->conn_dontroute = connp->conn_dontroute; 5657 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5658 5659 /* Put a ref on the listener for the eager. */ 5660 CONN_INC_REF(connp); 5661 mutex_enter(&tcp->tcp_eager_lock); 5662 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5663 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5664 tcp->tcp_eager_next_q0 = eager; 5665 eager->tcp_eager_prev_q0 = tcp; 5666 5667 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5668 eager->tcp_listener = tcp; 5669 eager->tcp_saved_listener = tcp; 5670 5671 /* 5672 * Tag this detached tcp vector for later retrieval 5673 * by our listener client in tcp_accept(). 5674 */ 5675 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5676 tcp->tcp_conn_req_cnt_q0++; 5677 if (++tcp->tcp_conn_req_seqnum == -1) { 5678 /* 5679 * -1 is "special" and defined in TPI as something 5680 * that should never be used in T_CONN_IND 5681 */ 5682 ++tcp->tcp_conn_req_seqnum; 5683 } 5684 mutex_exit(&tcp->tcp_eager_lock); 5685 5686 if (tcp->tcp_syn_defense) { 5687 /* Don't drop the SYN that comes from a good IP source */ 5688 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5689 if (addr_cache != NULL && eager->tcp_remote == 5690 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5691 eager->tcp_dontdrop = B_TRUE; 5692 } 5693 } 5694 5695 /* 5696 * We need to insert the eager in its own perimeter but as soon 5697 * as we do that, we expose the eager to the classifier and 5698 * should not touch any field outside the eager's perimeter. 5699 * So do all the work necessary before inserting the eager 5700 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5701 * will succeed but undo everything if it fails. 5702 */ 5703 seg_seq = ABE32_TO_U32(tcph->th_seq); 5704 eager->tcp_irs = seg_seq; 5705 eager->tcp_rack = seg_seq; 5706 eager->tcp_rnxt = seg_seq + 1; 5707 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5708 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5709 eager->tcp_state = TCPS_SYN_RCVD; 5710 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5711 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5712 if (mp1 == NULL) { 5713 /* 5714 * Increment the ref count as we are going to 5715 * enqueueing an mp in squeue 5716 */ 5717 CONN_INC_REF(econnp); 5718 goto error; 5719 } 5720 5721 /* 5722 * Note that in theory this should use the current pid 5723 * so that getpeerucred on the client returns the actual listener 5724 * that does accept. But accept() hasn't been called yet. We could use 5725 * the pid of the process that did bind/listen on the server. 5726 * However, with common usage like inetd() the bind/listen can be done 5727 * by a different process than the accept(). 5728 * Hence we do the simple thing of using the open pid here. 5729 * Note that db_credp is set later in tcp_send_data(). 5730 */ 5731 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5732 eager->tcp_cpid = tcp->tcp_cpid; 5733 eager->tcp_open_time = lbolt64; 5734 5735 /* 5736 * We need to start the rto timer. In normal case, we start 5737 * the timer after sending the packet on the wire (or at 5738 * least believing that packet was sent by waiting for 5739 * CALL_IP_WPUT() to return). Since this is the first packet 5740 * being sent on the wire for the eager, our initial tcp_rto 5741 * is at least tcp_rexmit_interval_min which is a fairly 5742 * large value to allow the algorithm to adjust slowly to large 5743 * fluctuations of RTT during first few transmissions. 5744 * 5745 * Starting the timer first and then sending the packet in this 5746 * case shouldn't make much difference since tcp_rexmit_interval_min 5747 * is of the order of several 100ms and starting the timer 5748 * first and then sending the packet will result in difference 5749 * of few micro seconds. 5750 * 5751 * Without this optimization, we are forced to hold the fanout 5752 * lock across the ipcl_bind_insert() and sending the packet 5753 * so that we don't race against an incoming packet (maybe RST) 5754 * for this eager. 5755 * 5756 * It is necessary to acquire an extra reference on the eager 5757 * at this point and hold it until after tcp_send_data() to 5758 * ensure against an eager close race. 5759 */ 5760 5761 CONN_INC_REF(eager->tcp_connp); 5762 5763 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5764 5765 /* 5766 * Insert the eager in its own perimeter now. We are ready to deal 5767 * with any packets on eager. 5768 */ 5769 if (eager->tcp_ipversion == IPV4_VERSION) { 5770 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5771 goto error; 5772 } 5773 } else { 5774 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5775 goto error; 5776 } 5777 } 5778 5779 /* mark conn as fully-bound */ 5780 econnp->conn_fully_bound = B_TRUE; 5781 5782 /* Send the SYN-ACK */ 5783 tcp_send_data(eager, eager->tcp_wq, mp1); 5784 CONN_DEC_REF(eager->tcp_connp); 5785 freemsg(mp); 5786 5787 return; 5788 error: 5789 freemsg(mp1); 5790 eager->tcp_closemp_used = B_TRUE; 5791 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5792 mp1 = &eager->tcp_closemp; 5793 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5794 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5795 5796 /* 5797 * If a connection already exists, send the mp to that connections so 5798 * that it can be appropriately dealt with. 5799 */ 5800 ipst = tcps->tcps_netstack->netstack_ip; 5801 5802 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5803 if (!IPCL_IS_CONNECTED(econnp)) { 5804 /* 5805 * Something bad happened. ipcl_conn_insert() 5806 * failed because a connection already existed 5807 * in connected hash but we can't find it 5808 * anymore (someone blew it away). Just 5809 * free this message and hopefully remote 5810 * will retransmit at which time the SYN can be 5811 * treated as a new connection or dealth with 5812 * a TH_RST if a connection already exists. 5813 */ 5814 CONN_DEC_REF(econnp); 5815 freemsg(mp); 5816 } else { 5817 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5818 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5819 } 5820 } else { 5821 /* Nobody wants this packet */ 5822 freemsg(mp); 5823 } 5824 return; 5825 error3: 5826 CONN_DEC_REF(econnp); 5827 error2: 5828 freemsg(mp); 5829 } 5830 5831 /* 5832 * In an ideal case of vertical partition in NUMA architecture, its 5833 * beneficial to have the listener and all the incoming connections 5834 * tied to the same squeue. The other constraint is that incoming 5835 * connections should be tied to the squeue attached to interrupted 5836 * CPU for obvious locality reason so this leaves the listener to 5837 * be tied to the same squeue. Our only problem is that when listener 5838 * is binding, the CPU that will get interrupted by the NIC whose 5839 * IP address the listener is binding to is not even known. So 5840 * the code below allows us to change that binding at the time the 5841 * CPU is interrupted by virtue of incoming connection's squeue. 5842 * 5843 * This is usefull only in case of a listener bound to a specific IP 5844 * address. For other kind of listeners, they get bound the 5845 * very first time and there is no attempt to rebind them. 5846 */ 5847 void 5848 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5849 { 5850 conn_t *connp = (conn_t *)arg; 5851 squeue_t *sqp = (squeue_t *)arg2; 5852 squeue_t *new_sqp; 5853 uint32_t conn_flags; 5854 5855 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5856 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5857 } else { 5858 goto done; 5859 } 5860 5861 if (connp->conn_fanout == NULL) 5862 goto done; 5863 5864 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5865 mutex_enter(&connp->conn_fanout->connf_lock); 5866 mutex_enter(&connp->conn_lock); 5867 /* 5868 * No one from read or write side can access us now 5869 * except for already queued packets on this squeue. 5870 * But since we haven't changed the squeue yet, they 5871 * can't execute. If they are processed after we have 5872 * changed the squeue, they are sent back to the 5873 * correct squeue down below. 5874 * But a listner close can race with processing of 5875 * incoming SYN. If incoming SYN processing changes 5876 * the squeue then the listener close which is waiting 5877 * to enter the squeue would operate on the wrong 5878 * squeue. Hence we don't change the squeue here unless 5879 * the refcount is exactly the minimum refcount. The 5880 * minimum refcount of 4 is counted as - 1 each for 5881 * TCP and IP, 1 for being in the classifier hash, and 5882 * 1 for the mblk being processed. 5883 */ 5884 5885 if (connp->conn_ref != 4 || 5886 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5887 mutex_exit(&connp->conn_lock); 5888 mutex_exit(&connp->conn_fanout->connf_lock); 5889 goto done; 5890 } 5891 if (connp->conn_sqp != new_sqp) { 5892 while (connp->conn_sqp != new_sqp) 5893 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5894 } 5895 5896 do { 5897 conn_flags = connp->conn_flags; 5898 conn_flags |= IPCL_FULLY_BOUND; 5899 (void) cas32(&connp->conn_flags, connp->conn_flags, 5900 conn_flags); 5901 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5902 5903 mutex_exit(&connp->conn_fanout->connf_lock); 5904 mutex_exit(&connp->conn_lock); 5905 } 5906 5907 done: 5908 if (connp->conn_sqp != sqp) { 5909 CONN_INC_REF(connp); 5910 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5911 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5912 } else { 5913 tcp_conn_request(connp, mp, sqp); 5914 } 5915 } 5916 5917 /* 5918 * Successful connect request processing begins when our client passes 5919 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5920 * our T_OK_ACK reply message upstream. The control flow looks like this: 5921 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5922 * upstream <- tcp_rput() <- IP 5923 * After various error checks are completed, tcp_tpi_connect() lays 5924 * the target address and port into the composite header template, 5925 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5926 * request followed by an IRE request, and passes the three mblk message 5927 * down to IP looking like this: 5928 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5929 * Processing continues in tcp_rput() when we receive the following message: 5930 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5931 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5932 * to fire off the connection request, and then passes the T_OK_ACK mblk 5933 * upstream that we filled in below. There are, of course, numerous 5934 * error conditions along the way which truncate the processing described 5935 * above. 5936 */ 5937 static void 5938 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5939 { 5940 sin_t *sin; 5941 queue_t *q = tcp->tcp_wq; 5942 struct T_conn_req *tcr; 5943 struct sockaddr *sa; 5944 socklen_t len; 5945 int error; 5946 cred_t *cr; 5947 pid_t cpid; 5948 5949 /* 5950 * All Solaris components should pass a db_credp 5951 * for this TPI message, hence we ASSERT. 5952 * But in case there is some other M_PROTO that looks 5953 * like a TPI message sent by some other kernel 5954 * component, we check and return an error. 5955 */ 5956 cr = msg_getcred(mp, &cpid); 5957 ASSERT(cr != NULL); 5958 if (cr == NULL) { 5959 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5960 return; 5961 } 5962 5963 tcr = (struct T_conn_req *)mp->b_rptr; 5964 5965 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5966 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5967 tcp_err_ack(tcp, mp, TPROTO, 0); 5968 return; 5969 } 5970 5971 /* 5972 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5973 * will always have that to send up. Otherwise, we need to do 5974 * special handling in case the allocation fails at that time. 5975 * If the end point is TPI, the tcp_t can be reused and the 5976 * tcp_ordrel_mp may be allocated already. 5977 */ 5978 if (tcp->tcp_ordrel_mp == NULL) { 5979 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5980 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5981 return; 5982 } 5983 } 5984 5985 /* 5986 * Determine packet type based on type of address passed in 5987 * the request should contain an IPv4 or IPv6 address. 5988 * Make sure that address family matches the type of 5989 * family of the the address passed down 5990 */ 5991 switch (tcr->DEST_length) { 5992 default: 5993 tcp_err_ack(tcp, mp, TBADADDR, 0); 5994 return; 5995 5996 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5997 /* 5998 * XXX: The check for valid DEST_length was not there 5999 * in earlier releases and some buggy 6000 * TLI apps (e.g Sybase) got away with not feeding 6001 * in sin_zero part of address. 6002 * We allow that bug to keep those buggy apps humming. 6003 * Test suites require the check on DEST_length. 6004 * We construct a new mblk with valid DEST_length 6005 * free the original so the rest of the code does 6006 * not have to keep track of this special shorter 6007 * length address case. 6008 */ 6009 mblk_t *nmp; 6010 struct T_conn_req *ntcr; 6011 sin_t *nsin; 6012 6013 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6014 tcr->OPT_length, BPRI_HI); 6015 if (nmp == NULL) { 6016 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6017 return; 6018 } 6019 ntcr = (struct T_conn_req *)nmp->b_rptr; 6020 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6021 ntcr->PRIM_type = T_CONN_REQ; 6022 ntcr->DEST_length = sizeof (sin_t); 6023 ntcr->DEST_offset = sizeof (struct T_conn_req); 6024 6025 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6026 *nsin = sin_null; 6027 /* Get pointer to shorter address to copy from original mp */ 6028 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6029 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6030 if (sin == NULL || !OK_32PTR((char *)sin)) { 6031 freemsg(nmp); 6032 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6033 return; 6034 } 6035 nsin->sin_family = sin->sin_family; 6036 nsin->sin_port = sin->sin_port; 6037 nsin->sin_addr = sin->sin_addr; 6038 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6039 nmp->b_wptr = (uchar_t *)&nsin[1]; 6040 if (tcr->OPT_length != 0) { 6041 ntcr->OPT_length = tcr->OPT_length; 6042 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6043 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6044 (uchar_t *)ntcr + ntcr->OPT_offset, 6045 tcr->OPT_length); 6046 nmp->b_wptr += tcr->OPT_length; 6047 } 6048 freemsg(mp); /* original mp freed */ 6049 mp = nmp; /* re-initialize original variables */ 6050 tcr = ntcr; 6051 } 6052 /* FALLTHRU */ 6053 6054 case sizeof (sin_t): 6055 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6056 sizeof (sin_t)); 6057 len = sizeof (sin_t); 6058 break; 6059 6060 case sizeof (sin6_t): 6061 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6062 sizeof (sin6_t)); 6063 len = sizeof (sin6_t); 6064 break; 6065 } 6066 6067 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6068 if (error != 0) { 6069 tcp_err_ack(tcp, mp, TSYSERR, error); 6070 return; 6071 } 6072 6073 /* 6074 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6075 * should key on their sequence number and cut them loose. 6076 */ 6077 6078 /* 6079 * If options passed in, feed it for verification and handling 6080 */ 6081 if (tcr->OPT_length != 0) { 6082 mblk_t *ok_mp; 6083 mblk_t *discon_mp; 6084 mblk_t *conn_opts_mp; 6085 int t_error, sys_error, do_disconnect; 6086 6087 conn_opts_mp = NULL; 6088 6089 if (tcp_conprim_opt_process(tcp, mp, 6090 &do_disconnect, &t_error, &sys_error) < 0) { 6091 if (do_disconnect) { 6092 ASSERT(t_error == 0 && sys_error == 0); 6093 discon_mp = mi_tpi_discon_ind(NULL, 6094 ECONNREFUSED, 0); 6095 if (!discon_mp) { 6096 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6097 TSYSERR, ENOMEM); 6098 return; 6099 } 6100 ok_mp = mi_tpi_ok_ack_alloc(mp); 6101 if (!ok_mp) { 6102 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6103 TSYSERR, ENOMEM); 6104 return; 6105 } 6106 qreply(q, ok_mp); 6107 qreply(q, discon_mp); /* no flush! */ 6108 } else { 6109 ASSERT(t_error != 0); 6110 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6111 sys_error); 6112 } 6113 return; 6114 } 6115 /* 6116 * Success in setting options, the mp option buffer represented 6117 * by OPT_length/offset has been potentially modified and 6118 * contains results of option processing. We copy it in 6119 * another mp to save it for potentially influencing returning 6120 * it in T_CONN_CONN. 6121 */ 6122 if (tcr->OPT_length != 0) { /* there are resulting options */ 6123 conn_opts_mp = copyb(mp); 6124 if (!conn_opts_mp) { 6125 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6126 TSYSERR, ENOMEM); 6127 return; 6128 } 6129 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6130 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6131 /* 6132 * Note: 6133 * These resulting option negotiation can include any 6134 * end-to-end negotiation options but there no such 6135 * thing (yet?) in our TCP/IP. 6136 */ 6137 } 6138 } 6139 6140 /* call the non-TPI version */ 6141 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6142 if (error < 0) { 6143 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6144 } else if (error > 0) { 6145 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6146 } else { 6147 mp = mi_tpi_ok_ack_alloc(mp); 6148 } 6149 6150 /* 6151 * Note: Code below is the "failure" case 6152 */ 6153 /* return error ack and blow away saved option results if any */ 6154 connect_failed: 6155 if (mp != NULL) 6156 putnext(tcp->tcp_rq, mp); 6157 else { 6158 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6159 TSYSERR, ENOMEM); 6160 } 6161 } 6162 6163 /* 6164 * Handle connect to IPv4 destinations, including connections for AF_INET6 6165 * sockets connecting to IPv4 mapped IPv6 destinations. 6166 */ 6167 static int 6168 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6169 uint_t srcid, cred_t *cr, pid_t pid) 6170 { 6171 tcph_t *tcph; 6172 mblk_t *mp; 6173 ipaddr_t dstaddr = *dstaddrp; 6174 int32_t oldstate; 6175 uint16_t lport; 6176 int error = 0; 6177 tcp_stack_t *tcps = tcp->tcp_tcps; 6178 6179 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6180 6181 /* Check for attempt to connect to INADDR_ANY */ 6182 if (dstaddr == INADDR_ANY) { 6183 /* 6184 * SunOS 4.x and 4.3 BSD allow an application 6185 * to connect a TCP socket to INADDR_ANY. 6186 * When they do this, the kernel picks the 6187 * address of one interface and uses it 6188 * instead. The kernel usually ends up 6189 * picking the address of the loopback 6190 * interface. This is an undocumented feature. 6191 * However, we provide the same thing here 6192 * in order to have source and binary 6193 * compatibility with SunOS 4.x. 6194 * Update the T_CONN_REQ (sin/sin6) since it is used to 6195 * generate the T_CONN_CON. 6196 */ 6197 dstaddr = htonl(INADDR_LOOPBACK); 6198 *dstaddrp = dstaddr; 6199 } 6200 6201 /* Handle __sin6_src_id if socket not bound to an IP address */ 6202 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6203 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6204 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6205 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6206 tcp->tcp_ipha->ipha_src); 6207 } 6208 6209 /* 6210 * Don't let an endpoint connect to itself. Note that 6211 * the test here does not catch the case where the 6212 * source IP addr was left unspecified by the user. In 6213 * this case, the source addr is set in tcp_adapt_ire() 6214 * using the reply to the T_BIND message that we send 6215 * down to IP here and the check is repeated in tcp_rput_other. 6216 */ 6217 if (dstaddr == tcp->tcp_ipha->ipha_src && 6218 dstport == tcp->tcp_lport) { 6219 error = -TBADADDR; 6220 goto failed; 6221 } 6222 6223 tcp->tcp_ipha->ipha_dst = dstaddr; 6224 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6225 6226 /* 6227 * Massage a source route if any putting the first hop 6228 * in iph_dst. Compute a starting value for the checksum which 6229 * takes into account that the original iph_dst should be 6230 * included in the checksum but that ip will include the 6231 * first hop in the source route in the tcp checksum. 6232 */ 6233 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6234 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6235 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6236 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6237 if ((int)tcp->tcp_sum < 0) 6238 tcp->tcp_sum--; 6239 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6240 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6241 (tcp->tcp_sum >> 16)); 6242 tcph = tcp->tcp_tcph; 6243 *(uint16_t *)tcph->th_fport = dstport; 6244 tcp->tcp_fport = dstport; 6245 6246 oldstate = tcp->tcp_state; 6247 /* 6248 * At this point the remote destination address and remote port fields 6249 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6250 * have to see which state tcp was in so we can take apropriate action. 6251 */ 6252 if (oldstate == TCPS_IDLE) { 6253 /* 6254 * We support a quick connect capability here, allowing 6255 * clients to transition directly from IDLE to SYN_SENT 6256 * tcp_bindi will pick an unused port, insert the connection 6257 * in the bind hash and transition to BOUND state. 6258 */ 6259 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6260 tcp, B_TRUE); 6261 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6262 B_FALSE, B_FALSE); 6263 if (lport == 0) { 6264 error = -TNOADDR; 6265 goto failed; 6266 } 6267 } 6268 tcp->tcp_state = TCPS_SYN_SENT; 6269 6270 mp = allocb(sizeof (ire_t), BPRI_HI); 6271 if (mp == NULL) { 6272 tcp->tcp_state = oldstate; 6273 error = ENOMEM; 6274 goto failed; 6275 } 6276 6277 mp->b_wptr += sizeof (ire_t); 6278 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6279 tcp->tcp_hard_binding = 1; 6280 6281 /* 6282 * We need to make sure that the conn_recv is set to a non-null 6283 * value before we insert the conn_t into the classifier table. 6284 * This is to avoid a race with an incoming packet which does 6285 * an ipcl_classify(). 6286 */ 6287 tcp->tcp_connp->conn_recv = tcp_input; 6288 6289 if (tcp->tcp_family == AF_INET) { 6290 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6291 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6292 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6293 } else { 6294 in6_addr_t v6src; 6295 if (tcp->tcp_ipversion == IPV4_VERSION) { 6296 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6297 } else { 6298 v6src = tcp->tcp_ip6h->ip6_src; 6299 } 6300 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6301 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6302 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6303 } 6304 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6305 tcp->tcp_active_open = 1; 6306 6307 6308 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6309 failed: 6310 /* return error ack and blow away saved option results if any */ 6311 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6312 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6313 return (error); 6314 } 6315 6316 /* 6317 * Handle connect to IPv6 destinations. 6318 */ 6319 static int 6320 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6321 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6322 { 6323 tcph_t *tcph; 6324 mblk_t *mp; 6325 ip6_rthdr_t *rth; 6326 int32_t oldstate; 6327 uint16_t lport; 6328 tcp_stack_t *tcps = tcp->tcp_tcps; 6329 int error = 0; 6330 conn_t *connp = tcp->tcp_connp; 6331 6332 ASSERT(tcp->tcp_family == AF_INET6); 6333 6334 /* 6335 * If we're here, it means that the destination address is a native 6336 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6337 * reason why it might not be IPv6 is if the socket was bound to an 6338 * IPv4-mapped IPv6 address. 6339 */ 6340 if (tcp->tcp_ipversion != IPV6_VERSION) { 6341 return (-TBADADDR); 6342 } 6343 6344 /* 6345 * Interpret a zero destination to mean loopback. 6346 * Update the T_CONN_REQ (sin/sin6) since it is used to 6347 * generate the T_CONN_CON. 6348 */ 6349 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6350 *dstaddrp = ipv6_loopback; 6351 } 6352 6353 /* Handle __sin6_src_id if socket not bound to an IP address */ 6354 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6355 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6356 connp->conn_zoneid, tcps->tcps_netstack); 6357 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6358 } 6359 6360 /* 6361 * Take care of the scope_id now and add ip6i_t 6362 * if ip6i_t is not already allocated through TCP 6363 * sticky options. At this point tcp_ip6h does not 6364 * have dst info, thus use dstaddrp. 6365 */ 6366 if (scope_id != 0 && 6367 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6368 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6369 ip6i_t *ip6i; 6370 6371 ipp->ipp_ifindex = scope_id; 6372 ip6i = (ip6i_t *)tcp->tcp_iphc; 6373 6374 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6375 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6376 /* Already allocated */ 6377 ip6i->ip6i_flags |= IP6I_IFINDEX; 6378 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6379 ipp->ipp_fields |= IPPF_SCOPE_ID; 6380 } else { 6381 int reterr; 6382 6383 ipp->ipp_fields |= IPPF_SCOPE_ID; 6384 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6385 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6386 reterr = tcp_build_hdrs(tcp); 6387 if (reterr != 0) 6388 goto failed; 6389 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6390 } 6391 } 6392 6393 /* 6394 * Don't let an endpoint connect to itself. Note that 6395 * the test here does not catch the case where the 6396 * source IP addr was left unspecified by the user. In 6397 * this case, the source addr is set in tcp_adapt_ire() 6398 * using the reply to the T_BIND message that we send 6399 * down to IP here and the check is repeated in tcp_rput_other. 6400 */ 6401 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6402 (dstport == tcp->tcp_lport)) { 6403 error = -TBADADDR; 6404 goto failed; 6405 } 6406 6407 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6408 tcp->tcp_remote_v6 = *dstaddrp; 6409 tcp->tcp_ip6h->ip6_vcf = 6410 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6411 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6412 6413 /* 6414 * Massage a routing header (if present) putting the first hop 6415 * in ip6_dst. Compute a starting value for the checksum which 6416 * takes into account that the original ip6_dst should be 6417 * included in the checksum but that ip will include the 6418 * first hop in the source route in the tcp checksum. 6419 */ 6420 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6421 if (rth != NULL) { 6422 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6423 tcps->tcps_netstack); 6424 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6425 (tcp->tcp_sum >> 16)); 6426 } else { 6427 tcp->tcp_sum = 0; 6428 } 6429 6430 tcph = tcp->tcp_tcph; 6431 *(uint16_t *)tcph->th_fport = dstport; 6432 tcp->tcp_fport = dstport; 6433 6434 oldstate = tcp->tcp_state; 6435 /* 6436 * At this point the remote destination address and remote port fields 6437 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6438 * have to see which state tcp was in so we can take apropriate action. 6439 */ 6440 if (oldstate == TCPS_IDLE) { 6441 /* 6442 * We support a quick connect capability here, allowing 6443 * clients to transition directly from IDLE to SYN_SENT 6444 * tcp_bindi will pick an unused port, insert the connection 6445 * in the bind hash and transition to BOUND state. 6446 */ 6447 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6448 tcp, B_TRUE); 6449 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6450 B_FALSE, B_FALSE); 6451 if (lport == 0) { 6452 error = -TNOADDR; 6453 goto failed; 6454 } 6455 } 6456 tcp->tcp_state = TCPS_SYN_SENT; 6457 6458 mp = allocb(sizeof (ire_t), BPRI_HI); 6459 if (mp != NULL) { 6460 in6_addr_t v6src; 6461 6462 mp->b_wptr += sizeof (ire_t); 6463 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6464 6465 tcp->tcp_hard_binding = 1; 6466 6467 /* 6468 * We need to make sure that the conn_recv is set to a non-null 6469 * value before we insert the conn_t into the classifier table. 6470 * This is to avoid a race with an incoming packet which does 6471 * an ipcl_classify(). 6472 */ 6473 tcp->tcp_connp->conn_recv = tcp_input; 6474 6475 if (tcp->tcp_ipversion == IPV4_VERSION) { 6476 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6477 } else { 6478 v6src = tcp->tcp_ip6h->ip6_src; 6479 } 6480 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6481 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6482 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6483 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6484 tcp->tcp_active_open = 1; 6485 6486 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6487 } 6488 /* Error case */ 6489 tcp->tcp_state = oldstate; 6490 error = ENOMEM; 6491 6492 failed: 6493 /* return error ack and blow away saved option results if any */ 6494 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6495 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6496 return (error); 6497 } 6498 6499 /* 6500 * We need a stream q for detached closing tcp connections 6501 * to use. Our client hereby indicates that this q is the 6502 * one to use. 6503 */ 6504 static void 6505 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6506 { 6507 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6508 queue_t *q = tcp->tcp_wq; 6509 tcp_stack_t *tcps = tcp->tcp_tcps; 6510 6511 #ifdef NS_DEBUG 6512 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6513 tcps->tcps_netstack->netstack_stackid); 6514 #endif 6515 mp->b_datap->db_type = M_IOCACK; 6516 iocp->ioc_count = 0; 6517 mutex_enter(&tcps->tcps_g_q_lock); 6518 if (tcps->tcps_g_q != NULL) { 6519 mutex_exit(&tcps->tcps_g_q_lock); 6520 iocp->ioc_error = EALREADY; 6521 } else { 6522 int error = 0; 6523 conn_t *connp = tcp->tcp_connp; 6524 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6525 6526 tcps->tcps_g_q = tcp->tcp_rq; 6527 mutex_exit(&tcps->tcps_g_q_lock); 6528 iocp->ioc_error = 0; 6529 iocp->ioc_rval = 0; 6530 /* 6531 * We are passing tcp_sticky_ipp as NULL 6532 * as it is not useful for tcp_default queue 6533 * 6534 * Set conn_recv just in case. 6535 */ 6536 tcp->tcp_connp->conn_recv = tcp_conn_request; 6537 6538 ASSERT(connp->conn_af_isv6); 6539 connp->conn_ulp = IPPROTO_TCP; 6540 6541 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6542 NULL || connp->conn_mac_exempt) { 6543 error = -TBADADDR; 6544 } else { 6545 connp->conn_srcv6 = ipv6_all_zeros; 6546 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6547 } 6548 6549 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6550 } 6551 qreply(q, mp); 6552 } 6553 6554 static int 6555 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6556 { 6557 tcp_t *ltcp = NULL; 6558 conn_t *connp; 6559 tcp_stack_t *tcps = tcp->tcp_tcps; 6560 6561 /* 6562 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6563 * when the stream is in BOUND state. Do not send a reset, 6564 * since the destination IP address is not valid, and it can 6565 * be the initialized value of all zeros (broadcast address). 6566 * 6567 * XXX There won't be any pending bind request to IP. 6568 */ 6569 if (tcp->tcp_state <= TCPS_BOUND) { 6570 if (tcp->tcp_debug) { 6571 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6572 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6573 } 6574 return (TOUTSTATE); 6575 } 6576 6577 6578 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6579 6580 /* 6581 * According to TPI, for non-listeners, ignore seqnum 6582 * and disconnect. 6583 * Following interpretation of -1 seqnum is historical 6584 * and implied TPI ? (TPI only states that for T_CONN_IND, 6585 * a valid seqnum should not be -1). 6586 * 6587 * -1 means disconnect everything 6588 * regardless even on a listener. 6589 */ 6590 6591 int old_state = tcp->tcp_state; 6592 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6593 6594 /* 6595 * The connection can't be on the tcp_time_wait_head list 6596 * since it is not detached. 6597 */ 6598 ASSERT(tcp->tcp_time_wait_next == NULL); 6599 ASSERT(tcp->tcp_time_wait_prev == NULL); 6600 ASSERT(tcp->tcp_time_wait_expire == 0); 6601 ltcp = NULL; 6602 /* 6603 * If it used to be a listener, check to make sure no one else 6604 * has taken the port before switching back to LISTEN state. 6605 */ 6606 if (tcp->tcp_ipversion == IPV4_VERSION) { 6607 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6608 tcp->tcp_ipha->ipha_src, 6609 tcp->tcp_connp->conn_zoneid, ipst); 6610 if (connp != NULL) 6611 ltcp = connp->conn_tcp; 6612 } else { 6613 /* Allow tcp_bound_if listeners? */ 6614 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6615 &tcp->tcp_ip6h->ip6_src, 0, 6616 tcp->tcp_connp->conn_zoneid, ipst); 6617 if (connp != NULL) 6618 ltcp = connp->conn_tcp; 6619 } 6620 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6621 tcp->tcp_state = TCPS_LISTEN; 6622 } else if (old_state > TCPS_BOUND) { 6623 tcp->tcp_conn_req_max = 0; 6624 tcp->tcp_state = TCPS_BOUND; 6625 } 6626 if (ltcp != NULL) 6627 CONN_DEC_REF(ltcp->tcp_connp); 6628 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6629 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6630 } else if (old_state == TCPS_ESTABLISHED || 6631 old_state == TCPS_CLOSE_WAIT) { 6632 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6633 } 6634 6635 if (tcp->tcp_fused) 6636 tcp_unfuse(tcp); 6637 6638 mutex_enter(&tcp->tcp_eager_lock); 6639 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6640 (tcp->tcp_conn_req_cnt_q != 0)) { 6641 tcp_eager_cleanup(tcp, 0); 6642 } 6643 mutex_exit(&tcp->tcp_eager_lock); 6644 6645 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6646 tcp->tcp_rnxt, TH_RST | TH_ACK); 6647 6648 tcp_reinit(tcp); 6649 6650 return (0); 6651 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6652 return (TBADSEQ); 6653 } 6654 return (0); 6655 } 6656 6657 /* 6658 * Our client hereby directs us to reject the connection request 6659 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6660 * of sending the appropriate RST, not an ICMP error. 6661 */ 6662 static void 6663 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6664 { 6665 t_scalar_t seqnum; 6666 int error; 6667 6668 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6669 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6670 tcp_err_ack(tcp, mp, TPROTO, 0); 6671 return; 6672 } 6673 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6674 error = tcp_disconnect_common(tcp, seqnum); 6675 if (error != 0) 6676 tcp_err_ack(tcp, mp, error, 0); 6677 else { 6678 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6679 /* Send M_FLUSH according to TPI */ 6680 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6681 } 6682 mp = mi_tpi_ok_ack_alloc(mp); 6683 if (mp) 6684 putnext(tcp->tcp_rq, mp); 6685 } 6686 } 6687 6688 /* 6689 * Diagnostic routine used to return a string associated with the tcp state. 6690 * Note that if the caller does not supply a buffer, it will use an internal 6691 * static string. This means that if multiple threads call this function at 6692 * the same time, output can be corrupted... Note also that this function 6693 * does not check the size of the supplied buffer. The caller has to make 6694 * sure that it is big enough. 6695 */ 6696 static char * 6697 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6698 { 6699 char buf1[30]; 6700 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6701 char *buf; 6702 char *cp; 6703 in6_addr_t local, remote; 6704 char local_addrbuf[INET6_ADDRSTRLEN]; 6705 char remote_addrbuf[INET6_ADDRSTRLEN]; 6706 6707 if (sup_buf != NULL) 6708 buf = sup_buf; 6709 else 6710 buf = priv_buf; 6711 6712 if (tcp == NULL) 6713 return ("NULL_TCP"); 6714 switch (tcp->tcp_state) { 6715 case TCPS_CLOSED: 6716 cp = "TCP_CLOSED"; 6717 break; 6718 case TCPS_IDLE: 6719 cp = "TCP_IDLE"; 6720 break; 6721 case TCPS_BOUND: 6722 cp = "TCP_BOUND"; 6723 break; 6724 case TCPS_LISTEN: 6725 cp = "TCP_LISTEN"; 6726 break; 6727 case TCPS_SYN_SENT: 6728 cp = "TCP_SYN_SENT"; 6729 break; 6730 case TCPS_SYN_RCVD: 6731 cp = "TCP_SYN_RCVD"; 6732 break; 6733 case TCPS_ESTABLISHED: 6734 cp = "TCP_ESTABLISHED"; 6735 break; 6736 case TCPS_CLOSE_WAIT: 6737 cp = "TCP_CLOSE_WAIT"; 6738 break; 6739 case TCPS_FIN_WAIT_1: 6740 cp = "TCP_FIN_WAIT_1"; 6741 break; 6742 case TCPS_CLOSING: 6743 cp = "TCP_CLOSING"; 6744 break; 6745 case TCPS_LAST_ACK: 6746 cp = "TCP_LAST_ACK"; 6747 break; 6748 case TCPS_FIN_WAIT_2: 6749 cp = "TCP_FIN_WAIT_2"; 6750 break; 6751 case TCPS_TIME_WAIT: 6752 cp = "TCP_TIME_WAIT"; 6753 break; 6754 default: 6755 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6756 cp = buf1; 6757 break; 6758 } 6759 switch (format) { 6760 case DISP_ADDR_AND_PORT: 6761 if (tcp->tcp_ipversion == IPV4_VERSION) { 6762 /* 6763 * Note that we use the remote address in the tcp_b 6764 * structure. This means that it will print out 6765 * the real destination address, not the next hop's 6766 * address if source routing is used. 6767 */ 6768 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6769 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6770 6771 } else { 6772 local = tcp->tcp_ip_src_v6; 6773 remote = tcp->tcp_remote_v6; 6774 } 6775 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6776 sizeof (local_addrbuf)); 6777 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6778 sizeof (remote_addrbuf)); 6779 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6780 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6781 ntohs(tcp->tcp_fport), cp); 6782 break; 6783 case DISP_PORT_ONLY: 6784 default: 6785 (void) mi_sprintf(buf, "[%u, %u] %s", 6786 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6787 break; 6788 } 6789 6790 return (buf); 6791 } 6792 6793 /* 6794 * Called via squeue to get on to eager's perimeter. It sends a 6795 * TH_RST if eager is in the fanout table. The listener wants the 6796 * eager to disappear either by means of tcp_eager_blowoff() or 6797 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6798 * called (via squeue) if the eager cannot be inserted in the 6799 * fanout table in tcp_conn_request(). 6800 */ 6801 /* ARGSUSED */ 6802 void 6803 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6804 { 6805 conn_t *econnp = (conn_t *)arg; 6806 tcp_t *eager = econnp->conn_tcp; 6807 tcp_t *listener = eager->tcp_listener; 6808 tcp_stack_t *tcps = eager->tcp_tcps; 6809 6810 /* 6811 * We could be called because listener is closing. Since 6812 * the eager is using listener's queue's, its not safe. 6813 * Better use the default queue just to send the TH_RST 6814 * out. 6815 */ 6816 ASSERT(tcps->tcps_g_q != NULL); 6817 eager->tcp_rq = tcps->tcps_g_q; 6818 eager->tcp_wq = WR(tcps->tcps_g_q); 6819 6820 /* 6821 * An eager's conn_fanout will be NULL if it's a duplicate 6822 * for an existing 4-tuples in the conn fanout table. 6823 * We don't want to send an RST out in such case. 6824 */ 6825 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6826 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6827 eager, eager->tcp_snxt, 0, TH_RST); 6828 } 6829 6830 /* We are here because listener wants this eager gone */ 6831 if (listener != NULL) { 6832 mutex_enter(&listener->tcp_eager_lock); 6833 tcp_eager_unlink(eager); 6834 if (eager->tcp_tconnind_started) { 6835 /* 6836 * The eager has sent a conn_ind up to the 6837 * listener but listener decides to close 6838 * instead. We need to drop the extra ref 6839 * placed on eager in tcp_rput_data() before 6840 * sending the conn_ind to listener. 6841 */ 6842 CONN_DEC_REF(econnp); 6843 } 6844 mutex_exit(&listener->tcp_eager_lock); 6845 CONN_DEC_REF(listener->tcp_connp); 6846 } 6847 6848 if (eager->tcp_state > TCPS_BOUND) 6849 tcp_close_detached(eager); 6850 } 6851 6852 /* 6853 * Reset any eager connection hanging off this listener marked 6854 * with 'seqnum' and then reclaim it's resources. 6855 */ 6856 static boolean_t 6857 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6858 { 6859 tcp_t *eager; 6860 mblk_t *mp; 6861 tcp_stack_t *tcps = listener->tcp_tcps; 6862 6863 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6864 eager = listener; 6865 mutex_enter(&listener->tcp_eager_lock); 6866 do { 6867 eager = eager->tcp_eager_next_q; 6868 if (eager == NULL) { 6869 mutex_exit(&listener->tcp_eager_lock); 6870 return (B_FALSE); 6871 } 6872 } while (eager->tcp_conn_req_seqnum != seqnum); 6873 6874 if (eager->tcp_closemp_used) { 6875 mutex_exit(&listener->tcp_eager_lock); 6876 return (B_TRUE); 6877 } 6878 eager->tcp_closemp_used = B_TRUE; 6879 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6880 CONN_INC_REF(eager->tcp_connp); 6881 mutex_exit(&listener->tcp_eager_lock); 6882 mp = &eager->tcp_closemp; 6883 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6884 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6885 return (B_TRUE); 6886 } 6887 6888 /* 6889 * Reset any eager connection hanging off this listener 6890 * and then reclaim it's resources. 6891 */ 6892 static void 6893 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6894 { 6895 tcp_t *eager; 6896 mblk_t *mp; 6897 tcp_stack_t *tcps = listener->tcp_tcps; 6898 6899 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6900 6901 if (!q0_only) { 6902 /* First cleanup q */ 6903 TCP_STAT(tcps, tcp_eager_blowoff_q); 6904 eager = listener->tcp_eager_next_q; 6905 while (eager != NULL) { 6906 if (!eager->tcp_closemp_used) { 6907 eager->tcp_closemp_used = B_TRUE; 6908 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6909 CONN_INC_REF(eager->tcp_connp); 6910 mp = &eager->tcp_closemp; 6911 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6912 tcp_eager_kill, eager->tcp_connp, 6913 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6914 } 6915 eager = eager->tcp_eager_next_q; 6916 } 6917 } 6918 /* Then cleanup q0 */ 6919 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6920 eager = listener->tcp_eager_next_q0; 6921 while (eager != listener) { 6922 if (!eager->tcp_closemp_used) { 6923 eager->tcp_closemp_used = B_TRUE; 6924 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6925 CONN_INC_REF(eager->tcp_connp); 6926 mp = &eager->tcp_closemp; 6927 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6928 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6929 SQTAG_TCP_EAGER_CLEANUP_Q0); 6930 } 6931 eager = eager->tcp_eager_next_q0; 6932 } 6933 } 6934 6935 /* 6936 * If we are an eager connection hanging off a listener that hasn't 6937 * formally accepted the connection yet, get off his list and blow off 6938 * any data that we have accumulated. 6939 */ 6940 static void 6941 tcp_eager_unlink(tcp_t *tcp) 6942 { 6943 tcp_t *listener = tcp->tcp_listener; 6944 6945 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6946 ASSERT(listener != NULL); 6947 if (tcp->tcp_eager_next_q0 != NULL) { 6948 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6949 6950 /* Remove the eager tcp from q0 */ 6951 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6952 tcp->tcp_eager_prev_q0; 6953 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6954 tcp->tcp_eager_next_q0; 6955 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6956 listener->tcp_conn_req_cnt_q0--; 6957 6958 tcp->tcp_eager_next_q0 = NULL; 6959 tcp->tcp_eager_prev_q0 = NULL; 6960 6961 /* 6962 * Take the eager out, if it is in the list of droppable 6963 * eagers. 6964 */ 6965 MAKE_UNDROPPABLE(tcp); 6966 6967 if (tcp->tcp_syn_rcvd_timeout != 0) { 6968 /* we have timed out before */ 6969 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6970 listener->tcp_syn_rcvd_timeout--; 6971 } 6972 } else { 6973 tcp_t **tcpp = &listener->tcp_eager_next_q; 6974 tcp_t *prev = NULL; 6975 6976 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6977 if (tcpp[0] == tcp) { 6978 if (listener->tcp_eager_last_q == tcp) { 6979 /* 6980 * If we are unlinking the last 6981 * element on the list, adjust 6982 * tail pointer. Set tail pointer 6983 * to nil when list is empty. 6984 */ 6985 ASSERT(tcp->tcp_eager_next_q == NULL); 6986 if (listener->tcp_eager_last_q == 6987 listener->tcp_eager_next_q) { 6988 listener->tcp_eager_last_q = 6989 NULL; 6990 } else { 6991 /* 6992 * We won't get here if there 6993 * is only one eager in the 6994 * list. 6995 */ 6996 ASSERT(prev != NULL); 6997 listener->tcp_eager_last_q = 6998 prev; 6999 } 7000 } 7001 tcpp[0] = tcp->tcp_eager_next_q; 7002 tcp->tcp_eager_next_q = NULL; 7003 tcp->tcp_eager_last_q = NULL; 7004 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7005 listener->tcp_conn_req_cnt_q--; 7006 break; 7007 } 7008 prev = tcpp[0]; 7009 } 7010 } 7011 tcp->tcp_listener = NULL; 7012 } 7013 7014 /* Shorthand to generate and send TPI error acks to our client */ 7015 static void 7016 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7017 { 7018 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7019 putnext(tcp->tcp_rq, mp); 7020 } 7021 7022 /* Shorthand to generate and send TPI error acks to our client */ 7023 static void 7024 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7025 int t_error, int sys_error) 7026 { 7027 struct T_error_ack *teackp; 7028 7029 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7030 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7031 teackp = (struct T_error_ack *)mp->b_rptr; 7032 teackp->ERROR_prim = primitive; 7033 teackp->TLI_error = t_error; 7034 teackp->UNIX_error = sys_error; 7035 putnext(tcp->tcp_rq, mp); 7036 } 7037 } 7038 7039 /* 7040 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7041 * but instead the code relies on: 7042 * - the fact that the address of the array and its size never changes 7043 * - the atomic assignment of the elements of the array 7044 */ 7045 /* ARGSUSED */ 7046 static int 7047 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7048 { 7049 int i; 7050 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7051 7052 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7053 if (tcps->tcps_g_epriv_ports[i] != 0) 7054 (void) mi_mpprintf(mp, "%d ", 7055 tcps->tcps_g_epriv_ports[i]); 7056 } 7057 return (0); 7058 } 7059 7060 /* 7061 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7062 * threads from changing it at the same time. 7063 */ 7064 /* ARGSUSED */ 7065 static int 7066 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7067 cred_t *cr) 7068 { 7069 long new_value; 7070 int i; 7071 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7072 7073 /* 7074 * Fail the request if the new value does not lie within the 7075 * port number limits. 7076 */ 7077 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7078 new_value <= 0 || new_value >= 65536) { 7079 return (EINVAL); 7080 } 7081 7082 mutex_enter(&tcps->tcps_epriv_port_lock); 7083 /* Check if the value is already in the list */ 7084 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7085 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7086 mutex_exit(&tcps->tcps_epriv_port_lock); 7087 return (EEXIST); 7088 } 7089 } 7090 /* Find an empty slot */ 7091 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7092 if (tcps->tcps_g_epriv_ports[i] == 0) 7093 break; 7094 } 7095 if (i == tcps->tcps_g_num_epriv_ports) { 7096 mutex_exit(&tcps->tcps_epriv_port_lock); 7097 return (EOVERFLOW); 7098 } 7099 /* Set the new value */ 7100 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7101 mutex_exit(&tcps->tcps_epriv_port_lock); 7102 return (0); 7103 } 7104 7105 /* 7106 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7107 * threads from changing it at the same time. 7108 */ 7109 /* ARGSUSED */ 7110 static int 7111 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7112 cred_t *cr) 7113 { 7114 long new_value; 7115 int i; 7116 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7117 7118 /* 7119 * Fail the request if the new value does not lie within the 7120 * port number limits. 7121 */ 7122 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7123 new_value >= 65536) { 7124 return (EINVAL); 7125 } 7126 7127 mutex_enter(&tcps->tcps_epriv_port_lock); 7128 /* Check that the value is already in the list */ 7129 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7130 if (tcps->tcps_g_epriv_ports[i] == new_value) 7131 break; 7132 } 7133 if (i == tcps->tcps_g_num_epriv_ports) { 7134 mutex_exit(&tcps->tcps_epriv_port_lock); 7135 return (ESRCH); 7136 } 7137 /* Clear the value */ 7138 tcps->tcps_g_epriv_ports[i] = 0; 7139 mutex_exit(&tcps->tcps_epriv_port_lock); 7140 return (0); 7141 } 7142 7143 /* Return the TPI/TLI equivalent of our current tcp_state */ 7144 static int 7145 tcp_tpistate(tcp_t *tcp) 7146 { 7147 switch (tcp->tcp_state) { 7148 case TCPS_IDLE: 7149 return (TS_UNBND); 7150 case TCPS_LISTEN: 7151 /* 7152 * Return whether there are outstanding T_CONN_IND waiting 7153 * for the matching T_CONN_RES. Therefore don't count q0. 7154 */ 7155 if (tcp->tcp_conn_req_cnt_q > 0) 7156 return (TS_WRES_CIND); 7157 else 7158 return (TS_IDLE); 7159 case TCPS_BOUND: 7160 return (TS_IDLE); 7161 case TCPS_SYN_SENT: 7162 return (TS_WCON_CREQ); 7163 case TCPS_SYN_RCVD: 7164 /* 7165 * Note: assumption: this has to the active open SYN_RCVD. 7166 * The passive instance is detached in SYN_RCVD stage of 7167 * incoming connection processing so we cannot get request 7168 * for T_info_ack on it. 7169 */ 7170 return (TS_WACK_CRES); 7171 case TCPS_ESTABLISHED: 7172 return (TS_DATA_XFER); 7173 case TCPS_CLOSE_WAIT: 7174 return (TS_WREQ_ORDREL); 7175 case TCPS_FIN_WAIT_1: 7176 return (TS_WIND_ORDREL); 7177 case TCPS_FIN_WAIT_2: 7178 return (TS_WIND_ORDREL); 7179 7180 case TCPS_CLOSING: 7181 case TCPS_LAST_ACK: 7182 case TCPS_TIME_WAIT: 7183 case TCPS_CLOSED: 7184 /* 7185 * Following TS_WACK_DREQ7 is a rendition of "not 7186 * yet TS_IDLE" TPI state. There is no best match to any 7187 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7188 * choose a value chosen that will map to TLI/XTI level 7189 * state of TSTATECHNG (state is process of changing) which 7190 * captures what this dummy state represents. 7191 */ 7192 return (TS_WACK_DREQ7); 7193 default: 7194 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7195 tcp->tcp_state, tcp_display(tcp, NULL, 7196 DISP_PORT_ONLY)); 7197 return (TS_UNBND); 7198 } 7199 } 7200 7201 static void 7202 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7203 { 7204 tcp_stack_t *tcps = tcp->tcp_tcps; 7205 7206 if (tcp->tcp_family == AF_INET6) 7207 *tia = tcp_g_t_info_ack_v6; 7208 else 7209 *tia = tcp_g_t_info_ack; 7210 tia->CURRENT_state = tcp_tpistate(tcp); 7211 tia->OPT_size = tcp_max_optsize; 7212 if (tcp->tcp_mss == 0) { 7213 /* Not yet set - tcp_open does not set mss */ 7214 if (tcp->tcp_ipversion == IPV4_VERSION) 7215 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7216 else 7217 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7218 } else { 7219 tia->TIDU_size = tcp->tcp_mss; 7220 } 7221 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7222 } 7223 7224 static void 7225 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7226 t_uscalar_t cap_bits1) 7227 { 7228 tcap->CAP_bits1 = 0; 7229 7230 if (cap_bits1 & TC1_INFO) { 7231 tcp_copy_info(&tcap->INFO_ack, tcp); 7232 tcap->CAP_bits1 |= TC1_INFO; 7233 } 7234 7235 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7236 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7237 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7238 } 7239 7240 } 7241 7242 /* 7243 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7244 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7245 * tcp_g_t_info_ack. The current state of the stream is copied from 7246 * tcp_state. 7247 */ 7248 static void 7249 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7250 { 7251 t_uscalar_t cap_bits1; 7252 struct T_capability_ack *tcap; 7253 7254 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7255 freemsg(mp); 7256 return; 7257 } 7258 7259 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7260 7261 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7262 mp->b_datap->db_type, T_CAPABILITY_ACK); 7263 if (mp == NULL) 7264 return; 7265 7266 tcap = (struct T_capability_ack *)mp->b_rptr; 7267 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7268 7269 putnext(tcp->tcp_rq, mp); 7270 } 7271 7272 /* 7273 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7274 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7275 * The current state of the stream is copied from tcp_state. 7276 */ 7277 static void 7278 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7279 { 7280 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7281 T_INFO_ACK); 7282 if (!mp) { 7283 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7284 return; 7285 } 7286 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7287 putnext(tcp->tcp_rq, mp); 7288 } 7289 7290 /* Respond to the TPI addr request */ 7291 static void 7292 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7293 { 7294 sin_t *sin; 7295 mblk_t *ackmp; 7296 struct T_addr_ack *taa; 7297 7298 /* Make it large enough for worst case */ 7299 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7300 2 * sizeof (sin6_t), 1); 7301 if (ackmp == NULL) { 7302 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7303 return; 7304 } 7305 7306 if (tcp->tcp_ipversion == IPV6_VERSION) { 7307 tcp_addr_req_ipv6(tcp, ackmp); 7308 return; 7309 } 7310 taa = (struct T_addr_ack *)ackmp->b_rptr; 7311 7312 bzero(taa, sizeof (struct T_addr_ack)); 7313 ackmp->b_wptr = (uchar_t *)&taa[1]; 7314 7315 taa->PRIM_type = T_ADDR_ACK; 7316 ackmp->b_datap->db_type = M_PCPROTO; 7317 7318 /* 7319 * Note: Following code assumes 32 bit alignment of basic 7320 * data structures like sin_t and struct T_addr_ack. 7321 */ 7322 if (tcp->tcp_state >= TCPS_BOUND) { 7323 /* 7324 * Fill in local address 7325 */ 7326 taa->LOCADDR_length = sizeof (sin_t); 7327 taa->LOCADDR_offset = sizeof (*taa); 7328 7329 sin = (sin_t *)&taa[1]; 7330 7331 /* Fill zeroes and then intialize non-zero fields */ 7332 *sin = sin_null; 7333 7334 sin->sin_family = AF_INET; 7335 7336 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7337 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7338 7339 ackmp->b_wptr = (uchar_t *)&sin[1]; 7340 7341 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7342 /* 7343 * Fill in Remote address 7344 */ 7345 taa->REMADDR_length = sizeof (sin_t); 7346 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7347 taa->LOCADDR_length); 7348 7349 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7350 *sin = sin_null; 7351 sin->sin_family = AF_INET; 7352 sin->sin_addr.s_addr = tcp->tcp_remote; 7353 sin->sin_port = tcp->tcp_fport; 7354 7355 ackmp->b_wptr = (uchar_t *)&sin[1]; 7356 } 7357 } 7358 putnext(tcp->tcp_rq, ackmp); 7359 } 7360 7361 /* Assumes that tcp_addr_req gets enough space and alignment */ 7362 static void 7363 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7364 { 7365 sin6_t *sin6; 7366 struct T_addr_ack *taa; 7367 7368 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7369 ASSERT(OK_32PTR(ackmp->b_rptr)); 7370 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7371 2 * sizeof (sin6_t)); 7372 7373 taa = (struct T_addr_ack *)ackmp->b_rptr; 7374 7375 bzero(taa, sizeof (struct T_addr_ack)); 7376 ackmp->b_wptr = (uchar_t *)&taa[1]; 7377 7378 taa->PRIM_type = T_ADDR_ACK; 7379 ackmp->b_datap->db_type = M_PCPROTO; 7380 7381 /* 7382 * Note: Following code assumes 32 bit alignment of basic 7383 * data structures like sin6_t and struct T_addr_ack. 7384 */ 7385 if (tcp->tcp_state >= TCPS_BOUND) { 7386 /* 7387 * Fill in local address 7388 */ 7389 taa->LOCADDR_length = sizeof (sin6_t); 7390 taa->LOCADDR_offset = sizeof (*taa); 7391 7392 sin6 = (sin6_t *)&taa[1]; 7393 *sin6 = sin6_null; 7394 7395 sin6->sin6_family = AF_INET6; 7396 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7397 sin6->sin6_port = tcp->tcp_lport; 7398 7399 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7400 7401 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7402 /* 7403 * Fill in Remote address 7404 */ 7405 taa->REMADDR_length = sizeof (sin6_t); 7406 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7407 taa->LOCADDR_length); 7408 7409 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7410 *sin6 = sin6_null; 7411 sin6->sin6_family = AF_INET6; 7412 sin6->sin6_flowinfo = 7413 tcp->tcp_ip6h->ip6_vcf & 7414 ~IPV6_VERS_AND_FLOW_MASK; 7415 sin6->sin6_addr = tcp->tcp_remote_v6; 7416 sin6->sin6_port = tcp->tcp_fport; 7417 7418 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7419 } 7420 } 7421 putnext(tcp->tcp_rq, ackmp); 7422 } 7423 7424 /* 7425 * Handle reinitialization of a tcp structure. 7426 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7427 */ 7428 static void 7429 tcp_reinit(tcp_t *tcp) 7430 { 7431 mblk_t *mp; 7432 int err; 7433 tcp_stack_t *tcps = tcp->tcp_tcps; 7434 7435 TCP_STAT(tcps, tcp_reinit_calls); 7436 7437 /* tcp_reinit should never be called for detached tcp_t's */ 7438 ASSERT(tcp->tcp_listener == NULL); 7439 ASSERT((tcp->tcp_family == AF_INET && 7440 tcp->tcp_ipversion == IPV4_VERSION) || 7441 (tcp->tcp_family == AF_INET6 && 7442 (tcp->tcp_ipversion == IPV4_VERSION || 7443 tcp->tcp_ipversion == IPV6_VERSION))); 7444 7445 /* Cancel outstanding timers */ 7446 tcp_timers_stop(tcp); 7447 7448 /* 7449 * Reset everything in the state vector, after updating global 7450 * MIB data from instance counters. 7451 */ 7452 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7453 tcp->tcp_ibsegs = 0; 7454 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7455 tcp->tcp_obsegs = 0; 7456 7457 tcp_close_mpp(&tcp->tcp_xmit_head); 7458 if (tcp->tcp_snd_zcopy_aware) 7459 tcp_zcopy_notify(tcp); 7460 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7461 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7462 mutex_enter(&tcp->tcp_non_sq_lock); 7463 if (tcp->tcp_flow_stopped && 7464 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7465 tcp_clrqfull(tcp); 7466 } 7467 mutex_exit(&tcp->tcp_non_sq_lock); 7468 tcp_close_mpp(&tcp->tcp_reass_head); 7469 tcp->tcp_reass_tail = NULL; 7470 if (tcp->tcp_rcv_list != NULL) { 7471 /* Free b_next chain */ 7472 tcp_close_mpp(&tcp->tcp_rcv_list); 7473 tcp->tcp_rcv_last_head = NULL; 7474 tcp->tcp_rcv_last_tail = NULL; 7475 tcp->tcp_rcv_cnt = 0; 7476 } 7477 tcp->tcp_rcv_last_tail = NULL; 7478 7479 if ((mp = tcp->tcp_urp_mp) != NULL) { 7480 freemsg(mp); 7481 tcp->tcp_urp_mp = NULL; 7482 } 7483 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7484 freemsg(mp); 7485 tcp->tcp_urp_mark_mp = NULL; 7486 } 7487 if (tcp->tcp_fused_sigurg_mp != NULL) { 7488 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7489 freeb(tcp->tcp_fused_sigurg_mp); 7490 tcp->tcp_fused_sigurg_mp = NULL; 7491 } 7492 if (tcp->tcp_ordrel_mp != NULL) { 7493 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7494 freeb(tcp->tcp_ordrel_mp); 7495 tcp->tcp_ordrel_mp = NULL; 7496 } 7497 7498 /* 7499 * Following is a union with two members which are 7500 * identical types and size so the following cleanup 7501 * is enough. 7502 */ 7503 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7504 7505 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7506 7507 /* 7508 * The connection can't be on the tcp_time_wait_head list 7509 * since it is not detached. 7510 */ 7511 ASSERT(tcp->tcp_time_wait_next == NULL); 7512 ASSERT(tcp->tcp_time_wait_prev == NULL); 7513 ASSERT(tcp->tcp_time_wait_expire == 0); 7514 7515 if (tcp->tcp_kssl_pending) { 7516 tcp->tcp_kssl_pending = B_FALSE; 7517 7518 /* Don't reset if the initialized by bind. */ 7519 if (tcp->tcp_kssl_ent != NULL) { 7520 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7521 KSSL_NO_PROXY); 7522 } 7523 } 7524 if (tcp->tcp_kssl_ctx != NULL) { 7525 kssl_release_ctx(tcp->tcp_kssl_ctx); 7526 tcp->tcp_kssl_ctx = NULL; 7527 } 7528 7529 /* 7530 * Reset/preserve other values 7531 */ 7532 tcp_reinit_values(tcp); 7533 ipcl_hash_remove(tcp->tcp_connp); 7534 conn_delete_ire(tcp->tcp_connp, NULL); 7535 tcp_ipsec_cleanup(tcp); 7536 7537 if (tcp->tcp_conn_req_max != 0) { 7538 /* 7539 * This is the case when a TLI program uses the same 7540 * transport end point to accept a connection. This 7541 * makes the TCP both a listener and acceptor. When 7542 * this connection is closed, we need to set the state 7543 * back to TCPS_LISTEN. Make sure that the eager list 7544 * is reinitialized. 7545 * 7546 * Note that this stream is still bound to the four 7547 * tuples of the previous connection in IP. If a new 7548 * SYN with different foreign address comes in, IP will 7549 * not find it and will send it to the global queue. In 7550 * the global queue, TCP will do a tcp_lookup_listener() 7551 * to find this stream. This works because this stream 7552 * is only removed from connected hash. 7553 * 7554 */ 7555 tcp->tcp_state = TCPS_LISTEN; 7556 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7557 tcp->tcp_eager_next_drop_q0 = tcp; 7558 tcp->tcp_eager_prev_drop_q0 = tcp; 7559 tcp->tcp_connp->conn_recv = tcp_conn_request; 7560 if (tcp->tcp_family == AF_INET6) { 7561 ASSERT(tcp->tcp_connp->conn_af_isv6); 7562 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7563 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7564 } else { 7565 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7566 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7567 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7568 } 7569 } else { 7570 tcp->tcp_state = TCPS_BOUND; 7571 } 7572 7573 /* 7574 * Initialize to default values 7575 * Can't fail since enough header template space already allocated 7576 * at open(). 7577 */ 7578 err = tcp_init_values(tcp); 7579 ASSERT(err == 0); 7580 /* Restore state in tcp_tcph */ 7581 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7582 if (tcp->tcp_ipversion == IPV4_VERSION) 7583 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7584 else 7585 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7586 /* 7587 * Copy of the src addr. in tcp_t is needed in tcp_t 7588 * since the lookup funcs can only lookup on tcp_t 7589 */ 7590 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7591 7592 ASSERT(tcp->tcp_ptpbhn != NULL); 7593 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7594 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7595 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7596 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7597 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7598 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7599 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7600 } 7601 7602 /* 7603 * Force values to zero that need be zero. 7604 * Do not touch values asociated with the BOUND or LISTEN state 7605 * since the connection will end up in that state after the reinit. 7606 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7607 * structure! 7608 */ 7609 static void 7610 tcp_reinit_values(tcp) 7611 tcp_t *tcp; 7612 { 7613 tcp_stack_t *tcps = tcp->tcp_tcps; 7614 7615 #ifndef lint 7616 #define DONTCARE(x) 7617 #define PRESERVE(x) 7618 #else 7619 #define DONTCARE(x) ((x) = (x)) 7620 #define PRESERVE(x) ((x) = (x)) 7621 #endif /* lint */ 7622 7623 PRESERVE(tcp->tcp_bind_hash_port); 7624 PRESERVE(tcp->tcp_bind_hash); 7625 PRESERVE(tcp->tcp_ptpbhn); 7626 PRESERVE(tcp->tcp_acceptor_hash); 7627 PRESERVE(tcp->tcp_ptpahn); 7628 7629 /* Should be ASSERT NULL on these with new code! */ 7630 ASSERT(tcp->tcp_time_wait_next == NULL); 7631 ASSERT(tcp->tcp_time_wait_prev == NULL); 7632 ASSERT(tcp->tcp_time_wait_expire == 0); 7633 PRESERVE(tcp->tcp_state); 7634 PRESERVE(tcp->tcp_rq); 7635 PRESERVE(tcp->tcp_wq); 7636 7637 ASSERT(tcp->tcp_xmit_head == NULL); 7638 ASSERT(tcp->tcp_xmit_last == NULL); 7639 ASSERT(tcp->tcp_unsent == 0); 7640 ASSERT(tcp->tcp_xmit_tail == NULL); 7641 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7642 7643 tcp->tcp_snxt = 0; /* Displayed in mib */ 7644 tcp->tcp_suna = 0; /* Displayed in mib */ 7645 tcp->tcp_swnd = 0; 7646 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7647 7648 ASSERT(tcp->tcp_ibsegs == 0); 7649 ASSERT(tcp->tcp_obsegs == 0); 7650 7651 if (tcp->tcp_iphc != NULL) { 7652 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7653 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7654 } 7655 7656 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7657 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7658 DONTCARE(tcp->tcp_ipha); 7659 DONTCARE(tcp->tcp_ip6h); 7660 DONTCARE(tcp->tcp_ip_hdr_len); 7661 DONTCARE(tcp->tcp_tcph); 7662 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7663 tcp->tcp_valid_bits = 0; 7664 7665 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7666 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7667 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7668 tcp->tcp_last_rcv_lbolt = 0; 7669 7670 tcp->tcp_init_cwnd = 0; 7671 7672 tcp->tcp_urp_last_valid = 0; 7673 tcp->tcp_hard_binding = 0; 7674 tcp->tcp_hard_bound = 0; 7675 PRESERVE(tcp->tcp_cred); 7676 PRESERVE(tcp->tcp_cpid); 7677 PRESERVE(tcp->tcp_open_time); 7678 PRESERVE(tcp->tcp_exclbind); 7679 7680 tcp->tcp_fin_acked = 0; 7681 tcp->tcp_fin_rcvd = 0; 7682 tcp->tcp_fin_sent = 0; 7683 tcp->tcp_ordrel_done = 0; 7684 7685 tcp->tcp_debug = 0; 7686 tcp->tcp_dontroute = 0; 7687 tcp->tcp_broadcast = 0; 7688 7689 tcp->tcp_useloopback = 0; 7690 tcp->tcp_reuseaddr = 0; 7691 tcp->tcp_oobinline = 0; 7692 tcp->tcp_dgram_errind = 0; 7693 7694 tcp->tcp_detached = 0; 7695 tcp->tcp_bind_pending = 0; 7696 tcp->tcp_unbind_pending = 0; 7697 7698 tcp->tcp_snd_ws_ok = B_FALSE; 7699 tcp->tcp_snd_ts_ok = B_FALSE; 7700 tcp->tcp_linger = 0; 7701 tcp->tcp_ka_enabled = 0; 7702 tcp->tcp_zero_win_probe = 0; 7703 7704 tcp->tcp_loopback = 0; 7705 tcp->tcp_refuse = 0; 7706 tcp->tcp_localnet = 0; 7707 tcp->tcp_syn_defense = 0; 7708 tcp->tcp_set_timer = 0; 7709 7710 tcp->tcp_active_open = 0; 7711 tcp->tcp_rexmit = B_FALSE; 7712 tcp->tcp_xmit_zc_clean = B_FALSE; 7713 7714 tcp->tcp_snd_sack_ok = B_FALSE; 7715 PRESERVE(tcp->tcp_recvdstaddr); 7716 tcp->tcp_hwcksum = B_FALSE; 7717 7718 tcp->tcp_ire_ill_check_done = B_FALSE; 7719 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7720 7721 tcp->tcp_mdt = B_FALSE; 7722 tcp->tcp_mdt_hdr_head = 0; 7723 tcp->tcp_mdt_hdr_tail = 0; 7724 7725 tcp->tcp_conn_def_q0 = 0; 7726 tcp->tcp_ip_forward_progress = B_FALSE; 7727 tcp->tcp_anon_priv_bind = 0; 7728 tcp->tcp_ecn_ok = B_FALSE; 7729 7730 tcp->tcp_cwr = B_FALSE; 7731 tcp->tcp_ecn_echo_on = B_FALSE; 7732 7733 if (tcp->tcp_sack_info != NULL) { 7734 if (tcp->tcp_notsack_list != NULL) { 7735 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7736 } 7737 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7738 tcp->tcp_sack_info = NULL; 7739 } 7740 7741 tcp->tcp_rcv_ws = 0; 7742 tcp->tcp_snd_ws = 0; 7743 tcp->tcp_ts_recent = 0; 7744 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7745 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7746 tcp->tcp_if_mtu = 0; 7747 7748 ASSERT(tcp->tcp_reass_head == NULL); 7749 ASSERT(tcp->tcp_reass_tail == NULL); 7750 7751 tcp->tcp_cwnd_cnt = 0; 7752 7753 ASSERT(tcp->tcp_rcv_list == NULL); 7754 ASSERT(tcp->tcp_rcv_last_head == NULL); 7755 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7756 ASSERT(tcp->tcp_rcv_cnt == 0); 7757 7758 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7759 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7760 tcp->tcp_csuna = 0; 7761 7762 tcp->tcp_rto = 0; /* Displayed in MIB */ 7763 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7764 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7765 tcp->tcp_rtt_update = 0; 7766 7767 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7768 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7769 7770 tcp->tcp_rack = 0; /* Displayed in mib */ 7771 tcp->tcp_rack_cnt = 0; 7772 tcp->tcp_rack_cur_max = 0; 7773 tcp->tcp_rack_abs_max = 0; 7774 7775 tcp->tcp_max_swnd = 0; 7776 7777 ASSERT(tcp->tcp_listener == NULL); 7778 7779 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7780 7781 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7782 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7783 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7784 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7785 7786 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7787 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7788 PRESERVE(tcp->tcp_conn_req_max); 7789 PRESERVE(tcp->tcp_conn_req_seqnum); 7790 7791 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7792 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7793 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7794 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7795 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7796 7797 tcp->tcp_lingertime = 0; 7798 7799 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7800 ASSERT(tcp->tcp_urp_mp == NULL); 7801 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7802 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7803 7804 ASSERT(tcp->tcp_eager_next_q == NULL); 7805 ASSERT(tcp->tcp_eager_last_q == NULL); 7806 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7807 tcp->tcp_eager_prev_q0 == NULL) || 7808 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7809 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7810 7811 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7812 tcp->tcp_eager_prev_drop_q0 == NULL) || 7813 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7814 7815 tcp->tcp_client_errno = 0; 7816 7817 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7818 7819 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7820 7821 PRESERVE(tcp->tcp_bound_source_v6); 7822 tcp->tcp_last_sent_len = 0; 7823 tcp->tcp_dupack_cnt = 0; 7824 7825 tcp->tcp_fport = 0; /* Displayed in MIB */ 7826 PRESERVE(tcp->tcp_lport); 7827 7828 PRESERVE(tcp->tcp_acceptor_lockp); 7829 7830 ASSERT(tcp->tcp_ordrel_mp == NULL); 7831 PRESERVE(tcp->tcp_acceptor_id); 7832 DONTCARE(tcp->tcp_ipsec_overhead); 7833 7834 PRESERVE(tcp->tcp_family); 7835 if (tcp->tcp_family == AF_INET6) { 7836 tcp->tcp_ipversion = IPV6_VERSION; 7837 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7838 } else { 7839 tcp->tcp_ipversion = IPV4_VERSION; 7840 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7841 } 7842 7843 tcp->tcp_bound_if = 0; 7844 tcp->tcp_ipv6_recvancillary = 0; 7845 tcp->tcp_recvifindex = 0; 7846 tcp->tcp_recvhops = 0; 7847 tcp->tcp_closed = 0; 7848 tcp->tcp_cleandeathtag = 0; 7849 if (tcp->tcp_hopopts != NULL) { 7850 mi_free(tcp->tcp_hopopts); 7851 tcp->tcp_hopopts = NULL; 7852 tcp->tcp_hopoptslen = 0; 7853 } 7854 ASSERT(tcp->tcp_hopoptslen == 0); 7855 if (tcp->tcp_dstopts != NULL) { 7856 mi_free(tcp->tcp_dstopts); 7857 tcp->tcp_dstopts = NULL; 7858 tcp->tcp_dstoptslen = 0; 7859 } 7860 ASSERT(tcp->tcp_dstoptslen == 0); 7861 if (tcp->tcp_rtdstopts != NULL) { 7862 mi_free(tcp->tcp_rtdstopts); 7863 tcp->tcp_rtdstopts = NULL; 7864 tcp->tcp_rtdstoptslen = 0; 7865 } 7866 ASSERT(tcp->tcp_rtdstoptslen == 0); 7867 if (tcp->tcp_rthdr != NULL) { 7868 mi_free(tcp->tcp_rthdr); 7869 tcp->tcp_rthdr = NULL; 7870 tcp->tcp_rthdrlen = 0; 7871 } 7872 ASSERT(tcp->tcp_rthdrlen == 0); 7873 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7874 7875 /* Reset fusion-related fields */ 7876 tcp->tcp_fused = B_FALSE; 7877 tcp->tcp_unfusable = B_FALSE; 7878 tcp->tcp_fused_sigurg = B_FALSE; 7879 tcp->tcp_direct_sockfs = B_FALSE; 7880 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7881 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7882 tcp->tcp_loopback_peer = NULL; 7883 tcp->tcp_fuse_rcv_hiwater = 0; 7884 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7885 tcp->tcp_fuse_rcv_unread_cnt = 0; 7886 7887 tcp->tcp_lso = B_FALSE; 7888 7889 tcp->tcp_in_ack_unsent = 0; 7890 tcp->tcp_cork = B_FALSE; 7891 tcp->tcp_tconnind_started = B_FALSE; 7892 7893 PRESERVE(tcp->tcp_squeue_bytes); 7894 7895 ASSERT(tcp->tcp_kssl_ctx == NULL); 7896 ASSERT(!tcp->tcp_kssl_pending); 7897 PRESERVE(tcp->tcp_kssl_ent); 7898 7899 /* Sodirect */ 7900 tcp->tcp_sodirect = NULL; 7901 7902 tcp->tcp_closemp_used = B_FALSE; 7903 7904 PRESERVE(tcp->tcp_rsrv_mp); 7905 PRESERVE(tcp->tcp_rsrv_mp_lock); 7906 7907 #ifdef DEBUG 7908 DONTCARE(tcp->tcmp_stk[0]); 7909 #endif 7910 7911 PRESERVE(tcp->tcp_connid); 7912 7913 7914 #undef DONTCARE 7915 #undef PRESERVE 7916 } 7917 7918 /* 7919 * Allocate necessary resources and initialize state vector. 7920 * Guaranteed not to fail so that when an error is returned, 7921 * the caller doesn't need to do any additional cleanup. 7922 */ 7923 int 7924 tcp_init(tcp_t *tcp, queue_t *q) 7925 { 7926 int err; 7927 7928 tcp->tcp_rq = q; 7929 tcp->tcp_wq = WR(q); 7930 tcp->tcp_state = TCPS_IDLE; 7931 if ((err = tcp_init_values(tcp)) != 0) 7932 tcp_timers_stop(tcp); 7933 return (err); 7934 } 7935 7936 static int 7937 tcp_init_values(tcp_t *tcp) 7938 { 7939 int err; 7940 tcp_stack_t *tcps = tcp->tcp_tcps; 7941 7942 ASSERT((tcp->tcp_family == AF_INET && 7943 tcp->tcp_ipversion == IPV4_VERSION) || 7944 (tcp->tcp_family == AF_INET6 && 7945 (tcp->tcp_ipversion == IPV4_VERSION || 7946 tcp->tcp_ipversion == IPV6_VERSION))); 7947 7948 /* 7949 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7950 * will be close to tcp_rexmit_interval_initial. By doing this, we 7951 * allow the algorithm to adjust slowly to large fluctuations of RTT 7952 * during first few transmissions of a connection as seen in slow 7953 * links. 7954 */ 7955 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7956 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7957 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7958 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7959 tcps->tcps_conn_grace_period; 7960 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7961 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7962 tcp->tcp_timer_backoff = 0; 7963 tcp->tcp_ms_we_have_waited = 0; 7964 tcp->tcp_last_recv_time = lbolt; 7965 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7966 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7967 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7968 7969 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7970 7971 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7972 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7973 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7974 /* 7975 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7976 * passive open. 7977 */ 7978 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7979 7980 tcp->tcp_naglim = tcps->tcps_naglim_def; 7981 7982 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7983 7984 tcp->tcp_mdt_hdr_head = 0; 7985 tcp->tcp_mdt_hdr_tail = 0; 7986 7987 /* Reset fusion-related fields */ 7988 tcp->tcp_fused = B_FALSE; 7989 tcp->tcp_unfusable = B_FALSE; 7990 tcp->tcp_fused_sigurg = B_FALSE; 7991 tcp->tcp_direct_sockfs = B_FALSE; 7992 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7993 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7994 tcp->tcp_loopback_peer = NULL; 7995 tcp->tcp_fuse_rcv_hiwater = 0; 7996 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7997 tcp->tcp_fuse_rcv_unread_cnt = 0; 7998 7999 /* Sodirect */ 8000 tcp->tcp_sodirect = NULL; 8001 8002 /* Initialize the header template */ 8003 if (tcp->tcp_ipversion == IPV4_VERSION) { 8004 err = tcp_header_init_ipv4(tcp); 8005 } else { 8006 err = tcp_header_init_ipv6(tcp); 8007 } 8008 if (err) 8009 return (err); 8010 8011 /* 8012 * Init the window scale to the max so tcp_rwnd_set() won't pare 8013 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8014 */ 8015 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8016 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8017 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8018 8019 tcp->tcp_cork = B_FALSE; 8020 /* 8021 * Init the tcp_debug option. This value determines whether TCP 8022 * calls strlog() to print out debug messages. Doing this 8023 * initialization here means that this value is not inherited thru 8024 * tcp_reinit(). 8025 */ 8026 tcp->tcp_debug = tcps->tcps_dbg; 8027 8028 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8029 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8030 8031 return (0); 8032 } 8033 8034 /* 8035 * Initialize the IPv4 header. Loses any record of any IP options. 8036 */ 8037 static int 8038 tcp_header_init_ipv4(tcp_t *tcp) 8039 { 8040 tcph_t *tcph; 8041 uint32_t sum; 8042 conn_t *connp; 8043 tcp_stack_t *tcps = tcp->tcp_tcps; 8044 8045 /* 8046 * This is a simple initialization. If there's 8047 * already a template, it should never be too small, 8048 * so reuse it. Otherwise, allocate space for the new one. 8049 */ 8050 if (tcp->tcp_iphc == NULL) { 8051 ASSERT(tcp->tcp_iphc_len == 0); 8052 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8053 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8054 if (tcp->tcp_iphc == NULL) { 8055 tcp->tcp_iphc_len = 0; 8056 return (ENOMEM); 8057 } 8058 } 8059 8060 /* options are gone; may need a new label */ 8061 connp = tcp->tcp_connp; 8062 connp->conn_mlp_type = mlptSingle; 8063 connp->conn_ulp_labeled = !is_system_labeled(); 8064 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8065 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8066 tcp->tcp_ip6h = NULL; 8067 tcp->tcp_ipversion = IPV4_VERSION; 8068 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8069 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8070 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8071 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8072 tcp->tcp_ipha->ipha_version_and_hdr_length 8073 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8074 tcp->tcp_ipha->ipha_ident = 0; 8075 8076 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8077 tcp->tcp_tos = 0; 8078 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8079 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8080 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8081 8082 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8083 tcp->tcp_tcph = tcph; 8084 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8085 /* 8086 * IP wants our header length in the checksum field to 8087 * allow it to perform a single pseudo-header+checksum 8088 * calculation on behalf of TCP. 8089 * Include the adjustment for a source route once IP_OPTIONS is set. 8090 */ 8091 sum = sizeof (tcph_t) + tcp->tcp_sum; 8092 sum = (sum >> 16) + (sum & 0xFFFF); 8093 U16_TO_ABE16(sum, tcph->th_sum); 8094 return (0); 8095 } 8096 8097 /* 8098 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8099 */ 8100 static int 8101 tcp_header_init_ipv6(tcp_t *tcp) 8102 { 8103 tcph_t *tcph; 8104 uint32_t sum; 8105 conn_t *connp; 8106 tcp_stack_t *tcps = tcp->tcp_tcps; 8107 8108 /* 8109 * This is a simple initialization. If there's 8110 * already a template, it should never be too small, 8111 * so reuse it. Otherwise, allocate space for the new one. 8112 * Ensure that there is enough space to "downgrade" the tcp_t 8113 * to an IPv4 tcp_t. This requires having space for a full load 8114 * of IPv4 options, as well as a full load of TCP options 8115 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8116 * than a v6 header and a TCP header with a full load of TCP options 8117 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8118 * We want to avoid reallocation in the "downgraded" case when 8119 * processing outbound IPv4 options. 8120 */ 8121 if (tcp->tcp_iphc == NULL) { 8122 ASSERT(tcp->tcp_iphc_len == 0); 8123 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8124 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8125 if (tcp->tcp_iphc == NULL) { 8126 tcp->tcp_iphc_len = 0; 8127 return (ENOMEM); 8128 } 8129 } 8130 8131 /* options are gone; may need a new label */ 8132 connp = tcp->tcp_connp; 8133 connp->conn_mlp_type = mlptSingle; 8134 connp->conn_ulp_labeled = !is_system_labeled(); 8135 8136 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8137 tcp->tcp_ipversion = IPV6_VERSION; 8138 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8139 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8140 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8141 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8142 tcp->tcp_ipha = NULL; 8143 8144 /* Initialize the header template */ 8145 8146 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8147 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8148 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8149 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8150 8151 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8152 tcp->tcp_tcph = tcph; 8153 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8154 /* 8155 * IP wants our header length in the checksum field to 8156 * allow it to perform a single psuedo-header+checksum 8157 * calculation on behalf of TCP. 8158 * Include the adjustment for a source route when IPV6_RTHDR is set. 8159 */ 8160 sum = sizeof (tcph_t) + tcp->tcp_sum; 8161 sum = (sum >> 16) + (sum & 0xFFFF); 8162 U16_TO_ABE16(sum, tcph->th_sum); 8163 return (0); 8164 } 8165 8166 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8167 #define ICMP_MIN_TCP_HDR 8 8168 8169 /* 8170 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8171 * passed up by IP. The message is always received on the correct tcp_t. 8172 * Assumes that IP has pulled up everything up to and including the ICMP header. 8173 */ 8174 void 8175 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8176 { 8177 icmph_t *icmph; 8178 ipha_t *ipha; 8179 int iph_hdr_length; 8180 tcph_t *tcph; 8181 boolean_t ipsec_mctl = B_FALSE; 8182 boolean_t secure; 8183 mblk_t *first_mp = mp; 8184 int32_t new_mss; 8185 uint32_t ratio; 8186 size_t mp_size = MBLKL(mp); 8187 uint32_t seg_seq; 8188 tcp_stack_t *tcps = tcp->tcp_tcps; 8189 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8190 8191 /* Assume IP provides aligned packets - otherwise toss */ 8192 if (!OK_32PTR(mp->b_rptr)) { 8193 freemsg(mp); 8194 return; 8195 } 8196 8197 /* 8198 * Since ICMP errors are normal data marked with M_CTL when sent 8199 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8200 * packets starting with an ipsec_info_t, see ipsec_info.h. 8201 */ 8202 if ((mp_size == sizeof (ipsec_info_t)) && 8203 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8204 ASSERT(mp->b_cont != NULL); 8205 mp = mp->b_cont; 8206 /* IP should have done this */ 8207 ASSERT(OK_32PTR(mp->b_rptr)); 8208 mp_size = MBLKL(mp); 8209 ipsec_mctl = B_TRUE; 8210 } 8211 8212 /* 8213 * Verify that we have a complete outer IP header. If not, drop it. 8214 */ 8215 if (mp_size < sizeof (ipha_t)) { 8216 noticmpv4: 8217 freemsg(first_mp); 8218 return; 8219 } 8220 8221 ipha = (ipha_t *)mp->b_rptr; 8222 /* 8223 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8224 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8225 */ 8226 switch (IPH_HDR_VERSION(ipha)) { 8227 case IPV6_VERSION: 8228 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8229 return; 8230 case IPV4_VERSION: 8231 break; 8232 default: 8233 goto noticmpv4; 8234 } 8235 8236 /* Skip past the outer IP and ICMP headers */ 8237 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8238 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8239 /* 8240 * If we don't have the correct outer IP header length or if the ULP 8241 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8242 * send it upstream. 8243 */ 8244 if (iph_hdr_length < sizeof (ipha_t) || 8245 ipha->ipha_protocol != IPPROTO_ICMP || 8246 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8247 goto noticmpv4; 8248 } 8249 ipha = (ipha_t *)&icmph[1]; 8250 8251 /* Skip past the inner IP and find the ULP header */ 8252 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8253 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8254 /* 8255 * If we don't have the correct inner IP header length or if the ULP 8256 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8257 * bytes of TCP header, drop it. 8258 */ 8259 if (iph_hdr_length < sizeof (ipha_t) || 8260 ipha->ipha_protocol != IPPROTO_TCP || 8261 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8262 goto noticmpv4; 8263 } 8264 8265 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8266 if (ipsec_mctl) { 8267 secure = ipsec_in_is_secure(first_mp); 8268 } else { 8269 secure = B_FALSE; 8270 } 8271 if (secure) { 8272 /* 8273 * If we are willing to accept this in clear 8274 * we don't have to verify policy. 8275 */ 8276 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8277 if (!tcp_check_policy(tcp, first_mp, 8278 ipha, NULL, secure, ipsec_mctl)) { 8279 /* 8280 * tcp_check_policy called 8281 * ip_drop_packet() on failure. 8282 */ 8283 return; 8284 } 8285 } 8286 } 8287 } else if (ipsec_mctl) { 8288 /* 8289 * This is a hard_bound connection. IP has already 8290 * verified policy. We don't have to do it again. 8291 */ 8292 freeb(first_mp); 8293 first_mp = mp; 8294 ipsec_mctl = B_FALSE; 8295 } 8296 8297 seg_seq = ABE32_TO_U32(tcph->th_seq); 8298 /* 8299 * TCP SHOULD check that the TCP sequence number contained in 8300 * payload of the ICMP error message is within the range 8301 * SND.UNA <= SEG.SEQ < SND.NXT. 8302 */ 8303 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8304 /* 8305 * The ICMP message is bogus, just drop it. But if this is 8306 * an ICMP too big message, IP has already changed 8307 * the ire_max_frag to the bogus value. We need to change 8308 * it back. 8309 */ 8310 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8311 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8312 conn_t *connp = tcp->tcp_connp; 8313 ire_t *ire; 8314 int flag; 8315 8316 if (tcp->tcp_ipversion == IPV4_VERSION) { 8317 flag = tcp->tcp_ipha-> 8318 ipha_fragment_offset_and_flags; 8319 } else { 8320 flag = 0; 8321 } 8322 mutex_enter(&connp->conn_lock); 8323 if ((ire = connp->conn_ire_cache) != NULL) { 8324 mutex_enter(&ire->ire_lock); 8325 mutex_exit(&connp->conn_lock); 8326 ire->ire_max_frag = tcp->tcp_if_mtu; 8327 ire->ire_frag_flag |= flag; 8328 mutex_exit(&ire->ire_lock); 8329 } else { 8330 mutex_exit(&connp->conn_lock); 8331 } 8332 } 8333 goto noticmpv4; 8334 } 8335 8336 switch (icmph->icmph_type) { 8337 case ICMP_DEST_UNREACHABLE: 8338 switch (icmph->icmph_code) { 8339 case ICMP_FRAGMENTATION_NEEDED: 8340 /* 8341 * Reduce the MSS based on the new MTU. This will 8342 * eliminate any fragmentation locally. 8343 * N.B. There may well be some funny side-effects on 8344 * the local send policy and the remote receive policy. 8345 * Pending further research, we provide 8346 * tcp_ignore_path_mtu just in case this proves 8347 * disastrous somewhere. 8348 * 8349 * After updating the MSS, retransmit part of the 8350 * dropped segment using the new mss by calling 8351 * tcp_wput_data(). Need to adjust all those 8352 * params to make sure tcp_wput_data() work properly. 8353 */ 8354 if (tcps->tcps_ignore_path_mtu || 8355 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8356 break; 8357 8358 /* 8359 * Decrease the MSS by time stamp options 8360 * IP options and IPSEC options. tcp_hdr_len 8361 * includes time stamp option and IP option 8362 * length. Note that new_mss may be negative 8363 * if tcp_ipsec_overhead is large and the 8364 * icmph_du_mtu is the minimum value, which is 68. 8365 */ 8366 new_mss = ntohs(icmph->icmph_du_mtu) - 8367 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8368 8369 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8370 new_mss); 8371 8372 /* 8373 * Only update the MSS if the new one is 8374 * smaller than the previous one. This is 8375 * to avoid problems when getting multiple 8376 * ICMP errors for the same MTU. 8377 */ 8378 if (new_mss >= tcp->tcp_mss) 8379 break; 8380 8381 /* 8382 * Note that we are using the template header's DF 8383 * bit in the fast path sending. So we need to compare 8384 * the new mss with both tcps_mss_min and ip_pmtu_min. 8385 * And stop doing IPv4 PMTUd if new_mss is less than 8386 * MAX(tcps_mss_min, ip_pmtu_min). 8387 */ 8388 if (new_mss < tcps->tcps_mss_min || 8389 new_mss < ipst->ips_ip_pmtu_min) { 8390 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8391 0; 8392 } 8393 8394 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8395 ASSERT(ratio >= 1); 8396 tcp_mss_set(tcp, new_mss, B_TRUE); 8397 8398 /* 8399 * Make sure we have something to 8400 * send. 8401 */ 8402 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8403 (tcp->tcp_xmit_head != NULL)) { 8404 /* 8405 * Shrink tcp_cwnd in 8406 * proportion to the old MSS/new MSS. 8407 */ 8408 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8409 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8410 (tcp->tcp_unsent == 0)) { 8411 tcp->tcp_rexmit_max = tcp->tcp_fss; 8412 } else { 8413 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8414 } 8415 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8416 tcp->tcp_rexmit = B_TRUE; 8417 tcp->tcp_dupack_cnt = 0; 8418 tcp->tcp_snd_burst = TCP_CWND_SS; 8419 tcp_ss_rexmit(tcp); 8420 } 8421 break; 8422 case ICMP_PORT_UNREACHABLE: 8423 case ICMP_PROTOCOL_UNREACHABLE: 8424 switch (tcp->tcp_state) { 8425 case TCPS_SYN_SENT: 8426 case TCPS_SYN_RCVD: 8427 /* 8428 * ICMP can snipe away incipient 8429 * TCP connections as long as 8430 * seq number is same as initial 8431 * send seq number. 8432 */ 8433 if (seg_seq == tcp->tcp_iss) { 8434 (void) tcp_clean_death(tcp, 8435 ECONNREFUSED, 6); 8436 } 8437 break; 8438 } 8439 break; 8440 case ICMP_HOST_UNREACHABLE: 8441 case ICMP_NET_UNREACHABLE: 8442 /* Record the error in case we finally time out. */ 8443 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8444 tcp->tcp_client_errno = EHOSTUNREACH; 8445 else 8446 tcp->tcp_client_errno = ENETUNREACH; 8447 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8448 if (tcp->tcp_listener != NULL && 8449 tcp->tcp_listener->tcp_syn_defense) { 8450 /* 8451 * Ditch the half-open connection if we 8452 * suspect a SYN attack is under way. 8453 */ 8454 tcp_ip_ire_mark_advice(tcp); 8455 (void) tcp_clean_death(tcp, 8456 tcp->tcp_client_errno, 7); 8457 } 8458 } 8459 break; 8460 default: 8461 break; 8462 } 8463 break; 8464 case ICMP_SOURCE_QUENCH: { 8465 /* 8466 * use a global boolean to control 8467 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8468 * The default is false. 8469 */ 8470 if (tcp_icmp_source_quench) { 8471 /* 8472 * Reduce the sending rate as if we got a 8473 * retransmit timeout 8474 */ 8475 uint32_t npkt; 8476 8477 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8478 tcp->tcp_mss; 8479 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8480 tcp->tcp_cwnd = tcp->tcp_mss; 8481 tcp->tcp_cwnd_cnt = 0; 8482 } 8483 break; 8484 } 8485 } 8486 freemsg(first_mp); 8487 } 8488 8489 /* 8490 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8491 * error messages passed up by IP. 8492 * Assumes that IP has pulled up all the extension headers as well 8493 * as the ICMPv6 header. 8494 */ 8495 static void 8496 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8497 { 8498 icmp6_t *icmp6; 8499 ip6_t *ip6h; 8500 uint16_t iph_hdr_length; 8501 tcpha_t *tcpha; 8502 uint8_t *nexthdrp; 8503 uint32_t new_mss; 8504 uint32_t ratio; 8505 boolean_t secure; 8506 mblk_t *first_mp = mp; 8507 size_t mp_size; 8508 uint32_t seg_seq; 8509 tcp_stack_t *tcps = tcp->tcp_tcps; 8510 8511 /* 8512 * The caller has determined if this is an IPSEC_IN packet and 8513 * set ipsec_mctl appropriately (see tcp_icmp_error). 8514 */ 8515 if (ipsec_mctl) 8516 mp = mp->b_cont; 8517 8518 mp_size = MBLKL(mp); 8519 8520 /* 8521 * Verify that we have a complete IP header. If not, send it upstream. 8522 */ 8523 if (mp_size < sizeof (ip6_t)) { 8524 noticmpv6: 8525 freemsg(first_mp); 8526 return; 8527 } 8528 8529 /* 8530 * Verify this is an ICMPV6 packet, else send it upstream. 8531 */ 8532 ip6h = (ip6_t *)mp->b_rptr; 8533 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8534 iph_hdr_length = IPV6_HDR_LEN; 8535 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8536 &nexthdrp) || 8537 *nexthdrp != IPPROTO_ICMPV6) { 8538 goto noticmpv6; 8539 } 8540 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8541 ip6h = (ip6_t *)&icmp6[1]; 8542 /* 8543 * Verify if we have a complete ICMP and inner IP header. 8544 */ 8545 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8546 goto noticmpv6; 8547 8548 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8549 goto noticmpv6; 8550 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8551 /* 8552 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8553 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8554 * packet. 8555 */ 8556 if ((*nexthdrp != IPPROTO_TCP) || 8557 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8558 goto noticmpv6; 8559 } 8560 8561 /* 8562 * ICMP errors come on the right queue or come on 8563 * listener/global queue for detached connections and 8564 * get switched to the right queue. If it comes on the 8565 * right queue, policy check has already been done by IP 8566 * and thus free the first_mp without verifying the policy. 8567 * If it has come for a non-hard bound connection, we need 8568 * to verify policy as IP may not have done it. 8569 */ 8570 if (!tcp->tcp_hard_bound) { 8571 if (ipsec_mctl) { 8572 secure = ipsec_in_is_secure(first_mp); 8573 } else { 8574 secure = B_FALSE; 8575 } 8576 if (secure) { 8577 /* 8578 * If we are willing to accept this in clear 8579 * we don't have to verify policy. 8580 */ 8581 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8582 if (!tcp_check_policy(tcp, first_mp, 8583 NULL, ip6h, secure, ipsec_mctl)) { 8584 /* 8585 * tcp_check_policy called 8586 * ip_drop_packet() on failure. 8587 */ 8588 return; 8589 } 8590 } 8591 } 8592 } else if (ipsec_mctl) { 8593 /* 8594 * This is a hard_bound connection. IP has already 8595 * verified policy. We don't have to do it again. 8596 */ 8597 freeb(first_mp); 8598 first_mp = mp; 8599 ipsec_mctl = B_FALSE; 8600 } 8601 8602 seg_seq = ntohl(tcpha->tha_seq); 8603 /* 8604 * TCP SHOULD check that the TCP sequence number contained in 8605 * payload of the ICMP error message is within the range 8606 * SND.UNA <= SEG.SEQ < SND.NXT. 8607 */ 8608 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8609 /* 8610 * If the ICMP message is bogus, should we kill the 8611 * connection, or should we just drop the bogus ICMP 8612 * message? It would probably make more sense to just 8613 * drop the message so that if this one managed to get 8614 * in, the real connection should not suffer. 8615 */ 8616 goto noticmpv6; 8617 } 8618 8619 switch (icmp6->icmp6_type) { 8620 case ICMP6_PACKET_TOO_BIG: 8621 /* 8622 * Reduce the MSS based on the new MTU. This will 8623 * eliminate any fragmentation locally. 8624 * N.B. There may well be some funny side-effects on 8625 * the local send policy and the remote receive policy. 8626 * Pending further research, we provide 8627 * tcp_ignore_path_mtu just in case this proves 8628 * disastrous somewhere. 8629 * 8630 * After updating the MSS, retransmit part of the 8631 * dropped segment using the new mss by calling 8632 * tcp_wput_data(). Need to adjust all those 8633 * params to make sure tcp_wput_data() work properly. 8634 */ 8635 if (tcps->tcps_ignore_path_mtu) 8636 break; 8637 8638 /* 8639 * Decrease the MSS by time stamp options 8640 * IP options and IPSEC options. tcp_hdr_len 8641 * includes time stamp option and IP option 8642 * length. 8643 */ 8644 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8645 tcp->tcp_ipsec_overhead; 8646 8647 /* 8648 * Only update the MSS if the new one is 8649 * smaller than the previous one. This is 8650 * to avoid problems when getting multiple 8651 * ICMP errors for the same MTU. 8652 */ 8653 if (new_mss >= tcp->tcp_mss) 8654 break; 8655 8656 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8657 ASSERT(ratio >= 1); 8658 tcp_mss_set(tcp, new_mss, B_TRUE); 8659 8660 /* 8661 * Make sure we have something to 8662 * send. 8663 */ 8664 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8665 (tcp->tcp_xmit_head != NULL)) { 8666 /* 8667 * Shrink tcp_cwnd in 8668 * proportion to the old MSS/new MSS. 8669 */ 8670 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8671 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8672 (tcp->tcp_unsent == 0)) { 8673 tcp->tcp_rexmit_max = tcp->tcp_fss; 8674 } else { 8675 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8676 } 8677 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8678 tcp->tcp_rexmit = B_TRUE; 8679 tcp->tcp_dupack_cnt = 0; 8680 tcp->tcp_snd_burst = TCP_CWND_SS; 8681 tcp_ss_rexmit(tcp); 8682 } 8683 break; 8684 8685 case ICMP6_DST_UNREACH: 8686 switch (icmp6->icmp6_code) { 8687 case ICMP6_DST_UNREACH_NOPORT: 8688 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8689 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8690 (seg_seq == tcp->tcp_iss)) { 8691 (void) tcp_clean_death(tcp, 8692 ECONNREFUSED, 8); 8693 } 8694 break; 8695 8696 case ICMP6_DST_UNREACH_ADMIN: 8697 case ICMP6_DST_UNREACH_NOROUTE: 8698 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8699 case ICMP6_DST_UNREACH_ADDR: 8700 /* Record the error in case we finally time out. */ 8701 tcp->tcp_client_errno = EHOSTUNREACH; 8702 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8703 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8704 (seg_seq == tcp->tcp_iss)) { 8705 if (tcp->tcp_listener != NULL && 8706 tcp->tcp_listener->tcp_syn_defense) { 8707 /* 8708 * Ditch the half-open connection if we 8709 * suspect a SYN attack is under way. 8710 */ 8711 tcp_ip_ire_mark_advice(tcp); 8712 (void) tcp_clean_death(tcp, 8713 tcp->tcp_client_errno, 9); 8714 } 8715 } 8716 8717 8718 break; 8719 default: 8720 break; 8721 } 8722 break; 8723 8724 case ICMP6_PARAM_PROB: 8725 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8726 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8727 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8728 (uchar_t *)nexthdrp) { 8729 if (tcp->tcp_state == TCPS_SYN_SENT || 8730 tcp->tcp_state == TCPS_SYN_RCVD) { 8731 (void) tcp_clean_death(tcp, 8732 ECONNREFUSED, 10); 8733 } 8734 break; 8735 } 8736 break; 8737 8738 case ICMP6_TIME_EXCEEDED: 8739 default: 8740 break; 8741 } 8742 freemsg(first_mp); 8743 } 8744 8745 /* 8746 * Notify IP that we are having trouble with this connection. IP should 8747 * blow the IRE away and start over. 8748 */ 8749 static void 8750 tcp_ip_notify(tcp_t *tcp) 8751 { 8752 struct iocblk *iocp; 8753 ipid_t *ipid; 8754 mblk_t *mp; 8755 8756 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8757 if (tcp->tcp_ipversion == IPV6_VERSION) 8758 return; 8759 8760 mp = mkiocb(IP_IOCTL); 8761 if (mp == NULL) 8762 return; 8763 8764 iocp = (struct iocblk *)mp->b_rptr; 8765 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8766 8767 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8768 if (!mp->b_cont) { 8769 freeb(mp); 8770 return; 8771 } 8772 8773 ipid = (ipid_t *)mp->b_cont->b_rptr; 8774 mp->b_cont->b_wptr += iocp->ioc_count; 8775 bzero(ipid, sizeof (*ipid)); 8776 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8777 ipid->ipid_ire_type = IRE_CACHE; 8778 ipid->ipid_addr_offset = sizeof (ipid_t); 8779 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8780 /* 8781 * Note: in the case of source routing we want to blow away the 8782 * route to the first source route hop. 8783 */ 8784 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8785 sizeof (tcp->tcp_ipha->ipha_dst)); 8786 8787 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8788 } 8789 8790 /* Unlink and return any mblk that looks like it contains an ire */ 8791 static mblk_t * 8792 tcp_ire_mp(mblk_t **mpp) 8793 { 8794 mblk_t *mp = *mpp; 8795 mblk_t *prev_mp = NULL; 8796 8797 for (;;) { 8798 switch (DB_TYPE(mp)) { 8799 case IRE_DB_TYPE: 8800 case IRE_DB_REQ_TYPE: 8801 if (mp == *mpp) { 8802 *mpp = mp->b_cont; 8803 } else { 8804 prev_mp->b_cont = mp->b_cont; 8805 } 8806 mp->b_cont = NULL; 8807 return (mp); 8808 default: 8809 break; 8810 } 8811 prev_mp = mp; 8812 mp = mp->b_cont; 8813 if (mp == NULL) 8814 break; 8815 } 8816 return (mp); 8817 } 8818 8819 /* 8820 * Timer callback routine for keepalive probe. We do a fake resend of 8821 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8822 * check to see if we have heard anything from the other end for the last 8823 * RTO period. If we have, set the timer to expire for another 8824 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8825 * RTO << 1 and check again when it expires. Keep exponentially increasing 8826 * the timeout if we have not heard from the other side. If for more than 8827 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8828 * kill the connection unless the keepalive abort threshold is 0. In 8829 * that case, we will probe "forever." 8830 */ 8831 static void 8832 tcp_keepalive_killer(void *arg) 8833 { 8834 mblk_t *mp; 8835 conn_t *connp = (conn_t *)arg; 8836 tcp_t *tcp = connp->conn_tcp; 8837 int32_t firetime; 8838 int32_t idletime; 8839 int32_t ka_intrvl; 8840 tcp_stack_t *tcps = tcp->tcp_tcps; 8841 8842 tcp->tcp_ka_tid = 0; 8843 8844 if (tcp->tcp_fused) 8845 return; 8846 8847 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8848 ka_intrvl = tcp->tcp_ka_interval; 8849 8850 /* 8851 * Keepalive probe should only be sent if the application has not 8852 * done a close on the connection. 8853 */ 8854 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8855 return; 8856 } 8857 /* Timer fired too early, restart it. */ 8858 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8859 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8860 MSEC_TO_TICK(ka_intrvl)); 8861 return; 8862 } 8863 8864 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8865 /* 8866 * If we have not heard from the other side for a long 8867 * time, kill the connection unless the keepalive abort 8868 * threshold is 0. In that case, we will probe "forever." 8869 */ 8870 if (tcp->tcp_ka_abort_thres != 0 && 8871 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8872 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8873 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8874 tcp->tcp_client_errno : ETIMEDOUT, 11); 8875 return; 8876 } 8877 8878 if (tcp->tcp_snxt == tcp->tcp_suna && 8879 idletime >= ka_intrvl) { 8880 /* Fake resend of last ACKed byte. */ 8881 mblk_t *mp1 = allocb(1, BPRI_LO); 8882 8883 if (mp1 != NULL) { 8884 *mp1->b_wptr++ = '\0'; 8885 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8886 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8887 freeb(mp1); 8888 /* 8889 * if allocation failed, fall through to start the 8890 * timer back. 8891 */ 8892 if (mp != NULL) { 8893 tcp_send_data(tcp, tcp->tcp_wq, mp); 8894 BUMP_MIB(&tcps->tcps_mib, 8895 tcpTimKeepaliveProbe); 8896 if (tcp->tcp_ka_last_intrvl != 0) { 8897 int max; 8898 /* 8899 * We should probe again at least 8900 * in ka_intrvl, but not more than 8901 * tcp_rexmit_interval_max. 8902 */ 8903 max = tcps->tcps_rexmit_interval_max; 8904 firetime = MIN(ka_intrvl - 1, 8905 tcp->tcp_ka_last_intrvl << 1); 8906 if (firetime > max) 8907 firetime = max; 8908 } else { 8909 firetime = tcp->tcp_rto; 8910 } 8911 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8912 tcp_keepalive_killer, 8913 MSEC_TO_TICK(firetime)); 8914 tcp->tcp_ka_last_intrvl = firetime; 8915 return; 8916 } 8917 } 8918 } else { 8919 tcp->tcp_ka_last_intrvl = 0; 8920 } 8921 8922 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8923 if ((firetime = ka_intrvl - idletime) < 0) { 8924 firetime = ka_intrvl; 8925 } 8926 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8927 MSEC_TO_TICK(firetime)); 8928 } 8929 8930 int 8931 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8932 { 8933 queue_t *q = tcp->tcp_rq; 8934 int32_t mss = tcp->tcp_mss; 8935 int maxpsz; 8936 conn_t *connp = tcp->tcp_connp; 8937 8938 if (TCP_IS_DETACHED(tcp)) 8939 return (mss); 8940 if (tcp->tcp_fused) { 8941 maxpsz = tcp_fuse_maxpsz_set(tcp); 8942 mss = INFPSZ; 8943 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8944 /* 8945 * Set the sd_qn_maxpsz according to the socket send buffer 8946 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8947 * instruct the stream head to copyin user data into contiguous 8948 * kernel-allocated buffers without breaking it up into smaller 8949 * chunks. We round up the buffer size to the nearest SMSS. 8950 */ 8951 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8952 if (tcp->tcp_kssl_ctx == NULL) 8953 mss = INFPSZ; 8954 else 8955 mss = SSL3_MAX_RECORD_LEN; 8956 } else { 8957 /* 8958 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8959 * (and a multiple of the mss). This instructs the stream 8960 * head to break down larger than SMSS writes into SMSS- 8961 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8962 */ 8963 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8964 maxpsz = tcp->tcp_maxpsz * mss; 8965 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8966 maxpsz = tcp->tcp_xmit_hiwater/2; 8967 /* Round up to nearest mss */ 8968 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8969 } 8970 } 8971 8972 (void) proto_set_maxpsz(q, connp, maxpsz); 8973 if (!(IPCL_IS_NONSTR(connp))) { 8974 /* XXX do it in set_maxpsz()? */ 8975 tcp->tcp_wq->q_maxpsz = maxpsz; 8976 } 8977 8978 if (set_maxblk) 8979 (void) proto_set_tx_maxblk(q, connp, mss); 8980 return (mss); 8981 } 8982 8983 /* 8984 * Extract option values from a tcp header. We put any found values into the 8985 * tcpopt struct and return a bitmask saying which options were found. 8986 */ 8987 static int 8988 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8989 { 8990 uchar_t *endp; 8991 int len; 8992 uint32_t mss; 8993 uchar_t *up = (uchar_t *)tcph; 8994 int found = 0; 8995 int32_t sack_len; 8996 tcp_seq sack_begin, sack_end; 8997 tcp_t *tcp; 8998 8999 endp = up + TCP_HDR_LENGTH(tcph); 9000 up += TCP_MIN_HEADER_LENGTH; 9001 while (up < endp) { 9002 len = endp - up; 9003 switch (*up) { 9004 case TCPOPT_EOL: 9005 break; 9006 9007 case TCPOPT_NOP: 9008 up++; 9009 continue; 9010 9011 case TCPOPT_MAXSEG: 9012 if (len < TCPOPT_MAXSEG_LEN || 9013 up[1] != TCPOPT_MAXSEG_LEN) 9014 break; 9015 9016 mss = BE16_TO_U16(up+2); 9017 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9018 tcpopt->tcp_opt_mss = mss; 9019 found |= TCP_OPT_MSS_PRESENT; 9020 9021 up += TCPOPT_MAXSEG_LEN; 9022 continue; 9023 9024 case TCPOPT_WSCALE: 9025 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9026 break; 9027 9028 if (up[2] > TCP_MAX_WINSHIFT) 9029 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9030 else 9031 tcpopt->tcp_opt_wscale = up[2]; 9032 found |= TCP_OPT_WSCALE_PRESENT; 9033 9034 up += TCPOPT_WS_LEN; 9035 continue; 9036 9037 case TCPOPT_SACK_PERMITTED: 9038 if (len < TCPOPT_SACK_OK_LEN || 9039 up[1] != TCPOPT_SACK_OK_LEN) 9040 break; 9041 found |= TCP_OPT_SACK_OK_PRESENT; 9042 up += TCPOPT_SACK_OK_LEN; 9043 continue; 9044 9045 case TCPOPT_SACK: 9046 if (len <= 2 || up[1] <= 2 || len < up[1]) 9047 break; 9048 9049 /* If TCP is not interested in SACK blks... */ 9050 if ((tcp = tcpopt->tcp) == NULL) { 9051 up += up[1]; 9052 continue; 9053 } 9054 sack_len = up[1] - TCPOPT_HEADER_LEN; 9055 up += TCPOPT_HEADER_LEN; 9056 9057 /* 9058 * If the list is empty, allocate one and assume 9059 * nothing is sack'ed. 9060 */ 9061 ASSERT(tcp->tcp_sack_info != NULL); 9062 if (tcp->tcp_notsack_list == NULL) { 9063 tcp_notsack_update(&(tcp->tcp_notsack_list), 9064 tcp->tcp_suna, tcp->tcp_snxt, 9065 &(tcp->tcp_num_notsack_blk), 9066 &(tcp->tcp_cnt_notsack_list)); 9067 9068 /* 9069 * Make sure tcp_notsack_list is not NULL. 9070 * This happens when kmem_alloc(KM_NOSLEEP) 9071 * returns NULL. 9072 */ 9073 if (tcp->tcp_notsack_list == NULL) { 9074 up += sack_len; 9075 continue; 9076 } 9077 tcp->tcp_fack = tcp->tcp_suna; 9078 } 9079 9080 while (sack_len > 0) { 9081 if (up + 8 > endp) { 9082 up = endp; 9083 break; 9084 } 9085 sack_begin = BE32_TO_U32(up); 9086 up += 4; 9087 sack_end = BE32_TO_U32(up); 9088 up += 4; 9089 sack_len -= 8; 9090 /* 9091 * Bounds checking. Make sure the SACK 9092 * info is within tcp_suna and tcp_snxt. 9093 * If this SACK blk is out of bound, ignore 9094 * it but continue to parse the following 9095 * blks. 9096 */ 9097 if (SEQ_LEQ(sack_end, sack_begin) || 9098 SEQ_LT(sack_begin, tcp->tcp_suna) || 9099 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9100 continue; 9101 } 9102 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9103 sack_begin, sack_end, 9104 &(tcp->tcp_num_notsack_blk), 9105 &(tcp->tcp_cnt_notsack_list)); 9106 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9107 tcp->tcp_fack = sack_end; 9108 } 9109 } 9110 found |= TCP_OPT_SACK_PRESENT; 9111 continue; 9112 9113 case TCPOPT_TSTAMP: 9114 if (len < TCPOPT_TSTAMP_LEN || 9115 up[1] != TCPOPT_TSTAMP_LEN) 9116 break; 9117 9118 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9119 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9120 9121 found |= TCP_OPT_TSTAMP_PRESENT; 9122 9123 up += TCPOPT_TSTAMP_LEN; 9124 continue; 9125 9126 default: 9127 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9128 break; 9129 up += up[1]; 9130 continue; 9131 } 9132 break; 9133 } 9134 return (found); 9135 } 9136 9137 /* 9138 * Set the mss associated with a particular tcp based on its current value, 9139 * and a new one passed in. Observe minimums and maximums, and reset 9140 * other state variables that we want to view as multiples of mss. 9141 * 9142 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9143 * highwater marks etc. need to be initialized or adjusted. 9144 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9145 * packet arrives. 9146 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9147 * ICMP6_PACKET_TOO_BIG arrives. 9148 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9149 * to increase the MSS to use the extra bytes available. 9150 * 9151 * Callers except tcp_paws_check() ensure that they only reduce mss. 9152 */ 9153 static void 9154 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9155 { 9156 uint32_t mss_max; 9157 tcp_stack_t *tcps = tcp->tcp_tcps; 9158 9159 if (tcp->tcp_ipversion == IPV4_VERSION) 9160 mss_max = tcps->tcps_mss_max_ipv4; 9161 else 9162 mss_max = tcps->tcps_mss_max_ipv6; 9163 9164 if (mss < tcps->tcps_mss_min) 9165 mss = tcps->tcps_mss_min; 9166 if (mss > mss_max) 9167 mss = mss_max; 9168 /* 9169 * Unless naglim has been set by our client to 9170 * a non-mss value, force naglim to track mss. 9171 * This can help to aggregate small writes. 9172 */ 9173 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9174 tcp->tcp_naglim = mss; 9175 /* 9176 * TCP should be able to buffer at least 4 MSS data for obvious 9177 * performance reason. 9178 */ 9179 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9180 tcp->tcp_xmit_hiwater = mss << 2; 9181 9182 if (do_ss) { 9183 /* 9184 * Either the tcp_cwnd is as yet uninitialized, or mss is 9185 * changing due to a reduction in MTU, presumably as a 9186 * result of a new path component, reset cwnd to its 9187 * "initial" value, as a multiple of the new mss. 9188 */ 9189 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9190 } else { 9191 /* 9192 * Called by tcp_paws_check(), the mss increased 9193 * marginally to allow use of space previously taken 9194 * by the timestamp option. It would be inappropriate 9195 * to apply slow start or tcp_init_cwnd values to 9196 * tcp_cwnd, simply adjust to a multiple of the new mss. 9197 */ 9198 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9199 tcp->tcp_cwnd_cnt = 0; 9200 } 9201 tcp->tcp_mss = mss; 9202 (void) tcp_maxpsz_set(tcp, B_TRUE); 9203 } 9204 9205 /* For /dev/tcp aka AF_INET open */ 9206 static int 9207 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9208 { 9209 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9210 } 9211 9212 /* For /dev/tcp6 aka AF_INET6 open */ 9213 static int 9214 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9215 { 9216 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9217 } 9218 9219 static conn_t * 9220 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9221 boolean_t issocket, int *errorp) 9222 { 9223 tcp_t *tcp = NULL; 9224 conn_t *connp; 9225 int err; 9226 zoneid_t zoneid; 9227 tcp_stack_t *tcps; 9228 squeue_t *sqp; 9229 9230 ASSERT(errorp != NULL); 9231 /* 9232 * Find the proper zoneid and netstack. 9233 */ 9234 /* 9235 * Special case for install: miniroot needs to be able to 9236 * access files via NFS as though it were always in the 9237 * global zone. 9238 */ 9239 if (credp == kcred && nfs_global_client_only != 0) { 9240 zoneid = GLOBAL_ZONEID; 9241 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9242 netstack_tcp; 9243 ASSERT(tcps != NULL); 9244 } else { 9245 netstack_t *ns; 9246 9247 ns = netstack_find_by_cred(credp); 9248 ASSERT(ns != NULL); 9249 tcps = ns->netstack_tcp; 9250 ASSERT(tcps != NULL); 9251 9252 /* 9253 * For exclusive stacks we set the zoneid to zero 9254 * to make TCP operate as if in the global zone. 9255 */ 9256 if (tcps->tcps_netstack->netstack_stackid != 9257 GLOBAL_NETSTACKID) 9258 zoneid = GLOBAL_ZONEID; 9259 else 9260 zoneid = crgetzoneid(credp); 9261 } 9262 /* 9263 * For stackid zero this is done from strplumb.c, but 9264 * non-zero stackids are handled here. 9265 */ 9266 if (tcps->tcps_g_q == NULL && 9267 tcps->tcps_netstack->netstack_stackid != 9268 GLOBAL_NETSTACKID) { 9269 tcp_g_q_setup(tcps); 9270 } 9271 9272 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9273 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9274 /* 9275 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9276 * so we drop it by one. 9277 */ 9278 netstack_rele(tcps->tcps_netstack); 9279 if (connp == NULL) { 9280 *errorp = ENOSR; 9281 return (NULL); 9282 } 9283 connp->conn_sqp = sqp; 9284 connp->conn_initial_sqp = connp->conn_sqp; 9285 tcp = connp->conn_tcp; 9286 9287 if (isv6) { 9288 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9289 connp->conn_send = ip_output_v6; 9290 connp->conn_af_isv6 = B_TRUE; 9291 connp->conn_pkt_isv6 = B_TRUE; 9292 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9293 tcp->tcp_ipversion = IPV6_VERSION; 9294 tcp->tcp_family = AF_INET6; 9295 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9296 } else { 9297 connp->conn_flags |= IPCL_TCP4; 9298 connp->conn_send = ip_output; 9299 connp->conn_af_isv6 = B_FALSE; 9300 connp->conn_pkt_isv6 = B_FALSE; 9301 tcp->tcp_ipversion = IPV4_VERSION; 9302 tcp->tcp_family = AF_INET; 9303 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9304 } 9305 9306 /* 9307 * TCP keeps a copy of cred for cache locality reasons but 9308 * we put a reference only once. If connp->conn_cred 9309 * becomes invalid, tcp_cred should also be set to NULL. 9310 */ 9311 tcp->tcp_cred = connp->conn_cred = credp; 9312 crhold(connp->conn_cred); 9313 tcp->tcp_cpid = curproc->p_pid; 9314 tcp->tcp_open_time = lbolt64; 9315 connp->conn_zoneid = zoneid; 9316 connp->conn_mlp_type = mlptSingle; 9317 connp->conn_ulp_labeled = !is_system_labeled(); 9318 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9319 ASSERT(tcp->tcp_tcps == tcps); 9320 9321 /* 9322 * If the caller has the process-wide flag set, then default to MAC 9323 * exempt mode. This allows read-down to unlabeled hosts. 9324 */ 9325 if (getpflags(NET_MAC_AWARE, credp) != 0) 9326 connp->conn_mac_exempt = B_TRUE; 9327 9328 connp->conn_dev = NULL; 9329 if (issocket) { 9330 connp->conn_flags |= IPCL_SOCKET; 9331 tcp->tcp_issocket = 1; 9332 } 9333 9334 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9335 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9336 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9337 9338 /* Non-zero default values */ 9339 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9340 9341 if (q == NULL) { 9342 /* 9343 * Create a helper stream for non-STREAMS socket. 9344 */ 9345 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9346 if (err != 0) { 9347 ip1dbg(("tcp_create_common: create of IP helper stream " 9348 "failed\n")); 9349 CONN_DEC_REF(connp); 9350 *errorp = err; 9351 return (NULL); 9352 } 9353 q = connp->conn_rq; 9354 } else { 9355 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9356 } 9357 9358 SOCK_CONNID_INIT(tcp->tcp_connid); 9359 err = tcp_init(tcp, q); 9360 if (err != 0) { 9361 CONN_DEC_REF(connp); 9362 *errorp = err; 9363 return (NULL); 9364 } 9365 9366 return (connp); 9367 } 9368 9369 static int 9370 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9371 boolean_t isv6) 9372 { 9373 tcp_t *tcp = NULL; 9374 conn_t *connp = NULL; 9375 int err; 9376 vmem_t *minor_arena = NULL; 9377 dev_t conn_dev; 9378 boolean_t issocket; 9379 9380 if (q->q_ptr != NULL) 9381 return (0); 9382 9383 if (sflag == MODOPEN) 9384 return (EINVAL); 9385 9386 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9387 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9388 minor_arena = ip_minor_arena_la; 9389 } else { 9390 /* 9391 * Either minor numbers in the large arena were exhausted 9392 * or a non socket application is doing the open. 9393 * Try to allocate from the small arena. 9394 */ 9395 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9396 return (EBUSY); 9397 } 9398 minor_arena = ip_minor_arena_sa; 9399 } 9400 9401 ASSERT(minor_arena != NULL); 9402 9403 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9404 9405 if (flag & SO_FALLBACK) { 9406 /* 9407 * Non streams socket needs a stream to fallback to 9408 */ 9409 RD(q)->q_ptr = (void *)conn_dev; 9410 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9411 WR(q)->q_ptr = (void *)minor_arena; 9412 qprocson(q); 9413 return (0); 9414 } else if (flag & SO_ACCEPTOR) { 9415 q->q_qinfo = &tcp_acceptor_rinit; 9416 /* 9417 * the conn_dev and minor_arena will be subsequently used by 9418 * tcp_wput_accept() and tcpclose_accept() to figure out the 9419 * minor device number for this connection from the q_ptr. 9420 */ 9421 RD(q)->q_ptr = (void *)conn_dev; 9422 WR(q)->q_qinfo = &tcp_acceptor_winit; 9423 WR(q)->q_ptr = (void *)minor_arena; 9424 qprocson(q); 9425 return (0); 9426 } 9427 9428 issocket = flag & SO_SOCKSTR; 9429 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9430 9431 if (connp == NULL) { 9432 inet_minor_free(minor_arena, conn_dev); 9433 q->q_ptr = WR(q)->q_ptr = NULL; 9434 return (err); 9435 } 9436 9437 q->q_ptr = WR(q)->q_ptr = connp; 9438 9439 connp->conn_dev = conn_dev; 9440 connp->conn_minor_arena = minor_arena; 9441 9442 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9443 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9444 9445 if (issocket) { 9446 WR(q)->q_qinfo = &tcp_sock_winit; 9447 } else { 9448 tcp = connp->conn_tcp; 9449 #ifdef _ILP32 9450 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9451 #else 9452 tcp->tcp_acceptor_id = conn_dev; 9453 #endif /* _ILP32 */ 9454 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9455 } 9456 9457 /* 9458 * Put the ref for TCP. Ref for IP was already put 9459 * by ipcl_conn_create. Also Make the conn_t globally 9460 * visible to walkers 9461 */ 9462 mutex_enter(&connp->conn_lock); 9463 CONN_INC_REF_LOCKED(connp); 9464 ASSERT(connp->conn_ref == 2); 9465 connp->conn_state_flags &= ~CONN_INCIPIENT; 9466 mutex_exit(&connp->conn_lock); 9467 9468 qprocson(q); 9469 return (0); 9470 } 9471 9472 /* 9473 * Some TCP options can be "set" by requesting them in the option 9474 * buffer. This is needed for XTI feature test though we do not 9475 * allow it in general. We interpret that this mechanism is more 9476 * applicable to OSI protocols and need not be allowed in general. 9477 * This routine filters out options for which it is not allowed (most) 9478 * and lets through those (few) for which it is. [ The XTI interface 9479 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9480 * ever implemented will have to be allowed here ]. 9481 */ 9482 static boolean_t 9483 tcp_allow_connopt_set(int level, int name) 9484 { 9485 9486 switch (level) { 9487 case IPPROTO_TCP: 9488 switch (name) { 9489 case TCP_NODELAY: 9490 return (B_TRUE); 9491 default: 9492 return (B_FALSE); 9493 } 9494 /*NOTREACHED*/ 9495 default: 9496 return (B_FALSE); 9497 } 9498 /*NOTREACHED*/ 9499 } 9500 9501 /* 9502 * this routine gets default values of certain options whose default 9503 * values are maintained by protocol specific code 9504 */ 9505 /* ARGSUSED */ 9506 int 9507 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9508 { 9509 int32_t *i1 = (int32_t *)ptr; 9510 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9511 9512 switch (level) { 9513 case IPPROTO_TCP: 9514 switch (name) { 9515 case TCP_NOTIFY_THRESHOLD: 9516 *i1 = tcps->tcps_ip_notify_interval; 9517 break; 9518 case TCP_ABORT_THRESHOLD: 9519 *i1 = tcps->tcps_ip_abort_interval; 9520 break; 9521 case TCP_CONN_NOTIFY_THRESHOLD: 9522 *i1 = tcps->tcps_ip_notify_cinterval; 9523 break; 9524 case TCP_CONN_ABORT_THRESHOLD: 9525 *i1 = tcps->tcps_ip_abort_cinterval; 9526 break; 9527 default: 9528 return (-1); 9529 } 9530 break; 9531 case IPPROTO_IP: 9532 switch (name) { 9533 case IP_TTL: 9534 *i1 = tcps->tcps_ipv4_ttl; 9535 break; 9536 default: 9537 return (-1); 9538 } 9539 break; 9540 case IPPROTO_IPV6: 9541 switch (name) { 9542 case IPV6_UNICAST_HOPS: 9543 *i1 = tcps->tcps_ipv6_hoplimit; 9544 break; 9545 default: 9546 return (-1); 9547 } 9548 break; 9549 default: 9550 return (-1); 9551 } 9552 return (sizeof (int)); 9553 } 9554 9555 static int 9556 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9557 { 9558 int *i1 = (int *)ptr; 9559 tcp_t *tcp = connp->conn_tcp; 9560 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9561 9562 switch (level) { 9563 case SOL_SOCKET: 9564 switch (name) { 9565 case SO_LINGER: { 9566 struct linger *lgr = (struct linger *)ptr; 9567 9568 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9569 lgr->l_linger = tcp->tcp_lingertime; 9570 } 9571 return (sizeof (struct linger)); 9572 case SO_DEBUG: 9573 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9574 break; 9575 case SO_KEEPALIVE: 9576 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9577 break; 9578 case SO_DONTROUTE: 9579 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9580 break; 9581 case SO_USELOOPBACK: 9582 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9583 break; 9584 case SO_BROADCAST: 9585 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9586 break; 9587 case SO_REUSEADDR: 9588 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9589 break; 9590 case SO_OOBINLINE: 9591 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9592 break; 9593 case SO_DGRAM_ERRIND: 9594 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9595 break; 9596 case SO_TYPE: 9597 *i1 = SOCK_STREAM; 9598 break; 9599 case SO_SNDBUF: 9600 *i1 = tcp->tcp_xmit_hiwater; 9601 break; 9602 case SO_RCVBUF: 9603 *i1 = tcp->tcp_recv_hiwater; 9604 break; 9605 case SO_SND_COPYAVOID: 9606 *i1 = tcp->tcp_snd_zcopy_on ? 9607 SO_SND_COPYAVOID : 0; 9608 break; 9609 case SO_ALLZONES: 9610 *i1 = connp->conn_allzones ? 1 : 0; 9611 break; 9612 case SO_ANON_MLP: 9613 *i1 = connp->conn_anon_mlp; 9614 break; 9615 case SO_MAC_EXEMPT: 9616 *i1 = connp->conn_mac_exempt; 9617 break; 9618 case SO_EXCLBIND: 9619 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9620 break; 9621 case SO_PROTOTYPE: 9622 *i1 = IPPROTO_TCP; 9623 break; 9624 case SO_DOMAIN: 9625 *i1 = tcp->tcp_family; 9626 break; 9627 case SO_ACCEPTCONN: 9628 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9629 default: 9630 return (-1); 9631 } 9632 break; 9633 case IPPROTO_TCP: 9634 switch (name) { 9635 case TCP_NODELAY: 9636 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9637 break; 9638 case TCP_MAXSEG: 9639 *i1 = tcp->tcp_mss; 9640 break; 9641 case TCP_NOTIFY_THRESHOLD: 9642 *i1 = (int)tcp->tcp_first_timer_threshold; 9643 break; 9644 case TCP_ABORT_THRESHOLD: 9645 *i1 = tcp->tcp_second_timer_threshold; 9646 break; 9647 case TCP_CONN_NOTIFY_THRESHOLD: 9648 *i1 = tcp->tcp_first_ctimer_threshold; 9649 break; 9650 case TCP_CONN_ABORT_THRESHOLD: 9651 *i1 = tcp->tcp_second_ctimer_threshold; 9652 break; 9653 case TCP_RECVDSTADDR: 9654 *i1 = tcp->tcp_recvdstaddr; 9655 break; 9656 case TCP_ANONPRIVBIND: 9657 *i1 = tcp->tcp_anon_priv_bind; 9658 break; 9659 case TCP_EXCLBIND: 9660 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9661 break; 9662 case TCP_INIT_CWND: 9663 *i1 = tcp->tcp_init_cwnd; 9664 break; 9665 case TCP_KEEPALIVE_THRESHOLD: 9666 *i1 = tcp->tcp_ka_interval; 9667 break; 9668 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9669 *i1 = tcp->tcp_ka_abort_thres; 9670 break; 9671 case TCP_CORK: 9672 *i1 = tcp->tcp_cork; 9673 break; 9674 default: 9675 return (-1); 9676 } 9677 break; 9678 case IPPROTO_IP: 9679 if (tcp->tcp_family != AF_INET) 9680 return (-1); 9681 switch (name) { 9682 case IP_OPTIONS: 9683 case T_IP_OPTIONS: { 9684 /* 9685 * This is compatible with BSD in that in only return 9686 * the reverse source route with the final destination 9687 * as the last entry. The first 4 bytes of the option 9688 * will contain the final destination. 9689 */ 9690 int opt_len; 9691 9692 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9693 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9694 ASSERT(opt_len >= 0); 9695 /* Caller ensures enough space */ 9696 if (opt_len > 0) { 9697 /* 9698 * TODO: Do we have to handle getsockopt on an 9699 * initiator as well? 9700 */ 9701 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9702 } 9703 return (0); 9704 } 9705 case IP_TOS: 9706 case T_IP_TOS: 9707 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9708 break; 9709 case IP_TTL: 9710 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9711 break; 9712 case IP_NEXTHOP: 9713 /* Handled at IP level */ 9714 return (-EINVAL); 9715 default: 9716 return (-1); 9717 } 9718 break; 9719 case IPPROTO_IPV6: 9720 /* 9721 * IPPROTO_IPV6 options are only supported for sockets 9722 * that are using IPv6 on the wire. 9723 */ 9724 if (tcp->tcp_ipversion != IPV6_VERSION) { 9725 return (-1); 9726 } 9727 switch (name) { 9728 case IPV6_UNICAST_HOPS: 9729 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9730 break; /* goto sizeof (int) option return */ 9731 case IPV6_BOUND_IF: 9732 /* Zero if not set */ 9733 *i1 = tcp->tcp_bound_if; 9734 break; /* goto sizeof (int) option return */ 9735 case IPV6_RECVPKTINFO: 9736 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9737 *i1 = 1; 9738 else 9739 *i1 = 0; 9740 break; /* goto sizeof (int) option return */ 9741 case IPV6_RECVTCLASS: 9742 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9743 *i1 = 1; 9744 else 9745 *i1 = 0; 9746 break; /* goto sizeof (int) option return */ 9747 case IPV6_RECVHOPLIMIT: 9748 if (tcp->tcp_ipv6_recvancillary & 9749 TCP_IPV6_RECVHOPLIMIT) 9750 *i1 = 1; 9751 else 9752 *i1 = 0; 9753 break; /* goto sizeof (int) option return */ 9754 case IPV6_RECVHOPOPTS: 9755 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9756 *i1 = 1; 9757 else 9758 *i1 = 0; 9759 break; /* goto sizeof (int) option return */ 9760 case IPV6_RECVDSTOPTS: 9761 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9762 *i1 = 1; 9763 else 9764 *i1 = 0; 9765 break; /* goto sizeof (int) option return */ 9766 case _OLD_IPV6_RECVDSTOPTS: 9767 if (tcp->tcp_ipv6_recvancillary & 9768 TCP_OLD_IPV6_RECVDSTOPTS) 9769 *i1 = 1; 9770 else 9771 *i1 = 0; 9772 break; /* goto sizeof (int) option return */ 9773 case IPV6_RECVRTHDR: 9774 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9775 *i1 = 1; 9776 else 9777 *i1 = 0; 9778 break; /* goto sizeof (int) option return */ 9779 case IPV6_RECVRTHDRDSTOPTS: 9780 if (tcp->tcp_ipv6_recvancillary & 9781 TCP_IPV6_RECVRTDSTOPTS) 9782 *i1 = 1; 9783 else 9784 *i1 = 0; 9785 break; /* goto sizeof (int) option return */ 9786 case IPV6_PKTINFO: { 9787 /* XXX assumes that caller has room for max size! */ 9788 struct in6_pktinfo *pkti; 9789 9790 pkti = (struct in6_pktinfo *)ptr; 9791 if (ipp->ipp_fields & IPPF_IFINDEX) 9792 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9793 else 9794 pkti->ipi6_ifindex = 0; 9795 if (ipp->ipp_fields & IPPF_ADDR) 9796 pkti->ipi6_addr = ipp->ipp_addr; 9797 else 9798 pkti->ipi6_addr = ipv6_all_zeros; 9799 return (sizeof (struct in6_pktinfo)); 9800 } 9801 case IPV6_TCLASS: 9802 if (ipp->ipp_fields & IPPF_TCLASS) 9803 *i1 = ipp->ipp_tclass; 9804 else 9805 *i1 = IPV6_FLOW_TCLASS( 9806 IPV6_DEFAULT_VERS_AND_FLOW); 9807 break; /* goto sizeof (int) option return */ 9808 case IPV6_NEXTHOP: { 9809 sin6_t *sin6 = (sin6_t *)ptr; 9810 9811 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9812 return (0); 9813 *sin6 = sin6_null; 9814 sin6->sin6_family = AF_INET6; 9815 sin6->sin6_addr = ipp->ipp_nexthop; 9816 return (sizeof (sin6_t)); 9817 } 9818 case IPV6_HOPOPTS: 9819 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9820 return (0); 9821 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9822 return (0); 9823 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9824 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9825 if (tcp->tcp_label_len > 0) { 9826 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9827 ptr[1] = (ipp->ipp_hopoptslen - 9828 tcp->tcp_label_len + 7) / 8 - 1; 9829 } 9830 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9831 case IPV6_RTHDRDSTOPTS: 9832 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9833 return (0); 9834 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9835 return (ipp->ipp_rtdstoptslen); 9836 case IPV6_RTHDR: 9837 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9838 return (0); 9839 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9840 return (ipp->ipp_rthdrlen); 9841 case IPV6_DSTOPTS: 9842 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9843 return (0); 9844 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9845 return (ipp->ipp_dstoptslen); 9846 case IPV6_SRC_PREFERENCES: 9847 return (ip6_get_src_preferences(connp, 9848 (uint32_t *)ptr)); 9849 case IPV6_PATHMTU: { 9850 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9851 9852 if (tcp->tcp_state < TCPS_ESTABLISHED) 9853 return (-1); 9854 9855 return (ip_fill_mtuinfo(&connp->conn_remv6, 9856 connp->conn_fport, mtuinfo, 9857 connp->conn_netstack)); 9858 } 9859 default: 9860 return (-1); 9861 } 9862 break; 9863 default: 9864 return (-1); 9865 } 9866 return (sizeof (int)); 9867 } 9868 9869 /* 9870 * TCP routine to get the values of options. 9871 */ 9872 int 9873 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9874 { 9875 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9876 } 9877 9878 /* returns UNIX error, the optlen is a value-result arg */ 9879 int 9880 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9881 void *optvalp, socklen_t *optlen, cred_t *cr) 9882 { 9883 conn_t *connp = (conn_t *)proto_handle; 9884 squeue_t *sqp = connp->conn_sqp; 9885 int error; 9886 t_uscalar_t max_optbuf_len; 9887 void *optvalp_buf; 9888 int len; 9889 9890 ASSERT(connp->conn_upper_handle != NULL); 9891 9892 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9893 tcp_opt_obj.odb_opt_des_arr, 9894 tcp_opt_obj.odb_opt_arr_cnt, 9895 tcp_opt_obj.odb_topmost_tpiprovider, 9896 B_FALSE, B_TRUE, cr); 9897 if (error != 0) { 9898 if (error < 0) { 9899 error = proto_tlitosyserr(-error); 9900 } 9901 return (error); 9902 } 9903 9904 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9905 9906 error = squeue_synch_enter(sqp, connp, 0); 9907 if (error == ENOMEM) { 9908 return (ENOMEM); 9909 } 9910 9911 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9912 squeue_synch_exit(sqp, connp); 9913 9914 if (len < 0) { 9915 /* 9916 * Pass on to IP 9917 */ 9918 kmem_free(optvalp_buf, max_optbuf_len); 9919 return (ip_get_options(connp, level, option_name, 9920 optvalp, optlen, cr)); 9921 } else { 9922 /* 9923 * update optlen and copy option value 9924 */ 9925 t_uscalar_t size = MIN(len, *optlen); 9926 bcopy(optvalp_buf, optvalp, size); 9927 bcopy(&size, optlen, sizeof (size)); 9928 9929 kmem_free(optvalp_buf, max_optbuf_len); 9930 return (0); 9931 } 9932 } 9933 9934 /* 9935 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9936 * Parameters are assumed to be verified by the caller. 9937 */ 9938 /* ARGSUSED */ 9939 int 9940 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9941 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9942 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9943 { 9944 tcp_t *tcp = connp->conn_tcp; 9945 int *i1 = (int *)invalp; 9946 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9947 boolean_t checkonly; 9948 int reterr; 9949 tcp_stack_t *tcps = tcp->tcp_tcps; 9950 9951 switch (optset_context) { 9952 case SETFN_OPTCOM_CHECKONLY: 9953 checkonly = B_TRUE; 9954 /* 9955 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9956 * inlen != 0 implies value supplied and 9957 * we have to "pretend" to set it. 9958 * inlen == 0 implies that there is no 9959 * value part in T_CHECK request and just validation 9960 * done elsewhere should be enough, we just return here. 9961 */ 9962 if (inlen == 0) { 9963 *outlenp = 0; 9964 return (0); 9965 } 9966 break; 9967 case SETFN_OPTCOM_NEGOTIATE: 9968 checkonly = B_FALSE; 9969 break; 9970 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9971 case SETFN_CONN_NEGOTIATE: 9972 checkonly = B_FALSE; 9973 /* 9974 * Negotiating local and "association-related" options 9975 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9976 * primitives is allowed by XTI, but we choose 9977 * to not implement this style negotiation for Internet 9978 * protocols (We interpret it is a must for OSI world but 9979 * optional for Internet protocols) for all options. 9980 * [ Will do only for the few options that enable test 9981 * suites that our XTI implementation of this feature 9982 * works for transports that do allow it ] 9983 */ 9984 if (!tcp_allow_connopt_set(level, name)) { 9985 *outlenp = 0; 9986 return (EINVAL); 9987 } 9988 break; 9989 default: 9990 /* 9991 * We should never get here 9992 */ 9993 *outlenp = 0; 9994 return (EINVAL); 9995 } 9996 9997 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9998 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9999 10000 /* 10001 * For TCP, we should have no ancillary data sent down 10002 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10003 * has to be zero. 10004 */ 10005 ASSERT(thisdg_attrs == NULL); 10006 10007 /* 10008 * For fixed length options, no sanity check 10009 * of passed in length is done. It is assumed *_optcom_req() 10010 * routines do the right thing. 10011 */ 10012 switch (level) { 10013 case SOL_SOCKET: 10014 switch (name) { 10015 case SO_LINGER: { 10016 struct linger *lgr = (struct linger *)invalp; 10017 10018 if (!checkonly) { 10019 if (lgr->l_onoff) { 10020 tcp->tcp_linger = 1; 10021 tcp->tcp_lingertime = lgr->l_linger; 10022 } else { 10023 tcp->tcp_linger = 0; 10024 tcp->tcp_lingertime = 0; 10025 } 10026 /* struct copy */ 10027 *(struct linger *)outvalp = *lgr; 10028 } else { 10029 if (!lgr->l_onoff) { 10030 ((struct linger *) 10031 outvalp)->l_onoff = 0; 10032 ((struct linger *) 10033 outvalp)->l_linger = 0; 10034 } else { 10035 /* struct copy */ 10036 *(struct linger *)outvalp = *lgr; 10037 } 10038 } 10039 *outlenp = sizeof (struct linger); 10040 return (0); 10041 } 10042 case SO_DEBUG: 10043 if (!checkonly) 10044 tcp->tcp_debug = onoff; 10045 break; 10046 case SO_KEEPALIVE: 10047 if (checkonly) { 10048 /* check only case */ 10049 break; 10050 } 10051 10052 if (!onoff) { 10053 if (tcp->tcp_ka_enabled) { 10054 if (tcp->tcp_ka_tid != 0) { 10055 (void) TCP_TIMER_CANCEL(tcp, 10056 tcp->tcp_ka_tid); 10057 tcp->tcp_ka_tid = 0; 10058 } 10059 tcp->tcp_ka_enabled = 0; 10060 } 10061 break; 10062 } 10063 if (!tcp->tcp_ka_enabled) { 10064 /* Crank up the keepalive timer */ 10065 tcp->tcp_ka_last_intrvl = 0; 10066 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10067 tcp_keepalive_killer, 10068 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10069 tcp->tcp_ka_enabled = 1; 10070 } 10071 break; 10072 case SO_DONTROUTE: 10073 /* 10074 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10075 * only of interest to IP. We track them here only so 10076 * that we can report their current value. 10077 */ 10078 if (!checkonly) { 10079 tcp->tcp_dontroute = onoff; 10080 tcp->tcp_connp->conn_dontroute = onoff; 10081 } 10082 break; 10083 case SO_USELOOPBACK: 10084 if (!checkonly) { 10085 tcp->tcp_useloopback = onoff; 10086 tcp->tcp_connp->conn_loopback = onoff; 10087 } 10088 break; 10089 case SO_BROADCAST: 10090 if (!checkonly) { 10091 tcp->tcp_broadcast = onoff; 10092 tcp->tcp_connp->conn_broadcast = onoff; 10093 } 10094 break; 10095 case SO_REUSEADDR: 10096 if (!checkonly) { 10097 tcp->tcp_reuseaddr = onoff; 10098 tcp->tcp_connp->conn_reuseaddr = onoff; 10099 } 10100 break; 10101 case SO_OOBINLINE: 10102 if (!checkonly) { 10103 tcp->tcp_oobinline = onoff; 10104 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10105 proto_set_rx_oob_opt(connp, onoff); 10106 } 10107 break; 10108 case SO_DGRAM_ERRIND: 10109 if (!checkonly) 10110 tcp->tcp_dgram_errind = onoff; 10111 break; 10112 case SO_SNDBUF: { 10113 if (*i1 > tcps->tcps_max_buf) { 10114 *outlenp = 0; 10115 return (ENOBUFS); 10116 } 10117 if (checkonly) 10118 break; 10119 10120 tcp->tcp_xmit_hiwater = *i1; 10121 if (tcps->tcps_snd_lowat_fraction != 0) 10122 tcp->tcp_xmit_lowater = 10123 tcp->tcp_xmit_hiwater / 10124 tcps->tcps_snd_lowat_fraction; 10125 (void) tcp_maxpsz_set(tcp, B_TRUE); 10126 /* 10127 * If we are flow-controlled, recheck the condition. 10128 * There are apps that increase SO_SNDBUF size when 10129 * flow-controlled (EWOULDBLOCK), and expect the flow 10130 * control condition to be lifted right away. 10131 */ 10132 mutex_enter(&tcp->tcp_non_sq_lock); 10133 if (tcp->tcp_flow_stopped && 10134 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10135 tcp_clrqfull(tcp); 10136 } 10137 mutex_exit(&tcp->tcp_non_sq_lock); 10138 break; 10139 } 10140 case SO_RCVBUF: 10141 if (*i1 > tcps->tcps_max_buf) { 10142 *outlenp = 0; 10143 return (ENOBUFS); 10144 } 10145 /* Silently ignore zero */ 10146 if (!checkonly && *i1 != 0) { 10147 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10148 (void) tcp_rwnd_set(tcp, *i1); 10149 } 10150 /* 10151 * XXX should we return the rwnd here 10152 * and tcp_opt_get ? 10153 */ 10154 break; 10155 case SO_SND_COPYAVOID: 10156 if (!checkonly) { 10157 /* we only allow enable at most once for now */ 10158 if (tcp->tcp_loopback || 10159 (tcp->tcp_kssl_ctx != NULL) || 10160 (!tcp->tcp_snd_zcopy_aware && 10161 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10162 *outlenp = 0; 10163 return (EOPNOTSUPP); 10164 } 10165 tcp->tcp_snd_zcopy_aware = 1; 10166 } 10167 break; 10168 case SO_RCVTIMEO: 10169 case SO_SNDTIMEO: 10170 /* 10171 * Pass these two options in order for third part 10172 * protocol usage. Here just return directly. 10173 */ 10174 return (0); 10175 case SO_ALLZONES: 10176 /* Pass option along to IP level for handling */ 10177 return (-EINVAL); 10178 case SO_ANON_MLP: 10179 /* Pass option along to IP level for handling */ 10180 return (-EINVAL); 10181 case SO_MAC_EXEMPT: 10182 /* Pass option along to IP level for handling */ 10183 return (-EINVAL); 10184 case SO_EXCLBIND: 10185 if (!checkonly) 10186 tcp->tcp_exclbind = onoff; 10187 break; 10188 default: 10189 *outlenp = 0; 10190 return (EINVAL); 10191 } 10192 break; 10193 case IPPROTO_TCP: 10194 switch (name) { 10195 case TCP_NODELAY: 10196 if (!checkonly) 10197 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10198 break; 10199 case TCP_NOTIFY_THRESHOLD: 10200 if (!checkonly) 10201 tcp->tcp_first_timer_threshold = *i1; 10202 break; 10203 case TCP_ABORT_THRESHOLD: 10204 if (!checkonly) 10205 tcp->tcp_second_timer_threshold = *i1; 10206 break; 10207 case TCP_CONN_NOTIFY_THRESHOLD: 10208 if (!checkonly) 10209 tcp->tcp_first_ctimer_threshold = *i1; 10210 break; 10211 case TCP_CONN_ABORT_THRESHOLD: 10212 if (!checkonly) 10213 tcp->tcp_second_ctimer_threshold = *i1; 10214 break; 10215 case TCP_RECVDSTADDR: 10216 if (tcp->tcp_state > TCPS_LISTEN) 10217 return (EOPNOTSUPP); 10218 if (!checkonly) 10219 tcp->tcp_recvdstaddr = onoff; 10220 break; 10221 case TCP_ANONPRIVBIND: 10222 if ((reterr = secpolicy_net_privaddr(cr, 0, 10223 IPPROTO_TCP)) != 0) { 10224 *outlenp = 0; 10225 return (reterr); 10226 } 10227 if (!checkonly) { 10228 tcp->tcp_anon_priv_bind = onoff; 10229 } 10230 break; 10231 case TCP_EXCLBIND: 10232 if (!checkonly) 10233 tcp->tcp_exclbind = onoff; 10234 break; /* goto sizeof (int) option return */ 10235 case TCP_INIT_CWND: { 10236 uint32_t init_cwnd = *((uint32_t *)invalp); 10237 10238 if (checkonly) 10239 break; 10240 10241 /* 10242 * Only allow socket with network configuration 10243 * privilege to set the initial cwnd to be larger 10244 * than allowed by RFC 3390. 10245 */ 10246 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10247 tcp->tcp_init_cwnd = init_cwnd; 10248 break; 10249 } 10250 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10251 *outlenp = 0; 10252 return (reterr); 10253 } 10254 if (init_cwnd > TCP_MAX_INIT_CWND) { 10255 *outlenp = 0; 10256 return (EINVAL); 10257 } 10258 tcp->tcp_init_cwnd = init_cwnd; 10259 break; 10260 } 10261 case TCP_KEEPALIVE_THRESHOLD: 10262 if (checkonly) 10263 break; 10264 10265 if (*i1 < tcps->tcps_keepalive_interval_low || 10266 *i1 > tcps->tcps_keepalive_interval_high) { 10267 *outlenp = 0; 10268 return (EINVAL); 10269 } 10270 if (*i1 != tcp->tcp_ka_interval) { 10271 tcp->tcp_ka_interval = *i1; 10272 /* 10273 * Check if we need to restart the 10274 * keepalive timer. 10275 */ 10276 if (tcp->tcp_ka_tid != 0) { 10277 ASSERT(tcp->tcp_ka_enabled); 10278 (void) TCP_TIMER_CANCEL(tcp, 10279 tcp->tcp_ka_tid); 10280 tcp->tcp_ka_last_intrvl = 0; 10281 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10282 tcp_keepalive_killer, 10283 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10284 } 10285 } 10286 break; 10287 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10288 if (!checkonly) { 10289 if (*i1 < 10290 tcps->tcps_keepalive_abort_interval_low || 10291 *i1 > 10292 tcps->tcps_keepalive_abort_interval_high) { 10293 *outlenp = 0; 10294 return (EINVAL); 10295 } 10296 tcp->tcp_ka_abort_thres = *i1; 10297 } 10298 break; 10299 case TCP_CORK: 10300 if (!checkonly) { 10301 /* 10302 * if tcp->tcp_cork was set and is now 10303 * being unset, we have to make sure that 10304 * the remaining data gets sent out. Also 10305 * unset tcp->tcp_cork so that tcp_wput_data() 10306 * can send data even if it is less than mss 10307 */ 10308 if (tcp->tcp_cork && onoff == 0 && 10309 tcp->tcp_unsent > 0) { 10310 tcp->tcp_cork = B_FALSE; 10311 tcp_wput_data(tcp, NULL, B_FALSE); 10312 } 10313 tcp->tcp_cork = onoff; 10314 } 10315 break; 10316 default: 10317 *outlenp = 0; 10318 return (EINVAL); 10319 } 10320 break; 10321 case IPPROTO_IP: 10322 if (tcp->tcp_family != AF_INET) { 10323 *outlenp = 0; 10324 return (ENOPROTOOPT); 10325 } 10326 switch (name) { 10327 case IP_OPTIONS: 10328 case T_IP_OPTIONS: 10329 reterr = tcp_opt_set_header(tcp, checkonly, 10330 invalp, inlen); 10331 if (reterr) { 10332 *outlenp = 0; 10333 return (reterr); 10334 } 10335 /* OK return - copy input buffer into output buffer */ 10336 if (invalp != outvalp) { 10337 /* don't trust bcopy for identical src/dst */ 10338 bcopy(invalp, outvalp, inlen); 10339 } 10340 *outlenp = inlen; 10341 return (0); 10342 case IP_TOS: 10343 case T_IP_TOS: 10344 if (!checkonly) { 10345 tcp->tcp_ipha->ipha_type_of_service = 10346 (uchar_t)*i1; 10347 tcp->tcp_tos = (uchar_t)*i1; 10348 } 10349 break; 10350 case IP_TTL: 10351 if (!checkonly) { 10352 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10353 tcp->tcp_ttl = (uchar_t)*i1; 10354 } 10355 break; 10356 case IP_BOUND_IF: 10357 case IP_NEXTHOP: 10358 /* Handled at the IP level */ 10359 return (-EINVAL); 10360 case IP_SEC_OPT: 10361 /* 10362 * We should not allow policy setting after 10363 * we start listening for connections. 10364 */ 10365 if (tcp->tcp_state == TCPS_LISTEN) { 10366 return (EINVAL); 10367 } else { 10368 /* Handled at the IP level */ 10369 return (-EINVAL); 10370 } 10371 default: 10372 *outlenp = 0; 10373 return (EINVAL); 10374 } 10375 break; 10376 case IPPROTO_IPV6: { 10377 ip6_pkt_t *ipp; 10378 10379 /* 10380 * IPPROTO_IPV6 options are only supported for sockets 10381 * that are using IPv6 on the wire. 10382 */ 10383 if (tcp->tcp_ipversion != IPV6_VERSION) { 10384 *outlenp = 0; 10385 return (ENOPROTOOPT); 10386 } 10387 /* 10388 * Only sticky options; no ancillary data 10389 */ 10390 ipp = &tcp->tcp_sticky_ipp; 10391 10392 switch (name) { 10393 case IPV6_UNICAST_HOPS: 10394 /* -1 means use default */ 10395 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10396 *outlenp = 0; 10397 return (EINVAL); 10398 } 10399 if (!checkonly) { 10400 if (*i1 == -1) { 10401 tcp->tcp_ip6h->ip6_hops = 10402 ipp->ipp_unicast_hops = 10403 (uint8_t)tcps->tcps_ipv6_hoplimit; 10404 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10405 /* Pass modified value to IP. */ 10406 *i1 = tcp->tcp_ip6h->ip6_hops; 10407 } else { 10408 tcp->tcp_ip6h->ip6_hops = 10409 ipp->ipp_unicast_hops = 10410 (uint8_t)*i1; 10411 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10412 } 10413 reterr = tcp_build_hdrs(tcp); 10414 if (reterr != 0) 10415 return (reterr); 10416 } 10417 break; 10418 case IPV6_BOUND_IF: 10419 if (!checkonly) { 10420 tcp->tcp_bound_if = *i1; 10421 PASS_OPT_TO_IP(connp); 10422 } 10423 break; 10424 /* 10425 * Set boolean switches for ancillary data delivery 10426 */ 10427 case IPV6_RECVPKTINFO: 10428 if (!checkonly) { 10429 if (onoff) 10430 tcp->tcp_ipv6_recvancillary |= 10431 TCP_IPV6_RECVPKTINFO; 10432 else 10433 tcp->tcp_ipv6_recvancillary &= 10434 ~TCP_IPV6_RECVPKTINFO; 10435 /* Force it to be sent up with the next msg */ 10436 tcp->tcp_recvifindex = 0; 10437 PASS_OPT_TO_IP(connp); 10438 } 10439 break; 10440 case IPV6_RECVTCLASS: 10441 if (!checkonly) { 10442 if (onoff) 10443 tcp->tcp_ipv6_recvancillary |= 10444 TCP_IPV6_RECVTCLASS; 10445 else 10446 tcp->tcp_ipv6_recvancillary &= 10447 ~TCP_IPV6_RECVTCLASS; 10448 PASS_OPT_TO_IP(connp); 10449 } 10450 break; 10451 case IPV6_RECVHOPLIMIT: 10452 if (!checkonly) { 10453 if (onoff) 10454 tcp->tcp_ipv6_recvancillary |= 10455 TCP_IPV6_RECVHOPLIMIT; 10456 else 10457 tcp->tcp_ipv6_recvancillary &= 10458 ~TCP_IPV6_RECVHOPLIMIT; 10459 /* Force it to be sent up with the next msg */ 10460 tcp->tcp_recvhops = 0xffffffffU; 10461 PASS_OPT_TO_IP(connp); 10462 } 10463 break; 10464 case IPV6_RECVHOPOPTS: 10465 if (!checkonly) { 10466 if (onoff) 10467 tcp->tcp_ipv6_recvancillary |= 10468 TCP_IPV6_RECVHOPOPTS; 10469 else 10470 tcp->tcp_ipv6_recvancillary &= 10471 ~TCP_IPV6_RECVHOPOPTS; 10472 PASS_OPT_TO_IP(connp); 10473 } 10474 break; 10475 case IPV6_RECVDSTOPTS: 10476 if (!checkonly) { 10477 if (onoff) 10478 tcp->tcp_ipv6_recvancillary |= 10479 TCP_IPV6_RECVDSTOPTS; 10480 else 10481 tcp->tcp_ipv6_recvancillary &= 10482 ~TCP_IPV6_RECVDSTOPTS; 10483 PASS_OPT_TO_IP(connp); 10484 } 10485 break; 10486 case _OLD_IPV6_RECVDSTOPTS: 10487 if (!checkonly) { 10488 if (onoff) 10489 tcp->tcp_ipv6_recvancillary |= 10490 TCP_OLD_IPV6_RECVDSTOPTS; 10491 else 10492 tcp->tcp_ipv6_recvancillary &= 10493 ~TCP_OLD_IPV6_RECVDSTOPTS; 10494 } 10495 break; 10496 case IPV6_RECVRTHDR: 10497 if (!checkonly) { 10498 if (onoff) 10499 tcp->tcp_ipv6_recvancillary |= 10500 TCP_IPV6_RECVRTHDR; 10501 else 10502 tcp->tcp_ipv6_recvancillary &= 10503 ~TCP_IPV6_RECVRTHDR; 10504 PASS_OPT_TO_IP(connp); 10505 } 10506 break; 10507 case IPV6_RECVRTHDRDSTOPTS: 10508 if (!checkonly) { 10509 if (onoff) 10510 tcp->tcp_ipv6_recvancillary |= 10511 TCP_IPV6_RECVRTDSTOPTS; 10512 else 10513 tcp->tcp_ipv6_recvancillary &= 10514 ~TCP_IPV6_RECVRTDSTOPTS; 10515 PASS_OPT_TO_IP(connp); 10516 } 10517 break; 10518 case IPV6_PKTINFO: 10519 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10520 return (EINVAL); 10521 if (checkonly) 10522 break; 10523 10524 if (inlen == 0) { 10525 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10526 } else { 10527 struct in6_pktinfo *pkti; 10528 10529 pkti = (struct in6_pktinfo *)invalp; 10530 /* 10531 * RFC 3542 states that ipi6_addr must be 10532 * the unspecified address when setting the 10533 * IPV6_PKTINFO sticky socket option on a 10534 * TCP socket. 10535 */ 10536 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10537 return (EINVAL); 10538 /* 10539 * IP will validate the source address and 10540 * interface index. 10541 */ 10542 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10543 reterr = ip_set_options(tcp->tcp_connp, 10544 level, name, invalp, inlen, cr); 10545 } else { 10546 reterr = ip6_set_pktinfo(cr, 10547 tcp->tcp_connp, pkti); 10548 } 10549 if (reterr != 0) 10550 return (reterr); 10551 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10552 ipp->ipp_addr = pkti->ipi6_addr; 10553 if (ipp->ipp_ifindex != 0) 10554 ipp->ipp_fields |= IPPF_IFINDEX; 10555 else 10556 ipp->ipp_fields &= ~IPPF_IFINDEX; 10557 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10558 ipp->ipp_fields |= IPPF_ADDR; 10559 else 10560 ipp->ipp_fields &= ~IPPF_ADDR; 10561 } 10562 reterr = tcp_build_hdrs(tcp); 10563 if (reterr != 0) 10564 return (reterr); 10565 break; 10566 case IPV6_TCLASS: 10567 if (inlen != 0 && inlen != sizeof (int)) 10568 return (EINVAL); 10569 if (checkonly) 10570 break; 10571 10572 if (inlen == 0) { 10573 ipp->ipp_fields &= ~IPPF_TCLASS; 10574 } else { 10575 if (*i1 > 255 || *i1 < -1) 10576 return (EINVAL); 10577 if (*i1 == -1) { 10578 ipp->ipp_tclass = 0; 10579 *i1 = 0; 10580 } else { 10581 ipp->ipp_tclass = *i1; 10582 } 10583 ipp->ipp_fields |= IPPF_TCLASS; 10584 } 10585 reterr = tcp_build_hdrs(tcp); 10586 if (reterr != 0) 10587 return (reterr); 10588 break; 10589 case IPV6_NEXTHOP: 10590 /* 10591 * IP will verify that the nexthop is reachable 10592 * and fail for sticky options. 10593 */ 10594 if (inlen != 0 && inlen != sizeof (sin6_t)) 10595 return (EINVAL); 10596 if (checkonly) 10597 break; 10598 10599 if (inlen == 0) { 10600 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10601 } else { 10602 sin6_t *sin6 = (sin6_t *)invalp; 10603 10604 if (sin6->sin6_family != AF_INET6) 10605 return (EAFNOSUPPORT); 10606 if (IN6_IS_ADDR_V4MAPPED( 10607 &sin6->sin6_addr)) 10608 return (EADDRNOTAVAIL); 10609 ipp->ipp_nexthop = sin6->sin6_addr; 10610 if (!IN6_IS_ADDR_UNSPECIFIED( 10611 &ipp->ipp_nexthop)) 10612 ipp->ipp_fields |= IPPF_NEXTHOP; 10613 else 10614 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10615 } 10616 reterr = tcp_build_hdrs(tcp); 10617 if (reterr != 0) 10618 return (reterr); 10619 PASS_OPT_TO_IP(connp); 10620 break; 10621 case IPV6_HOPOPTS: { 10622 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10623 10624 /* 10625 * Sanity checks - minimum size, size a multiple of 10626 * eight bytes, and matching size passed in. 10627 */ 10628 if (inlen != 0 && 10629 inlen != (8 * (hopts->ip6h_len + 1))) 10630 return (EINVAL); 10631 10632 if (checkonly) 10633 break; 10634 10635 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10636 (uchar_t **)&ipp->ipp_hopopts, 10637 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10638 if (reterr != 0) 10639 return (reterr); 10640 if (ipp->ipp_hopoptslen == 0) 10641 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10642 else 10643 ipp->ipp_fields |= IPPF_HOPOPTS; 10644 reterr = tcp_build_hdrs(tcp); 10645 if (reterr != 0) 10646 return (reterr); 10647 break; 10648 } 10649 case IPV6_RTHDRDSTOPTS: { 10650 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10651 10652 /* 10653 * Sanity checks - minimum size, size a multiple of 10654 * eight bytes, and matching size passed in. 10655 */ 10656 if (inlen != 0 && 10657 inlen != (8 * (dopts->ip6d_len + 1))) 10658 return (EINVAL); 10659 10660 if (checkonly) 10661 break; 10662 10663 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10664 (uchar_t **)&ipp->ipp_rtdstopts, 10665 &ipp->ipp_rtdstoptslen, 0); 10666 if (reterr != 0) 10667 return (reterr); 10668 if (ipp->ipp_rtdstoptslen == 0) 10669 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10670 else 10671 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10672 reterr = tcp_build_hdrs(tcp); 10673 if (reterr != 0) 10674 return (reterr); 10675 break; 10676 } 10677 case IPV6_DSTOPTS: { 10678 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10679 10680 /* 10681 * Sanity checks - minimum size, size a multiple of 10682 * eight bytes, and matching size passed in. 10683 */ 10684 if (inlen != 0 && 10685 inlen != (8 * (dopts->ip6d_len + 1))) 10686 return (EINVAL); 10687 10688 if (checkonly) 10689 break; 10690 10691 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10692 (uchar_t **)&ipp->ipp_dstopts, 10693 &ipp->ipp_dstoptslen, 0); 10694 if (reterr != 0) 10695 return (reterr); 10696 if (ipp->ipp_dstoptslen == 0) 10697 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10698 else 10699 ipp->ipp_fields |= IPPF_DSTOPTS; 10700 reterr = tcp_build_hdrs(tcp); 10701 if (reterr != 0) 10702 return (reterr); 10703 break; 10704 } 10705 case IPV6_RTHDR: { 10706 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10707 10708 /* 10709 * Sanity checks - minimum size, size a multiple of 10710 * eight bytes, and matching size passed in. 10711 */ 10712 if (inlen != 0 && 10713 inlen != (8 * (rt->ip6r_len + 1))) 10714 return (EINVAL); 10715 10716 if (checkonly) 10717 break; 10718 10719 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10720 (uchar_t **)&ipp->ipp_rthdr, 10721 &ipp->ipp_rthdrlen, 0); 10722 if (reterr != 0) 10723 return (reterr); 10724 if (ipp->ipp_rthdrlen == 0) 10725 ipp->ipp_fields &= ~IPPF_RTHDR; 10726 else 10727 ipp->ipp_fields |= IPPF_RTHDR; 10728 reterr = tcp_build_hdrs(tcp); 10729 if (reterr != 0) 10730 return (reterr); 10731 break; 10732 } 10733 case IPV6_V6ONLY: 10734 if (!checkonly) { 10735 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10736 } 10737 break; 10738 case IPV6_USE_MIN_MTU: 10739 if (inlen != sizeof (int)) 10740 return (EINVAL); 10741 10742 if (*i1 < -1 || *i1 > 1) 10743 return (EINVAL); 10744 10745 if (checkonly) 10746 break; 10747 10748 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10749 ipp->ipp_use_min_mtu = *i1; 10750 break; 10751 case IPV6_SEC_OPT: 10752 /* 10753 * We should not allow policy setting after 10754 * we start listening for connections. 10755 */ 10756 if (tcp->tcp_state == TCPS_LISTEN) { 10757 return (EINVAL); 10758 } else { 10759 /* Handled at the IP level */ 10760 return (-EINVAL); 10761 } 10762 case IPV6_SRC_PREFERENCES: 10763 if (inlen != sizeof (uint32_t)) 10764 return (EINVAL); 10765 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10766 *(uint32_t *)invalp); 10767 if (reterr != 0) { 10768 *outlenp = 0; 10769 return (reterr); 10770 } 10771 break; 10772 default: 10773 *outlenp = 0; 10774 return (EINVAL); 10775 } 10776 break; 10777 } /* end IPPROTO_IPV6 */ 10778 default: 10779 *outlenp = 0; 10780 return (EINVAL); 10781 } 10782 /* 10783 * Common case of OK return with outval same as inval 10784 */ 10785 if (invalp != outvalp) { 10786 /* don't trust bcopy for identical src/dst */ 10787 (void) bcopy(invalp, outvalp, inlen); 10788 } 10789 *outlenp = inlen; 10790 return (0); 10791 } 10792 10793 /* ARGSUSED */ 10794 int 10795 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10796 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10797 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10798 { 10799 conn_t *connp = Q_TO_CONN(q); 10800 10801 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10802 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10803 } 10804 10805 int 10806 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10807 const void *optvalp, socklen_t optlen, cred_t *cr) 10808 { 10809 conn_t *connp = (conn_t *)proto_handle; 10810 squeue_t *sqp = connp->conn_sqp; 10811 int error; 10812 10813 ASSERT(connp->conn_upper_handle != NULL); 10814 /* 10815 * Entering the squeue synchronously can result in a context switch, 10816 * which can cause a rather sever performance degradation. So we try to 10817 * handle whatever options we can without entering the squeue. 10818 */ 10819 if (level == IPPROTO_TCP) { 10820 switch (option_name) { 10821 case TCP_NODELAY: 10822 if (optlen != sizeof (int32_t)) 10823 return (EINVAL); 10824 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10825 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10826 connp->conn_tcp->tcp_mss; 10827 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10828 return (0); 10829 default: 10830 break; 10831 } 10832 } 10833 10834 error = squeue_synch_enter(sqp, connp, 0); 10835 if (error == ENOMEM) { 10836 return (ENOMEM); 10837 } 10838 10839 error = proto_opt_check(level, option_name, optlen, NULL, 10840 tcp_opt_obj.odb_opt_des_arr, 10841 tcp_opt_obj.odb_opt_arr_cnt, 10842 tcp_opt_obj.odb_topmost_tpiprovider, 10843 B_TRUE, B_FALSE, cr); 10844 10845 if (error != 0) { 10846 if (error < 0) { 10847 error = proto_tlitosyserr(-error); 10848 } 10849 squeue_synch_exit(sqp, connp); 10850 return (error); 10851 } 10852 10853 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10854 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10855 NULL, cr, NULL); 10856 squeue_synch_exit(sqp, connp); 10857 10858 if (error < 0) { 10859 /* 10860 * Pass on to ip 10861 */ 10862 error = ip_set_options(connp, level, option_name, optvalp, 10863 optlen, cr); 10864 } 10865 return (error); 10866 } 10867 10868 /* 10869 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10870 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10871 * headers, and the maximum size tcp header (to avoid reallocation 10872 * on the fly for additional tcp options). 10873 * Returns failure if can't allocate memory. 10874 */ 10875 static int 10876 tcp_build_hdrs(tcp_t *tcp) 10877 { 10878 char *hdrs; 10879 uint_t hdrs_len; 10880 ip6i_t *ip6i; 10881 char buf[TCP_MAX_HDR_LENGTH]; 10882 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10883 in6_addr_t src, dst; 10884 tcp_stack_t *tcps = tcp->tcp_tcps; 10885 conn_t *connp = tcp->tcp_connp; 10886 10887 /* 10888 * save the existing tcp header and source/dest IP addresses 10889 */ 10890 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10891 src = tcp->tcp_ip6h->ip6_src; 10892 dst = tcp->tcp_ip6h->ip6_dst; 10893 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10894 ASSERT(hdrs_len != 0); 10895 if (hdrs_len > tcp->tcp_iphc_len) { 10896 /* Need to reallocate */ 10897 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10898 if (hdrs == NULL) 10899 return (ENOMEM); 10900 if (tcp->tcp_iphc != NULL) { 10901 if (tcp->tcp_hdr_grown) { 10902 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10903 } else { 10904 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10905 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10906 } 10907 tcp->tcp_iphc_len = 0; 10908 } 10909 ASSERT(tcp->tcp_iphc_len == 0); 10910 tcp->tcp_iphc = hdrs; 10911 tcp->tcp_iphc_len = hdrs_len; 10912 tcp->tcp_hdr_grown = B_TRUE; 10913 } 10914 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10915 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10916 10917 /* Set header fields not in ipp */ 10918 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10919 ip6i = (ip6i_t *)tcp->tcp_iphc; 10920 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10921 } else { 10922 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10923 } 10924 /* 10925 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10926 * 10927 * tcp->tcp_tcp_hdr_len doesn't change here. 10928 */ 10929 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10930 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10931 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10932 10933 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10934 10935 tcp->tcp_ip6h->ip6_src = src; 10936 tcp->tcp_ip6h->ip6_dst = dst; 10937 10938 /* 10939 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10940 * the default value for TCP. 10941 */ 10942 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10943 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10944 10945 /* 10946 * If we're setting extension headers after a connection 10947 * has been established, and if we have a routing header 10948 * among the extension headers, call ip_massage_options_v6 to 10949 * manipulate the routing header/ip6_dst set the checksum 10950 * difference in the tcp header template. 10951 * (This happens in tcp_connect_ipv6 if the routing header 10952 * is set prior to the connect.) 10953 * Set the tcp_sum to zero first in case we've cleared a 10954 * routing header or don't have one at all. 10955 */ 10956 tcp->tcp_sum = 0; 10957 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10958 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10959 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10960 (uint8_t *)tcp->tcp_tcph); 10961 if (rth != NULL) { 10962 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10963 rth, tcps->tcps_netstack); 10964 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10965 (tcp->tcp_sum >> 16)); 10966 } 10967 } 10968 10969 /* Try to get everything in a single mblk */ 10970 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10971 hdrs_len + tcps->tcps_wroff_xtra); 10972 return (0); 10973 } 10974 10975 /* 10976 * Transfer any source route option from ipha to buf/dst in reversed form. 10977 */ 10978 static int 10979 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10980 { 10981 ipoptp_t opts; 10982 uchar_t *opt; 10983 uint8_t optval; 10984 uint8_t optlen; 10985 uint32_t len = 0; 10986 10987 for (optval = ipoptp_first(&opts, ipha); 10988 optval != IPOPT_EOL; 10989 optval = ipoptp_next(&opts)) { 10990 opt = opts.ipoptp_cur; 10991 optlen = opts.ipoptp_len; 10992 switch (optval) { 10993 int off1, off2; 10994 case IPOPT_SSRR: 10995 case IPOPT_LSRR: 10996 10997 /* Reverse source route */ 10998 /* 10999 * First entry should be the next to last one in the 11000 * current source route (the last entry is our 11001 * address.) 11002 * The last entry should be the final destination. 11003 */ 11004 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11005 buf[IPOPT_OLEN] = (uint8_t)optlen; 11006 off1 = IPOPT_MINOFF_SR - 1; 11007 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11008 if (off2 < 0) { 11009 /* No entries in source route */ 11010 break; 11011 } 11012 bcopy(opt + off2, dst, IP_ADDR_LEN); 11013 /* 11014 * Note: use src since ipha has not had its src 11015 * and dst reversed (it is in the state it was 11016 * received. 11017 */ 11018 bcopy(&ipha->ipha_src, buf + off2, 11019 IP_ADDR_LEN); 11020 off2 -= IP_ADDR_LEN; 11021 11022 while (off2 > 0) { 11023 bcopy(opt + off2, buf + off1, 11024 IP_ADDR_LEN); 11025 off1 += IP_ADDR_LEN; 11026 off2 -= IP_ADDR_LEN; 11027 } 11028 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11029 buf += optlen; 11030 len += optlen; 11031 break; 11032 } 11033 } 11034 done: 11035 /* Pad the resulting options */ 11036 while (len & 0x3) { 11037 *buf++ = IPOPT_EOL; 11038 len++; 11039 } 11040 return (len); 11041 } 11042 11043 11044 /* 11045 * Extract and revert a source route from ipha (if any) 11046 * and then update the relevant fields in both tcp_t and the standard header. 11047 */ 11048 static void 11049 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11050 { 11051 char buf[TCP_MAX_HDR_LENGTH]; 11052 uint_t tcph_len; 11053 int len; 11054 11055 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11056 len = IPH_HDR_LENGTH(ipha); 11057 if (len == IP_SIMPLE_HDR_LENGTH) 11058 /* Nothing to do */ 11059 return; 11060 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11061 (len & 0x3)) 11062 return; 11063 11064 tcph_len = tcp->tcp_tcp_hdr_len; 11065 bcopy(tcp->tcp_tcph, buf, tcph_len); 11066 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11067 (tcp->tcp_ipha->ipha_dst & 0xffff); 11068 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11069 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11070 len += IP_SIMPLE_HDR_LENGTH; 11071 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11072 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11073 if ((int)tcp->tcp_sum < 0) 11074 tcp->tcp_sum--; 11075 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11076 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11077 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11078 bcopy(buf, tcp->tcp_tcph, tcph_len); 11079 tcp->tcp_ip_hdr_len = len; 11080 tcp->tcp_ipha->ipha_version_and_hdr_length = 11081 (IP_VERSION << 4) | (len >> 2); 11082 len += tcph_len; 11083 tcp->tcp_hdr_len = len; 11084 } 11085 11086 /* 11087 * Copy the standard header into its new location, 11088 * lay in the new options and then update the relevant 11089 * fields in both tcp_t and the standard header. 11090 */ 11091 static int 11092 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11093 { 11094 uint_t tcph_len; 11095 uint8_t *ip_optp; 11096 tcph_t *new_tcph; 11097 tcp_stack_t *tcps = tcp->tcp_tcps; 11098 conn_t *connp = tcp->tcp_connp; 11099 11100 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11101 return (EINVAL); 11102 11103 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11104 return (EINVAL); 11105 11106 if (checkonly) { 11107 /* 11108 * do not really set, just pretend to - T_CHECK 11109 */ 11110 return (0); 11111 } 11112 11113 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11114 if (tcp->tcp_label_len > 0) { 11115 int padlen; 11116 uint8_t opt; 11117 11118 /* convert list termination to no-ops */ 11119 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11120 ip_optp += ip_optp[IPOPT_OLEN]; 11121 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11122 while (--padlen >= 0) 11123 *ip_optp++ = opt; 11124 } 11125 tcph_len = tcp->tcp_tcp_hdr_len; 11126 new_tcph = (tcph_t *)(ip_optp + len); 11127 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11128 tcp->tcp_tcph = new_tcph; 11129 bcopy(ptr, ip_optp, len); 11130 11131 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11132 11133 tcp->tcp_ip_hdr_len = len; 11134 tcp->tcp_ipha->ipha_version_and_hdr_length = 11135 (IP_VERSION << 4) | (len >> 2); 11136 tcp->tcp_hdr_len = len + tcph_len; 11137 if (!TCP_IS_DETACHED(tcp)) { 11138 /* Always allocate room for all options. */ 11139 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11140 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11141 } 11142 return (0); 11143 } 11144 11145 /* Get callback routine passed to nd_load by tcp_param_register */ 11146 /* ARGSUSED */ 11147 static int 11148 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11149 { 11150 tcpparam_t *tcppa = (tcpparam_t *)cp; 11151 11152 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11153 return (0); 11154 } 11155 11156 /* 11157 * Walk through the param array specified registering each element with the 11158 * named dispatch handler. 11159 */ 11160 static boolean_t 11161 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11162 { 11163 for (; cnt-- > 0; tcppa++) { 11164 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11165 if (!nd_load(ndp, tcppa->tcp_param_name, 11166 tcp_param_get, tcp_param_set, 11167 (caddr_t)tcppa)) { 11168 nd_free(ndp); 11169 return (B_FALSE); 11170 } 11171 } 11172 } 11173 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11174 KM_SLEEP); 11175 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11176 sizeof (tcpparam_t)); 11177 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11178 tcp_param_get, tcp_param_set_aligned, 11179 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11180 nd_free(ndp); 11181 return (B_FALSE); 11182 } 11183 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11184 KM_SLEEP); 11185 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11186 sizeof (tcpparam_t)); 11187 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11188 tcp_param_get, tcp_param_set_aligned, 11189 (caddr_t)tcps->tcps_mdt_head_param)) { 11190 nd_free(ndp); 11191 return (B_FALSE); 11192 } 11193 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11194 KM_SLEEP); 11195 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11196 sizeof (tcpparam_t)); 11197 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11198 tcp_param_get, tcp_param_set_aligned, 11199 (caddr_t)tcps->tcps_mdt_tail_param)) { 11200 nd_free(ndp); 11201 return (B_FALSE); 11202 } 11203 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11204 KM_SLEEP); 11205 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11206 sizeof (tcpparam_t)); 11207 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11208 tcp_param_get, tcp_param_set_aligned, 11209 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11210 nd_free(ndp); 11211 return (B_FALSE); 11212 } 11213 if (!nd_load(ndp, "tcp_extra_priv_ports", 11214 tcp_extra_priv_ports_get, NULL, NULL)) { 11215 nd_free(ndp); 11216 return (B_FALSE); 11217 } 11218 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11219 NULL, tcp_extra_priv_ports_add, NULL)) { 11220 nd_free(ndp); 11221 return (B_FALSE); 11222 } 11223 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11224 NULL, tcp_extra_priv_ports_del, NULL)) { 11225 nd_free(ndp); 11226 return (B_FALSE); 11227 } 11228 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11229 NULL)) { 11230 nd_free(ndp); 11231 return (B_FALSE); 11232 } 11233 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11234 NULL, NULL)) { 11235 nd_free(ndp); 11236 return (B_FALSE); 11237 } 11238 if (!nd_load(ndp, "tcp_listen_hash", 11239 tcp_listen_hash_report, NULL, NULL)) { 11240 nd_free(ndp); 11241 return (B_FALSE); 11242 } 11243 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11244 NULL, NULL)) { 11245 nd_free(ndp); 11246 return (B_FALSE); 11247 } 11248 if (!nd_load(ndp, "tcp_acceptor_hash", 11249 tcp_acceptor_hash_report, NULL, NULL)) { 11250 nd_free(ndp); 11251 return (B_FALSE); 11252 } 11253 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11254 tcp_1948_phrase_set, NULL)) { 11255 nd_free(ndp); 11256 return (B_FALSE); 11257 } 11258 /* 11259 * Dummy ndd variables - only to convey obsolescence information 11260 * through printing of their name (no get or set routines) 11261 * XXX Remove in future releases ? 11262 */ 11263 if (!nd_load(ndp, 11264 "tcp_close_wait_interval(obsoleted - " 11265 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11266 nd_free(ndp); 11267 return (B_FALSE); 11268 } 11269 return (B_TRUE); 11270 } 11271 11272 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11273 /* ARGSUSED */ 11274 static int 11275 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11276 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 /* 11287 * Need to make sure new_value is a multiple of 4. If it is not, 11288 * round it up. For future 64 bit requirement, we actually make it 11289 * a multiple of 8. 11290 */ 11291 if (new_value & 0x7) { 11292 new_value = (new_value & ~0x7) + 0x8; 11293 } 11294 tcppa->tcp_param_val = new_value; 11295 return (0); 11296 } 11297 11298 /* Set callback routine passed to nd_load by tcp_param_register */ 11299 /* ARGSUSED */ 11300 static int 11301 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11302 { 11303 long new_value; 11304 tcpparam_t *tcppa = (tcpparam_t *)cp; 11305 11306 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11307 new_value < tcppa->tcp_param_min || 11308 new_value > tcppa->tcp_param_max) { 11309 return (EINVAL); 11310 } 11311 tcppa->tcp_param_val = new_value; 11312 return (0); 11313 } 11314 11315 /* 11316 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11317 * is filled, return as much as we can. The message passed in may be 11318 * multi-part, chained using b_cont. "start" is the starting sequence 11319 * number for this piece. 11320 */ 11321 static mblk_t * 11322 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11323 { 11324 uint32_t end; 11325 mblk_t *mp1; 11326 mblk_t *mp2; 11327 mblk_t *next_mp; 11328 uint32_t u1; 11329 tcp_stack_t *tcps = tcp->tcp_tcps; 11330 11331 /* Walk through all the new pieces. */ 11332 do { 11333 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11334 (uintptr_t)INT_MAX); 11335 end = start + (int)(mp->b_wptr - mp->b_rptr); 11336 next_mp = mp->b_cont; 11337 if (start == end) { 11338 /* Empty. Blast it. */ 11339 freeb(mp); 11340 continue; 11341 } 11342 mp->b_cont = NULL; 11343 TCP_REASS_SET_SEQ(mp, start); 11344 TCP_REASS_SET_END(mp, end); 11345 mp1 = tcp->tcp_reass_tail; 11346 if (!mp1) { 11347 tcp->tcp_reass_tail = mp; 11348 tcp->tcp_reass_head = mp; 11349 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11350 UPDATE_MIB(&tcps->tcps_mib, 11351 tcpInDataUnorderBytes, end - start); 11352 continue; 11353 } 11354 /* New stuff completely beyond tail? */ 11355 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11356 /* Link it on end. */ 11357 mp1->b_cont = mp; 11358 tcp->tcp_reass_tail = mp; 11359 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11360 UPDATE_MIB(&tcps->tcps_mib, 11361 tcpInDataUnorderBytes, end - start); 11362 continue; 11363 } 11364 mp1 = tcp->tcp_reass_head; 11365 u1 = TCP_REASS_SEQ(mp1); 11366 /* New stuff at the front? */ 11367 if (SEQ_LT(start, u1)) { 11368 /* Yes... Check for overlap. */ 11369 mp->b_cont = mp1; 11370 tcp->tcp_reass_head = mp; 11371 tcp_reass_elim_overlap(tcp, mp); 11372 continue; 11373 } 11374 /* 11375 * The new piece fits somewhere between the head and tail. 11376 * We find our slot, where mp1 precedes us and mp2 trails. 11377 */ 11378 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11379 u1 = TCP_REASS_SEQ(mp2); 11380 if (SEQ_LEQ(start, u1)) 11381 break; 11382 } 11383 /* Link ourselves in */ 11384 mp->b_cont = mp2; 11385 mp1->b_cont = mp; 11386 11387 /* Trim overlap with following mblk(s) first */ 11388 tcp_reass_elim_overlap(tcp, mp); 11389 11390 /* Trim overlap with preceding mblk */ 11391 tcp_reass_elim_overlap(tcp, mp1); 11392 11393 } while (start = end, mp = next_mp); 11394 mp1 = tcp->tcp_reass_head; 11395 /* Anything ready to go? */ 11396 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11397 return (NULL); 11398 /* Eat what we can off the queue */ 11399 for (;;) { 11400 mp = mp1->b_cont; 11401 end = TCP_REASS_END(mp1); 11402 TCP_REASS_SET_SEQ(mp1, 0); 11403 TCP_REASS_SET_END(mp1, 0); 11404 if (!mp) { 11405 tcp->tcp_reass_tail = NULL; 11406 break; 11407 } 11408 if (end != TCP_REASS_SEQ(mp)) { 11409 mp1->b_cont = NULL; 11410 break; 11411 } 11412 mp1 = mp; 11413 } 11414 mp1 = tcp->tcp_reass_head; 11415 tcp->tcp_reass_head = mp; 11416 return (mp1); 11417 } 11418 11419 /* Eliminate any overlap that mp may have over later mblks */ 11420 static void 11421 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11422 { 11423 uint32_t end; 11424 mblk_t *mp1; 11425 uint32_t u1; 11426 tcp_stack_t *tcps = tcp->tcp_tcps; 11427 11428 end = TCP_REASS_END(mp); 11429 while ((mp1 = mp->b_cont) != NULL) { 11430 u1 = TCP_REASS_SEQ(mp1); 11431 if (!SEQ_GT(end, u1)) 11432 break; 11433 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11434 mp->b_wptr -= end - u1; 11435 TCP_REASS_SET_END(mp, u1); 11436 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11437 UPDATE_MIB(&tcps->tcps_mib, 11438 tcpInDataPartDupBytes, end - u1); 11439 break; 11440 } 11441 mp->b_cont = mp1->b_cont; 11442 TCP_REASS_SET_SEQ(mp1, 0); 11443 TCP_REASS_SET_END(mp1, 0); 11444 freeb(mp1); 11445 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11446 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11447 } 11448 if (!mp1) 11449 tcp->tcp_reass_tail = mp; 11450 } 11451 11452 static uint_t 11453 tcp_rwnd_reopen(tcp_t *tcp) 11454 { 11455 uint_t ret = 0; 11456 uint_t thwin; 11457 11458 /* Learn the latest rwnd information that we sent to the other side. */ 11459 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11460 << tcp->tcp_rcv_ws; 11461 /* This is peer's calculated send window (our receive window). */ 11462 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11463 /* 11464 * Increase the receive window to max. But we need to do receiver 11465 * SWS avoidance. This means that we need to check the increase of 11466 * of receive window is at least 1 MSS. 11467 */ 11468 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11469 /* 11470 * If the window that the other side knows is less than max 11471 * deferred acks segments, send an update immediately. 11472 */ 11473 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11474 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11475 ret = TH_ACK_NEEDED; 11476 } 11477 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11478 } 11479 return (ret); 11480 } 11481 11482 /* 11483 * Send up all messages queued on tcp_rcv_list. 11484 */ 11485 static uint_t 11486 tcp_rcv_drain(tcp_t *tcp) 11487 { 11488 mblk_t *mp; 11489 uint_t ret = 0; 11490 #ifdef DEBUG 11491 uint_t cnt = 0; 11492 #endif 11493 queue_t *q = tcp->tcp_rq; 11494 11495 /* Can't drain on an eager connection */ 11496 if (tcp->tcp_listener != NULL) 11497 return (ret); 11498 11499 /* Can't be a non-STREAMS connection or sodirect enabled */ 11500 ASSERT((!IPCL_IS_NONSTR(tcp->tcp_connp)) && SOD_NOT_ENABLED(tcp)); 11501 11502 /* No need for the push timer now. */ 11503 if (tcp->tcp_push_tid != 0) { 11504 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11505 tcp->tcp_push_tid = 0; 11506 } 11507 11508 /* 11509 * Handle two cases here: we are currently fused or we were 11510 * previously fused and have some urgent data to be delivered 11511 * upstream. The latter happens because we either ran out of 11512 * memory or were detached and therefore sending the SIGURG was 11513 * deferred until this point. In either case we pass control 11514 * over to tcp_fuse_rcv_drain() since it may need to complete 11515 * some work. 11516 */ 11517 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11518 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11519 tcp->tcp_fused_sigurg_mp != NULL); 11520 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11521 &tcp->tcp_fused_sigurg_mp)) 11522 return (ret); 11523 } 11524 11525 while ((mp = tcp->tcp_rcv_list) != NULL) { 11526 tcp->tcp_rcv_list = mp->b_next; 11527 mp->b_next = NULL; 11528 #ifdef DEBUG 11529 cnt += msgdsize(mp); 11530 #endif 11531 /* Does this need SSL processing first? */ 11532 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11533 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11534 mblk_t *, mp); 11535 tcp_kssl_input(tcp, mp); 11536 continue; 11537 } 11538 putnext(q, mp); 11539 } 11540 #ifdef DEBUG 11541 ASSERT(cnt == tcp->tcp_rcv_cnt); 11542 #endif 11543 tcp->tcp_rcv_last_head = NULL; 11544 tcp->tcp_rcv_last_tail = NULL; 11545 tcp->tcp_rcv_cnt = 0; 11546 11547 if (canputnext(q)) 11548 return (tcp_rwnd_reopen(tcp)); 11549 11550 return (ret); 11551 } 11552 11553 /* 11554 * Queue data on tcp_rcv_list which is a b_next chain. 11555 * tcp_rcv_last_head/tail is the last element of this chain. 11556 * Each element of the chain is a b_cont chain. 11557 * 11558 * M_DATA messages are added to the current element. 11559 * Other messages are added as new (b_next) elements. 11560 */ 11561 void 11562 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11563 { 11564 ASSERT(seg_len == msgdsize(mp)); 11565 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11566 11567 if (tcp->tcp_rcv_list == NULL) { 11568 ASSERT(tcp->tcp_rcv_last_head == NULL); 11569 tcp->tcp_rcv_list = mp; 11570 tcp->tcp_rcv_last_head = mp; 11571 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11572 tcp->tcp_rcv_last_tail->b_cont = mp; 11573 } else { 11574 tcp->tcp_rcv_last_head->b_next = mp; 11575 tcp->tcp_rcv_last_head = mp; 11576 } 11577 11578 while (mp->b_cont) 11579 mp = mp->b_cont; 11580 11581 tcp->tcp_rcv_last_tail = mp; 11582 tcp->tcp_rcv_cnt += seg_len; 11583 tcp->tcp_rwnd -= seg_len; 11584 } 11585 11586 /* 11587 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11588 * above, in addition when uioa is enabled schedule an asynchronous uio 11589 * prior to enqueuing. They implement the combinhed semantics of the 11590 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11591 * canputnext(), i.e. flow-control with backenable. 11592 * 11593 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11594 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11595 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11596 * 11597 * Must be called with sodp->sod_lockp held and will return with the lock 11598 * released. 11599 */ 11600 static uint_t 11601 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11602 { 11603 queue_t *q = tcp->tcp_rq; 11604 uint_t thwin; 11605 tcp_stack_t *tcps = tcp->tcp_tcps; 11606 uint_t ret = 0; 11607 11608 /* Can't be an eager connection */ 11609 ASSERT(tcp->tcp_listener == NULL); 11610 11611 /* Caller must have lock held */ 11612 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11613 11614 /* Sodirect mode so must not be a tcp_rcv_list */ 11615 ASSERT(tcp->tcp_rcv_list == NULL); 11616 11617 if (SOD_QFULL(sodp)) { 11618 /* Q is full, mark Q for need backenable */ 11619 SOD_QSETBE(sodp); 11620 } 11621 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11622 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11623 << tcp->tcp_rcv_ws; 11624 /* This is peer's calculated send window (our available rwnd). */ 11625 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11626 /* 11627 * Increase the receive window to max. But we need to do receiver 11628 * SWS avoidance. This means that we need to check the increase of 11629 * of receive window is at least 1 MSS. 11630 */ 11631 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11632 /* 11633 * If the window that the other side knows is less than max 11634 * deferred acks segments, send an update immediately. 11635 */ 11636 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11637 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11638 ret = TH_ACK_NEEDED; 11639 } 11640 tcp->tcp_rwnd = q->q_hiwat; 11641 } 11642 11643 if (!SOD_QEMPTY(sodp)) { 11644 /* Wakeup to socket */ 11645 sodp->sod_state &= SOD_WAKE_CLR; 11646 sodp->sod_state |= SOD_WAKE_DONE; 11647 (sodp->sod_wakeup)(sodp); 11648 /* wakeup() does the mutex_ext() */ 11649 } else { 11650 /* Q is empty, no need to wake */ 11651 sodp->sod_state &= SOD_WAKE_CLR; 11652 sodp->sod_state |= SOD_WAKE_NOT; 11653 mutex_exit(sodp->sod_lockp); 11654 } 11655 11656 /* No need for the push timer now. */ 11657 if (tcp->tcp_push_tid != 0) { 11658 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11659 tcp->tcp_push_tid = 0; 11660 } 11661 11662 return (ret); 11663 } 11664 11665 /* 11666 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11667 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11668 * to the user-land buffer and flag the mblk_t as such. 11669 * 11670 * Also, handle tcp_rwnd. 11671 */ 11672 uint_t 11673 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11674 { 11675 uioa_t *uioap = &sodp->sod_uioa; 11676 boolean_t qfull; 11677 uint_t thwin; 11678 11679 /* Can't be an eager connection */ 11680 ASSERT(tcp->tcp_listener == NULL); 11681 11682 /* Caller must have lock held */ 11683 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11684 11685 /* Sodirect mode so must not be a tcp_rcv_list */ 11686 ASSERT(tcp->tcp_rcv_list == NULL); 11687 11688 /* Passed in segment length must be equal to mblk_t chain data size */ 11689 ASSERT(seg_len == msgdsize(mp)); 11690 11691 if (DB_TYPE(mp) != M_DATA) { 11692 /* Only process M_DATA mblk_t's */ 11693 goto enq; 11694 } 11695 if (uioap->uioa_state & UIOA_ENABLED) { 11696 /* Uioa is enabled */ 11697 mblk_t *mp1 = mp; 11698 mblk_t *lmp = NULL; 11699 11700 if (seg_len > uioap->uio_resid) { 11701 /* 11702 * There isn't enough uio space for the mblk_t chain 11703 * so disable uioa such that this and any additional 11704 * mblk_t data is handled by the socket and schedule 11705 * the socket for wakeup to finish this uioa. 11706 */ 11707 uioap->uioa_state &= UIOA_CLR; 11708 uioap->uioa_state |= UIOA_FINI; 11709 if (sodp->sod_state & SOD_WAKE_NOT) { 11710 sodp->sod_state &= SOD_WAKE_CLR; 11711 sodp->sod_state |= SOD_WAKE_NEED; 11712 } 11713 goto enq; 11714 } 11715 do { 11716 uint32_t len = MBLKL(mp1); 11717 11718 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11719 /* Scheduled, mark dblk_t as such */ 11720 DB_FLAGS(mp1) |= DBLK_UIOA; 11721 } else { 11722 /* Error, turn off async processing */ 11723 uioap->uioa_state &= UIOA_CLR; 11724 uioap->uioa_state |= UIOA_FINI; 11725 break; 11726 } 11727 lmp = mp1; 11728 } while ((mp1 = mp1->b_cont) != NULL); 11729 11730 if (mp1 != NULL || uioap->uio_resid == 0) { 11731 /* 11732 * Not all mblk_t(s) uioamoved (error) or all uio 11733 * space has been consumed so schedule the socket 11734 * for wakeup to finish this uio. 11735 */ 11736 sodp->sod_state &= SOD_WAKE_CLR; 11737 sodp->sod_state |= SOD_WAKE_NEED; 11738 11739 /* Break the mblk chain if neccessary. */ 11740 if (mp1 != NULL && lmp != NULL) { 11741 mp->b_next = mp1; 11742 lmp->b_cont = NULL; 11743 } 11744 } 11745 } else if (uioap->uioa_state & UIOA_FINI) { 11746 /* 11747 * Post UIO_ENABLED waiting for socket to finish processing 11748 * so just enqueue and update tcp_rwnd. 11749 */ 11750 if (SOD_QFULL(sodp)) 11751 tcp->tcp_rwnd -= seg_len; 11752 } else if (sodp->sod_want > 0) { 11753 /* 11754 * Uioa isn't enabled but sodirect has a pending read(). 11755 */ 11756 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11757 if (sodp->sod_state & SOD_WAKE_NOT) { 11758 /* Schedule socket for wakeup */ 11759 sodp->sod_state &= SOD_WAKE_CLR; 11760 sodp->sod_state |= SOD_WAKE_NEED; 11761 } 11762 tcp->tcp_rwnd -= seg_len; 11763 } 11764 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11765 /* 11766 * No pending sodirect read() so used the default 11767 * TCP push logic to guess that a push is needed. 11768 */ 11769 if (sodp->sod_state & SOD_WAKE_NOT) { 11770 /* Schedule socket for wakeup */ 11771 sodp->sod_state &= SOD_WAKE_CLR; 11772 sodp->sod_state |= SOD_WAKE_NEED; 11773 } 11774 tcp->tcp_rwnd -= seg_len; 11775 } else { 11776 /* Just update tcp_rwnd */ 11777 tcp->tcp_rwnd -= seg_len; 11778 } 11779 enq: 11780 qfull = SOD_QFULL(sodp); 11781 11782 (sodp->sod_enqueue)(sodp, mp); 11783 11784 if (! qfull && SOD_QFULL(sodp)) { 11785 /* Wasn't QFULL, now QFULL, need back-enable */ 11786 SOD_QSETBE(sodp); 11787 } 11788 11789 /* 11790 * Check to see if remote avail swnd < mss due to delayed ACK, 11791 * first get advertised rwnd. 11792 */ 11793 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11794 /* Minus delayed ACK count */ 11795 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11796 if (thwin < tcp->tcp_mss) { 11797 /* Remote avail swnd < mss, need ACK now */ 11798 return (TH_ACK_NEEDED); 11799 } 11800 11801 return (0); 11802 } 11803 11804 /* 11805 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11806 * 11807 * This is the default entry function into TCP on the read side. TCP is 11808 * always entered via squeue i.e. using squeue's for mutual exclusion. 11809 * When classifier does a lookup to find the tcp, it also puts a reference 11810 * on the conn structure associated so the tcp is guaranteed to exist 11811 * when we come here. We still need to check the state because it might 11812 * as well has been closed. The squeue processing function i.e. squeue_enter, 11813 * is responsible for doing the CONN_DEC_REF. 11814 * 11815 * Apart from the default entry point, IP also sends packets directly to 11816 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11817 * connections. 11818 */ 11819 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11820 void 11821 tcp_input(void *arg, mblk_t *mp, void *arg2) 11822 { 11823 conn_t *connp = (conn_t *)arg; 11824 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11825 11826 /* arg2 is the sqp */ 11827 ASSERT(arg2 != NULL); 11828 ASSERT(mp != NULL); 11829 11830 /* 11831 * Don't accept any input on a closed tcp as this TCP logically does 11832 * not exist on the system. Don't proceed further with this TCP. 11833 * For eg. this packet could trigger another close of this tcp 11834 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11835 * tcp_clean_death / tcp_closei_local must be called at most once 11836 * on a TCP. In this case we need to refeed the packet into the 11837 * classifier and figure out where the packet should go. Need to 11838 * preserve the recv_ill somehow. Until we figure that out, for 11839 * now just drop the packet if we can't classify the packet. 11840 */ 11841 if (tcp->tcp_state == TCPS_CLOSED || 11842 tcp->tcp_state == TCPS_BOUND) { 11843 conn_t *new_connp; 11844 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11845 11846 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11847 if (new_connp != NULL) { 11848 tcp_reinput(new_connp, mp, arg2); 11849 return; 11850 } 11851 /* We failed to classify. For now just drop the packet */ 11852 freemsg(mp); 11853 return; 11854 } 11855 11856 if (DB_TYPE(mp) != M_DATA) { 11857 tcp_rput_common(tcp, mp); 11858 return; 11859 } 11860 11861 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11862 squeue_t *final_sqp; 11863 11864 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11865 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11866 DB_CKSUMSTART(mp) = 0; 11867 if (tcp->tcp_state == TCPS_SYN_SENT && 11868 connp->conn_final_sqp == NULL && 11869 tcp_outbound_squeue_switch) { 11870 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11871 connp->conn_final_sqp = final_sqp; 11872 if (connp->conn_final_sqp != connp->conn_sqp) { 11873 CONN_INC_REF(connp); 11874 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11875 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11876 tcp_rput_data, connp, ip_squeue_flag, 11877 SQTAG_CONNECT_FINISH); 11878 return; 11879 } 11880 } 11881 } 11882 tcp_rput_data(connp, mp, arg2); 11883 } 11884 11885 /* 11886 * The read side put procedure. 11887 * The packets passed up by ip are assume to be aligned according to 11888 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11889 */ 11890 static void 11891 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11892 { 11893 /* 11894 * tcp_rput_data() does not expect M_CTL except for the case 11895 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11896 * type. Need to make sure that any other M_CTLs don't make 11897 * it to tcp_rput_data since it is not expecting any and doesn't 11898 * check for it. 11899 */ 11900 if (DB_TYPE(mp) == M_CTL) { 11901 switch (*(uint32_t *)(mp->b_rptr)) { 11902 case TCP_IOC_ABORT_CONN: 11903 /* 11904 * Handle connection abort request. 11905 */ 11906 tcp_ioctl_abort_handler(tcp, mp); 11907 return; 11908 case IPSEC_IN: 11909 /* 11910 * Only secure icmp arrive in TCP and they 11911 * don't go through data path. 11912 */ 11913 tcp_icmp_error(tcp, mp); 11914 return; 11915 case IN_PKTINFO: 11916 /* 11917 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11918 * sockets that are receiving IPv4 traffic. tcp 11919 */ 11920 ASSERT(tcp->tcp_family == AF_INET6); 11921 ASSERT(tcp->tcp_ipv6_recvancillary & 11922 TCP_IPV6_RECVPKTINFO); 11923 tcp_rput_data(tcp->tcp_connp, mp, 11924 tcp->tcp_connp->conn_sqp); 11925 return; 11926 case MDT_IOC_INFO_UPDATE: 11927 /* 11928 * Handle Multidata information update; the 11929 * following routine will free the message. 11930 */ 11931 if (tcp->tcp_connp->conn_mdt_ok) { 11932 tcp_mdt_update(tcp, 11933 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11934 B_FALSE); 11935 } 11936 freemsg(mp); 11937 return; 11938 case LSO_IOC_INFO_UPDATE: 11939 /* 11940 * Handle LSO information update; the following 11941 * routine will free the message. 11942 */ 11943 if (tcp->tcp_connp->conn_lso_ok) { 11944 tcp_lso_update(tcp, 11945 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11946 } 11947 freemsg(mp); 11948 return; 11949 default: 11950 /* 11951 * tcp_icmp_err() will process the M_CTL packets. 11952 * Non-ICMP packets, if any, will be discarded in 11953 * tcp_icmp_err(). We will process the ICMP packet 11954 * even if we are TCP_IS_DETACHED_NONEAGER as the 11955 * incoming ICMP packet may result in changing 11956 * the tcp_mss, which we would need if we have 11957 * packets to retransmit. 11958 */ 11959 tcp_icmp_error(tcp, mp); 11960 return; 11961 } 11962 } 11963 11964 /* No point processing the message if tcp is already closed */ 11965 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11966 freemsg(mp); 11967 return; 11968 } 11969 11970 tcp_rput_other(tcp, mp); 11971 } 11972 11973 11974 /* The minimum of smoothed mean deviation in RTO calculation. */ 11975 #define TCP_SD_MIN 400 11976 11977 /* 11978 * Set RTO for this connection. The formula is from Jacobson and Karels' 11979 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11980 * are the same as those in Appendix A.2 of that paper. 11981 * 11982 * m = new measurement 11983 * sa = smoothed RTT average (8 * average estimates). 11984 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11985 */ 11986 static void 11987 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11988 { 11989 long m = TICK_TO_MSEC(rtt); 11990 clock_t sa = tcp->tcp_rtt_sa; 11991 clock_t sv = tcp->tcp_rtt_sd; 11992 clock_t rto; 11993 tcp_stack_t *tcps = tcp->tcp_tcps; 11994 11995 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11996 tcp->tcp_rtt_update++; 11997 11998 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11999 if (sa != 0) { 12000 /* 12001 * Update average estimator: 12002 * new rtt = 7/8 old rtt + 1/8 Error 12003 */ 12004 12005 /* m is now Error in estimate. */ 12006 m -= sa >> 3; 12007 if ((sa += m) <= 0) { 12008 /* 12009 * Don't allow the smoothed average to be negative. 12010 * We use 0 to denote reinitialization of the 12011 * variables. 12012 */ 12013 sa = 1; 12014 } 12015 12016 /* 12017 * Update deviation estimator: 12018 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12019 */ 12020 if (m < 0) 12021 m = -m; 12022 m -= sv >> 2; 12023 sv += m; 12024 } else { 12025 /* 12026 * This follows BSD's implementation. So the reinitialized 12027 * RTO is 3 * m. We cannot go less than 2 because if the 12028 * link is bandwidth dominated, doubling the window size 12029 * during slow start means doubling the RTT. We want to be 12030 * more conservative when we reinitialize our estimates. 3 12031 * is just a convenient number. 12032 */ 12033 sa = m << 3; 12034 sv = m << 1; 12035 } 12036 if (sv < TCP_SD_MIN) { 12037 /* 12038 * We do not know that if sa captures the delay ACK 12039 * effect as in a long train of segments, a receiver 12040 * does not delay its ACKs. So set the minimum of sv 12041 * to be TCP_SD_MIN, which is default to 400 ms, twice 12042 * of BSD DATO. That means the minimum of mean 12043 * deviation is 100 ms. 12044 * 12045 */ 12046 sv = TCP_SD_MIN; 12047 } 12048 tcp->tcp_rtt_sa = sa; 12049 tcp->tcp_rtt_sd = sv; 12050 /* 12051 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12052 * 12053 * Add tcp_rexmit_interval extra in case of extreme environment 12054 * where the algorithm fails to work. The default value of 12055 * tcp_rexmit_interval_extra should be 0. 12056 * 12057 * As we use a finer grained clock than BSD and update 12058 * RTO for every ACKs, add in another .25 of RTT to the 12059 * deviation of RTO to accomodate burstiness of 1/4 of 12060 * window size. 12061 */ 12062 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12063 12064 if (rto > tcps->tcps_rexmit_interval_max) { 12065 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12066 } else if (rto < tcps->tcps_rexmit_interval_min) { 12067 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12068 } else { 12069 tcp->tcp_rto = rto; 12070 } 12071 12072 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12073 tcp->tcp_timer_backoff = 0; 12074 } 12075 12076 /* 12077 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12078 * send queue which starts at the given seq. no. 12079 * 12080 * Parameters: 12081 * tcp_t *tcp: the tcp instance pointer. 12082 * uint32_t seq: the starting seq. no of the requested segment. 12083 * int32_t *off: after the execution, *off will be the offset to 12084 * the returned mblk which points to the requested seq no. 12085 * It is the caller's responsibility to send in a non-null off. 12086 * 12087 * Return: 12088 * A mblk_t pointer pointing to the requested segment in send queue. 12089 */ 12090 static mblk_t * 12091 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12092 { 12093 int32_t cnt; 12094 mblk_t *mp; 12095 12096 /* Defensive coding. Make sure we don't send incorrect data. */ 12097 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12098 return (NULL); 12099 12100 cnt = seq - tcp->tcp_suna; 12101 mp = tcp->tcp_xmit_head; 12102 while (cnt > 0 && mp != NULL) { 12103 cnt -= mp->b_wptr - mp->b_rptr; 12104 if (cnt < 0) { 12105 cnt += mp->b_wptr - mp->b_rptr; 12106 break; 12107 } 12108 mp = mp->b_cont; 12109 } 12110 ASSERT(mp != NULL); 12111 *off = cnt; 12112 return (mp); 12113 } 12114 12115 /* 12116 * This function handles all retransmissions if SACK is enabled for this 12117 * connection. First it calculates how many segments can be retransmitted 12118 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12119 * segments. A segment is eligible if sack_cnt for that segment is greater 12120 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12121 * all eligible segments, it checks to see if TCP can send some new segments 12122 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12123 * 12124 * Parameters: 12125 * tcp_t *tcp: the tcp structure of the connection. 12126 * uint_t *flags: in return, appropriate value will be set for 12127 * tcp_rput_data(). 12128 */ 12129 static void 12130 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12131 { 12132 notsack_blk_t *notsack_blk; 12133 int32_t usable_swnd; 12134 int32_t mss; 12135 uint32_t seg_len; 12136 mblk_t *xmit_mp; 12137 tcp_stack_t *tcps = tcp->tcp_tcps; 12138 12139 ASSERT(tcp->tcp_sack_info != NULL); 12140 ASSERT(tcp->tcp_notsack_list != NULL); 12141 ASSERT(tcp->tcp_rexmit == B_FALSE); 12142 12143 /* Defensive coding in case there is a bug... */ 12144 if (tcp->tcp_notsack_list == NULL) { 12145 return; 12146 } 12147 notsack_blk = tcp->tcp_notsack_list; 12148 mss = tcp->tcp_mss; 12149 12150 /* 12151 * Limit the num of outstanding data in the network to be 12152 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12153 */ 12154 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12155 12156 /* At least retransmit 1 MSS of data. */ 12157 if (usable_swnd <= 0) { 12158 usable_swnd = mss; 12159 } 12160 12161 /* Make sure no new RTT samples will be taken. */ 12162 tcp->tcp_csuna = tcp->tcp_snxt; 12163 12164 notsack_blk = tcp->tcp_notsack_list; 12165 while (usable_swnd > 0) { 12166 mblk_t *snxt_mp, *tmp_mp; 12167 tcp_seq begin = tcp->tcp_sack_snxt; 12168 tcp_seq end; 12169 int32_t off; 12170 12171 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12172 if (SEQ_GT(notsack_blk->end, begin) && 12173 (notsack_blk->sack_cnt >= 12174 tcps->tcps_dupack_fast_retransmit)) { 12175 end = notsack_blk->end; 12176 if (SEQ_LT(begin, notsack_blk->begin)) { 12177 begin = notsack_blk->begin; 12178 } 12179 break; 12180 } 12181 } 12182 /* 12183 * All holes are filled. Manipulate tcp_cwnd to send more 12184 * if we can. Note that after the SACK recovery, tcp_cwnd is 12185 * set to tcp_cwnd_ssthresh. 12186 */ 12187 if (notsack_blk == NULL) { 12188 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12189 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12190 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12191 ASSERT(tcp->tcp_cwnd > 0); 12192 return; 12193 } else { 12194 usable_swnd = usable_swnd / mss; 12195 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12196 MAX(usable_swnd * mss, mss); 12197 *flags |= TH_XMIT_NEEDED; 12198 return; 12199 } 12200 } 12201 12202 /* 12203 * Note that we may send more than usable_swnd allows here 12204 * because of round off, but no more than 1 MSS of data. 12205 */ 12206 seg_len = end - begin; 12207 if (seg_len > mss) 12208 seg_len = mss; 12209 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12210 ASSERT(snxt_mp != NULL); 12211 /* This should not happen. Defensive coding again... */ 12212 if (snxt_mp == NULL) { 12213 return; 12214 } 12215 12216 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12217 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12218 if (xmit_mp == NULL) 12219 return; 12220 12221 usable_swnd -= seg_len; 12222 tcp->tcp_pipe += seg_len; 12223 tcp->tcp_sack_snxt = begin + seg_len; 12224 12225 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12226 12227 /* 12228 * Update the send timestamp to avoid false retransmission. 12229 */ 12230 snxt_mp->b_prev = (mblk_t *)lbolt; 12231 12232 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12233 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12234 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12235 /* 12236 * Update tcp_rexmit_max to extend this SACK recovery phase. 12237 * This happens when new data sent during fast recovery is 12238 * also lost. If TCP retransmits those new data, it needs 12239 * to extend SACK recover phase to avoid starting another 12240 * fast retransmit/recovery unnecessarily. 12241 */ 12242 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12243 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12244 } 12245 } 12246 } 12247 12248 /* 12249 * This function handles policy checking at TCP level for non-hard_bound/ 12250 * detached connections. 12251 */ 12252 static boolean_t 12253 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12254 boolean_t secure, boolean_t mctl_present) 12255 { 12256 ipsec_latch_t *ipl = NULL; 12257 ipsec_action_t *act = NULL; 12258 mblk_t *data_mp; 12259 ipsec_in_t *ii; 12260 const char *reason; 12261 kstat_named_t *counter; 12262 tcp_stack_t *tcps = tcp->tcp_tcps; 12263 ipsec_stack_t *ipss; 12264 ip_stack_t *ipst; 12265 12266 ASSERT(mctl_present || !secure); 12267 12268 ASSERT((ipha == NULL && ip6h != NULL) || 12269 (ip6h == NULL && ipha != NULL)); 12270 12271 /* 12272 * We don't necessarily have an ipsec_in_act action to verify 12273 * policy because of assymetrical policy where we have only 12274 * outbound policy and no inbound policy (possible with global 12275 * policy). 12276 */ 12277 if (!secure) { 12278 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12279 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12280 return (B_TRUE); 12281 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12282 "tcp_check_policy", ipha, ip6h, secure, 12283 tcps->tcps_netstack); 12284 ipss = tcps->tcps_netstack->netstack_ipsec; 12285 12286 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12287 DROPPER(ipss, ipds_tcp_clear), 12288 &tcps->tcps_dropper); 12289 return (B_FALSE); 12290 } 12291 12292 /* 12293 * We have a secure packet. 12294 */ 12295 if (act == NULL) { 12296 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12297 "tcp_check_policy", ipha, ip6h, secure, 12298 tcps->tcps_netstack); 12299 ipss = tcps->tcps_netstack->netstack_ipsec; 12300 12301 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12302 DROPPER(ipss, ipds_tcp_secure), 12303 &tcps->tcps_dropper); 12304 return (B_FALSE); 12305 } 12306 12307 /* 12308 * XXX This whole routine is currently incorrect. ipl should 12309 * be set to the latch pointer, but is currently not set, so 12310 * we initialize it to NULL to avoid picking up random garbage. 12311 */ 12312 if (ipl == NULL) 12313 return (B_TRUE); 12314 12315 data_mp = first_mp->b_cont; 12316 12317 ii = (ipsec_in_t *)first_mp->b_rptr; 12318 12319 ipst = tcps->tcps_netstack->netstack_ip; 12320 12321 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12322 &counter, tcp->tcp_connp)) { 12323 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12324 return (B_TRUE); 12325 } 12326 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12327 "tcp inbound policy mismatch: %s, packet dropped\n", 12328 reason); 12329 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12330 12331 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12332 &tcps->tcps_dropper); 12333 return (B_FALSE); 12334 } 12335 12336 /* 12337 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12338 * retransmission after a timeout. 12339 * 12340 * To limit the number of duplicate segments, we limit the number of segment 12341 * to be sent in one time to tcp_snd_burst, the burst variable. 12342 */ 12343 static void 12344 tcp_ss_rexmit(tcp_t *tcp) 12345 { 12346 uint32_t snxt; 12347 uint32_t smax; 12348 int32_t win; 12349 int32_t mss; 12350 int32_t off; 12351 int32_t burst = tcp->tcp_snd_burst; 12352 mblk_t *snxt_mp; 12353 tcp_stack_t *tcps = tcp->tcp_tcps; 12354 12355 /* 12356 * Note that tcp_rexmit can be set even though TCP has retransmitted 12357 * all unack'ed segments. 12358 */ 12359 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12360 smax = tcp->tcp_rexmit_max; 12361 snxt = tcp->tcp_rexmit_nxt; 12362 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12363 snxt = tcp->tcp_suna; 12364 } 12365 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12366 win -= snxt - tcp->tcp_suna; 12367 mss = tcp->tcp_mss; 12368 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12369 12370 while (SEQ_LT(snxt, smax) && (win > 0) && 12371 (burst > 0) && (snxt_mp != NULL)) { 12372 mblk_t *xmit_mp; 12373 mblk_t *old_snxt_mp = snxt_mp; 12374 uint32_t cnt = mss; 12375 12376 if (win < cnt) { 12377 cnt = win; 12378 } 12379 if (SEQ_GT(snxt + cnt, smax)) { 12380 cnt = smax - snxt; 12381 } 12382 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12383 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12384 if (xmit_mp == NULL) 12385 return; 12386 12387 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12388 12389 snxt += cnt; 12390 win -= cnt; 12391 /* 12392 * Update the send timestamp to avoid false 12393 * retransmission. 12394 */ 12395 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12396 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12397 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12398 12399 tcp->tcp_rexmit_nxt = snxt; 12400 burst--; 12401 } 12402 /* 12403 * If we have transmitted all we have at the time 12404 * we started the retranmission, we can leave 12405 * the rest of the job to tcp_wput_data(). But we 12406 * need to check the send window first. If the 12407 * win is not 0, go on with tcp_wput_data(). 12408 */ 12409 if (SEQ_LT(snxt, smax) || win == 0) { 12410 return; 12411 } 12412 } 12413 /* Only call tcp_wput_data() if there is data to be sent. */ 12414 if (tcp->tcp_unsent) { 12415 tcp_wput_data(tcp, NULL, B_FALSE); 12416 } 12417 } 12418 12419 /* 12420 * Process all TCP option in SYN segment. Note that this function should 12421 * be called after tcp_adapt_ire() is called so that the necessary info 12422 * from IRE is already set in the tcp structure. 12423 * 12424 * This function sets up the correct tcp_mss value according to the 12425 * MSS option value and our header size. It also sets up the window scale 12426 * and timestamp values, and initialize SACK info blocks. But it does not 12427 * change receive window size after setting the tcp_mss value. The caller 12428 * should do the appropriate change. 12429 */ 12430 void 12431 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12432 { 12433 int options; 12434 tcp_opt_t tcpopt; 12435 uint32_t mss_max; 12436 char *tmp_tcph; 12437 tcp_stack_t *tcps = tcp->tcp_tcps; 12438 12439 tcpopt.tcp = NULL; 12440 options = tcp_parse_options(tcph, &tcpopt); 12441 12442 /* 12443 * Process MSS option. Note that MSS option value does not account 12444 * for IP or TCP options. This means that it is equal to MTU - minimum 12445 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12446 * IPv6. 12447 */ 12448 if (!(options & TCP_OPT_MSS_PRESENT)) { 12449 if (tcp->tcp_ipversion == IPV4_VERSION) 12450 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12451 else 12452 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12453 } else { 12454 if (tcp->tcp_ipversion == IPV4_VERSION) 12455 mss_max = tcps->tcps_mss_max_ipv4; 12456 else 12457 mss_max = tcps->tcps_mss_max_ipv6; 12458 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12459 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12460 else if (tcpopt.tcp_opt_mss > mss_max) 12461 tcpopt.tcp_opt_mss = mss_max; 12462 } 12463 12464 /* Process Window Scale option. */ 12465 if (options & TCP_OPT_WSCALE_PRESENT) { 12466 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12467 tcp->tcp_snd_ws_ok = B_TRUE; 12468 } else { 12469 tcp->tcp_snd_ws = B_FALSE; 12470 tcp->tcp_snd_ws_ok = B_FALSE; 12471 tcp->tcp_rcv_ws = B_FALSE; 12472 } 12473 12474 /* Process Timestamp option. */ 12475 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12476 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12477 tmp_tcph = (char *)tcp->tcp_tcph; 12478 12479 tcp->tcp_snd_ts_ok = B_TRUE; 12480 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12481 tcp->tcp_last_rcv_lbolt = lbolt64; 12482 ASSERT(OK_32PTR(tmp_tcph)); 12483 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12484 12485 /* Fill in our template header with basic timestamp option. */ 12486 tmp_tcph += tcp->tcp_tcp_hdr_len; 12487 tmp_tcph[0] = TCPOPT_NOP; 12488 tmp_tcph[1] = TCPOPT_NOP; 12489 tmp_tcph[2] = TCPOPT_TSTAMP; 12490 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12491 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12492 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12493 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12494 } else { 12495 tcp->tcp_snd_ts_ok = B_FALSE; 12496 } 12497 12498 /* 12499 * Process SACK options. If SACK is enabled for this connection, 12500 * then allocate the SACK info structure. Note the following ways 12501 * when tcp_snd_sack_ok is set to true. 12502 * 12503 * For active connection: in tcp_adapt_ire() called in 12504 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12505 * is checked. 12506 * 12507 * For passive connection: in tcp_adapt_ire() called in 12508 * tcp_accept_comm(). 12509 * 12510 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12511 * That check makes sure that if we did not send a SACK OK option, 12512 * we will not enable SACK for this connection even though the other 12513 * side sends us SACK OK option. For active connection, the SACK 12514 * info structure has already been allocated. So we need to free 12515 * it if SACK is disabled. 12516 */ 12517 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12518 (tcp->tcp_snd_sack_ok || 12519 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12520 /* This should be true only in the passive case. */ 12521 if (tcp->tcp_sack_info == NULL) { 12522 ASSERT(TCP_IS_DETACHED(tcp)); 12523 tcp->tcp_sack_info = 12524 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12525 } 12526 if (tcp->tcp_sack_info == NULL) { 12527 tcp->tcp_snd_sack_ok = B_FALSE; 12528 } else { 12529 tcp->tcp_snd_sack_ok = B_TRUE; 12530 if (tcp->tcp_snd_ts_ok) { 12531 tcp->tcp_max_sack_blk = 3; 12532 } else { 12533 tcp->tcp_max_sack_blk = 4; 12534 } 12535 } 12536 } else { 12537 /* 12538 * Resetting tcp_snd_sack_ok to B_FALSE so that 12539 * no SACK info will be used for this 12540 * connection. This assumes that SACK usage 12541 * permission is negotiated. This may need 12542 * to be changed once this is clarified. 12543 */ 12544 if (tcp->tcp_sack_info != NULL) { 12545 ASSERT(tcp->tcp_notsack_list == NULL); 12546 kmem_cache_free(tcp_sack_info_cache, 12547 tcp->tcp_sack_info); 12548 tcp->tcp_sack_info = NULL; 12549 } 12550 tcp->tcp_snd_sack_ok = B_FALSE; 12551 } 12552 12553 /* 12554 * Now we know the exact TCP/IP header length, subtract 12555 * that from tcp_mss to get our side's MSS. 12556 */ 12557 tcp->tcp_mss -= tcp->tcp_hdr_len; 12558 /* 12559 * Here we assume that the other side's header size will be equal to 12560 * our header size. We calculate the real MSS accordingly. Need to 12561 * take into additional stuffs IPsec puts in. 12562 * 12563 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12564 */ 12565 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12566 ((tcp->tcp_ipversion == IPV4_VERSION ? 12567 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12568 12569 /* 12570 * Set MSS to the smaller one of both ends of the connection. 12571 * We should not have called tcp_mss_set() before, but our 12572 * side of the MSS should have been set to a proper value 12573 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12574 * STREAM head parameters properly. 12575 * 12576 * If we have a larger-than-16-bit window but the other side 12577 * didn't want to do window scale, tcp_rwnd_set() will take 12578 * care of that. 12579 */ 12580 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12581 } 12582 12583 /* 12584 * Sends the T_CONN_IND to the listener. The caller calls this 12585 * functions via squeue to get inside the listener's perimeter 12586 * once the 3 way hand shake is done a T_CONN_IND needs to be 12587 * sent. As an optimization, the caller can call this directly 12588 * if listener's perimeter is same as eager's. 12589 */ 12590 /* ARGSUSED */ 12591 void 12592 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12593 { 12594 conn_t *lconnp = (conn_t *)arg; 12595 tcp_t *listener = lconnp->conn_tcp; 12596 tcp_t *tcp; 12597 struct T_conn_ind *conn_ind; 12598 ipaddr_t *addr_cache; 12599 boolean_t need_send_conn_ind = B_FALSE; 12600 tcp_stack_t *tcps = listener->tcp_tcps; 12601 12602 /* retrieve the eager */ 12603 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12604 ASSERT(conn_ind->OPT_offset != 0 && 12605 conn_ind->OPT_length == sizeof (intptr_t)); 12606 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12607 conn_ind->OPT_length); 12608 12609 /* 12610 * TLI/XTI applications will get confused by 12611 * sending eager as an option since it violates 12612 * the option semantics. So remove the eager as 12613 * option since TLI/XTI app doesn't need it anyway. 12614 */ 12615 if (!TCP_IS_SOCKET(listener)) { 12616 conn_ind->OPT_length = 0; 12617 conn_ind->OPT_offset = 0; 12618 } 12619 if (listener->tcp_state == TCPS_CLOSED || 12620 TCP_IS_DETACHED(listener)) { 12621 /* 12622 * If listener has closed, it would have caused a 12623 * a cleanup/blowoff to happen for the eager. We 12624 * just need to return. 12625 */ 12626 freemsg(mp); 12627 return; 12628 } 12629 12630 12631 /* 12632 * if the conn_req_q is full defer passing up the 12633 * T_CONN_IND until space is availabe after t_accept() 12634 * processing 12635 */ 12636 mutex_enter(&listener->tcp_eager_lock); 12637 12638 /* 12639 * Take the eager out, if it is in the list of droppable eagers 12640 * as we are here because the 3W handshake is over. 12641 */ 12642 MAKE_UNDROPPABLE(tcp); 12643 12644 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12645 tcp_t *tail; 12646 12647 /* 12648 * The eager already has an extra ref put in tcp_rput_data 12649 * so that it stays till accept comes back even though it 12650 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12651 */ 12652 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12653 listener->tcp_conn_req_cnt_q0--; 12654 listener->tcp_conn_req_cnt_q++; 12655 12656 /* Move from SYN_RCVD to ESTABLISHED list */ 12657 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12658 tcp->tcp_eager_prev_q0; 12659 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12660 tcp->tcp_eager_next_q0; 12661 tcp->tcp_eager_prev_q0 = NULL; 12662 tcp->tcp_eager_next_q0 = NULL; 12663 12664 /* 12665 * Insert at end of the queue because sockfs 12666 * sends down T_CONN_RES in chronological 12667 * order. Leaving the older conn indications 12668 * at front of the queue helps reducing search 12669 * time. 12670 */ 12671 tail = listener->tcp_eager_last_q; 12672 if (tail != NULL) 12673 tail->tcp_eager_next_q = tcp; 12674 else 12675 listener->tcp_eager_next_q = tcp; 12676 listener->tcp_eager_last_q = tcp; 12677 tcp->tcp_eager_next_q = NULL; 12678 /* 12679 * Delay sending up the T_conn_ind until we are 12680 * done with the eager. Once we have have sent up 12681 * the T_conn_ind, the accept can potentially complete 12682 * any time and release the refhold we have on the eager. 12683 */ 12684 need_send_conn_ind = B_TRUE; 12685 } else { 12686 /* 12687 * Defer connection on q0 and set deferred 12688 * connection bit true 12689 */ 12690 tcp->tcp_conn_def_q0 = B_TRUE; 12691 12692 /* take tcp out of q0 ... */ 12693 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12694 tcp->tcp_eager_next_q0; 12695 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12696 tcp->tcp_eager_prev_q0; 12697 12698 /* ... and place it at the end of q0 */ 12699 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12700 tcp->tcp_eager_next_q0 = listener; 12701 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12702 listener->tcp_eager_prev_q0 = tcp; 12703 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12704 } 12705 12706 /* we have timed out before */ 12707 if (tcp->tcp_syn_rcvd_timeout != 0) { 12708 tcp->tcp_syn_rcvd_timeout = 0; 12709 listener->tcp_syn_rcvd_timeout--; 12710 if (listener->tcp_syn_defense && 12711 listener->tcp_syn_rcvd_timeout <= 12712 (tcps->tcps_conn_req_max_q0 >> 5) && 12713 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12714 listener->tcp_last_rcv_lbolt)) { 12715 /* 12716 * Turn off the defense mode if we 12717 * believe the SYN attack is over. 12718 */ 12719 listener->tcp_syn_defense = B_FALSE; 12720 if (listener->tcp_ip_addr_cache) { 12721 kmem_free((void *)listener->tcp_ip_addr_cache, 12722 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12723 listener->tcp_ip_addr_cache = NULL; 12724 } 12725 } 12726 } 12727 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12728 if (addr_cache != NULL) { 12729 /* 12730 * We have finished a 3-way handshake with this 12731 * remote host. This proves the IP addr is good. 12732 * Cache it! 12733 */ 12734 addr_cache[IP_ADDR_CACHE_HASH( 12735 tcp->tcp_remote)] = tcp->tcp_remote; 12736 } 12737 mutex_exit(&listener->tcp_eager_lock); 12738 if (need_send_conn_ind) 12739 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12740 } 12741 12742 /* 12743 * Send the newconn notification to ulp. The eager is blown off if the 12744 * notification fails. 12745 */ 12746 static void 12747 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12748 { 12749 if (IPCL_IS_NONSTR(lconnp)) { 12750 cred_t *cr; 12751 pid_t cpid; 12752 12753 cr = msg_getcred(mp, &cpid); 12754 12755 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12756 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12757 lconnp->conn_tcp); 12758 12759 /* Keep the message around in case of a fallback to TPI */ 12760 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12761 12762 /* 12763 * Notify the ULP about the newconn. It is guaranteed that no 12764 * tcp_accept() call will be made for the eager if the 12765 * notification fails, so it's safe to blow it off in that 12766 * case. 12767 * 12768 * The upper handle will be assigned when tcp_accept() is 12769 * called. 12770 */ 12771 if ((*lconnp->conn_upcalls->su_newconn) 12772 (lconnp->conn_upper_handle, 12773 (sock_lower_handle_t)econnp, 12774 &sock_tcp_downcalls, cr, cpid, 12775 &econnp->conn_upcalls) == NULL) { 12776 /* Failed to allocate a socket */ 12777 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12778 tcpEstabResets); 12779 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12780 econnp->conn_tcp->tcp_conn_req_seqnum); 12781 } 12782 } else { 12783 putnext(lconnp->conn_tcp->tcp_rq, mp); 12784 } 12785 } 12786 12787 mblk_t * 12788 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12789 uint_t *ifindexp, ip6_pkt_t *ippp) 12790 { 12791 ip_pktinfo_t *pinfo; 12792 ip6_t *ip6h; 12793 uchar_t *rptr; 12794 mblk_t *first_mp = mp; 12795 boolean_t mctl_present = B_FALSE; 12796 uint_t ifindex = 0; 12797 ip6_pkt_t ipp; 12798 uint_t ipvers; 12799 uint_t ip_hdr_len; 12800 tcp_stack_t *tcps = tcp->tcp_tcps; 12801 12802 rptr = mp->b_rptr; 12803 ASSERT(OK_32PTR(rptr)); 12804 ASSERT(tcp != NULL); 12805 ipp.ipp_fields = 0; 12806 12807 switch DB_TYPE(mp) { 12808 case M_CTL: 12809 mp = mp->b_cont; 12810 if (mp == NULL) { 12811 freemsg(first_mp); 12812 return (NULL); 12813 } 12814 if (DB_TYPE(mp) != M_DATA) { 12815 freemsg(first_mp); 12816 return (NULL); 12817 } 12818 mctl_present = B_TRUE; 12819 break; 12820 case M_DATA: 12821 break; 12822 default: 12823 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12824 freemsg(mp); 12825 return (NULL); 12826 } 12827 ipvers = IPH_HDR_VERSION(rptr); 12828 if (ipvers == IPV4_VERSION) { 12829 if (tcp == NULL) { 12830 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12831 goto done; 12832 } 12833 12834 ipp.ipp_fields |= IPPF_HOPLIMIT; 12835 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12836 12837 /* 12838 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12839 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12840 */ 12841 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12842 mctl_present) { 12843 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12844 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12845 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12846 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12847 ipp.ipp_fields |= IPPF_IFINDEX; 12848 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12849 ifindex = pinfo->ip_pkt_ifindex; 12850 } 12851 freeb(first_mp); 12852 mctl_present = B_FALSE; 12853 } 12854 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12855 } else { 12856 ip6h = (ip6_t *)rptr; 12857 12858 ASSERT(ipvers == IPV6_VERSION); 12859 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12860 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12861 ipp.ipp_hoplimit = ip6h->ip6_hops; 12862 12863 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12864 uint8_t nexthdrp; 12865 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12866 12867 /* Look for ifindex information */ 12868 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12869 ip6i_t *ip6i = (ip6i_t *)ip6h; 12870 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12871 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12872 freemsg(first_mp); 12873 return (NULL); 12874 } 12875 12876 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12877 ASSERT(ip6i->ip6i_ifindex != 0); 12878 ipp.ipp_fields |= IPPF_IFINDEX; 12879 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12880 ifindex = ip6i->ip6i_ifindex; 12881 } 12882 rptr = (uchar_t *)&ip6i[1]; 12883 mp->b_rptr = rptr; 12884 if (rptr == mp->b_wptr) { 12885 mblk_t *mp1; 12886 mp1 = mp->b_cont; 12887 freeb(mp); 12888 mp = mp1; 12889 rptr = mp->b_rptr; 12890 } 12891 if (MBLKL(mp) < IPV6_HDR_LEN + 12892 sizeof (tcph_t)) { 12893 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12894 freemsg(first_mp); 12895 return (NULL); 12896 } 12897 ip6h = (ip6_t *)rptr; 12898 } 12899 12900 /* 12901 * Find any potentially interesting extension headers 12902 * as well as the length of the IPv6 + extension 12903 * headers. 12904 */ 12905 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12906 /* Verify if this is a TCP packet */ 12907 if (nexthdrp != IPPROTO_TCP) { 12908 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12909 freemsg(first_mp); 12910 return (NULL); 12911 } 12912 } else { 12913 ip_hdr_len = IPV6_HDR_LEN; 12914 } 12915 } 12916 12917 done: 12918 if (ipversp != NULL) 12919 *ipversp = ipvers; 12920 if (ip_hdr_lenp != NULL) 12921 *ip_hdr_lenp = ip_hdr_len; 12922 if (ippp != NULL) 12923 *ippp = ipp; 12924 if (ifindexp != NULL) 12925 *ifindexp = ifindex; 12926 if (mctl_present) { 12927 freeb(first_mp); 12928 } 12929 return (mp); 12930 } 12931 12932 /* 12933 * Handle M_DATA messages from IP. Its called directly from IP via 12934 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12935 * in this path. 12936 * 12937 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12938 * v4 and v6), we are called through tcp_input() and a M_CTL can 12939 * be present for options but tcp_find_pktinfo() deals with it. We 12940 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12941 * 12942 * The first argument is always the connp/tcp to which the mp belongs. 12943 * There are no exceptions to this rule. The caller has already put 12944 * a reference on this connp/tcp and once tcp_rput_data() returns, 12945 * the squeue will do the refrele. 12946 * 12947 * The TH_SYN for the listener directly go to tcp_conn_request via 12948 * squeue. 12949 * 12950 * sqp: NULL = recursive, sqp != NULL means called from squeue 12951 */ 12952 void 12953 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12954 { 12955 int32_t bytes_acked; 12956 int32_t gap; 12957 mblk_t *mp1; 12958 uint_t flags; 12959 uint32_t new_swnd = 0; 12960 uchar_t *iphdr; 12961 uchar_t *rptr; 12962 int32_t rgap; 12963 uint32_t seg_ack; 12964 int seg_len; 12965 uint_t ip_hdr_len; 12966 uint32_t seg_seq; 12967 tcph_t *tcph; 12968 int urp; 12969 tcp_opt_t tcpopt; 12970 uint_t ipvers; 12971 ip6_pkt_t ipp; 12972 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12973 uint32_t cwnd; 12974 uint32_t add; 12975 int npkt; 12976 int mss; 12977 conn_t *connp = (conn_t *)arg; 12978 squeue_t *sqp = (squeue_t *)arg2; 12979 tcp_t *tcp = connp->conn_tcp; 12980 tcp_stack_t *tcps = tcp->tcp_tcps; 12981 12982 /* 12983 * RST from fused tcp loopback peer should trigger an unfuse. 12984 */ 12985 if (tcp->tcp_fused) { 12986 TCP_STAT(tcps, tcp_fusion_aborted); 12987 tcp_unfuse(tcp); 12988 } 12989 12990 iphdr = mp->b_rptr; 12991 rptr = mp->b_rptr; 12992 ASSERT(OK_32PTR(rptr)); 12993 12994 /* 12995 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12996 * processing here. For rest call tcp_find_pktinfo to fill up the 12997 * necessary information. 12998 */ 12999 if (IPCL_IS_TCP4(connp)) { 13000 ipvers = IPV4_VERSION; 13001 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13002 } else { 13003 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13004 NULL, &ipp); 13005 if (mp == NULL) { 13006 TCP_STAT(tcps, tcp_rput_v6_error); 13007 return; 13008 } 13009 iphdr = mp->b_rptr; 13010 rptr = mp->b_rptr; 13011 } 13012 ASSERT(DB_TYPE(mp) == M_DATA); 13013 ASSERT(mp->b_next == NULL); 13014 13015 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13016 seg_seq = ABE32_TO_U32(tcph->th_seq); 13017 seg_ack = ABE32_TO_U32(tcph->th_ack); 13018 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13019 seg_len = (int)(mp->b_wptr - rptr) - 13020 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13021 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13022 do { 13023 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13024 (uintptr_t)INT_MAX); 13025 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13026 } while ((mp1 = mp1->b_cont) != NULL && 13027 mp1->b_datap->db_type == M_DATA); 13028 } 13029 13030 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13031 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13032 seg_len, tcph); 13033 return; 13034 } 13035 13036 if (sqp != NULL) { 13037 /* 13038 * This is the correct place to update tcp_last_recv_time. Note 13039 * that it is also updated for tcp structure that belongs to 13040 * global and listener queues which do not really need updating. 13041 * But that should not cause any harm. And it is updated for 13042 * all kinds of incoming segments, not only for data segments. 13043 */ 13044 tcp->tcp_last_recv_time = lbolt; 13045 } 13046 13047 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13048 13049 BUMP_LOCAL(tcp->tcp_ibsegs); 13050 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13051 13052 if ((flags & TH_URG) && sqp != NULL) { 13053 /* 13054 * TCP can't handle urgent pointers that arrive before 13055 * the connection has been accept()ed since it can't 13056 * buffer OOB data. Discard segment if this happens. 13057 * 13058 * We can't just rely on a non-null tcp_listener to indicate 13059 * that the accept() has completed since unlinking of the 13060 * eager and completion of the accept are not atomic. 13061 * tcp_detached, when it is not set (B_FALSE) indicates 13062 * that the accept() has completed. 13063 * 13064 * Nor can it reassemble urgent pointers, so discard 13065 * if it's not the next segment expected. 13066 * 13067 * Otherwise, collapse chain into one mblk (discard if 13068 * that fails). This makes sure the headers, retransmitted 13069 * data, and new data all are in the same mblk. 13070 */ 13071 ASSERT(mp != NULL); 13072 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13073 freemsg(mp); 13074 return; 13075 } 13076 /* Update pointers into message */ 13077 iphdr = rptr = mp->b_rptr; 13078 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13079 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13080 /* 13081 * Since we can't handle any data with this urgent 13082 * pointer that is out of sequence, we expunge 13083 * the data. This allows us to still register 13084 * the urgent mark and generate the M_PCSIG, 13085 * which we can do. 13086 */ 13087 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13088 seg_len = 0; 13089 } 13090 } 13091 13092 switch (tcp->tcp_state) { 13093 case TCPS_SYN_SENT: 13094 if (flags & TH_ACK) { 13095 /* 13096 * Note that our stack cannot send data before a 13097 * connection is established, therefore the 13098 * following check is valid. Otherwise, it has 13099 * to be changed. 13100 */ 13101 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13102 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13103 freemsg(mp); 13104 if (flags & TH_RST) 13105 return; 13106 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13107 tcp, seg_ack, 0, TH_RST); 13108 return; 13109 } 13110 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13111 } 13112 if (flags & TH_RST) { 13113 freemsg(mp); 13114 if (flags & TH_ACK) 13115 (void) tcp_clean_death(tcp, 13116 ECONNREFUSED, 13); 13117 return; 13118 } 13119 if (!(flags & TH_SYN)) { 13120 freemsg(mp); 13121 return; 13122 } 13123 13124 /* Process all TCP options. */ 13125 tcp_process_options(tcp, tcph); 13126 /* 13127 * The following changes our rwnd to be a multiple of the 13128 * MIN(peer MSS, our MSS) for performance reason. 13129 */ 13130 (void) tcp_rwnd_set(tcp, 13131 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 13132 13133 /* Is the other end ECN capable? */ 13134 if (tcp->tcp_ecn_ok) { 13135 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13136 tcp->tcp_ecn_ok = B_FALSE; 13137 } 13138 } 13139 /* 13140 * Clear ECN flags because it may interfere with later 13141 * processing. 13142 */ 13143 flags &= ~(TH_ECE|TH_CWR); 13144 13145 tcp->tcp_irs = seg_seq; 13146 tcp->tcp_rack = seg_seq; 13147 tcp->tcp_rnxt = seg_seq + 1; 13148 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13149 if (!TCP_IS_DETACHED(tcp)) { 13150 /* Allocate room for SACK options if needed. */ 13151 if (tcp->tcp_snd_sack_ok) { 13152 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13153 tcp->tcp_hdr_len + 13154 TCPOPT_MAX_SACK_LEN + 13155 (tcp->tcp_loopback ? 0 : 13156 tcps->tcps_wroff_xtra)); 13157 } else { 13158 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13159 tcp->tcp_hdr_len + 13160 (tcp->tcp_loopback ? 0 : 13161 tcps->tcps_wroff_xtra)); 13162 } 13163 } 13164 if (flags & TH_ACK) { 13165 /* 13166 * If we can't get the confirmation upstream, pretend 13167 * we didn't even see this one. 13168 * 13169 * XXX: how can we pretend we didn't see it if we 13170 * have updated rnxt et. al. 13171 * 13172 * For loopback we defer sending up the T_CONN_CON 13173 * until after some checks below. 13174 */ 13175 mp1 = NULL; 13176 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13177 tcp->tcp_loopback ? &mp1 : NULL)) { 13178 freemsg(mp); 13179 return; 13180 } 13181 /* SYN was acked - making progress */ 13182 if (tcp->tcp_ipversion == IPV6_VERSION) 13183 tcp->tcp_ip_forward_progress = B_TRUE; 13184 13185 /* One for the SYN */ 13186 tcp->tcp_suna = tcp->tcp_iss + 1; 13187 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13188 tcp->tcp_state = TCPS_ESTABLISHED; 13189 13190 /* 13191 * If SYN was retransmitted, need to reset all 13192 * retransmission info. This is because this 13193 * segment will be treated as a dup ACK. 13194 */ 13195 if (tcp->tcp_rexmit) { 13196 tcp->tcp_rexmit = B_FALSE; 13197 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13198 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13199 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13200 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13201 tcp->tcp_ms_we_have_waited = 0; 13202 13203 /* 13204 * Set tcp_cwnd back to 1 MSS, per 13205 * recommendation from 13206 * draft-floyd-incr-init-win-01.txt, 13207 * Increasing TCP's Initial Window. 13208 */ 13209 tcp->tcp_cwnd = tcp->tcp_mss; 13210 } 13211 13212 tcp->tcp_swl1 = seg_seq; 13213 tcp->tcp_swl2 = seg_ack; 13214 13215 new_swnd = BE16_TO_U16(tcph->th_win); 13216 tcp->tcp_swnd = new_swnd; 13217 if (new_swnd > tcp->tcp_max_swnd) 13218 tcp->tcp_max_swnd = new_swnd; 13219 13220 /* 13221 * Always send the three-way handshake ack immediately 13222 * in order to make the connection complete as soon as 13223 * possible on the accepting host. 13224 */ 13225 flags |= TH_ACK_NEEDED; 13226 13227 /* 13228 * Special case for loopback. At this point we have 13229 * received SYN-ACK from the remote endpoint. In 13230 * order to ensure that both endpoints reach the 13231 * fused state prior to any data exchange, the final 13232 * ACK needs to be sent before we indicate T_CONN_CON 13233 * to the module upstream. 13234 */ 13235 if (tcp->tcp_loopback) { 13236 mblk_t *ack_mp; 13237 13238 ASSERT(!tcp->tcp_unfusable); 13239 ASSERT(mp1 != NULL); 13240 /* 13241 * For loopback, we always get a pure SYN-ACK 13242 * and only need to send back the final ACK 13243 * with no data (this is because the other 13244 * tcp is ours and we don't do T/TCP). This 13245 * final ACK triggers the passive side to 13246 * perform fusion in ESTABLISHED state. 13247 */ 13248 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13249 if (tcp->tcp_ack_tid != 0) { 13250 (void) TCP_TIMER_CANCEL(tcp, 13251 tcp->tcp_ack_tid); 13252 tcp->tcp_ack_tid = 0; 13253 } 13254 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13255 BUMP_LOCAL(tcp->tcp_obsegs); 13256 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13257 13258 if (!IPCL_IS_NONSTR(connp)) { 13259 /* Send up T_CONN_CON */ 13260 putnext(tcp->tcp_rq, mp1); 13261 } else { 13262 cred_t *cr; 13263 pid_t cpid; 13264 13265 cr = msg_getcred(mp1, &cpid); 13266 (*connp->conn_upcalls-> 13267 su_connected) 13268 (connp->conn_upper_handle, 13269 tcp->tcp_connid, cr, cpid); 13270 freemsg(mp1); 13271 } 13272 13273 freemsg(mp); 13274 return; 13275 } 13276 /* 13277 * Forget fusion; we need to handle more 13278 * complex cases below. Send the deferred 13279 * T_CONN_CON message upstream and proceed 13280 * as usual. Mark this tcp as not capable 13281 * of fusion. 13282 */ 13283 TCP_STAT(tcps, tcp_fusion_unfusable); 13284 tcp->tcp_unfusable = B_TRUE; 13285 if (!IPCL_IS_NONSTR(connp)) { 13286 putnext(tcp->tcp_rq, mp1); 13287 } else { 13288 cred_t *cr; 13289 pid_t cpid; 13290 13291 cr = msg_getcred(mp1, &cpid); 13292 (*connp->conn_upcalls->su_connected) 13293 (connp->conn_upper_handle, 13294 tcp->tcp_connid, cr, cpid); 13295 freemsg(mp1); 13296 } 13297 } 13298 13299 /* 13300 * Check to see if there is data to be sent. If 13301 * yes, set the transmit flag. Then check to see 13302 * if received data processing needs to be done. 13303 * If not, go straight to xmit_check. This short 13304 * cut is OK as we don't support T/TCP. 13305 */ 13306 if (tcp->tcp_unsent) 13307 flags |= TH_XMIT_NEEDED; 13308 13309 if (seg_len == 0 && !(flags & TH_URG)) { 13310 freemsg(mp); 13311 goto xmit_check; 13312 } 13313 13314 flags &= ~TH_SYN; 13315 seg_seq++; 13316 break; 13317 } 13318 tcp->tcp_state = TCPS_SYN_RCVD; 13319 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13320 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13321 if (mp1) { 13322 /* 13323 * See comment in tcp_conn_request() for why we use 13324 * the open() time pid here. 13325 */ 13326 DB_CPID(mp1) = tcp->tcp_cpid; 13327 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13328 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13329 } 13330 freemsg(mp); 13331 return; 13332 case TCPS_SYN_RCVD: 13333 if (flags & TH_ACK) { 13334 /* 13335 * In this state, a SYN|ACK packet is either bogus 13336 * because the other side must be ACKing our SYN which 13337 * indicates it has seen the ACK for their SYN and 13338 * shouldn't retransmit it or we're crossing SYNs 13339 * on active open. 13340 */ 13341 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13342 freemsg(mp); 13343 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13344 tcp, seg_ack, 0, TH_RST); 13345 return; 13346 } 13347 /* 13348 * NOTE: RFC 793 pg. 72 says this should be 13349 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13350 * but that would mean we have an ack that ignored 13351 * our SYN. 13352 */ 13353 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13354 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13355 freemsg(mp); 13356 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13357 tcp, seg_ack, 0, TH_RST); 13358 return; 13359 } 13360 } 13361 break; 13362 case TCPS_LISTEN: 13363 /* 13364 * Only a TLI listener can come through this path when a 13365 * acceptor is going back to be a listener and a packet 13366 * for the acceptor hits the classifier. For a socket 13367 * listener, this can never happen because a listener 13368 * can never accept connection on itself and hence a 13369 * socket acceptor can not go back to being a listener. 13370 */ 13371 ASSERT(!TCP_IS_SOCKET(tcp)); 13372 /*FALLTHRU*/ 13373 case TCPS_CLOSED: 13374 case TCPS_BOUND: { 13375 conn_t *new_connp; 13376 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13377 13378 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13379 if (new_connp != NULL) { 13380 tcp_reinput(new_connp, mp, connp->conn_sqp); 13381 return; 13382 } 13383 /* We failed to classify. For now just drop the packet */ 13384 freemsg(mp); 13385 return; 13386 } 13387 case TCPS_IDLE: 13388 /* 13389 * Handle the case where the tcp_clean_death() has happened 13390 * on a connection (application hasn't closed yet) but a packet 13391 * was already queued on squeue before tcp_clean_death() 13392 * was processed. Calling tcp_clean_death() twice on same 13393 * connection can result in weird behaviour. 13394 */ 13395 freemsg(mp); 13396 return; 13397 default: 13398 break; 13399 } 13400 13401 /* 13402 * Already on the correct queue/perimeter. 13403 * If this is a detached connection and not an eager 13404 * connection hanging off a listener then new data 13405 * (past the FIN) will cause a reset. 13406 * We do a special check here where it 13407 * is out of the main line, rather than check 13408 * if we are detached every time we see new 13409 * data down below. 13410 */ 13411 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13412 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13413 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13414 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13415 13416 freemsg(mp); 13417 /* 13418 * This could be an SSL closure alert. We're detached so just 13419 * acknowledge it this last time. 13420 */ 13421 if (tcp->tcp_kssl_ctx != NULL) { 13422 kssl_release_ctx(tcp->tcp_kssl_ctx); 13423 tcp->tcp_kssl_ctx = NULL; 13424 13425 tcp->tcp_rnxt += seg_len; 13426 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13427 flags |= TH_ACK_NEEDED; 13428 goto ack_check; 13429 } 13430 13431 tcp_xmit_ctl("new data when detached", tcp, 13432 tcp->tcp_snxt, 0, TH_RST); 13433 (void) tcp_clean_death(tcp, EPROTO, 12); 13434 return; 13435 } 13436 13437 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13438 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13439 new_swnd = BE16_TO_U16(tcph->th_win) << 13440 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13441 13442 if (tcp->tcp_snd_ts_ok) { 13443 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13444 /* 13445 * This segment is not acceptable. 13446 * Drop it and send back an ACK. 13447 */ 13448 freemsg(mp); 13449 flags |= TH_ACK_NEEDED; 13450 goto ack_check; 13451 } 13452 } else if (tcp->tcp_snd_sack_ok) { 13453 ASSERT(tcp->tcp_sack_info != NULL); 13454 tcpopt.tcp = tcp; 13455 /* 13456 * SACK info in already updated in tcp_parse_options. Ignore 13457 * all other TCP options... 13458 */ 13459 (void) tcp_parse_options(tcph, &tcpopt); 13460 } 13461 try_again:; 13462 mss = tcp->tcp_mss; 13463 gap = seg_seq - tcp->tcp_rnxt; 13464 rgap = tcp->tcp_rwnd - (gap + seg_len); 13465 /* 13466 * gap is the amount of sequence space between what we expect to see 13467 * and what we got for seg_seq. A positive value for gap means 13468 * something got lost. A negative value means we got some old stuff. 13469 */ 13470 if (gap < 0) { 13471 /* Old stuff present. Is the SYN in there? */ 13472 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13473 (seg_len != 0)) { 13474 flags &= ~TH_SYN; 13475 seg_seq++; 13476 urp--; 13477 /* Recompute the gaps after noting the SYN. */ 13478 goto try_again; 13479 } 13480 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13481 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13482 (seg_len > -gap ? -gap : seg_len)); 13483 /* Remove the old stuff from seg_len. */ 13484 seg_len += gap; 13485 /* 13486 * Anything left? 13487 * Make sure to check for unack'd FIN when rest of data 13488 * has been previously ack'd. 13489 */ 13490 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13491 /* 13492 * Resets are only valid if they lie within our offered 13493 * window. If the RST bit is set, we just ignore this 13494 * segment. 13495 */ 13496 if (flags & TH_RST) { 13497 freemsg(mp); 13498 return; 13499 } 13500 13501 /* 13502 * The arriving of dup data packets indicate that we 13503 * may have postponed an ack for too long, or the other 13504 * side's RTT estimate is out of shape. Start acking 13505 * more often. 13506 */ 13507 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13508 tcp->tcp_rack_cnt >= 1 && 13509 tcp->tcp_rack_abs_max > 2) { 13510 tcp->tcp_rack_abs_max--; 13511 } 13512 tcp->tcp_rack_cur_max = 1; 13513 13514 /* 13515 * This segment is "unacceptable". None of its 13516 * sequence space lies within our advertized window. 13517 * 13518 * Adjust seg_len to the original value for tracing. 13519 */ 13520 seg_len -= gap; 13521 if (tcp->tcp_debug) { 13522 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13523 "tcp_rput: unacceptable, gap %d, rgap %d, " 13524 "flags 0x%x, seg_seq %u, seg_ack %u, " 13525 "seg_len %d, rnxt %u, snxt %u, %s", 13526 gap, rgap, flags, seg_seq, seg_ack, 13527 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13528 tcp_display(tcp, NULL, 13529 DISP_ADDR_AND_PORT)); 13530 } 13531 13532 /* 13533 * Arrange to send an ACK in response to the 13534 * unacceptable segment per RFC 793 page 69. There 13535 * is only one small difference between ours and the 13536 * acceptability test in the RFC - we accept ACK-only 13537 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13538 * will be generated. 13539 * 13540 * Note that we have to ACK an ACK-only packet at least 13541 * for stacks that send 0-length keep-alives with 13542 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13543 * section 4.2.3.6. As long as we don't ever generate 13544 * an unacceptable packet in response to an incoming 13545 * packet that is unacceptable, it should not cause 13546 * "ACK wars". 13547 */ 13548 flags |= TH_ACK_NEEDED; 13549 13550 /* 13551 * Continue processing this segment in order to use the 13552 * ACK information it contains, but skip all other 13553 * sequence-number processing. Processing the ACK 13554 * information is necessary in order to 13555 * re-synchronize connections that may have lost 13556 * synchronization. 13557 * 13558 * We clear seg_len and flag fields related to 13559 * sequence number processing as they are not 13560 * to be trusted for an unacceptable segment. 13561 */ 13562 seg_len = 0; 13563 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13564 goto process_ack; 13565 } 13566 13567 /* Fix seg_seq, and chew the gap off the front. */ 13568 seg_seq = tcp->tcp_rnxt; 13569 urp += gap; 13570 do { 13571 mblk_t *mp2; 13572 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13573 (uintptr_t)UINT_MAX); 13574 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13575 if (gap > 0) { 13576 mp->b_rptr = mp->b_wptr - gap; 13577 break; 13578 } 13579 mp2 = mp; 13580 mp = mp->b_cont; 13581 freeb(mp2); 13582 } while (gap < 0); 13583 /* 13584 * If the urgent data has already been acknowledged, we 13585 * should ignore TH_URG below 13586 */ 13587 if (urp < 0) 13588 flags &= ~TH_URG; 13589 } 13590 /* 13591 * rgap is the amount of stuff received out of window. A negative 13592 * value is the amount out of window. 13593 */ 13594 if (rgap < 0) { 13595 mblk_t *mp2; 13596 13597 if (tcp->tcp_rwnd == 0) { 13598 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13599 } else { 13600 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13601 UPDATE_MIB(&tcps->tcps_mib, 13602 tcpInDataPastWinBytes, -rgap); 13603 } 13604 13605 /* 13606 * seg_len does not include the FIN, so if more than 13607 * just the FIN is out of window, we act like we don't 13608 * see it. (If just the FIN is out of window, rgap 13609 * will be zero and we will go ahead and acknowledge 13610 * the FIN.) 13611 */ 13612 flags &= ~TH_FIN; 13613 13614 /* Fix seg_len and make sure there is something left. */ 13615 seg_len += rgap; 13616 if (seg_len <= 0) { 13617 /* 13618 * Resets are only valid if they lie within our offered 13619 * window. If the RST bit is set, we just ignore this 13620 * segment. 13621 */ 13622 if (flags & TH_RST) { 13623 freemsg(mp); 13624 return; 13625 } 13626 13627 /* Per RFC 793, we need to send back an ACK. */ 13628 flags |= TH_ACK_NEEDED; 13629 13630 /* 13631 * Send SIGURG as soon as possible i.e. even 13632 * if the TH_URG was delivered in a window probe 13633 * packet (which will be unacceptable). 13634 * 13635 * We generate a signal if none has been generated 13636 * for this connection or if this is a new urgent 13637 * byte. Also send a zero-length "unmarked" message 13638 * to inform SIOCATMARK that this is not the mark. 13639 * 13640 * tcp_urp_last_valid is cleared when the T_exdata_ind 13641 * is sent up. This plus the check for old data 13642 * (gap >= 0) handles the wraparound of the sequence 13643 * number space without having to always track the 13644 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13645 * this max in its rcv_up variable). 13646 * 13647 * This prevents duplicate SIGURGS due to a "late" 13648 * zero-window probe when the T_EXDATA_IND has already 13649 * been sent up. 13650 */ 13651 if ((flags & TH_URG) && 13652 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13653 tcp->tcp_urp_last))) { 13654 if (IPCL_IS_NONSTR(connp)) { 13655 if (!TCP_IS_DETACHED(tcp)) { 13656 (*connp->conn_upcalls-> 13657 su_signal_oob) 13658 (connp->conn_upper_handle, 13659 urp); 13660 } 13661 } else { 13662 mp1 = allocb(0, BPRI_MED); 13663 if (mp1 == NULL) { 13664 freemsg(mp); 13665 return; 13666 } 13667 if (!TCP_IS_DETACHED(tcp) && 13668 !putnextctl1(tcp->tcp_rq, 13669 M_PCSIG, SIGURG)) { 13670 /* Try again on the rexmit. */ 13671 freemsg(mp1); 13672 freemsg(mp); 13673 return; 13674 } 13675 /* 13676 * If the next byte would be the mark 13677 * then mark with MARKNEXT else mark 13678 * with NOTMARKNEXT. 13679 */ 13680 if (gap == 0 && urp == 0) 13681 mp1->b_flag |= MSGMARKNEXT; 13682 else 13683 mp1->b_flag |= MSGNOTMARKNEXT; 13684 freemsg(tcp->tcp_urp_mark_mp); 13685 tcp->tcp_urp_mark_mp = mp1; 13686 flags |= TH_SEND_URP_MARK; 13687 } 13688 tcp->tcp_urp_last_valid = B_TRUE; 13689 tcp->tcp_urp_last = urp + seg_seq; 13690 } 13691 /* 13692 * If this is a zero window probe, continue to 13693 * process the ACK part. But we need to set seg_len 13694 * to 0 to avoid data processing. Otherwise just 13695 * drop the segment and send back an ACK. 13696 */ 13697 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13698 flags &= ~(TH_SYN | TH_URG); 13699 seg_len = 0; 13700 goto process_ack; 13701 } else { 13702 freemsg(mp); 13703 goto ack_check; 13704 } 13705 } 13706 /* Pitch out of window stuff off the end. */ 13707 rgap = seg_len; 13708 mp2 = mp; 13709 do { 13710 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13711 (uintptr_t)INT_MAX); 13712 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13713 if (rgap < 0) { 13714 mp2->b_wptr += rgap; 13715 if ((mp1 = mp2->b_cont) != NULL) { 13716 mp2->b_cont = NULL; 13717 freemsg(mp1); 13718 } 13719 break; 13720 } 13721 } while ((mp2 = mp2->b_cont) != NULL); 13722 } 13723 ok:; 13724 /* 13725 * TCP should check ECN info for segments inside the window only. 13726 * Therefore the check should be done here. 13727 */ 13728 if (tcp->tcp_ecn_ok) { 13729 if (flags & TH_CWR) { 13730 tcp->tcp_ecn_echo_on = B_FALSE; 13731 } 13732 /* 13733 * Note that both ECN_CE and CWR can be set in the 13734 * same segment. In this case, we once again turn 13735 * on ECN_ECHO. 13736 */ 13737 if (tcp->tcp_ipversion == IPV4_VERSION) { 13738 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13739 13740 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13741 tcp->tcp_ecn_echo_on = B_TRUE; 13742 } 13743 } else { 13744 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13745 13746 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13747 htonl(IPH_ECN_CE << 20)) { 13748 tcp->tcp_ecn_echo_on = B_TRUE; 13749 } 13750 } 13751 } 13752 13753 /* 13754 * Check whether we can update tcp_ts_recent. This test is 13755 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13756 * Extensions for High Performance: An Update", Internet Draft. 13757 */ 13758 if (tcp->tcp_snd_ts_ok && 13759 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13760 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13761 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13762 tcp->tcp_last_rcv_lbolt = lbolt64; 13763 } 13764 13765 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13766 /* 13767 * FIN in an out of order segment. We record this in 13768 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13769 * Clear the FIN so that any check on FIN flag will fail. 13770 * Remember that FIN also counts in the sequence number 13771 * space. So we need to ack out of order FIN only segments. 13772 */ 13773 if (flags & TH_FIN) { 13774 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13775 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13776 flags &= ~TH_FIN; 13777 flags |= TH_ACK_NEEDED; 13778 } 13779 if (seg_len > 0) { 13780 /* Fill in the SACK blk list. */ 13781 if (tcp->tcp_snd_sack_ok) { 13782 ASSERT(tcp->tcp_sack_info != NULL); 13783 tcp_sack_insert(tcp->tcp_sack_list, 13784 seg_seq, seg_seq + seg_len, 13785 &(tcp->tcp_num_sack_blk)); 13786 } 13787 13788 /* 13789 * Attempt reassembly and see if we have something 13790 * ready to go. 13791 */ 13792 mp = tcp_reass(tcp, mp, seg_seq); 13793 /* Always ack out of order packets */ 13794 flags |= TH_ACK_NEEDED | TH_PUSH; 13795 if (mp) { 13796 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13797 (uintptr_t)INT_MAX); 13798 seg_len = mp->b_cont ? msgdsize(mp) : 13799 (int)(mp->b_wptr - mp->b_rptr); 13800 seg_seq = tcp->tcp_rnxt; 13801 /* 13802 * A gap is filled and the seq num and len 13803 * of the gap match that of a previously 13804 * received FIN, put the FIN flag back in. 13805 */ 13806 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13807 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13808 flags |= TH_FIN; 13809 tcp->tcp_valid_bits &= 13810 ~TCP_OFO_FIN_VALID; 13811 } 13812 } else { 13813 /* 13814 * Keep going even with NULL mp. 13815 * There may be a useful ACK or something else 13816 * we don't want to miss. 13817 * 13818 * But TCP should not perform fast retransmit 13819 * because of the ack number. TCP uses 13820 * seg_len == 0 to determine if it is a pure 13821 * ACK. And this is not a pure ACK. 13822 */ 13823 seg_len = 0; 13824 ofo_seg = B_TRUE; 13825 } 13826 } 13827 } else if (seg_len > 0) { 13828 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13829 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13830 /* 13831 * If an out of order FIN was received before, and the seq 13832 * num and len of the new segment match that of the FIN, 13833 * put the FIN flag back in. 13834 */ 13835 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13836 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13837 flags |= TH_FIN; 13838 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13839 } 13840 } 13841 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13842 if (flags & TH_RST) { 13843 freemsg(mp); 13844 switch (tcp->tcp_state) { 13845 case TCPS_SYN_RCVD: 13846 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13847 break; 13848 case TCPS_ESTABLISHED: 13849 case TCPS_FIN_WAIT_1: 13850 case TCPS_FIN_WAIT_2: 13851 case TCPS_CLOSE_WAIT: 13852 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13853 break; 13854 case TCPS_CLOSING: 13855 case TCPS_LAST_ACK: 13856 (void) tcp_clean_death(tcp, 0, 16); 13857 break; 13858 default: 13859 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13860 (void) tcp_clean_death(tcp, ENXIO, 17); 13861 break; 13862 } 13863 return; 13864 } 13865 if (flags & TH_SYN) { 13866 /* 13867 * See RFC 793, Page 71 13868 * 13869 * The seq number must be in the window as it should 13870 * be "fixed" above. If it is outside window, it should 13871 * be already rejected. Note that we allow seg_seq to be 13872 * rnxt + rwnd because we want to accept 0 window probe. 13873 */ 13874 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13875 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13876 freemsg(mp); 13877 /* 13878 * If the ACK flag is not set, just use our snxt as the 13879 * seq number of the RST segment. 13880 */ 13881 if (!(flags & TH_ACK)) { 13882 seg_ack = tcp->tcp_snxt; 13883 } 13884 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13885 TH_RST|TH_ACK); 13886 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13887 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13888 return; 13889 } 13890 /* 13891 * urp could be -1 when the urp field in the packet is 0 13892 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13893 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13894 */ 13895 if (flags & TH_URG && urp >= 0) { 13896 if (!tcp->tcp_urp_last_valid || 13897 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13898 if (IPCL_IS_NONSTR(connp)) { 13899 if (!TCP_IS_DETACHED(tcp)) { 13900 (*connp->conn_upcalls->su_signal_oob) 13901 (connp->conn_upper_handle, urp); 13902 } 13903 } else { 13904 /* 13905 * If we haven't generated the signal yet for 13906 * this urgent pointer value, do it now. Also, 13907 * send up a zero-length M_DATA indicating 13908 * whether or not this is the mark. The latter 13909 * is not needed when a T_EXDATA_IND is sent up. 13910 * However, if there are allocation failures 13911 * this code relies on the sender retransmitting 13912 * and the socket code for determining the mark 13913 * should not block waiting for the peer to 13914 * transmit. Thus, for simplicity we always 13915 * send up the mark indication. 13916 */ 13917 mp1 = allocb(0, BPRI_MED); 13918 if (mp1 == NULL) { 13919 freemsg(mp); 13920 return; 13921 } 13922 if (!TCP_IS_DETACHED(tcp) && 13923 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13924 SIGURG)) { 13925 /* Try again on the rexmit. */ 13926 freemsg(mp1); 13927 freemsg(mp); 13928 return; 13929 } 13930 /* 13931 * Mark with NOTMARKNEXT for now. 13932 * The code below will change this to MARKNEXT 13933 * if we are at the mark. 13934 * 13935 * If there are allocation failures (e.g. in 13936 * dupmsg below) the next time tcp_rput_data 13937 * sees the urgent segment it will send up the 13938 * MSGMARKNEXT message. 13939 */ 13940 mp1->b_flag |= MSGNOTMARKNEXT; 13941 freemsg(tcp->tcp_urp_mark_mp); 13942 tcp->tcp_urp_mark_mp = mp1; 13943 flags |= TH_SEND_URP_MARK; 13944 #ifdef DEBUG 13945 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13946 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13947 "last %x, %s", 13948 seg_seq, urp, tcp->tcp_urp_last, 13949 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13950 #endif /* DEBUG */ 13951 } 13952 tcp->tcp_urp_last_valid = B_TRUE; 13953 tcp->tcp_urp_last = urp + seg_seq; 13954 } else if (tcp->tcp_urp_mark_mp != NULL) { 13955 /* 13956 * An allocation failure prevented the previous 13957 * tcp_rput_data from sending up the allocated 13958 * MSG*MARKNEXT message - send it up this time 13959 * around. 13960 */ 13961 flags |= TH_SEND_URP_MARK; 13962 } 13963 13964 /* 13965 * If the urgent byte is in this segment, make sure that it is 13966 * all by itself. This makes it much easier to deal with the 13967 * possibility of an allocation failure on the T_exdata_ind. 13968 * Note that seg_len is the number of bytes in the segment, and 13969 * urp is the offset into the segment of the urgent byte. 13970 * urp < seg_len means that the urgent byte is in this segment. 13971 */ 13972 if (urp < seg_len) { 13973 if (seg_len != 1) { 13974 uint32_t tmp_rnxt; 13975 /* 13976 * Break it up and feed it back in. 13977 * Re-attach the IP header. 13978 */ 13979 mp->b_rptr = iphdr; 13980 if (urp > 0) { 13981 /* 13982 * There is stuff before the urgent 13983 * byte. 13984 */ 13985 mp1 = dupmsg(mp); 13986 if (!mp1) { 13987 /* 13988 * Trim from urgent byte on. 13989 * The rest will come back. 13990 */ 13991 (void) adjmsg(mp, 13992 urp - seg_len); 13993 tcp_rput_data(connp, 13994 mp, NULL); 13995 return; 13996 } 13997 (void) adjmsg(mp1, urp - seg_len); 13998 /* Feed this piece back in. */ 13999 tmp_rnxt = tcp->tcp_rnxt; 14000 tcp_rput_data(connp, mp1, NULL); 14001 /* 14002 * If the data passed back in was not 14003 * processed (ie: bad ACK) sending 14004 * the remainder back in will cause a 14005 * loop. In this case, drop the 14006 * packet and let the sender try 14007 * sending a good packet. 14008 */ 14009 if (tmp_rnxt == tcp->tcp_rnxt) { 14010 freemsg(mp); 14011 return; 14012 } 14013 } 14014 if (urp != seg_len - 1) { 14015 uint32_t tmp_rnxt; 14016 /* 14017 * There is stuff after the urgent 14018 * byte. 14019 */ 14020 mp1 = dupmsg(mp); 14021 if (!mp1) { 14022 /* 14023 * Trim everything beyond the 14024 * urgent byte. The rest will 14025 * come back. 14026 */ 14027 (void) adjmsg(mp, 14028 urp + 1 - seg_len); 14029 tcp_rput_data(connp, 14030 mp, NULL); 14031 return; 14032 } 14033 (void) adjmsg(mp1, urp + 1 - seg_len); 14034 tmp_rnxt = tcp->tcp_rnxt; 14035 tcp_rput_data(connp, mp1, NULL); 14036 /* 14037 * If the data passed back in was not 14038 * processed (ie: bad ACK) sending 14039 * the remainder back in will cause a 14040 * loop. In this case, drop the 14041 * packet and let the sender try 14042 * sending a good packet. 14043 */ 14044 if (tmp_rnxt == tcp->tcp_rnxt) { 14045 freemsg(mp); 14046 return; 14047 } 14048 } 14049 tcp_rput_data(connp, mp, NULL); 14050 return; 14051 } 14052 /* 14053 * This segment contains only the urgent byte. We 14054 * have to allocate the T_exdata_ind, if we can. 14055 */ 14056 if (IPCL_IS_NONSTR(connp)) { 14057 int error; 14058 14059 (*connp->conn_upcalls->su_recv) 14060 (connp->conn_upper_handle, mp, seg_len, 14061 MSG_OOB, &error, NULL); 14062 /* 14063 * We should never be in middle of a 14064 * fallback, the squeue guarantees that. 14065 */ 14066 ASSERT(error != EOPNOTSUPP); 14067 mp = NULL; 14068 goto update_ack; 14069 } else if (!tcp->tcp_urp_mp) { 14070 struct T_exdata_ind *tei; 14071 mp1 = allocb(sizeof (struct T_exdata_ind), 14072 BPRI_MED); 14073 if (!mp1) { 14074 /* 14075 * Sigh... It'll be back. 14076 * Generate any MSG*MARK message now. 14077 */ 14078 freemsg(mp); 14079 seg_len = 0; 14080 if (flags & TH_SEND_URP_MARK) { 14081 14082 14083 ASSERT(tcp->tcp_urp_mark_mp); 14084 tcp->tcp_urp_mark_mp->b_flag &= 14085 ~MSGNOTMARKNEXT; 14086 tcp->tcp_urp_mark_mp->b_flag |= 14087 MSGMARKNEXT; 14088 } 14089 goto ack_check; 14090 } 14091 mp1->b_datap->db_type = M_PROTO; 14092 tei = (struct T_exdata_ind *)mp1->b_rptr; 14093 tei->PRIM_type = T_EXDATA_IND; 14094 tei->MORE_flag = 0; 14095 mp1->b_wptr = (uchar_t *)&tei[1]; 14096 tcp->tcp_urp_mp = mp1; 14097 #ifdef DEBUG 14098 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14099 "tcp_rput: allocated exdata_ind %s", 14100 tcp_display(tcp, NULL, 14101 DISP_PORT_ONLY)); 14102 #endif /* DEBUG */ 14103 /* 14104 * There is no need to send a separate MSG*MARK 14105 * message since the T_EXDATA_IND will be sent 14106 * now. 14107 */ 14108 flags &= ~TH_SEND_URP_MARK; 14109 freemsg(tcp->tcp_urp_mark_mp); 14110 tcp->tcp_urp_mark_mp = NULL; 14111 } 14112 /* 14113 * Now we are all set. On the next putnext upstream, 14114 * tcp_urp_mp will be non-NULL and will get prepended 14115 * to what has to be this piece containing the urgent 14116 * byte. If for any reason we abort this segment below, 14117 * if it comes back, we will have this ready, or it 14118 * will get blown off in close. 14119 */ 14120 } else if (urp == seg_len) { 14121 /* 14122 * The urgent byte is the next byte after this sequence 14123 * number. If there is data it is marked with 14124 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14125 * since it is not needed. Otherwise, if the code 14126 * above just allocated a zero-length tcp_urp_mark_mp 14127 * message, that message is tagged with MSGMARKNEXT. 14128 * Sending up these MSGMARKNEXT messages makes 14129 * SIOCATMARK work correctly even though 14130 * the T_EXDATA_IND will not be sent up until the 14131 * urgent byte arrives. 14132 */ 14133 if (seg_len != 0) { 14134 flags |= TH_MARKNEXT_NEEDED; 14135 freemsg(tcp->tcp_urp_mark_mp); 14136 tcp->tcp_urp_mark_mp = NULL; 14137 flags &= ~TH_SEND_URP_MARK; 14138 } else if (tcp->tcp_urp_mark_mp != NULL) { 14139 flags |= TH_SEND_URP_MARK; 14140 tcp->tcp_urp_mark_mp->b_flag &= 14141 ~MSGNOTMARKNEXT; 14142 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14143 } 14144 #ifdef DEBUG 14145 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14146 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14147 seg_len, flags, 14148 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14149 #endif /* DEBUG */ 14150 } 14151 #ifdef DEBUG 14152 else { 14153 /* Data left until we hit mark */ 14154 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14155 "tcp_rput: URP %d bytes left, %s", 14156 urp - seg_len, tcp_display(tcp, NULL, 14157 DISP_PORT_ONLY)); 14158 } 14159 #endif /* DEBUG */ 14160 } 14161 14162 process_ack: 14163 if (!(flags & TH_ACK)) { 14164 freemsg(mp); 14165 goto xmit_check; 14166 } 14167 } 14168 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14169 14170 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14171 tcp->tcp_ip_forward_progress = B_TRUE; 14172 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14173 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14174 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14175 /* 3-way handshake complete - pass up the T_CONN_IND */ 14176 tcp_t *listener = tcp->tcp_listener; 14177 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14178 14179 tcp->tcp_tconnind_started = B_TRUE; 14180 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14181 /* 14182 * We are here means eager is fine but it can 14183 * get a TH_RST at any point between now and till 14184 * accept completes and disappear. We need to 14185 * ensure that reference to eager is valid after 14186 * we get out of eager's perimeter. So we do 14187 * an extra refhold. 14188 */ 14189 CONN_INC_REF(connp); 14190 14191 /* 14192 * The listener also exists because of the refhold 14193 * done in tcp_conn_request. Its possible that it 14194 * might have closed. We will check that once we 14195 * get inside listeners context. 14196 */ 14197 CONN_INC_REF(listener->tcp_connp); 14198 if (listener->tcp_connp->conn_sqp == 14199 connp->conn_sqp) { 14200 /* 14201 * We optimize by not calling an SQUEUE_ENTER 14202 * on the listener since we know that the 14203 * listener and eager squeues are the same. 14204 * We are able to make this check safely only 14205 * because neither the eager nor the listener 14206 * can change its squeue. Only an active connect 14207 * can change its squeue 14208 */ 14209 tcp_send_conn_ind(listener->tcp_connp, mp, 14210 listener->tcp_connp->conn_sqp); 14211 CONN_DEC_REF(listener->tcp_connp); 14212 } else if (!tcp->tcp_loopback) { 14213 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14214 mp, tcp_send_conn_ind, 14215 listener->tcp_connp, SQ_FILL, 14216 SQTAG_TCP_CONN_IND); 14217 } else { 14218 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14219 mp, tcp_send_conn_ind, 14220 listener->tcp_connp, SQ_PROCESS, 14221 SQTAG_TCP_CONN_IND); 14222 } 14223 } 14224 14225 if (tcp->tcp_active_open) { 14226 /* 14227 * We are seeing the final ack in the three way 14228 * hand shake of a active open'ed connection 14229 * so we must send up a T_CONN_CON 14230 */ 14231 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14232 freemsg(mp); 14233 return; 14234 } 14235 /* 14236 * Don't fuse the loopback endpoints for 14237 * simultaneous active opens. 14238 */ 14239 if (tcp->tcp_loopback) { 14240 TCP_STAT(tcps, tcp_fusion_unfusable); 14241 tcp->tcp_unfusable = B_TRUE; 14242 } 14243 } 14244 14245 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14246 bytes_acked--; 14247 /* SYN was acked - making progress */ 14248 if (tcp->tcp_ipversion == IPV6_VERSION) 14249 tcp->tcp_ip_forward_progress = B_TRUE; 14250 14251 /* 14252 * If SYN was retransmitted, need to reset all 14253 * retransmission info as this segment will be 14254 * treated as a dup ACK. 14255 */ 14256 if (tcp->tcp_rexmit) { 14257 tcp->tcp_rexmit = B_FALSE; 14258 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14259 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14260 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14261 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14262 tcp->tcp_ms_we_have_waited = 0; 14263 tcp->tcp_cwnd = mss; 14264 } 14265 14266 /* 14267 * We set the send window to zero here. 14268 * This is needed if there is data to be 14269 * processed already on the queue. 14270 * Later (at swnd_update label), the 14271 * "new_swnd > tcp_swnd" condition is satisfied 14272 * the XMIT_NEEDED flag is set in the current 14273 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14274 * called if there is already data on queue in 14275 * this state. 14276 */ 14277 tcp->tcp_swnd = 0; 14278 14279 if (new_swnd > tcp->tcp_max_swnd) 14280 tcp->tcp_max_swnd = new_swnd; 14281 tcp->tcp_swl1 = seg_seq; 14282 tcp->tcp_swl2 = seg_ack; 14283 tcp->tcp_state = TCPS_ESTABLISHED; 14284 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14285 14286 /* Fuse when both sides are in ESTABLISHED state */ 14287 if (tcp->tcp_loopback && do_tcp_fusion) 14288 tcp_fuse(tcp, iphdr, tcph); 14289 14290 } 14291 /* This code follows 4.4BSD-Lite2 mostly. */ 14292 if (bytes_acked < 0) 14293 goto est; 14294 14295 /* 14296 * If TCP is ECN capable and the congestion experience bit is 14297 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14298 * done once per window (or more loosely, per RTT). 14299 */ 14300 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14301 tcp->tcp_cwr = B_FALSE; 14302 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14303 if (!tcp->tcp_cwr) { 14304 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14305 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14306 tcp->tcp_cwnd = npkt * mss; 14307 /* 14308 * If the cwnd is 0, use the timer to clock out 14309 * new segments. This is required by the ECN spec. 14310 */ 14311 if (npkt == 0) { 14312 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14313 /* 14314 * This makes sure that when the ACK comes 14315 * back, we will increase tcp_cwnd by 1 MSS. 14316 */ 14317 tcp->tcp_cwnd_cnt = 0; 14318 } 14319 tcp->tcp_cwr = B_TRUE; 14320 /* 14321 * This marks the end of the current window of in 14322 * flight data. That is why we don't use 14323 * tcp_suna + tcp_swnd. Only data in flight can 14324 * provide ECN info. 14325 */ 14326 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14327 tcp->tcp_ecn_cwr_sent = B_FALSE; 14328 } 14329 } 14330 14331 mp1 = tcp->tcp_xmit_head; 14332 if (bytes_acked == 0) { 14333 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14334 int dupack_cnt; 14335 14336 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14337 /* 14338 * Fast retransmit. When we have seen exactly three 14339 * identical ACKs while we have unacked data 14340 * outstanding we take it as a hint that our peer 14341 * dropped something. 14342 * 14343 * If TCP is retransmitting, don't do fast retransmit. 14344 */ 14345 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14346 ! tcp->tcp_rexmit) { 14347 /* Do Limited Transmit */ 14348 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14349 tcps->tcps_dupack_fast_retransmit) { 14350 /* 14351 * RFC 3042 14352 * 14353 * What we need to do is temporarily 14354 * increase tcp_cwnd so that new 14355 * data can be sent if it is allowed 14356 * by the receive window (tcp_rwnd). 14357 * tcp_wput_data() will take care of 14358 * the rest. 14359 * 14360 * If the connection is SACK capable, 14361 * only do limited xmit when there 14362 * is SACK info. 14363 * 14364 * Note how tcp_cwnd is incremented. 14365 * The first dup ACK will increase 14366 * it by 1 MSS. The second dup ACK 14367 * will increase it by 2 MSS. This 14368 * means that only 1 new segment will 14369 * be sent for each dup ACK. 14370 */ 14371 if (tcp->tcp_unsent > 0 && 14372 (!tcp->tcp_snd_sack_ok || 14373 (tcp->tcp_snd_sack_ok && 14374 tcp->tcp_notsack_list != NULL))) { 14375 tcp->tcp_cwnd += mss << 14376 (tcp->tcp_dupack_cnt - 1); 14377 flags |= TH_LIMIT_XMIT; 14378 } 14379 } else if (dupack_cnt == 14380 tcps->tcps_dupack_fast_retransmit) { 14381 14382 /* 14383 * If we have reduced tcp_ssthresh 14384 * because of ECN, do not reduce it again 14385 * unless it is already one window of data 14386 * away. After one window of data, tcp_cwr 14387 * should then be cleared. Note that 14388 * for non ECN capable connection, tcp_cwr 14389 * should always be false. 14390 * 14391 * Adjust cwnd since the duplicate 14392 * ack indicates that a packet was 14393 * dropped (due to congestion.) 14394 */ 14395 if (!tcp->tcp_cwr) { 14396 npkt = ((tcp->tcp_snxt - 14397 tcp->tcp_suna) >> 1) / mss; 14398 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14399 mss; 14400 tcp->tcp_cwnd = (npkt + 14401 tcp->tcp_dupack_cnt) * mss; 14402 } 14403 if (tcp->tcp_ecn_ok) { 14404 tcp->tcp_cwr = B_TRUE; 14405 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14406 tcp->tcp_ecn_cwr_sent = B_FALSE; 14407 } 14408 14409 /* 14410 * We do Hoe's algorithm. Refer to her 14411 * paper "Improving the Start-up Behavior 14412 * of a Congestion Control Scheme for TCP," 14413 * appeared in SIGCOMM'96. 14414 * 14415 * Save highest seq no we have sent so far. 14416 * Be careful about the invisible FIN byte. 14417 */ 14418 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14419 (tcp->tcp_unsent == 0)) { 14420 tcp->tcp_rexmit_max = tcp->tcp_fss; 14421 } else { 14422 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14423 } 14424 14425 /* 14426 * Do not allow bursty traffic during. 14427 * fast recovery. Refer to Fall and Floyd's 14428 * paper "Simulation-based Comparisons of 14429 * Tahoe, Reno and SACK TCP" (in CCR?) 14430 * This is a best current practise. 14431 */ 14432 tcp->tcp_snd_burst = TCP_CWND_SS; 14433 14434 /* 14435 * For SACK: 14436 * Calculate tcp_pipe, which is the 14437 * estimated number of bytes in 14438 * network. 14439 * 14440 * tcp_fack is the highest sack'ed seq num 14441 * TCP has received. 14442 * 14443 * tcp_pipe is explained in the above quoted 14444 * Fall and Floyd's paper. tcp_fack is 14445 * explained in Mathis and Mahdavi's 14446 * "Forward Acknowledgment: Refining TCP 14447 * Congestion Control" in SIGCOMM '96. 14448 */ 14449 if (tcp->tcp_snd_sack_ok) { 14450 ASSERT(tcp->tcp_sack_info != NULL); 14451 if (tcp->tcp_notsack_list != NULL) { 14452 tcp->tcp_pipe = tcp->tcp_snxt - 14453 tcp->tcp_fack; 14454 tcp->tcp_sack_snxt = seg_ack; 14455 flags |= TH_NEED_SACK_REXMIT; 14456 } else { 14457 /* 14458 * Always initialize tcp_pipe 14459 * even though we don't have 14460 * any SACK info. If later 14461 * we get SACK info and 14462 * tcp_pipe is not initialized, 14463 * funny things will happen. 14464 */ 14465 tcp->tcp_pipe = 14466 tcp->tcp_cwnd_ssthresh; 14467 } 14468 } else { 14469 flags |= TH_REXMIT_NEEDED; 14470 } /* tcp_snd_sack_ok */ 14471 14472 } else { 14473 /* 14474 * Here we perform congestion 14475 * avoidance, but NOT slow start. 14476 * This is known as the Fast 14477 * Recovery Algorithm. 14478 */ 14479 if (tcp->tcp_snd_sack_ok && 14480 tcp->tcp_notsack_list != NULL) { 14481 flags |= TH_NEED_SACK_REXMIT; 14482 tcp->tcp_pipe -= mss; 14483 if (tcp->tcp_pipe < 0) 14484 tcp->tcp_pipe = 0; 14485 } else { 14486 /* 14487 * We know that one more packet has 14488 * left the pipe thus we can update 14489 * cwnd. 14490 */ 14491 cwnd = tcp->tcp_cwnd + mss; 14492 if (cwnd > tcp->tcp_cwnd_max) 14493 cwnd = tcp->tcp_cwnd_max; 14494 tcp->tcp_cwnd = cwnd; 14495 if (tcp->tcp_unsent > 0) 14496 flags |= TH_XMIT_NEEDED; 14497 } 14498 } 14499 } 14500 } else if (tcp->tcp_zero_win_probe) { 14501 /* 14502 * If the window has opened, need to arrange 14503 * to send additional data. 14504 */ 14505 if (new_swnd != 0) { 14506 /* tcp_suna != tcp_snxt */ 14507 /* Packet contains a window update */ 14508 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14509 tcp->tcp_zero_win_probe = 0; 14510 tcp->tcp_timer_backoff = 0; 14511 tcp->tcp_ms_we_have_waited = 0; 14512 14513 /* 14514 * Transmit starting with tcp_suna since 14515 * the one byte probe is not ack'ed. 14516 * If TCP has sent more than one identical 14517 * probe, tcp_rexmit will be set. That means 14518 * tcp_ss_rexmit() will send out the one 14519 * byte along with new data. Otherwise, 14520 * fake the retransmission. 14521 */ 14522 flags |= TH_XMIT_NEEDED; 14523 if (!tcp->tcp_rexmit) { 14524 tcp->tcp_rexmit = B_TRUE; 14525 tcp->tcp_dupack_cnt = 0; 14526 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14527 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14528 } 14529 } 14530 } 14531 goto swnd_update; 14532 } 14533 14534 /* 14535 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14536 * If the ACK value acks something that we have not yet sent, it might 14537 * be an old duplicate segment. Send an ACK to re-synchronize the 14538 * other side. 14539 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14540 * state is handled above, so we can always just drop the segment and 14541 * send an ACK here. 14542 * 14543 * Should we send ACKs in response to ACK only segments? 14544 */ 14545 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14546 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14547 /* drop the received segment */ 14548 freemsg(mp); 14549 14550 /* 14551 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14552 * greater than 0, check if the number of such 14553 * bogus ACks is greater than that count. If yes, 14554 * don't send back any ACK. This prevents TCP from 14555 * getting into an ACK storm if somehow an attacker 14556 * successfully spoofs an acceptable segment to our 14557 * peer. 14558 */ 14559 if (tcp_drop_ack_unsent_cnt > 0 && 14560 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14561 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14562 return; 14563 } 14564 mp = tcp_ack_mp(tcp); 14565 if (mp != NULL) { 14566 BUMP_LOCAL(tcp->tcp_obsegs); 14567 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14568 tcp_send_data(tcp, tcp->tcp_wq, mp); 14569 } 14570 return; 14571 } 14572 14573 /* 14574 * TCP gets a new ACK, update the notsack'ed list to delete those 14575 * blocks that are covered by this ACK. 14576 */ 14577 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14578 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14579 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14580 } 14581 14582 /* 14583 * If we got an ACK after fast retransmit, check to see 14584 * if it is a partial ACK. If it is not and the congestion 14585 * window was inflated to account for the other side's 14586 * cached packets, retract it. If it is, do Hoe's algorithm. 14587 */ 14588 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14589 ASSERT(tcp->tcp_rexmit == B_FALSE); 14590 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14591 tcp->tcp_dupack_cnt = 0; 14592 /* 14593 * Restore the orig tcp_cwnd_ssthresh after 14594 * fast retransmit phase. 14595 */ 14596 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14597 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14598 } 14599 tcp->tcp_rexmit_max = seg_ack; 14600 tcp->tcp_cwnd_cnt = 0; 14601 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14602 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14603 14604 /* 14605 * Remove all notsack info to avoid confusion with 14606 * the next fast retrasnmit/recovery phase. 14607 */ 14608 if (tcp->tcp_snd_sack_ok && 14609 tcp->tcp_notsack_list != NULL) { 14610 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14611 } 14612 } else { 14613 if (tcp->tcp_snd_sack_ok && 14614 tcp->tcp_notsack_list != NULL) { 14615 flags |= TH_NEED_SACK_REXMIT; 14616 tcp->tcp_pipe -= mss; 14617 if (tcp->tcp_pipe < 0) 14618 tcp->tcp_pipe = 0; 14619 } else { 14620 /* 14621 * Hoe's algorithm: 14622 * 14623 * Retransmit the unack'ed segment and 14624 * restart fast recovery. Note that we 14625 * need to scale back tcp_cwnd to the 14626 * original value when we started fast 14627 * recovery. This is to prevent overly 14628 * aggressive behaviour in sending new 14629 * segments. 14630 */ 14631 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14632 tcps->tcps_dupack_fast_retransmit * mss; 14633 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14634 flags |= TH_REXMIT_NEEDED; 14635 } 14636 } 14637 } else { 14638 tcp->tcp_dupack_cnt = 0; 14639 if (tcp->tcp_rexmit) { 14640 /* 14641 * TCP is retranmitting. If the ACK ack's all 14642 * outstanding data, update tcp_rexmit_max and 14643 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14644 * to the correct value. 14645 * 14646 * Note that SEQ_LEQ() is used. This is to avoid 14647 * unnecessary fast retransmit caused by dup ACKs 14648 * received when TCP does slow start retransmission 14649 * after a time out. During this phase, TCP may 14650 * send out segments which are already received. 14651 * This causes dup ACKs to be sent back. 14652 */ 14653 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14654 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14655 tcp->tcp_rexmit_nxt = seg_ack; 14656 } 14657 if (seg_ack != tcp->tcp_rexmit_max) { 14658 flags |= TH_XMIT_NEEDED; 14659 } 14660 } else { 14661 tcp->tcp_rexmit = B_FALSE; 14662 tcp->tcp_xmit_zc_clean = B_FALSE; 14663 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14664 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14665 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14666 } 14667 tcp->tcp_ms_we_have_waited = 0; 14668 } 14669 } 14670 14671 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14672 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14673 tcp->tcp_suna = seg_ack; 14674 if (tcp->tcp_zero_win_probe != 0) { 14675 tcp->tcp_zero_win_probe = 0; 14676 tcp->tcp_timer_backoff = 0; 14677 } 14678 14679 /* 14680 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14681 * Note that it cannot be the SYN being ack'ed. The code flow 14682 * will not reach here. 14683 */ 14684 if (mp1 == NULL) { 14685 goto fin_acked; 14686 } 14687 14688 /* 14689 * Update the congestion window. 14690 * 14691 * If TCP is not ECN capable or TCP is ECN capable but the 14692 * congestion experience bit is not set, increase the tcp_cwnd as 14693 * usual. 14694 */ 14695 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14696 cwnd = tcp->tcp_cwnd; 14697 add = mss; 14698 14699 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14700 /* 14701 * This is to prevent an increase of less than 1 MSS of 14702 * tcp_cwnd. With partial increase, tcp_wput_data() 14703 * may send out tinygrams in order to preserve mblk 14704 * boundaries. 14705 * 14706 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14707 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14708 * increased by 1 MSS for every RTTs. 14709 */ 14710 if (tcp->tcp_cwnd_cnt <= 0) { 14711 tcp->tcp_cwnd_cnt = cwnd + add; 14712 } else { 14713 tcp->tcp_cwnd_cnt -= add; 14714 add = 0; 14715 } 14716 } 14717 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14718 } 14719 14720 /* See if the latest urgent data has been acknowledged */ 14721 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14722 SEQ_GT(seg_ack, tcp->tcp_urg)) 14723 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14724 14725 /* Can we update the RTT estimates? */ 14726 if (tcp->tcp_snd_ts_ok) { 14727 /* Ignore zero timestamp echo-reply. */ 14728 if (tcpopt.tcp_opt_ts_ecr != 0) { 14729 tcp_set_rto(tcp, (int32_t)lbolt - 14730 (int32_t)tcpopt.tcp_opt_ts_ecr); 14731 } 14732 14733 /* If needed, restart the timer. */ 14734 if (tcp->tcp_set_timer == 1) { 14735 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14736 tcp->tcp_set_timer = 0; 14737 } 14738 /* 14739 * Update tcp_csuna in case the other side stops sending 14740 * us timestamps. 14741 */ 14742 tcp->tcp_csuna = tcp->tcp_snxt; 14743 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14744 /* 14745 * An ACK sequence we haven't seen before, so get the RTT 14746 * and update the RTO. But first check if the timestamp is 14747 * valid to use. 14748 */ 14749 if ((mp1->b_next != NULL) && 14750 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14751 tcp_set_rto(tcp, (int32_t)lbolt - 14752 (int32_t)(intptr_t)mp1->b_prev); 14753 else 14754 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14755 14756 /* Remeber the last sequence to be ACKed */ 14757 tcp->tcp_csuna = seg_ack; 14758 if (tcp->tcp_set_timer == 1) { 14759 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14760 tcp->tcp_set_timer = 0; 14761 } 14762 } else { 14763 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14764 } 14765 14766 /* Eat acknowledged bytes off the xmit queue. */ 14767 for (;;) { 14768 mblk_t *mp2; 14769 uchar_t *wptr; 14770 14771 wptr = mp1->b_wptr; 14772 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14773 bytes_acked -= (int)(wptr - mp1->b_rptr); 14774 if (bytes_acked < 0) { 14775 mp1->b_rptr = wptr + bytes_acked; 14776 /* 14777 * Set a new timestamp if all the bytes timed by the 14778 * old timestamp have been ack'ed. 14779 */ 14780 if (SEQ_GT(seg_ack, 14781 (uint32_t)(uintptr_t)(mp1->b_next))) { 14782 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14783 mp1->b_next = NULL; 14784 } 14785 break; 14786 } 14787 mp1->b_next = NULL; 14788 mp1->b_prev = NULL; 14789 mp2 = mp1; 14790 mp1 = mp1->b_cont; 14791 14792 /* 14793 * This notification is required for some zero-copy 14794 * clients to maintain a copy semantic. After the data 14795 * is ack'ed, client is safe to modify or reuse the buffer. 14796 */ 14797 if (tcp->tcp_snd_zcopy_aware && 14798 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14799 tcp_zcopy_notify(tcp); 14800 freeb(mp2); 14801 if (bytes_acked == 0) { 14802 if (mp1 == NULL) { 14803 /* Everything is ack'ed, clear the tail. */ 14804 tcp->tcp_xmit_tail = NULL; 14805 /* 14806 * Cancel the timer unless we are still 14807 * waiting for an ACK for the FIN packet. 14808 */ 14809 if (tcp->tcp_timer_tid != 0 && 14810 tcp->tcp_snxt == tcp->tcp_suna) { 14811 (void) TCP_TIMER_CANCEL(tcp, 14812 tcp->tcp_timer_tid); 14813 tcp->tcp_timer_tid = 0; 14814 } 14815 goto pre_swnd_update; 14816 } 14817 if (mp2 != tcp->tcp_xmit_tail) 14818 break; 14819 tcp->tcp_xmit_tail = mp1; 14820 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14821 (uintptr_t)INT_MAX); 14822 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14823 mp1->b_rptr); 14824 break; 14825 } 14826 if (mp1 == NULL) { 14827 /* 14828 * More was acked but there is nothing more 14829 * outstanding. This means that the FIN was 14830 * just acked or that we're talking to a clown. 14831 */ 14832 fin_acked: 14833 ASSERT(tcp->tcp_fin_sent); 14834 tcp->tcp_xmit_tail = NULL; 14835 if (tcp->tcp_fin_sent) { 14836 /* FIN was acked - making progress */ 14837 if (tcp->tcp_ipversion == IPV6_VERSION && 14838 !tcp->tcp_fin_acked) 14839 tcp->tcp_ip_forward_progress = B_TRUE; 14840 tcp->tcp_fin_acked = B_TRUE; 14841 if (tcp->tcp_linger_tid != 0 && 14842 TCP_TIMER_CANCEL(tcp, 14843 tcp->tcp_linger_tid) >= 0) { 14844 tcp_stop_lingering(tcp); 14845 freemsg(mp); 14846 mp = NULL; 14847 } 14848 } else { 14849 /* 14850 * We should never get here because 14851 * we have already checked that the 14852 * number of bytes ack'ed should be 14853 * smaller than or equal to what we 14854 * have sent so far (it is the 14855 * acceptability check of the ACK). 14856 * We can only get here if the send 14857 * queue is corrupted. 14858 * 14859 * Terminate the connection and 14860 * panic the system. It is better 14861 * for us to panic instead of 14862 * continuing to avoid other disaster. 14863 */ 14864 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14865 tcp->tcp_rnxt, TH_RST|TH_ACK); 14866 panic("Memory corruption " 14867 "detected for connection %s.", 14868 tcp_display(tcp, NULL, 14869 DISP_ADDR_AND_PORT)); 14870 /*NOTREACHED*/ 14871 } 14872 goto pre_swnd_update; 14873 } 14874 ASSERT(mp2 != tcp->tcp_xmit_tail); 14875 } 14876 if (tcp->tcp_unsent) { 14877 flags |= TH_XMIT_NEEDED; 14878 } 14879 pre_swnd_update: 14880 tcp->tcp_xmit_head = mp1; 14881 swnd_update: 14882 /* 14883 * The following check is different from most other implementations. 14884 * For bi-directional transfer, when segments are dropped, the 14885 * "normal" check will not accept a window update in those 14886 * retransmitted segemnts. Failing to do that, TCP may send out 14887 * segments which are outside receiver's window. As TCP accepts 14888 * the ack in those retransmitted segments, if the window update in 14889 * the same segment is not accepted, TCP will incorrectly calculates 14890 * that it can send more segments. This can create a deadlock 14891 * with the receiver if its window becomes zero. 14892 */ 14893 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14894 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14895 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14896 /* 14897 * The criteria for update is: 14898 * 14899 * 1. the segment acknowledges some data. Or 14900 * 2. the segment is new, i.e. it has a higher seq num. Or 14901 * 3. the segment is not old and the advertised window is 14902 * larger than the previous advertised window. 14903 */ 14904 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14905 flags |= TH_XMIT_NEEDED; 14906 tcp->tcp_swnd = new_swnd; 14907 if (new_swnd > tcp->tcp_max_swnd) 14908 tcp->tcp_max_swnd = new_swnd; 14909 tcp->tcp_swl1 = seg_seq; 14910 tcp->tcp_swl2 = seg_ack; 14911 } 14912 est: 14913 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14914 14915 switch (tcp->tcp_state) { 14916 case TCPS_FIN_WAIT_1: 14917 if (tcp->tcp_fin_acked) { 14918 tcp->tcp_state = TCPS_FIN_WAIT_2; 14919 /* 14920 * We implement the non-standard BSD/SunOS 14921 * FIN_WAIT_2 flushing algorithm. 14922 * If there is no user attached to this 14923 * TCP endpoint, then this TCP struct 14924 * could hang around forever in FIN_WAIT_2 14925 * state if the peer forgets to send us 14926 * a FIN. To prevent this, we wait only 14927 * 2*MSL (a convenient time value) for 14928 * the FIN to arrive. If it doesn't show up, 14929 * we flush the TCP endpoint. This algorithm, 14930 * though a violation of RFC-793, has worked 14931 * for over 10 years in BSD systems. 14932 * Note: SunOS 4.x waits 675 seconds before 14933 * flushing the FIN_WAIT_2 connection. 14934 */ 14935 TCP_TIMER_RESTART(tcp, 14936 tcps->tcps_fin_wait_2_flush_interval); 14937 } 14938 break; 14939 case TCPS_FIN_WAIT_2: 14940 break; /* Shutdown hook? */ 14941 case TCPS_LAST_ACK: 14942 freemsg(mp); 14943 if (tcp->tcp_fin_acked) { 14944 (void) tcp_clean_death(tcp, 0, 19); 14945 return; 14946 } 14947 goto xmit_check; 14948 case TCPS_CLOSING: 14949 if (tcp->tcp_fin_acked) { 14950 tcp->tcp_state = TCPS_TIME_WAIT; 14951 /* 14952 * Unconditionally clear the exclusive binding 14953 * bit so this TIME-WAIT connection won't 14954 * interfere with new ones. 14955 */ 14956 tcp->tcp_exclbind = 0; 14957 if (!TCP_IS_DETACHED(tcp)) { 14958 TCP_TIMER_RESTART(tcp, 14959 tcps->tcps_time_wait_interval); 14960 } else { 14961 tcp_time_wait_append(tcp); 14962 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14963 } 14964 } 14965 /*FALLTHRU*/ 14966 case TCPS_CLOSE_WAIT: 14967 freemsg(mp); 14968 goto xmit_check; 14969 default: 14970 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14971 break; 14972 } 14973 } 14974 if (flags & TH_FIN) { 14975 /* Make sure we ack the fin */ 14976 flags |= TH_ACK_NEEDED; 14977 if (!tcp->tcp_fin_rcvd) { 14978 tcp->tcp_fin_rcvd = B_TRUE; 14979 tcp->tcp_rnxt++; 14980 tcph = tcp->tcp_tcph; 14981 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14982 14983 /* 14984 * Generate the ordrel_ind at the end unless we 14985 * are an eager guy. 14986 * In the eager case tcp_rsrv will do this when run 14987 * after tcp_accept is done. 14988 */ 14989 if (tcp->tcp_listener == NULL && 14990 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14991 flags |= TH_ORDREL_NEEDED; 14992 switch (tcp->tcp_state) { 14993 case TCPS_SYN_RCVD: 14994 case TCPS_ESTABLISHED: 14995 tcp->tcp_state = TCPS_CLOSE_WAIT; 14996 /* Keepalive? */ 14997 break; 14998 case TCPS_FIN_WAIT_1: 14999 if (!tcp->tcp_fin_acked) { 15000 tcp->tcp_state = TCPS_CLOSING; 15001 break; 15002 } 15003 /* FALLTHRU */ 15004 case TCPS_FIN_WAIT_2: 15005 tcp->tcp_state = TCPS_TIME_WAIT; 15006 /* 15007 * Unconditionally clear the exclusive binding 15008 * bit so this TIME-WAIT connection won't 15009 * interfere with new ones. 15010 */ 15011 tcp->tcp_exclbind = 0; 15012 if (!TCP_IS_DETACHED(tcp)) { 15013 TCP_TIMER_RESTART(tcp, 15014 tcps->tcps_time_wait_interval); 15015 } else { 15016 tcp_time_wait_append(tcp); 15017 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15018 } 15019 if (seg_len) { 15020 /* 15021 * implies data piggybacked on FIN. 15022 * break to handle data. 15023 */ 15024 break; 15025 } 15026 freemsg(mp); 15027 goto ack_check; 15028 } 15029 } 15030 } 15031 if (mp == NULL) 15032 goto xmit_check; 15033 if (seg_len == 0) { 15034 freemsg(mp); 15035 goto xmit_check; 15036 } 15037 if (mp->b_rptr == mp->b_wptr) { 15038 /* 15039 * The header has been consumed, so we remove the 15040 * zero-length mblk here. 15041 */ 15042 mp1 = mp; 15043 mp = mp->b_cont; 15044 freeb(mp1); 15045 } 15046 update_ack: 15047 tcph = tcp->tcp_tcph; 15048 tcp->tcp_rack_cnt++; 15049 { 15050 uint32_t cur_max; 15051 15052 cur_max = tcp->tcp_rack_cur_max; 15053 if (tcp->tcp_rack_cnt >= cur_max) { 15054 /* 15055 * We have more unacked data than we should - send 15056 * an ACK now. 15057 */ 15058 flags |= TH_ACK_NEEDED; 15059 cur_max++; 15060 if (cur_max > tcp->tcp_rack_abs_max) 15061 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15062 else 15063 tcp->tcp_rack_cur_max = cur_max; 15064 } else if (TCP_IS_DETACHED(tcp)) { 15065 /* We don't have an ACK timer for detached TCP. */ 15066 flags |= TH_ACK_NEEDED; 15067 } else if (seg_len < mss) { 15068 /* 15069 * If we get a segment that is less than an mss, and we 15070 * already have unacknowledged data, and the amount 15071 * unacknowledged is not a multiple of mss, then we 15072 * better generate an ACK now. Otherwise, this may be 15073 * the tail piece of a transaction, and we would rather 15074 * wait for the response. 15075 */ 15076 uint32_t udif; 15077 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15078 (uintptr_t)INT_MAX); 15079 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15080 if (udif && (udif % mss)) 15081 flags |= TH_ACK_NEEDED; 15082 else 15083 flags |= TH_ACK_TIMER_NEEDED; 15084 } else { 15085 /* Start delayed ack timer */ 15086 flags |= TH_ACK_TIMER_NEEDED; 15087 } 15088 } 15089 tcp->tcp_rnxt += seg_len; 15090 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15091 15092 if (mp == NULL) 15093 goto xmit_check; 15094 15095 /* Update SACK list */ 15096 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15097 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15098 &(tcp->tcp_num_sack_blk)); 15099 } 15100 15101 if (tcp->tcp_urp_mp) { 15102 tcp->tcp_urp_mp->b_cont = mp; 15103 mp = tcp->tcp_urp_mp; 15104 tcp->tcp_urp_mp = NULL; 15105 /* Ready for a new signal. */ 15106 tcp->tcp_urp_last_valid = B_FALSE; 15107 #ifdef DEBUG 15108 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15109 "tcp_rput: sending exdata_ind %s", 15110 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15111 #endif /* DEBUG */ 15112 } 15113 15114 /* 15115 * Check for ancillary data changes compared to last segment. 15116 */ 15117 if (tcp->tcp_ipv6_recvancillary != 0) { 15118 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15119 ASSERT(mp != NULL); 15120 } 15121 15122 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15123 /* 15124 * Side queue inbound data until the accept happens. 15125 * tcp_accept/tcp_rput drains this when the accept happens. 15126 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15127 * T_EXDATA_IND) it is queued on b_next. 15128 * XXX Make urgent data use this. Requires: 15129 * Removing tcp_listener check for TH_URG 15130 * Making M_PCPROTO and MARK messages skip the eager case 15131 */ 15132 15133 if (tcp->tcp_kssl_pending) { 15134 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15135 mblk_t *, mp); 15136 tcp_kssl_input(tcp, mp); 15137 } else { 15138 tcp_rcv_enqueue(tcp, mp, seg_len); 15139 } 15140 } else { 15141 sodirect_t *sodp = tcp->tcp_sodirect; 15142 15143 /* 15144 * If an sodirect connection and an enabled sodirect_t then 15145 * sodp will be set to point to the tcp_t/sonode_t shared 15146 * sodirect_t and the sodirect_t's lock will be held. 15147 */ 15148 if (sodp != NULL) { 15149 mutex_enter(sodp->sod_lockp); 15150 if (!(sodp->sod_state & SOD_ENABLED) || 15151 (tcp->tcp_kssl_ctx != NULL && 15152 DB_TYPE(mp) == M_DATA)) { 15153 mutex_exit(sodp->sod_lockp); 15154 sodp = NULL; 15155 } else { 15156 mutex_exit(sodp->sod_lockp); 15157 } 15158 } 15159 if (mp->b_datap->db_type != M_DATA || 15160 (flags & TH_MARKNEXT_NEEDED)) { 15161 if (IPCL_IS_NONSTR(connp)) { 15162 int error; 15163 15164 if ((*connp->conn_upcalls->su_recv) 15165 (connp->conn_upper_handle, mp, 15166 seg_len, 0, &error, NULL) <= 0) { 15167 /* 15168 * We should never be in middle of a 15169 * fallback, the squeue guarantees that. 15170 */ 15171 ASSERT(error != EOPNOTSUPP); 15172 if (error == ENOSPC) 15173 tcp->tcp_rwnd -= seg_len; 15174 } 15175 } else if (sodp != NULL) { 15176 mutex_enter(sodp->sod_lockp); 15177 SOD_UIOAFINI(sodp); 15178 if (!SOD_QEMPTY(sodp) && 15179 (sodp->sod_state & SOD_WAKE_NOT)) { 15180 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15181 /* sod_wakeup() did the mutex_exit() */ 15182 } else { 15183 mutex_exit(sodp->sod_lockp); 15184 } 15185 } else if (tcp->tcp_rcv_list != NULL) { 15186 flags |= tcp_rcv_drain(tcp); 15187 } 15188 ASSERT(tcp->tcp_rcv_list == NULL || 15189 tcp->tcp_fused_sigurg); 15190 15191 if (flags & TH_MARKNEXT_NEEDED) { 15192 #ifdef DEBUG 15193 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15194 "tcp_rput: sending MSGMARKNEXT %s", 15195 tcp_display(tcp, NULL, 15196 DISP_PORT_ONLY)); 15197 #endif /* DEBUG */ 15198 mp->b_flag |= MSGMARKNEXT; 15199 flags &= ~TH_MARKNEXT_NEEDED; 15200 } 15201 15202 /* Does this need SSL processing first? */ 15203 if ((tcp->tcp_kssl_ctx != NULL) && 15204 (DB_TYPE(mp) == M_DATA)) { 15205 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15206 mblk_t *, mp); 15207 tcp_kssl_input(tcp, mp); 15208 } else if (!IPCL_IS_NONSTR(connp)) { 15209 /* Already handled non-STREAMS case. */ 15210 putnext(tcp->tcp_rq, mp); 15211 if (!canputnext(tcp->tcp_rq)) 15212 tcp->tcp_rwnd -= seg_len; 15213 } 15214 } else if ((tcp->tcp_kssl_ctx != NULL) && 15215 (DB_TYPE(mp) == M_DATA)) { 15216 /* Does this need SSL processing first? */ 15217 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15218 tcp_kssl_input(tcp, mp); 15219 } else if (IPCL_IS_NONSTR(connp)) { 15220 /* Non-STREAMS socket */ 15221 boolean_t push = flags & (TH_PUSH|TH_FIN); 15222 int error; 15223 15224 if ((*connp->conn_upcalls->su_recv)( 15225 connp->conn_upper_handle, 15226 mp, seg_len, 0, &error, &push) <= 0) { 15227 /* 15228 * We should never be in middle of a 15229 * fallback, the squeue guarantees that. 15230 */ 15231 ASSERT(error != EOPNOTSUPP); 15232 if (error == ENOSPC) 15233 tcp->tcp_rwnd -= seg_len; 15234 } else if (push) { 15235 /* 15236 * PUSH bit set and sockfs is not 15237 * flow controlled 15238 */ 15239 flags |= tcp_rwnd_reopen(tcp); 15240 } 15241 } else if (sodp != NULL) { 15242 /* 15243 * Sodirect so all mblk_t's are queued on the 15244 * socket directly, check for wakeup of blocked 15245 * reader (if any), and last if flow-controled. 15246 */ 15247 mutex_enter(sodp->sod_lockp); 15248 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15249 if ((sodp->sod_state & SOD_WAKE_NEED) || 15250 (flags & (TH_PUSH|TH_FIN))) { 15251 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15252 /* sod_wakeup() did the mutex_exit() */ 15253 } else { 15254 if (SOD_QFULL(sodp)) { 15255 /* Q is full, need backenable */ 15256 SOD_QSETBE(sodp); 15257 } 15258 mutex_exit(sodp->sod_lockp); 15259 } 15260 } else if ((flags & (TH_PUSH|TH_FIN)) || 15261 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15262 if (tcp->tcp_rcv_list != NULL) { 15263 /* 15264 * Enqueue the new segment first and then 15265 * call tcp_rcv_drain() to send all data 15266 * up. The other way to do this is to 15267 * send all queued data up and then call 15268 * putnext() to send the new segment up. 15269 * This way can remove the else part later 15270 * on. 15271 * 15272 * We don't do this to avoid one more call to 15273 * canputnext() as tcp_rcv_drain() needs to 15274 * call canputnext(). 15275 */ 15276 tcp_rcv_enqueue(tcp, mp, seg_len); 15277 flags |= tcp_rcv_drain(tcp); 15278 } else { 15279 putnext(tcp->tcp_rq, mp); 15280 if (!canputnext(tcp->tcp_rq)) 15281 tcp->tcp_rwnd -= seg_len; 15282 } 15283 } else { 15284 /* 15285 * Enqueue all packets when processing an mblk 15286 * from the co queue and also enqueue normal packets. 15287 * For packets which belong to SSL stream do SSL 15288 * processing first. 15289 */ 15290 tcp_rcv_enqueue(tcp, mp, seg_len); 15291 } 15292 /* 15293 * Make sure the timer is running if we have data waiting 15294 * for a push bit. This provides resiliency against 15295 * implementations that do not correctly generate push bits. 15296 * 15297 * Note, for sodirect if Q isn't empty and there's not a 15298 * pending wakeup then we need a timer. Also note that sodp 15299 * is assumed to be still valid after exit()ing the sod_lockp 15300 * above and while the SOD state can change it can only change 15301 * such that the Q is empty now even though data was added 15302 * above. 15303 */ 15304 if (!IPCL_IS_NONSTR(connp) && 15305 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15306 (sodp->sod_state & SOD_WAKE_NOT)) || 15307 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15308 tcp->tcp_push_tid == 0) { 15309 /* 15310 * The connection may be closed at this point, so don't 15311 * do anything for a detached tcp. 15312 */ 15313 if (!TCP_IS_DETACHED(tcp)) 15314 tcp->tcp_push_tid = TCP_TIMER(tcp, 15315 tcp_push_timer, 15316 MSEC_TO_TICK( 15317 tcps->tcps_push_timer_interval)); 15318 } 15319 } 15320 15321 xmit_check: 15322 /* Is there anything left to do? */ 15323 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15324 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15325 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15326 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15327 goto done; 15328 15329 /* Any transmit work to do and a non-zero window? */ 15330 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15331 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15332 if (flags & TH_REXMIT_NEEDED) { 15333 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15334 15335 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15336 if (snd_size > mss) 15337 snd_size = mss; 15338 if (snd_size > tcp->tcp_swnd) 15339 snd_size = tcp->tcp_swnd; 15340 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15341 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15342 B_TRUE); 15343 15344 if (mp1 != NULL) { 15345 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15346 tcp->tcp_csuna = tcp->tcp_snxt; 15347 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15348 UPDATE_MIB(&tcps->tcps_mib, 15349 tcpRetransBytes, snd_size); 15350 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15351 } 15352 } 15353 if (flags & TH_NEED_SACK_REXMIT) { 15354 tcp_sack_rxmit(tcp, &flags); 15355 } 15356 /* 15357 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15358 * out new segment. Note that tcp_rexmit should not be 15359 * set, otherwise TH_LIMIT_XMIT should not be set. 15360 */ 15361 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15362 if (!tcp->tcp_rexmit) { 15363 tcp_wput_data(tcp, NULL, B_FALSE); 15364 } else { 15365 tcp_ss_rexmit(tcp); 15366 } 15367 } 15368 /* 15369 * Adjust tcp_cwnd back to normal value after sending 15370 * new data segments. 15371 */ 15372 if (flags & TH_LIMIT_XMIT) { 15373 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15374 /* 15375 * This will restart the timer. Restarting the 15376 * timer is used to avoid a timeout before the 15377 * limited transmitted segment's ACK gets back. 15378 */ 15379 if (tcp->tcp_xmit_head != NULL) 15380 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15381 } 15382 15383 /* Anything more to do? */ 15384 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15385 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15386 goto done; 15387 } 15388 ack_check: 15389 if (flags & TH_SEND_URP_MARK) { 15390 ASSERT(tcp->tcp_urp_mark_mp); 15391 ASSERT(!IPCL_IS_NONSTR(connp)); 15392 /* 15393 * Send up any queued data and then send the mark message 15394 */ 15395 sodirect_t *sodp; 15396 15397 SOD_PTR_ENTER(tcp, sodp); 15398 15399 mp1 = tcp->tcp_urp_mark_mp; 15400 tcp->tcp_urp_mark_mp = NULL; 15401 if (sodp != NULL) { 15402 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15403 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15404 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15405 } 15406 ASSERT(tcp->tcp_rcv_list == NULL); 15407 15408 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15409 /* sod_wakeup() does the mutex_exit() */ 15410 } else if (tcp->tcp_rcv_list != NULL) { 15411 flags |= tcp_rcv_drain(tcp); 15412 15413 ASSERT(tcp->tcp_rcv_list == NULL || 15414 tcp->tcp_fused_sigurg); 15415 15416 } 15417 putnext(tcp->tcp_rq, mp1); 15418 #ifdef DEBUG 15419 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15420 "tcp_rput: sending zero-length %s %s", 15421 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15422 "MSGNOTMARKNEXT"), 15423 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15424 #endif /* DEBUG */ 15425 flags &= ~TH_SEND_URP_MARK; 15426 } 15427 if (flags & TH_ACK_NEEDED) { 15428 /* 15429 * Time to send an ack for some reason. 15430 */ 15431 mp1 = tcp_ack_mp(tcp); 15432 15433 if (mp1 != NULL) { 15434 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15435 BUMP_LOCAL(tcp->tcp_obsegs); 15436 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15437 } 15438 if (tcp->tcp_ack_tid != 0) { 15439 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15440 tcp->tcp_ack_tid = 0; 15441 } 15442 } 15443 if (flags & TH_ACK_TIMER_NEEDED) { 15444 /* 15445 * Arrange for deferred ACK or push wait timeout. 15446 * Start timer if it is not already running. 15447 */ 15448 if (tcp->tcp_ack_tid == 0) { 15449 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15450 MSEC_TO_TICK(tcp->tcp_localnet ? 15451 (clock_t)tcps->tcps_local_dack_interval : 15452 (clock_t)tcps->tcps_deferred_ack_interval)); 15453 } 15454 } 15455 if (flags & TH_ORDREL_NEEDED) { 15456 /* 15457 * Send up the ordrel_ind unless we are an eager guy. 15458 * In the eager case tcp_rsrv will do this when run 15459 * after tcp_accept is done. 15460 */ 15461 sodirect_t *sodp; 15462 15463 ASSERT(tcp->tcp_listener == NULL); 15464 15465 if (IPCL_IS_NONSTR(connp)) { 15466 ASSERT(tcp->tcp_ordrel_mp == NULL); 15467 tcp->tcp_ordrel_done = B_TRUE; 15468 (*connp->conn_upcalls->su_opctl) 15469 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15470 goto done; 15471 } 15472 15473 SOD_PTR_ENTER(tcp, sodp); 15474 if (sodp != NULL) { 15475 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15476 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15477 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15478 } 15479 /* No more sodirect */ 15480 tcp->tcp_sodirect = NULL; 15481 if (!SOD_QEMPTY(sodp)) { 15482 /* Mblk(s) to process, notify */ 15483 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15484 /* sod_wakeup() does the mutex_exit() */ 15485 } else { 15486 /* Nothing to process */ 15487 mutex_exit(sodp->sod_lockp); 15488 } 15489 } else if (tcp->tcp_rcv_list != NULL) { 15490 /* 15491 * Push any mblk(s) enqueued from co processing. 15492 */ 15493 flags |= tcp_rcv_drain(tcp); 15494 15495 ASSERT(tcp->tcp_rcv_list == NULL || 15496 tcp->tcp_fused_sigurg); 15497 } 15498 15499 mp1 = tcp->tcp_ordrel_mp; 15500 tcp->tcp_ordrel_mp = NULL; 15501 tcp->tcp_ordrel_done = B_TRUE; 15502 putnext(tcp->tcp_rq, mp1); 15503 } 15504 done: 15505 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15506 } 15507 15508 /* 15509 * This function does PAWS protection check. Returns B_TRUE if the 15510 * segment passes the PAWS test, else returns B_FALSE. 15511 */ 15512 boolean_t 15513 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15514 { 15515 uint8_t flags; 15516 int options; 15517 uint8_t *up; 15518 15519 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15520 /* 15521 * If timestamp option is aligned nicely, get values inline, 15522 * otherwise call general routine to parse. Only do that 15523 * if timestamp is the only option. 15524 */ 15525 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15526 TCPOPT_REAL_TS_LEN && 15527 OK_32PTR((up = ((uint8_t *)tcph) + 15528 TCP_MIN_HEADER_LENGTH)) && 15529 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15530 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15531 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15532 15533 options = TCP_OPT_TSTAMP_PRESENT; 15534 } else { 15535 if (tcp->tcp_snd_sack_ok) { 15536 tcpoptp->tcp = tcp; 15537 } else { 15538 tcpoptp->tcp = NULL; 15539 } 15540 options = tcp_parse_options(tcph, tcpoptp); 15541 } 15542 15543 if (options & TCP_OPT_TSTAMP_PRESENT) { 15544 /* 15545 * Do PAWS per RFC 1323 section 4.2. Accept RST 15546 * regardless of the timestamp, page 18 RFC 1323.bis. 15547 */ 15548 if ((flags & TH_RST) == 0 && 15549 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15550 tcp->tcp_ts_recent)) { 15551 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15552 PAWS_TIMEOUT)) { 15553 /* This segment is not acceptable. */ 15554 return (B_FALSE); 15555 } else { 15556 /* 15557 * Connection has been idle for 15558 * too long. Reset the timestamp 15559 * and assume the segment is valid. 15560 */ 15561 tcp->tcp_ts_recent = 15562 tcpoptp->tcp_opt_ts_val; 15563 } 15564 } 15565 } else { 15566 /* 15567 * If we don't get a timestamp on every packet, we 15568 * figure we can't really trust 'em, so we stop sending 15569 * and parsing them. 15570 */ 15571 tcp->tcp_snd_ts_ok = B_FALSE; 15572 15573 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15574 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15575 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15576 /* 15577 * Adjust the tcp_mss accordingly. We also need to 15578 * adjust tcp_cwnd here in accordance with the new mss. 15579 * But we avoid doing a slow start here so as to not 15580 * to lose on the transfer rate built up so far. 15581 */ 15582 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15583 if (tcp->tcp_snd_sack_ok) { 15584 ASSERT(tcp->tcp_sack_info != NULL); 15585 tcp->tcp_max_sack_blk = 4; 15586 } 15587 } 15588 return (B_TRUE); 15589 } 15590 15591 /* 15592 * Attach ancillary data to a received TCP segments for the 15593 * ancillary pieces requested by the application that are 15594 * different than they were in the previous data segment. 15595 * 15596 * Save the "current" values once memory allocation is ok so that 15597 * when memory allocation fails we can just wait for the next data segment. 15598 */ 15599 static mblk_t * 15600 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15601 { 15602 struct T_optdata_ind *todi; 15603 int optlen; 15604 uchar_t *optptr; 15605 struct T_opthdr *toh; 15606 uint_t addflag; /* Which pieces to add */ 15607 mblk_t *mp1; 15608 15609 optlen = 0; 15610 addflag = 0; 15611 /* If app asked for pktinfo and the index has changed ... */ 15612 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15613 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15614 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15615 optlen += sizeof (struct T_opthdr) + 15616 sizeof (struct in6_pktinfo); 15617 addflag |= TCP_IPV6_RECVPKTINFO; 15618 } 15619 /* If app asked for hoplimit and it has changed ... */ 15620 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15621 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15622 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15623 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15624 addflag |= TCP_IPV6_RECVHOPLIMIT; 15625 } 15626 /* If app asked for tclass and it has changed ... */ 15627 if ((ipp->ipp_fields & IPPF_TCLASS) && 15628 ipp->ipp_tclass != tcp->tcp_recvtclass && 15629 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15630 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15631 addflag |= TCP_IPV6_RECVTCLASS; 15632 } 15633 /* 15634 * If app asked for hopbyhop headers and it has changed ... 15635 * For security labels, note that (1) security labels can't change on 15636 * a connected socket at all, (2) we're connected to at most one peer, 15637 * (3) if anything changes, then it must be some other extra option. 15638 */ 15639 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15640 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15641 (ipp->ipp_fields & IPPF_HOPOPTS), 15642 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15643 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15644 tcp->tcp_label_len; 15645 addflag |= TCP_IPV6_RECVHOPOPTS; 15646 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15647 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15648 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15649 return (mp); 15650 } 15651 /* If app asked for dst headers before routing headers ... */ 15652 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15653 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15654 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15655 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15656 optlen += sizeof (struct T_opthdr) + 15657 ipp->ipp_rtdstoptslen; 15658 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15659 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15660 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15661 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15662 return (mp); 15663 } 15664 /* If app asked for routing headers and it has changed ... */ 15665 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15666 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15667 (ipp->ipp_fields & IPPF_RTHDR), 15668 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15669 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15670 addflag |= TCP_IPV6_RECVRTHDR; 15671 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15672 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15673 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15674 return (mp); 15675 } 15676 /* If app asked for dest headers and it has changed ... */ 15677 if ((tcp->tcp_ipv6_recvancillary & 15678 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15679 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15680 (ipp->ipp_fields & IPPF_DSTOPTS), 15681 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15682 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15683 addflag |= TCP_IPV6_RECVDSTOPTS; 15684 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15685 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15686 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15687 return (mp); 15688 } 15689 15690 if (optlen == 0) { 15691 /* Nothing to add */ 15692 return (mp); 15693 } 15694 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15695 if (mp1 == NULL) { 15696 /* 15697 * Defer sending ancillary data until the next TCP segment 15698 * arrives. 15699 */ 15700 return (mp); 15701 } 15702 mp1->b_cont = mp; 15703 mp = mp1; 15704 mp->b_wptr += sizeof (*todi) + optlen; 15705 mp->b_datap->db_type = M_PROTO; 15706 todi = (struct T_optdata_ind *)mp->b_rptr; 15707 todi->PRIM_type = T_OPTDATA_IND; 15708 todi->DATA_flag = 1; /* MORE data */ 15709 todi->OPT_length = optlen; 15710 todi->OPT_offset = sizeof (*todi); 15711 optptr = (uchar_t *)&todi[1]; 15712 /* 15713 * If app asked for pktinfo and the index has changed ... 15714 * Note that the local address never changes for the connection. 15715 */ 15716 if (addflag & TCP_IPV6_RECVPKTINFO) { 15717 struct in6_pktinfo *pkti; 15718 15719 toh = (struct T_opthdr *)optptr; 15720 toh->level = IPPROTO_IPV6; 15721 toh->name = IPV6_PKTINFO; 15722 toh->len = sizeof (*toh) + sizeof (*pkti); 15723 toh->status = 0; 15724 optptr += sizeof (*toh); 15725 pkti = (struct in6_pktinfo *)optptr; 15726 if (tcp->tcp_ipversion == IPV6_VERSION) 15727 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15728 else 15729 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15730 &pkti->ipi6_addr); 15731 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15732 optptr += sizeof (*pkti); 15733 ASSERT(OK_32PTR(optptr)); 15734 /* Save as "last" value */ 15735 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15736 } 15737 /* If app asked for hoplimit and it has changed ... */ 15738 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15739 toh = (struct T_opthdr *)optptr; 15740 toh->level = IPPROTO_IPV6; 15741 toh->name = IPV6_HOPLIMIT; 15742 toh->len = sizeof (*toh) + sizeof (uint_t); 15743 toh->status = 0; 15744 optptr += sizeof (*toh); 15745 *(uint_t *)optptr = ipp->ipp_hoplimit; 15746 optptr += sizeof (uint_t); 15747 ASSERT(OK_32PTR(optptr)); 15748 /* Save as "last" value */ 15749 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15750 } 15751 /* If app asked for tclass and it has changed ... */ 15752 if (addflag & TCP_IPV6_RECVTCLASS) { 15753 toh = (struct T_opthdr *)optptr; 15754 toh->level = IPPROTO_IPV6; 15755 toh->name = IPV6_TCLASS; 15756 toh->len = sizeof (*toh) + sizeof (uint_t); 15757 toh->status = 0; 15758 optptr += sizeof (*toh); 15759 *(uint_t *)optptr = ipp->ipp_tclass; 15760 optptr += sizeof (uint_t); 15761 ASSERT(OK_32PTR(optptr)); 15762 /* Save as "last" value */ 15763 tcp->tcp_recvtclass = ipp->ipp_tclass; 15764 } 15765 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15766 toh = (struct T_opthdr *)optptr; 15767 toh->level = IPPROTO_IPV6; 15768 toh->name = IPV6_HOPOPTS; 15769 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15770 tcp->tcp_label_len; 15771 toh->status = 0; 15772 optptr += sizeof (*toh); 15773 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15774 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15775 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15776 ASSERT(OK_32PTR(optptr)); 15777 /* Save as last value */ 15778 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15779 (ipp->ipp_fields & IPPF_HOPOPTS), 15780 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15781 } 15782 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15783 toh = (struct T_opthdr *)optptr; 15784 toh->level = IPPROTO_IPV6; 15785 toh->name = IPV6_RTHDRDSTOPTS; 15786 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15787 toh->status = 0; 15788 optptr += sizeof (*toh); 15789 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15790 optptr += ipp->ipp_rtdstoptslen; 15791 ASSERT(OK_32PTR(optptr)); 15792 /* Save as last value */ 15793 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15794 &tcp->tcp_rtdstoptslen, 15795 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15796 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15797 } 15798 if (addflag & TCP_IPV6_RECVRTHDR) { 15799 toh = (struct T_opthdr *)optptr; 15800 toh->level = IPPROTO_IPV6; 15801 toh->name = IPV6_RTHDR; 15802 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15803 toh->status = 0; 15804 optptr += sizeof (*toh); 15805 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15806 optptr += ipp->ipp_rthdrlen; 15807 ASSERT(OK_32PTR(optptr)); 15808 /* Save as last value */ 15809 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15810 (ipp->ipp_fields & IPPF_RTHDR), 15811 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15812 } 15813 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15814 toh = (struct T_opthdr *)optptr; 15815 toh->level = IPPROTO_IPV6; 15816 toh->name = IPV6_DSTOPTS; 15817 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15818 toh->status = 0; 15819 optptr += sizeof (*toh); 15820 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15821 optptr += ipp->ipp_dstoptslen; 15822 ASSERT(OK_32PTR(optptr)); 15823 /* Save as last value */ 15824 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15825 (ipp->ipp_fields & IPPF_DSTOPTS), 15826 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15827 } 15828 ASSERT(optptr == mp->b_wptr); 15829 return (mp); 15830 } 15831 15832 /* 15833 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15834 * messages. 15835 */ 15836 void 15837 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15838 { 15839 uchar_t *rptr = mp->b_rptr; 15840 queue_t *q = tcp->tcp_rq; 15841 struct T_error_ack *tea; 15842 15843 switch (mp->b_datap->db_type) { 15844 case M_PROTO: 15845 case M_PCPROTO: 15846 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15847 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15848 break; 15849 tea = (struct T_error_ack *)rptr; 15850 ASSERT(tea->PRIM_type != T_BIND_ACK); 15851 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15852 tea->ERROR_prim != T_BIND_REQ); 15853 switch (tea->PRIM_type) { 15854 case T_ERROR_ACK: 15855 if (tcp->tcp_debug) { 15856 (void) strlog(TCP_MOD_ID, 0, 1, 15857 SL_TRACE|SL_ERROR, 15858 "tcp_rput_other: case T_ERROR_ACK, " 15859 "ERROR_prim == %d", 15860 tea->ERROR_prim); 15861 } 15862 switch (tea->ERROR_prim) { 15863 case T_SVR4_OPTMGMT_REQ: 15864 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15865 /* T_OPTMGMT_REQ generated by TCP */ 15866 printf("T_SVR4_OPTMGMT_REQ failed " 15867 "%d/%d - dropped (cnt %d)\n", 15868 tea->TLI_error, tea->UNIX_error, 15869 tcp->tcp_drop_opt_ack_cnt); 15870 freemsg(mp); 15871 tcp->tcp_drop_opt_ack_cnt--; 15872 return; 15873 } 15874 break; 15875 } 15876 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15877 tcp->tcp_drop_opt_ack_cnt > 0) { 15878 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15879 "- dropped (cnt %d)\n", 15880 tea->TLI_error, tea->UNIX_error, 15881 tcp->tcp_drop_opt_ack_cnt); 15882 freemsg(mp); 15883 tcp->tcp_drop_opt_ack_cnt--; 15884 return; 15885 } 15886 break; 15887 case T_OPTMGMT_ACK: 15888 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15889 /* T_OPTMGMT_REQ generated by TCP */ 15890 freemsg(mp); 15891 tcp->tcp_drop_opt_ack_cnt--; 15892 return; 15893 } 15894 break; 15895 default: 15896 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15897 break; 15898 } 15899 break; 15900 case M_FLUSH: 15901 if (*rptr & FLUSHR) 15902 flushq(q, FLUSHDATA); 15903 break; 15904 default: 15905 /* M_CTL will be directly sent to tcp_icmp_error() */ 15906 ASSERT(DB_TYPE(mp) != M_CTL); 15907 break; 15908 } 15909 /* 15910 * Make sure we set this bit before sending the ACK for 15911 * bind. Otherwise accept could possibly run and free 15912 * this tcp struct. 15913 */ 15914 ASSERT(q != NULL); 15915 putnext(q, mp); 15916 } 15917 15918 /* ARGSUSED */ 15919 static void 15920 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15921 { 15922 conn_t *connp = (conn_t *)arg; 15923 tcp_t *tcp = connp->conn_tcp; 15924 queue_t *q = tcp->tcp_rq; 15925 uint_t thwin; 15926 tcp_stack_t *tcps = tcp->tcp_tcps; 15927 sodirect_t *sodp; 15928 boolean_t fc; 15929 15930 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15931 tcp->tcp_rsrv_mp = mp; 15932 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15933 15934 TCP_STAT(tcps, tcp_rsrv_calls); 15935 15936 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15937 return; 15938 } 15939 15940 if (tcp->tcp_fused) { 15941 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15942 15943 ASSERT(tcp->tcp_fused); 15944 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15945 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15946 ASSERT(!TCP_IS_DETACHED(tcp)); 15947 ASSERT(tcp->tcp_connp->conn_sqp == 15948 peer_tcp->tcp_connp->conn_sqp); 15949 15950 /* 15951 * Normally we would not get backenabled in synchronous 15952 * streams mode, but in case this happens, we need to plug 15953 * synchronous streams during our drain to prevent a race 15954 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15955 */ 15956 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15957 if (tcp->tcp_rcv_list != NULL) 15958 (void) tcp_rcv_drain(tcp); 15959 15960 if (peer_tcp > tcp) { 15961 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15962 mutex_enter(&tcp->tcp_non_sq_lock); 15963 } else { 15964 mutex_enter(&tcp->tcp_non_sq_lock); 15965 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15966 } 15967 15968 if (peer_tcp->tcp_flow_stopped && 15969 (TCP_UNSENT_BYTES(peer_tcp) <= 15970 peer_tcp->tcp_xmit_lowater)) { 15971 tcp_clrqfull(peer_tcp); 15972 } 15973 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15974 mutex_exit(&tcp->tcp_non_sq_lock); 15975 15976 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15977 TCP_STAT(tcps, tcp_fusion_backenabled); 15978 return; 15979 } 15980 15981 SOD_PTR_ENTER(tcp, sodp); 15982 if (sodp != NULL) { 15983 /* An sodirect connection */ 15984 if (SOD_QFULL(sodp)) { 15985 /* Flow-controlled, need another back-enable */ 15986 fc = B_TRUE; 15987 SOD_QSETBE(sodp); 15988 } else { 15989 /* Not flow-controlled */ 15990 fc = B_FALSE; 15991 } 15992 mutex_exit(sodp->sod_lockp); 15993 } else if (canputnext(q)) { 15994 /* STREAMS, not flow-controlled */ 15995 fc = B_FALSE; 15996 } else { 15997 /* STREAMS, flow-controlled */ 15998 fc = B_TRUE; 15999 } 16000 if (!fc) { 16001 /* Not flow-controlled, open rwnd */ 16002 tcp->tcp_rwnd = q->q_hiwat; 16003 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 16004 << tcp->tcp_rcv_ws; 16005 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 16006 /* 16007 * Send back a window update immediately if TCP is above 16008 * ESTABLISHED state and the increase of the rcv window 16009 * that the other side knows is at least 1 MSS after flow 16010 * control is lifted. 16011 */ 16012 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16013 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16014 tcp_xmit_ctl(NULL, tcp, 16015 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16016 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16017 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16018 } 16019 } 16020 } 16021 16022 /* 16023 * The read side service routine is called mostly when we get back-enabled as a 16024 * result of flow control relief. Since we don't actually queue anything in 16025 * TCP, we have no data to send out of here. What we do is clear the receive 16026 * window, and send out a window update. 16027 */ 16028 static void 16029 tcp_rsrv(queue_t *q) 16030 { 16031 conn_t *connp = Q_TO_CONN(q); 16032 tcp_t *tcp = connp->conn_tcp; 16033 mblk_t *mp; 16034 tcp_stack_t *tcps = tcp->tcp_tcps; 16035 16036 /* No code does a putq on the read side */ 16037 ASSERT(q->q_first == NULL); 16038 16039 /* Nothing to do for the default queue */ 16040 if (q == tcps->tcps_g_q) { 16041 return; 16042 } 16043 16044 /* 16045 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16046 * been run. So just return. 16047 */ 16048 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16049 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16050 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16051 return; 16052 } 16053 tcp->tcp_rsrv_mp = NULL; 16054 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16055 16056 CONN_INC_REF(connp); 16057 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16058 SQ_PROCESS, SQTAG_TCP_RSRV); 16059 } 16060 16061 /* 16062 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16063 * We do not allow the receive window to shrink. After setting rwnd, 16064 * set the flow control hiwat of the stream. 16065 * 16066 * This function is called in 2 cases: 16067 * 16068 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16069 * connection (passive open) and in tcp_rput_data() for active connect. 16070 * This is called after tcp_mss_set() when the desired MSS value is known. 16071 * This makes sure that our window size is a mutiple of the other side's 16072 * MSS. 16073 * 2) Handling SO_RCVBUF option. 16074 * 16075 * It is ASSUMED that the requested size is a multiple of the current MSS. 16076 * 16077 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16078 * user requests so. 16079 */ 16080 static int 16081 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16082 { 16083 uint32_t mss = tcp->tcp_mss; 16084 uint32_t old_max_rwnd; 16085 uint32_t max_transmittable_rwnd; 16086 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16087 tcp_stack_t *tcps = tcp->tcp_tcps; 16088 16089 if (tcp->tcp_fused) { 16090 size_t sth_hiwat; 16091 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16092 16093 ASSERT(peer_tcp != NULL); 16094 /* 16095 * Record the stream head's high water mark for 16096 * this endpoint; this is used for flow-control 16097 * purposes in tcp_fuse_output(). 16098 */ 16099 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16100 if (!tcp_detached) { 16101 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16102 sth_hiwat); 16103 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16104 conn_t *connp = tcp->tcp_connp; 16105 struct sock_proto_props sopp; 16106 16107 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16108 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16109 16110 (*connp->conn_upcalls->su_set_proto_props) 16111 (connp->conn_upper_handle, &sopp); 16112 } 16113 } 16114 16115 /* 16116 * In the fusion case, the maxpsz stream head value of 16117 * our peer is set according to its send buffer size 16118 * and our receive buffer size; since the latter may 16119 * have changed we need to update the peer's maxpsz. 16120 */ 16121 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16122 return (rwnd); 16123 } 16124 16125 if (tcp_detached) { 16126 old_max_rwnd = tcp->tcp_rwnd; 16127 } else { 16128 old_max_rwnd = tcp->tcp_recv_hiwater; 16129 } 16130 16131 /* 16132 * Insist on a receive window that is at least 16133 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16134 * funny TCP interactions of Nagle algorithm, SWS avoidance 16135 * and delayed acknowledgement. 16136 */ 16137 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16138 16139 /* 16140 * If window size info has already been exchanged, TCP should not 16141 * shrink the window. Shrinking window is doable if done carefully. 16142 * We may add that support later. But so far there is not a real 16143 * need to do that. 16144 */ 16145 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16146 /* MSS may have changed, do a round up again. */ 16147 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16148 } 16149 16150 /* 16151 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16152 * can be applied even before the window scale option is decided. 16153 */ 16154 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16155 if (rwnd > max_transmittable_rwnd) { 16156 rwnd = max_transmittable_rwnd - 16157 (max_transmittable_rwnd % mss); 16158 if (rwnd < mss) 16159 rwnd = max_transmittable_rwnd; 16160 /* 16161 * If we're over the limit we may have to back down tcp_rwnd. 16162 * The increment below won't work for us. So we set all three 16163 * here and the increment below will have no effect. 16164 */ 16165 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16166 } 16167 if (tcp->tcp_localnet) { 16168 tcp->tcp_rack_abs_max = 16169 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16170 } else { 16171 /* 16172 * For a remote host on a different subnet (through a router), 16173 * we ack every other packet to be conforming to RFC1122. 16174 * tcp_deferred_acks_max is default to 2. 16175 */ 16176 tcp->tcp_rack_abs_max = 16177 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16178 } 16179 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16180 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16181 else 16182 tcp->tcp_rack_cur_max = 0; 16183 /* 16184 * Increment the current rwnd by the amount the maximum grew (we 16185 * can not overwrite it since we might be in the middle of a 16186 * connection.) 16187 */ 16188 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16189 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16190 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16191 tcp->tcp_cwnd_max = rwnd; 16192 16193 if (tcp_detached) 16194 return (rwnd); 16195 /* 16196 * We set the maximum receive window into rq->q_hiwat if it is 16197 * a STREAMS socket. 16198 * This is not actually used for flow control. 16199 */ 16200 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16201 tcp->tcp_rq->q_hiwat = rwnd; 16202 tcp->tcp_recv_hiwater = rwnd; 16203 /* 16204 * Set the STREAM head high water mark. This doesn't have to be 16205 * here, since we are simply using default values, but we would 16206 * prefer to choose these values algorithmically, with a likely 16207 * relationship to rwnd. 16208 */ 16209 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16210 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16211 return (rwnd); 16212 } 16213 16214 /* 16215 * Return SNMP stuff in buffer in mpdata. 16216 */ 16217 mblk_t * 16218 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16219 { 16220 mblk_t *mpdata; 16221 mblk_t *mp_conn_ctl = NULL; 16222 mblk_t *mp_conn_tail; 16223 mblk_t *mp_attr_ctl = NULL; 16224 mblk_t *mp_attr_tail; 16225 mblk_t *mp6_conn_ctl = NULL; 16226 mblk_t *mp6_conn_tail; 16227 mblk_t *mp6_attr_ctl = NULL; 16228 mblk_t *mp6_attr_tail; 16229 struct opthdr *optp; 16230 mib2_tcpConnEntry_t tce; 16231 mib2_tcp6ConnEntry_t tce6; 16232 mib2_transportMLPEntry_t mlp; 16233 connf_t *connfp; 16234 int i; 16235 boolean_t ispriv; 16236 zoneid_t zoneid; 16237 int v4_conn_idx; 16238 int v6_conn_idx; 16239 conn_t *connp = Q_TO_CONN(q); 16240 tcp_stack_t *tcps; 16241 ip_stack_t *ipst; 16242 mblk_t *mp2ctl; 16243 16244 /* 16245 * make a copy of the original message 16246 */ 16247 mp2ctl = copymsg(mpctl); 16248 16249 if (mpctl == NULL || 16250 (mpdata = mpctl->b_cont) == NULL || 16251 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16252 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16253 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16254 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16255 freemsg(mp_conn_ctl); 16256 freemsg(mp_attr_ctl); 16257 freemsg(mp6_conn_ctl); 16258 freemsg(mp6_attr_ctl); 16259 freemsg(mpctl); 16260 freemsg(mp2ctl); 16261 return (NULL); 16262 } 16263 16264 ipst = connp->conn_netstack->netstack_ip; 16265 tcps = connp->conn_netstack->netstack_tcp; 16266 16267 /* build table of connections -- need count in fixed part */ 16268 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16269 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16270 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16271 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16272 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16273 16274 ispriv = 16275 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16276 zoneid = Q_TO_CONN(q)->conn_zoneid; 16277 16278 v4_conn_idx = v6_conn_idx = 0; 16279 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16280 16281 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16282 ipst = tcps->tcps_netstack->netstack_ip; 16283 16284 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16285 16286 connp = NULL; 16287 16288 while ((connp = 16289 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16290 tcp_t *tcp; 16291 boolean_t needattr; 16292 16293 if (connp->conn_zoneid != zoneid) 16294 continue; /* not in this zone */ 16295 16296 tcp = connp->conn_tcp; 16297 UPDATE_MIB(&tcps->tcps_mib, 16298 tcpHCInSegs, tcp->tcp_ibsegs); 16299 tcp->tcp_ibsegs = 0; 16300 UPDATE_MIB(&tcps->tcps_mib, 16301 tcpHCOutSegs, tcp->tcp_obsegs); 16302 tcp->tcp_obsegs = 0; 16303 16304 tce6.tcp6ConnState = tce.tcpConnState = 16305 tcp_snmp_state(tcp); 16306 if (tce.tcpConnState == MIB2_TCP_established || 16307 tce.tcpConnState == MIB2_TCP_closeWait) 16308 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16309 16310 needattr = B_FALSE; 16311 bzero(&mlp, sizeof (mlp)); 16312 if (connp->conn_mlp_type != mlptSingle) { 16313 if (connp->conn_mlp_type == mlptShared || 16314 connp->conn_mlp_type == mlptBoth) 16315 mlp.tme_flags |= MIB2_TMEF_SHARED; 16316 if (connp->conn_mlp_type == mlptPrivate || 16317 connp->conn_mlp_type == mlptBoth) 16318 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16319 needattr = B_TRUE; 16320 } 16321 if (connp->conn_peercred != NULL) { 16322 ts_label_t *tsl; 16323 16324 tsl = crgetlabel(connp->conn_peercred); 16325 mlp.tme_doi = label2doi(tsl); 16326 mlp.tme_label = *label2bslabel(tsl); 16327 needattr = B_TRUE; 16328 } 16329 16330 /* Create a message to report on IPv6 entries */ 16331 if (tcp->tcp_ipversion == IPV6_VERSION) { 16332 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16333 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16334 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16335 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16336 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16337 /* Don't want just anybody seeing these... */ 16338 if (ispriv) { 16339 tce6.tcp6ConnEntryInfo.ce_snxt = 16340 tcp->tcp_snxt; 16341 tce6.tcp6ConnEntryInfo.ce_suna = 16342 tcp->tcp_suna; 16343 tce6.tcp6ConnEntryInfo.ce_rnxt = 16344 tcp->tcp_rnxt; 16345 tce6.tcp6ConnEntryInfo.ce_rack = 16346 tcp->tcp_rack; 16347 } else { 16348 /* 16349 * Netstat, unfortunately, uses this to 16350 * get send/receive queue sizes. How to fix? 16351 * Why not compute the difference only? 16352 */ 16353 tce6.tcp6ConnEntryInfo.ce_snxt = 16354 tcp->tcp_snxt - tcp->tcp_suna; 16355 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16356 tce6.tcp6ConnEntryInfo.ce_rnxt = 16357 tcp->tcp_rnxt - tcp->tcp_rack; 16358 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16359 } 16360 16361 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16362 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16363 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16364 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16365 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16366 16367 tce6.tcp6ConnCreationProcess = 16368 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16369 tcp->tcp_cpid; 16370 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16371 16372 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16373 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16374 16375 mlp.tme_connidx = v6_conn_idx++; 16376 if (needattr) 16377 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16378 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16379 } 16380 /* 16381 * Create an IPv4 table entry for IPv4 entries and also 16382 * for IPv6 entries which are bound to in6addr_any 16383 * but don't have IPV6_V6ONLY set. 16384 * (i.e. anything an IPv4 peer could connect to) 16385 */ 16386 if (tcp->tcp_ipversion == IPV4_VERSION || 16387 (tcp->tcp_state <= TCPS_LISTEN && 16388 !tcp->tcp_connp->conn_ipv6_v6only && 16389 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16390 if (tcp->tcp_ipversion == IPV6_VERSION) { 16391 tce.tcpConnRemAddress = INADDR_ANY; 16392 tce.tcpConnLocalAddress = INADDR_ANY; 16393 } else { 16394 tce.tcpConnRemAddress = 16395 tcp->tcp_remote; 16396 tce.tcpConnLocalAddress = 16397 tcp->tcp_ip_src; 16398 } 16399 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16400 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16401 /* Don't want just anybody seeing these... */ 16402 if (ispriv) { 16403 tce.tcpConnEntryInfo.ce_snxt = 16404 tcp->tcp_snxt; 16405 tce.tcpConnEntryInfo.ce_suna = 16406 tcp->tcp_suna; 16407 tce.tcpConnEntryInfo.ce_rnxt = 16408 tcp->tcp_rnxt; 16409 tce.tcpConnEntryInfo.ce_rack = 16410 tcp->tcp_rack; 16411 } else { 16412 /* 16413 * Netstat, unfortunately, uses this to 16414 * get send/receive queue sizes. How 16415 * to fix? 16416 * Why not compute the difference only? 16417 */ 16418 tce.tcpConnEntryInfo.ce_snxt = 16419 tcp->tcp_snxt - tcp->tcp_suna; 16420 tce.tcpConnEntryInfo.ce_suna = 0; 16421 tce.tcpConnEntryInfo.ce_rnxt = 16422 tcp->tcp_rnxt - tcp->tcp_rack; 16423 tce.tcpConnEntryInfo.ce_rack = 0; 16424 } 16425 16426 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16427 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16428 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16429 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16430 tce.tcpConnEntryInfo.ce_state = 16431 tcp->tcp_state; 16432 16433 tce.tcpConnCreationProcess = 16434 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16435 tcp->tcp_cpid; 16436 tce.tcpConnCreationTime = tcp->tcp_open_time; 16437 16438 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16439 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16440 16441 mlp.tme_connidx = v4_conn_idx++; 16442 if (needattr) 16443 (void) snmp_append_data2( 16444 mp_attr_ctl->b_cont, 16445 &mp_attr_tail, (char *)&mlp, 16446 sizeof (mlp)); 16447 } 16448 } 16449 } 16450 16451 /* fixed length structure for IPv4 and IPv6 counters */ 16452 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16453 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16454 sizeof (mib2_tcp6ConnEntry_t)); 16455 /* synchronize 32- and 64-bit counters */ 16456 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16457 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16458 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16459 optp->level = MIB2_TCP; 16460 optp->name = 0; 16461 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16462 sizeof (tcps->tcps_mib)); 16463 optp->len = msgdsize(mpdata); 16464 qreply(q, mpctl); 16465 16466 /* table of connections... */ 16467 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16468 sizeof (struct T_optmgmt_ack)]; 16469 optp->level = MIB2_TCP; 16470 optp->name = MIB2_TCP_CONN; 16471 optp->len = msgdsize(mp_conn_ctl->b_cont); 16472 qreply(q, mp_conn_ctl); 16473 16474 /* table of MLP attributes... */ 16475 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16476 sizeof (struct T_optmgmt_ack)]; 16477 optp->level = MIB2_TCP; 16478 optp->name = EXPER_XPORT_MLP; 16479 optp->len = msgdsize(mp_attr_ctl->b_cont); 16480 if (optp->len == 0) 16481 freemsg(mp_attr_ctl); 16482 else 16483 qreply(q, mp_attr_ctl); 16484 16485 /* table of IPv6 connections... */ 16486 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16487 sizeof (struct T_optmgmt_ack)]; 16488 optp->level = MIB2_TCP6; 16489 optp->name = MIB2_TCP6_CONN; 16490 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16491 qreply(q, mp6_conn_ctl); 16492 16493 /* table of IPv6 MLP attributes... */ 16494 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16495 sizeof (struct T_optmgmt_ack)]; 16496 optp->level = MIB2_TCP6; 16497 optp->name = EXPER_XPORT_MLP; 16498 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16499 if (optp->len == 0) 16500 freemsg(mp6_attr_ctl); 16501 else 16502 qreply(q, mp6_attr_ctl); 16503 return (mp2ctl); 16504 } 16505 16506 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16507 /* ARGSUSED */ 16508 int 16509 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16510 { 16511 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16512 16513 switch (level) { 16514 case MIB2_TCP: 16515 switch (name) { 16516 case 13: 16517 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16518 return (0); 16519 /* TODO: delete entry defined by tce */ 16520 return (1); 16521 default: 16522 return (0); 16523 } 16524 default: 16525 return (1); 16526 } 16527 } 16528 16529 /* Translate TCP state to MIB2 TCP state. */ 16530 static int 16531 tcp_snmp_state(tcp_t *tcp) 16532 { 16533 if (tcp == NULL) 16534 return (0); 16535 16536 switch (tcp->tcp_state) { 16537 case TCPS_CLOSED: 16538 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16539 case TCPS_BOUND: 16540 return (MIB2_TCP_closed); 16541 case TCPS_LISTEN: 16542 return (MIB2_TCP_listen); 16543 case TCPS_SYN_SENT: 16544 return (MIB2_TCP_synSent); 16545 case TCPS_SYN_RCVD: 16546 return (MIB2_TCP_synReceived); 16547 case TCPS_ESTABLISHED: 16548 return (MIB2_TCP_established); 16549 case TCPS_CLOSE_WAIT: 16550 return (MIB2_TCP_closeWait); 16551 case TCPS_FIN_WAIT_1: 16552 return (MIB2_TCP_finWait1); 16553 case TCPS_CLOSING: 16554 return (MIB2_TCP_closing); 16555 case TCPS_LAST_ACK: 16556 return (MIB2_TCP_lastAck); 16557 case TCPS_FIN_WAIT_2: 16558 return (MIB2_TCP_finWait2); 16559 case TCPS_TIME_WAIT: 16560 return (MIB2_TCP_timeWait); 16561 default: 16562 return (0); 16563 } 16564 } 16565 16566 static char tcp_report_header[] = 16567 "TCP " MI_COL_HDRPAD_STR 16568 "zone dest snxt suna " 16569 "swnd rnxt rack rwnd rto mss w sw rw t " 16570 "recent [lport,fport] state"; 16571 16572 /* 16573 * TCP status report triggered via the Named Dispatch mechanism. 16574 */ 16575 /* ARGSUSED */ 16576 static void 16577 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16578 cred_t *cr) 16579 { 16580 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16581 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16582 char cflag; 16583 in6_addr_t v6dst; 16584 char buf[80]; 16585 uint_t print_len, buf_len; 16586 16587 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16588 if (buf_len <= 0) 16589 return; 16590 16591 if (hashval >= 0) 16592 (void) sprintf(hash, "%03d ", hashval); 16593 else 16594 hash[0] = '\0'; 16595 16596 /* 16597 * Note that we use the remote address in the tcp_b structure. 16598 * This means that it will print out the real destination address, 16599 * not the next hop's address if source routing is used. This 16600 * avoid the confusion on the output because user may not 16601 * know that source routing is used for a connection. 16602 */ 16603 if (tcp->tcp_ipversion == IPV4_VERSION) { 16604 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16605 } else { 16606 v6dst = tcp->tcp_remote_v6; 16607 } 16608 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16609 /* 16610 * the ispriv checks are so that normal users cannot determine 16611 * sequence number information using NDD. 16612 */ 16613 16614 if (TCP_IS_DETACHED(tcp)) 16615 cflag = '*'; 16616 else 16617 cflag = ' '; 16618 print_len = snprintf((char *)mp->b_wptr, buf_len, 16619 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16620 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16621 hash, 16622 (void *)tcp, 16623 tcp->tcp_connp->conn_zoneid, 16624 addrbuf, 16625 (ispriv) ? tcp->tcp_snxt : 0, 16626 (ispriv) ? tcp->tcp_suna : 0, 16627 tcp->tcp_swnd, 16628 (ispriv) ? tcp->tcp_rnxt : 0, 16629 (ispriv) ? tcp->tcp_rack : 0, 16630 tcp->tcp_rwnd, 16631 tcp->tcp_rto, 16632 tcp->tcp_mss, 16633 tcp->tcp_snd_ws_ok, 16634 tcp->tcp_snd_ws, 16635 tcp->tcp_rcv_ws, 16636 tcp->tcp_snd_ts_ok, 16637 tcp->tcp_ts_recent, 16638 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16639 if (print_len < buf_len) { 16640 ((mblk_t *)mp)->b_wptr += print_len; 16641 } else { 16642 ((mblk_t *)mp)->b_wptr += buf_len; 16643 } 16644 } 16645 16646 /* 16647 * TCP status report (for listeners only) triggered via the Named Dispatch 16648 * mechanism. 16649 */ 16650 /* ARGSUSED */ 16651 static void 16652 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16653 { 16654 char addrbuf[INET6_ADDRSTRLEN]; 16655 in6_addr_t v6dst; 16656 uint_t print_len, buf_len; 16657 16658 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16659 if (buf_len <= 0) 16660 return; 16661 16662 if (tcp->tcp_ipversion == IPV4_VERSION) { 16663 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16664 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16665 } else { 16666 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16667 addrbuf, sizeof (addrbuf)); 16668 } 16669 print_len = snprintf((char *)mp->b_wptr, buf_len, 16670 "%03d " 16671 MI_COL_PTRFMT_STR 16672 "%d %s %05u %08u %d/%d/%d%c\n", 16673 hashval, (void *)tcp, 16674 tcp->tcp_connp->conn_zoneid, 16675 addrbuf, 16676 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16677 tcp->tcp_conn_req_seqnum, 16678 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16679 tcp->tcp_conn_req_max, 16680 tcp->tcp_syn_defense ? '*' : ' '); 16681 if (print_len < buf_len) { 16682 ((mblk_t *)mp)->b_wptr += print_len; 16683 } else { 16684 ((mblk_t *)mp)->b_wptr += buf_len; 16685 } 16686 } 16687 16688 /* TCP status report triggered via the Named Dispatch mechanism. */ 16689 /* ARGSUSED */ 16690 static int 16691 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16692 { 16693 tcp_t *tcp; 16694 int i; 16695 conn_t *connp; 16696 connf_t *connfp; 16697 zoneid_t zoneid; 16698 tcp_stack_t *tcps; 16699 ip_stack_t *ipst; 16700 16701 zoneid = Q_TO_CONN(q)->conn_zoneid; 16702 tcps = Q_TO_TCP(q)->tcp_tcps; 16703 16704 /* 16705 * Because of the ndd constraint, at most we can have 64K buffer 16706 * to put in all TCP info. So to be more efficient, just 16707 * allocate a 64K buffer here, assuming we need that large buffer. 16708 * This may be a problem as any user can read tcp_status. Therefore 16709 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16710 * This should be OK as normal users should not do this too often. 16711 */ 16712 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16713 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16714 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16715 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16716 return (0); 16717 } 16718 } 16719 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16720 /* The following may work even if we cannot get a large buf. */ 16721 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16722 return (0); 16723 } 16724 16725 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16726 16727 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16728 16729 ipst = tcps->tcps_netstack->netstack_ip; 16730 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16731 16732 connp = NULL; 16733 16734 while ((connp = 16735 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16736 tcp = connp->conn_tcp; 16737 if (zoneid != GLOBAL_ZONEID && 16738 zoneid != connp->conn_zoneid) 16739 continue; 16740 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16741 cr); 16742 } 16743 16744 } 16745 16746 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16747 return (0); 16748 } 16749 16750 /* TCP status report triggered via the Named Dispatch mechanism. */ 16751 /* ARGSUSED */ 16752 static int 16753 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16754 { 16755 tf_t *tbf; 16756 tcp_t *tcp, *ltcp; 16757 int i; 16758 zoneid_t zoneid; 16759 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16760 16761 zoneid = Q_TO_CONN(q)->conn_zoneid; 16762 16763 /* Refer to comments in tcp_status_report(). */ 16764 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16765 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16766 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16767 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16768 return (0); 16769 } 16770 } 16771 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16772 /* The following may work even if we cannot get a large buf. */ 16773 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16774 return (0); 16775 } 16776 16777 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16778 16779 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16780 tbf = &tcps->tcps_bind_fanout[i]; 16781 mutex_enter(&tbf->tf_lock); 16782 for (ltcp = tbf->tf_tcp; ltcp != NULL; 16783 ltcp = ltcp->tcp_bind_hash) { 16784 for (tcp = ltcp; tcp != NULL; 16785 tcp = tcp->tcp_bind_hash_port) { 16786 if (zoneid != GLOBAL_ZONEID && 16787 zoneid != tcp->tcp_connp->conn_zoneid) 16788 continue; 16789 CONN_INC_REF(tcp->tcp_connp); 16790 tcp_report_item(mp->b_cont, tcp, i, 16791 Q_TO_TCP(q), cr); 16792 CONN_DEC_REF(tcp->tcp_connp); 16793 } 16794 } 16795 mutex_exit(&tbf->tf_lock); 16796 } 16797 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16798 return (0); 16799 } 16800 16801 /* TCP status report triggered via the Named Dispatch mechanism. */ 16802 /* ARGSUSED */ 16803 static int 16804 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16805 { 16806 connf_t *connfp; 16807 conn_t *connp; 16808 tcp_t *tcp; 16809 int i; 16810 zoneid_t zoneid; 16811 tcp_stack_t *tcps; 16812 ip_stack_t *ipst; 16813 16814 zoneid = Q_TO_CONN(q)->conn_zoneid; 16815 tcps = Q_TO_TCP(q)->tcp_tcps; 16816 16817 /* Refer to comments in tcp_status_report(). */ 16818 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16819 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16820 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16821 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16822 return (0); 16823 } 16824 } 16825 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16826 /* The following may work even if we cannot get a large buf. */ 16827 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16828 return (0); 16829 } 16830 16831 (void) mi_mpprintf(mp, 16832 " TCP " MI_COL_HDRPAD_STR 16833 "zone IP addr port seqnum backlog (q0/q/max)"); 16834 16835 ipst = tcps->tcps_netstack->netstack_ip; 16836 16837 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16838 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16839 connp = NULL; 16840 while ((connp = 16841 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16842 tcp = connp->conn_tcp; 16843 if (zoneid != GLOBAL_ZONEID && 16844 zoneid != connp->conn_zoneid) 16845 continue; 16846 tcp_report_listener(mp->b_cont, tcp, i); 16847 } 16848 } 16849 16850 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16851 return (0); 16852 } 16853 16854 /* TCP status report triggered via the Named Dispatch mechanism. */ 16855 /* ARGSUSED */ 16856 static int 16857 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16858 { 16859 connf_t *connfp; 16860 conn_t *connp; 16861 tcp_t *tcp; 16862 int i; 16863 zoneid_t zoneid; 16864 tcp_stack_t *tcps; 16865 ip_stack_t *ipst; 16866 16867 zoneid = Q_TO_CONN(q)->conn_zoneid; 16868 tcps = Q_TO_TCP(q)->tcp_tcps; 16869 ipst = tcps->tcps_netstack->netstack_ip; 16870 16871 /* Refer to comments in tcp_status_report(). */ 16872 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16873 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16874 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16875 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16876 return (0); 16877 } 16878 } 16879 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16880 /* The following may work even if we cannot get a large buf. */ 16881 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16882 return (0); 16883 } 16884 16885 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16886 ipst->ips_ipcl_conn_fanout_size); 16887 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16888 16889 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16890 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16891 connp = NULL; 16892 while ((connp = 16893 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16894 tcp = connp->conn_tcp; 16895 if (zoneid != GLOBAL_ZONEID && 16896 zoneid != connp->conn_zoneid) 16897 continue; 16898 tcp_report_item(mp->b_cont, tcp, i, 16899 Q_TO_TCP(q), cr); 16900 } 16901 } 16902 16903 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16904 return (0); 16905 } 16906 16907 /* TCP status report triggered via the Named Dispatch mechanism. */ 16908 /* ARGSUSED */ 16909 static int 16910 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16911 { 16912 tf_t *tf; 16913 tcp_t *tcp; 16914 int i; 16915 zoneid_t zoneid; 16916 tcp_stack_t *tcps; 16917 16918 zoneid = Q_TO_CONN(q)->conn_zoneid; 16919 tcps = Q_TO_TCP(q)->tcp_tcps; 16920 16921 /* Refer to comments in tcp_status_report(). */ 16922 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16923 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16924 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16925 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16926 return (0); 16927 } 16928 } 16929 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16930 /* The following may work even if we cannot get a large buf. */ 16931 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16932 return (0); 16933 } 16934 16935 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16936 16937 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16938 tf = &tcps->tcps_acceptor_fanout[i]; 16939 mutex_enter(&tf->tf_lock); 16940 for (tcp = tf->tf_tcp; tcp != NULL; 16941 tcp = tcp->tcp_acceptor_hash) { 16942 if (zoneid != GLOBAL_ZONEID && 16943 zoneid != tcp->tcp_connp->conn_zoneid) 16944 continue; 16945 tcp_report_item(mp->b_cont, tcp, i, 16946 Q_TO_TCP(q), cr); 16947 } 16948 mutex_exit(&tf->tf_lock); 16949 } 16950 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16951 return (0); 16952 } 16953 16954 /* 16955 * tcp_timer is the timer service routine. It handles the retransmission, 16956 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16957 * from the state of the tcp instance what kind of action needs to be done 16958 * at the time it is called. 16959 */ 16960 static void 16961 tcp_timer(void *arg) 16962 { 16963 mblk_t *mp; 16964 clock_t first_threshold; 16965 clock_t second_threshold; 16966 clock_t ms; 16967 uint32_t mss; 16968 conn_t *connp = (conn_t *)arg; 16969 tcp_t *tcp = connp->conn_tcp; 16970 tcp_stack_t *tcps = tcp->tcp_tcps; 16971 16972 tcp->tcp_timer_tid = 0; 16973 16974 if (tcp->tcp_fused) 16975 return; 16976 16977 first_threshold = tcp->tcp_first_timer_threshold; 16978 second_threshold = tcp->tcp_second_timer_threshold; 16979 switch (tcp->tcp_state) { 16980 case TCPS_IDLE: 16981 case TCPS_BOUND: 16982 case TCPS_LISTEN: 16983 return; 16984 case TCPS_SYN_RCVD: { 16985 tcp_t *listener = tcp->tcp_listener; 16986 16987 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16988 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16989 /* it's our first timeout */ 16990 tcp->tcp_syn_rcvd_timeout = 1; 16991 mutex_enter(&listener->tcp_eager_lock); 16992 listener->tcp_syn_rcvd_timeout++; 16993 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16994 /* 16995 * Make this eager available for drop if we 16996 * need to drop one to accomodate a new 16997 * incoming SYN request. 16998 */ 16999 MAKE_DROPPABLE(listener, tcp); 17000 } 17001 if (!listener->tcp_syn_defense && 17002 (listener->tcp_syn_rcvd_timeout > 17003 (tcps->tcps_conn_req_max_q0 >> 2)) && 17004 (tcps->tcps_conn_req_max_q0 > 200)) { 17005 /* We may be under attack. Put on a defense. */ 17006 listener->tcp_syn_defense = B_TRUE; 17007 cmn_err(CE_WARN, "High TCP connect timeout " 17008 "rate! System (port %d) may be under a " 17009 "SYN flood attack!", 17010 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17011 17012 listener->tcp_ip_addr_cache = kmem_zalloc( 17013 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17014 KM_NOSLEEP); 17015 } 17016 mutex_exit(&listener->tcp_eager_lock); 17017 } else if (listener != NULL) { 17018 mutex_enter(&listener->tcp_eager_lock); 17019 tcp->tcp_syn_rcvd_timeout++; 17020 if (tcp->tcp_syn_rcvd_timeout > 1 && 17021 !tcp->tcp_closemp_used) { 17022 /* 17023 * This is our second timeout. Put the tcp in 17024 * the list of droppable eagers to allow it to 17025 * be dropped, if needed. We don't check 17026 * whether tcp_dontdrop is set or not to 17027 * protect ourselve from a SYN attack where a 17028 * remote host can spoof itself as one of the 17029 * good IP source and continue to hold 17030 * resources too long. 17031 */ 17032 MAKE_DROPPABLE(listener, tcp); 17033 } 17034 mutex_exit(&listener->tcp_eager_lock); 17035 } 17036 } 17037 /* FALLTHRU */ 17038 case TCPS_SYN_SENT: 17039 first_threshold = tcp->tcp_first_ctimer_threshold; 17040 second_threshold = tcp->tcp_second_ctimer_threshold; 17041 break; 17042 case TCPS_ESTABLISHED: 17043 case TCPS_FIN_WAIT_1: 17044 case TCPS_CLOSING: 17045 case TCPS_CLOSE_WAIT: 17046 case TCPS_LAST_ACK: 17047 /* If we have data to rexmit */ 17048 if (tcp->tcp_suna != tcp->tcp_snxt) { 17049 clock_t time_to_wait; 17050 17051 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17052 if (!tcp->tcp_xmit_head) 17053 break; 17054 time_to_wait = lbolt - 17055 (clock_t)tcp->tcp_xmit_head->b_prev; 17056 time_to_wait = tcp->tcp_rto - 17057 TICK_TO_MSEC(time_to_wait); 17058 /* 17059 * If the timer fires too early, 1 clock tick earlier, 17060 * restart the timer. 17061 */ 17062 if (time_to_wait > msec_per_tick) { 17063 TCP_STAT(tcps, tcp_timer_fire_early); 17064 TCP_TIMER_RESTART(tcp, time_to_wait); 17065 return; 17066 } 17067 /* 17068 * When we probe zero windows, we force the swnd open. 17069 * If our peer acks with a closed window swnd will be 17070 * set to zero by tcp_rput(). As long as we are 17071 * receiving acks tcp_rput will 17072 * reset 'tcp_ms_we_have_waited' so as not to trip the 17073 * first and second interval actions. NOTE: the timer 17074 * interval is allowed to continue its exponential 17075 * backoff. 17076 */ 17077 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17078 if (tcp->tcp_debug) { 17079 (void) strlog(TCP_MOD_ID, 0, 1, 17080 SL_TRACE, "tcp_timer: zero win"); 17081 } 17082 } else { 17083 /* 17084 * After retransmission, we need to do 17085 * slow start. Set the ssthresh to one 17086 * half of current effective window and 17087 * cwnd to one MSS. Also reset 17088 * tcp_cwnd_cnt. 17089 * 17090 * Note that if tcp_ssthresh is reduced because 17091 * of ECN, do not reduce it again unless it is 17092 * already one window of data away (tcp_cwr 17093 * should then be cleared) or this is a 17094 * timeout for a retransmitted segment. 17095 */ 17096 uint32_t npkt; 17097 17098 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17099 npkt = ((tcp->tcp_timer_backoff ? 17100 tcp->tcp_cwnd_ssthresh : 17101 tcp->tcp_snxt - 17102 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17103 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17104 tcp->tcp_mss; 17105 } 17106 tcp->tcp_cwnd = tcp->tcp_mss; 17107 tcp->tcp_cwnd_cnt = 0; 17108 if (tcp->tcp_ecn_ok) { 17109 tcp->tcp_cwr = B_TRUE; 17110 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17111 tcp->tcp_ecn_cwr_sent = B_FALSE; 17112 } 17113 } 17114 break; 17115 } 17116 /* 17117 * We have something to send yet we cannot send. The 17118 * reason can be: 17119 * 17120 * 1. Zero send window: we need to do zero window probe. 17121 * 2. Zero cwnd: because of ECN, we need to "clock out 17122 * segments. 17123 * 3. SWS avoidance: receiver may have shrunk window, 17124 * reset our knowledge. 17125 * 17126 * Note that condition 2 can happen with either 1 or 17127 * 3. But 1 and 3 are exclusive. 17128 */ 17129 if (tcp->tcp_unsent != 0) { 17130 if (tcp->tcp_cwnd == 0) { 17131 /* 17132 * Set tcp_cwnd to 1 MSS so that a 17133 * new segment can be sent out. We 17134 * are "clocking out" new data when 17135 * the network is really congested. 17136 */ 17137 ASSERT(tcp->tcp_ecn_ok); 17138 tcp->tcp_cwnd = tcp->tcp_mss; 17139 } 17140 if (tcp->tcp_swnd == 0) { 17141 /* Extend window for zero window probe */ 17142 tcp->tcp_swnd++; 17143 tcp->tcp_zero_win_probe = B_TRUE; 17144 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17145 } else { 17146 /* 17147 * Handle timeout from sender SWS avoidance. 17148 * Reset our knowledge of the max send window 17149 * since the receiver might have reduced its 17150 * receive buffer. Avoid setting tcp_max_swnd 17151 * to one since that will essentially disable 17152 * the SWS checks. 17153 * 17154 * Note that since we don't have a SWS 17155 * state variable, if the timeout is set 17156 * for ECN but not for SWS, this 17157 * code will also be executed. This is 17158 * fine as tcp_max_swnd is updated 17159 * constantly and it will not affect 17160 * anything. 17161 */ 17162 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17163 } 17164 tcp_wput_data(tcp, NULL, B_FALSE); 17165 return; 17166 } 17167 /* Is there a FIN that needs to be to re retransmitted? */ 17168 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17169 !tcp->tcp_fin_acked) 17170 break; 17171 /* Nothing to do, return without restarting timer. */ 17172 TCP_STAT(tcps, tcp_timer_fire_miss); 17173 return; 17174 case TCPS_FIN_WAIT_2: 17175 /* 17176 * User closed the TCP endpoint and peer ACK'ed our FIN. 17177 * We waited some time for for peer's FIN, but it hasn't 17178 * arrived. We flush the connection now to avoid 17179 * case where the peer has rebooted. 17180 */ 17181 if (TCP_IS_DETACHED(tcp)) { 17182 (void) tcp_clean_death(tcp, 0, 23); 17183 } else { 17184 TCP_TIMER_RESTART(tcp, 17185 tcps->tcps_fin_wait_2_flush_interval); 17186 } 17187 return; 17188 case TCPS_TIME_WAIT: 17189 (void) tcp_clean_death(tcp, 0, 24); 17190 return; 17191 default: 17192 if (tcp->tcp_debug) { 17193 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17194 "tcp_timer: strange state (%d) %s", 17195 tcp->tcp_state, tcp_display(tcp, NULL, 17196 DISP_PORT_ONLY)); 17197 } 17198 return; 17199 } 17200 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17201 /* 17202 * For zero window probe, we need to send indefinitely, 17203 * unless we have not heard from the other side for some 17204 * time... 17205 */ 17206 if ((tcp->tcp_zero_win_probe == 0) || 17207 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17208 second_threshold)) { 17209 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17210 /* 17211 * If TCP is in SYN_RCVD state, send back a 17212 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17213 * should be zero in TCPS_SYN_RCVD state. 17214 */ 17215 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17216 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17217 "in SYN_RCVD", 17218 tcp, tcp->tcp_snxt, 17219 tcp->tcp_rnxt, TH_RST | TH_ACK); 17220 } 17221 (void) tcp_clean_death(tcp, 17222 tcp->tcp_client_errno ? 17223 tcp->tcp_client_errno : ETIMEDOUT, 25); 17224 return; 17225 } else { 17226 /* 17227 * Set tcp_ms_we_have_waited to second_threshold 17228 * so that in next timeout, we will do the above 17229 * check (lbolt - tcp_last_recv_time). This is 17230 * also to avoid overflow. 17231 * 17232 * We don't need to decrement tcp_timer_backoff 17233 * to avoid overflow because it will be decremented 17234 * later if new timeout value is greater than 17235 * tcp_rexmit_interval_max. In the case when 17236 * tcp_rexmit_interval_max is greater than 17237 * second_threshold, it means that we will wait 17238 * longer than second_threshold to send the next 17239 * window probe. 17240 */ 17241 tcp->tcp_ms_we_have_waited = second_threshold; 17242 } 17243 } else if (ms > first_threshold) { 17244 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17245 tcp->tcp_xmit_head != NULL) { 17246 tcp->tcp_xmit_head = 17247 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17248 } 17249 /* 17250 * We have been retransmitting for too long... The RTT 17251 * we calculated is probably incorrect. Reinitialize it. 17252 * Need to compensate for 0 tcp_rtt_sa. Reset 17253 * tcp_rtt_update so that we won't accidentally cache a 17254 * bad value. But only do this if this is not a zero 17255 * window probe. 17256 */ 17257 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17258 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17259 (tcp->tcp_rtt_sa >> 5); 17260 tcp->tcp_rtt_sa = 0; 17261 tcp_ip_notify(tcp); 17262 tcp->tcp_rtt_update = 0; 17263 } 17264 } 17265 tcp->tcp_timer_backoff++; 17266 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17267 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17268 tcps->tcps_rexmit_interval_min) { 17269 /* 17270 * This means the original RTO is tcp_rexmit_interval_min. 17271 * So we will use tcp_rexmit_interval_min as the RTO value 17272 * and do the backoff. 17273 */ 17274 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17275 } else { 17276 ms <<= tcp->tcp_timer_backoff; 17277 } 17278 if (ms > tcps->tcps_rexmit_interval_max) { 17279 ms = tcps->tcps_rexmit_interval_max; 17280 /* 17281 * ms is at max, decrement tcp_timer_backoff to avoid 17282 * overflow. 17283 */ 17284 tcp->tcp_timer_backoff--; 17285 } 17286 tcp->tcp_ms_we_have_waited += ms; 17287 if (tcp->tcp_zero_win_probe == 0) { 17288 tcp->tcp_rto = ms; 17289 } 17290 TCP_TIMER_RESTART(tcp, ms); 17291 /* 17292 * This is after a timeout and tcp_rto is backed off. Set 17293 * tcp_set_timer to 1 so that next time RTO is updated, we will 17294 * restart the timer with a correct value. 17295 */ 17296 tcp->tcp_set_timer = 1; 17297 mss = tcp->tcp_snxt - tcp->tcp_suna; 17298 if (mss > tcp->tcp_mss) 17299 mss = tcp->tcp_mss; 17300 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17301 mss = tcp->tcp_swnd; 17302 17303 if ((mp = tcp->tcp_xmit_head) != NULL) 17304 mp->b_prev = (mblk_t *)lbolt; 17305 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17306 B_TRUE); 17307 17308 /* 17309 * When slow start after retransmission begins, start with 17310 * this seq no. tcp_rexmit_max marks the end of special slow 17311 * start phase. tcp_snd_burst controls how many segments 17312 * can be sent because of an ack. 17313 */ 17314 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17315 tcp->tcp_snd_burst = TCP_CWND_SS; 17316 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17317 (tcp->tcp_unsent == 0)) { 17318 tcp->tcp_rexmit_max = tcp->tcp_fss; 17319 } else { 17320 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17321 } 17322 tcp->tcp_rexmit = B_TRUE; 17323 tcp->tcp_dupack_cnt = 0; 17324 17325 /* 17326 * Remove all rexmit SACK blk to start from fresh. 17327 */ 17328 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17329 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17330 tcp->tcp_num_notsack_blk = 0; 17331 tcp->tcp_cnt_notsack_list = 0; 17332 } 17333 if (mp == NULL) { 17334 return; 17335 } 17336 /* 17337 * Attach credentials to retransmitted initial SYNs. 17338 * In theory we should use the credentials from the connect() 17339 * call to ensure that getpeerucred() on the peer will be correct. 17340 * But we assume that SYN's are not dropped for loopback connections. 17341 */ 17342 if (tcp->tcp_state == TCPS_SYN_SENT) { 17343 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 17344 } 17345 17346 tcp->tcp_csuna = tcp->tcp_snxt; 17347 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17348 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17349 tcp_send_data(tcp, tcp->tcp_wq, mp); 17350 17351 } 17352 17353 static int 17354 tcp_do_unbind(conn_t *connp) 17355 { 17356 tcp_t *tcp = connp->conn_tcp; 17357 int error = 0; 17358 17359 switch (tcp->tcp_state) { 17360 case TCPS_BOUND: 17361 case TCPS_LISTEN: 17362 break; 17363 default: 17364 return (-TOUTSTATE); 17365 } 17366 17367 /* 17368 * Need to clean up all the eagers since after the unbind, segments 17369 * will no longer be delivered to this listener stream. 17370 */ 17371 mutex_enter(&tcp->tcp_eager_lock); 17372 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17373 tcp_eager_cleanup(tcp, 0); 17374 } 17375 mutex_exit(&tcp->tcp_eager_lock); 17376 17377 if (tcp->tcp_ipversion == IPV4_VERSION) { 17378 tcp->tcp_ipha->ipha_src = 0; 17379 } else { 17380 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17381 } 17382 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17383 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17384 tcp_bind_hash_remove(tcp); 17385 tcp->tcp_state = TCPS_IDLE; 17386 tcp->tcp_mdt = B_FALSE; 17387 17388 connp = tcp->tcp_connp; 17389 connp->conn_mdt_ok = B_FALSE; 17390 ipcl_hash_remove(connp); 17391 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17392 17393 return (error); 17394 } 17395 17396 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17397 static void 17398 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 17399 { 17400 int error = tcp_do_unbind(tcp->tcp_connp); 17401 17402 if (error > 0) { 17403 tcp_err_ack(tcp, mp, TSYSERR, error); 17404 } else if (error < 0) { 17405 tcp_err_ack(tcp, mp, -error, 0); 17406 } else { 17407 /* Send M_FLUSH according to TPI */ 17408 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17409 17410 mp = mi_tpi_ok_ack_alloc(mp); 17411 putnext(tcp->tcp_rq, mp); 17412 } 17413 } 17414 17415 /* 17416 * Don't let port fall into the privileged range. 17417 * Since the extra privileged ports can be arbitrary we also 17418 * ensure that we exclude those from consideration. 17419 * tcp_g_epriv_ports is not sorted thus we loop over it until 17420 * there are no changes. 17421 * 17422 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17423 * but instead the code relies on: 17424 * - the fact that the address of the array and its size never changes 17425 * - the atomic assignment of the elements of the array 17426 * 17427 * Returns 0 if there are no more ports available. 17428 * 17429 * TS note: skip multilevel ports. 17430 */ 17431 static in_port_t 17432 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17433 { 17434 int i; 17435 boolean_t restart = B_FALSE; 17436 tcp_stack_t *tcps = tcp->tcp_tcps; 17437 17438 if (random && tcp_random_anon_port != 0) { 17439 (void) random_get_pseudo_bytes((uint8_t *)&port, 17440 sizeof (in_port_t)); 17441 /* 17442 * Unless changed by a sys admin, the smallest anon port 17443 * is 32768 and the largest anon port is 65535. It is 17444 * very likely (50%) for the random port to be smaller 17445 * than the smallest anon port. When that happens, 17446 * add port % (anon port range) to the smallest anon 17447 * port to get the random port. It should fall into the 17448 * valid anon port range. 17449 */ 17450 if (port < tcps->tcps_smallest_anon_port) { 17451 port = tcps->tcps_smallest_anon_port + 17452 port % (tcps->tcps_largest_anon_port - 17453 tcps->tcps_smallest_anon_port); 17454 } 17455 } 17456 17457 retry: 17458 if (port < tcps->tcps_smallest_anon_port) 17459 port = (in_port_t)tcps->tcps_smallest_anon_port; 17460 17461 if (port > tcps->tcps_largest_anon_port) { 17462 if (restart) 17463 return (0); 17464 restart = B_TRUE; 17465 port = (in_port_t)tcps->tcps_smallest_anon_port; 17466 } 17467 17468 if (port < tcps->tcps_smallest_nonpriv_port) 17469 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17470 17471 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17472 if (port == tcps->tcps_g_epriv_ports[i]) { 17473 port++; 17474 /* 17475 * Make sure whether the port is in the 17476 * valid range. 17477 */ 17478 goto retry; 17479 } 17480 } 17481 if (is_system_labeled() && 17482 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17483 IPPROTO_TCP, B_TRUE)) != 0) { 17484 port = i; 17485 goto retry; 17486 } 17487 return (port); 17488 } 17489 17490 /* 17491 * Return the next anonymous port in the privileged port range for 17492 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17493 * downwards. This is the same behavior as documented in the userland 17494 * library call rresvport(3N). 17495 * 17496 * TS note: skip multilevel ports. 17497 */ 17498 static in_port_t 17499 tcp_get_next_priv_port(const tcp_t *tcp) 17500 { 17501 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17502 in_port_t nextport; 17503 boolean_t restart = B_FALSE; 17504 tcp_stack_t *tcps = tcp->tcp_tcps; 17505 retry: 17506 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17507 next_priv_port >= IPPORT_RESERVED) { 17508 next_priv_port = IPPORT_RESERVED - 1; 17509 if (restart) 17510 return (0); 17511 restart = B_TRUE; 17512 } 17513 if (is_system_labeled() && 17514 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17515 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17516 next_priv_port = nextport; 17517 goto retry; 17518 } 17519 return (next_priv_port--); 17520 } 17521 17522 /* The write side r/w procedure. */ 17523 17524 #if CCS_STATS 17525 struct { 17526 struct { 17527 int64_t count, bytes; 17528 } tot, hit; 17529 } wrw_stats; 17530 #endif 17531 17532 /* 17533 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17534 * messages. 17535 */ 17536 /* ARGSUSED */ 17537 static void 17538 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17539 { 17540 conn_t *connp = (conn_t *)arg; 17541 tcp_t *tcp = connp->conn_tcp; 17542 queue_t *q = tcp->tcp_wq; 17543 17544 ASSERT(DB_TYPE(mp) != M_IOCTL); 17545 /* 17546 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17547 * Once the close starts, streamhead and sockfs will not let any data 17548 * packets come down (close ensures that there are no threads using the 17549 * queue and no new threads will come down) but since qprocsoff() 17550 * hasn't happened yet, a M_FLUSH or some non data message might 17551 * get reflected back (in response to our own FLUSHRW) and get 17552 * processed after tcp_close() is done. The conn would still be valid 17553 * because a ref would have added but we need to check the state 17554 * before actually processing the packet. 17555 */ 17556 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17557 freemsg(mp); 17558 return; 17559 } 17560 17561 switch (DB_TYPE(mp)) { 17562 case M_IOCDATA: 17563 tcp_wput_iocdata(tcp, mp); 17564 break; 17565 case M_FLUSH: 17566 tcp_wput_flush(tcp, mp); 17567 break; 17568 default: 17569 CALL_IP_WPUT(connp, q, mp); 17570 break; 17571 } 17572 } 17573 17574 /* 17575 * The TCP fast path write put procedure. 17576 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17577 */ 17578 /* ARGSUSED */ 17579 void 17580 tcp_output(void *arg, mblk_t *mp, void *arg2) 17581 { 17582 int len; 17583 int hdrlen; 17584 int plen; 17585 mblk_t *mp1; 17586 uchar_t *rptr; 17587 uint32_t snxt; 17588 tcph_t *tcph; 17589 struct datab *db; 17590 uint32_t suna; 17591 uint32_t mss; 17592 ipaddr_t *dst; 17593 ipaddr_t *src; 17594 uint32_t sum; 17595 int usable; 17596 conn_t *connp = (conn_t *)arg; 17597 tcp_t *tcp = connp->conn_tcp; 17598 uint32_t msize; 17599 tcp_stack_t *tcps = tcp->tcp_tcps; 17600 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17601 17602 /* 17603 * Try and ASSERT the minimum possible references on the 17604 * conn early enough. Since we are executing on write side, 17605 * the connection is obviously not detached and that means 17606 * there is a ref each for TCP and IP. Since we are behind 17607 * the squeue, the minimum references needed are 3. If the 17608 * conn is in classifier hash list, there should be an 17609 * extra ref for that (we check both the possibilities). 17610 */ 17611 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17612 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17613 17614 ASSERT(DB_TYPE(mp) == M_DATA); 17615 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17616 17617 mutex_enter(&tcp->tcp_non_sq_lock); 17618 tcp->tcp_squeue_bytes -= msize; 17619 mutex_exit(&tcp->tcp_non_sq_lock); 17620 17621 /* Check to see if this connection wants to be re-fused. */ 17622 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17623 if (tcp->tcp_ipversion == IPV4_VERSION) { 17624 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17625 &tcp->tcp_saved_tcph); 17626 } else { 17627 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17628 &tcp->tcp_saved_tcph); 17629 } 17630 } 17631 /* Bypass tcp protocol for fused tcp loopback */ 17632 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17633 return; 17634 17635 mss = tcp->tcp_mss; 17636 if (tcp->tcp_xmit_zc_clean) 17637 mp = tcp_zcopy_backoff(tcp, mp, 0); 17638 17639 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17640 len = (int)(mp->b_wptr - mp->b_rptr); 17641 17642 /* 17643 * Criteria for fast path: 17644 * 17645 * 1. no unsent data 17646 * 2. single mblk in request 17647 * 3. connection established 17648 * 4. data in mblk 17649 * 5. len <= mss 17650 * 6. no tcp_valid bits 17651 */ 17652 if ((tcp->tcp_unsent != 0) || 17653 (tcp->tcp_cork) || 17654 (mp->b_cont != NULL) || 17655 (tcp->tcp_state != TCPS_ESTABLISHED) || 17656 (len == 0) || 17657 (len > mss) || 17658 (tcp->tcp_valid_bits != 0)) { 17659 tcp_wput_data(tcp, mp, B_FALSE); 17660 return; 17661 } 17662 17663 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17664 ASSERT(tcp->tcp_fin_sent == 0); 17665 17666 /* queue new packet onto retransmission queue */ 17667 if (tcp->tcp_xmit_head == NULL) { 17668 tcp->tcp_xmit_head = mp; 17669 } else { 17670 tcp->tcp_xmit_last->b_cont = mp; 17671 } 17672 tcp->tcp_xmit_last = mp; 17673 tcp->tcp_xmit_tail = mp; 17674 17675 /* find out how much we can send */ 17676 /* BEGIN CSTYLED */ 17677 /* 17678 * un-acked usable 17679 * |--------------|-----------------| 17680 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17681 */ 17682 /* END CSTYLED */ 17683 17684 /* start sending from tcp_snxt */ 17685 snxt = tcp->tcp_snxt; 17686 17687 /* 17688 * Check to see if this connection has been idled for some 17689 * time and no ACK is expected. If it is, we need to slow 17690 * start again to get back the connection's "self-clock" as 17691 * described in VJ's paper. 17692 * 17693 * Refer to the comment in tcp_mss_set() for the calculation 17694 * of tcp_cwnd after idle. 17695 */ 17696 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17697 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17698 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17699 } 17700 17701 usable = tcp->tcp_swnd; /* tcp window size */ 17702 if (usable > tcp->tcp_cwnd) 17703 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17704 usable -= snxt; /* subtract stuff already sent */ 17705 suna = tcp->tcp_suna; 17706 usable += suna; 17707 /* usable can be < 0 if the congestion window is smaller */ 17708 if (len > usable) { 17709 /* Can't send complete M_DATA in one shot */ 17710 goto slow; 17711 } 17712 17713 mutex_enter(&tcp->tcp_non_sq_lock); 17714 if (tcp->tcp_flow_stopped && 17715 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17716 tcp_clrqfull(tcp); 17717 } 17718 mutex_exit(&tcp->tcp_non_sq_lock); 17719 17720 /* 17721 * determine if anything to send (Nagle). 17722 * 17723 * 1. len < tcp_mss (i.e. small) 17724 * 2. unacknowledged data present 17725 * 3. len < nagle limit 17726 * 4. last packet sent < nagle limit (previous packet sent) 17727 */ 17728 if ((len < mss) && (snxt != suna) && 17729 (len < (int)tcp->tcp_naglim) && 17730 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17731 /* 17732 * This was the first unsent packet and normally 17733 * mss < xmit_hiwater so there is no need to worry 17734 * about flow control. The next packet will go 17735 * through the flow control check in tcp_wput_data(). 17736 */ 17737 /* leftover work from above */ 17738 tcp->tcp_unsent = len; 17739 tcp->tcp_xmit_tail_unsent = len; 17740 17741 return; 17742 } 17743 17744 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17745 17746 if (snxt == suna) { 17747 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17748 } 17749 17750 /* we have always sent something */ 17751 tcp->tcp_rack_cnt = 0; 17752 17753 tcp->tcp_snxt = snxt + len; 17754 tcp->tcp_rack = tcp->tcp_rnxt; 17755 17756 if ((mp1 = dupb(mp)) == 0) 17757 goto no_memory; 17758 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17759 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17760 17761 /* adjust tcp header information */ 17762 tcph = tcp->tcp_tcph; 17763 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17764 17765 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17766 sum = (sum >> 16) + (sum & 0xFFFF); 17767 U16_TO_ABE16(sum, tcph->th_sum); 17768 17769 U32_TO_ABE32(snxt, tcph->th_seq); 17770 17771 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17772 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17773 BUMP_LOCAL(tcp->tcp_obsegs); 17774 17775 /* Update the latest receive window size in TCP header. */ 17776 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17777 tcph->th_win); 17778 17779 tcp->tcp_last_sent_len = (ushort_t)len; 17780 17781 plen = len + tcp->tcp_hdr_len; 17782 17783 if (tcp->tcp_ipversion == IPV4_VERSION) { 17784 tcp->tcp_ipha->ipha_length = htons(plen); 17785 } else { 17786 tcp->tcp_ip6h->ip6_plen = htons(plen - 17787 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17788 } 17789 17790 /* see if we need to allocate a mblk for the headers */ 17791 hdrlen = tcp->tcp_hdr_len; 17792 rptr = mp1->b_rptr - hdrlen; 17793 db = mp1->b_datap; 17794 if ((db->db_ref != 2) || rptr < db->db_base || 17795 (!OK_32PTR(rptr))) { 17796 /* NOTE: we assume allocb returns an OK_32PTR */ 17797 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17798 tcps->tcps_wroff_xtra, BPRI_MED); 17799 if (!mp) { 17800 freemsg(mp1); 17801 goto no_memory; 17802 } 17803 mp->b_cont = mp1; 17804 mp1 = mp; 17805 /* Leave room for Link Level header */ 17806 /* hdrlen = tcp->tcp_hdr_len; */ 17807 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17808 mp1->b_wptr = &rptr[hdrlen]; 17809 } 17810 mp1->b_rptr = rptr; 17811 17812 /* Fill in the timestamp option. */ 17813 if (tcp->tcp_snd_ts_ok) { 17814 U32_TO_BE32((uint32_t)lbolt, 17815 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17816 U32_TO_BE32(tcp->tcp_ts_recent, 17817 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17818 } else { 17819 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17820 } 17821 17822 /* copy header into outgoing packet */ 17823 dst = (ipaddr_t *)rptr; 17824 src = (ipaddr_t *)tcp->tcp_iphc; 17825 dst[0] = src[0]; 17826 dst[1] = src[1]; 17827 dst[2] = src[2]; 17828 dst[3] = src[3]; 17829 dst[4] = src[4]; 17830 dst[5] = src[5]; 17831 dst[6] = src[6]; 17832 dst[7] = src[7]; 17833 dst[8] = src[8]; 17834 dst[9] = src[9]; 17835 if (hdrlen -= 40) { 17836 hdrlen >>= 2; 17837 dst += 10; 17838 src += 10; 17839 do { 17840 *dst++ = *src++; 17841 } while (--hdrlen); 17842 } 17843 17844 /* 17845 * Set the ECN info in the TCP header. Note that this 17846 * is not the template header. 17847 */ 17848 if (tcp->tcp_ecn_ok) { 17849 SET_ECT(tcp, rptr); 17850 17851 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17852 if (tcp->tcp_ecn_echo_on) 17853 tcph->th_flags[0] |= TH_ECE; 17854 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17855 tcph->th_flags[0] |= TH_CWR; 17856 tcp->tcp_ecn_cwr_sent = B_TRUE; 17857 } 17858 } 17859 17860 if (tcp->tcp_ip_forward_progress) { 17861 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17862 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17863 tcp->tcp_ip_forward_progress = B_FALSE; 17864 } 17865 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17866 return; 17867 17868 /* 17869 * If we ran out of memory, we pretend to have sent the packet 17870 * and that it was lost on the wire. 17871 */ 17872 no_memory: 17873 return; 17874 17875 slow: 17876 /* leftover work from above */ 17877 tcp->tcp_unsent = len; 17878 tcp->tcp_xmit_tail_unsent = len; 17879 tcp_wput_data(tcp, NULL, B_FALSE); 17880 } 17881 17882 /* ARGSUSED */ 17883 void 17884 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17885 { 17886 conn_t *connp = (conn_t *)arg; 17887 tcp_t *tcp = connp->conn_tcp; 17888 queue_t *q = tcp->tcp_rq; 17889 struct tcp_options *tcpopt; 17890 tcp_stack_t *tcps = tcp->tcp_tcps; 17891 17892 /* socket options */ 17893 uint_t sopp_flags; 17894 ssize_t sopp_rxhiwat; 17895 ssize_t sopp_maxblk; 17896 ushort_t sopp_wroff; 17897 ushort_t sopp_tail; 17898 ushort_t sopp_copyopt; 17899 17900 tcpopt = (struct tcp_options *)mp->b_rptr; 17901 17902 /* 17903 * Drop the eager's ref on the listener, that was placed when 17904 * this eager began life in tcp_conn_request. 17905 */ 17906 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17907 if (IPCL_IS_NONSTR(connp)) { 17908 /* Safe to free conn_ind message */ 17909 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17910 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17911 } 17912 17913 tcp->tcp_detached = B_FALSE; 17914 17915 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17916 /* 17917 * Someone blewoff the eager before we could finish 17918 * the accept. 17919 * 17920 * The only reason eager exists it because we put in 17921 * a ref on it when conn ind went up. We need to send 17922 * a disconnect indication up while the last reference 17923 * on the eager will be dropped by the squeue when we 17924 * return. 17925 */ 17926 ASSERT(tcp->tcp_listener == NULL); 17927 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17928 if (IPCL_IS_NONSTR(connp)) { 17929 ASSERT(tcp->tcp_issocket); 17930 (*connp->conn_upcalls->su_disconnected)( 17931 connp->conn_upper_handle, tcp->tcp_connid, 17932 ECONNREFUSED); 17933 freemsg(mp); 17934 } else { 17935 struct T_discon_ind *tdi; 17936 17937 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17938 /* 17939 * Let us reuse the incoming mblk to avoid 17940 * memory allocation failure problems. We know 17941 * that the size of the incoming mblk i.e. 17942 * stroptions is greater than sizeof 17943 * T_discon_ind. So the reallocb below can't 17944 * fail. 17945 */ 17946 freemsg(mp->b_cont); 17947 mp->b_cont = NULL; 17948 ASSERT(DB_REF(mp) == 1); 17949 mp = reallocb(mp, sizeof (struct T_discon_ind), 17950 B_FALSE); 17951 ASSERT(mp != NULL); 17952 DB_TYPE(mp) = M_PROTO; 17953 ((union T_primitives *)mp->b_rptr)->type = 17954 T_DISCON_IND; 17955 tdi = (struct T_discon_ind *)mp->b_rptr; 17956 if (tcp->tcp_issocket) { 17957 tdi->DISCON_reason = ECONNREFUSED; 17958 tdi->SEQ_number = 0; 17959 } else { 17960 tdi->DISCON_reason = ENOPROTOOPT; 17961 tdi->SEQ_number = 17962 tcp->tcp_conn_req_seqnum; 17963 } 17964 mp->b_wptr = mp->b_rptr + 17965 sizeof (struct T_discon_ind); 17966 putnext(q, mp); 17967 return; 17968 } 17969 } 17970 if (tcp->tcp_hard_binding) { 17971 tcp->tcp_hard_binding = B_FALSE; 17972 tcp->tcp_hard_bound = B_TRUE; 17973 } 17974 return; 17975 } 17976 17977 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17978 int boundif = tcpopt->to_boundif; 17979 uint_t len = sizeof (int); 17980 17981 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17982 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17983 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17984 } 17985 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17986 uint_t on = 1; 17987 uint_t len = sizeof (uint_t); 17988 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17989 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17990 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17991 } 17992 17993 /* 17994 * For a loopback connection with tcp_direct_sockfs on, note that 17995 * we don't have to protect tcp_rcv_list yet because synchronous 17996 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17997 * possibly race with us. 17998 */ 17999 18000 /* 18001 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 18002 * properly. This is the first time we know of the acceptor' 18003 * queue. So we do it here. 18004 * 18005 * XXX 18006 */ 18007 if (tcp->tcp_rcv_list == NULL) { 18008 /* 18009 * Recv queue is empty, tcp_rwnd should not have changed. 18010 * That means it should be equal to the listener's tcp_rwnd. 18011 */ 18012 if (!IPCL_IS_NONSTR(connp)) 18013 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18014 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 18015 } else { 18016 #ifdef DEBUG 18017 mblk_t *tmp; 18018 mblk_t *mp1; 18019 uint_t cnt = 0; 18020 18021 mp1 = tcp->tcp_rcv_list; 18022 while ((tmp = mp1) != NULL) { 18023 mp1 = tmp->b_next; 18024 cnt += msgdsize(tmp); 18025 } 18026 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18027 #endif 18028 /* There is some data, add them back to get the max. */ 18029 if (!IPCL_IS_NONSTR(connp)) 18030 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18031 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18032 } 18033 /* 18034 * This is the first time we run on the correct 18035 * queue after tcp_accept. So fix all the q parameters 18036 * here. 18037 */ 18038 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 18039 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18040 18041 /* 18042 * Record the stream head's high water mark for this endpoint; 18043 * this is used for flow-control purposes. 18044 */ 18045 sopp_rxhiwat = tcp->tcp_fused ? 18046 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 18047 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 18048 18049 /* 18050 * Determine what write offset value to use depending on SACK and 18051 * whether the endpoint is fused or not. 18052 */ 18053 if (tcp->tcp_fused) { 18054 ASSERT(tcp->tcp_loopback); 18055 ASSERT(tcp->tcp_loopback_peer != NULL); 18056 /* 18057 * For fused tcp loopback, set the stream head's write 18058 * offset value to zero since we won't be needing any room 18059 * for TCP/IP headers. This would also improve performance 18060 * since it would reduce the amount of work done by kmem. 18061 * Non-fused tcp loopback case is handled separately below. 18062 */ 18063 sopp_wroff = 0; 18064 /* 18065 * Update the peer's transmit parameters according to 18066 * our recently calculated high water mark value. 18067 */ 18068 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18069 } else if (tcp->tcp_snd_sack_ok) { 18070 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18071 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18072 } else { 18073 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18074 tcps->tcps_wroff_xtra); 18075 } 18076 18077 /* 18078 * If this is endpoint is handling SSL, then reserve extra 18079 * offset and space at the end. 18080 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18081 * overriding the previous setting. The extra cost of signing and 18082 * encrypting multiple MSS-size records (12 of them with Ethernet), 18083 * instead of a single contiguous one by the stream head 18084 * largely outweighs the statistical reduction of ACKs, when 18085 * applicable. The peer will also save on decryption and verification 18086 * costs. 18087 */ 18088 if (tcp->tcp_kssl_ctx != NULL) { 18089 sopp_wroff += SSL3_WROFFSET; 18090 18091 sopp_flags |= SOCKOPT_TAIL; 18092 sopp_tail = SSL3_MAX_TAIL_LEN; 18093 18094 sopp_flags |= SOCKOPT_ZCOPY; 18095 sopp_copyopt = ZCVMUNSAFE; 18096 18097 sopp_maxblk = SSL3_MAX_RECORD_LEN; 18098 } 18099 18100 /* Send the options up */ 18101 if (IPCL_IS_NONSTR(connp)) { 18102 struct sock_proto_props sopp; 18103 18104 sopp.sopp_flags = sopp_flags; 18105 sopp.sopp_wroff = sopp_wroff; 18106 sopp.sopp_maxblk = sopp_maxblk; 18107 sopp.sopp_rxhiwat = sopp_rxhiwat; 18108 if (sopp_flags & SOCKOPT_TAIL) { 18109 ASSERT(tcp->tcp_kssl_ctx != NULL); 18110 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 18111 sopp.sopp_tail = sopp_tail; 18112 sopp.sopp_zcopyflag = sopp_copyopt; 18113 } 18114 (*connp->conn_upcalls->su_set_proto_props) 18115 (connp->conn_upper_handle, &sopp); 18116 } else { 18117 struct stroptions *stropt; 18118 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18119 if (stropt_mp == NULL) { 18120 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 18121 return; 18122 } 18123 DB_TYPE(stropt_mp) = M_SETOPTS; 18124 stropt = (struct stroptions *)stropt_mp->b_rptr; 18125 stropt_mp->b_wptr += sizeof (struct stroptions); 18126 stropt = (struct stroptions *)stropt_mp->b_rptr; 18127 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 18128 stropt->so_hiwat = sopp_rxhiwat; 18129 stropt->so_wroff = sopp_wroff; 18130 stropt->so_maxblk = sopp_maxblk; 18131 18132 if (sopp_flags & SOCKOPT_TAIL) { 18133 ASSERT(tcp->tcp_kssl_ctx != NULL); 18134 18135 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 18136 stropt->so_tail = sopp_tail; 18137 stropt->so_copyopt = sopp_copyopt; 18138 } 18139 18140 /* Send the options up */ 18141 putnext(q, stropt_mp); 18142 } 18143 18144 freemsg(mp); 18145 /* 18146 * Pass up any data and/or a fin that has been received. 18147 * 18148 * Adjust receive window in case it had decreased 18149 * (because there is data <=> tcp_rcv_list != NULL) 18150 * while the connection was detached. Note that 18151 * in case the eager was flow-controlled, w/o this 18152 * code, the rwnd may never open up again! 18153 */ 18154 if (tcp->tcp_rcv_list != NULL) { 18155 if (IPCL_IS_NONSTR(connp)) { 18156 mblk_t *mp; 18157 int space_left; 18158 int error; 18159 boolean_t push = B_TRUE; 18160 18161 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 18162 (connp->conn_upper_handle, NULL, 0, 0, &error, 18163 &push) >= 0) { 18164 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 18165 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18166 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18167 tcp_xmit_ctl(NULL, 18168 tcp, (tcp->tcp_swnd == 0) ? 18169 tcp->tcp_suna : tcp->tcp_snxt, 18170 tcp->tcp_rnxt, TH_ACK); 18171 } 18172 } 18173 while ((mp = tcp->tcp_rcv_list) != NULL) { 18174 push = B_TRUE; 18175 tcp->tcp_rcv_list = mp->b_next; 18176 mp->b_next = NULL; 18177 space_left = (*connp->conn_upcalls->su_recv) 18178 (connp->conn_upper_handle, mp, msgdsize(mp), 18179 0, &error, &push); 18180 if (space_left < 0) { 18181 /* 18182 * We should never be in middle of a 18183 * fallback, the squeue guarantees that. 18184 */ 18185 ASSERT(error != EOPNOTSUPP); 18186 } 18187 } 18188 tcp->tcp_rcv_last_head = NULL; 18189 tcp->tcp_rcv_last_tail = NULL; 18190 tcp->tcp_rcv_cnt = 0; 18191 } else { 18192 /* We drain directly in case of fused tcp loopback */ 18193 sodirect_t *sodp; 18194 18195 if (!tcp->tcp_fused && canputnext(q)) { 18196 tcp->tcp_rwnd = q->q_hiwat; 18197 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18198 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18199 tcp_xmit_ctl(NULL, 18200 tcp, (tcp->tcp_swnd == 0) ? 18201 tcp->tcp_suna : tcp->tcp_snxt, 18202 tcp->tcp_rnxt, TH_ACK); 18203 } 18204 } 18205 18206 SOD_PTR_ENTER(tcp, sodp); 18207 if (sodp != NULL) { 18208 /* Sodirect, move from rcv_list */ 18209 ASSERT(!tcp->tcp_fused); 18210 while ((mp = tcp->tcp_rcv_list) != NULL) { 18211 tcp->tcp_rcv_list = mp->b_next; 18212 mp->b_next = NULL; 18213 (void) tcp_rcv_sod_enqueue(tcp, sodp, 18214 mp, msgdsize(mp)); 18215 } 18216 tcp->tcp_rcv_last_head = NULL; 18217 tcp->tcp_rcv_last_tail = NULL; 18218 tcp->tcp_rcv_cnt = 0; 18219 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18220 /* sod_wakeup() did the mutex_exit() */ 18221 } else { 18222 /* Not sodirect, drain */ 18223 (void) tcp_rcv_drain(tcp); 18224 } 18225 } 18226 18227 /* 18228 * For fused tcp loopback, back-enable peer endpoint 18229 * if it's currently flow-controlled. 18230 */ 18231 if (tcp->tcp_fused) { 18232 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18233 18234 ASSERT(peer_tcp != NULL); 18235 ASSERT(peer_tcp->tcp_fused); 18236 /* 18237 * In order to change the peer's tcp_flow_stopped, 18238 * we need to take locks for both end points. The 18239 * highest address is taken first. 18240 */ 18241 if (peer_tcp > tcp) { 18242 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18243 mutex_enter(&tcp->tcp_non_sq_lock); 18244 } else { 18245 mutex_enter(&tcp->tcp_non_sq_lock); 18246 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18247 } 18248 if (peer_tcp->tcp_flow_stopped) { 18249 tcp_clrqfull(peer_tcp); 18250 TCP_STAT(tcps, tcp_fusion_backenabled); 18251 } 18252 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18253 mutex_exit(&tcp->tcp_non_sq_lock); 18254 } 18255 } 18256 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18257 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18258 tcp->tcp_ordrel_done = B_TRUE; 18259 if (IPCL_IS_NONSTR(connp)) { 18260 ASSERT(tcp->tcp_ordrel_mp == NULL); 18261 (*connp->conn_upcalls->su_opctl)( 18262 connp->conn_upper_handle, 18263 SOCK_OPCTL_SHUT_RECV, 0); 18264 } else { 18265 mp = tcp->tcp_ordrel_mp; 18266 tcp->tcp_ordrel_mp = NULL; 18267 putnext(q, mp); 18268 } 18269 } 18270 if (tcp->tcp_hard_binding) { 18271 tcp->tcp_hard_binding = B_FALSE; 18272 tcp->tcp_hard_bound = B_TRUE; 18273 } 18274 18275 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 18276 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 18277 tcp->tcp_loopback_peer != NULL && 18278 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 18279 tcp_fuse_syncstr_enable_pair(tcp); 18280 } 18281 18282 if (tcp->tcp_ka_enabled) { 18283 tcp->tcp_ka_last_intrvl = 0; 18284 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18285 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18286 } 18287 18288 /* 18289 * At this point, eager is fully established and will 18290 * have the following references - 18291 * 18292 * 2 references for connection to exist (1 for TCP and 1 for IP). 18293 * 1 reference for the squeue which will be dropped by the squeue as 18294 * soon as this function returns. 18295 * There will be 1 additonal reference for being in classifier 18296 * hash list provided something bad hasn't happened. 18297 */ 18298 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18299 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18300 } 18301 18302 /* 18303 * The function called through squeue to get behind listener's perimeter to 18304 * send a deffered conn_ind. 18305 */ 18306 /* ARGSUSED */ 18307 void 18308 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18309 { 18310 conn_t *connp = (conn_t *)arg; 18311 tcp_t *listener = connp->conn_tcp; 18312 struct T_conn_ind *conn_ind; 18313 tcp_t *tcp; 18314 18315 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18316 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18317 conn_ind->OPT_length); 18318 18319 if (listener->tcp_state == TCPS_CLOSED || 18320 TCP_IS_DETACHED(listener)) { 18321 /* 18322 * If listener has closed, it would have caused a 18323 * a cleanup/blowoff to happen for the eager. 18324 * 18325 * We need to drop the ref on eager that was put 18326 * tcp_rput_data() before trying to send the conn_ind 18327 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18328 * and tcp_wput_accept() is sending this deferred conn_ind but 18329 * listener is closed so we drop the ref. 18330 */ 18331 CONN_DEC_REF(tcp->tcp_connp); 18332 freemsg(mp); 18333 return; 18334 } 18335 18336 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 18337 } 18338 18339 /* ARGSUSED */ 18340 static int 18341 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 18342 { 18343 tcp_t *listener, *eager; 18344 mblk_t *opt_mp; 18345 struct tcp_options *tcpopt; 18346 18347 listener = lconnp->conn_tcp; 18348 ASSERT(listener->tcp_state == TCPS_LISTEN); 18349 eager = econnp->conn_tcp; 18350 ASSERT(eager->tcp_listener != NULL); 18351 18352 ASSERT(eager->tcp_rq != NULL); 18353 18354 /* If tcp_fused and sodirect enabled disable it */ 18355 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18356 /* Fused, disable sodirect */ 18357 mutex_enter(eager->tcp_sodirect->sod_lockp); 18358 SOD_DISABLE(eager->tcp_sodirect); 18359 mutex_exit(eager->tcp_sodirect->sod_lockp); 18360 eager->tcp_sodirect = NULL; 18361 } 18362 18363 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 18364 if (opt_mp == NULL) { 18365 return (-TPROTO); 18366 } 18367 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 18368 eager->tcp_issocket = B_TRUE; 18369 18370 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18371 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18372 ASSERT(econnp->conn_netstack == 18373 listener->tcp_connp->conn_netstack); 18374 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18375 18376 /* Put the ref for IP */ 18377 CONN_INC_REF(econnp); 18378 18379 /* 18380 * We should have minimum of 3 references on the conn 18381 * at this point. One each for TCP and IP and one for 18382 * the T_conn_ind that was sent up when the 3-way handshake 18383 * completed. In the normal case we would also have another 18384 * reference (making a total of 4) for the conn being in the 18385 * classifier hash list. However the eager could have received 18386 * an RST subsequently and tcp_closei_local could have removed 18387 * the eager from the classifier hash list, hence we can't 18388 * assert that reference. 18389 */ 18390 ASSERT(econnp->conn_ref >= 3); 18391 18392 opt_mp->b_datap->db_type = M_SETOPTS; 18393 opt_mp->b_wptr += sizeof (struct tcp_options); 18394 18395 /* 18396 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18397 * from listener to acceptor. 18398 */ 18399 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 18400 tcpopt->to_flags = 0; 18401 18402 if (listener->tcp_bound_if != 0) { 18403 tcpopt->to_flags |= TCPOPT_BOUNDIF; 18404 tcpopt->to_boundif = listener->tcp_bound_if; 18405 } 18406 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18407 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 18408 } 18409 18410 mutex_enter(&listener->tcp_eager_lock); 18411 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18412 18413 tcp_t *tail; 18414 tcp_t *tcp; 18415 mblk_t *mp1; 18416 18417 tcp = listener->tcp_eager_prev_q0; 18418 /* 18419 * listener->tcp_eager_prev_q0 points to the TAIL of the 18420 * deferred T_conn_ind queue. We need to get to the head 18421 * of the queue in order to send up T_conn_ind the same 18422 * order as how the 3WHS is completed. 18423 */ 18424 while (tcp != listener) { 18425 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18426 !tcp->tcp_kssl_pending) 18427 break; 18428 else 18429 tcp = tcp->tcp_eager_prev_q0; 18430 } 18431 /* None of the pending eagers can be sent up now */ 18432 if (tcp == listener) 18433 goto no_more_eagers; 18434 18435 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18436 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18437 /* Move from q0 to q */ 18438 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18439 listener->tcp_conn_req_cnt_q0--; 18440 listener->tcp_conn_req_cnt_q++; 18441 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18442 tcp->tcp_eager_prev_q0; 18443 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18444 tcp->tcp_eager_next_q0; 18445 tcp->tcp_eager_prev_q0 = NULL; 18446 tcp->tcp_eager_next_q0 = NULL; 18447 tcp->tcp_conn_def_q0 = B_FALSE; 18448 18449 /* Make sure the tcp isn't in the list of droppables */ 18450 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18451 tcp->tcp_eager_prev_drop_q0 == NULL); 18452 18453 /* 18454 * Insert at end of the queue because sockfs sends 18455 * down T_CONN_RES in chronological order. Leaving 18456 * the older conn indications at front of the queue 18457 * helps reducing search time. 18458 */ 18459 tail = listener->tcp_eager_last_q; 18460 if (tail != NULL) { 18461 tail->tcp_eager_next_q = tcp; 18462 } else { 18463 listener->tcp_eager_next_q = tcp; 18464 } 18465 listener->tcp_eager_last_q = tcp; 18466 tcp->tcp_eager_next_q = NULL; 18467 18468 /* Need to get inside the listener perimeter */ 18469 CONN_INC_REF(listener->tcp_connp); 18470 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18471 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18472 SQTAG_TCP_SEND_PENDING); 18473 } 18474 no_more_eagers: 18475 tcp_eager_unlink(eager); 18476 mutex_exit(&listener->tcp_eager_lock); 18477 18478 /* 18479 * At this point, the eager is detached from the listener 18480 * but we still have an extra refs on eager (apart from the 18481 * usual tcp references). The ref was placed in tcp_rput_data 18482 * before sending the conn_ind in tcp_send_conn_ind. 18483 * The ref will be dropped in tcp_accept_finish(). 18484 */ 18485 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18486 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18487 return (0); 18488 } 18489 18490 int 18491 tcp_accept(sock_lower_handle_t lproto_handle, 18492 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18493 cred_t *cr) 18494 { 18495 conn_t *lconnp, *econnp; 18496 tcp_t *listener, *eager; 18497 tcp_stack_t *tcps; 18498 18499 lconnp = (conn_t *)lproto_handle; 18500 listener = lconnp->conn_tcp; 18501 ASSERT(listener->tcp_state == TCPS_LISTEN); 18502 econnp = (conn_t *)eproto_handle; 18503 eager = econnp->conn_tcp; 18504 ASSERT(eager->tcp_listener != NULL); 18505 tcps = eager->tcp_tcps; 18506 18507 /* 18508 * It is OK to manipulate these fields outside the eager's squeue 18509 * because they will not start being used until tcp_accept_finish 18510 * has been called. 18511 */ 18512 ASSERT(lconnp->conn_upper_handle != NULL); 18513 ASSERT(econnp->conn_upper_handle == NULL); 18514 econnp->conn_upper_handle = sock_handle; 18515 econnp->conn_upcalls = lconnp->conn_upcalls; 18516 ASSERT(IPCL_IS_NONSTR(econnp)); 18517 /* 18518 * Create helper stream if it is a non-TPI TCP connection. 18519 */ 18520 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18521 ip1dbg(("tcp_accept: create of IP helper stream" 18522 " failed\n")); 18523 return (EPROTO); 18524 } 18525 eager->tcp_rq = econnp->conn_rq; 18526 eager->tcp_wq = econnp->conn_wq; 18527 18528 ASSERT(eager->tcp_rq != NULL); 18529 18530 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18531 return (tcp_accept_common(lconnp, econnp, cr)); 18532 } 18533 18534 18535 /* 18536 * This is the STREAMS entry point for T_CONN_RES coming down on 18537 * Acceptor STREAM when sockfs listener does accept processing. 18538 * Read the block comment on top of tcp_conn_request(). 18539 */ 18540 void 18541 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18542 { 18543 queue_t *rq = RD(q); 18544 struct T_conn_res *conn_res; 18545 tcp_t *eager; 18546 tcp_t *listener; 18547 struct T_ok_ack *ok; 18548 t_scalar_t PRIM_type; 18549 conn_t *econnp; 18550 cred_t *cr; 18551 18552 ASSERT(DB_TYPE(mp) == M_PROTO); 18553 18554 /* 18555 * All Solaris components should pass a db_credp 18556 * for this TPI message, hence we ASSERT. 18557 * But in case there is some other M_PROTO that looks 18558 * like a TPI message sent by some other kernel 18559 * component, we check and return an error. 18560 */ 18561 cr = msg_getcred(mp, NULL); 18562 ASSERT(cr != NULL); 18563 if (cr == NULL) { 18564 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 18565 if (mp != NULL) 18566 putnext(rq, mp); 18567 return; 18568 } 18569 conn_res = (struct T_conn_res *)mp->b_rptr; 18570 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18571 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18572 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18573 if (mp != NULL) 18574 putnext(rq, mp); 18575 return; 18576 } 18577 switch (conn_res->PRIM_type) { 18578 case O_T_CONN_RES: 18579 case T_CONN_RES: 18580 /* 18581 * We pass up an err ack if allocb fails. This will 18582 * cause sockfs to issue a T_DISCON_REQ which will cause 18583 * tcp_eager_blowoff to be called. sockfs will then call 18584 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18585 * we need to do the allocb up here because we have to 18586 * make sure rq->q_qinfo->qi_qclose still points to the 18587 * correct function (tcpclose_accept) in case allocb 18588 * fails. 18589 */ 18590 bcopy(mp->b_rptr + conn_res->OPT_offset, 18591 &eager, conn_res->OPT_length); 18592 PRIM_type = conn_res->PRIM_type; 18593 mp->b_datap->db_type = M_PCPROTO; 18594 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18595 ok = (struct T_ok_ack *)mp->b_rptr; 18596 ok->PRIM_type = T_OK_ACK; 18597 ok->CORRECT_prim = PRIM_type; 18598 econnp = eager->tcp_connp; 18599 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18600 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18601 eager->tcp_rq = rq; 18602 eager->tcp_wq = q; 18603 rq->q_ptr = econnp; 18604 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18605 q->q_ptr = econnp; 18606 q->q_qinfo = &tcp_winit; 18607 listener = eager->tcp_listener; 18608 18609 /* 18610 * TCP is _D_SODIRECT and sockfs is directly above so 18611 * save shared sodirect_t pointer (if any). 18612 */ 18613 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18614 if (tcp_accept_common(listener->tcp_connp, 18615 econnp, cr) < 0) { 18616 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18617 if (mp != NULL) 18618 putnext(rq, mp); 18619 return; 18620 } 18621 18622 /* 18623 * Send the new local address also up to sockfs. There 18624 * should already be enough space in the mp that came 18625 * down from soaccept(). 18626 */ 18627 if (eager->tcp_family == AF_INET) { 18628 sin_t *sin; 18629 18630 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18631 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18632 sin = (sin_t *)mp->b_wptr; 18633 mp->b_wptr += sizeof (sin_t); 18634 sin->sin_family = AF_INET; 18635 sin->sin_port = eager->tcp_lport; 18636 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18637 } else { 18638 sin6_t *sin6; 18639 18640 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18641 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18642 sin6 = (sin6_t *)mp->b_wptr; 18643 mp->b_wptr += sizeof (sin6_t); 18644 sin6->sin6_family = AF_INET6; 18645 sin6->sin6_port = eager->tcp_lport; 18646 if (eager->tcp_ipversion == IPV4_VERSION) { 18647 sin6->sin6_flowinfo = 0; 18648 IN6_IPADDR_TO_V4MAPPED( 18649 eager->tcp_ipha->ipha_src, 18650 &sin6->sin6_addr); 18651 } else { 18652 ASSERT(eager->tcp_ip6h != NULL); 18653 sin6->sin6_flowinfo = 18654 eager->tcp_ip6h->ip6_vcf & 18655 ~IPV6_VERS_AND_FLOW_MASK; 18656 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18657 } 18658 sin6->sin6_scope_id = 0; 18659 sin6->__sin6_src_id = 0; 18660 } 18661 18662 putnext(rq, mp); 18663 return; 18664 default: 18665 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18666 if (mp != NULL) 18667 putnext(rq, mp); 18668 return; 18669 } 18670 } 18671 18672 static int 18673 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18674 { 18675 sin_t *sin = (sin_t *)sa; 18676 sin6_t *sin6 = (sin6_t *)sa; 18677 18678 switch (tcp->tcp_family) { 18679 case AF_INET: 18680 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18681 18682 if (*salenp < sizeof (sin_t)) 18683 return (EINVAL); 18684 18685 *sin = sin_null; 18686 sin->sin_family = AF_INET; 18687 if (tcp->tcp_state >= TCPS_BOUND) { 18688 sin->sin_port = tcp->tcp_lport; 18689 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18690 } 18691 *salenp = sizeof (sin_t); 18692 break; 18693 18694 case AF_INET6: 18695 if (*salenp < sizeof (sin6_t)) 18696 return (EINVAL); 18697 18698 *sin6 = sin6_null; 18699 sin6->sin6_family = AF_INET6; 18700 if (tcp->tcp_state >= TCPS_BOUND) { 18701 sin6->sin6_port = tcp->tcp_lport; 18702 if (tcp->tcp_ipversion == IPV4_VERSION) { 18703 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18704 &sin6->sin6_addr); 18705 } else { 18706 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18707 } 18708 } 18709 *salenp = sizeof (sin6_t); 18710 break; 18711 } 18712 18713 return (0); 18714 } 18715 18716 static int 18717 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18718 { 18719 sin_t *sin = (sin_t *)sa; 18720 sin6_t *sin6 = (sin6_t *)sa; 18721 18722 if (tcp->tcp_state < TCPS_SYN_RCVD) 18723 return (ENOTCONN); 18724 18725 switch (tcp->tcp_family) { 18726 case AF_INET: 18727 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18728 18729 if (*salenp < sizeof (sin_t)) 18730 return (EINVAL); 18731 18732 *sin = sin_null; 18733 sin->sin_family = AF_INET; 18734 sin->sin_port = tcp->tcp_fport; 18735 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18736 sin->sin_addr.s_addr); 18737 *salenp = sizeof (sin_t); 18738 break; 18739 18740 case AF_INET6: 18741 if (*salenp < sizeof (sin6_t)) 18742 return (EINVAL); 18743 18744 *sin6 = sin6_null; 18745 sin6->sin6_family = AF_INET6; 18746 sin6->sin6_port = tcp->tcp_fport; 18747 sin6->sin6_addr = tcp->tcp_remote_v6; 18748 if (tcp->tcp_ipversion == IPV6_VERSION) { 18749 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18750 ~IPV6_VERS_AND_FLOW_MASK; 18751 } 18752 *salenp = sizeof (sin6_t); 18753 break; 18754 } 18755 18756 return (0); 18757 } 18758 18759 /* 18760 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18761 */ 18762 static void 18763 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18764 { 18765 void *data; 18766 mblk_t *datamp = mp->b_cont; 18767 tcp_t *tcp = Q_TO_TCP(q); 18768 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18769 18770 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18771 cmdp->cb_error = EPROTO; 18772 qreply(q, mp); 18773 return; 18774 } 18775 18776 data = datamp->b_rptr; 18777 18778 switch (cmdp->cb_cmd) { 18779 case TI_GETPEERNAME: 18780 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 18781 break; 18782 case TI_GETMYNAME: 18783 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 18784 break; 18785 default: 18786 cmdp->cb_error = EINVAL; 18787 break; 18788 } 18789 18790 qreply(q, mp); 18791 } 18792 18793 void 18794 tcp_wput(queue_t *q, mblk_t *mp) 18795 { 18796 conn_t *connp = Q_TO_CONN(q); 18797 tcp_t *tcp; 18798 void (*output_proc)(); 18799 t_scalar_t type; 18800 uchar_t *rptr; 18801 struct iocblk *iocp; 18802 size_t size; 18803 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18804 18805 ASSERT(connp->conn_ref >= 2); 18806 18807 switch (DB_TYPE(mp)) { 18808 case M_DATA: 18809 tcp = connp->conn_tcp; 18810 ASSERT(tcp != NULL); 18811 18812 size = msgdsize(mp); 18813 18814 mutex_enter(&tcp->tcp_non_sq_lock); 18815 tcp->tcp_squeue_bytes += size; 18816 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18817 tcp_setqfull(tcp); 18818 } 18819 mutex_exit(&tcp->tcp_non_sq_lock); 18820 18821 CONN_INC_REF(connp); 18822 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18823 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18824 return; 18825 18826 case M_CMD: 18827 tcp_wput_cmdblk(q, mp); 18828 return; 18829 18830 case M_PROTO: 18831 case M_PCPROTO: 18832 /* 18833 * if it is a snmp message, don't get behind the squeue 18834 */ 18835 tcp = connp->conn_tcp; 18836 rptr = mp->b_rptr; 18837 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18838 type = ((union T_primitives *)rptr)->type; 18839 } else { 18840 if (tcp->tcp_debug) { 18841 (void) strlog(TCP_MOD_ID, 0, 1, 18842 SL_ERROR|SL_TRACE, 18843 "tcp_wput_proto, dropping one..."); 18844 } 18845 freemsg(mp); 18846 return; 18847 } 18848 if (type == T_SVR4_OPTMGMT_REQ) { 18849 /* 18850 * All Solaris components should pass a db_credp 18851 * for this TPI message, hence we ASSERT. 18852 * But in case there is some other M_PROTO that looks 18853 * like a TPI message sent by some other kernel 18854 * component, we check and return an error. 18855 */ 18856 cred_t *cr = msg_getcred(mp, NULL); 18857 18858 ASSERT(cr != NULL); 18859 if (cr == NULL) { 18860 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18861 return; 18862 } 18863 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18864 cr)) { 18865 /* 18866 * This was a SNMP request 18867 */ 18868 return; 18869 } else { 18870 output_proc = tcp_wput_proto; 18871 } 18872 } else { 18873 output_proc = tcp_wput_proto; 18874 } 18875 break; 18876 case M_IOCTL: 18877 /* 18878 * Most ioctls can be processed right away without going via 18879 * squeues - process them right here. Those that do require 18880 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18881 * are processed by tcp_wput_ioctl(). 18882 */ 18883 iocp = (struct iocblk *)mp->b_rptr; 18884 tcp = connp->conn_tcp; 18885 18886 switch (iocp->ioc_cmd) { 18887 case TCP_IOC_ABORT_CONN: 18888 tcp_ioctl_abort_conn(q, mp); 18889 return; 18890 case TI_GETPEERNAME: 18891 case TI_GETMYNAME: 18892 mi_copyin(q, mp, NULL, 18893 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18894 return; 18895 case ND_SET: 18896 /* nd_getset does the necessary checks */ 18897 case ND_GET: 18898 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18899 CALL_IP_WPUT(connp, q, mp); 18900 return; 18901 } 18902 qreply(q, mp); 18903 return; 18904 case TCP_IOC_DEFAULT_Q: 18905 /* 18906 * Wants to be the default wq. Check the credentials 18907 * first, the rest is executed via squeue. 18908 */ 18909 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18910 iocp->ioc_error = EPERM; 18911 iocp->ioc_count = 0; 18912 mp->b_datap->db_type = M_IOCACK; 18913 qreply(q, mp); 18914 return; 18915 } 18916 output_proc = tcp_wput_ioctl; 18917 break; 18918 default: 18919 output_proc = tcp_wput_ioctl; 18920 break; 18921 } 18922 break; 18923 default: 18924 output_proc = tcp_wput_nondata; 18925 break; 18926 } 18927 18928 CONN_INC_REF(connp); 18929 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18930 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18931 } 18932 18933 /* 18934 * Initial STREAMS write side put() procedure for sockets. It tries to 18935 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18936 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18937 * are handled by tcp_wput() as usual. 18938 * 18939 * All further messages will also be handled by tcp_wput() because we cannot 18940 * be sure that the above short cut is safe later. 18941 */ 18942 static void 18943 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18944 { 18945 conn_t *connp = Q_TO_CONN(wq); 18946 tcp_t *tcp = connp->conn_tcp; 18947 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18948 18949 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18950 wq->q_qinfo = &tcp_winit; 18951 18952 ASSERT(IPCL_IS_TCP(connp)); 18953 ASSERT(TCP_IS_SOCKET(tcp)); 18954 18955 if (DB_TYPE(mp) == M_PCPROTO && 18956 MBLKL(mp) == sizeof (struct T_capability_req) && 18957 car->PRIM_type == T_CAPABILITY_REQ) { 18958 tcp_capability_req(tcp, mp); 18959 return; 18960 } 18961 18962 tcp_wput(wq, mp); 18963 } 18964 18965 /* ARGSUSED */ 18966 static void 18967 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18968 { 18969 #ifdef DEBUG 18970 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18971 #endif 18972 freemsg(mp); 18973 } 18974 18975 static boolean_t 18976 tcp_zcopy_check(tcp_t *tcp) 18977 { 18978 conn_t *connp = tcp->tcp_connp; 18979 ire_t *ire; 18980 boolean_t zc_enabled = B_FALSE; 18981 tcp_stack_t *tcps = tcp->tcp_tcps; 18982 18983 if (do_tcpzcopy == 2) 18984 zc_enabled = B_TRUE; 18985 else if (tcp->tcp_ipversion == IPV4_VERSION && 18986 IPCL_IS_CONNECTED(connp) && 18987 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18988 connp->conn_dontroute == 0 && 18989 !connp->conn_nexthop_set && 18990 connp->conn_outgoing_ill == NULL && 18991 do_tcpzcopy == 1) { 18992 /* 18993 * the checks above closely resemble the fast path checks 18994 * in tcp_send_data(). 18995 */ 18996 mutex_enter(&connp->conn_lock); 18997 ire = connp->conn_ire_cache; 18998 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18999 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19000 IRE_REFHOLD(ire); 19001 if (ire->ire_stq != NULL) { 19002 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 19003 19004 zc_enabled = ill && (ill->ill_capabilities & 19005 ILL_CAPAB_ZEROCOPY) && 19006 (ill->ill_zerocopy_capab-> 19007 ill_zerocopy_flags != 0); 19008 } 19009 IRE_REFRELE(ire); 19010 } 19011 mutex_exit(&connp->conn_lock); 19012 } 19013 tcp->tcp_snd_zcopy_on = zc_enabled; 19014 if (!TCP_IS_DETACHED(tcp)) { 19015 if (zc_enabled) { 19016 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19017 ZCVMSAFE); 19018 TCP_STAT(tcps, tcp_zcopy_on); 19019 } else { 19020 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19021 ZCVMUNSAFE); 19022 TCP_STAT(tcps, tcp_zcopy_off); 19023 } 19024 } 19025 return (zc_enabled); 19026 } 19027 19028 static mblk_t * 19029 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19030 { 19031 tcp_stack_t *tcps = tcp->tcp_tcps; 19032 19033 if (do_tcpzcopy == 2) 19034 return (bp); 19035 else if (tcp->tcp_snd_zcopy_on) { 19036 tcp->tcp_snd_zcopy_on = B_FALSE; 19037 if (!TCP_IS_DETACHED(tcp)) { 19038 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 19039 ZCVMUNSAFE); 19040 TCP_STAT(tcps, tcp_zcopy_disable); 19041 } 19042 } 19043 return (tcp_zcopy_backoff(tcp, bp, 0)); 19044 } 19045 19046 /* 19047 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19048 * the original desballoca'ed segmapped mblk. 19049 */ 19050 static mblk_t * 19051 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19052 { 19053 mblk_t *head, *tail, *nbp; 19054 tcp_stack_t *tcps = tcp->tcp_tcps; 19055 19056 if (IS_VMLOANED_MBLK(bp)) { 19057 TCP_STAT(tcps, tcp_zcopy_backoff); 19058 if ((head = copyb(bp)) == NULL) { 19059 /* fail to backoff; leave it for the next backoff */ 19060 tcp->tcp_xmit_zc_clean = B_FALSE; 19061 return (bp); 19062 } 19063 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19064 if (fix_xmitlist) 19065 tcp_zcopy_notify(tcp); 19066 else 19067 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19068 } 19069 nbp = bp->b_cont; 19070 if (fix_xmitlist) { 19071 head->b_prev = bp->b_prev; 19072 head->b_next = bp->b_next; 19073 if (tcp->tcp_xmit_tail == bp) 19074 tcp->tcp_xmit_tail = head; 19075 } 19076 bp->b_next = NULL; 19077 bp->b_prev = NULL; 19078 freeb(bp); 19079 } else { 19080 head = bp; 19081 nbp = bp->b_cont; 19082 } 19083 tail = head; 19084 while (nbp) { 19085 if (IS_VMLOANED_MBLK(nbp)) { 19086 TCP_STAT(tcps, tcp_zcopy_backoff); 19087 if ((tail->b_cont = copyb(nbp)) == NULL) { 19088 tcp->tcp_xmit_zc_clean = B_FALSE; 19089 tail->b_cont = nbp; 19090 return (head); 19091 } 19092 tail = tail->b_cont; 19093 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19094 if (fix_xmitlist) 19095 tcp_zcopy_notify(tcp); 19096 else 19097 tail->b_datap->db_struioflag |= 19098 STRUIO_ZCNOTIFY; 19099 } 19100 bp = nbp; 19101 nbp = nbp->b_cont; 19102 if (fix_xmitlist) { 19103 tail->b_prev = bp->b_prev; 19104 tail->b_next = bp->b_next; 19105 if (tcp->tcp_xmit_tail == bp) 19106 tcp->tcp_xmit_tail = tail; 19107 } 19108 bp->b_next = NULL; 19109 bp->b_prev = NULL; 19110 freeb(bp); 19111 } else { 19112 tail->b_cont = nbp; 19113 tail = nbp; 19114 nbp = nbp->b_cont; 19115 } 19116 } 19117 if (fix_xmitlist) { 19118 tcp->tcp_xmit_last = tail; 19119 tcp->tcp_xmit_zc_clean = B_TRUE; 19120 } 19121 return (head); 19122 } 19123 19124 static void 19125 tcp_zcopy_notify(tcp_t *tcp) 19126 { 19127 struct stdata *stp; 19128 conn_t *connp; 19129 19130 if (tcp->tcp_detached) 19131 return; 19132 connp = tcp->tcp_connp; 19133 if (IPCL_IS_NONSTR(connp)) { 19134 (*connp->conn_upcalls->su_zcopy_notify) 19135 (connp->conn_upper_handle); 19136 return; 19137 } 19138 stp = STREAM(tcp->tcp_rq); 19139 mutex_enter(&stp->sd_lock); 19140 stp->sd_flag |= STZCNOTIFY; 19141 cv_broadcast(&stp->sd_zcopy_wait); 19142 mutex_exit(&stp->sd_lock); 19143 } 19144 19145 static boolean_t 19146 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19147 { 19148 ire_t *ire; 19149 conn_t *connp = tcp->tcp_connp; 19150 tcp_stack_t *tcps = tcp->tcp_tcps; 19151 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19152 19153 mutex_enter(&connp->conn_lock); 19154 ire = connp->conn_ire_cache; 19155 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19156 19157 if ((ire != NULL) && 19158 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19159 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19160 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19161 IRE_REFHOLD(ire); 19162 mutex_exit(&connp->conn_lock); 19163 } else { 19164 boolean_t cached = B_FALSE; 19165 ts_label_t *tsl; 19166 19167 /* force a recheck later on */ 19168 tcp->tcp_ire_ill_check_done = B_FALSE; 19169 19170 TCP_DBGSTAT(tcps, tcp_ire_null1); 19171 connp->conn_ire_cache = NULL; 19172 mutex_exit(&connp->conn_lock); 19173 19174 if (ire != NULL) 19175 IRE_REFRELE_NOTR(ire); 19176 19177 tsl = crgetlabel(CONN_CRED(connp)); 19178 ire = (dst ? 19179 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19180 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19181 connp->conn_zoneid, tsl, ipst)); 19182 19183 if (ire == NULL) { 19184 TCP_STAT(tcps, tcp_ire_null); 19185 return (B_FALSE); 19186 } 19187 19188 IRE_REFHOLD_NOTR(ire); 19189 19190 mutex_enter(&connp->conn_lock); 19191 if (CONN_CACHE_IRE(connp)) { 19192 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19193 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19194 TCP_CHECK_IREINFO(tcp, ire); 19195 connp->conn_ire_cache = ire; 19196 cached = B_TRUE; 19197 } 19198 rw_exit(&ire->ire_bucket->irb_lock); 19199 } 19200 mutex_exit(&connp->conn_lock); 19201 19202 /* 19203 * We can continue to use the ire but since it was 19204 * not cached, we should drop the extra reference. 19205 */ 19206 if (!cached) 19207 IRE_REFRELE_NOTR(ire); 19208 19209 /* 19210 * Rampart note: no need to select a new label here, since 19211 * labels are not allowed to change during the life of a TCP 19212 * connection. 19213 */ 19214 } 19215 19216 *irep = ire; 19217 19218 return (B_TRUE); 19219 } 19220 19221 /* 19222 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19223 * 19224 * 0 = success; 19225 * 1 = failed to find ire and ill. 19226 */ 19227 static boolean_t 19228 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19229 { 19230 ipha_t *ipha; 19231 ipaddr_t dst; 19232 ire_t *ire; 19233 ill_t *ill; 19234 mblk_t *ire_fp_mp; 19235 tcp_stack_t *tcps = tcp->tcp_tcps; 19236 19237 if (mp != NULL) 19238 ipha = (ipha_t *)mp->b_rptr; 19239 else 19240 ipha = tcp->tcp_ipha; 19241 dst = ipha->ipha_dst; 19242 19243 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19244 return (B_FALSE); 19245 19246 if ((ire->ire_flags & RTF_MULTIRT) || 19247 (ire->ire_stq == NULL) || 19248 (ire->ire_nce == NULL) || 19249 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19250 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19251 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19252 TCP_STAT(tcps, tcp_ip_ire_send); 19253 IRE_REFRELE(ire); 19254 return (B_FALSE); 19255 } 19256 19257 ill = ire_to_ill(ire); 19258 ASSERT(ill != NULL); 19259 19260 if (!tcp->tcp_ire_ill_check_done) { 19261 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19262 tcp->tcp_ire_ill_check_done = B_TRUE; 19263 } 19264 19265 *irep = ire; 19266 *illp = ill; 19267 19268 return (B_TRUE); 19269 } 19270 19271 static void 19272 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19273 { 19274 ipha_t *ipha; 19275 ipaddr_t src; 19276 ipaddr_t dst; 19277 uint32_t cksum; 19278 ire_t *ire; 19279 uint16_t *up; 19280 ill_t *ill; 19281 conn_t *connp = tcp->tcp_connp; 19282 uint32_t hcksum_txflags = 0; 19283 mblk_t *ire_fp_mp; 19284 uint_t ire_fp_mp_len; 19285 tcp_stack_t *tcps = tcp->tcp_tcps; 19286 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19287 cred_t *cr; 19288 pid_t cpid; 19289 19290 ASSERT(DB_TYPE(mp) == M_DATA); 19291 19292 /* 19293 * Here we need to handle the overloading of the cred_t for 19294 * both getpeerucred and TX. 19295 * If this is a SYN then the caller already set db_credp so 19296 * that getpeerucred will work. But if TX is in use we might have 19297 * a conn_peercred which is different, and we need to use that cred 19298 * to make TX use the correct label and label dependent route. 19299 */ 19300 if (is_system_labeled()) { 19301 cr = msg_getcred(mp, &cpid); 19302 if (cr == NULL || connp->conn_peercred != NULL) 19303 mblk_setcred(mp, CONN_CRED(connp), cpid); 19304 } 19305 19306 ipha = (ipha_t *)mp->b_rptr; 19307 src = ipha->ipha_src; 19308 dst = ipha->ipha_dst; 19309 19310 ASSERT(q != NULL); 19311 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19312 19313 /* 19314 * Drop off fast path for IPv6 and also if options are present or 19315 * we need to resolve a TS label. 19316 */ 19317 if (tcp->tcp_ipversion != IPV4_VERSION || 19318 !IPCL_IS_CONNECTED(connp) || 19319 !CONN_IS_LSO_MD_FASTPATH(connp) || 19320 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19321 !connp->conn_ulp_labeled || 19322 ipha->ipha_ident == IP_HDR_INCLUDED || 19323 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19324 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19325 if (tcp->tcp_snd_zcopy_aware) 19326 mp = tcp_zcopy_disable(tcp, mp); 19327 TCP_STAT(tcps, tcp_ip_send); 19328 CALL_IP_WPUT(connp, q, mp); 19329 return; 19330 } 19331 19332 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19333 if (tcp->tcp_snd_zcopy_aware) 19334 mp = tcp_zcopy_backoff(tcp, mp, 0); 19335 CALL_IP_WPUT(connp, q, mp); 19336 return; 19337 } 19338 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19339 ire_fp_mp_len = MBLKL(ire_fp_mp); 19340 19341 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19342 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19343 #ifndef _BIG_ENDIAN 19344 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19345 #endif 19346 19347 /* 19348 * Check to see if we need to re-enable LSO/MDT for this connection 19349 * because it was previously disabled due to changes in the ill; 19350 * note that by doing it here, this re-enabling only applies when 19351 * the packet is not dispatched through CALL_IP_WPUT(). 19352 * 19353 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19354 * case, since that's how we ended up here. For IPv6, we do the 19355 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19356 */ 19357 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19358 /* 19359 * Restore LSO for this connection, so that next time around 19360 * it is eligible to go through tcp_lsosend() path again. 19361 */ 19362 TCP_STAT(tcps, tcp_lso_enabled); 19363 tcp->tcp_lso = B_TRUE; 19364 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19365 "interface %s\n", (void *)connp, ill->ill_name)); 19366 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19367 /* 19368 * Restore MDT for this connection, so that next time around 19369 * it is eligible to go through tcp_multisend() path again. 19370 */ 19371 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19372 tcp->tcp_mdt = B_TRUE; 19373 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19374 "interface %s\n", (void *)connp, ill->ill_name)); 19375 } 19376 19377 if (tcp->tcp_snd_zcopy_aware) { 19378 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19379 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19380 mp = tcp_zcopy_disable(tcp, mp); 19381 /* 19382 * we shouldn't need to reset ipha as the mp containing 19383 * ipha should never be a zero-copy mp. 19384 */ 19385 } 19386 19387 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19388 ASSERT(ill->ill_hcksum_capab != NULL); 19389 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19390 } 19391 19392 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19393 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19394 19395 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19396 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19397 19398 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19399 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19400 19401 /* Software checksum? */ 19402 if (DB_CKSUMFLAGS(mp) == 0) { 19403 TCP_STAT(tcps, tcp_out_sw_cksum); 19404 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19405 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19406 } 19407 19408 /* Calculate IP header checksum if hardware isn't capable */ 19409 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19410 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19411 ((uint16_t *)ipha)[4]); 19412 } 19413 19414 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19415 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19416 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19417 19418 UPDATE_OB_PKT_COUNT(ire); 19419 ire->ire_last_used_time = lbolt; 19420 19421 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19422 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19423 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19424 ntohs(ipha->ipha_length)); 19425 19426 DTRACE_PROBE4(ip4__physical__out__start, 19427 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 19428 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19429 ipst->ips_ipv4firewall_physical_out, 19430 NULL, ill, ipha, mp, mp, 0, ipst); 19431 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19432 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 19433 19434 if (mp != NULL) { 19435 if (ipst->ips_ipobs_enabled) { 19436 zoneid_t szone; 19437 19438 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 19439 ipst, ALL_ZONES); 19440 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 19441 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 19442 } 19443 19444 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 19445 } 19446 19447 IRE_REFRELE(ire); 19448 } 19449 19450 /* 19451 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19452 * if the receiver shrinks the window, i.e. moves the right window to the 19453 * left, the we should not send new data, but should retransmit normally the 19454 * old unacked data between suna and suna + swnd. We might has sent data 19455 * that is now outside the new window, pretend that we didn't send it. 19456 */ 19457 static void 19458 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19459 { 19460 uint32_t snxt = tcp->tcp_snxt; 19461 mblk_t *xmit_tail; 19462 int32_t offset; 19463 19464 ASSERT(shrunk_count > 0); 19465 19466 /* Pretend we didn't send the data outside the window */ 19467 snxt -= shrunk_count; 19468 19469 /* Get the mblk and the offset in it per the shrunk window */ 19470 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19471 19472 ASSERT(xmit_tail != NULL); 19473 19474 /* Reset all the values per the now shrunk window */ 19475 tcp->tcp_snxt = snxt; 19476 tcp->tcp_xmit_tail = xmit_tail; 19477 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19478 offset; 19479 tcp->tcp_unsent += shrunk_count; 19480 19481 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19482 /* 19483 * Make sure the timer is running so that we will probe a zero 19484 * window. 19485 */ 19486 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19487 } 19488 19489 19490 /* 19491 * The TCP normal data output path. 19492 * NOTE: the logic of the fast path is duplicated from this function. 19493 */ 19494 static void 19495 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19496 { 19497 int len; 19498 mblk_t *local_time; 19499 mblk_t *mp1; 19500 uint32_t snxt; 19501 int tail_unsent; 19502 int tcpstate; 19503 int usable = 0; 19504 mblk_t *xmit_tail; 19505 queue_t *q = tcp->tcp_wq; 19506 int32_t mss; 19507 int32_t num_sack_blk = 0; 19508 int32_t tcp_hdr_len; 19509 int32_t tcp_tcp_hdr_len; 19510 int mdt_thres; 19511 int rc; 19512 tcp_stack_t *tcps = tcp->tcp_tcps; 19513 ip_stack_t *ipst; 19514 19515 tcpstate = tcp->tcp_state; 19516 if (mp == NULL) { 19517 /* 19518 * tcp_wput_data() with NULL mp should only be called when 19519 * there is unsent data. 19520 */ 19521 ASSERT(tcp->tcp_unsent > 0); 19522 /* Really tacky... but we need this for detached closes. */ 19523 len = tcp->tcp_unsent; 19524 goto data_null; 19525 } 19526 19527 #if CCS_STATS 19528 wrw_stats.tot.count++; 19529 wrw_stats.tot.bytes += msgdsize(mp); 19530 #endif 19531 ASSERT(mp->b_datap->db_type == M_DATA); 19532 /* 19533 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19534 * or before a connection attempt has begun. 19535 */ 19536 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19537 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19538 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19539 #ifdef DEBUG 19540 cmn_err(CE_WARN, 19541 "tcp_wput_data: data after ordrel, %s", 19542 tcp_display(tcp, NULL, 19543 DISP_ADDR_AND_PORT)); 19544 #else 19545 if (tcp->tcp_debug) { 19546 (void) strlog(TCP_MOD_ID, 0, 1, 19547 SL_TRACE|SL_ERROR, 19548 "tcp_wput_data: data after ordrel, %s\n", 19549 tcp_display(tcp, NULL, 19550 DISP_ADDR_AND_PORT)); 19551 } 19552 #endif /* DEBUG */ 19553 } 19554 if (tcp->tcp_snd_zcopy_aware && 19555 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19556 tcp_zcopy_notify(tcp); 19557 freemsg(mp); 19558 mutex_enter(&tcp->tcp_non_sq_lock); 19559 if (tcp->tcp_flow_stopped && 19560 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19561 tcp_clrqfull(tcp); 19562 } 19563 mutex_exit(&tcp->tcp_non_sq_lock); 19564 return; 19565 } 19566 19567 /* Strip empties */ 19568 for (;;) { 19569 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19570 (uintptr_t)INT_MAX); 19571 len = (int)(mp->b_wptr - mp->b_rptr); 19572 if (len > 0) 19573 break; 19574 mp1 = mp; 19575 mp = mp->b_cont; 19576 freeb(mp1); 19577 if (!mp) { 19578 return; 19579 } 19580 } 19581 19582 /* If we are the first on the list ... */ 19583 if (tcp->tcp_xmit_head == NULL) { 19584 tcp->tcp_xmit_head = mp; 19585 tcp->tcp_xmit_tail = mp; 19586 tcp->tcp_xmit_tail_unsent = len; 19587 } else { 19588 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19589 struct datab *dp; 19590 19591 mp1 = tcp->tcp_xmit_last; 19592 if (len < tcp_tx_pull_len && 19593 (dp = mp1->b_datap)->db_ref == 1 && 19594 dp->db_lim - mp1->b_wptr >= len) { 19595 ASSERT(len > 0); 19596 ASSERT(!mp1->b_cont); 19597 if (len == 1) { 19598 *mp1->b_wptr++ = *mp->b_rptr; 19599 } else { 19600 bcopy(mp->b_rptr, mp1->b_wptr, len); 19601 mp1->b_wptr += len; 19602 } 19603 if (mp1 == tcp->tcp_xmit_tail) 19604 tcp->tcp_xmit_tail_unsent += len; 19605 mp1->b_cont = mp->b_cont; 19606 if (tcp->tcp_snd_zcopy_aware && 19607 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19608 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19609 freeb(mp); 19610 mp = mp1; 19611 } else { 19612 tcp->tcp_xmit_last->b_cont = mp; 19613 } 19614 len += tcp->tcp_unsent; 19615 } 19616 19617 /* Tack on however many more positive length mblks we have */ 19618 if ((mp1 = mp->b_cont) != NULL) { 19619 do { 19620 int tlen; 19621 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19622 (uintptr_t)INT_MAX); 19623 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19624 if (tlen <= 0) { 19625 mp->b_cont = mp1->b_cont; 19626 freeb(mp1); 19627 } else { 19628 len += tlen; 19629 mp = mp1; 19630 } 19631 } while ((mp1 = mp->b_cont) != NULL); 19632 } 19633 tcp->tcp_xmit_last = mp; 19634 tcp->tcp_unsent = len; 19635 19636 if (urgent) 19637 usable = 1; 19638 19639 data_null: 19640 snxt = tcp->tcp_snxt; 19641 xmit_tail = tcp->tcp_xmit_tail; 19642 tail_unsent = tcp->tcp_xmit_tail_unsent; 19643 19644 /* 19645 * Note that tcp_mss has been adjusted to take into account the 19646 * timestamp option if applicable. Because SACK options do not 19647 * appear in every TCP segments and they are of variable lengths, 19648 * they cannot be included in tcp_mss. Thus we need to calculate 19649 * the actual segment length when we need to send a segment which 19650 * includes SACK options. 19651 */ 19652 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19653 int32_t opt_len; 19654 19655 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19656 tcp->tcp_num_sack_blk); 19657 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19658 2 + TCPOPT_HEADER_LEN; 19659 mss = tcp->tcp_mss - opt_len; 19660 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19661 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19662 } else { 19663 mss = tcp->tcp_mss; 19664 tcp_hdr_len = tcp->tcp_hdr_len; 19665 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19666 } 19667 19668 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19669 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19670 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19671 } 19672 if (tcpstate == TCPS_SYN_RCVD) { 19673 /* 19674 * The three-way connection establishment handshake is not 19675 * complete yet. We want to queue the data for transmission 19676 * after entering ESTABLISHED state (RFC793). A jump to 19677 * "done" label effectively leaves data on the queue. 19678 */ 19679 goto done; 19680 } else { 19681 int usable_r; 19682 19683 /* 19684 * In the special case when cwnd is zero, which can only 19685 * happen if the connection is ECN capable, return now. 19686 * New segments is sent using tcp_timer(). The timer 19687 * is set in tcp_rput_data(). 19688 */ 19689 if (tcp->tcp_cwnd == 0) { 19690 /* 19691 * Note that tcp_cwnd is 0 before 3-way handshake is 19692 * finished. 19693 */ 19694 ASSERT(tcp->tcp_ecn_ok || 19695 tcp->tcp_state < TCPS_ESTABLISHED); 19696 return; 19697 } 19698 19699 /* NOTE: trouble if xmitting while SYN not acked? */ 19700 usable_r = snxt - tcp->tcp_suna; 19701 usable_r = tcp->tcp_swnd - usable_r; 19702 19703 /* 19704 * Check if the receiver has shrunk the window. If 19705 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19706 * cannot be set as there is unsent data, so FIN cannot 19707 * be sent out. Otherwise, we need to take into account 19708 * of FIN as it consumes an "invisible" sequence number. 19709 */ 19710 ASSERT(tcp->tcp_fin_sent == 0); 19711 if (usable_r < 0) { 19712 /* 19713 * The receiver has shrunk the window and we have sent 19714 * -usable_r date beyond the window, re-adjust. 19715 * 19716 * If TCP window scaling is enabled, there can be 19717 * round down error as the advertised receive window 19718 * is actually right shifted n bits. This means that 19719 * the lower n bits info is wiped out. It will look 19720 * like the window is shrunk. Do a check here to 19721 * see if the shrunk amount is actually within the 19722 * error in window calculation. If it is, just 19723 * return. Note that this check is inside the 19724 * shrunk window check. This makes sure that even 19725 * though tcp_process_shrunk_swnd() is not called, 19726 * we will stop further processing. 19727 */ 19728 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19729 tcp_process_shrunk_swnd(tcp, -usable_r); 19730 } 19731 return; 19732 } 19733 19734 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19735 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19736 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19737 19738 /* usable = MIN(usable, unsent) */ 19739 if (usable_r > len) 19740 usable_r = len; 19741 19742 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19743 if (usable_r > 0) { 19744 usable = usable_r; 19745 } else { 19746 /* Bypass all other unnecessary processing. */ 19747 goto done; 19748 } 19749 } 19750 19751 local_time = (mblk_t *)lbolt; 19752 19753 /* 19754 * "Our" Nagle Algorithm. This is not the same as in the old 19755 * BSD. This is more in line with the true intent of Nagle. 19756 * 19757 * The conditions are: 19758 * 1. The amount of unsent data (or amount of data which can be 19759 * sent, whichever is smaller) is less than Nagle limit. 19760 * 2. The last sent size is also less than Nagle limit. 19761 * 3. There is unack'ed data. 19762 * 4. Urgent pointer is not set. Send urgent data ignoring the 19763 * Nagle algorithm. This reduces the probability that urgent 19764 * bytes get "merged" together. 19765 * 5. The app has not closed the connection. This eliminates the 19766 * wait time of the receiving side waiting for the last piece of 19767 * (small) data. 19768 * 19769 * If all are satisified, exit without sending anything. Note 19770 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19771 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19772 * 4095). 19773 */ 19774 if (usable < (int)tcp->tcp_naglim && 19775 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19776 snxt != tcp->tcp_suna && 19777 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19778 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19779 goto done; 19780 } 19781 19782 if (tcp->tcp_cork) { 19783 /* 19784 * if the tcp->tcp_cork option is set, then we have to force 19785 * TCP not to send partial segment (smaller than MSS bytes). 19786 * We are calculating the usable now based on full mss and 19787 * will save the rest of remaining data for later. 19788 */ 19789 if (usable < mss) 19790 goto done; 19791 usable = (usable / mss) * mss; 19792 } 19793 19794 /* Update the latest receive window size in TCP header. */ 19795 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19796 tcp->tcp_tcph->th_win); 19797 19798 /* 19799 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19800 * 19801 * 1. Simple TCP/IP{v4,v6} (no options). 19802 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19803 * 3. If the TCP connection is in ESTABLISHED state. 19804 * 4. The TCP is not detached. 19805 * 19806 * If any of the above conditions have changed during the 19807 * connection, stop using LSO/MDT and restore the stream head 19808 * parameters accordingly. 19809 */ 19810 ipst = tcps->tcps_netstack->netstack_ip; 19811 19812 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19813 ((tcp->tcp_ipversion == IPV4_VERSION && 19814 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19815 (tcp->tcp_ipversion == IPV6_VERSION && 19816 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19817 tcp->tcp_state != TCPS_ESTABLISHED || 19818 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19819 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19820 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19821 if (tcp->tcp_lso) { 19822 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19823 tcp->tcp_lso = B_FALSE; 19824 } else { 19825 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19826 tcp->tcp_mdt = B_FALSE; 19827 } 19828 19829 /* Anything other than detached is considered pathological */ 19830 if (!TCP_IS_DETACHED(tcp)) { 19831 if (tcp->tcp_lso) 19832 TCP_STAT(tcps, tcp_lso_disabled); 19833 else 19834 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19835 (void) tcp_maxpsz_set(tcp, B_TRUE); 19836 } 19837 } 19838 19839 /* Use MDT if sendable amount is greater than the threshold */ 19840 if (tcp->tcp_mdt && 19841 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19842 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19843 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19844 (tcp->tcp_valid_bits == 0 || 19845 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19846 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19847 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19848 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19849 local_time, mdt_thres); 19850 } else { 19851 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19852 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19853 local_time, INT_MAX); 19854 } 19855 19856 /* Pretend that all we were trying to send really got sent */ 19857 if (rc < 0 && tail_unsent < 0) { 19858 do { 19859 xmit_tail = xmit_tail->b_cont; 19860 xmit_tail->b_prev = local_time; 19861 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19862 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19863 tail_unsent += (int)(xmit_tail->b_wptr - 19864 xmit_tail->b_rptr); 19865 } while (tail_unsent < 0); 19866 } 19867 done:; 19868 tcp->tcp_xmit_tail = xmit_tail; 19869 tcp->tcp_xmit_tail_unsent = tail_unsent; 19870 len = tcp->tcp_snxt - snxt; 19871 if (len) { 19872 /* 19873 * If new data was sent, need to update the notsack 19874 * list, which is, afterall, data blocks that have 19875 * not been sack'ed by the receiver. New data is 19876 * not sack'ed. 19877 */ 19878 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19879 /* len is a negative value. */ 19880 tcp->tcp_pipe -= len; 19881 tcp_notsack_update(&(tcp->tcp_notsack_list), 19882 tcp->tcp_snxt, snxt, 19883 &(tcp->tcp_num_notsack_blk), 19884 &(tcp->tcp_cnt_notsack_list)); 19885 } 19886 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19887 tcp->tcp_rack = tcp->tcp_rnxt; 19888 tcp->tcp_rack_cnt = 0; 19889 if ((snxt + len) == tcp->tcp_suna) { 19890 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19891 } 19892 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19893 /* 19894 * Didn't send anything. Make sure the timer is running 19895 * so that we will probe a zero window. 19896 */ 19897 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19898 } 19899 /* Note that len is the amount we just sent but with a negative sign */ 19900 tcp->tcp_unsent += len; 19901 mutex_enter(&tcp->tcp_non_sq_lock); 19902 if (tcp->tcp_flow_stopped) { 19903 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19904 tcp_clrqfull(tcp); 19905 } 19906 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19907 tcp_setqfull(tcp); 19908 } 19909 mutex_exit(&tcp->tcp_non_sq_lock); 19910 } 19911 19912 /* 19913 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19914 * outgoing TCP header with the template header, as well as other 19915 * options such as time-stamp, ECN and/or SACK. 19916 */ 19917 static void 19918 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19919 { 19920 tcph_t *tcp_tmpl, *tcp_h; 19921 uint32_t *dst, *src; 19922 int hdrlen; 19923 19924 ASSERT(OK_32PTR(rptr)); 19925 19926 /* Template header */ 19927 tcp_tmpl = tcp->tcp_tcph; 19928 19929 /* Header of outgoing packet */ 19930 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19931 19932 /* dst and src are opaque 32-bit fields, used for copying */ 19933 dst = (uint32_t *)rptr; 19934 src = (uint32_t *)tcp->tcp_iphc; 19935 hdrlen = tcp->tcp_hdr_len; 19936 19937 /* Fill time-stamp option if needed */ 19938 if (tcp->tcp_snd_ts_ok) { 19939 U32_TO_BE32((uint32_t)now, 19940 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19941 U32_TO_BE32(tcp->tcp_ts_recent, 19942 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19943 } else { 19944 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19945 } 19946 19947 /* 19948 * Copy the template header; is this really more efficient than 19949 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19950 * but perhaps not for other scenarios. 19951 */ 19952 dst[0] = src[0]; 19953 dst[1] = src[1]; 19954 dst[2] = src[2]; 19955 dst[3] = src[3]; 19956 dst[4] = src[4]; 19957 dst[5] = src[5]; 19958 dst[6] = src[6]; 19959 dst[7] = src[7]; 19960 dst[8] = src[8]; 19961 dst[9] = src[9]; 19962 if (hdrlen -= 40) { 19963 hdrlen >>= 2; 19964 dst += 10; 19965 src += 10; 19966 do { 19967 *dst++ = *src++; 19968 } while (--hdrlen); 19969 } 19970 19971 /* 19972 * Set the ECN info in the TCP header if it is not a zero 19973 * window probe. Zero window probe is only sent in 19974 * tcp_wput_data() and tcp_timer(). 19975 */ 19976 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19977 SET_ECT(tcp, rptr); 19978 19979 if (tcp->tcp_ecn_echo_on) 19980 tcp_h->th_flags[0] |= TH_ECE; 19981 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19982 tcp_h->th_flags[0] |= TH_CWR; 19983 tcp->tcp_ecn_cwr_sent = B_TRUE; 19984 } 19985 } 19986 19987 /* Fill in SACK options */ 19988 if (num_sack_blk > 0) { 19989 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19990 sack_blk_t *tmp; 19991 int32_t i; 19992 19993 wptr[0] = TCPOPT_NOP; 19994 wptr[1] = TCPOPT_NOP; 19995 wptr[2] = TCPOPT_SACK; 19996 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19997 sizeof (sack_blk_t); 19998 wptr += TCPOPT_REAL_SACK_LEN; 19999 20000 tmp = tcp->tcp_sack_list; 20001 for (i = 0; i < num_sack_blk; i++) { 20002 U32_TO_BE32(tmp[i].begin, wptr); 20003 wptr += sizeof (tcp_seq); 20004 U32_TO_BE32(tmp[i].end, wptr); 20005 wptr += sizeof (tcp_seq); 20006 } 20007 tcp_h->th_offset_and_rsrvd[0] += 20008 ((num_sack_blk * 2 + 1) << 4); 20009 } 20010 } 20011 20012 /* 20013 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20014 * the destination address and SAP attribute, and if necessary, the 20015 * hardware checksum offload attribute to a Multidata message. 20016 */ 20017 static int 20018 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20019 const uint32_t start, const uint32_t stuff, const uint32_t end, 20020 const uint32_t flags, tcp_stack_t *tcps) 20021 { 20022 /* Add global destination address & SAP attribute */ 20023 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20024 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20025 "destination address+SAP\n")); 20026 20027 if (dlmp != NULL) 20028 TCP_STAT(tcps, tcp_mdt_allocfail); 20029 return (-1); 20030 } 20031 20032 /* Add global hwcksum attribute */ 20033 if (hwcksum && 20034 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20035 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20036 "checksum attribute\n")); 20037 20038 TCP_STAT(tcps, tcp_mdt_allocfail); 20039 return (-1); 20040 } 20041 20042 return (0); 20043 } 20044 20045 /* 20046 * Smaller and private version of pdescinfo_t used specifically for TCP, 20047 * which allows for only two payload spans per packet. 20048 */ 20049 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20050 20051 /* 20052 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20053 * scheme, and returns one the following: 20054 * 20055 * -1 = failed allocation. 20056 * 0 = success; burst count reached, or usable send window is too small, 20057 * and that we'd rather wait until later before sending again. 20058 */ 20059 static int 20060 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20061 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20062 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20063 const int mdt_thres) 20064 { 20065 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20066 multidata_t *mmd; 20067 uint_t obsegs, obbytes, hdr_frag_sz; 20068 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20069 int num_burst_seg, max_pld; 20070 pdesc_t *pkt; 20071 tcp_pdescinfo_t tcp_pkt_info; 20072 pdescinfo_t *pkt_info; 20073 int pbuf_idx, pbuf_idx_nxt; 20074 int seg_len, len, spill, af; 20075 boolean_t add_buffer, zcopy, clusterwide; 20076 boolean_t rconfirm = B_FALSE; 20077 boolean_t done = B_FALSE; 20078 uint32_t cksum; 20079 uint32_t hwcksum_flags; 20080 ire_t *ire = NULL; 20081 ill_t *ill; 20082 ipha_t *ipha; 20083 ip6_t *ip6h; 20084 ipaddr_t src, dst; 20085 ill_zerocopy_capab_t *zc_cap = NULL; 20086 uint16_t *up; 20087 int err; 20088 conn_t *connp; 20089 tcp_stack_t *tcps = tcp->tcp_tcps; 20090 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20091 int usable_mmd, tail_unsent_mmd; 20092 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20093 mblk_t *xmit_tail_mmd; 20094 netstackid_t stack_id; 20095 20096 #ifdef _BIG_ENDIAN 20097 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20098 #else 20099 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20100 #endif 20101 20102 #define PREP_NEW_MULTIDATA() { \ 20103 mmd = NULL; \ 20104 md_mp = md_hbuf = NULL; \ 20105 cur_hdr_off = 0; \ 20106 max_pld = tcp->tcp_mdt_max_pld; \ 20107 pbuf_idx = pbuf_idx_nxt = -1; \ 20108 add_buffer = B_TRUE; \ 20109 zcopy = B_FALSE; \ 20110 } 20111 20112 #define PREP_NEW_PBUF() { \ 20113 md_pbuf = md_pbuf_nxt = NULL; \ 20114 pbuf_idx = pbuf_idx_nxt = -1; \ 20115 cur_pld_off = 0; \ 20116 first_snxt = *snxt; \ 20117 ASSERT(*tail_unsent > 0); \ 20118 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20119 } 20120 20121 ASSERT(mdt_thres >= mss); 20122 ASSERT(*usable > 0 && *usable > mdt_thres); 20123 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20124 ASSERT(!TCP_IS_DETACHED(tcp)); 20125 ASSERT(tcp->tcp_valid_bits == 0 || 20126 tcp->tcp_valid_bits == TCP_FSS_VALID); 20127 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20128 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20129 (tcp->tcp_ipversion == IPV6_VERSION && 20130 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20131 20132 connp = tcp->tcp_connp; 20133 ASSERT(connp != NULL); 20134 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20135 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20136 20137 stack_id = connp->conn_netstack->netstack_stackid; 20138 20139 usable_mmd = tail_unsent_mmd = 0; 20140 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20141 xmit_tail_mmd = NULL; 20142 /* 20143 * Note that tcp will only declare at most 2 payload spans per 20144 * packet, which is much lower than the maximum allowable number 20145 * of packet spans per Multidata. For this reason, we use the 20146 * privately declared and smaller descriptor info structure, in 20147 * order to save some stack space. 20148 */ 20149 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20150 20151 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20152 if (af == AF_INET) { 20153 dst = tcp->tcp_ipha->ipha_dst; 20154 src = tcp->tcp_ipha->ipha_src; 20155 ASSERT(!CLASSD(dst)); 20156 } 20157 ASSERT(af == AF_INET || 20158 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20159 20160 obsegs = obbytes = 0; 20161 num_burst_seg = tcp->tcp_snd_burst; 20162 md_mp_head = NULL; 20163 PREP_NEW_MULTIDATA(); 20164 20165 /* 20166 * Before we go on further, make sure there is an IRE that we can 20167 * use, and that the ILL supports MDT. Otherwise, there's no point 20168 * in proceeding any further, and we should just hand everything 20169 * off to the legacy path. 20170 */ 20171 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20172 goto legacy_send_no_md; 20173 20174 ASSERT(ire != NULL); 20175 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20176 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20177 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20178 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20179 /* 20180 * If we do support loopback for MDT (which requires modifications 20181 * to the receiving paths), the following assertions should go away, 20182 * and we would be sending the Multidata to loopback conn later on. 20183 */ 20184 ASSERT(!IRE_IS_LOCAL(ire)); 20185 ASSERT(ire->ire_stq != NULL); 20186 20187 ill = ire_to_ill(ire); 20188 ASSERT(ill != NULL); 20189 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20190 20191 if (!tcp->tcp_ire_ill_check_done) { 20192 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20193 tcp->tcp_ire_ill_check_done = B_TRUE; 20194 } 20195 20196 /* 20197 * If the underlying interface conditions have changed, or if the 20198 * new interface does not support MDT, go back to legacy path. 20199 */ 20200 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20201 /* don't go through this path anymore for this connection */ 20202 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20203 tcp->tcp_mdt = B_FALSE; 20204 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20205 "interface %s\n", (void *)connp, ill->ill_name)); 20206 /* IRE will be released prior to returning */ 20207 goto legacy_send_no_md; 20208 } 20209 20210 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20211 zc_cap = ill->ill_zerocopy_capab; 20212 20213 /* 20214 * Check if we can take tcp fast-path. Note that "incomplete" 20215 * ire's (where the link-layer for next hop is not resolved 20216 * or where the fast-path header in nce_fp_mp is not available 20217 * yet) are sent down the legacy (slow) path. 20218 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20219 */ 20220 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20221 /* IRE will be released prior to returning */ 20222 goto legacy_send_no_md; 20223 } 20224 20225 /* go to legacy path if interface doesn't support zerocopy */ 20226 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20227 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20228 /* IRE will be released prior to returning */ 20229 goto legacy_send_no_md; 20230 } 20231 20232 /* does the interface support hardware checksum offload? */ 20233 hwcksum_flags = 0; 20234 if (ILL_HCKSUM_CAPABLE(ill) && 20235 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20236 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20237 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20238 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20239 HCKSUM_IPHDRCKSUM) 20240 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20241 20242 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20243 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20244 hwcksum_flags |= HCK_FULLCKSUM; 20245 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20246 HCKSUM_INET_PARTIAL) 20247 hwcksum_flags |= HCK_PARTIALCKSUM; 20248 } 20249 20250 /* 20251 * Each header fragment consists of the leading extra space, 20252 * followed by the TCP/IP header, and the trailing extra space. 20253 * We make sure that each header fragment begins on a 32-bit 20254 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20255 * aligned in tcp_mdt_update). 20256 */ 20257 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20258 tcp->tcp_mdt_hdr_tail), 4); 20259 20260 /* are we starting from the beginning of data block? */ 20261 if (*tail_unsent == 0) { 20262 *xmit_tail = (*xmit_tail)->b_cont; 20263 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20264 *tail_unsent = (int)MBLKL(*xmit_tail); 20265 } 20266 20267 /* 20268 * Here we create one or more Multidata messages, each made up of 20269 * one header buffer and up to N payload buffers. This entire 20270 * operation is done within two loops: 20271 * 20272 * The outer loop mostly deals with creating the Multidata message, 20273 * as well as the header buffer that gets added to it. It also 20274 * links the Multidata messages together such that all of them can 20275 * be sent down to the lower layer in a single putnext call; this 20276 * linking behavior depends on the tcp_mdt_chain tunable. 20277 * 20278 * The inner loop takes an existing Multidata message, and adds 20279 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20280 * packetizes those buffers by filling up the corresponding header 20281 * buffer fragments with the proper IP and TCP headers, and by 20282 * describing the layout of each packet in the packet descriptors 20283 * that get added to the Multidata. 20284 */ 20285 do { 20286 /* 20287 * If usable send window is too small, or data blocks in 20288 * transmit list are smaller than our threshold (i.e. app 20289 * performs large writes followed by small ones), we hand 20290 * off the control over to the legacy path. Note that we'll 20291 * get back the control once it encounters a large block. 20292 */ 20293 if (*usable < mss || (*tail_unsent <= mdt_thres && 20294 (*xmit_tail)->b_cont != NULL && 20295 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20296 /* send down what we've got so far */ 20297 if (md_mp_head != NULL) { 20298 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20299 obsegs, obbytes, &rconfirm); 20300 } 20301 /* 20302 * Pass control over to tcp_send(), but tell it to 20303 * return to us once a large-size transmission is 20304 * possible. 20305 */ 20306 TCP_STAT(tcps, tcp_mdt_legacy_small); 20307 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20308 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20309 tail_unsent, xmit_tail, local_time, 20310 mdt_thres)) <= 0) { 20311 /* burst count reached, or alloc failed */ 20312 IRE_REFRELE(ire); 20313 return (err); 20314 } 20315 20316 /* tcp_send() may have sent everything, so check */ 20317 if (*usable <= 0) { 20318 IRE_REFRELE(ire); 20319 return (0); 20320 } 20321 20322 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20323 /* 20324 * We may have delivered the Multidata, so make sure 20325 * to re-initialize before the next round. 20326 */ 20327 md_mp_head = NULL; 20328 obsegs = obbytes = 0; 20329 num_burst_seg = tcp->tcp_snd_burst; 20330 PREP_NEW_MULTIDATA(); 20331 20332 /* are we starting from the beginning of data block? */ 20333 if (*tail_unsent == 0) { 20334 *xmit_tail = (*xmit_tail)->b_cont; 20335 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20336 (uintptr_t)INT_MAX); 20337 *tail_unsent = (int)MBLKL(*xmit_tail); 20338 } 20339 } 20340 /* 20341 * Record current values for parameters we may need to pass 20342 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20343 * each iteration of the outer loop (each multidata message 20344 * creation). If we have a failure in the inner loop, we send 20345 * any complete multidata messages we have before reverting 20346 * to using the traditional non-md path. 20347 */ 20348 snxt_mmd = *snxt; 20349 usable_mmd = *usable; 20350 xmit_tail_mmd = *xmit_tail; 20351 tail_unsent_mmd = *tail_unsent; 20352 obsegs_mmd = obsegs; 20353 obbytes_mmd = obbytes; 20354 20355 /* 20356 * max_pld limits the number of mblks in tcp's transmit 20357 * queue that can be added to a Multidata message. Once 20358 * this counter reaches zero, no more additional mblks 20359 * can be added to it. What happens afterwards depends 20360 * on whether or not we are set to chain the Multidata 20361 * messages. If we are to link them together, reset 20362 * max_pld to its original value (tcp_mdt_max_pld) and 20363 * prepare to create a new Multidata message which will 20364 * get linked to md_mp_head. Else, leave it alone and 20365 * let the inner loop break on its own. 20366 */ 20367 if (tcp_mdt_chain && max_pld == 0) 20368 PREP_NEW_MULTIDATA(); 20369 20370 /* adding a payload buffer; re-initialize values */ 20371 if (add_buffer) 20372 PREP_NEW_PBUF(); 20373 20374 /* 20375 * If we don't have a Multidata, either because we just 20376 * (re)entered this outer loop, or after we branched off 20377 * to tcp_send above, setup the Multidata and header 20378 * buffer to be used. 20379 */ 20380 if (md_mp == NULL) { 20381 int md_hbuflen; 20382 uint32_t start, stuff; 20383 20384 /* 20385 * Calculate Multidata header buffer size large enough 20386 * to hold all of the headers that can possibly be 20387 * sent at this moment. We'd rather over-estimate 20388 * the size than running out of space; this is okay 20389 * since this buffer is small anyway. 20390 */ 20391 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20392 20393 /* 20394 * Start and stuff offset for partial hardware 20395 * checksum offload; these are currently for IPv4. 20396 * For full checksum offload, they are set to zero. 20397 */ 20398 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20399 if (af == AF_INET) { 20400 start = IP_SIMPLE_HDR_LENGTH; 20401 stuff = IP_SIMPLE_HDR_LENGTH + 20402 TCP_CHECKSUM_OFFSET; 20403 } else { 20404 start = IPV6_HDR_LEN; 20405 stuff = IPV6_HDR_LEN + 20406 TCP_CHECKSUM_OFFSET; 20407 } 20408 } else { 20409 start = stuff = 0; 20410 } 20411 20412 /* 20413 * Create the header buffer, Multidata, as well as 20414 * any necessary attributes (destination address, 20415 * SAP and hardware checksum offload) that should 20416 * be associated with the Multidata message. 20417 */ 20418 ASSERT(cur_hdr_off == 0); 20419 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20420 ((md_hbuf->b_wptr += md_hbuflen), 20421 (mmd = mmd_alloc(md_hbuf, &md_mp, 20422 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20423 /* fastpath mblk */ 20424 ire->ire_nce->nce_res_mp, 20425 /* hardware checksum enabled */ 20426 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20427 /* hardware checksum offsets */ 20428 start, stuff, 0, 20429 /* hardware checksum flag */ 20430 hwcksum_flags, tcps) != 0)) { 20431 legacy_send: 20432 /* 20433 * We arrive here from a failure within the 20434 * inner (packetizer) loop or we fail one of 20435 * the conditionals above. We restore the 20436 * previously checkpointed values for: 20437 * xmit_tail 20438 * usable 20439 * tail_unsent 20440 * snxt 20441 * obbytes 20442 * obsegs 20443 * We should then be able to dispatch any 20444 * complete multidata before reverting to the 20445 * traditional path with consistent parameters 20446 * (the inner loop updates these as it 20447 * iterates). 20448 */ 20449 *xmit_tail = xmit_tail_mmd; 20450 *usable = usable_mmd; 20451 *tail_unsent = tail_unsent_mmd; 20452 *snxt = snxt_mmd; 20453 obbytes = obbytes_mmd; 20454 obsegs = obsegs_mmd; 20455 if (md_mp != NULL) { 20456 /* Unlink message from the chain */ 20457 if (md_mp_head != NULL) { 20458 err = (intptr_t)rmvb(md_mp_head, 20459 md_mp); 20460 /* 20461 * We can't assert that rmvb 20462 * did not return -1, since we 20463 * may get here before linkb 20464 * happens. We do, however, 20465 * check if we just removed the 20466 * only element in the list. 20467 */ 20468 if (err == 0) 20469 md_mp_head = NULL; 20470 } 20471 /* md_hbuf gets freed automatically */ 20472 TCP_STAT(tcps, tcp_mdt_discarded); 20473 freeb(md_mp); 20474 } else { 20475 /* Either allocb or mmd_alloc failed */ 20476 TCP_STAT(tcps, tcp_mdt_allocfail); 20477 if (md_hbuf != NULL) 20478 freeb(md_hbuf); 20479 } 20480 20481 /* send down what we've got so far */ 20482 if (md_mp_head != NULL) { 20483 tcp_multisend_data(tcp, ire, ill, 20484 md_mp_head, obsegs, obbytes, 20485 &rconfirm); 20486 } 20487 legacy_send_no_md: 20488 if (ire != NULL) 20489 IRE_REFRELE(ire); 20490 /* 20491 * Too bad; let the legacy path handle this. 20492 * We specify INT_MAX for the threshold, since 20493 * we gave up with the Multidata processings 20494 * and let the old path have it all. 20495 */ 20496 TCP_STAT(tcps, tcp_mdt_legacy_all); 20497 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20498 tcp_tcp_hdr_len, num_sack_blk, usable, 20499 snxt, tail_unsent, xmit_tail, local_time, 20500 INT_MAX)); 20501 } 20502 20503 /* link to any existing ones, if applicable */ 20504 TCP_STAT(tcps, tcp_mdt_allocd); 20505 if (md_mp_head == NULL) { 20506 md_mp_head = md_mp; 20507 } else if (tcp_mdt_chain) { 20508 TCP_STAT(tcps, tcp_mdt_linked); 20509 linkb(md_mp_head, md_mp); 20510 } 20511 } 20512 20513 ASSERT(md_mp_head != NULL); 20514 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20515 ASSERT(md_mp != NULL && mmd != NULL); 20516 ASSERT(md_hbuf != NULL); 20517 20518 /* 20519 * Packetize the transmittable portion of the data block; 20520 * each data block is essentially added to the Multidata 20521 * as a payload buffer. We also deal with adding more 20522 * than one payload buffers, which happens when the remaining 20523 * packetized portion of the current payload buffer is less 20524 * than MSS, while the next data block in transmit queue 20525 * has enough data to make up for one. This "spillover" 20526 * case essentially creates a split-packet, where portions 20527 * of the packet's payload fragments may span across two 20528 * virtually discontiguous address blocks. 20529 */ 20530 seg_len = mss; 20531 do { 20532 len = seg_len; 20533 20534 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20535 ipha = NULL; 20536 ip6h = NULL; 20537 20538 ASSERT(len > 0); 20539 ASSERT(max_pld >= 0); 20540 ASSERT(!add_buffer || cur_pld_off == 0); 20541 20542 /* 20543 * First time around for this payload buffer; note 20544 * in the case of a spillover, the following has 20545 * been done prior to adding the split-packet 20546 * descriptor to Multidata, and we don't want to 20547 * repeat the process. 20548 */ 20549 if (add_buffer) { 20550 ASSERT(mmd != NULL); 20551 ASSERT(md_pbuf == NULL); 20552 ASSERT(md_pbuf_nxt == NULL); 20553 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20554 20555 /* 20556 * Have we reached the limit? We'd get to 20557 * this case when we're not chaining the 20558 * Multidata messages together, and since 20559 * we're done, terminate this loop. 20560 */ 20561 if (max_pld == 0) 20562 break; /* done */ 20563 20564 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20565 TCP_STAT(tcps, tcp_mdt_allocfail); 20566 goto legacy_send; /* out_of_mem */ 20567 } 20568 20569 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20570 zc_cap != NULL) { 20571 if (!ip_md_zcopy_attr(mmd, NULL, 20572 zc_cap->ill_zerocopy_flags)) { 20573 freeb(md_pbuf); 20574 TCP_STAT(tcps, 20575 tcp_mdt_allocfail); 20576 /* out_of_mem */ 20577 goto legacy_send; 20578 } 20579 zcopy = B_TRUE; 20580 } 20581 20582 md_pbuf->b_rptr += base_pld_off; 20583 20584 /* 20585 * Add a payload buffer to the Multidata; this 20586 * operation must not fail, or otherwise our 20587 * logic in this routine is broken. There 20588 * is no memory allocation done by the 20589 * routine, so any returned failure simply 20590 * tells us that we've done something wrong. 20591 * 20592 * A failure tells us that either we're adding 20593 * the same payload buffer more than once, or 20594 * we're trying to add more buffers than 20595 * allowed (max_pld calculation is wrong). 20596 * None of the above cases should happen, and 20597 * we panic because either there's horrible 20598 * heap corruption, and/or programming mistake. 20599 */ 20600 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20601 if (pbuf_idx < 0) { 20602 cmn_err(CE_PANIC, "tcp_multisend: " 20603 "payload buffer logic error " 20604 "detected for tcp %p mmd %p " 20605 "pbuf %p (%d)\n", 20606 (void *)tcp, (void *)mmd, 20607 (void *)md_pbuf, pbuf_idx); 20608 } 20609 20610 ASSERT(max_pld > 0); 20611 --max_pld; 20612 add_buffer = B_FALSE; 20613 } 20614 20615 ASSERT(md_mp_head != NULL); 20616 ASSERT(md_pbuf != NULL); 20617 ASSERT(md_pbuf_nxt == NULL); 20618 ASSERT(pbuf_idx != -1); 20619 ASSERT(pbuf_idx_nxt == -1); 20620 ASSERT(*usable > 0); 20621 20622 /* 20623 * We spillover to the next payload buffer only 20624 * if all of the following is true: 20625 * 20626 * 1. There is not enough data on the current 20627 * payload buffer to make up `len', 20628 * 2. We are allowed to send `len', 20629 * 3. The next payload buffer length is large 20630 * enough to accomodate `spill'. 20631 */ 20632 if ((spill = len - *tail_unsent) > 0 && 20633 *usable >= len && 20634 MBLKL((*xmit_tail)->b_cont) >= spill && 20635 max_pld > 0) { 20636 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20637 if (md_pbuf_nxt == NULL) { 20638 TCP_STAT(tcps, tcp_mdt_allocfail); 20639 goto legacy_send; /* out_of_mem */ 20640 } 20641 20642 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20643 zc_cap != NULL) { 20644 if (!ip_md_zcopy_attr(mmd, NULL, 20645 zc_cap->ill_zerocopy_flags)) { 20646 freeb(md_pbuf_nxt); 20647 TCP_STAT(tcps, 20648 tcp_mdt_allocfail); 20649 /* out_of_mem */ 20650 goto legacy_send; 20651 } 20652 zcopy = B_TRUE; 20653 } 20654 20655 /* 20656 * See comments above on the first call to 20657 * mmd_addpldbuf for explanation on the panic. 20658 */ 20659 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20660 if (pbuf_idx_nxt < 0) { 20661 panic("tcp_multisend: " 20662 "next payload buffer logic error " 20663 "detected for tcp %p mmd %p " 20664 "pbuf %p (%d)\n", 20665 (void *)tcp, (void *)mmd, 20666 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20667 } 20668 20669 ASSERT(max_pld > 0); 20670 --max_pld; 20671 } else if (spill > 0) { 20672 /* 20673 * If there's a spillover, but the following 20674 * xmit_tail couldn't give us enough octets 20675 * to reach "len", then stop the current 20676 * Multidata creation and let the legacy 20677 * tcp_send() path take over. We don't want 20678 * to send the tiny segment as part of this 20679 * Multidata for performance reasons; instead, 20680 * we let the legacy path deal with grouping 20681 * it with the subsequent small mblks. 20682 */ 20683 if (*usable >= len && 20684 MBLKL((*xmit_tail)->b_cont) < spill) { 20685 max_pld = 0; 20686 break; /* done */ 20687 } 20688 20689 /* 20690 * We can't spillover, and we are near 20691 * the end of the current payload buffer, 20692 * so send what's left. 20693 */ 20694 ASSERT(*tail_unsent > 0); 20695 len = *tail_unsent; 20696 } 20697 20698 /* tail_unsent is negated if there is a spillover */ 20699 *tail_unsent -= len; 20700 *usable -= len; 20701 ASSERT(*usable >= 0); 20702 20703 if (*usable < mss) 20704 seg_len = *usable; 20705 /* 20706 * Sender SWS avoidance; see comments in tcp_send(); 20707 * everything else is the same, except that we only 20708 * do this here if there is no more data to be sent 20709 * following the current xmit_tail. We don't check 20710 * for 1-byte urgent data because we shouldn't get 20711 * here if TCP_URG_VALID is set. 20712 */ 20713 if (*usable > 0 && *usable < mss && 20714 ((md_pbuf_nxt == NULL && 20715 (*xmit_tail)->b_cont == NULL) || 20716 (md_pbuf_nxt != NULL && 20717 (*xmit_tail)->b_cont->b_cont == NULL)) && 20718 seg_len < (tcp->tcp_max_swnd >> 1) && 20719 (tcp->tcp_unsent - 20720 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20721 !tcp->tcp_zero_win_probe) { 20722 if ((*snxt + len) == tcp->tcp_snxt && 20723 (*snxt + len) == tcp->tcp_suna) { 20724 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20725 } 20726 done = B_TRUE; 20727 } 20728 20729 /* 20730 * Prime pump for IP's checksumming on our behalf; 20731 * include the adjustment for a source route if any. 20732 * Do this only for software/partial hardware checksum 20733 * offload, as this field gets zeroed out later for 20734 * the full hardware checksum offload case. 20735 */ 20736 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20737 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20738 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20739 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20740 } 20741 20742 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20743 *snxt += len; 20744 20745 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20746 /* 20747 * We set the PUSH bit only if TCP has no more buffered 20748 * data to be transmitted (or if sender SWS avoidance 20749 * takes place), as opposed to setting it for every 20750 * last packet in the burst. 20751 */ 20752 if (done || 20753 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20754 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20755 20756 /* 20757 * Set FIN bit if this is our last segment; snxt 20758 * already includes its length, and it will not 20759 * be adjusted after this point. 20760 */ 20761 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20762 *snxt == tcp->tcp_fss) { 20763 if (!tcp->tcp_fin_acked) { 20764 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20765 BUMP_MIB(&tcps->tcps_mib, 20766 tcpOutControl); 20767 } 20768 if (!tcp->tcp_fin_sent) { 20769 tcp->tcp_fin_sent = B_TRUE; 20770 /* 20771 * tcp state must be ESTABLISHED 20772 * in order for us to get here in 20773 * the first place. 20774 */ 20775 tcp->tcp_state = TCPS_FIN_WAIT_1; 20776 20777 /* 20778 * Upon returning from this routine, 20779 * tcp_wput_data() will set tcp_snxt 20780 * to be equal to snxt + tcp_fin_sent. 20781 * This is essentially the same as 20782 * setting it to tcp_fss + 1. 20783 */ 20784 } 20785 } 20786 20787 tcp->tcp_last_sent_len = (ushort_t)len; 20788 20789 len += tcp_hdr_len; 20790 if (tcp->tcp_ipversion == IPV4_VERSION) 20791 tcp->tcp_ipha->ipha_length = htons(len); 20792 else 20793 tcp->tcp_ip6h->ip6_plen = htons(len - 20794 ((char *)&tcp->tcp_ip6h[1] - 20795 tcp->tcp_iphc)); 20796 20797 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20798 20799 /* setup header fragment */ 20800 PDESC_HDR_ADD(pkt_info, 20801 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20802 tcp->tcp_mdt_hdr_head, /* head room */ 20803 tcp_hdr_len, /* len */ 20804 tcp->tcp_mdt_hdr_tail); /* tail room */ 20805 20806 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20807 hdr_frag_sz); 20808 ASSERT(MBLKIN(md_hbuf, 20809 (pkt_info->hdr_base - md_hbuf->b_rptr), 20810 PDESC_HDRSIZE(pkt_info))); 20811 20812 /* setup first payload fragment */ 20813 PDESC_PLD_INIT(pkt_info); 20814 PDESC_PLD_SPAN_ADD(pkt_info, 20815 pbuf_idx, /* index */ 20816 md_pbuf->b_rptr + cur_pld_off, /* start */ 20817 tcp->tcp_last_sent_len); /* len */ 20818 20819 /* create a split-packet in case of a spillover */ 20820 if (md_pbuf_nxt != NULL) { 20821 ASSERT(spill > 0); 20822 ASSERT(pbuf_idx_nxt > pbuf_idx); 20823 ASSERT(!add_buffer); 20824 20825 md_pbuf = md_pbuf_nxt; 20826 md_pbuf_nxt = NULL; 20827 pbuf_idx = pbuf_idx_nxt; 20828 pbuf_idx_nxt = -1; 20829 cur_pld_off = spill; 20830 20831 /* trim out first payload fragment */ 20832 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20833 20834 /* setup second payload fragment */ 20835 PDESC_PLD_SPAN_ADD(pkt_info, 20836 pbuf_idx, /* index */ 20837 md_pbuf->b_rptr, /* start */ 20838 spill); /* len */ 20839 20840 if ((*xmit_tail)->b_next == NULL) { 20841 /* 20842 * Store the lbolt used for RTT 20843 * estimation. We can only record one 20844 * timestamp per mblk so we do it when 20845 * we reach the end of the payload 20846 * buffer. Also we only take a new 20847 * timestamp sample when the previous 20848 * timed data from the same mblk has 20849 * been ack'ed. 20850 */ 20851 (*xmit_tail)->b_prev = local_time; 20852 (*xmit_tail)->b_next = 20853 (mblk_t *)(uintptr_t)first_snxt; 20854 } 20855 20856 first_snxt = *snxt - spill; 20857 20858 /* 20859 * Advance xmit_tail; usable could be 0 by 20860 * the time we got here, but we made sure 20861 * above that we would only spillover to 20862 * the next data block if usable includes 20863 * the spilled-over amount prior to the 20864 * subtraction. Therefore, we are sure 20865 * that xmit_tail->b_cont can't be NULL. 20866 */ 20867 ASSERT((*xmit_tail)->b_cont != NULL); 20868 *xmit_tail = (*xmit_tail)->b_cont; 20869 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20870 (uintptr_t)INT_MAX); 20871 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20872 } else { 20873 cur_pld_off += tcp->tcp_last_sent_len; 20874 } 20875 20876 /* 20877 * Fill in the header using the template header, and 20878 * add options such as time-stamp, ECN and/or SACK, 20879 * as needed. 20880 */ 20881 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20882 (clock_t)local_time, num_sack_blk); 20883 20884 /* take care of some IP header businesses */ 20885 if (af == AF_INET) { 20886 ipha = (ipha_t *)pkt_info->hdr_rptr; 20887 20888 ASSERT(OK_32PTR((uchar_t *)ipha)); 20889 ASSERT(PDESC_HDRL(pkt_info) >= 20890 IP_SIMPLE_HDR_LENGTH); 20891 ASSERT(ipha->ipha_version_and_hdr_length == 20892 IP_SIMPLE_HDR_VERSION); 20893 20894 /* 20895 * Assign ident value for current packet; see 20896 * related comments in ip_wput_ire() about the 20897 * contract private interface with clustering 20898 * group. 20899 */ 20900 clusterwide = B_FALSE; 20901 if (cl_inet_ipident != NULL) { 20902 ASSERT(cl_inet_isclusterwide != NULL); 20903 if ((*cl_inet_isclusterwide)(stack_id, 20904 IPPROTO_IP, AF_INET, 20905 (uint8_t *)(uintptr_t)src, NULL)) { 20906 ipha->ipha_ident = 20907 (*cl_inet_ipident)(stack_id, 20908 IPPROTO_IP, AF_INET, 20909 (uint8_t *)(uintptr_t)src, 20910 (uint8_t *)(uintptr_t)dst, 20911 NULL); 20912 clusterwide = B_TRUE; 20913 } 20914 } 20915 20916 if (!clusterwide) { 20917 ipha->ipha_ident = (uint16_t) 20918 atomic_add_32_nv( 20919 &ire->ire_ident, 1); 20920 } 20921 #ifndef _BIG_ENDIAN 20922 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20923 (ipha->ipha_ident >> 8); 20924 #endif 20925 } else { 20926 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20927 20928 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20929 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20930 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20931 ASSERT(PDESC_HDRL(pkt_info) >= 20932 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20933 TCP_CHECKSUM_SIZE)); 20934 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20935 20936 if (tcp->tcp_ip_forward_progress) { 20937 rconfirm = B_TRUE; 20938 tcp->tcp_ip_forward_progress = B_FALSE; 20939 } 20940 } 20941 20942 /* at least one payload span, and at most two */ 20943 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20944 20945 /* add the packet descriptor to Multidata */ 20946 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20947 KM_NOSLEEP)) == NULL) { 20948 /* 20949 * Any failure other than ENOMEM indicates 20950 * that we have passed in invalid pkt_info 20951 * or parameters to mmd_addpdesc, which must 20952 * not happen. 20953 * 20954 * EINVAL is a result of failure on boundary 20955 * checks against the pkt_info contents. It 20956 * should not happen, and we panic because 20957 * either there's horrible heap corruption, 20958 * and/or programming mistake. 20959 */ 20960 if (err != ENOMEM) { 20961 cmn_err(CE_PANIC, "tcp_multisend: " 20962 "pdesc logic error detected for " 20963 "tcp %p mmd %p pinfo %p (%d)\n", 20964 (void *)tcp, (void *)mmd, 20965 (void *)pkt_info, err); 20966 } 20967 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20968 goto legacy_send; /* out_of_mem */ 20969 } 20970 ASSERT(pkt != NULL); 20971 20972 /* calculate IP header and TCP checksums */ 20973 if (af == AF_INET) { 20974 /* calculate pseudo-header checksum */ 20975 cksum = (dst >> 16) + (dst & 0xFFFF) + 20976 (src >> 16) + (src & 0xFFFF); 20977 20978 /* offset for TCP header checksum */ 20979 up = IPH_TCPH_CHECKSUMP(ipha, 20980 IP_SIMPLE_HDR_LENGTH); 20981 } else { 20982 up = (uint16_t *)&ip6h->ip6_src; 20983 20984 /* calculate pseudo-header checksum */ 20985 cksum = up[0] + up[1] + up[2] + up[3] + 20986 up[4] + up[5] + up[6] + up[7] + 20987 up[8] + up[9] + up[10] + up[11] + 20988 up[12] + up[13] + up[14] + up[15]; 20989 20990 /* Fold the initial sum */ 20991 cksum = (cksum & 0xffff) + (cksum >> 16); 20992 20993 up = (uint16_t *)(((uchar_t *)ip6h) + 20994 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20995 } 20996 20997 if (hwcksum_flags & HCK_FULLCKSUM) { 20998 /* clear checksum field for hardware */ 20999 *up = 0; 21000 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 21001 uint32_t sum; 21002 21003 /* pseudo-header checksumming */ 21004 sum = *up + cksum + IP_TCP_CSUM_COMP; 21005 sum = (sum & 0xFFFF) + (sum >> 16); 21006 *up = (sum & 0xFFFF) + (sum >> 16); 21007 } else { 21008 /* software checksumming */ 21009 TCP_STAT(tcps, tcp_out_sw_cksum); 21010 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21011 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21012 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21013 cksum + IP_TCP_CSUM_COMP); 21014 if (*up == 0) 21015 *up = 0xFFFF; 21016 } 21017 21018 /* IPv4 header checksum */ 21019 if (af == AF_INET) { 21020 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21021 ipha->ipha_hdr_checksum = 0; 21022 } else { 21023 IP_HDR_CKSUM(ipha, cksum, 21024 ((uint32_t *)ipha)[0], 21025 ((uint16_t *)ipha)[4]); 21026 } 21027 } 21028 21029 if (af == AF_INET && 21030 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21031 af == AF_INET6 && 21032 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21033 mblk_t *mp, *mp1; 21034 uchar_t *hdr_rptr, *hdr_wptr; 21035 uchar_t *pld_rptr, *pld_wptr; 21036 21037 /* 21038 * We reconstruct a pseudo packet for the hooks 21039 * framework using mmd_transform_link(). 21040 * If it is a split packet we pullup the 21041 * payload. FW_HOOKS expects a pkt comprising 21042 * of two mblks: a header and the payload. 21043 */ 21044 if ((mp = mmd_transform_link(pkt)) == NULL) { 21045 TCP_STAT(tcps, tcp_mdt_allocfail); 21046 goto legacy_send; 21047 } 21048 21049 if (pkt_info->pld_cnt > 1) { 21050 /* split payload, more than one pld */ 21051 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21052 NULL) { 21053 freemsg(mp); 21054 TCP_STAT(tcps, 21055 tcp_mdt_allocfail); 21056 goto legacy_send; 21057 } 21058 freemsg(mp->b_cont); 21059 mp->b_cont = mp1; 21060 } else { 21061 mp1 = mp->b_cont; 21062 } 21063 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21064 21065 /* 21066 * Remember the message offsets. This is so we 21067 * can detect changes when we return from the 21068 * FW_HOOKS callbacks. 21069 */ 21070 hdr_rptr = mp->b_rptr; 21071 hdr_wptr = mp->b_wptr; 21072 pld_rptr = mp->b_cont->b_rptr; 21073 pld_wptr = mp->b_cont->b_wptr; 21074 21075 if (af == AF_INET) { 21076 DTRACE_PROBE4( 21077 ip4__physical__out__start, 21078 ill_t *, NULL, 21079 ill_t *, ill, 21080 ipha_t *, ipha, 21081 mblk_t *, mp); 21082 FW_HOOKS( 21083 ipst->ips_ip4_physical_out_event, 21084 ipst->ips_ipv4firewall_physical_out, 21085 NULL, ill, ipha, mp, mp, 0, ipst); 21086 DTRACE_PROBE1( 21087 ip4__physical__out__end, 21088 mblk_t *, mp); 21089 } else { 21090 DTRACE_PROBE4( 21091 ip6__physical__out_start, 21092 ill_t *, NULL, 21093 ill_t *, ill, 21094 ip6_t *, ip6h, 21095 mblk_t *, mp); 21096 FW_HOOKS6( 21097 ipst->ips_ip6_physical_out_event, 21098 ipst->ips_ipv6firewall_physical_out, 21099 NULL, ill, ip6h, mp, mp, 0, ipst); 21100 DTRACE_PROBE1( 21101 ip6__physical__out__end, 21102 mblk_t *, mp); 21103 } 21104 21105 if (mp == NULL || 21106 (mp1 = mp->b_cont) == NULL || 21107 mp->b_rptr != hdr_rptr || 21108 mp->b_wptr != hdr_wptr || 21109 mp1->b_rptr != pld_rptr || 21110 mp1->b_wptr != pld_wptr || 21111 mp1->b_cont != NULL) { 21112 /* 21113 * We abandon multidata processing and 21114 * return to the normal path, either 21115 * when a packet is blocked, or when 21116 * the boundaries of header buffer or 21117 * payload buffer have been changed by 21118 * FW_HOOKS[6]. 21119 */ 21120 if (mp != NULL) 21121 freemsg(mp); 21122 goto legacy_send; 21123 } 21124 /* Finished with the pseudo packet */ 21125 freemsg(mp); 21126 } 21127 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21128 ill, ipha, ip6h); 21129 /* advance header offset */ 21130 cur_hdr_off += hdr_frag_sz; 21131 21132 obbytes += tcp->tcp_last_sent_len; 21133 ++obsegs; 21134 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21135 *tail_unsent > 0); 21136 21137 if ((*xmit_tail)->b_next == NULL) { 21138 /* 21139 * Store the lbolt used for RTT estimation. We can only 21140 * record one timestamp per mblk so we do it when we 21141 * reach the end of the payload buffer. Also we only 21142 * take a new timestamp sample when the previous timed 21143 * data from the same mblk has been ack'ed. 21144 */ 21145 (*xmit_tail)->b_prev = local_time; 21146 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21147 } 21148 21149 ASSERT(*tail_unsent >= 0); 21150 if (*tail_unsent > 0) { 21151 /* 21152 * We got here because we broke out of the above 21153 * loop due to of one of the following cases: 21154 * 21155 * 1. len < adjusted MSS (i.e. small), 21156 * 2. Sender SWS avoidance, 21157 * 3. max_pld is zero. 21158 * 21159 * We are done for this Multidata, so trim our 21160 * last payload buffer (if any) accordingly. 21161 */ 21162 if (md_pbuf != NULL) 21163 md_pbuf->b_wptr -= *tail_unsent; 21164 } else if (*usable > 0) { 21165 *xmit_tail = (*xmit_tail)->b_cont; 21166 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21167 (uintptr_t)INT_MAX); 21168 *tail_unsent = (int)MBLKL(*xmit_tail); 21169 add_buffer = B_TRUE; 21170 } 21171 } while (!done && *usable > 0 && num_burst_seg > 0 && 21172 (tcp_mdt_chain || max_pld > 0)); 21173 21174 if (md_mp_head != NULL) { 21175 /* send everything down */ 21176 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21177 &rconfirm); 21178 } 21179 21180 #undef PREP_NEW_MULTIDATA 21181 #undef PREP_NEW_PBUF 21182 #undef IPVER 21183 21184 IRE_REFRELE(ire); 21185 return (0); 21186 } 21187 21188 /* 21189 * A wrapper function for sending one or more Multidata messages down to 21190 * the module below ip; this routine does not release the reference of the 21191 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21192 */ 21193 static void 21194 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21195 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21196 { 21197 uint64_t delta; 21198 nce_t *nce; 21199 tcp_stack_t *tcps = tcp->tcp_tcps; 21200 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21201 21202 ASSERT(ire != NULL && ill != NULL); 21203 ASSERT(ire->ire_stq != NULL); 21204 ASSERT(md_mp_head != NULL); 21205 ASSERT(rconfirm != NULL); 21206 21207 /* adjust MIBs and IRE timestamp */ 21208 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21209 tcp->tcp_obsegs += obsegs; 21210 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21211 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21212 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21213 21214 if (tcp->tcp_ipversion == IPV4_VERSION) { 21215 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21216 } else { 21217 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21218 } 21219 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21220 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21221 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21222 21223 ire->ire_ob_pkt_count += obsegs; 21224 if (ire->ire_ipif != NULL) 21225 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21226 ire->ire_last_used_time = lbolt; 21227 21228 if (ipst->ips_ipobs_enabled) { 21229 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21230 pdesc_t *dl_pkt; 21231 pdescinfo_t pinfo; 21232 mblk_t *nmp; 21233 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21234 21235 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21236 (dl_pkt != NULL); 21237 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21238 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21239 continue; 21240 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21241 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21242 freemsg(nmp); 21243 } 21244 } 21245 21246 /* send it down */ 21247 putnext(ire->ire_stq, md_mp_head); 21248 21249 /* we're done for TCP/IPv4 */ 21250 if (tcp->tcp_ipversion == IPV4_VERSION) 21251 return; 21252 21253 nce = ire->ire_nce; 21254 21255 ASSERT(nce != NULL); 21256 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21257 ASSERT(nce->nce_state != ND_INCOMPLETE); 21258 21259 /* reachability confirmation? */ 21260 if (*rconfirm) { 21261 nce->nce_last = TICK_TO_MSEC(lbolt64); 21262 if (nce->nce_state != ND_REACHABLE) { 21263 mutex_enter(&nce->nce_lock); 21264 nce->nce_state = ND_REACHABLE; 21265 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21266 mutex_exit(&nce->nce_lock); 21267 (void) untimeout(nce->nce_timeout_id); 21268 if (ip_debug > 2) { 21269 /* ip1dbg */ 21270 pr_addr_dbg("tcp_multisend_data: state " 21271 "for %s changed to REACHABLE\n", 21272 AF_INET6, &ire->ire_addr_v6); 21273 } 21274 } 21275 /* reset transport reachability confirmation */ 21276 *rconfirm = B_FALSE; 21277 } 21278 21279 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21280 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21281 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21282 21283 if (delta > (uint64_t)ill->ill_reachable_time) { 21284 mutex_enter(&nce->nce_lock); 21285 switch (nce->nce_state) { 21286 case ND_REACHABLE: 21287 case ND_STALE: 21288 /* 21289 * ND_REACHABLE is identical to ND_STALE in this 21290 * specific case. If reachable time has expired for 21291 * this neighbor (delta is greater than reachable 21292 * time), conceptually, the neighbor cache is no 21293 * longer in REACHABLE state, but already in STALE 21294 * state. So the correct transition here is to 21295 * ND_DELAY. 21296 */ 21297 nce->nce_state = ND_DELAY; 21298 mutex_exit(&nce->nce_lock); 21299 NDP_RESTART_TIMER(nce, 21300 ipst->ips_delay_first_probe_time); 21301 if (ip_debug > 3) { 21302 /* ip2dbg */ 21303 pr_addr_dbg("tcp_multisend_data: state " 21304 "for %s changed to DELAY\n", 21305 AF_INET6, &ire->ire_addr_v6); 21306 } 21307 break; 21308 case ND_DELAY: 21309 case ND_PROBE: 21310 mutex_exit(&nce->nce_lock); 21311 /* Timers have already started */ 21312 break; 21313 case ND_UNREACHABLE: 21314 /* 21315 * ndp timer has detected that this nce is 21316 * unreachable and initiated deleting this nce 21317 * and all its associated IREs. This is a race 21318 * where we found the ire before it was deleted 21319 * and have just sent out a packet using this 21320 * unreachable nce. 21321 */ 21322 mutex_exit(&nce->nce_lock); 21323 break; 21324 default: 21325 ASSERT(0); 21326 } 21327 } 21328 } 21329 21330 /* 21331 * Derived from tcp_send_data(). 21332 */ 21333 static void 21334 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21335 int num_lso_seg) 21336 { 21337 ipha_t *ipha; 21338 mblk_t *ire_fp_mp; 21339 uint_t ire_fp_mp_len; 21340 uint32_t hcksum_txflags = 0; 21341 ipaddr_t src; 21342 ipaddr_t dst; 21343 uint32_t cksum; 21344 uint16_t *up; 21345 tcp_stack_t *tcps = tcp->tcp_tcps; 21346 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21347 21348 ASSERT(DB_TYPE(mp) == M_DATA); 21349 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21350 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21351 ASSERT(tcp->tcp_connp != NULL); 21352 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21353 21354 ipha = (ipha_t *)mp->b_rptr; 21355 src = ipha->ipha_src; 21356 dst = ipha->ipha_dst; 21357 21358 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21359 21360 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21361 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21362 num_lso_seg); 21363 #ifndef _BIG_ENDIAN 21364 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21365 #endif 21366 if (tcp->tcp_snd_zcopy_aware) { 21367 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21368 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21369 mp = tcp_zcopy_disable(tcp, mp); 21370 } 21371 21372 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21373 ASSERT(ill->ill_hcksum_capab != NULL); 21374 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21375 } 21376 21377 /* 21378 * Since the TCP checksum should be recalculated by h/w, we can just 21379 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21380 * pseudo-header checksum for HCK_PARTIALCKSUM. 21381 * The partial pseudo-header excludes TCP length, that was calculated 21382 * in tcp_send(), so to zero *up before further processing. 21383 */ 21384 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21385 21386 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21387 *up = 0; 21388 21389 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21390 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21391 21392 /* 21393 * Append LSO flags and mss to the mp. 21394 */ 21395 lso_info_set(mp, mss, HW_LSO); 21396 21397 ipha->ipha_fragment_offset_and_flags |= 21398 (uint32_t)htons(ire->ire_frag_flag); 21399 21400 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21401 ire_fp_mp_len = MBLKL(ire_fp_mp); 21402 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21403 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21404 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21405 21406 UPDATE_OB_PKT_COUNT(ire); 21407 ire->ire_last_used_time = lbolt; 21408 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21409 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21410 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21411 ntohs(ipha->ipha_length)); 21412 21413 DTRACE_PROBE4(ip4__physical__out__start, 21414 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 21415 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21416 ipst->ips_ipv4firewall_physical_out, NULL, 21417 ill, ipha, mp, mp, 0, ipst); 21418 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21419 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 21420 21421 if (mp != NULL) { 21422 if (ipst->ips_ipobs_enabled) { 21423 zoneid_t szone; 21424 21425 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 21426 ipst, ALL_ZONES); 21427 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21428 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 21429 } 21430 21431 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 21432 } 21433 } 21434 21435 /* 21436 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21437 * scheme, and returns one of the following: 21438 * 21439 * -1 = failed allocation. 21440 * 0 = success; burst count reached, or usable send window is too small, 21441 * and that we'd rather wait until later before sending again. 21442 * 1 = success; we are called from tcp_multisend(), and both usable send 21443 * window and tail_unsent are greater than the MDT threshold, and thus 21444 * Multidata Transmit should be used instead. 21445 */ 21446 static int 21447 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21448 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21449 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21450 const int mdt_thres) 21451 { 21452 int num_burst_seg = tcp->tcp_snd_burst; 21453 ire_t *ire = NULL; 21454 ill_t *ill = NULL; 21455 mblk_t *ire_fp_mp = NULL; 21456 uint_t ire_fp_mp_len = 0; 21457 int num_lso_seg = 1; 21458 uint_t lso_usable; 21459 boolean_t do_lso_send = B_FALSE; 21460 tcp_stack_t *tcps = tcp->tcp_tcps; 21461 21462 /* 21463 * Check LSO capability before any further work. And the similar check 21464 * need to be done in for(;;) loop. 21465 * LSO will be deployed when therer is more than one mss of available 21466 * data and a burst transmission is allowed. 21467 */ 21468 if (tcp->tcp_lso && 21469 (tcp->tcp_valid_bits == 0 || 21470 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21471 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21472 /* 21473 * Try to find usable IRE/ILL and do basic check to the ILL. 21474 * Double check LSO usability before going further, since the 21475 * underlying interface could have been changed. In case of any 21476 * change of LSO capability, set tcp_ire_ill_check_done to 21477 * B_FALSE to force to check the ILL with the next send. 21478 */ 21479 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 21480 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 21481 /* 21482 * Enable LSO with this transmission. 21483 * Since IRE has been hold in tcp_send_find_ire_ill(), 21484 * IRE_REFRELE(ire) should be called before return. 21485 */ 21486 do_lso_send = B_TRUE; 21487 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21488 ire_fp_mp_len = MBLKL(ire_fp_mp); 21489 /* Round up to multiple of 4 */ 21490 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21491 } else { 21492 tcp->tcp_lso = B_FALSE; 21493 tcp->tcp_ire_ill_check_done = B_FALSE; 21494 do_lso_send = B_FALSE; 21495 ill = NULL; 21496 } 21497 } 21498 21499 for (;;) { 21500 struct datab *db; 21501 tcph_t *tcph; 21502 uint32_t sum; 21503 mblk_t *mp, *mp1; 21504 uchar_t *rptr; 21505 int len; 21506 21507 /* 21508 * If we're called by tcp_multisend(), and the amount of 21509 * sendable data as well as the size of current xmit_tail 21510 * is beyond the MDT threshold, return to the caller and 21511 * let the large data transmit be done using MDT. 21512 */ 21513 if (*usable > 0 && *usable > mdt_thres && 21514 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21515 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21516 ASSERT(tcp->tcp_mdt); 21517 return (1); /* success; do large send */ 21518 } 21519 21520 if (num_burst_seg == 0) 21521 break; /* success; burst count reached */ 21522 21523 /* 21524 * Calculate the maximum payload length we can send in *one* 21525 * time. 21526 */ 21527 if (do_lso_send) { 21528 /* 21529 * Check whether need to do LSO any more. 21530 */ 21531 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21532 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21533 lso_usable = MIN(lso_usable, 21534 num_burst_seg * mss); 21535 21536 num_lso_seg = lso_usable / mss; 21537 if (lso_usable % mss) { 21538 num_lso_seg++; 21539 tcp->tcp_last_sent_len = (ushort_t) 21540 (lso_usable % mss); 21541 } else { 21542 tcp->tcp_last_sent_len = (ushort_t)mss; 21543 } 21544 } else { 21545 do_lso_send = B_FALSE; 21546 num_lso_seg = 1; 21547 lso_usable = mss; 21548 } 21549 } 21550 21551 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21552 21553 /* 21554 * Adjust num_burst_seg here. 21555 */ 21556 num_burst_seg -= num_lso_seg; 21557 21558 len = mss; 21559 if (len > *usable) { 21560 ASSERT(do_lso_send == B_FALSE); 21561 21562 len = *usable; 21563 if (len <= 0) { 21564 /* Terminate the loop */ 21565 break; /* success; too small */ 21566 } 21567 /* 21568 * Sender silly-window avoidance. 21569 * Ignore this if we are going to send a 21570 * zero window probe out. 21571 * 21572 * TODO: force data into microscopic window? 21573 * ==> (!pushed || (unsent > usable)) 21574 */ 21575 if (len < (tcp->tcp_max_swnd >> 1) && 21576 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21577 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21578 len == 1) && (! tcp->tcp_zero_win_probe)) { 21579 /* 21580 * If the retransmit timer is not running 21581 * we start it so that we will retransmit 21582 * in the case when the the receiver has 21583 * decremented the window. 21584 */ 21585 if (*snxt == tcp->tcp_snxt && 21586 *snxt == tcp->tcp_suna) { 21587 /* 21588 * We are not supposed to send 21589 * anything. So let's wait a little 21590 * bit longer before breaking SWS 21591 * avoidance. 21592 * 21593 * What should the value be? 21594 * Suggestion: MAX(init rexmit time, 21595 * tcp->tcp_rto) 21596 */ 21597 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21598 } 21599 break; /* success; too small */ 21600 } 21601 } 21602 21603 tcph = tcp->tcp_tcph; 21604 21605 /* 21606 * The reason to adjust len here is that we need to set flags 21607 * and calculate checksum. 21608 */ 21609 if (do_lso_send) 21610 len = lso_usable; 21611 21612 *usable -= len; /* Approximate - can be adjusted later */ 21613 if (*usable > 0) 21614 tcph->th_flags[0] = TH_ACK; 21615 else 21616 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21617 21618 /* 21619 * Prime pump for IP's checksumming on our behalf 21620 * Include the adjustment for a source route if any. 21621 */ 21622 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21623 sum = (sum >> 16) + (sum & 0xFFFF); 21624 U16_TO_ABE16(sum, tcph->th_sum); 21625 21626 U32_TO_ABE32(*snxt, tcph->th_seq); 21627 21628 /* 21629 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21630 * set. For the case when TCP_FSS_VALID is the only valid 21631 * bit (normal active close), branch off only when we think 21632 * that the FIN flag needs to be set. Note for this case, 21633 * that (snxt + len) may not reflect the actual seg_len, 21634 * as len may be further reduced in tcp_xmit_mp(). If len 21635 * gets modified, we will end up here again. 21636 */ 21637 if (tcp->tcp_valid_bits != 0 && 21638 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21639 ((*snxt + len) == tcp->tcp_fss))) { 21640 uchar_t *prev_rptr; 21641 uint32_t prev_snxt = tcp->tcp_snxt; 21642 21643 if (*tail_unsent == 0) { 21644 ASSERT((*xmit_tail)->b_cont != NULL); 21645 *xmit_tail = (*xmit_tail)->b_cont; 21646 prev_rptr = (*xmit_tail)->b_rptr; 21647 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21648 (*xmit_tail)->b_rptr); 21649 } else { 21650 prev_rptr = (*xmit_tail)->b_rptr; 21651 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21652 *tail_unsent; 21653 } 21654 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21655 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21656 /* Restore tcp_snxt so we get amount sent right. */ 21657 tcp->tcp_snxt = prev_snxt; 21658 if (prev_rptr == (*xmit_tail)->b_rptr) { 21659 /* 21660 * If the previous timestamp is still in use, 21661 * don't stomp on it. 21662 */ 21663 if ((*xmit_tail)->b_next == NULL) { 21664 (*xmit_tail)->b_prev = local_time; 21665 (*xmit_tail)->b_next = 21666 (mblk_t *)(uintptr_t)(*snxt); 21667 } 21668 } else 21669 (*xmit_tail)->b_rptr = prev_rptr; 21670 21671 if (mp == NULL) { 21672 if (ire != NULL) 21673 IRE_REFRELE(ire); 21674 return (-1); 21675 } 21676 mp1 = mp->b_cont; 21677 21678 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21679 tcp->tcp_last_sent_len = (ushort_t)len; 21680 while (mp1->b_cont) { 21681 *xmit_tail = (*xmit_tail)->b_cont; 21682 (*xmit_tail)->b_prev = local_time; 21683 (*xmit_tail)->b_next = 21684 (mblk_t *)(uintptr_t)(*snxt); 21685 mp1 = mp1->b_cont; 21686 } 21687 *snxt += len; 21688 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21689 BUMP_LOCAL(tcp->tcp_obsegs); 21690 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21691 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21692 tcp_send_data(tcp, q, mp); 21693 continue; 21694 } 21695 21696 *snxt += len; /* Adjust later if we don't send all of len */ 21697 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21698 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21699 21700 if (*tail_unsent) { 21701 /* Are the bytes above us in flight? */ 21702 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21703 if (rptr != (*xmit_tail)->b_rptr) { 21704 *tail_unsent -= len; 21705 if (len <= mss) /* LSO is unusable */ 21706 tcp->tcp_last_sent_len = (ushort_t)len; 21707 len += tcp_hdr_len; 21708 if (tcp->tcp_ipversion == IPV4_VERSION) 21709 tcp->tcp_ipha->ipha_length = htons(len); 21710 else 21711 tcp->tcp_ip6h->ip6_plen = 21712 htons(len - 21713 ((char *)&tcp->tcp_ip6h[1] - 21714 tcp->tcp_iphc)); 21715 mp = dupb(*xmit_tail); 21716 if (mp == NULL) { 21717 if (ire != NULL) 21718 IRE_REFRELE(ire); 21719 return (-1); /* out_of_mem */ 21720 } 21721 mp->b_rptr = rptr; 21722 /* 21723 * If the old timestamp is no longer in use, 21724 * sample a new timestamp now. 21725 */ 21726 if ((*xmit_tail)->b_next == NULL) { 21727 (*xmit_tail)->b_prev = local_time; 21728 (*xmit_tail)->b_next = 21729 (mblk_t *)(uintptr_t)(*snxt-len); 21730 } 21731 goto must_alloc; 21732 } 21733 } else { 21734 *xmit_tail = (*xmit_tail)->b_cont; 21735 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21736 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21737 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21738 (*xmit_tail)->b_rptr); 21739 } 21740 21741 (*xmit_tail)->b_prev = local_time; 21742 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21743 21744 *tail_unsent -= len; 21745 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21746 tcp->tcp_last_sent_len = (ushort_t)len; 21747 21748 len += tcp_hdr_len; 21749 if (tcp->tcp_ipversion == IPV4_VERSION) 21750 tcp->tcp_ipha->ipha_length = htons(len); 21751 else 21752 tcp->tcp_ip6h->ip6_plen = htons(len - 21753 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21754 21755 mp = dupb(*xmit_tail); 21756 if (mp == NULL) { 21757 if (ire != NULL) 21758 IRE_REFRELE(ire); 21759 return (-1); /* out_of_mem */ 21760 } 21761 21762 len = tcp_hdr_len; 21763 /* 21764 * There are four reasons to allocate a new hdr mblk: 21765 * 1) The bytes above us are in use by another packet 21766 * 2) We don't have good alignment 21767 * 3) The mblk is being shared 21768 * 4) We don't have enough room for a header 21769 */ 21770 rptr = mp->b_rptr - len; 21771 if (!OK_32PTR(rptr) || 21772 ((db = mp->b_datap), db->db_ref != 2) || 21773 rptr < db->db_base + ire_fp_mp_len) { 21774 /* NOTE: we assume allocb returns an OK_32PTR */ 21775 21776 must_alloc:; 21777 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21778 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21779 if (mp1 == NULL) { 21780 freemsg(mp); 21781 if (ire != NULL) 21782 IRE_REFRELE(ire); 21783 return (-1); /* out_of_mem */ 21784 } 21785 mp1->b_cont = mp; 21786 mp = mp1; 21787 /* Leave room for Link Level header */ 21788 len = tcp_hdr_len; 21789 rptr = 21790 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21791 mp->b_wptr = &rptr[len]; 21792 } 21793 21794 /* 21795 * Fill in the header using the template header, and add 21796 * options such as time-stamp, ECN and/or SACK, as needed. 21797 */ 21798 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21799 21800 mp->b_rptr = rptr; 21801 21802 if (*tail_unsent) { 21803 int spill = *tail_unsent; 21804 21805 mp1 = mp->b_cont; 21806 if (mp1 == NULL) 21807 mp1 = mp; 21808 21809 /* 21810 * If we're a little short, tack on more mblks until 21811 * there is no more spillover. 21812 */ 21813 while (spill < 0) { 21814 mblk_t *nmp; 21815 int nmpsz; 21816 21817 nmp = (*xmit_tail)->b_cont; 21818 nmpsz = MBLKL(nmp); 21819 21820 /* 21821 * Excess data in mblk; can we split it? 21822 * If MDT is enabled for the connection, 21823 * keep on splitting as this is a transient 21824 * send path. 21825 */ 21826 if (!do_lso_send && !tcp->tcp_mdt && 21827 (spill + nmpsz > 0)) { 21828 /* 21829 * Don't split if stream head was 21830 * told to break up larger writes 21831 * into smaller ones. 21832 */ 21833 if (tcp->tcp_maxpsz > 0) 21834 break; 21835 21836 /* 21837 * Next mblk is less than SMSS/2 21838 * rounded up to nearest 64-byte; 21839 * let it get sent as part of the 21840 * next segment. 21841 */ 21842 if (tcp->tcp_localnet && 21843 !tcp->tcp_cork && 21844 (nmpsz < roundup((mss >> 1), 64))) 21845 break; 21846 } 21847 21848 *xmit_tail = nmp; 21849 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21850 /* Stash for rtt use later */ 21851 (*xmit_tail)->b_prev = local_time; 21852 (*xmit_tail)->b_next = 21853 (mblk_t *)(uintptr_t)(*snxt - len); 21854 mp1->b_cont = dupb(*xmit_tail); 21855 mp1 = mp1->b_cont; 21856 21857 spill += nmpsz; 21858 if (mp1 == NULL) { 21859 *tail_unsent = spill; 21860 freemsg(mp); 21861 if (ire != NULL) 21862 IRE_REFRELE(ire); 21863 return (-1); /* out_of_mem */ 21864 } 21865 } 21866 21867 /* Trim back any surplus on the last mblk */ 21868 if (spill >= 0) { 21869 mp1->b_wptr -= spill; 21870 *tail_unsent = spill; 21871 } else { 21872 /* 21873 * We did not send everything we could in 21874 * order to remain within the b_cont limit. 21875 */ 21876 *usable -= spill; 21877 *snxt += spill; 21878 tcp->tcp_last_sent_len += spill; 21879 UPDATE_MIB(&tcps->tcps_mib, 21880 tcpOutDataBytes, spill); 21881 /* 21882 * Adjust the checksum 21883 */ 21884 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21885 sum += spill; 21886 sum = (sum >> 16) + (sum & 0xFFFF); 21887 U16_TO_ABE16(sum, tcph->th_sum); 21888 if (tcp->tcp_ipversion == IPV4_VERSION) { 21889 sum = ntohs( 21890 ((ipha_t *)rptr)->ipha_length) + 21891 spill; 21892 ((ipha_t *)rptr)->ipha_length = 21893 htons(sum); 21894 } else { 21895 sum = ntohs( 21896 ((ip6_t *)rptr)->ip6_plen) + 21897 spill; 21898 ((ip6_t *)rptr)->ip6_plen = 21899 htons(sum); 21900 } 21901 *tail_unsent = 0; 21902 } 21903 } 21904 if (tcp->tcp_ip_forward_progress) { 21905 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21906 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21907 tcp->tcp_ip_forward_progress = B_FALSE; 21908 } 21909 21910 if (do_lso_send) { 21911 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21912 num_lso_seg); 21913 tcp->tcp_obsegs += num_lso_seg; 21914 21915 TCP_STAT(tcps, tcp_lso_times); 21916 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21917 } else { 21918 tcp_send_data(tcp, q, mp); 21919 BUMP_LOCAL(tcp->tcp_obsegs); 21920 } 21921 } 21922 21923 if (ire != NULL) 21924 IRE_REFRELE(ire); 21925 return (0); 21926 } 21927 21928 /* Unlink and return any mblk that looks like it contains a MDT info */ 21929 static mblk_t * 21930 tcp_mdt_info_mp(mblk_t *mp) 21931 { 21932 mblk_t *prev_mp; 21933 21934 for (;;) { 21935 prev_mp = mp; 21936 /* no more to process? */ 21937 if ((mp = mp->b_cont) == NULL) 21938 break; 21939 21940 switch (DB_TYPE(mp)) { 21941 case M_CTL: 21942 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21943 continue; 21944 ASSERT(prev_mp != NULL); 21945 prev_mp->b_cont = mp->b_cont; 21946 mp->b_cont = NULL; 21947 return (mp); 21948 default: 21949 break; 21950 } 21951 } 21952 return (mp); 21953 } 21954 21955 /* MDT info update routine, called when IP notifies us about MDT */ 21956 static void 21957 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21958 { 21959 boolean_t prev_state; 21960 tcp_stack_t *tcps = tcp->tcp_tcps; 21961 21962 /* 21963 * IP is telling us to abort MDT on this connection? We know 21964 * this because the capability is only turned off when IP 21965 * encounters some pathological cases, e.g. link-layer change 21966 * where the new driver doesn't support MDT, or in situation 21967 * where MDT usage on the link-layer has been switched off. 21968 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21969 * if the link-layer doesn't support MDT, and if it does, it 21970 * will indicate that the feature is to be turned on. 21971 */ 21972 prev_state = tcp->tcp_mdt; 21973 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21974 if (!tcp->tcp_mdt && !first) { 21975 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21976 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21977 (void *)tcp->tcp_connp)); 21978 } 21979 21980 /* 21981 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21982 * so disable MDT otherwise. The checks are done here 21983 * and in tcp_wput_data(). 21984 */ 21985 if (tcp->tcp_mdt && 21986 (tcp->tcp_ipversion == IPV4_VERSION && 21987 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21988 (tcp->tcp_ipversion == IPV6_VERSION && 21989 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21990 tcp->tcp_mdt = B_FALSE; 21991 21992 if (tcp->tcp_mdt) { 21993 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21994 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21995 "version (%d), expected version is %d", 21996 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21997 tcp->tcp_mdt = B_FALSE; 21998 return; 21999 } 22000 22001 /* 22002 * We need the driver to be able to handle at least three 22003 * spans per packet in order for tcp MDT to be utilized. 22004 * The first is for the header portion, while the rest are 22005 * needed to handle a packet that straddles across two 22006 * virtually non-contiguous buffers; a typical tcp packet 22007 * therefore consists of only two spans. Note that we take 22008 * a zero as "don't care". 22009 */ 22010 if (mdt_capab->ill_mdt_span_limit > 0 && 22011 mdt_capab->ill_mdt_span_limit < 3) { 22012 tcp->tcp_mdt = B_FALSE; 22013 return; 22014 } 22015 22016 /* a zero means driver wants default value */ 22017 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22018 tcps->tcps_mdt_max_pbufs); 22019 if (tcp->tcp_mdt_max_pld == 0) 22020 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22021 22022 /* ensure 32-bit alignment */ 22023 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22024 mdt_capab->ill_mdt_hdr_head), 4); 22025 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22026 mdt_capab->ill_mdt_hdr_tail), 4); 22027 22028 if (!first && !prev_state) { 22029 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22030 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22031 (void *)tcp->tcp_connp)); 22032 } 22033 } 22034 } 22035 22036 /* Unlink and return any mblk that looks like it contains a LSO info */ 22037 static mblk_t * 22038 tcp_lso_info_mp(mblk_t *mp) 22039 { 22040 mblk_t *prev_mp; 22041 22042 for (;;) { 22043 prev_mp = mp; 22044 /* no more to process? */ 22045 if ((mp = mp->b_cont) == NULL) 22046 break; 22047 22048 switch (DB_TYPE(mp)) { 22049 case M_CTL: 22050 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22051 continue; 22052 ASSERT(prev_mp != NULL); 22053 prev_mp->b_cont = mp->b_cont; 22054 mp->b_cont = NULL; 22055 return (mp); 22056 default: 22057 break; 22058 } 22059 } 22060 22061 return (mp); 22062 } 22063 22064 /* LSO info update routine, called when IP notifies us about LSO */ 22065 static void 22066 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22067 { 22068 tcp_stack_t *tcps = tcp->tcp_tcps; 22069 22070 /* 22071 * IP is telling us to abort LSO on this connection? We know 22072 * this because the capability is only turned off when IP 22073 * encounters some pathological cases, e.g. link-layer change 22074 * where the new NIC/driver doesn't support LSO, or in situation 22075 * where LSO usage on the link-layer has been switched off. 22076 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22077 * if the link-layer doesn't support LSO, and if it does, it 22078 * will indicate that the feature is to be turned on. 22079 */ 22080 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22081 TCP_STAT(tcps, tcp_lso_enabled); 22082 22083 /* 22084 * We currently only support LSO on simple TCP/IPv4, 22085 * so disable LSO otherwise. The checks are done here 22086 * and in tcp_wput_data(). 22087 */ 22088 if (tcp->tcp_lso && 22089 (tcp->tcp_ipversion == IPV4_VERSION && 22090 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22091 (tcp->tcp_ipversion == IPV6_VERSION)) { 22092 tcp->tcp_lso = B_FALSE; 22093 TCP_STAT(tcps, tcp_lso_disabled); 22094 } else { 22095 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22096 lso_capab->ill_lso_max); 22097 } 22098 } 22099 22100 static void 22101 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22102 { 22103 conn_t *connp = tcp->tcp_connp; 22104 tcp_stack_t *tcps = tcp->tcp_tcps; 22105 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22106 22107 ASSERT(ire != NULL); 22108 22109 /* 22110 * We may be in the fastpath here, and although we essentially do 22111 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22112 * we try to keep things as brief as possible. After all, these 22113 * are only best-effort checks, and we do more thorough ones prior 22114 * to calling tcp_send()/tcp_multisend(). 22115 */ 22116 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22117 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22118 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22119 !(ire->ire_flags & RTF_MULTIRT) && 22120 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22121 CONN_IS_LSO_MD_FASTPATH(connp)) { 22122 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22123 /* Cache the result */ 22124 connp->conn_lso_ok = B_TRUE; 22125 22126 ASSERT(ill->ill_lso_capab != NULL); 22127 if (!ill->ill_lso_capab->ill_lso_on) { 22128 ill->ill_lso_capab->ill_lso_on = 1; 22129 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22130 "LSO for interface %s\n", (void *)connp, 22131 ill->ill_name)); 22132 } 22133 tcp_lso_update(tcp, ill->ill_lso_capab); 22134 } else if (ipst->ips_ip_multidata_outbound && 22135 ILL_MDT_CAPABLE(ill)) { 22136 /* Cache the result */ 22137 connp->conn_mdt_ok = B_TRUE; 22138 22139 ASSERT(ill->ill_mdt_capab != NULL); 22140 if (!ill->ill_mdt_capab->ill_mdt_on) { 22141 ill->ill_mdt_capab->ill_mdt_on = 1; 22142 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22143 "MDT for interface %s\n", (void *)connp, 22144 ill->ill_name)); 22145 } 22146 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22147 } 22148 } 22149 22150 /* 22151 * The goal is to reduce the number of generated tcp segments by 22152 * setting the maxpsz multiplier to 0; this will have an affect on 22153 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22154 * into each packet, up to SMSS bytes. Doing this reduces the number 22155 * of outbound segments and incoming ACKs, thus allowing for better 22156 * network and system performance. In contrast the legacy behavior 22157 * may result in sending less than SMSS size, because the last mblk 22158 * for some packets may have more data than needed to make up SMSS, 22159 * and the legacy code refused to "split" it. 22160 * 22161 * We apply the new behavior on following situations: 22162 * 22163 * 1) Loopback connections, 22164 * 2) Connections in which the remote peer is not on local subnet, 22165 * 3) Local subnet connections over the bge interface (see below). 22166 * 22167 * Ideally, we would like this behavior to apply for interfaces other 22168 * than bge. However, doing so would negatively impact drivers which 22169 * perform dynamic mapping and unmapping of DMA resources, which are 22170 * increased by setting the maxpsz multiplier to 0 (more mblks per 22171 * packet will be generated by tcp). The bge driver does not suffer 22172 * from this, as it copies the mblks into pre-mapped buffers, and 22173 * therefore does not require more I/O resources than before. 22174 * 22175 * Otherwise, this behavior is present on all network interfaces when 22176 * the destination endpoint is non-local, since reducing the number 22177 * of packets in general is good for the network. 22178 * 22179 * TODO We need to remove this hard-coded conditional for bge once 22180 * a better "self-tuning" mechanism, or a way to comprehend 22181 * the driver transmit strategy is devised. Until the solution 22182 * is found and well understood, we live with this hack. 22183 */ 22184 if (!tcp_static_maxpsz && 22185 (tcp->tcp_loopback || !tcp->tcp_localnet || 22186 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22187 /* override the default value */ 22188 tcp->tcp_maxpsz = 0; 22189 22190 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22191 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22192 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22193 } 22194 22195 /* set the stream head parameters accordingly */ 22196 (void) tcp_maxpsz_set(tcp, B_TRUE); 22197 } 22198 22199 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22200 static void 22201 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22202 { 22203 uchar_t fval = *mp->b_rptr; 22204 mblk_t *tail; 22205 queue_t *q = tcp->tcp_wq; 22206 22207 /* TODO: How should flush interact with urgent data? */ 22208 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22209 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22210 /* 22211 * Flush only data that has not yet been put on the wire. If 22212 * we flush data that we have already transmitted, life, as we 22213 * know it, may come to an end. 22214 */ 22215 tail = tcp->tcp_xmit_tail; 22216 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22217 tcp->tcp_xmit_tail_unsent = 0; 22218 tcp->tcp_unsent = 0; 22219 if (tail->b_wptr != tail->b_rptr) 22220 tail = tail->b_cont; 22221 if (tail) { 22222 mblk_t **excess = &tcp->tcp_xmit_head; 22223 for (;;) { 22224 mblk_t *mp1 = *excess; 22225 if (mp1 == tail) 22226 break; 22227 tcp->tcp_xmit_tail = mp1; 22228 tcp->tcp_xmit_last = mp1; 22229 excess = &mp1->b_cont; 22230 } 22231 *excess = NULL; 22232 tcp_close_mpp(&tail); 22233 if (tcp->tcp_snd_zcopy_aware) 22234 tcp_zcopy_notify(tcp); 22235 } 22236 /* 22237 * We have no unsent data, so unsent must be less than 22238 * tcp_xmit_lowater, so re-enable flow. 22239 */ 22240 mutex_enter(&tcp->tcp_non_sq_lock); 22241 if (tcp->tcp_flow_stopped) { 22242 tcp_clrqfull(tcp); 22243 } 22244 mutex_exit(&tcp->tcp_non_sq_lock); 22245 } 22246 /* 22247 * TODO: you can't just flush these, you have to increase rwnd for one 22248 * thing. For another, how should urgent data interact? 22249 */ 22250 if (fval & FLUSHR) { 22251 *mp->b_rptr = fval & ~FLUSHW; 22252 /* XXX */ 22253 qreply(q, mp); 22254 return; 22255 } 22256 freemsg(mp); 22257 } 22258 22259 /* 22260 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22261 * messages. 22262 */ 22263 static void 22264 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22265 { 22266 mblk_t *mp1; 22267 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22268 STRUCT_HANDLE(strbuf, sb); 22269 queue_t *q = tcp->tcp_wq; 22270 int error; 22271 uint_t addrlen; 22272 22273 /* Make sure it is one of ours. */ 22274 switch (iocp->ioc_cmd) { 22275 case TI_GETMYNAME: 22276 case TI_GETPEERNAME: 22277 break; 22278 default: 22279 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22280 return; 22281 } 22282 switch (mi_copy_state(q, mp, &mp1)) { 22283 case -1: 22284 return; 22285 case MI_COPY_CASE(MI_COPY_IN, 1): 22286 break; 22287 case MI_COPY_CASE(MI_COPY_OUT, 1): 22288 /* Copy out the strbuf. */ 22289 mi_copyout(q, mp); 22290 return; 22291 case MI_COPY_CASE(MI_COPY_OUT, 2): 22292 /* All done. */ 22293 mi_copy_done(q, mp, 0); 22294 return; 22295 default: 22296 mi_copy_done(q, mp, EPROTO); 22297 return; 22298 } 22299 /* Check alignment of the strbuf */ 22300 if (!OK_32PTR(mp1->b_rptr)) { 22301 mi_copy_done(q, mp, EINVAL); 22302 return; 22303 } 22304 22305 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22306 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22307 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22308 mi_copy_done(q, mp, EINVAL); 22309 return; 22310 } 22311 22312 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22313 if (mp1 == NULL) 22314 return; 22315 22316 switch (iocp->ioc_cmd) { 22317 case TI_GETMYNAME: 22318 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 22319 break; 22320 case TI_GETPEERNAME: 22321 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22322 break; 22323 } 22324 22325 if (error != 0) { 22326 mi_copy_done(q, mp, error); 22327 } else { 22328 mp1->b_wptr += addrlen; 22329 STRUCT_FSET(sb, len, addrlen); 22330 22331 /* Copy out the address */ 22332 mi_copyout(q, mp); 22333 } 22334 } 22335 22336 static void 22337 tcp_disable_direct_sockfs(tcp_t *tcp) 22338 { 22339 #ifdef _ILP32 22340 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 22341 #else 22342 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22343 #endif 22344 /* 22345 * Insert this socket into the acceptor hash. 22346 * We might need it for T_CONN_RES message 22347 */ 22348 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22349 22350 if (tcp->tcp_fused) { 22351 /* 22352 * This is a fused loopback tcp; disable 22353 * read-side synchronous streams interface 22354 * and drain any queued data. It is okay 22355 * to do this for non-synchronous streams 22356 * fused tcp as well. 22357 */ 22358 tcp_fuse_disable_pair(tcp, B_FALSE); 22359 } 22360 tcp->tcp_issocket = B_FALSE; 22361 tcp->tcp_sodirect = NULL; 22362 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 22363 } 22364 22365 /* 22366 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22367 * messages. 22368 */ 22369 /* ARGSUSED */ 22370 static void 22371 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22372 { 22373 conn_t *connp = (conn_t *)arg; 22374 tcp_t *tcp = connp->conn_tcp; 22375 queue_t *q = tcp->tcp_wq; 22376 struct iocblk *iocp; 22377 22378 ASSERT(DB_TYPE(mp) == M_IOCTL); 22379 /* 22380 * Try and ASSERT the minimum possible references on the 22381 * conn early enough. Since we are executing on write side, 22382 * the connection is obviously not detached and that means 22383 * there is a ref each for TCP and IP. Since we are behind 22384 * the squeue, the minimum references needed are 3. If the 22385 * conn is in classifier hash list, there should be an 22386 * extra ref for that (we check both the possibilities). 22387 */ 22388 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22389 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22390 22391 iocp = (struct iocblk *)mp->b_rptr; 22392 switch (iocp->ioc_cmd) { 22393 case TCP_IOC_DEFAULT_Q: 22394 /* Wants to be the default wq. */ 22395 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22396 iocp->ioc_error = EPERM; 22397 iocp->ioc_count = 0; 22398 mp->b_datap->db_type = M_IOCACK; 22399 qreply(q, mp); 22400 return; 22401 } 22402 tcp_def_q_set(tcp, mp); 22403 return; 22404 case _SIOCSOCKFALLBACK: 22405 /* 22406 * Either sockmod is about to be popped and the socket 22407 * would now be treated as a plain stream, or a module 22408 * is about to be pushed so we could no longer use read- 22409 * side synchronous streams for fused loopback tcp. 22410 * Drain any queued data and disable direct sockfs 22411 * interface from now on. 22412 */ 22413 if (!tcp->tcp_issocket) { 22414 DB_TYPE(mp) = M_IOCNAK; 22415 iocp->ioc_error = EINVAL; 22416 } else { 22417 tcp_disable_direct_sockfs(tcp); 22418 DB_TYPE(mp) = M_IOCACK; 22419 iocp->ioc_error = 0; 22420 } 22421 iocp->ioc_count = 0; 22422 iocp->ioc_rval = 0; 22423 qreply(q, mp); 22424 return; 22425 } 22426 CALL_IP_WPUT(connp, q, mp); 22427 } 22428 22429 /* 22430 * This routine is called by tcp_wput() to handle all TPI requests. 22431 */ 22432 /* ARGSUSED */ 22433 static void 22434 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22435 { 22436 conn_t *connp = (conn_t *)arg; 22437 tcp_t *tcp = connp->conn_tcp; 22438 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22439 uchar_t *rptr; 22440 t_scalar_t type; 22441 cred_t *cr; 22442 22443 /* 22444 * Try and ASSERT the minimum possible references on the 22445 * conn early enough. Since we are executing on write side, 22446 * the connection is obviously not detached and that means 22447 * there is a ref each for TCP and IP. Since we are behind 22448 * the squeue, the minimum references needed are 3. If the 22449 * conn is in classifier hash list, there should be an 22450 * extra ref for that (we check both the possibilities). 22451 */ 22452 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22453 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22454 22455 rptr = mp->b_rptr; 22456 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22457 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22458 type = ((union T_primitives *)rptr)->type; 22459 if (type == T_EXDATA_REQ) { 22460 tcp_output_urgent(connp, mp->b_cont, arg2); 22461 freeb(mp); 22462 } else if (type != T_DATA_REQ) { 22463 goto non_urgent_data; 22464 } else { 22465 /* TODO: options, flags, ... from user */ 22466 /* Set length to zero for reclamation below */ 22467 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22468 freeb(mp); 22469 } 22470 return; 22471 } else { 22472 if (tcp->tcp_debug) { 22473 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22474 "tcp_wput_proto, dropping one..."); 22475 } 22476 freemsg(mp); 22477 return; 22478 } 22479 22480 non_urgent_data: 22481 22482 switch ((int)tprim->type) { 22483 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22484 /* 22485 * save the kssl_ent_t from the next block, and convert this 22486 * back to a normal bind_req. 22487 */ 22488 if (mp->b_cont != NULL) { 22489 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22490 22491 if (tcp->tcp_kssl_ent != NULL) { 22492 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22493 KSSL_NO_PROXY); 22494 tcp->tcp_kssl_ent = NULL; 22495 } 22496 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22497 sizeof (kssl_ent_t)); 22498 kssl_hold_ent(tcp->tcp_kssl_ent); 22499 freemsg(mp->b_cont); 22500 mp->b_cont = NULL; 22501 } 22502 tprim->type = T_BIND_REQ; 22503 22504 /* FALLTHROUGH */ 22505 case O_T_BIND_REQ: /* bind request */ 22506 case T_BIND_REQ: /* new semantics bind request */ 22507 tcp_tpi_bind(tcp, mp); 22508 break; 22509 case T_UNBIND_REQ: /* unbind request */ 22510 tcp_tpi_unbind(tcp, mp); 22511 break; 22512 case O_T_CONN_RES: /* old connection response XXX */ 22513 case T_CONN_RES: /* connection response */ 22514 tcp_tli_accept(tcp, mp); 22515 break; 22516 case T_CONN_REQ: /* connection request */ 22517 tcp_tpi_connect(tcp, mp); 22518 break; 22519 case T_DISCON_REQ: /* disconnect request */ 22520 tcp_disconnect(tcp, mp); 22521 break; 22522 case T_CAPABILITY_REQ: 22523 tcp_capability_req(tcp, mp); /* capability request */ 22524 break; 22525 case T_INFO_REQ: /* information request */ 22526 tcp_info_req(tcp, mp); 22527 break; 22528 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22529 case T_OPTMGMT_REQ: 22530 /* 22531 * Note: no support for snmpcom_req() through new 22532 * T_OPTMGMT_REQ. See comments in ip.c 22533 */ 22534 22535 /* 22536 * All Solaris components should pass a db_credp 22537 * for this TPI message, hence we ASSERT. 22538 * But in case there is some other M_PROTO that looks 22539 * like a TPI message sent by some other kernel 22540 * component, we check and return an error. 22541 */ 22542 cr = msg_getcred(mp, NULL); 22543 ASSERT(cr != NULL); 22544 if (cr == NULL) { 22545 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 22546 return; 22547 } 22548 /* 22549 * If EINPROGRESS is returned, the request has been queued 22550 * for subsequent processing by ip_restart_optmgmt(), which 22551 * will do the CONN_DEC_REF(). 22552 */ 22553 CONN_INC_REF(connp); 22554 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 22555 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22556 B_TRUE) != EINPROGRESS) { 22557 CONN_DEC_REF(connp); 22558 } 22559 } else { 22560 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22561 B_TRUE) != EINPROGRESS) { 22562 CONN_DEC_REF(connp); 22563 } 22564 } 22565 break; 22566 22567 case T_UNITDATA_REQ: /* unitdata request */ 22568 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22569 break; 22570 case T_ORDREL_REQ: /* orderly release req */ 22571 freemsg(mp); 22572 22573 if (tcp->tcp_fused) 22574 tcp_unfuse(tcp); 22575 22576 if (tcp_xmit_end(tcp) != 0) { 22577 /* 22578 * We were crossing FINs and got a reset from 22579 * the other side. Just ignore it. 22580 */ 22581 if (tcp->tcp_debug) { 22582 (void) strlog(TCP_MOD_ID, 0, 1, 22583 SL_ERROR|SL_TRACE, 22584 "tcp_wput_proto, T_ORDREL_REQ out of " 22585 "state %s", 22586 tcp_display(tcp, NULL, 22587 DISP_ADDR_AND_PORT)); 22588 } 22589 } 22590 break; 22591 case T_ADDR_REQ: 22592 tcp_addr_req(tcp, mp); 22593 break; 22594 default: 22595 if (tcp->tcp_debug) { 22596 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22597 "tcp_wput_proto, bogus TPI msg, type %d", 22598 tprim->type); 22599 } 22600 /* 22601 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22602 * to recover. 22603 */ 22604 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22605 break; 22606 } 22607 } 22608 22609 /* 22610 * The TCP write service routine should never be called... 22611 */ 22612 /* ARGSUSED */ 22613 static void 22614 tcp_wsrv(queue_t *q) 22615 { 22616 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22617 22618 TCP_STAT(tcps, tcp_wsrv_called); 22619 } 22620 22621 /* Non overlapping byte exchanger */ 22622 static void 22623 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22624 { 22625 uchar_t uch; 22626 22627 while (len-- > 0) { 22628 uch = a[len]; 22629 a[len] = b[len]; 22630 b[len] = uch; 22631 } 22632 } 22633 22634 /* 22635 * Send out a control packet on the tcp connection specified. This routine 22636 * is typically called where we need a simple ACK or RST generated. 22637 */ 22638 static void 22639 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22640 { 22641 uchar_t *rptr; 22642 tcph_t *tcph; 22643 ipha_t *ipha = NULL; 22644 ip6_t *ip6h = NULL; 22645 uint32_t sum; 22646 int tcp_hdr_len; 22647 int tcp_ip_hdr_len; 22648 mblk_t *mp; 22649 tcp_stack_t *tcps = tcp->tcp_tcps; 22650 22651 /* 22652 * Save sum for use in source route later. 22653 */ 22654 ASSERT(tcp != NULL); 22655 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22656 tcp_hdr_len = tcp->tcp_hdr_len; 22657 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22658 22659 /* If a text string is passed in with the request, pass it to strlog. */ 22660 if (str != NULL && tcp->tcp_debug) { 22661 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22662 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22663 str, seq, ack, ctl); 22664 } 22665 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22666 BPRI_MED); 22667 if (mp == NULL) { 22668 return; 22669 } 22670 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22671 mp->b_rptr = rptr; 22672 mp->b_wptr = &rptr[tcp_hdr_len]; 22673 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22674 22675 if (tcp->tcp_ipversion == IPV4_VERSION) { 22676 ipha = (ipha_t *)rptr; 22677 ipha->ipha_length = htons(tcp_hdr_len); 22678 } else { 22679 ip6h = (ip6_t *)rptr; 22680 ASSERT(tcp != NULL); 22681 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22682 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22683 } 22684 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22685 tcph->th_flags[0] = (uint8_t)ctl; 22686 if (ctl & TH_RST) { 22687 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22688 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22689 /* 22690 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22691 */ 22692 if (tcp->tcp_snd_ts_ok && 22693 tcp->tcp_state > TCPS_SYN_SENT) { 22694 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22695 *(mp->b_wptr) = TCPOPT_EOL; 22696 if (tcp->tcp_ipversion == IPV4_VERSION) { 22697 ipha->ipha_length = htons(tcp_hdr_len - 22698 TCPOPT_REAL_TS_LEN); 22699 } else { 22700 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22701 TCPOPT_REAL_TS_LEN); 22702 } 22703 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22704 sum -= TCPOPT_REAL_TS_LEN; 22705 } 22706 } 22707 if (ctl & TH_ACK) { 22708 if (tcp->tcp_snd_ts_ok) { 22709 U32_TO_BE32(lbolt, 22710 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22711 U32_TO_BE32(tcp->tcp_ts_recent, 22712 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22713 } 22714 22715 /* Update the latest receive window size in TCP header. */ 22716 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22717 tcph->th_win); 22718 tcp->tcp_rack = ack; 22719 tcp->tcp_rack_cnt = 0; 22720 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22721 } 22722 BUMP_LOCAL(tcp->tcp_obsegs); 22723 U32_TO_BE32(seq, tcph->th_seq); 22724 U32_TO_BE32(ack, tcph->th_ack); 22725 /* 22726 * Include the adjustment for a source route if any. 22727 */ 22728 sum = (sum >> 16) + (sum & 0xFFFF); 22729 U16_TO_BE16(sum, tcph->th_sum); 22730 tcp_send_data(tcp, tcp->tcp_wq, mp); 22731 } 22732 22733 /* 22734 * If this routine returns B_TRUE, TCP can generate a RST in response 22735 * to a segment. If it returns B_FALSE, TCP should not respond. 22736 */ 22737 static boolean_t 22738 tcp_send_rst_chk(tcp_stack_t *tcps) 22739 { 22740 clock_t now; 22741 22742 /* 22743 * TCP needs to protect itself from generating too many RSTs. 22744 * This can be a DoS attack by sending us random segments 22745 * soliciting RSTs. 22746 * 22747 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22748 * in each 1 second interval. In this way, TCP still generate 22749 * RSTs in normal cases but when under attack, the impact is 22750 * limited. 22751 */ 22752 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22753 now = lbolt; 22754 /* lbolt can wrap around. */ 22755 if ((tcps->tcps_last_rst_intrvl > now) || 22756 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22757 1*SECONDS)) { 22758 tcps->tcps_last_rst_intrvl = now; 22759 tcps->tcps_rst_cnt = 1; 22760 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22761 return (B_FALSE); 22762 } 22763 } 22764 return (B_TRUE); 22765 } 22766 22767 /* 22768 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22769 */ 22770 static void 22771 tcp_ip_ire_mark_advice(tcp_t *tcp) 22772 { 22773 mblk_t *mp; 22774 ipic_t *ipic; 22775 22776 if (tcp->tcp_ipversion == IPV4_VERSION) { 22777 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22778 &ipic); 22779 } else { 22780 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22781 &ipic); 22782 } 22783 if (mp == NULL) 22784 return; 22785 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22786 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22787 } 22788 22789 /* 22790 * Return an IP advice ioctl mblk and set ipic to be the pointer 22791 * to the advice structure. 22792 */ 22793 static mblk_t * 22794 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22795 { 22796 struct iocblk *ioc; 22797 mblk_t *mp, *mp1; 22798 22799 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22800 if (mp == NULL) 22801 return (NULL); 22802 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22803 *ipic = (ipic_t *)mp->b_rptr; 22804 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22805 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22806 22807 bcopy(addr, *ipic + 1, addr_len); 22808 22809 (*ipic)->ipic_addr_length = addr_len; 22810 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22811 22812 mp1 = mkiocb(IP_IOCTL); 22813 if (mp1 == NULL) { 22814 freemsg(mp); 22815 return (NULL); 22816 } 22817 mp1->b_cont = mp; 22818 ioc = (struct iocblk *)mp1->b_rptr; 22819 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22820 22821 return (mp1); 22822 } 22823 22824 /* 22825 * Generate a reset based on an inbound packet, connp is set by caller 22826 * when RST is in response to an unexpected inbound packet for which 22827 * there is active tcp state in the system. 22828 * 22829 * IPSEC NOTE : Try to send the reply with the same protection as it came 22830 * in. We still have the ipsec_mp that the packet was attached to. Thus 22831 * the packet will go out at the same level of protection as it came in by 22832 * converting the IPSEC_IN to IPSEC_OUT. 22833 */ 22834 static void 22835 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22836 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22837 tcp_stack_t *tcps, conn_t *connp) 22838 { 22839 ipha_t *ipha = NULL; 22840 ip6_t *ip6h = NULL; 22841 ushort_t len; 22842 tcph_t *tcph; 22843 int i; 22844 mblk_t *ipsec_mp; 22845 boolean_t mctl_present; 22846 ipic_t *ipic; 22847 ipaddr_t v4addr; 22848 in6_addr_t v6addr; 22849 int addr_len; 22850 void *addr; 22851 queue_t *q = tcps->tcps_g_q; 22852 tcp_t *tcp; 22853 cred_t *cr; 22854 mblk_t *nmp; 22855 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22856 22857 if (tcps->tcps_g_q == NULL) { 22858 /* 22859 * For non-zero stackids the default queue isn't created 22860 * until the first open, thus there can be a need to send 22861 * a reset before then. But we can't do that, hence we just 22862 * drop the packet. Later during boot, when the default queue 22863 * has been setup, a retransmitted packet from the peer 22864 * will result in a reset. 22865 */ 22866 ASSERT(tcps->tcps_netstack->netstack_stackid != 22867 GLOBAL_NETSTACKID); 22868 freemsg(mp); 22869 return; 22870 } 22871 22872 if (connp != NULL) 22873 tcp = connp->conn_tcp; 22874 else 22875 tcp = Q_TO_TCP(q); 22876 22877 if (!tcp_send_rst_chk(tcps)) { 22878 tcps->tcps_rst_unsent++; 22879 freemsg(mp); 22880 return; 22881 } 22882 22883 if (mp->b_datap->db_type == M_CTL) { 22884 ipsec_mp = mp; 22885 mp = mp->b_cont; 22886 mctl_present = B_TRUE; 22887 } else { 22888 ipsec_mp = mp; 22889 mctl_present = B_FALSE; 22890 } 22891 22892 if (str && q && tcps->tcps_dbg) { 22893 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22894 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22895 "flags 0x%x", 22896 str, seq, ack, ctl); 22897 } 22898 if (mp->b_datap->db_ref != 1) { 22899 mblk_t *mp1 = copyb(mp); 22900 freemsg(mp); 22901 mp = mp1; 22902 if (!mp) { 22903 if (mctl_present) 22904 freeb(ipsec_mp); 22905 return; 22906 } else { 22907 if (mctl_present) { 22908 ipsec_mp->b_cont = mp; 22909 } else { 22910 ipsec_mp = mp; 22911 } 22912 } 22913 } else if (mp->b_cont) { 22914 freemsg(mp->b_cont); 22915 mp->b_cont = NULL; 22916 } 22917 /* 22918 * We skip reversing source route here. 22919 * (for now we replace all IP options with EOL) 22920 */ 22921 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22922 ipha = (ipha_t *)mp->b_rptr; 22923 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22924 mp->b_rptr[i] = IPOPT_EOL; 22925 /* 22926 * Make sure that src address isn't flagrantly invalid. 22927 * Not all broadcast address checking for the src address 22928 * is possible, since we don't know the netmask of the src 22929 * addr. No check for destination address is done, since 22930 * IP will not pass up a packet with a broadcast dest 22931 * address to TCP. Similar checks are done below for IPv6. 22932 */ 22933 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22934 CLASSD(ipha->ipha_src)) { 22935 freemsg(ipsec_mp); 22936 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22937 return; 22938 } 22939 } else { 22940 ip6h = (ip6_t *)mp->b_rptr; 22941 22942 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22943 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22944 freemsg(ipsec_mp); 22945 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22946 return; 22947 } 22948 22949 /* Remove any extension headers assuming partial overlay */ 22950 if (ip_hdr_len > IPV6_HDR_LEN) { 22951 uint8_t *to; 22952 22953 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22954 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22955 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22956 ip_hdr_len = IPV6_HDR_LEN; 22957 ip6h = (ip6_t *)mp->b_rptr; 22958 ip6h->ip6_nxt = IPPROTO_TCP; 22959 } 22960 } 22961 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22962 if (tcph->th_flags[0] & TH_RST) { 22963 freemsg(ipsec_mp); 22964 return; 22965 } 22966 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22967 len = ip_hdr_len + sizeof (tcph_t); 22968 mp->b_wptr = &mp->b_rptr[len]; 22969 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22970 ipha->ipha_length = htons(len); 22971 /* Swap addresses */ 22972 v4addr = ipha->ipha_src; 22973 ipha->ipha_src = ipha->ipha_dst; 22974 ipha->ipha_dst = v4addr; 22975 ipha->ipha_ident = 0; 22976 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22977 addr_len = IP_ADDR_LEN; 22978 addr = &v4addr; 22979 } else { 22980 /* No ip6i_t in this case */ 22981 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22982 /* Swap addresses */ 22983 v6addr = ip6h->ip6_src; 22984 ip6h->ip6_src = ip6h->ip6_dst; 22985 ip6h->ip6_dst = v6addr; 22986 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22987 addr_len = IPV6_ADDR_LEN; 22988 addr = &v6addr; 22989 } 22990 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22991 U32_TO_BE32(ack, tcph->th_ack); 22992 U32_TO_BE32(seq, tcph->th_seq); 22993 U16_TO_BE16(0, tcph->th_win); 22994 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22995 tcph->th_flags[0] = (uint8_t)ctl; 22996 if (ctl & TH_RST) { 22997 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22998 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22999 } 23000 23001 /* IP trusts us to set up labels when required. */ 23002 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 23003 crgetlabel(cr) != NULL) { 23004 int err; 23005 23006 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 23007 err = tsol_check_label(cr, &mp, 23008 tcp->tcp_connp->conn_mac_exempt, 23009 tcps->tcps_netstack->netstack_ip); 23010 else 23011 err = tsol_check_label_v6(cr, &mp, 23012 tcp->tcp_connp->conn_mac_exempt, 23013 tcps->tcps_netstack->netstack_ip); 23014 if (mctl_present) 23015 ipsec_mp->b_cont = mp; 23016 else 23017 ipsec_mp = mp; 23018 if (err != 0) { 23019 freemsg(ipsec_mp); 23020 return; 23021 } 23022 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23023 ipha = (ipha_t *)mp->b_rptr; 23024 } else { 23025 ip6h = (ip6_t *)mp->b_rptr; 23026 } 23027 } 23028 23029 if (mctl_present) { 23030 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23031 23032 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23033 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23034 return; 23035 } 23036 } 23037 if (zoneid == ALL_ZONES) 23038 zoneid = GLOBAL_ZONEID; 23039 23040 /* Add the zoneid so ip_output routes it properly */ 23041 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23042 freemsg(ipsec_mp); 23043 return; 23044 } 23045 ipsec_mp = nmp; 23046 23047 /* 23048 * NOTE: one might consider tracing a TCP packet here, but 23049 * this function has no active TCP state and no tcp structure 23050 * that has a trace buffer. If we traced here, we would have 23051 * to keep a local trace buffer in tcp_record_trace(). 23052 * 23053 * TSol note: The mblk that contains the incoming packet was 23054 * reused by tcp_xmit_listener_reset, so it already contains 23055 * the right credentials and we don't need to call mblk_setcred. 23056 * Also the conn's cred is not right since it is associated 23057 * with tcps_g_q. 23058 */ 23059 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23060 23061 /* 23062 * Tell IP to mark the IRE used for this destination temporary. 23063 * This way, we can limit our exposure to DoS attack because IP 23064 * creates an IRE for each destination. If there are too many, 23065 * the time to do any routing lookup will be extremely long. And 23066 * the lookup can be in interrupt context. 23067 * 23068 * Note that in normal circumstances, this marking should not 23069 * affect anything. It would be nice if only 1 message is 23070 * needed to inform IP that the IRE created for this RST should 23071 * not be added to the cache table. But there is currently 23072 * not such communication mechanism between TCP and IP. So 23073 * the best we can do now is to send the advice ioctl to IP 23074 * to mark the IRE temporary. 23075 */ 23076 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23077 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23078 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23079 } 23080 } 23081 23082 /* 23083 * Initiate closedown sequence on an active connection. (May be called as 23084 * writer.) Return value zero for OK return, non-zero for error return. 23085 */ 23086 static int 23087 tcp_xmit_end(tcp_t *tcp) 23088 { 23089 ipic_t *ipic; 23090 mblk_t *mp; 23091 tcp_stack_t *tcps = tcp->tcp_tcps; 23092 23093 if (tcp->tcp_state < TCPS_SYN_RCVD || 23094 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23095 /* 23096 * Invalid state, only states TCPS_SYN_RCVD, 23097 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23098 */ 23099 return (-1); 23100 } 23101 23102 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23103 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23104 /* 23105 * If there is nothing more unsent, send the FIN now. 23106 * Otherwise, it will go out with the last segment. 23107 */ 23108 if (tcp->tcp_unsent == 0) { 23109 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23110 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23111 23112 if (mp) { 23113 tcp_send_data(tcp, tcp->tcp_wq, mp); 23114 } else { 23115 /* 23116 * Couldn't allocate msg. Pretend we got it out. 23117 * Wait for rexmit timeout. 23118 */ 23119 tcp->tcp_snxt = tcp->tcp_fss + 1; 23120 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23121 } 23122 23123 /* 23124 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23125 * changed. 23126 */ 23127 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23128 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23129 } 23130 } else { 23131 /* 23132 * If tcp->tcp_cork is set, then the data will not get sent, 23133 * so we have to check that and unset it first. 23134 */ 23135 if (tcp->tcp_cork) 23136 tcp->tcp_cork = B_FALSE; 23137 tcp_wput_data(tcp, NULL, B_FALSE); 23138 } 23139 23140 /* 23141 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23142 * is 0, don't update the cache. 23143 */ 23144 if (tcps->tcps_rtt_updates == 0 || 23145 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23146 return (0); 23147 23148 /* 23149 * NOTE: should not update if source routes i.e. if tcp_remote if 23150 * different from the destination. 23151 */ 23152 if (tcp->tcp_ipversion == IPV4_VERSION) { 23153 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23154 return (0); 23155 } 23156 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23157 &ipic); 23158 } else { 23159 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23160 &tcp->tcp_ip6h->ip6_dst))) { 23161 return (0); 23162 } 23163 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23164 &ipic); 23165 } 23166 23167 /* Record route attributes in the IRE for use by future connections. */ 23168 if (mp == NULL) 23169 return (0); 23170 23171 /* 23172 * We do not have a good algorithm to update ssthresh at this time. 23173 * So don't do any update. 23174 */ 23175 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23176 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23177 23178 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23179 23180 return (0); 23181 } 23182 23183 /* ARGSUSED */ 23184 void 23185 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 23186 { 23187 conn_t *connp = (conn_t *)arg; 23188 mblk_t *mp1; 23189 tcp_t *tcp = connp->conn_tcp; 23190 tcp_xmit_reset_event_t *eventp; 23191 23192 ASSERT(mp->b_datap->db_type == M_PROTO && 23193 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 23194 23195 if (tcp->tcp_state != TCPS_LISTEN) { 23196 freemsg(mp); 23197 return; 23198 } 23199 23200 mp1 = mp->b_cont; 23201 mp->b_cont = NULL; 23202 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 23203 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 23204 connp->conn_netstack); 23205 23206 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 23207 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 23208 freemsg(mp); 23209 } 23210 23211 /* 23212 * Generate a "no listener here" RST in response to an "unknown" segment. 23213 * connp is set by caller when RST is in response to an unexpected 23214 * inbound packet for which there is active tcp state in the system. 23215 * Note that we are reusing the incoming mp to construct the outgoing RST. 23216 */ 23217 void 23218 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23219 tcp_stack_t *tcps, conn_t *connp) 23220 { 23221 uchar_t *rptr; 23222 uint32_t seg_len; 23223 tcph_t *tcph; 23224 uint32_t seg_seq; 23225 uint32_t seg_ack; 23226 uint_t flags; 23227 mblk_t *ipsec_mp; 23228 ipha_t *ipha; 23229 ip6_t *ip6h; 23230 boolean_t mctl_present = B_FALSE; 23231 boolean_t check = B_TRUE; 23232 boolean_t policy_present; 23233 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23234 23235 TCP_STAT(tcps, tcp_no_listener); 23236 23237 ipsec_mp = mp; 23238 23239 if (mp->b_datap->db_type == M_CTL) { 23240 ipsec_in_t *ii; 23241 23242 mctl_present = B_TRUE; 23243 mp = mp->b_cont; 23244 23245 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23246 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23247 if (ii->ipsec_in_dont_check) { 23248 check = B_FALSE; 23249 if (!ii->ipsec_in_secure) { 23250 freeb(ipsec_mp); 23251 mctl_present = B_FALSE; 23252 ipsec_mp = mp; 23253 } 23254 } 23255 } 23256 23257 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23258 policy_present = ipss->ipsec_inbound_v4_policy_present; 23259 ipha = (ipha_t *)mp->b_rptr; 23260 ip6h = NULL; 23261 } else { 23262 policy_present = ipss->ipsec_inbound_v6_policy_present; 23263 ipha = NULL; 23264 ip6h = (ip6_t *)mp->b_rptr; 23265 } 23266 23267 if (check && policy_present) { 23268 /* 23269 * The conn_t parameter is NULL because we already know 23270 * nobody's home. 23271 */ 23272 ipsec_mp = ipsec_check_global_policy( 23273 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23274 tcps->tcps_netstack); 23275 if (ipsec_mp == NULL) 23276 return; 23277 } 23278 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23279 DTRACE_PROBE2( 23280 tx__ip__log__error__nolistener__tcp, 23281 char *, "Could not reply with RST to mp(1)", 23282 mblk_t *, mp); 23283 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23284 freemsg(ipsec_mp); 23285 return; 23286 } 23287 23288 rptr = mp->b_rptr; 23289 23290 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23291 seg_seq = BE32_TO_U32(tcph->th_seq); 23292 seg_ack = BE32_TO_U32(tcph->th_ack); 23293 flags = tcph->th_flags[0]; 23294 23295 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23296 if (flags & TH_RST) { 23297 freemsg(ipsec_mp); 23298 } else if (flags & TH_ACK) { 23299 tcp_xmit_early_reset("no tcp, reset", 23300 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23301 connp); 23302 } else { 23303 if (flags & TH_SYN) { 23304 seg_len++; 23305 } else { 23306 /* 23307 * Here we violate the RFC. Note that a normal 23308 * TCP will never send a segment without the ACK 23309 * flag, except for RST or SYN segment. This 23310 * segment is neither. Just drop it on the 23311 * floor. 23312 */ 23313 freemsg(ipsec_mp); 23314 tcps->tcps_rst_unsent++; 23315 return; 23316 } 23317 23318 tcp_xmit_early_reset("no tcp, reset/ack", 23319 ipsec_mp, 0, seg_seq + seg_len, 23320 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23321 } 23322 } 23323 23324 /* 23325 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23326 * ip and tcp header ready to pass down to IP. If the mp passed in is 23327 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23328 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23329 * otherwise it will dup partial mblks.) 23330 * Otherwise, an appropriate ACK packet will be generated. This 23331 * routine is not usually called to send new data for the first time. It 23332 * is mostly called out of the timer for retransmits, and to generate ACKs. 23333 * 23334 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23335 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23336 * of the original mblk chain will be returned in *offset and *end_mp. 23337 */ 23338 mblk_t * 23339 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23340 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23341 boolean_t rexmit) 23342 { 23343 int data_length; 23344 int32_t off = 0; 23345 uint_t flags; 23346 mblk_t *mp1; 23347 mblk_t *mp2; 23348 uchar_t *rptr; 23349 tcph_t *tcph; 23350 int32_t num_sack_blk = 0; 23351 int32_t sack_opt_len = 0; 23352 tcp_stack_t *tcps = tcp->tcp_tcps; 23353 23354 /* Allocate for our maximum TCP header + link-level */ 23355 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23356 tcps->tcps_wroff_xtra, BPRI_MED); 23357 if (!mp1) 23358 return (NULL); 23359 data_length = 0; 23360 23361 /* 23362 * Note that tcp_mss has been adjusted to take into account the 23363 * timestamp option if applicable. Because SACK options do not 23364 * appear in every TCP segments and they are of variable lengths, 23365 * they cannot be included in tcp_mss. Thus we need to calculate 23366 * the actual segment length when we need to send a segment which 23367 * includes SACK options. 23368 */ 23369 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23370 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23371 tcp->tcp_num_sack_blk); 23372 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23373 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23374 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23375 max_to_send -= sack_opt_len; 23376 } 23377 23378 if (offset != NULL) { 23379 off = *offset; 23380 /* We use offset as an indicator that end_mp is not NULL. */ 23381 *end_mp = NULL; 23382 } 23383 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23384 /* This could be faster with cooperation from downstream */ 23385 if (mp2 != mp1 && !sendall && 23386 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23387 max_to_send) 23388 /* 23389 * Don't send the next mblk since the whole mblk 23390 * does not fit. 23391 */ 23392 break; 23393 mp2->b_cont = dupb(mp); 23394 mp2 = mp2->b_cont; 23395 if (!mp2) { 23396 freemsg(mp1); 23397 return (NULL); 23398 } 23399 mp2->b_rptr += off; 23400 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23401 (uintptr_t)INT_MAX); 23402 23403 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23404 if (data_length > max_to_send) { 23405 mp2->b_wptr -= data_length - max_to_send; 23406 data_length = max_to_send; 23407 off = mp2->b_wptr - mp->b_rptr; 23408 break; 23409 } else { 23410 off = 0; 23411 } 23412 } 23413 if (offset != NULL) { 23414 *offset = off; 23415 *end_mp = mp; 23416 } 23417 if (seg_len != NULL) { 23418 *seg_len = data_length; 23419 } 23420 23421 /* Update the latest receive window size in TCP header. */ 23422 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23423 tcp->tcp_tcph->th_win); 23424 23425 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23426 mp1->b_rptr = rptr; 23427 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23428 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23429 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23430 U32_TO_ABE32(seq, tcph->th_seq); 23431 23432 /* 23433 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23434 * that this function was called from tcp_wput_data. Thus, when called 23435 * to retransmit data the setting of the PUSH bit may appear some 23436 * what random in that it might get set when it should not. This 23437 * should not pose any performance issues. 23438 */ 23439 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23440 tcp->tcp_unsent == data_length)) { 23441 flags = TH_ACK | TH_PUSH; 23442 } else { 23443 flags = TH_ACK; 23444 } 23445 23446 if (tcp->tcp_ecn_ok) { 23447 if (tcp->tcp_ecn_echo_on) 23448 flags |= TH_ECE; 23449 23450 /* 23451 * Only set ECT bit and ECN_CWR if a segment contains new data. 23452 * There is no TCP flow control for non-data segments, and 23453 * only data segment is transmitted reliably. 23454 */ 23455 if (data_length > 0 && !rexmit) { 23456 SET_ECT(tcp, rptr); 23457 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23458 flags |= TH_CWR; 23459 tcp->tcp_ecn_cwr_sent = B_TRUE; 23460 } 23461 } 23462 } 23463 23464 if (tcp->tcp_valid_bits) { 23465 uint32_t u1; 23466 23467 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23468 seq == tcp->tcp_iss) { 23469 uchar_t *wptr; 23470 23471 /* 23472 * If TCP_ISS_VALID and the seq number is tcp_iss, 23473 * TCP can only be in SYN-SENT, SYN-RCVD or 23474 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23475 * our SYN is not ack'ed but the app closes this 23476 * TCP connection. 23477 */ 23478 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23479 tcp->tcp_state == TCPS_SYN_RCVD || 23480 tcp->tcp_state == TCPS_FIN_WAIT_1); 23481 23482 /* 23483 * Tack on the MSS option. It is always needed 23484 * for both active and passive open. 23485 * 23486 * MSS option value should be interface MTU - MIN 23487 * TCP/IP header according to RFC 793 as it means 23488 * the maximum segment size TCP can receive. But 23489 * to get around some broken middle boxes/end hosts 23490 * out there, we allow the option value to be the 23491 * same as the MSS option size on the peer side. 23492 * In this way, the other side will not send 23493 * anything larger than they can receive. 23494 * 23495 * Note that for SYN_SENT state, the ndd param 23496 * tcp_use_smss_as_mss_opt has no effect as we 23497 * don't know the peer's MSS option value. So 23498 * the only case we need to take care of is in 23499 * SYN_RCVD state, which is done later. 23500 */ 23501 wptr = mp1->b_wptr; 23502 wptr[0] = TCPOPT_MAXSEG; 23503 wptr[1] = TCPOPT_MAXSEG_LEN; 23504 wptr += 2; 23505 u1 = tcp->tcp_if_mtu - 23506 (tcp->tcp_ipversion == IPV4_VERSION ? 23507 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23508 TCP_MIN_HEADER_LENGTH; 23509 U16_TO_BE16(u1, wptr); 23510 mp1->b_wptr = wptr + 2; 23511 /* Update the offset to cover the additional word */ 23512 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23513 23514 /* 23515 * Note that the following way of filling in 23516 * TCP options are not optimal. Some NOPs can 23517 * be saved. But there is no need at this time 23518 * to optimize it. When it is needed, we will 23519 * do it. 23520 */ 23521 switch (tcp->tcp_state) { 23522 case TCPS_SYN_SENT: 23523 flags = TH_SYN; 23524 23525 if (tcp->tcp_snd_ts_ok) { 23526 uint32_t llbolt = (uint32_t)lbolt; 23527 23528 wptr = mp1->b_wptr; 23529 wptr[0] = TCPOPT_NOP; 23530 wptr[1] = TCPOPT_NOP; 23531 wptr[2] = TCPOPT_TSTAMP; 23532 wptr[3] = TCPOPT_TSTAMP_LEN; 23533 wptr += 4; 23534 U32_TO_BE32(llbolt, wptr); 23535 wptr += 4; 23536 ASSERT(tcp->tcp_ts_recent == 0); 23537 U32_TO_BE32(0L, wptr); 23538 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23539 tcph->th_offset_and_rsrvd[0] += 23540 (3 << 4); 23541 } 23542 23543 /* 23544 * Set up all the bits to tell other side 23545 * we are ECN capable. 23546 */ 23547 if (tcp->tcp_ecn_ok) { 23548 flags |= (TH_ECE | TH_CWR); 23549 } 23550 break; 23551 case TCPS_SYN_RCVD: 23552 flags |= TH_SYN; 23553 23554 /* 23555 * Reset the MSS option value to be SMSS 23556 * We should probably add back the bytes 23557 * for timestamp option and IPsec. We 23558 * don't do that as this is a workaround 23559 * for broken middle boxes/end hosts, it 23560 * is better for us to be more cautious. 23561 * They may not take these things into 23562 * account in their SMSS calculation. Thus 23563 * the peer's calculated SMSS may be smaller 23564 * than what it can be. This should be OK. 23565 */ 23566 if (tcps->tcps_use_smss_as_mss_opt) { 23567 u1 = tcp->tcp_mss; 23568 U16_TO_BE16(u1, wptr); 23569 } 23570 23571 /* 23572 * If the other side is ECN capable, reply 23573 * that we are also ECN capable. 23574 */ 23575 if (tcp->tcp_ecn_ok) 23576 flags |= TH_ECE; 23577 break; 23578 default: 23579 /* 23580 * The above ASSERT() makes sure that this 23581 * must be FIN-WAIT-1 state. Our SYN has 23582 * not been ack'ed so retransmit it. 23583 */ 23584 flags |= TH_SYN; 23585 break; 23586 } 23587 23588 if (tcp->tcp_snd_ws_ok) { 23589 wptr = mp1->b_wptr; 23590 wptr[0] = TCPOPT_NOP; 23591 wptr[1] = TCPOPT_WSCALE; 23592 wptr[2] = TCPOPT_WS_LEN; 23593 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23594 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23595 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23596 } 23597 23598 if (tcp->tcp_snd_sack_ok) { 23599 wptr = mp1->b_wptr; 23600 wptr[0] = TCPOPT_NOP; 23601 wptr[1] = TCPOPT_NOP; 23602 wptr[2] = TCPOPT_SACK_PERMITTED; 23603 wptr[3] = TCPOPT_SACK_OK_LEN; 23604 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23605 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23606 } 23607 23608 /* allocb() of adequate mblk assures space */ 23609 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23610 (uintptr_t)INT_MAX); 23611 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23612 /* 23613 * Get IP set to checksum on our behalf 23614 * Include the adjustment for a source route if any. 23615 */ 23616 u1 += tcp->tcp_sum; 23617 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23618 U16_TO_BE16(u1, tcph->th_sum); 23619 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23620 } 23621 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23622 (seq + data_length) == tcp->tcp_fss) { 23623 if (!tcp->tcp_fin_acked) { 23624 flags |= TH_FIN; 23625 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23626 } 23627 if (!tcp->tcp_fin_sent) { 23628 tcp->tcp_fin_sent = B_TRUE; 23629 switch (tcp->tcp_state) { 23630 case TCPS_SYN_RCVD: 23631 case TCPS_ESTABLISHED: 23632 tcp->tcp_state = TCPS_FIN_WAIT_1; 23633 break; 23634 case TCPS_CLOSE_WAIT: 23635 tcp->tcp_state = TCPS_LAST_ACK; 23636 break; 23637 } 23638 if (tcp->tcp_suna == tcp->tcp_snxt) 23639 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23640 tcp->tcp_snxt = tcp->tcp_fss + 1; 23641 } 23642 } 23643 /* 23644 * Note the trick here. u1 is unsigned. When tcp_urg 23645 * is smaller than seq, u1 will become a very huge value. 23646 * So the comparison will fail. Also note that tcp_urp 23647 * should be positive, see RFC 793 page 17. 23648 */ 23649 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23650 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23651 u1 < (uint32_t)(64 * 1024)) { 23652 flags |= TH_URG; 23653 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23654 U32_TO_ABE16(u1, tcph->th_urp); 23655 } 23656 } 23657 tcph->th_flags[0] = (uchar_t)flags; 23658 tcp->tcp_rack = tcp->tcp_rnxt; 23659 tcp->tcp_rack_cnt = 0; 23660 23661 if (tcp->tcp_snd_ts_ok) { 23662 if (tcp->tcp_state != TCPS_SYN_SENT) { 23663 uint32_t llbolt = (uint32_t)lbolt; 23664 23665 U32_TO_BE32(llbolt, 23666 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23667 U32_TO_BE32(tcp->tcp_ts_recent, 23668 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23669 } 23670 } 23671 23672 if (num_sack_blk > 0) { 23673 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23674 sack_blk_t *tmp; 23675 int32_t i; 23676 23677 wptr[0] = TCPOPT_NOP; 23678 wptr[1] = TCPOPT_NOP; 23679 wptr[2] = TCPOPT_SACK; 23680 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23681 sizeof (sack_blk_t); 23682 wptr += TCPOPT_REAL_SACK_LEN; 23683 23684 tmp = tcp->tcp_sack_list; 23685 for (i = 0; i < num_sack_blk; i++) { 23686 U32_TO_BE32(tmp[i].begin, wptr); 23687 wptr += sizeof (tcp_seq); 23688 U32_TO_BE32(tmp[i].end, wptr); 23689 wptr += sizeof (tcp_seq); 23690 } 23691 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23692 } 23693 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23694 data_length += (int)(mp1->b_wptr - rptr); 23695 if (tcp->tcp_ipversion == IPV4_VERSION) { 23696 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23697 } else { 23698 ip6_t *ip6 = (ip6_t *)(rptr + 23699 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23700 sizeof (ip6i_t) : 0)); 23701 23702 ip6->ip6_plen = htons(data_length - 23703 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23704 } 23705 23706 /* 23707 * Prime pump for IP 23708 * Include the adjustment for a source route if any. 23709 */ 23710 data_length -= tcp->tcp_ip_hdr_len; 23711 data_length += tcp->tcp_sum; 23712 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23713 U16_TO_ABE16(data_length, tcph->th_sum); 23714 if (tcp->tcp_ip_forward_progress) { 23715 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23716 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23717 tcp->tcp_ip_forward_progress = B_FALSE; 23718 } 23719 return (mp1); 23720 } 23721 23722 /* This function handles the push timeout. */ 23723 void 23724 tcp_push_timer(void *arg) 23725 { 23726 conn_t *connp = (conn_t *)arg; 23727 tcp_t *tcp = connp->conn_tcp; 23728 uint_t flags; 23729 sodirect_t *sodp; 23730 23731 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23732 23733 ASSERT(tcp->tcp_listener == NULL); 23734 23735 ASSERT(!IPCL_IS_NONSTR(connp)); 23736 23737 /* 23738 * We need to plug synchronous streams during our drain to prevent 23739 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23740 */ 23741 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23742 tcp->tcp_push_tid = 0; 23743 23744 SOD_PTR_ENTER(tcp, sodp); 23745 if (sodp != NULL) { 23746 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23747 /* sod_wakeup() does the mutex_exit() */ 23748 } else if (tcp->tcp_rcv_list != NULL) { 23749 flags = tcp_rcv_drain(tcp); 23750 } 23751 if (flags == TH_ACK_NEEDED) 23752 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23753 23754 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23755 } 23756 23757 /* 23758 * This function handles delayed ACK timeout. 23759 */ 23760 static void 23761 tcp_ack_timer(void *arg) 23762 { 23763 conn_t *connp = (conn_t *)arg; 23764 tcp_t *tcp = connp->conn_tcp; 23765 mblk_t *mp; 23766 tcp_stack_t *tcps = tcp->tcp_tcps; 23767 23768 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23769 23770 tcp->tcp_ack_tid = 0; 23771 23772 if (tcp->tcp_fused) 23773 return; 23774 23775 /* 23776 * Do not send ACK if there is no outstanding unack'ed data. 23777 */ 23778 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23779 return; 23780 } 23781 23782 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23783 /* 23784 * Make sure we don't allow deferred ACKs to result in 23785 * timer-based ACKing. If we have held off an ACK 23786 * when there was more than an mss here, and the timer 23787 * goes off, we have to worry about the possibility 23788 * that the sender isn't doing slow-start, or is out 23789 * of step with us for some other reason. We fall 23790 * permanently back in the direction of 23791 * ACK-every-other-packet as suggested in RFC 1122. 23792 */ 23793 if (tcp->tcp_rack_abs_max > 2) 23794 tcp->tcp_rack_abs_max--; 23795 tcp->tcp_rack_cur_max = 2; 23796 } 23797 mp = tcp_ack_mp(tcp); 23798 23799 if (mp != NULL) { 23800 BUMP_LOCAL(tcp->tcp_obsegs); 23801 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23802 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23803 tcp_send_data(tcp, tcp->tcp_wq, mp); 23804 } 23805 } 23806 23807 23808 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23809 static mblk_t * 23810 tcp_ack_mp(tcp_t *tcp) 23811 { 23812 uint32_t seq_no; 23813 tcp_stack_t *tcps = tcp->tcp_tcps; 23814 23815 /* 23816 * There are a few cases to be considered while setting the sequence no. 23817 * Essentially, we can come here while processing an unacceptable pkt 23818 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23819 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23820 * If we are here for a zero window probe, stick with suna. In all 23821 * other cases, we check if suna + swnd encompasses snxt and set 23822 * the sequence number to snxt, if so. If snxt falls outside the 23823 * window (the receiver probably shrunk its window), we will go with 23824 * suna + swnd, otherwise the sequence no will be unacceptable to the 23825 * receiver. 23826 */ 23827 if (tcp->tcp_zero_win_probe) { 23828 seq_no = tcp->tcp_suna; 23829 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23830 ASSERT(tcp->tcp_swnd == 0); 23831 seq_no = tcp->tcp_snxt; 23832 } else { 23833 seq_no = SEQ_GT(tcp->tcp_snxt, 23834 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23835 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23836 } 23837 23838 if (tcp->tcp_valid_bits) { 23839 /* 23840 * For the complex case where we have to send some 23841 * controls (FIN or SYN), let tcp_xmit_mp do it. 23842 */ 23843 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23844 NULL, B_FALSE)); 23845 } else { 23846 /* Generate a simple ACK */ 23847 int data_length; 23848 uchar_t *rptr; 23849 tcph_t *tcph; 23850 mblk_t *mp1; 23851 int32_t tcp_hdr_len; 23852 int32_t tcp_tcp_hdr_len; 23853 int32_t num_sack_blk = 0; 23854 int32_t sack_opt_len; 23855 23856 /* 23857 * Allocate space for TCP + IP headers 23858 * and link-level header 23859 */ 23860 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23861 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23862 tcp->tcp_num_sack_blk); 23863 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23864 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23865 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23866 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23867 } else { 23868 tcp_hdr_len = tcp->tcp_hdr_len; 23869 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23870 } 23871 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23872 if (!mp1) 23873 return (NULL); 23874 23875 /* Update the latest receive window size in TCP header. */ 23876 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23877 tcp->tcp_tcph->th_win); 23878 /* copy in prototype TCP + IP header */ 23879 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23880 mp1->b_rptr = rptr; 23881 mp1->b_wptr = rptr + tcp_hdr_len; 23882 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23883 23884 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23885 23886 /* Set the TCP sequence number. */ 23887 U32_TO_ABE32(seq_no, tcph->th_seq); 23888 23889 /* Set up the TCP flag field. */ 23890 tcph->th_flags[0] = (uchar_t)TH_ACK; 23891 if (tcp->tcp_ecn_echo_on) 23892 tcph->th_flags[0] |= TH_ECE; 23893 23894 tcp->tcp_rack = tcp->tcp_rnxt; 23895 tcp->tcp_rack_cnt = 0; 23896 23897 /* fill in timestamp option if in use */ 23898 if (tcp->tcp_snd_ts_ok) { 23899 uint32_t llbolt = (uint32_t)lbolt; 23900 23901 U32_TO_BE32(llbolt, 23902 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23903 U32_TO_BE32(tcp->tcp_ts_recent, 23904 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23905 } 23906 23907 /* Fill in SACK options */ 23908 if (num_sack_blk > 0) { 23909 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23910 sack_blk_t *tmp; 23911 int32_t i; 23912 23913 wptr[0] = TCPOPT_NOP; 23914 wptr[1] = TCPOPT_NOP; 23915 wptr[2] = TCPOPT_SACK; 23916 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23917 sizeof (sack_blk_t); 23918 wptr += TCPOPT_REAL_SACK_LEN; 23919 23920 tmp = tcp->tcp_sack_list; 23921 for (i = 0; i < num_sack_blk; i++) { 23922 U32_TO_BE32(tmp[i].begin, wptr); 23923 wptr += sizeof (tcp_seq); 23924 U32_TO_BE32(tmp[i].end, wptr); 23925 wptr += sizeof (tcp_seq); 23926 } 23927 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23928 << 4); 23929 } 23930 23931 if (tcp->tcp_ipversion == IPV4_VERSION) { 23932 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23933 } else { 23934 /* Check for ip6i_t header in sticky hdrs */ 23935 ip6_t *ip6 = (ip6_t *)(rptr + 23936 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23937 sizeof (ip6i_t) : 0)); 23938 23939 ip6->ip6_plen = htons(tcp_hdr_len - 23940 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23941 } 23942 23943 /* 23944 * Prime pump for checksum calculation in IP. Include the 23945 * adjustment for a source route if any. 23946 */ 23947 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23948 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23949 U16_TO_ABE16(data_length, tcph->th_sum); 23950 23951 if (tcp->tcp_ip_forward_progress) { 23952 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23953 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23954 tcp->tcp_ip_forward_progress = B_FALSE; 23955 } 23956 return (mp1); 23957 } 23958 } 23959 23960 /* 23961 * Hash list insertion routine for tcp_t structures. Each hash bucket 23962 * contains a list of tcp_t entries, and each entry is bound to a unique 23963 * port. If there are multiple tcp_t's that are bound to the same port, then 23964 * one of them will be linked into the hash bucket list, and the rest will 23965 * hang off of that one entry. For each port, entries bound to a specific IP 23966 * address will be inserted before those those bound to INADDR_ANY. 23967 */ 23968 static void 23969 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23970 { 23971 tcp_t **tcpp; 23972 tcp_t *tcpnext; 23973 tcp_t *tcphash; 23974 23975 if (tcp->tcp_ptpbhn != NULL) { 23976 ASSERT(!caller_holds_lock); 23977 tcp_bind_hash_remove(tcp); 23978 } 23979 tcpp = &tbf->tf_tcp; 23980 if (!caller_holds_lock) { 23981 mutex_enter(&tbf->tf_lock); 23982 } else { 23983 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23984 } 23985 tcphash = tcpp[0]; 23986 tcpnext = NULL; 23987 if (tcphash != NULL) { 23988 /* Look for an entry using the same port */ 23989 while ((tcphash = tcpp[0]) != NULL && 23990 tcp->tcp_lport != tcphash->tcp_lport) 23991 tcpp = &(tcphash->tcp_bind_hash); 23992 23993 /* The port was not found, just add to the end */ 23994 if (tcphash == NULL) 23995 goto insert; 23996 23997 /* 23998 * OK, there already exists an entry bound to the 23999 * same port. 24000 * 24001 * If the new tcp bound to the INADDR_ANY address 24002 * and the first one in the list is not bound to 24003 * INADDR_ANY we skip all entries until we find the 24004 * first one bound to INADDR_ANY. 24005 * This makes sure that applications binding to a 24006 * specific address get preference over those binding to 24007 * INADDR_ANY. 24008 */ 24009 tcpnext = tcphash; 24010 tcphash = NULL; 24011 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 24012 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 24013 while ((tcpnext = tcpp[0]) != NULL && 24014 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24015 tcpp = &(tcpnext->tcp_bind_hash_port); 24016 24017 if (tcpnext) { 24018 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 24019 tcphash = tcpnext->tcp_bind_hash; 24020 if (tcphash != NULL) { 24021 tcphash->tcp_ptpbhn = 24022 &(tcp->tcp_bind_hash); 24023 tcpnext->tcp_bind_hash = NULL; 24024 } 24025 } 24026 } else { 24027 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 24028 tcphash = tcpnext->tcp_bind_hash; 24029 if (tcphash != NULL) { 24030 tcphash->tcp_ptpbhn = 24031 &(tcp->tcp_bind_hash); 24032 tcpnext->tcp_bind_hash = NULL; 24033 } 24034 } 24035 } 24036 insert: 24037 tcp->tcp_bind_hash_port = tcpnext; 24038 tcp->tcp_bind_hash = tcphash; 24039 tcp->tcp_ptpbhn = tcpp; 24040 tcpp[0] = tcp; 24041 if (!caller_holds_lock) 24042 mutex_exit(&tbf->tf_lock); 24043 } 24044 24045 /* 24046 * Hash list removal routine for tcp_t structures. 24047 */ 24048 static void 24049 tcp_bind_hash_remove(tcp_t *tcp) 24050 { 24051 tcp_t *tcpnext; 24052 kmutex_t *lockp; 24053 tcp_stack_t *tcps = tcp->tcp_tcps; 24054 24055 if (tcp->tcp_ptpbhn == NULL) 24056 return; 24057 24058 /* 24059 * Extract the lock pointer in case there are concurrent 24060 * hash_remove's for this instance. 24061 */ 24062 ASSERT(tcp->tcp_lport != 0); 24063 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24064 24065 ASSERT(lockp != NULL); 24066 mutex_enter(lockp); 24067 if (tcp->tcp_ptpbhn) { 24068 tcpnext = tcp->tcp_bind_hash_port; 24069 if (tcpnext != NULL) { 24070 tcp->tcp_bind_hash_port = NULL; 24071 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24072 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 24073 if (tcpnext->tcp_bind_hash != NULL) { 24074 tcpnext->tcp_bind_hash->tcp_ptpbhn = 24075 &(tcpnext->tcp_bind_hash); 24076 tcp->tcp_bind_hash = NULL; 24077 } 24078 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 24079 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24080 tcp->tcp_bind_hash = NULL; 24081 } 24082 *tcp->tcp_ptpbhn = tcpnext; 24083 tcp->tcp_ptpbhn = NULL; 24084 } 24085 mutex_exit(lockp); 24086 } 24087 24088 24089 /* 24090 * Hash list lookup routine for tcp_t structures. 24091 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24092 */ 24093 static tcp_t * 24094 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24095 { 24096 tf_t *tf; 24097 tcp_t *tcp; 24098 24099 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24100 mutex_enter(&tf->tf_lock); 24101 for (tcp = tf->tf_tcp; tcp != NULL; 24102 tcp = tcp->tcp_acceptor_hash) { 24103 if (tcp->tcp_acceptor_id == id) { 24104 CONN_INC_REF(tcp->tcp_connp); 24105 mutex_exit(&tf->tf_lock); 24106 return (tcp); 24107 } 24108 } 24109 mutex_exit(&tf->tf_lock); 24110 return (NULL); 24111 } 24112 24113 24114 /* 24115 * Hash list insertion routine for tcp_t structures. 24116 */ 24117 void 24118 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24119 { 24120 tf_t *tf; 24121 tcp_t **tcpp; 24122 tcp_t *tcpnext; 24123 tcp_stack_t *tcps = tcp->tcp_tcps; 24124 24125 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24126 24127 if (tcp->tcp_ptpahn != NULL) 24128 tcp_acceptor_hash_remove(tcp); 24129 tcpp = &tf->tf_tcp; 24130 mutex_enter(&tf->tf_lock); 24131 tcpnext = tcpp[0]; 24132 if (tcpnext) 24133 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24134 tcp->tcp_acceptor_hash = tcpnext; 24135 tcp->tcp_ptpahn = tcpp; 24136 tcpp[0] = tcp; 24137 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24138 mutex_exit(&tf->tf_lock); 24139 } 24140 24141 /* 24142 * Hash list removal routine for tcp_t structures. 24143 */ 24144 static void 24145 tcp_acceptor_hash_remove(tcp_t *tcp) 24146 { 24147 tcp_t *tcpnext; 24148 kmutex_t *lockp; 24149 24150 /* 24151 * Extract the lock pointer in case there are concurrent 24152 * hash_remove's for this instance. 24153 */ 24154 lockp = tcp->tcp_acceptor_lockp; 24155 24156 if (tcp->tcp_ptpahn == NULL) 24157 return; 24158 24159 ASSERT(lockp != NULL); 24160 mutex_enter(lockp); 24161 if (tcp->tcp_ptpahn) { 24162 tcpnext = tcp->tcp_acceptor_hash; 24163 if (tcpnext) { 24164 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24165 tcp->tcp_acceptor_hash = NULL; 24166 } 24167 *tcp->tcp_ptpahn = tcpnext; 24168 tcp->tcp_ptpahn = NULL; 24169 } 24170 mutex_exit(lockp); 24171 tcp->tcp_acceptor_lockp = NULL; 24172 } 24173 24174 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24175 24176 static ipaddr_t netmasks[] = { 24177 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24178 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24179 }; 24180 24181 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24182 24183 /* 24184 * XXX This routine should go away and instead we should use the metrics 24185 * associated with the routes to determine the default sndspace and rcvspace. 24186 */ 24187 static tcp_hsp_t * 24188 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24189 { 24190 tcp_hsp_t *hsp = NULL; 24191 24192 /* Quick check without acquiring the lock. */ 24193 if (tcps->tcps_hsp_hash == NULL) 24194 return (NULL); 24195 24196 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24197 24198 /* This routine finds the best-matching HSP for address addr. */ 24199 24200 if (tcps->tcps_hsp_hash) { 24201 int i; 24202 ipaddr_t srchaddr; 24203 tcp_hsp_t *hsp_net; 24204 24205 /* We do three passes: host, network, and subnet. */ 24206 24207 srchaddr = addr; 24208 24209 for (i = 1; i <= 3; i++) { 24210 /* Look for exact match on srchaddr */ 24211 24212 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24213 while (hsp) { 24214 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24215 hsp->tcp_hsp_addr == srchaddr) 24216 break; 24217 hsp = hsp->tcp_hsp_next; 24218 } 24219 ASSERT(hsp == NULL || 24220 hsp->tcp_hsp_vers == IPV4_VERSION); 24221 24222 /* 24223 * If this is the first pass: 24224 * If we found a match, great, return it. 24225 * If not, search for the network on the second pass. 24226 */ 24227 24228 if (i == 1) 24229 if (hsp) 24230 break; 24231 else 24232 { 24233 srchaddr = addr & netmask(addr); 24234 continue; 24235 } 24236 24237 /* 24238 * If this is the second pass: 24239 * If we found a match, but there's a subnet mask, 24240 * save the match but try again using the subnet 24241 * mask on the third pass. 24242 * Otherwise, return whatever we found. 24243 */ 24244 24245 if (i == 2) { 24246 if (hsp && hsp->tcp_hsp_subnet) { 24247 hsp_net = hsp; 24248 srchaddr = addr & hsp->tcp_hsp_subnet; 24249 continue; 24250 } else { 24251 break; 24252 } 24253 } 24254 24255 /* 24256 * This must be the third pass. If we didn't find 24257 * anything, return the saved network HSP instead. 24258 */ 24259 24260 if (!hsp) 24261 hsp = hsp_net; 24262 } 24263 } 24264 24265 rw_exit(&tcps->tcps_hsp_lock); 24266 return (hsp); 24267 } 24268 24269 /* 24270 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24271 * match lookup. 24272 */ 24273 static tcp_hsp_t * 24274 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24275 { 24276 tcp_hsp_t *hsp = NULL; 24277 24278 /* Quick check without acquiring the lock. */ 24279 if (tcps->tcps_hsp_hash == NULL) 24280 return (NULL); 24281 24282 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24283 24284 /* This routine finds the best-matching HSP for address addr. */ 24285 24286 if (tcps->tcps_hsp_hash) { 24287 int i; 24288 in6_addr_t v6srchaddr; 24289 tcp_hsp_t *hsp_net; 24290 24291 /* We do three passes: host, network, and subnet. */ 24292 24293 v6srchaddr = *v6addr; 24294 24295 for (i = 1; i <= 3; i++) { 24296 /* Look for exact match on srchaddr */ 24297 24298 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24299 V4_PART_OF_V6(v6srchaddr))]; 24300 while (hsp) { 24301 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24302 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24303 &v6srchaddr)) 24304 break; 24305 hsp = hsp->tcp_hsp_next; 24306 } 24307 24308 /* 24309 * If this is the first pass: 24310 * If we found a match, great, return it. 24311 * If not, search for the network on the second pass. 24312 */ 24313 24314 if (i == 1) 24315 if (hsp) 24316 break; 24317 else { 24318 /* Assume a 64 bit mask */ 24319 v6srchaddr.s6_addr32[0] = 24320 v6addr->s6_addr32[0]; 24321 v6srchaddr.s6_addr32[1] = 24322 v6addr->s6_addr32[1]; 24323 v6srchaddr.s6_addr32[2] = 0; 24324 v6srchaddr.s6_addr32[3] = 0; 24325 continue; 24326 } 24327 24328 /* 24329 * If this is the second pass: 24330 * If we found a match, but there's a subnet mask, 24331 * save the match but try again using the subnet 24332 * mask on the third pass. 24333 * Otherwise, return whatever we found. 24334 */ 24335 24336 if (i == 2) { 24337 ASSERT(hsp == NULL || 24338 hsp->tcp_hsp_vers == IPV6_VERSION); 24339 if (hsp && 24340 !IN6_IS_ADDR_UNSPECIFIED( 24341 &hsp->tcp_hsp_subnet_v6)) { 24342 hsp_net = hsp; 24343 V6_MASK_COPY(*v6addr, 24344 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24345 continue; 24346 } else { 24347 break; 24348 } 24349 } 24350 24351 /* 24352 * This must be the third pass. If we didn't find 24353 * anything, return the saved network HSP instead. 24354 */ 24355 24356 if (!hsp) 24357 hsp = hsp_net; 24358 } 24359 } 24360 24361 rw_exit(&tcps->tcps_hsp_lock); 24362 return (hsp); 24363 } 24364 24365 /* 24366 * Type three generator adapted from the random() function in 4.4 BSD: 24367 */ 24368 24369 /* 24370 * Copyright (c) 1983, 1993 24371 * The Regents of the University of California. All rights reserved. 24372 * 24373 * Redistribution and use in source and binary forms, with or without 24374 * modification, are permitted provided that the following conditions 24375 * are met: 24376 * 1. Redistributions of source code must retain the above copyright 24377 * notice, this list of conditions and the following disclaimer. 24378 * 2. Redistributions in binary form must reproduce the above copyright 24379 * notice, this list of conditions and the following disclaimer in the 24380 * documentation and/or other materials provided with the distribution. 24381 * 3. All advertising materials mentioning features or use of this software 24382 * must display the following acknowledgement: 24383 * This product includes software developed by the University of 24384 * California, Berkeley and its contributors. 24385 * 4. Neither the name of the University nor the names of its contributors 24386 * may be used to endorse or promote products derived from this software 24387 * without specific prior written permission. 24388 * 24389 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24390 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24391 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24392 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24393 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24394 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24395 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24396 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24397 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24398 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24399 * SUCH DAMAGE. 24400 */ 24401 24402 /* Type 3 -- x**31 + x**3 + 1 */ 24403 #define DEG_3 31 24404 #define SEP_3 3 24405 24406 24407 /* Protected by tcp_random_lock */ 24408 static int tcp_randtbl[DEG_3 + 1]; 24409 24410 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24411 static int *tcp_random_rptr = &tcp_randtbl[1]; 24412 24413 static int *tcp_random_state = &tcp_randtbl[1]; 24414 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24415 24416 kmutex_t tcp_random_lock; 24417 24418 void 24419 tcp_random_init(void) 24420 { 24421 int i; 24422 hrtime_t hrt; 24423 time_t wallclock; 24424 uint64_t result; 24425 24426 /* 24427 * Use high-res timer and current time for seed. Gethrtime() returns 24428 * a longlong, which may contain resolution down to nanoseconds. 24429 * The current time will either be a 32-bit or a 64-bit quantity. 24430 * XOR the two together in a 64-bit result variable. 24431 * Convert the result to a 32-bit value by multiplying the high-order 24432 * 32-bits by the low-order 32-bits. 24433 */ 24434 24435 hrt = gethrtime(); 24436 (void) drv_getparm(TIME, &wallclock); 24437 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24438 mutex_enter(&tcp_random_lock); 24439 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24440 (result & 0xffffffff); 24441 24442 for (i = 1; i < DEG_3; i++) 24443 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24444 + 12345; 24445 tcp_random_fptr = &tcp_random_state[SEP_3]; 24446 tcp_random_rptr = &tcp_random_state[0]; 24447 mutex_exit(&tcp_random_lock); 24448 for (i = 0; i < 10 * DEG_3; i++) 24449 (void) tcp_random(); 24450 } 24451 24452 /* 24453 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24454 * This range is selected to be approximately centered on TCP_ISS / 2, 24455 * and easy to compute. We get this value by generating a 32-bit random 24456 * number, selecting out the high-order 17 bits, and then adding one so 24457 * that we never return zero. 24458 */ 24459 int 24460 tcp_random(void) 24461 { 24462 int i; 24463 24464 mutex_enter(&tcp_random_lock); 24465 *tcp_random_fptr += *tcp_random_rptr; 24466 24467 /* 24468 * The high-order bits are more random than the low-order bits, 24469 * so we select out the high-order 17 bits and add one so that 24470 * we never return zero. 24471 */ 24472 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24473 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24474 tcp_random_fptr = tcp_random_state; 24475 ++tcp_random_rptr; 24476 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24477 tcp_random_rptr = tcp_random_state; 24478 24479 mutex_exit(&tcp_random_lock); 24480 return (i); 24481 } 24482 24483 static int 24484 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24485 int *t_errorp, int *sys_errorp) 24486 { 24487 int error; 24488 int is_absreq_failure; 24489 t_scalar_t *opt_lenp; 24490 t_scalar_t opt_offset; 24491 int prim_type; 24492 struct T_conn_req *tcreqp; 24493 struct T_conn_res *tcresp; 24494 cred_t *cr; 24495 24496 /* 24497 * All Solaris components should pass a db_credp 24498 * for this TPI message, hence we ASSERT. 24499 * But in case there is some other M_PROTO that looks 24500 * like a TPI message sent by some other kernel 24501 * component, we check and return an error. 24502 */ 24503 cr = msg_getcred(mp, NULL); 24504 ASSERT(cr != NULL); 24505 if (cr == NULL) 24506 return (-1); 24507 24508 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24509 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24510 prim_type == T_CONN_RES); 24511 24512 switch (prim_type) { 24513 case T_CONN_REQ: 24514 tcreqp = (struct T_conn_req *)mp->b_rptr; 24515 opt_offset = tcreqp->OPT_offset; 24516 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24517 break; 24518 case O_T_CONN_RES: 24519 case T_CONN_RES: 24520 tcresp = (struct T_conn_res *)mp->b_rptr; 24521 opt_offset = tcresp->OPT_offset; 24522 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24523 break; 24524 } 24525 24526 *t_errorp = 0; 24527 *sys_errorp = 0; 24528 *do_disconnectp = 0; 24529 24530 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24531 opt_offset, cr, &tcp_opt_obj, 24532 NULL, &is_absreq_failure); 24533 24534 switch (error) { 24535 case 0: /* no error */ 24536 ASSERT(is_absreq_failure == 0); 24537 return (0); 24538 case ENOPROTOOPT: 24539 *t_errorp = TBADOPT; 24540 break; 24541 case EACCES: 24542 *t_errorp = TACCES; 24543 break; 24544 default: 24545 *t_errorp = TSYSERR; *sys_errorp = error; 24546 break; 24547 } 24548 if (is_absreq_failure != 0) { 24549 /* 24550 * The connection request should get the local ack 24551 * T_OK_ACK and then a T_DISCON_IND. 24552 */ 24553 *do_disconnectp = 1; 24554 } 24555 return (-1); 24556 } 24557 24558 /* 24559 * Split this function out so that if the secret changes, I'm okay. 24560 * 24561 * Initialize the tcp_iss_cookie and tcp_iss_key. 24562 */ 24563 24564 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24565 24566 static void 24567 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24568 { 24569 struct { 24570 int32_t current_time; 24571 uint32_t randnum; 24572 uint16_t pad; 24573 uint8_t ether[6]; 24574 uint8_t passwd[PASSWD_SIZE]; 24575 } tcp_iss_cookie; 24576 time_t t; 24577 24578 /* 24579 * Start with the current absolute time. 24580 */ 24581 (void) drv_getparm(TIME, &t); 24582 tcp_iss_cookie.current_time = t; 24583 24584 /* 24585 * XXX - Need a more random number per RFC 1750, not this crap. 24586 * OTOH, if what follows is pretty random, then I'm in better shape. 24587 */ 24588 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24589 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24590 24591 /* 24592 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24593 * as a good template. 24594 */ 24595 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24596 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24597 24598 /* 24599 * The pass-phrase. Normally this is supplied by user-called NDD. 24600 */ 24601 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24602 24603 /* 24604 * See 4010593 if this section becomes a problem again, 24605 * but the local ethernet address is useful here. 24606 */ 24607 (void) localetheraddr(NULL, 24608 (struct ether_addr *)&tcp_iss_cookie.ether); 24609 24610 /* 24611 * Hash 'em all together. The MD5Final is called per-connection. 24612 */ 24613 mutex_enter(&tcps->tcps_iss_key_lock); 24614 MD5Init(&tcps->tcps_iss_key); 24615 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24616 sizeof (tcp_iss_cookie)); 24617 mutex_exit(&tcps->tcps_iss_key_lock); 24618 } 24619 24620 /* 24621 * Set the RFC 1948 pass phrase 24622 */ 24623 /* ARGSUSED */ 24624 static int 24625 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24626 cred_t *cr) 24627 { 24628 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24629 24630 /* 24631 * Basically, value contains a new pass phrase. Pass it along! 24632 */ 24633 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24634 return (0); 24635 } 24636 24637 /* ARGSUSED */ 24638 static int 24639 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24640 { 24641 bzero(buf, sizeof (tcp_sack_info_t)); 24642 return (0); 24643 } 24644 24645 /* ARGSUSED */ 24646 static int 24647 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24648 { 24649 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24650 return (0); 24651 } 24652 24653 /* 24654 * Make sure we wait until the default queue is setup, yet allow 24655 * tcp_g_q_create() to open a TCP stream. 24656 * We need to allow tcp_g_q_create() do do an open 24657 * of tcp, hence we compare curhread. 24658 * All others have to wait until the tcps_g_q has been 24659 * setup. 24660 */ 24661 void 24662 tcp_g_q_setup(tcp_stack_t *tcps) 24663 { 24664 mutex_enter(&tcps->tcps_g_q_lock); 24665 if (tcps->tcps_g_q != NULL) { 24666 mutex_exit(&tcps->tcps_g_q_lock); 24667 return; 24668 } 24669 if (tcps->tcps_g_q_creator == NULL) { 24670 /* This thread will set it up */ 24671 tcps->tcps_g_q_creator = curthread; 24672 mutex_exit(&tcps->tcps_g_q_lock); 24673 tcp_g_q_create(tcps); 24674 mutex_enter(&tcps->tcps_g_q_lock); 24675 ASSERT(tcps->tcps_g_q_creator == curthread); 24676 tcps->tcps_g_q_creator = NULL; 24677 cv_signal(&tcps->tcps_g_q_cv); 24678 ASSERT(tcps->tcps_g_q != NULL); 24679 mutex_exit(&tcps->tcps_g_q_lock); 24680 return; 24681 } 24682 /* Everybody but the creator has to wait */ 24683 if (tcps->tcps_g_q_creator != curthread) { 24684 while (tcps->tcps_g_q == NULL) 24685 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24686 } 24687 mutex_exit(&tcps->tcps_g_q_lock); 24688 } 24689 24690 #define IP "ip" 24691 24692 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24693 24694 /* 24695 * Create a default tcp queue here instead of in strplumb 24696 */ 24697 void 24698 tcp_g_q_create(tcp_stack_t *tcps) 24699 { 24700 int error; 24701 ldi_handle_t lh = NULL; 24702 ldi_ident_t li = NULL; 24703 int rval; 24704 cred_t *cr; 24705 major_t IP_MAJ; 24706 24707 #ifdef NS_DEBUG 24708 (void) printf("tcp_g_q_create()\n"); 24709 #endif 24710 24711 IP_MAJ = ddi_name_to_major(IP); 24712 24713 ASSERT(tcps->tcps_g_q_creator == curthread); 24714 24715 error = ldi_ident_from_major(IP_MAJ, &li); 24716 if (error) { 24717 #ifdef DEBUG 24718 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24719 error); 24720 #endif 24721 return; 24722 } 24723 24724 cr = zone_get_kcred(netstackid_to_zoneid( 24725 tcps->tcps_netstack->netstack_stackid)); 24726 ASSERT(cr != NULL); 24727 /* 24728 * We set the tcp default queue to IPv6 because IPv4 falls 24729 * back to IPv6 when it can't find a client, but 24730 * IPv6 does not fall back to IPv4. 24731 */ 24732 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24733 if (error) { 24734 #ifdef DEBUG 24735 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24736 error); 24737 #endif 24738 goto out; 24739 } 24740 24741 /* 24742 * This ioctl causes the tcp framework to cache a pointer to 24743 * this stream, so we don't want to close the stream after 24744 * this operation. 24745 * Use the kernel credentials that are for the zone we're in. 24746 */ 24747 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24748 (intptr_t)0, FKIOCTL, cr, &rval); 24749 if (error) { 24750 #ifdef DEBUG 24751 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24752 "error %d\n", error); 24753 #endif 24754 goto out; 24755 } 24756 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24757 lh = NULL; 24758 out: 24759 /* Close layered handles */ 24760 if (li) 24761 ldi_ident_release(li); 24762 /* Keep cred around until _inactive needs it */ 24763 tcps->tcps_g_q_cr = cr; 24764 } 24765 24766 /* 24767 * We keep tcp_g_q set until all other tcp_t's in the zone 24768 * has gone away, and then when tcp_g_q_inactive() is called 24769 * we clear it. 24770 */ 24771 void 24772 tcp_g_q_destroy(tcp_stack_t *tcps) 24773 { 24774 #ifdef NS_DEBUG 24775 (void) printf("tcp_g_q_destroy()for stack %d\n", 24776 tcps->tcps_netstack->netstack_stackid); 24777 #endif 24778 24779 if (tcps->tcps_g_q == NULL) { 24780 return; /* Nothing to cleanup */ 24781 } 24782 /* 24783 * Drop reference corresponding to the default queue. 24784 * This reference was added from tcp_open when the default queue 24785 * was created, hence we compensate for this extra drop in 24786 * tcp_g_q_close. If the refcnt drops to zero here it means 24787 * the default queue was the last one to be open, in which 24788 * case, then tcp_g_q_inactive will be 24789 * called as a result of the refrele. 24790 */ 24791 TCPS_REFRELE(tcps); 24792 } 24793 24794 /* 24795 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24796 * Run by tcp_q_q_inactive using a taskq. 24797 */ 24798 static void 24799 tcp_g_q_close(void *arg) 24800 { 24801 tcp_stack_t *tcps = arg; 24802 int error; 24803 ldi_handle_t lh = NULL; 24804 ldi_ident_t li = NULL; 24805 cred_t *cr; 24806 major_t IP_MAJ; 24807 24808 IP_MAJ = ddi_name_to_major(IP); 24809 24810 #ifdef NS_DEBUG 24811 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24812 tcps->tcps_netstack->netstack_stackid, 24813 tcps->tcps_netstack->netstack_refcnt); 24814 #endif 24815 lh = tcps->tcps_g_q_lh; 24816 if (lh == NULL) 24817 return; /* Nothing to cleanup */ 24818 24819 ASSERT(tcps->tcps_refcnt == 1); 24820 ASSERT(tcps->tcps_g_q != NULL); 24821 24822 error = ldi_ident_from_major(IP_MAJ, &li); 24823 if (error) { 24824 #ifdef DEBUG 24825 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24826 error); 24827 #endif 24828 return; 24829 } 24830 24831 cr = tcps->tcps_g_q_cr; 24832 tcps->tcps_g_q_cr = NULL; 24833 ASSERT(cr != NULL); 24834 24835 /* 24836 * Make sure we can break the recursion when tcp_close decrements 24837 * the reference count causing g_q_inactive to be called again. 24838 */ 24839 tcps->tcps_g_q_lh = NULL; 24840 24841 /* close the default queue */ 24842 (void) ldi_close(lh, FREAD|FWRITE, cr); 24843 /* 24844 * At this point in time tcps and the rest of netstack_t might 24845 * have been deleted. 24846 */ 24847 tcps = NULL; 24848 24849 /* Close layered handles */ 24850 ldi_ident_release(li); 24851 crfree(cr); 24852 } 24853 24854 /* 24855 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24856 * 24857 * Have to ensure that the ldi routines are not used by an 24858 * interrupt thread by using a taskq. 24859 */ 24860 void 24861 tcp_g_q_inactive(tcp_stack_t *tcps) 24862 { 24863 if (tcps->tcps_g_q_lh == NULL) 24864 return; /* Nothing to cleanup */ 24865 24866 ASSERT(tcps->tcps_refcnt == 0); 24867 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24868 24869 if (servicing_interrupt()) { 24870 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24871 (void *) tcps, TQ_SLEEP); 24872 } else { 24873 tcp_g_q_close(tcps); 24874 } 24875 } 24876 24877 /* 24878 * Called by IP when IP is loaded into the kernel 24879 */ 24880 void 24881 tcp_ddi_g_init(void) 24882 { 24883 tcp_timercache = kmem_cache_create("tcp_timercache", 24884 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24885 NULL, NULL, NULL, NULL, NULL, 0); 24886 24887 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24888 sizeof (tcp_sack_info_t), 0, 24889 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24890 24891 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24892 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24893 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24894 24895 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24896 24897 /* Initialize the random number generator */ 24898 tcp_random_init(); 24899 24900 /* A single callback independently of how many netstacks we have */ 24901 ip_squeue_init(tcp_squeue_add); 24902 24903 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24904 24905 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24906 TASKQ_PREPOPULATE); 24907 24908 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24909 24910 /* 24911 * We want to be informed each time a stack is created or 24912 * destroyed in the kernel, so we can maintain the 24913 * set of tcp_stack_t's. 24914 */ 24915 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24916 tcp_stack_fini); 24917 } 24918 24919 24920 #define INET_NAME "ip" 24921 24922 /* 24923 * Initialize the TCP stack instance. 24924 */ 24925 static void * 24926 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24927 { 24928 tcp_stack_t *tcps; 24929 tcpparam_t *pa; 24930 int i; 24931 int error = 0; 24932 major_t major; 24933 24934 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24935 tcps->tcps_netstack = ns; 24936 24937 /* Initialize locks */ 24938 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24939 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24940 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24941 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24942 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24943 24944 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24945 tcps->tcps_g_epriv_ports[0] = 2049; 24946 tcps->tcps_g_epriv_ports[1] = 4045; 24947 tcps->tcps_min_anonpriv_port = 512; 24948 24949 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24950 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24951 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24952 TCP_FANOUT_SIZE, KM_SLEEP); 24953 24954 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24955 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24956 MUTEX_DEFAULT, NULL); 24957 } 24958 24959 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24960 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24961 MUTEX_DEFAULT, NULL); 24962 } 24963 24964 /* TCP's IPsec code calls the packet dropper. */ 24965 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24966 24967 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24968 tcps->tcps_params = pa; 24969 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24970 24971 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24972 A_CNT(lcl_tcp_param_arr), tcps); 24973 24974 /* 24975 * Note: To really walk the device tree you need the devinfo 24976 * pointer to your device which is only available after probe/attach. 24977 * The following is safe only because it uses ddi_root_node() 24978 */ 24979 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24980 tcp_opt_obj.odb_opt_arr_cnt); 24981 24982 /* 24983 * Initialize RFC 1948 secret values. This will probably be reset once 24984 * by the boot scripts. 24985 * 24986 * Use NULL name, as the name is caught by the new lockstats. 24987 * 24988 * Initialize with some random, non-guessable string, like the global 24989 * T_INFO_ACK. 24990 */ 24991 24992 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24993 sizeof (tcp_g_t_info_ack), tcps); 24994 24995 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24996 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24997 24998 major = mod_name_to_major(INET_NAME); 24999 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 25000 ASSERT(error == 0); 25001 return (tcps); 25002 } 25003 25004 /* 25005 * Called when the IP module is about to be unloaded. 25006 */ 25007 void 25008 tcp_ddi_g_destroy(void) 25009 { 25010 tcp_g_kstat_fini(tcp_g_kstat); 25011 tcp_g_kstat = NULL; 25012 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 25013 25014 mutex_destroy(&tcp_random_lock); 25015 25016 kmem_cache_destroy(tcp_timercache); 25017 kmem_cache_destroy(tcp_sack_info_cache); 25018 kmem_cache_destroy(tcp_iphc_cache); 25019 25020 netstack_unregister(NS_TCP); 25021 taskq_destroy(tcp_taskq); 25022 } 25023 25024 /* 25025 * Shut down the TCP stack instance. 25026 */ 25027 /* ARGSUSED */ 25028 static void 25029 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25030 { 25031 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25032 25033 tcp_g_q_destroy(tcps); 25034 } 25035 25036 /* 25037 * Free the TCP stack instance. 25038 */ 25039 static void 25040 tcp_stack_fini(netstackid_t stackid, void *arg) 25041 { 25042 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25043 int i; 25044 25045 nd_free(&tcps->tcps_g_nd); 25046 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25047 tcps->tcps_params = NULL; 25048 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25049 tcps->tcps_wroff_xtra_param = NULL; 25050 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25051 tcps->tcps_mdt_head_param = NULL; 25052 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25053 tcps->tcps_mdt_tail_param = NULL; 25054 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25055 tcps->tcps_mdt_max_pbufs_param = NULL; 25056 25057 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25058 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25059 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25060 } 25061 25062 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25063 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25064 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25065 } 25066 25067 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25068 tcps->tcps_bind_fanout = NULL; 25069 25070 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25071 tcps->tcps_acceptor_fanout = NULL; 25072 25073 mutex_destroy(&tcps->tcps_iss_key_lock); 25074 rw_destroy(&tcps->tcps_hsp_lock); 25075 mutex_destroy(&tcps->tcps_g_q_lock); 25076 cv_destroy(&tcps->tcps_g_q_cv); 25077 mutex_destroy(&tcps->tcps_epriv_port_lock); 25078 25079 ip_drop_unregister(&tcps->tcps_dropper); 25080 25081 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25082 tcps->tcps_kstat = NULL; 25083 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25084 25085 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25086 tcps->tcps_mibkp = NULL; 25087 25088 ldi_ident_release(tcps->tcps_ldi_ident); 25089 kmem_free(tcps, sizeof (*tcps)); 25090 } 25091 25092 /* 25093 * Generate ISS, taking into account NDD changes may happen halfway through. 25094 * (If the iss is not zero, set it.) 25095 */ 25096 25097 static void 25098 tcp_iss_init(tcp_t *tcp) 25099 { 25100 MD5_CTX context; 25101 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25102 uint32_t answer[4]; 25103 tcp_stack_t *tcps = tcp->tcp_tcps; 25104 25105 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25106 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25107 switch (tcps->tcps_strong_iss) { 25108 case 2: 25109 mutex_enter(&tcps->tcps_iss_key_lock); 25110 context = tcps->tcps_iss_key; 25111 mutex_exit(&tcps->tcps_iss_key_lock); 25112 arg.ports = tcp->tcp_ports; 25113 if (tcp->tcp_ipversion == IPV4_VERSION) { 25114 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25115 &arg.src); 25116 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25117 &arg.dst); 25118 } else { 25119 arg.src = tcp->tcp_ip6h->ip6_src; 25120 arg.dst = tcp->tcp_ip6h->ip6_dst; 25121 } 25122 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25123 MD5Final((uchar_t *)answer, &context); 25124 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25125 /* 25126 * Now that we've hashed into a unique per-connection sequence 25127 * space, add a random increment per strong_iss == 1. So I 25128 * guess we'll have to... 25129 */ 25130 /* FALLTHRU */ 25131 case 1: 25132 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25133 break; 25134 default: 25135 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25136 break; 25137 } 25138 tcp->tcp_valid_bits = TCP_ISS_VALID; 25139 tcp->tcp_fss = tcp->tcp_iss - 1; 25140 tcp->tcp_suna = tcp->tcp_iss; 25141 tcp->tcp_snxt = tcp->tcp_iss + 1; 25142 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25143 tcp->tcp_csuna = tcp->tcp_snxt; 25144 } 25145 25146 /* 25147 * Exported routine for extracting active tcp connection status. 25148 * 25149 * This is used by the Solaris Cluster Networking software to 25150 * gather a list of connections that need to be forwarded to 25151 * specific nodes in the cluster when configuration changes occur. 25152 * 25153 * The callback is invoked for each tcp_t structure from all netstacks, 25154 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 25155 * from the netstack with the specified stack_id. Returning 25156 * non-zero from the callback routine terminates the search. 25157 */ 25158 int 25159 cl_tcp_walk_list(netstackid_t stack_id, 25160 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 25161 { 25162 netstack_handle_t nh; 25163 netstack_t *ns; 25164 int ret = 0; 25165 25166 if (stack_id >= 0) { 25167 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 25168 return (EINVAL); 25169 25170 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25171 ns->netstack_tcp); 25172 netstack_rele(ns); 25173 return (ret); 25174 } 25175 25176 netstack_next_init(&nh); 25177 while ((ns = netstack_next(&nh)) != NULL) { 25178 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25179 ns->netstack_tcp); 25180 netstack_rele(ns); 25181 } 25182 netstack_next_fini(&nh); 25183 return (ret); 25184 } 25185 25186 static int 25187 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25188 tcp_stack_t *tcps) 25189 { 25190 tcp_t *tcp; 25191 cl_tcp_info_t cl_tcpi; 25192 connf_t *connfp; 25193 conn_t *connp; 25194 int i; 25195 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25196 25197 ASSERT(callback != NULL); 25198 25199 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25200 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25201 connp = NULL; 25202 25203 while ((connp = 25204 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25205 25206 tcp = connp->conn_tcp; 25207 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25208 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25209 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25210 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25211 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25212 /* 25213 * The macros tcp_laddr and tcp_faddr give the IPv4 25214 * addresses. They are copied implicitly below as 25215 * mapped addresses. 25216 */ 25217 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25218 if (tcp->tcp_ipversion == IPV4_VERSION) { 25219 cl_tcpi.cl_tcpi_faddr = 25220 tcp->tcp_ipha->ipha_dst; 25221 } else { 25222 cl_tcpi.cl_tcpi_faddr_v6 = 25223 tcp->tcp_ip6h->ip6_dst; 25224 } 25225 25226 /* 25227 * If the callback returns non-zero 25228 * we terminate the traversal. 25229 */ 25230 if ((*callback)(&cl_tcpi, arg) != 0) { 25231 CONN_DEC_REF(tcp->tcp_connp); 25232 return (1); 25233 } 25234 } 25235 } 25236 25237 return (0); 25238 } 25239 25240 /* 25241 * Macros used for accessing the different types of sockaddr 25242 * structures inside a tcp_ioc_abort_conn_t. 25243 */ 25244 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25245 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25246 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25247 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25248 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25249 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25250 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25251 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25252 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25253 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25254 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25255 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25256 25257 /* 25258 * Return the correct error code to mimic the behavior 25259 * of a connection reset. 25260 */ 25261 #define TCP_AC_GET_ERRCODE(state, err) { \ 25262 switch ((state)) { \ 25263 case TCPS_SYN_SENT: \ 25264 case TCPS_SYN_RCVD: \ 25265 (err) = ECONNREFUSED; \ 25266 break; \ 25267 case TCPS_ESTABLISHED: \ 25268 case TCPS_FIN_WAIT_1: \ 25269 case TCPS_FIN_WAIT_2: \ 25270 case TCPS_CLOSE_WAIT: \ 25271 (err) = ECONNRESET; \ 25272 break; \ 25273 case TCPS_CLOSING: \ 25274 case TCPS_LAST_ACK: \ 25275 case TCPS_TIME_WAIT: \ 25276 (err) = 0; \ 25277 break; \ 25278 default: \ 25279 (err) = ENXIO; \ 25280 } \ 25281 } 25282 25283 /* 25284 * Check if a tcp structure matches the info in acp. 25285 */ 25286 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25287 (((acp)->ac_local.ss_family == AF_INET) ? \ 25288 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25289 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25290 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25291 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25292 (TCP_AC_V4LPORT((acp)) == 0 || \ 25293 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25294 (TCP_AC_V4RPORT((acp)) == 0 || \ 25295 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25296 (acp)->ac_start <= (tcp)->tcp_state && \ 25297 (acp)->ac_end >= (tcp)->tcp_state) : \ 25298 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25299 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25300 &(tcp)->tcp_ip_src_v6)) && \ 25301 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25302 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25303 &(tcp)->tcp_remote_v6)) && \ 25304 (TCP_AC_V6LPORT((acp)) == 0 || \ 25305 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25306 (TCP_AC_V6RPORT((acp)) == 0 || \ 25307 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25308 (acp)->ac_start <= (tcp)->tcp_state && \ 25309 (acp)->ac_end >= (tcp)->tcp_state)) 25310 25311 #define TCP_AC_MATCH(acp, tcp) \ 25312 (((acp)->ac_zoneid == ALL_ZONES || \ 25313 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25314 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25315 25316 /* 25317 * Build a message containing a tcp_ioc_abort_conn_t structure 25318 * which is filled in with information from acp and tp. 25319 */ 25320 static mblk_t * 25321 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25322 { 25323 mblk_t *mp; 25324 tcp_ioc_abort_conn_t *tacp; 25325 25326 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25327 if (mp == NULL) 25328 return (NULL); 25329 25330 mp->b_datap->db_type = M_CTL; 25331 25332 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25333 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25334 sizeof (uint32_t)); 25335 25336 tacp->ac_start = acp->ac_start; 25337 tacp->ac_end = acp->ac_end; 25338 tacp->ac_zoneid = acp->ac_zoneid; 25339 25340 if (acp->ac_local.ss_family == AF_INET) { 25341 tacp->ac_local.ss_family = AF_INET; 25342 tacp->ac_remote.ss_family = AF_INET; 25343 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25344 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25345 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25346 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25347 } else { 25348 tacp->ac_local.ss_family = AF_INET6; 25349 tacp->ac_remote.ss_family = AF_INET6; 25350 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25351 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25352 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25353 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25354 } 25355 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25356 return (mp); 25357 } 25358 25359 /* 25360 * Print a tcp_ioc_abort_conn_t structure. 25361 */ 25362 static void 25363 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25364 { 25365 char lbuf[128]; 25366 char rbuf[128]; 25367 sa_family_t af; 25368 in_port_t lport, rport; 25369 ushort_t logflags; 25370 25371 af = acp->ac_local.ss_family; 25372 25373 if (af == AF_INET) { 25374 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25375 lbuf, 128); 25376 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25377 rbuf, 128); 25378 lport = ntohs(TCP_AC_V4LPORT(acp)); 25379 rport = ntohs(TCP_AC_V4RPORT(acp)); 25380 } else { 25381 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25382 lbuf, 128); 25383 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25384 rbuf, 128); 25385 lport = ntohs(TCP_AC_V6LPORT(acp)); 25386 rport = ntohs(TCP_AC_V6RPORT(acp)); 25387 } 25388 25389 logflags = SL_TRACE | SL_NOTE; 25390 /* 25391 * Don't print this message to the console if the operation was done 25392 * to a non-global zone. 25393 */ 25394 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25395 logflags |= SL_CONSOLE; 25396 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25397 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25398 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25399 acp->ac_start, acp->ac_end); 25400 } 25401 25402 /* 25403 * Called inside tcp_rput when a message built using 25404 * tcp_ioctl_abort_build_msg is put into a queue. 25405 * Note that when we get here there is no wildcard in acp any more. 25406 */ 25407 static void 25408 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25409 { 25410 tcp_ioc_abort_conn_t *acp; 25411 25412 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25413 if (tcp->tcp_state <= acp->ac_end) { 25414 /* 25415 * If we get here, we are already on the correct 25416 * squeue. This ioctl follows the following path 25417 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25418 * ->tcp_ioctl_abort->squeue_enter (if on a 25419 * different squeue) 25420 */ 25421 int errcode; 25422 25423 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25424 (void) tcp_clean_death(tcp, errcode, 26); 25425 } 25426 freemsg(mp); 25427 } 25428 25429 /* 25430 * Abort all matching connections on a hash chain. 25431 */ 25432 static int 25433 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25434 boolean_t exact, tcp_stack_t *tcps) 25435 { 25436 int nmatch, err = 0; 25437 tcp_t *tcp; 25438 MBLKP mp, last, listhead = NULL; 25439 conn_t *tconnp; 25440 connf_t *connfp; 25441 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25442 25443 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25444 25445 startover: 25446 nmatch = 0; 25447 25448 mutex_enter(&connfp->connf_lock); 25449 for (tconnp = connfp->connf_head; tconnp != NULL; 25450 tconnp = tconnp->conn_next) { 25451 tcp = tconnp->conn_tcp; 25452 if (TCP_AC_MATCH(acp, tcp)) { 25453 CONN_INC_REF(tcp->tcp_connp); 25454 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25455 if (mp == NULL) { 25456 err = ENOMEM; 25457 CONN_DEC_REF(tcp->tcp_connp); 25458 break; 25459 } 25460 mp->b_prev = (mblk_t *)tcp; 25461 25462 if (listhead == NULL) { 25463 listhead = mp; 25464 last = mp; 25465 } else { 25466 last->b_next = mp; 25467 last = mp; 25468 } 25469 nmatch++; 25470 if (exact) 25471 break; 25472 } 25473 25474 /* Avoid holding lock for too long. */ 25475 if (nmatch >= 500) 25476 break; 25477 } 25478 mutex_exit(&connfp->connf_lock); 25479 25480 /* Pass mp into the correct tcp */ 25481 while ((mp = listhead) != NULL) { 25482 listhead = listhead->b_next; 25483 tcp = (tcp_t *)mp->b_prev; 25484 mp->b_next = mp->b_prev = NULL; 25485 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 25486 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 25487 } 25488 25489 *count += nmatch; 25490 if (nmatch >= 500 && err == 0) 25491 goto startover; 25492 return (err); 25493 } 25494 25495 /* 25496 * Abort all connections that matches the attributes specified in acp. 25497 */ 25498 static int 25499 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25500 { 25501 sa_family_t af; 25502 uint32_t ports; 25503 uint16_t *pports; 25504 int err = 0, count = 0; 25505 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25506 int index = -1; 25507 ushort_t logflags; 25508 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25509 25510 af = acp->ac_local.ss_family; 25511 25512 if (af == AF_INET) { 25513 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25514 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25515 pports = (uint16_t *)&ports; 25516 pports[1] = TCP_AC_V4LPORT(acp); 25517 pports[0] = TCP_AC_V4RPORT(acp); 25518 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25519 } 25520 } else { 25521 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25522 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25523 pports = (uint16_t *)&ports; 25524 pports[1] = TCP_AC_V6LPORT(acp); 25525 pports[0] = TCP_AC_V6RPORT(acp); 25526 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25527 } 25528 } 25529 25530 /* 25531 * For cases where remote addr, local port, and remote port are non- 25532 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25533 */ 25534 if (index != -1) { 25535 err = tcp_ioctl_abort_bucket(acp, index, 25536 &count, exact, tcps); 25537 } else { 25538 /* 25539 * loop through all entries for wildcard case 25540 */ 25541 for (index = 0; 25542 index < ipst->ips_ipcl_conn_fanout_size; 25543 index++) { 25544 err = tcp_ioctl_abort_bucket(acp, index, 25545 &count, exact, tcps); 25546 if (err != 0) 25547 break; 25548 } 25549 } 25550 25551 logflags = SL_TRACE | SL_NOTE; 25552 /* 25553 * Don't print this message to the console if the operation was done 25554 * to a non-global zone. 25555 */ 25556 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25557 logflags |= SL_CONSOLE; 25558 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25559 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25560 if (err == 0 && count == 0) 25561 err = ENOENT; 25562 return (err); 25563 } 25564 25565 /* 25566 * Process the TCP_IOC_ABORT_CONN ioctl request. 25567 */ 25568 static void 25569 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25570 { 25571 int err; 25572 IOCP iocp; 25573 MBLKP mp1; 25574 sa_family_t laf, raf; 25575 tcp_ioc_abort_conn_t *acp; 25576 zone_t *zptr; 25577 conn_t *connp = Q_TO_CONN(q); 25578 zoneid_t zoneid = connp->conn_zoneid; 25579 tcp_t *tcp = connp->conn_tcp; 25580 tcp_stack_t *tcps = tcp->tcp_tcps; 25581 25582 iocp = (IOCP)mp->b_rptr; 25583 25584 if ((mp1 = mp->b_cont) == NULL || 25585 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25586 err = EINVAL; 25587 goto out; 25588 } 25589 25590 /* check permissions */ 25591 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25592 err = EPERM; 25593 goto out; 25594 } 25595 25596 if (mp1->b_cont != NULL) { 25597 freemsg(mp1->b_cont); 25598 mp1->b_cont = NULL; 25599 } 25600 25601 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25602 laf = acp->ac_local.ss_family; 25603 raf = acp->ac_remote.ss_family; 25604 25605 /* check that a zone with the supplied zoneid exists */ 25606 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25607 zptr = zone_find_by_id(zoneid); 25608 if (zptr != NULL) { 25609 zone_rele(zptr); 25610 } else { 25611 err = EINVAL; 25612 goto out; 25613 } 25614 } 25615 25616 /* 25617 * For exclusive stacks we set the zoneid to zero 25618 * to make TCP operate as if in the global zone. 25619 */ 25620 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25621 acp->ac_zoneid = GLOBAL_ZONEID; 25622 25623 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25624 acp->ac_start > acp->ac_end || laf != raf || 25625 (laf != AF_INET && laf != AF_INET6)) { 25626 err = EINVAL; 25627 goto out; 25628 } 25629 25630 tcp_ioctl_abort_dump(acp); 25631 err = tcp_ioctl_abort(acp, tcps); 25632 25633 out: 25634 if (mp1 != NULL) { 25635 freemsg(mp1); 25636 mp->b_cont = NULL; 25637 } 25638 25639 if (err != 0) 25640 miocnak(q, mp, 0, err); 25641 else 25642 miocack(q, mp, 0, 0); 25643 } 25644 25645 /* 25646 * tcp_time_wait_processing() handles processing of incoming packets when 25647 * the tcp is in the TIME_WAIT state. 25648 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25649 * on the time wait list. 25650 */ 25651 void 25652 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25653 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25654 { 25655 int32_t bytes_acked; 25656 int32_t gap; 25657 int32_t rgap; 25658 tcp_opt_t tcpopt; 25659 uint_t flags; 25660 uint32_t new_swnd = 0; 25661 conn_t *connp; 25662 tcp_stack_t *tcps = tcp->tcp_tcps; 25663 25664 BUMP_LOCAL(tcp->tcp_ibsegs); 25665 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25666 25667 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25668 new_swnd = BE16_TO_U16(tcph->th_win) << 25669 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25670 if (tcp->tcp_snd_ts_ok) { 25671 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25672 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25673 tcp->tcp_rnxt, TH_ACK); 25674 goto done; 25675 } 25676 } 25677 gap = seg_seq - tcp->tcp_rnxt; 25678 rgap = tcp->tcp_rwnd - (gap + seg_len); 25679 if (gap < 0) { 25680 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25681 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25682 (seg_len > -gap ? -gap : seg_len)); 25683 seg_len += gap; 25684 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25685 if (flags & TH_RST) { 25686 goto done; 25687 } 25688 if ((flags & TH_FIN) && seg_len == -1) { 25689 /* 25690 * When TCP receives a duplicate FIN in 25691 * TIME_WAIT state, restart the 2 MSL timer. 25692 * See page 73 in RFC 793. Make sure this TCP 25693 * is already on the TIME_WAIT list. If not, 25694 * just restart the timer. 25695 */ 25696 if (TCP_IS_DETACHED(tcp)) { 25697 if (tcp_time_wait_remove(tcp, NULL) == 25698 B_TRUE) { 25699 tcp_time_wait_append(tcp); 25700 TCP_DBGSTAT(tcps, 25701 tcp_rput_time_wait); 25702 } 25703 } else { 25704 ASSERT(tcp != NULL); 25705 TCP_TIMER_RESTART(tcp, 25706 tcps->tcps_time_wait_interval); 25707 } 25708 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25709 tcp->tcp_rnxt, TH_ACK); 25710 goto done; 25711 } 25712 flags |= TH_ACK_NEEDED; 25713 seg_len = 0; 25714 goto process_ack; 25715 } 25716 25717 /* Fix seg_seq, and chew the gap off the front. */ 25718 seg_seq = tcp->tcp_rnxt; 25719 } 25720 25721 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25722 /* 25723 * Make sure that when we accept the connection, pick 25724 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25725 * old connection. 25726 * 25727 * The next ISS generated is equal to tcp_iss_incr_extra 25728 * + ISS_INCR/2 + other components depending on the 25729 * value of tcp_strong_iss. We pre-calculate the new 25730 * ISS here and compare with tcp_snxt to determine if 25731 * we need to make adjustment to tcp_iss_incr_extra. 25732 * 25733 * The above calculation is ugly and is a 25734 * waste of CPU cycles... 25735 */ 25736 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25737 int32_t adj; 25738 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25739 25740 switch (tcps->tcps_strong_iss) { 25741 case 2: { 25742 /* Add time and MD5 components. */ 25743 uint32_t answer[4]; 25744 struct { 25745 uint32_t ports; 25746 in6_addr_t src; 25747 in6_addr_t dst; 25748 } arg; 25749 MD5_CTX context; 25750 25751 mutex_enter(&tcps->tcps_iss_key_lock); 25752 context = tcps->tcps_iss_key; 25753 mutex_exit(&tcps->tcps_iss_key_lock); 25754 arg.ports = tcp->tcp_ports; 25755 /* We use MAPPED addresses in tcp_iss_init */ 25756 arg.src = tcp->tcp_ip_src_v6; 25757 if (tcp->tcp_ipversion == IPV4_VERSION) { 25758 IN6_IPADDR_TO_V4MAPPED( 25759 tcp->tcp_ipha->ipha_dst, 25760 &arg.dst); 25761 } else { 25762 arg.dst = 25763 tcp->tcp_ip6h->ip6_dst; 25764 } 25765 MD5Update(&context, (uchar_t *)&arg, 25766 sizeof (arg)); 25767 MD5Final((uchar_t *)answer, &context); 25768 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25769 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25770 break; 25771 } 25772 case 1: 25773 /* Add time component and min random (i.e. 1). */ 25774 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25775 break; 25776 default: 25777 /* Add only time component. */ 25778 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25779 break; 25780 } 25781 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25782 /* 25783 * New ISS not guaranteed to be ISS_INCR/2 25784 * ahead of the current tcp_snxt, so add the 25785 * difference to tcp_iss_incr_extra. 25786 */ 25787 tcps->tcps_iss_incr_extra += adj; 25788 } 25789 /* 25790 * If tcp_clean_death() can not perform the task now, 25791 * drop the SYN packet and let the other side re-xmit. 25792 * Otherwise pass the SYN packet back in, since the 25793 * old tcp state has been cleaned up or freed. 25794 */ 25795 if (tcp_clean_death(tcp, 0, 27) == -1) 25796 goto done; 25797 /* 25798 * We will come back to tcp_rput_data 25799 * on the global queue. Packets destined 25800 * for the global queue will be checked 25801 * with global policy. But the policy for 25802 * this packet has already been checked as 25803 * this was destined for the detached 25804 * connection. We need to bypass policy 25805 * check this time by attaching a dummy 25806 * ipsec_in with ipsec_in_dont_check set. 25807 */ 25808 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25809 if (connp != NULL) { 25810 TCP_STAT(tcps, tcp_time_wait_syn_success); 25811 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25812 return; 25813 } 25814 goto done; 25815 } 25816 25817 /* 25818 * rgap is the amount of stuff received out of window. A negative 25819 * value is the amount out of window. 25820 */ 25821 if (rgap < 0) { 25822 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25823 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25824 /* Fix seg_len and make sure there is something left. */ 25825 seg_len += rgap; 25826 if (seg_len <= 0) { 25827 if (flags & TH_RST) { 25828 goto done; 25829 } 25830 flags |= TH_ACK_NEEDED; 25831 seg_len = 0; 25832 goto process_ack; 25833 } 25834 } 25835 /* 25836 * Check whether we can update tcp_ts_recent. This test is 25837 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25838 * Extensions for High Performance: An Update", Internet Draft. 25839 */ 25840 if (tcp->tcp_snd_ts_ok && 25841 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25842 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25843 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25844 tcp->tcp_last_rcv_lbolt = lbolt64; 25845 } 25846 25847 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25848 /* Always ack out of order packets */ 25849 flags |= TH_ACK_NEEDED; 25850 seg_len = 0; 25851 } else if (seg_len > 0) { 25852 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25853 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25854 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25855 } 25856 if (flags & TH_RST) { 25857 (void) tcp_clean_death(tcp, 0, 28); 25858 goto done; 25859 } 25860 if (flags & TH_SYN) { 25861 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25862 TH_RST|TH_ACK); 25863 /* 25864 * Do not delete the TCP structure if it is in 25865 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25866 */ 25867 goto done; 25868 } 25869 process_ack: 25870 if (flags & TH_ACK) { 25871 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25872 if (bytes_acked <= 0) { 25873 if (bytes_acked == 0 && seg_len == 0 && 25874 new_swnd == tcp->tcp_swnd) 25875 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25876 } else { 25877 /* Acks something not sent */ 25878 flags |= TH_ACK_NEEDED; 25879 } 25880 } 25881 if (flags & TH_ACK_NEEDED) { 25882 /* 25883 * Time to send an ack for some reason. 25884 */ 25885 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25886 tcp->tcp_rnxt, TH_ACK); 25887 } 25888 done: 25889 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25890 DB_CKSUMSTART(mp) = 0; 25891 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25892 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25893 } 25894 freemsg(mp); 25895 } 25896 25897 /* 25898 * TCP Timers Implementation. 25899 */ 25900 timeout_id_t 25901 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25902 { 25903 mblk_t *mp; 25904 tcp_timer_t *tcpt; 25905 tcp_t *tcp = connp->conn_tcp; 25906 25907 ASSERT(connp->conn_sqp != NULL); 25908 25909 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25910 25911 if (tcp->tcp_timercache == NULL) { 25912 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25913 } else { 25914 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25915 mp = tcp->tcp_timercache; 25916 tcp->tcp_timercache = mp->b_next; 25917 mp->b_next = NULL; 25918 ASSERT(mp->b_wptr == NULL); 25919 } 25920 25921 CONN_INC_REF(connp); 25922 tcpt = (tcp_timer_t *)mp->b_rptr; 25923 tcpt->connp = connp; 25924 tcpt->tcpt_proc = f; 25925 /* 25926 * TCP timers are normal timeouts. Plus, they do not require more than 25927 * a 10 millisecond resolution. By choosing a coarser resolution and by 25928 * rounding up the expiration to the next resolution boundary, we can 25929 * batch timers in the callout subsystem to make TCP timers more 25930 * efficient. The roundup also protects short timers from expiring too 25931 * early before they have a chance to be cancelled. 25932 */ 25933 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25934 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25935 25936 return ((timeout_id_t)mp); 25937 } 25938 25939 static void 25940 tcp_timer_callback(void *arg) 25941 { 25942 mblk_t *mp = (mblk_t *)arg; 25943 tcp_timer_t *tcpt; 25944 conn_t *connp; 25945 25946 tcpt = (tcp_timer_t *)mp->b_rptr; 25947 connp = tcpt->connp; 25948 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25949 SQ_FILL, SQTAG_TCP_TIMER); 25950 } 25951 25952 static void 25953 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25954 { 25955 tcp_timer_t *tcpt; 25956 conn_t *connp = (conn_t *)arg; 25957 tcp_t *tcp = connp->conn_tcp; 25958 25959 tcpt = (tcp_timer_t *)mp->b_rptr; 25960 ASSERT(connp == tcpt->connp); 25961 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25962 25963 /* 25964 * If the TCP has reached the closed state, don't proceed any 25965 * further. This TCP logically does not exist on the system. 25966 * tcpt_proc could for example access queues, that have already 25967 * been qprocoff'ed off. Also see comments at the start of tcp_input 25968 */ 25969 if (tcp->tcp_state != TCPS_CLOSED) { 25970 (*tcpt->tcpt_proc)(connp); 25971 } else { 25972 tcp->tcp_timer_tid = 0; 25973 } 25974 tcp_timer_free(connp->conn_tcp, mp); 25975 } 25976 25977 /* 25978 * There is potential race with untimeout and the handler firing at the same 25979 * time. The mblock may be freed by the handler while we are trying to use 25980 * it. But since both should execute on the same squeue, this race should not 25981 * occur. 25982 */ 25983 clock_t 25984 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25985 { 25986 mblk_t *mp = (mblk_t *)id; 25987 tcp_timer_t *tcpt; 25988 clock_t delta; 25989 25990 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25991 25992 if (mp == NULL) 25993 return (-1); 25994 25995 tcpt = (tcp_timer_t *)mp->b_rptr; 25996 ASSERT(tcpt->connp == connp); 25997 25998 delta = untimeout_default(tcpt->tcpt_tid, 0); 25999 26000 if (delta >= 0) { 26001 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 26002 tcp_timer_free(connp->conn_tcp, mp); 26003 CONN_DEC_REF(connp); 26004 } 26005 26006 return (delta); 26007 } 26008 26009 /* 26010 * Allocate space for the timer event. The allocation looks like mblk, but it is 26011 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 26012 * 26013 * Dealing with failures: If we can't allocate from the timer cache we try 26014 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26015 * points to b_rptr. 26016 * If we can't allocate anything using allocb_tryhard(), we perform a last 26017 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26018 * save the actual allocation size in b_datap. 26019 */ 26020 mblk_t * 26021 tcp_timermp_alloc(int kmflags) 26022 { 26023 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26024 kmflags & ~KM_PANIC); 26025 26026 if (mp != NULL) { 26027 mp->b_next = mp->b_prev = NULL; 26028 mp->b_rptr = (uchar_t *)(&mp[1]); 26029 mp->b_wptr = NULL; 26030 mp->b_datap = NULL; 26031 mp->b_queue = NULL; 26032 mp->b_cont = NULL; 26033 } else if (kmflags & KM_PANIC) { 26034 /* 26035 * Failed to allocate memory for the timer. Try allocating from 26036 * dblock caches. 26037 */ 26038 /* ipclassifier calls this from a constructor - hence no tcps */ 26039 TCP_G_STAT(tcp_timermp_allocfail); 26040 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26041 if (mp == NULL) { 26042 size_t size = 0; 26043 /* 26044 * Memory is really low. Try tryhard allocation. 26045 * 26046 * ipclassifier calls this from a constructor - 26047 * hence no tcps 26048 */ 26049 TCP_G_STAT(tcp_timermp_allocdblfail); 26050 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26051 sizeof (tcp_timer_t), &size, kmflags); 26052 mp->b_rptr = (uchar_t *)(&mp[1]); 26053 mp->b_next = mp->b_prev = NULL; 26054 mp->b_wptr = (uchar_t *)-1; 26055 mp->b_datap = (dblk_t *)size; 26056 mp->b_queue = NULL; 26057 mp->b_cont = NULL; 26058 } 26059 ASSERT(mp->b_wptr != NULL); 26060 } 26061 /* ipclassifier calls this from a constructor - hence no tcps */ 26062 TCP_G_DBGSTAT(tcp_timermp_alloced); 26063 26064 return (mp); 26065 } 26066 26067 /* 26068 * Free per-tcp timer cache. 26069 * It can only contain entries from tcp_timercache. 26070 */ 26071 void 26072 tcp_timermp_free(tcp_t *tcp) 26073 { 26074 mblk_t *mp; 26075 26076 while ((mp = tcp->tcp_timercache) != NULL) { 26077 ASSERT(mp->b_wptr == NULL); 26078 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26079 kmem_cache_free(tcp_timercache, mp); 26080 } 26081 } 26082 26083 /* 26084 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26085 * events there already (currently at most two events are cached). 26086 * If the event is not allocated from the timer cache, free it right away. 26087 */ 26088 static void 26089 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26090 { 26091 mblk_t *mp1 = tcp->tcp_timercache; 26092 26093 if (mp->b_wptr != NULL) { 26094 /* 26095 * This allocation is not from a timer cache, free it right 26096 * away. 26097 */ 26098 if (mp->b_wptr != (uchar_t *)-1) 26099 freeb(mp); 26100 else 26101 kmem_free(mp, (size_t)mp->b_datap); 26102 } else if (mp1 == NULL || mp1->b_next == NULL) { 26103 /* Cache this timer block for future allocations */ 26104 mp->b_rptr = (uchar_t *)(&mp[1]); 26105 mp->b_next = mp1; 26106 tcp->tcp_timercache = mp; 26107 } else { 26108 kmem_cache_free(tcp_timercache, mp); 26109 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 26110 } 26111 } 26112 26113 /* 26114 * End of TCP Timers implementation. 26115 */ 26116 26117 /* 26118 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26119 * on the specified backing STREAMS q. Note, the caller may make the 26120 * decision to call based on the tcp_t.tcp_flow_stopped value which 26121 * when check outside the q's lock is only an advisory check ... 26122 */ 26123 void 26124 tcp_setqfull(tcp_t *tcp) 26125 { 26126 tcp_stack_t *tcps = tcp->tcp_tcps; 26127 conn_t *connp = tcp->tcp_connp; 26128 26129 if (tcp->tcp_closed) 26130 return; 26131 26132 if (IPCL_IS_NONSTR(connp)) { 26133 (*connp->conn_upcalls->su_txq_full) 26134 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 26135 tcp->tcp_flow_stopped = B_TRUE; 26136 } else { 26137 queue_t *q = tcp->tcp_wq; 26138 26139 if (!(q->q_flag & QFULL)) { 26140 mutex_enter(QLOCK(q)); 26141 if (!(q->q_flag & QFULL)) { 26142 /* still need to set QFULL */ 26143 q->q_flag |= QFULL; 26144 tcp->tcp_flow_stopped = B_TRUE; 26145 mutex_exit(QLOCK(q)); 26146 TCP_STAT(tcps, tcp_flwctl_on); 26147 } else { 26148 mutex_exit(QLOCK(q)); 26149 } 26150 } 26151 } 26152 } 26153 26154 void 26155 tcp_clrqfull(tcp_t *tcp) 26156 { 26157 conn_t *connp = tcp->tcp_connp; 26158 26159 if (tcp->tcp_closed) 26160 return; 26161 26162 if (IPCL_IS_NONSTR(connp)) { 26163 (*connp->conn_upcalls->su_txq_full) 26164 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 26165 tcp->tcp_flow_stopped = B_FALSE; 26166 } else { 26167 queue_t *q = tcp->tcp_wq; 26168 26169 if (q->q_flag & QFULL) { 26170 mutex_enter(QLOCK(q)); 26171 if (q->q_flag & QFULL) { 26172 q->q_flag &= ~QFULL; 26173 tcp->tcp_flow_stopped = B_FALSE; 26174 mutex_exit(QLOCK(q)); 26175 if (q->q_flag & QWANTW) 26176 qbackenable(q, 0); 26177 } else { 26178 mutex_exit(QLOCK(q)); 26179 } 26180 } 26181 } 26182 } 26183 26184 /* 26185 * kstats related to squeues i.e. not per IP instance 26186 */ 26187 static void * 26188 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26189 { 26190 kstat_t *ksp; 26191 26192 tcp_g_stat_t template = { 26193 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26194 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26195 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26196 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26197 }; 26198 26199 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26200 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26201 KSTAT_FLAG_VIRTUAL); 26202 26203 if (ksp == NULL) 26204 return (NULL); 26205 26206 bcopy(&template, tcp_g_statp, sizeof (template)); 26207 ksp->ks_data = (void *)tcp_g_statp; 26208 26209 kstat_install(ksp); 26210 return (ksp); 26211 } 26212 26213 static void 26214 tcp_g_kstat_fini(kstat_t *ksp) 26215 { 26216 if (ksp != NULL) { 26217 kstat_delete(ksp); 26218 } 26219 } 26220 26221 26222 static void * 26223 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26224 { 26225 kstat_t *ksp; 26226 26227 tcp_stat_t template = { 26228 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26229 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26230 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26231 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26232 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26233 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26234 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26235 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26236 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26237 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26238 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26239 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26240 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26241 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26242 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26243 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26244 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26245 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26246 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26247 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26248 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26249 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26250 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26251 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26252 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26253 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26254 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26255 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26256 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26257 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26258 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26259 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26260 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26261 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26262 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26263 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26264 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26265 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26266 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26267 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26268 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26269 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26270 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26271 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26272 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26273 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26274 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26275 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26276 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26277 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26278 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26279 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26280 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26281 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26282 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26283 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26284 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26285 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26286 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26287 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26288 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26289 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26290 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26291 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26292 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26293 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26294 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26295 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26296 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26297 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26298 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26299 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26300 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26301 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26302 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26303 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26304 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26305 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26306 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26307 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26308 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26309 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26310 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26311 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26312 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26313 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26314 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26315 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26316 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26317 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26318 }; 26319 26320 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26321 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26322 KSTAT_FLAG_VIRTUAL, stackid); 26323 26324 if (ksp == NULL) 26325 return (NULL); 26326 26327 bcopy(&template, tcps_statisticsp, sizeof (template)); 26328 ksp->ks_data = (void *)tcps_statisticsp; 26329 ksp->ks_private = (void *)(uintptr_t)stackid; 26330 26331 kstat_install(ksp); 26332 return (ksp); 26333 } 26334 26335 static void 26336 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26337 { 26338 if (ksp != NULL) { 26339 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26340 kstat_delete_netstack(ksp, stackid); 26341 } 26342 } 26343 26344 /* 26345 * TCP Kstats implementation 26346 */ 26347 static void * 26348 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26349 { 26350 kstat_t *ksp; 26351 26352 tcp_named_kstat_t template = { 26353 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26354 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26355 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26356 { "maxConn", KSTAT_DATA_INT32, 0 }, 26357 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26358 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26359 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26360 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26361 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26362 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26363 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26364 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26365 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26366 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26367 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26368 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26369 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26370 { "outAck", KSTAT_DATA_UINT32, 0 }, 26371 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26372 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26373 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26374 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26375 { "outControl", KSTAT_DATA_UINT32, 0 }, 26376 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26377 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26378 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26379 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26380 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26381 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26382 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26383 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26384 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26385 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26386 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26387 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26388 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26389 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26390 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26391 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26392 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26393 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26394 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26395 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26396 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26397 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26398 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26399 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26400 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26401 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26402 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26403 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26404 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26405 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26406 }; 26407 26408 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26409 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26410 26411 if (ksp == NULL) 26412 return (NULL); 26413 26414 template.rtoAlgorithm.value.ui32 = 4; 26415 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26416 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26417 template.maxConn.value.i32 = -1; 26418 26419 bcopy(&template, ksp->ks_data, sizeof (template)); 26420 ksp->ks_update = tcp_kstat_update; 26421 ksp->ks_private = (void *)(uintptr_t)stackid; 26422 26423 kstat_install(ksp); 26424 return (ksp); 26425 } 26426 26427 static void 26428 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26429 { 26430 if (ksp != NULL) { 26431 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26432 kstat_delete_netstack(ksp, stackid); 26433 } 26434 } 26435 26436 static int 26437 tcp_kstat_update(kstat_t *kp, int rw) 26438 { 26439 tcp_named_kstat_t *tcpkp; 26440 tcp_t *tcp; 26441 connf_t *connfp; 26442 conn_t *connp; 26443 int i; 26444 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26445 netstack_t *ns; 26446 tcp_stack_t *tcps; 26447 ip_stack_t *ipst; 26448 26449 if ((kp == NULL) || (kp->ks_data == NULL)) 26450 return (EIO); 26451 26452 if (rw == KSTAT_WRITE) 26453 return (EACCES); 26454 26455 ns = netstack_find_by_stackid(stackid); 26456 if (ns == NULL) 26457 return (-1); 26458 tcps = ns->netstack_tcp; 26459 if (tcps == NULL) { 26460 netstack_rele(ns); 26461 return (-1); 26462 } 26463 26464 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26465 26466 tcpkp->currEstab.value.ui32 = 0; 26467 26468 ipst = ns->netstack_ip; 26469 26470 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26471 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26472 connp = NULL; 26473 while ((connp = 26474 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26475 tcp = connp->conn_tcp; 26476 switch (tcp_snmp_state(tcp)) { 26477 case MIB2_TCP_established: 26478 case MIB2_TCP_closeWait: 26479 tcpkp->currEstab.value.ui32++; 26480 break; 26481 } 26482 } 26483 } 26484 26485 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26486 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26487 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26488 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26489 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26490 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26491 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26492 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26493 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26494 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26495 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26496 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26497 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26498 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26499 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26500 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26501 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26502 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26503 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26504 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26505 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26506 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26507 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26508 tcpkp->inDataInorderSegs.value.ui32 = 26509 tcps->tcps_mib.tcpInDataInorderSegs; 26510 tcpkp->inDataInorderBytes.value.ui32 = 26511 tcps->tcps_mib.tcpInDataInorderBytes; 26512 tcpkp->inDataUnorderSegs.value.ui32 = 26513 tcps->tcps_mib.tcpInDataUnorderSegs; 26514 tcpkp->inDataUnorderBytes.value.ui32 = 26515 tcps->tcps_mib.tcpInDataUnorderBytes; 26516 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26517 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26518 tcpkp->inDataPartDupSegs.value.ui32 = 26519 tcps->tcps_mib.tcpInDataPartDupSegs; 26520 tcpkp->inDataPartDupBytes.value.ui32 = 26521 tcps->tcps_mib.tcpInDataPartDupBytes; 26522 tcpkp->inDataPastWinSegs.value.ui32 = 26523 tcps->tcps_mib.tcpInDataPastWinSegs; 26524 tcpkp->inDataPastWinBytes.value.ui32 = 26525 tcps->tcps_mib.tcpInDataPastWinBytes; 26526 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26527 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26528 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26529 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26530 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26531 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26532 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26533 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26534 tcpkp->timKeepaliveProbe.value.ui32 = 26535 tcps->tcps_mib.tcpTimKeepaliveProbe; 26536 tcpkp->timKeepaliveDrop.value.ui32 = 26537 tcps->tcps_mib.tcpTimKeepaliveDrop; 26538 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26539 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26540 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26541 tcpkp->outSackRetransSegs.value.ui32 = 26542 tcps->tcps_mib.tcpOutSackRetransSegs; 26543 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26544 26545 netstack_rele(ns); 26546 return (0); 26547 } 26548 26549 void 26550 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26551 { 26552 uint16_t hdr_len; 26553 ipha_t *ipha; 26554 uint8_t *nexthdrp; 26555 tcph_t *tcph; 26556 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26557 26558 /* Already has an eager */ 26559 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26560 TCP_STAT(tcps, tcp_reinput_syn); 26561 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26562 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 26563 return; 26564 } 26565 26566 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26567 case IPV4_VERSION: 26568 ipha = (ipha_t *)mp->b_rptr; 26569 hdr_len = IPH_HDR_LENGTH(ipha); 26570 break; 26571 case IPV6_VERSION: 26572 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26573 &hdr_len, &nexthdrp)) { 26574 CONN_DEC_REF(connp); 26575 freemsg(mp); 26576 return; 26577 } 26578 break; 26579 } 26580 26581 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26582 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26583 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26584 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26585 } 26586 26587 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26588 SQ_FILL, SQTAG_TCP_REINPUT); 26589 } 26590 26591 static int 26592 tcp_squeue_switch(int val) 26593 { 26594 int rval = SQ_FILL; 26595 26596 switch (val) { 26597 case 1: 26598 rval = SQ_NODRAIN; 26599 break; 26600 case 2: 26601 rval = SQ_PROCESS; 26602 break; 26603 default: 26604 break; 26605 } 26606 return (rval); 26607 } 26608 26609 /* 26610 * This is called once for each squeue - globally for all stack 26611 * instances. 26612 */ 26613 static void 26614 tcp_squeue_add(squeue_t *sqp) 26615 { 26616 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26617 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26618 26619 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26620 tcp_time_wait->tcp_time_wait_tid = 26621 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26622 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26623 CALLOUT_FLAG_ROUNDUP); 26624 if (tcp_free_list_max_cnt == 0) { 26625 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26626 max_ncpus : boot_max_ncpus); 26627 26628 /* 26629 * Limit number of entries to 1% of availble memory / tcp_ncpus 26630 */ 26631 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26632 (tcp_ncpus * sizeof (tcp_t) * 100); 26633 } 26634 tcp_time_wait->tcp_free_list_cnt = 0; 26635 } 26636 26637 static int 26638 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 26639 { 26640 mblk_t *ire_mp = NULL; 26641 mblk_t *syn_mp; 26642 mblk_t *mdti; 26643 mblk_t *lsoi; 26644 int retval; 26645 tcph_t *tcph; 26646 uint32_t mss; 26647 queue_t *q = tcp->tcp_rq; 26648 conn_t *connp = tcp->tcp_connp; 26649 tcp_stack_t *tcps = tcp->tcp_tcps; 26650 26651 if (error == 0) { 26652 /* 26653 * Adapt Multidata information, if any. The 26654 * following tcp_mdt_update routine will free 26655 * the message. 26656 */ 26657 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26658 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26659 b_rptr)->mdt_capab, B_TRUE); 26660 freemsg(mdti); 26661 } 26662 26663 /* 26664 * Check to update LSO information with tcp, and 26665 * tcp_lso_update routine will free the message. 26666 */ 26667 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26668 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26669 b_rptr)->lso_capab); 26670 freemsg(lsoi); 26671 } 26672 26673 /* Get the IRE, if we had requested for it */ 26674 if (mp != NULL) 26675 ire_mp = tcp_ire_mp(&mp); 26676 26677 if (tcp->tcp_hard_binding) { 26678 tcp->tcp_hard_binding = B_FALSE; 26679 tcp->tcp_hard_bound = B_TRUE; 26680 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26681 if (retval != 0) { 26682 error = EADDRINUSE; 26683 goto bind_failed; 26684 } 26685 } else { 26686 if (ire_mp != NULL) 26687 freeb(ire_mp); 26688 goto after_syn_sent; 26689 } 26690 26691 retval = tcp_adapt_ire(tcp, ire_mp); 26692 if (ire_mp != NULL) 26693 freeb(ire_mp); 26694 if (retval == 0) { 26695 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26696 ENETUNREACH : EADDRNOTAVAIL); 26697 goto ipcl_rm; 26698 } 26699 /* 26700 * Don't let an endpoint connect to itself. 26701 * Also checked in tcp_connect() but that 26702 * check can't handle the case when the 26703 * local IP address is INADDR_ANY. 26704 */ 26705 if (tcp->tcp_ipversion == IPV4_VERSION) { 26706 if ((tcp->tcp_ipha->ipha_dst == 26707 tcp->tcp_ipha->ipha_src) && 26708 (BE16_EQL(tcp->tcp_tcph->th_lport, 26709 tcp->tcp_tcph->th_fport))) { 26710 error = EADDRNOTAVAIL; 26711 goto ipcl_rm; 26712 } 26713 } else { 26714 if (IN6_ARE_ADDR_EQUAL( 26715 &tcp->tcp_ip6h->ip6_dst, 26716 &tcp->tcp_ip6h->ip6_src) && 26717 (BE16_EQL(tcp->tcp_tcph->th_lport, 26718 tcp->tcp_tcph->th_fport))) { 26719 error = EADDRNOTAVAIL; 26720 goto ipcl_rm; 26721 } 26722 } 26723 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26724 /* 26725 * This should not be possible! Just for 26726 * defensive coding... 26727 */ 26728 if (tcp->tcp_state != TCPS_SYN_SENT) 26729 goto after_syn_sent; 26730 26731 if (is_system_labeled() && 26732 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26733 error = EHOSTUNREACH; 26734 goto ipcl_rm; 26735 } 26736 26737 /* 26738 * tcp_adapt_ire() does not adjust 26739 * for TCP/IP header length. 26740 */ 26741 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26742 26743 /* 26744 * Just make sure our rwnd is at 26745 * least tcp_recv_hiwat_mss * MSS 26746 * large, and round up to the nearest 26747 * MSS. 26748 * 26749 * We do the round up here because 26750 * we need to get the interface 26751 * MTU first before we can do the 26752 * round up. 26753 */ 26754 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26755 tcps->tcps_recv_hiwat_minmss * mss); 26756 if (!IPCL_IS_NONSTR(connp)) 26757 q->q_hiwat = tcp->tcp_rwnd; 26758 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26759 tcp_set_ws_value(tcp); 26760 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26761 tcp->tcp_tcph->th_win); 26762 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26763 tcp->tcp_snd_ws_ok = B_TRUE; 26764 26765 /* 26766 * Set tcp_snd_ts_ok to true 26767 * so that tcp_xmit_mp will 26768 * include the timestamp 26769 * option in the SYN segment. 26770 */ 26771 if (tcps->tcps_tstamp_always || 26772 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26773 tcp->tcp_snd_ts_ok = B_TRUE; 26774 } 26775 26776 /* 26777 * tcp_snd_sack_ok can be set in 26778 * tcp_adapt_ire() if the sack metric 26779 * is set. So check it here also. 26780 */ 26781 if (tcps->tcps_sack_permitted == 2 || 26782 tcp->tcp_snd_sack_ok) { 26783 if (tcp->tcp_sack_info == NULL) { 26784 tcp->tcp_sack_info = 26785 kmem_cache_alloc(tcp_sack_info_cache, 26786 KM_SLEEP); 26787 } 26788 tcp->tcp_snd_sack_ok = B_TRUE; 26789 } 26790 26791 /* 26792 * Should we use ECN? Note that the current 26793 * default value (SunOS 5.9) of tcp_ecn_permitted 26794 * is 1. The reason for doing this is that there 26795 * are equipments out there that will drop ECN 26796 * enabled IP packets. Setting it to 1 avoids 26797 * compatibility problems. 26798 */ 26799 if (tcps->tcps_ecn_permitted == 2) 26800 tcp->tcp_ecn_ok = B_TRUE; 26801 26802 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26803 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26804 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26805 if (syn_mp) { 26806 if (cr == NULL) { 26807 cr = tcp->tcp_cred; 26808 pid = tcp->tcp_cpid; 26809 } 26810 mblk_setcred(syn_mp, cr, pid); 26811 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26812 } 26813 after_syn_sent: 26814 if (mp != NULL) { 26815 ASSERT(mp->b_cont == NULL); 26816 freeb(mp); 26817 } 26818 return (error); 26819 } else { 26820 /* error */ 26821 if (tcp->tcp_debug) { 26822 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26823 "tcp_post_ip_bind: error == %d", error); 26824 } 26825 if (mp != NULL) { 26826 freeb(mp); 26827 } 26828 } 26829 26830 ipcl_rm: 26831 /* 26832 * Need to unbind with classifier since we were just 26833 * told that our bind succeeded. a.k.a error == 0 at the entry. 26834 */ 26835 tcp->tcp_hard_bound = B_FALSE; 26836 tcp->tcp_hard_binding = B_FALSE; 26837 26838 ipcl_hash_remove(connp); 26839 26840 bind_failed: 26841 tcp->tcp_state = TCPS_IDLE; 26842 if (tcp->tcp_ipversion == IPV4_VERSION) 26843 tcp->tcp_ipha->ipha_src = 0; 26844 else 26845 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26846 /* 26847 * Copy of the src addr. in tcp_t is needed since 26848 * the lookup funcs. can only look at tcp_t 26849 */ 26850 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26851 26852 tcph = tcp->tcp_tcph; 26853 tcph->th_lport[0] = 0; 26854 tcph->th_lport[1] = 0; 26855 tcp_bind_hash_remove(tcp); 26856 bzero(&connp->u_port, sizeof (connp->u_port)); 26857 /* blow away saved option results if any */ 26858 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26859 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26860 26861 conn_delete_ire(tcp->tcp_connp, NULL); 26862 26863 return (error); 26864 } 26865 26866 static int 26867 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26868 boolean_t bind_to_req_port_only, cred_t *cr) 26869 { 26870 in_port_t mlp_port; 26871 mlp_type_t addrtype, mlptype; 26872 boolean_t user_specified; 26873 in_port_t allocated_port; 26874 in_port_t requested_port = *requested_port_ptr; 26875 conn_t *connp; 26876 zone_t *zone; 26877 tcp_stack_t *tcps = tcp->tcp_tcps; 26878 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26879 26880 /* 26881 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26882 */ 26883 if (cr == NULL) 26884 cr = tcp->tcp_cred; 26885 /* 26886 * Get a valid port (within the anonymous range and should not 26887 * be a privileged one) to use if the user has not given a port. 26888 * If multiple threads are here, they may all start with 26889 * with the same initial port. But, it should be fine as long as 26890 * tcp_bindi will ensure that no two threads will be assigned 26891 * the same port. 26892 * 26893 * NOTE: XXX If a privileged process asks for an anonymous port, we 26894 * still check for ports only in the range > tcp_smallest_non_priv_port, 26895 * unless TCP_ANONPRIVBIND option is set. 26896 */ 26897 mlptype = mlptSingle; 26898 mlp_port = requested_port; 26899 if (requested_port == 0) { 26900 requested_port = tcp->tcp_anon_priv_bind ? 26901 tcp_get_next_priv_port(tcp) : 26902 tcp_update_next_port(tcps->tcps_next_port_to_try, 26903 tcp, B_TRUE); 26904 if (requested_port == 0) { 26905 return (-TNOADDR); 26906 } 26907 user_specified = B_FALSE; 26908 26909 /* 26910 * If the user went through one of the RPC interfaces to create 26911 * this socket and RPC is MLP in this zone, then give him an 26912 * anonymous MLP. 26913 */ 26914 connp = tcp->tcp_connp; 26915 if (connp->conn_anon_mlp && is_system_labeled()) { 26916 zone = crgetzone(cr); 26917 addrtype = tsol_mlp_addr_type(zone->zone_id, 26918 IPV6_VERSION, &v6addr, 26919 tcps->tcps_netstack->netstack_ip); 26920 if (addrtype == mlptSingle) { 26921 return (-TNOADDR); 26922 } 26923 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26924 PMAPPORT, addrtype); 26925 mlp_port = PMAPPORT; 26926 } 26927 } else { 26928 int i; 26929 boolean_t priv = B_FALSE; 26930 26931 /* 26932 * If the requested_port is in the well-known privileged range, 26933 * verify that the stream was opened by a privileged user. 26934 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26935 * but instead the code relies on: 26936 * - the fact that the address of the array and its size never 26937 * changes 26938 * - the atomic assignment of the elements of the array 26939 */ 26940 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26941 priv = B_TRUE; 26942 } else { 26943 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26944 if (requested_port == 26945 tcps->tcps_g_epriv_ports[i]) { 26946 priv = B_TRUE; 26947 break; 26948 } 26949 } 26950 } 26951 if (priv) { 26952 if (secpolicy_net_privaddr(cr, requested_port, 26953 IPPROTO_TCP) != 0) { 26954 if (tcp->tcp_debug) { 26955 (void) strlog(TCP_MOD_ID, 0, 1, 26956 SL_ERROR|SL_TRACE, 26957 "tcp_bind: no priv for port %d", 26958 requested_port); 26959 } 26960 return (-TACCES); 26961 } 26962 } 26963 user_specified = B_TRUE; 26964 26965 connp = tcp->tcp_connp; 26966 if (is_system_labeled()) { 26967 zone = crgetzone(cr); 26968 addrtype = tsol_mlp_addr_type(zone->zone_id, 26969 IPV6_VERSION, &v6addr, 26970 tcps->tcps_netstack->netstack_ip); 26971 if (addrtype == mlptSingle) { 26972 return (-TNOADDR); 26973 } 26974 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26975 requested_port, addrtype); 26976 } 26977 } 26978 26979 if (mlptype != mlptSingle) { 26980 if (secpolicy_net_bindmlp(cr) != 0) { 26981 if (tcp->tcp_debug) { 26982 (void) strlog(TCP_MOD_ID, 0, 1, 26983 SL_ERROR|SL_TRACE, 26984 "tcp_bind: no priv for multilevel port %d", 26985 requested_port); 26986 } 26987 return (-TACCES); 26988 } 26989 26990 /* 26991 * If we're specifically binding a shared IP address and the 26992 * port is MLP on shared addresses, then check to see if this 26993 * zone actually owns the MLP. Reject if not. 26994 */ 26995 if (mlptype == mlptShared && addrtype == mlptShared) { 26996 /* 26997 * No need to handle exclusive-stack zones since 26998 * ALL_ZONES only applies to the shared stack. 26999 */ 27000 zoneid_t mlpzone; 27001 27002 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 27003 htons(mlp_port)); 27004 if (connp->conn_zoneid != mlpzone) { 27005 if (tcp->tcp_debug) { 27006 (void) strlog(TCP_MOD_ID, 0, 1, 27007 SL_ERROR|SL_TRACE, 27008 "tcp_bind: attempt to bind port " 27009 "%d on shared addr in zone %d " 27010 "(should be %d)", 27011 mlp_port, connp->conn_zoneid, 27012 mlpzone); 27013 } 27014 return (-TACCES); 27015 } 27016 } 27017 27018 if (!user_specified) { 27019 int err; 27020 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27021 requested_port, B_TRUE); 27022 if (err != 0) { 27023 if (tcp->tcp_debug) { 27024 (void) strlog(TCP_MOD_ID, 0, 1, 27025 SL_ERROR|SL_TRACE, 27026 "tcp_bind: cannot establish anon " 27027 "MLP for port %d", 27028 requested_port); 27029 } 27030 return (err); 27031 } 27032 connp->conn_anon_port = B_TRUE; 27033 } 27034 connp->conn_mlp_type = mlptype; 27035 } 27036 27037 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 27038 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 27039 27040 if (allocated_port == 0) { 27041 connp->conn_mlp_type = mlptSingle; 27042 if (connp->conn_anon_port) { 27043 connp->conn_anon_port = B_FALSE; 27044 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27045 requested_port, B_FALSE); 27046 } 27047 if (bind_to_req_port_only) { 27048 if (tcp->tcp_debug) { 27049 (void) strlog(TCP_MOD_ID, 0, 1, 27050 SL_ERROR|SL_TRACE, 27051 "tcp_bind: requested addr busy"); 27052 } 27053 return (-TADDRBUSY); 27054 } else { 27055 /* If we are out of ports, fail the bind. */ 27056 if (tcp->tcp_debug) { 27057 (void) strlog(TCP_MOD_ID, 0, 1, 27058 SL_ERROR|SL_TRACE, 27059 "tcp_bind: out of ports?"); 27060 } 27061 return (-TNOADDR); 27062 } 27063 } 27064 27065 /* Pass the allocated port back */ 27066 *requested_port_ptr = allocated_port; 27067 return (0); 27068 } 27069 27070 static int 27071 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27072 boolean_t bind_to_req_port_only) 27073 { 27074 tcp_t *tcp = connp->conn_tcp; 27075 sin_t *sin; 27076 sin6_t *sin6; 27077 sin6_t sin6addr; 27078 in_port_t requested_port; 27079 ipaddr_t v4addr; 27080 in6_addr_t v6addr; 27081 uint_t origipversion; 27082 int error = 0; 27083 27084 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 27085 27086 if (tcp->tcp_state == TCPS_BOUND) { 27087 return (0); 27088 } else if (tcp->tcp_state > TCPS_BOUND) { 27089 if (tcp->tcp_debug) { 27090 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27091 "tcp_bind: bad state, %d", tcp->tcp_state); 27092 } 27093 return (-TOUTSTATE); 27094 } 27095 origipversion = tcp->tcp_ipversion; 27096 27097 if (sa != NULL && !OK_32PTR((char *)sa)) { 27098 if (tcp->tcp_debug) { 27099 (void) strlog(TCP_MOD_ID, 0, 1, 27100 SL_ERROR|SL_TRACE, 27101 "tcp_bind: bad address parameter, " 27102 "address %p, len %d", 27103 (void *)sa, len); 27104 } 27105 return (-TPROTO); 27106 } 27107 27108 switch (len) { 27109 case 0: /* request for a generic port */ 27110 if (tcp->tcp_family == AF_INET) { 27111 sin = (sin_t *)&sin6addr; 27112 *sin = sin_null; 27113 sin->sin_family = AF_INET; 27114 tcp->tcp_ipversion = IPV4_VERSION; 27115 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 27116 } else { 27117 ASSERT(tcp->tcp_family == AF_INET6); 27118 sin6 = (sin6_t *)&sin6addr; 27119 *sin6 = sin6_null; 27120 sin6->sin6_family = AF_INET6; 27121 tcp->tcp_ipversion = IPV6_VERSION; 27122 V6_SET_ZERO(v6addr); 27123 } 27124 requested_port = 0; 27125 break; 27126 27127 case sizeof (sin_t): /* Complete IPv4 address */ 27128 sin = (sin_t *)sa; 27129 /* 27130 * With sockets sockfs will accept bogus sin_family in 27131 * bind() and replace it with the family used in the socket 27132 * call. 27133 */ 27134 if (sin->sin_family != AF_INET || 27135 tcp->tcp_family != AF_INET) { 27136 return (EAFNOSUPPORT); 27137 } 27138 requested_port = ntohs(sin->sin_port); 27139 tcp->tcp_ipversion = IPV4_VERSION; 27140 v4addr = sin->sin_addr.s_addr; 27141 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 27142 break; 27143 27144 case sizeof (sin6_t): /* Complete IPv6 address */ 27145 sin6 = (sin6_t *)sa; 27146 if (sin6->sin6_family != AF_INET6 || 27147 tcp->tcp_family != AF_INET6) { 27148 return (EAFNOSUPPORT); 27149 } 27150 requested_port = ntohs(sin6->sin6_port); 27151 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 27152 IPV4_VERSION : IPV6_VERSION; 27153 v6addr = sin6->sin6_addr; 27154 break; 27155 27156 default: 27157 if (tcp->tcp_debug) { 27158 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27159 "tcp_bind: bad address length, %d", len); 27160 } 27161 return (EAFNOSUPPORT); 27162 /* return (-TBADADDR); */ 27163 } 27164 27165 tcp->tcp_bound_source_v6 = v6addr; 27166 27167 /* Check for change in ipversion */ 27168 if (origipversion != tcp->tcp_ipversion) { 27169 ASSERT(tcp->tcp_family == AF_INET6); 27170 error = tcp->tcp_ipversion == IPV6_VERSION ? 27171 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 27172 if (error) { 27173 return (ENOMEM); 27174 } 27175 } 27176 27177 /* 27178 * Initialize family specific fields. Copy of the src addr. 27179 * in tcp_t is needed for the lookup funcs. 27180 */ 27181 if (tcp->tcp_ipversion == IPV6_VERSION) { 27182 tcp->tcp_ip6h->ip6_src = v6addr; 27183 } else { 27184 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 27185 } 27186 tcp->tcp_ip_src_v6 = v6addr; 27187 27188 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 27189 27190 error = tcp_bind_select_lport(tcp, &requested_port, 27191 bind_to_req_port_only, cr); 27192 27193 return (error); 27194 } 27195 27196 /* 27197 * Return unix error is tli error is TSYSERR, otherwise return a negative 27198 * tli error. 27199 */ 27200 int 27201 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27202 boolean_t bind_to_req_port_only) 27203 { 27204 int error; 27205 tcp_t *tcp = connp->conn_tcp; 27206 27207 if (tcp->tcp_state >= TCPS_BOUND) { 27208 if (tcp->tcp_debug) { 27209 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27210 "tcp_bind: bad state, %d", tcp->tcp_state); 27211 } 27212 return (-TOUTSTATE); 27213 } 27214 27215 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 27216 if (error != 0) 27217 return (error); 27218 27219 ASSERT(tcp->tcp_state == TCPS_BOUND); 27220 27221 tcp->tcp_conn_req_max = 0; 27222 27223 /* 27224 * We need to make sure that the conn_recv is set to a non-null 27225 * value before we insert the conn into the classifier table. 27226 * This is to avoid a race with an incoming packet which does an 27227 * ipcl_classify(). 27228 */ 27229 connp->conn_recv = tcp_conn_request; 27230 27231 if (tcp->tcp_family == AF_INET6) { 27232 ASSERT(tcp->tcp_connp->conn_af_isv6); 27233 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27234 &tcp->tcp_bound_source_v6, 0, B_FALSE); 27235 } else { 27236 ASSERT(!tcp->tcp_connp->conn_af_isv6); 27237 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 27238 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 27239 } 27240 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27241 } 27242 27243 int 27244 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 27245 socklen_t len, cred_t *cr) 27246 { 27247 int error; 27248 conn_t *connp = (conn_t *)proto_handle; 27249 squeue_t *sqp = connp->conn_sqp; 27250 27251 /* All Solaris components should pass a cred for this operation. */ 27252 ASSERT(cr != NULL); 27253 27254 ASSERT(sqp != NULL); 27255 ASSERT(connp->conn_upper_handle != NULL); 27256 27257 error = squeue_synch_enter(sqp, connp, 0); 27258 if (error != 0) { 27259 /* failed to enter */ 27260 return (ENOSR); 27261 } 27262 27263 /* binding to a NULL address really means unbind */ 27264 if (sa == NULL) { 27265 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 27266 error = tcp_do_unbind(connp); 27267 else 27268 error = EINVAL; 27269 } else { 27270 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 27271 } 27272 27273 squeue_synch_exit(sqp, connp); 27274 27275 if (error < 0) { 27276 if (error == -TOUTSTATE) 27277 error = EINVAL; 27278 else 27279 error = proto_tlitosyserr(-error); 27280 } 27281 27282 return (error); 27283 } 27284 27285 /* 27286 * If the return value from this function is positive, it's a UNIX error. 27287 * Otherwise, if it's negative, then the absolute value is a TLI error. 27288 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 27289 */ 27290 int 27291 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 27292 cred_t *cr, pid_t pid) 27293 { 27294 tcp_t *tcp = connp->conn_tcp; 27295 sin_t *sin = (sin_t *)sa; 27296 sin6_t *sin6 = (sin6_t *)sa; 27297 ipaddr_t *dstaddrp; 27298 in_port_t dstport; 27299 uint_t srcid; 27300 int error = 0; 27301 27302 switch (len) { 27303 default: 27304 /* 27305 * Should never happen 27306 */ 27307 return (EINVAL); 27308 27309 case sizeof (sin_t): 27310 sin = (sin_t *)sa; 27311 if (sin->sin_port == 0) { 27312 return (-TBADADDR); 27313 } 27314 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 27315 return (EAFNOSUPPORT); 27316 } 27317 break; 27318 27319 case sizeof (sin6_t): 27320 sin6 = (sin6_t *)sa; 27321 if (sin6->sin6_port == 0) { 27322 return (-TBADADDR); 27323 } 27324 break; 27325 } 27326 /* 27327 * If we're connecting to an IPv4-mapped IPv6 address, we need to 27328 * make sure that the template IP header in the tcp structure is an 27329 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 27330 * need to this before we call tcp_bindi() so that the port lookup 27331 * code will look for ports in the correct port space (IPv4 and 27332 * IPv6 have separate port spaces). 27333 */ 27334 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 27335 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27336 int err = 0; 27337 27338 err = tcp_header_init_ipv4(tcp); 27339 if (err != 0) { 27340 error = ENOMEM; 27341 goto connect_failed; 27342 } 27343 if (tcp->tcp_lport != 0) 27344 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 27345 } 27346 27347 switch (tcp->tcp_state) { 27348 case TCPS_LISTEN: 27349 /* 27350 * Listening sockets are not allowed to issue connect(). 27351 */ 27352 if (IPCL_IS_NONSTR(connp)) 27353 return (EOPNOTSUPP); 27354 /* FALLTHRU */ 27355 case TCPS_IDLE: 27356 /* 27357 * We support quick connect, refer to comments in 27358 * tcp_connect_*() 27359 */ 27360 /* FALLTHRU */ 27361 case TCPS_BOUND: 27362 /* 27363 * We must bump the generation before the operation start. 27364 * This is done to ensure that any upcall made later on sends 27365 * up the right generation to the socket. 27366 */ 27367 SOCK_CONNID_BUMP(tcp->tcp_connid); 27368 27369 if (tcp->tcp_family == AF_INET6) { 27370 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27371 return (tcp_connect_ipv6(tcp, 27372 &sin6->sin6_addr, 27373 sin6->sin6_port, sin6->sin6_flowinfo, 27374 sin6->__sin6_src_id, sin6->sin6_scope_id, 27375 cr, pid)); 27376 } 27377 /* 27378 * Destination adress is mapped IPv6 address. 27379 * Source bound address should be unspecified or 27380 * IPv6 mapped address as well. 27381 */ 27382 if (!IN6_IS_ADDR_UNSPECIFIED( 27383 &tcp->tcp_bound_source_v6) && 27384 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 27385 return (EADDRNOTAVAIL); 27386 } 27387 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 27388 dstport = sin6->sin6_port; 27389 srcid = sin6->__sin6_src_id; 27390 } else { 27391 dstaddrp = &sin->sin_addr.s_addr; 27392 dstport = sin->sin_port; 27393 srcid = 0; 27394 } 27395 27396 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 27397 pid); 27398 break; 27399 default: 27400 return (-TOUTSTATE); 27401 } 27402 /* 27403 * Note: Code below is the "failure" case 27404 */ 27405 connect_failed: 27406 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 27407 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 27408 return (error); 27409 } 27410 27411 int 27412 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 27413 socklen_t len, sock_connid_t *id, cred_t *cr) 27414 { 27415 conn_t *connp = (conn_t *)proto_handle; 27416 tcp_t *tcp = connp->conn_tcp; 27417 squeue_t *sqp = connp->conn_sqp; 27418 int error; 27419 27420 ASSERT(connp->conn_upper_handle != NULL); 27421 27422 /* All Solaris components should pass a cred for this operation. */ 27423 ASSERT(cr != NULL); 27424 27425 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 27426 if (error != 0) { 27427 return (error); 27428 } 27429 27430 error = squeue_synch_enter(sqp, connp, 0); 27431 if (error != 0) { 27432 /* failed to enter */ 27433 return (ENOSR); 27434 } 27435 27436 /* 27437 * TCP supports quick connect, so no need to do an implicit bind 27438 */ 27439 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 27440 if (error == 0) { 27441 *id = connp->conn_tcp->tcp_connid; 27442 } else if (error < 0) { 27443 if (error == -TOUTSTATE) { 27444 switch (connp->conn_tcp->tcp_state) { 27445 case TCPS_SYN_SENT: 27446 error = EALREADY; 27447 break; 27448 case TCPS_ESTABLISHED: 27449 error = EISCONN; 27450 break; 27451 case TCPS_LISTEN: 27452 error = EOPNOTSUPP; 27453 break; 27454 default: 27455 error = EINVAL; 27456 break; 27457 } 27458 } else { 27459 error = proto_tlitosyserr(-error); 27460 } 27461 } 27462 done: 27463 squeue_synch_exit(sqp, connp); 27464 27465 return ((error == 0) ? EINPROGRESS : error); 27466 } 27467 27468 /* ARGSUSED */ 27469 sock_lower_handle_t 27470 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 27471 uint_t *smodep, int *errorp, int flags, cred_t *credp) 27472 { 27473 conn_t *connp; 27474 boolean_t isv6 = family == AF_INET6; 27475 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 27476 (proto != 0 && proto != IPPROTO_TCP)) { 27477 *errorp = EPROTONOSUPPORT; 27478 return (NULL); 27479 } 27480 27481 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 27482 if (connp == NULL) { 27483 return (NULL); 27484 } 27485 27486 /* 27487 * Put the ref for TCP. Ref for IP was already put 27488 * by ipcl_conn_create. Also Make the conn_t globally 27489 * visible to walkers 27490 */ 27491 mutex_enter(&connp->conn_lock); 27492 CONN_INC_REF_LOCKED(connp); 27493 ASSERT(connp->conn_ref == 2); 27494 connp->conn_state_flags &= ~CONN_INCIPIENT; 27495 27496 connp->conn_flags |= IPCL_NONSTR; 27497 mutex_exit(&connp->conn_lock); 27498 27499 ASSERT(errorp != NULL); 27500 *errorp = 0; 27501 *sock_downcalls = &sock_tcp_downcalls; 27502 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 27503 SM_SENDFILESUPP; 27504 27505 return ((sock_lower_handle_t)connp); 27506 } 27507 27508 /* ARGSUSED */ 27509 void 27510 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 27511 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 27512 { 27513 conn_t *connp = (conn_t *)proto_handle; 27514 struct sock_proto_props sopp; 27515 27516 ASSERT(connp->conn_upper_handle == NULL); 27517 27518 /* All Solaris components should pass a cred for this operation. */ 27519 ASSERT(cr != NULL); 27520 27521 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 27522 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 27523 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 27524 27525 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 27526 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 27527 sopp.sopp_maxpsz = INFPSZ; 27528 sopp.sopp_maxblk = INFPSZ; 27529 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 27530 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 27531 sopp.sopp_maxaddrlen = sizeof (sin6_t); 27532 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 27533 tcp_rinfo.mi_minpsz; 27534 27535 connp->conn_upcalls = sock_upcalls; 27536 connp->conn_upper_handle = sock_handle; 27537 27538 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 27539 } 27540 27541 /* ARGSUSED */ 27542 int 27543 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 27544 { 27545 conn_t *connp = (conn_t *)proto_handle; 27546 27547 ASSERT(connp->conn_upper_handle != NULL); 27548 27549 /* All Solaris components should pass a cred for this operation. */ 27550 ASSERT(cr != NULL); 27551 27552 tcp_close_common(connp, flags); 27553 27554 ip_free_helper_stream(connp); 27555 27556 /* 27557 * Drop IP's reference on the conn. This is the last reference 27558 * on the connp if the state was less than established. If the 27559 * connection has gone into timewait state, then we will have 27560 * one ref for the TCP and one more ref (total of two) for the 27561 * classifier connected hash list (a timewait connections stays 27562 * in connected hash till closed). 27563 * 27564 * We can't assert the references because there might be other 27565 * transient reference places because of some walkers or queued 27566 * packets in squeue for the timewait state. 27567 */ 27568 CONN_DEC_REF(connp); 27569 return (0); 27570 } 27571 27572 /* ARGSUSED */ 27573 int 27574 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 27575 cred_t *cr) 27576 { 27577 tcp_t *tcp; 27578 uint32_t msize; 27579 conn_t *connp = (conn_t *)proto_handle; 27580 int32_t tcpstate; 27581 27582 /* All Solaris components should pass a cred for this operation. */ 27583 ASSERT(cr != NULL); 27584 27585 ASSERT(connp->conn_ref >= 2); 27586 ASSERT(connp->conn_upper_handle != NULL); 27587 27588 if (msg->msg_controllen != 0) { 27589 return (EOPNOTSUPP); 27590 27591 } 27592 switch (DB_TYPE(mp)) { 27593 case M_DATA: 27594 tcp = connp->conn_tcp; 27595 ASSERT(tcp != NULL); 27596 27597 tcpstate = tcp->tcp_state; 27598 if (tcpstate < TCPS_ESTABLISHED) { 27599 freemsg(mp); 27600 return (ENOTCONN); 27601 } else if (tcpstate > TCPS_CLOSE_WAIT) { 27602 freemsg(mp); 27603 return (EPIPE); 27604 } 27605 27606 msize = msgdsize(mp); 27607 27608 mutex_enter(&tcp->tcp_non_sq_lock); 27609 tcp->tcp_squeue_bytes += msize; 27610 /* 27611 * Squeue Flow Control 27612 */ 27613 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 27614 tcp_setqfull(tcp); 27615 } 27616 mutex_exit(&tcp->tcp_non_sq_lock); 27617 27618 /* 27619 * The application may pass in an address in the msghdr, but 27620 * we ignore the address on connection-oriented sockets. 27621 * Just like BSD this code does not generate an error for 27622 * TCP (a CONNREQUIRED socket) when sending to an address 27623 * passed in with sendto/sendmsg. Instead the data is 27624 * delivered on the connection as if no address had been 27625 * supplied. 27626 */ 27627 CONN_INC_REF(connp); 27628 27629 if (msg != NULL && msg->msg_flags & MSG_OOB) { 27630 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 27631 tcp_output_urgent, connp, tcp_squeue_flag, 27632 SQTAG_TCP_OUTPUT); 27633 } else { 27634 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 27635 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 27636 } 27637 27638 return (0); 27639 27640 default: 27641 ASSERT(0); 27642 } 27643 27644 freemsg(mp); 27645 return (0); 27646 } 27647 27648 /* ARGSUSED */ 27649 void 27650 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 27651 { 27652 int len; 27653 uint32_t msize; 27654 conn_t *connp = (conn_t *)arg; 27655 tcp_t *tcp = connp->conn_tcp; 27656 27657 msize = msgdsize(mp); 27658 27659 len = msize - 1; 27660 if (len < 0) { 27661 freemsg(mp); 27662 return; 27663 } 27664 27665 /* 27666 * Try to force urgent data out on the wire. 27667 * Even if we have unsent data this will 27668 * at least send the urgent flag. 27669 * XXX does not handle more flag correctly. 27670 */ 27671 len += tcp->tcp_unsent; 27672 len += tcp->tcp_snxt; 27673 tcp->tcp_urg = len; 27674 tcp->tcp_valid_bits |= TCP_URG_VALID; 27675 27676 /* Bypass tcp protocol for fused tcp loopback */ 27677 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27678 return; 27679 tcp_wput_data(tcp, mp, B_TRUE); 27680 } 27681 27682 /* ARGSUSED */ 27683 int 27684 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27685 socklen_t *addrlenp, cred_t *cr) 27686 { 27687 conn_t *connp = (conn_t *)proto_handle; 27688 tcp_t *tcp = connp->conn_tcp; 27689 27690 ASSERT(connp->conn_upper_handle != NULL); 27691 /* All Solaris components should pass a cred for this operation. */ 27692 ASSERT(cr != NULL); 27693 27694 ASSERT(tcp != NULL); 27695 27696 return (tcp_do_getpeername(tcp, addr, addrlenp)); 27697 } 27698 27699 /* ARGSUSED */ 27700 int 27701 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27702 socklen_t *addrlenp, cred_t *cr) 27703 { 27704 conn_t *connp = (conn_t *)proto_handle; 27705 tcp_t *tcp = connp->conn_tcp; 27706 27707 /* All Solaris components should pass a cred for this operation. */ 27708 ASSERT(cr != NULL); 27709 27710 ASSERT(connp->conn_upper_handle != NULL); 27711 27712 return (tcp_do_getsockname(tcp, addr, addrlenp)); 27713 } 27714 27715 /* 27716 * tcp_fallback 27717 * 27718 * A direct socket is falling back to using STREAMS. The queue 27719 * that is being passed down was created using tcp_open() with 27720 * the SO_FALLBACK flag set. As a result, the queue is not 27721 * associated with a conn, and the q_ptrs instead contain the 27722 * dev and minor area that should be used. 27723 * 27724 * The 'direct_sockfs' flag indicates whether the FireEngine 27725 * optimizations should be used. The common case would be that 27726 * optimizations are enabled, and they might be subsequently 27727 * disabled using the _SIOCSOCKFALLBACK ioctl. 27728 */ 27729 27730 /* 27731 * An active connection is falling back to TPI. Gather all the information 27732 * required by the STREAM head and TPI sonode and send it up. 27733 */ 27734 void 27735 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 27736 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27737 { 27738 conn_t *connp = tcp->tcp_connp; 27739 struct stroptions *stropt; 27740 struct T_capability_ack tca; 27741 struct sockaddr_in6 laddr, faddr; 27742 socklen_t laddrlen, faddrlen; 27743 short opts; 27744 int error; 27745 mblk_t *mp; 27746 27747 /* Disable I/OAT during fallback */ 27748 tcp->tcp_sodirect = NULL; 27749 27750 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27751 connp->conn_minor_arena = WR(q)->q_ptr; 27752 27753 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27754 27755 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27756 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27757 27758 WR(q)->q_qinfo = &tcp_sock_winit; 27759 27760 if (!direct_sockfs) 27761 tcp_disable_direct_sockfs(tcp); 27762 27763 /* 27764 * free the helper stream 27765 */ 27766 ip_free_helper_stream(connp); 27767 27768 /* 27769 * Notify the STREAM head about options 27770 */ 27771 DB_TYPE(stropt_mp) = M_SETOPTS; 27772 stropt = (struct stroptions *)stropt_mp->b_rptr; 27773 stropt_mp->b_wptr += sizeof (struct stroptions); 27774 stropt = (struct stroptions *)stropt_mp->b_rptr; 27775 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27776 27777 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27778 tcp->tcp_tcps->tcps_wroff_xtra); 27779 if (tcp->tcp_snd_sack_ok) 27780 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27781 stropt->so_hiwat = tcp->tcp_fused ? 27782 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27783 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27784 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27785 27786 putnext(RD(q), stropt_mp); 27787 27788 /* 27789 * Collect the information needed to sync with the sonode 27790 */ 27791 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27792 27793 laddrlen = faddrlen = sizeof (sin6_t); 27794 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 27795 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 27796 if (error != 0) 27797 faddrlen = 0; 27798 27799 opts = 0; 27800 if (tcp->tcp_oobinline) 27801 opts |= SO_OOBINLINE; 27802 if (tcp->tcp_dontroute) 27803 opts |= SO_DONTROUTE; 27804 27805 /* 27806 * Notify the socket that the protocol is now quiescent, 27807 * and it's therefore safe move data from the socket 27808 * to the stream head. 27809 */ 27810 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27811 (struct sockaddr *)&laddr, laddrlen, 27812 (struct sockaddr *)&faddr, faddrlen, opts); 27813 27814 while ((mp = tcp->tcp_rcv_list) != NULL) { 27815 tcp->tcp_rcv_list = mp->b_next; 27816 mp->b_next = NULL; 27817 putnext(q, mp); 27818 } 27819 tcp->tcp_rcv_last_head = NULL; 27820 tcp->tcp_rcv_last_tail = NULL; 27821 tcp->tcp_rcv_cnt = 0; 27822 } 27823 27824 /* 27825 * An eager is falling back to TPI. All we have to do is send 27826 * up a T_CONN_IND. 27827 */ 27828 void 27829 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 27830 { 27831 tcp_t *listener = eager->tcp_listener; 27832 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 27833 27834 ASSERT(listener != NULL); 27835 ASSERT(mp != NULL); 27836 27837 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27838 27839 /* 27840 * TLI/XTI applications will get confused by 27841 * sending eager as an option since it violates 27842 * the option semantics. So remove the eager as 27843 * option since TLI/XTI app doesn't need it anyway. 27844 */ 27845 if (!direct_sockfs) { 27846 struct T_conn_ind *conn_ind; 27847 27848 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27849 conn_ind->OPT_length = 0; 27850 conn_ind->OPT_offset = 0; 27851 } 27852 27853 /* 27854 * Sockfs guarantees that the listener will not be closed 27855 * during fallback. So we can safely use the listener's queue. 27856 */ 27857 putnext(listener->tcp_rq, mp); 27858 } 27859 27860 int 27861 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27862 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27863 { 27864 tcp_t *tcp; 27865 conn_t *connp = (conn_t *)proto_handle; 27866 int error; 27867 mblk_t *stropt_mp; 27868 mblk_t *ordrel_mp; 27869 mblk_t *fused_sigurp_mp; 27870 27871 tcp = connp->conn_tcp; 27872 27873 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 27874 NULL); 27875 27876 /* Pre-allocate the T_ordrel_ind mblk. */ 27877 ASSERT(tcp->tcp_ordrel_mp == NULL); 27878 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27879 STR_NOSIG, NULL); 27880 ordrel_mp->b_datap->db_type = M_PROTO; 27881 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27882 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27883 27884 /* Pre-allocate the M_PCSIG used by fusion */ 27885 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27886 27887 /* 27888 * Enter the squeue so that no new packets can come in 27889 */ 27890 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27891 if (error != 0) { 27892 /* failed to enter, free all the pre-allocated messages. */ 27893 freeb(stropt_mp); 27894 freeb(ordrel_mp); 27895 freeb(fused_sigurp_mp); 27896 /* 27897 * We cannot process the eager, so at least send out a 27898 * RST so the peer can reconnect. 27899 */ 27900 if (tcp->tcp_listener != NULL) { 27901 (void) tcp_eager_blowoff(tcp->tcp_listener, 27902 tcp->tcp_conn_req_seqnum); 27903 } 27904 return (ENOMEM); 27905 } 27906 27907 /* 27908 * No longer a direct socket 27909 */ 27910 connp->conn_flags &= ~IPCL_NONSTR; 27911 27912 tcp->tcp_ordrel_mp = ordrel_mp; 27913 27914 if (tcp->tcp_fused) { 27915 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27916 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27917 } else { 27918 freeb(fused_sigurp_mp); 27919 } 27920 27921 if (tcp->tcp_listener != NULL) { 27922 /* The eager will deal with opts when accept() is called */ 27923 freeb(stropt_mp); 27924 tcp_fallback_eager(tcp, direct_sockfs); 27925 } else { 27926 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 27927 quiesced_cb); 27928 } 27929 27930 /* 27931 * There should be atleast two ref's (IP + TCP) 27932 */ 27933 ASSERT(connp->conn_ref >= 2); 27934 squeue_synch_exit(connp->conn_sqp, connp); 27935 27936 return (0); 27937 } 27938 27939 /* ARGSUSED */ 27940 static void 27941 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27942 { 27943 conn_t *connp = (conn_t *)arg; 27944 tcp_t *tcp = connp->conn_tcp; 27945 27946 freemsg(mp); 27947 27948 if (tcp->tcp_fused) 27949 tcp_unfuse(tcp); 27950 27951 if (tcp_xmit_end(tcp) != 0) { 27952 /* 27953 * We were crossing FINs and got a reset from 27954 * the other side. Just ignore it. 27955 */ 27956 if (tcp->tcp_debug) { 27957 (void) strlog(TCP_MOD_ID, 0, 1, 27958 SL_ERROR|SL_TRACE, 27959 "tcp_shutdown_output() out of state %s", 27960 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27961 } 27962 } 27963 } 27964 27965 /* ARGSUSED */ 27966 int 27967 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27968 { 27969 conn_t *connp = (conn_t *)proto_handle; 27970 tcp_t *tcp = connp->conn_tcp; 27971 27972 ASSERT(connp->conn_upper_handle != NULL); 27973 27974 /* All Solaris components should pass a cred for this operation. */ 27975 ASSERT(cr != NULL); 27976 27977 /* 27978 * X/Open requires that we check the connected state. 27979 */ 27980 if (tcp->tcp_state < TCPS_SYN_SENT) 27981 return (ENOTCONN); 27982 27983 /* shutdown the send side */ 27984 if (how != SHUT_RD) { 27985 mblk_t *bp; 27986 27987 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27988 CONN_INC_REF(connp); 27989 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27990 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27991 27992 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27993 SOCK_OPCTL_SHUT_SEND, 0); 27994 } 27995 27996 /* shutdown the recv side */ 27997 if (how != SHUT_WR) 27998 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27999 SOCK_OPCTL_SHUT_RECV, 0); 28000 28001 return (0); 28002 } 28003 28004 /* 28005 * SOP_LISTEN() calls into tcp_listen(). 28006 */ 28007 /* ARGSUSED */ 28008 int 28009 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 28010 { 28011 conn_t *connp = (conn_t *)proto_handle; 28012 int error; 28013 squeue_t *sqp = connp->conn_sqp; 28014 28015 ASSERT(connp->conn_upper_handle != NULL); 28016 28017 /* All Solaris components should pass a cred for this operation. */ 28018 ASSERT(cr != NULL); 28019 28020 error = squeue_synch_enter(sqp, connp, 0); 28021 if (error != 0) { 28022 /* failed to enter */ 28023 return (ENOBUFS); 28024 } 28025 28026 error = tcp_do_listen(connp, backlog, cr); 28027 if (error == 0) { 28028 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 28029 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 28030 } else if (error < 0) { 28031 if (error == -TOUTSTATE) 28032 error = EINVAL; 28033 else 28034 error = proto_tlitosyserr(-error); 28035 } 28036 squeue_synch_exit(sqp, connp); 28037 return (error); 28038 } 28039 28040 static int 28041 tcp_do_listen(conn_t *connp, int backlog, cred_t *cr) 28042 { 28043 tcp_t *tcp = connp->conn_tcp; 28044 sin_t *sin; 28045 sin6_t *sin6; 28046 int error = 0; 28047 tcp_stack_t *tcps = tcp->tcp_tcps; 28048 28049 /* All Solaris components should pass a cred for this operation. */ 28050 ASSERT(cr != NULL); 28051 28052 if (tcp->tcp_state >= TCPS_BOUND) { 28053 if ((tcp->tcp_state == TCPS_BOUND || 28054 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 28055 /* 28056 * Handle listen() increasing backlog. 28057 * This is more "liberal" then what the TPI spec 28058 * requires but is needed to avoid a t_unbind 28059 * when handling listen() since the port number 28060 * might be "stolen" between the unbind and bind. 28061 */ 28062 goto do_listen; 28063 } 28064 if (tcp->tcp_debug) { 28065 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 28066 "tcp_listen: bad state, %d", tcp->tcp_state); 28067 } 28068 return (-TOUTSTATE); 28069 } else { 28070 int32_t len; 28071 sin6_t addr; 28072 28073 /* Do an implicit bind: Request for a generic port. */ 28074 if (tcp->tcp_family == AF_INET) { 28075 len = sizeof (sin_t); 28076 sin = (sin_t *)&addr; 28077 *sin = sin_null; 28078 sin->sin_family = AF_INET; 28079 tcp->tcp_ipversion = IPV4_VERSION; 28080 } else { 28081 ASSERT(tcp->tcp_family == AF_INET6); 28082 len = sizeof (sin6_t); 28083 sin6 = (sin6_t *)&addr; 28084 *sin6 = sin6_null; 28085 sin6->sin6_family = AF_INET6; 28086 tcp->tcp_ipversion = IPV6_VERSION; 28087 } 28088 28089 error = tcp_bind_check(connp, (struct sockaddr *)&addr, len, 28090 cr, B_FALSE); 28091 if (error) 28092 return (error); 28093 /* Fall through and do the fanout insertion */ 28094 } 28095 28096 do_listen: 28097 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 28098 tcp->tcp_conn_req_max = backlog; 28099 if (tcp->tcp_conn_req_max) { 28100 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 28101 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 28102 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 28103 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 28104 /* 28105 * If this is a listener, do not reset the eager list 28106 * and other stuffs. Note that we don't check if the 28107 * existing eager list meets the new tcp_conn_req_max 28108 * requirement. 28109 */ 28110 if (tcp->tcp_state != TCPS_LISTEN) { 28111 tcp->tcp_state = TCPS_LISTEN; 28112 /* Initialize the chain. Don't need the eager_lock */ 28113 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 28114 tcp->tcp_eager_next_drop_q0 = tcp; 28115 tcp->tcp_eager_prev_drop_q0 = tcp; 28116 tcp->tcp_second_ctimer_threshold = 28117 tcps->tcps_ip_abort_linterval; 28118 } 28119 } 28120 28121 /* 28122 * We can call ip_bind directly, the processing continues 28123 * in tcp_post_ip_bind(). 28124 * 28125 * We need to make sure that the conn_recv is set to a non-null 28126 * value before we insert the conn into the classifier table. 28127 * This is to avoid a race with an incoming packet which does an 28128 * ipcl_classify(). 28129 */ 28130 connp->conn_recv = tcp_conn_request; 28131 if (tcp->tcp_family == AF_INET) { 28132 error = ip_proto_bind_laddr_v4(connp, NULL, 28133 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 28134 } else { 28135 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 28136 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 28137 } 28138 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 28139 } 28140 28141 void 28142 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 28143 { 28144 conn_t *connp = (conn_t *)proto_handle; 28145 tcp_t *tcp = connp->conn_tcp; 28146 tcp_stack_t *tcps = tcp->tcp_tcps; 28147 uint_t thwin; 28148 28149 ASSERT(connp->conn_upper_handle != NULL); 28150 28151 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 28152 28153 /* Flow control condition has been removed. */ 28154 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 28155 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 28156 << tcp->tcp_rcv_ws; 28157 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 28158 /* 28159 * Send back a window update immediately if TCP is above 28160 * ESTABLISHED state and the increase of the rcv window 28161 * that the other side knows is at least 1 MSS after flow 28162 * control is lifted. 28163 */ 28164 if (tcp->tcp_state >= TCPS_ESTABLISHED && 28165 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 28166 tcp_xmit_ctl(NULL, tcp, 28167 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 28168 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 28169 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 28170 } 28171 28172 squeue_synch_exit(connp->conn_sqp, connp); 28173 } 28174 28175 /* ARGSUSED */ 28176 int 28177 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 28178 int mode, int32_t *rvalp, cred_t *cr) 28179 { 28180 conn_t *connp = (conn_t *)proto_handle; 28181 int error; 28182 28183 ASSERT(connp->conn_upper_handle != NULL); 28184 28185 /* All Solaris components should pass a cred for this operation. */ 28186 ASSERT(cr != NULL); 28187 28188 switch (cmd) { 28189 case ND_SET: 28190 case ND_GET: 28191 case TCP_IOC_DEFAULT_Q: 28192 case _SIOCSOCKFALLBACK: 28193 case TCP_IOC_ABORT_CONN: 28194 case TI_GETPEERNAME: 28195 case TI_GETMYNAME: 28196 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 28197 cmd)); 28198 error = EINVAL; 28199 break; 28200 default: 28201 /* 28202 * Pass on to IP using helper stream 28203 */ 28204 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 28205 cmd, arg, mode, cr, rvalp); 28206 break; 28207 } 28208 return (error); 28209 } 28210 28211 sock_downcalls_t sock_tcp_downcalls = { 28212 tcp_activate, 28213 tcp_accept, 28214 tcp_bind, 28215 tcp_listen, 28216 tcp_connect, 28217 tcp_getpeername, 28218 tcp_getsockname, 28219 tcp_getsockopt, 28220 tcp_setsockopt, 28221 tcp_sendmsg, 28222 NULL, 28223 NULL, 28224 NULL, 28225 tcp_shutdown, 28226 tcp_clr_flowctrl, 28227 tcp_ioctl, 28228 tcp_close, 28229 }; 28230