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 /* 633 * TCP options struct returned from tcp_parse_options. 634 */ 635 typedef struct tcp_opt_s { 636 uint32_t tcp_opt_mss; 637 uint32_t tcp_opt_wscale; 638 uint32_t tcp_opt_ts_val; 639 uint32_t tcp_opt_ts_ecr; 640 tcp_t *tcp; 641 } tcp_opt_t; 642 643 /* 644 * TCP option struct passing information b/w lisenter and eager. 645 */ 646 struct tcp_options { 647 uint_t to_flags; 648 ssize_t to_boundif; /* IPV6_BOUND_IF */ 649 }; 650 651 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 652 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 653 654 /* 655 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 656 */ 657 658 #ifdef _BIG_ENDIAN 659 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 660 (TCPOPT_TSTAMP << 8) | 10) 661 #else 662 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 663 (TCPOPT_NOP << 8) | TCPOPT_NOP) 664 #endif 665 666 /* 667 * Flags returned from tcp_parse_options. 668 */ 669 #define TCP_OPT_MSS_PRESENT 1 670 #define TCP_OPT_WSCALE_PRESENT 2 671 #define TCP_OPT_TSTAMP_PRESENT 4 672 #define TCP_OPT_SACK_OK_PRESENT 8 673 #define TCP_OPT_SACK_PRESENT 16 674 675 /* TCP option length */ 676 #define TCPOPT_NOP_LEN 1 677 #define TCPOPT_MAXSEG_LEN 4 678 #define TCPOPT_WS_LEN 3 679 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 680 #define TCPOPT_TSTAMP_LEN 10 681 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 682 #define TCPOPT_SACK_OK_LEN 2 683 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 684 #define TCPOPT_REAL_SACK_LEN 4 685 #define TCPOPT_MAX_SACK_LEN 36 686 #define TCPOPT_HEADER_LEN 2 687 688 /* TCP cwnd burst factor. */ 689 #define TCP_CWND_INFINITE 65535 690 #define TCP_CWND_SS 3 691 #define TCP_CWND_NORMAL 5 692 693 /* Maximum TCP initial cwin (start/restart). */ 694 #define TCP_MAX_INIT_CWND 8 695 696 /* 697 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 698 * either tcp_slow_start_initial or tcp_slow_start_after idle 699 * depending on the caller. If the upper layer has not used the 700 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 701 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 702 * If the upper layer has changed set the tcp_init_cwnd, just use 703 * it to calculate the tcp_cwnd. 704 */ 705 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 706 { \ 707 if ((tcp)->tcp_init_cwnd == 0) { \ 708 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 709 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 710 } else { \ 711 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 712 } \ 713 tcp->tcp_cwnd_cnt = 0; \ 714 } 715 716 /* TCP Timer control structure */ 717 typedef struct tcpt_s { 718 pfv_t tcpt_pfv; /* The routine we are to call */ 719 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 720 } tcpt_t; 721 722 /* 723 * Functions called directly via squeue having a prototype of edesc_t. 724 */ 725 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 726 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 727 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 728 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 729 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 730 void tcp_input(void *arg, mblk_t *mp, void *arg2); 731 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 732 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 733 void tcp_output(void *arg, mblk_t *mp, void *arg2); 734 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 735 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 736 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 737 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 738 739 740 /* Prototype for TCP functions */ 741 static void tcp_random_init(void); 742 int tcp_random(void); 743 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 744 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 745 tcp_t *eager); 746 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 747 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 748 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 749 boolean_t user_specified); 750 static void tcp_closei_local(tcp_t *tcp); 751 static void tcp_close_detached(tcp_t *tcp); 752 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 753 mblk_t *idmp, mblk_t **defermp); 754 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 755 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 756 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 757 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 758 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 759 uint32_t scope_id, cred_t *cr, pid_t pid); 760 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 761 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 762 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 763 static char *tcp_display(tcp_t *tcp, char *, char); 764 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 765 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 766 static void tcp_eager_unlink(tcp_t *tcp); 767 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 768 int unixerr); 769 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 770 int tlierr, int unixerr); 771 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 772 cred_t *cr); 773 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 774 char *value, caddr_t cp, cred_t *cr); 775 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 776 char *value, caddr_t cp, cred_t *cr); 777 static int tcp_tpistate(tcp_t *tcp); 778 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 779 int caller_holds_lock); 780 static void tcp_bind_hash_remove(tcp_t *tcp); 781 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 782 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 783 static void tcp_acceptor_hash_remove(tcp_t *tcp); 784 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 785 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 786 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 787 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 788 void tcp_g_q_setup(tcp_stack_t *); 789 void tcp_g_q_create(tcp_stack_t *); 790 void tcp_g_q_destroy(tcp_stack_t *); 791 static int tcp_header_init_ipv4(tcp_t *tcp); 792 static int tcp_header_init_ipv6(tcp_t *tcp); 793 int tcp_init(tcp_t *tcp, queue_t *q); 794 static int tcp_init_values(tcp_t *tcp); 795 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 796 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 797 static void tcp_ip_notify(tcp_t *tcp); 798 static mblk_t *tcp_ire_mp(mblk_t **mpp); 799 static void tcp_iss_init(tcp_t *tcp); 800 static void tcp_keepalive_killer(void *arg); 801 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 802 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 803 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 804 int *do_disconnectp, int *t_errorp, int *sys_errorp); 805 static boolean_t tcp_allow_connopt_set(int level, int name); 806 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 807 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 808 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 809 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 810 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 811 mblk_t *mblk); 812 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 813 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 814 uchar_t *ptr, uint_t len); 815 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 816 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 817 tcp_stack_t *); 818 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 819 caddr_t cp, cred_t *cr); 820 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 821 caddr_t cp, cred_t *cr); 822 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 823 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 824 caddr_t cp, cred_t *cr); 825 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 826 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 827 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 828 static void tcp_reinit(tcp_t *tcp); 829 static void tcp_reinit_values(tcp_t *tcp); 830 831 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 832 static uint_t tcp_rcv_drain(tcp_t *tcp); 833 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 834 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 835 static void tcp_ss_rexmit(tcp_t *tcp); 836 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 837 static void tcp_process_options(tcp_t *, tcph_t *); 838 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 839 static void tcp_rsrv(queue_t *q); 840 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 841 static int tcp_snmp_state(tcp_t *tcp); 842 static void tcp_timer(void *arg); 843 static void tcp_timer_callback(void *); 844 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 845 boolean_t random); 846 static in_port_t tcp_get_next_priv_port(const tcp_t *); 847 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 848 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 849 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 850 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 851 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 852 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 853 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 854 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 855 const int num_sack_blk, int *usable, uint_t *snxt, 856 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 857 const int mdt_thres); 858 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 859 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 860 const int num_sack_blk, int *usable, uint_t *snxt, 861 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 862 const int mdt_thres); 863 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 864 int num_sack_blk); 865 static void tcp_wsrv(queue_t *q); 866 static int tcp_xmit_end(tcp_t *tcp); 867 static void tcp_ack_timer(void *arg); 868 static mblk_t *tcp_ack_mp(tcp_t *tcp); 869 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 870 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 871 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 872 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 873 uint32_t ack, int ctl); 874 static int setmaxps(queue_t *q, int maxpsz); 875 static void tcp_set_rto(tcp_t *, time_t); 876 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 877 boolean_t, boolean_t); 878 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 879 boolean_t ipsec_mctl); 880 static int tcp_build_hdrs(tcp_t *); 881 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 882 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 883 tcph_t *tcph); 884 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 885 static mblk_t *tcp_mdt_info_mp(mblk_t *); 886 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 887 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 888 const boolean_t, const uint32_t, const uint32_t, 889 const uint32_t, const uint32_t, tcp_stack_t *); 890 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 891 const uint_t, const uint_t, boolean_t *); 892 static mblk_t *tcp_lso_info_mp(mblk_t *); 893 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 894 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 895 extern mblk_t *tcp_timermp_alloc(int); 896 extern void tcp_timermp_free(tcp_t *); 897 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 898 static void tcp_stop_lingering(tcp_t *tcp); 899 static void tcp_close_linger_timeout(void *arg); 900 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 901 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 902 static void tcp_stack_fini(netstackid_t stackid, void *arg); 903 static void *tcp_g_kstat_init(tcp_g_stat_t *); 904 static void tcp_g_kstat_fini(kstat_t *); 905 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 906 static void tcp_kstat_fini(netstackid_t, kstat_t *); 907 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 908 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 909 static int tcp_kstat_update(kstat_t *kp, int rw); 910 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 911 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 912 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 913 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 914 tcph_t *tcph, mblk_t *idmp); 915 static int tcp_squeue_switch(int); 916 917 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 918 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 919 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 920 static int tcp_tpi_close(queue_t *, int); 921 static int tcp_tpi_close_accept(queue_t *); 922 923 static void tcp_squeue_add(squeue_t *); 924 static boolean_t tcp_zcopy_check(tcp_t *); 925 static void tcp_zcopy_notify(tcp_t *); 926 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 927 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 928 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 929 930 extern void tcp_kssl_input(tcp_t *, mblk_t *); 931 932 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 933 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 934 935 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 936 sock_upper_handle_t, cred_t *); 937 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 938 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 939 static int tcp_do_listen(conn_t *, struct sockaddr *, socklen_t, int, cred_t *, 940 boolean_t); 941 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 942 cred_t *, pid_t); 943 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 944 boolean_t); 945 static int tcp_do_unbind(conn_t *); 946 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 947 boolean_t); 948 949 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 950 951 /* 952 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 953 * 954 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 955 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 956 * (defined in tcp.h) needs to be filled in and passed into the kernel 957 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 958 * structure contains the four-tuple of a TCP connection and a range of TCP 959 * states (specified by ac_start and ac_end). The use of wildcard addresses 960 * and ports is allowed. Connections with a matching four tuple and a state 961 * within the specified range will be aborted. The valid states for the 962 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 963 * inclusive. 964 * 965 * An application which has its connection aborted by this ioctl will receive 966 * an error that is dependent on the connection state at the time of the abort. 967 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 968 * though a RST packet has been received. If the connection state is equal to 969 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 970 * and all resources associated with the connection will be freed. 971 */ 972 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 973 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 974 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 975 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 976 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 977 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 978 boolean_t, tcp_stack_t *); 979 980 static struct module_info tcp_rinfo = { 981 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 982 }; 983 984 static struct module_info tcp_winfo = { 985 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 986 }; 987 988 /* 989 * Entry points for TCP as a device. The normal case which supports 990 * the TCP functionality. 991 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 992 */ 993 struct qinit tcp_rinitv4 = { 994 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 995 }; 996 997 struct qinit tcp_rinitv6 = { 998 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 999 }; 1000 1001 struct qinit tcp_winit = { 1002 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1003 }; 1004 1005 /* Initial entry point for TCP in socket mode. */ 1006 struct qinit tcp_sock_winit = { 1007 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1008 }; 1009 1010 /* TCP entry point during fallback */ 1011 struct qinit tcp_fallback_sock_winit = { 1012 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 1013 }; 1014 1015 /* 1016 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1017 * an accept. Avoid allocating data structures since eager has already 1018 * been created. 1019 */ 1020 struct qinit tcp_acceptor_rinit = { 1021 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo 1022 }; 1023 1024 struct qinit tcp_acceptor_winit = { 1025 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1026 }; 1027 1028 /* 1029 * Entry points for TCP loopback (read side only) 1030 * The open routine is only used for reopens, thus no need to 1031 * have a separate one for tcp_openv6. 1032 */ 1033 struct qinit tcp_loopback_rinit = { 1034 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 1035 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1036 }; 1037 1038 /* For AF_INET aka /dev/tcp */ 1039 struct streamtab tcpinfov4 = { 1040 &tcp_rinitv4, &tcp_winit 1041 }; 1042 1043 /* For AF_INET6 aka /dev/tcp6 */ 1044 struct streamtab tcpinfov6 = { 1045 &tcp_rinitv6, &tcp_winit 1046 }; 1047 1048 sock_downcalls_t sock_tcp_downcalls; 1049 1050 /* 1051 * Have to ensure that tcp_g_q_close is not done by an 1052 * interrupt thread. 1053 */ 1054 static taskq_t *tcp_taskq; 1055 1056 /* Setable only in /etc/system. Move to ndd? */ 1057 boolean_t tcp_icmp_source_quench = B_FALSE; 1058 1059 /* 1060 * Following assumes TPI alignment requirements stay along 32 bit 1061 * boundaries 1062 */ 1063 #define ROUNDUP32(x) \ 1064 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1065 1066 /* Template for response to info request. */ 1067 static struct T_info_ack tcp_g_t_info_ack = { 1068 T_INFO_ACK, /* PRIM_type */ 1069 0, /* TSDU_size */ 1070 T_INFINITE, /* ETSDU_size */ 1071 T_INVALID, /* CDATA_size */ 1072 T_INVALID, /* DDATA_size */ 1073 sizeof (sin_t), /* ADDR_size */ 1074 0, /* OPT_size - not initialized here */ 1075 TIDUSZ, /* TIDU_size */ 1076 T_COTS_ORD, /* SERV_type */ 1077 TCPS_IDLE, /* CURRENT_state */ 1078 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1079 }; 1080 1081 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1082 T_INFO_ACK, /* PRIM_type */ 1083 0, /* TSDU_size */ 1084 T_INFINITE, /* ETSDU_size */ 1085 T_INVALID, /* CDATA_size */ 1086 T_INVALID, /* DDATA_size */ 1087 sizeof (sin6_t), /* ADDR_size */ 1088 0, /* OPT_size - not initialized here */ 1089 TIDUSZ, /* TIDU_size */ 1090 T_COTS_ORD, /* SERV_type */ 1091 TCPS_IDLE, /* CURRENT_state */ 1092 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1093 }; 1094 1095 #define MS 1L 1096 #define SECONDS (1000 * MS) 1097 #define MINUTES (60 * SECONDS) 1098 #define HOURS (60 * MINUTES) 1099 #define DAYS (24 * HOURS) 1100 1101 #define PARAM_MAX (~(uint32_t)0) 1102 1103 /* Max size IP datagram is 64k - 1 */ 1104 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1105 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1106 /* Max of the above */ 1107 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1108 1109 /* Largest TCP port number */ 1110 #define TCP_MAX_PORT (64 * 1024 - 1) 1111 1112 /* 1113 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1114 * layer header. It has to be a multiple of 4. 1115 */ 1116 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1117 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1118 1119 /* 1120 * All of these are alterable, within the min/max values given, at run time. 1121 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1122 * per the TCP spec. 1123 */ 1124 /* BEGIN CSTYLED */ 1125 static tcpparam_t lcl_tcp_param_arr[] = { 1126 /*min max value name */ 1127 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1128 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1129 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1130 { 1, 1024, 1, "tcp_conn_req_min" }, 1131 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1132 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1133 { 0, 10, 0, "tcp_debug" }, 1134 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1135 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1136 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1137 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1138 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1139 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1140 { 1, 255, 64, "tcp_ipv4_ttl"}, 1141 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1142 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1143 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1144 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1145 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1146 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1147 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1148 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1149 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1150 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1151 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1152 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1153 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1154 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1155 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1156 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1157 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1158 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1159 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1160 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1161 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1162 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1163 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1164 /* 1165 * Question: What default value should I set for tcp_strong_iss? 1166 */ 1167 { 0, 2, 1, "tcp_strong_iss"}, 1168 { 0, 65536, 20, "tcp_rtt_updates"}, 1169 { 0, 1, 1, "tcp_wscale_always"}, 1170 { 0, 1, 0, "tcp_tstamp_always"}, 1171 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1172 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1173 { 0, 16, 2, "tcp_deferred_acks_max"}, 1174 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1175 { 1, 4, 4, "tcp_slow_start_initial"}, 1176 { 0, 2, 2, "tcp_sack_permitted"}, 1177 { 0, 1, 1, "tcp_compression_enabled"}, 1178 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1179 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1180 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1181 { 0, 1, 0, "tcp_rev_src_routes"}, 1182 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1183 { 0, 16, 8, "tcp_local_dacks_max"}, 1184 { 0, 2, 1, "tcp_ecn_permitted"}, 1185 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1186 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1187 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1188 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1189 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1190 }; 1191 /* END CSTYLED */ 1192 1193 /* 1194 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1195 * each header fragment in the header buffer. Each parameter value has 1196 * to be a multiple of 4 (32-bit aligned). 1197 */ 1198 static tcpparam_t lcl_tcp_mdt_head_param = 1199 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1200 static tcpparam_t lcl_tcp_mdt_tail_param = 1201 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1202 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1203 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1204 1205 /* 1206 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1207 * the maximum number of payload buffers associated per Multidata. 1208 */ 1209 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1210 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1211 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1212 1213 /* Round up the value to the nearest mss. */ 1214 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1215 1216 /* 1217 * Set ECN capable transport (ECT) code point in IP header. 1218 * 1219 * Note that there are 2 ECT code points '01' and '10', which are called 1220 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1221 * point ECT(0) for TCP as described in RFC 2481. 1222 */ 1223 #define SET_ECT(tcp, iph) \ 1224 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1225 /* We need to clear the code point first. */ \ 1226 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1227 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1228 } else { \ 1229 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1230 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1231 } 1232 1233 /* 1234 * The format argument to pass to tcp_display(). 1235 * DISP_PORT_ONLY means that the returned string has only port info. 1236 * DISP_ADDR_AND_PORT means that the returned string also contains the 1237 * remote and local IP address. 1238 */ 1239 #define DISP_PORT_ONLY 1 1240 #define DISP_ADDR_AND_PORT 2 1241 1242 #define IS_VMLOANED_MBLK(mp) \ 1243 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1244 1245 1246 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1247 boolean_t tcp_mdt_chain = B_TRUE; 1248 1249 /* 1250 * MDT threshold in the form of effective send MSS multiplier; we take 1251 * the MDT path if the amount of unsent data exceeds the threshold value 1252 * (default threshold is 1*SMSS). 1253 */ 1254 uint_t tcp_mdt_smss_threshold = 1; 1255 1256 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1257 1258 /* 1259 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1260 * tunable settable via NDD. Otherwise, the per-connection behavior is 1261 * determined dynamically during tcp_adapt_ire(), which is the default. 1262 */ 1263 boolean_t tcp_static_maxpsz = B_FALSE; 1264 1265 /* Setable in /etc/system */ 1266 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1267 uint32_t tcp_random_anon_port = 1; 1268 1269 /* 1270 * To reach to an eager in Q0 which can be dropped due to an incoming 1271 * new SYN request when Q0 is full, a new doubly linked list is 1272 * introduced. This list allows to select an eager from Q0 in O(1) time. 1273 * This is needed to avoid spending too much time walking through the 1274 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1275 * this new list has to be a member of Q0. 1276 * This list is headed by listener's tcp_t. When the list is empty, 1277 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1278 * of listener's tcp_t point to listener's tcp_t itself. 1279 * 1280 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1281 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1282 * These macros do not affect the eager's membership to Q0. 1283 */ 1284 1285 1286 #define MAKE_DROPPABLE(listener, eager) \ 1287 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1288 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1289 = (eager); \ 1290 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1291 (eager)->tcp_eager_next_drop_q0 = \ 1292 (listener)->tcp_eager_next_drop_q0; \ 1293 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1294 } 1295 1296 #define MAKE_UNDROPPABLE(eager) \ 1297 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1298 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1299 = (eager)->tcp_eager_prev_drop_q0; \ 1300 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1301 = (eager)->tcp_eager_next_drop_q0; \ 1302 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1303 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1304 } 1305 1306 /* 1307 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1308 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1309 * data, TCP will not respond with an ACK. RFC 793 requires that 1310 * TCP responds with an ACK for such a bogus ACK. By not following 1311 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1312 * an attacker successfully spoofs an acceptable segment to our 1313 * peer; or when our peer is "confused." 1314 */ 1315 uint32_t tcp_drop_ack_unsent_cnt = 10; 1316 1317 /* 1318 * Hook functions to enable cluster networking 1319 * On non-clustered systems these vectors must always be NULL. 1320 */ 1321 1322 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1323 sa_family_t addr_family, uint8_t *laddrp, 1324 in_port_t lport, void *args) = NULL; 1325 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1326 sa_family_t addr_family, uint8_t *laddrp, 1327 in_port_t lport, void *args) = NULL; 1328 1329 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1330 boolean_t is_outgoing, 1331 sa_family_t addr_family, 1332 uint8_t *laddrp, in_port_t lport, 1333 uint8_t *faddrp, in_port_t fport, 1334 void *args) = NULL; 1335 1336 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1337 sa_family_t addr_family, uint8_t *laddrp, 1338 in_port_t lport, uint8_t *faddrp, 1339 in_port_t fport, void *args) = NULL; 1340 1341 /* 1342 * The following are defined in ip.c 1343 */ 1344 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1345 sa_family_t addr_family, uint8_t *laddrp, 1346 void *args); 1347 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1348 sa_family_t addr_family, uint8_t *laddrp, 1349 uint8_t *faddrp, void *args); 1350 1351 1352 /* 1353 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1354 */ 1355 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1356 (err) = 0; \ 1357 if (cl_inet_connect2 != NULL) { \ 1358 /* \ 1359 * Running in cluster mode - register active connection \ 1360 * information \ 1361 */ \ 1362 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1363 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1364 (err) = (*cl_inet_connect2)( \ 1365 (connp)->conn_netstack->netstack_stackid,\ 1366 IPPROTO_TCP, is_outgoing, AF_INET, \ 1367 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1368 (in_port_t)(tcp)->tcp_lport, \ 1369 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1370 (in_port_t)(tcp)->tcp_fport, NULL); \ 1371 } \ 1372 } else { \ 1373 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1374 &(tcp)->tcp_ip6h->ip6_src)) { \ 1375 (err) = (*cl_inet_connect2)( \ 1376 (connp)->conn_netstack->netstack_stackid,\ 1377 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1378 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1379 (in_port_t)(tcp)->tcp_lport, \ 1380 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1381 (in_port_t)(tcp)->tcp_fport, NULL); \ 1382 } \ 1383 } \ 1384 } \ 1385 } 1386 1387 #define CL_INET_DISCONNECT(connp, tcp) { \ 1388 if (cl_inet_disconnect != NULL) { \ 1389 /* \ 1390 * Running in cluster mode - deregister active \ 1391 * connection information \ 1392 */ \ 1393 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1394 if ((tcp)->tcp_ip_src != 0) { \ 1395 (*cl_inet_disconnect)( \ 1396 (connp)->conn_netstack->netstack_stackid,\ 1397 IPPROTO_TCP, AF_INET, \ 1398 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1399 (in_port_t)(tcp)->tcp_lport, \ 1400 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1401 (in_port_t)(tcp)->tcp_fport, NULL); \ 1402 } \ 1403 } else { \ 1404 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1405 &(tcp)->tcp_ip_src_v6)) { \ 1406 (*cl_inet_disconnect)( \ 1407 (connp)->conn_netstack->netstack_stackid,\ 1408 IPPROTO_TCP, AF_INET6, \ 1409 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1410 (in_port_t)(tcp)->tcp_lport, \ 1411 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1412 (in_port_t)(tcp)->tcp_fport, NULL); \ 1413 } \ 1414 } \ 1415 } \ 1416 } 1417 1418 /* 1419 * Cluster networking hook for traversing current connection list. 1420 * This routine is used to extract the current list of live connections 1421 * which must continue to to be dispatched to this node. 1422 */ 1423 int cl_tcp_walk_list(netstackid_t stack_id, 1424 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1425 1426 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1427 void *arg, tcp_stack_t *tcps); 1428 1429 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1430 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1431 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1432 ip6_t *, ip6h, int, 0); 1433 1434 /* 1435 * Figure out the value of window scale opton. Note that the rwnd is 1436 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1437 * We cannot find the scale value and then do a round up of tcp_rwnd 1438 * because the scale value may not be correct after that. 1439 * 1440 * Set the compiler flag to make this function inline. 1441 */ 1442 static void 1443 tcp_set_ws_value(tcp_t *tcp) 1444 { 1445 int i; 1446 uint32_t rwnd = tcp->tcp_rwnd; 1447 1448 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1449 i++, rwnd >>= 1) 1450 ; 1451 tcp->tcp_rcv_ws = i; 1452 } 1453 1454 /* 1455 * Remove a connection from the list of detached TIME_WAIT connections. 1456 * It returns B_FALSE if it can't remove the connection from the list 1457 * as the connection has already been removed from the list due to an 1458 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1459 */ 1460 static boolean_t 1461 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1462 { 1463 boolean_t locked = B_FALSE; 1464 1465 if (tcp_time_wait == NULL) { 1466 tcp_time_wait = *((tcp_squeue_priv_t **) 1467 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1468 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1469 locked = B_TRUE; 1470 } else { 1471 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1472 } 1473 1474 if (tcp->tcp_time_wait_expire == 0) { 1475 ASSERT(tcp->tcp_time_wait_next == NULL); 1476 ASSERT(tcp->tcp_time_wait_prev == NULL); 1477 if (locked) 1478 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1479 return (B_FALSE); 1480 } 1481 ASSERT(TCP_IS_DETACHED(tcp)); 1482 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1483 1484 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1485 ASSERT(tcp->tcp_time_wait_prev == NULL); 1486 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1487 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1488 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1489 NULL; 1490 } else { 1491 tcp_time_wait->tcp_time_wait_tail = NULL; 1492 } 1493 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1494 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1495 ASSERT(tcp->tcp_time_wait_next == NULL); 1496 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1497 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1498 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1499 } else { 1500 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1501 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1502 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1503 tcp->tcp_time_wait_next; 1504 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1505 tcp->tcp_time_wait_prev; 1506 } 1507 tcp->tcp_time_wait_next = NULL; 1508 tcp->tcp_time_wait_prev = NULL; 1509 tcp->tcp_time_wait_expire = 0; 1510 1511 if (locked) 1512 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1513 return (B_TRUE); 1514 } 1515 1516 /* 1517 * Add a connection to the list of detached TIME_WAIT connections 1518 * and set its time to expire. 1519 */ 1520 static void 1521 tcp_time_wait_append(tcp_t *tcp) 1522 { 1523 tcp_stack_t *tcps = tcp->tcp_tcps; 1524 tcp_squeue_priv_t *tcp_time_wait = 1525 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1526 SQPRIVATE_TCP)); 1527 1528 tcp_timers_stop(tcp); 1529 1530 /* Freed above */ 1531 ASSERT(tcp->tcp_timer_tid == 0); 1532 ASSERT(tcp->tcp_ack_tid == 0); 1533 1534 /* must have happened at the time of detaching the tcp */ 1535 ASSERT(tcp->tcp_ptpahn == NULL); 1536 ASSERT(tcp->tcp_flow_stopped == 0); 1537 ASSERT(tcp->tcp_time_wait_next == NULL); 1538 ASSERT(tcp->tcp_time_wait_prev == NULL); 1539 ASSERT(tcp->tcp_time_wait_expire == NULL); 1540 ASSERT(tcp->tcp_listener == NULL); 1541 1542 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1543 /* 1544 * The value computed below in tcp->tcp_time_wait_expire may 1545 * appear negative or wrap around. That is ok since our 1546 * interest is only in the difference between the current lbolt 1547 * value and tcp->tcp_time_wait_expire. But the value should not 1548 * be zero, since it means the tcp is not in the TIME_WAIT list. 1549 * The corresponding comparison in tcp_time_wait_collector() uses 1550 * modular arithmetic. 1551 */ 1552 tcp->tcp_time_wait_expire += 1553 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1554 if (tcp->tcp_time_wait_expire == 0) 1555 tcp->tcp_time_wait_expire = 1; 1556 1557 ASSERT(TCP_IS_DETACHED(tcp)); 1558 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1559 ASSERT(tcp->tcp_time_wait_next == NULL); 1560 ASSERT(tcp->tcp_time_wait_prev == NULL); 1561 TCP_DBGSTAT(tcps, tcp_time_wait); 1562 1563 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1564 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1565 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1566 tcp_time_wait->tcp_time_wait_head = tcp; 1567 } else { 1568 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1569 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1570 TCPS_TIME_WAIT); 1571 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1572 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1573 } 1574 tcp_time_wait->tcp_time_wait_tail = tcp; 1575 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1576 } 1577 1578 /* ARGSUSED */ 1579 void 1580 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1581 { 1582 conn_t *connp = (conn_t *)arg; 1583 tcp_t *tcp = connp->conn_tcp; 1584 tcp_stack_t *tcps = tcp->tcp_tcps; 1585 1586 ASSERT(tcp != NULL); 1587 if (tcp->tcp_state == TCPS_CLOSED) { 1588 return; 1589 } 1590 1591 ASSERT((tcp->tcp_family == AF_INET && 1592 tcp->tcp_ipversion == IPV4_VERSION) || 1593 (tcp->tcp_family == AF_INET6 && 1594 (tcp->tcp_ipversion == IPV4_VERSION || 1595 tcp->tcp_ipversion == IPV6_VERSION))); 1596 ASSERT(!tcp->tcp_listener); 1597 1598 TCP_STAT(tcps, tcp_time_wait_reap); 1599 ASSERT(TCP_IS_DETACHED(tcp)); 1600 1601 /* 1602 * Because they have no upstream client to rebind or tcp_close() 1603 * them later, we axe the connection here and now. 1604 */ 1605 tcp_close_detached(tcp); 1606 } 1607 1608 /* 1609 * Remove cached/latched IPsec references. 1610 */ 1611 void 1612 tcp_ipsec_cleanup(tcp_t *tcp) 1613 { 1614 conn_t *connp = tcp->tcp_connp; 1615 1616 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1617 1618 if (connp->conn_latch != NULL) { 1619 IPLATCH_REFRELE(connp->conn_latch, 1620 connp->conn_netstack); 1621 connp->conn_latch = NULL; 1622 } 1623 if (connp->conn_policy != NULL) { 1624 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1625 connp->conn_policy = NULL; 1626 } 1627 } 1628 1629 /* 1630 * Cleaup before placing on free list. 1631 * Disassociate from the netstack/tcp_stack_t since the freelist 1632 * is per squeue and not per netstack. 1633 */ 1634 void 1635 tcp_cleanup(tcp_t *tcp) 1636 { 1637 mblk_t *mp; 1638 char *tcp_iphc; 1639 int tcp_iphc_len; 1640 int tcp_hdr_grown; 1641 tcp_sack_info_t *tcp_sack_info; 1642 conn_t *connp = tcp->tcp_connp; 1643 tcp_stack_t *tcps = tcp->tcp_tcps; 1644 netstack_t *ns = tcps->tcps_netstack; 1645 mblk_t *tcp_rsrv_mp; 1646 1647 tcp_bind_hash_remove(tcp); 1648 1649 /* Cleanup that which needs the netstack first */ 1650 tcp_ipsec_cleanup(tcp); 1651 1652 tcp_free(tcp); 1653 1654 /* Release any SSL context */ 1655 if (tcp->tcp_kssl_ent != NULL) { 1656 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1657 tcp->tcp_kssl_ent = NULL; 1658 } 1659 1660 if (tcp->tcp_kssl_ctx != NULL) { 1661 kssl_release_ctx(tcp->tcp_kssl_ctx); 1662 tcp->tcp_kssl_ctx = NULL; 1663 } 1664 tcp->tcp_kssl_pending = B_FALSE; 1665 1666 conn_delete_ire(connp, NULL); 1667 1668 /* 1669 * Since we will bzero the entire structure, we need to 1670 * remove it and reinsert it in global hash list. We 1671 * know the walkers can't get to this conn because we 1672 * had set CONDEMNED flag earlier and checked reference 1673 * under conn_lock so walker won't pick it and when we 1674 * go the ipcl_globalhash_remove() below, no walker 1675 * can get to it. 1676 */ 1677 ipcl_globalhash_remove(connp); 1678 1679 /* 1680 * Now it is safe to decrement the reference counts. 1681 * This might be the last reference on the netstack and TCPS 1682 * in which case it will cause the tcp_g_q_close and 1683 * the freeing of the IP Instance. 1684 */ 1685 connp->conn_netstack = NULL; 1686 netstack_rele(ns); 1687 ASSERT(tcps != NULL); 1688 tcp->tcp_tcps = NULL; 1689 TCPS_REFRELE(tcps); 1690 1691 /* Save some state */ 1692 mp = tcp->tcp_timercache; 1693 1694 tcp_sack_info = tcp->tcp_sack_info; 1695 tcp_iphc = tcp->tcp_iphc; 1696 tcp_iphc_len = tcp->tcp_iphc_len; 1697 tcp_hdr_grown = tcp->tcp_hdr_grown; 1698 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1699 1700 if (connp->conn_cred != NULL) { 1701 crfree(connp->conn_cred); 1702 connp->conn_cred = NULL; 1703 } 1704 if (connp->conn_peercred != NULL) { 1705 crfree(connp->conn_peercred); 1706 connp->conn_peercred = NULL; 1707 } 1708 ipcl_conn_cleanup(connp); 1709 connp->conn_flags = IPCL_TCPCONN; 1710 bzero(tcp, sizeof (tcp_t)); 1711 1712 /* restore the state */ 1713 tcp->tcp_timercache = mp; 1714 1715 tcp->tcp_sack_info = tcp_sack_info; 1716 tcp->tcp_iphc = tcp_iphc; 1717 tcp->tcp_iphc_len = tcp_iphc_len; 1718 tcp->tcp_hdr_grown = tcp_hdr_grown; 1719 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1720 1721 tcp->tcp_connp = connp; 1722 1723 ASSERT(connp->conn_tcp == tcp); 1724 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1725 connp->conn_state_flags = CONN_INCIPIENT; 1726 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1727 ASSERT(connp->conn_ref == 1); 1728 } 1729 1730 /* 1731 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1732 * is done forwards from the head. 1733 * This walks all stack instances since 1734 * tcp_time_wait remains global across all stacks. 1735 */ 1736 /* ARGSUSED */ 1737 void 1738 tcp_time_wait_collector(void *arg) 1739 { 1740 tcp_t *tcp; 1741 clock_t now; 1742 mblk_t *mp; 1743 conn_t *connp; 1744 kmutex_t *lock; 1745 boolean_t removed; 1746 1747 squeue_t *sqp = (squeue_t *)arg; 1748 tcp_squeue_priv_t *tcp_time_wait = 1749 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1750 1751 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1752 tcp_time_wait->tcp_time_wait_tid = 0; 1753 1754 if (tcp_time_wait->tcp_free_list != NULL && 1755 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1756 TCP_G_STAT(tcp_freelist_cleanup); 1757 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1758 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1759 tcp->tcp_time_wait_next = NULL; 1760 tcp_time_wait->tcp_free_list_cnt--; 1761 ASSERT(tcp->tcp_tcps == NULL); 1762 CONN_DEC_REF(tcp->tcp_connp); 1763 } 1764 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1765 } 1766 1767 /* 1768 * In order to reap time waits reliably, we should use a 1769 * source of time that is not adjustable by the user -- hence 1770 * the call to ddi_get_lbolt(). 1771 */ 1772 now = ddi_get_lbolt(); 1773 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1774 /* 1775 * Compare times using modular arithmetic, since 1776 * lbolt can wrapover. 1777 */ 1778 if ((now - tcp->tcp_time_wait_expire) < 0) { 1779 break; 1780 } 1781 1782 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1783 ASSERT(removed); 1784 1785 connp = tcp->tcp_connp; 1786 ASSERT(connp->conn_fanout != NULL); 1787 lock = &connp->conn_fanout->connf_lock; 1788 /* 1789 * This is essentially a TW reclaim fast path optimization for 1790 * performance where the timewait collector checks under the 1791 * fanout lock (so that no one else can get access to the 1792 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1793 * the classifier hash list. If ref count is indeed 2, we can 1794 * just remove the conn under the fanout lock and avoid 1795 * cleaning up the conn under the squeue, provided that 1796 * clustering callbacks are not enabled. If clustering is 1797 * enabled, we need to make the clustering callback before 1798 * setting the CONDEMNED flag and after dropping all locks and 1799 * so we forego this optimization and fall back to the slow 1800 * path. Also please see the comments in tcp_closei_local 1801 * regarding the refcnt logic. 1802 * 1803 * Since we are holding the tcp_time_wait_lock, its better 1804 * not to block on the fanout_lock because other connections 1805 * can't add themselves to time_wait list. So we do a 1806 * tryenter instead of mutex_enter. 1807 */ 1808 if (mutex_tryenter(lock)) { 1809 mutex_enter(&connp->conn_lock); 1810 if ((connp->conn_ref == 2) && 1811 (cl_inet_disconnect == NULL)) { 1812 ipcl_hash_remove_locked(connp, 1813 connp->conn_fanout); 1814 /* 1815 * Set the CONDEMNED flag now itself so that 1816 * the refcnt cannot increase due to any 1817 * walker. But we have still not cleaned up 1818 * conn_ire_cache. This is still ok since 1819 * we are going to clean it up in tcp_cleanup 1820 * immediately and any interface unplumb 1821 * thread will wait till the ire is blown away 1822 */ 1823 connp->conn_state_flags |= CONN_CONDEMNED; 1824 mutex_exit(lock); 1825 mutex_exit(&connp->conn_lock); 1826 if (tcp_time_wait->tcp_free_list_cnt < 1827 tcp_free_list_max_cnt) { 1828 /* Add to head of tcp_free_list */ 1829 mutex_exit( 1830 &tcp_time_wait->tcp_time_wait_lock); 1831 tcp_cleanup(tcp); 1832 ASSERT(connp->conn_latch == NULL); 1833 ASSERT(connp->conn_policy == NULL); 1834 ASSERT(tcp->tcp_tcps == NULL); 1835 ASSERT(connp->conn_netstack == NULL); 1836 1837 mutex_enter( 1838 &tcp_time_wait->tcp_time_wait_lock); 1839 tcp->tcp_time_wait_next = 1840 tcp_time_wait->tcp_free_list; 1841 tcp_time_wait->tcp_free_list = tcp; 1842 tcp_time_wait->tcp_free_list_cnt++; 1843 continue; 1844 } else { 1845 /* Do not add to tcp_free_list */ 1846 mutex_exit( 1847 &tcp_time_wait->tcp_time_wait_lock); 1848 tcp_bind_hash_remove(tcp); 1849 conn_delete_ire(tcp->tcp_connp, NULL); 1850 tcp_ipsec_cleanup(tcp); 1851 CONN_DEC_REF(tcp->tcp_connp); 1852 } 1853 } else { 1854 CONN_INC_REF_LOCKED(connp); 1855 mutex_exit(lock); 1856 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1857 mutex_exit(&connp->conn_lock); 1858 /* 1859 * We can reuse the closemp here since conn has 1860 * detached (otherwise we wouldn't even be in 1861 * time_wait list). tcp_closemp_used can safely 1862 * be changed without taking a lock as no other 1863 * thread can concurrently access it at this 1864 * point in the connection lifecycle. 1865 */ 1866 1867 if (tcp->tcp_closemp.b_prev == NULL) 1868 tcp->tcp_closemp_used = B_TRUE; 1869 else 1870 cmn_err(CE_PANIC, 1871 "tcp_timewait_collector: " 1872 "concurrent use of tcp_closemp: " 1873 "connp %p tcp %p\n", (void *)connp, 1874 (void *)tcp); 1875 1876 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1877 mp = &tcp->tcp_closemp; 1878 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1879 tcp_timewait_output, connp, 1880 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1881 } 1882 } else { 1883 mutex_enter(&connp->conn_lock); 1884 CONN_INC_REF_LOCKED(connp); 1885 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1886 mutex_exit(&connp->conn_lock); 1887 /* 1888 * We can reuse the closemp here since conn has 1889 * detached (otherwise we wouldn't even be in 1890 * time_wait list). tcp_closemp_used can safely 1891 * be changed without taking a lock as no other 1892 * thread can concurrently access it at this 1893 * point in the connection lifecycle. 1894 */ 1895 1896 if (tcp->tcp_closemp.b_prev == NULL) 1897 tcp->tcp_closemp_used = B_TRUE; 1898 else 1899 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1900 "concurrent use of tcp_closemp: " 1901 "connp %p tcp %p\n", (void *)connp, 1902 (void *)tcp); 1903 1904 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1905 mp = &tcp->tcp_closemp; 1906 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1907 tcp_timewait_output, connp, 1908 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1909 } 1910 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1911 } 1912 1913 if (tcp_time_wait->tcp_free_list != NULL) 1914 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1915 1916 tcp_time_wait->tcp_time_wait_tid = 1917 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1918 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1919 CALLOUT_FLAG_ROUNDUP); 1920 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1921 } 1922 1923 /* 1924 * Reply to a clients T_CONN_RES TPI message. This function 1925 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1926 * on the acceptor STREAM and processed in tcp_wput_accept(). 1927 * Read the block comment on top of tcp_conn_request(). 1928 */ 1929 static void 1930 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1931 { 1932 tcp_t *acceptor; 1933 tcp_t *eager; 1934 tcp_t *tcp; 1935 struct T_conn_res *tcr; 1936 t_uscalar_t acceptor_id; 1937 t_scalar_t seqnum; 1938 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1939 struct tcp_options *tcpopt; 1940 mblk_t *ok_mp; 1941 mblk_t *mp1; 1942 tcp_stack_t *tcps = listener->tcp_tcps; 1943 1944 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1945 tcp_err_ack(listener, mp, TPROTO, 0); 1946 return; 1947 } 1948 tcr = (struct T_conn_res *)mp->b_rptr; 1949 1950 /* 1951 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1952 * read side queue of the streams device underneath us i.e. the 1953 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1954 * look it up in the queue_hash. Under LP64 it sends down the 1955 * minor_t of the accepting endpoint. 1956 * 1957 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1958 * fanout hash lock is held. 1959 * This prevents any thread from entering the acceptor queue from 1960 * below (since it has not been hard bound yet i.e. any inbound 1961 * packets will arrive on the listener or default tcp queue and 1962 * go through tcp_lookup). 1963 * The CONN_INC_REF will prevent the acceptor from closing. 1964 * 1965 * XXX It is still possible for a tli application to send down data 1966 * on the accepting stream while another thread calls t_accept. 1967 * This should not be a problem for well-behaved applications since 1968 * the T_OK_ACK is sent after the queue swapping is completed. 1969 * 1970 * If the accepting fd is the same as the listening fd, avoid 1971 * queue hash lookup since that will return an eager listener in a 1972 * already established state. 1973 */ 1974 acceptor_id = tcr->ACCEPTOR_id; 1975 mutex_enter(&listener->tcp_eager_lock); 1976 if (listener->tcp_acceptor_id == acceptor_id) { 1977 eager = listener->tcp_eager_next_q; 1978 /* only count how many T_CONN_INDs so don't count q0 */ 1979 if ((listener->tcp_conn_req_cnt_q != 1) || 1980 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1981 mutex_exit(&listener->tcp_eager_lock); 1982 tcp_err_ack(listener, mp, TBADF, 0); 1983 return; 1984 } 1985 if (listener->tcp_conn_req_cnt_q0 != 0) { 1986 /* Throw away all the eagers on q0. */ 1987 tcp_eager_cleanup(listener, 1); 1988 } 1989 if (listener->tcp_syn_defense) { 1990 listener->tcp_syn_defense = B_FALSE; 1991 if (listener->tcp_ip_addr_cache != NULL) { 1992 kmem_free(listener->tcp_ip_addr_cache, 1993 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1994 listener->tcp_ip_addr_cache = NULL; 1995 } 1996 } 1997 /* 1998 * Transfer tcp_conn_req_max to the eager so that when 1999 * a disconnect occurs we can revert the endpoint to the 2000 * listen state. 2001 */ 2002 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2003 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2004 /* 2005 * Get a reference on the acceptor just like the 2006 * tcp_acceptor_hash_lookup below. 2007 */ 2008 acceptor = listener; 2009 CONN_INC_REF(acceptor->tcp_connp); 2010 } else { 2011 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 2012 if (acceptor == NULL) { 2013 if (listener->tcp_debug) { 2014 (void) strlog(TCP_MOD_ID, 0, 1, 2015 SL_ERROR|SL_TRACE, 2016 "tcp_accept: did not find acceptor 0x%x\n", 2017 acceptor_id); 2018 } 2019 mutex_exit(&listener->tcp_eager_lock); 2020 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2021 return; 2022 } 2023 /* 2024 * Verify acceptor state. The acceptable states for an acceptor 2025 * include TCPS_IDLE and TCPS_BOUND. 2026 */ 2027 switch (acceptor->tcp_state) { 2028 case TCPS_IDLE: 2029 /* FALLTHRU */ 2030 case TCPS_BOUND: 2031 break; 2032 default: 2033 CONN_DEC_REF(acceptor->tcp_connp); 2034 mutex_exit(&listener->tcp_eager_lock); 2035 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2036 return; 2037 } 2038 } 2039 2040 /* The listener must be in TCPS_LISTEN */ 2041 if (listener->tcp_state != TCPS_LISTEN) { 2042 CONN_DEC_REF(acceptor->tcp_connp); 2043 mutex_exit(&listener->tcp_eager_lock); 2044 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2045 return; 2046 } 2047 2048 /* 2049 * Rendezvous with an eager connection request packet hanging off 2050 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2051 * tcp structure when the connection packet arrived in 2052 * tcp_conn_request(). 2053 */ 2054 seqnum = tcr->SEQ_number; 2055 eager = listener; 2056 do { 2057 eager = eager->tcp_eager_next_q; 2058 if (eager == NULL) { 2059 CONN_DEC_REF(acceptor->tcp_connp); 2060 mutex_exit(&listener->tcp_eager_lock); 2061 tcp_err_ack(listener, mp, TBADSEQ, 0); 2062 return; 2063 } 2064 } while (eager->tcp_conn_req_seqnum != seqnum); 2065 mutex_exit(&listener->tcp_eager_lock); 2066 2067 /* 2068 * At this point, both acceptor and listener have 2 ref 2069 * that they begin with. Acceptor has one additional ref 2070 * we placed in lookup while listener has 3 additional 2071 * ref for being behind the squeue (tcp_accept() is 2072 * done on listener's squeue); being in classifier hash; 2073 * and eager's ref on listener. 2074 */ 2075 ASSERT(listener->tcp_connp->conn_ref >= 5); 2076 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2077 2078 /* 2079 * The eager at this point is set in its own squeue and 2080 * could easily have been killed (tcp_accept_finish will 2081 * deal with that) because of a TH_RST so we can only 2082 * ASSERT for a single ref. 2083 */ 2084 ASSERT(eager->tcp_connp->conn_ref >= 1); 2085 2086 /* Pre allocate the stroptions mblk also */ 2087 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2088 sizeof (struct T_conn_res)), BPRI_HI); 2089 if (opt_mp == NULL) { 2090 CONN_DEC_REF(acceptor->tcp_connp); 2091 CONN_DEC_REF(eager->tcp_connp); 2092 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2093 return; 2094 } 2095 DB_TYPE(opt_mp) = M_SETOPTS; 2096 opt_mp->b_wptr += sizeof (struct tcp_options); 2097 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2098 tcpopt->to_flags = 0; 2099 2100 /* 2101 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2102 * from listener to acceptor. 2103 */ 2104 if (listener->tcp_bound_if != 0) { 2105 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2106 tcpopt->to_boundif = listener->tcp_bound_if; 2107 } 2108 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2109 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2110 } 2111 2112 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2113 if ((mp1 = copymsg(mp)) == NULL) { 2114 CONN_DEC_REF(acceptor->tcp_connp); 2115 CONN_DEC_REF(eager->tcp_connp); 2116 freemsg(opt_mp); 2117 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2118 return; 2119 } 2120 2121 tcr = (struct T_conn_res *)mp1->b_rptr; 2122 2123 /* 2124 * This is an expanded version of mi_tpi_ok_ack_alloc() 2125 * which allocates a larger mblk and appends the new 2126 * local address to the ok_ack. The address is copied by 2127 * soaccept() for getsockname(). 2128 */ 2129 { 2130 int extra; 2131 2132 extra = (eager->tcp_family == AF_INET) ? 2133 sizeof (sin_t) : sizeof (sin6_t); 2134 2135 /* 2136 * Try to re-use mp, if possible. Otherwise, allocate 2137 * an mblk and return it as ok_mp. In any case, mp 2138 * is no longer usable upon return. 2139 */ 2140 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2141 CONN_DEC_REF(acceptor->tcp_connp); 2142 CONN_DEC_REF(eager->tcp_connp); 2143 freemsg(opt_mp); 2144 /* Original mp has been freed by now, so use mp1 */ 2145 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2146 return; 2147 } 2148 2149 mp = NULL; /* We should never use mp after this point */ 2150 2151 switch (extra) { 2152 case sizeof (sin_t): { 2153 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2154 2155 ok_mp->b_wptr += extra; 2156 sin->sin_family = AF_INET; 2157 sin->sin_port = eager->tcp_lport; 2158 sin->sin_addr.s_addr = 2159 eager->tcp_ipha->ipha_src; 2160 break; 2161 } 2162 case sizeof (sin6_t): { 2163 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2164 2165 ok_mp->b_wptr += extra; 2166 sin6->sin6_family = AF_INET6; 2167 sin6->sin6_port = eager->tcp_lport; 2168 if (eager->tcp_ipversion == IPV4_VERSION) { 2169 sin6->sin6_flowinfo = 0; 2170 IN6_IPADDR_TO_V4MAPPED( 2171 eager->tcp_ipha->ipha_src, 2172 &sin6->sin6_addr); 2173 } else { 2174 ASSERT(eager->tcp_ip6h != NULL); 2175 sin6->sin6_flowinfo = 2176 eager->tcp_ip6h->ip6_vcf & 2177 ~IPV6_VERS_AND_FLOW_MASK; 2178 sin6->sin6_addr = 2179 eager->tcp_ip6h->ip6_src; 2180 } 2181 sin6->sin6_scope_id = 0; 2182 sin6->__sin6_src_id = 0; 2183 break; 2184 } 2185 default: 2186 break; 2187 } 2188 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2189 } 2190 2191 /* 2192 * If there are no options we know that the T_CONN_RES will 2193 * succeed. However, we can't send the T_OK_ACK upstream until 2194 * the tcp_accept_swap is done since it would be dangerous to 2195 * let the application start using the new fd prior to the swap. 2196 */ 2197 tcp_accept_swap(listener, acceptor, eager); 2198 2199 /* 2200 * tcp_accept_swap unlinks eager from listener but does not drop 2201 * the eager's reference on the listener. 2202 */ 2203 ASSERT(eager->tcp_listener == NULL); 2204 ASSERT(listener->tcp_connp->conn_ref >= 5); 2205 2206 /* 2207 * The eager is now associated with its own queue. Insert in 2208 * the hash so that the connection can be reused for a future 2209 * T_CONN_RES. 2210 */ 2211 tcp_acceptor_hash_insert(acceptor_id, eager); 2212 2213 /* 2214 * We now do the processing of options with T_CONN_RES. 2215 * We delay till now since we wanted to have queue to pass to 2216 * option processing routines that points back to the right 2217 * instance structure which does not happen until after 2218 * tcp_accept_swap(). 2219 * 2220 * Note: 2221 * The sanity of the logic here assumes that whatever options 2222 * are appropriate to inherit from listner=>eager are done 2223 * before this point, and whatever were to be overridden (or not) 2224 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2225 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2226 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2227 * This may not be true at this point in time but can be fixed 2228 * independently. This option processing code starts with 2229 * the instantiated acceptor instance and the final queue at 2230 * this point. 2231 */ 2232 2233 if (tcr->OPT_length != 0) { 2234 /* Options to process */ 2235 int t_error = 0; 2236 int sys_error = 0; 2237 int do_disconnect = 0; 2238 2239 if (tcp_conprim_opt_process(eager, mp1, 2240 &do_disconnect, &t_error, &sys_error) < 0) { 2241 eager->tcp_accept_error = 1; 2242 if (do_disconnect) { 2243 /* 2244 * An option failed which does not allow 2245 * connection to be accepted. 2246 * 2247 * We allow T_CONN_RES to succeed and 2248 * put a T_DISCON_IND on the eager queue. 2249 */ 2250 ASSERT(t_error == 0 && sys_error == 0); 2251 eager->tcp_send_discon_ind = 1; 2252 } else { 2253 ASSERT(t_error != 0); 2254 freemsg(ok_mp); 2255 /* 2256 * Original mp was either freed or set 2257 * to ok_mp above, so use mp1 instead. 2258 */ 2259 tcp_err_ack(listener, mp1, t_error, sys_error); 2260 goto finish; 2261 } 2262 } 2263 /* 2264 * Most likely success in setting options (except if 2265 * eager->tcp_send_discon_ind set). 2266 * mp1 option buffer represented by OPT_length/offset 2267 * potentially modified and contains results of setting 2268 * options at this point 2269 */ 2270 } 2271 2272 /* We no longer need mp1, since all options processing has passed */ 2273 freemsg(mp1); 2274 2275 putnext(listener->tcp_rq, ok_mp); 2276 2277 mutex_enter(&listener->tcp_eager_lock); 2278 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2279 tcp_t *tail; 2280 mblk_t *conn_ind; 2281 2282 /* 2283 * This path should not be executed if listener and 2284 * acceptor streams are the same. 2285 */ 2286 ASSERT(listener != acceptor); 2287 2288 tcp = listener->tcp_eager_prev_q0; 2289 /* 2290 * listener->tcp_eager_prev_q0 points to the TAIL of the 2291 * deferred T_conn_ind queue. We need to get to the head of 2292 * the queue in order to send up T_conn_ind the same order as 2293 * how the 3WHS is completed. 2294 */ 2295 while (tcp != listener) { 2296 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2297 break; 2298 else 2299 tcp = tcp->tcp_eager_prev_q0; 2300 } 2301 ASSERT(tcp != listener); 2302 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2303 ASSERT(conn_ind != NULL); 2304 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2305 2306 /* Move from q0 to q */ 2307 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2308 listener->tcp_conn_req_cnt_q0--; 2309 listener->tcp_conn_req_cnt_q++; 2310 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2311 tcp->tcp_eager_prev_q0; 2312 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2313 tcp->tcp_eager_next_q0; 2314 tcp->tcp_eager_prev_q0 = NULL; 2315 tcp->tcp_eager_next_q0 = NULL; 2316 tcp->tcp_conn_def_q0 = B_FALSE; 2317 2318 /* Make sure the tcp isn't in the list of droppables */ 2319 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2320 tcp->tcp_eager_prev_drop_q0 == NULL); 2321 2322 /* 2323 * Insert at end of the queue because sockfs sends 2324 * down T_CONN_RES in chronological order. Leaving 2325 * the older conn indications at front of the queue 2326 * helps reducing search time. 2327 */ 2328 tail = listener->tcp_eager_last_q; 2329 if (tail != NULL) 2330 tail->tcp_eager_next_q = tcp; 2331 else 2332 listener->tcp_eager_next_q = tcp; 2333 listener->tcp_eager_last_q = tcp; 2334 tcp->tcp_eager_next_q = NULL; 2335 mutex_exit(&listener->tcp_eager_lock); 2336 putnext(tcp->tcp_rq, conn_ind); 2337 } else { 2338 mutex_exit(&listener->tcp_eager_lock); 2339 } 2340 2341 /* 2342 * Done with the acceptor - free it 2343 * 2344 * Note: from this point on, no access to listener should be made 2345 * as listener can be equal to acceptor. 2346 */ 2347 finish: 2348 ASSERT(acceptor->tcp_detached); 2349 ASSERT(tcps->tcps_g_q != NULL); 2350 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2351 acceptor->tcp_rq = tcps->tcps_g_q; 2352 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2353 (void) tcp_clean_death(acceptor, 0, 2); 2354 CONN_DEC_REF(acceptor->tcp_connp); 2355 2356 /* 2357 * In case we already received a FIN we have to make tcp_rput send 2358 * the ordrel_ind. This will also send up a window update if the window 2359 * has opened up. 2360 * 2361 * In the normal case of a successful connection acceptance 2362 * we give the O_T_BIND_REQ to the read side put procedure as an 2363 * indication that this was just accepted. This tells tcp_rput to 2364 * pass up any data queued in tcp_rcv_list. 2365 * 2366 * In the fringe case where options sent with T_CONN_RES failed and 2367 * we required, we would be indicating a T_DISCON_IND to blow 2368 * away this connection. 2369 */ 2370 2371 /* 2372 * XXX: we currently have a problem if XTI application closes the 2373 * acceptor stream in between. This problem exists in on10-gate also 2374 * and is well know but nothing can be done short of major rewrite 2375 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2376 * eager same squeue as listener (we can distinguish non socket 2377 * listeners at the time of handling a SYN in tcp_conn_request) 2378 * and do most of the work that tcp_accept_finish does here itself 2379 * and then get behind the acceptor squeue to access the acceptor 2380 * queue. 2381 */ 2382 /* 2383 * We already have a ref on tcp so no need to do one before squeue_enter 2384 */ 2385 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2386 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2387 } 2388 2389 /* 2390 * Swap information between the eager and acceptor for a TLI/XTI client. 2391 * The sockfs accept is done on the acceptor stream and control goes 2392 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2393 * called. In either case, both the eager and listener are in their own 2394 * perimeter (squeue) and the code has to deal with potential race. 2395 * 2396 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2397 */ 2398 static void 2399 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2400 { 2401 conn_t *econnp, *aconnp; 2402 2403 ASSERT(eager->tcp_rq == listener->tcp_rq); 2404 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2405 ASSERT(!eager->tcp_hard_bound); 2406 ASSERT(!TCP_IS_SOCKET(acceptor)); 2407 ASSERT(!TCP_IS_SOCKET(eager)); 2408 ASSERT(!TCP_IS_SOCKET(listener)); 2409 2410 acceptor->tcp_detached = B_TRUE; 2411 /* 2412 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2413 * the acceptor id. 2414 */ 2415 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2416 2417 /* remove eager from listen list... */ 2418 mutex_enter(&listener->tcp_eager_lock); 2419 tcp_eager_unlink(eager); 2420 ASSERT(eager->tcp_eager_next_q == NULL && 2421 eager->tcp_eager_last_q == NULL); 2422 ASSERT(eager->tcp_eager_next_q0 == NULL && 2423 eager->tcp_eager_prev_q0 == NULL); 2424 mutex_exit(&listener->tcp_eager_lock); 2425 eager->tcp_rq = acceptor->tcp_rq; 2426 eager->tcp_wq = acceptor->tcp_wq; 2427 2428 econnp = eager->tcp_connp; 2429 aconnp = acceptor->tcp_connp; 2430 2431 eager->tcp_rq->q_ptr = econnp; 2432 eager->tcp_wq->q_ptr = econnp; 2433 2434 /* 2435 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2436 * which might be a different squeue from our peer TCP instance. 2437 * For TCP Fusion, the peer expects that whenever tcp_detached is 2438 * clear, our TCP queues point to the acceptor's queues. Thus, use 2439 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2440 * above reach global visibility prior to the clearing of tcp_detached. 2441 */ 2442 membar_producer(); 2443 eager->tcp_detached = B_FALSE; 2444 2445 ASSERT(eager->tcp_ack_tid == 0); 2446 2447 econnp->conn_dev = aconnp->conn_dev; 2448 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2449 ASSERT(econnp->conn_minor_arena != NULL); 2450 if (eager->tcp_cred != NULL) 2451 crfree(eager->tcp_cred); 2452 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2453 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2454 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2455 2456 aconnp->conn_cred = NULL; 2457 2458 econnp->conn_zoneid = aconnp->conn_zoneid; 2459 econnp->conn_allzones = aconnp->conn_allzones; 2460 2461 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2462 aconnp->conn_mac_exempt = B_FALSE; 2463 2464 ASSERT(aconnp->conn_peercred == NULL); 2465 2466 /* Do the IPC initialization */ 2467 CONN_INC_REF(econnp); 2468 2469 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2470 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2471 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2472 2473 /* Done with old IPC. Drop its ref on its connp */ 2474 CONN_DEC_REF(aconnp); 2475 } 2476 2477 2478 /* 2479 * Adapt to the information, such as rtt and rtt_sd, provided from the 2480 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2481 * 2482 * Checks for multicast and broadcast destination address. 2483 * Returns zero on failure; non-zero if ok. 2484 * 2485 * Note that the MSS calculation here is based on the info given in 2486 * the IRE. We do not do any calculation based on TCP options. They 2487 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2488 * knows which options to use. 2489 * 2490 * Note on how TCP gets its parameters for a connection. 2491 * 2492 * When a tcp_t structure is allocated, it gets all the default parameters. 2493 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2494 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2495 * default. 2496 * 2497 * An incoming SYN with a multicast or broadcast destination address, is dropped 2498 * in 1 of 2 places. 2499 * 2500 * 1. If the packet was received over the wire it is dropped in 2501 * ip_rput_process_broadcast() 2502 * 2503 * 2. If the packet was received through internal IP loopback, i.e. the packet 2504 * was generated and received on the same machine, it is dropped in 2505 * ip_wput_local() 2506 * 2507 * An incoming SYN with a multicast or broadcast source address is always 2508 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2509 * reject an attempt to connect to a broadcast or multicast (destination) 2510 * address. 2511 */ 2512 static int 2513 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2514 { 2515 ire_t *ire; 2516 ire_t *sire = NULL; 2517 iulp_t *ire_uinfo = NULL; 2518 uint32_t mss_max; 2519 uint32_t mss; 2520 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2521 conn_t *connp = tcp->tcp_connp; 2522 boolean_t ire_cacheable = B_FALSE; 2523 zoneid_t zoneid = connp->conn_zoneid; 2524 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2525 MATCH_IRE_SECATTR; 2526 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2527 ill_t *ill = NULL; 2528 boolean_t incoming = (ire_mp == NULL); 2529 tcp_stack_t *tcps = tcp->tcp_tcps; 2530 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2531 2532 ASSERT(connp->conn_ire_cache == NULL); 2533 2534 if (tcp->tcp_ipversion == IPV4_VERSION) { 2535 2536 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2537 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2538 return (0); 2539 } 2540 /* 2541 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2542 * for the destination with the nexthop as gateway. 2543 * ire_ctable_lookup() is used because this particular 2544 * ire, if it exists, will be marked private. 2545 * If that is not available, use the interface ire 2546 * for the nexthop. 2547 * 2548 * TSol: tcp_update_label will detect label mismatches based 2549 * only on the destination's label, but that would not 2550 * detect label mismatches based on the security attributes 2551 * of routes or next hop gateway. Hence we need to pass the 2552 * label to ire_ftable_lookup below in order to locate the 2553 * right prefix (and/or) ire cache. Similarly we also need 2554 * pass the label to the ire_cache_lookup below to locate 2555 * the right ire that also matches on the label. 2556 */ 2557 if (tcp->tcp_connp->conn_nexthop_set) { 2558 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2559 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2560 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2561 ipst); 2562 if (ire == NULL) { 2563 ire = ire_ftable_lookup( 2564 tcp->tcp_connp->conn_nexthop_v4, 2565 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2566 tsl, match_flags, ipst); 2567 if (ire == NULL) 2568 return (0); 2569 } else { 2570 ire_uinfo = &ire->ire_uinfo; 2571 } 2572 } else { 2573 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2574 zoneid, tsl, ipst); 2575 if (ire != NULL) { 2576 ire_cacheable = B_TRUE; 2577 ire_uinfo = (ire_mp != NULL) ? 2578 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2579 &ire->ire_uinfo; 2580 2581 } else { 2582 if (ire_mp == NULL) { 2583 ire = ire_ftable_lookup( 2584 tcp->tcp_connp->conn_rem, 2585 0, 0, 0, NULL, &sire, zoneid, 0, 2586 tsl, (MATCH_IRE_RECURSIVE | 2587 MATCH_IRE_DEFAULT), ipst); 2588 if (ire == NULL) 2589 return (0); 2590 ire_uinfo = (sire != NULL) ? 2591 &sire->ire_uinfo : 2592 &ire->ire_uinfo; 2593 } else { 2594 ire = (ire_t *)ire_mp->b_rptr; 2595 ire_uinfo = 2596 &((ire_t *) 2597 ire_mp->b_rptr)->ire_uinfo; 2598 } 2599 } 2600 } 2601 ASSERT(ire != NULL); 2602 2603 if ((ire->ire_src_addr == INADDR_ANY) || 2604 (ire->ire_type & IRE_BROADCAST)) { 2605 /* 2606 * ire->ire_mp is non null when ire_mp passed in is used 2607 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2608 */ 2609 if (ire->ire_mp == NULL) 2610 ire_refrele(ire); 2611 if (sire != NULL) 2612 ire_refrele(sire); 2613 return (0); 2614 } 2615 2616 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2617 ipaddr_t src_addr; 2618 2619 /* 2620 * ip_bind_connected() has stored the correct source 2621 * address in conn_src. 2622 */ 2623 src_addr = tcp->tcp_connp->conn_src; 2624 tcp->tcp_ipha->ipha_src = src_addr; 2625 /* 2626 * Copy of the src addr. in tcp_t is needed 2627 * for the lookup funcs. 2628 */ 2629 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2630 } 2631 /* 2632 * Set the fragment bit so that IP will tell us if the MTU 2633 * should change. IP tells us the latest setting of 2634 * ip_path_mtu_discovery through ire_frag_flag. 2635 */ 2636 if (ipst->ips_ip_path_mtu_discovery) { 2637 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2638 htons(IPH_DF); 2639 } 2640 /* 2641 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2642 * for IP_NEXTHOP. No cache ire has been found for the 2643 * destination and we are working with the nexthop's 2644 * interface ire. Since we need to forward all packets 2645 * to the nexthop first, we "blindly" set tcp_localnet 2646 * to false, eventhough the destination may also be 2647 * onlink. 2648 */ 2649 if (ire_uinfo == NULL) 2650 tcp->tcp_localnet = 0; 2651 else 2652 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2653 } else { 2654 /* 2655 * For incoming connection ire_mp = NULL 2656 * For outgoing connection ire_mp != NULL 2657 * Technically we should check conn_incoming_ill 2658 * when ire_mp is NULL and conn_outgoing_ill when 2659 * ire_mp is non-NULL. But this is performance 2660 * critical path and for IPV*_BOUND_IF, outgoing 2661 * and incoming ill are always set to the same value. 2662 */ 2663 ill_t *dst_ill = NULL; 2664 ipif_t *dst_ipif = NULL; 2665 2666 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2667 2668 if (connp->conn_outgoing_ill != NULL) { 2669 /* Outgoing or incoming path */ 2670 int err; 2671 2672 dst_ill = conn_get_held_ill(connp, 2673 &connp->conn_outgoing_ill, &err); 2674 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2675 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2676 return (0); 2677 } 2678 match_flags |= MATCH_IRE_ILL; 2679 dst_ipif = dst_ill->ill_ipif; 2680 } 2681 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2682 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2683 2684 if (ire != NULL) { 2685 ire_cacheable = B_TRUE; 2686 ire_uinfo = (ire_mp != NULL) ? 2687 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2688 &ire->ire_uinfo; 2689 } else { 2690 if (ire_mp == NULL) { 2691 ire = ire_ftable_lookup_v6( 2692 &tcp->tcp_connp->conn_remv6, 2693 0, 0, 0, dst_ipif, &sire, zoneid, 2694 0, tsl, match_flags, ipst); 2695 if (ire == NULL) { 2696 if (dst_ill != NULL) 2697 ill_refrele(dst_ill); 2698 return (0); 2699 } 2700 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2701 &ire->ire_uinfo; 2702 } else { 2703 ire = (ire_t *)ire_mp->b_rptr; 2704 ire_uinfo = 2705 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2706 } 2707 } 2708 if (dst_ill != NULL) 2709 ill_refrele(dst_ill); 2710 2711 ASSERT(ire != NULL); 2712 ASSERT(ire_uinfo != NULL); 2713 2714 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2715 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2716 /* 2717 * ire->ire_mp is non null when ire_mp passed in is used 2718 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2719 */ 2720 if (ire->ire_mp == NULL) 2721 ire_refrele(ire); 2722 if (sire != NULL) 2723 ire_refrele(sire); 2724 return (0); 2725 } 2726 2727 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2728 in6_addr_t src_addr; 2729 2730 /* 2731 * ip_bind_connected_v6() has stored the correct source 2732 * address per IPv6 addr. selection policy in 2733 * conn_src_v6. 2734 */ 2735 src_addr = tcp->tcp_connp->conn_srcv6; 2736 2737 tcp->tcp_ip6h->ip6_src = src_addr; 2738 /* 2739 * Copy of the src addr. in tcp_t is needed 2740 * for the lookup funcs. 2741 */ 2742 tcp->tcp_ip_src_v6 = src_addr; 2743 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2744 &connp->conn_srcv6)); 2745 } 2746 tcp->tcp_localnet = 2747 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2748 } 2749 2750 /* 2751 * This allows applications to fail quickly when connections are made 2752 * to dead hosts. Hosts can be labeled dead by adding a reject route 2753 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2754 */ 2755 if ((ire->ire_flags & RTF_REJECT) && 2756 (ire->ire_flags & RTF_PRIVATE)) 2757 goto error; 2758 2759 /* 2760 * Make use of the cached rtt and rtt_sd values to calculate the 2761 * initial RTO. Note that they are already initialized in 2762 * tcp_init_values(). 2763 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2764 * IP_NEXTHOP, but instead are using the interface ire for the 2765 * nexthop, then we do not use the ire_uinfo from that ire to 2766 * do any initializations. 2767 */ 2768 if (ire_uinfo != NULL) { 2769 if (ire_uinfo->iulp_rtt != 0) { 2770 clock_t rto; 2771 2772 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2773 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2774 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2775 tcps->tcps_rexmit_interval_extra + 2776 (tcp->tcp_rtt_sa >> 5); 2777 2778 if (rto > tcps->tcps_rexmit_interval_max) { 2779 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2780 } else if (rto < tcps->tcps_rexmit_interval_min) { 2781 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2782 } else { 2783 tcp->tcp_rto = rto; 2784 } 2785 } 2786 if (ire_uinfo->iulp_ssthresh != 0) 2787 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2788 else 2789 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2790 if (ire_uinfo->iulp_spipe > 0) { 2791 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2792 tcps->tcps_max_buf); 2793 if (tcps->tcps_snd_lowat_fraction != 0) 2794 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2795 tcps->tcps_snd_lowat_fraction; 2796 (void) tcp_maxpsz_set(tcp, B_TRUE); 2797 } 2798 /* 2799 * Note that up till now, acceptor always inherits receive 2800 * window from the listener. But if there is a metrics 2801 * associated with a host, we should use that instead of 2802 * inheriting it from listener. Thus we need to pass this 2803 * info back to the caller. 2804 */ 2805 if (ire_uinfo->iulp_rpipe > 0) { 2806 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2807 tcps->tcps_max_buf); 2808 } 2809 2810 if (ire_uinfo->iulp_rtomax > 0) { 2811 tcp->tcp_second_timer_threshold = 2812 ire_uinfo->iulp_rtomax; 2813 } 2814 2815 /* 2816 * Use the metric option settings, iulp_tstamp_ok and 2817 * iulp_wscale_ok, only for active open. What this means 2818 * is that if the other side uses timestamp or window 2819 * scale option, TCP will also use those options. That 2820 * is for passive open. If the application sets a 2821 * large window, window scale is enabled regardless of 2822 * the value in iulp_wscale_ok. This is the behavior 2823 * since 2.6. So we keep it. 2824 * The only case left in passive open processing is the 2825 * check for SACK. 2826 * For ECN, it should probably be like SACK. But the 2827 * current value is binary, so we treat it like the other 2828 * cases. The metric only controls active open.For passive 2829 * open, the ndd param, tcp_ecn_permitted, controls the 2830 * behavior. 2831 */ 2832 if (!tcp_detached) { 2833 /* 2834 * The if check means that the following can only 2835 * be turned on by the metrics only IRE, but not off. 2836 */ 2837 if (ire_uinfo->iulp_tstamp_ok) 2838 tcp->tcp_snd_ts_ok = B_TRUE; 2839 if (ire_uinfo->iulp_wscale_ok) 2840 tcp->tcp_snd_ws_ok = B_TRUE; 2841 if (ire_uinfo->iulp_sack == 2) 2842 tcp->tcp_snd_sack_ok = B_TRUE; 2843 if (ire_uinfo->iulp_ecn_ok) 2844 tcp->tcp_ecn_ok = B_TRUE; 2845 } else { 2846 /* 2847 * Passive open. 2848 * 2849 * As above, the if check means that SACK can only be 2850 * turned on by the metric only IRE. 2851 */ 2852 if (ire_uinfo->iulp_sack > 0) { 2853 tcp->tcp_snd_sack_ok = B_TRUE; 2854 } 2855 } 2856 } 2857 2858 2859 /* 2860 * XXX: Note that currently, ire_max_frag can be as small as 68 2861 * because of PMTUd. So tcp_mss may go to negative if combined 2862 * length of all those options exceeds 28 bytes. But because 2863 * of the tcp_mss_min check below, we may not have a problem if 2864 * tcp_mss_min is of a reasonable value. The default is 1 so 2865 * the negative problem still exists. And the check defeats PMTUd. 2866 * In fact, if PMTUd finds that the MSS should be smaller than 2867 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2868 * value. 2869 * 2870 * We do not deal with that now. All those problems related to 2871 * PMTUd will be fixed later. 2872 */ 2873 ASSERT(ire->ire_max_frag != 0); 2874 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2875 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2876 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2877 mss = MIN(mss, IPV6_MIN_MTU); 2878 } 2879 } 2880 2881 /* Sanity check for MSS value. */ 2882 if (tcp->tcp_ipversion == IPV4_VERSION) 2883 mss_max = tcps->tcps_mss_max_ipv4; 2884 else 2885 mss_max = tcps->tcps_mss_max_ipv6; 2886 2887 if (tcp->tcp_ipversion == IPV6_VERSION && 2888 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2889 /* 2890 * After receiving an ICMPv6 "packet too big" message with a 2891 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2892 * will insert a 8-byte fragment header in every packet; we 2893 * reduce the MSS by that amount here. 2894 */ 2895 mss -= sizeof (ip6_frag_t); 2896 } 2897 2898 if (tcp->tcp_ipsec_overhead == 0) 2899 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2900 2901 mss -= tcp->tcp_ipsec_overhead; 2902 2903 if (mss < tcps->tcps_mss_min) 2904 mss = tcps->tcps_mss_min; 2905 if (mss > mss_max) 2906 mss = mss_max; 2907 2908 /* Note that this is the maximum MSS, excluding all options. */ 2909 tcp->tcp_mss = mss; 2910 2911 /* 2912 * Initialize the ISS here now that we have the full connection ID. 2913 * The RFC 1948 method of initial sequence number generation requires 2914 * knowledge of the full connection ID before setting the ISS. 2915 */ 2916 2917 tcp_iss_init(tcp); 2918 2919 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2920 tcp->tcp_loopback = B_TRUE; 2921 2922 if (sire != NULL) 2923 IRE_REFRELE(sire); 2924 2925 /* 2926 * If we got an IRE_CACHE and an ILL, go through their properties; 2927 * otherwise, this is deferred until later when we have an IRE_CACHE. 2928 */ 2929 if (tcp->tcp_loopback || 2930 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2931 /* 2932 * For incoming, see if this tcp may be MDT-capable. For 2933 * outgoing, this process has been taken care of through 2934 * tcp_rput_other. 2935 */ 2936 tcp_ire_ill_check(tcp, ire, ill, incoming); 2937 tcp->tcp_ire_ill_check_done = B_TRUE; 2938 } 2939 2940 mutex_enter(&connp->conn_lock); 2941 /* 2942 * Make sure that conn is not marked incipient 2943 * for incoming connections. A blind 2944 * removal of incipient flag is cheaper than 2945 * check and removal. 2946 */ 2947 connp->conn_state_flags &= ~CONN_INCIPIENT; 2948 2949 /* 2950 * Must not cache forwarding table routes 2951 * or recache an IRE after the conn_t has 2952 * had conn_ire_cache cleared and is flagged 2953 * unusable, (see the CONN_CACHE_IRE() macro). 2954 */ 2955 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2956 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2957 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2958 connp->conn_ire_cache = ire; 2959 IRE_UNTRACE_REF(ire); 2960 rw_exit(&ire->ire_bucket->irb_lock); 2961 mutex_exit(&connp->conn_lock); 2962 return (1); 2963 } 2964 rw_exit(&ire->ire_bucket->irb_lock); 2965 } 2966 mutex_exit(&connp->conn_lock); 2967 2968 if (ire->ire_mp == NULL) 2969 ire_refrele(ire); 2970 return (1); 2971 2972 error: 2973 if (ire->ire_mp == NULL) 2974 ire_refrele(ire); 2975 if (sire != NULL) 2976 ire_refrele(sire); 2977 return (0); 2978 } 2979 2980 static void 2981 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 2982 { 2983 int error; 2984 conn_t *connp = tcp->tcp_connp; 2985 struct sockaddr *sa; 2986 mblk_t *mp1; 2987 struct T_bind_req *tbr; 2988 int backlog; 2989 socklen_t len; 2990 sin_t *sin; 2991 sin6_t *sin6; 2992 cred_t *cr; 2993 2994 /* 2995 * All Solaris components should pass a db_credp 2996 * for this TPI message, hence we ASSERT. 2997 * But in case there is some other M_PROTO that looks 2998 * like a TPI message sent by some other kernel 2999 * component, we check and return an error. 3000 */ 3001 cr = msg_getcred(mp, NULL); 3002 ASSERT(cr != NULL); 3003 if (cr == NULL) { 3004 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3005 return; 3006 } 3007 3008 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3009 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3010 if (tcp->tcp_debug) { 3011 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3012 "tcp_tpi_bind: bad req, len %u", 3013 (uint_t)(mp->b_wptr - mp->b_rptr)); 3014 } 3015 tcp_err_ack(tcp, mp, TPROTO, 0); 3016 return; 3017 } 3018 /* Make sure the largest address fits */ 3019 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3020 if (mp1 == NULL) { 3021 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3022 return; 3023 } 3024 mp = mp1; 3025 tbr = (struct T_bind_req *)mp->b_rptr; 3026 3027 backlog = tbr->CONIND_number; 3028 len = tbr->ADDR_length; 3029 3030 switch (len) { 3031 case 0: /* request for a generic port */ 3032 tbr->ADDR_offset = sizeof (struct T_bind_req); 3033 if (tcp->tcp_family == AF_INET) { 3034 tbr->ADDR_length = sizeof (sin_t); 3035 sin = (sin_t *)&tbr[1]; 3036 *sin = sin_null; 3037 sin->sin_family = AF_INET; 3038 sa = (struct sockaddr *)sin; 3039 len = sizeof (sin_t); 3040 mp->b_wptr = (uchar_t *)&sin[1]; 3041 } else { 3042 ASSERT(tcp->tcp_family == AF_INET6); 3043 tbr->ADDR_length = sizeof (sin6_t); 3044 sin6 = (sin6_t *)&tbr[1]; 3045 *sin6 = sin6_null; 3046 sin6->sin6_family = AF_INET6; 3047 sa = (struct sockaddr *)sin6; 3048 len = sizeof (sin6_t); 3049 mp->b_wptr = (uchar_t *)&sin6[1]; 3050 } 3051 break; 3052 3053 case sizeof (sin_t): /* Complete IPv4 address */ 3054 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3055 sizeof (sin_t)); 3056 break; 3057 3058 case sizeof (sin6_t): /* Complete IPv6 address */ 3059 sa = (struct sockaddr *)mi_offset_param(mp, 3060 tbr->ADDR_offset, sizeof (sin6_t)); 3061 break; 3062 3063 default: 3064 if (tcp->tcp_debug) { 3065 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3066 "tcp_tpi_bind: bad address length, %d", 3067 tbr->ADDR_length); 3068 } 3069 tcp_err_ack(tcp, mp, TBADADDR, 0); 3070 return; 3071 } 3072 3073 if (backlog > 0) { 3074 error = tcp_do_listen(connp, sa, len, backlog, DB_CRED(mp), 3075 tbr->PRIM_type != O_T_BIND_REQ); 3076 } else { 3077 error = tcp_do_bind(connp, sa, len, DB_CRED(mp), 3078 tbr->PRIM_type != O_T_BIND_REQ); 3079 } 3080 done: 3081 if (error > 0) { 3082 tcp_err_ack(tcp, mp, TSYSERR, error); 3083 } else if (error < 0) { 3084 tcp_err_ack(tcp, mp, -error, 0); 3085 } else { 3086 /* 3087 * Update port information as sockfs/tpi needs it for checking 3088 */ 3089 if (tcp->tcp_family == AF_INET) { 3090 sin = (sin_t *)sa; 3091 sin->sin_port = tcp->tcp_lport; 3092 } else { 3093 sin6 = (sin6_t *)sa; 3094 sin6->sin6_port = tcp->tcp_lport; 3095 } 3096 mp->b_datap->db_type = M_PCPROTO; 3097 tbr->PRIM_type = T_BIND_ACK; 3098 putnext(tcp->tcp_rq, mp); 3099 } 3100 } 3101 3102 /* 3103 * If the "bind_to_req_port_only" parameter is set, if the requested port 3104 * number is available, return it, If not return 0 3105 * 3106 * If "bind_to_req_port_only" parameter is not set and 3107 * If the requested port number is available, return it. If not, return 3108 * the first anonymous port we happen across. If no anonymous ports are 3109 * available, return 0. addr is the requested local address, if any. 3110 * 3111 * In either case, when succeeding update the tcp_t to record the port number 3112 * and insert it in the bind hash table. 3113 * 3114 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3115 * without setting SO_REUSEADDR. This is needed so that they 3116 * can be viewed as two independent transport protocols. 3117 */ 3118 static in_port_t 3119 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3120 int reuseaddr, boolean_t quick_connect, 3121 boolean_t bind_to_req_port_only, boolean_t user_specified) 3122 { 3123 /* number of times we have run around the loop */ 3124 int count = 0; 3125 /* maximum number of times to run around the loop */ 3126 int loopmax; 3127 conn_t *connp = tcp->tcp_connp; 3128 zoneid_t zoneid = connp->conn_zoneid; 3129 tcp_stack_t *tcps = tcp->tcp_tcps; 3130 3131 /* 3132 * Lookup for free addresses is done in a loop and "loopmax" 3133 * influences how long we spin in the loop 3134 */ 3135 if (bind_to_req_port_only) { 3136 /* 3137 * If the requested port is busy, don't bother to look 3138 * for a new one. Setting loop maximum count to 1 has 3139 * that effect. 3140 */ 3141 loopmax = 1; 3142 } else { 3143 /* 3144 * If the requested port is busy, look for a free one 3145 * in the anonymous port range. 3146 * Set loopmax appropriately so that one does not look 3147 * forever in the case all of the anonymous ports are in use. 3148 */ 3149 if (tcp->tcp_anon_priv_bind) { 3150 /* 3151 * loopmax = 3152 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3153 */ 3154 loopmax = IPPORT_RESERVED - 3155 tcps->tcps_min_anonpriv_port; 3156 } else { 3157 loopmax = (tcps->tcps_largest_anon_port - 3158 tcps->tcps_smallest_anon_port + 1); 3159 } 3160 } 3161 do { 3162 uint16_t lport; 3163 tf_t *tbf; 3164 tcp_t *ltcp; 3165 conn_t *lconnp; 3166 3167 lport = htons(port); 3168 3169 /* 3170 * Ensure that the tcp_t is not currently in the bind hash. 3171 * Hold the lock on the hash bucket to ensure that 3172 * the duplicate check plus the insertion is an atomic 3173 * operation. 3174 * 3175 * This function does an inline lookup on the bind hash list 3176 * Make sure that we access only members of tcp_t 3177 * and that we don't look at tcp_tcp, since we are not 3178 * doing a CONN_INC_REF. 3179 */ 3180 tcp_bind_hash_remove(tcp); 3181 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3182 mutex_enter(&tbf->tf_lock); 3183 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3184 ltcp = ltcp->tcp_bind_hash) { 3185 if (lport == ltcp->tcp_lport) 3186 break; 3187 } 3188 3189 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3190 boolean_t not_socket; 3191 boolean_t exclbind; 3192 3193 lconnp = ltcp->tcp_connp; 3194 3195 /* 3196 * On a labeled system, we must treat bindings to ports 3197 * on shared IP addresses by sockets with MAC exemption 3198 * privilege as being in all zones, as there's 3199 * otherwise no way to identify the right receiver. 3200 */ 3201 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3202 IPCL_ZONE_MATCH(connp, 3203 ltcp->tcp_connp->conn_zoneid)) && 3204 !lconnp->conn_mac_exempt && 3205 !connp->conn_mac_exempt) 3206 continue; 3207 3208 /* 3209 * If TCP_EXCLBIND is set for either the bound or 3210 * binding endpoint, the semantics of bind 3211 * is changed according to the following. 3212 * 3213 * spec = specified address (v4 or v6) 3214 * unspec = unspecified address (v4 or v6) 3215 * A = specified addresses are different for endpoints 3216 * 3217 * bound bind to allowed 3218 * ------------------------------------- 3219 * unspec unspec no 3220 * unspec spec no 3221 * spec unspec no 3222 * spec spec yes if A 3223 * 3224 * For labeled systems, SO_MAC_EXEMPT behaves the same 3225 * as TCP_EXCLBIND, except that zoneid is ignored. 3226 * 3227 * Note: 3228 * 3229 * 1. Because of TLI semantics, an endpoint can go 3230 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3231 * TCPS_BOUND, depending on whether it is originally 3232 * a listener or not. That is why we need to check 3233 * for states greater than or equal to TCPS_BOUND 3234 * here. 3235 * 3236 * 2. Ideally, we should only check for state equals 3237 * to TCPS_LISTEN. And the following check should be 3238 * added. 3239 * 3240 * if (ltcp->tcp_state == TCPS_LISTEN || 3241 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3242 * ... 3243 * } 3244 * 3245 * The semantics will be changed to this. If the 3246 * endpoint on the list is in state not equal to 3247 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3248 * set, let the bind succeed. 3249 * 3250 * Because of (1), we cannot do that for TLI 3251 * endpoints. But we can do that for socket endpoints. 3252 * If in future, we can change this going back 3253 * semantics, we can use the above check for TLI also. 3254 */ 3255 not_socket = !(TCP_IS_SOCKET(ltcp) && 3256 TCP_IS_SOCKET(tcp)); 3257 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3258 3259 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3260 (exclbind && (not_socket || 3261 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3262 if (V6_OR_V4_INADDR_ANY( 3263 ltcp->tcp_bound_source_v6) || 3264 V6_OR_V4_INADDR_ANY(*laddr) || 3265 IN6_ARE_ADDR_EQUAL(laddr, 3266 <cp->tcp_bound_source_v6)) { 3267 break; 3268 } 3269 continue; 3270 } 3271 3272 /* 3273 * Check ipversion to allow IPv4 and IPv6 sockets to 3274 * have disjoint port number spaces, if *_EXCLBIND 3275 * is not set and only if the application binds to a 3276 * specific port. We use the same autoassigned port 3277 * number space for IPv4 and IPv6 sockets. 3278 */ 3279 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3280 bind_to_req_port_only) 3281 continue; 3282 3283 /* 3284 * Ideally, we should make sure that the source 3285 * address, remote address, and remote port in the 3286 * four tuple for this tcp-connection is unique. 3287 * However, trying to find out the local source 3288 * address would require too much code duplication 3289 * with IP, since IP needs needs to have that code 3290 * to support userland TCP implementations. 3291 */ 3292 if (quick_connect && 3293 (ltcp->tcp_state > TCPS_LISTEN) && 3294 ((tcp->tcp_fport != ltcp->tcp_fport) || 3295 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3296 <cp->tcp_remote_v6))) 3297 continue; 3298 3299 if (!reuseaddr) { 3300 /* 3301 * No socket option SO_REUSEADDR. 3302 * If existing port is bound to 3303 * a non-wildcard IP address 3304 * and the requesting stream is 3305 * bound to a distinct 3306 * different IP addresses 3307 * (non-wildcard, also), keep 3308 * going. 3309 */ 3310 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3311 !V6_OR_V4_INADDR_ANY( 3312 ltcp->tcp_bound_source_v6) && 3313 !IN6_ARE_ADDR_EQUAL(laddr, 3314 <cp->tcp_bound_source_v6)) 3315 continue; 3316 if (ltcp->tcp_state >= TCPS_BOUND) { 3317 /* 3318 * This port is being used and 3319 * its state is >= TCPS_BOUND, 3320 * so we can't bind to it. 3321 */ 3322 break; 3323 } 3324 } else { 3325 /* 3326 * socket option SO_REUSEADDR is set on the 3327 * binding tcp_t. 3328 * 3329 * If two streams are bound to 3330 * same IP address or both addr 3331 * and bound source are wildcards 3332 * (INADDR_ANY), we want to stop 3333 * searching. 3334 * We have found a match of IP source 3335 * address and source port, which is 3336 * refused regardless of the 3337 * SO_REUSEADDR setting, so we break. 3338 */ 3339 if (IN6_ARE_ADDR_EQUAL(laddr, 3340 <cp->tcp_bound_source_v6) && 3341 (ltcp->tcp_state == TCPS_LISTEN || 3342 ltcp->tcp_state == TCPS_BOUND)) 3343 break; 3344 } 3345 } 3346 if (ltcp != NULL) { 3347 /* The port number is busy */ 3348 mutex_exit(&tbf->tf_lock); 3349 } else { 3350 /* 3351 * This port is ours. Insert in fanout and mark as 3352 * bound to prevent others from getting the port 3353 * number. 3354 */ 3355 tcp->tcp_state = TCPS_BOUND; 3356 tcp->tcp_lport = htons(port); 3357 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3358 3359 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3360 tcp->tcp_lport)] == tbf); 3361 tcp_bind_hash_insert(tbf, tcp, 1); 3362 3363 mutex_exit(&tbf->tf_lock); 3364 3365 /* 3366 * We don't want tcp_next_port_to_try to "inherit" 3367 * a port number supplied by the user in a bind. 3368 */ 3369 if (user_specified) 3370 return (port); 3371 3372 /* 3373 * This is the only place where tcp_next_port_to_try 3374 * is updated. After the update, it may or may not 3375 * be in the valid range. 3376 */ 3377 if (!tcp->tcp_anon_priv_bind) 3378 tcps->tcps_next_port_to_try = port + 1; 3379 return (port); 3380 } 3381 3382 if (tcp->tcp_anon_priv_bind) { 3383 port = tcp_get_next_priv_port(tcp); 3384 } else { 3385 if (count == 0 && user_specified) { 3386 /* 3387 * We may have to return an anonymous port. So 3388 * get one to start with. 3389 */ 3390 port = 3391 tcp_update_next_port( 3392 tcps->tcps_next_port_to_try, 3393 tcp, B_TRUE); 3394 user_specified = B_FALSE; 3395 } else { 3396 port = tcp_update_next_port(port + 1, tcp, 3397 B_FALSE); 3398 } 3399 } 3400 if (port == 0) 3401 break; 3402 3403 /* 3404 * Don't let this loop run forever in the case where 3405 * all of the anonymous ports are in use. 3406 */ 3407 } while (++count < loopmax); 3408 return (0); 3409 } 3410 3411 /* 3412 * tcp_clean_death / tcp_close_detached must not be called more than once 3413 * on a tcp. Thus every function that potentially calls tcp_clean_death 3414 * must check for the tcp state before calling tcp_clean_death. 3415 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3416 * tcp_timer_handler, all check for the tcp state. 3417 */ 3418 /* ARGSUSED */ 3419 void 3420 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3421 { 3422 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3423 3424 freemsg(mp); 3425 if (tcp->tcp_state > TCPS_BOUND) 3426 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3427 ETIMEDOUT, 5); 3428 } 3429 3430 /* 3431 * We are dying for some reason. Try to do it gracefully. (May be called 3432 * as writer.) 3433 * 3434 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3435 * done by a service procedure). 3436 * TBD - Should the return value distinguish between the tcp_t being 3437 * freed and it being reinitialized? 3438 */ 3439 static int 3440 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3441 { 3442 mblk_t *mp; 3443 queue_t *q; 3444 conn_t *connp = tcp->tcp_connp; 3445 tcp_stack_t *tcps = tcp->tcp_tcps; 3446 sodirect_t *sodp; 3447 3448 TCP_CLD_STAT(tag); 3449 3450 #if TCP_TAG_CLEAN_DEATH 3451 tcp->tcp_cleandeathtag = tag; 3452 #endif 3453 3454 if (tcp->tcp_fused) 3455 tcp_unfuse(tcp); 3456 3457 if (tcp->tcp_linger_tid != 0 && 3458 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3459 tcp_stop_lingering(tcp); 3460 } 3461 3462 ASSERT(tcp != NULL); 3463 ASSERT((tcp->tcp_family == AF_INET && 3464 tcp->tcp_ipversion == IPV4_VERSION) || 3465 (tcp->tcp_family == AF_INET6 && 3466 (tcp->tcp_ipversion == IPV4_VERSION || 3467 tcp->tcp_ipversion == IPV6_VERSION))); 3468 3469 if (TCP_IS_DETACHED(tcp)) { 3470 if (tcp->tcp_hard_binding) { 3471 /* 3472 * Its an eager that we are dealing with. We close the 3473 * eager but in case a conn_ind has already gone to the 3474 * listener, let tcp_accept_finish() send a discon_ind 3475 * to the listener and drop the last reference. If the 3476 * listener doesn't even know about the eager i.e. the 3477 * conn_ind hasn't gone up, blow away the eager and drop 3478 * the last reference as well. If the conn_ind has gone 3479 * up, state should be BOUND. tcp_accept_finish 3480 * will figure out that the connection has received a 3481 * RST and will send a DISCON_IND to the application. 3482 */ 3483 tcp_closei_local(tcp); 3484 if (!tcp->tcp_tconnind_started) { 3485 CONN_DEC_REF(connp); 3486 } else { 3487 tcp->tcp_state = TCPS_BOUND; 3488 } 3489 } else { 3490 tcp_close_detached(tcp); 3491 } 3492 return (0); 3493 } 3494 3495 TCP_STAT(tcps, tcp_clean_death_nondetached); 3496 3497 /* If sodirect, not anymore */ 3498 SOD_PTR_ENTER(tcp, sodp); 3499 if (sodp != NULL) { 3500 tcp->tcp_sodirect = NULL; 3501 mutex_exit(sodp->sod_lockp); 3502 } 3503 3504 q = tcp->tcp_rq; 3505 3506 /* Trash all inbound data */ 3507 if (!IPCL_IS_NONSTR(connp)) { 3508 ASSERT(q != NULL); 3509 flushq(q, FLUSHALL); 3510 } 3511 3512 /* 3513 * If we are at least part way open and there is error 3514 * (err==0 implies no error) 3515 * notify our client by a T_DISCON_IND. 3516 */ 3517 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3518 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3519 !TCP_IS_SOCKET(tcp)) { 3520 /* 3521 * Send M_FLUSH according to TPI. Because sockets will 3522 * (and must) ignore FLUSHR we do that only for TPI 3523 * endpoints and sockets in STREAMS mode. 3524 */ 3525 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3526 } 3527 if (tcp->tcp_debug) { 3528 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3529 "tcp_clean_death: discon err %d", err); 3530 } 3531 if (IPCL_IS_NONSTR(connp)) { 3532 /* Direct socket, use upcall */ 3533 (*connp->conn_upcalls->su_disconnected)( 3534 connp->conn_upper_handle, tcp->tcp_connid, err); 3535 } else { 3536 mp = mi_tpi_discon_ind(NULL, err, 0); 3537 if (mp != NULL) { 3538 putnext(q, mp); 3539 } else { 3540 if (tcp->tcp_debug) { 3541 (void) strlog(TCP_MOD_ID, 0, 1, 3542 SL_ERROR|SL_TRACE, 3543 "tcp_clean_death, sending M_ERROR"); 3544 } 3545 (void) putnextctl1(q, M_ERROR, EPROTO); 3546 } 3547 } 3548 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3549 /* SYN_SENT or SYN_RCVD */ 3550 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3551 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3552 /* ESTABLISHED or CLOSE_WAIT */ 3553 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3554 } 3555 } 3556 3557 tcp_reinit(tcp); 3558 if (IPCL_IS_NONSTR(connp)) 3559 (void) tcp_do_unbind(connp); 3560 3561 return (-1); 3562 } 3563 3564 /* 3565 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3566 * to expire, stop the wait and finish the close. 3567 */ 3568 static void 3569 tcp_stop_lingering(tcp_t *tcp) 3570 { 3571 clock_t delta = 0; 3572 tcp_stack_t *tcps = tcp->tcp_tcps; 3573 3574 tcp->tcp_linger_tid = 0; 3575 if (tcp->tcp_state > TCPS_LISTEN) { 3576 tcp_acceptor_hash_remove(tcp); 3577 mutex_enter(&tcp->tcp_non_sq_lock); 3578 if (tcp->tcp_flow_stopped) { 3579 tcp_clrqfull(tcp); 3580 } 3581 mutex_exit(&tcp->tcp_non_sq_lock); 3582 3583 if (tcp->tcp_timer_tid != 0) { 3584 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3585 tcp->tcp_timer_tid = 0; 3586 } 3587 /* 3588 * Need to cancel those timers which will not be used when 3589 * TCP is detached. This has to be done before the tcp_wq 3590 * is set to the global queue. 3591 */ 3592 tcp_timers_stop(tcp); 3593 3594 tcp->tcp_detached = B_TRUE; 3595 ASSERT(tcps->tcps_g_q != NULL); 3596 tcp->tcp_rq = tcps->tcps_g_q; 3597 tcp->tcp_wq = WR(tcps->tcps_g_q); 3598 3599 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3600 tcp_time_wait_append(tcp); 3601 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3602 goto finish; 3603 } 3604 3605 /* 3606 * If delta is zero the timer event wasn't executed and was 3607 * successfully canceled. In this case we need to restart it 3608 * with the minimal delta possible. 3609 */ 3610 if (delta >= 0) { 3611 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3612 delta ? delta : 1); 3613 } 3614 } else { 3615 tcp_closei_local(tcp); 3616 CONN_DEC_REF(tcp->tcp_connp); 3617 } 3618 finish: 3619 /* Signal closing thread that it can complete close */ 3620 mutex_enter(&tcp->tcp_closelock); 3621 tcp->tcp_detached = B_TRUE; 3622 ASSERT(tcps->tcps_g_q != NULL); 3623 3624 tcp->tcp_rq = tcps->tcps_g_q; 3625 tcp->tcp_wq = WR(tcps->tcps_g_q); 3626 3627 tcp->tcp_closed = 1; 3628 cv_signal(&tcp->tcp_closecv); 3629 mutex_exit(&tcp->tcp_closelock); 3630 } 3631 3632 /* 3633 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3634 * expires. 3635 */ 3636 static void 3637 tcp_close_linger_timeout(void *arg) 3638 { 3639 conn_t *connp = (conn_t *)arg; 3640 tcp_t *tcp = connp->conn_tcp; 3641 3642 tcp->tcp_client_errno = ETIMEDOUT; 3643 tcp_stop_lingering(tcp); 3644 } 3645 3646 static void 3647 tcp_close_common(conn_t *connp, int flags) 3648 { 3649 tcp_t *tcp = connp->conn_tcp; 3650 mblk_t *mp = &tcp->tcp_closemp; 3651 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3652 mblk_t *bp; 3653 3654 ASSERT(connp->conn_ref >= 2); 3655 3656 /* 3657 * Mark the conn as closing. ill_pending_mp_add will not 3658 * add any mp to the pending mp list, after this conn has 3659 * started closing. Same for sq_pending_mp_add 3660 */ 3661 mutex_enter(&connp->conn_lock); 3662 connp->conn_state_flags |= CONN_CLOSING; 3663 if (connp->conn_oper_pending_ill != NULL) 3664 conn_ioctl_cleanup_reqd = B_TRUE; 3665 CONN_INC_REF_LOCKED(connp); 3666 mutex_exit(&connp->conn_lock); 3667 tcp->tcp_closeflags = (uint8_t)flags; 3668 ASSERT(connp->conn_ref >= 3); 3669 3670 /* 3671 * tcp_closemp_used is used below without any protection of a lock 3672 * as we don't expect any one else to use it concurrently at this 3673 * point otherwise it would be a major defect. 3674 */ 3675 3676 if (mp->b_prev == NULL) 3677 tcp->tcp_closemp_used = B_TRUE; 3678 else 3679 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3680 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3681 3682 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3683 3684 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3685 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3686 3687 mutex_enter(&tcp->tcp_closelock); 3688 while (!tcp->tcp_closed) { 3689 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3690 /* 3691 * The cv_wait_sig() was interrupted. We now do the 3692 * following: 3693 * 3694 * 1) If the endpoint was lingering, we allow this 3695 * to be interrupted by cancelling the linger timeout 3696 * and closing normally. 3697 * 3698 * 2) Revert to calling cv_wait() 3699 * 3700 * We revert to using cv_wait() to avoid an 3701 * infinite loop which can occur if the calling 3702 * thread is higher priority than the squeue worker 3703 * thread and is bound to the same cpu. 3704 */ 3705 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3706 mutex_exit(&tcp->tcp_closelock); 3707 /* Entering squeue, bump ref count. */ 3708 CONN_INC_REF(connp); 3709 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3710 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3711 tcp_linger_interrupted, connp, 3712 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3713 mutex_enter(&tcp->tcp_closelock); 3714 } 3715 break; 3716 } 3717 } 3718 while (!tcp->tcp_closed) 3719 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3720 mutex_exit(&tcp->tcp_closelock); 3721 3722 /* 3723 * In the case of listener streams that have eagers in the q or q0 3724 * we wait for the eagers to drop their reference to us. tcp_rq and 3725 * tcp_wq of the eagers point to our queues. By waiting for the 3726 * refcnt to drop to 1, we are sure that the eagers have cleaned 3727 * up their queue pointers and also dropped their references to us. 3728 */ 3729 if (tcp->tcp_wait_for_eagers) { 3730 mutex_enter(&connp->conn_lock); 3731 while (connp->conn_ref != 1) { 3732 cv_wait(&connp->conn_cv, &connp->conn_lock); 3733 } 3734 mutex_exit(&connp->conn_lock); 3735 } 3736 /* 3737 * ioctl cleanup. The mp is queued in the 3738 * ill_pending_mp or in the sq_pending_mp. 3739 */ 3740 if (conn_ioctl_cleanup_reqd) 3741 conn_ioctl_cleanup(connp); 3742 3743 tcp->tcp_cpid = -1; 3744 } 3745 3746 static int 3747 tcp_tpi_close(queue_t *q, int flags) 3748 { 3749 conn_t *connp; 3750 3751 ASSERT(WR(q)->q_next == NULL); 3752 3753 if (flags & SO_FALLBACK) { 3754 /* 3755 * stream is being closed while in fallback 3756 * simply free the resources that were allocated 3757 */ 3758 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3759 qprocsoff(q); 3760 goto done; 3761 } 3762 3763 connp = Q_TO_CONN(q); 3764 /* 3765 * We are being closed as /dev/tcp or /dev/tcp6. 3766 */ 3767 tcp_close_common(connp, flags); 3768 3769 qprocsoff(q); 3770 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3771 3772 /* 3773 * Drop IP's reference on the conn. This is the last reference 3774 * on the connp if the state was less than established. If the 3775 * connection has gone into timewait state, then we will have 3776 * one ref for the TCP and one more ref (total of two) for the 3777 * classifier connected hash list (a timewait connections stays 3778 * in connected hash till closed). 3779 * 3780 * We can't assert the references because there might be other 3781 * transient reference places because of some walkers or queued 3782 * packets in squeue for the timewait state. 3783 */ 3784 CONN_DEC_REF(connp); 3785 done: 3786 q->q_ptr = WR(q)->q_ptr = NULL; 3787 return (0); 3788 } 3789 3790 static int 3791 tcp_tpi_close_accept(queue_t *q) 3792 { 3793 vmem_t *minor_arena; 3794 dev_t conn_dev; 3795 3796 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3797 3798 /* 3799 * We had opened an acceptor STREAM for sockfs which is 3800 * now being closed due to some error. 3801 */ 3802 qprocsoff(q); 3803 3804 minor_arena = (vmem_t *)WR(q)->q_ptr; 3805 conn_dev = (dev_t)RD(q)->q_ptr; 3806 ASSERT(minor_arena != NULL); 3807 ASSERT(conn_dev != 0); 3808 inet_minor_free(minor_arena, conn_dev); 3809 q->q_ptr = WR(q)->q_ptr = NULL; 3810 return (0); 3811 } 3812 3813 /* 3814 * Called by tcp_close() routine via squeue when lingering is 3815 * interrupted by a signal. 3816 */ 3817 3818 /* ARGSUSED */ 3819 static void 3820 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3821 { 3822 conn_t *connp = (conn_t *)arg; 3823 tcp_t *tcp = connp->conn_tcp; 3824 3825 freeb(mp); 3826 if (tcp->tcp_linger_tid != 0 && 3827 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3828 tcp_stop_lingering(tcp); 3829 tcp->tcp_client_errno = EINTR; 3830 } 3831 } 3832 3833 /* 3834 * Called by streams close routine via squeues when our client blows off her 3835 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3836 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3837 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3838 * acked. 3839 * 3840 * NOTE: tcp_close potentially returns error when lingering. 3841 * However, the stream head currently does not pass these errors 3842 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3843 * errors to the application (from tsleep()) and not errors 3844 * like ECONNRESET caused by receiving a reset packet. 3845 */ 3846 3847 /* ARGSUSED */ 3848 static void 3849 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3850 { 3851 char *msg; 3852 conn_t *connp = (conn_t *)arg; 3853 tcp_t *tcp = connp->conn_tcp; 3854 clock_t delta = 0; 3855 tcp_stack_t *tcps = tcp->tcp_tcps; 3856 3857 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3858 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3859 3860 mutex_enter(&tcp->tcp_eager_lock); 3861 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3862 /* Cleanup for listener */ 3863 tcp_eager_cleanup(tcp, 0); 3864 tcp->tcp_wait_for_eagers = 1; 3865 } 3866 mutex_exit(&tcp->tcp_eager_lock); 3867 3868 connp->conn_mdt_ok = B_FALSE; 3869 tcp->tcp_mdt = B_FALSE; 3870 3871 connp->conn_lso_ok = B_FALSE; 3872 tcp->tcp_lso = B_FALSE; 3873 3874 msg = NULL; 3875 switch (tcp->tcp_state) { 3876 case TCPS_CLOSED: 3877 case TCPS_IDLE: 3878 case TCPS_BOUND: 3879 case TCPS_LISTEN: 3880 break; 3881 case TCPS_SYN_SENT: 3882 msg = "tcp_close, during connect"; 3883 break; 3884 case TCPS_SYN_RCVD: 3885 /* 3886 * Close during the connect 3-way handshake 3887 * but here there may or may not be pending data 3888 * already on queue. Process almost same as in 3889 * the ESTABLISHED state. 3890 */ 3891 /* FALLTHRU */ 3892 default: 3893 if (tcp->tcp_sodirect != NULL) { 3894 /* Ok, no more sodirect */ 3895 tcp->tcp_sodirect = NULL; 3896 } 3897 3898 if (tcp->tcp_fused) 3899 tcp_unfuse(tcp); 3900 3901 /* 3902 * If SO_LINGER has set a zero linger time, abort the 3903 * connection with a reset. 3904 */ 3905 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3906 msg = "tcp_close, zero lingertime"; 3907 break; 3908 } 3909 3910 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3911 /* 3912 * Abort connection if there is unread data queued. 3913 */ 3914 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3915 msg = "tcp_close, unread data"; 3916 break; 3917 } 3918 /* 3919 * tcp_hard_bound is now cleared thus all packets go through 3920 * tcp_lookup. This fact is used by tcp_detach below. 3921 * 3922 * We have done a qwait() above which could have possibly 3923 * drained more messages in turn causing transition to a 3924 * different state. Check whether we have to do the rest 3925 * of the processing or not. 3926 */ 3927 if (tcp->tcp_state <= TCPS_LISTEN) 3928 break; 3929 3930 /* 3931 * Transmit the FIN before detaching the tcp_t. 3932 * After tcp_detach returns this queue/perimeter 3933 * no longer owns the tcp_t thus others can modify it. 3934 */ 3935 (void) tcp_xmit_end(tcp); 3936 3937 /* 3938 * If lingering on close then wait until the fin is acked, 3939 * the SO_LINGER time passes, or a reset is sent/received. 3940 */ 3941 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3942 !(tcp->tcp_fin_acked) && 3943 tcp->tcp_state >= TCPS_ESTABLISHED) { 3944 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3945 tcp->tcp_client_errno = EWOULDBLOCK; 3946 } else if (tcp->tcp_client_errno == 0) { 3947 3948 ASSERT(tcp->tcp_linger_tid == 0); 3949 3950 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3951 tcp_close_linger_timeout, 3952 tcp->tcp_lingertime * hz); 3953 3954 /* tcp_close_linger_timeout will finish close */ 3955 if (tcp->tcp_linger_tid == 0) 3956 tcp->tcp_client_errno = ENOSR; 3957 else 3958 return; 3959 } 3960 3961 /* 3962 * Check if we need to detach or just close 3963 * the instance. 3964 */ 3965 if (tcp->tcp_state <= TCPS_LISTEN) 3966 break; 3967 } 3968 3969 /* 3970 * Make sure that no other thread will access the tcp_rq of 3971 * this instance (through lookups etc.) as tcp_rq will go 3972 * away shortly. 3973 */ 3974 tcp_acceptor_hash_remove(tcp); 3975 3976 mutex_enter(&tcp->tcp_non_sq_lock); 3977 if (tcp->tcp_flow_stopped) { 3978 tcp_clrqfull(tcp); 3979 } 3980 mutex_exit(&tcp->tcp_non_sq_lock); 3981 3982 if (tcp->tcp_timer_tid != 0) { 3983 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3984 tcp->tcp_timer_tid = 0; 3985 } 3986 /* 3987 * Need to cancel those timers which will not be used when 3988 * TCP is detached. This has to be done before the tcp_wq 3989 * is set to the global queue. 3990 */ 3991 tcp_timers_stop(tcp); 3992 3993 tcp->tcp_detached = B_TRUE; 3994 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3995 tcp_time_wait_append(tcp); 3996 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3997 ASSERT(connp->conn_ref >= 3); 3998 goto finish; 3999 } 4000 4001 /* 4002 * If delta is zero the timer event wasn't executed and was 4003 * successfully canceled. In this case we need to restart it 4004 * with the minimal delta possible. 4005 */ 4006 if (delta >= 0) 4007 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4008 delta ? delta : 1); 4009 4010 ASSERT(connp->conn_ref >= 3); 4011 goto finish; 4012 } 4013 4014 /* Detach did not complete. Still need to remove q from stream. */ 4015 if (msg) { 4016 if (tcp->tcp_state == TCPS_ESTABLISHED || 4017 tcp->tcp_state == TCPS_CLOSE_WAIT) 4018 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4019 if (tcp->tcp_state == TCPS_SYN_SENT || 4020 tcp->tcp_state == TCPS_SYN_RCVD) 4021 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4022 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4023 } 4024 4025 tcp_closei_local(tcp); 4026 CONN_DEC_REF(connp); 4027 ASSERT(connp->conn_ref >= 2); 4028 4029 finish: 4030 /* 4031 * Although packets are always processed on the correct 4032 * tcp's perimeter and access is serialized via squeue's, 4033 * IP still needs a queue when sending packets in time_wait 4034 * state so use WR(tcps_g_q) till ip_output() can be 4035 * changed to deal with just connp. For read side, we 4036 * could have set tcp_rq to NULL but there are some cases 4037 * in tcp_rput_data() from early days of this code which 4038 * do a putnext without checking if tcp is closed. Those 4039 * need to be identified before both tcp_rq and tcp_wq 4040 * can be set to NULL and tcps_g_q can disappear forever. 4041 */ 4042 mutex_enter(&tcp->tcp_closelock); 4043 /* 4044 * Don't change the queues in the case of a listener that has 4045 * eagers in its q or q0. It could surprise the eagers. 4046 * Instead wait for the eagers outside the squeue. 4047 */ 4048 if (!tcp->tcp_wait_for_eagers) { 4049 tcp->tcp_detached = B_TRUE; 4050 /* 4051 * When default queue is closing we set tcps_g_q to NULL 4052 * after the close is done. 4053 */ 4054 ASSERT(tcps->tcps_g_q != NULL); 4055 tcp->tcp_rq = tcps->tcps_g_q; 4056 tcp->tcp_wq = WR(tcps->tcps_g_q); 4057 } 4058 4059 /* Signal tcp_close() to finish closing. */ 4060 tcp->tcp_closed = 1; 4061 cv_signal(&tcp->tcp_closecv); 4062 mutex_exit(&tcp->tcp_closelock); 4063 } 4064 4065 /* 4066 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4067 * Some stream heads get upset if they see these later on as anything but NULL. 4068 */ 4069 static void 4070 tcp_close_mpp(mblk_t **mpp) 4071 { 4072 mblk_t *mp; 4073 4074 if ((mp = *mpp) != NULL) { 4075 do { 4076 mp->b_next = NULL; 4077 mp->b_prev = NULL; 4078 } while ((mp = mp->b_cont) != NULL); 4079 4080 mp = *mpp; 4081 *mpp = NULL; 4082 freemsg(mp); 4083 } 4084 } 4085 4086 /* Do detached close. */ 4087 static void 4088 tcp_close_detached(tcp_t *tcp) 4089 { 4090 if (tcp->tcp_fused) 4091 tcp_unfuse(tcp); 4092 4093 /* 4094 * Clustering code serializes TCP disconnect callbacks and 4095 * cluster tcp list walks by blocking a TCP disconnect callback 4096 * if a cluster tcp list walk is in progress. This ensures 4097 * accurate accounting of TCPs in the cluster code even though 4098 * the TCP list walk itself is not atomic. 4099 */ 4100 tcp_closei_local(tcp); 4101 CONN_DEC_REF(tcp->tcp_connp); 4102 } 4103 4104 /* 4105 * Stop all TCP timers, and free the timer mblks if requested. 4106 */ 4107 void 4108 tcp_timers_stop(tcp_t *tcp) 4109 { 4110 if (tcp->tcp_timer_tid != 0) { 4111 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4112 tcp->tcp_timer_tid = 0; 4113 } 4114 if (tcp->tcp_ka_tid != 0) { 4115 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4116 tcp->tcp_ka_tid = 0; 4117 } 4118 if (tcp->tcp_ack_tid != 0) { 4119 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4120 tcp->tcp_ack_tid = 0; 4121 } 4122 if (tcp->tcp_push_tid != 0) { 4123 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4124 tcp->tcp_push_tid = 0; 4125 } 4126 } 4127 4128 /* 4129 * The tcp_t is going away. Remove it from all lists and set it 4130 * to TCPS_CLOSED. The freeing up of memory is deferred until 4131 * tcp_inactive. This is needed since a thread in tcp_rput might have 4132 * done a CONN_INC_REF on this structure before it was removed from the 4133 * hashes. 4134 */ 4135 static void 4136 tcp_closei_local(tcp_t *tcp) 4137 { 4138 ire_t *ire; 4139 conn_t *connp = tcp->tcp_connp; 4140 tcp_stack_t *tcps = tcp->tcp_tcps; 4141 4142 if (!TCP_IS_SOCKET(tcp)) 4143 tcp_acceptor_hash_remove(tcp); 4144 4145 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4146 tcp->tcp_ibsegs = 0; 4147 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4148 tcp->tcp_obsegs = 0; 4149 4150 /* 4151 * If we are an eager connection hanging off a listener that 4152 * hasn't formally accepted the connection yet, get off his 4153 * list and blow off any data that we have accumulated. 4154 */ 4155 if (tcp->tcp_listener != NULL) { 4156 tcp_t *listener = tcp->tcp_listener; 4157 mutex_enter(&listener->tcp_eager_lock); 4158 /* 4159 * tcp_tconnind_started == B_TRUE means that the 4160 * conn_ind has already gone to listener. At 4161 * this point, eager will be closed but we 4162 * leave it in listeners eager list so that 4163 * if listener decides to close without doing 4164 * accept, we can clean this up. In tcp_wput_accept 4165 * we take care of the case of accept on closed 4166 * eager. 4167 */ 4168 if (!tcp->tcp_tconnind_started) { 4169 tcp_eager_unlink(tcp); 4170 mutex_exit(&listener->tcp_eager_lock); 4171 /* 4172 * We don't want to have any pointers to the 4173 * listener queue, after we have released our 4174 * reference on the listener 4175 */ 4176 ASSERT(tcps->tcps_g_q != NULL); 4177 tcp->tcp_rq = tcps->tcps_g_q; 4178 tcp->tcp_wq = WR(tcps->tcps_g_q); 4179 CONN_DEC_REF(listener->tcp_connp); 4180 } else { 4181 mutex_exit(&listener->tcp_eager_lock); 4182 } 4183 } 4184 4185 /* Stop all the timers */ 4186 tcp_timers_stop(tcp); 4187 4188 if (tcp->tcp_state == TCPS_LISTEN) { 4189 if (tcp->tcp_ip_addr_cache) { 4190 kmem_free((void *)tcp->tcp_ip_addr_cache, 4191 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4192 tcp->tcp_ip_addr_cache = NULL; 4193 } 4194 } 4195 mutex_enter(&tcp->tcp_non_sq_lock); 4196 if (tcp->tcp_flow_stopped) 4197 tcp_clrqfull(tcp); 4198 mutex_exit(&tcp->tcp_non_sq_lock); 4199 4200 tcp_bind_hash_remove(tcp); 4201 /* 4202 * If the tcp_time_wait_collector (which runs outside the squeue) 4203 * is trying to remove this tcp from the time wait list, we will 4204 * block in tcp_time_wait_remove while trying to acquire the 4205 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4206 * requires the ipcl_hash_remove to be ordered after the 4207 * tcp_time_wait_remove for the refcnt checks to work correctly. 4208 */ 4209 if (tcp->tcp_state == TCPS_TIME_WAIT) 4210 (void) tcp_time_wait_remove(tcp, NULL); 4211 CL_INET_DISCONNECT(connp, tcp); 4212 ipcl_hash_remove(connp); 4213 4214 /* 4215 * Delete the cached ire in conn_ire_cache and also mark 4216 * the conn as CONDEMNED 4217 */ 4218 mutex_enter(&connp->conn_lock); 4219 connp->conn_state_flags |= CONN_CONDEMNED; 4220 ire = connp->conn_ire_cache; 4221 connp->conn_ire_cache = NULL; 4222 mutex_exit(&connp->conn_lock); 4223 if (ire != NULL) 4224 IRE_REFRELE_NOTR(ire); 4225 4226 /* Need to cleanup any pending ioctls */ 4227 ASSERT(tcp->tcp_time_wait_next == NULL); 4228 ASSERT(tcp->tcp_time_wait_prev == NULL); 4229 ASSERT(tcp->tcp_time_wait_expire == 0); 4230 tcp->tcp_state = TCPS_CLOSED; 4231 4232 /* Release any SSL context */ 4233 if (tcp->tcp_kssl_ent != NULL) { 4234 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4235 tcp->tcp_kssl_ent = NULL; 4236 } 4237 if (tcp->tcp_kssl_ctx != NULL) { 4238 kssl_release_ctx(tcp->tcp_kssl_ctx); 4239 tcp->tcp_kssl_ctx = NULL; 4240 } 4241 tcp->tcp_kssl_pending = B_FALSE; 4242 4243 tcp_ipsec_cleanup(tcp); 4244 } 4245 4246 /* 4247 * tcp is dying (called from ipcl_conn_destroy and error cases). 4248 * Free the tcp_t in either case. 4249 */ 4250 void 4251 tcp_free(tcp_t *tcp) 4252 { 4253 mblk_t *mp; 4254 ip6_pkt_t *ipp; 4255 4256 ASSERT(tcp != NULL); 4257 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4258 4259 tcp->tcp_rq = NULL; 4260 tcp->tcp_wq = NULL; 4261 4262 tcp_close_mpp(&tcp->tcp_xmit_head); 4263 tcp_close_mpp(&tcp->tcp_reass_head); 4264 if (tcp->tcp_rcv_list != NULL) { 4265 /* Free b_next chain */ 4266 tcp_close_mpp(&tcp->tcp_rcv_list); 4267 } 4268 if ((mp = tcp->tcp_urp_mp) != NULL) { 4269 freemsg(mp); 4270 } 4271 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4272 freemsg(mp); 4273 } 4274 4275 if (tcp->tcp_fused_sigurg_mp != NULL) { 4276 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4277 freeb(tcp->tcp_fused_sigurg_mp); 4278 tcp->tcp_fused_sigurg_mp = NULL; 4279 } 4280 4281 if (tcp->tcp_ordrel_mp != NULL) { 4282 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4283 freeb(tcp->tcp_ordrel_mp); 4284 tcp->tcp_ordrel_mp = NULL; 4285 } 4286 4287 if (tcp->tcp_sack_info != NULL) { 4288 if (tcp->tcp_notsack_list != NULL) { 4289 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4290 } 4291 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4292 } 4293 4294 if (tcp->tcp_hopopts != NULL) { 4295 mi_free(tcp->tcp_hopopts); 4296 tcp->tcp_hopopts = NULL; 4297 tcp->tcp_hopoptslen = 0; 4298 } 4299 ASSERT(tcp->tcp_hopoptslen == 0); 4300 if (tcp->tcp_dstopts != NULL) { 4301 mi_free(tcp->tcp_dstopts); 4302 tcp->tcp_dstopts = NULL; 4303 tcp->tcp_dstoptslen = 0; 4304 } 4305 ASSERT(tcp->tcp_dstoptslen == 0); 4306 if (tcp->tcp_rtdstopts != NULL) { 4307 mi_free(tcp->tcp_rtdstopts); 4308 tcp->tcp_rtdstopts = NULL; 4309 tcp->tcp_rtdstoptslen = 0; 4310 } 4311 ASSERT(tcp->tcp_rtdstoptslen == 0); 4312 if (tcp->tcp_rthdr != NULL) { 4313 mi_free(tcp->tcp_rthdr); 4314 tcp->tcp_rthdr = NULL; 4315 tcp->tcp_rthdrlen = 0; 4316 } 4317 ASSERT(tcp->tcp_rthdrlen == 0); 4318 4319 ipp = &tcp->tcp_sticky_ipp; 4320 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4321 IPPF_RTHDR)) 4322 ip6_pkt_free(ipp); 4323 4324 /* 4325 * Free memory associated with the tcp/ip header template. 4326 */ 4327 4328 if (tcp->tcp_iphc != NULL) 4329 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4330 4331 /* 4332 * Following is really a blowing away a union. 4333 * It happens to have exactly two members of identical size 4334 * the following code is enough. 4335 */ 4336 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4337 } 4338 4339 4340 /* 4341 * Put a connection confirmation message upstream built from the 4342 * address information within 'iph' and 'tcph'. Report our success or failure. 4343 */ 4344 static boolean_t 4345 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4346 mblk_t **defermp) 4347 { 4348 sin_t sin; 4349 sin6_t sin6; 4350 mblk_t *mp; 4351 char *optp = NULL; 4352 int optlen = 0; 4353 4354 if (defermp != NULL) 4355 *defermp = NULL; 4356 4357 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4358 /* 4359 * Return in T_CONN_CON results of option negotiation through 4360 * the T_CONN_REQ. Note: If there is an real end-to-end option 4361 * negotiation, then what is received from remote end needs 4362 * to be taken into account but there is no such thing (yet?) 4363 * in our TCP/IP. 4364 * Note: We do not use mi_offset_param() here as 4365 * tcp_opts_conn_req contents do not directly come from 4366 * an application and are either generated in kernel or 4367 * from user input that was already verified. 4368 */ 4369 mp = tcp->tcp_conn.tcp_opts_conn_req; 4370 optp = (char *)(mp->b_rptr + 4371 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4372 optlen = (int) 4373 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4374 } 4375 4376 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4377 ipha_t *ipha = (ipha_t *)iphdr; 4378 4379 /* packet is IPv4 */ 4380 if (tcp->tcp_family == AF_INET) { 4381 sin = sin_null; 4382 sin.sin_addr.s_addr = ipha->ipha_src; 4383 sin.sin_port = *(uint16_t *)tcph->th_lport; 4384 sin.sin_family = AF_INET; 4385 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4386 (int)sizeof (sin_t), optp, optlen); 4387 } else { 4388 sin6 = sin6_null; 4389 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4390 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4391 sin6.sin6_family = AF_INET6; 4392 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4393 (int)sizeof (sin6_t), optp, optlen); 4394 4395 } 4396 } else { 4397 ip6_t *ip6h = (ip6_t *)iphdr; 4398 4399 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4400 ASSERT(tcp->tcp_family == AF_INET6); 4401 sin6 = sin6_null; 4402 sin6.sin6_addr = ip6h->ip6_src; 4403 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4404 sin6.sin6_family = AF_INET6; 4405 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4406 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4407 (int)sizeof (sin6_t), optp, optlen); 4408 } 4409 4410 if (!mp) 4411 return (B_FALSE); 4412 4413 mblk_copycred(mp, idmp); 4414 4415 if (defermp == NULL) { 4416 conn_t *connp = tcp->tcp_connp; 4417 if (IPCL_IS_NONSTR(connp)) { 4418 cred_t *cr; 4419 pid_t cpid; 4420 4421 cr = msg_getcred(mp, &cpid); 4422 (*connp->conn_upcalls->su_connected) 4423 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4424 cpid); 4425 freemsg(mp); 4426 } else { 4427 putnext(tcp->tcp_rq, mp); 4428 } 4429 } else { 4430 *defermp = mp; 4431 } 4432 4433 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4434 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4435 return (B_TRUE); 4436 } 4437 4438 /* 4439 * Defense for the SYN attack - 4440 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4441 * one from the list of droppable eagers. This list is a subset of q0. 4442 * see comments before the definition of MAKE_DROPPABLE(). 4443 * 2. Don't drop a SYN request before its first timeout. This gives every 4444 * request at least til the first timeout to complete its 3-way handshake. 4445 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4446 * requests currently on the queue that has timed out. This will be used 4447 * as an indicator of whether an attack is under way, so that appropriate 4448 * actions can be taken. (It's incremented in tcp_timer() and decremented 4449 * either when eager goes into ESTABLISHED, or gets freed up.) 4450 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4451 * # of timeout drops back to <= q0len/32 => SYN alert off 4452 */ 4453 static boolean_t 4454 tcp_drop_q0(tcp_t *tcp) 4455 { 4456 tcp_t *eager; 4457 mblk_t *mp; 4458 tcp_stack_t *tcps = tcp->tcp_tcps; 4459 4460 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4461 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4462 4463 /* Pick oldest eager from the list of droppable eagers */ 4464 eager = tcp->tcp_eager_prev_drop_q0; 4465 4466 /* If list is empty. return B_FALSE */ 4467 if (eager == tcp) { 4468 return (B_FALSE); 4469 } 4470 4471 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4472 if ((mp = allocb(0, BPRI_HI)) == NULL) 4473 return (B_FALSE); 4474 4475 /* 4476 * Take this eager out from the list of droppable eagers since we are 4477 * going to drop it. 4478 */ 4479 MAKE_UNDROPPABLE(eager); 4480 4481 if (tcp->tcp_debug) { 4482 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4483 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4484 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4485 tcp->tcp_conn_req_cnt_q0, 4486 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4487 } 4488 4489 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4490 4491 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4492 CONN_INC_REF(eager->tcp_connp); 4493 4494 /* Mark the IRE created for this SYN request temporary */ 4495 tcp_ip_ire_mark_advice(eager); 4496 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4497 tcp_clean_death_wrapper, eager->tcp_connp, 4498 SQ_FILL, SQTAG_TCP_DROP_Q0); 4499 4500 return (B_TRUE); 4501 } 4502 4503 int 4504 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4505 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4506 { 4507 tcp_t *ltcp = lconnp->conn_tcp; 4508 tcp_t *tcp = connp->conn_tcp; 4509 mblk_t *tpi_mp; 4510 ipha_t *ipha; 4511 ip6_t *ip6h; 4512 sin6_t sin6; 4513 in6_addr_t v6dst; 4514 int err; 4515 int ifindex = 0; 4516 tcp_stack_t *tcps = tcp->tcp_tcps; 4517 4518 if (ipvers == IPV4_VERSION) { 4519 ipha = (ipha_t *)mp->b_rptr; 4520 4521 connp->conn_send = ip_output; 4522 connp->conn_recv = tcp_input; 4523 4524 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4525 &connp->conn_bound_source_v6); 4526 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4527 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4528 4529 sin6 = sin6_null; 4530 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4531 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4532 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4533 sin6.sin6_family = AF_INET6; 4534 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4535 lconnp->conn_zoneid, tcps->tcps_netstack); 4536 if (tcp->tcp_recvdstaddr) { 4537 sin6_t sin6d; 4538 4539 sin6d = sin6_null; 4540 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4541 &sin6d.sin6_addr); 4542 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4543 sin6d.sin6_family = AF_INET; 4544 tpi_mp = mi_tpi_extconn_ind(NULL, 4545 (char *)&sin6d, sizeof (sin6_t), 4546 (char *)&tcp, 4547 (t_scalar_t)sizeof (intptr_t), 4548 (char *)&sin6d, sizeof (sin6_t), 4549 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4550 } else { 4551 tpi_mp = mi_tpi_conn_ind(NULL, 4552 (char *)&sin6, sizeof (sin6_t), 4553 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4554 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4555 } 4556 } else { 4557 ip6h = (ip6_t *)mp->b_rptr; 4558 4559 connp->conn_send = ip_output_v6; 4560 connp->conn_recv = tcp_input; 4561 4562 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4563 connp->conn_srcv6 = ip6h->ip6_dst; 4564 connp->conn_remv6 = ip6h->ip6_src; 4565 4566 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4567 ifindex = (int)DB_CKSUMSTUFF(mp); 4568 DB_CKSUMSTUFF(mp) = 0; 4569 4570 sin6 = sin6_null; 4571 sin6.sin6_addr = ip6h->ip6_src; 4572 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4573 sin6.sin6_family = AF_INET6; 4574 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4575 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4576 lconnp->conn_zoneid, tcps->tcps_netstack); 4577 4578 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4579 /* Pass up the scope_id of remote addr */ 4580 sin6.sin6_scope_id = ifindex; 4581 } else { 4582 sin6.sin6_scope_id = 0; 4583 } 4584 if (tcp->tcp_recvdstaddr) { 4585 sin6_t sin6d; 4586 4587 sin6d = sin6_null; 4588 sin6.sin6_addr = ip6h->ip6_dst; 4589 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4590 sin6d.sin6_family = AF_INET; 4591 tpi_mp = mi_tpi_extconn_ind(NULL, 4592 (char *)&sin6d, sizeof (sin6_t), 4593 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4594 (char *)&sin6d, sizeof (sin6_t), 4595 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4596 } else { 4597 tpi_mp = mi_tpi_conn_ind(NULL, 4598 (char *)&sin6, sizeof (sin6_t), 4599 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4600 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4601 } 4602 } 4603 4604 if (tpi_mp == NULL) 4605 return (ENOMEM); 4606 4607 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4608 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4609 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4610 connp->conn_fully_bound = B_FALSE; 4611 4612 /* Inherit information from the "parent" */ 4613 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4614 tcp->tcp_family = ltcp->tcp_family; 4615 4616 tcp->tcp_wq = ltcp->tcp_wq; 4617 tcp->tcp_rq = ltcp->tcp_rq; 4618 4619 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4620 tcp->tcp_detached = B_TRUE; 4621 SOCK_CONNID_INIT(tcp->tcp_connid); 4622 if ((err = tcp_init_values(tcp)) != 0) { 4623 freemsg(tpi_mp); 4624 return (err); 4625 } 4626 4627 if (ipvers == IPV4_VERSION) { 4628 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4629 freemsg(tpi_mp); 4630 return (err); 4631 } 4632 ASSERT(tcp->tcp_ipha != NULL); 4633 } else { 4634 /* ifindex must be already set */ 4635 ASSERT(ifindex != 0); 4636 4637 if (ltcp->tcp_bound_if != 0) 4638 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4639 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4640 tcp->tcp_bound_if = ifindex; 4641 4642 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4643 tcp->tcp_recvifindex = 0; 4644 tcp->tcp_recvhops = 0xffffffffU; 4645 ASSERT(tcp->tcp_ip6h != NULL); 4646 } 4647 4648 tcp->tcp_lport = ltcp->tcp_lport; 4649 4650 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4651 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4652 /* 4653 * Listener had options of some sort; eager inherits. 4654 * Free up the eager template and allocate one 4655 * of the right size. 4656 */ 4657 if (tcp->tcp_hdr_grown) { 4658 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4659 } else { 4660 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4661 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4662 } 4663 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4664 KM_NOSLEEP); 4665 if (tcp->tcp_iphc == NULL) { 4666 tcp->tcp_iphc_len = 0; 4667 freemsg(tpi_mp); 4668 return (ENOMEM); 4669 } 4670 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4671 tcp->tcp_hdr_grown = B_TRUE; 4672 } 4673 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4674 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4675 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4676 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4677 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4678 4679 /* 4680 * Copy the IP+TCP header template from listener to eager 4681 */ 4682 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4683 if (tcp->tcp_ipversion == IPV6_VERSION) { 4684 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4685 IPPROTO_RAW) { 4686 tcp->tcp_ip6h = 4687 (ip6_t *)(tcp->tcp_iphc + 4688 sizeof (ip6i_t)); 4689 } else { 4690 tcp->tcp_ip6h = 4691 (ip6_t *)(tcp->tcp_iphc); 4692 } 4693 tcp->tcp_ipha = NULL; 4694 } else { 4695 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4696 tcp->tcp_ip6h = NULL; 4697 } 4698 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4699 tcp->tcp_ip_hdr_len); 4700 } else { 4701 /* 4702 * only valid case when ipversion of listener and 4703 * eager differ is when listener is IPv6 and 4704 * eager is IPv4. 4705 * Eager header template has been initialized to the 4706 * maximum v4 header sizes, which includes space for 4707 * TCP and IP options. 4708 */ 4709 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4710 (tcp->tcp_ipversion == IPV4_VERSION)); 4711 ASSERT(tcp->tcp_iphc_len >= 4712 TCP_MAX_COMBINED_HEADER_LENGTH); 4713 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4714 /* copy IP header fields individually */ 4715 tcp->tcp_ipha->ipha_ttl = 4716 ltcp->tcp_ip6h->ip6_hops; 4717 bcopy(ltcp->tcp_tcph->th_lport, 4718 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4719 } 4720 4721 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4722 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4723 sizeof (in_port_t)); 4724 4725 if (ltcp->tcp_lport == 0) { 4726 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4727 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4728 sizeof (in_port_t)); 4729 } 4730 4731 if (tcp->tcp_ipversion == IPV4_VERSION) { 4732 ASSERT(ipha != NULL); 4733 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4734 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4735 4736 /* Source routing option copyover (reverse it) */ 4737 if (tcps->tcps_rev_src_routes) 4738 tcp_opt_reverse(tcp, ipha); 4739 } else { 4740 ASSERT(ip6h != NULL); 4741 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4742 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4743 } 4744 4745 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4746 ASSERT(!tcp->tcp_tconnind_started); 4747 /* 4748 * If the SYN contains a credential, it's a loopback packet; attach 4749 * the credential to the TPI message. 4750 */ 4751 mblk_copycred(tpi_mp, idmp); 4752 4753 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4754 4755 /* Inherit the listener's SSL protection state */ 4756 4757 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4758 kssl_hold_ent(tcp->tcp_kssl_ent); 4759 tcp->tcp_kssl_pending = B_TRUE; 4760 } 4761 4762 /* Inherit the listener's non-STREAMS flag */ 4763 if (IPCL_IS_NONSTR(lconnp)) { 4764 connp->conn_flags |= IPCL_NONSTR; 4765 } 4766 4767 return (0); 4768 } 4769 4770 4771 int 4772 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4773 tcph_t *tcph, mblk_t *idmp) 4774 { 4775 tcp_t *ltcp = lconnp->conn_tcp; 4776 tcp_t *tcp = connp->conn_tcp; 4777 sin_t sin; 4778 mblk_t *tpi_mp = NULL; 4779 int err; 4780 tcp_stack_t *tcps = tcp->tcp_tcps; 4781 4782 sin = sin_null; 4783 sin.sin_addr.s_addr = ipha->ipha_src; 4784 sin.sin_port = *(uint16_t *)tcph->th_lport; 4785 sin.sin_family = AF_INET; 4786 if (ltcp->tcp_recvdstaddr) { 4787 sin_t sind; 4788 4789 sind = sin_null; 4790 sind.sin_addr.s_addr = ipha->ipha_dst; 4791 sind.sin_port = *(uint16_t *)tcph->th_fport; 4792 sind.sin_family = AF_INET; 4793 tpi_mp = mi_tpi_extconn_ind(NULL, 4794 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4795 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4796 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4797 } else { 4798 tpi_mp = mi_tpi_conn_ind(NULL, 4799 (char *)&sin, sizeof (sin_t), 4800 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4801 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4802 } 4803 4804 if (tpi_mp == NULL) { 4805 return (ENOMEM); 4806 } 4807 4808 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4809 connp->conn_send = ip_output; 4810 connp->conn_recv = tcp_input; 4811 connp->conn_fully_bound = B_FALSE; 4812 4813 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4814 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4815 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4816 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4817 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4818 4819 /* Inherit information from the "parent" */ 4820 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4821 tcp->tcp_family = ltcp->tcp_family; 4822 tcp->tcp_wq = ltcp->tcp_wq; 4823 tcp->tcp_rq = ltcp->tcp_rq; 4824 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4825 tcp->tcp_detached = B_TRUE; 4826 SOCK_CONNID_INIT(tcp->tcp_connid); 4827 if ((err = tcp_init_values(tcp)) != 0) { 4828 freemsg(tpi_mp); 4829 return (err); 4830 } 4831 4832 /* 4833 * Let's make sure that eager tcp template has enough space to 4834 * copy IPv4 listener's tcp template. Since the conn_t structure is 4835 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4836 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4837 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4838 * extension headers or with ip6i_t struct). Note that bcopy() below 4839 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4840 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4841 */ 4842 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4843 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4844 4845 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4846 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4847 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4848 tcp->tcp_ttl = ltcp->tcp_ttl; 4849 tcp->tcp_tos = ltcp->tcp_tos; 4850 4851 /* Copy the IP+TCP header template from listener to eager */ 4852 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4853 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4854 tcp->tcp_ip6h = NULL; 4855 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4856 tcp->tcp_ip_hdr_len); 4857 4858 /* Initialize the IP addresses and Ports */ 4859 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4860 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4861 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4862 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4863 4864 /* Source routing option copyover (reverse it) */ 4865 if (tcps->tcps_rev_src_routes) 4866 tcp_opt_reverse(tcp, ipha); 4867 4868 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4869 ASSERT(!tcp->tcp_tconnind_started); 4870 4871 /* 4872 * If the SYN contains a credential, it's a loopback packet; attach 4873 * the credential to the TPI message. 4874 */ 4875 mblk_copycred(tpi_mp, idmp); 4876 4877 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4878 4879 /* Inherit the listener's SSL protection state */ 4880 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4881 kssl_hold_ent(tcp->tcp_kssl_ent); 4882 tcp->tcp_kssl_pending = B_TRUE; 4883 } 4884 4885 /* Inherit the listener's non-STREAMS flag */ 4886 if (IPCL_IS_NONSTR(lconnp)) { 4887 connp->conn_flags |= IPCL_NONSTR; 4888 } 4889 4890 return (0); 4891 } 4892 4893 /* 4894 * sets up conn for ipsec. 4895 * if the first mblk is M_CTL it is consumed and mpp is updated. 4896 * in case of error mpp is freed. 4897 */ 4898 conn_t * 4899 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp, 4900 boolean_t is_streams) 4901 { 4902 conn_t *connp = tcp->tcp_connp; 4903 conn_t *econnp; 4904 squeue_t *new_sqp; 4905 mblk_t *first_mp = *mpp; 4906 mblk_t *mp = *mpp; 4907 boolean_t mctl_present = B_FALSE; 4908 uint_t ipvers; 4909 4910 econnp = tcp_get_conn(sqp, tcp->tcp_tcps, is_streams); 4911 if (econnp == NULL) { 4912 freemsg(first_mp); 4913 return (NULL); 4914 } 4915 if (DB_TYPE(mp) == M_CTL) { 4916 if (mp->b_cont == NULL || 4917 mp->b_cont->b_datap->db_type != M_DATA) { 4918 freemsg(first_mp); 4919 return (NULL); 4920 } 4921 mp = mp->b_cont; 4922 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4923 freemsg(first_mp); 4924 return (NULL); 4925 } 4926 4927 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4928 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4929 mctl_present = B_TRUE; 4930 } else { 4931 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4932 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4933 } 4934 4935 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4936 DB_CKSUMSTART(mp) = 0; 4937 4938 ASSERT(OK_32PTR(mp->b_rptr)); 4939 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4940 if (ipvers == IPV4_VERSION) { 4941 uint16_t *up; 4942 uint32_t ports; 4943 ipha_t *ipha; 4944 4945 ipha = (ipha_t *)mp->b_rptr; 4946 up = (uint16_t *)((uchar_t *)ipha + 4947 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4948 ports = *(uint32_t *)up; 4949 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4950 ipha->ipha_dst, ipha->ipha_src, ports); 4951 } else { 4952 uint16_t *up; 4953 uint32_t ports; 4954 uint16_t ip_hdr_len; 4955 uint8_t *nexthdrp; 4956 ip6_t *ip6h; 4957 tcph_t *tcph; 4958 4959 ip6h = (ip6_t *)mp->b_rptr; 4960 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4961 ip_hdr_len = IPV6_HDR_LEN; 4962 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4963 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4964 CONN_DEC_REF(econnp); 4965 freemsg(first_mp); 4966 return (NULL); 4967 } 4968 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4969 up = (uint16_t *)tcph->th_lport; 4970 ports = *(uint32_t *)up; 4971 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4972 ip6h->ip6_dst, ip6h->ip6_src, ports); 4973 } 4974 4975 /* 4976 * The caller already ensured that there is a sqp present. 4977 */ 4978 econnp->conn_sqp = new_sqp; 4979 econnp->conn_initial_sqp = new_sqp; 4980 4981 if (connp->conn_policy != NULL) { 4982 ipsec_in_t *ii; 4983 ii = (ipsec_in_t *)(first_mp->b_rptr); 4984 ASSERT(ii->ipsec_in_policy == NULL); 4985 IPPH_REFHOLD(connp->conn_policy); 4986 ii->ipsec_in_policy = connp->conn_policy; 4987 4988 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4989 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4990 CONN_DEC_REF(econnp); 4991 freemsg(first_mp); 4992 return (NULL); 4993 } 4994 } 4995 4996 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4997 CONN_DEC_REF(econnp); 4998 freemsg(first_mp); 4999 return (NULL); 5000 } 5001 5002 /* 5003 * If we know we have some policy, pass the "IPSEC" 5004 * options size TCP uses this adjust the MSS. 5005 */ 5006 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5007 if (mctl_present) { 5008 freeb(first_mp); 5009 *mpp = mp; 5010 } 5011 5012 return (econnp); 5013 } 5014 5015 /* 5016 * tcp_get_conn/tcp_free_conn 5017 * 5018 * tcp_get_conn is used to get a clean tcp connection structure. 5019 * It tries to reuse the connections put on the freelist by the 5020 * time_wait_collector failing which it goes to kmem_cache. This 5021 * way has two benefits compared to just allocating from and 5022 * freeing to kmem_cache. 5023 * 1) The time_wait_collector can free (which includes the cleanup) 5024 * outside the squeue. So when the interrupt comes, we have a clean 5025 * connection sitting in the freelist. Obviously, this buys us 5026 * performance. 5027 * 5028 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5029 * has multiple disadvantages - tying up the squeue during alloc, and the 5030 * fact that IPSec policy initialization has to happen here which 5031 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5032 * But allocating the conn/tcp in IP land is also not the best since 5033 * we can't check the 'q' and 'q0' which are protected by squeue and 5034 * blindly allocate memory which might have to be freed here if we are 5035 * not allowed to accept the connection. By using the freelist and 5036 * putting the conn/tcp back in freelist, we don't pay a penalty for 5037 * allocating memory without checking 'q/q0' and freeing it if we can't 5038 * accept the connection. 5039 * 5040 * Care should be taken to put the conn back in the same squeue's freelist 5041 * from which it was allocated. Best results are obtained if conn is 5042 * allocated from listener's squeue and freed to the same. Time wait 5043 * collector will free up the freelist is the connection ends up sitting 5044 * there for too long. 5045 */ 5046 void * 5047 tcp_get_conn(void *arg, tcp_stack_t *tcps, boolean_t is_streams) 5048 { 5049 tcp_t *tcp = NULL; 5050 conn_t *connp = NULL; 5051 squeue_t *sqp = (squeue_t *)arg; 5052 tcp_squeue_priv_t *tcp_time_wait; 5053 netstack_t *ns; 5054 mblk_t *tcp_rsrv_mp = NULL; 5055 5056 tcp_time_wait = 5057 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5058 5059 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5060 tcp = tcp_time_wait->tcp_free_list; 5061 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5062 if (tcp != NULL) { 5063 if (is_streams && tcp->tcp_rsrv_mp == NULL) { 5064 /* 5065 * Pre-allocate the tcp_rsrv_mp if neccessary. 5066 * This mblk will not be freed until this conn_t/tcp_t 5067 * is freed at ipcl_conn_destroy(). 5068 */ 5069 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5070 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5071 return (NULL); 5072 } 5073 mutex_init(&tcp->tcp_rsrv_mp_lock, 5074 NULL, MUTEX_DEFAULT, NULL); 5075 } 5076 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5077 tcp_time_wait->tcp_free_list_cnt--; 5078 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5079 tcp->tcp_time_wait_next = NULL; 5080 connp = tcp->tcp_connp; 5081 connp->conn_flags |= IPCL_REUSED; 5082 5083 ASSERT(tcp->tcp_tcps == NULL); 5084 ASSERT(connp->conn_netstack == NULL); 5085 ASSERT(!is_streams || tcp->tcp_rsrv_mp != NULL); 5086 ns = tcps->tcps_netstack; 5087 netstack_hold(ns); 5088 connp->conn_netstack = ns; 5089 tcp->tcp_tcps = tcps; 5090 TCPS_REFHOLD(tcps); 5091 ipcl_globalhash_insert(connp); 5092 return ((void *)connp); 5093 } 5094 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5095 if (is_streams) { 5096 /* 5097 * Pre-allocate the tcp_rsrv_mp if neccessary. 5098 * This mblk will not be freed until this conn_t/tcp_t 5099 * is freed at ipcl_conn_destroy(). 5100 */ 5101 tcp_rsrv_mp = allocb(0, BPRI_HI); 5102 if (tcp_rsrv_mp == NULL) 5103 return (NULL); 5104 } 5105 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5106 tcps->tcps_netstack)) == NULL) { 5107 if (is_streams) { 5108 ASSERT(tcp_rsrv_mp != NULL); 5109 freeb(tcp_rsrv_mp); 5110 } 5111 return (NULL); 5112 } 5113 5114 tcp = connp->conn_tcp; 5115 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5116 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5117 5118 tcp->tcp_tcps = tcps; 5119 TCPS_REFHOLD(tcps); 5120 5121 return ((void *)connp); 5122 } 5123 5124 /* 5125 * Update the cached label for the given tcp_t. This should be called once per 5126 * connection, and before any packets are sent or tcp_process_options is 5127 * invoked. Returns B_FALSE if the correct label could not be constructed. 5128 */ 5129 static boolean_t 5130 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5131 { 5132 conn_t *connp = tcp->tcp_connp; 5133 5134 if (tcp->tcp_ipversion == IPV4_VERSION) { 5135 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5136 int added; 5137 5138 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5139 connp->conn_mac_exempt, 5140 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5141 return (B_FALSE); 5142 5143 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5144 if (added == -1) 5145 return (B_FALSE); 5146 tcp->tcp_hdr_len += added; 5147 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5148 tcp->tcp_ip_hdr_len += added; 5149 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5150 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5151 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5152 tcp->tcp_hdr_len); 5153 if (added == -1) 5154 return (B_FALSE); 5155 tcp->tcp_hdr_len += added; 5156 tcp->tcp_tcph = (tcph_t *) 5157 ((uchar_t *)tcp->tcp_tcph + added); 5158 tcp->tcp_ip_hdr_len += added; 5159 } 5160 } else { 5161 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5162 5163 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5164 connp->conn_mac_exempt, 5165 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5166 return (B_FALSE); 5167 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5168 &tcp->tcp_label_len, optbuf) != 0) 5169 return (B_FALSE); 5170 if (tcp_build_hdrs(tcp) != 0) 5171 return (B_FALSE); 5172 } 5173 5174 connp->conn_ulp_labeled = 1; 5175 5176 return (B_TRUE); 5177 } 5178 5179 /* BEGIN CSTYLED */ 5180 /* 5181 * 5182 * The sockfs ACCEPT path: 5183 * ======================= 5184 * 5185 * The eager is now established in its own perimeter as soon as SYN is 5186 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5187 * completes the accept processing on the acceptor STREAM. The sending 5188 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5189 * listener but a TLI/XTI listener completes the accept processing 5190 * on the listener perimeter. 5191 * 5192 * Common control flow for 3 way handshake: 5193 * ---------------------------------------- 5194 * 5195 * incoming SYN (listener perimeter) -> tcp_rput_data() 5196 * -> tcp_conn_request() 5197 * 5198 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5199 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5200 * 5201 * Sockfs ACCEPT Path: 5202 * ------------------- 5203 * 5204 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5205 * as STREAM entry point) 5206 * 5207 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5208 * 5209 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5210 * association (we are not behind eager's squeue but sockfs is protecting us 5211 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5212 * is changed to point at tcp_wput(). 5213 * 5214 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5215 * listener (done on listener's perimeter). 5216 * 5217 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5218 * accept. 5219 * 5220 * TLI/XTI client ACCEPT path: 5221 * --------------------------- 5222 * 5223 * soaccept() sends T_CONN_RES on the listener STREAM. 5224 * 5225 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5226 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5227 * 5228 * Locks: 5229 * ====== 5230 * 5231 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5232 * and listeners->tcp_eager_next_q. 5233 * 5234 * Referencing: 5235 * ============ 5236 * 5237 * 1) We start out in tcp_conn_request by eager placing a ref on 5238 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5239 * 5240 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5241 * doing so we place a ref on the eager. This ref is finally dropped at the 5242 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5243 * reference is dropped by the squeue framework. 5244 * 5245 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5246 * 5247 * The reference must be released by the same entity that added the reference 5248 * In the above scheme, the eager is the entity that adds and releases the 5249 * references. Note that tcp_accept_finish executes in the squeue of the eager 5250 * (albeit after it is attached to the acceptor stream). Though 1. executes 5251 * in the listener's squeue, the eager is nascent at this point and the 5252 * reference can be considered to have been added on behalf of the eager. 5253 * 5254 * Eager getting a Reset or listener closing: 5255 * ========================================== 5256 * 5257 * Once the listener and eager are linked, the listener never does the unlink. 5258 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5259 * a message on all eager perimeter. The eager then does the unlink, clears 5260 * any pointers to the listener's queue and drops the reference to the 5261 * listener. The listener waits in tcp_close outside the squeue until its 5262 * refcount has dropped to 1. This ensures that the listener has waited for 5263 * all eagers to clear their association with the listener. 5264 * 5265 * Similarly, if eager decides to go away, it can unlink itself and close. 5266 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5267 * the reference to eager is still valid because of the extra ref we put 5268 * in tcp_send_conn_ind. 5269 * 5270 * Listener can always locate the eager under the protection 5271 * of the listener->tcp_eager_lock, and then do a refhold 5272 * on the eager during the accept processing. 5273 * 5274 * The acceptor stream accesses the eager in the accept processing 5275 * based on the ref placed on eager before sending T_conn_ind. 5276 * The only entity that can negate this refhold is a listener close 5277 * which is mutually exclusive with an active acceptor stream. 5278 * 5279 * Eager's reference on the listener 5280 * =================================== 5281 * 5282 * If the accept happens (even on a closed eager) the eager drops its 5283 * reference on the listener at the start of tcp_accept_finish. If the 5284 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5285 * the reference is dropped in tcp_closei_local. If the listener closes, 5286 * the reference is dropped in tcp_eager_kill. In all cases the reference 5287 * is dropped while executing in the eager's context (squeue). 5288 */ 5289 /* END CSTYLED */ 5290 5291 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5292 5293 /* 5294 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5295 * tcp_rput_data will not see any SYN packets. 5296 */ 5297 /* ARGSUSED */ 5298 void 5299 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5300 { 5301 tcph_t *tcph; 5302 uint32_t seg_seq; 5303 tcp_t *eager; 5304 uint_t ipvers; 5305 ipha_t *ipha; 5306 ip6_t *ip6h; 5307 int err; 5308 conn_t *econnp = NULL; 5309 squeue_t *new_sqp; 5310 mblk_t *mp1; 5311 uint_t ip_hdr_len; 5312 conn_t *connp = (conn_t *)arg; 5313 tcp_t *tcp = connp->conn_tcp; 5314 cred_t *credp; 5315 tcp_stack_t *tcps = tcp->tcp_tcps; 5316 ip_stack_t *ipst; 5317 5318 if (tcp->tcp_state != TCPS_LISTEN) 5319 goto error2; 5320 5321 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5322 5323 mutex_enter(&tcp->tcp_eager_lock); 5324 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5325 mutex_exit(&tcp->tcp_eager_lock); 5326 TCP_STAT(tcps, tcp_listendrop); 5327 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5328 if (tcp->tcp_debug) { 5329 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5330 "tcp_conn_request: listen backlog (max=%d) " 5331 "overflow (%d pending) on %s", 5332 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5333 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5334 } 5335 goto error2; 5336 } 5337 5338 if (tcp->tcp_conn_req_cnt_q0 >= 5339 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5340 /* 5341 * Q0 is full. Drop a pending half-open req from the queue 5342 * to make room for the new SYN req. Also mark the time we 5343 * drop a SYN. 5344 * 5345 * A more aggressive defense against SYN attack will 5346 * be to set the "tcp_syn_defense" flag now. 5347 */ 5348 TCP_STAT(tcps, tcp_listendropq0); 5349 tcp->tcp_last_rcv_lbolt = lbolt64; 5350 if (!tcp_drop_q0(tcp)) { 5351 mutex_exit(&tcp->tcp_eager_lock); 5352 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5353 if (tcp->tcp_debug) { 5354 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5355 "tcp_conn_request: listen half-open queue " 5356 "(max=%d) full (%d pending) on %s", 5357 tcps->tcps_conn_req_max_q0, 5358 tcp->tcp_conn_req_cnt_q0, 5359 tcp_display(tcp, NULL, 5360 DISP_PORT_ONLY)); 5361 } 5362 goto error2; 5363 } 5364 } 5365 mutex_exit(&tcp->tcp_eager_lock); 5366 5367 /* 5368 * IP adds STRUIO_EAGER and ensures that the received packet is 5369 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5370 * link local address. If IPSec is enabled, db_struioflag has 5371 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5372 * otherwise an error case if neither of them is set. 5373 */ 5374 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5375 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5376 DB_CKSUMSTART(mp) = 0; 5377 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5378 econnp = (conn_t *)tcp_get_conn(arg2, tcps, 5379 !IPCL_IS_NONSTR(connp)); 5380 if (econnp == NULL) 5381 goto error2; 5382 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5383 econnp->conn_sqp = new_sqp; 5384 econnp->conn_initial_sqp = new_sqp; 5385 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5386 /* 5387 * mp is updated in tcp_get_ipsec_conn(). 5388 */ 5389 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp, 5390 !IPCL_IS_NONSTR(connp)); 5391 if (econnp == NULL) { 5392 /* 5393 * mp freed by tcp_get_ipsec_conn. 5394 */ 5395 return; 5396 } 5397 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5398 } else { 5399 goto error2; 5400 } 5401 5402 ASSERT(DB_TYPE(mp) == M_DATA); 5403 5404 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5405 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5406 ASSERT(OK_32PTR(mp->b_rptr)); 5407 if (ipvers == IPV4_VERSION) { 5408 ipha = (ipha_t *)mp->b_rptr; 5409 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5410 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5411 } else { 5412 ip6h = (ip6_t *)mp->b_rptr; 5413 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5414 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5415 } 5416 5417 if (tcp->tcp_family == AF_INET) { 5418 ASSERT(ipvers == IPV4_VERSION); 5419 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5420 } else { 5421 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5422 } 5423 5424 if (err) 5425 goto error3; 5426 5427 eager = econnp->conn_tcp; 5428 ASSERT(eager->tcp_ordrel_mp == NULL); 5429 5430 if (!IPCL_IS_NONSTR(econnp)) { 5431 /* 5432 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5433 * at close time, we will always have that to send up. 5434 * Otherwise, we need to do special handling in case the 5435 * allocation fails at that time. 5436 */ 5437 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5438 goto error3; 5439 } 5440 /* Inherit various TCP parameters from the listener */ 5441 eager->tcp_naglim = tcp->tcp_naglim; 5442 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5443 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5444 5445 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5446 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5447 5448 /* 5449 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5450 * If it does not, the eager's receive window will be set to the 5451 * listener's receive window later in this function. 5452 */ 5453 eager->tcp_rwnd = 0; 5454 5455 /* 5456 * Inherit listener's tcp_init_cwnd. Need to do this before 5457 * calling tcp_process_options() where tcp_mss_set() is called 5458 * to set the initial cwnd. 5459 */ 5460 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5461 5462 /* 5463 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5464 * zone id before the accept is completed in tcp_wput_accept(). 5465 */ 5466 econnp->conn_zoneid = connp->conn_zoneid; 5467 econnp->conn_allzones = connp->conn_allzones; 5468 5469 /* Copy nexthop information from listener to eager */ 5470 if (connp->conn_nexthop_set) { 5471 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5472 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5473 } 5474 5475 /* 5476 * TSOL: tsol_input_proc() needs the eager's cred before the 5477 * eager is accepted 5478 */ 5479 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5480 crhold(credp); 5481 5482 /* 5483 * If the caller has the process-wide flag set, then default to MAC 5484 * exempt mode. This allows read-down to unlabeled hosts. 5485 */ 5486 if (getpflags(NET_MAC_AWARE, credp) != 0) 5487 econnp->conn_mac_exempt = B_TRUE; 5488 5489 if (is_system_labeled()) { 5490 cred_t *cr; 5491 5492 if (connp->conn_mlp_type != mlptSingle) { 5493 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5494 if (cr != NULL) 5495 crhold(cr); 5496 else 5497 cr = econnp->conn_cred; 5498 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5499 econnp, cred_t *, cr) 5500 } else { 5501 cr = econnp->conn_cred; 5502 DTRACE_PROBE2(syn_accept, conn_t *, 5503 econnp, cred_t *, cr) 5504 } 5505 5506 if (!tcp_update_label(eager, cr)) { 5507 DTRACE_PROBE3( 5508 tx__ip__log__error__connrequest__tcp, 5509 char *, "eager connp(1) label on SYN mp(2) failed", 5510 conn_t *, econnp, mblk_t *, mp); 5511 goto error3; 5512 } 5513 } 5514 5515 eager->tcp_hard_binding = B_TRUE; 5516 5517 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5518 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5519 5520 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5521 if (err != 0) { 5522 tcp_bind_hash_remove(eager); 5523 goto error3; 5524 } 5525 5526 /* 5527 * No need to check for multicast destination since ip will only pass 5528 * up multicasts to those that have expressed interest 5529 * TODO: what about rejecting broadcasts? 5530 * Also check that source is not a multicast or broadcast address. 5531 */ 5532 eager->tcp_state = TCPS_SYN_RCVD; 5533 5534 5535 /* 5536 * There should be no ire in the mp as we are being called after 5537 * receiving the SYN. 5538 */ 5539 ASSERT(tcp_ire_mp(&mp) == NULL); 5540 5541 /* 5542 * Adapt our mss, ttl, ... according to information provided in IRE. 5543 */ 5544 5545 if (tcp_adapt_ire(eager, NULL) == 0) { 5546 /* Undo the bind_hash_insert */ 5547 tcp_bind_hash_remove(eager); 5548 goto error3; 5549 } 5550 5551 /* Process all TCP options. */ 5552 tcp_process_options(eager, tcph); 5553 5554 /* Is the other end ECN capable? */ 5555 if (tcps->tcps_ecn_permitted >= 1 && 5556 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5557 eager->tcp_ecn_ok = B_TRUE; 5558 } 5559 5560 /* 5561 * listener->tcp_rq->q_hiwat should be the default window size or a 5562 * window size changed via SO_RCVBUF option. First round up the 5563 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5564 * scale option value if needed. Call tcp_rwnd_set() to finish the 5565 * setting. 5566 * 5567 * Note if there is a rpipe metric associated with the remote host, 5568 * we should not inherit receive window size from listener. 5569 */ 5570 eager->tcp_rwnd = MSS_ROUNDUP( 5571 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5572 eager->tcp_rwnd), eager->tcp_mss); 5573 if (eager->tcp_snd_ws_ok) 5574 tcp_set_ws_value(eager); 5575 /* 5576 * Note that this is the only place tcp_rwnd_set() is called for 5577 * accepting a connection. We need to call it here instead of 5578 * after the 3-way handshake because we need to tell the other 5579 * side our rwnd in the SYN-ACK segment. 5580 */ 5581 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5582 5583 /* 5584 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5585 * via soaccept()->soinheritoptions() which essentially applies 5586 * all the listener options to the new STREAM. The options that we 5587 * need to take care of are: 5588 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5589 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5590 * SO_SNDBUF, SO_RCVBUF. 5591 * 5592 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5593 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5594 * tcp_maxpsz_set() gets called later from 5595 * tcp_accept_finish(), the option takes effect. 5596 * 5597 */ 5598 /* Set the TCP options */ 5599 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5600 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5601 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5602 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5603 eager->tcp_oobinline = tcp->tcp_oobinline; 5604 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5605 eager->tcp_broadcast = tcp->tcp_broadcast; 5606 eager->tcp_useloopback = tcp->tcp_useloopback; 5607 eager->tcp_dontroute = tcp->tcp_dontroute; 5608 eager->tcp_debug = tcp->tcp_debug; 5609 eager->tcp_linger = tcp->tcp_linger; 5610 eager->tcp_lingertime = tcp->tcp_lingertime; 5611 if (tcp->tcp_ka_enabled) 5612 eager->tcp_ka_enabled = 1; 5613 5614 /* Set the IP options */ 5615 econnp->conn_broadcast = connp->conn_broadcast; 5616 econnp->conn_loopback = connp->conn_loopback; 5617 econnp->conn_dontroute = connp->conn_dontroute; 5618 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5619 5620 /* Put a ref on the listener for the eager. */ 5621 CONN_INC_REF(connp); 5622 mutex_enter(&tcp->tcp_eager_lock); 5623 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5624 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5625 tcp->tcp_eager_next_q0 = eager; 5626 eager->tcp_eager_prev_q0 = tcp; 5627 5628 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5629 eager->tcp_listener = tcp; 5630 eager->tcp_saved_listener = tcp; 5631 5632 /* 5633 * Tag this detached tcp vector for later retrieval 5634 * by our listener client in tcp_accept(). 5635 */ 5636 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5637 tcp->tcp_conn_req_cnt_q0++; 5638 if (++tcp->tcp_conn_req_seqnum == -1) { 5639 /* 5640 * -1 is "special" and defined in TPI as something 5641 * that should never be used in T_CONN_IND 5642 */ 5643 ++tcp->tcp_conn_req_seqnum; 5644 } 5645 mutex_exit(&tcp->tcp_eager_lock); 5646 5647 if (tcp->tcp_syn_defense) { 5648 /* Don't drop the SYN that comes from a good IP source */ 5649 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5650 if (addr_cache != NULL && eager->tcp_remote == 5651 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5652 eager->tcp_dontdrop = B_TRUE; 5653 } 5654 } 5655 5656 /* 5657 * We need to insert the eager in its own perimeter but as soon 5658 * as we do that, we expose the eager to the classifier and 5659 * should not touch any field outside the eager's perimeter. 5660 * So do all the work necessary before inserting the eager 5661 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5662 * will succeed but undo everything if it fails. 5663 */ 5664 seg_seq = ABE32_TO_U32(tcph->th_seq); 5665 eager->tcp_irs = seg_seq; 5666 eager->tcp_rack = seg_seq; 5667 eager->tcp_rnxt = seg_seq + 1; 5668 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5669 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5670 eager->tcp_state = TCPS_SYN_RCVD; 5671 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5672 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5673 if (mp1 == NULL) { 5674 /* 5675 * Increment the ref count as we are going to 5676 * enqueueing an mp in squeue 5677 */ 5678 CONN_INC_REF(econnp); 5679 goto error; 5680 } 5681 5682 /* 5683 * Note that in theory this should use the current pid 5684 * so that getpeerucred on the client returns the actual listener 5685 * that does accept. But accept() hasn't been called yet. We could use 5686 * the pid of the process that did bind/listen on the server. 5687 * However, with common usage like inetd() the bind/listen can be done 5688 * by a different process than the accept(). 5689 * Hence we do the simple thing of using the open pid here. 5690 * Note that db_credp is set later in tcp_send_data(). 5691 */ 5692 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5693 eager->tcp_cpid = tcp->tcp_cpid; 5694 eager->tcp_open_time = lbolt64; 5695 5696 /* 5697 * We need to start the rto timer. In normal case, we start 5698 * the timer after sending the packet on the wire (or at 5699 * least believing that packet was sent by waiting for 5700 * CALL_IP_WPUT() to return). Since this is the first packet 5701 * being sent on the wire for the eager, our initial tcp_rto 5702 * is at least tcp_rexmit_interval_min which is a fairly 5703 * large value to allow the algorithm to adjust slowly to large 5704 * fluctuations of RTT during first few transmissions. 5705 * 5706 * Starting the timer first and then sending the packet in this 5707 * case shouldn't make much difference since tcp_rexmit_interval_min 5708 * is of the order of several 100ms and starting the timer 5709 * first and then sending the packet will result in difference 5710 * of few micro seconds. 5711 * 5712 * Without this optimization, we are forced to hold the fanout 5713 * lock across the ipcl_bind_insert() and sending the packet 5714 * so that we don't race against an incoming packet (maybe RST) 5715 * for this eager. 5716 * 5717 * It is necessary to acquire an extra reference on the eager 5718 * at this point and hold it until after tcp_send_data() to 5719 * ensure against an eager close race. 5720 */ 5721 5722 CONN_INC_REF(eager->tcp_connp); 5723 5724 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5725 5726 /* 5727 * Insert the eager in its own perimeter now. We are ready to deal 5728 * with any packets on eager. 5729 */ 5730 if (eager->tcp_ipversion == IPV4_VERSION) { 5731 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5732 goto error; 5733 } 5734 } else { 5735 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5736 goto error; 5737 } 5738 } 5739 5740 /* mark conn as fully-bound */ 5741 econnp->conn_fully_bound = B_TRUE; 5742 5743 /* Send the SYN-ACK */ 5744 tcp_send_data(eager, eager->tcp_wq, mp1); 5745 CONN_DEC_REF(eager->tcp_connp); 5746 freemsg(mp); 5747 5748 return; 5749 error: 5750 freemsg(mp1); 5751 eager->tcp_closemp_used = B_TRUE; 5752 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5753 mp1 = &eager->tcp_closemp; 5754 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5755 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5756 5757 /* 5758 * If a connection already exists, send the mp to that connections so 5759 * that it can be appropriately dealt with. 5760 */ 5761 ipst = tcps->tcps_netstack->netstack_ip; 5762 5763 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5764 if (!IPCL_IS_CONNECTED(econnp)) { 5765 /* 5766 * Something bad happened. ipcl_conn_insert() 5767 * failed because a connection already existed 5768 * in connected hash but we can't find it 5769 * anymore (someone blew it away). Just 5770 * free this message and hopefully remote 5771 * will retransmit at which time the SYN can be 5772 * treated as a new connection or dealth with 5773 * a TH_RST if a connection already exists. 5774 */ 5775 CONN_DEC_REF(econnp); 5776 freemsg(mp); 5777 } else { 5778 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5779 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5780 } 5781 } else { 5782 /* Nobody wants this packet */ 5783 freemsg(mp); 5784 } 5785 return; 5786 error3: 5787 CONN_DEC_REF(econnp); 5788 error2: 5789 freemsg(mp); 5790 } 5791 5792 /* 5793 * In an ideal case of vertical partition in NUMA architecture, its 5794 * beneficial to have the listener and all the incoming connections 5795 * tied to the same squeue. The other constraint is that incoming 5796 * connections should be tied to the squeue attached to interrupted 5797 * CPU for obvious locality reason so this leaves the listener to 5798 * be tied to the same squeue. Our only problem is that when listener 5799 * is binding, the CPU that will get interrupted by the NIC whose 5800 * IP address the listener is binding to is not even known. So 5801 * the code below allows us to change that binding at the time the 5802 * CPU is interrupted by virtue of incoming connection's squeue. 5803 * 5804 * This is usefull only in case of a listener bound to a specific IP 5805 * address. For other kind of listeners, they get bound the 5806 * very first time and there is no attempt to rebind them. 5807 */ 5808 void 5809 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5810 { 5811 conn_t *connp = (conn_t *)arg; 5812 squeue_t *sqp = (squeue_t *)arg2; 5813 squeue_t *new_sqp; 5814 uint32_t conn_flags; 5815 5816 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5817 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5818 } else { 5819 goto done; 5820 } 5821 5822 if (connp->conn_fanout == NULL) 5823 goto done; 5824 5825 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5826 mutex_enter(&connp->conn_fanout->connf_lock); 5827 mutex_enter(&connp->conn_lock); 5828 /* 5829 * No one from read or write side can access us now 5830 * except for already queued packets on this squeue. 5831 * But since we haven't changed the squeue yet, they 5832 * can't execute. If they are processed after we have 5833 * changed the squeue, they are sent back to the 5834 * correct squeue down below. 5835 * But a listner close can race with processing of 5836 * incoming SYN. If incoming SYN processing changes 5837 * the squeue then the listener close which is waiting 5838 * to enter the squeue would operate on the wrong 5839 * squeue. Hence we don't change the squeue here unless 5840 * the refcount is exactly the minimum refcount. The 5841 * minimum refcount of 4 is counted as - 1 each for 5842 * TCP and IP, 1 for being in the classifier hash, and 5843 * 1 for the mblk being processed. 5844 */ 5845 5846 if (connp->conn_ref != 4 || 5847 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5848 mutex_exit(&connp->conn_lock); 5849 mutex_exit(&connp->conn_fanout->connf_lock); 5850 goto done; 5851 } 5852 if (connp->conn_sqp != new_sqp) { 5853 while (connp->conn_sqp != new_sqp) 5854 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5855 } 5856 5857 do { 5858 conn_flags = connp->conn_flags; 5859 conn_flags |= IPCL_FULLY_BOUND; 5860 (void) cas32(&connp->conn_flags, connp->conn_flags, 5861 conn_flags); 5862 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5863 5864 mutex_exit(&connp->conn_fanout->connf_lock); 5865 mutex_exit(&connp->conn_lock); 5866 } 5867 5868 done: 5869 if (connp->conn_sqp != sqp) { 5870 CONN_INC_REF(connp); 5871 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5872 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5873 } else { 5874 tcp_conn_request(connp, mp, sqp); 5875 } 5876 } 5877 5878 /* 5879 * Successful connect request processing begins when our client passes 5880 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5881 * our T_OK_ACK reply message upstream. The control flow looks like this: 5882 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5883 * upstream <- tcp_rput() <- IP 5884 * After various error checks are completed, tcp_tpi_connect() lays 5885 * the target address and port into the composite header template, 5886 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5887 * request followed by an IRE request, and passes the three mblk message 5888 * down to IP looking like this: 5889 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5890 * Processing continues in tcp_rput() when we receive the following message: 5891 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5892 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5893 * to fire off the connection request, and then passes the T_OK_ACK mblk 5894 * upstream that we filled in below. There are, of course, numerous 5895 * error conditions along the way which truncate the processing described 5896 * above. 5897 */ 5898 static void 5899 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5900 { 5901 sin_t *sin; 5902 queue_t *q = tcp->tcp_wq; 5903 struct T_conn_req *tcr; 5904 struct sockaddr *sa; 5905 socklen_t len; 5906 int error; 5907 cred_t *cr; 5908 pid_t cpid; 5909 5910 /* 5911 * All Solaris components should pass a db_credp 5912 * for this TPI message, hence we ASSERT. 5913 * But in case there is some other M_PROTO that looks 5914 * like a TPI message sent by some other kernel 5915 * component, we check and return an error. 5916 */ 5917 cr = msg_getcred(mp, &cpid); 5918 ASSERT(cr != NULL); 5919 if (cr == NULL) { 5920 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5921 return; 5922 } 5923 5924 tcr = (struct T_conn_req *)mp->b_rptr; 5925 5926 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5927 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5928 tcp_err_ack(tcp, mp, TPROTO, 0); 5929 return; 5930 } 5931 5932 /* 5933 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5934 * will always have that to send up. Otherwise, we need to do 5935 * special handling in case the allocation fails at that time. 5936 * If the end point is TPI, the tcp_t can be reused and the 5937 * tcp_ordrel_mp may be allocated already. 5938 */ 5939 if (tcp->tcp_ordrel_mp == NULL) { 5940 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5941 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5942 return; 5943 } 5944 } 5945 5946 /* 5947 * Determine packet type based on type of address passed in 5948 * the request should contain an IPv4 or IPv6 address. 5949 * Make sure that address family matches the type of 5950 * family of the the address passed down 5951 */ 5952 switch (tcr->DEST_length) { 5953 default: 5954 tcp_err_ack(tcp, mp, TBADADDR, 0); 5955 return; 5956 5957 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5958 /* 5959 * XXX: The check for valid DEST_length was not there 5960 * in earlier releases and some buggy 5961 * TLI apps (e.g Sybase) got away with not feeding 5962 * in sin_zero part of address. 5963 * We allow that bug to keep those buggy apps humming. 5964 * Test suites require the check on DEST_length. 5965 * We construct a new mblk with valid DEST_length 5966 * free the original so the rest of the code does 5967 * not have to keep track of this special shorter 5968 * length address case. 5969 */ 5970 mblk_t *nmp; 5971 struct T_conn_req *ntcr; 5972 sin_t *nsin; 5973 5974 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5975 tcr->OPT_length, BPRI_HI); 5976 if (nmp == NULL) { 5977 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5978 return; 5979 } 5980 ntcr = (struct T_conn_req *)nmp->b_rptr; 5981 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5982 ntcr->PRIM_type = T_CONN_REQ; 5983 ntcr->DEST_length = sizeof (sin_t); 5984 ntcr->DEST_offset = sizeof (struct T_conn_req); 5985 5986 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5987 *nsin = sin_null; 5988 /* Get pointer to shorter address to copy from original mp */ 5989 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5990 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5991 if (sin == NULL || !OK_32PTR((char *)sin)) { 5992 freemsg(nmp); 5993 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5994 return; 5995 } 5996 nsin->sin_family = sin->sin_family; 5997 nsin->sin_port = sin->sin_port; 5998 nsin->sin_addr = sin->sin_addr; 5999 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6000 nmp->b_wptr = (uchar_t *)&nsin[1]; 6001 if (tcr->OPT_length != 0) { 6002 ntcr->OPT_length = tcr->OPT_length; 6003 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6004 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6005 (uchar_t *)ntcr + ntcr->OPT_offset, 6006 tcr->OPT_length); 6007 nmp->b_wptr += tcr->OPT_length; 6008 } 6009 freemsg(mp); /* original mp freed */ 6010 mp = nmp; /* re-initialize original variables */ 6011 tcr = ntcr; 6012 } 6013 /* FALLTHRU */ 6014 6015 case sizeof (sin_t): 6016 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6017 sizeof (sin_t)); 6018 len = sizeof (sin_t); 6019 break; 6020 6021 case sizeof (sin6_t): 6022 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6023 sizeof (sin6_t)); 6024 len = sizeof (sin6_t); 6025 break; 6026 } 6027 6028 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6029 if (error != 0) { 6030 tcp_err_ack(tcp, mp, TSYSERR, error); 6031 return; 6032 } 6033 6034 /* 6035 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6036 * should key on their sequence number and cut them loose. 6037 */ 6038 6039 /* 6040 * If options passed in, feed it for verification and handling 6041 */ 6042 if (tcr->OPT_length != 0) { 6043 mblk_t *ok_mp; 6044 mblk_t *discon_mp; 6045 mblk_t *conn_opts_mp; 6046 int t_error, sys_error, do_disconnect; 6047 6048 conn_opts_mp = NULL; 6049 6050 if (tcp_conprim_opt_process(tcp, mp, 6051 &do_disconnect, &t_error, &sys_error) < 0) { 6052 if (do_disconnect) { 6053 ASSERT(t_error == 0 && sys_error == 0); 6054 discon_mp = mi_tpi_discon_ind(NULL, 6055 ECONNREFUSED, 0); 6056 if (!discon_mp) { 6057 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6058 TSYSERR, ENOMEM); 6059 return; 6060 } 6061 ok_mp = mi_tpi_ok_ack_alloc(mp); 6062 if (!ok_mp) { 6063 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6064 TSYSERR, ENOMEM); 6065 return; 6066 } 6067 qreply(q, ok_mp); 6068 qreply(q, discon_mp); /* no flush! */ 6069 } else { 6070 ASSERT(t_error != 0); 6071 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6072 sys_error); 6073 } 6074 return; 6075 } 6076 /* 6077 * Success in setting options, the mp option buffer represented 6078 * by OPT_length/offset has been potentially modified and 6079 * contains results of option processing. We copy it in 6080 * another mp to save it for potentially influencing returning 6081 * it in T_CONN_CONN. 6082 */ 6083 if (tcr->OPT_length != 0) { /* there are resulting options */ 6084 conn_opts_mp = copyb(mp); 6085 if (!conn_opts_mp) { 6086 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6087 TSYSERR, ENOMEM); 6088 return; 6089 } 6090 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6091 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6092 /* 6093 * Note: 6094 * These resulting option negotiation can include any 6095 * end-to-end negotiation options but there no such 6096 * thing (yet?) in our TCP/IP. 6097 */ 6098 } 6099 } 6100 6101 /* call the non-TPI version */ 6102 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6103 if (error < 0) { 6104 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6105 } else if (error > 0) { 6106 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6107 } else { 6108 mp = mi_tpi_ok_ack_alloc(mp); 6109 } 6110 6111 /* 6112 * Note: Code below is the "failure" case 6113 */ 6114 /* return error ack and blow away saved option results if any */ 6115 connect_failed: 6116 if (mp != NULL) 6117 putnext(tcp->tcp_rq, mp); 6118 else { 6119 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6120 TSYSERR, ENOMEM); 6121 } 6122 } 6123 6124 /* 6125 * Handle connect to IPv4 destinations, including connections for AF_INET6 6126 * sockets connecting to IPv4 mapped IPv6 destinations. 6127 */ 6128 static int 6129 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6130 uint_t srcid, cred_t *cr, pid_t pid) 6131 { 6132 tcph_t *tcph; 6133 mblk_t *mp; 6134 ipaddr_t dstaddr = *dstaddrp; 6135 int32_t oldstate; 6136 uint16_t lport; 6137 int error = 0; 6138 tcp_stack_t *tcps = tcp->tcp_tcps; 6139 6140 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6141 6142 /* Check for attempt to connect to INADDR_ANY */ 6143 if (dstaddr == INADDR_ANY) { 6144 /* 6145 * SunOS 4.x and 4.3 BSD allow an application 6146 * to connect a TCP socket to INADDR_ANY. 6147 * When they do this, the kernel picks the 6148 * address of one interface and uses it 6149 * instead. The kernel usually ends up 6150 * picking the address of the loopback 6151 * interface. This is an undocumented feature. 6152 * However, we provide the same thing here 6153 * in order to have source and binary 6154 * compatibility with SunOS 4.x. 6155 * Update the T_CONN_REQ (sin/sin6) since it is used to 6156 * generate the T_CONN_CON. 6157 */ 6158 dstaddr = htonl(INADDR_LOOPBACK); 6159 *dstaddrp = dstaddr; 6160 } 6161 6162 /* Handle __sin6_src_id if socket not bound to an IP address */ 6163 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6164 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6165 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6166 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6167 tcp->tcp_ipha->ipha_src); 6168 } 6169 6170 /* 6171 * Don't let an endpoint connect to itself. Note that 6172 * the test here does not catch the case where the 6173 * source IP addr was left unspecified by the user. In 6174 * this case, the source addr is set in tcp_adapt_ire() 6175 * using the reply to the T_BIND message that we send 6176 * down to IP here and the check is repeated in tcp_rput_other. 6177 */ 6178 if (dstaddr == tcp->tcp_ipha->ipha_src && 6179 dstport == tcp->tcp_lport) { 6180 error = -TBADADDR; 6181 goto failed; 6182 } 6183 6184 tcp->tcp_ipha->ipha_dst = dstaddr; 6185 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6186 6187 /* 6188 * Massage a source route if any putting the first hop 6189 * in iph_dst. Compute a starting value for the checksum which 6190 * takes into account that the original iph_dst should be 6191 * included in the checksum but that ip will include the 6192 * first hop in the source route in the tcp checksum. 6193 */ 6194 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6195 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6196 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6197 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6198 if ((int)tcp->tcp_sum < 0) 6199 tcp->tcp_sum--; 6200 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6201 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6202 (tcp->tcp_sum >> 16)); 6203 tcph = tcp->tcp_tcph; 6204 *(uint16_t *)tcph->th_fport = dstport; 6205 tcp->tcp_fport = dstport; 6206 6207 oldstate = tcp->tcp_state; 6208 /* 6209 * At this point the remote destination address and remote port fields 6210 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6211 * have to see which state tcp was in so we can take apropriate action. 6212 */ 6213 if (oldstate == TCPS_IDLE) { 6214 /* 6215 * We support a quick connect capability here, allowing 6216 * clients to transition directly from IDLE to SYN_SENT 6217 * tcp_bindi will pick an unused port, insert the connection 6218 * in the bind hash and transition to BOUND state. 6219 */ 6220 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6221 tcp, B_TRUE); 6222 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6223 B_FALSE, B_FALSE); 6224 if (lport == 0) { 6225 error = -TNOADDR; 6226 goto failed; 6227 } 6228 } 6229 tcp->tcp_state = TCPS_SYN_SENT; 6230 6231 mp = allocb(sizeof (ire_t), BPRI_HI); 6232 if (mp == NULL) { 6233 tcp->tcp_state = oldstate; 6234 error = ENOMEM; 6235 goto failed; 6236 } 6237 6238 mp->b_wptr += sizeof (ire_t); 6239 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6240 tcp->tcp_hard_binding = 1; 6241 6242 /* 6243 * We need to make sure that the conn_recv is set to a non-null 6244 * value before we insert the conn_t into the classifier table. 6245 * This is to avoid a race with an incoming packet which does 6246 * an ipcl_classify(). 6247 */ 6248 tcp->tcp_connp->conn_recv = tcp_input; 6249 6250 if (tcp->tcp_family == AF_INET) { 6251 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6252 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6253 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6254 } else { 6255 in6_addr_t v6src; 6256 if (tcp->tcp_ipversion == IPV4_VERSION) { 6257 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6258 } else { 6259 v6src = tcp->tcp_ip6h->ip6_src; 6260 } 6261 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6262 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6263 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6264 } 6265 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6266 tcp->tcp_active_open = 1; 6267 6268 6269 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6270 failed: 6271 /* return error ack and blow away saved option results if any */ 6272 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6273 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6274 return (error); 6275 } 6276 6277 /* 6278 * Handle connect to IPv6 destinations. 6279 */ 6280 static int 6281 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6282 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6283 { 6284 tcph_t *tcph; 6285 mblk_t *mp; 6286 ip6_rthdr_t *rth; 6287 int32_t oldstate; 6288 uint16_t lport; 6289 tcp_stack_t *tcps = tcp->tcp_tcps; 6290 int error = 0; 6291 conn_t *connp = tcp->tcp_connp; 6292 6293 ASSERT(tcp->tcp_family == AF_INET6); 6294 6295 /* 6296 * If we're here, it means that the destination address is a native 6297 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6298 * reason why it might not be IPv6 is if the socket was bound to an 6299 * IPv4-mapped IPv6 address. 6300 */ 6301 if (tcp->tcp_ipversion != IPV6_VERSION) { 6302 return (-TBADADDR); 6303 } 6304 6305 /* 6306 * Interpret a zero destination to mean loopback. 6307 * Update the T_CONN_REQ (sin/sin6) since it is used to 6308 * generate the T_CONN_CON. 6309 */ 6310 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6311 *dstaddrp = ipv6_loopback; 6312 } 6313 6314 /* Handle __sin6_src_id if socket not bound to an IP address */ 6315 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6316 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6317 connp->conn_zoneid, tcps->tcps_netstack); 6318 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6319 } 6320 6321 /* 6322 * Take care of the scope_id now and add ip6i_t 6323 * if ip6i_t is not already allocated through TCP 6324 * sticky options. At this point tcp_ip6h does not 6325 * have dst info, thus use dstaddrp. 6326 */ 6327 if (scope_id != 0 && 6328 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6329 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6330 ip6i_t *ip6i; 6331 6332 ipp->ipp_ifindex = scope_id; 6333 ip6i = (ip6i_t *)tcp->tcp_iphc; 6334 6335 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6336 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6337 /* Already allocated */ 6338 ip6i->ip6i_flags |= IP6I_IFINDEX; 6339 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6340 ipp->ipp_fields |= IPPF_SCOPE_ID; 6341 } else { 6342 int reterr; 6343 6344 ipp->ipp_fields |= IPPF_SCOPE_ID; 6345 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6346 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6347 reterr = tcp_build_hdrs(tcp); 6348 if (reterr != 0) 6349 goto failed; 6350 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6351 } 6352 } 6353 6354 /* 6355 * Don't let an endpoint connect to itself. Note that 6356 * the test here does not catch the case where the 6357 * source IP addr was left unspecified by the user. In 6358 * this case, the source addr is set in tcp_adapt_ire() 6359 * using the reply to the T_BIND message that we send 6360 * down to IP here and the check is repeated in tcp_rput_other. 6361 */ 6362 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6363 (dstport == tcp->tcp_lport)) { 6364 error = -TBADADDR; 6365 goto failed; 6366 } 6367 6368 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6369 tcp->tcp_remote_v6 = *dstaddrp; 6370 tcp->tcp_ip6h->ip6_vcf = 6371 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6372 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6373 6374 /* 6375 * Massage a routing header (if present) putting the first hop 6376 * in ip6_dst. Compute a starting value for the checksum which 6377 * takes into account that the original ip6_dst should be 6378 * included in the checksum but that ip will include the 6379 * first hop in the source route in the tcp checksum. 6380 */ 6381 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6382 if (rth != NULL) { 6383 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6384 tcps->tcps_netstack); 6385 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6386 (tcp->tcp_sum >> 16)); 6387 } else { 6388 tcp->tcp_sum = 0; 6389 } 6390 6391 tcph = tcp->tcp_tcph; 6392 *(uint16_t *)tcph->th_fport = dstport; 6393 tcp->tcp_fport = dstport; 6394 6395 oldstate = tcp->tcp_state; 6396 /* 6397 * At this point the remote destination address and remote port fields 6398 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6399 * have to see which state tcp was in so we can take apropriate action. 6400 */ 6401 if (oldstate == TCPS_IDLE) { 6402 /* 6403 * We support a quick connect capability here, allowing 6404 * clients to transition directly from IDLE to SYN_SENT 6405 * tcp_bindi will pick an unused port, insert the connection 6406 * in the bind hash and transition to BOUND state. 6407 */ 6408 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6409 tcp, B_TRUE); 6410 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6411 B_FALSE, B_FALSE); 6412 if (lport == 0) { 6413 error = -TNOADDR; 6414 goto failed; 6415 } 6416 } 6417 tcp->tcp_state = TCPS_SYN_SENT; 6418 6419 mp = allocb(sizeof (ire_t), BPRI_HI); 6420 if (mp != NULL) { 6421 in6_addr_t v6src; 6422 6423 mp->b_wptr += sizeof (ire_t); 6424 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6425 6426 tcp->tcp_hard_binding = 1; 6427 6428 /* 6429 * We need to make sure that the conn_recv is set to a non-null 6430 * value before we insert the conn_t into the classifier table. 6431 * This is to avoid a race with an incoming packet which does 6432 * an ipcl_classify(). 6433 */ 6434 tcp->tcp_connp->conn_recv = tcp_input; 6435 6436 if (tcp->tcp_ipversion == IPV4_VERSION) { 6437 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6438 } else { 6439 v6src = tcp->tcp_ip6h->ip6_src; 6440 } 6441 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6442 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6443 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6444 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6445 tcp->tcp_active_open = 1; 6446 6447 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6448 } 6449 /* Error case */ 6450 tcp->tcp_state = oldstate; 6451 error = ENOMEM; 6452 6453 failed: 6454 /* return error ack and blow away saved option results if any */ 6455 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6456 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6457 return (error); 6458 } 6459 6460 /* 6461 * We need a stream q for detached closing tcp connections 6462 * to use. Our client hereby indicates that this q is the 6463 * one to use. 6464 */ 6465 static void 6466 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6467 { 6468 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6469 queue_t *q = tcp->tcp_wq; 6470 tcp_stack_t *tcps = tcp->tcp_tcps; 6471 6472 #ifdef NS_DEBUG 6473 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6474 tcps->tcps_netstack->netstack_stackid); 6475 #endif 6476 mp->b_datap->db_type = M_IOCACK; 6477 iocp->ioc_count = 0; 6478 mutex_enter(&tcps->tcps_g_q_lock); 6479 if (tcps->tcps_g_q != NULL) { 6480 mutex_exit(&tcps->tcps_g_q_lock); 6481 iocp->ioc_error = EALREADY; 6482 } else { 6483 int error = 0; 6484 conn_t *connp = tcp->tcp_connp; 6485 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6486 6487 tcps->tcps_g_q = tcp->tcp_rq; 6488 mutex_exit(&tcps->tcps_g_q_lock); 6489 iocp->ioc_error = 0; 6490 iocp->ioc_rval = 0; 6491 /* 6492 * We are passing tcp_sticky_ipp as NULL 6493 * as it is not useful for tcp_default queue 6494 * 6495 * Set conn_recv just in case. 6496 */ 6497 tcp->tcp_connp->conn_recv = tcp_conn_request; 6498 6499 ASSERT(connp->conn_af_isv6); 6500 connp->conn_ulp = IPPROTO_TCP; 6501 6502 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6503 NULL || connp->conn_mac_exempt) { 6504 error = -TBADADDR; 6505 } else { 6506 connp->conn_srcv6 = ipv6_all_zeros; 6507 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6508 } 6509 6510 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6511 } 6512 qreply(q, mp); 6513 } 6514 6515 static int 6516 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6517 { 6518 tcp_t *ltcp = NULL; 6519 conn_t *connp; 6520 tcp_stack_t *tcps = tcp->tcp_tcps; 6521 6522 /* 6523 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6524 * when the stream is in BOUND state. Do not send a reset, 6525 * since the destination IP address is not valid, and it can 6526 * be the initialized value of all zeros (broadcast address). 6527 * 6528 * XXX There won't be any pending bind request to IP. 6529 */ 6530 if (tcp->tcp_state <= TCPS_BOUND) { 6531 if (tcp->tcp_debug) { 6532 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6533 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6534 } 6535 return (TOUTSTATE); 6536 } 6537 6538 6539 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6540 6541 /* 6542 * According to TPI, for non-listeners, ignore seqnum 6543 * and disconnect. 6544 * Following interpretation of -1 seqnum is historical 6545 * and implied TPI ? (TPI only states that for T_CONN_IND, 6546 * a valid seqnum should not be -1). 6547 * 6548 * -1 means disconnect everything 6549 * regardless even on a listener. 6550 */ 6551 6552 int old_state = tcp->tcp_state; 6553 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6554 6555 /* 6556 * The connection can't be on the tcp_time_wait_head list 6557 * since it is not detached. 6558 */ 6559 ASSERT(tcp->tcp_time_wait_next == NULL); 6560 ASSERT(tcp->tcp_time_wait_prev == NULL); 6561 ASSERT(tcp->tcp_time_wait_expire == 0); 6562 ltcp = NULL; 6563 /* 6564 * If it used to be a listener, check to make sure no one else 6565 * has taken the port before switching back to LISTEN state. 6566 */ 6567 if (tcp->tcp_ipversion == IPV4_VERSION) { 6568 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6569 tcp->tcp_ipha->ipha_src, 6570 tcp->tcp_connp->conn_zoneid, ipst); 6571 if (connp != NULL) 6572 ltcp = connp->conn_tcp; 6573 } else { 6574 /* Allow tcp_bound_if listeners? */ 6575 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6576 &tcp->tcp_ip6h->ip6_src, 0, 6577 tcp->tcp_connp->conn_zoneid, ipst); 6578 if (connp != NULL) 6579 ltcp = connp->conn_tcp; 6580 } 6581 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6582 tcp->tcp_state = TCPS_LISTEN; 6583 } else if (old_state > TCPS_BOUND) { 6584 tcp->tcp_conn_req_max = 0; 6585 tcp->tcp_state = TCPS_BOUND; 6586 } 6587 if (ltcp != NULL) 6588 CONN_DEC_REF(ltcp->tcp_connp); 6589 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6590 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6591 } else if (old_state == TCPS_ESTABLISHED || 6592 old_state == TCPS_CLOSE_WAIT) { 6593 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6594 } 6595 6596 if (tcp->tcp_fused) 6597 tcp_unfuse(tcp); 6598 6599 mutex_enter(&tcp->tcp_eager_lock); 6600 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6601 (tcp->tcp_conn_req_cnt_q != 0)) { 6602 tcp_eager_cleanup(tcp, 0); 6603 } 6604 mutex_exit(&tcp->tcp_eager_lock); 6605 6606 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6607 tcp->tcp_rnxt, TH_RST | TH_ACK); 6608 6609 tcp_reinit(tcp); 6610 6611 return (0); 6612 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6613 return (TBADSEQ); 6614 } 6615 return (0); 6616 } 6617 6618 /* 6619 * Our client hereby directs us to reject the connection request 6620 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6621 * of sending the appropriate RST, not an ICMP error. 6622 */ 6623 static void 6624 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6625 { 6626 t_scalar_t seqnum; 6627 int error; 6628 6629 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6630 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6631 tcp_err_ack(tcp, mp, TPROTO, 0); 6632 return; 6633 } 6634 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6635 error = tcp_disconnect_common(tcp, seqnum); 6636 if (error != 0) 6637 tcp_err_ack(tcp, mp, error, 0); 6638 else { 6639 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6640 /* Send M_FLUSH according to TPI */ 6641 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6642 } 6643 mp = mi_tpi_ok_ack_alloc(mp); 6644 if (mp) 6645 putnext(tcp->tcp_rq, mp); 6646 } 6647 } 6648 6649 /* 6650 * Diagnostic routine used to return a string associated with the tcp state. 6651 * Note that if the caller does not supply a buffer, it will use an internal 6652 * static string. This means that if multiple threads call this function at 6653 * the same time, output can be corrupted... Note also that this function 6654 * does not check the size of the supplied buffer. The caller has to make 6655 * sure that it is big enough. 6656 */ 6657 static char * 6658 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6659 { 6660 char buf1[30]; 6661 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6662 char *buf; 6663 char *cp; 6664 in6_addr_t local, remote; 6665 char local_addrbuf[INET6_ADDRSTRLEN]; 6666 char remote_addrbuf[INET6_ADDRSTRLEN]; 6667 6668 if (sup_buf != NULL) 6669 buf = sup_buf; 6670 else 6671 buf = priv_buf; 6672 6673 if (tcp == NULL) 6674 return ("NULL_TCP"); 6675 switch (tcp->tcp_state) { 6676 case TCPS_CLOSED: 6677 cp = "TCP_CLOSED"; 6678 break; 6679 case TCPS_IDLE: 6680 cp = "TCP_IDLE"; 6681 break; 6682 case TCPS_BOUND: 6683 cp = "TCP_BOUND"; 6684 break; 6685 case TCPS_LISTEN: 6686 cp = "TCP_LISTEN"; 6687 break; 6688 case TCPS_SYN_SENT: 6689 cp = "TCP_SYN_SENT"; 6690 break; 6691 case TCPS_SYN_RCVD: 6692 cp = "TCP_SYN_RCVD"; 6693 break; 6694 case TCPS_ESTABLISHED: 6695 cp = "TCP_ESTABLISHED"; 6696 break; 6697 case TCPS_CLOSE_WAIT: 6698 cp = "TCP_CLOSE_WAIT"; 6699 break; 6700 case TCPS_FIN_WAIT_1: 6701 cp = "TCP_FIN_WAIT_1"; 6702 break; 6703 case TCPS_CLOSING: 6704 cp = "TCP_CLOSING"; 6705 break; 6706 case TCPS_LAST_ACK: 6707 cp = "TCP_LAST_ACK"; 6708 break; 6709 case TCPS_FIN_WAIT_2: 6710 cp = "TCP_FIN_WAIT_2"; 6711 break; 6712 case TCPS_TIME_WAIT: 6713 cp = "TCP_TIME_WAIT"; 6714 break; 6715 default: 6716 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6717 cp = buf1; 6718 break; 6719 } 6720 switch (format) { 6721 case DISP_ADDR_AND_PORT: 6722 if (tcp->tcp_ipversion == IPV4_VERSION) { 6723 /* 6724 * Note that we use the remote address in the tcp_b 6725 * structure. This means that it will print out 6726 * the real destination address, not the next hop's 6727 * address if source routing is used. 6728 */ 6729 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6730 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6731 6732 } else { 6733 local = tcp->tcp_ip_src_v6; 6734 remote = tcp->tcp_remote_v6; 6735 } 6736 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6737 sizeof (local_addrbuf)); 6738 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6739 sizeof (remote_addrbuf)); 6740 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6741 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6742 ntohs(tcp->tcp_fport), cp); 6743 break; 6744 case DISP_PORT_ONLY: 6745 default: 6746 (void) mi_sprintf(buf, "[%u, %u] %s", 6747 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6748 break; 6749 } 6750 6751 return (buf); 6752 } 6753 6754 /* 6755 * Called via squeue to get on to eager's perimeter. It sends a 6756 * TH_RST if eager is in the fanout table. The listener wants the 6757 * eager to disappear either by means of tcp_eager_blowoff() or 6758 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6759 * called (via squeue) if the eager cannot be inserted in the 6760 * fanout table in tcp_conn_request(). 6761 */ 6762 /* ARGSUSED */ 6763 void 6764 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6765 { 6766 conn_t *econnp = (conn_t *)arg; 6767 tcp_t *eager = econnp->conn_tcp; 6768 tcp_t *listener = eager->tcp_listener; 6769 tcp_stack_t *tcps = eager->tcp_tcps; 6770 6771 /* 6772 * We could be called because listener is closing. Since 6773 * the eager is using listener's queue's, its not safe. 6774 * Better use the default queue just to send the TH_RST 6775 * out. 6776 */ 6777 ASSERT(tcps->tcps_g_q != NULL); 6778 eager->tcp_rq = tcps->tcps_g_q; 6779 eager->tcp_wq = WR(tcps->tcps_g_q); 6780 6781 /* 6782 * An eager's conn_fanout will be NULL if it's a duplicate 6783 * for an existing 4-tuples in the conn fanout table. 6784 * We don't want to send an RST out in such case. 6785 */ 6786 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6787 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6788 eager, eager->tcp_snxt, 0, TH_RST); 6789 } 6790 6791 /* We are here because listener wants this eager gone */ 6792 if (listener != NULL) { 6793 mutex_enter(&listener->tcp_eager_lock); 6794 tcp_eager_unlink(eager); 6795 if (eager->tcp_tconnind_started) { 6796 /* 6797 * The eager has sent a conn_ind up to the 6798 * listener but listener decides to close 6799 * instead. We need to drop the extra ref 6800 * placed on eager in tcp_rput_data() before 6801 * sending the conn_ind to listener. 6802 */ 6803 CONN_DEC_REF(econnp); 6804 } 6805 mutex_exit(&listener->tcp_eager_lock); 6806 CONN_DEC_REF(listener->tcp_connp); 6807 } 6808 6809 if (eager->tcp_state > TCPS_BOUND) 6810 tcp_close_detached(eager); 6811 } 6812 6813 /* 6814 * Reset any eager connection hanging off this listener marked 6815 * with 'seqnum' and then reclaim it's resources. 6816 */ 6817 static boolean_t 6818 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6819 { 6820 tcp_t *eager; 6821 mblk_t *mp; 6822 tcp_stack_t *tcps = listener->tcp_tcps; 6823 6824 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6825 eager = listener; 6826 mutex_enter(&listener->tcp_eager_lock); 6827 do { 6828 eager = eager->tcp_eager_next_q; 6829 if (eager == NULL) { 6830 mutex_exit(&listener->tcp_eager_lock); 6831 return (B_FALSE); 6832 } 6833 } while (eager->tcp_conn_req_seqnum != seqnum); 6834 6835 if (eager->tcp_closemp_used) { 6836 mutex_exit(&listener->tcp_eager_lock); 6837 return (B_TRUE); 6838 } 6839 eager->tcp_closemp_used = B_TRUE; 6840 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6841 CONN_INC_REF(eager->tcp_connp); 6842 mutex_exit(&listener->tcp_eager_lock); 6843 mp = &eager->tcp_closemp; 6844 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6845 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6846 return (B_TRUE); 6847 } 6848 6849 /* 6850 * Reset any eager connection hanging off this listener 6851 * and then reclaim it's resources. 6852 */ 6853 static void 6854 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6855 { 6856 tcp_t *eager; 6857 mblk_t *mp; 6858 tcp_stack_t *tcps = listener->tcp_tcps; 6859 6860 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6861 6862 if (!q0_only) { 6863 /* First cleanup q */ 6864 TCP_STAT(tcps, tcp_eager_blowoff_q); 6865 eager = listener->tcp_eager_next_q; 6866 while (eager != NULL) { 6867 if (!eager->tcp_closemp_used) { 6868 eager->tcp_closemp_used = B_TRUE; 6869 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6870 CONN_INC_REF(eager->tcp_connp); 6871 mp = &eager->tcp_closemp; 6872 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6873 tcp_eager_kill, eager->tcp_connp, 6874 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6875 } 6876 eager = eager->tcp_eager_next_q; 6877 } 6878 } 6879 /* Then cleanup q0 */ 6880 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6881 eager = listener->tcp_eager_next_q0; 6882 while (eager != listener) { 6883 if (!eager->tcp_closemp_used) { 6884 eager->tcp_closemp_used = B_TRUE; 6885 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6886 CONN_INC_REF(eager->tcp_connp); 6887 mp = &eager->tcp_closemp; 6888 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6889 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6890 SQTAG_TCP_EAGER_CLEANUP_Q0); 6891 } 6892 eager = eager->tcp_eager_next_q0; 6893 } 6894 } 6895 6896 /* 6897 * If we are an eager connection hanging off a listener that hasn't 6898 * formally accepted the connection yet, get off his list and blow off 6899 * any data that we have accumulated. 6900 */ 6901 static void 6902 tcp_eager_unlink(tcp_t *tcp) 6903 { 6904 tcp_t *listener = tcp->tcp_listener; 6905 6906 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6907 ASSERT(listener != NULL); 6908 if (tcp->tcp_eager_next_q0 != NULL) { 6909 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6910 6911 /* Remove the eager tcp from q0 */ 6912 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6913 tcp->tcp_eager_prev_q0; 6914 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6915 tcp->tcp_eager_next_q0; 6916 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6917 listener->tcp_conn_req_cnt_q0--; 6918 6919 tcp->tcp_eager_next_q0 = NULL; 6920 tcp->tcp_eager_prev_q0 = NULL; 6921 6922 /* 6923 * Take the eager out, if it is in the list of droppable 6924 * eagers. 6925 */ 6926 MAKE_UNDROPPABLE(tcp); 6927 6928 if (tcp->tcp_syn_rcvd_timeout != 0) { 6929 /* we have timed out before */ 6930 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6931 listener->tcp_syn_rcvd_timeout--; 6932 } 6933 } else { 6934 tcp_t **tcpp = &listener->tcp_eager_next_q; 6935 tcp_t *prev = NULL; 6936 6937 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6938 if (tcpp[0] == tcp) { 6939 if (listener->tcp_eager_last_q == tcp) { 6940 /* 6941 * If we are unlinking the last 6942 * element on the list, adjust 6943 * tail pointer. Set tail pointer 6944 * to nil when list is empty. 6945 */ 6946 ASSERT(tcp->tcp_eager_next_q == NULL); 6947 if (listener->tcp_eager_last_q == 6948 listener->tcp_eager_next_q) { 6949 listener->tcp_eager_last_q = 6950 NULL; 6951 } else { 6952 /* 6953 * We won't get here if there 6954 * is only one eager in the 6955 * list. 6956 */ 6957 ASSERT(prev != NULL); 6958 listener->tcp_eager_last_q = 6959 prev; 6960 } 6961 } 6962 tcpp[0] = tcp->tcp_eager_next_q; 6963 tcp->tcp_eager_next_q = NULL; 6964 tcp->tcp_eager_last_q = NULL; 6965 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6966 listener->tcp_conn_req_cnt_q--; 6967 break; 6968 } 6969 prev = tcpp[0]; 6970 } 6971 } 6972 tcp->tcp_listener = NULL; 6973 } 6974 6975 /* Shorthand to generate and send TPI error acks to our client */ 6976 static void 6977 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6978 { 6979 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6980 putnext(tcp->tcp_rq, mp); 6981 } 6982 6983 /* Shorthand to generate and send TPI error acks to our client */ 6984 static void 6985 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6986 int t_error, int sys_error) 6987 { 6988 struct T_error_ack *teackp; 6989 6990 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6991 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6992 teackp = (struct T_error_ack *)mp->b_rptr; 6993 teackp->ERROR_prim = primitive; 6994 teackp->TLI_error = t_error; 6995 teackp->UNIX_error = sys_error; 6996 putnext(tcp->tcp_rq, mp); 6997 } 6998 } 6999 7000 /* 7001 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7002 * but instead the code relies on: 7003 * - the fact that the address of the array and its size never changes 7004 * - the atomic assignment of the elements of the array 7005 */ 7006 /* ARGSUSED */ 7007 static int 7008 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7009 { 7010 int i; 7011 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7012 7013 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7014 if (tcps->tcps_g_epriv_ports[i] != 0) 7015 (void) mi_mpprintf(mp, "%d ", 7016 tcps->tcps_g_epriv_ports[i]); 7017 } 7018 return (0); 7019 } 7020 7021 /* 7022 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7023 * threads from changing it at the same time. 7024 */ 7025 /* ARGSUSED */ 7026 static int 7027 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7028 cred_t *cr) 7029 { 7030 long new_value; 7031 int i; 7032 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7033 7034 /* 7035 * Fail the request if the new value does not lie within the 7036 * port number limits. 7037 */ 7038 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7039 new_value <= 0 || new_value >= 65536) { 7040 return (EINVAL); 7041 } 7042 7043 mutex_enter(&tcps->tcps_epriv_port_lock); 7044 /* Check if the value is already in the list */ 7045 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7046 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7047 mutex_exit(&tcps->tcps_epriv_port_lock); 7048 return (EEXIST); 7049 } 7050 } 7051 /* Find an empty slot */ 7052 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7053 if (tcps->tcps_g_epriv_ports[i] == 0) 7054 break; 7055 } 7056 if (i == tcps->tcps_g_num_epriv_ports) { 7057 mutex_exit(&tcps->tcps_epriv_port_lock); 7058 return (EOVERFLOW); 7059 } 7060 /* Set the new value */ 7061 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7062 mutex_exit(&tcps->tcps_epriv_port_lock); 7063 return (0); 7064 } 7065 7066 /* 7067 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7068 * threads from changing it at the same time. 7069 */ 7070 /* ARGSUSED */ 7071 static int 7072 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7073 cred_t *cr) 7074 { 7075 long new_value; 7076 int i; 7077 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7078 7079 /* 7080 * Fail the request if the new value does not lie within the 7081 * port number limits. 7082 */ 7083 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7084 new_value >= 65536) { 7085 return (EINVAL); 7086 } 7087 7088 mutex_enter(&tcps->tcps_epriv_port_lock); 7089 /* Check that the value is already in the list */ 7090 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7091 if (tcps->tcps_g_epriv_ports[i] == new_value) 7092 break; 7093 } 7094 if (i == tcps->tcps_g_num_epriv_ports) { 7095 mutex_exit(&tcps->tcps_epriv_port_lock); 7096 return (ESRCH); 7097 } 7098 /* Clear the value */ 7099 tcps->tcps_g_epriv_ports[i] = 0; 7100 mutex_exit(&tcps->tcps_epriv_port_lock); 7101 return (0); 7102 } 7103 7104 /* Return the TPI/TLI equivalent of our current tcp_state */ 7105 static int 7106 tcp_tpistate(tcp_t *tcp) 7107 { 7108 switch (tcp->tcp_state) { 7109 case TCPS_IDLE: 7110 return (TS_UNBND); 7111 case TCPS_LISTEN: 7112 /* 7113 * Return whether there are outstanding T_CONN_IND waiting 7114 * for the matching T_CONN_RES. Therefore don't count q0. 7115 */ 7116 if (tcp->tcp_conn_req_cnt_q > 0) 7117 return (TS_WRES_CIND); 7118 else 7119 return (TS_IDLE); 7120 case TCPS_BOUND: 7121 return (TS_IDLE); 7122 case TCPS_SYN_SENT: 7123 return (TS_WCON_CREQ); 7124 case TCPS_SYN_RCVD: 7125 /* 7126 * Note: assumption: this has to the active open SYN_RCVD. 7127 * The passive instance is detached in SYN_RCVD stage of 7128 * incoming connection processing so we cannot get request 7129 * for T_info_ack on it. 7130 */ 7131 return (TS_WACK_CRES); 7132 case TCPS_ESTABLISHED: 7133 return (TS_DATA_XFER); 7134 case TCPS_CLOSE_WAIT: 7135 return (TS_WREQ_ORDREL); 7136 case TCPS_FIN_WAIT_1: 7137 return (TS_WIND_ORDREL); 7138 case TCPS_FIN_WAIT_2: 7139 return (TS_WIND_ORDREL); 7140 7141 case TCPS_CLOSING: 7142 case TCPS_LAST_ACK: 7143 case TCPS_TIME_WAIT: 7144 case TCPS_CLOSED: 7145 /* 7146 * Following TS_WACK_DREQ7 is a rendition of "not 7147 * yet TS_IDLE" TPI state. There is no best match to any 7148 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7149 * choose a value chosen that will map to TLI/XTI level 7150 * state of TSTATECHNG (state is process of changing) which 7151 * captures what this dummy state represents. 7152 */ 7153 return (TS_WACK_DREQ7); 7154 default: 7155 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7156 tcp->tcp_state, tcp_display(tcp, NULL, 7157 DISP_PORT_ONLY)); 7158 return (TS_UNBND); 7159 } 7160 } 7161 7162 static void 7163 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7164 { 7165 tcp_stack_t *tcps = tcp->tcp_tcps; 7166 7167 if (tcp->tcp_family == AF_INET6) 7168 *tia = tcp_g_t_info_ack_v6; 7169 else 7170 *tia = tcp_g_t_info_ack; 7171 tia->CURRENT_state = tcp_tpistate(tcp); 7172 tia->OPT_size = tcp_max_optsize; 7173 if (tcp->tcp_mss == 0) { 7174 /* Not yet set - tcp_open does not set mss */ 7175 if (tcp->tcp_ipversion == IPV4_VERSION) 7176 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7177 else 7178 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7179 } else { 7180 tia->TIDU_size = tcp->tcp_mss; 7181 } 7182 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7183 } 7184 7185 static void 7186 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7187 t_uscalar_t cap_bits1) 7188 { 7189 tcap->CAP_bits1 = 0; 7190 7191 if (cap_bits1 & TC1_INFO) { 7192 tcp_copy_info(&tcap->INFO_ack, tcp); 7193 tcap->CAP_bits1 |= TC1_INFO; 7194 } 7195 7196 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7197 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7198 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7199 } 7200 7201 } 7202 7203 /* 7204 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7205 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7206 * tcp_g_t_info_ack. The current state of the stream is copied from 7207 * tcp_state. 7208 */ 7209 static void 7210 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7211 { 7212 t_uscalar_t cap_bits1; 7213 struct T_capability_ack *tcap; 7214 7215 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7216 freemsg(mp); 7217 return; 7218 } 7219 7220 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7221 7222 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7223 mp->b_datap->db_type, T_CAPABILITY_ACK); 7224 if (mp == NULL) 7225 return; 7226 7227 tcap = (struct T_capability_ack *)mp->b_rptr; 7228 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7229 7230 putnext(tcp->tcp_rq, mp); 7231 } 7232 7233 /* 7234 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7235 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7236 * The current state of the stream is copied from tcp_state. 7237 */ 7238 static void 7239 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7240 { 7241 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7242 T_INFO_ACK); 7243 if (!mp) { 7244 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7245 return; 7246 } 7247 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7248 putnext(tcp->tcp_rq, mp); 7249 } 7250 7251 /* Respond to the TPI addr request */ 7252 static void 7253 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7254 { 7255 sin_t *sin; 7256 mblk_t *ackmp; 7257 struct T_addr_ack *taa; 7258 7259 /* Make it large enough for worst case */ 7260 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7261 2 * sizeof (sin6_t), 1); 7262 if (ackmp == NULL) { 7263 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7264 return; 7265 } 7266 7267 if (tcp->tcp_ipversion == IPV6_VERSION) { 7268 tcp_addr_req_ipv6(tcp, ackmp); 7269 return; 7270 } 7271 taa = (struct T_addr_ack *)ackmp->b_rptr; 7272 7273 bzero(taa, sizeof (struct T_addr_ack)); 7274 ackmp->b_wptr = (uchar_t *)&taa[1]; 7275 7276 taa->PRIM_type = T_ADDR_ACK; 7277 ackmp->b_datap->db_type = M_PCPROTO; 7278 7279 /* 7280 * Note: Following code assumes 32 bit alignment of basic 7281 * data structures like sin_t and struct T_addr_ack. 7282 */ 7283 if (tcp->tcp_state >= TCPS_BOUND) { 7284 /* 7285 * Fill in local address 7286 */ 7287 taa->LOCADDR_length = sizeof (sin_t); 7288 taa->LOCADDR_offset = sizeof (*taa); 7289 7290 sin = (sin_t *)&taa[1]; 7291 7292 /* Fill zeroes and then intialize non-zero fields */ 7293 *sin = sin_null; 7294 7295 sin->sin_family = AF_INET; 7296 7297 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7298 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7299 7300 ackmp->b_wptr = (uchar_t *)&sin[1]; 7301 7302 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7303 /* 7304 * Fill in Remote address 7305 */ 7306 taa->REMADDR_length = sizeof (sin_t); 7307 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7308 taa->LOCADDR_length); 7309 7310 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7311 *sin = sin_null; 7312 sin->sin_family = AF_INET; 7313 sin->sin_addr.s_addr = tcp->tcp_remote; 7314 sin->sin_port = tcp->tcp_fport; 7315 7316 ackmp->b_wptr = (uchar_t *)&sin[1]; 7317 } 7318 } 7319 putnext(tcp->tcp_rq, ackmp); 7320 } 7321 7322 /* Assumes that tcp_addr_req gets enough space and alignment */ 7323 static void 7324 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7325 { 7326 sin6_t *sin6; 7327 struct T_addr_ack *taa; 7328 7329 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7330 ASSERT(OK_32PTR(ackmp->b_rptr)); 7331 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7332 2 * sizeof (sin6_t)); 7333 7334 taa = (struct T_addr_ack *)ackmp->b_rptr; 7335 7336 bzero(taa, sizeof (struct T_addr_ack)); 7337 ackmp->b_wptr = (uchar_t *)&taa[1]; 7338 7339 taa->PRIM_type = T_ADDR_ACK; 7340 ackmp->b_datap->db_type = M_PCPROTO; 7341 7342 /* 7343 * Note: Following code assumes 32 bit alignment of basic 7344 * data structures like sin6_t and struct T_addr_ack. 7345 */ 7346 if (tcp->tcp_state >= TCPS_BOUND) { 7347 /* 7348 * Fill in local address 7349 */ 7350 taa->LOCADDR_length = sizeof (sin6_t); 7351 taa->LOCADDR_offset = sizeof (*taa); 7352 7353 sin6 = (sin6_t *)&taa[1]; 7354 *sin6 = sin6_null; 7355 7356 sin6->sin6_family = AF_INET6; 7357 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7358 sin6->sin6_port = tcp->tcp_lport; 7359 7360 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7361 7362 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7363 /* 7364 * Fill in Remote address 7365 */ 7366 taa->REMADDR_length = sizeof (sin6_t); 7367 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7368 taa->LOCADDR_length); 7369 7370 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7371 *sin6 = sin6_null; 7372 sin6->sin6_family = AF_INET6; 7373 sin6->sin6_flowinfo = 7374 tcp->tcp_ip6h->ip6_vcf & 7375 ~IPV6_VERS_AND_FLOW_MASK; 7376 sin6->sin6_addr = tcp->tcp_remote_v6; 7377 sin6->sin6_port = tcp->tcp_fport; 7378 7379 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7380 } 7381 } 7382 putnext(tcp->tcp_rq, ackmp); 7383 } 7384 7385 /* 7386 * Handle reinitialization of a tcp structure. 7387 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7388 */ 7389 static void 7390 tcp_reinit(tcp_t *tcp) 7391 { 7392 mblk_t *mp; 7393 int err; 7394 tcp_stack_t *tcps = tcp->tcp_tcps; 7395 7396 TCP_STAT(tcps, tcp_reinit_calls); 7397 7398 /* tcp_reinit should never be called for detached tcp_t's */ 7399 ASSERT(tcp->tcp_listener == NULL); 7400 ASSERT((tcp->tcp_family == AF_INET && 7401 tcp->tcp_ipversion == IPV4_VERSION) || 7402 (tcp->tcp_family == AF_INET6 && 7403 (tcp->tcp_ipversion == IPV4_VERSION || 7404 tcp->tcp_ipversion == IPV6_VERSION))); 7405 7406 /* Cancel outstanding timers */ 7407 tcp_timers_stop(tcp); 7408 7409 /* 7410 * Reset everything in the state vector, after updating global 7411 * MIB data from instance counters. 7412 */ 7413 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7414 tcp->tcp_ibsegs = 0; 7415 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7416 tcp->tcp_obsegs = 0; 7417 7418 tcp_close_mpp(&tcp->tcp_xmit_head); 7419 if (tcp->tcp_snd_zcopy_aware) 7420 tcp_zcopy_notify(tcp); 7421 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7422 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7423 mutex_enter(&tcp->tcp_non_sq_lock); 7424 if (tcp->tcp_flow_stopped && 7425 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7426 tcp_clrqfull(tcp); 7427 } 7428 mutex_exit(&tcp->tcp_non_sq_lock); 7429 tcp_close_mpp(&tcp->tcp_reass_head); 7430 tcp->tcp_reass_tail = NULL; 7431 if (tcp->tcp_rcv_list != NULL) { 7432 /* Free b_next chain */ 7433 tcp_close_mpp(&tcp->tcp_rcv_list); 7434 tcp->tcp_rcv_last_head = NULL; 7435 tcp->tcp_rcv_last_tail = NULL; 7436 tcp->tcp_rcv_cnt = 0; 7437 } 7438 tcp->tcp_rcv_last_tail = NULL; 7439 7440 if ((mp = tcp->tcp_urp_mp) != NULL) { 7441 freemsg(mp); 7442 tcp->tcp_urp_mp = NULL; 7443 } 7444 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7445 freemsg(mp); 7446 tcp->tcp_urp_mark_mp = NULL; 7447 } 7448 if (tcp->tcp_fused_sigurg_mp != NULL) { 7449 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7450 freeb(tcp->tcp_fused_sigurg_mp); 7451 tcp->tcp_fused_sigurg_mp = NULL; 7452 } 7453 if (tcp->tcp_ordrel_mp != NULL) { 7454 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7455 freeb(tcp->tcp_ordrel_mp); 7456 tcp->tcp_ordrel_mp = NULL; 7457 } 7458 7459 /* 7460 * Following is a union with two members which are 7461 * identical types and size so the following cleanup 7462 * is enough. 7463 */ 7464 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7465 7466 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7467 7468 /* 7469 * The connection can't be on the tcp_time_wait_head list 7470 * since it is not detached. 7471 */ 7472 ASSERT(tcp->tcp_time_wait_next == NULL); 7473 ASSERT(tcp->tcp_time_wait_prev == NULL); 7474 ASSERT(tcp->tcp_time_wait_expire == 0); 7475 7476 if (tcp->tcp_kssl_pending) { 7477 tcp->tcp_kssl_pending = B_FALSE; 7478 7479 /* Don't reset if the initialized by bind. */ 7480 if (tcp->tcp_kssl_ent != NULL) { 7481 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7482 KSSL_NO_PROXY); 7483 } 7484 } 7485 if (tcp->tcp_kssl_ctx != NULL) { 7486 kssl_release_ctx(tcp->tcp_kssl_ctx); 7487 tcp->tcp_kssl_ctx = NULL; 7488 } 7489 7490 /* 7491 * Reset/preserve other values 7492 */ 7493 tcp_reinit_values(tcp); 7494 ipcl_hash_remove(tcp->tcp_connp); 7495 conn_delete_ire(tcp->tcp_connp, NULL); 7496 tcp_ipsec_cleanup(tcp); 7497 7498 if (tcp->tcp_conn_req_max != 0) { 7499 /* 7500 * This is the case when a TLI program uses the same 7501 * transport end point to accept a connection. This 7502 * makes the TCP both a listener and acceptor. When 7503 * this connection is closed, we need to set the state 7504 * back to TCPS_LISTEN. Make sure that the eager list 7505 * is reinitialized. 7506 * 7507 * Note that this stream is still bound to the four 7508 * tuples of the previous connection in IP. If a new 7509 * SYN with different foreign address comes in, IP will 7510 * not find it and will send it to the global queue. In 7511 * the global queue, TCP will do a tcp_lookup_listener() 7512 * to find this stream. This works because this stream 7513 * is only removed from connected hash. 7514 * 7515 */ 7516 tcp->tcp_state = TCPS_LISTEN; 7517 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7518 tcp->tcp_eager_next_drop_q0 = tcp; 7519 tcp->tcp_eager_prev_drop_q0 = tcp; 7520 tcp->tcp_connp->conn_recv = tcp_conn_request; 7521 if (tcp->tcp_family == AF_INET6) { 7522 ASSERT(tcp->tcp_connp->conn_af_isv6); 7523 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7524 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7525 } else { 7526 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7527 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7528 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7529 } 7530 } else { 7531 tcp->tcp_state = TCPS_BOUND; 7532 } 7533 7534 /* 7535 * Initialize to default values 7536 * Can't fail since enough header template space already allocated 7537 * at open(). 7538 */ 7539 err = tcp_init_values(tcp); 7540 ASSERT(err == 0); 7541 /* Restore state in tcp_tcph */ 7542 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7543 if (tcp->tcp_ipversion == IPV4_VERSION) 7544 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7545 else 7546 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7547 /* 7548 * Copy of the src addr. in tcp_t is needed in tcp_t 7549 * since the lookup funcs can only lookup on tcp_t 7550 */ 7551 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7552 7553 ASSERT(tcp->tcp_ptpbhn != NULL); 7554 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7555 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7556 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7557 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7558 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7559 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7560 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7561 } 7562 7563 /* 7564 * Force values to zero that need be zero. 7565 * Do not touch values asociated with the BOUND or LISTEN state 7566 * since the connection will end up in that state after the reinit. 7567 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7568 * structure! 7569 */ 7570 static void 7571 tcp_reinit_values(tcp) 7572 tcp_t *tcp; 7573 { 7574 tcp_stack_t *tcps = tcp->tcp_tcps; 7575 7576 #ifndef lint 7577 #define DONTCARE(x) 7578 #define PRESERVE(x) 7579 #else 7580 #define DONTCARE(x) ((x) = (x)) 7581 #define PRESERVE(x) ((x) = (x)) 7582 #endif /* lint */ 7583 7584 PRESERVE(tcp->tcp_bind_hash_port); 7585 PRESERVE(tcp->tcp_bind_hash); 7586 PRESERVE(tcp->tcp_ptpbhn); 7587 PRESERVE(tcp->tcp_acceptor_hash); 7588 PRESERVE(tcp->tcp_ptpahn); 7589 7590 /* Should be ASSERT NULL on these with new code! */ 7591 ASSERT(tcp->tcp_time_wait_next == NULL); 7592 ASSERT(tcp->tcp_time_wait_prev == NULL); 7593 ASSERT(tcp->tcp_time_wait_expire == 0); 7594 PRESERVE(tcp->tcp_state); 7595 PRESERVE(tcp->tcp_rq); 7596 PRESERVE(tcp->tcp_wq); 7597 7598 ASSERT(tcp->tcp_xmit_head == NULL); 7599 ASSERT(tcp->tcp_xmit_last == NULL); 7600 ASSERT(tcp->tcp_unsent == 0); 7601 ASSERT(tcp->tcp_xmit_tail == NULL); 7602 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7603 7604 tcp->tcp_snxt = 0; /* Displayed in mib */ 7605 tcp->tcp_suna = 0; /* Displayed in mib */ 7606 tcp->tcp_swnd = 0; 7607 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7608 7609 ASSERT(tcp->tcp_ibsegs == 0); 7610 ASSERT(tcp->tcp_obsegs == 0); 7611 7612 if (tcp->tcp_iphc != NULL) { 7613 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7614 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7615 } 7616 7617 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7618 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7619 DONTCARE(tcp->tcp_ipha); 7620 DONTCARE(tcp->tcp_ip6h); 7621 DONTCARE(tcp->tcp_ip_hdr_len); 7622 DONTCARE(tcp->tcp_tcph); 7623 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7624 tcp->tcp_valid_bits = 0; 7625 7626 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7627 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7628 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7629 tcp->tcp_last_rcv_lbolt = 0; 7630 7631 tcp->tcp_init_cwnd = 0; 7632 7633 tcp->tcp_urp_last_valid = 0; 7634 tcp->tcp_hard_binding = 0; 7635 tcp->tcp_hard_bound = 0; 7636 PRESERVE(tcp->tcp_cred); 7637 PRESERVE(tcp->tcp_cpid); 7638 PRESERVE(tcp->tcp_open_time); 7639 PRESERVE(tcp->tcp_exclbind); 7640 7641 tcp->tcp_fin_acked = 0; 7642 tcp->tcp_fin_rcvd = 0; 7643 tcp->tcp_fin_sent = 0; 7644 tcp->tcp_ordrel_done = 0; 7645 7646 tcp->tcp_debug = 0; 7647 tcp->tcp_dontroute = 0; 7648 tcp->tcp_broadcast = 0; 7649 7650 tcp->tcp_useloopback = 0; 7651 tcp->tcp_reuseaddr = 0; 7652 tcp->tcp_oobinline = 0; 7653 tcp->tcp_dgram_errind = 0; 7654 7655 tcp->tcp_detached = 0; 7656 tcp->tcp_bind_pending = 0; 7657 tcp->tcp_unbind_pending = 0; 7658 7659 tcp->tcp_snd_ws_ok = B_FALSE; 7660 tcp->tcp_snd_ts_ok = B_FALSE; 7661 tcp->tcp_linger = 0; 7662 tcp->tcp_ka_enabled = 0; 7663 tcp->tcp_zero_win_probe = 0; 7664 7665 tcp->tcp_loopback = 0; 7666 tcp->tcp_refuse = 0; 7667 tcp->tcp_localnet = 0; 7668 tcp->tcp_syn_defense = 0; 7669 tcp->tcp_set_timer = 0; 7670 7671 tcp->tcp_active_open = 0; 7672 tcp->tcp_rexmit = B_FALSE; 7673 tcp->tcp_xmit_zc_clean = B_FALSE; 7674 7675 tcp->tcp_snd_sack_ok = B_FALSE; 7676 PRESERVE(tcp->tcp_recvdstaddr); 7677 tcp->tcp_hwcksum = B_FALSE; 7678 7679 tcp->tcp_ire_ill_check_done = B_FALSE; 7680 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7681 7682 tcp->tcp_mdt = B_FALSE; 7683 tcp->tcp_mdt_hdr_head = 0; 7684 tcp->tcp_mdt_hdr_tail = 0; 7685 7686 tcp->tcp_conn_def_q0 = 0; 7687 tcp->tcp_ip_forward_progress = B_FALSE; 7688 tcp->tcp_anon_priv_bind = 0; 7689 tcp->tcp_ecn_ok = B_FALSE; 7690 7691 tcp->tcp_cwr = B_FALSE; 7692 tcp->tcp_ecn_echo_on = B_FALSE; 7693 7694 if (tcp->tcp_sack_info != NULL) { 7695 if (tcp->tcp_notsack_list != NULL) { 7696 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7697 } 7698 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7699 tcp->tcp_sack_info = NULL; 7700 } 7701 7702 tcp->tcp_rcv_ws = 0; 7703 tcp->tcp_snd_ws = 0; 7704 tcp->tcp_ts_recent = 0; 7705 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7706 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7707 tcp->tcp_if_mtu = 0; 7708 7709 ASSERT(tcp->tcp_reass_head == NULL); 7710 ASSERT(tcp->tcp_reass_tail == NULL); 7711 7712 tcp->tcp_cwnd_cnt = 0; 7713 7714 ASSERT(tcp->tcp_rcv_list == NULL); 7715 ASSERT(tcp->tcp_rcv_last_head == NULL); 7716 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7717 ASSERT(tcp->tcp_rcv_cnt == 0); 7718 7719 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7720 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7721 tcp->tcp_csuna = 0; 7722 7723 tcp->tcp_rto = 0; /* Displayed in MIB */ 7724 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7725 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7726 tcp->tcp_rtt_update = 0; 7727 7728 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7729 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7730 7731 tcp->tcp_rack = 0; /* Displayed in mib */ 7732 tcp->tcp_rack_cnt = 0; 7733 tcp->tcp_rack_cur_max = 0; 7734 tcp->tcp_rack_abs_max = 0; 7735 7736 tcp->tcp_max_swnd = 0; 7737 7738 ASSERT(tcp->tcp_listener == NULL); 7739 7740 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7741 7742 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7743 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7744 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7745 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7746 7747 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7748 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7749 PRESERVE(tcp->tcp_conn_req_max); 7750 PRESERVE(tcp->tcp_conn_req_seqnum); 7751 7752 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7753 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7754 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7755 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7756 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7757 7758 tcp->tcp_lingertime = 0; 7759 7760 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7761 ASSERT(tcp->tcp_urp_mp == NULL); 7762 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7763 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7764 7765 ASSERT(tcp->tcp_eager_next_q == NULL); 7766 ASSERT(tcp->tcp_eager_last_q == NULL); 7767 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7768 tcp->tcp_eager_prev_q0 == NULL) || 7769 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7770 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7771 7772 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7773 tcp->tcp_eager_prev_drop_q0 == NULL) || 7774 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7775 7776 tcp->tcp_client_errno = 0; 7777 7778 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7779 7780 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7781 7782 PRESERVE(tcp->tcp_bound_source_v6); 7783 tcp->tcp_last_sent_len = 0; 7784 tcp->tcp_dupack_cnt = 0; 7785 7786 tcp->tcp_fport = 0; /* Displayed in MIB */ 7787 PRESERVE(tcp->tcp_lport); 7788 7789 PRESERVE(tcp->tcp_acceptor_lockp); 7790 7791 ASSERT(tcp->tcp_ordrel_mp == NULL); 7792 PRESERVE(tcp->tcp_acceptor_id); 7793 DONTCARE(tcp->tcp_ipsec_overhead); 7794 7795 PRESERVE(tcp->tcp_family); 7796 if (tcp->tcp_family == AF_INET6) { 7797 tcp->tcp_ipversion = IPV6_VERSION; 7798 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7799 } else { 7800 tcp->tcp_ipversion = IPV4_VERSION; 7801 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7802 } 7803 7804 tcp->tcp_bound_if = 0; 7805 tcp->tcp_ipv6_recvancillary = 0; 7806 tcp->tcp_recvifindex = 0; 7807 tcp->tcp_recvhops = 0; 7808 tcp->tcp_closed = 0; 7809 tcp->tcp_cleandeathtag = 0; 7810 if (tcp->tcp_hopopts != NULL) { 7811 mi_free(tcp->tcp_hopopts); 7812 tcp->tcp_hopopts = NULL; 7813 tcp->tcp_hopoptslen = 0; 7814 } 7815 ASSERT(tcp->tcp_hopoptslen == 0); 7816 if (tcp->tcp_dstopts != NULL) { 7817 mi_free(tcp->tcp_dstopts); 7818 tcp->tcp_dstopts = NULL; 7819 tcp->tcp_dstoptslen = 0; 7820 } 7821 ASSERT(tcp->tcp_dstoptslen == 0); 7822 if (tcp->tcp_rtdstopts != NULL) { 7823 mi_free(tcp->tcp_rtdstopts); 7824 tcp->tcp_rtdstopts = NULL; 7825 tcp->tcp_rtdstoptslen = 0; 7826 } 7827 ASSERT(tcp->tcp_rtdstoptslen == 0); 7828 if (tcp->tcp_rthdr != NULL) { 7829 mi_free(tcp->tcp_rthdr); 7830 tcp->tcp_rthdr = NULL; 7831 tcp->tcp_rthdrlen = 0; 7832 } 7833 ASSERT(tcp->tcp_rthdrlen == 0); 7834 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7835 7836 /* Reset fusion-related fields */ 7837 tcp->tcp_fused = B_FALSE; 7838 tcp->tcp_unfusable = B_FALSE; 7839 tcp->tcp_fused_sigurg = B_FALSE; 7840 tcp->tcp_direct_sockfs = B_FALSE; 7841 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7842 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7843 tcp->tcp_loopback_peer = NULL; 7844 tcp->tcp_fuse_rcv_hiwater = 0; 7845 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7846 tcp->tcp_fuse_rcv_unread_cnt = 0; 7847 7848 tcp->tcp_lso = B_FALSE; 7849 7850 tcp->tcp_in_ack_unsent = 0; 7851 tcp->tcp_cork = B_FALSE; 7852 tcp->tcp_tconnind_started = B_FALSE; 7853 7854 PRESERVE(tcp->tcp_squeue_bytes); 7855 7856 ASSERT(tcp->tcp_kssl_ctx == NULL); 7857 ASSERT(!tcp->tcp_kssl_pending); 7858 PRESERVE(tcp->tcp_kssl_ent); 7859 7860 /* Sodirect */ 7861 tcp->tcp_sodirect = NULL; 7862 7863 tcp->tcp_closemp_used = B_FALSE; 7864 7865 PRESERVE(tcp->tcp_rsrv_mp); 7866 PRESERVE(tcp->tcp_rsrv_mp_lock); 7867 7868 #ifdef DEBUG 7869 DONTCARE(tcp->tcmp_stk[0]); 7870 #endif 7871 7872 PRESERVE(tcp->tcp_connid); 7873 7874 7875 #undef DONTCARE 7876 #undef PRESERVE 7877 } 7878 7879 /* 7880 * Allocate necessary resources and initialize state vector. 7881 * Guaranteed not to fail so that when an error is returned, 7882 * the caller doesn't need to do any additional cleanup. 7883 */ 7884 int 7885 tcp_init(tcp_t *tcp, queue_t *q) 7886 { 7887 int err; 7888 7889 tcp->tcp_rq = q; 7890 tcp->tcp_wq = WR(q); 7891 tcp->tcp_state = TCPS_IDLE; 7892 if ((err = tcp_init_values(tcp)) != 0) 7893 tcp_timers_stop(tcp); 7894 return (err); 7895 } 7896 7897 static int 7898 tcp_init_values(tcp_t *tcp) 7899 { 7900 int err; 7901 tcp_stack_t *tcps = tcp->tcp_tcps; 7902 7903 ASSERT((tcp->tcp_family == AF_INET && 7904 tcp->tcp_ipversion == IPV4_VERSION) || 7905 (tcp->tcp_family == AF_INET6 && 7906 (tcp->tcp_ipversion == IPV4_VERSION || 7907 tcp->tcp_ipversion == IPV6_VERSION))); 7908 7909 /* 7910 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7911 * will be close to tcp_rexmit_interval_initial. By doing this, we 7912 * allow the algorithm to adjust slowly to large fluctuations of RTT 7913 * during first few transmissions of a connection as seen in slow 7914 * links. 7915 */ 7916 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7917 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7918 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7919 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7920 tcps->tcps_conn_grace_period; 7921 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7922 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7923 tcp->tcp_timer_backoff = 0; 7924 tcp->tcp_ms_we_have_waited = 0; 7925 tcp->tcp_last_recv_time = lbolt; 7926 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7927 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7928 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7929 7930 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7931 7932 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7933 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7934 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7935 /* 7936 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7937 * passive open. 7938 */ 7939 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7940 7941 tcp->tcp_naglim = tcps->tcps_naglim_def; 7942 7943 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7944 7945 tcp->tcp_mdt_hdr_head = 0; 7946 tcp->tcp_mdt_hdr_tail = 0; 7947 7948 /* Reset fusion-related fields */ 7949 tcp->tcp_fused = B_FALSE; 7950 tcp->tcp_unfusable = B_FALSE; 7951 tcp->tcp_fused_sigurg = B_FALSE; 7952 tcp->tcp_direct_sockfs = B_FALSE; 7953 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7954 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7955 tcp->tcp_loopback_peer = NULL; 7956 tcp->tcp_fuse_rcv_hiwater = 0; 7957 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7958 tcp->tcp_fuse_rcv_unread_cnt = 0; 7959 7960 /* Sodirect */ 7961 tcp->tcp_sodirect = NULL; 7962 7963 /* Initialize the header template */ 7964 if (tcp->tcp_ipversion == IPV4_VERSION) { 7965 err = tcp_header_init_ipv4(tcp); 7966 } else { 7967 err = tcp_header_init_ipv6(tcp); 7968 } 7969 if (err) 7970 return (err); 7971 7972 /* 7973 * Init the window scale to the max so tcp_rwnd_set() won't pare 7974 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7975 */ 7976 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7977 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7978 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7979 7980 tcp->tcp_cork = B_FALSE; 7981 /* 7982 * Init the tcp_debug option. This value determines whether TCP 7983 * calls strlog() to print out debug messages. Doing this 7984 * initialization here means that this value is not inherited thru 7985 * tcp_reinit(). 7986 */ 7987 tcp->tcp_debug = tcps->tcps_dbg; 7988 7989 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7990 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7991 7992 return (0); 7993 } 7994 7995 /* 7996 * Initialize the IPv4 header. Loses any record of any IP options. 7997 */ 7998 static int 7999 tcp_header_init_ipv4(tcp_t *tcp) 8000 { 8001 tcph_t *tcph; 8002 uint32_t sum; 8003 conn_t *connp; 8004 tcp_stack_t *tcps = tcp->tcp_tcps; 8005 8006 /* 8007 * This is a simple initialization. If there's 8008 * already a template, it should never be too small, 8009 * so reuse it. Otherwise, allocate space for the new one. 8010 */ 8011 if (tcp->tcp_iphc == NULL) { 8012 ASSERT(tcp->tcp_iphc_len == 0); 8013 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8014 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8015 if (tcp->tcp_iphc == NULL) { 8016 tcp->tcp_iphc_len = 0; 8017 return (ENOMEM); 8018 } 8019 } 8020 8021 /* options are gone; may need a new label */ 8022 connp = tcp->tcp_connp; 8023 connp->conn_mlp_type = mlptSingle; 8024 connp->conn_ulp_labeled = !is_system_labeled(); 8025 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8026 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8027 tcp->tcp_ip6h = NULL; 8028 tcp->tcp_ipversion = IPV4_VERSION; 8029 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8030 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8031 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8032 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8033 tcp->tcp_ipha->ipha_version_and_hdr_length 8034 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8035 tcp->tcp_ipha->ipha_ident = 0; 8036 8037 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8038 tcp->tcp_tos = 0; 8039 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8040 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8041 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8042 8043 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8044 tcp->tcp_tcph = tcph; 8045 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8046 /* 8047 * IP wants our header length in the checksum field to 8048 * allow it to perform a single pseudo-header+checksum 8049 * calculation on behalf of TCP. 8050 * Include the adjustment for a source route once IP_OPTIONS is set. 8051 */ 8052 sum = sizeof (tcph_t) + tcp->tcp_sum; 8053 sum = (sum >> 16) + (sum & 0xFFFF); 8054 U16_TO_ABE16(sum, tcph->th_sum); 8055 return (0); 8056 } 8057 8058 /* 8059 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8060 */ 8061 static int 8062 tcp_header_init_ipv6(tcp_t *tcp) 8063 { 8064 tcph_t *tcph; 8065 uint32_t sum; 8066 conn_t *connp; 8067 tcp_stack_t *tcps = tcp->tcp_tcps; 8068 8069 /* 8070 * This is a simple initialization. If there's 8071 * already a template, it should never be too small, 8072 * so reuse it. Otherwise, allocate space for the new one. 8073 * Ensure that there is enough space to "downgrade" the tcp_t 8074 * to an IPv4 tcp_t. This requires having space for a full load 8075 * of IPv4 options, as well as a full load of TCP options 8076 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8077 * than a v6 header and a TCP header with a full load of TCP options 8078 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8079 * We want to avoid reallocation in the "downgraded" case when 8080 * processing outbound IPv4 options. 8081 */ 8082 if (tcp->tcp_iphc == NULL) { 8083 ASSERT(tcp->tcp_iphc_len == 0); 8084 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8085 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8086 if (tcp->tcp_iphc == NULL) { 8087 tcp->tcp_iphc_len = 0; 8088 return (ENOMEM); 8089 } 8090 } 8091 8092 /* options are gone; may need a new label */ 8093 connp = tcp->tcp_connp; 8094 connp->conn_mlp_type = mlptSingle; 8095 connp->conn_ulp_labeled = !is_system_labeled(); 8096 8097 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8098 tcp->tcp_ipversion = IPV6_VERSION; 8099 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8100 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8101 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8102 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8103 tcp->tcp_ipha = NULL; 8104 8105 /* Initialize the header template */ 8106 8107 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8108 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8109 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8110 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8111 8112 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8113 tcp->tcp_tcph = tcph; 8114 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8115 /* 8116 * IP wants our header length in the checksum field to 8117 * allow it to perform a single psuedo-header+checksum 8118 * calculation on behalf of TCP. 8119 * Include the adjustment for a source route when IPV6_RTHDR is set. 8120 */ 8121 sum = sizeof (tcph_t) + tcp->tcp_sum; 8122 sum = (sum >> 16) + (sum & 0xFFFF); 8123 U16_TO_ABE16(sum, tcph->th_sum); 8124 return (0); 8125 } 8126 8127 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8128 #define ICMP_MIN_TCP_HDR 8 8129 8130 /* 8131 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8132 * passed up by IP. The message is always received on the correct tcp_t. 8133 * Assumes that IP has pulled up everything up to and including the ICMP header. 8134 */ 8135 void 8136 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8137 { 8138 icmph_t *icmph; 8139 ipha_t *ipha; 8140 int iph_hdr_length; 8141 tcph_t *tcph; 8142 boolean_t ipsec_mctl = B_FALSE; 8143 boolean_t secure; 8144 mblk_t *first_mp = mp; 8145 int32_t new_mss; 8146 uint32_t ratio; 8147 size_t mp_size = MBLKL(mp); 8148 uint32_t seg_seq; 8149 tcp_stack_t *tcps = tcp->tcp_tcps; 8150 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8151 8152 /* Assume IP provides aligned packets - otherwise toss */ 8153 if (!OK_32PTR(mp->b_rptr)) { 8154 freemsg(mp); 8155 return; 8156 } 8157 8158 /* 8159 * Since ICMP errors are normal data marked with M_CTL when sent 8160 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8161 * packets starting with an ipsec_info_t, see ipsec_info.h. 8162 */ 8163 if ((mp_size == sizeof (ipsec_info_t)) && 8164 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8165 ASSERT(mp->b_cont != NULL); 8166 mp = mp->b_cont; 8167 /* IP should have done this */ 8168 ASSERT(OK_32PTR(mp->b_rptr)); 8169 mp_size = MBLKL(mp); 8170 ipsec_mctl = B_TRUE; 8171 } 8172 8173 /* 8174 * Verify that we have a complete outer IP header. If not, drop it. 8175 */ 8176 if (mp_size < sizeof (ipha_t)) { 8177 noticmpv4: 8178 freemsg(first_mp); 8179 return; 8180 } 8181 8182 ipha = (ipha_t *)mp->b_rptr; 8183 /* 8184 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8185 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8186 */ 8187 switch (IPH_HDR_VERSION(ipha)) { 8188 case IPV6_VERSION: 8189 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8190 return; 8191 case IPV4_VERSION: 8192 break; 8193 default: 8194 goto noticmpv4; 8195 } 8196 8197 /* Skip past the outer IP and ICMP headers */ 8198 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8199 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8200 /* 8201 * If we don't have the correct outer IP header length or if the ULP 8202 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8203 * send it upstream. 8204 */ 8205 if (iph_hdr_length < sizeof (ipha_t) || 8206 ipha->ipha_protocol != IPPROTO_ICMP || 8207 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8208 goto noticmpv4; 8209 } 8210 ipha = (ipha_t *)&icmph[1]; 8211 8212 /* Skip past the inner IP and find the ULP header */ 8213 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8214 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8215 /* 8216 * If we don't have the correct inner IP header length or if the ULP 8217 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8218 * bytes of TCP header, drop it. 8219 */ 8220 if (iph_hdr_length < sizeof (ipha_t) || 8221 ipha->ipha_protocol != IPPROTO_TCP || 8222 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8223 goto noticmpv4; 8224 } 8225 8226 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8227 if (ipsec_mctl) { 8228 secure = ipsec_in_is_secure(first_mp); 8229 } else { 8230 secure = B_FALSE; 8231 } 8232 if (secure) { 8233 /* 8234 * If we are willing to accept this in clear 8235 * we don't have to verify policy. 8236 */ 8237 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8238 if (!tcp_check_policy(tcp, first_mp, 8239 ipha, NULL, secure, ipsec_mctl)) { 8240 /* 8241 * tcp_check_policy called 8242 * ip_drop_packet() on failure. 8243 */ 8244 return; 8245 } 8246 } 8247 } 8248 } else if (ipsec_mctl) { 8249 /* 8250 * This is a hard_bound connection. IP has already 8251 * verified policy. We don't have to do it again. 8252 */ 8253 freeb(first_mp); 8254 first_mp = mp; 8255 ipsec_mctl = B_FALSE; 8256 } 8257 8258 seg_seq = ABE32_TO_U32(tcph->th_seq); 8259 /* 8260 * TCP SHOULD check that the TCP sequence number contained in 8261 * payload of the ICMP error message is within the range 8262 * SND.UNA <= SEG.SEQ < SND.NXT. 8263 */ 8264 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8265 /* 8266 * The ICMP message is bogus, just drop it. But if this is 8267 * an ICMP too big message, IP has already changed 8268 * the ire_max_frag to the bogus value. We need to change 8269 * it back. 8270 */ 8271 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8272 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8273 conn_t *connp = tcp->tcp_connp; 8274 ire_t *ire; 8275 int flag; 8276 8277 if (tcp->tcp_ipversion == IPV4_VERSION) { 8278 flag = tcp->tcp_ipha-> 8279 ipha_fragment_offset_and_flags; 8280 } else { 8281 flag = 0; 8282 } 8283 mutex_enter(&connp->conn_lock); 8284 if ((ire = connp->conn_ire_cache) != NULL) { 8285 mutex_enter(&ire->ire_lock); 8286 mutex_exit(&connp->conn_lock); 8287 ire->ire_max_frag = tcp->tcp_if_mtu; 8288 ire->ire_frag_flag |= flag; 8289 mutex_exit(&ire->ire_lock); 8290 } else { 8291 mutex_exit(&connp->conn_lock); 8292 } 8293 } 8294 goto noticmpv4; 8295 } 8296 8297 switch (icmph->icmph_type) { 8298 case ICMP_DEST_UNREACHABLE: 8299 switch (icmph->icmph_code) { 8300 case ICMP_FRAGMENTATION_NEEDED: 8301 /* 8302 * Reduce the MSS based on the new MTU. This will 8303 * eliminate any fragmentation locally. 8304 * N.B. There may well be some funny side-effects on 8305 * the local send policy and the remote receive policy. 8306 * Pending further research, we provide 8307 * tcp_ignore_path_mtu just in case this proves 8308 * disastrous somewhere. 8309 * 8310 * After updating the MSS, retransmit part of the 8311 * dropped segment using the new mss by calling 8312 * tcp_wput_data(). Need to adjust all those 8313 * params to make sure tcp_wput_data() work properly. 8314 */ 8315 if (tcps->tcps_ignore_path_mtu || 8316 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8317 break; 8318 8319 /* 8320 * Decrease the MSS by time stamp options 8321 * IP options and IPSEC options. tcp_hdr_len 8322 * includes time stamp option and IP option 8323 * length. Note that new_mss may be negative 8324 * if tcp_ipsec_overhead is large and the 8325 * icmph_du_mtu is the minimum value, which is 68. 8326 */ 8327 new_mss = ntohs(icmph->icmph_du_mtu) - 8328 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8329 8330 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8331 new_mss); 8332 8333 /* 8334 * Only update the MSS if the new one is 8335 * smaller than the previous one. This is 8336 * to avoid problems when getting multiple 8337 * ICMP errors for the same MTU. 8338 */ 8339 if (new_mss >= tcp->tcp_mss) 8340 break; 8341 8342 /* 8343 * Note that we are using the template header's DF 8344 * bit in the fast path sending. So we need to compare 8345 * the new mss with both tcps_mss_min and ip_pmtu_min. 8346 * And stop doing IPv4 PMTUd if new_mss is less than 8347 * MAX(tcps_mss_min, ip_pmtu_min). 8348 */ 8349 if (new_mss < tcps->tcps_mss_min || 8350 new_mss < ipst->ips_ip_pmtu_min) { 8351 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8352 0; 8353 } 8354 8355 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8356 ASSERT(ratio >= 1); 8357 tcp_mss_set(tcp, new_mss, B_TRUE); 8358 8359 /* 8360 * Make sure we have something to 8361 * send. 8362 */ 8363 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8364 (tcp->tcp_xmit_head != NULL)) { 8365 /* 8366 * Shrink tcp_cwnd in 8367 * proportion to the old MSS/new MSS. 8368 */ 8369 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8370 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8371 (tcp->tcp_unsent == 0)) { 8372 tcp->tcp_rexmit_max = tcp->tcp_fss; 8373 } else { 8374 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8375 } 8376 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8377 tcp->tcp_rexmit = B_TRUE; 8378 tcp->tcp_dupack_cnt = 0; 8379 tcp->tcp_snd_burst = TCP_CWND_SS; 8380 tcp_ss_rexmit(tcp); 8381 } 8382 break; 8383 case ICMP_PORT_UNREACHABLE: 8384 case ICMP_PROTOCOL_UNREACHABLE: 8385 switch (tcp->tcp_state) { 8386 case TCPS_SYN_SENT: 8387 case TCPS_SYN_RCVD: 8388 /* 8389 * ICMP can snipe away incipient 8390 * TCP connections as long as 8391 * seq number is same as initial 8392 * send seq number. 8393 */ 8394 if (seg_seq == tcp->tcp_iss) { 8395 (void) tcp_clean_death(tcp, 8396 ECONNREFUSED, 6); 8397 } 8398 break; 8399 } 8400 break; 8401 case ICMP_HOST_UNREACHABLE: 8402 case ICMP_NET_UNREACHABLE: 8403 /* Record the error in case we finally time out. */ 8404 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8405 tcp->tcp_client_errno = EHOSTUNREACH; 8406 else 8407 tcp->tcp_client_errno = ENETUNREACH; 8408 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8409 if (tcp->tcp_listener != NULL && 8410 tcp->tcp_listener->tcp_syn_defense) { 8411 /* 8412 * Ditch the half-open connection if we 8413 * suspect a SYN attack is under way. 8414 */ 8415 tcp_ip_ire_mark_advice(tcp); 8416 (void) tcp_clean_death(tcp, 8417 tcp->tcp_client_errno, 7); 8418 } 8419 } 8420 break; 8421 default: 8422 break; 8423 } 8424 break; 8425 case ICMP_SOURCE_QUENCH: { 8426 /* 8427 * use a global boolean to control 8428 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8429 * The default is false. 8430 */ 8431 if (tcp_icmp_source_quench) { 8432 /* 8433 * Reduce the sending rate as if we got a 8434 * retransmit timeout 8435 */ 8436 uint32_t npkt; 8437 8438 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8439 tcp->tcp_mss; 8440 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8441 tcp->tcp_cwnd = tcp->tcp_mss; 8442 tcp->tcp_cwnd_cnt = 0; 8443 } 8444 break; 8445 } 8446 } 8447 freemsg(first_mp); 8448 } 8449 8450 /* 8451 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8452 * error messages passed up by IP. 8453 * Assumes that IP has pulled up all the extension headers as well 8454 * as the ICMPv6 header. 8455 */ 8456 static void 8457 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8458 { 8459 icmp6_t *icmp6; 8460 ip6_t *ip6h; 8461 uint16_t iph_hdr_length; 8462 tcpha_t *tcpha; 8463 uint8_t *nexthdrp; 8464 uint32_t new_mss; 8465 uint32_t ratio; 8466 boolean_t secure; 8467 mblk_t *first_mp = mp; 8468 size_t mp_size; 8469 uint32_t seg_seq; 8470 tcp_stack_t *tcps = tcp->tcp_tcps; 8471 8472 /* 8473 * The caller has determined if this is an IPSEC_IN packet and 8474 * set ipsec_mctl appropriately (see tcp_icmp_error). 8475 */ 8476 if (ipsec_mctl) 8477 mp = mp->b_cont; 8478 8479 mp_size = MBLKL(mp); 8480 8481 /* 8482 * Verify that we have a complete IP header. If not, send it upstream. 8483 */ 8484 if (mp_size < sizeof (ip6_t)) { 8485 noticmpv6: 8486 freemsg(first_mp); 8487 return; 8488 } 8489 8490 /* 8491 * Verify this is an ICMPV6 packet, else send it upstream. 8492 */ 8493 ip6h = (ip6_t *)mp->b_rptr; 8494 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8495 iph_hdr_length = IPV6_HDR_LEN; 8496 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8497 &nexthdrp) || 8498 *nexthdrp != IPPROTO_ICMPV6) { 8499 goto noticmpv6; 8500 } 8501 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8502 ip6h = (ip6_t *)&icmp6[1]; 8503 /* 8504 * Verify if we have a complete ICMP and inner IP header. 8505 */ 8506 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8507 goto noticmpv6; 8508 8509 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8510 goto noticmpv6; 8511 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8512 /* 8513 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8514 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8515 * packet. 8516 */ 8517 if ((*nexthdrp != IPPROTO_TCP) || 8518 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8519 goto noticmpv6; 8520 } 8521 8522 /* 8523 * ICMP errors come on the right queue or come on 8524 * listener/global queue for detached connections and 8525 * get switched to the right queue. If it comes on the 8526 * right queue, policy check has already been done by IP 8527 * and thus free the first_mp without verifying the policy. 8528 * If it has come for a non-hard bound connection, we need 8529 * to verify policy as IP may not have done it. 8530 */ 8531 if (!tcp->tcp_hard_bound) { 8532 if (ipsec_mctl) { 8533 secure = ipsec_in_is_secure(first_mp); 8534 } else { 8535 secure = B_FALSE; 8536 } 8537 if (secure) { 8538 /* 8539 * If we are willing to accept this in clear 8540 * we don't have to verify policy. 8541 */ 8542 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8543 if (!tcp_check_policy(tcp, first_mp, 8544 NULL, ip6h, secure, ipsec_mctl)) { 8545 /* 8546 * tcp_check_policy called 8547 * ip_drop_packet() on failure. 8548 */ 8549 return; 8550 } 8551 } 8552 } 8553 } else if (ipsec_mctl) { 8554 /* 8555 * This is a hard_bound connection. IP has already 8556 * verified policy. We don't have to do it again. 8557 */ 8558 freeb(first_mp); 8559 first_mp = mp; 8560 ipsec_mctl = B_FALSE; 8561 } 8562 8563 seg_seq = ntohl(tcpha->tha_seq); 8564 /* 8565 * TCP SHOULD check that the TCP sequence number contained in 8566 * payload of the ICMP error message is within the range 8567 * SND.UNA <= SEG.SEQ < SND.NXT. 8568 */ 8569 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8570 /* 8571 * If the ICMP message is bogus, should we kill the 8572 * connection, or should we just drop the bogus ICMP 8573 * message? It would probably make more sense to just 8574 * drop the message so that if this one managed to get 8575 * in, the real connection should not suffer. 8576 */ 8577 goto noticmpv6; 8578 } 8579 8580 switch (icmp6->icmp6_type) { 8581 case ICMP6_PACKET_TOO_BIG: 8582 /* 8583 * Reduce the MSS based on the new MTU. This will 8584 * eliminate any fragmentation locally. 8585 * N.B. There may well be some funny side-effects on 8586 * the local send policy and the remote receive policy. 8587 * Pending further research, we provide 8588 * tcp_ignore_path_mtu just in case this proves 8589 * disastrous somewhere. 8590 * 8591 * After updating the MSS, retransmit part of the 8592 * dropped segment using the new mss by calling 8593 * tcp_wput_data(). Need to adjust all those 8594 * params to make sure tcp_wput_data() work properly. 8595 */ 8596 if (tcps->tcps_ignore_path_mtu) 8597 break; 8598 8599 /* 8600 * Decrease the MSS by time stamp options 8601 * IP options and IPSEC options. tcp_hdr_len 8602 * includes time stamp option and IP option 8603 * length. 8604 */ 8605 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8606 tcp->tcp_ipsec_overhead; 8607 8608 /* 8609 * Only update the MSS if the new one is 8610 * smaller than the previous one. This is 8611 * to avoid problems when getting multiple 8612 * ICMP errors for the same MTU. 8613 */ 8614 if (new_mss >= tcp->tcp_mss) 8615 break; 8616 8617 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8618 ASSERT(ratio >= 1); 8619 tcp_mss_set(tcp, new_mss, B_TRUE); 8620 8621 /* 8622 * Make sure we have something to 8623 * send. 8624 */ 8625 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8626 (tcp->tcp_xmit_head != NULL)) { 8627 /* 8628 * Shrink tcp_cwnd in 8629 * proportion to the old MSS/new MSS. 8630 */ 8631 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8632 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8633 (tcp->tcp_unsent == 0)) { 8634 tcp->tcp_rexmit_max = tcp->tcp_fss; 8635 } else { 8636 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8637 } 8638 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8639 tcp->tcp_rexmit = B_TRUE; 8640 tcp->tcp_dupack_cnt = 0; 8641 tcp->tcp_snd_burst = TCP_CWND_SS; 8642 tcp_ss_rexmit(tcp); 8643 } 8644 break; 8645 8646 case ICMP6_DST_UNREACH: 8647 switch (icmp6->icmp6_code) { 8648 case ICMP6_DST_UNREACH_NOPORT: 8649 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8650 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8651 (seg_seq == tcp->tcp_iss)) { 8652 (void) tcp_clean_death(tcp, 8653 ECONNREFUSED, 8); 8654 } 8655 break; 8656 8657 case ICMP6_DST_UNREACH_ADMIN: 8658 case ICMP6_DST_UNREACH_NOROUTE: 8659 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8660 case ICMP6_DST_UNREACH_ADDR: 8661 /* Record the error in case we finally time out. */ 8662 tcp->tcp_client_errno = EHOSTUNREACH; 8663 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8664 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8665 (seg_seq == tcp->tcp_iss)) { 8666 if (tcp->tcp_listener != NULL && 8667 tcp->tcp_listener->tcp_syn_defense) { 8668 /* 8669 * Ditch the half-open connection if we 8670 * suspect a SYN attack is under way. 8671 */ 8672 tcp_ip_ire_mark_advice(tcp); 8673 (void) tcp_clean_death(tcp, 8674 tcp->tcp_client_errno, 9); 8675 } 8676 } 8677 8678 8679 break; 8680 default: 8681 break; 8682 } 8683 break; 8684 8685 case ICMP6_PARAM_PROB: 8686 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8687 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8688 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8689 (uchar_t *)nexthdrp) { 8690 if (tcp->tcp_state == TCPS_SYN_SENT || 8691 tcp->tcp_state == TCPS_SYN_RCVD) { 8692 (void) tcp_clean_death(tcp, 8693 ECONNREFUSED, 10); 8694 } 8695 break; 8696 } 8697 break; 8698 8699 case ICMP6_TIME_EXCEEDED: 8700 default: 8701 break; 8702 } 8703 freemsg(first_mp); 8704 } 8705 8706 /* 8707 * Notify IP that we are having trouble with this connection. IP should 8708 * blow the IRE away and start over. 8709 */ 8710 static void 8711 tcp_ip_notify(tcp_t *tcp) 8712 { 8713 struct iocblk *iocp; 8714 ipid_t *ipid; 8715 mblk_t *mp; 8716 8717 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8718 if (tcp->tcp_ipversion == IPV6_VERSION) 8719 return; 8720 8721 mp = mkiocb(IP_IOCTL); 8722 if (mp == NULL) 8723 return; 8724 8725 iocp = (struct iocblk *)mp->b_rptr; 8726 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8727 8728 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8729 if (!mp->b_cont) { 8730 freeb(mp); 8731 return; 8732 } 8733 8734 ipid = (ipid_t *)mp->b_cont->b_rptr; 8735 mp->b_cont->b_wptr += iocp->ioc_count; 8736 bzero(ipid, sizeof (*ipid)); 8737 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8738 ipid->ipid_ire_type = IRE_CACHE; 8739 ipid->ipid_addr_offset = sizeof (ipid_t); 8740 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8741 /* 8742 * Note: in the case of source routing we want to blow away the 8743 * route to the first source route hop. 8744 */ 8745 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8746 sizeof (tcp->tcp_ipha->ipha_dst)); 8747 8748 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8749 } 8750 8751 /* Unlink and return any mblk that looks like it contains an ire */ 8752 static mblk_t * 8753 tcp_ire_mp(mblk_t **mpp) 8754 { 8755 mblk_t *mp = *mpp; 8756 mblk_t *prev_mp = NULL; 8757 8758 for (;;) { 8759 switch (DB_TYPE(mp)) { 8760 case IRE_DB_TYPE: 8761 case IRE_DB_REQ_TYPE: 8762 if (mp == *mpp) { 8763 *mpp = mp->b_cont; 8764 } else { 8765 prev_mp->b_cont = mp->b_cont; 8766 } 8767 mp->b_cont = NULL; 8768 return (mp); 8769 default: 8770 break; 8771 } 8772 prev_mp = mp; 8773 mp = mp->b_cont; 8774 if (mp == NULL) 8775 break; 8776 } 8777 return (mp); 8778 } 8779 8780 /* 8781 * Timer callback routine for keepalive probe. We do a fake resend of 8782 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8783 * check to see if we have heard anything from the other end for the last 8784 * RTO period. If we have, set the timer to expire for another 8785 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8786 * RTO << 1 and check again when it expires. Keep exponentially increasing 8787 * the timeout if we have not heard from the other side. If for more than 8788 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8789 * kill the connection unless the keepalive abort threshold is 0. In 8790 * that case, we will probe "forever." 8791 */ 8792 static void 8793 tcp_keepalive_killer(void *arg) 8794 { 8795 mblk_t *mp; 8796 conn_t *connp = (conn_t *)arg; 8797 tcp_t *tcp = connp->conn_tcp; 8798 int32_t firetime; 8799 int32_t idletime; 8800 int32_t ka_intrvl; 8801 tcp_stack_t *tcps = tcp->tcp_tcps; 8802 8803 tcp->tcp_ka_tid = 0; 8804 8805 if (tcp->tcp_fused) 8806 return; 8807 8808 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8809 ka_intrvl = tcp->tcp_ka_interval; 8810 8811 /* 8812 * Keepalive probe should only be sent if the application has not 8813 * done a close on the connection. 8814 */ 8815 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8816 return; 8817 } 8818 /* Timer fired too early, restart it. */ 8819 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8820 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8821 MSEC_TO_TICK(ka_intrvl)); 8822 return; 8823 } 8824 8825 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8826 /* 8827 * If we have not heard from the other side for a long 8828 * time, kill the connection unless the keepalive abort 8829 * threshold is 0. In that case, we will probe "forever." 8830 */ 8831 if (tcp->tcp_ka_abort_thres != 0 && 8832 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8833 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8834 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8835 tcp->tcp_client_errno : ETIMEDOUT, 11); 8836 return; 8837 } 8838 8839 if (tcp->tcp_snxt == tcp->tcp_suna && 8840 idletime >= ka_intrvl) { 8841 /* Fake resend of last ACKed byte. */ 8842 mblk_t *mp1 = allocb(1, BPRI_LO); 8843 8844 if (mp1 != NULL) { 8845 *mp1->b_wptr++ = '\0'; 8846 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8847 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8848 freeb(mp1); 8849 /* 8850 * if allocation failed, fall through to start the 8851 * timer back. 8852 */ 8853 if (mp != NULL) { 8854 tcp_send_data(tcp, tcp->tcp_wq, mp); 8855 BUMP_MIB(&tcps->tcps_mib, 8856 tcpTimKeepaliveProbe); 8857 if (tcp->tcp_ka_last_intrvl != 0) { 8858 int max; 8859 /* 8860 * We should probe again at least 8861 * in ka_intrvl, but not more than 8862 * tcp_rexmit_interval_max. 8863 */ 8864 max = tcps->tcps_rexmit_interval_max; 8865 firetime = MIN(ka_intrvl - 1, 8866 tcp->tcp_ka_last_intrvl << 1); 8867 if (firetime > max) 8868 firetime = max; 8869 } else { 8870 firetime = tcp->tcp_rto; 8871 } 8872 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8873 tcp_keepalive_killer, 8874 MSEC_TO_TICK(firetime)); 8875 tcp->tcp_ka_last_intrvl = firetime; 8876 return; 8877 } 8878 } 8879 } else { 8880 tcp->tcp_ka_last_intrvl = 0; 8881 } 8882 8883 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8884 if ((firetime = ka_intrvl - idletime) < 0) { 8885 firetime = ka_intrvl; 8886 } 8887 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8888 MSEC_TO_TICK(firetime)); 8889 } 8890 8891 int 8892 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8893 { 8894 queue_t *q = tcp->tcp_rq; 8895 int32_t mss = tcp->tcp_mss; 8896 int maxpsz; 8897 conn_t *connp = tcp->tcp_connp; 8898 8899 if (TCP_IS_DETACHED(tcp)) 8900 return (mss); 8901 if (tcp->tcp_fused) { 8902 maxpsz = tcp_fuse_maxpsz_set(tcp); 8903 mss = INFPSZ; 8904 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8905 /* 8906 * Set the sd_qn_maxpsz according to the socket send buffer 8907 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8908 * instruct the stream head to copyin user data into contiguous 8909 * kernel-allocated buffers without breaking it up into smaller 8910 * chunks. We round up the buffer size to the nearest SMSS. 8911 */ 8912 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8913 if (tcp->tcp_kssl_ctx == NULL) 8914 mss = INFPSZ; 8915 else 8916 mss = SSL3_MAX_RECORD_LEN; 8917 } else { 8918 /* 8919 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8920 * (and a multiple of the mss). This instructs the stream 8921 * head to break down larger than SMSS writes into SMSS- 8922 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8923 */ 8924 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8925 maxpsz = tcp->tcp_maxpsz * mss; 8926 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8927 maxpsz = tcp->tcp_xmit_hiwater/2; 8928 /* Round up to nearest mss */ 8929 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8930 } 8931 } 8932 8933 (void) proto_set_maxpsz(q, connp, maxpsz); 8934 if (!(IPCL_IS_NONSTR(connp))) { 8935 /* XXX do it in set_maxpsz()? */ 8936 tcp->tcp_wq->q_maxpsz = maxpsz; 8937 } 8938 8939 if (set_maxblk) 8940 (void) proto_set_tx_maxblk(q, connp, mss); 8941 return (mss); 8942 } 8943 8944 /* 8945 * Extract option values from a tcp header. We put any found values into the 8946 * tcpopt struct and return a bitmask saying which options were found. 8947 */ 8948 static int 8949 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8950 { 8951 uchar_t *endp; 8952 int len; 8953 uint32_t mss; 8954 uchar_t *up = (uchar_t *)tcph; 8955 int found = 0; 8956 int32_t sack_len; 8957 tcp_seq sack_begin, sack_end; 8958 tcp_t *tcp; 8959 8960 endp = up + TCP_HDR_LENGTH(tcph); 8961 up += TCP_MIN_HEADER_LENGTH; 8962 while (up < endp) { 8963 len = endp - up; 8964 switch (*up) { 8965 case TCPOPT_EOL: 8966 break; 8967 8968 case TCPOPT_NOP: 8969 up++; 8970 continue; 8971 8972 case TCPOPT_MAXSEG: 8973 if (len < TCPOPT_MAXSEG_LEN || 8974 up[1] != TCPOPT_MAXSEG_LEN) 8975 break; 8976 8977 mss = BE16_TO_U16(up+2); 8978 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8979 tcpopt->tcp_opt_mss = mss; 8980 found |= TCP_OPT_MSS_PRESENT; 8981 8982 up += TCPOPT_MAXSEG_LEN; 8983 continue; 8984 8985 case TCPOPT_WSCALE: 8986 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8987 break; 8988 8989 if (up[2] > TCP_MAX_WINSHIFT) 8990 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8991 else 8992 tcpopt->tcp_opt_wscale = up[2]; 8993 found |= TCP_OPT_WSCALE_PRESENT; 8994 8995 up += TCPOPT_WS_LEN; 8996 continue; 8997 8998 case TCPOPT_SACK_PERMITTED: 8999 if (len < TCPOPT_SACK_OK_LEN || 9000 up[1] != TCPOPT_SACK_OK_LEN) 9001 break; 9002 found |= TCP_OPT_SACK_OK_PRESENT; 9003 up += TCPOPT_SACK_OK_LEN; 9004 continue; 9005 9006 case TCPOPT_SACK: 9007 if (len <= 2 || up[1] <= 2 || len < up[1]) 9008 break; 9009 9010 /* If TCP is not interested in SACK blks... */ 9011 if ((tcp = tcpopt->tcp) == NULL) { 9012 up += up[1]; 9013 continue; 9014 } 9015 sack_len = up[1] - TCPOPT_HEADER_LEN; 9016 up += TCPOPT_HEADER_LEN; 9017 9018 /* 9019 * If the list is empty, allocate one and assume 9020 * nothing is sack'ed. 9021 */ 9022 ASSERT(tcp->tcp_sack_info != NULL); 9023 if (tcp->tcp_notsack_list == NULL) { 9024 tcp_notsack_update(&(tcp->tcp_notsack_list), 9025 tcp->tcp_suna, tcp->tcp_snxt, 9026 &(tcp->tcp_num_notsack_blk), 9027 &(tcp->tcp_cnt_notsack_list)); 9028 9029 /* 9030 * Make sure tcp_notsack_list is not NULL. 9031 * This happens when kmem_alloc(KM_NOSLEEP) 9032 * returns NULL. 9033 */ 9034 if (tcp->tcp_notsack_list == NULL) { 9035 up += sack_len; 9036 continue; 9037 } 9038 tcp->tcp_fack = tcp->tcp_suna; 9039 } 9040 9041 while (sack_len > 0) { 9042 if (up + 8 > endp) { 9043 up = endp; 9044 break; 9045 } 9046 sack_begin = BE32_TO_U32(up); 9047 up += 4; 9048 sack_end = BE32_TO_U32(up); 9049 up += 4; 9050 sack_len -= 8; 9051 /* 9052 * Bounds checking. Make sure the SACK 9053 * info is within tcp_suna and tcp_snxt. 9054 * If this SACK blk is out of bound, ignore 9055 * it but continue to parse the following 9056 * blks. 9057 */ 9058 if (SEQ_LEQ(sack_end, sack_begin) || 9059 SEQ_LT(sack_begin, tcp->tcp_suna) || 9060 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9061 continue; 9062 } 9063 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9064 sack_begin, sack_end, 9065 &(tcp->tcp_num_notsack_blk), 9066 &(tcp->tcp_cnt_notsack_list)); 9067 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9068 tcp->tcp_fack = sack_end; 9069 } 9070 } 9071 found |= TCP_OPT_SACK_PRESENT; 9072 continue; 9073 9074 case TCPOPT_TSTAMP: 9075 if (len < TCPOPT_TSTAMP_LEN || 9076 up[1] != TCPOPT_TSTAMP_LEN) 9077 break; 9078 9079 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9080 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9081 9082 found |= TCP_OPT_TSTAMP_PRESENT; 9083 9084 up += TCPOPT_TSTAMP_LEN; 9085 continue; 9086 9087 default: 9088 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9089 break; 9090 up += up[1]; 9091 continue; 9092 } 9093 break; 9094 } 9095 return (found); 9096 } 9097 9098 /* 9099 * Set the mss associated with a particular tcp based on its current value, 9100 * and a new one passed in. Observe minimums and maximums, and reset 9101 * other state variables that we want to view as multiples of mss. 9102 * 9103 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9104 * highwater marks etc. need to be initialized or adjusted. 9105 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9106 * packet arrives. 9107 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9108 * ICMP6_PACKET_TOO_BIG arrives. 9109 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9110 * to increase the MSS to use the extra bytes available. 9111 * 9112 * Callers except tcp_paws_check() ensure that they only reduce mss. 9113 */ 9114 static void 9115 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9116 { 9117 uint32_t mss_max; 9118 tcp_stack_t *tcps = tcp->tcp_tcps; 9119 9120 if (tcp->tcp_ipversion == IPV4_VERSION) 9121 mss_max = tcps->tcps_mss_max_ipv4; 9122 else 9123 mss_max = tcps->tcps_mss_max_ipv6; 9124 9125 if (mss < tcps->tcps_mss_min) 9126 mss = tcps->tcps_mss_min; 9127 if (mss > mss_max) 9128 mss = mss_max; 9129 /* 9130 * Unless naglim has been set by our client to 9131 * a non-mss value, force naglim to track mss. 9132 * This can help to aggregate small writes. 9133 */ 9134 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9135 tcp->tcp_naglim = mss; 9136 /* 9137 * TCP should be able to buffer at least 4 MSS data for obvious 9138 * performance reason. 9139 */ 9140 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9141 tcp->tcp_xmit_hiwater = mss << 2; 9142 9143 if (do_ss) { 9144 /* 9145 * Either the tcp_cwnd is as yet uninitialized, or mss is 9146 * changing due to a reduction in MTU, presumably as a 9147 * result of a new path component, reset cwnd to its 9148 * "initial" value, as a multiple of the new mss. 9149 */ 9150 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9151 } else { 9152 /* 9153 * Called by tcp_paws_check(), the mss increased 9154 * marginally to allow use of space previously taken 9155 * by the timestamp option. It would be inappropriate 9156 * to apply slow start or tcp_init_cwnd values to 9157 * tcp_cwnd, simply adjust to a multiple of the new mss. 9158 */ 9159 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9160 tcp->tcp_cwnd_cnt = 0; 9161 } 9162 tcp->tcp_mss = mss; 9163 (void) tcp_maxpsz_set(tcp, B_TRUE); 9164 } 9165 9166 /* For /dev/tcp aka AF_INET open */ 9167 static int 9168 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9169 { 9170 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9171 } 9172 9173 /* For /dev/tcp6 aka AF_INET6 open */ 9174 static int 9175 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9176 { 9177 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9178 } 9179 9180 static conn_t * 9181 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9182 boolean_t issocket, int *errorp) 9183 { 9184 tcp_t *tcp = NULL; 9185 conn_t *connp; 9186 int err; 9187 zoneid_t zoneid; 9188 tcp_stack_t *tcps; 9189 squeue_t *sqp; 9190 9191 ASSERT(errorp != NULL); 9192 /* 9193 * Find the proper zoneid and netstack. 9194 */ 9195 /* 9196 * Special case for install: miniroot needs to be able to 9197 * access files via NFS as though it were always in the 9198 * global zone. 9199 */ 9200 if (credp == kcred && nfs_global_client_only != 0) { 9201 zoneid = GLOBAL_ZONEID; 9202 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9203 netstack_tcp; 9204 ASSERT(tcps != NULL); 9205 } else { 9206 netstack_t *ns; 9207 9208 ns = netstack_find_by_cred(credp); 9209 ASSERT(ns != NULL); 9210 tcps = ns->netstack_tcp; 9211 ASSERT(tcps != NULL); 9212 9213 /* 9214 * For exclusive stacks we set the zoneid to zero 9215 * to make TCP operate as if in the global zone. 9216 */ 9217 if (tcps->tcps_netstack->netstack_stackid != 9218 GLOBAL_NETSTACKID) 9219 zoneid = GLOBAL_ZONEID; 9220 else 9221 zoneid = crgetzoneid(credp); 9222 } 9223 /* 9224 * For stackid zero this is done from strplumb.c, but 9225 * non-zero stackids are handled here. 9226 */ 9227 if (tcps->tcps_g_q == NULL && 9228 tcps->tcps_netstack->netstack_stackid != 9229 GLOBAL_NETSTACKID) { 9230 tcp_g_q_setup(tcps); 9231 } 9232 9233 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9234 connp = (conn_t *)tcp_get_conn(sqp, tcps, q != NULL ? B_TRUE : B_FALSE); 9235 /* 9236 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9237 * so we drop it by one. 9238 */ 9239 netstack_rele(tcps->tcps_netstack); 9240 if (connp == NULL) { 9241 *errorp = ENOSR; 9242 return (NULL); 9243 } 9244 connp->conn_sqp = sqp; 9245 connp->conn_initial_sqp = connp->conn_sqp; 9246 tcp = connp->conn_tcp; 9247 9248 if (isv6) { 9249 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9250 connp->conn_send = ip_output_v6; 9251 connp->conn_af_isv6 = B_TRUE; 9252 connp->conn_pkt_isv6 = B_TRUE; 9253 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9254 tcp->tcp_ipversion = IPV6_VERSION; 9255 tcp->tcp_family = AF_INET6; 9256 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9257 } else { 9258 connp->conn_flags |= IPCL_TCP4; 9259 connp->conn_send = ip_output; 9260 connp->conn_af_isv6 = B_FALSE; 9261 connp->conn_pkt_isv6 = B_FALSE; 9262 tcp->tcp_ipversion = IPV4_VERSION; 9263 tcp->tcp_family = AF_INET; 9264 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9265 } 9266 9267 /* 9268 * TCP keeps a copy of cred for cache locality reasons but 9269 * we put a reference only once. If connp->conn_cred 9270 * becomes invalid, tcp_cred should also be set to NULL. 9271 */ 9272 tcp->tcp_cred = connp->conn_cred = credp; 9273 crhold(connp->conn_cred); 9274 tcp->tcp_cpid = curproc->p_pid; 9275 tcp->tcp_open_time = lbolt64; 9276 connp->conn_zoneid = zoneid; 9277 connp->conn_mlp_type = mlptSingle; 9278 connp->conn_ulp_labeled = !is_system_labeled(); 9279 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9280 ASSERT(tcp->tcp_tcps == tcps); 9281 9282 /* 9283 * If the caller has the process-wide flag set, then default to MAC 9284 * exempt mode. This allows read-down to unlabeled hosts. 9285 */ 9286 if (getpflags(NET_MAC_AWARE, credp) != 0) 9287 connp->conn_mac_exempt = B_TRUE; 9288 9289 connp->conn_dev = NULL; 9290 if (issocket) { 9291 connp->conn_flags |= IPCL_SOCKET; 9292 tcp->tcp_issocket = 1; 9293 } 9294 9295 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9296 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9297 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9298 9299 /* Non-zero default values */ 9300 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9301 9302 if (q == NULL) { 9303 /* 9304 * Create a helper stream for non-STREAMS socket. 9305 */ 9306 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9307 if (err != 0) { 9308 ip1dbg(("tcp_create_common: create of IP helper stream " 9309 "failed\n")); 9310 CONN_DEC_REF(connp); 9311 *errorp = err; 9312 return (NULL); 9313 } 9314 q = connp->conn_rq; 9315 } else { 9316 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9317 } 9318 9319 SOCK_CONNID_INIT(tcp->tcp_connid); 9320 err = tcp_init(tcp, q); 9321 if (err != 0) { 9322 CONN_DEC_REF(connp); 9323 *errorp = err; 9324 return (NULL); 9325 } 9326 9327 return (connp); 9328 } 9329 9330 static int 9331 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9332 boolean_t isv6) 9333 { 9334 tcp_t *tcp = NULL; 9335 conn_t *connp = NULL; 9336 int err; 9337 vmem_t *minor_arena = NULL; 9338 dev_t conn_dev; 9339 boolean_t issocket; 9340 9341 if (q->q_ptr != NULL) 9342 return (0); 9343 9344 if (sflag == MODOPEN) 9345 return (EINVAL); 9346 9347 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9348 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9349 minor_arena = ip_minor_arena_la; 9350 } else { 9351 /* 9352 * Either minor numbers in the large arena were exhausted 9353 * or a non socket application is doing the open. 9354 * Try to allocate from the small arena. 9355 */ 9356 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9357 return (EBUSY); 9358 } 9359 minor_arena = ip_minor_arena_sa; 9360 } 9361 9362 ASSERT(minor_arena != NULL); 9363 9364 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9365 9366 if (flag & SO_FALLBACK) { 9367 /* 9368 * Non streams socket needs a stream to fallback to 9369 */ 9370 RD(q)->q_ptr = (void *)conn_dev; 9371 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9372 WR(q)->q_ptr = (void *)minor_arena; 9373 qprocson(q); 9374 return (0); 9375 } else if (flag & SO_ACCEPTOR) { 9376 q->q_qinfo = &tcp_acceptor_rinit; 9377 /* 9378 * the conn_dev and minor_arena will be subsequently used by 9379 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9380 * the minor device number for this connection from the q_ptr. 9381 */ 9382 RD(q)->q_ptr = (void *)conn_dev; 9383 WR(q)->q_qinfo = &tcp_acceptor_winit; 9384 WR(q)->q_ptr = (void *)minor_arena; 9385 qprocson(q); 9386 return (0); 9387 } 9388 9389 issocket = flag & SO_SOCKSTR; 9390 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9391 9392 if (connp == NULL) { 9393 inet_minor_free(minor_arena, conn_dev); 9394 q->q_ptr = WR(q)->q_ptr = NULL; 9395 return (err); 9396 } 9397 9398 q->q_ptr = WR(q)->q_ptr = connp; 9399 9400 connp->conn_dev = conn_dev; 9401 connp->conn_minor_arena = minor_arena; 9402 9403 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9404 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9405 9406 tcp = connp->conn_tcp; 9407 9408 if (issocket) { 9409 WR(q)->q_qinfo = &tcp_sock_winit; 9410 } else { 9411 #ifdef _ILP32 9412 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9413 #else 9414 tcp->tcp_acceptor_id = conn_dev; 9415 #endif /* _ILP32 */ 9416 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9417 } 9418 9419 /* 9420 * Put the ref for TCP. Ref for IP was already put 9421 * by ipcl_conn_create. Also Make the conn_t globally 9422 * visible to walkers 9423 */ 9424 mutex_enter(&connp->conn_lock); 9425 CONN_INC_REF_LOCKED(connp); 9426 ASSERT(connp->conn_ref == 2); 9427 connp->conn_state_flags &= ~CONN_INCIPIENT; 9428 mutex_exit(&connp->conn_lock); 9429 9430 qprocson(q); 9431 return (0); 9432 } 9433 9434 /* 9435 * Some TCP options can be "set" by requesting them in the option 9436 * buffer. This is needed for XTI feature test though we do not 9437 * allow it in general. We interpret that this mechanism is more 9438 * applicable to OSI protocols and need not be allowed in general. 9439 * This routine filters out options for which it is not allowed (most) 9440 * and lets through those (few) for which it is. [ The XTI interface 9441 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9442 * ever implemented will have to be allowed here ]. 9443 */ 9444 static boolean_t 9445 tcp_allow_connopt_set(int level, int name) 9446 { 9447 9448 switch (level) { 9449 case IPPROTO_TCP: 9450 switch (name) { 9451 case TCP_NODELAY: 9452 return (B_TRUE); 9453 default: 9454 return (B_FALSE); 9455 } 9456 /*NOTREACHED*/ 9457 default: 9458 return (B_FALSE); 9459 } 9460 /*NOTREACHED*/ 9461 } 9462 9463 /* 9464 * this routine gets default values of certain options whose default 9465 * values are maintained by protocol specific code 9466 */ 9467 /* ARGSUSED */ 9468 int 9469 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9470 { 9471 int32_t *i1 = (int32_t *)ptr; 9472 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9473 9474 switch (level) { 9475 case IPPROTO_TCP: 9476 switch (name) { 9477 case TCP_NOTIFY_THRESHOLD: 9478 *i1 = tcps->tcps_ip_notify_interval; 9479 break; 9480 case TCP_ABORT_THRESHOLD: 9481 *i1 = tcps->tcps_ip_abort_interval; 9482 break; 9483 case TCP_CONN_NOTIFY_THRESHOLD: 9484 *i1 = tcps->tcps_ip_notify_cinterval; 9485 break; 9486 case TCP_CONN_ABORT_THRESHOLD: 9487 *i1 = tcps->tcps_ip_abort_cinterval; 9488 break; 9489 default: 9490 return (-1); 9491 } 9492 break; 9493 case IPPROTO_IP: 9494 switch (name) { 9495 case IP_TTL: 9496 *i1 = tcps->tcps_ipv4_ttl; 9497 break; 9498 default: 9499 return (-1); 9500 } 9501 break; 9502 case IPPROTO_IPV6: 9503 switch (name) { 9504 case IPV6_UNICAST_HOPS: 9505 *i1 = tcps->tcps_ipv6_hoplimit; 9506 break; 9507 default: 9508 return (-1); 9509 } 9510 break; 9511 default: 9512 return (-1); 9513 } 9514 return (sizeof (int)); 9515 } 9516 9517 static int 9518 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9519 { 9520 int *i1 = (int *)ptr; 9521 tcp_t *tcp = connp->conn_tcp; 9522 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9523 9524 switch (level) { 9525 case SOL_SOCKET: 9526 switch (name) { 9527 case SO_LINGER: { 9528 struct linger *lgr = (struct linger *)ptr; 9529 9530 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9531 lgr->l_linger = tcp->tcp_lingertime; 9532 } 9533 return (sizeof (struct linger)); 9534 case SO_DEBUG: 9535 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9536 break; 9537 case SO_KEEPALIVE: 9538 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9539 break; 9540 case SO_DONTROUTE: 9541 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9542 break; 9543 case SO_USELOOPBACK: 9544 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9545 break; 9546 case SO_BROADCAST: 9547 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9548 break; 9549 case SO_REUSEADDR: 9550 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9551 break; 9552 case SO_OOBINLINE: 9553 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9554 break; 9555 case SO_DGRAM_ERRIND: 9556 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9557 break; 9558 case SO_TYPE: 9559 *i1 = SOCK_STREAM; 9560 break; 9561 case SO_SNDBUF: 9562 *i1 = tcp->tcp_xmit_hiwater; 9563 break; 9564 case SO_RCVBUF: 9565 *i1 = tcp->tcp_recv_hiwater; 9566 break; 9567 case SO_SND_COPYAVOID: 9568 *i1 = tcp->tcp_snd_zcopy_on ? 9569 SO_SND_COPYAVOID : 0; 9570 break; 9571 case SO_ALLZONES: 9572 *i1 = connp->conn_allzones ? 1 : 0; 9573 break; 9574 case SO_ANON_MLP: 9575 *i1 = connp->conn_anon_mlp; 9576 break; 9577 case SO_MAC_EXEMPT: 9578 *i1 = connp->conn_mac_exempt; 9579 break; 9580 case SO_EXCLBIND: 9581 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9582 break; 9583 case SO_PROTOTYPE: 9584 *i1 = IPPROTO_TCP; 9585 break; 9586 case SO_DOMAIN: 9587 *i1 = tcp->tcp_family; 9588 break; 9589 case SO_ACCEPTCONN: 9590 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9591 default: 9592 return (-1); 9593 } 9594 break; 9595 case IPPROTO_TCP: 9596 switch (name) { 9597 case TCP_NODELAY: 9598 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9599 break; 9600 case TCP_MAXSEG: 9601 *i1 = tcp->tcp_mss; 9602 break; 9603 case TCP_NOTIFY_THRESHOLD: 9604 *i1 = (int)tcp->tcp_first_timer_threshold; 9605 break; 9606 case TCP_ABORT_THRESHOLD: 9607 *i1 = tcp->tcp_second_timer_threshold; 9608 break; 9609 case TCP_CONN_NOTIFY_THRESHOLD: 9610 *i1 = tcp->tcp_first_ctimer_threshold; 9611 break; 9612 case TCP_CONN_ABORT_THRESHOLD: 9613 *i1 = tcp->tcp_second_ctimer_threshold; 9614 break; 9615 case TCP_RECVDSTADDR: 9616 *i1 = tcp->tcp_recvdstaddr; 9617 break; 9618 case TCP_ANONPRIVBIND: 9619 *i1 = tcp->tcp_anon_priv_bind; 9620 break; 9621 case TCP_EXCLBIND: 9622 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9623 break; 9624 case TCP_INIT_CWND: 9625 *i1 = tcp->tcp_init_cwnd; 9626 break; 9627 case TCP_KEEPALIVE_THRESHOLD: 9628 *i1 = tcp->tcp_ka_interval; 9629 break; 9630 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9631 *i1 = tcp->tcp_ka_abort_thres; 9632 break; 9633 case TCP_CORK: 9634 *i1 = tcp->tcp_cork; 9635 break; 9636 default: 9637 return (-1); 9638 } 9639 break; 9640 case IPPROTO_IP: 9641 if (tcp->tcp_family != AF_INET) 9642 return (-1); 9643 switch (name) { 9644 case IP_OPTIONS: 9645 case T_IP_OPTIONS: { 9646 /* 9647 * This is compatible with BSD in that in only return 9648 * the reverse source route with the final destination 9649 * as the last entry. The first 4 bytes of the option 9650 * will contain the final destination. 9651 */ 9652 int opt_len; 9653 9654 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9655 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9656 ASSERT(opt_len >= 0); 9657 /* Caller ensures enough space */ 9658 if (opt_len > 0) { 9659 /* 9660 * TODO: Do we have to handle getsockopt on an 9661 * initiator as well? 9662 */ 9663 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9664 } 9665 return (0); 9666 } 9667 case IP_TOS: 9668 case T_IP_TOS: 9669 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9670 break; 9671 case IP_TTL: 9672 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9673 break; 9674 case IP_NEXTHOP: 9675 /* Handled at IP level */ 9676 return (-EINVAL); 9677 default: 9678 return (-1); 9679 } 9680 break; 9681 case IPPROTO_IPV6: 9682 /* 9683 * IPPROTO_IPV6 options are only supported for sockets 9684 * that are using IPv6 on the wire. 9685 */ 9686 if (tcp->tcp_ipversion != IPV6_VERSION) { 9687 return (-1); 9688 } 9689 switch (name) { 9690 case IPV6_UNICAST_HOPS: 9691 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9692 break; /* goto sizeof (int) option return */ 9693 case IPV6_BOUND_IF: 9694 /* Zero if not set */ 9695 *i1 = tcp->tcp_bound_if; 9696 break; /* goto sizeof (int) option return */ 9697 case IPV6_RECVPKTINFO: 9698 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9699 *i1 = 1; 9700 else 9701 *i1 = 0; 9702 break; /* goto sizeof (int) option return */ 9703 case IPV6_RECVTCLASS: 9704 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9705 *i1 = 1; 9706 else 9707 *i1 = 0; 9708 break; /* goto sizeof (int) option return */ 9709 case IPV6_RECVHOPLIMIT: 9710 if (tcp->tcp_ipv6_recvancillary & 9711 TCP_IPV6_RECVHOPLIMIT) 9712 *i1 = 1; 9713 else 9714 *i1 = 0; 9715 break; /* goto sizeof (int) option return */ 9716 case IPV6_RECVHOPOPTS: 9717 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9718 *i1 = 1; 9719 else 9720 *i1 = 0; 9721 break; /* goto sizeof (int) option return */ 9722 case IPV6_RECVDSTOPTS: 9723 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9724 *i1 = 1; 9725 else 9726 *i1 = 0; 9727 break; /* goto sizeof (int) option return */ 9728 case _OLD_IPV6_RECVDSTOPTS: 9729 if (tcp->tcp_ipv6_recvancillary & 9730 TCP_OLD_IPV6_RECVDSTOPTS) 9731 *i1 = 1; 9732 else 9733 *i1 = 0; 9734 break; /* goto sizeof (int) option return */ 9735 case IPV6_RECVRTHDR: 9736 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9737 *i1 = 1; 9738 else 9739 *i1 = 0; 9740 break; /* goto sizeof (int) option return */ 9741 case IPV6_RECVRTHDRDSTOPTS: 9742 if (tcp->tcp_ipv6_recvancillary & 9743 TCP_IPV6_RECVRTDSTOPTS) 9744 *i1 = 1; 9745 else 9746 *i1 = 0; 9747 break; /* goto sizeof (int) option return */ 9748 case IPV6_PKTINFO: { 9749 /* XXX assumes that caller has room for max size! */ 9750 struct in6_pktinfo *pkti; 9751 9752 pkti = (struct in6_pktinfo *)ptr; 9753 if (ipp->ipp_fields & IPPF_IFINDEX) 9754 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9755 else 9756 pkti->ipi6_ifindex = 0; 9757 if (ipp->ipp_fields & IPPF_ADDR) 9758 pkti->ipi6_addr = ipp->ipp_addr; 9759 else 9760 pkti->ipi6_addr = ipv6_all_zeros; 9761 return (sizeof (struct in6_pktinfo)); 9762 } 9763 case IPV6_TCLASS: 9764 if (ipp->ipp_fields & IPPF_TCLASS) 9765 *i1 = ipp->ipp_tclass; 9766 else 9767 *i1 = IPV6_FLOW_TCLASS( 9768 IPV6_DEFAULT_VERS_AND_FLOW); 9769 break; /* goto sizeof (int) option return */ 9770 case IPV6_NEXTHOP: { 9771 sin6_t *sin6 = (sin6_t *)ptr; 9772 9773 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9774 return (0); 9775 *sin6 = sin6_null; 9776 sin6->sin6_family = AF_INET6; 9777 sin6->sin6_addr = ipp->ipp_nexthop; 9778 return (sizeof (sin6_t)); 9779 } 9780 case IPV6_HOPOPTS: 9781 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9782 return (0); 9783 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9784 return (0); 9785 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9786 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9787 if (tcp->tcp_label_len > 0) { 9788 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9789 ptr[1] = (ipp->ipp_hopoptslen - 9790 tcp->tcp_label_len + 7) / 8 - 1; 9791 } 9792 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9793 case IPV6_RTHDRDSTOPTS: 9794 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9795 return (0); 9796 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9797 return (ipp->ipp_rtdstoptslen); 9798 case IPV6_RTHDR: 9799 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9800 return (0); 9801 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9802 return (ipp->ipp_rthdrlen); 9803 case IPV6_DSTOPTS: 9804 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9805 return (0); 9806 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9807 return (ipp->ipp_dstoptslen); 9808 case IPV6_SRC_PREFERENCES: 9809 return (ip6_get_src_preferences(connp, 9810 (uint32_t *)ptr)); 9811 case IPV6_PATHMTU: { 9812 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9813 9814 if (tcp->tcp_state < TCPS_ESTABLISHED) 9815 return (-1); 9816 9817 return (ip_fill_mtuinfo(&connp->conn_remv6, 9818 connp->conn_fport, mtuinfo, 9819 connp->conn_netstack)); 9820 } 9821 default: 9822 return (-1); 9823 } 9824 break; 9825 default: 9826 return (-1); 9827 } 9828 return (sizeof (int)); 9829 } 9830 9831 /* 9832 * TCP routine to get the values of options. 9833 */ 9834 int 9835 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9836 { 9837 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9838 } 9839 9840 /* returns UNIX error, the optlen is a value-result arg */ 9841 int 9842 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9843 void *optvalp, socklen_t *optlen, cred_t *cr) 9844 { 9845 conn_t *connp = (conn_t *)proto_handle; 9846 squeue_t *sqp = connp->conn_sqp; 9847 int error; 9848 t_uscalar_t max_optbuf_len; 9849 void *optvalp_buf; 9850 int len; 9851 9852 ASSERT(connp->conn_upper_handle != NULL); 9853 9854 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9855 tcp_opt_obj.odb_opt_des_arr, 9856 tcp_opt_obj.odb_opt_arr_cnt, 9857 tcp_opt_obj.odb_topmost_tpiprovider, 9858 B_FALSE, B_TRUE, cr); 9859 if (error != 0) { 9860 if (error < 0) { 9861 error = proto_tlitosyserr(-error); 9862 } 9863 return (error); 9864 } 9865 9866 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9867 9868 error = squeue_synch_enter(sqp, connp, 0); 9869 if (error == ENOMEM) { 9870 return (ENOMEM); 9871 } 9872 9873 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9874 squeue_synch_exit(sqp, connp); 9875 9876 if (len < 0) { 9877 /* 9878 * Pass on to IP 9879 */ 9880 kmem_free(optvalp_buf, max_optbuf_len); 9881 return (ip_get_options(connp, level, option_name, 9882 optvalp, optlen, cr)); 9883 } else { 9884 /* 9885 * update optlen and copy option value 9886 */ 9887 t_uscalar_t size = MIN(len, *optlen); 9888 bcopy(optvalp_buf, optvalp, size); 9889 bcopy(&size, optlen, sizeof (size)); 9890 9891 kmem_free(optvalp_buf, max_optbuf_len); 9892 return (0); 9893 } 9894 } 9895 9896 /* 9897 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9898 * Parameters are assumed to be verified by the caller. 9899 */ 9900 /* ARGSUSED */ 9901 int 9902 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9903 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9904 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9905 { 9906 tcp_t *tcp = connp->conn_tcp; 9907 int *i1 = (int *)invalp; 9908 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9909 boolean_t checkonly; 9910 int reterr; 9911 tcp_stack_t *tcps = tcp->tcp_tcps; 9912 9913 switch (optset_context) { 9914 case SETFN_OPTCOM_CHECKONLY: 9915 checkonly = B_TRUE; 9916 /* 9917 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9918 * inlen != 0 implies value supplied and 9919 * we have to "pretend" to set it. 9920 * inlen == 0 implies that there is no 9921 * value part in T_CHECK request and just validation 9922 * done elsewhere should be enough, we just return here. 9923 */ 9924 if (inlen == 0) { 9925 *outlenp = 0; 9926 return (0); 9927 } 9928 break; 9929 case SETFN_OPTCOM_NEGOTIATE: 9930 checkonly = B_FALSE; 9931 break; 9932 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9933 case SETFN_CONN_NEGOTIATE: 9934 checkonly = B_FALSE; 9935 /* 9936 * Negotiating local and "association-related" options 9937 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9938 * primitives is allowed by XTI, but we choose 9939 * to not implement this style negotiation for Internet 9940 * protocols (We interpret it is a must for OSI world but 9941 * optional for Internet protocols) for all options. 9942 * [ Will do only for the few options that enable test 9943 * suites that our XTI implementation of this feature 9944 * works for transports that do allow it ] 9945 */ 9946 if (!tcp_allow_connopt_set(level, name)) { 9947 *outlenp = 0; 9948 return (EINVAL); 9949 } 9950 break; 9951 default: 9952 /* 9953 * We should never get here 9954 */ 9955 *outlenp = 0; 9956 return (EINVAL); 9957 } 9958 9959 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9960 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9961 9962 /* 9963 * For TCP, we should have no ancillary data sent down 9964 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9965 * has to be zero. 9966 */ 9967 ASSERT(thisdg_attrs == NULL); 9968 9969 /* 9970 * For fixed length options, no sanity check 9971 * of passed in length is done. It is assumed *_optcom_req() 9972 * routines do the right thing. 9973 */ 9974 switch (level) { 9975 case SOL_SOCKET: 9976 switch (name) { 9977 case SO_LINGER: { 9978 struct linger *lgr = (struct linger *)invalp; 9979 9980 if (!checkonly) { 9981 if (lgr->l_onoff) { 9982 tcp->tcp_linger = 1; 9983 tcp->tcp_lingertime = lgr->l_linger; 9984 } else { 9985 tcp->tcp_linger = 0; 9986 tcp->tcp_lingertime = 0; 9987 } 9988 /* struct copy */ 9989 *(struct linger *)outvalp = *lgr; 9990 } else { 9991 if (!lgr->l_onoff) { 9992 ((struct linger *) 9993 outvalp)->l_onoff = 0; 9994 ((struct linger *) 9995 outvalp)->l_linger = 0; 9996 } else { 9997 /* struct copy */ 9998 *(struct linger *)outvalp = *lgr; 9999 } 10000 } 10001 *outlenp = sizeof (struct linger); 10002 return (0); 10003 } 10004 case SO_DEBUG: 10005 if (!checkonly) 10006 tcp->tcp_debug = onoff; 10007 break; 10008 case SO_KEEPALIVE: 10009 if (checkonly) { 10010 /* check only case */ 10011 break; 10012 } 10013 10014 if (!onoff) { 10015 if (tcp->tcp_ka_enabled) { 10016 if (tcp->tcp_ka_tid != 0) { 10017 (void) TCP_TIMER_CANCEL(tcp, 10018 tcp->tcp_ka_tid); 10019 tcp->tcp_ka_tid = 0; 10020 } 10021 tcp->tcp_ka_enabled = 0; 10022 } 10023 break; 10024 } 10025 if (!tcp->tcp_ka_enabled) { 10026 /* Crank up the keepalive timer */ 10027 tcp->tcp_ka_last_intrvl = 0; 10028 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10029 tcp_keepalive_killer, 10030 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10031 tcp->tcp_ka_enabled = 1; 10032 } 10033 break; 10034 case SO_DONTROUTE: 10035 /* 10036 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10037 * only of interest to IP. We track them here only so 10038 * that we can report their current value. 10039 */ 10040 if (!checkonly) { 10041 tcp->tcp_dontroute = onoff; 10042 tcp->tcp_connp->conn_dontroute = onoff; 10043 } 10044 break; 10045 case SO_USELOOPBACK: 10046 if (!checkonly) { 10047 tcp->tcp_useloopback = onoff; 10048 tcp->tcp_connp->conn_loopback = onoff; 10049 } 10050 break; 10051 case SO_BROADCAST: 10052 if (!checkonly) { 10053 tcp->tcp_broadcast = onoff; 10054 tcp->tcp_connp->conn_broadcast = onoff; 10055 } 10056 break; 10057 case SO_REUSEADDR: 10058 if (!checkonly) { 10059 tcp->tcp_reuseaddr = onoff; 10060 tcp->tcp_connp->conn_reuseaddr = onoff; 10061 } 10062 break; 10063 case SO_OOBINLINE: 10064 if (!checkonly) { 10065 tcp->tcp_oobinline = onoff; 10066 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10067 proto_set_rx_oob_opt(connp, onoff); 10068 } 10069 break; 10070 case SO_DGRAM_ERRIND: 10071 if (!checkonly) 10072 tcp->tcp_dgram_errind = onoff; 10073 break; 10074 case SO_SNDBUF: { 10075 if (*i1 > tcps->tcps_max_buf) { 10076 *outlenp = 0; 10077 return (ENOBUFS); 10078 } 10079 if (checkonly) 10080 break; 10081 10082 tcp->tcp_xmit_hiwater = *i1; 10083 if (tcps->tcps_snd_lowat_fraction != 0) 10084 tcp->tcp_xmit_lowater = 10085 tcp->tcp_xmit_hiwater / 10086 tcps->tcps_snd_lowat_fraction; 10087 (void) tcp_maxpsz_set(tcp, B_TRUE); 10088 /* 10089 * If we are flow-controlled, recheck the condition. 10090 * There are apps that increase SO_SNDBUF size when 10091 * flow-controlled (EWOULDBLOCK), and expect the flow 10092 * control condition to be lifted right away. 10093 */ 10094 mutex_enter(&tcp->tcp_non_sq_lock); 10095 if (tcp->tcp_flow_stopped && 10096 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10097 tcp_clrqfull(tcp); 10098 } 10099 mutex_exit(&tcp->tcp_non_sq_lock); 10100 break; 10101 } 10102 case SO_RCVBUF: 10103 if (*i1 > tcps->tcps_max_buf) { 10104 *outlenp = 0; 10105 return (ENOBUFS); 10106 } 10107 /* Silently ignore zero */ 10108 if (!checkonly && *i1 != 0) { 10109 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10110 (void) tcp_rwnd_set(tcp, *i1); 10111 } 10112 /* 10113 * XXX should we return the rwnd here 10114 * and tcp_opt_get ? 10115 */ 10116 break; 10117 case SO_SND_COPYAVOID: 10118 if (!checkonly) { 10119 /* we only allow enable at most once for now */ 10120 if (tcp->tcp_loopback || 10121 (tcp->tcp_kssl_ctx != NULL) || 10122 (!tcp->tcp_snd_zcopy_aware && 10123 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10124 *outlenp = 0; 10125 return (EOPNOTSUPP); 10126 } 10127 tcp->tcp_snd_zcopy_aware = 1; 10128 } 10129 break; 10130 case SO_RCVTIMEO: 10131 case SO_SNDTIMEO: 10132 /* 10133 * Pass these two options in order for third part 10134 * protocol usage. Here just return directly. 10135 */ 10136 return (0); 10137 case SO_ALLZONES: 10138 /* Pass option along to IP level for handling */ 10139 return (-EINVAL); 10140 case SO_ANON_MLP: 10141 /* Pass option along to IP level for handling */ 10142 return (-EINVAL); 10143 case SO_MAC_EXEMPT: 10144 /* Pass option along to IP level for handling */ 10145 return (-EINVAL); 10146 case SO_EXCLBIND: 10147 if (!checkonly) 10148 tcp->tcp_exclbind = onoff; 10149 break; 10150 default: 10151 *outlenp = 0; 10152 return (EINVAL); 10153 } 10154 break; 10155 case IPPROTO_TCP: 10156 switch (name) { 10157 case TCP_NODELAY: 10158 if (!checkonly) 10159 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10160 break; 10161 case TCP_NOTIFY_THRESHOLD: 10162 if (!checkonly) 10163 tcp->tcp_first_timer_threshold = *i1; 10164 break; 10165 case TCP_ABORT_THRESHOLD: 10166 if (!checkonly) 10167 tcp->tcp_second_timer_threshold = *i1; 10168 break; 10169 case TCP_CONN_NOTIFY_THRESHOLD: 10170 if (!checkonly) 10171 tcp->tcp_first_ctimer_threshold = *i1; 10172 break; 10173 case TCP_CONN_ABORT_THRESHOLD: 10174 if (!checkonly) 10175 tcp->tcp_second_ctimer_threshold = *i1; 10176 break; 10177 case TCP_RECVDSTADDR: 10178 if (tcp->tcp_state > TCPS_LISTEN) 10179 return (EOPNOTSUPP); 10180 if (!checkonly) 10181 tcp->tcp_recvdstaddr = onoff; 10182 break; 10183 case TCP_ANONPRIVBIND: 10184 if ((reterr = secpolicy_net_privaddr(cr, 0, 10185 IPPROTO_TCP)) != 0) { 10186 *outlenp = 0; 10187 return (reterr); 10188 } 10189 if (!checkonly) { 10190 tcp->tcp_anon_priv_bind = onoff; 10191 } 10192 break; 10193 case TCP_EXCLBIND: 10194 if (!checkonly) 10195 tcp->tcp_exclbind = onoff; 10196 break; /* goto sizeof (int) option return */ 10197 case TCP_INIT_CWND: { 10198 uint32_t init_cwnd = *((uint32_t *)invalp); 10199 10200 if (checkonly) 10201 break; 10202 10203 /* 10204 * Only allow socket with network configuration 10205 * privilege to set the initial cwnd to be larger 10206 * than allowed by RFC 3390. 10207 */ 10208 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10209 tcp->tcp_init_cwnd = init_cwnd; 10210 break; 10211 } 10212 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10213 *outlenp = 0; 10214 return (reterr); 10215 } 10216 if (init_cwnd > TCP_MAX_INIT_CWND) { 10217 *outlenp = 0; 10218 return (EINVAL); 10219 } 10220 tcp->tcp_init_cwnd = init_cwnd; 10221 break; 10222 } 10223 case TCP_KEEPALIVE_THRESHOLD: 10224 if (checkonly) 10225 break; 10226 10227 if (*i1 < tcps->tcps_keepalive_interval_low || 10228 *i1 > tcps->tcps_keepalive_interval_high) { 10229 *outlenp = 0; 10230 return (EINVAL); 10231 } 10232 if (*i1 != tcp->tcp_ka_interval) { 10233 tcp->tcp_ka_interval = *i1; 10234 /* 10235 * Check if we need to restart the 10236 * keepalive timer. 10237 */ 10238 if (tcp->tcp_ka_tid != 0) { 10239 ASSERT(tcp->tcp_ka_enabled); 10240 (void) TCP_TIMER_CANCEL(tcp, 10241 tcp->tcp_ka_tid); 10242 tcp->tcp_ka_last_intrvl = 0; 10243 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10244 tcp_keepalive_killer, 10245 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10246 } 10247 } 10248 break; 10249 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10250 if (!checkonly) { 10251 if (*i1 < 10252 tcps->tcps_keepalive_abort_interval_low || 10253 *i1 > 10254 tcps->tcps_keepalive_abort_interval_high) { 10255 *outlenp = 0; 10256 return (EINVAL); 10257 } 10258 tcp->tcp_ka_abort_thres = *i1; 10259 } 10260 break; 10261 case TCP_CORK: 10262 if (!checkonly) { 10263 /* 10264 * if tcp->tcp_cork was set and is now 10265 * being unset, we have to make sure that 10266 * the remaining data gets sent out. Also 10267 * unset tcp->tcp_cork so that tcp_wput_data() 10268 * can send data even if it is less than mss 10269 */ 10270 if (tcp->tcp_cork && onoff == 0 && 10271 tcp->tcp_unsent > 0) { 10272 tcp->tcp_cork = B_FALSE; 10273 tcp_wput_data(tcp, NULL, B_FALSE); 10274 } 10275 tcp->tcp_cork = onoff; 10276 } 10277 break; 10278 default: 10279 *outlenp = 0; 10280 return (EINVAL); 10281 } 10282 break; 10283 case IPPROTO_IP: 10284 if (tcp->tcp_family != AF_INET) { 10285 *outlenp = 0; 10286 return (ENOPROTOOPT); 10287 } 10288 switch (name) { 10289 case IP_OPTIONS: 10290 case T_IP_OPTIONS: 10291 reterr = tcp_opt_set_header(tcp, checkonly, 10292 invalp, inlen); 10293 if (reterr) { 10294 *outlenp = 0; 10295 return (reterr); 10296 } 10297 /* OK return - copy input buffer into output buffer */ 10298 if (invalp != outvalp) { 10299 /* don't trust bcopy for identical src/dst */ 10300 bcopy(invalp, outvalp, inlen); 10301 } 10302 *outlenp = inlen; 10303 return (0); 10304 case IP_TOS: 10305 case T_IP_TOS: 10306 if (!checkonly) { 10307 tcp->tcp_ipha->ipha_type_of_service = 10308 (uchar_t)*i1; 10309 tcp->tcp_tos = (uchar_t)*i1; 10310 } 10311 break; 10312 case IP_TTL: 10313 if (!checkonly) { 10314 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10315 tcp->tcp_ttl = (uchar_t)*i1; 10316 } 10317 break; 10318 case IP_BOUND_IF: 10319 case IP_NEXTHOP: 10320 /* Handled at the IP level */ 10321 return (-EINVAL); 10322 case IP_SEC_OPT: 10323 /* 10324 * We should not allow policy setting after 10325 * we start listening for connections. 10326 */ 10327 if (tcp->tcp_state == TCPS_LISTEN) { 10328 return (EINVAL); 10329 } else { 10330 /* Handled at the IP level */ 10331 return (-EINVAL); 10332 } 10333 default: 10334 *outlenp = 0; 10335 return (EINVAL); 10336 } 10337 break; 10338 case IPPROTO_IPV6: { 10339 ip6_pkt_t *ipp; 10340 10341 /* 10342 * IPPROTO_IPV6 options are only supported for sockets 10343 * that are using IPv6 on the wire. 10344 */ 10345 if (tcp->tcp_ipversion != IPV6_VERSION) { 10346 *outlenp = 0; 10347 return (ENOPROTOOPT); 10348 } 10349 /* 10350 * Only sticky options; no ancillary data 10351 */ 10352 ipp = &tcp->tcp_sticky_ipp; 10353 10354 switch (name) { 10355 case IPV6_UNICAST_HOPS: 10356 /* -1 means use default */ 10357 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10358 *outlenp = 0; 10359 return (EINVAL); 10360 } 10361 if (!checkonly) { 10362 if (*i1 == -1) { 10363 tcp->tcp_ip6h->ip6_hops = 10364 ipp->ipp_unicast_hops = 10365 (uint8_t)tcps->tcps_ipv6_hoplimit; 10366 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10367 /* Pass modified value to IP. */ 10368 *i1 = tcp->tcp_ip6h->ip6_hops; 10369 } else { 10370 tcp->tcp_ip6h->ip6_hops = 10371 ipp->ipp_unicast_hops = 10372 (uint8_t)*i1; 10373 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10374 } 10375 reterr = tcp_build_hdrs(tcp); 10376 if (reterr != 0) 10377 return (reterr); 10378 } 10379 break; 10380 case IPV6_BOUND_IF: 10381 if (!checkonly) { 10382 tcp->tcp_bound_if = *i1; 10383 PASS_OPT_TO_IP(connp); 10384 } 10385 break; 10386 /* 10387 * Set boolean switches for ancillary data delivery 10388 */ 10389 case IPV6_RECVPKTINFO: 10390 if (!checkonly) { 10391 if (onoff) 10392 tcp->tcp_ipv6_recvancillary |= 10393 TCP_IPV6_RECVPKTINFO; 10394 else 10395 tcp->tcp_ipv6_recvancillary &= 10396 ~TCP_IPV6_RECVPKTINFO; 10397 /* Force it to be sent up with the next msg */ 10398 tcp->tcp_recvifindex = 0; 10399 PASS_OPT_TO_IP(connp); 10400 } 10401 break; 10402 case IPV6_RECVTCLASS: 10403 if (!checkonly) { 10404 if (onoff) 10405 tcp->tcp_ipv6_recvancillary |= 10406 TCP_IPV6_RECVTCLASS; 10407 else 10408 tcp->tcp_ipv6_recvancillary &= 10409 ~TCP_IPV6_RECVTCLASS; 10410 PASS_OPT_TO_IP(connp); 10411 } 10412 break; 10413 case IPV6_RECVHOPLIMIT: 10414 if (!checkonly) { 10415 if (onoff) 10416 tcp->tcp_ipv6_recvancillary |= 10417 TCP_IPV6_RECVHOPLIMIT; 10418 else 10419 tcp->tcp_ipv6_recvancillary &= 10420 ~TCP_IPV6_RECVHOPLIMIT; 10421 /* Force it to be sent up with the next msg */ 10422 tcp->tcp_recvhops = 0xffffffffU; 10423 PASS_OPT_TO_IP(connp); 10424 } 10425 break; 10426 case IPV6_RECVHOPOPTS: 10427 if (!checkonly) { 10428 if (onoff) 10429 tcp->tcp_ipv6_recvancillary |= 10430 TCP_IPV6_RECVHOPOPTS; 10431 else 10432 tcp->tcp_ipv6_recvancillary &= 10433 ~TCP_IPV6_RECVHOPOPTS; 10434 PASS_OPT_TO_IP(connp); 10435 } 10436 break; 10437 case IPV6_RECVDSTOPTS: 10438 if (!checkonly) { 10439 if (onoff) 10440 tcp->tcp_ipv6_recvancillary |= 10441 TCP_IPV6_RECVDSTOPTS; 10442 else 10443 tcp->tcp_ipv6_recvancillary &= 10444 ~TCP_IPV6_RECVDSTOPTS; 10445 PASS_OPT_TO_IP(connp); 10446 } 10447 break; 10448 case _OLD_IPV6_RECVDSTOPTS: 10449 if (!checkonly) { 10450 if (onoff) 10451 tcp->tcp_ipv6_recvancillary |= 10452 TCP_OLD_IPV6_RECVDSTOPTS; 10453 else 10454 tcp->tcp_ipv6_recvancillary &= 10455 ~TCP_OLD_IPV6_RECVDSTOPTS; 10456 } 10457 break; 10458 case IPV6_RECVRTHDR: 10459 if (!checkonly) { 10460 if (onoff) 10461 tcp->tcp_ipv6_recvancillary |= 10462 TCP_IPV6_RECVRTHDR; 10463 else 10464 tcp->tcp_ipv6_recvancillary &= 10465 ~TCP_IPV6_RECVRTHDR; 10466 PASS_OPT_TO_IP(connp); 10467 } 10468 break; 10469 case IPV6_RECVRTHDRDSTOPTS: 10470 if (!checkonly) { 10471 if (onoff) 10472 tcp->tcp_ipv6_recvancillary |= 10473 TCP_IPV6_RECVRTDSTOPTS; 10474 else 10475 tcp->tcp_ipv6_recvancillary &= 10476 ~TCP_IPV6_RECVRTDSTOPTS; 10477 PASS_OPT_TO_IP(connp); 10478 } 10479 break; 10480 case IPV6_PKTINFO: 10481 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10482 return (EINVAL); 10483 if (checkonly) 10484 break; 10485 10486 if (inlen == 0) { 10487 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10488 } else { 10489 struct in6_pktinfo *pkti; 10490 10491 pkti = (struct in6_pktinfo *)invalp; 10492 /* 10493 * RFC 3542 states that ipi6_addr must be 10494 * the unspecified address when setting the 10495 * IPV6_PKTINFO sticky socket option on a 10496 * TCP socket. 10497 */ 10498 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10499 return (EINVAL); 10500 /* 10501 * IP will validate the source address and 10502 * interface index. 10503 */ 10504 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10505 reterr = ip_set_options(tcp->tcp_connp, 10506 level, name, invalp, inlen, cr); 10507 } else { 10508 reterr = ip6_set_pktinfo(cr, 10509 tcp->tcp_connp, pkti); 10510 } 10511 if (reterr != 0) 10512 return (reterr); 10513 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10514 ipp->ipp_addr = pkti->ipi6_addr; 10515 if (ipp->ipp_ifindex != 0) 10516 ipp->ipp_fields |= IPPF_IFINDEX; 10517 else 10518 ipp->ipp_fields &= ~IPPF_IFINDEX; 10519 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10520 ipp->ipp_fields |= IPPF_ADDR; 10521 else 10522 ipp->ipp_fields &= ~IPPF_ADDR; 10523 } 10524 reterr = tcp_build_hdrs(tcp); 10525 if (reterr != 0) 10526 return (reterr); 10527 break; 10528 case IPV6_TCLASS: 10529 if (inlen != 0 && inlen != sizeof (int)) 10530 return (EINVAL); 10531 if (checkonly) 10532 break; 10533 10534 if (inlen == 0) { 10535 ipp->ipp_fields &= ~IPPF_TCLASS; 10536 } else { 10537 if (*i1 > 255 || *i1 < -1) 10538 return (EINVAL); 10539 if (*i1 == -1) { 10540 ipp->ipp_tclass = 0; 10541 *i1 = 0; 10542 } else { 10543 ipp->ipp_tclass = *i1; 10544 } 10545 ipp->ipp_fields |= IPPF_TCLASS; 10546 } 10547 reterr = tcp_build_hdrs(tcp); 10548 if (reterr != 0) 10549 return (reterr); 10550 break; 10551 case IPV6_NEXTHOP: 10552 /* 10553 * IP will verify that the nexthop is reachable 10554 * and fail for sticky options. 10555 */ 10556 if (inlen != 0 && inlen != sizeof (sin6_t)) 10557 return (EINVAL); 10558 if (checkonly) 10559 break; 10560 10561 if (inlen == 0) { 10562 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10563 } else { 10564 sin6_t *sin6 = (sin6_t *)invalp; 10565 10566 if (sin6->sin6_family != AF_INET6) 10567 return (EAFNOSUPPORT); 10568 if (IN6_IS_ADDR_V4MAPPED( 10569 &sin6->sin6_addr)) 10570 return (EADDRNOTAVAIL); 10571 ipp->ipp_nexthop = sin6->sin6_addr; 10572 if (!IN6_IS_ADDR_UNSPECIFIED( 10573 &ipp->ipp_nexthop)) 10574 ipp->ipp_fields |= IPPF_NEXTHOP; 10575 else 10576 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10577 } 10578 reterr = tcp_build_hdrs(tcp); 10579 if (reterr != 0) 10580 return (reterr); 10581 PASS_OPT_TO_IP(connp); 10582 break; 10583 case IPV6_HOPOPTS: { 10584 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10585 10586 /* 10587 * Sanity checks - minimum size, size a multiple of 10588 * eight bytes, and matching size passed in. 10589 */ 10590 if (inlen != 0 && 10591 inlen != (8 * (hopts->ip6h_len + 1))) 10592 return (EINVAL); 10593 10594 if (checkonly) 10595 break; 10596 10597 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10598 (uchar_t **)&ipp->ipp_hopopts, 10599 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10600 if (reterr != 0) 10601 return (reterr); 10602 if (ipp->ipp_hopoptslen == 0) 10603 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10604 else 10605 ipp->ipp_fields |= IPPF_HOPOPTS; 10606 reterr = tcp_build_hdrs(tcp); 10607 if (reterr != 0) 10608 return (reterr); 10609 break; 10610 } 10611 case IPV6_RTHDRDSTOPTS: { 10612 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10613 10614 /* 10615 * Sanity checks - minimum size, size a multiple of 10616 * eight bytes, and matching size passed in. 10617 */ 10618 if (inlen != 0 && 10619 inlen != (8 * (dopts->ip6d_len + 1))) 10620 return (EINVAL); 10621 10622 if (checkonly) 10623 break; 10624 10625 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10626 (uchar_t **)&ipp->ipp_rtdstopts, 10627 &ipp->ipp_rtdstoptslen, 0); 10628 if (reterr != 0) 10629 return (reterr); 10630 if (ipp->ipp_rtdstoptslen == 0) 10631 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10632 else 10633 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10634 reterr = tcp_build_hdrs(tcp); 10635 if (reterr != 0) 10636 return (reterr); 10637 break; 10638 } 10639 case IPV6_DSTOPTS: { 10640 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10641 10642 /* 10643 * Sanity checks - minimum size, size a multiple of 10644 * eight bytes, and matching size passed in. 10645 */ 10646 if (inlen != 0 && 10647 inlen != (8 * (dopts->ip6d_len + 1))) 10648 return (EINVAL); 10649 10650 if (checkonly) 10651 break; 10652 10653 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10654 (uchar_t **)&ipp->ipp_dstopts, 10655 &ipp->ipp_dstoptslen, 0); 10656 if (reterr != 0) 10657 return (reterr); 10658 if (ipp->ipp_dstoptslen == 0) 10659 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10660 else 10661 ipp->ipp_fields |= IPPF_DSTOPTS; 10662 reterr = tcp_build_hdrs(tcp); 10663 if (reterr != 0) 10664 return (reterr); 10665 break; 10666 } 10667 case IPV6_RTHDR: { 10668 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10669 10670 /* 10671 * Sanity checks - minimum size, size a multiple of 10672 * eight bytes, and matching size passed in. 10673 */ 10674 if (inlen != 0 && 10675 inlen != (8 * (rt->ip6r_len + 1))) 10676 return (EINVAL); 10677 10678 if (checkonly) 10679 break; 10680 10681 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10682 (uchar_t **)&ipp->ipp_rthdr, 10683 &ipp->ipp_rthdrlen, 0); 10684 if (reterr != 0) 10685 return (reterr); 10686 if (ipp->ipp_rthdrlen == 0) 10687 ipp->ipp_fields &= ~IPPF_RTHDR; 10688 else 10689 ipp->ipp_fields |= IPPF_RTHDR; 10690 reterr = tcp_build_hdrs(tcp); 10691 if (reterr != 0) 10692 return (reterr); 10693 break; 10694 } 10695 case IPV6_V6ONLY: 10696 if (!checkonly) { 10697 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10698 } 10699 break; 10700 case IPV6_USE_MIN_MTU: 10701 if (inlen != sizeof (int)) 10702 return (EINVAL); 10703 10704 if (*i1 < -1 || *i1 > 1) 10705 return (EINVAL); 10706 10707 if (checkonly) 10708 break; 10709 10710 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10711 ipp->ipp_use_min_mtu = *i1; 10712 break; 10713 case IPV6_SEC_OPT: 10714 /* 10715 * We should not allow policy setting after 10716 * we start listening for connections. 10717 */ 10718 if (tcp->tcp_state == TCPS_LISTEN) { 10719 return (EINVAL); 10720 } else { 10721 /* Handled at the IP level */ 10722 return (-EINVAL); 10723 } 10724 case IPV6_SRC_PREFERENCES: 10725 if (inlen != sizeof (uint32_t)) 10726 return (EINVAL); 10727 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10728 *(uint32_t *)invalp); 10729 if (reterr != 0) { 10730 *outlenp = 0; 10731 return (reterr); 10732 } 10733 break; 10734 default: 10735 *outlenp = 0; 10736 return (EINVAL); 10737 } 10738 break; 10739 } /* end IPPROTO_IPV6 */ 10740 default: 10741 *outlenp = 0; 10742 return (EINVAL); 10743 } 10744 /* 10745 * Common case of OK return with outval same as inval 10746 */ 10747 if (invalp != outvalp) { 10748 /* don't trust bcopy for identical src/dst */ 10749 (void) bcopy(invalp, outvalp, inlen); 10750 } 10751 *outlenp = inlen; 10752 return (0); 10753 } 10754 10755 /* ARGSUSED */ 10756 int 10757 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10758 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10759 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10760 { 10761 conn_t *connp = Q_TO_CONN(q); 10762 10763 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10764 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10765 } 10766 10767 int 10768 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10769 const void *optvalp, socklen_t optlen, cred_t *cr) 10770 { 10771 conn_t *connp = (conn_t *)proto_handle; 10772 squeue_t *sqp = connp->conn_sqp; 10773 int error; 10774 10775 ASSERT(connp->conn_upper_handle != NULL); 10776 /* 10777 * Entering the squeue synchronously can result in a context switch, 10778 * which can cause a rather sever performance degradation. So we try to 10779 * handle whatever options we can without entering the squeue. 10780 */ 10781 if (level == IPPROTO_TCP) { 10782 switch (option_name) { 10783 case TCP_NODELAY: 10784 if (optlen != sizeof (int32_t)) 10785 return (EINVAL); 10786 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10787 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10788 connp->conn_tcp->tcp_mss; 10789 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10790 return (0); 10791 default: 10792 break; 10793 } 10794 } 10795 10796 error = squeue_synch_enter(sqp, connp, 0); 10797 if (error == ENOMEM) { 10798 return (ENOMEM); 10799 } 10800 10801 error = proto_opt_check(level, option_name, optlen, NULL, 10802 tcp_opt_obj.odb_opt_des_arr, 10803 tcp_opt_obj.odb_opt_arr_cnt, 10804 tcp_opt_obj.odb_topmost_tpiprovider, 10805 B_TRUE, B_FALSE, cr); 10806 10807 if (error != 0) { 10808 if (error < 0) { 10809 error = proto_tlitosyserr(-error); 10810 } 10811 squeue_synch_exit(sqp, connp); 10812 return (error); 10813 } 10814 10815 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10816 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10817 NULL, cr, NULL); 10818 squeue_synch_exit(sqp, connp); 10819 10820 if (error < 0) { 10821 /* 10822 * Pass on to ip 10823 */ 10824 error = ip_set_options(connp, level, option_name, optvalp, 10825 optlen, cr); 10826 } 10827 return (error); 10828 } 10829 10830 /* 10831 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10832 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10833 * headers, and the maximum size tcp header (to avoid reallocation 10834 * on the fly for additional tcp options). 10835 * Returns failure if can't allocate memory. 10836 */ 10837 static int 10838 tcp_build_hdrs(tcp_t *tcp) 10839 { 10840 char *hdrs; 10841 uint_t hdrs_len; 10842 ip6i_t *ip6i; 10843 char buf[TCP_MAX_HDR_LENGTH]; 10844 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10845 in6_addr_t src, dst; 10846 tcp_stack_t *tcps = tcp->tcp_tcps; 10847 conn_t *connp = tcp->tcp_connp; 10848 10849 /* 10850 * save the existing tcp header and source/dest IP addresses 10851 */ 10852 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10853 src = tcp->tcp_ip6h->ip6_src; 10854 dst = tcp->tcp_ip6h->ip6_dst; 10855 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10856 ASSERT(hdrs_len != 0); 10857 if (hdrs_len > tcp->tcp_iphc_len) { 10858 /* Need to reallocate */ 10859 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10860 if (hdrs == NULL) 10861 return (ENOMEM); 10862 if (tcp->tcp_iphc != NULL) { 10863 if (tcp->tcp_hdr_grown) { 10864 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10865 } else { 10866 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10867 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10868 } 10869 tcp->tcp_iphc_len = 0; 10870 } 10871 ASSERT(tcp->tcp_iphc_len == 0); 10872 tcp->tcp_iphc = hdrs; 10873 tcp->tcp_iphc_len = hdrs_len; 10874 tcp->tcp_hdr_grown = B_TRUE; 10875 } 10876 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10877 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10878 10879 /* Set header fields not in ipp */ 10880 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10881 ip6i = (ip6i_t *)tcp->tcp_iphc; 10882 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10883 } else { 10884 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10885 } 10886 /* 10887 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10888 * 10889 * tcp->tcp_tcp_hdr_len doesn't change here. 10890 */ 10891 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10892 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10893 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10894 10895 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10896 10897 tcp->tcp_ip6h->ip6_src = src; 10898 tcp->tcp_ip6h->ip6_dst = dst; 10899 10900 /* 10901 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10902 * the default value for TCP. 10903 */ 10904 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10905 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10906 10907 /* 10908 * If we're setting extension headers after a connection 10909 * has been established, and if we have a routing header 10910 * among the extension headers, call ip_massage_options_v6 to 10911 * manipulate the routing header/ip6_dst set the checksum 10912 * difference in the tcp header template. 10913 * (This happens in tcp_connect_ipv6 if the routing header 10914 * is set prior to the connect.) 10915 * Set the tcp_sum to zero first in case we've cleared a 10916 * routing header or don't have one at all. 10917 */ 10918 tcp->tcp_sum = 0; 10919 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10920 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10921 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10922 (uint8_t *)tcp->tcp_tcph); 10923 if (rth != NULL) { 10924 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10925 rth, tcps->tcps_netstack); 10926 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10927 (tcp->tcp_sum >> 16)); 10928 } 10929 } 10930 10931 /* Try to get everything in a single mblk */ 10932 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10933 hdrs_len + tcps->tcps_wroff_xtra); 10934 return (0); 10935 } 10936 10937 /* 10938 * Transfer any source route option from ipha to buf/dst in reversed form. 10939 */ 10940 static int 10941 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10942 { 10943 ipoptp_t opts; 10944 uchar_t *opt; 10945 uint8_t optval; 10946 uint8_t optlen; 10947 uint32_t len = 0; 10948 10949 for (optval = ipoptp_first(&opts, ipha); 10950 optval != IPOPT_EOL; 10951 optval = ipoptp_next(&opts)) { 10952 opt = opts.ipoptp_cur; 10953 optlen = opts.ipoptp_len; 10954 switch (optval) { 10955 int off1, off2; 10956 case IPOPT_SSRR: 10957 case IPOPT_LSRR: 10958 10959 /* Reverse source route */ 10960 /* 10961 * First entry should be the next to last one in the 10962 * current source route (the last entry is our 10963 * address.) 10964 * The last entry should be the final destination. 10965 */ 10966 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10967 buf[IPOPT_OLEN] = (uint8_t)optlen; 10968 off1 = IPOPT_MINOFF_SR - 1; 10969 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10970 if (off2 < 0) { 10971 /* No entries in source route */ 10972 break; 10973 } 10974 bcopy(opt + off2, dst, IP_ADDR_LEN); 10975 /* 10976 * Note: use src since ipha has not had its src 10977 * and dst reversed (it is in the state it was 10978 * received. 10979 */ 10980 bcopy(&ipha->ipha_src, buf + off2, 10981 IP_ADDR_LEN); 10982 off2 -= IP_ADDR_LEN; 10983 10984 while (off2 > 0) { 10985 bcopy(opt + off2, buf + off1, 10986 IP_ADDR_LEN); 10987 off1 += IP_ADDR_LEN; 10988 off2 -= IP_ADDR_LEN; 10989 } 10990 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10991 buf += optlen; 10992 len += optlen; 10993 break; 10994 } 10995 } 10996 done: 10997 /* Pad the resulting options */ 10998 while (len & 0x3) { 10999 *buf++ = IPOPT_EOL; 11000 len++; 11001 } 11002 return (len); 11003 } 11004 11005 11006 /* 11007 * Extract and revert a source route from ipha (if any) 11008 * and then update the relevant fields in both tcp_t and the standard header. 11009 */ 11010 static void 11011 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11012 { 11013 char buf[TCP_MAX_HDR_LENGTH]; 11014 uint_t tcph_len; 11015 int len; 11016 11017 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11018 len = IPH_HDR_LENGTH(ipha); 11019 if (len == IP_SIMPLE_HDR_LENGTH) 11020 /* Nothing to do */ 11021 return; 11022 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11023 (len & 0x3)) 11024 return; 11025 11026 tcph_len = tcp->tcp_tcp_hdr_len; 11027 bcopy(tcp->tcp_tcph, buf, tcph_len); 11028 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11029 (tcp->tcp_ipha->ipha_dst & 0xffff); 11030 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11031 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11032 len += IP_SIMPLE_HDR_LENGTH; 11033 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11034 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11035 if ((int)tcp->tcp_sum < 0) 11036 tcp->tcp_sum--; 11037 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11038 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11039 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11040 bcopy(buf, tcp->tcp_tcph, tcph_len); 11041 tcp->tcp_ip_hdr_len = len; 11042 tcp->tcp_ipha->ipha_version_and_hdr_length = 11043 (IP_VERSION << 4) | (len >> 2); 11044 len += tcph_len; 11045 tcp->tcp_hdr_len = len; 11046 } 11047 11048 /* 11049 * Copy the standard header into its new location, 11050 * lay in the new options and then update the relevant 11051 * fields in both tcp_t and the standard header. 11052 */ 11053 static int 11054 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11055 { 11056 uint_t tcph_len; 11057 uint8_t *ip_optp; 11058 tcph_t *new_tcph; 11059 tcp_stack_t *tcps = tcp->tcp_tcps; 11060 conn_t *connp = tcp->tcp_connp; 11061 11062 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11063 return (EINVAL); 11064 11065 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11066 return (EINVAL); 11067 11068 if (checkonly) { 11069 /* 11070 * do not really set, just pretend to - T_CHECK 11071 */ 11072 return (0); 11073 } 11074 11075 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11076 if (tcp->tcp_label_len > 0) { 11077 int padlen; 11078 uint8_t opt; 11079 11080 /* convert list termination to no-ops */ 11081 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11082 ip_optp += ip_optp[IPOPT_OLEN]; 11083 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11084 while (--padlen >= 0) 11085 *ip_optp++ = opt; 11086 } 11087 tcph_len = tcp->tcp_tcp_hdr_len; 11088 new_tcph = (tcph_t *)(ip_optp + len); 11089 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11090 tcp->tcp_tcph = new_tcph; 11091 bcopy(ptr, ip_optp, len); 11092 11093 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11094 11095 tcp->tcp_ip_hdr_len = len; 11096 tcp->tcp_ipha->ipha_version_and_hdr_length = 11097 (IP_VERSION << 4) | (len >> 2); 11098 tcp->tcp_hdr_len = len + tcph_len; 11099 if (!TCP_IS_DETACHED(tcp)) { 11100 /* Always allocate room for all options. */ 11101 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11102 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11103 } 11104 return (0); 11105 } 11106 11107 /* Get callback routine passed to nd_load by tcp_param_register */ 11108 /* ARGSUSED */ 11109 static int 11110 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11111 { 11112 tcpparam_t *tcppa = (tcpparam_t *)cp; 11113 11114 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11115 return (0); 11116 } 11117 11118 /* 11119 * Walk through the param array specified registering each element with the 11120 * named dispatch handler. 11121 */ 11122 static boolean_t 11123 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11124 { 11125 for (; cnt-- > 0; tcppa++) { 11126 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11127 if (!nd_load(ndp, tcppa->tcp_param_name, 11128 tcp_param_get, tcp_param_set, 11129 (caddr_t)tcppa)) { 11130 nd_free(ndp); 11131 return (B_FALSE); 11132 } 11133 } 11134 } 11135 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11136 KM_SLEEP); 11137 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11138 sizeof (tcpparam_t)); 11139 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11140 tcp_param_get, tcp_param_set_aligned, 11141 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11142 nd_free(ndp); 11143 return (B_FALSE); 11144 } 11145 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11146 KM_SLEEP); 11147 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11148 sizeof (tcpparam_t)); 11149 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11150 tcp_param_get, tcp_param_set_aligned, 11151 (caddr_t)tcps->tcps_mdt_head_param)) { 11152 nd_free(ndp); 11153 return (B_FALSE); 11154 } 11155 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11156 KM_SLEEP); 11157 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11158 sizeof (tcpparam_t)); 11159 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11160 tcp_param_get, tcp_param_set_aligned, 11161 (caddr_t)tcps->tcps_mdt_tail_param)) { 11162 nd_free(ndp); 11163 return (B_FALSE); 11164 } 11165 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11166 KM_SLEEP); 11167 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11168 sizeof (tcpparam_t)); 11169 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11170 tcp_param_get, tcp_param_set_aligned, 11171 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11172 nd_free(ndp); 11173 return (B_FALSE); 11174 } 11175 if (!nd_load(ndp, "tcp_extra_priv_ports", 11176 tcp_extra_priv_ports_get, NULL, NULL)) { 11177 nd_free(ndp); 11178 return (B_FALSE); 11179 } 11180 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11181 NULL, tcp_extra_priv_ports_add, NULL)) { 11182 nd_free(ndp); 11183 return (B_FALSE); 11184 } 11185 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11186 NULL, tcp_extra_priv_ports_del, NULL)) { 11187 nd_free(ndp); 11188 return (B_FALSE); 11189 } 11190 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11191 tcp_1948_phrase_set, NULL)) { 11192 nd_free(ndp); 11193 return (B_FALSE); 11194 } 11195 /* 11196 * Dummy ndd variables - only to convey obsolescence information 11197 * through printing of their name (no get or set routines) 11198 * XXX Remove in future releases ? 11199 */ 11200 if (!nd_load(ndp, 11201 "tcp_close_wait_interval(obsoleted - " 11202 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11203 nd_free(ndp); 11204 return (B_FALSE); 11205 } 11206 return (B_TRUE); 11207 } 11208 11209 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11210 /* ARGSUSED */ 11211 static int 11212 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11213 cred_t *cr) 11214 { 11215 long new_value; 11216 tcpparam_t *tcppa = (tcpparam_t *)cp; 11217 11218 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11219 new_value < tcppa->tcp_param_min || 11220 new_value > tcppa->tcp_param_max) { 11221 return (EINVAL); 11222 } 11223 /* 11224 * Need to make sure new_value is a multiple of 4. If it is not, 11225 * round it up. For future 64 bit requirement, we actually make it 11226 * a multiple of 8. 11227 */ 11228 if (new_value & 0x7) { 11229 new_value = (new_value & ~0x7) + 0x8; 11230 } 11231 tcppa->tcp_param_val = new_value; 11232 return (0); 11233 } 11234 11235 /* Set callback routine passed to nd_load by tcp_param_register */ 11236 /* ARGSUSED */ 11237 static int 11238 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11239 { 11240 long new_value; 11241 tcpparam_t *tcppa = (tcpparam_t *)cp; 11242 11243 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11244 new_value < tcppa->tcp_param_min || 11245 new_value > tcppa->tcp_param_max) { 11246 return (EINVAL); 11247 } 11248 tcppa->tcp_param_val = new_value; 11249 return (0); 11250 } 11251 11252 /* 11253 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11254 * is filled, return as much as we can. The message passed in may be 11255 * multi-part, chained using b_cont. "start" is the starting sequence 11256 * number for this piece. 11257 */ 11258 static mblk_t * 11259 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11260 { 11261 uint32_t end; 11262 mblk_t *mp1; 11263 mblk_t *mp2; 11264 mblk_t *next_mp; 11265 uint32_t u1; 11266 tcp_stack_t *tcps = tcp->tcp_tcps; 11267 11268 /* Walk through all the new pieces. */ 11269 do { 11270 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11271 (uintptr_t)INT_MAX); 11272 end = start + (int)(mp->b_wptr - mp->b_rptr); 11273 next_mp = mp->b_cont; 11274 if (start == end) { 11275 /* Empty. Blast it. */ 11276 freeb(mp); 11277 continue; 11278 } 11279 mp->b_cont = NULL; 11280 TCP_REASS_SET_SEQ(mp, start); 11281 TCP_REASS_SET_END(mp, end); 11282 mp1 = tcp->tcp_reass_tail; 11283 if (!mp1) { 11284 tcp->tcp_reass_tail = mp; 11285 tcp->tcp_reass_head = mp; 11286 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11287 UPDATE_MIB(&tcps->tcps_mib, 11288 tcpInDataUnorderBytes, end - start); 11289 continue; 11290 } 11291 /* New stuff completely beyond tail? */ 11292 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11293 /* Link it on end. */ 11294 mp1->b_cont = mp; 11295 tcp->tcp_reass_tail = mp; 11296 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11297 UPDATE_MIB(&tcps->tcps_mib, 11298 tcpInDataUnorderBytes, end - start); 11299 continue; 11300 } 11301 mp1 = tcp->tcp_reass_head; 11302 u1 = TCP_REASS_SEQ(mp1); 11303 /* New stuff at the front? */ 11304 if (SEQ_LT(start, u1)) { 11305 /* Yes... Check for overlap. */ 11306 mp->b_cont = mp1; 11307 tcp->tcp_reass_head = mp; 11308 tcp_reass_elim_overlap(tcp, mp); 11309 continue; 11310 } 11311 /* 11312 * The new piece fits somewhere between the head and tail. 11313 * We find our slot, where mp1 precedes us and mp2 trails. 11314 */ 11315 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11316 u1 = TCP_REASS_SEQ(mp2); 11317 if (SEQ_LEQ(start, u1)) 11318 break; 11319 } 11320 /* Link ourselves in */ 11321 mp->b_cont = mp2; 11322 mp1->b_cont = mp; 11323 11324 /* Trim overlap with following mblk(s) first */ 11325 tcp_reass_elim_overlap(tcp, mp); 11326 11327 /* Trim overlap with preceding mblk */ 11328 tcp_reass_elim_overlap(tcp, mp1); 11329 11330 } while (start = end, mp = next_mp); 11331 mp1 = tcp->tcp_reass_head; 11332 /* Anything ready to go? */ 11333 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11334 return (NULL); 11335 /* Eat what we can off the queue */ 11336 for (;;) { 11337 mp = mp1->b_cont; 11338 end = TCP_REASS_END(mp1); 11339 TCP_REASS_SET_SEQ(mp1, 0); 11340 TCP_REASS_SET_END(mp1, 0); 11341 if (!mp) { 11342 tcp->tcp_reass_tail = NULL; 11343 break; 11344 } 11345 if (end != TCP_REASS_SEQ(mp)) { 11346 mp1->b_cont = NULL; 11347 break; 11348 } 11349 mp1 = mp; 11350 } 11351 mp1 = tcp->tcp_reass_head; 11352 tcp->tcp_reass_head = mp; 11353 return (mp1); 11354 } 11355 11356 /* Eliminate any overlap that mp may have over later mblks */ 11357 static void 11358 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11359 { 11360 uint32_t end; 11361 mblk_t *mp1; 11362 uint32_t u1; 11363 tcp_stack_t *tcps = tcp->tcp_tcps; 11364 11365 end = TCP_REASS_END(mp); 11366 while ((mp1 = mp->b_cont) != NULL) { 11367 u1 = TCP_REASS_SEQ(mp1); 11368 if (!SEQ_GT(end, u1)) 11369 break; 11370 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11371 mp->b_wptr -= end - u1; 11372 TCP_REASS_SET_END(mp, u1); 11373 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11374 UPDATE_MIB(&tcps->tcps_mib, 11375 tcpInDataPartDupBytes, end - u1); 11376 break; 11377 } 11378 mp->b_cont = mp1->b_cont; 11379 TCP_REASS_SET_SEQ(mp1, 0); 11380 TCP_REASS_SET_END(mp1, 0); 11381 freeb(mp1); 11382 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11383 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11384 } 11385 if (!mp1) 11386 tcp->tcp_reass_tail = mp; 11387 } 11388 11389 static uint_t 11390 tcp_rwnd_reopen(tcp_t *tcp) 11391 { 11392 uint_t ret = 0; 11393 uint_t thwin; 11394 11395 /* Learn the latest rwnd information that we sent to the other side. */ 11396 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11397 << tcp->tcp_rcv_ws; 11398 /* This is peer's calculated send window (our receive window). */ 11399 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11400 /* 11401 * Increase the receive window to max. But we need to do receiver 11402 * SWS avoidance. This means that we need to check the increase of 11403 * of receive window is at least 1 MSS. 11404 */ 11405 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11406 /* 11407 * If the window that the other side knows is less than max 11408 * deferred acks segments, send an update immediately. 11409 */ 11410 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11411 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11412 ret = TH_ACK_NEEDED; 11413 } 11414 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11415 } 11416 return (ret); 11417 } 11418 11419 /* 11420 * Send up all messages queued on tcp_rcv_list. 11421 */ 11422 static uint_t 11423 tcp_rcv_drain(tcp_t *tcp) 11424 { 11425 mblk_t *mp; 11426 uint_t ret = 0; 11427 #ifdef DEBUG 11428 uint_t cnt = 0; 11429 #endif 11430 queue_t *q = tcp->tcp_rq; 11431 11432 /* Can't drain on an eager connection */ 11433 if (tcp->tcp_listener != NULL) 11434 return (ret); 11435 11436 /* Can't be a non-STREAMS connection or sodirect enabled */ 11437 ASSERT((!IPCL_IS_NONSTR(tcp->tcp_connp)) && SOD_NOT_ENABLED(tcp)); 11438 11439 /* No need for the push timer now. */ 11440 if (tcp->tcp_push_tid != 0) { 11441 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11442 tcp->tcp_push_tid = 0; 11443 } 11444 11445 /* 11446 * Handle two cases here: we are currently fused or we were 11447 * previously fused and have some urgent data to be delivered 11448 * upstream. The latter happens because we either ran out of 11449 * memory or were detached and therefore sending the SIGURG was 11450 * deferred until this point. In either case we pass control 11451 * over to tcp_fuse_rcv_drain() since it may need to complete 11452 * some work. 11453 */ 11454 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11455 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11456 tcp->tcp_fused_sigurg_mp != NULL); 11457 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11458 &tcp->tcp_fused_sigurg_mp)) 11459 return (ret); 11460 } 11461 11462 while ((mp = tcp->tcp_rcv_list) != NULL) { 11463 tcp->tcp_rcv_list = mp->b_next; 11464 mp->b_next = NULL; 11465 #ifdef DEBUG 11466 cnt += msgdsize(mp); 11467 #endif 11468 /* Does this need SSL processing first? */ 11469 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11470 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11471 mblk_t *, mp); 11472 tcp_kssl_input(tcp, mp); 11473 continue; 11474 } 11475 putnext(q, mp); 11476 } 11477 #ifdef DEBUG 11478 ASSERT(cnt == tcp->tcp_rcv_cnt); 11479 #endif 11480 tcp->tcp_rcv_last_head = NULL; 11481 tcp->tcp_rcv_last_tail = NULL; 11482 tcp->tcp_rcv_cnt = 0; 11483 11484 if (canputnext(q)) 11485 return (tcp_rwnd_reopen(tcp)); 11486 11487 return (ret); 11488 } 11489 11490 /* 11491 * Queue data on tcp_rcv_list which is a b_next chain. 11492 * tcp_rcv_last_head/tail is the last element of this chain. 11493 * Each element of the chain is a b_cont chain. 11494 * 11495 * M_DATA messages are added to the current element. 11496 * Other messages are added as new (b_next) elements. 11497 */ 11498 void 11499 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11500 { 11501 ASSERT(seg_len == msgdsize(mp)); 11502 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11503 11504 if (tcp->tcp_rcv_list == NULL) { 11505 ASSERT(tcp->tcp_rcv_last_head == NULL); 11506 tcp->tcp_rcv_list = mp; 11507 tcp->tcp_rcv_last_head = mp; 11508 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11509 tcp->tcp_rcv_last_tail->b_cont = mp; 11510 } else { 11511 tcp->tcp_rcv_last_head->b_next = mp; 11512 tcp->tcp_rcv_last_head = mp; 11513 } 11514 11515 while (mp->b_cont) 11516 mp = mp->b_cont; 11517 11518 tcp->tcp_rcv_last_tail = mp; 11519 tcp->tcp_rcv_cnt += seg_len; 11520 tcp->tcp_rwnd -= seg_len; 11521 } 11522 11523 /* 11524 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11525 * above, in addition when uioa is enabled schedule an asynchronous uio 11526 * prior to enqueuing. They implement the combinhed semantics of the 11527 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11528 * canputnext(), i.e. flow-control with backenable. 11529 * 11530 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11531 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11532 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11533 * 11534 * Must be called with sodp->sod_lockp held and will return with the lock 11535 * released. 11536 */ 11537 static uint_t 11538 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11539 { 11540 queue_t *q = tcp->tcp_rq; 11541 uint_t thwin; 11542 tcp_stack_t *tcps = tcp->tcp_tcps; 11543 uint_t ret = 0; 11544 11545 /* Can't be an eager connection */ 11546 ASSERT(tcp->tcp_listener == NULL); 11547 11548 /* Caller must have lock held */ 11549 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11550 11551 /* Sodirect mode so must not be a tcp_rcv_list */ 11552 ASSERT(tcp->tcp_rcv_list == NULL); 11553 11554 if (SOD_QFULL(sodp)) { 11555 /* Q is full, mark Q for need backenable */ 11556 SOD_QSETBE(sodp); 11557 } 11558 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11559 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11560 << tcp->tcp_rcv_ws; 11561 /* This is peer's calculated send window (our available rwnd). */ 11562 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11563 /* 11564 * Increase the receive window to max. But we need to do receiver 11565 * SWS avoidance. This means that we need to check the increase of 11566 * of receive window is at least 1 MSS. 11567 */ 11568 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11569 /* 11570 * If the window that the other side knows is less than max 11571 * deferred acks segments, send an update immediately. 11572 */ 11573 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11574 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11575 ret = TH_ACK_NEEDED; 11576 } 11577 tcp->tcp_rwnd = q->q_hiwat; 11578 } 11579 11580 if (!SOD_QEMPTY(sodp)) { 11581 /* Wakeup to socket */ 11582 sodp->sod_state &= SOD_WAKE_CLR; 11583 sodp->sod_state |= SOD_WAKE_DONE; 11584 (sodp->sod_wakeup)(sodp); 11585 /* wakeup() does the mutex_ext() */ 11586 } else { 11587 /* Q is empty, no need to wake */ 11588 sodp->sod_state &= SOD_WAKE_CLR; 11589 sodp->sod_state |= SOD_WAKE_NOT; 11590 mutex_exit(sodp->sod_lockp); 11591 } 11592 11593 /* No need for the push timer now. */ 11594 if (tcp->tcp_push_tid != 0) { 11595 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11596 tcp->tcp_push_tid = 0; 11597 } 11598 11599 return (ret); 11600 } 11601 11602 /* 11603 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11604 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11605 * to the user-land buffer and flag the mblk_t as such. 11606 * 11607 * Also, handle tcp_rwnd. 11608 */ 11609 uint_t 11610 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11611 { 11612 uioa_t *uioap = &sodp->sod_uioa; 11613 boolean_t qfull; 11614 uint_t thwin; 11615 11616 /* Can't be an eager connection */ 11617 ASSERT(tcp->tcp_listener == NULL); 11618 11619 /* Caller must have lock held */ 11620 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11621 11622 /* Sodirect mode so must not be a tcp_rcv_list */ 11623 ASSERT(tcp->tcp_rcv_list == NULL); 11624 11625 /* Passed in segment length must be equal to mblk_t chain data size */ 11626 ASSERT(seg_len == msgdsize(mp)); 11627 11628 if (DB_TYPE(mp) != M_DATA) { 11629 /* Only process M_DATA mblk_t's */ 11630 goto enq; 11631 } 11632 if (uioap->uioa_state & UIOA_ENABLED) { 11633 /* Uioa is enabled */ 11634 mblk_t *mp1 = mp; 11635 mblk_t *lmp = NULL; 11636 11637 if (seg_len > uioap->uio_resid) { 11638 /* 11639 * There isn't enough uio space for the mblk_t chain 11640 * so disable uioa such that this and any additional 11641 * mblk_t data is handled by the socket and schedule 11642 * the socket for wakeup to finish this uioa. 11643 */ 11644 uioap->uioa_state &= UIOA_CLR; 11645 uioap->uioa_state |= UIOA_FINI; 11646 if (sodp->sod_state & SOD_WAKE_NOT) { 11647 sodp->sod_state &= SOD_WAKE_CLR; 11648 sodp->sod_state |= SOD_WAKE_NEED; 11649 } 11650 goto enq; 11651 } 11652 do { 11653 uint32_t len = MBLKL(mp1); 11654 11655 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11656 /* Scheduled, mark dblk_t as such */ 11657 DB_FLAGS(mp1) |= DBLK_UIOA; 11658 } else { 11659 /* Error, turn off async processing */ 11660 uioap->uioa_state &= UIOA_CLR; 11661 uioap->uioa_state |= UIOA_FINI; 11662 break; 11663 } 11664 lmp = mp1; 11665 } while ((mp1 = mp1->b_cont) != NULL); 11666 11667 if (mp1 != NULL || uioap->uio_resid == 0) { 11668 /* 11669 * Not all mblk_t(s) uioamoved (error) or all uio 11670 * space has been consumed so schedule the socket 11671 * for wakeup to finish this uio. 11672 */ 11673 sodp->sod_state &= SOD_WAKE_CLR; 11674 sodp->sod_state |= SOD_WAKE_NEED; 11675 11676 /* Break the mblk chain if neccessary. */ 11677 if (mp1 != NULL && lmp != NULL) { 11678 mp->b_next = mp1; 11679 lmp->b_cont = NULL; 11680 } 11681 } 11682 } else if (uioap->uioa_state & UIOA_FINI) { 11683 /* 11684 * Post UIO_ENABLED waiting for socket to finish processing 11685 * so just enqueue and update tcp_rwnd. 11686 */ 11687 if (SOD_QFULL(sodp)) 11688 tcp->tcp_rwnd -= seg_len; 11689 } else if (sodp->sod_want > 0) { 11690 /* 11691 * Uioa isn't enabled but sodirect has a pending read(). 11692 */ 11693 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11694 if (sodp->sod_state & SOD_WAKE_NOT) { 11695 /* Schedule socket for wakeup */ 11696 sodp->sod_state &= SOD_WAKE_CLR; 11697 sodp->sod_state |= SOD_WAKE_NEED; 11698 } 11699 tcp->tcp_rwnd -= seg_len; 11700 } 11701 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11702 /* 11703 * No pending sodirect read() so used the default 11704 * TCP push logic to guess that a push is needed. 11705 */ 11706 if (sodp->sod_state & SOD_WAKE_NOT) { 11707 /* Schedule socket for wakeup */ 11708 sodp->sod_state &= SOD_WAKE_CLR; 11709 sodp->sod_state |= SOD_WAKE_NEED; 11710 } 11711 tcp->tcp_rwnd -= seg_len; 11712 } else { 11713 /* Just update tcp_rwnd */ 11714 tcp->tcp_rwnd -= seg_len; 11715 } 11716 enq: 11717 qfull = SOD_QFULL(sodp); 11718 11719 (sodp->sod_enqueue)(sodp, mp); 11720 11721 if (! qfull && SOD_QFULL(sodp)) { 11722 /* Wasn't QFULL, now QFULL, need back-enable */ 11723 SOD_QSETBE(sodp); 11724 } 11725 11726 /* 11727 * Check to see if remote avail swnd < mss due to delayed ACK, 11728 * first get advertised rwnd. 11729 */ 11730 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11731 /* Minus delayed ACK count */ 11732 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11733 if (thwin < tcp->tcp_mss) { 11734 /* Remote avail swnd < mss, need ACK now */ 11735 return (TH_ACK_NEEDED); 11736 } 11737 11738 return (0); 11739 } 11740 11741 /* 11742 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11743 * 11744 * This is the default entry function into TCP on the read side. TCP is 11745 * always entered via squeue i.e. using squeue's for mutual exclusion. 11746 * When classifier does a lookup to find the tcp, it also puts a reference 11747 * on the conn structure associated so the tcp is guaranteed to exist 11748 * when we come here. We still need to check the state because it might 11749 * as well has been closed. The squeue processing function i.e. squeue_enter, 11750 * is responsible for doing the CONN_DEC_REF. 11751 * 11752 * Apart from the default entry point, IP also sends packets directly to 11753 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11754 * connections. 11755 */ 11756 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11757 void 11758 tcp_input(void *arg, mblk_t *mp, void *arg2) 11759 { 11760 conn_t *connp = (conn_t *)arg; 11761 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11762 11763 /* arg2 is the sqp */ 11764 ASSERT(arg2 != NULL); 11765 ASSERT(mp != NULL); 11766 11767 /* 11768 * Don't accept any input on a closed tcp as this TCP logically does 11769 * not exist on the system. Don't proceed further with this TCP. 11770 * For eg. this packet could trigger another close of this tcp 11771 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11772 * tcp_clean_death / tcp_closei_local must be called at most once 11773 * on a TCP. In this case we need to refeed the packet into the 11774 * classifier and figure out where the packet should go. Need to 11775 * preserve the recv_ill somehow. Until we figure that out, for 11776 * now just drop the packet if we can't classify the packet. 11777 */ 11778 if (tcp->tcp_state == TCPS_CLOSED || 11779 tcp->tcp_state == TCPS_BOUND) { 11780 conn_t *new_connp; 11781 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11782 11783 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11784 if (new_connp != NULL) { 11785 tcp_reinput(new_connp, mp, arg2); 11786 return; 11787 } 11788 /* We failed to classify. For now just drop the packet */ 11789 freemsg(mp); 11790 return; 11791 } 11792 11793 if (DB_TYPE(mp) != M_DATA) { 11794 tcp_rput_common(tcp, mp); 11795 return; 11796 } 11797 11798 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11799 squeue_t *final_sqp; 11800 11801 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11802 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11803 DB_CKSUMSTART(mp) = 0; 11804 if (tcp->tcp_state == TCPS_SYN_SENT && 11805 connp->conn_final_sqp == NULL && 11806 tcp_outbound_squeue_switch) { 11807 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11808 connp->conn_final_sqp = final_sqp; 11809 if (connp->conn_final_sqp != connp->conn_sqp) { 11810 CONN_INC_REF(connp); 11811 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11812 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11813 tcp_rput_data, connp, ip_squeue_flag, 11814 SQTAG_CONNECT_FINISH); 11815 return; 11816 } 11817 } 11818 } 11819 tcp_rput_data(connp, mp, arg2); 11820 } 11821 11822 /* 11823 * The read side put procedure. 11824 * The packets passed up by ip are assume to be aligned according to 11825 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11826 */ 11827 static void 11828 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11829 { 11830 /* 11831 * tcp_rput_data() does not expect M_CTL except for the case 11832 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11833 * type. Need to make sure that any other M_CTLs don't make 11834 * it to tcp_rput_data since it is not expecting any and doesn't 11835 * check for it. 11836 */ 11837 if (DB_TYPE(mp) == M_CTL) { 11838 switch (*(uint32_t *)(mp->b_rptr)) { 11839 case TCP_IOC_ABORT_CONN: 11840 /* 11841 * Handle connection abort request. 11842 */ 11843 tcp_ioctl_abort_handler(tcp, mp); 11844 return; 11845 case IPSEC_IN: 11846 /* 11847 * Only secure icmp arrive in TCP and they 11848 * don't go through data path. 11849 */ 11850 tcp_icmp_error(tcp, mp); 11851 return; 11852 case IN_PKTINFO: 11853 /* 11854 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11855 * sockets that are receiving IPv4 traffic. tcp 11856 */ 11857 ASSERT(tcp->tcp_family == AF_INET6); 11858 ASSERT(tcp->tcp_ipv6_recvancillary & 11859 TCP_IPV6_RECVPKTINFO); 11860 tcp_rput_data(tcp->tcp_connp, mp, 11861 tcp->tcp_connp->conn_sqp); 11862 return; 11863 case MDT_IOC_INFO_UPDATE: 11864 /* 11865 * Handle Multidata information update; the 11866 * following routine will free the message. 11867 */ 11868 if (tcp->tcp_connp->conn_mdt_ok) { 11869 tcp_mdt_update(tcp, 11870 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11871 B_FALSE); 11872 } 11873 freemsg(mp); 11874 return; 11875 case LSO_IOC_INFO_UPDATE: 11876 /* 11877 * Handle LSO information update; the following 11878 * routine will free the message. 11879 */ 11880 if (tcp->tcp_connp->conn_lso_ok) { 11881 tcp_lso_update(tcp, 11882 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11883 } 11884 freemsg(mp); 11885 return; 11886 default: 11887 /* 11888 * tcp_icmp_err() will process the M_CTL packets. 11889 * Non-ICMP packets, if any, will be discarded in 11890 * tcp_icmp_err(). We will process the ICMP packet 11891 * even if we are TCP_IS_DETACHED_NONEAGER as the 11892 * incoming ICMP packet may result in changing 11893 * the tcp_mss, which we would need if we have 11894 * packets to retransmit. 11895 */ 11896 tcp_icmp_error(tcp, mp); 11897 return; 11898 } 11899 } 11900 11901 /* No point processing the message if tcp is already closed */ 11902 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11903 freemsg(mp); 11904 return; 11905 } 11906 11907 tcp_rput_other(tcp, mp); 11908 } 11909 11910 11911 /* The minimum of smoothed mean deviation in RTO calculation. */ 11912 #define TCP_SD_MIN 400 11913 11914 /* 11915 * Set RTO for this connection. The formula is from Jacobson and Karels' 11916 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11917 * are the same as those in Appendix A.2 of that paper. 11918 * 11919 * m = new measurement 11920 * sa = smoothed RTT average (8 * average estimates). 11921 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11922 */ 11923 static void 11924 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11925 { 11926 long m = TICK_TO_MSEC(rtt); 11927 clock_t sa = tcp->tcp_rtt_sa; 11928 clock_t sv = tcp->tcp_rtt_sd; 11929 clock_t rto; 11930 tcp_stack_t *tcps = tcp->tcp_tcps; 11931 11932 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11933 tcp->tcp_rtt_update++; 11934 11935 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11936 if (sa != 0) { 11937 /* 11938 * Update average estimator: 11939 * new rtt = 7/8 old rtt + 1/8 Error 11940 */ 11941 11942 /* m is now Error in estimate. */ 11943 m -= sa >> 3; 11944 if ((sa += m) <= 0) { 11945 /* 11946 * Don't allow the smoothed average to be negative. 11947 * We use 0 to denote reinitialization of the 11948 * variables. 11949 */ 11950 sa = 1; 11951 } 11952 11953 /* 11954 * Update deviation estimator: 11955 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11956 */ 11957 if (m < 0) 11958 m = -m; 11959 m -= sv >> 2; 11960 sv += m; 11961 } else { 11962 /* 11963 * This follows BSD's implementation. So the reinitialized 11964 * RTO is 3 * m. We cannot go less than 2 because if the 11965 * link is bandwidth dominated, doubling the window size 11966 * during slow start means doubling the RTT. We want to be 11967 * more conservative when we reinitialize our estimates. 3 11968 * is just a convenient number. 11969 */ 11970 sa = m << 3; 11971 sv = m << 1; 11972 } 11973 if (sv < TCP_SD_MIN) { 11974 /* 11975 * We do not know that if sa captures the delay ACK 11976 * effect as in a long train of segments, a receiver 11977 * does not delay its ACKs. So set the minimum of sv 11978 * to be TCP_SD_MIN, which is default to 400 ms, twice 11979 * of BSD DATO. That means the minimum of mean 11980 * deviation is 100 ms. 11981 * 11982 */ 11983 sv = TCP_SD_MIN; 11984 } 11985 tcp->tcp_rtt_sa = sa; 11986 tcp->tcp_rtt_sd = sv; 11987 /* 11988 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11989 * 11990 * Add tcp_rexmit_interval extra in case of extreme environment 11991 * where the algorithm fails to work. The default value of 11992 * tcp_rexmit_interval_extra should be 0. 11993 * 11994 * As we use a finer grained clock than BSD and update 11995 * RTO for every ACKs, add in another .25 of RTT to the 11996 * deviation of RTO to accomodate burstiness of 1/4 of 11997 * window size. 11998 */ 11999 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12000 12001 if (rto > tcps->tcps_rexmit_interval_max) { 12002 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12003 } else if (rto < tcps->tcps_rexmit_interval_min) { 12004 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12005 } else { 12006 tcp->tcp_rto = rto; 12007 } 12008 12009 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12010 tcp->tcp_timer_backoff = 0; 12011 } 12012 12013 /* 12014 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12015 * send queue which starts at the given seq. no. 12016 * 12017 * Parameters: 12018 * tcp_t *tcp: the tcp instance pointer. 12019 * uint32_t seq: the starting seq. no of the requested segment. 12020 * int32_t *off: after the execution, *off will be the offset to 12021 * the returned mblk which points to the requested seq no. 12022 * It is the caller's responsibility to send in a non-null off. 12023 * 12024 * Return: 12025 * A mblk_t pointer pointing to the requested segment in send queue. 12026 */ 12027 static mblk_t * 12028 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12029 { 12030 int32_t cnt; 12031 mblk_t *mp; 12032 12033 /* Defensive coding. Make sure we don't send incorrect data. */ 12034 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12035 return (NULL); 12036 12037 cnt = seq - tcp->tcp_suna; 12038 mp = tcp->tcp_xmit_head; 12039 while (cnt > 0 && mp != NULL) { 12040 cnt -= mp->b_wptr - mp->b_rptr; 12041 if (cnt < 0) { 12042 cnt += mp->b_wptr - mp->b_rptr; 12043 break; 12044 } 12045 mp = mp->b_cont; 12046 } 12047 ASSERT(mp != NULL); 12048 *off = cnt; 12049 return (mp); 12050 } 12051 12052 /* 12053 * This function handles all retransmissions if SACK is enabled for this 12054 * connection. First it calculates how many segments can be retransmitted 12055 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12056 * segments. A segment is eligible if sack_cnt for that segment is greater 12057 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12058 * all eligible segments, it checks to see if TCP can send some new segments 12059 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12060 * 12061 * Parameters: 12062 * tcp_t *tcp: the tcp structure of the connection. 12063 * uint_t *flags: in return, appropriate value will be set for 12064 * tcp_rput_data(). 12065 */ 12066 static void 12067 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12068 { 12069 notsack_blk_t *notsack_blk; 12070 int32_t usable_swnd; 12071 int32_t mss; 12072 uint32_t seg_len; 12073 mblk_t *xmit_mp; 12074 tcp_stack_t *tcps = tcp->tcp_tcps; 12075 12076 ASSERT(tcp->tcp_sack_info != NULL); 12077 ASSERT(tcp->tcp_notsack_list != NULL); 12078 ASSERT(tcp->tcp_rexmit == B_FALSE); 12079 12080 /* Defensive coding in case there is a bug... */ 12081 if (tcp->tcp_notsack_list == NULL) { 12082 return; 12083 } 12084 notsack_blk = tcp->tcp_notsack_list; 12085 mss = tcp->tcp_mss; 12086 12087 /* 12088 * Limit the num of outstanding data in the network to be 12089 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12090 */ 12091 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12092 12093 /* At least retransmit 1 MSS of data. */ 12094 if (usable_swnd <= 0) { 12095 usable_swnd = mss; 12096 } 12097 12098 /* Make sure no new RTT samples will be taken. */ 12099 tcp->tcp_csuna = tcp->tcp_snxt; 12100 12101 notsack_blk = tcp->tcp_notsack_list; 12102 while (usable_swnd > 0) { 12103 mblk_t *snxt_mp, *tmp_mp; 12104 tcp_seq begin = tcp->tcp_sack_snxt; 12105 tcp_seq end; 12106 int32_t off; 12107 12108 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12109 if (SEQ_GT(notsack_blk->end, begin) && 12110 (notsack_blk->sack_cnt >= 12111 tcps->tcps_dupack_fast_retransmit)) { 12112 end = notsack_blk->end; 12113 if (SEQ_LT(begin, notsack_blk->begin)) { 12114 begin = notsack_blk->begin; 12115 } 12116 break; 12117 } 12118 } 12119 /* 12120 * All holes are filled. Manipulate tcp_cwnd to send more 12121 * if we can. Note that after the SACK recovery, tcp_cwnd is 12122 * set to tcp_cwnd_ssthresh. 12123 */ 12124 if (notsack_blk == NULL) { 12125 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12126 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12127 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12128 ASSERT(tcp->tcp_cwnd > 0); 12129 return; 12130 } else { 12131 usable_swnd = usable_swnd / mss; 12132 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12133 MAX(usable_swnd * mss, mss); 12134 *flags |= TH_XMIT_NEEDED; 12135 return; 12136 } 12137 } 12138 12139 /* 12140 * Note that we may send more than usable_swnd allows here 12141 * because of round off, but no more than 1 MSS of data. 12142 */ 12143 seg_len = end - begin; 12144 if (seg_len > mss) 12145 seg_len = mss; 12146 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12147 ASSERT(snxt_mp != NULL); 12148 /* This should not happen. Defensive coding again... */ 12149 if (snxt_mp == NULL) { 12150 return; 12151 } 12152 12153 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12154 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12155 if (xmit_mp == NULL) 12156 return; 12157 12158 usable_swnd -= seg_len; 12159 tcp->tcp_pipe += seg_len; 12160 tcp->tcp_sack_snxt = begin + seg_len; 12161 12162 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12163 12164 /* 12165 * Update the send timestamp to avoid false retransmission. 12166 */ 12167 snxt_mp->b_prev = (mblk_t *)lbolt; 12168 12169 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12170 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12171 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12172 /* 12173 * Update tcp_rexmit_max to extend this SACK recovery phase. 12174 * This happens when new data sent during fast recovery is 12175 * also lost. If TCP retransmits those new data, it needs 12176 * to extend SACK recover phase to avoid starting another 12177 * fast retransmit/recovery unnecessarily. 12178 */ 12179 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12180 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12181 } 12182 } 12183 } 12184 12185 /* 12186 * This function handles policy checking at TCP level for non-hard_bound/ 12187 * detached connections. 12188 */ 12189 static boolean_t 12190 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12191 boolean_t secure, boolean_t mctl_present) 12192 { 12193 ipsec_latch_t *ipl = NULL; 12194 ipsec_action_t *act = NULL; 12195 mblk_t *data_mp; 12196 ipsec_in_t *ii; 12197 const char *reason; 12198 kstat_named_t *counter; 12199 tcp_stack_t *tcps = tcp->tcp_tcps; 12200 ipsec_stack_t *ipss; 12201 ip_stack_t *ipst; 12202 12203 ASSERT(mctl_present || !secure); 12204 12205 ASSERT((ipha == NULL && ip6h != NULL) || 12206 (ip6h == NULL && ipha != NULL)); 12207 12208 /* 12209 * We don't necessarily have an ipsec_in_act action to verify 12210 * policy because of assymetrical policy where we have only 12211 * outbound policy and no inbound policy (possible with global 12212 * policy). 12213 */ 12214 if (!secure) { 12215 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12216 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12217 return (B_TRUE); 12218 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12219 "tcp_check_policy", ipha, ip6h, secure, 12220 tcps->tcps_netstack); 12221 ipss = tcps->tcps_netstack->netstack_ipsec; 12222 12223 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12224 DROPPER(ipss, ipds_tcp_clear), 12225 &tcps->tcps_dropper); 12226 return (B_FALSE); 12227 } 12228 12229 /* 12230 * We have a secure packet. 12231 */ 12232 if (act == NULL) { 12233 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12234 "tcp_check_policy", ipha, ip6h, secure, 12235 tcps->tcps_netstack); 12236 ipss = tcps->tcps_netstack->netstack_ipsec; 12237 12238 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12239 DROPPER(ipss, ipds_tcp_secure), 12240 &tcps->tcps_dropper); 12241 return (B_FALSE); 12242 } 12243 12244 /* 12245 * XXX This whole routine is currently incorrect. ipl should 12246 * be set to the latch pointer, but is currently not set, so 12247 * we initialize it to NULL to avoid picking up random garbage. 12248 */ 12249 if (ipl == NULL) 12250 return (B_TRUE); 12251 12252 data_mp = first_mp->b_cont; 12253 12254 ii = (ipsec_in_t *)first_mp->b_rptr; 12255 12256 ipst = tcps->tcps_netstack->netstack_ip; 12257 12258 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12259 &counter, tcp->tcp_connp)) { 12260 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12261 return (B_TRUE); 12262 } 12263 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12264 "tcp inbound policy mismatch: %s, packet dropped\n", 12265 reason); 12266 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12267 12268 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12269 &tcps->tcps_dropper); 12270 return (B_FALSE); 12271 } 12272 12273 /* 12274 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12275 * retransmission after a timeout. 12276 * 12277 * To limit the number of duplicate segments, we limit the number of segment 12278 * to be sent in one time to tcp_snd_burst, the burst variable. 12279 */ 12280 static void 12281 tcp_ss_rexmit(tcp_t *tcp) 12282 { 12283 uint32_t snxt; 12284 uint32_t smax; 12285 int32_t win; 12286 int32_t mss; 12287 int32_t off; 12288 int32_t burst = tcp->tcp_snd_burst; 12289 mblk_t *snxt_mp; 12290 tcp_stack_t *tcps = tcp->tcp_tcps; 12291 12292 /* 12293 * Note that tcp_rexmit can be set even though TCP has retransmitted 12294 * all unack'ed segments. 12295 */ 12296 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12297 smax = tcp->tcp_rexmit_max; 12298 snxt = tcp->tcp_rexmit_nxt; 12299 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12300 snxt = tcp->tcp_suna; 12301 } 12302 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12303 win -= snxt - tcp->tcp_suna; 12304 mss = tcp->tcp_mss; 12305 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12306 12307 while (SEQ_LT(snxt, smax) && (win > 0) && 12308 (burst > 0) && (snxt_mp != NULL)) { 12309 mblk_t *xmit_mp; 12310 mblk_t *old_snxt_mp = snxt_mp; 12311 uint32_t cnt = mss; 12312 12313 if (win < cnt) { 12314 cnt = win; 12315 } 12316 if (SEQ_GT(snxt + cnt, smax)) { 12317 cnt = smax - snxt; 12318 } 12319 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12320 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12321 if (xmit_mp == NULL) 12322 return; 12323 12324 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12325 12326 snxt += cnt; 12327 win -= cnt; 12328 /* 12329 * Update the send timestamp to avoid false 12330 * retransmission. 12331 */ 12332 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12333 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12334 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12335 12336 tcp->tcp_rexmit_nxt = snxt; 12337 burst--; 12338 } 12339 /* 12340 * If we have transmitted all we have at the time 12341 * we started the retranmission, we can leave 12342 * the rest of the job to tcp_wput_data(). But we 12343 * need to check the send window first. If the 12344 * win is not 0, go on with tcp_wput_data(). 12345 */ 12346 if (SEQ_LT(snxt, smax) || win == 0) { 12347 return; 12348 } 12349 } 12350 /* Only call tcp_wput_data() if there is data to be sent. */ 12351 if (tcp->tcp_unsent) { 12352 tcp_wput_data(tcp, NULL, B_FALSE); 12353 } 12354 } 12355 12356 /* 12357 * Process all TCP option in SYN segment. Note that this function should 12358 * be called after tcp_adapt_ire() is called so that the necessary info 12359 * from IRE is already set in the tcp structure. 12360 * 12361 * This function sets up the correct tcp_mss value according to the 12362 * MSS option value and our header size. It also sets up the window scale 12363 * and timestamp values, and initialize SACK info blocks. But it does not 12364 * change receive window size after setting the tcp_mss value. The caller 12365 * should do the appropriate change. 12366 */ 12367 void 12368 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12369 { 12370 int options; 12371 tcp_opt_t tcpopt; 12372 uint32_t mss_max; 12373 char *tmp_tcph; 12374 tcp_stack_t *tcps = tcp->tcp_tcps; 12375 12376 tcpopt.tcp = NULL; 12377 options = tcp_parse_options(tcph, &tcpopt); 12378 12379 /* 12380 * Process MSS option. Note that MSS option value does not account 12381 * for IP or TCP options. This means that it is equal to MTU - minimum 12382 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12383 * IPv6. 12384 */ 12385 if (!(options & TCP_OPT_MSS_PRESENT)) { 12386 if (tcp->tcp_ipversion == IPV4_VERSION) 12387 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12388 else 12389 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12390 } else { 12391 if (tcp->tcp_ipversion == IPV4_VERSION) 12392 mss_max = tcps->tcps_mss_max_ipv4; 12393 else 12394 mss_max = tcps->tcps_mss_max_ipv6; 12395 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12396 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12397 else if (tcpopt.tcp_opt_mss > mss_max) 12398 tcpopt.tcp_opt_mss = mss_max; 12399 } 12400 12401 /* Process Window Scale option. */ 12402 if (options & TCP_OPT_WSCALE_PRESENT) { 12403 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12404 tcp->tcp_snd_ws_ok = B_TRUE; 12405 } else { 12406 tcp->tcp_snd_ws = B_FALSE; 12407 tcp->tcp_snd_ws_ok = B_FALSE; 12408 tcp->tcp_rcv_ws = B_FALSE; 12409 } 12410 12411 /* Process Timestamp option. */ 12412 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12413 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12414 tmp_tcph = (char *)tcp->tcp_tcph; 12415 12416 tcp->tcp_snd_ts_ok = B_TRUE; 12417 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12418 tcp->tcp_last_rcv_lbolt = lbolt64; 12419 ASSERT(OK_32PTR(tmp_tcph)); 12420 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12421 12422 /* Fill in our template header with basic timestamp option. */ 12423 tmp_tcph += tcp->tcp_tcp_hdr_len; 12424 tmp_tcph[0] = TCPOPT_NOP; 12425 tmp_tcph[1] = TCPOPT_NOP; 12426 tmp_tcph[2] = TCPOPT_TSTAMP; 12427 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12428 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12429 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12430 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12431 } else { 12432 tcp->tcp_snd_ts_ok = B_FALSE; 12433 } 12434 12435 /* 12436 * Process SACK options. If SACK is enabled for this connection, 12437 * then allocate the SACK info structure. Note the following ways 12438 * when tcp_snd_sack_ok is set to true. 12439 * 12440 * For active connection: in tcp_adapt_ire() called in 12441 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12442 * is checked. 12443 * 12444 * For passive connection: in tcp_adapt_ire() called in 12445 * tcp_accept_comm(). 12446 * 12447 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12448 * That check makes sure that if we did not send a SACK OK option, 12449 * we will not enable SACK for this connection even though the other 12450 * side sends us SACK OK option. For active connection, the SACK 12451 * info structure has already been allocated. So we need to free 12452 * it if SACK is disabled. 12453 */ 12454 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12455 (tcp->tcp_snd_sack_ok || 12456 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12457 /* This should be true only in the passive case. */ 12458 if (tcp->tcp_sack_info == NULL) { 12459 ASSERT(TCP_IS_DETACHED(tcp)); 12460 tcp->tcp_sack_info = 12461 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12462 } 12463 if (tcp->tcp_sack_info == NULL) { 12464 tcp->tcp_snd_sack_ok = B_FALSE; 12465 } else { 12466 tcp->tcp_snd_sack_ok = B_TRUE; 12467 if (tcp->tcp_snd_ts_ok) { 12468 tcp->tcp_max_sack_blk = 3; 12469 } else { 12470 tcp->tcp_max_sack_blk = 4; 12471 } 12472 } 12473 } else { 12474 /* 12475 * Resetting tcp_snd_sack_ok to B_FALSE so that 12476 * no SACK info will be used for this 12477 * connection. This assumes that SACK usage 12478 * permission is negotiated. This may need 12479 * to be changed once this is clarified. 12480 */ 12481 if (tcp->tcp_sack_info != NULL) { 12482 ASSERT(tcp->tcp_notsack_list == NULL); 12483 kmem_cache_free(tcp_sack_info_cache, 12484 tcp->tcp_sack_info); 12485 tcp->tcp_sack_info = NULL; 12486 } 12487 tcp->tcp_snd_sack_ok = B_FALSE; 12488 } 12489 12490 /* 12491 * Now we know the exact TCP/IP header length, subtract 12492 * that from tcp_mss to get our side's MSS. 12493 */ 12494 tcp->tcp_mss -= tcp->tcp_hdr_len; 12495 /* 12496 * Here we assume that the other side's header size will be equal to 12497 * our header size. We calculate the real MSS accordingly. Need to 12498 * take into additional stuffs IPsec puts in. 12499 * 12500 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12501 */ 12502 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12503 ((tcp->tcp_ipversion == IPV4_VERSION ? 12504 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12505 12506 /* 12507 * Set MSS to the smaller one of both ends of the connection. 12508 * We should not have called tcp_mss_set() before, but our 12509 * side of the MSS should have been set to a proper value 12510 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12511 * STREAM head parameters properly. 12512 * 12513 * If we have a larger-than-16-bit window but the other side 12514 * didn't want to do window scale, tcp_rwnd_set() will take 12515 * care of that. 12516 */ 12517 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12518 } 12519 12520 /* 12521 * Sends the T_CONN_IND to the listener. The caller calls this 12522 * functions via squeue to get inside the listener's perimeter 12523 * once the 3 way hand shake is done a T_CONN_IND needs to be 12524 * sent. As an optimization, the caller can call this directly 12525 * if listener's perimeter is same as eager's. 12526 */ 12527 /* ARGSUSED */ 12528 void 12529 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12530 { 12531 conn_t *lconnp = (conn_t *)arg; 12532 tcp_t *listener = lconnp->conn_tcp; 12533 tcp_t *tcp; 12534 struct T_conn_ind *conn_ind; 12535 ipaddr_t *addr_cache; 12536 boolean_t need_send_conn_ind = B_FALSE; 12537 tcp_stack_t *tcps = listener->tcp_tcps; 12538 12539 /* retrieve the eager */ 12540 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12541 ASSERT(conn_ind->OPT_offset != 0 && 12542 conn_ind->OPT_length == sizeof (intptr_t)); 12543 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12544 conn_ind->OPT_length); 12545 12546 /* 12547 * TLI/XTI applications will get confused by 12548 * sending eager as an option since it violates 12549 * the option semantics. So remove the eager as 12550 * option since TLI/XTI app doesn't need it anyway. 12551 */ 12552 if (!TCP_IS_SOCKET(listener)) { 12553 conn_ind->OPT_length = 0; 12554 conn_ind->OPT_offset = 0; 12555 } 12556 if (listener->tcp_state == TCPS_CLOSED || 12557 TCP_IS_DETACHED(listener)) { 12558 /* 12559 * If listener has closed, it would have caused a 12560 * a cleanup/blowoff to happen for the eager. We 12561 * just need to return. 12562 */ 12563 freemsg(mp); 12564 return; 12565 } 12566 12567 12568 /* 12569 * if the conn_req_q is full defer passing up the 12570 * T_CONN_IND until space is availabe after t_accept() 12571 * processing 12572 */ 12573 mutex_enter(&listener->tcp_eager_lock); 12574 12575 /* 12576 * Take the eager out, if it is in the list of droppable eagers 12577 * as we are here because the 3W handshake is over. 12578 */ 12579 MAKE_UNDROPPABLE(tcp); 12580 12581 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12582 tcp_t *tail; 12583 12584 /* 12585 * The eager already has an extra ref put in tcp_rput_data 12586 * so that it stays till accept comes back even though it 12587 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12588 */ 12589 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12590 listener->tcp_conn_req_cnt_q0--; 12591 listener->tcp_conn_req_cnt_q++; 12592 12593 /* Move from SYN_RCVD to ESTABLISHED list */ 12594 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12595 tcp->tcp_eager_prev_q0; 12596 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12597 tcp->tcp_eager_next_q0; 12598 tcp->tcp_eager_prev_q0 = NULL; 12599 tcp->tcp_eager_next_q0 = NULL; 12600 12601 /* 12602 * Insert at end of the queue because sockfs 12603 * sends down T_CONN_RES in chronological 12604 * order. Leaving the older conn indications 12605 * at front of the queue helps reducing search 12606 * time. 12607 */ 12608 tail = listener->tcp_eager_last_q; 12609 if (tail != NULL) 12610 tail->tcp_eager_next_q = tcp; 12611 else 12612 listener->tcp_eager_next_q = tcp; 12613 listener->tcp_eager_last_q = tcp; 12614 tcp->tcp_eager_next_q = NULL; 12615 /* 12616 * Delay sending up the T_conn_ind until we are 12617 * done with the eager. Once we have have sent up 12618 * the T_conn_ind, the accept can potentially complete 12619 * any time and release the refhold we have on the eager. 12620 */ 12621 need_send_conn_ind = B_TRUE; 12622 } else { 12623 /* 12624 * Defer connection on q0 and set deferred 12625 * connection bit true 12626 */ 12627 tcp->tcp_conn_def_q0 = B_TRUE; 12628 12629 /* take tcp out of q0 ... */ 12630 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12631 tcp->tcp_eager_next_q0; 12632 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12633 tcp->tcp_eager_prev_q0; 12634 12635 /* ... and place it at the end of q0 */ 12636 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12637 tcp->tcp_eager_next_q0 = listener; 12638 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12639 listener->tcp_eager_prev_q0 = tcp; 12640 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12641 } 12642 12643 /* we have timed out before */ 12644 if (tcp->tcp_syn_rcvd_timeout != 0) { 12645 tcp->tcp_syn_rcvd_timeout = 0; 12646 listener->tcp_syn_rcvd_timeout--; 12647 if (listener->tcp_syn_defense && 12648 listener->tcp_syn_rcvd_timeout <= 12649 (tcps->tcps_conn_req_max_q0 >> 5) && 12650 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12651 listener->tcp_last_rcv_lbolt)) { 12652 /* 12653 * Turn off the defense mode if we 12654 * believe the SYN attack is over. 12655 */ 12656 listener->tcp_syn_defense = B_FALSE; 12657 if (listener->tcp_ip_addr_cache) { 12658 kmem_free((void *)listener->tcp_ip_addr_cache, 12659 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12660 listener->tcp_ip_addr_cache = NULL; 12661 } 12662 } 12663 } 12664 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12665 if (addr_cache != NULL) { 12666 /* 12667 * We have finished a 3-way handshake with this 12668 * remote host. This proves the IP addr is good. 12669 * Cache it! 12670 */ 12671 addr_cache[IP_ADDR_CACHE_HASH( 12672 tcp->tcp_remote)] = tcp->tcp_remote; 12673 } 12674 mutex_exit(&listener->tcp_eager_lock); 12675 if (need_send_conn_ind) 12676 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12677 } 12678 12679 /* 12680 * Send the newconn notification to ulp. The eager is blown off if the 12681 * notification fails. 12682 */ 12683 static void 12684 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12685 { 12686 if (IPCL_IS_NONSTR(lconnp)) { 12687 cred_t *cr; 12688 pid_t cpid; 12689 12690 cr = msg_getcred(mp, &cpid); 12691 12692 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12693 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12694 lconnp->conn_tcp); 12695 12696 /* Keep the message around in case of a fallback to TPI */ 12697 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12698 12699 /* 12700 * Notify the ULP about the newconn. It is guaranteed that no 12701 * tcp_accept() call will be made for the eager if the 12702 * notification fails, so it's safe to blow it off in that 12703 * case. 12704 * 12705 * The upper handle will be assigned when tcp_accept() is 12706 * called. 12707 */ 12708 if ((*lconnp->conn_upcalls->su_newconn) 12709 (lconnp->conn_upper_handle, 12710 (sock_lower_handle_t)econnp, 12711 &sock_tcp_downcalls, cr, cpid, 12712 &econnp->conn_upcalls) == NULL) { 12713 /* Failed to allocate a socket */ 12714 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12715 tcpEstabResets); 12716 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12717 econnp->conn_tcp->tcp_conn_req_seqnum); 12718 } 12719 } else { 12720 putnext(lconnp->conn_tcp->tcp_rq, mp); 12721 } 12722 } 12723 12724 mblk_t * 12725 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12726 uint_t *ifindexp, ip6_pkt_t *ippp) 12727 { 12728 ip_pktinfo_t *pinfo; 12729 ip6_t *ip6h; 12730 uchar_t *rptr; 12731 mblk_t *first_mp = mp; 12732 boolean_t mctl_present = B_FALSE; 12733 uint_t ifindex = 0; 12734 ip6_pkt_t ipp; 12735 uint_t ipvers; 12736 uint_t ip_hdr_len; 12737 tcp_stack_t *tcps = tcp->tcp_tcps; 12738 12739 rptr = mp->b_rptr; 12740 ASSERT(OK_32PTR(rptr)); 12741 ASSERT(tcp != NULL); 12742 ipp.ipp_fields = 0; 12743 12744 switch DB_TYPE(mp) { 12745 case M_CTL: 12746 mp = mp->b_cont; 12747 if (mp == NULL) { 12748 freemsg(first_mp); 12749 return (NULL); 12750 } 12751 if (DB_TYPE(mp) != M_DATA) { 12752 freemsg(first_mp); 12753 return (NULL); 12754 } 12755 mctl_present = B_TRUE; 12756 break; 12757 case M_DATA: 12758 break; 12759 default: 12760 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12761 freemsg(mp); 12762 return (NULL); 12763 } 12764 ipvers = IPH_HDR_VERSION(rptr); 12765 if (ipvers == IPV4_VERSION) { 12766 if (tcp == NULL) { 12767 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12768 goto done; 12769 } 12770 12771 ipp.ipp_fields |= IPPF_HOPLIMIT; 12772 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12773 12774 /* 12775 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12776 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12777 */ 12778 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12779 mctl_present) { 12780 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12781 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12782 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12783 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12784 ipp.ipp_fields |= IPPF_IFINDEX; 12785 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12786 ifindex = pinfo->ip_pkt_ifindex; 12787 } 12788 freeb(first_mp); 12789 mctl_present = B_FALSE; 12790 } 12791 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12792 } else { 12793 ip6h = (ip6_t *)rptr; 12794 12795 ASSERT(ipvers == IPV6_VERSION); 12796 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12797 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12798 ipp.ipp_hoplimit = ip6h->ip6_hops; 12799 12800 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12801 uint8_t nexthdrp; 12802 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12803 12804 /* Look for ifindex information */ 12805 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12806 ip6i_t *ip6i = (ip6i_t *)ip6h; 12807 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12808 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12809 freemsg(first_mp); 12810 return (NULL); 12811 } 12812 12813 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12814 ASSERT(ip6i->ip6i_ifindex != 0); 12815 ipp.ipp_fields |= IPPF_IFINDEX; 12816 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12817 ifindex = ip6i->ip6i_ifindex; 12818 } 12819 rptr = (uchar_t *)&ip6i[1]; 12820 mp->b_rptr = rptr; 12821 if (rptr == mp->b_wptr) { 12822 mblk_t *mp1; 12823 mp1 = mp->b_cont; 12824 freeb(mp); 12825 mp = mp1; 12826 rptr = mp->b_rptr; 12827 } 12828 if (MBLKL(mp) < IPV6_HDR_LEN + 12829 sizeof (tcph_t)) { 12830 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12831 freemsg(first_mp); 12832 return (NULL); 12833 } 12834 ip6h = (ip6_t *)rptr; 12835 } 12836 12837 /* 12838 * Find any potentially interesting extension headers 12839 * as well as the length of the IPv6 + extension 12840 * headers. 12841 */ 12842 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12843 /* Verify if this is a TCP packet */ 12844 if (nexthdrp != IPPROTO_TCP) { 12845 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12846 freemsg(first_mp); 12847 return (NULL); 12848 } 12849 } else { 12850 ip_hdr_len = IPV6_HDR_LEN; 12851 } 12852 } 12853 12854 done: 12855 if (ipversp != NULL) 12856 *ipversp = ipvers; 12857 if (ip_hdr_lenp != NULL) 12858 *ip_hdr_lenp = ip_hdr_len; 12859 if (ippp != NULL) 12860 *ippp = ipp; 12861 if (ifindexp != NULL) 12862 *ifindexp = ifindex; 12863 if (mctl_present) { 12864 freeb(first_mp); 12865 } 12866 return (mp); 12867 } 12868 12869 /* 12870 * Handle M_DATA messages from IP. Its called directly from IP via 12871 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12872 * in this path. 12873 * 12874 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12875 * v4 and v6), we are called through tcp_input() and a M_CTL can 12876 * be present for options but tcp_find_pktinfo() deals with it. We 12877 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12878 * 12879 * The first argument is always the connp/tcp to which the mp belongs. 12880 * There are no exceptions to this rule. The caller has already put 12881 * a reference on this connp/tcp and once tcp_rput_data() returns, 12882 * the squeue will do the refrele. 12883 * 12884 * The TH_SYN for the listener directly go to tcp_conn_request via 12885 * squeue. 12886 * 12887 * sqp: NULL = recursive, sqp != NULL means called from squeue 12888 */ 12889 void 12890 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12891 { 12892 int32_t bytes_acked; 12893 int32_t gap; 12894 mblk_t *mp1; 12895 uint_t flags; 12896 uint32_t new_swnd = 0; 12897 uchar_t *iphdr; 12898 uchar_t *rptr; 12899 int32_t rgap; 12900 uint32_t seg_ack; 12901 int seg_len; 12902 uint_t ip_hdr_len; 12903 uint32_t seg_seq; 12904 tcph_t *tcph; 12905 int urp; 12906 tcp_opt_t tcpopt; 12907 uint_t ipvers; 12908 ip6_pkt_t ipp; 12909 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12910 uint32_t cwnd; 12911 uint32_t add; 12912 int npkt; 12913 int mss; 12914 conn_t *connp = (conn_t *)arg; 12915 squeue_t *sqp = (squeue_t *)arg2; 12916 tcp_t *tcp = connp->conn_tcp; 12917 tcp_stack_t *tcps = tcp->tcp_tcps; 12918 12919 /* 12920 * RST from fused tcp loopback peer should trigger an unfuse. 12921 */ 12922 if (tcp->tcp_fused) { 12923 TCP_STAT(tcps, tcp_fusion_aborted); 12924 tcp_unfuse(tcp); 12925 } 12926 12927 iphdr = mp->b_rptr; 12928 rptr = mp->b_rptr; 12929 ASSERT(OK_32PTR(rptr)); 12930 12931 /* 12932 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12933 * processing here. For rest call tcp_find_pktinfo to fill up the 12934 * necessary information. 12935 */ 12936 if (IPCL_IS_TCP4(connp)) { 12937 ipvers = IPV4_VERSION; 12938 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12939 } else { 12940 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12941 NULL, &ipp); 12942 if (mp == NULL) { 12943 TCP_STAT(tcps, tcp_rput_v6_error); 12944 return; 12945 } 12946 iphdr = mp->b_rptr; 12947 rptr = mp->b_rptr; 12948 } 12949 ASSERT(DB_TYPE(mp) == M_DATA); 12950 ASSERT(mp->b_next == NULL); 12951 12952 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12953 seg_seq = ABE32_TO_U32(tcph->th_seq); 12954 seg_ack = ABE32_TO_U32(tcph->th_ack); 12955 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12956 seg_len = (int)(mp->b_wptr - rptr) - 12957 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12958 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12959 do { 12960 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12961 (uintptr_t)INT_MAX); 12962 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12963 } while ((mp1 = mp1->b_cont) != NULL && 12964 mp1->b_datap->db_type == M_DATA); 12965 } 12966 12967 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12968 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12969 seg_len, tcph); 12970 return; 12971 } 12972 12973 if (sqp != NULL) { 12974 /* 12975 * This is the correct place to update tcp_last_recv_time. Note 12976 * that it is also updated for tcp structure that belongs to 12977 * global and listener queues which do not really need updating. 12978 * But that should not cause any harm. And it is updated for 12979 * all kinds of incoming segments, not only for data segments. 12980 */ 12981 tcp->tcp_last_recv_time = lbolt; 12982 } 12983 12984 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12985 12986 BUMP_LOCAL(tcp->tcp_ibsegs); 12987 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12988 12989 if ((flags & TH_URG) && sqp != NULL) { 12990 /* 12991 * TCP can't handle urgent pointers that arrive before 12992 * the connection has been accept()ed since it can't 12993 * buffer OOB data. Discard segment if this happens. 12994 * 12995 * We can't just rely on a non-null tcp_listener to indicate 12996 * that the accept() has completed since unlinking of the 12997 * eager and completion of the accept are not atomic. 12998 * tcp_detached, when it is not set (B_FALSE) indicates 12999 * that the accept() has completed. 13000 * 13001 * Nor can it reassemble urgent pointers, so discard 13002 * if it's not the next segment expected. 13003 * 13004 * Otherwise, collapse chain into one mblk (discard if 13005 * that fails). This makes sure the headers, retransmitted 13006 * data, and new data all are in the same mblk. 13007 */ 13008 ASSERT(mp != NULL); 13009 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13010 freemsg(mp); 13011 return; 13012 } 13013 /* Update pointers into message */ 13014 iphdr = rptr = mp->b_rptr; 13015 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13016 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13017 /* 13018 * Since we can't handle any data with this urgent 13019 * pointer that is out of sequence, we expunge 13020 * the data. This allows us to still register 13021 * the urgent mark and generate the M_PCSIG, 13022 * which we can do. 13023 */ 13024 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13025 seg_len = 0; 13026 } 13027 } 13028 13029 switch (tcp->tcp_state) { 13030 case TCPS_SYN_SENT: 13031 if (flags & TH_ACK) { 13032 /* 13033 * Note that our stack cannot send data before a 13034 * connection is established, therefore the 13035 * following check is valid. Otherwise, it has 13036 * to be changed. 13037 */ 13038 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13039 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13040 freemsg(mp); 13041 if (flags & TH_RST) 13042 return; 13043 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13044 tcp, seg_ack, 0, TH_RST); 13045 return; 13046 } 13047 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13048 } 13049 if (flags & TH_RST) { 13050 freemsg(mp); 13051 if (flags & TH_ACK) 13052 (void) tcp_clean_death(tcp, 13053 ECONNREFUSED, 13); 13054 return; 13055 } 13056 if (!(flags & TH_SYN)) { 13057 freemsg(mp); 13058 return; 13059 } 13060 13061 /* Process all TCP options. */ 13062 tcp_process_options(tcp, tcph); 13063 /* 13064 * The following changes our rwnd to be a multiple of the 13065 * MIN(peer MSS, our MSS) for performance reason. 13066 */ 13067 (void) tcp_rwnd_set(tcp, 13068 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 13069 13070 /* Is the other end ECN capable? */ 13071 if (tcp->tcp_ecn_ok) { 13072 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13073 tcp->tcp_ecn_ok = B_FALSE; 13074 } 13075 } 13076 /* 13077 * Clear ECN flags because it may interfere with later 13078 * processing. 13079 */ 13080 flags &= ~(TH_ECE|TH_CWR); 13081 13082 tcp->tcp_irs = seg_seq; 13083 tcp->tcp_rack = seg_seq; 13084 tcp->tcp_rnxt = seg_seq + 1; 13085 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13086 if (!TCP_IS_DETACHED(tcp)) { 13087 /* Allocate room for SACK options if needed. */ 13088 if (tcp->tcp_snd_sack_ok) { 13089 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13090 tcp->tcp_hdr_len + 13091 TCPOPT_MAX_SACK_LEN + 13092 (tcp->tcp_loopback ? 0 : 13093 tcps->tcps_wroff_xtra)); 13094 } else { 13095 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13096 tcp->tcp_hdr_len + 13097 (tcp->tcp_loopback ? 0 : 13098 tcps->tcps_wroff_xtra)); 13099 } 13100 } 13101 if (flags & TH_ACK) { 13102 /* 13103 * If we can't get the confirmation upstream, pretend 13104 * we didn't even see this one. 13105 * 13106 * XXX: how can we pretend we didn't see it if we 13107 * have updated rnxt et. al. 13108 * 13109 * For loopback we defer sending up the T_CONN_CON 13110 * until after some checks below. 13111 */ 13112 mp1 = NULL; 13113 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13114 tcp->tcp_loopback ? &mp1 : NULL)) { 13115 freemsg(mp); 13116 return; 13117 } 13118 /* SYN was acked - making progress */ 13119 if (tcp->tcp_ipversion == IPV6_VERSION) 13120 tcp->tcp_ip_forward_progress = B_TRUE; 13121 13122 /* One for the SYN */ 13123 tcp->tcp_suna = tcp->tcp_iss + 1; 13124 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13125 tcp->tcp_state = TCPS_ESTABLISHED; 13126 13127 /* 13128 * If SYN was retransmitted, need to reset all 13129 * retransmission info. This is because this 13130 * segment will be treated as a dup ACK. 13131 */ 13132 if (tcp->tcp_rexmit) { 13133 tcp->tcp_rexmit = B_FALSE; 13134 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13135 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13136 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13137 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13138 tcp->tcp_ms_we_have_waited = 0; 13139 13140 /* 13141 * Set tcp_cwnd back to 1 MSS, per 13142 * recommendation from 13143 * draft-floyd-incr-init-win-01.txt, 13144 * Increasing TCP's Initial Window. 13145 */ 13146 tcp->tcp_cwnd = tcp->tcp_mss; 13147 } 13148 13149 tcp->tcp_swl1 = seg_seq; 13150 tcp->tcp_swl2 = seg_ack; 13151 13152 new_swnd = BE16_TO_U16(tcph->th_win); 13153 tcp->tcp_swnd = new_swnd; 13154 if (new_swnd > tcp->tcp_max_swnd) 13155 tcp->tcp_max_swnd = new_swnd; 13156 13157 /* 13158 * Always send the three-way handshake ack immediately 13159 * in order to make the connection complete as soon as 13160 * possible on the accepting host. 13161 */ 13162 flags |= TH_ACK_NEEDED; 13163 13164 /* 13165 * Special case for loopback. At this point we have 13166 * received SYN-ACK from the remote endpoint. In 13167 * order to ensure that both endpoints reach the 13168 * fused state prior to any data exchange, the final 13169 * ACK needs to be sent before we indicate T_CONN_CON 13170 * to the module upstream. 13171 */ 13172 if (tcp->tcp_loopback) { 13173 mblk_t *ack_mp; 13174 13175 ASSERT(!tcp->tcp_unfusable); 13176 ASSERT(mp1 != NULL); 13177 /* 13178 * For loopback, we always get a pure SYN-ACK 13179 * and only need to send back the final ACK 13180 * with no data (this is because the other 13181 * tcp is ours and we don't do T/TCP). This 13182 * final ACK triggers the passive side to 13183 * perform fusion in ESTABLISHED state. 13184 */ 13185 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13186 if (tcp->tcp_ack_tid != 0) { 13187 (void) TCP_TIMER_CANCEL(tcp, 13188 tcp->tcp_ack_tid); 13189 tcp->tcp_ack_tid = 0; 13190 } 13191 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13192 BUMP_LOCAL(tcp->tcp_obsegs); 13193 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13194 13195 if (!IPCL_IS_NONSTR(connp)) { 13196 /* Send up T_CONN_CON */ 13197 putnext(tcp->tcp_rq, mp1); 13198 } else { 13199 cred_t *cr; 13200 pid_t cpid; 13201 13202 cr = msg_getcred(mp1, &cpid); 13203 (*connp->conn_upcalls-> 13204 su_connected) 13205 (connp->conn_upper_handle, 13206 tcp->tcp_connid, cr, cpid); 13207 freemsg(mp1); 13208 } 13209 13210 freemsg(mp); 13211 return; 13212 } 13213 /* 13214 * Forget fusion; we need to handle more 13215 * complex cases below. Send the deferred 13216 * T_CONN_CON message upstream and proceed 13217 * as usual. Mark this tcp as not capable 13218 * of fusion. 13219 */ 13220 TCP_STAT(tcps, tcp_fusion_unfusable); 13221 tcp->tcp_unfusable = B_TRUE; 13222 if (!IPCL_IS_NONSTR(connp)) { 13223 putnext(tcp->tcp_rq, mp1); 13224 } else { 13225 cred_t *cr; 13226 pid_t cpid; 13227 13228 cr = msg_getcred(mp1, &cpid); 13229 (*connp->conn_upcalls->su_connected) 13230 (connp->conn_upper_handle, 13231 tcp->tcp_connid, cr, cpid); 13232 freemsg(mp1); 13233 } 13234 } 13235 13236 /* 13237 * Check to see if there is data to be sent. If 13238 * yes, set the transmit flag. Then check to see 13239 * if received data processing needs to be done. 13240 * If not, go straight to xmit_check. This short 13241 * cut is OK as we don't support T/TCP. 13242 */ 13243 if (tcp->tcp_unsent) 13244 flags |= TH_XMIT_NEEDED; 13245 13246 if (seg_len == 0 && !(flags & TH_URG)) { 13247 freemsg(mp); 13248 goto xmit_check; 13249 } 13250 13251 flags &= ~TH_SYN; 13252 seg_seq++; 13253 break; 13254 } 13255 tcp->tcp_state = TCPS_SYN_RCVD; 13256 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13257 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13258 if (mp1) { 13259 /* 13260 * See comment in tcp_conn_request() for why we use 13261 * the open() time pid here. 13262 */ 13263 DB_CPID(mp1) = tcp->tcp_cpid; 13264 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13265 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13266 } 13267 freemsg(mp); 13268 return; 13269 case TCPS_SYN_RCVD: 13270 if (flags & TH_ACK) { 13271 /* 13272 * In this state, a SYN|ACK packet is either bogus 13273 * because the other side must be ACKing our SYN which 13274 * indicates it has seen the ACK for their SYN and 13275 * shouldn't retransmit it or we're crossing SYNs 13276 * on active open. 13277 */ 13278 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13279 freemsg(mp); 13280 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13281 tcp, seg_ack, 0, TH_RST); 13282 return; 13283 } 13284 /* 13285 * NOTE: RFC 793 pg. 72 says this should be 13286 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13287 * but that would mean we have an ack that ignored 13288 * our SYN. 13289 */ 13290 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13291 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13292 freemsg(mp); 13293 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13294 tcp, seg_ack, 0, TH_RST); 13295 return; 13296 } 13297 } 13298 break; 13299 case TCPS_LISTEN: 13300 /* 13301 * Only a TLI listener can come through this path when a 13302 * acceptor is going back to be a listener and a packet 13303 * for the acceptor hits the classifier. For a socket 13304 * listener, this can never happen because a listener 13305 * can never accept connection on itself and hence a 13306 * socket acceptor can not go back to being a listener. 13307 */ 13308 ASSERT(!TCP_IS_SOCKET(tcp)); 13309 /*FALLTHRU*/ 13310 case TCPS_CLOSED: 13311 case TCPS_BOUND: { 13312 conn_t *new_connp; 13313 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13314 13315 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13316 if (new_connp != NULL) { 13317 tcp_reinput(new_connp, mp, connp->conn_sqp); 13318 return; 13319 } 13320 /* We failed to classify. For now just drop the packet */ 13321 freemsg(mp); 13322 return; 13323 } 13324 case TCPS_IDLE: 13325 /* 13326 * Handle the case where the tcp_clean_death() has happened 13327 * on a connection (application hasn't closed yet) but a packet 13328 * was already queued on squeue before tcp_clean_death() 13329 * was processed. Calling tcp_clean_death() twice on same 13330 * connection can result in weird behaviour. 13331 */ 13332 freemsg(mp); 13333 return; 13334 default: 13335 break; 13336 } 13337 13338 /* 13339 * Already on the correct queue/perimeter. 13340 * If this is a detached connection and not an eager 13341 * connection hanging off a listener then new data 13342 * (past the FIN) will cause a reset. 13343 * We do a special check here where it 13344 * is out of the main line, rather than check 13345 * if we are detached every time we see new 13346 * data down below. 13347 */ 13348 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13349 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13350 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13351 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13352 13353 freemsg(mp); 13354 /* 13355 * This could be an SSL closure alert. We're detached so just 13356 * acknowledge it this last time. 13357 */ 13358 if (tcp->tcp_kssl_ctx != NULL) { 13359 kssl_release_ctx(tcp->tcp_kssl_ctx); 13360 tcp->tcp_kssl_ctx = NULL; 13361 13362 tcp->tcp_rnxt += seg_len; 13363 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13364 flags |= TH_ACK_NEEDED; 13365 goto ack_check; 13366 } 13367 13368 tcp_xmit_ctl("new data when detached", tcp, 13369 tcp->tcp_snxt, 0, TH_RST); 13370 (void) tcp_clean_death(tcp, EPROTO, 12); 13371 return; 13372 } 13373 13374 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13375 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13376 new_swnd = BE16_TO_U16(tcph->th_win) << 13377 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13378 13379 if (tcp->tcp_snd_ts_ok) { 13380 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13381 /* 13382 * This segment is not acceptable. 13383 * Drop it and send back an ACK. 13384 */ 13385 freemsg(mp); 13386 flags |= TH_ACK_NEEDED; 13387 goto ack_check; 13388 } 13389 } else if (tcp->tcp_snd_sack_ok) { 13390 ASSERT(tcp->tcp_sack_info != NULL); 13391 tcpopt.tcp = tcp; 13392 /* 13393 * SACK info in already updated in tcp_parse_options. Ignore 13394 * all other TCP options... 13395 */ 13396 (void) tcp_parse_options(tcph, &tcpopt); 13397 } 13398 try_again:; 13399 mss = tcp->tcp_mss; 13400 gap = seg_seq - tcp->tcp_rnxt; 13401 rgap = tcp->tcp_rwnd - (gap + seg_len); 13402 /* 13403 * gap is the amount of sequence space between what we expect to see 13404 * and what we got for seg_seq. A positive value for gap means 13405 * something got lost. A negative value means we got some old stuff. 13406 */ 13407 if (gap < 0) { 13408 /* Old stuff present. Is the SYN in there? */ 13409 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13410 (seg_len != 0)) { 13411 flags &= ~TH_SYN; 13412 seg_seq++; 13413 urp--; 13414 /* Recompute the gaps after noting the SYN. */ 13415 goto try_again; 13416 } 13417 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13418 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13419 (seg_len > -gap ? -gap : seg_len)); 13420 /* Remove the old stuff from seg_len. */ 13421 seg_len += gap; 13422 /* 13423 * Anything left? 13424 * Make sure to check for unack'd FIN when rest of data 13425 * has been previously ack'd. 13426 */ 13427 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13428 /* 13429 * Resets are only valid if they lie within our offered 13430 * window. If the RST bit is set, we just ignore this 13431 * segment. 13432 */ 13433 if (flags & TH_RST) { 13434 freemsg(mp); 13435 return; 13436 } 13437 13438 /* 13439 * The arriving of dup data packets indicate that we 13440 * may have postponed an ack for too long, or the other 13441 * side's RTT estimate is out of shape. Start acking 13442 * more often. 13443 */ 13444 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13445 tcp->tcp_rack_cnt >= 1 && 13446 tcp->tcp_rack_abs_max > 2) { 13447 tcp->tcp_rack_abs_max--; 13448 } 13449 tcp->tcp_rack_cur_max = 1; 13450 13451 /* 13452 * This segment is "unacceptable". None of its 13453 * sequence space lies within our advertized window. 13454 * 13455 * Adjust seg_len to the original value for tracing. 13456 */ 13457 seg_len -= gap; 13458 if (tcp->tcp_debug) { 13459 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13460 "tcp_rput: unacceptable, gap %d, rgap %d, " 13461 "flags 0x%x, seg_seq %u, seg_ack %u, " 13462 "seg_len %d, rnxt %u, snxt %u, %s", 13463 gap, rgap, flags, seg_seq, seg_ack, 13464 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13465 tcp_display(tcp, NULL, 13466 DISP_ADDR_AND_PORT)); 13467 } 13468 13469 /* 13470 * Arrange to send an ACK in response to the 13471 * unacceptable segment per RFC 793 page 69. There 13472 * is only one small difference between ours and the 13473 * acceptability test in the RFC - we accept ACK-only 13474 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13475 * will be generated. 13476 * 13477 * Note that we have to ACK an ACK-only packet at least 13478 * for stacks that send 0-length keep-alives with 13479 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13480 * section 4.2.3.6. As long as we don't ever generate 13481 * an unacceptable packet in response to an incoming 13482 * packet that is unacceptable, it should not cause 13483 * "ACK wars". 13484 */ 13485 flags |= TH_ACK_NEEDED; 13486 13487 /* 13488 * Continue processing this segment in order to use the 13489 * ACK information it contains, but skip all other 13490 * sequence-number processing. Processing the ACK 13491 * information is necessary in order to 13492 * re-synchronize connections that may have lost 13493 * synchronization. 13494 * 13495 * We clear seg_len and flag fields related to 13496 * sequence number processing as they are not 13497 * to be trusted for an unacceptable segment. 13498 */ 13499 seg_len = 0; 13500 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13501 goto process_ack; 13502 } 13503 13504 /* Fix seg_seq, and chew the gap off the front. */ 13505 seg_seq = tcp->tcp_rnxt; 13506 urp += gap; 13507 do { 13508 mblk_t *mp2; 13509 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13510 (uintptr_t)UINT_MAX); 13511 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13512 if (gap > 0) { 13513 mp->b_rptr = mp->b_wptr - gap; 13514 break; 13515 } 13516 mp2 = mp; 13517 mp = mp->b_cont; 13518 freeb(mp2); 13519 } while (gap < 0); 13520 /* 13521 * If the urgent data has already been acknowledged, we 13522 * should ignore TH_URG below 13523 */ 13524 if (urp < 0) 13525 flags &= ~TH_URG; 13526 } 13527 /* 13528 * rgap is the amount of stuff received out of window. A negative 13529 * value is the amount out of window. 13530 */ 13531 if (rgap < 0) { 13532 mblk_t *mp2; 13533 13534 if (tcp->tcp_rwnd == 0) { 13535 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13536 } else { 13537 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13538 UPDATE_MIB(&tcps->tcps_mib, 13539 tcpInDataPastWinBytes, -rgap); 13540 } 13541 13542 /* 13543 * seg_len does not include the FIN, so if more than 13544 * just the FIN is out of window, we act like we don't 13545 * see it. (If just the FIN is out of window, rgap 13546 * will be zero and we will go ahead and acknowledge 13547 * the FIN.) 13548 */ 13549 flags &= ~TH_FIN; 13550 13551 /* Fix seg_len and make sure there is something left. */ 13552 seg_len += rgap; 13553 if (seg_len <= 0) { 13554 /* 13555 * Resets are only valid if they lie within our offered 13556 * window. If the RST bit is set, we just ignore this 13557 * segment. 13558 */ 13559 if (flags & TH_RST) { 13560 freemsg(mp); 13561 return; 13562 } 13563 13564 /* Per RFC 793, we need to send back an ACK. */ 13565 flags |= TH_ACK_NEEDED; 13566 13567 /* 13568 * Send SIGURG as soon as possible i.e. even 13569 * if the TH_URG was delivered in a window probe 13570 * packet (which will be unacceptable). 13571 * 13572 * We generate a signal if none has been generated 13573 * for this connection or if this is a new urgent 13574 * byte. Also send a zero-length "unmarked" message 13575 * to inform SIOCATMARK that this is not the mark. 13576 * 13577 * tcp_urp_last_valid is cleared when the T_exdata_ind 13578 * is sent up. This plus the check for old data 13579 * (gap >= 0) handles the wraparound of the sequence 13580 * number space without having to always track the 13581 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13582 * this max in its rcv_up variable). 13583 * 13584 * This prevents duplicate SIGURGS due to a "late" 13585 * zero-window probe when the T_EXDATA_IND has already 13586 * been sent up. 13587 */ 13588 if ((flags & TH_URG) && 13589 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13590 tcp->tcp_urp_last))) { 13591 if (IPCL_IS_NONSTR(connp)) { 13592 if (!TCP_IS_DETACHED(tcp)) { 13593 (*connp->conn_upcalls-> 13594 su_signal_oob) 13595 (connp->conn_upper_handle, 13596 urp); 13597 } 13598 } else { 13599 mp1 = allocb(0, BPRI_MED); 13600 if (mp1 == NULL) { 13601 freemsg(mp); 13602 return; 13603 } 13604 if (!TCP_IS_DETACHED(tcp) && 13605 !putnextctl1(tcp->tcp_rq, 13606 M_PCSIG, SIGURG)) { 13607 /* Try again on the rexmit. */ 13608 freemsg(mp1); 13609 freemsg(mp); 13610 return; 13611 } 13612 /* 13613 * If the next byte would be the mark 13614 * then mark with MARKNEXT else mark 13615 * with NOTMARKNEXT. 13616 */ 13617 if (gap == 0 && urp == 0) 13618 mp1->b_flag |= MSGMARKNEXT; 13619 else 13620 mp1->b_flag |= MSGNOTMARKNEXT; 13621 freemsg(tcp->tcp_urp_mark_mp); 13622 tcp->tcp_urp_mark_mp = mp1; 13623 flags |= TH_SEND_URP_MARK; 13624 } 13625 tcp->tcp_urp_last_valid = B_TRUE; 13626 tcp->tcp_urp_last = urp + seg_seq; 13627 } 13628 /* 13629 * If this is a zero window probe, continue to 13630 * process the ACK part. But we need to set seg_len 13631 * to 0 to avoid data processing. Otherwise just 13632 * drop the segment and send back an ACK. 13633 */ 13634 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13635 flags &= ~(TH_SYN | TH_URG); 13636 seg_len = 0; 13637 goto process_ack; 13638 } else { 13639 freemsg(mp); 13640 goto ack_check; 13641 } 13642 } 13643 /* Pitch out of window stuff off the end. */ 13644 rgap = seg_len; 13645 mp2 = mp; 13646 do { 13647 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13648 (uintptr_t)INT_MAX); 13649 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13650 if (rgap < 0) { 13651 mp2->b_wptr += rgap; 13652 if ((mp1 = mp2->b_cont) != NULL) { 13653 mp2->b_cont = NULL; 13654 freemsg(mp1); 13655 } 13656 break; 13657 } 13658 } while ((mp2 = mp2->b_cont) != NULL); 13659 } 13660 ok:; 13661 /* 13662 * TCP should check ECN info for segments inside the window only. 13663 * Therefore the check should be done here. 13664 */ 13665 if (tcp->tcp_ecn_ok) { 13666 if (flags & TH_CWR) { 13667 tcp->tcp_ecn_echo_on = B_FALSE; 13668 } 13669 /* 13670 * Note that both ECN_CE and CWR can be set in the 13671 * same segment. In this case, we once again turn 13672 * on ECN_ECHO. 13673 */ 13674 if (tcp->tcp_ipversion == IPV4_VERSION) { 13675 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13676 13677 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13678 tcp->tcp_ecn_echo_on = B_TRUE; 13679 } 13680 } else { 13681 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13682 13683 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13684 htonl(IPH_ECN_CE << 20)) { 13685 tcp->tcp_ecn_echo_on = B_TRUE; 13686 } 13687 } 13688 } 13689 13690 /* 13691 * Check whether we can update tcp_ts_recent. This test is 13692 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13693 * Extensions for High Performance: An Update", Internet Draft. 13694 */ 13695 if (tcp->tcp_snd_ts_ok && 13696 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13697 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13698 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13699 tcp->tcp_last_rcv_lbolt = lbolt64; 13700 } 13701 13702 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13703 /* 13704 * FIN in an out of order segment. We record this in 13705 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13706 * Clear the FIN so that any check on FIN flag will fail. 13707 * Remember that FIN also counts in the sequence number 13708 * space. So we need to ack out of order FIN only segments. 13709 */ 13710 if (flags & TH_FIN) { 13711 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13712 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13713 flags &= ~TH_FIN; 13714 flags |= TH_ACK_NEEDED; 13715 } 13716 if (seg_len > 0) { 13717 /* Fill in the SACK blk list. */ 13718 if (tcp->tcp_snd_sack_ok) { 13719 ASSERT(tcp->tcp_sack_info != NULL); 13720 tcp_sack_insert(tcp->tcp_sack_list, 13721 seg_seq, seg_seq + seg_len, 13722 &(tcp->tcp_num_sack_blk)); 13723 } 13724 13725 /* 13726 * Attempt reassembly and see if we have something 13727 * ready to go. 13728 */ 13729 mp = tcp_reass(tcp, mp, seg_seq); 13730 /* Always ack out of order packets */ 13731 flags |= TH_ACK_NEEDED | TH_PUSH; 13732 if (mp) { 13733 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13734 (uintptr_t)INT_MAX); 13735 seg_len = mp->b_cont ? msgdsize(mp) : 13736 (int)(mp->b_wptr - mp->b_rptr); 13737 seg_seq = tcp->tcp_rnxt; 13738 /* 13739 * A gap is filled and the seq num and len 13740 * of the gap match that of a previously 13741 * received FIN, put the FIN flag back in. 13742 */ 13743 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13744 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13745 flags |= TH_FIN; 13746 tcp->tcp_valid_bits &= 13747 ~TCP_OFO_FIN_VALID; 13748 } 13749 } else { 13750 /* 13751 * Keep going even with NULL mp. 13752 * There may be a useful ACK or something else 13753 * we don't want to miss. 13754 * 13755 * But TCP should not perform fast retransmit 13756 * because of the ack number. TCP uses 13757 * seg_len == 0 to determine if it is a pure 13758 * ACK. And this is not a pure ACK. 13759 */ 13760 seg_len = 0; 13761 ofo_seg = B_TRUE; 13762 } 13763 } 13764 } else if (seg_len > 0) { 13765 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13766 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13767 /* 13768 * If an out of order FIN was received before, and the seq 13769 * num and len of the new segment match that of the FIN, 13770 * put the FIN flag back in. 13771 */ 13772 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13773 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13774 flags |= TH_FIN; 13775 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13776 } 13777 } 13778 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13779 if (flags & TH_RST) { 13780 freemsg(mp); 13781 switch (tcp->tcp_state) { 13782 case TCPS_SYN_RCVD: 13783 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13784 break; 13785 case TCPS_ESTABLISHED: 13786 case TCPS_FIN_WAIT_1: 13787 case TCPS_FIN_WAIT_2: 13788 case TCPS_CLOSE_WAIT: 13789 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13790 break; 13791 case TCPS_CLOSING: 13792 case TCPS_LAST_ACK: 13793 (void) tcp_clean_death(tcp, 0, 16); 13794 break; 13795 default: 13796 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13797 (void) tcp_clean_death(tcp, ENXIO, 17); 13798 break; 13799 } 13800 return; 13801 } 13802 if (flags & TH_SYN) { 13803 /* 13804 * See RFC 793, Page 71 13805 * 13806 * The seq number must be in the window as it should 13807 * be "fixed" above. If it is outside window, it should 13808 * be already rejected. Note that we allow seg_seq to be 13809 * rnxt + rwnd because we want to accept 0 window probe. 13810 */ 13811 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13812 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13813 freemsg(mp); 13814 /* 13815 * If the ACK flag is not set, just use our snxt as the 13816 * seq number of the RST segment. 13817 */ 13818 if (!(flags & TH_ACK)) { 13819 seg_ack = tcp->tcp_snxt; 13820 } 13821 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13822 TH_RST|TH_ACK); 13823 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13824 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13825 return; 13826 } 13827 /* 13828 * urp could be -1 when the urp field in the packet is 0 13829 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13830 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13831 */ 13832 if (flags & TH_URG && urp >= 0) { 13833 if (!tcp->tcp_urp_last_valid || 13834 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13835 if (IPCL_IS_NONSTR(connp)) { 13836 if (!TCP_IS_DETACHED(tcp)) { 13837 (*connp->conn_upcalls->su_signal_oob) 13838 (connp->conn_upper_handle, urp); 13839 } 13840 } else { 13841 /* 13842 * If we haven't generated the signal yet for 13843 * this urgent pointer value, do it now. Also, 13844 * send up a zero-length M_DATA indicating 13845 * whether or not this is the mark. The latter 13846 * is not needed when a T_EXDATA_IND is sent up. 13847 * However, if there are allocation failures 13848 * this code relies on the sender retransmitting 13849 * and the socket code for determining the mark 13850 * should not block waiting for the peer to 13851 * transmit. Thus, for simplicity we always 13852 * send up the mark indication. 13853 */ 13854 mp1 = allocb(0, BPRI_MED); 13855 if (mp1 == NULL) { 13856 freemsg(mp); 13857 return; 13858 } 13859 if (!TCP_IS_DETACHED(tcp) && 13860 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13861 SIGURG)) { 13862 /* Try again on the rexmit. */ 13863 freemsg(mp1); 13864 freemsg(mp); 13865 return; 13866 } 13867 /* 13868 * Mark with NOTMARKNEXT for now. 13869 * The code below will change this to MARKNEXT 13870 * if we are at the mark. 13871 * 13872 * If there are allocation failures (e.g. in 13873 * dupmsg below) the next time tcp_rput_data 13874 * sees the urgent segment it will send up the 13875 * MSGMARKNEXT message. 13876 */ 13877 mp1->b_flag |= MSGNOTMARKNEXT; 13878 freemsg(tcp->tcp_urp_mark_mp); 13879 tcp->tcp_urp_mark_mp = mp1; 13880 flags |= TH_SEND_URP_MARK; 13881 #ifdef DEBUG 13882 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13883 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13884 "last %x, %s", 13885 seg_seq, urp, tcp->tcp_urp_last, 13886 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13887 #endif /* DEBUG */ 13888 } 13889 tcp->tcp_urp_last_valid = B_TRUE; 13890 tcp->tcp_urp_last = urp + seg_seq; 13891 } else if (tcp->tcp_urp_mark_mp != NULL) { 13892 /* 13893 * An allocation failure prevented the previous 13894 * tcp_rput_data from sending up the allocated 13895 * MSG*MARKNEXT message - send it up this time 13896 * around. 13897 */ 13898 flags |= TH_SEND_URP_MARK; 13899 } 13900 13901 /* 13902 * If the urgent byte is in this segment, make sure that it is 13903 * all by itself. This makes it much easier to deal with the 13904 * possibility of an allocation failure on the T_exdata_ind. 13905 * Note that seg_len is the number of bytes in the segment, and 13906 * urp is the offset into the segment of the urgent byte. 13907 * urp < seg_len means that the urgent byte is in this segment. 13908 */ 13909 if (urp < seg_len) { 13910 if (seg_len != 1) { 13911 uint32_t tmp_rnxt; 13912 /* 13913 * Break it up and feed it back in. 13914 * Re-attach the IP header. 13915 */ 13916 mp->b_rptr = iphdr; 13917 if (urp > 0) { 13918 /* 13919 * There is stuff before the urgent 13920 * byte. 13921 */ 13922 mp1 = dupmsg(mp); 13923 if (!mp1) { 13924 /* 13925 * Trim from urgent byte on. 13926 * The rest will come back. 13927 */ 13928 (void) adjmsg(mp, 13929 urp - seg_len); 13930 tcp_rput_data(connp, 13931 mp, NULL); 13932 return; 13933 } 13934 (void) adjmsg(mp1, urp - seg_len); 13935 /* Feed this piece back in. */ 13936 tmp_rnxt = tcp->tcp_rnxt; 13937 tcp_rput_data(connp, mp1, NULL); 13938 /* 13939 * If the data passed back in was not 13940 * processed (ie: bad ACK) sending 13941 * the remainder back in will cause a 13942 * loop. In this case, drop the 13943 * packet and let the sender try 13944 * sending a good packet. 13945 */ 13946 if (tmp_rnxt == tcp->tcp_rnxt) { 13947 freemsg(mp); 13948 return; 13949 } 13950 } 13951 if (urp != seg_len - 1) { 13952 uint32_t tmp_rnxt; 13953 /* 13954 * There is stuff after the urgent 13955 * byte. 13956 */ 13957 mp1 = dupmsg(mp); 13958 if (!mp1) { 13959 /* 13960 * Trim everything beyond the 13961 * urgent byte. The rest will 13962 * come back. 13963 */ 13964 (void) adjmsg(mp, 13965 urp + 1 - seg_len); 13966 tcp_rput_data(connp, 13967 mp, NULL); 13968 return; 13969 } 13970 (void) adjmsg(mp1, urp + 1 - seg_len); 13971 tmp_rnxt = tcp->tcp_rnxt; 13972 tcp_rput_data(connp, mp1, NULL); 13973 /* 13974 * If the data passed back in was not 13975 * processed (ie: bad ACK) sending 13976 * the remainder back in will cause a 13977 * loop. In this case, drop the 13978 * packet and let the sender try 13979 * sending a good packet. 13980 */ 13981 if (tmp_rnxt == tcp->tcp_rnxt) { 13982 freemsg(mp); 13983 return; 13984 } 13985 } 13986 tcp_rput_data(connp, mp, NULL); 13987 return; 13988 } 13989 /* 13990 * This segment contains only the urgent byte. We 13991 * have to allocate the T_exdata_ind, if we can. 13992 */ 13993 if (IPCL_IS_NONSTR(connp)) { 13994 int error; 13995 13996 (*connp->conn_upcalls->su_recv) 13997 (connp->conn_upper_handle, mp, seg_len, 13998 MSG_OOB, &error, NULL); 13999 /* 14000 * We should never be in middle of a 14001 * fallback, the squeue guarantees that. 14002 */ 14003 ASSERT(error != EOPNOTSUPP); 14004 mp = NULL; 14005 goto update_ack; 14006 } else if (!tcp->tcp_urp_mp) { 14007 struct T_exdata_ind *tei; 14008 mp1 = allocb(sizeof (struct T_exdata_ind), 14009 BPRI_MED); 14010 if (!mp1) { 14011 /* 14012 * Sigh... It'll be back. 14013 * Generate any MSG*MARK message now. 14014 */ 14015 freemsg(mp); 14016 seg_len = 0; 14017 if (flags & TH_SEND_URP_MARK) { 14018 14019 14020 ASSERT(tcp->tcp_urp_mark_mp); 14021 tcp->tcp_urp_mark_mp->b_flag &= 14022 ~MSGNOTMARKNEXT; 14023 tcp->tcp_urp_mark_mp->b_flag |= 14024 MSGMARKNEXT; 14025 } 14026 goto ack_check; 14027 } 14028 mp1->b_datap->db_type = M_PROTO; 14029 tei = (struct T_exdata_ind *)mp1->b_rptr; 14030 tei->PRIM_type = T_EXDATA_IND; 14031 tei->MORE_flag = 0; 14032 mp1->b_wptr = (uchar_t *)&tei[1]; 14033 tcp->tcp_urp_mp = mp1; 14034 #ifdef DEBUG 14035 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14036 "tcp_rput: allocated exdata_ind %s", 14037 tcp_display(tcp, NULL, 14038 DISP_PORT_ONLY)); 14039 #endif /* DEBUG */ 14040 /* 14041 * There is no need to send a separate MSG*MARK 14042 * message since the T_EXDATA_IND will be sent 14043 * now. 14044 */ 14045 flags &= ~TH_SEND_URP_MARK; 14046 freemsg(tcp->tcp_urp_mark_mp); 14047 tcp->tcp_urp_mark_mp = NULL; 14048 } 14049 /* 14050 * Now we are all set. On the next putnext upstream, 14051 * tcp_urp_mp will be non-NULL and will get prepended 14052 * to what has to be this piece containing the urgent 14053 * byte. If for any reason we abort this segment below, 14054 * if it comes back, we will have this ready, or it 14055 * will get blown off in close. 14056 */ 14057 } else if (urp == seg_len) { 14058 /* 14059 * The urgent byte is the next byte after this sequence 14060 * number. If there is data it is marked with 14061 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14062 * since it is not needed. Otherwise, if the code 14063 * above just allocated a zero-length tcp_urp_mark_mp 14064 * message, that message is tagged with MSGMARKNEXT. 14065 * Sending up these MSGMARKNEXT messages makes 14066 * SIOCATMARK work correctly even though 14067 * the T_EXDATA_IND will not be sent up until the 14068 * urgent byte arrives. 14069 */ 14070 if (seg_len != 0) { 14071 flags |= TH_MARKNEXT_NEEDED; 14072 freemsg(tcp->tcp_urp_mark_mp); 14073 tcp->tcp_urp_mark_mp = NULL; 14074 flags &= ~TH_SEND_URP_MARK; 14075 } else if (tcp->tcp_urp_mark_mp != NULL) { 14076 flags |= TH_SEND_URP_MARK; 14077 tcp->tcp_urp_mark_mp->b_flag &= 14078 ~MSGNOTMARKNEXT; 14079 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14080 } 14081 #ifdef DEBUG 14082 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14083 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14084 seg_len, flags, 14085 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14086 #endif /* DEBUG */ 14087 } 14088 #ifdef DEBUG 14089 else { 14090 /* Data left until we hit mark */ 14091 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14092 "tcp_rput: URP %d bytes left, %s", 14093 urp - seg_len, tcp_display(tcp, NULL, 14094 DISP_PORT_ONLY)); 14095 } 14096 #endif /* DEBUG */ 14097 } 14098 14099 process_ack: 14100 if (!(flags & TH_ACK)) { 14101 freemsg(mp); 14102 goto xmit_check; 14103 } 14104 } 14105 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14106 14107 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14108 tcp->tcp_ip_forward_progress = B_TRUE; 14109 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14110 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14111 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14112 /* 3-way handshake complete - pass up the T_CONN_IND */ 14113 tcp_t *listener = tcp->tcp_listener; 14114 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14115 14116 tcp->tcp_tconnind_started = B_TRUE; 14117 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14118 /* 14119 * We are here means eager is fine but it can 14120 * get a TH_RST at any point between now and till 14121 * accept completes and disappear. We need to 14122 * ensure that reference to eager is valid after 14123 * we get out of eager's perimeter. So we do 14124 * an extra refhold. 14125 */ 14126 CONN_INC_REF(connp); 14127 14128 /* 14129 * The listener also exists because of the refhold 14130 * done in tcp_conn_request. Its possible that it 14131 * might have closed. We will check that once we 14132 * get inside listeners context. 14133 */ 14134 CONN_INC_REF(listener->tcp_connp); 14135 if (listener->tcp_connp->conn_sqp == 14136 connp->conn_sqp) { 14137 /* 14138 * We optimize by not calling an SQUEUE_ENTER 14139 * on the listener since we know that the 14140 * listener and eager squeues are the same. 14141 * We are able to make this check safely only 14142 * because neither the eager nor the listener 14143 * can change its squeue. Only an active connect 14144 * can change its squeue 14145 */ 14146 tcp_send_conn_ind(listener->tcp_connp, mp, 14147 listener->tcp_connp->conn_sqp); 14148 CONN_DEC_REF(listener->tcp_connp); 14149 } else if (!tcp->tcp_loopback) { 14150 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14151 mp, tcp_send_conn_ind, 14152 listener->tcp_connp, SQ_FILL, 14153 SQTAG_TCP_CONN_IND); 14154 } else { 14155 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14156 mp, tcp_send_conn_ind, 14157 listener->tcp_connp, SQ_PROCESS, 14158 SQTAG_TCP_CONN_IND); 14159 } 14160 } 14161 14162 if (tcp->tcp_active_open) { 14163 /* 14164 * We are seeing the final ack in the three way 14165 * hand shake of a active open'ed connection 14166 * so we must send up a T_CONN_CON 14167 */ 14168 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14169 freemsg(mp); 14170 return; 14171 } 14172 /* 14173 * Don't fuse the loopback endpoints for 14174 * simultaneous active opens. 14175 */ 14176 if (tcp->tcp_loopback) { 14177 TCP_STAT(tcps, tcp_fusion_unfusable); 14178 tcp->tcp_unfusable = B_TRUE; 14179 } 14180 } 14181 14182 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14183 bytes_acked--; 14184 /* SYN was acked - making progress */ 14185 if (tcp->tcp_ipversion == IPV6_VERSION) 14186 tcp->tcp_ip_forward_progress = B_TRUE; 14187 14188 /* 14189 * If SYN was retransmitted, need to reset all 14190 * retransmission info as this segment will be 14191 * treated as a dup ACK. 14192 */ 14193 if (tcp->tcp_rexmit) { 14194 tcp->tcp_rexmit = B_FALSE; 14195 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14196 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14197 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14198 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14199 tcp->tcp_ms_we_have_waited = 0; 14200 tcp->tcp_cwnd = mss; 14201 } 14202 14203 /* 14204 * We set the send window to zero here. 14205 * This is needed if there is data to be 14206 * processed already on the queue. 14207 * Later (at swnd_update label), the 14208 * "new_swnd > tcp_swnd" condition is satisfied 14209 * the XMIT_NEEDED flag is set in the current 14210 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14211 * called if there is already data on queue in 14212 * this state. 14213 */ 14214 tcp->tcp_swnd = 0; 14215 14216 if (new_swnd > tcp->tcp_max_swnd) 14217 tcp->tcp_max_swnd = new_swnd; 14218 tcp->tcp_swl1 = seg_seq; 14219 tcp->tcp_swl2 = seg_ack; 14220 tcp->tcp_state = TCPS_ESTABLISHED; 14221 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14222 14223 /* Fuse when both sides are in ESTABLISHED state */ 14224 if (tcp->tcp_loopback && do_tcp_fusion) 14225 tcp_fuse(tcp, iphdr, tcph); 14226 14227 } 14228 /* This code follows 4.4BSD-Lite2 mostly. */ 14229 if (bytes_acked < 0) 14230 goto est; 14231 14232 /* 14233 * If TCP is ECN capable and the congestion experience bit is 14234 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14235 * done once per window (or more loosely, per RTT). 14236 */ 14237 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14238 tcp->tcp_cwr = B_FALSE; 14239 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14240 if (!tcp->tcp_cwr) { 14241 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14242 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14243 tcp->tcp_cwnd = npkt * mss; 14244 /* 14245 * If the cwnd is 0, use the timer to clock out 14246 * new segments. This is required by the ECN spec. 14247 */ 14248 if (npkt == 0) { 14249 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14250 /* 14251 * This makes sure that when the ACK comes 14252 * back, we will increase tcp_cwnd by 1 MSS. 14253 */ 14254 tcp->tcp_cwnd_cnt = 0; 14255 } 14256 tcp->tcp_cwr = B_TRUE; 14257 /* 14258 * This marks the end of the current window of in 14259 * flight data. That is why we don't use 14260 * tcp_suna + tcp_swnd. Only data in flight can 14261 * provide ECN info. 14262 */ 14263 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14264 tcp->tcp_ecn_cwr_sent = B_FALSE; 14265 } 14266 } 14267 14268 mp1 = tcp->tcp_xmit_head; 14269 if (bytes_acked == 0) { 14270 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14271 int dupack_cnt; 14272 14273 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14274 /* 14275 * Fast retransmit. When we have seen exactly three 14276 * identical ACKs while we have unacked data 14277 * outstanding we take it as a hint that our peer 14278 * dropped something. 14279 * 14280 * If TCP is retransmitting, don't do fast retransmit. 14281 */ 14282 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14283 ! tcp->tcp_rexmit) { 14284 /* Do Limited Transmit */ 14285 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14286 tcps->tcps_dupack_fast_retransmit) { 14287 /* 14288 * RFC 3042 14289 * 14290 * What we need to do is temporarily 14291 * increase tcp_cwnd so that new 14292 * data can be sent if it is allowed 14293 * by the receive window (tcp_rwnd). 14294 * tcp_wput_data() will take care of 14295 * the rest. 14296 * 14297 * If the connection is SACK capable, 14298 * only do limited xmit when there 14299 * is SACK info. 14300 * 14301 * Note how tcp_cwnd is incremented. 14302 * The first dup ACK will increase 14303 * it by 1 MSS. The second dup ACK 14304 * will increase it by 2 MSS. This 14305 * means that only 1 new segment will 14306 * be sent for each dup ACK. 14307 */ 14308 if (tcp->tcp_unsent > 0 && 14309 (!tcp->tcp_snd_sack_ok || 14310 (tcp->tcp_snd_sack_ok && 14311 tcp->tcp_notsack_list != NULL))) { 14312 tcp->tcp_cwnd += mss << 14313 (tcp->tcp_dupack_cnt - 1); 14314 flags |= TH_LIMIT_XMIT; 14315 } 14316 } else if (dupack_cnt == 14317 tcps->tcps_dupack_fast_retransmit) { 14318 14319 /* 14320 * If we have reduced tcp_ssthresh 14321 * because of ECN, do not reduce it again 14322 * unless it is already one window of data 14323 * away. After one window of data, tcp_cwr 14324 * should then be cleared. Note that 14325 * for non ECN capable connection, tcp_cwr 14326 * should always be false. 14327 * 14328 * Adjust cwnd since the duplicate 14329 * ack indicates that a packet was 14330 * dropped (due to congestion.) 14331 */ 14332 if (!tcp->tcp_cwr) { 14333 npkt = ((tcp->tcp_snxt - 14334 tcp->tcp_suna) >> 1) / mss; 14335 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14336 mss; 14337 tcp->tcp_cwnd = (npkt + 14338 tcp->tcp_dupack_cnt) * mss; 14339 } 14340 if (tcp->tcp_ecn_ok) { 14341 tcp->tcp_cwr = B_TRUE; 14342 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14343 tcp->tcp_ecn_cwr_sent = B_FALSE; 14344 } 14345 14346 /* 14347 * We do Hoe's algorithm. Refer to her 14348 * paper "Improving the Start-up Behavior 14349 * of a Congestion Control Scheme for TCP," 14350 * appeared in SIGCOMM'96. 14351 * 14352 * Save highest seq no we have sent so far. 14353 * Be careful about the invisible FIN byte. 14354 */ 14355 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14356 (tcp->tcp_unsent == 0)) { 14357 tcp->tcp_rexmit_max = tcp->tcp_fss; 14358 } else { 14359 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14360 } 14361 14362 /* 14363 * Do not allow bursty traffic during. 14364 * fast recovery. Refer to Fall and Floyd's 14365 * paper "Simulation-based Comparisons of 14366 * Tahoe, Reno and SACK TCP" (in CCR?) 14367 * This is a best current practise. 14368 */ 14369 tcp->tcp_snd_burst = TCP_CWND_SS; 14370 14371 /* 14372 * For SACK: 14373 * Calculate tcp_pipe, which is the 14374 * estimated number of bytes in 14375 * network. 14376 * 14377 * tcp_fack is the highest sack'ed seq num 14378 * TCP has received. 14379 * 14380 * tcp_pipe is explained in the above quoted 14381 * Fall and Floyd's paper. tcp_fack is 14382 * explained in Mathis and Mahdavi's 14383 * "Forward Acknowledgment: Refining TCP 14384 * Congestion Control" in SIGCOMM '96. 14385 */ 14386 if (tcp->tcp_snd_sack_ok) { 14387 ASSERT(tcp->tcp_sack_info != NULL); 14388 if (tcp->tcp_notsack_list != NULL) { 14389 tcp->tcp_pipe = tcp->tcp_snxt - 14390 tcp->tcp_fack; 14391 tcp->tcp_sack_snxt = seg_ack; 14392 flags |= TH_NEED_SACK_REXMIT; 14393 } else { 14394 /* 14395 * Always initialize tcp_pipe 14396 * even though we don't have 14397 * any SACK info. If later 14398 * we get SACK info and 14399 * tcp_pipe is not initialized, 14400 * funny things will happen. 14401 */ 14402 tcp->tcp_pipe = 14403 tcp->tcp_cwnd_ssthresh; 14404 } 14405 } else { 14406 flags |= TH_REXMIT_NEEDED; 14407 } /* tcp_snd_sack_ok */ 14408 14409 } else { 14410 /* 14411 * Here we perform congestion 14412 * avoidance, but NOT slow start. 14413 * This is known as the Fast 14414 * Recovery Algorithm. 14415 */ 14416 if (tcp->tcp_snd_sack_ok && 14417 tcp->tcp_notsack_list != NULL) { 14418 flags |= TH_NEED_SACK_REXMIT; 14419 tcp->tcp_pipe -= mss; 14420 if (tcp->tcp_pipe < 0) 14421 tcp->tcp_pipe = 0; 14422 } else { 14423 /* 14424 * We know that one more packet has 14425 * left the pipe thus we can update 14426 * cwnd. 14427 */ 14428 cwnd = tcp->tcp_cwnd + mss; 14429 if (cwnd > tcp->tcp_cwnd_max) 14430 cwnd = tcp->tcp_cwnd_max; 14431 tcp->tcp_cwnd = cwnd; 14432 if (tcp->tcp_unsent > 0) 14433 flags |= TH_XMIT_NEEDED; 14434 } 14435 } 14436 } 14437 } else if (tcp->tcp_zero_win_probe) { 14438 /* 14439 * If the window has opened, need to arrange 14440 * to send additional data. 14441 */ 14442 if (new_swnd != 0) { 14443 /* tcp_suna != tcp_snxt */ 14444 /* Packet contains a window update */ 14445 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14446 tcp->tcp_zero_win_probe = 0; 14447 tcp->tcp_timer_backoff = 0; 14448 tcp->tcp_ms_we_have_waited = 0; 14449 14450 /* 14451 * Transmit starting with tcp_suna since 14452 * the one byte probe is not ack'ed. 14453 * If TCP has sent more than one identical 14454 * probe, tcp_rexmit will be set. That means 14455 * tcp_ss_rexmit() will send out the one 14456 * byte along with new data. Otherwise, 14457 * fake the retransmission. 14458 */ 14459 flags |= TH_XMIT_NEEDED; 14460 if (!tcp->tcp_rexmit) { 14461 tcp->tcp_rexmit = B_TRUE; 14462 tcp->tcp_dupack_cnt = 0; 14463 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14464 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14465 } 14466 } 14467 } 14468 goto swnd_update; 14469 } 14470 14471 /* 14472 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14473 * If the ACK value acks something that we have not yet sent, it might 14474 * be an old duplicate segment. Send an ACK to re-synchronize the 14475 * other side. 14476 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14477 * state is handled above, so we can always just drop the segment and 14478 * send an ACK here. 14479 * 14480 * Should we send ACKs in response to ACK only segments? 14481 */ 14482 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14483 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14484 /* drop the received segment */ 14485 freemsg(mp); 14486 14487 /* 14488 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14489 * greater than 0, check if the number of such 14490 * bogus ACks is greater than that count. If yes, 14491 * don't send back any ACK. This prevents TCP from 14492 * getting into an ACK storm if somehow an attacker 14493 * successfully spoofs an acceptable segment to our 14494 * peer. 14495 */ 14496 if (tcp_drop_ack_unsent_cnt > 0 && 14497 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14498 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14499 return; 14500 } 14501 mp = tcp_ack_mp(tcp); 14502 if (mp != NULL) { 14503 BUMP_LOCAL(tcp->tcp_obsegs); 14504 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14505 tcp_send_data(tcp, tcp->tcp_wq, mp); 14506 } 14507 return; 14508 } 14509 14510 /* 14511 * TCP gets a new ACK, update the notsack'ed list to delete those 14512 * blocks that are covered by this ACK. 14513 */ 14514 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14515 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14516 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14517 } 14518 14519 /* 14520 * If we got an ACK after fast retransmit, check to see 14521 * if it is a partial ACK. If it is not and the congestion 14522 * window was inflated to account for the other side's 14523 * cached packets, retract it. If it is, do Hoe's algorithm. 14524 */ 14525 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14526 ASSERT(tcp->tcp_rexmit == B_FALSE); 14527 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14528 tcp->tcp_dupack_cnt = 0; 14529 /* 14530 * Restore the orig tcp_cwnd_ssthresh after 14531 * fast retransmit phase. 14532 */ 14533 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14534 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14535 } 14536 tcp->tcp_rexmit_max = seg_ack; 14537 tcp->tcp_cwnd_cnt = 0; 14538 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14539 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14540 14541 /* 14542 * Remove all notsack info to avoid confusion with 14543 * the next fast retrasnmit/recovery phase. 14544 */ 14545 if (tcp->tcp_snd_sack_ok && 14546 tcp->tcp_notsack_list != NULL) { 14547 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14548 } 14549 } else { 14550 if (tcp->tcp_snd_sack_ok && 14551 tcp->tcp_notsack_list != NULL) { 14552 flags |= TH_NEED_SACK_REXMIT; 14553 tcp->tcp_pipe -= mss; 14554 if (tcp->tcp_pipe < 0) 14555 tcp->tcp_pipe = 0; 14556 } else { 14557 /* 14558 * Hoe's algorithm: 14559 * 14560 * Retransmit the unack'ed segment and 14561 * restart fast recovery. Note that we 14562 * need to scale back tcp_cwnd to the 14563 * original value when we started fast 14564 * recovery. This is to prevent overly 14565 * aggressive behaviour in sending new 14566 * segments. 14567 */ 14568 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14569 tcps->tcps_dupack_fast_retransmit * mss; 14570 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14571 flags |= TH_REXMIT_NEEDED; 14572 } 14573 } 14574 } else { 14575 tcp->tcp_dupack_cnt = 0; 14576 if (tcp->tcp_rexmit) { 14577 /* 14578 * TCP is retranmitting. If the ACK ack's all 14579 * outstanding data, update tcp_rexmit_max and 14580 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14581 * to the correct value. 14582 * 14583 * Note that SEQ_LEQ() is used. This is to avoid 14584 * unnecessary fast retransmit caused by dup ACKs 14585 * received when TCP does slow start retransmission 14586 * after a time out. During this phase, TCP may 14587 * send out segments which are already received. 14588 * This causes dup ACKs to be sent back. 14589 */ 14590 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14591 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14592 tcp->tcp_rexmit_nxt = seg_ack; 14593 } 14594 if (seg_ack != tcp->tcp_rexmit_max) { 14595 flags |= TH_XMIT_NEEDED; 14596 } 14597 } else { 14598 tcp->tcp_rexmit = B_FALSE; 14599 tcp->tcp_xmit_zc_clean = B_FALSE; 14600 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14601 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14602 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14603 } 14604 tcp->tcp_ms_we_have_waited = 0; 14605 } 14606 } 14607 14608 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14609 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14610 tcp->tcp_suna = seg_ack; 14611 if (tcp->tcp_zero_win_probe != 0) { 14612 tcp->tcp_zero_win_probe = 0; 14613 tcp->tcp_timer_backoff = 0; 14614 } 14615 14616 /* 14617 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14618 * Note that it cannot be the SYN being ack'ed. The code flow 14619 * will not reach here. 14620 */ 14621 if (mp1 == NULL) { 14622 goto fin_acked; 14623 } 14624 14625 /* 14626 * Update the congestion window. 14627 * 14628 * If TCP is not ECN capable or TCP is ECN capable but the 14629 * congestion experience bit is not set, increase the tcp_cwnd as 14630 * usual. 14631 */ 14632 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14633 cwnd = tcp->tcp_cwnd; 14634 add = mss; 14635 14636 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14637 /* 14638 * This is to prevent an increase of less than 1 MSS of 14639 * tcp_cwnd. With partial increase, tcp_wput_data() 14640 * may send out tinygrams in order to preserve mblk 14641 * boundaries. 14642 * 14643 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14644 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14645 * increased by 1 MSS for every RTTs. 14646 */ 14647 if (tcp->tcp_cwnd_cnt <= 0) { 14648 tcp->tcp_cwnd_cnt = cwnd + add; 14649 } else { 14650 tcp->tcp_cwnd_cnt -= add; 14651 add = 0; 14652 } 14653 } 14654 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14655 } 14656 14657 /* See if the latest urgent data has been acknowledged */ 14658 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14659 SEQ_GT(seg_ack, tcp->tcp_urg)) 14660 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14661 14662 /* Can we update the RTT estimates? */ 14663 if (tcp->tcp_snd_ts_ok) { 14664 /* Ignore zero timestamp echo-reply. */ 14665 if (tcpopt.tcp_opt_ts_ecr != 0) { 14666 tcp_set_rto(tcp, (int32_t)lbolt - 14667 (int32_t)tcpopt.tcp_opt_ts_ecr); 14668 } 14669 14670 /* If needed, restart the timer. */ 14671 if (tcp->tcp_set_timer == 1) { 14672 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14673 tcp->tcp_set_timer = 0; 14674 } 14675 /* 14676 * Update tcp_csuna in case the other side stops sending 14677 * us timestamps. 14678 */ 14679 tcp->tcp_csuna = tcp->tcp_snxt; 14680 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14681 /* 14682 * An ACK sequence we haven't seen before, so get the RTT 14683 * and update the RTO. But first check if the timestamp is 14684 * valid to use. 14685 */ 14686 if ((mp1->b_next != NULL) && 14687 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14688 tcp_set_rto(tcp, (int32_t)lbolt - 14689 (int32_t)(intptr_t)mp1->b_prev); 14690 else 14691 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14692 14693 /* Remeber the last sequence to be ACKed */ 14694 tcp->tcp_csuna = seg_ack; 14695 if (tcp->tcp_set_timer == 1) { 14696 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14697 tcp->tcp_set_timer = 0; 14698 } 14699 } else { 14700 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14701 } 14702 14703 /* Eat acknowledged bytes off the xmit queue. */ 14704 for (;;) { 14705 mblk_t *mp2; 14706 uchar_t *wptr; 14707 14708 wptr = mp1->b_wptr; 14709 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14710 bytes_acked -= (int)(wptr - mp1->b_rptr); 14711 if (bytes_acked < 0) { 14712 mp1->b_rptr = wptr + bytes_acked; 14713 /* 14714 * Set a new timestamp if all the bytes timed by the 14715 * old timestamp have been ack'ed. 14716 */ 14717 if (SEQ_GT(seg_ack, 14718 (uint32_t)(uintptr_t)(mp1->b_next))) { 14719 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14720 mp1->b_next = NULL; 14721 } 14722 break; 14723 } 14724 mp1->b_next = NULL; 14725 mp1->b_prev = NULL; 14726 mp2 = mp1; 14727 mp1 = mp1->b_cont; 14728 14729 /* 14730 * This notification is required for some zero-copy 14731 * clients to maintain a copy semantic. After the data 14732 * is ack'ed, client is safe to modify or reuse the buffer. 14733 */ 14734 if (tcp->tcp_snd_zcopy_aware && 14735 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14736 tcp_zcopy_notify(tcp); 14737 freeb(mp2); 14738 if (bytes_acked == 0) { 14739 if (mp1 == NULL) { 14740 /* Everything is ack'ed, clear the tail. */ 14741 tcp->tcp_xmit_tail = NULL; 14742 /* 14743 * Cancel the timer unless we are still 14744 * waiting for an ACK for the FIN packet. 14745 */ 14746 if (tcp->tcp_timer_tid != 0 && 14747 tcp->tcp_snxt == tcp->tcp_suna) { 14748 (void) TCP_TIMER_CANCEL(tcp, 14749 tcp->tcp_timer_tid); 14750 tcp->tcp_timer_tid = 0; 14751 } 14752 goto pre_swnd_update; 14753 } 14754 if (mp2 != tcp->tcp_xmit_tail) 14755 break; 14756 tcp->tcp_xmit_tail = mp1; 14757 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14758 (uintptr_t)INT_MAX); 14759 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14760 mp1->b_rptr); 14761 break; 14762 } 14763 if (mp1 == NULL) { 14764 /* 14765 * More was acked but there is nothing more 14766 * outstanding. This means that the FIN was 14767 * just acked or that we're talking to a clown. 14768 */ 14769 fin_acked: 14770 ASSERT(tcp->tcp_fin_sent); 14771 tcp->tcp_xmit_tail = NULL; 14772 if (tcp->tcp_fin_sent) { 14773 /* FIN was acked - making progress */ 14774 if (tcp->tcp_ipversion == IPV6_VERSION && 14775 !tcp->tcp_fin_acked) 14776 tcp->tcp_ip_forward_progress = B_TRUE; 14777 tcp->tcp_fin_acked = B_TRUE; 14778 if (tcp->tcp_linger_tid != 0 && 14779 TCP_TIMER_CANCEL(tcp, 14780 tcp->tcp_linger_tid) >= 0) { 14781 tcp_stop_lingering(tcp); 14782 freemsg(mp); 14783 mp = NULL; 14784 } 14785 } else { 14786 /* 14787 * We should never get here because 14788 * we have already checked that the 14789 * number of bytes ack'ed should be 14790 * smaller than or equal to what we 14791 * have sent so far (it is the 14792 * acceptability check of the ACK). 14793 * We can only get here if the send 14794 * queue is corrupted. 14795 * 14796 * Terminate the connection and 14797 * panic the system. It is better 14798 * for us to panic instead of 14799 * continuing to avoid other disaster. 14800 */ 14801 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14802 tcp->tcp_rnxt, TH_RST|TH_ACK); 14803 panic("Memory corruption " 14804 "detected for connection %s.", 14805 tcp_display(tcp, NULL, 14806 DISP_ADDR_AND_PORT)); 14807 /*NOTREACHED*/ 14808 } 14809 goto pre_swnd_update; 14810 } 14811 ASSERT(mp2 != tcp->tcp_xmit_tail); 14812 } 14813 if (tcp->tcp_unsent) { 14814 flags |= TH_XMIT_NEEDED; 14815 } 14816 pre_swnd_update: 14817 tcp->tcp_xmit_head = mp1; 14818 swnd_update: 14819 /* 14820 * The following check is different from most other implementations. 14821 * For bi-directional transfer, when segments are dropped, the 14822 * "normal" check will not accept a window update in those 14823 * retransmitted segemnts. Failing to do that, TCP may send out 14824 * segments which are outside receiver's window. As TCP accepts 14825 * the ack in those retransmitted segments, if the window update in 14826 * the same segment is not accepted, TCP will incorrectly calculates 14827 * that it can send more segments. This can create a deadlock 14828 * with the receiver if its window becomes zero. 14829 */ 14830 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14831 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14832 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14833 /* 14834 * The criteria for update is: 14835 * 14836 * 1. the segment acknowledges some data. Or 14837 * 2. the segment is new, i.e. it has a higher seq num. Or 14838 * 3. the segment is not old and the advertised window is 14839 * larger than the previous advertised window. 14840 */ 14841 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14842 flags |= TH_XMIT_NEEDED; 14843 tcp->tcp_swnd = new_swnd; 14844 if (new_swnd > tcp->tcp_max_swnd) 14845 tcp->tcp_max_swnd = new_swnd; 14846 tcp->tcp_swl1 = seg_seq; 14847 tcp->tcp_swl2 = seg_ack; 14848 } 14849 est: 14850 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14851 14852 switch (tcp->tcp_state) { 14853 case TCPS_FIN_WAIT_1: 14854 if (tcp->tcp_fin_acked) { 14855 tcp->tcp_state = TCPS_FIN_WAIT_2; 14856 /* 14857 * We implement the non-standard BSD/SunOS 14858 * FIN_WAIT_2 flushing algorithm. 14859 * If there is no user attached to this 14860 * TCP endpoint, then this TCP struct 14861 * could hang around forever in FIN_WAIT_2 14862 * state if the peer forgets to send us 14863 * a FIN. To prevent this, we wait only 14864 * 2*MSL (a convenient time value) for 14865 * the FIN to arrive. If it doesn't show up, 14866 * we flush the TCP endpoint. This algorithm, 14867 * though a violation of RFC-793, has worked 14868 * for over 10 years in BSD systems. 14869 * Note: SunOS 4.x waits 675 seconds before 14870 * flushing the FIN_WAIT_2 connection. 14871 */ 14872 TCP_TIMER_RESTART(tcp, 14873 tcps->tcps_fin_wait_2_flush_interval); 14874 } 14875 break; 14876 case TCPS_FIN_WAIT_2: 14877 break; /* Shutdown hook? */ 14878 case TCPS_LAST_ACK: 14879 freemsg(mp); 14880 if (tcp->tcp_fin_acked) { 14881 (void) tcp_clean_death(tcp, 0, 19); 14882 return; 14883 } 14884 goto xmit_check; 14885 case TCPS_CLOSING: 14886 if (tcp->tcp_fin_acked) { 14887 tcp->tcp_state = TCPS_TIME_WAIT; 14888 /* 14889 * Unconditionally clear the exclusive binding 14890 * bit so this TIME-WAIT connection won't 14891 * interfere with new ones. 14892 */ 14893 tcp->tcp_exclbind = 0; 14894 if (!TCP_IS_DETACHED(tcp)) { 14895 TCP_TIMER_RESTART(tcp, 14896 tcps->tcps_time_wait_interval); 14897 } else { 14898 tcp_time_wait_append(tcp); 14899 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14900 } 14901 } 14902 /*FALLTHRU*/ 14903 case TCPS_CLOSE_WAIT: 14904 freemsg(mp); 14905 goto xmit_check; 14906 default: 14907 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14908 break; 14909 } 14910 } 14911 if (flags & TH_FIN) { 14912 /* Make sure we ack the fin */ 14913 flags |= TH_ACK_NEEDED; 14914 if (!tcp->tcp_fin_rcvd) { 14915 tcp->tcp_fin_rcvd = B_TRUE; 14916 tcp->tcp_rnxt++; 14917 tcph = tcp->tcp_tcph; 14918 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14919 14920 /* 14921 * Generate the ordrel_ind at the end unless we 14922 * are an eager guy. 14923 * In the eager case tcp_rsrv will do this when run 14924 * after tcp_accept is done. 14925 */ 14926 if (tcp->tcp_listener == NULL && 14927 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14928 flags |= TH_ORDREL_NEEDED; 14929 switch (tcp->tcp_state) { 14930 case TCPS_SYN_RCVD: 14931 case TCPS_ESTABLISHED: 14932 tcp->tcp_state = TCPS_CLOSE_WAIT; 14933 /* Keepalive? */ 14934 break; 14935 case TCPS_FIN_WAIT_1: 14936 if (!tcp->tcp_fin_acked) { 14937 tcp->tcp_state = TCPS_CLOSING; 14938 break; 14939 } 14940 /* FALLTHRU */ 14941 case TCPS_FIN_WAIT_2: 14942 tcp->tcp_state = TCPS_TIME_WAIT; 14943 /* 14944 * Unconditionally clear the exclusive binding 14945 * bit so this TIME-WAIT connection won't 14946 * interfere with new ones. 14947 */ 14948 tcp->tcp_exclbind = 0; 14949 if (!TCP_IS_DETACHED(tcp)) { 14950 TCP_TIMER_RESTART(tcp, 14951 tcps->tcps_time_wait_interval); 14952 } else { 14953 tcp_time_wait_append(tcp); 14954 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14955 } 14956 if (seg_len) { 14957 /* 14958 * implies data piggybacked on FIN. 14959 * break to handle data. 14960 */ 14961 break; 14962 } 14963 freemsg(mp); 14964 goto ack_check; 14965 } 14966 } 14967 } 14968 if (mp == NULL) 14969 goto xmit_check; 14970 if (seg_len == 0) { 14971 freemsg(mp); 14972 goto xmit_check; 14973 } 14974 if (mp->b_rptr == mp->b_wptr) { 14975 /* 14976 * The header has been consumed, so we remove the 14977 * zero-length mblk here. 14978 */ 14979 mp1 = mp; 14980 mp = mp->b_cont; 14981 freeb(mp1); 14982 } 14983 update_ack: 14984 tcph = tcp->tcp_tcph; 14985 tcp->tcp_rack_cnt++; 14986 { 14987 uint32_t cur_max; 14988 14989 cur_max = tcp->tcp_rack_cur_max; 14990 if (tcp->tcp_rack_cnt >= cur_max) { 14991 /* 14992 * We have more unacked data than we should - send 14993 * an ACK now. 14994 */ 14995 flags |= TH_ACK_NEEDED; 14996 cur_max++; 14997 if (cur_max > tcp->tcp_rack_abs_max) 14998 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14999 else 15000 tcp->tcp_rack_cur_max = cur_max; 15001 } else if (TCP_IS_DETACHED(tcp)) { 15002 /* We don't have an ACK timer for detached TCP. */ 15003 flags |= TH_ACK_NEEDED; 15004 } else if (seg_len < mss) { 15005 /* 15006 * If we get a segment that is less than an mss, and we 15007 * already have unacknowledged data, and the amount 15008 * unacknowledged is not a multiple of mss, then we 15009 * better generate an ACK now. Otherwise, this may be 15010 * the tail piece of a transaction, and we would rather 15011 * wait for the response. 15012 */ 15013 uint32_t udif; 15014 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15015 (uintptr_t)INT_MAX); 15016 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15017 if (udif && (udif % mss)) 15018 flags |= TH_ACK_NEEDED; 15019 else 15020 flags |= TH_ACK_TIMER_NEEDED; 15021 } else { 15022 /* Start delayed ack timer */ 15023 flags |= TH_ACK_TIMER_NEEDED; 15024 } 15025 } 15026 tcp->tcp_rnxt += seg_len; 15027 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15028 15029 if (mp == NULL) 15030 goto xmit_check; 15031 15032 /* Update SACK list */ 15033 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15034 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15035 &(tcp->tcp_num_sack_blk)); 15036 } 15037 15038 if (tcp->tcp_urp_mp) { 15039 tcp->tcp_urp_mp->b_cont = mp; 15040 mp = tcp->tcp_urp_mp; 15041 tcp->tcp_urp_mp = NULL; 15042 /* Ready for a new signal. */ 15043 tcp->tcp_urp_last_valid = B_FALSE; 15044 #ifdef DEBUG 15045 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15046 "tcp_rput: sending exdata_ind %s", 15047 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15048 #endif /* DEBUG */ 15049 } 15050 15051 /* 15052 * Check for ancillary data changes compared to last segment. 15053 */ 15054 if (tcp->tcp_ipv6_recvancillary != 0) { 15055 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15056 ASSERT(mp != NULL); 15057 } 15058 15059 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15060 /* 15061 * Side queue inbound data until the accept happens. 15062 * tcp_accept/tcp_rput drains this when the accept happens. 15063 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15064 * T_EXDATA_IND) it is queued on b_next. 15065 * XXX Make urgent data use this. Requires: 15066 * Removing tcp_listener check for TH_URG 15067 * Making M_PCPROTO and MARK messages skip the eager case 15068 */ 15069 15070 if (tcp->tcp_kssl_pending) { 15071 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15072 mblk_t *, mp); 15073 tcp_kssl_input(tcp, mp); 15074 } else { 15075 tcp_rcv_enqueue(tcp, mp, seg_len); 15076 } 15077 } else { 15078 sodirect_t *sodp = tcp->tcp_sodirect; 15079 15080 /* 15081 * If an sodirect connection and an enabled sodirect_t then 15082 * sodp will be set to point to the tcp_t/sonode_t shared 15083 * sodirect_t and the sodirect_t's lock will be held. 15084 */ 15085 if (sodp != NULL) { 15086 mutex_enter(sodp->sod_lockp); 15087 if (!(sodp->sod_state & SOD_ENABLED) || 15088 (tcp->tcp_kssl_ctx != NULL && 15089 DB_TYPE(mp) == M_DATA)) { 15090 mutex_exit(sodp->sod_lockp); 15091 sodp = NULL; 15092 } else { 15093 mutex_exit(sodp->sod_lockp); 15094 } 15095 } 15096 if (mp->b_datap->db_type != M_DATA || 15097 (flags & TH_MARKNEXT_NEEDED)) { 15098 if (IPCL_IS_NONSTR(connp)) { 15099 int error; 15100 15101 if ((*connp->conn_upcalls->su_recv) 15102 (connp->conn_upper_handle, mp, 15103 seg_len, 0, &error, NULL) <= 0) { 15104 /* 15105 * We should never be in middle of a 15106 * fallback, the squeue guarantees that. 15107 */ 15108 ASSERT(error != EOPNOTSUPP); 15109 if (error == ENOSPC) 15110 tcp->tcp_rwnd -= seg_len; 15111 } 15112 } else if (sodp != NULL) { 15113 mutex_enter(sodp->sod_lockp); 15114 SOD_UIOAFINI(sodp); 15115 if (!SOD_QEMPTY(sodp) && 15116 (sodp->sod_state & SOD_WAKE_NOT)) { 15117 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15118 /* sod_wakeup() did the mutex_exit() */ 15119 } else { 15120 mutex_exit(sodp->sod_lockp); 15121 } 15122 } else if (tcp->tcp_rcv_list != NULL) { 15123 flags |= tcp_rcv_drain(tcp); 15124 } 15125 ASSERT(tcp->tcp_rcv_list == NULL || 15126 tcp->tcp_fused_sigurg); 15127 15128 if (flags & TH_MARKNEXT_NEEDED) { 15129 #ifdef DEBUG 15130 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15131 "tcp_rput: sending MSGMARKNEXT %s", 15132 tcp_display(tcp, NULL, 15133 DISP_PORT_ONLY)); 15134 #endif /* DEBUG */ 15135 mp->b_flag |= MSGMARKNEXT; 15136 flags &= ~TH_MARKNEXT_NEEDED; 15137 } 15138 15139 /* Does this need SSL processing first? */ 15140 if ((tcp->tcp_kssl_ctx != NULL) && 15141 (DB_TYPE(mp) == M_DATA)) { 15142 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15143 mblk_t *, mp); 15144 tcp_kssl_input(tcp, mp); 15145 } else if (!IPCL_IS_NONSTR(connp)) { 15146 /* Already handled non-STREAMS case. */ 15147 putnext(tcp->tcp_rq, mp); 15148 if (!canputnext(tcp->tcp_rq)) 15149 tcp->tcp_rwnd -= seg_len; 15150 } 15151 } else if ((tcp->tcp_kssl_ctx != NULL) && 15152 (DB_TYPE(mp) == M_DATA)) { 15153 /* Does this need SSL processing first? */ 15154 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15155 tcp_kssl_input(tcp, mp); 15156 } else if (IPCL_IS_NONSTR(connp)) { 15157 /* Non-STREAMS socket */ 15158 boolean_t push = flags & (TH_PUSH|TH_FIN); 15159 int error; 15160 15161 if ((*connp->conn_upcalls->su_recv)( 15162 connp->conn_upper_handle, 15163 mp, seg_len, 0, &error, &push) <= 0) { 15164 /* 15165 * We should never be in middle of a 15166 * fallback, the squeue guarantees that. 15167 */ 15168 ASSERT(error != EOPNOTSUPP); 15169 if (error == ENOSPC) 15170 tcp->tcp_rwnd -= seg_len; 15171 } else if (push) { 15172 /* 15173 * PUSH bit set and sockfs is not 15174 * flow controlled 15175 */ 15176 flags |= tcp_rwnd_reopen(tcp); 15177 } 15178 } else if (sodp != NULL) { 15179 /* 15180 * Sodirect so all mblk_t's are queued on the 15181 * socket directly, check for wakeup of blocked 15182 * reader (if any), and last if flow-controled. 15183 */ 15184 mutex_enter(sodp->sod_lockp); 15185 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15186 if ((sodp->sod_state & SOD_WAKE_NEED) || 15187 (flags & (TH_PUSH|TH_FIN))) { 15188 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15189 /* sod_wakeup() did the mutex_exit() */ 15190 } else { 15191 if (SOD_QFULL(sodp)) { 15192 /* Q is full, need backenable */ 15193 SOD_QSETBE(sodp); 15194 } 15195 mutex_exit(sodp->sod_lockp); 15196 } 15197 } else if ((flags & (TH_PUSH|TH_FIN)) || 15198 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15199 if (tcp->tcp_rcv_list != NULL) { 15200 /* 15201 * Enqueue the new segment first and then 15202 * call tcp_rcv_drain() to send all data 15203 * up. The other way to do this is to 15204 * send all queued data up and then call 15205 * putnext() to send the new segment up. 15206 * This way can remove the else part later 15207 * on. 15208 * 15209 * We don't do this to avoid one more call to 15210 * canputnext() as tcp_rcv_drain() needs to 15211 * call canputnext(). 15212 */ 15213 tcp_rcv_enqueue(tcp, mp, seg_len); 15214 flags |= tcp_rcv_drain(tcp); 15215 } else { 15216 putnext(tcp->tcp_rq, mp); 15217 if (!canputnext(tcp->tcp_rq)) 15218 tcp->tcp_rwnd -= seg_len; 15219 } 15220 } else { 15221 /* 15222 * Enqueue all packets when processing an mblk 15223 * from the co queue and also enqueue normal packets. 15224 * For packets which belong to SSL stream do SSL 15225 * processing first. 15226 */ 15227 tcp_rcv_enqueue(tcp, mp, seg_len); 15228 } 15229 /* 15230 * Make sure the timer is running if we have data waiting 15231 * for a push bit. This provides resiliency against 15232 * implementations that do not correctly generate push bits. 15233 * 15234 * Note, for sodirect if Q isn't empty and there's not a 15235 * pending wakeup then we need a timer. Also note that sodp 15236 * is assumed to be still valid after exit()ing the sod_lockp 15237 * above and while the SOD state can change it can only change 15238 * such that the Q is empty now even though data was added 15239 * above. 15240 */ 15241 if (!IPCL_IS_NONSTR(connp) && 15242 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15243 (sodp->sod_state & SOD_WAKE_NOT)) || 15244 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15245 tcp->tcp_push_tid == 0) { 15246 /* 15247 * The connection may be closed at this point, so don't 15248 * do anything for a detached tcp. 15249 */ 15250 if (!TCP_IS_DETACHED(tcp)) 15251 tcp->tcp_push_tid = TCP_TIMER(tcp, 15252 tcp_push_timer, 15253 MSEC_TO_TICK( 15254 tcps->tcps_push_timer_interval)); 15255 } 15256 } 15257 15258 xmit_check: 15259 /* Is there anything left to do? */ 15260 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15261 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15262 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15263 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15264 goto done; 15265 15266 /* Any transmit work to do and a non-zero window? */ 15267 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15268 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15269 if (flags & TH_REXMIT_NEEDED) { 15270 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15271 15272 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15273 if (snd_size > mss) 15274 snd_size = mss; 15275 if (snd_size > tcp->tcp_swnd) 15276 snd_size = tcp->tcp_swnd; 15277 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15278 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15279 B_TRUE); 15280 15281 if (mp1 != NULL) { 15282 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15283 tcp->tcp_csuna = tcp->tcp_snxt; 15284 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15285 UPDATE_MIB(&tcps->tcps_mib, 15286 tcpRetransBytes, snd_size); 15287 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15288 } 15289 } 15290 if (flags & TH_NEED_SACK_REXMIT) { 15291 tcp_sack_rxmit(tcp, &flags); 15292 } 15293 /* 15294 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15295 * out new segment. Note that tcp_rexmit should not be 15296 * set, otherwise TH_LIMIT_XMIT should not be set. 15297 */ 15298 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15299 if (!tcp->tcp_rexmit) { 15300 tcp_wput_data(tcp, NULL, B_FALSE); 15301 } else { 15302 tcp_ss_rexmit(tcp); 15303 } 15304 } 15305 /* 15306 * Adjust tcp_cwnd back to normal value after sending 15307 * new data segments. 15308 */ 15309 if (flags & TH_LIMIT_XMIT) { 15310 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15311 /* 15312 * This will restart the timer. Restarting the 15313 * timer is used to avoid a timeout before the 15314 * limited transmitted segment's ACK gets back. 15315 */ 15316 if (tcp->tcp_xmit_head != NULL) 15317 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15318 } 15319 15320 /* Anything more to do? */ 15321 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15322 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15323 goto done; 15324 } 15325 ack_check: 15326 if (flags & TH_SEND_URP_MARK) { 15327 ASSERT(tcp->tcp_urp_mark_mp); 15328 ASSERT(!IPCL_IS_NONSTR(connp)); 15329 /* 15330 * Send up any queued data and then send the mark message 15331 */ 15332 sodirect_t *sodp; 15333 15334 SOD_PTR_ENTER(tcp, sodp); 15335 15336 mp1 = tcp->tcp_urp_mark_mp; 15337 tcp->tcp_urp_mark_mp = NULL; 15338 if (sodp != NULL) { 15339 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15340 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15341 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15342 } 15343 ASSERT(tcp->tcp_rcv_list == NULL); 15344 15345 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15346 /* sod_wakeup() does the mutex_exit() */ 15347 } else if (tcp->tcp_rcv_list != NULL) { 15348 flags |= tcp_rcv_drain(tcp); 15349 15350 ASSERT(tcp->tcp_rcv_list == NULL || 15351 tcp->tcp_fused_sigurg); 15352 15353 } 15354 putnext(tcp->tcp_rq, mp1); 15355 #ifdef DEBUG 15356 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15357 "tcp_rput: sending zero-length %s %s", 15358 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15359 "MSGNOTMARKNEXT"), 15360 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15361 #endif /* DEBUG */ 15362 flags &= ~TH_SEND_URP_MARK; 15363 } 15364 if (flags & TH_ACK_NEEDED) { 15365 /* 15366 * Time to send an ack for some reason. 15367 */ 15368 mp1 = tcp_ack_mp(tcp); 15369 15370 if (mp1 != NULL) { 15371 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15372 BUMP_LOCAL(tcp->tcp_obsegs); 15373 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15374 } 15375 if (tcp->tcp_ack_tid != 0) { 15376 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15377 tcp->tcp_ack_tid = 0; 15378 } 15379 } 15380 if (flags & TH_ACK_TIMER_NEEDED) { 15381 /* 15382 * Arrange for deferred ACK or push wait timeout. 15383 * Start timer if it is not already running. 15384 */ 15385 if (tcp->tcp_ack_tid == 0) { 15386 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15387 MSEC_TO_TICK(tcp->tcp_localnet ? 15388 (clock_t)tcps->tcps_local_dack_interval : 15389 (clock_t)tcps->tcps_deferred_ack_interval)); 15390 } 15391 } 15392 if (flags & TH_ORDREL_NEEDED) { 15393 /* 15394 * Send up the ordrel_ind unless we are an eager guy. 15395 * In the eager case tcp_rsrv will do this when run 15396 * after tcp_accept is done. 15397 */ 15398 sodirect_t *sodp; 15399 15400 ASSERT(tcp->tcp_listener == NULL); 15401 15402 if (IPCL_IS_NONSTR(connp)) { 15403 ASSERT(tcp->tcp_ordrel_mp == NULL); 15404 tcp->tcp_ordrel_done = B_TRUE; 15405 (*connp->conn_upcalls->su_opctl) 15406 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15407 goto done; 15408 } 15409 15410 SOD_PTR_ENTER(tcp, sodp); 15411 if (sodp != NULL) { 15412 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15413 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15414 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15415 } 15416 /* No more sodirect */ 15417 tcp->tcp_sodirect = NULL; 15418 if (!SOD_QEMPTY(sodp)) { 15419 /* Mblk(s) to process, notify */ 15420 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15421 /* sod_wakeup() does the mutex_exit() */ 15422 } else { 15423 /* Nothing to process */ 15424 mutex_exit(sodp->sod_lockp); 15425 } 15426 } else if (tcp->tcp_rcv_list != NULL) { 15427 /* 15428 * Push any mblk(s) enqueued from co processing. 15429 */ 15430 flags |= tcp_rcv_drain(tcp); 15431 15432 ASSERT(tcp->tcp_rcv_list == NULL || 15433 tcp->tcp_fused_sigurg); 15434 } 15435 15436 mp1 = tcp->tcp_ordrel_mp; 15437 tcp->tcp_ordrel_mp = NULL; 15438 tcp->tcp_ordrel_done = B_TRUE; 15439 putnext(tcp->tcp_rq, mp1); 15440 } 15441 done: 15442 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15443 } 15444 15445 /* 15446 * This function does PAWS protection check. Returns B_TRUE if the 15447 * segment passes the PAWS test, else returns B_FALSE. 15448 */ 15449 boolean_t 15450 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15451 { 15452 uint8_t flags; 15453 int options; 15454 uint8_t *up; 15455 15456 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15457 /* 15458 * If timestamp option is aligned nicely, get values inline, 15459 * otherwise call general routine to parse. Only do that 15460 * if timestamp is the only option. 15461 */ 15462 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15463 TCPOPT_REAL_TS_LEN && 15464 OK_32PTR((up = ((uint8_t *)tcph) + 15465 TCP_MIN_HEADER_LENGTH)) && 15466 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15467 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15468 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15469 15470 options = TCP_OPT_TSTAMP_PRESENT; 15471 } else { 15472 if (tcp->tcp_snd_sack_ok) { 15473 tcpoptp->tcp = tcp; 15474 } else { 15475 tcpoptp->tcp = NULL; 15476 } 15477 options = tcp_parse_options(tcph, tcpoptp); 15478 } 15479 15480 if (options & TCP_OPT_TSTAMP_PRESENT) { 15481 /* 15482 * Do PAWS per RFC 1323 section 4.2. Accept RST 15483 * regardless of the timestamp, page 18 RFC 1323.bis. 15484 */ 15485 if ((flags & TH_RST) == 0 && 15486 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15487 tcp->tcp_ts_recent)) { 15488 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15489 PAWS_TIMEOUT)) { 15490 /* This segment is not acceptable. */ 15491 return (B_FALSE); 15492 } else { 15493 /* 15494 * Connection has been idle for 15495 * too long. Reset the timestamp 15496 * and assume the segment is valid. 15497 */ 15498 tcp->tcp_ts_recent = 15499 tcpoptp->tcp_opt_ts_val; 15500 } 15501 } 15502 } else { 15503 /* 15504 * If we don't get a timestamp on every packet, we 15505 * figure we can't really trust 'em, so we stop sending 15506 * and parsing them. 15507 */ 15508 tcp->tcp_snd_ts_ok = B_FALSE; 15509 15510 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15511 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15512 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15513 /* 15514 * Adjust the tcp_mss accordingly. We also need to 15515 * adjust tcp_cwnd here in accordance with the new mss. 15516 * But we avoid doing a slow start here so as to not 15517 * to lose on the transfer rate built up so far. 15518 */ 15519 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15520 if (tcp->tcp_snd_sack_ok) { 15521 ASSERT(tcp->tcp_sack_info != NULL); 15522 tcp->tcp_max_sack_blk = 4; 15523 } 15524 } 15525 return (B_TRUE); 15526 } 15527 15528 /* 15529 * Attach ancillary data to a received TCP segments for the 15530 * ancillary pieces requested by the application that are 15531 * different than they were in the previous data segment. 15532 * 15533 * Save the "current" values once memory allocation is ok so that 15534 * when memory allocation fails we can just wait for the next data segment. 15535 */ 15536 static mblk_t * 15537 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15538 { 15539 struct T_optdata_ind *todi; 15540 int optlen; 15541 uchar_t *optptr; 15542 struct T_opthdr *toh; 15543 uint_t addflag; /* Which pieces to add */ 15544 mblk_t *mp1; 15545 15546 optlen = 0; 15547 addflag = 0; 15548 /* If app asked for pktinfo and the index has changed ... */ 15549 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15550 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15551 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15552 optlen += sizeof (struct T_opthdr) + 15553 sizeof (struct in6_pktinfo); 15554 addflag |= TCP_IPV6_RECVPKTINFO; 15555 } 15556 /* If app asked for hoplimit and it has changed ... */ 15557 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15558 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15559 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15560 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15561 addflag |= TCP_IPV6_RECVHOPLIMIT; 15562 } 15563 /* If app asked for tclass and it has changed ... */ 15564 if ((ipp->ipp_fields & IPPF_TCLASS) && 15565 ipp->ipp_tclass != tcp->tcp_recvtclass && 15566 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15567 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15568 addflag |= TCP_IPV6_RECVTCLASS; 15569 } 15570 /* 15571 * If app asked for hopbyhop headers and it has changed ... 15572 * For security labels, note that (1) security labels can't change on 15573 * a connected socket at all, (2) we're connected to at most one peer, 15574 * (3) if anything changes, then it must be some other extra option. 15575 */ 15576 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15577 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15578 (ipp->ipp_fields & IPPF_HOPOPTS), 15579 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15580 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15581 tcp->tcp_label_len; 15582 addflag |= TCP_IPV6_RECVHOPOPTS; 15583 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15584 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15585 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15586 return (mp); 15587 } 15588 /* If app asked for dst headers before routing headers ... */ 15589 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15590 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15591 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15592 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15593 optlen += sizeof (struct T_opthdr) + 15594 ipp->ipp_rtdstoptslen; 15595 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15596 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15597 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15598 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15599 return (mp); 15600 } 15601 /* If app asked for routing headers and it has changed ... */ 15602 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15603 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15604 (ipp->ipp_fields & IPPF_RTHDR), 15605 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15606 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15607 addflag |= TCP_IPV6_RECVRTHDR; 15608 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15609 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15610 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15611 return (mp); 15612 } 15613 /* If app asked for dest headers and it has changed ... */ 15614 if ((tcp->tcp_ipv6_recvancillary & 15615 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15616 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15617 (ipp->ipp_fields & IPPF_DSTOPTS), 15618 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15619 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15620 addflag |= TCP_IPV6_RECVDSTOPTS; 15621 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15622 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15623 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15624 return (mp); 15625 } 15626 15627 if (optlen == 0) { 15628 /* Nothing to add */ 15629 return (mp); 15630 } 15631 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15632 if (mp1 == NULL) { 15633 /* 15634 * Defer sending ancillary data until the next TCP segment 15635 * arrives. 15636 */ 15637 return (mp); 15638 } 15639 mp1->b_cont = mp; 15640 mp = mp1; 15641 mp->b_wptr += sizeof (*todi) + optlen; 15642 mp->b_datap->db_type = M_PROTO; 15643 todi = (struct T_optdata_ind *)mp->b_rptr; 15644 todi->PRIM_type = T_OPTDATA_IND; 15645 todi->DATA_flag = 1; /* MORE data */ 15646 todi->OPT_length = optlen; 15647 todi->OPT_offset = sizeof (*todi); 15648 optptr = (uchar_t *)&todi[1]; 15649 /* 15650 * If app asked for pktinfo and the index has changed ... 15651 * Note that the local address never changes for the connection. 15652 */ 15653 if (addflag & TCP_IPV6_RECVPKTINFO) { 15654 struct in6_pktinfo *pkti; 15655 15656 toh = (struct T_opthdr *)optptr; 15657 toh->level = IPPROTO_IPV6; 15658 toh->name = IPV6_PKTINFO; 15659 toh->len = sizeof (*toh) + sizeof (*pkti); 15660 toh->status = 0; 15661 optptr += sizeof (*toh); 15662 pkti = (struct in6_pktinfo *)optptr; 15663 if (tcp->tcp_ipversion == IPV6_VERSION) 15664 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15665 else 15666 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15667 &pkti->ipi6_addr); 15668 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15669 optptr += sizeof (*pkti); 15670 ASSERT(OK_32PTR(optptr)); 15671 /* Save as "last" value */ 15672 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15673 } 15674 /* If app asked for hoplimit and it has changed ... */ 15675 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15676 toh = (struct T_opthdr *)optptr; 15677 toh->level = IPPROTO_IPV6; 15678 toh->name = IPV6_HOPLIMIT; 15679 toh->len = sizeof (*toh) + sizeof (uint_t); 15680 toh->status = 0; 15681 optptr += sizeof (*toh); 15682 *(uint_t *)optptr = ipp->ipp_hoplimit; 15683 optptr += sizeof (uint_t); 15684 ASSERT(OK_32PTR(optptr)); 15685 /* Save as "last" value */ 15686 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15687 } 15688 /* If app asked for tclass and it has changed ... */ 15689 if (addflag & TCP_IPV6_RECVTCLASS) { 15690 toh = (struct T_opthdr *)optptr; 15691 toh->level = IPPROTO_IPV6; 15692 toh->name = IPV6_TCLASS; 15693 toh->len = sizeof (*toh) + sizeof (uint_t); 15694 toh->status = 0; 15695 optptr += sizeof (*toh); 15696 *(uint_t *)optptr = ipp->ipp_tclass; 15697 optptr += sizeof (uint_t); 15698 ASSERT(OK_32PTR(optptr)); 15699 /* Save as "last" value */ 15700 tcp->tcp_recvtclass = ipp->ipp_tclass; 15701 } 15702 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15703 toh = (struct T_opthdr *)optptr; 15704 toh->level = IPPROTO_IPV6; 15705 toh->name = IPV6_HOPOPTS; 15706 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15707 tcp->tcp_label_len; 15708 toh->status = 0; 15709 optptr += sizeof (*toh); 15710 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15711 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15712 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15713 ASSERT(OK_32PTR(optptr)); 15714 /* Save as last value */ 15715 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15716 (ipp->ipp_fields & IPPF_HOPOPTS), 15717 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15718 } 15719 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15720 toh = (struct T_opthdr *)optptr; 15721 toh->level = IPPROTO_IPV6; 15722 toh->name = IPV6_RTHDRDSTOPTS; 15723 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15724 toh->status = 0; 15725 optptr += sizeof (*toh); 15726 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15727 optptr += ipp->ipp_rtdstoptslen; 15728 ASSERT(OK_32PTR(optptr)); 15729 /* Save as last value */ 15730 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15731 &tcp->tcp_rtdstoptslen, 15732 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15733 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15734 } 15735 if (addflag & TCP_IPV6_RECVRTHDR) { 15736 toh = (struct T_opthdr *)optptr; 15737 toh->level = IPPROTO_IPV6; 15738 toh->name = IPV6_RTHDR; 15739 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15740 toh->status = 0; 15741 optptr += sizeof (*toh); 15742 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15743 optptr += ipp->ipp_rthdrlen; 15744 ASSERT(OK_32PTR(optptr)); 15745 /* Save as last value */ 15746 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15747 (ipp->ipp_fields & IPPF_RTHDR), 15748 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15749 } 15750 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15751 toh = (struct T_opthdr *)optptr; 15752 toh->level = IPPROTO_IPV6; 15753 toh->name = IPV6_DSTOPTS; 15754 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15755 toh->status = 0; 15756 optptr += sizeof (*toh); 15757 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15758 optptr += ipp->ipp_dstoptslen; 15759 ASSERT(OK_32PTR(optptr)); 15760 /* Save as last value */ 15761 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15762 (ipp->ipp_fields & IPPF_DSTOPTS), 15763 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15764 } 15765 ASSERT(optptr == mp->b_wptr); 15766 return (mp); 15767 } 15768 15769 /* 15770 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15771 * messages. 15772 */ 15773 void 15774 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15775 { 15776 uchar_t *rptr = mp->b_rptr; 15777 queue_t *q = tcp->tcp_rq; 15778 struct T_error_ack *tea; 15779 15780 switch (mp->b_datap->db_type) { 15781 case M_PROTO: 15782 case M_PCPROTO: 15783 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15784 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15785 break; 15786 tea = (struct T_error_ack *)rptr; 15787 ASSERT(tea->PRIM_type != T_BIND_ACK); 15788 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15789 tea->ERROR_prim != T_BIND_REQ); 15790 switch (tea->PRIM_type) { 15791 case T_ERROR_ACK: 15792 if (tcp->tcp_debug) { 15793 (void) strlog(TCP_MOD_ID, 0, 1, 15794 SL_TRACE|SL_ERROR, 15795 "tcp_rput_other: case T_ERROR_ACK, " 15796 "ERROR_prim == %d", 15797 tea->ERROR_prim); 15798 } 15799 switch (tea->ERROR_prim) { 15800 case T_SVR4_OPTMGMT_REQ: 15801 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15802 /* T_OPTMGMT_REQ generated by TCP */ 15803 printf("T_SVR4_OPTMGMT_REQ failed " 15804 "%d/%d - dropped (cnt %d)\n", 15805 tea->TLI_error, tea->UNIX_error, 15806 tcp->tcp_drop_opt_ack_cnt); 15807 freemsg(mp); 15808 tcp->tcp_drop_opt_ack_cnt--; 15809 return; 15810 } 15811 break; 15812 } 15813 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15814 tcp->tcp_drop_opt_ack_cnt > 0) { 15815 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15816 "- dropped (cnt %d)\n", 15817 tea->TLI_error, tea->UNIX_error, 15818 tcp->tcp_drop_opt_ack_cnt); 15819 freemsg(mp); 15820 tcp->tcp_drop_opt_ack_cnt--; 15821 return; 15822 } 15823 break; 15824 case T_OPTMGMT_ACK: 15825 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15826 /* T_OPTMGMT_REQ generated by TCP */ 15827 freemsg(mp); 15828 tcp->tcp_drop_opt_ack_cnt--; 15829 return; 15830 } 15831 break; 15832 default: 15833 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15834 break; 15835 } 15836 break; 15837 case M_FLUSH: 15838 if (*rptr & FLUSHR) 15839 flushq(q, FLUSHDATA); 15840 break; 15841 default: 15842 /* M_CTL will be directly sent to tcp_icmp_error() */ 15843 ASSERT(DB_TYPE(mp) != M_CTL); 15844 break; 15845 } 15846 /* 15847 * Make sure we set this bit before sending the ACK for 15848 * bind. Otherwise accept could possibly run and free 15849 * this tcp struct. 15850 */ 15851 ASSERT(q != NULL); 15852 putnext(q, mp); 15853 } 15854 15855 /* ARGSUSED */ 15856 static void 15857 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15858 { 15859 conn_t *connp = (conn_t *)arg; 15860 tcp_t *tcp = connp->conn_tcp; 15861 queue_t *q = tcp->tcp_rq; 15862 uint_t thwin; 15863 tcp_stack_t *tcps = tcp->tcp_tcps; 15864 sodirect_t *sodp; 15865 boolean_t fc; 15866 15867 ASSERT(!IPCL_IS_NONSTR(connp)); 15868 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15869 tcp->tcp_rsrv_mp = mp; 15870 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15871 15872 TCP_STAT(tcps, tcp_rsrv_calls); 15873 15874 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15875 return; 15876 } 15877 15878 if (tcp->tcp_fused) { 15879 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15880 15881 ASSERT(tcp->tcp_fused); 15882 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15883 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15884 ASSERT(!TCP_IS_DETACHED(tcp)); 15885 ASSERT(tcp->tcp_connp->conn_sqp == 15886 peer_tcp->tcp_connp->conn_sqp); 15887 15888 /* 15889 * Normally we would not get backenabled in synchronous 15890 * streams mode, but in case this happens, we need to plug 15891 * synchronous streams during our drain to prevent a race 15892 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15893 */ 15894 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15895 if (tcp->tcp_rcv_list != NULL) 15896 (void) tcp_rcv_drain(tcp); 15897 15898 if (peer_tcp > tcp) { 15899 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15900 mutex_enter(&tcp->tcp_non_sq_lock); 15901 } else { 15902 mutex_enter(&tcp->tcp_non_sq_lock); 15903 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15904 } 15905 15906 if (peer_tcp->tcp_flow_stopped && 15907 (TCP_UNSENT_BYTES(peer_tcp) <= 15908 peer_tcp->tcp_xmit_lowater)) { 15909 tcp_clrqfull(peer_tcp); 15910 } 15911 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15912 mutex_exit(&tcp->tcp_non_sq_lock); 15913 15914 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15915 TCP_STAT(tcps, tcp_fusion_backenabled); 15916 return; 15917 } 15918 15919 SOD_PTR_ENTER(tcp, sodp); 15920 if (sodp != NULL) { 15921 /* An sodirect connection */ 15922 if (SOD_QFULL(sodp)) { 15923 /* Flow-controlled, need another back-enable */ 15924 fc = B_TRUE; 15925 SOD_QSETBE(sodp); 15926 } else { 15927 /* Not flow-controlled */ 15928 fc = B_FALSE; 15929 } 15930 mutex_exit(sodp->sod_lockp); 15931 } else if (canputnext(q)) { 15932 /* STREAMS, not flow-controlled */ 15933 fc = B_FALSE; 15934 } else { 15935 /* STREAMS, flow-controlled */ 15936 fc = B_TRUE; 15937 } 15938 if (!fc) { 15939 /* Not flow-controlled, open rwnd */ 15940 tcp->tcp_rwnd = q->q_hiwat; 15941 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15942 << tcp->tcp_rcv_ws; 15943 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15944 /* 15945 * Send back a window update immediately if TCP is above 15946 * ESTABLISHED state and the increase of the rcv window 15947 * that the other side knows is at least 1 MSS after flow 15948 * control is lifted. 15949 */ 15950 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15951 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15952 tcp_xmit_ctl(NULL, tcp, 15953 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15954 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15955 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15956 } 15957 } 15958 } 15959 15960 /* 15961 * The read side service routine is called mostly when we get back-enabled as a 15962 * result of flow control relief. Since we don't actually queue anything in 15963 * TCP, we have no data to send out of here. What we do is clear the receive 15964 * window, and send out a window update. 15965 */ 15966 static void 15967 tcp_rsrv(queue_t *q) 15968 { 15969 conn_t *connp = Q_TO_CONN(q); 15970 tcp_t *tcp = connp->conn_tcp; 15971 mblk_t *mp; 15972 tcp_stack_t *tcps = tcp->tcp_tcps; 15973 15974 /* No code does a putq on the read side */ 15975 ASSERT(q->q_first == NULL); 15976 15977 /* Nothing to do for the default queue */ 15978 if (q == tcps->tcps_g_q) { 15979 return; 15980 } 15981 15982 /* 15983 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15984 * been run. So just return. 15985 */ 15986 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15987 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15988 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15989 return; 15990 } 15991 tcp->tcp_rsrv_mp = NULL; 15992 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15993 15994 CONN_INC_REF(connp); 15995 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15996 SQ_PROCESS, SQTAG_TCP_RSRV); 15997 } 15998 15999 /* 16000 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16001 * We do not allow the receive window to shrink. After setting rwnd, 16002 * set the flow control hiwat of the stream. 16003 * 16004 * This function is called in 2 cases: 16005 * 16006 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16007 * connection (passive open) and in tcp_rput_data() for active connect. 16008 * This is called after tcp_mss_set() when the desired MSS value is known. 16009 * This makes sure that our window size is a mutiple of the other side's 16010 * MSS. 16011 * 2) Handling SO_RCVBUF option. 16012 * 16013 * It is ASSUMED that the requested size is a multiple of the current MSS. 16014 * 16015 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16016 * user requests so. 16017 */ 16018 static int 16019 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16020 { 16021 uint32_t mss = tcp->tcp_mss; 16022 uint32_t old_max_rwnd; 16023 uint32_t max_transmittable_rwnd; 16024 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16025 tcp_stack_t *tcps = tcp->tcp_tcps; 16026 16027 if (tcp->tcp_fused) { 16028 size_t sth_hiwat; 16029 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16030 16031 ASSERT(peer_tcp != NULL); 16032 /* 16033 * Record the stream head's high water mark for 16034 * this endpoint; this is used for flow-control 16035 * purposes in tcp_fuse_output(). 16036 */ 16037 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16038 if (!tcp_detached) { 16039 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16040 sth_hiwat); 16041 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16042 conn_t *connp = tcp->tcp_connp; 16043 struct sock_proto_props sopp; 16044 16045 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16046 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16047 16048 (*connp->conn_upcalls->su_set_proto_props) 16049 (connp->conn_upper_handle, &sopp); 16050 } 16051 } 16052 16053 /* 16054 * In the fusion case, the maxpsz stream head value of 16055 * our peer is set according to its send buffer size 16056 * and our receive buffer size; since the latter may 16057 * have changed we need to update the peer's maxpsz. 16058 */ 16059 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16060 return (rwnd); 16061 } 16062 16063 if (tcp_detached) { 16064 old_max_rwnd = tcp->tcp_rwnd; 16065 } else { 16066 old_max_rwnd = tcp->tcp_recv_hiwater; 16067 } 16068 16069 /* 16070 * Insist on a receive window that is at least 16071 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16072 * funny TCP interactions of Nagle algorithm, SWS avoidance 16073 * and delayed acknowledgement. 16074 */ 16075 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16076 16077 /* 16078 * If window size info has already been exchanged, TCP should not 16079 * shrink the window. Shrinking window is doable if done carefully. 16080 * We may add that support later. But so far there is not a real 16081 * need to do that. 16082 */ 16083 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16084 /* MSS may have changed, do a round up again. */ 16085 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16086 } 16087 16088 /* 16089 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16090 * can be applied even before the window scale option is decided. 16091 */ 16092 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16093 if (rwnd > max_transmittable_rwnd) { 16094 rwnd = max_transmittable_rwnd - 16095 (max_transmittable_rwnd % mss); 16096 if (rwnd < mss) 16097 rwnd = max_transmittable_rwnd; 16098 /* 16099 * If we're over the limit we may have to back down tcp_rwnd. 16100 * The increment below won't work for us. So we set all three 16101 * here and the increment below will have no effect. 16102 */ 16103 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16104 } 16105 if (tcp->tcp_localnet) { 16106 tcp->tcp_rack_abs_max = 16107 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16108 } else { 16109 /* 16110 * For a remote host on a different subnet (through a router), 16111 * we ack every other packet to be conforming to RFC1122. 16112 * tcp_deferred_acks_max is default to 2. 16113 */ 16114 tcp->tcp_rack_abs_max = 16115 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16116 } 16117 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16118 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16119 else 16120 tcp->tcp_rack_cur_max = 0; 16121 /* 16122 * Increment the current rwnd by the amount the maximum grew (we 16123 * can not overwrite it since we might be in the middle of a 16124 * connection.) 16125 */ 16126 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16127 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16128 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16129 tcp->tcp_cwnd_max = rwnd; 16130 16131 if (tcp_detached) 16132 return (rwnd); 16133 /* 16134 * We set the maximum receive window into rq->q_hiwat if it is 16135 * a STREAMS socket. 16136 * This is not actually used for flow control. 16137 */ 16138 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16139 tcp->tcp_rq->q_hiwat = rwnd; 16140 tcp->tcp_recv_hiwater = rwnd; 16141 /* 16142 * Set the STREAM head high water mark. This doesn't have to be 16143 * here, since we are simply using default values, but we would 16144 * prefer to choose these values algorithmically, with a likely 16145 * relationship to rwnd. 16146 */ 16147 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16148 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16149 return (rwnd); 16150 } 16151 16152 /* 16153 * Return SNMP stuff in buffer in mpdata. 16154 */ 16155 mblk_t * 16156 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16157 { 16158 mblk_t *mpdata; 16159 mblk_t *mp_conn_ctl = NULL; 16160 mblk_t *mp_conn_tail; 16161 mblk_t *mp_attr_ctl = NULL; 16162 mblk_t *mp_attr_tail; 16163 mblk_t *mp6_conn_ctl = NULL; 16164 mblk_t *mp6_conn_tail; 16165 mblk_t *mp6_attr_ctl = NULL; 16166 mblk_t *mp6_attr_tail; 16167 struct opthdr *optp; 16168 mib2_tcpConnEntry_t tce; 16169 mib2_tcp6ConnEntry_t tce6; 16170 mib2_transportMLPEntry_t mlp; 16171 connf_t *connfp; 16172 int i; 16173 boolean_t ispriv; 16174 zoneid_t zoneid; 16175 int v4_conn_idx; 16176 int v6_conn_idx; 16177 conn_t *connp = Q_TO_CONN(q); 16178 tcp_stack_t *tcps; 16179 ip_stack_t *ipst; 16180 mblk_t *mp2ctl; 16181 16182 /* 16183 * make a copy of the original message 16184 */ 16185 mp2ctl = copymsg(mpctl); 16186 16187 if (mpctl == NULL || 16188 (mpdata = mpctl->b_cont) == NULL || 16189 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16190 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16191 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16192 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16193 freemsg(mp_conn_ctl); 16194 freemsg(mp_attr_ctl); 16195 freemsg(mp6_conn_ctl); 16196 freemsg(mp6_attr_ctl); 16197 freemsg(mpctl); 16198 freemsg(mp2ctl); 16199 return (NULL); 16200 } 16201 16202 ipst = connp->conn_netstack->netstack_ip; 16203 tcps = connp->conn_netstack->netstack_tcp; 16204 16205 /* build table of connections -- need count in fixed part */ 16206 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16207 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16208 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16209 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16210 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16211 16212 ispriv = 16213 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16214 zoneid = Q_TO_CONN(q)->conn_zoneid; 16215 16216 v4_conn_idx = v6_conn_idx = 0; 16217 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16218 16219 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16220 ipst = tcps->tcps_netstack->netstack_ip; 16221 16222 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16223 16224 connp = NULL; 16225 16226 while ((connp = 16227 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16228 tcp_t *tcp; 16229 boolean_t needattr; 16230 16231 if (connp->conn_zoneid != zoneid) 16232 continue; /* not in this zone */ 16233 16234 tcp = connp->conn_tcp; 16235 UPDATE_MIB(&tcps->tcps_mib, 16236 tcpHCInSegs, tcp->tcp_ibsegs); 16237 tcp->tcp_ibsegs = 0; 16238 UPDATE_MIB(&tcps->tcps_mib, 16239 tcpHCOutSegs, tcp->tcp_obsegs); 16240 tcp->tcp_obsegs = 0; 16241 16242 tce6.tcp6ConnState = tce.tcpConnState = 16243 tcp_snmp_state(tcp); 16244 if (tce.tcpConnState == MIB2_TCP_established || 16245 tce.tcpConnState == MIB2_TCP_closeWait) 16246 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16247 16248 needattr = B_FALSE; 16249 bzero(&mlp, sizeof (mlp)); 16250 if (connp->conn_mlp_type != mlptSingle) { 16251 if (connp->conn_mlp_type == mlptShared || 16252 connp->conn_mlp_type == mlptBoth) 16253 mlp.tme_flags |= MIB2_TMEF_SHARED; 16254 if (connp->conn_mlp_type == mlptPrivate || 16255 connp->conn_mlp_type == mlptBoth) 16256 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16257 needattr = B_TRUE; 16258 } 16259 if (connp->conn_peercred != NULL) { 16260 ts_label_t *tsl; 16261 16262 tsl = crgetlabel(connp->conn_peercred); 16263 mlp.tme_doi = label2doi(tsl); 16264 mlp.tme_label = *label2bslabel(tsl); 16265 needattr = B_TRUE; 16266 } 16267 16268 /* Create a message to report on IPv6 entries */ 16269 if (tcp->tcp_ipversion == IPV6_VERSION) { 16270 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16271 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16272 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16273 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16274 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16275 /* Don't want just anybody seeing these... */ 16276 if (ispriv) { 16277 tce6.tcp6ConnEntryInfo.ce_snxt = 16278 tcp->tcp_snxt; 16279 tce6.tcp6ConnEntryInfo.ce_suna = 16280 tcp->tcp_suna; 16281 tce6.tcp6ConnEntryInfo.ce_rnxt = 16282 tcp->tcp_rnxt; 16283 tce6.tcp6ConnEntryInfo.ce_rack = 16284 tcp->tcp_rack; 16285 } else { 16286 /* 16287 * Netstat, unfortunately, uses this to 16288 * get send/receive queue sizes. How to fix? 16289 * Why not compute the difference only? 16290 */ 16291 tce6.tcp6ConnEntryInfo.ce_snxt = 16292 tcp->tcp_snxt - tcp->tcp_suna; 16293 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16294 tce6.tcp6ConnEntryInfo.ce_rnxt = 16295 tcp->tcp_rnxt - tcp->tcp_rack; 16296 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16297 } 16298 16299 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16300 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16301 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16302 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16303 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16304 16305 tce6.tcp6ConnCreationProcess = 16306 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16307 tcp->tcp_cpid; 16308 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16309 16310 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16311 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16312 16313 mlp.tme_connidx = v6_conn_idx++; 16314 if (needattr) 16315 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16316 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16317 } 16318 /* 16319 * Create an IPv4 table entry for IPv4 entries and also 16320 * for IPv6 entries which are bound to in6addr_any 16321 * but don't have IPV6_V6ONLY set. 16322 * (i.e. anything an IPv4 peer could connect to) 16323 */ 16324 if (tcp->tcp_ipversion == IPV4_VERSION || 16325 (tcp->tcp_state <= TCPS_LISTEN && 16326 !tcp->tcp_connp->conn_ipv6_v6only && 16327 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16328 if (tcp->tcp_ipversion == IPV6_VERSION) { 16329 tce.tcpConnRemAddress = INADDR_ANY; 16330 tce.tcpConnLocalAddress = INADDR_ANY; 16331 } else { 16332 tce.tcpConnRemAddress = 16333 tcp->tcp_remote; 16334 tce.tcpConnLocalAddress = 16335 tcp->tcp_ip_src; 16336 } 16337 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16338 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16339 /* Don't want just anybody seeing these... */ 16340 if (ispriv) { 16341 tce.tcpConnEntryInfo.ce_snxt = 16342 tcp->tcp_snxt; 16343 tce.tcpConnEntryInfo.ce_suna = 16344 tcp->tcp_suna; 16345 tce.tcpConnEntryInfo.ce_rnxt = 16346 tcp->tcp_rnxt; 16347 tce.tcpConnEntryInfo.ce_rack = 16348 tcp->tcp_rack; 16349 } else { 16350 /* 16351 * Netstat, unfortunately, uses this to 16352 * get send/receive queue sizes. How 16353 * to fix? 16354 * Why not compute the difference only? 16355 */ 16356 tce.tcpConnEntryInfo.ce_snxt = 16357 tcp->tcp_snxt - tcp->tcp_suna; 16358 tce.tcpConnEntryInfo.ce_suna = 0; 16359 tce.tcpConnEntryInfo.ce_rnxt = 16360 tcp->tcp_rnxt - tcp->tcp_rack; 16361 tce.tcpConnEntryInfo.ce_rack = 0; 16362 } 16363 16364 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16365 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16366 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16367 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16368 tce.tcpConnEntryInfo.ce_state = 16369 tcp->tcp_state; 16370 16371 tce.tcpConnCreationProcess = 16372 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16373 tcp->tcp_cpid; 16374 tce.tcpConnCreationTime = tcp->tcp_open_time; 16375 16376 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16377 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16378 16379 mlp.tme_connidx = v4_conn_idx++; 16380 if (needattr) 16381 (void) snmp_append_data2( 16382 mp_attr_ctl->b_cont, 16383 &mp_attr_tail, (char *)&mlp, 16384 sizeof (mlp)); 16385 } 16386 } 16387 } 16388 16389 /* fixed length structure for IPv4 and IPv6 counters */ 16390 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16391 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16392 sizeof (mib2_tcp6ConnEntry_t)); 16393 /* synchronize 32- and 64-bit counters */ 16394 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16395 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16396 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16397 optp->level = MIB2_TCP; 16398 optp->name = 0; 16399 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16400 sizeof (tcps->tcps_mib)); 16401 optp->len = msgdsize(mpdata); 16402 qreply(q, mpctl); 16403 16404 /* table of connections... */ 16405 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16406 sizeof (struct T_optmgmt_ack)]; 16407 optp->level = MIB2_TCP; 16408 optp->name = MIB2_TCP_CONN; 16409 optp->len = msgdsize(mp_conn_ctl->b_cont); 16410 qreply(q, mp_conn_ctl); 16411 16412 /* table of MLP attributes... */ 16413 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16414 sizeof (struct T_optmgmt_ack)]; 16415 optp->level = MIB2_TCP; 16416 optp->name = EXPER_XPORT_MLP; 16417 optp->len = msgdsize(mp_attr_ctl->b_cont); 16418 if (optp->len == 0) 16419 freemsg(mp_attr_ctl); 16420 else 16421 qreply(q, mp_attr_ctl); 16422 16423 /* table of IPv6 connections... */ 16424 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16425 sizeof (struct T_optmgmt_ack)]; 16426 optp->level = MIB2_TCP6; 16427 optp->name = MIB2_TCP6_CONN; 16428 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16429 qreply(q, mp6_conn_ctl); 16430 16431 /* table of IPv6 MLP attributes... */ 16432 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16433 sizeof (struct T_optmgmt_ack)]; 16434 optp->level = MIB2_TCP6; 16435 optp->name = EXPER_XPORT_MLP; 16436 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16437 if (optp->len == 0) 16438 freemsg(mp6_attr_ctl); 16439 else 16440 qreply(q, mp6_attr_ctl); 16441 return (mp2ctl); 16442 } 16443 16444 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16445 /* ARGSUSED */ 16446 int 16447 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16448 { 16449 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16450 16451 switch (level) { 16452 case MIB2_TCP: 16453 switch (name) { 16454 case 13: 16455 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16456 return (0); 16457 /* TODO: delete entry defined by tce */ 16458 return (1); 16459 default: 16460 return (0); 16461 } 16462 default: 16463 return (1); 16464 } 16465 } 16466 16467 /* Translate TCP state to MIB2 TCP state. */ 16468 static int 16469 tcp_snmp_state(tcp_t *tcp) 16470 { 16471 if (tcp == NULL) 16472 return (0); 16473 16474 switch (tcp->tcp_state) { 16475 case TCPS_CLOSED: 16476 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16477 case TCPS_BOUND: 16478 return (MIB2_TCP_closed); 16479 case TCPS_LISTEN: 16480 return (MIB2_TCP_listen); 16481 case TCPS_SYN_SENT: 16482 return (MIB2_TCP_synSent); 16483 case TCPS_SYN_RCVD: 16484 return (MIB2_TCP_synReceived); 16485 case TCPS_ESTABLISHED: 16486 return (MIB2_TCP_established); 16487 case TCPS_CLOSE_WAIT: 16488 return (MIB2_TCP_closeWait); 16489 case TCPS_FIN_WAIT_1: 16490 return (MIB2_TCP_finWait1); 16491 case TCPS_CLOSING: 16492 return (MIB2_TCP_closing); 16493 case TCPS_LAST_ACK: 16494 return (MIB2_TCP_lastAck); 16495 case TCPS_FIN_WAIT_2: 16496 return (MIB2_TCP_finWait2); 16497 case TCPS_TIME_WAIT: 16498 return (MIB2_TCP_timeWait); 16499 default: 16500 return (0); 16501 } 16502 } 16503 16504 /* 16505 * tcp_timer is the timer service routine. It handles the retransmission, 16506 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16507 * from the state of the tcp instance what kind of action needs to be done 16508 * at the time it is called. 16509 */ 16510 static void 16511 tcp_timer(void *arg) 16512 { 16513 mblk_t *mp; 16514 clock_t first_threshold; 16515 clock_t second_threshold; 16516 clock_t ms; 16517 uint32_t mss; 16518 conn_t *connp = (conn_t *)arg; 16519 tcp_t *tcp = connp->conn_tcp; 16520 tcp_stack_t *tcps = tcp->tcp_tcps; 16521 16522 tcp->tcp_timer_tid = 0; 16523 16524 if (tcp->tcp_fused) 16525 return; 16526 16527 first_threshold = tcp->tcp_first_timer_threshold; 16528 second_threshold = tcp->tcp_second_timer_threshold; 16529 switch (tcp->tcp_state) { 16530 case TCPS_IDLE: 16531 case TCPS_BOUND: 16532 case TCPS_LISTEN: 16533 return; 16534 case TCPS_SYN_RCVD: { 16535 tcp_t *listener = tcp->tcp_listener; 16536 16537 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16538 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16539 /* it's our first timeout */ 16540 tcp->tcp_syn_rcvd_timeout = 1; 16541 mutex_enter(&listener->tcp_eager_lock); 16542 listener->tcp_syn_rcvd_timeout++; 16543 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16544 /* 16545 * Make this eager available for drop if we 16546 * need to drop one to accomodate a new 16547 * incoming SYN request. 16548 */ 16549 MAKE_DROPPABLE(listener, tcp); 16550 } 16551 if (!listener->tcp_syn_defense && 16552 (listener->tcp_syn_rcvd_timeout > 16553 (tcps->tcps_conn_req_max_q0 >> 2)) && 16554 (tcps->tcps_conn_req_max_q0 > 200)) { 16555 /* We may be under attack. Put on a defense. */ 16556 listener->tcp_syn_defense = B_TRUE; 16557 cmn_err(CE_WARN, "High TCP connect timeout " 16558 "rate! System (port %d) may be under a " 16559 "SYN flood attack!", 16560 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16561 16562 listener->tcp_ip_addr_cache = kmem_zalloc( 16563 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16564 KM_NOSLEEP); 16565 } 16566 mutex_exit(&listener->tcp_eager_lock); 16567 } else if (listener != NULL) { 16568 mutex_enter(&listener->tcp_eager_lock); 16569 tcp->tcp_syn_rcvd_timeout++; 16570 if (tcp->tcp_syn_rcvd_timeout > 1 && 16571 !tcp->tcp_closemp_used) { 16572 /* 16573 * This is our second timeout. Put the tcp in 16574 * the list of droppable eagers to allow it to 16575 * be dropped, if needed. We don't check 16576 * whether tcp_dontdrop is set or not to 16577 * protect ourselve from a SYN attack where a 16578 * remote host can spoof itself as one of the 16579 * good IP source and continue to hold 16580 * resources too long. 16581 */ 16582 MAKE_DROPPABLE(listener, tcp); 16583 } 16584 mutex_exit(&listener->tcp_eager_lock); 16585 } 16586 } 16587 /* FALLTHRU */ 16588 case TCPS_SYN_SENT: 16589 first_threshold = tcp->tcp_first_ctimer_threshold; 16590 second_threshold = tcp->tcp_second_ctimer_threshold; 16591 break; 16592 case TCPS_ESTABLISHED: 16593 case TCPS_FIN_WAIT_1: 16594 case TCPS_CLOSING: 16595 case TCPS_CLOSE_WAIT: 16596 case TCPS_LAST_ACK: 16597 /* If we have data to rexmit */ 16598 if (tcp->tcp_suna != tcp->tcp_snxt) { 16599 clock_t time_to_wait; 16600 16601 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16602 if (!tcp->tcp_xmit_head) 16603 break; 16604 time_to_wait = lbolt - 16605 (clock_t)tcp->tcp_xmit_head->b_prev; 16606 time_to_wait = tcp->tcp_rto - 16607 TICK_TO_MSEC(time_to_wait); 16608 /* 16609 * If the timer fires too early, 1 clock tick earlier, 16610 * restart the timer. 16611 */ 16612 if (time_to_wait > msec_per_tick) { 16613 TCP_STAT(tcps, tcp_timer_fire_early); 16614 TCP_TIMER_RESTART(tcp, time_to_wait); 16615 return; 16616 } 16617 /* 16618 * When we probe zero windows, we force the swnd open. 16619 * If our peer acks with a closed window swnd will be 16620 * set to zero by tcp_rput(). As long as we are 16621 * receiving acks tcp_rput will 16622 * reset 'tcp_ms_we_have_waited' so as not to trip the 16623 * first and second interval actions. NOTE: the timer 16624 * interval is allowed to continue its exponential 16625 * backoff. 16626 */ 16627 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16628 if (tcp->tcp_debug) { 16629 (void) strlog(TCP_MOD_ID, 0, 1, 16630 SL_TRACE, "tcp_timer: zero win"); 16631 } 16632 } else { 16633 /* 16634 * After retransmission, we need to do 16635 * slow start. Set the ssthresh to one 16636 * half of current effective window and 16637 * cwnd to one MSS. Also reset 16638 * tcp_cwnd_cnt. 16639 * 16640 * Note that if tcp_ssthresh is reduced because 16641 * of ECN, do not reduce it again unless it is 16642 * already one window of data away (tcp_cwr 16643 * should then be cleared) or this is a 16644 * timeout for a retransmitted segment. 16645 */ 16646 uint32_t npkt; 16647 16648 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16649 npkt = ((tcp->tcp_timer_backoff ? 16650 tcp->tcp_cwnd_ssthresh : 16651 tcp->tcp_snxt - 16652 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16653 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16654 tcp->tcp_mss; 16655 } 16656 tcp->tcp_cwnd = tcp->tcp_mss; 16657 tcp->tcp_cwnd_cnt = 0; 16658 if (tcp->tcp_ecn_ok) { 16659 tcp->tcp_cwr = B_TRUE; 16660 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16661 tcp->tcp_ecn_cwr_sent = B_FALSE; 16662 } 16663 } 16664 break; 16665 } 16666 /* 16667 * We have something to send yet we cannot send. The 16668 * reason can be: 16669 * 16670 * 1. Zero send window: we need to do zero window probe. 16671 * 2. Zero cwnd: because of ECN, we need to "clock out 16672 * segments. 16673 * 3. SWS avoidance: receiver may have shrunk window, 16674 * reset our knowledge. 16675 * 16676 * Note that condition 2 can happen with either 1 or 16677 * 3. But 1 and 3 are exclusive. 16678 */ 16679 if (tcp->tcp_unsent != 0) { 16680 if (tcp->tcp_cwnd == 0) { 16681 /* 16682 * Set tcp_cwnd to 1 MSS so that a 16683 * new segment can be sent out. We 16684 * are "clocking out" new data when 16685 * the network is really congested. 16686 */ 16687 ASSERT(tcp->tcp_ecn_ok); 16688 tcp->tcp_cwnd = tcp->tcp_mss; 16689 } 16690 if (tcp->tcp_swnd == 0) { 16691 /* Extend window for zero window probe */ 16692 tcp->tcp_swnd++; 16693 tcp->tcp_zero_win_probe = B_TRUE; 16694 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16695 } else { 16696 /* 16697 * Handle timeout from sender SWS avoidance. 16698 * Reset our knowledge of the max send window 16699 * since the receiver might have reduced its 16700 * receive buffer. Avoid setting tcp_max_swnd 16701 * to one since that will essentially disable 16702 * the SWS checks. 16703 * 16704 * Note that since we don't have a SWS 16705 * state variable, if the timeout is set 16706 * for ECN but not for SWS, this 16707 * code will also be executed. This is 16708 * fine as tcp_max_swnd is updated 16709 * constantly and it will not affect 16710 * anything. 16711 */ 16712 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16713 } 16714 tcp_wput_data(tcp, NULL, B_FALSE); 16715 return; 16716 } 16717 /* Is there a FIN that needs to be to re retransmitted? */ 16718 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16719 !tcp->tcp_fin_acked) 16720 break; 16721 /* Nothing to do, return without restarting timer. */ 16722 TCP_STAT(tcps, tcp_timer_fire_miss); 16723 return; 16724 case TCPS_FIN_WAIT_2: 16725 /* 16726 * User closed the TCP endpoint and peer ACK'ed our FIN. 16727 * We waited some time for for peer's FIN, but it hasn't 16728 * arrived. We flush the connection now to avoid 16729 * case where the peer has rebooted. 16730 */ 16731 if (TCP_IS_DETACHED(tcp)) { 16732 (void) tcp_clean_death(tcp, 0, 23); 16733 } else { 16734 TCP_TIMER_RESTART(tcp, 16735 tcps->tcps_fin_wait_2_flush_interval); 16736 } 16737 return; 16738 case TCPS_TIME_WAIT: 16739 (void) tcp_clean_death(tcp, 0, 24); 16740 return; 16741 default: 16742 if (tcp->tcp_debug) { 16743 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16744 "tcp_timer: strange state (%d) %s", 16745 tcp->tcp_state, tcp_display(tcp, NULL, 16746 DISP_PORT_ONLY)); 16747 } 16748 return; 16749 } 16750 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16751 /* 16752 * For zero window probe, we need to send indefinitely, 16753 * unless we have not heard from the other side for some 16754 * time... 16755 */ 16756 if ((tcp->tcp_zero_win_probe == 0) || 16757 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16758 second_threshold)) { 16759 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16760 /* 16761 * If TCP is in SYN_RCVD state, send back a 16762 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16763 * should be zero in TCPS_SYN_RCVD state. 16764 */ 16765 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16766 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16767 "in SYN_RCVD", 16768 tcp, tcp->tcp_snxt, 16769 tcp->tcp_rnxt, TH_RST | TH_ACK); 16770 } 16771 (void) tcp_clean_death(tcp, 16772 tcp->tcp_client_errno ? 16773 tcp->tcp_client_errno : ETIMEDOUT, 25); 16774 return; 16775 } else { 16776 /* 16777 * Set tcp_ms_we_have_waited to second_threshold 16778 * so that in next timeout, we will do the above 16779 * check (lbolt - tcp_last_recv_time). This is 16780 * also to avoid overflow. 16781 * 16782 * We don't need to decrement tcp_timer_backoff 16783 * to avoid overflow because it will be decremented 16784 * later if new timeout value is greater than 16785 * tcp_rexmit_interval_max. In the case when 16786 * tcp_rexmit_interval_max is greater than 16787 * second_threshold, it means that we will wait 16788 * longer than second_threshold to send the next 16789 * window probe. 16790 */ 16791 tcp->tcp_ms_we_have_waited = second_threshold; 16792 } 16793 } else if (ms > first_threshold) { 16794 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16795 tcp->tcp_xmit_head != NULL) { 16796 tcp->tcp_xmit_head = 16797 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16798 } 16799 /* 16800 * We have been retransmitting for too long... The RTT 16801 * we calculated is probably incorrect. Reinitialize it. 16802 * Need to compensate for 0 tcp_rtt_sa. Reset 16803 * tcp_rtt_update so that we won't accidentally cache a 16804 * bad value. But only do this if this is not a zero 16805 * window probe. 16806 */ 16807 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16808 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16809 (tcp->tcp_rtt_sa >> 5); 16810 tcp->tcp_rtt_sa = 0; 16811 tcp_ip_notify(tcp); 16812 tcp->tcp_rtt_update = 0; 16813 } 16814 } 16815 tcp->tcp_timer_backoff++; 16816 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16817 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16818 tcps->tcps_rexmit_interval_min) { 16819 /* 16820 * This means the original RTO is tcp_rexmit_interval_min. 16821 * So we will use tcp_rexmit_interval_min as the RTO value 16822 * and do the backoff. 16823 */ 16824 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16825 } else { 16826 ms <<= tcp->tcp_timer_backoff; 16827 } 16828 if (ms > tcps->tcps_rexmit_interval_max) { 16829 ms = tcps->tcps_rexmit_interval_max; 16830 /* 16831 * ms is at max, decrement tcp_timer_backoff to avoid 16832 * overflow. 16833 */ 16834 tcp->tcp_timer_backoff--; 16835 } 16836 tcp->tcp_ms_we_have_waited += ms; 16837 if (tcp->tcp_zero_win_probe == 0) { 16838 tcp->tcp_rto = ms; 16839 } 16840 TCP_TIMER_RESTART(tcp, ms); 16841 /* 16842 * This is after a timeout and tcp_rto is backed off. Set 16843 * tcp_set_timer to 1 so that next time RTO is updated, we will 16844 * restart the timer with a correct value. 16845 */ 16846 tcp->tcp_set_timer = 1; 16847 mss = tcp->tcp_snxt - tcp->tcp_suna; 16848 if (mss > tcp->tcp_mss) 16849 mss = tcp->tcp_mss; 16850 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16851 mss = tcp->tcp_swnd; 16852 16853 if ((mp = tcp->tcp_xmit_head) != NULL) 16854 mp->b_prev = (mblk_t *)lbolt; 16855 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16856 B_TRUE); 16857 16858 /* 16859 * When slow start after retransmission begins, start with 16860 * this seq no. tcp_rexmit_max marks the end of special slow 16861 * start phase. tcp_snd_burst controls how many segments 16862 * can be sent because of an ack. 16863 */ 16864 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16865 tcp->tcp_snd_burst = TCP_CWND_SS; 16866 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16867 (tcp->tcp_unsent == 0)) { 16868 tcp->tcp_rexmit_max = tcp->tcp_fss; 16869 } else { 16870 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16871 } 16872 tcp->tcp_rexmit = B_TRUE; 16873 tcp->tcp_dupack_cnt = 0; 16874 16875 /* 16876 * Remove all rexmit SACK blk to start from fresh. 16877 */ 16878 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16879 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16880 tcp->tcp_num_notsack_blk = 0; 16881 tcp->tcp_cnt_notsack_list = 0; 16882 } 16883 if (mp == NULL) { 16884 return; 16885 } 16886 /* 16887 * Attach credentials to retransmitted initial SYNs. 16888 * In theory we should use the credentials from the connect() 16889 * call to ensure that getpeerucred() on the peer will be correct. 16890 * But we assume that SYN's are not dropped for loopback connections. 16891 */ 16892 if (tcp->tcp_state == TCPS_SYN_SENT) { 16893 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 16894 } 16895 16896 tcp->tcp_csuna = tcp->tcp_snxt; 16897 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16898 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16899 tcp_send_data(tcp, tcp->tcp_wq, mp); 16900 16901 } 16902 16903 static int 16904 tcp_do_unbind(conn_t *connp) 16905 { 16906 tcp_t *tcp = connp->conn_tcp; 16907 int error = 0; 16908 16909 switch (tcp->tcp_state) { 16910 case TCPS_BOUND: 16911 case TCPS_LISTEN: 16912 break; 16913 default: 16914 return (-TOUTSTATE); 16915 } 16916 16917 /* 16918 * Need to clean up all the eagers since after the unbind, segments 16919 * will no longer be delivered to this listener stream. 16920 */ 16921 mutex_enter(&tcp->tcp_eager_lock); 16922 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16923 tcp_eager_cleanup(tcp, 0); 16924 } 16925 mutex_exit(&tcp->tcp_eager_lock); 16926 16927 if (tcp->tcp_ipversion == IPV4_VERSION) { 16928 tcp->tcp_ipha->ipha_src = 0; 16929 } else { 16930 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16931 } 16932 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16933 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16934 tcp_bind_hash_remove(tcp); 16935 tcp->tcp_state = TCPS_IDLE; 16936 tcp->tcp_mdt = B_FALSE; 16937 16938 connp = tcp->tcp_connp; 16939 connp->conn_mdt_ok = B_FALSE; 16940 ipcl_hash_remove(connp); 16941 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16942 16943 return (error); 16944 } 16945 16946 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16947 static void 16948 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16949 { 16950 int error = tcp_do_unbind(tcp->tcp_connp); 16951 16952 if (error > 0) { 16953 tcp_err_ack(tcp, mp, TSYSERR, error); 16954 } else if (error < 0) { 16955 tcp_err_ack(tcp, mp, -error, 0); 16956 } else { 16957 /* Send M_FLUSH according to TPI */ 16958 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16959 16960 mp = mi_tpi_ok_ack_alloc(mp); 16961 putnext(tcp->tcp_rq, mp); 16962 } 16963 } 16964 16965 /* 16966 * Don't let port fall into the privileged range. 16967 * Since the extra privileged ports can be arbitrary we also 16968 * ensure that we exclude those from consideration. 16969 * tcp_g_epriv_ports is not sorted thus we loop over it until 16970 * there are no changes. 16971 * 16972 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16973 * but instead the code relies on: 16974 * - the fact that the address of the array and its size never changes 16975 * - the atomic assignment of the elements of the array 16976 * 16977 * Returns 0 if there are no more ports available. 16978 * 16979 * TS note: skip multilevel ports. 16980 */ 16981 static in_port_t 16982 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16983 { 16984 int i; 16985 boolean_t restart = B_FALSE; 16986 tcp_stack_t *tcps = tcp->tcp_tcps; 16987 16988 if (random && tcp_random_anon_port != 0) { 16989 (void) random_get_pseudo_bytes((uint8_t *)&port, 16990 sizeof (in_port_t)); 16991 /* 16992 * Unless changed by a sys admin, the smallest anon port 16993 * is 32768 and the largest anon port is 65535. It is 16994 * very likely (50%) for the random port to be smaller 16995 * than the smallest anon port. When that happens, 16996 * add port % (anon port range) to the smallest anon 16997 * port to get the random port. It should fall into the 16998 * valid anon port range. 16999 */ 17000 if (port < tcps->tcps_smallest_anon_port) { 17001 port = tcps->tcps_smallest_anon_port + 17002 port % (tcps->tcps_largest_anon_port - 17003 tcps->tcps_smallest_anon_port); 17004 } 17005 } 17006 17007 retry: 17008 if (port < tcps->tcps_smallest_anon_port) 17009 port = (in_port_t)tcps->tcps_smallest_anon_port; 17010 17011 if (port > tcps->tcps_largest_anon_port) { 17012 if (restart) 17013 return (0); 17014 restart = B_TRUE; 17015 port = (in_port_t)tcps->tcps_smallest_anon_port; 17016 } 17017 17018 if (port < tcps->tcps_smallest_nonpriv_port) 17019 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17020 17021 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17022 if (port == tcps->tcps_g_epriv_ports[i]) { 17023 port++; 17024 /* 17025 * Make sure whether the port is in the 17026 * valid range. 17027 */ 17028 goto retry; 17029 } 17030 } 17031 if (is_system_labeled() && 17032 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17033 IPPROTO_TCP, B_TRUE)) != 0) { 17034 port = i; 17035 goto retry; 17036 } 17037 return (port); 17038 } 17039 17040 /* 17041 * Return the next anonymous port in the privileged port range for 17042 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17043 * downwards. This is the same behavior as documented in the userland 17044 * library call rresvport(3N). 17045 * 17046 * TS note: skip multilevel ports. 17047 */ 17048 static in_port_t 17049 tcp_get_next_priv_port(const tcp_t *tcp) 17050 { 17051 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17052 in_port_t nextport; 17053 boolean_t restart = B_FALSE; 17054 tcp_stack_t *tcps = tcp->tcp_tcps; 17055 retry: 17056 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17057 next_priv_port >= IPPORT_RESERVED) { 17058 next_priv_port = IPPORT_RESERVED - 1; 17059 if (restart) 17060 return (0); 17061 restart = B_TRUE; 17062 } 17063 if (is_system_labeled() && 17064 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17065 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17066 next_priv_port = nextport; 17067 goto retry; 17068 } 17069 return (next_priv_port--); 17070 } 17071 17072 /* The write side r/w procedure. */ 17073 17074 #if CCS_STATS 17075 struct { 17076 struct { 17077 int64_t count, bytes; 17078 } tot, hit; 17079 } wrw_stats; 17080 #endif 17081 17082 /* 17083 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17084 * messages. 17085 */ 17086 /* ARGSUSED */ 17087 static void 17088 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17089 { 17090 conn_t *connp = (conn_t *)arg; 17091 tcp_t *tcp = connp->conn_tcp; 17092 queue_t *q = tcp->tcp_wq; 17093 17094 ASSERT(DB_TYPE(mp) != M_IOCTL); 17095 /* 17096 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17097 * Once the close starts, streamhead and sockfs will not let any data 17098 * packets come down (close ensures that there are no threads using the 17099 * queue and no new threads will come down) but since qprocsoff() 17100 * hasn't happened yet, a M_FLUSH or some non data message might 17101 * get reflected back (in response to our own FLUSHRW) and get 17102 * processed after tcp_close() is done. The conn would still be valid 17103 * because a ref would have added but we need to check the state 17104 * before actually processing the packet. 17105 */ 17106 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17107 freemsg(mp); 17108 return; 17109 } 17110 17111 switch (DB_TYPE(mp)) { 17112 case M_IOCDATA: 17113 tcp_wput_iocdata(tcp, mp); 17114 break; 17115 case M_FLUSH: 17116 tcp_wput_flush(tcp, mp); 17117 break; 17118 default: 17119 CALL_IP_WPUT(connp, q, mp); 17120 break; 17121 } 17122 } 17123 17124 /* 17125 * The TCP fast path write put procedure. 17126 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17127 */ 17128 /* ARGSUSED */ 17129 void 17130 tcp_output(void *arg, mblk_t *mp, void *arg2) 17131 { 17132 int len; 17133 int hdrlen; 17134 int plen; 17135 mblk_t *mp1; 17136 uchar_t *rptr; 17137 uint32_t snxt; 17138 tcph_t *tcph; 17139 struct datab *db; 17140 uint32_t suna; 17141 uint32_t mss; 17142 ipaddr_t *dst; 17143 ipaddr_t *src; 17144 uint32_t sum; 17145 int usable; 17146 conn_t *connp = (conn_t *)arg; 17147 tcp_t *tcp = connp->conn_tcp; 17148 uint32_t msize; 17149 tcp_stack_t *tcps = tcp->tcp_tcps; 17150 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17151 17152 /* 17153 * Try and ASSERT the minimum possible references on the 17154 * conn early enough. Since we are executing on write side, 17155 * the connection is obviously not detached and that means 17156 * there is a ref each for TCP and IP. Since we are behind 17157 * the squeue, the minimum references needed are 3. If the 17158 * conn is in classifier hash list, there should be an 17159 * extra ref for that (we check both the possibilities). 17160 */ 17161 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17162 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17163 17164 ASSERT(DB_TYPE(mp) == M_DATA); 17165 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17166 17167 mutex_enter(&tcp->tcp_non_sq_lock); 17168 tcp->tcp_squeue_bytes -= msize; 17169 mutex_exit(&tcp->tcp_non_sq_lock); 17170 17171 /* Check to see if this connection wants to be re-fused. */ 17172 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17173 if (tcp->tcp_ipversion == IPV4_VERSION) { 17174 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17175 &tcp->tcp_saved_tcph); 17176 } else { 17177 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17178 &tcp->tcp_saved_tcph); 17179 } 17180 } 17181 /* Bypass tcp protocol for fused tcp loopback */ 17182 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17183 return; 17184 17185 mss = tcp->tcp_mss; 17186 if (tcp->tcp_xmit_zc_clean) 17187 mp = tcp_zcopy_backoff(tcp, mp, 0); 17188 17189 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17190 len = (int)(mp->b_wptr - mp->b_rptr); 17191 17192 /* 17193 * Criteria for fast path: 17194 * 17195 * 1. no unsent data 17196 * 2. single mblk in request 17197 * 3. connection established 17198 * 4. data in mblk 17199 * 5. len <= mss 17200 * 6. no tcp_valid bits 17201 */ 17202 if ((tcp->tcp_unsent != 0) || 17203 (tcp->tcp_cork) || 17204 (mp->b_cont != NULL) || 17205 (tcp->tcp_state != TCPS_ESTABLISHED) || 17206 (len == 0) || 17207 (len > mss) || 17208 (tcp->tcp_valid_bits != 0)) { 17209 tcp_wput_data(tcp, mp, B_FALSE); 17210 return; 17211 } 17212 17213 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17214 ASSERT(tcp->tcp_fin_sent == 0); 17215 17216 /* queue new packet onto retransmission queue */ 17217 if (tcp->tcp_xmit_head == NULL) { 17218 tcp->tcp_xmit_head = mp; 17219 } else { 17220 tcp->tcp_xmit_last->b_cont = mp; 17221 } 17222 tcp->tcp_xmit_last = mp; 17223 tcp->tcp_xmit_tail = mp; 17224 17225 /* find out how much we can send */ 17226 /* BEGIN CSTYLED */ 17227 /* 17228 * un-acked usable 17229 * |--------------|-----------------| 17230 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17231 */ 17232 /* END CSTYLED */ 17233 17234 /* start sending from tcp_snxt */ 17235 snxt = tcp->tcp_snxt; 17236 17237 /* 17238 * Check to see if this connection has been idled for some 17239 * time and no ACK is expected. If it is, we need to slow 17240 * start again to get back the connection's "self-clock" as 17241 * described in VJ's paper. 17242 * 17243 * Refer to the comment in tcp_mss_set() for the calculation 17244 * of tcp_cwnd after idle. 17245 */ 17246 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17247 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17248 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17249 } 17250 17251 usable = tcp->tcp_swnd; /* tcp window size */ 17252 if (usable > tcp->tcp_cwnd) 17253 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17254 usable -= snxt; /* subtract stuff already sent */ 17255 suna = tcp->tcp_suna; 17256 usable += suna; 17257 /* usable can be < 0 if the congestion window is smaller */ 17258 if (len > usable) { 17259 /* Can't send complete M_DATA in one shot */ 17260 goto slow; 17261 } 17262 17263 mutex_enter(&tcp->tcp_non_sq_lock); 17264 if (tcp->tcp_flow_stopped && 17265 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17266 tcp_clrqfull(tcp); 17267 } 17268 mutex_exit(&tcp->tcp_non_sq_lock); 17269 17270 /* 17271 * determine if anything to send (Nagle). 17272 * 17273 * 1. len < tcp_mss (i.e. small) 17274 * 2. unacknowledged data present 17275 * 3. len < nagle limit 17276 * 4. last packet sent < nagle limit (previous packet sent) 17277 */ 17278 if ((len < mss) && (snxt != suna) && 17279 (len < (int)tcp->tcp_naglim) && 17280 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17281 /* 17282 * This was the first unsent packet and normally 17283 * mss < xmit_hiwater so there is no need to worry 17284 * about flow control. The next packet will go 17285 * through the flow control check in tcp_wput_data(). 17286 */ 17287 /* leftover work from above */ 17288 tcp->tcp_unsent = len; 17289 tcp->tcp_xmit_tail_unsent = len; 17290 17291 return; 17292 } 17293 17294 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17295 17296 if (snxt == suna) { 17297 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17298 } 17299 17300 /* we have always sent something */ 17301 tcp->tcp_rack_cnt = 0; 17302 17303 tcp->tcp_snxt = snxt + len; 17304 tcp->tcp_rack = tcp->tcp_rnxt; 17305 17306 if ((mp1 = dupb(mp)) == 0) 17307 goto no_memory; 17308 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17309 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17310 17311 /* adjust tcp header information */ 17312 tcph = tcp->tcp_tcph; 17313 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17314 17315 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17316 sum = (sum >> 16) + (sum & 0xFFFF); 17317 U16_TO_ABE16(sum, tcph->th_sum); 17318 17319 U32_TO_ABE32(snxt, tcph->th_seq); 17320 17321 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17322 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17323 BUMP_LOCAL(tcp->tcp_obsegs); 17324 17325 /* Update the latest receive window size in TCP header. */ 17326 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17327 tcph->th_win); 17328 17329 tcp->tcp_last_sent_len = (ushort_t)len; 17330 17331 plen = len + tcp->tcp_hdr_len; 17332 17333 if (tcp->tcp_ipversion == IPV4_VERSION) { 17334 tcp->tcp_ipha->ipha_length = htons(plen); 17335 } else { 17336 tcp->tcp_ip6h->ip6_plen = htons(plen - 17337 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17338 } 17339 17340 /* see if we need to allocate a mblk for the headers */ 17341 hdrlen = tcp->tcp_hdr_len; 17342 rptr = mp1->b_rptr - hdrlen; 17343 db = mp1->b_datap; 17344 if ((db->db_ref != 2) || rptr < db->db_base || 17345 (!OK_32PTR(rptr))) { 17346 /* NOTE: we assume allocb returns an OK_32PTR */ 17347 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17348 tcps->tcps_wroff_xtra, BPRI_MED); 17349 if (!mp) { 17350 freemsg(mp1); 17351 goto no_memory; 17352 } 17353 mp->b_cont = mp1; 17354 mp1 = mp; 17355 /* Leave room for Link Level header */ 17356 /* hdrlen = tcp->tcp_hdr_len; */ 17357 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17358 mp1->b_wptr = &rptr[hdrlen]; 17359 } 17360 mp1->b_rptr = rptr; 17361 17362 /* Fill in the timestamp option. */ 17363 if (tcp->tcp_snd_ts_ok) { 17364 U32_TO_BE32((uint32_t)lbolt, 17365 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17366 U32_TO_BE32(tcp->tcp_ts_recent, 17367 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17368 } else { 17369 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17370 } 17371 17372 /* copy header into outgoing packet */ 17373 dst = (ipaddr_t *)rptr; 17374 src = (ipaddr_t *)tcp->tcp_iphc; 17375 dst[0] = src[0]; 17376 dst[1] = src[1]; 17377 dst[2] = src[2]; 17378 dst[3] = src[3]; 17379 dst[4] = src[4]; 17380 dst[5] = src[5]; 17381 dst[6] = src[6]; 17382 dst[7] = src[7]; 17383 dst[8] = src[8]; 17384 dst[9] = src[9]; 17385 if (hdrlen -= 40) { 17386 hdrlen >>= 2; 17387 dst += 10; 17388 src += 10; 17389 do { 17390 *dst++ = *src++; 17391 } while (--hdrlen); 17392 } 17393 17394 /* 17395 * Set the ECN info in the TCP header. Note that this 17396 * is not the template header. 17397 */ 17398 if (tcp->tcp_ecn_ok) { 17399 SET_ECT(tcp, rptr); 17400 17401 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17402 if (tcp->tcp_ecn_echo_on) 17403 tcph->th_flags[0] |= TH_ECE; 17404 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17405 tcph->th_flags[0] |= TH_CWR; 17406 tcp->tcp_ecn_cwr_sent = B_TRUE; 17407 } 17408 } 17409 17410 if (tcp->tcp_ip_forward_progress) { 17411 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17412 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17413 tcp->tcp_ip_forward_progress = B_FALSE; 17414 } 17415 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17416 return; 17417 17418 /* 17419 * If we ran out of memory, we pretend to have sent the packet 17420 * and that it was lost on the wire. 17421 */ 17422 no_memory: 17423 return; 17424 17425 slow: 17426 /* leftover work from above */ 17427 tcp->tcp_unsent = len; 17428 tcp->tcp_xmit_tail_unsent = len; 17429 tcp_wput_data(tcp, NULL, B_FALSE); 17430 } 17431 17432 /* ARGSUSED */ 17433 void 17434 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17435 { 17436 conn_t *connp = (conn_t *)arg; 17437 tcp_t *tcp = connp->conn_tcp; 17438 queue_t *q = tcp->tcp_rq; 17439 struct tcp_options *tcpopt; 17440 tcp_stack_t *tcps = tcp->tcp_tcps; 17441 17442 /* socket options */ 17443 uint_t sopp_flags; 17444 ssize_t sopp_rxhiwat; 17445 ssize_t sopp_maxblk; 17446 ushort_t sopp_wroff; 17447 ushort_t sopp_tail; 17448 ushort_t sopp_copyopt; 17449 17450 tcpopt = (struct tcp_options *)mp->b_rptr; 17451 17452 /* 17453 * Drop the eager's ref on the listener, that was placed when 17454 * this eager began life in tcp_conn_request. 17455 */ 17456 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17457 if (IPCL_IS_NONSTR(connp)) { 17458 /* Safe to free conn_ind message */ 17459 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17460 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17461 } 17462 17463 tcp->tcp_detached = B_FALSE; 17464 17465 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17466 /* 17467 * Someone blewoff the eager before we could finish 17468 * the accept. 17469 * 17470 * The only reason eager exists it because we put in 17471 * a ref on it when conn ind went up. We need to send 17472 * a disconnect indication up while the last reference 17473 * on the eager will be dropped by the squeue when we 17474 * return. 17475 */ 17476 ASSERT(tcp->tcp_listener == NULL); 17477 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17478 if (IPCL_IS_NONSTR(connp)) { 17479 ASSERT(tcp->tcp_issocket); 17480 (*connp->conn_upcalls->su_disconnected)( 17481 connp->conn_upper_handle, tcp->tcp_connid, 17482 ECONNREFUSED); 17483 freemsg(mp); 17484 } else { 17485 struct T_discon_ind *tdi; 17486 17487 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17488 /* 17489 * Let us reuse the incoming mblk to avoid 17490 * memory allocation failure problems. We know 17491 * that the size of the incoming mblk i.e. 17492 * stroptions is greater than sizeof 17493 * T_discon_ind. So the reallocb below can't 17494 * fail. 17495 */ 17496 freemsg(mp->b_cont); 17497 mp->b_cont = NULL; 17498 ASSERT(DB_REF(mp) == 1); 17499 mp = reallocb(mp, sizeof (struct T_discon_ind), 17500 B_FALSE); 17501 ASSERT(mp != NULL); 17502 DB_TYPE(mp) = M_PROTO; 17503 ((union T_primitives *)mp->b_rptr)->type = 17504 T_DISCON_IND; 17505 tdi = (struct T_discon_ind *)mp->b_rptr; 17506 if (tcp->tcp_issocket) { 17507 tdi->DISCON_reason = ECONNREFUSED; 17508 tdi->SEQ_number = 0; 17509 } else { 17510 tdi->DISCON_reason = ENOPROTOOPT; 17511 tdi->SEQ_number = 17512 tcp->tcp_conn_req_seqnum; 17513 } 17514 mp->b_wptr = mp->b_rptr + 17515 sizeof (struct T_discon_ind); 17516 putnext(q, mp); 17517 return; 17518 } 17519 } 17520 if (tcp->tcp_hard_binding) { 17521 tcp->tcp_hard_binding = B_FALSE; 17522 tcp->tcp_hard_bound = B_TRUE; 17523 } 17524 return; 17525 } 17526 17527 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17528 int boundif = tcpopt->to_boundif; 17529 uint_t len = sizeof (int); 17530 17531 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17532 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17533 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17534 } 17535 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17536 uint_t on = 1; 17537 uint_t len = sizeof (uint_t); 17538 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17539 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17540 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17541 } 17542 17543 /* 17544 * For a loopback connection with tcp_direct_sockfs on, note that 17545 * we don't have to protect tcp_rcv_list yet because synchronous 17546 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17547 * possibly race with us. 17548 */ 17549 17550 /* 17551 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17552 * properly. This is the first time we know of the acceptor' 17553 * queue. So we do it here. 17554 * 17555 * XXX 17556 */ 17557 if (tcp->tcp_rcv_list == NULL) { 17558 /* 17559 * Recv queue is empty, tcp_rwnd should not have changed. 17560 * That means it should be equal to the listener's tcp_rwnd. 17561 */ 17562 if (!IPCL_IS_NONSTR(connp)) 17563 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17564 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17565 } else { 17566 #ifdef DEBUG 17567 mblk_t *tmp; 17568 mblk_t *mp1; 17569 uint_t cnt = 0; 17570 17571 mp1 = tcp->tcp_rcv_list; 17572 while ((tmp = mp1) != NULL) { 17573 mp1 = tmp->b_next; 17574 cnt += msgdsize(tmp); 17575 } 17576 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17577 #endif 17578 /* There is some data, add them back to get the max. */ 17579 if (!IPCL_IS_NONSTR(connp)) 17580 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17581 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17582 } 17583 /* 17584 * This is the first time we run on the correct 17585 * queue after tcp_accept. So fix all the q parameters 17586 * here. 17587 */ 17588 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17589 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17590 17591 /* 17592 * Record the stream head's high water mark for this endpoint; 17593 * this is used for flow-control purposes. 17594 */ 17595 sopp_rxhiwat = tcp->tcp_fused ? 17596 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17597 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17598 17599 /* 17600 * Determine what write offset value to use depending on SACK and 17601 * whether the endpoint is fused or not. 17602 */ 17603 if (tcp->tcp_fused) { 17604 ASSERT(tcp->tcp_loopback); 17605 ASSERT(tcp->tcp_loopback_peer != NULL); 17606 /* 17607 * For fused tcp loopback, set the stream head's write 17608 * offset value to zero since we won't be needing any room 17609 * for TCP/IP headers. This would also improve performance 17610 * since it would reduce the amount of work done by kmem. 17611 * Non-fused tcp loopback case is handled separately below. 17612 */ 17613 sopp_wroff = 0; 17614 /* 17615 * Update the peer's transmit parameters according to 17616 * our recently calculated high water mark value. 17617 */ 17618 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17619 } else if (tcp->tcp_snd_sack_ok) { 17620 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17621 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17622 } else { 17623 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17624 tcps->tcps_wroff_xtra); 17625 } 17626 17627 /* 17628 * If this is endpoint is handling SSL, then reserve extra 17629 * offset and space at the end. 17630 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17631 * overriding the previous setting. The extra cost of signing and 17632 * encrypting multiple MSS-size records (12 of them with Ethernet), 17633 * instead of a single contiguous one by the stream head 17634 * largely outweighs the statistical reduction of ACKs, when 17635 * applicable. The peer will also save on decryption and verification 17636 * costs. 17637 */ 17638 if (tcp->tcp_kssl_ctx != NULL) { 17639 sopp_wroff += SSL3_WROFFSET; 17640 17641 sopp_flags |= SOCKOPT_TAIL; 17642 sopp_tail = SSL3_MAX_TAIL_LEN; 17643 17644 sopp_flags |= SOCKOPT_ZCOPY; 17645 sopp_copyopt = ZCVMUNSAFE; 17646 17647 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17648 } 17649 17650 /* Send the options up */ 17651 if (IPCL_IS_NONSTR(connp)) { 17652 struct sock_proto_props sopp; 17653 17654 sopp.sopp_flags = sopp_flags; 17655 sopp.sopp_wroff = sopp_wroff; 17656 sopp.sopp_maxblk = sopp_maxblk; 17657 sopp.sopp_rxhiwat = sopp_rxhiwat; 17658 if (sopp_flags & SOCKOPT_TAIL) { 17659 ASSERT(tcp->tcp_kssl_ctx != NULL); 17660 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17661 sopp.sopp_tail = sopp_tail; 17662 sopp.sopp_zcopyflag = sopp_copyopt; 17663 } 17664 (*connp->conn_upcalls->su_set_proto_props) 17665 (connp->conn_upper_handle, &sopp); 17666 } else { 17667 struct stroptions *stropt; 17668 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17669 if (stropt_mp == NULL) { 17670 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17671 return; 17672 } 17673 DB_TYPE(stropt_mp) = M_SETOPTS; 17674 stropt = (struct stroptions *)stropt_mp->b_rptr; 17675 stropt_mp->b_wptr += sizeof (struct stroptions); 17676 stropt = (struct stroptions *)stropt_mp->b_rptr; 17677 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 17678 stropt->so_hiwat = sopp_rxhiwat; 17679 stropt->so_wroff = sopp_wroff; 17680 stropt->so_maxblk = sopp_maxblk; 17681 17682 if (sopp_flags & SOCKOPT_TAIL) { 17683 ASSERT(tcp->tcp_kssl_ctx != NULL); 17684 17685 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17686 stropt->so_tail = sopp_tail; 17687 stropt->so_copyopt = sopp_copyopt; 17688 } 17689 17690 /* Send the options up */ 17691 putnext(q, stropt_mp); 17692 } 17693 17694 freemsg(mp); 17695 /* 17696 * Pass up any data and/or a fin that has been received. 17697 * 17698 * Adjust receive window in case it had decreased 17699 * (because there is data <=> tcp_rcv_list != NULL) 17700 * while the connection was detached. Note that 17701 * in case the eager was flow-controlled, w/o this 17702 * code, the rwnd may never open up again! 17703 */ 17704 if (tcp->tcp_rcv_list != NULL) { 17705 if (IPCL_IS_NONSTR(connp)) { 17706 mblk_t *mp; 17707 int space_left; 17708 int error; 17709 boolean_t push = B_TRUE; 17710 17711 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17712 (connp->conn_upper_handle, NULL, 0, 0, &error, 17713 &push) >= 0) { 17714 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17715 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17716 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17717 tcp_xmit_ctl(NULL, 17718 tcp, (tcp->tcp_swnd == 0) ? 17719 tcp->tcp_suna : tcp->tcp_snxt, 17720 tcp->tcp_rnxt, TH_ACK); 17721 } 17722 } 17723 while ((mp = tcp->tcp_rcv_list) != NULL) { 17724 push = B_TRUE; 17725 tcp->tcp_rcv_list = mp->b_next; 17726 mp->b_next = NULL; 17727 space_left = (*connp->conn_upcalls->su_recv) 17728 (connp->conn_upper_handle, mp, msgdsize(mp), 17729 0, &error, &push); 17730 if (space_left < 0) { 17731 /* 17732 * We should never be in middle of a 17733 * fallback, the squeue guarantees that. 17734 */ 17735 ASSERT(error != EOPNOTSUPP); 17736 } 17737 } 17738 tcp->tcp_rcv_last_head = NULL; 17739 tcp->tcp_rcv_last_tail = NULL; 17740 tcp->tcp_rcv_cnt = 0; 17741 } else { 17742 /* We drain directly in case of fused tcp loopback */ 17743 sodirect_t *sodp; 17744 17745 if (!tcp->tcp_fused && canputnext(q)) { 17746 tcp->tcp_rwnd = q->q_hiwat; 17747 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17748 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17749 tcp_xmit_ctl(NULL, 17750 tcp, (tcp->tcp_swnd == 0) ? 17751 tcp->tcp_suna : tcp->tcp_snxt, 17752 tcp->tcp_rnxt, TH_ACK); 17753 } 17754 } 17755 17756 SOD_PTR_ENTER(tcp, sodp); 17757 if (sodp != NULL) { 17758 /* Sodirect, move from rcv_list */ 17759 ASSERT(!tcp->tcp_fused); 17760 while ((mp = tcp->tcp_rcv_list) != NULL) { 17761 tcp->tcp_rcv_list = mp->b_next; 17762 mp->b_next = NULL; 17763 (void) tcp_rcv_sod_enqueue(tcp, sodp, 17764 mp, msgdsize(mp)); 17765 } 17766 tcp->tcp_rcv_last_head = NULL; 17767 tcp->tcp_rcv_last_tail = NULL; 17768 tcp->tcp_rcv_cnt = 0; 17769 (void) tcp_rcv_sod_wakeup(tcp, sodp); 17770 /* sod_wakeup() did the mutex_exit() */ 17771 } else { 17772 /* Not sodirect, drain */ 17773 (void) tcp_rcv_drain(tcp); 17774 } 17775 } 17776 17777 /* 17778 * For fused tcp loopback, back-enable peer endpoint 17779 * if it's currently flow-controlled. 17780 */ 17781 if (tcp->tcp_fused) { 17782 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17783 17784 ASSERT(peer_tcp != NULL); 17785 ASSERT(peer_tcp->tcp_fused); 17786 /* 17787 * In order to change the peer's tcp_flow_stopped, 17788 * we need to take locks for both end points. The 17789 * highest address is taken first. 17790 */ 17791 if (peer_tcp > tcp) { 17792 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17793 mutex_enter(&tcp->tcp_non_sq_lock); 17794 } else { 17795 mutex_enter(&tcp->tcp_non_sq_lock); 17796 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17797 } 17798 if (peer_tcp->tcp_flow_stopped) { 17799 tcp_clrqfull(peer_tcp); 17800 TCP_STAT(tcps, tcp_fusion_backenabled); 17801 } 17802 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17803 mutex_exit(&tcp->tcp_non_sq_lock); 17804 } 17805 } 17806 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17807 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17808 tcp->tcp_ordrel_done = B_TRUE; 17809 if (IPCL_IS_NONSTR(connp)) { 17810 ASSERT(tcp->tcp_ordrel_mp == NULL); 17811 (*connp->conn_upcalls->su_opctl)( 17812 connp->conn_upper_handle, 17813 SOCK_OPCTL_SHUT_RECV, 0); 17814 } else { 17815 mp = tcp->tcp_ordrel_mp; 17816 tcp->tcp_ordrel_mp = NULL; 17817 putnext(q, mp); 17818 } 17819 } 17820 if (tcp->tcp_hard_binding) { 17821 tcp->tcp_hard_binding = B_FALSE; 17822 tcp->tcp_hard_bound = B_TRUE; 17823 } 17824 17825 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17826 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17827 tcp->tcp_loopback_peer != NULL && 17828 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17829 tcp_fuse_syncstr_enable_pair(tcp); 17830 } 17831 17832 if (tcp->tcp_ka_enabled) { 17833 tcp->tcp_ka_last_intrvl = 0; 17834 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17835 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17836 } 17837 17838 /* 17839 * At this point, eager is fully established and will 17840 * have the following references - 17841 * 17842 * 2 references for connection to exist (1 for TCP and 1 for IP). 17843 * 1 reference for the squeue which will be dropped by the squeue as 17844 * soon as this function returns. 17845 * There will be 1 additonal reference for being in classifier 17846 * hash list provided something bad hasn't happened. 17847 */ 17848 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17849 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17850 } 17851 17852 /* 17853 * The function called through squeue to get behind listener's perimeter to 17854 * send a deffered conn_ind. 17855 */ 17856 /* ARGSUSED */ 17857 void 17858 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17859 { 17860 conn_t *connp = (conn_t *)arg; 17861 tcp_t *listener = connp->conn_tcp; 17862 struct T_conn_ind *conn_ind; 17863 tcp_t *tcp; 17864 17865 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17866 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17867 conn_ind->OPT_length); 17868 17869 if (listener->tcp_state == TCPS_CLOSED || 17870 TCP_IS_DETACHED(listener)) { 17871 /* 17872 * If listener has closed, it would have caused a 17873 * a cleanup/blowoff to happen for the eager. 17874 * 17875 * We need to drop the ref on eager that was put 17876 * tcp_rput_data() before trying to send the conn_ind 17877 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17878 * and tcp_wput_accept() is sending this deferred conn_ind but 17879 * listener is closed so we drop the ref. 17880 */ 17881 CONN_DEC_REF(tcp->tcp_connp); 17882 freemsg(mp); 17883 return; 17884 } 17885 17886 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17887 } 17888 17889 /* ARGSUSED */ 17890 static int 17891 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17892 { 17893 tcp_t *listener, *eager; 17894 mblk_t *opt_mp; 17895 struct tcp_options *tcpopt; 17896 17897 listener = lconnp->conn_tcp; 17898 ASSERT(listener->tcp_state == TCPS_LISTEN); 17899 eager = econnp->conn_tcp; 17900 ASSERT(eager->tcp_listener != NULL); 17901 17902 ASSERT(eager->tcp_rq != NULL); 17903 17904 /* If tcp_fused and sodirect enabled disable it */ 17905 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 17906 /* Fused, disable sodirect */ 17907 mutex_enter(eager->tcp_sodirect->sod_lockp); 17908 SOD_DISABLE(eager->tcp_sodirect); 17909 mutex_exit(eager->tcp_sodirect->sod_lockp); 17910 eager->tcp_sodirect = NULL; 17911 } 17912 17913 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17914 if (opt_mp == NULL) { 17915 return (-TPROTO); 17916 } 17917 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17918 eager->tcp_issocket = B_TRUE; 17919 17920 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17921 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17922 ASSERT(econnp->conn_netstack == 17923 listener->tcp_connp->conn_netstack); 17924 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17925 17926 /* Put the ref for IP */ 17927 CONN_INC_REF(econnp); 17928 17929 /* 17930 * We should have minimum of 3 references on the conn 17931 * at this point. One each for TCP and IP and one for 17932 * the T_conn_ind that was sent up when the 3-way handshake 17933 * completed. In the normal case we would also have another 17934 * reference (making a total of 4) for the conn being in the 17935 * classifier hash list. However the eager could have received 17936 * an RST subsequently and tcp_closei_local could have removed 17937 * the eager from the classifier hash list, hence we can't 17938 * assert that reference. 17939 */ 17940 ASSERT(econnp->conn_ref >= 3); 17941 17942 opt_mp->b_datap->db_type = M_SETOPTS; 17943 opt_mp->b_wptr += sizeof (struct tcp_options); 17944 17945 /* 17946 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17947 * from listener to acceptor. 17948 */ 17949 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17950 tcpopt->to_flags = 0; 17951 17952 if (listener->tcp_bound_if != 0) { 17953 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17954 tcpopt->to_boundif = listener->tcp_bound_if; 17955 } 17956 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17957 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17958 } 17959 17960 mutex_enter(&listener->tcp_eager_lock); 17961 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17962 17963 tcp_t *tail; 17964 tcp_t *tcp; 17965 mblk_t *mp1; 17966 17967 tcp = listener->tcp_eager_prev_q0; 17968 /* 17969 * listener->tcp_eager_prev_q0 points to the TAIL of the 17970 * deferred T_conn_ind queue. We need to get to the head 17971 * of the queue in order to send up T_conn_ind the same 17972 * order as how the 3WHS is completed. 17973 */ 17974 while (tcp != listener) { 17975 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17976 !tcp->tcp_kssl_pending) 17977 break; 17978 else 17979 tcp = tcp->tcp_eager_prev_q0; 17980 } 17981 /* None of the pending eagers can be sent up now */ 17982 if (tcp == listener) 17983 goto no_more_eagers; 17984 17985 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17986 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17987 /* Move from q0 to q */ 17988 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17989 listener->tcp_conn_req_cnt_q0--; 17990 listener->tcp_conn_req_cnt_q++; 17991 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17992 tcp->tcp_eager_prev_q0; 17993 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17994 tcp->tcp_eager_next_q0; 17995 tcp->tcp_eager_prev_q0 = NULL; 17996 tcp->tcp_eager_next_q0 = NULL; 17997 tcp->tcp_conn_def_q0 = B_FALSE; 17998 17999 /* Make sure the tcp isn't in the list of droppables */ 18000 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18001 tcp->tcp_eager_prev_drop_q0 == NULL); 18002 18003 /* 18004 * Insert at end of the queue because sockfs sends 18005 * down T_CONN_RES in chronological order. Leaving 18006 * the older conn indications at front of the queue 18007 * helps reducing search time. 18008 */ 18009 tail = listener->tcp_eager_last_q; 18010 if (tail != NULL) { 18011 tail->tcp_eager_next_q = tcp; 18012 } else { 18013 listener->tcp_eager_next_q = tcp; 18014 } 18015 listener->tcp_eager_last_q = tcp; 18016 tcp->tcp_eager_next_q = NULL; 18017 18018 /* Need to get inside the listener perimeter */ 18019 CONN_INC_REF(listener->tcp_connp); 18020 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18021 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18022 SQTAG_TCP_SEND_PENDING); 18023 } 18024 no_more_eagers: 18025 tcp_eager_unlink(eager); 18026 mutex_exit(&listener->tcp_eager_lock); 18027 18028 /* 18029 * At this point, the eager is detached from the listener 18030 * but we still have an extra refs on eager (apart from the 18031 * usual tcp references). The ref was placed in tcp_rput_data 18032 * before sending the conn_ind in tcp_send_conn_ind. 18033 * The ref will be dropped in tcp_accept_finish(). 18034 */ 18035 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18036 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18037 return (0); 18038 } 18039 18040 int 18041 tcp_accept(sock_lower_handle_t lproto_handle, 18042 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18043 cred_t *cr) 18044 { 18045 conn_t *lconnp, *econnp; 18046 tcp_t *listener, *eager; 18047 tcp_stack_t *tcps; 18048 18049 lconnp = (conn_t *)lproto_handle; 18050 listener = lconnp->conn_tcp; 18051 ASSERT(listener->tcp_state == TCPS_LISTEN); 18052 econnp = (conn_t *)eproto_handle; 18053 eager = econnp->conn_tcp; 18054 ASSERT(eager->tcp_listener != NULL); 18055 tcps = eager->tcp_tcps; 18056 18057 /* 18058 * It is OK to manipulate these fields outside the eager's squeue 18059 * because they will not start being used until tcp_accept_finish 18060 * has been called. 18061 */ 18062 ASSERT(lconnp->conn_upper_handle != NULL); 18063 ASSERT(econnp->conn_upper_handle == NULL); 18064 econnp->conn_upper_handle = sock_handle; 18065 econnp->conn_upcalls = lconnp->conn_upcalls; 18066 ASSERT(IPCL_IS_NONSTR(econnp)); 18067 /* 18068 * Create helper stream if it is a non-TPI TCP connection. 18069 */ 18070 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18071 ip1dbg(("tcp_accept: create of IP helper stream" 18072 " failed\n")); 18073 return (EPROTO); 18074 } 18075 eager->tcp_rq = econnp->conn_rq; 18076 eager->tcp_wq = econnp->conn_wq; 18077 18078 ASSERT(eager->tcp_rq != NULL); 18079 18080 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18081 return (tcp_accept_common(lconnp, econnp, cr)); 18082 } 18083 18084 18085 /* 18086 * This is the STREAMS entry point for T_CONN_RES coming down on 18087 * Acceptor STREAM when sockfs listener does accept processing. 18088 * Read the block comment on top of tcp_conn_request(). 18089 */ 18090 void 18091 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18092 { 18093 queue_t *rq = RD(q); 18094 struct T_conn_res *conn_res; 18095 tcp_t *eager; 18096 tcp_t *listener; 18097 struct T_ok_ack *ok; 18098 t_scalar_t PRIM_type; 18099 conn_t *econnp; 18100 cred_t *cr; 18101 18102 ASSERT(DB_TYPE(mp) == M_PROTO); 18103 18104 /* 18105 * All Solaris components should pass a db_credp 18106 * for this TPI message, hence we ASSERT. 18107 * But in case there is some other M_PROTO that looks 18108 * like a TPI message sent by some other kernel 18109 * component, we check and return an error. 18110 */ 18111 cr = msg_getcred(mp, NULL); 18112 ASSERT(cr != NULL); 18113 if (cr == NULL) { 18114 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 18115 if (mp != NULL) 18116 putnext(rq, mp); 18117 return; 18118 } 18119 conn_res = (struct T_conn_res *)mp->b_rptr; 18120 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18121 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18122 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18123 if (mp != NULL) 18124 putnext(rq, mp); 18125 return; 18126 } 18127 switch (conn_res->PRIM_type) { 18128 case O_T_CONN_RES: 18129 case T_CONN_RES: 18130 /* 18131 * We pass up an err ack if allocb fails. This will 18132 * cause sockfs to issue a T_DISCON_REQ which will cause 18133 * tcp_eager_blowoff to be called. sockfs will then call 18134 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18135 * we need to do the allocb up here because we have to 18136 * make sure rq->q_qinfo->qi_qclose still points to the 18137 * correct function (tcp_tpi_close_accept) in case allocb 18138 * fails. 18139 */ 18140 bcopy(mp->b_rptr + conn_res->OPT_offset, 18141 &eager, conn_res->OPT_length); 18142 PRIM_type = conn_res->PRIM_type; 18143 mp->b_datap->db_type = M_PCPROTO; 18144 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18145 ok = (struct T_ok_ack *)mp->b_rptr; 18146 ok->PRIM_type = T_OK_ACK; 18147 ok->CORRECT_prim = PRIM_type; 18148 econnp = eager->tcp_connp; 18149 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18150 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18151 eager->tcp_rq = rq; 18152 eager->tcp_wq = q; 18153 rq->q_ptr = econnp; 18154 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18155 q->q_ptr = econnp; 18156 q->q_qinfo = &tcp_winit; 18157 listener = eager->tcp_listener; 18158 18159 /* 18160 * TCP is _D_SODIRECT and sockfs is directly above so 18161 * save shared sodirect_t pointer (if any). 18162 */ 18163 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18164 if (tcp_accept_common(listener->tcp_connp, 18165 econnp, cr) < 0) { 18166 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18167 if (mp != NULL) 18168 putnext(rq, mp); 18169 return; 18170 } 18171 18172 /* 18173 * Send the new local address also up to sockfs. There 18174 * should already be enough space in the mp that came 18175 * down from soaccept(). 18176 */ 18177 if (eager->tcp_family == AF_INET) { 18178 sin_t *sin; 18179 18180 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18181 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18182 sin = (sin_t *)mp->b_wptr; 18183 mp->b_wptr += sizeof (sin_t); 18184 sin->sin_family = AF_INET; 18185 sin->sin_port = eager->tcp_lport; 18186 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18187 } else { 18188 sin6_t *sin6; 18189 18190 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18191 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18192 sin6 = (sin6_t *)mp->b_wptr; 18193 mp->b_wptr += sizeof (sin6_t); 18194 sin6->sin6_family = AF_INET6; 18195 sin6->sin6_port = eager->tcp_lport; 18196 if (eager->tcp_ipversion == IPV4_VERSION) { 18197 sin6->sin6_flowinfo = 0; 18198 IN6_IPADDR_TO_V4MAPPED( 18199 eager->tcp_ipha->ipha_src, 18200 &sin6->sin6_addr); 18201 } else { 18202 ASSERT(eager->tcp_ip6h != NULL); 18203 sin6->sin6_flowinfo = 18204 eager->tcp_ip6h->ip6_vcf & 18205 ~IPV6_VERS_AND_FLOW_MASK; 18206 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18207 } 18208 sin6->sin6_scope_id = 0; 18209 sin6->__sin6_src_id = 0; 18210 } 18211 18212 putnext(rq, mp); 18213 return; 18214 default: 18215 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18216 if (mp != NULL) 18217 putnext(rq, mp); 18218 return; 18219 } 18220 } 18221 18222 static int 18223 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18224 { 18225 sin_t *sin = (sin_t *)sa; 18226 sin6_t *sin6 = (sin6_t *)sa; 18227 18228 switch (tcp->tcp_family) { 18229 case AF_INET: 18230 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18231 18232 if (*salenp < sizeof (sin_t)) 18233 return (EINVAL); 18234 18235 *sin = sin_null; 18236 sin->sin_family = AF_INET; 18237 if (tcp->tcp_state >= TCPS_BOUND) { 18238 sin->sin_port = tcp->tcp_lport; 18239 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18240 } 18241 *salenp = sizeof (sin_t); 18242 break; 18243 18244 case AF_INET6: 18245 if (*salenp < sizeof (sin6_t)) 18246 return (EINVAL); 18247 18248 *sin6 = sin6_null; 18249 sin6->sin6_family = AF_INET6; 18250 if (tcp->tcp_state >= TCPS_BOUND) { 18251 sin6->sin6_port = tcp->tcp_lport; 18252 if (tcp->tcp_ipversion == IPV4_VERSION) { 18253 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18254 &sin6->sin6_addr); 18255 } else { 18256 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18257 } 18258 } 18259 *salenp = sizeof (sin6_t); 18260 break; 18261 } 18262 18263 return (0); 18264 } 18265 18266 static int 18267 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18268 { 18269 sin_t *sin = (sin_t *)sa; 18270 sin6_t *sin6 = (sin6_t *)sa; 18271 18272 if (tcp->tcp_state < TCPS_SYN_RCVD) 18273 return (ENOTCONN); 18274 18275 switch (tcp->tcp_family) { 18276 case AF_INET: 18277 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18278 18279 if (*salenp < sizeof (sin_t)) 18280 return (EINVAL); 18281 18282 *sin = sin_null; 18283 sin->sin_family = AF_INET; 18284 sin->sin_port = tcp->tcp_fport; 18285 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18286 sin->sin_addr.s_addr); 18287 *salenp = sizeof (sin_t); 18288 break; 18289 18290 case AF_INET6: 18291 if (*salenp < sizeof (sin6_t)) 18292 return (EINVAL); 18293 18294 *sin6 = sin6_null; 18295 sin6->sin6_family = AF_INET6; 18296 sin6->sin6_port = tcp->tcp_fport; 18297 sin6->sin6_addr = tcp->tcp_remote_v6; 18298 if (tcp->tcp_ipversion == IPV6_VERSION) { 18299 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18300 ~IPV6_VERS_AND_FLOW_MASK; 18301 } 18302 *salenp = sizeof (sin6_t); 18303 break; 18304 } 18305 18306 return (0); 18307 } 18308 18309 /* 18310 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18311 */ 18312 static void 18313 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18314 { 18315 void *data; 18316 mblk_t *datamp = mp->b_cont; 18317 tcp_t *tcp = Q_TO_TCP(q); 18318 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18319 18320 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18321 cmdp->cb_error = EPROTO; 18322 qreply(q, mp); 18323 return; 18324 } 18325 18326 data = datamp->b_rptr; 18327 18328 switch (cmdp->cb_cmd) { 18329 case TI_GETPEERNAME: 18330 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 18331 break; 18332 case TI_GETMYNAME: 18333 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 18334 break; 18335 default: 18336 cmdp->cb_error = EINVAL; 18337 break; 18338 } 18339 18340 qreply(q, mp); 18341 } 18342 18343 void 18344 tcp_wput(queue_t *q, mblk_t *mp) 18345 { 18346 conn_t *connp = Q_TO_CONN(q); 18347 tcp_t *tcp; 18348 void (*output_proc)(); 18349 t_scalar_t type; 18350 uchar_t *rptr; 18351 struct iocblk *iocp; 18352 size_t size; 18353 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18354 18355 ASSERT(connp->conn_ref >= 2); 18356 18357 switch (DB_TYPE(mp)) { 18358 case M_DATA: 18359 tcp = connp->conn_tcp; 18360 ASSERT(tcp != NULL); 18361 18362 size = msgdsize(mp); 18363 18364 mutex_enter(&tcp->tcp_non_sq_lock); 18365 tcp->tcp_squeue_bytes += size; 18366 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18367 tcp_setqfull(tcp); 18368 } 18369 mutex_exit(&tcp->tcp_non_sq_lock); 18370 18371 CONN_INC_REF(connp); 18372 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18373 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18374 return; 18375 18376 case M_CMD: 18377 tcp_wput_cmdblk(q, mp); 18378 return; 18379 18380 case M_PROTO: 18381 case M_PCPROTO: 18382 /* 18383 * if it is a snmp message, don't get behind the squeue 18384 */ 18385 tcp = connp->conn_tcp; 18386 rptr = mp->b_rptr; 18387 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18388 type = ((union T_primitives *)rptr)->type; 18389 } else { 18390 if (tcp->tcp_debug) { 18391 (void) strlog(TCP_MOD_ID, 0, 1, 18392 SL_ERROR|SL_TRACE, 18393 "tcp_wput_proto, dropping one..."); 18394 } 18395 freemsg(mp); 18396 return; 18397 } 18398 if (type == T_SVR4_OPTMGMT_REQ) { 18399 /* 18400 * All Solaris components should pass a db_credp 18401 * for this TPI message, hence we ASSERT. 18402 * But in case there is some other M_PROTO that looks 18403 * like a TPI message sent by some other kernel 18404 * component, we check and return an error. 18405 */ 18406 cred_t *cr = msg_getcred(mp, NULL); 18407 18408 ASSERT(cr != NULL); 18409 if (cr == NULL) { 18410 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18411 return; 18412 } 18413 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18414 cr)) { 18415 /* 18416 * This was a SNMP request 18417 */ 18418 return; 18419 } else { 18420 output_proc = tcp_wput_proto; 18421 } 18422 } else { 18423 output_proc = tcp_wput_proto; 18424 } 18425 break; 18426 case M_IOCTL: 18427 /* 18428 * Most ioctls can be processed right away without going via 18429 * squeues - process them right here. Those that do require 18430 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18431 * are processed by tcp_wput_ioctl(). 18432 */ 18433 iocp = (struct iocblk *)mp->b_rptr; 18434 tcp = connp->conn_tcp; 18435 18436 switch (iocp->ioc_cmd) { 18437 case TCP_IOC_ABORT_CONN: 18438 tcp_ioctl_abort_conn(q, mp); 18439 return; 18440 case TI_GETPEERNAME: 18441 case TI_GETMYNAME: 18442 mi_copyin(q, mp, NULL, 18443 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18444 return; 18445 case ND_SET: 18446 /* nd_getset does the necessary checks */ 18447 case ND_GET: 18448 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18449 CALL_IP_WPUT(connp, q, mp); 18450 return; 18451 } 18452 qreply(q, mp); 18453 return; 18454 case TCP_IOC_DEFAULT_Q: 18455 /* 18456 * Wants to be the default wq. Check the credentials 18457 * first, the rest is executed via squeue. 18458 */ 18459 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18460 iocp->ioc_error = EPERM; 18461 iocp->ioc_count = 0; 18462 mp->b_datap->db_type = M_IOCACK; 18463 qreply(q, mp); 18464 return; 18465 } 18466 output_proc = tcp_wput_ioctl; 18467 break; 18468 default: 18469 output_proc = tcp_wput_ioctl; 18470 break; 18471 } 18472 break; 18473 default: 18474 output_proc = tcp_wput_nondata; 18475 break; 18476 } 18477 18478 CONN_INC_REF(connp); 18479 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18480 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18481 } 18482 18483 /* 18484 * Initial STREAMS write side put() procedure for sockets. It tries to 18485 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18486 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18487 * are handled by tcp_wput() as usual. 18488 * 18489 * All further messages will also be handled by tcp_wput() because we cannot 18490 * be sure that the above short cut is safe later. 18491 */ 18492 static void 18493 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18494 { 18495 conn_t *connp = Q_TO_CONN(wq); 18496 tcp_t *tcp = connp->conn_tcp; 18497 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18498 18499 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18500 wq->q_qinfo = &tcp_winit; 18501 18502 ASSERT(IPCL_IS_TCP(connp)); 18503 ASSERT(TCP_IS_SOCKET(tcp)); 18504 18505 if (DB_TYPE(mp) == M_PCPROTO && 18506 MBLKL(mp) == sizeof (struct T_capability_req) && 18507 car->PRIM_type == T_CAPABILITY_REQ) { 18508 tcp_capability_req(tcp, mp); 18509 return; 18510 } 18511 18512 tcp_wput(wq, mp); 18513 } 18514 18515 /* ARGSUSED */ 18516 static void 18517 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18518 { 18519 #ifdef DEBUG 18520 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18521 #endif 18522 freemsg(mp); 18523 } 18524 18525 static boolean_t 18526 tcp_zcopy_check(tcp_t *tcp) 18527 { 18528 conn_t *connp = tcp->tcp_connp; 18529 ire_t *ire; 18530 boolean_t zc_enabled = B_FALSE; 18531 tcp_stack_t *tcps = tcp->tcp_tcps; 18532 18533 if (do_tcpzcopy == 2) 18534 zc_enabled = B_TRUE; 18535 else if (tcp->tcp_ipversion == IPV4_VERSION && 18536 IPCL_IS_CONNECTED(connp) && 18537 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18538 connp->conn_dontroute == 0 && 18539 !connp->conn_nexthop_set && 18540 connp->conn_outgoing_ill == NULL && 18541 do_tcpzcopy == 1) { 18542 /* 18543 * the checks above closely resemble the fast path checks 18544 * in tcp_send_data(). 18545 */ 18546 mutex_enter(&connp->conn_lock); 18547 ire = connp->conn_ire_cache; 18548 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18549 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18550 IRE_REFHOLD(ire); 18551 if (ire->ire_stq != NULL) { 18552 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18553 18554 zc_enabled = ill && (ill->ill_capabilities & 18555 ILL_CAPAB_ZEROCOPY) && 18556 (ill->ill_zerocopy_capab-> 18557 ill_zerocopy_flags != 0); 18558 } 18559 IRE_REFRELE(ire); 18560 } 18561 mutex_exit(&connp->conn_lock); 18562 } 18563 tcp->tcp_snd_zcopy_on = zc_enabled; 18564 if (!TCP_IS_DETACHED(tcp)) { 18565 if (zc_enabled) { 18566 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18567 ZCVMSAFE); 18568 TCP_STAT(tcps, tcp_zcopy_on); 18569 } else { 18570 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18571 ZCVMUNSAFE); 18572 TCP_STAT(tcps, tcp_zcopy_off); 18573 } 18574 } 18575 return (zc_enabled); 18576 } 18577 18578 static mblk_t * 18579 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18580 { 18581 tcp_stack_t *tcps = tcp->tcp_tcps; 18582 18583 if (do_tcpzcopy == 2) 18584 return (bp); 18585 else if (tcp->tcp_snd_zcopy_on) { 18586 tcp->tcp_snd_zcopy_on = B_FALSE; 18587 if (!TCP_IS_DETACHED(tcp)) { 18588 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18589 ZCVMUNSAFE); 18590 TCP_STAT(tcps, tcp_zcopy_disable); 18591 } 18592 } 18593 return (tcp_zcopy_backoff(tcp, bp, 0)); 18594 } 18595 18596 /* 18597 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18598 * the original desballoca'ed segmapped mblk. 18599 */ 18600 static mblk_t * 18601 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18602 { 18603 mblk_t *head, *tail, *nbp; 18604 tcp_stack_t *tcps = tcp->tcp_tcps; 18605 18606 if (IS_VMLOANED_MBLK(bp)) { 18607 TCP_STAT(tcps, tcp_zcopy_backoff); 18608 if ((head = copyb(bp)) == NULL) { 18609 /* fail to backoff; leave it for the next backoff */ 18610 tcp->tcp_xmit_zc_clean = B_FALSE; 18611 return (bp); 18612 } 18613 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18614 if (fix_xmitlist) 18615 tcp_zcopy_notify(tcp); 18616 else 18617 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18618 } 18619 nbp = bp->b_cont; 18620 if (fix_xmitlist) { 18621 head->b_prev = bp->b_prev; 18622 head->b_next = bp->b_next; 18623 if (tcp->tcp_xmit_tail == bp) 18624 tcp->tcp_xmit_tail = head; 18625 } 18626 bp->b_next = NULL; 18627 bp->b_prev = NULL; 18628 freeb(bp); 18629 } else { 18630 head = bp; 18631 nbp = bp->b_cont; 18632 } 18633 tail = head; 18634 while (nbp) { 18635 if (IS_VMLOANED_MBLK(nbp)) { 18636 TCP_STAT(tcps, tcp_zcopy_backoff); 18637 if ((tail->b_cont = copyb(nbp)) == NULL) { 18638 tcp->tcp_xmit_zc_clean = B_FALSE; 18639 tail->b_cont = nbp; 18640 return (head); 18641 } 18642 tail = tail->b_cont; 18643 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18644 if (fix_xmitlist) 18645 tcp_zcopy_notify(tcp); 18646 else 18647 tail->b_datap->db_struioflag |= 18648 STRUIO_ZCNOTIFY; 18649 } 18650 bp = nbp; 18651 nbp = nbp->b_cont; 18652 if (fix_xmitlist) { 18653 tail->b_prev = bp->b_prev; 18654 tail->b_next = bp->b_next; 18655 if (tcp->tcp_xmit_tail == bp) 18656 tcp->tcp_xmit_tail = tail; 18657 } 18658 bp->b_next = NULL; 18659 bp->b_prev = NULL; 18660 freeb(bp); 18661 } else { 18662 tail->b_cont = nbp; 18663 tail = nbp; 18664 nbp = nbp->b_cont; 18665 } 18666 } 18667 if (fix_xmitlist) { 18668 tcp->tcp_xmit_last = tail; 18669 tcp->tcp_xmit_zc_clean = B_TRUE; 18670 } 18671 return (head); 18672 } 18673 18674 static void 18675 tcp_zcopy_notify(tcp_t *tcp) 18676 { 18677 struct stdata *stp; 18678 conn_t *connp; 18679 18680 if (tcp->tcp_detached) 18681 return; 18682 connp = tcp->tcp_connp; 18683 if (IPCL_IS_NONSTR(connp)) { 18684 (*connp->conn_upcalls->su_zcopy_notify) 18685 (connp->conn_upper_handle); 18686 return; 18687 } 18688 stp = STREAM(tcp->tcp_rq); 18689 mutex_enter(&stp->sd_lock); 18690 stp->sd_flag |= STZCNOTIFY; 18691 cv_broadcast(&stp->sd_zcopy_wait); 18692 mutex_exit(&stp->sd_lock); 18693 } 18694 18695 static boolean_t 18696 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18697 { 18698 ire_t *ire; 18699 conn_t *connp = tcp->tcp_connp; 18700 tcp_stack_t *tcps = tcp->tcp_tcps; 18701 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18702 18703 mutex_enter(&connp->conn_lock); 18704 ire = connp->conn_ire_cache; 18705 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18706 18707 if ((ire != NULL) && 18708 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18709 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18710 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18711 IRE_REFHOLD(ire); 18712 mutex_exit(&connp->conn_lock); 18713 } else { 18714 boolean_t cached = B_FALSE; 18715 ts_label_t *tsl; 18716 18717 /* force a recheck later on */ 18718 tcp->tcp_ire_ill_check_done = B_FALSE; 18719 18720 TCP_DBGSTAT(tcps, tcp_ire_null1); 18721 connp->conn_ire_cache = NULL; 18722 mutex_exit(&connp->conn_lock); 18723 18724 if (ire != NULL) 18725 IRE_REFRELE_NOTR(ire); 18726 18727 tsl = crgetlabel(CONN_CRED(connp)); 18728 ire = (dst ? 18729 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18730 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18731 connp->conn_zoneid, tsl, ipst)); 18732 18733 if (ire == NULL) { 18734 TCP_STAT(tcps, tcp_ire_null); 18735 return (B_FALSE); 18736 } 18737 18738 IRE_REFHOLD_NOTR(ire); 18739 18740 mutex_enter(&connp->conn_lock); 18741 if (CONN_CACHE_IRE(connp)) { 18742 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18743 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18744 TCP_CHECK_IREINFO(tcp, ire); 18745 connp->conn_ire_cache = ire; 18746 cached = B_TRUE; 18747 } 18748 rw_exit(&ire->ire_bucket->irb_lock); 18749 } 18750 mutex_exit(&connp->conn_lock); 18751 18752 /* 18753 * We can continue to use the ire but since it was 18754 * not cached, we should drop the extra reference. 18755 */ 18756 if (!cached) 18757 IRE_REFRELE_NOTR(ire); 18758 18759 /* 18760 * Rampart note: no need to select a new label here, since 18761 * labels are not allowed to change during the life of a TCP 18762 * connection. 18763 */ 18764 } 18765 18766 *irep = ire; 18767 18768 return (B_TRUE); 18769 } 18770 18771 /* 18772 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18773 * 18774 * 0 = success; 18775 * 1 = failed to find ire and ill. 18776 */ 18777 static boolean_t 18778 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18779 { 18780 ipha_t *ipha; 18781 ipaddr_t dst; 18782 ire_t *ire; 18783 ill_t *ill; 18784 mblk_t *ire_fp_mp; 18785 tcp_stack_t *tcps = tcp->tcp_tcps; 18786 18787 if (mp != NULL) 18788 ipha = (ipha_t *)mp->b_rptr; 18789 else 18790 ipha = tcp->tcp_ipha; 18791 dst = ipha->ipha_dst; 18792 18793 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18794 return (B_FALSE); 18795 18796 if ((ire->ire_flags & RTF_MULTIRT) || 18797 (ire->ire_stq == NULL) || 18798 (ire->ire_nce == NULL) || 18799 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18800 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18801 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18802 TCP_STAT(tcps, tcp_ip_ire_send); 18803 IRE_REFRELE(ire); 18804 return (B_FALSE); 18805 } 18806 18807 ill = ire_to_ill(ire); 18808 ASSERT(ill != NULL); 18809 18810 if (!tcp->tcp_ire_ill_check_done) { 18811 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18812 tcp->tcp_ire_ill_check_done = B_TRUE; 18813 } 18814 18815 *irep = ire; 18816 *illp = ill; 18817 18818 return (B_TRUE); 18819 } 18820 18821 static void 18822 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18823 { 18824 ipha_t *ipha; 18825 ipaddr_t src; 18826 ipaddr_t dst; 18827 uint32_t cksum; 18828 ire_t *ire; 18829 uint16_t *up; 18830 ill_t *ill; 18831 conn_t *connp = tcp->tcp_connp; 18832 uint32_t hcksum_txflags = 0; 18833 mblk_t *ire_fp_mp; 18834 uint_t ire_fp_mp_len; 18835 tcp_stack_t *tcps = tcp->tcp_tcps; 18836 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18837 cred_t *cr; 18838 pid_t cpid; 18839 18840 ASSERT(DB_TYPE(mp) == M_DATA); 18841 18842 /* 18843 * Here we need to handle the overloading of the cred_t for 18844 * both getpeerucred and TX. 18845 * If this is a SYN then the caller already set db_credp so 18846 * that getpeerucred will work. But if TX is in use we might have 18847 * a conn_peercred which is different, and we need to use that cred 18848 * to make TX use the correct label and label dependent route. 18849 */ 18850 if (is_system_labeled()) { 18851 cr = msg_getcred(mp, &cpid); 18852 if (cr == NULL || connp->conn_peercred != NULL) 18853 mblk_setcred(mp, CONN_CRED(connp), cpid); 18854 } 18855 18856 ipha = (ipha_t *)mp->b_rptr; 18857 src = ipha->ipha_src; 18858 dst = ipha->ipha_dst; 18859 18860 ASSERT(q != NULL); 18861 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18862 18863 /* 18864 * Drop off fast path for IPv6 and also if options are present or 18865 * we need to resolve a TS label. 18866 */ 18867 if (tcp->tcp_ipversion != IPV4_VERSION || 18868 !IPCL_IS_CONNECTED(connp) || 18869 !CONN_IS_LSO_MD_FASTPATH(connp) || 18870 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18871 !connp->conn_ulp_labeled || 18872 ipha->ipha_ident == IP_HDR_INCLUDED || 18873 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18874 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18875 if (tcp->tcp_snd_zcopy_aware) 18876 mp = tcp_zcopy_disable(tcp, mp); 18877 TCP_STAT(tcps, tcp_ip_send); 18878 CALL_IP_WPUT(connp, q, mp); 18879 return; 18880 } 18881 18882 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18883 if (tcp->tcp_snd_zcopy_aware) 18884 mp = tcp_zcopy_backoff(tcp, mp, 0); 18885 CALL_IP_WPUT(connp, q, mp); 18886 return; 18887 } 18888 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18889 ire_fp_mp_len = MBLKL(ire_fp_mp); 18890 18891 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18892 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18893 #ifndef _BIG_ENDIAN 18894 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18895 #endif 18896 18897 /* 18898 * Check to see if we need to re-enable LSO/MDT for this connection 18899 * because it was previously disabled due to changes in the ill; 18900 * note that by doing it here, this re-enabling only applies when 18901 * the packet is not dispatched through CALL_IP_WPUT(). 18902 * 18903 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18904 * case, since that's how we ended up here. For IPv6, we do the 18905 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18906 */ 18907 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18908 /* 18909 * Restore LSO for this connection, so that next time around 18910 * it is eligible to go through tcp_lsosend() path again. 18911 */ 18912 TCP_STAT(tcps, tcp_lso_enabled); 18913 tcp->tcp_lso = B_TRUE; 18914 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18915 "interface %s\n", (void *)connp, ill->ill_name)); 18916 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18917 /* 18918 * Restore MDT for this connection, so that next time around 18919 * it is eligible to go through tcp_multisend() path again. 18920 */ 18921 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18922 tcp->tcp_mdt = B_TRUE; 18923 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18924 "interface %s\n", (void *)connp, ill->ill_name)); 18925 } 18926 18927 if (tcp->tcp_snd_zcopy_aware) { 18928 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18929 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18930 mp = tcp_zcopy_disable(tcp, mp); 18931 /* 18932 * we shouldn't need to reset ipha as the mp containing 18933 * ipha should never be a zero-copy mp. 18934 */ 18935 } 18936 18937 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18938 ASSERT(ill->ill_hcksum_capab != NULL); 18939 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18940 } 18941 18942 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18943 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18944 18945 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18946 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18947 18948 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18949 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18950 18951 /* Software checksum? */ 18952 if (DB_CKSUMFLAGS(mp) == 0) { 18953 TCP_STAT(tcps, tcp_out_sw_cksum); 18954 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18955 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18956 } 18957 18958 /* Calculate IP header checksum if hardware isn't capable */ 18959 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18960 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18961 ((uint16_t *)ipha)[4]); 18962 } 18963 18964 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18965 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18966 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18967 18968 UPDATE_OB_PKT_COUNT(ire); 18969 ire->ire_last_used_time = lbolt; 18970 18971 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18972 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18973 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18974 ntohs(ipha->ipha_length)); 18975 18976 DTRACE_PROBE4(ip4__physical__out__start, 18977 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18978 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18979 ipst->ips_ipv4firewall_physical_out, 18980 NULL, ill, ipha, mp, mp, 0, ipst); 18981 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18982 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18983 18984 if (mp != NULL) { 18985 if (ipst->ips_ipobs_enabled) { 18986 zoneid_t szone; 18987 18988 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18989 ipst, ALL_ZONES); 18990 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18991 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18992 } 18993 18994 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18995 } 18996 18997 IRE_REFRELE(ire); 18998 } 18999 19000 /* 19001 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19002 * if the receiver shrinks the window, i.e. moves the right window to the 19003 * left, the we should not send new data, but should retransmit normally the 19004 * old unacked data between suna and suna + swnd. We might has sent data 19005 * that is now outside the new window, pretend that we didn't send it. 19006 */ 19007 static void 19008 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19009 { 19010 uint32_t snxt = tcp->tcp_snxt; 19011 mblk_t *xmit_tail; 19012 int32_t offset; 19013 19014 ASSERT(shrunk_count > 0); 19015 19016 /* Pretend we didn't send the data outside the window */ 19017 snxt -= shrunk_count; 19018 19019 /* Get the mblk and the offset in it per the shrunk window */ 19020 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19021 19022 ASSERT(xmit_tail != NULL); 19023 19024 /* Reset all the values per the now shrunk window */ 19025 tcp->tcp_snxt = snxt; 19026 tcp->tcp_xmit_tail = xmit_tail; 19027 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19028 offset; 19029 tcp->tcp_unsent += shrunk_count; 19030 19031 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19032 /* 19033 * Make sure the timer is running so that we will probe a zero 19034 * window. 19035 */ 19036 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19037 } 19038 19039 19040 /* 19041 * The TCP normal data output path. 19042 * NOTE: the logic of the fast path is duplicated from this function. 19043 */ 19044 static void 19045 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19046 { 19047 int len; 19048 mblk_t *local_time; 19049 mblk_t *mp1; 19050 uint32_t snxt; 19051 int tail_unsent; 19052 int tcpstate; 19053 int usable = 0; 19054 mblk_t *xmit_tail; 19055 queue_t *q = tcp->tcp_wq; 19056 int32_t mss; 19057 int32_t num_sack_blk = 0; 19058 int32_t tcp_hdr_len; 19059 int32_t tcp_tcp_hdr_len; 19060 int mdt_thres; 19061 int rc; 19062 tcp_stack_t *tcps = tcp->tcp_tcps; 19063 ip_stack_t *ipst; 19064 19065 tcpstate = tcp->tcp_state; 19066 if (mp == NULL) { 19067 /* 19068 * tcp_wput_data() with NULL mp should only be called when 19069 * there is unsent data. 19070 */ 19071 ASSERT(tcp->tcp_unsent > 0); 19072 /* Really tacky... but we need this for detached closes. */ 19073 len = tcp->tcp_unsent; 19074 goto data_null; 19075 } 19076 19077 #if CCS_STATS 19078 wrw_stats.tot.count++; 19079 wrw_stats.tot.bytes += msgdsize(mp); 19080 #endif 19081 ASSERT(mp->b_datap->db_type == M_DATA); 19082 /* 19083 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19084 * or before a connection attempt has begun. 19085 */ 19086 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19087 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19088 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19089 #ifdef DEBUG 19090 cmn_err(CE_WARN, 19091 "tcp_wput_data: data after ordrel, %s", 19092 tcp_display(tcp, NULL, 19093 DISP_ADDR_AND_PORT)); 19094 #else 19095 if (tcp->tcp_debug) { 19096 (void) strlog(TCP_MOD_ID, 0, 1, 19097 SL_TRACE|SL_ERROR, 19098 "tcp_wput_data: data after ordrel, %s\n", 19099 tcp_display(tcp, NULL, 19100 DISP_ADDR_AND_PORT)); 19101 } 19102 #endif /* DEBUG */ 19103 } 19104 if (tcp->tcp_snd_zcopy_aware && 19105 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19106 tcp_zcopy_notify(tcp); 19107 freemsg(mp); 19108 mutex_enter(&tcp->tcp_non_sq_lock); 19109 if (tcp->tcp_flow_stopped && 19110 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19111 tcp_clrqfull(tcp); 19112 } 19113 mutex_exit(&tcp->tcp_non_sq_lock); 19114 return; 19115 } 19116 19117 /* Strip empties */ 19118 for (;;) { 19119 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19120 (uintptr_t)INT_MAX); 19121 len = (int)(mp->b_wptr - mp->b_rptr); 19122 if (len > 0) 19123 break; 19124 mp1 = mp; 19125 mp = mp->b_cont; 19126 freeb(mp1); 19127 if (!mp) { 19128 return; 19129 } 19130 } 19131 19132 /* If we are the first on the list ... */ 19133 if (tcp->tcp_xmit_head == NULL) { 19134 tcp->tcp_xmit_head = mp; 19135 tcp->tcp_xmit_tail = mp; 19136 tcp->tcp_xmit_tail_unsent = len; 19137 } else { 19138 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19139 struct datab *dp; 19140 19141 mp1 = tcp->tcp_xmit_last; 19142 if (len < tcp_tx_pull_len && 19143 (dp = mp1->b_datap)->db_ref == 1 && 19144 dp->db_lim - mp1->b_wptr >= len) { 19145 ASSERT(len > 0); 19146 ASSERT(!mp1->b_cont); 19147 if (len == 1) { 19148 *mp1->b_wptr++ = *mp->b_rptr; 19149 } else { 19150 bcopy(mp->b_rptr, mp1->b_wptr, len); 19151 mp1->b_wptr += len; 19152 } 19153 if (mp1 == tcp->tcp_xmit_tail) 19154 tcp->tcp_xmit_tail_unsent += len; 19155 mp1->b_cont = mp->b_cont; 19156 if (tcp->tcp_snd_zcopy_aware && 19157 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19158 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19159 freeb(mp); 19160 mp = mp1; 19161 } else { 19162 tcp->tcp_xmit_last->b_cont = mp; 19163 } 19164 len += tcp->tcp_unsent; 19165 } 19166 19167 /* Tack on however many more positive length mblks we have */ 19168 if ((mp1 = mp->b_cont) != NULL) { 19169 do { 19170 int tlen; 19171 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19172 (uintptr_t)INT_MAX); 19173 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19174 if (tlen <= 0) { 19175 mp->b_cont = mp1->b_cont; 19176 freeb(mp1); 19177 } else { 19178 len += tlen; 19179 mp = mp1; 19180 } 19181 } while ((mp1 = mp->b_cont) != NULL); 19182 } 19183 tcp->tcp_xmit_last = mp; 19184 tcp->tcp_unsent = len; 19185 19186 if (urgent) 19187 usable = 1; 19188 19189 data_null: 19190 snxt = tcp->tcp_snxt; 19191 xmit_tail = tcp->tcp_xmit_tail; 19192 tail_unsent = tcp->tcp_xmit_tail_unsent; 19193 19194 /* 19195 * Note that tcp_mss has been adjusted to take into account the 19196 * timestamp option if applicable. Because SACK options do not 19197 * appear in every TCP segments and they are of variable lengths, 19198 * they cannot be included in tcp_mss. Thus we need to calculate 19199 * the actual segment length when we need to send a segment which 19200 * includes SACK options. 19201 */ 19202 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19203 int32_t opt_len; 19204 19205 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19206 tcp->tcp_num_sack_blk); 19207 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19208 2 + TCPOPT_HEADER_LEN; 19209 mss = tcp->tcp_mss - opt_len; 19210 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19211 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19212 } else { 19213 mss = tcp->tcp_mss; 19214 tcp_hdr_len = tcp->tcp_hdr_len; 19215 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19216 } 19217 19218 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19219 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19220 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19221 } 19222 if (tcpstate == TCPS_SYN_RCVD) { 19223 /* 19224 * The three-way connection establishment handshake is not 19225 * complete yet. We want to queue the data for transmission 19226 * after entering ESTABLISHED state (RFC793). A jump to 19227 * "done" label effectively leaves data on the queue. 19228 */ 19229 goto done; 19230 } else { 19231 int usable_r; 19232 19233 /* 19234 * In the special case when cwnd is zero, which can only 19235 * happen if the connection is ECN capable, return now. 19236 * New segments is sent using tcp_timer(). The timer 19237 * is set in tcp_rput_data(). 19238 */ 19239 if (tcp->tcp_cwnd == 0) { 19240 /* 19241 * Note that tcp_cwnd is 0 before 3-way handshake is 19242 * finished. 19243 */ 19244 ASSERT(tcp->tcp_ecn_ok || 19245 tcp->tcp_state < TCPS_ESTABLISHED); 19246 return; 19247 } 19248 19249 /* NOTE: trouble if xmitting while SYN not acked? */ 19250 usable_r = snxt - tcp->tcp_suna; 19251 usable_r = tcp->tcp_swnd - usable_r; 19252 19253 /* 19254 * Check if the receiver has shrunk the window. If 19255 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19256 * cannot be set as there is unsent data, so FIN cannot 19257 * be sent out. Otherwise, we need to take into account 19258 * of FIN as it consumes an "invisible" sequence number. 19259 */ 19260 ASSERT(tcp->tcp_fin_sent == 0); 19261 if (usable_r < 0) { 19262 /* 19263 * The receiver has shrunk the window and we have sent 19264 * -usable_r date beyond the window, re-adjust. 19265 * 19266 * If TCP window scaling is enabled, there can be 19267 * round down error as the advertised receive window 19268 * is actually right shifted n bits. This means that 19269 * the lower n bits info is wiped out. It will look 19270 * like the window is shrunk. Do a check here to 19271 * see if the shrunk amount is actually within the 19272 * error in window calculation. If it is, just 19273 * return. Note that this check is inside the 19274 * shrunk window check. This makes sure that even 19275 * though tcp_process_shrunk_swnd() is not called, 19276 * we will stop further processing. 19277 */ 19278 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19279 tcp_process_shrunk_swnd(tcp, -usable_r); 19280 } 19281 return; 19282 } 19283 19284 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19285 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19286 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19287 19288 /* usable = MIN(usable, unsent) */ 19289 if (usable_r > len) 19290 usable_r = len; 19291 19292 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19293 if (usable_r > 0) { 19294 usable = usable_r; 19295 } else { 19296 /* Bypass all other unnecessary processing. */ 19297 goto done; 19298 } 19299 } 19300 19301 local_time = (mblk_t *)lbolt; 19302 19303 /* 19304 * "Our" Nagle Algorithm. This is not the same as in the old 19305 * BSD. This is more in line with the true intent of Nagle. 19306 * 19307 * The conditions are: 19308 * 1. The amount of unsent data (or amount of data which can be 19309 * sent, whichever is smaller) is less than Nagle limit. 19310 * 2. The last sent size is also less than Nagle limit. 19311 * 3. There is unack'ed data. 19312 * 4. Urgent pointer is not set. Send urgent data ignoring the 19313 * Nagle algorithm. This reduces the probability that urgent 19314 * bytes get "merged" together. 19315 * 5. The app has not closed the connection. This eliminates the 19316 * wait time of the receiving side waiting for the last piece of 19317 * (small) data. 19318 * 19319 * If all are satisified, exit without sending anything. Note 19320 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19321 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19322 * 4095). 19323 */ 19324 if (usable < (int)tcp->tcp_naglim && 19325 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19326 snxt != tcp->tcp_suna && 19327 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19328 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19329 goto done; 19330 } 19331 19332 if (tcp->tcp_cork) { 19333 /* 19334 * if the tcp->tcp_cork option is set, then we have to force 19335 * TCP not to send partial segment (smaller than MSS bytes). 19336 * We are calculating the usable now based on full mss and 19337 * will save the rest of remaining data for later. 19338 */ 19339 if (usable < mss) 19340 goto done; 19341 usable = (usable / mss) * mss; 19342 } 19343 19344 /* Update the latest receive window size in TCP header. */ 19345 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19346 tcp->tcp_tcph->th_win); 19347 19348 /* 19349 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19350 * 19351 * 1. Simple TCP/IP{v4,v6} (no options). 19352 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19353 * 3. If the TCP connection is in ESTABLISHED state. 19354 * 4. The TCP is not detached. 19355 * 19356 * If any of the above conditions have changed during the 19357 * connection, stop using LSO/MDT and restore the stream head 19358 * parameters accordingly. 19359 */ 19360 ipst = tcps->tcps_netstack->netstack_ip; 19361 19362 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19363 ((tcp->tcp_ipversion == IPV4_VERSION && 19364 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19365 (tcp->tcp_ipversion == IPV6_VERSION && 19366 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19367 tcp->tcp_state != TCPS_ESTABLISHED || 19368 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19369 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19370 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19371 if (tcp->tcp_lso) { 19372 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19373 tcp->tcp_lso = B_FALSE; 19374 } else { 19375 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19376 tcp->tcp_mdt = B_FALSE; 19377 } 19378 19379 /* Anything other than detached is considered pathological */ 19380 if (!TCP_IS_DETACHED(tcp)) { 19381 if (tcp->tcp_lso) 19382 TCP_STAT(tcps, tcp_lso_disabled); 19383 else 19384 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19385 (void) tcp_maxpsz_set(tcp, B_TRUE); 19386 } 19387 } 19388 19389 /* Use MDT if sendable amount is greater than the threshold */ 19390 if (tcp->tcp_mdt && 19391 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19392 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19393 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19394 (tcp->tcp_valid_bits == 0 || 19395 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19396 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19397 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19398 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19399 local_time, mdt_thres); 19400 } else { 19401 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19402 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19403 local_time, INT_MAX); 19404 } 19405 19406 /* Pretend that all we were trying to send really got sent */ 19407 if (rc < 0 && tail_unsent < 0) { 19408 do { 19409 xmit_tail = xmit_tail->b_cont; 19410 xmit_tail->b_prev = local_time; 19411 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19412 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19413 tail_unsent += (int)(xmit_tail->b_wptr - 19414 xmit_tail->b_rptr); 19415 } while (tail_unsent < 0); 19416 } 19417 done:; 19418 tcp->tcp_xmit_tail = xmit_tail; 19419 tcp->tcp_xmit_tail_unsent = tail_unsent; 19420 len = tcp->tcp_snxt - snxt; 19421 if (len) { 19422 /* 19423 * If new data was sent, need to update the notsack 19424 * list, which is, afterall, data blocks that have 19425 * not been sack'ed by the receiver. New data is 19426 * not sack'ed. 19427 */ 19428 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19429 /* len is a negative value. */ 19430 tcp->tcp_pipe -= len; 19431 tcp_notsack_update(&(tcp->tcp_notsack_list), 19432 tcp->tcp_snxt, snxt, 19433 &(tcp->tcp_num_notsack_blk), 19434 &(tcp->tcp_cnt_notsack_list)); 19435 } 19436 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19437 tcp->tcp_rack = tcp->tcp_rnxt; 19438 tcp->tcp_rack_cnt = 0; 19439 if ((snxt + len) == tcp->tcp_suna) { 19440 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19441 } 19442 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19443 /* 19444 * Didn't send anything. Make sure the timer is running 19445 * so that we will probe a zero window. 19446 */ 19447 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19448 } 19449 /* Note that len is the amount we just sent but with a negative sign */ 19450 tcp->tcp_unsent += len; 19451 mutex_enter(&tcp->tcp_non_sq_lock); 19452 if (tcp->tcp_flow_stopped) { 19453 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19454 tcp_clrqfull(tcp); 19455 } 19456 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19457 tcp_setqfull(tcp); 19458 } 19459 mutex_exit(&tcp->tcp_non_sq_lock); 19460 } 19461 19462 /* 19463 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19464 * outgoing TCP header with the template header, as well as other 19465 * options such as time-stamp, ECN and/or SACK. 19466 */ 19467 static void 19468 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19469 { 19470 tcph_t *tcp_tmpl, *tcp_h; 19471 uint32_t *dst, *src; 19472 int hdrlen; 19473 19474 ASSERT(OK_32PTR(rptr)); 19475 19476 /* Template header */ 19477 tcp_tmpl = tcp->tcp_tcph; 19478 19479 /* Header of outgoing packet */ 19480 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19481 19482 /* dst and src are opaque 32-bit fields, used for copying */ 19483 dst = (uint32_t *)rptr; 19484 src = (uint32_t *)tcp->tcp_iphc; 19485 hdrlen = tcp->tcp_hdr_len; 19486 19487 /* Fill time-stamp option if needed */ 19488 if (tcp->tcp_snd_ts_ok) { 19489 U32_TO_BE32((uint32_t)now, 19490 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19491 U32_TO_BE32(tcp->tcp_ts_recent, 19492 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19493 } else { 19494 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19495 } 19496 19497 /* 19498 * Copy the template header; is this really more efficient than 19499 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19500 * but perhaps not for other scenarios. 19501 */ 19502 dst[0] = src[0]; 19503 dst[1] = src[1]; 19504 dst[2] = src[2]; 19505 dst[3] = src[3]; 19506 dst[4] = src[4]; 19507 dst[5] = src[5]; 19508 dst[6] = src[6]; 19509 dst[7] = src[7]; 19510 dst[8] = src[8]; 19511 dst[9] = src[9]; 19512 if (hdrlen -= 40) { 19513 hdrlen >>= 2; 19514 dst += 10; 19515 src += 10; 19516 do { 19517 *dst++ = *src++; 19518 } while (--hdrlen); 19519 } 19520 19521 /* 19522 * Set the ECN info in the TCP header if it is not a zero 19523 * window probe. Zero window probe is only sent in 19524 * tcp_wput_data() and tcp_timer(). 19525 */ 19526 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19527 SET_ECT(tcp, rptr); 19528 19529 if (tcp->tcp_ecn_echo_on) 19530 tcp_h->th_flags[0] |= TH_ECE; 19531 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19532 tcp_h->th_flags[0] |= TH_CWR; 19533 tcp->tcp_ecn_cwr_sent = B_TRUE; 19534 } 19535 } 19536 19537 /* Fill in SACK options */ 19538 if (num_sack_blk > 0) { 19539 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19540 sack_blk_t *tmp; 19541 int32_t i; 19542 19543 wptr[0] = TCPOPT_NOP; 19544 wptr[1] = TCPOPT_NOP; 19545 wptr[2] = TCPOPT_SACK; 19546 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19547 sizeof (sack_blk_t); 19548 wptr += TCPOPT_REAL_SACK_LEN; 19549 19550 tmp = tcp->tcp_sack_list; 19551 for (i = 0; i < num_sack_blk; i++) { 19552 U32_TO_BE32(tmp[i].begin, wptr); 19553 wptr += sizeof (tcp_seq); 19554 U32_TO_BE32(tmp[i].end, wptr); 19555 wptr += sizeof (tcp_seq); 19556 } 19557 tcp_h->th_offset_and_rsrvd[0] += 19558 ((num_sack_blk * 2 + 1) << 4); 19559 } 19560 } 19561 19562 /* 19563 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19564 * the destination address and SAP attribute, and if necessary, the 19565 * hardware checksum offload attribute to a Multidata message. 19566 */ 19567 static int 19568 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19569 const uint32_t start, const uint32_t stuff, const uint32_t end, 19570 const uint32_t flags, tcp_stack_t *tcps) 19571 { 19572 /* Add global destination address & SAP attribute */ 19573 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19574 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19575 "destination address+SAP\n")); 19576 19577 if (dlmp != NULL) 19578 TCP_STAT(tcps, tcp_mdt_allocfail); 19579 return (-1); 19580 } 19581 19582 /* Add global hwcksum attribute */ 19583 if (hwcksum && 19584 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19585 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19586 "checksum attribute\n")); 19587 19588 TCP_STAT(tcps, tcp_mdt_allocfail); 19589 return (-1); 19590 } 19591 19592 return (0); 19593 } 19594 19595 /* 19596 * Smaller and private version of pdescinfo_t used specifically for TCP, 19597 * which allows for only two payload spans per packet. 19598 */ 19599 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19600 19601 /* 19602 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19603 * scheme, and returns one the following: 19604 * 19605 * -1 = failed allocation. 19606 * 0 = success; burst count reached, or usable send window is too small, 19607 * and that we'd rather wait until later before sending again. 19608 */ 19609 static int 19610 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19611 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19612 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19613 const int mdt_thres) 19614 { 19615 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19616 multidata_t *mmd; 19617 uint_t obsegs, obbytes, hdr_frag_sz; 19618 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19619 int num_burst_seg, max_pld; 19620 pdesc_t *pkt; 19621 tcp_pdescinfo_t tcp_pkt_info; 19622 pdescinfo_t *pkt_info; 19623 int pbuf_idx, pbuf_idx_nxt; 19624 int seg_len, len, spill, af; 19625 boolean_t add_buffer, zcopy, clusterwide; 19626 boolean_t rconfirm = B_FALSE; 19627 boolean_t done = B_FALSE; 19628 uint32_t cksum; 19629 uint32_t hwcksum_flags; 19630 ire_t *ire = NULL; 19631 ill_t *ill; 19632 ipha_t *ipha; 19633 ip6_t *ip6h; 19634 ipaddr_t src, dst; 19635 ill_zerocopy_capab_t *zc_cap = NULL; 19636 uint16_t *up; 19637 int err; 19638 conn_t *connp; 19639 tcp_stack_t *tcps = tcp->tcp_tcps; 19640 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19641 int usable_mmd, tail_unsent_mmd; 19642 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19643 mblk_t *xmit_tail_mmd; 19644 netstackid_t stack_id; 19645 19646 #ifdef _BIG_ENDIAN 19647 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19648 #else 19649 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19650 #endif 19651 19652 #define PREP_NEW_MULTIDATA() { \ 19653 mmd = NULL; \ 19654 md_mp = md_hbuf = NULL; \ 19655 cur_hdr_off = 0; \ 19656 max_pld = tcp->tcp_mdt_max_pld; \ 19657 pbuf_idx = pbuf_idx_nxt = -1; \ 19658 add_buffer = B_TRUE; \ 19659 zcopy = B_FALSE; \ 19660 } 19661 19662 #define PREP_NEW_PBUF() { \ 19663 md_pbuf = md_pbuf_nxt = NULL; \ 19664 pbuf_idx = pbuf_idx_nxt = -1; \ 19665 cur_pld_off = 0; \ 19666 first_snxt = *snxt; \ 19667 ASSERT(*tail_unsent > 0); \ 19668 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19669 } 19670 19671 ASSERT(mdt_thres >= mss); 19672 ASSERT(*usable > 0 && *usable > mdt_thres); 19673 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19674 ASSERT(!TCP_IS_DETACHED(tcp)); 19675 ASSERT(tcp->tcp_valid_bits == 0 || 19676 tcp->tcp_valid_bits == TCP_FSS_VALID); 19677 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19678 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19679 (tcp->tcp_ipversion == IPV6_VERSION && 19680 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19681 19682 connp = tcp->tcp_connp; 19683 ASSERT(connp != NULL); 19684 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19685 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19686 19687 stack_id = connp->conn_netstack->netstack_stackid; 19688 19689 usable_mmd = tail_unsent_mmd = 0; 19690 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19691 xmit_tail_mmd = NULL; 19692 /* 19693 * Note that tcp will only declare at most 2 payload spans per 19694 * packet, which is much lower than the maximum allowable number 19695 * of packet spans per Multidata. For this reason, we use the 19696 * privately declared and smaller descriptor info structure, in 19697 * order to save some stack space. 19698 */ 19699 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19700 19701 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19702 if (af == AF_INET) { 19703 dst = tcp->tcp_ipha->ipha_dst; 19704 src = tcp->tcp_ipha->ipha_src; 19705 ASSERT(!CLASSD(dst)); 19706 } 19707 ASSERT(af == AF_INET || 19708 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19709 19710 obsegs = obbytes = 0; 19711 num_burst_seg = tcp->tcp_snd_burst; 19712 md_mp_head = NULL; 19713 PREP_NEW_MULTIDATA(); 19714 19715 /* 19716 * Before we go on further, make sure there is an IRE that we can 19717 * use, and that the ILL supports MDT. Otherwise, there's no point 19718 * in proceeding any further, and we should just hand everything 19719 * off to the legacy path. 19720 */ 19721 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19722 goto legacy_send_no_md; 19723 19724 ASSERT(ire != NULL); 19725 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19726 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19727 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19728 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19729 /* 19730 * If we do support loopback for MDT (which requires modifications 19731 * to the receiving paths), the following assertions should go away, 19732 * and we would be sending the Multidata to loopback conn later on. 19733 */ 19734 ASSERT(!IRE_IS_LOCAL(ire)); 19735 ASSERT(ire->ire_stq != NULL); 19736 19737 ill = ire_to_ill(ire); 19738 ASSERT(ill != NULL); 19739 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19740 19741 if (!tcp->tcp_ire_ill_check_done) { 19742 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19743 tcp->tcp_ire_ill_check_done = B_TRUE; 19744 } 19745 19746 /* 19747 * If the underlying interface conditions have changed, or if the 19748 * new interface does not support MDT, go back to legacy path. 19749 */ 19750 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19751 /* don't go through this path anymore for this connection */ 19752 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19753 tcp->tcp_mdt = B_FALSE; 19754 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19755 "interface %s\n", (void *)connp, ill->ill_name)); 19756 /* IRE will be released prior to returning */ 19757 goto legacy_send_no_md; 19758 } 19759 19760 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19761 zc_cap = ill->ill_zerocopy_capab; 19762 19763 /* 19764 * Check if we can take tcp fast-path. Note that "incomplete" 19765 * ire's (where the link-layer for next hop is not resolved 19766 * or where the fast-path header in nce_fp_mp is not available 19767 * yet) are sent down the legacy (slow) path. 19768 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19769 */ 19770 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19771 /* IRE will be released prior to returning */ 19772 goto legacy_send_no_md; 19773 } 19774 19775 /* go to legacy path if interface doesn't support zerocopy */ 19776 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19777 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19778 /* IRE will be released prior to returning */ 19779 goto legacy_send_no_md; 19780 } 19781 19782 /* does the interface support hardware checksum offload? */ 19783 hwcksum_flags = 0; 19784 if (ILL_HCKSUM_CAPABLE(ill) && 19785 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19786 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19787 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19788 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19789 HCKSUM_IPHDRCKSUM) 19790 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19791 19792 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19793 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19794 hwcksum_flags |= HCK_FULLCKSUM; 19795 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19796 HCKSUM_INET_PARTIAL) 19797 hwcksum_flags |= HCK_PARTIALCKSUM; 19798 } 19799 19800 /* 19801 * Each header fragment consists of the leading extra space, 19802 * followed by the TCP/IP header, and the trailing extra space. 19803 * We make sure that each header fragment begins on a 32-bit 19804 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19805 * aligned in tcp_mdt_update). 19806 */ 19807 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19808 tcp->tcp_mdt_hdr_tail), 4); 19809 19810 /* are we starting from the beginning of data block? */ 19811 if (*tail_unsent == 0) { 19812 *xmit_tail = (*xmit_tail)->b_cont; 19813 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19814 *tail_unsent = (int)MBLKL(*xmit_tail); 19815 } 19816 19817 /* 19818 * Here we create one or more Multidata messages, each made up of 19819 * one header buffer and up to N payload buffers. This entire 19820 * operation is done within two loops: 19821 * 19822 * The outer loop mostly deals with creating the Multidata message, 19823 * as well as the header buffer that gets added to it. It also 19824 * links the Multidata messages together such that all of them can 19825 * be sent down to the lower layer in a single putnext call; this 19826 * linking behavior depends on the tcp_mdt_chain tunable. 19827 * 19828 * The inner loop takes an existing Multidata message, and adds 19829 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19830 * packetizes those buffers by filling up the corresponding header 19831 * buffer fragments with the proper IP and TCP headers, and by 19832 * describing the layout of each packet in the packet descriptors 19833 * that get added to the Multidata. 19834 */ 19835 do { 19836 /* 19837 * If usable send window is too small, or data blocks in 19838 * transmit list are smaller than our threshold (i.e. app 19839 * performs large writes followed by small ones), we hand 19840 * off the control over to the legacy path. Note that we'll 19841 * get back the control once it encounters a large block. 19842 */ 19843 if (*usable < mss || (*tail_unsent <= mdt_thres && 19844 (*xmit_tail)->b_cont != NULL && 19845 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19846 /* send down what we've got so far */ 19847 if (md_mp_head != NULL) { 19848 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19849 obsegs, obbytes, &rconfirm); 19850 } 19851 /* 19852 * Pass control over to tcp_send(), but tell it to 19853 * return to us once a large-size transmission is 19854 * possible. 19855 */ 19856 TCP_STAT(tcps, tcp_mdt_legacy_small); 19857 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19858 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19859 tail_unsent, xmit_tail, local_time, 19860 mdt_thres)) <= 0) { 19861 /* burst count reached, or alloc failed */ 19862 IRE_REFRELE(ire); 19863 return (err); 19864 } 19865 19866 /* tcp_send() may have sent everything, so check */ 19867 if (*usable <= 0) { 19868 IRE_REFRELE(ire); 19869 return (0); 19870 } 19871 19872 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19873 /* 19874 * We may have delivered the Multidata, so make sure 19875 * to re-initialize before the next round. 19876 */ 19877 md_mp_head = NULL; 19878 obsegs = obbytes = 0; 19879 num_burst_seg = tcp->tcp_snd_burst; 19880 PREP_NEW_MULTIDATA(); 19881 19882 /* are we starting from the beginning of data block? */ 19883 if (*tail_unsent == 0) { 19884 *xmit_tail = (*xmit_tail)->b_cont; 19885 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19886 (uintptr_t)INT_MAX); 19887 *tail_unsent = (int)MBLKL(*xmit_tail); 19888 } 19889 } 19890 /* 19891 * Record current values for parameters we may need to pass 19892 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19893 * each iteration of the outer loop (each multidata message 19894 * creation). If we have a failure in the inner loop, we send 19895 * any complete multidata messages we have before reverting 19896 * to using the traditional non-md path. 19897 */ 19898 snxt_mmd = *snxt; 19899 usable_mmd = *usable; 19900 xmit_tail_mmd = *xmit_tail; 19901 tail_unsent_mmd = *tail_unsent; 19902 obsegs_mmd = obsegs; 19903 obbytes_mmd = obbytes; 19904 19905 /* 19906 * max_pld limits the number of mblks in tcp's transmit 19907 * queue that can be added to a Multidata message. Once 19908 * this counter reaches zero, no more additional mblks 19909 * can be added to it. What happens afterwards depends 19910 * on whether or not we are set to chain the Multidata 19911 * messages. If we are to link them together, reset 19912 * max_pld to its original value (tcp_mdt_max_pld) and 19913 * prepare to create a new Multidata message which will 19914 * get linked to md_mp_head. Else, leave it alone and 19915 * let the inner loop break on its own. 19916 */ 19917 if (tcp_mdt_chain && max_pld == 0) 19918 PREP_NEW_MULTIDATA(); 19919 19920 /* adding a payload buffer; re-initialize values */ 19921 if (add_buffer) 19922 PREP_NEW_PBUF(); 19923 19924 /* 19925 * If we don't have a Multidata, either because we just 19926 * (re)entered this outer loop, or after we branched off 19927 * to tcp_send above, setup the Multidata and header 19928 * buffer to be used. 19929 */ 19930 if (md_mp == NULL) { 19931 int md_hbuflen; 19932 uint32_t start, stuff; 19933 19934 /* 19935 * Calculate Multidata header buffer size large enough 19936 * to hold all of the headers that can possibly be 19937 * sent at this moment. We'd rather over-estimate 19938 * the size than running out of space; this is okay 19939 * since this buffer is small anyway. 19940 */ 19941 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19942 19943 /* 19944 * Start and stuff offset for partial hardware 19945 * checksum offload; these are currently for IPv4. 19946 * For full checksum offload, they are set to zero. 19947 */ 19948 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19949 if (af == AF_INET) { 19950 start = IP_SIMPLE_HDR_LENGTH; 19951 stuff = IP_SIMPLE_HDR_LENGTH + 19952 TCP_CHECKSUM_OFFSET; 19953 } else { 19954 start = IPV6_HDR_LEN; 19955 stuff = IPV6_HDR_LEN + 19956 TCP_CHECKSUM_OFFSET; 19957 } 19958 } else { 19959 start = stuff = 0; 19960 } 19961 19962 /* 19963 * Create the header buffer, Multidata, as well as 19964 * any necessary attributes (destination address, 19965 * SAP and hardware checksum offload) that should 19966 * be associated with the Multidata message. 19967 */ 19968 ASSERT(cur_hdr_off == 0); 19969 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19970 ((md_hbuf->b_wptr += md_hbuflen), 19971 (mmd = mmd_alloc(md_hbuf, &md_mp, 19972 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19973 /* fastpath mblk */ 19974 ire->ire_nce->nce_res_mp, 19975 /* hardware checksum enabled */ 19976 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19977 /* hardware checksum offsets */ 19978 start, stuff, 0, 19979 /* hardware checksum flag */ 19980 hwcksum_flags, tcps) != 0)) { 19981 legacy_send: 19982 /* 19983 * We arrive here from a failure within the 19984 * inner (packetizer) loop or we fail one of 19985 * the conditionals above. We restore the 19986 * previously checkpointed values for: 19987 * xmit_tail 19988 * usable 19989 * tail_unsent 19990 * snxt 19991 * obbytes 19992 * obsegs 19993 * We should then be able to dispatch any 19994 * complete multidata before reverting to the 19995 * traditional path with consistent parameters 19996 * (the inner loop updates these as it 19997 * iterates). 19998 */ 19999 *xmit_tail = xmit_tail_mmd; 20000 *usable = usable_mmd; 20001 *tail_unsent = tail_unsent_mmd; 20002 *snxt = snxt_mmd; 20003 obbytes = obbytes_mmd; 20004 obsegs = obsegs_mmd; 20005 if (md_mp != NULL) { 20006 /* Unlink message from the chain */ 20007 if (md_mp_head != NULL) { 20008 err = (intptr_t)rmvb(md_mp_head, 20009 md_mp); 20010 /* 20011 * We can't assert that rmvb 20012 * did not return -1, since we 20013 * may get here before linkb 20014 * happens. We do, however, 20015 * check if we just removed the 20016 * only element in the list. 20017 */ 20018 if (err == 0) 20019 md_mp_head = NULL; 20020 } 20021 /* md_hbuf gets freed automatically */ 20022 TCP_STAT(tcps, tcp_mdt_discarded); 20023 freeb(md_mp); 20024 } else { 20025 /* Either allocb or mmd_alloc failed */ 20026 TCP_STAT(tcps, tcp_mdt_allocfail); 20027 if (md_hbuf != NULL) 20028 freeb(md_hbuf); 20029 } 20030 20031 /* send down what we've got so far */ 20032 if (md_mp_head != NULL) { 20033 tcp_multisend_data(tcp, ire, ill, 20034 md_mp_head, obsegs, obbytes, 20035 &rconfirm); 20036 } 20037 legacy_send_no_md: 20038 if (ire != NULL) 20039 IRE_REFRELE(ire); 20040 /* 20041 * Too bad; let the legacy path handle this. 20042 * We specify INT_MAX for the threshold, since 20043 * we gave up with the Multidata processings 20044 * and let the old path have it all. 20045 */ 20046 TCP_STAT(tcps, tcp_mdt_legacy_all); 20047 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20048 tcp_tcp_hdr_len, num_sack_blk, usable, 20049 snxt, tail_unsent, xmit_tail, local_time, 20050 INT_MAX)); 20051 } 20052 20053 /* link to any existing ones, if applicable */ 20054 TCP_STAT(tcps, tcp_mdt_allocd); 20055 if (md_mp_head == NULL) { 20056 md_mp_head = md_mp; 20057 } else if (tcp_mdt_chain) { 20058 TCP_STAT(tcps, tcp_mdt_linked); 20059 linkb(md_mp_head, md_mp); 20060 } 20061 } 20062 20063 ASSERT(md_mp_head != NULL); 20064 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20065 ASSERT(md_mp != NULL && mmd != NULL); 20066 ASSERT(md_hbuf != NULL); 20067 20068 /* 20069 * Packetize the transmittable portion of the data block; 20070 * each data block is essentially added to the Multidata 20071 * as a payload buffer. We also deal with adding more 20072 * than one payload buffers, which happens when the remaining 20073 * packetized portion of the current payload buffer is less 20074 * than MSS, while the next data block in transmit queue 20075 * has enough data to make up for one. This "spillover" 20076 * case essentially creates a split-packet, where portions 20077 * of the packet's payload fragments may span across two 20078 * virtually discontiguous address blocks. 20079 */ 20080 seg_len = mss; 20081 do { 20082 len = seg_len; 20083 20084 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20085 ipha = NULL; 20086 ip6h = NULL; 20087 20088 ASSERT(len > 0); 20089 ASSERT(max_pld >= 0); 20090 ASSERT(!add_buffer || cur_pld_off == 0); 20091 20092 /* 20093 * First time around for this payload buffer; note 20094 * in the case of a spillover, the following has 20095 * been done prior to adding the split-packet 20096 * descriptor to Multidata, and we don't want to 20097 * repeat the process. 20098 */ 20099 if (add_buffer) { 20100 ASSERT(mmd != NULL); 20101 ASSERT(md_pbuf == NULL); 20102 ASSERT(md_pbuf_nxt == NULL); 20103 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20104 20105 /* 20106 * Have we reached the limit? We'd get to 20107 * this case when we're not chaining the 20108 * Multidata messages together, and since 20109 * we're done, terminate this loop. 20110 */ 20111 if (max_pld == 0) 20112 break; /* done */ 20113 20114 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20115 TCP_STAT(tcps, tcp_mdt_allocfail); 20116 goto legacy_send; /* out_of_mem */ 20117 } 20118 20119 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20120 zc_cap != NULL) { 20121 if (!ip_md_zcopy_attr(mmd, NULL, 20122 zc_cap->ill_zerocopy_flags)) { 20123 freeb(md_pbuf); 20124 TCP_STAT(tcps, 20125 tcp_mdt_allocfail); 20126 /* out_of_mem */ 20127 goto legacy_send; 20128 } 20129 zcopy = B_TRUE; 20130 } 20131 20132 md_pbuf->b_rptr += base_pld_off; 20133 20134 /* 20135 * Add a payload buffer to the Multidata; this 20136 * operation must not fail, or otherwise our 20137 * logic in this routine is broken. There 20138 * is no memory allocation done by the 20139 * routine, so any returned failure simply 20140 * tells us that we've done something wrong. 20141 * 20142 * A failure tells us that either we're adding 20143 * the same payload buffer more than once, or 20144 * we're trying to add more buffers than 20145 * allowed (max_pld calculation is wrong). 20146 * None of the above cases should happen, and 20147 * we panic because either there's horrible 20148 * heap corruption, and/or programming mistake. 20149 */ 20150 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20151 if (pbuf_idx < 0) { 20152 cmn_err(CE_PANIC, "tcp_multisend: " 20153 "payload buffer logic error " 20154 "detected for tcp %p mmd %p " 20155 "pbuf %p (%d)\n", 20156 (void *)tcp, (void *)mmd, 20157 (void *)md_pbuf, pbuf_idx); 20158 } 20159 20160 ASSERT(max_pld > 0); 20161 --max_pld; 20162 add_buffer = B_FALSE; 20163 } 20164 20165 ASSERT(md_mp_head != NULL); 20166 ASSERT(md_pbuf != NULL); 20167 ASSERT(md_pbuf_nxt == NULL); 20168 ASSERT(pbuf_idx != -1); 20169 ASSERT(pbuf_idx_nxt == -1); 20170 ASSERT(*usable > 0); 20171 20172 /* 20173 * We spillover to the next payload buffer only 20174 * if all of the following is true: 20175 * 20176 * 1. There is not enough data on the current 20177 * payload buffer to make up `len', 20178 * 2. We are allowed to send `len', 20179 * 3. The next payload buffer length is large 20180 * enough to accomodate `spill'. 20181 */ 20182 if ((spill = len - *tail_unsent) > 0 && 20183 *usable >= len && 20184 MBLKL((*xmit_tail)->b_cont) >= spill && 20185 max_pld > 0) { 20186 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20187 if (md_pbuf_nxt == NULL) { 20188 TCP_STAT(tcps, tcp_mdt_allocfail); 20189 goto legacy_send; /* out_of_mem */ 20190 } 20191 20192 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20193 zc_cap != NULL) { 20194 if (!ip_md_zcopy_attr(mmd, NULL, 20195 zc_cap->ill_zerocopy_flags)) { 20196 freeb(md_pbuf_nxt); 20197 TCP_STAT(tcps, 20198 tcp_mdt_allocfail); 20199 /* out_of_mem */ 20200 goto legacy_send; 20201 } 20202 zcopy = B_TRUE; 20203 } 20204 20205 /* 20206 * See comments above on the first call to 20207 * mmd_addpldbuf for explanation on the panic. 20208 */ 20209 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20210 if (pbuf_idx_nxt < 0) { 20211 panic("tcp_multisend: " 20212 "next payload buffer logic error " 20213 "detected for tcp %p mmd %p " 20214 "pbuf %p (%d)\n", 20215 (void *)tcp, (void *)mmd, 20216 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20217 } 20218 20219 ASSERT(max_pld > 0); 20220 --max_pld; 20221 } else if (spill > 0) { 20222 /* 20223 * If there's a spillover, but the following 20224 * xmit_tail couldn't give us enough octets 20225 * to reach "len", then stop the current 20226 * Multidata creation and let the legacy 20227 * tcp_send() path take over. We don't want 20228 * to send the tiny segment as part of this 20229 * Multidata for performance reasons; instead, 20230 * we let the legacy path deal with grouping 20231 * it with the subsequent small mblks. 20232 */ 20233 if (*usable >= len && 20234 MBLKL((*xmit_tail)->b_cont) < spill) { 20235 max_pld = 0; 20236 break; /* done */ 20237 } 20238 20239 /* 20240 * We can't spillover, and we are near 20241 * the end of the current payload buffer, 20242 * so send what's left. 20243 */ 20244 ASSERT(*tail_unsent > 0); 20245 len = *tail_unsent; 20246 } 20247 20248 /* tail_unsent is negated if there is a spillover */ 20249 *tail_unsent -= len; 20250 *usable -= len; 20251 ASSERT(*usable >= 0); 20252 20253 if (*usable < mss) 20254 seg_len = *usable; 20255 /* 20256 * Sender SWS avoidance; see comments in tcp_send(); 20257 * everything else is the same, except that we only 20258 * do this here if there is no more data to be sent 20259 * following the current xmit_tail. We don't check 20260 * for 1-byte urgent data because we shouldn't get 20261 * here if TCP_URG_VALID is set. 20262 */ 20263 if (*usable > 0 && *usable < mss && 20264 ((md_pbuf_nxt == NULL && 20265 (*xmit_tail)->b_cont == NULL) || 20266 (md_pbuf_nxt != NULL && 20267 (*xmit_tail)->b_cont->b_cont == NULL)) && 20268 seg_len < (tcp->tcp_max_swnd >> 1) && 20269 (tcp->tcp_unsent - 20270 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20271 !tcp->tcp_zero_win_probe) { 20272 if ((*snxt + len) == tcp->tcp_snxt && 20273 (*snxt + len) == tcp->tcp_suna) { 20274 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20275 } 20276 done = B_TRUE; 20277 } 20278 20279 /* 20280 * Prime pump for IP's checksumming on our behalf; 20281 * include the adjustment for a source route if any. 20282 * Do this only for software/partial hardware checksum 20283 * offload, as this field gets zeroed out later for 20284 * the full hardware checksum offload case. 20285 */ 20286 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20287 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20288 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20289 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20290 } 20291 20292 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20293 *snxt += len; 20294 20295 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20296 /* 20297 * We set the PUSH bit only if TCP has no more buffered 20298 * data to be transmitted (or if sender SWS avoidance 20299 * takes place), as opposed to setting it for every 20300 * last packet in the burst. 20301 */ 20302 if (done || 20303 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20304 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20305 20306 /* 20307 * Set FIN bit if this is our last segment; snxt 20308 * already includes its length, and it will not 20309 * be adjusted after this point. 20310 */ 20311 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20312 *snxt == tcp->tcp_fss) { 20313 if (!tcp->tcp_fin_acked) { 20314 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20315 BUMP_MIB(&tcps->tcps_mib, 20316 tcpOutControl); 20317 } 20318 if (!tcp->tcp_fin_sent) { 20319 tcp->tcp_fin_sent = B_TRUE; 20320 /* 20321 * tcp state must be ESTABLISHED 20322 * in order for us to get here in 20323 * the first place. 20324 */ 20325 tcp->tcp_state = TCPS_FIN_WAIT_1; 20326 20327 /* 20328 * Upon returning from this routine, 20329 * tcp_wput_data() will set tcp_snxt 20330 * to be equal to snxt + tcp_fin_sent. 20331 * This is essentially the same as 20332 * setting it to tcp_fss + 1. 20333 */ 20334 } 20335 } 20336 20337 tcp->tcp_last_sent_len = (ushort_t)len; 20338 20339 len += tcp_hdr_len; 20340 if (tcp->tcp_ipversion == IPV4_VERSION) 20341 tcp->tcp_ipha->ipha_length = htons(len); 20342 else 20343 tcp->tcp_ip6h->ip6_plen = htons(len - 20344 ((char *)&tcp->tcp_ip6h[1] - 20345 tcp->tcp_iphc)); 20346 20347 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20348 20349 /* setup header fragment */ 20350 PDESC_HDR_ADD(pkt_info, 20351 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20352 tcp->tcp_mdt_hdr_head, /* head room */ 20353 tcp_hdr_len, /* len */ 20354 tcp->tcp_mdt_hdr_tail); /* tail room */ 20355 20356 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20357 hdr_frag_sz); 20358 ASSERT(MBLKIN(md_hbuf, 20359 (pkt_info->hdr_base - md_hbuf->b_rptr), 20360 PDESC_HDRSIZE(pkt_info))); 20361 20362 /* setup first payload fragment */ 20363 PDESC_PLD_INIT(pkt_info); 20364 PDESC_PLD_SPAN_ADD(pkt_info, 20365 pbuf_idx, /* index */ 20366 md_pbuf->b_rptr + cur_pld_off, /* start */ 20367 tcp->tcp_last_sent_len); /* len */ 20368 20369 /* create a split-packet in case of a spillover */ 20370 if (md_pbuf_nxt != NULL) { 20371 ASSERT(spill > 0); 20372 ASSERT(pbuf_idx_nxt > pbuf_idx); 20373 ASSERT(!add_buffer); 20374 20375 md_pbuf = md_pbuf_nxt; 20376 md_pbuf_nxt = NULL; 20377 pbuf_idx = pbuf_idx_nxt; 20378 pbuf_idx_nxt = -1; 20379 cur_pld_off = spill; 20380 20381 /* trim out first payload fragment */ 20382 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20383 20384 /* setup second payload fragment */ 20385 PDESC_PLD_SPAN_ADD(pkt_info, 20386 pbuf_idx, /* index */ 20387 md_pbuf->b_rptr, /* start */ 20388 spill); /* len */ 20389 20390 if ((*xmit_tail)->b_next == NULL) { 20391 /* 20392 * Store the lbolt used for RTT 20393 * estimation. We can only record one 20394 * timestamp per mblk so we do it when 20395 * we reach the end of the payload 20396 * buffer. Also we only take a new 20397 * timestamp sample when the previous 20398 * timed data from the same mblk has 20399 * been ack'ed. 20400 */ 20401 (*xmit_tail)->b_prev = local_time; 20402 (*xmit_tail)->b_next = 20403 (mblk_t *)(uintptr_t)first_snxt; 20404 } 20405 20406 first_snxt = *snxt - spill; 20407 20408 /* 20409 * Advance xmit_tail; usable could be 0 by 20410 * the time we got here, but we made sure 20411 * above that we would only spillover to 20412 * the next data block if usable includes 20413 * the spilled-over amount prior to the 20414 * subtraction. Therefore, we are sure 20415 * that xmit_tail->b_cont can't be NULL. 20416 */ 20417 ASSERT((*xmit_tail)->b_cont != NULL); 20418 *xmit_tail = (*xmit_tail)->b_cont; 20419 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20420 (uintptr_t)INT_MAX); 20421 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20422 } else { 20423 cur_pld_off += tcp->tcp_last_sent_len; 20424 } 20425 20426 /* 20427 * Fill in the header using the template header, and 20428 * add options such as time-stamp, ECN and/or SACK, 20429 * as needed. 20430 */ 20431 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20432 (clock_t)local_time, num_sack_blk); 20433 20434 /* take care of some IP header businesses */ 20435 if (af == AF_INET) { 20436 ipha = (ipha_t *)pkt_info->hdr_rptr; 20437 20438 ASSERT(OK_32PTR((uchar_t *)ipha)); 20439 ASSERT(PDESC_HDRL(pkt_info) >= 20440 IP_SIMPLE_HDR_LENGTH); 20441 ASSERT(ipha->ipha_version_and_hdr_length == 20442 IP_SIMPLE_HDR_VERSION); 20443 20444 /* 20445 * Assign ident value for current packet; see 20446 * related comments in ip_wput_ire() about the 20447 * contract private interface with clustering 20448 * group. 20449 */ 20450 clusterwide = B_FALSE; 20451 if (cl_inet_ipident != NULL) { 20452 ASSERT(cl_inet_isclusterwide != NULL); 20453 if ((*cl_inet_isclusterwide)(stack_id, 20454 IPPROTO_IP, AF_INET, 20455 (uint8_t *)(uintptr_t)src, NULL)) { 20456 ipha->ipha_ident = 20457 (*cl_inet_ipident)(stack_id, 20458 IPPROTO_IP, AF_INET, 20459 (uint8_t *)(uintptr_t)src, 20460 (uint8_t *)(uintptr_t)dst, 20461 NULL); 20462 clusterwide = B_TRUE; 20463 } 20464 } 20465 20466 if (!clusterwide) { 20467 ipha->ipha_ident = (uint16_t) 20468 atomic_add_32_nv( 20469 &ire->ire_ident, 1); 20470 } 20471 #ifndef _BIG_ENDIAN 20472 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20473 (ipha->ipha_ident >> 8); 20474 #endif 20475 } else { 20476 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20477 20478 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20479 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20480 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20481 ASSERT(PDESC_HDRL(pkt_info) >= 20482 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20483 TCP_CHECKSUM_SIZE)); 20484 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20485 20486 if (tcp->tcp_ip_forward_progress) { 20487 rconfirm = B_TRUE; 20488 tcp->tcp_ip_forward_progress = B_FALSE; 20489 } 20490 } 20491 20492 /* at least one payload span, and at most two */ 20493 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20494 20495 /* add the packet descriptor to Multidata */ 20496 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20497 KM_NOSLEEP)) == NULL) { 20498 /* 20499 * Any failure other than ENOMEM indicates 20500 * that we have passed in invalid pkt_info 20501 * or parameters to mmd_addpdesc, which must 20502 * not happen. 20503 * 20504 * EINVAL is a result of failure on boundary 20505 * checks against the pkt_info contents. It 20506 * should not happen, and we panic because 20507 * either there's horrible heap corruption, 20508 * and/or programming mistake. 20509 */ 20510 if (err != ENOMEM) { 20511 cmn_err(CE_PANIC, "tcp_multisend: " 20512 "pdesc logic error detected for " 20513 "tcp %p mmd %p pinfo %p (%d)\n", 20514 (void *)tcp, (void *)mmd, 20515 (void *)pkt_info, err); 20516 } 20517 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20518 goto legacy_send; /* out_of_mem */ 20519 } 20520 ASSERT(pkt != NULL); 20521 20522 /* calculate IP header and TCP checksums */ 20523 if (af == AF_INET) { 20524 /* calculate pseudo-header checksum */ 20525 cksum = (dst >> 16) + (dst & 0xFFFF) + 20526 (src >> 16) + (src & 0xFFFF); 20527 20528 /* offset for TCP header checksum */ 20529 up = IPH_TCPH_CHECKSUMP(ipha, 20530 IP_SIMPLE_HDR_LENGTH); 20531 } else { 20532 up = (uint16_t *)&ip6h->ip6_src; 20533 20534 /* calculate pseudo-header checksum */ 20535 cksum = up[0] + up[1] + up[2] + up[3] + 20536 up[4] + up[5] + up[6] + up[7] + 20537 up[8] + up[9] + up[10] + up[11] + 20538 up[12] + up[13] + up[14] + up[15]; 20539 20540 /* Fold the initial sum */ 20541 cksum = (cksum & 0xffff) + (cksum >> 16); 20542 20543 up = (uint16_t *)(((uchar_t *)ip6h) + 20544 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20545 } 20546 20547 if (hwcksum_flags & HCK_FULLCKSUM) { 20548 /* clear checksum field for hardware */ 20549 *up = 0; 20550 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20551 uint32_t sum; 20552 20553 /* pseudo-header checksumming */ 20554 sum = *up + cksum + IP_TCP_CSUM_COMP; 20555 sum = (sum & 0xFFFF) + (sum >> 16); 20556 *up = (sum & 0xFFFF) + (sum >> 16); 20557 } else { 20558 /* software checksumming */ 20559 TCP_STAT(tcps, tcp_out_sw_cksum); 20560 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20561 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20562 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20563 cksum + IP_TCP_CSUM_COMP); 20564 if (*up == 0) 20565 *up = 0xFFFF; 20566 } 20567 20568 /* IPv4 header checksum */ 20569 if (af == AF_INET) { 20570 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20571 ipha->ipha_hdr_checksum = 0; 20572 } else { 20573 IP_HDR_CKSUM(ipha, cksum, 20574 ((uint32_t *)ipha)[0], 20575 ((uint16_t *)ipha)[4]); 20576 } 20577 } 20578 20579 if (af == AF_INET && 20580 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20581 af == AF_INET6 && 20582 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20583 mblk_t *mp, *mp1; 20584 uchar_t *hdr_rptr, *hdr_wptr; 20585 uchar_t *pld_rptr, *pld_wptr; 20586 20587 /* 20588 * We reconstruct a pseudo packet for the hooks 20589 * framework using mmd_transform_link(). 20590 * If it is a split packet we pullup the 20591 * payload. FW_HOOKS expects a pkt comprising 20592 * of two mblks: a header and the payload. 20593 */ 20594 if ((mp = mmd_transform_link(pkt)) == NULL) { 20595 TCP_STAT(tcps, tcp_mdt_allocfail); 20596 goto legacy_send; 20597 } 20598 20599 if (pkt_info->pld_cnt > 1) { 20600 /* split payload, more than one pld */ 20601 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20602 NULL) { 20603 freemsg(mp); 20604 TCP_STAT(tcps, 20605 tcp_mdt_allocfail); 20606 goto legacy_send; 20607 } 20608 freemsg(mp->b_cont); 20609 mp->b_cont = mp1; 20610 } else { 20611 mp1 = mp->b_cont; 20612 } 20613 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20614 20615 /* 20616 * Remember the message offsets. This is so we 20617 * can detect changes when we return from the 20618 * FW_HOOKS callbacks. 20619 */ 20620 hdr_rptr = mp->b_rptr; 20621 hdr_wptr = mp->b_wptr; 20622 pld_rptr = mp->b_cont->b_rptr; 20623 pld_wptr = mp->b_cont->b_wptr; 20624 20625 if (af == AF_INET) { 20626 DTRACE_PROBE4( 20627 ip4__physical__out__start, 20628 ill_t *, NULL, 20629 ill_t *, ill, 20630 ipha_t *, ipha, 20631 mblk_t *, mp); 20632 FW_HOOKS( 20633 ipst->ips_ip4_physical_out_event, 20634 ipst->ips_ipv4firewall_physical_out, 20635 NULL, ill, ipha, mp, mp, 0, ipst); 20636 DTRACE_PROBE1( 20637 ip4__physical__out__end, 20638 mblk_t *, mp); 20639 } else { 20640 DTRACE_PROBE4( 20641 ip6__physical__out_start, 20642 ill_t *, NULL, 20643 ill_t *, ill, 20644 ip6_t *, ip6h, 20645 mblk_t *, mp); 20646 FW_HOOKS6( 20647 ipst->ips_ip6_physical_out_event, 20648 ipst->ips_ipv6firewall_physical_out, 20649 NULL, ill, ip6h, mp, mp, 0, ipst); 20650 DTRACE_PROBE1( 20651 ip6__physical__out__end, 20652 mblk_t *, mp); 20653 } 20654 20655 if (mp == NULL || 20656 (mp1 = mp->b_cont) == NULL || 20657 mp->b_rptr != hdr_rptr || 20658 mp->b_wptr != hdr_wptr || 20659 mp1->b_rptr != pld_rptr || 20660 mp1->b_wptr != pld_wptr || 20661 mp1->b_cont != NULL) { 20662 /* 20663 * We abandon multidata processing and 20664 * return to the normal path, either 20665 * when a packet is blocked, or when 20666 * the boundaries of header buffer or 20667 * payload buffer have been changed by 20668 * FW_HOOKS[6]. 20669 */ 20670 if (mp != NULL) 20671 freemsg(mp); 20672 goto legacy_send; 20673 } 20674 /* Finished with the pseudo packet */ 20675 freemsg(mp); 20676 } 20677 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20678 ill, ipha, ip6h); 20679 /* advance header offset */ 20680 cur_hdr_off += hdr_frag_sz; 20681 20682 obbytes += tcp->tcp_last_sent_len; 20683 ++obsegs; 20684 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20685 *tail_unsent > 0); 20686 20687 if ((*xmit_tail)->b_next == NULL) { 20688 /* 20689 * Store the lbolt used for RTT estimation. We can only 20690 * record one timestamp per mblk so we do it when we 20691 * reach the end of the payload buffer. Also we only 20692 * take a new timestamp sample when the previous timed 20693 * data from the same mblk has been ack'ed. 20694 */ 20695 (*xmit_tail)->b_prev = local_time; 20696 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20697 } 20698 20699 ASSERT(*tail_unsent >= 0); 20700 if (*tail_unsent > 0) { 20701 /* 20702 * We got here because we broke out of the above 20703 * loop due to of one of the following cases: 20704 * 20705 * 1. len < adjusted MSS (i.e. small), 20706 * 2. Sender SWS avoidance, 20707 * 3. max_pld is zero. 20708 * 20709 * We are done for this Multidata, so trim our 20710 * last payload buffer (if any) accordingly. 20711 */ 20712 if (md_pbuf != NULL) 20713 md_pbuf->b_wptr -= *tail_unsent; 20714 } else if (*usable > 0) { 20715 *xmit_tail = (*xmit_tail)->b_cont; 20716 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20717 (uintptr_t)INT_MAX); 20718 *tail_unsent = (int)MBLKL(*xmit_tail); 20719 add_buffer = B_TRUE; 20720 } 20721 } while (!done && *usable > 0 && num_burst_seg > 0 && 20722 (tcp_mdt_chain || max_pld > 0)); 20723 20724 if (md_mp_head != NULL) { 20725 /* send everything down */ 20726 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20727 &rconfirm); 20728 } 20729 20730 #undef PREP_NEW_MULTIDATA 20731 #undef PREP_NEW_PBUF 20732 #undef IPVER 20733 20734 IRE_REFRELE(ire); 20735 return (0); 20736 } 20737 20738 /* 20739 * A wrapper function for sending one or more Multidata messages down to 20740 * the module below ip; this routine does not release the reference of the 20741 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20742 */ 20743 static void 20744 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20745 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20746 { 20747 uint64_t delta; 20748 nce_t *nce; 20749 tcp_stack_t *tcps = tcp->tcp_tcps; 20750 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20751 20752 ASSERT(ire != NULL && ill != NULL); 20753 ASSERT(ire->ire_stq != NULL); 20754 ASSERT(md_mp_head != NULL); 20755 ASSERT(rconfirm != NULL); 20756 20757 /* adjust MIBs and IRE timestamp */ 20758 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20759 tcp->tcp_obsegs += obsegs; 20760 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20761 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20762 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20763 20764 if (tcp->tcp_ipversion == IPV4_VERSION) { 20765 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20766 } else { 20767 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20768 } 20769 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20770 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20771 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20772 20773 ire->ire_ob_pkt_count += obsegs; 20774 if (ire->ire_ipif != NULL) 20775 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20776 ire->ire_last_used_time = lbolt; 20777 20778 if (ipst->ips_ipobs_enabled) { 20779 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20780 pdesc_t *dl_pkt; 20781 pdescinfo_t pinfo; 20782 mblk_t *nmp; 20783 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20784 20785 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20786 (dl_pkt != NULL); 20787 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20788 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20789 continue; 20790 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20791 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20792 freemsg(nmp); 20793 } 20794 } 20795 20796 /* send it down */ 20797 putnext(ire->ire_stq, md_mp_head); 20798 20799 /* we're done for TCP/IPv4 */ 20800 if (tcp->tcp_ipversion == IPV4_VERSION) 20801 return; 20802 20803 nce = ire->ire_nce; 20804 20805 ASSERT(nce != NULL); 20806 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20807 ASSERT(nce->nce_state != ND_INCOMPLETE); 20808 20809 /* reachability confirmation? */ 20810 if (*rconfirm) { 20811 nce->nce_last = TICK_TO_MSEC(lbolt64); 20812 if (nce->nce_state != ND_REACHABLE) { 20813 mutex_enter(&nce->nce_lock); 20814 nce->nce_state = ND_REACHABLE; 20815 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20816 mutex_exit(&nce->nce_lock); 20817 (void) untimeout(nce->nce_timeout_id); 20818 if (ip_debug > 2) { 20819 /* ip1dbg */ 20820 pr_addr_dbg("tcp_multisend_data: state " 20821 "for %s changed to REACHABLE\n", 20822 AF_INET6, &ire->ire_addr_v6); 20823 } 20824 } 20825 /* reset transport reachability confirmation */ 20826 *rconfirm = B_FALSE; 20827 } 20828 20829 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20830 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20831 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20832 20833 if (delta > (uint64_t)ill->ill_reachable_time) { 20834 mutex_enter(&nce->nce_lock); 20835 switch (nce->nce_state) { 20836 case ND_REACHABLE: 20837 case ND_STALE: 20838 /* 20839 * ND_REACHABLE is identical to ND_STALE in this 20840 * specific case. If reachable time has expired for 20841 * this neighbor (delta is greater than reachable 20842 * time), conceptually, the neighbor cache is no 20843 * longer in REACHABLE state, but already in STALE 20844 * state. So the correct transition here is to 20845 * ND_DELAY. 20846 */ 20847 nce->nce_state = ND_DELAY; 20848 mutex_exit(&nce->nce_lock); 20849 NDP_RESTART_TIMER(nce, 20850 ipst->ips_delay_first_probe_time); 20851 if (ip_debug > 3) { 20852 /* ip2dbg */ 20853 pr_addr_dbg("tcp_multisend_data: state " 20854 "for %s changed to DELAY\n", 20855 AF_INET6, &ire->ire_addr_v6); 20856 } 20857 break; 20858 case ND_DELAY: 20859 case ND_PROBE: 20860 mutex_exit(&nce->nce_lock); 20861 /* Timers have already started */ 20862 break; 20863 case ND_UNREACHABLE: 20864 /* 20865 * ndp timer has detected that this nce is 20866 * unreachable and initiated deleting this nce 20867 * and all its associated IREs. This is a race 20868 * where we found the ire before it was deleted 20869 * and have just sent out a packet using this 20870 * unreachable nce. 20871 */ 20872 mutex_exit(&nce->nce_lock); 20873 break; 20874 default: 20875 ASSERT(0); 20876 } 20877 } 20878 } 20879 20880 /* 20881 * Derived from tcp_send_data(). 20882 */ 20883 static void 20884 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20885 int num_lso_seg) 20886 { 20887 ipha_t *ipha; 20888 mblk_t *ire_fp_mp; 20889 uint_t ire_fp_mp_len; 20890 uint32_t hcksum_txflags = 0; 20891 ipaddr_t src; 20892 ipaddr_t dst; 20893 uint32_t cksum; 20894 uint16_t *up; 20895 tcp_stack_t *tcps = tcp->tcp_tcps; 20896 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20897 20898 ASSERT(DB_TYPE(mp) == M_DATA); 20899 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20900 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20901 ASSERT(tcp->tcp_connp != NULL); 20902 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20903 20904 ipha = (ipha_t *)mp->b_rptr; 20905 src = ipha->ipha_src; 20906 dst = ipha->ipha_dst; 20907 20908 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20909 20910 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20911 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20912 num_lso_seg); 20913 #ifndef _BIG_ENDIAN 20914 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20915 #endif 20916 if (tcp->tcp_snd_zcopy_aware) { 20917 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20918 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20919 mp = tcp_zcopy_disable(tcp, mp); 20920 } 20921 20922 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20923 ASSERT(ill->ill_hcksum_capab != NULL); 20924 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20925 } 20926 20927 /* 20928 * Since the TCP checksum should be recalculated by h/w, we can just 20929 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20930 * pseudo-header checksum for HCK_PARTIALCKSUM. 20931 * The partial pseudo-header excludes TCP length, that was calculated 20932 * in tcp_send(), so to zero *up before further processing. 20933 */ 20934 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20935 20936 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20937 *up = 0; 20938 20939 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20940 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20941 20942 /* 20943 * Append LSO flags and mss to the mp. 20944 */ 20945 lso_info_set(mp, mss, HW_LSO); 20946 20947 ipha->ipha_fragment_offset_and_flags |= 20948 (uint32_t)htons(ire->ire_frag_flag); 20949 20950 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20951 ire_fp_mp_len = MBLKL(ire_fp_mp); 20952 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20953 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20954 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20955 20956 UPDATE_OB_PKT_COUNT(ire); 20957 ire->ire_last_used_time = lbolt; 20958 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20959 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20960 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20961 ntohs(ipha->ipha_length)); 20962 20963 DTRACE_PROBE4(ip4__physical__out__start, 20964 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20965 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20966 ipst->ips_ipv4firewall_physical_out, NULL, 20967 ill, ipha, mp, mp, 0, ipst); 20968 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20969 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20970 20971 if (mp != NULL) { 20972 if (ipst->ips_ipobs_enabled) { 20973 zoneid_t szone; 20974 20975 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20976 ipst, ALL_ZONES); 20977 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20978 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20979 } 20980 20981 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20982 } 20983 } 20984 20985 /* 20986 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20987 * scheme, and returns one of the following: 20988 * 20989 * -1 = failed allocation. 20990 * 0 = success; burst count reached, or usable send window is too small, 20991 * and that we'd rather wait until later before sending again. 20992 * 1 = success; we are called from tcp_multisend(), and both usable send 20993 * window and tail_unsent are greater than the MDT threshold, and thus 20994 * Multidata Transmit should be used instead. 20995 */ 20996 static int 20997 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20998 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20999 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21000 const int mdt_thres) 21001 { 21002 int num_burst_seg = tcp->tcp_snd_burst; 21003 ire_t *ire = NULL; 21004 ill_t *ill = NULL; 21005 mblk_t *ire_fp_mp = NULL; 21006 uint_t ire_fp_mp_len = 0; 21007 int num_lso_seg = 1; 21008 uint_t lso_usable; 21009 boolean_t do_lso_send = B_FALSE; 21010 tcp_stack_t *tcps = tcp->tcp_tcps; 21011 21012 /* 21013 * Check LSO capability before any further work. And the similar check 21014 * need to be done in for(;;) loop. 21015 * LSO will be deployed when therer is more than one mss of available 21016 * data and a burst transmission is allowed. 21017 */ 21018 if (tcp->tcp_lso && 21019 (tcp->tcp_valid_bits == 0 || 21020 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21021 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21022 /* 21023 * Try to find usable IRE/ILL and do basic check to the ILL. 21024 * Double check LSO usability before going further, since the 21025 * underlying interface could have been changed. In case of any 21026 * change of LSO capability, set tcp_ire_ill_check_done to 21027 * B_FALSE to force to check the ILL with the next send. 21028 */ 21029 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 21030 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 21031 /* 21032 * Enable LSO with this transmission. 21033 * Since IRE has been hold in tcp_send_find_ire_ill(), 21034 * IRE_REFRELE(ire) should be called before return. 21035 */ 21036 do_lso_send = B_TRUE; 21037 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21038 ire_fp_mp_len = MBLKL(ire_fp_mp); 21039 /* Round up to multiple of 4 */ 21040 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21041 } else { 21042 tcp->tcp_lso = B_FALSE; 21043 tcp->tcp_ire_ill_check_done = B_FALSE; 21044 do_lso_send = B_FALSE; 21045 ill = NULL; 21046 } 21047 } 21048 21049 for (;;) { 21050 struct datab *db; 21051 tcph_t *tcph; 21052 uint32_t sum; 21053 mblk_t *mp, *mp1; 21054 uchar_t *rptr; 21055 int len; 21056 21057 /* 21058 * If we're called by tcp_multisend(), and the amount of 21059 * sendable data as well as the size of current xmit_tail 21060 * is beyond the MDT threshold, return to the caller and 21061 * let the large data transmit be done using MDT. 21062 */ 21063 if (*usable > 0 && *usable > mdt_thres && 21064 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21065 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21066 ASSERT(tcp->tcp_mdt); 21067 return (1); /* success; do large send */ 21068 } 21069 21070 if (num_burst_seg == 0) 21071 break; /* success; burst count reached */ 21072 21073 /* 21074 * Calculate the maximum payload length we can send in *one* 21075 * time. 21076 */ 21077 if (do_lso_send) { 21078 /* 21079 * Check whether need to do LSO any more. 21080 */ 21081 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21082 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21083 lso_usable = MIN(lso_usable, 21084 num_burst_seg * mss); 21085 21086 num_lso_seg = lso_usable / mss; 21087 if (lso_usable % mss) { 21088 num_lso_seg++; 21089 tcp->tcp_last_sent_len = (ushort_t) 21090 (lso_usable % mss); 21091 } else { 21092 tcp->tcp_last_sent_len = (ushort_t)mss; 21093 } 21094 } else { 21095 do_lso_send = B_FALSE; 21096 num_lso_seg = 1; 21097 lso_usable = mss; 21098 } 21099 } 21100 21101 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21102 21103 /* 21104 * Adjust num_burst_seg here. 21105 */ 21106 num_burst_seg -= num_lso_seg; 21107 21108 len = mss; 21109 if (len > *usable) { 21110 ASSERT(do_lso_send == B_FALSE); 21111 21112 len = *usable; 21113 if (len <= 0) { 21114 /* Terminate the loop */ 21115 break; /* success; too small */ 21116 } 21117 /* 21118 * Sender silly-window avoidance. 21119 * Ignore this if we are going to send a 21120 * zero window probe out. 21121 * 21122 * TODO: force data into microscopic window? 21123 * ==> (!pushed || (unsent > usable)) 21124 */ 21125 if (len < (tcp->tcp_max_swnd >> 1) && 21126 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21127 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21128 len == 1) && (! tcp->tcp_zero_win_probe)) { 21129 /* 21130 * If the retransmit timer is not running 21131 * we start it so that we will retransmit 21132 * in the case when the the receiver has 21133 * decremented the window. 21134 */ 21135 if (*snxt == tcp->tcp_snxt && 21136 *snxt == tcp->tcp_suna) { 21137 /* 21138 * We are not supposed to send 21139 * anything. So let's wait a little 21140 * bit longer before breaking SWS 21141 * avoidance. 21142 * 21143 * What should the value be? 21144 * Suggestion: MAX(init rexmit time, 21145 * tcp->tcp_rto) 21146 */ 21147 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21148 } 21149 break; /* success; too small */ 21150 } 21151 } 21152 21153 tcph = tcp->tcp_tcph; 21154 21155 /* 21156 * The reason to adjust len here is that we need to set flags 21157 * and calculate checksum. 21158 */ 21159 if (do_lso_send) 21160 len = lso_usable; 21161 21162 *usable -= len; /* Approximate - can be adjusted later */ 21163 if (*usable > 0) 21164 tcph->th_flags[0] = TH_ACK; 21165 else 21166 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21167 21168 /* 21169 * Prime pump for IP's checksumming on our behalf 21170 * Include the adjustment for a source route if any. 21171 */ 21172 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21173 sum = (sum >> 16) + (sum & 0xFFFF); 21174 U16_TO_ABE16(sum, tcph->th_sum); 21175 21176 U32_TO_ABE32(*snxt, tcph->th_seq); 21177 21178 /* 21179 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21180 * set. For the case when TCP_FSS_VALID is the only valid 21181 * bit (normal active close), branch off only when we think 21182 * that the FIN flag needs to be set. Note for this case, 21183 * that (snxt + len) may not reflect the actual seg_len, 21184 * as len may be further reduced in tcp_xmit_mp(). If len 21185 * gets modified, we will end up here again. 21186 */ 21187 if (tcp->tcp_valid_bits != 0 && 21188 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21189 ((*snxt + len) == tcp->tcp_fss))) { 21190 uchar_t *prev_rptr; 21191 uint32_t prev_snxt = tcp->tcp_snxt; 21192 21193 if (*tail_unsent == 0) { 21194 ASSERT((*xmit_tail)->b_cont != NULL); 21195 *xmit_tail = (*xmit_tail)->b_cont; 21196 prev_rptr = (*xmit_tail)->b_rptr; 21197 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21198 (*xmit_tail)->b_rptr); 21199 } else { 21200 prev_rptr = (*xmit_tail)->b_rptr; 21201 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21202 *tail_unsent; 21203 } 21204 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21205 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21206 /* Restore tcp_snxt so we get amount sent right. */ 21207 tcp->tcp_snxt = prev_snxt; 21208 if (prev_rptr == (*xmit_tail)->b_rptr) { 21209 /* 21210 * If the previous timestamp is still in use, 21211 * don't stomp on it. 21212 */ 21213 if ((*xmit_tail)->b_next == NULL) { 21214 (*xmit_tail)->b_prev = local_time; 21215 (*xmit_tail)->b_next = 21216 (mblk_t *)(uintptr_t)(*snxt); 21217 } 21218 } else 21219 (*xmit_tail)->b_rptr = prev_rptr; 21220 21221 if (mp == NULL) { 21222 if (ire != NULL) 21223 IRE_REFRELE(ire); 21224 return (-1); 21225 } 21226 mp1 = mp->b_cont; 21227 21228 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21229 tcp->tcp_last_sent_len = (ushort_t)len; 21230 while (mp1->b_cont) { 21231 *xmit_tail = (*xmit_tail)->b_cont; 21232 (*xmit_tail)->b_prev = local_time; 21233 (*xmit_tail)->b_next = 21234 (mblk_t *)(uintptr_t)(*snxt); 21235 mp1 = mp1->b_cont; 21236 } 21237 *snxt += len; 21238 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21239 BUMP_LOCAL(tcp->tcp_obsegs); 21240 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21241 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21242 tcp_send_data(tcp, q, mp); 21243 continue; 21244 } 21245 21246 *snxt += len; /* Adjust later if we don't send all of len */ 21247 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21248 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21249 21250 if (*tail_unsent) { 21251 /* Are the bytes above us in flight? */ 21252 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21253 if (rptr != (*xmit_tail)->b_rptr) { 21254 *tail_unsent -= len; 21255 if (len <= mss) /* LSO is unusable */ 21256 tcp->tcp_last_sent_len = (ushort_t)len; 21257 len += tcp_hdr_len; 21258 if (tcp->tcp_ipversion == IPV4_VERSION) 21259 tcp->tcp_ipha->ipha_length = htons(len); 21260 else 21261 tcp->tcp_ip6h->ip6_plen = 21262 htons(len - 21263 ((char *)&tcp->tcp_ip6h[1] - 21264 tcp->tcp_iphc)); 21265 mp = dupb(*xmit_tail); 21266 if (mp == NULL) { 21267 if (ire != NULL) 21268 IRE_REFRELE(ire); 21269 return (-1); /* out_of_mem */ 21270 } 21271 mp->b_rptr = rptr; 21272 /* 21273 * If the old timestamp is no longer in use, 21274 * sample a new timestamp now. 21275 */ 21276 if ((*xmit_tail)->b_next == NULL) { 21277 (*xmit_tail)->b_prev = local_time; 21278 (*xmit_tail)->b_next = 21279 (mblk_t *)(uintptr_t)(*snxt-len); 21280 } 21281 goto must_alloc; 21282 } 21283 } else { 21284 *xmit_tail = (*xmit_tail)->b_cont; 21285 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21286 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21287 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21288 (*xmit_tail)->b_rptr); 21289 } 21290 21291 (*xmit_tail)->b_prev = local_time; 21292 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21293 21294 *tail_unsent -= len; 21295 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21296 tcp->tcp_last_sent_len = (ushort_t)len; 21297 21298 len += tcp_hdr_len; 21299 if (tcp->tcp_ipversion == IPV4_VERSION) 21300 tcp->tcp_ipha->ipha_length = htons(len); 21301 else 21302 tcp->tcp_ip6h->ip6_plen = htons(len - 21303 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21304 21305 mp = dupb(*xmit_tail); 21306 if (mp == NULL) { 21307 if (ire != NULL) 21308 IRE_REFRELE(ire); 21309 return (-1); /* out_of_mem */ 21310 } 21311 21312 len = tcp_hdr_len; 21313 /* 21314 * There are four reasons to allocate a new hdr mblk: 21315 * 1) The bytes above us are in use by another packet 21316 * 2) We don't have good alignment 21317 * 3) The mblk is being shared 21318 * 4) We don't have enough room for a header 21319 */ 21320 rptr = mp->b_rptr - len; 21321 if (!OK_32PTR(rptr) || 21322 ((db = mp->b_datap), db->db_ref != 2) || 21323 rptr < db->db_base + ire_fp_mp_len) { 21324 /* NOTE: we assume allocb returns an OK_32PTR */ 21325 21326 must_alloc:; 21327 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21328 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21329 if (mp1 == NULL) { 21330 freemsg(mp); 21331 if (ire != NULL) 21332 IRE_REFRELE(ire); 21333 return (-1); /* out_of_mem */ 21334 } 21335 mp1->b_cont = mp; 21336 mp = mp1; 21337 /* Leave room for Link Level header */ 21338 len = tcp_hdr_len; 21339 rptr = 21340 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21341 mp->b_wptr = &rptr[len]; 21342 } 21343 21344 /* 21345 * Fill in the header using the template header, and add 21346 * options such as time-stamp, ECN and/or SACK, as needed. 21347 */ 21348 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21349 21350 mp->b_rptr = rptr; 21351 21352 if (*tail_unsent) { 21353 int spill = *tail_unsent; 21354 21355 mp1 = mp->b_cont; 21356 if (mp1 == NULL) 21357 mp1 = mp; 21358 21359 /* 21360 * If we're a little short, tack on more mblks until 21361 * there is no more spillover. 21362 */ 21363 while (spill < 0) { 21364 mblk_t *nmp; 21365 int nmpsz; 21366 21367 nmp = (*xmit_tail)->b_cont; 21368 nmpsz = MBLKL(nmp); 21369 21370 /* 21371 * Excess data in mblk; can we split it? 21372 * If MDT is enabled for the connection, 21373 * keep on splitting as this is a transient 21374 * send path. 21375 */ 21376 if (!do_lso_send && !tcp->tcp_mdt && 21377 (spill + nmpsz > 0)) { 21378 /* 21379 * Don't split if stream head was 21380 * told to break up larger writes 21381 * into smaller ones. 21382 */ 21383 if (tcp->tcp_maxpsz > 0) 21384 break; 21385 21386 /* 21387 * Next mblk is less than SMSS/2 21388 * rounded up to nearest 64-byte; 21389 * let it get sent as part of the 21390 * next segment. 21391 */ 21392 if (tcp->tcp_localnet && 21393 !tcp->tcp_cork && 21394 (nmpsz < roundup((mss >> 1), 64))) 21395 break; 21396 } 21397 21398 *xmit_tail = nmp; 21399 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21400 /* Stash for rtt use later */ 21401 (*xmit_tail)->b_prev = local_time; 21402 (*xmit_tail)->b_next = 21403 (mblk_t *)(uintptr_t)(*snxt - len); 21404 mp1->b_cont = dupb(*xmit_tail); 21405 mp1 = mp1->b_cont; 21406 21407 spill += nmpsz; 21408 if (mp1 == NULL) { 21409 *tail_unsent = spill; 21410 freemsg(mp); 21411 if (ire != NULL) 21412 IRE_REFRELE(ire); 21413 return (-1); /* out_of_mem */ 21414 } 21415 } 21416 21417 /* Trim back any surplus on the last mblk */ 21418 if (spill >= 0) { 21419 mp1->b_wptr -= spill; 21420 *tail_unsent = spill; 21421 } else { 21422 /* 21423 * We did not send everything we could in 21424 * order to remain within the b_cont limit. 21425 */ 21426 *usable -= spill; 21427 *snxt += spill; 21428 tcp->tcp_last_sent_len += spill; 21429 UPDATE_MIB(&tcps->tcps_mib, 21430 tcpOutDataBytes, spill); 21431 /* 21432 * Adjust the checksum 21433 */ 21434 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21435 sum += spill; 21436 sum = (sum >> 16) + (sum & 0xFFFF); 21437 U16_TO_ABE16(sum, tcph->th_sum); 21438 if (tcp->tcp_ipversion == IPV4_VERSION) { 21439 sum = ntohs( 21440 ((ipha_t *)rptr)->ipha_length) + 21441 spill; 21442 ((ipha_t *)rptr)->ipha_length = 21443 htons(sum); 21444 } else { 21445 sum = ntohs( 21446 ((ip6_t *)rptr)->ip6_plen) + 21447 spill; 21448 ((ip6_t *)rptr)->ip6_plen = 21449 htons(sum); 21450 } 21451 *tail_unsent = 0; 21452 } 21453 } 21454 if (tcp->tcp_ip_forward_progress) { 21455 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21456 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21457 tcp->tcp_ip_forward_progress = B_FALSE; 21458 } 21459 21460 if (do_lso_send) { 21461 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21462 num_lso_seg); 21463 tcp->tcp_obsegs += num_lso_seg; 21464 21465 TCP_STAT(tcps, tcp_lso_times); 21466 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21467 } else { 21468 tcp_send_data(tcp, q, mp); 21469 BUMP_LOCAL(tcp->tcp_obsegs); 21470 } 21471 } 21472 21473 if (ire != NULL) 21474 IRE_REFRELE(ire); 21475 return (0); 21476 } 21477 21478 /* Unlink and return any mblk that looks like it contains a MDT info */ 21479 static mblk_t * 21480 tcp_mdt_info_mp(mblk_t *mp) 21481 { 21482 mblk_t *prev_mp; 21483 21484 for (;;) { 21485 prev_mp = mp; 21486 /* no more to process? */ 21487 if ((mp = mp->b_cont) == NULL) 21488 break; 21489 21490 switch (DB_TYPE(mp)) { 21491 case M_CTL: 21492 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21493 continue; 21494 ASSERT(prev_mp != NULL); 21495 prev_mp->b_cont = mp->b_cont; 21496 mp->b_cont = NULL; 21497 return (mp); 21498 default: 21499 break; 21500 } 21501 } 21502 return (mp); 21503 } 21504 21505 /* MDT info update routine, called when IP notifies us about MDT */ 21506 static void 21507 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21508 { 21509 boolean_t prev_state; 21510 tcp_stack_t *tcps = tcp->tcp_tcps; 21511 21512 /* 21513 * IP is telling us to abort MDT on this connection? We know 21514 * this because the capability is only turned off when IP 21515 * encounters some pathological cases, e.g. link-layer change 21516 * where the new driver doesn't support MDT, or in situation 21517 * where MDT usage on the link-layer has been switched off. 21518 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21519 * if the link-layer doesn't support MDT, and if it does, it 21520 * will indicate that the feature is to be turned on. 21521 */ 21522 prev_state = tcp->tcp_mdt; 21523 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21524 if (!tcp->tcp_mdt && !first) { 21525 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21526 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21527 (void *)tcp->tcp_connp)); 21528 } 21529 21530 /* 21531 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21532 * so disable MDT otherwise. The checks are done here 21533 * and in tcp_wput_data(). 21534 */ 21535 if (tcp->tcp_mdt && 21536 (tcp->tcp_ipversion == IPV4_VERSION && 21537 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21538 (tcp->tcp_ipversion == IPV6_VERSION && 21539 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21540 tcp->tcp_mdt = B_FALSE; 21541 21542 if (tcp->tcp_mdt) { 21543 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21544 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21545 "version (%d), expected version is %d", 21546 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21547 tcp->tcp_mdt = B_FALSE; 21548 return; 21549 } 21550 21551 /* 21552 * We need the driver to be able to handle at least three 21553 * spans per packet in order for tcp MDT to be utilized. 21554 * The first is for the header portion, while the rest are 21555 * needed to handle a packet that straddles across two 21556 * virtually non-contiguous buffers; a typical tcp packet 21557 * therefore consists of only two spans. Note that we take 21558 * a zero as "don't care". 21559 */ 21560 if (mdt_capab->ill_mdt_span_limit > 0 && 21561 mdt_capab->ill_mdt_span_limit < 3) { 21562 tcp->tcp_mdt = B_FALSE; 21563 return; 21564 } 21565 21566 /* a zero means driver wants default value */ 21567 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21568 tcps->tcps_mdt_max_pbufs); 21569 if (tcp->tcp_mdt_max_pld == 0) 21570 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21571 21572 /* ensure 32-bit alignment */ 21573 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21574 mdt_capab->ill_mdt_hdr_head), 4); 21575 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21576 mdt_capab->ill_mdt_hdr_tail), 4); 21577 21578 if (!first && !prev_state) { 21579 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21580 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21581 (void *)tcp->tcp_connp)); 21582 } 21583 } 21584 } 21585 21586 /* Unlink and return any mblk that looks like it contains a LSO info */ 21587 static mblk_t * 21588 tcp_lso_info_mp(mblk_t *mp) 21589 { 21590 mblk_t *prev_mp; 21591 21592 for (;;) { 21593 prev_mp = mp; 21594 /* no more to process? */ 21595 if ((mp = mp->b_cont) == NULL) 21596 break; 21597 21598 switch (DB_TYPE(mp)) { 21599 case M_CTL: 21600 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21601 continue; 21602 ASSERT(prev_mp != NULL); 21603 prev_mp->b_cont = mp->b_cont; 21604 mp->b_cont = NULL; 21605 return (mp); 21606 default: 21607 break; 21608 } 21609 } 21610 21611 return (mp); 21612 } 21613 21614 /* LSO info update routine, called when IP notifies us about LSO */ 21615 static void 21616 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21617 { 21618 tcp_stack_t *tcps = tcp->tcp_tcps; 21619 21620 /* 21621 * IP is telling us to abort LSO on this connection? We know 21622 * this because the capability is only turned off when IP 21623 * encounters some pathological cases, e.g. link-layer change 21624 * where the new NIC/driver doesn't support LSO, or in situation 21625 * where LSO usage on the link-layer has been switched off. 21626 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21627 * if the link-layer doesn't support LSO, and if it does, it 21628 * will indicate that the feature is to be turned on. 21629 */ 21630 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21631 TCP_STAT(tcps, tcp_lso_enabled); 21632 21633 /* 21634 * We currently only support LSO on simple TCP/IPv4, 21635 * so disable LSO otherwise. The checks are done here 21636 * and in tcp_wput_data(). 21637 */ 21638 if (tcp->tcp_lso && 21639 (tcp->tcp_ipversion == IPV4_VERSION && 21640 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21641 (tcp->tcp_ipversion == IPV6_VERSION)) { 21642 tcp->tcp_lso = B_FALSE; 21643 TCP_STAT(tcps, tcp_lso_disabled); 21644 } else { 21645 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21646 lso_capab->ill_lso_max); 21647 } 21648 } 21649 21650 static void 21651 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21652 { 21653 conn_t *connp = tcp->tcp_connp; 21654 tcp_stack_t *tcps = tcp->tcp_tcps; 21655 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21656 21657 ASSERT(ire != NULL); 21658 21659 /* 21660 * We may be in the fastpath here, and although we essentially do 21661 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21662 * we try to keep things as brief as possible. After all, these 21663 * are only best-effort checks, and we do more thorough ones prior 21664 * to calling tcp_send()/tcp_multisend(). 21665 */ 21666 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21667 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21668 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21669 !(ire->ire_flags & RTF_MULTIRT) && 21670 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21671 CONN_IS_LSO_MD_FASTPATH(connp)) { 21672 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21673 /* Cache the result */ 21674 connp->conn_lso_ok = B_TRUE; 21675 21676 ASSERT(ill->ill_lso_capab != NULL); 21677 if (!ill->ill_lso_capab->ill_lso_on) { 21678 ill->ill_lso_capab->ill_lso_on = 1; 21679 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21680 "LSO for interface %s\n", (void *)connp, 21681 ill->ill_name)); 21682 } 21683 tcp_lso_update(tcp, ill->ill_lso_capab); 21684 } else if (ipst->ips_ip_multidata_outbound && 21685 ILL_MDT_CAPABLE(ill)) { 21686 /* Cache the result */ 21687 connp->conn_mdt_ok = B_TRUE; 21688 21689 ASSERT(ill->ill_mdt_capab != NULL); 21690 if (!ill->ill_mdt_capab->ill_mdt_on) { 21691 ill->ill_mdt_capab->ill_mdt_on = 1; 21692 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21693 "MDT for interface %s\n", (void *)connp, 21694 ill->ill_name)); 21695 } 21696 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21697 } 21698 } 21699 21700 /* 21701 * The goal is to reduce the number of generated tcp segments by 21702 * setting the maxpsz multiplier to 0; this will have an affect on 21703 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21704 * into each packet, up to SMSS bytes. Doing this reduces the number 21705 * of outbound segments and incoming ACKs, thus allowing for better 21706 * network and system performance. In contrast the legacy behavior 21707 * may result in sending less than SMSS size, because the last mblk 21708 * for some packets may have more data than needed to make up SMSS, 21709 * and the legacy code refused to "split" it. 21710 * 21711 * We apply the new behavior on following situations: 21712 * 21713 * 1) Loopback connections, 21714 * 2) Connections in which the remote peer is not on local subnet, 21715 * 3) Local subnet connections over the bge interface (see below). 21716 * 21717 * Ideally, we would like this behavior to apply for interfaces other 21718 * than bge. However, doing so would negatively impact drivers which 21719 * perform dynamic mapping and unmapping of DMA resources, which are 21720 * increased by setting the maxpsz multiplier to 0 (more mblks per 21721 * packet will be generated by tcp). The bge driver does not suffer 21722 * from this, as it copies the mblks into pre-mapped buffers, and 21723 * therefore does not require more I/O resources than before. 21724 * 21725 * Otherwise, this behavior is present on all network interfaces when 21726 * the destination endpoint is non-local, since reducing the number 21727 * of packets in general is good for the network. 21728 * 21729 * TODO We need to remove this hard-coded conditional for bge once 21730 * a better "self-tuning" mechanism, or a way to comprehend 21731 * the driver transmit strategy is devised. Until the solution 21732 * is found and well understood, we live with this hack. 21733 */ 21734 if (!tcp_static_maxpsz && 21735 (tcp->tcp_loopback || !tcp->tcp_localnet || 21736 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21737 /* override the default value */ 21738 tcp->tcp_maxpsz = 0; 21739 21740 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21741 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21742 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21743 } 21744 21745 /* set the stream head parameters accordingly */ 21746 (void) tcp_maxpsz_set(tcp, B_TRUE); 21747 } 21748 21749 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21750 static void 21751 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21752 { 21753 uchar_t fval = *mp->b_rptr; 21754 mblk_t *tail; 21755 queue_t *q = tcp->tcp_wq; 21756 21757 /* TODO: How should flush interact with urgent data? */ 21758 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21759 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21760 /* 21761 * Flush only data that has not yet been put on the wire. If 21762 * we flush data that we have already transmitted, life, as we 21763 * know it, may come to an end. 21764 */ 21765 tail = tcp->tcp_xmit_tail; 21766 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21767 tcp->tcp_xmit_tail_unsent = 0; 21768 tcp->tcp_unsent = 0; 21769 if (tail->b_wptr != tail->b_rptr) 21770 tail = tail->b_cont; 21771 if (tail) { 21772 mblk_t **excess = &tcp->tcp_xmit_head; 21773 for (;;) { 21774 mblk_t *mp1 = *excess; 21775 if (mp1 == tail) 21776 break; 21777 tcp->tcp_xmit_tail = mp1; 21778 tcp->tcp_xmit_last = mp1; 21779 excess = &mp1->b_cont; 21780 } 21781 *excess = NULL; 21782 tcp_close_mpp(&tail); 21783 if (tcp->tcp_snd_zcopy_aware) 21784 tcp_zcopy_notify(tcp); 21785 } 21786 /* 21787 * We have no unsent data, so unsent must be less than 21788 * tcp_xmit_lowater, so re-enable flow. 21789 */ 21790 mutex_enter(&tcp->tcp_non_sq_lock); 21791 if (tcp->tcp_flow_stopped) { 21792 tcp_clrqfull(tcp); 21793 } 21794 mutex_exit(&tcp->tcp_non_sq_lock); 21795 } 21796 /* 21797 * TODO: you can't just flush these, you have to increase rwnd for one 21798 * thing. For another, how should urgent data interact? 21799 */ 21800 if (fval & FLUSHR) { 21801 *mp->b_rptr = fval & ~FLUSHW; 21802 /* XXX */ 21803 qreply(q, mp); 21804 return; 21805 } 21806 freemsg(mp); 21807 } 21808 21809 /* 21810 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21811 * messages. 21812 */ 21813 static void 21814 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21815 { 21816 mblk_t *mp1; 21817 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21818 STRUCT_HANDLE(strbuf, sb); 21819 queue_t *q = tcp->tcp_wq; 21820 int error; 21821 uint_t addrlen; 21822 21823 /* Make sure it is one of ours. */ 21824 switch (iocp->ioc_cmd) { 21825 case TI_GETMYNAME: 21826 case TI_GETPEERNAME: 21827 break; 21828 default: 21829 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21830 return; 21831 } 21832 switch (mi_copy_state(q, mp, &mp1)) { 21833 case -1: 21834 return; 21835 case MI_COPY_CASE(MI_COPY_IN, 1): 21836 break; 21837 case MI_COPY_CASE(MI_COPY_OUT, 1): 21838 /* Copy out the strbuf. */ 21839 mi_copyout(q, mp); 21840 return; 21841 case MI_COPY_CASE(MI_COPY_OUT, 2): 21842 /* All done. */ 21843 mi_copy_done(q, mp, 0); 21844 return; 21845 default: 21846 mi_copy_done(q, mp, EPROTO); 21847 return; 21848 } 21849 /* Check alignment of the strbuf */ 21850 if (!OK_32PTR(mp1->b_rptr)) { 21851 mi_copy_done(q, mp, EINVAL); 21852 return; 21853 } 21854 21855 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21856 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21857 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21858 mi_copy_done(q, mp, EINVAL); 21859 return; 21860 } 21861 21862 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21863 if (mp1 == NULL) 21864 return; 21865 21866 switch (iocp->ioc_cmd) { 21867 case TI_GETMYNAME: 21868 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21869 break; 21870 case TI_GETPEERNAME: 21871 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21872 break; 21873 } 21874 21875 if (error != 0) { 21876 mi_copy_done(q, mp, error); 21877 } else { 21878 mp1->b_wptr += addrlen; 21879 STRUCT_FSET(sb, len, addrlen); 21880 21881 /* Copy out the address */ 21882 mi_copyout(q, mp); 21883 } 21884 } 21885 21886 static void 21887 tcp_disable_direct_sockfs(tcp_t *tcp) 21888 { 21889 #ifdef _ILP32 21890 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21891 #else 21892 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21893 #endif 21894 /* 21895 * Insert this socket into the acceptor hash. 21896 * We might need it for T_CONN_RES message 21897 */ 21898 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21899 21900 if (tcp->tcp_fused) { 21901 /* 21902 * This is a fused loopback tcp; disable 21903 * read-side synchronous streams interface 21904 * and drain any queued data. It is okay 21905 * to do this for non-synchronous streams 21906 * fused tcp as well. 21907 */ 21908 tcp_fuse_disable_pair(tcp, B_FALSE); 21909 } 21910 tcp->tcp_issocket = B_FALSE; 21911 tcp->tcp_sodirect = NULL; 21912 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21913 } 21914 21915 /* 21916 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21917 * messages. 21918 */ 21919 /* ARGSUSED */ 21920 static void 21921 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21922 { 21923 conn_t *connp = (conn_t *)arg; 21924 tcp_t *tcp = connp->conn_tcp; 21925 queue_t *q = tcp->tcp_wq; 21926 struct iocblk *iocp; 21927 21928 ASSERT(DB_TYPE(mp) == M_IOCTL); 21929 /* 21930 * Try and ASSERT the minimum possible references on the 21931 * conn early enough. Since we are executing on write side, 21932 * the connection is obviously not detached and that means 21933 * there is a ref each for TCP and IP. Since we are behind 21934 * the squeue, the minimum references needed are 3. If the 21935 * conn is in classifier hash list, there should be an 21936 * extra ref for that (we check both the possibilities). 21937 */ 21938 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21939 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21940 21941 iocp = (struct iocblk *)mp->b_rptr; 21942 switch (iocp->ioc_cmd) { 21943 case TCP_IOC_DEFAULT_Q: 21944 /* Wants to be the default wq. */ 21945 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21946 iocp->ioc_error = EPERM; 21947 iocp->ioc_count = 0; 21948 mp->b_datap->db_type = M_IOCACK; 21949 qreply(q, mp); 21950 return; 21951 } 21952 tcp_def_q_set(tcp, mp); 21953 return; 21954 case _SIOCSOCKFALLBACK: 21955 /* 21956 * Either sockmod is about to be popped and the socket 21957 * would now be treated as a plain stream, or a module 21958 * is about to be pushed so we could no longer use read- 21959 * side synchronous streams for fused loopback tcp. 21960 * Drain any queued data and disable direct sockfs 21961 * interface from now on. 21962 */ 21963 if (!tcp->tcp_issocket) { 21964 DB_TYPE(mp) = M_IOCNAK; 21965 iocp->ioc_error = EINVAL; 21966 } else { 21967 tcp_disable_direct_sockfs(tcp); 21968 DB_TYPE(mp) = M_IOCACK; 21969 iocp->ioc_error = 0; 21970 } 21971 iocp->ioc_count = 0; 21972 iocp->ioc_rval = 0; 21973 qreply(q, mp); 21974 return; 21975 } 21976 CALL_IP_WPUT(connp, q, mp); 21977 } 21978 21979 /* 21980 * This routine is called by tcp_wput() to handle all TPI requests. 21981 */ 21982 /* ARGSUSED */ 21983 static void 21984 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21985 { 21986 conn_t *connp = (conn_t *)arg; 21987 tcp_t *tcp = connp->conn_tcp; 21988 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21989 uchar_t *rptr; 21990 t_scalar_t type; 21991 cred_t *cr; 21992 21993 /* 21994 * Try and ASSERT the minimum possible references on the 21995 * conn early enough. Since we are executing on write side, 21996 * the connection is obviously not detached and that means 21997 * there is a ref each for TCP and IP. Since we are behind 21998 * the squeue, the minimum references needed are 3. If the 21999 * conn is in classifier hash list, there should be an 22000 * extra ref for that (we check both the possibilities). 22001 */ 22002 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22003 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22004 22005 rptr = mp->b_rptr; 22006 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22007 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22008 type = ((union T_primitives *)rptr)->type; 22009 if (type == T_EXDATA_REQ) { 22010 tcp_output_urgent(connp, mp->b_cont, arg2); 22011 freeb(mp); 22012 } else if (type != T_DATA_REQ) { 22013 goto non_urgent_data; 22014 } else { 22015 /* TODO: options, flags, ... from user */ 22016 /* Set length to zero for reclamation below */ 22017 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22018 freeb(mp); 22019 } 22020 return; 22021 } else { 22022 if (tcp->tcp_debug) { 22023 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22024 "tcp_wput_proto, dropping one..."); 22025 } 22026 freemsg(mp); 22027 return; 22028 } 22029 22030 non_urgent_data: 22031 22032 switch ((int)tprim->type) { 22033 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22034 /* 22035 * save the kssl_ent_t from the next block, and convert this 22036 * back to a normal bind_req. 22037 */ 22038 if (mp->b_cont != NULL) { 22039 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22040 22041 if (tcp->tcp_kssl_ent != NULL) { 22042 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22043 KSSL_NO_PROXY); 22044 tcp->tcp_kssl_ent = NULL; 22045 } 22046 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22047 sizeof (kssl_ent_t)); 22048 kssl_hold_ent(tcp->tcp_kssl_ent); 22049 freemsg(mp->b_cont); 22050 mp->b_cont = NULL; 22051 } 22052 tprim->type = T_BIND_REQ; 22053 22054 /* FALLTHROUGH */ 22055 case O_T_BIND_REQ: /* bind request */ 22056 case T_BIND_REQ: /* new semantics bind request */ 22057 tcp_tpi_bind(tcp, mp); 22058 break; 22059 case T_UNBIND_REQ: /* unbind request */ 22060 tcp_tpi_unbind(tcp, mp); 22061 break; 22062 case O_T_CONN_RES: /* old connection response XXX */ 22063 case T_CONN_RES: /* connection response */ 22064 tcp_tli_accept(tcp, mp); 22065 break; 22066 case T_CONN_REQ: /* connection request */ 22067 tcp_tpi_connect(tcp, mp); 22068 break; 22069 case T_DISCON_REQ: /* disconnect request */ 22070 tcp_disconnect(tcp, mp); 22071 break; 22072 case T_CAPABILITY_REQ: 22073 tcp_capability_req(tcp, mp); /* capability request */ 22074 break; 22075 case T_INFO_REQ: /* information request */ 22076 tcp_info_req(tcp, mp); 22077 break; 22078 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22079 case T_OPTMGMT_REQ: 22080 /* 22081 * Note: no support for snmpcom_req() through new 22082 * T_OPTMGMT_REQ. See comments in ip.c 22083 */ 22084 22085 /* 22086 * All Solaris components should pass a db_credp 22087 * for this TPI message, hence we ASSERT. 22088 * But in case there is some other M_PROTO that looks 22089 * like a TPI message sent by some other kernel 22090 * component, we check and return an error. 22091 */ 22092 cr = msg_getcred(mp, NULL); 22093 ASSERT(cr != NULL); 22094 if (cr == NULL) { 22095 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 22096 return; 22097 } 22098 /* 22099 * If EINPROGRESS is returned, the request has been queued 22100 * for subsequent processing by ip_restart_optmgmt(), which 22101 * will do the CONN_DEC_REF(). 22102 */ 22103 CONN_INC_REF(connp); 22104 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 22105 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22106 B_TRUE) != EINPROGRESS) { 22107 CONN_DEC_REF(connp); 22108 } 22109 } else { 22110 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22111 B_TRUE) != EINPROGRESS) { 22112 CONN_DEC_REF(connp); 22113 } 22114 } 22115 break; 22116 22117 case T_UNITDATA_REQ: /* unitdata request */ 22118 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22119 break; 22120 case T_ORDREL_REQ: /* orderly release req */ 22121 freemsg(mp); 22122 22123 if (tcp->tcp_fused) 22124 tcp_unfuse(tcp); 22125 22126 if (tcp_xmit_end(tcp) != 0) { 22127 /* 22128 * We were crossing FINs and got a reset from 22129 * the other side. Just ignore it. 22130 */ 22131 if (tcp->tcp_debug) { 22132 (void) strlog(TCP_MOD_ID, 0, 1, 22133 SL_ERROR|SL_TRACE, 22134 "tcp_wput_proto, T_ORDREL_REQ out of " 22135 "state %s", 22136 tcp_display(tcp, NULL, 22137 DISP_ADDR_AND_PORT)); 22138 } 22139 } 22140 break; 22141 case T_ADDR_REQ: 22142 tcp_addr_req(tcp, mp); 22143 break; 22144 default: 22145 if (tcp->tcp_debug) { 22146 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22147 "tcp_wput_proto, bogus TPI msg, type %d", 22148 tprim->type); 22149 } 22150 /* 22151 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22152 * to recover. 22153 */ 22154 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22155 break; 22156 } 22157 } 22158 22159 /* 22160 * The TCP write service routine should never be called... 22161 */ 22162 /* ARGSUSED */ 22163 static void 22164 tcp_wsrv(queue_t *q) 22165 { 22166 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22167 22168 TCP_STAT(tcps, tcp_wsrv_called); 22169 } 22170 22171 /* Non overlapping byte exchanger */ 22172 static void 22173 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22174 { 22175 uchar_t uch; 22176 22177 while (len-- > 0) { 22178 uch = a[len]; 22179 a[len] = b[len]; 22180 b[len] = uch; 22181 } 22182 } 22183 22184 /* 22185 * Send out a control packet on the tcp connection specified. This routine 22186 * is typically called where we need a simple ACK or RST generated. 22187 */ 22188 static void 22189 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22190 { 22191 uchar_t *rptr; 22192 tcph_t *tcph; 22193 ipha_t *ipha = NULL; 22194 ip6_t *ip6h = NULL; 22195 uint32_t sum; 22196 int tcp_hdr_len; 22197 int tcp_ip_hdr_len; 22198 mblk_t *mp; 22199 tcp_stack_t *tcps = tcp->tcp_tcps; 22200 22201 /* 22202 * Save sum for use in source route later. 22203 */ 22204 ASSERT(tcp != NULL); 22205 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22206 tcp_hdr_len = tcp->tcp_hdr_len; 22207 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22208 22209 /* If a text string is passed in with the request, pass it to strlog. */ 22210 if (str != NULL && tcp->tcp_debug) { 22211 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22212 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22213 str, seq, ack, ctl); 22214 } 22215 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22216 BPRI_MED); 22217 if (mp == NULL) { 22218 return; 22219 } 22220 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22221 mp->b_rptr = rptr; 22222 mp->b_wptr = &rptr[tcp_hdr_len]; 22223 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22224 22225 if (tcp->tcp_ipversion == IPV4_VERSION) { 22226 ipha = (ipha_t *)rptr; 22227 ipha->ipha_length = htons(tcp_hdr_len); 22228 } else { 22229 ip6h = (ip6_t *)rptr; 22230 ASSERT(tcp != NULL); 22231 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22232 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22233 } 22234 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22235 tcph->th_flags[0] = (uint8_t)ctl; 22236 if (ctl & TH_RST) { 22237 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22238 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22239 /* 22240 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22241 */ 22242 if (tcp->tcp_snd_ts_ok && 22243 tcp->tcp_state > TCPS_SYN_SENT) { 22244 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22245 *(mp->b_wptr) = TCPOPT_EOL; 22246 if (tcp->tcp_ipversion == IPV4_VERSION) { 22247 ipha->ipha_length = htons(tcp_hdr_len - 22248 TCPOPT_REAL_TS_LEN); 22249 } else { 22250 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22251 TCPOPT_REAL_TS_LEN); 22252 } 22253 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22254 sum -= TCPOPT_REAL_TS_LEN; 22255 } 22256 } 22257 if (ctl & TH_ACK) { 22258 if (tcp->tcp_snd_ts_ok) { 22259 U32_TO_BE32(lbolt, 22260 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22261 U32_TO_BE32(tcp->tcp_ts_recent, 22262 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22263 } 22264 22265 /* Update the latest receive window size in TCP header. */ 22266 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22267 tcph->th_win); 22268 tcp->tcp_rack = ack; 22269 tcp->tcp_rack_cnt = 0; 22270 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22271 } 22272 BUMP_LOCAL(tcp->tcp_obsegs); 22273 U32_TO_BE32(seq, tcph->th_seq); 22274 U32_TO_BE32(ack, tcph->th_ack); 22275 /* 22276 * Include the adjustment for a source route if any. 22277 */ 22278 sum = (sum >> 16) + (sum & 0xFFFF); 22279 U16_TO_BE16(sum, tcph->th_sum); 22280 tcp_send_data(tcp, tcp->tcp_wq, mp); 22281 } 22282 22283 /* 22284 * If this routine returns B_TRUE, TCP can generate a RST in response 22285 * to a segment. If it returns B_FALSE, TCP should not respond. 22286 */ 22287 static boolean_t 22288 tcp_send_rst_chk(tcp_stack_t *tcps) 22289 { 22290 clock_t now; 22291 22292 /* 22293 * TCP needs to protect itself from generating too many RSTs. 22294 * This can be a DoS attack by sending us random segments 22295 * soliciting RSTs. 22296 * 22297 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22298 * in each 1 second interval. In this way, TCP still generate 22299 * RSTs in normal cases but when under attack, the impact is 22300 * limited. 22301 */ 22302 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22303 now = lbolt; 22304 /* lbolt can wrap around. */ 22305 if ((tcps->tcps_last_rst_intrvl > now) || 22306 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22307 1*SECONDS)) { 22308 tcps->tcps_last_rst_intrvl = now; 22309 tcps->tcps_rst_cnt = 1; 22310 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22311 return (B_FALSE); 22312 } 22313 } 22314 return (B_TRUE); 22315 } 22316 22317 /* 22318 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22319 */ 22320 static void 22321 tcp_ip_ire_mark_advice(tcp_t *tcp) 22322 { 22323 mblk_t *mp; 22324 ipic_t *ipic; 22325 22326 if (tcp->tcp_ipversion == IPV4_VERSION) { 22327 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22328 &ipic); 22329 } else { 22330 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22331 &ipic); 22332 } 22333 if (mp == NULL) 22334 return; 22335 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22336 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22337 } 22338 22339 /* 22340 * Return an IP advice ioctl mblk and set ipic to be the pointer 22341 * to the advice structure. 22342 */ 22343 static mblk_t * 22344 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22345 { 22346 struct iocblk *ioc; 22347 mblk_t *mp, *mp1; 22348 22349 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22350 if (mp == NULL) 22351 return (NULL); 22352 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22353 *ipic = (ipic_t *)mp->b_rptr; 22354 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22355 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22356 22357 bcopy(addr, *ipic + 1, addr_len); 22358 22359 (*ipic)->ipic_addr_length = addr_len; 22360 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22361 22362 mp1 = mkiocb(IP_IOCTL); 22363 if (mp1 == NULL) { 22364 freemsg(mp); 22365 return (NULL); 22366 } 22367 mp1->b_cont = mp; 22368 ioc = (struct iocblk *)mp1->b_rptr; 22369 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22370 22371 return (mp1); 22372 } 22373 22374 /* 22375 * Generate a reset based on an inbound packet, connp is set by caller 22376 * when RST is in response to an unexpected inbound packet for which 22377 * there is active tcp state in the system. 22378 * 22379 * IPSEC NOTE : Try to send the reply with the same protection as it came 22380 * in. We still have the ipsec_mp that the packet was attached to. Thus 22381 * the packet will go out at the same level of protection as it came in by 22382 * converting the IPSEC_IN to IPSEC_OUT. 22383 */ 22384 static void 22385 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22386 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22387 tcp_stack_t *tcps, conn_t *connp) 22388 { 22389 ipha_t *ipha = NULL; 22390 ip6_t *ip6h = NULL; 22391 ushort_t len; 22392 tcph_t *tcph; 22393 int i; 22394 mblk_t *ipsec_mp; 22395 boolean_t mctl_present; 22396 ipic_t *ipic; 22397 ipaddr_t v4addr; 22398 in6_addr_t v6addr; 22399 int addr_len; 22400 void *addr; 22401 queue_t *q = tcps->tcps_g_q; 22402 tcp_t *tcp; 22403 cred_t *cr; 22404 mblk_t *nmp; 22405 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22406 22407 if (tcps->tcps_g_q == NULL) { 22408 /* 22409 * For non-zero stackids the default queue isn't created 22410 * until the first open, thus there can be a need to send 22411 * a reset before then. But we can't do that, hence we just 22412 * drop the packet. Later during boot, when the default queue 22413 * has been setup, a retransmitted packet from the peer 22414 * will result in a reset. 22415 */ 22416 ASSERT(tcps->tcps_netstack->netstack_stackid != 22417 GLOBAL_NETSTACKID); 22418 freemsg(mp); 22419 return; 22420 } 22421 22422 if (connp != NULL) 22423 tcp = connp->conn_tcp; 22424 else 22425 tcp = Q_TO_TCP(q); 22426 22427 if (!tcp_send_rst_chk(tcps)) { 22428 tcps->tcps_rst_unsent++; 22429 freemsg(mp); 22430 return; 22431 } 22432 22433 if (mp->b_datap->db_type == M_CTL) { 22434 ipsec_mp = mp; 22435 mp = mp->b_cont; 22436 mctl_present = B_TRUE; 22437 } else { 22438 ipsec_mp = mp; 22439 mctl_present = B_FALSE; 22440 } 22441 22442 if (str && q && tcps->tcps_dbg) { 22443 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22444 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22445 "flags 0x%x", 22446 str, seq, ack, ctl); 22447 } 22448 if (mp->b_datap->db_ref != 1) { 22449 mblk_t *mp1 = copyb(mp); 22450 freemsg(mp); 22451 mp = mp1; 22452 if (!mp) { 22453 if (mctl_present) 22454 freeb(ipsec_mp); 22455 return; 22456 } else { 22457 if (mctl_present) { 22458 ipsec_mp->b_cont = mp; 22459 } else { 22460 ipsec_mp = mp; 22461 } 22462 } 22463 } else if (mp->b_cont) { 22464 freemsg(mp->b_cont); 22465 mp->b_cont = NULL; 22466 } 22467 /* 22468 * We skip reversing source route here. 22469 * (for now we replace all IP options with EOL) 22470 */ 22471 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22472 ipha = (ipha_t *)mp->b_rptr; 22473 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22474 mp->b_rptr[i] = IPOPT_EOL; 22475 /* 22476 * Make sure that src address isn't flagrantly invalid. 22477 * Not all broadcast address checking for the src address 22478 * is possible, since we don't know the netmask of the src 22479 * addr. No check for destination address is done, since 22480 * IP will not pass up a packet with a broadcast dest 22481 * address to TCP. Similar checks are done below for IPv6. 22482 */ 22483 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22484 CLASSD(ipha->ipha_src)) { 22485 freemsg(ipsec_mp); 22486 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22487 return; 22488 } 22489 } else { 22490 ip6h = (ip6_t *)mp->b_rptr; 22491 22492 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22493 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22494 freemsg(ipsec_mp); 22495 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22496 return; 22497 } 22498 22499 /* Remove any extension headers assuming partial overlay */ 22500 if (ip_hdr_len > IPV6_HDR_LEN) { 22501 uint8_t *to; 22502 22503 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22504 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22505 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22506 ip_hdr_len = IPV6_HDR_LEN; 22507 ip6h = (ip6_t *)mp->b_rptr; 22508 ip6h->ip6_nxt = IPPROTO_TCP; 22509 } 22510 } 22511 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22512 if (tcph->th_flags[0] & TH_RST) { 22513 freemsg(ipsec_mp); 22514 return; 22515 } 22516 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22517 len = ip_hdr_len + sizeof (tcph_t); 22518 mp->b_wptr = &mp->b_rptr[len]; 22519 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22520 ipha->ipha_length = htons(len); 22521 /* Swap addresses */ 22522 v4addr = ipha->ipha_src; 22523 ipha->ipha_src = ipha->ipha_dst; 22524 ipha->ipha_dst = v4addr; 22525 ipha->ipha_ident = 0; 22526 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22527 addr_len = IP_ADDR_LEN; 22528 addr = &v4addr; 22529 } else { 22530 /* No ip6i_t in this case */ 22531 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22532 /* Swap addresses */ 22533 v6addr = ip6h->ip6_src; 22534 ip6h->ip6_src = ip6h->ip6_dst; 22535 ip6h->ip6_dst = v6addr; 22536 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22537 addr_len = IPV6_ADDR_LEN; 22538 addr = &v6addr; 22539 } 22540 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22541 U32_TO_BE32(ack, tcph->th_ack); 22542 U32_TO_BE32(seq, tcph->th_seq); 22543 U16_TO_BE16(0, tcph->th_win); 22544 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22545 tcph->th_flags[0] = (uint8_t)ctl; 22546 if (ctl & TH_RST) { 22547 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22548 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22549 } 22550 22551 /* IP trusts us to set up labels when required. */ 22552 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22553 crgetlabel(cr) != NULL) { 22554 int err; 22555 22556 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22557 err = tsol_check_label(cr, &mp, 22558 tcp->tcp_connp->conn_mac_exempt, 22559 tcps->tcps_netstack->netstack_ip); 22560 else 22561 err = tsol_check_label_v6(cr, &mp, 22562 tcp->tcp_connp->conn_mac_exempt, 22563 tcps->tcps_netstack->netstack_ip); 22564 if (mctl_present) 22565 ipsec_mp->b_cont = mp; 22566 else 22567 ipsec_mp = mp; 22568 if (err != 0) { 22569 freemsg(ipsec_mp); 22570 return; 22571 } 22572 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22573 ipha = (ipha_t *)mp->b_rptr; 22574 } else { 22575 ip6h = (ip6_t *)mp->b_rptr; 22576 } 22577 } 22578 22579 if (mctl_present) { 22580 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22581 22582 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22583 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22584 return; 22585 } 22586 } 22587 if (zoneid == ALL_ZONES) 22588 zoneid = GLOBAL_ZONEID; 22589 22590 /* Add the zoneid so ip_output routes it properly */ 22591 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22592 freemsg(ipsec_mp); 22593 return; 22594 } 22595 ipsec_mp = nmp; 22596 22597 /* 22598 * NOTE: one might consider tracing a TCP packet here, but 22599 * this function has no active TCP state and no tcp structure 22600 * that has a trace buffer. If we traced here, we would have 22601 * to keep a local trace buffer in tcp_record_trace(). 22602 * 22603 * TSol note: The mblk that contains the incoming packet was 22604 * reused by tcp_xmit_listener_reset, so it already contains 22605 * the right credentials and we don't need to call mblk_setcred. 22606 * Also the conn's cred is not right since it is associated 22607 * with tcps_g_q. 22608 */ 22609 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22610 22611 /* 22612 * Tell IP to mark the IRE used for this destination temporary. 22613 * This way, we can limit our exposure to DoS attack because IP 22614 * creates an IRE for each destination. If there are too many, 22615 * the time to do any routing lookup will be extremely long. And 22616 * the lookup can be in interrupt context. 22617 * 22618 * Note that in normal circumstances, this marking should not 22619 * affect anything. It would be nice if only 1 message is 22620 * needed to inform IP that the IRE created for this RST should 22621 * not be added to the cache table. But there is currently 22622 * not such communication mechanism between TCP and IP. So 22623 * the best we can do now is to send the advice ioctl to IP 22624 * to mark the IRE temporary. 22625 */ 22626 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22627 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22628 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22629 } 22630 } 22631 22632 /* 22633 * Initiate closedown sequence on an active connection. (May be called as 22634 * writer.) Return value zero for OK return, non-zero for error return. 22635 */ 22636 static int 22637 tcp_xmit_end(tcp_t *tcp) 22638 { 22639 ipic_t *ipic; 22640 mblk_t *mp; 22641 tcp_stack_t *tcps = tcp->tcp_tcps; 22642 22643 if (tcp->tcp_state < TCPS_SYN_RCVD || 22644 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22645 /* 22646 * Invalid state, only states TCPS_SYN_RCVD, 22647 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22648 */ 22649 return (-1); 22650 } 22651 22652 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22653 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22654 /* 22655 * If there is nothing more unsent, send the FIN now. 22656 * Otherwise, it will go out with the last segment. 22657 */ 22658 if (tcp->tcp_unsent == 0) { 22659 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22660 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22661 22662 if (mp) { 22663 tcp_send_data(tcp, tcp->tcp_wq, mp); 22664 } else { 22665 /* 22666 * Couldn't allocate msg. Pretend we got it out. 22667 * Wait for rexmit timeout. 22668 */ 22669 tcp->tcp_snxt = tcp->tcp_fss + 1; 22670 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22671 } 22672 22673 /* 22674 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22675 * changed. 22676 */ 22677 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22678 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22679 } 22680 } else { 22681 /* 22682 * If tcp->tcp_cork is set, then the data will not get sent, 22683 * so we have to check that and unset it first. 22684 */ 22685 if (tcp->tcp_cork) 22686 tcp->tcp_cork = B_FALSE; 22687 tcp_wput_data(tcp, NULL, B_FALSE); 22688 } 22689 22690 /* 22691 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22692 * is 0, don't update the cache. 22693 */ 22694 if (tcps->tcps_rtt_updates == 0 || 22695 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22696 return (0); 22697 22698 /* 22699 * NOTE: should not update if source routes i.e. if tcp_remote if 22700 * different from the destination. 22701 */ 22702 if (tcp->tcp_ipversion == IPV4_VERSION) { 22703 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22704 return (0); 22705 } 22706 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22707 &ipic); 22708 } else { 22709 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22710 &tcp->tcp_ip6h->ip6_dst))) { 22711 return (0); 22712 } 22713 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22714 &ipic); 22715 } 22716 22717 /* Record route attributes in the IRE for use by future connections. */ 22718 if (mp == NULL) 22719 return (0); 22720 22721 /* 22722 * We do not have a good algorithm to update ssthresh at this time. 22723 * So don't do any update. 22724 */ 22725 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22726 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22727 22728 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22729 22730 return (0); 22731 } 22732 22733 /* ARGSUSED */ 22734 void 22735 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22736 { 22737 conn_t *connp = (conn_t *)arg; 22738 mblk_t *mp1; 22739 tcp_t *tcp = connp->conn_tcp; 22740 tcp_xmit_reset_event_t *eventp; 22741 22742 ASSERT(mp->b_datap->db_type == M_PROTO && 22743 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22744 22745 if (tcp->tcp_state != TCPS_LISTEN) { 22746 freemsg(mp); 22747 return; 22748 } 22749 22750 mp1 = mp->b_cont; 22751 mp->b_cont = NULL; 22752 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22753 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22754 connp->conn_netstack); 22755 22756 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22757 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22758 freemsg(mp); 22759 } 22760 22761 /* 22762 * Generate a "no listener here" RST in response to an "unknown" segment. 22763 * connp is set by caller when RST is in response to an unexpected 22764 * inbound packet for which there is active tcp state in the system. 22765 * Note that we are reusing the incoming mp to construct the outgoing RST. 22766 */ 22767 void 22768 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22769 tcp_stack_t *tcps, conn_t *connp) 22770 { 22771 uchar_t *rptr; 22772 uint32_t seg_len; 22773 tcph_t *tcph; 22774 uint32_t seg_seq; 22775 uint32_t seg_ack; 22776 uint_t flags; 22777 mblk_t *ipsec_mp; 22778 ipha_t *ipha; 22779 ip6_t *ip6h; 22780 boolean_t mctl_present = B_FALSE; 22781 boolean_t check = B_TRUE; 22782 boolean_t policy_present; 22783 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22784 22785 TCP_STAT(tcps, tcp_no_listener); 22786 22787 ipsec_mp = mp; 22788 22789 if (mp->b_datap->db_type == M_CTL) { 22790 ipsec_in_t *ii; 22791 22792 mctl_present = B_TRUE; 22793 mp = mp->b_cont; 22794 22795 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22796 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22797 if (ii->ipsec_in_dont_check) { 22798 check = B_FALSE; 22799 if (!ii->ipsec_in_secure) { 22800 freeb(ipsec_mp); 22801 mctl_present = B_FALSE; 22802 ipsec_mp = mp; 22803 } 22804 } 22805 } 22806 22807 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22808 policy_present = ipss->ipsec_inbound_v4_policy_present; 22809 ipha = (ipha_t *)mp->b_rptr; 22810 ip6h = NULL; 22811 } else { 22812 policy_present = ipss->ipsec_inbound_v6_policy_present; 22813 ipha = NULL; 22814 ip6h = (ip6_t *)mp->b_rptr; 22815 } 22816 22817 if (check && policy_present) { 22818 /* 22819 * The conn_t parameter is NULL because we already know 22820 * nobody's home. 22821 */ 22822 ipsec_mp = ipsec_check_global_policy( 22823 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22824 tcps->tcps_netstack); 22825 if (ipsec_mp == NULL) 22826 return; 22827 } 22828 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22829 DTRACE_PROBE2( 22830 tx__ip__log__error__nolistener__tcp, 22831 char *, "Could not reply with RST to mp(1)", 22832 mblk_t *, mp); 22833 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22834 freemsg(ipsec_mp); 22835 return; 22836 } 22837 22838 rptr = mp->b_rptr; 22839 22840 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22841 seg_seq = BE32_TO_U32(tcph->th_seq); 22842 seg_ack = BE32_TO_U32(tcph->th_ack); 22843 flags = tcph->th_flags[0]; 22844 22845 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22846 if (flags & TH_RST) { 22847 freemsg(ipsec_mp); 22848 } else if (flags & TH_ACK) { 22849 tcp_xmit_early_reset("no tcp, reset", 22850 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22851 connp); 22852 } else { 22853 if (flags & TH_SYN) { 22854 seg_len++; 22855 } else { 22856 /* 22857 * Here we violate the RFC. Note that a normal 22858 * TCP will never send a segment without the ACK 22859 * flag, except for RST or SYN segment. This 22860 * segment is neither. Just drop it on the 22861 * floor. 22862 */ 22863 freemsg(ipsec_mp); 22864 tcps->tcps_rst_unsent++; 22865 return; 22866 } 22867 22868 tcp_xmit_early_reset("no tcp, reset/ack", 22869 ipsec_mp, 0, seg_seq + seg_len, 22870 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22871 } 22872 } 22873 22874 /* 22875 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22876 * ip and tcp header ready to pass down to IP. If the mp passed in is 22877 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22878 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22879 * otherwise it will dup partial mblks.) 22880 * Otherwise, an appropriate ACK packet will be generated. This 22881 * routine is not usually called to send new data for the first time. It 22882 * is mostly called out of the timer for retransmits, and to generate ACKs. 22883 * 22884 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22885 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22886 * of the original mblk chain will be returned in *offset and *end_mp. 22887 */ 22888 mblk_t * 22889 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22890 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22891 boolean_t rexmit) 22892 { 22893 int data_length; 22894 int32_t off = 0; 22895 uint_t flags; 22896 mblk_t *mp1; 22897 mblk_t *mp2; 22898 uchar_t *rptr; 22899 tcph_t *tcph; 22900 int32_t num_sack_blk = 0; 22901 int32_t sack_opt_len = 0; 22902 tcp_stack_t *tcps = tcp->tcp_tcps; 22903 22904 /* Allocate for our maximum TCP header + link-level */ 22905 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22906 tcps->tcps_wroff_xtra, BPRI_MED); 22907 if (!mp1) 22908 return (NULL); 22909 data_length = 0; 22910 22911 /* 22912 * Note that tcp_mss has been adjusted to take into account the 22913 * timestamp option if applicable. Because SACK options do not 22914 * appear in every TCP segments and they are of variable lengths, 22915 * they cannot be included in tcp_mss. Thus we need to calculate 22916 * the actual segment length when we need to send a segment which 22917 * includes SACK options. 22918 */ 22919 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22920 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22921 tcp->tcp_num_sack_blk); 22922 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22923 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22924 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22925 max_to_send -= sack_opt_len; 22926 } 22927 22928 if (offset != NULL) { 22929 off = *offset; 22930 /* We use offset as an indicator that end_mp is not NULL. */ 22931 *end_mp = NULL; 22932 } 22933 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22934 /* This could be faster with cooperation from downstream */ 22935 if (mp2 != mp1 && !sendall && 22936 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22937 max_to_send) 22938 /* 22939 * Don't send the next mblk since the whole mblk 22940 * does not fit. 22941 */ 22942 break; 22943 mp2->b_cont = dupb(mp); 22944 mp2 = mp2->b_cont; 22945 if (!mp2) { 22946 freemsg(mp1); 22947 return (NULL); 22948 } 22949 mp2->b_rptr += off; 22950 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22951 (uintptr_t)INT_MAX); 22952 22953 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22954 if (data_length > max_to_send) { 22955 mp2->b_wptr -= data_length - max_to_send; 22956 data_length = max_to_send; 22957 off = mp2->b_wptr - mp->b_rptr; 22958 break; 22959 } else { 22960 off = 0; 22961 } 22962 } 22963 if (offset != NULL) { 22964 *offset = off; 22965 *end_mp = mp; 22966 } 22967 if (seg_len != NULL) { 22968 *seg_len = data_length; 22969 } 22970 22971 /* Update the latest receive window size in TCP header. */ 22972 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22973 tcp->tcp_tcph->th_win); 22974 22975 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22976 mp1->b_rptr = rptr; 22977 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22978 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22979 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22980 U32_TO_ABE32(seq, tcph->th_seq); 22981 22982 /* 22983 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22984 * that this function was called from tcp_wput_data. Thus, when called 22985 * to retransmit data the setting of the PUSH bit may appear some 22986 * what random in that it might get set when it should not. This 22987 * should not pose any performance issues. 22988 */ 22989 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22990 tcp->tcp_unsent == data_length)) { 22991 flags = TH_ACK | TH_PUSH; 22992 } else { 22993 flags = TH_ACK; 22994 } 22995 22996 if (tcp->tcp_ecn_ok) { 22997 if (tcp->tcp_ecn_echo_on) 22998 flags |= TH_ECE; 22999 23000 /* 23001 * Only set ECT bit and ECN_CWR if a segment contains new data. 23002 * There is no TCP flow control for non-data segments, and 23003 * only data segment is transmitted reliably. 23004 */ 23005 if (data_length > 0 && !rexmit) { 23006 SET_ECT(tcp, rptr); 23007 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23008 flags |= TH_CWR; 23009 tcp->tcp_ecn_cwr_sent = B_TRUE; 23010 } 23011 } 23012 } 23013 23014 if (tcp->tcp_valid_bits) { 23015 uint32_t u1; 23016 23017 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23018 seq == tcp->tcp_iss) { 23019 uchar_t *wptr; 23020 23021 /* 23022 * If TCP_ISS_VALID and the seq number is tcp_iss, 23023 * TCP can only be in SYN-SENT, SYN-RCVD or 23024 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23025 * our SYN is not ack'ed but the app closes this 23026 * TCP connection. 23027 */ 23028 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23029 tcp->tcp_state == TCPS_SYN_RCVD || 23030 tcp->tcp_state == TCPS_FIN_WAIT_1); 23031 23032 /* 23033 * Tack on the MSS option. It is always needed 23034 * for both active and passive open. 23035 * 23036 * MSS option value should be interface MTU - MIN 23037 * TCP/IP header according to RFC 793 as it means 23038 * the maximum segment size TCP can receive. But 23039 * to get around some broken middle boxes/end hosts 23040 * out there, we allow the option value to be the 23041 * same as the MSS option size on the peer side. 23042 * In this way, the other side will not send 23043 * anything larger than they can receive. 23044 * 23045 * Note that for SYN_SENT state, the ndd param 23046 * tcp_use_smss_as_mss_opt has no effect as we 23047 * don't know the peer's MSS option value. So 23048 * the only case we need to take care of is in 23049 * SYN_RCVD state, which is done later. 23050 */ 23051 wptr = mp1->b_wptr; 23052 wptr[0] = TCPOPT_MAXSEG; 23053 wptr[1] = TCPOPT_MAXSEG_LEN; 23054 wptr += 2; 23055 u1 = tcp->tcp_if_mtu - 23056 (tcp->tcp_ipversion == IPV4_VERSION ? 23057 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23058 TCP_MIN_HEADER_LENGTH; 23059 U16_TO_BE16(u1, wptr); 23060 mp1->b_wptr = wptr + 2; 23061 /* Update the offset to cover the additional word */ 23062 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23063 23064 /* 23065 * Note that the following way of filling in 23066 * TCP options are not optimal. Some NOPs can 23067 * be saved. But there is no need at this time 23068 * to optimize it. When it is needed, we will 23069 * do it. 23070 */ 23071 switch (tcp->tcp_state) { 23072 case TCPS_SYN_SENT: 23073 flags = TH_SYN; 23074 23075 if (tcp->tcp_snd_ts_ok) { 23076 uint32_t llbolt = (uint32_t)lbolt; 23077 23078 wptr = mp1->b_wptr; 23079 wptr[0] = TCPOPT_NOP; 23080 wptr[1] = TCPOPT_NOP; 23081 wptr[2] = TCPOPT_TSTAMP; 23082 wptr[3] = TCPOPT_TSTAMP_LEN; 23083 wptr += 4; 23084 U32_TO_BE32(llbolt, wptr); 23085 wptr += 4; 23086 ASSERT(tcp->tcp_ts_recent == 0); 23087 U32_TO_BE32(0L, wptr); 23088 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23089 tcph->th_offset_and_rsrvd[0] += 23090 (3 << 4); 23091 } 23092 23093 /* 23094 * Set up all the bits to tell other side 23095 * we are ECN capable. 23096 */ 23097 if (tcp->tcp_ecn_ok) { 23098 flags |= (TH_ECE | TH_CWR); 23099 } 23100 break; 23101 case TCPS_SYN_RCVD: 23102 flags |= TH_SYN; 23103 23104 /* 23105 * Reset the MSS option value to be SMSS 23106 * We should probably add back the bytes 23107 * for timestamp option and IPsec. We 23108 * don't do that as this is a workaround 23109 * for broken middle boxes/end hosts, it 23110 * is better for us to be more cautious. 23111 * They may not take these things into 23112 * account in their SMSS calculation. Thus 23113 * the peer's calculated SMSS may be smaller 23114 * than what it can be. This should be OK. 23115 */ 23116 if (tcps->tcps_use_smss_as_mss_opt) { 23117 u1 = tcp->tcp_mss; 23118 U16_TO_BE16(u1, wptr); 23119 } 23120 23121 /* 23122 * If the other side is ECN capable, reply 23123 * that we are also ECN capable. 23124 */ 23125 if (tcp->tcp_ecn_ok) 23126 flags |= TH_ECE; 23127 break; 23128 default: 23129 /* 23130 * The above ASSERT() makes sure that this 23131 * must be FIN-WAIT-1 state. Our SYN has 23132 * not been ack'ed so retransmit it. 23133 */ 23134 flags |= TH_SYN; 23135 break; 23136 } 23137 23138 if (tcp->tcp_snd_ws_ok) { 23139 wptr = mp1->b_wptr; 23140 wptr[0] = TCPOPT_NOP; 23141 wptr[1] = TCPOPT_WSCALE; 23142 wptr[2] = TCPOPT_WS_LEN; 23143 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23144 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23145 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23146 } 23147 23148 if (tcp->tcp_snd_sack_ok) { 23149 wptr = mp1->b_wptr; 23150 wptr[0] = TCPOPT_NOP; 23151 wptr[1] = TCPOPT_NOP; 23152 wptr[2] = TCPOPT_SACK_PERMITTED; 23153 wptr[3] = TCPOPT_SACK_OK_LEN; 23154 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23155 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23156 } 23157 23158 /* allocb() of adequate mblk assures space */ 23159 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23160 (uintptr_t)INT_MAX); 23161 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23162 /* 23163 * Get IP set to checksum on our behalf 23164 * Include the adjustment for a source route if any. 23165 */ 23166 u1 += tcp->tcp_sum; 23167 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23168 U16_TO_BE16(u1, tcph->th_sum); 23169 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23170 } 23171 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23172 (seq + data_length) == tcp->tcp_fss) { 23173 if (!tcp->tcp_fin_acked) { 23174 flags |= TH_FIN; 23175 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23176 } 23177 if (!tcp->tcp_fin_sent) { 23178 tcp->tcp_fin_sent = B_TRUE; 23179 switch (tcp->tcp_state) { 23180 case TCPS_SYN_RCVD: 23181 case TCPS_ESTABLISHED: 23182 tcp->tcp_state = TCPS_FIN_WAIT_1; 23183 break; 23184 case TCPS_CLOSE_WAIT: 23185 tcp->tcp_state = TCPS_LAST_ACK; 23186 break; 23187 } 23188 if (tcp->tcp_suna == tcp->tcp_snxt) 23189 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23190 tcp->tcp_snxt = tcp->tcp_fss + 1; 23191 } 23192 } 23193 /* 23194 * Note the trick here. u1 is unsigned. When tcp_urg 23195 * is smaller than seq, u1 will become a very huge value. 23196 * So the comparison will fail. Also note that tcp_urp 23197 * should be positive, see RFC 793 page 17. 23198 */ 23199 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23200 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23201 u1 < (uint32_t)(64 * 1024)) { 23202 flags |= TH_URG; 23203 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23204 U32_TO_ABE16(u1, tcph->th_urp); 23205 } 23206 } 23207 tcph->th_flags[0] = (uchar_t)flags; 23208 tcp->tcp_rack = tcp->tcp_rnxt; 23209 tcp->tcp_rack_cnt = 0; 23210 23211 if (tcp->tcp_snd_ts_ok) { 23212 if (tcp->tcp_state != TCPS_SYN_SENT) { 23213 uint32_t llbolt = (uint32_t)lbolt; 23214 23215 U32_TO_BE32(llbolt, 23216 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23217 U32_TO_BE32(tcp->tcp_ts_recent, 23218 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23219 } 23220 } 23221 23222 if (num_sack_blk > 0) { 23223 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23224 sack_blk_t *tmp; 23225 int32_t i; 23226 23227 wptr[0] = TCPOPT_NOP; 23228 wptr[1] = TCPOPT_NOP; 23229 wptr[2] = TCPOPT_SACK; 23230 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23231 sizeof (sack_blk_t); 23232 wptr += TCPOPT_REAL_SACK_LEN; 23233 23234 tmp = tcp->tcp_sack_list; 23235 for (i = 0; i < num_sack_blk; i++) { 23236 U32_TO_BE32(tmp[i].begin, wptr); 23237 wptr += sizeof (tcp_seq); 23238 U32_TO_BE32(tmp[i].end, wptr); 23239 wptr += sizeof (tcp_seq); 23240 } 23241 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23242 } 23243 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23244 data_length += (int)(mp1->b_wptr - rptr); 23245 if (tcp->tcp_ipversion == IPV4_VERSION) { 23246 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23247 } else { 23248 ip6_t *ip6 = (ip6_t *)(rptr + 23249 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23250 sizeof (ip6i_t) : 0)); 23251 23252 ip6->ip6_plen = htons(data_length - 23253 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23254 } 23255 23256 /* 23257 * Prime pump for IP 23258 * Include the adjustment for a source route if any. 23259 */ 23260 data_length -= tcp->tcp_ip_hdr_len; 23261 data_length += tcp->tcp_sum; 23262 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23263 U16_TO_ABE16(data_length, tcph->th_sum); 23264 if (tcp->tcp_ip_forward_progress) { 23265 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23266 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23267 tcp->tcp_ip_forward_progress = B_FALSE; 23268 } 23269 return (mp1); 23270 } 23271 23272 /* This function handles the push timeout. */ 23273 void 23274 tcp_push_timer(void *arg) 23275 { 23276 conn_t *connp = (conn_t *)arg; 23277 tcp_t *tcp = connp->conn_tcp; 23278 uint_t flags; 23279 sodirect_t *sodp; 23280 23281 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23282 23283 ASSERT(tcp->tcp_listener == NULL); 23284 23285 ASSERT(!IPCL_IS_NONSTR(connp)); 23286 23287 /* 23288 * We need to plug synchronous streams during our drain to prevent 23289 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23290 */ 23291 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23292 tcp->tcp_push_tid = 0; 23293 23294 SOD_PTR_ENTER(tcp, sodp); 23295 if (sodp != NULL) { 23296 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23297 /* sod_wakeup() does the mutex_exit() */ 23298 } else if (tcp->tcp_rcv_list != NULL) { 23299 flags = tcp_rcv_drain(tcp); 23300 } 23301 if (flags == TH_ACK_NEEDED) 23302 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23303 23304 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23305 } 23306 23307 /* 23308 * This function handles delayed ACK timeout. 23309 */ 23310 static void 23311 tcp_ack_timer(void *arg) 23312 { 23313 conn_t *connp = (conn_t *)arg; 23314 tcp_t *tcp = connp->conn_tcp; 23315 mblk_t *mp; 23316 tcp_stack_t *tcps = tcp->tcp_tcps; 23317 23318 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23319 23320 tcp->tcp_ack_tid = 0; 23321 23322 if (tcp->tcp_fused) 23323 return; 23324 23325 /* 23326 * Do not send ACK if there is no outstanding unack'ed data. 23327 */ 23328 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23329 return; 23330 } 23331 23332 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23333 /* 23334 * Make sure we don't allow deferred ACKs to result in 23335 * timer-based ACKing. If we have held off an ACK 23336 * when there was more than an mss here, and the timer 23337 * goes off, we have to worry about the possibility 23338 * that the sender isn't doing slow-start, or is out 23339 * of step with us for some other reason. We fall 23340 * permanently back in the direction of 23341 * ACK-every-other-packet as suggested in RFC 1122. 23342 */ 23343 if (tcp->tcp_rack_abs_max > 2) 23344 tcp->tcp_rack_abs_max--; 23345 tcp->tcp_rack_cur_max = 2; 23346 } 23347 mp = tcp_ack_mp(tcp); 23348 23349 if (mp != NULL) { 23350 BUMP_LOCAL(tcp->tcp_obsegs); 23351 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23352 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23353 tcp_send_data(tcp, tcp->tcp_wq, mp); 23354 } 23355 } 23356 23357 23358 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23359 static mblk_t * 23360 tcp_ack_mp(tcp_t *tcp) 23361 { 23362 uint32_t seq_no; 23363 tcp_stack_t *tcps = tcp->tcp_tcps; 23364 23365 /* 23366 * There are a few cases to be considered while setting the sequence no. 23367 * Essentially, we can come here while processing an unacceptable pkt 23368 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23369 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23370 * If we are here for a zero window probe, stick with suna. In all 23371 * other cases, we check if suna + swnd encompasses snxt and set 23372 * the sequence number to snxt, if so. If snxt falls outside the 23373 * window (the receiver probably shrunk its window), we will go with 23374 * suna + swnd, otherwise the sequence no will be unacceptable to the 23375 * receiver. 23376 */ 23377 if (tcp->tcp_zero_win_probe) { 23378 seq_no = tcp->tcp_suna; 23379 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23380 ASSERT(tcp->tcp_swnd == 0); 23381 seq_no = tcp->tcp_snxt; 23382 } else { 23383 seq_no = SEQ_GT(tcp->tcp_snxt, 23384 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23385 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23386 } 23387 23388 if (tcp->tcp_valid_bits) { 23389 /* 23390 * For the complex case where we have to send some 23391 * controls (FIN or SYN), let tcp_xmit_mp do it. 23392 */ 23393 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23394 NULL, B_FALSE)); 23395 } else { 23396 /* Generate a simple ACK */ 23397 int data_length; 23398 uchar_t *rptr; 23399 tcph_t *tcph; 23400 mblk_t *mp1; 23401 int32_t tcp_hdr_len; 23402 int32_t tcp_tcp_hdr_len; 23403 int32_t num_sack_blk = 0; 23404 int32_t sack_opt_len; 23405 23406 /* 23407 * Allocate space for TCP + IP headers 23408 * and link-level header 23409 */ 23410 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23411 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23412 tcp->tcp_num_sack_blk); 23413 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23414 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23415 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23416 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23417 } else { 23418 tcp_hdr_len = tcp->tcp_hdr_len; 23419 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23420 } 23421 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23422 if (!mp1) 23423 return (NULL); 23424 23425 /* Update the latest receive window size in TCP header. */ 23426 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23427 tcp->tcp_tcph->th_win); 23428 /* copy in prototype TCP + IP header */ 23429 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23430 mp1->b_rptr = rptr; 23431 mp1->b_wptr = rptr + tcp_hdr_len; 23432 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23433 23434 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23435 23436 /* Set the TCP sequence number. */ 23437 U32_TO_ABE32(seq_no, tcph->th_seq); 23438 23439 /* Set up the TCP flag field. */ 23440 tcph->th_flags[0] = (uchar_t)TH_ACK; 23441 if (tcp->tcp_ecn_echo_on) 23442 tcph->th_flags[0] |= TH_ECE; 23443 23444 tcp->tcp_rack = tcp->tcp_rnxt; 23445 tcp->tcp_rack_cnt = 0; 23446 23447 /* fill in timestamp option if in use */ 23448 if (tcp->tcp_snd_ts_ok) { 23449 uint32_t llbolt = (uint32_t)lbolt; 23450 23451 U32_TO_BE32(llbolt, 23452 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23453 U32_TO_BE32(tcp->tcp_ts_recent, 23454 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23455 } 23456 23457 /* Fill in SACK options */ 23458 if (num_sack_blk > 0) { 23459 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23460 sack_blk_t *tmp; 23461 int32_t i; 23462 23463 wptr[0] = TCPOPT_NOP; 23464 wptr[1] = TCPOPT_NOP; 23465 wptr[2] = TCPOPT_SACK; 23466 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23467 sizeof (sack_blk_t); 23468 wptr += TCPOPT_REAL_SACK_LEN; 23469 23470 tmp = tcp->tcp_sack_list; 23471 for (i = 0; i < num_sack_blk; i++) { 23472 U32_TO_BE32(tmp[i].begin, wptr); 23473 wptr += sizeof (tcp_seq); 23474 U32_TO_BE32(tmp[i].end, wptr); 23475 wptr += sizeof (tcp_seq); 23476 } 23477 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23478 << 4); 23479 } 23480 23481 if (tcp->tcp_ipversion == IPV4_VERSION) { 23482 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23483 } else { 23484 /* Check for ip6i_t header in sticky hdrs */ 23485 ip6_t *ip6 = (ip6_t *)(rptr + 23486 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23487 sizeof (ip6i_t) : 0)); 23488 23489 ip6->ip6_plen = htons(tcp_hdr_len - 23490 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23491 } 23492 23493 /* 23494 * Prime pump for checksum calculation in IP. Include the 23495 * adjustment for a source route if any. 23496 */ 23497 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23498 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23499 U16_TO_ABE16(data_length, tcph->th_sum); 23500 23501 if (tcp->tcp_ip_forward_progress) { 23502 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23503 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23504 tcp->tcp_ip_forward_progress = B_FALSE; 23505 } 23506 return (mp1); 23507 } 23508 } 23509 23510 /* 23511 * Hash list insertion routine for tcp_t structures. Each hash bucket 23512 * contains a list of tcp_t entries, and each entry is bound to a unique 23513 * port. If there are multiple tcp_t's that are bound to the same port, then 23514 * one of them will be linked into the hash bucket list, and the rest will 23515 * hang off of that one entry. For each port, entries bound to a specific IP 23516 * address will be inserted before those those bound to INADDR_ANY. 23517 */ 23518 static void 23519 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23520 { 23521 tcp_t **tcpp; 23522 tcp_t *tcpnext; 23523 tcp_t *tcphash; 23524 23525 if (tcp->tcp_ptpbhn != NULL) { 23526 ASSERT(!caller_holds_lock); 23527 tcp_bind_hash_remove(tcp); 23528 } 23529 tcpp = &tbf->tf_tcp; 23530 if (!caller_holds_lock) { 23531 mutex_enter(&tbf->tf_lock); 23532 } else { 23533 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23534 } 23535 tcphash = tcpp[0]; 23536 tcpnext = NULL; 23537 if (tcphash != NULL) { 23538 /* Look for an entry using the same port */ 23539 while ((tcphash = tcpp[0]) != NULL && 23540 tcp->tcp_lport != tcphash->tcp_lport) 23541 tcpp = &(tcphash->tcp_bind_hash); 23542 23543 /* The port was not found, just add to the end */ 23544 if (tcphash == NULL) 23545 goto insert; 23546 23547 /* 23548 * OK, there already exists an entry bound to the 23549 * same port. 23550 * 23551 * If the new tcp bound to the INADDR_ANY address 23552 * and the first one in the list is not bound to 23553 * INADDR_ANY we skip all entries until we find the 23554 * first one bound to INADDR_ANY. 23555 * This makes sure that applications binding to a 23556 * specific address get preference over those binding to 23557 * INADDR_ANY. 23558 */ 23559 tcpnext = tcphash; 23560 tcphash = NULL; 23561 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23562 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23563 while ((tcpnext = tcpp[0]) != NULL && 23564 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23565 tcpp = &(tcpnext->tcp_bind_hash_port); 23566 23567 if (tcpnext) { 23568 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23569 tcphash = tcpnext->tcp_bind_hash; 23570 if (tcphash != NULL) { 23571 tcphash->tcp_ptpbhn = 23572 &(tcp->tcp_bind_hash); 23573 tcpnext->tcp_bind_hash = NULL; 23574 } 23575 } 23576 } else { 23577 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23578 tcphash = tcpnext->tcp_bind_hash; 23579 if (tcphash != NULL) { 23580 tcphash->tcp_ptpbhn = 23581 &(tcp->tcp_bind_hash); 23582 tcpnext->tcp_bind_hash = NULL; 23583 } 23584 } 23585 } 23586 insert: 23587 tcp->tcp_bind_hash_port = tcpnext; 23588 tcp->tcp_bind_hash = tcphash; 23589 tcp->tcp_ptpbhn = tcpp; 23590 tcpp[0] = tcp; 23591 if (!caller_holds_lock) 23592 mutex_exit(&tbf->tf_lock); 23593 } 23594 23595 /* 23596 * Hash list removal routine for tcp_t structures. 23597 */ 23598 static void 23599 tcp_bind_hash_remove(tcp_t *tcp) 23600 { 23601 tcp_t *tcpnext; 23602 kmutex_t *lockp; 23603 tcp_stack_t *tcps = tcp->tcp_tcps; 23604 23605 if (tcp->tcp_ptpbhn == NULL) 23606 return; 23607 23608 /* 23609 * Extract the lock pointer in case there are concurrent 23610 * hash_remove's for this instance. 23611 */ 23612 ASSERT(tcp->tcp_lport != 0); 23613 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23614 23615 ASSERT(lockp != NULL); 23616 mutex_enter(lockp); 23617 if (tcp->tcp_ptpbhn) { 23618 tcpnext = tcp->tcp_bind_hash_port; 23619 if (tcpnext != NULL) { 23620 tcp->tcp_bind_hash_port = NULL; 23621 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23622 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23623 if (tcpnext->tcp_bind_hash != NULL) { 23624 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23625 &(tcpnext->tcp_bind_hash); 23626 tcp->tcp_bind_hash = NULL; 23627 } 23628 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23629 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23630 tcp->tcp_bind_hash = NULL; 23631 } 23632 *tcp->tcp_ptpbhn = tcpnext; 23633 tcp->tcp_ptpbhn = NULL; 23634 } 23635 mutex_exit(lockp); 23636 } 23637 23638 23639 /* 23640 * Hash list lookup routine for tcp_t structures. 23641 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23642 */ 23643 static tcp_t * 23644 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23645 { 23646 tf_t *tf; 23647 tcp_t *tcp; 23648 23649 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23650 mutex_enter(&tf->tf_lock); 23651 for (tcp = tf->tf_tcp; tcp != NULL; 23652 tcp = tcp->tcp_acceptor_hash) { 23653 if (tcp->tcp_acceptor_id == id) { 23654 CONN_INC_REF(tcp->tcp_connp); 23655 mutex_exit(&tf->tf_lock); 23656 return (tcp); 23657 } 23658 } 23659 mutex_exit(&tf->tf_lock); 23660 return (NULL); 23661 } 23662 23663 23664 /* 23665 * Hash list insertion routine for tcp_t structures. 23666 */ 23667 void 23668 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23669 { 23670 tf_t *tf; 23671 tcp_t **tcpp; 23672 tcp_t *tcpnext; 23673 tcp_stack_t *tcps = tcp->tcp_tcps; 23674 23675 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23676 23677 if (tcp->tcp_ptpahn != NULL) 23678 tcp_acceptor_hash_remove(tcp); 23679 tcpp = &tf->tf_tcp; 23680 mutex_enter(&tf->tf_lock); 23681 tcpnext = tcpp[0]; 23682 if (tcpnext) 23683 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23684 tcp->tcp_acceptor_hash = tcpnext; 23685 tcp->tcp_ptpahn = tcpp; 23686 tcpp[0] = tcp; 23687 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23688 mutex_exit(&tf->tf_lock); 23689 } 23690 23691 /* 23692 * Hash list removal routine for tcp_t structures. 23693 */ 23694 static void 23695 tcp_acceptor_hash_remove(tcp_t *tcp) 23696 { 23697 tcp_t *tcpnext; 23698 kmutex_t *lockp; 23699 23700 /* 23701 * Extract the lock pointer in case there are concurrent 23702 * hash_remove's for this instance. 23703 */ 23704 lockp = tcp->tcp_acceptor_lockp; 23705 23706 if (tcp->tcp_ptpahn == NULL) 23707 return; 23708 23709 ASSERT(lockp != NULL); 23710 mutex_enter(lockp); 23711 if (tcp->tcp_ptpahn) { 23712 tcpnext = tcp->tcp_acceptor_hash; 23713 if (tcpnext) { 23714 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23715 tcp->tcp_acceptor_hash = NULL; 23716 } 23717 *tcp->tcp_ptpahn = tcpnext; 23718 tcp->tcp_ptpahn = NULL; 23719 } 23720 mutex_exit(lockp); 23721 tcp->tcp_acceptor_lockp = NULL; 23722 } 23723 23724 /* 23725 * Type three generator adapted from the random() function in 4.4 BSD: 23726 */ 23727 23728 /* 23729 * Copyright (c) 1983, 1993 23730 * The Regents of the University of California. All rights reserved. 23731 * 23732 * Redistribution and use in source and binary forms, with or without 23733 * modification, are permitted provided that the following conditions 23734 * are met: 23735 * 1. Redistributions of source code must retain the above copyright 23736 * notice, this list of conditions and the following disclaimer. 23737 * 2. Redistributions in binary form must reproduce the above copyright 23738 * notice, this list of conditions and the following disclaimer in the 23739 * documentation and/or other materials provided with the distribution. 23740 * 3. All advertising materials mentioning features or use of this software 23741 * must display the following acknowledgement: 23742 * This product includes software developed by the University of 23743 * California, Berkeley and its contributors. 23744 * 4. Neither the name of the University nor the names of its contributors 23745 * may be used to endorse or promote products derived from this software 23746 * without specific prior written permission. 23747 * 23748 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23749 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23750 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23751 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23752 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23753 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23754 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23755 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23756 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23757 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23758 * SUCH DAMAGE. 23759 */ 23760 23761 /* Type 3 -- x**31 + x**3 + 1 */ 23762 #define DEG_3 31 23763 #define SEP_3 3 23764 23765 23766 /* Protected by tcp_random_lock */ 23767 static int tcp_randtbl[DEG_3 + 1]; 23768 23769 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23770 static int *tcp_random_rptr = &tcp_randtbl[1]; 23771 23772 static int *tcp_random_state = &tcp_randtbl[1]; 23773 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23774 23775 kmutex_t tcp_random_lock; 23776 23777 void 23778 tcp_random_init(void) 23779 { 23780 int i; 23781 hrtime_t hrt; 23782 time_t wallclock; 23783 uint64_t result; 23784 23785 /* 23786 * Use high-res timer and current time for seed. Gethrtime() returns 23787 * a longlong, which may contain resolution down to nanoseconds. 23788 * The current time will either be a 32-bit or a 64-bit quantity. 23789 * XOR the two together in a 64-bit result variable. 23790 * Convert the result to a 32-bit value by multiplying the high-order 23791 * 32-bits by the low-order 32-bits. 23792 */ 23793 23794 hrt = gethrtime(); 23795 (void) drv_getparm(TIME, &wallclock); 23796 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23797 mutex_enter(&tcp_random_lock); 23798 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23799 (result & 0xffffffff); 23800 23801 for (i = 1; i < DEG_3; i++) 23802 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23803 + 12345; 23804 tcp_random_fptr = &tcp_random_state[SEP_3]; 23805 tcp_random_rptr = &tcp_random_state[0]; 23806 mutex_exit(&tcp_random_lock); 23807 for (i = 0; i < 10 * DEG_3; i++) 23808 (void) tcp_random(); 23809 } 23810 23811 /* 23812 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23813 * This range is selected to be approximately centered on TCP_ISS / 2, 23814 * and easy to compute. We get this value by generating a 32-bit random 23815 * number, selecting out the high-order 17 bits, and then adding one so 23816 * that we never return zero. 23817 */ 23818 int 23819 tcp_random(void) 23820 { 23821 int i; 23822 23823 mutex_enter(&tcp_random_lock); 23824 *tcp_random_fptr += *tcp_random_rptr; 23825 23826 /* 23827 * The high-order bits are more random than the low-order bits, 23828 * so we select out the high-order 17 bits and add one so that 23829 * we never return zero. 23830 */ 23831 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23832 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23833 tcp_random_fptr = tcp_random_state; 23834 ++tcp_random_rptr; 23835 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23836 tcp_random_rptr = tcp_random_state; 23837 23838 mutex_exit(&tcp_random_lock); 23839 return (i); 23840 } 23841 23842 static int 23843 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23844 int *t_errorp, int *sys_errorp) 23845 { 23846 int error; 23847 int is_absreq_failure; 23848 t_scalar_t *opt_lenp; 23849 t_scalar_t opt_offset; 23850 int prim_type; 23851 struct T_conn_req *tcreqp; 23852 struct T_conn_res *tcresp; 23853 cred_t *cr; 23854 23855 /* 23856 * All Solaris components should pass a db_credp 23857 * for this TPI message, hence we ASSERT. 23858 * But in case there is some other M_PROTO that looks 23859 * like a TPI message sent by some other kernel 23860 * component, we check and return an error. 23861 */ 23862 cr = msg_getcred(mp, NULL); 23863 ASSERT(cr != NULL); 23864 if (cr == NULL) 23865 return (-1); 23866 23867 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23868 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23869 prim_type == T_CONN_RES); 23870 23871 switch (prim_type) { 23872 case T_CONN_REQ: 23873 tcreqp = (struct T_conn_req *)mp->b_rptr; 23874 opt_offset = tcreqp->OPT_offset; 23875 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23876 break; 23877 case O_T_CONN_RES: 23878 case T_CONN_RES: 23879 tcresp = (struct T_conn_res *)mp->b_rptr; 23880 opt_offset = tcresp->OPT_offset; 23881 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23882 break; 23883 } 23884 23885 *t_errorp = 0; 23886 *sys_errorp = 0; 23887 *do_disconnectp = 0; 23888 23889 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23890 opt_offset, cr, &tcp_opt_obj, 23891 NULL, &is_absreq_failure); 23892 23893 switch (error) { 23894 case 0: /* no error */ 23895 ASSERT(is_absreq_failure == 0); 23896 return (0); 23897 case ENOPROTOOPT: 23898 *t_errorp = TBADOPT; 23899 break; 23900 case EACCES: 23901 *t_errorp = TACCES; 23902 break; 23903 default: 23904 *t_errorp = TSYSERR; *sys_errorp = error; 23905 break; 23906 } 23907 if (is_absreq_failure != 0) { 23908 /* 23909 * The connection request should get the local ack 23910 * T_OK_ACK and then a T_DISCON_IND. 23911 */ 23912 *do_disconnectp = 1; 23913 } 23914 return (-1); 23915 } 23916 23917 /* 23918 * Split this function out so that if the secret changes, I'm okay. 23919 * 23920 * Initialize the tcp_iss_cookie and tcp_iss_key. 23921 */ 23922 23923 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23924 23925 static void 23926 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23927 { 23928 struct { 23929 int32_t current_time; 23930 uint32_t randnum; 23931 uint16_t pad; 23932 uint8_t ether[6]; 23933 uint8_t passwd[PASSWD_SIZE]; 23934 } tcp_iss_cookie; 23935 time_t t; 23936 23937 /* 23938 * Start with the current absolute time. 23939 */ 23940 (void) drv_getparm(TIME, &t); 23941 tcp_iss_cookie.current_time = t; 23942 23943 /* 23944 * XXX - Need a more random number per RFC 1750, not this crap. 23945 * OTOH, if what follows is pretty random, then I'm in better shape. 23946 */ 23947 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23948 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23949 23950 /* 23951 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23952 * as a good template. 23953 */ 23954 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23955 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23956 23957 /* 23958 * The pass-phrase. Normally this is supplied by user-called NDD. 23959 */ 23960 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23961 23962 /* 23963 * See 4010593 if this section becomes a problem again, 23964 * but the local ethernet address is useful here. 23965 */ 23966 (void) localetheraddr(NULL, 23967 (struct ether_addr *)&tcp_iss_cookie.ether); 23968 23969 /* 23970 * Hash 'em all together. The MD5Final is called per-connection. 23971 */ 23972 mutex_enter(&tcps->tcps_iss_key_lock); 23973 MD5Init(&tcps->tcps_iss_key); 23974 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23975 sizeof (tcp_iss_cookie)); 23976 mutex_exit(&tcps->tcps_iss_key_lock); 23977 } 23978 23979 /* 23980 * Set the RFC 1948 pass phrase 23981 */ 23982 /* ARGSUSED */ 23983 static int 23984 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23985 cred_t *cr) 23986 { 23987 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23988 23989 /* 23990 * Basically, value contains a new pass phrase. Pass it along! 23991 */ 23992 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23993 return (0); 23994 } 23995 23996 /* ARGSUSED */ 23997 static int 23998 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23999 { 24000 bzero(buf, sizeof (tcp_sack_info_t)); 24001 return (0); 24002 } 24003 24004 /* ARGSUSED */ 24005 static int 24006 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24007 { 24008 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24009 return (0); 24010 } 24011 24012 /* 24013 * Make sure we wait until the default queue is setup, yet allow 24014 * tcp_g_q_create() to open a TCP stream. 24015 * We need to allow tcp_g_q_create() do do an open 24016 * of tcp, hence we compare curhread. 24017 * All others have to wait until the tcps_g_q has been 24018 * setup. 24019 */ 24020 void 24021 tcp_g_q_setup(tcp_stack_t *tcps) 24022 { 24023 mutex_enter(&tcps->tcps_g_q_lock); 24024 if (tcps->tcps_g_q != NULL) { 24025 mutex_exit(&tcps->tcps_g_q_lock); 24026 return; 24027 } 24028 if (tcps->tcps_g_q_creator == NULL) { 24029 /* This thread will set it up */ 24030 tcps->tcps_g_q_creator = curthread; 24031 mutex_exit(&tcps->tcps_g_q_lock); 24032 tcp_g_q_create(tcps); 24033 mutex_enter(&tcps->tcps_g_q_lock); 24034 ASSERT(tcps->tcps_g_q_creator == curthread); 24035 tcps->tcps_g_q_creator = NULL; 24036 cv_signal(&tcps->tcps_g_q_cv); 24037 ASSERT(tcps->tcps_g_q != NULL); 24038 mutex_exit(&tcps->tcps_g_q_lock); 24039 return; 24040 } 24041 /* Everybody but the creator has to wait */ 24042 if (tcps->tcps_g_q_creator != curthread) { 24043 while (tcps->tcps_g_q == NULL) 24044 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24045 } 24046 mutex_exit(&tcps->tcps_g_q_lock); 24047 } 24048 24049 #define IP "ip" 24050 24051 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24052 24053 /* 24054 * Create a default tcp queue here instead of in strplumb 24055 */ 24056 void 24057 tcp_g_q_create(tcp_stack_t *tcps) 24058 { 24059 int error; 24060 ldi_handle_t lh = NULL; 24061 ldi_ident_t li = NULL; 24062 int rval; 24063 cred_t *cr; 24064 major_t IP_MAJ; 24065 24066 #ifdef NS_DEBUG 24067 (void) printf("tcp_g_q_create()\n"); 24068 #endif 24069 24070 IP_MAJ = ddi_name_to_major(IP); 24071 24072 ASSERT(tcps->tcps_g_q_creator == curthread); 24073 24074 error = ldi_ident_from_major(IP_MAJ, &li); 24075 if (error) { 24076 #ifdef DEBUG 24077 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24078 error); 24079 #endif 24080 return; 24081 } 24082 24083 cr = zone_get_kcred(netstackid_to_zoneid( 24084 tcps->tcps_netstack->netstack_stackid)); 24085 ASSERT(cr != NULL); 24086 /* 24087 * We set the tcp default queue to IPv6 because IPv4 falls 24088 * back to IPv6 when it can't find a client, but 24089 * IPv6 does not fall back to IPv4. 24090 */ 24091 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24092 if (error) { 24093 #ifdef DEBUG 24094 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24095 error); 24096 #endif 24097 goto out; 24098 } 24099 24100 /* 24101 * This ioctl causes the tcp framework to cache a pointer to 24102 * this stream, so we don't want to close the stream after 24103 * this operation. 24104 * Use the kernel credentials that are for the zone we're in. 24105 */ 24106 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24107 (intptr_t)0, FKIOCTL, cr, &rval); 24108 if (error) { 24109 #ifdef DEBUG 24110 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24111 "error %d\n", error); 24112 #endif 24113 goto out; 24114 } 24115 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24116 lh = NULL; 24117 out: 24118 /* Close layered handles */ 24119 if (li) 24120 ldi_ident_release(li); 24121 /* Keep cred around until _inactive needs it */ 24122 tcps->tcps_g_q_cr = cr; 24123 } 24124 24125 /* 24126 * We keep tcp_g_q set until all other tcp_t's in the zone 24127 * has gone away, and then when tcp_g_q_inactive() is called 24128 * we clear it. 24129 */ 24130 void 24131 tcp_g_q_destroy(tcp_stack_t *tcps) 24132 { 24133 #ifdef NS_DEBUG 24134 (void) printf("tcp_g_q_destroy()for stack %d\n", 24135 tcps->tcps_netstack->netstack_stackid); 24136 #endif 24137 24138 if (tcps->tcps_g_q == NULL) { 24139 return; /* Nothing to cleanup */ 24140 } 24141 /* 24142 * Drop reference corresponding to the default queue. 24143 * This reference was added from tcp_open when the default queue 24144 * was created, hence we compensate for this extra drop in 24145 * tcp_g_q_close. If the refcnt drops to zero here it means 24146 * the default queue was the last one to be open, in which 24147 * case, then tcp_g_q_inactive will be 24148 * called as a result of the refrele. 24149 */ 24150 TCPS_REFRELE(tcps); 24151 } 24152 24153 /* 24154 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24155 * Run by tcp_q_q_inactive using a taskq. 24156 */ 24157 static void 24158 tcp_g_q_close(void *arg) 24159 { 24160 tcp_stack_t *tcps = arg; 24161 int error; 24162 ldi_handle_t lh = NULL; 24163 ldi_ident_t li = NULL; 24164 cred_t *cr; 24165 major_t IP_MAJ; 24166 24167 IP_MAJ = ddi_name_to_major(IP); 24168 24169 #ifdef NS_DEBUG 24170 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24171 tcps->tcps_netstack->netstack_stackid, 24172 tcps->tcps_netstack->netstack_refcnt); 24173 #endif 24174 lh = tcps->tcps_g_q_lh; 24175 if (lh == NULL) 24176 return; /* Nothing to cleanup */ 24177 24178 ASSERT(tcps->tcps_refcnt == 1); 24179 ASSERT(tcps->tcps_g_q != NULL); 24180 24181 error = ldi_ident_from_major(IP_MAJ, &li); 24182 if (error) { 24183 #ifdef DEBUG 24184 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24185 error); 24186 #endif 24187 return; 24188 } 24189 24190 cr = tcps->tcps_g_q_cr; 24191 tcps->tcps_g_q_cr = NULL; 24192 ASSERT(cr != NULL); 24193 24194 /* 24195 * Make sure we can break the recursion when tcp_close decrements 24196 * the reference count causing g_q_inactive to be called again. 24197 */ 24198 tcps->tcps_g_q_lh = NULL; 24199 24200 /* close the default queue */ 24201 (void) ldi_close(lh, FREAD|FWRITE, cr); 24202 /* 24203 * At this point in time tcps and the rest of netstack_t might 24204 * have been deleted. 24205 */ 24206 tcps = NULL; 24207 24208 /* Close layered handles */ 24209 ldi_ident_release(li); 24210 crfree(cr); 24211 } 24212 24213 /* 24214 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24215 * 24216 * Have to ensure that the ldi routines are not used by an 24217 * interrupt thread by using a taskq. 24218 */ 24219 void 24220 tcp_g_q_inactive(tcp_stack_t *tcps) 24221 { 24222 if (tcps->tcps_g_q_lh == NULL) 24223 return; /* Nothing to cleanup */ 24224 24225 ASSERT(tcps->tcps_refcnt == 0); 24226 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24227 24228 if (servicing_interrupt()) { 24229 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24230 (void *) tcps, TQ_SLEEP); 24231 } else { 24232 tcp_g_q_close(tcps); 24233 } 24234 } 24235 24236 /* 24237 * Called by IP when IP is loaded into the kernel 24238 */ 24239 void 24240 tcp_ddi_g_init(void) 24241 { 24242 tcp_timercache = kmem_cache_create("tcp_timercache", 24243 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24244 NULL, NULL, NULL, NULL, NULL, 0); 24245 24246 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24247 sizeof (tcp_sack_info_t), 0, 24248 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24249 24250 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24251 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24252 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24253 24254 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24255 24256 /* Initialize the random number generator */ 24257 tcp_random_init(); 24258 24259 /* A single callback independently of how many netstacks we have */ 24260 ip_squeue_init(tcp_squeue_add); 24261 24262 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24263 24264 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24265 TASKQ_PREPOPULATE); 24266 24267 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24268 24269 /* 24270 * We want to be informed each time a stack is created or 24271 * destroyed in the kernel, so we can maintain the 24272 * set of tcp_stack_t's. 24273 */ 24274 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24275 tcp_stack_fini); 24276 } 24277 24278 24279 #define INET_NAME "ip" 24280 24281 /* 24282 * Initialize the TCP stack instance. 24283 */ 24284 static void * 24285 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24286 { 24287 tcp_stack_t *tcps; 24288 tcpparam_t *pa; 24289 int i; 24290 int error = 0; 24291 major_t major; 24292 24293 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24294 tcps->tcps_netstack = ns; 24295 24296 /* Initialize locks */ 24297 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24298 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24299 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24300 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24301 24302 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24303 tcps->tcps_g_epriv_ports[0] = 2049; 24304 tcps->tcps_g_epriv_ports[1] = 4045; 24305 tcps->tcps_min_anonpriv_port = 512; 24306 24307 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24308 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24309 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24310 TCP_FANOUT_SIZE, KM_SLEEP); 24311 24312 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24313 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24314 MUTEX_DEFAULT, NULL); 24315 } 24316 24317 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24318 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24319 MUTEX_DEFAULT, NULL); 24320 } 24321 24322 /* TCP's IPsec code calls the packet dropper. */ 24323 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24324 24325 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24326 tcps->tcps_params = pa; 24327 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24328 24329 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24330 A_CNT(lcl_tcp_param_arr), tcps); 24331 24332 /* 24333 * Note: To really walk the device tree you need the devinfo 24334 * pointer to your device which is only available after probe/attach. 24335 * The following is safe only because it uses ddi_root_node() 24336 */ 24337 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24338 tcp_opt_obj.odb_opt_arr_cnt); 24339 24340 /* 24341 * Initialize RFC 1948 secret values. This will probably be reset once 24342 * by the boot scripts. 24343 * 24344 * Use NULL name, as the name is caught by the new lockstats. 24345 * 24346 * Initialize with some random, non-guessable string, like the global 24347 * T_INFO_ACK. 24348 */ 24349 24350 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24351 sizeof (tcp_g_t_info_ack), tcps); 24352 24353 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24354 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24355 24356 major = mod_name_to_major(INET_NAME); 24357 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24358 ASSERT(error == 0); 24359 return (tcps); 24360 } 24361 24362 /* 24363 * Called when the IP module is about to be unloaded. 24364 */ 24365 void 24366 tcp_ddi_g_destroy(void) 24367 { 24368 tcp_g_kstat_fini(tcp_g_kstat); 24369 tcp_g_kstat = NULL; 24370 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24371 24372 mutex_destroy(&tcp_random_lock); 24373 24374 kmem_cache_destroy(tcp_timercache); 24375 kmem_cache_destroy(tcp_sack_info_cache); 24376 kmem_cache_destroy(tcp_iphc_cache); 24377 24378 netstack_unregister(NS_TCP); 24379 taskq_destroy(tcp_taskq); 24380 } 24381 24382 /* 24383 * Shut down the TCP stack instance. 24384 */ 24385 /* ARGSUSED */ 24386 static void 24387 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24388 { 24389 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24390 24391 tcp_g_q_destroy(tcps); 24392 } 24393 24394 /* 24395 * Free the TCP stack instance. 24396 */ 24397 static void 24398 tcp_stack_fini(netstackid_t stackid, void *arg) 24399 { 24400 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24401 int i; 24402 24403 nd_free(&tcps->tcps_g_nd); 24404 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24405 tcps->tcps_params = NULL; 24406 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24407 tcps->tcps_wroff_xtra_param = NULL; 24408 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24409 tcps->tcps_mdt_head_param = NULL; 24410 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24411 tcps->tcps_mdt_tail_param = NULL; 24412 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 24413 tcps->tcps_mdt_max_pbufs_param = NULL; 24414 24415 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24416 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 24417 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 24418 } 24419 24420 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24421 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 24422 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 24423 } 24424 24425 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 24426 tcps->tcps_bind_fanout = NULL; 24427 24428 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 24429 tcps->tcps_acceptor_fanout = NULL; 24430 24431 mutex_destroy(&tcps->tcps_iss_key_lock); 24432 mutex_destroy(&tcps->tcps_g_q_lock); 24433 cv_destroy(&tcps->tcps_g_q_cv); 24434 mutex_destroy(&tcps->tcps_epriv_port_lock); 24435 24436 ip_drop_unregister(&tcps->tcps_dropper); 24437 24438 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 24439 tcps->tcps_kstat = NULL; 24440 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 24441 24442 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 24443 tcps->tcps_mibkp = NULL; 24444 24445 ldi_ident_release(tcps->tcps_ldi_ident); 24446 kmem_free(tcps, sizeof (*tcps)); 24447 } 24448 24449 /* 24450 * Generate ISS, taking into account NDD changes may happen halfway through. 24451 * (If the iss is not zero, set it.) 24452 */ 24453 24454 static void 24455 tcp_iss_init(tcp_t *tcp) 24456 { 24457 MD5_CTX context; 24458 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24459 uint32_t answer[4]; 24460 tcp_stack_t *tcps = tcp->tcp_tcps; 24461 24462 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 24463 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 24464 switch (tcps->tcps_strong_iss) { 24465 case 2: 24466 mutex_enter(&tcps->tcps_iss_key_lock); 24467 context = tcps->tcps_iss_key; 24468 mutex_exit(&tcps->tcps_iss_key_lock); 24469 arg.ports = tcp->tcp_ports; 24470 if (tcp->tcp_ipversion == IPV4_VERSION) { 24471 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24472 &arg.src); 24473 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24474 &arg.dst); 24475 } else { 24476 arg.src = tcp->tcp_ip6h->ip6_src; 24477 arg.dst = tcp->tcp_ip6h->ip6_dst; 24478 } 24479 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24480 MD5Final((uchar_t *)answer, &context); 24481 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24482 /* 24483 * Now that we've hashed into a unique per-connection sequence 24484 * space, add a random increment per strong_iss == 1. So I 24485 * guess we'll have to... 24486 */ 24487 /* FALLTHRU */ 24488 case 1: 24489 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24490 break; 24491 default: 24492 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24493 break; 24494 } 24495 tcp->tcp_valid_bits = TCP_ISS_VALID; 24496 tcp->tcp_fss = tcp->tcp_iss - 1; 24497 tcp->tcp_suna = tcp->tcp_iss; 24498 tcp->tcp_snxt = tcp->tcp_iss + 1; 24499 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24500 tcp->tcp_csuna = tcp->tcp_snxt; 24501 } 24502 24503 /* 24504 * Exported routine for extracting active tcp connection status. 24505 * 24506 * This is used by the Solaris Cluster Networking software to 24507 * gather a list of connections that need to be forwarded to 24508 * specific nodes in the cluster when configuration changes occur. 24509 * 24510 * The callback is invoked for each tcp_t structure from all netstacks, 24511 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24512 * from the netstack with the specified stack_id. Returning 24513 * non-zero from the callback routine terminates the search. 24514 */ 24515 int 24516 cl_tcp_walk_list(netstackid_t stack_id, 24517 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24518 { 24519 netstack_handle_t nh; 24520 netstack_t *ns; 24521 int ret = 0; 24522 24523 if (stack_id >= 0) { 24524 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24525 return (EINVAL); 24526 24527 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24528 ns->netstack_tcp); 24529 netstack_rele(ns); 24530 return (ret); 24531 } 24532 24533 netstack_next_init(&nh); 24534 while ((ns = netstack_next(&nh)) != NULL) { 24535 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24536 ns->netstack_tcp); 24537 netstack_rele(ns); 24538 } 24539 netstack_next_fini(&nh); 24540 return (ret); 24541 } 24542 24543 static int 24544 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24545 tcp_stack_t *tcps) 24546 { 24547 tcp_t *tcp; 24548 cl_tcp_info_t cl_tcpi; 24549 connf_t *connfp; 24550 conn_t *connp; 24551 int i; 24552 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24553 24554 ASSERT(callback != NULL); 24555 24556 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24557 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24558 connp = NULL; 24559 24560 while ((connp = 24561 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24562 24563 tcp = connp->conn_tcp; 24564 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24565 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24566 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24567 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24568 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24569 /* 24570 * The macros tcp_laddr and tcp_faddr give the IPv4 24571 * addresses. They are copied implicitly below as 24572 * mapped addresses. 24573 */ 24574 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24575 if (tcp->tcp_ipversion == IPV4_VERSION) { 24576 cl_tcpi.cl_tcpi_faddr = 24577 tcp->tcp_ipha->ipha_dst; 24578 } else { 24579 cl_tcpi.cl_tcpi_faddr_v6 = 24580 tcp->tcp_ip6h->ip6_dst; 24581 } 24582 24583 /* 24584 * If the callback returns non-zero 24585 * we terminate the traversal. 24586 */ 24587 if ((*callback)(&cl_tcpi, arg) != 0) { 24588 CONN_DEC_REF(tcp->tcp_connp); 24589 return (1); 24590 } 24591 } 24592 } 24593 24594 return (0); 24595 } 24596 24597 /* 24598 * Macros used for accessing the different types of sockaddr 24599 * structures inside a tcp_ioc_abort_conn_t. 24600 */ 24601 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24602 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24603 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24604 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24605 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24606 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24607 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24608 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24609 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24610 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24611 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24612 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24613 24614 /* 24615 * Return the correct error code to mimic the behavior 24616 * of a connection reset. 24617 */ 24618 #define TCP_AC_GET_ERRCODE(state, err) { \ 24619 switch ((state)) { \ 24620 case TCPS_SYN_SENT: \ 24621 case TCPS_SYN_RCVD: \ 24622 (err) = ECONNREFUSED; \ 24623 break; \ 24624 case TCPS_ESTABLISHED: \ 24625 case TCPS_FIN_WAIT_1: \ 24626 case TCPS_FIN_WAIT_2: \ 24627 case TCPS_CLOSE_WAIT: \ 24628 (err) = ECONNRESET; \ 24629 break; \ 24630 case TCPS_CLOSING: \ 24631 case TCPS_LAST_ACK: \ 24632 case TCPS_TIME_WAIT: \ 24633 (err) = 0; \ 24634 break; \ 24635 default: \ 24636 (err) = ENXIO; \ 24637 } \ 24638 } 24639 24640 /* 24641 * Check if a tcp structure matches the info in acp. 24642 */ 24643 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24644 (((acp)->ac_local.ss_family == AF_INET) ? \ 24645 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24646 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24647 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24648 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24649 (TCP_AC_V4LPORT((acp)) == 0 || \ 24650 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24651 (TCP_AC_V4RPORT((acp)) == 0 || \ 24652 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24653 (acp)->ac_start <= (tcp)->tcp_state && \ 24654 (acp)->ac_end >= (tcp)->tcp_state) : \ 24655 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24656 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24657 &(tcp)->tcp_ip_src_v6)) && \ 24658 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24659 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24660 &(tcp)->tcp_remote_v6)) && \ 24661 (TCP_AC_V6LPORT((acp)) == 0 || \ 24662 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24663 (TCP_AC_V6RPORT((acp)) == 0 || \ 24664 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24665 (acp)->ac_start <= (tcp)->tcp_state && \ 24666 (acp)->ac_end >= (tcp)->tcp_state)) 24667 24668 #define TCP_AC_MATCH(acp, tcp) \ 24669 (((acp)->ac_zoneid == ALL_ZONES || \ 24670 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24671 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24672 24673 /* 24674 * Build a message containing a tcp_ioc_abort_conn_t structure 24675 * which is filled in with information from acp and tp. 24676 */ 24677 static mblk_t * 24678 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24679 { 24680 mblk_t *mp; 24681 tcp_ioc_abort_conn_t *tacp; 24682 24683 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24684 if (mp == NULL) 24685 return (NULL); 24686 24687 mp->b_datap->db_type = M_CTL; 24688 24689 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24690 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24691 sizeof (uint32_t)); 24692 24693 tacp->ac_start = acp->ac_start; 24694 tacp->ac_end = acp->ac_end; 24695 tacp->ac_zoneid = acp->ac_zoneid; 24696 24697 if (acp->ac_local.ss_family == AF_INET) { 24698 tacp->ac_local.ss_family = AF_INET; 24699 tacp->ac_remote.ss_family = AF_INET; 24700 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24701 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24702 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24703 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24704 } else { 24705 tacp->ac_local.ss_family = AF_INET6; 24706 tacp->ac_remote.ss_family = AF_INET6; 24707 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24708 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24709 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24710 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24711 } 24712 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24713 return (mp); 24714 } 24715 24716 /* 24717 * Print a tcp_ioc_abort_conn_t structure. 24718 */ 24719 static void 24720 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24721 { 24722 char lbuf[128]; 24723 char rbuf[128]; 24724 sa_family_t af; 24725 in_port_t lport, rport; 24726 ushort_t logflags; 24727 24728 af = acp->ac_local.ss_family; 24729 24730 if (af == AF_INET) { 24731 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24732 lbuf, 128); 24733 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24734 rbuf, 128); 24735 lport = ntohs(TCP_AC_V4LPORT(acp)); 24736 rport = ntohs(TCP_AC_V4RPORT(acp)); 24737 } else { 24738 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24739 lbuf, 128); 24740 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24741 rbuf, 128); 24742 lport = ntohs(TCP_AC_V6LPORT(acp)); 24743 rport = ntohs(TCP_AC_V6RPORT(acp)); 24744 } 24745 24746 logflags = SL_TRACE | SL_NOTE; 24747 /* 24748 * Don't print this message to the console if the operation was done 24749 * to a non-global zone. 24750 */ 24751 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24752 logflags |= SL_CONSOLE; 24753 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24754 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24755 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24756 acp->ac_start, acp->ac_end); 24757 } 24758 24759 /* 24760 * Called inside tcp_rput when a message built using 24761 * tcp_ioctl_abort_build_msg is put into a queue. 24762 * Note that when we get here there is no wildcard in acp any more. 24763 */ 24764 static void 24765 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24766 { 24767 tcp_ioc_abort_conn_t *acp; 24768 24769 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24770 if (tcp->tcp_state <= acp->ac_end) { 24771 /* 24772 * If we get here, we are already on the correct 24773 * squeue. This ioctl follows the following path 24774 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24775 * ->tcp_ioctl_abort->squeue_enter (if on a 24776 * different squeue) 24777 */ 24778 int errcode; 24779 24780 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24781 (void) tcp_clean_death(tcp, errcode, 26); 24782 } 24783 freemsg(mp); 24784 } 24785 24786 /* 24787 * Abort all matching connections on a hash chain. 24788 */ 24789 static int 24790 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24791 boolean_t exact, tcp_stack_t *tcps) 24792 { 24793 int nmatch, err = 0; 24794 tcp_t *tcp; 24795 MBLKP mp, last, listhead = NULL; 24796 conn_t *tconnp; 24797 connf_t *connfp; 24798 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24799 24800 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24801 24802 startover: 24803 nmatch = 0; 24804 24805 mutex_enter(&connfp->connf_lock); 24806 for (tconnp = connfp->connf_head; tconnp != NULL; 24807 tconnp = tconnp->conn_next) { 24808 tcp = tconnp->conn_tcp; 24809 if (TCP_AC_MATCH(acp, tcp)) { 24810 CONN_INC_REF(tcp->tcp_connp); 24811 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24812 if (mp == NULL) { 24813 err = ENOMEM; 24814 CONN_DEC_REF(tcp->tcp_connp); 24815 break; 24816 } 24817 mp->b_prev = (mblk_t *)tcp; 24818 24819 if (listhead == NULL) { 24820 listhead = mp; 24821 last = mp; 24822 } else { 24823 last->b_next = mp; 24824 last = mp; 24825 } 24826 nmatch++; 24827 if (exact) 24828 break; 24829 } 24830 24831 /* Avoid holding lock for too long. */ 24832 if (nmatch >= 500) 24833 break; 24834 } 24835 mutex_exit(&connfp->connf_lock); 24836 24837 /* Pass mp into the correct tcp */ 24838 while ((mp = listhead) != NULL) { 24839 listhead = listhead->b_next; 24840 tcp = (tcp_t *)mp->b_prev; 24841 mp->b_next = mp->b_prev = NULL; 24842 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24843 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24844 } 24845 24846 *count += nmatch; 24847 if (nmatch >= 500 && err == 0) 24848 goto startover; 24849 return (err); 24850 } 24851 24852 /* 24853 * Abort all connections that matches the attributes specified in acp. 24854 */ 24855 static int 24856 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24857 { 24858 sa_family_t af; 24859 uint32_t ports; 24860 uint16_t *pports; 24861 int err = 0, count = 0; 24862 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24863 int index = -1; 24864 ushort_t logflags; 24865 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24866 24867 af = acp->ac_local.ss_family; 24868 24869 if (af == AF_INET) { 24870 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24871 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24872 pports = (uint16_t *)&ports; 24873 pports[1] = TCP_AC_V4LPORT(acp); 24874 pports[0] = TCP_AC_V4RPORT(acp); 24875 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24876 } 24877 } else { 24878 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24879 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24880 pports = (uint16_t *)&ports; 24881 pports[1] = TCP_AC_V6LPORT(acp); 24882 pports[0] = TCP_AC_V6RPORT(acp); 24883 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24884 } 24885 } 24886 24887 /* 24888 * For cases where remote addr, local port, and remote port are non- 24889 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24890 */ 24891 if (index != -1) { 24892 err = tcp_ioctl_abort_bucket(acp, index, 24893 &count, exact, tcps); 24894 } else { 24895 /* 24896 * loop through all entries for wildcard case 24897 */ 24898 for (index = 0; 24899 index < ipst->ips_ipcl_conn_fanout_size; 24900 index++) { 24901 err = tcp_ioctl_abort_bucket(acp, index, 24902 &count, exact, tcps); 24903 if (err != 0) 24904 break; 24905 } 24906 } 24907 24908 logflags = SL_TRACE | SL_NOTE; 24909 /* 24910 * Don't print this message to the console if the operation was done 24911 * to a non-global zone. 24912 */ 24913 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24914 logflags |= SL_CONSOLE; 24915 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24916 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24917 if (err == 0 && count == 0) 24918 err = ENOENT; 24919 return (err); 24920 } 24921 24922 /* 24923 * Process the TCP_IOC_ABORT_CONN ioctl request. 24924 */ 24925 static void 24926 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24927 { 24928 int err; 24929 IOCP iocp; 24930 MBLKP mp1; 24931 sa_family_t laf, raf; 24932 tcp_ioc_abort_conn_t *acp; 24933 zone_t *zptr; 24934 conn_t *connp = Q_TO_CONN(q); 24935 zoneid_t zoneid = connp->conn_zoneid; 24936 tcp_t *tcp = connp->conn_tcp; 24937 tcp_stack_t *tcps = tcp->tcp_tcps; 24938 24939 iocp = (IOCP)mp->b_rptr; 24940 24941 if ((mp1 = mp->b_cont) == NULL || 24942 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24943 err = EINVAL; 24944 goto out; 24945 } 24946 24947 /* check permissions */ 24948 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24949 err = EPERM; 24950 goto out; 24951 } 24952 24953 if (mp1->b_cont != NULL) { 24954 freemsg(mp1->b_cont); 24955 mp1->b_cont = NULL; 24956 } 24957 24958 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24959 laf = acp->ac_local.ss_family; 24960 raf = acp->ac_remote.ss_family; 24961 24962 /* check that a zone with the supplied zoneid exists */ 24963 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24964 zptr = zone_find_by_id(zoneid); 24965 if (zptr != NULL) { 24966 zone_rele(zptr); 24967 } else { 24968 err = EINVAL; 24969 goto out; 24970 } 24971 } 24972 24973 /* 24974 * For exclusive stacks we set the zoneid to zero 24975 * to make TCP operate as if in the global zone. 24976 */ 24977 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24978 acp->ac_zoneid = GLOBAL_ZONEID; 24979 24980 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24981 acp->ac_start > acp->ac_end || laf != raf || 24982 (laf != AF_INET && laf != AF_INET6)) { 24983 err = EINVAL; 24984 goto out; 24985 } 24986 24987 tcp_ioctl_abort_dump(acp); 24988 err = tcp_ioctl_abort(acp, tcps); 24989 24990 out: 24991 if (mp1 != NULL) { 24992 freemsg(mp1); 24993 mp->b_cont = NULL; 24994 } 24995 24996 if (err != 0) 24997 miocnak(q, mp, 0, err); 24998 else 24999 miocack(q, mp, 0, 0); 25000 } 25001 25002 /* 25003 * tcp_time_wait_processing() handles processing of incoming packets when 25004 * the tcp is in the TIME_WAIT state. 25005 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25006 * on the time wait list. 25007 */ 25008 void 25009 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25010 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25011 { 25012 int32_t bytes_acked; 25013 int32_t gap; 25014 int32_t rgap; 25015 tcp_opt_t tcpopt; 25016 uint_t flags; 25017 uint32_t new_swnd = 0; 25018 conn_t *connp; 25019 tcp_stack_t *tcps = tcp->tcp_tcps; 25020 25021 BUMP_LOCAL(tcp->tcp_ibsegs); 25022 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25023 25024 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25025 new_swnd = BE16_TO_U16(tcph->th_win) << 25026 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25027 if (tcp->tcp_snd_ts_ok) { 25028 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25029 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25030 tcp->tcp_rnxt, TH_ACK); 25031 goto done; 25032 } 25033 } 25034 gap = seg_seq - tcp->tcp_rnxt; 25035 rgap = tcp->tcp_rwnd - (gap + seg_len); 25036 if (gap < 0) { 25037 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25038 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25039 (seg_len > -gap ? -gap : seg_len)); 25040 seg_len += gap; 25041 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25042 if (flags & TH_RST) { 25043 goto done; 25044 } 25045 if ((flags & TH_FIN) && seg_len == -1) { 25046 /* 25047 * When TCP receives a duplicate FIN in 25048 * TIME_WAIT state, restart the 2 MSL timer. 25049 * See page 73 in RFC 793. Make sure this TCP 25050 * is already on the TIME_WAIT list. If not, 25051 * just restart the timer. 25052 */ 25053 if (TCP_IS_DETACHED(tcp)) { 25054 if (tcp_time_wait_remove(tcp, NULL) == 25055 B_TRUE) { 25056 tcp_time_wait_append(tcp); 25057 TCP_DBGSTAT(tcps, 25058 tcp_rput_time_wait); 25059 } 25060 } else { 25061 ASSERT(tcp != NULL); 25062 TCP_TIMER_RESTART(tcp, 25063 tcps->tcps_time_wait_interval); 25064 } 25065 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25066 tcp->tcp_rnxt, TH_ACK); 25067 goto done; 25068 } 25069 flags |= TH_ACK_NEEDED; 25070 seg_len = 0; 25071 goto process_ack; 25072 } 25073 25074 /* Fix seg_seq, and chew the gap off the front. */ 25075 seg_seq = tcp->tcp_rnxt; 25076 } 25077 25078 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25079 /* 25080 * Make sure that when we accept the connection, pick 25081 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25082 * old connection. 25083 * 25084 * The next ISS generated is equal to tcp_iss_incr_extra 25085 * + ISS_INCR/2 + other components depending on the 25086 * value of tcp_strong_iss. We pre-calculate the new 25087 * ISS here and compare with tcp_snxt to determine if 25088 * we need to make adjustment to tcp_iss_incr_extra. 25089 * 25090 * The above calculation is ugly and is a 25091 * waste of CPU cycles... 25092 */ 25093 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25094 int32_t adj; 25095 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25096 25097 switch (tcps->tcps_strong_iss) { 25098 case 2: { 25099 /* Add time and MD5 components. */ 25100 uint32_t answer[4]; 25101 struct { 25102 uint32_t ports; 25103 in6_addr_t src; 25104 in6_addr_t dst; 25105 } arg; 25106 MD5_CTX context; 25107 25108 mutex_enter(&tcps->tcps_iss_key_lock); 25109 context = tcps->tcps_iss_key; 25110 mutex_exit(&tcps->tcps_iss_key_lock); 25111 arg.ports = tcp->tcp_ports; 25112 /* We use MAPPED addresses in tcp_iss_init */ 25113 arg.src = tcp->tcp_ip_src_v6; 25114 if (tcp->tcp_ipversion == IPV4_VERSION) { 25115 IN6_IPADDR_TO_V4MAPPED( 25116 tcp->tcp_ipha->ipha_dst, 25117 &arg.dst); 25118 } else { 25119 arg.dst = 25120 tcp->tcp_ip6h->ip6_dst; 25121 } 25122 MD5Update(&context, (uchar_t *)&arg, 25123 sizeof (arg)); 25124 MD5Final((uchar_t *)answer, &context); 25125 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25126 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25127 break; 25128 } 25129 case 1: 25130 /* Add time component and min random (i.e. 1). */ 25131 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25132 break; 25133 default: 25134 /* Add only time component. */ 25135 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25136 break; 25137 } 25138 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25139 /* 25140 * New ISS not guaranteed to be ISS_INCR/2 25141 * ahead of the current tcp_snxt, so add the 25142 * difference to tcp_iss_incr_extra. 25143 */ 25144 tcps->tcps_iss_incr_extra += adj; 25145 } 25146 /* 25147 * If tcp_clean_death() can not perform the task now, 25148 * drop the SYN packet and let the other side re-xmit. 25149 * Otherwise pass the SYN packet back in, since the 25150 * old tcp state has been cleaned up or freed. 25151 */ 25152 if (tcp_clean_death(tcp, 0, 27) == -1) 25153 goto done; 25154 /* 25155 * We will come back to tcp_rput_data 25156 * on the global queue. Packets destined 25157 * for the global queue will be checked 25158 * with global policy. But the policy for 25159 * this packet has already been checked as 25160 * this was destined for the detached 25161 * connection. We need to bypass policy 25162 * check this time by attaching a dummy 25163 * ipsec_in with ipsec_in_dont_check set. 25164 */ 25165 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25166 if (connp != NULL) { 25167 TCP_STAT(tcps, tcp_time_wait_syn_success); 25168 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25169 return; 25170 } 25171 goto done; 25172 } 25173 25174 /* 25175 * rgap is the amount of stuff received out of window. A negative 25176 * value is the amount out of window. 25177 */ 25178 if (rgap < 0) { 25179 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25180 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25181 /* Fix seg_len and make sure there is something left. */ 25182 seg_len += rgap; 25183 if (seg_len <= 0) { 25184 if (flags & TH_RST) { 25185 goto done; 25186 } 25187 flags |= TH_ACK_NEEDED; 25188 seg_len = 0; 25189 goto process_ack; 25190 } 25191 } 25192 /* 25193 * Check whether we can update tcp_ts_recent. This test is 25194 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25195 * Extensions for High Performance: An Update", Internet Draft. 25196 */ 25197 if (tcp->tcp_snd_ts_ok && 25198 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25199 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25200 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25201 tcp->tcp_last_rcv_lbolt = lbolt64; 25202 } 25203 25204 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25205 /* Always ack out of order packets */ 25206 flags |= TH_ACK_NEEDED; 25207 seg_len = 0; 25208 } else if (seg_len > 0) { 25209 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25210 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25211 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25212 } 25213 if (flags & TH_RST) { 25214 (void) tcp_clean_death(tcp, 0, 28); 25215 goto done; 25216 } 25217 if (flags & TH_SYN) { 25218 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25219 TH_RST|TH_ACK); 25220 /* 25221 * Do not delete the TCP structure if it is in 25222 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25223 */ 25224 goto done; 25225 } 25226 process_ack: 25227 if (flags & TH_ACK) { 25228 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25229 if (bytes_acked <= 0) { 25230 if (bytes_acked == 0 && seg_len == 0 && 25231 new_swnd == tcp->tcp_swnd) 25232 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25233 } else { 25234 /* Acks something not sent */ 25235 flags |= TH_ACK_NEEDED; 25236 } 25237 } 25238 if (flags & TH_ACK_NEEDED) { 25239 /* 25240 * Time to send an ack for some reason. 25241 */ 25242 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25243 tcp->tcp_rnxt, TH_ACK); 25244 } 25245 done: 25246 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25247 DB_CKSUMSTART(mp) = 0; 25248 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25249 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25250 } 25251 freemsg(mp); 25252 } 25253 25254 /* 25255 * TCP Timers Implementation. 25256 */ 25257 timeout_id_t 25258 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25259 { 25260 mblk_t *mp; 25261 tcp_timer_t *tcpt; 25262 tcp_t *tcp = connp->conn_tcp; 25263 25264 ASSERT(connp->conn_sqp != NULL); 25265 25266 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25267 25268 if (tcp->tcp_timercache == NULL) { 25269 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25270 } else { 25271 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25272 mp = tcp->tcp_timercache; 25273 tcp->tcp_timercache = mp->b_next; 25274 mp->b_next = NULL; 25275 ASSERT(mp->b_wptr == NULL); 25276 } 25277 25278 CONN_INC_REF(connp); 25279 tcpt = (tcp_timer_t *)mp->b_rptr; 25280 tcpt->connp = connp; 25281 tcpt->tcpt_proc = f; 25282 /* 25283 * TCP timers are normal timeouts. Plus, they do not require more than 25284 * a 10 millisecond resolution. By choosing a coarser resolution and by 25285 * rounding up the expiration to the next resolution boundary, we can 25286 * batch timers in the callout subsystem to make TCP timers more 25287 * efficient. The roundup also protects short timers from expiring too 25288 * early before they have a chance to be cancelled. 25289 */ 25290 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25291 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25292 25293 return ((timeout_id_t)mp); 25294 } 25295 25296 static void 25297 tcp_timer_callback(void *arg) 25298 { 25299 mblk_t *mp = (mblk_t *)arg; 25300 tcp_timer_t *tcpt; 25301 conn_t *connp; 25302 25303 tcpt = (tcp_timer_t *)mp->b_rptr; 25304 connp = tcpt->connp; 25305 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25306 SQ_FILL, SQTAG_TCP_TIMER); 25307 } 25308 25309 static void 25310 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25311 { 25312 tcp_timer_t *tcpt; 25313 conn_t *connp = (conn_t *)arg; 25314 tcp_t *tcp = connp->conn_tcp; 25315 25316 tcpt = (tcp_timer_t *)mp->b_rptr; 25317 ASSERT(connp == tcpt->connp); 25318 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25319 25320 /* 25321 * If the TCP has reached the closed state, don't proceed any 25322 * further. This TCP logically does not exist on the system. 25323 * tcpt_proc could for example access queues, that have already 25324 * been qprocoff'ed off. Also see comments at the start of tcp_input 25325 */ 25326 if (tcp->tcp_state != TCPS_CLOSED) { 25327 (*tcpt->tcpt_proc)(connp); 25328 } else { 25329 tcp->tcp_timer_tid = 0; 25330 } 25331 tcp_timer_free(connp->conn_tcp, mp); 25332 } 25333 25334 /* 25335 * There is potential race with untimeout and the handler firing at the same 25336 * time. The mblock may be freed by the handler while we are trying to use 25337 * it. But since both should execute on the same squeue, this race should not 25338 * occur. 25339 */ 25340 clock_t 25341 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25342 { 25343 mblk_t *mp = (mblk_t *)id; 25344 tcp_timer_t *tcpt; 25345 clock_t delta; 25346 25347 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25348 25349 if (mp == NULL) 25350 return (-1); 25351 25352 tcpt = (tcp_timer_t *)mp->b_rptr; 25353 ASSERT(tcpt->connp == connp); 25354 25355 delta = untimeout_default(tcpt->tcpt_tid, 0); 25356 25357 if (delta >= 0) { 25358 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25359 tcp_timer_free(connp->conn_tcp, mp); 25360 CONN_DEC_REF(connp); 25361 } 25362 25363 return (delta); 25364 } 25365 25366 /* 25367 * Allocate space for the timer event. The allocation looks like mblk, but it is 25368 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25369 * 25370 * Dealing with failures: If we can't allocate from the timer cache we try 25371 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25372 * points to b_rptr. 25373 * If we can't allocate anything using allocb_tryhard(), we perform a last 25374 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25375 * save the actual allocation size in b_datap. 25376 */ 25377 mblk_t * 25378 tcp_timermp_alloc(int kmflags) 25379 { 25380 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25381 kmflags & ~KM_PANIC); 25382 25383 if (mp != NULL) { 25384 mp->b_next = mp->b_prev = NULL; 25385 mp->b_rptr = (uchar_t *)(&mp[1]); 25386 mp->b_wptr = NULL; 25387 mp->b_datap = NULL; 25388 mp->b_queue = NULL; 25389 mp->b_cont = NULL; 25390 } else if (kmflags & KM_PANIC) { 25391 /* 25392 * Failed to allocate memory for the timer. Try allocating from 25393 * dblock caches. 25394 */ 25395 /* ipclassifier calls this from a constructor - hence no tcps */ 25396 TCP_G_STAT(tcp_timermp_allocfail); 25397 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25398 if (mp == NULL) { 25399 size_t size = 0; 25400 /* 25401 * Memory is really low. Try tryhard allocation. 25402 * 25403 * ipclassifier calls this from a constructor - 25404 * hence no tcps 25405 */ 25406 TCP_G_STAT(tcp_timermp_allocdblfail); 25407 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25408 sizeof (tcp_timer_t), &size, kmflags); 25409 mp->b_rptr = (uchar_t *)(&mp[1]); 25410 mp->b_next = mp->b_prev = NULL; 25411 mp->b_wptr = (uchar_t *)-1; 25412 mp->b_datap = (dblk_t *)size; 25413 mp->b_queue = NULL; 25414 mp->b_cont = NULL; 25415 } 25416 ASSERT(mp->b_wptr != NULL); 25417 } 25418 /* ipclassifier calls this from a constructor - hence no tcps */ 25419 TCP_G_DBGSTAT(tcp_timermp_alloced); 25420 25421 return (mp); 25422 } 25423 25424 /* 25425 * Free per-tcp timer cache. 25426 * It can only contain entries from tcp_timercache. 25427 */ 25428 void 25429 tcp_timermp_free(tcp_t *tcp) 25430 { 25431 mblk_t *mp; 25432 25433 while ((mp = tcp->tcp_timercache) != NULL) { 25434 ASSERT(mp->b_wptr == NULL); 25435 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25436 kmem_cache_free(tcp_timercache, mp); 25437 } 25438 } 25439 25440 /* 25441 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25442 * events there already (currently at most two events are cached). 25443 * If the event is not allocated from the timer cache, free it right away. 25444 */ 25445 static void 25446 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25447 { 25448 mblk_t *mp1 = tcp->tcp_timercache; 25449 25450 if (mp->b_wptr != NULL) { 25451 /* 25452 * This allocation is not from a timer cache, free it right 25453 * away. 25454 */ 25455 if (mp->b_wptr != (uchar_t *)-1) 25456 freeb(mp); 25457 else 25458 kmem_free(mp, (size_t)mp->b_datap); 25459 } else if (mp1 == NULL || mp1->b_next == NULL) { 25460 /* Cache this timer block for future allocations */ 25461 mp->b_rptr = (uchar_t *)(&mp[1]); 25462 mp->b_next = mp1; 25463 tcp->tcp_timercache = mp; 25464 } else { 25465 kmem_cache_free(tcp_timercache, mp); 25466 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 25467 } 25468 } 25469 25470 /* 25471 * End of TCP Timers implementation. 25472 */ 25473 25474 /* 25475 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25476 * on the specified backing STREAMS q. Note, the caller may make the 25477 * decision to call based on the tcp_t.tcp_flow_stopped value which 25478 * when check outside the q's lock is only an advisory check ... 25479 */ 25480 void 25481 tcp_setqfull(tcp_t *tcp) 25482 { 25483 tcp_stack_t *tcps = tcp->tcp_tcps; 25484 conn_t *connp = tcp->tcp_connp; 25485 25486 if (tcp->tcp_closed) 25487 return; 25488 25489 if (IPCL_IS_NONSTR(connp)) { 25490 (*connp->conn_upcalls->su_txq_full) 25491 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25492 tcp->tcp_flow_stopped = B_TRUE; 25493 } else { 25494 queue_t *q = tcp->tcp_wq; 25495 25496 if (!(q->q_flag & QFULL)) { 25497 mutex_enter(QLOCK(q)); 25498 if (!(q->q_flag & QFULL)) { 25499 /* still need to set QFULL */ 25500 q->q_flag |= QFULL; 25501 tcp->tcp_flow_stopped = B_TRUE; 25502 mutex_exit(QLOCK(q)); 25503 TCP_STAT(tcps, tcp_flwctl_on); 25504 } else { 25505 mutex_exit(QLOCK(q)); 25506 } 25507 } 25508 } 25509 } 25510 25511 void 25512 tcp_clrqfull(tcp_t *tcp) 25513 { 25514 conn_t *connp = tcp->tcp_connp; 25515 25516 if (tcp->tcp_closed) 25517 return; 25518 25519 if (IPCL_IS_NONSTR(connp)) { 25520 (*connp->conn_upcalls->su_txq_full) 25521 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25522 tcp->tcp_flow_stopped = B_FALSE; 25523 } else { 25524 queue_t *q = tcp->tcp_wq; 25525 25526 if (q->q_flag & QFULL) { 25527 mutex_enter(QLOCK(q)); 25528 if (q->q_flag & QFULL) { 25529 q->q_flag &= ~QFULL; 25530 tcp->tcp_flow_stopped = B_FALSE; 25531 mutex_exit(QLOCK(q)); 25532 if (q->q_flag & QWANTW) 25533 qbackenable(q, 0); 25534 } else { 25535 mutex_exit(QLOCK(q)); 25536 } 25537 } 25538 } 25539 } 25540 25541 /* 25542 * kstats related to squeues i.e. not per IP instance 25543 */ 25544 static void * 25545 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25546 { 25547 kstat_t *ksp; 25548 25549 tcp_g_stat_t template = { 25550 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25551 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25552 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25553 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25554 }; 25555 25556 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25557 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25558 KSTAT_FLAG_VIRTUAL); 25559 25560 if (ksp == NULL) 25561 return (NULL); 25562 25563 bcopy(&template, tcp_g_statp, sizeof (template)); 25564 ksp->ks_data = (void *)tcp_g_statp; 25565 25566 kstat_install(ksp); 25567 return (ksp); 25568 } 25569 25570 static void 25571 tcp_g_kstat_fini(kstat_t *ksp) 25572 { 25573 if (ksp != NULL) { 25574 kstat_delete(ksp); 25575 } 25576 } 25577 25578 25579 static void * 25580 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25581 { 25582 kstat_t *ksp; 25583 25584 tcp_stat_t template = { 25585 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25586 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25587 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25588 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25589 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25590 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25591 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25592 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25593 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25594 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25595 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25596 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25597 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25598 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25599 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25600 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25601 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25602 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25603 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25604 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25605 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25606 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25607 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25608 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25609 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25610 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25611 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25612 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25613 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25614 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25615 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25616 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25617 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25618 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25619 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25620 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25621 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25622 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25623 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25624 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25625 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25626 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25627 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25628 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25629 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25630 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25631 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25632 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25633 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25634 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25635 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25636 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25637 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25638 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25639 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25640 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25641 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25642 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25643 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25644 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25645 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25646 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25647 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25648 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25649 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25650 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25651 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25652 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25653 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25654 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25655 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25656 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25657 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25658 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25659 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25660 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25661 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25662 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25663 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25664 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25665 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25666 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25667 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25668 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25669 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25670 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25671 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25672 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25673 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25674 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25675 }; 25676 25677 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25678 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25679 KSTAT_FLAG_VIRTUAL, stackid); 25680 25681 if (ksp == NULL) 25682 return (NULL); 25683 25684 bcopy(&template, tcps_statisticsp, sizeof (template)); 25685 ksp->ks_data = (void *)tcps_statisticsp; 25686 ksp->ks_private = (void *)(uintptr_t)stackid; 25687 25688 kstat_install(ksp); 25689 return (ksp); 25690 } 25691 25692 static void 25693 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25694 { 25695 if (ksp != NULL) { 25696 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25697 kstat_delete_netstack(ksp, stackid); 25698 } 25699 } 25700 25701 /* 25702 * TCP Kstats implementation 25703 */ 25704 static void * 25705 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25706 { 25707 kstat_t *ksp; 25708 25709 tcp_named_kstat_t template = { 25710 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25711 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25712 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25713 { "maxConn", KSTAT_DATA_INT32, 0 }, 25714 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25715 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25716 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25717 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25718 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25719 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25720 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25721 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25722 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25723 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25724 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25725 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25726 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25727 { "outAck", KSTAT_DATA_UINT32, 0 }, 25728 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25729 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25730 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25731 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25732 { "outControl", KSTAT_DATA_UINT32, 0 }, 25733 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25734 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25735 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25736 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25737 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25738 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25739 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25740 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25741 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25742 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25743 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25744 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25745 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25746 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25747 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25748 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25749 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25750 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25751 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25752 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25753 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25754 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25755 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25756 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25757 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25758 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25759 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25760 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25761 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25762 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25763 }; 25764 25765 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25766 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25767 25768 if (ksp == NULL) 25769 return (NULL); 25770 25771 template.rtoAlgorithm.value.ui32 = 4; 25772 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25773 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25774 template.maxConn.value.i32 = -1; 25775 25776 bcopy(&template, ksp->ks_data, sizeof (template)); 25777 ksp->ks_update = tcp_kstat_update; 25778 ksp->ks_private = (void *)(uintptr_t)stackid; 25779 25780 kstat_install(ksp); 25781 return (ksp); 25782 } 25783 25784 static void 25785 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25786 { 25787 if (ksp != NULL) { 25788 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25789 kstat_delete_netstack(ksp, stackid); 25790 } 25791 } 25792 25793 static int 25794 tcp_kstat_update(kstat_t *kp, int rw) 25795 { 25796 tcp_named_kstat_t *tcpkp; 25797 tcp_t *tcp; 25798 connf_t *connfp; 25799 conn_t *connp; 25800 int i; 25801 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25802 netstack_t *ns; 25803 tcp_stack_t *tcps; 25804 ip_stack_t *ipst; 25805 25806 if ((kp == NULL) || (kp->ks_data == NULL)) 25807 return (EIO); 25808 25809 if (rw == KSTAT_WRITE) 25810 return (EACCES); 25811 25812 ns = netstack_find_by_stackid(stackid); 25813 if (ns == NULL) 25814 return (-1); 25815 tcps = ns->netstack_tcp; 25816 if (tcps == NULL) { 25817 netstack_rele(ns); 25818 return (-1); 25819 } 25820 25821 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25822 25823 tcpkp->currEstab.value.ui32 = 0; 25824 25825 ipst = ns->netstack_ip; 25826 25827 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25828 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25829 connp = NULL; 25830 while ((connp = 25831 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25832 tcp = connp->conn_tcp; 25833 switch (tcp_snmp_state(tcp)) { 25834 case MIB2_TCP_established: 25835 case MIB2_TCP_closeWait: 25836 tcpkp->currEstab.value.ui32++; 25837 break; 25838 } 25839 } 25840 } 25841 25842 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25843 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25844 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25845 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25846 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25847 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25848 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25849 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25850 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25851 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25852 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25853 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25854 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25855 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25856 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25857 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25858 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25859 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25860 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25861 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25862 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25863 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25864 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25865 tcpkp->inDataInorderSegs.value.ui32 = 25866 tcps->tcps_mib.tcpInDataInorderSegs; 25867 tcpkp->inDataInorderBytes.value.ui32 = 25868 tcps->tcps_mib.tcpInDataInorderBytes; 25869 tcpkp->inDataUnorderSegs.value.ui32 = 25870 tcps->tcps_mib.tcpInDataUnorderSegs; 25871 tcpkp->inDataUnorderBytes.value.ui32 = 25872 tcps->tcps_mib.tcpInDataUnorderBytes; 25873 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25874 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25875 tcpkp->inDataPartDupSegs.value.ui32 = 25876 tcps->tcps_mib.tcpInDataPartDupSegs; 25877 tcpkp->inDataPartDupBytes.value.ui32 = 25878 tcps->tcps_mib.tcpInDataPartDupBytes; 25879 tcpkp->inDataPastWinSegs.value.ui32 = 25880 tcps->tcps_mib.tcpInDataPastWinSegs; 25881 tcpkp->inDataPastWinBytes.value.ui32 = 25882 tcps->tcps_mib.tcpInDataPastWinBytes; 25883 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25884 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25885 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25886 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25887 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25888 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25889 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25890 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25891 tcpkp->timKeepaliveProbe.value.ui32 = 25892 tcps->tcps_mib.tcpTimKeepaliveProbe; 25893 tcpkp->timKeepaliveDrop.value.ui32 = 25894 tcps->tcps_mib.tcpTimKeepaliveDrop; 25895 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25896 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25897 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25898 tcpkp->outSackRetransSegs.value.ui32 = 25899 tcps->tcps_mib.tcpOutSackRetransSegs; 25900 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25901 25902 netstack_rele(ns); 25903 return (0); 25904 } 25905 25906 void 25907 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25908 { 25909 uint16_t hdr_len; 25910 ipha_t *ipha; 25911 uint8_t *nexthdrp; 25912 tcph_t *tcph; 25913 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25914 25915 /* Already has an eager */ 25916 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25917 TCP_STAT(tcps, tcp_reinput_syn); 25918 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25919 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25920 return; 25921 } 25922 25923 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25924 case IPV4_VERSION: 25925 ipha = (ipha_t *)mp->b_rptr; 25926 hdr_len = IPH_HDR_LENGTH(ipha); 25927 break; 25928 case IPV6_VERSION: 25929 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25930 &hdr_len, &nexthdrp)) { 25931 CONN_DEC_REF(connp); 25932 freemsg(mp); 25933 return; 25934 } 25935 break; 25936 } 25937 25938 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25939 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25940 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25941 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25942 } 25943 25944 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25945 SQ_FILL, SQTAG_TCP_REINPUT); 25946 } 25947 25948 static int 25949 tcp_squeue_switch(int val) 25950 { 25951 int rval = SQ_FILL; 25952 25953 switch (val) { 25954 case 1: 25955 rval = SQ_NODRAIN; 25956 break; 25957 case 2: 25958 rval = SQ_PROCESS; 25959 break; 25960 default: 25961 break; 25962 } 25963 return (rval); 25964 } 25965 25966 /* 25967 * This is called once for each squeue - globally for all stack 25968 * instances. 25969 */ 25970 static void 25971 tcp_squeue_add(squeue_t *sqp) 25972 { 25973 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25974 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25975 25976 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25977 tcp_time_wait->tcp_time_wait_tid = 25978 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25979 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25980 CALLOUT_FLAG_ROUNDUP); 25981 if (tcp_free_list_max_cnt == 0) { 25982 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25983 max_ncpus : boot_max_ncpus); 25984 25985 /* 25986 * Limit number of entries to 1% of availble memory / tcp_ncpus 25987 */ 25988 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25989 (tcp_ncpus * sizeof (tcp_t) * 100); 25990 } 25991 tcp_time_wait->tcp_free_list_cnt = 0; 25992 } 25993 25994 static int 25995 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25996 { 25997 mblk_t *ire_mp = NULL; 25998 mblk_t *syn_mp; 25999 mblk_t *mdti; 26000 mblk_t *lsoi; 26001 int retval; 26002 tcph_t *tcph; 26003 uint32_t mss; 26004 queue_t *q = tcp->tcp_rq; 26005 conn_t *connp = tcp->tcp_connp; 26006 tcp_stack_t *tcps = tcp->tcp_tcps; 26007 26008 if (error == 0) { 26009 /* 26010 * Adapt Multidata information, if any. The 26011 * following tcp_mdt_update routine will free 26012 * the message. 26013 */ 26014 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26015 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26016 b_rptr)->mdt_capab, B_TRUE); 26017 freemsg(mdti); 26018 } 26019 26020 /* 26021 * Check to update LSO information with tcp, and 26022 * tcp_lso_update routine will free the message. 26023 */ 26024 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26025 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26026 b_rptr)->lso_capab); 26027 freemsg(lsoi); 26028 } 26029 26030 /* Get the IRE, if we had requested for it */ 26031 if (mp != NULL) 26032 ire_mp = tcp_ire_mp(&mp); 26033 26034 if (tcp->tcp_hard_binding) { 26035 tcp->tcp_hard_binding = B_FALSE; 26036 tcp->tcp_hard_bound = B_TRUE; 26037 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26038 if (retval != 0) { 26039 error = EADDRINUSE; 26040 goto bind_failed; 26041 } 26042 } else { 26043 if (ire_mp != NULL) 26044 freeb(ire_mp); 26045 goto after_syn_sent; 26046 } 26047 26048 retval = tcp_adapt_ire(tcp, ire_mp); 26049 if (ire_mp != NULL) 26050 freeb(ire_mp); 26051 if (retval == 0) { 26052 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26053 ENETUNREACH : EADDRNOTAVAIL); 26054 goto ipcl_rm; 26055 } 26056 /* 26057 * Don't let an endpoint connect to itself. 26058 * Also checked in tcp_connect() but that 26059 * check can't handle the case when the 26060 * local IP address is INADDR_ANY. 26061 */ 26062 if (tcp->tcp_ipversion == IPV4_VERSION) { 26063 if ((tcp->tcp_ipha->ipha_dst == 26064 tcp->tcp_ipha->ipha_src) && 26065 (BE16_EQL(tcp->tcp_tcph->th_lport, 26066 tcp->tcp_tcph->th_fport))) { 26067 error = EADDRNOTAVAIL; 26068 goto ipcl_rm; 26069 } 26070 } else { 26071 if (IN6_ARE_ADDR_EQUAL( 26072 &tcp->tcp_ip6h->ip6_dst, 26073 &tcp->tcp_ip6h->ip6_src) && 26074 (BE16_EQL(tcp->tcp_tcph->th_lport, 26075 tcp->tcp_tcph->th_fport))) { 26076 error = EADDRNOTAVAIL; 26077 goto ipcl_rm; 26078 } 26079 } 26080 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26081 /* 26082 * This should not be possible! Just for 26083 * defensive coding... 26084 */ 26085 if (tcp->tcp_state != TCPS_SYN_SENT) 26086 goto after_syn_sent; 26087 26088 if (is_system_labeled() && 26089 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26090 error = EHOSTUNREACH; 26091 goto ipcl_rm; 26092 } 26093 26094 /* 26095 * tcp_adapt_ire() does not adjust 26096 * for TCP/IP header length. 26097 */ 26098 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26099 26100 /* 26101 * Just make sure our rwnd is at 26102 * least tcp_recv_hiwat_mss * MSS 26103 * large, and round up to the nearest 26104 * MSS. 26105 * 26106 * We do the round up here because 26107 * we need to get the interface 26108 * MTU first before we can do the 26109 * round up. 26110 */ 26111 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26112 tcps->tcps_recv_hiwat_minmss * mss); 26113 if (!IPCL_IS_NONSTR(connp)) 26114 q->q_hiwat = tcp->tcp_rwnd; 26115 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26116 tcp_set_ws_value(tcp); 26117 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26118 tcp->tcp_tcph->th_win); 26119 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26120 tcp->tcp_snd_ws_ok = B_TRUE; 26121 26122 /* 26123 * Set tcp_snd_ts_ok to true 26124 * so that tcp_xmit_mp will 26125 * include the timestamp 26126 * option in the SYN segment. 26127 */ 26128 if (tcps->tcps_tstamp_always || 26129 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26130 tcp->tcp_snd_ts_ok = B_TRUE; 26131 } 26132 26133 /* 26134 * tcp_snd_sack_ok can be set in 26135 * tcp_adapt_ire() if the sack metric 26136 * is set. So check it here also. 26137 */ 26138 if (tcps->tcps_sack_permitted == 2 || 26139 tcp->tcp_snd_sack_ok) { 26140 if (tcp->tcp_sack_info == NULL) { 26141 tcp->tcp_sack_info = 26142 kmem_cache_alloc(tcp_sack_info_cache, 26143 KM_SLEEP); 26144 } 26145 tcp->tcp_snd_sack_ok = B_TRUE; 26146 } 26147 26148 /* 26149 * Should we use ECN? Note that the current 26150 * default value (SunOS 5.9) of tcp_ecn_permitted 26151 * is 1. The reason for doing this is that there 26152 * are equipments out there that will drop ECN 26153 * enabled IP packets. Setting it to 1 avoids 26154 * compatibility problems. 26155 */ 26156 if (tcps->tcps_ecn_permitted == 2) 26157 tcp->tcp_ecn_ok = B_TRUE; 26158 26159 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26160 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26161 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26162 if (syn_mp) { 26163 if (cr == NULL) { 26164 cr = tcp->tcp_cred; 26165 pid = tcp->tcp_cpid; 26166 } 26167 mblk_setcred(syn_mp, cr, pid); 26168 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26169 } 26170 after_syn_sent: 26171 if (mp != NULL) { 26172 ASSERT(mp->b_cont == NULL); 26173 freeb(mp); 26174 } 26175 return (error); 26176 } else { 26177 /* error */ 26178 if (tcp->tcp_debug) { 26179 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26180 "tcp_post_ip_bind: error == %d", error); 26181 } 26182 if (mp != NULL) { 26183 freeb(mp); 26184 } 26185 } 26186 26187 ipcl_rm: 26188 /* 26189 * Need to unbind with classifier since we were just 26190 * told that our bind succeeded. a.k.a error == 0 at the entry. 26191 */ 26192 tcp->tcp_hard_bound = B_FALSE; 26193 tcp->tcp_hard_binding = B_FALSE; 26194 26195 ipcl_hash_remove(connp); 26196 26197 bind_failed: 26198 tcp->tcp_state = TCPS_IDLE; 26199 if (tcp->tcp_ipversion == IPV4_VERSION) 26200 tcp->tcp_ipha->ipha_src = 0; 26201 else 26202 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26203 /* 26204 * Copy of the src addr. in tcp_t is needed since 26205 * the lookup funcs. can only look at tcp_t 26206 */ 26207 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26208 26209 tcph = tcp->tcp_tcph; 26210 tcph->th_lport[0] = 0; 26211 tcph->th_lport[1] = 0; 26212 tcp_bind_hash_remove(tcp); 26213 bzero(&connp->u_port, sizeof (connp->u_port)); 26214 /* blow away saved option results if any */ 26215 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26216 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26217 26218 conn_delete_ire(tcp->tcp_connp, NULL); 26219 26220 return (error); 26221 } 26222 26223 static int 26224 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26225 boolean_t bind_to_req_port_only, cred_t *cr) 26226 { 26227 in_port_t mlp_port; 26228 mlp_type_t addrtype, mlptype; 26229 boolean_t user_specified; 26230 in_port_t allocated_port; 26231 in_port_t requested_port = *requested_port_ptr; 26232 conn_t *connp; 26233 zone_t *zone; 26234 tcp_stack_t *tcps = tcp->tcp_tcps; 26235 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26236 26237 /* 26238 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26239 */ 26240 if (cr == NULL) 26241 cr = tcp->tcp_cred; 26242 /* 26243 * Get a valid port (within the anonymous range and should not 26244 * be a privileged one) to use if the user has not given a port. 26245 * If multiple threads are here, they may all start with 26246 * with the same initial port. But, it should be fine as long as 26247 * tcp_bindi will ensure that no two threads will be assigned 26248 * the same port. 26249 * 26250 * NOTE: XXX If a privileged process asks for an anonymous port, we 26251 * still check for ports only in the range > tcp_smallest_non_priv_port, 26252 * unless TCP_ANONPRIVBIND option is set. 26253 */ 26254 mlptype = mlptSingle; 26255 mlp_port = requested_port; 26256 if (requested_port == 0) { 26257 requested_port = tcp->tcp_anon_priv_bind ? 26258 tcp_get_next_priv_port(tcp) : 26259 tcp_update_next_port(tcps->tcps_next_port_to_try, 26260 tcp, B_TRUE); 26261 if (requested_port == 0) { 26262 return (-TNOADDR); 26263 } 26264 user_specified = B_FALSE; 26265 26266 /* 26267 * If the user went through one of the RPC interfaces to create 26268 * this socket and RPC is MLP in this zone, then give him an 26269 * anonymous MLP. 26270 */ 26271 connp = tcp->tcp_connp; 26272 if (connp->conn_anon_mlp && is_system_labeled()) { 26273 zone = crgetzone(cr); 26274 addrtype = tsol_mlp_addr_type(zone->zone_id, 26275 IPV6_VERSION, &v6addr, 26276 tcps->tcps_netstack->netstack_ip); 26277 if (addrtype == mlptSingle) { 26278 return (-TNOADDR); 26279 } 26280 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26281 PMAPPORT, addrtype); 26282 mlp_port = PMAPPORT; 26283 } 26284 } else { 26285 int i; 26286 boolean_t priv = B_FALSE; 26287 26288 /* 26289 * If the requested_port is in the well-known privileged range, 26290 * verify that the stream was opened by a privileged user. 26291 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26292 * but instead the code relies on: 26293 * - the fact that the address of the array and its size never 26294 * changes 26295 * - the atomic assignment of the elements of the array 26296 */ 26297 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26298 priv = B_TRUE; 26299 } else { 26300 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26301 if (requested_port == 26302 tcps->tcps_g_epriv_ports[i]) { 26303 priv = B_TRUE; 26304 break; 26305 } 26306 } 26307 } 26308 if (priv) { 26309 if (secpolicy_net_privaddr(cr, requested_port, 26310 IPPROTO_TCP) != 0) { 26311 if (tcp->tcp_debug) { 26312 (void) strlog(TCP_MOD_ID, 0, 1, 26313 SL_ERROR|SL_TRACE, 26314 "tcp_bind: no priv for port %d", 26315 requested_port); 26316 } 26317 return (-TACCES); 26318 } 26319 } 26320 user_specified = B_TRUE; 26321 26322 connp = tcp->tcp_connp; 26323 if (is_system_labeled()) { 26324 zone = crgetzone(cr); 26325 addrtype = tsol_mlp_addr_type(zone->zone_id, 26326 IPV6_VERSION, &v6addr, 26327 tcps->tcps_netstack->netstack_ip); 26328 if (addrtype == mlptSingle) { 26329 return (-TNOADDR); 26330 } 26331 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26332 requested_port, addrtype); 26333 } 26334 } 26335 26336 if (mlptype != mlptSingle) { 26337 if (secpolicy_net_bindmlp(cr) != 0) { 26338 if (tcp->tcp_debug) { 26339 (void) strlog(TCP_MOD_ID, 0, 1, 26340 SL_ERROR|SL_TRACE, 26341 "tcp_bind: no priv for multilevel port %d", 26342 requested_port); 26343 } 26344 return (-TACCES); 26345 } 26346 26347 /* 26348 * If we're specifically binding a shared IP address and the 26349 * port is MLP on shared addresses, then check to see if this 26350 * zone actually owns the MLP. Reject if not. 26351 */ 26352 if (mlptype == mlptShared && addrtype == mlptShared) { 26353 /* 26354 * No need to handle exclusive-stack zones since 26355 * ALL_ZONES only applies to the shared stack. 26356 */ 26357 zoneid_t mlpzone; 26358 26359 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26360 htons(mlp_port)); 26361 if (connp->conn_zoneid != mlpzone) { 26362 if (tcp->tcp_debug) { 26363 (void) strlog(TCP_MOD_ID, 0, 1, 26364 SL_ERROR|SL_TRACE, 26365 "tcp_bind: attempt to bind port " 26366 "%d on shared addr in zone %d " 26367 "(should be %d)", 26368 mlp_port, connp->conn_zoneid, 26369 mlpzone); 26370 } 26371 return (-TACCES); 26372 } 26373 } 26374 26375 if (!user_specified) { 26376 int err; 26377 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26378 requested_port, B_TRUE); 26379 if (err != 0) { 26380 if (tcp->tcp_debug) { 26381 (void) strlog(TCP_MOD_ID, 0, 1, 26382 SL_ERROR|SL_TRACE, 26383 "tcp_bind: cannot establish anon " 26384 "MLP for port %d", 26385 requested_port); 26386 } 26387 return (err); 26388 } 26389 connp->conn_anon_port = B_TRUE; 26390 } 26391 connp->conn_mlp_type = mlptype; 26392 } 26393 26394 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26395 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26396 26397 if (allocated_port == 0) { 26398 connp->conn_mlp_type = mlptSingle; 26399 if (connp->conn_anon_port) { 26400 connp->conn_anon_port = B_FALSE; 26401 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26402 requested_port, B_FALSE); 26403 } 26404 if (bind_to_req_port_only) { 26405 if (tcp->tcp_debug) { 26406 (void) strlog(TCP_MOD_ID, 0, 1, 26407 SL_ERROR|SL_TRACE, 26408 "tcp_bind: requested addr busy"); 26409 } 26410 return (-TADDRBUSY); 26411 } else { 26412 /* If we are out of ports, fail the bind. */ 26413 if (tcp->tcp_debug) { 26414 (void) strlog(TCP_MOD_ID, 0, 1, 26415 SL_ERROR|SL_TRACE, 26416 "tcp_bind: out of ports?"); 26417 } 26418 return (-TNOADDR); 26419 } 26420 } 26421 26422 /* Pass the allocated port back */ 26423 *requested_port_ptr = allocated_port; 26424 return (0); 26425 } 26426 26427 static int 26428 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26429 boolean_t bind_to_req_port_only) 26430 { 26431 tcp_t *tcp = connp->conn_tcp; 26432 sin_t *sin; 26433 sin6_t *sin6; 26434 in_port_t requested_port; 26435 ipaddr_t v4addr; 26436 in6_addr_t v6addr; 26437 uint_t origipversion; 26438 int error = 0; 26439 26440 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 26441 26442 if (tcp->tcp_state == TCPS_BOUND) { 26443 return (0); 26444 } else if (tcp->tcp_state > TCPS_BOUND) { 26445 if (tcp->tcp_debug) { 26446 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26447 "tcp_bind: bad state, %d", tcp->tcp_state); 26448 } 26449 return (-TOUTSTATE); 26450 } 26451 origipversion = tcp->tcp_ipversion; 26452 26453 ASSERT(sa != NULL && len != 0); 26454 26455 if (!OK_32PTR((char *)sa)) { 26456 if (tcp->tcp_debug) { 26457 (void) strlog(TCP_MOD_ID, 0, 1, 26458 SL_ERROR|SL_TRACE, 26459 "tcp_bind: bad address parameter, " 26460 "address %p, len %d", 26461 (void *)sa, len); 26462 } 26463 return (-TPROTO); 26464 } 26465 26466 switch (len) { 26467 case sizeof (sin_t): /* Complete IPv4 address */ 26468 sin = (sin_t *)sa; 26469 /* 26470 * With sockets sockfs will accept bogus sin_family in 26471 * bind() and replace it with the family used in the socket 26472 * call. 26473 */ 26474 if (sin->sin_family != AF_INET || 26475 tcp->tcp_family != AF_INET) { 26476 return (EAFNOSUPPORT); 26477 } 26478 requested_port = ntohs(sin->sin_port); 26479 tcp->tcp_ipversion = IPV4_VERSION; 26480 v4addr = sin->sin_addr.s_addr; 26481 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 26482 break; 26483 26484 case sizeof (sin6_t): /* Complete IPv6 address */ 26485 sin6 = (sin6_t *)sa; 26486 if (sin6->sin6_family != AF_INET6 || 26487 tcp->tcp_family != AF_INET6) { 26488 return (EAFNOSUPPORT); 26489 } 26490 requested_port = ntohs(sin6->sin6_port); 26491 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26492 IPV4_VERSION : IPV6_VERSION; 26493 v6addr = sin6->sin6_addr; 26494 break; 26495 26496 default: 26497 if (tcp->tcp_debug) { 26498 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26499 "tcp_bind: bad address length, %d", len); 26500 } 26501 return (EAFNOSUPPORT); 26502 /* return (-TBADADDR); */ 26503 } 26504 26505 tcp->tcp_bound_source_v6 = v6addr; 26506 26507 /* Check for change in ipversion */ 26508 if (origipversion != tcp->tcp_ipversion) { 26509 ASSERT(tcp->tcp_family == AF_INET6); 26510 error = tcp->tcp_ipversion == IPV6_VERSION ? 26511 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26512 if (error) { 26513 return (ENOMEM); 26514 } 26515 } 26516 26517 /* 26518 * Initialize family specific fields. Copy of the src addr. 26519 * in tcp_t is needed for the lookup funcs. 26520 */ 26521 if (tcp->tcp_ipversion == IPV6_VERSION) { 26522 tcp->tcp_ip6h->ip6_src = v6addr; 26523 } else { 26524 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26525 } 26526 tcp->tcp_ip_src_v6 = v6addr; 26527 26528 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26529 26530 error = tcp_bind_select_lport(tcp, &requested_port, 26531 bind_to_req_port_only, cr); 26532 26533 return (error); 26534 } 26535 26536 /* 26537 * Return unix error is tli error is TSYSERR, otherwise return a negative 26538 * tli error. 26539 */ 26540 int 26541 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26542 boolean_t bind_to_req_port_only) 26543 { 26544 int error; 26545 tcp_t *tcp = connp->conn_tcp; 26546 26547 if (tcp->tcp_state >= TCPS_BOUND) { 26548 if (tcp->tcp_debug) { 26549 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26550 "tcp_bind: bad state, %d", tcp->tcp_state); 26551 } 26552 return (-TOUTSTATE); 26553 } 26554 26555 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26556 if (error != 0) 26557 return (error); 26558 26559 ASSERT(tcp->tcp_state == TCPS_BOUND); 26560 26561 tcp->tcp_conn_req_max = 0; 26562 26563 if (tcp->tcp_family == AF_INET6) { 26564 ASSERT(tcp->tcp_connp->conn_af_isv6); 26565 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26566 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26567 } else { 26568 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26569 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26570 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26571 } 26572 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26573 } 26574 26575 int 26576 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26577 socklen_t len, cred_t *cr) 26578 { 26579 int error; 26580 conn_t *connp = (conn_t *)proto_handle; 26581 squeue_t *sqp = connp->conn_sqp; 26582 26583 /* All Solaris components should pass a cred for this operation. */ 26584 ASSERT(cr != NULL); 26585 26586 ASSERT(sqp != NULL); 26587 ASSERT(connp->conn_upper_handle != NULL); 26588 26589 error = squeue_synch_enter(sqp, connp, 0); 26590 if (error != 0) { 26591 /* failed to enter */ 26592 return (ENOSR); 26593 } 26594 26595 /* binding to a NULL address really means unbind */ 26596 if (sa == NULL) { 26597 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26598 error = tcp_do_unbind(connp); 26599 else 26600 error = EINVAL; 26601 } else { 26602 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26603 } 26604 26605 squeue_synch_exit(sqp, connp); 26606 26607 if (error < 0) { 26608 if (error == -TOUTSTATE) 26609 error = EINVAL; 26610 else 26611 error = proto_tlitosyserr(-error); 26612 } 26613 26614 return (error); 26615 } 26616 26617 /* 26618 * If the return value from this function is positive, it's a UNIX error. 26619 * Otherwise, if it's negative, then the absolute value is a TLI error. 26620 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26621 */ 26622 int 26623 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26624 cred_t *cr, pid_t pid) 26625 { 26626 tcp_t *tcp = connp->conn_tcp; 26627 sin_t *sin = (sin_t *)sa; 26628 sin6_t *sin6 = (sin6_t *)sa; 26629 ipaddr_t *dstaddrp; 26630 in_port_t dstport; 26631 uint_t srcid; 26632 int error = 0; 26633 26634 switch (len) { 26635 default: 26636 /* 26637 * Should never happen 26638 */ 26639 return (EINVAL); 26640 26641 case sizeof (sin_t): 26642 sin = (sin_t *)sa; 26643 if (sin->sin_port == 0) { 26644 return (-TBADADDR); 26645 } 26646 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26647 return (EAFNOSUPPORT); 26648 } 26649 break; 26650 26651 case sizeof (sin6_t): 26652 sin6 = (sin6_t *)sa; 26653 if (sin6->sin6_port == 0) { 26654 return (-TBADADDR); 26655 } 26656 break; 26657 } 26658 /* 26659 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26660 * make sure that the template IP header in the tcp structure is an 26661 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26662 * need to this before we call tcp_bindi() so that the port lookup 26663 * code will look for ports in the correct port space (IPv4 and 26664 * IPv6 have separate port spaces). 26665 */ 26666 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26667 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26668 int err = 0; 26669 26670 err = tcp_header_init_ipv4(tcp); 26671 if (err != 0) { 26672 error = ENOMEM; 26673 goto connect_failed; 26674 } 26675 if (tcp->tcp_lport != 0) 26676 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26677 } 26678 26679 switch (tcp->tcp_state) { 26680 case TCPS_LISTEN: 26681 /* 26682 * Listening sockets are not allowed to issue connect(). 26683 */ 26684 if (IPCL_IS_NONSTR(connp)) 26685 return (EOPNOTSUPP); 26686 /* FALLTHRU */ 26687 case TCPS_IDLE: 26688 /* 26689 * We support quick connect, refer to comments in 26690 * tcp_connect_*() 26691 */ 26692 /* FALLTHRU */ 26693 case TCPS_BOUND: 26694 /* 26695 * We must bump the generation before the operation start. 26696 * This is done to ensure that any upcall made later on sends 26697 * up the right generation to the socket. 26698 */ 26699 SOCK_CONNID_BUMP(tcp->tcp_connid); 26700 26701 if (tcp->tcp_family == AF_INET6) { 26702 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26703 return (tcp_connect_ipv6(tcp, 26704 &sin6->sin6_addr, 26705 sin6->sin6_port, sin6->sin6_flowinfo, 26706 sin6->__sin6_src_id, sin6->sin6_scope_id, 26707 cr, pid)); 26708 } 26709 /* 26710 * Destination adress is mapped IPv6 address. 26711 * Source bound address should be unspecified or 26712 * IPv6 mapped address as well. 26713 */ 26714 if (!IN6_IS_ADDR_UNSPECIFIED( 26715 &tcp->tcp_bound_source_v6) && 26716 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26717 return (EADDRNOTAVAIL); 26718 } 26719 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26720 dstport = sin6->sin6_port; 26721 srcid = sin6->__sin6_src_id; 26722 } else { 26723 dstaddrp = &sin->sin_addr.s_addr; 26724 dstport = sin->sin_port; 26725 srcid = 0; 26726 } 26727 26728 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26729 pid); 26730 break; 26731 default: 26732 return (-TOUTSTATE); 26733 } 26734 /* 26735 * Note: Code below is the "failure" case 26736 */ 26737 connect_failed: 26738 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26739 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26740 return (error); 26741 } 26742 26743 int 26744 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26745 socklen_t len, sock_connid_t *id, cred_t *cr) 26746 { 26747 conn_t *connp = (conn_t *)proto_handle; 26748 tcp_t *tcp = connp->conn_tcp; 26749 squeue_t *sqp = connp->conn_sqp; 26750 int error; 26751 26752 ASSERT(connp->conn_upper_handle != NULL); 26753 26754 /* All Solaris components should pass a cred for this operation. */ 26755 ASSERT(cr != NULL); 26756 26757 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26758 if (error != 0) { 26759 return (error); 26760 } 26761 26762 error = squeue_synch_enter(sqp, connp, 0); 26763 if (error != 0) { 26764 /* failed to enter */ 26765 return (ENOSR); 26766 } 26767 26768 /* 26769 * TCP supports quick connect, so no need to do an implicit bind 26770 */ 26771 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26772 if (error == 0) { 26773 *id = connp->conn_tcp->tcp_connid; 26774 } else if (error < 0) { 26775 if (error == -TOUTSTATE) { 26776 switch (connp->conn_tcp->tcp_state) { 26777 case TCPS_SYN_SENT: 26778 error = EALREADY; 26779 break; 26780 case TCPS_ESTABLISHED: 26781 error = EISCONN; 26782 break; 26783 case TCPS_LISTEN: 26784 error = EOPNOTSUPP; 26785 break; 26786 default: 26787 error = EINVAL; 26788 break; 26789 } 26790 } else { 26791 error = proto_tlitosyserr(-error); 26792 } 26793 } 26794 done: 26795 squeue_synch_exit(sqp, connp); 26796 26797 return ((error == 0) ? EINPROGRESS : error); 26798 } 26799 26800 /* ARGSUSED */ 26801 sock_lower_handle_t 26802 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26803 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26804 { 26805 conn_t *connp; 26806 boolean_t isv6 = family == AF_INET6; 26807 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26808 (proto != 0 && proto != IPPROTO_TCP)) { 26809 *errorp = EPROTONOSUPPORT; 26810 return (NULL); 26811 } 26812 26813 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26814 if (connp == NULL) { 26815 return (NULL); 26816 } 26817 26818 /* 26819 * Put the ref for TCP. Ref for IP was already put 26820 * by ipcl_conn_create. Also Make the conn_t globally 26821 * visible to walkers 26822 */ 26823 mutex_enter(&connp->conn_lock); 26824 CONN_INC_REF_LOCKED(connp); 26825 ASSERT(connp->conn_ref == 2); 26826 connp->conn_state_flags &= ~CONN_INCIPIENT; 26827 26828 connp->conn_flags |= IPCL_NONSTR; 26829 mutex_exit(&connp->conn_lock); 26830 26831 ASSERT(errorp != NULL); 26832 *errorp = 0; 26833 *sock_downcalls = &sock_tcp_downcalls; 26834 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26835 SM_SENDFILESUPP; 26836 26837 return ((sock_lower_handle_t)connp); 26838 } 26839 26840 /* ARGSUSED */ 26841 void 26842 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26843 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26844 { 26845 conn_t *connp = (conn_t *)proto_handle; 26846 struct sock_proto_props sopp; 26847 26848 ASSERT(connp->conn_upper_handle == NULL); 26849 26850 /* All Solaris components should pass a cred for this operation. */ 26851 ASSERT(cr != NULL); 26852 26853 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26854 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26855 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26856 26857 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26858 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26859 sopp.sopp_maxpsz = INFPSZ; 26860 sopp.sopp_maxblk = INFPSZ; 26861 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26862 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26863 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26864 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26865 tcp_rinfo.mi_minpsz; 26866 26867 connp->conn_upcalls = sock_upcalls; 26868 connp->conn_upper_handle = sock_handle; 26869 26870 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26871 } 26872 26873 /* ARGSUSED */ 26874 int 26875 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26876 { 26877 conn_t *connp = (conn_t *)proto_handle; 26878 26879 ASSERT(connp->conn_upper_handle != NULL); 26880 26881 /* All Solaris components should pass a cred for this operation. */ 26882 ASSERT(cr != NULL); 26883 26884 tcp_close_common(connp, flags); 26885 26886 ip_free_helper_stream(connp); 26887 26888 /* 26889 * Drop IP's reference on the conn. This is the last reference 26890 * on the connp if the state was less than established. If the 26891 * connection has gone into timewait state, then we will have 26892 * one ref for the TCP and one more ref (total of two) for the 26893 * classifier connected hash list (a timewait connections stays 26894 * in connected hash till closed). 26895 * 26896 * We can't assert the references because there might be other 26897 * transient reference places because of some walkers or queued 26898 * packets in squeue for the timewait state. 26899 */ 26900 CONN_DEC_REF(connp); 26901 return (0); 26902 } 26903 26904 /* ARGSUSED */ 26905 int 26906 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26907 cred_t *cr) 26908 { 26909 tcp_t *tcp; 26910 uint32_t msize; 26911 conn_t *connp = (conn_t *)proto_handle; 26912 int32_t tcpstate; 26913 26914 /* All Solaris components should pass a cred for this operation. */ 26915 ASSERT(cr != NULL); 26916 26917 ASSERT(connp->conn_ref >= 2); 26918 ASSERT(connp->conn_upper_handle != NULL); 26919 26920 if (msg->msg_controllen != 0) { 26921 return (EOPNOTSUPP); 26922 26923 } 26924 switch (DB_TYPE(mp)) { 26925 case M_DATA: 26926 tcp = connp->conn_tcp; 26927 ASSERT(tcp != NULL); 26928 26929 tcpstate = tcp->tcp_state; 26930 if (tcpstate < TCPS_ESTABLISHED) { 26931 freemsg(mp); 26932 return (ENOTCONN); 26933 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26934 freemsg(mp); 26935 return (EPIPE); 26936 } 26937 26938 msize = msgdsize(mp); 26939 26940 mutex_enter(&tcp->tcp_non_sq_lock); 26941 tcp->tcp_squeue_bytes += msize; 26942 /* 26943 * Squeue Flow Control 26944 */ 26945 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26946 tcp_setqfull(tcp); 26947 } 26948 mutex_exit(&tcp->tcp_non_sq_lock); 26949 26950 /* 26951 * The application may pass in an address in the msghdr, but 26952 * we ignore the address on connection-oriented sockets. 26953 * Just like BSD this code does not generate an error for 26954 * TCP (a CONNREQUIRED socket) when sending to an address 26955 * passed in with sendto/sendmsg. Instead the data is 26956 * delivered on the connection as if no address had been 26957 * supplied. 26958 */ 26959 CONN_INC_REF(connp); 26960 26961 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26962 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26963 tcp_output_urgent, connp, tcp_squeue_flag, 26964 SQTAG_TCP_OUTPUT); 26965 } else { 26966 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26967 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26968 } 26969 26970 return (0); 26971 26972 default: 26973 ASSERT(0); 26974 } 26975 26976 freemsg(mp); 26977 return (0); 26978 } 26979 26980 /* ARGSUSED */ 26981 void 26982 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26983 { 26984 int len; 26985 uint32_t msize; 26986 conn_t *connp = (conn_t *)arg; 26987 tcp_t *tcp = connp->conn_tcp; 26988 26989 msize = msgdsize(mp); 26990 26991 len = msize - 1; 26992 if (len < 0) { 26993 freemsg(mp); 26994 return; 26995 } 26996 26997 /* 26998 * Try to force urgent data out on the wire. 26999 * Even if we have unsent data this will 27000 * at least send the urgent flag. 27001 * XXX does not handle more flag correctly. 27002 */ 27003 len += tcp->tcp_unsent; 27004 len += tcp->tcp_snxt; 27005 tcp->tcp_urg = len; 27006 tcp->tcp_valid_bits |= TCP_URG_VALID; 27007 27008 /* Bypass tcp protocol for fused tcp loopback */ 27009 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27010 return; 27011 tcp_wput_data(tcp, mp, B_TRUE); 27012 } 27013 27014 /* ARGSUSED */ 27015 int 27016 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27017 socklen_t *addrlenp, cred_t *cr) 27018 { 27019 conn_t *connp = (conn_t *)proto_handle; 27020 tcp_t *tcp = connp->conn_tcp; 27021 27022 ASSERT(connp->conn_upper_handle != NULL); 27023 /* All Solaris components should pass a cred for this operation. */ 27024 ASSERT(cr != NULL); 27025 27026 ASSERT(tcp != NULL); 27027 27028 return (tcp_do_getpeername(tcp, addr, addrlenp)); 27029 } 27030 27031 /* ARGSUSED */ 27032 int 27033 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27034 socklen_t *addrlenp, cred_t *cr) 27035 { 27036 conn_t *connp = (conn_t *)proto_handle; 27037 tcp_t *tcp = connp->conn_tcp; 27038 27039 /* All Solaris components should pass a cred for this operation. */ 27040 ASSERT(cr != NULL); 27041 27042 ASSERT(connp->conn_upper_handle != NULL); 27043 27044 return (tcp_do_getsockname(tcp, addr, addrlenp)); 27045 } 27046 27047 /* 27048 * tcp_fallback 27049 * 27050 * A direct socket is falling back to using STREAMS. The queue 27051 * that is being passed down was created using tcp_open() with 27052 * the SO_FALLBACK flag set. As a result, the queue is not 27053 * associated with a conn, and the q_ptrs instead contain the 27054 * dev and minor area that should be used. 27055 * 27056 * The 'direct_sockfs' flag indicates whether the FireEngine 27057 * optimizations should be used. The common case would be that 27058 * optimizations are enabled, and they might be subsequently 27059 * disabled using the _SIOCSOCKFALLBACK ioctl. 27060 */ 27061 27062 /* 27063 * An active connection is falling back to TPI. Gather all the information 27064 * required by the STREAM head and TPI sonode and send it up. 27065 */ 27066 void 27067 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 27068 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27069 { 27070 conn_t *connp = tcp->tcp_connp; 27071 struct stroptions *stropt; 27072 struct T_capability_ack tca; 27073 struct sockaddr_in6 laddr, faddr; 27074 socklen_t laddrlen, faddrlen; 27075 short opts; 27076 int error; 27077 mblk_t *mp; 27078 27079 /* Disable I/OAT during fallback */ 27080 tcp->tcp_sodirect = NULL; 27081 27082 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27083 connp->conn_minor_arena = WR(q)->q_ptr; 27084 27085 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27086 27087 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27088 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27089 27090 WR(q)->q_qinfo = &tcp_sock_winit; 27091 27092 if (!direct_sockfs) 27093 tcp_disable_direct_sockfs(tcp); 27094 27095 /* 27096 * free the helper stream 27097 */ 27098 ip_free_helper_stream(connp); 27099 27100 /* 27101 * Notify the STREAM head about options 27102 */ 27103 DB_TYPE(stropt_mp) = M_SETOPTS; 27104 stropt = (struct stroptions *)stropt_mp->b_rptr; 27105 stropt_mp->b_wptr += sizeof (struct stroptions); 27106 stropt = (struct stroptions *)stropt_mp->b_rptr; 27107 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27108 27109 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27110 tcp->tcp_tcps->tcps_wroff_xtra); 27111 if (tcp->tcp_snd_sack_ok) 27112 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27113 stropt->so_hiwat = tcp->tcp_fused ? 27114 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27115 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27116 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27117 27118 putnext(RD(q), stropt_mp); 27119 27120 /* 27121 * Collect the information needed to sync with the sonode 27122 */ 27123 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27124 27125 laddrlen = faddrlen = sizeof (sin6_t); 27126 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 27127 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 27128 if (error != 0) 27129 faddrlen = 0; 27130 27131 opts = 0; 27132 if (tcp->tcp_oobinline) 27133 opts |= SO_OOBINLINE; 27134 if (tcp->tcp_dontroute) 27135 opts |= SO_DONTROUTE; 27136 27137 /* 27138 * Notify the socket that the protocol is now quiescent, 27139 * and it's therefore safe move data from the socket 27140 * to the stream head. 27141 */ 27142 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27143 (struct sockaddr *)&laddr, laddrlen, 27144 (struct sockaddr *)&faddr, faddrlen, opts); 27145 27146 while ((mp = tcp->tcp_rcv_list) != NULL) { 27147 tcp->tcp_rcv_list = mp->b_next; 27148 mp->b_next = NULL; 27149 putnext(q, mp); 27150 } 27151 tcp->tcp_rcv_last_head = NULL; 27152 tcp->tcp_rcv_last_tail = NULL; 27153 tcp->tcp_rcv_cnt = 0; 27154 } 27155 27156 /* 27157 * An eager is falling back to TPI. All we have to do is send 27158 * up a T_CONN_IND. 27159 */ 27160 void 27161 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 27162 { 27163 tcp_t *listener = eager->tcp_listener; 27164 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 27165 27166 ASSERT(listener != NULL); 27167 ASSERT(mp != NULL); 27168 27169 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27170 27171 /* 27172 * TLI/XTI applications will get confused by 27173 * sending eager as an option since it violates 27174 * the option semantics. So remove the eager as 27175 * option since TLI/XTI app doesn't need it anyway. 27176 */ 27177 if (!direct_sockfs) { 27178 struct T_conn_ind *conn_ind; 27179 27180 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27181 conn_ind->OPT_length = 0; 27182 conn_ind->OPT_offset = 0; 27183 } 27184 27185 /* 27186 * Sockfs guarantees that the listener will not be closed 27187 * during fallback. So we can safely use the listener's queue. 27188 */ 27189 putnext(listener->tcp_rq, mp); 27190 } 27191 27192 int 27193 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27194 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27195 { 27196 tcp_t *tcp; 27197 conn_t *connp = (conn_t *)proto_handle; 27198 int error; 27199 mblk_t *stropt_mp; 27200 mblk_t *ordrel_mp; 27201 mblk_t *fused_sigurp_mp; 27202 mblk_t *tcp_rsrv_mp; 27203 27204 tcp = connp->conn_tcp; 27205 27206 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 27207 NULL); 27208 27209 /* Pre-allocate the T_ordrel_ind mblk. */ 27210 ASSERT(tcp->tcp_ordrel_mp == NULL); 27211 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27212 STR_NOSIG, NULL); 27213 ordrel_mp->b_datap->db_type = M_PROTO; 27214 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27215 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27216 27217 /* Pre-allocate the M_PCSIG used by fusion */ 27218 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27219 27220 /* 27221 * Pre-allocate the tcp_rsrv_mp mblk. 27222 * It is possible that this conn was previously used for a streams 27223 * socket and already has tcp_rsrv_mp 27224 */ 27225 tcp_rsrv_mp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27226 27227 /* 27228 * Enter the squeue so that no new packets can come in 27229 */ 27230 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27231 if (error != 0) { 27232 /* failed to enter, free all the pre-allocated messages. */ 27233 freeb(stropt_mp); 27234 freeb(ordrel_mp); 27235 freeb(fused_sigurp_mp); 27236 freeb(tcp_rsrv_mp); 27237 /* 27238 * We cannot process the eager, so at least send out a 27239 * RST so the peer can reconnect. 27240 */ 27241 if (tcp->tcp_listener != NULL) { 27242 (void) tcp_eager_blowoff(tcp->tcp_listener, 27243 tcp->tcp_conn_req_seqnum); 27244 } 27245 return (ENOMEM); 27246 } 27247 27248 /* 27249 * No longer a direct socket 27250 */ 27251 connp->conn_flags &= ~IPCL_NONSTR; 27252 27253 tcp->tcp_ordrel_mp = ordrel_mp; 27254 27255 if (tcp->tcp_fused) { 27256 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27257 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27258 } else { 27259 freeb(fused_sigurp_mp); 27260 } 27261 27262 if (tcp->tcp_rsrv_mp == NULL) { 27263 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 27264 } else { 27265 /* 27266 * reusing a conn that was previously used for streams socket 27267 */ 27268 freeb(tcp_rsrv_mp); 27269 } 27270 if (tcp->tcp_listener != NULL) { 27271 /* The eager will deal with opts when accept() is called */ 27272 freeb(stropt_mp); 27273 tcp_fallback_eager(tcp, direct_sockfs); 27274 } else { 27275 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 27276 quiesced_cb); 27277 } 27278 27279 /* 27280 * There should be atleast two ref's (IP + TCP) 27281 */ 27282 ASSERT(connp->conn_ref >= 2); 27283 squeue_synch_exit(connp->conn_sqp, connp); 27284 27285 return (0); 27286 } 27287 27288 /* ARGSUSED */ 27289 static void 27290 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27291 { 27292 conn_t *connp = (conn_t *)arg; 27293 tcp_t *tcp = connp->conn_tcp; 27294 27295 freemsg(mp); 27296 27297 if (tcp->tcp_fused) 27298 tcp_unfuse(tcp); 27299 27300 if (tcp_xmit_end(tcp) != 0) { 27301 /* 27302 * We were crossing FINs and got a reset from 27303 * the other side. Just ignore it. 27304 */ 27305 if (tcp->tcp_debug) { 27306 (void) strlog(TCP_MOD_ID, 0, 1, 27307 SL_ERROR|SL_TRACE, 27308 "tcp_shutdown_output() out of state %s", 27309 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27310 } 27311 } 27312 } 27313 27314 /* ARGSUSED */ 27315 int 27316 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27317 { 27318 conn_t *connp = (conn_t *)proto_handle; 27319 tcp_t *tcp = connp->conn_tcp; 27320 27321 ASSERT(connp->conn_upper_handle != NULL); 27322 27323 /* All Solaris components should pass a cred for this operation. */ 27324 ASSERT(cr != NULL); 27325 27326 /* 27327 * X/Open requires that we check the connected state. 27328 */ 27329 if (tcp->tcp_state < TCPS_SYN_SENT) 27330 return (ENOTCONN); 27331 27332 /* shutdown the send side */ 27333 if (how != SHUT_RD) { 27334 mblk_t *bp; 27335 27336 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27337 CONN_INC_REF(connp); 27338 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27339 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27340 27341 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27342 SOCK_OPCTL_SHUT_SEND, 0); 27343 } 27344 27345 /* shutdown the recv side */ 27346 if (how != SHUT_WR) 27347 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27348 SOCK_OPCTL_SHUT_RECV, 0); 27349 27350 return (0); 27351 } 27352 27353 /* 27354 * SOP_LISTEN() calls into tcp_listen(). 27355 */ 27356 /* ARGSUSED */ 27357 int 27358 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27359 { 27360 conn_t *connp = (conn_t *)proto_handle; 27361 int error; 27362 squeue_t *sqp = connp->conn_sqp; 27363 27364 ASSERT(connp->conn_upper_handle != NULL); 27365 27366 /* All Solaris components should pass a cred for this operation. */ 27367 ASSERT(cr != NULL); 27368 27369 error = squeue_synch_enter(sqp, connp, 0); 27370 if (error != 0) { 27371 /* failed to enter */ 27372 return (ENOBUFS); 27373 } 27374 27375 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 27376 if (error == 0) { 27377 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27378 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 27379 } else if (error < 0) { 27380 if (error == -TOUTSTATE) 27381 error = EINVAL; 27382 else 27383 error = proto_tlitosyserr(-error); 27384 } 27385 squeue_synch_exit(sqp, connp); 27386 return (error); 27387 } 27388 27389 static int 27390 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 27391 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 27392 { 27393 tcp_t *tcp = connp->conn_tcp; 27394 int error = 0; 27395 tcp_stack_t *tcps = tcp->tcp_tcps; 27396 27397 /* All Solaris components should pass a cred for this operation. */ 27398 ASSERT(cr != NULL); 27399 27400 if (tcp->tcp_state >= TCPS_BOUND) { 27401 if ((tcp->tcp_state == TCPS_BOUND || 27402 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 27403 /* 27404 * Handle listen() increasing backlog. 27405 * This is more "liberal" then what the TPI spec 27406 * requires but is needed to avoid a t_unbind 27407 * when handling listen() since the port number 27408 * might be "stolen" between the unbind and bind. 27409 */ 27410 goto do_listen; 27411 } 27412 if (tcp->tcp_debug) { 27413 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27414 "tcp_listen: bad state, %d", tcp->tcp_state); 27415 } 27416 return (-TOUTSTATE); 27417 } else { 27418 if (sa == NULL) { 27419 sin6_t addr; 27420 sin_t *sin; 27421 sin6_t *sin6; 27422 27423 ASSERT(IPCL_IS_NONSTR(connp)); 27424 27425 /* Do an implicit bind: Request for a generic port. */ 27426 if (tcp->tcp_family == AF_INET) { 27427 len = sizeof (sin_t); 27428 sin = (sin_t *)&addr; 27429 *sin = sin_null; 27430 sin->sin_family = AF_INET; 27431 tcp->tcp_ipversion = IPV4_VERSION; 27432 } else { 27433 ASSERT(tcp->tcp_family == AF_INET6); 27434 len = sizeof (sin6_t); 27435 sin6 = (sin6_t *)&addr; 27436 *sin6 = sin6_null; 27437 sin6->sin6_family = AF_INET6; 27438 tcp->tcp_ipversion = IPV6_VERSION; 27439 } 27440 sa = (struct sockaddr *)&addr; 27441 } 27442 27443 error = tcp_bind_check(connp, sa, len, cr, 27444 bind_to_req_port_only); 27445 if (error) 27446 return (error); 27447 /* Fall through and do the fanout insertion */ 27448 } 27449 27450 do_listen: 27451 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 27452 tcp->tcp_conn_req_max = backlog; 27453 if (tcp->tcp_conn_req_max) { 27454 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 27455 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 27456 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 27457 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 27458 /* 27459 * If this is a listener, do not reset the eager list 27460 * and other stuffs. Note that we don't check if the 27461 * existing eager list meets the new tcp_conn_req_max 27462 * requirement. 27463 */ 27464 if (tcp->tcp_state != TCPS_LISTEN) { 27465 tcp->tcp_state = TCPS_LISTEN; 27466 /* Initialize the chain. Don't need the eager_lock */ 27467 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 27468 tcp->tcp_eager_next_drop_q0 = tcp; 27469 tcp->tcp_eager_prev_drop_q0 = tcp; 27470 tcp->tcp_second_ctimer_threshold = 27471 tcps->tcps_ip_abort_linterval; 27472 } 27473 } 27474 27475 /* 27476 * We can call ip_bind directly, the processing continues 27477 * in tcp_post_ip_bind(). 27478 * 27479 * We need to make sure that the conn_recv is set to a non-null 27480 * value before we insert the conn into the classifier table. 27481 * This is to avoid a race with an incoming packet which does an 27482 * ipcl_classify(). 27483 */ 27484 connp->conn_recv = tcp_conn_request; 27485 if (tcp->tcp_family == AF_INET) { 27486 error = ip_proto_bind_laddr_v4(connp, NULL, 27487 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 27488 } else { 27489 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27490 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 27491 } 27492 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27493 } 27494 27495 void 27496 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 27497 { 27498 conn_t *connp = (conn_t *)proto_handle; 27499 tcp_t *tcp = connp->conn_tcp; 27500 tcp_stack_t *tcps = tcp->tcp_tcps; 27501 uint_t thwin; 27502 27503 ASSERT(connp->conn_upper_handle != NULL); 27504 27505 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 27506 27507 /* Flow control condition has been removed. */ 27508 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27509 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 27510 << tcp->tcp_rcv_ws; 27511 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 27512 /* 27513 * Send back a window update immediately if TCP is above 27514 * ESTABLISHED state and the increase of the rcv window 27515 * that the other side knows is at least 1 MSS after flow 27516 * control is lifted. 27517 */ 27518 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27519 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 27520 tcp_xmit_ctl(NULL, tcp, 27521 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27522 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27523 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 27524 } 27525 27526 squeue_synch_exit(connp->conn_sqp, connp); 27527 } 27528 27529 /* ARGSUSED */ 27530 int 27531 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27532 int mode, int32_t *rvalp, cred_t *cr) 27533 { 27534 conn_t *connp = (conn_t *)proto_handle; 27535 int error; 27536 27537 ASSERT(connp->conn_upper_handle != NULL); 27538 27539 /* All Solaris components should pass a cred for this operation. */ 27540 ASSERT(cr != NULL); 27541 27542 switch (cmd) { 27543 case ND_SET: 27544 case ND_GET: 27545 case TCP_IOC_DEFAULT_Q: 27546 case _SIOCSOCKFALLBACK: 27547 case TCP_IOC_ABORT_CONN: 27548 case TI_GETPEERNAME: 27549 case TI_GETMYNAME: 27550 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27551 cmd)); 27552 error = EINVAL; 27553 break; 27554 default: 27555 /* 27556 * Pass on to IP using helper stream 27557 */ 27558 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27559 cmd, arg, mode, cr, rvalp); 27560 break; 27561 } 27562 return (error); 27563 } 27564 27565 sock_downcalls_t sock_tcp_downcalls = { 27566 tcp_activate, 27567 tcp_accept, 27568 tcp_bind, 27569 tcp_listen, 27570 tcp_connect, 27571 tcp_getpeername, 27572 tcp_getsockname, 27573 tcp_getsockopt, 27574 tcp_setsockopt, 27575 tcp_sendmsg, 27576 NULL, 27577 NULL, 27578 NULL, 27579 tcp_shutdown, 27580 tcp_clr_flowctrl, 27581 tcp_ioctl, 27582 tcp_close, 27583 }; 27584