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 mp = NULL; 14063 goto update_ack; 14064 } else if (!tcp->tcp_urp_mp) { 14065 struct T_exdata_ind *tei; 14066 mp1 = allocb(sizeof (struct T_exdata_ind), 14067 BPRI_MED); 14068 if (!mp1) { 14069 /* 14070 * Sigh... It'll be back. 14071 * Generate any MSG*MARK message now. 14072 */ 14073 freemsg(mp); 14074 seg_len = 0; 14075 if (flags & TH_SEND_URP_MARK) { 14076 14077 14078 ASSERT(tcp->tcp_urp_mark_mp); 14079 tcp->tcp_urp_mark_mp->b_flag &= 14080 ~MSGNOTMARKNEXT; 14081 tcp->tcp_urp_mark_mp->b_flag |= 14082 MSGMARKNEXT; 14083 } 14084 goto ack_check; 14085 } 14086 mp1->b_datap->db_type = M_PROTO; 14087 tei = (struct T_exdata_ind *)mp1->b_rptr; 14088 tei->PRIM_type = T_EXDATA_IND; 14089 tei->MORE_flag = 0; 14090 mp1->b_wptr = (uchar_t *)&tei[1]; 14091 tcp->tcp_urp_mp = mp1; 14092 #ifdef DEBUG 14093 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14094 "tcp_rput: allocated exdata_ind %s", 14095 tcp_display(tcp, NULL, 14096 DISP_PORT_ONLY)); 14097 #endif /* DEBUG */ 14098 /* 14099 * There is no need to send a separate MSG*MARK 14100 * message since the T_EXDATA_IND will be sent 14101 * now. 14102 */ 14103 flags &= ~TH_SEND_URP_MARK; 14104 freemsg(tcp->tcp_urp_mark_mp); 14105 tcp->tcp_urp_mark_mp = NULL; 14106 } 14107 /* 14108 * Now we are all set. On the next putnext upstream, 14109 * tcp_urp_mp will be non-NULL and will get prepended 14110 * to what has to be this piece containing the urgent 14111 * byte. If for any reason we abort this segment below, 14112 * if it comes back, we will have this ready, or it 14113 * will get blown off in close. 14114 */ 14115 } else if (urp == seg_len) { 14116 /* 14117 * The urgent byte is the next byte after this sequence 14118 * number. If there is data it is marked with 14119 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14120 * since it is not needed. Otherwise, if the code 14121 * above just allocated a zero-length tcp_urp_mark_mp 14122 * message, that message is tagged with MSGMARKNEXT. 14123 * Sending up these MSGMARKNEXT messages makes 14124 * SIOCATMARK work correctly even though 14125 * the T_EXDATA_IND will not be sent up until the 14126 * urgent byte arrives. 14127 */ 14128 if (seg_len != 0) { 14129 flags |= TH_MARKNEXT_NEEDED; 14130 freemsg(tcp->tcp_urp_mark_mp); 14131 tcp->tcp_urp_mark_mp = NULL; 14132 flags &= ~TH_SEND_URP_MARK; 14133 } else if (tcp->tcp_urp_mark_mp != NULL) { 14134 flags |= TH_SEND_URP_MARK; 14135 tcp->tcp_urp_mark_mp->b_flag &= 14136 ~MSGNOTMARKNEXT; 14137 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14138 } 14139 #ifdef DEBUG 14140 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14141 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14142 seg_len, flags, 14143 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14144 #endif /* DEBUG */ 14145 } 14146 #ifdef DEBUG 14147 else { 14148 /* Data left until we hit mark */ 14149 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14150 "tcp_rput: URP %d bytes left, %s", 14151 urp - seg_len, tcp_display(tcp, NULL, 14152 DISP_PORT_ONLY)); 14153 } 14154 #endif /* DEBUG */ 14155 } 14156 14157 process_ack: 14158 if (!(flags & TH_ACK)) { 14159 freemsg(mp); 14160 goto xmit_check; 14161 } 14162 } 14163 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14164 14165 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14166 tcp->tcp_ip_forward_progress = B_TRUE; 14167 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14168 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14169 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14170 /* 3-way handshake complete - pass up the T_CONN_IND */ 14171 tcp_t *listener = tcp->tcp_listener; 14172 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14173 14174 tcp->tcp_tconnind_started = B_TRUE; 14175 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14176 /* 14177 * We are here means eager is fine but it can 14178 * get a TH_RST at any point between now and till 14179 * accept completes and disappear. We need to 14180 * ensure that reference to eager is valid after 14181 * we get out of eager's perimeter. So we do 14182 * an extra refhold. 14183 */ 14184 CONN_INC_REF(connp); 14185 14186 /* 14187 * The listener also exists because of the refhold 14188 * done in tcp_conn_request. Its possible that it 14189 * might have closed. We will check that once we 14190 * get inside listeners context. 14191 */ 14192 CONN_INC_REF(listener->tcp_connp); 14193 if (listener->tcp_connp->conn_sqp == 14194 connp->conn_sqp) { 14195 /* 14196 * We optimize by not calling an SQUEUE_ENTER 14197 * on the listener since we know that the 14198 * listener and eager squeues are the same. 14199 * We are able to make this check safely only 14200 * because neither the eager nor the listener 14201 * can change its squeue. Only an active connect 14202 * can change its squeue 14203 */ 14204 tcp_send_conn_ind(listener->tcp_connp, mp, 14205 listener->tcp_connp->conn_sqp); 14206 CONN_DEC_REF(listener->tcp_connp); 14207 } else if (!tcp->tcp_loopback) { 14208 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14209 mp, tcp_send_conn_ind, 14210 listener->tcp_connp, SQ_FILL, 14211 SQTAG_TCP_CONN_IND); 14212 } else { 14213 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14214 mp, tcp_send_conn_ind, 14215 listener->tcp_connp, SQ_PROCESS, 14216 SQTAG_TCP_CONN_IND); 14217 } 14218 } 14219 14220 if (tcp->tcp_active_open) { 14221 /* 14222 * We are seeing the final ack in the three way 14223 * hand shake of a active open'ed connection 14224 * so we must send up a T_CONN_CON 14225 */ 14226 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14227 freemsg(mp); 14228 return; 14229 } 14230 /* 14231 * Don't fuse the loopback endpoints for 14232 * simultaneous active opens. 14233 */ 14234 if (tcp->tcp_loopback) { 14235 TCP_STAT(tcps, tcp_fusion_unfusable); 14236 tcp->tcp_unfusable = B_TRUE; 14237 } 14238 } 14239 14240 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14241 bytes_acked--; 14242 /* SYN was acked - making progress */ 14243 if (tcp->tcp_ipversion == IPV6_VERSION) 14244 tcp->tcp_ip_forward_progress = B_TRUE; 14245 14246 /* 14247 * If SYN was retransmitted, need to reset all 14248 * retransmission info as this segment will be 14249 * treated as a dup ACK. 14250 */ 14251 if (tcp->tcp_rexmit) { 14252 tcp->tcp_rexmit = B_FALSE; 14253 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14254 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14255 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14256 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14257 tcp->tcp_ms_we_have_waited = 0; 14258 tcp->tcp_cwnd = mss; 14259 } 14260 14261 /* 14262 * We set the send window to zero here. 14263 * This is needed if there is data to be 14264 * processed already on the queue. 14265 * Later (at swnd_update label), the 14266 * "new_swnd > tcp_swnd" condition is satisfied 14267 * the XMIT_NEEDED flag is set in the current 14268 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14269 * called if there is already data on queue in 14270 * this state. 14271 */ 14272 tcp->tcp_swnd = 0; 14273 14274 if (new_swnd > tcp->tcp_max_swnd) 14275 tcp->tcp_max_swnd = new_swnd; 14276 tcp->tcp_swl1 = seg_seq; 14277 tcp->tcp_swl2 = seg_ack; 14278 tcp->tcp_state = TCPS_ESTABLISHED; 14279 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14280 14281 /* Fuse when both sides are in ESTABLISHED state */ 14282 if (tcp->tcp_loopback && do_tcp_fusion) 14283 tcp_fuse(tcp, iphdr, tcph); 14284 14285 } 14286 /* This code follows 4.4BSD-Lite2 mostly. */ 14287 if (bytes_acked < 0) 14288 goto est; 14289 14290 /* 14291 * If TCP is ECN capable and the congestion experience bit is 14292 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14293 * done once per window (or more loosely, per RTT). 14294 */ 14295 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14296 tcp->tcp_cwr = B_FALSE; 14297 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14298 if (!tcp->tcp_cwr) { 14299 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14300 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14301 tcp->tcp_cwnd = npkt * mss; 14302 /* 14303 * If the cwnd is 0, use the timer to clock out 14304 * new segments. This is required by the ECN spec. 14305 */ 14306 if (npkt == 0) { 14307 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14308 /* 14309 * This makes sure that when the ACK comes 14310 * back, we will increase tcp_cwnd by 1 MSS. 14311 */ 14312 tcp->tcp_cwnd_cnt = 0; 14313 } 14314 tcp->tcp_cwr = B_TRUE; 14315 /* 14316 * This marks the end of the current window of in 14317 * flight data. That is why we don't use 14318 * tcp_suna + tcp_swnd. Only data in flight can 14319 * provide ECN info. 14320 */ 14321 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14322 tcp->tcp_ecn_cwr_sent = B_FALSE; 14323 } 14324 } 14325 14326 mp1 = tcp->tcp_xmit_head; 14327 if (bytes_acked == 0) { 14328 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14329 int dupack_cnt; 14330 14331 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14332 /* 14333 * Fast retransmit. When we have seen exactly three 14334 * identical ACKs while we have unacked data 14335 * outstanding we take it as a hint that our peer 14336 * dropped something. 14337 * 14338 * If TCP is retransmitting, don't do fast retransmit. 14339 */ 14340 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14341 ! tcp->tcp_rexmit) { 14342 /* Do Limited Transmit */ 14343 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14344 tcps->tcps_dupack_fast_retransmit) { 14345 /* 14346 * RFC 3042 14347 * 14348 * What we need to do is temporarily 14349 * increase tcp_cwnd so that new 14350 * data can be sent if it is allowed 14351 * by the receive window (tcp_rwnd). 14352 * tcp_wput_data() will take care of 14353 * the rest. 14354 * 14355 * If the connection is SACK capable, 14356 * only do limited xmit when there 14357 * is SACK info. 14358 * 14359 * Note how tcp_cwnd is incremented. 14360 * The first dup ACK will increase 14361 * it by 1 MSS. The second dup ACK 14362 * will increase it by 2 MSS. This 14363 * means that only 1 new segment will 14364 * be sent for each dup ACK. 14365 */ 14366 if (tcp->tcp_unsent > 0 && 14367 (!tcp->tcp_snd_sack_ok || 14368 (tcp->tcp_snd_sack_ok && 14369 tcp->tcp_notsack_list != NULL))) { 14370 tcp->tcp_cwnd += mss << 14371 (tcp->tcp_dupack_cnt - 1); 14372 flags |= TH_LIMIT_XMIT; 14373 } 14374 } else if (dupack_cnt == 14375 tcps->tcps_dupack_fast_retransmit) { 14376 14377 /* 14378 * If we have reduced tcp_ssthresh 14379 * because of ECN, do not reduce it again 14380 * unless it is already one window of data 14381 * away. After one window of data, tcp_cwr 14382 * should then be cleared. Note that 14383 * for non ECN capable connection, tcp_cwr 14384 * should always be false. 14385 * 14386 * Adjust cwnd since the duplicate 14387 * ack indicates that a packet was 14388 * dropped (due to congestion.) 14389 */ 14390 if (!tcp->tcp_cwr) { 14391 npkt = ((tcp->tcp_snxt - 14392 tcp->tcp_suna) >> 1) / mss; 14393 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14394 mss; 14395 tcp->tcp_cwnd = (npkt + 14396 tcp->tcp_dupack_cnt) * mss; 14397 } 14398 if (tcp->tcp_ecn_ok) { 14399 tcp->tcp_cwr = B_TRUE; 14400 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14401 tcp->tcp_ecn_cwr_sent = B_FALSE; 14402 } 14403 14404 /* 14405 * We do Hoe's algorithm. Refer to her 14406 * paper "Improving the Start-up Behavior 14407 * of a Congestion Control Scheme for TCP," 14408 * appeared in SIGCOMM'96. 14409 * 14410 * Save highest seq no we have sent so far. 14411 * Be careful about the invisible FIN byte. 14412 */ 14413 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14414 (tcp->tcp_unsent == 0)) { 14415 tcp->tcp_rexmit_max = tcp->tcp_fss; 14416 } else { 14417 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14418 } 14419 14420 /* 14421 * Do not allow bursty traffic during. 14422 * fast recovery. Refer to Fall and Floyd's 14423 * paper "Simulation-based Comparisons of 14424 * Tahoe, Reno and SACK TCP" (in CCR?) 14425 * This is a best current practise. 14426 */ 14427 tcp->tcp_snd_burst = TCP_CWND_SS; 14428 14429 /* 14430 * For SACK: 14431 * Calculate tcp_pipe, which is the 14432 * estimated number of bytes in 14433 * network. 14434 * 14435 * tcp_fack is the highest sack'ed seq num 14436 * TCP has received. 14437 * 14438 * tcp_pipe is explained in the above quoted 14439 * Fall and Floyd's paper. tcp_fack is 14440 * explained in Mathis and Mahdavi's 14441 * "Forward Acknowledgment: Refining TCP 14442 * Congestion Control" in SIGCOMM '96. 14443 */ 14444 if (tcp->tcp_snd_sack_ok) { 14445 ASSERT(tcp->tcp_sack_info != NULL); 14446 if (tcp->tcp_notsack_list != NULL) { 14447 tcp->tcp_pipe = tcp->tcp_snxt - 14448 tcp->tcp_fack; 14449 tcp->tcp_sack_snxt = seg_ack; 14450 flags |= TH_NEED_SACK_REXMIT; 14451 } else { 14452 /* 14453 * Always initialize tcp_pipe 14454 * even though we don't have 14455 * any SACK info. If later 14456 * we get SACK info and 14457 * tcp_pipe is not initialized, 14458 * funny things will happen. 14459 */ 14460 tcp->tcp_pipe = 14461 tcp->tcp_cwnd_ssthresh; 14462 } 14463 } else { 14464 flags |= TH_REXMIT_NEEDED; 14465 } /* tcp_snd_sack_ok */ 14466 14467 } else { 14468 /* 14469 * Here we perform congestion 14470 * avoidance, but NOT slow start. 14471 * This is known as the Fast 14472 * Recovery Algorithm. 14473 */ 14474 if (tcp->tcp_snd_sack_ok && 14475 tcp->tcp_notsack_list != NULL) { 14476 flags |= TH_NEED_SACK_REXMIT; 14477 tcp->tcp_pipe -= mss; 14478 if (tcp->tcp_pipe < 0) 14479 tcp->tcp_pipe = 0; 14480 } else { 14481 /* 14482 * We know that one more packet has 14483 * left the pipe thus we can update 14484 * cwnd. 14485 */ 14486 cwnd = tcp->tcp_cwnd + mss; 14487 if (cwnd > tcp->tcp_cwnd_max) 14488 cwnd = tcp->tcp_cwnd_max; 14489 tcp->tcp_cwnd = cwnd; 14490 if (tcp->tcp_unsent > 0) 14491 flags |= TH_XMIT_NEEDED; 14492 } 14493 } 14494 } 14495 } else if (tcp->tcp_zero_win_probe) { 14496 /* 14497 * If the window has opened, need to arrange 14498 * to send additional data. 14499 */ 14500 if (new_swnd != 0) { 14501 /* tcp_suna != tcp_snxt */ 14502 /* Packet contains a window update */ 14503 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14504 tcp->tcp_zero_win_probe = 0; 14505 tcp->tcp_timer_backoff = 0; 14506 tcp->tcp_ms_we_have_waited = 0; 14507 14508 /* 14509 * Transmit starting with tcp_suna since 14510 * the one byte probe is not ack'ed. 14511 * If TCP has sent more than one identical 14512 * probe, tcp_rexmit will be set. That means 14513 * tcp_ss_rexmit() will send out the one 14514 * byte along with new data. Otherwise, 14515 * fake the retransmission. 14516 */ 14517 flags |= TH_XMIT_NEEDED; 14518 if (!tcp->tcp_rexmit) { 14519 tcp->tcp_rexmit = B_TRUE; 14520 tcp->tcp_dupack_cnt = 0; 14521 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14522 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14523 } 14524 } 14525 } 14526 goto swnd_update; 14527 } 14528 14529 /* 14530 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14531 * If the ACK value acks something that we have not yet sent, it might 14532 * be an old duplicate segment. Send an ACK to re-synchronize the 14533 * other side. 14534 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14535 * state is handled above, so we can always just drop the segment and 14536 * send an ACK here. 14537 * 14538 * Should we send ACKs in response to ACK only segments? 14539 */ 14540 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14541 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14542 /* drop the received segment */ 14543 freemsg(mp); 14544 14545 /* 14546 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14547 * greater than 0, check if the number of such 14548 * bogus ACks is greater than that count. If yes, 14549 * don't send back any ACK. This prevents TCP from 14550 * getting into an ACK storm if somehow an attacker 14551 * successfully spoofs an acceptable segment to our 14552 * peer. 14553 */ 14554 if (tcp_drop_ack_unsent_cnt > 0 && 14555 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14556 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14557 return; 14558 } 14559 mp = tcp_ack_mp(tcp); 14560 if (mp != NULL) { 14561 BUMP_LOCAL(tcp->tcp_obsegs); 14562 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14563 tcp_send_data(tcp, tcp->tcp_wq, mp); 14564 } 14565 return; 14566 } 14567 14568 /* 14569 * TCP gets a new ACK, update the notsack'ed list to delete those 14570 * blocks that are covered by this ACK. 14571 */ 14572 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14573 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14574 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14575 } 14576 14577 /* 14578 * If we got an ACK after fast retransmit, check to see 14579 * if it is a partial ACK. If it is not and the congestion 14580 * window was inflated to account for the other side's 14581 * cached packets, retract it. If it is, do Hoe's algorithm. 14582 */ 14583 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14584 ASSERT(tcp->tcp_rexmit == B_FALSE); 14585 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14586 tcp->tcp_dupack_cnt = 0; 14587 /* 14588 * Restore the orig tcp_cwnd_ssthresh after 14589 * fast retransmit phase. 14590 */ 14591 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14592 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14593 } 14594 tcp->tcp_rexmit_max = seg_ack; 14595 tcp->tcp_cwnd_cnt = 0; 14596 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14597 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14598 14599 /* 14600 * Remove all notsack info to avoid confusion with 14601 * the next fast retrasnmit/recovery phase. 14602 */ 14603 if (tcp->tcp_snd_sack_ok && 14604 tcp->tcp_notsack_list != NULL) { 14605 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14606 } 14607 } else { 14608 if (tcp->tcp_snd_sack_ok && 14609 tcp->tcp_notsack_list != NULL) { 14610 flags |= TH_NEED_SACK_REXMIT; 14611 tcp->tcp_pipe -= mss; 14612 if (tcp->tcp_pipe < 0) 14613 tcp->tcp_pipe = 0; 14614 } else { 14615 /* 14616 * Hoe's algorithm: 14617 * 14618 * Retransmit the unack'ed segment and 14619 * restart fast recovery. Note that we 14620 * need to scale back tcp_cwnd to the 14621 * original value when we started fast 14622 * recovery. This is to prevent overly 14623 * aggressive behaviour in sending new 14624 * segments. 14625 */ 14626 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14627 tcps->tcps_dupack_fast_retransmit * mss; 14628 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14629 flags |= TH_REXMIT_NEEDED; 14630 } 14631 } 14632 } else { 14633 tcp->tcp_dupack_cnt = 0; 14634 if (tcp->tcp_rexmit) { 14635 /* 14636 * TCP is retranmitting. If the ACK ack's all 14637 * outstanding data, update tcp_rexmit_max and 14638 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14639 * to the correct value. 14640 * 14641 * Note that SEQ_LEQ() is used. This is to avoid 14642 * unnecessary fast retransmit caused by dup ACKs 14643 * received when TCP does slow start retransmission 14644 * after a time out. During this phase, TCP may 14645 * send out segments which are already received. 14646 * This causes dup ACKs to be sent back. 14647 */ 14648 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14649 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14650 tcp->tcp_rexmit_nxt = seg_ack; 14651 } 14652 if (seg_ack != tcp->tcp_rexmit_max) { 14653 flags |= TH_XMIT_NEEDED; 14654 } 14655 } else { 14656 tcp->tcp_rexmit = B_FALSE; 14657 tcp->tcp_xmit_zc_clean = B_FALSE; 14658 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14659 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14660 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14661 } 14662 tcp->tcp_ms_we_have_waited = 0; 14663 } 14664 } 14665 14666 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14667 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14668 tcp->tcp_suna = seg_ack; 14669 if (tcp->tcp_zero_win_probe != 0) { 14670 tcp->tcp_zero_win_probe = 0; 14671 tcp->tcp_timer_backoff = 0; 14672 } 14673 14674 /* 14675 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14676 * Note that it cannot be the SYN being ack'ed. The code flow 14677 * will not reach here. 14678 */ 14679 if (mp1 == NULL) { 14680 goto fin_acked; 14681 } 14682 14683 /* 14684 * Update the congestion window. 14685 * 14686 * If TCP is not ECN capable or TCP is ECN capable but the 14687 * congestion experience bit is not set, increase the tcp_cwnd as 14688 * usual. 14689 */ 14690 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14691 cwnd = tcp->tcp_cwnd; 14692 add = mss; 14693 14694 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14695 /* 14696 * This is to prevent an increase of less than 1 MSS of 14697 * tcp_cwnd. With partial increase, tcp_wput_data() 14698 * may send out tinygrams in order to preserve mblk 14699 * boundaries. 14700 * 14701 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14702 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14703 * increased by 1 MSS for every RTTs. 14704 */ 14705 if (tcp->tcp_cwnd_cnt <= 0) { 14706 tcp->tcp_cwnd_cnt = cwnd + add; 14707 } else { 14708 tcp->tcp_cwnd_cnt -= add; 14709 add = 0; 14710 } 14711 } 14712 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14713 } 14714 14715 /* See if the latest urgent data has been acknowledged */ 14716 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14717 SEQ_GT(seg_ack, tcp->tcp_urg)) 14718 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14719 14720 /* Can we update the RTT estimates? */ 14721 if (tcp->tcp_snd_ts_ok) { 14722 /* Ignore zero timestamp echo-reply. */ 14723 if (tcpopt.tcp_opt_ts_ecr != 0) { 14724 tcp_set_rto(tcp, (int32_t)lbolt - 14725 (int32_t)tcpopt.tcp_opt_ts_ecr); 14726 } 14727 14728 /* If needed, restart the timer. */ 14729 if (tcp->tcp_set_timer == 1) { 14730 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14731 tcp->tcp_set_timer = 0; 14732 } 14733 /* 14734 * Update tcp_csuna in case the other side stops sending 14735 * us timestamps. 14736 */ 14737 tcp->tcp_csuna = tcp->tcp_snxt; 14738 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14739 /* 14740 * An ACK sequence we haven't seen before, so get the RTT 14741 * and update the RTO. But first check if the timestamp is 14742 * valid to use. 14743 */ 14744 if ((mp1->b_next != NULL) && 14745 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14746 tcp_set_rto(tcp, (int32_t)lbolt - 14747 (int32_t)(intptr_t)mp1->b_prev); 14748 else 14749 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14750 14751 /* Remeber the last sequence to be ACKed */ 14752 tcp->tcp_csuna = seg_ack; 14753 if (tcp->tcp_set_timer == 1) { 14754 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14755 tcp->tcp_set_timer = 0; 14756 } 14757 } else { 14758 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14759 } 14760 14761 /* Eat acknowledged bytes off the xmit queue. */ 14762 for (;;) { 14763 mblk_t *mp2; 14764 uchar_t *wptr; 14765 14766 wptr = mp1->b_wptr; 14767 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14768 bytes_acked -= (int)(wptr - mp1->b_rptr); 14769 if (bytes_acked < 0) { 14770 mp1->b_rptr = wptr + bytes_acked; 14771 /* 14772 * Set a new timestamp if all the bytes timed by the 14773 * old timestamp have been ack'ed. 14774 */ 14775 if (SEQ_GT(seg_ack, 14776 (uint32_t)(uintptr_t)(mp1->b_next))) { 14777 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14778 mp1->b_next = NULL; 14779 } 14780 break; 14781 } 14782 mp1->b_next = NULL; 14783 mp1->b_prev = NULL; 14784 mp2 = mp1; 14785 mp1 = mp1->b_cont; 14786 14787 /* 14788 * This notification is required for some zero-copy 14789 * clients to maintain a copy semantic. After the data 14790 * is ack'ed, client is safe to modify or reuse the buffer. 14791 */ 14792 if (tcp->tcp_snd_zcopy_aware && 14793 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14794 tcp_zcopy_notify(tcp); 14795 freeb(mp2); 14796 if (bytes_acked == 0) { 14797 if (mp1 == NULL) { 14798 /* Everything is ack'ed, clear the tail. */ 14799 tcp->tcp_xmit_tail = NULL; 14800 /* 14801 * Cancel the timer unless we are still 14802 * waiting for an ACK for the FIN packet. 14803 */ 14804 if (tcp->tcp_timer_tid != 0 && 14805 tcp->tcp_snxt == tcp->tcp_suna) { 14806 (void) TCP_TIMER_CANCEL(tcp, 14807 tcp->tcp_timer_tid); 14808 tcp->tcp_timer_tid = 0; 14809 } 14810 goto pre_swnd_update; 14811 } 14812 if (mp2 != tcp->tcp_xmit_tail) 14813 break; 14814 tcp->tcp_xmit_tail = mp1; 14815 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14816 (uintptr_t)INT_MAX); 14817 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14818 mp1->b_rptr); 14819 break; 14820 } 14821 if (mp1 == NULL) { 14822 /* 14823 * More was acked but there is nothing more 14824 * outstanding. This means that the FIN was 14825 * just acked or that we're talking to a clown. 14826 */ 14827 fin_acked: 14828 ASSERT(tcp->tcp_fin_sent); 14829 tcp->tcp_xmit_tail = NULL; 14830 if (tcp->tcp_fin_sent) { 14831 /* FIN was acked - making progress */ 14832 if (tcp->tcp_ipversion == IPV6_VERSION && 14833 !tcp->tcp_fin_acked) 14834 tcp->tcp_ip_forward_progress = B_TRUE; 14835 tcp->tcp_fin_acked = B_TRUE; 14836 if (tcp->tcp_linger_tid != 0 && 14837 TCP_TIMER_CANCEL(tcp, 14838 tcp->tcp_linger_tid) >= 0) { 14839 tcp_stop_lingering(tcp); 14840 freemsg(mp); 14841 mp = NULL; 14842 } 14843 } else { 14844 /* 14845 * We should never get here because 14846 * we have already checked that the 14847 * number of bytes ack'ed should be 14848 * smaller than or equal to what we 14849 * have sent so far (it is the 14850 * acceptability check of the ACK). 14851 * We can only get here if the send 14852 * queue is corrupted. 14853 * 14854 * Terminate the connection and 14855 * panic the system. It is better 14856 * for us to panic instead of 14857 * continuing to avoid other disaster. 14858 */ 14859 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14860 tcp->tcp_rnxt, TH_RST|TH_ACK); 14861 panic("Memory corruption " 14862 "detected for connection %s.", 14863 tcp_display(tcp, NULL, 14864 DISP_ADDR_AND_PORT)); 14865 /*NOTREACHED*/ 14866 } 14867 goto pre_swnd_update; 14868 } 14869 ASSERT(mp2 != tcp->tcp_xmit_tail); 14870 } 14871 if (tcp->tcp_unsent) { 14872 flags |= TH_XMIT_NEEDED; 14873 } 14874 pre_swnd_update: 14875 tcp->tcp_xmit_head = mp1; 14876 swnd_update: 14877 /* 14878 * The following check is different from most other implementations. 14879 * For bi-directional transfer, when segments are dropped, the 14880 * "normal" check will not accept a window update in those 14881 * retransmitted segemnts. Failing to do that, TCP may send out 14882 * segments which are outside receiver's window. As TCP accepts 14883 * the ack in those retransmitted segments, if the window update in 14884 * the same segment is not accepted, TCP will incorrectly calculates 14885 * that it can send more segments. This can create a deadlock 14886 * with the receiver if its window becomes zero. 14887 */ 14888 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14889 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14890 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14891 /* 14892 * The criteria for update is: 14893 * 14894 * 1. the segment acknowledges some data. Or 14895 * 2. the segment is new, i.e. it has a higher seq num. Or 14896 * 3. the segment is not old and the advertised window is 14897 * larger than the previous advertised window. 14898 */ 14899 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14900 flags |= TH_XMIT_NEEDED; 14901 tcp->tcp_swnd = new_swnd; 14902 if (new_swnd > tcp->tcp_max_swnd) 14903 tcp->tcp_max_swnd = new_swnd; 14904 tcp->tcp_swl1 = seg_seq; 14905 tcp->tcp_swl2 = seg_ack; 14906 } 14907 est: 14908 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14909 14910 switch (tcp->tcp_state) { 14911 case TCPS_FIN_WAIT_1: 14912 if (tcp->tcp_fin_acked) { 14913 tcp->tcp_state = TCPS_FIN_WAIT_2; 14914 /* 14915 * We implement the non-standard BSD/SunOS 14916 * FIN_WAIT_2 flushing algorithm. 14917 * If there is no user attached to this 14918 * TCP endpoint, then this TCP struct 14919 * could hang around forever in FIN_WAIT_2 14920 * state if the peer forgets to send us 14921 * a FIN. To prevent this, we wait only 14922 * 2*MSL (a convenient time value) for 14923 * the FIN to arrive. If it doesn't show up, 14924 * we flush the TCP endpoint. This algorithm, 14925 * though a violation of RFC-793, has worked 14926 * for over 10 years in BSD systems. 14927 * Note: SunOS 4.x waits 675 seconds before 14928 * flushing the FIN_WAIT_2 connection. 14929 */ 14930 TCP_TIMER_RESTART(tcp, 14931 tcps->tcps_fin_wait_2_flush_interval); 14932 } 14933 break; 14934 case TCPS_FIN_WAIT_2: 14935 break; /* Shutdown hook? */ 14936 case TCPS_LAST_ACK: 14937 freemsg(mp); 14938 if (tcp->tcp_fin_acked) { 14939 (void) tcp_clean_death(tcp, 0, 19); 14940 return; 14941 } 14942 goto xmit_check; 14943 case TCPS_CLOSING: 14944 if (tcp->tcp_fin_acked) { 14945 tcp->tcp_state = TCPS_TIME_WAIT; 14946 /* 14947 * Unconditionally clear the exclusive binding 14948 * bit so this TIME-WAIT connection won't 14949 * interfere with new ones. 14950 */ 14951 tcp->tcp_exclbind = 0; 14952 if (!TCP_IS_DETACHED(tcp)) { 14953 TCP_TIMER_RESTART(tcp, 14954 tcps->tcps_time_wait_interval); 14955 } else { 14956 tcp_time_wait_append(tcp); 14957 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14958 } 14959 } 14960 /*FALLTHRU*/ 14961 case TCPS_CLOSE_WAIT: 14962 freemsg(mp); 14963 goto xmit_check; 14964 default: 14965 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14966 break; 14967 } 14968 } 14969 if (flags & TH_FIN) { 14970 /* Make sure we ack the fin */ 14971 flags |= TH_ACK_NEEDED; 14972 if (!tcp->tcp_fin_rcvd) { 14973 tcp->tcp_fin_rcvd = B_TRUE; 14974 tcp->tcp_rnxt++; 14975 tcph = tcp->tcp_tcph; 14976 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14977 14978 /* 14979 * Generate the ordrel_ind at the end unless we 14980 * are an eager guy. 14981 * In the eager case tcp_rsrv will do this when run 14982 * after tcp_accept is done. 14983 */ 14984 if (tcp->tcp_listener == NULL && 14985 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14986 flags |= TH_ORDREL_NEEDED; 14987 switch (tcp->tcp_state) { 14988 case TCPS_SYN_RCVD: 14989 case TCPS_ESTABLISHED: 14990 tcp->tcp_state = TCPS_CLOSE_WAIT; 14991 /* Keepalive? */ 14992 break; 14993 case TCPS_FIN_WAIT_1: 14994 if (!tcp->tcp_fin_acked) { 14995 tcp->tcp_state = TCPS_CLOSING; 14996 break; 14997 } 14998 /* FALLTHRU */ 14999 case TCPS_FIN_WAIT_2: 15000 tcp->tcp_state = TCPS_TIME_WAIT; 15001 /* 15002 * Unconditionally clear the exclusive binding 15003 * bit so this TIME-WAIT connection won't 15004 * interfere with new ones. 15005 */ 15006 tcp->tcp_exclbind = 0; 15007 if (!TCP_IS_DETACHED(tcp)) { 15008 TCP_TIMER_RESTART(tcp, 15009 tcps->tcps_time_wait_interval); 15010 } else { 15011 tcp_time_wait_append(tcp); 15012 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15013 } 15014 if (seg_len) { 15015 /* 15016 * implies data piggybacked on FIN. 15017 * break to handle data. 15018 */ 15019 break; 15020 } 15021 freemsg(mp); 15022 goto ack_check; 15023 } 15024 } 15025 } 15026 if (mp == NULL) 15027 goto xmit_check; 15028 if (seg_len == 0) { 15029 freemsg(mp); 15030 goto xmit_check; 15031 } 15032 if (mp->b_rptr == mp->b_wptr) { 15033 /* 15034 * The header has been consumed, so we remove the 15035 * zero-length mblk here. 15036 */ 15037 mp1 = mp; 15038 mp = mp->b_cont; 15039 freeb(mp1); 15040 } 15041 update_ack: 15042 tcph = tcp->tcp_tcph; 15043 tcp->tcp_rack_cnt++; 15044 { 15045 uint32_t cur_max; 15046 15047 cur_max = tcp->tcp_rack_cur_max; 15048 if (tcp->tcp_rack_cnt >= cur_max) { 15049 /* 15050 * We have more unacked data than we should - send 15051 * an ACK now. 15052 */ 15053 flags |= TH_ACK_NEEDED; 15054 cur_max++; 15055 if (cur_max > tcp->tcp_rack_abs_max) 15056 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15057 else 15058 tcp->tcp_rack_cur_max = cur_max; 15059 } else if (TCP_IS_DETACHED(tcp)) { 15060 /* We don't have an ACK timer for detached TCP. */ 15061 flags |= TH_ACK_NEEDED; 15062 } else if (seg_len < mss) { 15063 /* 15064 * If we get a segment that is less than an mss, and we 15065 * already have unacknowledged data, and the amount 15066 * unacknowledged is not a multiple of mss, then we 15067 * better generate an ACK now. Otherwise, this may be 15068 * the tail piece of a transaction, and we would rather 15069 * wait for the response. 15070 */ 15071 uint32_t udif; 15072 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15073 (uintptr_t)INT_MAX); 15074 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15075 if (udif && (udif % mss)) 15076 flags |= TH_ACK_NEEDED; 15077 else 15078 flags |= TH_ACK_TIMER_NEEDED; 15079 } else { 15080 /* Start delayed ack timer */ 15081 flags |= TH_ACK_TIMER_NEEDED; 15082 } 15083 } 15084 tcp->tcp_rnxt += seg_len; 15085 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15086 15087 if (mp == NULL) 15088 goto xmit_check; 15089 15090 /* Update SACK list */ 15091 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15092 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15093 &(tcp->tcp_num_sack_blk)); 15094 } 15095 15096 if (tcp->tcp_urp_mp) { 15097 tcp->tcp_urp_mp->b_cont = mp; 15098 mp = tcp->tcp_urp_mp; 15099 tcp->tcp_urp_mp = NULL; 15100 /* Ready for a new signal. */ 15101 tcp->tcp_urp_last_valid = B_FALSE; 15102 #ifdef DEBUG 15103 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15104 "tcp_rput: sending exdata_ind %s", 15105 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15106 #endif /* DEBUG */ 15107 } 15108 15109 /* 15110 * Check for ancillary data changes compared to last segment. 15111 */ 15112 if (tcp->tcp_ipv6_recvancillary != 0) { 15113 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15114 ASSERT(mp != NULL); 15115 } 15116 15117 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15118 /* 15119 * Side queue inbound data until the accept happens. 15120 * tcp_accept/tcp_rput drains this when the accept happens. 15121 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15122 * T_EXDATA_IND) it is queued on b_next. 15123 * XXX Make urgent data use this. Requires: 15124 * Removing tcp_listener check for TH_URG 15125 * Making M_PCPROTO and MARK messages skip the eager case 15126 */ 15127 15128 if (tcp->tcp_kssl_pending) { 15129 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15130 mblk_t *, mp); 15131 tcp_kssl_input(tcp, mp); 15132 } else { 15133 tcp_rcv_enqueue(tcp, mp, seg_len); 15134 } 15135 } else { 15136 sodirect_t *sodp = tcp->tcp_sodirect; 15137 15138 /* 15139 * If an sodirect connection and an enabled sodirect_t then 15140 * sodp will be set to point to the tcp_t/sonode_t shared 15141 * sodirect_t and the sodirect_t's lock will be held. 15142 */ 15143 if (sodp != NULL) { 15144 mutex_enter(sodp->sod_lockp); 15145 if (!(sodp->sod_state & SOD_ENABLED) || 15146 (tcp->tcp_kssl_ctx != NULL && 15147 DB_TYPE(mp) == M_DATA)) { 15148 sodp = NULL; 15149 } 15150 mutex_exit(sodp->sod_lockp); 15151 } 15152 if (mp->b_datap->db_type != M_DATA || 15153 (flags & TH_MARKNEXT_NEEDED)) { 15154 if (IPCL_IS_NONSTR(connp)) { 15155 int error; 15156 15157 if ((*connp->conn_upcalls->su_recv) 15158 (connp->conn_upper_handle, mp, 15159 seg_len, 0, &error, NULL) <= 0) { 15160 if (error == ENOSPC) { 15161 tcp->tcp_rwnd -= seg_len; 15162 } else if (error == EOPNOTSUPP) { 15163 tcp_rcv_enqueue(tcp, mp, 15164 seg_len); 15165 } 15166 } 15167 } else if (sodp != NULL) { 15168 mutex_enter(sodp->sod_lockp); 15169 SOD_UIOAFINI(sodp); 15170 if (!SOD_QEMPTY(sodp) && 15171 (sodp->sod_state & SOD_WAKE_NOT)) { 15172 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15173 /* sod_wakeup() did the mutex_exit() */ 15174 } else { 15175 mutex_exit(sodp->sod_lockp); 15176 } 15177 } else if (tcp->tcp_rcv_list != NULL) { 15178 flags |= tcp_rcv_drain(tcp); 15179 } 15180 ASSERT(tcp->tcp_rcv_list == NULL || 15181 tcp->tcp_fused_sigurg); 15182 15183 if (flags & TH_MARKNEXT_NEEDED) { 15184 #ifdef DEBUG 15185 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15186 "tcp_rput: sending MSGMARKNEXT %s", 15187 tcp_display(tcp, NULL, 15188 DISP_PORT_ONLY)); 15189 #endif /* DEBUG */ 15190 mp->b_flag |= MSGMARKNEXT; 15191 flags &= ~TH_MARKNEXT_NEEDED; 15192 } 15193 15194 /* Does this need SSL processing first? */ 15195 if ((tcp->tcp_kssl_ctx != NULL) && 15196 (DB_TYPE(mp) == M_DATA)) { 15197 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15198 mblk_t *, mp); 15199 tcp_kssl_input(tcp, mp); 15200 } else if (!IPCL_IS_NONSTR(connp)) { 15201 /* Already handled non-STREAMS case. */ 15202 putnext(tcp->tcp_rq, mp); 15203 if (!canputnext(tcp->tcp_rq)) 15204 tcp->tcp_rwnd -= seg_len; 15205 } 15206 } else if ((tcp->tcp_kssl_ctx != NULL) && 15207 (DB_TYPE(mp) == M_DATA)) { 15208 /* Does this need SSL processing first? */ 15209 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15210 tcp_kssl_input(tcp, mp); 15211 } else if (IPCL_IS_NONSTR(connp)) { 15212 /* Non-STREAMS socket */ 15213 boolean_t push = flags & (TH_PUSH|TH_FIN); 15214 int error; 15215 15216 if ((*connp->conn_upcalls->su_recv)( 15217 connp->conn_upper_handle, 15218 mp, seg_len, 0, &error, &push) <= 0) { 15219 if (error == ENOSPC) { 15220 tcp->tcp_rwnd -= seg_len; 15221 } else if (error == EOPNOTSUPP) { 15222 tcp_rcv_enqueue(tcp, mp, seg_len); 15223 } 15224 } else if (push) { 15225 /* 15226 * PUSH bit set and sockfs is not 15227 * flow controlled 15228 */ 15229 flags |= tcp_rwnd_reopen(tcp); 15230 } 15231 } else if (sodp != NULL) { 15232 /* 15233 * Sodirect so all mblk_t's are queued on the 15234 * socket directly, check for wakeup of blocked 15235 * reader (if any), and last if flow-controled. 15236 */ 15237 mutex_enter(sodp->sod_lockp); 15238 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15239 if ((sodp->sod_state & SOD_WAKE_NEED) || 15240 (flags & (TH_PUSH|TH_FIN))) { 15241 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15242 /* sod_wakeup() did the mutex_exit() */ 15243 } else { 15244 if (SOD_QFULL(sodp)) { 15245 /* Q is full, need backenable */ 15246 SOD_QSETBE(sodp); 15247 } 15248 mutex_exit(sodp->sod_lockp); 15249 } 15250 } else if ((flags & (TH_PUSH|TH_FIN)) || 15251 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15252 if (tcp->tcp_rcv_list != NULL) { 15253 /* 15254 * Enqueue the new segment first and then 15255 * call tcp_rcv_drain() to send all data 15256 * up. The other way to do this is to 15257 * send all queued data up and then call 15258 * putnext() to send the new segment up. 15259 * This way can remove the else part later 15260 * on. 15261 * 15262 * We don't do this to avoid one more call to 15263 * canputnext() as tcp_rcv_drain() needs to 15264 * call canputnext(). 15265 */ 15266 tcp_rcv_enqueue(tcp, mp, seg_len); 15267 flags |= tcp_rcv_drain(tcp); 15268 } else { 15269 putnext(tcp->tcp_rq, mp); 15270 if (!canputnext(tcp->tcp_rq)) 15271 tcp->tcp_rwnd -= seg_len; 15272 } 15273 } else { 15274 /* 15275 * Enqueue all packets when processing an mblk 15276 * from the co queue and also enqueue normal packets. 15277 * For packets which belong to SSL stream do SSL 15278 * processing first. 15279 */ 15280 tcp_rcv_enqueue(tcp, mp, seg_len); 15281 } 15282 /* 15283 * Make sure the timer is running if we have data waiting 15284 * for a push bit. This provides resiliency against 15285 * implementations that do not correctly generate push bits. 15286 * 15287 * Note, for sodirect if Q isn't empty and there's not a 15288 * pending wakeup then we need a timer. Also note that sodp 15289 * is assumed to be still valid after exit()ing the sod_lockp 15290 * above and while the SOD state can change it can only change 15291 * such that the Q is empty now even though data was added 15292 * above. 15293 */ 15294 if (!IPCL_IS_NONSTR(connp) && 15295 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15296 (sodp->sod_state & SOD_WAKE_NOT)) || 15297 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15298 tcp->tcp_push_tid == 0) { 15299 /* 15300 * The connection may be closed at this point, so don't 15301 * do anything for a detached tcp. 15302 */ 15303 if (!TCP_IS_DETACHED(tcp)) 15304 tcp->tcp_push_tid = TCP_TIMER(tcp, 15305 tcp_push_timer, 15306 MSEC_TO_TICK( 15307 tcps->tcps_push_timer_interval)); 15308 } 15309 } 15310 15311 xmit_check: 15312 /* Is there anything left to do? */ 15313 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15314 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15315 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15316 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15317 goto done; 15318 15319 /* Any transmit work to do and a non-zero window? */ 15320 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15321 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15322 if (flags & TH_REXMIT_NEEDED) { 15323 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15324 15325 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15326 if (snd_size > mss) 15327 snd_size = mss; 15328 if (snd_size > tcp->tcp_swnd) 15329 snd_size = tcp->tcp_swnd; 15330 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15331 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15332 B_TRUE); 15333 15334 if (mp1 != NULL) { 15335 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15336 tcp->tcp_csuna = tcp->tcp_snxt; 15337 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15338 UPDATE_MIB(&tcps->tcps_mib, 15339 tcpRetransBytes, snd_size); 15340 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15341 } 15342 } 15343 if (flags & TH_NEED_SACK_REXMIT) { 15344 tcp_sack_rxmit(tcp, &flags); 15345 } 15346 /* 15347 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15348 * out new segment. Note that tcp_rexmit should not be 15349 * set, otherwise TH_LIMIT_XMIT should not be set. 15350 */ 15351 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15352 if (!tcp->tcp_rexmit) { 15353 tcp_wput_data(tcp, NULL, B_FALSE); 15354 } else { 15355 tcp_ss_rexmit(tcp); 15356 } 15357 } 15358 /* 15359 * Adjust tcp_cwnd back to normal value after sending 15360 * new data segments. 15361 */ 15362 if (flags & TH_LIMIT_XMIT) { 15363 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15364 /* 15365 * This will restart the timer. Restarting the 15366 * timer is used to avoid a timeout before the 15367 * limited transmitted segment's ACK gets back. 15368 */ 15369 if (tcp->tcp_xmit_head != NULL) 15370 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15371 } 15372 15373 /* Anything more to do? */ 15374 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15375 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15376 goto done; 15377 } 15378 ack_check: 15379 if (flags & TH_SEND_URP_MARK) { 15380 ASSERT(tcp->tcp_urp_mark_mp); 15381 ASSERT(!IPCL_IS_NONSTR(connp)); 15382 /* 15383 * Send up any queued data and then send the mark message 15384 */ 15385 sodirect_t *sodp; 15386 15387 SOD_PTR_ENTER(tcp, sodp); 15388 15389 mp1 = tcp->tcp_urp_mark_mp; 15390 tcp->tcp_urp_mark_mp = NULL; 15391 if (sodp != NULL) { 15392 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15393 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15394 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15395 } 15396 ASSERT(tcp->tcp_rcv_list == NULL); 15397 15398 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15399 /* sod_wakeup() does the mutex_exit() */ 15400 } else if (tcp->tcp_rcv_list != NULL) { 15401 flags |= tcp_rcv_drain(tcp); 15402 15403 ASSERT(tcp->tcp_rcv_list == NULL || 15404 tcp->tcp_fused_sigurg); 15405 15406 } 15407 putnext(tcp->tcp_rq, mp1); 15408 #ifdef DEBUG 15409 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15410 "tcp_rput: sending zero-length %s %s", 15411 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15412 "MSGNOTMARKNEXT"), 15413 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15414 #endif /* DEBUG */ 15415 flags &= ~TH_SEND_URP_MARK; 15416 } 15417 if (flags & TH_ACK_NEEDED) { 15418 /* 15419 * Time to send an ack for some reason. 15420 */ 15421 mp1 = tcp_ack_mp(tcp); 15422 15423 if (mp1 != NULL) { 15424 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15425 BUMP_LOCAL(tcp->tcp_obsegs); 15426 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15427 } 15428 if (tcp->tcp_ack_tid != 0) { 15429 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15430 tcp->tcp_ack_tid = 0; 15431 } 15432 } 15433 if (flags & TH_ACK_TIMER_NEEDED) { 15434 /* 15435 * Arrange for deferred ACK or push wait timeout. 15436 * Start timer if it is not already running. 15437 */ 15438 if (tcp->tcp_ack_tid == 0) { 15439 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15440 MSEC_TO_TICK(tcp->tcp_localnet ? 15441 (clock_t)tcps->tcps_local_dack_interval : 15442 (clock_t)tcps->tcps_deferred_ack_interval)); 15443 } 15444 } 15445 if (flags & TH_ORDREL_NEEDED) { 15446 /* 15447 * Send up the ordrel_ind unless we are an eager guy. 15448 * In the eager case tcp_rsrv will do this when run 15449 * after tcp_accept is done. 15450 */ 15451 sodirect_t *sodp; 15452 15453 ASSERT(tcp->tcp_listener == NULL); 15454 15455 if (IPCL_IS_NONSTR(connp)) { 15456 ASSERT(tcp->tcp_ordrel_mp == NULL); 15457 tcp->tcp_ordrel_done = B_TRUE; 15458 (*connp->conn_upcalls->su_opctl) 15459 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15460 goto done; 15461 } 15462 15463 SOD_PTR_ENTER(tcp, sodp); 15464 if (sodp != NULL) { 15465 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15466 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15467 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15468 } 15469 /* No more sodirect */ 15470 tcp->tcp_sodirect = NULL; 15471 if (!SOD_QEMPTY(sodp)) { 15472 /* Mblk(s) to process, notify */ 15473 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15474 /* sod_wakeup() does the mutex_exit() */ 15475 } else { 15476 /* Nothing to process */ 15477 mutex_exit(sodp->sod_lockp); 15478 } 15479 } else if (tcp->tcp_rcv_list != NULL) { 15480 /* 15481 * Push any mblk(s) enqueued from co processing. 15482 */ 15483 flags |= tcp_rcv_drain(tcp); 15484 15485 ASSERT(tcp->tcp_rcv_list == NULL || 15486 tcp->tcp_fused_sigurg); 15487 } 15488 15489 mp1 = tcp->tcp_ordrel_mp; 15490 tcp->tcp_ordrel_mp = NULL; 15491 tcp->tcp_ordrel_done = B_TRUE; 15492 putnext(tcp->tcp_rq, mp1); 15493 } 15494 done: 15495 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15496 } 15497 15498 /* 15499 * This function does PAWS protection check. Returns B_TRUE if the 15500 * segment passes the PAWS test, else returns B_FALSE. 15501 */ 15502 boolean_t 15503 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15504 { 15505 uint8_t flags; 15506 int options; 15507 uint8_t *up; 15508 15509 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15510 /* 15511 * If timestamp option is aligned nicely, get values inline, 15512 * otherwise call general routine to parse. Only do that 15513 * if timestamp is the only option. 15514 */ 15515 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15516 TCPOPT_REAL_TS_LEN && 15517 OK_32PTR((up = ((uint8_t *)tcph) + 15518 TCP_MIN_HEADER_LENGTH)) && 15519 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15520 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15521 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15522 15523 options = TCP_OPT_TSTAMP_PRESENT; 15524 } else { 15525 if (tcp->tcp_snd_sack_ok) { 15526 tcpoptp->tcp = tcp; 15527 } else { 15528 tcpoptp->tcp = NULL; 15529 } 15530 options = tcp_parse_options(tcph, tcpoptp); 15531 } 15532 15533 if (options & TCP_OPT_TSTAMP_PRESENT) { 15534 /* 15535 * Do PAWS per RFC 1323 section 4.2. Accept RST 15536 * regardless of the timestamp, page 18 RFC 1323.bis. 15537 */ 15538 if ((flags & TH_RST) == 0 && 15539 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15540 tcp->tcp_ts_recent)) { 15541 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15542 PAWS_TIMEOUT)) { 15543 /* This segment is not acceptable. */ 15544 return (B_FALSE); 15545 } else { 15546 /* 15547 * Connection has been idle for 15548 * too long. Reset the timestamp 15549 * and assume the segment is valid. 15550 */ 15551 tcp->tcp_ts_recent = 15552 tcpoptp->tcp_opt_ts_val; 15553 } 15554 } 15555 } else { 15556 /* 15557 * If we don't get a timestamp on every packet, we 15558 * figure we can't really trust 'em, so we stop sending 15559 * and parsing them. 15560 */ 15561 tcp->tcp_snd_ts_ok = B_FALSE; 15562 15563 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15564 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15565 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15566 /* 15567 * Adjust the tcp_mss accordingly. We also need to 15568 * adjust tcp_cwnd here in accordance with the new mss. 15569 * But we avoid doing a slow start here so as to not 15570 * to lose on the transfer rate built up so far. 15571 */ 15572 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15573 if (tcp->tcp_snd_sack_ok) { 15574 ASSERT(tcp->tcp_sack_info != NULL); 15575 tcp->tcp_max_sack_blk = 4; 15576 } 15577 } 15578 return (B_TRUE); 15579 } 15580 15581 /* 15582 * Attach ancillary data to a received TCP segments for the 15583 * ancillary pieces requested by the application that are 15584 * different than they were in the previous data segment. 15585 * 15586 * Save the "current" values once memory allocation is ok so that 15587 * when memory allocation fails we can just wait for the next data segment. 15588 */ 15589 static mblk_t * 15590 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15591 { 15592 struct T_optdata_ind *todi; 15593 int optlen; 15594 uchar_t *optptr; 15595 struct T_opthdr *toh; 15596 uint_t addflag; /* Which pieces to add */ 15597 mblk_t *mp1; 15598 15599 optlen = 0; 15600 addflag = 0; 15601 /* If app asked for pktinfo and the index has changed ... */ 15602 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15603 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15604 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15605 optlen += sizeof (struct T_opthdr) + 15606 sizeof (struct in6_pktinfo); 15607 addflag |= TCP_IPV6_RECVPKTINFO; 15608 } 15609 /* If app asked for hoplimit and it has changed ... */ 15610 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15611 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15612 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15613 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15614 addflag |= TCP_IPV6_RECVHOPLIMIT; 15615 } 15616 /* If app asked for tclass and it has changed ... */ 15617 if ((ipp->ipp_fields & IPPF_TCLASS) && 15618 ipp->ipp_tclass != tcp->tcp_recvtclass && 15619 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15620 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15621 addflag |= TCP_IPV6_RECVTCLASS; 15622 } 15623 /* 15624 * If app asked for hopbyhop headers and it has changed ... 15625 * For security labels, note that (1) security labels can't change on 15626 * a connected socket at all, (2) we're connected to at most one peer, 15627 * (3) if anything changes, then it must be some other extra option. 15628 */ 15629 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15630 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15631 (ipp->ipp_fields & IPPF_HOPOPTS), 15632 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15633 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15634 tcp->tcp_label_len; 15635 addflag |= TCP_IPV6_RECVHOPOPTS; 15636 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15637 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15638 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15639 return (mp); 15640 } 15641 /* If app asked for dst headers before routing headers ... */ 15642 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15643 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15644 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15645 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15646 optlen += sizeof (struct T_opthdr) + 15647 ipp->ipp_rtdstoptslen; 15648 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15649 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15650 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15651 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15652 return (mp); 15653 } 15654 /* If app asked for routing headers and it has changed ... */ 15655 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15656 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15657 (ipp->ipp_fields & IPPF_RTHDR), 15658 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15659 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15660 addflag |= TCP_IPV6_RECVRTHDR; 15661 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15662 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15663 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15664 return (mp); 15665 } 15666 /* If app asked for dest headers and it has changed ... */ 15667 if ((tcp->tcp_ipv6_recvancillary & 15668 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15669 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15670 (ipp->ipp_fields & IPPF_DSTOPTS), 15671 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15672 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15673 addflag |= TCP_IPV6_RECVDSTOPTS; 15674 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15675 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15676 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15677 return (mp); 15678 } 15679 15680 if (optlen == 0) { 15681 /* Nothing to add */ 15682 return (mp); 15683 } 15684 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15685 if (mp1 == NULL) { 15686 /* 15687 * Defer sending ancillary data until the next TCP segment 15688 * arrives. 15689 */ 15690 return (mp); 15691 } 15692 mp1->b_cont = mp; 15693 mp = mp1; 15694 mp->b_wptr += sizeof (*todi) + optlen; 15695 mp->b_datap->db_type = M_PROTO; 15696 todi = (struct T_optdata_ind *)mp->b_rptr; 15697 todi->PRIM_type = T_OPTDATA_IND; 15698 todi->DATA_flag = 1; /* MORE data */ 15699 todi->OPT_length = optlen; 15700 todi->OPT_offset = sizeof (*todi); 15701 optptr = (uchar_t *)&todi[1]; 15702 /* 15703 * If app asked for pktinfo and the index has changed ... 15704 * Note that the local address never changes for the connection. 15705 */ 15706 if (addflag & TCP_IPV6_RECVPKTINFO) { 15707 struct in6_pktinfo *pkti; 15708 15709 toh = (struct T_opthdr *)optptr; 15710 toh->level = IPPROTO_IPV6; 15711 toh->name = IPV6_PKTINFO; 15712 toh->len = sizeof (*toh) + sizeof (*pkti); 15713 toh->status = 0; 15714 optptr += sizeof (*toh); 15715 pkti = (struct in6_pktinfo *)optptr; 15716 if (tcp->tcp_ipversion == IPV6_VERSION) 15717 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15718 else 15719 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15720 &pkti->ipi6_addr); 15721 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15722 optptr += sizeof (*pkti); 15723 ASSERT(OK_32PTR(optptr)); 15724 /* Save as "last" value */ 15725 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15726 } 15727 /* If app asked for hoplimit and it has changed ... */ 15728 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15729 toh = (struct T_opthdr *)optptr; 15730 toh->level = IPPROTO_IPV6; 15731 toh->name = IPV6_HOPLIMIT; 15732 toh->len = sizeof (*toh) + sizeof (uint_t); 15733 toh->status = 0; 15734 optptr += sizeof (*toh); 15735 *(uint_t *)optptr = ipp->ipp_hoplimit; 15736 optptr += sizeof (uint_t); 15737 ASSERT(OK_32PTR(optptr)); 15738 /* Save as "last" value */ 15739 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15740 } 15741 /* If app asked for tclass and it has changed ... */ 15742 if (addflag & TCP_IPV6_RECVTCLASS) { 15743 toh = (struct T_opthdr *)optptr; 15744 toh->level = IPPROTO_IPV6; 15745 toh->name = IPV6_TCLASS; 15746 toh->len = sizeof (*toh) + sizeof (uint_t); 15747 toh->status = 0; 15748 optptr += sizeof (*toh); 15749 *(uint_t *)optptr = ipp->ipp_tclass; 15750 optptr += sizeof (uint_t); 15751 ASSERT(OK_32PTR(optptr)); 15752 /* Save as "last" value */ 15753 tcp->tcp_recvtclass = ipp->ipp_tclass; 15754 } 15755 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15756 toh = (struct T_opthdr *)optptr; 15757 toh->level = IPPROTO_IPV6; 15758 toh->name = IPV6_HOPOPTS; 15759 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15760 tcp->tcp_label_len; 15761 toh->status = 0; 15762 optptr += sizeof (*toh); 15763 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15764 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15765 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15766 ASSERT(OK_32PTR(optptr)); 15767 /* Save as last value */ 15768 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15769 (ipp->ipp_fields & IPPF_HOPOPTS), 15770 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15771 } 15772 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15773 toh = (struct T_opthdr *)optptr; 15774 toh->level = IPPROTO_IPV6; 15775 toh->name = IPV6_RTHDRDSTOPTS; 15776 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15777 toh->status = 0; 15778 optptr += sizeof (*toh); 15779 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15780 optptr += ipp->ipp_rtdstoptslen; 15781 ASSERT(OK_32PTR(optptr)); 15782 /* Save as last value */ 15783 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15784 &tcp->tcp_rtdstoptslen, 15785 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15786 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15787 } 15788 if (addflag & TCP_IPV6_RECVRTHDR) { 15789 toh = (struct T_opthdr *)optptr; 15790 toh->level = IPPROTO_IPV6; 15791 toh->name = IPV6_RTHDR; 15792 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15793 toh->status = 0; 15794 optptr += sizeof (*toh); 15795 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15796 optptr += ipp->ipp_rthdrlen; 15797 ASSERT(OK_32PTR(optptr)); 15798 /* Save as last value */ 15799 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15800 (ipp->ipp_fields & IPPF_RTHDR), 15801 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15802 } 15803 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15804 toh = (struct T_opthdr *)optptr; 15805 toh->level = IPPROTO_IPV6; 15806 toh->name = IPV6_DSTOPTS; 15807 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15808 toh->status = 0; 15809 optptr += sizeof (*toh); 15810 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15811 optptr += ipp->ipp_dstoptslen; 15812 ASSERT(OK_32PTR(optptr)); 15813 /* Save as last value */ 15814 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15815 (ipp->ipp_fields & IPPF_DSTOPTS), 15816 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15817 } 15818 ASSERT(optptr == mp->b_wptr); 15819 return (mp); 15820 } 15821 15822 /* 15823 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15824 * messages. 15825 */ 15826 void 15827 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15828 { 15829 uchar_t *rptr = mp->b_rptr; 15830 queue_t *q = tcp->tcp_rq; 15831 struct T_error_ack *tea; 15832 15833 switch (mp->b_datap->db_type) { 15834 case M_PROTO: 15835 case M_PCPROTO: 15836 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15837 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15838 break; 15839 tea = (struct T_error_ack *)rptr; 15840 ASSERT(tea->PRIM_type != T_BIND_ACK); 15841 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15842 tea->ERROR_prim != T_BIND_REQ); 15843 switch (tea->PRIM_type) { 15844 case T_ERROR_ACK: 15845 if (tcp->tcp_debug) { 15846 (void) strlog(TCP_MOD_ID, 0, 1, 15847 SL_TRACE|SL_ERROR, 15848 "tcp_rput_other: case T_ERROR_ACK, " 15849 "ERROR_prim == %d", 15850 tea->ERROR_prim); 15851 } 15852 switch (tea->ERROR_prim) { 15853 case T_SVR4_OPTMGMT_REQ: 15854 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15855 /* T_OPTMGMT_REQ generated by TCP */ 15856 printf("T_SVR4_OPTMGMT_REQ failed " 15857 "%d/%d - dropped (cnt %d)\n", 15858 tea->TLI_error, tea->UNIX_error, 15859 tcp->tcp_drop_opt_ack_cnt); 15860 freemsg(mp); 15861 tcp->tcp_drop_opt_ack_cnt--; 15862 return; 15863 } 15864 break; 15865 } 15866 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15867 tcp->tcp_drop_opt_ack_cnt > 0) { 15868 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15869 "- dropped (cnt %d)\n", 15870 tea->TLI_error, tea->UNIX_error, 15871 tcp->tcp_drop_opt_ack_cnt); 15872 freemsg(mp); 15873 tcp->tcp_drop_opt_ack_cnt--; 15874 return; 15875 } 15876 break; 15877 case T_OPTMGMT_ACK: 15878 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15879 /* T_OPTMGMT_REQ generated by TCP */ 15880 freemsg(mp); 15881 tcp->tcp_drop_opt_ack_cnt--; 15882 return; 15883 } 15884 break; 15885 default: 15886 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15887 break; 15888 } 15889 break; 15890 case M_FLUSH: 15891 if (*rptr & FLUSHR) 15892 flushq(q, FLUSHDATA); 15893 break; 15894 default: 15895 /* M_CTL will be directly sent to tcp_icmp_error() */ 15896 ASSERT(DB_TYPE(mp) != M_CTL); 15897 break; 15898 } 15899 /* 15900 * Make sure we set this bit before sending the ACK for 15901 * bind. Otherwise accept could possibly run and free 15902 * this tcp struct. 15903 */ 15904 ASSERT(q != NULL); 15905 putnext(q, mp); 15906 } 15907 15908 /* ARGSUSED */ 15909 static void 15910 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15911 { 15912 conn_t *connp = (conn_t *)arg; 15913 tcp_t *tcp = connp->conn_tcp; 15914 queue_t *q = tcp->tcp_rq; 15915 uint_t thwin; 15916 tcp_stack_t *tcps = tcp->tcp_tcps; 15917 sodirect_t *sodp; 15918 boolean_t fc; 15919 15920 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15921 tcp->tcp_rsrv_mp = mp; 15922 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15923 15924 TCP_STAT(tcps, tcp_rsrv_calls); 15925 15926 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15927 return; 15928 } 15929 15930 if (tcp->tcp_fused) { 15931 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15932 15933 ASSERT(tcp->tcp_fused); 15934 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15935 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15936 ASSERT(!TCP_IS_DETACHED(tcp)); 15937 ASSERT(tcp->tcp_connp->conn_sqp == 15938 peer_tcp->tcp_connp->conn_sqp); 15939 15940 /* 15941 * Normally we would not get backenabled in synchronous 15942 * streams mode, but in case this happens, we need to plug 15943 * synchronous streams during our drain to prevent a race 15944 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15945 */ 15946 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15947 if (tcp->tcp_rcv_list != NULL) 15948 (void) tcp_rcv_drain(tcp); 15949 15950 if (peer_tcp > tcp) { 15951 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15952 mutex_enter(&tcp->tcp_non_sq_lock); 15953 } else { 15954 mutex_enter(&tcp->tcp_non_sq_lock); 15955 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15956 } 15957 15958 if (peer_tcp->tcp_flow_stopped && 15959 (TCP_UNSENT_BYTES(peer_tcp) <= 15960 peer_tcp->tcp_xmit_lowater)) { 15961 tcp_clrqfull(peer_tcp); 15962 } 15963 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15964 mutex_exit(&tcp->tcp_non_sq_lock); 15965 15966 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15967 TCP_STAT(tcps, tcp_fusion_backenabled); 15968 return; 15969 } 15970 15971 SOD_PTR_ENTER(tcp, sodp); 15972 if (sodp != NULL) { 15973 /* An sodirect connection */ 15974 if (SOD_QFULL(sodp)) { 15975 /* Flow-controlled, need another back-enable */ 15976 fc = B_TRUE; 15977 SOD_QSETBE(sodp); 15978 } else { 15979 /* Not flow-controlled */ 15980 fc = B_FALSE; 15981 } 15982 mutex_exit(sodp->sod_lockp); 15983 } else if (canputnext(q)) { 15984 /* STREAMS, not flow-controlled */ 15985 fc = B_FALSE; 15986 } else { 15987 /* STREAMS, flow-controlled */ 15988 fc = B_TRUE; 15989 } 15990 if (!fc) { 15991 /* Not flow-controlled, open rwnd */ 15992 tcp->tcp_rwnd = q->q_hiwat; 15993 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15994 << tcp->tcp_rcv_ws; 15995 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15996 /* 15997 * Send back a window update immediately if TCP is above 15998 * ESTABLISHED state and the increase of the rcv window 15999 * that the other side knows is at least 1 MSS after flow 16000 * control is lifted. 16001 */ 16002 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16003 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16004 tcp_xmit_ctl(NULL, tcp, 16005 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16006 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16007 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16008 } 16009 } 16010 } 16011 16012 /* 16013 * The read side service routine is called mostly when we get back-enabled as a 16014 * result of flow control relief. Since we don't actually queue anything in 16015 * TCP, we have no data to send out of here. What we do is clear the receive 16016 * window, and send out a window update. 16017 */ 16018 static void 16019 tcp_rsrv(queue_t *q) 16020 { 16021 conn_t *connp = Q_TO_CONN(q); 16022 tcp_t *tcp = connp->conn_tcp; 16023 mblk_t *mp; 16024 tcp_stack_t *tcps = tcp->tcp_tcps; 16025 16026 /* No code does a putq on the read side */ 16027 ASSERT(q->q_first == NULL); 16028 16029 /* Nothing to do for the default queue */ 16030 if (q == tcps->tcps_g_q) { 16031 return; 16032 } 16033 16034 /* 16035 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16036 * been run. So just return. 16037 */ 16038 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16039 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16040 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16041 return; 16042 } 16043 tcp->tcp_rsrv_mp = NULL; 16044 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16045 16046 CONN_INC_REF(connp); 16047 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16048 SQ_PROCESS, SQTAG_TCP_RSRV); 16049 } 16050 16051 /* 16052 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16053 * We do not allow the receive window to shrink. After setting rwnd, 16054 * set the flow control hiwat of the stream. 16055 * 16056 * This function is called in 2 cases: 16057 * 16058 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16059 * connection (passive open) and in tcp_rput_data() for active connect. 16060 * This is called after tcp_mss_set() when the desired MSS value is known. 16061 * This makes sure that our window size is a mutiple of the other side's 16062 * MSS. 16063 * 2) Handling SO_RCVBUF option. 16064 * 16065 * It is ASSUMED that the requested size is a multiple of the current MSS. 16066 * 16067 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16068 * user requests so. 16069 */ 16070 static int 16071 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16072 { 16073 uint32_t mss = tcp->tcp_mss; 16074 uint32_t old_max_rwnd; 16075 uint32_t max_transmittable_rwnd; 16076 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16077 tcp_stack_t *tcps = tcp->tcp_tcps; 16078 16079 if (tcp->tcp_fused) { 16080 size_t sth_hiwat; 16081 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16082 16083 ASSERT(peer_tcp != NULL); 16084 /* 16085 * Record the stream head's high water mark for 16086 * this endpoint; this is used for flow-control 16087 * purposes in tcp_fuse_output(). 16088 */ 16089 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16090 if (!tcp_detached) { 16091 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16092 sth_hiwat); 16093 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16094 conn_t *connp = tcp->tcp_connp; 16095 struct sock_proto_props sopp; 16096 16097 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16098 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16099 16100 (*connp->conn_upcalls->su_set_proto_props) 16101 (connp->conn_upper_handle, &sopp); 16102 } 16103 } 16104 16105 /* 16106 * In the fusion case, the maxpsz stream head value of 16107 * our peer is set according to its send buffer size 16108 * and our receive buffer size; since the latter may 16109 * have changed we need to update the peer's maxpsz. 16110 */ 16111 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16112 return (rwnd); 16113 } 16114 16115 if (tcp_detached) { 16116 old_max_rwnd = tcp->tcp_rwnd; 16117 } else { 16118 old_max_rwnd = tcp->tcp_recv_hiwater; 16119 } 16120 16121 /* 16122 * Insist on a receive window that is at least 16123 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16124 * funny TCP interactions of Nagle algorithm, SWS avoidance 16125 * and delayed acknowledgement. 16126 */ 16127 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16128 16129 /* 16130 * If window size info has already been exchanged, TCP should not 16131 * shrink the window. Shrinking window is doable if done carefully. 16132 * We may add that support later. But so far there is not a real 16133 * need to do that. 16134 */ 16135 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16136 /* MSS may have changed, do a round up again. */ 16137 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16138 } 16139 16140 /* 16141 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16142 * can be applied even before the window scale option is decided. 16143 */ 16144 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16145 if (rwnd > max_transmittable_rwnd) { 16146 rwnd = max_transmittable_rwnd - 16147 (max_transmittable_rwnd % mss); 16148 if (rwnd < mss) 16149 rwnd = max_transmittable_rwnd; 16150 /* 16151 * If we're over the limit we may have to back down tcp_rwnd. 16152 * The increment below won't work for us. So we set all three 16153 * here and the increment below will have no effect. 16154 */ 16155 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16156 } 16157 if (tcp->tcp_localnet) { 16158 tcp->tcp_rack_abs_max = 16159 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16160 } else { 16161 /* 16162 * For a remote host on a different subnet (through a router), 16163 * we ack every other packet to be conforming to RFC1122. 16164 * tcp_deferred_acks_max is default to 2. 16165 */ 16166 tcp->tcp_rack_abs_max = 16167 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16168 } 16169 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16170 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16171 else 16172 tcp->tcp_rack_cur_max = 0; 16173 /* 16174 * Increment the current rwnd by the amount the maximum grew (we 16175 * can not overwrite it since we might be in the middle of a 16176 * connection.) 16177 */ 16178 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16179 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16180 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16181 tcp->tcp_cwnd_max = rwnd; 16182 16183 if (tcp_detached) 16184 return (rwnd); 16185 /* 16186 * We set the maximum receive window into rq->q_hiwat if it is 16187 * a STREAMS socket. 16188 * This is not actually used for flow control. 16189 */ 16190 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16191 tcp->tcp_rq->q_hiwat = rwnd; 16192 tcp->tcp_recv_hiwater = rwnd; 16193 /* 16194 * Set the STREAM head high water mark. This doesn't have to be 16195 * here, since we are simply using default values, but we would 16196 * prefer to choose these values algorithmically, with a likely 16197 * relationship to rwnd. 16198 */ 16199 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16200 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16201 return (rwnd); 16202 } 16203 16204 /* 16205 * Return SNMP stuff in buffer in mpdata. 16206 */ 16207 mblk_t * 16208 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16209 { 16210 mblk_t *mpdata; 16211 mblk_t *mp_conn_ctl = NULL; 16212 mblk_t *mp_conn_tail; 16213 mblk_t *mp_attr_ctl = NULL; 16214 mblk_t *mp_attr_tail; 16215 mblk_t *mp6_conn_ctl = NULL; 16216 mblk_t *mp6_conn_tail; 16217 mblk_t *mp6_attr_ctl = NULL; 16218 mblk_t *mp6_attr_tail; 16219 struct opthdr *optp; 16220 mib2_tcpConnEntry_t tce; 16221 mib2_tcp6ConnEntry_t tce6; 16222 mib2_transportMLPEntry_t mlp; 16223 connf_t *connfp; 16224 int i; 16225 boolean_t ispriv; 16226 zoneid_t zoneid; 16227 int v4_conn_idx; 16228 int v6_conn_idx; 16229 conn_t *connp = Q_TO_CONN(q); 16230 tcp_stack_t *tcps; 16231 ip_stack_t *ipst; 16232 mblk_t *mp2ctl; 16233 16234 /* 16235 * make a copy of the original message 16236 */ 16237 mp2ctl = copymsg(mpctl); 16238 16239 if (mpctl == NULL || 16240 (mpdata = mpctl->b_cont) == NULL || 16241 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16242 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16243 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16244 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16245 freemsg(mp_conn_ctl); 16246 freemsg(mp_attr_ctl); 16247 freemsg(mp6_conn_ctl); 16248 freemsg(mp6_attr_ctl); 16249 freemsg(mpctl); 16250 freemsg(mp2ctl); 16251 return (NULL); 16252 } 16253 16254 ipst = connp->conn_netstack->netstack_ip; 16255 tcps = connp->conn_netstack->netstack_tcp; 16256 16257 /* build table of connections -- need count in fixed part */ 16258 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16259 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16260 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16261 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16262 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16263 16264 ispriv = 16265 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16266 zoneid = Q_TO_CONN(q)->conn_zoneid; 16267 16268 v4_conn_idx = v6_conn_idx = 0; 16269 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16270 16271 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16272 ipst = tcps->tcps_netstack->netstack_ip; 16273 16274 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16275 16276 connp = NULL; 16277 16278 while ((connp = 16279 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16280 tcp_t *tcp; 16281 boolean_t needattr; 16282 16283 if (connp->conn_zoneid != zoneid) 16284 continue; /* not in this zone */ 16285 16286 tcp = connp->conn_tcp; 16287 UPDATE_MIB(&tcps->tcps_mib, 16288 tcpHCInSegs, tcp->tcp_ibsegs); 16289 tcp->tcp_ibsegs = 0; 16290 UPDATE_MIB(&tcps->tcps_mib, 16291 tcpHCOutSegs, tcp->tcp_obsegs); 16292 tcp->tcp_obsegs = 0; 16293 16294 tce6.tcp6ConnState = tce.tcpConnState = 16295 tcp_snmp_state(tcp); 16296 if (tce.tcpConnState == MIB2_TCP_established || 16297 tce.tcpConnState == MIB2_TCP_closeWait) 16298 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16299 16300 needattr = B_FALSE; 16301 bzero(&mlp, sizeof (mlp)); 16302 if (connp->conn_mlp_type != mlptSingle) { 16303 if (connp->conn_mlp_type == mlptShared || 16304 connp->conn_mlp_type == mlptBoth) 16305 mlp.tme_flags |= MIB2_TMEF_SHARED; 16306 if (connp->conn_mlp_type == mlptPrivate || 16307 connp->conn_mlp_type == mlptBoth) 16308 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16309 needattr = B_TRUE; 16310 } 16311 if (connp->conn_peercred != NULL) { 16312 ts_label_t *tsl; 16313 16314 tsl = crgetlabel(connp->conn_peercred); 16315 mlp.tme_doi = label2doi(tsl); 16316 mlp.tme_label = *label2bslabel(tsl); 16317 needattr = B_TRUE; 16318 } 16319 16320 /* Create a message to report on IPv6 entries */ 16321 if (tcp->tcp_ipversion == IPV6_VERSION) { 16322 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16323 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16324 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16325 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16326 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16327 /* Don't want just anybody seeing these... */ 16328 if (ispriv) { 16329 tce6.tcp6ConnEntryInfo.ce_snxt = 16330 tcp->tcp_snxt; 16331 tce6.tcp6ConnEntryInfo.ce_suna = 16332 tcp->tcp_suna; 16333 tce6.tcp6ConnEntryInfo.ce_rnxt = 16334 tcp->tcp_rnxt; 16335 tce6.tcp6ConnEntryInfo.ce_rack = 16336 tcp->tcp_rack; 16337 } else { 16338 /* 16339 * Netstat, unfortunately, uses this to 16340 * get send/receive queue sizes. How to fix? 16341 * Why not compute the difference only? 16342 */ 16343 tce6.tcp6ConnEntryInfo.ce_snxt = 16344 tcp->tcp_snxt - tcp->tcp_suna; 16345 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16346 tce6.tcp6ConnEntryInfo.ce_rnxt = 16347 tcp->tcp_rnxt - tcp->tcp_rack; 16348 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16349 } 16350 16351 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16352 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16353 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16354 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16355 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16356 16357 tce6.tcp6ConnCreationProcess = 16358 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16359 tcp->tcp_cpid; 16360 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16361 16362 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16363 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16364 16365 mlp.tme_connidx = v6_conn_idx++; 16366 if (needattr) 16367 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16368 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16369 } 16370 /* 16371 * Create an IPv4 table entry for IPv4 entries and also 16372 * for IPv6 entries which are bound to in6addr_any 16373 * but don't have IPV6_V6ONLY set. 16374 * (i.e. anything an IPv4 peer could connect to) 16375 */ 16376 if (tcp->tcp_ipversion == IPV4_VERSION || 16377 (tcp->tcp_state <= TCPS_LISTEN && 16378 !tcp->tcp_connp->conn_ipv6_v6only && 16379 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16380 if (tcp->tcp_ipversion == IPV6_VERSION) { 16381 tce.tcpConnRemAddress = INADDR_ANY; 16382 tce.tcpConnLocalAddress = INADDR_ANY; 16383 } else { 16384 tce.tcpConnRemAddress = 16385 tcp->tcp_remote; 16386 tce.tcpConnLocalAddress = 16387 tcp->tcp_ip_src; 16388 } 16389 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16390 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16391 /* Don't want just anybody seeing these... */ 16392 if (ispriv) { 16393 tce.tcpConnEntryInfo.ce_snxt = 16394 tcp->tcp_snxt; 16395 tce.tcpConnEntryInfo.ce_suna = 16396 tcp->tcp_suna; 16397 tce.tcpConnEntryInfo.ce_rnxt = 16398 tcp->tcp_rnxt; 16399 tce.tcpConnEntryInfo.ce_rack = 16400 tcp->tcp_rack; 16401 } else { 16402 /* 16403 * Netstat, unfortunately, uses this to 16404 * get send/receive queue sizes. How 16405 * to fix? 16406 * Why not compute the difference only? 16407 */ 16408 tce.tcpConnEntryInfo.ce_snxt = 16409 tcp->tcp_snxt - tcp->tcp_suna; 16410 tce.tcpConnEntryInfo.ce_suna = 0; 16411 tce.tcpConnEntryInfo.ce_rnxt = 16412 tcp->tcp_rnxt - tcp->tcp_rack; 16413 tce.tcpConnEntryInfo.ce_rack = 0; 16414 } 16415 16416 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16417 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16418 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16419 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16420 tce.tcpConnEntryInfo.ce_state = 16421 tcp->tcp_state; 16422 16423 tce.tcpConnCreationProcess = 16424 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16425 tcp->tcp_cpid; 16426 tce.tcpConnCreationTime = tcp->tcp_open_time; 16427 16428 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16429 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16430 16431 mlp.tme_connidx = v4_conn_idx++; 16432 if (needattr) 16433 (void) snmp_append_data2( 16434 mp_attr_ctl->b_cont, 16435 &mp_attr_tail, (char *)&mlp, 16436 sizeof (mlp)); 16437 } 16438 } 16439 } 16440 16441 /* fixed length structure for IPv4 and IPv6 counters */ 16442 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16443 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16444 sizeof (mib2_tcp6ConnEntry_t)); 16445 /* synchronize 32- and 64-bit counters */ 16446 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16447 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16448 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16449 optp->level = MIB2_TCP; 16450 optp->name = 0; 16451 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16452 sizeof (tcps->tcps_mib)); 16453 optp->len = msgdsize(mpdata); 16454 qreply(q, mpctl); 16455 16456 /* table of connections... */ 16457 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16458 sizeof (struct T_optmgmt_ack)]; 16459 optp->level = MIB2_TCP; 16460 optp->name = MIB2_TCP_CONN; 16461 optp->len = msgdsize(mp_conn_ctl->b_cont); 16462 qreply(q, mp_conn_ctl); 16463 16464 /* table of MLP attributes... */ 16465 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16466 sizeof (struct T_optmgmt_ack)]; 16467 optp->level = MIB2_TCP; 16468 optp->name = EXPER_XPORT_MLP; 16469 optp->len = msgdsize(mp_attr_ctl->b_cont); 16470 if (optp->len == 0) 16471 freemsg(mp_attr_ctl); 16472 else 16473 qreply(q, mp_attr_ctl); 16474 16475 /* table of IPv6 connections... */ 16476 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16477 sizeof (struct T_optmgmt_ack)]; 16478 optp->level = MIB2_TCP6; 16479 optp->name = MIB2_TCP6_CONN; 16480 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16481 qreply(q, mp6_conn_ctl); 16482 16483 /* table of IPv6 MLP attributes... */ 16484 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16485 sizeof (struct T_optmgmt_ack)]; 16486 optp->level = MIB2_TCP6; 16487 optp->name = EXPER_XPORT_MLP; 16488 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16489 if (optp->len == 0) 16490 freemsg(mp6_attr_ctl); 16491 else 16492 qreply(q, mp6_attr_ctl); 16493 return (mp2ctl); 16494 } 16495 16496 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16497 /* ARGSUSED */ 16498 int 16499 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16500 { 16501 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16502 16503 switch (level) { 16504 case MIB2_TCP: 16505 switch (name) { 16506 case 13: 16507 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16508 return (0); 16509 /* TODO: delete entry defined by tce */ 16510 return (1); 16511 default: 16512 return (0); 16513 } 16514 default: 16515 return (1); 16516 } 16517 } 16518 16519 /* Translate TCP state to MIB2 TCP state. */ 16520 static int 16521 tcp_snmp_state(tcp_t *tcp) 16522 { 16523 if (tcp == NULL) 16524 return (0); 16525 16526 switch (tcp->tcp_state) { 16527 case TCPS_CLOSED: 16528 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16529 case TCPS_BOUND: 16530 return (MIB2_TCP_closed); 16531 case TCPS_LISTEN: 16532 return (MIB2_TCP_listen); 16533 case TCPS_SYN_SENT: 16534 return (MIB2_TCP_synSent); 16535 case TCPS_SYN_RCVD: 16536 return (MIB2_TCP_synReceived); 16537 case TCPS_ESTABLISHED: 16538 return (MIB2_TCP_established); 16539 case TCPS_CLOSE_WAIT: 16540 return (MIB2_TCP_closeWait); 16541 case TCPS_FIN_WAIT_1: 16542 return (MIB2_TCP_finWait1); 16543 case TCPS_CLOSING: 16544 return (MIB2_TCP_closing); 16545 case TCPS_LAST_ACK: 16546 return (MIB2_TCP_lastAck); 16547 case TCPS_FIN_WAIT_2: 16548 return (MIB2_TCP_finWait2); 16549 case TCPS_TIME_WAIT: 16550 return (MIB2_TCP_timeWait); 16551 default: 16552 return (0); 16553 } 16554 } 16555 16556 static char tcp_report_header[] = 16557 "TCP " MI_COL_HDRPAD_STR 16558 "zone dest snxt suna " 16559 "swnd rnxt rack rwnd rto mss w sw rw t " 16560 "recent [lport,fport] state"; 16561 16562 /* 16563 * TCP status report triggered via the Named Dispatch mechanism. 16564 */ 16565 /* ARGSUSED */ 16566 static void 16567 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16568 cred_t *cr) 16569 { 16570 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16571 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16572 char cflag; 16573 in6_addr_t v6dst; 16574 char buf[80]; 16575 uint_t print_len, buf_len; 16576 16577 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16578 if (buf_len <= 0) 16579 return; 16580 16581 if (hashval >= 0) 16582 (void) sprintf(hash, "%03d ", hashval); 16583 else 16584 hash[0] = '\0'; 16585 16586 /* 16587 * Note that we use the remote address in the tcp_b structure. 16588 * This means that it will print out the real destination address, 16589 * not the next hop's address if source routing is used. This 16590 * avoid the confusion on the output because user may not 16591 * know that source routing is used for a connection. 16592 */ 16593 if (tcp->tcp_ipversion == IPV4_VERSION) { 16594 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16595 } else { 16596 v6dst = tcp->tcp_remote_v6; 16597 } 16598 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16599 /* 16600 * the ispriv checks are so that normal users cannot determine 16601 * sequence number information using NDD. 16602 */ 16603 16604 if (TCP_IS_DETACHED(tcp)) 16605 cflag = '*'; 16606 else 16607 cflag = ' '; 16608 print_len = snprintf((char *)mp->b_wptr, buf_len, 16609 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16610 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16611 hash, 16612 (void *)tcp, 16613 tcp->tcp_connp->conn_zoneid, 16614 addrbuf, 16615 (ispriv) ? tcp->tcp_snxt : 0, 16616 (ispriv) ? tcp->tcp_suna : 0, 16617 tcp->tcp_swnd, 16618 (ispriv) ? tcp->tcp_rnxt : 0, 16619 (ispriv) ? tcp->tcp_rack : 0, 16620 tcp->tcp_rwnd, 16621 tcp->tcp_rto, 16622 tcp->tcp_mss, 16623 tcp->tcp_snd_ws_ok, 16624 tcp->tcp_snd_ws, 16625 tcp->tcp_rcv_ws, 16626 tcp->tcp_snd_ts_ok, 16627 tcp->tcp_ts_recent, 16628 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16629 if (print_len < buf_len) { 16630 ((mblk_t *)mp)->b_wptr += print_len; 16631 } else { 16632 ((mblk_t *)mp)->b_wptr += buf_len; 16633 } 16634 } 16635 16636 /* 16637 * TCP status report (for listeners only) triggered via the Named Dispatch 16638 * mechanism. 16639 */ 16640 /* ARGSUSED */ 16641 static void 16642 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16643 { 16644 char addrbuf[INET6_ADDRSTRLEN]; 16645 in6_addr_t v6dst; 16646 uint_t print_len, buf_len; 16647 16648 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16649 if (buf_len <= 0) 16650 return; 16651 16652 if (tcp->tcp_ipversion == IPV4_VERSION) { 16653 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16654 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16655 } else { 16656 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16657 addrbuf, sizeof (addrbuf)); 16658 } 16659 print_len = snprintf((char *)mp->b_wptr, buf_len, 16660 "%03d " 16661 MI_COL_PTRFMT_STR 16662 "%d %s %05u %08u %d/%d/%d%c\n", 16663 hashval, (void *)tcp, 16664 tcp->tcp_connp->conn_zoneid, 16665 addrbuf, 16666 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16667 tcp->tcp_conn_req_seqnum, 16668 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16669 tcp->tcp_conn_req_max, 16670 tcp->tcp_syn_defense ? '*' : ' '); 16671 if (print_len < buf_len) { 16672 ((mblk_t *)mp)->b_wptr += print_len; 16673 } else { 16674 ((mblk_t *)mp)->b_wptr += buf_len; 16675 } 16676 } 16677 16678 /* TCP status report triggered via the Named Dispatch mechanism. */ 16679 /* ARGSUSED */ 16680 static int 16681 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16682 { 16683 tcp_t *tcp; 16684 int i; 16685 conn_t *connp; 16686 connf_t *connfp; 16687 zoneid_t zoneid; 16688 tcp_stack_t *tcps; 16689 ip_stack_t *ipst; 16690 16691 zoneid = Q_TO_CONN(q)->conn_zoneid; 16692 tcps = Q_TO_TCP(q)->tcp_tcps; 16693 16694 /* 16695 * Because of the ndd constraint, at most we can have 64K buffer 16696 * to put in all TCP info. So to be more efficient, just 16697 * allocate a 64K buffer here, assuming we need that large buffer. 16698 * This may be a problem as any user can read tcp_status. Therefore 16699 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16700 * This should be OK as normal users should not do this too often. 16701 */ 16702 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16703 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16704 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16705 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16706 return (0); 16707 } 16708 } 16709 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16710 /* The following may work even if we cannot get a large buf. */ 16711 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16712 return (0); 16713 } 16714 16715 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16716 16717 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16718 16719 ipst = tcps->tcps_netstack->netstack_ip; 16720 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16721 16722 connp = NULL; 16723 16724 while ((connp = 16725 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16726 tcp = connp->conn_tcp; 16727 if (zoneid != GLOBAL_ZONEID && 16728 zoneid != connp->conn_zoneid) 16729 continue; 16730 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16731 cr); 16732 } 16733 16734 } 16735 16736 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16737 return (0); 16738 } 16739 16740 /* TCP status report triggered via the Named Dispatch mechanism. */ 16741 /* ARGSUSED */ 16742 static int 16743 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16744 { 16745 tf_t *tbf; 16746 tcp_t *tcp, *ltcp; 16747 int i; 16748 zoneid_t zoneid; 16749 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16750 16751 zoneid = Q_TO_CONN(q)->conn_zoneid; 16752 16753 /* Refer to comments in tcp_status_report(). */ 16754 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16755 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16756 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16757 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16758 return (0); 16759 } 16760 } 16761 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16762 /* The following may work even if we cannot get a large buf. */ 16763 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16764 return (0); 16765 } 16766 16767 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16768 16769 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16770 tbf = &tcps->tcps_bind_fanout[i]; 16771 mutex_enter(&tbf->tf_lock); 16772 for (ltcp = tbf->tf_tcp; ltcp != NULL; 16773 ltcp = ltcp->tcp_bind_hash) { 16774 for (tcp = ltcp; tcp != NULL; 16775 tcp = tcp->tcp_bind_hash_port) { 16776 if (zoneid != GLOBAL_ZONEID && 16777 zoneid != tcp->tcp_connp->conn_zoneid) 16778 continue; 16779 CONN_INC_REF(tcp->tcp_connp); 16780 tcp_report_item(mp->b_cont, tcp, i, 16781 Q_TO_TCP(q), cr); 16782 CONN_DEC_REF(tcp->tcp_connp); 16783 } 16784 } 16785 mutex_exit(&tbf->tf_lock); 16786 } 16787 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16788 return (0); 16789 } 16790 16791 /* TCP status report triggered via the Named Dispatch mechanism. */ 16792 /* ARGSUSED */ 16793 static int 16794 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16795 { 16796 connf_t *connfp; 16797 conn_t *connp; 16798 tcp_t *tcp; 16799 int i; 16800 zoneid_t zoneid; 16801 tcp_stack_t *tcps; 16802 ip_stack_t *ipst; 16803 16804 zoneid = Q_TO_CONN(q)->conn_zoneid; 16805 tcps = Q_TO_TCP(q)->tcp_tcps; 16806 16807 /* Refer to comments in tcp_status_report(). */ 16808 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16809 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16810 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16811 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16812 return (0); 16813 } 16814 } 16815 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16816 /* The following may work even if we cannot get a large buf. */ 16817 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16818 return (0); 16819 } 16820 16821 (void) mi_mpprintf(mp, 16822 " TCP " MI_COL_HDRPAD_STR 16823 "zone IP addr port seqnum backlog (q0/q/max)"); 16824 16825 ipst = tcps->tcps_netstack->netstack_ip; 16826 16827 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16828 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16829 connp = NULL; 16830 while ((connp = 16831 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16832 tcp = connp->conn_tcp; 16833 if (zoneid != GLOBAL_ZONEID && 16834 zoneid != connp->conn_zoneid) 16835 continue; 16836 tcp_report_listener(mp->b_cont, tcp, i); 16837 } 16838 } 16839 16840 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16841 return (0); 16842 } 16843 16844 /* TCP status report triggered via the Named Dispatch mechanism. */ 16845 /* ARGSUSED */ 16846 static int 16847 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16848 { 16849 connf_t *connfp; 16850 conn_t *connp; 16851 tcp_t *tcp; 16852 int i; 16853 zoneid_t zoneid; 16854 tcp_stack_t *tcps; 16855 ip_stack_t *ipst; 16856 16857 zoneid = Q_TO_CONN(q)->conn_zoneid; 16858 tcps = Q_TO_TCP(q)->tcp_tcps; 16859 ipst = tcps->tcps_netstack->netstack_ip; 16860 16861 /* Refer to comments in tcp_status_report(). */ 16862 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16863 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16864 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16865 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16866 return (0); 16867 } 16868 } 16869 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16870 /* The following may work even if we cannot get a large buf. */ 16871 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16872 return (0); 16873 } 16874 16875 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16876 ipst->ips_ipcl_conn_fanout_size); 16877 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16878 16879 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16880 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16881 connp = NULL; 16882 while ((connp = 16883 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16884 tcp = connp->conn_tcp; 16885 if (zoneid != GLOBAL_ZONEID && 16886 zoneid != connp->conn_zoneid) 16887 continue; 16888 tcp_report_item(mp->b_cont, tcp, i, 16889 Q_TO_TCP(q), cr); 16890 } 16891 } 16892 16893 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16894 return (0); 16895 } 16896 16897 /* TCP status report triggered via the Named Dispatch mechanism. */ 16898 /* ARGSUSED */ 16899 static int 16900 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16901 { 16902 tf_t *tf; 16903 tcp_t *tcp; 16904 int i; 16905 zoneid_t zoneid; 16906 tcp_stack_t *tcps; 16907 16908 zoneid = Q_TO_CONN(q)->conn_zoneid; 16909 tcps = Q_TO_TCP(q)->tcp_tcps; 16910 16911 /* Refer to comments in tcp_status_report(). */ 16912 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16913 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16914 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16915 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16916 return (0); 16917 } 16918 } 16919 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16920 /* The following may work even if we cannot get a large buf. */ 16921 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16922 return (0); 16923 } 16924 16925 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16926 16927 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16928 tf = &tcps->tcps_acceptor_fanout[i]; 16929 mutex_enter(&tf->tf_lock); 16930 for (tcp = tf->tf_tcp; tcp != NULL; 16931 tcp = tcp->tcp_acceptor_hash) { 16932 if (zoneid != GLOBAL_ZONEID && 16933 zoneid != tcp->tcp_connp->conn_zoneid) 16934 continue; 16935 tcp_report_item(mp->b_cont, tcp, i, 16936 Q_TO_TCP(q), cr); 16937 } 16938 mutex_exit(&tf->tf_lock); 16939 } 16940 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16941 return (0); 16942 } 16943 16944 /* 16945 * tcp_timer is the timer service routine. It handles the retransmission, 16946 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16947 * from the state of the tcp instance what kind of action needs to be done 16948 * at the time it is called. 16949 */ 16950 static void 16951 tcp_timer(void *arg) 16952 { 16953 mblk_t *mp; 16954 clock_t first_threshold; 16955 clock_t second_threshold; 16956 clock_t ms; 16957 uint32_t mss; 16958 conn_t *connp = (conn_t *)arg; 16959 tcp_t *tcp = connp->conn_tcp; 16960 tcp_stack_t *tcps = tcp->tcp_tcps; 16961 16962 tcp->tcp_timer_tid = 0; 16963 16964 if (tcp->tcp_fused) 16965 return; 16966 16967 first_threshold = tcp->tcp_first_timer_threshold; 16968 second_threshold = tcp->tcp_second_timer_threshold; 16969 switch (tcp->tcp_state) { 16970 case TCPS_IDLE: 16971 case TCPS_BOUND: 16972 case TCPS_LISTEN: 16973 return; 16974 case TCPS_SYN_RCVD: { 16975 tcp_t *listener = tcp->tcp_listener; 16976 16977 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16978 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16979 /* it's our first timeout */ 16980 tcp->tcp_syn_rcvd_timeout = 1; 16981 mutex_enter(&listener->tcp_eager_lock); 16982 listener->tcp_syn_rcvd_timeout++; 16983 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16984 /* 16985 * Make this eager available for drop if we 16986 * need to drop one to accomodate a new 16987 * incoming SYN request. 16988 */ 16989 MAKE_DROPPABLE(listener, tcp); 16990 } 16991 if (!listener->tcp_syn_defense && 16992 (listener->tcp_syn_rcvd_timeout > 16993 (tcps->tcps_conn_req_max_q0 >> 2)) && 16994 (tcps->tcps_conn_req_max_q0 > 200)) { 16995 /* We may be under attack. Put on a defense. */ 16996 listener->tcp_syn_defense = B_TRUE; 16997 cmn_err(CE_WARN, "High TCP connect timeout " 16998 "rate! System (port %d) may be under a " 16999 "SYN flood attack!", 17000 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17001 17002 listener->tcp_ip_addr_cache = kmem_zalloc( 17003 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17004 KM_NOSLEEP); 17005 } 17006 mutex_exit(&listener->tcp_eager_lock); 17007 } else if (listener != NULL) { 17008 mutex_enter(&listener->tcp_eager_lock); 17009 tcp->tcp_syn_rcvd_timeout++; 17010 if (tcp->tcp_syn_rcvd_timeout > 1 && 17011 !tcp->tcp_closemp_used) { 17012 /* 17013 * This is our second timeout. Put the tcp in 17014 * the list of droppable eagers to allow it to 17015 * be dropped, if needed. We don't check 17016 * whether tcp_dontdrop is set or not to 17017 * protect ourselve from a SYN attack where a 17018 * remote host can spoof itself as one of the 17019 * good IP source and continue to hold 17020 * resources too long. 17021 */ 17022 MAKE_DROPPABLE(listener, tcp); 17023 } 17024 mutex_exit(&listener->tcp_eager_lock); 17025 } 17026 } 17027 /* FALLTHRU */ 17028 case TCPS_SYN_SENT: 17029 first_threshold = tcp->tcp_first_ctimer_threshold; 17030 second_threshold = tcp->tcp_second_ctimer_threshold; 17031 break; 17032 case TCPS_ESTABLISHED: 17033 case TCPS_FIN_WAIT_1: 17034 case TCPS_CLOSING: 17035 case TCPS_CLOSE_WAIT: 17036 case TCPS_LAST_ACK: 17037 /* If we have data to rexmit */ 17038 if (tcp->tcp_suna != tcp->tcp_snxt) { 17039 clock_t time_to_wait; 17040 17041 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17042 if (!tcp->tcp_xmit_head) 17043 break; 17044 time_to_wait = lbolt - 17045 (clock_t)tcp->tcp_xmit_head->b_prev; 17046 time_to_wait = tcp->tcp_rto - 17047 TICK_TO_MSEC(time_to_wait); 17048 /* 17049 * If the timer fires too early, 1 clock tick earlier, 17050 * restart the timer. 17051 */ 17052 if (time_to_wait > msec_per_tick) { 17053 TCP_STAT(tcps, tcp_timer_fire_early); 17054 TCP_TIMER_RESTART(tcp, time_to_wait); 17055 return; 17056 } 17057 /* 17058 * When we probe zero windows, we force the swnd open. 17059 * If our peer acks with a closed window swnd will be 17060 * set to zero by tcp_rput(). As long as we are 17061 * receiving acks tcp_rput will 17062 * reset 'tcp_ms_we_have_waited' so as not to trip the 17063 * first and second interval actions. NOTE: the timer 17064 * interval is allowed to continue its exponential 17065 * backoff. 17066 */ 17067 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17068 if (tcp->tcp_debug) { 17069 (void) strlog(TCP_MOD_ID, 0, 1, 17070 SL_TRACE, "tcp_timer: zero win"); 17071 } 17072 } else { 17073 /* 17074 * After retransmission, we need to do 17075 * slow start. Set the ssthresh to one 17076 * half of current effective window and 17077 * cwnd to one MSS. Also reset 17078 * tcp_cwnd_cnt. 17079 * 17080 * Note that if tcp_ssthresh is reduced because 17081 * of ECN, do not reduce it again unless it is 17082 * already one window of data away (tcp_cwr 17083 * should then be cleared) or this is a 17084 * timeout for a retransmitted segment. 17085 */ 17086 uint32_t npkt; 17087 17088 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17089 npkt = ((tcp->tcp_timer_backoff ? 17090 tcp->tcp_cwnd_ssthresh : 17091 tcp->tcp_snxt - 17092 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17093 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17094 tcp->tcp_mss; 17095 } 17096 tcp->tcp_cwnd = tcp->tcp_mss; 17097 tcp->tcp_cwnd_cnt = 0; 17098 if (tcp->tcp_ecn_ok) { 17099 tcp->tcp_cwr = B_TRUE; 17100 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17101 tcp->tcp_ecn_cwr_sent = B_FALSE; 17102 } 17103 } 17104 break; 17105 } 17106 /* 17107 * We have something to send yet we cannot send. The 17108 * reason can be: 17109 * 17110 * 1. Zero send window: we need to do zero window probe. 17111 * 2. Zero cwnd: because of ECN, we need to "clock out 17112 * segments. 17113 * 3. SWS avoidance: receiver may have shrunk window, 17114 * reset our knowledge. 17115 * 17116 * Note that condition 2 can happen with either 1 or 17117 * 3. But 1 and 3 are exclusive. 17118 */ 17119 if (tcp->tcp_unsent != 0) { 17120 if (tcp->tcp_cwnd == 0) { 17121 /* 17122 * Set tcp_cwnd to 1 MSS so that a 17123 * new segment can be sent out. We 17124 * are "clocking out" new data when 17125 * the network is really congested. 17126 */ 17127 ASSERT(tcp->tcp_ecn_ok); 17128 tcp->tcp_cwnd = tcp->tcp_mss; 17129 } 17130 if (tcp->tcp_swnd == 0) { 17131 /* Extend window for zero window probe */ 17132 tcp->tcp_swnd++; 17133 tcp->tcp_zero_win_probe = B_TRUE; 17134 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17135 } else { 17136 /* 17137 * Handle timeout from sender SWS avoidance. 17138 * Reset our knowledge of the max send window 17139 * since the receiver might have reduced its 17140 * receive buffer. Avoid setting tcp_max_swnd 17141 * to one since that will essentially disable 17142 * the SWS checks. 17143 * 17144 * Note that since we don't have a SWS 17145 * state variable, if the timeout is set 17146 * for ECN but not for SWS, this 17147 * code will also be executed. This is 17148 * fine as tcp_max_swnd is updated 17149 * constantly and it will not affect 17150 * anything. 17151 */ 17152 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17153 } 17154 tcp_wput_data(tcp, NULL, B_FALSE); 17155 return; 17156 } 17157 /* Is there a FIN that needs to be to re retransmitted? */ 17158 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17159 !tcp->tcp_fin_acked) 17160 break; 17161 /* Nothing to do, return without restarting timer. */ 17162 TCP_STAT(tcps, tcp_timer_fire_miss); 17163 return; 17164 case TCPS_FIN_WAIT_2: 17165 /* 17166 * User closed the TCP endpoint and peer ACK'ed our FIN. 17167 * We waited some time for for peer's FIN, but it hasn't 17168 * arrived. We flush the connection now to avoid 17169 * case where the peer has rebooted. 17170 */ 17171 if (TCP_IS_DETACHED(tcp)) { 17172 (void) tcp_clean_death(tcp, 0, 23); 17173 } else { 17174 TCP_TIMER_RESTART(tcp, 17175 tcps->tcps_fin_wait_2_flush_interval); 17176 } 17177 return; 17178 case TCPS_TIME_WAIT: 17179 (void) tcp_clean_death(tcp, 0, 24); 17180 return; 17181 default: 17182 if (tcp->tcp_debug) { 17183 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17184 "tcp_timer: strange state (%d) %s", 17185 tcp->tcp_state, tcp_display(tcp, NULL, 17186 DISP_PORT_ONLY)); 17187 } 17188 return; 17189 } 17190 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17191 /* 17192 * For zero window probe, we need to send indefinitely, 17193 * unless we have not heard from the other side for some 17194 * time... 17195 */ 17196 if ((tcp->tcp_zero_win_probe == 0) || 17197 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17198 second_threshold)) { 17199 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17200 /* 17201 * If TCP is in SYN_RCVD state, send back a 17202 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17203 * should be zero in TCPS_SYN_RCVD state. 17204 */ 17205 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17206 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17207 "in SYN_RCVD", 17208 tcp, tcp->tcp_snxt, 17209 tcp->tcp_rnxt, TH_RST | TH_ACK); 17210 } 17211 (void) tcp_clean_death(tcp, 17212 tcp->tcp_client_errno ? 17213 tcp->tcp_client_errno : ETIMEDOUT, 25); 17214 return; 17215 } else { 17216 /* 17217 * Set tcp_ms_we_have_waited to second_threshold 17218 * so that in next timeout, we will do the above 17219 * check (lbolt - tcp_last_recv_time). This is 17220 * also to avoid overflow. 17221 * 17222 * We don't need to decrement tcp_timer_backoff 17223 * to avoid overflow because it will be decremented 17224 * later if new timeout value is greater than 17225 * tcp_rexmit_interval_max. In the case when 17226 * tcp_rexmit_interval_max is greater than 17227 * second_threshold, it means that we will wait 17228 * longer than second_threshold to send the next 17229 * window probe. 17230 */ 17231 tcp->tcp_ms_we_have_waited = second_threshold; 17232 } 17233 } else if (ms > first_threshold) { 17234 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17235 tcp->tcp_xmit_head != NULL) { 17236 tcp->tcp_xmit_head = 17237 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17238 } 17239 /* 17240 * We have been retransmitting for too long... The RTT 17241 * we calculated is probably incorrect. Reinitialize it. 17242 * Need to compensate for 0 tcp_rtt_sa. Reset 17243 * tcp_rtt_update so that we won't accidentally cache a 17244 * bad value. But only do this if this is not a zero 17245 * window probe. 17246 */ 17247 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17248 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17249 (tcp->tcp_rtt_sa >> 5); 17250 tcp->tcp_rtt_sa = 0; 17251 tcp_ip_notify(tcp); 17252 tcp->tcp_rtt_update = 0; 17253 } 17254 } 17255 tcp->tcp_timer_backoff++; 17256 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17257 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17258 tcps->tcps_rexmit_interval_min) { 17259 /* 17260 * This means the original RTO is tcp_rexmit_interval_min. 17261 * So we will use tcp_rexmit_interval_min as the RTO value 17262 * and do the backoff. 17263 */ 17264 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17265 } else { 17266 ms <<= tcp->tcp_timer_backoff; 17267 } 17268 if (ms > tcps->tcps_rexmit_interval_max) { 17269 ms = tcps->tcps_rexmit_interval_max; 17270 /* 17271 * ms is at max, decrement tcp_timer_backoff to avoid 17272 * overflow. 17273 */ 17274 tcp->tcp_timer_backoff--; 17275 } 17276 tcp->tcp_ms_we_have_waited += ms; 17277 if (tcp->tcp_zero_win_probe == 0) { 17278 tcp->tcp_rto = ms; 17279 } 17280 TCP_TIMER_RESTART(tcp, ms); 17281 /* 17282 * This is after a timeout and tcp_rto is backed off. Set 17283 * tcp_set_timer to 1 so that next time RTO is updated, we will 17284 * restart the timer with a correct value. 17285 */ 17286 tcp->tcp_set_timer = 1; 17287 mss = tcp->tcp_snxt - tcp->tcp_suna; 17288 if (mss > tcp->tcp_mss) 17289 mss = tcp->tcp_mss; 17290 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17291 mss = tcp->tcp_swnd; 17292 17293 if ((mp = tcp->tcp_xmit_head) != NULL) 17294 mp->b_prev = (mblk_t *)lbolt; 17295 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17296 B_TRUE); 17297 17298 /* 17299 * When slow start after retransmission begins, start with 17300 * this seq no. tcp_rexmit_max marks the end of special slow 17301 * start phase. tcp_snd_burst controls how many segments 17302 * can be sent because of an ack. 17303 */ 17304 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17305 tcp->tcp_snd_burst = TCP_CWND_SS; 17306 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17307 (tcp->tcp_unsent == 0)) { 17308 tcp->tcp_rexmit_max = tcp->tcp_fss; 17309 } else { 17310 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17311 } 17312 tcp->tcp_rexmit = B_TRUE; 17313 tcp->tcp_dupack_cnt = 0; 17314 17315 /* 17316 * Remove all rexmit SACK blk to start from fresh. 17317 */ 17318 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17319 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17320 tcp->tcp_num_notsack_blk = 0; 17321 tcp->tcp_cnt_notsack_list = 0; 17322 } 17323 if (mp == NULL) { 17324 return; 17325 } 17326 /* 17327 * Attach credentials to retransmitted initial SYNs. 17328 * In theory we should use the credentials from the connect() 17329 * call to ensure that getpeerucred() on the peer will be correct. 17330 * But we assume that SYN's are not dropped for loopback connections. 17331 */ 17332 if (tcp->tcp_state == TCPS_SYN_SENT) { 17333 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 17334 } 17335 17336 tcp->tcp_csuna = tcp->tcp_snxt; 17337 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17338 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17339 tcp_send_data(tcp, tcp->tcp_wq, mp); 17340 17341 } 17342 17343 static int 17344 tcp_do_unbind(conn_t *connp) 17345 { 17346 tcp_t *tcp = connp->conn_tcp; 17347 int error = 0; 17348 17349 switch (tcp->tcp_state) { 17350 case TCPS_BOUND: 17351 case TCPS_LISTEN: 17352 break; 17353 default: 17354 return (-TOUTSTATE); 17355 } 17356 17357 /* 17358 * Need to clean up all the eagers since after the unbind, segments 17359 * will no longer be delivered to this listener stream. 17360 */ 17361 mutex_enter(&tcp->tcp_eager_lock); 17362 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17363 tcp_eager_cleanup(tcp, 0); 17364 } 17365 mutex_exit(&tcp->tcp_eager_lock); 17366 17367 if (tcp->tcp_ipversion == IPV4_VERSION) { 17368 tcp->tcp_ipha->ipha_src = 0; 17369 } else { 17370 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17371 } 17372 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17373 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17374 tcp_bind_hash_remove(tcp); 17375 tcp->tcp_state = TCPS_IDLE; 17376 tcp->tcp_mdt = B_FALSE; 17377 17378 connp = tcp->tcp_connp; 17379 connp->conn_mdt_ok = B_FALSE; 17380 ipcl_hash_remove(connp); 17381 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17382 17383 return (error); 17384 } 17385 17386 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17387 static void 17388 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 17389 { 17390 int error = tcp_do_unbind(tcp->tcp_connp); 17391 17392 if (error > 0) { 17393 tcp_err_ack(tcp, mp, TSYSERR, error); 17394 } else if (error < 0) { 17395 tcp_err_ack(tcp, mp, -error, 0); 17396 } else { 17397 /* Send M_FLUSH according to TPI */ 17398 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17399 17400 mp = mi_tpi_ok_ack_alloc(mp); 17401 putnext(tcp->tcp_rq, mp); 17402 } 17403 } 17404 17405 /* 17406 * Don't let port fall into the privileged range. 17407 * Since the extra privileged ports can be arbitrary we also 17408 * ensure that we exclude those from consideration. 17409 * tcp_g_epriv_ports is not sorted thus we loop over it until 17410 * there are no changes. 17411 * 17412 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17413 * but instead the code relies on: 17414 * - the fact that the address of the array and its size never changes 17415 * - the atomic assignment of the elements of the array 17416 * 17417 * Returns 0 if there are no more ports available. 17418 * 17419 * TS note: skip multilevel ports. 17420 */ 17421 static in_port_t 17422 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17423 { 17424 int i; 17425 boolean_t restart = B_FALSE; 17426 tcp_stack_t *tcps = tcp->tcp_tcps; 17427 17428 if (random && tcp_random_anon_port != 0) { 17429 (void) random_get_pseudo_bytes((uint8_t *)&port, 17430 sizeof (in_port_t)); 17431 /* 17432 * Unless changed by a sys admin, the smallest anon port 17433 * is 32768 and the largest anon port is 65535. It is 17434 * very likely (50%) for the random port to be smaller 17435 * than the smallest anon port. When that happens, 17436 * add port % (anon port range) to the smallest anon 17437 * port to get the random port. It should fall into the 17438 * valid anon port range. 17439 */ 17440 if (port < tcps->tcps_smallest_anon_port) { 17441 port = tcps->tcps_smallest_anon_port + 17442 port % (tcps->tcps_largest_anon_port - 17443 tcps->tcps_smallest_anon_port); 17444 } 17445 } 17446 17447 retry: 17448 if (port < tcps->tcps_smallest_anon_port) 17449 port = (in_port_t)tcps->tcps_smallest_anon_port; 17450 17451 if (port > tcps->tcps_largest_anon_port) { 17452 if (restart) 17453 return (0); 17454 restart = B_TRUE; 17455 port = (in_port_t)tcps->tcps_smallest_anon_port; 17456 } 17457 17458 if (port < tcps->tcps_smallest_nonpriv_port) 17459 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17460 17461 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17462 if (port == tcps->tcps_g_epriv_ports[i]) { 17463 port++; 17464 /* 17465 * Make sure whether the port is in the 17466 * valid range. 17467 */ 17468 goto retry; 17469 } 17470 } 17471 if (is_system_labeled() && 17472 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17473 IPPROTO_TCP, B_TRUE)) != 0) { 17474 port = i; 17475 goto retry; 17476 } 17477 return (port); 17478 } 17479 17480 /* 17481 * Return the next anonymous port in the privileged port range for 17482 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17483 * downwards. This is the same behavior as documented in the userland 17484 * library call rresvport(3N). 17485 * 17486 * TS note: skip multilevel ports. 17487 */ 17488 static in_port_t 17489 tcp_get_next_priv_port(const tcp_t *tcp) 17490 { 17491 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17492 in_port_t nextport; 17493 boolean_t restart = B_FALSE; 17494 tcp_stack_t *tcps = tcp->tcp_tcps; 17495 retry: 17496 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17497 next_priv_port >= IPPORT_RESERVED) { 17498 next_priv_port = IPPORT_RESERVED - 1; 17499 if (restart) 17500 return (0); 17501 restart = B_TRUE; 17502 } 17503 if (is_system_labeled() && 17504 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17505 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17506 next_priv_port = nextport; 17507 goto retry; 17508 } 17509 return (next_priv_port--); 17510 } 17511 17512 /* The write side r/w procedure. */ 17513 17514 #if CCS_STATS 17515 struct { 17516 struct { 17517 int64_t count, bytes; 17518 } tot, hit; 17519 } wrw_stats; 17520 #endif 17521 17522 /* 17523 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17524 * messages. 17525 */ 17526 /* ARGSUSED */ 17527 static void 17528 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17529 { 17530 conn_t *connp = (conn_t *)arg; 17531 tcp_t *tcp = connp->conn_tcp; 17532 queue_t *q = tcp->tcp_wq; 17533 17534 ASSERT(DB_TYPE(mp) != M_IOCTL); 17535 /* 17536 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17537 * Once the close starts, streamhead and sockfs will not let any data 17538 * packets come down (close ensures that there are no threads using the 17539 * queue and no new threads will come down) but since qprocsoff() 17540 * hasn't happened yet, a M_FLUSH or some non data message might 17541 * get reflected back (in response to our own FLUSHRW) and get 17542 * processed after tcp_close() is done. The conn would still be valid 17543 * because a ref would have added but we need to check the state 17544 * before actually processing the packet. 17545 */ 17546 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17547 freemsg(mp); 17548 return; 17549 } 17550 17551 switch (DB_TYPE(mp)) { 17552 case M_IOCDATA: 17553 tcp_wput_iocdata(tcp, mp); 17554 break; 17555 case M_FLUSH: 17556 tcp_wput_flush(tcp, mp); 17557 break; 17558 default: 17559 CALL_IP_WPUT(connp, q, mp); 17560 break; 17561 } 17562 } 17563 17564 /* 17565 * The TCP fast path write put procedure. 17566 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17567 */ 17568 /* ARGSUSED */ 17569 void 17570 tcp_output(void *arg, mblk_t *mp, void *arg2) 17571 { 17572 int len; 17573 int hdrlen; 17574 int plen; 17575 mblk_t *mp1; 17576 uchar_t *rptr; 17577 uint32_t snxt; 17578 tcph_t *tcph; 17579 struct datab *db; 17580 uint32_t suna; 17581 uint32_t mss; 17582 ipaddr_t *dst; 17583 ipaddr_t *src; 17584 uint32_t sum; 17585 int usable; 17586 conn_t *connp = (conn_t *)arg; 17587 tcp_t *tcp = connp->conn_tcp; 17588 uint32_t msize; 17589 tcp_stack_t *tcps = tcp->tcp_tcps; 17590 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17591 17592 /* 17593 * Try and ASSERT the minimum possible references on the 17594 * conn early enough. Since we are executing on write side, 17595 * the connection is obviously not detached and that means 17596 * there is a ref each for TCP and IP. Since we are behind 17597 * the squeue, the minimum references needed are 3. If the 17598 * conn is in classifier hash list, there should be an 17599 * extra ref for that (we check both the possibilities). 17600 */ 17601 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17602 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17603 17604 ASSERT(DB_TYPE(mp) == M_DATA); 17605 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17606 17607 mutex_enter(&tcp->tcp_non_sq_lock); 17608 tcp->tcp_squeue_bytes -= msize; 17609 mutex_exit(&tcp->tcp_non_sq_lock); 17610 17611 /* Check to see if this connection wants to be re-fused. */ 17612 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17613 if (tcp->tcp_ipversion == IPV4_VERSION) { 17614 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17615 &tcp->tcp_saved_tcph); 17616 } else { 17617 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17618 &tcp->tcp_saved_tcph); 17619 } 17620 } 17621 /* Bypass tcp protocol for fused tcp loopback */ 17622 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17623 return; 17624 17625 mss = tcp->tcp_mss; 17626 if (tcp->tcp_xmit_zc_clean) 17627 mp = tcp_zcopy_backoff(tcp, mp, 0); 17628 17629 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17630 len = (int)(mp->b_wptr - mp->b_rptr); 17631 17632 /* 17633 * Criteria for fast path: 17634 * 17635 * 1. no unsent data 17636 * 2. single mblk in request 17637 * 3. connection established 17638 * 4. data in mblk 17639 * 5. len <= mss 17640 * 6. no tcp_valid bits 17641 */ 17642 if ((tcp->tcp_unsent != 0) || 17643 (tcp->tcp_cork) || 17644 (mp->b_cont != NULL) || 17645 (tcp->tcp_state != TCPS_ESTABLISHED) || 17646 (len == 0) || 17647 (len > mss) || 17648 (tcp->tcp_valid_bits != 0)) { 17649 tcp_wput_data(tcp, mp, B_FALSE); 17650 return; 17651 } 17652 17653 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17654 ASSERT(tcp->tcp_fin_sent == 0); 17655 17656 /* queue new packet onto retransmission queue */ 17657 if (tcp->tcp_xmit_head == NULL) { 17658 tcp->tcp_xmit_head = mp; 17659 } else { 17660 tcp->tcp_xmit_last->b_cont = mp; 17661 } 17662 tcp->tcp_xmit_last = mp; 17663 tcp->tcp_xmit_tail = mp; 17664 17665 /* find out how much we can send */ 17666 /* BEGIN CSTYLED */ 17667 /* 17668 * un-acked usable 17669 * |--------------|-----------------| 17670 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17671 */ 17672 /* END CSTYLED */ 17673 17674 /* start sending from tcp_snxt */ 17675 snxt = tcp->tcp_snxt; 17676 17677 /* 17678 * Check to see if this connection has been idled for some 17679 * time and no ACK is expected. If it is, we need to slow 17680 * start again to get back the connection's "self-clock" as 17681 * described in VJ's paper. 17682 * 17683 * Refer to the comment in tcp_mss_set() for the calculation 17684 * of tcp_cwnd after idle. 17685 */ 17686 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17687 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17688 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17689 } 17690 17691 usable = tcp->tcp_swnd; /* tcp window size */ 17692 if (usable > tcp->tcp_cwnd) 17693 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17694 usable -= snxt; /* subtract stuff already sent */ 17695 suna = tcp->tcp_suna; 17696 usable += suna; 17697 /* usable can be < 0 if the congestion window is smaller */ 17698 if (len > usable) { 17699 /* Can't send complete M_DATA in one shot */ 17700 goto slow; 17701 } 17702 17703 mutex_enter(&tcp->tcp_non_sq_lock); 17704 if (tcp->tcp_flow_stopped && 17705 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17706 tcp_clrqfull(tcp); 17707 } 17708 mutex_exit(&tcp->tcp_non_sq_lock); 17709 17710 /* 17711 * determine if anything to send (Nagle). 17712 * 17713 * 1. len < tcp_mss (i.e. small) 17714 * 2. unacknowledged data present 17715 * 3. len < nagle limit 17716 * 4. last packet sent < nagle limit (previous packet sent) 17717 */ 17718 if ((len < mss) && (snxt != suna) && 17719 (len < (int)tcp->tcp_naglim) && 17720 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17721 /* 17722 * This was the first unsent packet and normally 17723 * mss < xmit_hiwater so there is no need to worry 17724 * about flow control. The next packet will go 17725 * through the flow control check in tcp_wput_data(). 17726 */ 17727 /* leftover work from above */ 17728 tcp->tcp_unsent = len; 17729 tcp->tcp_xmit_tail_unsent = len; 17730 17731 return; 17732 } 17733 17734 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17735 17736 if (snxt == suna) { 17737 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17738 } 17739 17740 /* we have always sent something */ 17741 tcp->tcp_rack_cnt = 0; 17742 17743 tcp->tcp_snxt = snxt + len; 17744 tcp->tcp_rack = tcp->tcp_rnxt; 17745 17746 if ((mp1 = dupb(mp)) == 0) 17747 goto no_memory; 17748 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17749 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17750 17751 /* adjust tcp header information */ 17752 tcph = tcp->tcp_tcph; 17753 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17754 17755 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17756 sum = (sum >> 16) + (sum & 0xFFFF); 17757 U16_TO_ABE16(sum, tcph->th_sum); 17758 17759 U32_TO_ABE32(snxt, tcph->th_seq); 17760 17761 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17762 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17763 BUMP_LOCAL(tcp->tcp_obsegs); 17764 17765 /* Update the latest receive window size in TCP header. */ 17766 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17767 tcph->th_win); 17768 17769 tcp->tcp_last_sent_len = (ushort_t)len; 17770 17771 plen = len + tcp->tcp_hdr_len; 17772 17773 if (tcp->tcp_ipversion == IPV4_VERSION) { 17774 tcp->tcp_ipha->ipha_length = htons(plen); 17775 } else { 17776 tcp->tcp_ip6h->ip6_plen = htons(plen - 17777 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17778 } 17779 17780 /* see if we need to allocate a mblk for the headers */ 17781 hdrlen = tcp->tcp_hdr_len; 17782 rptr = mp1->b_rptr - hdrlen; 17783 db = mp1->b_datap; 17784 if ((db->db_ref != 2) || rptr < db->db_base || 17785 (!OK_32PTR(rptr))) { 17786 /* NOTE: we assume allocb returns an OK_32PTR */ 17787 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17788 tcps->tcps_wroff_xtra, BPRI_MED); 17789 if (!mp) { 17790 freemsg(mp1); 17791 goto no_memory; 17792 } 17793 mp->b_cont = mp1; 17794 mp1 = mp; 17795 /* Leave room for Link Level header */ 17796 /* hdrlen = tcp->tcp_hdr_len; */ 17797 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17798 mp1->b_wptr = &rptr[hdrlen]; 17799 } 17800 mp1->b_rptr = rptr; 17801 17802 /* Fill in the timestamp option. */ 17803 if (tcp->tcp_snd_ts_ok) { 17804 U32_TO_BE32((uint32_t)lbolt, 17805 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17806 U32_TO_BE32(tcp->tcp_ts_recent, 17807 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17808 } else { 17809 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17810 } 17811 17812 /* copy header into outgoing packet */ 17813 dst = (ipaddr_t *)rptr; 17814 src = (ipaddr_t *)tcp->tcp_iphc; 17815 dst[0] = src[0]; 17816 dst[1] = src[1]; 17817 dst[2] = src[2]; 17818 dst[3] = src[3]; 17819 dst[4] = src[4]; 17820 dst[5] = src[5]; 17821 dst[6] = src[6]; 17822 dst[7] = src[7]; 17823 dst[8] = src[8]; 17824 dst[9] = src[9]; 17825 if (hdrlen -= 40) { 17826 hdrlen >>= 2; 17827 dst += 10; 17828 src += 10; 17829 do { 17830 *dst++ = *src++; 17831 } while (--hdrlen); 17832 } 17833 17834 /* 17835 * Set the ECN info in the TCP header. Note that this 17836 * is not the template header. 17837 */ 17838 if (tcp->tcp_ecn_ok) { 17839 SET_ECT(tcp, rptr); 17840 17841 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17842 if (tcp->tcp_ecn_echo_on) 17843 tcph->th_flags[0] |= TH_ECE; 17844 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17845 tcph->th_flags[0] |= TH_CWR; 17846 tcp->tcp_ecn_cwr_sent = B_TRUE; 17847 } 17848 } 17849 17850 if (tcp->tcp_ip_forward_progress) { 17851 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17852 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17853 tcp->tcp_ip_forward_progress = B_FALSE; 17854 } 17855 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17856 return; 17857 17858 /* 17859 * If we ran out of memory, we pretend to have sent the packet 17860 * and that it was lost on the wire. 17861 */ 17862 no_memory: 17863 return; 17864 17865 slow: 17866 /* leftover work from above */ 17867 tcp->tcp_unsent = len; 17868 tcp->tcp_xmit_tail_unsent = len; 17869 tcp_wput_data(tcp, NULL, B_FALSE); 17870 } 17871 17872 /* ARGSUSED */ 17873 void 17874 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17875 { 17876 conn_t *connp = (conn_t *)arg; 17877 tcp_t *tcp = connp->conn_tcp; 17878 queue_t *q = tcp->tcp_rq; 17879 struct tcp_options *tcpopt; 17880 tcp_stack_t *tcps = tcp->tcp_tcps; 17881 17882 /* socket options */ 17883 uint_t sopp_flags; 17884 ssize_t sopp_rxhiwat; 17885 ssize_t sopp_maxblk; 17886 ushort_t sopp_wroff; 17887 ushort_t sopp_tail; 17888 ushort_t sopp_copyopt; 17889 17890 tcpopt = (struct tcp_options *)mp->b_rptr; 17891 17892 /* 17893 * Drop the eager's ref on the listener, that was placed when 17894 * this eager began life in tcp_conn_request. 17895 */ 17896 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17897 if (IPCL_IS_NONSTR(connp)) { 17898 /* Safe to free conn_ind message */ 17899 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17900 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17901 } 17902 17903 tcp->tcp_detached = B_FALSE; 17904 17905 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17906 /* 17907 * Someone blewoff the eager before we could finish 17908 * the accept. 17909 * 17910 * The only reason eager exists it because we put in 17911 * a ref on it when conn ind went up. We need to send 17912 * a disconnect indication up while the last reference 17913 * on the eager will be dropped by the squeue when we 17914 * return. 17915 */ 17916 ASSERT(tcp->tcp_listener == NULL); 17917 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17918 if (IPCL_IS_NONSTR(connp)) { 17919 ASSERT(tcp->tcp_issocket); 17920 (*connp->conn_upcalls->su_disconnected)( 17921 connp->conn_upper_handle, tcp->tcp_connid, 17922 ECONNREFUSED); 17923 freemsg(mp); 17924 } else { 17925 struct T_discon_ind *tdi; 17926 17927 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17928 /* 17929 * Let us reuse the incoming mblk to avoid 17930 * memory allocation failure problems. We know 17931 * that the size of the incoming mblk i.e. 17932 * stroptions is greater than sizeof 17933 * T_discon_ind. So the reallocb below can't 17934 * fail. 17935 */ 17936 freemsg(mp->b_cont); 17937 mp->b_cont = NULL; 17938 ASSERT(DB_REF(mp) == 1); 17939 mp = reallocb(mp, sizeof (struct T_discon_ind), 17940 B_FALSE); 17941 ASSERT(mp != NULL); 17942 DB_TYPE(mp) = M_PROTO; 17943 ((union T_primitives *)mp->b_rptr)->type = 17944 T_DISCON_IND; 17945 tdi = (struct T_discon_ind *)mp->b_rptr; 17946 if (tcp->tcp_issocket) { 17947 tdi->DISCON_reason = ECONNREFUSED; 17948 tdi->SEQ_number = 0; 17949 } else { 17950 tdi->DISCON_reason = ENOPROTOOPT; 17951 tdi->SEQ_number = 17952 tcp->tcp_conn_req_seqnum; 17953 } 17954 mp->b_wptr = mp->b_rptr + 17955 sizeof (struct T_discon_ind); 17956 putnext(q, mp); 17957 return; 17958 } 17959 } 17960 if (tcp->tcp_hard_binding) { 17961 tcp->tcp_hard_binding = B_FALSE; 17962 tcp->tcp_hard_bound = B_TRUE; 17963 } 17964 return; 17965 } 17966 17967 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17968 int boundif = tcpopt->to_boundif; 17969 uint_t len = sizeof (int); 17970 17971 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17972 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17973 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17974 } 17975 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17976 uint_t on = 1; 17977 uint_t len = sizeof (uint_t); 17978 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17979 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17980 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17981 } 17982 17983 /* 17984 * For a loopback connection with tcp_direct_sockfs on, note that 17985 * we don't have to protect tcp_rcv_list yet because synchronous 17986 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17987 * possibly race with us. 17988 */ 17989 17990 /* 17991 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17992 * properly. This is the first time we know of the acceptor' 17993 * queue. So we do it here. 17994 * 17995 * XXX 17996 */ 17997 if (tcp->tcp_rcv_list == NULL) { 17998 /* 17999 * Recv queue is empty, tcp_rwnd should not have changed. 18000 * That means it should be equal to the listener's tcp_rwnd. 18001 */ 18002 if (!IPCL_IS_NONSTR(connp)) 18003 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18004 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 18005 } else { 18006 #ifdef DEBUG 18007 mblk_t *tmp; 18008 mblk_t *mp1; 18009 uint_t cnt = 0; 18010 18011 mp1 = tcp->tcp_rcv_list; 18012 while ((tmp = mp1) != NULL) { 18013 mp1 = tmp->b_next; 18014 cnt += msgdsize(tmp); 18015 } 18016 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18017 #endif 18018 /* There is some data, add them back to get the max. */ 18019 if (!IPCL_IS_NONSTR(connp)) 18020 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18021 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18022 } 18023 /* 18024 * This is the first time we run on the correct 18025 * queue after tcp_accept. So fix all the q parameters 18026 * here. 18027 */ 18028 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 18029 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18030 18031 /* 18032 * Record the stream head's high water mark for this endpoint; 18033 * this is used for flow-control purposes. 18034 */ 18035 sopp_rxhiwat = tcp->tcp_fused ? 18036 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 18037 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 18038 18039 /* 18040 * Determine what write offset value to use depending on SACK and 18041 * whether the endpoint is fused or not. 18042 */ 18043 if (tcp->tcp_fused) { 18044 ASSERT(tcp->tcp_loopback); 18045 ASSERT(tcp->tcp_loopback_peer != NULL); 18046 /* 18047 * For fused tcp loopback, set the stream head's write 18048 * offset value to zero since we won't be needing any room 18049 * for TCP/IP headers. This would also improve performance 18050 * since it would reduce the amount of work done by kmem. 18051 * Non-fused tcp loopback case is handled separately below. 18052 */ 18053 sopp_wroff = 0; 18054 /* 18055 * Update the peer's transmit parameters according to 18056 * our recently calculated high water mark value. 18057 */ 18058 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18059 } else if (tcp->tcp_snd_sack_ok) { 18060 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18061 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18062 } else { 18063 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18064 tcps->tcps_wroff_xtra); 18065 } 18066 18067 /* 18068 * If this is endpoint is handling SSL, then reserve extra 18069 * offset and space at the end. 18070 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18071 * overriding the previous setting. The extra cost of signing and 18072 * encrypting multiple MSS-size records (12 of them with Ethernet), 18073 * instead of a single contiguous one by the stream head 18074 * largely outweighs the statistical reduction of ACKs, when 18075 * applicable. The peer will also save on decryption and verification 18076 * costs. 18077 */ 18078 if (tcp->tcp_kssl_ctx != NULL) { 18079 sopp_wroff += SSL3_WROFFSET; 18080 18081 sopp_flags |= SOCKOPT_TAIL; 18082 sopp_tail = SSL3_MAX_TAIL_LEN; 18083 18084 sopp_flags |= SOCKOPT_ZCOPY; 18085 sopp_copyopt = ZCVMUNSAFE; 18086 18087 sopp_maxblk = SSL3_MAX_RECORD_LEN; 18088 } 18089 18090 /* Send the options up */ 18091 if (IPCL_IS_NONSTR(connp)) { 18092 struct sock_proto_props sopp; 18093 18094 sopp.sopp_flags = sopp_flags; 18095 sopp.sopp_wroff = sopp_wroff; 18096 sopp.sopp_maxblk = sopp_maxblk; 18097 sopp.sopp_rxhiwat = sopp_rxhiwat; 18098 if (sopp_flags & SOCKOPT_TAIL) { 18099 ASSERT(tcp->tcp_kssl_ctx != NULL); 18100 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 18101 sopp.sopp_tail = sopp_tail; 18102 sopp.sopp_zcopyflag = sopp_copyopt; 18103 } 18104 (*connp->conn_upcalls->su_set_proto_props) 18105 (connp->conn_upper_handle, &sopp); 18106 } else { 18107 struct stroptions *stropt; 18108 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18109 if (stropt_mp == NULL) { 18110 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 18111 return; 18112 } 18113 DB_TYPE(stropt_mp) = M_SETOPTS; 18114 stropt = (struct stroptions *)stropt_mp->b_rptr; 18115 stropt_mp->b_wptr += sizeof (struct stroptions); 18116 stropt = (struct stroptions *)stropt_mp->b_rptr; 18117 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 18118 stropt->so_hiwat = sopp_rxhiwat; 18119 stropt->so_wroff = sopp_wroff; 18120 stropt->so_maxblk = sopp_maxblk; 18121 18122 if (sopp_flags & SOCKOPT_TAIL) { 18123 ASSERT(tcp->tcp_kssl_ctx != NULL); 18124 18125 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 18126 stropt->so_tail = sopp_tail; 18127 stropt->so_copyopt = sopp_copyopt; 18128 } 18129 18130 /* Send the options up */ 18131 putnext(q, stropt_mp); 18132 } 18133 18134 freemsg(mp); 18135 /* 18136 * Pass up any data and/or a fin that has been received. 18137 * 18138 * Adjust receive window in case it had decreased 18139 * (because there is data <=> tcp_rcv_list != NULL) 18140 * while the connection was detached. Note that 18141 * in case the eager was flow-controlled, w/o this 18142 * code, the rwnd may never open up again! 18143 */ 18144 if (tcp->tcp_rcv_list != NULL) { 18145 if (IPCL_IS_NONSTR(connp)) { 18146 mblk_t *mp; 18147 int space_left; 18148 int error; 18149 boolean_t push = B_TRUE; 18150 18151 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 18152 (connp->conn_upper_handle, NULL, 0, 0, &error, 18153 &push) >= 0) { 18154 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 18155 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18156 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18157 tcp_xmit_ctl(NULL, 18158 tcp, (tcp->tcp_swnd == 0) ? 18159 tcp->tcp_suna : tcp->tcp_snxt, 18160 tcp->tcp_rnxt, TH_ACK); 18161 } 18162 } 18163 while ((mp = tcp->tcp_rcv_list) != NULL) { 18164 push = B_TRUE; 18165 tcp->tcp_rcv_list = mp->b_next; 18166 mp->b_next = NULL; 18167 space_left = (*connp->conn_upcalls->su_recv) 18168 (connp->conn_upper_handle, mp, msgdsize(mp), 18169 0, &error, &push); 18170 if (space_left < 0) { 18171 /* 18172 * At this point the eager is not 18173 * visible to anyone, so fallback 18174 * can not happen. 18175 */ 18176 ASSERT(error != EOPNOTSUPP); 18177 } 18178 } 18179 tcp->tcp_rcv_last_head = NULL; 18180 tcp->tcp_rcv_last_tail = NULL; 18181 tcp->tcp_rcv_cnt = 0; 18182 } else { 18183 /* We drain directly in case of fused tcp loopback */ 18184 sodirect_t *sodp; 18185 18186 if (!tcp->tcp_fused && canputnext(q)) { 18187 tcp->tcp_rwnd = q->q_hiwat; 18188 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18189 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18190 tcp_xmit_ctl(NULL, 18191 tcp, (tcp->tcp_swnd == 0) ? 18192 tcp->tcp_suna : tcp->tcp_snxt, 18193 tcp->tcp_rnxt, TH_ACK); 18194 } 18195 } 18196 18197 SOD_PTR_ENTER(tcp, sodp); 18198 if (sodp != NULL) { 18199 /* Sodirect, move from rcv_list */ 18200 ASSERT(!tcp->tcp_fused); 18201 while ((mp = tcp->tcp_rcv_list) != NULL) { 18202 tcp->tcp_rcv_list = mp->b_next; 18203 mp->b_next = NULL; 18204 (void) tcp_rcv_sod_enqueue(tcp, sodp, 18205 mp, msgdsize(mp)); 18206 } 18207 tcp->tcp_rcv_last_head = NULL; 18208 tcp->tcp_rcv_last_tail = NULL; 18209 tcp->tcp_rcv_cnt = 0; 18210 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18211 /* sod_wakeup() did the mutex_exit() */ 18212 } else { 18213 /* Not sodirect, drain */ 18214 (void) tcp_rcv_drain(tcp); 18215 } 18216 } 18217 18218 /* 18219 * For fused tcp loopback, back-enable peer endpoint 18220 * if it's currently flow-controlled. 18221 */ 18222 if (tcp->tcp_fused) { 18223 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18224 18225 ASSERT(peer_tcp != NULL); 18226 ASSERT(peer_tcp->tcp_fused); 18227 /* 18228 * In order to change the peer's tcp_flow_stopped, 18229 * we need to take locks for both end points. The 18230 * highest address is taken first. 18231 */ 18232 if (peer_tcp > tcp) { 18233 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18234 mutex_enter(&tcp->tcp_non_sq_lock); 18235 } else { 18236 mutex_enter(&tcp->tcp_non_sq_lock); 18237 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18238 } 18239 if (peer_tcp->tcp_flow_stopped) { 18240 tcp_clrqfull(peer_tcp); 18241 TCP_STAT(tcps, tcp_fusion_backenabled); 18242 } 18243 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18244 mutex_exit(&tcp->tcp_non_sq_lock); 18245 } 18246 } 18247 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18248 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18249 tcp->tcp_ordrel_done = B_TRUE; 18250 if (IPCL_IS_NONSTR(connp)) { 18251 ASSERT(tcp->tcp_ordrel_mp == NULL); 18252 (*connp->conn_upcalls->su_opctl)( 18253 connp->conn_upper_handle, 18254 SOCK_OPCTL_SHUT_RECV, 0); 18255 } else { 18256 mp = tcp->tcp_ordrel_mp; 18257 tcp->tcp_ordrel_mp = NULL; 18258 putnext(q, mp); 18259 } 18260 } 18261 if (tcp->tcp_hard_binding) { 18262 tcp->tcp_hard_binding = B_FALSE; 18263 tcp->tcp_hard_bound = B_TRUE; 18264 } 18265 18266 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 18267 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 18268 tcp->tcp_loopback_peer != NULL && 18269 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 18270 tcp_fuse_syncstr_enable_pair(tcp); 18271 } 18272 18273 if (tcp->tcp_ka_enabled) { 18274 tcp->tcp_ka_last_intrvl = 0; 18275 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18276 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18277 } 18278 18279 /* 18280 * At this point, eager is fully established and will 18281 * have the following references - 18282 * 18283 * 2 references for connection to exist (1 for TCP and 1 for IP). 18284 * 1 reference for the squeue which will be dropped by the squeue as 18285 * soon as this function returns. 18286 * There will be 1 additonal reference for being in classifier 18287 * hash list provided something bad hasn't happened. 18288 */ 18289 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18290 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18291 } 18292 18293 /* 18294 * The function called through squeue to get behind listener's perimeter to 18295 * send a deffered conn_ind. 18296 */ 18297 /* ARGSUSED */ 18298 void 18299 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18300 { 18301 conn_t *connp = (conn_t *)arg; 18302 tcp_t *listener = connp->conn_tcp; 18303 struct T_conn_ind *conn_ind; 18304 tcp_t *tcp; 18305 18306 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18307 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18308 conn_ind->OPT_length); 18309 18310 if (listener->tcp_state == TCPS_CLOSED || 18311 TCP_IS_DETACHED(listener)) { 18312 /* 18313 * If listener has closed, it would have caused a 18314 * a cleanup/blowoff to happen for the eager. 18315 * 18316 * We need to drop the ref on eager that was put 18317 * tcp_rput_data() before trying to send the conn_ind 18318 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18319 * and tcp_wput_accept() is sending this deferred conn_ind but 18320 * listener is closed so we drop the ref. 18321 */ 18322 CONN_DEC_REF(tcp->tcp_connp); 18323 freemsg(mp); 18324 return; 18325 } 18326 18327 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 18328 } 18329 18330 /* ARGSUSED */ 18331 static int 18332 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 18333 { 18334 tcp_t *listener, *eager; 18335 mblk_t *opt_mp; 18336 struct tcp_options *tcpopt; 18337 18338 listener = lconnp->conn_tcp; 18339 ASSERT(listener->tcp_state == TCPS_LISTEN); 18340 eager = econnp->conn_tcp; 18341 ASSERT(eager->tcp_listener != NULL); 18342 18343 ASSERT(eager->tcp_rq != NULL); 18344 18345 /* If tcp_fused and sodirect enabled disable it */ 18346 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18347 /* Fused, disable sodirect */ 18348 mutex_enter(eager->tcp_sodirect->sod_lockp); 18349 SOD_DISABLE(eager->tcp_sodirect); 18350 mutex_exit(eager->tcp_sodirect->sod_lockp); 18351 eager->tcp_sodirect = NULL; 18352 } 18353 18354 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 18355 if (opt_mp == NULL) { 18356 return (-TPROTO); 18357 } 18358 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 18359 eager->tcp_issocket = B_TRUE; 18360 18361 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18362 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18363 ASSERT(econnp->conn_netstack == 18364 listener->tcp_connp->conn_netstack); 18365 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18366 18367 /* Put the ref for IP */ 18368 CONN_INC_REF(econnp); 18369 18370 /* 18371 * We should have minimum of 3 references on the conn 18372 * at this point. One each for TCP and IP and one for 18373 * the T_conn_ind that was sent up when the 3-way handshake 18374 * completed. In the normal case we would also have another 18375 * reference (making a total of 4) for the conn being in the 18376 * classifier hash list. However the eager could have received 18377 * an RST subsequently and tcp_closei_local could have removed 18378 * the eager from the classifier hash list, hence we can't 18379 * assert that reference. 18380 */ 18381 ASSERT(econnp->conn_ref >= 3); 18382 18383 opt_mp->b_datap->db_type = M_SETOPTS; 18384 opt_mp->b_wptr += sizeof (struct tcp_options); 18385 18386 /* 18387 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18388 * from listener to acceptor. 18389 */ 18390 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 18391 tcpopt->to_flags = 0; 18392 18393 if (listener->tcp_bound_if != 0) { 18394 tcpopt->to_flags |= TCPOPT_BOUNDIF; 18395 tcpopt->to_boundif = listener->tcp_bound_if; 18396 } 18397 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18398 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 18399 } 18400 18401 mutex_enter(&listener->tcp_eager_lock); 18402 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18403 18404 tcp_t *tail; 18405 tcp_t *tcp; 18406 mblk_t *mp1; 18407 18408 tcp = listener->tcp_eager_prev_q0; 18409 /* 18410 * listener->tcp_eager_prev_q0 points to the TAIL of the 18411 * deferred T_conn_ind queue. We need to get to the head 18412 * of the queue in order to send up T_conn_ind the same 18413 * order as how the 3WHS is completed. 18414 */ 18415 while (tcp != listener) { 18416 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18417 !tcp->tcp_kssl_pending) 18418 break; 18419 else 18420 tcp = tcp->tcp_eager_prev_q0; 18421 } 18422 /* None of the pending eagers can be sent up now */ 18423 if (tcp == listener) 18424 goto no_more_eagers; 18425 18426 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18427 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18428 /* Move from q0 to q */ 18429 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18430 listener->tcp_conn_req_cnt_q0--; 18431 listener->tcp_conn_req_cnt_q++; 18432 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18433 tcp->tcp_eager_prev_q0; 18434 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18435 tcp->tcp_eager_next_q0; 18436 tcp->tcp_eager_prev_q0 = NULL; 18437 tcp->tcp_eager_next_q0 = NULL; 18438 tcp->tcp_conn_def_q0 = B_FALSE; 18439 18440 /* Make sure the tcp isn't in the list of droppables */ 18441 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18442 tcp->tcp_eager_prev_drop_q0 == NULL); 18443 18444 /* 18445 * Insert at end of the queue because sockfs sends 18446 * down T_CONN_RES in chronological order. Leaving 18447 * the older conn indications at front of the queue 18448 * helps reducing search time. 18449 */ 18450 tail = listener->tcp_eager_last_q; 18451 if (tail != NULL) { 18452 tail->tcp_eager_next_q = tcp; 18453 } else { 18454 listener->tcp_eager_next_q = tcp; 18455 } 18456 listener->tcp_eager_last_q = tcp; 18457 tcp->tcp_eager_next_q = NULL; 18458 18459 /* Need to get inside the listener perimeter */ 18460 CONN_INC_REF(listener->tcp_connp); 18461 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18462 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18463 SQTAG_TCP_SEND_PENDING); 18464 } 18465 no_more_eagers: 18466 tcp_eager_unlink(eager); 18467 mutex_exit(&listener->tcp_eager_lock); 18468 18469 /* 18470 * At this point, the eager is detached from the listener 18471 * but we still have an extra refs on eager (apart from the 18472 * usual tcp references). The ref was placed in tcp_rput_data 18473 * before sending the conn_ind in tcp_send_conn_ind. 18474 * The ref will be dropped in tcp_accept_finish(). 18475 */ 18476 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18477 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18478 return (0); 18479 } 18480 18481 int 18482 tcp_accept(sock_lower_handle_t lproto_handle, 18483 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18484 cred_t *cr) 18485 { 18486 conn_t *lconnp, *econnp; 18487 tcp_t *listener, *eager; 18488 tcp_stack_t *tcps; 18489 18490 lconnp = (conn_t *)lproto_handle; 18491 listener = lconnp->conn_tcp; 18492 ASSERT(listener->tcp_state == TCPS_LISTEN); 18493 econnp = (conn_t *)eproto_handle; 18494 eager = econnp->conn_tcp; 18495 ASSERT(eager->tcp_listener != NULL); 18496 tcps = eager->tcp_tcps; 18497 18498 /* 18499 * It is OK to manipulate these fields outside the eager's squeue 18500 * because they will not start being used until tcp_accept_finish 18501 * has been called. 18502 */ 18503 ASSERT(lconnp->conn_upper_handle != NULL); 18504 ASSERT(econnp->conn_upper_handle == NULL); 18505 econnp->conn_upper_handle = sock_handle; 18506 econnp->conn_upcalls = lconnp->conn_upcalls; 18507 ASSERT(IPCL_IS_NONSTR(econnp)); 18508 /* 18509 * Create helper stream if it is a non-TPI TCP connection. 18510 */ 18511 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18512 ip1dbg(("tcp_accept: create of IP helper stream" 18513 " failed\n")); 18514 return (EPROTO); 18515 } 18516 eager->tcp_rq = econnp->conn_rq; 18517 eager->tcp_wq = econnp->conn_wq; 18518 18519 ASSERT(eager->tcp_rq != NULL); 18520 18521 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18522 return (tcp_accept_common(lconnp, econnp, cr)); 18523 } 18524 18525 18526 /* 18527 * This is the STREAMS entry point for T_CONN_RES coming down on 18528 * Acceptor STREAM when sockfs listener does accept processing. 18529 * Read the block comment on top of tcp_conn_request(). 18530 */ 18531 void 18532 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18533 { 18534 queue_t *rq = RD(q); 18535 struct T_conn_res *conn_res; 18536 tcp_t *eager; 18537 tcp_t *listener; 18538 struct T_ok_ack *ok; 18539 t_scalar_t PRIM_type; 18540 conn_t *econnp; 18541 cred_t *cr; 18542 18543 ASSERT(DB_TYPE(mp) == M_PROTO); 18544 18545 /* 18546 * All Solaris components should pass a db_credp 18547 * for this TPI message, hence we ASSERT. 18548 * But in case there is some other M_PROTO that looks 18549 * like a TPI message sent by some other kernel 18550 * component, we check and return an error. 18551 */ 18552 cr = msg_getcred(mp, NULL); 18553 ASSERT(cr != NULL); 18554 if (cr == NULL) { 18555 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 18556 if (mp != NULL) 18557 putnext(rq, mp); 18558 return; 18559 } 18560 conn_res = (struct T_conn_res *)mp->b_rptr; 18561 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18562 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18563 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18564 if (mp != NULL) 18565 putnext(rq, mp); 18566 return; 18567 } 18568 switch (conn_res->PRIM_type) { 18569 case O_T_CONN_RES: 18570 case T_CONN_RES: 18571 /* 18572 * We pass up an err ack if allocb fails. This will 18573 * cause sockfs to issue a T_DISCON_REQ which will cause 18574 * tcp_eager_blowoff to be called. sockfs will then call 18575 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18576 * we need to do the allocb up here because we have to 18577 * make sure rq->q_qinfo->qi_qclose still points to the 18578 * correct function (tcpclose_accept) in case allocb 18579 * fails. 18580 */ 18581 bcopy(mp->b_rptr + conn_res->OPT_offset, 18582 &eager, conn_res->OPT_length); 18583 PRIM_type = conn_res->PRIM_type; 18584 mp->b_datap->db_type = M_PCPROTO; 18585 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18586 ok = (struct T_ok_ack *)mp->b_rptr; 18587 ok->PRIM_type = T_OK_ACK; 18588 ok->CORRECT_prim = PRIM_type; 18589 econnp = eager->tcp_connp; 18590 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18591 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18592 eager->tcp_rq = rq; 18593 eager->tcp_wq = q; 18594 rq->q_ptr = econnp; 18595 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18596 q->q_ptr = econnp; 18597 q->q_qinfo = &tcp_winit; 18598 listener = eager->tcp_listener; 18599 18600 /* 18601 * TCP is _D_SODIRECT and sockfs is directly above so 18602 * save shared sodirect_t pointer (if any). 18603 */ 18604 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18605 if (tcp_accept_common(listener->tcp_connp, 18606 econnp, cr) < 0) { 18607 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18608 if (mp != NULL) 18609 putnext(rq, mp); 18610 return; 18611 } 18612 18613 /* 18614 * Send the new local address also up to sockfs. There 18615 * should already be enough space in the mp that came 18616 * down from soaccept(). 18617 */ 18618 if (eager->tcp_family == AF_INET) { 18619 sin_t *sin; 18620 18621 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18622 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18623 sin = (sin_t *)mp->b_wptr; 18624 mp->b_wptr += sizeof (sin_t); 18625 sin->sin_family = AF_INET; 18626 sin->sin_port = eager->tcp_lport; 18627 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18628 } else { 18629 sin6_t *sin6; 18630 18631 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18632 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18633 sin6 = (sin6_t *)mp->b_wptr; 18634 mp->b_wptr += sizeof (sin6_t); 18635 sin6->sin6_family = AF_INET6; 18636 sin6->sin6_port = eager->tcp_lport; 18637 if (eager->tcp_ipversion == IPV4_VERSION) { 18638 sin6->sin6_flowinfo = 0; 18639 IN6_IPADDR_TO_V4MAPPED( 18640 eager->tcp_ipha->ipha_src, 18641 &sin6->sin6_addr); 18642 } else { 18643 ASSERT(eager->tcp_ip6h != NULL); 18644 sin6->sin6_flowinfo = 18645 eager->tcp_ip6h->ip6_vcf & 18646 ~IPV6_VERS_AND_FLOW_MASK; 18647 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18648 } 18649 sin6->sin6_scope_id = 0; 18650 sin6->__sin6_src_id = 0; 18651 } 18652 18653 putnext(rq, mp); 18654 return; 18655 default: 18656 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18657 if (mp != NULL) 18658 putnext(rq, mp); 18659 return; 18660 } 18661 } 18662 18663 static int 18664 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18665 { 18666 sin_t *sin = (sin_t *)sa; 18667 sin6_t *sin6 = (sin6_t *)sa; 18668 18669 switch (tcp->tcp_family) { 18670 case AF_INET: 18671 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18672 18673 if (*salenp < sizeof (sin_t)) 18674 return (EINVAL); 18675 18676 *sin = sin_null; 18677 sin->sin_family = AF_INET; 18678 if (tcp->tcp_state >= TCPS_BOUND) { 18679 sin->sin_port = tcp->tcp_lport; 18680 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18681 } 18682 *salenp = sizeof (sin_t); 18683 break; 18684 18685 case AF_INET6: 18686 if (*salenp < sizeof (sin6_t)) 18687 return (EINVAL); 18688 18689 *sin6 = sin6_null; 18690 sin6->sin6_family = AF_INET6; 18691 if (tcp->tcp_state >= TCPS_BOUND) { 18692 sin6->sin6_port = tcp->tcp_lport; 18693 if (tcp->tcp_ipversion == IPV4_VERSION) { 18694 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18695 &sin6->sin6_addr); 18696 } else { 18697 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18698 } 18699 } 18700 *salenp = sizeof (sin6_t); 18701 break; 18702 } 18703 18704 return (0); 18705 } 18706 18707 static int 18708 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18709 { 18710 sin_t *sin = (sin_t *)sa; 18711 sin6_t *sin6 = (sin6_t *)sa; 18712 18713 if (tcp->tcp_state < TCPS_SYN_RCVD) 18714 return (ENOTCONN); 18715 18716 switch (tcp->tcp_family) { 18717 case AF_INET: 18718 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18719 18720 if (*salenp < sizeof (sin_t)) 18721 return (EINVAL); 18722 18723 *sin = sin_null; 18724 sin->sin_family = AF_INET; 18725 sin->sin_port = tcp->tcp_fport; 18726 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18727 sin->sin_addr.s_addr); 18728 *salenp = sizeof (sin_t); 18729 break; 18730 18731 case AF_INET6: 18732 if (*salenp < sizeof (sin6_t)) 18733 return (EINVAL); 18734 18735 *sin6 = sin6_null; 18736 sin6->sin6_family = AF_INET6; 18737 sin6->sin6_port = tcp->tcp_fport; 18738 sin6->sin6_addr = tcp->tcp_remote_v6; 18739 if (tcp->tcp_ipversion == IPV6_VERSION) { 18740 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18741 ~IPV6_VERS_AND_FLOW_MASK; 18742 } 18743 *salenp = sizeof (sin6_t); 18744 break; 18745 } 18746 18747 return (0); 18748 } 18749 18750 /* 18751 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18752 */ 18753 static void 18754 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18755 { 18756 void *data; 18757 mblk_t *datamp = mp->b_cont; 18758 tcp_t *tcp = Q_TO_TCP(q); 18759 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18760 18761 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18762 cmdp->cb_error = EPROTO; 18763 qreply(q, mp); 18764 return; 18765 } 18766 18767 data = datamp->b_rptr; 18768 18769 switch (cmdp->cb_cmd) { 18770 case TI_GETPEERNAME: 18771 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 18772 break; 18773 case TI_GETMYNAME: 18774 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 18775 break; 18776 default: 18777 cmdp->cb_error = EINVAL; 18778 break; 18779 } 18780 18781 qreply(q, mp); 18782 } 18783 18784 void 18785 tcp_wput(queue_t *q, mblk_t *mp) 18786 { 18787 conn_t *connp = Q_TO_CONN(q); 18788 tcp_t *tcp; 18789 void (*output_proc)(); 18790 t_scalar_t type; 18791 uchar_t *rptr; 18792 struct iocblk *iocp; 18793 size_t size; 18794 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18795 18796 ASSERT(connp->conn_ref >= 2); 18797 18798 switch (DB_TYPE(mp)) { 18799 case M_DATA: 18800 tcp = connp->conn_tcp; 18801 ASSERT(tcp != NULL); 18802 18803 size = msgdsize(mp); 18804 18805 mutex_enter(&tcp->tcp_non_sq_lock); 18806 tcp->tcp_squeue_bytes += size; 18807 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18808 tcp_setqfull(tcp); 18809 } 18810 mutex_exit(&tcp->tcp_non_sq_lock); 18811 18812 CONN_INC_REF(connp); 18813 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18814 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18815 return; 18816 18817 case M_CMD: 18818 tcp_wput_cmdblk(q, mp); 18819 return; 18820 18821 case M_PROTO: 18822 case M_PCPROTO: 18823 /* 18824 * if it is a snmp message, don't get behind the squeue 18825 */ 18826 tcp = connp->conn_tcp; 18827 rptr = mp->b_rptr; 18828 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18829 type = ((union T_primitives *)rptr)->type; 18830 } else { 18831 if (tcp->tcp_debug) { 18832 (void) strlog(TCP_MOD_ID, 0, 1, 18833 SL_ERROR|SL_TRACE, 18834 "tcp_wput_proto, dropping one..."); 18835 } 18836 freemsg(mp); 18837 return; 18838 } 18839 if (type == T_SVR4_OPTMGMT_REQ) { 18840 /* 18841 * All Solaris components should pass a db_credp 18842 * for this TPI message, hence we ASSERT. 18843 * But in case there is some other M_PROTO that looks 18844 * like a TPI message sent by some other kernel 18845 * component, we check and return an error. 18846 */ 18847 cred_t *cr = msg_getcred(mp, NULL); 18848 18849 ASSERT(cr != NULL); 18850 if (cr == NULL) { 18851 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18852 return; 18853 } 18854 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18855 cr)) { 18856 /* 18857 * This was a SNMP request 18858 */ 18859 return; 18860 } else { 18861 output_proc = tcp_wput_proto; 18862 } 18863 } else { 18864 output_proc = tcp_wput_proto; 18865 } 18866 break; 18867 case M_IOCTL: 18868 /* 18869 * Most ioctls can be processed right away without going via 18870 * squeues - process them right here. Those that do require 18871 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18872 * are processed by tcp_wput_ioctl(). 18873 */ 18874 iocp = (struct iocblk *)mp->b_rptr; 18875 tcp = connp->conn_tcp; 18876 18877 switch (iocp->ioc_cmd) { 18878 case TCP_IOC_ABORT_CONN: 18879 tcp_ioctl_abort_conn(q, mp); 18880 return; 18881 case TI_GETPEERNAME: 18882 case TI_GETMYNAME: 18883 mi_copyin(q, mp, NULL, 18884 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18885 return; 18886 case ND_SET: 18887 /* nd_getset does the necessary checks */ 18888 case ND_GET: 18889 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18890 CALL_IP_WPUT(connp, q, mp); 18891 return; 18892 } 18893 qreply(q, mp); 18894 return; 18895 case TCP_IOC_DEFAULT_Q: 18896 /* 18897 * Wants to be the default wq. Check the credentials 18898 * first, the rest is executed via squeue. 18899 */ 18900 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18901 iocp->ioc_error = EPERM; 18902 iocp->ioc_count = 0; 18903 mp->b_datap->db_type = M_IOCACK; 18904 qreply(q, mp); 18905 return; 18906 } 18907 output_proc = tcp_wput_ioctl; 18908 break; 18909 default: 18910 output_proc = tcp_wput_ioctl; 18911 break; 18912 } 18913 break; 18914 default: 18915 output_proc = tcp_wput_nondata; 18916 break; 18917 } 18918 18919 CONN_INC_REF(connp); 18920 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18921 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18922 } 18923 18924 /* 18925 * Initial STREAMS write side put() procedure for sockets. It tries to 18926 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18927 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18928 * are handled by tcp_wput() as usual. 18929 * 18930 * All further messages will also be handled by tcp_wput() because we cannot 18931 * be sure that the above short cut is safe later. 18932 */ 18933 static void 18934 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18935 { 18936 conn_t *connp = Q_TO_CONN(wq); 18937 tcp_t *tcp = connp->conn_tcp; 18938 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18939 18940 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18941 wq->q_qinfo = &tcp_winit; 18942 18943 ASSERT(IPCL_IS_TCP(connp)); 18944 ASSERT(TCP_IS_SOCKET(tcp)); 18945 18946 if (DB_TYPE(mp) == M_PCPROTO && 18947 MBLKL(mp) == sizeof (struct T_capability_req) && 18948 car->PRIM_type == T_CAPABILITY_REQ) { 18949 tcp_capability_req(tcp, mp); 18950 return; 18951 } 18952 18953 tcp_wput(wq, mp); 18954 } 18955 18956 /* ARGSUSED */ 18957 static void 18958 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18959 { 18960 #ifdef DEBUG 18961 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18962 #endif 18963 freemsg(mp); 18964 } 18965 18966 static boolean_t 18967 tcp_zcopy_check(tcp_t *tcp) 18968 { 18969 conn_t *connp = tcp->tcp_connp; 18970 ire_t *ire; 18971 boolean_t zc_enabled = B_FALSE; 18972 tcp_stack_t *tcps = tcp->tcp_tcps; 18973 18974 if (do_tcpzcopy == 2) 18975 zc_enabled = B_TRUE; 18976 else if (tcp->tcp_ipversion == IPV4_VERSION && 18977 IPCL_IS_CONNECTED(connp) && 18978 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18979 connp->conn_dontroute == 0 && 18980 !connp->conn_nexthop_set && 18981 connp->conn_outgoing_ill == NULL && 18982 do_tcpzcopy == 1) { 18983 /* 18984 * the checks above closely resemble the fast path checks 18985 * in tcp_send_data(). 18986 */ 18987 mutex_enter(&connp->conn_lock); 18988 ire = connp->conn_ire_cache; 18989 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18990 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18991 IRE_REFHOLD(ire); 18992 if (ire->ire_stq != NULL) { 18993 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18994 18995 zc_enabled = ill && (ill->ill_capabilities & 18996 ILL_CAPAB_ZEROCOPY) && 18997 (ill->ill_zerocopy_capab-> 18998 ill_zerocopy_flags != 0); 18999 } 19000 IRE_REFRELE(ire); 19001 } 19002 mutex_exit(&connp->conn_lock); 19003 } 19004 tcp->tcp_snd_zcopy_on = zc_enabled; 19005 if (!TCP_IS_DETACHED(tcp)) { 19006 if (zc_enabled) { 19007 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19008 ZCVMSAFE); 19009 TCP_STAT(tcps, tcp_zcopy_on); 19010 } else { 19011 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19012 ZCVMUNSAFE); 19013 TCP_STAT(tcps, tcp_zcopy_off); 19014 } 19015 } 19016 return (zc_enabled); 19017 } 19018 19019 static mblk_t * 19020 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19021 { 19022 tcp_stack_t *tcps = tcp->tcp_tcps; 19023 19024 if (do_tcpzcopy == 2) 19025 return (bp); 19026 else if (tcp->tcp_snd_zcopy_on) { 19027 tcp->tcp_snd_zcopy_on = B_FALSE; 19028 if (!TCP_IS_DETACHED(tcp)) { 19029 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 19030 ZCVMUNSAFE); 19031 TCP_STAT(tcps, tcp_zcopy_disable); 19032 } 19033 } 19034 return (tcp_zcopy_backoff(tcp, bp, 0)); 19035 } 19036 19037 /* 19038 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19039 * the original desballoca'ed segmapped mblk. 19040 */ 19041 static mblk_t * 19042 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19043 { 19044 mblk_t *head, *tail, *nbp; 19045 tcp_stack_t *tcps = tcp->tcp_tcps; 19046 19047 if (IS_VMLOANED_MBLK(bp)) { 19048 TCP_STAT(tcps, tcp_zcopy_backoff); 19049 if ((head = copyb(bp)) == NULL) { 19050 /* fail to backoff; leave it for the next backoff */ 19051 tcp->tcp_xmit_zc_clean = B_FALSE; 19052 return (bp); 19053 } 19054 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19055 if (fix_xmitlist) 19056 tcp_zcopy_notify(tcp); 19057 else 19058 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19059 } 19060 nbp = bp->b_cont; 19061 if (fix_xmitlist) { 19062 head->b_prev = bp->b_prev; 19063 head->b_next = bp->b_next; 19064 if (tcp->tcp_xmit_tail == bp) 19065 tcp->tcp_xmit_tail = head; 19066 } 19067 bp->b_next = NULL; 19068 bp->b_prev = NULL; 19069 freeb(bp); 19070 } else { 19071 head = bp; 19072 nbp = bp->b_cont; 19073 } 19074 tail = head; 19075 while (nbp) { 19076 if (IS_VMLOANED_MBLK(nbp)) { 19077 TCP_STAT(tcps, tcp_zcopy_backoff); 19078 if ((tail->b_cont = copyb(nbp)) == NULL) { 19079 tcp->tcp_xmit_zc_clean = B_FALSE; 19080 tail->b_cont = nbp; 19081 return (head); 19082 } 19083 tail = tail->b_cont; 19084 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19085 if (fix_xmitlist) 19086 tcp_zcopy_notify(tcp); 19087 else 19088 tail->b_datap->db_struioflag |= 19089 STRUIO_ZCNOTIFY; 19090 } 19091 bp = nbp; 19092 nbp = nbp->b_cont; 19093 if (fix_xmitlist) { 19094 tail->b_prev = bp->b_prev; 19095 tail->b_next = bp->b_next; 19096 if (tcp->tcp_xmit_tail == bp) 19097 tcp->tcp_xmit_tail = tail; 19098 } 19099 bp->b_next = NULL; 19100 bp->b_prev = NULL; 19101 freeb(bp); 19102 } else { 19103 tail->b_cont = nbp; 19104 tail = nbp; 19105 nbp = nbp->b_cont; 19106 } 19107 } 19108 if (fix_xmitlist) { 19109 tcp->tcp_xmit_last = tail; 19110 tcp->tcp_xmit_zc_clean = B_TRUE; 19111 } 19112 return (head); 19113 } 19114 19115 static void 19116 tcp_zcopy_notify(tcp_t *tcp) 19117 { 19118 struct stdata *stp; 19119 conn_t *connp; 19120 19121 if (tcp->tcp_detached) 19122 return; 19123 connp = tcp->tcp_connp; 19124 if (IPCL_IS_NONSTR(connp)) { 19125 (*connp->conn_upcalls->su_zcopy_notify) 19126 (connp->conn_upper_handle); 19127 return; 19128 } 19129 stp = STREAM(tcp->tcp_rq); 19130 mutex_enter(&stp->sd_lock); 19131 stp->sd_flag |= STZCNOTIFY; 19132 cv_broadcast(&stp->sd_zcopy_wait); 19133 mutex_exit(&stp->sd_lock); 19134 } 19135 19136 static boolean_t 19137 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19138 { 19139 ire_t *ire; 19140 conn_t *connp = tcp->tcp_connp; 19141 tcp_stack_t *tcps = tcp->tcp_tcps; 19142 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19143 19144 mutex_enter(&connp->conn_lock); 19145 ire = connp->conn_ire_cache; 19146 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19147 19148 if ((ire != NULL) && 19149 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19150 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19151 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19152 IRE_REFHOLD(ire); 19153 mutex_exit(&connp->conn_lock); 19154 } else { 19155 boolean_t cached = B_FALSE; 19156 ts_label_t *tsl; 19157 19158 /* force a recheck later on */ 19159 tcp->tcp_ire_ill_check_done = B_FALSE; 19160 19161 TCP_DBGSTAT(tcps, tcp_ire_null1); 19162 connp->conn_ire_cache = NULL; 19163 mutex_exit(&connp->conn_lock); 19164 19165 if (ire != NULL) 19166 IRE_REFRELE_NOTR(ire); 19167 19168 tsl = crgetlabel(CONN_CRED(connp)); 19169 ire = (dst ? 19170 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19171 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19172 connp->conn_zoneid, tsl, ipst)); 19173 19174 if (ire == NULL) { 19175 TCP_STAT(tcps, tcp_ire_null); 19176 return (B_FALSE); 19177 } 19178 19179 IRE_REFHOLD_NOTR(ire); 19180 19181 mutex_enter(&connp->conn_lock); 19182 if (CONN_CACHE_IRE(connp)) { 19183 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19184 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19185 TCP_CHECK_IREINFO(tcp, ire); 19186 connp->conn_ire_cache = ire; 19187 cached = B_TRUE; 19188 } 19189 rw_exit(&ire->ire_bucket->irb_lock); 19190 } 19191 mutex_exit(&connp->conn_lock); 19192 19193 /* 19194 * We can continue to use the ire but since it was 19195 * not cached, we should drop the extra reference. 19196 */ 19197 if (!cached) 19198 IRE_REFRELE_NOTR(ire); 19199 19200 /* 19201 * Rampart note: no need to select a new label here, since 19202 * labels are not allowed to change during the life of a TCP 19203 * connection. 19204 */ 19205 } 19206 19207 *irep = ire; 19208 19209 return (B_TRUE); 19210 } 19211 19212 /* 19213 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19214 * 19215 * 0 = success; 19216 * 1 = failed to find ire and ill. 19217 */ 19218 static boolean_t 19219 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19220 { 19221 ipha_t *ipha; 19222 ipaddr_t dst; 19223 ire_t *ire; 19224 ill_t *ill; 19225 mblk_t *ire_fp_mp; 19226 tcp_stack_t *tcps = tcp->tcp_tcps; 19227 19228 if (mp != NULL) 19229 ipha = (ipha_t *)mp->b_rptr; 19230 else 19231 ipha = tcp->tcp_ipha; 19232 dst = ipha->ipha_dst; 19233 19234 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19235 return (B_FALSE); 19236 19237 if ((ire->ire_flags & RTF_MULTIRT) || 19238 (ire->ire_stq == NULL) || 19239 (ire->ire_nce == NULL) || 19240 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19241 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19242 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19243 TCP_STAT(tcps, tcp_ip_ire_send); 19244 IRE_REFRELE(ire); 19245 return (B_FALSE); 19246 } 19247 19248 ill = ire_to_ill(ire); 19249 ASSERT(ill != NULL); 19250 19251 if (!tcp->tcp_ire_ill_check_done) { 19252 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19253 tcp->tcp_ire_ill_check_done = B_TRUE; 19254 } 19255 19256 *irep = ire; 19257 *illp = ill; 19258 19259 return (B_TRUE); 19260 } 19261 19262 static void 19263 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19264 { 19265 ipha_t *ipha; 19266 ipaddr_t src; 19267 ipaddr_t dst; 19268 uint32_t cksum; 19269 ire_t *ire; 19270 uint16_t *up; 19271 ill_t *ill; 19272 conn_t *connp = tcp->tcp_connp; 19273 uint32_t hcksum_txflags = 0; 19274 mblk_t *ire_fp_mp; 19275 uint_t ire_fp_mp_len; 19276 tcp_stack_t *tcps = tcp->tcp_tcps; 19277 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19278 cred_t *cr; 19279 pid_t cpid; 19280 19281 ASSERT(DB_TYPE(mp) == M_DATA); 19282 19283 /* 19284 * Here we need to handle the overloading of the cred_t for 19285 * both getpeerucred and TX. 19286 * If this is a SYN then the caller already set db_credp so 19287 * that getpeerucred will work. But if TX is in use we might have 19288 * a conn_peercred which is different, and we need to use that cred 19289 * to make TX use the correct label and label dependent route. 19290 */ 19291 if (is_system_labeled()) { 19292 cr = msg_getcred(mp, &cpid); 19293 if (cr == NULL || connp->conn_peercred != NULL) 19294 mblk_setcred(mp, CONN_CRED(connp), cpid); 19295 } 19296 19297 ipha = (ipha_t *)mp->b_rptr; 19298 src = ipha->ipha_src; 19299 dst = ipha->ipha_dst; 19300 19301 ASSERT(q != NULL); 19302 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19303 19304 /* 19305 * Drop off fast path for IPv6 and also if options are present or 19306 * we need to resolve a TS label. 19307 */ 19308 if (tcp->tcp_ipversion != IPV4_VERSION || 19309 !IPCL_IS_CONNECTED(connp) || 19310 !CONN_IS_LSO_MD_FASTPATH(connp) || 19311 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19312 !connp->conn_ulp_labeled || 19313 ipha->ipha_ident == IP_HDR_INCLUDED || 19314 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19315 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19316 if (tcp->tcp_snd_zcopy_aware) 19317 mp = tcp_zcopy_disable(tcp, mp); 19318 TCP_STAT(tcps, tcp_ip_send); 19319 CALL_IP_WPUT(connp, q, mp); 19320 return; 19321 } 19322 19323 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19324 if (tcp->tcp_snd_zcopy_aware) 19325 mp = tcp_zcopy_backoff(tcp, mp, 0); 19326 CALL_IP_WPUT(connp, q, mp); 19327 return; 19328 } 19329 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19330 ire_fp_mp_len = MBLKL(ire_fp_mp); 19331 19332 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19333 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19334 #ifndef _BIG_ENDIAN 19335 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19336 #endif 19337 19338 /* 19339 * Check to see if we need to re-enable LSO/MDT for this connection 19340 * because it was previously disabled due to changes in the ill; 19341 * note that by doing it here, this re-enabling only applies when 19342 * the packet is not dispatched through CALL_IP_WPUT(). 19343 * 19344 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19345 * case, since that's how we ended up here. For IPv6, we do the 19346 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19347 */ 19348 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19349 /* 19350 * Restore LSO for this connection, so that next time around 19351 * it is eligible to go through tcp_lsosend() path again. 19352 */ 19353 TCP_STAT(tcps, tcp_lso_enabled); 19354 tcp->tcp_lso = B_TRUE; 19355 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19356 "interface %s\n", (void *)connp, ill->ill_name)); 19357 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19358 /* 19359 * Restore MDT for this connection, so that next time around 19360 * it is eligible to go through tcp_multisend() path again. 19361 */ 19362 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19363 tcp->tcp_mdt = B_TRUE; 19364 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19365 "interface %s\n", (void *)connp, ill->ill_name)); 19366 } 19367 19368 if (tcp->tcp_snd_zcopy_aware) { 19369 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19370 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19371 mp = tcp_zcopy_disable(tcp, mp); 19372 /* 19373 * we shouldn't need to reset ipha as the mp containing 19374 * ipha should never be a zero-copy mp. 19375 */ 19376 } 19377 19378 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19379 ASSERT(ill->ill_hcksum_capab != NULL); 19380 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19381 } 19382 19383 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19384 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19385 19386 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19387 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19388 19389 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19390 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19391 19392 /* Software checksum? */ 19393 if (DB_CKSUMFLAGS(mp) == 0) { 19394 TCP_STAT(tcps, tcp_out_sw_cksum); 19395 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19396 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19397 } 19398 19399 /* Calculate IP header checksum if hardware isn't capable */ 19400 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19401 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19402 ((uint16_t *)ipha)[4]); 19403 } 19404 19405 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19406 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19407 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19408 19409 UPDATE_OB_PKT_COUNT(ire); 19410 ire->ire_last_used_time = lbolt; 19411 19412 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19413 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19414 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19415 ntohs(ipha->ipha_length)); 19416 19417 DTRACE_PROBE4(ip4__physical__out__start, 19418 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 19419 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19420 ipst->ips_ipv4firewall_physical_out, 19421 NULL, ill, ipha, mp, mp, 0, ipst); 19422 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19423 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 19424 19425 if (mp != NULL) { 19426 if (ipst->ips_ipobs_enabled) { 19427 zoneid_t szone; 19428 19429 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 19430 ipst, ALL_ZONES); 19431 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 19432 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 19433 } 19434 19435 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 19436 } 19437 19438 IRE_REFRELE(ire); 19439 } 19440 19441 /* 19442 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19443 * if the receiver shrinks the window, i.e. moves the right window to the 19444 * left, the we should not send new data, but should retransmit normally the 19445 * old unacked data between suna and suna + swnd. We might has sent data 19446 * that is now outside the new window, pretend that we didn't send it. 19447 */ 19448 static void 19449 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19450 { 19451 uint32_t snxt = tcp->tcp_snxt; 19452 mblk_t *xmit_tail; 19453 int32_t offset; 19454 19455 ASSERT(shrunk_count > 0); 19456 19457 /* Pretend we didn't send the data outside the window */ 19458 snxt -= shrunk_count; 19459 19460 /* Get the mblk and the offset in it per the shrunk window */ 19461 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19462 19463 ASSERT(xmit_tail != NULL); 19464 19465 /* Reset all the values per the now shrunk window */ 19466 tcp->tcp_snxt = snxt; 19467 tcp->tcp_xmit_tail = xmit_tail; 19468 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19469 offset; 19470 tcp->tcp_unsent += shrunk_count; 19471 19472 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19473 /* 19474 * Make sure the timer is running so that we will probe a zero 19475 * window. 19476 */ 19477 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19478 } 19479 19480 19481 /* 19482 * The TCP normal data output path. 19483 * NOTE: the logic of the fast path is duplicated from this function. 19484 */ 19485 static void 19486 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19487 { 19488 int len; 19489 mblk_t *local_time; 19490 mblk_t *mp1; 19491 uint32_t snxt; 19492 int tail_unsent; 19493 int tcpstate; 19494 int usable = 0; 19495 mblk_t *xmit_tail; 19496 queue_t *q = tcp->tcp_wq; 19497 int32_t mss; 19498 int32_t num_sack_blk = 0; 19499 int32_t tcp_hdr_len; 19500 int32_t tcp_tcp_hdr_len; 19501 int mdt_thres; 19502 int rc; 19503 tcp_stack_t *tcps = tcp->tcp_tcps; 19504 ip_stack_t *ipst; 19505 19506 tcpstate = tcp->tcp_state; 19507 if (mp == NULL) { 19508 /* 19509 * tcp_wput_data() with NULL mp should only be called when 19510 * there is unsent data. 19511 */ 19512 ASSERT(tcp->tcp_unsent > 0); 19513 /* Really tacky... but we need this for detached closes. */ 19514 len = tcp->tcp_unsent; 19515 goto data_null; 19516 } 19517 19518 #if CCS_STATS 19519 wrw_stats.tot.count++; 19520 wrw_stats.tot.bytes += msgdsize(mp); 19521 #endif 19522 ASSERT(mp->b_datap->db_type == M_DATA); 19523 /* 19524 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19525 * or before a connection attempt has begun. 19526 */ 19527 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19528 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19529 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19530 #ifdef DEBUG 19531 cmn_err(CE_WARN, 19532 "tcp_wput_data: data after ordrel, %s", 19533 tcp_display(tcp, NULL, 19534 DISP_ADDR_AND_PORT)); 19535 #else 19536 if (tcp->tcp_debug) { 19537 (void) strlog(TCP_MOD_ID, 0, 1, 19538 SL_TRACE|SL_ERROR, 19539 "tcp_wput_data: data after ordrel, %s\n", 19540 tcp_display(tcp, NULL, 19541 DISP_ADDR_AND_PORT)); 19542 } 19543 #endif /* DEBUG */ 19544 } 19545 if (tcp->tcp_snd_zcopy_aware && 19546 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19547 tcp_zcopy_notify(tcp); 19548 freemsg(mp); 19549 mutex_enter(&tcp->tcp_non_sq_lock); 19550 if (tcp->tcp_flow_stopped && 19551 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19552 tcp_clrqfull(tcp); 19553 } 19554 mutex_exit(&tcp->tcp_non_sq_lock); 19555 return; 19556 } 19557 19558 /* Strip empties */ 19559 for (;;) { 19560 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19561 (uintptr_t)INT_MAX); 19562 len = (int)(mp->b_wptr - mp->b_rptr); 19563 if (len > 0) 19564 break; 19565 mp1 = mp; 19566 mp = mp->b_cont; 19567 freeb(mp1); 19568 if (!mp) { 19569 return; 19570 } 19571 } 19572 19573 /* If we are the first on the list ... */ 19574 if (tcp->tcp_xmit_head == NULL) { 19575 tcp->tcp_xmit_head = mp; 19576 tcp->tcp_xmit_tail = mp; 19577 tcp->tcp_xmit_tail_unsent = len; 19578 } else { 19579 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19580 struct datab *dp; 19581 19582 mp1 = tcp->tcp_xmit_last; 19583 if (len < tcp_tx_pull_len && 19584 (dp = mp1->b_datap)->db_ref == 1 && 19585 dp->db_lim - mp1->b_wptr >= len) { 19586 ASSERT(len > 0); 19587 ASSERT(!mp1->b_cont); 19588 if (len == 1) { 19589 *mp1->b_wptr++ = *mp->b_rptr; 19590 } else { 19591 bcopy(mp->b_rptr, mp1->b_wptr, len); 19592 mp1->b_wptr += len; 19593 } 19594 if (mp1 == tcp->tcp_xmit_tail) 19595 tcp->tcp_xmit_tail_unsent += len; 19596 mp1->b_cont = mp->b_cont; 19597 if (tcp->tcp_snd_zcopy_aware && 19598 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19599 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19600 freeb(mp); 19601 mp = mp1; 19602 } else { 19603 tcp->tcp_xmit_last->b_cont = mp; 19604 } 19605 len += tcp->tcp_unsent; 19606 } 19607 19608 /* Tack on however many more positive length mblks we have */ 19609 if ((mp1 = mp->b_cont) != NULL) { 19610 do { 19611 int tlen; 19612 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19613 (uintptr_t)INT_MAX); 19614 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19615 if (tlen <= 0) { 19616 mp->b_cont = mp1->b_cont; 19617 freeb(mp1); 19618 } else { 19619 len += tlen; 19620 mp = mp1; 19621 } 19622 } while ((mp1 = mp->b_cont) != NULL); 19623 } 19624 tcp->tcp_xmit_last = mp; 19625 tcp->tcp_unsent = len; 19626 19627 if (urgent) 19628 usable = 1; 19629 19630 data_null: 19631 snxt = tcp->tcp_snxt; 19632 xmit_tail = tcp->tcp_xmit_tail; 19633 tail_unsent = tcp->tcp_xmit_tail_unsent; 19634 19635 /* 19636 * Note that tcp_mss has been adjusted to take into account the 19637 * timestamp option if applicable. Because SACK options do not 19638 * appear in every TCP segments and they are of variable lengths, 19639 * they cannot be included in tcp_mss. Thus we need to calculate 19640 * the actual segment length when we need to send a segment which 19641 * includes SACK options. 19642 */ 19643 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19644 int32_t opt_len; 19645 19646 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19647 tcp->tcp_num_sack_blk); 19648 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19649 2 + TCPOPT_HEADER_LEN; 19650 mss = tcp->tcp_mss - opt_len; 19651 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19652 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19653 } else { 19654 mss = tcp->tcp_mss; 19655 tcp_hdr_len = tcp->tcp_hdr_len; 19656 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19657 } 19658 19659 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19660 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19661 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19662 } 19663 if (tcpstate == TCPS_SYN_RCVD) { 19664 /* 19665 * The three-way connection establishment handshake is not 19666 * complete yet. We want to queue the data for transmission 19667 * after entering ESTABLISHED state (RFC793). A jump to 19668 * "done" label effectively leaves data on the queue. 19669 */ 19670 goto done; 19671 } else { 19672 int usable_r; 19673 19674 /* 19675 * In the special case when cwnd is zero, which can only 19676 * happen if the connection is ECN capable, return now. 19677 * New segments is sent using tcp_timer(). The timer 19678 * is set in tcp_rput_data(). 19679 */ 19680 if (tcp->tcp_cwnd == 0) { 19681 /* 19682 * Note that tcp_cwnd is 0 before 3-way handshake is 19683 * finished. 19684 */ 19685 ASSERT(tcp->tcp_ecn_ok || 19686 tcp->tcp_state < TCPS_ESTABLISHED); 19687 return; 19688 } 19689 19690 /* NOTE: trouble if xmitting while SYN not acked? */ 19691 usable_r = snxt - tcp->tcp_suna; 19692 usable_r = tcp->tcp_swnd - usable_r; 19693 19694 /* 19695 * Check if the receiver has shrunk the window. If 19696 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19697 * cannot be set as there is unsent data, so FIN cannot 19698 * be sent out. Otherwise, we need to take into account 19699 * of FIN as it consumes an "invisible" sequence number. 19700 */ 19701 ASSERT(tcp->tcp_fin_sent == 0); 19702 if (usable_r < 0) { 19703 /* 19704 * The receiver has shrunk the window and we have sent 19705 * -usable_r date beyond the window, re-adjust. 19706 * 19707 * If TCP window scaling is enabled, there can be 19708 * round down error as the advertised receive window 19709 * is actually right shifted n bits. This means that 19710 * the lower n bits info is wiped out. It will look 19711 * like the window is shrunk. Do a check here to 19712 * see if the shrunk amount is actually within the 19713 * error in window calculation. If it is, just 19714 * return. Note that this check is inside the 19715 * shrunk window check. This makes sure that even 19716 * though tcp_process_shrunk_swnd() is not called, 19717 * we will stop further processing. 19718 */ 19719 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19720 tcp_process_shrunk_swnd(tcp, -usable_r); 19721 } 19722 return; 19723 } 19724 19725 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19726 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19727 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19728 19729 /* usable = MIN(usable, unsent) */ 19730 if (usable_r > len) 19731 usable_r = len; 19732 19733 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19734 if (usable_r > 0) { 19735 usable = usable_r; 19736 } else { 19737 /* Bypass all other unnecessary processing. */ 19738 goto done; 19739 } 19740 } 19741 19742 local_time = (mblk_t *)lbolt; 19743 19744 /* 19745 * "Our" Nagle Algorithm. This is not the same as in the old 19746 * BSD. This is more in line with the true intent of Nagle. 19747 * 19748 * The conditions are: 19749 * 1. The amount of unsent data (or amount of data which can be 19750 * sent, whichever is smaller) is less than Nagle limit. 19751 * 2. The last sent size is also less than Nagle limit. 19752 * 3. There is unack'ed data. 19753 * 4. Urgent pointer is not set. Send urgent data ignoring the 19754 * Nagle algorithm. This reduces the probability that urgent 19755 * bytes get "merged" together. 19756 * 5. The app has not closed the connection. This eliminates the 19757 * wait time of the receiving side waiting for the last piece of 19758 * (small) data. 19759 * 19760 * If all are satisified, exit without sending anything. Note 19761 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19762 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19763 * 4095). 19764 */ 19765 if (usable < (int)tcp->tcp_naglim && 19766 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19767 snxt != tcp->tcp_suna && 19768 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19769 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19770 goto done; 19771 } 19772 19773 if (tcp->tcp_cork) { 19774 /* 19775 * if the tcp->tcp_cork option is set, then we have to force 19776 * TCP not to send partial segment (smaller than MSS bytes). 19777 * We are calculating the usable now based on full mss and 19778 * will save the rest of remaining data for later. 19779 */ 19780 if (usable < mss) 19781 goto done; 19782 usable = (usable / mss) * mss; 19783 } 19784 19785 /* Update the latest receive window size in TCP header. */ 19786 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19787 tcp->tcp_tcph->th_win); 19788 19789 /* 19790 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19791 * 19792 * 1. Simple TCP/IP{v4,v6} (no options). 19793 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19794 * 3. If the TCP connection is in ESTABLISHED state. 19795 * 4. The TCP is not detached. 19796 * 19797 * If any of the above conditions have changed during the 19798 * connection, stop using LSO/MDT and restore the stream head 19799 * parameters accordingly. 19800 */ 19801 ipst = tcps->tcps_netstack->netstack_ip; 19802 19803 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19804 ((tcp->tcp_ipversion == IPV4_VERSION && 19805 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19806 (tcp->tcp_ipversion == IPV6_VERSION && 19807 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19808 tcp->tcp_state != TCPS_ESTABLISHED || 19809 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19810 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19811 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19812 if (tcp->tcp_lso) { 19813 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19814 tcp->tcp_lso = B_FALSE; 19815 } else { 19816 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19817 tcp->tcp_mdt = B_FALSE; 19818 } 19819 19820 /* Anything other than detached is considered pathological */ 19821 if (!TCP_IS_DETACHED(tcp)) { 19822 if (tcp->tcp_lso) 19823 TCP_STAT(tcps, tcp_lso_disabled); 19824 else 19825 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19826 (void) tcp_maxpsz_set(tcp, B_TRUE); 19827 } 19828 } 19829 19830 /* Use MDT if sendable amount is greater than the threshold */ 19831 if (tcp->tcp_mdt && 19832 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19833 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19834 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19835 (tcp->tcp_valid_bits == 0 || 19836 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19837 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19838 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19839 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19840 local_time, mdt_thres); 19841 } else { 19842 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19843 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19844 local_time, INT_MAX); 19845 } 19846 19847 /* Pretend that all we were trying to send really got sent */ 19848 if (rc < 0 && tail_unsent < 0) { 19849 do { 19850 xmit_tail = xmit_tail->b_cont; 19851 xmit_tail->b_prev = local_time; 19852 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19853 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19854 tail_unsent += (int)(xmit_tail->b_wptr - 19855 xmit_tail->b_rptr); 19856 } while (tail_unsent < 0); 19857 } 19858 done:; 19859 tcp->tcp_xmit_tail = xmit_tail; 19860 tcp->tcp_xmit_tail_unsent = tail_unsent; 19861 len = tcp->tcp_snxt - snxt; 19862 if (len) { 19863 /* 19864 * If new data was sent, need to update the notsack 19865 * list, which is, afterall, data blocks that have 19866 * not been sack'ed by the receiver. New data is 19867 * not sack'ed. 19868 */ 19869 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19870 /* len is a negative value. */ 19871 tcp->tcp_pipe -= len; 19872 tcp_notsack_update(&(tcp->tcp_notsack_list), 19873 tcp->tcp_snxt, snxt, 19874 &(tcp->tcp_num_notsack_blk), 19875 &(tcp->tcp_cnt_notsack_list)); 19876 } 19877 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19878 tcp->tcp_rack = tcp->tcp_rnxt; 19879 tcp->tcp_rack_cnt = 0; 19880 if ((snxt + len) == tcp->tcp_suna) { 19881 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19882 } 19883 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19884 /* 19885 * Didn't send anything. Make sure the timer is running 19886 * so that we will probe a zero window. 19887 */ 19888 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19889 } 19890 /* Note that len is the amount we just sent but with a negative sign */ 19891 tcp->tcp_unsent += len; 19892 mutex_enter(&tcp->tcp_non_sq_lock); 19893 if (tcp->tcp_flow_stopped) { 19894 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19895 tcp_clrqfull(tcp); 19896 } 19897 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19898 tcp_setqfull(tcp); 19899 } 19900 mutex_exit(&tcp->tcp_non_sq_lock); 19901 } 19902 19903 /* 19904 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19905 * outgoing TCP header with the template header, as well as other 19906 * options such as time-stamp, ECN and/or SACK. 19907 */ 19908 static void 19909 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19910 { 19911 tcph_t *tcp_tmpl, *tcp_h; 19912 uint32_t *dst, *src; 19913 int hdrlen; 19914 19915 ASSERT(OK_32PTR(rptr)); 19916 19917 /* Template header */ 19918 tcp_tmpl = tcp->tcp_tcph; 19919 19920 /* Header of outgoing packet */ 19921 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19922 19923 /* dst and src are opaque 32-bit fields, used for copying */ 19924 dst = (uint32_t *)rptr; 19925 src = (uint32_t *)tcp->tcp_iphc; 19926 hdrlen = tcp->tcp_hdr_len; 19927 19928 /* Fill time-stamp option if needed */ 19929 if (tcp->tcp_snd_ts_ok) { 19930 U32_TO_BE32((uint32_t)now, 19931 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19932 U32_TO_BE32(tcp->tcp_ts_recent, 19933 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19934 } else { 19935 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19936 } 19937 19938 /* 19939 * Copy the template header; is this really more efficient than 19940 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19941 * but perhaps not for other scenarios. 19942 */ 19943 dst[0] = src[0]; 19944 dst[1] = src[1]; 19945 dst[2] = src[2]; 19946 dst[3] = src[3]; 19947 dst[4] = src[4]; 19948 dst[5] = src[5]; 19949 dst[6] = src[6]; 19950 dst[7] = src[7]; 19951 dst[8] = src[8]; 19952 dst[9] = src[9]; 19953 if (hdrlen -= 40) { 19954 hdrlen >>= 2; 19955 dst += 10; 19956 src += 10; 19957 do { 19958 *dst++ = *src++; 19959 } while (--hdrlen); 19960 } 19961 19962 /* 19963 * Set the ECN info in the TCP header if it is not a zero 19964 * window probe. Zero window probe is only sent in 19965 * tcp_wput_data() and tcp_timer(). 19966 */ 19967 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19968 SET_ECT(tcp, rptr); 19969 19970 if (tcp->tcp_ecn_echo_on) 19971 tcp_h->th_flags[0] |= TH_ECE; 19972 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19973 tcp_h->th_flags[0] |= TH_CWR; 19974 tcp->tcp_ecn_cwr_sent = B_TRUE; 19975 } 19976 } 19977 19978 /* Fill in SACK options */ 19979 if (num_sack_blk > 0) { 19980 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19981 sack_blk_t *tmp; 19982 int32_t i; 19983 19984 wptr[0] = TCPOPT_NOP; 19985 wptr[1] = TCPOPT_NOP; 19986 wptr[2] = TCPOPT_SACK; 19987 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19988 sizeof (sack_blk_t); 19989 wptr += TCPOPT_REAL_SACK_LEN; 19990 19991 tmp = tcp->tcp_sack_list; 19992 for (i = 0; i < num_sack_blk; i++) { 19993 U32_TO_BE32(tmp[i].begin, wptr); 19994 wptr += sizeof (tcp_seq); 19995 U32_TO_BE32(tmp[i].end, wptr); 19996 wptr += sizeof (tcp_seq); 19997 } 19998 tcp_h->th_offset_and_rsrvd[0] += 19999 ((num_sack_blk * 2 + 1) << 4); 20000 } 20001 } 20002 20003 /* 20004 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20005 * the destination address and SAP attribute, and if necessary, the 20006 * hardware checksum offload attribute to a Multidata message. 20007 */ 20008 static int 20009 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20010 const uint32_t start, const uint32_t stuff, const uint32_t end, 20011 const uint32_t flags, tcp_stack_t *tcps) 20012 { 20013 /* Add global destination address & SAP attribute */ 20014 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20015 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20016 "destination address+SAP\n")); 20017 20018 if (dlmp != NULL) 20019 TCP_STAT(tcps, tcp_mdt_allocfail); 20020 return (-1); 20021 } 20022 20023 /* Add global hwcksum attribute */ 20024 if (hwcksum && 20025 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20026 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20027 "checksum attribute\n")); 20028 20029 TCP_STAT(tcps, tcp_mdt_allocfail); 20030 return (-1); 20031 } 20032 20033 return (0); 20034 } 20035 20036 /* 20037 * Smaller and private version of pdescinfo_t used specifically for TCP, 20038 * which allows for only two payload spans per packet. 20039 */ 20040 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20041 20042 /* 20043 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20044 * scheme, and returns one the following: 20045 * 20046 * -1 = failed allocation. 20047 * 0 = success; burst count reached, or usable send window is too small, 20048 * and that we'd rather wait until later before sending again. 20049 */ 20050 static int 20051 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20052 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20053 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20054 const int mdt_thres) 20055 { 20056 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20057 multidata_t *mmd; 20058 uint_t obsegs, obbytes, hdr_frag_sz; 20059 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20060 int num_burst_seg, max_pld; 20061 pdesc_t *pkt; 20062 tcp_pdescinfo_t tcp_pkt_info; 20063 pdescinfo_t *pkt_info; 20064 int pbuf_idx, pbuf_idx_nxt; 20065 int seg_len, len, spill, af; 20066 boolean_t add_buffer, zcopy, clusterwide; 20067 boolean_t rconfirm = B_FALSE; 20068 boolean_t done = B_FALSE; 20069 uint32_t cksum; 20070 uint32_t hwcksum_flags; 20071 ire_t *ire = NULL; 20072 ill_t *ill; 20073 ipha_t *ipha; 20074 ip6_t *ip6h; 20075 ipaddr_t src, dst; 20076 ill_zerocopy_capab_t *zc_cap = NULL; 20077 uint16_t *up; 20078 int err; 20079 conn_t *connp; 20080 tcp_stack_t *tcps = tcp->tcp_tcps; 20081 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20082 int usable_mmd, tail_unsent_mmd; 20083 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20084 mblk_t *xmit_tail_mmd; 20085 netstackid_t stack_id; 20086 20087 #ifdef _BIG_ENDIAN 20088 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20089 #else 20090 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20091 #endif 20092 20093 #define PREP_NEW_MULTIDATA() { \ 20094 mmd = NULL; \ 20095 md_mp = md_hbuf = NULL; \ 20096 cur_hdr_off = 0; \ 20097 max_pld = tcp->tcp_mdt_max_pld; \ 20098 pbuf_idx = pbuf_idx_nxt = -1; \ 20099 add_buffer = B_TRUE; \ 20100 zcopy = B_FALSE; \ 20101 } 20102 20103 #define PREP_NEW_PBUF() { \ 20104 md_pbuf = md_pbuf_nxt = NULL; \ 20105 pbuf_idx = pbuf_idx_nxt = -1; \ 20106 cur_pld_off = 0; \ 20107 first_snxt = *snxt; \ 20108 ASSERT(*tail_unsent > 0); \ 20109 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20110 } 20111 20112 ASSERT(mdt_thres >= mss); 20113 ASSERT(*usable > 0 && *usable > mdt_thres); 20114 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20115 ASSERT(!TCP_IS_DETACHED(tcp)); 20116 ASSERT(tcp->tcp_valid_bits == 0 || 20117 tcp->tcp_valid_bits == TCP_FSS_VALID); 20118 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20119 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20120 (tcp->tcp_ipversion == IPV6_VERSION && 20121 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20122 20123 connp = tcp->tcp_connp; 20124 ASSERT(connp != NULL); 20125 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20126 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20127 20128 stack_id = connp->conn_netstack->netstack_stackid; 20129 20130 usable_mmd = tail_unsent_mmd = 0; 20131 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20132 xmit_tail_mmd = NULL; 20133 /* 20134 * Note that tcp will only declare at most 2 payload spans per 20135 * packet, which is much lower than the maximum allowable number 20136 * of packet spans per Multidata. For this reason, we use the 20137 * privately declared and smaller descriptor info structure, in 20138 * order to save some stack space. 20139 */ 20140 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20141 20142 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20143 if (af == AF_INET) { 20144 dst = tcp->tcp_ipha->ipha_dst; 20145 src = tcp->tcp_ipha->ipha_src; 20146 ASSERT(!CLASSD(dst)); 20147 } 20148 ASSERT(af == AF_INET || 20149 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20150 20151 obsegs = obbytes = 0; 20152 num_burst_seg = tcp->tcp_snd_burst; 20153 md_mp_head = NULL; 20154 PREP_NEW_MULTIDATA(); 20155 20156 /* 20157 * Before we go on further, make sure there is an IRE that we can 20158 * use, and that the ILL supports MDT. Otherwise, there's no point 20159 * in proceeding any further, and we should just hand everything 20160 * off to the legacy path. 20161 */ 20162 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20163 goto legacy_send_no_md; 20164 20165 ASSERT(ire != NULL); 20166 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20167 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20168 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20169 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20170 /* 20171 * If we do support loopback for MDT (which requires modifications 20172 * to the receiving paths), the following assertions should go away, 20173 * and we would be sending the Multidata to loopback conn later on. 20174 */ 20175 ASSERT(!IRE_IS_LOCAL(ire)); 20176 ASSERT(ire->ire_stq != NULL); 20177 20178 ill = ire_to_ill(ire); 20179 ASSERT(ill != NULL); 20180 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20181 20182 if (!tcp->tcp_ire_ill_check_done) { 20183 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20184 tcp->tcp_ire_ill_check_done = B_TRUE; 20185 } 20186 20187 /* 20188 * If the underlying interface conditions have changed, or if the 20189 * new interface does not support MDT, go back to legacy path. 20190 */ 20191 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20192 /* don't go through this path anymore for this connection */ 20193 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20194 tcp->tcp_mdt = B_FALSE; 20195 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20196 "interface %s\n", (void *)connp, ill->ill_name)); 20197 /* IRE will be released prior to returning */ 20198 goto legacy_send_no_md; 20199 } 20200 20201 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20202 zc_cap = ill->ill_zerocopy_capab; 20203 20204 /* 20205 * Check if we can take tcp fast-path. Note that "incomplete" 20206 * ire's (where the link-layer for next hop is not resolved 20207 * or where the fast-path header in nce_fp_mp is not available 20208 * yet) are sent down the legacy (slow) path. 20209 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20210 */ 20211 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20212 /* IRE will be released prior to returning */ 20213 goto legacy_send_no_md; 20214 } 20215 20216 /* go to legacy path if interface doesn't support zerocopy */ 20217 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20218 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20219 /* IRE will be released prior to returning */ 20220 goto legacy_send_no_md; 20221 } 20222 20223 /* does the interface support hardware checksum offload? */ 20224 hwcksum_flags = 0; 20225 if (ILL_HCKSUM_CAPABLE(ill) && 20226 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20227 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20228 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20229 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20230 HCKSUM_IPHDRCKSUM) 20231 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20232 20233 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20234 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20235 hwcksum_flags |= HCK_FULLCKSUM; 20236 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20237 HCKSUM_INET_PARTIAL) 20238 hwcksum_flags |= HCK_PARTIALCKSUM; 20239 } 20240 20241 /* 20242 * Each header fragment consists of the leading extra space, 20243 * followed by the TCP/IP header, and the trailing extra space. 20244 * We make sure that each header fragment begins on a 32-bit 20245 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20246 * aligned in tcp_mdt_update). 20247 */ 20248 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20249 tcp->tcp_mdt_hdr_tail), 4); 20250 20251 /* are we starting from the beginning of data block? */ 20252 if (*tail_unsent == 0) { 20253 *xmit_tail = (*xmit_tail)->b_cont; 20254 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20255 *tail_unsent = (int)MBLKL(*xmit_tail); 20256 } 20257 20258 /* 20259 * Here we create one or more Multidata messages, each made up of 20260 * one header buffer and up to N payload buffers. This entire 20261 * operation is done within two loops: 20262 * 20263 * The outer loop mostly deals with creating the Multidata message, 20264 * as well as the header buffer that gets added to it. It also 20265 * links the Multidata messages together such that all of them can 20266 * be sent down to the lower layer in a single putnext call; this 20267 * linking behavior depends on the tcp_mdt_chain tunable. 20268 * 20269 * The inner loop takes an existing Multidata message, and adds 20270 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20271 * packetizes those buffers by filling up the corresponding header 20272 * buffer fragments with the proper IP and TCP headers, and by 20273 * describing the layout of each packet in the packet descriptors 20274 * that get added to the Multidata. 20275 */ 20276 do { 20277 /* 20278 * If usable send window is too small, or data blocks in 20279 * transmit list are smaller than our threshold (i.e. app 20280 * performs large writes followed by small ones), we hand 20281 * off the control over to the legacy path. Note that we'll 20282 * get back the control once it encounters a large block. 20283 */ 20284 if (*usable < mss || (*tail_unsent <= mdt_thres && 20285 (*xmit_tail)->b_cont != NULL && 20286 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20287 /* send down what we've got so far */ 20288 if (md_mp_head != NULL) { 20289 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20290 obsegs, obbytes, &rconfirm); 20291 } 20292 /* 20293 * Pass control over to tcp_send(), but tell it to 20294 * return to us once a large-size transmission is 20295 * possible. 20296 */ 20297 TCP_STAT(tcps, tcp_mdt_legacy_small); 20298 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20299 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20300 tail_unsent, xmit_tail, local_time, 20301 mdt_thres)) <= 0) { 20302 /* burst count reached, or alloc failed */ 20303 IRE_REFRELE(ire); 20304 return (err); 20305 } 20306 20307 /* tcp_send() may have sent everything, so check */ 20308 if (*usable <= 0) { 20309 IRE_REFRELE(ire); 20310 return (0); 20311 } 20312 20313 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20314 /* 20315 * We may have delivered the Multidata, so make sure 20316 * to re-initialize before the next round. 20317 */ 20318 md_mp_head = NULL; 20319 obsegs = obbytes = 0; 20320 num_burst_seg = tcp->tcp_snd_burst; 20321 PREP_NEW_MULTIDATA(); 20322 20323 /* are we starting from the beginning of data block? */ 20324 if (*tail_unsent == 0) { 20325 *xmit_tail = (*xmit_tail)->b_cont; 20326 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20327 (uintptr_t)INT_MAX); 20328 *tail_unsent = (int)MBLKL(*xmit_tail); 20329 } 20330 } 20331 /* 20332 * Record current values for parameters we may need to pass 20333 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20334 * each iteration of the outer loop (each multidata message 20335 * creation). If we have a failure in the inner loop, we send 20336 * any complete multidata messages we have before reverting 20337 * to using the traditional non-md path. 20338 */ 20339 snxt_mmd = *snxt; 20340 usable_mmd = *usable; 20341 xmit_tail_mmd = *xmit_tail; 20342 tail_unsent_mmd = *tail_unsent; 20343 obsegs_mmd = obsegs; 20344 obbytes_mmd = obbytes; 20345 20346 /* 20347 * max_pld limits the number of mblks in tcp's transmit 20348 * queue that can be added to a Multidata message. Once 20349 * this counter reaches zero, no more additional mblks 20350 * can be added to it. What happens afterwards depends 20351 * on whether or not we are set to chain the Multidata 20352 * messages. If we are to link them together, reset 20353 * max_pld to its original value (tcp_mdt_max_pld) and 20354 * prepare to create a new Multidata message which will 20355 * get linked to md_mp_head. Else, leave it alone and 20356 * let the inner loop break on its own. 20357 */ 20358 if (tcp_mdt_chain && max_pld == 0) 20359 PREP_NEW_MULTIDATA(); 20360 20361 /* adding a payload buffer; re-initialize values */ 20362 if (add_buffer) 20363 PREP_NEW_PBUF(); 20364 20365 /* 20366 * If we don't have a Multidata, either because we just 20367 * (re)entered this outer loop, or after we branched off 20368 * to tcp_send above, setup the Multidata and header 20369 * buffer to be used. 20370 */ 20371 if (md_mp == NULL) { 20372 int md_hbuflen; 20373 uint32_t start, stuff; 20374 20375 /* 20376 * Calculate Multidata header buffer size large enough 20377 * to hold all of the headers that can possibly be 20378 * sent at this moment. We'd rather over-estimate 20379 * the size than running out of space; this is okay 20380 * since this buffer is small anyway. 20381 */ 20382 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20383 20384 /* 20385 * Start and stuff offset for partial hardware 20386 * checksum offload; these are currently for IPv4. 20387 * For full checksum offload, they are set to zero. 20388 */ 20389 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20390 if (af == AF_INET) { 20391 start = IP_SIMPLE_HDR_LENGTH; 20392 stuff = IP_SIMPLE_HDR_LENGTH + 20393 TCP_CHECKSUM_OFFSET; 20394 } else { 20395 start = IPV6_HDR_LEN; 20396 stuff = IPV6_HDR_LEN + 20397 TCP_CHECKSUM_OFFSET; 20398 } 20399 } else { 20400 start = stuff = 0; 20401 } 20402 20403 /* 20404 * Create the header buffer, Multidata, as well as 20405 * any necessary attributes (destination address, 20406 * SAP and hardware checksum offload) that should 20407 * be associated with the Multidata message. 20408 */ 20409 ASSERT(cur_hdr_off == 0); 20410 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20411 ((md_hbuf->b_wptr += md_hbuflen), 20412 (mmd = mmd_alloc(md_hbuf, &md_mp, 20413 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20414 /* fastpath mblk */ 20415 ire->ire_nce->nce_res_mp, 20416 /* hardware checksum enabled */ 20417 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20418 /* hardware checksum offsets */ 20419 start, stuff, 0, 20420 /* hardware checksum flag */ 20421 hwcksum_flags, tcps) != 0)) { 20422 legacy_send: 20423 /* 20424 * We arrive here from a failure within the 20425 * inner (packetizer) loop or we fail one of 20426 * the conditionals above. We restore the 20427 * previously checkpointed values for: 20428 * xmit_tail 20429 * usable 20430 * tail_unsent 20431 * snxt 20432 * obbytes 20433 * obsegs 20434 * We should then be able to dispatch any 20435 * complete multidata before reverting to the 20436 * traditional path with consistent parameters 20437 * (the inner loop updates these as it 20438 * iterates). 20439 */ 20440 *xmit_tail = xmit_tail_mmd; 20441 *usable = usable_mmd; 20442 *tail_unsent = tail_unsent_mmd; 20443 *snxt = snxt_mmd; 20444 obbytes = obbytes_mmd; 20445 obsegs = obsegs_mmd; 20446 if (md_mp != NULL) { 20447 /* Unlink message from the chain */ 20448 if (md_mp_head != NULL) { 20449 err = (intptr_t)rmvb(md_mp_head, 20450 md_mp); 20451 /* 20452 * We can't assert that rmvb 20453 * did not return -1, since we 20454 * may get here before linkb 20455 * happens. We do, however, 20456 * check if we just removed the 20457 * only element in the list. 20458 */ 20459 if (err == 0) 20460 md_mp_head = NULL; 20461 } 20462 /* md_hbuf gets freed automatically */ 20463 TCP_STAT(tcps, tcp_mdt_discarded); 20464 freeb(md_mp); 20465 } else { 20466 /* Either allocb or mmd_alloc failed */ 20467 TCP_STAT(tcps, tcp_mdt_allocfail); 20468 if (md_hbuf != NULL) 20469 freeb(md_hbuf); 20470 } 20471 20472 /* send down what we've got so far */ 20473 if (md_mp_head != NULL) { 20474 tcp_multisend_data(tcp, ire, ill, 20475 md_mp_head, obsegs, obbytes, 20476 &rconfirm); 20477 } 20478 legacy_send_no_md: 20479 if (ire != NULL) 20480 IRE_REFRELE(ire); 20481 /* 20482 * Too bad; let the legacy path handle this. 20483 * We specify INT_MAX for the threshold, since 20484 * we gave up with the Multidata processings 20485 * and let the old path have it all. 20486 */ 20487 TCP_STAT(tcps, tcp_mdt_legacy_all); 20488 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20489 tcp_tcp_hdr_len, num_sack_blk, usable, 20490 snxt, tail_unsent, xmit_tail, local_time, 20491 INT_MAX)); 20492 } 20493 20494 /* link to any existing ones, if applicable */ 20495 TCP_STAT(tcps, tcp_mdt_allocd); 20496 if (md_mp_head == NULL) { 20497 md_mp_head = md_mp; 20498 } else if (tcp_mdt_chain) { 20499 TCP_STAT(tcps, tcp_mdt_linked); 20500 linkb(md_mp_head, md_mp); 20501 } 20502 } 20503 20504 ASSERT(md_mp_head != NULL); 20505 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20506 ASSERT(md_mp != NULL && mmd != NULL); 20507 ASSERT(md_hbuf != NULL); 20508 20509 /* 20510 * Packetize the transmittable portion of the data block; 20511 * each data block is essentially added to the Multidata 20512 * as a payload buffer. We also deal with adding more 20513 * than one payload buffers, which happens when the remaining 20514 * packetized portion of the current payload buffer is less 20515 * than MSS, while the next data block in transmit queue 20516 * has enough data to make up for one. This "spillover" 20517 * case essentially creates a split-packet, where portions 20518 * of the packet's payload fragments may span across two 20519 * virtually discontiguous address blocks. 20520 */ 20521 seg_len = mss; 20522 do { 20523 len = seg_len; 20524 20525 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20526 ipha = NULL; 20527 ip6h = NULL; 20528 20529 ASSERT(len > 0); 20530 ASSERT(max_pld >= 0); 20531 ASSERT(!add_buffer || cur_pld_off == 0); 20532 20533 /* 20534 * First time around for this payload buffer; note 20535 * in the case of a spillover, the following has 20536 * been done prior to adding the split-packet 20537 * descriptor to Multidata, and we don't want to 20538 * repeat the process. 20539 */ 20540 if (add_buffer) { 20541 ASSERT(mmd != NULL); 20542 ASSERT(md_pbuf == NULL); 20543 ASSERT(md_pbuf_nxt == NULL); 20544 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20545 20546 /* 20547 * Have we reached the limit? We'd get to 20548 * this case when we're not chaining the 20549 * Multidata messages together, and since 20550 * we're done, terminate this loop. 20551 */ 20552 if (max_pld == 0) 20553 break; /* done */ 20554 20555 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20556 TCP_STAT(tcps, tcp_mdt_allocfail); 20557 goto legacy_send; /* out_of_mem */ 20558 } 20559 20560 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20561 zc_cap != NULL) { 20562 if (!ip_md_zcopy_attr(mmd, NULL, 20563 zc_cap->ill_zerocopy_flags)) { 20564 freeb(md_pbuf); 20565 TCP_STAT(tcps, 20566 tcp_mdt_allocfail); 20567 /* out_of_mem */ 20568 goto legacy_send; 20569 } 20570 zcopy = B_TRUE; 20571 } 20572 20573 md_pbuf->b_rptr += base_pld_off; 20574 20575 /* 20576 * Add a payload buffer to the Multidata; this 20577 * operation must not fail, or otherwise our 20578 * logic in this routine is broken. There 20579 * is no memory allocation done by the 20580 * routine, so any returned failure simply 20581 * tells us that we've done something wrong. 20582 * 20583 * A failure tells us that either we're adding 20584 * the same payload buffer more than once, or 20585 * we're trying to add more buffers than 20586 * allowed (max_pld calculation is wrong). 20587 * None of the above cases should happen, and 20588 * we panic because either there's horrible 20589 * heap corruption, and/or programming mistake. 20590 */ 20591 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20592 if (pbuf_idx < 0) { 20593 cmn_err(CE_PANIC, "tcp_multisend: " 20594 "payload buffer logic error " 20595 "detected for tcp %p mmd %p " 20596 "pbuf %p (%d)\n", 20597 (void *)tcp, (void *)mmd, 20598 (void *)md_pbuf, pbuf_idx); 20599 } 20600 20601 ASSERT(max_pld > 0); 20602 --max_pld; 20603 add_buffer = B_FALSE; 20604 } 20605 20606 ASSERT(md_mp_head != NULL); 20607 ASSERT(md_pbuf != NULL); 20608 ASSERT(md_pbuf_nxt == NULL); 20609 ASSERT(pbuf_idx != -1); 20610 ASSERT(pbuf_idx_nxt == -1); 20611 ASSERT(*usable > 0); 20612 20613 /* 20614 * We spillover to the next payload buffer only 20615 * if all of the following is true: 20616 * 20617 * 1. There is not enough data on the current 20618 * payload buffer to make up `len', 20619 * 2. We are allowed to send `len', 20620 * 3. The next payload buffer length is large 20621 * enough to accomodate `spill'. 20622 */ 20623 if ((spill = len - *tail_unsent) > 0 && 20624 *usable >= len && 20625 MBLKL((*xmit_tail)->b_cont) >= spill && 20626 max_pld > 0) { 20627 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20628 if (md_pbuf_nxt == NULL) { 20629 TCP_STAT(tcps, tcp_mdt_allocfail); 20630 goto legacy_send; /* out_of_mem */ 20631 } 20632 20633 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20634 zc_cap != NULL) { 20635 if (!ip_md_zcopy_attr(mmd, NULL, 20636 zc_cap->ill_zerocopy_flags)) { 20637 freeb(md_pbuf_nxt); 20638 TCP_STAT(tcps, 20639 tcp_mdt_allocfail); 20640 /* out_of_mem */ 20641 goto legacy_send; 20642 } 20643 zcopy = B_TRUE; 20644 } 20645 20646 /* 20647 * See comments above on the first call to 20648 * mmd_addpldbuf for explanation on the panic. 20649 */ 20650 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20651 if (pbuf_idx_nxt < 0) { 20652 panic("tcp_multisend: " 20653 "next payload buffer logic error " 20654 "detected for tcp %p mmd %p " 20655 "pbuf %p (%d)\n", 20656 (void *)tcp, (void *)mmd, 20657 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20658 } 20659 20660 ASSERT(max_pld > 0); 20661 --max_pld; 20662 } else if (spill > 0) { 20663 /* 20664 * If there's a spillover, but the following 20665 * xmit_tail couldn't give us enough octets 20666 * to reach "len", then stop the current 20667 * Multidata creation and let the legacy 20668 * tcp_send() path take over. We don't want 20669 * to send the tiny segment as part of this 20670 * Multidata for performance reasons; instead, 20671 * we let the legacy path deal with grouping 20672 * it with the subsequent small mblks. 20673 */ 20674 if (*usable >= len && 20675 MBLKL((*xmit_tail)->b_cont) < spill) { 20676 max_pld = 0; 20677 break; /* done */ 20678 } 20679 20680 /* 20681 * We can't spillover, and we are near 20682 * the end of the current payload buffer, 20683 * so send what's left. 20684 */ 20685 ASSERT(*tail_unsent > 0); 20686 len = *tail_unsent; 20687 } 20688 20689 /* tail_unsent is negated if there is a spillover */ 20690 *tail_unsent -= len; 20691 *usable -= len; 20692 ASSERT(*usable >= 0); 20693 20694 if (*usable < mss) 20695 seg_len = *usable; 20696 /* 20697 * Sender SWS avoidance; see comments in tcp_send(); 20698 * everything else is the same, except that we only 20699 * do this here if there is no more data to be sent 20700 * following the current xmit_tail. We don't check 20701 * for 1-byte urgent data because we shouldn't get 20702 * here if TCP_URG_VALID is set. 20703 */ 20704 if (*usable > 0 && *usable < mss && 20705 ((md_pbuf_nxt == NULL && 20706 (*xmit_tail)->b_cont == NULL) || 20707 (md_pbuf_nxt != NULL && 20708 (*xmit_tail)->b_cont->b_cont == NULL)) && 20709 seg_len < (tcp->tcp_max_swnd >> 1) && 20710 (tcp->tcp_unsent - 20711 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20712 !tcp->tcp_zero_win_probe) { 20713 if ((*snxt + len) == tcp->tcp_snxt && 20714 (*snxt + len) == tcp->tcp_suna) { 20715 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20716 } 20717 done = B_TRUE; 20718 } 20719 20720 /* 20721 * Prime pump for IP's checksumming on our behalf; 20722 * include the adjustment for a source route if any. 20723 * Do this only for software/partial hardware checksum 20724 * offload, as this field gets zeroed out later for 20725 * the full hardware checksum offload case. 20726 */ 20727 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20728 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20729 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20730 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20731 } 20732 20733 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20734 *snxt += len; 20735 20736 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20737 /* 20738 * We set the PUSH bit only if TCP has no more buffered 20739 * data to be transmitted (or if sender SWS avoidance 20740 * takes place), as opposed to setting it for every 20741 * last packet in the burst. 20742 */ 20743 if (done || 20744 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20745 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20746 20747 /* 20748 * Set FIN bit if this is our last segment; snxt 20749 * already includes its length, and it will not 20750 * be adjusted after this point. 20751 */ 20752 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20753 *snxt == tcp->tcp_fss) { 20754 if (!tcp->tcp_fin_acked) { 20755 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20756 BUMP_MIB(&tcps->tcps_mib, 20757 tcpOutControl); 20758 } 20759 if (!tcp->tcp_fin_sent) { 20760 tcp->tcp_fin_sent = B_TRUE; 20761 /* 20762 * tcp state must be ESTABLISHED 20763 * in order for us to get here in 20764 * the first place. 20765 */ 20766 tcp->tcp_state = TCPS_FIN_WAIT_1; 20767 20768 /* 20769 * Upon returning from this routine, 20770 * tcp_wput_data() will set tcp_snxt 20771 * to be equal to snxt + tcp_fin_sent. 20772 * This is essentially the same as 20773 * setting it to tcp_fss + 1. 20774 */ 20775 } 20776 } 20777 20778 tcp->tcp_last_sent_len = (ushort_t)len; 20779 20780 len += tcp_hdr_len; 20781 if (tcp->tcp_ipversion == IPV4_VERSION) 20782 tcp->tcp_ipha->ipha_length = htons(len); 20783 else 20784 tcp->tcp_ip6h->ip6_plen = htons(len - 20785 ((char *)&tcp->tcp_ip6h[1] - 20786 tcp->tcp_iphc)); 20787 20788 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20789 20790 /* setup header fragment */ 20791 PDESC_HDR_ADD(pkt_info, 20792 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20793 tcp->tcp_mdt_hdr_head, /* head room */ 20794 tcp_hdr_len, /* len */ 20795 tcp->tcp_mdt_hdr_tail); /* tail room */ 20796 20797 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20798 hdr_frag_sz); 20799 ASSERT(MBLKIN(md_hbuf, 20800 (pkt_info->hdr_base - md_hbuf->b_rptr), 20801 PDESC_HDRSIZE(pkt_info))); 20802 20803 /* setup first payload fragment */ 20804 PDESC_PLD_INIT(pkt_info); 20805 PDESC_PLD_SPAN_ADD(pkt_info, 20806 pbuf_idx, /* index */ 20807 md_pbuf->b_rptr + cur_pld_off, /* start */ 20808 tcp->tcp_last_sent_len); /* len */ 20809 20810 /* create a split-packet in case of a spillover */ 20811 if (md_pbuf_nxt != NULL) { 20812 ASSERT(spill > 0); 20813 ASSERT(pbuf_idx_nxt > pbuf_idx); 20814 ASSERT(!add_buffer); 20815 20816 md_pbuf = md_pbuf_nxt; 20817 md_pbuf_nxt = NULL; 20818 pbuf_idx = pbuf_idx_nxt; 20819 pbuf_idx_nxt = -1; 20820 cur_pld_off = spill; 20821 20822 /* trim out first payload fragment */ 20823 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20824 20825 /* setup second payload fragment */ 20826 PDESC_PLD_SPAN_ADD(pkt_info, 20827 pbuf_idx, /* index */ 20828 md_pbuf->b_rptr, /* start */ 20829 spill); /* len */ 20830 20831 if ((*xmit_tail)->b_next == NULL) { 20832 /* 20833 * Store the lbolt used for RTT 20834 * estimation. We can only record one 20835 * timestamp per mblk so we do it when 20836 * we reach the end of the payload 20837 * buffer. Also we only take a new 20838 * timestamp sample when the previous 20839 * timed data from the same mblk has 20840 * been ack'ed. 20841 */ 20842 (*xmit_tail)->b_prev = local_time; 20843 (*xmit_tail)->b_next = 20844 (mblk_t *)(uintptr_t)first_snxt; 20845 } 20846 20847 first_snxt = *snxt - spill; 20848 20849 /* 20850 * Advance xmit_tail; usable could be 0 by 20851 * the time we got here, but we made sure 20852 * above that we would only spillover to 20853 * the next data block if usable includes 20854 * the spilled-over amount prior to the 20855 * subtraction. Therefore, we are sure 20856 * that xmit_tail->b_cont can't be NULL. 20857 */ 20858 ASSERT((*xmit_tail)->b_cont != NULL); 20859 *xmit_tail = (*xmit_tail)->b_cont; 20860 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20861 (uintptr_t)INT_MAX); 20862 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20863 } else { 20864 cur_pld_off += tcp->tcp_last_sent_len; 20865 } 20866 20867 /* 20868 * Fill in the header using the template header, and 20869 * add options such as time-stamp, ECN and/or SACK, 20870 * as needed. 20871 */ 20872 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20873 (clock_t)local_time, num_sack_blk); 20874 20875 /* take care of some IP header businesses */ 20876 if (af == AF_INET) { 20877 ipha = (ipha_t *)pkt_info->hdr_rptr; 20878 20879 ASSERT(OK_32PTR((uchar_t *)ipha)); 20880 ASSERT(PDESC_HDRL(pkt_info) >= 20881 IP_SIMPLE_HDR_LENGTH); 20882 ASSERT(ipha->ipha_version_and_hdr_length == 20883 IP_SIMPLE_HDR_VERSION); 20884 20885 /* 20886 * Assign ident value for current packet; see 20887 * related comments in ip_wput_ire() about the 20888 * contract private interface with clustering 20889 * group. 20890 */ 20891 clusterwide = B_FALSE; 20892 if (cl_inet_ipident != NULL) { 20893 ASSERT(cl_inet_isclusterwide != NULL); 20894 if ((*cl_inet_isclusterwide)(stack_id, 20895 IPPROTO_IP, AF_INET, 20896 (uint8_t *)(uintptr_t)src, NULL)) { 20897 ipha->ipha_ident = 20898 (*cl_inet_ipident)(stack_id, 20899 IPPROTO_IP, AF_INET, 20900 (uint8_t *)(uintptr_t)src, 20901 (uint8_t *)(uintptr_t)dst, 20902 NULL); 20903 clusterwide = B_TRUE; 20904 } 20905 } 20906 20907 if (!clusterwide) { 20908 ipha->ipha_ident = (uint16_t) 20909 atomic_add_32_nv( 20910 &ire->ire_ident, 1); 20911 } 20912 #ifndef _BIG_ENDIAN 20913 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20914 (ipha->ipha_ident >> 8); 20915 #endif 20916 } else { 20917 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20918 20919 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20920 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20921 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20922 ASSERT(PDESC_HDRL(pkt_info) >= 20923 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20924 TCP_CHECKSUM_SIZE)); 20925 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20926 20927 if (tcp->tcp_ip_forward_progress) { 20928 rconfirm = B_TRUE; 20929 tcp->tcp_ip_forward_progress = B_FALSE; 20930 } 20931 } 20932 20933 /* at least one payload span, and at most two */ 20934 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20935 20936 /* add the packet descriptor to Multidata */ 20937 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20938 KM_NOSLEEP)) == NULL) { 20939 /* 20940 * Any failure other than ENOMEM indicates 20941 * that we have passed in invalid pkt_info 20942 * or parameters to mmd_addpdesc, which must 20943 * not happen. 20944 * 20945 * EINVAL is a result of failure on boundary 20946 * checks against the pkt_info contents. It 20947 * should not happen, and we panic because 20948 * either there's horrible heap corruption, 20949 * and/or programming mistake. 20950 */ 20951 if (err != ENOMEM) { 20952 cmn_err(CE_PANIC, "tcp_multisend: " 20953 "pdesc logic error detected for " 20954 "tcp %p mmd %p pinfo %p (%d)\n", 20955 (void *)tcp, (void *)mmd, 20956 (void *)pkt_info, err); 20957 } 20958 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20959 goto legacy_send; /* out_of_mem */ 20960 } 20961 ASSERT(pkt != NULL); 20962 20963 /* calculate IP header and TCP checksums */ 20964 if (af == AF_INET) { 20965 /* calculate pseudo-header checksum */ 20966 cksum = (dst >> 16) + (dst & 0xFFFF) + 20967 (src >> 16) + (src & 0xFFFF); 20968 20969 /* offset for TCP header checksum */ 20970 up = IPH_TCPH_CHECKSUMP(ipha, 20971 IP_SIMPLE_HDR_LENGTH); 20972 } else { 20973 up = (uint16_t *)&ip6h->ip6_src; 20974 20975 /* calculate pseudo-header checksum */ 20976 cksum = up[0] + up[1] + up[2] + up[3] + 20977 up[4] + up[5] + up[6] + up[7] + 20978 up[8] + up[9] + up[10] + up[11] + 20979 up[12] + up[13] + up[14] + up[15]; 20980 20981 /* Fold the initial sum */ 20982 cksum = (cksum & 0xffff) + (cksum >> 16); 20983 20984 up = (uint16_t *)(((uchar_t *)ip6h) + 20985 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20986 } 20987 20988 if (hwcksum_flags & HCK_FULLCKSUM) { 20989 /* clear checksum field for hardware */ 20990 *up = 0; 20991 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20992 uint32_t sum; 20993 20994 /* pseudo-header checksumming */ 20995 sum = *up + cksum + IP_TCP_CSUM_COMP; 20996 sum = (sum & 0xFFFF) + (sum >> 16); 20997 *up = (sum & 0xFFFF) + (sum >> 16); 20998 } else { 20999 /* software checksumming */ 21000 TCP_STAT(tcps, tcp_out_sw_cksum); 21001 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21002 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21003 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21004 cksum + IP_TCP_CSUM_COMP); 21005 if (*up == 0) 21006 *up = 0xFFFF; 21007 } 21008 21009 /* IPv4 header checksum */ 21010 if (af == AF_INET) { 21011 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21012 ipha->ipha_hdr_checksum = 0; 21013 } else { 21014 IP_HDR_CKSUM(ipha, cksum, 21015 ((uint32_t *)ipha)[0], 21016 ((uint16_t *)ipha)[4]); 21017 } 21018 } 21019 21020 if (af == AF_INET && 21021 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21022 af == AF_INET6 && 21023 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21024 mblk_t *mp, *mp1; 21025 uchar_t *hdr_rptr, *hdr_wptr; 21026 uchar_t *pld_rptr, *pld_wptr; 21027 21028 /* 21029 * We reconstruct a pseudo packet for the hooks 21030 * framework using mmd_transform_link(). 21031 * If it is a split packet we pullup the 21032 * payload. FW_HOOKS expects a pkt comprising 21033 * of two mblks: a header and the payload. 21034 */ 21035 if ((mp = mmd_transform_link(pkt)) == NULL) { 21036 TCP_STAT(tcps, tcp_mdt_allocfail); 21037 goto legacy_send; 21038 } 21039 21040 if (pkt_info->pld_cnt > 1) { 21041 /* split payload, more than one pld */ 21042 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21043 NULL) { 21044 freemsg(mp); 21045 TCP_STAT(tcps, 21046 tcp_mdt_allocfail); 21047 goto legacy_send; 21048 } 21049 freemsg(mp->b_cont); 21050 mp->b_cont = mp1; 21051 } else { 21052 mp1 = mp->b_cont; 21053 } 21054 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21055 21056 /* 21057 * Remember the message offsets. This is so we 21058 * can detect changes when we return from the 21059 * FW_HOOKS callbacks. 21060 */ 21061 hdr_rptr = mp->b_rptr; 21062 hdr_wptr = mp->b_wptr; 21063 pld_rptr = mp->b_cont->b_rptr; 21064 pld_wptr = mp->b_cont->b_wptr; 21065 21066 if (af == AF_INET) { 21067 DTRACE_PROBE4( 21068 ip4__physical__out__start, 21069 ill_t *, NULL, 21070 ill_t *, ill, 21071 ipha_t *, ipha, 21072 mblk_t *, mp); 21073 FW_HOOKS( 21074 ipst->ips_ip4_physical_out_event, 21075 ipst->ips_ipv4firewall_physical_out, 21076 NULL, ill, ipha, mp, mp, 0, ipst); 21077 DTRACE_PROBE1( 21078 ip4__physical__out__end, 21079 mblk_t *, mp); 21080 } else { 21081 DTRACE_PROBE4( 21082 ip6__physical__out_start, 21083 ill_t *, NULL, 21084 ill_t *, ill, 21085 ip6_t *, ip6h, 21086 mblk_t *, mp); 21087 FW_HOOKS6( 21088 ipst->ips_ip6_physical_out_event, 21089 ipst->ips_ipv6firewall_physical_out, 21090 NULL, ill, ip6h, mp, mp, 0, ipst); 21091 DTRACE_PROBE1( 21092 ip6__physical__out__end, 21093 mblk_t *, mp); 21094 } 21095 21096 if (mp == NULL || 21097 (mp1 = mp->b_cont) == NULL || 21098 mp->b_rptr != hdr_rptr || 21099 mp->b_wptr != hdr_wptr || 21100 mp1->b_rptr != pld_rptr || 21101 mp1->b_wptr != pld_wptr || 21102 mp1->b_cont != NULL) { 21103 /* 21104 * We abandon multidata processing and 21105 * return to the normal path, either 21106 * when a packet is blocked, or when 21107 * the boundaries of header buffer or 21108 * payload buffer have been changed by 21109 * FW_HOOKS[6]. 21110 */ 21111 if (mp != NULL) 21112 freemsg(mp); 21113 goto legacy_send; 21114 } 21115 /* Finished with the pseudo packet */ 21116 freemsg(mp); 21117 } 21118 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21119 ill, ipha, ip6h); 21120 /* advance header offset */ 21121 cur_hdr_off += hdr_frag_sz; 21122 21123 obbytes += tcp->tcp_last_sent_len; 21124 ++obsegs; 21125 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21126 *tail_unsent > 0); 21127 21128 if ((*xmit_tail)->b_next == NULL) { 21129 /* 21130 * Store the lbolt used for RTT estimation. We can only 21131 * record one timestamp per mblk so we do it when we 21132 * reach the end of the payload buffer. Also we only 21133 * take a new timestamp sample when the previous timed 21134 * data from the same mblk has been ack'ed. 21135 */ 21136 (*xmit_tail)->b_prev = local_time; 21137 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21138 } 21139 21140 ASSERT(*tail_unsent >= 0); 21141 if (*tail_unsent > 0) { 21142 /* 21143 * We got here because we broke out of the above 21144 * loop due to of one of the following cases: 21145 * 21146 * 1. len < adjusted MSS (i.e. small), 21147 * 2. Sender SWS avoidance, 21148 * 3. max_pld is zero. 21149 * 21150 * We are done for this Multidata, so trim our 21151 * last payload buffer (if any) accordingly. 21152 */ 21153 if (md_pbuf != NULL) 21154 md_pbuf->b_wptr -= *tail_unsent; 21155 } else if (*usable > 0) { 21156 *xmit_tail = (*xmit_tail)->b_cont; 21157 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21158 (uintptr_t)INT_MAX); 21159 *tail_unsent = (int)MBLKL(*xmit_tail); 21160 add_buffer = B_TRUE; 21161 } 21162 } while (!done && *usable > 0 && num_burst_seg > 0 && 21163 (tcp_mdt_chain || max_pld > 0)); 21164 21165 if (md_mp_head != NULL) { 21166 /* send everything down */ 21167 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21168 &rconfirm); 21169 } 21170 21171 #undef PREP_NEW_MULTIDATA 21172 #undef PREP_NEW_PBUF 21173 #undef IPVER 21174 21175 IRE_REFRELE(ire); 21176 return (0); 21177 } 21178 21179 /* 21180 * A wrapper function for sending one or more Multidata messages down to 21181 * the module below ip; this routine does not release the reference of the 21182 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21183 */ 21184 static void 21185 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21186 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21187 { 21188 uint64_t delta; 21189 nce_t *nce; 21190 tcp_stack_t *tcps = tcp->tcp_tcps; 21191 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21192 21193 ASSERT(ire != NULL && ill != NULL); 21194 ASSERT(ire->ire_stq != NULL); 21195 ASSERT(md_mp_head != NULL); 21196 ASSERT(rconfirm != NULL); 21197 21198 /* adjust MIBs and IRE timestamp */ 21199 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21200 tcp->tcp_obsegs += obsegs; 21201 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21202 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21203 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21204 21205 if (tcp->tcp_ipversion == IPV4_VERSION) { 21206 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21207 } else { 21208 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21209 } 21210 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21211 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21212 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21213 21214 ire->ire_ob_pkt_count += obsegs; 21215 if (ire->ire_ipif != NULL) 21216 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21217 ire->ire_last_used_time = lbolt; 21218 21219 if (ipst->ips_ipobs_enabled) { 21220 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21221 pdesc_t *dl_pkt; 21222 pdescinfo_t pinfo; 21223 mblk_t *nmp; 21224 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21225 21226 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21227 (dl_pkt != NULL); 21228 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21229 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21230 continue; 21231 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21232 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21233 freemsg(nmp); 21234 } 21235 } 21236 21237 /* send it down */ 21238 putnext(ire->ire_stq, md_mp_head); 21239 21240 /* we're done for TCP/IPv4 */ 21241 if (tcp->tcp_ipversion == IPV4_VERSION) 21242 return; 21243 21244 nce = ire->ire_nce; 21245 21246 ASSERT(nce != NULL); 21247 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21248 ASSERT(nce->nce_state != ND_INCOMPLETE); 21249 21250 /* reachability confirmation? */ 21251 if (*rconfirm) { 21252 nce->nce_last = TICK_TO_MSEC(lbolt64); 21253 if (nce->nce_state != ND_REACHABLE) { 21254 mutex_enter(&nce->nce_lock); 21255 nce->nce_state = ND_REACHABLE; 21256 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21257 mutex_exit(&nce->nce_lock); 21258 (void) untimeout(nce->nce_timeout_id); 21259 if (ip_debug > 2) { 21260 /* ip1dbg */ 21261 pr_addr_dbg("tcp_multisend_data: state " 21262 "for %s changed to REACHABLE\n", 21263 AF_INET6, &ire->ire_addr_v6); 21264 } 21265 } 21266 /* reset transport reachability confirmation */ 21267 *rconfirm = B_FALSE; 21268 } 21269 21270 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21271 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21272 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21273 21274 if (delta > (uint64_t)ill->ill_reachable_time) { 21275 mutex_enter(&nce->nce_lock); 21276 switch (nce->nce_state) { 21277 case ND_REACHABLE: 21278 case ND_STALE: 21279 /* 21280 * ND_REACHABLE is identical to ND_STALE in this 21281 * specific case. If reachable time has expired for 21282 * this neighbor (delta is greater than reachable 21283 * time), conceptually, the neighbor cache is no 21284 * longer in REACHABLE state, but already in STALE 21285 * state. So the correct transition here is to 21286 * ND_DELAY. 21287 */ 21288 nce->nce_state = ND_DELAY; 21289 mutex_exit(&nce->nce_lock); 21290 NDP_RESTART_TIMER(nce, 21291 ipst->ips_delay_first_probe_time); 21292 if (ip_debug > 3) { 21293 /* ip2dbg */ 21294 pr_addr_dbg("tcp_multisend_data: state " 21295 "for %s changed to DELAY\n", 21296 AF_INET6, &ire->ire_addr_v6); 21297 } 21298 break; 21299 case ND_DELAY: 21300 case ND_PROBE: 21301 mutex_exit(&nce->nce_lock); 21302 /* Timers have already started */ 21303 break; 21304 case ND_UNREACHABLE: 21305 /* 21306 * ndp timer has detected that this nce is 21307 * unreachable and initiated deleting this nce 21308 * and all its associated IREs. This is a race 21309 * where we found the ire before it was deleted 21310 * and have just sent out a packet using this 21311 * unreachable nce. 21312 */ 21313 mutex_exit(&nce->nce_lock); 21314 break; 21315 default: 21316 ASSERT(0); 21317 } 21318 } 21319 } 21320 21321 /* 21322 * Derived from tcp_send_data(). 21323 */ 21324 static void 21325 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21326 int num_lso_seg) 21327 { 21328 ipha_t *ipha; 21329 mblk_t *ire_fp_mp; 21330 uint_t ire_fp_mp_len; 21331 uint32_t hcksum_txflags = 0; 21332 ipaddr_t src; 21333 ipaddr_t dst; 21334 uint32_t cksum; 21335 uint16_t *up; 21336 tcp_stack_t *tcps = tcp->tcp_tcps; 21337 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21338 21339 ASSERT(DB_TYPE(mp) == M_DATA); 21340 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21341 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21342 ASSERT(tcp->tcp_connp != NULL); 21343 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21344 21345 ipha = (ipha_t *)mp->b_rptr; 21346 src = ipha->ipha_src; 21347 dst = ipha->ipha_dst; 21348 21349 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21350 21351 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21352 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21353 num_lso_seg); 21354 #ifndef _BIG_ENDIAN 21355 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21356 #endif 21357 if (tcp->tcp_snd_zcopy_aware) { 21358 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21359 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21360 mp = tcp_zcopy_disable(tcp, mp); 21361 } 21362 21363 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21364 ASSERT(ill->ill_hcksum_capab != NULL); 21365 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21366 } 21367 21368 /* 21369 * Since the TCP checksum should be recalculated by h/w, we can just 21370 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21371 * pseudo-header checksum for HCK_PARTIALCKSUM. 21372 * The partial pseudo-header excludes TCP length, that was calculated 21373 * in tcp_send(), so to zero *up before further processing. 21374 */ 21375 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21376 21377 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21378 *up = 0; 21379 21380 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21381 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21382 21383 /* 21384 * Append LSO flags and mss to the mp. 21385 */ 21386 lso_info_set(mp, mss, HW_LSO); 21387 21388 ipha->ipha_fragment_offset_and_flags |= 21389 (uint32_t)htons(ire->ire_frag_flag); 21390 21391 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21392 ire_fp_mp_len = MBLKL(ire_fp_mp); 21393 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21394 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21395 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21396 21397 UPDATE_OB_PKT_COUNT(ire); 21398 ire->ire_last_used_time = lbolt; 21399 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21400 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21401 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21402 ntohs(ipha->ipha_length)); 21403 21404 DTRACE_PROBE4(ip4__physical__out__start, 21405 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 21406 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21407 ipst->ips_ipv4firewall_physical_out, NULL, 21408 ill, ipha, mp, mp, 0, ipst); 21409 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21410 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 21411 21412 if (mp != NULL) { 21413 if (ipst->ips_ipobs_enabled) { 21414 zoneid_t szone; 21415 21416 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 21417 ipst, ALL_ZONES); 21418 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21419 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 21420 } 21421 21422 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 21423 } 21424 } 21425 21426 /* 21427 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21428 * scheme, and returns one of the following: 21429 * 21430 * -1 = failed allocation. 21431 * 0 = success; burst count reached, or usable send window is too small, 21432 * and that we'd rather wait until later before sending again. 21433 * 1 = success; we are called from tcp_multisend(), and both usable send 21434 * window and tail_unsent are greater than the MDT threshold, and thus 21435 * Multidata Transmit should be used instead. 21436 */ 21437 static int 21438 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21439 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21440 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21441 const int mdt_thres) 21442 { 21443 int num_burst_seg = tcp->tcp_snd_burst; 21444 ire_t *ire = NULL; 21445 ill_t *ill = NULL; 21446 mblk_t *ire_fp_mp = NULL; 21447 uint_t ire_fp_mp_len = 0; 21448 int num_lso_seg = 1; 21449 uint_t lso_usable; 21450 boolean_t do_lso_send = B_FALSE; 21451 tcp_stack_t *tcps = tcp->tcp_tcps; 21452 21453 /* 21454 * Check LSO capability before any further work. And the similar check 21455 * need to be done in for(;;) loop. 21456 * LSO will be deployed when therer is more than one mss of available 21457 * data and a burst transmission is allowed. 21458 */ 21459 if (tcp->tcp_lso && 21460 (tcp->tcp_valid_bits == 0 || 21461 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21462 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21463 /* 21464 * Try to find usable IRE/ILL and do basic check to the ILL. 21465 */ 21466 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21467 /* 21468 * Enable LSO with this transmission. 21469 * Since IRE has been hold in 21470 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21471 * should be called before return. 21472 */ 21473 do_lso_send = B_TRUE; 21474 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21475 ire_fp_mp_len = MBLKL(ire_fp_mp); 21476 /* Round up to multiple of 4 */ 21477 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21478 } else { 21479 do_lso_send = B_FALSE; 21480 ill = NULL; 21481 } 21482 } 21483 21484 for (;;) { 21485 struct datab *db; 21486 tcph_t *tcph; 21487 uint32_t sum; 21488 mblk_t *mp, *mp1; 21489 uchar_t *rptr; 21490 int len; 21491 21492 /* 21493 * If we're called by tcp_multisend(), and the amount of 21494 * sendable data as well as the size of current xmit_tail 21495 * is beyond the MDT threshold, return to the caller and 21496 * let the large data transmit be done using MDT. 21497 */ 21498 if (*usable > 0 && *usable > mdt_thres && 21499 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21500 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21501 ASSERT(tcp->tcp_mdt); 21502 return (1); /* success; do large send */ 21503 } 21504 21505 if (num_burst_seg == 0) 21506 break; /* success; burst count reached */ 21507 21508 /* 21509 * Calculate the maximum payload length we can send in *one* 21510 * time. 21511 */ 21512 if (do_lso_send) { 21513 /* 21514 * Check whether need to do LSO any more. 21515 */ 21516 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21517 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21518 lso_usable = MIN(lso_usable, 21519 num_burst_seg * mss); 21520 21521 num_lso_seg = lso_usable / mss; 21522 if (lso_usable % mss) { 21523 num_lso_seg++; 21524 tcp->tcp_last_sent_len = (ushort_t) 21525 (lso_usable % mss); 21526 } else { 21527 tcp->tcp_last_sent_len = (ushort_t)mss; 21528 } 21529 } else { 21530 do_lso_send = B_FALSE; 21531 num_lso_seg = 1; 21532 lso_usable = mss; 21533 } 21534 } 21535 21536 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21537 21538 /* 21539 * Adjust num_burst_seg here. 21540 */ 21541 num_burst_seg -= num_lso_seg; 21542 21543 len = mss; 21544 if (len > *usable) { 21545 ASSERT(do_lso_send == B_FALSE); 21546 21547 len = *usable; 21548 if (len <= 0) { 21549 /* Terminate the loop */ 21550 break; /* success; too small */ 21551 } 21552 /* 21553 * Sender silly-window avoidance. 21554 * Ignore this if we are going to send a 21555 * zero window probe out. 21556 * 21557 * TODO: force data into microscopic window? 21558 * ==> (!pushed || (unsent > usable)) 21559 */ 21560 if (len < (tcp->tcp_max_swnd >> 1) && 21561 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21562 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21563 len == 1) && (! tcp->tcp_zero_win_probe)) { 21564 /* 21565 * If the retransmit timer is not running 21566 * we start it so that we will retransmit 21567 * in the case when the the receiver has 21568 * decremented the window. 21569 */ 21570 if (*snxt == tcp->tcp_snxt && 21571 *snxt == tcp->tcp_suna) { 21572 /* 21573 * We are not supposed to send 21574 * anything. So let's wait a little 21575 * bit longer before breaking SWS 21576 * avoidance. 21577 * 21578 * What should the value be? 21579 * Suggestion: MAX(init rexmit time, 21580 * tcp->tcp_rto) 21581 */ 21582 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21583 } 21584 break; /* success; too small */ 21585 } 21586 } 21587 21588 tcph = tcp->tcp_tcph; 21589 21590 /* 21591 * The reason to adjust len here is that we need to set flags 21592 * and calculate checksum. 21593 */ 21594 if (do_lso_send) 21595 len = lso_usable; 21596 21597 *usable -= len; /* Approximate - can be adjusted later */ 21598 if (*usable > 0) 21599 tcph->th_flags[0] = TH_ACK; 21600 else 21601 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21602 21603 /* 21604 * Prime pump for IP's checksumming on our behalf 21605 * Include the adjustment for a source route if any. 21606 */ 21607 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21608 sum = (sum >> 16) + (sum & 0xFFFF); 21609 U16_TO_ABE16(sum, tcph->th_sum); 21610 21611 U32_TO_ABE32(*snxt, tcph->th_seq); 21612 21613 /* 21614 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21615 * set. For the case when TCP_FSS_VALID is the only valid 21616 * bit (normal active close), branch off only when we think 21617 * that the FIN flag needs to be set. Note for this case, 21618 * that (snxt + len) may not reflect the actual seg_len, 21619 * as len may be further reduced in tcp_xmit_mp(). If len 21620 * gets modified, we will end up here again. 21621 */ 21622 if (tcp->tcp_valid_bits != 0 && 21623 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21624 ((*snxt + len) == tcp->tcp_fss))) { 21625 uchar_t *prev_rptr; 21626 uint32_t prev_snxt = tcp->tcp_snxt; 21627 21628 if (*tail_unsent == 0) { 21629 ASSERT((*xmit_tail)->b_cont != NULL); 21630 *xmit_tail = (*xmit_tail)->b_cont; 21631 prev_rptr = (*xmit_tail)->b_rptr; 21632 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21633 (*xmit_tail)->b_rptr); 21634 } else { 21635 prev_rptr = (*xmit_tail)->b_rptr; 21636 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21637 *tail_unsent; 21638 } 21639 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21640 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21641 /* Restore tcp_snxt so we get amount sent right. */ 21642 tcp->tcp_snxt = prev_snxt; 21643 if (prev_rptr == (*xmit_tail)->b_rptr) { 21644 /* 21645 * If the previous timestamp is still in use, 21646 * don't stomp on it. 21647 */ 21648 if ((*xmit_tail)->b_next == NULL) { 21649 (*xmit_tail)->b_prev = local_time; 21650 (*xmit_tail)->b_next = 21651 (mblk_t *)(uintptr_t)(*snxt); 21652 } 21653 } else 21654 (*xmit_tail)->b_rptr = prev_rptr; 21655 21656 if (mp == NULL) { 21657 if (ire != NULL) 21658 IRE_REFRELE(ire); 21659 return (-1); 21660 } 21661 mp1 = mp->b_cont; 21662 21663 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21664 tcp->tcp_last_sent_len = (ushort_t)len; 21665 while (mp1->b_cont) { 21666 *xmit_tail = (*xmit_tail)->b_cont; 21667 (*xmit_tail)->b_prev = local_time; 21668 (*xmit_tail)->b_next = 21669 (mblk_t *)(uintptr_t)(*snxt); 21670 mp1 = mp1->b_cont; 21671 } 21672 *snxt += len; 21673 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21674 BUMP_LOCAL(tcp->tcp_obsegs); 21675 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21676 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21677 tcp_send_data(tcp, q, mp); 21678 continue; 21679 } 21680 21681 *snxt += len; /* Adjust later if we don't send all of len */ 21682 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21683 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21684 21685 if (*tail_unsent) { 21686 /* Are the bytes above us in flight? */ 21687 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21688 if (rptr != (*xmit_tail)->b_rptr) { 21689 *tail_unsent -= len; 21690 if (len <= mss) /* LSO is unusable */ 21691 tcp->tcp_last_sent_len = (ushort_t)len; 21692 len += tcp_hdr_len; 21693 if (tcp->tcp_ipversion == IPV4_VERSION) 21694 tcp->tcp_ipha->ipha_length = htons(len); 21695 else 21696 tcp->tcp_ip6h->ip6_plen = 21697 htons(len - 21698 ((char *)&tcp->tcp_ip6h[1] - 21699 tcp->tcp_iphc)); 21700 mp = dupb(*xmit_tail); 21701 if (mp == NULL) { 21702 if (ire != NULL) 21703 IRE_REFRELE(ire); 21704 return (-1); /* out_of_mem */ 21705 } 21706 mp->b_rptr = rptr; 21707 /* 21708 * If the old timestamp is no longer in use, 21709 * sample a new timestamp now. 21710 */ 21711 if ((*xmit_tail)->b_next == NULL) { 21712 (*xmit_tail)->b_prev = local_time; 21713 (*xmit_tail)->b_next = 21714 (mblk_t *)(uintptr_t)(*snxt-len); 21715 } 21716 goto must_alloc; 21717 } 21718 } else { 21719 *xmit_tail = (*xmit_tail)->b_cont; 21720 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21721 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21722 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21723 (*xmit_tail)->b_rptr); 21724 } 21725 21726 (*xmit_tail)->b_prev = local_time; 21727 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21728 21729 *tail_unsent -= len; 21730 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21731 tcp->tcp_last_sent_len = (ushort_t)len; 21732 21733 len += tcp_hdr_len; 21734 if (tcp->tcp_ipversion == IPV4_VERSION) 21735 tcp->tcp_ipha->ipha_length = htons(len); 21736 else 21737 tcp->tcp_ip6h->ip6_plen = htons(len - 21738 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21739 21740 mp = dupb(*xmit_tail); 21741 if (mp == NULL) { 21742 if (ire != NULL) 21743 IRE_REFRELE(ire); 21744 return (-1); /* out_of_mem */ 21745 } 21746 21747 len = tcp_hdr_len; 21748 /* 21749 * There are four reasons to allocate a new hdr mblk: 21750 * 1) The bytes above us are in use by another packet 21751 * 2) We don't have good alignment 21752 * 3) The mblk is being shared 21753 * 4) We don't have enough room for a header 21754 */ 21755 rptr = mp->b_rptr - len; 21756 if (!OK_32PTR(rptr) || 21757 ((db = mp->b_datap), db->db_ref != 2) || 21758 rptr < db->db_base + ire_fp_mp_len) { 21759 /* NOTE: we assume allocb returns an OK_32PTR */ 21760 21761 must_alloc:; 21762 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21763 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21764 if (mp1 == NULL) { 21765 freemsg(mp); 21766 if (ire != NULL) 21767 IRE_REFRELE(ire); 21768 return (-1); /* out_of_mem */ 21769 } 21770 mp1->b_cont = mp; 21771 mp = mp1; 21772 /* Leave room for Link Level header */ 21773 len = tcp_hdr_len; 21774 rptr = 21775 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21776 mp->b_wptr = &rptr[len]; 21777 } 21778 21779 /* 21780 * Fill in the header using the template header, and add 21781 * options such as time-stamp, ECN and/or SACK, as needed. 21782 */ 21783 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21784 21785 mp->b_rptr = rptr; 21786 21787 if (*tail_unsent) { 21788 int spill = *tail_unsent; 21789 21790 mp1 = mp->b_cont; 21791 if (mp1 == NULL) 21792 mp1 = mp; 21793 21794 /* 21795 * If we're a little short, tack on more mblks until 21796 * there is no more spillover. 21797 */ 21798 while (spill < 0) { 21799 mblk_t *nmp; 21800 int nmpsz; 21801 21802 nmp = (*xmit_tail)->b_cont; 21803 nmpsz = MBLKL(nmp); 21804 21805 /* 21806 * Excess data in mblk; can we split it? 21807 * If MDT is enabled for the connection, 21808 * keep on splitting as this is a transient 21809 * send path. 21810 */ 21811 if (!do_lso_send && !tcp->tcp_mdt && 21812 (spill + nmpsz > 0)) { 21813 /* 21814 * Don't split if stream head was 21815 * told to break up larger writes 21816 * into smaller ones. 21817 */ 21818 if (tcp->tcp_maxpsz > 0) 21819 break; 21820 21821 /* 21822 * Next mblk is less than SMSS/2 21823 * rounded up to nearest 64-byte; 21824 * let it get sent as part of the 21825 * next segment. 21826 */ 21827 if (tcp->tcp_localnet && 21828 !tcp->tcp_cork && 21829 (nmpsz < roundup((mss >> 1), 64))) 21830 break; 21831 } 21832 21833 *xmit_tail = nmp; 21834 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21835 /* Stash for rtt use later */ 21836 (*xmit_tail)->b_prev = local_time; 21837 (*xmit_tail)->b_next = 21838 (mblk_t *)(uintptr_t)(*snxt - len); 21839 mp1->b_cont = dupb(*xmit_tail); 21840 mp1 = mp1->b_cont; 21841 21842 spill += nmpsz; 21843 if (mp1 == NULL) { 21844 *tail_unsent = spill; 21845 freemsg(mp); 21846 if (ire != NULL) 21847 IRE_REFRELE(ire); 21848 return (-1); /* out_of_mem */ 21849 } 21850 } 21851 21852 /* Trim back any surplus on the last mblk */ 21853 if (spill >= 0) { 21854 mp1->b_wptr -= spill; 21855 *tail_unsent = spill; 21856 } else { 21857 /* 21858 * We did not send everything we could in 21859 * order to remain within the b_cont limit. 21860 */ 21861 *usable -= spill; 21862 *snxt += spill; 21863 tcp->tcp_last_sent_len += spill; 21864 UPDATE_MIB(&tcps->tcps_mib, 21865 tcpOutDataBytes, spill); 21866 /* 21867 * Adjust the checksum 21868 */ 21869 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21870 sum += spill; 21871 sum = (sum >> 16) + (sum & 0xFFFF); 21872 U16_TO_ABE16(sum, tcph->th_sum); 21873 if (tcp->tcp_ipversion == IPV4_VERSION) { 21874 sum = ntohs( 21875 ((ipha_t *)rptr)->ipha_length) + 21876 spill; 21877 ((ipha_t *)rptr)->ipha_length = 21878 htons(sum); 21879 } else { 21880 sum = ntohs( 21881 ((ip6_t *)rptr)->ip6_plen) + 21882 spill; 21883 ((ip6_t *)rptr)->ip6_plen = 21884 htons(sum); 21885 } 21886 *tail_unsent = 0; 21887 } 21888 } 21889 if (tcp->tcp_ip_forward_progress) { 21890 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21891 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21892 tcp->tcp_ip_forward_progress = B_FALSE; 21893 } 21894 21895 if (do_lso_send) { 21896 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21897 num_lso_seg); 21898 tcp->tcp_obsegs += num_lso_seg; 21899 21900 TCP_STAT(tcps, tcp_lso_times); 21901 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21902 } else { 21903 tcp_send_data(tcp, q, mp); 21904 BUMP_LOCAL(tcp->tcp_obsegs); 21905 } 21906 } 21907 21908 if (ire != NULL) 21909 IRE_REFRELE(ire); 21910 return (0); 21911 } 21912 21913 /* Unlink and return any mblk that looks like it contains a MDT info */ 21914 static mblk_t * 21915 tcp_mdt_info_mp(mblk_t *mp) 21916 { 21917 mblk_t *prev_mp; 21918 21919 for (;;) { 21920 prev_mp = mp; 21921 /* no more to process? */ 21922 if ((mp = mp->b_cont) == NULL) 21923 break; 21924 21925 switch (DB_TYPE(mp)) { 21926 case M_CTL: 21927 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21928 continue; 21929 ASSERT(prev_mp != NULL); 21930 prev_mp->b_cont = mp->b_cont; 21931 mp->b_cont = NULL; 21932 return (mp); 21933 default: 21934 break; 21935 } 21936 } 21937 return (mp); 21938 } 21939 21940 /* MDT info update routine, called when IP notifies us about MDT */ 21941 static void 21942 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21943 { 21944 boolean_t prev_state; 21945 tcp_stack_t *tcps = tcp->tcp_tcps; 21946 21947 /* 21948 * IP is telling us to abort MDT on this connection? We know 21949 * this because the capability is only turned off when IP 21950 * encounters some pathological cases, e.g. link-layer change 21951 * where the new driver doesn't support MDT, or in situation 21952 * where MDT usage on the link-layer has been switched off. 21953 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21954 * if the link-layer doesn't support MDT, and if it does, it 21955 * will indicate that the feature is to be turned on. 21956 */ 21957 prev_state = tcp->tcp_mdt; 21958 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21959 if (!tcp->tcp_mdt && !first) { 21960 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21961 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21962 (void *)tcp->tcp_connp)); 21963 } 21964 21965 /* 21966 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21967 * so disable MDT otherwise. The checks are done here 21968 * and in tcp_wput_data(). 21969 */ 21970 if (tcp->tcp_mdt && 21971 (tcp->tcp_ipversion == IPV4_VERSION && 21972 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21973 (tcp->tcp_ipversion == IPV6_VERSION && 21974 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21975 tcp->tcp_mdt = B_FALSE; 21976 21977 if (tcp->tcp_mdt) { 21978 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21979 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21980 "version (%d), expected version is %d", 21981 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21982 tcp->tcp_mdt = B_FALSE; 21983 return; 21984 } 21985 21986 /* 21987 * We need the driver to be able to handle at least three 21988 * spans per packet in order for tcp MDT to be utilized. 21989 * The first is for the header portion, while the rest are 21990 * needed to handle a packet that straddles across two 21991 * virtually non-contiguous buffers; a typical tcp packet 21992 * therefore consists of only two spans. Note that we take 21993 * a zero as "don't care". 21994 */ 21995 if (mdt_capab->ill_mdt_span_limit > 0 && 21996 mdt_capab->ill_mdt_span_limit < 3) { 21997 tcp->tcp_mdt = B_FALSE; 21998 return; 21999 } 22000 22001 /* a zero means driver wants default value */ 22002 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22003 tcps->tcps_mdt_max_pbufs); 22004 if (tcp->tcp_mdt_max_pld == 0) 22005 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22006 22007 /* ensure 32-bit alignment */ 22008 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22009 mdt_capab->ill_mdt_hdr_head), 4); 22010 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22011 mdt_capab->ill_mdt_hdr_tail), 4); 22012 22013 if (!first && !prev_state) { 22014 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22015 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22016 (void *)tcp->tcp_connp)); 22017 } 22018 } 22019 } 22020 22021 /* Unlink and return any mblk that looks like it contains a LSO info */ 22022 static mblk_t * 22023 tcp_lso_info_mp(mblk_t *mp) 22024 { 22025 mblk_t *prev_mp; 22026 22027 for (;;) { 22028 prev_mp = mp; 22029 /* no more to process? */ 22030 if ((mp = mp->b_cont) == NULL) 22031 break; 22032 22033 switch (DB_TYPE(mp)) { 22034 case M_CTL: 22035 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22036 continue; 22037 ASSERT(prev_mp != NULL); 22038 prev_mp->b_cont = mp->b_cont; 22039 mp->b_cont = NULL; 22040 return (mp); 22041 default: 22042 break; 22043 } 22044 } 22045 22046 return (mp); 22047 } 22048 22049 /* LSO info update routine, called when IP notifies us about LSO */ 22050 static void 22051 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22052 { 22053 tcp_stack_t *tcps = tcp->tcp_tcps; 22054 22055 /* 22056 * IP is telling us to abort LSO on this connection? We know 22057 * this because the capability is only turned off when IP 22058 * encounters some pathological cases, e.g. link-layer change 22059 * where the new NIC/driver doesn't support LSO, or in situation 22060 * where LSO usage on the link-layer has been switched off. 22061 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22062 * if the link-layer doesn't support LSO, and if it does, it 22063 * will indicate that the feature is to be turned on. 22064 */ 22065 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22066 TCP_STAT(tcps, tcp_lso_enabled); 22067 22068 /* 22069 * We currently only support LSO on simple TCP/IPv4, 22070 * so disable LSO otherwise. The checks are done here 22071 * and in tcp_wput_data(). 22072 */ 22073 if (tcp->tcp_lso && 22074 (tcp->tcp_ipversion == IPV4_VERSION && 22075 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22076 (tcp->tcp_ipversion == IPV6_VERSION)) { 22077 tcp->tcp_lso = B_FALSE; 22078 TCP_STAT(tcps, tcp_lso_disabled); 22079 } else { 22080 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22081 lso_capab->ill_lso_max); 22082 } 22083 } 22084 22085 static void 22086 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22087 { 22088 conn_t *connp = tcp->tcp_connp; 22089 tcp_stack_t *tcps = tcp->tcp_tcps; 22090 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22091 22092 ASSERT(ire != NULL); 22093 22094 /* 22095 * We may be in the fastpath here, and although we essentially do 22096 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22097 * we try to keep things as brief as possible. After all, these 22098 * are only best-effort checks, and we do more thorough ones prior 22099 * to calling tcp_send()/tcp_multisend(). 22100 */ 22101 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22102 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22103 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22104 !(ire->ire_flags & RTF_MULTIRT) && 22105 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22106 CONN_IS_LSO_MD_FASTPATH(connp)) { 22107 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22108 /* Cache the result */ 22109 connp->conn_lso_ok = B_TRUE; 22110 22111 ASSERT(ill->ill_lso_capab != NULL); 22112 if (!ill->ill_lso_capab->ill_lso_on) { 22113 ill->ill_lso_capab->ill_lso_on = 1; 22114 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22115 "LSO for interface %s\n", (void *)connp, 22116 ill->ill_name)); 22117 } 22118 tcp_lso_update(tcp, ill->ill_lso_capab); 22119 } else if (ipst->ips_ip_multidata_outbound && 22120 ILL_MDT_CAPABLE(ill)) { 22121 /* Cache the result */ 22122 connp->conn_mdt_ok = B_TRUE; 22123 22124 ASSERT(ill->ill_mdt_capab != NULL); 22125 if (!ill->ill_mdt_capab->ill_mdt_on) { 22126 ill->ill_mdt_capab->ill_mdt_on = 1; 22127 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22128 "MDT for interface %s\n", (void *)connp, 22129 ill->ill_name)); 22130 } 22131 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22132 } 22133 } 22134 22135 /* 22136 * The goal is to reduce the number of generated tcp segments by 22137 * setting the maxpsz multiplier to 0; this will have an affect on 22138 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22139 * into each packet, up to SMSS bytes. Doing this reduces the number 22140 * of outbound segments and incoming ACKs, thus allowing for better 22141 * network and system performance. In contrast the legacy behavior 22142 * may result in sending less than SMSS size, because the last mblk 22143 * for some packets may have more data than needed to make up SMSS, 22144 * and the legacy code refused to "split" it. 22145 * 22146 * We apply the new behavior on following situations: 22147 * 22148 * 1) Loopback connections, 22149 * 2) Connections in which the remote peer is not on local subnet, 22150 * 3) Local subnet connections over the bge interface (see below). 22151 * 22152 * Ideally, we would like this behavior to apply for interfaces other 22153 * than bge. However, doing so would negatively impact drivers which 22154 * perform dynamic mapping and unmapping of DMA resources, which are 22155 * increased by setting the maxpsz multiplier to 0 (more mblks per 22156 * packet will be generated by tcp). The bge driver does not suffer 22157 * from this, as it copies the mblks into pre-mapped buffers, and 22158 * therefore does not require more I/O resources than before. 22159 * 22160 * Otherwise, this behavior is present on all network interfaces when 22161 * the destination endpoint is non-local, since reducing the number 22162 * of packets in general is good for the network. 22163 * 22164 * TODO We need to remove this hard-coded conditional for bge once 22165 * a better "self-tuning" mechanism, or a way to comprehend 22166 * the driver transmit strategy is devised. Until the solution 22167 * is found and well understood, we live with this hack. 22168 */ 22169 if (!tcp_static_maxpsz && 22170 (tcp->tcp_loopback || !tcp->tcp_localnet || 22171 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22172 /* override the default value */ 22173 tcp->tcp_maxpsz = 0; 22174 22175 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22176 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22177 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22178 } 22179 22180 /* set the stream head parameters accordingly */ 22181 (void) tcp_maxpsz_set(tcp, B_TRUE); 22182 } 22183 22184 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22185 static void 22186 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22187 { 22188 uchar_t fval = *mp->b_rptr; 22189 mblk_t *tail; 22190 queue_t *q = tcp->tcp_wq; 22191 22192 /* TODO: How should flush interact with urgent data? */ 22193 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22194 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22195 /* 22196 * Flush only data that has not yet been put on the wire. If 22197 * we flush data that we have already transmitted, life, as we 22198 * know it, may come to an end. 22199 */ 22200 tail = tcp->tcp_xmit_tail; 22201 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22202 tcp->tcp_xmit_tail_unsent = 0; 22203 tcp->tcp_unsent = 0; 22204 if (tail->b_wptr != tail->b_rptr) 22205 tail = tail->b_cont; 22206 if (tail) { 22207 mblk_t **excess = &tcp->tcp_xmit_head; 22208 for (;;) { 22209 mblk_t *mp1 = *excess; 22210 if (mp1 == tail) 22211 break; 22212 tcp->tcp_xmit_tail = mp1; 22213 tcp->tcp_xmit_last = mp1; 22214 excess = &mp1->b_cont; 22215 } 22216 *excess = NULL; 22217 tcp_close_mpp(&tail); 22218 if (tcp->tcp_snd_zcopy_aware) 22219 tcp_zcopy_notify(tcp); 22220 } 22221 /* 22222 * We have no unsent data, so unsent must be less than 22223 * tcp_xmit_lowater, so re-enable flow. 22224 */ 22225 mutex_enter(&tcp->tcp_non_sq_lock); 22226 if (tcp->tcp_flow_stopped) { 22227 tcp_clrqfull(tcp); 22228 } 22229 mutex_exit(&tcp->tcp_non_sq_lock); 22230 } 22231 /* 22232 * TODO: you can't just flush these, you have to increase rwnd for one 22233 * thing. For another, how should urgent data interact? 22234 */ 22235 if (fval & FLUSHR) { 22236 *mp->b_rptr = fval & ~FLUSHW; 22237 /* XXX */ 22238 qreply(q, mp); 22239 return; 22240 } 22241 freemsg(mp); 22242 } 22243 22244 /* 22245 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22246 * messages. 22247 */ 22248 static void 22249 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22250 { 22251 mblk_t *mp1; 22252 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22253 STRUCT_HANDLE(strbuf, sb); 22254 queue_t *q = tcp->tcp_wq; 22255 int error; 22256 uint_t addrlen; 22257 22258 /* Make sure it is one of ours. */ 22259 switch (iocp->ioc_cmd) { 22260 case TI_GETMYNAME: 22261 case TI_GETPEERNAME: 22262 break; 22263 default: 22264 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22265 return; 22266 } 22267 switch (mi_copy_state(q, mp, &mp1)) { 22268 case -1: 22269 return; 22270 case MI_COPY_CASE(MI_COPY_IN, 1): 22271 break; 22272 case MI_COPY_CASE(MI_COPY_OUT, 1): 22273 /* Copy out the strbuf. */ 22274 mi_copyout(q, mp); 22275 return; 22276 case MI_COPY_CASE(MI_COPY_OUT, 2): 22277 /* All done. */ 22278 mi_copy_done(q, mp, 0); 22279 return; 22280 default: 22281 mi_copy_done(q, mp, EPROTO); 22282 return; 22283 } 22284 /* Check alignment of the strbuf */ 22285 if (!OK_32PTR(mp1->b_rptr)) { 22286 mi_copy_done(q, mp, EINVAL); 22287 return; 22288 } 22289 22290 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22291 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22292 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22293 mi_copy_done(q, mp, EINVAL); 22294 return; 22295 } 22296 22297 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22298 if (mp1 == NULL) 22299 return; 22300 22301 switch (iocp->ioc_cmd) { 22302 case TI_GETMYNAME: 22303 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 22304 break; 22305 case TI_GETPEERNAME: 22306 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22307 break; 22308 } 22309 22310 if (error != 0) { 22311 mi_copy_done(q, mp, error); 22312 } else { 22313 mp1->b_wptr += addrlen; 22314 STRUCT_FSET(sb, len, addrlen); 22315 22316 /* Copy out the address */ 22317 mi_copyout(q, mp); 22318 } 22319 } 22320 22321 static void 22322 tcp_disable_direct_sockfs(tcp_t *tcp) 22323 { 22324 #ifdef _ILP32 22325 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 22326 #else 22327 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22328 #endif 22329 /* 22330 * Insert this socket into the acceptor hash. 22331 * We might need it for T_CONN_RES message 22332 */ 22333 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22334 22335 if (tcp->tcp_fused) { 22336 /* 22337 * This is a fused loopback tcp; disable 22338 * read-side synchronous streams interface 22339 * and drain any queued data. It is okay 22340 * to do this for non-synchronous streams 22341 * fused tcp as well. 22342 */ 22343 tcp_fuse_disable_pair(tcp, B_FALSE); 22344 } 22345 tcp->tcp_issocket = B_FALSE; 22346 tcp->tcp_sodirect = NULL; 22347 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 22348 } 22349 22350 /* 22351 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22352 * messages. 22353 */ 22354 /* ARGSUSED */ 22355 static void 22356 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22357 { 22358 conn_t *connp = (conn_t *)arg; 22359 tcp_t *tcp = connp->conn_tcp; 22360 queue_t *q = tcp->tcp_wq; 22361 struct iocblk *iocp; 22362 22363 ASSERT(DB_TYPE(mp) == M_IOCTL); 22364 /* 22365 * Try and ASSERT the minimum possible references on the 22366 * conn early enough. Since we are executing on write side, 22367 * the connection is obviously not detached and that means 22368 * there is a ref each for TCP and IP. Since we are behind 22369 * the squeue, the minimum references needed are 3. If the 22370 * conn is in classifier hash list, there should be an 22371 * extra ref for that (we check both the possibilities). 22372 */ 22373 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22374 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22375 22376 iocp = (struct iocblk *)mp->b_rptr; 22377 switch (iocp->ioc_cmd) { 22378 case TCP_IOC_DEFAULT_Q: 22379 /* Wants to be the default wq. */ 22380 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22381 iocp->ioc_error = EPERM; 22382 iocp->ioc_count = 0; 22383 mp->b_datap->db_type = M_IOCACK; 22384 qreply(q, mp); 22385 return; 22386 } 22387 tcp_def_q_set(tcp, mp); 22388 return; 22389 case _SIOCSOCKFALLBACK: 22390 /* 22391 * Either sockmod is about to be popped and the socket 22392 * would now be treated as a plain stream, or a module 22393 * is about to be pushed so we could no longer use read- 22394 * side synchronous streams for fused loopback tcp. 22395 * Drain any queued data and disable direct sockfs 22396 * interface from now on. 22397 */ 22398 if (!tcp->tcp_issocket) { 22399 DB_TYPE(mp) = M_IOCNAK; 22400 iocp->ioc_error = EINVAL; 22401 } else { 22402 tcp_disable_direct_sockfs(tcp); 22403 DB_TYPE(mp) = M_IOCACK; 22404 iocp->ioc_error = 0; 22405 } 22406 iocp->ioc_count = 0; 22407 iocp->ioc_rval = 0; 22408 qreply(q, mp); 22409 return; 22410 } 22411 CALL_IP_WPUT(connp, q, mp); 22412 } 22413 22414 /* 22415 * This routine is called by tcp_wput() to handle all TPI requests. 22416 */ 22417 /* ARGSUSED */ 22418 static void 22419 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22420 { 22421 conn_t *connp = (conn_t *)arg; 22422 tcp_t *tcp = connp->conn_tcp; 22423 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22424 uchar_t *rptr; 22425 t_scalar_t type; 22426 cred_t *cr; 22427 22428 /* 22429 * Try and ASSERT the minimum possible references on the 22430 * conn early enough. Since we are executing on write side, 22431 * the connection is obviously not detached and that means 22432 * there is a ref each for TCP and IP. Since we are behind 22433 * the squeue, the minimum references needed are 3. If the 22434 * conn is in classifier hash list, there should be an 22435 * extra ref for that (we check both the possibilities). 22436 */ 22437 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22438 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22439 22440 rptr = mp->b_rptr; 22441 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22442 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22443 type = ((union T_primitives *)rptr)->type; 22444 if (type == T_EXDATA_REQ) { 22445 tcp_output_urgent(connp, mp->b_cont, arg2); 22446 freeb(mp); 22447 } else if (type != T_DATA_REQ) { 22448 goto non_urgent_data; 22449 } else { 22450 /* TODO: options, flags, ... from user */ 22451 /* Set length to zero for reclamation below */ 22452 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22453 freeb(mp); 22454 } 22455 return; 22456 } else { 22457 if (tcp->tcp_debug) { 22458 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22459 "tcp_wput_proto, dropping one..."); 22460 } 22461 freemsg(mp); 22462 return; 22463 } 22464 22465 non_urgent_data: 22466 22467 switch ((int)tprim->type) { 22468 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22469 /* 22470 * save the kssl_ent_t from the next block, and convert this 22471 * back to a normal bind_req. 22472 */ 22473 if (mp->b_cont != NULL) { 22474 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22475 22476 if (tcp->tcp_kssl_ent != NULL) { 22477 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22478 KSSL_NO_PROXY); 22479 tcp->tcp_kssl_ent = NULL; 22480 } 22481 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22482 sizeof (kssl_ent_t)); 22483 kssl_hold_ent(tcp->tcp_kssl_ent); 22484 freemsg(mp->b_cont); 22485 mp->b_cont = NULL; 22486 } 22487 tprim->type = T_BIND_REQ; 22488 22489 /* FALLTHROUGH */ 22490 case O_T_BIND_REQ: /* bind request */ 22491 case T_BIND_REQ: /* new semantics bind request */ 22492 tcp_tpi_bind(tcp, mp); 22493 break; 22494 case T_UNBIND_REQ: /* unbind request */ 22495 tcp_tpi_unbind(tcp, mp); 22496 break; 22497 case O_T_CONN_RES: /* old connection response XXX */ 22498 case T_CONN_RES: /* connection response */ 22499 tcp_tli_accept(tcp, mp); 22500 break; 22501 case T_CONN_REQ: /* connection request */ 22502 tcp_tpi_connect(tcp, mp); 22503 break; 22504 case T_DISCON_REQ: /* disconnect request */ 22505 tcp_disconnect(tcp, mp); 22506 break; 22507 case T_CAPABILITY_REQ: 22508 tcp_capability_req(tcp, mp); /* capability request */ 22509 break; 22510 case T_INFO_REQ: /* information request */ 22511 tcp_info_req(tcp, mp); 22512 break; 22513 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22514 case T_OPTMGMT_REQ: 22515 /* 22516 * Note: no support for snmpcom_req() through new 22517 * T_OPTMGMT_REQ. See comments in ip.c 22518 */ 22519 22520 /* 22521 * All Solaris components should pass a db_credp 22522 * for this TPI message, hence we ASSERT. 22523 * But in case there is some other M_PROTO that looks 22524 * like a TPI message sent by some other kernel 22525 * component, we check and return an error. 22526 */ 22527 cr = msg_getcred(mp, NULL); 22528 ASSERT(cr != NULL); 22529 if (cr == NULL) { 22530 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 22531 return; 22532 } 22533 /* 22534 * If EINPROGRESS is returned, the request has been queued 22535 * for subsequent processing by ip_restart_optmgmt(), which 22536 * will do the CONN_DEC_REF(). 22537 */ 22538 CONN_INC_REF(connp); 22539 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 22540 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22541 B_TRUE) != EINPROGRESS) { 22542 CONN_DEC_REF(connp); 22543 } 22544 } else { 22545 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22546 B_TRUE) != EINPROGRESS) { 22547 CONN_DEC_REF(connp); 22548 } 22549 } 22550 break; 22551 22552 case T_UNITDATA_REQ: /* unitdata request */ 22553 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22554 break; 22555 case T_ORDREL_REQ: /* orderly release req */ 22556 freemsg(mp); 22557 22558 if (tcp->tcp_fused) 22559 tcp_unfuse(tcp); 22560 22561 if (tcp_xmit_end(tcp) != 0) { 22562 /* 22563 * We were crossing FINs and got a reset from 22564 * the other side. Just ignore it. 22565 */ 22566 if (tcp->tcp_debug) { 22567 (void) strlog(TCP_MOD_ID, 0, 1, 22568 SL_ERROR|SL_TRACE, 22569 "tcp_wput_proto, T_ORDREL_REQ out of " 22570 "state %s", 22571 tcp_display(tcp, NULL, 22572 DISP_ADDR_AND_PORT)); 22573 } 22574 } 22575 break; 22576 case T_ADDR_REQ: 22577 tcp_addr_req(tcp, mp); 22578 break; 22579 default: 22580 if (tcp->tcp_debug) { 22581 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22582 "tcp_wput_proto, bogus TPI msg, type %d", 22583 tprim->type); 22584 } 22585 /* 22586 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22587 * to recover. 22588 */ 22589 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22590 break; 22591 } 22592 } 22593 22594 /* 22595 * The TCP write service routine should never be called... 22596 */ 22597 /* ARGSUSED */ 22598 static void 22599 tcp_wsrv(queue_t *q) 22600 { 22601 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22602 22603 TCP_STAT(tcps, tcp_wsrv_called); 22604 } 22605 22606 /* Non overlapping byte exchanger */ 22607 static void 22608 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22609 { 22610 uchar_t uch; 22611 22612 while (len-- > 0) { 22613 uch = a[len]; 22614 a[len] = b[len]; 22615 b[len] = uch; 22616 } 22617 } 22618 22619 /* 22620 * Send out a control packet on the tcp connection specified. This routine 22621 * is typically called where we need a simple ACK or RST generated. 22622 */ 22623 static void 22624 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22625 { 22626 uchar_t *rptr; 22627 tcph_t *tcph; 22628 ipha_t *ipha = NULL; 22629 ip6_t *ip6h = NULL; 22630 uint32_t sum; 22631 int tcp_hdr_len; 22632 int tcp_ip_hdr_len; 22633 mblk_t *mp; 22634 tcp_stack_t *tcps = tcp->tcp_tcps; 22635 22636 /* 22637 * Save sum for use in source route later. 22638 */ 22639 ASSERT(tcp != NULL); 22640 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22641 tcp_hdr_len = tcp->tcp_hdr_len; 22642 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22643 22644 /* If a text string is passed in with the request, pass it to strlog. */ 22645 if (str != NULL && tcp->tcp_debug) { 22646 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22647 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22648 str, seq, ack, ctl); 22649 } 22650 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22651 BPRI_MED); 22652 if (mp == NULL) { 22653 return; 22654 } 22655 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22656 mp->b_rptr = rptr; 22657 mp->b_wptr = &rptr[tcp_hdr_len]; 22658 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22659 22660 if (tcp->tcp_ipversion == IPV4_VERSION) { 22661 ipha = (ipha_t *)rptr; 22662 ipha->ipha_length = htons(tcp_hdr_len); 22663 } else { 22664 ip6h = (ip6_t *)rptr; 22665 ASSERT(tcp != NULL); 22666 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22667 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22668 } 22669 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22670 tcph->th_flags[0] = (uint8_t)ctl; 22671 if (ctl & TH_RST) { 22672 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22673 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22674 /* 22675 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22676 */ 22677 if (tcp->tcp_snd_ts_ok && 22678 tcp->tcp_state > TCPS_SYN_SENT) { 22679 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22680 *(mp->b_wptr) = TCPOPT_EOL; 22681 if (tcp->tcp_ipversion == IPV4_VERSION) { 22682 ipha->ipha_length = htons(tcp_hdr_len - 22683 TCPOPT_REAL_TS_LEN); 22684 } else { 22685 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22686 TCPOPT_REAL_TS_LEN); 22687 } 22688 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22689 sum -= TCPOPT_REAL_TS_LEN; 22690 } 22691 } 22692 if (ctl & TH_ACK) { 22693 if (tcp->tcp_snd_ts_ok) { 22694 U32_TO_BE32(lbolt, 22695 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22696 U32_TO_BE32(tcp->tcp_ts_recent, 22697 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22698 } 22699 22700 /* Update the latest receive window size in TCP header. */ 22701 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22702 tcph->th_win); 22703 tcp->tcp_rack = ack; 22704 tcp->tcp_rack_cnt = 0; 22705 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22706 } 22707 BUMP_LOCAL(tcp->tcp_obsegs); 22708 U32_TO_BE32(seq, tcph->th_seq); 22709 U32_TO_BE32(ack, tcph->th_ack); 22710 /* 22711 * Include the adjustment for a source route if any. 22712 */ 22713 sum = (sum >> 16) + (sum & 0xFFFF); 22714 U16_TO_BE16(sum, tcph->th_sum); 22715 tcp_send_data(tcp, tcp->tcp_wq, mp); 22716 } 22717 22718 /* 22719 * If this routine returns B_TRUE, TCP can generate a RST in response 22720 * to a segment. If it returns B_FALSE, TCP should not respond. 22721 */ 22722 static boolean_t 22723 tcp_send_rst_chk(tcp_stack_t *tcps) 22724 { 22725 clock_t now; 22726 22727 /* 22728 * TCP needs to protect itself from generating too many RSTs. 22729 * This can be a DoS attack by sending us random segments 22730 * soliciting RSTs. 22731 * 22732 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22733 * in each 1 second interval. In this way, TCP still generate 22734 * RSTs in normal cases but when under attack, the impact is 22735 * limited. 22736 */ 22737 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22738 now = lbolt; 22739 /* lbolt can wrap around. */ 22740 if ((tcps->tcps_last_rst_intrvl > now) || 22741 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22742 1*SECONDS)) { 22743 tcps->tcps_last_rst_intrvl = now; 22744 tcps->tcps_rst_cnt = 1; 22745 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22746 return (B_FALSE); 22747 } 22748 } 22749 return (B_TRUE); 22750 } 22751 22752 /* 22753 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22754 */ 22755 static void 22756 tcp_ip_ire_mark_advice(tcp_t *tcp) 22757 { 22758 mblk_t *mp; 22759 ipic_t *ipic; 22760 22761 if (tcp->tcp_ipversion == IPV4_VERSION) { 22762 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22763 &ipic); 22764 } else { 22765 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22766 &ipic); 22767 } 22768 if (mp == NULL) 22769 return; 22770 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22771 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22772 } 22773 22774 /* 22775 * Return an IP advice ioctl mblk and set ipic to be the pointer 22776 * to the advice structure. 22777 */ 22778 static mblk_t * 22779 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22780 { 22781 struct iocblk *ioc; 22782 mblk_t *mp, *mp1; 22783 22784 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22785 if (mp == NULL) 22786 return (NULL); 22787 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22788 *ipic = (ipic_t *)mp->b_rptr; 22789 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22790 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22791 22792 bcopy(addr, *ipic + 1, addr_len); 22793 22794 (*ipic)->ipic_addr_length = addr_len; 22795 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22796 22797 mp1 = mkiocb(IP_IOCTL); 22798 if (mp1 == NULL) { 22799 freemsg(mp); 22800 return (NULL); 22801 } 22802 mp1->b_cont = mp; 22803 ioc = (struct iocblk *)mp1->b_rptr; 22804 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22805 22806 return (mp1); 22807 } 22808 22809 /* 22810 * Generate a reset based on an inbound packet, connp is set by caller 22811 * when RST is in response to an unexpected inbound packet for which 22812 * there is active tcp state in the system. 22813 * 22814 * IPSEC NOTE : Try to send the reply with the same protection as it came 22815 * in. We still have the ipsec_mp that the packet was attached to. Thus 22816 * the packet will go out at the same level of protection as it came in by 22817 * converting the IPSEC_IN to IPSEC_OUT. 22818 */ 22819 static void 22820 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22821 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22822 tcp_stack_t *tcps, conn_t *connp) 22823 { 22824 ipha_t *ipha = NULL; 22825 ip6_t *ip6h = NULL; 22826 ushort_t len; 22827 tcph_t *tcph; 22828 int i; 22829 mblk_t *ipsec_mp; 22830 boolean_t mctl_present; 22831 ipic_t *ipic; 22832 ipaddr_t v4addr; 22833 in6_addr_t v6addr; 22834 int addr_len; 22835 void *addr; 22836 queue_t *q = tcps->tcps_g_q; 22837 tcp_t *tcp; 22838 cred_t *cr; 22839 mblk_t *nmp; 22840 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22841 22842 if (tcps->tcps_g_q == NULL) { 22843 /* 22844 * For non-zero stackids the default queue isn't created 22845 * until the first open, thus there can be a need to send 22846 * a reset before then. But we can't do that, hence we just 22847 * drop the packet. Later during boot, when the default queue 22848 * has been setup, a retransmitted packet from the peer 22849 * will result in a reset. 22850 */ 22851 ASSERT(tcps->tcps_netstack->netstack_stackid != 22852 GLOBAL_NETSTACKID); 22853 freemsg(mp); 22854 return; 22855 } 22856 22857 if (connp != NULL) 22858 tcp = connp->conn_tcp; 22859 else 22860 tcp = Q_TO_TCP(q); 22861 22862 if (!tcp_send_rst_chk(tcps)) { 22863 tcps->tcps_rst_unsent++; 22864 freemsg(mp); 22865 return; 22866 } 22867 22868 if (mp->b_datap->db_type == M_CTL) { 22869 ipsec_mp = mp; 22870 mp = mp->b_cont; 22871 mctl_present = B_TRUE; 22872 } else { 22873 ipsec_mp = mp; 22874 mctl_present = B_FALSE; 22875 } 22876 22877 if (str && q && tcps->tcps_dbg) { 22878 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22879 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22880 "flags 0x%x", 22881 str, seq, ack, ctl); 22882 } 22883 if (mp->b_datap->db_ref != 1) { 22884 mblk_t *mp1 = copyb(mp); 22885 freemsg(mp); 22886 mp = mp1; 22887 if (!mp) { 22888 if (mctl_present) 22889 freeb(ipsec_mp); 22890 return; 22891 } else { 22892 if (mctl_present) { 22893 ipsec_mp->b_cont = mp; 22894 } else { 22895 ipsec_mp = mp; 22896 } 22897 } 22898 } else if (mp->b_cont) { 22899 freemsg(mp->b_cont); 22900 mp->b_cont = NULL; 22901 } 22902 /* 22903 * We skip reversing source route here. 22904 * (for now we replace all IP options with EOL) 22905 */ 22906 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22907 ipha = (ipha_t *)mp->b_rptr; 22908 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22909 mp->b_rptr[i] = IPOPT_EOL; 22910 /* 22911 * Make sure that src address isn't flagrantly invalid. 22912 * Not all broadcast address checking for the src address 22913 * is possible, since we don't know the netmask of the src 22914 * addr. No check for destination address is done, since 22915 * IP will not pass up a packet with a broadcast dest 22916 * address to TCP. Similar checks are done below for IPv6. 22917 */ 22918 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22919 CLASSD(ipha->ipha_src)) { 22920 freemsg(ipsec_mp); 22921 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22922 return; 22923 } 22924 } else { 22925 ip6h = (ip6_t *)mp->b_rptr; 22926 22927 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22928 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22929 freemsg(ipsec_mp); 22930 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22931 return; 22932 } 22933 22934 /* Remove any extension headers assuming partial overlay */ 22935 if (ip_hdr_len > IPV6_HDR_LEN) { 22936 uint8_t *to; 22937 22938 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22939 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22940 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22941 ip_hdr_len = IPV6_HDR_LEN; 22942 ip6h = (ip6_t *)mp->b_rptr; 22943 ip6h->ip6_nxt = IPPROTO_TCP; 22944 } 22945 } 22946 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22947 if (tcph->th_flags[0] & TH_RST) { 22948 freemsg(ipsec_mp); 22949 return; 22950 } 22951 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22952 len = ip_hdr_len + sizeof (tcph_t); 22953 mp->b_wptr = &mp->b_rptr[len]; 22954 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22955 ipha->ipha_length = htons(len); 22956 /* Swap addresses */ 22957 v4addr = ipha->ipha_src; 22958 ipha->ipha_src = ipha->ipha_dst; 22959 ipha->ipha_dst = v4addr; 22960 ipha->ipha_ident = 0; 22961 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22962 addr_len = IP_ADDR_LEN; 22963 addr = &v4addr; 22964 } else { 22965 /* No ip6i_t in this case */ 22966 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22967 /* Swap addresses */ 22968 v6addr = ip6h->ip6_src; 22969 ip6h->ip6_src = ip6h->ip6_dst; 22970 ip6h->ip6_dst = v6addr; 22971 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22972 addr_len = IPV6_ADDR_LEN; 22973 addr = &v6addr; 22974 } 22975 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22976 U32_TO_BE32(ack, tcph->th_ack); 22977 U32_TO_BE32(seq, tcph->th_seq); 22978 U16_TO_BE16(0, tcph->th_win); 22979 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22980 tcph->th_flags[0] = (uint8_t)ctl; 22981 if (ctl & TH_RST) { 22982 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22983 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22984 } 22985 22986 /* IP trusts us to set up labels when required. */ 22987 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22988 crgetlabel(cr) != NULL) { 22989 int err; 22990 22991 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22992 err = tsol_check_label(cr, &mp, 22993 tcp->tcp_connp->conn_mac_exempt, 22994 tcps->tcps_netstack->netstack_ip); 22995 else 22996 err = tsol_check_label_v6(cr, &mp, 22997 tcp->tcp_connp->conn_mac_exempt, 22998 tcps->tcps_netstack->netstack_ip); 22999 if (mctl_present) 23000 ipsec_mp->b_cont = mp; 23001 else 23002 ipsec_mp = mp; 23003 if (err != 0) { 23004 freemsg(ipsec_mp); 23005 return; 23006 } 23007 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23008 ipha = (ipha_t *)mp->b_rptr; 23009 } else { 23010 ip6h = (ip6_t *)mp->b_rptr; 23011 } 23012 } 23013 23014 if (mctl_present) { 23015 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23016 23017 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23018 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23019 return; 23020 } 23021 } 23022 if (zoneid == ALL_ZONES) 23023 zoneid = GLOBAL_ZONEID; 23024 23025 /* Add the zoneid so ip_output routes it properly */ 23026 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23027 freemsg(ipsec_mp); 23028 return; 23029 } 23030 ipsec_mp = nmp; 23031 23032 /* 23033 * NOTE: one might consider tracing a TCP packet here, but 23034 * this function has no active TCP state and no tcp structure 23035 * that has a trace buffer. If we traced here, we would have 23036 * to keep a local trace buffer in tcp_record_trace(). 23037 * 23038 * TSol note: The mblk that contains the incoming packet was 23039 * reused by tcp_xmit_listener_reset, so it already contains 23040 * the right credentials and we don't need to call mblk_setcred. 23041 * Also the conn's cred is not right since it is associated 23042 * with tcps_g_q. 23043 */ 23044 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23045 23046 /* 23047 * Tell IP to mark the IRE used for this destination temporary. 23048 * This way, we can limit our exposure to DoS attack because IP 23049 * creates an IRE for each destination. If there are too many, 23050 * the time to do any routing lookup will be extremely long. And 23051 * the lookup can be in interrupt context. 23052 * 23053 * Note that in normal circumstances, this marking should not 23054 * affect anything. It would be nice if only 1 message is 23055 * needed to inform IP that the IRE created for this RST should 23056 * not be added to the cache table. But there is currently 23057 * not such communication mechanism between TCP and IP. So 23058 * the best we can do now is to send the advice ioctl to IP 23059 * to mark the IRE temporary. 23060 */ 23061 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23062 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23063 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23064 } 23065 } 23066 23067 /* 23068 * Initiate closedown sequence on an active connection. (May be called as 23069 * writer.) Return value zero for OK return, non-zero for error return. 23070 */ 23071 static int 23072 tcp_xmit_end(tcp_t *tcp) 23073 { 23074 ipic_t *ipic; 23075 mblk_t *mp; 23076 tcp_stack_t *tcps = tcp->tcp_tcps; 23077 23078 if (tcp->tcp_state < TCPS_SYN_RCVD || 23079 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23080 /* 23081 * Invalid state, only states TCPS_SYN_RCVD, 23082 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23083 */ 23084 return (-1); 23085 } 23086 23087 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23088 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23089 /* 23090 * If there is nothing more unsent, send the FIN now. 23091 * Otherwise, it will go out with the last segment. 23092 */ 23093 if (tcp->tcp_unsent == 0) { 23094 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23095 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23096 23097 if (mp) { 23098 tcp_send_data(tcp, tcp->tcp_wq, mp); 23099 } else { 23100 /* 23101 * Couldn't allocate msg. Pretend we got it out. 23102 * Wait for rexmit timeout. 23103 */ 23104 tcp->tcp_snxt = tcp->tcp_fss + 1; 23105 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23106 } 23107 23108 /* 23109 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23110 * changed. 23111 */ 23112 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23113 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23114 } 23115 } else { 23116 /* 23117 * If tcp->tcp_cork is set, then the data will not get sent, 23118 * so we have to check that and unset it first. 23119 */ 23120 if (tcp->tcp_cork) 23121 tcp->tcp_cork = B_FALSE; 23122 tcp_wput_data(tcp, NULL, B_FALSE); 23123 } 23124 23125 /* 23126 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23127 * is 0, don't update the cache. 23128 */ 23129 if (tcps->tcps_rtt_updates == 0 || 23130 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23131 return (0); 23132 23133 /* 23134 * NOTE: should not update if source routes i.e. if tcp_remote if 23135 * different from the destination. 23136 */ 23137 if (tcp->tcp_ipversion == IPV4_VERSION) { 23138 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23139 return (0); 23140 } 23141 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23142 &ipic); 23143 } else { 23144 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23145 &tcp->tcp_ip6h->ip6_dst))) { 23146 return (0); 23147 } 23148 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23149 &ipic); 23150 } 23151 23152 /* Record route attributes in the IRE for use by future connections. */ 23153 if (mp == NULL) 23154 return (0); 23155 23156 /* 23157 * We do not have a good algorithm to update ssthresh at this time. 23158 * So don't do any update. 23159 */ 23160 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23161 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23162 23163 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23164 23165 return (0); 23166 } 23167 23168 /* ARGSUSED */ 23169 void 23170 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 23171 { 23172 conn_t *connp = (conn_t *)arg; 23173 mblk_t *mp1; 23174 tcp_t *tcp = connp->conn_tcp; 23175 tcp_xmit_reset_event_t *eventp; 23176 23177 ASSERT(mp->b_datap->db_type == M_PROTO && 23178 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 23179 23180 if (tcp->tcp_state != TCPS_LISTEN) { 23181 freemsg(mp); 23182 return; 23183 } 23184 23185 mp1 = mp->b_cont; 23186 mp->b_cont = NULL; 23187 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 23188 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 23189 connp->conn_netstack); 23190 23191 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 23192 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 23193 freemsg(mp); 23194 } 23195 23196 /* 23197 * Generate a "no listener here" RST in response to an "unknown" segment. 23198 * connp is set by caller when RST is in response to an unexpected 23199 * inbound packet for which there is active tcp state in the system. 23200 * Note that we are reusing the incoming mp to construct the outgoing RST. 23201 */ 23202 void 23203 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23204 tcp_stack_t *tcps, conn_t *connp) 23205 { 23206 uchar_t *rptr; 23207 uint32_t seg_len; 23208 tcph_t *tcph; 23209 uint32_t seg_seq; 23210 uint32_t seg_ack; 23211 uint_t flags; 23212 mblk_t *ipsec_mp; 23213 ipha_t *ipha; 23214 ip6_t *ip6h; 23215 boolean_t mctl_present = B_FALSE; 23216 boolean_t check = B_TRUE; 23217 boolean_t policy_present; 23218 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23219 23220 TCP_STAT(tcps, tcp_no_listener); 23221 23222 ipsec_mp = mp; 23223 23224 if (mp->b_datap->db_type == M_CTL) { 23225 ipsec_in_t *ii; 23226 23227 mctl_present = B_TRUE; 23228 mp = mp->b_cont; 23229 23230 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23231 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23232 if (ii->ipsec_in_dont_check) { 23233 check = B_FALSE; 23234 if (!ii->ipsec_in_secure) { 23235 freeb(ipsec_mp); 23236 mctl_present = B_FALSE; 23237 ipsec_mp = mp; 23238 } 23239 } 23240 } 23241 23242 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23243 policy_present = ipss->ipsec_inbound_v4_policy_present; 23244 ipha = (ipha_t *)mp->b_rptr; 23245 ip6h = NULL; 23246 } else { 23247 policy_present = ipss->ipsec_inbound_v6_policy_present; 23248 ipha = NULL; 23249 ip6h = (ip6_t *)mp->b_rptr; 23250 } 23251 23252 if (check && policy_present) { 23253 /* 23254 * The conn_t parameter is NULL because we already know 23255 * nobody's home. 23256 */ 23257 ipsec_mp = ipsec_check_global_policy( 23258 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23259 tcps->tcps_netstack); 23260 if (ipsec_mp == NULL) 23261 return; 23262 } 23263 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23264 DTRACE_PROBE2( 23265 tx__ip__log__error__nolistener__tcp, 23266 char *, "Could not reply with RST to mp(1)", 23267 mblk_t *, mp); 23268 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23269 freemsg(ipsec_mp); 23270 return; 23271 } 23272 23273 rptr = mp->b_rptr; 23274 23275 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23276 seg_seq = BE32_TO_U32(tcph->th_seq); 23277 seg_ack = BE32_TO_U32(tcph->th_ack); 23278 flags = tcph->th_flags[0]; 23279 23280 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23281 if (flags & TH_RST) { 23282 freemsg(ipsec_mp); 23283 } else if (flags & TH_ACK) { 23284 tcp_xmit_early_reset("no tcp, reset", 23285 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23286 connp); 23287 } else { 23288 if (flags & TH_SYN) { 23289 seg_len++; 23290 } else { 23291 /* 23292 * Here we violate the RFC. Note that a normal 23293 * TCP will never send a segment without the ACK 23294 * flag, except for RST or SYN segment. This 23295 * segment is neither. Just drop it on the 23296 * floor. 23297 */ 23298 freemsg(ipsec_mp); 23299 tcps->tcps_rst_unsent++; 23300 return; 23301 } 23302 23303 tcp_xmit_early_reset("no tcp, reset/ack", 23304 ipsec_mp, 0, seg_seq + seg_len, 23305 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23306 } 23307 } 23308 23309 /* 23310 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23311 * ip and tcp header ready to pass down to IP. If the mp passed in is 23312 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23313 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23314 * otherwise it will dup partial mblks.) 23315 * Otherwise, an appropriate ACK packet will be generated. This 23316 * routine is not usually called to send new data for the first time. It 23317 * is mostly called out of the timer for retransmits, and to generate ACKs. 23318 * 23319 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23320 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23321 * of the original mblk chain will be returned in *offset and *end_mp. 23322 */ 23323 mblk_t * 23324 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23325 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23326 boolean_t rexmit) 23327 { 23328 int data_length; 23329 int32_t off = 0; 23330 uint_t flags; 23331 mblk_t *mp1; 23332 mblk_t *mp2; 23333 uchar_t *rptr; 23334 tcph_t *tcph; 23335 int32_t num_sack_blk = 0; 23336 int32_t sack_opt_len = 0; 23337 tcp_stack_t *tcps = tcp->tcp_tcps; 23338 23339 /* Allocate for our maximum TCP header + link-level */ 23340 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23341 tcps->tcps_wroff_xtra, BPRI_MED); 23342 if (!mp1) 23343 return (NULL); 23344 data_length = 0; 23345 23346 /* 23347 * Note that tcp_mss has been adjusted to take into account the 23348 * timestamp option if applicable. Because SACK options do not 23349 * appear in every TCP segments and they are of variable lengths, 23350 * they cannot be included in tcp_mss. Thus we need to calculate 23351 * the actual segment length when we need to send a segment which 23352 * includes SACK options. 23353 */ 23354 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23355 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23356 tcp->tcp_num_sack_blk); 23357 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23358 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23359 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23360 max_to_send -= sack_opt_len; 23361 } 23362 23363 if (offset != NULL) { 23364 off = *offset; 23365 /* We use offset as an indicator that end_mp is not NULL. */ 23366 *end_mp = NULL; 23367 } 23368 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23369 /* This could be faster with cooperation from downstream */ 23370 if (mp2 != mp1 && !sendall && 23371 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23372 max_to_send) 23373 /* 23374 * Don't send the next mblk since the whole mblk 23375 * does not fit. 23376 */ 23377 break; 23378 mp2->b_cont = dupb(mp); 23379 mp2 = mp2->b_cont; 23380 if (!mp2) { 23381 freemsg(mp1); 23382 return (NULL); 23383 } 23384 mp2->b_rptr += off; 23385 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23386 (uintptr_t)INT_MAX); 23387 23388 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23389 if (data_length > max_to_send) { 23390 mp2->b_wptr -= data_length - max_to_send; 23391 data_length = max_to_send; 23392 off = mp2->b_wptr - mp->b_rptr; 23393 break; 23394 } else { 23395 off = 0; 23396 } 23397 } 23398 if (offset != NULL) { 23399 *offset = off; 23400 *end_mp = mp; 23401 } 23402 if (seg_len != NULL) { 23403 *seg_len = data_length; 23404 } 23405 23406 /* Update the latest receive window size in TCP header. */ 23407 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23408 tcp->tcp_tcph->th_win); 23409 23410 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23411 mp1->b_rptr = rptr; 23412 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23413 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23414 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23415 U32_TO_ABE32(seq, tcph->th_seq); 23416 23417 /* 23418 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23419 * that this function was called from tcp_wput_data. Thus, when called 23420 * to retransmit data the setting of the PUSH bit may appear some 23421 * what random in that it might get set when it should not. This 23422 * should not pose any performance issues. 23423 */ 23424 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23425 tcp->tcp_unsent == data_length)) { 23426 flags = TH_ACK | TH_PUSH; 23427 } else { 23428 flags = TH_ACK; 23429 } 23430 23431 if (tcp->tcp_ecn_ok) { 23432 if (tcp->tcp_ecn_echo_on) 23433 flags |= TH_ECE; 23434 23435 /* 23436 * Only set ECT bit and ECN_CWR if a segment contains new data. 23437 * There is no TCP flow control for non-data segments, and 23438 * only data segment is transmitted reliably. 23439 */ 23440 if (data_length > 0 && !rexmit) { 23441 SET_ECT(tcp, rptr); 23442 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23443 flags |= TH_CWR; 23444 tcp->tcp_ecn_cwr_sent = B_TRUE; 23445 } 23446 } 23447 } 23448 23449 if (tcp->tcp_valid_bits) { 23450 uint32_t u1; 23451 23452 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23453 seq == tcp->tcp_iss) { 23454 uchar_t *wptr; 23455 23456 /* 23457 * If TCP_ISS_VALID and the seq number is tcp_iss, 23458 * TCP can only be in SYN-SENT, SYN-RCVD or 23459 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23460 * our SYN is not ack'ed but the app closes this 23461 * TCP connection. 23462 */ 23463 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23464 tcp->tcp_state == TCPS_SYN_RCVD || 23465 tcp->tcp_state == TCPS_FIN_WAIT_1); 23466 23467 /* 23468 * Tack on the MSS option. It is always needed 23469 * for both active and passive open. 23470 * 23471 * MSS option value should be interface MTU - MIN 23472 * TCP/IP header according to RFC 793 as it means 23473 * the maximum segment size TCP can receive. But 23474 * to get around some broken middle boxes/end hosts 23475 * out there, we allow the option value to be the 23476 * same as the MSS option size on the peer side. 23477 * In this way, the other side will not send 23478 * anything larger than they can receive. 23479 * 23480 * Note that for SYN_SENT state, the ndd param 23481 * tcp_use_smss_as_mss_opt has no effect as we 23482 * don't know the peer's MSS option value. So 23483 * the only case we need to take care of is in 23484 * SYN_RCVD state, which is done later. 23485 */ 23486 wptr = mp1->b_wptr; 23487 wptr[0] = TCPOPT_MAXSEG; 23488 wptr[1] = TCPOPT_MAXSEG_LEN; 23489 wptr += 2; 23490 u1 = tcp->tcp_if_mtu - 23491 (tcp->tcp_ipversion == IPV4_VERSION ? 23492 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23493 TCP_MIN_HEADER_LENGTH; 23494 U16_TO_BE16(u1, wptr); 23495 mp1->b_wptr = wptr + 2; 23496 /* Update the offset to cover the additional word */ 23497 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23498 23499 /* 23500 * Note that the following way of filling in 23501 * TCP options are not optimal. Some NOPs can 23502 * be saved. But there is no need at this time 23503 * to optimize it. When it is needed, we will 23504 * do it. 23505 */ 23506 switch (tcp->tcp_state) { 23507 case TCPS_SYN_SENT: 23508 flags = TH_SYN; 23509 23510 if (tcp->tcp_snd_ts_ok) { 23511 uint32_t llbolt = (uint32_t)lbolt; 23512 23513 wptr = mp1->b_wptr; 23514 wptr[0] = TCPOPT_NOP; 23515 wptr[1] = TCPOPT_NOP; 23516 wptr[2] = TCPOPT_TSTAMP; 23517 wptr[3] = TCPOPT_TSTAMP_LEN; 23518 wptr += 4; 23519 U32_TO_BE32(llbolt, wptr); 23520 wptr += 4; 23521 ASSERT(tcp->tcp_ts_recent == 0); 23522 U32_TO_BE32(0L, wptr); 23523 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23524 tcph->th_offset_and_rsrvd[0] += 23525 (3 << 4); 23526 } 23527 23528 /* 23529 * Set up all the bits to tell other side 23530 * we are ECN capable. 23531 */ 23532 if (tcp->tcp_ecn_ok) { 23533 flags |= (TH_ECE | TH_CWR); 23534 } 23535 break; 23536 case TCPS_SYN_RCVD: 23537 flags |= TH_SYN; 23538 23539 /* 23540 * Reset the MSS option value to be SMSS 23541 * We should probably add back the bytes 23542 * for timestamp option and IPsec. We 23543 * don't do that as this is a workaround 23544 * for broken middle boxes/end hosts, it 23545 * is better for us to be more cautious. 23546 * They may not take these things into 23547 * account in their SMSS calculation. Thus 23548 * the peer's calculated SMSS may be smaller 23549 * than what it can be. This should be OK. 23550 */ 23551 if (tcps->tcps_use_smss_as_mss_opt) { 23552 u1 = tcp->tcp_mss; 23553 U16_TO_BE16(u1, wptr); 23554 } 23555 23556 /* 23557 * If the other side is ECN capable, reply 23558 * that we are also ECN capable. 23559 */ 23560 if (tcp->tcp_ecn_ok) 23561 flags |= TH_ECE; 23562 break; 23563 default: 23564 /* 23565 * The above ASSERT() makes sure that this 23566 * must be FIN-WAIT-1 state. Our SYN has 23567 * not been ack'ed so retransmit it. 23568 */ 23569 flags |= TH_SYN; 23570 break; 23571 } 23572 23573 if (tcp->tcp_snd_ws_ok) { 23574 wptr = mp1->b_wptr; 23575 wptr[0] = TCPOPT_NOP; 23576 wptr[1] = TCPOPT_WSCALE; 23577 wptr[2] = TCPOPT_WS_LEN; 23578 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23579 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23580 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23581 } 23582 23583 if (tcp->tcp_snd_sack_ok) { 23584 wptr = mp1->b_wptr; 23585 wptr[0] = TCPOPT_NOP; 23586 wptr[1] = TCPOPT_NOP; 23587 wptr[2] = TCPOPT_SACK_PERMITTED; 23588 wptr[3] = TCPOPT_SACK_OK_LEN; 23589 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23590 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23591 } 23592 23593 /* allocb() of adequate mblk assures space */ 23594 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23595 (uintptr_t)INT_MAX); 23596 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23597 /* 23598 * Get IP set to checksum on our behalf 23599 * Include the adjustment for a source route if any. 23600 */ 23601 u1 += tcp->tcp_sum; 23602 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23603 U16_TO_BE16(u1, tcph->th_sum); 23604 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23605 } 23606 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23607 (seq + data_length) == tcp->tcp_fss) { 23608 if (!tcp->tcp_fin_acked) { 23609 flags |= TH_FIN; 23610 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23611 } 23612 if (!tcp->tcp_fin_sent) { 23613 tcp->tcp_fin_sent = B_TRUE; 23614 switch (tcp->tcp_state) { 23615 case TCPS_SYN_RCVD: 23616 case TCPS_ESTABLISHED: 23617 tcp->tcp_state = TCPS_FIN_WAIT_1; 23618 break; 23619 case TCPS_CLOSE_WAIT: 23620 tcp->tcp_state = TCPS_LAST_ACK; 23621 break; 23622 } 23623 if (tcp->tcp_suna == tcp->tcp_snxt) 23624 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23625 tcp->tcp_snxt = tcp->tcp_fss + 1; 23626 } 23627 } 23628 /* 23629 * Note the trick here. u1 is unsigned. When tcp_urg 23630 * is smaller than seq, u1 will become a very huge value. 23631 * So the comparison will fail. Also note that tcp_urp 23632 * should be positive, see RFC 793 page 17. 23633 */ 23634 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23635 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23636 u1 < (uint32_t)(64 * 1024)) { 23637 flags |= TH_URG; 23638 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23639 U32_TO_ABE16(u1, tcph->th_urp); 23640 } 23641 } 23642 tcph->th_flags[0] = (uchar_t)flags; 23643 tcp->tcp_rack = tcp->tcp_rnxt; 23644 tcp->tcp_rack_cnt = 0; 23645 23646 if (tcp->tcp_snd_ts_ok) { 23647 if (tcp->tcp_state != TCPS_SYN_SENT) { 23648 uint32_t llbolt = (uint32_t)lbolt; 23649 23650 U32_TO_BE32(llbolt, 23651 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23652 U32_TO_BE32(tcp->tcp_ts_recent, 23653 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23654 } 23655 } 23656 23657 if (num_sack_blk > 0) { 23658 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23659 sack_blk_t *tmp; 23660 int32_t i; 23661 23662 wptr[0] = TCPOPT_NOP; 23663 wptr[1] = TCPOPT_NOP; 23664 wptr[2] = TCPOPT_SACK; 23665 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23666 sizeof (sack_blk_t); 23667 wptr += TCPOPT_REAL_SACK_LEN; 23668 23669 tmp = tcp->tcp_sack_list; 23670 for (i = 0; i < num_sack_blk; i++) { 23671 U32_TO_BE32(tmp[i].begin, wptr); 23672 wptr += sizeof (tcp_seq); 23673 U32_TO_BE32(tmp[i].end, wptr); 23674 wptr += sizeof (tcp_seq); 23675 } 23676 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23677 } 23678 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23679 data_length += (int)(mp1->b_wptr - rptr); 23680 if (tcp->tcp_ipversion == IPV4_VERSION) { 23681 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23682 } else { 23683 ip6_t *ip6 = (ip6_t *)(rptr + 23684 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23685 sizeof (ip6i_t) : 0)); 23686 23687 ip6->ip6_plen = htons(data_length - 23688 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23689 } 23690 23691 /* 23692 * Prime pump for IP 23693 * Include the adjustment for a source route if any. 23694 */ 23695 data_length -= tcp->tcp_ip_hdr_len; 23696 data_length += tcp->tcp_sum; 23697 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23698 U16_TO_ABE16(data_length, tcph->th_sum); 23699 if (tcp->tcp_ip_forward_progress) { 23700 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23701 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23702 tcp->tcp_ip_forward_progress = B_FALSE; 23703 } 23704 return (mp1); 23705 } 23706 23707 /* This function handles the push timeout. */ 23708 void 23709 tcp_push_timer(void *arg) 23710 { 23711 conn_t *connp = (conn_t *)arg; 23712 tcp_t *tcp = connp->conn_tcp; 23713 uint_t flags; 23714 sodirect_t *sodp; 23715 23716 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23717 23718 ASSERT(tcp->tcp_listener == NULL); 23719 23720 ASSERT(!IPCL_IS_NONSTR(connp)); 23721 23722 /* 23723 * We need to plug synchronous streams during our drain to prevent 23724 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23725 */ 23726 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23727 tcp->tcp_push_tid = 0; 23728 23729 SOD_PTR_ENTER(tcp, sodp); 23730 if (sodp != NULL) { 23731 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23732 /* sod_wakeup() does the mutex_exit() */ 23733 } else if (tcp->tcp_rcv_list != NULL) { 23734 flags = tcp_rcv_drain(tcp); 23735 } 23736 if (flags == TH_ACK_NEEDED) 23737 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23738 23739 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23740 } 23741 23742 /* 23743 * This function handles delayed ACK timeout. 23744 */ 23745 static void 23746 tcp_ack_timer(void *arg) 23747 { 23748 conn_t *connp = (conn_t *)arg; 23749 tcp_t *tcp = connp->conn_tcp; 23750 mblk_t *mp; 23751 tcp_stack_t *tcps = tcp->tcp_tcps; 23752 23753 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23754 23755 tcp->tcp_ack_tid = 0; 23756 23757 if (tcp->tcp_fused) 23758 return; 23759 23760 /* 23761 * Do not send ACK if there is no outstanding unack'ed data. 23762 */ 23763 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23764 return; 23765 } 23766 23767 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23768 /* 23769 * Make sure we don't allow deferred ACKs to result in 23770 * timer-based ACKing. If we have held off an ACK 23771 * when there was more than an mss here, and the timer 23772 * goes off, we have to worry about the possibility 23773 * that the sender isn't doing slow-start, or is out 23774 * of step with us for some other reason. We fall 23775 * permanently back in the direction of 23776 * ACK-every-other-packet as suggested in RFC 1122. 23777 */ 23778 if (tcp->tcp_rack_abs_max > 2) 23779 tcp->tcp_rack_abs_max--; 23780 tcp->tcp_rack_cur_max = 2; 23781 } 23782 mp = tcp_ack_mp(tcp); 23783 23784 if (mp != NULL) { 23785 BUMP_LOCAL(tcp->tcp_obsegs); 23786 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23787 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23788 tcp_send_data(tcp, tcp->tcp_wq, mp); 23789 } 23790 } 23791 23792 23793 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23794 static mblk_t * 23795 tcp_ack_mp(tcp_t *tcp) 23796 { 23797 uint32_t seq_no; 23798 tcp_stack_t *tcps = tcp->tcp_tcps; 23799 23800 /* 23801 * There are a few cases to be considered while setting the sequence no. 23802 * Essentially, we can come here while processing an unacceptable pkt 23803 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23804 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23805 * If we are here for a zero window probe, stick with suna. In all 23806 * other cases, we check if suna + swnd encompasses snxt and set 23807 * the sequence number to snxt, if so. If snxt falls outside the 23808 * window (the receiver probably shrunk its window), we will go with 23809 * suna + swnd, otherwise the sequence no will be unacceptable to the 23810 * receiver. 23811 */ 23812 if (tcp->tcp_zero_win_probe) { 23813 seq_no = tcp->tcp_suna; 23814 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23815 ASSERT(tcp->tcp_swnd == 0); 23816 seq_no = tcp->tcp_snxt; 23817 } else { 23818 seq_no = SEQ_GT(tcp->tcp_snxt, 23819 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23820 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23821 } 23822 23823 if (tcp->tcp_valid_bits) { 23824 /* 23825 * For the complex case where we have to send some 23826 * controls (FIN or SYN), let tcp_xmit_mp do it. 23827 */ 23828 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23829 NULL, B_FALSE)); 23830 } else { 23831 /* Generate a simple ACK */ 23832 int data_length; 23833 uchar_t *rptr; 23834 tcph_t *tcph; 23835 mblk_t *mp1; 23836 int32_t tcp_hdr_len; 23837 int32_t tcp_tcp_hdr_len; 23838 int32_t num_sack_blk = 0; 23839 int32_t sack_opt_len; 23840 23841 /* 23842 * Allocate space for TCP + IP headers 23843 * and link-level header 23844 */ 23845 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23846 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23847 tcp->tcp_num_sack_blk); 23848 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23849 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23850 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23851 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23852 } else { 23853 tcp_hdr_len = tcp->tcp_hdr_len; 23854 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23855 } 23856 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23857 if (!mp1) 23858 return (NULL); 23859 23860 /* Update the latest receive window size in TCP header. */ 23861 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23862 tcp->tcp_tcph->th_win); 23863 /* copy in prototype TCP + IP header */ 23864 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23865 mp1->b_rptr = rptr; 23866 mp1->b_wptr = rptr + tcp_hdr_len; 23867 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23868 23869 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23870 23871 /* Set the TCP sequence number. */ 23872 U32_TO_ABE32(seq_no, tcph->th_seq); 23873 23874 /* Set up the TCP flag field. */ 23875 tcph->th_flags[0] = (uchar_t)TH_ACK; 23876 if (tcp->tcp_ecn_echo_on) 23877 tcph->th_flags[0] |= TH_ECE; 23878 23879 tcp->tcp_rack = tcp->tcp_rnxt; 23880 tcp->tcp_rack_cnt = 0; 23881 23882 /* fill in timestamp option if in use */ 23883 if (tcp->tcp_snd_ts_ok) { 23884 uint32_t llbolt = (uint32_t)lbolt; 23885 23886 U32_TO_BE32(llbolt, 23887 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23888 U32_TO_BE32(tcp->tcp_ts_recent, 23889 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23890 } 23891 23892 /* Fill in SACK options */ 23893 if (num_sack_blk > 0) { 23894 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23895 sack_blk_t *tmp; 23896 int32_t i; 23897 23898 wptr[0] = TCPOPT_NOP; 23899 wptr[1] = TCPOPT_NOP; 23900 wptr[2] = TCPOPT_SACK; 23901 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23902 sizeof (sack_blk_t); 23903 wptr += TCPOPT_REAL_SACK_LEN; 23904 23905 tmp = tcp->tcp_sack_list; 23906 for (i = 0; i < num_sack_blk; i++) { 23907 U32_TO_BE32(tmp[i].begin, wptr); 23908 wptr += sizeof (tcp_seq); 23909 U32_TO_BE32(tmp[i].end, wptr); 23910 wptr += sizeof (tcp_seq); 23911 } 23912 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23913 << 4); 23914 } 23915 23916 if (tcp->tcp_ipversion == IPV4_VERSION) { 23917 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23918 } else { 23919 /* Check for ip6i_t header in sticky hdrs */ 23920 ip6_t *ip6 = (ip6_t *)(rptr + 23921 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23922 sizeof (ip6i_t) : 0)); 23923 23924 ip6->ip6_plen = htons(tcp_hdr_len - 23925 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23926 } 23927 23928 /* 23929 * Prime pump for checksum calculation in IP. Include the 23930 * adjustment for a source route if any. 23931 */ 23932 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23933 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23934 U16_TO_ABE16(data_length, tcph->th_sum); 23935 23936 if (tcp->tcp_ip_forward_progress) { 23937 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23938 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23939 tcp->tcp_ip_forward_progress = B_FALSE; 23940 } 23941 return (mp1); 23942 } 23943 } 23944 23945 /* 23946 * Hash list insertion routine for tcp_t structures. Each hash bucket 23947 * contains a list of tcp_t entries, and each entry is bound to a unique 23948 * port. If there are multiple tcp_t's that are bound to the same port, then 23949 * one of them will be linked into the hash bucket list, and the rest will 23950 * hang off of that one entry. For each port, entries bound to a specific IP 23951 * address will be inserted before those those bound to INADDR_ANY. 23952 */ 23953 static void 23954 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23955 { 23956 tcp_t **tcpp; 23957 tcp_t *tcpnext; 23958 tcp_t *tcphash; 23959 23960 if (tcp->tcp_ptpbhn != NULL) { 23961 ASSERT(!caller_holds_lock); 23962 tcp_bind_hash_remove(tcp); 23963 } 23964 tcpp = &tbf->tf_tcp; 23965 if (!caller_holds_lock) { 23966 mutex_enter(&tbf->tf_lock); 23967 } else { 23968 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23969 } 23970 tcphash = tcpp[0]; 23971 tcpnext = NULL; 23972 if (tcphash != NULL) { 23973 /* Look for an entry using the same port */ 23974 while ((tcphash = tcpp[0]) != NULL && 23975 tcp->tcp_lport != tcphash->tcp_lport) 23976 tcpp = &(tcphash->tcp_bind_hash); 23977 23978 /* The port was not found, just add to the end */ 23979 if (tcphash == NULL) 23980 goto insert; 23981 23982 /* 23983 * OK, there already exists an entry bound to the 23984 * same port. 23985 * 23986 * If the new tcp bound to the INADDR_ANY address 23987 * and the first one in the list is not bound to 23988 * INADDR_ANY we skip all entries until we find the 23989 * first one bound to INADDR_ANY. 23990 * This makes sure that applications binding to a 23991 * specific address get preference over those binding to 23992 * INADDR_ANY. 23993 */ 23994 tcpnext = tcphash; 23995 tcphash = NULL; 23996 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23997 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23998 while ((tcpnext = tcpp[0]) != NULL && 23999 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 24000 tcpp = &(tcpnext->tcp_bind_hash_port); 24001 24002 if (tcpnext) { 24003 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 24004 tcphash = tcpnext->tcp_bind_hash; 24005 if (tcphash != NULL) { 24006 tcphash->tcp_ptpbhn = 24007 &(tcp->tcp_bind_hash); 24008 tcpnext->tcp_bind_hash = NULL; 24009 } 24010 } 24011 } else { 24012 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 24013 tcphash = tcpnext->tcp_bind_hash; 24014 if (tcphash != NULL) { 24015 tcphash->tcp_ptpbhn = 24016 &(tcp->tcp_bind_hash); 24017 tcpnext->tcp_bind_hash = NULL; 24018 } 24019 } 24020 } 24021 insert: 24022 tcp->tcp_bind_hash_port = tcpnext; 24023 tcp->tcp_bind_hash = tcphash; 24024 tcp->tcp_ptpbhn = tcpp; 24025 tcpp[0] = tcp; 24026 if (!caller_holds_lock) 24027 mutex_exit(&tbf->tf_lock); 24028 } 24029 24030 /* 24031 * Hash list removal routine for tcp_t structures. 24032 */ 24033 static void 24034 tcp_bind_hash_remove(tcp_t *tcp) 24035 { 24036 tcp_t *tcpnext; 24037 kmutex_t *lockp; 24038 tcp_stack_t *tcps = tcp->tcp_tcps; 24039 24040 if (tcp->tcp_ptpbhn == NULL) 24041 return; 24042 24043 /* 24044 * Extract the lock pointer in case there are concurrent 24045 * hash_remove's for this instance. 24046 */ 24047 ASSERT(tcp->tcp_lport != 0); 24048 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24049 24050 ASSERT(lockp != NULL); 24051 mutex_enter(lockp); 24052 if (tcp->tcp_ptpbhn) { 24053 tcpnext = tcp->tcp_bind_hash_port; 24054 if (tcpnext != NULL) { 24055 tcp->tcp_bind_hash_port = NULL; 24056 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24057 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 24058 if (tcpnext->tcp_bind_hash != NULL) { 24059 tcpnext->tcp_bind_hash->tcp_ptpbhn = 24060 &(tcpnext->tcp_bind_hash); 24061 tcp->tcp_bind_hash = NULL; 24062 } 24063 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 24064 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24065 tcp->tcp_bind_hash = NULL; 24066 } 24067 *tcp->tcp_ptpbhn = tcpnext; 24068 tcp->tcp_ptpbhn = NULL; 24069 } 24070 mutex_exit(lockp); 24071 } 24072 24073 24074 /* 24075 * Hash list lookup routine for tcp_t structures. 24076 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24077 */ 24078 static tcp_t * 24079 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24080 { 24081 tf_t *tf; 24082 tcp_t *tcp; 24083 24084 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24085 mutex_enter(&tf->tf_lock); 24086 for (tcp = tf->tf_tcp; tcp != NULL; 24087 tcp = tcp->tcp_acceptor_hash) { 24088 if (tcp->tcp_acceptor_id == id) { 24089 CONN_INC_REF(tcp->tcp_connp); 24090 mutex_exit(&tf->tf_lock); 24091 return (tcp); 24092 } 24093 } 24094 mutex_exit(&tf->tf_lock); 24095 return (NULL); 24096 } 24097 24098 24099 /* 24100 * Hash list insertion routine for tcp_t structures. 24101 */ 24102 void 24103 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24104 { 24105 tf_t *tf; 24106 tcp_t **tcpp; 24107 tcp_t *tcpnext; 24108 tcp_stack_t *tcps = tcp->tcp_tcps; 24109 24110 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24111 24112 if (tcp->tcp_ptpahn != NULL) 24113 tcp_acceptor_hash_remove(tcp); 24114 tcpp = &tf->tf_tcp; 24115 mutex_enter(&tf->tf_lock); 24116 tcpnext = tcpp[0]; 24117 if (tcpnext) 24118 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24119 tcp->tcp_acceptor_hash = tcpnext; 24120 tcp->tcp_ptpahn = tcpp; 24121 tcpp[0] = tcp; 24122 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24123 mutex_exit(&tf->tf_lock); 24124 } 24125 24126 /* 24127 * Hash list removal routine for tcp_t structures. 24128 */ 24129 static void 24130 tcp_acceptor_hash_remove(tcp_t *tcp) 24131 { 24132 tcp_t *tcpnext; 24133 kmutex_t *lockp; 24134 24135 /* 24136 * Extract the lock pointer in case there are concurrent 24137 * hash_remove's for this instance. 24138 */ 24139 lockp = tcp->tcp_acceptor_lockp; 24140 24141 if (tcp->tcp_ptpahn == NULL) 24142 return; 24143 24144 ASSERT(lockp != NULL); 24145 mutex_enter(lockp); 24146 if (tcp->tcp_ptpahn) { 24147 tcpnext = tcp->tcp_acceptor_hash; 24148 if (tcpnext) { 24149 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24150 tcp->tcp_acceptor_hash = NULL; 24151 } 24152 *tcp->tcp_ptpahn = tcpnext; 24153 tcp->tcp_ptpahn = NULL; 24154 } 24155 mutex_exit(lockp); 24156 tcp->tcp_acceptor_lockp = NULL; 24157 } 24158 24159 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24160 24161 static ipaddr_t netmasks[] = { 24162 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24163 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24164 }; 24165 24166 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24167 24168 /* 24169 * XXX This routine should go away and instead we should use the metrics 24170 * associated with the routes to determine the default sndspace and rcvspace. 24171 */ 24172 static tcp_hsp_t * 24173 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24174 { 24175 tcp_hsp_t *hsp = NULL; 24176 24177 /* Quick check without acquiring the lock. */ 24178 if (tcps->tcps_hsp_hash == NULL) 24179 return (NULL); 24180 24181 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24182 24183 /* This routine finds the best-matching HSP for address addr. */ 24184 24185 if (tcps->tcps_hsp_hash) { 24186 int i; 24187 ipaddr_t srchaddr; 24188 tcp_hsp_t *hsp_net; 24189 24190 /* We do three passes: host, network, and subnet. */ 24191 24192 srchaddr = addr; 24193 24194 for (i = 1; i <= 3; i++) { 24195 /* Look for exact match on srchaddr */ 24196 24197 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24198 while (hsp) { 24199 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24200 hsp->tcp_hsp_addr == srchaddr) 24201 break; 24202 hsp = hsp->tcp_hsp_next; 24203 } 24204 ASSERT(hsp == NULL || 24205 hsp->tcp_hsp_vers == IPV4_VERSION); 24206 24207 /* 24208 * If this is the first pass: 24209 * If we found a match, great, return it. 24210 * If not, search for the network on the second pass. 24211 */ 24212 24213 if (i == 1) 24214 if (hsp) 24215 break; 24216 else 24217 { 24218 srchaddr = addr & netmask(addr); 24219 continue; 24220 } 24221 24222 /* 24223 * If this is the second pass: 24224 * If we found a match, but there's a subnet mask, 24225 * save the match but try again using the subnet 24226 * mask on the third pass. 24227 * Otherwise, return whatever we found. 24228 */ 24229 24230 if (i == 2) { 24231 if (hsp && hsp->tcp_hsp_subnet) { 24232 hsp_net = hsp; 24233 srchaddr = addr & hsp->tcp_hsp_subnet; 24234 continue; 24235 } else { 24236 break; 24237 } 24238 } 24239 24240 /* 24241 * This must be the third pass. If we didn't find 24242 * anything, return the saved network HSP instead. 24243 */ 24244 24245 if (!hsp) 24246 hsp = hsp_net; 24247 } 24248 } 24249 24250 rw_exit(&tcps->tcps_hsp_lock); 24251 return (hsp); 24252 } 24253 24254 /* 24255 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24256 * match lookup. 24257 */ 24258 static tcp_hsp_t * 24259 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24260 { 24261 tcp_hsp_t *hsp = NULL; 24262 24263 /* Quick check without acquiring the lock. */ 24264 if (tcps->tcps_hsp_hash == NULL) 24265 return (NULL); 24266 24267 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24268 24269 /* This routine finds the best-matching HSP for address addr. */ 24270 24271 if (tcps->tcps_hsp_hash) { 24272 int i; 24273 in6_addr_t v6srchaddr; 24274 tcp_hsp_t *hsp_net; 24275 24276 /* We do three passes: host, network, and subnet. */ 24277 24278 v6srchaddr = *v6addr; 24279 24280 for (i = 1; i <= 3; i++) { 24281 /* Look for exact match on srchaddr */ 24282 24283 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24284 V4_PART_OF_V6(v6srchaddr))]; 24285 while (hsp) { 24286 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24287 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24288 &v6srchaddr)) 24289 break; 24290 hsp = hsp->tcp_hsp_next; 24291 } 24292 24293 /* 24294 * If this is the first pass: 24295 * If we found a match, great, return it. 24296 * If not, search for the network on the second pass. 24297 */ 24298 24299 if (i == 1) 24300 if (hsp) 24301 break; 24302 else { 24303 /* Assume a 64 bit mask */ 24304 v6srchaddr.s6_addr32[0] = 24305 v6addr->s6_addr32[0]; 24306 v6srchaddr.s6_addr32[1] = 24307 v6addr->s6_addr32[1]; 24308 v6srchaddr.s6_addr32[2] = 0; 24309 v6srchaddr.s6_addr32[3] = 0; 24310 continue; 24311 } 24312 24313 /* 24314 * If this is the second pass: 24315 * If we found a match, but there's a subnet mask, 24316 * save the match but try again using the subnet 24317 * mask on the third pass. 24318 * Otherwise, return whatever we found. 24319 */ 24320 24321 if (i == 2) { 24322 ASSERT(hsp == NULL || 24323 hsp->tcp_hsp_vers == IPV6_VERSION); 24324 if (hsp && 24325 !IN6_IS_ADDR_UNSPECIFIED( 24326 &hsp->tcp_hsp_subnet_v6)) { 24327 hsp_net = hsp; 24328 V6_MASK_COPY(*v6addr, 24329 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24330 continue; 24331 } else { 24332 break; 24333 } 24334 } 24335 24336 /* 24337 * This must be the third pass. If we didn't find 24338 * anything, return the saved network HSP instead. 24339 */ 24340 24341 if (!hsp) 24342 hsp = hsp_net; 24343 } 24344 } 24345 24346 rw_exit(&tcps->tcps_hsp_lock); 24347 return (hsp); 24348 } 24349 24350 /* 24351 * Type three generator adapted from the random() function in 4.4 BSD: 24352 */ 24353 24354 /* 24355 * Copyright (c) 1983, 1993 24356 * The Regents of the University of California. All rights reserved. 24357 * 24358 * Redistribution and use in source and binary forms, with or without 24359 * modification, are permitted provided that the following conditions 24360 * are met: 24361 * 1. Redistributions of source code must retain the above copyright 24362 * notice, this list of conditions and the following disclaimer. 24363 * 2. Redistributions in binary form must reproduce the above copyright 24364 * notice, this list of conditions and the following disclaimer in the 24365 * documentation and/or other materials provided with the distribution. 24366 * 3. All advertising materials mentioning features or use of this software 24367 * must display the following acknowledgement: 24368 * This product includes software developed by the University of 24369 * California, Berkeley and its contributors. 24370 * 4. Neither the name of the University nor the names of its contributors 24371 * may be used to endorse or promote products derived from this software 24372 * without specific prior written permission. 24373 * 24374 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24375 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24376 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24377 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24378 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24379 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24380 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24381 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24382 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24383 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24384 * SUCH DAMAGE. 24385 */ 24386 24387 /* Type 3 -- x**31 + x**3 + 1 */ 24388 #define DEG_3 31 24389 #define SEP_3 3 24390 24391 24392 /* Protected by tcp_random_lock */ 24393 static int tcp_randtbl[DEG_3 + 1]; 24394 24395 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24396 static int *tcp_random_rptr = &tcp_randtbl[1]; 24397 24398 static int *tcp_random_state = &tcp_randtbl[1]; 24399 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24400 24401 kmutex_t tcp_random_lock; 24402 24403 void 24404 tcp_random_init(void) 24405 { 24406 int i; 24407 hrtime_t hrt; 24408 time_t wallclock; 24409 uint64_t result; 24410 24411 /* 24412 * Use high-res timer and current time for seed. Gethrtime() returns 24413 * a longlong, which may contain resolution down to nanoseconds. 24414 * The current time will either be a 32-bit or a 64-bit quantity. 24415 * XOR the two together in a 64-bit result variable. 24416 * Convert the result to a 32-bit value by multiplying the high-order 24417 * 32-bits by the low-order 32-bits. 24418 */ 24419 24420 hrt = gethrtime(); 24421 (void) drv_getparm(TIME, &wallclock); 24422 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24423 mutex_enter(&tcp_random_lock); 24424 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24425 (result & 0xffffffff); 24426 24427 for (i = 1; i < DEG_3; i++) 24428 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24429 + 12345; 24430 tcp_random_fptr = &tcp_random_state[SEP_3]; 24431 tcp_random_rptr = &tcp_random_state[0]; 24432 mutex_exit(&tcp_random_lock); 24433 for (i = 0; i < 10 * DEG_3; i++) 24434 (void) tcp_random(); 24435 } 24436 24437 /* 24438 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24439 * This range is selected to be approximately centered on TCP_ISS / 2, 24440 * and easy to compute. We get this value by generating a 32-bit random 24441 * number, selecting out the high-order 17 bits, and then adding one so 24442 * that we never return zero. 24443 */ 24444 int 24445 tcp_random(void) 24446 { 24447 int i; 24448 24449 mutex_enter(&tcp_random_lock); 24450 *tcp_random_fptr += *tcp_random_rptr; 24451 24452 /* 24453 * The high-order bits are more random than the low-order bits, 24454 * so we select out the high-order 17 bits and add one so that 24455 * we never return zero. 24456 */ 24457 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24458 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24459 tcp_random_fptr = tcp_random_state; 24460 ++tcp_random_rptr; 24461 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24462 tcp_random_rptr = tcp_random_state; 24463 24464 mutex_exit(&tcp_random_lock); 24465 return (i); 24466 } 24467 24468 static int 24469 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24470 int *t_errorp, int *sys_errorp) 24471 { 24472 int error; 24473 int is_absreq_failure; 24474 t_scalar_t *opt_lenp; 24475 t_scalar_t opt_offset; 24476 int prim_type; 24477 struct T_conn_req *tcreqp; 24478 struct T_conn_res *tcresp; 24479 cred_t *cr; 24480 24481 /* 24482 * All Solaris components should pass a db_credp 24483 * for this TPI message, hence we ASSERT. 24484 * But in case there is some other M_PROTO that looks 24485 * like a TPI message sent by some other kernel 24486 * component, we check and return an error. 24487 */ 24488 cr = msg_getcred(mp, NULL); 24489 ASSERT(cr != NULL); 24490 if (cr == NULL) 24491 return (-1); 24492 24493 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24494 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24495 prim_type == T_CONN_RES); 24496 24497 switch (prim_type) { 24498 case T_CONN_REQ: 24499 tcreqp = (struct T_conn_req *)mp->b_rptr; 24500 opt_offset = tcreqp->OPT_offset; 24501 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24502 break; 24503 case O_T_CONN_RES: 24504 case T_CONN_RES: 24505 tcresp = (struct T_conn_res *)mp->b_rptr; 24506 opt_offset = tcresp->OPT_offset; 24507 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24508 break; 24509 } 24510 24511 *t_errorp = 0; 24512 *sys_errorp = 0; 24513 *do_disconnectp = 0; 24514 24515 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24516 opt_offset, cr, &tcp_opt_obj, 24517 NULL, &is_absreq_failure); 24518 24519 switch (error) { 24520 case 0: /* no error */ 24521 ASSERT(is_absreq_failure == 0); 24522 return (0); 24523 case ENOPROTOOPT: 24524 *t_errorp = TBADOPT; 24525 break; 24526 case EACCES: 24527 *t_errorp = TACCES; 24528 break; 24529 default: 24530 *t_errorp = TSYSERR; *sys_errorp = error; 24531 break; 24532 } 24533 if (is_absreq_failure != 0) { 24534 /* 24535 * The connection request should get the local ack 24536 * T_OK_ACK and then a T_DISCON_IND. 24537 */ 24538 *do_disconnectp = 1; 24539 } 24540 return (-1); 24541 } 24542 24543 /* 24544 * Split this function out so that if the secret changes, I'm okay. 24545 * 24546 * Initialize the tcp_iss_cookie and tcp_iss_key. 24547 */ 24548 24549 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24550 24551 static void 24552 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24553 { 24554 struct { 24555 int32_t current_time; 24556 uint32_t randnum; 24557 uint16_t pad; 24558 uint8_t ether[6]; 24559 uint8_t passwd[PASSWD_SIZE]; 24560 } tcp_iss_cookie; 24561 time_t t; 24562 24563 /* 24564 * Start with the current absolute time. 24565 */ 24566 (void) drv_getparm(TIME, &t); 24567 tcp_iss_cookie.current_time = t; 24568 24569 /* 24570 * XXX - Need a more random number per RFC 1750, not this crap. 24571 * OTOH, if what follows is pretty random, then I'm in better shape. 24572 */ 24573 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24574 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24575 24576 /* 24577 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24578 * as a good template. 24579 */ 24580 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24581 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24582 24583 /* 24584 * The pass-phrase. Normally this is supplied by user-called NDD. 24585 */ 24586 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24587 24588 /* 24589 * See 4010593 if this section becomes a problem again, 24590 * but the local ethernet address is useful here. 24591 */ 24592 (void) localetheraddr(NULL, 24593 (struct ether_addr *)&tcp_iss_cookie.ether); 24594 24595 /* 24596 * Hash 'em all together. The MD5Final is called per-connection. 24597 */ 24598 mutex_enter(&tcps->tcps_iss_key_lock); 24599 MD5Init(&tcps->tcps_iss_key); 24600 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24601 sizeof (tcp_iss_cookie)); 24602 mutex_exit(&tcps->tcps_iss_key_lock); 24603 } 24604 24605 /* 24606 * Set the RFC 1948 pass phrase 24607 */ 24608 /* ARGSUSED */ 24609 static int 24610 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24611 cred_t *cr) 24612 { 24613 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24614 24615 /* 24616 * Basically, value contains a new pass phrase. Pass it along! 24617 */ 24618 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24619 return (0); 24620 } 24621 24622 /* ARGSUSED */ 24623 static int 24624 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24625 { 24626 bzero(buf, sizeof (tcp_sack_info_t)); 24627 return (0); 24628 } 24629 24630 /* ARGSUSED */ 24631 static int 24632 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24633 { 24634 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24635 return (0); 24636 } 24637 24638 /* 24639 * Make sure we wait until the default queue is setup, yet allow 24640 * tcp_g_q_create() to open a TCP stream. 24641 * We need to allow tcp_g_q_create() do do an open 24642 * of tcp, hence we compare curhread. 24643 * All others have to wait until the tcps_g_q has been 24644 * setup. 24645 */ 24646 void 24647 tcp_g_q_setup(tcp_stack_t *tcps) 24648 { 24649 mutex_enter(&tcps->tcps_g_q_lock); 24650 if (tcps->tcps_g_q != NULL) { 24651 mutex_exit(&tcps->tcps_g_q_lock); 24652 return; 24653 } 24654 if (tcps->tcps_g_q_creator == NULL) { 24655 /* This thread will set it up */ 24656 tcps->tcps_g_q_creator = curthread; 24657 mutex_exit(&tcps->tcps_g_q_lock); 24658 tcp_g_q_create(tcps); 24659 mutex_enter(&tcps->tcps_g_q_lock); 24660 ASSERT(tcps->tcps_g_q_creator == curthread); 24661 tcps->tcps_g_q_creator = NULL; 24662 cv_signal(&tcps->tcps_g_q_cv); 24663 ASSERT(tcps->tcps_g_q != NULL); 24664 mutex_exit(&tcps->tcps_g_q_lock); 24665 return; 24666 } 24667 /* Everybody but the creator has to wait */ 24668 if (tcps->tcps_g_q_creator != curthread) { 24669 while (tcps->tcps_g_q == NULL) 24670 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24671 } 24672 mutex_exit(&tcps->tcps_g_q_lock); 24673 } 24674 24675 #define IP "ip" 24676 24677 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24678 24679 /* 24680 * Create a default tcp queue here instead of in strplumb 24681 */ 24682 void 24683 tcp_g_q_create(tcp_stack_t *tcps) 24684 { 24685 int error; 24686 ldi_handle_t lh = NULL; 24687 ldi_ident_t li = NULL; 24688 int rval; 24689 cred_t *cr; 24690 major_t IP_MAJ; 24691 24692 #ifdef NS_DEBUG 24693 (void) printf("tcp_g_q_create()\n"); 24694 #endif 24695 24696 IP_MAJ = ddi_name_to_major(IP); 24697 24698 ASSERT(tcps->tcps_g_q_creator == curthread); 24699 24700 error = ldi_ident_from_major(IP_MAJ, &li); 24701 if (error) { 24702 #ifdef DEBUG 24703 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24704 error); 24705 #endif 24706 return; 24707 } 24708 24709 cr = zone_get_kcred(netstackid_to_zoneid( 24710 tcps->tcps_netstack->netstack_stackid)); 24711 ASSERT(cr != NULL); 24712 /* 24713 * We set the tcp default queue to IPv6 because IPv4 falls 24714 * back to IPv6 when it can't find a client, but 24715 * IPv6 does not fall back to IPv4. 24716 */ 24717 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24718 if (error) { 24719 #ifdef DEBUG 24720 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24721 error); 24722 #endif 24723 goto out; 24724 } 24725 24726 /* 24727 * This ioctl causes the tcp framework to cache a pointer to 24728 * this stream, so we don't want to close the stream after 24729 * this operation. 24730 * Use the kernel credentials that are for the zone we're in. 24731 */ 24732 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24733 (intptr_t)0, FKIOCTL, cr, &rval); 24734 if (error) { 24735 #ifdef DEBUG 24736 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24737 "error %d\n", error); 24738 #endif 24739 goto out; 24740 } 24741 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24742 lh = NULL; 24743 out: 24744 /* Close layered handles */ 24745 if (li) 24746 ldi_ident_release(li); 24747 /* Keep cred around until _inactive needs it */ 24748 tcps->tcps_g_q_cr = cr; 24749 } 24750 24751 /* 24752 * We keep tcp_g_q set until all other tcp_t's in the zone 24753 * has gone away, and then when tcp_g_q_inactive() is called 24754 * we clear it. 24755 */ 24756 void 24757 tcp_g_q_destroy(tcp_stack_t *tcps) 24758 { 24759 #ifdef NS_DEBUG 24760 (void) printf("tcp_g_q_destroy()for stack %d\n", 24761 tcps->tcps_netstack->netstack_stackid); 24762 #endif 24763 24764 if (tcps->tcps_g_q == NULL) { 24765 return; /* Nothing to cleanup */ 24766 } 24767 /* 24768 * Drop reference corresponding to the default queue. 24769 * This reference was added from tcp_open when the default queue 24770 * was created, hence we compensate for this extra drop in 24771 * tcp_g_q_close. If the refcnt drops to zero here it means 24772 * the default queue was the last one to be open, in which 24773 * case, then tcp_g_q_inactive will be 24774 * called as a result of the refrele. 24775 */ 24776 TCPS_REFRELE(tcps); 24777 } 24778 24779 /* 24780 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24781 * Run by tcp_q_q_inactive using a taskq. 24782 */ 24783 static void 24784 tcp_g_q_close(void *arg) 24785 { 24786 tcp_stack_t *tcps = arg; 24787 int error; 24788 ldi_handle_t lh = NULL; 24789 ldi_ident_t li = NULL; 24790 cred_t *cr; 24791 major_t IP_MAJ; 24792 24793 IP_MAJ = ddi_name_to_major(IP); 24794 24795 #ifdef NS_DEBUG 24796 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24797 tcps->tcps_netstack->netstack_stackid, 24798 tcps->tcps_netstack->netstack_refcnt); 24799 #endif 24800 lh = tcps->tcps_g_q_lh; 24801 if (lh == NULL) 24802 return; /* Nothing to cleanup */ 24803 24804 ASSERT(tcps->tcps_refcnt == 1); 24805 ASSERT(tcps->tcps_g_q != NULL); 24806 24807 error = ldi_ident_from_major(IP_MAJ, &li); 24808 if (error) { 24809 #ifdef DEBUG 24810 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24811 error); 24812 #endif 24813 return; 24814 } 24815 24816 cr = tcps->tcps_g_q_cr; 24817 tcps->tcps_g_q_cr = NULL; 24818 ASSERT(cr != NULL); 24819 24820 /* 24821 * Make sure we can break the recursion when tcp_close decrements 24822 * the reference count causing g_q_inactive to be called again. 24823 */ 24824 tcps->tcps_g_q_lh = NULL; 24825 24826 /* close the default queue */ 24827 (void) ldi_close(lh, FREAD|FWRITE, cr); 24828 /* 24829 * At this point in time tcps and the rest of netstack_t might 24830 * have been deleted. 24831 */ 24832 tcps = NULL; 24833 24834 /* Close layered handles */ 24835 ldi_ident_release(li); 24836 crfree(cr); 24837 } 24838 24839 /* 24840 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24841 * 24842 * Have to ensure that the ldi routines are not used by an 24843 * interrupt thread by using a taskq. 24844 */ 24845 void 24846 tcp_g_q_inactive(tcp_stack_t *tcps) 24847 { 24848 if (tcps->tcps_g_q_lh == NULL) 24849 return; /* Nothing to cleanup */ 24850 24851 ASSERT(tcps->tcps_refcnt == 0); 24852 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24853 24854 if (servicing_interrupt()) { 24855 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24856 (void *) tcps, TQ_SLEEP); 24857 } else { 24858 tcp_g_q_close(tcps); 24859 } 24860 } 24861 24862 /* 24863 * Called by IP when IP is loaded into the kernel 24864 */ 24865 void 24866 tcp_ddi_g_init(void) 24867 { 24868 tcp_timercache = kmem_cache_create("tcp_timercache", 24869 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24870 NULL, NULL, NULL, NULL, NULL, 0); 24871 24872 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24873 sizeof (tcp_sack_info_t), 0, 24874 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24875 24876 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24877 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24878 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24879 24880 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24881 24882 /* Initialize the random number generator */ 24883 tcp_random_init(); 24884 24885 /* A single callback independently of how many netstacks we have */ 24886 ip_squeue_init(tcp_squeue_add); 24887 24888 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24889 24890 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24891 TASKQ_PREPOPULATE); 24892 24893 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24894 24895 /* 24896 * We want to be informed each time a stack is created or 24897 * destroyed in the kernel, so we can maintain the 24898 * set of tcp_stack_t's. 24899 */ 24900 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24901 tcp_stack_fini); 24902 } 24903 24904 24905 #define INET_NAME "ip" 24906 24907 /* 24908 * Initialize the TCP stack instance. 24909 */ 24910 static void * 24911 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24912 { 24913 tcp_stack_t *tcps; 24914 tcpparam_t *pa; 24915 int i; 24916 int error = 0; 24917 major_t major; 24918 24919 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24920 tcps->tcps_netstack = ns; 24921 24922 /* Initialize locks */ 24923 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24924 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24925 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24926 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24927 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24928 24929 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24930 tcps->tcps_g_epriv_ports[0] = 2049; 24931 tcps->tcps_g_epriv_ports[1] = 4045; 24932 tcps->tcps_min_anonpriv_port = 512; 24933 24934 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24935 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24936 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24937 TCP_FANOUT_SIZE, KM_SLEEP); 24938 24939 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24940 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24941 MUTEX_DEFAULT, NULL); 24942 } 24943 24944 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24945 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24946 MUTEX_DEFAULT, NULL); 24947 } 24948 24949 /* TCP's IPsec code calls the packet dropper. */ 24950 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24951 24952 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24953 tcps->tcps_params = pa; 24954 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24955 24956 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24957 A_CNT(lcl_tcp_param_arr), tcps); 24958 24959 /* 24960 * Note: To really walk the device tree you need the devinfo 24961 * pointer to your device which is only available after probe/attach. 24962 * The following is safe only because it uses ddi_root_node() 24963 */ 24964 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24965 tcp_opt_obj.odb_opt_arr_cnt); 24966 24967 /* 24968 * Initialize RFC 1948 secret values. This will probably be reset once 24969 * by the boot scripts. 24970 * 24971 * Use NULL name, as the name is caught by the new lockstats. 24972 * 24973 * Initialize with some random, non-guessable string, like the global 24974 * T_INFO_ACK. 24975 */ 24976 24977 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24978 sizeof (tcp_g_t_info_ack), tcps); 24979 24980 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24981 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24982 24983 major = mod_name_to_major(INET_NAME); 24984 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24985 ASSERT(error == 0); 24986 return (tcps); 24987 } 24988 24989 /* 24990 * Called when the IP module is about to be unloaded. 24991 */ 24992 void 24993 tcp_ddi_g_destroy(void) 24994 { 24995 tcp_g_kstat_fini(tcp_g_kstat); 24996 tcp_g_kstat = NULL; 24997 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24998 24999 mutex_destroy(&tcp_random_lock); 25000 25001 kmem_cache_destroy(tcp_timercache); 25002 kmem_cache_destroy(tcp_sack_info_cache); 25003 kmem_cache_destroy(tcp_iphc_cache); 25004 25005 netstack_unregister(NS_TCP); 25006 taskq_destroy(tcp_taskq); 25007 } 25008 25009 /* 25010 * Shut down the TCP stack instance. 25011 */ 25012 /* ARGSUSED */ 25013 static void 25014 tcp_stack_shutdown(netstackid_t stackid, void *arg) 25015 { 25016 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25017 25018 tcp_g_q_destroy(tcps); 25019 } 25020 25021 /* 25022 * Free the TCP stack instance. 25023 */ 25024 static void 25025 tcp_stack_fini(netstackid_t stackid, void *arg) 25026 { 25027 tcp_stack_t *tcps = (tcp_stack_t *)arg; 25028 int i; 25029 25030 nd_free(&tcps->tcps_g_nd); 25031 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25032 tcps->tcps_params = NULL; 25033 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25034 tcps->tcps_wroff_xtra_param = NULL; 25035 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25036 tcps->tcps_mdt_head_param = NULL; 25037 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25038 tcps->tcps_mdt_tail_param = NULL; 25039 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25040 tcps->tcps_mdt_max_pbufs_param = NULL; 25041 25042 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25043 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25044 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25045 } 25046 25047 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25048 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25049 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25050 } 25051 25052 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25053 tcps->tcps_bind_fanout = NULL; 25054 25055 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25056 tcps->tcps_acceptor_fanout = NULL; 25057 25058 mutex_destroy(&tcps->tcps_iss_key_lock); 25059 rw_destroy(&tcps->tcps_hsp_lock); 25060 mutex_destroy(&tcps->tcps_g_q_lock); 25061 cv_destroy(&tcps->tcps_g_q_cv); 25062 mutex_destroy(&tcps->tcps_epriv_port_lock); 25063 25064 ip_drop_unregister(&tcps->tcps_dropper); 25065 25066 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25067 tcps->tcps_kstat = NULL; 25068 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25069 25070 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25071 tcps->tcps_mibkp = NULL; 25072 25073 ldi_ident_release(tcps->tcps_ldi_ident); 25074 kmem_free(tcps, sizeof (*tcps)); 25075 } 25076 25077 /* 25078 * Generate ISS, taking into account NDD changes may happen halfway through. 25079 * (If the iss is not zero, set it.) 25080 */ 25081 25082 static void 25083 tcp_iss_init(tcp_t *tcp) 25084 { 25085 MD5_CTX context; 25086 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25087 uint32_t answer[4]; 25088 tcp_stack_t *tcps = tcp->tcp_tcps; 25089 25090 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25091 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25092 switch (tcps->tcps_strong_iss) { 25093 case 2: 25094 mutex_enter(&tcps->tcps_iss_key_lock); 25095 context = tcps->tcps_iss_key; 25096 mutex_exit(&tcps->tcps_iss_key_lock); 25097 arg.ports = tcp->tcp_ports; 25098 if (tcp->tcp_ipversion == IPV4_VERSION) { 25099 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25100 &arg.src); 25101 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25102 &arg.dst); 25103 } else { 25104 arg.src = tcp->tcp_ip6h->ip6_src; 25105 arg.dst = tcp->tcp_ip6h->ip6_dst; 25106 } 25107 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25108 MD5Final((uchar_t *)answer, &context); 25109 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25110 /* 25111 * Now that we've hashed into a unique per-connection sequence 25112 * space, add a random increment per strong_iss == 1. So I 25113 * guess we'll have to... 25114 */ 25115 /* FALLTHRU */ 25116 case 1: 25117 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25118 break; 25119 default: 25120 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25121 break; 25122 } 25123 tcp->tcp_valid_bits = TCP_ISS_VALID; 25124 tcp->tcp_fss = tcp->tcp_iss - 1; 25125 tcp->tcp_suna = tcp->tcp_iss; 25126 tcp->tcp_snxt = tcp->tcp_iss + 1; 25127 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25128 tcp->tcp_csuna = tcp->tcp_snxt; 25129 } 25130 25131 /* 25132 * Exported routine for extracting active tcp connection status. 25133 * 25134 * This is used by the Solaris Cluster Networking software to 25135 * gather a list of connections that need to be forwarded to 25136 * specific nodes in the cluster when configuration changes occur. 25137 * 25138 * The callback is invoked for each tcp_t structure from all netstacks, 25139 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 25140 * from the netstack with the specified stack_id. Returning 25141 * non-zero from the callback routine terminates the search. 25142 */ 25143 int 25144 cl_tcp_walk_list(netstackid_t stack_id, 25145 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 25146 { 25147 netstack_handle_t nh; 25148 netstack_t *ns; 25149 int ret = 0; 25150 25151 if (stack_id >= 0) { 25152 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 25153 return (EINVAL); 25154 25155 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25156 ns->netstack_tcp); 25157 netstack_rele(ns); 25158 return (ret); 25159 } 25160 25161 netstack_next_init(&nh); 25162 while ((ns = netstack_next(&nh)) != NULL) { 25163 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25164 ns->netstack_tcp); 25165 netstack_rele(ns); 25166 } 25167 netstack_next_fini(&nh); 25168 return (ret); 25169 } 25170 25171 static int 25172 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25173 tcp_stack_t *tcps) 25174 { 25175 tcp_t *tcp; 25176 cl_tcp_info_t cl_tcpi; 25177 connf_t *connfp; 25178 conn_t *connp; 25179 int i; 25180 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25181 25182 ASSERT(callback != NULL); 25183 25184 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25185 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25186 connp = NULL; 25187 25188 while ((connp = 25189 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25190 25191 tcp = connp->conn_tcp; 25192 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25193 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25194 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25195 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25196 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25197 /* 25198 * The macros tcp_laddr and tcp_faddr give the IPv4 25199 * addresses. They are copied implicitly below as 25200 * mapped addresses. 25201 */ 25202 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25203 if (tcp->tcp_ipversion == IPV4_VERSION) { 25204 cl_tcpi.cl_tcpi_faddr = 25205 tcp->tcp_ipha->ipha_dst; 25206 } else { 25207 cl_tcpi.cl_tcpi_faddr_v6 = 25208 tcp->tcp_ip6h->ip6_dst; 25209 } 25210 25211 /* 25212 * If the callback returns non-zero 25213 * we terminate the traversal. 25214 */ 25215 if ((*callback)(&cl_tcpi, arg) != 0) { 25216 CONN_DEC_REF(tcp->tcp_connp); 25217 return (1); 25218 } 25219 } 25220 } 25221 25222 return (0); 25223 } 25224 25225 /* 25226 * Macros used for accessing the different types of sockaddr 25227 * structures inside a tcp_ioc_abort_conn_t. 25228 */ 25229 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25230 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25231 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25232 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25233 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25234 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25235 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25236 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25237 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25238 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25239 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25240 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25241 25242 /* 25243 * Return the correct error code to mimic the behavior 25244 * of a connection reset. 25245 */ 25246 #define TCP_AC_GET_ERRCODE(state, err) { \ 25247 switch ((state)) { \ 25248 case TCPS_SYN_SENT: \ 25249 case TCPS_SYN_RCVD: \ 25250 (err) = ECONNREFUSED; \ 25251 break; \ 25252 case TCPS_ESTABLISHED: \ 25253 case TCPS_FIN_WAIT_1: \ 25254 case TCPS_FIN_WAIT_2: \ 25255 case TCPS_CLOSE_WAIT: \ 25256 (err) = ECONNRESET; \ 25257 break; \ 25258 case TCPS_CLOSING: \ 25259 case TCPS_LAST_ACK: \ 25260 case TCPS_TIME_WAIT: \ 25261 (err) = 0; \ 25262 break; \ 25263 default: \ 25264 (err) = ENXIO; \ 25265 } \ 25266 } 25267 25268 /* 25269 * Check if a tcp structure matches the info in acp. 25270 */ 25271 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25272 (((acp)->ac_local.ss_family == AF_INET) ? \ 25273 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25274 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25275 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25276 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25277 (TCP_AC_V4LPORT((acp)) == 0 || \ 25278 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25279 (TCP_AC_V4RPORT((acp)) == 0 || \ 25280 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25281 (acp)->ac_start <= (tcp)->tcp_state && \ 25282 (acp)->ac_end >= (tcp)->tcp_state) : \ 25283 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25284 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25285 &(tcp)->tcp_ip_src_v6)) && \ 25286 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25287 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25288 &(tcp)->tcp_remote_v6)) && \ 25289 (TCP_AC_V6LPORT((acp)) == 0 || \ 25290 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25291 (TCP_AC_V6RPORT((acp)) == 0 || \ 25292 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25293 (acp)->ac_start <= (tcp)->tcp_state && \ 25294 (acp)->ac_end >= (tcp)->tcp_state)) 25295 25296 #define TCP_AC_MATCH(acp, tcp) \ 25297 (((acp)->ac_zoneid == ALL_ZONES || \ 25298 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25299 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25300 25301 /* 25302 * Build a message containing a tcp_ioc_abort_conn_t structure 25303 * which is filled in with information from acp and tp. 25304 */ 25305 static mblk_t * 25306 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25307 { 25308 mblk_t *mp; 25309 tcp_ioc_abort_conn_t *tacp; 25310 25311 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25312 if (mp == NULL) 25313 return (NULL); 25314 25315 mp->b_datap->db_type = M_CTL; 25316 25317 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25318 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25319 sizeof (uint32_t)); 25320 25321 tacp->ac_start = acp->ac_start; 25322 tacp->ac_end = acp->ac_end; 25323 tacp->ac_zoneid = acp->ac_zoneid; 25324 25325 if (acp->ac_local.ss_family == AF_INET) { 25326 tacp->ac_local.ss_family = AF_INET; 25327 tacp->ac_remote.ss_family = AF_INET; 25328 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25329 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25330 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25331 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25332 } else { 25333 tacp->ac_local.ss_family = AF_INET6; 25334 tacp->ac_remote.ss_family = AF_INET6; 25335 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25336 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25337 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25338 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25339 } 25340 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25341 return (mp); 25342 } 25343 25344 /* 25345 * Print a tcp_ioc_abort_conn_t structure. 25346 */ 25347 static void 25348 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25349 { 25350 char lbuf[128]; 25351 char rbuf[128]; 25352 sa_family_t af; 25353 in_port_t lport, rport; 25354 ushort_t logflags; 25355 25356 af = acp->ac_local.ss_family; 25357 25358 if (af == AF_INET) { 25359 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25360 lbuf, 128); 25361 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25362 rbuf, 128); 25363 lport = ntohs(TCP_AC_V4LPORT(acp)); 25364 rport = ntohs(TCP_AC_V4RPORT(acp)); 25365 } else { 25366 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25367 lbuf, 128); 25368 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25369 rbuf, 128); 25370 lport = ntohs(TCP_AC_V6LPORT(acp)); 25371 rport = ntohs(TCP_AC_V6RPORT(acp)); 25372 } 25373 25374 logflags = SL_TRACE | SL_NOTE; 25375 /* 25376 * Don't print this message to the console if the operation was done 25377 * to a non-global zone. 25378 */ 25379 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25380 logflags |= SL_CONSOLE; 25381 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25382 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25383 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25384 acp->ac_start, acp->ac_end); 25385 } 25386 25387 /* 25388 * Called inside tcp_rput when a message built using 25389 * tcp_ioctl_abort_build_msg is put into a queue. 25390 * Note that when we get here there is no wildcard in acp any more. 25391 */ 25392 static void 25393 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25394 { 25395 tcp_ioc_abort_conn_t *acp; 25396 25397 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25398 if (tcp->tcp_state <= acp->ac_end) { 25399 /* 25400 * If we get here, we are already on the correct 25401 * squeue. This ioctl follows the following path 25402 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25403 * ->tcp_ioctl_abort->squeue_enter (if on a 25404 * different squeue) 25405 */ 25406 int errcode; 25407 25408 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25409 (void) tcp_clean_death(tcp, errcode, 26); 25410 } 25411 freemsg(mp); 25412 } 25413 25414 /* 25415 * Abort all matching connections on a hash chain. 25416 */ 25417 static int 25418 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25419 boolean_t exact, tcp_stack_t *tcps) 25420 { 25421 int nmatch, err = 0; 25422 tcp_t *tcp; 25423 MBLKP mp, last, listhead = NULL; 25424 conn_t *tconnp; 25425 connf_t *connfp; 25426 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25427 25428 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25429 25430 startover: 25431 nmatch = 0; 25432 25433 mutex_enter(&connfp->connf_lock); 25434 for (tconnp = connfp->connf_head; tconnp != NULL; 25435 tconnp = tconnp->conn_next) { 25436 tcp = tconnp->conn_tcp; 25437 if (TCP_AC_MATCH(acp, tcp)) { 25438 CONN_INC_REF(tcp->tcp_connp); 25439 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25440 if (mp == NULL) { 25441 err = ENOMEM; 25442 CONN_DEC_REF(tcp->tcp_connp); 25443 break; 25444 } 25445 mp->b_prev = (mblk_t *)tcp; 25446 25447 if (listhead == NULL) { 25448 listhead = mp; 25449 last = mp; 25450 } else { 25451 last->b_next = mp; 25452 last = mp; 25453 } 25454 nmatch++; 25455 if (exact) 25456 break; 25457 } 25458 25459 /* Avoid holding lock for too long. */ 25460 if (nmatch >= 500) 25461 break; 25462 } 25463 mutex_exit(&connfp->connf_lock); 25464 25465 /* Pass mp into the correct tcp */ 25466 while ((mp = listhead) != NULL) { 25467 listhead = listhead->b_next; 25468 tcp = (tcp_t *)mp->b_prev; 25469 mp->b_next = mp->b_prev = NULL; 25470 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 25471 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 25472 } 25473 25474 *count += nmatch; 25475 if (nmatch >= 500 && err == 0) 25476 goto startover; 25477 return (err); 25478 } 25479 25480 /* 25481 * Abort all connections that matches the attributes specified in acp. 25482 */ 25483 static int 25484 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25485 { 25486 sa_family_t af; 25487 uint32_t ports; 25488 uint16_t *pports; 25489 int err = 0, count = 0; 25490 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25491 int index = -1; 25492 ushort_t logflags; 25493 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25494 25495 af = acp->ac_local.ss_family; 25496 25497 if (af == AF_INET) { 25498 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25499 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25500 pports = (uint16_t *)&ports; 25501 pports[1] = TCP_AC_V4LPORT(acp); 25502 pports[0] = TCP_AC_V4RPORT(acp); 25503 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25504 } 25505 } else { 25506 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25507 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25508 pports = (uint16_t *)&ports; 25509 pports[1] = TCP_AC_V6LPORT(acp); 25510 pports[0] = TCP_AC_V6RPORT(acp); 25511 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25512 } 25513 } 25514 25515 /* 25516 * For cases where remote addr, local port, and remote port are non- 25517 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25518 */ 25519 if (index != -1) { 25520 err = tcp_ioctl_abort_bucket(acp, index, 25521 &count, exact, tcps); 25522 } else { 25523 /* 25524 * loop through all entries for wildcard case 25525 */ 25526 for (index = 0; 25527 index < ipst->ips_ipcl_conn_fanout_size; 25528 index++) { 25529 err = tcp_ioctl_abort_bucket(acp, index, 25530 &count, exact, tcps); 25531 if (err != 0) 25532 break; 25533 } 25534 } 25535 25536 logflags = SL_TRACE | SL_NOTE; 25537 /* 25538 * Don't print this message to the console if the operation was done 25539 * to a non-global zone. 25540 */ 25541 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25542 logflags |= SL_CONSOLE; 25543 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25544 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25545 if (err == 0 && count == 0) 25546 err = ENOENT; 25547 return (err); 25548 } 25549 25550 /* 25551 * Process the TCP_IOC_ABORT_CONN ioctl request. 25552 */ 25553 static void 25554 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25555 { 25556 int err; 25557 IOCP iocp; 25558 MBLKP mp1; 25559 sa_family_t laf, raf; 25560 tcp_ioc_abort_conn_t *acp; 25561 zone_t *zptr; 25562 conn_t *connp = Q_TO_CONN(q); 25563 zoneid_t zoneid = connp->conn_zoneid; 25564 tcp_t *tcp = connp->conn_tcp; 25565 tcp_stack_t *tcps = tcp->tcp_tcps; 25566 25567 iocp = (IOCP)mp->b_rptr; 25568 25569 if ((mp1 = mp->b_cont) == NULL || 25570 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25571 err = EINVAL; 25572 goto out; 25573 } 25574 25575 /* check permissions */ 25576 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25577 err = EPERM; 25578 goto out; 25579 } 25580 25581 if (mp1->b_cont != NULL) { 25582 freemsg(mp1->b_cont); 25583 mp1->b_cont = NULL; 25584 } 25585 25586 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25587 laf = acp->ac_local.ss_family; 25588 raf = acp->ac_remote.ss_family; 25589 25590 /* check that a zone with the supplied zoneid exists */ 25591 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25592 zptr = zone_find_by_id(zoneid); 25593 if (zptr != NULL) { 25594 zone_rele(zptr); 25595 } else { 25596 err = EINVAL; 25597 goto out; 25598 } 25599 } 25600 25601 /* 25602 * For exclusive stacks we set the zoneid to zero 25603 * to make TCP operate as if in the global zone. 25604 */ 25605 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25606 acp->ac_zoneid = GLOBAL_ZONEID; 25607 25608 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25609 acp->ac_start > acp->ac_end || laf != raf || 25610 (laf != AF_INET && laf != AF_INET6)) { 25611 err = EINVAL; 25612 goto out; 25613 } 25614 25615 tcp_ioctl_abort_dump(acp); 25616 err = tcp_ioctl_abort(acp, tcps); 25617 25618 out: 25619 if (mp1 != NULL) { 25620 freemsg(mp1); 25621 mp->b_cont = NULL; 25622 } 25623 25624 if (err != 0) 25625 miocnak(q, mp, 0, err); 25626 else 25627 miocack(q, mp, 0, 0); 25628 } 25629 25630 /* 25631 * tcp_time_wait_processing() handles processing of incoming packets when 25632 * the tcp is in the TIME_WAIT state. 25633 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25634 * on the time wait list. 25635 */ 25636 void 25637 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25638 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25639 { 25640 int32_t bytes_acked; 25641 int32_t gap; 25642 int32_t rgap; 25643 tcp_opt_t tcpopt; 25644 uint_t flags; 25645 uint32_t new_swnd = 0; 25646 conn_t *connp; 25647 tcp_stack_t *tcps = tcp->tcp_tcps; 25648 25649 BUMP_LOCAL(tcp->tcp_ibsegs); 25650 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25651 25652 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25653 new_swnd = BE16_TO_U16(tcph->th_win) << 25654 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25655 if (tcp->tcp_snd_ts_ok) { 25656 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25657 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25658 tcp->tcp_rnxt, TH_ACK); 25659 goto done; 25660 } 25661 } 25662 gap = seg_seq - tcp->tcp_rnxt; 25663 rgap = tcp->tcp_rwnd - (gap + seg_len); 25664 if (gap < 0) { 25665 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25666 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25667 (seg_len > -gap ? -gap : seg_len)); 25668 seg_len += gap; 25669 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25670 if (flags & TH_RST) { 25671 goto done; 25672 } 25673 if ((flags & TH_FIN) && seg_len == -1) { 25674 /* 25675 * When TCP receives a duplicate FIN in 25676 * TIME_WAIT state, restart the 2 MSL timer. 25677 * See page 73 in RFC 793. Make sure this TCP 25678 * is already on the TIME_WAIT list. If not, 25679 * just restart the timer. 25680 */ 25681 if (TCP_IS_DETACHED(tcp)) { 25682 if (tcp_time_wait_remove(tcp, NULL) == 25683 B_TRUE) { 25684 tcp_time_wait_append(tcp); 25685 TCP_DBGSTAT(tcps, 25686 tcp_rput_time_wait); 25687 } 25688 } else { 25689 ASSERT(tcp != NULL); 25690 TCP_TIMER_RESTART(tcp, 25691 tcps->tcps_time_wait_interval); 25692 } 25693 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25694 tcp->tcp_rnxt, TH_ACK); 25695 goto done; 25696 } 25697 flags |= TH_ACK_NEEDED; 25698 seg_len = 0; 25699 goto process_ack; 25700 } 25701 25702 /* Fix seg_seq, and chew the gap off the front. */ 25703 seg_seq = tcp->tcp_rnxt; 25704 } 25705 25706 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25707 /* 25708 * Make sure that when we accept the connection, pick 25709 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25710 * old connection. 25711 * 25712 * The next ISS generated is equal to tcp_iss_incr_extra 25713 * + ISS_INCR/2 + other components depending on the 25714 * value of tcp_strong_iss. We pre-calculate the new 25715 * ISS here and compare with tcp_snxt to determine if 25716 * we need to make adjustment to tcp_iss_incr_extra. 25717 * 25718 * The above calculation is ugly and is a 25719 * waste of CPU cycles... 25720 */ 25721 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25722 int32_t adj; 25723 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25724 25725 switch (tcps->tcps_strong_iss) { 25726 case 2: { 25727 /* Add time and MD5 components. */ 25728 uint32_t answer[4]; 25729 struct { 25730 uint32_t ports; 25731 in6_addr_t src; 25732 in6_addr_t dst; 25733 } arg; 25734 MD5_CTX context; 25735 25736 mutex_enter(&tcps->tcps_iss_key_lock); 25737 context = tcps->tcps_iss_key; 25738 mutex_exit(&tcps->tcps_iss_key_lock); 25739 arg.ports = tcp->tcp_ports; 25740 /* We use MAPPED addresses in tcp_iss_init */ 25741 arg.src = tcp->tcp_ip_src_v6; 25742 if (tcp->tcp_ipversion == IPV4_VERSION) { 25743 IN6_IPADDR_TO_V4MAPPED( 25744 tcp->tcp_ipha->ipha_dst, 25745 &arg.dst); 25746 } else { 25747 arg.dst = 25748 tcp->tcp_ip6h->ip6_dst; 25749 } 25750 MD5Update(&context, (uchar_t *)&arg, 25751 sizeof (arg)); 25752 MD5Final((uchar_t *)answer, &context); 25753 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25754 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25755 break; 25756 } 25757 case 1: 25758 /* Add time component and min random (i.e. 1). */ 25759 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25760 break; 25761 default: 25762 /* Add only time component. */ 25763 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25764 break; 25765 } 25766 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25767 /* 25768 * New ISS not guaranteed to be ISS_INCR/2 25769 * ahead of the current tcp_snxt, so add the 25770 * difference to tcp_iss_incr_extra. 25771 */ 25772 tcps->tcps_iss_incr_extra += adj; 25773 } 25774 /* 25775 * If tcp_clean_death() can not perform the task now, 25776 * drop the SYN packet and let the other side re-xmit. 25777 * Otherwise pass the SYN packet back in, since the 25778 * old tcp state has been cleaned up or freed. 25779 */ 25780 if (tcp_clean_death(tcp, 0, 27) == -1) 25781 goto done; 25782 /* 25783 * We will come back to tcp_rput_data 25784 * on the global queue. Packets destined 25785 * for the global queue will be checked 25786 * with global policy. But the policy for 25787 * this packet has already been checked as 25788 * this was destined for the detached 25789 * connection. We need to bypass policy 25790 * check this time by attaching a dummy 25791 * ipsec_in with ipsec_in_dont_check set. 25792 */ 25793 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25794 if (connp != NULL) { 25795 TCP_STAT(tcps, tcp_time_wait_syn_success); 25796 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25797 return; 25798 } 25799 goto done; 25800 } 25801 25802 /* 25803 * rgap is the amount of stuff received out of window. A negative 25804 * value is the amount out of window. 25805 */ 25806 if (rgap < 0) { 25807 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25808 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25809 /* Fix seg_len and make sure there is something left. */ 25810 seg_len += rgap; 25811 if (seg_len <= 0) { 25812 if (flags & TH_RST) { 25813 goto done; 25814 } 25815 flags |= TH_ACK_NEEDED; 25816 seg_len = 0; 25817 goto process_ack; 25818 } 25819 } 25820 /* 25821 * Check whether we can update tcp_ts_recent. This test is 25822 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25823 * Extensions for High Performance: An Update", Internet Draft. 25824 */ 25825 if (tcp->tcp_snd_ts_ok && 25826 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25827 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25828 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25829 tcp->tcp_last_rcv_lbolt = lbolt64; 25830 } 25831 25832 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25833 /* Always ack out of order packets */ 25834 flags |= TH_ACK_NEEDED; 25835 seg_len = 0; 25836 } else if (seg_len > 0) { 25837 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25838 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25839 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25840 } 25841 if (flags & TH_RST) { 25842 (void) tcp_clean_death(tcp, 0, 28); 25843 goto done; 25844 } 25845 if (flags & TH_SYN) { 25846 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25847 TH_RST|TH_ACK); 25848 /* 25849 * Do not delete the TCP structure if it is in 25850 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25851 */ 25852 goto done; 25853 } 25854 process_ack: 25855 if (flags & TH_ACK) { 25856 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25857 if (bytes_acked <= 0) { 25858 if (bytes_acked == 0 && seg_len == 0 && 25859 new_swnd == tcp->tcp_swnd) 25860 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25861 } else { 25862 /* Acks something not sent */ 25863 flags |= TH_ACK_NEEDED; 25864 } 25865 } 25866 if (flags & TH_ACK_NEEDED) { 25867 /* 25868 * Time to send an ack for some reason. 25869 */ 25870 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25871 tcp->tcp_rnxt, TH_ACK); 25872 } 25873 done: 25874 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25875 DB_CKSUMSTART(mp) = 0; 25876 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25877 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25878 } 25879 freemsg(mp); 25880 } 25881 25882 /* 25883 * TCP Timers Implementation. 25884 */ 25885 timeout_id_t 25886 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25887 { 25888 mblk_t *mp; 25889 tcp_timer_t *tcpt; 25890 tcp_t *tcp = connp->conn_tcp; 25891 25892 ASSERT(connp->conn_sqp != NULL); 25893 25894 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25895 25896 if (tcp->tcp_timercache == NULL) { 25897 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25898 } else { 25899 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25900 mp = tcp->tcp_timercache; 25901 tcp->tcp_timercache = mp->b_next; 25902 mp->b_next = NULL; 25903 ASSERT(mp->b_wptr == NULL); 25904 } 25905 25906 CONN_INC_REF(connp); 25907 tcpt = (tcp_timer_t *)mp->b_rptr; 25908 tcpt->connp = connp; 25909 tcpt->tcpt_proc = f; 25910 /* 25911 * TCP timers are normal timeouts. Plus, they do not require more than 25912 * a 10 millisecond resolution. By choosing a coarser resolution and by 25913 * rounding up the expiration to the next resolution boundary, we can 25914 * batch timers in the callout subsystem to make TCP timers more 25915 * efficient. The roundup also protects short timers from expiring too 25916 * early before they have a chance to be cancelled. 25917 */ 25918 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25919 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25920 25921 return ((timeout_id_t)mp); 25922 } 25923 25924 static void 25925 tcp_timer_callback(void *arg) 25926 { 25927 mblk_t *mp = (mblk_t *)arg; 25928 tcp_timer_t *tcpt; 25929 conn_t *connp; 25930 25931 tcpt = (tcp_timer_t *)mp->b_rptr; 25932 connp = tcpt->connp; 25933 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25934 SQ_FILL, SQTAG_TCP_TIMER); 25935 } 25936 25937 static void 25938 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25939 { 25940 tcp_timer_t *tcpt; 25941 conn_t *connp = (conn_t *)arg; 25942 tcp_t *tcp = connp->conn_tcp; 25943 25944 tcpt = (tcp_timer_t *)mp->b_rptr; 25945 ASSERT(connp == tcpt->connp); 25946 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25947 25948 /* 25949 * If the TCP has reached the closed state, don't proceed any 25950 * further. This TCP logically does not exist on the system. 25951 * tcpt_proc could for example access queues, that have already 25952 * been qprocoff'ed off. Also see comments at the start of tcp_input 25953 */ 25954 if (tcp->tcp_state != TCPS_CLOSED) { 25955 (*tcpt->tcpt_proc)(connp); 25956 } else { 25957 tcp->tcp_timer_tid = 0; 25958 } 25959 tcp_timer_free(connp->conn_tcp, mp); 25960 } 25961 25962 /* 25963 * There is potential race with untimeout and the handler firing at the same 25964 * time. The mblock may be freed by the handler while we are trying to use 25965 * it. But since both should execute on the same squeue, this race should not 25966 * occur. 25967 */ 25968 clock_t 25969 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25970 { 25971 mblk_t *mp = (mblk_t *)id; 25972 tcp_timer_t *tcpt; 25973 clock_t delta; 25974 25975 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25976 25977 if (mp == NULL) 25978 return (-1); 25979 25980 tcpt = (tcp_timer_t *)mp->b_rptr; 25981 ASSERT(tcpt->connp == connp); 25982 25983 delta = untimeout_default(tcpt->tcpt_tid, 0); 25984 25985 if (delta >= 0) { 25986 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25987 tcp_timer_free(connp->conn_tcp, mp); 25988 CONN_DEC_REF(connp); 25989 } 25990 25991 return (delta); 25992 } 25993 25994 /* 25995 * Allocate space for the timer event. The allocation looks like mblk, but it is 25996 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25997 * 25998 * Dealing with failures: If we can't allocate from the timer cache we try 25999 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 26000 * points to b_rptr. 26001 * If we can't allocate anything using allocb_tryhard(), we perform a last 26002 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 26003 * save the actual allocation size in b_datap. 26004 */ 26005 mblk_t * 26006 tcp_timermp_alloc(int kmflags) 26007 { 26008 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 26009 kmflags & ~KM_PANIC); 26010 26011 if (mp != NULL) { 26012 mp->b_next = mp->b_prev = NULL; 26013 mp->b_rptr = (uchar_t *)(&mp[1]); 26014 mp->b_wptr = NULL; 26015 mp->b_datap = NULL; 26016 mp->b_queue = NULL; 26017 mp->b_cont = NULL; 26018 } else if (kmflags & KM_PANIC) { 26019 /* 26020 * Failed to allocate memory for the timer. Try allocating from 26021 * dblock caches. 26022 */ 26023 /* ipclassifier calls this from a constructor - hence no tcps */ 26024 TCP_G_STAT(tcp_timermp_allocfail); 26025 mp = allocb_tryhard(sizeof (tcp_timer_t)); 26026 if (mp == NULL) { 26027 size_t size = 0; 26028 /* 26029 * Memory is really low. Try tryhard allocation. 26030 * 26031 * ipclassifier calls this from a constructor - 26032 * hence no tcps 26033 */ 26034 TCP_G_STAT(tcp_timermp_allocdblfail); 26035 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26036 sizeof (tcp_timer_t), &size, kmflags); 26037 mp->b_rptr = (uchar_t *)(&mp[1]); 26038 mp->b_next = mp->b_prev = NULL; 26039 mp->b_wptr = (uchar_t *)-1; 26040 mp->b_datap = (dblk_t *)size; 26041 mp->b_queue = NULL; 26042 mp->b_cont = NULL; 26043 } 26044 ASSERT(mp->b_wptr != NULL); 26045 } 26046 /* ipclassifier calls this from a constructor - hence no tcps */ 26047 TCP_G_DBGSTAT(tcp_timermp_alloced); 26048 26049 return (mp); 26050 } 26051 26052 /* 26053 * Free per-tcp timer cache. 26054 * It can only contain entries from tcp_timercache. 26055 */ 26056 void 26057 tcp_timermp_free(tcp_t *tcp) 26058 { 26059 mblk_t *mp; 26060 26061 while ((mp = tcp->tcp_timercache) != NULL) { 26062 ASSERT(mp->b_wptr == NULL); 26063 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26064 kmem_cache_free(tcp_timercache, mp); 26065 } 26066 } 26067 26068 /* 26069 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26070 * events there already (currently at most two events are cached). 26071 * If the event is not allocated from the timer cache, free it right away. 26072 */ 26073 static void 26074 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26075 { 26076 mblk_t *mp1 = tcp->tcp_timercache; 26077 26078 if (mp->b_wptr != NULL) { 26079 /* 26080 * This allocation is not from a timer cache, free it right 26081 * away. 26082 */ 26083 if (mp->b_wptr != (uchar_t *)-1) 26084 freeb(mp); 26085 else 26086 kmem_free(mp, (size_t)mp->b_datap); 26087 } else if (mp1 == NULL || mp1->b_next == NULL) { 26088 /* Cache this timer block for future allocations */ 26089 mp->b_rptr = (uchar_t *)(&mp[1]); 26090 mp->b_next = mp1; 26091 tcp->tcp_timercache = mp; 26092 } else { 26093 kmem_cache_free(tcp_timercache, mp); 26094 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 26095 } 26096 } 26097 26098 /* 26099 * End of TCP Timers implementation. 26100 */ 26101 26102 /* 26103 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26104 * on the specified backing STREAMS q. Note, the caller may make the 26105 * decision to call based on the tcp_t.tcp_flow_stopped value which 26106 * when check outside the q's lock is only an advisory check ... 26107 */ 26108 void 26109 tcp_setqfull(tcp_t *tcp) 26110 { 26111 tcp_stack_t *tcps = tcp->tcp_tcps; 26112 conn_t *connp = tcp->tcp_connp; 26113 26114 if (tcp->tcp_closed) 26115 return; 26116 26117 if (IPCL_IS_NONSTR(connp)) { 26118 (*connp->conn_upcalls->su_txq_full) 26119 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 26120 tcp->tcp_flow_stopped = B_TRUE; 26121 } else { 26122 queue_t *q = tcp->tcp_wq; 26123 26124 if (!(q->q_flag & QFULL)) { 26125 mutex_enter(QLOCK(q)); 26126 if (!(q->q_flag & QFULL)) { 26127 /* still need to set QFULL */ 26128 q->q_flag |= QFULL; 26129 tcp->tcp_flow_stopped = B_TRUE; 26130 mutex_exit(QLOCK(q)); 26131 TCP_STAT(tcps, tcp_flwctl_on); 26132 } else { 26133 mutex_exit(QLOCK(q)); 26134 } 26135 } 26136 } 26137 } 26138 26139 void 26140 tcp_clrqfull(tcp_t *tcp) 26141 { 26142 conn_t *connp = tcp->tcp_connp; 26143 26144 if (tcp->tcp_closed) 26145 return; 26146 26147 if (IPCL_IS_NONSTR(connp)) { 26148 (*connp->conn_upcalls->su_txq_full) 26149 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 26150 tcp->tcp_flow_stopped = B_FALSE; 26151 } else { 26152 queue_t *q = tcp->tcp_wq; 26153 26154 if (q->q_flag & QFULL) { 26155 mutex_enter(QLOCK(q)); 26156 if (q->q_flag & QFULL) { 26157 q->q_flag &= ~QFULL; 26158 tcp->tcp_flow_stopped = B_FALSE; 26159 mutex_exit(QLOCK(q)); 26160 if (q->q_flag & QWANTW) 26161 qbackenable(q, 0); 26162 } else { 26163 mutex_exit(QLOCK(q)); 26164 } 26165 } 26166 } 26167 } 26168 26169 /* 26170 * kstats related to squeues i.e. not per IP instance 26171 */ 26172 static void * 26173 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26174 { 26175 kstat_t *ksp; 26176 26177 tcp_g_stat_t template = { 26178 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26179 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26180 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26181 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26182 }; 26183 26184 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26185 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26186 KSTAT_FLAG_VIRTUAL); 26187 26188 if (ksp == NULL) 26189 return (NULL); 26190 26191 bcopy(&template, tcp_g_statp, sizeof (template)); 26192 ksp->ks_data = (void *)tcp_g_statp; 26193 26194 kstat_install(ksp); 26195 return (ksp); 26196 } 26197 26198 static void 26199 tcp_g_kstat_fini(kstat_t *ksp) 26200 { 26201 if (ksp != NULL) { 26202 kstat_delete(ksp); 26203 } 26204 } 26205 26206 26207 static void * 26208 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26209 { 26210 kstat_t *ksp; 26211 26212 tcp_stat_t template = { 26213 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26214 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26215 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26216 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26217 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26218 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26219 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26220 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26221 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26222 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26223 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26224 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26225 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26226 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26227 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26228 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26229 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26230 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26231 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26232 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26233 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26234 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26235 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26236 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26237 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26238 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26239 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26240 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26241 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26242 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26243 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26244 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26245 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26246 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26247 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26248 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26249 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26250 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26251 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26252 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26253 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26254 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26255 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26256 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26257 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26258 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26259 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26260 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26261 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26262 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26263 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26264 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26265 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26266 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26267 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26268 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26269 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26270 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26271 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26272 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26273 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26274 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26275 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26276 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26277 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26278 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26279 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26280 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26281 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26282 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26283 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26284 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26285 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26286 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26287 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26288 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26289 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26290 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26291 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26292 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26293 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26294 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26295 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26296 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26297 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26298 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26299 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26300 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26301 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26302 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26303 }; 26304 26305 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26306 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26307 KSTAT_FLAG_VIRTUAL, stackid); 26308 26309 if (ksp == NULL) 26310 return (NULL); 26311 26312 bcopy(&template, tcps_statisticsp, sizeof (template)); 26313 ksp->ks_data = (void *)tcps_statisticsp; 26314 ksp->ks_private = (void *)(uintptr_t)stackid; 26315 26316 kstat_install(ksp); 26317 return (ksp); 26318 } 26319 26320 static void 26321 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26322 { 26323 if (ksp != NULL) { 26324 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26325 kstat_delete_netstack(ksp, stackid); 26326 } 26327 } 26328 26329 /* 26330 * TCP Kstats implementation 26331 */ 26332 static void * 26333 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26334 { 26335 kstat_t *ksp; 26336 26337 tcp_named_kstat_t template = { 26338 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26339 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26340 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26341 { "maxConn", KSTAT_DATA_INT32, 0 }, 26342 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26343 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26344 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26345 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26346 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26347 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26348 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26349 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26350 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26351 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26352 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26353 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26354 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26355 { "outAck", KSTAT_DATA_UINT32, 0 }, 26356 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26357 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26358 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26359 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26360 { "outControl", KSTAT_DATA_UINT32, 0 }, 26361 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26362 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26363 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26364 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26365 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26366 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26367 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26368 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26369 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26370 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26371 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26372 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26373 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26374 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26375 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26376 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26377 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26378 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26379 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26380 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26381 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26382 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26383 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26384 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26385 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26386 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26387 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26388 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26389 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26390 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26391 }; 26392 26393 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26394 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26395 26396 if (ksp == NULL) 26397 return (NULL); 26398 26399 template.rtoAlgorithm.value.ui32 = 4; 26400 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26401 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26402 template.maxConn.value.i32 = -1; 26403 26404 bcopy(&template, ksp->ks_data, sizeof (template)); 26405 ksp->ks_update = tcp_kstat_update; 26406 ksp->ks_private = (void *)(uintptr_t)stackid; 26407 26408 kstat_install(ksp); 26409 return (ksp); 26410 } 26411 26412 static void 26413 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26414 { 26415 if (ksp != NULL) { 26416 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26417 kstat_delete_netstack(ksp, stackid); 26418 } 26419 } 26420 26421 static int 26422 tcp_kstat_update(kstat_t *kp, int rw) 26423 { 26424 tcp_named_kstat_t *tcpkp; 26425 tcp_t *tcp; 26426 connf_t *connfp; 26427 conn_t *connp; 26428 int i; 26429 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26430 netstack_t *ns; 26431 tcp_stack_t *tcps; 26432 ip_stack_t *ipst; 26433 26434 if ((kp == NULL) || (kp->ks_data == NULL)) 26435 return (EIO); 26436 26437 if (rw == KSTAT_WRITE) 26438 return (EACCES); 26439 26440 ns = netstack_find_by_stackid(stackid); 26441 if (ns == NULL) 26442 return (-1); 26443 tcps = ns->netstack_tcp; 26444 if (tcps == NULL) { 26445 netstack_rele(ns); 26446 return (-1); 26447 } 26448 26449 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26450 26451 tcpkp->currEstab.value.ui32 = 0; 26452 26453 ipst = ns->netstack_ip; 26454 26455 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26456 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26457 connp = NULL; 26458 while ((connp = 26459 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26460 tcp = connp->conn_tcp; 26461 switch (tcp_snmp_state(tcp)) { 26462 case MIB2_TCP_established: 26463 case MIB2_TCP_closeWait: 26464 tcpkp->currEstab.value.ui32++; 26465 break; 26466 } 26467 } 26468 } 26469 26470 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26471 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26472 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26473 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26474 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26475 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26476 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26477 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26478 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26479 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26480 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26481 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26482 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26483 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26484 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26485 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26486 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26487 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26488 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26489 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26490 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26491 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26492 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26493 tcpkp->inDataInorderSegs.value.ui32 = 26494 tcps->tcps_mib.tcpInDataInorderSegs; 26495 tcpkp->inDataInorderBytes.value.ui32 = 26496 tcps->tcps_mib.tcpInDataInorderBytes; 26497 tcpkp->inDataUnorderSegs.value.ui32 = 26498 tcps->tcps_mib.tcpInDataUnorderSegs; 26499 tcpkp->inDataUnorderBytes.value.ui32 = 26500 tcps->tcps_mib.tcpInDataUnorderBytes; 26501 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26502 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26503 tcpkp->inDataPartDupSegs.value.ui32 = 26504 tcps->tcps_mib.tcpInDataPartDupSegs; 26505 tcpkp->inDataPartDupBytes.value.ui32 = 26506 tcps->tcps_mib.tcpInDataPartDupBytes; 26507 tcpkp->inDataPastWinSegs.value.ui32 = 26508 tcps->tcps_mib.tcpInDataPastWinSegs; 26509 tcpkp->inDataPastWinBytes.value.ui32 = 26510 tcps->tcps_mib.tcpInDataPastWinBytes; 26511 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26512 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26513 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26514 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26515 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26516 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26517 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26518 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26519 tcpkp->timKeepaliveProbe.value.ui32 = 26520 tcps->tcps_mib.tcpTimKeepaliveProbe; 26521 tcpkp->timKeepaliveDrop.value.ui32 = 26522 tcps->tcps_mib.tcpTimKeepaliveDrop; 26523 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26524 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26525 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26526 tcpkp->outSackRetransSegs.value.ui32 = 26527 tcps->tcps_mib.tcpOutSackRetransSegs; 26528 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26529 26530 netstack_rele(ns); 26531 return (0); 26532 } 26533 26534 void 26535 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26536 { 26537 uint16_t hdr_len; 26538 ipha_t *ipha; 26539 uint8_t *nexthdrp; 26540 tcph_t *tcph; 26541 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26542 26543 /* Already has an eager */ 26544 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26545 TCP_STAT(tcps, tcp_reinput_syn); 26546 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26547 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 26548 return; 26549 } 26550 26551 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26552 case IPV4_VERSION: 26553 ipha = (ipha_t *)mp->b_rptr; 26554 hdr_len = IPH_HDR_LENGTH(ipha); 26555 break; 26556 case IPV6_VERSION: 26557 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26558 &hdr_len, &nexthdrp)) { 26559 CONN_DEC_REF(connp); 26560 freemsg(mp); 26561 return; 26562 } 26563 break; 26564 } 26565 26566 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26567 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26568 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26569 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26570 } 26571 26572 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26573 SQ_FILL, SQTAG_TCP_REINPUT); 26574 } 26575 26576 static int 26577 tcp_squeue_switch(int val) 26578 { 26579 int rval = SQ_FILL; 26580 26581 switch (val) { 26582 case 1: 26583 rval = SQ_NODRAIN; 26584 break; 26585 case 2: 26586 rval = SQ_PROCESS; 26587 break; 26588 default: 26589 break; 26590 } 26591 return (rval); 26592 } 26593 26594 /* 26595 * This is called once for each squeue - globally for all stack 26596 * instances. 26597 */ 26598 static void 26599 tcp_squeue_add(squeue_t *sqp) 26600 { 26601 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26602 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26603 26604 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26605 tcp_time_wait->tcp_time_wait_tid = 26606 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26607 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26608 CALLOUT_FLAG_ROUNDUP); 26609 if (tcp_free_list_max_cnt == 0) { 26610 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26611 max_ncpus : boot_max_ncpus); 26612 26613 /* 26614 * Limit number of entries to 1% of availble memory / tcp_ncpus 26615 */ 26616 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26617 (tcp_ncpus * sizeof (tcp_t) * 100); 26618 } 26619 tcp_time_wait->tcp_free_list_cnt = 0; 26620 } 26621 26622 static int 26623 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 26624 { 26625 mblk_t *ire_mp = NULL; 26626 mblk_t *syn_mp; 26627 mblk_t *mdti; 26628 mblk_t *lsoi; 26629 int retval; 26630 tcph_t *tcph; 26631 uint32_t mss; 26632 queue_t *q = tcp->tcp_rq; 26633 conn_t *connp = tcp->tcp_connp; 26634 tcp_stack_t *tcps = tcp->tcp_tcps; 26635 26636 if (error == 0) { 26637 /* 26638 * Adapt Multidata information, if any. The 26639 * following tcp_mdt_update routine will free 26640 * the message. 26641 */ 26642 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26643 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26644 b_rptr)->mdt_capab, B_TRUE); 26645 freemsg(mdti); 26646 } 26647 26648 /* 26649 * Check to update LSO information with tcp, and 26650 * tcp_lso_update routine will free the message. 26651 */ 26652 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26653 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26654 b_rptr)->lso_capab); 26655 freemsg(lsoi); 26656 } 26657 26658 /* Get the IRE, if we had requested for it */ 26659 if (mp != NULL) 26660 ire_mp = tcp_ire_mp(&mp); 26661 26662 if (tcp->tcp_hard_binding) { 26663 tcp->tcp_hard_binding = B_FALSE; 26664 tcp->tcp_hard_bound = B_TRUE; 26665 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26666 if (retval != 0) { 26667 error = EADDRINUSE; 26668 goto bind_failed; 26669 } 26670 } else { 26671 if (ire_mp != NULL) 26672 freeb(ire_mp); 26673 goto after_syn_sent; 26674 } 26675 26676 retval = tcp_adapt_ire(tcp, ire_mp); 26677 if (ire_mp != NULL) 26678 freeb(ire_mp); 26679 if (retval == 0) { 26680 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26681 ENETUNREACH : EADDRNOTAVAIL); 26682 goto ipcl_rm; 26683 } 26684 /* 26685 * Don't let an endpoint connect to itself. 26686 * Also checked in tcp_connect() but that 26687 * check can't handle the case when the 26688 * local IP address is INADDR_ANY. 26689 */ 26690 if (tcp->tcp_ipversion == IPV4_VERSION) { 26691 if ((tcp->tcp_ipha->ipha_dst == 26692 tcp->tcp_ipha->ipha_src) && 26693 (BE16_EQL(tcp->tcp_tcph->th_lport, 26694 tcp->tcp_tcph->th_fport))) { 26695 error = EADDRNOTAVAIL; 26696 goto ipcl_rm; 26697 } 26698 } else { 26699 if (IN6_ARE_ADDR_EQUAL( 26700 &tcp->tcp_ip6h->ip6_dst, 26701 &tcp->tcp_ip6h->ip6_src) && 26702 (BE16_EQL(tcp->tcp_tcph->th_lport, 26703 tcp->tcp_tcph->th_fport))) { 26704 error = EADDRNOTAVAIL; 26705 goto ipcl_rm; 26706 } 26707 } 26708 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26709 /* 26710 * This should not be possible! Just for 26711 * defensive coding... 26712 */ 26713 if (tcp->tcp_state != TCPS_SYN_SENT) 26714 goto after_syn_sent; 26715 26716 if (is_system_labeled() && 26717 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26718 error = EHOSTUNREACH; 26719 goto ipcl_rm; 26720 } 26721 26722 /* 26723 * tcp_adapt_ire() does not adjust 26724 * for TCP/IP header length. 26725 */ 26726 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26727 26728 /* 26729 * Just make sure our rwnd is at 26730 * least tcp_recv_hiwat_mss * MSS 26731 * large, and round up to the nearest 26732 * MSS. 26733 * 26734 * We do the round up here because 26735 * we need to get the interface 26736 * MTU first before we can do the 26737 * round up. 26738 */ 26739 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26740 tcps->tcps_recv_hiwat_minmss * mss); 26741 if (!IPCL_IS_NONSTR(connp)) 26742 q->q_hiwat = tcp->tcp_rwnd; 26743 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26744 tcp_set_ws_value(tcp); 26745 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26746 tcp->tcp_tcph->th_win); 26747 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26748 tcp->tcp_snd_ws_ok = B_TRUE; 26749 26750 /* 26751 * Set tcp_snd_ts_ok to true 26752 * so that tcp_xmit_mp will 26753 * include the timestamp 26754 * option in the SYN segment. 26755 */ 26756 if (tcps->tcps_tstamp_always || 26757 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26758 tcp->tcp_snd_ts_ok = B_TRUE; 26759 } 26760 26761 /* 26762 * tcp_snd_sack_ok can be set in 26763 * tcp_adapt_ire() if the sack metric 26764 * is set. So check it here also. 26765 */ 26766 if (tcps->tcps_sack_permitted == 2 || 26767 tcp->tcp_snd_sack_ok) { 26768 if (tcp->tcp_sack_info == NULL) { 26769 tcp->tcp_sack_info = 26770 kmem_cache_alloc(tcp_sack_info_cache, 26771 KM_SLEEP); 26772 } 26773 tcp->tcp_snd_sack_ok = B_TRUE; 26774 } 26775 26776 /* 26777 * Should we use ECN? Note that the current 26778 * default value (SunOS 5.9) of tcp_ecn_permitted 26779 * is 1. The reason for doing this is that there 26780 * are equipments out there that will drop ECN 26781 * enabled IP packets. Setting it to 1 avoids 26782 * compatibility problems. 26783 */ 26784 if (tcps->tcps_ecn_permitted == 2) 26785 tcp->tcp_ecn_ok = B_TRUE; 26786 26787 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26788 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26789 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26790 if (syn_mp) { 26791 if (cr == NULL) { 26792 cr = tcp->tcp_cred; 26793 pid = tcp->tcp_cpid; 26794 } 26795 mblk_setcred(syn_mp, cr, pid); 26796 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26797 } 26798 after_syn_sent: 26799 if (mp != NULL) { 26800 ASSERT(mp->b_cont == NULL); 26801 freeb(mp); 26802 } 26803 return (error); 26804 } else { 26805 /* error */ 26806 if (tcp->tcp_debug) { 26807 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26808 "tcp_post_ip_bind: error == %d", error); 26809 } 26810 if (mp != NULL) { 26811 freeb(mp); 26812 } 26813 } 26814 26815 ipcl_rm: 26816 /* 26817 * Need to unbind with classifier since we were just 26818 * told that our bind succeeded. a.k.a error == 0 at the entry. 26819 */ 26820 tcp->tcp_hard_bound = B_FALSE; 26821 tcp->tcp_hard_binding = B_FALSE; 26822 26823 ipcl_hash_remove(connp); 26824 26825 bind_failed: 26826 tcp->tcp_state = TCPS_IDLE; 26827 if (tcp->tcp_ipversion == IPV4_VERSION) 26828 tcp->tcp_ipha->ipha_src = 0; 26829 else 26830 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26831 /* 26832 * Copy of the src addr. in tcp_t is needed since 26833 * the lookup funcs. can only look at tcp_t 26834 */ 26835 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26836 26837 tcph = tcp->tcp_tcph; 26838 tcph->th_lport[0] = 0; 26839 tcph->th_lport[1] = 0; 26840 tcp_bind_hash_remove(tcp); 26841 bzero(&connp->u_port, sizeof (connp->u_port)); 26842 /* blow away saved option results if any */ 26843 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26844 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26845 26846 conn_delete_ire(tcp->tcp_connp, NULL); 26847 26848 return (error); 26849 } 26850 26851 static int 26852 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26853 boolean_t bind_to_req_port_only, cred_t *cr) 26854 { 26855 in_port_t mlp_port; 26856 mlp_type_t addrtype, mlptype; 26857 boolean_t user_specified; 26858 in_port_t allocated_port; 26859 in_port_t requested_port = *requested_port_ptr; 26860 conn_t *connp; 26861 zone_t *zone; 26862 tcp_stack_t *tcps = tcp->tcp_tcps; 26863 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26864 26865 /* 26866 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26867 */ 26868 if (cr == NULL) 26869 cr = tcp->tcp_cred; 26870 /* 26871 * Get a valid port (within the anonymous range and should not 26872 * be a privileged one) to use if the user has not given a port. 26873 * If multiple threads are here, they may all start with 26874 * with the same initial port. But, it should be fine as long as 26875 * tcp_bindi will ensure that no two threads will be assigned 26876 * the same port. 26877 * 26878 * NOTE: XXX If a privileged process asks for an anonymous port, we 26879 * still check for ports only in the range > tcp_smallest_non_priv_port, 26880 * unless TCP_ANONPRIVBIND option is set. 26881 */ 26882 mlptype = mlptSingle; 26883 mlp_port = requested_port; 26884 if (requested_port == 0) { 26885 requested_port = tcp->tcp_anon_priv_bind ? 26886 tcp_get_next_priv_port(tcp) : 26887 tcp_update_next_port(tcps->tcps_next_port_to_try, 26888 tcp, B_TRUE); 26889 if (requested_port == 0) { 26890 return (-TNOADDR); 26891 } 26892 user_specified = B_FALSE; 26893 26894 /* 26895 * If the user went through one of the RPC interfaces to create 26896 * this socket and RPC is MLP in this zone, then give him an 26897 * anonymous MLP. 26898 */ 26899 connp = tcp->tcp_connp; 26900 if (connp->conn_anon_mlp && is_system_labeled()) { 26901 zone = crgetzone(cr); 26902 addrtype = tsol_mlp_addr_type(zone->zone_id, 26903 IPV6_VERSION, &v6addr, 26904 tcps->tcps_netstack->netstack_ip); 26905 if (addrtype == mlptSingle) { 26906 return (-TNOADDR); 26907 } 26908 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26909 PMAPPORT, addrtype); 26910 mlp_port = PMAPPORT; 26911 } 26912 } else { 26913 int i; 26914 boolean_t priv = B_FALSE; 26915 26916 /* 26917 * If the requested_port is in the well-known privileged range, 26918 * verify that the stream was opened by a privileged user. 26919 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26920 * but instead the code relies on: 26921 * - the fact that the address of the array and its size never 26922 * changes 26923 * - the atomic assignment of the elements of the array 26924 */ 26925 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26926 priv = B_TRUE; 26927 } else { 26928 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26929 if (requested_port == 26930 tcps->tcps_g_epriv_ports[i]) { 26931 priv = B_TRUE; 26932 break; 26933 } 26934 } 26935 } 26936 if (priv) { 26937 if (secpolicy_net_privaddr(cr, requested_port, 26938 IPPROTO_TCP) != 0) { 26939 if (tcp->tcp_debug) { 26940 (void) strlog(TCP_MOD_ID, 0, 1, 26941 SL_ERROR|SL_TRACE, 26942 "tcp_bind: no priv for port %d", 26943 requested_port); 26944 } 26945 return (-TACCES); 26946 } 26947 } 26948 user_specified = B_TRUE; 26949 26950 connp = tcp->tcp_connp; 26951 if (is_system_labeled()) { 26952 zone = crgetzone(cr); 26953 addrtype = tsol_mlp_addr_type(zone->zone_id, 26954 IPV6_VERSION, &v6addr, 26955 tcps->tcps_netstack->netstack_ip); 26956 if (addrtype == mlptSingle) { 26957 return (-TNOADDR); 26958 } 26959 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26960 requested_port, addrtype); 26961 } 26962 } 26963 26964 if (mlptype != mlptSingle) { 26965 if (secpolicy_net_bindmlp(cr) != 0) { 26966 if (tcp->tcp_debug) { 26967 (void) strlog(TCP_MOD_ID, 0, 1, 26968 SL_ERROR|SL_TRACE, 26969 "tcp_bind: no priv for multilevel port %d", 26970 requested_port); 26971 } 26972 return (-TACCES); 26973 } 26974 26975 /* 26976 * If we're specifically binding a shared IP address and the 26977 * port is MLP on shared addresses, then check to see if this 26978 * zone actually owns the MLP. Reject if not. 26979 */ 26980 if (mlptype == mlptShared && addrtype == mlptShared) { 26981 /* 26982 * No need to handle exclusive-stack zones since 26983 * ALL_ZONES only applies to the shared stack. 26984 */ 26985 zoneid_t mlpzone; 26986 26987 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26988 htons(mlp_port)); 26989 if (connp->conn_zoneid != mlpzone) { 26990 if (tcp->tcp_debug) { 26991 (void) strlog(TCP_MOD_ID, 0, 1, 26992 SL_ERROR|SL_TRACE, 26993 "tcp_bind: attempt to bind port " 26994 "%d on shared addr in zone %d " 26995 "(should be %d)", 26996 mlp_port, connp->conn_zoneid, 26997 mlpzone); 26998 } 26999 return (-TACCES); 27000 } 27001 } 27002 27003 if (!user_specified) { 27004 int err; 27005 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27006 requested_port, B_TRUE); 27007 if (err != 0) { 27008 if (tcp->tcp_debug) { 27009 (void) strlog(TCP_MOD_ID, 0, 1, 27010 SL_ERROR|SL_TRACE, 27011 "tcp_bind: cannot establish anon " 27012 "MLP for port %d", 27013 requested_port); 27014 } 27015 return (err); 27016 } 27017 connp->conn_anon_port = B_TRUE; 27018 } 27019 connp->conn_mlp_type = mlptype; 27020 } 27021 27022 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 27023 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 27024 27025 if (allocated_port == 0) { 27026 connp->conn_mlp_type = mlptSingle; 27027 if (connp->conn_anon_port) { 27028 connp->conn_anon_port = B_FALSE; 27029 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27030 requested_port, B_FALSE); 27031 } 27032 if (bind_to_req_port_only) { 27033 if (tcp->tcp_debug) { 27034 (void) strlog(TCP_MOD_ID, 0, 1, 27035 SL_ERROR|SL_TRACE, 27036 "tcp_bind: requested addr busy"); 27037 } 27038 return (-TADDRBUSY); 27039 } else { 27040 /* If we are out of ports, fail the bind. */ 27041 if (tcp->tcp_debug) { 27042 (void) strlog(TCP_MOD_ID, 0, 1, 27043 SL_ERROR|SL_TRACE, 27044 "tcp_bind: out of ports?"); 27045 } 27046 return (-TNOADDR); 27047 } 27048 } 27049 27050 /* Pass the allocated port back */ 27051 *requested_port_ptr = allocated_port; 27052 return (0); 27053 } 27054 27055 static int 27056 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27057 boolean_t bind_to_req_port_only) 27058 { 27059 tcp_t *tcp = connp->conn_tcp; 27060 sin_t *sin; 27061 sin6_t *sin6; 27062 sin6_t sin6addr; 27063 in_port_t requested_port; 27064 ipaddr_t v4addr; 27065 in6_addr_t v6addr; 27066 uint_t origipversion; 27067 int error = 0; 27068 27069 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 27070 27071 if (tcp->tcp_state == TCPS_BOUND) { 27072 return (0); 27073 } else if (tcp->tcp_state > TCPS_BOUND) { 27074 if (tcp->tcp_debug) { 27075 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27076 "tcp_bind: bad state, %d", tcp->tcp_state); 27077 } 27078 return (-TOUTSTATE); 27079 } 27080 origipversion = tcp->tcp_ipversion; 27081 27082 if (sa != NULL && !OK_32PTR((char *)sa)) { 27083 if (tcp->tcp_debug) { 27084 (void) strlog(TCP_MOD_ID, 0, 1, 27085 SL_ERROR|SL_TRACE, 27086 "tcp_bind: bad address parameter, " 27087 "address %p, len %d", 27088 (void *)sa, len); 27089 } 27090 return (-TPROTO); 27091 } 27092 27093 switch (len) { 27094 case 0: /* request for a generic port */ 27095 if (tcp->tcp_family == AF_INET) { 27096 sin = (sin_t *)&sin6addr; 27097 *sin = sin_null; 27098 sin->sin_family = AF_INET; 27099 tcp->tcp_ipversion = IPV4_VERSION; 27100 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 27101 } else { 27102 ASSERT(tcp->tcp_family == AF_INET6); 27103 sin6 = (sin6_t *)&sin6addr; 27104 *sin6 = sin6_null; 27105 sin6->sin6_family = AF_INET6; 27106 tcp->tcp_ipversion = IPV6_VERSION; 27107 V6_SET_ZERO(v6addr); 27108 } 27109 requested_port = 0; 27110 break; 27111 27112 case sizeof (sin_t): /* Complete IPv4 address */ 27113 sin = (sin_t *)sa; 27114 /* 27115 * With sockets sockfs will accept bogus sin_family in 27116 * bind() and replace it with the family used in the socket 27117 * call. 27118 */ 27119 if (sin->sin_family != AF_INET || 27120 tcp->tcp_family != AF_INET) { 27121 return (EAFNOSUPPORT); 27122 } 27123 requested_port = ntohs(sin->sin_port); 27124 tcp->tcp_ipversion = IPV4_VERSION; 27125 v4addr = sin->sin_addr.s_addr; 27126 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 27127 break; 27128 27129 case sizeof (sin6_t): /* Complete IPv6 address */ 27130 sin6 = (sin6_t *)sa; 27131 if (sin6->sin6_family != AF_INET6 || 27132 tcp->tcp_family != AF_INET6) { 27133 return (EAFNOSUPPORT); 27134 } 27135 requested_port = ntohs(sin6->sin6_port); 27136 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 27137 IPV4_VERSION : IPV6_VERSION; 27138 v6addr = sin6->sin6_addr; 27139 break; 27140 27141 default: 27142 if (tcp->tcp_debug) { 27143 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27144 "tcp_bind: bad address length, %d", len); 27145 } 27146 return (EAFNOSUPPORT); 27147 /* return (-TBADADDR); */ 27148 } 27149 27150 tcp->tcp_bound_source_v6 = v6addr; 27151 27152 /* Check for change in ipversion */ 27153 if (origipversion != tcp->tcp_ipversion) { 27154 ASSERT(tcp->tcp_family == AF_INET6); 27155 error = tcp->tcp_ipversion == IPV6_VERSION ? 27156 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 27157 if (error) { 27158 return (ENOMEM); 27159 } 27160 } 27161 27162 /* 27163 * Initialize family specific fields. Copy of the src addr. 27164 * in tcp_t is needed for the lookup funcs. 27165 */ 27166 if (tcp->tcp_ipversion == IPV6_VERSION) { 27167 tcp->tcp_ip6h->ip6_src = v6addr; 27168 } else { 27169 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 27170 } 27171 tcp->tcp_ip_src_v6 = v6addr; 27172 27173 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 27174 27175 error = tcp_bind_select_lport(tcp, &requested_port, 27176 bind_to_req_port_only, cr); 27177 27178 return (error); 27179 } 27180 27181 /* 27182 * Return unix error is tli error is TSYSERR, otherwise return a negative 27183 * tli error. 27184 */ 27185 int 27186 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27187 boolean_t bind_to_req_port_only) 27188 { 27189 int error; 27190 tcp_t *tcp = connp->conn_tcp; 27191 27192 if (tcp->tcp_state >= TCPS_BOUND) { 27193 if (tcp->tcp_debug) { 27194 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27195 "tcp_bind: bad state, %d", tcp->tcp_state); 27196 } 27197 return (-TOUTSTATE); 27198 } 27199 27200 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 27201 if (error != 0) 27202 return (error); 27203 27204 ASSERT(tcp->tcp_state == TCPS_BOUND); 27205 27206 tcp->tcp_conn_req_max = 0; 27207 27208 /* 27209 * We need to make sure that the conn_recv is set to a non-null 27210 * value before we insert the conn into the classifier table. 27211 * This is to avoid a race with an incoming packet which does an 27212 * ipcl_classify(). 27213 */ 27214 connp->conn_recv = tcp_conn_request; 27215 27216 if (tcp->tcp_family == AF_INET6) { 27217 ASSERT(tcp->tcp_connp->conn_af_isv6); 27218 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27219 &tcp->tcp_bound_source_v6, 0, B_FALSE); 27220 } else { 27221 ASSERT(!tcp->tcp_connp->conn_af_isv6); 27222 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 27223 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 27224 } 27225 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27226 } 27227 27228 int 27229 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 27230 socklen_t len, cred_t *cr) 27231 { 27232 int error; 27233 conn_t *connp = (conn_t *)proto_handle; 27234 squeue_t *sqp = connp->conn_sqp; 27235 27236 /* All Solaris components should pass a cred for this operation. */ 27237 ASSERT(cr != NULL); 27238 27239 ASSERT(sqp != NULL); 27240 ASSERT(connp->conn_upper_handle != NULL); 27241 27242 error = squeue_synch_enter(sqp, connp, 0); 27243 if (error != 0) { 27244 /* failed to enter */ 27245 return (ENOSR); 27246 } 27247 27248 /* binding to a NULL address really means unbind */ 27249 if (sa == NULL) { 27250 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 27251 error = tcp_do_unbind(connp); 27252 else 27253 error = EINVAL; 27254 } else { 27255 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 27256 } 27257 27258 squeue_synch_exit(sqp, connp); 27259 27260 if (error < 0) { 27261 if (error == -TOUTSTATE) 27262 error = EINVAL; 27263 else 27264 error = proto_tlitosyserr(-error); 27265 } 27266 27267 return (error); 27268 } 27269 27270 /* 27271 * If the return value from this function is positive, it's a UNIX error. 27272 * Otherwise, if it's negative, then the absolute value is a TLI error. 27273 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 27274 */ 27275 int 27276 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 27277 cred_t *cr, pid_t pid) 27278 { 27279 tcp_t *tcp = connp->conn_tcp; 27280 sin_t *sin = (sin_t *)sa; 27281 sin6_t *sin6 = (sin6_t *)sa; 27282 ipaddr_t *dstaddrp; 27283 in_port_t dstport; 27284 uint_t srcid; 27285 int error = 0; 27286 27287 switch (len) { 27288 default: 27289 /* 27290 * Should never happen 27291 */ 27292 return (EINVAL); 27293 27294 case sizeof (sin_t): 27295 sin = (sin_t *)sa; 27296 if (sin->sin_port == 0) { 27297 return (-TBADADDR); 27298 } 27299 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 27300 return (EAFNOSUPPORT); 27301 } 27302 break; 27303 27304 case sizeof (sin6_t): 27305 sin6 = (sin6_t *)sa; 27306 if (sin6->sin6_port == 0) { 27307 return (-TBADADDR); 27308 } 27309 break; 27310 } 27311 /* 27312 * If we're connecting to an IPv4-mapped IPv6 address, we need to 27313 * make sure that the template IP header in the tcp structure is an 27314 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 27315 * need to this before we call tcp_bindi() so that the port lookup 27316 * code will look for ports in the correct port space (IPv4 and 27317 * IPv6 have separate port spaces). 27318 */ 27319 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 27320 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27321 int err = 0; 27322 27323 err = tcp_header_init_ipv4(tcp); 27324 if (err != 0) { 27325 error = ENOMEM; 27326 goto connect_failed; 27327 } 27328 if (tcp->tcp_lport != 0) 27329 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 27330 } 27331 27332 switch (tcp->tcp_state) { 27333 case TCPS_LISTEN: 27334 /* 27335 * Listening sockets are not allowed to issue connect(). 27336 */ 27337 if (IPCL_IS_NONSTR(connp)) 27338 return (EOPNOTSUPP); 27339 /* FALLTHRU */ 27340 case TCPS_IDLE: 27341 /* 27342 * We support quick connect, refer to comments in 27343 * tcp_connect_*() 27344 */ 27345 /* FALLTHRU */ 27346 case TCPS_BOUND: 27347 /* 27348 * We must bump the generation before the operation start. 27349 * This is done to ensure that any upcall made later on sends 27350 * up the right generation to the socket. 27351 */ 27352 SOCK_CONNID_BUMP(tcp->tcp_connid); 27353 27354 if (tcp->tcp_family == AF_INET6) { 27355 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27356 return (tcp_connect_ipv6(tcp, 27357 &sin6->sin6_addr, 27358 sin6->sin6_port, sin6->sin6_flowinfo, 27359 sin6->__sin6_src_id, sin6->sin6_scope_id, 27360 cr, pid)); 27361 } 27362 /* 27363 * Destination adress is mapped IPv6 address. 27364 * Source bound address should be unspecified or 27365 * IPv6 mapped address as well. 27366 */ 27367 if (!IN6_IS_ADDR_UNSPECIFIED( 27368 &tcp->tcp_bound_source_v6) && 27369 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 27370 return (EADDRNOTAVAIL); 27371 } 27372 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 27373 dstport = sin6->sin6_port; 27374 srcid = sin6->__sin6_src_id; 27375 } else { 27376 dstaddrp = &sin->sin_addr.s_addr; 27377 dstport = sin->sin_port; 27378 srcid = 0; 27379 } 27380 27381 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 27382 pid); 27383 break; 27384 default: 27385 return (-TOUTSTATE); 27386 } 27387 /* 27388 * Note: Code below is the "failure" case 27389 */ 27390 connect_failed: 27391 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 27392 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 27393 return (error); 27394 } 27395 27396 int 27397 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 27398 socklen_t len, sock_connid_t *id, cred_t *cr) 27399 { 27400 conn_t *connp = (conn_t *)proto_handle; 27401 tcp_t *tcp = connp->conn_tcp; 27402 squeue_t *sqp = connp->conn_sqp; 27403 int error; 27404 27405 ASSERT(connp->conn_upper_handle != NULL); 27406 27407 /* All Solaris components should pass a cred for this operation. */ 27408 ASSERT(cr != NULL); 27409 27410 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 27411 if (error != 0) { 27412 return (error); 27413 } 27414 27415 error = squeue_synch_enter(sqp, connp, 0); 27416 if (error != 0) { 27417 /* failed to enter */ 27418 return (ENOSR); 27419 } 27420 27421 /* 27422 * TCP supports quick connect, so no need to do an implicit bind 27423 */ 27424 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 27425 if (error == 0) { 27426 *id = connp->conn_tcp->tcp_connid; 27427 } else if (error < 0) { 27428 if (error == -TOUTSTATE) { 27429 switch (connp->conn_tcp->tcp_state) { 27430 case TCPS_SYN_SENT: 27431 error = EALREADY; 27432 break; 27433 case TCPS_ESTABLISHED: 27434 error = EISCONN; 27435 break; 27436 case TCPS_LISTEN: 27437 error = EOPNOTSUPP; 27438 break; 27439 default: 27440 error = EINVAL; 27441 break; 27442 } 27443 } else { 27444 error = proto_tlitosyserr(-error); 27445 } 27446 } 27447 done: 27448 squeue_synch_exit(sqp, connp); 27449 27450 return ((error == 0) ? EINPROGRESS : error); 27451 } 27452 27453 /* ARGSUSED */ 27454 sock_lower_handle_t 27455 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 27456 uint_t *smodep, int *errorp, int flags, cred_t *credp) 27457 { 27458 conn_t *connp; 27459 boolean_t isv6 = family == AF_INET6; 27460 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 27461 (proto != 0 && proto != IPPROTO_TCP)) { 27462 *errorp = EPROTONOSUPPORT; 27463 return (NULL); 27464 } 27465 27466 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 27467 if (connp == NULL) { 27468 return (NULL); 27469 } 27470 27471 /* 27472 * Put the ref for TCP. Ref for IP was already put 27473 * by ipcl_conn_create. Also Make the conn_t globally 27474 * visible to walkers 27475 */ 27476 mutex_enter(&connp->conn_lock); 27477 CONN_INC_REF_LOCKED(connp); 27478 ASSERT(connp->conn_ref == 2); 27479 connp->conn_state_flags &= ~CONN_INCIPIENT; 27480 27481 connp->conn_flags |= IPCL_NONSTR; 27482 mutex_exit(&connp->conn_lock); 27483 27484 ASSERT(errorp != NULL); 27485 *errorp = 0; 27486 *sock_downcalls = &sock_tcp_downcalls; 27487 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 27488 SM_SENDFILESUPP; 27489 27490 return ((sock_lower_handle_t)connp); 27491 } 27492 27493 /* ARGSUSED */ 27494 void 27495 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 27496 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 27497 { 27498 conn_t *connp = (conn_t *)proto_handle; 27499 struct sock_proto_props sopp; 27500 27501 ASSERT(connp->conn_upper_handle == NULL); 27502 27503 /* All Solaris components should pass a cred for this operation. */ 27504 ASSERT(cr != NULL); 27505 27506 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 27507 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 27508 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 27509 27510 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 27511 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 27512 sopp.sopp_maxpsz = INFPSZ; 27513 sopp.sopp_maxblk = INFPSZ; 27514 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 27515 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 27516 sopp.sopp_maxaddrlen = sizeof (sin6_t); 27517 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 27518 tcp_rinfo.mi_minpsz; 27519 27520 connp->conn_upcalls = sock_upcalls; 27521 connp->conn_upper_handle = sock_handle; 27522 27523 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 27524 } 27525 27526 /* ARGSUSED */ 27527 int 27528 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 27529 { 27530 conn_t *connp = (conn_t *)proto_handle; 27531 27532 ASSERT(connp->conn_upper_handle != NULL); 27533 27534 /* All Solaris components should pass a cred for this operation. */ 27535 ASSERT(cr != NULL); 27536 27537 tcp_close_common(connp, flags); 27538 27539 ip_free_helper_stream(connp); 27540 27541 /* 27542 * Drop IP's reference on the conn. This is the last reference 27543 * on the connp if the state was less than established. If the 27544 * connection has gone into timewait state, then we will have 27545 * one ref for the TCP and one more ref (total of two) for the 27546 * classifier connected hash list (a timewait connections stays 27547 * in connected hash till closed). 27548 * 27549 * We can't assert the references because there might be other 27550 * transient reference places because of some walkers or queued 27551 * packets in squeue for the timewait state. 27552 */ 27553 CONN_DEC_REF(connp); 27554 return (0); 27555 } 27556 27557 /* ARGSUSED */ 27558 int 27559 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 27560 cred_t *cr) 27561 { 27562 tcp_t *tcp; 27563 uint32_t msize; 27564 conn_t *connp = (conn_t *)proto_handle; 27565 int32_t tcpstate; 27566 27567 /* All Solaris components should pass a cred for this operation. */ 27568 ASSERT(cr != NULL); 27569 27570 ASSERT(connp->conn_ref >= 2); 27571 ASSERT(connp->conn_upper_handle != NULL); 27572 27573 if (msg->msg_controllen != 0) { 27574 return (EOPNOTSUPP); 27575 27576 } 27577 switch (DB_TYPE(mp)) { 27578 case M_DATA: 27579 tcp = connp->conn_tcp; 27580 ASSERT(tcp != NULL); 27581 27582 tcpstate = tcp->tcp_state; 27583 if (tcpstate < TCPS_ESTABLISHED) { 27584 freemsg(mp); 27585 return (ENOTCONN); 27586 } else if (tcpstate > TCPS_CLOSE_WAIT) { 27587 freemsg(mp); 27588 return (EPIPE); 27589 } 27590 27591 msize = msgdsize(mp); 27592 27593 mutex_enter(&tcp->tcp_non_sq_lock); 27594 tcp->tcp_squeue_bytes += msize; 27595 /* 27596 * Squeue Flow Control 27597 */ 27598 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 27599 tcp_setqfull(tcp); 27600 } 27601 mutex_exit(&tcp->tcp_non_sq_lock); 27602 27603 /* 27604 * The application may pass in an address in the msghdr, but 27605 * we ignore the address on connection-oriented sockets. 27606 * Just like BSD this code does not generate an error for 27607 * TCP (a CONNREQUIRED socket) when sending to an address 27608 * passed in with sendto/sendmsg. Instead the data is 27609 * delivered on the connection as if no address had been 27610 * supplied. 27611 */ 27612 CONN_INC_REF(connp); 27613 27614 if (msg != NULL && msg->msg_flags & MSG_OOB) { 27615 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 27616 tcp_output_urgent, connp, tcp_squeue_flag, 27617 SQTAG_TCP_OUTPUT); 27618 } else { 27619 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 27620 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 27621 } 27622 27623 return (0); 27624 27625 default: 27626 ASSERT(0); 27627 } 27628 27629 freemsg(mp); 27630 return (0); 27631 } 27632 27633 /* ARGSUSED */ 27634 void 27635 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 27636 { 27637 int len; 27638 uint32_t msize; 27639 conn_t *connp = (conn_t *)arg; 27640 tcp_t *tcp = connp->conn_tcp; 27641 27642 msize = msgdsize(mp); 27643 27644 len = msize - 1; 27645 if (len < 0) { 27646 freemsg(mp); 27647 return; 27648 } 27649 27650 /* 27651 * Try to force urgent data out on the wire. 27652 * Even if we have unsent data this will 27653 * at least send the urgent flag. 27654 * XXX does not handle more flag correctly. 27655 */ 27656 len += tcp->tcp_unsent; 27657 len += tcp->tcp_snxt; 27658 tcp->tcp_urg = len; 27659 tcp->tcp_valid_bits |= TCP_URG_VALID; 27660 27661 /* Bypass tcp protocol for fused tcp loopback */ 27662 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27663 return; 27664 tcp_wput_data(tcp, mp, B_TRUE); 27665 } 27666 27667 /* ARGSUSED */ 27668 int 27669 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27670 socklen_t *addrlenp, cred_t *cr) 27671 { 27672 conn_t *connp = (conn_t *)proto_handle; 27673 tcp_t *tcp = connp->conn_tcp; 27674 27675 ASSERT(connp->conn_upper_handle != NULL); 27676 /* All Solaris components should pass a cred for this operation. */ 27677 ASSERT(cr != NULL); 27678 27679 ASSERT(tcp != NULL); 27680 27681 return (tcp_do_getpeername(tcp, addr, addrlenp)); 27682 } 27683 27684 /* ARGSUSED */ 27685 int 27686 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27687 socklen_t *addrlenp, cred_t *cr) 27688 { 27689 conn_t *connp = (conn_t *)proto_handle; 27690 tcp_t *tcp = connp->conn_tcp; 27691 27692 /* All Solaris components should pass a cred for this operation. */ 27693 ASSERT(cr != NULL); 27694 27695 ASSERT(connp->conn_upper_handle != NULL); 27696 27697 return (tcp_do_getsockname(tcp, addr, addrlenp)); 27698 } 27699 27700 /* 27701 * tcp_fallback 27702 * 27703 * A direct socket is falling back to using STREAMS. Hanging 27704 * off of the queue is a temporary tcp_t, which was created using 27705 * tcp_open(). The tcp_open() was called as part of the regular 27706 * sockfs create path, i.e., the SO_SOCKSTR flag is passed down, 27707 * and therefore the temporary tcp_t is marked to be a socket 27708 * (i.e., IPCL_SOCKET, tcp_issocket). So the optimizations 27709 * introduced by FireEngine will be used. 27710 * 27711 * The tcp_t associated with the socket falling back will 27712 * still be marked as a socket, although the direct socket flag 27713 * (IPCL_NONSTR) is removed. A fall back to true TPI semantics 27714 * will not take place until a _SIOCSOCKFALLBACK ioctl is issued. 27715 * 27716 * If the above mentioned behavior, i.e., the tmp tcp_t is created 27717 * as a STREAMS/TPI endpoint, then we will need to do more work here. 27718 * Such as inserting the direct socket into the acceptor hash. 27719 */ 27720 void 27721 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27722 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27723 { 27724 tcp_t *tcp, *eager; 27725 conn_t *connp = (conn_t *)proto_handle; 27726 int error; 27727 struct T_capability_ack tca; 27728 struct sockaddr_in6 laddr, faddr; 27729 socklen_t laddrlen, faddrlen; 27730 short opts; 27731 struct stroptions *stropt; 27732 mblk_t *stropt_mp; 27733 mblk_t *mp; 27734 mblk_t *conn_ind_head = NULL; 27735 mblk_t *conn_ind_tail = NULL; 27736 mblk_t *ordrel_mp; 27737 mblk_t *fused_sigurp_mp; 27738 27739 tcp = connp->conn_tcp; 27740 /* 27741 * No support for acceptor fallback 27742 */ 27743 ASSERT(q->q_qinfo != &tcp_acceptor_rinit); 27744 27745 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL); 27746 27747 /* Pre-allocate the T_ordrel_ind mblk. */ 27748 ASSERT(tcp->tcp_ordrel_mp == NULL); 27749 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27750 STR_NOSIG, NULL); 27751 ordrel_mp->b_datap->db_type = M_PROTO; 27752 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27753 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27754 27755 /* Pre-allocate the M_PCSIG anyway */ 27756 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27757 27758 /* 27759 * Enter the squeue so that no new packets can come in 27760 */ 27761 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27762 if (error != 0) { 27763 /* failed to enter, free all the pre-allocated messages. */ 27764 freeb(stropt_mp); 27765 freeb(ordrel_mp); 27766 freeb(fused_sigurp_mp); 27767 return; 27768 } 27769 27770 /* Disable I/OAT during fallback */ 27771 tcp->tcp_sodirect = NULL; 27772 27773 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27774 connp->conn_minor_arena = WR(q)->q_ptr; 27775 27776 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27777 27778 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27779 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27780 27781 WR(q)->q_qinfo = &tcp_sock_winit; 27782 27783 if (!direct_sockfs) 27784 tcp_disable_direct_sockfs(tcp); 27785 27786 /* 27787 * free the helper stream 27788 */ 27789 ip_free_helper_stream(connp); 27790 27791 /* 27792 * Notify the STREAM head about options 27793 */ 27794 DB_TYPE(stropt_mp) = M_SETOPTS; 27795 stropt = (struct stroptions *)stropt_mp->b_rptr; 27796 stropt_mp->b_wptr += sizeof (struct stroptions); 27797 stropt = (struct stroptions *)stropt_mp->b_rptr; 27798 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27799 27800 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27801 tcp->tcp_tcps->tcps_wroff_xtra); 27802 if (tcp->tcp_snd_sack_ok) 27803 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27804 stropt->so_hiwat = tcp->tcp_fused ? 27805 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27806 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27807 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27808 27809 putnext(RD(q), stropt_mp); 27810 27811 /* 27812 * Collect the information needed to sync with the sonode 27813 */ 27814 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27815 27816 laddrlen = faddrlen = sizeof (sin6_t); 27817 (void) tcp_getsockname(proto_handle, (struct sockaddr *)&laddr, 27818 &laddrlen, CRED()); 27819 error = tcp_getpeername(proto_handle, (struct sockaddr *)&faddr, 27820 &faddrlen, CRED()); 27821 if (error != 0) 27822 faddrlen = 0; 27823 27824 opts = 0; 27825 if (tcp->tcp_oobinline) 27826 opts |= SO_OOBINLINE; 27827 if (tcp->tcp_dontroute) 27828 opts |= SO_DONTROUTE; 27829 27830 /* 27831 * Notify the socket that the protocol is now quiescent, 27832 * and it's therefore safe move data from the socket 27833 * to the stream head. 27834 */ 27835 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27836 (struct sockaddr *)&laddr, laddrlen, 27837 (struct sockaddr *)&faddr, faddrlen, opts); 27838 27839 while ((mp = tcp->tcp_rcv_list) != NULL) { 27840 tcp->tcp_rcv_list = mp->b_next; 27841 mp->b_next = NULL; 27842 putnext(q, mp); 27843 } 27844 tcp->tcp_rcv_last_head = NULL; 27845 tcp->tcp_rcv_last_tail = NULL; 27846 tcp->tcp_rcv_cnt = 0; 27847 27848 /* 27849 * No longer a direct socket 27850 */ 27851 connp->conn_flags &= ~IPCL_NONSTR; 27852 27853 tcp->tcp_ordrel_mp = ordrel_mp; 27854 27855 if (tcp->tcp_fused) { 27856 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27857 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27858 } else { 27859 freeb(fused_sigurp_mp); 27860 } 27861 27862 /* 27863 * Send T_CONN_IND messages for all ESTABLISHED connections. 27864 */ 27865 mutex_enter(&tcp->tcp_eager_lock); 27866 for (eager = tcp->tcp_eager_next_q; eager != NULL; 27867 eager = eager->tcp_eager_next_q) { 27868 mp = eager->tcp_conn.tcp_eager_conn_ind; 27869 27870 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27871 ASSERT(mp != NULL); 27872 /* 27873 * TLI/XTI applications will get confused by 27874 * sending eager as an option since it violates 27875 * the option semantics. So remove the eager as 27876 * option since TLI/XTI app doesn't need it anyway. 27877 */ 27878 if (!TCP_IS_SOCKET(tcp)) { 27879 struct T_conn_ind *conn_ind; 27880 27881 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27882 conn_ind->OPT_length = 0; 27883 conn_ind->OPT_offset = 0; 27884 } 27885 if (conn_ind_head == NULL) { 27886 conn_ind_head = mp; 27887 } else { 27888 conn_ind_tail->b_next = mp; 27889 } 27890 conn_ind_tail = mp; 27891 } 27892 mutex_exit(&tcp->tcp_eager_lock); 27893 27894 mp = conn_ind_head; 27895 while (mp != NULL) { 27896 mblk_t *nmp = mp->b_next; 27897 mp->b_next = NULL; 27898 27899 putnext(tcp->tcp_rq, mp); 27900 mp = nmp; 27901 } 27902 27903 /* 27904 * There should be atleast two ref's (IP + TCP) 27905 */ 27906 ASSERT(connp->conn_ref >= 2); 27907 squeue_synch_exit(connp->conn_sqp, connp); 27908 } 27909 27910 /* ARGSUSED */ 27911 static void 27912 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27913 { 27914 conn_t *connp = (conn_t *)arg; 27915 tcp_t *tcp = connp->conn_tcp; 27916 27917 freemsg(mp); 27918 27919 if (tcp->tcp_fused) 27920 tcp_unfuse(tcp); 27921 27922 if (tcp_xmit_end(tcp) != 0) { 27923 /* 27924 * We were crossing FINs and got a reset from 27925 * the other side. Just ignore it. 27926 */ 27927 if (tcp->tcp_debug) { 27928 (void) strlog(TCP_MOD_ID, 0, 1, 27929 SL_ERROR|SL_TRACE, 27930 "tcp_shutdown_output() out of state %s", 27931 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27932 } 27933 } 27934 } 27935 27936 /* ARGSUSED */ 27937 int 27938 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27939 { 27940 conn_t *connp = (conn_t *)proto_handle; 27941 tcp_t *tcp = connp->conn_tcp; 27942 27943 ASSERT(connp->conn_upper_handle != NULL); 27944 27945 /* All Solaris components should pass a cred for this operation. */ 27946 ASSERT(cr != NULL); 27947 27948 /* 27949 * X/Open requires that we check the connected state. 27950 */ 27951 if (tcp->tcp_state < TCPS_SYN_SENT) 27952 return (ENOTCONN); 27953 27954 /* shutdown the send side */ 27955 if (how != SHUT_RD) { 27956 mblk_t *bp; 27957 27958 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27959 CONN_INC_REF(connp); 27960 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27961 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27962 27963 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27964 SOCK_OPCTL_SHUT_SEND, 0); 27965 } 27966 27967 /* shutdown the recv side */ 27968 if (how != SHUT_WR) 27969 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27970 SOCK_OPCTL_SHUT_RECV, 0); 27971 27972 return (0); 27973 } 27974 27975 /* 27976 * SOP_LISTEN() calls into tcp_listen(). 27977 */ 27978 /* ARGSUSED */ 27979 int 27980 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27981 { 27982 conn_t *connp = (conn_t *)proto_handle; 27983 int error; 27984 squeue_t *sqp = connp->conn_sqp; 27985 27986 ASSERT(connp->conn_upper_handle != NULL); 27987 27988 /* All Solaris components should pass a cred for this operation. */ 27989 ASSERT(cr != NULL); 27990 27991 error = squeue_synch_enter(sqp, connp, 0); 27992 if (error != 0) { 27993 /* failed to enter */ 27994 return (ENOBUFS); 27995 } 27996 27997 error = tcp_do_listen(connp, backlog, cr); 27998 if (error == 0) { 27999 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 28000 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 28001 } else if (error < 0) { 28002 if (error == -TOUTSTATE) 28003 error = EINVAL; 28004 else 28005 error = proto_tlitosyserr(-error); 28006 } 28007 squeue_synch_exit(sqp, connp); 28008 return (error); 28009 } 28010 28011 static int 28012 tcp_do_listen(conn_t *connp, int backlog, cred_t *cr) 28013 { 28014 tcp_t *tcp = connp->conn_tcp; 28015 sin_t *sin; 28016 sin6_t *sin6; 28017 int error = 0; 28018 tcp_stack_t *tcps = tcp->tcp_tcps; 28019 28020 /* All Solaris components should pass a cred for this operation. */ 28021 ASSERT(cr != NULL); 28022 28023 if (tcp->tcp_state >= TCPS_BOUND) { 28024 if ((tcp->tcp_state == TCPS_BOUND || 28025 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 28026 /* 28027 * Handle listen() increasing backlog. 28028 * This is more "liberal" then what the TPI spec 28029 * requires but is needed to avoid a t_unbind 28030 * when handling listen() since the port number 28031 * might be "stolen" between the unbind and bind. 28032 */ 28033 goto do_listen; 28034 } 28035 if (tcp->tcp_debug) { 28036 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 28037 "tcp_listen: bad state, %d", tcp->tcp_state); 28038 } 28039 return (-TOUTSTATE); 28040 } else { 28041 int32_t len; 28042 sin6_t addr; 28043 28044 /* Do an implicit bind: Request for a generic port. */ 28045 if (tcp->tcp_family == AF_INET) { 28046 len = sizeof (sin_t); 28047 sin = (sin_t *)&addr; 28048 *sin = sin_null; 28049 sin->sin_family = AF_INET; 28050 tcp->tcp_ipversion = IPV4_VERSION; 28051 } else { 28052 ASSERT(tcp->tcp_family == AF_INET6); 28053 len = sizeof (sin6_t); 28054 sin6 = (sin6_t *)&addr; 28055 *sin6 = sin6_null; 28056 sin6->sin6_family = AF_INET6; 28057 tcp->tcp_ipversion = IPV6_VERSION; 28058 } 28059 28060 error = tcp_bind_check(connp, (struct sockaddr *)&addr, len, 28061 cr, B_FALSE); 28062 if (error) 28063 return (error); 28064 /* Fall through and do the fanout insertion */ 28065 } 28066 28067 do_listen: 28068 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 28069 tcp->tcp_conn_req_max = backlog; 28070 if (tcp->tcp_conn_req_max) { 28071 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 28072 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 28073 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 28074 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 28075 /* 28076 * If this is a listener, do not reset the eager list 28077 * and other stuffs. Note that we don't check if the 28078 * existing eager list meets the new tcp_conn_req_max 28079 * requirement. 28080 */ 28081 if (tcp->tcp_state != TCPS_LISTEN) { 28082 tcp->tcp_state = TCPS_LISTEN; 28083 /* Initialize the chain. Don't need the eager_lock */ 28084 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 28085 tcp->tcp_eager_next_drop_q0 = tcp; 28086 tcp->tcp_eager_prev_drop_q0 = tcp; 28087 tcp->tcp_second_ctimer_threshold = 28088 tcps->tcps_ip_abort_linterval; 28089 } 28090 } 28091 28092 /* 28093 * We can call ip_bind directly, the processing continues 28094 * in tcp_post_ip_bind(). 28095 * 28096 * We need to make sure that the conn_recv is set to a non-null 28097 * value before we insert the conn into the classifier table. 28098 * This is to avoid a race with an incoming packet which does an 28099 * ipcl_classify(). 28100 */ 28101 connp->conn_recv = tcp_conn_request; 28102 if (tcp->tcp_family == AF_INET) { 28103 error = ip_proto_bind_laddr_v4(connp, NULL, 28104 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 28105 } else { 28106 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 28107 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 28108 } 28109 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 28110 } 28111 28112 void 28113 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 28114 { 28115 conn_t *connp = (conn_t *)proto_handle; 28116 tcp_t *tcp = connp->conn_tcp; 28117 tcp_stack_t *tcps = tcp->tcp_tcps; 28118 uint_t thwin; 28119 28120 ASSERT(connp->conn_upper_handle != NULL); 28121 28122 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 28123 28124 /* Flow control condition has been removed. */ 28125 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 28126 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 28127 << tcp->tcp_rcv_ws; 28128 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 28129 /* 28130 * Send back a window update immediately if TCP is above 28131 * ESTABLISHED state and the increase of the rcv window 28132 * that the other side knows is at least 1 MSS after flow 28133 * control is lifted. 28134 */ 28135 if (tcp->tcp_state >= TCPS_ESTABLISHED && 28136 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 28137 tcp_xmit_ctl(NULL, tcp, 28138 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 28139 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 28140 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 28141 } 28142 28143 squeue_synch_exit(connp->conn_sqp, connp); 28144 } 28145 28146 /* ARGSUSED */ 28147 int 28148 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 28149 int mode, int32_t *rvalp, cred_t *cr) 28150 { 28151 conn_t *connp = (conn_t *)proto_handle; 28152 int error; 28153 28154 ASSERT(connp->conn_upper_handle != NULL); 28155 28156 /* All Solaris components should pass a cred for this operation. */ 28157 ASSERT(cr != NULL); 28158 28159 switch (cmd) { 28160 case ND_SET: 28161 case ND_GET: 28162 case TCP_IOC_DEFAULT_Q: 28163 case _SIOCSOCKFALLBACK: 28164 case TCP_IOC_ABORT_CONN: 28165 case TI_GETPEERNAME: 28166 case TI_GETMYNAME: 28167 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 28168 cmd)); 28169 error = EINVAL; 28170 break; 28171 default: 28172 /* 28173 * Pass on to IP using helper stream 28174 */ 28175 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 28176 cmd, arg, mode, cr, rvalp); 28177 break; 28178 } 28179 return (error); 28180 } 28181 28182 sock_downcalls_t sock_tcp_downcalls = { 28183 tcp_activate, 28184 tcp_accept, 28185 tcp_bind, 28186 tcp_listen, 28187 tcp_connect, 28188 tcp_getpeername, 28189 tcp_getsockname, 28190 tcp_getsockopt, 28191 tcp_setsockopt, 28192 tcp_sendmsg, 28193 NULL, 28194 NULL, 28195 NULL, 28196 tcp_shutdown, 28197 tcp_clr_flowctrl, 28198 tcp_ioctl, 28199 tcp_close, 28200 }; 28201