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 <net/pfkeyv2.h> 90 #include <inet/ipsec_info.h> 91 #include <inet/ipdrop.h> 92 93 #include <inet/ipclassifier.h> 94 #include <inet/ip_ire.h> 95 #include <inet/ip_ftable.h> 96 #include <inet/ip_if.h> 97 #include <inet/ipp_common.h> 98 #include <inet/ip_netinfo.h> 99 #include <sys/squeue_impl.h> 100 #include <sys/squeue.h> 101 #include <inet/kssl/ksslapi.h> 102 #include <sys/tsol/label.h> 103 #include <sys/tsol/tnet.h> 104 #include <rpc/pmap_prot.h> 105 #include <sys/callo.h> 106 107 /* 108 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 109 * 110 * (Read the detailed design doc in PSARC case directory) 111 * 112 * The entire tcp state is contained in tcp_t and conn_t structure 113 * which are allocated in tandem using ipcl_conn_create() and passing 114 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 115 * the references on the tcp_t. The tcp_t structure is never compressed 116 * and packets always land on the correct TCP perimeter from the time 117 * eager is created till the time tcp_t dies (as such the old mentat 118 * TCP global queue is not used for detached state and no IPSEC checking 119 * is required). The global queue is still allocated to send out resets 120 * for connection which have no listeners and IP directly calls 121 * tcp_xmit_listeners_reset() which does any policy check. 122 * 123 * Protection and Synchronisation mechanism: 124 * 125 * The tcp data structure does not use any kind of lock for protecting 126 * its state but instead uses 'squeues' for mutual exclusion from various 127 * read and write side threads. To access a tcp member, the thread should 128 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS, 129 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller 130 * can pass any tcp function having prototype of edesc_t as argument 131 * (different from traditional STREAMs model where packets come in only 132 * designated entry points). The list of functions that can be directly 133 * called via squeue are listed before the usual function prototype. 134 * 135 * Referencing: 136 * 137 * TCP is MT-Hot and we use a reference based scheme to make sure that the 138 * tcp structure doesn't disappear when its needed. When the application 139 * creates an outgoing connection or accepts an incoming connection, we 140 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 141 * The IP reference is just a symbolic reference since ip_tcpclose() 142 * looks at tcp structure after tcp_close_output() returns which could 143 * have dropped the last TCP reference. So as long as the connection is 144 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 145 * conn_t. The classifier puts its own reference when the connection is 146 * inserted in listen or connected hash. Anytime a thread needs to enter 147 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 148 * on write side or by doing a classify on read side and then puts a 149 * reference on the conn before doing squeue_enter/tryenter/fill. For 150 * read side, the classifier itself puts the reference under fanout lock 151 * to make sure that tcp can't disappear before it gets processed. The 152 * squeue will drop this reference automatically so the called function 153 * doesn't have to do a DEC_REF. 154 * 155 * Opening a new connection: 156 * 157 * The outgoing connection open is pretty simple. tcp_open() does the 158 * work in creating the conn/tcp structure and initializing it. The 159 * squeue assignment is done based on the CPU the application 160 * is running on. So for outbound connections, processing is always done 161 * on application CPU which might be different from the incoming CPU 162 * being interrupted by the NIC. An optimal way would be to figure out 163 * the NIC <-> CPU binding at listen time, and assign the outgoing 164 * connection to the squeue attached to the CPU that will be interrupted 165 * for incoming packets (we know the NIC based on the bind IP address). 166 * This might seem like a problem if more data is going out but the 167 * fact is that in most cases the transmit is ACK driven transmit where 168 * the outgoing data normally sits on TCP's xmit queue waiting to be 169 * transmitted. 170 * 171 * Accepting a connection: 172 * 173 * This is a more interesting case because of various races involved in 174 * establishing a eager in its own perimeter. Read the meta comment on 175 * top of tcp_conn_request(). But briefly, the squeue is picked by 176 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 177 * 178 * Closing a connection: 179 * 180 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 181 * via squeue to do the close and mark the tcp as detached if the connection 182 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 183 * reference but tcp_close() drop IP's reference always. So if tcp was 184 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 185 * and 1 because it is in classifier's connected hash. This is the condition 186 * we use to determine that its OK to clean up the tcp outside of squeue 187 * when time wait expires (check the ref under fanout and conn_lock and 188 * if it is 2, remove it from fanout hash and kill it). 189 * 190 * Although close just drops the necessary references and marks the 191 * tcp_detached state, tcp_close needs to know the tcp_detached has been 192 * set (under squeue) before letting the STREAM go away (because a 193 * inbound packet might attempt to go up the STREAM while the close 194 * has happened and tcp_detached is not set). So a special lock and 195 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 196 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 197 * tcp_detached. 198 * 199 * Special provisions and fast paths: 200 * 201 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 202 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 203 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 204 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 205 * check to send packets directly to tcp_rput_data via squeue. Everyone 206 * else comes through tcp_input() on the read side. 207 * 208 * We also make special provisions for sockfs by marking tcp_issocket 209 * whenever we have only sockfs on top of TCP. This allows us to skip 210 * putting the tcp in acceptor hash since a sockfs listener can never 211 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 212 * since eager has already been allocated and the accept now happens 213 * on acceptor STREAM. There is a big blob of comment on top of 214 * tcp_conn_request explaining the new accept. When socket is POP'd, 215 * sockfs sends us an ioctl to mark the fact and we go back to old 216 * behaviour. Once tcp_issocket is unset, its never set for the 217 * life of that connection. 218 * 219 * In support of on-board asynchronous DMA hardware (e.g. Intel I/OAT) 220 * two consoldiation private KAPIs are used to enqueue M_DATA mblk_t's 221 * directly to the socket (sodirect) and start an asynchronous copyout 222 * to a user-land receive-side buffer (uioa) when a blocking socket read 223 * (e.g. read, recv, ...) is pending. 224 * 225 * This is accomplished when tcp_issocket is set and tcp_sodirect is not 226 * NULL so points to an sodirect_t and if marked enabled then we enqueue 227 * all mblk_t's directly to the socket. 228 * 229 * Further, if the sodirect_t sod_uioa and if marked enabled (due to a 230 * blocking socket read, e.g. user-land read, recv, ...) then an asynchronous 231 * copyout will be started directly to the user-land uio buffer. Also, as we 232 * have a pending read, TCP's push logic can take into account the number of 233 * bytes to be received and only awake the blocked read()er when the uioa_t 234 * byte count has been satisfied. 235 * 236 * IPsec notes : 237 * 238 * Since a packet is always executed on the correct TCP perimeter 239 * all IPsec processing is defered to IP including checking new 240 * connections and setting IPSEC policies for new connection. The 241 * only exception is tcp_xmit_listeners_reset() which is called 242 * directly from IP and needs to policy check to see if TH_RST 243 * can be sent out. 244 * 245 * PFHooks notes : 246 * 247 * For mdt case, one meta buffer contains multiple packets. Mblks for every 248 * packet are assembled and passed to the hooks. When packets are blocked, 249 * or boundary of any packet is changed, the mdt processing is stopped, and 250 * packets of the meta buffer are send to the IP path one by one. 251 */ 252 253 /* 254 * Values for squeue switch: 255 * 1: SQ_NODRAIN 256 * 2: SQ_PROCESS 257 * 3: SQ_FILL 258 */ 259 int tcp_squeue_wput = 2; /* /etc/systems */ 260 int tcp_squeue_flag; 261 262 /* 263 * Macros for sodirect: 264 * 265 * SOD_PTR_ENTER(tcp, sodp) - for the tcp_t pointer "tcp" set the 266 * sodirect_t pointer "sodp" to the socket/tcp shared sodirect_t 267 * if it exists and is enabled, else to NULL. Note, in the current 268 * sodirect implementation the sod_lockp must not be held across any 269 * STREAMS call (e.g. putnext) else a "recursive mutex_enter" PANIC 270 * will result as sod_lockp is the streamhead stdata.sd_lock. 271 * 272 * SOD_NOT_ENABLED(tcp) - return true if not a sodirect tcp_t or the 273 * sodirect_t isn't enabled, usefull for ASSERT()ing that a recieve 274 * side tcp code path dealing with a tcp_rcv_list or putnext() isn't 275 * being used when sodirect code paths should be. 276 */ 277 278 #define SOD_PTR_ENTER(tcp, sodp) \ 279 (sodp) = (tcp)->tcp_sodirect; \ 280 \ 281 if ((sodp) != NULL) { \ 282 mutex_enter((sodp)->sod_lockp); \ 283 if (!((sodp)->sod_state & SOD_ENABLED)) { \ 284 mutex_exit((sodp)->sod_lockp); \ 285 (sodp) = NULL; \ 286 } \ 287 } 288 289 #define SOD_NOT_ENABLED(tcp) \ 290 ((tcp)->tcp_sodirect == NULL || \ 291 !((tcp)->tcp_sodirect->sod_state & SOD_ENABLED)) 292 293 /* 294 * This controls how tiny a write must be before we try to copy it 295 * into the the mblk on the tail of the transmit queue. Not much 296 * speedup is observed for values larger than sixteen. Zero will 297 * disable the optimisation. 298 */ 299 int tcp_tx_pull_len = 16; 300 301 /* 302 * TCP Statistics. 303 * 304 * How TCP statistics work. 305 * 306 * There are two types of statistics invoked by two macros. 307 * 308 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 309 * supposed to be used in non MT-hot paths of the code. 310 * 311 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 312 * supposed to be used for DEBUG purposes and may be used on a hot path. 313 * 314 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 315 * (use "kstat tcp" to get them). 316 * 317 * There is also additional debugging facility that marks tcp_clean_death() 318 * instances and saves them in tcp_t structure. It is triggered by 319 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 320 * tcp_clean_death() calls that counts the number of times each tag was hit. It 321 * is triggered by TCP_CLD_COUNTERS define. 322 * 323 * How to add new counters. 324 * 325 * 1) Add a field in the tcp_stat structure describing your counter. 326 * 2) Add a line in the template in tcp_kstat2_init() with the name 327 * of the counter. 328 * 329 * IMPORTANT!! - make sure that both are in sync !! 330 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 331 * 332 * Please avoid using private counters which are not kstat-exported. 333 * 334 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 335 * in tcp_t structure. 336 * 337 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 338 */ 339 340 #ifndef TCP_DEBUG_COUNTER 341 #ifdef DEBUG 342 #define TCP_DEBUG_COUNTER 1 343 #else 344 #define TCP_DEBUG_COUNTER 0 345 #endif 346 #endif 347 348 #define TCP_CLD_COUNTERS 0 349 350 #define TCP_TAG_CLEAN_DEATH 1 351 #define TCP_MAX_CLEAN_DEATH_TAG 32 352 353 #ifdef lint 354 static int _lint_dummy_; 355 #endif 356 357 #if TCP_CLD_COUNTERS 358 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 359 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 360 #elif defined(lint) 361 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 362 #else 363 #define TCP_CLD_STAT(x) 364 #endif 365 366 #if TCP_DEBUG_COUNTER 367 #define TCP_DBGSTAT(tcps, x) \ 368 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 369 #define TCP_G_DBGSTAT(x) \ 370 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 371 #elif defined(lint) 372 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 373 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 374 #else 375 #define TCP_DBGSTAT(tcps, x) 376 #define TCP_G_DBGSTAT(x) 377 #endif 378 379 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 380 381 tcp_g_stat_t tcp_g_statistics; 382 kstat_t *tcp_g_kstat; 383 384 /* 385 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 386 * tcp write side. 387 */ 388 #define CALL_IP_WPUT(connp, q, mp) { \ 389 ASSERT(((q)->q_flag & QREADR) == 0); \ 390 TCP_DBGSTAT(connp->conn_netstack->netstack_tcp, tcp_ip_output); \ 391 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 392 } 393 394 /* Macros for timestamp comparisons */ 395 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 396 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 397 398 /* 399 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 400 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 401 * by adding three components: a time component which grows by 1 every 4096 402 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 403 * a per-connection component which grows by 125000 for every new connection; 404 * and an "extra" component that grows by a random amount centered 405 * approximately on 64000. This causes the the ISS generator to cycle every 406 * 4.89 hours if no TCP connections are made, and faster if connections are 407 * made. 408 * 409 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 410 * components: a time component which grows by 250000 every second; and 411 * a per-connection component which grows by 125000 for every new connections. 412 * 413 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 414 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 415 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 416 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 417 * password. 418 */ 419 #define ISS_INCR 250000 420 #define ISS_NSEC_SHT 12 421 422 static sin_t sin_null; /* Zero address for quick clears */ 423 static sin6_t sin6_null; /* Zero address for quick clears */ 424 425 /* 426 * This implementation follows the 4.3BSD interpretation of the urgent 427 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 428 * incompatible changes in protocols like telnet and rlogin. 429 */ 430 #define TCP_OLD_URP_INTERPRETATION 1 431 432 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 433 (TCP_IS_DETACHED(tcp) && \ 434 (!(tcp)->tcp_hard_binding)) 435 436 /* 437 * TCP reassembly macros. We hide starting and ending sequence numbers in 438 * b_next and b_prev of messages on the reassembly queue. The messages are 439 * chained using b_cont. These macros are used in tcp_reass() so we don't 440 * have to see the ugly casts and assignments. 441 */ 442 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 443 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 444 (mblk_t *)(uintptr_t)(u)) 445 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 446 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 447 (mblk_t *)(uintptr_t)(u)) 448 449 /* 450 * Implementation of TCP Timers. 451 * ============================= 452 * 453 * INTERFACE: 454 * 455 * There are two basic functions dealing with tcp timers: 456 * 457 * timeout_id_t tcp_timeout(connp, func, time) 458 * clock_t tcp_timeout_cancel(connp, timeout_id) 459 * TCP_TIMER_RESTART(tcp, intvl) 460 * 461 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 462 * after 'time' ticks passed. The function called by timeout() must adhere to 463 * the same restrictions as a driver soft interrupt handler - it must not sleep 464 * or call other functions that might sleep. The value returned is the opaque 465 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 466 * cancel the request. The call to tcp_timeout() may fail in which case it 467 * returns zero. This is different from the timeout(9F) function which never 468 * fails. 469 * 470 * The call-back function 'func' always receives 'connp' as its single 471 * argument. It is always executed in the squeue corresponding to the tcp 472 * structure. The tcp structure is guaranteed to be present at the time the 473 * call-back is called. 474 * 475 * NOTE: The call-back function 'func' is never called if tcp is in 476 * the TCPS_CLOSED state. 477 * 478 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 479 * request. locks acquired by the call-back routine should not be held across 480 * the call to tcp_timeout_cancel() or a deadlock may result. 481 * 482 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 483 * Otherwise, it returns an integer value greater than or equal to 0. In 484 * particular, if the call-back function is already placed on the squeue, it can 485 * not be canceled. 486 * 487 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 488 * within squeue context corresponding to the tcp instance. Since the 489 * call-back is also called via the same squeue, there are no race 490 * conditions described in untimeout(9F) manual page since all calls are 491 * strictly serialized. 492 * 493 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 494 * stored in tcp_timer_tid and starts a new one using 495 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 496 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 497 * field. 498 * 499 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 500 * call-back may still be called, so it is possible tcp_timer() will be 501 * called several times. This should not be a problem since tcp_timer() 502 * should always check the tcp instance state. 503 * 504 * 505 * IMPLEMENTATION: 506 * 507 * TCP timers are implemented using three-stage process. The call to 508 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 509 * when the timer expires. The tcp_timer_callback() arranges the call of the 510 * tcp_timer_handler() function via squeue corresponding to the tcp 511 * instance. The tcp_timer_handler() calls actual requested timeout call-back 512 * and passes tcp instance as an argument to it. Information is passed between 513 * stages using the tcp_timer_t structure which contains the connp pointer, the 514 * tcp call-back to call and the timeout id returned by the timeout(9F). 515 * 516 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 517 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 518 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 519 * returns the pointer to this mblk. 520 * 521 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 522 * looks like a normal mblk without actual dblk attached to it. 523 * 524 * To optimize performance each tcp instance holds a small cache of timer 525 * mblocks. In the current implementation it caches up to two timer mblocks per 526 * tcp instance. The cache is preserved over tcp frees and is only freed when 527 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 528 * timer processing happens on a corresponding squeue, the cache manipulation 529 * does not require any locks. Experiments show that majority of timer mblocks 530 * allocations are satisfied from the tcp cache and do not involve kmem calls. 531 * 532 * The tcp_timeout() places a refhold on the connp instance which guarantees 533 * that it will be present at the time the call-back function fires. The 534 * tcp_timer_handler() drops the reference after calling the call-back, so the 535 * call-back function does not need to manipulate the references explicitly. 536 */ 537 538 typedef struct tcp_timer_s { 539 conn_t *connp; 540 void (*tcpt_proc)(void *); 541 callout_id_t tcpt_tid; 542 } tcp_timer_t; 543 544 static kmem_cache_t *tcp_timercache; 545 kmem_cache_t *tcp_sack_info_cache; 546 kmem_cache_t *tcp_iphc_cache; 547 548 /* 549 * For scalability, we must not run a timer for every TCP connection 550 * in TIME_WAIT state. To see why, consider (for time wait interval of 551 * 4 minutes): 552 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 553 * 554 * This list is ordered by time, so you need only delete from the head 555 * until you get to entries which aren't old enough to delete yet. 556 * The list consists of only the detached TIME_WAIT connections. 557 * 558 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 559 * becomes detached TIME_WAIT (either by changing the state and already 560 * being detached or the other way around). This means that the TIME_WAIT 561 * state can be extended (up to doubled) if the connection doesn't become 562 * detached for a long time. 563 * 564 * The list manipulations (including tcp_time_wait_next/prev) 565 * are protected by the tcp_time_wait_lock. The content of the 566 * detached TIME_WAIT connections is protected by the normal perimeters. 567 * 568 * This list is per squeue and squeues are shared across the tcp_stack_t's. 569 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 570 * and conn_netstack. 571 * The tcp_t's that are added to tcp_free_list are disassociated and 572 * have NULL tcp_tcps and conn_netstack pointers. 573 */ 574 typedef struct tcp_squeue_priv_s { 575 kmutex_t tcp_time_wait_lock; 576 callout_id_t tcp_time_wait_tid; 577 tcp_t *tcp_time_wait_head; 578 tcp_t *tcp_time_wait_tail; 579 tcp_t *tcp_free_list; 580 uint_t tcp_free_list_cnt; 581 } tcp_squeue_priv_t; 582 583 /* 584 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 585 * Running it every 5 seconds seems to give the best results. 586 */ 587 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 588 589 /* 590 * To prevent memory hog, limit the number of entries in tcp_free_list 591 * to 1% of available memory / number of cpus 592 */ 593 uint_t tcp_free_list_max_cnt = 0; 594 595 #define TCP_XMIT_LOWATER 4096 596 #define TCP_XMIT_HIWATER 49152 597 #define TCP_RECV_LOWATER 2048 598 #define TCP_RECV_HIWATER 49152 599 600 /* 601 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 602 */ 603 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 604 605 #define TIDUSZ 4096 /* transport interface data unit size */ 606 607 /* 608 * Bind hash list size and has function. It has to be a power of 2 for 609 * hashing. 610 */ 611 #define TCP_BIND_FANOUT_SIZE 512 612 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 613 /* 614 * Size of listen and acceptor hash list. It has to be a power of 2 for 615 * hashing. 616 */ 617 #define TCP_FANOUT_SIZE 256 618 619 #ifdef _ILP32 620 #define TCP_ACCEPTOR_HASH(accid) \ 621 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 622 #else 623 #define TCP_ACCEPTOR_HASH(accid) \ 624 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 625 #endif /* _ILP32 */ 626 627 #define IP_ADDR_CACHE_SIZE 2048 628 #define IP_ADDR_CACHE_HASH(faddr) \ 629 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 630 631 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 632 #define TCP_HSP_HASH_SIZE 256 633 634 #define TCP_HSP_HASH(addr) \ 635 (((addr>>24) ^ (addr >>16) ^ \ 636 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 637 638 /* 639 * TCP options struct returned from tcp_parse_options. 640 */ 641 typedef struct tcp_opt_s { 642 uint32_t tcp_opt_mss; 643 uint32_t tcp_opt_wscale; 644 uint32_t tcp_opt_ts_val; 645 uint32_t tcp_opt_ts_ecr; 646 tcp_t *tcp; 647 } tcp_opt_t; 648 649 /* 650 * TCP option struct passing information b/w lisenter and eager. 651 */ 652 struct tcp_options { 653 uint_t to_flags; 654 ssize_t to_boundif; /* IPV6_BOUND_IF */ 655 }; 656 657 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 658 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 659 660 /* 661 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 662 */ 663 664 #ifdef _BIG_ENDIAN 665 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 666 (TCPOPT_TSTAMP << 8) | 10) 667 #else 668 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 669 (TCPOPT_NOP << 8) | TCPOPT_NOP) 670 #endif 671 672 /* 673 * Flags returned from tcp_parse_options. 674 */ 675 #define TCP_OPT_MSS_PRESENT 1 676 #define TCP_OPT_WSCALE_PRESENT 2 677 #define TCP_OPT_TSTAMP_PRESENT 4 678 #define TCP_OPT_SACK_OK_PRESENT 8 679 #define TCP_OPT_SACK_PRESENT 16 680 681 /* TCP option length */ 682 #define TCPOPT_NOP_LEN 1 683 #define TCPOPT_MAXSEG_LEN 4 684 #define TCPOPT_WS_LEN 3 685 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 686 #define TCPOPT_TSTAMP_LEN 10 687 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 688 #define TCPOPT_SACK_OK_LEN 2 689 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 690 #define TCPOPT_REAL_SACK_LEN 4 691 #define TCPOPT_MAX_SACK_LEN 36 692 #define TCPOPT_HEADER_LEN 2 693 694 /* TCP cwnd burst factor. */ 695 #define TCP_CWND_INFINITE 65535 696 #define TCP_CWND_SS 3 697 #define TCP_CWND_NORMAL 5 698 699 /* Maximum TCP initial cwin (start/restart). */ 700 #define TCP_MAX_INIT_CWND 8 701 702 /* 703 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 704 * either tcp_slow_start_initial or tcp_slow_start_after idle 705 * depending on the caller. If the upper layer has not used the 706 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 707 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 708 * If the upper layer has changed set the tcp_init_cwnd, just use 709 * it to calculate the tcp_cwnd. 710 */ 711 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 712 { \ 713 if ((tcp)->tcp_init_cwnd == 0) { \ 714 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 715 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 716 } else { \ 717 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 718 } \ 719 tcp->tcp_cwnd_cnt = 0; \ 720 } 721 722 /* TCP Timer control structure */ 723 typedef struct tcpt_s { 724 pfv_t tcpt_pfv; /* The routine we are to call */ 725 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 726 } tcpt_t; 727 728 /* Host Specific Parameter structure */ 729 typedef struct tcp_hsp { 730 struct tcp_hsp *tcp_hsp_next; 731 in6_addr_t tcp_hsp_addr_v6; 732 in6_addr_t tcp_hsp_subnet_v6; 733 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 734 int32_t tcp_hsp_sendspace; 735 int32_t tcp_hsp_recvspace; 736 int32_t tcp_hsp_tstamp; 737 } tcp_hsp_t; 738 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 739 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 740 741 /* 742 * Functions called directly via squeue having a prototype of edesc_t. 743 */ 744 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 745 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 746 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 747 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 748 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 749 void tcp_input(void *arg, mblk_t *mp, void *arg2); 750 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 751 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 752 void tcp_output(void *arg, mblk_t *mp, void *arg2); 753 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 754 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 755 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 756 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 757 758 759 /* Prototype for TCP functions */ 760 static void tcp_random_init(void); 761 int tcp_random(void); 762 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 763 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 764 tcp_t *eager); 765 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 766 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 767 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 768 boolean_t user_specified); 769 static void tcp_closei_local(tcp_t *tcp); 770 static void tcp_close_detached(tcp_t *tcp); 771 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 772 mblk_t *idmp, mblk_t **defermp); 773 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 774 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 775 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 776 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 777 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 778 uint32_t scope_id, cred_t *cr, pid_t pid); 779 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 780 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 781 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 782 static char *tcp_display(tcp_t *tcp, char *, char); 783 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 784 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 785 static void tcp_eager_unlink(tcp_t *tcp); 786 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 787 int unixerr); 788 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 789 int tlierr, int unixerr); 790 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 791 cred_t *cr); 792 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 793 char *value, caddr_t cp, cred_t *cr); 794 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 795 char *value, caddr_t cp, cred_t *cr); 796 static int tcp_tpistate(tcp_t *tcp); 797 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 798 int caller_holds_lock); 799 static void tcp_bind_hash_remove(tcp_t *tcp); 800 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 801 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 802 static void tcp_acceptor_hash_remove(tcp_t *tcp); 803 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 804 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 805 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 806 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 807 void tcp_g_q_setup(tcp_stack_t *); 808 void tcp_g_q_create(tcp_stack_t *); 809 void tcp_g_q_destroy(tcp_stack_t *); 810 static int tcp_header_init_ipv4(tcp_t *tcp); 811 static int tcp_header_init_ipv6(tcp_t *tcp); 812 int tcp_init(tcp_t *tcp, queue_t *q); 813 static int tcp_init_values(tcp_t *tcp); 814 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 815 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 816 static void tcp_ip_notify(tcp_t *tcp); 817 static mblk_t *tcp_ire_mp(mblk_t **mpp); 818 static void tcp_iss_init(tcp_t *tcp); 819 static void tcp_keepalive_killer(void *arg); 820 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 821 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 822 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 823 int *do_disconnectp, int *t_errorp, int *sys_errorp); 824 static boolean_t tcp_allow_connopt_set(int level, int name); 825 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 826 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 827 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 828 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 829 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 830 mblk_t *mblk); 831 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 832 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 833 uchar_t *ptr, uint_t len); 834 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 835 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 836 tcp_stack_t *); 837 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 838 caddr_t cp, cred_t *cr); 839 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 840 caddr_t cp, cred_t *cr); 841 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 842 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 843 caddr_t cp, cred_t *cr); 844 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 845 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 846 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 847 static void tcp_reinit(tcp_t *tcp); 848 static void tcp_reinit_values(tcp_t *tcp); 849 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 850 tcp_t *thisstream, cred_t *cr); 851 852 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 853 static uint_t tcp_rcv_drain(tcp_t *tcp); 854 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 855 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 856 static void tcp_ss_rexmit(tcp_t *tcp); 857 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 858 static void tcp_process_options(tcp_t *, tcph_t *); 859 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 860 static void tcp_rsrv(queue_t *q); 861 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 862 static int tcp_snmp_state(tcp_t *tcp); 863 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 864 cred_t *cr); 865 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 866 cred_t *cr); 867 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 868 cred_t *cr); 869 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 870 cred_t *cr); 871 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 872 cred_t *cr); 873 static void tcp_timer(void *arg); 874 static void tcp_timer_callback(void *); 875 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 876 boolean_t random); 877 static in_port_t tcp_get_next_priv_port(const tcp_t *); 878 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 879 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 880 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 881 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 882 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 883 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 884 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 885 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 886 const int num_sack_blk, int *usable, uint_t *snxt, 887 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 888 const int mdt_thres); 889 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 890 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 891 const int num_sack_blk, int *usable, uint_t *snxt, 892 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 893 const int mdt_thres); 894 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 895 int num_sack_blk); 896 static void tcp_wsrv(queue_t *q); 897 static int tcp_xmit_end(tcp_t *tcp); 898 static void tcp_ack_timer(void *arg); 899 static mblk_t *tcp_ack_mp(tcp_t *tcp); 900 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 901 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 902 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 903 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 904 uint32_t ack, int ctl); 905 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 906 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 907 static int setmaxps(queue_t *q, int maxpsz); 908 static void tcp_set_rto(tcp_t *, time_t); 909 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 910 boolean_t, boolean_t); 911 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 912 boolean_t ipsec_mctl); 913 static int tcp_build_hdrs(tcp_t *); 914 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 915 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 916 tcph_t *tcph); 917 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 918 static mblk_t *tcp_mdt_info_mp(mblk_t *); 919 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 920 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 921 const boolean_t, const uint32_t, const uint32_t, 922 const uint32_t, const uint32_t, tcp_stack_t *); 923 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 924 const uint_t, const uint_t, boolean_t *); 925 static mblk_t *tcp_lso_info_mp(mblk_t *); 926 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 927 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 928 extern mblk_t *tcp_timermp_alloc(int); 929 extern void tcp_timermp_free(tcp_t *); 930 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 931 static void tcp_stop_lingering(tcp_t *tcp); 932 static void tcp_close_linger_timeout(void *arg); 933 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 934 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 935 static void tcp_stack_fini(netstackid_t stackid, void *arg); 936 static void *tcp_g_kstat_init(tcp_g_stat_t *); 937 static void tcp_g_kstat_fini(kstat_t *); 938 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 939 static void tcp_kstat_fini(netstackid_t, kstat_t *); 940 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 941 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 942 static int tcp_kstat_update(kstat_t *kp, int rw); 943 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 944 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 945 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 946 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 947 tcph_t *tcph, mblk_t *idmp); 948 static int tcp_squeue_switch(int); 949 950 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 951 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 952 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 953 static int tcp_tpi_close(queue_t *, int); 954 static int tcpclose_accept(queue_t *); 955 956 static void tcp_squeue_add(squeue_t *); 957 static boolean_t tcp_zcopy_check(tcp_t *); 958 static void tcp_zcopy_notify(tcp_t *); 959 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 960 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 961 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 962 963 extern void tcp_kssl_input(tcp_t *, mblk_t *); 964 965 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 966 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 967 968 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 969 sock_upper_handle_t, cred_t *); 970 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 971 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 972 static int tcp_do_listen(conn_t *, int, cred_t *); 973 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 974 cred_t *, pid_t); 975 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 976 boolean_t); 977 static int tcp_do_unbind(conn_t *); 978 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 979 boolean_t); 980 981 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 982 983 /* 984 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 985 * 986 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 987 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 988 * (defined in tcp.h) needs to be filled in and passed into the kernel 989 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 990 * structure contains the four-tuple of a TCP connection and a range of TCP 991 * states (specified by ac_start and ac_end). The use of wildcard addresses 992 * and ports is allowed. Connections with a matching four tuple and a state 993 * within the specified range will be aborted. The valid states for the 994 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 995 * inclusive. 996 * 997 * An application which has its connection aborted by this ioctl will receive 998 * an error that is dependent on the connection state at the time of the abort. 999 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1000 * though a RST packet has been received. If the connection state is equal to 1001 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1002 * and all resources associated with the connection will be freed. 1003 */ 1004 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1005 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1006 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1007 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 1008 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1009 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1010 boolean_t, tcp_stack_t *); 1011 1012 static struct module_info tcp_rinfo = { 1013 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1014 }; 1015 1016 static struct module_info tcp_winfo = { 1017 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1018 }; 1019 1020 /* 1021 * Entry points for TCP as a device. The normal case which supports 1022 * the TCP functionality. 1023 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 1024 */ 1025 struct qinit tcp_rinitv4 = { 1026 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 1027 }; 1028 1029 struct qinit tcp_rinitv6 = { 1030 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 1031 }; 1032 1033 struct qinit tcp_winit = { 1034 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1035 }; 1036 1037 /* Initial entry point for TCP in socket mode. */ 1038 struct qinit tcp_sock_winit = { 1039 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1040 }; 1041 1042 /* TCP entry point during fallback */ 1043 struct qinit tcp_fallback_sock_winit = { 1044 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 1045 }; 1046 1047 /* 1048 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1049 * an accept. Avoid allocating data structures since eager has already 1050 * been created. 1051 */ 1052 struct qinit tcp_acceptor_rinit = { 1053 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1054 }; 1055 1056 struct qinit tcp_acceptor_winit = { 1057 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1058 }; 1059 1060 /* 1061 * Entry points for TCP loopback (read side only) 1062 * The open routine is only used for reopens, thus no need to 1063 * have a separate one for tcp_openv6. 1064 */ 1065 struct qinit tcp_loopback_rinit = { 1066 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 1067 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1068 }; 1069 1070 /* For AF_INET aka /dev/tcp */ 1071 struct streamtab tcpinfov4 = { 1072 &tcp_rinitv4, &tcp_winit 1073 }; 1074 1075 /* For AF_INET6 aka /dev/tcp6 */ 1076 struct streamtab tcpinfov6 = { 1077 &tcp_rinitv6, &tcp_winit 1078 }; 1079 1080 sock_downcalls_t sock_tcp_downcalls; 1081 1082 /* 1083 * Have to ensure that tcp_g_q_close is not done by an 1084 * interrupt thread. 1085 */ 1086 static taskq_t *tcp_taskq; 1087 1088 /* Setable only in /etc/system. Move to ndd? */ 1089 boolean_t tcp_icmp_source_quench = B_FALSE; 1090 1091 /* 1092 * Following assumes TPI alignment requirements stay along 32 bit 1093 * boundaries 1094 */ 1095 #define ROUNDUP32(x) \ 1096 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1097 1098 /* Template for response to info request. */ 1099 static struct T_info_ack tcp_g_t_info_ack = { 1100 T_INFO_ACK, /* PRIM_type */ 1101 0, /* TSDU_size */ 1102 T_INFINITE, /* ETSDU_size */ 1103 T_INVALID, /* CDATA_size */ 1104 T_INVALID, /* DDATA_size */ 1105 sizeof (sin_t), /* ADDR_size */ 1106 0, /* OPT_size - not initialized here */ 1107 TIDUSZ, /* TIDU_size */ 1108 T_COTS_ORD, /* SERV_type */ 1109 TCPS_IDLE, /* CURRENT_state */ 1110 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1111 }; 1112 1113 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1114 T_INFO_ACK, /* PRIM_type */ 1115 0, /* TSDU_size */ 1116 T_INFINITE, /* ETSDU_size */ 1117 T_INVALID, /* CDATA_size */ 1118 T_INVALID, /* DDATA_size */ 1119 sizeof (sin6_t), /* ADDR_size */ 1120 0, /* OPT_size - not initialized here */ 1121 TIDUSZ, /* TIDU_size */ 1122 T_COTS_ORD, /* SERV_type */ 1123 TCPS_IDLE, /* CURRENT_state */ 1124 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1125 }; 1126 1127 #define MS 1L 1128 #define SECONDS (1000 * MS) 1129 #define MINUTES (60 * SECONDS) 1130 #define HOURS (60 * MINUTES) 1131 #define DAYS (24 * HOURS) 1132 1133 #define PARAM_MAX (~(uint32_t)0) 1134 1135 /* Max size IP datagram is 64k - 1 */ 1136 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1137 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1138 /* Max of the above */ 1139 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1140 1141 /* Largest TCP port number */ 1142 #define TCP_MAX_PORT (64 * 1024 - 1) 1143 1144 /* 1145 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1146 * layer header. It has to be a multiple of 4. 1147 */ 1148 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1149 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1150 1151 /* 1152 * All of these are alterable, within the min/max values given, at run time. 1153 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1154 * per the TCP spec. 1155 */ 1156 /* BEGIN CSTYLED */ 1157 static tcpparam_t lcl_tcp_param_arr[] = { 1158 /*min max value name */ 1159 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1160 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1161 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1162 { 1, 1024, 1, "tcp_conn_req_min" }, 1163 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1164 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1165 { 0, 10, 0, "tcp_debug" }, 1166 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1167 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1168 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1169 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1170 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1171 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1172 { 1, 255, 64, "tcp_ipv4_ttl"}, 1173 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1174 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1175 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1176 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1177 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1178 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1179 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1180 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1181 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1182 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1183 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1184 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1185 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1186 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1187 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1188 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1189 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1190 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1191 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1192 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1193 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1194 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1195 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1196 /* 1197 * Question: What default value should I set for tcp_strong_iss? 1198 */ 1199 { 0, 2, 1, "tcp_strong_iss"}, 1200 { 0, 65536, 20, "tcp_rtt_updates"}, 1201 { 0, 1, 1, "tcp_wscale_always"}, 1202 { 0, 1, 0, "tcp_tstamp_always"}, 1203 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1204 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1205 { 0, 16, 2, "tcp_deferred_acks_max"}, 1206 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1207 { 1, 4, 4, "tcp_slow_start_initial"}, 1208 { 0, 2, 2, "tcp_sack_permitted"}, 1209 { 0, 1, 1, "tcp_compression_enabled"}, 1210 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1211 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1212 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1213 { 0, 1, 0, "tcp_rev_src_routes"}, 1214 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1215 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1216 { 0, 16, 8, "tcp_local_dacks_max"}, 1217 { 0, 2, 1, "tcp_ecn_permitted"}, 1218 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1219 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1220 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1221 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1222 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1223 }; 1224 /* END CSTYLED */ 1225 1226 /* 1227 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1228 * each header fragment in the header buffer. Each parameter value has 1229 * to be a multiple of 4 (32-bit aligned). 1230 */ 1231 static tcpparam_t lcl_tcp_mdt_head_param = 1232 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1233 static tcpparam_t lcl_tcp_mdt_tail_param = 1234 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1235 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1236 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1237 1238 /* 1239 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1240 * the maximum number of payload buffers associated per Multidata. 1241 */ 1242 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1243 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1244 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1245 1246 /* Round up the value to the nearest mss. */ 1247 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1248 1249 /* 1250 * Set ECN capable transport (ECT) code point in IP header. 1251 * 1252 * Note that there are 2 ECT code points '01' and '10', which are called 1253 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1254 * point ECT(0) for TCP as described in RFC 2481. 1255 */ 1256 #define SET_ECT(tcp, iph) \ 1257 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1258 /* We need to clear the code point first. */ \ 1259 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1260 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1261 } else { \ 1262 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1263 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1264 } 1265 1266 /* 1267 * The format argument to pass to tcp_display(). 1268 * DISP_PORT_ONLY means that the returned string has only port info. 1269 * DISP_ADDR_AND_PORT means that the returned string also contains the 1270 * remote and local IP address. 1271 */ 1272 #define DISP_PORT_ONLY 1 1273 #define DISP_ADDR_AND_PORT 2 1274 1275 #define NDD_TOO_QUICK_MSG \ 1276 "ndd get info rate too high for non-privileged users, try again " \ 1277 "later.\n" 1278 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1279 1280 #define IS_VMLOANED_MBLK(mp) \ 1281 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1282 1283 1284 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1285 boolean_t tcp_mdt_chain = B_TRUE; 1286 1287 /* 1288 * MDT threshold in the form of effective send MSS multiplier; we take 1289 * the MDT path if the amount of unsent data exceeds the threshold value 1290 * (default threshold is 1*SMSS). 1291 */ 1292 uint_t tcp_mdt_smss_threshold = 1; 1293 1294 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1295 1296 /* 1297 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1298 * tunable settable via NDD. Otherwise, the per-connection behavior is 1299 * determined dynamically during tcp_adapt_ire(), which is the default. 1300 */ 1301 boolean_t tcp_static_maxpsz = B_FALSE; 1302 1303 /* Setable in /etc/system */ 1304 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1305 uint32_t tcp_random_anon_port = 1; 1306 1307 /* 1308 * To reach to an eager in Q0 which can be dropped due to an incoming 1309 * new SYN request when Q0 is full, a new doubly linked list is 1310 * introduced. This list allows to select an eager from Q0 in O(1) time. 1311 * This is needed to avoid spending too much time walking through the 1312 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1313 * this new list has to be a member of Q0. 1314 * This list is headed by listener's tcp_t. When the list is empty, 1315 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1316 * of listener's tcp_t point to listener's tcp_t itself. 1317 * 1318 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1319 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1320 * These macros do not affect the eager's membership to Q0. 1321 */ 1322 1323 1324 #define MAKE_DROPPABLE(listener, eager) \ 1325 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1326 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1327 = (eager); \ 1328 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1329 (eager)->tcp_eager_next_drop_q0 = \ 1330 (listener)->tcp_eager_next_drop_q0; \ 1331 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1332 } 1333 1334 #define MAKE_UNDROPPABLE(eager) \ 1335 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1336 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1337 = (eager)->tcp_eager_prev_drop_q0; \ 1338 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1339 = (eager)->tcp_eager_next_drop_q0; \ 1340 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1341 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1342 } 1343 1344 /* 1345 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1346 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1347 * data, TCP will not respond with an ACK. RFC 793 requires that 1348 * TCP responds with an ACK for such a bogus ACK. By not following 1349 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1350 * an attacker successfully spoofs an acceptable segment to our 1351 * peer; or when our peer is "confused." 1352 */ 1353 uint32_t tcp_drop_ack_unsent_cnt = 10; 1354 1355 /* 1356 * Hook functions to enable cluster networking 1357 * On non-clustered systems these vectors must always be NULL. 1358 */ 1359 1360 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1361 sa_family_t addr_family, uint8_t *laddrp, 1362 in_port_t lport, void *args) = NULL; 1363 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1364 sa_family_t addr_family, uint8_t *laddrp, 1365 in_port_t lport, void *args) = NULL; 1366 1367 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1368 boolean_t is_outgoing, 1369 sa_family_t addr_family, 1370 uint8_t *laddrp, in_port_t lport, 1371 uint8_t *faddrp, in_port_t fport, 1372 void *args) = NULL; 1373 1374 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1375 sa_family_t addr_family, uint8_t *laddrp, 1376 in_port_t lport, uint8_t *faddrp, 1377 in_port_t fport, void *args) = NULL; 1378 1379 /* 1380 * The following are defined in ip.c 1381 */ 1382 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1383 sa_family_t addr_family, uint8_t *laddrp, 1384 void *args); 1385 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1386 sa_family_t addr_family, uint8_t *laddrp, 1387 uint8_t *faddrp, void *args); 1388 1389 1390 /* 1391 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1392 */ 1393 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1394 (err) = 0; \ 1395 if (cl_inet_connect2 != NULL) { \ 1396 /* \ 1397 * Running in cluster mode - register active connection \ 1398 * information \ 1399 */ \ 1400 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1401 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1402 (err) = (*cl_inet_connect2)( \ 1403 (connp)->conn_netstack->netstack_stackid,\ 1404 IPPROTO_TCP, is_outgoing, AF_INET, \ 1405 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1406 (in_port_t)(tcp)->tcp_lport, \ 1407 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1408 (in_port_t)(tcp)->tcp_fport, NULL); \ 1409 } \ 1410 } else { \ 1411 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1412 &(tcp)->tcp_ip6h->ip6_src)) { \ 1413 (err) = (*cl_inet_connect2)( \ 1414 (connp)->conn_netstack->netstack_stackid,\ 1415 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1416 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1417 (in_port_t)(tcp)->tcp_lport, \ 1418 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1419 (in_port_t)(tcp)->tcp_fport, NULL); \ 1420 } \ 1421 } \ 1422 } \ 1423 } 1424 1425 #define CL_INET_DISCONNECT(connp, tcp) { \ 1426 if (cl_inet_disconnect != NULL) { \ 1427 /* \ 1428 * Running in cluster mode - deregister active \ 1429 * connection information \ 1430 */ \ 1431 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1432 if ((tcp)->tcp_ip_src != 0) { \ 1433 (*cl_inet_disconnect)( \ 1434 (connp)->conn_netstack->netstack_stackid,\ 1435 IPPROTO_TCP, AF_INET, \ 1436 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1437 (in_port_t)(tcp)->tcp_lport, \ 1438 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1439 (in_port_t)(tcp)->tcp_fport, NULL); \ 1440 } \ 1441 } else { \ 1442 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1443 &(tcp)->tcp_ip_src_v6)) { \ 1444 (*cl_inet_disconnect)( \ 1445 (connp)->conn_netstack->netstack_stackid,\ 1446 IPPROTO_TCP, AF_INET6, \ 1447 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1448 (in_port_t)(tcp)->tcp_lport, \ 1449 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1450 (in_port_t)(tcp)->tcp_fport, NULL); \ 1451 } \ 1452 } \ 1453 } \ 1454 } 1455 1456 /* 1457 * Cluster networking hook for traversing current connection list. 1458 * This routine is used to extract the current list of live connections 1459 * which must continue to to be dispatched to this node. 1460 */ 1461 int cl_tcp_walk_list(netstackid_t stack_id, 1462 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1463 1464 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1465 void *arg, tcp_stack_t *tcps); 1466 1467 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1468 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1469 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1470 ip6_t *, ip6h, int, 0); 1471 1472 /* 1473 * Figure out the value of window scale opton. Note that the rwnd is 1474 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1475 * We cannot find the scale value and then do a round up of tcp_rwnd 1476 * because the scale value may not be correct after that. 1477 * 1478 * Set the compiler flag to make this function inline. 1479 */ 1480 static void 1481 tcp_set_ws_value(tcp_t *tcp) 1482 { 1483 int i; 1484 uint32_t rwnd = tcp->tcp_rwnd; 1485 1486 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1487 i++, rwnd >>= 1) 1488 ; 1489 tcp->tcp_rcv_ws = i; 1490 } 1491 1492 /* 1493 * Remove a connection from the list of detached TIME_WAIT connections. 1494 * It returns B_FALSE if it can't remove the connection from the list 1495 * as the connection has already been removed from the list due to an 1496 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1497 */ 1498 static boolean_t 1499 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1500 { 1501 boolean_t locked = B_FALSE; 1502 1503 if (tcp_time_wait == NULL) { 1504 tcp_time_wait = *((tcp_squeue_priv_t **) 1505 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1506 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1507 locked = B_TRUE; 1508 } else { 1509 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1510 } 1511 1512 if (tcp->tcp_time_wait_expire == 0) { 1513 ASSERT(tcp->tcp_time_wait_next == NULL); 1514 ASSERT(tcp->tcp_time_wait_prev == NULL); 1515 if (locked) 1516 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1517 return (B_FALSE); 1518 } 1519 ASSERT(TCP_IS_DETACHED(tcp)); 1520 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1521 1522 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1523 ASSERT(tcp->tcp_time_wait_prev == NULL); 1524 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1525 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1526 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1527 NULL; 1528 } else { 1529 tcp_time_wait->tcp_time_wait_tail = NULL; 1530 } 1531 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1532 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1533 ASSERT(tcp->tcp_time_wait_next == NULL); 1534 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1535 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1536 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1537 } else { 1538 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1539 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1540 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1541 tcp->tcp_time_wait_next; 1542 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1543 tcp->tcp_time_wait_prev; 1544 } 1545 tcp->tcp_time_wait_next = NULL; 1546 tcp->tcp_time_wait_prev = NULL; 1547 tcp->tcp_time_wait_expire = 0; 1548 1549 if (locked) 1550 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1551 return (B_TRUE); 1552 } 1553 1554 /* 1555 * Add a connection to the list of detached TIME_WAIT connections 1556 * and set its time to expire. 1557 */ 1558 static void 1559 tcp_time_wait_append(tcp_t *tcp) 1560 { 1561 tcp_stack_t *tcps = tcp->tcp_tcps; 1562 tcp_squeue_priv_t *tcp_time_wait = 1563 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1564 SQPRIVATE_TCP)); 1565 1566 tcp_timers_stop(tcp); 1567 1568 /* Freed above */ 1569 ASSERT(tcp->tcp_timer_tid == 0); 1570 ASSERT(tcp->tcp_ack_tid == 0); 1571 1572 /* must have happened at the time of detaching the tcp */ 1573 ASSERT(tcp->tcp_ptpahn == NULL); 1574 ASSERT(tcp->tcp_flow_stopped == 0); 1575 ASSERT(tcp->tcp_time_wait_next == NULL); 1576 ASSERT(tcp->tcp_time_wait_prev == NULL); 1577 ASSERT(tcp->tcp_time_wait_expire == NULL); 1578 ASSERT(tcp->tcp_listener == NULL); 1579 1580 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1581 /* 1582 * The value computed below in tcp->tcp_time_wait_expire may 1583 * appear negative or wrap around. That is ok since our 1584 * interest is only in the difference between the current lbolt 1585 * value and tcp->tcp_time_wait_expire. But the value should not 1586 * be zero, since it means the tcp is not in the TIME_WAIT list. 1587 * The corresponding comparison in tcp_time_wait_collector() uses 1588 * modular arithmetic. 1589 */ 1590 tcp->tcp_time_wait_expire += 1591 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1592 if (tcp->tcp_time_wait_expire == 0) 1593 tcp->tcp_time_wait_expire = 1; 1594 1595 ASSERT(TCP_IS_DETACHED(tcp)); 1596 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1597 ASSERT(tcp->tcp_time_wait_next == NULL); 1598 ASSERT(tcp->tcp_time_wait_prev == NULL); 1599 TCP_DBGSTAT(tcps, tcp_time_wait); 1600 1601 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1602 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1603 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1604 tcp_time_wait->tcp_time_wait_head = tcp; 1605 } else { 1606 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1607 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1608 TCPS_TIME_WAIT); 1609 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1610 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1611 } 1612 tcp_time_wait->tcp_time_wait_tail = tcp; 1613 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1614 } 1615 1616 /* ARGSUSED */ 1617 void 1618 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1619 { 1620 conn_t *connp = (conn_t *)arg; 1621 tcp_t *tcp = connp->conn_tcp; 1622 tcp_stack_t *tcps = tcp->tcp_tcps; 1623 1624 ASSERT(tcp != NULL); 1625 if (tcp->tcp_state == TCPS_CLOSED) { 1626 return; 1627 } 1628 1629 ASSERT((tcp->tcp_family == AF_INET && 1630 tcp->tcp_ipversion == IPV4_VERSION) || 1631 (tcp->tcp_family == AF_INET6 && 1632 (tcp->tcp_ipversion == IPV4_VERSION || 1633 tcp->tcp_ipversion == IPV6_VERSION))); 1634 ASSERT(!tcp->tcp_listener); 1635 1636 TCP_STAT(tcps, tcp_time_wait_reap); 1637 ASSERT(TCP_IS_DETACHED(tcp)); 1638 1639 /* 1640 * Because they have no upstream client to rebind or tcp_close() 1641 * them later, we axe the connection here and now. 1642 */ 1643 tcp_close_detached(tcp); 1644 } 1645 1646 /* 1647 * Remove cached/latched IPsec references. 1648 */ 1649 void 1650 tcp_ipsec_cleanup(tcp_t *tcp) 1651 { 1652 conn_t *connp = tcp->tcp_connp; 1653 1654 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1655 1656 if (connp->conn_latch != NULL) { 1657 IPLATCH_REFRELE(connp->conn_latch, 1658 connp->conn_netstack); 1659 connp->conn_latch = NULL; 1660 } 1661 if (connp->conn_policy != NULL) { 1662 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1663 connp->conn_policy = NULL; 1664 } 1665 } 1666 1667 /* 1668 * Cleaup before placing on free list. 1669 * Disassociate from the netstack/tcp_stack_t since the freelist 1670 * is per squeue and not per netstack. 1671 */ 1672 void 1673 tcp_cleanup(tcp_t *tcp) 1674 { 1675 mblk_t *mp; 1676 char *tcp_iphc; 1677 int tcp_iphc_len; 1678 int tcp_hdr_grown; 1679 tcp_sack_info_t *tcp_sack_info; 1680 conn_t *connp = tcp->tcp_connp; 1681 tcp_stack_t *tcps = tcp->tcp_tcps; 1682 netstack_t *ns = tcps->tcps_netstack; 1683 mblk_t *tcp_rsrv_mp; 1684 1685 tcp_bind_hash_remove(tcp); 1686 1687 /* Cleanup that which needs the netstack first */ 1688 tcp_ipsec_cleanup(tcp); 1689 1690 tcp_free(tcp); 1691 1692 /* Release any SSL context */ 1693 if (tcp->tcp_kssl_ent != NULL) { 1694 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1695 tcp->tcp_kssl_ent = NULL; 1696 } 1697 1698 if (tcp->tcp_kssl_ctx != NULL) { 1699 kssl_release_ctx(tcp->tcp_kssl_ctx); 1700 tcp->tcp_kssl_ctx = NULL; 1701 } 1702 tcp->tcp_kssl_pending = B_FALSE; 1703 1704 conn_delete_ire(connp, NULL); 1705 1706 /* 1707 * Since we will bzero the entire structure, we need to 1708 * remove it and reinsert it in global hash list. We 1709 * know the walkers can't get to this conn because we 1710 * had set CONDEMNED flag earlier and checked reference 1711 * under conn_lock so walker won't pick it and when we 1712 * go the ipcl_globalhash_remove() below, no walker 1713 * can get to it. 1714 */ 1715 ipcl_globalhash_remove(connp); 1716 1717 /* 1718 * Now it is safe to decrement the reference counts. 1719 * This might be the last reference on the netstack and TCPS 1720 * in which case it will cause the tcp_g_q_close and 1721 * the freeing of the IP Instance. 1722 */ 1723 connp->conn_netstack = NULL; 1724 netstack_rele(ns); 1725 ASSERT(tcps != NULL); 1726 tcp->tcp_tcps = NULL; 1727 TCPS_REFRELE(tcps); 1728 1729 /* Save some state */ 1730 mp = tcp->tcp_timercache; 1731 1732 tcp_sack_info = tcp->tcp_sack_info; 1733 tcp_iphc = tcp->tcp_iphc; 1734 tcp_iphc_len = tcp->tcp_iphc_len; 1735 tcp_hdr_grown = tcp->tcp_hdr_grown; 1736 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1737 1738 if (connp->conn_cred != NULL) { 1739 crfree(connp->conn_cred); 1740 connp->conn_cred = NULL; 1741 } 1742 if (connp->conn_peercred != NULL) { 1743 crfree(connp->conn_peercred); 1744 connp->conn_peercred = NULL; 1745 } 1746 ipcl_conn_cleanup(connp); 1747 connp->conn_flags = IPCL_TCPCONN; 1748 bzero(tcp, sizeof (tcp_t)); 1749 1750 /* restore the state */ 1751 tcp->tcp_timercache = mp; 1752 1753 tcp->tcp_sack_info = tcp_sack_info; 1754 tcp->tcp_iphc = tcp_iphc; 1755 tcp->tcp_iphc_len = tcp_iphc_len; 1756 tcp->tcp_hdr_grown = tcp_hdr_grown; 1757 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1758 1759 tcp->tcp_connp = connp; 1760 1761 ASSERT(connp->conn_tcp == tcp); 1762 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1763 connp->conn_state_flags = CONN_INCIPIENT; 1764 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1765 ASSERT(connp->conn_ref == 1); 1766 } 1767 1768 /* 1769 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1770 * is done forwards from the head. 1771 * This walks all stack instances since 1772 * tcp_time_wait remains global across all stacks. 1773 */ 1774 /* ARGSUSED */ 1775 void 1776 tcp_time_wait_collector(void *arg) 1777 { 1778 tcp_t *tcp; 1779 clock_t now; 1780 mblk_t *mp; 1781 conn_t *connp; 1782 kmutex_t *lock; 1783 boolean_t removed; 1784 1785 squeue_t *sqp = (squeue_t *)arg; 1786 tcp_squeue_priv_t *tcp_time_wait = 1787 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1788 1789 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1790 tcp_time_wait->tcp_time_wait_tid = 0; 1791 1792 if (tcp_time_wait->tcp_free_list != NULL && 1793 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1794 TCP_G_STAT(tcp_freelist_cleanup); 1795 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1796 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1797 tcp->tcp_time_wait_next = NULL; 1798 tcp_time_wait->tcp_free_list_cnt--; 1799 ASSERT(tcp->tcp_tcps == NULL); 1800 CONN_DEC_REF(tcp->tcp_connp); 1801 } 1802 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1803 } 1804 1805 /* 1806 * In order to reap time waits reliably, we should use a 1807 * source of time that is not adjustable by the user -- hence 1808 * the call to ddi_get_lbolt(). 1809 */ 1810 now = ddi_get_lbolt(); 1811 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1812 /* 1813 * Compare times using modular arithmetic, since 1814 * lbolt can wrapover. 1815 */ 1816 if ((now - tcp->tcp_time_wait_expire) < 0) { 1817 break; 1818 } 1819 1820 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1821 ASSERT(removed); 1822 1823 connp = tcp->tcp_connp; 1824 ASSERT(connp->conn_fanout != NULL); 1825 lock = &connp->conn_fanout->connf_lock; 1826 /* 1827 * This is essentially a TW reclaim fast path optimization for 1828 * performance where the timewait collector checks under the 1829 * fanout lock (so that no one else can get access to the 1830 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1831 * the classifier hash list. If ref count is indeed 2, we can 1832 * just remove the conn under the fanout lock and avoid 1833 * cleaning up the conn under the squeue, provided that 1834 * clustering callbacks are not enabled. If clustering is 1835 * enabled, we need to make the clustering callback before 1836 * setting the CONDEMNED flag and after dropping all locks and 1837 * so we forego this optimization and fall back to the slow 1838 * path. Also please see the comments in tcp_closei_local 1839 * regarding the refcnt logic. 1840 * 1841 * Since we are holding the tcp_time_wait_lock, its better 1842 * not to block on the fanout_lock because other connections 1843 * can't add themselves to time_wait list. So we do a 1844 * tryenter instead of mutex_enter. 1845 */ 1846 if (mutex_tryenter(lock)) { 1847 mutex_enter(&connp->conn_lock); 1848 if ((connp->conn_ref == 2) && 1849 (cl_inet_disconnect == NULL)) { 1850 ipcl_hash_remove_locked(connp, 1851 connp->conn_fanout); 1852 /* 1853 * Set the CONDEMNED flag now itself so that 1854 * the refcnt cannot increase due to any 1855 * walker. But we have still not cleaned up 1856 * conn_ire_cache. This is still ok since 1857 * we are going to clean it up in tcp_cleanup 1858 * immediately and any interface unplumb 1859 * thread will wait till the ire is blown away 1860 */ 1861 connp->conn_state_flags |= CONN_CONDEMNED; 1862 mutex_exit(lock); 1863 mutex_exit(&connp->conn_lock); 1864 if (tcp_time_wait->tcp_free_list_cnt < 1865 tcp_free_list_max_cnt) { 1866 /* Add to head of tcp_free_list */ 1867 mutex_exit( 1868 &tcp_time_wait->tcp_time_wait_lock); 1869 tcp_cleanup(tcp); 1870 ASSERT(connp->conn_latch == NULL); 1871 ASSERT(connp->conn_policy == NULL); 1872 ASSERT(tcp->tcp_tcps == NULL); 1873 ASSERT(connp->conn_netstack == NULL); 1874 1875 mutex_enter( 1876 &tcp_time_wait->tcp_time_wait_lock); 1877 tcp->tcp_time_wait_next = 1878 tcp_time_wait->tcp_free_list; 1879 tcp_time_wait->tcp_free_list = tcp; 1880 tcp_time_wait->tcp_free_list_cnt++; 1881 continue; 1882 } else { 1883 /* Do not add to tcp_free_list */ 1884 mutex_exit( 1885 &tcp_time_wait->tcp_time_wait_lock); 1886 tcp_bind_hash_remove(tcp); 1887 conn_delete_ire(tcp->tcp_connp, NULL); 1888 tcp_ipsec_cleanup(tcp); 1889 CONN_DEC_REF(tcp->tcp_connp); 1890 } 1891 } else { 1892 CONN_INC_REF_LOCKED(connp); 1893 mutex_exit(lock); 1894 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1895 mutex_exit(&connp->conn_lock); 1896 /* 1897 * We can reuse the closemp here since conn has 1898 * detached (otherwise we wouldn't even be in 1899 * time_wait list). tcp_closemp_used can safely 1900 * be changed without taking a lock as no other 1901 * thread can concurrently access it at this 1902 * point in the connection lifecycle. 1903 */ 1904 1905 if (tcp->tcp_closemp.b_prev == NULL) 1906 tcp->tcp_closemp_used = B_TRUE; 1907 else 1908 cmn_err(CE_PANIC, 1909 "tcp_timewait_collector: " 1910 "concurrent use of tcp_closemp: " 1911 "connp %p tcp %p\n", (void *)connp, 1912 (void *)tcp); 1913 1914 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1915 mp = &tcp->tcp_closemp; 1916 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1917 tcp_timewait_output, connp, 1918 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1919 } 1920 } else { 1921 mutex_enter(&connp->conn_lock); 1922 CONN_INC_REF_LOCKED(connp); 1923 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1924 mutex_exit(&connp->conn_lock); 1925 /* 1926 * We can reuse the closemp here since conn has 1927 * detached (otherwise we wouldn't even be in 1928 * time_wait list). tcp_closemp_used can safely 1929 * be changed without taking a lock as no other 1930 * thread can concurrently access it at this 1931 * point in the connection lifecycle. 1932 */ 1933 1934 if (tcp->tcp_closemp.b_prev == NULL) 1935 tcp->tcp_closemp_used = B_TRUE; 1936 else 1937 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1938 "concurrent use of tcp_closemp: " 1939 "connp %p tcp %p\n", (void *)connp, 1940 (void *)tcp); 1941 1942 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1943 mp = &tcp->tcp_closemp; 1944 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1945 tcp_timewait_output, connp, 1946 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1947 } 1948 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1949 } 1950 1951 if (tcp_time_wait->tcp_free_list != NULL) 1952 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1953 1954 tcp_time_wait->tcp_time_wait_tid = 1955 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1956 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1957 CALLOUT_FLAG_ROUNDUP); 1958 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1959 } 1960 1961 /* 1962 * Reply to a clients T_CONN_RES TPI message. This function 1963 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1964 * on the acceptor STREAM and processed in tcp_wput_accept(). 1965 * Read the block comment on top of tcp_conn_request(). 1966 */ 1967 static void 1968 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1969 { 1970 tcp_t *acceptor; 1971 tcp_t *eager; 1972 tcp_t *tcp; 1973 struct T_conn_res *tcr; 1974 t_uscalar_t acceptor_id; 1975 t_scalar_t seqnum; 1976 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1977 struct tcp_options *tcpopt; 1978 mblk_t *ok_mp; 1979 mblk_t *mp1; 1980 tcp_stack_t *tcps = listener->tcp_tcps; 1981 1982 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1983 tcp_err_ack(listener, mp, TPROTO, 0); 1984 return; 1985 } 1986 tcr = (struct T_conn_res *)mp->b_rptr; 1987 1988 /* 1989 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1990 * read side queue of the streams device underneath us i.e. the 1991 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1992 * look it up in the queue_hash. Under LP64 it sends down the 1993 * minor_t of the accepting endpoint. 1994 * 1995 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1996 * fanout hash lock is held. 1997 * This prevents any thread from entering the acceptor queue from 1998 * below (since it has not been hard bound yet i.e. any inbound 1999 * packets will arrive on the listener or default tcp queue and 2000 * go through tcp_lookup). 2001 * The CONN_INC_REF will prevent the acceptor from closing. 2002 * 2003 * XXX It is still possible for a tli application to send down data 2004 * on the accepting stream while another thread calls t_accept. 2005 * This should not be a problem for well-behaved applications since 2006 * the T_OK_ACK is sent after the queue swapping is completed. 2007 * 2008 * If the accepting fd is the same as the listening fd, avoid 2009 * queue hash lookup since that will return an eager listener in a 2010 * already established state. 2011 */ 2012 acceptor_id = tcr->ACCEPTOR_id; 2013 mutex_enter(&listener->tcp_eager_lock); 2014 if (listener->tcp_acceptor_id == acceptor_id) { 2015 eager = listener->tcp_eager_next_q; 2016 /* only count how many T_CONN_INDs so don't count q0 */ 2017 if ((listener->tcp_conn_req_cnt_q != 1) || 2018 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 2019 mutex_exit(&listener->tcp_eager_lock); 2020 tcp_err_ack(listener, mp, TBADF, 0); 2021 return; 2022 } 2023 if (listener->tcp_conn_req_cnt_q0 != 0) { 2024 /* Throw away all the eagers on q0. */ 2025 tcp_eager_cleanup(listener, 1); 2026 } 2027 if (listener->tcp_syn_defense) { 2028 listener->tcp_syn_defense = B_FALSE; 2029 if (listener->tcp_ip_addr_cache != NULL) { 2030 kmem_free(listener->tcp_ip_addr_cache, 2031 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 2032 listener->tcp_ip_addr_cache = NULL; 2033 } 2034 } 2035 /* 2036 * Transfer tcp_conn_req_max to the eager so that when 2037 * a disconnect occurs we can revert the endpoint to the 2038 * listen state. 2039 */ 2040 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 2041 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 2042 /* 2043 * Get a reference on the acceptor just like the 2044 * tcp_acceptor_hash_lookup below. 2045 */ 2046 acceptor = listener; 2047 CONN_INC_REF(acceptor->tcp_connp); 2048 } else { 2049 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 2050 if (acceptor == NULL) { 2051 if (listener->tcp_debug) { 2052 (void) strlog(TCP_MOD_ID, 0, 1, 2053 SL_ERROR|SL_TRACE, 2054 "tcp_accept: did not find acceptor 0x%x\n", 2055 acceptor_id); 2056 } 2057 mutex_exit(&listener->tcp_eager_lock); 2058 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 2059 return; 2060 } 2061 /* 2062 * Verify acceptor state. The acceptable states for an acceptor 2063 * include TCPS_IDLE and TCPS_BOUND. 2064 */ 2065 switch (acceptor->tcp_state) { 2066 case TCPS_IDLE: 2067 /* FALLTHRU */ 2068 case TCPS_BOUND: 2069 break; 2070 default: 2071 CONN_DEC_REF(acceptor->tcp_connp); 2072 mutex_exit(&listener->tcp_eager_lock); 2073 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2074 return; 2075 } 2076 } 2077 2078 /* The listener must be in TCPS_LISTEN */ 2079 if (listener->tcp_state != TCPS_LISTEN) { 2080 CONN_DEC_REF(acceptor->tcp_connp); 2081 mutex_exit(&listener->tcp_eager_lock); 2082 tcp_err_ack(listener, mp, TOUTSTATE, 0); 2083 return; 2084 } 2085 2086 /* 2087 * Rendezvous with an eager connection request packet hanging off 2088 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2089 * tcp structure when the connection packet arrived in 2090 * tcp_conn_request(). 2091 */ 2092 seqnum = tcr->SEQ_number; 2093 eager = listener; 2094 do { 2095 eager = eager->tcp_eager_next_q; 2096 if (eager == NULL) { 2097 CONN_DEC_REF(acceptor->tcp_connp); 2098 mutex_exit(&listener->tcp_eager_lock); 2099 tcp_err_ack(listener, mp, TBADSEQ, 0); 2100 return; 2101 } 2102 } while (eager->tcp_conn_req_seqnum != seqnum); 2103 mutex_exit(&listener->tcp_eager_lock); 2104 2105 /* 2106 * At this point, both acceptor and listener have 2 ref 2107 * that they begin with. Acceptor has one additional ref 2108 * we placed in lookup while listener has 3 additional 2109 * ref for being behind the squeue (tcp_accept() is 2110 * done on listener's squeue); being in classifier hash; 2111 * and eager's ref on listener. 2112 */ 2113 ASSERT(listener->tcp_connp->conn_ref >= 5); 2114 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2115 2116 /* 2117 * The eager at this point is set in its own squeue and 2118 * could easily have been killed (tcp_accept_finish will 2119 * deal with that) because of a TH_RST so we can only 2120 * ASSERT for a single ref. 2121 */ 2122 ASSERT(eager->tcp_connp->conn_ref >= 1); 2123 2124 /* Pre allocate the stroptions mblk also */ 2125 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2126 sizeof (struct T_conn_res)), BPRI_HI); 2127 if (opt_mp == NULL) { 2128 CONN_DEC_REF(acceptor->tcp_connp); 2129 CONN_DEC_REF(eager->tcp_connp); 2130 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2131 return; 2132 } 2133 DB_TYPE(opt_mp) = M_SETOPTS; 2134 opt_mp->b_wptr += sizeof (struct tcp_options); 2135 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2136 tcpopt->to_flags = 0; 2137 2138 /* 2139 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2140 * from listener to acceptor. 2141 */ 2142 if (listener->tcp_bound_if != 0) { 2143 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2144 tcpopt->to_boundif = listener->tcp_bound_if; 2145 } 2146 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2147 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2148 } 2149 2150 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2151 if ((mp1 = copymsg(mp)) == NULL) { 2152 CONN_DEC_REF(acceptor->tcp_connp); 2153 CONN_DEC_REF(eager->tcp_connp); 2154 freemsg(opt_mp); 2155 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2156 return; 2157 } 2158 2159 tcr = (struct T_conn_res *)mp1->b_rptr; 2160 2161 /* 2162 * This is an expanded version of mi_tpi_ok_ack_alloc() 2163 * which allocates a larger mblk and appends the new 2164 * local address to the ok_ack. The address is copied by 2165 * soaccept() for getsockname(). 2166 */ 2167 { 2168 int extra; 2169 2170 extra = (eager->tcp_family == AF_INET) ? 2171 sizeof (sin_t) : sizeof (sin6_t); 2172 2173 /* 2174 * Try to re-use mp, if possible. Otherwise, allocate 2175 * an mblk and return it as ok_mp. In any case, mp 2176 * is no longer usable upon return. 2177 */ 2178 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2179 CONN_DEC_REF(acceptor->tcp_connp); 2180 CONN_DEC_REF(eager->tcp_connp); 2181 freemsg(opt_mp); 2182 /* Original mp has been freed by now, so use mp1 */ 2183 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2184 return; 2185 } 2186 2187 mp = NULL; /* We should never use mp after this point */ 2188 2189 switch (extra) { 2190 case sizeof (sin_t): { 2191 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2192 2193 ok_mp->b_wptr += extra; 2194 sin->sin_family = AF_INET; 2195 sin->sin_port = eager->tcp_lport; 2196 sin->sin_addr.s_addr = 2197 eager->tcp_ipha->ipha_src; 2198 break; 2199 } 2200 case sizeof (sin6_t): { 2201 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2202 2203 ok_mp->b_wptr += extra; 2204 sin6->sin6_family = AF_INET6; 2205 sin6->sin6_port = eager->tcp_lport; 2206 if (eager->tcp_ipversion == IPV4_VERSION) { 2207 sin6->sin6_flowinfo = 0; 2208 IN6_IPADDR_TO_V4MAPPED( 2209 eager->tcp_ipha->ipha_src, 2210 &sin6->sin6_addr); 2211 } else { 2212 ASSERT(eager->tcp_ip6h != NULL); 2213 sin6->sin6_flowinfo = 2214 eager->tcp_ip6h->ip6_vcf & 2215 ~IPV6_VERS_AND_FLOW_MASK; 2216 sin6->sin6_addr = 2217 eager->tcp_ip6h->ip6_src; 2218 } 2219 sin6->sin6_scope_id = 0; 2220 sin6->__sin6_src_id = 0; 2221 break; 2222 } 2223 default: 2224 break; 2225 } 2226 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2227 } 2228 2229 /* 2230 * If there are no options we know that the T_CONN_RES will 2231 * succeed. However, we can't send the T_OK_ACK upstream until 2232 * the tcp_accept_swap is done since it would be dangerous to 2233 * let the application start using the new fd prior to the swap. 2234 */ 2235 tcp_accept_swap(listener, acceptor, eager); 2236 2237 /* 2238 * tcp_accept_swap unlinks eager from listener but does not drop 2239 * the eager's reference on the listener. 2240 */ 2241 ASSERT(eager->tcp_listener == NULL); 2242 ASSERT(listener->tcp_connp->conn_ref >= 5); 2243 2244 /* 2245 * The eager is now associated with its own queue. Insert in 2246 * the hash so that the connection can be reused for a future 2247 * T_CONN_RES. 2248 */ 2249 tcp_acceptor_hash_insert(acceptor_id, eager); 2250 2251 /* 2252 * We now do the processing of options with T_CONN_RES. 2253 * We delay till now since we wanted to have queue to pass to 2254 * option processing routines that points back to the right 2255 * instance structure which does not happen until after 2256 * tcp_accept_swap(). 2257 * 2258 * Note: 2259 * The sanity of the logic here assumes that whatever options 2260 * are appropriate to inherit from listner=>eager are done 2261 * before this point, and whatever were to be overridden (or not) 2262 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2263 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2264 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2265 * This may not be true at this point in time but can be fixed 2266 * independently. This option processing code starts with 2267 * the instantiated acceptor instance and the final queue at 2268 * this point. 2269 */ 2270 2271 if (tcr->OPT_length != 0) { 2272 /* Options to process */ 2273 int t_error = 0; 2274 int sys_error = 0; 2275 int do_disconnect = 0; 2276 2277 if (tcp_conprim_opt_process(eager, mp1, 2278 &do_disconnect, &t_error, &sys_error) < 0) { 2279 eager->tcp_accept_error = 1; 2280 if (do_disconnect) { 2281 /* 2282 * An option failed which does not allow 2283 * connection to be accepted. 2284 * 2285 * We allow T_CONN_RES to succeed and 2286 * put a T_DISCON_IND on the eager queue. 2287 */ 2288 ASSERT(t_error == 0 && sys_error == 0); 2289 eager->tcp_send_discon_ind = 1; 2290 } else { 2291 ASSERT(t_error != 0); 2292 freemsg(ok_mp); 2293 /* 2294 * Original mp was either freed or set 2295 * to ok_mp above, so use mp1 instead. 2296 */ 2297 tcp_err_ack(listener, mp1, t_error, sys_error); 2298 goto finish; 2299 } 2300 } 2301 /* 2302 * Most likely success in setting options (except if 2303 * eager->tcp_send_discon_ind set). 2304 * mp1 option buffer represented by OPT_length/offset 2305 * potentially modified and contains results of setting 2306 * options at this point 2307 */ 2308 } 2309 2310 /* We no longer need mp1, since all options processing has passed */ 2311 freemsg(mp1); 2312 2313 putnext(listener->tcp_rq, ok_mp); 2314 2315 mutex_enter(&listener->tcp_eager_lock); 2316 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2317 tcp_t *tail; 2318 mblk_t *conn_ind; 2319 2320 /* 2321 * This path should not be executed if listener and 2322 * acceptor streams are the same. 2323 */ 2324 ASSERT(listener != acceptor); 2325 2326 tcp = listener->tcp_eager_prev_q0; 2327 /* 2328 * listener->tcp_eager_prev_q0 points to the TAIL of the 2329 * deferred T_conn_ind queue. We need to get to the head of 2330 * the queue in order to send up T_conn_ind the same order as 2331 * how the 3WHS is completed. 2332 */ 2333 while (tcp != listener) { 2334 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2335 break; 2336 else 2337 tcp = tcp->tcp_eager_prev_q0; 2338 } 2339 ASSERT(tcp != listener); 2340 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2341 ASSERT(conn_ind != NULL); 2342 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2343 2344 /* Move from q0 to q */ 2345 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2346 listener->tcp_conn_req_cnt_q0--; 2347 listener->tcp_conn_req_cnt_q++; 2348 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2349 tcp->tcp_eager_prev_q0; 2350 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2351 tcp->tcp_eager_next_q0; 2352 tcp->tcp_eager_prev_q0 = NULL; 2353 tcp->tcp_eager_next_q0 = NULL; 2354 tcp->tcp_conn_def_q0 = B_FALSE; 2355 2356 /* Make sure the tcp isn't in the list of droppables */ 2357 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2358 tcp->tcp_eager_prev_drop_q0 == NULL); 2359 2360 /* 2361 * Insert at end of the queue because sockfs sends 2362 * down T_CONN_RES in chronological order. Leaving 2363 * the older conn indications at front of the queue 2364 * helps reducing search time. 2365 */ 2366 tail = listener->tcp_eager_last_q; 2367 if (tail != NULL) 2368 tail->tcp_eager_next_q = tcp; 2369 else 2370 listener->tcp_eager_next_q = tcp; 2371 listener->tcp_eager_last_q = tcp; 2372 tcp->tcp_eager_next_q = NULL; 2373 mutex_exit(&listener->tcp_eager_lock); 2374 putnext(tcp->tcp_rq, conn_ind); 2375 } else { 2376 mutex_exit(&listener->tcp_eager_lock); 2377 } 2378 2379 /* 2380 * Done with the acceptor - free it 2381 * 2382 * Note: from this point on, no access to listener should be made 2383 * as listener can be equal to acceptor. 2384 */ 2385 finish: 2386 ASSERT(acceptor->tcp_detached); 2387 ASSERT(tcps->tcps_g_q != NULL); 2388 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2389 acceptor->tcp_rq = tcps->tcps_g_q; 2390 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2391 (void) tcp_clean_death(acceptor, 0, 2); 2392 CONN_DEC_REF(acceptor->tcp_connp); 2393 2394 /* 2395 * In case we already received a FIN we have to make tcp_rput send 2396 * the ordrel_ind. This will also send up a window update if the window 2397 * has opened up. 2398 * 2399 * In the normal case of a successful connection acceptance 2400 * we give the O_T_BIND_REQ to the read side put procedure as an 2401 * indication that this was just accepted. This tells tcp_rput to 2402 * pass up any data queued in tcp_rcv_list. 2403 * 2404 * In the fringe case where options sent with T_CONN_RES failed and 2405 * we required, we would be indicating a T_DISCON_IND to blow 2406 * away this connection. 2407 */ 2408 2409 /* 2410 * XXX: we currently have a problem if XTI application closes the 2411 * acceptor stream in between. This problem exists in on10-gate also 2412 * and is well know but nothing can be done short of major rewrite 2413 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2414 * eager same squeue as listener (we can distinguish non socket 2415 * listeners at the time of handling a SYN in tcp_conn_request) 2416 * and do most of the work that tcp_accept_finish does here itself 2417 * and then get behind the acceptor squeue to access the acceptor 2418 * queue. 2419 */ 2420 /* 2421 * We already have a ref on tcp so no need to do one before squeue_enter 2422 */ 2423 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2424 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2425 } 2426 2427 /* 2428 * Swap information between the eager and acceptor for a TLI/XTI client. 2429 * The sockfs accept is done on the acceptor stream and control goes 2430 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2431 * called. In either case, both the eager and listener are in their own 2432 * perimeter (squeue) and the code has to deal with potential race. 2433 * 2434 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2435 */ 2436 static void 2437 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2438 { 2439 conn_t *econnp, *aconnp; 2440 2441 ASSERT(eager->tcp_rq == listener->tcp_rq); 2442 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2443 ASSERT(!eager->tcp_hard_bound); 2444 ASSERT(!TCP_IS_SOCKET(acceptor)); 2445 ASSERT(!TCP_IS_SOCKET(eager)); 2446 ASSERT(!TCP_IS_SOCKET(listener)); 2447 2448 acceptor->tcp_detached = B_TRUE; 2449 /* 2450 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2451 * the acceptor id. 2452 */ 2453 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2454 2455 /* remove eager from listen list... */ 2456 mutex_enter(&listener->tcp_eager_lock); 2457 tcp_eager_unlink(eager); 2458 ASSERT(eager->tcp_eager_next_q == NULL && 2459 eager->tcp_eager_last_q == NULL); 2460 ASSERT(eager->tcp_eager_next_q0 == NULL && 2461 eager->tcp_eager_prev_q0 == NULL); 2462 mutex_exit(&listener->tcp_eager_lock); 2463 eager->tcp_rq = acceptor->tcp_rq; 2464 eager->tcp_wq = acceptor->tcp_wq; 2465 2466 econnp = eager->tcp_connp; 2467 aconnp = acceptor->tcp_connp; 2468 2469 eager->tcp_rq->q_ptr = econnp; 2470 eager->tcp_wq->q_ptr = econnp; 2471 2472 /* 2473 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2474 * which might be a different squeue from our peer TCP instance. 2475 * For TCP Fusion, the peer expects that whenever tcp_detached is 2476 * clear, our TCP queues point to the acceptor's queues. Thus, use 2477 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2478 * above reach global visibility prior to the clearing of tcp_detached. 2479 */ 2480 membar_producer(); 2481 eager->tcp_detached = B_FALSE; 2482 2483 ASSERT(eager->tcp_ack_tid == 0); 2484 2485 econnp->conn_dev = aconnp->conn_dev; 2486 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2487 ASSERT(econnp->conn_minor_arena != NULL); 2488 if (eager->tcp_cred != NULL) 2489 crfree(eager->tcp_cred); 2490 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2491 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2492 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2493 2494 aconnp->conn_cred = NULL; 2495 2496 econnp->conn_zoneid = aconnp->conn_zoneid; 2497 econnp->conn_allzones = aconnp->conn_allzones; 2498 2499 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2500 aconnp->conn_mac_exempt = B_FALSE; 2501 2502 ASSERT(aconnp->conn_peercred == NULL); 2503 2504 /* Do the IPC initialization */ 2505 CONN_INC_REF(econnp); 2506 2507 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2508 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2509 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2510 2511 /* Done with old IPC. Drop its ref on its connp */ 2512 CONN_DEC_REF(aconnp); 2513 } 2514 2515 2516 /* 2517 * Adapt to the information, such as rtt and rtt_sd, provided from the 2518 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2519 * 2520 * Checks for multicast and broadcast destination address. 2521 * Returns zero on failure; non-zero if ok. 2522 * 2523 * Note that the MSS calculation here is based on the info given in 2524 * the IRE. We do not do any calculation based on TCP options. They 2525 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2526 * knows which options to use. 2527 * 2528 * Note on how TCP gets its parameters for a connection. 2529 * 2530 * When a tcp_t structure is allocated, it gets all the default parameters. 2531 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2532 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2533 * default. 2534 * 2535 * An incoming SYN with a multicast or broadcast destination address, is dropped 2536 * in 1 of 2 places. 2537 * 2538 * 1. If the packet was received over the wire it is dropped in 2539 * ip_rput_process_broadcast() 2540 * 2541 * 2. If the packet was received through internal IP loopback, i.e. the packet 2542 * was generated and received on the same machine, it is dropped in 2543 * ip_wput_local() 2544 * 2545 * An incoming SYN with a multicast or broadcast source address is always 2546 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2547 * reject an attempt to connect to a broadcast or multicast (destination) 2548 * address. 2549 */ 2550 static int 2551 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2552 { 2553 tcp_hsp_t *hsp; 2554 ire_t *ire; 2555 ire_t *sire = NULL; 2556 iulp_t *ire_uinfo = NULL; 2557 uint32_t mss_max; 2558 uint32_t mss; 2559 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2560 conn_t *connp = tcp->tcp_connp; 2561 boolean_t ire_cacheable = B_FALSE; 2562 zoneid_t zoneid = connp->conn_zoneid; 2563 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2564 MATCH_IRE_SECATTR; 2565 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2566 ill_t *ill = NULL; 2567 boolean_t incoming = (ire_mp == NULL); 2568 tcp_stack_t *tcps = tcp->tcp_tcps; 2569 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2570 2571 ASSERT(connp->conn_ire_cache == NULL); 2572 2573 if (tcp->tcp_ipversion == IPV4_VERSION) { 2574 2575 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2576 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2577 return (0); 2578 } 2579 /* 2580 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2581 * for the destination with the nexthop as gateway. 2582 * ire_ctable_lookup() is used because this particular 2583 * ire, if it exists, will be marked private. 2584 * If that is not available, use the interface ire 2585 * for the nexthop. 2586 * 2587 * TSol: tcp_update_label will detect label mismatches based 2588 * only on the destination's label, but that would not 2589 * detect label mismatches based on the security attributes 2590 * of routes or next hop gateway. Hence we need to pass the 2591 * label to ire_ftable_lookup below in order to locate the 2592 * right prefix (and/or) ire cache. Similarly we also need 2593 * pass the label to the ire_cache_lookup below to locate 2594 * the right ire that also matches on the label. 2595 */ 2596 if (tcp->tcp_connp->conn_nexthop_set) { 2597 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2598 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2599 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2600 ipst); 2601 if (ire == NULL) { 2602 ire = ire_ftable_lookup( 2603 tcp->tcp_connp->conn_nexthop_v4, 2604 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2605 tsl, match_flags, ipst); 2606 if (ire == NULL) 2607 return (0); 2608 } else { 2609 ire_uinfo = &ire->ire_uinfo; 2610 } 2611 } else { 2612 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2613 zoneid, tsl, ipst); 2614 if (ire != NULL) { 2615 ire_cacheable = B_TRUE; 2616 ire_uinfo = (ire_mp != NULL) ? 2617 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2618 &ire->ire_uinfo; 2619 2620 } else { 2621 if (ire_mp == NULL) { 2622 ire = ire_ftable_lookup( 2623 tcp->tcp_connp->conn_rem, 2624 0, 0, 0, NULL, &sire, zoneid, 0, 2625 tsl, (MATCH_IRE_RECURSIVE | 2626 MATCH_IRE_DEFAULT), ipst); 2627 if (ire == NULL) 2628 return (0); 2629 ire_uinfo = (sire != NULL) ? 2630 &sire->ire_uinfo : 2631 &ire->ire_uinfo; 2632 } else { 2633 ire = (ire_t *)ire_mp->b_rptr; 2634 ire_uinfo = 2635 &((ire_t *) 2636 ire_mp->b_rptr)->ire_uinfo; 2637 } 2638 } 2639 } 2640 ASSERT(ire != NULL); 2641 2642 if ((ire->ire_src_addr == INADDR_ANY) || 2643 (ire->ire_type & IRE_BROADCAST)) { 2644 /* 2645 * ire->ire_mp is non null when ire_mp passed in is used 2646 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2647 */ 2648 if (ire->ire_mp == NULL) 2649 ire_refrele(ire); 2650 if (sire != NULL) 2651 ire_refrele(sire); 2652 return (0); 2653 } 2654 2655 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2656 ipaddr_t src_addr; 2657 2658 /* 2659 * ip_bind_connected() has stored the correct source 2660 * address in conn_src. 2661 */ 2662 src_addr = tcp->tcp_connp->conn_src; 2663 tcp->tcp_ipha->ipha_src = src_addr; 2664 /* 2665 * Copy of the src addr. in tcp_t is needed 2666 * for the lookup funcs. 2667 */ 2668 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2669 } 2670 /* 2671 * Set the fragment bit so that IP will tell us if the MTU 2672 * should change. IP tells us the latest setting of 2673 * ip_path_mtu_discovery through ire_frag_flag. 2674 */ 2675 if (ipst->ips_ip_path_mtu_discovery) { 2676 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2677 htons(IPH_DF); 2678 } 2679 /* 2680 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2681 * for IP_NEXTHOP. No cache ire has been found for the 2682 * destination and we are working with the nexthop's 2683 * interface ire. Since we need to forward all packets 2684 * to the nexthop first, we "blindly" set tcp_localnet 2685 * to false, eventhough the destination may also be 2686 * onlink. 2687 */ 2688 if (ire_uinfo == NULL) 2689 tcp->tcp_localnet = 0; 2690 else 2691 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2692 } else { 2693 /* 2694 * For incoming connection ire_mp = NULL 2695 * For outgoing connection ire_mp != NULL 2696 * Technically we should check conn_incoming_ill 2697 * when ire_mp is NULL and conn_outgoing_ill when 2698 * ire_mp is non-NULL. But this is performance 2699 * critical path and for IPV*_BOUND_IF, outgoing 2700 * and incoming ill are always set to the same value. 2701 */ 2702 ill_t *dst_ill = NULL; 2703 ipif_t *dst_ipif = NULL; 2704 2705 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2706 2707 if (connp->conn_outgoing_ill != NULL) { 2708 /* Outgoing or incoming path */ 2709 int err; 2710 2711 dst_ill = conn_get_held_ill(connp, 2712 &connp->conn_outgoing_ill, &err); 2713 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2714 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2715 return (0); 2716 } 2717 match_flags |= MATCH_IRE_ILL; 2718 dst_ipif = dst_ill->ill_ipif; 2719 } 2720 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2721 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2722 2723 if (ire != NULL) { 2724 ire_cacheable = B_TRUE; 2725 ire_uinfo = (ire_mp != NULL) ? 2726 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2727 &ire->ire_uinfo; 2728 } else { 2729 if (ire_mp == NULL) { 2730 ire = ire_ftable_lookup_v6( 2731 &tcp->tcp_connp->conn_remv6, 2732 0, 0, 0, dst_ipif, &sire, zoneid, 2733 0, tsl, match_flags, ipst); 2734 if (ire == NULL) { 2735 if (dst_ill != NULL) 2736 ill_refrele(dst_ill); 2737 return (0); 2738 } 2739 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2740 &ire->ire_uinfo; 2741 } else { 2742 ire = (ire_t *)ire_mp->b_rptr; 2743 ire_uinfo = 2744 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2745 } 2746 } 2747 if (dst_ill != NULL) 2748 ill_refrele(dst_ill); 2749 2750 ASSERT(ire != NULL); 2751 ASSERT(ire_uinfo != NULL); 2752 2753 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2754 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2755 /* 2756 * ire->ire_mp is non null when ire_mp passed in is used 2757 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2758 */ 2759 if (ire->ire_mp == NULL) 2760 ire_refrele(ire); 2761 if (sire != NULL) 2762 ire_refrele(sire); 2763 return (0); 2764 } 2765 2766 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2767 in6_addr_t src_addr; 2768 2769 /* 2770 * ip_bind_connected_v6() has stored the correct source 2771 * address per IPv6 addr. selection policy in 2772 * conn_src_v6. 2773 */ 2774 src_addr = tcp->tcp_connp->conn_srcv6; 2775 2776 tcp->tcp_ip6h->ip6_src = src_addr; 2777 /* 2778 * Copy of the src addr. in tcp_t is needed 2779 * for the lookup funcs. 2780 */ 2781 tcp->tcp_ip_src_v6 = src_addr; 2782 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2783 &connp->conn_srcv6)); 2784 } 2785 tcp->tcp_localnet = 2786 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2787 } 2788 2789 /* 2790 * This allows applications to fail quickly when connections are made 2791 * to dead hosts. Hosts can be labeled dead by adding a reject route 2792 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2793 */ 2794 if ((ire->ire_flags & RTF_REJECT) && 2795 (ire->ire_flags & RTF_PRIVATE)) 2796 goto error; 2797 2798 /* 2799 * Make use of the cached rtt and rtt_sd values to calculate the 2800 * initial RTO. Note that they are already initialized in 2801 * tcp_init_values(). 2802 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2803 * IP_NEXTHOP, but instead are using the interface ire for the 2804 * nexthop, then we do not use the ire_uinfo from that ire to 2805 * do any initializations. 2806 */ 2807 if (ire_uinfo != NULL) { 2808 if (ire_uinfo->iulp_rtt != 0) { 2809 clock_t rto; 2810 2811 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2812 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2813 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2814 tcps->tcps_rexmit_interval_extra + 2815 (tcp->tcp_rtt_sa >> 5); 2816 2817 if (rto > tcps->tcps_rexmit_interval_max) { 2818 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2819 } else if (rto < tcps->tcps_rexmit_interval_min) { 2820 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2821 } else { 2822 tcp->tcp_rto = rto; 2823 } 2824 } 2825 if (ire_uinfo->iulp_ssthresh != 0) 2826 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2827 else 2828 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2829 if (ire_uinfo->iulp_spipe > 0) { 2830 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2831 tcps->tcps_max_buf); 2832 if (tcps->tcps_snd_lowat_fraction != 0) 2833 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2834 tcps->tcps_snd_lowat_fraction; 2835 (void) tcp_maxpsz_set(tcp, B_TRUE); 2836 } 2837 /* 2838 * Note that up till now, acceptor always inherits receive 2839 * window from the listener. But if there is a metrics 2840 * associated with a host, we should use that instead of 2841 * inheriting it from listener. Thus we need to pass this 2842 * info back to the caller. 2843 */ 2844 if (ire_uinfo->iulp_rpipe > 0) { 2845 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2846 tcps->tcps_max_buf); 2847 } 2848 2849 if (ire_uinfo->iulp_rtomax > 0) { 2850 tcp->tcp_second_timer_threshold = 2851 ire_uinfo->iulp_rtomax; 2852 } 2853 2854 /* 2855 * Use the metric option settings, iulp_tstamp_ok and 2856 * iulp_wscale_ok, only for active open. What this means 2857 * is that if the other side uses timestamp or window 2858 * scale option, TCP will also use those options. That 2859 * is for passive open. If the application sets a 2860 * large window, window scale is enabled regardless of 2861 * the value in iulp_wscale_ok. This is the behavior 2862 * since 2.6. So we keep it. 2863 * The only case left in passive open processing is the 2864 * check for SACK. 2865 * For ECN, it should probably be like SACK. But the 2866 * current value is binary, so we treat it like the other 2867 * cases. The metric only controls active open.For passive 2868 * open, the ndd param, tcp_ecn_permitted, controls the 2869 * behavior. 2870 */ 2871 if (!tcp_detached) { 2872 /* 2873 * The if check means that the following can only 2874 * be turned on by the metrics only IRE, but not off. 2875 */ 2876 if (ire_uinfo->iulp_tstamp_ok) 2877 tcp->tcp_snd_ts_ok = B_TRUE; 2878 if (ire_uinfo->iulp_wscale_ok) 2879 tcp->tcp_snd_ws_ok = B_TRUE; 2880 if (ire_uinfo->iulp_sack == 2) 2881 tcp->tcp_snd_sack_ok = B_TRUE; 2882 if (ire_uinfo->iulp_ecn_ok) 2883 tcp->tcp_ecn_ok = B_TRUE; 2884 } else { 2885 /* 2886 * Passive open. 2887 * 2888 * As above, the if check means that SACK can only be 2889 * turned on by the metric only IRE. 2890 */ 2891 if (ire_uinfo->iulp_sack > 0) { 2892 tcp->tcp_snd_sack_ok = B_TRUE; 2893 } 2894 } 2895 } 2896 2897 2898 /* 2899 * XXX: Note that currently, ire_max_frag can be as small as 68 2900 * because of PMTUd. So tcp_mss may go to negative if combined 2901 * length of all those options exceeds 28 bytes. But because 2902 * of the tcp_mss_min check below, we may not have a problem if 2903 * tcp_mss_min is of a reasonable value. The default is 1 so 2904 * the negative problem still exists. And the check defeats PMTUd. 2905 * In fact, if PMTUd finds that the MSS should be smaller than 2906 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2907 * value. 2908 * 2909 * We do not deal with that now. All those problems related to 2910 * PMTUd will be fixed later. 2911 */ 2912 ASSERT(ire->ire_max_frag != 0); 2913 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2914 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2915 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2916 mss = MIN(mss, IPV6_MIN_MTU); 2917 } 2918 } 2919 2920 /* Sanity check for MSS value. */ 2921 if (tcp->tcp_ipversion == IPV4_VERSION) 2922 mss_max = tcps->tcps_mss_max_ipv4; 2923 else 2924 mss_max = tcps->tcps_mss_max_ipv6; 2925 2926 if (tcp->tcp_ipversion == IPV6_VERSION && 2927 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2928 /* 2929 * After receiving an ICMPv6 "packet too big" message with a 2930 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2931 * will insert a 8-byte fragment header in every packet; we 2932 * reduce the MSS by that amount here. 2933 */ 2934 mss -= sizeof (ip6_frag_t); 2935 } 2936 2937 if (tcp->tcp_ipsec_overhead == 0) 2938 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2939 2940 mss -= tcp->tcp_ipsec_overhead; 2941 2942 if (mss < tcps->tcps_mss_min) 2943 mss = tcps->tcps_mss_min; 2944 if (mss > mss_max) 2945 mss = mss_max; 2946 2947 /* Note that this is the maximum MSS, excluding all options. */ 2948 tcp->tcp_mss = mss; 2949 2950 /* 2951 * Initialize the ISS here now that we have the full connection ID. 2952 * The RFC 1948 method of initial sequence number generation requires 2953 * knowledge of the full connection ID before setting the ISS. 2954 */ 2955 2956 tcp_iss_init(tcp); 2957 2958 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2959 tcp->tcp_loopback = B_TRUE; 2960 2961 if (tcp->tcp_ipversion == IPV4_VERSION) { 2962 hsp = tcp_hsp_lookup(tcp->tcp_remote, tcps); 2963 } else { 2964 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6, tcps); 2965 } 2966 2967 if (hsp != NULL) { 2968 /* Only modify if we're going to make them bigger */ 2969 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2970 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2971 if (tcps->tcps_snd_lowat_fraction != 0) 2972 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2973 tcps->tcps_snd_lowat_fraction; 2974 } 2975 2976 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2977 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2978 } 2979 2980 /* Copy timestamp flag only for active open */ 2981 if (!tcp_detached) 2982 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2983 } 2984 2985 if (sire != NULL) 2986 IRE_REFRELE(sire); 2987 2988 /* 2989 * If we got an IRE_CACHE and an ILL, go through their properties; 2990 * otherwise, this is deferred until later when we have an IRE_CACHE. 2991 */ 2992 if (tcp->tcp_loopback || 2993 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2994 /* 2995 * For incoming, see if this tcp may be MDT-capable. For 2996 * outgoing, this process has been taken care of through 2997 * tcp_rput_other. 2998 */ 2999 tcp_ire_ill_check(tcp, ire, ill, incoming); 3000 tcp->tcp_ire_ill_check_done = B_TRUE; 3001 } 3002 3003 mutex_enter(&connp->conn_lock); 3004 /* 3005 * Make sure that conn is not marked incipient 3006 * for incoming connections. A blind 3007 * removal of incipient flag is cheaper than 3008 * check and removal. 3009 */ 3010 connp->conn_state_flags &= ~CONN_INCIPIENT; 3011 3012 /* 3013 * Must not cache forwarding table routes 3014 * or recache an IRE after the conn_t has 3015 * had conn_ire_cache cleared and is flagged 3016 * unusable, (see the CONN_CACHE_IRE() macro). 3017 */ 3018 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 3019 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 3020 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 3021 connp->conn_ire_cache = ire; 3022 IRE_UNTRACE_REF(ire); 3023 rw_exit(&ire->ire_bucket->irb_lock); 3024 mutex_exit(&connp->conn_lock); 3025 return (1); 3026 } 3027 rw_exit(&ire->ire_bucket->irb_lock); 3028 } 3029 mutex_exit(&connp->conn_lock); 3030 3031 if (ire->ire_mp == NULL) 3032 ire_refrele(ire); 3033 return (1); 3034 3035 error: 3036 if (ire->ire_mp == NULL) 3037 ire_refrele(ire); 3038 if (sire != NULL) 3039 ire_refrele(sire); 3040 return (0); 3041 } 3042 3043 static void 3044 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 3045 { 3046 int error; 3047 conn_t *connp = tcp->tcp_connp; 3048 struct sockaddr *sa; 3049 mblk_t *mp1; 3050 struct T_bind_req *tbr; 3051 int backlog; 3052 socklen_t len; 3053 sin_t *sin; 3054 sin6_t *sin6; 3055 cred_t *cr; 3056 3057 /* 3058 * All Solaris components should pass a db_credp 3059 * for this TPI message, hence we ASSERT. 3060 * But in case there is some other M_PROTO that looks 3061 * like a TPI message sent by some other kernel 3062 * component, we check and return an error. 3063 */ 3064 cr = msg_getcred(mp, NULL); 3065 ASSERT(cr != NULL); 3066 if (cr == NULL) { 3067 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3068 return; 3069 } 3070 3071 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3072 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3073 if (tcp->tcp_debug) { 3074 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3075 "tcp_tpi_bind: bad req, len %u", 3076 (uint_t)(mp->b_wptr - mp->b_rptr)); 3077 } 3078 tcp_err_ack(tcp, mp, TPROTO, 0); 3079 return; 3080 } 3081 /* Make sure the largest address fits */ 3082 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3083 if (mp1 == NULL) { 3084 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3085 return; 3086 } 3087 mp = mp1; 3088 tbr = (struct T_bind_req *)mp->b_rptr; 3089 3090 backlog = tbr->CONIND_number; 3091 len = tbr->ADDR_length; 3092 3093 switch (len) { 3094 case 0: /* request for a generic port */ 3095 tbr->ADDR_offset = sizeof (struct T_bind_req); 3096 if (tcp->tcp_family == AF_INET) { 3097 tbr->ADDR_length = sizeof (sin_t); 3098 sin = (sin_t *)&tbr[1]; 3099 *sin = sin_null; 3100 sin->sin_family = AF_INET; 3101 sa = (struct sockaddr *)sin; 3102 len = sizeof (sin_t); 3103 mp->b_wptr = (uchar_t *)&sin[1]; 3104 } else { 3105 ASSERT(tcp->tcp_family == AF_INET6); 3106 tbr->ADDR_length = sizeof (sin6_t); 3107 sin6 = (sin6_t *)&tbr[1]; 3108 *sin6 = sin6_null; 3109 sin6->sin6_family = AF_INET6; 3110 sa = (struct sockaddr *)sin6; 3111 len = sizeof (sin6_t); 3112 mp->b_wptr = (uchar_t *)&sin6[1]; 3113 } 3114 break; 3115 3116 case sizeof (sin_t): /* Complete IPv4 address */ 3117 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3118 sizeof (sin_t)); 3119 break; 3120 3121 case sizeof (sin6_t): /* Complete IPv6 address */ 3122 sa = (struct sockaddr *)mi_offset_param(mp, 3123 tbr->ADDR_offset, sizeof (sin6_t)); 3124 break; 3125 3126 default: 3127 if (tcp->tcp_debug) { 3128 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3129 "tcp_tpi_bind: bad address length, %d", 3130 tbr->ADDR_length); 3131 } 3132 tcp_err_ack(tcp, mp, TBADADDR, 0); 3133 return; 3134 } 3135 3136 error = tcp_bind_check(connp, sa, len, cr, 3137 tbr->PRIM_type != O_T_BIND_REQ); 3138 if (error == 0) { 3139 if (tcp->tcp_family == AF_INET) { 3140 sin = (sin_t *)sa; 3141 sin->sin_port = tcp->tcp_lport; 3142 } else { 3143 sin6 = (sin6_t *)sa; 3144 sin6->sin6_port = tcp->tcp_lport; 3145 } 3146 3147 if (backlog > 0) { 3148 error = tcp_do_listen(connp, backlog, cr); 3149 } 3150 } 3151 done: 3152 if (error > 0) { 3153 tcp_err_ack(tcp, mp, TSYSERR, error); 3154 } else if (error < 0) { 3155 tcp_err_ack(tcp, mp, -error, 0); 3156 } else { 3157 mp->b_datap->db_type = M_PCPROTO; 3158 tbr->PRIM_type = T_BIND_ACK; 3159 putnext(tcp->tcp_rq, mp); 3160 } 3161 } 3162 3163 /* 3164 * If the "bind_to_req_port_only" parameter is set, if the requested port 3165 * number is available, return it, If not return 0 3166 * 3167 * If "bind_to_req_port_only" parameter is not set and 3168 * If the requested port number is available, return it. If not, return 3169 * the first anonymous port we happen across. If no anonymous ports are 3170 * available, return 0. addr is the requested local address, if any. 3171 * 3172 * In either case, when succeeding update the tcp_t to record the port number 3173 * and insert it in the bind hash table. 3174 * 3175 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3176 * without setting SO_REUSEADDR. This is needed so that they 3177 * can be viewed as two independent transport protocols. 3178 */ 3179 static in_port_t 3180 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3181 int reuseaddr, boolean_t quick_connect, 3182 boolean_t bind_to_req_port_only, boolean_t user_specified) 3183 { 3184 /* number of times we have run around the loop */ 3185 int count = 0; 3186 /* maximum number of times to run around the loop */ 3187 int loopmax; 3188 conn_t *connp = tcp->tcp_connp; 3189 zoneid_t zoneid = connp->conn_zoneid; 3190 tcp_stack_t *tcps = tcp->tcp_tcps; 3191 3192 /* 3193 * Lookup for free addresses is done in a loop and "loopmax" 3194 * influences how long we spin in the loop 3195 */ 3196 if (bind_to_req_port_only) { 3197 /* 3198 * If the requested port is busy, don't bother to look 3199 * for a new one. Setting loop maximum count to 1 has 3200 * that effect. 3201 */ 3202 loopmax = 1; 3203 } else { 3204 /* 3205 * If the requested port is busy, look for a free one 3206 * in the anonymous port range. 3207 * Set loopmax appropriately so that one does not look 3208 * forever in the case all of the anonymous ports are in use. 3209 */ 3210 if (tcp->tcp_anon_priv_bind) { 3211 /* 3212 * loopmax = 3213 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3214 */ 3215 loopmax = IPPORT_RESERVED - 3216 tcps->tcps_min_anonpriv_port; 3217 } else { 3218 loopmax = (tcps->tcps_largest_anon_port - 3219 tcps->tcps_smallest_anon_port + 1); 3220 } 3221 } 3222 do { 3223 uint16_t lport; 3224 tf_t *tbf; 3225 tcp_t *ltcp; 3226 conn_t *lconnp; 3227 3228 lport = htons(port); 3229 3230 /* 3231 * Ensure that the tcp_t is not currently in the bind hash. 3232 * Hold the lock on the hash bucket to ensure that 3233 * the duplicate check plus the insertion is an atomic 3234 * operation. 3235 * 3236 * This function does an inline lookup on the bind hash list 3237 * Make sure that we access only members of tcp_t 3238 * and that we don't look at tcp_tcp, since we are not 3239 * doing a CONN_INC_REF. 3240 */ 3241 tcp_bind_hash_remove(tcp); 3242 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3243 mutex_enter(&tbf->tf_lock); 3244 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3245 ltcp = ltcp->tcp_bind_hash) { 3246 if (lport == ltcp->tcp_lport) 3247 break; 3248 } 3249 3250 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3251 boolean_t not_socket; 3252 boolean_t exclbind; 3253 3254 lconnp = ltcp->tcp_connp; 3255 3256 /* 3257 * On a labeled system, we must treat bindings to ports 3258 * on shared IP addresses by sockets with MAC exemption 3259 * privilege as being in all zones, as there's 3260 * otherwise no way to identify the right receiver. 3261 */ 3262 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3263 IPCL_ZONE_MATCH(connp, 3264 ltcp->tcp_connp->conn_zoneid)) && 3265 !lconnp->conn_mac_exempt && 3266 !connp->conn_mac_exempt) 3267 continue; 3268 3269 /* 3270 * If TCP_EXCLBIND is set for either the bound or 3271 * binding endpoint, the semantics of bind 3272 * is changed according to the following. 3273 * 3274 * spec = specified address (v4 or v6) 3275 * unspec = unspecified address (v4 or v6) 3276 * A = specified addresses are different for endpoints 3277 * 3278 * bound bind to allowed 3279 * ------------------------------------- 3280 * unspec unspec no 3281 * unspec spec no 3282 * spec unspec no 3283 * spec spec yes if A 3284 * 3285 * For labeled systems, SO_MAC_EXEMPT behaves the same 3286 * as TCP_EXCLBIND, except that zoneid is ignored. 3287 * 3288 * Note: 3289 * 3290 * 1. Because of TLI semantics, an endpoint can go 3291 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3292 * TCPS_BOUND, depending on whether it is originally 3293 * a listener or not. That is why we need to check 3294 * for states greater than or equal to TCPS_BOUND 3295 * here. 3296 * 3297 * 2. Ideally, we should only check for state equals 3298 * to TCPS_LISTEN. And the following check should be 3299 * added. 3300 * 3301 * if (ltcp->tcp_state == TCPS_LISTEN || 3302 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3303 * ... 3304 * } 3305 * 3306 * The semantics will be changed to this. If the 3307 * endpoint on the list is in state not equal to 3308 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3309 * set, let the bind succeed. 3310 * 3311 * Because of (1), we cannot do that for TLI 3312 * endpoints. But we can do that for socket endpoints. 3313 * If in future, we can change this going back 3314 * semantics, we can use the above check for TLI also. 3315 */ 3316 not_socket = !(TCP_IS_SOCKET(ltcp) && 3317 TCP_IS_SOCKET(tcp)); 3318 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3319 3320 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3321 (exclbind && (not_socket || 3322 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3323 if (V6_OR_V4_INADDR_ANY( 3324 ltcp->tcp_bound_source_v6) || 3325 V6_OR_V4_INADDR_ANY(*laddr) || 3326 IN6_ARE_ADDR_EQUAL(laddr, 3327 <cp->tcp_bound_source_v6)) { 3328 break; 3329 } 3330 continue; 3331 } 3332 3333 /* 3334 * Check ipversion to allow IPv4 and IPv6 sockets to 3335 * have disjoint port number spaces, if *_EXCLBIND 3336 * is not set and only if the application binds to a 3337 * specific port. We use the same autoassigned port 3338 * number space for IPv4 and IPv6 sockets. 3339 */ 3340 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3341 bind_to_req_port_only) 3342 continue; 3343 3344 /* 3345 * Ideally, we should make sure that the source 3346 * address, remote address, and remote port in the 3347 * four tuple for this tcp-connection is unique. 3348 * However, trying to find out the local source 3349 * address would require too much code duplication 3350 * with IP, since IP needs needs to have that code 3351 * to support userland TCP implementations. 3352 */ 3353 if (quick_connect && 3354 (ltcp->tcp_state > TCPS_LISTEN) && 3355 ((tcp->tcp_fport != ltcp->tcp_fport) || 3356 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3357 <cp->tcp_remote_v6))) 3358 continue; 3359 3360 if (!reuseaddr) { 3361 /* 3362 * No socket option SO_REUSEADDR. 3363 * If existing port is bound to 3364 * a non-wildcard IP address 3365 * and the requesting stream is 3366 * bound to a distinct 3367 * different IP addresses 3368 * (non-wildcard, also), keep 3369 * going. 3370 */ 3371 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3372 !V6_OR_V4_INADDR_ANY( 3373 ltcp->tcp_bound_source_v6) && 3374 !IN6_ARE_ADDR_EQUAL(laddr, 3375 <cp->tcp_bound_source_v6)) 3376 continue; 3377 if (ltcp->tcp_state >= TCPS_BOUND) { 3378 /* 3379 * This port is being used and 3380 * its state is >= TCPS_BOUND, 3381 * so we can't bind to it. 3382 */ 3383 break; 3384 } 3385 } else { 3386 /* 3387 * socket option SO_REUSEADDR is set on the 3388 * binding tcp_t. 3389 * 3390 * If two streams are bound to 3391 * same IP address or both addr 3392 * and bound source are wildcards 3393 * (INADDR_ANY), we want to stop 3394 * searching. 3395 * We have found a match of IP source 3396 * address and source port, which is 3397 * refused regardless of the 3398 * SO_REUSEADDR setting, so we break. 3399 */ 3400 if (IN6_ARE_ADDR_EQUAL(laddr, 3401 <cp->tcp_bound_source_v6) && 3402 (ltcp->tcp_state == TCPS_LISTEN || 3403 ltcp->tcp_state == TCPS_BOUND)) 3404 break; 3405 } 3406 } 3407 if (ltcp != NULL) { 3408 /* The port number is busy */ 3409 mutex_exit(&tbf->tf_lock); 3410 } else { 3411 /* 3412 * This port is ours. Insert in fanout and mark as 3413 * bound to prevent others from getting the port 3414 * number. 3415 */ 3416 tcp->tcp_state = TCPS_BOUND; 3417 tcp->tcp_lport = htons(port); 3418 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3419 3420 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3421 tcp->tcp_lport)] == tbf); 3422 tcp_bind_hash_insert(tbf, tcp, 1); 3423 3424 mutex_exit(&tbf->tf_lock); 3425 3426 /* 3427 * We don't want tcp_next_port_to_try to "inherit" 3428 * a port number supplied by the user in a bind. 3429 */ 3430 if (user_specified) 3431 return (port); 3432 3433 /* 3434 * This is the only place where tcp_next_port_to_try 3435 * is updated. After the update, it may or may not 3436 * be in the valid range. 3437 */ 3438 if (!tcp->tcp_anon_priv_bind) 3439 tcps->tcps_next_port_to_try = port + 1; 3440 return (port); 3441 } 3442 3443 if (tcp->tcp_anon_priv_bind) { 3444 port = tcp_get_next_priv_port(tcp); 3445 } else { 3446 if (count == 0 && user_specified) { 3447 /* 3448 * We may have to return an anonymous port. So 3449 * get one to start with. 3450 */ 3451 port = 3452 tcp_update_next_port( 3453 tcps->tcps_next_port_to_try, 3454 tcp, B_TRUE); 3455 user_specified = B_FALSE; 3456 } else { 3457 port = tcp_update_next_port(port + 1, tcp, 3458 B_FALSE); 3459 } 3460 } 3461 if (port == 0) 3462 break; 3463 3464 /* 3465 * Don't let this loop run forever in the case where 3466 * all of the anonymous ports are in use. 3467 */ 3468 } while (++count < loopmax); 3469 return (0); 3470 } 3471 3472 /* 3473 * tcp_clean_death / tcp_close_detached must not be called more than once 3474 * on a tcp. Thus every function that potentially calls tcp_clean_death 3475 * must check for the tcp state before calling tcp_clean_death. 3476 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3477 * tcp_timer_handler, all check for the tcp state. 3478 */ 3479 /* ARGSUSED */ 3480 void 3481 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3482 { 3483 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3484 3485 freemsg(mp); 3486 if (tcp->tcp_state > TCPS_BOUND) 3487 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3488 ETIMEDOUT, 5); 3489 } 3490 3491 /* 3492 * We are dying for some reason. Try to do it gracefully. (May be called 3493 * as writer.) 3494 * 3495 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3496 * done by a service procedure). 3497 * TBD - Should the return value distinguish between the tcp_t being 3498 * freed and it being reinitialized? 3499 */ 3500 static int 3501 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3502 { 3503 mblk_t *mp; 3504 queue_t *q; 3505 conn_t *connp = tcp->tcp_connp; 3506 tcp_stack_t *tcps = tcp->tcp_tcps; 3507 sodirect_t *sodp; 3508 3509 TCP_CLD_STAT(tag); 3510 3511 #if TCP_TAG_CLEAN_DEATH 3512 tcp->tcp_cleandeathtag = tag; 3513 #endif 3514 3515 if (tcp->tcp_fused) 3516 tcp_unfuse(tcp); 3517 3518 if (tcp->tcp_linger_tid != 0 && 3519 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3520 tcp_stop_lingering(tcp); 3521 } 3522 3523 ASSERT(tcp != NULL); 3524 ASSERT((tcp->tcp_family == AF_INET && 3525 tcp->tcp_ipversion == IPV4_VERSION) || 3526 (tcp->tcp_family == AF_INET6 && 3527 (tcp->tcp_ipversion == IPV4_VERSION || 3528 tcp->tcp_ipversion == IPV6_VERSION))); 3529 3530 if (TCP_IS_DETACHED(tcp)) { 3531 if (tcp->tcp_hard_binding) { 3532 /* 3533 * Its an eager that we are dealing with. We close the 3534 * eager but in case a conn_ind has already gone to the 3535 * listener, let tcp_accept_finish() send a discon_ind 3536 * to the listener and drop the last reference. If the 3537 * listener doesn't even know about the eager i.e. the 3538 * conn_ind hasn't gone up, blow away the eager and drop 3539 * the last reference as well. If the conn_ind has gone 3540 * up, state should be BOUND. tcp_accept_finish 3541 * will figure out that the connection has received a 3542 * RST and will send a DISCON_IND to the application. 3543 */ 3544 tcp_closei_local(tcp); 3545 if (!tcp->tcp_tconnind_started) { 3546 CONN_DEC_REF(connp); 3547 } else { 3548 tcp->tcp_state = TCPS_BOUND; 3549 } 3550 } else { 3551 tcp_close_detached(tcp); 3552 } 3553 return (0); 3554 } 3555 3556 TCP_STAT(tcps, tcp_clean_death_nondetached); 3557 3558 /* If sodirect, not anymore */ 3559 SOD_PTR_ENTER(tcp, sodp); 3560 if (sodp != NULL) { 3561 tcp->tcp_sodirect = NULL; 3562 mutex_exit(sodp->sod_lockp); 3563 } 3564 3565 q = tcp->tcp_rq; 3566 3567 /* Trash all inbound data */ 3568 if (!IPCL_IS_NONSTR(connp)) { 3569 ASSERT(q != NULL); 3570 flushq(q, FLUSHALL); 3571 } 3572 3573 /* 3574 * If we are at least part way open and there is error 3575 * (err==0 implies no error) 3576 * notify our client by a T_DISCON_IND. 3577 */ 3578 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3579 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3580 !TCP_IS_SOCKET(tcp)) { 3581 /* 3582 * Send M_FLUSH according to TPI. Because sockets will 3583 * (and must) ignore FLUSHR we do that only for TPI 3584 * endpoints and sockets in STREAMS mode. 3585 */ 3586 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3587 } 3588 if (tcp->tcp_debug) { 3589 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3590 "tcp_clean_death: discon err %d", err); 3591 } 3592 if (IPCL_IS_NONSTR(connp)) { 3593 /* Direct socket, use upcall */ 3594 (*connp->conn_upcalls->su_disconnected)( 3595 connp->conn_upper_handle, tcp->tcp_connid, err); 3596 } else { 3597 mp = mi_tpi_discon_ind(NULL, err, 0); 3598 if (mp != NULL) { 3599 putnext(q, mp); 3600 } else { 3601 if (tcp->tcp_debug) { 3602 (void) strlog(TCP_MOD_ID, 0, 1, 3603 SL_ERROR|SL_TRACE, 3604 "tcp_clean_death, sending M_ERROR"); 3605 } 3606 (void) putnextctl1(q, M_ERROR, EPROTO); 3607 } 3608 } 3609 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3610 /* SYN_SENT or SYN_RCVD */ 3611 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3612 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3613 /* ESTABLISHED or CLOSE_WAIT */ 3614 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3615 } 3616 } 3617 3618 tcp_reinit(tcp); 3619 if (IPCL_IS_NONSTR(connp)) 3620 (void) tcp_do_unbind(connp); 3621 3622 return (-1); 3623 } 3624 3625 /* 3626 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3627 * to expire, stop the wait and finish the close. 3628 */ 3629 static void 3630 tcp_stop_lingering(tcp_t *tcp) 3631 { 3632 clock_t delta = 0; 3633 tcp_stack_t *tcps = tcp->tcp_tcps; 3634 3635 tcp->tcp_linger_tid = 0; 3636 if (tcp->tcp_state > TCPS_LISTEN) { 3637 tcp_acceptor_hash_remove(tcp); 3638 mutex_enter(&tcp->tcp_non_sq_lock); 3639 if (tcp->tcp_flow_stopped) { 3640 tcp_clrqfull(tcp); 3641 } 3642 mutex_exit(&tcp->tcp_non_sq_lock); 3643 3644 if (tcp->tcp_timer_tid != 0) { 3645 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3646 tcp->tcp_timer_tid = 0; 3647 } 3648 /* 3649 * Need to cancel those timers which will not be used when 3650 * TCP is detached. This has to be done before the tcp_wq 3651 * is set to the global queue. 3652 */ 3653 tcp_timers_stop(tcp); 3654 3655 tcp->tcp_detached = B_TRUE; 3656 ASSERT(tcps->tcps_g_q != NULL); 3657 tcp->tcp_rq = tcps->tcps_g_q; 3658 tcp->tcp_wq = WR(tcps->tcps_g_q); 3659 3660 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3661 tcp_time_wait_append(tcp); 3662 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3663 goto finish; 3664 } 3665 3666 /* 3667 * If delta is zero the timer event wasn't executed and was 3668 * successfully canceled. In this case we need to restart it 3669 * with the minimal delta possible. 3670 */ 3671 if (delta >= 0) { 3672 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3673 delta ? delta : 1); 3674 } 3675 } else { 3676 tcp_closei_local(tcp); 3677 CONN_DEC_REF(tcp->tcp_connp); 3678 } 3679 finish: 3680 /* Signal closing thread that it can complete close */ 3681 mutex_enter(&tcp->tcp_closelock); 3682 tcp->tcp_detached = B_TRUE; 3683 ASSERT(tcps->tcps_g_q != NULL); 3684 3685 tcp->tcp_rq = tcps->tcps_g_q; 3686 tcp->tcp_wq = WR(tcps->tcps_g_q); 3687 3688 tcp->tcp_closed = 1; 3689 cv_signal(&tcp->tcp_closecv); 3690 mutex_exit(&tcp->tcp_closelock); 3691 } 3692 3693 /* 3694 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3695 * expires. 3696 */ 3697 static void 3698 tcp_close_linger_timeout(void *arg) 3699 { 3700 conn_t *connp = (conn_t *)arg; 3701 tcp_t *tcp = connp->conn_tcp; 3702 3703 tcp->tcp_client_errno = ETIMEDOUT; 3704 tcp_stop_lingering(tcp); 3705 } 3706 3707 static void 3708 tcp_close_common(conn_t *connp, int flags) 3709 { 3710 tcp_t *tcp = connp->conn_tcp; 3711 mblk_t *mp = &tcp->tcp_closemp; 3712 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3713 mblk_t *bp; 3714 3715 ASSERT(connp->conn_ref >= 2); 3716 3717 /* 3718 * Mark the conn as closing. ill_pending_mp_add will not 3719 * add any mp to the pending mp list, after this conn has 3720 * started closing. Same for sq_pending_mp_add 3721 */ 3722 mutex_enter(&connp->conn_lock); 3723 connp->conn_state_flags |= CONN_CLOSING; 3724 if (connp->conn_oper_pending_ill != NULL) 3725 conn_ioctl_cleanup_reqd = B_TRUE; 3726 CONN_INC_REF_LOCKED(connp); 3727 mutex_exit(&connp->conn_lock); 3728 tcp->tcp_closeflags = (uint8_t)flags; 3729 ASSERT(connp->conn_ref >= 3); 3730 3731 /* 3732 * tcp_closemp_used is used below without any protection of a lock 3733 * as we don't expect any one else to use it concurrently at this 3734 * point otherwise it would be a major defect. 3735 */ 3736 3737 if (mp->b_prev == NULL) 3738 tcp->tcp_closemp_used = B_TRUE; 3739 else 3740 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3741 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3742 3743 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3744 3745 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3746 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3747 3748 mutex_enter(&tcp->tcp_closelock); 3749 while (!tcp->tcp_closed) { 3750 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3751 /* 3752 * The cv_wait_sig() was interrupted. We now do the 3753 * following: 3754 * 3755 * 1) If the endpoint was lingering, we allow this 3756 * to be interrupted by cancelling the linger timeout 3757 * and closing normally. 3758 * 3759 * 2) Revert to calling cv_wait() 3760 * 3761 * We revert to using cv_wait() to avoid an 3762 * infinite loop which can occur if the calling 3763 * thread is higher priority than the squeue worker 3764 * thread and is bound to the same cpu. 3765 */ 3766 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3767 mutex_exit(&tcp->tcp_closelock); 3768 /* Entering squeue, bump ref count. */ 3769 CONN_INC_REF(connp); 3770 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3771 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3772 tcp_linger_interrupted, connp, 3773 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3774 mutex_enter(&tcp->tcp_closelock); 3775 } 3776 break; 3777 } 3778 } 3779 while (!tcp->tcp_closed) 3780 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3781 mutex_exit(&tcp->tcp_closelock); 3782 3783 /* 3784 * In the case of listener streams that have eagers in the q or q0 3785 * we wait for the eagers to drop their reference to us. tcp_rq and 3786 * tcp_wq of the eagers point to our queues. By waiting for the 3787 * refcnt to drop to 1, we are sure that the eagers have cleaned 3788 * up their queue pointers and also dropped their references to us. 3789 */ 3790 if (tcp->tcp_wait_for_eagers) { 3791 mutex_enter(&connp->conn_lock); 3792 while (connp->conn_ref != 1) { 3793 cv_wait(&connp->conn_cv, &connp->conn_lock); 3794 } 3795 mutex_exit(&connp->conn_lock); 3796 } 3797 /* 3798 * ioctl cleanup. The mp is queued in the 3799 * ill_pending_mp or in the sq_pending_mp. 3800 */ 3801 if (conn_ioctl_cleanup_reqd) 3802 conn_ioctl_cleanup(connp); 3803 3804 tcp->tcp_cpid = -1; 3805 } 3806 3807 static int 3808 tcp_tpi_close(queue_t *q, int flags) 3809 { 3810 conn_t *connp; 3811 3812 ASSERT(WR(q)->q_next == NULL); 3813 3814 if (flags & SO_FALLBACK) { 3815 /* 3816 * stream is being closed while in fallback 3817 * simply free the resources that were allocated 3818 */ 3819 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3820 qprocsoff(q); 3821 goto done; 3822 } 3823 3824 connp = Q_TO_CONN(q); 3825 /* 3826 * We are being closed as /dev/tcp or /dev/tcp6. 3827 */ 3828 tcp_close_common(connp, flags); 3829 3830 qprocsoff(q); 3831 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3832 3833 /* 3834 * Drop IP's reference on the conn. This is the last reference 3835 * on the connp if the state was less than established. If the 3836 * connection has gone into timewait state, then we will have 3837 * one ref for the TCP and one more ref (total of two) for the 3838 * classifier connected hash list (a timewait connections stays 3839 * in connected hash till closed). 3840 * 3841 * We can't assert the references because there might be other 3842 * transient reference places because of some walkers or queued 3843 * packets in squeue for the timewait state. 3844 */ 3845 CONN_DEC_REF(connp); 3846 done: 3847 q->q_ptr = WR(q)->q_ptr = NULL; 3848 return (0); 3849 } 3850 3851 static int 3852 tcpclose_accept(queue_t *q) 3853 { 3854 vmem_t *minor_arena; 3855 dev_t conn_dev; 3856 3857 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3858 3859 /* 3860 * We had opened an acceptor STREAM for sockfs which is 3861 * now being closed due to some error. 3862 */ 3863 qprocsoff(q); 3864 3865 minor_arena = (vmem_t *)WR(q)->q_ptr; 3866 conn_dev = (dev_t)RD(q)->q_ptr; 3867 ASSERT(minor_arena != NULL); 3868 ASSERT(conn_dev != 0); 3869 inet_minor_free(minor_arena, conn_dev); 3870 q->q_ptr = WR(q)->q_ptr = NULL; 3871 return (0); 3872 } 3873 3874 /* 3875 * Called by tcp_close() routine via squeue when lingering is 3876 * interrupted by a signal. 3877 */ 3878 3879 /* ARGSUSED */ 3880 static void 3881 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3882 { 3883 conn_t *connp = (conn_t *)arg; 3884 tcp_t *tcp = connp->conn_tcp; 3885 3886 freeb(mp); 3887 if (tcp->tcp_linger_tid != 0 && 3888 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3889 tcp_stop_lingering(tcp); 3890 tcp->tcp_client_errno = EINTR; 3891 } 3892 } 3893 3894 /* 3895 * Called by streams close routine via squeues when our client blows off her 3896 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3897 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3898 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3899 * acked. 3900 * 3901 * NOTE: tcp_close potentially returns error when lingering. 3902 * However, the stream head currently does not pass these errors 3903 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3904 * errors to the application (from tsleep()) and not errors 3905 * like ECONNRESET caused by receiving a reset packet. 3906 */ 3907 3908 /* ARGSUSED */ 3909 static void 3910 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3911 { 3912 char *msg; 3913 conn_t *connp = (conn_t *)arg; 3914 tcp_t *tcp = connp->conn_tcp; 3915 clock_t delta = 0; 3916 tcp_stack_t *tcps = tcp->tcp_tcps; 3917 3918 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3919 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3920 3921 mutex_enter(&tcp->tcp_eager_lock); 3922 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3923 /* Cleanup for listener */ 3924 tcp_eager_cleanup(tcp, 0); 3925 tcp->tcp_wait_for_eagers = 1; 3926 } 3927 mutex_exit(&tcp->tcp_eager_lock); 3928 3929 connp->conn_mdt_ok = B_FALSE; 3930 tcp->tcp_mdt = B_FALSE; 3931 3932 connp->conn_lso_ok = B_FALSE; 3933 tcp->tcp_lso = B_FALSE; 3934 3935 msg = NULL; 3936 switch (tcp->tcp_state) { 3937 case TCPS_CLOSED: 3938 case TCPS_IDLE: 3939 case TCPS_BOUND: 3940 case TCPS_LISTEN: 3941 break; 3942 case TCPS_SYN_SENT: 3943 msg = "tcp_close, during connect"; 3944 break; 3945 case TCPS_SYN_RCVD: 3946 /* 3947 * Close during the connect 3-way handshake 3948 * but here there may or may not be pending data 3949 * already on queue. Process almost same as in 3950 * the ESTABLISHED state. 3951 */ 3952 /* FALLTHRU */ 3953 default: 3954 if (tcp->tcp_sodirect != NULL) { 3955 /* Ok, no more sodirect */ 3956 tcp->tcp_sodirect = NULL; 3957 } 3958 3959 if (tcp->tcp_fused) 3960 tcp_unfuse(tcp); 3961 3962 /* 3963 * If SO_LINGER has set a zero linger time, abort the 3964 * connection with a reset. 3965 */ 3966 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3967 msg = "tcp_close, zero lingertime"; 3968 break; 3969 } 3970 3971 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3972 /* 3973 * Abort connection if there is unread data queued. 3974 */ 3975 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3976 msg = "tcp_close, unread data"; 3977 break; 3978 } 3979 /* 3980 * tcp_hard_bound is now cleared thus all packets go through 3981 * tcp_lookup. This fact is used by tcp_detach below. 3982 * 3983 * We have done a qwait() above which could have possibly 3984 * drained more messages in turn causing transition to a 3985 * different state. Check whether we have to do the rest 3986 * of the processing or not. 3987 */ 3988 if (tcp->tcp_state <= TCPS_LISTEN) 3989 break; 3990 3991 /* 3992 * Transmit the FIN before detaching the tcp_t. 3993 * After tcp_detach returns this queue/perimeter 3994 * no longer owns the tcp_t thus others can modify it. 3995 */ 3996 (void) tcp_xmit_end(tcp); 3997 3998 /* 3999 * If lingering on close then wait until the fin is acked, 4000 * the SO_LINGER time passes, or a reset is sent/received. 4001 */ 4002 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4003 !(tcp->tcp_fin_acked) && 4004 tcp->tcp_state >= TCPS_ESTABLISHED) { 4005 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4006 tcp->tcp_client_errno = EWOULDBLOCK; 4007 } else if (tcp->tcp_client_errno == 0) { 4008 4009 ASSERT(tcp->tcp_linger_tid == 0); 4010 4011 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4012 tcp_close_linger_timeout, 4013 tcp->tcp_lingertime * hz); 4014 4015 /* tcp_close_linger_timeout will finish close */ 4016 if (tcp->tcp_linger_tid == 0) 4017 tcp->tcp_client_errno = ENOSR; 4018 else 4019 return; 4020 } 4021 4022 /* 4023 * Check if we need to detach or just close 4024 * the instance. 4025 */ 4026 if (tcp->tcp_state <= TCPS_LISTEN) 4027 break; 4028 } 4029 4030 /* 4031 * Make sure that no other thread will access the tcp_rq of 4032 * this instance (through lookups etc.) as tcp_rq will go 4033 * away shortly. 4034 */ 4035 tcp_acceptor_hash_remove(tcp); 4036 4037 mutex_enter(&tcp->tcp_non_sq_lock); 4038 if (tcp->tcp_flow_stopped) { 4039 tcp_clrqfull(tcp); 4040 } 4041 mutex_exit(&tcp->tcp_non_sq_lock); 4042 4043 if (tcp->tcp_timer_tid != 0) { 4044 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4045 tcp->tcp_timer_tid = 0; 4046 } 4047 /* 4048 * Need to cancel those timers which will not be used when 4049 * TCP is detached. This has to be done before the tcp_wq 4050 * is set to the global queue. 4051 */ 4052 tcp_timers_stop(tcp); 4053 4054 tcp->tcp_detached = B_TRUE; 4055 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4056 tcp_time_wait_append(tcp); 4057 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4058 ASSERT(connp->conn_ref >= 3); 4059 goto finish; 4060 } 4061 4062 /* 4063 * If delta is zero the timer event wasn't executed and was 4064 * successfully canceled. In this case we need to restart it 4065 * with the minimal delta possible. 4066 */ 4067 if (delta >= 0) 4068 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4069 delta ? delta : 1); 4070 4071 ASSERT(connp->conn_ref >= 3); 4072 goto finish; 4073 } 4074 4075 /* Detach did not complete. Still need to remove q from stream. */ 4076 if (msg) { 4077 if (tcp->tcp_state == TCPS_ESTABLISHED || 4078 tcp->tcp_state == TCPS_CLOSE_WAIT) 4079 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4080 if (tcp->tcp_state == TCPS_SYN_SENT || 4081 tcp->tcp_state == TCPS_SYN_RCVD) 4082 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4083 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4084 } 4085 4086 tcp_closei_local(tcp); 4087 CONN_DEC_REF(connp); 4088 ASSERT(connp->conn_ref >= 2); 4089 4090 finish: 4091 /* 4092 * Although packets are always processed on the correct 4093 * tcp's perimeter and access is serialized via squeue's, 4094 * IP still needs a queue when sending packets in time_wait 4095 * state so use WR(tcps_g_q) till ip_output() can be 4096 * changed to deal with just connp. For read side, we 4097 * could have set tcp_rq to NULL but there are some cases 4098 * in tcp_rput_data() from early days of this code which 4099 * do a putnext without checking if tcp is closed. Those 4100 * need to be identified before both tcp_rq and tcp_wq 4101 * can be set to NULL and tcps_g_q can disappear forever. 4102 */ 4103 mutex_enter(&tcp->tcp_closelock); 4104 /* 4105 * Don't change the queues in the case of a listener that has 4106 * eagers in its q or q0. It could surprise the eagers. 4107 * Instead wait for the eagers outside the squeue. 4108 */ 4109 if (!tcp->tcp_wait_for_eagers) { 4110 tcp->tcp_detached = B_TRUE; 4111 /* 4112 * When default queue is closing we set tcps_g_q to NULL 4113 * after the close is done. 4114 */ 4115 ASSERT(tcps->tcps_g_q != NULL); 4116 tcp->tcp_rq = tcps->tcps_g_q; 4117 tcp->tcp_wq = WR(tcps->tcps_g_q); 4118 } 4119 4120 /* Signal tcp_close() to finish closing. */ 4121 tcp->tcp_closed = 1; 4122 cv_signal(&tcp->tcp_closecv); 4123 mutex_exit(&tcp->tcp_closelock); 4124 } 4125 4126 4127 /* 4128 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4129 * Some stream heads get upset if they see these later on as anything but NULL. 4130 */ 4131 static void 4132 tcp_close_mpp(mblk_t **mpp) 4133 { 4134 mblk_t *mp; 4135 4136 if ((mp = *mpp) != NULL) { 4137 do { 4138 mp->b_next = NULL; 4139 mp->b_prev = NULL; 4140 } while ((mp = mp->b_cont) != NULL); 4141 4142 mp = *mpp; 4143 *mpp = NULL; 4144 freemsg(mp); 4145 } 4146 } 4147 4148 /* Do detached close. */ 4149 static void 4150 tcp_close_detached(tcp_t *tcp) 4151 { 4152 if (tcp->tcp_fused) 4153 tcp_unfuse(tcp); 4154 4155 /* 4156 * Clustering code serializes TCP disconnect callbacks and 4157 * cluster tcp list walks by blocking a TCP disconnect callback 4158 * if a cluster tcp list walk is in progress. This ensures 4159 * accurate accounting of TCPs in the cluster code even though 4160 * the TCP list walk itself is not atomic. 4161 */ 4162 tcp_closei_local(tcp); 4163 CONN_DEC_REF(tcp->tcp_connp); 4164 } 4165 4166 /* 4167 * Stop all TCP timers, and free the timer mblks if requested. 4168 */ 4169 void 4170 tcp_timers_stop(tcp_t *tcp) 4171 { 4172 if (tcp->tcp_timer_tid != 0) { 4173 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4174 tcp->tcp_timer_tid = 0; 4175 } 4176 if (tcp->tcp_ka_tid != 0) { 4177 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4178 tcp->tcp_ka_tid = 0; 4179 } 4180 if (tcp->tcp_ack_tid != 0) { 4181 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4182 tcp->tcp_ack_tid = 0; 4183 } 4184 if (tcp->tcp_push_tid != 0) { 4185 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4186 tcp->tcp_push_tid = 0; 4187 } 4188 } 4189 4190 /* 4191 * The tcp_t is going away. Remove it from all lists and set it 4192 * to TCPS_CLOSED. The freeing up of memory is deferred until 4193 * tcp_inactive. This is needed since a thread in tcp_rput might have 4194 * done a CONN_INC_REF on this structure before it was removed from the 4195 * hashes. 4196 */ 4197 static void 4198 tcp_closei_local(tcp_t *tcp) 4199 { 4200 ire_t *ire; 4201 conn_t *connp = tcp->tcp_connp; 4202 tcp_stack_t *tcps = tcp->tcp_tcps; 4203 4204 if (!TCP_IS_SOCKET(tcp)) 4205 tcp_acceptor_hash_remove(tcp); 4206 4207 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4208 tcp->tcp_ibsegs = 0; 4209 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4210 tcp->tcp_obsegs = 0; 4211 4212 /* 4213 * If we are an eager connection hanging off a listener that 4214 * hasn't formally accepted the connection yet, get off his 4215 * list and blow off any data that we have accumulated. 4216 */ 4217 if (tcp->tcp_listener != NULL) { 4218 tcp_t *listener = tcp->tcp_listener; 4219 mutex_enter(&listener->tcp_eager_lock); 4220 /* 4221 * tcp_tconnind_started == B_TRUE means that the 4222 * conn_ind has already gone to listener. At 4223 * this point, eager will be closed but we 4224 * leave it in listeners eager list so that 4225 * if listener decides to close without doing 4226 * accept, we can clean this up. In tcp_wput_accept 4227 * we take care of the case of accept on closed 4228 * eager. 4229 */ 4230 if (!tcp->tcp_tconnind_started) { 4231 tcp_eager_unlink(tcp); 4232 mutex_exit(&listener->tcp_eager_lock); 4233 /* 4234 * We don't want to have any pointers to the 4235 * listener queue, after we have released our 4236 * reference on the listener 4237 */ 4238 ASSERT(tcps->tcps_g_q != NULL); 4239 tcp->tcp_rq = tcps->tcps_g_q; 4240 tcp->tcp_wq = WR(tcps->tcps_g_q); 4241 CONN_DEC_REF(listener->tcp_connp); 4242 } else { 4243 mutex_exit(&listener->tcp_eager_lock); 4244 } 4245 } 4246 4247 /* Stop all the timers */ 4248 tcp_timers_stop(tcp); 4249 4250 if (tcp->tcp_state == TCPS_LISTEN) { 4251 if (tcp->tcp_ip_addr_cache) { 4252 kmem_free((void *)tcp->tcp_ip_addr_cache, 4253 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4254 tcp->tcp_ip_addr_cache = NULL; 4255 } 4256 } 4257 mutex_enter(&tcp->tcp_non_sq_lock); 4258 if (tcp->tcp_flow_stopped) 4259 tcp_clrqfull(tcp); 4260 mutex_exit(&tcp->tcp_non_sq_lock); 4261 4262 tcp_bind_hash_remove(tcp); 4263 /* 4264 * If the tcp_time_wait_collector (which runs outside the squeue) 4265 * is trying to remove this tcp from the time wait list, we will 4266 * block in tcp_time_wait_remove while trying to acquire the 4267 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4268 * requires the ipcl_hash_remove to be ordered after the 4269 * tcp_time_wait_remove for the refcnt checks to work correctly. 4270 */ 4271 if (tcp->tcp_state == TCPS_TIME_WAIT) 4272 (void) tcp_time_wait_remove(tcp, NULL); 4273 CL_INET_DISCONNECT(connp, tcp); 4274 ipcl_hash_remove(connp); 4275 4276 /* 4277 * Delete the cached ire in conn_ire_cache and also mark 4278 * the conn as CONDEMNED 4279 */ 4280 mutex_enter(&connp->conn_lock); 4281 connp->conn_state_flags |= CONN_CONDEMNED; 4282 ire = connp->conn_ire_cache; 4283 connp->conn_ire_cache = NULL; 4284 mutex_exit(&connp->conn_lock); 4285 if (ire != NULL) 4286 IRE_REFRELE_NOTR(ire); 4287 4288 /* Need to cleanup any pending ioctls */ 4289 ASSERT(tcp->tcp_time_wait_next == NULL); 4290 ASSERT(tcp->tcp_time_wait_prev == NULL); 4291 ASSERT(tcp->tcp_time_wait_expire == 0); 4292 tcp->tcp_state = TCPS_CLOSED; 4293 4294 /* Release any SSL context */ 4295 if (tcp->tcp_kssl_ent != NULL) { 4296 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4297 tcp->tcp_kssl_ent = NULL; 4298 } 4299 if (tcp->tcp_kssl_ctx != NULL) { 4300 kssl_release_ctx(tcp->tcp_kssl_ctx); 4301 tcp->tcp_kssl_ctx = NULL; 4302 } 4303 tcp->tcp_kssl_pending = B_FALSE; 4304 4305 tcp_ipsec_cleanup(tcp); 4306 } 4307 4308 /* 4309 * tcp is dying (called from ipcl_conn_destroy and error cases). 4310 * Free the tcp_t in either case. 4311 */ 4312 void 4313 tcp_free(tcp_t *tcp) 4314 { 4315 mblk_t *mp; 4316 ip6_pkt_t *ipp; 4317 4318 ASSERT(tcp != NULL); 4319 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4320 4321 tcp->tcp_rq = NULL; 4322 tcp->tcp_wq = NULL; 4323 4324 tcp_close_mpp(&tcp->tcp_xmit_head); 4325 tcp_close_mpp(&tcp->tcp_reass_head); 4326 if (tcp->tcp_rcv_list != NULL) { 4327 /* Free b_next chain */ 4328 tcp_close_mpp(&tcp->tcp_rcv_list); 4329 } 4330 if ((mp = tcp->tcp_urp_mp) != NULL) { 4331 freemsg(mp); 4332 } 4333 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4334 freemsg(mp); 4335 } 4336 4337 if (tcp->tcp_fused_sigurg_mp != NULL) { 4338 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4339 freeb(tcp->tcp_fused_sigurg_mp); 4340 tcp->tcp_fused_sigurg_mp = NULL; 4341 } 4342 4343 if (tcp->tcp_ordrel_mp != NULL) { 4344 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4345 freeb(tcp->tcp_ordrel_mp); 4346 tcp->tcp_ordrel_mp = NULL; 4347 } 4348 4349 if (tcp->tcp_sack_info != NULL) { 4350 if (tcp->tcp_notsack_list != NULL) { 4351 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4352 } 4353 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4354 } 4355 4356 if (tcp->tcp_hopopts != NULL) { 4357 mi_free(tcp->tcp_hopopts); 4358 tcp->tcp_hopopts = NULL; 4359 tcp->tcp_hopoptslen = 0; 4360 } 4361 ASSERT(tcp->tcp_hopoptslen == 0); 4362 if (tcp->tcp_dstopts != NULL) { 4363 mi_free(tcp->tcp_dstopts); 4364 tcp->tcp_dstopts = NULL; 4365 tcp->tcp_dstoptslen = 0; 4366 } 4367 ASSERT(tcp->tcp_dstoptslen == 0); 4368 if (tcp->tcp_rtdstopts != NULL) { 4369 mi_free(tcp->tcp_rtdstopts); 4370 tcp->tcp_rtdstopts = NULL; 4371 tcp->tcp_rtdstoptslen = 0; 4372 } 4373 ASSERT(tcp->tcp_rtdstoptslen == 0); 4374 if (tcp->tcp_rthdr != NULL) { 4375 mi_free(tcp->tcp_rthdr); 4376 tcp->tcp_rthdr = NULL; 4377 tcp->tcp_rthdrlen = 0; 4378 } 4379 ASSERT(tcp->tcp_rthdrlen == 0); 4380 4381 ipp = &tcp->tcp_sticky_ipp; 4382 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4383 IPPF_RTHDR)) 4384 ip6_pkt_free(ipp); 4385 4386 /* 4387 * Free memory associated with the tcp/ip header template. 4388 */ 4389 4390 if (tcp->tcp_iphc != NULL) 4391 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4392 4393 /* 4394 * Following is really a blowing away a union. 4395 * It happens to have exactly two members of identical size 4396 * the following code is enough. 4397 */ 4398 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4399 } 4400 4401 4402 /* 4403 * Put a connection confirmation message upstream built from the 4404 * address information within 'iph' and 'tcph'. Report our success or failure. 4405 */ 4406 static boolean_t 4407 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4408 mblk_t **defermp) 4409 { 4410 sin_t sin; 4411 sin6_t sin6; 4412 mblk_t *mp; 4413 char *optp = NULL; 4414 int optlen = 0; 4415 4416 if (defermp != NULL) 4417 *defermp = NULL; 4418 4419 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4420 /* 4421 * Return in T_CONN_CON results of option negotiation through 4422 * the T_CONN_REQ. Note: If there is an real end-to-end option 4423 * negotiation, then what is received from remote end needs 4424 * to be taken into account but there is no such thing (yet?) 4425 * in our TCP/IP. 4426 * Note: We do not use mi_offset_param() here as 4427 * tcp_opts_conn_req contents do not directly come from 4428 * an application and are either generated in kernel or 4429 * from user input that was already verified. 4430 */ 4431 mp = tcp->tcp_conn.tcp_opts_conn_req; 4432 optp = (char *)(mp->b_rptr + 4433 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4434 optlen = (int) 4435 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4436 } 4437 4438 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4439 ipha_t *ipha = (ipha_t *)iphdr; 4440 4441 /* packet is IPv4 */ 4442 if (tcp->tcp_family == AF_INET) { 4443 sin = sin_null; 4444 sin.sin_addr.s_addr = ipha->ipha_src; 4445 sin.sin_port = *(uint16_t *)tcph->th_lport; 4446 sin.sin_family = AF_INET; 4447 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4448 (int)sizeof (sin_t), optp, optlen); 4449 } else { 4450 sin6 = sin6_null; 4451 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4452 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4453 sin6.sin6_family = AF_INET6; 4454 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4455 (int)sizeof (sin6_t), optp, optlen); 4456 4457 } 4458 } else { 4459 ip6_t *ip6h = (ip6_t *)iphdr; 4460 4461 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4462 ASSERT(tcp->tcp_family == AF_INET6); 4463 sin6 = sin6_null; 4464 sin6.sin6_addr = ip6h->ip6_src; 4465 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4466 sin6.sin6_family = AF_INET6; 4467 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4468 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4469 (int)sizeof (sin6_t), optp, optlen); 4470 } 4471 4472 if (!mp) 4473 return (B_FALSE); 4474 4475 mblk_copycred(mp, idmp); 4476 4477 if (defermp == NULL) { 4478 conn_t *connp = tcp->tcp_connp; 4479 if (IPCL_IS_NONSTR(connp)) { 4480 cred_t *cr; 4481 pid_t cpid; 4482 4483 cr = msg_getcred(mp, &cpid); 4484 (*connp->conn_upcalls->su_connected) 4485 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4486 cpid); 4487 freemsg(mp); 4488 } else { 4489 putnext(tcp->tcp_rq, mp); 4490 } 4491 } else { 4492 *defermp = mp; 4493 } 4494 4495 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4496 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4497 return (B_TRUE); 4498 } 4499 4500 /* 4501 * Defense for the SYN attack - 4502 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4503 * one from the list of droppable eagers. This list is a subset of q0. 4504 * see comments before the definition of MAKE_DROPPABLE(). 4505 * 2. Don't drop a SYN request before its first timeout. This gives every 4506 * request at least til the first timeout to complete its 3-way handshake. 4507 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4508 * requests currently on the queue that has timed out. This will be used 4509 * as an indicator of whether an attack is under way, so that appropriate 4510 * actions can be taken. (It's incremented in tcp_timer() and decremented 4511 * either when eager goes into ESTABLISHED, or gets freed up.) 4512 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4513 * # of timeout drops back to <= q0len/32 => SYN alert off 4514 */ 4515 static boolean_t 4516 tcp_drop_q0(tcp_t *tcp) 4517 { 4518 tcp_t *eager; 4519 mblk_t *mp; 4520 tcp_stack_t *tcps = tcp->tcp_tcps; 4521 4522 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4523 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4524 4525 /* Pick oldest eager from the list of droppable eagers */ 4526 eager = tcp->tcp_eager_prev_drop_q0; 4527 4528 /* If list is empty. return B_FALSE */ 4529 if (eager == tcp) { 4530 return (B_FALSE); 4531 } 4532 4533 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4534 if ((mp = allocb(0, BPRI_HI)) == NULL) 4535 return (B_FALSE); 4536 4537 /* 4538 * Take this eager out from the list of droppable eagers since we are 4539 * going to drop it. 4540 */ 4541 MAKE_UNDROPPABLE(eager); 4542 4543 if (tcp->tcp_debug) { 4544 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4545 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4546 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4547 tcp->tcp_conn_req_cnt_q0, 4548 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4549 } 4550 4551 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4552 4553 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4554 CONN_INC_REF(eager->tcp_connp); 4555 4556 /* Mark the IRE created for this SYN request temporary */ 4557 tcp_ip_ire_mark_advice(eager); 4558 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4559 tcp_clean_death_wrapper, eager->tcp_connp, 4560 SQ_FILL, SQTAG_TCP_DROP_Q0); 4561 4562 return (B_TRUE); 4563 } 4564 4565 int 4566 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4567 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4568 { 4569 tcp_t *ltcp = lconnp->conn_tcp; 4570 tcp_t *tcp = connp->conn_tcp; 4571 mblk_t *tpi_mp; 4572 ipha_t *ipha; 4573 ip6_t *ip6h; 4574 sin6_t sin6; 4575 in6_addr_t v6dst; 4576 int err; 4577 int ifindex = 0; 4578 tcp_stack_t *tcps = tcp->tcp_tcps; 4579 4580 if (ipvers == IPV4_VERSION) { 4581 ipha = (ipha_t *)mp->b_rptr; 4582 4583 connp->conn_send = ip_output; 4584 connp->conn_recv = tcp_input; 4585 4586 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4587 &connp->conn_bound_source_v6); 4588 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4589 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4590 4591 sin6 = sin6_null; 4592 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4593 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4594 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4595 sin6.sin6_family = AF_INET6; 4596 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4597 lconnp->conn_zoneid, tcps->tcps_netstack); 4598 if (tcp->tcp_recvdstaddr) { 4599 sin6_t sin6d; 4600 4601 sin6d = sin6_null; 4602 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4603 &sin6d.sin6_addr); 4604 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4605 sin6d.sin6_family = AF_INET; 4606 tpi_mp = mi_tpi_extconn_ind(NULL, 4607 (char *)&sin6d, sizeof (sin6_t), 4608 (char *)&tcp, 4609 (t_scalar_t)sizeof (intptr_t), 4610 (char *)&sin6d, sizeof (sin6_t), 4611 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4612 } else { 4613 tpi_mp = mi_tpi_conn_ind(NULL, 4614 (char *)&sin6, sizeof (sin6_t), 4615 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4616 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4617 } 4618 } else { 4619 ip6h = (ip6_t *)mp->b_rptr; 4620 4621 connp->conn_send = ip_output_v6; 4622 connp->conn_recv = tcp_input; 4623 4624 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4625 connp->conn_srcv6 = ip6h->ip6_dst; 4626 connp->conn_remv6 = ip6h->ip6_src; 4627 4628 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4629 ifindex = (int)DB_CKSUMSTUFF(mp); 4630 DB_CKSUMSTUFF(mp) = 0; 4631 4632 sin6 = sin6_null; 4633 sin6.sin6_addr = ip6h->ip6_src; 4634 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4635 sin6.sin6_family = AF_INET6; 4636 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4637 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4638 lconnp->conn_zoneid, tcps->tcps_netstack); 4639 4640 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4641 /* Pass up the scope_id of remote addr */ 4642 sin6.sin6_scope_id = ifindex; 4643 } else { 4644 sin6.sin6_scope_id = 0; 4645 } 4646 if (tcp->tcp_recvdstaddr) { 4647 sin6_t sin6d; 4648 4649 sin6d = sin6_null; 4650 sin6.sin6_addr = ip6h->ip6_dst; 4651 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4652 sin6d.sin6_family = AF_INET; 4653 tpi_mp = mi_tpi_extconn_ind(NULL, 4654 (char *)&sin6d, sizeof (sin6_t), 4655 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4656 (char *)&sin6d, sizeof (sin6_t), 4657 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4658 } else { 4659 tpi_mp = mi_tpi_conn_ind(NULL, 4660 (char *)&sin6, sizeof (sin6_t), 4661 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4662 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4663 } 4664 } 4665 4666 if (tpi_mp == NULL) 4667 return (ENOMEM); 4668 4669 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4670 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4671 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4672 connp->conn_fully_bound = B_FALSE; 4673 4674 /* Inherit information from the "parent" */ 4675 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4676 tcp->tcp_family = ltcp->tcp_family; 4677 4678 tcp->tcp_wq = ltcp->tcp_wq; 4679 tcp->tcp_rq = ltcp->tcp_rq; 4680 4681 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4682 tcp->tcp_detached = B_TRUE; 4683 SOCK_CONNID_INIT(tcp->tcp_connid); 4684 if ((err = tcp_init_values(tcp)) != 0) { 4685 freemsg(tpi_mp); 4686 return (err); 4687 } 4688 4689 if (ipvers == IPV4_VERSION) { 4690 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4691 freemsg(tpi_mp); 4692 return (err); 4693 } 4694 ASSERT(tcp->tcp_ipha != NULL); 4695 } else { 4696 /* ifindex must be already set */ 4697 ASSERT(ifindex != 0); 4698 4699 if (ltcp->tcp_bound_if != 0) 4700 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4701 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4702 tcp->tcp_bound_if = ifindex; 4703 4704 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4705 tcp->tcp_recvifindex = 0; 4706 tcp->tcp_recvhops = 0xffffffffU; 4707 ASSERT(tcp->tcp_ip6h != NULL); 4708 } 4709 4710 tcp->tcp_lport = ltcp->tcp_lport; 4711 4712 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4713 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4714 /* 4715 * Listener had options of some sort; eager inherits. 4716 * Free up the eager template and allocate one 4717 * of the right size. 4718 */ 4719 if (tcp->tcp_hdr_grown) { 4720 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4721 } else { 4722 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4723 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4724 } 4725 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4726 KM_NOSLEEP); 4727 if (tcp->tcp_iphc == NULL) { 4728 tcp->tcp_iphc_len = 0; 4729 freemsg(tpi_mp); 4730 return (ENOMEM); 4731 } 4732 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4733 tcp->tcp_hdr_grown = B_TRUE; 4734 } 4735 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4736 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4737 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4738 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4739 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4740 4741 /* 4742 * Copy the IP+TCP header template from listener to eager 4743 */ 4744 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4745 if (tcp->tcp_ipversion == IPV6_VERSION) { 4746 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4747 IPPROTO_RAW) { 4748 tcp->tcp_ip6h = 4749 (ip6_t *)(tcp->tcp_iphc + 4750 sizeof (ip6i_t)); 4751 } else { 4752 tcp->tcp_ip6h = 4753 (ip6_t *)(tcp->tcp_iphc); 4754 } 4755 tcp->tcp_ipha = NULL; 4756 } else { 4757 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4758 tcp->tcp_ip6h = NULL; 4759 } 4760 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4761 tcp->tcp_ip_hdr_len); 4762 } else { 4763 /* 4764 * only valid case when ipversion of listener and 4765 * eager differ is when listener is IPv6 and 4766 * eager is IPv4. 4767 * Eager header template has been initialized to the 4768 * maximum v4 header sizes, which includes space for 4769 * TCP and IP options. 4770 */ 4771 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4772 (tcp->tcp_ipversion == IPV4_VERSION)); 4773 ASSERT(tcp->tcp_iphc_len >= 4774 TCP_MAX_COMBINED_HEADER_LENGTH); 4775 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4776 /* copy IP header fields individually */ 4777 tcp->tcp_ipha->ipha_ttl = 4778 ltcp->tcp_ip6h->ip6_hops; 4779 bcopy(ltcp->tcp_tcph->th_lport, 4780 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4781 } 4782 4783 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4784 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4785 sizeof (in_port_t)); 4786 4787 if (ltcp->tcp_lport == 0) { 4788 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4789 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4790 sizeof (in_port_t)); 4791 } 4792 4793 if (tcp->tcp_ipversion == IPV4_VERSION) { 4794 ASSERT(ipha != NULL); 4795 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4796 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4797 4798 /* Source routing option copyover (reverse it) */ 4799 if (tcps->tcps_rev_src_routes) 4800 tcp_opt_reverse(tcp, ipha); 4801 } else { 4802 ASSERT(ip6h != NULL); 4803 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4804 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4805 } 4806 4807 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4808 ASSERT(!tcp->tcp_tconnind_started); 4809 /* 4810 * If the SYN contains a credential, it's a loopback packet; attach 4811 * the credential to the TPI message. 4812 */ 4813 mblk_copycred(tpi_mp, idmp); 4814 4815 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4816 4817 /* Inherit the listener's SSL protection state */ 4818 4819 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4820 kssl_hold_ent(tcp->tcp_kssl_ent); 4821 tcp->tcp_kssl_pending = B_TRUE; 4822 } 4823 4824 /* Inherit the listener's non-STREAMS flag */ 4825 if (IPCL_IS_NONSTR(lconnp)) { 4826 connp->conn_flags |= IPCL_NONSTR; 4827 } 4828 4829 return (0); 4830 } 4831 4832 4833 int 4834 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4835 tcph_t *tcph, mblk_t *idmp) 4836 { 4837 tcp_t *ltcp = lconnp->conn_tcp; 4838 tcp_t *tcp = connp->conn_tcp; 4839 sin_t sin; 4840 mblk_t *tpi_mp = NULL; 4841 int err; 4842 tcp_stack_t *tcps = tcp->tcp_tcps; 4843 4844 sin = sin_null; 4845 sin.sin_addr.s_addr = ipha->ipha_src; 4846 sin.sin_port = *(uint16_t *)tcph->th_lport; 4847 sin.sin_family = AF_INET; 4848 if (ltcp->tcp_recvdstaddr) { 4849 sin_t sind; 4850 4851 sind = sin_null; 4852 sind.sin_addr.s_addr = ipha->ipha_dst; 4853 sind.sin_port = *(uint16_t *)tcph->th_fport; 4854 sind.sin_family = AF_INET; 4855 tpi_mp = mi_tpi_extconn_ind(NULL, 4856 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4857 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4858 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4859 } else { 4860 tpi_mp = mi_tpi_conn_ind(NULL, 4861 (char *)&sin, sizeof (sin_t), 4862 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4863 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4864 } 4865 4866 if (tpi_mp == NULL) { 4867 return (ENOMEM); 4868 } 4869 4870 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4871 connp->conn_send = ip_output; 4872 connp->conn_recv = tcp_input; 4873 connp->conn_fully_bound = B_FALSE; 4874 4875 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4876 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4877 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4878 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4879 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4880 4881 /* Inherit information from the "parent" */ 4882 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4883 tcp->tcp_family = ltcp->tcp_family; 4884 tcp->tcp_wq = ltcp->tcp_wq; 4885 tcp->tcp_rq = ltcp->tcp_rq; 4886 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4887 tcp->tcp_detached = B_TRUE; 4888 SOCK_CONNID_INIT(tcp->tcp_connid); 4889 if ((err = tcp_init_values(tcp)) != 0) { 4890 freemsg(tpi_mp); 4891 return (err); 4892 } 4893 4894 /* 4895 * Let's make sure that eager tcp template has enough space to 4896 * copy IPv4 listener's tcp template. Since the conn_t structure is 4897 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4898 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4899 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4900 * extension headers or with ip6i_t struct). Note that bcopy() below 4901 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4902 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4903 */ 4904 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4905 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4906 4907 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4908 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4909 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4910 tcp->tcp_ttl = ltcp->tcp_ttl; 4911 tcp->tcp_tos = ltcp->tcp_tos; 4912 4913 /* Copy the IP+TCP header template from listener to eager */ 4914 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4915 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4916 tcp->tcp_ip6h = NULL; 4917 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4918 tcp->tcp_ip_hdr_len); 4919 4920 /* Initialize the IP addresses and Ports */ 4921 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4922 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4923 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4924 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4925 4926 /* Source routing option copyover (reverse it) */ 4927 if (tcps->tcps_rev_src_routes) 4928 tcp_opt_reverse(tcp, ipha); 4929 4930 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4931 ASSERT(!tcp->tcp_tconnind_started); 4932 4933 /* 4934 * If the SYN contains a credential, it's a loopback packet; attach 4935 * the credential to the TPI message. 4936 */ 4937 mblk_copycred(tpi_mp, idmp); 4938 4939 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4940 4941 /* Inherit the listener's SSL protection state */ 4942 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4943 kssl_hold_ent(tcp->tcp_kssl_ent); 4944 tcp->tcp_kssl_pending = B_TRUE; 4945 } 4946 4947 /* Inherit the listener's non-STREAMS flag */ 4948 if (IPCL_IS_NONSTR(lconnp)) { 4949 connp->conn_flags |= IPCL_NONSTR; 4950 } 4951 4952 return (0); 4953 } 4954 4955 /* 4956 * sets up conn for ipsec. 4957 * if the first mblk is M_CTL it is consumed and mpp is updated. 4958 * in case of error mpp is freed. 4959 */ 4960 conn_t * 4961 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4962 { 4963 conn_t *connp = tcp->tcp_connp; 4964 conn_t *econnp; 4965 squeue_t *new_sqp; 4966 mblk_t *first_mp = *mpp; 4967 mblk_t *mp = *mpp; 4968 boolean_t mctl_present = B_FALSE; 4969 uint_t ipvers; 4970 4971 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4972 if (econnp == NULL) { 4973 freemsg(first_mp); 4974 return (NULL); 4975 } 4976 if (DB_TYPE(mp) == M_CTL) { 4977 if (mp->b_cont == NULL || 4978 mp->b_cont->b_datap->db_type != M_DATA) { 4979 freemsg(first_mp); 4980 return (NULL); 4981 } 4982 mp = mp->b_cont; 4983 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4984 freemsg(first_mp); 4985 return (NULL); 4986 } 4987 4988 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4989 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4990 mctl_present = B_TRUE; 4991 } else { 4992 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4993 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4994 } 4995 4996 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4997 DB_CKSUMSTART(mp) = 0; 4998 4999 ASSERT(OK_32PTR(mp->b_rptr)); 5000 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5001 if (ipvers == IPV4_VERSION) { 5002 uint16_t *up; 5003 uint32_t ports; 5004 ipha_t *ipha; 5005 5006 ipha = (ipha_t *)mp->b_rptr; 5007 up = (uint16_t *)((uchar_t *)ipha + 5008 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5009 ports = *(uint32_t *)up; 5010 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5011 ipha->ipha_dst, ipha->ipha_src, ports); 5012 } else { 5013 uint16_t *up; 5014 uint32_t ports; 5015 uint16_t ip_hdr_len; 5016 uint8_t *nexthdrp; 5017 ip6_t *ip6h; 5018 tcph_t *tcph; 5019 5020 ip6h = (ip6_t *)mp->b_rptr; 5021 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5022 ip_hdr_len = IPV6_HDR_LEN; 5023 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5024 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5025 CONN_DEC_REF(econnp); 5026 freemsg(first_mp); 5027 return (NULL); 5028 } 5029 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5030 up = (uint16_t *)tcph->th_lport; 5031 ports = *(uint32_t *)up; 5032 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5033 ip6h->ip6_dst, ip6h->ip6_src, ports); 5034 } 5035 5036 /* 5037 * The caller already ensured that there is a sqp present. 5038 */ 5039 econnp->conn_sqp = new_sqp; 5040 econnp->conn_initial_sqp = new_sqp; 5041 5042 if (connp->conn_policy != NULL) { 5043 ipsec_in_t *ii; 5044 ii = (ipsec_in_t *)(first_mp->b_rptr); 5045 ASSERT(ii->ipsec_in_policy == NULL); 5046 IPPH_REFHOLD(connp->conn_policy); 5047 ii->ipsec_in_policy = connp->conn_policy; 5048 5049 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5050 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5051 CONN_DEC_REF(econnp); 5052 freemsg(first_mp); 5053 return (NULL); 5054 } 5055 } 5056 5057 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5058 CONN_DEC_REF(econnp); 5059 freemsg(first_mp); 5060 return (NULL); 5061 } 5062 5063 /* 5064 * If we know we have some policy, pass the "IPSEC" 5065 * options size TCP uses this adjust the MSS. 5066 */ 5067 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5068 if (mctl_present) { 5069 freeb(first_mp); 5070 *mpp = mp; 5071 } 5072 5073 return (econnp); 5074 } 5075 5076 /* 5077 * tcp_get_conn/tcp_free_conn 5078 * 5079 * tcp_get_conn is used to get a clean tcp connection structure. 5080 * It tries to reuse the connections put on the freelist by the 5081 * time_wait_collector failing which it goes to kmem_cache. This 5082 * way has two benefits compared to just allocating from and 5083 * freeing to kmem_cache. 5084 * 1) The time_wait_collector can free (which includes the cleanup) 5085 * outside the squeue. So when the interrupt comes, we have a clean 5086 * connection sitting in the freelist. Obviously, this buys us 5087 * performance. 5088 * 5089 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5090 * has multiple disadvantages - tying up the squeue during alloc, and the 5091 * fact that IPSec policy initialization has to happen here which 5092 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5093 * But allocating the conn/tcp in IP land is also not the best since 5094 * we can't check the 'q' and 'q0' which are protected by squeue and 5095 * blindly allocate memory which might have to be freed here if we are 5096 * not allowed to accept the connection. By using the freelist and 5097 * putting the conn/tcp back in freelist, we don't pay a penalty for 5098 * allocating memory without checking 'q/q0' and freeing it if we can't 5099 * accept the connection. 5100 * 5101 * Care should be taken to put the conn back in the same squeue's freelist 5102 * from which it was allocated. Best results are obtained if conn is 5103 * allocated from listener's squeue and freed to the same. Time wait 5104 * collector will free up the freelist is the connection ends up sitting 5105 * there for too long. 5106 */ 5107 void * 5108 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5109 { 5110 tcp_t *tcp = NULL; 5111 conn_t *connp = NULL; 5112 squeue_t *sqp = (squeue_t *)arg; 5113 tcp_squeue_priv_t *tcp_time_wait; 5114 netstack_t *ns; 5115 5116 tcp_time_wait = 5117 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5118 5119 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5120 tcp = tcp_time_wait->tcp_free_list; 5121 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5122 if (tcp != NULL) { 5123 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5124 tcp_time_wait->tcp_free_list_cnt--; 5125 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5126 tcp->tcp_time_wait_next = NULL; 5127 connp = tcp->tcp_connp; 5128 connp->conn_flags |= IPCL_REUSED; 5129 5130 ASSERT(tcp->tcp_tcps == NULL); 5131 ASSERT(connp->conn_netstack == NULL); 5132 ASSERT(tcp->tcp_rsrv_mp != NULL); 5133 ns = tcps->tcps_netstack; 5134 netstack_hold(ns); 5135 connp->conn_netstack = ns; 5136 tcp->tcp_tcps = tcps; 5137 TCPS_REFHOLD(tcps); 5138 ipcl_globalhash_insert(connp); 5139 return ((void *)connp); 5140 } 5141 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5142 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5143 tcps->tcps_netstack)) == NULL) 5144 return (NULL); 5145 tcp = connp->conn_tcp; 5146 /* 5147 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5148 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5149 */ 5150 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5151 ipcl_conn_destroy(connp); 5152 return (NULL); 5153 } 5154 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5155 tcp->tcp_tcps = tcps; 5156 TCPS_REFHOLD(tcps); 5157 5158 return ((void *)connp); 5159 } 5160 5161 /* 5162 * Update the cached label for the given tcp_t. This should be called once per 5163 * connection, and before any packets are sent or tcp_process_options is 5164 * invoked. Returns B_FALSE if the correct label could not be constructed. 5165 */ 5166 static boolean_t 5167 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5168 { 5169 conn_t *connp = tcp->tcp_connp; 5170 5171 if (tcp->tcp_ipversion == IPV4_VERSION) { 5172 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5173 int added; 5174 5175 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5176 connp->conn_mac_exempt, 5177 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5178 return (B_FALSE); 5179 5180 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5181 if (added == -1) 5182 return (B_FALSE); 5183 tcp->tcp_hdr_len += added; 5184 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5185 tcp->tcp_ip_hdr_len += added; 5186 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5187 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5188 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5189 tcp->tcp_hdr_len); 5190 if (added == -1) 5191 return (B_FALSE); 5192 tcp->tcp_hdr_len += added; 5193 tcp->tcp_tcph = (tcph_t *) 5194 ((uchar_t *)tcp->tcp_tcph + added); 5195 tcp->tcp_ip_hdr_len += added; 5196 } 5197 } else { 5198 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5199 5200 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5201 connp->conn_mac_exempt, 5202 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5203 return (B_FALSE); 5204 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5205 &tcp->tcp_label_len, optbuf) != 0) 5206 return (B_FALSE); 5207 if (tcp_build_hdrs(tcp) != 0) 5208 return (B_FALSE); 5209 } 5210 5211 connp->conn_ulp_labeled = 1; 5212 5213 return (B_TRUE); 5214 } 5215 5216 /* BEGIN CSTYLED */ 5217 /* 5218 * 5219 * The sockfs ACCEPT path: 5220 * ======================= 5221 * 5222 * The eager is now established in its own perimeter as soon as SYN is 5223 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5224 * completes the accept processing on the acceptor STREAM. The sending 5225 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5226 * listener but a TLI/XTI listener completes the accept processing 5227 * on the listener perimeter. 5228 * 5229 * Common control flow for 3 way handshake: 5230 * ---------------------------------------- 5231 * 5232 * incoming SYN (listener perimeter) -> tcp_rput_data() 5233 * -> tcp_conn_request() 5234 * 5235 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5236 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5237 * 5238 * Sockfs ACCEPT Path: 5239 * ------------------- 5240 * 5241 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5242 * as STREAM entry point) 5243 * 5244 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5245 * 5246 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5247 * association (we are not behind eager's squeue but sockfs is protecting us 5248 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5249 * is changed to point at tcp_wput(). 5250 * 5251 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5252 * listener (done on listener's perimeter). 5253 * 5254 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5255 * accept. 5256 * 5257 * TLI/XTI client ACCEPT path: 5258 * --------------------------- 5259 * 5260 * soaccept() sends T_CONN_RES on the listener STREAM. 5261 * 5262 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5263 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5264 * 5265 * Locks: 5266 * ====== 5267 * 5268 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5269 * and listeners->tcp_eager_next_q. 5270 * 5271 * Referencing: 5272 * ============ 5273 * 5274 * 1) We start out in tcp_conn_request by eager placing a ref on 5275 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5276 * 5277 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5278 * doing so we place a ref on the eager. This ref is finally dropped at the 5279 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5280 * reference is dropped by the squeue framework. 5281 * 5282 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5283 * 5284 * The reference must be released by the same entity that added the reference 5285 * In the above scheme, the eager is the entity that adds and releases the 5286 * references. Note that tcp_accept_finish executes in the squeue of the eager 5287 * (albeit after it is attached to the acceptor stream). Though 1. executes 5288 * in the listener's squeue, the eager is nascent at this point and the 5289 * reference can be considered to have been added on behalf of the eager. 5290 * 5291 * Eager getting a Reset or listener closing: 5292 * ========================================== 5293 * 5294 * Once the listener and eager are linked, the listener never does the unlink. 5295 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5296 * a message on all eager perimeter. The eager then does the unlink, clears 5297 * any pointers to the listener's queue and drops the reference to the 5298 * listener. The listener waits in tcp_close outside the squeue until its 5299 * refcount has dropped to 1. This ensures that the listener has waited for 5300 * all eagers to clear their association with the listener. 5301 * 5302 * Similarly, if eager decides to go away, it can unlink itself and close. 5303 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5304 * the reference to eager is still valid because of the extra ref we put 5305 * in tcp_send_conn_ind. 5306 * 5307 * Listener can always locate the eager under the protection 5308 * of the listener->tcp_eager_lock, and then do a refhold 5309 * on the eager during the accept processing. 5310 * 5311 * The acceptor stream accesses the eager in the accept processing 5312 * based on the ref placed on eager before sending T_conn_ind. 5313 * The only entity that can negate this refhold is a listener close 5314 * which is mutually exclusive with an active acceptor stream. 5315 * 5316 * Eager's reference on the listener 5317 * =================================== 5318 * 5319 * If the accept happens (even on a closed eager) the eager drops its 5320 * reference on the listener at the start of tcp_accept_finish. If the 5321 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5322 * the reference is dropped in tcp_closei_local. If the listener closes, 5323 * the reference is dropped in tcp_eager_kill. In all cases the reference 5324 * is dropped while executing in the eager's context (squeue). 5325 */ 5326 /* END CSTYLED */ 5327 5328 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5329 5330 /* 5331 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5332 * tcp_rput_data will not see any SYN packets. 5333 */ 5334 /* ARGSUSED */ 5335 void 5336 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5337 { 5338 tcph_t *tcph; 5339 uint32_t seg_seq; 5340 tcp_t *eager; 5341 uint_t ipvers; 5342 ipha_t *ipha; 5343 ip6_t *ip6h; 5344 int err; 5345 conn_t *econnp = NULL; 5346 squeue_t *new_sqp; 5347 mblk_t *mp1; 5348 uint_t ip_hdr_len; 5349 conn_t *connp = (conn_t *)arg; 5350 tcp_t *tcp = connp->conn_tcp; 5351 cred_t *credp; 5352 tcp_stack_t *tcps = tcp->tcp_tcps; 5353 ip_stack_t *ipst; 5354 5355 if (tcp->tcp_state != TCPS_LISTEN) 5356 goto error2; 5357 5358 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5359 5360 mutex_enter(&tcp->tcp_eager_lock); 5361 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5362 mutex_exit(&tcp->tcp_eager_lock); 5363 TCP_STAT(tcps, tcp_listendrop); 5364 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5365 if (tcp->tcp_debug) { 5366 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5367 "tcp_conn_request: listen backlog (max=%d) " 5368 "overflow (%d pending) on %s", 5369 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5370 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5371 } 5372 goto error2; 5373 } 5374 5375 if (tcp->tcp_conn_req_cnt_q0 >= 5376 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5377 /* 5378 * Q0 is full. Drop a pending half-open req from the queue 5379 * to make room for the new SYN req. Also mark the time we 5380 * drop a SYN. 5381 * 5382 * A more aggressive defense against SYN attack will 5383 * be to set the "tcp_syn_defense" flag now. 5384 */ 5385 TCP_STAT(tcps, tcp_listendropq0); 5386 tcp->tcp_last_rcv_lbolt = lbolt64; 5387 if (!tcp_drop_q0(tcp)) { 5388 mutex_exit(&tcp->tcp_eager_lock); 5389 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5390 if (tcp->tcp_debug) { 5391 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5392 "tcp_conn_request: listen half-open queue " 5393 "(max=%d) full (%d pending) on %s", 5394 tcps->tcps_conn_req_max_q0, 5395 tcp->tcp_conn_req_cnt_q0, 5396 tcp_display(tcp, NULL, 5397 DISP_PORT_ONLY)); 5398 } 5399 goto error2; 5400 } 5401 } 5402 mutex_exit(&tcp->tcp_eager_lock); 5403 5404 /* 5405 * IP adds STRUIO_EAGER and ensures that the received packet is 5406 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5407 * link local address. If IPSec is enabled, db_struioflag has 5408 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5409 * otherwise an error case if neither of them is set. 5410 */ 5411 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5412 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5413 DB_CKSUMSTART(mp) = 0; 5414 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5415 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5416 if (econnp == NULL) 5417 goto error2; 5418 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5419 econnp->conn_sqp = new_sqp; 5420 econnp->conn_initial_sqp = new_sqp; 5421 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5422 /* 5423 * mp is updated in tcp_get_ipsec_conn(). 5424 */ 5425 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5426 if (econnp == NULL) { 5427 /* 5428 * mp freed by tcp_get_ipsec_conn. 5429 */ 5430 return; 5431 } 5432 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5433 } else { 5434 goto error2; 5435 } 5436 5437 ASSERT(DB_TYPE(mp) == M_DATA); 5438 5439 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5440 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5441 ASSERT(OK_32PTR(mp->b_rptr)); 5442 if (ipvers == IPV4_VERSION) { 5443 ipha = (ipha_t *)mp->b_rptr; 5444 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5445 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5446 } else { 5447 ip6h = (ip6_t *)mp->b_rptr; 5448 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5449 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5450 } 5451 5452 if (tcp->tcp_family == AF_INET) { 5453 ASSERT(ipvers == IPV4_VERSION); 5454 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5455 } else { 5456 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5457 } 5458 5459 if (err) 5460 goto error3; 5461 5462 eager = econnp->conn_tcp; 5463 5464 /* 5465 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that at close 5466 * time, we will always have that to send up. Otherwise, we need to do 5467 * special handling in case the allocation fails at that time. 5468 */ 5469 ASSERT(eager->tcp_ordrel_mp == NULL); 5470 if (!IPCL_IS_NONSTR(econnp) && 5471 (eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5472 goto error3; 5473 5474 /* Inherit various TCP parameters from the listener */ 5475 eager->tcp_naglim = tcp->tcp_naglim; 5476 eager->tcp_first_timer_threshold = 5477 tcp->tcp_first_timer_threshold; 5478 eager->tcp_second_timer_threshold = 5479 tcp->tcp_second_timer_threshold; 5480 5481 eager->tcp_first_ctimer_threshold = 5482 tcp->tcp_first_ctimer_threshold; 5483 eager->tcp_second_ctimer_threshold = 5484 tcp->tcp_second_ctimer_threshold; 5485 5486 /* 5487 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5488 * If it does not, the eager's receive window will be set to the 5489 * listener's receive window later in this function. 5490 */ 5491 eager->tcp_rwnd = 0; 5492 5493 /* 5494 * Inherit listener's tcp_init_cwnd. Need to do this before 5495 * calling tcp_process_options() where tcp_mss_set() is called 5496 * to set the initial cwnd. 5497 */ 5498 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5499 5500 /* 5501 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5502 * zone id before the accept is completed in tcp_wput_accept(). 5503 */ 5504 econnp->conn_zoneid = connp->conn_zoneid; 5505 econnp->conn_allzones = connp->conn_allzones; 5506 5507 /* Copy nexthop information from listener to eager */ 5508 if (connp->conn_nexthop_set) { 5509 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5510 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5511 } 5512 5513 /* 5514 * TSOL: tsol_input_proc() needs the eager's cred before the 5515 * eager is accepted 5516 */ 5517 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5518 crhold(credp); 5519 5520 /* 5521 * If the caller has the process-wide flag set, then default to MAC 5522 * exempt mode. This allows read-down to unlabeled hosts. 5523 */ 5524 if (getpflags(NET_MAC_AWARE, credp) != 0) 5525 econnp->conn_mac_exempt = B_TRUE; 5526 5527 if (is_system_labeled()) { 5528 cred_t *cr; 5529 5530 if (connp->conn_mlp_type != mlptSingle) { 5531 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5532 if (cr != NULL) 5533 crhold(cr); 5534 else 5535 cr = econnp->conn_cred; 5536 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5537 econnp, cred_t *, cr) 5538 } else { 5539 cr = econnp->conn_cred; 5540 DTRACE_PROBE2(syn_accept, conn_t *, 5541 econnp, cred_t *, cr) 5542 } 5543 5544 if (!tcp_update_label(eager, cr)) { 5545 DTRACE_PROBE3( 5546 tx__ip__log__error__connrequest__tcp, 5547 char *, "eager connp(1) label on SYN mp(2) failed", 5548 conn_t *, econnp, mblk_t *, mp); 5549 goto error3; 5550 } 5551 } 5552 5553 eager->tcp_hard_binding = B_TRUE; 5554 5555 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5556 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5557 5558 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5559 if (err != 0) { 5560 tcp_bind_hash_remove(eager); 5561 goto error3; 5562 } 5563 5564 /* 5565 * No need to check for multicast destination since ip will only pass 5566 * up multicasts to those that have expressed interest 5567 * TODO: what about rejecting broadcasts? 5568 * Also check that source is not a multicast or broadcast address. 5569 */ 5570 eager->tcp_state = TCPS_SYN_RCVD; 5571 5572 5573 /* 5574 * There should be no ire in the mp as we are being called after 5575 * receiving the SYN. 5576 */ 5577 ASSERT(tcp_ire_mp(&mp) == NULL); 5578 5579 /* 5580 * Adapt our mss, ttl, ... according to information provided in IRE. 5581 */ 5582 5583 if (tcp_adapt_ire(eager, NULL) == 0) { 5584 /* Undo the bind_hash_insert */ 5585 tcp_bind_hash_remove(eager); 5586 goto error3; 5587 } 5588 5589 /* Process all TCP options. */ 5590 tcp_process_options(eager, tcph); 5591 5592 /* Is the other end ECN capable? */ 5593 if (tcps->tcps_ecn_permitted >= 1 && 5594 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5595 eager->tcp_ecn_ok = B_TRUE; 5596 } 5597 5598 /* 5599 * listener->tcp_rq->q_hiwat should be the default window size or a 5600 * window size changed via SO_RCVBUF option. First round up the 5601 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5602 * scale option value if needed. Call tcp_rwnd_set() to finish the 5603 * setting. 5604 * 5605 * Note if there is a rpipe metric associated with the remote host, 5606 * we should not inherit receive window size from listener. 5607 */ 5608 eager->tcp_rwnd = MSS_ROUNDUP( 5609 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5610 eager->tcp_rwnd), eager->tcp_mss); 5611 if (eager->tcp_snd_ws_ok) 5612 tcp_set_ws_value(eager); 5613 /* 5614 * Note that this is the only place tcp_rwnd_set() is called for 5615 * accepting a connection. We need to call it here instead of 5616 * after the 3-way handshake because we need to tell the other 5617 * side our rwnd in the SYN-ACK segment. 5618 */ 5619 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5620 5621 /* 5622 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5623 * via soaccept()->soinheritoptions() which essentially applies 5624 * all the listener options to the new STREAM. The options that we 5625 * need to take care of are: 5626 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5627 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5628 * SO_SNDBUF, SO_RCVBUF. 5629 * 5630 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5631 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5632 * tcp_maxpsz_set() gets called later from 5633 * tcp_accept_finish(), the option takes effect. 5634 * 5635 */ 5636 /* Set the TCP options */ 5637 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5638 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5639 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5640 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5641 eager->tcp_oobinline = tcp->tcp_oobinline; 5642 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5643 eager->tcp_broadcast = tcp->tcp_broadcast; 5644 eager->tcp_useloopback = tcp->tcp_useloopback; 5645 eager->tcp_dontroute = tcp->tcp_dontroute; 5646 eager->tcp_debug = tcp->tcp_debug; 5647 eager->tcp_linger = tcp->tcp_linger; 5648 eager->tcp_lingertime = tcp->tcp_lingertime; 5649 if (tcp->tcp_ka_enabled) 5650 eager->tcp_ka_enabled = 1; 5651 5652 /* Set the IP options */ 5653 econnp->conn_broadcast = connp->conn_broadcast; 5654 econnp->conn_loopback = connp->conn_loopback; 5655 econnp->conn_dontroute = connp->conn_dontroute; 5656 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5657 5658 /* Put a ref on the listener for the eager. */ 5659 CONN_INC_REF(connp); 5660 mutex_enter(&tcp->tcp_eager_lock); 5661 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5662 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5663 tcp->tcp_eager_next_q0 = eager; 5664 eager->tcp_eager_prev_q0 = tcp; 5665 5666 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5667 eager->tcp_listener = tcp; 5668 eager->tcp_saved_listener = tcp; 5669 5670 /* 5671 * Tag this detached tcp vector for later retrieval 5672 * by our listener client in tcp_accept(). 5673 */ 5674 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5675 tcp->tcp_conn_req_cnt_q0++; 5676 if (++tcp->tcp_conn_req_seqnum == -1) { 5677 /* 5678 * -1 is "special" and defined in TPI as something 5679 * that should never be used in T_CONN_IND 5680 */ 5681 ++tcp->tcp_conn_req_seqnum; 5682 } 5683 mutex_exit(&tcp->tcp_eager_lock); 5684 5685 if (tcp->tcp_syn_defense) { 5686 /* Don't drop the SYN that comes from a good IP source */ 5687 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5688 if (addr_cache != NULL && eager->tcp_remote == 5689 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5690 eager->tcp_dontdrop = B_TRUE; 5691 } 5692 } 5693 5694 /* 5695 * We need to insert the eager in its own perimeter but as soon 5696 * as we do that, we expose the eager to the classifier and 5697 * should not touch any field outside the eager's perimeter. 5698 * So do all the work necessary before inserting the eager 5699 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5700 * will succeed but undo everything if it fails. 5701 */ 5702 seg_seq = ABE32_TO_U32(tcph->th_seq); 5703 eager->tcp_irs = seg_seq; 5704 eager->tcp_rack = seg_seq; 5705 eager->tcp_rnxt = seg_seq + 1; 5706 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5707 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5708 eager->tcp_state = TCPS_SYN_RCVD; 5709 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5710 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5711 if (mp1 == NULL) { 5712 /* 5713 * Increment the ref count as we are going to 5714 * enqueueing an mp in squeue 5715 */ 5716 CONN_INC_REF(econnp); 5717 goto error; 5718 } 5719 5720 /* 5721 * Note that in theory this should use the current pid 5722 * so that getpeerucred on the client returns the actual listener 5723 * that does accept. But accept() hasn't been called yet. We could use 5724 * the pid of the process that did bind/listen on the server. 5725 * However, with common usage like inetd() the bind/listen can be done 5726 * by a different process than the accept(). 5727 * Hence we do the simple thing of using the open pid here. 5728 * Note that db_credp is set later in tcp_send_data(). 5729 */ 5730 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5731 eager->tcp_cpid = tcp->tcp_cpid; 5732 eager->tcp_open_time = lbolt64; 5733 5734 /* 5735 * We need to start the rto timer. In normal case, we start 5736 * the timer after sending the packet on the wire (or at 5737 * least believing that packet was sent by waiting for 5738 * CALL_IP_WPUT() to return). Since this is the first packet 5739 * being sent on the wire for the eager, our initial tcp_rto 5740 * is at least tcp_rexmit_interval_min which is a fairly 5741 * large value to allow the algorithm to adjust slowly to large 5742 * fluctuations of RTT during first few transmissions. 5743 * 5744 * Starting the timer first and then sending the packet in this 5745 * case shouldn't make much difference since tcp_rexmit_interval_min 5746 * is of the order of several 100ms and starting the timer 5747 * first and then sending the packet will result in difference 5748 * of few micro seconds. 5749 * 5750 * Without this optimization, we are forced to hold the fanout 5751 * lock across the ipcl_bind_insert() and sending the packet 5752 * so that we don't race against an incoming packet (maybe RST) 5753 * for this eager. 5754 * 5755 * It is necessary to acquire an extra reference on the eager 5756 * at this point and hold it until after tcp_send_data() to 5757 * ensure against an eager close race. 5758 */ 5759 5760 CONN_INC_REF(eager->tcp_connp); 5761 5762 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5763 5764 /* 5765 * Insert the eager in its own perimeter now. We are ready to deal 5766 * with any packets on eager. 5767 */ 5768 if (eager->tcp_ipversion == IPV4_VERSION) { 5769 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5770 goto error; 5771 } 5772 } else { 5773 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5774 goto error; 5775 } 5776 } 5777 5778 /* mark conn as fully-bound */ 5779 econnp->conn_fully_bound = B_TRUE; 5780 5781 /* Send the SYN-ACK */ 5782 tcp_send_data(eager, eager->tcp_wq, mp1); 5783 CONN_DEC_REF(eager->tcp_connp); 5784 freemsg(mp); 5785 5786 return; 5787 error: 5788 freemsg(mp1); 5789 eager->tcp_closemp_used = B_TRUE; 5790 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5791 mp1 = &eager->tcp_closemp; 5792 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5793 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5794 5795 /* 5796 * If a connection already exists, send the mp to that connections so 5797 * that it can be appropriately dealt with. 5798 */ 5799 ipst = tcps->tcps_netstack->netstack_ip; 5800 5801 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5802 if (!IPCL_IS_CONNECTED(econnp)) { 5803 /* 5804 * Something bad happened. ipcl_conn_insert() 5805 * failed because a connection already existed 5806 * in connected hash but we can't find it 5807 * anymore (someone blew it away). Just 5808 * free this message and hopefully remote 5809 * will retransmit at which time the SYN can be 5810 * treated as a new connection or dealth with 5811 * a TH_RST if a connection already exists. 5812 */ 5813 CONN_DEC_REF(econnp); 5814 freemsg(mp); 5815 } else { 5816 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5817 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5818 } 5819 } else { 5820 /* Nobody wants this packet */ 5821 freemsg(mp); 5822 } 5823 return; 5824 error3: 5825 CONN_DEC_REF(econnp); 5826 error2: 5827 freemsg(mp); 5828 } 5829 5830 /* 5831 * In an ideal case of vertical partition in NUMA architecture, its 5832 * beneficial to have the listener and all the incoming connections 5833 * tied to the same squeue. The other constraint is that incoming 5834 * connections should be tied to the squeue attached to interrupted 5835 * CPU for obvious locality reason so this leaves the listener to 5836 * be tied to the same squeue. Our only problem is that when listener 5837 * is binding, the CPU that will get interrupted by the NIC whose 5838 * IP address the listener is binding to is not even known. So 5839 * the code below allows us to change that binding at the time the 5840 * CPU is interrupted by virtue of incoming connection's squeue. 5841 * 5842 * This is usefull only in case of a listener bound to a specific IP 5843 * address. For other kind of listeners, they get bound the 5844 * very first time and there is no attempt to rebind them. 5845 */ 5846 void 5847 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5848 { 5849 conn_t *connp = (conn_t *)arg; 5850 squeue_t *sqp = (squeue_t *)arg2; 5851 squeue_t *new_sqp; 5852 uint32_t conn_flags; 5853 5854 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5855 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5856 } else { 5857 goto done; 5858 } 5859 5860 if (connp->conn_fanout == NULL) 5861 goto done; 5862 5863 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5864 mutex_enter(&connp->conn_fanout->connf_lock); 5865 mutex_enter(&connp->conn_lock); 5866 /* 5867 * No one from read or write side can access us now 5868 * except for already queued packets on this squeue. 5869 * But since we haven't changed the squeue yet, they 5870 * can't execute. If they are processed after we have 5871 * changed the squeue, they are sent back to the 5872 * correct squeue down below. 5873 * But a listner close can race with processing of 5874 * incoming SYN. If incoming SYN processing changes 5875 * the squeue then the listener close which is waiting 5876 * to enter the squeue would operate on the wrong 5877 * squeue. Hence we don't change the squeue here unless 5878 * the refcount is exactly the minimum refcount. The 5879 * minimum refcount of 4 is counted as - 1 each for 5880 * TCP and IP, 1 for being in the classifier hash, and 5881 * 1 for the mblk being processed. 5882 */ 5883 5884 if (connp->conn_ref != 4 || 5885 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5886 mutex_exit(&connp->conn_lock); 5887 mutex_exit(&connp->conn_fanout->connf_lock); 5888 goto done; 5889 } 5890 if (connp->conn_sqp != new_sqp) { 5891 while (connp->conn_sqp != new_sqp) 5892 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5893 } 5894 5895 do { 5896 conn_flags = connp->conn_flags; 5897 conn_flags |= IPCL_FULLY_BOUND; 5898 (void) cas32(&connp->conn_flags, connp->conn_flags, 5899 conn_flags); 5900 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5901 5902 mutex_exit(&connp->conn_fanout->connf_lock); 5903 mutex_exit(&connp->conn_lock); 5904 } 5905 5906 done: 5907 if (connp->conn_sqp != sqp) { 5908 CONN_INC_REF(connp); 5909 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5910 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5911 } else { 5912 tcp_conn_request(connp, mp, sqp); 5913 } 5914 } 5915 5916 /* 5917 * Successful connect request processing begins when our client passes 5918 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5919 * our T_OK_ACK reply message upstream. The control flow looks like this: 5920 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5921 * upstream <- tcp_rput() <- IP 5922 * After various error checks are completed, tcp_tpi_connect() lays 5923 * the target address and port into the composite header template, 5924 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5925 * request followed by an IRE request, and passes the three mblk message 5926 * down to IP looking like this: 5927 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5928 * Processing continues in tcp_rput() when we receive the following message: 5929 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5930 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5931 * to fire off the connection request, and then passes the T_OK_ACK mblk 5932 * upstream that we filled in below. There are, of course, numerous 5933 * error conditions along the way which truncate the processing described 5934 * above. 5935 */ 5936 static void 5937 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5938 { 5939 sin_t *sin; 5940 queue_t *q = tcp->tcp_wq; 5941 struct T_conn_req *tcr; 5942 struct sockaddr *sa; 5943 socklen_t len; 5944 int error; 5945 cred_t *cr; 5946 pid_t cpid; 5947 5948 /* 5949 * All Solaris components should pass a db_credp 5950 * for this TPI message, hence we ASSERT. 5951 * But in case there is some other M_PROTO that looks 5952 * like a TPI message sent by some other kernel 5953 * component, we check and return an error. 5954 */ 5955 cr = msg_getcred(mp, &cpid); 5956 ASSERT(cr != NULL); 5957 if (cr == NULL) { 5958 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5959 return; 5960 } 5961 5962 tcr = (struct T_conn_req *)mp->b_rptr; 5963 5964 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5965 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5966 tcp_err_ack(tcp, mp, TPROTO, 0); 5967 return; 5968 } 5969 5970 /* 5971 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5972 * will always have that to send up. Otherwise, we need to do 5973 * special handling in case the allocation fails at that time. 5974 * If the end point is TPI, the tcp_t can be reused and the 5975 * tcp_ordrel_mp may be allocated already. 5976 */ 5977 if (tcp->tcp_ordrel_mp == NULL) { 5978 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5979 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5980 return; 5981 } 5982 } 5983 5984 /* 5985 * Determine packet type based on type of address passed in 5986 * the request should contain an IPv4 or IPv6 address. 5987 * Make sure that address family matches the type of 5988 * family of the the address passed down 5989 */ 5990 switch (tcr->DEST_length) { 5991 default: 5992 tcp_err_ack(tcp, mp, TBADADDR, 0); 5993 return; 5994 5995 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5996 /* 5997 * XXX: The check for valid DEST_length was not there 5998 * in earlier releases and some buggy 5999 * TLI apps (e.g Sybase) got away with not feeding 6000 * in sin_zero part of address. 6001 * We allow that bug to keep those buggy apps humming. 6002 * Test suites require the check on DEST_length. 6003 * We construct a new mblk with valid DEST_length 6004 * free the original so the rest of the code does 6005 * not have to keep track of this special shorter 6006 * length address case. 6007 */ 6008 mblk_t *nmp; 6009 struct T_conn_req *ntcr; 6010 sin_t *nsin; 6011 6012 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6013 tcr->OPT_length, BPRI_HI); 6014 if (nmp == NULL) { 6015 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6016 return; 6017 } 6018 ntcr = (struct T_conn_req *)nmp->b_rptr; 6019 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6020 ntcr->PRIM_type = T_CONN_REQ; 6021 ntcr->DEST_length = sizeof (sin_t); 6022 ntcr->DEST_offset = sizeof (struct T_conn_req); 6023 6024 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6025 *nsin = sin_null; 6026 /* Get pointer to shorter address to copy from original mp */ 6027 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6028 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6029 if (sin == NULL || !OK_32PTR((char *)sin)) { 6030 freemsg(nmp); 6031 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6032 return; 6033 } 6034 nsin->sin_family = sin->sin_family; 6035 nsin->sin_port = sin->sin_port; 6036 nsin->sin_addr = sin->sin_addr; 6037 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6038 nmp->b_wptr = (uchar_t *)&nsin[1]; 6039 if (tcr->OPT_length != 0) { 6040 ntcr->OPT_length = tcr->OPT_length; 6041 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6042 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6043 (uchar_t *)ntcr + ntcr->OPT_offset, 6044 tcr->OPT_length); 6045 nmp->b_wptr += tcr->OPT_length; 6046 } 6047 freemsg(mp); /* original mp freed */ 6048 mp = nmp; /* re-initialize original variables */ 6049 tcr = ntcr; 6050 } 6051 /* FALLTHRU */ 6052 6053 case sizeof (sin_t): 6054 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6055 sizeof (sin_t)); 6056 len = sizeof (sin_t); 6057 break; 6058 6059 case sizeof (sin6_t): 6060 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6061 sizeof (sin6_t)); 6062 len = sizeof (sin6_t); 6063 break; 6064 } 6065 6066 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6067 if (error != 0) { 6068 tcp_err_ack(tcp, mp, TSYSERR, error); 6069 return; 6070 } 6071 6072 /* 6073 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6074 * should key on their sequence number and cut them loose. 6075 */ 6076 6077 /* 6078 * If options passed in, feed it for verification and handling 6079 */ 6080 if (tcr->OPT_length != 0) { 6081 mblk_t *ok_mp; 6082 mblk_t *discon_mp; 6083 mblk_t *conn_opts_mp; 6084 int t_error, sys_error, do_disconnect; 6085 6086 conn_opts_mp = NULL; 6087 6088 if (tcp_conprim_opt_process(tcp, mp, 6089 &do_disconnect, &t_error, &sys_error) < 0) { 6090 if (do_disconnect) { 6091 ASSERT(t_error == 0 && sys_error == 0); 6092 discon_mp = mi_tpi_discon_ind(NULL, 6093 ECONNREFUSED, 0); 6094 if (!discon_mp) { 6095 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6096 TSYSERR, ENOMEM); 6097 return; 6098 } 6099 ok_mp = mi_tpi_ok_ack_alloc(mp); 6100 if (!ok_mp) { 6101 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6102 TSYSERR, ENOMEM); 6103 return; 6104 } 6105 qreply(q, ok_mp); 6106 qreply(q, discon_mp); /* no flush! */ 6107 } else { 6108 ASSERT(t_error != 0); 6109 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6110 sys_error); 6111 } 6112 return; 6113 } 6114 /* 6115 * Success in setting options, the mp option buffer represented 6116 * by OPT_length/offset has been potentially modified and 6117 * contains results of option processing. We copy it in 6118 * another mp to save it for potentially influencing returning 6119 * it in T_CONN_CONN. 6120 */ 6121 if (tcr->OPT_length != 0) { /* there are resulting options */ 6122 conn_opts_mp = copyb(mp); 6123 if (!conn_opts_mp) { 6124 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6125 TSYSERR, ENOMEM); 6126 return; 6127 } 6128 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6129 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6130 /* 6131 * Note: 6132 * These resulting option negotiation can include any 6133 * end-to-end negotiation options but there no such 6134 * thing (yet?) in our TCP/IP. 6135 */ 6136 } 6137 } 6138 6139 /* call the non-TPI version */ 6140 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6141 if (error < 0) { 6142 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6143 } else if (error > 0) { 6144 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6145 } else { 6146 mp = mi_tpi_ok_ack_alloc(mp); 6147 } 6148 6149 /* 6150 * Note: Code below is the "failure" case 6151 */ 6152 /* return error ack and blow away saved option results if any */ 6153 connect_failed: 6154 if (mp != NULL) 6155 putnext(tcp->tcp_rq, mp); 6156 else { 6157 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6158 TSYSERR, ENOMEM); 6159 } 6160 } 6161 6162 /* 6163 * Handle connect to IPv4 destinations, including connections for AF_INET6 6164 * sockets connecting to IPv4 mapped IPv6 destinations. 6165 */ 6166 static int 6167 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6168 uint_t srcid, cred_t *cr, pid_t pid) 6169 { 6170 tcph_t *tcph; 6171 mblk_t *mp; 6172 ipaddr_t dstaddr = *dstaddrp; 6173 int32_t oldstate; 6174 uint16_t lport; 6175 int error = 0; 6176 tcp_stack_t *tcps = tcp->tcp_tcps; 6177 6178 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6179 6180 /* Check for attempt to connect to INADDR_ANY */ 6181 if (dstaddr == INADDR_ANY) { 6182 /* 6183 * SunOS 4.x and 4.3 BSD allow an application 6184 * to connect a TCP socket to INADDR_ANY. 6185 * When they do this, the kernel picks the 6186 * address of one interface and uses it 6187 * instead. The kernel usually ends up 6188 * picking the address of the loopback 6189 * interface. This is an undocumented feature. 6190 * However, we provide the same thing here 6191 * in order to have source and binary 6192 * compatibility with SunOS 4.x. 6193 * Update the T_CONN_REQ (sin/sin6) since it is used to 6194 * generate the T_CONN_CON. 6195 */ 6196 dstaddr = htonl(INADDR_LOOPBACK); 6197 *dstaddrp = dstaddr; 6198 } 6199 6200 /* Handle __sin6_src_id if socket not bound to an IP address */ 6201 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6202 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6203 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6204 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6205 tcp->tcp_ipha->ipha_src); 6206 } 6207 6208 /* 6209 * Don't let an endpoint connect to itself. Note that 6210 * the test here does not catch the case where the 6211 * source IP addr was left unspecified by the user. In 6212 * this case, the source addr is set in tcp_adapt_ire() 6213 * using the reply to the T_BIND message that we send 6214 * down to IP here and the check is repeated in tcp_rput_other. 6215 */ 6216 if (dstaddr == tcp->tcp_ipha->ipha_src && 6217 dstport == tcp->tcp_lport) { 6218 error = -TBADADDR; 6219 goto failed; 6220 } 6221 6222 tcp->tcp_ipha->ipha_dst = dstaddr; 6223 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6224 6225 /* 6226 * Massage a source route if any putting the first hop 6227 * in iph_dst. Compute a starting value for the checksum which 6228 * takes into account that the original iph_dst should be 6229 * included in the checksum but that ip will include the 6230 * first hop in the source route in the tcp checksum. 6231 */ 6232 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6233 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6234 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6235 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6236 if ((int)tcp->tcp_sum < 0) 6237 tcp->tcp_sum--; 6238 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6239 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6240 (tcp->tcp_sum >> 16)); 6241 tcph = tcp->tcp_tcph; 6242 *(uint16_t *)tcph->th_fport = dstport; 6243 tcp->tcp_fport = dstport; 6244 6245 oldstate = tcp->tcp_state; 6246 /* 6247 * At this point the remote destination address and remote port fields 6248 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6249 * have to see which state tcp was in so we can take apropriate action. 6250 */ 6251 if (oldstate == TCPS_IDLE) { 6252 /* 6253 * We support a quick connect capability here, allowing 6254 * clients to transition directly from IDLE to SYN_SENT 6255 * tcp_bindi will pick an unused port, insert the connection 6256 * in the bind hash and transition to BOUND state. 6257 */ 6258 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6259 tcp, B_TRUE); 6260 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6261 B_FALSE, B_FALSE); 6262 if (lport == 0) { 6263 error = -TNOADDR; 6264 goto failed; 6265 } 6266 } 6267 tcp->tcp_state = TCPS_SYN_SENT; 6268 6269 mp = allocb(sizeof (ire_t), BPRI_HI); 6270 if (mp == NULL) { 6271 tcp->tcp_state = oldstate; 6272 error = ENOMEM; 6273 goto failed; 6274 } 6275 6276 mp->b_wptr += sizeof (ire_t); 6277 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6278 tcp->tcp_hard_binding = 1; 6279 6280 /* 6281 * We need to make sure that the conn_recv is set to a non-null 6282 * value before we insert the conn_t into the classifier table. 6283 * This is to avoid a race with an incoming packet which does 6284 * an ipcl_classify(). 6285 */ 6286 tcp->tcp_connp->conn_recv = tcp_input; 6287 6288 if (tcp->tcp_family == AF_INET) { 6289 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6290 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6291 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6292 } else { 6293 in6_addr_t v6src; 6294 if (tcp->tcp_ipversion == IPV4_VERSION) { 6295 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6296 } else { 6297 v6src = tcp->tcp_ip6h->ip6_src; 6298 } 6299 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6300 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6301 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6302 } 6303 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6304 tcp->tcp_active_open = 1; 6305 6306 6307 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6308 failed: 6309 /* return error ack and blow away saved option results if any */ 6310 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6311 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6312 return (error); 6313 } 6314 6315 /* 6316 * Handle connect to IPv6 destinations. 6317 */ 6318 static int 6319 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6320 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6321 { 6322 tcph_t *tcph; 6323 mblk_t *mp; 6324 ip6_rthdr_t *rth; 6325 int32_t oldstate; 6326 uint16_t lport; 6327 tcp_stack_t *tcps = tcp->tcp_tcps; 6328 int error = 0; 6329 conn_t *connp = tcp->tcp_connp; 6330 6331 ASSERT(tcp->tcp_family == AF_INET6); 6332 6333 /* 6334 * If we're here, it means that the destination address is a native 6335 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6336 * reason why it might not be IPv6 is if the socket was bound to an 6337 * IPv4-mapped IPv6 address. 6338 */ 6339 if (tcp->tcp_ipversion != IPV6_VERSION) { 6340 return (-TBADADDR); 6341 } 6342 6343 /* 6344 * Interpret a zero destination to mean loopback. 6345 * Update the T_CONN_REQ (sin/sin6) since it is used to 6346 * generate the T_CONN_CON. 6347 */ 6348 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6349 *dstaddrp = ipv6_loopback; 6350 } 6351 6352 /* Handle __sin6_src_id if socket not bound to an IP address */ 6353 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6354 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6355 connp->conn_zoneid, tcps->tcps_netstack); 6356 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6357 } 6358 6359 /* 6360 * Take care of the scope_id now and add ip6i_t 6361 * if ip6i_t is not already allocated through TCP 6362 * sticky options. At this point tcp_ip6h does not 6363 * have dst info, thus use dstaddrp. 6364 */ 6365 if (scope_id != 0 && 6366 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6367 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6368 ip6i_t *ip6i; 6369 6370 ipp->ipp_ifindex = scope_id; 6371 ip6i = (ip6i_t *)tcp->tcp_iphc; 6372 6373 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6374 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6375 /* Already allocated */ 6376 ip6i->ip6i_flags |= IP6I_IFINDEX; 6377 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6378 ipp->ipp_fields |= IPPF_SCOPE_ID; 6379 } else { 6380 int reterr; 6381 6382 ipp->ipp_fields |= IPPF_SCOPE_ID; 6383 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6384 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6385 reterr = tcp_build_hdrs(tcp); 6386 if (reterr != 0) 6387 goto failed; 6388 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6389 } 6390 } 6391 6392 /* 6393 * Don't let an endpoint connect to itself. Note that 6394 * the test here does not catch the case where the 6395 * source IP addr was left unspecified by the user. In 6396 * this case, the source addr is set in tcp_adapt_ire() 6397 * using the reply to the T_BIND message that we send 6398 * down to IP here and the check is repeated in tcp_rput_other. 6399 */ 6400 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6401 (dstport == tcp->tcp_lport)) { 6402 error = -TBADADDR; 6403 goto failed; 6404 } 6405 6406 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6407 tcp->tcp_remote_v6 = *dstaddrp; 6408 tcp->tcp_ip6h->ip6_vcf = 6409 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6410 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6411 6412 /* 6413 * Massage a routing header (if present) putting the first hop 6414 * in ip6_dst. Compute a starting value for the checksum which 6415 * takes into account that the original ip6_dst should be 6416 * included in the checksum but that ip will include the 6417 * first hop in the source route in the tcp checksum. 6418 */ 6419 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6420 if (rth != NULL) { 6421 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6422 tcps->tcps_netstack); 6423 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6424 (tcp->tcp_sum >> 16)); 6425 } else { 6426 tcp->tcp_sum = 0; 6427 } 6428 6429 tcph = tcp->tcp_tcph; 6430 *(uint16_t *)tcph->th_fport = dstport; 6431 tcp->tcp_fport = dstport; 6432 6433 oldstate = tcp->tcp_state; 6434 /* 6435 * At this point the remote destination address and remote port fields 6436 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6437 * have to see which state tcp was in so we can take apropriate action. 6438 */ 6439 if (oldstate == TCPS_IDLE) { 6440 /* 6441 * We support a quick connect capability here, allowing 6442 * clients to transition directly from IDLE to SYN_SENT 6443 * tcp_bindi will pick an unused port, insert the connection 6444 * in the bind hash and transition to BOUND state. 6445 */ 6446 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6447 tcp, B_TRUE); 6448 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6449 B_FALSE, B_FALSE); 6450 if (lport == 0) { 6451 error = -TNOADDR; 6452 goto failed; 6453 } 6454 } 6455 tcp->tcp_state = TCPS_SYN_SENT; 6456 6457 mp = allocb(sizeof (ire_t), BPRI_HI); 6458 if (mp != NULL) { 6459 in6_addr_t v6src; 6460 6461 mp->b_wptr += sizeof (ire_t); 6462 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6463 6464 tcp->tcp_hard_binding = 1; 6465 6466 /* 6467 * We need to make sure that the conn_recv is set to a non-null 6468 * value before we insert the conn_t into the classifier table. 6469 * This is to avoid a race with an incoming packet which does 6470 * an ipcl_classify(). 6471 */ 6472 tcp->tcp_connp->conn_recv = tcp_input; 6473 6474 if (tcp->tcp_ipversion == IPV4_VERSION) { 6475 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6476 } else { 6477 v6src = tcp->tcp_ip6h->ip6_src; 6478 } 6479 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6480 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6481 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6482 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6483 tcp->tcp_active_open = 1; 6484 6485 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6486 } 6487 /* Error case */ 6488 tcp->tcp_state = oldstate; 6489 error = ENOMEM; 6490 6491 failed: 6492 /* return error ack and blow away saved option results if any */ 6493 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6494 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6495 return (error); 6496 } 6497 6498 /* 6499 * We need a stream q for detached closing tcp connections 6500 * to use. Our client hereby indicates that this q is the 6501 * one to use. 6502 */ 6503 static void 6504 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6505 { 6506 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6507 queue_t *q = tcp->tcp_wq; 6508 tcp_stack_t *tcps = tcp->tcp_tcps; 6509 6510 #ifdef NS_DEBUG 6511 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6512 tcps->tcps_netstack->netstack_stackid); 6513 #endif 6514 mp->b_datap->db_type = M_IOCACK; 6515 iocp->ioc_count = 0; 6516 mutex_enter(&tcps->tcps_g_q_lock); 6517 if (tcps->tcps_g_q != NULL) { 6518 mutex_exit(&tcps->tcps_g_q_lock); 6519 iocp->ioc_error = EALREADY; 6520 } else { 6521 int error = 0; 6522 conn_t *connp = tcp->tcp_connp; 6523 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6524 6525 tcps->tcps_g_q = tcp->tcp_rq; 6526 mutex_exit(&tcps->tcps_g_q_lock); 6527 iocp->ioc_error = 0; 6528 iocp->ioc_rval = 0; 6529 /* 6530 * We are passing tcp_sticky_ipp as NULL 6531 * as it is not useful for tcp_default queue 6532 * 6533 * Set conn_recv just in case. 6534 */ 6535 tcp->tcp_connp->conn_recv = tcp_conn_request; 6536 6537 ASSERT(connp->conn_af_isv6); 6538 connp->conn_ulp = IPPROTO_TCP; 6539 6540 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6541 NULL || connp->conn_mac_exempt) { 6542 error = -TBADADDR; 6543 } else { 6544 connp->conn_srcv6 = ipv6_all_zeros; 6545 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6546 } 6547 6548 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6549 } 6550 qreply(q, mp); 6551 } 6552 6553 static int 6554 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6555 { 6556 tcp_t *ltcp = NULL; 6557 conn_t *connp; 6558 tcp_stack_t *tcps = tcp->tcp_tcps; 6559 6560 /* 6561 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6562 * when the stream is in BOUND state. Do not send a reset, 6563 * since the destination IP address is not valid, and it can 6564 * be the initialized value of all zeros (broadcast address). 6565 * 6566 * XXX There won't be any pending bind request to IP. 6567 */ 6568 if (tcp->tcp_state <= TCPS_BOUND) { 6569 if (tcp->tcp_debug) { 6570 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6571 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6572 } 6573 return (TOUTSTATE); 6574 } 6575 6576 6577 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6578 6579 /* 6580 * According to TPI, for non-listeners, ignore seqnum 6581 * and disconnect. 6582 * Following interpretation of -1 seqnum is historical 6583 * and implied TPI ? (TPI only states that for T_CONN_IND, 6584 * a valid seqnum should not be -1). 6585 * 6586 * -1 means disconnect everything 6587 * regardless even on a listener. 6588 */ 6589 6590 int old_state = tcp->tcp_state; 6591 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6592 6593 /* 6594 * The connection can't be on the tcp_time_wait_head list 6595 * since it is not detached. 6596 */ 6597 ASSERT(tcp->tcp_time_wait_next == NULL); 6598 ASSERT(tcp->tcp_time_wait_prev == NULL); 6599 ASSERT(tcp->tcp_time_wait_expire == 0); 6600 ltcp = NULL; 6601 /* 6602 * If it used to be a listener, check to make sure no one else 6603 * has taken the port before switching back to LISTEN state. 6604 */ 6605 if (tcp->tcp_ipversion == IPV4_VERSION) { 6606 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6607 tcp->tcp_ipha->ipha_src, 6608 tcp->tcp_connp->conn_zoneid, ipst); 6609 if (connp != NULL) 6610 ltcp = connp->conn_tcp; 6611 } else { 6612 /* Allow tcp_bound_if listeners? */ 6613 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6614 &tcp->tcp_ip6h->ip6_src, 0, 6615 tcp->tcp_connp->conn_zoneid, ipst); 6616 if (connp != NULL) 6617 ltcp = connp->conn_tcp; 6618 } 6619 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6620 tcp->tcp_state = TCPS_LISTEN; 6621 } else if (old_state > TCPS_BOUND) { 6622 tcp->tcp_conn_req_max = 0; 6623 tcp->tcp_state = TCPS_BOUND; 6624 } 6625 if (ltcp != NULL) 6626 CONN_DEC_REF(ltcp->tcp_connp); 6627 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6628 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6629 } else if (old_state == TCPS_ESTABLISHED || 6630 old_state == TCPS_CLOSE_WAIT) { 6631 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6632 } 6633 6634 if (tcp->tcp_fused) 6635 tcp_unfuse(tcp); 6636 6637 mutex_enter(&tcp->tcp_eager_lock); 6638 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6639 (tcp->tcp_conn_req_cnt_q != 0)) { 6640 tcp_eager_cleanup(tcp, 0); 6641 } 6642 mutex_exit(&tcp->tcp_eager_lock); 6643 6644 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6645 tcp->tcp_rnxt, TH_RST | TH_ACK); 6646 6647 tcp_reinit(tcp); 6648 6649 return (0); 6650 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6651 return (TBADSEQ); 6652 } 6653 return (0); 6654 } 6655 6656 /* 6657 * Our client hereby directs us to reject the connection request 6658 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6659 * of sending the appropriate RST, not an ICMP error. 6660 */ 6661 static void 6662 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6663 { 6664 t_scalar_t seqnum; 6665 int error; 6666 6667 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6668 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6669 tcp_err_ack(tcp, mp, TPROTO, 0); 6670 return; 6671 } 6672 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6673 error = tcp_disconnect_common(tcp, seqnum); 6674 if (error != 0) 6675 tcp_err_ack(tcp, mp, error, 0); 6676 else { 6677 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6678 /* Send M_FLUSH according to TPI */ 6679 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6680 } 6681 mp = mi_tpi_ok_ack_alloc(mp); 6682 if (mp) 6683 putnext(tcp->tcp_rq, mp); 6684 } 6685 } 6686 6687 /* 6688 * Diagnostic routine used to return a string associated with the tcp state. 6689 * Note that if the caller does not supply a buffer, it will use an internal 6690 * static string. This means that if multiple threads call this function at 6691 * the same time, output can be corrupted... Note also that this function 6692 * does not check the size of the supplied buffer. The caller has to make 6693 * sure that it is big enough. 6694 */ 6695 static char * 6696 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6697 { 6698 char buf1[30]; 6699 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6700 char *buf; 6701 char *cp; 6702 in6_addr_t local, remote; 6703 char local_addrbuf[INET6_ADDRSTRLEN]; 6704 char remote_addrbuf[INET6_ADDRSTRLEN]; 6705 6706 if (sup_buf != NULL) 6707 buf = sup_buf; 6708 else 6709 buf = priv_buf; 6710 6711 if (tcp == NULL) 6712 return ("NULL_TCP"); 6713 switch (tcp->tcp_state) { 6714 case TCPS_CLOSED: 6715 cp = "TCP_CLOSED"; 6716 break; 6717 case TCPS_IDLE: 6718 cp = "TCP_IDLE"; 6719 break; 6720 case TCPS_BOUND: 6721 cp = "TCP_BOUND"; 6722 break; 6723 case TCPS_LISTEN: 6724 cp = "TCP_LISTEN"; 6725 break; 6726 case TCPS_SYN_SENT: 6727 cp = "TCP_SYN_SENT"; 6728 break; 6729 case TCPS_SYN_RCVD: 6730 cp = "TCP_SYN_RCVD"; 6731 break; 6732 case TCPS_ESTABLISHED: 6733 cp = "TCP_ESTABLISHED"; 6734 break; 6735 case TCPS_CLOSE_WAIT: 6736 cp = "TCP_CLOSE_WAIT"; 6737 break; 6738 case TCPS_FIN_WAIT_1: 6739 cp = "TCP_FIN_WAIT_1"; 6740 break; 6741 case TCPS_CLOSING: 6742 cp = "TCP_CLOSING"; 6743 break; 6744 case TCPS_LAST_ACK: 6745 cp = "TCP_LAST_ACK"; 6746 break; 6747 case TCPS_FIN_WAIT_2: 6748 cp = "TCP_FIN_WAIT_2"; 6749 break; 6750 case TCPS_TIME_WAIT: 6751 cp = "TCP_TIME_WAIT"; 6752 break; 6753 default: 6754 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6755 cp = buf1; 6756 break; 6757 } 6758 switch (format) { 6759 case DISP_ADDR_AND_PORT: 6760 if (tcp->tcp_ipversion == IPV4_VERSION) { 6761 /* 6762 * Note that we use the remote address in the tcp_b 6763 * structure. This means that it will print out 6764 * the real destination address, not the next hop's 6765 * address if source routing is used. 6766 */ 6767 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6768 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6769 6770 } else { 6771 local = tcp->tcp_ip_src_v6; 6772 remote = tcp->tcp_remote_v6; 6773 } 6774 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6775 sizeof (local_addrbuf)); 6776 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6777 sizeof (remote_addrbuf)); 6778 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6779 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6780 ntohs(tcp->tcp_fport), cp); 6781 break; 6782 case DISP_PORT_ONLY: 6783 default: 6784 (void) mi_sprintf(buf, "[%u, %u] %s", 6785 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6786 break; 6787 } 6788 6789 return (buf); 6790 } 6791 6792 /* 6793 * Called via squeue to get on to eager's perimeter. It sends a 6794 * TH_RST if eager is in the fanout table. The listener wants the 6795 * eager to disappear either by means of tcp_eager_blowoff() or 6796 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6797 * called (via squeue) if the eager cannot be inserted in the 6798 * fanout table in tcp_conn_request(). 6799 */ 6800 /* ARGSUSED */ 6801 void 6802 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6803 { 6804 conn_t *econnp = (conn_t *)arg; 6805 tcp_t *eager = econnp->conn_tcp; 6806 tcp_t *listener = eager->tcp_listener; 6807 tcp_stack_t *tcps = eager->tcp_tcps; 6808 6809 /* 6810 * We could be called because listener is closing. Since 6811 * the eager is using listener's queue's, its not safe. 6812 * Better use the default queue just to send the TH_RST 6813 * out. 6814 */ 6815 ASSERT(tcps->tcps_g_q != NULL); 6816 eager->tcp_rq = tcps->tcps_g_q; 6817 eager->tcp_wq = WR(tcps->tcps_g_q); 6818 6819 /* 6820 * An eager's conn_fanout will be NULL if it's a duplicate 6821 * for an existing 4-tuples in the conn fanout table. 6822 * We don't want to send an RST out in such case. 6823 */ 6824 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6825 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6826 eager, eager->tcp_snxt, 0, TH_RST); 6827 } 6828 6829 /* We are here because listener wants this eager gone */ 6830 if (listener != NULL) { 6831 mutex_enter(&listener->tcp_eager_lock); 6832 tcp_eager_unlink(eager); 6833 if (eager->tcp_tconnind_started) { 6834 /* 6835 * The eager has sent a conn_ind up to the 6836 * listener but listener decides to close 6837 * instead. We need to drop the extra ref 6838 * placed on eager in tcp_rput_data() before 6839 * sending the conn_ind to listener. 6840 */ 6841 CONN_DEC_REF(econnp); 6842 } 6843 mutex_exit(&listener->tcp_eager_lock); 6844 CONN_DEC_REF(listener->tcp_connp); 6845 } 6846 6847 if (eager->tcp_state > TCPS_BOUND) 6848 tcp_close_detached(eager); 6849 } 6850 6851 /* 6852 * Reset any eager connection hanging off this listener marked 6853 * with 'seqnum' and then reclaim it's resources. 6854 */ 6855 static boolean_t 6856 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6857 { 6858 tcp_t *eager; 6859 mblk_t *mp; 6860 tcp_stack_t *tcps = listener->tcp_tcps; 6861 6862 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6863 eager = listener; 6864 mutex_enter(&listener->tcp_eager_lock); 6865 do { 6866 eager = eager->tcp_eager_next_q; 6867 if (eager == NULL) { 6868 mutex_exit(&listener->tcp_eager_lock); 6869 return (B_FALSE); 6870 } 6871 } while (eager->tcp_conn_req_seqnum != seqnum); 6872 6873 if (eager->tcp_closemp_used) { 6874 mutex_exit(&listener->tcp_eager_lock); 6875 return (B_TRUE); 6876 } 6877 eager->tcp_closemp_used = B_TRUE; 6878 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6879 CONN_INC_REF(eager->tcp_connp); 6880 mutex_exit(&listener->tcp_eager_lock); 6881 mp = &eager->tcp_closemp; 6882 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6883 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6884 return (B_TRUE); 6885 } 6886 6887 /* 6888 * Reset any eager connection hanging off this listener 6889 * and then reclaim it's resources. 6890 */ 6891 static void 6892 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6893 { 6894 tcp_t *eager; 6895 mblk_t *mp; 6896 tcp_stack_t *tcps = listener->tcp_tcps; 6897 6898 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6899 6900 if (!q0_only) { 6901 /* First cleanup q */ 6902 TCP_STAT(tcps, tcp_eager_blowoff_q); 6903 eager = listener->tcp_eager_next_q; 6904 while (eager != NULL) { 6905 if (!eager->tcp_closemp_used) { 6906 eager->tcp_closemp_used = B_TRUE; 6907 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6908 CONN_INC_REF(eager->tcp_connp); 6909 mp = &eager->tcp_closemp; 6910 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6911 tcp_eager_kill, eager->tcp_connp, 6912 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6913 } 6914 eager = eager->tcp_eager_next_q; 6915 } 6916 } 6917 /* Then cleanup q0 */ 6918 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6919 eager = listener->tcp_eager_next_q0; 6920 while (eager != listener) { 6921 if (!eager->tcp_closemp_used) { 6922 eager->tcp_closemp_used = B_TRUE; 6923 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6924 CONN_INC_REF(eager->tcp_connp); 6925 mp = &eager->tcp_closemp; 6926 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6927 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6928 SQTAG_TCP_EAGER_CLEANUP_Q0); 6929 } 6930 eager = eager->tcp_eager_next_q0; 6931 } 6932 } 6933 6934 /* 6935 * If we are an eager connection hanging off a listener that hasn't 6936 * formally accepted the connection yet, get off his list and blow off 6937 * any data that we have accumulated. 6938 */ 6939 static void 6940 tcp_eager_unlink(tcp_t *tcp) 6941 { 6942 tcp_t *listener = tcp->tcp_listener; 6943 6944 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6945 ASSERT(listener != NULL); 6946 if (tcp->tcp_eager_next_q0 != NULL) { 6947 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6948 6949 /* Remove the eager tcp from q0 */ 6950 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6951 tcp->tcp_eager_prev_q0; 6952 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6953 tcp->tcp_eager_next_q0; 6954 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6955 listener->tcp_conn_req_cnt_q0--; 6956 6957 tcp->tcp_eager_next_q0 = NULL; 6958 tcp->tcp_eager_prev_q0 = NULL; 6959 6960 /* 6961 * Take the eager out, if it is in the list of droppable 6962 * eagers. 6963 */ 6964 MAKE_UNDROPPABLE(tcp); 6965 6966 if (tcp->tcp_syn_rcvd_timeout != 0) { 6967 /* we have timed out before */ 6968 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6969 listener->tcp_syn_rcvd_timeout--; 6970 } 6971 } else { 6972 tcp_t **tcpp = &listener->tcp_eager_next_q; 6973 tcp_t *prev = NULL; 6974 6975 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6976 if (tcpp[0] == tcp) { 6977 if (listener->tcp_eager_last_q == tcp) { 6978 /* 6979 * If we are unlinking the last 6980 * element on the list, adjust 6981 * tail pointer. Set tail pointer 6982 * to nil when list is empty. 6983 */ 6984 ASSERT(tcp->tcp_eager_next_q == NULL); 6985 if (listener->tcp_eager_last_q == 6986 listener->tcp_eager_next_q) { 6987 listener->tcp_eager_last_q = 6988 NULL; 6989 } else { 6990 /* 6991 * We won't get here if there 6992 * is only one eager in the 6993 * list. 6994 */ 6995 ASSERT(prev != NULL); 6996 listener->tcp_eager_last_q = 6997 prev; 6998 } 6999 } 7000 tcpp[0] = tcp->tcp_eager_next_q; 7001 tcp->tcp_eager_next_q = NULL; 7002 tcp->tcp_eager_last_q = NULL; 7003 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7004 listener->tcp_conn_req_cnt_q--; 7005 break; 7006 } 7007 prev = tcpp[0]; 7008 } 7009 } 7010 tcp->tcp_listener = NULL; 7011 } 7012 7013 /* Shorthand to generate and send TPI error acks to our client */ 7014 static void 7015 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7016 { 7017 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7018 putnext(tcp->tcp_rq, mp); 7019 } 7020 7021 /* Shorthand to generate and send TPI error acks to our client */ 7022 static void 7023 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7024 int t_error, int sys_error) 7025 { 7026 struct T_error_ack *teackp; 7027 7028 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7029 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7030 teackp = (struct T_error_ack *)mp->b_rptr; 7031 teackp->ERROR_prim = primitive; 7032 teackp->TLI_error = t_error; 7033 teackp->UNIX_error = sys_error; 7034 putnext(tcp->tcp_rq, mp); 7035 } 7036 } 7037 7038 /* 7039 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7040 * but instead the code relies on: 7041 * - the fact that the address of the array and its size never changes 7042 * - the atomic assignment of the elements of the array 7043 */ 7044 /* ARGSUSED */ 7045 static int 7046 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7047 { 7048 int i; 7049 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7050 7051 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7052 if (tcps->tcps_g_epriv_ports[i] != 0) 7053 (void) mi_mpprintf(mp, "%d ", 7054 tcps->tcps_g_epriv_ports[i]); 7055 } 7056 return (0); 7057 } 7058 7059 /* 7060 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7061 * threads from changing it at the same time. 7062 */ 7063 /* ARGSUSED */ 7064 static int 7065 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7066 cred_t *cr) 7067 { 7068 long new_value; 7069 int i; 7070 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7071 7072 /* 7073 * Fail the request if the new value does not lie within the 7074 * port number limits. 7075 */ 7076 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7077 new_value <= 0 || new_value >= 65536) { 7078 return (EINVAL); 7079 } 7080 7081 mutex_enter(&tcps->tcps_epriv_port_lock); 7082 /* Check if the value is already in the list */ 7083 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7084 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7085 mutex_exit(&tcps->tcps_epriv_port_lock); 7086 return (EEXIST); 7087 } 7088 } 7089 /* Find an empty slot */ 7090 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7091 if (tcps->tcps_g_epriv_ports[i] == 0) 7092 break; 7093 } 7094 if (i == tcps->tcps_g_num_epriv_ports) { 7095 mutex_exit(&tcps->tcps_epriv_port_lock); 7096 return (EOVERFLOW); 7097 } 7098 /* Set the new value */ 7099 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7100 mutex_exit(&tcps->tcps_epriv_port_lock); 7101 return (0); 7102 } 7103 7104 /* 7105 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7106 * threads from changing it at the same time. 7107 */ 7108 /* ARGSUSED */ 7109 static int 7110 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7111 cred_t *cr) 7112 { 7113 long new_value; 7114 int i; 7115 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7116 7117 /* 7118 * Fail the request if the new value does not lie within the 7119 * port number limits. 7120 */ 7121 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7122 new_value >= 65536) { 7123 return (EINVAL); 7124 } 7125 7126 mutex_enter(&tcps->tcps_epriv_port_lock); 7127 /* Check that the value is already in the list */ 7128 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7129 if (tcps->tcps_g_epriv_ports[i] == new_value) 7130 break; 7131 } 7132 if (i == tcps->tcps_g_num_epriv_ports) { 7133 mutex_exit(&tcps->tcps_epriv_port_lock); 7134 return (ESRCH); 7135 } 7136 /* Clear the value */ 7137 tcps->tcps_g_epriv_ports[i] = 0; 7138 mutex_exit(&tcps->tcps_epriv_port_lock); 7139 return (0); 7140 } 7141 7142 /* Return the TPI/TLI equivalent of our current tcp_state */ 7143 static int 7144 tcp_tpistate(tcp_t *tcp) 7145 { 7146 switch (tcp->tcp_state) { 7147 case TCPS_IDLE: 7148 return (TS_UNBND); 7149 case TCPS_LISTEN: 7150 /* 7151 * Return whether there are outstanding T_CONN_IND waiting 7152 * for the matching T_CONN_RES. Therefore don't count q0. 7153 */ 7154 if (tcp->tcp_conn_req_cnt_q > 0) 7155 return (TS_WRES_CIND); 7156 else 7157 return (TS_IDLE); 7158 case TCPS_BOUND: 7159 return (TS_IDLE); 7160 case TCPS_SYN_SENT: 7161 return (TS_WCON_CREQ); 7162 case TCPS_SYN_RCVD: 7163 /* 7164 * Note: assumption: this has to the active open SYN_RCVD. 7165 * The passive instance is detached in SYN_RCVD stage of 7166 * incoming connection processing so we cannot get request 7167 * for T_info_ack on it. 7168 */ 7169 return (TS_WACK_CRES); 7170 case TCPS_ESTABLISHED: 7171 return (TS_DATA_XFER); 7172 case TCPS_CLOSE_WAIT: 7173 return (TS_WREQ_ORDREL); 7174 case TCPS_FIN_WAIT_1: 7175 return (TS_WIND_ORDREL); 7176 case TCPS_FIN_WAIT_2: 7177 return (TS_WIND_ORDREL); 7178 7179 case TCPS_CLOSING: 7180 case TCPS_LAST_ACK: 7181 case TCPS_TIME_WAIT: 7182 case TCPS_CLOSED: 7183 /* 7184 * Following TS_WACK_DREQ7 is a rendition of "not 7185 * yet TS_IDLE" TPI state. There is no best match to any 7186 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7187 * choose a value chosen that will map to TLI/XTI level 7188 * state of TSTATECHNG (state is process of changing) which 7189 * captures what this dummy state represents. 7190 */ 7191 return (TS_WACK_DREQ7); 7192 default: 7193 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7194 tcp->tcp_state, tcp_display(tcp, NULL, 7195 DISP_PORT_ONLY)); 7196 return (TS_UNBND); 7197 } 7198 } 7199 7200 static void 7201 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7202 { 7203 tcp_stack_t *tcps = tcp->tcp_tcps; 7204 7205 if (tcp->tcp_family == AF_INET6) 7206 *tia = tcp_g_t_info_ack_v6; 7207 else 7208 *tia = tcp_g_t_info_ack; 7209 tia->CURRENT_state = tcp_tpistate(tcp); 7210 tia->OPT_size = tcp_max_optsize; 7211 if (tcp->tcp_mss == 0) { 7212 /* Not yet set - tcp_open does not set mss */ 7213 if (tcp->tcp_ipversion == IPV4_VERSION) 7214 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7215 else 7216 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7217 } else { 7218 tia->TIDU_size = tcp->tcp_mss; 7219 } 7220 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7221 } 7222 7223 static void 7224 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7225 t_uscalar_t cap_bits1) 7226 { 7227 tcap->CAP_bits1 = 0; 7228 7229 if (cap_bits1 & TC1_INFO) { 7230 tcp_copy_info(&tcap->INFO_ack, tcp); 7231 tcap->CAP_bits1 |= TC1_INFO; 7232 } 7233 7234 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7235 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7236 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7237 } 7238 7239 } 7240 7241 /* 7242 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7243 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7244 * tcp_g_t_info_ack. The current state of the stream is copied from 7245 * tcp_state. 7246 */ 7247 static void 7248 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7249 { 7250 t_uscalar_t cap_bits1; 7251 struct T_capability_ack *tcap; 7252 7253 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7254 freemsg(mp); 7255 return; 7256 } 7257 7258 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7259 7260 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7261 mp->b_datap->db_type, T_CAPABILITY_ACK); 7262 if (mp == NULL) 7263 return; 7264 7265 tcap = (struct T_capability_ack *)mp->b_rptr; 7266 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7267 7268 putnext(tcp->tcp_rq, mp); 7269 } 7270 7271 /* 7272 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7273 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7274 * The current state of the stream is copied from tcp_state. 7275 */ 7276 static void 7277 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7278 { 7279 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7280 T_INFO_ACK); 7281 if (!mp) { 7282 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7283 return; 7284 } 7285 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7286 putnext(tcp->tcp_rq, mp); 7287 } 7288 7289 /* Respond to the TPI addr request */ 7290 static void 7291 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7292 { 7293 sin_t *sin; 7294 mblk_t *ackmp; 7295 struct T_addr_ack *taa; 7296 7297 /* Make it large enough for worst case */ 7298 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7299 2 * sizeof (sin6_t), 1); 7300 if (ackmp == NULL) { 7301 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7302 return; 7303 } 7304 7305 if (tcp->tcp_ipversion == IPV6_VERSION) { 7306 tcp_addr_req_ipv6(tcp, ackmp); 7307 return; 7308 } 7309 taa = (struct T_addr_ack *)ackmp->b_rptr; 7310 7311 bzero(taa, sizeof (struct T_addr_ack)); 7312 ackmp->b_wptr = (uchar_t *)&taa[1]; 7313 7314 taa->PRIM_type = T_ADDR_ACK; 7315 ackmp->b_datap->db_type = M_PCPROTO; 7316 7317 /* 7318 * Note: Following code assumes 32 bit alignment of basic 7319 * data structures like sin_t and struct T_addr_ack. 7320 */ 7321 if (tcp->tcp_state >= TCPS_BOUND) { 7322 /* 7323 * Fill in local address 7324 */ 7325 taa->LOCADDR_length = sizeof (sin_t); 7326 taa->LOCADDR_offset = sizeof (*taa); 7327 7328 sin = (sin_t *)&taa[1]; 7329 7330 /* Fill zeroes and then intialize non-zero fields */ 7331 *sin = sin_null; 7332 7333 sin->sin_family = AF_INET; 7334 7335 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7336 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7337 7338 ackmp->b_wptr = (uchar_t *)&sin[1]; 7339 7340 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7341 /* 7342 * Fill in Remote address 7343 */ 7344 taa->REMADDR_length = sizeof (sin_t); 7345 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7346 taa->LOCADDR_length); 7347 7348 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7349 *sin = sin_null; 7350 sin->sin_family = AF_INET; 7351 sin->sin_addr.s_addr = tcp->tcp_remote; 7352 sin->sin_port = tcp->tcp_fport; 7353 7354 ackmp->b_wptr = (uchar_t *)&sin[1]; 7355 } 7356 } 7357 putnext(tcp->tcp_rq, ackmp); 7358 } 7359 7360 /* Assumes that tcp_addr_req gets enough space and alignment */ 7361 static void 7362 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7363 { 7364 sin6_t *sin6; 7365 struct T_addr_ack *taa; 7366 7367 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7368 ASSERT(OK_32PTR(ackmp->b_rptr)); 7369 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7370 2 * sizeof (sin6_t)); 7371 7372 taa = (struct T_addr_ack *)ackmp->b_rptr; 7373 7374 bzero(taa, sizeof (struct T_addr_ack)); 7375 ackmp->b_wptr = (uchar_t *)&taa[1]; 7376 7377 taa->PRIM_type = T_ADDR_ACK; 7378 ackmp->b_datap->db_type = M_PCPROTO; 7379 7380 /* 7381 * Note: Following code assumes 32 bit alignment of basic 7382 * data structures like sin6_t and struct T_addr_ack. 7383 */ 7384 if (tcp->tcp_state >= TCPS_BOUND) { 7385 /* 7386 * Fill in local address 7387 */ 7388 taa->LOCADDR_length = sizeof (sin6_t); 7389 taa->LOCADDR_offset = sizeof (*taa); 7390 7391 sin6 = (sin6_t *)&taa[1]; 7392 *sin6 = sin6_null; 7393 7394 sin6->sin6_family = AF_INET6; 7395 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7396 sin6->sin6_port = tcp->tcp_lport; 7397 7398 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7399 7400 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7401 /* 7402 * Fill in Remote address 7403 */ 7404 taa->REMADDR_length = sizeof (sin6_t); 7405 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7406 taa->LOCADDR_length); 7407 7408 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7409 *sin6 = sin6_null; 7410 sin6->sin6_family = AF_INET6; 7411 sin6->sin6_flowinfo = 7412 tcp->tcp_ip6h->ip6_vcf & 7413 ~IPV6_VERS_AND_FLOW_MASK; 7414 sin6->sin6_addr = tcp->tcp_remote_v6; 7415 sin6->sin6_port = tcp->tcp_fport; 7416 7417 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7418 } 7419 } 7420 putnext(tcp->tcp_rq, ackmp); 7421 } 7422 7423 /* 7424 * Handle reinitialization of a tcp structure. 7425 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7426 */ 7427 static void 7428 tcp_reinit(tcp_t *tcp) 7429 { 7430 mblk_t *mp; 7431 int err; 7432 tcp_stack_t *tcps = tcp->tcp_tcps; 7433 7434 TCP_STAT(tcps, tcp_reinit_calls); 7435 7436 /* tcp_reinit should never be called for detached tcp_t's */ 7437 ASSERT(tcp->tcp_listener == NULL); 7438 ASSERT((tcp->tcp_family == AF_INET && 7439 tcp->tcp_ipversion == IPV4_VERSION) || 7440 (tcp->tcp_family == AF_INET6 && 7441 (tcp->tcp_ipversion == IPV4_VERSION || 7442 tcp->tcp_ipversion == IPV6_VERSION))); 7443 7444 /* Cancel outstanding timers */ 7445 tcp_timers_stop(tcp); 7446 7447 /* 7448 * Reset everything in the state vector, after updating global 7449 * MIB data from instance counters. 7450 */ 7451 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7452 tcp->tcp_ibsegs = 0; 7453 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7454 tcp->tcp_obsegs = 0; 7455 7456 tcp_close_mpp(&tcp->tcp_xmit_head); 7457 if (tcp->tcp_snd_zcopy_aware) 7458 tcp_zcopy_notify(tcp); 7459 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7460 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7461 mutex_enter(&tcp->tcp_non_sq_lock); 7462 if (tcp->tcp_flow_stopped && 7463 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7464 tcp_clrqfull(tcp); 7465 } 7466 mutex_exit(&tcp->tcp_non_sq_lock); 7467 tcp_close_mpp(&tcp->tcp_reass_head); 7468 tcp->tcp_reass_tail = NULL; 7469 if (tcp->tcp_rcv_list != NULL) { 7470 /* Free b_next chain */ 7471 tcp_close_mpp(&tcp->tcp_rcv_list); 7472 tcp->tcp_rcv_last_head = NULL; 7473 tcp->tcp_rcv_last_tail = NULL; 7474 tcp->tcp_rcv_cnt = 0; 7475 } 7476 tcp->tcp_rcv_last_tail = NULL; 7477 7478 if ((mp = tcp->tcp_urp_mp) != NULL) { 7479 freemsg(mp); 7480 tcp->tcp_urp_mp = NULL; 7481 } 7482 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7483 freemsg(mp); 7484 tcp->tcp_urp_mark_mp = NULL; 7485 } 7486 if (tcp->tcp_fused_sigurg_mp != NULL) { 7487 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7488 freeb(tcp->tcp_fused_sigurg_mp); 7489 tcp->tcp_fused_sigurg_mp = NULL; 7490 } 7491 if (tcp->tcp_ordrel_mp != NULL) { 7492 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7493 freeb(tcp->tcp_ordrel_mp); 7494 tcp->tcp_ordrel_mp = NULL; 7495 } 7496 7497 /* 7498 * Following is a union with two members which are 7499 * identical types and size so the following cleanup 7500 * is enough. 7501 */ 7502 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7503 7504 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7505 7506 /* 7507 * The connection can't be on the tcp_time_wait_head list 7508 * since it is not detached. 7509 */ 7510 ASSERT(tcp->tcp_time_wait_next == NULL); 7511 ASSERT(tcp->tcp_time_wait_prev == NULL); 7512 ASSERT(tcp->tcp_time_wait_expire == 0); 7513 7514 if (tcp->tcp_kssl_pending) { 7515 tcp->tcp_kssl_pending = B_FALSE; 7516 7517 /* Don't reset if the initialized by bind. */ 7518 if (tcp->tcp_kssl_ent != NULL) { 7519 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7520 KSSL_NO_PROXY); 7521 } 7522 } 7523 if (tcp->tcp_kssl_ctx != NULL) { 7524 kssl_release_ctx(tcp->tcp_kssl_ctx); 7525 tcp->tcp_kssl_ctx = NULL; 7526 } 7527 7528 /* 7529 * Reset/preserve other values 7530 */ 7531 tcp_reinit_values(tcp); 7532 ipcl_hash_remove(tcp->tcp_connp); 7533 conn_delete_ire(tcp->tcp_connp, NULL); 7534 tcp_ipsec_cleanup(tcp); 7535 7536 if (tcp->tcp_conn_req_max != 0) { 7537 /* 7538 * This is the case when a TLI program uses the same 7539 * transport end point to accept a connection. This 7540 * makes the TCP both a listener and acceptor. When 7541 * this connection is closed, we need to set the state 7542 * back to TCPS_LISTEN. Make sure that the eager list 7543 * is reinitialized. 7544 * 7545 * Note that this stream is still bound to the four 7546 * tuples of the previous connection in IP. If a new 7547 * SYN with different foreign address comes in, IP will 7548 * not find it and will send it to the global queue. In 7549 * the global queue, TCP will do a tcp_lookup_listener() 7550 * to find this stream. This works because this stream 7551 * is only removed from connected hash. 7552 * 7553 */ 7554 tcp->tcp_state = TCPS_LISTEN; 7555 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7556 tcp->tcp_eager_next_drop_q0 = tcp; 7557 tcp->tcp_eager_prev_drop_q0 = tcp; 7558 tcp->tcp_connp->conn_recv = tcp_conn_request; 7559 if (tcp->tcp_family == AF_INET6) { 7560 ASSERT(tcp->tcp_connp->conn_af_isv6); 7561 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7562 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7563 } else { 7564 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7565 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7566 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7567 } 7568 } else { 7569 tcp->tcp_state = TCPS_BOUND; 7570 } 7571 7572 /* 7573 * Initialize to default values 7574 * Can't fail since enough header template space already allocated 7575 * at open(). 7576 */ 7577 err = tcp_init_values(tcp); 7578 ASSERT(err == 0); 7579 /* Restore state in tcp_tcph */ 7580 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7581 if (tcp->tcp_ipversion == IPV4_VERSION) 7582 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7583 else 7584 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7585 /* 7586 * Copy of the src addr. in tcp_t is needed in tcp_t 7587 * since the lookup funcs can only lookup on tcp_t 7588 */ 7589 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7590 7591 ASSERT(tcp->tcp_ptpbhn != NULL); 7592 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7593 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7594 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7595 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7596 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7597 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7598 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7599 } 7600 7601 /* 7602 * Force values to zero that need be zero. 7603 * Do not touch values asociated with the BOUND or LISTEN state 7604 * since the connection will end up in that state after the reinit. 7605 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7606 * structure! 7607 */ 7608 static void 7609 tcp_reinit_values(tcp) 7610 tcp_t *tcp; 7611 { 7612 tcp_stack_t *tcps = tcp->tcp_tcps; 7613 7614 #ifndef lint 7615 #define DONTCARE(x) 7616 #define PRESERVE(x) 7617 #else 7618 #define DONTCARE(x) ((x) = (x)) 7619 #define PRESERVE(x) ((x) = (x)) 7620 #endif /* lint */ 7621 7622 PRESERVE(tcp->tcp_bind_hash_port); 7623 PRESERVE(tcp->tcp_bind_hash); 7624 PRESERVE(tcp->tcp_ptpbhn); 7625 PRESERVE(tcp->tcp_acceptor_hash); 7626 PRESERVE(tcp->tcp_ptpahn); 7627 7628 /* Should be ASSERT NULL on these with new code! */ 7629 ASSERT(tcp->tcp_time_wait_next == NULL); 7630 ASSERT(tcp->tcp_time_wait_prev == NULL); 7631 ASSERT(tcp->tcp_time_wait_expire == 0); 7632 PRESERVE(tcp->tcp_state); 7633 PRESERVE(tcp->tcp_rq); 7634 PRESERVE(tcp->tcp_wq); 7635 7636 ASSERT(tcp->tcp_xmit_head == NULL); 7637 ASSERT(tcp->tcp_xmit_last == NULL); 7638 ASSERT(tcp->tcp_unsent == 0); 7639 ASSERT(tcp->tcp_xmit_tail == NULL); 7640 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7641 7642 tcp->tcp_snxt = 0; /* Displayed in mib */ 7643 tcp->tcp_suna = 0; /* Displayed in mib */ 7644 tcp->tcp_swnd = 0; 7645 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7646 7647 ASSERT(tcp->tcp_ibsegs == 0); 7648 ASSERT(tcp->tcp_obsegs == 0); 7649 7650 if (tcp->tcp_iphc != NULL) { 7651 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7652 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7653 } 7654 7655 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7656 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7657 DONTCARE(tcp->tcp_ipha); 7658 DONTCARE(tcp->tcp_ip6h); 7659 DONTCARE(tcp->tcp_ip_hdr_len); 7660 DONTCARE(tcp->tcp_tcph); 7661 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7662 tcp->tcp_valid_bits = 0; 7663 7664 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7665 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7666 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7667 tcp->tcp_last_rcv_lbolt = 0; 7668 7669 tcp->tcp_init_cwnd = 0; 7670 7671 tcp->tcp_urp_last_valid = 0; 7672 tcp->tcp_hard_binding = 0; 7673 tcp->tcp_hard_bound = 0; 7674 PRESERVE(tcp->tcp_cred); 7675 PRESERVE(tcp->tcp_cpid); 7676 PRESERVE(tcp->tcp_open_time); 7677 PRESERVE(tcp->tcp_exclbind); 7678 7679 tcp->tcp_fin_acked = 0; 7680 tcp->tcp_fin_rcvd = 0; 7681 tcp->tcp_fin_sent = 0; 7682 tcp->tcp_ordrel_done = 0; 7683 7684 tcp->tcp_debug = 0; 7685 tcp->tcp_dontroute = 0; 7686 tcp->tcp_broadcast = 0; 7687 7688 tcp->tcp_useloopback = 0; 7689 tcp->tcp_reuseaddr = 0; 7690 tcp->tcp_oobinline = 0; 7691 tcp->tcp_dgram_errind = 0; 7692 7693 tcp->tcp_detached = 0; 7694 tcp->tcp_bind_pending = 0; 7695 tcp->tcp_unbind_pending = 0; 7696 7697 tcp->tcp_snd_ws_ok = B_FALSE; 7698 tcp->tcp_snd_ts_ok = B_FALSE; 7699 tcp->tcp_linger = 0; 7700 tcp->tcp_ka_enabled = 0; 7701 tcp->tcp_zero_win_probe = 0; 7702 7703 tcp->tcp_loopback = 0; 7704 tcp->tcp_refuse = 0; 7705 tcp->tcp_localnet = 0; 7706 tcp->tcp_syn_defense = 0; 7707 tcp->tcp_set_timer = 0; 7708 7709 tcp->tcp_active_open = 0; 7710 tcp->tcp_rexmit = B_FALSE; 7711 tcp->tcp_xmit_zc_clean = B_FALSE; 7712 7713 tcp->tcp_snd_sack_ok = B_FALSE; 7714 PRESERVE(tcp->tcp_recvdstaddr); 7715 tcp->tcp_hwcksum = B_FALSE; 7716 7717 tcp->tcp_ire_ill_check_done = B_FALSE; 7718 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7719 7720 tcp->tcp_mdt = B_FALSE; 7721 tcp->tcp_mdt_hdr_head = 0; 7722 tcp->tcp_mdt_hdr_tail = 0; 7723 7724 tcp->tcp_conn_def_q0 = 0; 7725 tcp->tcp_ip_forward_progress = B_FALSE; 7726 tcp->tcp_anon_priv_bind = 0; 7727 tcp->tcp_ecn_ok = B_FALSE; 7728 7729 tcp->tcp_cwr = B_FALSE; 7730 tcp->tcp_ecn_echo_on = B_FALSE; 7731 7732 if (tcp->tcp_sack_info != NULL) { 7733 if (tcp->tcp_notsack_list != NULL) { 7734 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7735 } 7736 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7737 tcp->tcp_sack_info = NULL; 7738 } 7739 7740 tcp->tcp_rcv_ws = 0; 7741 tcp->tcp_snd_ws = 0; 7742 tcp->tcp_ts_recent = 0; 7743 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7744 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7745 tcp->tcp_if_mtu = 0; 7746 7747 ASSERT(tcp->tcp_reass_head == NULL); 7748 ASSERT(tcp->tcp_reass_tail == NULL); 7749 7750 tcp->tcp_cwnd_cnt = 0; 7751 7752 ASSERT(tcp->tcp_rcv_list == NULL); 7753 ASSERT(tcp->tcp_rcv_last_head == NULL); 7754 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7755 ASSERT(tcp->tcp_rcv_cnt == 0); 7756 7757 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7758 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7759 tcp->tcp_csuna = 0; 7760 7761 tcp->tcp_rto = 0; /* Displayed in MIB */ 7762 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7763 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7764 tcp->tcp_rtt_update = 0; 7765 7766 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7767 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7768 7769 tcp->tcp_rack = 0; /* Displayed in mib */ 7770 tcp->tcp_rack_cnt = 0; 7771 tcp->tcp_rack_cur_max = 0; 7772 tcp->tcp_rack_abs_max = 0; 7773 7774 tcp->tcp_max_swnd = 0; 7775 7776 ASSERT(tcp->tcp_listener == NULL); 7777 7778 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7779 7780 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7781 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7782 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7783 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7784 7785 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7786 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7787 PRESERVE(tcp->tcp_conn_req_max); 7788 PRESERVE(tcp->tcp_conn_req_seqnum); 7789 7790 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7791 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7792 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7793 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7794 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7795 7796 tcp->tcp_lingertime = 0; 7797 7798 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7799 ASSERT(tcp->tcp_urp_mp == NULL); 7800 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7801 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7802 7803 ASSERT(tcp->tcp_eager_next_q == NULL); 7804 ASSERT(tcp->tcp_eager_last_q == NULL); 7805 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7806 tcp->tcp_eager_prev_q0 == NULL) || 7807 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7808 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7809 7810 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7811 tcp->tcp_eager_prev_drop_q0 == NULL) || 7812 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7813 7814 tcp->tcp_client_errno = 0; 7815 7816 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7817 7818 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7819 7820 PRESERVE(tcp->tcp_bound_source_v6); 7821 tcp->tcp_last_sent_len = 0; 7822 tcp->tcp_dupack_cnt = 0; 7823 7824 tcp->tcp_fport = 0; /* Displayed in MIB */ 7825 PRESERVE(tcp->tcp_lport); 7826 7827 PRESERVE(tcp->tcp_acceptor_lockp); 7828 7829 ASSERT(tcp->tcp_ordrel_mp == NULL); 7830 PRESERVE(tcp->tcp_acceptor_id); 7831 DONTCARE(tcp->tcp_ipsec_overhead); 7832 7833 PRESERVE(tcp->tcp_family); 7834 if (tcp->tcp_family == AF_INET6) { 7835 tcp->tcp_ipversion = IPV6_VERSION; 7836 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7837 } else { 7838 tcp->tcp_ipversion = IPV4_VERSION; 7839 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7840 } 7841 7842 tcp->tcp_bound_if = 0; 7843 tcp->tcp_ipv6_recvancillary = 0; 7844 tcp->tcp_recvifindex = 0; 7845 tcp->tcp_recvhops = 0; 7846 tcp->tcp_closed = 0; 7847 tcp->tcp_cleandeathtag = 0; 7848 if (tcp->tcp_hopopts != NULL) { 7849 mi_free(tcp->tcp_hopopts); 7850 tcp->tcp_hopopts = NULL; 7851 tcp->tcp_hopoptslen = 0; 7852 } 7853 ASSERT(tcp->tcp_hopoptslen == 0); 7854 if (tcp->tcp_dstopts != NULL) { 7855 mi_free(tcp->tcp_dstopts); 7856 tcp->tcp_dstopts = NULL; 7857 tcp->tcp_dstoptslen = 0; 7858 } 7859 ASSERT(tcp->tcp_dstoptslen == 0); 7860 if (tcp->tcp_rtdstopts != NULL) { 7861 mi_free(tcp->tcp_rtdstopts); 7862 tcp->tcp_rtdstopts = NULL; 7863 tcp->tcp_rtdstoptslen = 0; 7864 } 7865 ASSERT(tcp->tcp_rtdstoptslen == 0); 7866 if (tcp->tcp_rthdr != NULL) { 7867 mi_free(tcp->tcp_rthdr); 7868 tcp->tcp_rthdr = NULL; 7869 tcp->tcp_rthdrlen = 0; 7870 } 7871 ASSERT(tcp->tcp_rthdrlen == 0); 7872 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7873 7874 /* Reset fusion-related fields */ 7875 tcp->tcp_fused = B_FALSE; 7876 tcp->tcp_unfusable = B_FALSE; 7877 tcp->tcp_fused_sigurg = B_FALSE; 7878 tcp->tcp_direct_sockfs = B_FALSE; 7879 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7880 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7881 tcp->tcp_loopback_peer = NULL; 7882 tcp->tcp_fuse_rcv_hiwater = 0; 7883 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7884 tcp->tcp_fuse_rcv_unread_cnt = 0; 7885 7886 tcp->tcp_lso = B_FALSE; 7887 7888 tcp->tcp_in_ack_unsent = 0; 7889 tcp->tcp_cork = B_FALSE; 7890 tcp->tcp_tconnind_started = B_FALSE; 7891 7892 PRESERVE(tcp->tcp_squeue_bytes); 7893 7894 ASSERT(tcp->tcp_kssl_ctx == NULL); 7895 ASSERT(!tcp->tcp_kssl_pending); 7896 PRESERVE(tcp->tcp_kssl_ent); 7897 7898 /* Sodirect */ 7899 tcp->tcp_sodirect = NULL; 7900 7901 tcp->tcp_closemp_used = B_FALSE; 7902 7903 PRESERVE(tcp->tcp_rsrv_mp); 7904 PRESERVE(tcp->tcp_rsrv_mp_lock); 7905 7906 #ifdef DEBUG 7907 DONTCARE(tcp->tcmp_stk[0]); 7908 #endif 7909 7910 PRESERVE(tcp->tcp_connid); 7911 7912 7913 #undef DONTCARE 7914 #undef PRESERVE 7915 } 7916 7917 /* 7918 * Allocate necessary resources and initialize state vector. 7919 * Guaranteed not to fail so that when an error is returned, 7920 * the caller doesn't need to do any additional cleanup. 7921 */ 7922 int 7923 tcp_init(tcp_t *tcp, queue_t *q) 7924 { 7925 int err; 7926 7927 tcp->tcp_rq = q; 7928 tcp->tcp_wq = WR(q); 7929 tcp->tcp_state = TCPS_IDLE; 7930 if ((err = tcp_init_values(tcp)) != 0) 7931 tcp_timers_stop(tcp); 7932 return (err); 7933 } 7934 7935 static int 7936 tcp_init_values(tcp_t *tcp) 7937 { 7938 int err; 7939 tcp_stack_t *tcps = tcp->tcp_tcps; 7940 7941 ASSERT((tcp->tcp_family == AF_INET && 7942 tcp->tcp_ipversion == IPV4_VERSION) || 7943 (tcp->tcp_family == AF_INET6 && 7944 (tcp->tcp_ipversion == IPV4_VERSION || 7945 tcp->tcp_ipversion == IPV6_VERSION))); 7946 7947 /* 7948 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7949 * will be close to tcp_rexmit_interval_initial. By doing this, we 7950 * allow the algorithm to adjust slowly to large fluctuations of RTT 7951 * during first few transmissions of a connection as seen in slow 7952 * links. 7953 */ 7954 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7955 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7956 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7957 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7958 tcps->tcps_conn_grace_period; 7959 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7960 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7961 tcp->tcp_timer_backoff = 0; 7962 tcp->tcp_ms_we_have_waited = 0; 7963 tcp->tcp_last_recv_time = lbolt; 7964 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7965 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7966 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7967 7968 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7969 7970 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7971 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7972 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7973 /* 7974 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7975 * passive open. 7976 */ 7977 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7978 7979 tcp->tcp_naglim = tcps->tcps_naglim_def; 7980 7981 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7982 7983 tcp->tcp_mdt_hdr_head = 0; 7984 tcp->tcp_mdt_hdr_tail = 0; 7985 7986 /* Reset fusion-related fields */ 7987 tcp->tcp_fused = B_FALSE; 7988 tcp->tcp_unfusable = B_FALSE; 7989 tcp->tcp_fused_sigurg = B_FALSE; 7990 tcp->tcp_direct_sockfs = B_FALSE; 7991 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7992 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7993 tcp->tcp_loopback_peer = NULL; 7994 tcp->tcp_fuse_rcv_hiwater = 0; 7995 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7996 tcp->tcp_fuse_rcv_unread_cnt = 0; 7997 7998 /* Sodirect */ 7999 tcp->tcp_sodirect = NULL; 8000 8001 /* Initialize the header template */ 8002 if (tcp->tcp_ipversion == IPV4_VERSION) { 8003 err = tcp_header_init_ipv4(tcp); 8004 } else { 8005 err = tcp_header_init_ipv6(tcp); 8006 } 8007 if (err) 8008 return (err); 8009 8010 /* 8011 * Init the window scale to the max so tcp_rwnd_set() won't pare 8012 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 8013 */ 8014 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8015 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8016 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8017 8018 tcp->tcp_cork = B_FALSE; 8019 /* 8020 * Init the tcp_debug option. This value determines whether TCP 8021 * calls strlog() to print out debug messages. Doing this 8022 * initialization here means that this value is not inherited thru 8023 * tcp_reinit(). 8024 */ 8025 tcp->tcp_debug = tcps->tcps_dbg; 8026 8027 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8028 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8029 8030 return (0); 8031 } 8032 8033 /* 8034 * Initialize the IPv4 header. Loses any record of any IP options. 8035 */ 8036 static int 8037 tcp_header_init_ipv4(tcp_t *tcp) 8038 { 8039 tcph_t *tcph; 8040 uint32_t sum; 8041 conn_t *connp; 8042 tcp_stack_t *tcps = tcp->tcp_tcps; 8043 8044 /* 8045 * This is a simple initialization. If there's 8046 * already a template, it should never be too small, 8047 * so reuse it. Otherwise, allocate space for the new one. 8048 */ 8049 if (tcp->tcp_iphc == NULL) { 8050 ASSERT(tcp->tcp_iphc_len == 0); 8051 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8052 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8053 if (tcp->tcp_iphc == NULL) { 8054 tcp->tcp_iphc_len = 0; 8055 return (ENOMEM); 8056 } 8057 } 8058 8059 /* options are gone; may need a new label */ 8060 connp = tcp->tcp_connp; 8061 connp->conn_mlp_type = mlptSingle; 8062 connp->conn_ulp_labeled = !is_system_labeled(); 8063 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8064 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8065 tcp->tcp_ip6h = NULL; 8066 tcp->tcp_ipversion = IPV4_VERSION; 8067 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8068 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8069 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8070 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8071 tcp->tcp_ipha->ipha_version_and_hdr_length 8072 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8073 tcp->tcp_ipha->ipha_ident = 0; 8074 8075 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8076 tcp->tcp_tos = 0; 8077 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8078 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8079 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8080 8081 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8082 tcp->tcp_tcph = tcph; 8083 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8084 /* 8085 * IP wants our header length in the checksum field to 8086 * allow it to perform a single pseudo-header+checksum 8087 * calculation on behalf of TCP. 8088 * Include the adjustment for a source route once IP_OPTIONS is set. 8089 */ 8090 sum = sizeof (tcph_t) + tcp->tcp_sum; 8091 sum = (sum >> 16) + (sum & 0xFFFF); 8092 U16_TO_ABE16(sum, tcph->th_sum); 8093 return (0); 8094 } 8095 8096 /* 8097 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8098 */ 8099 static int 8100 tcp_header_init_ipv6(tcp_t *tcp) 8101 { 8102 tcph_t *tcph; 8103 uint32_t sum; 8104 conn_t *connp; 8105 tcp_stack_t *tcps = tcp->tcp_tcps; 8106 8107 /* 8108 * This is a simple initialization. If there's 8109 * already a template, it should never be too small, 8110 * so reuse it. Otherwise, allocate space for the new one. 8111 * Ensure that there is enough space to "downgrade" the tcp_t 8112 * to an IPv4 tcp_t. This requires having space for a full load 8113 * of IPv4 options, as well as a full load of TCP options 8114 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8115 * than a v6 header and a TCP header with a full load of TCP options 8116 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8117 * We want to avoid reallocation in the "downgraded" case when 8118 * processing outbound IPv4 options. 8119 */ 8120 if (tcp->tcp_iphc == NULL) { 8121 ASSERT(tcp->tcp_iphc_len == 0); 8122 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8123 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8124 if (tcp->tcp_iphc == NULL) { 8125 tcp->tcp_iphc_len = 0; 8126 return (ENOMEM); 8127 } 8128 } 8129 8130 /* options are gone; may need a new label */ 8131 connp = tcp->tcp_connp; 8132 connp->conn_mlp_type = mlptSingle; 8133 connp->conn_ulp_labeled = !is_system_labeled(); 8134 8135 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8136 tcp->tcp_ipversion = IPV6_VERSION; 8137 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8138 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8139 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8140 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8141 tcp->tcp_ipha = NULL; 8142 8143 /* Initialize the header template */ 8144 8145 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8146 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8147 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8148 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8149 8150 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8151 tcp->tcp_tcph = tcph; 8152 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8153 /* 8154 * IP wants our header length in the checksum field to 8155 * allow it to perform a single psuedo-header+checksum 8156 * calculation on behalf of TCP. 8157 * Include the adjustment for a source route when IPV6_RTHDR is set. 8158 */ 8159 sum = sizeof (tcph_t) + tcp->tcp_sum; 8160 sum = (sum >> 16) + (sum & 0xFFFF); 8161 U16_TO_ABE16(sum, tcph->th_sum); 8162 return (0); 8163 } 8164 8165 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8166 #define ICMP_MIN_TCP_HDR 8 8167 8168 /* 8169 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8170 * passed up by IP. The message is always received on the correct tcp_t. 8171 * Assumes that IP has pulled up everything up to and including the ICMP header. 8172 */ 8173 void 8174 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8175 { 8176 icmph_t *icmph; 8177 ipha_t *ipha; 8178 int iph_hdr_length; 8179 tcph_t *tcph; 8180 boolean_t ipsec_mctl = B_FALSE; 8181 boolean_t secure; 8182 mblk_t *first_mp = mp; 8183 int32_t new_mss; 8184 uint32_t ratio; 8185 size_t mp_size = MBLKL(mp); 8186 uint32_t seg_seq; 8187 tcp_stack_t *tcps = tcp->tcp_tcps; 8188 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8189 8190 /* Assume IP provides aligned packets - otherwise toss */ 8191 if (!OK_32PTR(mp->b_rptr)) { 8192 freemsg(mp); 8193 return; 8194 } 8195 8196 /* 8197 * Since ICMP errors are normal data marked with M_CTL when sent 8198 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8199 * packets starting with an ipsec_info_t, see ipsec_info.h. 8200 */ 8201 if ((mp_size == sizeof (ipsec_info_t)) && 8202 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8203 ASSERT(mp->b_cont != NULL); 8204 mp = mp->b_cont; 8205 /* IP should have done this */ 8206 ASSERT(OK_32PTR(mp->b_rptr)); 8207 mp_size = MBLKL(mp); 8208 ipsec_mctl = B_TRUE; 8209 } 8210 8211 /* 8212 * Verify that we have a complete outer IP header. If not, drop it. 8213 */ 8214 if (mp_size < sizeof (ipha_t)) { 8215 noticmpv4: 8216 freemsg(first_mp); 8217 return; 8218 } 8219 8220 ipha = (ipha_t *)mp->b_rptr; 8221 /* 8222 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8223 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8224 */ 8225 switch (IPH_HDR_VERSION(ipha)) { 8226 case IPV6_VERSION: 8227 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8228 return; 8229 case IPV4_VERSION: 8230 break; 8231 default: 8232 goto noticmpv4; 8233 } 8234 8235 /* Skip past the outer IP and ICMP headers */ 8236 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8237 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8238 /* 8239 * If we don't have the correct outer IP header length or if the ULP 8240 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8241 * send it upstream. 8242 */ 8243 if (iph_hdr_length < sizeof (ipha_t) || 8244 ipha->ipha_protocol != IPPROTO_ICMP || 8245 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8246 goto noticmpv4; 8247 } 8248 ipha = (ipha_t *)&icmph[1]; 8249 8250 /* Skip past the inner IP and find the ULP header */ 8251 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8252 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8253 /* 8254 * If we don't have the correct inner IP header length or if the ULP 8255 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8256 * bytes of TCP header, drop it. 8257 */ 8258 if (iph_hdr_length < sizeof (ipha_t) || 8259 ipha->ipha_protocol != IPPROTO_TCP || 8260 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8261 goto noticmpv4; 8262 } 8263 8264 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8265 if (ipsec_mctl) { 8266 secure = ipsec_in_is_secure(first_mp); 8267 } else { 8268 secure = B_FALSE; 8269 } 8270 if (secure) { 8271 /* 8272 * If we are willing to accept this in clear 8273 * we don't have to verify policy. 8274 */ 8275 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8276 if (!tcp_check_policy(tcp, first_mp, 8277 ipha, NULL, secure, ipsec_mctl)) { 8278 /* 8279 * tcp_check_policy called 8280 * ip_drop_packet() on failure. 8281 */ 8282 return; 8283 } 8284 } 8285 } 8286 } else if (ipsec_mctl) { 8287 /* 8288 * This is a hard_bound connection. IP has already 8289 * verified policy. We don't have to do it again. 8290 */ 8291 freeb(first_mp); 8292 first_mp = mp; 8293 ipsec_mctl = B_FALSE; 8294 } 8295 8296 seg_seq = ABE32_TO_U32(tcph->th_seq); 8297 /* 8298 * TCP SHOULD check that the TCP sequence number contained in 8299 * payload of the ICMP error message is within the range 8300 * SND.UNA <= SEG.SEQ < SND.NXT. 8301 */ 8302 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8303 /* 8304 * The ICMP message is bogus, just drop it. But if this is 8305 * an ICMP too big message, IP has already changed 8306 * the ire_max_frag to the bogus value. We need to change 8307 * it back. 8308 */ 8309 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8310 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8311 conn_t *connp = tcp->tcp_connp; 8312 ire_t *ire; 8313 int flag; 8314 8315 if (tcp->tcp_ipversion == IPV4_VERSION) { 8316 flag = tcp->tcp_ipha-> 8317 ipha_fragment_offset_and_flags; 8318 } else { 8319 flag = 0; 8320 } 8321 mutex_enter(&connp->conn_lock); 8322 if ((ire = connp->conn_ire_cache) != NULL) { 8323 mutex_enter(&ire->ire_lock); 8324 mutex_exit(&connp->conn_lock); 8325 ire->ire_max_frag = tcp->tcp_if_mtu; 8326 ire->ire_frag_flag |= flag; 8327 mutex_exit(&ire->ire_lock); 8328 } else { 8329 mutex_exit(&connp->conn_lock); 8330 } 8331 } 8332 goto noticmpv4; 8333 } 8334 8335 switch (icmph->icmph_type) { 8336 case ICMP_DEST_UNREACHABLE: 8337 switch (icmph->icmph_code) { 8338 case ICMP_FRAGMENTATION_NEEDED: 8339 /* 8340 * Reduce the MSS based on the new MTU. This will 8341 * eliminate any fragmentation locally. 8342 * N.B. There may well be some funny side-effects on 8343 * the local send policy and the remote receive policy. 8344 * Pending further research, we provide 8345 * tcp_ignore_path_mtu just in case this proves 8346 * disastrous somewhere. 8347 * 8348 * After updating the MSS, retransmit part of the 8349 * dropped segment using the new mss by calling 8350 * tcp_wput_data(). Need to adjust all those 8351 * params to make sure tcp_wput_data() work properly. 8352 */ 8353 if (tcps->tcps_ignore_path_mtu || 8354 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8355 break; 8356 8357 /* 8358 * Decrease the MSS by time stamp options 8359 * IP options and IPSEC options. tcp_hdr_len 8360 * includes time stamp option and IP option 8361 * length. Note that new_mss may be negative 8362 * if tcp_ipsec_overhead is large and the 8363 * icmph_du_mtu is the minimum value, which is 68. 8364 */ 8365 new_mss = ntohs(icmph->icmph_du_mtu) - 8366 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8367 8368 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8369 new_mss); 8370 8371 /* 8372 * Only update the MSS if the new one is 8373 * smaller than the previous one. This is 8374 * to avoid problems when getting multiple 8375 * ICMP errors for the same MTU. 8376 */ 8377 if (new_mss >= tcp->tcp_mss) 8378 break; 8379 8380 /* 8381 * Note that we are using the template header's DF 8382 * bit in the fast path sending. So we need to compare 8383 * the new mss with both tcps_mss_min and ip_pmtu_min. 8384 * And stop doing IPv4 PMTUd if new_mss is less than 8385 * MAX(tcps_mss_min, ip_pmtu_min). 8386 */ 8387 if (new_mss < tcps->tcps_mss_min || 8388 new_mss < ipst->ips_ip_pmtu_min) { 8389 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8390 0; 8391 } 8392 8393 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8394 ASSERT(ratio >= 1); 8395 tcp_mss_set(tcp, new_mss, B_TRUE); 8396 8397 /* 8398 * Make sure we have something to 8399 * send. 8400 */ 8401 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8402 (tcp->tcp_xmit_head != NULL)) { 8403 /* 8404 * Shrink tcp_cwnd in 8405 * proportion to the old MSS/new MSS. 8406 */ 8407 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8408 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8409 (tcp->tcp_unsent == 0)) { 8410 tcp->tcp_rexmit_max = tcp->tcp_fss; 8411 } else { 8412 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8413 } 8414 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8415 tcp->tcp_rexmit = B_TRUE; 8416 tcp->tcp_dupack_cnt = 0; 8417 tcp->tcp_snd_burst = TCP_CWND_SS; 8418 tcp_ss_rexmit(tcp); 8419 } 8420 break; 8421 case ICMP_PORT_UNREACHABLE: 8422 case ICMP_PROTOCOL_UNREACHABLE: 8423 switch (tcp->tcp_state) { 8424 case TCPS_SYN_SENT: 8425 case TCPS_SYN_RCVD: 8426 /* 8427 * ICMP can snipe away incipient 8428 * TCP connections as long as 8429 * seq number is same as initial 8430 * send seq number. 8431 */ 8432 if (seg_seq == tcp->tcp_iss) { 8433 (void) tcp_clean_death(tcp, 8434 ECONNREFUSED, 6); 8435 } 8436 break; 8437 } 8438 break; 8439 case ICMP_HOST_UNREACHABLE: 8440 case ICMP_NET_UNREACHABLE: 8441 /* Record the error in case we finally time out. */ 8442 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8443 tcp->tcp_client_errno = EHOSTUNREACH; 8444 else 8445 tcp->tcp_client_errno = ENETUNREACH; 8446 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8447 if (tcp->tcp_listener != NULL && 8448 tcp->tcp_listener->tcp_syn_defense) { 8449 /* 8450 * Ditch the half-open connection if we 8451 * suspect a SYN attack is under way. 8452 */ 8453 tcp_ip_ire_mark_advice(tcp); 8454 (void) tcp_clean_death(tcp, 8455 tcp->tcp_client_errno, 7); 8456 } 8457 } 8458 break; 8459 default: 8460 break; 8461 } 8462 break; 8463 case ICMP_SOURCE_QUENCH: { 8464 /* 8465 * use a global boolean to control 8466 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8467 * The default is false. 8468 */ 8469 if (tcp_icmp_source_quench) { 8470 /* 8471 * Reduce the sending rate as if we got a 8472 * retransmit timeout 8473 */ 8474 uint32_t npkt; 8475 8476 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8477 tcp->tcp_mss; 8478 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8479 tcp->tcp_cwnd = tcp->tcp_mss; 8480 tcp->tcp_cwnd_cnt = 0; 8481 } 8482 break; 8483 } 8484 } 8485 freemsg(first_mp); 8486 } 8487 8488 /* 8489 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8490 * error messages passed up by IP. 8491 * Assumes that IP has pulled up all the extension headers as well 8492 * as the ICMPv6 header. 8493 */ 8494 static void 8495 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8496 { 8497 icmp6_t *icmp6; 8498 ip6_t *ip6h; 8499 uint16_t iph_hdr_length; 8500 tcpha_t *tcpha; 8501 uint8_t *nexthdrp; 8502 uint32_t new_mss; 8503 uint32_t ratio; 8504 boolean_t secure; 8505 mblk_t *first_mp = mp; 8506 size_t mp_size; 8507 uint32_t seg_seq; 8508 tcp_stack_t *tcps = tcp->tcp_tcps; 8509 8510 /* 8511 * The caller has determined if this is an IPSEC_IN packet and 8512 * set ipsec_mctl appropriately (see tcp_icmp_error). 8513 */ 8514 if (ipsec_mctl) 8515 mp = mp->b_cont; 8516 8517 mp_size = MBLKL(mp); 8518 8519 /* 8520 * Verify that we have a complete IP header. If not, send it upstream. 8521 */ 8522 if (mp_size < sizeof (ip6_t)) { 8523 noticmpv6: 8524 freemsg(first_mp); 8525 return; 8526 } 8527 8528 /* 8529 * Verify this is an ICMPV6 packet, else send it upstream. 8530 */ 8531 ip6h = (ip6_t *)mp->b_rptr; 8532 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8533 iph_hdr_length = IPV6_HDR_LEN; 8534 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8535 &nexthdrp) || 8536 *nexthdrp != IPPROTO_ICMPV6) { 8537 goto noticmpv6; 8538 } 8539 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8540 ip6h = (ip6_t *)&icmp6[1]; 8541 /* 8542 * Verify if we have a complete ICMP and inner IP header. 8543 */ 8544 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8545 goto noticmpv6; 8546 8547 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8548 goto noticmpv6; 8549 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8550 /* 8551 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8552 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8553 * packet. 8554 */ 8555 if ((*nexthdrp != IPPROTO_TCP) || 8556 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8557 goto noticmpv6; 8558 } 8559 8560 /* 8561 * ICMP errors come on the right queue or come on 8562 * listener/global queue for detached connections and 8563 * get switched to the right queue. If it comes on the 8564 * right queue, policy check has already been done by IP 8565 * and thus free the first_mp without verifying the policy. 8566 * If it has come for a non-hard bound connection, we need 8567 * to verify policy as IP may not have done it. 8568 */ 8569 if (!tcp->tcp_hard_bound) { 8570 if (ipsec_mctl) { 8571 secure = ipsec_in_is_secure(first_mp); 8572 } else { 8573 secure = B_FALSE; 8574 } 8575 if (secure) { 8576 /* 8577 * If we are willing to accept this in clear 8578 * we don't have to verify policy. 8579 */ 8580 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8581 if (!tcp_check_policy(tcp, first_mp, 8582 NULL, ip6h, secure, ipsec_mctl)) { 8583 /* 8584 * tcp_check_policy called 8585 * ip_drop_packet() on failure. 8586 */ 8587 return; 8588 } 8589 } 8590 } 8591 } else if (ipsec_mctl) { 8592 /* 8593 * This is a hard_bound connection. IP has already 8594 * verified policy. We don't have to do it again. 8595 */ 8596 freeb(first_mp); 8597 first_mp = mp; 8598 ipsec_mctl = B_FALSE; 8599 } 8600 8601 seg_seq = ntohl(tcpha->tha_seq); 8602 /* 8603 * TCP SHOULD check that the TCP sequence number contained in 8604 * payload of the ICMP error message is within the range 8605 * SND.UNA <= SEG.SEQ < SND.NXT. 8606 */ 8607 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8608 /* 8609 * If the ICMP message is bogus, should we kill the 8610 * connection, or should we just drop the bogus ICMP 8611 * message? It would probably make more sense to just 8612 * drop the message so that if this one managed to get 8613 * in, the real connection should not suffer. 8614 */ 8615 goto noticmpv6; 8616 } 8617 8618 switch (icmp6->icmp6_type) { 8619 case ICMP6_PACKET_TOO_BIG: 8620 /* 8621 * Reduce the MSS based on the new MTU. This will 8622 * eliminate any fragmentation locally. 8623 * N.B. There may well be some funny side-effects on 8624 * the local send policy and the remote receive policy. 8625 * Pending further research, we provide 8626 * tcp_ignore_path_mtu just in case this proves 8627 * disastrous somewhere. 8628 * 8629 * After updating the MSS, retransmit part of the 8630 * dropped segment using the new mss by calling 8631 * tcp_wput_data(). Need to adjust all those 8632 * params to make sure tcp_wput_data() work properly. 8633 */ 8634 if (tcps->tcps_ignore_path_mtu) 8635 break; 8636 8637 /* 8638 * Decrease the MSS by time stamp options 8639 * IP options and IPSEC options. tcp_hdr_len 8640 * includes time stamp option and IP option 8641 * length. 8642 */ 8643 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8644 tcp->tcp_ipsec_overhead; 8645 8646 /* 8647 * Only update the MSS if the new one is 8648 * smaller than the previous one. This is 8649 * to avoid problems when getting multiple 8650 * ICMP errors for the same MTU. 8651 */ 8652 if (new_mss >= tcp->tcp_mss) 8653 break; 8654 8655 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8656 ASSERT(ratio >= 1); 8657 tcp_mss_set(tcp, new_mss, B_TRUE); 8658 8659 /* 8660 * Make sure we have something to 8661 * send. 8662 */ 8663 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8664 (tcp->tcp_xmit_head != NULL)) { 8665 /* 8666 * Shrink tcp_cwnd in 8667 * proportion to the old MSS/new MSS. 8668 */ 8669 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8670 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8671 (tcp->tcp_unsent == 0)) { 8672 tcp->tcp_rexmit_max = tcp->tcp_fss; 8673 } else { 8674 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8675 } 8676 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8677 tcp->tcp_rexmit = B_TRUE; 8678 tcp->tcp_dupack_cnt = 0; 8679 tcp->tcp_snd_burst = TCP_CWND_SS; 8680 tcp_ss_rexmit(tcp); 8681 } 8682 break; 8683 8684 case ICMP6_DST_UNREACH: 8685 switch (icmp6->icmp6_code) { 8686 case ICMP6_DST_UNREACH_NOPORT: 8687 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8688 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8689 (seg_seq == tcp->tcp_iss)) { 8690 (void) tcp_clean_death(tcp, 8691 ECONNREFUSED, 8); 8692 } 8693 break; 8694 8695 case ICMP6_DST_UNREACH_ADMIN: 8696 case ICMP6_DST_UNREACH_NOROUTE: 8697 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8698 case ICMP6_DST_UNREACH_ADDR: 8699 /* Record the error in case we finally time out. */ 8700 tcp->tcp_client_errno = EHOSTUNREACH; 8701 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8702 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8703 (seg_seq == tcp->tcp_iss)) { 8704 if (tcp->tcp_listener != NULL && 8705 tcp->tcp_listener->tcp_syn_defense) { 8706 /* 8707 * Ditch the half-open connection if we 8708 * suspect a SYN attack is under way. 8709 */ 8710 tcp_ip_ire_mark_advice(tcp); 8711 (void) tcp_clean_death(tcp, 8712 tcp->tcp_client_errno, 9); 8713 } 8714 } 8715 8716 8717 break; 8718 default: 8719 break; 8720 } 8721 break; 8722 8723 case ICMP6_PARAM_PROB: 8724 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8725 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8726 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8727 (uchar_t *)nexthdrp) { 8728 if (tcp->tcp_state == TCPS_SYN_SENT || 8729 tcp->tcp_state == TCPS_SYN_RCVD) { 8730 (void) tcp_clean_death(tcp, 8731 ECONNREFUSED, 10); 8732 } 8733 break; 8734 } 8735 break; 8736 8737 case ICMP6_TIME_EXCEEDED: 8738 default: 8739 break; 8740 } 8741 freemsg(first_mp); 8742 } 8743 8744 /* 8745 * Notify IP that we are having trouble with this connection. IP should 8746 * blow the IRE away and start over. 8747 */ 8748 static void 8749 tcp_ip_notify(tcp_t *tcp) 8750 { 8751 struct iocblk *iocp; 8752 ipid_t *ipid; 8753 mblk_t *mp; 8754 8755 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8756 if (tcp->tcp_ipversion == IPV6_VERSION) 8757 return; 8758 8759 mp = mkiocb(IP_IOCTL); 8760 if (mp == NULL) 8761 return; 8762 8763 iocp = (struct iocblk *)mp->b_rptr; 8764 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8765 8766 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8767 if (!mp->b_cont) { 8768 freeb(mp); 8769 return; 8770 } 8771 8772 ipid = (ipid_t *)mp->b_cont->b_rptr; 8773 mp->b_cont->b_wptr += iocp->ioc_count; 8774 bzero(ipid, sizeof (*ipid)); 8775 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8776 ipid->ipid_ire_type = IRE_CACHE; 8777 ipid->ipid_addr_offset = sizeof (ipid_t); 8778 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8779 /* 8780 * Note: in the case of source routing we want to blow away the 8781 * route to the first source route hop. 8782 */ 8783 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8784 sizeof (tcp->tcp_ipha->ipha_dst)); 8785 8786 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8787 } 8788 8789 /* Unlink and return any mblk that looks like it contains an ire */ 8790 static mblk_t * 8791 tcp_ire_mp(mblk_t **mpp) 8792 { 8793 mblk_t *mp = *mpp; 8794 mblk_t *prev_mp = NULL; 8795 8796 for (;;) { 8797 switch (DB_TYPE(mp)) { 8798 case IRE_DB_TYPE: 8799 case IRE_DB_REQ_TYPE: 8800 if (mp == *mpp) { 8801 *mpp = mp->b_cont; 8802 } else { 8803 prev_mp->b_cont = mp->b_cont; 8804 } 8805 mp->b_cont = NULL; 8806 return (mp); 8807 default: 8808 break; 8809 } 8810 prev_mp = mp; 8811 mp = mp->b_cont; 8812 if (mp == NULL) 8813 break; 8814 } 8815 return (mp); 8816 } 8817 8818 /* 8819 * Timer callback routine for keepalive probe. We do a fake resend of 8820 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8821 * check to see if we have heard anything from the other end for the last 8822 * RTO period. If we have, set the timer to expire for another 8823 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8824 * RTO << 1 and check again when it expires. Keep exponentially increasing 8825 * the timeout if we have not heard from the other side. If for more than 8826 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8827 * kill the connection unless the keepalive abort threshold is 0. In 8828 * that case, we will probe "forever." 8829 */ 8830 static void 8831 tcp_keepalive_killer(void *arg) 8832 { 8833 mblk_t *mp; 8834 conn_t *connp = (conn_t *)arg; 8835 tcp_t *tcp = connp->conn_tcp; 8836 int32_t firetime; 8837 int32_t idletime; 8838 int32_t ka_intrvl; 8839 tcp_stack_t *tcps = tcp->tcp_tcps; 8840 8841 tcp->tcp_ka_tid = 0; 8842 8843 if (tcp->tcp_fused) 8844 return; 8845 8846 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8847 ka_intrvl = tcp->tcp_ka_interval; 8848 8849 /* 8850 * Keepalive probe should only be sent if the application has not 8851 * done a close on the connection. 8852 */ 8853 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8854 return; 8855 } 8856 /* Timer fired too early, restart it. */ 8857 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8858 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8859 MSEC_TO_TICK(ka_intrvl)); 8860 return; 8861 } 8862 8863 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8864 /* 8865 * If we have not heard from the other side for a long 8866 * time, kill the connection unless the keepalive abort 8867 * threshold is 0. In that case, we will probe "forever." 8868 */ 8869 if (tcp->tcp_ka_abort_thres != 0 && 8870 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8871 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8872 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8873 tcp->tcp_client_errno : ETIMEDOUT, 11); 8874 return; 8875 } 8876 8877 if (tcp->tcp_snxt == tcp->tcp_suna && 8878 idletime >= ka_intrvl) { 8879 /* Fake resend of last ACKed byte. */ 8880 mblk_t *mp1 = allocb(1, BPRI_LO); 8881 8882 if (mp1 != NULL) { 8883 *mp1->b_wptr++ = '\0'; 8884 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8885 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8886 freeb(mp1); 8887 /* 8888 * if allocation failed, fall through to start the 8889 * timer back. 8890 */ 8891 if (mp != NULL) { 8892 tcp_send_data(tcp, tcp->tcp_wq, mp); 8893 BUMP_MIB(&tcps->tcps_mib, 8894 tcpTimKeepaliveProbe); 8895 if (tcp->tcp_ka_last_intrvl != 0) { 8896 int max; 8897 /* 8898 * We should probe again at least 8899 * in ka_intrvl, but not more than 8900 * tcp_rexmit_interval_max. 8901 */ 8902 max = tcps->tcps_rexmit_interval_max; 8903 firetime = MIN(ka_intrvl - 1, 8904 tcp->tcp_ka_last_intrvl << 1); 8905 if (firetime > max) 8906 firetime = max; 8907 } else { 8908 firetime = tcp->tcp_rto; 8909 } 8910 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8911 tcp_keepalive_killer, 8912 MSEC_TO_TICK(firetime)); 8913 tcp->tcp_ka_last_intrvl = firetime; 8914 return; 8915 } 8916 } 8917 } else { 8918 tcp->tcp_ka_last_intrvl = 0; 8919 } 8920 8921 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8922 if ((firetime = ka_intrvl - idletime) < 0) { 8923 firetime = ka_intrvl; 8924 } 8925 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8926 MSEC_TO_TICK(firetime)); 8927 } 8928 8929 int 8930 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8931 { 8932 queue_t *q = tcp->tcp_rq; 8933 int32_t mss = tcp->tcp_mss; 8934 int maxpsz; 8935 conn_t *connp = tcp->tcp_connp; 8936 8937 if (TCP_IS_DETACHED(tcp)) 8938 return (mss); 8939 if (tcp->tcp_fused) { 8940 maxpsz = tcp_fuse_maxpsz_set(tcp); 8941 mss = INFPSZ; 8942 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8943 /* 8944 * Set the sd_qn_maxpsz according to the socket send buffer 8945 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8946 * instruct the stream head to copyin user data into contiguous 8947 * kernel-allocated buffers without breaking it up into smaller 8948 * chunks. We round up the buffer size to the nearest SMSS. 8949 */ 8950 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8951 if (tcp->tcp_kssl_ctx == NULL) 8952 mss = INFPSZ; 8953 else 8954 mss = SSL3_MAX_RECORD_LEN; 8955 } else { 8956 /* 8957 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8958 * (and a multiple of the mss). This instructs the stream 8959 * head to break down larger than SMSS writes into SMSS- 8960 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8961 */ 8962 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8963 maxpsz = tcp->tcp_maxpsz * mss; 8964 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8965 maxpsz = tcp->tcp_xmit_hiwater/2; 8966 /* Round up to nearest mss */ 8967 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8968 } 8969 } 8970 8971 (void) proto_set_maxpsz(q, connp, maxpsz); 8972 if (!(IPCL_IS_NONSTR(connp))) { 8973 /* XXX do it in set_maxpsz()? */ 8974 tcp->tcp_wq->q_maxpsz = maxpsz; 8975 } 8976 8977 if (set_maxblk) 8978 (void) proto_set_tx_maxblk(q, connp, mss); 8979 return (mss); 8980 } 8981 8982 /* 8983 * Extract option values from a tcp header. We put any found values into the 8984 * tcpopt struct and return a bitmask saying which options were found. 8985 */ 8986 static int 8987 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8988 { 8989 uchar_t *endp; 8990 int len; 8991 uint32_t mss; 8992 uchar_t *up = (uchar_t *)tcph; 8993 int found = 0; 8994 int32_t sack_len; 8995 tcp_seq sack_begin, sack_end; 8996 tcp_t *tcp; 8997 8998 endp = up + TCP_HDR_LENGTH(tcph); 8999 up += TCP_MIN_HEADER_LENGTH; 9000 while (up < endp) { 9001 len = endp - up; 9002 switch (*up) { 9003 case TCPOPT_EOL: 9004 break; 9005 9006 case TCPOPT_NOP: 9007 up++; 9008 continue; 9009 9010 case TCPOPT_MAXSEG: 9011 if (len < TCPOPT_MAXSEG_LEN || 9012 up[1] != TCPOPT_MAXSEG_LEN) 9013 break; 9014 9015 mss = BE16_TO_U16(up+2); 9016 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9017 tcpopt->tcp_opt_mss = mss; 9018 found |= TCP_OPT_MSS_PRESENT; 9019 9020 up += TCPOPT_MAXSEG_LEN; 9021 continue; 9022 9023 case TCPOPT_WSCALE: 9024 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9025 break; 9026 9027 if (up[2] > TCP_MAX_WINSHIFT) 9028 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9029 else 9030 tcpopt->tcp_opt_wscale = up[2]; 9031 found |= TCP_OPT_WSCALE_PRESENT; 9032 9033 up += TCPOPT_WS_LEN; 9034 continue; 9035 9036 case TCPOPT_SACK_PERMITTED: 9037 if (len < TCPOPT_SACK_OK_LEN || 9038 up[1] != TCPOPT_SACK_OK_LEN) 9039 break; 9040 found |= TCP_OPT_SACK_OK_PRESENT; 9041 up += TCPOPT_SACK_OK_LEN; 9042 continue; 9043 9044 case TCPOPT_SACK: 9045 if (len <= 2 || up[1] <= 2 || len < up[1]) 9046 break; 9047 9048 /* If TCP is not interested in SACK blks... */ 9049 if ((tcp = tcpopt->tcp) == NULL) { 9050 up += up[1]; 9051 continue; 9052 } 9053 sack_len = up[1] - TCPOPT_HEADER_LEN; 9054 up += TCPOPT_HEADER_LEN; 9055 9056 /* 9057 * If the list is empty, allocate one and assume 9058 * nothing is sack'ed. 9059 */ 9060 ASSERT(tcp->tcp_sack_info != NULL); 9061 if (tcp->tcp_notsack_list == NULL) { 9062 tcp_notsack_update(&(tcp->tcp_notsack_list), 9063 tcp->tcp_suna, tcp->tcp_snxt, 9064 &(tcp->tcp_num_notsack_blk), 9065 &(tcp->tcp_cnt_notsack_list)); 9066 9067 /* 9068 * Make sure tcp_notsack_list is not NULL. 9069 * This happens when kmem_alloc(KM_NOSLEEP) 9070 * returns NULL. 9071 */ 9072 if (tcp->tcp_notsack_list == NULL) { 9073 up += sack_len; 9074 continue; 9075 } 9076 tcp->tcp_fack = tcp->tcp_suna; 9077 } 9078 9079 while (sack_len > 0) { 9080 if (up + 8 > endp) { 9081 up = endp; 9082 break; 9083 } 9084 sack_begin = BE32_TO_U32(up); 9085 up += 4; 9086 sack_end = BE32_TO_U32(up); 9087 up += 4; 9088 sack_len -= 8; 9089 /* 9090 * Bounds checking. Make sure the SACK 9091 * info is within tcp_suna and tcp_snxt. 9092 * If this SACK blk is out of bound, ignore 9093 * it but continue to parse the following 9094 * blks. 9095 */ 9096 if (SEQ_LEQ(sack_end, sack_begin) || 9097 SEQ_LT(sack_begin, tcp->tcp_suna) || 9098 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9099 continue; 9100 } 9101 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9102 sack_begin, sack_end, 9103 &(tcp->tcp_num_notsack_blk), 9104 &(tcp->tcp_cnt_notsack_list)); 9105 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9106 tcp->tcp_fack = sack_end; 9107 } 9108 } 9109 found |= TCP_OPT_SACK_PRESENT; 9110 continue; 9111 9112 case TCPOPT_TSTAMP: 9113 if (len < TCPOPT_TSTAMP_LEN || 9114 up[1] != TCPOPT_TSTAMP_LEN) 9115 break; 9116 9117 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9118 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9119 9120 found |= TCP_OPT_TSTAMP_PRESENT; 9121 9122 up += TCPOPT_TSTAMP_LEN; 9123 continue; 9124 9125 default: 9126 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9127 break; 9128 up += up[1]; 9129 continue; 9130 } 9131 break; 9132 } 9133 return (found); 9134 } 9135 9136 /* 9137 * Set the mss associated with a particular tcp based on its current value, 9138 * and a new one passed in. Observe minimums and maximums, and reset 9139 * other state variables that we want to view as multiples of mss. 9140 * 9141 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9142 * highwater marks etc. need to be initialized or adjusted. 9143 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9144 * packet arrives. 9145 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9146 * ICMP6_PACKET_TOO_BIG arrives. 9147 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9148 * to increase the MSS to use the extra bytes available. 9149 * 9150 * Callers except tcp_paws_check() ensure that they only reduce mss. 9151 */ 9152 static void 9153 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9154 { 9155 uint32_t mss_max; 9156 tcp_stack_t *tcps = tcp->tcp_tcps; 9157 9158 if (tcp->tcp_ipversion == IPV4_VERSION) 9159 mss_max = tcps->tcps_mss_max_ipv4; 9160 else 9161 mss_max = tcps->tcps_mss_max_ipv6; 9162 9163 if (mss < tcps->tcps_mss_min) 9164 mss = tcps->tcps_mss_min; 9165 if (mss > mss_max) 9166 mss = mss_max; 9167 /* 9168 * Unless naglim has been set by our client to 9169 * a non-mss value, force naglim to track mss. 9170 * This can help to aggregate small writes. 9171 */ 9172 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9173 tcp->tcp_naglim = mss; 9174 /* 9175 * TCP should be able to buffer at least 4 MSS data for obvious 9176 * performance reason. 9177 */ 9178 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9179 tcp->tcp_xmit_hiwater = mss << 2; 9180 9181 if (do_ss) { 9182 /* 9183 * Either the tcp_cwnd is as yet uninitialized, or mss is 9184 * changing due to a reduction in MTU, presumably as a 9185 * result of a new path component, reset cwnd to its 9186 * "initial" value, as a multiple of the new mss. 9187 */ 9188 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9189 } else { 9190 /* 9191 * Called by tcp_paws_check(), the mss increased 9192 * marginally to allow use of space previously taken 9193 * by the timestamp option. It would be inappropriate 9194 * to apply slow start or tcp_init_cwnd values to 9195 * tcp_cwnd, simply adjust to a multiple of the new mss. 9196 */ 9197 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9198 tcp->tcp_cwnd_cnt = 0; 9199 } 9200 tcp->tcp_mss = mss; 9201 (void) tcp_maxpsz_set(tcp, B_TRUE); 9202 } 9203 9204 /* For /dev/tcp aka AF_INET open */ 9205 static int 9206 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9207 { 9208 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9209 } 9210 9211 /* For /dev/tcp6 aka AF_INET6 open */ 9212 static int 9213 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9214 { 9215 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9216 } 9217 9218 static conn_t * 9219 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9220 boolean_t issocket, int *errorp) 9221 { 9222 tcp_t *tcp = NULL; 9223 conn_t *connp; 9224 int err; 9225 zoneid_t zoneid; 9226 tcp_stack_t *tcps; 9227 squeue_t *sqp; 9228 9229 ASSERT(errorp != NULL); 9230 /* 9231 * Find the proper zoneid and netstack. 9232 */ 9233 /* 9234 * Special case for install: miniroot needs to be able to 9235 * access files via NFS as though it were always in the 9236 * global zone. 9237 */ 9238 if (credp == kcred && nfs_global_client_only != 0) { 9239 zoneid = GLOBAL_ZONEID; 9240 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9241 netstack_tcp; 9242 ASSERT(tcps != NULL); 9243 } else { 9244 netstack_t *ns; 9245 9246 ns = netstack_find_by_cred(credp); 9247 ASSERT(ns != NULL); 9248 tcps = ns->netstack_tcp; 9249 ASSERT(tcps != NULL); 9250 9251 /* 9252 * For exclusive stacks we set the zoneid to zero 9253 * to make TCP operate as if in the global zone. 9254 */ 9255 if (tcps->tcps_netstack->netstack_stackid != 9256 GLOBAL_NETSTACKID) 9257 zoneid = GLOBAL_ZONEID; 9258 else 9259 zoneid = crgetzoneid(credp); 9260 } 9261 /* 9262 * For stackid zero this is done from strplumb.c, but 9263 * non-zero stackids are handled here. 9264 */ 9265 if (tcps->tcps_g_q == NULL && 9266 tcps->tcps_netstack->netstack_stackid != 9267 GLOBAL_NETSTACKID) { 9268 tcp_g_q_setup(tcps); 9269 } 9270 9271 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9272 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9273 /* 9274 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9275 * so we drop it by one. 9276 */ 9277 netstack_rele(tcps->tcps_netstack); 9278 if (connp == NULL) { 9279 *errorp = ENOSR; 9280 return (NULL); 9281 } 9282 connp->conn_sqp = sqp; 9283 connp->conn_initial_sqp = connp->conn_sqp; 9284 tcp = connp->conn_tcp; 9285 9286 if (isv6) { 9287 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9288 connp->conn_send = ip_output_v6; 9289 connp->conn_af_isv6 = B_TRUE; 9290 connp->conn_pkt_isv6 = B_TRUE; 9291 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9292 tcp->tcp_ipversion = IPV6_VERSION; 9293 tcp->tcp_family = AF_INET6; 9294 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9295 } else { 9296 connp->conn_flags |= IPCL_TCP4; 9297 connp->conn_send = ip_output; 9298 connp->conn_af_isv6 = B_FALSE; 9299 connp->conn_pkt_isv6 = B_FALSE; 9300 tcp->tcp_ipversion = IPV4_VERSION; 9301 tcp->tcp_family = AF_INET; 9302 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9303 } 9304 9305 /* 9306 * TCP keeps a copy of cred for cache locality reasons but 9307 * we put a reference only once. If connp->conn_cred 9308 * becomes invalid, tcp_cred should also be set to NULL. 9309 */ 9310 tcp->tcp_cred = connp->conn_cred = credp; 9311 crhold(connp->conn_cred); 9312 tcp->tcp_cpid = curproc->p_pid; 9313 tcp->tcp_open_time = lbolt64; 9314 connp->conn_zoneid = zoneid; 9315 connp->conn_mlp_type = mlptSingle; 9316 connp->conn_ulp_labeled = !is_system_labeled(); 9317 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9318 ASSERT(tcp->tcp_tcps == tcps); 9319 9320 /* 9321 * If the caller has the process-wide flag set, then default to MAC 9322 * exempt mode. This allows read-down to unlabeled hosts. 9323 */ 9324 if (getpflags(NET_MAC_AWARE, credp) != 0) 9325 connp->conn_mac_exempt = B_TRUE; 9326 9327 connp->conn_dev = NULL; 9328 if (issocket) { 9329 connp->conn_flags |= IPCL_SOCKET; 9330 tcp->tcp_issocket = 1; 9331 } 9332 9333 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9334 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9335 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9336 9337 /* Non-zero default values */ 9338 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9339 9340 if (q == NULL) { 9341 /* 9342 * Create a helper stream for non-STREAMS socket. 9343 */ 9344 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9345 if (err != 0) { 9346 ip1dbg(("tcp_create_common: create of IP helper stream " 9347 "failed\n")); 9348 CONN_DEC_REF(connp); 9349 *errorp = err; 9350 return (NULL); 9351 } 9352 q = connp->conn_rq; 9353 } else { 9354 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9355 } 9356 9357 SOCK_CONNID_INIT(tcp->tcp_connid); 9358 err = tcp_init(tcp, q); 9359 if (err != 0) { 9360 CONN_DEC_REF(connp); 9361 *errorp = err; 9362 return (NULL); 9363 } 9364 9365 return (connp); 9366 } 9367 9368 static int 9369 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9370 boolean_t isv6) 9371 { 9372 tcp_t *tcp = NULL; 9373 conn_t *connp = NULL; 9374 int err; 9375 vmem_t *minor_arena = NULL; 9376 dev_t conn_dev; 9377 boolean_t issocket; 9378 9379 if (q->q_ptr != NULL) 9380 return (0); 9381 9382 if (sflag == MODOPEN) 9383 return (EINVAL); 9384 9385 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9386 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9387 minor_arena = ip_minor_arena_la; 9388 } else { 9389 /* 9390 * Either minor numbers in the large arena were exhausted 9391 * or a non socket application is doing the open. 9392 * Try to allocate from the small arena. 9393 */ 9394 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9395 return (EBUSY); 9396 } 9397 minor_arena = ip_minor_arena_sa; 9398 } 9399 9400 ASSERT(minor_arena != NULL); 9401 9402 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9403 9404 if (flag & SO_FALLBACK) { 9405 /* 9406 * Non streams socket needs a stream to fallback to 9407 */ 9408 RD(q)->q_ptr = (void *)conn_dev; 9409 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9410 WR(q)->q_ptr = (void *)minor_arena; 9411 qprocson(q); 9412 return (0); 9413 } else if (flag & SO_ACCEPTOR) { 9414 q->q_qinfo = &tcp_acceptor_rinit; 9415 /* 9416 * the conn_dev and minor_arena will be subsequently used by 9417 * tcp_wput_accept() and tcpclose_accept() to figure out the 9418 * minor device number for this connection from the q_ptr. 9419 */ 9420 RD(q)->q_ptr = (void *)conn_dev; 9421 WR(q)->q_qinfo = &tcp_acceptor_winit; 9422 WR(q)->q_ptr = (void *)minor_arena; 9423 qprocson(q); 9424 return (0); 9425 } 9426 9427 issocket = flag & SO_SOCKSTR; 9428 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9429 9430 if (connp == NULL) { 9431 inet_minor_free(minor_arena, conn_dev); 9432 q->q_ptr = WR(q)->q_ptr = NULL; 9433 return (err); 9434 } 9435 9436 q->q_ptr = WR(q)->q_ptr = connp; 9437 9438 connp->conn_dev = conn_dev; 9439 connp->conn_minor_arena = minor_arena; 9440 9441 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9442 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9443 9444 if (issocket) { 9445 WR(q)->q_qinfo = &tcp_sock_winit; 9446 } else { 9447 tcp = connp->conn_tcp; 9448 #ifdef _ILP32 9449 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9450 #else 9451 tcp->tcp_acceptor_id = conn_dev; 9452 #endif /* _ILP32 */ 9453 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9454 } 9455 9456 /* 9457 * Put the ref for TCP. Ref for IP was already put 9458 * by ipcl_conn_create. Also Make the conn_t globally 9459 * visible to walkers 9460 */ 9461 mutex_enter(&connp->conn_lock); 9462 CONN_INC_REF_LOCKED(connp); 9463 ASSERT(connp->conn_ref == 2); 9464 connp->conn_state_flags &= ~CONN_INCIPIENT; 9465 mutex_exit(&connp->conn_lock); 9466 9467 qprocson(q); 9468 return (0); 9469 } 9470 9471 /* 9472 * Some TCP options can be "set" by requesting them in the option 9473 * buffer. This is needed for XTI feature test though we do not 9474 * allow it in general. We interpret that this mechanism is more 9475 * applicable to OSI protocols and need not be allowed in general. 9476 * This routine filters out options for which it is not allowed (most) 9477 * and lets through those (few) for which it is. [ The XTI interface 9478 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9479 * ever implemented will have to be allowed here ]. 9480 */ 9481 static boolean_t 9482 tcp_allow_connopt_set(int level, int name) 9483 { 9484 9485 switch (level) { 9486 case IPPROTO_TCP: 9487 switch (name) { 9488 case TCP_NODELAY: 9489 return (B_TRUE); 9490 default: 9491 return (B_FALSE); 9492 } 9493 /*NOTREACHED*/ 9494 default: 9495 return (B_FALSE); 9496 } 9497 /*NOTREACHED*/ 9498 } 9499 9500 /* 9501 * this routine gets default values of certain options whose default 9502 * values are maintained by protocol specific code 9503 */ 9504 /* ARGSUSED */ 9505 int 9506 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9507 { 9508 int32_t *i1 = (int32_t *)ptr; 9509 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9510 9511 switch (level) { 9512 case IPPROTO_TCP: 9513 switch (name) { 9514 case TCP_NOTIFY_THRESHOLD: 9515 *i1 = tcps->tcps_ip_notify_interval; 9516 break; 9517 case TCP_ABORT_THRESHOLD: 9518 *i1 = tcps->tcps_ip_abort_interval; 9519 break; 9520 case TCP_CONN_NOTIFY_THRESHOLD: 9521 *i1 = tcps->tcps_ip_notify_cinterval; 9522 break; 9523 case TCP_CONN_ABORT_THRESHOLD: 9524 *i1 = tcps->tcps_ip_abort_cinterval; 9525 break; 9526 default: 9527 return (-1); 9528 } 9529 break; 9530 case IPPROTO_IP: 9531 switch (name) { 9532 case IP_TTL: 9533 *i1 = tcps->tcps_ipv4_ttl; 9534 break; 9535 default: 9536 return (-1); 9537 } 9538 break; 9539 case IPPROTO_IPV6: 9540 switch (name) { 9541 case IPV6_UNICAST_HOPS: 9542 *i1 = tcps->tcps_ipv6_hoplimit; 9543 break; 9544 default: 9545 return (-1); 9546 } 9547 break; 9548 default: 9549 return (-1); 9550 } 9551 return (sizeof (int)); 9552 } 9553 9554 static int 9555 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9556 { 9557 int *i1 = (int *)ptr; 9558 tcp_t *tcp = connp->conn_tcp; 9559 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9560 9561 switch (level) { 9562 case SOL_SOCKET: 9563 switch (name) { 9564 case SO_LINGER: { 9565 struct linger *lgr = (struct linger *)ptr; 9566 9567 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9568 lgr->l_linger = tcp->tcp_lingertime; 9569 } 9570 return (sizeof (struct linger)); 9571 case SO_DEBUG: 9572 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9573 break; 9574 case SO_KEEPALIVE: 9575 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9576 break; 9577 case SO_DONTROUTE: 9578 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9579 break; 9580 case SO_USELOOPBACK: 9581 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9582 break; 9583 case SO_BROADCAST: 9584 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9585 break; 9586 case SO_REUSEADDR: 9587 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9588 break; 9589 case SO_OOBINLINE: 9590 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9591 break; 9592 case SO_DGRAM_ERRIND: 9593 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9594 break; 9595 case SO_TYPE: 9596 *i1 = SOCK_STREAM; 9597 break; 9598 case SO_SNDBUF: 9599 *i1 = tcp->tcp_xmit_hiwater; 9600 break; 9601 case SO_RCVBUF: 9602 *i1 = tcp->tcp_recv_hiwater; 9603 break; 9604 case SO_SND_COPYAVOID: 9605 *i1 = tcp->tcp_snd_zcopy_on ? 9606 SO_SND_COPYAVOID : 0; 9607 break; 9608 case SO_ALLZONES: 9609 *i1 = connp->conn_allzones ? 1 : 0; 9610 break; 9611 case SO_ANON_MLP: 9612 *i1 = connp->conn_anon_mlp; 9613 break; 9614 case SO_MAC_EXEMPT: 9615 *i1 = connp->conn_mac_exempt; 9616 break; 9617 case SO_EXCLBIND: 9618 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9619 break; 9620 case SO_PROTOTYPE: 9621 *i1 = IPPROTO_TCP; 9622 break; 9623 case SO_DOMAIN: 9624 *i1 = tcp->tcp_family; 9625 break; 9626 case SO_ACCEPTCONN: 9627 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9628 default: 9629 return (-1); 9630 } 9631 break; 9632 case IPPROTO_TCP: 9633 switch (name) { 9634 case TCP_NODELAY: 9635 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9636 break; 9637 case TCP_MAXSEG: 9638 *i1 = tcp->tcp_mss; 9639 break; 9640 case TCP_NOTIFY_THRESHOLD: 9641 *i1 = (int)tcp->tcp_first_timer_threshold; 9642 break; 9643 case TCP_ABORT_THRESHOLD: 9644 *i1 = tcp->tcp_second_timer_threshold; 9645 break; 9646 case TCP_CONN_NOTIFY_THRESHOLD: 9647 *i1 = tcp->tcp_first_ctimer_threshold; 9648 break; 9649 case TCP_CONN_ABORT_THRESHOLD: 9650 *i1 = tcp->tcp_second_ctimer_threshold; 9651 break; 9652 case TCP_RECVDSTADDR: 9653 *i1 = tcp->tcp_recvdstaddr; 9654 break; 9655 case TCP_ANONPRIVBIND: 9656 *i1 = tcp->tcp_anon_priv_bind; 9657 break; 9658 case TCP_EXCLBIND: 9659 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9660 break; 9661 case TCP_INIT_CWND: 9662 *i1 = tcp->tcp_init_cwnd; 9663 break; 9664 case TCP_KEEPALIVE_THRESHOLD: 9665 *i1 = tcp->tcp_ka_interval; 9666 break; 9667 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9668 *i1 = tcp->tcp_ka_abort_thres; 9669 break; 9670 case TCP_CORK: 9671 *i1 = tcp->tcp_cork; 9672 break; 9673 default: 9674 return (-1); 9675 } 9676 break; 9677 case IPPROTO_IP: 9678 if (tcp->tcp_family != AF_INET) 9679 return (-1); 9680 switch (name) { 9681 case IP_OPTIONS: 9682 case T_IP_OPTIONS: { 9683 /* 9684 * This is compatible with BSD in that in only return 9685 * the reverse source route with the final destination 9686 * as the last entry. The first 4 bytes of the option 9687 * will contain the final destination. 9688 */ 9689 int opt_len; 9690 9691 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9692 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9693 ASSERT(opt_len >= 0); 9694 /* Caller ensures enough space */ 9695 if (opt_len > 0) { 9696 /* 9697 * TODO: Do we have to handle getsockopt on an 9698 * initiator as well? 9699 */ 9700 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9701 } 9702 return (0); 9703 } 9704 case IP_TOS: 9705 case T_IP_TOS: 9706 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9707 break; 9708 case IP_TTL: 9709 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9710 break; 9711 case IP_NEXTHOP: 9712 /* Handled at IP level */ 9713 return (-EINVAL); 9714 default: 9715 return (-1); 9716 } 9717 break; 9718 case IPPROTO_IPV6: 9719 /* 9720 * IPPROTO_IPV6 options are only supported for sockets 9721 * that are using IPv6 on the wire. 9722 */ 9723 if (tcp->tcp_ipversion != IPV6_VERSION) { 9724 return (-1); 9725 } 9726 switch (name) { 9727 case IPV6_UNICAST_HOPS: 9728 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9729 break; /* goto sizeof (int) option return */ 9730 case IPV6_BOUND_IF: 9731 /* Zero if not set */ 9732 *i1 = tcp->tcp_bound_if; 9733 break; /* goto sizeof (int) option return */ 9734 case IPV6_RECVPKTINFO: 9735 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9736 *i1 = 1; 9737 else 9738 *i1 = 0; 9739 break; /* goto sizeof (int) option return */ 9740 case IPV6_RECVTCLASS: 9741 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9742 *i1 = 1; 9743 else 9744 *i1 = 0; 9745 break; /* goto sizeof (int) option return */ 9746 case IPV6_RECVHOPLIMIT: 9747 if (tcp->tcp_ipv6_recvancillary & 9748 TCP_IPV6_RECVHOPLIMIT) 9749 *i1 = 1; 9750 else 9751 *i1 = 0; 9752 break; /* goto sizeof (int) option return */ 9753 case IPV6_RECVHOPOPTS: 9754 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9755 *i1 = 1; 9756 else 9757 *i1 = 0; 9758 break; /* goto sizeof (int) option return */ 9759 case IPV6_RECVDSTOPTS: 9760 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9761 *i1 = 1; 9762 else 9763 *i1 = 0; 9764 break; /* goto sizeof (int) option return */ 9765 case _OLD_IPV6_RECVDSTOPTS: 9766 if (tcp->tcp_ipv6_recvancillary & 9767 TCP_OLD_IPV6_RECVDSTOPTS) 9768 *i1 = 1; 9769 else 9770 *i1 = 0; 9771 break; /* goto sizeof (int) option return */ 9772 case IPV6_RECVRTHDR: 9773 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9774 *i1 = 1; 9775 else 9776 *i1 = 0; 9777 break; /* goto sizeof (int) option return */ 9778 case IPV6_RECVRTHDRDSTOPTS: 9779 if (tcp->tcp_ipv6_recvancillary & 9780 TCP_IPV6_RECVRTDSTOPTS) 9781 *i1 = 1; 9782 else 9783 *i1 = 0; 9784 break; /* goto sizeof (int) option return */ 9785 case IPV6_PKTINFO: { 9786 /* XXX assumes that caller has room for max size! */ 9787 struct in6_pktinfo *pkti; 9788 9789 pkti = (struct in6_pktinfo *)ptr; 9790 if (ipp->ipp_fields & IPPF_IFINDEX) 9791 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9792 else 9793 pkti->ipi6_ifindex = 0; 9794 if (ipp->ipp_fields & IPPF_ADDR) 9795 pkti->ipi6_addr = ipp->ipp_addr; 9796 else 9797 pkti->ipi6_addr = ipv6_all_zeros; 9798 return (sizeof (struct in6_pktinfo)); 9799 } 9800 case IPV6_TCLASS: 9801 if (ipp->ipp_fields & IPPF_TCLASS) 9802 *i1 = ipp->ipp_tclass; 9803 else 9804 *i1 = IPV6_FLOW_TCLASS( 9805 IPV6_DEFAULT_VERS_AND_FLOW); 9806 break; /* goto sizeof (int) option return */ 9807 case IPV6_NEXTHOP: { 9808 sin6_t *sin6 = (sin6_t *)ptr; 9809 9810 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9811 return (0); 9812 *sin6 = sin6_null; 9813 sin6->sin6_family = AF_INET6; 9814 sin6->sin6_addr = ipp->ipp_nexthop; 9815 return (sizeof (sin6_t)); 9816 } 9817 case IPV6_HOPOPTS: 9818 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9819 return (0); 9820 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9821 return (0); 9822 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9823 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9824 if (tcp->tcp_label_len > 0) { 9825 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9826 ptr[1] = (ipp->ipp_hopoptslen - 9827 tcp->tcp_label_len + 7) / 8 - 1; 9828 } 9829 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9830 case IPV6_RTHDRDSTOPTS: 9831 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9832 return (0); 9833 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9834 return (ipp->ipp_rtdstoptslen); 9835 case IPV6_RTHDR: 9836 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9837 return (0); 9838 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9839 return (ipp->ipp_rthdrlen); 9840 case IPV6_DSTOPTS: 9841 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9842 return (0); 9843 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9844 return (ipp->ipp_dstoptslen); 9845 case IPV6_SRC_PREFERENCES: 9846 return (ip6_get_src_preferences(connp, 9847 (uint32_t *)ptr)); 9848 case IPV6_PATHMTU: { 9849 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9850 9851 if (tcp->tcp_state < TCPS_ESTABLISHED) 9852 return (-1); 9853 9854 return (ip_fill_mtuinfo(&connp->conn_remv6, 9855 connp->conn_fport, mtuinfo, 9856 connp->conn_netstack)); 9857 } 9858 default: 9859 return (-1); 9860 } 9861 break; 9862 default: 9863 return (-1); 9864 } 9865 return (sizeof (int)); 9866 } 9867 9868 /* 9869 * TCP routine to get the values of options. 9870 */ 9871 int 9872 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9873 { 9874 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9875 } 9876 9877 /* returns UNIX error, the optlen is a value-result arg */ 9878 int 9879 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9880 void *optvalp, socklen_t *optlen, cred_t *cr) 9881 { 9882 conn_t *connp = (conn_t *)proto_handle; 9883 squeue_t *sqp = connp->conn_sqp; 9884 int error; 9885 t_uscalar_t max_optbuf_len; 9886 void *optvalp_buf; 9887 int len; 9888 9889 ASSERT(connp->conn_upper_handle != NULL); 9890 9891 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9892 tcp_opt_obj.odb_opt_des_arr, 9893 tcp_opt_obj.odb_opt_arr_cnt, 9894 tcp_opt_obj.odb_topmost_tpiprovider, 9895 B_FALSE, B_TRUE, cr); 9896 if (error != 0) { 9897 if (error < 0) { 9898 error = proto_tlitosyserr(-error); 9899 } 9900 return (error); 9901 } 9902 9903 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9904 9905 error = squeue_synch_enter(sqp, connp, 0); 9906 if (error == ENOMEM) { 9907 return (ENOMEM); 9908 } 9909 9910 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9911 squeue_synch_exit(sqp, connp); 9912 9913 if (len < 0) { 9914 /* 9915 * Pass on to IP 9916 */ 9917 kmem_free(optvalp_buf, max_optbuf_len); 9918 return (ip_get_options(connp, level, option_name, 9919 optvalp, optlen, cr)); 9920 } else { 9921 /* 9922 * update optlen and copy option value 9923 */ 9924 t_uscalar_t size = MIN(len, *optlen); 9925 bcopy(optvalp_buf, optvalp, size); 9926 bcopy(&size, optlen, sizeof (size)); 9927 9928 kmem_free(optvalp_buf, max_optbuf_len); 9929 return (0); 9930 } 9931 } 9932 9933 /* 9934 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9935 * Parameters are assumed to be verified by the caller. 9936 */ 9937 /* ARGSUSED */ 9938 int 9939 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9940 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9941 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9942 { 9943 tcp_t *tcp = connp->conn_tcp; 9944 int *i1 = (int *)invalp; 9945 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9946 boolean_t checkonly; 9947 int reterr; 9948 tcp_stack_t *tcps = tcp->tcp_tcps; 9949 9950 switch (optset_context) { 9951 case SETFN_OPTCOM_CHECKONLY: 9952 checkonly = B_TRUE; 9953 /* 9954 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9955 * inlen != 0 implies value supplied and 9956 * we have to "pretend" to set it. 9957 * inlen == 0 implies that there is no 9958 * value part in T_CHECK request and just validation 9959 * done elsewhere should be enough, we just return here. 9960 */ 9961 if (inlen == 0) { 9962 *outlenp = 0; 9963 return (0); 9964 } 9965 break; 9966 case SETFN_OPTCOM_NEGOTIATE: 9967 checkonly = B_FALSE; 9968 break; 9969 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9970 case SETFN_CONN_NEGOTIATE: 9971 checkonly = B_FALSE; 9972 /* 9973 * Negotiating local and "association-related" options 9974 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9975 * primitives is allowed by XTI, but we choose 9976 * to not implement this style negotiation for Internet 9977 * protocols (We interpret it is a must for OSI world but 9978 * optional for Internet protocols) for all options. 9979 * [ Will do only for the few options that enable test 9980 * suites that our XTI implementation of this feature 9981 * works for transports that do allow it ] 9982 */ 9983 if (!tcp_allow_connopt_set(level, name)) { 9984 *outlenp = 0; 9985 return (EINVAL); 9986 } 9987 break; 9988 default: 9989 /* 9990 * We should never get here 9991 */ 9992 *outlenp = 0; 9993 return (EINVAL); 9994 } 9995 9996 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9997 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9998 9999 /* 10000 * For TCP, we should have no ancillary data sent down 10001 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 10002 * has to be zero. 10003 */ 10004 ASSERT(thisdg_attrs == NULL); 10005 10006 /* 10007 * For fixed length options, no sanity check 10008 * of passed in length is done. It is assumed *_optcom_req() 10009 * routines do the right thing. 10010 */ 10011 switch (level) { 10012 case SOL_SOCKET: 10013 switch (name) { 10014 case SO_LINGER: { 10015 struct linger *lgr = (struct linger *)invalp; 10016 10017 if (!checkonly) { 10018 if (lgr->l_onoff) { 10019 tcp->tcp_linger = 1; 10020 tcp->tcp_lingertime = lgr->l_linger; 10021 } else { 10022 tcp->tcp_linger = 0; 10023 tcp->tcp_lingertime = 0; 10024 } 10025 /* struct copy */ 10026 *(struct linger *)outvalp = *lgr; 10027 } else { 10028 if (!lgr->l_onoff) { 10029 ((struct linger *) 10030 outvalp)->l_onoff = 0; 10031 ((struct linger *) 10032 outvalp)->l_linger = 0; 10033 } else { 10034 /* struct copy */ 10035 *(struct linger *)outvalp = *lgr; 10036 } 10037 } 10038 *outlenp = sizeof (struct linger); 10039 return (0); 10040 } 10041 case SO_DEBUG: 10042 if (!checkonly) 10043 tcp->tcp_debug = onoff; 10044 break; 10045 case SO_KEEPALIVE: 10046 if (checkonly) { 10047 /* check only case */ 10048 break; 10049 } 10050 10051 if (!onoff) { 10052 if (tcp->tcp_ka_enabled) { 10053 if (tcp->tcp_ka_tid != 0) { 10054 (void) TCP_TIMER_CANCEL(tcp, 10055 tcp->tcp_ka_tid); 10056 tcp->tcp_ka_tid = 0; 10057 } 10058 tcp->tcp_ka_enabled = 0; 10059 } 10060 break; 10061 } 10062 if (!tcp->tcp_ka_enabled) { 10063 /* Crank up the keepalive timer */ 10064 tcp->tcp_ka_last_intrvl = 0; 10065 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10066 tcp_keepalive_killer, 10067 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10068 tcp->tcp_ka_enabled = 1; 10069 } 10070 break; 10071 case SO_DONTROUTE: 10072 /* 10073 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10074 * only of interest to IP. We track them here only so 10075 * that we can report their current value. 10076 */ 10077 if (!checkonly) { 10078 tcp->tcp_dontroute = onoff; 10079 tcp->tcp_connp->conn_dontroute = onoff; 10080 } 10081 break; 10082 case SO_USELOOPBACK: 10083 if (!checkonly) { 10084 tcp->tcp_useloopback = onoff; 10085 tcp->tcp_connp->conn_loopback = onoff; 10086 } 10087 break; 10088 case SO_BROADCAST: 10089 if (!checkonly) { 10090 tcp->tcp_broadcast = onoff; 10091 tcp->tcp_connp->conn_broadcast = onoff; 10092 } 10093 break; 10094 case SO_REUSEADDR: 10095 if (!checkonly) { 10096 tcp->tcp_reuseaddr = onoff; 10097 tcp->tcp_connp->conn_reuseaddr = onoff; 10098 } 10099 break; 10100 case SO_OOBINLINE: 10101 if (!checkonly) { 10102 tcp->tcp_oobinline = onoff; 10103 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10104 proto_set_rx_oob_opt(connp, onoff); 10105 } 10106 break; 10107 case SO_DGRAM_ERRIND: 10108 if (!checkonly) 10109 tcp->tcp_dgram_errind = onoff; 10110 break; 10111 case SO_SNDBUF: { 10112 if (*i1 > tcps->tcps_max_buf) { 10113 *outlenp = 0; 10114 return (ENOBUFS); 10115 } 10116 if (checkonly) 10117 break; 10118 10119 tcp->tcp_xmit_hiwater = *i1; 10120 if (tcps->tcps_snd_lowat_fraction != 0) 10121 tcp->tcp_xmit_lowater = 10122 tcp->tcp_xmit_hiwater / 10123 tcps->tcps_snd_lowat_fraction; 10124 (void) tcp_maxpsz_set(tcp, B_TRUE); 10125 /* 10126 * If we are flow-controlled, recheck the condition. 10127 * There are apps that increase SO_SNDBUF size when 10128 * flow-controlled (EWOULDBLOCK), and expect the flow 10129 * control condition to be lifted right away. 10130 */ 10131 mutex_enter(&tcp->tcp_non_sq_lock); 10132 if (tcp->tcp_flow_stopped && 10133 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10134 tcp_clrqfull(tcp); 10135 } 10136 mutex_exit(&tcp->tcp_non_sq_lock); 10137 break; 10138 } 10139 case SO_RCVBUF: 10140 if (*i1 > tcps->tcps_max_buf) { 10141 *outlenp = 0; 10142 return (ENOBUFS); 10143 } 10144 /* Silently ignore zero */ 10145 if (!checkonly && *i1 != 0) { 10146 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10147 (void) tcp_rwnd_set(tcp, *i1); 10148 } 10149 /* 10150 * XXX should we return the rwnd here 10151 * and tcp_opt_get ? 10152 */ 10153 break; 10154 case SO_SND_COPYAVOID: 10155 if (!checkonly) { 10156 /* we only allow enable at most once for now */ 10157 if (tcp->tcp_loopback || 10158 (tcp->tcp_kssl_ctx != NULL) || 10159 (!tcp->tcp_snd_zcopy_aware && 10160 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10161 *outlenp = 0; 10162 return (EOPNOTSUPP); 10163 } 10164 tcp->tcp_snd_zcopy_aware = 1; 10165 } 10166 break; 10167 case SO_RCVTIMEO: 10168 case SO_SNDTIMEO: 10169 /* 10170 * Pass these two options in order for third part 10171 * protocol usage. Here just return directly. 10172 */ 10173 return (0); 10174 case SO_ALLZONES: 10175 /* Pass option along to IP level for handling */ 10176 return (-EINVAL); 10177 case SO_ANON_MLP: 10178 /* Pass option along to IP level for handling */ 10179 return (-EINVAL); 10180 case SO_MAC_EXEMPT: 10181 /* Pass option along to IP level for handling */ 10182 return (-EINVAL); 10183 case SO_EXCLBIND: 10184 if (!checkonly) 10185 tcp->tcp_exclbind = onoff; 10186 break; 10187 default: 10188 *outlenp = 0; 10189 return (EINVAL); 10190 } 10191 break; 10192 case IPPROTO_TCP: 10193 switch (name) { 10194 case TCP_NODELAY: 10195 if (!checkonly) 10196 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10197 break; 10198 case TCP_NOTIFY_THRESHOLD: 10199 if (!checkonly) 10200 tcp->tcp_first_timer_threshold = *i1; 10201 break; 10202 case TCP_ABORT_THRESHOLD: 10203 if (!checkonly) 10204 tcp->tcp_second_timer_threshold = *i1; 10205 break; 10206 case TCP_CONN_NOTIFY_THRESHOLD: 10207 if (!checkonly) 10208 tcp->tcp_first_ctimer_threshold = *i1; 10209 break; 10210 case TCP_CONN_ABORT_THRESHOLD: 10211 if (!checkonly) 10212 tcp->tcp_second_ctimer_threshold = *i1; 10213 break; 10214 case TCP_RECVDSTADDR: 10215 if (tcp->tcp_state > TCPS_LISTEN) 10216 return (EOPNOTSUPP); 10217 if (!checkonly) 10218 tcp->tcp_recvdstaddr = onoff; 10219 break; 10220 case TCP_ANONPRIVBIND: 10221 if ((reterr = secpolicy_net_privaddr(cr, 0, 10222 IPPROTO_TCP)) != 0) { 10223 *outlenp = 0; 10224 return (reterr); 10225 } 10226 if (!checkonly) { 10227 tcp->tcp_anon_priv_bind = onoff; 10228 } 10229 break; 10230 case TCP_EXCLBIND: 10231 if (!checkonly) 10232 tcp->tcp_exclbind = onoff; 10233 break; /* goto sizeof (int) option return */ 10234 case TCP_INIT_CWND: { 10235 uint32_t init_cwnd = *((uint32_t *)invalp); 10236 10237 if (checkonly) 10238 break; 10239 10240 /* 10241 * Only allow socket with network configuration 10242 * privilege to set the initial cwnd to be larger 10243 * than allowed by RFC 3390. 10244 */ 10245 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10246 tcp->tcp_init_cwnd = init_cwnd; 10247 break; 10248 } 10249 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10250 *outlenp = 0; 10251 return (reterr); 10252 } 10253 if (init_cwnd > TCP_MAX_INIT_CWND) { 10254 *outlenp = 0; 10255 return (EINVAL); 10256 } 10257 tcp->tcp_init_cwnd = init_cwnd; 10258 break; 10259 } 10260 case TCP_KEEPALIVE_THRESHOLD: 10261 if (checkonly) 10262 break; 10263 10264 if (*i1 < tcps->tcps_keepalive_interval_low || 10265 *i1 > tcps->tcps_keepalive_interval_high) { 10266 *outlenp = 0; 10267 return (EINVAL); 10268 } 10269 if (*i1 != tcp->tcp_ka_interval) { 10270 tcp->tcp_ka_interval = *i1; 10271 /* 10272 * Check if we need to restart the 10273 * keepalive timer. 10274 */ 10275 if (tcp->tcp_ka_tid != 0) { 10276 ASSERT(tcp->tcp_ka_enabled); 10277 (void) TCP_TIMER_CANCEL(tcp, 10278 tcp->tcp_ka_tid); 10279 tcp->tcp_ka_last_intrvl = 0; 10280 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10281 tcp_keepalive_killer, 10282 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10283 } 10284 } 10285 break; 10286 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10287 if (!checkonly) { 10288 if (*i1 < 10289 tcps->tcps_keepalive_abort_interval_low || 10290 *i1 > 10291 tcps->tcps_keepalive_abort_interval_high) { 10292 *outlenp = 0; 10293 return (EINVAL); 10294 } 10295 tcp->tcp_ka_abort_thres = *i1; 10296 } 10297 break; 10298 case TCP_CORK: 10299 if (!checkonly) { 10300 /* 10301 * if tcp->tcp_cork was set and is now 10302 * being unset, we have to make sure that 10303 * the remaining data gets sent out. Also 10304 * unset tcp->tcp_cork so that tcp_wput_data() 10305 * can send data even if it is less than mss 10306 */ 10307 if (tcp->tcp_cork && onoff == 0 && 10308 tcp->tcp_unsent > 0) { 10309 tcp->tcp_cork = B_FALSE; 10310 tcp_wput_data(tcp, NULL, B_FALSE); 10311 } 10312 tcp->tcp_cork = onoff; 10313 } 10314 break; 10315 default: 10316 *outlenp = 0; 10317 return (EINVAL); 10318 } 10319 break; 10320 case IPPROTO_IP: 10321 if (tcp->tcp_family != AF_INET) { 10322 *outlenp = 0; 10323 return (ENOPROTOOPT); 10324 } 10325 switch (name) { 10326 case IP_OPTIONS: 10327 case T_IP_OPTIONS: 10328 reterr = tcp_opt_set_header(tcp, checkonly, 10329 invalp, inlen); 10330 if (reterr) { 10331 *outlenp = 0; 10332 return (reterr); 10333 } 10334 /* OK return - copy input buffer into output buffer */ 10335 if (invalp != outvalp) { 10336 /* don't trust bcopy for identical src/dst */ 10337 bcopy(invalp, outvalp, inlen); 10338 } 10339 *outlenp = inlen; 10340 return (0); 10341 case IP_TOS: 10342 case T_IP_TOS: 10343 if (!checkonly) { 10344 tcp->tcp_ipha->ipha_type_of_service = 10345 (uchar_t)*i1; 10346 tcp->tcp_tos = (uchar_t)*i1; 10347 } 10348 break; 10349 case IP_TTL: 10350 if (!checkonly) { 10351 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10352 tcp->tcp_ttl = (uchar_t)*i1; 10353 } 10354 break; 10355 case IP_BOUND_IF: 10356 case IP_NEXTHOP: 10357 /* Handled at the IP level */ 10358 return (-EINVAL); 10359 case IP_SEC_OPT: 10360 /* 10361 * We should not allow policy setting after 10362 * we start listening for connections. 10363 */ 10364 if (tcp->tcp_state == TCPS_LISTEN) { 10365 return (EINVAL); 10366 } else { 10367 /* Handled at the IP level */ 10368 return (-EINVAL); 10369 } 10370 default: 10371 *outlenp = 0; 10372 return (EINVAL); 10373 } 10374 break; 10375 case IPPROTO_IPV6: { 10376 ip6_pkt_t *ipp; 10377 10378 /* 10379 * IPPROTO_IPV6 options are only supported for sockets 10380 * that are using IPv6 on the wire. 10381 */ 10382 if (tcp->tcp_ipversion != IPV6_VERSION) { 10383 *outlenp = 0; 10384 return (ENOPROTOOPT); 10385 } 10386 /* 10387 * Only sticky options; no ancillary data 10388 */ 10389 ipp = &tcp->tcp_sticky_ipp; 10390 10391 switch (name) { 10392 case IPV6_UNICAST_HOPS: 10393 /* -1 means use default */ 10394 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10395 *outlenp = 0; 10396 return (EINVAL); 10397 } 10398 if (!checkonly) { 10399 if (*i1 == -1) { 10400 tcp->tcp_ip6h->ip6_hops = 10401 ipp->ipp_unicast_hops = 10402 (uint8_t)tcps->tcps_ipv6_hoplimit; 10403 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10404 /* Pass modified value to IP. */ 10405 *i1 = tcp->tcp_ip6h->ip6_hops; 10406 } else { 10407 tcp->tcp_ip6h->ip6_hops = 10408 ipp->ipp_unicast_hops = 10409 (uint8_t)*i1; 10410 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10411 } 10412 reterr = tcp_build_hdrs(tcp); 10413 if (reterr != 0) 10414 return (reterr); 10415 } 10416 break; 10417 case IPV6_BOUND_IF: 10418 if (!checkonly) { 10419 tcp->tcp_bound_if = *i1; 10420 PASS_OPT_TO_IP(connp); 10421 } 10422 break; 10423 /* 10424 * Set boolean switches for ancillary data delivery 10425 */ 10426 case IPV6_RECVPKTINFO: 10427 if (!checkonly) { 10428 if (onoff) 10429 tcp->tcp_ipv6_recvancillary |= 10430 TCP_IPV6_RECVPKTINFO; 10431 else 10432 tcp->tcp_ipv6_recvancillary &= 10433 ~TCP_IPV6_RECVPKTINFO; 10434 /* Force it to be sent up with the next msg */ 10435 tcp->tcp_recvifindex = 0; 10436 PASS_OPT_TO_IP(connp); 10437 } 10438 break; 10439 case IPV6_RECVTCLASS: 10440 if (!checkonly) { 10441 if (onoff) 10442 tcp->tcp_ipv6_recvancillary |= 10443 TCP_IPV6_RECVTCLASS; 10444 else 10445 tcp->tcp_ipv6_recvancillary &= 10446 ~TCP_IPV6_RECVTCLASS; 10447 PASS_OPT_TO_IP(connp); 10448 } 10449 break; 10450 case IPV6_RECVHOPLIMIT: 10451 if (!checkonly) { 10452 if (onoff) 10453 tcp->tcp_ipv6_recvancillary |= 10454 TCP_IPV6_RECVHOPLIMIT; 10455 else 10456 tcp->tcp_ipv6_recvancillary &= 10457 ~TCP_IPV6_RECVHOPLIMIT; 10458 /* Force it to be sent up with the next msg */ 10459 tcp->tcp_recvhops = 0xffffffffU; 10460 PASS_OPT_TO_IP(connp); 10461 } 10462 break; 10463 case IPV6_RECVHOPOPTS: 10464 if (!checkonly) { 10465 if (onoff) 10466 tcp->tcp_ipv6_recvancillary |= 10467 TCP_IPV6_RECVHOPOPTS; 10468 else 10469 tcp->tcp_ipv6_recvancillary &= 10470 ~TCP_IPV6_RECVHOPOPTS; 10471 PASS_OPT_TO_IP(connp); 10472 } 10473 break; 10474 case IPV6_RECVDSTOPTS: 10475 if (!checkonly) { 10476 if (onoff) 10477 tcp->tcp_ipv6_recvancillary |= 10478 TCP_IPV6_RECVDSTOPTS; 10479 else 10480 tcp->tcp_ipv6_recvancillary &= 10481 ~TCP_IPV6_RECVDSTOPTS; 10482 PASS_OPT_TO_IP(connp); 10483 } 10484 break; 10485 case _OLD_IPV6_RECVDSTOPTS: 10486 if (!checkonly) { 10487 if (onoff) 10488 tcp->tcp_ipv6_recvancillary |= 10489 TCP_OLD_IPV6_RECVDSTOPTS; 10490 else 10491 tcp->tcp_ipv6_recvancillary &= 10492 ~TCP_OLD_IPV6_RECVDSTOPTS; 10493 } 10494 break; 10495 case IPV6_RECVRTHDR: 10496 if (!checkonly) { 10497 if (onoff) 10498 tcp->tcp_ipv6_recvancillary |= 10499 TCP_IPV6_RECVRTHDR; 10500 else 10501 tcp->tcp_ipv6_recvancillary &= 10502 ~TCP_IPV6_RECVRTHDR; 10503 PASS_OPT_TO_IP(connp); 10504 } 10505 break; 10506 case IPV6_RECVRTHDRDSTOPTS: 10507 if (!checkonly) { 10508 if (onoff) 10509 tcp->tcp_ipv6_recvancillary |= 10510 TCP_IPV6_RECVRTDSTOPTS; 10511 else 10512 tcp->tcp_ipv6_recvancillary &= 10513 ~TCP_IPV6_RECVRTDSTOPTS; 10514 PASS_OPT_TO_IP(connp); 10515 } 10516 break; 10517 case IPV6_PKTINFO: 10518 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10519 return (EINVAL); 10520 if (checkonly) 10521 break; 10522 10523 if (inlen == 0) { 10524 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10525 } else { 10526 struct in6_pktinfo *pkti; 10527 10528 pkti = (struct in6_pktinfo *)invalp; 10529 /* 10530 * RFC 3542 states that ipi6_addr must be 10531 * the unspecified address when setting the 10532 * IPV6_PKTINFO sticky socket option on a 10533 * TCP socket. 10534 */ 10535 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10536 return (EINVAL); 10537 /* 10538 * IP will validate the source address and 10539 * interface index. 10540 */ 10541 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10542 reterr = ip_set_options(tcp->tcp_connp, 10543 level, name, invalp, inlen, cr); 10544 } else { 10545 reterr = ip6_set_pktinfo(cr, 10546 tcp->tcp_connp, pkti); 10547 } 10548 if (reterr != 0) 10549 return (reterr); 10550 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10551 ipp->ipp_addr = pkti->ipi6_addr; 10552 if (ipp->ipp_ifindex != 0) 10553 ipp->ipp_fields |= IPPF_IFINDEX; 10554 else 10555 ipp->ipp_fields &= ~IPPF_IFINDEX; 10556 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10557 ipp->ipp_fields |= IPPF_ADDR; 10558 else 10559 ipp->ipp_fields &= ~IPPF_ADDR; 10560 } 10561 reterr = tcp_build_hdrs(tcp); 10562 if (reterr != 0) 10563 return (reterr); 10564 break; 10565 case IPV6_TCLASS: 10566 if (inlen != 0 && inlen != sizeof (int)) 10567 return (EINVAL); 10568 if (checkonly) 10569 break; 10570 10571 if (inlen == 0) { 10572 ipp->ipp_fields &= ~IPPF_TCLASS; 10573 } else { 10574 if (*i1 > 255 || *i1 < -1) 10575 return (EINVAL); 10576 if (*i1 == -1) { 10577 ipp->ipp_tclass = 0; 10578 *i1 = 0; 10579 } else { 10580 ipp->ipp_tclass = *i1; 10581 } 10582 ipp->ipp_fields |= IPPF_TCLASS; 10583 } 10584 reterr = tcp_build_hdrs(tcp); 10585 if (reterr != 0) 10586 return (reterr); 10587 break; 10588 case IPV6_NEXTHOP: 10589 /* 10590 * IP will verify that the nexthop is reachable 10591 * and fail for sticky options. 10592 */ 10593 if (inlen != 0 && inlen != sizeof (sin6_t)) 10594 return (EINVAL); 10595 if (checkonly) 10596 break; 10597 10598 if (inlen == 0) { 10599 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10600 } else { 10601 sin6_t *sin6 = (sin6_t *)invalp; 10602 10603 if (sin6->sin6_family != AF_INET6) 10604 return (EAFNOSUPPORT); 10605 if (IN6_IS_ADDR_V4MAPPED( 10606 &sin6->sin6_addr)) 10607 return (EADDRNOTAVAIL); 10608 ipp->ipp_nexthop = sin6->sin6_addr; 10609 if (!IN6_IS_ADDR_UNSPECIFIED( 10610 &ipp->ipp_nexthop)) 10611 ipp->ipp_fields |= IPPF_NEXTHOP; 10612 else 10613 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10614 } 10615 reterr = tcp_build_hdrs(tcp); 10616 if (reterr != 0) 10617 return (reterr); 10618 PASS_OPT_TO_IP(connp); 10619 break; 10620 case IPV6_HOPOPTS: { 10621 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10622 10623 /* 10624 * Sanity checks - minimum size, size a multiple of 10625 * eight bytes, and matching size passed in. 10626 */ 10627 if (inlen != 0 && 10628 inlen != (8 * (hopts->ip6h_len + 1))) 10629 return (EINVAL); 10630 10631 if (checkonly) 10632 break; 10633 10634 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10635 (uchar_t **)&ipp->ipp_hopopts, 10636 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10637 if (reterr != 0) 10638 return (reterr); 10639 if (ipp->ipp_hopoptslen == 0) 10640 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10641 else 10642 ipp->ipp_fields |= IPPF_HOPOPTS; 10643 reterr = tcp_build_hdrs(tcp); 10644 if (reterr != 0) 10645 return (reterr); 10646 break; 10647 } 10648 case IPV6_RTHDRDSTOPTS: { 10649 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10650 10651 /* 10652 * Sanity checks - minimum size, size a multiple of 10653 * eight bytes, and matching size passed in. 10654 */ 10655 if (inlen != 0 && 10656 inlen != (8 * (dopts->ip6d_len + 1))) 10657 return (EINVAL); 10658 10659 if (checkonly) 10660 break; 10661 10662 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10663 (uchar_t **)&ipp->ipp_rtdstopts, 10664 &ipp->ipp_rtdstoptslen, 0); 10665 if (reterr != 0) 10666 return (reterr); 10667 if (ipp->ipp_rtdstoptslen == 0) 10668 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10669 else 10670 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10671 reterr = tcp_build_hdrs(tcp); 10672 if (reterr != 0) 10673 return (reterr); 10674 break; 10675 } 10676 case IPV6_DSTOPTS: { 10677 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10678 10679 /* 10680 * Sanity checks - minimum size, size a multiple of 10681 * eight bytes, and matching size passed in. 10682 */ 10683 if (inlen != 0 && 10684 inlen != (8 * (dopts->ip6d_len + 1))) 10685 return (EINVAL); 10686 10687 if (checkonly) 10688 break; 10689 10690 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10691 (uchar_t **)&ipp->ipp_dstopts, 10692 &ipp->ipp_dstoptslen, 0); 10693 if (reterr != 0) 10694 return (reterr); 10695 if (ipp->ipp_dstoptslen == 0) 10696 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10697 else 10698 ipp->ipp_fields |= IPPF_DSTOPTS; 10699 reterr = tcp_build_hdrs(tcp); 10700 if (reterr != 0) 10701 return (reterr); 10702 break; 10703 } 10704 case IPV6_RTHDR: { 10705 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10706 10707 /* 10708 * Sanity checks - minimum size, size a multiple of 10709 * eight bytes, and matching size passed in. 10710 */ 10711 if (inlen != 0 && 10712 inlen != (8 * (rt->ip6r_len + 1))) 10713 return (EINVAL); 10714 10715 if (checkonly) 10716 break; 10717 10718 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10719 (uchar_t **)&ipp->ipp_rthdr, 10720 &ipp->ipp_rthdrlen, 0); 10721 if (reterr != 0) 10722 return (reterr); 10723 if (ipp->ipp_rthdrlen == 0) 10724 ipp->ipp_fields &= ~IPPF_RTHDR; 10725 else 10726 ipp->ipp_fields |= IPPF_RTHDR; 10727 reterr = tcp_build_hdrs(tcp); 10728 if (reterr != 0) 10729 return (reterr); 10730 break; 10731 } 10732 case IPV6_V6ONLY: 10733 if (!checkonly) { 10734 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10735 } 10736 break; 10737 case IPV6_USE_MIN_MTU: 10738 if (inlen != sizeof (int)) 10739 return (EINVAL); 10740 10741 if (*i1 < -1 || *i1 > 1) 10742 return (EINVAL); 10743 10744 if (checkonly) 10745 break; 10746 10747 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10748 ipp->ipp_use_min_mtu = *i1; 10749 break; 10750 case IPV6_SEC_OPT: 10751 /* 10752 * We should not allow policy setting after 10753 * we start listening for connections. 10754 */ 10755 if (tcp->tcp_state == TCPS_LISTEN) { 10756 return (EINVAL); 10757 } else { 10758 /* Handled at the IP level */ 10759 return (-EINVAL); 10760 } 10761 case IPV6_SRC_PREFERENCES: 10762 if (inlen != sizeof (uint32_t)) 10763 return (EINVAL); 10764 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10765 *(uint32_t *)invalp); 10766 if (reterr != 0) { 10767 *outlenp = 0; 10768 return (reterr); 10769 } 10770 break; 10771 default: 10772 *outlenp = 0; 10773 return (EINVAL); 10774 } 10775 break; 10776 } /* end IPPROTO_IPV6 */ 10777 default: 10778 *outlenp = 0; 10779 return (EINVAL); 10780 } 10781 /* 10782 * Common case of OK return with outval same as inval 10783 */ 10784 if (invalp != outvalp) { 10785 /* don't trust bcopy for identical src/dst */ 10786 (void) bcopy(invalp, outvalp, inlen); 10787 } 10788 *outlenp = inlen; 10789 return (0); 10790 } 10791 10792 /* ARGSUSED */ 10793 int 10794 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10795 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10796 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10797 { 10798 conn_t *connp = Q_TO_CONN(q); 10799 10800 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10801 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10802 } 10803 10804 int 10805 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10806 const void *optvalp, socklen_t optlen, cred_t *cr) 10807 { 10808 conn_t *connp = (conn_t *)proto_handle; 10809 squeue_t *sqp = connp->conn_sqp; 10810 int error; 10811 10812 ASSERT(connp->conn_upper_handle != NULL); 10813 /* 10814 * Entering the squeue synchronously can result in a context switch, 10815 * which can cause a rather sever performance degradation. So we try to 10816 * handle whatever options we can without entering the squeue. 10817 */ 10818 if (level == IPPROTO_TCP) { 10819 switch (option_name) { 10820 case TCP_NODELAY: 10821 if (optlen != sizeof (int32_t)) 10822 return (EINVAL); 10823 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10824 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10825 connp->conn_tcp->tcp_mss; 10826 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10827 return (0); 10828 default: 10829 break; 10830 } 10831 } 10832 10833 error = squeue_synch_enter(sqp, connp, 0); 10834 if (error == ENOMEM) { 10835 return (ENOMEM); 10836 } 10837 10838 error = proto_opt_check(level, option_name, optlen, NULL, 10839 tcp_opt_obj.odb_opt_des_arr, 10840 tcp_opt_obj.odb_opt_arr_cnt, 10841 tcp_opt_obj.odb_topmost_tpiprovider, 10842 B_TRUE, B_FALSE, cr); 10843 10844 if (error != 0) { 10845 if (error < 0) { 10846 error = proto_tlitosyserr(-error); 10847 } 10848 squeue_synch_exit(sqp, connp); 10849 return (error); 10850 } 10851 10852 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10853 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10854 NULL, cr, NULL); 10855 squeue_synch_exit(sqp, connp); 10856 10857 if (error < 0) { 10858 /* 10859 * Pass on to ip 10860 */ 10861 error = ip_set_options(connp, level, option_name, optvalp, 10862 optlen, cr); 10863 } 10864 return (error); 10865 } 10866 10867 /* 10868 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10869 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10870 * headers, and the maximum size tcp header (to avoid reallocation 10871 * on the fly for additional tcp options). 10872 * Returns failure if can't allocate memory. 10873 */ 10874 static int 10875 tcp_build_hdrs(tcp_t *tcp) 10876 { 10877 char *hdrs; 10878 uint_t hdrs_len; 10879 ip6i_t *ip6i; 10880 char buf[TCP_MAX_HDR_LENGTH]; 10881 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10882 in6_addr_t src, dst; 10883 tcp_stack_t *tcps = tcp->tcp_tcps; 10884 conn_t *connp = tcp->tcp_connp; 10885 10886 /* 10887 * save the existing tcp header and source/dest IP addresses 10888 */ 10889 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10890 src = tcp->tcp_ip6h->ip6_src; 10891 dst = tcp->tcp_ip6h->ip6_dst; 10892 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10893 ASSERT(hdrs_len != 0); 10894 if (hdrs_len > tcp->tcp_iphc_len) { 10895 /* Need to reallocate */ 10896 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10897 if (hdrs == NULL) 10898 return (ENOMEM); 10899 if (tcp->tcp_iphc != NULL) { 10900 if (tcp->tcp_hdr_grown) { 10901 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10902 } else { 10903 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10904 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10905 } 10906 tcp->tcp_iphc_len = 0; 10907 } 10908 ASSERT(tcp->tcp_iphc_len == 0); 10909 tcp->tcp_iphc = hdrs; 10910 tcp->tcp_iphc_len = hdrs_len; 10911 tcp->tcp_hdr_grown = B_TRUE; 10912 } 10913 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10914 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10915 10916 /* Set header fields not in ipp */ 10917 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10918 ip6i = (ip6i_t *)tcp->tcp_iphc; 10919 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10920 } else { 10921 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10922 } 10923 /* 10924 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10925 * 10926 * tcp->tcp_tcp_hdr_len doesn't change here. 10927 */ 10928 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10929 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10930 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10931 10932 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10933 10934 tcp->tcp_ip6h->ip6_src = src; 10935 tcp->tcp_ip6h->ip6_dst = dst; 10936 10937 /* 10938 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10939 * the default value for TCP. 10940 */ 10941 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10942 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10943 10944 /* 10945 * If we're setting extension headers after a connection 10946 * has been established, and if we have a routing header 10947 * among the extension headers, call ip_massage_options_v6 to 10948 * manipulate the routing header/ip6_dst set the checksum 10949 * difference in the tcp header template. 10950 * (This happens in tcp_connect_ipv6 if the routing header 10951 * is set prior to the connect.) 10952 * Set the tcp_sum to zero first in case we've cleared a 10953 * routing header or don't have one at all. 10954 */ 10955 tcp->tcp_sum = 0; 10956 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10957 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10958 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10959 (uint8_t *)tcp->tcp_tcph); 10960 if (rth != NULL) { 10961 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10962 rth, tcps->tcps_netstack); 10963 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10964 (tcp->tcp_sum >> 16)); 10965 } 10966 } 10967 10968 /* Try to get everything in a single mblk */ 10969 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10970 hdrs_len + tcps->tcps_wroff_xtra); 10971 return (0); 10972 } 10973 10974 /* 10975 * Transfer any source route option from ipha to buf/dst in reversed form. 10976 */ 10977 static int 10978 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10979 { 10980 ipoptp_t opts; 10981 uchar_t *opt; 10982 uint8_t optval; 10983 uint8_t optlen; 10984 uint32_t len = 0; 10985 10986 for (optval = ipoptp_first(&opts, ipha); 10987 optval != IPOPT_EOL; 10988 optval = ipoptp_next(&opts)) { 10989 opt = opts.ipoptp_cur; 10990 optlen = opts.ipoptp_len; 10991 switch (optval) { 10992 int off1, off2; 10993 case IPOPT_SSRR: 10994 case IPOPT_LSRR: 10995 10996 /* Reverse source route */ 10997 /* 10998 * First entry should be the next to last one in the 10999 * current source route (the last entry is our 11000 * address.) 11001 * The last entry should be the final destination. 11002 */ 11003 buf[IPOPT_OPTVAL] = (uint8_t)optval; 11004 buf[IPOPT_OLEN] = (uint8_t)optlen; 11005 off1 = IPOPT_MINOFF_SR - 1; 11006 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 11007 if (off2 < 0) { 11008 /* No entries in source route */ 11009 break; 11010 } 11011 bcopy(opt + off2, dst, IP_ADDR_LEN); 11012 /* 11013 * Note: use src since ipha has not had its src 11014 * and dst reversed (it is in the state it was 11015 * received. 11016 */ 11017 bcopy(&ipha->ipha_src, buf + off2, 11018 IP_ADDR_LEN); 11019 off2 -= IP_ADDR_LEN; 11020 11021 while (off2 > 0) { 11022 bcopy(opt + off2, buf + off1, 11023 IP_ADDR_LEN); 11024 off1 += IP_ADDR_LEN; 11025 off2 -= IP_ADDR_LEN; 11026 } 11027 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11028 buf += optlen; 11029 len += optlen; 11030 break; 11031 } 11032 } 11033 done: 11034 /* Pad the resulting options */ 11035 while (len & 0x3) { 11036 *buf++ = IPOPT_EOL; 11037 len++; 11038 } 11039 return (len); 11040 } 11041 11042 11043 /* 11044 * Extract and revert a source route from ipha (if any) 11045 * and then update the relevant fields in both tcp_t and the standard header. 11046 */ 11047 static void 11048 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11049 { 11050 char buf[TCP_MAX_HDR_LENGTH]; 11051 uint_t tcph_len; 11052 int len; 11053 11054 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11055 len = IPH_HDR_LENGTH(ipha); 11056 if (len == IP_SIMPLE_HDR_LENGTH) 11057 /* Nothing to do */ 11058 return; 11059 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11060 (len & 0x3)) 11061 return; 11062 11063 tcph_len = tcp->tcp_tcp_hdr_len; 11064 bcopy(tcp->tcp_tcph, buf, tcph_len); 11065 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11066 (tcp->tcp_ipha->ipha_dst & 0xffff); 11067 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11068 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11069 len += IP_SIMPLE_HDR_LENGTH; 11070 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11071 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11072 if ((int)tcp->tcp_sum < 0) 11073 tcp->tcp_sum--; 11074 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11075 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11076 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11077 bcopy(buf, tcp->tcp_tcph, tcph_len); 11078 tcp->tcp_ip_hdr_len = len; 11079 tcp->tcp_ipha->ipha_version_and_hdr_length = 11080 (IP_VERSION << 4) | (len >> 2); 11081 len += tcph_len; 11082 tcp->tcp_hdr_len = len; 11083 } 11084 11085 /* 11086 * Copy the standard header into its new location, 11087 * lay in the new options and then update the relevant 11088 * fields in both tcp_t and the standard header. 11089 */ 11090 static int 11091 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11092 { 11093 uint_t tcph_len; 11094 uint8_t *ip_optp; 11095 tcph_t *new_tcph; 11096 tcp_stack_t *tcps = tcp->tcp_tcps; 11097 conn_t *connp = tcp->tcp_connp; 11098 11099 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11100 return (EINVAL); 11101 11102 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11103 return (EINVAL); 11104 11105 if (checkonly) { 11106 /* 11107 * do not really set, just pretend to - T_CHECK 11108 */ 11109 return (0); 11110 } 11111 11112 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11113 if (tcp->tcp_label_len > 0) { 11114 int padlen; 11115 uint8_t opt; 11116 11117 /* convert list termination to no-ops */ 11118 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11119 ip_optp += ip_optp[IPOPT_OLEN]; 11120 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11121 while (--padlen >= 0) 11122 *ip_optp++ = opt; 11123 } 11124 tcph_len = tcp->tcp_tcp_hdr_len; 11125 new_tcph = (tcph_t *)(ip_optp + len); 11126 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11127 tcp->tcp_tcph = new_tcph; 11128 bcopy(ptr, ip_optp, len); 11129 11130 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11131 11132 tcp->tcp_ip_hdr_len = len; 11133 tcp->tcp_ipha->ipha_version_and_hdr_length = 11134 (IP_VERSION << 4) | (len >> 2); 11135 tcp->tcp_hdr_len = len + tcph_len; 11136 if (!TCP_IS_DETACHED(tcp)) { 11137 /* Always allocate room for all options. */ 11138 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11139 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11140 } 11141 return (0); 11142 } 11143 11144 /* Get callback routine passed to nd_load by tcp_param_register */ 11145 /* ARGSUSED */ 11146 static int 11147 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11148 { 11149 tcpparam_t *tcppa = (tcpparam_t *)cp; 11150 11151 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11152 return (0); 11153 } 11154 11155 /* 11156 * Walk through the param array specified registering each element with the 11157 * named dispatch handler. 11158 */ 11159 static boolean_t 11160 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11161 { 11162 for (; cnt-- > 0; tcppa++) { 11163 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11164 if (!nd_load(ndp, tcppa->tcp_param_name, 11165 tcp_param_get, tcp_param_set, 11166 (caddr_t)tcppa)) { 11167 nd_free(ndp); 11168 return (B_FALSE); 11169 } 11170 } 11171 } 11172 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11173 KM_SLEEP); 11174 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11175 sizeof (tcpparam_t)); 11176 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11177 tcp_param_get, tcp_param_set_aligned, 11178 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11179 nd_free(ndp); 11180 return (B_FALSE); 11181 } 11182 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11183 KM_SLEEP); 11184 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11185 sizeof (tcpparam_t)); 11186 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11187 tcp_param_get, tcp_param_set_aligned, 11188 (caddr_t)tcps->tcps_mdt_head_param)) { 11189 nd_free(ndp); 11190 return (B_FALSE); 11191 } 11192 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11193 KM_SLEEP); 11194 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11195 sizeof (tcpparam_t)); 11196 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11197 tcp_param_get, tcp_param_set_aligned, 11198 (caddr_t)tcps->tcps_mdt_tail_param)) { 11199 nd_free(ndp); 11200 return (B_FALSE); 11201 } 11202 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11203 KM_SLEEP); 11204 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11205 sizeof (tcpparam_t)); 11206 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11207 tcp_param_get, tcp_param_set_aligned, 11208 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11209 nd_free(ndp); 11210 return (B_FALSE); 11211 } 11212 if (!nd_load(ndp, "tcp_extra_priv_ports", 11213 tcp_extra_priv_ports_get, NULL, NULL)) { 11214 nd_free(ndp); 11215 return (B_FALSE); 11216 } 11217 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11218 NULL, tcp_extra_priv_ports_add, NULL)) { 11219 nd_free(ndp); 11220 return (B_FALSE); 11221 } 11222 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11223 NULL, tcp_extra_priv_ports_del, NULL)) { 11224 nd_free(ndp); 11225 return (B_FALSE); 11226 } 11227 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11228 NULL)) { 11229 nd_free(ndp); 11230 return (B_FALSE); 11231 } 11232 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11233 NULL, NULL)) { 11234 nd_free(ndp); 11235 return (B_FALSE); 11236 } 11237 if (!nd_load(ndp, "tcp_listen_hash", 11238 tcp_listen_hash_report, NULL, NULL)) { 11239 nd_free(ndp); 11240 return (B_FALSE); 11241 } 11242 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11243 NULL, NULL)) { 11244 nd_free(ndp); 11245 return (B_FALSE); 11246 } 11247 if (!nd_load(ndp, "tcp_acceptor_hash", 11248 tcp_acceptor_hash_report, NULL, NULL)) { 11249 nd_free(ndp); 11250 return (B_FALSE); 11251 } 11252 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11253 tcp_1948_phrase_set, NULL)) { 11254 nd_free(ndp); 11255 return (B_FALSE); 11256 } 11257 /* 11258 * Dummy ndd variables - only to convey obsolescence information 11259 * through printing of their name (no get or set routines) 11260 * XXX Remove in future releases ? 11261 */ 11262 if (!nd_load(ndp, 11263 "tcp_close_wait_interval(obsoleted - " 11264 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11265 nd_free(ndp); 11266 return (B_FALSE); 11267 } 11268 return (B_TRUE); 11269 } 11270 11271 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11272 /* ARGSUSED */ 11273 static int 11274 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11275 cred_t *cr) 11276 { 11277 long new_value; 11278 tcpparam_t *tcppa = (tcpparam_t *)cp; 11279 11280 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11281 new_value < tcppa->tcp_param_min || 11282 new_value > tcppa->tcp_param_max) { 11283 return (EINVAL); 11284 } 11285 /* 11286 * Need to make sure new_value is a multiple of 4. If it is not, 11287 * round it up. For future 64 bit requirement, we actually make it 11288 * a multiple of 8. 11289 */ 11290 if (new_value & 0x7) { 11291 new_value = (new_value & ~0x7) + 0x8; 11292 } 11293 tcppa->tcp_param_val = new_value; 11294 return (0); 11295 } 11296 11297 /* Set callback routine passed to nd_load by tcp_param_register */ 11298 /* ARGSUSED */ 11299 static int 11300 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11301 { 11302 long new_value; 11303 tcpparam_t *tcppa = (tcpparam_t *)cp; 11304 11305 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11306 new_value < tcppa->tcp_param_min || 11307 new_value > tcppa->tcp_param_max) { 11308 return (EINVAL); 11309 } 11310 tcppa->tcp_param_val = new_value; 11311 return (0); 11312 } 11313 11314 /* 11315 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11316 * is filled, return as much as we can. The message passed in may be 11317 * multi-part, chained using b_cont. "start" is the starting sequence 11318 * number for this piece. 11319 */ 11320 static mblk_t * 11321 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11322 { 11323 uint32_t end; 11324 mblk_t *mp1; 11325 mblk_t *mp2; 11326 mblk_t *next_mp; 11327 uint32_t u1; 11328 tcp_stack_t *tcps = tcp->tcp_tcps; 11329 11330 /* Walk through all the new pieces. */ 11331 do { 11332 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11333 (uintptr_t)INT_MAX); 11334 end = start + (int)(mp->b_wptr - mp->b_rptr); 11335 next_mp = mp->b_cont; 11336 if (start == end) { 11337 /* Empty. Blast it. */ 11338 freeb(mp); 11339 continue; 11340 } 11341 mp->b_cont = NULL; 11342 TCP_REASS_SET_SEQ(mp, start); 11343 TCP_REASS_SET_END(mp, end); 11344 mp1 = tcp->tcp_reass_tail; 11345 if (!mp1) { 11346 tcp->tcp_reass_tail = mp; 11347 tcp->tcp_reass_head = mp; 11348 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11349 UPDATE_MIB(&tcps->tcps_mib, 11350 tcpInDataUnorderBytes, end - start); 11351 continue; 11352 } 11353 /* New stuff completely beyond tail? */ 11354 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11355 /* Link it on end. */ 11356 mp1->b_cont = mp; 11357 tcp->tcp_reass_tail = mp; 11358 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11359 UPDATE_MIB(&tcps->tcps_mib, 11360 tcpInDataUnorderBytes, end - start); 11361 continue; 11362 } 11363 mp1 = tcp->tcp_reass_head; 11364 u1 = TCP_REASS_SEQ(mp1); 11365 /* New stuff at the front? */ 11366 if (SEQ_LT(start, u1)) { 11367 /* Yes... Check for overlap. */ 11368 mp->b_cont = mp1; 11369 tcp->tcp_reass_head = mp; 11370 tcp_reass_elim_overlap(tcp, mp); 11371 continue; 11372 } 11373 /* 11374 * The new piece fits somewhere between the head and tail. 11375 * We find our slot, where mp1 precedes us and mp2 trails. 11376 */ 11377 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11378 u1 = TCP_REASS_SEQ(mp2); 11379 if (SEQ_LEQ(start, u1)) 11380 break; 11381 } 11382 /* Link ourselves in */ 11383 mp->b_cont = mp2; 11384 mp1->b_cont = mp; 11385 11386 /* Trim overlap with following mblk(s) first */ 11387 tcp_reass_elim_overlap(tcp, mp); 11388 11389 /* Trim overlap with preceding mblk */ 11390 tcp_reass_elim_overlap(tcp, mp1); 11391 11392 } while (start = end, mp = next_mp); 11393 mp1 = tcp->tcp_reass_head; 11394 /* Anything ready to go? */ 11395 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11396 return (NULL); 11397 /* Eat what we can off the queue */ 11398 for (;;) { 11399 mp = mp1->b_cont; 11400 end = TCP_REASS_END(mp1); 11401 TCP_REASS_SET_SEQ(mp1, 0); 11402 TCP_REASS_SET_END(mp1, 0); 11403 if (!mp) { 11404 tcp->tcp_reass_tail = NULL; 11405 break; 11406 } 11407 if (end != TCP_REASS_SEQ(mp)) { 11408 mp1->b_cont = NULL; 11409 break; 11410 } 11411 mp1 = mp; 11412 } 11413 mp1 = tcp->tcp_reass_head; 11414 tcp->tcp_reass_head = mp; 11415 return (mp1); 11416 } 11417 11418 /* Eliminate any overlap that mp may have over later mblks */ 11419 static void 11420 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11421 { 11422 uint32_t end; 11423 mblk_t *mp1; 11424 uint32_t u1; 11425 tcp_stack_t *tcps = tcp->tcp_tcps; 11426 11427 end = TCP_REASS_END(mp); 11428 while ((mp1 = mp->b_cont) != NULL) { 11429 u1 = TCP_REASS_SEQ(mp1); 11430 if (!SEQ_GT(end, u1)) 11431 break; 11432 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11433 mp->b_wptr -= end - u1; 11434 TCP_REASS_SET_END(mp, u1); 11435 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11436 UPDATE_MIB(&tcps->tcps_mib, 11437 tcpInDataPartDupBytes, end - u1); 11438 break; 11439 } 11440 mp->b_cont = mp1->b_cont; 11441 TCP_REASS_SET_SEQ(mp1, 0); 11442 TCP_REASS_SET_END(mp1, 0); 11443 freeb(mp1); 11444 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11445 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11446 } 11447 if (!mp1) 11448 tcp->tcp_reass_tail = mp; 11449 } 11450 11451 static uint_t 11452 tcp_rwnd_reopen(tcp_t *tcp) 11453 { 11454 uint_t ret = 0; 11455 uint_t thwin; 11456 11457 /* Learn the latest rwnd information that we sent to the other side. */ 11458 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11459 << tcp->tcp_rcv_ws; 11460 /* This is peer's calculated send window (our receive window). */ 11461 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11462 /* 11463 * Increase the receive window to max. But we need to do receiver 11464 * SWS avoidance. This means that we need to check the increase of 11465 * of receive window is at least 1 MSS. 11466 */ 11467 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11468 /* 11469 * If the window that the other side knows is less than max 11470 * deferred acks segments, send an update immediately. 11471 */ 11472 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11473 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11474 ret = TH_ACK_NEEDED; 11475 } 11476 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11477 } 11478 return (ret); 11479 } 11480 11481 /* 11482 * Send up all messages queued on tcp_rcv_list. 11483 */ 11484 static uint_t 11485 tcp_rcv_drain(tcp_t *tcp) 11486 { 11487 mblk_t *mp; 11488 uint_t ret = 0; 11489 #ifdef DEBUG 11490 uint_t cnt = 0; 11491 #endif 11492 queue_t *q = tcp->tcp_rq; 11493 11494 /* Can't drain on an eager connection */ 11495 if (tcp->tcp_listener != NULL) 11496 return (ret); 11497 11498 /* Can't be a non-STREAMS connection or sodirect enabled */ 11499 ASSERT((!IPCL_IS_NONSTR(tcp->tcp_connp)) && SOD_NOT_ENABLED(tcp)); 11500 11501 /* No need for the push timer now. */ 11502 if (tcp->tcp_push_tid != 0) { 11503 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11504 tcp->tcp_push_tid = 0; 11505 } 11506 11507 /* 11508 * Handle two cases here: we are currently fused or we were 11509 * previously fused and have some urgent data to be delivered 11510 * upstream. The latter happens because we either ran out of 11511 * memory or were detached and therefore sending the SIGURG was 11512 * deferred until this point. In either case we pass control 11513 * over to tcp_fuse_rcv_drain() since it may need to complete 11514 * some work. 11515 */ 11516 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11517 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11518 tcp->tcp_fused_sigurg_mp != NULL); 11519 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11520 &tcp->tcp_fused_sigurg_mp)) 11521 return (ret); 11522 } 11523 11524 while ((mp = tcp->tcp_rcv_list) != NULL) { 11525 tcp->tcp_rcv_list = mp->b_next; 11526 mp->b_next = NULL; 11527 #ifdef DEBUG 11528 cnt += msgdsize(mp); 11529 #endif 11530 /* Does this need SSL processing first? */ 11531 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11532 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11533 mblk_t *, mp); 11534 tcp_kssl_input(tcp, mp); 11535 continue; 11536 } 11537 putnext(q, mp); 11538 } 11539 #ifdef DEBUG 11540 ASSERT(cnt == tcp->tcp_rcv_cnt); 11541 #endif 11542 tcp->tcp_rcv_last_head = NULL; 11543 tcp->tcp_rcv_last_tail = NULL; 11544 tcp->tcp_rcv_cnt = 0; 11545 11546 if (canputnext(q)) 11547 return (tcp_rwnd_reopen(tcp)); 11548 11549 return (ret); 11550 } 11551 11552 /* 11553 * Queue data on tcp_rcv_list which is a b_next chain. 11554 * tcp_rcv_last_head/tail is the last element of this chain. 11555 * Each element of the chain is a b_cont chain. 11556 * 11557 * M_DATA messages are added to the current element. 11558 * Other messages are added as new (b_next) elements. 11559 */ 11560 void 11561 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11562 { 11563 ASSERT(seg_len == msgdsize(mp)); 11564 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11565 11566 if (tcp->tcp_rcv_list == NULL) { 11567 ASSERT(tcp->tcp_rcv_last_head == NULL); 11568 tcp->tcp_rcv_list = mp; 11569 tcp->tcp_rcv_last_head = mp; 11570 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11571 tcp->tcp_rcv_last_tail->b_cont = mp; 11572 } else { 11573 tcp->tcp_rcv_last_head->b_next = mp; 11574 tcp->tcp_rcv_last_head = mp; 11575 } 11576 11577 while (mp->b_cont) 11578 mp = mp->b_cont; 11579 11580 tcp->tcp_rcv_last_tail = mp; 11581 tcp->tcp_rcv_cnt += seg_len; 11582 tcp->tcp_rwnd -= seg_len; 11583 } 11584 11585 /* 11586 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11587 * above, in addition when uioa is enabled schedule an asynchronous uio 11588 * prior to enqueuing. They implement the combinhed semantics of the 11589 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11590 * canputnext(), i.e. flow-control with backenable. 11591 * 11592 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11593 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11594 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11595 * 11596 * Must be called with sodp->sod_lockp held and will return with the lock 11597 * released. 11598 */ 11599 static uint_t 11600 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11601 { 11602 queue_t *q = tcp->tcp_rq; 11603 uint_t thwin; 11604 tcp_stack_t *tcps = tcp->tcp_tcps; 11605 uint_t ret = 0; 11606 11607 /* Can't be an eager connection */ 11608 ASSERT(tcp->tcp_listener == NULL); 11609 11610 /* Caller must have lock held */ 11611 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11612 11613 /* Sodirect mode so must not be a tcp_rcv_list */ 11614 ASSERT(tcp->tcp_rcv_list == NULL); 11615 11616 if (SOD_QFULL(sodp)) { 11617 /* Q is full, mark Q for need backenable */ 11618 SOD_QSETBE(sodp); 11619 } 11620 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11621 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11622 << tcp->tcp_rcv_ws; 11623 /* This is peer's calculated send window (our available rwnd). */ 11624 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11625 /* 11626 * Increase the receive window to max. But we need to do receiver 11627 * SWS avoidance. This means that we need to check the increase of 11628 * of receive window is at least 1 MSS. 11629 */ 11630 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11631 /* 11632 * If the window that the other side knows is less than max 11633 * deferred acks segments, send an update immediately. 11634 */ 11635 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11636 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11637 ret = TH_ACK_NEEDED; 11638 } 11639 tcp->tcp_rwnd = q->q_hiwat; 11640 } 11641 11642 if (!SOD_QEMPTY(sodp)) { 11643 /* Wakeup to socket */ 11644 sodp->sod_state &= SOD_WAKE_CLR; 11645 sodp->sod_state |= SOD_WAKE_DONE; 11646 (sodp->sod_wakeup)(sodp); 11647 /* wakeup() does the mutex_ext() */ 11648 } else { 11649 /* Q is empty, no need to wake */ 11650 sodp->sod_state &= SOD_WAKE_CLR; 11651 sodp->sod_state |= SOD_WAKE_NOT; 11652 mutex_exit(sodp->sod_lockp); 11653 } 11654 11655 /* No need for the push timer now. */ 11656 if (tcp->tcp_push_tid != 0) { 11657 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11658 tcp->tcp_push_tid = 0; 11659 } 11660 11661 return (ret); 11662 } 11663 11664 /* 11665 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11666 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11667 * to the user-land buffer and flag the mblk_t as such. 11668 * 11669 * Also, handle tcp_rwnd. 11670 */ 11671 uint_t 11672 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11673 { 11674 uioa_t *uioap = &sodp->sod_uioa; 11675 boolean_t qfull; 11676 uint_t thwin; 11677 11678 /* Can't be an eager connection */ 11679 ASSERT(tcp->tcp_listener == NULL); 11680 11681 /* Caller must have lock held */ 11682 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11683 11684 /* Sodirect mode so must not be a tcp_rcv_list */ 11685 ASSERT(tcp->tcp_rcv_list == NULL); 11686 11687 /* Passed in segment length must be equal to mblk_t chain data size */ 11688 ASSERT(seg_len == msgdsize(mp)); 11689 11690 if (DB_TYPE(mp) != M_DATA) { 11691 /* Only process M_DATA mblk_t's */ 11692 goto enq; 11693 } 11694 if (uioap->uioa_state & UIOA_ENABLED) { 11695 /* Uioa is enabled */ 11696 mblk_t *mp1 = mp; 11697 mblk_t *lmp = NULL; 11698 11699 if (seg_len > uioap->uio_resid) { 11700 /* 11701 * There isn't enough uio space for the mblk_t chain 11702 * so disable uioa such that this and any additional 11703 * mblk_t data is handled by the socket and schedule 11704 * the socket for wakeup to finish this uioa. 11705 */ 11706 uioap->uioa_state &= UIOA_CLR; 11707 uioap->uioa_state |= UIOA_FINI; 11708 if (sodp->sod_state & SOD_WAKE_NOT) { 11709 sodp->sod_state &= SOD_WAKE_CLR; 11710 sodp->sod_state |= SOD_WAKE_NEED; 11711 } 11712 goto enq; 11713 } 11714 do { 11715 uint32_t len = MBLKL(mp1); 11716 11717 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11718 /* Scheduled, mark dblk_t as such */ 11719 DB_FLAGS(mp1) |= DBLK_UIOA; 11720 } else { 11721 /* Error, turn off async processing */ 11722 uioap->uioa_state &= UIOA_CLR; 11723 uioap->uioa_state |= UIOA_FINI; 11724 break; 11725 } 11726 lmp = mp1; 11727 } while ((mp1 = mp1->b_cont) != NULL); 11728 11729 if (mp1 != NULL || uioap->uio_resid == 0) { 11730 /* 11731 * Not all mblk_t(s) uioamoved (error) or all uio 11732 * space has been consumed so schedule the socket 11733 * for wakeup to finish this uio. 11734 */ 11735 sodp->sod_state &= SOD_WAKE_CLR; 11736 sodp->sod_state |= SOD_WAKE_NEED; 11737 11738 /* Break the mblk chain if neccessary. */ 11739 if (mp1 != NULL && lmp != NULL) { 11740 mp->b_next = mp1; 11741 lmp->b_cont = NULL; 11742 } 11743 } 11744 } else if (uioap->uioa_state & UIOA_FINI) { 11745 /* 11746 * Post UIO_ENABLED waiting for socket to finish processing 11747 * so just enqueue and update tcp_rwnd. 11748 */ 11749 if (SOD_QFULL(sodp)) 11750 tcp->tcp_rwnd -= seg_len; 11751 } else if (sodp->sod_want > 0) { 11752 /* 11753 * Uioa isn't enabled but sodirect has a pending read(). 11754 */ 11755 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11756 if (sodp->sod_state & SOD_WAKE_NOT) { 11757 /* Schedule socket for wakeup */ 11758 sodp->sod_state &= SOD_WAKE_CLR; 11759 sodp->sod_state |= SOD_WAKE_NEED; 11760 } 11761 tcp->tcp_rwnd -= seg_len; 11762 } 11763 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11764 /* 11765 * No pending sodirect read() so used the default 11766 * TCP push logic to guess that a push is needed. 11767 */ 11768 if (sodp->sod_state & SOD_WAKE_NOT) { 11769 /* Schedule socket for wakeup */ 11770 sodp->sod_state &= SOD_WAKE_CLR; 11771 sodp->sod_state |= SOD_WAKE_NEED; 11772 } 11773 tcp->tcp_rwnd -= seg_len; 11774 } else { 11775 /* Just update tcp_rwnd */ 11776 tcp->tcp_rwnd -= seg_len; 11777 } 11778 enq: 11779 qfull = SOD_QFULL(sodp); 11780 11781 (sodp->sod_enqueue)(sodp, mp); 11782 11783 if (! qfull && SOD_QFULL(sodp)) { 11784 /* Wasn't QFULL, now QFULL, need back-enable */ 11785 SOD_QSETBE(sodp); 11786 } 11787 11788 /* 11789 * Check to see if remote avail swnd < mss due to delayed ACK, 11790 * first get advertised rwnd. 11791 */ 11792 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11793 /* Minus delayed ACK count */ 11794 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11795 if (thwin < tcp->tcp_mss) { 11796 /* Remote avail swnd < mss, need ACK now */ 11797 return (TH_ACK_NEEDED); 11798 } 11799 11800 return (0); 11801 } 11802 11803 /* 11804 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11805 * 11806 * This is the default entry function into TCP on the read side. TCP is 11807 * always entered via squeue i.e. using squeue's for mutual exclusion. 11808 * When classifier does a lookup to find the tcp, it also puts a reference 11809 * on the conn structure associated so the tcp is guaranteed to exist 11810 * when we come here. We still need to check the state because it might 11811 * as well has been closed. The squeue processing function i.e. squeue_enter, 11812 * is responsible for doing the CONN_DEC_REF. 11813 * 11814 * Apart from the default entry point, IP also sends packets directly to 11815 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11816 * connections. 11817 */ 11818 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11819 void 11820 tcp_input(void *arg, mblk_t *mp, void *arg2) 11821 { 11822 conn_t *connp = (conn_t *)arg; 11823 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11824 11825 /* arg2 is the sqp */ 11826 ASSERT(arg2 != NULL); 11827 ASSERT(mp != NULL); 11828 11829 /* 11830 * Don't accept any input on a closed tcp as this TCP logically does 11831 * not exist on the system. Don't proceed further with this TCP. 11832 * For eg. this packet could trigger another close of this tcp 11833 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11834 * tcp_clean_death / tcp_closei_local must be called at most once 11835 * on a TCP. In this case we need to refeed the packet into the 11836 * classifier and figure out where the packet should go. Need to 11837 * preserve the recv_ill somehow. Until we figure that out, for 11838 * now just drop the packet if we can't classify the packet. 11839 */ 11840 if (tcp->tcp_state == TCPS_CLOSED || 11841 tcp->tcp_state == TCPS_BOUND) { 11842 conn_t *new_connp; 11843 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11844 11845 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11846 if (new_connp != NULL) { 11847 tcp_reinput(new_connp, mp, arg2); 11848 return; 11849 } 11850 /* We failed to classify. For now just drop the packet */ 11851 freemsg(mp); 11852 return; 11853 } 11854 11855 if (DB_TYPE(mp) != M_DATA) { 11856 tcp_rput_common(tcp, mp); 11857 return; 11858 } 11859 11860 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11861 squeue_t *final_sqp; 11862 11863 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11864 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11865 DB_CKSUMSTART(mp) = 0; 11866 if (tcp->tcp_state == TCPS_SYN_SENT && 11867 connp->conn_final_sqp == NULL && 11868 tcp_outbound_squeue_switch) { 11869 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11870 connp->conn_final_sqp = final_sqp; 11871 if (connp->conn_final_sqp != connp->conn_sqp) { 11872 CONN_INC_REF(connp); 11873 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11874 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11875 tcp_rput_data, connp, ip_squeue_flag, 11876 SQTAG_CONNECT_FINISH); 11877 return; 11878 } 11879 } 11880 } 11881 tcp_rput_data(connp, mp, arg2); 11882 } 11883 11884 /* 11885 * The read side put procedure. 11886 * The packets passed up by ip are assume to be aligned according to 11887 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11888 */ 11889 static void 11890 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11891 { 11892 /* 11893 * tcp_rput_data() does not expect M_CTL except for the case 11894 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11895 * type. Need to make sure that any other M_CTLs don't make 11896 * it to tcp_rput_data since it is not expecting any and doesn't 11897 * check for it. 11898 */ 11899 if (DB_TYPE(mp) == M_CTL) { 11900 switch (*(uint32_t *)(mp->b_rptr)) { 11901 case TCP_IOC_ABORT_CONN: 11902 /* 11903 * Handle connection abort request. 11904 */ 11905 tcp_ioctl_abort_handler(tcp, mp); 11906 return; 11907 case IPSEC_IN: 11908 /* 11909 * Only secure icmp arrive in TCP and they 11910 * don't go through data path. 11911 */ 11912 tcp_icmp_error(tcp, mp); 11913 return; 11914 case IN_PKTINFO: 11915 /* 11916 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11917 * sockets that are receiving IPv4 traffic. tcp 11918 */ 11919 ASSERT(tcp->tcp_family == AF_INET6); 11920 ASSERT(tcp->tcp_ipv6_recvancillary & 11921 TCP_IPV6_RECVPKTINFO); 11922 tcp_rput_data(tcp->tcp_connp, mp, 11923 tcp->tcp_connp->conn_sqp); 11924 return; 11925 case MDT_IOC_INFO_UPDATE: 11926 /* 11927 * Handle Multidata information update; the 11928 * following routine will free the message. 11929 */ 11930 if (tcp->tcp_connp->conn_mdt_ok) { 11931 tcp_mdt_update(tcp, 11932 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11933 B_FALSE); 11934 } 11935 freemsg(mp); 11936 return; 11937 case LSO_IOC_INFO_UPDATE: 11938 /* 11939 * Handle LSO information update; the following 11940 * routine will free the message. 11941 */ 11942 if (tcp->tcp_connp->conn_lso_ok) { 11943 tcp_lso_update(tcp, 11944 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11945 } 11946 freemsg(mp); 11947 return; 11948 default: 11949 /* 11950 * tcp_icmp_err() will process the M_CTL packets. 11951 * Non-ICMP packets, if any, will be discarded in 11952 * tcp_icmp_err(). We will process the ICMP packet 11953 * even if we are TCP_IS_DETACHED_NONEAGER as the 11954 * incoming ICMP packet may result in changing 11955 * the tcp_mss, which we would need if we have 11956 * packets to retransmit. 11957 */ 11958 tcp_icmp_error(tcp, mp); 11959 return; 11960 } 11961 } 11962 11963 /* No point processing the message if tcp is already closed */ 11964 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11965 freemsg(mp); 11966 return; 11967 } 11968 11969 tcp_rput_other(tcp, mp); 11970 } 11971 11972 11973 /* The minimum of smoothed mean deviation in RTO calculation. */ 11974 #define TCP_SD_MIN 400 11975 11976 /* 11977 * Set RTO for this connection. The formula is from Jacobson and Karels' 11978 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11979 * are the same as those in Appendix A.2 of that paper. 11980 * 11981 * m = new measurement 11982 * sa = smoothed RTT average (8 * average estimates). 11983 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11984 */ 11985 static void 11986 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11987 { 11988 long m = TICK_TO_MSEC(rtt); 11989 clock_t sa = tcp->tcp_rtt_sa; 11990 clock_t sv = tcp->tcp_rtt_sd; 11991 clock_t rto; 11992 tcp_stack_t *tcps = tcp->tcp_tcps; 11993 11994 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11995 tcp->tcp_rtt_update++; 11996 11997 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11998 if (sa != 0) { 11999 /* 12000 * Update average estimator: 12001 * new rtt = 7/8 old rtt + 1/8 Error 12002 */ 12003 12004 /* m is now Error in estimate. */ 12005 m -= sa >> 3; 12006 if ((sa += m) <= 0) { 12007 /* 12008 * Don't allow the smoothed average to be negative. 12009 * We use 0 to denote reinitialization of the 12010 * variables. 12011 */ 12012 sa = 1; 12013 } 12014 12015 /* 12016 * Update deviation estimator: 12017 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 12018 */ 12019 if (m < 0) 12020 m = -m; 12021 m -= sv >> 2; 12022 sv += m; 12023 } else { 12024 /* 12025 * This follows BSD's implementation. So the reinitialized 12026 * RTO is 3 * m. We cannot go less than 2 because if the 12027 * link is bandwidth dominated, doubling the window size 12028 * during slow start means doubling the RTT. We want to be 12029 * more conservative when we reinitialize our estimates. 3 12030 * is just a convenient number. 12031 */ 12032 sa = m << 3; 12033 sv = m << 1; 12034 } 12035 if (sv < TCP_SD_MIN) { 12036 /* 12037 * We do not know that if sa captures the delay ACK 12038 * effect as in a long train of segments, a receiver 12039 * does not delay its ACKs. So set the minimum of sv 12040 * to be TCP_SD_MIN, which is default to 400 ms, twice 12041 * of BSD DATO. That means the minimum of mean 12042 * deviation is 100 ms. 12043 * 12044 */ 12045 sv = TCP_SD_MIN; 12046 } 12047 tcp->tcp_rtt_sa = sa; 12048 tcp->tcp_rtt_sd = sv; 12049 /* 12050 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12051 * 12052 * Add tcp_rexmit_interval extra in case of extreme environment 12053 * where the algorithm fails to work. The default value of 12054 * tcp_rexmit_interval_extra should be 0. 12055 * 12056 * As we use a finer grained clock than BSD and update 12057 * RTO for every ACKs, add in another .25 of RTT to the 12058 * deviation of RTO to accomodate burstiness of 1/4 of 12059 * window size. 12060 */ 12061 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12062 12063 if (rto > tcps->tcps_rexmit_interval_max) { 12064 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12065 } else if (rto < tcps->tcps_rexmit_interval_min) { 12066 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12067 } else { 12068 tcp->tcp_rto = rto; 12069 } 12070 12071 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12072 tcp->tcp_timer_backoff = 0; 12073 } 12074 12075 /* 12076 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12077 * send queue which starts at the given seq. no. 12078 * 12079 * Parameters: 12080 * tcp_t *tcp: the tcp instance pointer. 12081 * uint32_t seq: the starting seq. no of the requested segment. 12082 * int32_t *off: after the execution, *off will be the offset to 12083 * the returned mblk which points to the requested seq no. 12084 * It is the caller's responsibility to send in a non-null off. 12085 * 12086 * Return: 12087 * A mblk_t pointer pointing to the requested segment in send queue. 12088 */ 12089 static mblk_t * 12090 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12091 { 12092 int32_t cnt; 12093 mblk_t *mp; 12094 12095 /* Defensive coding. Make sure we don't send incorrect data. */ 12096 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12097 return (NULL); 12098 12099 cnt = seq - tcp->tcp_suna; 12100 mp = tcp->tcp_xmit_head; 12101 while (cnt > 0 && mp != NULL) { 12102 cnt -= mp->b_wptr - mp->b_rptr; 12103 if (cnt < 0) { 12104 cnt += mp->b_wptr - mp->b_rptr; 12105 break; 12106 } 12107 mp = mp->b_cont; 12108 } 12109 ASSERT(mp != NULL); 12110 *off = cnt; 12111 return (mp); 12112 } 12113 12114 /* 12115 * This function handles all retransmissions if SACK is enabled for this 12116 * connection. First it calculates how many segments can be retransmitted 12117 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12118 * segments. A segment is eligible if sack_cnt for that segment is greater 12119 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12120 * all eligible segments, it checks to see if TCP can send some new segments 12121 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12122 * 12123 * Parameters: 12124 * tcp_t *tcp: the tcp structure of the connection. 12125 * uint_t *flags: in return, appropriate value will be set for 12126 * tcp_rput_data(). 12127 */ 12128 static void 12129 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12130 { 12131 notsack_blk_t *notsack_blk; 12132 int32_t usable_swnd; 12133 int32_t mss; 12134 uint32_t seg_len; 12135 mblk_t *xmit_mp; 12136 tcp_stack_t *tcps = tcp->tcp_tcps; 12137 12138 ASSERT(tcp->tcp_sack_info != NULL); 12139 ASSERT(tcp->tcp_notsack_list != NULL); 12140 ASSERT(tcp->tcp_rexmit == B_FALSE); 12141 12142 /* Defensive coding in case there is a bug... */ 12143 if (tcp->tcp_notsack_list == NULL) { 12144 return; 12145 } 12146 notsack_blk = tcp->tcp_notsack_list; 12147 mss = tcp->tcp_mss; 12148 12149 /* 12150 * Limit the num of outstanding data in the network to be 12151 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12152 */ 12153 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12154 12155 /* At least retransmit 1 MSS of data. */ 12156 if (usable_swnd <= 0) { 12157 usable_swnd = mss; 12158 } 12159 12160 /* Make sure no new RTT samples will be taken. */ 12161 tcp->tcp_csuna = tcp->tcp_snxt; 12162 12163 notsack_blk = tcp->tcp_notsack_list; 12164 while (usable_swnd > 0) { 12165 mblk_t *snxt_mp, *tmp_mp; 12166 tcp_seq begin = tcp->tcp_sack_snxt; 12167 tcp_seq end; 12168 int32_t off; 12169 12170 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12171 if (SEQ_GT(notsack_blk->end, begin) && 12172 (notsack_blk->sack_cnt >= 12173 tcps->tcps_dupack_fast_retransmit)) { 12174 end = notsack_blk->end; 12175 if (SEQ_LT(begin, notsack_blk->begin)) { 12176 begin = notsack_blk->begin; 12177 } 12178 break; 12179 } 12180 } 12181 /* 12182 * All holes are filled. Manipulate tcp_cwnd to send more 12183 * if we can. Note that after the SACK recovery, tcp_cwnd is 12184 * set to tcp_cwnd_ssthresh. 12185 */ 12186 if (notsack_blk == NULL) { 12187 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12188 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12189 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12190 ASSERT(tcp->tcp_cwnd > 0); 12191 return; 12192 } else { 12193 usable_swnd = usable_swnd / mss; 12194 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12195 MAX(usable_swnd * mss, mss); 12196 *flags |= TH_XMIT_NEEDED; 12197 return; 12198 } 12199 } 12200 12201 /* 12202 * Note that we may send more than usable_swnd allows here 12203 * because of round off, but no more than 1 MSS of data. 12204 */ 12205 seg_len = end - begin; 12206 if (seg_len > mss) 12207 seg_len = mss; 12208 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12209 ASSERT(snxt_mp != NULL); 12210 /* This should not happen. Defensive coding again... */ 12211 if (snxt_mp == NULL) { 12212 return; 12213 } 12214 12215 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12216 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12217 if (xmit_mp == NULL) 12218 return; 12219 12220 usable_swnd -= seg_len; 12221 tcp->tcp_pipe += seg_len; 12222 tcp->tcp_sack_snxt = begin + seg_len; 12223 12224 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12225 12226 /* 12227 * Update the send timestamp to avoid false retransmission. 12228 */ 12229 snxt_mp->b_prev = (mblk_t *)lbolt; 12230 12231 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12232 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12233 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12234 /* 12235 * Update tcp_rexmit_max to extend this SACK recovery phase. 12236 * This happens when new data sent during fast recovery is 12237 * also lost. If TCP retransmits those new data, it needs 12238 * to extend SACK recover phase to avoid starting another 12239 * fast retransmit/recovery unnecessarily. 12240 */ 12241 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12242 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12243 } 12244 } 12245 } 12246 12247 /* 12248 * This function handles policy checking at TCP level for non-hard_bound/ 12249 * detached connections. 12250 */ 12251 static boolean_t 12252 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12253 boolean_t secure, boolean_t mctl_present) 12254 { 12255 ipsec_latch_t *ipl = NULL; 12256 ipsec_action_t *act = NULL; 12257 mblk_t *data_mp; 12258 ipsec_in_t *ii; 12259 const char *reason; 12260 kstat_named_t *counter; 12261 tcp_stack_t *tcps = tcp->tcp_tcps; 12262 ipsec_stack_t *ipss; 12263 ip_stack_t *ipst; 12264 12265 ASSERT(mctl_present || !secure); 12266 12267 ASSERT((ipha == NULL && ip6h != NULL) || 12268 (ip6h == NULL && ipha != NULL)); 12269 12270 /* 12271 * We don't necessarily have an ipsec_in_act action to verify 12272 * policy because of assymetrical policy where we have only 12273 * outbound policy and no inbound policy (possible with global 12274 * policy). 12275 */ 12276 if (!secure) { 12277 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12278 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12279 return (B_TRUE); 12280 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12281 "tcp_check_policy", ipha, ip6h, secure, 12282 tcps->tcps_netstack); 12283 ipss = tcps->tcps_netstack->netstack_ipsec; 12284 12285 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12286 DROPPER(ipss, ipds_tcp_clear), 12287 &tcps->tcps_dropper); 12288 return (B_FALSE); 12289 } 12290 12291 /* 12292 * We have a secure packet. 12293 */ 12294 if (act == NULL) { 12295 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12296 "tcp_check_policy", ipha, ip6h, secure, 12297 tcps->tcps_netstack); 12298 ipss = tcps->tcps_netstack->netstack_ipsec; 12299 12300 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12301 DROPPER(ipss, ipds_tcp_secure), 12302 &tcps->tcps_dropper); 12303 return (B_FALSE); 12304 } 12305 12306 /* 12307 * XXX This whole routine is currently incorrect. ipl should 12308 * be set to the latch pointer, but is currently not set, so 12309 * we initialize it to NULL to avoid picking up random garbage. 12310 */ 12311 if (ipl == NULL) 12312 return (B_TRUE); 12313 12314 data_mp = first_mp->b_cont; 12315 12316 ii = (ipsec_in_t *)first_mp->b_rptr; 12317 12318 ipst = tcps->tcps_netstack->netstack_ip; 12319 12320 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12321 &counter, tcp->tcp_connp)) { 12322 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12323 return (B_TRUE); 12324 } 12325 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12326 "tcp inbound policy mismatch: %s, packet dropped\n", 12327 reason); 12328 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12329 12330 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12331 &tcps->tcps_dropper); 12332 return (B_FALSE); 12333 } 12334 12335 /* 12336 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12337 * retransmission after a timeout. 12338 * 12339 * To limit the number of duplicate segments, we limit the number of segment 12340 * to be sent in one time to tcp_snd_burst, the burst variable. 12341 */ 12342 static void 12343 tcp_ss_rexmit(tcp_t *tcp) 12344 { 12345 uint32_t snxt; 12346 uint32_t smax; 12347 int32_t win; 12348 int32_t mss; 12349 int32_t off; 12350 int32_t burst = tcp->tcp_snd_burst; 12351 mblk_t *snxt_mp; 12352 tcp_stack_t *tcps = tcp->tcp_tcps; 12353 12354 /* 12355 * Note that tcp_rexmit can be set even though TCP has retransmitted 12356 * all unack'ed segments. 12357 */ 12358 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12359 smax = tcp->tcp_rexmit_max; 12360 snxt = tcp->tcp_rexmit_nxt; 12361 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12362 snxt = tcp->tcp_suna; 12363 } 12364 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12365 win -= snxt - tcp->tcp_suna; 12366 mss = tcp->tcp_mss; 12367 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12368 12369 while (SEQ_LT(snxt, smax) && (win > 0) && 12370 (burst > 0) && (snxt_mp != NULL)) { 12371 mblk_t *xmit_mp; 12372 mblk_t *old_snxt_mp = snxt_mp; 12373 uint32_t cnt = mss; 12374 12375 if (win < cnt) { 12376 cnt = win; 12377 } 12378 if (SEQ_GT(snxt + cnt, smax)) { 12379 cnt = smax - snxt; 12380 } 12381 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12382 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12383 if (xmit_mp == NULL) 12384 return; 12385 12386 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12387 12388 snxt += cnt; 12389 win -= cnt; 12390 /* 12391 * Update the send timestamp to avoid false 12392 * retransmission. 12393 */ 12394 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12395 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12396 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12397 12398 tcp->tcp_rexmit_nxt = snxt; 12399 burst--; 12400 } 12401 /* 12402 * If we have transmitted all we have at the time 12403 * we started the retranmission, we can leave 12404 * the rest of the job to tcp_wput_data(). But we 12405 * need to check the send window first. If the 12406 * win is not 0, go on with tcp_wput_data(). 12407 */ 12408 if (SEQ_LT(snxt, smax) || win == 0) { 12409 return; 12410 } 12411 } 12412 /* Only call tcp_wput_data() if there is data to be sent. */ 12413 if (tcp->tcp_unsent) { 12414 tcp_wput_data(tcp, NULL, B_FALSE); 12415 } 12416 } 12417 12418 /* 12419 * Process all TCP option in SYN segment. Note that this function should 12420 * be called after tcp_adapt_ire() is called so that the necessary info 12421 * from IRE is already set in the tcp structure. 12422 * 12423 * This function sets up the correct tcp_mss value according to the 12424 * MSS option value and our header size. It also sets up the window scale 12425 * and timestamp values, and initialize SACK info blocks. But it does not 12426 * change receive window size after setting the tcp_mss value. The caller 12427 * should do the appropriate change. 12428 */ 12429 void 12430 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12431 { 12432 int options; 12433 tcp_opt_t tcpopt; 12434 uint32_t mss_max; 12435 char *tmp_tcph; 12436 tcp_stack_t *tcps = tcp->tcp_tcps; 12437 12438 tcpopt.tcp = NULL; 12439 options = tcp_parse_options(tcph, &tcpopt); 12440 12441 /* 12442 * Process MSS option. Note that MSS option value does not account 12443 * for IP or TCP options. This means that it is equal to MTU - minimum 12444 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12445 * IPv6. 12446 */ 12447 if (!(options & TCP_OPT_MSS_PRESENT)) { 12448 if (tcp->tcp_ipversion == IPV4_VERSION) 12449 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12450 else 12451 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12452 } else { 12453 if (tcp->tcp_ipversion == IPV4_VERSION) 12454 mss_max = tcps->tcps_mss_max_ipv4; 12455 else 12456 mss_max = tcps->tcps_mss_max_ipv6; 12457 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12458 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12459 else if (tcpopt.tcp_opt_mss > mss_max) 12460 tcpopt.tcp_opt_mss = mss_max; 12461 } 12462 12463 /* Process Window Scale option. */ 12464 if (options & TCP_OPT_WSCALE_PRESENT) { 12465 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12466 tcp->tcp_snd_ws_ok = B_TRUE; 12467 } else { 12468 tcp->tcp_snd_ws = B_FALSE; 12469 tcp->tcp_snd_ws_ok = B_FALSE; 12470 tcp->tcp_rcv_ws = B_FALSE; 12471 } 12472 12473 /* Process Timestamp option. */ 12474 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12475 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12476 tmp_tcph = (char *)tcp->tcp_tcph; 12477 12478 tcp->tcp_snd_ts_ok = B_TRUE; 12479 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12480 tcp->tcp_last_rcv_lbolt = lbolt64; 12481 ASSERT(OK_32PTR(tmp_tcph)); 12482 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12483 12484 /* Fill in our template header with basic timestamp option. */ 12485 tmp_tcph += tcp->tcp_tcp_hdr_len; 12486 tmp_tcph[0] = TCPOPT_NOP; 12487 tmp_tcph[1] = TCPOPT_NOP; 12488 tmp_tcph[2] = TCPOPT_TSTAMP; 12489 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12490 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12491 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12492 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12493 } else { 12494 tcp->tcp_snd_ts_ok = B_FALSE; 12495 } 12496 12497 /* 12498 * Process SACK options. If SACK is enabled for this connection, 12499 * then allocate the SACK info structure. Note the following ways 12500 * when tcp_snd_sack_ok is set to true. 12501 * 12502 * For active connection: in tcp_adapt_ire() called in 12503 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12504 * is checked. 12505 * 12506 * For passive connection: in tcp_adapt_ire() called in 12507 * tcp_accept_comm(). 12508 * 12509 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12510 * That check makes sure that if we did not send a SACK OK option, 12511 * we will not enable SACK for this connection even though the other 12512 * side sends us SACK OK option. For active connection, the SACK 12513 * info structure has already been allocated. So we need to free 12514 * it if SACK is disabled. 12515 */ 12516 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12517 (tcp->tcp_snd_sack_ok || 12518 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12519 /* This should be true only in the passive case. */ 12520 if (tcp->tcp_sack_info == NULL) { 12521 ASSERT(TCP_IS_DETACHED(tcp)); 12522 tcp->tcp_sack_info = 12523 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12524 } 12525 if (tcp->tcp_sack_info == NULL) { 12526 tcp->tcp_snd_sack_ok = B_FALSE; 12527 } else { 12528 tcp->tcp_snd_sack_ok = B_TRUE; 12529 if (tcp->tcp_snd_ts_ok) { 12530 tcp->tcp_max_sack_blk = 3; 12531 } else { 12532 tcp->tcp_max_sack_blk = 4; 12533 } 12534 } 12535 } else { 12536 /* 12537 * Resetting tcp_snd_sack_ok to B_FALSE so that 12538 * no SACK info will be used for this 12539 * connection. This assumes that SACK usage 12540 * permission is negotiated. This may need 12541 * to be changed once this is clarified. 12542 */ 12543 if (tcp->tcp_sack_info != NULL) { 12544 ASSERT(tcp->tcp_notsack_list == NULL); 12545 kmem_cache_free(tcp_sack_info_cache, 12546 tcp->tcp_sack_info); 12547 tcp->tcp_sack_info = NULL; 12548 } 12549 tcp->tcp_snd_sack_ok = B_FALSE; 12550 } 12551 12552 /* 12553 * Now we know the exact TCP/IP header length, subtract 12554 * that from tcp_mss to get our side's MSS. 12555 */ 12556 tcp->tcp_mss -= tcp->tcp_hdr_len; 12557 /* 12558 * Here we assume that the other side's header size will be equal to 12559 * our header size. We calculate the real MSS accordingly. Need to 12560 * take into additional stuffs IPsec puts in. 12561 * 12562 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12563 */ 12564 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12565 ((tcp->tcp_ipversion == IPV4_VERSION ? 12566 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12567 12568 /* 12569 * Set MSS to the smaller one of both ends of the connection. 12570 * We should not have called tcp_mss_set() before, but our 12571 * side of the MSS should have been set to a proper value 12572 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12573 * STREAM head parameters properly. 12574 * 12575 * If we have a larger-than-16-bit window but the other side 12576 * didn't want to do window scale, tcp_rwnd_set() will take 12577 * care of that. 12578 */ 12579 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12580 } 12581 12582 /* 12583 * Sends the T_CONN_IND to the listener. The caller calls this 12584 * functions via squeue to get inside the listener's perimeter 12585 * once the 3 way hand shake is done a T_CONN_IND needs to be 12586 * sent. As an optimization, the caller can call this directly 12587 * if listener's perimeter is same as eager's. 12588 */ 12589 /* ARGSUSED */ 12590 void 12591 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12592 { 12593 conn_t *lconnp = (conn_t *)arg; 12594 tcp_t *listener = lconnp->conn_tcp; 12595 tcp_t *tcp; 12596 struct T_conn_ind *conn_ind; 12597 ipaddr_t *addr_cache; 12598 boolean_t need_send_conn_ind = B_FALSE; 12599 tcp_stack_t *tcps = listener->tcp_tcps; 12600 12601 /* retrieve the eager */ 12602 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12603 ASSERT(conn_ind->OPT_offset != 0 && 12604 conn_ind->OPT_length == sizeof (intptr_t)); 12605 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12606 conn_ind->OPT_length); 12607 12608 /* 12609 * TLI/XTI applications will get confused by 12610 * sending eager as an option since it violates 12611 * the option semantics. So remove the eager as 12612 * option since TLI/XTI app doesn't need it anyway. 12613 */ 12614 if (!TCP_IS_SOCKET(listener)) { 12615 conn_ind->OPT_length = 0; 12616 conn_ind->OPT_offset = 0; 12617 } 12618 if (listener->tcp_state == TCPS_CLOSED || 12619 TCP_IS_DETACHED(listener)) { 12620 /* 12621 * If listener has closed, it would have caused a 12622 * a cleanup/blowoff to happen for the eager. We 12623 * just need to return. 12624 */ 12625 freemsg(mp); 12626 return; 12627 } 12628 12629 12630 /* 12631 * if the conn_req_q is full defer passing up the 12632 * T_CONN_IND until space is availabe after t_accept() 12633 * processing 12634 */ 12635 mutex_enter(&listener->tcp_eager_lock); 12636 12637 /* 12638 * Take the eager out, if it is in the list of droppable eagers 12639 * as we are here because the 3W handshake is over. 12640 */ 12641 MAKE_UNDROPPABLE(tcp); 12642 12643 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12644 tcp_t *tail; 12645 12646 /* 12647 * The eager already has an extra ref put in tcp_rput_data 12648 * so that it stays till accept comes back even though it 12649 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12650 */ 12651 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12652 listener->tcp_conn_req_cnt_q0--; 12653 listener->tcp_conn_req_cnt_q++; 12654 12655 /* Move from SYN_RCVD to ESTABLISHED list */ 12656 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12657 tcp->tcp_eager_prev_q0; 12658 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12659 tcp->tcp_eager_next_q0; 12660 tcp->tcp_eager_prev_q0 = NULL; 12661 tcp->tcp_eager_next_q0 = NULL; 12662 12663 /* 12664 * Insert at end of the queue because sockfs 12665 * sends down T_CONN_RES in chronological 12666 * order. Leaving the older conn indications 12667 * at front of the queue helps reducing search 12668 * time. 12669 */ 12670 tail = listener->tcp_eager_last_q; 12671 if (tail != NULL) 12672 tail->tcp_eager_next_q = tcp; 12673 else 12674 listener->tcp_eager_next_q = tcp; 12675 listener->tcp_eager_last_q = tcp; 12676 tcp->tcp_eager_next_q = NULL; 12677 /* 12678 * Delay sending up the T_conn_ind until we are 12679 * done with the eager. Once we have have sent up 12680 * the T_conn_ind, the accept can potentially complete 12681 * any time and release the refhold we have on the eager. 12682 */ 12683 need_send_conn_ind = B_TRUE; 12684 } else { 12685 /* 12686 * Defer connection on q0 and set deferred 12687 * connection bit true 12688 */ 12689 tcp->tcp_conn_def_q0 = B_TRUE; 12690 12691 /* take tcp out of q0 ... */ 12692 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12693 tcp->tcp_eager_next_q0; 12694 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12695 tcp->tcp_eager_prev_q0; 12696 12697 /* ... and place it at the end of q0 */ 12698 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12699 tcp->tcp_eager_next_q0 = listener; 12700 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12701 listener->tcp_eager_prev_q0 = tcp; 12702 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12703 } 12704 12705 /* we have timed out before */ 12706 if (tcp->tcp_syn_rcvd_timeout != 0) { 12707 tcp->tcp_syn_rcvd_timeout = 0; 12708 listener->tcp_syn_rcvd_timeout--; 12709 if (listener->tcp_syn_defense && 12710 listener->tcp_syn_rcvd_timeout <= 12711 (tcps->tcps_conn_req_max_q0 >> 5) && 12712 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12713 listener->tcp_last_rcv_lbolt)) { 12714 /* 12715 * Turn off the defense mode if we 12716 * believe the SYN attack is over. 12717 */ 12718 listener->tcp_syn_defense = B_FALSE; 12719 if (listener->tcp_ip_addr_cache) { 12720 kmem_free((void *)listener->tcp_ip_addr_cache, 12721 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12722 listener->tcp_ip_addr_cache = NULL; 12723 } 12724 } 12725 } 12726 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12727 if (addr_cache != NULL) { 12728 /* 12729 * We have finished a 3-way handshake with this 12730 * remote host. This proves the IP addr is good. 12731 * Cache it! 12732 */ 12733 addr_cache[IP_ADDR_CACHE_HASH( 12734 tcp->tcp_remote)] = tcp->tcp_remote; 12735 } 12736 mutex_exit(&listener->tcp_eager_lock); 12737 if (need_send_conn_ind) 12738 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12739 } 12740 12741 /* 12742 * Send the newconn notification to ulp. The eager is blown off if the 12743 * notification fails. 12744 */ 12745 static void 12746 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12747 { 12748 if (IPCL_IS_NONSTR(lconnp)) { 12749 cred_t *cr; 12750 pid_t cpid; 12751 12752 cr = msg_getcred(mp, &cpid); 12753 12754 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12755 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12756 lconnp->conn_tcp); 12757 12758 /* Keep the message around in case of a fallback to TPI */ 12759 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12760 12761 /* 12762 * Notify the ULP about the newconn. It is guaranteed that no 12763 * tcp_accept() call will be made for the eager if the 12764 * notification fails, so it's safe to blow it off in that 12765 * case. 12766 * 12767 * The upper handle will be assigned when tcp_accept() is 12768 * called. 12769 */ 12770 if ((*lconnp->conn_upcalls->su_newconn) 12771 (lconnp->conn_upper_handle, 12772 (sock_lower_handle_t)econnp, 12773 &sock_tcp_downcalls, cr, cpid, 12774 &econnp->conn_upcalls) == NULL) { 12775 /* Failed to allocate a socket */ 12776 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12777 tcpEstabResets); 12778 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12779 econnp->conn_tcp->tcp_conn_req_seqnum); 12780 } 12781 } else { 12782 putnext(lconnp->conn_tcp->tcp_rq, mp); 12783 } 12784 } 12785 12786 mblk_t * 12787 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12788 uint_t *ifindexp, ip6_pkt_t *ippp) 12789 { 12790 ip_pktinfo_t *pinfo; 12791 ip6_t *ip6h; 12792 uchar_t *rptr; 12793 mblk_t *first_mp = mp; 12794 boolean_t mctl_present = B_FALSE; 12795 uint_t ifindex = 0; 12796 ip6_pkt_t ipp; 12797 uint_t ipvers; 12798 uint_t ip_hdr_len; 12799 tcp_stack_t *tcps = tcp->tcp_tcps; 12800 12801 rptr = mp->b_rptr; 12802 ASSERT(OK_32PTR(rptr)); 12803 ASSERT(tcp != NULL); 12804 ipp.ipp_fields = 0; 12805 12806 switch DB_TYPE(mp) { 12807 case M_CTL: 12808 mp = mp->b_cont; 12809 if (mp == NULL) { 12810 freemsg(first_mp); 12811 return (NULL); 12812 } 12813 if (DB_TYPE(mp) != M_DATA) { 12814 freemsg(first_mp); 12815 return (NULL); 12816 } 12817 mctl_present = B_TRUE; 12818 break; 12819 case M_DATA: 12820 break; 12821 default: 12822 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12823 freemsg(mp); 12824 return (NULL); 12825 } 12826 ipvers = IPH_HDR_VERSION(rptr); 12827 if (ipvers == IPV4_VERSION) { 12828 if (tcp == NULL) { 12829 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12830 goto done; 12831 } 12832 12833 ipp.ipp_fields |= IPPF_HOPLIMIT; 12834 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12835 12836 /* 12837 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12838 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12839 */ 12840 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12841 mctl_present) { 12842 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12843 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12844 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12845 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12846 ipp.ipp_fields |= IPPF_IFINDEX; 12847 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12848 ifindex = pinfo->ip_pkt_ifindex; 12849 } 12850 freeb(first_mp); 12851 mctl_present = B_FALSE; 12852 } 12853 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12854 } else { 12855 ip6h = (ip6_t *)rptr; 12856 12857 ASSERT(ipvers == IPV6_VERSION); 12858 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12859 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12860 ipp.ipp_hoplimit = ip6h->ip6_hops; 12861 12862 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12863 uint8_t nexthdrp; 12864 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12865 12866 /* Look for ifindex information */ 12867 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12868 ip6i_t *ip6i = (ip6i_t *)ip6h; 12869 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12870 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12871 freemsg(first_mp); 12872 return (NULL); 12873 } 12874 12875 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12876 ASSERT(ip6i->ip6i_ifindex != 0); 12877 ipp.ipp_fields |= IPPF_IFINDEX; 12878 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12879 ifindex = ip6i->ip6i_ifindex; 12880 } 12881 rptr = (uchar_t *)&ip6i[1]; 12882 mp->b_rptr = rptr; 12883 if (rptr == mp->b_wptr) { 12884 mblk_t *mp1; 12885 mp1 = mp->b_cont; 12886 freeb(mp); 12887 mp = mp1; 12888 rptr = mp->b_rptr; 12889 } 12890 if (MBLKL(mp) < IPV6_HDR_LEN + 12891 sizeof (tcph_t)) { 12892 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12893 freemsg(first_mp); 12894 return (NULL); 12895 } 12896 ip6h = (ip6_t *)rptr; 12897 } 12898 12899 /* 12900 * Find any potentially interesting extension headers 12901 * as well as the length of the IPv6 + extension 12902 * headers. 12903 */ 12904 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12905 /* Verify if this is a TCP packet */ 12906 if (nexthdrp != IPPROTO_TCP) { 12907 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12908 freemsg(first_mp); 12909 return (NULL); 12910 } 12911 } else { 12912 ip_hdr_len = IPV6_HDR_LEN; 12913 } 12914 } 12915 12916 done: 12917 if (ipversp != NULL) 12918 *ipversp = ipvers; 12919 if (ip_hdr_lenp != NULL) 12920 *ip_hdr_lenp = ip_hdr_len; 12921 if (ippp != NULL) 12922 *ippp = ipp; 12923 if (ifindexp != NULL) 12924 *ifindexp = ifindex; 12925 if (mctl_present) { 12926 freeb(first_mp); 12927 } 12928 return (mp); 12929 } 12930 12931 /* 12932 * Handle M_DATA messages from IP. Its called directly from IP via 12933 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12934 * in this path. 12935 * 12936 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12937 * v4 and v6), we are called through tcp_input() and a M_CTL can 12938 * be present for options but tcp_find_pktinfo() deals with it. We 12939 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12940 * 12941 * The first argument is always the connp/tcp to which the mp belongs. 12942 * There are no exceptions to this rule. The caller has already put 12943 * a reference on this connp/tcp and once tcp_rput_data() returns, 12944 * the squeue will do the refrele. 12945 * 12946 * The TH_SYN for the listener directly go to tcp_conn_request via 12947 * squeue. 12948 * 12949 * sqp: NULL = recursive, sqp != NULL means called from squeue 12950 */ 12951 void 12952 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12953 { 12954 int32_t bytes_acked; 12955 int32_t gap; 12956 mblk_t *mp1; 12957 uint_t flags; 12958 uint32_t new_swnd = 0; 12959 uchar_t *iphdr; 12960 uchar_t *rptr; 12961 int32_t rgap; 12962 uint32_t seg_ack; 12963 int seg_len; 12964 uint_t ip_hdr_len; 12965 uint32_t seg_seq; 12966 tcph_t *tcph; 12967 int urp; 12968 tcp_opt_t tcpopt; 12969 uint_t ipvers; 12970 ip6_pkt_t ipp; 12971 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12972 uint32_t cwnd; 12973 uint32_t add; 12974 int npkt; 12975 int mss; 12976 conn_t *connp = (conn_t *)arg; 12977 squeue_t *sqp = (squeue_t *)arg2; 12978 tcp_t *tcp = connp->conn_tcp; 12979 tcp_stack_t *tcps = tcp->tcp_tcps; 12980 12981 /* 12982 * RST from fused tcp loopback peer should trigger an unfuse. 12983 */ 12984 if (tcp->tcp_fused) { 12985 TCP_STAT(tcps, tcp_fusion_aborted); 12986 tcp_unfuse(tcp); 12987 } 12988 12989 iphdr = mp->b_rptr; 12990 rptr = mp->b_rptr; 12991 ASSERT(OK_32PTR(rptr)); 12992 12993 /* 12994 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12995 * processing here. For rest call tcp_find_pktinfo to fill up the 12996 * necessary information. 12997 */ 12998 if (IPCL_IS_TCP4(connp)) { 12999 ipvers = IPV4_VERSION; 13000 ip_hdr_len = IPH_HDR_LENGTH(rptr); 13001 } else { 13002 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 13003 NULL, &ipp); 13004 if (mp == NULL) { 13005 TCP_STAT(tcps, tcp_rput_v6_error); 13006 return; 13007 } 13008 iphdr = mp->b_rptr; 13009 rptr = mp->b_rptr; 13010 } 13011 ASSERT(DB_TYPE(mp) == M_DATA); 13012 ASSERT(mp->b_next == NULL); 13013 13014 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13015 seg_seq = ABE32_TO_U32(tcph->th_seq); 13016 seg_ack = ABE32_TO_U32(tcph->th_ack); 13017 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 13018 seg_len = (int)(mp->b_wptr - rptr) - 13019 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 13020 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 13021 do { 13022 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 13023 (uintptr_t)INT_MAX); 13024 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 13025 } while ((mp1 = mp1->b_cont) != NULL && 13026 mp1->b_datap->db_type == M_DATA); 13027 } 13028 13029 if (tcp->tcp_state == TCPS_TIME_WAIT) { 13030 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 13031 seg_len, tcph); 13032 return; 13033 } 13034 13035 if (sqp != NULL) { 13036 /* 13037 * This is the correct place to update tcp_last_recv_time. Note 13038 * that it is also updated for tcp structure that belongs to 13039 * global and listener queues which do not really need updating. 13040 * But that should not cause any harm. And it is updated for 13041 * all kinds of incoming segments, not only for data segments. 13042 */ 13043 tcp->tcp_last_recv_time = lbolt; 13044 } 13045 13046 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 13047 13048 BUMP_LOCAL(tcp->tcp_ibsegs); 13049 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13050 13051 if ((flags & TH_URG) && sqp != NULL) { 13052 /* 13053 * TCP can't handle urgent pointers that arrive before 13054 * the connection has been accept()ed since it can't 13055 * buffer OOB data. Discard segment if this happens. 13056 * 13057 * We can't just rely on a non-null tcp_listener to indicate 13058 * that the accept() has completed since unlinking of the 13059 * eager and completion of the accept are not atomic. 13060 * tcp_detached, when it is not set (B_FALSE) indicates 13061 * that the accept() has completed. 13062 * 13063 * Nor can it reassemble urgent pointers, so discard 13064 * if it's not the next segment expected. 13065 * 13066 * Otherwise, collapse chain into one mblk (discard if 13067 * that fails). This makes sure the headers, retransmitted 13068 * data, and new data all are in the same mblk. 13069 */ 13070 ASSERT(mp != NULL); 13071 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13072 freemsg(mp); 13073 return; 13074 } 13075 /* Update pointers into message */ 13076 iphdr = rptr = mp->b_rptr; 13077 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13078 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13079 /* 13080 * Since we can't handle any data with this urgent 13081 * pointer that is out of sequence, we expunge 13082 * the data. This allows us to still register 13083 * the urgent mark and generate the M_PCSIG, 13084 * which we can do. 13085 */ 13086 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13087 seg_len = 0; 13088 } 13089 } 13090 13091 switch (tcp->tcp_state) { 13092 case TCPS_SYN_SENT: 13093 if (flags & TH_ACK) { 13094 /* 13095 * Note that our stack cannot send data before a 13096 * connection is established, therefore the 13097 * following check is valid. Otherwise, it has 13098 * to be changed. 13099 */ 13100 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13101 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13102 freemsg(mp); 13103 if (flags & TH_RST) 13104 return; 13105 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13106 tcp, seg_ack, 0, TH_RST); 13107 return; 13108 } 13109 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13110 } 13111 if (flags & TH_RST) { 13112 freemsg(mp); 13113 if (flags & TH_ACK) 13114 (void) tcp_clean_death(tcp, 13115 ECONNREFUSED, 13); 13116 return; 13117 } 13118 if (!(flags & TH_SYN)) { 13119 freemsg(mp); 13120 return; 13121 } 13122 13123 /* Process all TCP options. */ 13124 tcp_process_options(tcp, tcph); 13125 /* 13126 * The following changes our rwnd to be a multiple of the 13127 * MIN(peer MSS, our MSS) for performance reason. 13128 */ 13129 (void) tcp_rwnd_set(tcp, 13130 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 13131 13132 /* Is the other end ECN capable? */ 13133 if (tcp->tcp_ecn_ok) { 13134 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13135 tcp->tcp_ecn_ok = B_FALSE; 13136 } 13137 } 13138 /* 13139 * Clear ECN flags because it may interfere with later 13140 * processing. 13141 */ 13142 flags &= ~(TH_ECE|TH_CWR); 13143 13144 tcp->tcp_irs = seg_seq; 13145 tcp->tcp_rack = seg_seq; 13146 tcp->tcp_rnxt = seg_seq + 1; 13147 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13148 if (!TCP_IS_DETACHED(tcp)) { 13149 /* Allocate room for SACK options if needed. */ 13150 if (tcp->tcp_snd_sack_ok) { 13151 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13152 tcp->tcp_hdr_len + 13153 TCPOPT_MAX_SACK_LEN + 13154 (tcp->tcp_loopback ? 0 : 13155 tcps->tcps_wroff_xtra)); 13156 } else { 13157 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13158 tcp->tcp_hdr_len + 13159 (tcp->tcp_loopback ? 0 : 13160 tcps->tcps_wroff_xtra)); 13161 } 13162 } 13163 if (flags & TH_ACK) { 13164 /* 13165 * If we can't get the confirmation upstream, pretend 13166 * we didn't even see this one. 13167 * 13168 * XXX: how can we pretend we didn't see it if we 13169 * have updated rnxt et. al. 13170 * 13171 * For loopback we defer sending up the T_CONN_CON 13172 * until after some checks below. 13173 */ 13174 mp1 = NULL; 13175 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13176 tcp->tcp_loopback ? &mp1 : NULL)) { 13177 freemsg(mp); 13178 return; 13179 } 13180 /* SYN was acked - making progress */ 13181 if (tcp->tcp_ipversion == IPV6_VERSION) 13182 tcp->tcp_ip_forward_progress = B_TRUE; 13183 13184 /* One for the SYN */ 13185 tcp->tcp_suna = tcp->tcp_iss + 1; 13186 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13187 tcp->tcp_state = TCPS_ESTABLISHED; 13188 13189 /* 13190 * If SYN was retransmitted, need to reset all 13191 * retransmission info. This is because this 13192 * segment will be treated as a dup ACK. 13193 */ 13194 if (tcp->tcp_rexmit) { 13195 tcp->tcp_rexmit = B_FALSE; 13196 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13197 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13198 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13199 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13200 tcp->tcp_ms_we_have_waited = 0; 13201 13202 /* 13203 * Set tcp_cwnd back to 1 MSS, per 13204 * recommendation from 13205 * draft-floyd-incr-init-win-01.txt, 13206 * Increasing TCP's Initial Window. 13207 */ 13208 tcp->tcp_cwnd = tcp->tcp_mss; 13209 } 13210 13211 tcp->tcp_swl1 = seg_seq; 13212 tcp->tcp_swl2 = seg_ack; 13213 13214 new_swnd = BE16_TO_U16(tcph->th_win); 13215 tcp->tcp_swnd = new_swnd; 13216 if (new_swnd > tcp->tcp_max_swnd) 13217 tcp->tcp_max_swnd = new_swnd; 13218 13219 /* 13220 * Always send the three-way handshake ack immediately 13221 * in order to make the connection complete as soon as 13222 * possible on the accepting host. 13223 */ 13224 flags |= TH_ACK_NEEDED; 13225 13226 /* 13227 * Special case for loopback. At this point we have 13228 * received SYN-ACK from the remote endpoint. In 13229 * order to ensure that both endpoints reach the 13230 * fused state prior to any data exchange, the final 13231 * ACK needs to be sent before we indicate T_CONN_CON 13232 * to the module upstream. 13233 */ 13234 if (tcp->tcp_loopback) { 13235 mblk_t *ack_mp; 13236 13237 ASSERT(!tcp->tcp_unfusable); 13238 ASSERT(mp1 != NULL); 13239 /* 13240 * For loopback, we always get a pure SYN-ACK 13241 * and only need to send back the final ACK 13242 * with no data (this is because the other 13243 * tcp is ours and we don't do T/TCP). This 13244 * final ACK triggers the passive side to 13245 * perform fusion in ESTABLISHED state. 13246 */ 13247 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13248 if (tcp->tcp_ack_tid != 0) { 13249 (void) TCP_TIMER_CANCEL(tcp, 13250 tcp->tcp_ack_tid); 13251 tcp->tcp_ack_tid = 0; 13252 } 13253 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13254 BUMP_LOCAL(tcp->tcp_obsegs); 13255 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13256 13257 if (!IPCL_IS_NONSTR(connp)) { 13258 /* Send up T_CONN_CON */ 13259 putnext(tcp->tcp_rq, mp1); 13260 } else { 13261 cred_t *cr; 13262 pid_t cpid; 13263 13264 cr = msg_getcred(mp1, &cpid); 13265 (*connp->conn_upcalls-> 13266 su_connected) 13267 (connp->conn_upper_handle, 13268 tcp->tcp_connid, cr, cpid); 13269 freemsg(mp1); 13270 } 13271 13272 freemsg(mp); 13273 return; 13274 } 13275 /* 13276 * Forget fusion; we need to handle more 13277 * complex cases below. Send the deferred 13278 * T_CONN_CON message upstream and proceed 13279 * as usual. Mark this tcp as not capable 13280 * of fusion. 13281 */ 13282 TCP_STAT(tcps, tcp_fusion_unfusable); 13283 tcp->tcp_unfusable = B_TRUE; 13284 if (!IPCL_IS_NONSTR(connp)) { 13285 putnext(tcp->tcp_rq, mp1); 13286 } else { 13287 cred_t *cr; 13288 pid_t cpid; 13289 13290 cr = msg_getcred(mp1, &cpid); 13291 (*connp->conn_upcalls->su_connected) 13292 (connp->conn_upper_handle, 13293 tcp->tcp_connid, cr, cpid); 13294 freemsg(mp1); 13295 } 13296 } 13297 13298 /* 13299 * Check to see if there is data to be sent. If 13300 * yes, set the transmit flag. Then check to see 13301 * if received data processing needs to be done. 13302 * If not, go straight to xmit_check. This short 13303 * cut is OK as we don't support T/TCP. 13304 */ 13305 if (tcp->tcp_unsent) 13306 flags |= TH_XMIT_NEEDED; 13307 13308 if (seg_len == 0 && !(flags & TH_URG)) { 13309 freemsg(mp); 13310 goto xmit_check; 13311 } 13312 13313 flags &= ~TH_SYN; 13314 seg_seq++; 13315 break; 13316 } 13317 tcp->tcp_state = TCPS_SYN_RCVD; 13318 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13319 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13320 if (mp1) { 13321 /* 13322 * See comment in tcp_conn_request() for why we use 13323 * the open() time pid here. 13324 */ 13325 DB_CPID(mp1) = tcp->tcp_cpid; 13326 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13327 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13328 } 13329 freemsg(mp); 13330 return; 13331 case TCPS_SYN_RCVD: 13332 if (flags & TH_ACK) { 13333 /* 13334 * In this state, a SYN|ACK packet is either bogus 13335 * because the other side must be ACKing our SYN which 13336 * indicates it has seen the ACK for their SYN and 13337 * shouldn't retransmit it or we're crossing SYNs 13338 * on active open. 13339 */ 13340 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13341 freemsg(mp); 13342 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13343 tcp, seg_ack, 0, TH_RST); 13344 return; 13345 } 13346 /* 13347 * NOTE: RFC 793 pg. 72 says this should be 13348 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13349 * but that would mean we have an ack that ignored 13350 * our SYN. 13351 */ 13352 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13353 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13354 freemsg(mp); 13355 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13356 tcp, seg_ack, 0, TH_RST); 13357 return; 13358 } 13359 } 13360 break; 13361 case TCPS_LISTEN: 13362 /* 13363 * Only a TLI listener can come through this path when a 13364 * acceptor is going back to be a listener and a packet 13365 * for the acceptor hits the classifier. For a socket 13366 * listener, this can never happen because a listener 13367 * can never accept connection on itself and hence a 13368 * socket acceptor can not go back to being a listener. 13369 */ 13370 ASSERT(!TCP_IS_SOCKET(tcp)); 13371 /*FALLTHRU*/ 13372 case TCPS_CLOSED: 13373 case TCPS_BOUND: { 13374 conn_t *new_connp; 13375 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13376 13377 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13378 if (new_connp != NULL) { 13379 tcp_reinput(new_connp, mp, connp->conn_sqp); 13380 return; 13381 } 13382 /* We failed to classify. For now just drop the packet */ 13383 freemsg(mp); 13384 return; 13385 } 13386 case TCPS_IDLE: 13387 /* 13388 * Handle the case where the tcp_clean_death() has happened 13389 * on a connection (application hasn't closed yet) but a packet 13390 * was already queued on squeue before tcp_clean_death() 13391 * was processed. Calling tcp_clean_death() twice on same 13392 * connection can result in weird behaviour. 13393 */ 13394 freemsg(mp); 13395 return; 13396 default: 13397 break; 13398 } 13399 13400 /* 13401 * Already on the correct queue/perimeter. 13402 * If this is a detached connection and not an eager 13403 * connection hanging off a listener then new data 13404 * (past the FIN) will cause a reset. 13405 * We do a special check here where it 13406 * is out of the main line, rather than check 13407 * if we are detached every time we see new 13408 * data down below. 13409 */ 13410 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13411 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13412 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13413 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13414 13415 freemsg(mp); 13416 /* 13417 * This could be an SSL closure alert. We're detached so just 13418 * acknowledge it this last time. 13419 */ 13420 if (tcp->tcp_kssl_ctx != NULL) { 13421 kssl_release_ctx(tcp->tcp_kssl_ctx); 13422 tcp->tcp_kssl_ctx = NULL; 13423 13424 tcp->tcp_rnxt += seg_len; 13425 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13426 flags |= TH_ACK_NEEDED; 13427 goto ack_check; 13428 } 13429 13430 tcp_xmit_ctl("new data when detached", tcp, 13431 tcp->tcp_snxt, 0, TH_RST); 13432 (void) tcp_clean_death(tcp, EPROTO, 12); 13433 return; 13434 } 13435 13436 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13437 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13438 new_swnd = BE16_TO_U16(tcph->th_win) << 13439 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13440 13441 if (tcp->tcp_snd_ts_ok) { 13442 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13443 /* 13444 * This segment is not acceptable. 13445 * Drop it and send back an ACK. 13446 */ 13447 freemsg(mp); 13448 flags |= TH_ACK_NEEDED; 13449 goto ack_check; 13450 } 13451 } else if (tcp->tcp_snd_sack_ok) { 13452 ASSERT(tcp->tcp_sack_info != NULL); 13453 tcpopt.tcp = tcp; 13454 /* 13455 * SACK info in already updated in tcp_parse_options. Ignore 13456 * all other TCP options... 13457 */ 13458 (void) tcp_parse_options(tcph, &tcpopt); 13459 } 13460 try_again:; 13461 mss = tcp->tcp_mss; 13462 gap = seg_seq - tcp->tcp_rnxt; 13463 rgap = tcp->tcp_rwnd - (gap + seg_len); 13464 /* 13465 * gap is the amount of sequence space between what we expect to see 13466 * and what we got for seg_seq. A positive value for gap means 13467 * something got lost. A negative value means we got some old stuff. 13468 */ 13469 if (gap < 0) { 13470 /* Old stuff present. Is the SYN in there? */ 13471 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13472 (seg_len != 0)) { 13473 flags &= ~TH_SYN; 13474 seg_seq++; 13475 urp--; 13476 /* Recompute the gaps after noting the SYN. */ 13477 goto try_again; 13478 } 13479 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13480 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13481 (seg_len > -gap ? -gap : seg_len)); 13482 /* Remove the old stuff from seg_len. */ 13483 seg_len += gap; 13484 /* 13485 * Anything left? 13486 * Make sure to check for unack'd FIN when rest of data 13487 * has been previously ack'd. 13488 */ 13489 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13490 /* 13491 * Resets are only valid if they lie within our offered 13492 * window. If the RST bit is set, we just ignore this 13493 * segment. 13494 */ 13495 if (flags & TH_RST) { 13496 freemsg(mp); 13497 return; 13498 } 13499 13500 /* 13501 * The arriving of dup data packets indicate that we 13502 * may have postponed an ack for too long, or the other 13503 * side's RTT estimate is out of shape. Start acking 13504 * more often. 13505 */ 13506 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13507 tcp->tcp_rack_cnt >= 1 && 13508 tcp->tcp_rack_abs_max > 2) { 13509 tcp->tcp_rack_abs_max--; 13510 } 13511 tcp->tcp_rack_cur_max = 1; 13512 13513 /* 13514 * This segment is "unacceptable". None of its 13515 * sequence space lies within our advertized window. 13516 * 13517 * Adjust seg_len to the original value for tracing. 13518 */ 13519 seg_len -= gap; 13520 if (tcp->tcp_debug) { 13521 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13522 "tcp_rput: unacceptable, gap %d, rgap %d, " 13523 "flags 0x%x, seg_seq %u, seg_ack %u, " 13524 "seg_len %d, rnxt %u, snxt %u, %s", 13525 gap, rgap, flags, seg_seq, seg_ack, 13526 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13527 tcp_display(tcp, NULL, 13528 DISP_ADDR_AND_PORT)); 13529 } 13530 13531 /* 13532 * Arrange to send an ACK in response to the 13533 * unacceptable segment per RFC 793 page 69. There 13534 * is only one small difference between ours and the 13535 * acceptability test in the RFC - we accept ACK-only 13536 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13537 * will be generated. 13538 * 13539 * Note that we have to ACK an ACK-only packet at least 13540 * for stacks that send 0-length keep-alives with 13541 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13542 * section 4.2.3.6. As long as we don't ever generate 13543 * an unacceptable packet in response to an incoming 13544 * packet that is unacceptable, it should not cause 13545 * "ACK wars". 13546 */ 13547 flags |= TH_ACK_NEEDED; 13548 13549 /* 13550 * Continue processing this segment in order to use the 13551 * ACK information it contains, but skip all other 13552 * sequence-number processing. Processing the ACK 13553 * information is necessary in order to 13554 * re-synchronize connections that may have lost 13555 * synchronization. 13556 * 13557 * We clear seg_len and flag fields related to 13558 * sequence number processing as they are not 13559 * to be trusted for an unacceptable segment. 13560 */ 13561 seg_len = 0; 13562 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13563 goto process_ack; 13564 } 13565 13566 /* Fix seg_seq, and chew the gap off the front. */ 13567 seg_seq = tcp->tcp_rnxt; 13568 urp += gap; 13569 do { 13570 mblk_t *mp2; 13571 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13572 (uintptr_t)UINT_MAX); 13573 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13574 if (gap > 0) { 13575 mp->b_rptr = mp->b_wptr - gap; 13576 break; 13577 } 13578 mp2 = mp; 13579 mp = mp->b_cont; 13580 freeb(mp2); 13581 } while (gap < 0); 13582 /* 13583 * If the urgent data has already been acknowledged, we 13584 * should ignore TH_URG below 13585 */ 13586 if (urp < 0) 13587 flags &= ~TH_URG; 13588 } 13589 /* 13590 * rgap is the amount of stuff received out of window. A negative 13591 * value is the amount out of window. 13592 */ 13593 if (rgap < 0) { 13594 mblk_t *mp2; 13595 13596 if (tcp->tcp_rwnd == 0) { 13597 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13598 } else { 13599 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13600 UPDATE_MIB(&tcps->tcps_mib, 13601 tcpInDataPastWinBytes, -rgap); 13602 } 13603 13604 /* 13605 * seg_len does not include the FIN, so if more than 13606 * just the FIN is out of window, we act like we don't 13607 * see it. (If just the FIN is out of window, rgap 13608 * will be zero and we will go ahead and acknowledge 13609 * the FIN.) 13610 */ 13611 flags &= ~TH_FIN; 13612 13613 /* Fix seg_len and make sure there is something left. */ 13614 seg_len += rgap; 13615 if (seg_len <= 0) { 13616 /* 13617 * Resets are only valid if they lie within our offered 13618 * window. If the RST bit is set, we just ignore this 13619 * segment. 13620 */ 13621 if (flags & TH_RST) { 13622 freemsg(mp); 13623 return; 13624 } 13625 13626 /* Per RFC 793, we need to send back an ACK. */ 13627 flags |= TH_ACK_NEEDED; 13628 13629 /* 13630 * Send SIGURG as soon as possible i.e. even 13631 * if the TH_URG was delivered in a window probe 13632 * packet (which will be unacceptable). 13633 * 13634 * We generate a signal if none has been generated 13635 * for this connection or if this is a new urgent 13636 * byte. Also send a zero-length "unmarked" message 13637 * to inform SIOCATMARK that this is not the mark. 13638 * 13639 * tcp_urp_last_valid is cleared when the T_exdata_ind 13640 * is sent up. This plus the check for old data 13641 * (gap >= 0) handles the wraparound of the sequence 13642 * number space without having to always track the 13643 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13644 * this max in its rcv_up variable). 13645 * 13646 * This prevents duplicate SIGURGS due to a "late" 13647 * zero-window probe when the T_EXDATA_IND has already 13648 * been sent up. 13649 */ 13650 if ((flags & TH_URG) && 13651 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13652 tcp->tcp_urp_last))) { 13653 if (IPCL_IS_NONSTR(connp)) { 13654 if (!TCP_IS_DETACHED(tcp)) { 13655 (*connp->conn_upcalls-> 13656 su_signal_oob) 13657 (connp->conn_upper_handle, 13658 urp); 13659 } 13660 } else { 13661 mp1 = allocb(0, BPRI_MED); 13662 if (mp1 == NULL) { 13663 freemsg(mp); 13664 return; 13665 } 13666 if (!TCP_IS_DETACHED(tcp) && 13667 !putnextctl1(tcp->tcp_rq, 13668 M_PCSIG, SIGURG)) { 13669 /* Try again on the rexmit. */ 13670 freemsg(mp1); 13671 freemsg(mp); 13672 return; 13673 } 13674 /* 13675 * If the next byte would be the mark 13676 * then mark with MARKNEXT else mark 13677 * with NOTMARKNEXT. 13678 */ 13679 if (gap == 0 && urp == 0) 13680 mp1->b_flag |= MSGMARKNEXT; 13681 else 13682 mp1->b_flag |= MSGNOTMARKNEXT; 13683 freemsg(tcp->tcp_urp_mark_mp); 13684 tcp->tcp_urp_mark_mp = mp1; 13685 flags |= TH_SEND_URP_MARK; 13686 } 13687 tcp->tcp_urp_last_valid = B_TRUE; 13688 tcp->tcp_urp_last = urp + seg_seq; 13689 } 13690 /* 13691 * If this is a zero window probe, continue to 13692 * process the ACK part. But we need to set seg_len 13693 * to 0 to avoid data processing. Otherwise just 13694 * drop the segment and send back an ACK. 13695 */ 13696 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13697 flags &= ~(TH_SYN | TH_URG); 13698 seg_len = 0; 13699 goto process_ack; 13700 } else { 13701 freemsg(mp); 13702 goto ack_check; 13703 } 13704 } 13705 /* Pitch out of window stuff off the end. */ 13706 rgap = seg_len; 13707 mp2 = mp; 13708 do { 13709 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13710 (uintptr_t)INT_MAX); 13711 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13712 if (rgap < 0) { 13713 mp2->b_wptr += rgap; 13714 if ((mp1 = mp2->b_cont) != NULL) { 13715 mp2->b_cont = NULL; 13716 freemsg(mp1); 13717 } 13718 break; 13719 } 13720 } while ((mp2 = mp2->b_cont) != NULL); 13721 } 13722 ok:; 13723 /* 13724 * TCP should check ECN info for segments inside the window only. 13725 * Therefore the check should be done here. 13726 */ 13727 if (tcp->tcp_ecn_ok) { 13728 if (flags & TH_CWR) { 13729 tcp->tcp_ecn_echo_on = B_FALSE; 13730 } 13731 /* 13732 * Note that both ECN_CE and CWR can be set in the 13733 * same segment. In this case, we once again turn 13734 * on ECN_ECHO. 13735 */ 13736 if (tcp->tcp_ipversion == IPV4_VERSION) { 13737 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13738 13739 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13740 tcp->tcp_ecn_echo_on = B_TRUE; 13741 } 13742 } else { 13743 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13744 13745 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13746 htonl(IPH_ECN_CE << 20)) { 13747 tcp->tcp_ecn_echo_on = B_TRUE; 13748 } 13749 } 13750 } 13751 13752 /* 13753 * Check whether we can update tcp_ts_recent. This test is 13754 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13755 * Extensions for High Performance: An Update", Internet Draft. 13756 */ 13757 if (tcp->tcp_snd_ts_ok && 13758 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13759 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13760 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13761 tcp->tcp_last_rcv_lbolt = lbolt64; 13762 } 13763 13764 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13765 /* 13766 * FIN in an out of order segment. We record this in 13767 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13768 * Clear the FIN so that any check on FIN flag will fail. 13769 * Remember that FIN also counts in the sequence number 13770 * space. So we need to ack out of order FIN only segments. 13771 */ 13772 if (flags & TH_FIN) { 13773 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13774 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13775 flags &= ~TH_FIN; 13776 flags |= TH_ACK_NEEDED; 13777 } 13778 if (seg_len > 0) { 13779 /* Fill in the SACK blk list. */ 13780 if (tcp->tcp_snd_sack_ok) { 13781 ASSERT(tcp->tcp_sack_info != NULL); 13782 tcp_sack_insert(tcp->tcp_sack_list, 13783 seg_seq, seg_seq + seg_len, 13784 &(tcp->tcp_num_sack_blk)); 13785 } 13786 13787 /* 13788 * Attempt reassembly and see if we have something 13789 * ready to go. 13790 */ 13791 mp = tcp_reass(tcp, mp, seg_seq); 13792 /* Always ack out of order packets */ 13793 flags |= TH_ACK_NEEDED | TH_PUSH; 13794 if (mp) { 13795 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13796 (uintptr_t)INT_MAX); 13797 seg_len = mp->b_cont ? msgdsize(mp) : 13798 (int)(mp->b_wptr - mp->b_rptr); 13799 seg_seq = tcp->tcp_rnxt; 13800 /* 13801 * A gap is filled and the seq num and len 13802 * of the gap match that of a previously 13803 * received FIN, put the FIN flag back in. 13804 */ 13805 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13806 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13807 flags |= TH_FIN; 13808 tcp->tcp_valid_bits &= 13809 ~TCP_OFO_FIN_VALID; 13810 } 13811 } else { 13812 /* 13813 * Keep going even with NULL mp. 13814 * There may be a useful ACK or something else 13815 * we don't want to miss. 13816 * 13817 * But TCP should not perform fast retransmit 13818 * because of the ack number. TCP uses 13819 * seg_len == 0 to determine if it is a pure 13820 * ACK. And this is not a pure ACK. 13821 */ 13822 seg_len = 0; 13823 ofo_seg = B_TRUE; 13824 } 13825 } 13826 } else if (seg_len > 0) { 13827 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13828 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13829 /* 13830 * If an out of order FIN was received before, and the seq 13831 * num and len of the new segment match that of the FIN, 13832 * put the FIN flag back in. 13833 */ 13834 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13835 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13836 flags |= TH_FIN; 13837 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13838 } 13839 } 13840 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13841 if (flags & TH_RST) { 13842 freemsg(mp); 13843 switch (tcp->tcp_state) { 13844 case TCPS_SYN_RCVD: 13845 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13846 break; 13847 case TCPS_ESTABLISHED: 13848 case TCPS_FIN_WAIT_1: 13849 case TCPS_FIN_WAIT_2: 13850 case TCPS_CLOSE_WAIT: 13851 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13852 break; 13853 case TCPS_CLOSING: 13854 case TCPS_LAST_ACK: 13855 (void) tcp_clean_death(tcp, 0, 16); 13856 break; 13857 default: 13858 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13859 (void) tcp_clean_death(tcp, ENXIO, 17); 13860 break; 13861 } 13862 return; 13863 } 13864 if (flags & TH_SYN) { 13865 /* 13866 * See RFC 793, Page 71 13867 * 13868 * The seq number must be in the window as it should 13869 * be "fixed" above. If it is outside window, it should 13870 * be already rejected. Note that we allow seg_seq to be 13871 * rnxt + rwnd because we want to accept 0 window probe. 13872 */ 13873 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13874 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13875 freemsg(mp); 13876 /* 13877 * If the ACK flag is not set, just use our snxt as the 13878 * seq number of the RST segment. 13879 */ 13880 if (!(flags & TH_ACK)) { 13881 seg_ack = tcp->tcp_snxt; 13882 } 13883 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13884 TH_RST|TH_ACK); 13885 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13886 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13887 return; 13888 } 13889 /* 13890 * urp could be -1 when the urp field in the packet is 0 13891 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13892 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13893 */ 13894 if (flags & TH_URG && urp >= 0) { 13895 if (!tcp->tcp_urp_last_valid || 13896 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13897 if (IPCL_IS_NONSTR(connp)) { 13898 if (!TCP_IS_DETACHED(tcp)) { 13899 (*connp->conn_upcalls->su_signal_oob) 13900 (connp->conn_upper_handle, urp); 13901 } 13902 } else { 13903 /* 13904 * If we haven't generated the signal yet for 13905 * this urgent pointer value, do it now. Also, 13906 * send up a zero-length M_DATA indicating 13907 * whether or not this is the mark. The latter 13908 * is not needed when a T_EXDATA_IND is sent up. 13909 * However, if there are allocation failures 13910 * this code relies on the sender retransmitting 13911 * and the socket code for determining the mark 13912 * should not block waiting for the peer to 13913 * transmit. Thus, for simplicity we always 13914 * send up the mark indication. 13915 */ 13916 mp1 = allocb(0, BPRI_MED); 13917 if (mp1 == NULL) { 13918 freemsg(mp); 13919 return; 13920 } 13921 if (!TCP_IS_DETACHED(tcp) && 13922 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13923 SIGURG)) { 13924 /* Try again on the rexmit. */ 13925 freemsg(mp1); 13926 freemsg(mp); 13927 return; 13928 } 13929 /* 13930 * Mark with NOTMARKNEXT for now. 13931 * The code below will change this to MARKNEXT 13932 * if we are at the mark. 13933 * 13934 * If there are allocation failures (e.g. in 13935 * dupmsg below) the next time tcp_rput_data 13936 * sees the urgent segment it will send up the 13937 * MSGMARKNEXT message. 13938 */ 13939 mp1->b_flag |= MSGNOTMARKNEXT; 13940 freemsg(tcp->tcp_urp_mark_mp); 13941 tcp->tcp_urp_mark_mp = mp1; 13942 flags |= TH_SEND_URP_MARK; 13943 #ifdef DEBUG 13944 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13945 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13946 "last %x, %s", 13947 seg_seq, urp, tcp->tcp_urp_last, 13948 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13949 #endif /* DEBUG */ 13950 } 13951 tcp->tcp_urp_last_valid = B_TRUE; 13952 tcp->tcp_urp_last = urp + seg_seq; 13953 } else if (tcp->tcp_urp_mark_mp != NULL) { 13954 /* 13955 * An allocation failure prevented the previous 13956 * tcp_rput_data from sending up the allocated 13957 * MSG*MARKNEXT message - send it up this time 13958 * around. 13959 */ 13960 flags |= TH_SEND_URP_MARK; 13961 } 13962 13963 /* 13964 * If the urgent byte is in this segment, make sure that it is 13965 * all by itself. This makes it much easier to deal with the 13966 * possibility of an allocation failure on the T_exdata_ind. 13967 * Note that seg_len is the number of bytes in the segment, and 13968 * urp is the offset into the segment of the urgent byte. 13969 * urp < seg_len means that the urgent byte is in this segment. 13970 */ 13971 if (urp < seg_len) { 13972 if (seg_len != 1) { 13973 uint32_t tmp_rnxt; 13974 /* 13975 * Break it up and feed it back in. 13976 * Re-attach the IP header. 13977 */ 13978 mp->b_rptr = iphdr; 13979 if (urp > 0) { 13980 /* 13981 * There is stuff before the urgent 13982 * byte. 13983 */ 13984 mp1 = dupmsg(mp); 13985 if (!mp1) { 13986 /* 13987 * Trim from urgent byte on. 13988 * The rest will come back. 13989 */ 13990 (void) adjmsg(mp, 13991 urp - seg_len); 13992 tcp_rput_data(connp, 13993 mp, NULL); 13994 return; 13995 } 13996 (void) adjmsg(mp1, urp - seg_len); 13997 /* Feed this piece back in. */ 13998 tmp_rnxt = tcp->tcp_rnxt; 13999 tcp_rput_data(connp, mp1, NULL); 14000 /* 14001 * If the data passed back in was not 14002 * processed (ie: bad ACK) sending 14003 * the remainder back in will cause a 14004 * loop. In this case, drop the 14005 * packet and let the sender try 14006 * sending a good packet. 14007 */ 14008 if (tmp_rnxt == tcp->tcp_rnxt) { 14009 freemsg(mp); 14010 return; 14011 } 14012 } 14013 if (urp != seg_len - 1) { 14014 uint32_t tmp_rnxt; 14015 /* 14016 * There is stuff after the urgent 14017 * byte. 14018 */ 14019 mp1 = dupmsg(mp); 14020 if (!mp1) { 14021 /* 14022 * Trim everything beyond the 14023 * urgent byte. The rest will 14024 * come back. 14025 */ 14026 (void) adjmsg(mp, 14027 urp + 1 - seg_len); 14028 tcp_rput_data(connp, 14029 mp, NULL); 14030 return; 14031 } 14032 (void) adjmsg(mp1, urp + 1 - seg_len); 14033 tmp_rnxt = tcp->tcp_rnxt; 14034 tcp_rput_data(connp, mp1, NULL); 14035 /* 14036 * If the data passed back in was not 14037 * processed (ie: bad ACK) sending 14038 * the remainder back in will cause a 14039 * loop. In this case, drop the 14040 * packet and let the sender try 14041 * sending a good packet. 14042 */ 14043 if (tmp_rnxt == tcp->tcp_rnxt) { 14044 freemsg(mp); 14045 return; 14046 } 14047 } 14048 tcp_rput_data(connp, mp, NULL); 14049 return; 14050 } 14051 /* 14052 * This segment contains only the urgent byte. We 14053 * have to allocate the T_exdata_ind, if we can. 14054 */ 14055 if (IPCL_IS_NONSTR(connp)) { 14056 int error; 14057 14058 (*connp->conn_upcalls->su_recv) 14059 (connp->conn_upper_handle, mp, seg_len, 14060 MSG_OOB, &error, NULL); 14061 mp = NULL; 14062 goto update_ack; 14063 } else if (!tcp->tcp_urp_mp) { 14064 struct T_exdata_ind *tei; 14065 mp1 = allocb(sizeof (struct T_exdata_ind), 14066 BPRI_MED); 14067 if (!mp1) { 14068 /* 14069 * Sigh... It'll be back. 14070 * Generate any MSG*MARK message now. 14071 */ 14072 freemsg(mp); 14073 seg_len = 0; 14074 if (flags & TH_SEND_URP_MARK) { 14075 14076 14077 ASSERT(tcp->tcp_urp_mark_mp); 14078 tcp->tcp_urp_mark_mp->b_flag &= 14079 ~MSGNOTMARKNEXT; 14080 tcp->tcp_urp_mark_mp->b_flag |= 14081 MSGMARKNEXT; 14082 } 14083 goto ack_check; 14084 } 14085 mp1->b_datap->db_type = M_PROTO; 14086 tei = (struct T_exdata_ind *)mp1->b_rptr; 14087 tei->PRIM_type = T_EXDATA_IND; 14088 tei->MORE_flag = 0; 14089 mp1->b_wptr = (uchar_t *)&tei[1]; 14090 tcp->tcp_urp_mp = mp1; 14091 #ifdef DEBUG 14092 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14093 "tcp_rput: allocated exdata_ind %s", 14094 tcp_display(tcp, NULL, 14095 DISP_PORT_ONLY)); 14096 #endif /* DEBUG */ 14097 /* 14098 * There is no need to send a separate MSG*MARK 14099 * message since the T_EXDATA_IND will be sent 14100 * now. 14101 */ 14102 flags &= ~TH_SEND_URP_MARK; 14103 freemsg(tcp->tcp_urp_mark_mp); 14104 tcp->tcp_urp_mark_mp = NULL; 14105 } 14106 /* 14107 * Now we are all set. On the next putnext upstream, 14108 * tcp_urp_mp will be non-NULL and will get prepended 14109 * to what has to be this piece containing the urgent 14110 * byte. If for any reason we abort this segment below, 14111 * if it comes back, we will have this ready, or it 14112 * will get blown off in close. 14113 */ 14114 } else if (urp == seg_len) { 14115 /* 14116 * The urgent byte is the next byte after this sequence 14117 * number. If there is data it is marked with 14118 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14119 * since it is not needed. Otherwise, if the code 14120 * above just allocated a zero-length tcp_urp_mark_mp 14121 * message, that message is tagged with MSGMARKNEXT. 14122 * Sending up these MSGMARKNEXT messages makes 14123 * SIOCATMARK work correctly even though 14124 * the T_EXDATA_IND will not be sent up until the 14125 * urgent byte arrives. 14126 */ 14127 if (seg_len != 0) { 14128 flags |= TH_MARKNEXT_NEEDED; 14129 freemsg(tcp->tcp_urp_mark_mp); 14130 tcp->tcp_urp_mark_mp = NULL; 14131 flags &= ~TH_SEND_URP_MARK; 14132 } else if (tcp->tcp_urp_mark_mp != NULL) { 14133 flags |= TH_SEND_URP_MARK; 14134 tcp->tcp_urp_mark_mp->b_flag &= 14135 ~MSGNOTMARKNEXT; 14136 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14137 } 14138 #ifdef DEBUG 14139 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14140 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14141 seg_len, flags, 14142 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14143 #endif /* DEBUG */ 14144 } 14145 #ifdef DEBUG 14146 else { 14147 /* Data left until we hit mark */ 14148 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14149 "tcp_rput: URP %d bytes left, %s", 14150 urp - seg_len, tcp_display(tcp, NULL, 14151 DISP_PORT_ONLY)); 14152 } 14153 #endif /* DEBUG */ 14154 } 14155 14156 process_ack: 14157 if (!(flags & TH_ACK)) { 14158 freemsg(mp); 14159 goto xmit_check; 14160 } 14161 } 14162 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14163 14164 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14165 tcp->tcp_ip_forward_progress = B_TRUE; 14166 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14167 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14168 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14169 /* 3-way handshake complete - pass up the T_CONN_IND */ 14170 tcp_t *listener = tcp->tcp_listener; 14171 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14172 14173 tcp->tcp_tconnind_started = B_TRUE; 14174 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14175 /* 14176 * We are here means eager is fine but it can 14177 * get a TH_RST at any point between now and till 14178 * accept completes and disappear. We need to 14179 * ensure that reference to eager is valid after 14180 * we get out of eager's perimeter. So we do 14181 * an extra refhold. 14182 */ 14183 CONN_INC_REF(connp); 14184 14185 /* 14186 * The listener also exists because of the refhold 14187 * done in tcp_conn_request. Its possible that it 14188 * might have closed. We will check that once we 14189 * get inside listeners context. 14190 */ 14191 CONN_INC_REF(listener->tcp_connp); 14192 if (listener->tcp_connp->conn_sqp == 14193 connp->conn_sqp) { 14194 /* 14195 * We optimize by not calling an SQUEUE_ENTER 14196 * on the listener since we know that the 14197 * listener and eager squeues are the same. 14198 * We are able to make this check safely only 14199 * because neither the eager nor the listener 14200 * can change its squeue. Only an active connect 14201 * can change its squeue 14202 */ 14203 tcp_send_conn_ind(listener->tcp_connp, mp, 14204 listener->tcp_connp->conn_sqp); 14205 CONN_DEC_REF(listener->tcp_connp); 14206 } else if (!tcp->tcp_loopback) { 14207 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14208 mp, tcp_send_conn_ind, 14209 listener->tcp_connp, SQ_FILL, 14210 SQTAG_TCP_CONN_IND); 14211 } else { 14212 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14213 mp, tcp_send_conn_ind, 14214 listener->tcp_connp, SQ_PROCESS, 14215 SQTAG_TCP_CONN_IND); 14216 } 14217 } 14218 14219 if (tcp->tcp_active_open) { 14220 /* 14221 * We are seeing the final ack in the three way 14222 * hand shake of a active open'ed connection 14223 * so we must send up a T_CONN_CON 14224 */ 14225 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14226 freemsg(mp); 14227 return; 14228 } 14229 /* 14230 * Don't fuse the loopback endpoints for 14231 * simultaneous active opens. 14232 */ 14233 if (tcp->tcp_loopback) { 14234 TCP_STAT(tcps, tcp_fusion_unfusable); 14235 tcp->tcp_unfusable = B_TRUE; 14236 } 14237 } 14238 14239 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14240 bytes_acked--; 14241 /* SYN was acked - making progress */ 14242 if (tcp->tcp_ipversion == IPV6_VERSION) 14243 tcp->tcp_ip_forward_progress = B_TRUE; 14244 14245 /* 14246 * If SYN was retransmitted, need to reset all 14247 * retransmission info as this segment will be 14248 * treated as a dup ACK. 14249 */ 14250 if (tcp->tcp_rexmit) { 14251 tcp->tcp_rexmit = B_FALSE; 14252 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14253 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14254 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14255 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14256 tcp->tcp_ms_we_have_waited = 0; 14257 tcp->tcp_cwnd = mss; 14258 } 14259 14260 /* 14261 * We set the send window to zero here. 14262 * This is needed if there is data to be 14263 * processed already on the queue. 14264 * Later (at swnd_update label), the 14265 * "new_swnd > tcp_swnd" condition is satisfied 14266 * the XMIT_NEEDED flag is set in the current 14267 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14268 * called if there is already data on queue in 14269 * this state. 14270 */ 14271 tcp->tcp_swnd = 0; 14272 14273 if (new_swnd > tcp->tcp_max_swnd) 14274 tcp->tcp_max_swnd = new_swnd; 14275 tcp->tcp_swl1 = seg_seq; 14276 tcp->tcp_swl2 = seg_ack; 14277 tcp->tcp_state = TCPS_ESTABLISHED; 14278 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14279 14280 /* Fuse when both sides are in ESTABLISHED state */ 14281 if (tcp->tcp_loopback && do_tcp_fusion) 14282 tcp_fuse(tcp, iphdr, tcph); 14283 14284 } 14285 /* This code follows 4.4BSD-Lite2 mostly. */ 14286 if (bytes_acked < 0) 14287 goto est; 14288 14289 /* 14290 * If TCP is ECN capable and the congestion experience bit is 14291 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14292 * done once per window (or more loosely, per RTT). 14293 */ 14294 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14295 tcp->tcp_cwr = B_FALSE; 14296 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14297 if (!tcp->tcp_cwr) { 14298 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14299 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14300 tcp->tcp_cwnd = npkt * mss; 14301 /* 14302 * If the cwnd is 0, use the timer to clock out 14303 * new segments. This is required by the ECN spec. 14304 */ 14305 if (npkt == 0) { 14306 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14307 /* 14308 * This makes sure that when the ACK comes 14309 * back, we will increase tcp_cwnd by 1 MSS. 14310 */ 14311 tcp->tcp_cwnd_cnt = 0; 14312 } 14313 tcp->tcp_cwr = B_TRUE; 14314 /* 14315 * This marks the end of the current window of in 14316 * flight data. That is why we don't use 14317 * tcp_suna + tcp_swnd. Only data in flight can 14318 * provide ECN info. 14319 */ 14320 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14321 tcp->tcp_ecn_cwr_sent = B_FALSE; 14322 } 14323 } 14324 14325 mp1 = tcp->tcp_xmit_head; 14326 if (bytes_acked == 0) { 14327 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14328 int dupack_cnt; 14329 14330 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14331 /* 14332 * Fast retransmit. When we have seen exactly three 14333 * identical ACKs while we have unacked data 14334 * outstanding we take it as a hint that our peer 14335 * dropped something. 14336 * 14337 * If TCP is retransmitting, don't do fast retransmit. 14338 */ 14339 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14340 ! tcp->tcp_rexmit) { 14341 /* Do Limited Transmit */ 14342 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14343 tcps->tcps_dupack_fast_retransmit) { 14344 /* 14345 * RFC 3042 14346 * 14347 * What we need to do is temporarily 14348 * increase tcp_cwnd so that new 14349 * data can be sent if it is allowed 14350 * by the receive window (tcp_rwnd). 14351 * tcp_wput_data() will take care of 14352 * the rest. 14353 * 14354 * If the connection is SACK capable, 14355 * only do limited xmit when there 14356 * is SACK info. 14357 * 14358 * Note how tcp_cwnd is incremented. 14359 * The first dup ACK will increase 14360 * it by 1 MSS. The second dup ACK 14361 * will increase it by 2 MSS. This 14362 * means that only 1 new segment will 14363 * be sent for each dup ACK. 14364 */ 14365 if (tcp->tcp_unsent > 0 && 14366 (!tcp->tcp_snd_sack_ok || 14367 (tcp->tcp_snd_sack_ok && 14368 tcp->tcp_notsack_list != NULL))) { 14369 tcp->tcp_cwnd += mss << 14370 (tcp->tcp_dupack_cnt - 1); 14371 flags |= TH_LIMIT_XMIT; 14372 } 14373 } else if (dupack_cnt == 14374 tcps->tcps_dupack_fast_retransmit) { 14375 14376 /* 14377 * If we have reduced tcp_ssthresh 14378 * because of ECN, do not reduce it again 14379 * unless it is already one window of data 14380 * away. After one window of data, tcp_cwr 14381 * should then be cleared. Note that 14382 * for non ECN capable connection, tcp_cwr 14383 * should always be false. 14384 * 14385 * Adjust cwnd since the duplicate 14386 * ack indicates that a packet was 14387 * dropped (due to congestion.) 14388 */ 14389 if (!tcp->tcp_cwr) { 14390 npkt = ((tcp->tcp_snxt - 14391 tcp->tcp_suna) >> 1) / mss; 14392 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14393 mss; 14394 tcp->tcp_cwnd = (npkt + 14395 tcp->tcp_dupack_cnt) * mss; 14396 } 14397 if (tcp->tcp_ecn_ok) { 14398 tcp->tcp_cwr = B_TRUE; 14399 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14400 tcp->tcp_ecn_cwr_sent = B_FALSE; 14401 } 14402 14403 /* 14404 * We do Hoe's algorithm. Refer to her 14405 * paper "Improving the Start-up Behavior 14406 * of a Congestion Control Scheme for TCP," 14407 * appeared in SIGCOMM'96. 14408 * 14409 * Save highest seq no we have sent so far. 14410 * Be careful about the invisible FIN byte. 14411 */ 14412 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14413 (tcp->tcp_unsent == 0)) { 14414 tcp->tcp_rexmit_max = tcp->tcp_fss; 14415 } else { 14416 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14417 } 14418 14419 /* 14420 * Do not allow bursty traffic during. 14421 * fast recovery. Refer to Fall and Floyd's 14422 * paper "Simulation-based Comparisons of 14423 * Tahoe, Reno and SACK TCP" (in CCR?) 14424 * This is a best current practise. 14425 */ 14426 tcp->tcp_snd_burst = TCP_CWND_SS; 14427 14428 /* 14429 * For SACK: 14430 * Calculate tcp_pipe, which is the 14431 * estimated number of bytes in 14432 * network. 14433 * 14434 * tcp_fack is the highest sack'ed seq num 14435 * TCP has received. 14436 * 14437 * tcp_pipe is explained in the above quoted 14438 * Fall and Floyd's paper. tcp_fack is 14439 * explained in Mathis and Mahdavi's 14440 * "Forward Acknowledgment: Refining TCP 14441 * Congestion Control" in SIGCOMM '96. 14442 */ 14443 if (tcp->tcp_snd_sack_ok) { 14444 ASSERT(tcp->tcp_sack_info != NULL); 14445 if (tcp->tcp_notsack_list != NULL) { 14446 tcp->tcp_pipe = tcp->tcp_snxt - 14447 tcp->tcp_fack; 14448 tcp->tcp_sack_snxt = seg_ack; 14449 flags |= TH_NEED_SACK_REXMIT; 14450 } else { 14451 /* 14452 * Always initialize tcp_pipe 14453 * even though we don't have 14454 * any SACK info. If later 14455 * we get SACK info and 14456 * tcp_pipe is not initialized, 14457 * funny things will happen. 14458 */ 14459 tcp->tcp_pipe = 14460 tcp->tcp_cwnd_ssthresh; 14461 } 14462 } else { 14463 flags |= TH_REXMIT_NEEDED; 14464 } /* tcp_snd_sack_ok */ 14465 14466 } else { 14467 /* 14468 * Here we perform congestion 14469 * avoidance, but NOT slow start. 14470 * This is known as the Fast 14471 * Recovery Algorithm. 14472 */ 14473 if (tcp->tcp_snd_sack_ok && 14474 tcp->tcp_notsack_list != NULL) { 14475 flags |= TH_NEED_SACK_REXMIT; 14476 tcp->tcp_pipe -= mss; 14477 if (tcp->tcp_pipe < 0) 14478 tcp->tcp_pipe = 0; 14479 } else { 14480 /* 14481 * We know that one more packet has 14482 * left the pipe thus we can update 14483 * cwnd. 14484 */ 14485 cwnd = tcp->tcp_cwnd + mss; 14486 if (cwnd > tcp->tcp_cwnd_max) 14487 cwnd = tcp->tcp_cwnd_max; 14488 tcp->tcp_cwnd = cwnd; 14489 if (tcp->tcp_unsent > 0) 14490 flags |= TH_XMIT_NEEDED; 14491 } 14492 } 14493 } 14494 } else if (tcp->tcp_zero_win_probe) { 14495 /* 14496 * If the window has opened, need to arrange 14497 * to send additional data. 14498 */ 14499 if (new_swnd != 0) { 14500 /* tcp_suna != tcp_snxt */ 14501 /* Packet contains a window update */ 14502 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14503 tcp->tcp_zero_win_probe = 0; 14504 tcp->tcp_timer_backoff = 0; 14505 tcp->tcp_ms_we_have_waited = 0; 14506 14507 /* 14508 * Transmit starting with tcp_suna since 14509 * the one byte probe is not ack'ed. 14510 * If TCP has sent more than one identical 14511 * probe, tcp_rexmit will be set. That means 14512 * tcp_ss_rexmit() will send out the one 14513 * byte along with new data. Otherwise, 14514 * fake the retransmission. 14515 */ 14516 flags |= TH_XMIT_NEEDED; 14517 if (!tcp->tcp_rexmit) { 14518 tcp->tcp_rexmit = B_TRUE; 14519 tcp->tcp_dupack_cnt = 0; 14520 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14521 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14522 } 14523 } 14524 } 14525 goto swnd_update; 14526 } 14527 14528 /* 14529 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14530 * If the ACK value acks something that we have not yet sent, it might 14531 * be an old duplicate segment. Send an ACK to re-synchronize the 14532 * other side. 14533 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14534 * state is handled above, so we can always just drop the segment and 14535 * send an ACK here. 14536 * 14537 * Should we send ACKs in response to ACK only segments? 14538 */ 14539 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14540 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14541 /* drop the received segment */ 14542 freemsg(mp); 14543 14544 /* 14545 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14546 * greater than 0, check if the number of such 14547 * bogus ACks is greater than that count. If yes, 14548 * don't send back any ACK. This prevents TCP from 14549 * getting into an ACK storm if somehow an attacker 14550 * successfully spoofs an acceptable segment to our 14551 * peer. 14552 */ 14553 if (tcp_drop_ack_unsent_cnt > 0 && 14554 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14555 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14556 return; 14557 } 14558 mp = tcp_ack_mp(tcp); 14559 if (mp != NULL) { 14560 BUMP_LOCAL(tcp->tcp_obsegs); 14561 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14562 tcp_send_data(tcp, tcp->tcp_wq, mp); 14563 } 14564 return; 14565 } 14566 14567 /* 14568 * TCP gets a new ACK, update the notsack'ed list to delete those 14569 * blocks that are covered by this ACK. 14570 */ 14571 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14572 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14573 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14574 } 14575 14576 /* 14577 * If we got an ACK after fast retransmit, check to see 14578 * if it is a partial ACK. If it is not and the congestion 14579 * window was inflated to account for the other side's 14580 * cached packets, retract it. If it is, do Hoe's algorithm. 14581 */ 14582 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14583 ASSERT(tcp->tcp_rexmit == B_FALSE); 14584 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14585 tcp->tcp_dupack_cnt = 0; 14586 /* 14587 * Restore the orig tcp_cwnd_ssthresh after 14588 * fast retransmit phase. 14589 */ 14590 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14591 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14592 } 14593 tcp->tcp_rexmit_max = seg_ack; 14594 tcp->tcp_cwnd_cnt = 0; 14595 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14596 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14597 14598 /* 14599 * Remove all notsack info to avoid confusion with 14600 * the next fast retrasnmit/recovery phase. 14601 */ 14602 if (tcp->tcp_snd_sack_ok && 14603 tcp->tcp_notsack_list != NULL) { 14604 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14605 } 14606 } else { 14607 if (tcp->tcp_snd_sack_ok && 14608 tcp->tcp_notsack_list != NULL) { 14609 flags |= TH_NEED_SACK_REXMIT; 14610 tcp->tcp_pipe -= mss; 14611 if (tcp->tcp_pipe < 0) 14612 tcp->tcp_pipe = 0; 14613 } else { 14614 /* 14615 * Hoe's algorithm: 14616 * 14617 * Retransmit the unack'ed segment and 14618 * restart fast recovery. Note that we 14619 * need to scale back tcp_cwnd to the 14620 * original value when we started fast 14621 * recovery. This is to prevent overly 14622 * aggressive behaviour in sending new 14623 * segments. 14624 */ 14625 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14626 tcps->tcps_dupack_fast_retransmit * mss; 14627 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14628 flags |= TH_REXMIT_NEEDED; 14629 } 14630 } 14631 } else { 14632 tcp->tcp_dupack_cnt = 0; 14633 if (tcp->tcp_rexmit) { 14634 /* 14635 * TCP is retranmitting. If the ACK ack's all 14636 * outstanding data, update tcp_rexmit_max and 14637 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14638 * to the correct value. 14639 * 14640 * Note that SEQ_LEQ() is used. This is to avoid 14641 * unnecessary fast retransmit caused by dup ACKs 14642 * received when TCP does slow start retransmission 14643 * after a time out. During this phase, TCP may 14644 * send out segments which are already received. 14645 * This causes dup ACKs to be sent back. 14646 */ 14647 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14648 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14649 tcp->tcp_rexmit_nxt = seg_ack; 14650 } 14651 if (seg_ack != tcp->tcp_rexmit_max) { 14652 flags |= TH_XMIT_NEEDED; 14653 } 14654 } else { 14655 tcp->tcp_rexmit = B_FALSE; 14656 tcp->tcp_xmit_zc_clean = B_FALSE; 14657 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14658 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14659 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14660 } 14661 tcp->tcp_ms_we_have_waited = 0; 14662 } 14663 } 14664 14665 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14666 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14667 tcp->tcp_suna = seg_ack; 14668 if (tcp->tcp_zero_win_probe != 0) { 14669 tcp->tcp_zero_win_probe = 0; 14670 tcp->tcp_timer_backoff = 0; 14671 } 14672 14673 /* 14674 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14675 * Note that it cannot be the SYN being ack'ed. The code flow 14676 * will not reach here. 14677 */ 14678 if (mp1 == NULL) { 14679 goto fin_acked; 14680 } 14681 14682 /* 14683 * Update the congestion window. 14684 * 14685 * If TCP is not ECN capable or TCP is ECN capable but the 14686 * congestion experience bit is not set, increase the tcp_cwnd as 14687 * usual. 14688 */ 14689 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14690 cwnd = tcp->tcp_cwnd; 14691 add = mss; 14692 14693 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14694 /* 14695 * This is to prevent an increase of less than 1 MSS of 14696 * tcp_cwnd. With partial increase, tcp_wput_data() 14697 * may send out tinygrams in order to preserve mblk 14698 * boundaries. 14699 * 14700 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14701 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14702 * increased by 1 MSS for every RTTs. 14703 */ 14704 if (tcp->tcp_cwnd_cnt <= 0) { 14705 tcp->tcp_cwnd_cnt = cwnd + add; 14706 } else { 14707 tcp->tcp_cwnd_cnt -= add; 14708 add = 0; 14709 } 14710 } 14711 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14712 } 14713 14714 /* See if the latest urgent data has been acknowledged */ 14715 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14716 SEQ_GT(seg_ack, tcp->tcp_urg)) 14717 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14718 14719 /* Can we update the RTT estimates? */ 14720 if (tcp->tcp_snd_ts_ok) { 14721 /* Ignore zero timestamp echo-reply. */ 14722 if (tcpopt.tcp_opt_ts_ecr != 0) { 14723 tcp_set_rto(tcp, (int32_t)lbolt - 14724 (int32_t)tcpopt.tcp_opt_ts_ecr); 14725 } 14726 14727 /* If needed, restart the timer. */ 14728 if (tcp->tcp_set_timer == 1) { 14729 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14730 tcp->tcp_set_timer = 0; 14731 } 14732 /* 14733 * Update tcp_csuna in case the other side stops sending 14734 * us timestamps. 14735 */ 14736 tcp->tcp_csuna = tcp->tcp_snxt; 14737 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14738 /* 14739 * An ACK sequence we haven't seen before, so get the RTT 14740 * and update the RTO. But first check if the timestamp is 14741 * valid to use. 14742 */ 14743 if ((mp1->b_next != NULL) && 14744 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14745 tcp_set_rto(tcp, (int32_t)lbolt - 14746 (int32_t)(intptr_t)mp1->b_prev); 14747 else 14748 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14749 14750 /* Remeber the last sequence to be ACKed */ 14751 tcp->tcp_csuna = seg_ack; 14752 if (tcp->tcp_set_timer == 1) { 14753 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14754 tcp->tcp_set_timer = 0; 14755 } 14756 } else { 14757 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14758 } 14759 14760 /* Eat acknowledged bytes off the xmit queue. */ 14761 for (;;) { 14762 mblk_t *mp2; 14763 uchar_t *wptr; 14764 14765 wptr = mp1->b_wptr; 14766 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14767 bytes_acked -= (int)(wptr - mp1->b_rptr); 14768 if (bytes_acked < 0) { 14769 mp1->b_rptr = wptr + bytes_acked; 14770 /* 14771 * Set a new timestamp if all the bytes timed by the 14772 * old timestamp have been ack'ed. 14773 */ 14774 if (SEQ_GT(seg_ack, 14775 (uint32_t)(uintptr_t)(mp1->b_next))) { 14776 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14777 mp1->b_next = NULL; 14778 } 14779 break; 14780 } 14781 mp1->b_next = NULL; 14782 mp1->b_prev = NULL; 14783 mp2 = mp1; 14784 mp1 = mp1->b_cont; 14785 14786 /* 14787 * This notification is required for some zero-copy 14788 * clients to maintain a copy semantic. After the data 14789 * is ack'ed, client is safe to modify or reuse the buffer. 14790 */ 14791 if (tcp->tcp_snd_zcopy_aware && 14792 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14793 tcp_zcopy_notify(tcp); 14794 freeb(mp2); 14795 if (bytes_acked == 0) { 14796 if (mp1 == NULL) { 14797 /* Everything is ack'ed, clear the tail. */ 14798 tcp->tcp_xmit_tail = NULL; 14799 /* 14800 * Cancel the timer unless we are still 14801 * waiting for an ACK for the FIN packet. 14802 */ 14803 if (tcp->tcp_timer_tid != 0 && 14804 tcp->tcp_snxt == tcp->tcp_suna) { 14805 (void) TCP_TIMER_CANCEL(tcp, 14806 tcp->tcp_timer_tid); 14807 tcp->tcp_timer_tid = 0; 14808 } 14809 goto pre_swnd_update; 14810 } 14811 if (mp2 != tcp->tcp_xmit_tail) 14812 break; 14813 tcp->tcp_xmit_tail = mp1; 14814 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14815 (uintptr_t)INT_MAX); 14816 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14817 mp1->b_rptr); 14818 break; 14819 } 14820 if (mp1 == NULL) { 14821 /* 14822 * More was acked but there is nothing more 14823 * outstanding. This means that the FIN was 14824 * just acked or that we're talking to a clown. 14825 */ 14826 fin_acked: 14827 ASSERT(tcp->tcp_fin_sent); 14828 tcp->tcp_xmit_tail = NULL; 14829 if (tcp->tcp_fin_sent) { 14830 /* FIN was acked - making progress */ 14831 if (tcp->tcp_ipversion == IPV6_VERSION && 14832 !tcp->tcp_fin_acked) 14833 tcp->tcp_ip_forward_progress = B_TRUE; 14834 tcp->tcp_fin_acked = B_TRUE; 14835 if (tcp->tcp_linger_tid != 0 && 14836 TCP_TIMER_CANCEL(tcp, 14837 tcp->tcp_linger_tid) >= 0) { 14838 tcp_stop_lingering(tcp); 14839 freemsg(mp); 14840 mp = NULL; 14841 } 14842 } else { 14843 /* 14844 * We should never get here because 14845 * we have already checked that the 14846 * number of bytes ack'ed should be 14847 * smaller than or equal to what we 14848 * have sent so far (it is the 14849 * acceptability check of the ACK). 14850 * We can only get here if the send 14851 * queue is corrupted. 14852 * 14853 * Terminate the connection and 14854 * panic the system. It is better 14855 * for us to panic instead of 14856 * continuing to avoid other disaster. 14857 */ 14858 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14859 tcp->tcp_rnxt, TH_RST|TH_ACK); 14860 panic("Memory corruption " 14861 "detected for connection %s.", 14862 tcp_display(tcp, NULL, 14863 DISP_ADDR_AND_PORT)); 14864 /*NOTREACHED*/ 14865 } 14866 goto pre_swnd_update; 14867 } 14868 ASSERT(mp2 != tcp->tcp_xmit_tail); 14869 } 14870 if (tcp->tcp_unsent) { 14871 flags |= TH_XMIT_NEEDED; 14872 } 14873 pre_swnd_update: 14874 tcp->tcp_xmit_head = mp1; 14875 swnd_update: 14876 /* 14877 * The following check is different from most other implementations. 14878 * For bi-directional transfer, when segments are dropped, the 14879 * "normal" check will not accept a window update in those 14880 * retransmitted segemnts. Failing to do that, TCP may send out 14881 * segments which are outside receiver's window. As TCP accepts 14882 * the ack in those retransmitted segments, if the window update in 14883 * the same segment is not accepted, TCP will incorrectly calculates 14884 * that it can send more segments. This can create a deadlock 14885 * with the receiver if its window becomes zero. 14886 */ 14887 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14888 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14889 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14890 /* 14891 * The criteria for update is: 14892 * 14893 * 1. the segment acknowledges some data. Or 14894 * 2. the segment is new, i.e. it has a higher seq num. Or 14895 * 3. the segment is not old and the advertised window is 14896 * larger than the previous advertised window. 14897 */ 14898 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14899 flags |= TH_XMIT_NEEDED; 14900 tcp->tcp_swnd = new_swnd; 14901 if (new_swnd > tcp->tcp_max_swnd) 14902 tcp->tcp_max_swnd = new_swnd; 14903 tcp->tcp_swl1 = seg_seq; 14904 tcp->tcp_swl2 = seg_ack; 14905 } 14906 est: 14907 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14908 14909 switch (tcp->tcp_state) { 14910 case TCPS_FIN_WAIT_1: 14911 if (tcp->tcp_fin_acked) { 14912 tcp->tcp_state = TCPS_FIN_WAIT_2; 14913 /* 14914 * We implement the non-standard BSD/SunOS 14915 * FIN_WAIT_2 flushing algorithm. 14916 * If there is no user attached to this 14917 * TCP endpoint, then this TCP struct 14918 * could hang around forever in FIN_WAIT_2 14919 * state if the peer forgets to send us 14920 * a FIN. To prevent this, we wait only 14921 * 2*MSL (a convenient time value) for 14922 * the FIN to arrive. If it doesn't show up, 14923 * we flush the TCP endpoint. This algorithm, 14924 * though a violation of RFC-793, has worked 14925 * for over 10 years in BSD systems. 14926 * Note: SunOS 4.x waits 675 seconds before 14927 * flushing the FIN_WAIT_2 connection. 14928 */ 14929 TCP_TIMER_RESTART(tcp, 14930 tcps->tcps_fin_wait_2_flush_interval); 14931 } 14932 break; 14933 case TCPS_FIN_WAIT_2: 14934 break; /* Shutdown hook? */ 14935 case TCPS_LAST_ACK: 14936 freemsg(mp); 14937 if (tcp->tcp_fin_acked) { 14938 (void) tcp_clean_death(tcp, 0, 19); 14939 return; 14940 } 14941 goto xmit_check; 14942 case TCPS_CLOSING: 14943 if (tcp->tcp_fin_acked) { 14944 tcp->tcp_state = TCPS_TIME_WAIT; 14945 /* 14946 * Unconditionally clear the exclusive binding 14947 * bit so this TIME-WAIT connection won't 14948 * interfere with new ones. 14949 */ 14950 tcp->tcp_exclbind = 0; 14951 if (!TCP_IS_DETACHED(tcp)) { 14952 TCP_TIMER_RESTART(tcp, 14953 tcps->tcps_time_wait_interval); 14954 } else { 14955 tcp_time_wait_append(tcp); 14956 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14957 } 14958 } 14959 /*FALLTHRU*/ 14960 case TCPS_CLOSE_WAIT: 14961 freemsg(mp); 14962 goto xmit_check; 14963 default: 14964 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14965 break; 14966 } 14967 } 14968 if (flags & TH_FIN) { 14969 /* Make sure we ack the fin */ 14970 flags |= TH_ACK_NEEDED; 14971 if (!tcp->tcp_fin_rcvd) { 14972 tcp->tcp_fin_rcvd = B_TRUE; 14973 tcp->tcp_rnxt++; 14974 tcph = tcp->tcp_tcph; 14975 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14976 14977 /* 14978 * Generate the ordrel_ind at the end unless we 14979 * are an eager guy. 14980 * In the eager case tcp_rsrv will do this when run 14981 * after tcp_accept is done. 14982 */ 14983 if (tcp->tcp_listener == NULL && 14984 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14985 flags |= TH_ORDREL_NEEDED; 14986 switch (tcp->tcp_state) { 14987 case TCPS_SYN_RCVD: 14988 case TCPS_ESTABLISHED: 14989 tcp->tcp_state = TCPS_CLOSE_WAIT; 14990 /* Keepalive? */ 14991 break; 14992 case TCPS_FIN_WAIT_1: 14993 if (!tcp->tcp_fin_acked) { 14994 tcp->tcp_state = TCPS_CLOSING; 14995 break; 14996 } 14997 /* FALLTHRU */ 14998 case TCPS_FIN_WAIT_2: 14999 tcp->tcp_state = TCPS_TIME_WAIT; 15000 /* 15001 * Unconditionally clear the exclusive binding 15002 * bit so this TIME-WAIT connection won't 15003 * interfere with new ones. 15004 */ 15005 tcp->tcp_exclbind = 0; 15006 if (!TCP_IS_DETACHED(tcp)) { 15007 TCP_TIMER_RESTART(tcp, 15008 tcps->tcps_time_wait_interval); 15009 } else { 15010 tcp_time_wait_append(tcp); 15011 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 15012 } 15013 if (seg_len) { 15014 /* 15015 * implies data piggybacked on FIN. 15016 * break to handle data. 15017 */ 15018 break; 15019 } 15020 freemsg(mp); 15021 goto ack_check; 15022 } 15023 } 15024 } 15025 if (mp == NULL) 15026 goto xmit_check; 15027 if (seg_len == 0) { 15028 freemsg(mp); 15029 goto xmit_check; 15030 } 15031 if (mp->b_rptr == mp->b_wptr) { 15032 /* 15033 * The header has been consumed, so we remove the 15034 * zero-length mblk here. 15035 */ 15036 mp1 = mp; 15037 mp = mp->b_cont; 15038 freeb(mp1); 15039 } 15040 update_ack: 15041 tcph = tcp->tcp_tcph; 15042 tcp->tcp_rack_cnt++; 15043 { 15044 uint32_t cur_max; 15045 15046 cur_max = tcp->tcp_rack_cur_max; 15047 if (tcp->tcp_rack_cnt >= cur_max) { 15048 /* 15049 * We have more unacked data than we should - send 15050 * an ACK now. 15051 */ 15052 flags |= TH_ACK_NEEDED; 15053 cur_max++; 15054 if (cur_max > tcp->tcp_rack_abs_max) 15055 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15056 else 15057 tcp->tcp_rack_cur_max = cur_max; 15058 } else if (TCP_IS_DETACHED(tcp)) { 15059 /* We don't have an ACK timer for detached TCP. */ 15060 flags |= TH_ACK_NEEDED; 15061 } else if (seg_len < mss) { 15062 /* 15063 * If we get a segment that is less than an mss, and we 15064 * already have unacknowledged data, and the amount 15065 * unacknowledged is not a multiple of mss, then we 15066 * better generate an ACK now. Otherwise, this may be 15067 * the tail piece of a transaction, and we would rather 15068 * wait for the response. 15069 */ 15070 uint32_t udif; 15071 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15072 (uintptr_t)INT_MAX); 15073 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15074 if (udif && (udif % mss)) 15075 flags |= TH_ACK_NEEDED; 15076 else 15077 flags |= TH_ACK_TIMER_NEEDED; 15078 } else { 15079 /* Start delayed ack timer */ 15080 flags |= TH_ACK_TIMER_NEEDED; 15081 } 15082 } 15083 tcp->tcp_rnxt += seg_len; 15084 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15085 15086 if (mp == NULL) 15087 goto xmit_check; 15088 15089 /* Update SACK list */ 15090 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15091 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15092 &(tcp->tcp_num_sack_blk)); 15093 } 15094 15095 if (tcp->tcp_urp_mp) { 15096 tcp->tcp_urp_mp->b_cont = mp; 15097 mp = tcp->tcp_urp_mp; 15098 tcp->tcp_urp_mp = NULL; 15099 /* Ready for a new signal. */ 15100 tcp->tcp_urp_last_valid = B_FALSE; 15101 #ifdef DEBUG 15102 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15103 "tcp_rput: sending exdata_ind %s", 15104 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15105 #endif /* DEBUG */ 15106 } 15107 15108 /* 15109 * Check for ancillary data changes compared to last segment. 15110 */ 15111 if (tcp->tcp_ipv6_recvancillary != 0) { 15112 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15113 ASSERT(mp != NULL); 15114 } 15115 15116 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15117 /* 15118 * Side queue inbound data until the accept happens. 15119 * tcp_accept/tcp_rput drains this when the accept happens. 15120 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15121 * T_EXDATA_IND) it is queued on b_next. 15122 * XXX Make urgent data use this. Requires: 15123 * Removing tcp_listener check for TH_URG 15124 * Making M_PCPROTO and MARK messages skip the eager case 15125 */ 15126 15127 if (tcp->tcp_kssl_pending) { 15128 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15129 mblk_t *, mp); 15130 tcp_kssl_input(tcp, mp); 15131 } else { 15132 tcp_rcv_enqueue(tcp, mp, seg_len); 15133 } 15134 } else { 15135 sodirect_t *sodp = tcp->tcp_sodirect; 15136 15137 /* 15138 * If an sodirect connection and an enabled sodirect_t then 15139 * sodp will be set to point to the tcp_t/sonode_t shared 15140 * sodirect_t and the sodirect_t's lock will be held. 15141 */ 15142 if (sodp != NULL) { 15143 mutex_enter(sodp->sod_lockp); 15144 if (!(sodp->sod_state & SOD_ENABLED) || 15145 (tcp->tcp_kssl_ctx != NULL && 15146 DB_TYPE(mp) == M_DATA)) { 15147 sodp = NULL; 15148 } 15149 mutex_exit(sodp->sod_lockp); 15150 } 15151 if (mp->b_datap->db_type != M_DATA || 15152 (flags & TH_MARKNEXT_NEEDED)) { 15153 if (IPCL_IS_NONSTR(connp)) { 15154 int error; 15155 15156 if ((*connp->conn_upcalls->su_recv) 15157 (connp->conn_upper_handle, mp, 15158 seg_len, 0, &error, NULL) <= 0) { 15159 if (error == ENOSPC) { 15160 tcp->tcp_rwnd -= seg_len; 15161 } else if (error == EOPNOTSUPP) { 15162 tcp_rcv_enqueue(tcp, mp, 15163 seg_len); 15164 } 15165 } 15166 } else if (sodp != NULL) { 15167 mutex_enter(sodp->sod_lockp); 15168 SOD_UIOAFINI(sodp); 15169 if (!SOD_QEMPTY(sodp) && 15170 (sodp->sod_state & SOD_WAKE_NOT)) { 15171 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15172 /* sod_wakeup() did the mutex_exit() */ 15173 } else { 15174 mutex_exit(sodp->sod_lockp); 15175 } 15176 } else if (tcp->tcp_rcv_list != NULL) { 15177 flags |= tcp_rcv_drain(tcp); 15178 } 15179 ASSERT(tcp->tcp_rcv_list == NULL || 15180 tcp->tcp_fused_sigurg); 15181 15182 if (flags & TH_MARKNEXT_NEEDED) { 15183 #ifdef DEBUG 15184 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15185 "tcp_rput: sending MSGMARKNEXT %s", 15186 tcp_display(tcp, NULL, 15187 DISP_PORT_ONLY)); 15188 #endif /* DEBUG */ 15189 mp->b_flag |= MSGMARKNEXT; 15190 flags &= ~TH_MARKNEXT_NEEDED; 15191 } 15192 15193 /* Does this need SSL processing first? */ 15194 if ((tcp->tcp_kssl_ctx != NULL) && 15195 (DB_TYPE(mp) == M_DATA)) { 15196 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15197 mblk_t *, mp); 15198 tcp_kssl_input(tcp, mp); 15199 } else if (!IPCL_IS_NONSTR(connp)) { 15200 /* Already handled non-STREAMS case. */ 15201 putnext(tcp->tcp_rq, mp); 15202 if (!canputnext(tcp->tcp_rq)) 15203 tcp->tcp_rwnd -= seg_len; 15204 } 15205 } else if ((tcp->tcp_kssl_ctx != NULL) && 15206 (DB_TYPE(mp) == M_DATA)) { 15207 /* Does this need SSL processing first? */ 15208 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15209 tcp_kssl_input(tcp, mp); 15210 } else if (IPCL_IS_NONSTR(connp)) { 15211 /* Non-STREAMS socket */ 15212 boolean_t push = flags & (TH_PUSH|TH_FIN); 15213 int error; 15214 15215 if ((*connp->conn_upcalls->su_recv)( 15216 connp->conn_upper_handle, 15217 mp, seg_len, 0, &error, &push) <= 0) { 15218 if (error == ENOSPC) { 15219 tcp->tcp_rwnd -= seg_len; 15220 } else if (error == EOPNOTSUPP) { 15221 tcp_rcv_enqueue(tcp, mp, seg_len); 15222 } 15223 } else if (push) { 15224 /* 15225 * PUSH bit set and sockfs is not 15226 * flow controlled 15227 */ 15228 flags |= tcp_rwnd_reopen(tcp); 15229 } 15230 } else if (sodp != NULL) { 15231 /* 15232 * Sodirect so all mblk_t's are queued on the 15233 * socket directly, check for wakeup of blocked 15234 * reader (if any), and last if flow-controled. 15235 */ 15236 mutex_enter(sodp->sod_lockp); 15237 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15238 if ((sodp->sod_state & SOD_WAKE_NEED) || 15239 (flags & (TH_PUSH|TH_FIN))) { 15240 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15241 /* sod_wakeup() did the mutex_exit() */ 15242 } else { 15243 if (SOD_QFULL(sodp)) { 15244 /* Q is full, need backenable */ 15245 SOD_QSETBE(sodp); 15246 } 15247 mutex_exit(sodp->sod_lockp); 15248 } 15249 } else if ((flags & (TH_PUSH|TH_FIN)) || 15250 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15251 if (tcp->tcp_rcv_list != NULL) { 15252 /* 15253 * Enqueue the new segment first and then 15254 * call tcp_rcv_drain() to send all data 15255 * up. The other way to do this is to 15256 * send all queued data up and then call 15257 * putnext() to send the new segment up. 15258 * This way can remove the else part later 15259 * on. 15260 * 15261 * We don't do this to avoid one more call to 15262 * canputnext() as tcp_rcv_drain() needs to 15263 * call canputnext(). 15264 */ 15265 tcp_rcv_enqueue(tcp, mp, seg_len); 15266 flags |= tcp_rcv_drain(tcp); 15267 } else { 15268 putnext(tcp->tcp_rq, mp); 15269 if (!canputnext(tcp->tcp_rq)) 15270 tcp->tcp_rwnd -= seg_len; 15271 } 15272 } else { 15273 /* 15274 * Enqueue all packets when processing an mblk 15275 * from the co queue and also enqueue normal packets. 15276 * For packets which belong to SSL stream do SSL 15277 * processing first. 15278 */ 15279 tcp_rcv_enqueue(tcp, mp, seg_len); 15280 } 15281 /* 15282 * Make sure the timer is running if we have data waiting 15283 * for a push bit. This provides resiliency against 15284 * implementations that do not correctly generate push bits. 15285 * 15286 * Note, for sodirect if Q isn't empty and there's not a 15287 * pending wakeup then we need a timer. Also note that sodp 15288 * is assumed to be still valid after exit()ing the sod_lockp 15289 * above and while the SOD state can change it can only change 15290 * such that the Q is empty now even though data was added 15291 * above. 15292 */ 15293 if (!IPCL_IS_NONSTR(connp) && 15294 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15295 (sodp->sod_state & SOD_WAKE_NOT)) || 15296 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15297 tcp->tcp_push_tid == 0) { 15298 /* 15299 * The connection may be closed at this point, so don't 15300 * do anything for a detached tcp. 15301 */ 15302 if (!TCP_IS_DETACHED(tcp)) 15303 tcp->tcp_push_tid = TCP_TIMER(tcp, 15304 tcp_push_timer, 15305 MSEC_TO_TICK( 15306 tcps->tcps_push_timer_interval)); 15307 } 15308 } 15309 15310 xmit_check: 15311 /* Is there anything left to do? */ 15312 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15313 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15314 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15315 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15316 goto done; 15317 15318 /* Any transmit work to do and a non-zero window? */ 15319 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15320 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15321 if (flags & TH_REXMIT_NEEDED) { 15322 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15323 15324 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15325 if (snd_size > mss) 15326 snd_size = mss; 15327 if (snd_size > tcp->tcp_swnd) 15328 snd_size = tcp->tcp_swnd; 15329 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15330 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15331 B_TRUE); 15332 15333 if (mp1 != NULL) { 15334 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15335 tcp->tcp_csuna = tcp->tcp_snxt; 15336 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15337 UPDATE_MIB(&tcps->tcps_mib, 15338 tcpRetransBytes, snd_size); 15339 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15340 } 15341 } 15342 if (flags & TH_NEED_SACK_REXMIT) { 15343 tcp_sack_rxmit(tcp, &flags); 15344 } 15345 /* 15346 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15347 * out new segment. Note that tcp_rexmit should not be 15348 * set, otherwise TH_LIMIT_XMIT should not be set. 15349 */ 15350 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15351 if (!tcp->tcp_rexmit) { 15352 tcp_wput_data(tcp, NULL, B_FALSE); 15353 } else { 15354 tcp_ss_rexmit(tcp); 15355 } 15356 } 15357 /* 15358 * Adjust tcp_cwnd back to normal value after sending 15359 * new data segments. 15360 */ 15361 if (flags & TH_LIMIT_XMIT) { 15362 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15363 /* 15364 * This will restart the timer. Restarting the 15365 * timer is used to avoid a timeout before the 15366 * limited transmitted segment's ACK gets back. 15367 */ 15368 if (tcp->tcp_xmit_head != NULL) 15369 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15370 } 15371 15372 /* Anything more to do? */ 15373 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15374 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15375 goto done; 15376 } 15377 ack_check: 15378 if (flags & TH_SEND_URP_MARK) { 15379 ASSERT(tcp->tcp_urp_mark_mp); 15380 ASSERT(!IPCL_IS_NONSTR(connp)); 15381 /* 15382 * Send up any queued data and then send the mark message 15383 */ 15384 sodirect_t *sodp; 15385 15386 SOD_PTR_ENTER(tcp, sodp); 15387 15388 mp1 = tcp->tcp_urp_mark_mp; 15389 tcp->tcp_urp_mark_mp = NULL; 15390 if (sodp != NULL) { 15391 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15392 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15393 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15394 } 15395 ASSERT(tcp->tcp_rcv_list == NULL); 15396 15397 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15398 /* sod_wakeup() does the mutex_exit() */ 15399 } else if (tcp->tcp_rcv_list != NULL) { 15400 flags |= tcp_rcv_drain(tcp); 15401 15402 ASSERT(tcp->tcp_rcv_list == NULL || 15403 tcp->tcp_fused_sigurg); 15404 15405 } 15406 putnext(tcp->tcp_rq, mp1); 15407 #ifdef DEBUG 15408 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15409 "tcp_rput: sending zero-length %s %s", 15410 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15411 "MSGNOTMARKNEXT"), 15412 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15413 #endif /* DEBUG */ 15414 flags &= ~TH_SEND_URP_MARK; 15415 } 15416 if (flags & TH_ACK_NEEDED) { 15417 /* 15418 * Time to send an ack for some reason. 15419 */ 15420 mp1 = tcp_ack_mp(tcp); 15421 15422 if (mp1 != NULL) { 15423 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15424 BUMP_LOCAL(tcp->tcp_obsegs); 15425 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15426 } 15427 if (tcp->tcp_ack_tid != 0) { 15428 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15429 tcp->tcp_ack_tid = 0; 15430 } 15431 } 15432 if (flags & TH_ACK_TIMER_NEEDED) { 15433 /* 15434 * Arrange for deferred ACK or push wait timeout. 15435 * Start timer if it is not already running. 15436 */ 15437 if (tcp->tcp_ack_tid == 0) { 15438 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15439 MSEC_TO_TICK(tcp->tcp_localnet ? 15440 (clock_t)tcps->tcps_local_dack_interval : 15441 (clock_t)tcps->tcps_deferred_ack_interval)); 15442 } 15443 } 15444 if (flags & TH_ORDREL_NEEDED) { 15445 /* 15446 * Send up the ordrel_ind unless we are an eager guy. 15447 * In the eager case tcp_rsrv will do this when run 15448 * after tcp_accept is done. 15449 */ 15450 sodirect_t *sodp; 15451 15452 ASSERT(tcp->tcp_listener == NULL); 15453 15454 if (IPCL_IS_NONSTR(connp)) { 15455 ASSERT(tcp->tcp_ordrel_mp == NULL); 15456 tcp->tcp_ordrel_done = B_TRUE; 15457 (*connp->conn_upcalls->su_opctl) 15458 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15459 goto done; 15460 } 15461 15462 SOD_PTR_ENTER(tcp, sodp); 15463 if (sodp != NULL) { 15464 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15465 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15466 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15467 } 15468 /* No more sodirect */ 15469 tcp->tcp_sodirect = NULL; 15470 if (!SOD_QEMPTY(sodp)) { 15471 /* Mblk(s) to process, notify */ 15472 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15473 /* sod_wakeup() does the mutex_exit() */ 15474 } else { 15475 /* Nothing to process */ 15476 mutex_exit(sodp->sod_lockp); 15477 } 15478 } else if (tcp->tcp_rcv_list != NULL) { 15479 /* 15480 * Push any mblk(s) enqueued from co processing. 15481 */ 15482 flags |= tcp_rcv_drain(tcp); 15483 15484 ASSERT(tcp->tcp_rcv_list == NULL || 15485 tcp->tcp_fused_sigurg); 15486 } 15487 15488 mp1 = tcp->tcp_ordrel_mp; 15489 tcp->tcp_ordrel_mp = NULL; 15490 tcp->tcp_ordrel_done = B_TRUE; 15491 putnext(tcp->tcp_rq, mp1); 15492 } 15493 done: 15494 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15495 } 15496 15497 /* 15498 * This function does PAWS protection check. Returns B_TRUE if the 15499 * segment passes the PAWS test, else returns B_FALSE. 15500 */ 15501 boolean_t 15502 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15503 { 15504 uint8_t flags; 15505 int options; 15506 uint8_t *up; 15507 15508 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15509 /* 15510 * If timestamp option is aligned nicely, get values inline, 15511 * otherwise call general routine to parse. Only do that 15512 * if timestamp is the only option. 15513 */ 15514 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15515 TCPOPT_REAL_TS_LEN && 15516 OK_32PTR((up = ((uint8_t *)tcph) + 15517 TCP_MIN_HEADER_LENGTH)) && 15518 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15519 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15520 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15521 15522 options = TCP_OPT_TSTAMP_PRESENT; 15523 } else { 15524 if (tcp->tcp_snd_sack_ok) { 15525 tcpoptp->tcp = tcp; 15526 } else { 15527 tcpoptp->tcp = NULL; 15528 } 15529 options = tcp_parse_options(tcph, tcpoptp); 15530 } 15531 15532 if (options & TCP_OPT_TSTAMP_PRESENT) { 15533 /* 15534 * Do PAWS per RFC 1323 section 4.2. Accept RST 15535 * regardless of the timestamp, page 18 RFC 1323.bis. 15536 */ 15537 if ((flags & TH_RST) == 0 && 15538 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15539 tcp->tcp_ts_recent)) { 15540 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15541 PAWS_TIMEOUT)) { 15542 /* This segment is not acceptable. */ 15543 return (B_FALSE); 15544 } else { 15545 /* 15546 * Connection has been idle for 15547 * too long. Reset the timestamp 15548 * and assume the segment is valid. 15549 */ 15550 tcp->tcp_ts_recent = 15551 tcpoptp->tcp_opt_ts_val; 15552 } 15553 } 15554 } else { 15555 /* 15556 * If we don't get a timestamp on every packet, we 15557 * figure we can't really trust 'em, so we stop sending 15558 * and parsing them. 15559 */ 15560 tcp->tcp_snd_ts_ok = B_FALSE; 15561 15562 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15563 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15564 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15565 /* 15566 * Adjust the tcp_mss accordingly. We also need to 15567 * adjust tcp_cwnd here in accordance with the new mss. 15568 * But we avoid doing a slow start here so as to not 15569 * to lose on the transfer rate built up so far. 15570 */ 15571 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15572 if (tcp->tcp_snd_sack_ok) { 15573 ASSERT(tcp->tcp_sack_info != NULL); 15574 tcp->tcp_max_sack_blk = 4; 15575 } 15576 } 15577 return (B_TRUE); 15578 } 15579 15580 /* 15581 * Attach ancillary data to a received TCP segments for the 15582 * ancillary pieces requested by the application that are 15583 * different than they were in the previous data segment. 15584 * 15585 * Save the "current" values once memory allocation is ok so that 15586 * when memory allocation fails we can just wait for the next data segment. 15587 */ 15588 static mblk_t * 15589 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15590 { 15591 struct T_optdata_ind *todi; 15592 int optlen; 15593 uchar_t *optptr; 15594 struct T_opthdr *toh; 15595 uint_t addflag; /* Which pieces to add */ 15596 mblk_t *mp1; 15597 15598 optlen = 0; 15599 addflag = 0; 15600 /* If app asked for pktinfo and the index has changed ... */ 15601 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15602 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15603 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15604 optlen += sizeof (struct T_opthdr) + 15605 sizeof (struct in6_pktinfo); 15606 addflag |= TCP_IPV6_RECVPKTINFO; 15607 } 15608 /* If app asked for hoplimit and it has changed ... */ 15609 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15610 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15611 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15612 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15613 addflag |= TCP_IPV6_RECVHOPLIMIT; 15614 } 15615 /* If app asked for tclass and it has changed ... */ 15616 if ((ipp->ipp_fields & IPPF_TCLASS) && 15617 ipp->ipp_tclass != tcp->tcp_recvtclass && 15618 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15619 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15620 addflag |= TCP_IPV6_RECVTCLASS; 15621 } 15622 /* 15623 * If app asked for hopbyhop headers and it has changed ... 15624 * For security labels, note that (1) security labels can't change on 15625 * a connected socket at all, (2) we're connected to at most one peer, 15626 * (3) if anything changes, then it must be some other extra option. 15627 */ 15628 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15629 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15630 (ipp->ipp_fields & IPPF_HOPOPTS), 15631 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15632 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15633 tcp->tcp_label_len; 15634 addflag |= TCP_IPV6_RECVHOPOPTS; 15635 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15636 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15637 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15638 return (mp); 15639 } 15640 /* If app asked for dst headers before routing headers ... */ 15641 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15642 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15643 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15644 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15645 optlen += sizeof (struct T_opthdr) + 15646 ipp->ipp_rtdstoptslen; 15647 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15648 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15649 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15650 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15651 return (mp); 15652 } 15653 /* If app asked for routing headers and it has changed ... */ 15654 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15655 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15656 (ipp->ipp_fields & IPPF_RTHDR), 15657 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15658 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15659 addflag |= TCP_IPV6_RECVRTHDR; 15660 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15661 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15662 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15663 return (mp); 15664 } 15665 /* If app asked for dest headers and it has changed ... */ 15666 if ((tcp->tcp_ipv6_recvancillary & 15667 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15668 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15669 (ipp->ipp_fields & IPPF_DSTOPTS), 15670 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15671 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15672 addflag |= TCP_IPV6_RECVDSTOPTS; 15673 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15674 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15675 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15676 return (mp); 15677 } 15678 15679 if (optlen == 0) { 15680 /* Nothing to add */ 15681 return (mp); 15682 } 15683 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15684 if (mp1 == NULL) { 15685 /* 15686 * Defer sending ancillary data until the next TCP segment 15687 * arrives. 15688 */ 15689 return (mp); 15690 } 15691 mp1->b_cont = mp; 15692 mp = mp1; 15693 mp->b_wptr += sizeof (*todi) + optlen; 15694 mp->b_datap->db_type = M_PROTO; 15695 todi = (struct T_optdata_ind *)mp->b_rptr; 15696 todi->PRIM_type = T_OPTDATA_IND; 15697 todi->DATA_flag = 1; /* MORE data */ 15698 todi->OPT_length = optlen; 15699 todi->OPT_offset = sizeof (*todi); 15700 optptr = (uchar_t *)&todi[1]; 15701 /* 15702 * If app asked for pktinfo and the index has changed ... 15703 * Note that the local address never changes for the connection. 15704 */ 15705 if (addflag & TCP_IPV6_RECVPKTINFO) { 15706 struct in6_pktinfo *pkti; 15707 15708 toh = (struct T_opthdr *)optptr; 15709 toh->level = IPPROTO_IPV6; 15710 toh->name = IPV6_PKTINFO; 15711 toh->len = sizeof (*toh) + sizeof (*pkti); 15712 toh->status = 0; 15713 optptr += sizeof (*toh); 15714 pkti = (struct in6_pktinfo *)optptr; 15715 if (tcp->tcp_ipversion == IPV6_VERSION) 15716 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15717 else 15718 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15719 &pkti->ipi6_addr); 15720 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15721 optptr += sizeof (*pkti); 15722 ASSERT(OK_32PTR(optptr)); 15723 /* Save as "last" value */ 15724 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15725 } 15726 /* If app asked for hoplimit and it has changed ... */ 15727 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15728 toh = (struct T_opthdr *)optptr; 15729 toh->level = IPPROTO_IPV6; 15730 toh->name = IPV6_HOPLIMIT; 15731 toh->len = sizeof (*toh) + sizeof (uint_t); 15732 toh->status = 0; 15733 optptr += sizeof (*toh); 15734 *(uint_t *)optptr = ipp->ipp_hoplimit; 15735 optptr += sizeof (uint_t); 15736 ASSERT(OK_32PTR(optptr)); 15737 /* Save as "last" value */ 15738 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15739 } 15740 /* If app asked for tclass and it has changed ... */ 15741 if (addflag & TCP_IPV6_RECVTCLASS) { 15742 toh = (struct T_opthdr *)optptr; 15743 toh->level = IPPROTO_IPV6; 15744 toh->name = IPV6_TCLASS; 15745 toh->len = sizeof (*toh) + sizeof (uint_t); 15746 toh->status = 0; 15747 optptr += sizeof (*toh); 15748 *(uint_t *)optptr = ipp->ipp_tclass; 15749 optptr += sizeof (uint_t); 15750 ASSERT(OK_32PTR(optptr)); 15751 /* Save as "last" value */ 15752 tcp->tcp_recvtclass = ipp->ipp_tclass; 15753 } 15754 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15755 toh = (struct T_opthdr *)optptr; 15756 toh->level = IPPROTO_IPV6; 15757 toh->name = IPV6_HOPOPTS; 15758 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15759 tcp->tcp_label_len; 15760 toh->status = 0; 15761 optptr += sizeof (*toh); 15762 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15763 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15764 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15765 ASSERT(OK_32PTR(optptr)); 15766 /* Save as last value */ 15767 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15768 (ipp->ipp_fields & IPPF_HOPOPTS), 15769 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15770 } 15771 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15772 toh = (struct T_opthdr *)optptr; 15773 toh->level = IPPROTO_IPV6; 15774 toh->name = IPV6_RTHDRDSTOPTS; 15775 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15776 toh->status = 0; 15777 optptr += sizeof (*toh); 15778 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15779 optptr += ipp->ipp_rtdstoptslen; 15780 ASSERT(OK_32PTR(optptr)); 15781 /* Save as last value */ 15782 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15783 &tcp->tcp_rtdstoptslen, 15784 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15785 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15786 } 15787 if (addflag & TCP_IPV6_RECVRTHDR) { 15788 toh = (struct T_opthdr *)optptr; 15789 toh->level = IPPROTO_IPV6; 15790 toh->name = IPV6_RTHDR; 15791 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15792 toh->status = 0; 15793 optptr += sizeof (*toh); 15794 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15795 optptr += ipp->ipp_rthdrlen; 15796 ASSERT(OK_32PTR(optptr)); 15797 /* Save as last value */ 15798 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15799 (ipp->ipp_fields & IPPF_RTHDR), 15800 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15801 } 15802 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15803 toh = (struct T_opthdr *)optptr; 15804 toh->level = IPPROTO_IPV6; 15805 toh->name = IPV6_DSTOPTS; 15806 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15807 toh->status = 0; 15808 optptr += sizeof (*toh); 15809 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15810 optptr += ipp->ipp_dstoptslen; 15811 ASSERT(OK_32PTR(optptr)); 15812 /* Save as last value */ 15813 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15814 (ipp->ipp_fields & IPPF_DSTOPTS), 15815 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15816 } 15817 ASSERT(optptr == mp->b_wptr); 15818 return (mp); 15819 } 15820 15821 /* 15822 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15823 * messages. 15824 */ 15825 void 15826 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15827 { 15828 uchar_t *rptr = mp->b_rptr; 15829 queue_t *q = tcp->tcp_rq; 15830 struct T_error_ack *tea; 15831 15832 switch (mp->b_datap->db_type) { 15833 case M_PROTO: 15834 case M_PCPROTO: 15835 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15836 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15837 break; 15838 tea = (struct T_error_ack *)rptr; 15839 ASSERT(tea->PRIM_type != T_BIND_ACK); 15840 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15841 tea->ERROR_prim != T_BIND_REQ); 15842 switch (tea->PRIM_type) { 15843 case T_ERROR_ACK: 15844 if (tcp->tcp_debug) { 15845 (void) strlog(TCP_MOD_ID, 0, 1, 15846 SL_TRACE|SL_ERROR, 15847 "tcp_rput_other: case T_ERROR_ACK, " 15848 "ERROR_prim == %d", 15849 tea->ERROR_prim); 15850 } 15851 switch (tea->ERROR_prim) { 15852 case T_SVR4_OPTMGMT_REQ: 15853 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15854 /* T_OPTMGMT_REQ generated by TCP */ 15855 printf("T_SVR4_OPTMGMT_REQ failed " 15856 "%d/%d - dropped (cnt %d)\n", 15857 tea->TLI_error, tea->UNIX_error, 15858 tcp->tcp_drop_opt_ack_cnt); 15859 freemsg(mp); 15860 tcp->tcp_drop_opt_ack_cnt--; 15861 return; 15862 } 15863 break; 15864 } 15865 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15866 tcp->tcp_drop_opt_ack_cnt > 0) { 15867 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15868 "- dropped (cnt %d)\n", 15869 tea->TLI_error, tea->UNIX_error, 15870 tcp->tcp_drop_opt_ack_cnt); 15871 freemsg(mp); 15872 tcp->tcp_drop_opt_ack_cnt--; 15873 return; 15874 } 15875 break; 15876 case T_OPTMGMT_ACK: 15877 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15878 /* T_OPTMGMT_REQ generated by TCP */ 15879 freemsg(mp); 15880 tcp->tcp_drop_opt_ack_cnt--; 15881 return; 15882 } 15883 break; 15884 default: 15885 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15886 break; 15887 } 15888 break; 15889 case M_FLUSH: 15890 if (*rptr & FLUSHR) 15891 flushq(q, FLUSHDATA); 15892 break; 15893 default: 15894 /* M_CTL will be directly sent to tcp_icmp_error() */ 15895 ASSERT(DB_TYPE(mp) != M_CTL); 15896 break; 15897 } 15898 /* 15899 * Make sure we set this bit before sending the ACK for 15900 * bind. Otherwise accept could possibly run and free 15901 * this tcp struct. 15902 */ 15903 ASSERT(q != NULL); 15904 putnext(q, mp); 15905 } 15906 15907 /* ARGSUSED */ 15908 static void 15909 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15910 { 15911 conn_t *connp = (conn_t *)arg; 15912 tcp_t *tcp = connp->conn_tcp; 15913 queue_t *q = tcp->tcp_rq; 15914 uint_t thwin; 15915 tcp_stack_t *tcps = tcp->tcp_tcps; 15916 sodirect_t *sodp; 15917 boolean_t fc; 15918 15919 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15920 tcp->tcp_rsrv_mp = mp; 15921 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15922 15923 TCP_STAT(tcps, tcp_rsrv_calls); 15924 15925 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15926 return; 15927 } 15928 15929 if (tcp->tcp_fused) { 15930 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15931 15932 ASSERT(tcp->tcp_fused); 15933 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15934 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15935 ASSERT(!TCP_IS_DETACHED(tcp)); 15936 ASSERT(tcp->tcp_connp->conn_sqp == 15937 peer_tcp->tcp_connp->conn_sqp); 15938 15939 /* 15940 * Normally we would not get backenabled in synchronous 15941 * streams mode, but in case this happens, we need to plug 15942 * synchronous streams during our drain to prevent a race 15943 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15944 */ 15945 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15946 if (tcp->tcp_rcv_list != NULL) 15947 (void) tcp_rcv_drain(tcp); 15948 15949 if (peer_tcp > tcp) { 15950 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15951 mutex_enter(&tcp->tcp_non_sq_lock); 15952 } else { 15953 mutex_enter(&tcp->tcp_non_sq_lock); 15954 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15955 } 15956 15957 if (peer_tcp->tcp_flow_stopped && 15958 (TCP_UNSENT_BYTES(peer_tcp) <= 15959 peer_tcp->tcp_xmit_lowater)) { 15960 tcp_clrqfull(peer_tcp); 15961 } 15962 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15963 mutex_exit(&tcp->tcp_non_sq_lock); 15964 15965 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15966 TCP_STAT(tcps, tcp_fusion_backenabled); 15967 return; 15968 } 15969 15970 SOD_PTR_ENTER(tcp, sodp); 15971 if (sodp != NULL) { 15972 /* An sodirect connection */ 15973 if (SOD_QFULL(sodp)) { 15974 /* Flow-controlled, need another back-enable */ 15975 fc = B_TRUE; 15976 SOD_QSETBE(sodp); 15977 } else { 15978 /* Not flow-controlled */ 15979 fc = B_FALSE; 15980 } 15981 mutex_exit(sodp->sod_lockp); 15982 } else if (canputnext(q)) { 15983 /* STREAMS, not flow-controlled */ 15984 fc = B_FALSE; 15985 } else { 15986 /* STREAMS, flow-controlled */ 15987 fc = B_TRUE; 15988 } 15989 if (!fc) { 15990 /* Not flow-controlled, open rwnd */ 15991 tcp->tcp_rwnd = q->q_hiwat; 15992 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15993 << tcp->tcp_rcv_ws; 15994 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15995 /* 15996 * Send back a window update immediately if TCP is above 15997 * ESTABLISHED state and the increase of the rcv window 15998 * that the other side knows is at least 1 MSS after flow 15999 * control is lifted. 16000 */ 16001 if (tcp->tcp_state >= TCPS_ESTABLISHED && 16002 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 16003 tcp_xmit_ctl(NULL, tcp, 16004 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 16005 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 16006 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 16007 } 16008 } 16009 } 16010 16011 /* 16012 * The read side service routine is called mostly when we get back-enabled as a 16013 * result of flow control relief. Since we don't actually queue anything in 16014 * TCP, we have no data to send out of here. What we do is clear the receive 16015 * window, and send out a window update. 16016 */ 16017 static void 16018 tcp_rsrv(queue_t *q) 16019 { 16020 conn_t *connp = Q_TO_CONN(q); 16021 tcp_t *tcp = connp->conn_tcp; 16022 mblk_t *mp; 16023 tcp_stack_t *tcps = tcp->tcp_tcps; 16024 16025 /* No code does a putq on the read side */ 16026 ASSERT(q->q_first == NULL); 16027 16028 /* Nothing to do for the default queue */ 16029 if (q == tcps->tcps_g_q) { 16030 return; 16031 } 16032 16033 /* 16034 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 16035 * been run. So just return. 16036 */ 16037 mutex_enter(&tcp->tcp_rsrv_mp_lock); 16038 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 16039 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16040 return; 16041 } 16042 tcp->tcp_rsrv_mp = NULL; 16043 mutex_exit(&tcp->tcp_rsrv_mp_lock); 16044 16045 CONN_INC_REF(connp); 16046 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 16047 SQ_PROCESS, SQTAG_TCP_RSRV); 16048 } 16049 16050 /* 16051 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 16052 * We do not allow the receive window to shrink. After setting rwnd, 16053 * set the flow control hiwat of the stream. 16054 * 16055 * This function is called in 2 cases: 16056 * 16057 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 16058 * connection (passive open) and in tcp_rput_data() for active connect. 16059 * This is called after tcp_mss_set() when the desired MSS value is known. 16060 * This makes sure that our window size is a mutiple of the other side's 16061 * MSS. 16062 * 2) Handling SO_RCVBUF option. 16063 * 16064 * It is ASSUMED that the requested size is a multiple of the current MSS. 16065 * 16066 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 16067 * user requests so. 16068 */ 16069 static int 16070 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16071 { 16072 uint32_t mss = tcp->tcp_mss; 16073 uint32_t old_max_rwnd; 16074 uint32_t max_transmittable_rwnd; 16075 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16076 tcp_stack_t *tcps = tcp->tcp_tcps; 16077 16078 if (tcp->tcp_fused) { 16079 size_t sth_hiwat; 16080 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16081 16082 ASSERT(peer_tcp != NULL); 16083 /* 16084 * Record the stream head's high water mark for 16085 * this endpoint; this is used for flow-control 16086 * purposes in tcp_fuse_output(). 16087 */ 16088 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16089 if (!tcp_detached) { 16090 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16091 sth_hiwat); 16092 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16093 conn_t *connp = tcp->tcp_connp; 16094 struct sock_proto_props sopp; 16095 16096 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16097 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16098 16099 (*connp->conn_upcalls->su_set_proto_props) 16100 (connp->conn_upper_handle, &sopp); 16101 } 16102 } 16103 16104 /* 16105 * In the fusion case, the maxpsz stream head value of 16106 * our peer is set according to its send buffer size 16107 * and our receive buffer size; since the latter may 16108 * have changed we need to update the peer's maxpsz. 16109 */ 16110 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16111 return (rwnd); 16112 } 16113 16114 if (tcp_detached) { 16115 old_max_rwnd = tcp->tcp_rwnd; 16116 } else { 16117 old_max_rwnd = tcp->tcp_recv_hiwater; 16118 } 16119 16120 /* 16121 * Insist on a receive window that is at least 16122 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16123 * funny TCP interactions of Nagle algorithm, SWS avoidance 16124 * and delayed acknowledgement. 16125 */ 16126 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16127 16128 /* 16129 * If window size info has already been exchanged, TCP should not 16130 * shrink the window. Shrinking window is doable if done carefully. 16131 * We may add that support later. But so far there is not a real 16132 * need to do that. 16133 */ 16134 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16135 /* MSS may have changed, do a round up again. */ 16136 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16137 } 16138 16139 /* 16140 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16141 * can be applied even before the window scale option is decided. 16142 */ 16143 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16144 if (rwnd > max_transmittable_rwnd) { 16145 rwnd = max_transmittable_rwnd - 16146 (max_transmittable_rwnd % mss); 16147 if (rwnd < mss) 16148 rwnd = max_transmittable_rwnd; 16149 /* 16150 * If we're over the limit we may have to back down tcp_rwnd. 16151 * The increment below won't work for us. So we set all three 16152 * here and the increment below will have no effect. 16153 */ 16154 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16155 } 16156 if (tcp->tcp_localnet) { 16157 tcp->tcp_rack_abs_max = 16158 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16159 } else { 16160 /* 16161 * For a remote host on a different subnet (through a router), 16162 * we ack every other packet to be conforming to RFC1122. 16163 * tcp_deferred_acks_max is default to 2. 16164 */ 16165 tcp->tcp_rack_abs_max = 16166 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16167 } 16168 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16169 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16170 else 16171 tcp->tcp_rack_cur_max = 0; 16172 /* 16173 * Increment the current rwnd by the amount the maximum grew (we 16174 * can not overwrite it since we might be in the middle of a 16175 * connection.) 16176 */ 16177 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16178 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16179 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16180 tcp->tcp_cwnd_max = rwnd; 16181 16182 if (tcp_detached) 16183 return (rwnd); 16184 /* 16185 * We set the maximum receive window into rq->q_hiwat if it is 16186 * a STREAMS socket. 16187 * This is not actually used for flow control. 16188 */ 16189 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16190 tcp->tcp_rq->q_hiwat = rwnd; 16191 tcp->tcp_recv_hiwater = rwnd; 16192 /* 16193 * Set the STREAM head high water mark. This doesn't have to be 16194 * here, since we are simply using default values, but we would 16195 * prefer to choose these values algorithmically, with a likely 16196 * relationship to rwnd. 16197 */ 16198 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16199 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16200 return (rwnd); 16201 } 16202 16203 /* 16204 * Return SNMP stuff in buffer in mpdata. 16205 */ 16206 mblk_t * 16207 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16208 { 16209 mblk_t *mpdata; 16210 mblk_t *mp_conn_ctl = NULL; 16211 mblk_t *mp_conn_tail; 16212 mblk_t *mp_attr_ctl = NULL; 16213 mblk_t *mp_attr_tail; 16214 mblk_t *mp6_conn_ctl = NULL; 16215 mblk_t *mp6_conn_tail; 16216 mblk_t *mp6_attr_ctl = NULL; 16217 mblk_t *mp6_attr_tail; 16218 struct opthdr *optp; 16219 mib2_tcpConnEntry_t tce; 16220 mib2_tcp6ConnEntry_t tce6; 16221 mib2_transportMLPEntry_t mlp; 16222 connf_t *connfp; 16223 int i; 16224 boolean_t ispriv; 16225 zoneid_t zoneid; 16226 int v4_conn_idx; 16227 int v6_conn_idx; 16228 conn_t *connp = Q_TO_CONN(q); 16229 tcp_stack_t *tcps; 16230 ip_stack_t *ipst; 16231 mblk_t *mp2ctl; 16232 16233 /* 16234 * make a copy of the original message 16235 */ 16236 mp2ctl = copymsg(mpctl); 16237 16238 if (mpctl == NULL || 16239 (mpdata = mpctl->b_cont) == NULL || 16240 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16241 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16242 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16243 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16244 freemsg(mp_conn_ctl); 16245 freemsg(mp_attr_ctl); 16246 freemsg(mp6_conn_ctl); 16247 freemsg(mp6_attr_ctl); 16248 freemsg(mpctl); 16249 freemsg(mp2ctl); 16250 return (NULL); 16251 } 16252 16253 ipst = connp->conn_netstack->netstack_ip; 16254 tcps = connp->conn_netstack->netstack_tcp; 16255 16256 /* build table of connections -- need count in fixed part */ 16257 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16258 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16259 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16260 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16261 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16262 16263 ispriv = 16264 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16265 zoneid = Q_TO_CONN(q)->conn_zoneid; 16266 16267 v4_conn_idx = v6_conn_idx = 0; 16268 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16269 16270 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16271 ipst = tcps->tcps_netstack->netstack_ip; 16272 16273 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16274 16275 connp = NULL; 16276 16277 while ((connp = 16278 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16279 tcp_t *tcp; 16280 boolean_t needattr; 16281 16282 if (connp->conn_zoneid != zoneid) 16283 continue; /* not in this zone */ 16284 16285 tcp = connp->conn_tcp; 16286 UPDATE_MIB(&tcps->tcps_mib, 16287 tcpHCInSegs, tcp->tcp_ibsegs); 16288 tcp->tcp_ibsegs = 0; 16289 UPDATE_MIB(&tcps->tcps_mib, 16290 tcpHCOutSegs, tcp->tcp_obsegs); 16291 tcp->tcp_obsegs = 0; 16292 16293 tce6.tcp6ConnState = tce.tcpConnState = 16294 tcp_snmp_state(tcp); 16295 if (tce.tcpConnState == MIB2_TCP_established || 16296 tce.tcpConnState == MIB2_TCP_closeWait) 16297 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16298 16299 needattr = B_FALSE; 16300 bzero(&mlp, sizeof (mlp)); 16301 if (connp->conn_mlp_type != mlptSingle) { 16302 if (connp->conn_mlp_type == mlptShared || 16303 connp->conn_mlp_type == mlptBoth) 16304 mlp.tme_flags |= MIB2_TMEF_SHARED; 16305 if (connp->conn_mlp_type == mlptPrivate || 16306 connp->conn_mlp_type == mlptBoth) 16307 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16308 needattr = B_TRUE; 16309 } 16310 if (connp->conn_peercred != NULL) { 16311 ts_label_t *tsl; 16312 16313 tsl = crgetlabel(connp->conn_peercred); 16314 mlp.tme_doi = label2doi(tsl); 16315 mlp.tme_label = *label2bslabel(tsl); 16316 needattr = B_TRUE; 16317 } 16318 16319 /* Create a message to report on IPv6 entries */ 16320 if (tcp->tcp_ipversion == IPV6_VERSION) { 16321 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16322 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16323 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16324 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16325 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16326 /* Don't want just anybody seeing these... */ 16327 if (ispriv) { 16328 tce6.tcp6ConnEntryInfo.ce_snxt = 16329 tcp->tcp_snxt; 16330 tce6.tcp6ConnEntryInfo.ce_suna = 16331 tcp->tcp_suna; 16332 tce6.tcp6ConnEntryInfo.ce_rnxt = 16333 tcp->tcp_rnxt; 16334 tce6.tcp6ConnEntryInfo.ce_rack = 16335 tcp->tcp_rack; 16336 } else { 16337 /* 16338 * Netstat, unfortunately, uses this to 16339 * get send/receive queue sizes. How to fix? 16340 * Why not compute the difference only? 16341 */ 16342 tce6.tcp6ConnEntryInfo.ce_snxt = 16343 tcp->tcp_snxt - tcp->tcp_suna; 16344 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16345 tce6.tcp6ConnEntryInfo.ce_rnxt = 16346 tcp->tcp_rnxt - tcp->tcp_rack; 16347 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16348 } 16349 16350 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16351 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16352 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16353 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16354 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16355 16356 tce6.tcp6ConnCreationProcess = 16357 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16358 tcp->tcp_cpid; 16359 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16360 16361 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16362 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16363 16364 mlp.tme_connidx = v6_conn_idx++; 16365 if (needattr) 16366 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16367 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16368 } 16369 /* 16370 * Create an IPv4 table entry for IPv4 entries and also 16371 * for IPv6 entries which are bound to in6addr_any 16372 * but don't have IPV6_V6ONLY set. 16373 * (i.e. anything an IPv4 peer could connect to) 16374 */ 16375 if (tcp->tcp_ipversion == IPV4_VERSION || 16376 (tcp->tcp_state <= TCPS_LISTEN && 16377 !tcp->tcp_connp->conn_ipv6_v6only && 16378 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16379 if (tcp->tcp_ipversion == IPV6_VERSION) { 16380 tce.tcpConnRemAddress = INADDR_ANY; 16381 tce.tcpConnLocalAddress = INADDR_ANY; 16382 } else { 16383 tce.tcpConnRemAddress = 16384 tcp->tcp_remote; 16385 tce.tcpConnLocalAddress = 16386 tcp->tcp_ip_src; 16387 } 16388 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16389 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16390 /* Don't want just anybody seeing these... */ 16391 if (ispriv) { 16392 tce.tcpConnEntryInfo.ce_snxt = 16393 tcp->tcp_snxt; 16394 tce.tcpConnEntryInfo.ce_suna = 16395 tcp->tcp_suna; 16396 tce.tcpConnEntryInfo.ce_rnxt = 16397 tcp->tcp_rnxt; 16398 tce.tcpConnEntryInfo.ce_rack = 16399 tcp->tcp_rack; 16400 } else { 16401 /* 16402 * Netstat, unfortunately, uses this to 16403 * get send/receive queue sizes. How 16404 * to fix? 16405 * Why not compute the difference only? 16406 */ 16407 tce.tcpConnEntryInfo.ce_snxt = 16408 tcp->tcp_snxt - tcp->tcp_suna; 16409 tce.tcpConnEntryInfo.ce_suna = 0; 16410 tce.tcpConnEntryInfo.ce_rnxt = 16411 tcp->tcp_rnxt - tcp->tcp_rack; 16412 tce.tcpConnEntryInfo.ce_rack = 0; 16413 } 16414 16415 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16416 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16417 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16418 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16419 tce.tcpConnEntryInfo.ce_state = 16420 tcp->tcp_state; 16421 16422 tce.tcpConnCreationProcess = 16423 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16424 tcp->tcp_cpid; 16425 tce.tcpConnCreationTime = tcp->tcp_open_time; 16426 16427 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16428 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16429 16430 mlp.tme_connidx = v4_conn_idx++; 16431 if (needattr) 16432 (void) snmp_append_data2( 16433 mp_attr_ctl->b_cont, 16434 &mp_attr_tail, (char *)&mlp, 16435 sizeof (mlp)); 16436 } 16437 } 16438 } 16439 16440 /* fixed length structure for IPv4 and IPv6 counters */ 16441 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16442 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16443 sizeof (mib2_tcp6ConnEntry_t)); 16444 /* synchronize 32- and 64-bit counters */ 16445 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16446 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16447 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16448 optp->level = MIB2_TCP; 16449 optp->name = 0; 16450 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16451 sizeof (tcps->tcps_mib)); 16452 optp->len = msgdsize(mpdata); 16453 qreply(q, mpctl); 16454 16455 /* table of connections... */ 16456 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16457 sizeof (struct T_optmgmt_ack)]; 16458 optp->level = MIB2_TCP; 16459 optp->name = MIB2_TCP_CONN; 16460 optp->len = msgdsize(mp_conn_ctl->b_cont); 16461 qreply(q, mp_conn_ctl); 16462 16463 /* table of MLP attributes... */ 16464 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16465 sizeof (struct T_optmgmt_ack)]; 16466 optp->level = MIB2_TCP; 16467 optp->name = EXPER_XPORT_MLP; 16468 optp->len = msgdsize(mp_attr_ctl->b_cont); 16469 if (optp->len == 0) 16470 freemsg(mp_attr_ctl); 16471 else 16472 qreply(q, mp_attr_ctl); 16473 16474 /* table of IPv6 connections... */ 16475 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16476 sizeof (struct T_optmgmt_ack)]; 16477 optp->level = MIB2_TCP6; 16478 optp->name = MIB2_TCP6_CONN; 16479 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16480 qreply(q, mp6_conn_ctl); 16481 16482 /* table of IPv6 MLP attributes... */ 16483 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16484 sizeof (struct T_optmgmt_ack)]; 16485 optp->level = MIB2_TCP6; 16486 optp->name = EXPER_XPORT_MLP; 16487 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16488 if (optp->len == 0) 16489 freemsg(mp6_attr_ctl); 16490 else 16491 qreply(q, mp6_attr_ctl); 16492 return (mp2ctl); 16493 } 16494 16495 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16496 /* ARGSUSED */ 16497 int 16498 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16499 { 16500 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16501 16502 switch (level) { 16503 case MIB2_TCP: 16504 switch (name) { 16505 case 13: 16506 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16507 return (0); 16508 /* TODO: delete entry defined by tce */ 16509 return (1); 16510 default: 16511 return (0); 16512 } 16513 default: 16514 return (1); 16515 } 16516 } 16517 16518 /* Translate TCP state to MIB2 TCP state. */ 16519 static int 16520 tcp_snmp_state(tcp_t *tcp) 16521 { 16522 if (tcp == NULL) 16523 return (0); 16524 16525 switch (tcp->tcp_state) { 16526 case TCPS_CLOSED: 16527 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16528 case TCPS_BOUND: 16529 return (MIB2_TCP_closed); 16530 case TCPS_LISTEN: 16531 return (MIB2_TCP_listen); 16532 case TCPS_SYN_SENT: 16533 return (MIB2_TCP_synSent); 16534 case TCPS_SYN_RCVD: 16535 return (MIB2_TCP_synReceived); 16536 case TCPS_ESTABLISHED: 16537 return (MIB2_TCP_established); 16538 case TCPS_CLOSE_WAIT: 16539 return (MIB2_TCP_closeWait); 16540 case TCPS_FIN_WAIT_1: 16541 return (MIB2_TCP_finWait1); 16542 case TCPS_CLOSING: 16543 return (MIB2_TCP_closing); 16544 case TCPS_LAST_ACK: 16545 return (MIB2_TCP_lastAck); 16546 case TCPS_FIN_WAIT_2: 16547 return (MIB2_TCP_finWait2); 16548 case TCPS_TIME_WAIT: 16549 return (MIB2_TCP_timeWait); 16550 default: 16551 return (0); 16552 } 16553 } 16554 16555 static char tcp_report_header[] = 16556 "TCP " MI_COL_HDRPAD_STR 16557 "zone dest snxt suna " 16558 "swnd rnxt rack rwnd rto mss w sw rw t " 16559 "recent [lport,fport] state"; 16560 16561 /* 16562 * TCP status report triggered via the Named Dispatch mechanism. 16563 */ 16564 /* ARGSUSED */ 16565 static void 16566 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16567 cred_t *cr) 16568 { 16569 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16570 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16571 char cflag; 16572 in6_addr_t v6dst; 16573 char buf[80]; 16574 uint_t print_len, buf_len; 16575 16576 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16577 if (buf_len <= 0) 16578 return; 16579 16580 if (hashval >= 0) 16581 (void) sprintf(hash, "%03d ", hashval); 16582 else 16583 hash[0] = '\0'; 16584 16585 /* 16586 * Note that we use the remote address in the tcp_b structure. 16587 * This means that it will print out the real destination address, 16588 * not the next hop's address if source routing is used. This 16589 * avoid the confusion on the output because user may not 16590 * know that source routing is used for a connection. 16591 */ 16592 if (tcp->tcp_ipversion == IPV4_VERSION) { 16593 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16594 } else { 16595 v6dst = tcp->tcp_remote_v6; 16596 } 16597 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16598 /* 16599 * the ispriv checks are so that normal users cannot determine 16600 * sequence number information using NDD. 16601 */ 16602 16603 if (TCP_IS_DETACHED(tcp)) 16604 cflag = '*'; 16605 else 16606 cflag = ' '; 16607 print_len = snprintf((char *)mp->b_wptr, buf_len, 16608 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16609 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16610 hash, 16611 (void *)tcp, 16612 tcp->tcp_connp->conn_zoneid, 16613 addrbuf, 16614 (ispriv) ? tcp->tcp_snxt : 0, 16615 (ispriv) ? tcp->tcp_suna : 0, 16616 tcp->tcp_swnd, 16617 (ispriv) ? tcp->tcp_rnxt : 0, 16618 (ispriv) ? tcp->tcp_rack : 0, 16619 tcp->tcp_rwnd, 16620 tcp->tcp_rto, 16621 tcp->tcp_mss, 16622 tcp->tcp_snd_ws_ok, 16623 tcp->tcp_snd_ws, 16624 tcp->tcp_rcv_ws, 16625 tcp->tcp_snd_ts_ok, 16626 tcp->tcp_ts_recent, 16627 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16628 if (print_len < buf_len) { 16629 ((mblk_t *)mp)->b_wptr += print_len; 16630 } else { 16631 ((mblk_t *)mp)->b_wptr += buf_len; 16632 } 16633 } 16634 16635 /* 16636 * TCP status report (for listeners only) triggered via the Named Dispatch 16637 * mechanism. 16638 */ 16639 /* ARGSUSED */ 16640 static void 16641 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16642 { 16643 char addrbuf[INET6_ADDRSTRLEN]; 16644 in6_addr_t v6dst; 16645 uint_t print_len, buf_len; 16646 16647 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16648 if (buf_len <= 0) 16649 return; 16650 16651 if (tcp->tcp_ipversion == IPV4_VERSION) { 16652 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16653 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16654 } else { 16655 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16656 addrbuf, sizeof (addrbuf)); 16657 } 16658 print_len = snprintf((char *)mp->b_wptr, buf_len, 16659 "%03d " 16660 MI_COL_PTRFMT_STR 16661 "%d %s %05u %08u %d/%d/%d%c\n", 16662 hashval, (void *)tcp, 16663 tcp->tcp_connp->conn_zoneid, 16664 addrbuf, 16665 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16666 tcp->tcp_conn_req_seqnum, 16667 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16668 tcp->tcp_conn_req_max, 16669 tcp->tcp_syn_defense ? '*' : ' '); 16670 if (print_len < buf_len) { 16671 ((mblk_t *)mp)->b_wptr += print_len; 16672 } else { 16673 ((mblk_t *)mp)->b_wptr += buf_len; 16674 } 16675 } 16676 16677 /* TCP status report triggered via the Named Dispatch mechanism. */ 16678 /* ARGSUSED */ 16679 static int 16680 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16681 { 16682 tcp_t *tcp; 16683 int i; 16684 conn_t *connp; 16685 connf_t *connfp; 16686 zoneid_t zoneid; 16687 tcp_stack_t *tcps; 16688 ip_stack_t *ipst; 16689 16690 zoneid = Q_TO_CONN(q)->conn_zoneid; 16691 tcps = Q_TO_TCP(q)->tcp_tcps; 16692 16693 /* 16694 * Because of the ndd constraint, at most we can have 64K buffer 16695 * to put in all TCP info. So to be more efficient, just 16696 * allocate a 64K buffer here, assuming we need that large buffer. 16697 * This may be a problem as any user can read tcp_status. Therefore 16698 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16699 * This should be OK as normal users should not do this too often. 16700 */ 16701 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16702 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16703 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16704 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16705 return (0); 16706 } 16707 } 16708 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16709 /* The following may work even if we cannot get a large buf. */ 16710 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16711 return (0); 16712 } 16713 16714 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16715 16716 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16717 16718 ipst = tcps->tcps_netstack->netstack_ip; 16719 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16720 16721 connp = NULL; 16722 16723 while ((connp = 16724 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16725 tcp = connp->conn_tcp; 16726 if (zoneid != GLOBAL_ZONEID && 16727 zoneid != connp->conn_zoneid) 16728 continue; 16729 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16730 cr); 16731 } 16732 16733 } 16734 16735 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16736 return (0); 16737 } 16738 16739 /* TCP status report triggered via the Named Dispatch mechanism. */ 16740 /* ARGSUSED */ 16741 static int 16742 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16743 { 16744 tf_t *tbf; 16745 tcp_t *tcp, *ltcp; 16746 int i; 16747 zoneid_t zoneid; 16748 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16749 16750 zoneid = Q_TO_CONN(q)->conn_zoneid; 16751 16752 /* Refer to comments in tcp_status_report(). */ 16753 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16754 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16755 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16756 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16757 return (0); 16758 } 16759 } 16760 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16761 /* The following may work even if we cannot get a large buf. */ 16762 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16763 return (0); 16764 } 16765 16766 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16767 16768 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16769 tbf = &tcps->tcps_bind_fanout[i]; 16770 mutex_enter(&tbf->tf_lock); 16771 for (ltcp = tbf->tf_tcp; ltcp != NULL; 16772 ltcp = ltcp->tcp_bind_hash) { 16773 for (tcp = ltcp; tcp != NULL; 16774 tcp = tcp->tcp_bind_hash_port) { 16775 if (zoneid != GLOBAL_ZONEID && 16776 zoneid != tcp->tcp_connp->conn_zoneid) 16777 continue; 16778 CONN_INC_REF(tcp->tcp_connp); 16779 tcp_report_item(mp->b_cont, tcp, i, 16780 Q_TO_TCP(q), cr); 16781 CONN_DEC_REF(tcp->tcp_connp); 16782 } 16783 } 16784 mutex_exit(&tbf->tf_lock); 16785 } 16786 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16787 return (0); 16788 } 16789 16790 /* TCP status report triggered via the Named Dispatch mechanism. */ 16791 /* ARGSUSED */ 16792 static int 16793 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16794 { 16795 connf_t *connfp; 16796 conn_t *connp; 16797 tcp_t *tcp; 16798 int i; 16799 zoneid_t zoneid; 16800 tcp_stack_t *tcps; 16801 ip_stack_t *ipst; 16802 16803 zoneid = Q_TO_CONN(q)->conn_zoneid; 16804 tcps = Q_TO_TCP(q)->tcp_tcps; 16805 16806 /* Refer to comments in tcp_status_report(). */ 16807 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16808 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16809 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16810 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16811 return (0); 16812 } 16813 } 16814 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16815 /* The following may work even if we cannot get a large buf. */ 16816 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16817 return (0); 16818 } 16819 16820 (void) mi_mpprintf(mp, 16821 " TCP " MI_COL_HDRPAD_STR 16822 "zone IP addr port seqnum backlog (q0/q/max)"); 16823 16824 ipst = tcps->tcps_netstack->netstack_ip; 16825 16826 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16827 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16828 connp = NULL; 16829 while ((connp = 16830 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16831 tcp = connp->conn_tcp; 16832 if (zoneid != GLOBAL_ZONEID && 16833 zoneid != connp->conn_zoneid) 16834 continue; 16835 tcp_report_listener(mp->b_cont, tcp, i); 16836 } 16837 } 16838 16839 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16840 return (0); 16841 } 16842 16843 /* TCP status report triggered via the Named Dispatch mechanism. */ 16844 /* ARGSUSED */ 16845 static int 16846 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16847 { 16848 connf_t *connfp; 16849 conn_t *connp; 16850 tcp_t *tcp; 16851 int i; 16852 zoneid_t zoneid; 16853 tcp_stack_t *tcps; 16854 ip_stack_t *ipst; 16855 16856 zoneid = Q_TO_CONN(q)->conn_zoneid; 16857 tcps = Q_TO_TCP(q)->tcp_tcps; 16858 ipst = tcps->tcps_netstack->netstack_ip; 16859 16860 /* Refer to comments in tcp_status_report(). */ 16861 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16862 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16863 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16864 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16865 return (0); 16866 } 16867 } 16868 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16869 /* The following may work even if we cannot get a large buf. */ 16870 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16871 return (0); 16872 } 16873 16874 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16875 ipst->ips_ipcl_conn_fanout_size); 16876 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16877 16878 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16879 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16880 connp = NULL; 16881 while ((connp = 16882 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16883 tcp = connp->conn_tcp; 16884 if (zoneid != GLOBAL_ZONEID && 16885 zoneid != connp->conn_zoneid) 16886 continue; 16887 tcp_report_item(mp->b_cont, tcp, i, 16888 Q_TO_TCP(q), cr); 16889 } 16890 } 16891 16892 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16893 return (0); 16894 } 16895 16896 /* TCP status report triggered via the Named Dispatch mechanism. */ 16897 /* ARGSUSED */ 16898 static int 16899 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16900 { 16901 tf_t *tf; 16902 tcp_t *tcp; 16903 int i; 16904 zoneid_t zoneid; 16905 tcp_stack_t *tcps; 16906 16907 zoneid = Q_TO_CONN(q)->conn_zoneid; 16908 tcps = Q_TO_TCP(q)->tcp_tcps; 16909 16910 /* Refer to comments in tcp_status_report(). */ 16911 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16912 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16913 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16914 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16915 return (0); 16916 } 16917 } 16918 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16919 /* The following may work even if we cannot get a large buf. */ 16920 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16921 return (0); 16922 } 16923 16924 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16925 16926 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16927 tf = &tcps->tcps_acceptor_fanout[i]; 16928 mutex_enter(&tf->tf_lock); 16929 for (tcp = tf->tf_tcp; tcp != NULL; 16930 tcp = tcp->tcp_acceptor_hash) { 16931 if (zoneid != GLOBAL_ZONEID && 16932 zoneid != tcp->tcp_connp->conn_zoneid) 16933 continue; 16934 tcp_report_item(mp->b_cont, tcp, i, 16935 Q_TO_TCP(q), cr); 16936 } 16937 mutex_exit(&tf->tf_lock); 16938 } 16939 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16940 return (0); 16941 } 16942 16943 /* 16944 * tcp_timer is the timer service routine. It handles the retransmission, 16945 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16946 * from the state of the tcp instance what kind of action needs to be done 16947 * at the time it is called. 16948 */ 16949 static void 16950 tcp_timer(void *arg) 16951 { 16952 mblk_t *mp; 16953 clock_t first_threshold; 16954 clock_t second_threshold; 16955 clock_t ms; 16956 uint32_t mss; 16957 conn_t *connp = (conn_t *)arg; 16958 tcp_t *tcp = connp->conn_tcp; 16959 tcp_stack_t *tcps = tcp->tcp_tcps; 16960 16961 tcp->tcp_timer_tid = 0; 16962 16963 if (tcp->tcp_fused) 16964 return; 16965 16966 first_threshold = tcp->tcp_first_timer_threshold; 16967 second_threshold = tcp->tcp_second_timer_threshold; 16968 switch (tcp->tcp_state) { 16969 case TCPS_IDLE: 16970 case TCPS_BOUND: 16971 case TCPS_LISTEN: 16972 return; 16973 case TCPS_SYN_RCVD: { 16974 tcp_t *listener = tcp->tcp_listener; 16975 16976 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16977 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16978 /* it's our first timeout */ 16979 tcp->tcp_syn_rcvd_timeout = 1; 16980 mutex_enter(&listener->tcp_eager_lock); 16981 listener->tcp_syn_rcvd_timeout++; 16982 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16983 /* 16984 * Make this eager available for drop if we 16985 * need to drop one to accomodate a new 16986 * incoming SYN request. 16987 */ 16988 MAKE_DROPPABLE(listener, tcp); 16989 } 16990 if (!listener->tcp_syn_defense && 16991 (listener->tcp_syn_rcvd_timeout > 16992 (tcps->tcps_conn_req_max_q0 >> 2)) && 16993 (tcps->tcps_conn_req_max_q0 > 200)) { 16994 /* We may be under attack. Put on a defense. */ 16995 listener->tcp_syn_defense = B_TRUE; 16996 cmn_err(CE_WARN, "High TCP connect timeout " 16997 "rate! System (port %d) may be under a " 16998 "SYN flood attack!", 16999 BE16_TO_U16(listener->tcp_tcph->th_lport)); 17000 17001 listener->tcp_ip_addr_cache = kmem_zalloc( 17002 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 17003 KM_NOSLEEP); 17004 } 17005 mutex_exit(&listener->tcp_eager_lock); 17006 } else if (listener != NULL) { 17007 mutex_enter(&listener->tcp_eager_lock); 17008 tcp->tcp_syn_rcvd_timeout++; 17009 if (tcp->tcp_syn_rcvd_timeout > 1 && 17010 !tcp->tcp_closemp_used) { 17011 /* 17012 * This is our second timeout. Put the tcp in 17013 * the list of droppable eagers to allow it to 17014 * be dropped, if needed. We don't check 17015 * whether tcp_dontdrop is set or not to 17016 * protect ourselve from a SYN attack where a 17017 * remote host can spoof itself as one of the 17018 * good IP source and continue to hold 17019 * resources too long. 17020 */ 17021 MAKE_DROPPABLE(listener, tcp); 17022 } 17023 mutex_exit(&listener->tcp_eager_lock); 17024 } 17025 } 17026 /* FALLTHRU */ 17027 case TCPS_SYN_SENT: 17028 first_threshold = tcp->tcp_first_ctimer_threshold; 17029 second_threshold = tcp->tcp_second_ctimer_threshold; 17030 break; 17031 case TCPS_ESTABLISHED: 17032 case TCPS_FIN_WAIT_1: 17033 case TCPS_CLOSING: 17034 case TCPS_CLOSE_WAIT: 17035 case TCPS_LAST_ACK: 17036 /* If we have data to rexmit */ 17037 if (tcp->tcp_suna != tcp->tcp_snxt) { 17038 clock_t time_to_wait; 17039 17040 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 17041 if (!tcp->tcp_xmit_head) 17042 break; 17043 time_to_wait = lbolt - 17044 (clock_t)tcp->tcp_xmit_head->b_prev; 17045 time_to_wait = tcp->tcp_rto - 17046 TICK_TO_MSEC(time_to_wait); 17047 /* 17048 * If the timer fires too early, 1 clock tick earlier, 17049 * restart the timer. 17050 */ 17051 if (time_to_wait > msec_per_tick) { 17052 TCP_STAT(tcps, tcp_timer_fire_early); 17053 TCP_TIMER_RESTART(tcp, time_to_wait); 17054 return; 17055 } 17056 /* 17057 * When we probe zero windows, we force the swnd open. 17058 * If our peer acks with a closed window swnd will be 17059 * set to zero by tcp_rput(). As long as we are 17060 * receiving acks tcp_rput will 17061 * reset 'tcp_ms_we_have_waited' so as not to trip the 17062 * first and second interval actions. NOTE: the timer 17063 * interval is allowed to continue its exponential 17064 * backoff. 17065 */ 17066 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 17067 if (tcp->tcp_debug) { 17068 (void) strlog(TCP_MOD_ID, 0, 1, 17069 SL_TRACE, "tcp_timer: zero win"); 17070 } 17071 } else { 17072 /* 17073 * After retransmission, we need to do 17074 * slow start. Set the ssthresh to one 17075 * half of current effective window and 17076 * cwnd to one MSS. Also reset 17077 * tcp_cwnd_cnt. 17078 * 17079 * Note that if tcp_ssthresh is reduced because 17080 * of ECN, do not reduce it again unless it is 17081 * already one window of data away (tcp_cwr 17082 * should then be cleared) or this is a 17083 * timeout for a retransmitted segment. 17084 */ 17085 uint32_t npkt; 17086 17087 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17088 npkt = ((tcp->tcp_timer_backoff ? 17089 tcp->tcp_cwnd_ssthresh : 17090 tcp->tcp_snxt - 17091 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17092 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17093 tcp->tcp_mss; 17094 } 17095 tcp->tcp_cwnd = tcp->tcp_mss; 17096 tcp->tcp_cwnd_cnt = 0; 17097 if (tcp->tcp_ecn_ok) { 17098 tcp->tcp_cwr = B_TRUE; 17099 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17100 tcp->tcp_ecn_cwr_sent = B_FALSE; 17101 } 17102 } 17103 break; 17104 } 17105 /* 17106 * We have something to send yet we cannot send. The 17107 * reason can be: 17108 * 17109 * 1. Zero send window: we need to do zero window probe. 17110 * 2. Zero cwnd: because of ECN, we need to "clock out 17111 * segments. 17112 * 3. SWS avoidance: receiver may have shrunk window, 17113 * reset our knowledge. 17114 * 17115 * Note that condition 2 can happen with either 1 or 17116 * 3. But 1 and 3 are exclusive. 17117 */ 17118 if (tcp->tcp_unsent != 0) { 17119 if (tcp->tcp_cwnd == 0) { 17120 /* 17121 * Set tcp_cwnd to 1 MSS so that a 17122 * new segment can be sent out. We 17123 * are "clocking out" new data when 17124 * the network is really congested. 17125 */ 17126 ASSERT(tcp->tcp_ecn_ok); 17127 tcp->tcp_cwnd = tcp->tcp_mss; 17128 } 17129 if (tcp->tcp_swnd == 0) { 17130 /* Extend window for zero window probe */ 17131 tcp->tcp_swnd++; 17132 tcp->tcp_zero_win_probe = B_TRUE; 17133 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17134 } else { 17135 /* 17136 * Handle timeout from sender SWS avoidance. 17137 * Reset our knowledge of the max send window 17138 * since the receiver might have reduced its 17139 * receive buffer. Avoid setting tcp_max_swnd 17140 * to one since that will essentially disable 17141 * the SWS checks. 17142 * 17143 * Note that since we don't have a SWS 17144 * state variable, if the timeout is set 17145 * for ECN but not for SWS, this 17146 * code will also be executed. This is 17147 * fine as tcp_max_swnd is updated 17148 * constantly and it will not affect 17149 * anything. 17150 */ 17151 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17152 } 17153 tcp_wput_data(tcp, NULL, B_FALSE); 17154 return; 17155 } 17156 /* Is there a FIN that needs to be to re retransmitted? */ 17157 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17158 !tcp->tcp_fin_acked) 17159 break; 17160 /* Nothing to do, return without restarting timer. */ 17161 TCP_STAT(tcps, tcp_timer_fire_miss); 17162 return; 17163 case TCPS_FIN_WAIT_2: 17164 /* 17165 * User closed the TCP endpoint and peer ACK'ed our FIN. 17166 * We waited some time for for peer's FIN, but it hasn't 17167 * arrived. We flush the connection now to avoid 17168 * case where the peer has rebooted. 17169 */ 17170 if (TCP_IS_DETACHED(tcp)) { 17171 (void) tcp_clean_death(tcp, 0, 23); 17172 } else { 17173 TCP_TIMER_RESTART(tcp, 17174 tcps->tcps_fin_wait_2_flush_interval); 17175 } 17176 return; 17177 case TCPS_TIME_WAIT: 17178 (void) tcp_clean_death(tcp, 0, 24); 17179 return; 17180 default: 17181 if (tcp->tcp_debug) { 17182 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17183 "tcp_timer: strange state (%d) %s", 17184 tcp->tcp_state, tcp_display(tcp, NULL, 17185 DISP_PORT_ONLY)); 17186 } 17187 return; 17188 } 17189 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17190 /* 17191 * For zero window probe, we need to send indefinitely, 17192 * unless we have not heard from the other side for some 17193 * time... 17194 */ 17195 if ((tcp->tcp_zero_win_probe == 0) || 17196 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17197 second_threshold)) { 17198 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17199 /* 17200 * If TCP is in SYN_RCVD state, send back a 17201 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17202 * should be zero in TCPS_SYN_RCVD state. 17203 */ 17204 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17205 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17206 "in SYN_RCVD", 17207 tcp, tcp->tcp_snxt, 17208 tcp->tcp_rnxt, TH_RST | TH_ACK); 17209 } 17210 (void) tcp_clean_death(tcp, 17211 tcp->tcp_client_errno ? 17212 tcp->tcp_client_errno : ETIMEDOUT, 25); 17213 return; 17214 } else { 17215 /* 17216 * Set tcp_ms_we_have_waited to second_threshold 17217 * so that in next timeout, we will do the above 17218 * check (lbolt - tcp_last_recv_time). This is 17219 * also to avoid overflow. 17220 * 17221 * We don't need to decrement tcp_timer_backoff 17222 * to avoid overflow because it will be decremented 17223 * later if new timeout value is greater than 17224 * tcp_rexmit_interval_max. In the case when 17225 * tcp_rexmit_interval_max is greater than 17226 * second_threshold, it means that we will wait 17227 * longer than second_threshold to send the next 17228 * window probe. 17229 */ 17230 tcp->tcp_ms_we_have_waited = second_threshold; 17231 } 17232 } else if (ms > first_threshold) { 17233 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17234 tcp->tcp_xmit_head != NULL) { 17235 tcp->tcp_xmit_head = 17236 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17237 } 17238 /* 17239 * We have been retransmitting for too long... The RTT 17240 * we calculated is probably incorrect. Reinitialize it. 17241 * Need to compensate for 0 tcp_rtt_sa. Reset 17242 * tcp_rtt_update so that we won't accidentally cache a 17243 * bad value. But only do this if this is not a zero 17244 * window probe. 17245 */ 17246 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17247 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17248 (tcp->tcp_rtt_sa >> 5); 17249 tcp->tcp_rtt_sa = 0; 17250 tcp_ip_notify(tcp); 17251 tcp->tcp_rtt_update = 0; 17252 } 17253 } 17254 tcp->tcp_timer_backoff++; 17255 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17256 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17257 tcps->tcps_rexmit_interval_min) { 17258 /* 17259 * This means the original RTO is tcp_rexmit_interval_min. 17260 * So we will use tcp_rexmit_interval_min as the RTO value 17261 * and do the backoff. 17262 */ 17263 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17264 } else { 17265 ms <<= tcp->tcp_timer_backoff; 17266 } 17267 if (ms > tcps->tcps_rexmit_interval_max) { 17268 ms = tcps->tcps_rexmit_interval_max; 17269 /* 17270 * ms is at max, decrement tcp_timer_backoff to avoid 17271 * overflow. 17272 */ 17273 tcp->tcp_timer_backoff--; 17274 } 17275 tcp->tcp_ms_we_have_waited += ms; 17276 if (tcp->tcp_zero_win_probe == 0) { 17277 tcp->tcp_rto = ms; 17278 } 17279 TCP_TIMER_RESTART(tcp, ms); 17280 /* 17281 * This is after a timeout and tcp_rto is backed off. Set 17282 * tcp_set_timer to 1 so that next time RTO is updated, we will 17283 * restart the timer with a correct value. 17284 */ 17285 tcp->tcp_set_timer = 1; 17286 mss = tcp->tcp_snxt - tcp->tcp_suna; 17287 if (mss > tcp->tcp_mss) 17288 mss = tcp->tcp_mss; 17289 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17290 mss = tcp->tcp_swnd; 17291 17292 if ((mp = tcp->tcp_xmit_head) != NULL) 17293 mp->b_prev = (mblk_t *)lbolt; 17294 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17295 B_TRUE); 17296 17297 /* 17298 * When slow start after retransmission begins, start with 17299 * this seq no. tcp_rexmit_max marks the end of special slow 17300 * start phase. tcp_snd_burst controls how many segments 17301 * can be sent because of an ack. 17302 */ 17303 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17304 tcp->tcp_snd_burst = TCP_CWND_SS; 17305 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17306 (tcp->tcp_unsent == 0)) { 17307 tcp->tcp_rexmit_max = tcp->tcp_fss; 17308 } else { 17309 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17310 } 17311 tcp->tcp_rexmit = B_TRUE; 17312 tcp->tcp_dupack_cnt = 0; 17313 17314 /* 17315 * Remove all rexmit SACK blk to start from fresh. 17316 */ 17317 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17318 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17319 tcp->tcp_num_notsack_blk = 0; 17320 tcp->tcp_cnt_notsack_list = 0; 17321 } 17322 if (mp == NULL) { 17323 return; 17324 } 17325 /* 17326 * Attach credentials to retransmitted initial SYNs. 17327 * In theory we should use the credentials from the connect() 17328 * call to ensure that getpeerucred() on the peer will be correct. 17329 * But we assume that SYN's are not dropped for loopback connections. 17330 */ 17331 if (tcp->tcp_state == TCPS_SYN_SENT) { 17332 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 17333 } 17334 17335 tcp->tcp_csuna = tcp->tcp_snxt; 17336 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17337 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17338 tcp_send_data(tcp, tcp->tcp_wq, mp); 17339 17340 } 17341 17342 static int 17343 tcp_do_unbind(conn_t *connp) 17344 { 17345 tcp_t *tcp = connp->conn_tcp; 17346 int error = 0; 17347 17348 switch (tcp->tcp_state) { 17349 case TCPS_BOUND: 17350 case TCPS_LISTEN: 17351 break; 17352 default: 17353 return (-TOUTSTATE); 17354 } 17355 17356 /* 17357 * Need to clean up all the eagers since after the unbind, segments 17358 * will no longer be delivered to this listener stream. 17359 */ 17360 mutex_enter(&tcp->tcp_eager_lock); 17361 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17362 tcp_eager_cleanup(tcp, 0); 17363 } 17364 mutex_exit(&tcp->tcp_eager_lock); 17365 17366 if (tcp->tcp_ipversion == IPV4_VERSION) { 17367 tcp->tcp_ipha->ipha_src = 0; 17368 } else { 17369 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17370 } 17371 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17372 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17373 tcp_bind_hash_remove(tcp); 17374 tcp->tcp_state = TCPS_IDLE; 17375 tcp->tcp_mdt = B_FALSE; 17376 17377 connp = tcp->tcp_connp; 17378 connp->conn_mdt_ok = B_FALSE; 17379 ipcl_hash_remove(connp); 17380 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17381 17382 return (error); 17383 } 17384 17385 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17386 static void 17387 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 17388 { 17389 int error = tcp_do_unbind(tcp->tcp_connp); 17390 17391 if (error > 0) { 17392 tcp_err_ack(tcp, mp, TSYSERR, error); 17393 } else if (error < 0) { 17394 tcp_err_ack(tcp, mp, -error, 0); 17395 } else { 17396 /* Send M_FLUSH according to TPI */ 17397 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17398 17399 mp = mi_tpi_ok_ack_alloc(mp); 17400 putnext(tcp->tcp_rq, mp); 17401 } 17402 } 17403 17404 /* 17405 * Don't let port fall into the privileged range. 17406 * Since the extra privileged ports can be arbitrary we also 17407 * ensure that we exclude those from consideration. 17408 * tcp_g_epriv_ports is not sorted thus we loop over it until 17409 * there are no changes. 17410 * 17411 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17412 * but instead the code relies on: 17413 * - the fact that the address of the array and its size never changes 17414 * - the atomic assignment of the elements of the array 17415 * 17416 * Returns 0 if there are no more ports available. 17417 * 17418 * TS note: skip multilevel ports. 17419 */ 17420 static in_port_t 17421 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17422 { 17423 int i; 17424 boolean_t restart = B_FALSE; 17425 tcp_stack_t *tcps = tcp->tcp_tcps; 17426 17427 if (random && tcp_random_anon_port != 0) { 17428 (void) random_get_pseudo_bytes((uint8_t *)&port, 17429 sizeof (in_port_t)); 17430 /* 17431 * Unless changed by a sys admin, the smallest anon port 17432 * is 32768 and the largest anon port is 65535. It is 17433 * very likely (50%) for the random port to be smaller 17434 * than the smallest anon port. When that happens, 17435 * add port % (anon port range) to the smallest anon 17436 * port to get the random port. It should fall into the 17437 * valid anon port range. 17438 */ 17439 if (port < tcps->tcps_smallest_anon_port) { 17440 port = tcps->tcps_smallest_anon_port + 17441 port % (tcps->tcps_largest_anon_port - 17442 tcps->tcps_smallest_anon_port); 17443 } 17444 } 17445 17446 retry: 17447 if (port < tcps->tcps_smallest_anon_port) 17448 port = (in_port_t)tcps->tcps_smallest_anon_port; 17449 17450 if (port > tcps->tcps_largest_anon_port) { 17451 if (restart) 17452 return (0); 17453 restart = B_TRUE; 17454 port = (in_port_t)tcps->tcps_smallest_anon_port; 17455 } 17456 17457 if (port < tcps->tcps_smallest_nonpriv_port) 17458 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17459 17460 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17461 if (port == tcps->tcps_g_epriv_ports[i]) { 17462 port++; 17463 /* 17464 * Make sure whether the port is in the 17465 * valid range. 17466 */ 17467 goto retry; 17468 } 17469 } 17470 if (is_system_labeled() && 17471 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17472 IPPROTO_TCP, B_TRUE)) != 0) { 17473 port = i; 17474 goto retry; 17475 } 17476 return (port); 17477 } 17478 17479 /* 17480 * Return the next anonymous port in the privileged port range for 17481 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17482 * downwards. This is the same behavior as documented in the userland 17483 * library call rresvport(3N). 17484 * 17485 * TS note: skip multilevel ports. 17486 */ 17487 static in_port_t 17488 tcp_get_next_priv_port(const tcp_t *tcp) 17489 { 17490 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17491 in_port_t nextport; 17492 boolean_t restart = B_FALSE; 17493 tcp_stack_t *tcps = tcp->tcp_tcps; 17494 retry: 17495 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17496 next_priv_port >= IPPORT_RESERVED) { 17497 next_priv_port = IPPORT_RESERVED - 1; 17498 if (restart) 17499 return (0); 17500 restart = B_TRUE; 17501 } 17502 if (is_system_labeled() && 17503 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17504 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17505 next_priv_port = nextport; 17506 goto retry; 17507 } 17508 return (next_priv_port--); 17509 } 17510 17511 /* The write side r/w procedure. */ 17512 17513 #if CCS_STATS 17514 struct { 17515 struct { 17516 int64_t count, bytes; 17517 } tot, hit; 17518 } wrw_stats; 17519 #endif 17520 17521 /* 17522 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17523 * messages. 17524 */ 17525 /* ARGSUSED */ 17526 static void 17527 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17528 { 17529 conn_t *connp = (conn_t *)arg; 17530 tcp_t *tcp = connp->conn_tcp; 17531 queue_t *q = tcp->tcp_wq; 17532 17533 ASSERT(DB_TYPE(mp) != M_IOCTL); 17534 /* 17535 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17536 * Once the close starts, streamhead and sockfs will not let any data 17537 * packets come down (close ensures that there are no threads using the 17538 * queue and no new threads will come down) but since qprocsoff() 17539 * hasn't happened yet, a M_FLUSH or some non data message might 17540 * get reflected back (in response to our own FLUSHRW) and get 17541 * processed after tcp_close() is done. The conn would still be valid 17542 * because a ref would have added but we need to check the state 17543 * before actually processing the packet. 17544 */ 17545 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17546 freemsg(mp); 17547 return; 17548 } 17549 17550 switch (DB_TYPE(mp)) { 17551 case M_IOCDATA: 17552 tcp_wput_iocdata(tcp, mp); 17553 break; 17554 case M_FLUSH: 17555 tcp_wput_flush(tcp, mp); 17556 break; 17557 default: 17558 CALL_IP_WPUT(connp, q, mp); 17559 break; 17560 } 17561 } 17562 17563 /* 17564 * The TCP fast path write put procedure. 17565 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17566 */ 17567 /* ARGSUSED */ 17568 void 17569 tcp_output(void *arg, mblk_t *mp, void *arg2) 17570 { 17571 int len; 17572 int hdrlen; 17573 int plen; 17574 mblk_t *mp1; 17575 uchar_t *rptr; 17576 uint32_t snxt; 17577 tcph_t *tcph; 17578 struct datab *db; 17579 uint32_t suna; 17580 uint32_t mss; 17581 ipaddr_t *dst; 17582 ipaddr_t *src; 17583 uint32_t sum; 17584 int usable; 17585 conn_t *connp = (conn_t *)arg; 17586 tcp_t *tcp = connp->conn_tcp; 17587 uint32_t msize; 17588 tcp_stack_t *tcps = tcp->tcp_tcps; 17589 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17590 17591 /* 17592 * Try and ASSERT the minimum possible references on the 17593 * conn early enough. Since we are executing on write side, 17594 * the connection is obviously not detached and that means 17595 * there is a ref each for TCP and IP. Since we are behind 17596 * the squeue, the minimum references needed are 3. If the 17597 * conn is in classifier hash list, there should be an 17598 * extra ref for that (we check both the possibilities). 17599 */ 17600 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17601 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17602 17603 ASSERT(DB_TYPE(mp) == M_DATA); 17604 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17605 17606 mutex_enter(&tcp->tcp_non_sq_lock); 17607 tcp->tcp_squeue_bytes -= msize; 17608 mutex_exit(&tcp->tcp_non_sq_lock); 17609 17610 /* Check to see if this connection wants to be re-fused. */ 17611 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17612 if (tcp->tcp_ipversion == IPV4_VERSION) { 17613 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17614 &tcp->tcp_saved_tcph); 17615 } else { 17616 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17617 &tcp->tcp_saved_tcph); 17618 } 17619 } 17620 /* Bypass tcp protocol for fused tcp loopback */ 17621 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17622 return; 17623 17624 mss = tcp->tcp_mss; 17625 if (tcp->tcp_xmit_zc_clean) 17626 mp = tcp_zcopy_backoff(tcp, mp, 0); 17627 17628 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17629 len = (int)(mp->b_wptr - mp->b_rptr); 17630 17631 /* 17632 * Criteria for fast path: 17633 * 17634 * 1. no unsent data 17635 * 2. single mblk in request 17636 * 3. connection established 17637 * 4. data in mblk 17638 * 5. len <= mss 17639 * 6. no tcp_valid bits 17640 */ 17641 if ((tcp->tcp_unsent != 0) || 17642 (tcp->tcp_cork) || 17643 (mp->b_cont != NULL) || 17644 (tcp->tcp_state != TCPS_ESTABLISHED) || 17645 (len == 0) || 17646 (len > mss) || 17647 (tcp->tcp_valid_bits != 0)) { 17648 tcp_wput_data(tcp, mp, B_FALSE); 17649 return; 17650 } 17651 17652 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17653 ASSERT(tcp->tcp_fin_sent == 0); 17654 17655 /* queue new packet onto retransmission queue */ 17656 if (tcp->tcp_xmit_head == NULL) { 17657 tcp->tcp_xmit_head = mp; 17658 } else { 17659 tcp->tcp_xmit_last->b_cont = mp; 17660 } 17661 tcp->tcp_xmit_last = mp; 17662 tcp->tcp_xmit_tail = mp; 17663 17664 /* find out how much we can send */ 17665 /* BEGIN CSTYLED */ 17666 /* 17667 * un-acked usable 17668 * |--------------|-----------------| 17669 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17670 */ 17671 /* END CSTYLED */ 17672 17673 /* start sending from tcp_snxt */ 17674 snxt = tcp->tcp_snxt; 17675 17676 /* 17677 * Check to see if this connection has been idled for some 17678 * time and no ACK is expected. If it is, we need to slow 17679 * start again to get back the connection's "self-clock" as 17680 * described in VJ's paper. 17681 * 17682 * Refer to the comment in tcp_mss_set() for the calculation 17683 * of tcp_cwnd after idle. 17684 */ 17685 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17686 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17687 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17688 } 17689 17690 usable = tcp->tcp_swnd; /* tcp window size */ 17691 if (usable > tcp->tcp_cwnd) 17692 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17693 usable -= snxt; /* subtract stuff already sent */ 17694 suna = tcp->tcp_suna; 17695 usable += suna; 17696 /* usable can be < 0 if the congestion window is smaller */ 17697 if (len > usable) { 17698 /* Can't send complete M_DATA in one shot */ 17699 goto slow; 17700 } 17701 17702 mutex_enter(&tcp->tcp_non_sq_lock); 17703 if (tcp->tcp_flow_stopped && 17704 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17705 tcp_clrqfull(tcp); 17706 } 17707 mutex_exit(&tcp->tcp_non_sq_lock); 17708 17709 /* 17710 * determine if anything to send (Nagle). 17711 * 17712 * 1. len < tcp_mss (i.e. small) 17713 * 2. unacknowledged data present 17714 * 3. len < nagle limit 17715 * 4. last packet sent < nagle limit (previous packet sent) 17716 */ 17717 if ((len < mss) && (snxt != suna) && 17718 (len < (int)tcp->tcp_naglim) && 17719 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17720 /* 17721 * This was the first unsent packet and normally 17722 * mss < xmit_hiwater so there is no need to worry 17723 * about flow control. The next packet will go 17724 * through the flow control check in tcp_wput_data(). 17725 */ 17726 /* leftover work from above */ 17727 tcp->tcp_unsent = len; 17728 tcp->tcp_xmit_tail_unsent = len; 17729 17730 return; 17731 } 17732 17733 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17734 17735 if (snxt == suna) { 17736 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17737 } 17738 17739 /* we have always sent something */ 17740 tcp->tcp_rack_cnt = 0; 17741 17742 tcp->tcp_snxt = snxt + len; 17743 tcp->tcp_rack = tcp->tcp_rnxt; 17744 17745 if ((mp1 = dupb(mp)) == 0) 17746 goto no_memory; 17747 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17748 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17749 17750 /* adjust tcp header information */ 17751 tcph = tcp->tcp_tcph; 17752 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17753 17754 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17755 sum = (sum >> 16) + (sum & 0xFFFF); 17756 U16_TO_ABE16(sum, tcph->th_sum); 17757 17758 U32_TO_ABE32(snxt, tcph->th_seq); 17759 17760 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17761 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17762 BUMP_LOCAL(tcp->tcp_obsegs); 17763 17764 /* Update the latest receive window size in TCP header. */ 17765 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17766 tcph->th_win); 17767 17768 tcp->tcp_last_sent_len = (ushort_t)len; 17769 17770 plen = len + tcp->tcp_hdr_len; 17771 17772 if (tcp->tcp_ipversion == IPV4_VERSION) { 17773 tcp->tcp_ipha->ipha_length = htons(plen); 17774 } else { 17775 tcp->tcp_ip6h->ip6_plen = htons(plen - 17776 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17777 } 17778 17779 /* see if we need to allocate a mblk for the headers */ 17780 hdrlen = tcp->tcp_hdr_len; 17781 rptr = mp1->b_rptr - hdrlen; 17782 db = mp1->b_datap; 17783 if ((db->db_ref != 2) || rptr < db->db_base || 17784 (!OK_32PTR(rptr))) { 17785 /* NOTE: we assume allocb returns an OK_32PTR */ 17786 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17787 tcps->tcps_wroff_xtra, BPRI_MED); 17788 if (!mp) { 17789 freemsg(mp1); 17790 goto no_memory; 17791 } 17792 mp->b_cont = mp1; 17793 mp1 = mp; 17794 /* Leave room for Link Level header */ 17795 /* hdrlen = tcp->tcp_hdr_len; */ 17796 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17797 mp1->b_wptr = &rptr[hdrlen]; 17798 } 17799 mp1->b_rptr = rptr; 17800 17801 /* Fill in the timestamp option. */ 17802 if (tcp->tcp_snd_ts_ok) { 17803 U32_TO_BE32((uint32_t)lbolt, 17804 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17805 U32_TO_BE32(tcp->tcp_ts_recent, 17806 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17807 } else { 17808 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17809 } 17810 17811 /* copy header into outgoing packet */ 17812 dst = (ipaddr_t *)rptr; 17813 src = (ipaddr_t *)tcp->tcp_iphc; 17814 dst[0] = src[0]; 17815 dst[1] = src[1]; 17816 dst[2] = src[2]; 17817 dst[3] = src[3]; 17818 dst[4] = src[4]; 17819 dst[5] = src[5]; 17820 dst[6] = src[6]; 17821 dst[7] = src[7]; 17822 dst[8] = src[8]; 17823 dst[9] = src[9]; 17824 if (hdrlen -= 40) { 17825 hdrlen >>= 2; 17826 dst += 10; 17827 src += 10; 17828 do { 17829 *dst++ = *src++; 17830 } while (--hdrlen); 17831 } 17832 17833 /* 17834 * Set the ECN info in the TCP header. Note that this 17835 * is not the template header. 17836 */ 17837 if (tcp->tcp_ecn_ok) { 17838 SET_ECT(tcp, rptr); 17839 17840 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17841 if (tcp->tcp_ecn_echo_on) 17842 tcph->th_flags[0] |= TH_ECE; 17843 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17844 tcph->th_flags[0] |= TH_CWR; 17845 tcp->tcp_ecn_cwr_sent = B_TRUE; 17846 } 17847 } 17848 17849 if (tcp->tcp_ip_forward_progress) { 17850 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17851 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17852 tcp->tcp_ip_forward_progress = B_FALSE; 17853 } 17854 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17855 return; 17856 17857 /* 17858 * If we ran out of memory, we pretend to have sent the packet 17859 * and that it was lost on the wire. 17860 */ 17861 no_memory: 17862 return; 17863 17864 slow: 17865 /* leftover work from above */ 17866 tcp->tcp_unsent = len; 17867 tcp->tcp_xmit_tail_unsent = len; 17868 tcp_wput_data(tcp, NULL, B_FALSE); 17869 } 17870 17871 /* ARGSUSED */ 17872 void 17873 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17874 { 17875 conn_t *connp = (conn_t *)arg; 17876 tcp_t *tcp = connp->conn_tcp; 17877 queue_t *q = tcp->tcp_rq; 17878 struct tcp_options *tcpopt; 17879 tcp_stack_t *tcps = tcp->tcp_tcps; 17880 17881 /* socket options */ 17882 uint_t sopp_flags; 17883 ssize_t sopp_rxhiwat; 17884 ssize_t sopp_maxblk; 17885 ushort_t sopp_wroff; 17886 ushort_t sopp_tail; 17887 ushort_t sopp_copyopt; 17888 17889 tcpopt = (struct tcp_options *)mp->b_rptr; 17890 17891 /* 17892 * Drop the eager's ref on the listener, that was placed when 17893 * this eager began life in tcp_conn_request. 17894 */ 17895 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17896 if (IPCL_IS_NONSTR(connp)) { 17897 /* Safe to free conn_ind message */ 17898 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17899 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17900 } 17901 17902 tcp->tcp_detached = B_FALSE; 17903 17904 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17905 /* 17906 * Someone blewoff the eager before we could finish 17907 * the accept. 17908 * 17909 * The only reason eager exists it because we put in 17910 * a ref on it when conn ind went up. We need to send 17911 * a disconnect indication up while the last reference 17912 * on the eager will be dropped by the squeue when we 17913 * return. 17914 */ 17915 ASSERT(tcp->tcp_listener == NULL); 17916 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17917 if (IPCL_IS_NONSTR(connp)) { 17918 ASSERT(tcp->tcp_issocket); 17919 (*connp->conn_upcalls->su_disconnected)( 17920 connp->conn_upper_handle, tcp->tcp_connid, 17921 ECONNREFUSED); 17922 freemsg(mp); 17923 } else { 17924 struct T_discon_ind *tdi; 17925 17926 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17927 /* 17928 * Let us reuse the incoming mblk to avoid 17929 * memory allocation failure problems. We know 17930 * that the size of the incoming mblk i.e. 17931 * stroptions is greater than sizeof 17932 * T_discon_ind. So the reallocb below can't 17933 * fail. 17934 */ 17935 freemsg(mp->b_cont); 17936 mp->b_cont = NULL; 17937 ASSERT(DB_REF(mp) == 1); 17938 mp = reallocb(mp, sizeof (struct T_discon_ind), 17939 B_FALSE); 17940 ASSERT(mp != NULL); 17941 DB_TYPE(mp) = M_PROTO; 17942 ((union T_primitives *)mp->b_rptr)->type = 17943 T_DISCON_IND; 17944 tdi = (struct T_discon_ind *)mp->b_rptr; 17945 if (tcp->tcp_issocket) { 17946 tdi->DISCON_reason = ECONNREFUSED; 17947 tdi->SEQ_number = 0; 17948 } else { 17949 tdi->DISCON_reason = ENOPROTOOPT; 17950 tdi->SEQ_number = 17951 tcp->tcp_conn_req_seqnum; 17952 } 17953 mp->b_wptr = mp->b_rptr + 17954 sizeof (struct T_discon_ind); 17955 putnext(q, mp); 17956 return; 17957 } 17958 } 17959 if (tcp->tcp_hard_binding) { 17960 tcp->tcp_hard_binding = B_FALSE; 17961 tcp->tcp_hard_bound = B_TRUE; 17962 } 17963 return; 17964 } 17965 17966 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17967 int boundif = tcpopt->to_boundif; 17968 uint_t len = sizeof (int); 17969 17970 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17971 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17972 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17973 } 17974 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17975 uint_t on = 1; 17976 uint_t len = sizeof (uint_t); 17977 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17978 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17979 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17980 } 17981 17982 /* 17983 * For a loopback connection with tcp_direct_sockfs on, note that 17984 * we don't have to protect tcp_rcv_list yet because synchronous 17985 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17986 * possibly race with us. 17987 */ 17988 17989 /* 17990 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17991 * properly. This is the first time we know of the acceptor' 17992 * queue. So we do it here. 17993 * 17994 * XXX 17995 */ 17996 if (tcp->tcp_rcv_list == NULL) { 17997 /* 17998 * Recv queue is empty, tcp_rwnd should not have changed. 17999 * That means it should be equal to the listener's tcp_rwnd. 18000 */ 18001 if (!IPCL_IS_NONSTR(connp)) 18002 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 18003 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 18004 } else { 18005 #ifdef DEBUG 18006 mblk_t *tmp; 18007 mblk_t *mp1; 18008 uint_t cnt = 0; 18009 18010 mp1 = tcp->tcp_rcv_list; 18011 while ((tmp = mp1) != NULL) { 18012 mp1 = tmp->b_next; 18013 cnt += msgdsize(tmp); 18014 } 18015 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 18016 #endif 18017 /* There is some data, add them back to get the max. */ 18018 if (!IPCL_IS_NONSTR(connp)) 18019 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18020 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 18021 } 18022 /* 18023 * This is the first time we run on the correct 18024 * queue after tcp_accept. So fix all the q parameters 18025 * here. 18026 */ 18027 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 18028 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 18029 18030 /* 18031 * Record the stream head's high water mark for this endpoint; 18032 * this is used for flow-control purposes. 18033 */ 18034 sopp_rxhiwat = tcp->tcp_fused ? 18035 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 18036 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 18037 18038 /* 18039 * Determine what write offset value to use depending on SACK and 18040 * whether the endpoint is fused or not. 18041 */ 18042 if (tcp->tcp_fused) { 18043 ASSERT(tcp->tcp_loopback); 18044 ASSERT(tcp->tcp_loopback_peer != NULL); 18045 /* 18046 * For fused tcp loopback, set the stream head's write 18047 * offset value to zero since we won't be needing any room 18048 * for TCP/IP headers. This would also improve performance 18049 * since it would reduce the amount of work done by kmem. 18050 * Non-fused tcp loopback case is handled separately below. 18051 */ 18052 sopp_wroff = 0; 18053 /* 18054 * Update the peer's transmit parameters according to 18055 * our recently calculated high water mark value. 18056 */ 18057 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 18058 } else if (tcp->tcp_snd_sack_ok) { 18059 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 18060 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 18061 } else { 18062 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 18063 tcps->tcps_wroff_xtra); 18064 } 18065 18066 /* 18067 * If this is endpoint is handling SSL, then reserve extra 18068 * offset and space at the end. 18069 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18070 * overriding the previous setting. The extra cost of signing and 18071 * encrypting multiple MSS-size records (12 of them with Ethernet), 18072 * instead of a single contiguous one by the stream head 18073 * largely outweighs the statistical reduction of ACKs, when 18074 * applicable. The peer will also save on decryption and verification 18075 * costs. 18076 */ 18077 if (tcp->tcp_kssl_ctx != NULL) { 18078 sopp_wroff += SSL3_WROFFSET; 18079 18080 sopp_flags |= SOCKOPT_TAIL; 18081 sopp_tail = SSL3_MAX_TAIL_LEN; 18082 18083 sopp_flags |= SOCKOPT_ZCOPY; 18084 sopp_copyopt = ZCVMUNSAFE; 18085 18086 sopp_maxblk = SSL3_MAX_RECORD_LEN; 18087 } 18088 18089 /* Send the options up */ 18090 if (IPCL_IS_NONSTR(connp)) { 18091 struct sock_proto_props sopp; 18092 18093 sopp.sopp_flags = sopp_flags; 18094 sopp.sopp_wroff = sopp_wroff; 18095 sopp.sopp_maxblk = sopp_maxblk; 18096 sopp.sopp_rxhiwat = sopp_rxhiwat; 18097 if (sopp_flags & SOCKOPT_TAIL) { 18098 ASSERT(tcp->tcp_kssl_ctx != NULL); 18099 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 18100 sopp.sopp_tail = sopp_tail; 18101 sopp.sopp_zcopyflag = sopp_copyopt; 18102 } 18103 (*connp->conn_upcalls->su_set_proto_props) 18104 (connp->conn_upper_handle, &sopp); 18105 } else { 18106 struct stroptions *stropt; 18107 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18108 if (stropt_mp == NULL) { 18109 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 18110 return; 18111 } 18112 DB_TYPE(stropt_mp) = M_SETOPTS; 18113 stropt = (struct stroptions *)stropt_mp->b_rptr; 18114 stropt_mp->b_wptr += sizeof (struct stroptions); 18115 stropt = (struct stroptions *)stropt_mp->b_rptr; 18116 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 18117 stropt->so_hiwat = sopp_rxhiwat; 18118 stropt->so_wroff = sopp_wroff; 18119 stropt->so_maxblk = sopp_maxblk; 18120 18121 if (sopp_flags & SOCKOPT_TAIL) { 18122 ASSERT(tcp->tcp_kssl_ctx != NULL); 18123 18124 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 18125 stropt->so_tail = sopp_tail; 18126 stropt->so_copyopt = sopp_copyopt; 18127 } 18128 18129 /* Send the options up */ 18130 putnext(q, stropt_mp); 18131 } 18132 18133 freemsg(mp); 18134 /* 18135 * Pass up any data and/or a fin that has been received. 18136 * 18137 * Adjust receive window in case it had decreased 18138 * (because there is data <=> tcp_rcv_list != NULL) 18139 * while the connection was detached. Note that 18140 * in case the eager was flow-controlled, w/o this 18141 * code, the rwnd may never open up again! 18142 */ 18143 if (tcp->tcp_rcv_list != NULL) { 18144 if (IPCL_IS_NONSTR(connp)) { 18145 mblk_t *mp; 18146 int space_left; 18147 int error; 18148 boolean_t push = B_TRUE; 18149 18150 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 18151 (connp->conn_upper_handle, NULL, 0, 0, &error, 18152 &push) >= 0) { 18153 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 18154 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18155 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18156 tcp_xmit_ctl(NULL, 18157 tcp, (tcp->tcp_swnd == 0) ? 18158 tcp->tcp_suna : tcp->tcp_snxt, 18159 tcp->tcp_rnxt, TH_ACK); 18160 } 18161 } 18162 while ((mp = tcp->tcp_rcv_list) != NULL) { 18163 push = B_TRUE; 18164 tcp->tcp_rcv_list = mp->b_next; 18165 mp->b_next = NULL; 18166 space_left = (*connp->conn_upcalls->su_recv) 18167 (connp->conn_upper_handle, mp, msgdsize(mp), 18168 0, &error, &push); 18169 if (space_left < 0) { 18170 /* 18171 * At this point the eager is not 18172 * visible to anyone, so fallback 18173 * can not happen. 18174 */ 18175 ASSERT(error != EOPNOTSUPP); 18176 } 18177 } 18178 tcp->tcp_rcv_last_head = NULL; 18179 tcp->tcp_rcv_last_tail = NULL; 18180 tcp->tcp_rcv_cnt = 0; 18181 } else { 18182 /* We drain directly in case of fused tcp loopback */ 18183 sodirect_t *sodp; 18184 18185 if (!tcp->tcp_fused && canputnext(q)) { 18186 tcp->tcp_rwnd = q->q_hiwat; 18187 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18188 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18189 tcp_xmit_ctl(NULL, 18190 tcp, (tcp->tcp_swnd == 0) ? 18191 tcp->tcp_suna : tcp->tcp_snxt, 18192 tcp->tcp_rnxt, TH_ACK); 18193 } 18194 } 18195 18196 SOD_PTR_ENTER(tcp, sodp); 18197 if (sodp != NULL) { 18198 /* Sodirect, move from rcv_list */ 18199 ASSERT(!tcp->tcp_fused); 18200 while ((mp = tcp->tcp_rcv_list) != NULL) { 18201 tcp->tcp_rcv_list = mp->b_next; 18202 mp->b_next = NULL; 18203 (void) tcp_rcv_sod_enqueue(tcp, sodp, 18204 mp, msgdsize(mp)); 18205 } 18206 tcp->tcp_rcv_last_head = NULL; 18207 tcp->tcp_rcv_last_tail = NULL; 18208 tcp->tcp_rcv_cnt = 0; 18209 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18210 /* sod_wakeup() did the mutex_exit() */ 18211 } else { 18212 /* Not sodirect, drain */ 18213 (void) tcp_rcv_drain(tcp); 18214 } 18215 } 18216 18217 /* 18218 * For fused tcp loopback, back-enable peer endpoint 18219 * if it's currently flow-controlled. 18220 */ 18221 if (tcp->tcp_fused) { 18222 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18223 18224 ASSERT(peer_tcp != NULL); 18225 ASSERT(peer_tcp->tcp_fused); 18226 /* 18227 * In order to change the peer's tcp_flow_stopped, 18228 * we need to take locks for both end points. The 18229 * highest address is taken first. 18230 */ 18231 if (peer_tcp > tcp) { 18232 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18233 mutex_enter(&tcp->tcp_non_sq_lock); 18234 } else { 18235 mutex_enter(&tcp->tcp_non_sq_lock); 18236 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18237 } 18238 if (peer_tcp->tcp_flow_stopped) { 18239 tcp_clrqfull(peer_tcp); 18240 TCP_STAT(tcps, tcp_fusion_backenabled); 18241 } 18242 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18243 mutex_exit(&tcp->tcp_non_sq_lock); 18244 } 18245 } 18246 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18247 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18248 tcp->tcp_ordrel_done = B_TRUE; 18249 if (IPCL_IS_NONSTR(connp)) { 18250 ASSERT(tcp->tcp_ordrel_mp == NULL); 18251 (*connp->conn_upcalls->su_opctl)( 18252 connp->conn_upper_handle, 18253 SOCK_OPCTL_SHUT_RECV, 0); 18254 } else { 18255 mp = tcp->tcp_ordrel_mp; 18256 tcp->tcp_ordrel_mp = NULL; 18257 putnext(q, mp); 18258 } 18259 } 18260 if (tcp->tcp_hard_binding) { 18261 tcp->tcp_hard_binding = B_FALSE; 18262 tcp->tcp_hard_bound = B_TRUE; 18263 } 18264 18265 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 18266 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 18267 tcp->tcp_loopback_peer != NULL && 18268 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 18269 tcp_fuse_syncstr_enable_pair(tcp); 18270 } 18271 18272 if (tcp->tcp_ka_enabled) { 18273 tcp->tcp_ka_last_intrvl = 0; 18274 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18275 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18276 } 18277 18278 /* 18279 * At this point, eager is fully established and will 18280 * have the following references - 18281 * 18282 * 2 references for connection to exist (1 for TCP and 1 for IP). 18283 * 1 reference for the squeue which will be dropped by the squeue as 18284 * soon as this function returns. 18285 * There will be 1 additonal reference for being in classifier 18286 * hash list provided something bad hasn't happened. 18287 */ 18288 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18289 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18290 } 18291 18292 /* 18293 * The function called through squeue to get behind listener's perimeter to 18294 * send a deffered conn_ind. 18295 */ 18296 /* ARGSUSED */ 18297 void 18298 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18299 { 18300 conn_t *connp = (conn_t *)arg; 18301 tcp_t *listener = connp->conn_tcp; 18302 struct T_conn_ind *conn_ind; 18303 tcp_t *tcp; 18304 18305 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18306 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18307 conn_ind->OPT_length); 18308 18309 if (listener->tcp_state == TCPS_CLOSED || 18310 TCP_IS_DETACHED(listener)) { 18311 /* 18312 * If listener has closed, it would have caused a 18313 * a cleanup/blowoff to happen for the eager. 18314 * 18315 * We need to drop the ref on eager that was put 18316 * tcp_rput_data() before trying to send the conn_ind 18317 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18318 * and tcp_wput_accept() is sending this deferred conn_ind but 18319 * listener is closed so we drop the ref. 18320 */ 18321 CONN_DEC_REF(tcp->tcp_connp); 18322 freemsg(mp); 18323 return; 18324 } 18325 18326 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 18327 } 18328 18329 /* ARGSUSED */ 18330 static int 18331 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 18332 { 18333 tcp_t *listener, *eager; 18334 mblk_t *opt_mp; 18335 struct tcp_options *tcpopt; 18336 18337 listener = lconnp->conn_tcp; 18338 ASSERT(listener->tcp_state == TCPS_LISTEN); 18339 eager = econnp->conn_tcp; 18340 ASSERT(eager->tcp_listener != NULL); 18341 18342 ASSERT(eager->tcp_rq != NULL); 18343 18344 /* If tcp_fused and sodirect enabled disable it */ 18345 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18346 /* Fused, disable sodirect */ 18347 mutex_enter(eager->tcp_sodirect->sod_lockp); 18348 SOD_DISABLE(eager->tcp_sodirect); 18349 mutex_exit(eager->tcp_sodirect->sod_lockp); 18350 eager->tcp_sodirect = NULL; 18351 } 18352 18353 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 18354 if (opt_mp == NULL) { 18355 return (-TPROTO); 18356 } 18357 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 18358 eager->tcp_issocket = B_TRUE; 18359 18360 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18361 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18362 ASSERT(econnp->conn_netstack == 18363 listener->tcp_connp->conn_netstack); 18364 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18365 18366 /* Put the ref for IP */ 18367 CONN_INC_REF(econnp); 18368 18369 /* 18370 * We should have minimum of 3 references on the conn 18371 * at this point. One each for TCP and IP and one for 18372 * the T_conn_ind that was sent up when the 3-way handshake 18373 * completed. In the normal case we would also have another 18374 * reference (making a total of 4) for the conn being in the 18375 * classifier hash list. However the eager could have received 18376 * an RST subsequently and tcp_closei_local could have removed 18377 * the eager from the classifier hash list, hence we can't 18378 * assert that reference. 18379 */ 18380 ASSERT(econnp->conn_ref >= 3); 18381 18382 opt_mp->b_datap->db_type = M_SETOPTS; 18383 opt_mp->b_wptr += sizeof (struct tcp_options); 18384 18385 /* 18386 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18387 * from listener to acceptor. 18388 */ 18389 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 18390 tcpopt->to_flags = 0; 18391 18392 if (listener->tcp_bound_if != 0) { 18393 tcpopt->to_flags |= TCPOPT_BOUNDIF; 18394 tcpopt->to_boundif = listener->tcp_bound_if; 18395 } 18396 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18397 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 18398 } 18399 18400 mutex_enter(&listener->tcp_eager_lock); 18401 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18402 18403 tcp_t *tail; 18404 tcp_t *tcp; 18405 mblk_t *mp1; 18406 18407 tcp = listener->tcp_eager_prev_q0; 18408 /* 18409 * listener->tcp_eager_prev_q0 points to the TAIL of the 18410 * deferred T_conn_ind queue. We need to get to the head 18411 * of the queue in order to send up T_conn_ind the same 18412 * order as how the 3WHS is completed. 18413 */ 18414 while (tcp != listener) { 18415 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18416 !tcp->tcp_kssl_pending) 18417 break; 18418 else 18419 tcp = tcp->tcp_eager_prev_q0; 18420 } 18421 /* None of the pending eagers can be sent up now */ 18422 if (tcp == listener) 18423 goto no_more_eagers; 18424 18425 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18426 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18427 /* Move from q0 to q */ 18428 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18429 listener->tcp_conn_req_cnt_q0--; 18430 listener->tcp_conn_req_cnt_q++; 18431 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18432 tcp->tcp_eager_prev_q0; 18433 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18434 tcp->tcp_eager_next_q0; 18435 tcp->tcp_eager_prev_q0 = NULL; 18436 tcp->tcp_eager_next_q0 = NULL; 18437 tcp->tcp_conn_def_q0 = B_FALSE; 18438 18439 /* Make sure the tcp isn't in the list of droppables */ 18440 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18441 tcp->tcp_eager_prev_drop_q0 == NULL); 18442 18443 /* 18444 * Insert at end of the queue because sockfs sends 18445 * down T_CONN_RES in chronological order. Leaving 18446 * the older conn indications at front of the queue 18447 * helps reducing search time. 18448 */ 18449 tail = listener->tcp_eager_last_q; 18450 if (tail != NULL) { 18451 tail->tcp_eager_next_q = tcp; 18452 } else { 18453 listener->tcp_eager_next_q = tcp; 18454 } 18455 listener->tcp_eager_last_q = tcp; 18456 tcp->tcp_eager_next_q = NULL; 18457 18458 /* Need to get inside the listener perimeter */ 18459 CONN_INC_REF(listener->tcp_connp); 18460 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18461 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18462 SQTAG_TCP_SEND_PENDING); 18463 } 18464 no_more_eagers: 18465 tcp_eager_unlink(eager); 18466 mutex_exit(&listener->tcp_eager_lock); 18467 18468 /* 18469 * At this point, the eager is detached from the listener 18470 * but we still have an extra refs on eager (apart from the 18471 * usual tcp references). The ref was placed in tcp_rput_data 18472 * before sending the conn_ind in tcp_send_conn_ind. 18473 * The ref will be dropped in tcp_accept_finish(). 18474 */ 18475 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18476 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18477 return (0); 18478 } 18479 18480 int 18481 tcp_accept(sock_lower_handle_t lproto_handle, 18482 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18483 cred_t *cr) 18484 { 18485 conn_t *lconnp, *econnp; 18486 tcp_t *listener, *eager; 18487 tcp_stack_t *tcps; 18488 18489 lconnp = (conn_t *)lproto_handle; 18490 listener = lconnp->conn_tcp; 18491 ASSERT(listener->tcp_state == TCPS_LISTEN); 18492 econnp = (conn_t *)eproto_handle; 18493 eager = econnp->conn_tcp; 18494 ASSERT(eager->tcp_listener != NULL); 18495 tcps = eager->tcp_tcps; 18496 18497 /* 18498 * It is OK to manipulate these fields outside the eager's squeue 18499 * because they will not start being used until tcp_accept_finish 18500 * has been called. 18501 */ 18502 ASSERT(lconnp->conn_upper_handle != NULL); 18503 ASSERT(econnp->conn_upper_handle == NULL); 18504 econnp->conn_upper_handle = sock_handle; 18505 econnp->conn_upcalls = lconnp->conn_upcalls; 18506 ASSERT(IPCL_IS_NONSTR(econnp)); 18507 /* 18508 * Create helper stream if it is a non-TPI TCP connection. 18509 */ 18510 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18511 ip1dbg(("tcp_accept: create of IP helper stream" 18512 " failed\n")); 18513 return (EPROTO); 18514 } 18515 eager->tcp_rq = econnp->conn_rq; 18516 eager->tcp_wq = econnp->conn_wq; 18517 18518 ASSERT(eager->tcp_rq != NULL); 18519 18520 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18521 return (tcp_accept_common(lconnp, econnp, cr)); 18522 } 18523 18524 18525 /* 18526 * This is the STREAMS entry point for T_CONN_RES coming down on 18527 * Acceptor STREAM when sockfs listener does accept processing. 18528 * Read the block comment on top of tcp_conn_request(). 18529 */ 18530 void 18531 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18532 { 18533 queue_t *rq = RD(q); 18534 struct T_conn_res *conn_res; 18535 tcp_t *eager; 18536 tcp_t *listener; 18537 struct T_ok_ack *ok; 18538 t_scalar_t PRIM_type; 18539 conn_t *econnp; 18540 cred_t *cr; 18541 18542 ASSERT(DB_TYPE(mp) == M_PROTO); 18543 18544 /* 18545 * All Solaris components should pass a db_credp 18546 * for this TPI message, hence we ASSERT. 18547 * But in case there is some other M_PROTO that looks 18548 * like a TPI message sent by some other kernel 18549 * component, we check and return an error. 18550 */ 18551 cr = msg_getcred(mp, NULL); 18552 ASSERT(cr != NULL); 18553 if (cr == NULL) { 18554 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 18555 if (mp != NULL) 18556 putnext(rq, mp); 18557 return; 18558 } 18559 conn_res = (struct T_conn_res *)mp->b_rptr; 18560 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18561 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18562 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18563 if (mp != NULL) 18564 putnext(rq, mp); 18565 return; 18566 } 18567 switch (conn_res->PRIM_type) { 18568 case O_T_CONN_RES: 18569 case T_CONN_RES: 18570 /* 18571 * We pass up an err ack if allocb fails. This will 18572 * cause sockfs to issue a T_DISCON_REQ which will cause 18573 * tcp_eager_blowoff to be called. sockfs will then call 18574 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18575 * we need to do the allocb up here because we have to 18576 * make sure rq->q_qinfo->qi_qclose still points to the 18577 * correct function (tcpclose_accept) in case allocb 18578 * fails. 18579 */ 18580 bcopy(mp->b_rptr + conn_res->OPT_offset, 18581 &eager, conn_res->OPT_length); 18582 PRIM_type = conn_res->PRIM_type; 18583 mp->b_datap->db_type = M_PCPROTO; 18584 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18585 ok = (struct T_ok_ack *)mp->b_rptr; 18586 ok->PRIM_type = T_OK_ACK; 18587 ok->CORRECT_prim = PRIM_type; 18588 econnp = eager->tcp_connp; 18589 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18590 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18591 eager->tcp_rq = rq; 18592 eager->tcp_wq = q; 18593 rq->q_ptr = econnp; 18594 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18595 q->q_ptr = econnp; 18596 q->q_qinfo = &tcp_winit; 18597 listener = eager->tcp_listener; 18598 18599 /* 18600 * TCP is _D_SODIRECT and sockfs is directly above so 18601 * save shared sodirect_t pointer (if any). 18602 */ 18603 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18604 if (tcp_accept_common(listener->tcp_connp, 18605 econnp, cr) < 0) { 18606 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18607 if (mp != NULL) 18608 putnext(rq, mp); 18609 return; 18610 } 18611 18612 /* 18613 * Send the new local address also up to sockfs. There 18614 * should already be enough space in the mp that came 18615 * down from soaccept(). 18616 */ 18617 if (eager->tcp_family == AF_INET) { 18618 sin_t *sin; 18619 18620 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18621 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18622 sin = (sin_t *)mp->b_wptr; 18623 mp->b_wptr += sizeof (sin_t); 18624 sin->sin_family = AF_INET; 18625 sin->sin_port = eager->tcp_lport; 18626 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18627 } else { 18628 sin6_t *sin6; 18629 18630 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18631 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18632 sin6 = (sin6_t *)mp->b_wptr; 18633 mp->b_wptr += sizeof (sin6_t); 18634 sin6->sin6_family = AF_INET6; 18635 sin6->sin6_port = eager->tcp_lport; 18636 if (eager->tcp_ipversion == IPV4_VERSION) { 18637 sin6->sin6_flowinfo = 0; 18638 IN6_IPADDR_TO_V4MAPPED( 18639 eager->tcp_ipha->ipha_src, 18640 &sin6->sin6_addr); 18641 } else { 18642 ASSERT(eager->tcp_ip6h != NULL); 18643 sin6->sin6_flowinfo = 18644 eager->tcp_ip6h->ip6_vcf & 18645 ~IPV6_VERS_AND_FLOW_MASK; 18646 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18647 } 18648 sin6->sin6_scope_id = 0; 18649 sin6->__sin6_src_id = 0; 18650 } 18651 18652 putnext(rq, mp); 18653 return; 18654 default: 18655 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18656 if (mp != NULL) 18657 putnext(rq, mp); 18658 return; 18659 } 18660 } 18661 18662 static int 18663 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18664 { 18665 sin_t *sin = (sin_t *)sa; 18666 sin6_t *sin6 = (sin6_t *)sa; 18667 18668 switch (tcp->tcp_family) { 18669 case AF_INET: 18670 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18671 18672 if (*salenp < sizeof (sin_t)) 18673 return (EINVAL); 18674 18675 *sin = sin_null; 18676 sin->sin_family = AF_INET; 18677 if (tcp->tcp_state >= TCPS_BOUND) { 18678 sin->sin_port = tcp->tcp_lport; 18679 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18680 } 18681 *salenp = sizeof (sin_t); 18682 break; 18683 18684 case AF_INET6: 18685 if (*salenp < sizeof (sin6_t)) 18686 return (EINVAL); 18687 18688 *sin6 = sin6_null; 18689 sin6->sin6_family = AF_INET6; 18690 if (tcp->tcp_state >= TCPS_BOUND) { 18691 sin6->sin6_port = tcp->tcp_lport; 18692 if (tcp->tcp_ipversion == IPV4_VERSION) { 18693 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18694 &sin6->sin6_addr); 18695 } else { 18696 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18697 } 18698 } 18699 *salenp = sizeof (sin6_t); 18700 break; 18701 } 18702 18703 return (0); 18704 } 18705 18706 static int 18707 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18708 { 18709 sin_t *sin = (sin_t *)sa; 18710 sin6_t *sin6 = (sin6_t *)sa; 18711 18712 if (tcp->tcp_state < TCPS_SYN_RCVD) 18713 return (ENOTCONN); 18714 18715 switch (tcp->tcp_family) { 18716 case AF_INET: 18717 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18718 18719 if (*salenp < sizeof (sin_t)) 18720 return (EINVAL); 18721 18722 *sin = sin_null; 18723 sin->sin_family = AF_INET; 18724 sin->sin_port = tcp->tcp_fport; 18725 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18726 sin->sin_addr.s_addr); 18727 *salenp = sizeof (sin_t); 18728 break; 18729 18730 case AF_INET6: 18731 if (*salenp < sizeof (sin6_t)) 18732 return (EINVAL); 18733 18734 *sin6 = sin6_null; 18735 sin6->sin6_family = AF_INET6; 18736 sin6->sin6_port = tcp->tcp_fport; 18737 sin6->sin6_addr = tcp->tcp_remote_v6; 18738 if (tcp->tcp_ipversion == IPV6_VERSION) { 18739 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18740 ~IPV6_VERS_AND_FLOW_MASK; 18741 } 18742 *salenp = sizeof (sin6_t); 18743 break; 18744 } 18745 18746 return (0); 18747 } 18748 18749 /* 18750 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18751 */ 18752 static void 18753 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18754 { 18755 void *data; 18756 mblk_t *datamp = mp->b_cont; 18757 tcp_t *tcp = Q_TO_TCP(q); 18758 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18759 18760 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18761 cmdp->cb_error = EPROTO; 18762 qreply(q, mp); 18763 return; 18764 } 18765 18766 data = datamp->b_rptr; 18767 18768 switch (cmdp->cb_cmd) { 18769 case TI_GETPEERNAME: 18770 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 18771 break; 18772 case TI_GETMYNAME: 18773 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 18774 break; 18775 default: 18776 cmdp->cb_error = EINVAL; 18777 break; 18778 } 18779 18780 qreply(q, mp); 18781 } 18782 18783 void 18784 tcp_wput(queue_t *q, mblk_t *mp) 18785 { 18786 conn_t *connp = Q_TO_CONN(q); 18787 tcp_t *tcp; 18788 void (*output_proc)(); 18789 t_scalar_t type; 18790 uchar_t *rptr; 18791 struct iocblk *iocp; 18792 size_t size; 18793 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18794 18795 ASSERT(connp->conn_ref >= 2); 18796 18797 switch (DB_TYPE(mp)) { 18798 case M_DATA: 18799 tcp = connp->conn_tcp; 18800 ASSERT(tcp != NULL); 18801 18802 size = msgdsize(mp); 18803 18804 mutex_enter(&tcp->tcp_non_sq_lock); 18805 tcp->tcp_squeue_bytes += size; 18806 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18807 tcp_setqfull(tcp); 18808 } 18809 mutex_exit(&tcp->tcp_non_sq_lock); 18810 18811 CONN_INC_REF(connp); 18812 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18813 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18814 return; 18815 18816 case M_CMD: 18817 tcp_wput_cmdblk(q, mp); 18818 return; 18819 18820 case M_PROTO: 18821 case M_PCPROTO: 18822 /* 18823 * if it is a snmp message, don't get behind the squeue 18824 */ 18825 tcp = connp->conn_tcp; 18826 rptr = mp->b_rptr; 18827 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18828 type = ((union T_primitives *)rptr)->type; 18829 } else { 18830 if (tcp->tcp_debug) { 18831 (void) strlog(TCP_MOD_ID, 0, 1, 18832 SL_ERROR|SL_TRACE, 18833 "tcp_wput_proto, dropping one..."); 18834 } 18835 freemsg(mp); 18836 return; 18837 } 18838 if (type == T_SVR4_OPTMGMT_REQ) { 18839 /* 18840 * All Solaris components should pass a db_credp 18841 * for this TPI message, hence we ASSERT. 18842 * But in case there is some other M_PROTO that looks 18843 * like a TPI message sent by some other kernel 18844 * component, we check and return an error. 18845 */ 18846 cred_t *cr = msg_getcred(mp, NULL); 18847 18848 ASSERT(cr != NULL); 18849 if (cr == NULL) { 18850 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18851 return; 18852 } 18853 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18854 cr)) { 18855 /* 18856 * This was a SNMP request 18857 */ 18858 return; 18859 } else { 18860 output_proc = tcp_wput_proto; 18861 } 18862 } else { 18863 output_proc = tcp_wput_proto; 18864 } 18865 break; 18866 case M_IOCTL: 18867 /* 18868 * Most ioctls can be processed right away without going via 18869 * squeues - process them right here. Those that do require 18870 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18871 * are processed by tcp_wput_ioctl(). 18872 */ 18873 iocp = (struct iocblk *)mp->b_rptr; 18874 tcp = connp->conn_tcp; 18875 18876 switch (iocp->ioc_cmd) { 18877 case TCP_IOC_ABORT_CONN: 18878 tcp_ioctl_abort_conn(q, mp); 18879 return; 18880 case TI_GETPEERNAME: 18881 case TI_GETMYNAME: 18882 mi_copyin(q, mp, NULL, 18883 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18884 return; 18885 case ND_SET: 18886 /* nd_getset does the necessary checks */ 18887 case ND_GET: 18888 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18889 CALL_IP_WPUT(connp, q, mp); 18890 return; 18891 } 18892 qreply(q, mp); 18893 return; 18894 case TCP_IOC_DEFAULT_Q: 18895 /* 18896 * Wants to be the default wq. Check the credentials 18897 * first, the rest is executed via squeue. 18898 */ 18899 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18900 iocp->ioc_error = EPERM; 18901 iocp->ioc_count = 0; 18902 mp->b_datap->db_type = M_IOCACK; 18903 qreply(q, mp); 18904 return; 18905 } 18906 output_proc = tcp_wput_ioctl; 18907 break; 18908 default: 18909 output_proc = tcp_wput_ioctl; 18910 break; 18911 } 18912 break; 18913 default: 18914 output_proc = tcp_wput_nondata; 18915 break; 18916 } 18917 18918 CONN_INC_REF(connp); 18919 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18920 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18921 } 18922 18923 /* 18924 * Initial STREAMS write side put() procedure for sockets. It tries to 18925 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18926 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18927 * are handled by tcp_wput() as usual. 18928 * 18929 * All further messages will also be handled by tcp_wput() because we cannot 18930 * be sure that the above short cut is safe later. 18931 */ 18932 static void 18933 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18934 { 18935 conn_t *connp = Q_TO_CONN(wq); 18936 tcp_t *tcp = connp->conn_tcp; 18937 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18938 18939 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18940 wq->q_qinfo = &tcp_winit; 18941 18942 ASSERT(IPCL_IS_TCP(connp)); 18943 ASSERT(TCP_IS_SOCKET(tcp)); 18944 18945 if (DB_TYPE(mp) == M_PCPROTO && 18946 MBLKL(mp) == sizeof (struct T_capability_req) && 18947 car->PRIM_type == T_CAPABILITY_REQ) { 18948 tcp_capability_req(tcp, mp); 18949 return; 18950 } 18951 18952 tcp_wput(wq, mp); 18953 } 18954 18955 /* ARGSUSED */ 18956 static void 18957 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18958 { 18959 #ifdef DEBUG 18960 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18961 #endif 18962 freemsg(mp); 18963 } 18964 18965 static boolean_t 18966 tcp_zcopy_check(tcp_t *tcp) 18967 { 18968 conn_t *connp = tcp->tcp_connp; 18969 ire_t *ire; 18970 boolean_t zc_enabled = B_FALSE; 18971 tcp_stack_t *tcps = tcp->tcp_tcps; 18972 18973 if (do_tcpzcopy == 2) 18974 zc_enabled = B_TRUE; 18975 else if (tcp->tcp_ipversion == IPV4_VERSION && 18976 IPCL_IS_CONNECTED(connp) && 18977 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18978 connp->conn_dontroute == 0 && 18979 !connp->conn_nexthop_set && 18980 connp->conn_outgoing_ill == NULL && 18981 do_tcpzcopy == 1) { 18982 /* 18983 * the checks above closely resemble the fast path checks 18984 * in tcp_send_data(). 18985 */ 18986 mutex_enter(&connp->conn_lock); 18987 ire = connp->conn_ire_cache; 18988 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18989 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18990 IRE_REFHOLD(ire); 18991 if (ire->ire_stq != NULL) { 18992 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18993 18994 zc_enabled = ill && (ill->ill_capabilities & 18995 ILL_CAPAB_ZEROCOPY) && 18996 (ill->ill_zerocopy_capab-> 18997 ill_zerocopy_flags != 0); 18998 } 18999 IRE_REFRELE(ire); 19000 } 19001 mutex_exit(&connp->conn_lock); 19002 } 19003 tcp->tcp_snd_zcopy_on = zc_enabled; 19004 if (!TCP_IS_DETACHED(tcp)) { 19005 if (zc_enabled) { 19006 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19007 ZCVMSAFE); 19008 TCP_STAT(tcps, tcp_zcopy_on); 19009 } else { 19010 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 19011 ZCVMUNSAFE); 19012 TCP_STAT(tcps, tcp_zcopy_off); 19013 } 19014 } 19015 return (zc_enabled); 19016 } 19017 19018 static mblk_t * 19019 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 19020 { 19021 tcp_stack_t *tcps = tcp->tcp_tcps; 19022 19023 if (do_tcpzcopy == 2) 19024 return (bp); 19025 else if (tcp->tcp_snd_zcopy_on) { 19026 tcp->tcp_snd_zcopy_on = B_FALSE; 19027 if (!TCP_IS_DETACHED(tcp)) { 19028 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 19029 ZCVMUNSAFE); 19030 TCP_STAT(tcps, tcp_zcopy_disable); 19031 } 19032 } 19033 return (tcp_zcopy_backoff(tcp, bp, 0)); 19034 } 19035 19036 /* 19037 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 19038 * the original desballoca'ed segmapped mblk. 19039 */ 19040 static mblk_t * 19041 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 19042 { 19043 mblk_t *head, *tail, *nbp; 19044 tcp_stack_t *tcps = tcp->tcp_tcps; 19045 19046 if (IS_VMLOANED_MBLK(bp)) { 19047 TCP_STAT(tcps, tcp_zcopy_backoff); 19048 if ((head = copyb(bp)) == NULL) { 19049 /* fail to backoff; leave it for the next backoff */ 19050 tcp->tcp_xmit_zc_clean = B_FALSE; 19051 return (bp); 19052 } 19053 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19054 if (fix_xmitlist) 19055 tcp_zcopy_notify(tcp); 19056 else 19057 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19058 } 19059 nbp = bp->b_cont; 19060 if (fix_xmitlist) { 19061 head->b_prev = bp->b_prev; 19062 head->b_next = bp->b_next; 19063 if (tcp->tcp_xmit_tail == bp) 19064 tcp->tcp_xmit_tail = head; 19065 } 19066 bp->b_next = NULL; 19067 bp->b_prev = NULL; 19068 freeb(bp); 19069 } else { 19070 head = bp; 19071 nbp = bp->b_cont; 19072 } 19073 tail = head; 19074 while (nbp) { 19075 if (IS_VMLOANED_MBLK(nbp)) { 19076 TCP_STAT(tcps, tcp_zcopy_backoff); 19077 if ((tail->b_cont = copyb(nbp)) == NULL) { 19078 tcp->tcp_xmit_zc_clean = B_FALSE; 19079 tail->b_cont = nbp; 19080 return (head); 19081 } 19082 tail = tail->b_cont; 19083 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 19084 if (fix_xmitlist) 19085 tcp_zcopy_notify(tcp); 19086 else 19087 tail->b_datap->db_struioflag |= 19088 STRUIO_ZCNOTIFY; 19089 } 19090 bp = nbp; 19091 nbp = nbp->b_cont; 19092 if (fix_xmitlist) { 19093 tail->b_prev = bp->b_prev; 19094 tail->b_next = bp->b_next; 19095 if (tcp->tcp_xmit_tail == bp) 19096 tcp->tcp_xmit_tail = tail; 19097 } 19098 bp->b_next = NULL; 19099 bp->b_prev = NULL; 19100 freeb(bp); 19101 } else { 19102 tail->b_cont = nbp; 19103 tail = nbp; 19104 nbp = nbp->b_cont; 19105 } 19106 } 19107 if (fix_xmitlist) { 19108 tcp->tcp_xmit_last = tail; 19109 tcp->tcp_xmit_zc_clean = B_TRUE; 19110 } 19111 return (head); 19112 } 19113 19114 static void 19115 tcp_zcopy_notify(tcp_t *tcp) 19116 { 19117 struct stdata *stp; 19118 conn_t *connp; 19119 19120 if (tcp->tcp_detached) 19121 return; 19122 connp = tcp->tcp_connp; 19123 if (IPCL_IS_NONSTR(connp)) { 19124 (*connp->conn_upcalls->su_zcopy_notify) 19125 (connp->conn_upper_handle); 19126 return; 19127 } 19128 stp = STREAM(tcp->tcp_rq); 19129 mutex_enter(&stp->sd_lock); 19130 stp->sd_flag |= STZCNOTIFY; 19131 cv_broadcast(&stp->sd_zcopy_wait); 19132 mutex_exit(&stp->sd_lock); 19133 } 19134 19135 static boolean_t 19136 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19137 { 19138 ire_t *ire; 19139 conn_t *connp = tcp->tcp_connp; 19140 tcp_stack_t *tcps = tcp->tcp_tcps; 19141 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19142 19143 mutex_enter(&connp->conn_lock); 19144 ire = connp->conn_ire_cache; 19145 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19146 19147 if ((ire != NULL) && 19148 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19149 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19150 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19151 IRE_REFHOLD(ire); 19152 mutex_exit(&connp->conn_lock); 19153 } else { 19154 boolean_t cached = B_FALSE; 19155 ts_label_t *tsl; 19156 19157 /* force a recheck later on */ 19158 tcp->tcp_ire_ill_check_done = B_FALSE; 19159 19160 TCP_DBGSTAT(tcps, tcp_ire_null1); 19161 connp->conn_ire_cache = NULL; 19162 mutex_exit(&connp->conn_lock); 19163 19164 if (ire != NULL) 19165 IRE_REFRELE_NOTR(ire); 19166 19167 tsl = crgetlabel(CONN_CRED(connp)); 19168 ire = (dst ? 19169 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19170 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19171 connp->conn_zoneid, tsl, ipst)); 19172 19173 if (ire == NULL) { 19174 TCP_STAT(tcps, tcp_ire_null); 19175 return (B_FALSE); 19176 } 19177 19178 IRE_REFHOLD_NOTR(ire); 19179 19180 mutex_enter(&connp->conn_lock); 19181 if (CONN_CACHE_IRE(connp)) { 19182 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19183 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19184 TCP_CHECK_IREINFO(tcp, ire); 19185 connp->conn_ire_cache = ire; 19186 cached = B_TRUE; 19187 } 19188 rw_exit(&ire->ire_bucket->irb_lock); 19189 } 19190 mutex_exit(&connp->conn_lock); 19191 19192 /* 19193 * We can continue to use the ire but since it was 19194 * not cached, we should drop the extra reference. 19195 */ 19196 if (!cached) 19197 IRE_REFRELE_NOTR(ire); 19198 19199 /* 19200 * Rampart note: no need to select a new label here, since 19201 * labels are not allowed to change during the life of a TCP 19202 * connection. 19203 */ 19204 } 19205 19206 *irep = ire; 19207 19208 return (B_TRUE); 19209 } 19210 19211 /* 19212 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19213 * 19214 * 0 = success; 19215 * 1 = failed to find ire and ill. 19216 */ 19217 static boolean_t 19218 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19219 { 19220 ipha_t *ipha; 19221 ipaddr_t dst; 19222 ire_t *ire; 19223 ill_t *ill; 19224 mblk_t *ire_fp_mp; 19225 tcp_stack_t *tcps = tcp->tcp_tcps; 19226 19227 if (mp != NULL) 19228 ipha = (ipha_t *)mp->b_rptr; 19229 else 19230 ipha = tcp->tcp_ipha; 19231 dst = ipha->ipha_dst; 19232 19233 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19234 return (B_FALSE); 19235 19236 if ((ire->ire_flags & RTF_MULTIRT) || 19237 (ire->ire_stq == NULL) || 19238 (ire->ire_nce == NULL) || 19239 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19240 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19241 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19242 TCP_STAT(tcps, tcp_ip_ire_send); 19243 IRE_REFRELE(ire); 19244 return (B_FALSE); 19245 } 19246 19247 ill = ire_to_ill(ire); 19248 ASSERT(ill != NULL); 19249 19250 if (!tcp->tcp_ire_ill_check_done) { 19251 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19252 tcp->tcp_ire_ill_check_done = B_TRUE; 19253 } 19254 19255 *irep = ire; 19256 *illp = ill; 19257 19258 return (B_TRUE); 19259 } 19260 19261 static void 19262 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19263 { 19264 ipha_t *ipha; 19265 ipaddr_t src; 19266 ipaddr_t dst; 19267 uint32_t cksum; 19268 ire_t *ire; 19269 uint16_t *up; 19270 ill_t *ill; 19271 conn_t *connp = tcp->tcp_connp; 19272 uint32_t hcksum_txflags = 0; 19273 mblk_t *ire_fp_mp; 19274 uint_t ire_fp_mp_len; 19275 tcp_stack_t *tcps = tcp->tcp_tcps; 19276 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19277 cred_t *cr; 19278 pid_t cpid; 19279 19280 ASSERT(DB_TYPE(mp) == M_DATA); 19281 19282 /* 19283 * Here we need to handle the overloading of the cred_t for 19284 * both getpeerucred and TX. 19285 * If this is a SYN then the caller already set db_credp so 19286 * that getpeerucred will work. But if TX is in use we might have 19287 * a conn_peercred which is different, and we need to use that cred 19288 * to make TX use the correct label and label dependent route. 19289 */ 19290 if (is_system_labeled()) { 19291 cr = msg_getcred(mp, &cpid); 19292 if (cr == NULL || connp->conn_peercred != NULL) 19293 mblk_setcred(mp, CONN_CRED(connp), cpid); 19294 } 19295 19296 ipha = (ipha_t *)mp->b_rptr; 19297 src = ipha->ipha_src; 19298 dst = ipha->ipha_dst; 19299 19300 ASSERT(q != NULL); 19301 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19302 19303 /* 19304 * Drop off fast path for IPv6 and also if options are present or 19305 * we need to resolve a TS label. 19306 */ 19307 if (tcp->tcp_ipversion != IPV4_VERSION || 19308 !IPCL_IS_CONNECTED(connp) || 19309 !CONN_IS_LSO_MD_FASTPATH(connp) || 19310 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19311 !connp->conn_ulp_labeled || 19312 ipha->ipha_ident == IP_HDR_INCLUDED || 19313 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19314 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19315 if (tcp->tcp_snd_zcopy_aware) 19316 mp = tcp_zcopy_disable(tcp, mp); 19317 TCP_STAT(tcps, tcp_ip_send); 19318 CALL_IP_WPUT(connp, q, mp); 19319 return; 19320 } 19321 19322 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19323 if (tcp->tcp_snd_zcopy_aware) 19324 mp = tcp_zcopy_backoff(tcp, mp, 0); 19325 CALL_IP_WPUT(connp, q, mp); 19326 return; 19327 } 19328 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19329 ire_fp_mp_len = MBLKL(ire_fp_mp); 19330 19331 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19332 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19333 #ifndef _BIG_ENDIAN 19334 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19335 #endif 19336 19337 /* 19338 * Check to see if we need to re-enable LSO/MDT for this connection 19339 * because it was previously disabled due to changes in the ill; 19340 * note that by doing it here, this re-enabling only applies when 19341 * the packet is not dispatched through CALL_IP_WPUT(). 19342 * 19343 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19344 * case, since that's how we ended up here. For IPv6, we do the 19345 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19346 */ 19347 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19348 /* 19349 * Restore LSO for this connection, so that next time around 19350 * it is eligible to go through tcp_lsosend() path again. 19351 */ 19352 TCP_STAT(tcps, tcp_lso_enabled); 19353 tcp->tcp_lso = B_TRUE; 19354 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19355 "interface %s\n", (void *)connp, ill->ill_name)); 19356 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19357 /* 19358 * Restore MDT for this connection, so that next time around 19359 * it is eligible to go through tcp_multisend() path again. 19360 */ 19361 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19362 tcp->tcp_mdt = B_TRUE; 19363 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19364 "interface %s\n", (void *)connp, ill->ill_name)); 19365 } 19366 19367 if (tcp->tcp_snd_zcopy_aware) { 19368 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19369 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19370 mp = tcp_zcopy_disable(tcp, mp); 19371 /* 19372 * we shouldn't need to reset ipha as the mp containing 19373 * ipha should never be a zero-copy mp. 19374 */ 19375 } 19376 19377 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19378 ASSERT(ill->ill_hcksum_capab != NULL); 19379 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19380 } 19381 19382 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19383 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19384 19385 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19386 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19387 19388 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19389 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19390 19391 /* Software checksum? */ 19392 if (DB_CKSUMFLAGS(mp) == 0) { 19393 TCP_STAT(tcps, tcp_out_sw_cksum); 19394 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19395 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19396 } 19397 19398 /* Calculate IP header checksum if hardware isn't capable */ 19399 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19400 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19401 ((uint16_t *)ipha)[4]); 19402 } 19403 19404 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19405 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19406 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19407 19408 UPDATE_OB_PKT_COUNT(ire); 19409 ire->ire_last_used_time = lbolt; 19410 19411 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19412 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19413 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19414 ntohs(ipha->ipha_length)); 19415 19416 DTRACE_PROBE4(ip4__physical__out__start, 19417 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 19418 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19419 ipst->ips_ipv4firewall_physical_out, 19420 NULL, ill, ipha, mp, mp, 0, ipst); 19421 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19422 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 19423 19424 if (mp != NULL) { 19425 if (ipst->ips_ipobs_enabled) { 19426 zoneid_t szone; 19427 19428 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 19429 ipst, ALL_ZONES); 19430 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 19431 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 19432 } 19433 19434 ILL_SEND_TX(ill, ire, connp, mp, 0); 19435 } 19436 19437 IRE_REFRELE(ire); 19438 } 19439 19440 /* 19441 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19442 * if the receiver shrinks the window, i.e. moves the right window to the 19443 * left, the we should not send new data, but should retransmit normally the 19444 * old unacked data between suna and suna + swnd. We might has sent data 19445 * that is now outside the new window, pretend that we didn't send it. 19446 */ 19447 static void 19448 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19449 { 19450 uint32_t snxt = tcp->tcp_snxt; 19451 mblk_t *xmit_tail; 19452 int32_t offset; 19453 19454 ASSERT(shrunk_count > 0); 19455 19456 /* Pretend we didn't send the data outside the window */ 19457 snxt -= shrunk_count; 19458 19459 /* Get the mblk and the offset in it per the shrunk window */ 19460 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19461 19462 ASSERT(xmit_tail != NULL); 19463 19464 /* Reset all the values per the now shrunk window */ 19465 tcp->tcp_snxt = snxt; 19466 tcp->tcp_xmit_tail = xmit_tail; 19467 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19468 offset; 19469 tcp->tcp_unsent += shrunk_count; 19470 19471 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19472 /* 19473 * Make sure the timer is running so that we will probe a zero 19474 * window. 19475 */ 19476 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19477 } 19478 19479 19480 /* 19481 * The TCP normal data output path. 19482 * NOTE: the logic of the fast path is duplicated from this function. 19483 */ 19484 static void 19485 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19486 { 19487 int len; 19488 mblk_t *local_time; 19489 mblk_t *mp1; 19490 uint32_t snxt; 19491 int tail_unsent; 19492 int tcpstate; 19493 int usable = 0; 19494 mblk_t *xmit_tail; 19495 queue_t *q = tcp->tcp_wq; 19496 int32_t mss; 19497 int32_t num_sack_blk = 0; 19498 int32_t tcp_hdr_len; 19499 int32_t tcp_tcp_hdr_len; 19500 int mdt_thres; 19501 int rc; 19502 tcp_stack_t *tcps = tcp->tcp_tcps; 19503 ip_stack_t *ipst; 19504 19505 tcpstate = tcp->tcp_state; 19506 if (mp == NULL) { 19507 /* 19508 * tcp_wput_data() with NULL mp should only be called when 19509 * there is unsent data. 19510 */ 19511 ASSERT(tcp->tcp_unsent > 0); 19512 /* Really tacky... but we need this for detached closes. */ 19513 len = tcp->tcp_unsent; 19514 goto data_null; 19515 } 19516 19517 #if CCS_STATS 19518 wrw_stats.tot.count++; 19519 wrw_stats.tot.bytes += msgdsize(mp); 19520 #endif 19521 ASSERT(mp->b_datap->db_type == M_DATA); 19522 /* 19523 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19524 * or before a connection attempt has begun. 19525 */ 19526 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19527 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19528 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19529 #ifdef DEBUG 19530 cmn_err(CE_WARN, 19531 "tcp_wput_data: data after ordrel, %s", 19532 tcp_display(tcp, NULL, 19533 DISP_ADDR_AND_PORT)); 19534 #else 19535 if (tcp->tcp_debug) { 19536 (void) strlog(TCP_MOD_ID, 0, 1, 19537 SL_TRACE|SL_ERROR, 19538 "tcp_wput_data: data after ordrel, %s\n", 19539 tcp_display(tcp, NULL, 19540 DISP_ADDR_AND_PORT)); 19541 } 19542 #endif /* DEBUG */ 19543 } 19544 if (tcp->tcp_snd_zcopy_aware && 19545 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19546 tcp_zcopy_notify(tcp); 19547 freemsg(mp); 19548 mutex_enter(&tcp->tcp_non_sq_lock); 19549 if (tcp->tcp_flow_stopped && 19550 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19551 tcp_clrqfull(tcp); 19552 } 19553 mutex_exit(&tcp->tcp_non_sq_lock); 19554 return; 19555 } 19556 19557 /* Strip empties */ 19558 for (;;) { 19559 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19560 (uintptr_t)INT_MAX); 19561 len = (int)(mp->b_wptr - mp->b_rptr); 19562 if (len > 0) 19563 break; 19564 mp1 = mp; 19565 mp = mp->b_cont; 19566 freeb(mp1); 19567 if (!mp) { 19568 return; 19569 } 19570 } 19571 19572 /* If we are the first on the list ... */ 19573 if (tcp->tcp_xmit_head == NULL) { 19574 tcp->tcp_xmit_head = mp; 19575 tcp->tcp_xmit_tail = mp; 19576 tcp->tcp_xmit_tail_unsent = len; 19577 } else { 19578 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19579 struct datab *dp; 19580 19581 mp1 = tcp->tcp_xmit_last; 19582 if (len < tcp_tx_pull_len && 19583 (dp = mp1->b_datap)->db_ref == 1 && 19584 dp->db_lim - mp1->b_wptr >= len) { 19585 ASSERT(len > 0); 19586 ASSERT(!mp1->b_cont); 19587 if (len == 1) { 19588 *mp1->b_wptr++ = *mp->b_rptr; 19589 } else { 19590 bcopy(mp->b_rptr, mp1->b_wptr, len); 19591 mp1->b_wptr += len; 19592 } 19593 if (mp1 == tcp->tcp_xmit_tail) 19594 tcp->tcp_xmit_tail_unsent += len; 19595 mp1->b_cont = mp->b_cont; 19596 if (tcp->tcp_snd_zcopy_aware && 19597 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19598 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19599 freeb(mp); 19600 mp = mp1; 19601 } else { 19602 tcp->tcp_xmit_last->b_cont = mp; 19603 } 19604 len += tcp->tcp_unsent; 19605 } 19606 19607 /* Tack on however many more positive length mblks we have */ 19608 if ((mp1 = mp->b_cont) != NULL) { 19609 do { 19610 int tlen; 19611 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19612 (uintptr_t)INT_MAX); 19613 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19614 if (tlen <= 0) { 19615 mp->b_cont = mp1->b_cont; 19616 freeb(mp1); 19617 } else { 19618 len += tlen; 19619 mp = mp1; 19620 } 19621 } while ((mp1 = mp->b_cont) != NULL); 19622 } 19623 tcp->tcp_xmit_last = mp; 19624 tcp->tcp_unsent = len; 19625 19626 if (urgent) 19627 usable = 1; 19628 19629 data_null: 19630 snxt = tcp->tcp_snxt; 19631 xmit_tail = tcp->tcp_xmit_tail; 19632 tail_unsent = tcp->tcp_xmit_tail_unsent; 19633 19634 /* 19635 * Note that tcp_mss has been adjusted to take into account the 19636 * timestamp option if applicable. Because SACK options do not 19637 * appear in every TCP segments and they are of variable lengths, 19638 * they cannot be included in tcp_mss. Thus we need to calculate 19639 * the actual segment length when we need to send a segment which 19640 * includes SACK options. 19641 */ 19642 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19643 int32_t opt_len; 19644 19645 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19646 tcp->tcp_num_sack_blk); 19647 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19648 2 + TCPOPT_HEADER_LEN; 19649 mss = tcp->tcp_mss - opt_len; 19650 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19651 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19652 } else { 19653 mss = tcp->tcp_mss; 19654 tcp_hdr_len = tcp->tcp_hdr_len; 19655 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19656 } 19657 19658 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19659 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19660 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19661 } 19662 if (tcpstate == TCPS_SYN_RCVD) { 19663 /* 19664 * The three-way connection establishment handshake is not 19665 * complete yet. We want to queue the data for transmission 19666 * after entering ESTABLISHED state (RFC793). A jump to 19667 * "done" label effectively leaves data on the queue. 19668 */ 19669 goto done; 19670 } else { 19671 int usable_r; 19672 19673 /* 19674 * In the special case when cwnd is zero, which can only 19675 * happen if the connection is ECN capable, return now. 19676 * New segments is sent using tcp_timer(). The timer 19677 * is set in tcp_rput_data(). 19678 */ 19679 if (tcp->tcp_cwnd == 0) { 19680 /* 19681 * Note that tcp_cwnd is 0 before 3-way handshake is 19682 * finished. 19683 */ 19684 ASSERT(tcp->tcp_ecn_ok || 19685 tcp->tcp_state < TCPS_ESTABLISHED); 19686 return; 19687 } 19688 19689 /* NOTE: trouble if xmitting while SYN not acked? */ 19690 usable_r = snxt - tcp->tcp_suna; 19691 usable_r = tcp->tcp_swnd - usable_r; 19692 19693 /* 19694 * Check if the receiver has shrunk the window. If 19695 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19696 * cannot be set as there is unsent data, so FIN cannot 19697 * be sent out. Otherwise, we need to take into account 19698 * of FIN as it consumes an "invisible" sequence number. 19699 */ 19700 ASSERT(tcp->tcp_fin_sent == 0); 19701 if (usable_r < 0) { 19702 /* 19703 * The receiver has shrunk the window and we have sent 19704 * -usable_r date beyond the window, re-adjust. 19705 * 19706 * If TCP window scaling is enabled, there can be 19707 * round down error as the advertised receive window 19708 * is actually right shifted n bits. This means that 19709 * the lower n bits info is wiped out. It will look 19710 * like the window is shrunk. Do a check here to 19711 * see if the shrunk amount is actually within the 19712 * error in window calculation. If it is, just 19713 * return. Note that this check is inside the 19714 * shrunk window check. This makes sure that even 19715 * though tcp_process_shrunk_swnd() is not called, 19716 * we will stop further processing. 19717 */ 19718 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19719 tcp_process_shrunk_swnd(tcp, -usable_r); 19720 } 19721 return; 19722 } 19723 19724 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19725 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19726 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19727 19728 /* usable = MIN(usable, unsent) */ 19729 if (usable_r > len) 19730 usable_r = len; 19731 19732 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19733 if (usable_r > 0) { 19734 usable = usable_r; 19735 } else { 19736 /* Bypass all other unnecessary processing. */ 19737 goto done; 19738 } 19739 } 19740 19741 local_time = (mblk_t *)lbolt; 19742 19743 /* 19744 * "Our" Nagle Algorithm. This is not the same as in the old 19745 * BSD. This is more in line with the true intent of Nagle. 19746 * 19747 * The conditions are: 19748 * 1. The amount of unsent data (or amount of data which can be 19749 * sent, whichever is smaller) is less than Nagle limit. 19750 * 2. The last sent size is also less than Nagle limit. 19751 * 3. There is unack'ed data. 19752 * 4. Urgent pointer is not set. Send urgent data ignoring the 19753 * Nagle algorithm. This reduces the probability that urgent 19754 * bytes get "merged" together. 19755 * 5. The app has not closed the connection. This eliminates the 19756 * wait time of the receiving side waiting for the last piece of 19757 * (small) data. 19758 * 19759 * If all are satisified, exit without sending anything. Note 19760 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19761 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19762 * 4095). 19763 */ 19764 if (usable < (int)tcp->tcp_naglim && 19765 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19766 snxt != tcp->tcp_suna && 19767 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19768 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19769 goto done; 19770 } 19771 19772 if (tcp->tcp_cork) { 19773 /* 19774 * if the tcp->tcp_cork option is set, then we have to force 19775 * TCP not to send partial segment (smaller than MSS bytes). 19776 * We are calculating the usable now based on full mss and 19777 * will save the rest of remaining data for later. 19778 */ 19779 if (usable < mss) 19780 goto done; 19781 usable = (usable / mss) * mss; 19782 } 19783 19784 /* Update the latest receive window size in TCP header. */ 19785 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19786 tcp->tcp_tcph->th_win); 19787 19788 /* 19789 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19790 * 19791 * 1. Simple TCP/IP{v4,v6} (no options). 19792 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19793 * 3. If the TCP connection is in ESTABLISHED state. 19794 * 4. The TCP is not detached. 19795 * 19796 * If any of the above conditions have changed during the 19797 * connection, stop using LSO/MDT and restore the stream head 19798 * parameters accordingly. 19799 */ 19800 ipst = tcps->tcps_netstack->netstack_ip; 19801 19802 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19803 ((tcp->tcp_ipversion == IPV4_VERSION && 19804 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19805 (tcp->tcp_ipversion == IPV6_VERSION && 19806 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19807 tcp->tcp_state != TCPS_ESTABLISHED || 19808 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19809 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19810 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19811 if (tcp->tcp_lso) { 19812 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19813 tcp->tcp_lso = B_FALSE; 19814 } else { 19815 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19816 tcp->tcp_mdt = B_FALSE; 19817 } 19818 19819 /* Anything other than detached is considered pathological */ 19820 if (!TCP_IS_DETACHED(tcp)) { 19821 if (tcp->tcp_lso) 19822 TCP_STAT(tcps, tcp_lso_disabled); 19823 else 19824 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19825 (void) tcp_maxpsz_set(tcp, B_TRUE); 19826 } 19827 } 19828 19829 /* Use MDT if sendable amount is greater than the threshold */ 19830 if (tcp->tcp_mdt && 19831 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19832 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19833 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19834 (tcp->tcp_valid_bits == 0 || 19835 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19836 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19837 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19838 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19839 local_time, mdt_thres); 19840 } else { 19841 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19842 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19843 local_time, INT_MAX); 19844 } 19845 19846 /* Pretend that all we were trying to send really got sent */ 19847 if (rc < 0 && tail_unsent < 0) { 19848 do { 19849 xmit_tail = xmit_tail->b_cont; 19850 xmit_tail->b_prev = local_time; 19851 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19852 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19853 tail_unsent += (int)(xmit_tail->b_wptr - 19854 xmit_tail->b_rptr); 19855 } while (tail_unsent < 0); 19856 } 19857 done:; 19858 tcp->tcp_xmit_tail = xmit_tail; 19859 tcp->tcp_xmit_tail_unsent = tail_unsent; 19860 len = tcp->tcp_snxt - snxt; 19861 if (len) { 19862 /* 19863 * If new data was sent, need to update the notsack 19864 * list, which is, afterall, data blocks that have 19865 * not been sack'ed by the receiver. New data is 19866 * not sack'ed. 19867 */ 19868 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19869 /* len is a negative value. */ 19870 tcp->tcp_pipe -= len; 19871 tcp_notsack_update(&(tcp->tcp_notsack_list), 19872 tcp->tcp_snxt, snxt, 19873 &(tcp->tcp_num_notsack_blk), 19874 &(tcp->tcp_cnt_notsack_list)); 19875 } 19876 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19877 tcp->tcp_rack = tcp->tcp_rnxt; 19878 tcp->tcp_rack_cnt = 0; 19879 if ((snxt + len) == tcp->tcp_suna) { 19880 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19881 } 19882 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19883 /* 19884 * Didn't send anything. Make sure the timer is running 19885 * so that we will probe a zero window. 19886 */ 19887 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19888 } 19889 /* Note that len is the amount we just sent but with a negative sign */ 19890 tcp->tcp_unsent += len; 19891 mutex_enter(&tcp->tcp_non_sq_lock); 19892 if (tcp->tcp_flow_stopped) { 19893 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19894 tcp_clrqfull(tcp); 19895 } 19896 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19897 tcp_setqfull(tcp); 19898 } 19899 mutex_exit(&tcp->tcp_non_sq_lock); 19900 } 19901 19902 /* 19903 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19904 * outgoing TCP header with the template header, as well as other 19905 * options such as time-stamp, ECN and/or SACK. 19906 */ 19907 static void 19908 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19909 { 19910 tcph_t *tcp_tmpl, *tcp_h; 19911 uint32_t *dst, *src; 19912 int hdrlen; 19913 19914 ASSERT(OK_32PTR(rptr)); 19915 19916 /* Template header */ 19917 tcp_tmpl = tcp->tcp_tcph; 19918 19919 /* Header of outgoing packet */ 19920 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19921 19922 /* dst and src are opaque 32-bit fields, used for copying */ 19923 dst = (uint32_t *)rptr; 19924 src = (uint32_t *)tcp->tcp_iphc; 19925 hdrlen = tcp->tcp_hdr_len; 19926 19927 /* Fill time-stamp option if needed */ 19928 if (tcp->tcp_snd_ts_ok) { 19929 U32_TO_BE32((uint32_t)now, 19930 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19931 U32_TO_BE32(tcp->tcp_ts_recent, 19932 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19933 } else { 19934 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19935 } 19936 19937 /* 19938 * Copy the template header; is this really more efficient than 19939 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19940 * but perhaps not for other scenarios. 19941 */ 19942 dst[0] = src[0]; 19943 dst[1] = src[1]; 19944 dst[2] = src[2]; 19945 dst[3] = src[3]; 19946 dst[4] = src[4]; 19947 dst[5] = src[5]; 19948 dst[6] = src[6]; 19949 dst[7] = src[7]; 19950 dst[8] = src[8]; 19951 dst[9] = src[9]; 19952 if (hdrlen -= 40) { 19953 hdrlen >>= 2; 19954 dst += 10; 19955 src += 10; 19956 do { 19957 *dst++ = *src++; 19958 } while (--hdrlen); 19959 } 19960 19961 /* 19962 * Set the ECN info in the TCP header if it is not a zero 19963 * window probe. Zero window probe is only sent in 19964 * tcp_wput_data() and tcp_timer(). 19965 */ 19966 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19967 SET_ECT(tcp, rptr); 19968 19969 if (tcp->tcp_ecn_echo_on) 19970 tcp_h->th_flags[0] |= TH_ECE; 19971 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19972 tcp_h->th_flags[0] |= TH_CWR; 19973 tcp->tcp_ecn_cwr_sent = B_TRUE; 19974 } 19975 } 19976 19977 /* Fill in SACK options */ 19978 if (num_sack_blk > 0) { 19979 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19980 sack_blk_t *tmp; 19981 int32_t i; 19982 19983 wptr[0] = TCPOPT_NOP; 19984 wptr[1] = TCPOPT_NOP; 19985 wptr[2] = TCPOPT_SACK; 19986 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19987 sizeof (sack_blk_t); 19988 wptr += TCPOPT_REAL_SACK_LEN; 19989 19990 tmp = tcp->tcp_sack_list; 19991 for (i = 0; i < num_sack_blk; i++) { 19992 U32_TO_BE32(tmp[i].begin, wptr); 19993 wptr += sizeof (tcp_seq); 19994 U32_TO_BE32(tmp[i].end, wptr); 19995 wptr += sizeof (tcp_seq); 19996 } 19997 tcp_h->th_offset_and_rsrvd[0] += 19998 ((num_sack_blk * 2 + 1) << 4); 19999 } 20000 } 20001 20002 /* 20003 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 20004 * the destination address and SAP attribute, and if necessary, the 20005 * hardware checksum offload attribute to a Multidata message. 20006 */ 20007 static int 20008 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 20009 const uint32_t start, const uint32_t stuff, const uint32_t end, 20010 const uint32_t flags, tcp_stack_t *tcps) 20011 { 20012 /* Add global destination address & SAP attribute */ 20013 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 20014 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 20015 "destination address+SAP\n")); 20016 20017 if (dlmp != NULL) 20018 TCP_STAT(tcps, tcp_mdt_allocfail); 20019 return (-1); 20020 } 20021 20022 /* Add global hwcksum attribute */ 20023 if (hwcksum && 20024 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 20025 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 20026 "checksum attribute\n")); 20027 20028 TCP_STAT(tcps, tcp_mdt_allocfail); 20029 return (-1); 20030 } 20031 20032 return (0); 20033 } 20034 20035 /* 20036 * Smaller and private version of pdescinfo_t used specifically for TCP, 20037 * which allows for only two payload spans per packet. 20038 */ 20039 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 20040 20041 /* 20042 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 20043 * scheme, and returns one the following: 20044 * 20045 * -1 = failed allocation. 20046 * 0 = success; burst count reached, or usable send window is too small, 20047 * and that we'd rather wait until later before sending again. 20048 */ 20049 static int 20050 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20051 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20052 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20053 const int mdt_thres) 20054 { 20055 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 20056 multidata_t *mmd; 20057 uint_t obsegs, obbytes, hdr_frag_sz; 20058 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 20059 int num_burst_seg, max_pld; 20060 pdesc_t *pkt; 20061 tcp_pdescinfo_t tcp_pkt_info; 20062 pdescinfo_t *pkt_info; 20063 int pbuf_idx, pbuf_idx_nxt; 20064 int seg_len, len, spill, af; 20065 boolean_t add_buffer, zcopy, clusterwide; 20066 boolean_t rconfirm = B_FALSE; 20067 boolean_t done = B_FALSE; 20068 uint32_t cksum; 20069 uint32_t hwcksum_flags; 20070 ire_t *ire = NULL; 20071 ill_t *ill; 20072 ipha_t *ipha; 20073 ip6_t *ip6h; 20074 ipaddr_t src, dst; 20075 ill_zerocopy_capab_t *zc_cap = NULL; 20076 uint16_t *up; 20077 int err; 20078 conn_t *connp; 20079 tcp_stack_t *tcps = tcp->tcp_tcps; 20080 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20081 int usable_mmd, tail_unsent_mmd; 20082 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 20083 mblk_t *xmit_tail_mmd; 20084 netstackid_t stack_id; 20085 20086 #ifdef _BIG_ENDIAN 20087 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 20088 #else 20089 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 20090 #endif 20091 20092 #define PREP_NEW_MULTIDATA() { \ 20093 mmd = NULL; \ 20094 md_mp = md_hbuf = NULL; \ 20095 cur_hdr_off = 0; \ 20096 max_pld = tcp->tcp_mdt_max_pld; \ 20097 pbuf_idx = pbuf_idx_nxt = -1; \ 20098 add_buffer = B_TRUE; \ 20099 zcopy = B_FALSE; \ 20100 } 20101 20102 #define PREP_NEW_PBUF() { \ 20103 md_pbuf = md_pbuf_nxt = NULL; \ 20104 pbuf_idx = pbuf_idx_nxt = -1; \ 20105 cur_pld_off = 0; \ 20106 first_snxt = *snxt; \ 20107 ASSERT(*tail_unsent > 0); \ 20108 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20109 } 20110 20111 ASSERT(mdt_thres >= mss); 20112 ASSERT(*usable > 0 && *usable > mdt_thres); 20113 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20114 ASSERT(!TCP_IS_DETACHED(tcp)); 20115 ASSERT(tcp->tcp_valid_bits == 0 || 20116 tcp->tcp_valid_bits == TCP_FSS_VALID); 20117 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20118 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20119 (tcp->tcp_ipversion == IPV6_VERSION && 20120 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20121 20122 connp = tcp->tcp_connp; 20123 ASSERT(connp != NULL); 20124 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20125 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20126 20127 stack_id = connp->conn_netstack->netstack_stackid; 20128 20129 usable_mmd = tail_unsent_mmd = 0; 20130 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20131 xmit_tail_mmd = NULL; 20132 /* 20133 * Note that tcp will only declare at most 2 payload spans per 20134 * packet, which is much lower than the maximum allowable number 20135 * of packet spans per Multidata. For this reason, we use the 20136 * privately declared and smaller descriptor info structure, in 20137 * order to save some stack space. 20138 */ 20139 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20140 20141 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20142 if (af == AF_INET) { 20143 dst = tcp->tcp_ipha->ipha_dst; 20144 src = tcp->tcp_ipha->ipha_src; 20145 ASSERT(!CLASSD(dst)); 20146 } 20147 ASSERT(af == AF_INET || 20148 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20149 20150 obsegs = obbytes = 0; 20151 num_burst_seg = tcp->tcp_snd_burst; 20152 md_mp_head = NULL; 20153 PREP_NEW_MULTIDATA(); 20154 20155 /* 20156 * Before we go on further, make sure there is an IRE that we can 20157 * use, and that the ILL supports MDT. Otherwise, there's no point 20158 * in proceeding any further, and we should just hand everything 20159 * off to the legacy path. 20160 */ 20161 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20162 goto legacy_send_no_md; 20163 20164 ASSERT(ire != NULL); 20165 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20166 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20167 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20168 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20169 /* 20170 * If we do support loopback for MDT (which requires modifications 20171 * to the receiving paths), the following assertions should go away, 20172 * and we would be sending the Multidata to loopback conn later on. 20173 */ 20174 ASSERT(!IRE_IS_LOCAL(ire)); 20175 ASSERT(ire->ire_stq != NULL); 20176 20177 ill = ire_to_ill(ire); 20178 ASSERT(ill != NULL); 20179 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20180 20181 if (!tcp->tcp_ire_ill_check_done) { 20182 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20183 tcp->tcp_ire_ill_check_done = B_TRUE; 20184 } 20185 20186 /* 20187 * If the underlying interface conditions have changed, or if the 20188 * new interface does not support MDT, go back to legacy path. 20189 */ 20190 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20191 /* don't go through this path anymore for this connection */ 20192 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20193 tcp->tcp_mdt = B_FALSE; 20194 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20195 "interface %s\n", (void *)connp, ill->ill_name)); 20196 /* IRE will be released prior to returning */ 20197 goto legacy_send_no_md; 20198 } 20199 20200 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20201 zc_cap = ill->ill_zerocopy_capab; 20202 20203 /* 20204 * Check if we can take tcp fast-path. Note that "incomplete" 20205 * ire's (where the link-layer for next hop is not resolved 20206 * or where the fast-path header in nce_fp_mp is not available 20207 * yet) are sent down the legacy (slow) path. 20208 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20209 */ 20210 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20211 /* IRE will be released prior to returning */ 20212 goto legacy_send_no_md; 20213 } 20214 20215 /* go to legacy path if interface doesn't support zerocopy */ 20216 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20217 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20218 /* IRE will be released prior to returning */ 20219 goto legacy_send_no_md; 20220 } 20221 20222 /* does the interface support hardware checksum offload? */ 20223 hwcksum_flags = 0; 20224 if (ILL_HCKSUM_CAPABLE(ill) && 20225 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20226 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20227 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20228 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20229 HCKSUM_IPHDRCKSUM) 20230 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20231 20232 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20233 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20234 hwcksum_flags |= HCK_FULLCKSUM; 20235 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20236 HCKSUM_INET_PARTIAL) 20237 hwcksum_flags |= HCK_PARTIALCKSUM; 20238 } 20239 20240 /* 20241 * Each header fragment consists of the leading extra space, 20242 * followed by the TCP/IP header, and the trailing extra space. 20243 * We make sure that each header fragment begins on a 32-bit 20244 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20245 * aligned in tcp_mdt_update). 20246 */ 20247 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20248 tcp->tcp_mdt_hdr_tail), 4); 20249 20250 /* are we starting from the beginning of data block? */ 20251 if (*tail_unsent == 0) { 20252 *xmit_tail = (*xmit_tail)->b_cont; 20253 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20254 *tail_unsent = (int)MBLKL(*xmit_tail); 20255 } 20256 20257 /* 20258 * Here we create one or more Multidata messages, each made up of 20259 * one header buffer and up to N payload buffers. This entire 20260 * operation is done within two loops: 20261 * 20262 * The outer loop mostly deals with creating the Multidata message, 20263 * as well as the header buffer that gets added to it. It also 20264 * links the Multidata messages together such that all of them can 20265 * be sent down to the lower layer in a single putnext call; this 20266 * linking behavior depends on the tcp_mdt_chain tunable. 20267 * 20268 * The inner loop takes an existing Multidata message, and adds 20269 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20270 * packetizes those buffers by filling up the corresponding header 20271 * buffer fragments with the proper IP and TCP headers, and by 20272 * describing the layout of each packet in the packet descriptors 20273 * that get added to the Multidata. 20274 */ 20275 do { 20276 /* 20277 * If usable send window is too small, or data blocks in 20278 * transmit list are smaller than our threshold (i.e. app 20279 * performs large writes followed by small ones), we hand 20280 * off the control over to the legacy path. Note that we'll 20281 * get back the control once it encounters a large block. 20282 */ 20283 if (*usable < mss || (*tail_unsent <= mdt_thres && 20284 (*xmit_tail)->b_cont != NULL && 20285 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20286 /* send down what we've got so far */ 20287 if (md_mp_head != NULL) { 20288 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20289 obsegs, obbytes, &rconfirm); 20290 } 20291 /* 20292 * Pass control over to tcp_send(), but tell it to 20293 * return to us once a large-size transmission is 20294 * possible. 20295 */ 20296 TCP_STAT(tcps, tcp_mdt_legacy_small); 20297 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20298 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20299 tail_unsent, xmit_tail, local_time, 20300 mdt_thres)) <= 0) { 20301 /* burst count reached, or alloc failed */ 20302 IRE_REFRELE(ire); 20303 return (err); 20304 } 20305 20306 /* tcp_send() may have sent everything, so check */ 20307 if (*usable <= 0) { 20308 IRE_REFRELE(ire); 20309 return (0); 20310 } 20311 20312 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20313 /* 20314 * We may have delivered the Multidata, so make sure 20315 * to re-initialize before the next round. 20316 */ 20317 md_mp_head = NULL; 20318 obsegs = obbytes = 0; 20319 num_burst_seg = tcp->tcp_snd_burst; 20320 PREP_NEW_MULTIDATA(); 20321 20322 /* are we starting from the beginning of data block? */ 20323 if (*tail_unsent == 0) { 20324 *xmit_tail = (*xmit_tail)->b_cont; 20325 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20326 (uintptr_t)INT_MAX); 20327 *tail_unsent = (int)MBLKL(*xmit_tail); 20328 } 20329 } 20330 /* 20331 * Record current values for parameters we may need to pass 20332 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20333 * each iteration of the outer loop (each multidata message 20334 * creation). If we have a failure in the inner loop, we send 20335 * any complete multidata messages we have before reverting 20336 * to using the traditional non-md path. 20337 */ 20338 snxt_mmd = *snxt; 20339 usable_mmd = *usable; 20340 xmit_tail_mmd = *xmit_tail; 20341 tail_unsent_mmd = *tail_unsent; 20342 obsegs_mmd = obsegs; 20343 obbytes_mmd = obbytes; 20344 20345 /* 20346 * max_pld limits the number of mblks in tcp's transmit 20347 * queue that can be added to a Multidata message. Once 20348 * this counter reaches zero, no more additional mblks 20349 * can be added to it. What happens afterwards depends 20350 * on whether or not we are set to chain the Multidata 20351 * messages. If we are to link them together, reset 20352 * max_pld to its original value (tcp_mdt_max_pld) and 20353 * prepare to create a new Multidata message which will 20354 * get linked to md_mp_head. Else, leave it alone and 20355 * let the inner loop break on its own. 20356 */ 20357 if (tcp_mdt_chain && max_pld == 0) 20358 PREP_NEW_MULTIDATA(); 20359 20360 /* adding a payload buffer; re-initialize values */ 20361 if (add_buffer) 20362 PREP_NEW_PBUF(); 20363 20364 /* 20365 * If we don't have a Multidata, either because we just 20366 * (re)entered this outer loop, or after we branched off 20367 * to tcp_send above, setup the Multidata and header 20368 * buffer to be used. 20369 */ 20370 if (md_mp == NULL) { 20371 int md_hbuflen; 20372 uint32_t start, stuff; 20373 20374 /* 20375 * Calculate Multidata header buffer size large enough 20376 * to hold all of the headers that can possibly be 20377 * sent at this moment. We'd rather over-estimate 20378 * the size than running out of space; this is okay 20379 * since this buffer is small anyway. 20380 */ 20381 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20382 20383 /* 20384 * Start and stuff offset for partial hardware 20385 * checksum offload; these are currently for IPv4. 20386 * For full checksum offload, they are set to zero. 20387 */ 20388 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20389 if (af == AF_INET) { 20390 start = IP_SIMPLE_HDR_LENGTH; 20391 stuff = IP_SIMPLE_HDR_LENGTH + 20392 TCP_CHECKSUM_OFFSET; 20393 } else { 20394 start = IPV6_HDR_LEN; 20395 stuff = IPV6_HDR_LEN + 20396 TCP_CHECKSUM_OFFSET; 20397 } 20398 } else { 20399 start = stuff = 0; 20400 } 20401 20402 /* 20403 * Create the header buffer, Multidata, as well as 20404 * any necessary attributes (destination address, 20405 * SAP and hardware checksum offload) that should 20406 * be associated with the Multidata message. 20407 */ 20408 ASSERT(cur_hdr_off == 0); 20409 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20410 ((md_hbuf->b_wptr += md_hbuflen), 20411 (mmd = mmd_alloc(md_hbuf, &md_mp, 20412 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20413 /* fastpath mblk */ 20414 ire->ire_nce->nce_res_mp, 20415 /* hardware checksum enabled */ 20416 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20417 /* hardware checksum offsets */ 20418 start, stuff, 0, 20419 /* hardware checksum flag */ 20420 hwcksum_flags, tcps) != 0)) { 20421 legacy_send: 20422 /* 20423 * We arrive here from a failure within the 20424 * inner (packetizer) loop or we fail one of 20425 * the conditionals above. We restore the 20426 * previously checkpointed values for: 20427 * xmit_tail 20428 * usable 20429 * tail_unsent 20430 * snxt 20431 * obbytes 20432 * obsegs 20433 * We should then be able to dispatch any 20434 * complete multidata before reverting to the 20435 * traditional path with consistent parameters 20436 * (the inner loop updates these as it 20437 * iterates). 20438 */ 20439 *xmit_tail = xmit_tail_mmd; 20440 *usable = usable_mmd; 20441 *tail_unsent = tail_unsent_mmd; 20442 *snxt = snxt_mmd; 20443 obbytes = obbytes_mmd; 20444 obsegs = obsegs_mmd; 20445 if (md_mp != NULL) { 20446 /* Unlink message from the chain */ 20447 if (md_mp_head != NULL) { 20448 err = (intptr_t)rmvb(md_mp_head, 20449 md_mp); 20450 /* 20451 * We can't assert that rmvb 20452 * did not return -1, since we 20453 * may get here before linkb 20454 * happens. We do, however, 20455 * check if we just removed the 20456 * only element in the list. 20457 */ 20458 if (err == 0) 20459 md_mp_head = NULL; 20460 } 20461 /* md_hbuf gets freed automatically */ 20462 TCP_STAT(tcps, tcp_mdt_discarded); 20463 freeb(md_mp); 20464 } else { 20465 /* Either allocb or mmd_alloc failed */ 20466 TCP_STAT(tcps, tcp_mdt_allocfail); 20467 if (md_hbuf != NULL) 20468 freeb(md_hbuf); 20469 } 20470 20471 /* send down what we've got so far */ 20472 if (md_mp_head != NULL) { 20473 tcp_multisend_data(tcp, ire, ill, 20474 md_mp_head, obsegs, obbytes, 20475 &rconfirm); 20476 } 20477 legacy_send_no_md: 20478 if (ire != NULL) 20479 IRE_REFRELE(ire); 20480 /* 20481 * Too bad; let the legacy path handle this. 20482 * We specify INT_MAX for the threshold, since 20483 * we gave up with the Multidata processings 20484 * and let the old path have it all. 20485 */ 20486 TCP_STAT(tcps, tcp_mdt_legacy_all); 20487 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20488 tcp_tcp_hdr_len, num_sack_blk, usable, 20489 snxt, tail_unsent, xmit_tail, local_time, 20490 INT_MAX)); 20491 } 20492 20493 /* link to any existing ones, if applicable */ 20494 TCP_STAT(tcps, tcp_mdt_allocd); 20495 if (md_mp_head == NULL) { 20496 md_mp_head = md_mp; 20497 } else if (tcp_mdt_chain) { 20498 TCP_STAT(tcps, tcp_mdt_linked); 20499 linkb(md_mp_head, md_mp); 20500 } 20501 } 20502 20503 ASSERT(md_mp_head != NULL); 20504 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20505 ASSERT(md_mp != NULL && mmd != NULL); 20506 ASSERT(md_hbuf != NULL); 20507 20508 /* 20509 * Packetize the transmittable portion of the data block; 20510 * each data block is essentially added to the Multidata 20511 * as a payload buffer. We also deal with adding more 20512 * than one payload buffers, which happens when the remaining 20513 * packetized portion of the current payload buffer is less 20514 * than MSS, while the next data block in transmit queue 20515 * has enough data to make up for one. This "spillover" 20516 * case essentially creates a split-packet, where portions 20517 * of the packet's payload fragments may span across two 20518 * virtually discontiguous address blocks. 20519 */ 20520 seg_len = mss; 20521 do { 20522 len = seg_len; 20523 20524 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20525 ipha = NULL; 20526 ip6h = NULL; 20527 20528 ASSERT(len > 0); 20529 ASSERT(max_pld >= 0); 20530 ASSERT(!add_buffer || cur_pld_off == 0); 20531 20532 /* 20533 * First time around for this payload buffer; note 20534 * in the case of a spillover, the following has 20535 * been done prior to adding the split-packet 20536 * descriptor to Multidata, and we don't want to 20537 * repeat the process. 20538 */ 20539 if (add_buffer) { 20540 ASSERT(mmd != NULL); 20541 ASSERT(md_pbuf == NULL); 20542 ASSERT(md_pbuf_nxt == NULL); 20543 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20544 20545 /* 20546 * Have we reached the limit? We'd get to 20547 * this case when we're not chaining the 20548 * Multidata messages together, and since 20549 * we're done, terminate this loop. 20550 */ 20551 if (max_pld == 0) 20552 break; /* done */ 20553 20554 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20555 TCP_STAT(tcps, tcp_mdt_allocfail); 20556 goto legacy_send; /* out_of_mem */ 20557 } 20558 20559 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20560 zc_cap != NULL) { 20561 if (!ip_md_zcopy_attr(mmd, NULL, 20562 zc_cap->ill_zerocopy_flags)) { 20563 freeb(md_pbuf); 20564 TCP_STAT(tcps, 20565 tcp_mdt_allocfail); 20566 /* out_of_mem */ 20567 goto legacy_send; 20568 } 20569 zcopy = B_TRUE; 20570 } 20571 20572 md_pbuf->b_rptr += base_pld_off; 20573 20574 /* 20575 * Add a payload buffer to the Multidata; this 20576 * operation must not fail, or otherwise our 20577 * logic in this routine is broken. There 20578 * is no memory allocation done by the 20579 * routine, so any returned failure simply 20580 * tells us that we've done something wrong. 20581 * 20582 * A failure tells us that either we're adding 20583 * the same payload buffer more than once, or 20584 * we're trying to add more buffers than 20585 * allowed (max_pld calculation is wrong). 20586 * None of the above cases should happen, and 20587 * we panic because either there's horrible 20588 * heap corruption, and/or programming mistake. 20589 */ 20590 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20591 if (pbuf_idx < 0) { 20592 cmn_err(CE_PANIC, "tcp_multisend: " 20593 "payload buffer logic error " 20594 "detected for tcp %p mmd %p " 20595 "pbuf %p (%d)\n", 20596 (void *)tcp, (void *)mmd, 20597 (void *)md_pbuf, pbuf_idx); 20598 } 20599 20600 ASSERT(max_pld > 0); 20601 --max_pld; 20602 add_buffer = B_FALSE; 20603 } 20604 20605 ASSERT(md_mp_head != NULL); 20606 ASSERT(md_pbuf != NULL); 20607 ASSERT(md_pbuf_nxt == NULL); 20608 ASSERT(pbuf_idx != -1); 20609 ASSERT(pbuf_idx_nxt == -1); 20610 ASSERT(*usable > 0); 20611 20612 /* 20613 * We spillover to the next payload buffer only 20614 * if all of the following is true: 20615 * 20616 * 1. There is not enough data on the current 20617 * payload buffer to make up `len', 20618 * 2. We are allowed to send `len', 20619 * 3. The next payload buffer length is large 20620 * enough to accomodate `spill'. 20621 */ 20622 if ((spill = len - *tail_unsent) > 0 && 20623 *usable >= len && 20624 MBLKL((*xmit_tail)->b_cont) >= spill && 20625 max_pld > 0) { 20626 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20627 if (md_pbuf_nxt == NULL) { 20628 TCP_STAT(tcps, tcp_mdt_allocfail); 20629 goto legacy_send; /* out_of_mem */ 20630 } 20631 20632 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20633 zc_cap != NULL) { 20634 if (!ip_md_zcopy_attr(mmd, NULL, 20635 zc_cap->ill_zerocopy_flags)) { 20636 freeb(md_pbuf_nxt); 20637 TCP_STAT(tcps, 20638 tcp_mdt_allocfail); 20639 /* out_of_mem */ 20640 goto legacy_send; 20641 } 20642 zcopy = B_TRUE; 20643 } 20644 20645 /* 20646 * See comments above on the first call to 20647 * mmd_addpldbuf for explanation on the panic. 20648 */ 20649 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20650 if (pbuf_idx_nxt < 0) { 20651 panic("tcp_multisend: " 20652 "next payload buffer logic error " 20653 "detected for tcp %p mmd %p " 20654 "pbuf %p (%d)\n", 20655 (void *)tcp, (void *)mmd, 20656 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20657 } 20658 20659 ASSERT(max_pld > 0); 20660 --max_pld; 20661 } else if (spill > 0) { 20662 /* 20663 * If there's a spillover, but the following 20664 * xmit_tail couldn't give us enough octets 20665 * to reach "len", then stop the current 20666 * Multidata creation and let the legacy 20667 * tcp_send() path take over. We don't want 20668 * to send the tiny segment as part of this 20669 * Multidata for performance reasons; instead, 20670 * we let the legacy path deal with grouping 20671 * it with the subsequent small mblks. 20672 */ 20673 if (*usable >= len && 20674 MBLKL((*xmit_tail)->b_cont) < spill) { 20675 max_pld = 0; 20676 break; /* done */ 20677 } 20678 20679 /* 20680 * We can't spillover, and we are near 20681 * the end of the current payload buffer, 20682 * so send what's left. 20683 */ 20684 ASSERT(*tail_unsent > 0); 20685 len = *tail_unsent; 20686 } 20687 20688 /* tail_unsent is negated if there is a spillover */ 20689 *tail_unsent -= len; 20690 *usable -= len; 20691 ASSERT(*usable >= 0); 20692 20693 if (*usable < mss) 20694 seg_len = *usable; 20695 /* 20696 * Sender SWS avoidance; see comments in tcp_send(); 20697 * everything else is the same, except that we only 20698 * do this here if there is no more data to be sent 20699 * following the current xmit_tail. We don't check 20700 * for 1-byte urgent data because we shouldn't get 20701 * here if TCP_URG_VALID is set. 20702 */ 20703 if (*usable > 0 && *usable < mss && 20704 ((md_pbuf_nxt == NULL && 20705 (*xmit_tail)->b_cont == NULL) || 20706 (md_pbuf_nxt != NULL && 20707 (*xmit_tail)->b_cont->b_cont == NULL)) && 20708 seg_len < (tcp->tcp_max_swnd >> 1) && 20709 (tcp->tcp_unsent - 20710 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20711 !tcp->tcp_zero_win_probe) { 20712 if ((*snxt + len) == tcp->tcp_snxt && 20713 (*snxt + len) == tcp->tcp_suna) { 20714 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20715 } 20716 done = B_TRUE; 20717 } 20718 20719 /* 20720 * Prime pump for IP's checksumming on our behalf; 20721 * include the adjustment for a source route if any. 20722 * Do this only for software/partial hardware checksum 20723 * offload, as this field gets zeroed out later for 20724 * the full hardware checksum offload case. 20725 */ 20726 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20727 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20728 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20729 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20730 } 20731 20732 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20733 *snxt += len; 20734 20735 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20736 /* 20737 * We set the PUSH bit only if TCP has no more buffered 20738 * data to be transmitted (or if sender SWS avoidance 20739 * takes place), as opposed to setting it for every 20740 * last packet in the burst. 20741 */ 20742 if (done || 20743 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20744 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20745 20746 /* 20747 * Set FIN bit if this is our last segment; snxt 20748 * already includes its length, and it will not 20749 * be adjusted after this point. 20750 */ 20751 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20752 *snxt == tcp->tcp_fss) { 20753 if (!tcp->tcp_fin_acked) { 20754 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20755 BUMP_MIB(&tcps->tcps_mib, 20756 tcpOutControl); 20757 } 20758 if (!tcp->tcp_fin_sent) { 20759 tcp->tcp_fin_sent = B_TRUE; 20760 /* 20761 * tcp state must be ESTABLISHED 20762 * in order for us to get here in 20763 * the first place. 20764 */ 20765 tcp->tcp_state = TCPS_FIN_WAIT_1; 20766 20767 /* 20768 * Upon returning from this routine, 20769 * tcp_wput_data() will set tcp_snxt 20770 * to be equal to snxt + tcp_fin_sent. 20771 * This is essentially the same as 20772 * setting it to tcp_fss + 1. 20773 */ 20774 } 20775 } 20776 20777 tcp->tcp_last_sent_len = (ushort_t)len; 20778 20779 len += tcp_hdr_len; 20780 if (tcp->tcp_ipversion == IPV4_VERSION) 20781 tcp->tcp_ipha->ipha_length = htons(len); 20782 else 20783 tcp->tcp_ip6h->ip6_plen = htons(len - 20784 ((char *)&tcp->tcp_ip6h[1] - 20785 tcp->tcp_iphc)); 20786 20787 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20788 20789 /* setup header fragment */ 20790 PDESC_HDR_ADD(pkt_info, 20791 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20792 tcp->tcp_mdt_hdr_head, /* head room */ 20793 tcp_hdr_len, /* len */ 20794 tcp->tcp_mdt_hdr_tail); /* tail room */ 20795 20796 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20797 hdr_frag_sz); 20798 ASSERT(MBLKIN(md_hbuf, 20799 (pkt_info->hdr_base - md_hbuf->b_rptr), 20800 PDESC_HDRSIZE(pkt_info))); 20801 20802 /* setup first payload fragment */ 20803 PDESC_PLD_INIT(pkt_info); 20804 PDESC_PLD_SPAN_ADD(pkt_info, 20805 pbuf_idx, /* index */ 20806 md_pbuf->b_rptr + cur_pld_off, /* start */ 20807 tcp->tcp_last_sent_len); /* len */ 20808 20809 /* create a split-packet in case of a spillover */ 20810 if (md_pbuf_nxt != NULL) { 20811 ASSERT(spill > 0); 20812 ASSERT(pbuf_idx_nxt > pbuf_idx); 20813 ASSERT(!add_buffer); 20814 20815 md_pbuf = md_pbuf_nxt; 20816 md_pbuf_nxt = NULL; 20817 pbuf_idx = pbuf_idx_nxt; 20818 pbuf_idx_nxt = -1; 20819 cur_pld_off = spill; 20820 20821 /* trim out first payload fragment */ 20822 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20823 20824 /* setup second payload fragment */ 20825 PDESC_PLD_SPAN_ADD(pkt_info, 20826 pbuf_idx, /* index */ 20827 md_pbuf->b_rptr, /* start */ 20828 spill); /* len */ 20829 20830 if ((*xmit_tail)->b_next == NULL) { 20831 /* 20832 * Store the lbolt used for RTT 20833 * estimation. We can only record one 20834 * timestamp per mblk so we do it when 20835 * we reach the end of the payload 20836 * buffer. Also we only take a new 20837 * timestamp sample when the previous 20838 * timed data from the same mblk has 20839 * been ack'ed. 20840 */ 20841 (*xmit_tail)->b_prev = local_time; 20842 (*xmit_tail)->b_next = 20843 (mblk_t *)(uintptr_t)first_snxt; 20844 } 20845 20846 first_snxt = *snxt - spill; 20847 20848 /* 20849 * Advance xmit_tail; usable could be 0 by 20850 * the time we got here, but we made sure 20851 * above that we would only spillover to 20852 * the next data block if usable includes 20853 * the spilled-over amount prior to the 20854 * subtraction. Therefore, we are sure 20855 * that xmit_tail->b_cont can't be NULL. 20856 */ 20857 ASSERT((*xmit_tail)->b_cont != NULL); 20858 *xmit_tail = (*xmit_tail)->b_cont; 20859 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20860 (uintptr_t)INT_MAX); 20861 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20862 } else { 20863 cur_pld_off += tcp->tcp_last_sent_len; 20864 } 20865 20866 /* 20867 * Fill in the header using the template header, and 20868 * add options such as time-stamp, ECN and/or SACK, 20869 * as needed. 20870 */ 20871 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20872 (clock_t)local_time, num_sack_blk); 20873 20874 /* take care of some IP header businesses */ 20875 if (af == AF_INET) { 20876 ipha = (ipha_t *)pkt_info->hdr_rptr; 20877 20878 ASSERT(OK_32PTR((uchar_t *)ipha)); 20879 ASSERT(PDESC_HDRL(pkt_info) >= 20880 IP_SIMPLE_HDR_LENGTH); 20881 ASSERT(ipha->ipha_version_and_hdr_length == 20882 IP_SIMPLE_HDR_VERSION); 20883 20884 /* 20885 * Assign ident value for current packet; see 20886 * related comments in ip_wput_ire() about the 20887 * contract private interface with clustering 20888 * group. 20889 */ 20890 clusterwide = B_FALSE; 20891 if (cl_inet_ipident != NULL) { 20892 ASSERT(cl_inet_isclusterwide != NULL); 20893 if ((*cl_inet_isclusterwide)(stack_id, 20894 IPPROTO_IP, AF_INET, 20895 (uint8_t *)(uintptr_t)src, NULL)) { 20896 ipha->ipha_ident = 20897 (*cl_inet_ipident)(stack_id, 20898 IPPROTO_IP, AF_INET, 20899 (uint8_t *)(uintptr_t)src, 20900 (uint8_t *)(uintptr_t)dst, 20901 NULL); 20902 clusterwide = B_TRUE; 20903 } 20904 } 20905 20906 if (!clusterwide) { 20907 ipha->ipha_ident = (uint16_t) 20908 atomic_add_32_nv( 20909 &ire->ire_ident, 1); 20910 } 20911 #ifndef _BIG_ENDIAN 20912 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20913 (ipha->ipha_ident >> 8); 20914 #endif 20915 } else { 20916 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20917 20918 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20919 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20920 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20921 ASSERT(PDESC_HDRL(pkt_info) >= 20922 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20923 TCP_CHECKSUM_SIZE)); 20924 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20925 20926 if (tcp->tcp_ip_forward_progress) { 20927 rconfirm = B_TRUE; 20928 tcp->tcp_ip_forward_progress = B_FALSE; 20929 } 20930 } 20931 20932 /* at least one payload span, and at most two */ 20933 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20934 20935 /* add the packet descriptor to Multidata */ 20936 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20937 KM_NOSLEEP)) == NULL) { 20938 /* 20939 * Any failure other than ENOMEM indicates 20940 * that we have passed in invalid pkt_info 20941 * or parameters to mmd_addpdesc, which must 20942 * not happen. 20943 * 20944 * EINVAL is a result of failure on boundary 20945 * checks against the pkt_info contents. It 20946 * should not happen, and we panic because 20947 * either there's horrible heap corruption, 20948 * and/or programming mistake. 20949 */ 20950 if (err != ENOMEM) { 20951 cmn_err(CE_PANIC, "tcp_multisend: " 20952 "pdesc logic error detected for " 20953 "tcp %p mmd %p pinfo %p (%d)\n", 20954 (void *)tcp, (void *)mmd, 20955 (void *)pkt_info, err); 20956 } 20957 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20958 goto legacy_send; /* out_of_mem */ 20959 } 20960 ASSERT(pkt != NULL); 20961 20962 /* calculate IP header and TCP checksums */ 20963 if (af == AF_INET) { 20964 /* calculate pseudo-header checksum */ 20965 cksum = (dst >> 16) + (dst & 0xFFFF) + 20966 (src >> 16) + (src & 0xFFFF); 20967 20968 /* offset for TCP header checksum */ 20969 up = IPH_TCPH_CHECKSUMP(ipha, 20970 IP_SIMPLE_HDR_LENGTH); 20971 } else { 20972 up = (uint16_t *)&ip6h->ip6_src; 20973 20974 /* calculate pseudo-header checksum */ 20975 cksum = up[0] + up[1] + up[2] + up[3] + 20976 up[4] + up[5] + up[6] + up[7] + 20977 up[8] + up[9] + up[10] + up[11] + 20978 up[12] + up[13] + up[14] + up[15]; 20979 20980 /* Fold the initial sum */ 20981 cksum = (cksum & 0xffff) + (cksum >> 16); 20982 20983 up = (uint16_t *)(((uchar_t *)ip6h) + 20984 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20985 } 20986 20987 if (hwcksum_flags & HCK_FULLCKSUM) { 20988 /* clear checksum field for hardware */ 20989 *up = 0; 20990 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20991 uint32_t sum; 20992 20993 /* pseudo-header checksumming */ 20994 sum = *up + cksum + IP_TCP_CSUM_COMP; 20995 sum = (sum & 0xFFFF) + (sum >> 16); 20996 *up = (sum & 0xFFFF) + (sum >> 16); 20997 } else { 20998 /* software checksumming */ 20999 TCP_STAT(tcps, tcp_out_sw_cksum); 21000 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 21001 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 21002 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 21003 cksum + IP_TCP_CSUM_COMP); 21004 if (*up == 0) 21005 *up = 0xFFFF; 21006 } 21007 21008 /* IPv4 header checksum */ 21009 if (af == AF_INET) { 21010 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 21011 ipha->ipha_hdr_checksum = 0; 21012 } else { 21013 IP_HDR_CKSUM(ipha, cksum, 21014 ((uint32_t *)ipha)[0], 21015 ((uint16_t *)ipha)[4]); 21016 } 21017 } 21018 21019 if (af == AF_INET && 21020 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 21021 af == AF_INET6 && 21022 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 21023 mblk_t *mp, *mp1; 21024 uchar_t *hdr_rptr, *hdr_wptr; 21025 uchar_t *pld_rptr, *pld_wptr; 21026 21027 /* 21028 * We reconstruct a pseudo packet for the hooks 21029 * framework using mmd_transform_link(). 21030 * If it is a split packet we pullup the 21031 * payload. FW_HOOKS expects a pkt comprising 21032 * of two mblks: a header and the payload. 21033 */ 21034 if ((mp = mmd_transform_link(pkt)) == NULL) { 21035 TCP_STAT(tcps, tcp_mdt_allocfail); 21036 goto legacy_send; 21037 } 21038 21039 if (pkt_info->pld_cnt > 1) { 21040 /* split payload, more than one pld */ 21041 if ((mp1 = msgpullup(mp->b_cont, -1)) == 21042 NULL) { 21043 freemsg(mp); 21044 TCP_STAT(tcps, 21045 tcp_mdt_allocfail); 21046 goto legacy_send; 21047 } 21048 freemsg(mp->b_cont); 21049 mp->b_cont = mp1; 21050 } else { 21051 mp1 = mp->b_cont; 21052 } 21053 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 21054 21055 /* 21056 * Remember the message offsets. This is so we 21057 * can detect changes when we return from the 21058 * FW_HOOKS callbacks. 21059 */ 21060 hdr_rptr = mp->b_rptr; 21061 hdr_wptr = mp->b_wptr; 21062 pld_rptr = mp->b_cont->b_rptr; 21063 pld_wptr = mp->b_cont->b_wptr; 21064 21065 if (af == AF_INET) { 21066 DTRACE_PROBE4( 21067 ip4__physical__out__start, 21068 ill_t *, NULL, 21069 ill_t *, ill, 21070 ipha_t *, ipha, 21071 mblk_t *, mp); 21072 FW_HOOKS( 21073 ipst->ips_ip4_physical_out_event, 21074 ipst->ips_ipv4firewall_physical_out, 21075 NULL, ill, ipha, mp, mp, 0, ipst); 21076 DTRACE_PROBE1( 21077 ip4__physical__out__end, 21078 mblk_t *, mp); 21079 } else { 21080 DTRACE_PROBE4( 21081 ip6__physical__out_start, 21082 ill_t *, NULL, 21083 ill_t *, ill, 21084 ip6_t *, ip6h, 21085 mblk_t *, mp); 21086 FW_HOOKS6( 21087 ipst->ips_ip6_physical_out_event, 21088 ipst->ips_ipv6firewall_physical_out, 21089 NULL, ill, ip6h, mp, mp, 0, ipst); 21090 DTRACE_PROBE1( 21091 ip6__physical__out__end, 21092 mblk_t *, mp); 21093 } 21094 21095 if (mp == NULL || 21096 (mp1 = mp->b_cont) == NULL || 21097 mp->b_rptr != hdr_rptr || 21098 mp->b_wptr != hdr_wptr || 21099 mp1->b_rptr != pld_rptr || 21100 mp1->b_wptr != pld_wptr || 21101 mp1->b_cont != NULL) { 21102 /* 21103 * We abandon multidata processing and 21104 * return to the normal path, either 21105 * when a packet is blocked, or when 21106 * the boundaries of header buffer or 21107 * payload buffer have been changed by 21108 * FW_HOOKS[6]. 21109 */ 21110 if (mp != NULL) 21111 freemsg(mp); 21112 goto legacy_send; 21113 } 21114 /* Finished with the pseudo packet */ 21115 freemsg(mp); 21116 } 21117 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21118 ill, ipha, ip6h); 21119 /* advance header offset */ 21120 cur_hdr_off += hdr_frag_sz; 21121 21122 obbytes += tcp->tcp_last_sent_len; 21123 ++obsegs; 21124 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21125 *tail_unsent > 0); 21126 21127 if ((*xmit_tail)->b_next == NULL) { 21128 /* 21129 * Store the lbolt used for RTT estimation. We can only 21130 * record one timestamp per mblk so we do it when we 21131 * reach the end of the payload buffer. Also we only 21132 * take a new timestamp sample when the previous timed 21133 * data from the same mblk has been ack'ed. 21134 */ 21135 (*xmit_tail)->b_prev = local_time; 21136 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21137 } 21138 21139 ASSERT(*tail_unsent >= 0); 21140 if (*tail_unsent > 0) { 21141 /* 21142 * We got here because we broke out of the above 21143 * loop due to of one of the following cases: 21144 * 21145 * 1. len < adjusted MSS (i.e. small), 21146 * 2. Sender SWS avoidance, 21147 * 3. max_pld is zero. 21148 * 21149 * We are done for this Multidata, so trim our 21150 * last payload buffer (if any) accordingly. 21151 */ 21152 if (md_pbuf != NULL) 21153 md_pbuf->b_wptr -= *tail_unsent; 21154 } else if (*usable > 0) { 21155 *xmit_tail = (*xmit_tail)->b_cont; 21156 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21157 (uintptr_t)INT_MAX); 21158 *tail_unsent = (int)MBLKL(*xmit_tail); 21159 add_buffer = B_TRUE; 21160 } 21161 } while (!done && *usable > 0 && num_burst_seg > 0 && 21162 (tcp_mdt_chain || max_pld > 0)); 21163 21164 if (md_mp_head != NULL) { 21165 /* send everything down */ 21166 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21167 &rconfirm); 21168 } 21169 21170 #undef PREP_NEW_MULTIDATA 21171 #undef PREP_NEW_PBUF 21172 #undef IPVER 21173 21174 IRE_REFRELE(ire); 21175 return (0); 21176 } 21177 21178 /* 21179 * A wrapper function for sending one or more Multidata messages down to 21180 * the module below ip; this routine does not release the reference of the 21181 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21182 */ 21183 static void 21184 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21185 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21186 { 21187 uint64_t delta; 21188 nce_t *nce; 21189 tcp_stack_t *tcps = tcp->tcp_tcps; 21190 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21191 21192 ASSERT(ire != NULL && ill != NULL); 21193 ASSERT(ire->ire_stq != NULL); 21194 ASSERT(md_mp_head != NULL); 21195 ASSERT(rconfirm != NULL); 21196 21197 /* adjust MIBs and IRE timestamp */ 21198 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21199 tcp->tcp_obsegs += obsegs; 21200 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21201 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21202 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21203 21204 if (tcp->tcp_ipversion == IPV4_VERSION) { 21205 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21206 } else { 21207 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21208 } 21209 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21210 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21211 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21212 21213 ire->ire_ob_pkt_count += obsegs; 21214 if (ire->ire_ipif != NULL) 21215 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21216 ire->ire_last_used_time = lbolt; 21217 21218 if (ipst->ips_ipobs_enabled) { 21219 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21220 pdesc_t *dl_pkt; 21221 pdescinfo_t pinfo; 21222 mblk_t *nmp; 21223 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21224 21225 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21226 (dl_pkt != NULL); 21227 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21228 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21229 continue; 21230 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21231 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21232 freemsg(nmp); 21233 } 21234 } 21235 21236 /* send it down */ 21237 putnext(ire->ire_stq, md_mp_head); 21238 21239 /* we're done for TCP/IPv4 */ 21240 if (tcp->tcp_ipversion == IPV4_VERSION) 21241 return; 21242 21243 nce = ire->ire_nce; 21244 21245 ASSERT(nce != NULL); 21246 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21247 ASSERT(nce->nce_state != ND_INCOMPLETE); 21248 21249 /* reachability confirmation? */ 21250 if (*rconfirm) { 21251 nce->nce_last = TICK_TO_MSEC(lbolt64); 21252 if (nce->nce_state != ND_REACHABLE) { 21253 mutex_enter(&nce->nce_lock); 21254 nce->nce_state = ND_REACHABLE; 21255 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21256 mutex_exit(&nce->nce_lock); 21257 (void) untimeout(nce->nce_timeout_id); 21258 if (ip_debug > 2) { 21259 /* ip1dbg */ 21260 pr_addr_dbg("tcp_multisend_data: state " 21261 "for %s changed to REACHABLE\n", 21262 AF_INET6, &ire->ire_addr_v6); 21263 } 21264 } 21265 /* reset transport reachability confirmation */ 21266 *rconfirm = B_FALSE; 21267 } 21268 21269 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21270 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21271 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21272 21273 if (delta > (uint64_t)ill->ill_reachable_time) { 21274 mutex_enter(&nce->nce_lock); 21275 switch (nce->nce_state) { 21276 case ND_REACHABLE: 21277 case ND_STALE: 21278 /* 21279 * ND_REACHABLE is identical to ND_STALE in this 21280 * specific case. If reachable time has expired for 21281 * this neighbor (delta is greater than reachable 21282 * time), conceptually, the neighbor cache is no 21283 * longer in REACHABLE state, but already in STALE 21284 * state. So the correct transition here is to 21285 * ND_DELAY. 21286 */ 21287 nce->nce_state = ND_DELAY; 21288 mutex_exit(&nce->nce_lock); 21289 NDP_RESTART_TIMER(nce, 21290 ipst->ips_delay_first_probe_time); 21291 if (ip_debug > 3) { 21292 /* ip2dbg */ 21293 pr_addr_dbg("tcp_multisend_data: state " 21294 "for %s changed to DELAY\n", 21295 AF_INET6, &ire->ire_addr_v6); 21296 } 21297 break; 21298 case ND_DELAY: 21299 case ND_PROBE: 21300 mutex_exit(&nce->nce_lock); 21301 /* Timers have already started */ 21302 break; 21303 case ND_UNREACHABLE: 21304 /* 21305 * ndp timer has detected that this nce is 21306 * unreachable and initiated deleting this nce 21307 * and all its associated IREs. This is a race 21308 * where we found the ire before it was deleted 21309 * and have just sent out a packet using this 21310 * unreachable nce. 21311 */ 21312 mutex_exit(&nce->nce_lock); 21313 break; 21314 default: 21315 ASSERT(0); 21316 } 21317 } 21318 } 21319 21320 /* 21321 * Derived from tcp_send_data(). 21322 */ 21323 static void 21324 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21325 int num_lso_seg) 21326 { 21327 ipha_t *ipha; 21328 mblk_t *ire_fp_mp; 21329 uint_t ire_fp_mp_len; 21330 uint32_t hcksum_txflags = 0; 21331 ipaddr_t src; 21332 ipaddr_t dst; 21333 uint32_t cksum; 21334 uint16_t *up; 21335 tcp_stack_t *tcps = tcp->tcp_tcps; 21336 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21337 21338 ASSERT(DB_TYPE(mp) == M_DATA); 21339 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21340 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21341 ASSERT(tcp->tcp_connp != NULL); 21342 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21343 21344 ipha = (ipha_t *)mp->b_rptr; 21345 src = ipha->ipha_src; 21346 dst = ipha->ipha_dst; 21347 21348 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21349 21350 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21351 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21352 num_lso_seg); 21353 #ifndef _BIG_ENDIAN 21354 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21355 #endif 21356 if (tcp->tcp_snd_zcopy_aware) { 21357 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21358 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21359 mp = tcp_zcopy_disable(tcp, mp); 21360 } 21361 21362 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21363 ASSERT(ill->ill_hcksum_capab != NULL); 21364 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21365 } 21366 21367 /* 21368 * Since the TCP checksum should be recalculated by h/w, we can just 21369 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21370 * pseudo-header checksum for HCK_PARTIALCKSUM. 21371 * The partial pseudo-header excludes TCP length, that was calculated 21372 * in tcp_send(), so to zero *up before further processing. 21373 */ 21374 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21375 21376 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21377 *up = 0; 21378 21379 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21380 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21381 21382 /* 21383 * Append LSO flags and mss to the mp. 21384 */ 21385 lso_info_set(mp, mss, HW_LSO); 21386 21387 ipha->ipha_fragment_offset_and_flags |= 21388 (uint32_t)htons(ire->ire_frag_flag); 21389 21390 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21391 ire_fp_mp_len = MBLKL(ire_fp_mp); 21392 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21393 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21394 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21395 21396 UPDATE_OB_PKT_COUNT(ire); 21397 ire->ire_last_used_time = lbolt; 21398 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21399 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21400 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21401 ntohs(ipha->ipha_length)); 21402 21403 DTRACE_PROBE4(ip4__physical__out__start, 21404 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 21405 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21406 ipst->ips_ipv4firewall_physical_out, NULL, 21407 ill, ipha, mp, mp, 0, ipst); 21408 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21409 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 21410 21411 if (mp != NULL) { 21412 if (ipst->ips_ipobs_enabled) { 21413 zoneid_t szone; 21414 21415 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 21416 ipst, ALL_ZONES); 21417 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21418 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 21419 } 21420 21421 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0); 21422 } 21423 } 21424 21425 /* 21426 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21427 * scheme, and returns one of the following: 21428 * 21429 * -1 = failed allocation. 21430 * 0 = success; burst count reached, or usable send window is too small, 21431 * and that we'd rather wait until later before sending again. 21432 * 1 = success; we are called from tcp_multisend(), and both usable send 21433 * window and tail_unsent are greater than the MDT threshold, and thus 21434 * Multidata Transmit should be used instead. 21435 */ 21436 static int 21437 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21438 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21439 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21440 const int mdt_thres) 21441 { 21442 int num_burst_seg = tcp->tcp_snd_burst; 21443 ire_t *ire = NULL; 21444 ill_t *ill = NULL; 21445 mblk_t *ire_fp_mp = NULL; 21446 uint_t ire_fp_mp_len = 0; 21447 int num_lso_seg = 1; 21448 uint_t lso_usable; 21449 boolean_t do_lso_send = B_FALSE; 21450 tcp_stack_t *tcps = tcp->tcp_tcps; 21451 21452 /* 21453 * Check LSO capability before any further work. And the similar check 21454 * need to be done in for(;;) loop. 21455 * LSO will be deployed when therer is more than one mss of available 21456 * data and a burst transmission is allowed. 21457 */ 21458 if (tcp->tcp_lso && 21459 (tcp->tcp_valid_bits == 0 || 21460 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21461 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21462 /* 21463 * Try to find usable IRE/ILL and do basic check to the ILL. 21464 */ 21465 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21466 /* 21467 * Enable LSO with this transmission. 21468 * Since IRE has been hold in 21469 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21470 * should be called before return. 21471 */ 21472 do_lso_send = B_TRUE; 21473 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21474 ire_fp_mp_len = MBLKL(ire_fp_mp); 21475 /* Round up to multiple of 4 */ 21476 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21477 } else { 21478 do_lso_send = B_FALSE; 21479 ill = NULL; 21480 } 21481 } 21482 21483 for (;;) { 21484 struct datab *db; 21485 tcph_t *tcph; 21486 uint32_t sum; 21487 mblk_t *mp, *mp1; 21488 uchar_t *rptr; 21489 int len; 21490 21491 /* 21492 * If we're called by tcp_multisend(), and the amount of 21493 * sendable data as well as the size of current xmit_tail 21494 * is beyond the MDT threshold, return to the caller and 21495 * let the large data transmit be done using MDT. 21496 */ 21497 if (*usable > 0 && *usable > mdt_thres && 21498 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21499 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21500 ASSERT(tcp->tcp_mdt); 21501 return (1); /* success; do large send */ 21502 } 21503 21504 if (num_burst_seg == 0) 21505 break; /* success; burst count reached */ 21506 21507 /* 21508 * Calculate the maximum payload length we can send in *one* 21509 * time. 21510 */ 21511 if (do_lso_send) { 21512 /* 21513 * Check whether need to do LSO any more. 21514 */ 21515 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21516 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21517 lso_usable = MIN(lso_usable, 21518 num_burst_seg * mss); 21519 21520 num_lso_seg = lso_usable / mss; 21521 if (lso_usable % mss) { 21522 num_lso_seg++; 21523 tcp->tcp_last_sent_len = (ushort_t) 21524 (lso_usable % mss); 21525 } else { 21526 tcp->tcp_last_sent_len = (ushort_t)mss; 21527 } 21528 } else { 21529 do_lso_send = B_FALSE; 21530 num_lso_seg = 1; 21531 lso_usable = mss; 21532 } 21533 } 21534 21535 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21536 21537 /* 21538 * Adjust num_burst_seg here. 21539 */ 21540 num_burst_seg -= num_lso_seg; 21541 21542 len = mss; 21543 if (len > *usable) { 21544 ASSERT(do_lso_send == B_FALSE); 21545 21546 len = *usable; 21547 if (len <= 0) { 21548 /* Terminate the loop */ 21549 break; /* success; too small */ 21550 } 21551 /* 21552 * Sender silly-window avoidance. 21553 * Ignore this if we are going to send a 21554 * zero window probe out. 21555 * 21556 * TODO: force data into microscopic window? 21557 * ==> (!pushed || (unsent > usable)) 21558 */ 21559 if (len < (tcp->tcp_max_swnd >> 1) && 21560 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21561 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21562 len == 1) && (! tcp->tcp_zero_win_probe)) { 21563 /* 21564 * If the retransmit timer is not running 21565 * we start it so that we will retransmit 21566 * in the case when the the receiver has 21567 * decremented the window. 21568 */ 21569 if (*snxt == tcp->tcp_snxt && 21570 *snxt == tcp->tcp_suna) { 21571 /* 21572 * We are not supposed to send 21573 * anything. So let's wait a little 21574 * bit longer before breaking SWS 21575 * avoidance. 21576 * 21577 * What should the value be? 21578 * Suggestion: MAX(init rexmit time, 21579 * tcp->tcp_rto) 21580 */ 21581 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21582 } 21583 break; /* success; too small */ 21584 } 21585 } 21586 21587 tcph = tcp->tcp_tcph; 21588 21589 /* 21590 * The reason to adjust len here is that we need to set flags 21591 * and calculate checksum. 21592 */ 21593 if (do_lso_send) 21594 len = lso_usable; 21595 21596 *usable -= len; /* Approximate - can be adjusted later */ 21597 if (*usable > 0) 21598 tcph->th_flags[0] = TH_ACK; 21599 else 21600 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21601 21602 /* 21603 * Prime pump for IP's checksumming on our behalf 21604 * Include the adjustment for a source route if any. 21605 */ 21606 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21607 sum = (sum >> 16) + (sum & 0xFFFF); 21608 U16_TO_ABE16(sum, tcph->th_sum); 21609 21610 U32_TO_ABE32(*snxt, tcph->th_seq); 21611 21612 /* 21613 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21614 * set. For the case when TCP_FSS_VALID is the only valid 21615 * bit (normal active close), branch off only when we think 21616 * that the FIN flag needs to be set. Note for this case, 21617 * that (snxt + len) may not reflect the actual seg_len, 21618 * as len may be further reduced in tcp_xmit_mp(). If len 21619 * gets modified, we will end up here again. 21620 */ 21621 if (tcp->tcp_valid_bits != 0 && 21622 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21623 ((*snxt + len) == tcp->tcp_fss))) { 21624 uchar_t *prev_rptr; 21625 uint32_t prev_snxt = tcp->tcp_snxt; 21626 21627 if (*tail_unsent == 0) { 21628 ASSERT((*xmit_tail)->b_cont != NULL); 21629 *xmit_tail = (*xmit_tail)->b_cont; 21630 prev_rptr = (*xmit_tail)->b_rptr; 21631 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21632 (*xmit_tail)->b_rptr); 21633 } else { 21634 prev_rptr = (*xmit_tail)->b_rptr; 21635 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21636 *tail_unsent; 21637 } 21638 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21639 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21640 /* Restore tcp_snxt so we get amount sent right. */ 21641 tcp->tcp_snxt = prev_snxt; 21642 if (prev_rptr == (*xmit_tail)->b_rptr) { 21643 /* 21644 * If the previous timestamp is still in use, 21645 * don't stomp on it. 21646 */ 21647 if ((*xmit_tail)->b_next == NULL) { 21648 (*xmit_tail)->b_prev = local_time; 21649 (*xmit_tail)->b_next = 21650 (mblk_t *)(uintptr_t)(*snxt); 21651 } 21652 } else 21653 (*xmit_tail)->b_rptr = prev_rptr; 21654 21655 if (mp == NULL) { 21656 if (ire != NULL) 21657 IRE_REFRELE(ire); 21658 return (-1); 21659 } 21660 mp1 = mp->b_cont; 21661 21662 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21663 tcp->tcp_last_sent_len = (ushort_t)len; 21664 while (mp1->b_cont) { 21665 *xmit_tail = (*xmit_tail)->b_cont; 21666 (*xmit_tail)->b_prev = local_time; 21667 (*xmit_tail)->b_next = 21668 (mblk_t *)(uintptr_t)(*snxt); 21669 mp1 = mp1->b_cont; 21670 } 21671 *snxt += len; 21672 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21673 BUMP_LOCAL(tcp->tcp_obsegs); 21674 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21675 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21676 tcp_send_data(tcp, q, mp); 21677 continue; 21678 } 21679 21680 *snxt += len; /* Adjust later if we don't send all of len */ 21681 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21682 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21683 21684 if (*tail_unsent) { 21685 /* Are the bytes above us in flight? */ 21686 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21687 if (rptr != (*xmit_tail)->b_rptr) { 21688 *tail_unsent -= len; 21689 if (len <= mss) /* LSO is unusable */ 21690 tcp->tcp_last_sent_len = (ushort_t)len; 21691 len += tcp_hdr_len; 21692 if (tcp->tcp_ipversion == IPV4_VERSION) 21693 tcp->tcp_ipha->ipha_length = htons(len); 21694 else 21695 tcp->tcp_ip6h->ip6_plen = 21696 htons(len - 21697 ((char *)&tcp->tcp_ip6h[1] - 21698 tcp->tcp_iphc)); 21699 mp = dupb(*xmit_tail); 21700 if (mp == NULL) { 21701 if (ire != NULL) 21702 IRE_REFRELE(ire); 21703 return (-1); /* out_of_mem */ 21704 } 21705 mp->b_rptr = rptr; 21706 /* 21707 * If the old timestamp is no longer in use, 21708 * sample a new timestamp now. 21709 */ 21710 if ((*xmit_tail)->b_next == NULL) { 21711 (*xmit_tail)->b_prev = local_time; 21712 (*xmit_tail)->b_next = 21713 (mblk_t *)(uintptr_t)(*snxt-len); 21714 } 21715 goto must_alloc; 21716 } 21717 } else { 21718 *xmit_tail = (*xmit_tail)->b_cont; 21719 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21720 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21721 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21722 (*xmit_tail)->b_rptr); 21723 } 21724 21725 (*xmit_tail)->b_prev = local_time; 21726 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21727 21728 *tail_unsent -= len; 21729 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21730 tcp->tcp_last_sent_len = (ushort_t)len; 21731 21732 len += tcp_hdr_len; 21733 if (tcp->tcp_ipversion == IPV4_VERSION) 21734 tcp->tcp_ipha->ipha_length = htons(len); 21735 else 21736 tcp->tcp_ip6h->ip6_plen = htons(len - 21737 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21738 21739 mp = dupb(*xmit_tail); 21740 if (mp == NULL) { 21741 if (ire != NULL) 21742 IRE_REFRELE(ire); 21743 return (-1); /* out_of_mem */ 21744 } 21745 21746 len = tcp_hdr_len; 21747 /* 21748 * There are four reasons to allocate a new hdr mblk: 21749 * 1) The bytes above us are in use by another packet 21750 * 2) We don't have good alignment 21751 * 3) The mblk is being shared 21752 * 4) We don't have enough room for a header 21753 */ 21754 rptr = mp->b_rptr - len; 21755 if (!OK_32PTR(rptr) || 21756 ((db = mp->b_datap), db->db_ref != 2) || 21757 rptr < db->db_base + ire_fp_mp_len) { 21758 /* NOTE: we assume allocb returns an OK_32PTR */ 21759 21760 must_alloc:; 21761 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21762 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21763 if (mp1 == NULL) { 21764 freemsg(mp); 21765 if (ire != NULL) 21766 IRE_REFRELE(ire); 21767 return (-1); /* out_of_mem */ 21768 } 21769 mp1->b_cont = mp; 21770 mp = mp1; 21771 /* Leave room for Link Level header */ 21772 len = tcp_hdr_len; 21773 rptr = 21774 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21775 mp->b_wptr = &rptr[len]; 21776 } 21777 21778 /* 21779 * Fill in the header using the template header, and add 21780 * options such as time-stamp, ECN and/or SACK, as needed. 21781 */ 21782 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21783 21784 mp->b_rptr = rptr; 21785 21786 if (*tail_unsent) { 21787 int spill = *tail_unsent; 21788 21789 mp1 = mp->b_cont; 21790 if (mp1 == NULL) 21791 mp1 = mp; 21792 21793 /* 21794 * If we're a little short, tack on more mblks until 21795 * there is no more spillover. 21796 */ 21797 while (spill < 0) { 21798 mblk_t *nmp; 21799 int nmpsz; 21800 21801 nmp = (*xmit_tail)->b_cont; 21802 nmpsz = MBLKL(nmp); 21803 21804 /* 21805 * Excess data in mblk; can we split it? 21806 * If MDT is enabled for the connection, 21807 * keep on splitting as this is a transient 21808 * send path. 21809 */ 21810 if (!do_lso_send && !tcp->tcp_mdt && 21811 (spill + nmpsz > 0)) { 21812 /* 21813 * Don't split if stream head was 21814 * told to break up larger writes 21815 * into smaller ones. 21816 */ 21817 if (tcp->tcp_maxpsz > 0) 21818 break; 21819 21820 /* 21821 * Next mblk is less than SMSS/2 21822 * rounded up to nearest 64-byte; 21823 * let it get sent as part of the 21824 * next segment. 21825 */ 21826 if (tcp->tcp_localnet && 21827 !tcp->tcp_cork && 21828 (nmpsz < roundup((mss >> 1), 64))) 21829 break; 21830 } 21831 21832 *xmit_tail = nmp; 21833 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21834 /* Stash for rtt use later */ 21835 (*xmit_tail)->b_prev = local_time; 21836 (*xmit_tail)->b_next = 21837 (mblk_t *)(uintptr_t)(*snxt - len); 21838 mp1->b_cont = dupb(*xmit_tail); 21839 mp1 = mp1->b_cont; 21840 21841 spill += nmpsz; 21842 if (mp1 == NULL) { 21843 *tail_unsent = spill; 21844 freemsg(mp); 21845 if (ire != NULL) 21846 IRE_REFRELE(ire); 21847 return (-1); /* out_of_mem */ 21848 } 21849 } 21850 21851 /* Trim back any surplus on the last mblk */ 21852 if (spill >= 0) { 21853 mp1->b_wptr -= spill; 21854 *tail_unsent = spill; 21855 } else { 21856 /* 21857 * We did not send everything we could in 21858 * order to remain within the b_cont limit. 21859 */ 21860 *usable -= spill; 21861 *snxt += spill; 21862 tcp->tcp_last_sent_len += spill; 21863 UPDATE_MIB(&tcps->tcps_mib, 21864 tcpOutDataBytes, spill); 21865 /* 21866 * Adjust the checksum 21867 */ 21868 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21869 sum += spill; 21870 sum = (sum >> 16) + (sum & 0xFFFF); 21871 U16_TO_ABE16(sum, tcph->th_sum); 21872 if (tcp->tcp_ipversion == IPV4_VERSION) { 21873 sum = ntohs( 21874 ((ipha_t *)rptr)->ipha_length) + 21875 spill; 21876 ((ipha_t *)rptr)->ipha_length = 21877 htons(sum); 21878 } else { 21879 sum = ntohs( 21880 ((ip6_t *)rptr)->ip6_plen) + 21881 spill; 21882 ((ip6_t *)rptr)->ip6_plen = 21883 htons(sum); 21884 } 21885 *tail_unsent = 0; 21886 } 21887 } 21888 if (tcp->tcp_ip_forward_progress) { 21889 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21890 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21891 tcp->tcp_ip_forward_progress = B_FALSE; 21892 } 21893 21894 if (do_lso_send) { 21895 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21896 num_lso_seg); 21897 tcp->tcp_obsegs += num_lso_seg; 21898 21899 TCP_STAT(tcps, tcp_lso_times); 21900 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21901 } else { 21902 tcp_send_data(tcp, q, mp); 21903 BUMP_LOCAL(tcp->tcp_obsegs); 21904 } 21905 } 21906 21907 if (ire != NULL) 21908 IRE_REFRELE(ire); 21909 return (0); 21910 } 21911 21912 /* Unlink and return any mblk that looks like it contains a MDT info */ 21913 static mblk_t * 21914 tcp_mdt_info_mp(mblk_t *mp) 21915 { 21916 mblk_t *prev_mp; 21917 21918 for (;;) { 21919 prev_mp = mp; 21920 /* no more to process? */ 21921 if ((mp = mp->b_cont) == NULL) 21922 break; 21923 21924 switch (DB_TYPE(mp)) { 21925 case M_CTL: 21926 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21927 continue; 21928 ASSERT(prev_mp != NULL); 21929 prev_mp->b_cont = mp->b_cont; 21930 mp->b_cont = NULL; 21931 return (mp); 21932 default: 21933 break; 21934 } 21935 } 21936 return (mp); 21937 } 21938 21939 /* MDT info update routine, called when IP notifies us about MDT */ 21940 static void 21941 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21942 { 21943 boolean_t prev_state; 21944 tcp_stack_t *tcps = tcp->tcp_tcps; 21945 21946 /* 21947 * IP is telling us to abort MDT on this connection? We know 21948 * this because the capability is only turned off when IP 21949 * encounters some pathological cases, e.g. link-layer change 21950 * where the new driver doesn't support MDT, or in situation 21951 * where MDT usage on the link-layer has been switched off. 21952 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21953 * if the link-layer doesn't support MDT, and if it does, it 21954 * will indicate that the feature is to be turned on. 21955 */ 21956 prev_state = tcp->tcp_mdt; 21957 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21958 if (!tcp->tcp_mdt && !first) { 21959 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21960 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21961 (void *)tcp->tcp_connp)); 21962 } 21963 21964 /* 21965 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21966 * so disable MDT otherwise. The checks are done here 21967 * and in tcp_wput_data(). 21968 */ 21969 if (tcp->tcp_mdt && 21970 (tcp->tcp_ipversion == IPV4_VERSION && 21971 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21972 (tcp->tcp_ipversion == IPV6_VERSION && 21973 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21974 tcp->tcp_mdt = B_FALSE; 21975 21976 if (tcp->tcp_mdt) { 21977 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21978 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21979 "version (%d), expected version is %d", 21980 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21981 tcp->tcp_mdt = B_FALSE; 21982 return; 21983 } 21984 21985 /* 21986 * We need the driver to be able to handle at least three 21987 * spans per packet in order for tcp MDT to be utilized. 21988 * The first is for the header portion, while the rest are 21989 * needed to handle a packet that straddles across two 21990 * virtually non-contiguous buffers; a typical tcp packet 21991 * therefore consists of only two spans. Note that we take 21992 * a zero as "don't care". 21993 */ 21994 if (mdt_capab->ill_mdt_span_limit > 0 && 21995 mdt_capab->ill_mdt_span_limit < 3) { 21996 tcp->tcp_mdt = B_FALSE; 21997 return; 21998 } 21999 22000 /* a zero means driver wants default value */ 22001 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 22002 tcps->tcps_mdt_max_pbufs); 22003 if (tcp->tcp_mdt_max_pld == 0) 22004 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 22005 22006 /* ensure 32-bit alignment */ 22007 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 22008 mdt_capab->ill_mdt_hdr_head), 4); 22009 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 22010 mdt_capab->ill_mdt_hdr_tail), 4); 22011 22012 if (!first && !prev_state) { 22013 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 22014 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 22015 (void *)tcp->tcp_connp)); 22016 } 22017 } 22018 } 22019 22020 /* Unlink and return any mblk that looks like it contains a LSO info */ 22021 static mblk_t * 22022 tcp_lso_info_mp(mblk_t *mp) 22023 { 22024 mblk_t *prev_mp; 22025 22026 for (;;) { 22027 prev_mp = mp; 22028 /* no more to process? */ 22029 if ((mp = mp->b_cont) == NULL) 22030 break; 22031 22032 switch (DB_TYPE(mp)) { 22033 case M_CTL: 22034 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 22035 continue; 22036 ASSERT(prev_mp != NULL); 22037 prev_mp->b_cont = mp->b_cont; 22038 mp->b_cont = NULL; 22039 return (mp); 22040 default: 22041 break; 22042 } 22043 } 22044 22045 return (mp); 22046 } 22047 22048 /* LSO info update routine, called when IP notifies us about LSO */ 22049 static void 22050 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 22051 { 22052 tcp_stack_t *tcps = tcp->tcp_tcps; 22053 22054 /* 22055 * IP is telling us to abort LSO on this connection? We know 22056 * this because the capability is only turned off when IP 22057 * encounters some pathological cases, e.g. link-layer change 22058 * where the new NIC/driver doesn't support LSO, or in situation 22059 * where LSO usage on the link-layer has been switched off. 22060 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 22061 * if the link-layer doesn't support LSO, and if it does, it 22062 * will indicate that the feature is to be turned on. 22063 */ 22064 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 22065 TCP_STAT(tcps, tcp_lso_enabled); 22066 22067 /* 22068 * We currently only support LSO on simple TCP/IPv4, 22069 * so disable LSO otherwise. The checks are done here 22070 * and in tcp_wput_data(). 22071 */ 22072 if (tcp->tcp_lso && 22073 (tcp->tcp_ipversion == IPV4_VERSION && 22074 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 22075 (tcp->tcp_ipversion == IPV6_VERSION)) { 22076 tcp->tcp_lso = B_FALSE; 22077 TCP_STAT(tcps, tcp_lso_disabled); 22078 } else { 22079 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 22080 lso_capab->ill_lso_max); 22081 } 22082 } 22083 22084 static void 22085 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 22086 { 22087 conn_t *connp = tcp->tcp_connp; 22088 tcp_stack_t *tcps = tcp->tcp_tcps; 22089 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22090 22091 ASSERT(ire != NULL); 22092 22093 /* 22094 * We may be in the fastpath here, and although we essentially do 22095 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 22096 * we try to keep things as brief as possible. After all, these 22097 * are only best-effort checks, and we do more thorough ones prior 22098 * to calling tcp_send()/tcp_multisend(). 22099 */ 22100 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22101 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22102 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22103 !(ire->ire_flags & RTF_MULTIRT) && 22104 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22105 CONN_IS_LSO_MD_FASTPATH(connp)) { 22106 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22107 /* Cache the result */ 22108 connp->conn_lso_ok = B_TRUE; 22109 22110 ASSERT(ill->ill_lso_capab != NULL); 22111 if (!ill->ill_lso_capab->ill_lso_on) { 22112 ill->ill_lso_capab->ill_lso_on = 1; 22113 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22114 "LSO for interface %s\n", (void *)connp, 22115 ill->ill_name)); 22116 } 22117 tcp_lso_update(tcp, ill->ill_lso_capab); 22118 } else if (ipst->ips_ip_multidata_outbound && 22119 ILL_MDT_CAPABLE(ill)) { 22120 /* Cache the result */ 22121 connp->conn_mdt_ok = B_TRUE; 22122 22123 ASSERT(ill->ill_mdt_capab != NULL); 22124 if (!ill->ill_mdt_capab->ill_mdt_on) { 22125 ill->ill_mdt_capab->ill_mdt_on = 1; 22126 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22127 "MDT for interface %s\n", (void *)connp, 22128 ill->ill_name)); 22129 } 22130 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22131 } 22132 } 22133 22134 /* 22135 * The goal is to reduce the number of generated tcp segments by 22136 * setting the maxpsz multiplier to 0; this will have an affect on 22137 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22138 * into each packet, up to SMSS bytes. Doing this reduces the number 22139 * of outbound segments and incoming ACKs, thus allowing for better 22140 * network and system performance. In contrast the legacy behavior 22141 * may result in sending less than SMSS size, because the last mblk 22142 * for some packets may have more data than needed to make up SMSS, 22143 * and the legacy code refused to "split" it. 22144 * 22145 * We apply the new behavior on following situations: 22146 * 22147 * 1) Loopback connections, 22148 * 2) Connections in which the remote peer is not on local subnet, 22149 * 3) Local subnet connections over the bge interface (see below). 22150 * 22151 * Ideally, we would like this behavior to apply for interfaces other 22152 * than bge. However, doing so would negatively impact drivers which 22153 * perform dynamic mapping and unmapping of DMA resources, which are 22154 * increased by setting the maxpsz multiplier to 0 (more mblks per 22155 * packet will be generated by tcp). The bge driver does not suffer 22156 * from this, as it copies the mblks into pre-mapped buffers, and 22157 * therefore does not require more I/O resources than before. 22158 * 22159 * Otherwise, this behavior is present on all network interfaces when 22160 * the destination endpoint is non-local, since reducing the number 22161 * of packets in general is good for the network. 22162 * 22163 * TODO We need to remove this hard-coded conditional for bge once 22164 * a better "self-tuning" mechanism, or a way to comprehend 22165 * the driver transmit strategy is devised. Until the solution 22166 * is found and well understood, we live with this hack. 22167 */ 22168 if (!tcp_static_maxpsz && 22169 (tcp->tcp_loopback || !tcp->tcp_localnet || 22170 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22171 /* override the default value */ 22172 tcp->tcp_maxpsz = 0; 22173 22174 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22175 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22176 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22177 } 22178 22179 /* set the stream head parameters accordingly */ 22180 (void) tcp_maxpsz_set(tcp, B_TRUE); 22181 } 22182 22183 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22184 static void 22185 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22186 { 22187 uchar_t fval = *mp->b_rptr; 22188 mblk_t *tail; 22189 queue_t *q = tcp->tcp_wq; 22190 22191 /* TODO: How should flush interact with urgent data? */ 22192 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22193 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22194 /* 22195 * Flush only data that has not yet been put on the wire. If 22196 * we flush data that we have already transmitted, life, as we 22197 * know it, may come to an end. 22198 */ 22199 tail = tcp->tcp_xmit_tail; 22200 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22201 tcp->tcp_xmit_tail_unsent = 0; 22202 tcp->tcp_unsent = 0; 22203 if (tail->b_wptr != tail->b_rptr) 22204 tail = tail->b_cont; 22205 if (tail) { 22206 mblk_t **excess = &tcp->tcp_xmit_head; 22207 for (;;) { 22208 mblk_t *mp1 = *excess; 22209 if (mp1 == tail) 22210 break; 22211 tcp->tcp_xmit_tail = mp1; 22212 tcp->tcp_xmit_last = mp1; 22213 excess = &mp1->b_cont; 22214 } 22215 *excess = NULL; 22216 tcp_close_mpp(&tail); 22217 if (tcp->tcp_snd_zcopy_aware) 22218 tcp_zcopy_notify(tcp); 22219 } 22220 /* 22221 * We have no unsent data, so unsent must be less than 22222 * tcp_xmit_lowater, so re-enable flow. 22223 */ 22224 mutex_enter(&tcp->tcp_non_sq_lock); 22225 if (tcp->tcp_flow_stopped) { 22226 tcp_clrqfull(tcp); 22227 } 22228 mutex_exit(&tcp->tcp_non_sq_lock); 22229 } 22230 /* 22231 * TODO: you can't just flush these, you have to increase rwnd for one 22232 * thing. For another, how should urgent data interact? 22233 */ 22234 if (fval & FLUSHR) { 22235 *mp->b_rptr = fval & ~FLUSHW; 22236 /* XXX */ 22237 qreply(q, mp); 22238 return; 22239 } 22240 freemsg(mp); 22241 } 22242 22243 /* 22244 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22245 * messages. 22246 */ 22247 static void 22248 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22249 { 22250 mblk_t *mp1; 22251 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22252 STRUCT_HANDLE(strbuf, sb); 22253 queue_t *q = tcp->tcp_wq; 22254 int error; 22255 uint_t addrlen; 22256 22257 /* Make sure it is one of ours. */ 22258 switch (iocp->ioc_cmd) { 22259 case TI_GETMYNAME: 22260 case TI_GETPEERNAME: 22261 break; 22262 default: 22263 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22264 return; 22265 } 22266 switch (mi_copy_state(q, mp, &mp1)) { 22267 case -1: 22268 return; 22269 case MI_COPY_CASE(MI_COPY_IN, 1): 22270 break; 22271 case MI_COPY_CASE(MI_COPY_OUT, 1): 22272 /* Copy out the strbuf. */ 22273 mi_copyout(q, mp); 22274 return; 22275 case MI_COPY_CASE(MI_COPY_OUT, 2): 22276 /* All done. */ 22277 mi_copy_done(q, mp, 0); 22278 return; 22279 default: 22280 mi_copy_done(q, mp, EPROTO); 22281 return; 22282 } 22283 /* Check alignment of the strbuf */ 22284 if (!OK_32PTR(mp1->b_rptr)) { 22285 mi_copy_done(q, mp, EINVAL); 22286 return; 22287 } 22288 22289 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22290 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22291 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22292 mi_copy_done(q, mp, EINVAL); 22293 return; 22294 } 22295 22296 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22297 if (mp1 == NULL) 22298 return; 22299 22300 switch (iocp->ioc_cmd) { 22301 case TI_GETMYNAME: 22302 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 22303 break; 22304 case TI_GETPEERNAME: 22305 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22306 break; 22307 } 22308 22309 if (error != 0) { 22310 mi_copy_done(q, mp, error); 22311 } else { 22312 mp1->b_wptr += addrlen; 22313 STRUCT_FSET(sb, len, addrlen); 22314 22315 /* Copy out the address */ 22316 mi_copyout(q, mp); 22317 } 22318 } 22319 22320 static void 22321 tcp_disable_direct_sockfs(tcp_t *tcp) 22322 { 22323 #ifdef _ILP32 22324 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 22325 #else 22326 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22327 #endif 22328 /* 22329 * Insert this socket into the acceptor hash. 22330 * We might need it for T_CONN_RES message 22331 */ 22332 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22333 22334 if (tcp->tcp_fused) { 22335 /* 22336 * This is a fused loopback tcp; disable 22337 * read-side synchronous streams interface 22338 * and drain any queued data. It is okay 22339 * to do this for non-synchronous streams 22340 * fused tcp as well. 22341 */ 22342 tcp_fuse_disable_pair(tcp, B_FALSE); 22343 } 22344 tcp->tcp_issocket = B_FALSE; 22345 tcp->tcp_sodirect = NULL; 22346 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 22347 } 22348 22349 /* 22350 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22351 * messages. 22352 */ 22353 /* ARGSUSED */ 22354 static void 22355 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22356 { 22357 conn_t *connp = (conn_t *)arg; 22358 tcp_t *tcp = connp->conn_tcp; 22359 queue_t *q = tcp->tcp_wq; 22360 struct iocblk *iocp; 22361 22362 ASSERT(DB_TYPE(mp) == M_IOCTL); 22363 /* 22364 * Try and ASSERT the minimum possible references on the 22365 * conn early enough. Since we are executing on write side, 22366 * the connection is obviously not detached and that means 22367 * there is a ref each for TCP and IP. Since we are behind 22368 * the squeue, the minimum references needed are 3. If the 22369 * conn is in classifier hash list, there should be an 22370 * extra ref for that (we check both the possibilities). 22371 */ 22372 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22373 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22374 22375 iocp = (struct iocblk *)mp->b_rptr; 22376 switch (iocp->ioc_cmd) { 22377 case TCP_IOC_DEFAULT_Q: 22378 /* Wants to be the default wq. */ 22379 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22380 iocp->ioc_error = EPERM; 22381 iocp->ioc_count = 0; 22382 mp->b_datap->db_type = M_IOCACK; 22383 qreply(q, mp); 22384 return; 22385 } 22386 tcp_def_q_set(tcp, mp); 22387 return; 22388 case _SIOCSOCKFALLBACK: 22389 /* 22390 * Either sockmod is about to be popped and the socket 22391 * would now be treated as a plain stream, or a module 22392 * is about to be pushed so we could no longer use read- 22393 * side synchronous streams for fused loopback tcp. 22394 * Drain any queued data and disable direct sockfs 22395 * interface from now on. 22396 */ 22397 if (!tcp->tcp_issocket) { 22398 DB_TYPE(mp) = M_IOCNAK; 22399 iocp->ioc_error = EINVAL; 22400 } else { 22401 tcp_disable_direct_sockfs(tcp); 22402 DB_TYPE(mp) = M_IOCACK; 22403 iocp->ioc_error = 0; 22404 } 22405 iocp->ioc_count = 0; 22406 iocp->ioc_rval = 0; 22407 qreply(q, mp); 22408 return; 22409 } 22410 CALL_IP_WPUT(connp, q, mp); 22411 } 22412 22413 /* 22414 * This routine is called by tcp_wput() to handle all TPI requests. 22415 */ 22416 /* ARGSUSED */ 22417 static void 22418 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22419 { 22420 conn_t *connp = (conn_t *)arg; 22421 tcp_t *tcp = connp->conn_tcp; 22422 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22423 uchar_t *rptr; 22424 t_scalar_t type; 22425 cred_t *cr; 22426 22427 /* 22428 * Try and ASSERT the minimum possible references on the 22429 * conn early enough. Since we are executing on write side, 22430 * the connection is obviously not detached and that means 22431 * there is a ref each for TCP and IP. Since we are behind 22432 * the squeue, the minimum references needed are 3. If the 22433 * conn is in classifier hash list, there should be an 22434 * extra ref for that (we check both the possibilities). 22435 */ 22436 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22437 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22438 22439 rptr = mp->b_rptr; 22440 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22441 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22442 type = ((union T_primitives *)rptr)->type; 22443 if (type == T_EXDATA_REQ) { 22444 tcp_output_urgent(connp, mp->b_cont, arg2); 22445 freeb(mp); 22446 } else if (type != T_DATA_REQ) { 22447 goto non_urgent_data; 22448 } else { 22449 /* TODO: options, flags, ... from user */ 22450 /* Set length to zero for reclamation below */ 22451 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22452 freeb(mp); 22453 } 22454 return; 22455 } else { 22456 if (tcp->tcp_debug) { 22457 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22458 "tcp_wput_proto, dropping one..."); 22459 } 22460 freemsg(mp); 22461 return; 22462 } 22463 22464 non_urgent_data: 22465 22466 switch ((int)tprim->type) { 22467 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22468 /* 22469 * save the kssl_ent_t from the next block, and convert this 22470 * back to a normal bind_req. 22471 */ 22472 if (mp->b_cont != NULL) { 22473 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22474 22475 if (tcp->tcp_kssl_ent != NULL) { 22476 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22477 KSSL_NO_PROXY); 22478 tcp->tcp_kssl_ent = NULL; 22479 } 22480 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22481 sizeof (kssl_ent_t)); 22482 kssl_hold_ent(tcp->tcp_kssl_ent); 22483 freemsg(mp->b_cont); 22484 mp->b_cont = NULL; 22485 } 22486 tprim->type = T_BIND_REQ; 22487 22488 /* FALLTHROUGH */ 22489 case O_T_BIND_REQ: /* bind request */ 22490 case T_BIND_REQ: /* new semantics bind request */ 22491 tcp_tpi_bind(tcp, mp); 22492 break; 22493 case T_UNBIND_REQ: /* unbind request */ 22494 tcp_tpi_unbind(tcp, mp); 22495 break; 22496 case O_T_CONN_RES: /* old connection response XXX */ 22497 case T_CONN_RES: /* connection response */ 22498 tcp_tli_accept(tcp, mp); 22499 break; 22500 case T_CONN_REQ: /* connection request */ 22501 tcp_tpi_connect(tcp, mp); 22502 break; 22503 case T_DISCON_REQ: /* disconnect request */ 22504 tcp_disconnect(tcp, mp); 22505 break; 22506 case T_CAPABILITY_REQ: 22507 tcp_capability_req(tcp, mp); /* capability request */ 22508 break; 22509 case T_INFO_REQ: /* information request */ 22510 tcp_info_req(tcp, mp); 22511 break; 22512 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22513 case T_OPTMGMT_REQ: 22514 /* 22515 * Note: no support for snmpcom_req() through new 22516 * T_OPTMGMT_REQ. See comments in ip.c 22517 */ 22518 22519 /* 22520 * All Solaris components should pass a db_credp 22521 * for this TPI message, hence we ASSERT. 22522 * But in case there is some other M_PROTO that looks 22523 * like a TPI message sent by some other kernel 22524 * component, we check and return an error. 22525 */ 22526 cr = msg_getcred(mp, NULL); 22527 ASSERT(cr != NULL); 22528 if (cr == NULL) { 22529 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 22530 return; 22531 } 22532 /* 22533 * If EINPROGRESS is returned, the request has been queued 22534 * for subsequent processing by ip_restart_optmgmt(), which 22535 * will do the CONN_DEC_REF(). 22536 */ 22537 CONN_INC_REF(connp); 22538 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 22539 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22540 B_TRUE) != EINPROGRESS) { 22541 CONN_DEC_REF(connp); 22542 } 22543 } else { 22544 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22545 B_TRUE) != EINPROGRESS) { 22546 CONN_DEC_REF(connp); 22547 } 22548 } 22549 break; 22550 22551 case T_UNITDATA_REQ: /* unitdata request */ 22552 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22553 break; 22554 case T_ORDREL_REQ: /* orderly release req */ 22555 freemsg(mp); 22556 22557 if (tcp->tcp_fused) 22558 tcp_unfuse(tcp); 22559 22560 if (tcp_xmit_end(tcp) != 0) { 22561 /* 22562 * We were crossing FINs and got a reset from 22563 * the other side. Just ignore it. 22564 */ 22565 if (tcp->tcp_debug) { 22566 (void) strlog(TCP_MOD_ID, 0, 1, 22567 SL_ERROR|SL_TRACE, 22568 "tcp_wput_proto, T_ORDREL_REQ out of " 22569 "state %s", 22570 tcp_display(tcp, NULL, 22571 DISP_ADDR_AND_PORT)); 22572 } 22573 } 22574 break; 22575 case T_ADDR_REQ: 22576 tcp_addr_req(tcp, mp); 22577 break; 22578 default: 22579 if (tcp->tcp_debug) { 22580 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22581 "tcp_wput_proto, bogus TPI msg, type %d", 22582 tprim->type); 22583 } 22584 /* 22585 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22586 * to recover. 22587 */ 22588 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22589 break; 22590 } 22591 } 22592 22593 /* 22594 * The TCP write service routine should never be called... 22595 */ 22596 /* ARGSUSED */ 22597 static void 22598 tcp_wsrv(queue_t *q) 22599 { 22600 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22601 22602 TCP_STAT(tcps, tcp_wsrv_called); 22603 } 22604 22605 /* Non overlapping byte exchanger */ 22606 static void 22607 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22608 { 22609 uchar_t uch; 22610 22611 while (len-- > 0) { 22612 uch = a[len]; 22613 a[len] = b[len]; 22614 b[len] = uch; 22615 } 22616 } 22617 22618 /* 22619 * Send out a control packet on the tcp connection specified. This routine 22620 * is typically called where we need a simple ACK or RST generated. 22621 */ 22622 static void 22623 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22624 { 22625 uchar_t *rptr; 22626 tcph_t *tcph; 22627 ipha_t *ipha = NULL; 22628 ip6_t *ip6h = NULL; 22629 uint32_t sum; 22630 int tcp_hdr_len; 22631 int tcp_ip_hdr_len; 22632 mblk_t *mp; 22633 tcp_stack_t *tcps = tcp->tcp_tcps; 22634 22635 /* 22636 * Save sum for use in source route later. 22637 */ 22638 ASSERT(tcp != NULL); 22639 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22640 tcp_hdr_len = tcp->tcp_hdr_len; 22641 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22642 22643 /* If a text string is passed in with the request, pass it to strlog. */ 22644 if (str != NULL && tcp->tcp_debug) { 22645 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22646 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22647 str, seq, ack, ctl); 22648 } 22649 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22650 BPRI_MED); 22651 if (mp == NULL) { 22652 return; 22653 } 22654 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22655 mp->b_rptr = rptr; 22656 mp->b_wptr = &rptr[tcp_hdr_len]; 22657 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22658 22659 if (tcp->tcp_ipversion == IPV4_VERSION) { 22660 ipha = (ipha_t *)rptr; 22661 ipha->ipha_length = htons(tcp_hdr_len); 22662 } else { 22663 ip6h = (ip6_t *)rptr; 22664 ASSERT(tcp != NULL); 22665 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22666 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22667 } 22668 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22669 tcph->th_flags[0] = (uint8_t)ctl; 22670 if (ctl & TH_RST) { 22671 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22672 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22673 /* 22674 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22675 */ 22676 if (tcp->tcp_snd_ts_ok && 22677 tcp->tcp_state > TCPS_SYN_SENT) { 22678 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22679 *(mp->b_wptr) = TCPOPT_EOL; 22680 if (tcp->tcp_ipversion == IPV4_VERSION) { 22681 ipha->ipha_length = htons(tcp_hdr_len - 22682 TCPOPT_REAL_TS_LEN); 22683 } else { 22684 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22685 TCPOPT_REAL_TS_LEN); 22686 } 22687 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22688 sum -= TCPOPT_REAL_TS_LEN; 22689 } 22690 } 22691 if (ctl & TH_ACK) { 22692 if (tcp->tcp_snd_ts_ok) { 22693 U32_TO_BE32(lbolt, 22694 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22695 U32_TO_BE32(tcp->tcp_ts_recent, 22696 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22697 } 22698 22699 /* Update the latest receive window size in TCP header. */ 22700 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22701 tcph->th_win); 22702 tcp->tcp_rack = ack; 22703 tcp->tcp_rack_cnt = 0; 22704 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22705 } 22706 BUMP_LOCAL(tcp->tcp_obsegs); 22707 U32_TO_BE32(seq, tcph->th_seq); 22708 U32_TO_BE32(ack, tcph->th_ack); 22709 /* 22710 * Include the adjustment for a source route if any. 22711 */ 22712 sum = (sum >> 16) + (sum & 0xFFFF); 22713 U16_TO_BE16(sum, tcph->th_sum); 22714 tcp_send_data(tcp, tcp->tcp_wq, mp); 22715 } 22716 22717 /* 22718 * If this routine returns B_TRUE, TCP can generate a RST in response 22719 * to a segment. If it returns B_FALSE, TCP should not respond. 22720 */ 22721 static boolean_t 22722 tcp_send_rst_chk(tcp_stack_t *tcps) 22723 { 22724 clock_t now; 22725 22726 /* 22727 * TCP needs to protect itself from generating too many RSTs. 22728 * This can be a DoS attack by sending us random segments 22729 * soliciting RSTs. 22730 * 22731 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22732 * in each 1 second interval. In this way, TCP still generate 22733 * RSTs in normal cases but when under attack, the impact is 22734 * limited. 22735 */ 22736 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22737 now = lbolt; 22738 /* lbolt can wrap around. */ 22739 if ((tcps->tcps_last_rst_intrvl > now) || 22740 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22741 1*SECONDS)) { 22742 tcps->tcps_last_rst_intrvl = now; 22743 tcps->tcps_rst_cnt = 1; 22744 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22745 return (B_FALSE); 22746 } 22747 } 22748 return (B_TRUE); 22749 } 22750 22751 /* 22752 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22753 */ 22754 static void 22755 tcp_ip_ire_mark_advice(tcp_t *tcp) 22756 { 22757 mblk_t *mp; 22758 ipic_t *ipic; 22759 22760 if (tcp->tcp_ipversion == IPV4_VERSION) { 22761 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22762 &ipic); 22763 } else { 22764 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22765 &ipic); 22766 } 22767 if (mp == NULL) 22768 return; 22769 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22770 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22771 } 22772 22773 /* 22774 * Return an IP advice ioctl mblk and set ipic to be the pointer 22775 * to the advice structure. 22776 */ 22777 static mblk_t * 22778 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22779 { 22780 struct iocblk *ioc; 22781 mblk_t *mp, *mp1; 22782 22783 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22784 if (mp == NULL) 22785 return (NULL); 22786 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22787 *ipic = (ipic_t *)mp->b_rptr; 22788 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22789 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22790 22791 bcopy(addr, *ipic + 1, addr_len); 22792 22793 (*ipic)->ipic_addr_length = addr_len; 22794 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22795 22796 mp1 = mkiocb(IP_IOCTL); 22797 if (mp1 == NULL) { 22798 freemsg(mp); 22799 return (NULL); 22800 } 22801 mp1->b_cont = mp; 22802 ioc = (struct iocblk *)mp1->b_rptr; 22803 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22804 22805 return (mp1); 22806 } 22807 22808 /* 22809 * Generate a reset based on an inbound packet, connp is set by caller 22810 * when RST is in response to an unexpected inbound packet for which 22811 * there is active tcp state in the system. 22812 * 22813 * IPSEC NOTE : Try to send the reply with the same protection as it came 22814 * in. We still have the ipsec_mp that the packet was attached to. Thus 22815 * the packet will go out at the same level of protection as it came in by 22816 * converting the IPSEC_IN to IPSEC_OUT. 22817 */ 22818 static void 22819 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22820 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22821 tcp_stack_t *tcps, conn_t *connp) 22822 { 22823 ipha_t *ipha = NULL; 22824 ip6_t *ip6h = NULL; 22825 ushort_t len; 22826 tcph_t *tcph; 22827 int i; 22828 mblk_t *ipsec_mp; 22829 boolean_t mctl_present; 22830 ipic_t *ipic; 22831 ipaddr_t v4addr; 22832 in6_addr_t v6addr; 22833 int addr_len; 22834 void *addr; 22835 queue_t *q = tcps->tcps_g_q; 22836 tcp_t *tcp; 22837 cred_t *cr; 22838 mblk_t *nmp; 22839 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22840 22841 if (tcps->tcps_g_q == NULL) { 22842 /* 22843 * For non-zero stackids the default queue isn't created 22844 * until the first open, thus there can be a need to send 22845 * a reset before then. But we can't do that, hence we just 22846 * drop the packet. Later during boot, when the default queue 22847 * has been setup, a retransmitted packet from the peer 22848 * will result in a reset. 22849 */ 22850 ASSERT(tcps->tcps_netstack->netstack_stackid != 22851 GLOBAL_NETSTACKID); 22852 freemsg(mp); 22853 return; 22854 } 22855 22856 if (connp != NULL) 22857 tcp = connp->conn_tcp; 22858 else 22859 tcp = Q_TO_TCP(q); 22860 22861 if (!tcp_send_rst_chk(tcps)) { 22862 tcps->tcps_rst_unsent++; 22863 freemsg(mp); 22864 return; 22865 } 22866 22867 if (mp->b_datap->db_type == M_CTL) { 22868 ipsec_mp = mp; 22869 mp = mp->b_cont; 22870 mctl_present = B_TRUE; 22871 } else { 22872 ipsec_mp = mp; 22873 mctl_present = B_FALSE; 22874 } 22875 22876 if (str && q && tcps->tcps_dbg) { 22877 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22878 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22879 "flags 0x%x", 22880 str, seq, ack, ctl); 22881 } 22882 if (mp->b_datap->db_ref != 1) { 22883 mblk_t *mp1 = copyb(mp); 22884 freemsg(mp); 22885 mp = mp1; 22886 if (!mp) { 22887 if (mctl_present) 22888 freeb(ipsec_mp); 22889 return; 22890 } else { 22891 if (mctl_present) { 22892 ipsec_mp->b_cont = mp; 22893 } else { 22894 ipsec_mp = mp; 22895 } 22896 } 22897 } else if (mp->b_cont) { 22898 freemsg(mp->b_cont); 22899 mp->b_cont = NULL; 22900 } 22901 /* 22902 * We skip reversing source route here. 22903 * (for now we replace all IP options with EOL) 22904 */ 22905 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22906 ipha = (ipha_t *)mp->b_rptr; 22907 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22908 mp->b_rptr[i] = IPOPT_EOL; 22909 /* 22910 * Make sure that src address isn't flagrantly invalid. 22911 * Not all broadcast address checking for the src address 22912 * is possible, since we don't know the netmask of the src 22913 * addr. No check for destination address is done, since 22914 * IP will not pass up a packet with a broadcast dest 22915 * address to TCP. Similar checks are done below for IPv6. 22916 */ 22917 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22918 CLASSD(ipha->ipha_src)) { 22919 freemsg(ipsec_mp); 22920 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22921 return; 22922 } 22923 } else { 22924 ip6h = (ip6_t *)mp->b_rptr; 22925 22926 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22927 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22928 freemsg(ipsec_mp); 22929 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22930 return; 22931 } 22932 22933 /* Remove any extension headers assuming partial overlay */ 22934 if (ip_hdr_len > IPV6_HDR_LEN) { 22935 uint8_t *to; 22936 22937 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22938 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22939 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22940 ip_hdr_len = IPV6_HDR_LEN; 22941 ip6h = (ip6_t *)mp->b_rptr; 22942 ip6h->ip6_nxt = IPPROTO_TCP; 22943 } 22944 } 22945 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22946 if (tcph->th_flags[0] & TH_RST) { 22947 freemsg(ipsec_mp); 22948 return; 22949 } 22950 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22951 len = ip_hdr_len + sizeof (tcph_t); 22952 mp->b_wptr = &mp->b_rptr[len]; 22953 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22954 ipha->ipha_length = htons(len); 22955 /* Swap addresses */ 22956 v4addr = ipha->ipha_src; 22957 ipha->ipha_src = ipha->ipha_dst; 22958 ipha->ipha_dst = v4addr; 22959 ipha->ipha_ident = 0; 22960 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22961 addr_len = IP_ADDR_LEN; 22962 addr = &v4addr; 22963 } else { 22964 /* No ip6i_t in this case */ 22965 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22966 /* Swap addresses */ 22967 v6addr = ip6h->ip6_src; 22968 ip6h->ip6_src = ip6h->ip6_dst; 22969 ip6h->ip6_dst = v6addr; 22970 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22971 addr_len = IPV6_ADDR_LEN; 22972 addr = &v6addr; 22973 } 22974 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22975 U32_TO_BE32(ack, tcph->th_ack); 22976 U32_TO_BE32(seq, tcph->th_seq); 22977 U16_TO_BE16(0, tcph->th_win); 22978 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22979 tcph->th_flags[0] = (uint8_t)ctl; 22980 if (ctl & TH_RST) { 22981 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22982 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22983 } 22984 22985 /* IP trusts us to set up labels when required. */ 22986 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22987 crgetlabel(cr) != NULL) { 22988 int err; 22989 22990 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22991 err = tsol_check_label(cr, &mp, 22992 tcp->tcp_connp->conn_mac_exempt, 22993 tcps->tcps_netstack->netstack_ip); 22994 else 22995 err = tsol_check_label_v6(cr, &mp, 22996 tcp->tcp_connp->conn_mac_exempt, 22997 tcps->tcps_netstack->netstack_ip); 22998 if (mctl_present) 22999 ipsec_mp->b_cont = mp; 23000 else 23001 ipsec_mp = mp; 23002 if (err != 0) { 23003 freemsg(ipsec_mp); 23004 return; 23005 } 23006 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23007 ipha = (ipha_t *)mp->b_rptr; 23008 } else { 23009 ip6h = (ip6_t *)mp->b_rptr; 23010 } 23011 } 23012 23013 if (mctl_present) { 23014 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23015 23016 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23017 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 23018 return; 23019 } 23020 } 23021 if (zoneid == ALL_ZONES) 23022 zoneid = GLOBAL_ZONEID; 23023 23024 /* Add the zoneid so ip_output routes it properly */ 23025 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 23026 freemsg(ipsec_mp); 23027 return; 23028 } 23029 ipsec_mp = nmp; 23030 23031 /* 23032 * NOTE: one might consider tracing a TCP packet here, but 23033 * this function has no active TCP state and no tcp structure 23034 * that has a trace buffer. If we traced here, we would have 23035 * to keep a local trace buffer in tcp_record_trace(). 23036 * 23037 * TSol note: The mblk that contains the incoming packet was 23038 * reused by tcp_xmit_listener_reset, so it already contains 23039 * the right credentials and we don't need to call mblk_setcred. 23040 * Also the conn's cred is not right since it is associated 23041 * with tcps_g_q. 23042 */ 23043 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 23044 23045 /* 23046 * Tell IP to mark the IRE used for this destination temporary. 23047 * This way, we can limit our exposure to DoS attack because IP 23048 * creates an IRE for each destination. If there are too many, 23049 * the time to do any routing lookup will be extremely long. And 23050 * the lookup can be in interrupt context. 23051 * 23052 * Note that in normal circumstances, this marking should not 23053 * affect anything. It would be nice if only 1 message is 23054 * needed to inform IP that the IRE created for this RST should 23055 * not be added to the cache table. But there is currently 23056 * not such communication mechanism between TCP and IP. So 23057 * the best we can do now is to send the advice ioctl to IP 23058 * to mark the IRE temporary. 23059 */ 23060 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 23061 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 23062 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23063 } 23064 } 23065 23066 /* 23067 * Initiate closedown sequence on an active connection. (May be called as 23068 * writer.) Return value zero for OK return, non-zero for error return. 23069 */ 23070 static int 23071 tcp_xmit_end(tcp_t *tcp) 23072 { 23073 ipic_t *ipic; 23074 mblk_t *mp; 23075 tcp_stack_t *tcps = tcp->tcp_tcps; 23076 23077 if (tcp->tcp_state < TCPS_SYN_RCVD || 23078 tcp->tcp_state > TCPS_CLOSE_WAIT) { 23079 /* 23080 * Invalid state, only states TCPS_SYN_RCVD, 23081 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 23082 */ 23083 return (-1); 23084 } 23085 23086 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 23087 tcp->tcp_valid_bits |= TCP_FSS_VALID; 23088 /* 23089 * If there is nothing more unsent, send the FIN now. 23090 * Otherwise, it will go out with the last segment. 23091 */ 23092 if (tcp->tcp_unsent == 0) { 23093 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 23094 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 23095 23096 if (mp) { 23097 tcp_send_data(tcp, tcp->tcp_wq, mp); 23098 } else { 23099 /* 23100 * Couldn't allocate msg. Pretend we got it out. 23101 * Wait for rexmit timeout. 23102 */ 23103 tcp->tcp_snxt = tcp->tcp_fss + 1; 23104 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23105 } 23106 23107 /* 23108 * If needed, update tcp_rexmit_snxt as tcp_snxt is 23109 * changed. 23110 */ 23111 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 23112 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23113 } 23114 } else { 23115 /* 23116 * If tcp->tcp_cork is set, then the data will not get sent, 23117 * so we have to check that and unset it first. 23118 */ 23119 if (tcp->tcp_cork) 23120 tcp->tcp_cork = B_FALSE; 23121 tcp_wput_data(tcp, NULL, B_FALSE); 23122 } 23123 23124 /* 23125 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23126 * is 0, don't update the cache. 23127 */ 23128 if (tcps->tcps_rtt_updates == 0 || 23129 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23130 return (0); 23131 23132 /* 23133 * NOTE: should not update if source routes i.e. if tcp_remote if 23134 * different from the destination. 23135 */ 23136 if (tcp->tcp_ipversion == IPV4_VERSION) { 23137 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23138 return (0); 23139 } 23140 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23141 &ipic); 23142 } else { 23143 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23144 &tcp->tcp_ip6h->ip6_dst))) { 23145 return (0); 23146 } 23147 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23148 &ipic); 23149 } 23150 23151 /* Record route attributes in the IRE for use by future connections. */ 23152 if (mp == NULL) 23153 return (0); 23154 23155 /* 23156 * We do not have a good algorithm to update ssthresh at this time. 23157 * So don't do any update. 23158 */ 23159 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23160 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23161 23162 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23163 23164 return (0); 23165 } 23166 23167 /* 23168 * Generate a "no listener here" RST in response to an "unknown" segment. 23169 * connp is set by caller when RST is in response to an unexpected 23170 * inbound packet for which there is active tcp state in the system. 23171 * Note that we are reusing the incoming mp to construct the outgoing RST. 23172 */ 23173 void 23174 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23175 tcp_stack_t *tcps, conn_t *connp) 23176 { 23177 uchar_t *rptr; 23178 uint32_t seg_len; 23179 tcph_t *tcph; 23180 uint32_t seg_seq; 23181 uint32_t seg_ack; 23182 uint_t flags; 23183 mblk_t *ipsec_mp; 23184 ipha_t *ipha; 23185 ip6_t *ip6h; 23186 boolean_t mctl_present = B_FALSE; 23187 boolean_t check = B_TRUE; 23188 boolean_t policy_present; 23189 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23190 23191 TCP_STAT(tcps, tcp_no_listener); 23192 23193 ipsec_mp = mp; 23194 23195 if (mp->b_datap->db_type == M_CTL) { 23196 ipsec_in_t *ii; 23197 23198 mctl_present = B_TRUE; 23199 mp = mp->b_cont; 23200 23201 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23202 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23203 if (ii->ipsec_in_dont_check) { 23204 check = B_FALSE; 23205 if (!ii->ipsec_in_secure) { 23206 freeb(ipsec_mp); 23207 mctl_present = B_FALSE; 23208 ipsec_mp = mp; 23209 } 23210 } 23211 } 23212 23213 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23214 policy_present = ipss->ipsec_inbound_v4_policy_present; 23215 ipha = (ipha_t *)mp->b_rptr; 23216 ip6h = NULL; 23217 } else { 23218 policy_present = ipss->ipsec_inbound_v6_policy_present; 23219 ipha = NULL; 23220 ip6h = (ip6_t *)mp->b_rptr; 23221 } 23222 23223 if (check && policy_present) { 23224 /* 23225 * The conn_t parameter is NULL because we already know 23226 * nobody's home. 23227 */ 23228 ipsec_mp = ipsec_check_global_policy( 23229 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23230 tcps->tcps_netstack); 23231 if (ipsec_mp == NULL) 23232 return; 23233 } 23234 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23235 DTRACE_PROBE2( 23236 tx__ip__log__error__nolistener__tcp, 23237 char *, "Could not reply with RST to mp(1)", 23238 mblk_t *, mp); 23239 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23240 freemsg(ipsec_mp); 23241 return; 23242 } 23243 23244 rptr = mp->b_rptr; 23245 23246 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23247 seg_seq = BE32_TO_U32(tcph->th_seq); 23248 seg_ack = BE32_TO_U32(tcph->th_ack); 23249 flags = tcph->th_flags[0]; 23250 23251 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23252 if (flags & TH_RST) { 23253 freemsg(ipsec_mp); 23254 } else if (flags & TH_ACK) { 23255 tcp_xmit_early_reset("no tcp, reset", 23256 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23257 connp); 23258 } else { 23259 if (flags & TH_SYN) { 23260 seg_len++; 23261 } else { 23262 /* 23263 * Here we violate the RFC. Note that a normal 23264 * TCP will never send a segment without the ACK 23265 * flag, except for RST or SYN segment. This 23266 * segment is neither. Just drop it on the 23267 * floor. 23268 */ 23269 freemsg(ipsec_mp); 23270 tcps->tcps_rst_unsent++; 23271 return; 23272 } 23273 23274 tcp_xmit_early_reset("no tcp, reset/ack", 23275 ipsec_mp, 0, seg_seq + seg_len, 23276 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23277 } 23278 } 23279 23280 /* 23281 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23282 * ip and tcp header ready to pass down to IP. If the mp passed in is 23283 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23284 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23285 * otherwise it will dup partial mblks.) 23286 * Otherwise, an appropriate ACK packet will be generated. This 23287 * routine is not usually called to send new data for the first time. It 23288 * is mostly called out of the timer for retransmits, and to generate ACKs. 23289 * 23290 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23291 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23292 * of the original mblk chain will be returned in *offset and *end_mp. 23293 */ 23294 mblk_t * 23295 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23296 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23297 boolean_t rexmit) 23298 { 23299 int data_length; 23300 int32_t off = 0; 23301 uint_t flags; 23302 mblk_t *mp1; 23303 mblk_t *mp2; 23304 uchar_t *rptr; 23305 tcph_t *tcph; 23306 int32_t num_sack_blk = 0; 23307 int32_t sack_opt_len = 0; 23308 tcp_stack_t *tcps = tcp->tcp_tcps; 23309 23310 /* Allocate for our maximum TCP header + link-level */ 23311 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23312 tcps->tcps_wroff_xtra, BPRI_MED); 23313 if (!mp1) 23314 return (NULL); 23315 data_length = 0; 23316 23317 /* 23318 * Note that tcp_mss has been adjusted to take into account the 23319 * timestamp option if applicable. Because SACK options do not 23320 * appear in every TCP segments and they are of variable lengths, 23321 * they cannot be included in tcp_mss. Thus we need to calculate 23322 * the actual segment length when we need to send a segment which 23323 * includes SACK options. 23324 */ 23325 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23326 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23327 tcp->tcp_num_sack_blk); 23328 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23329 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23330 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23331 max_to_send -= sack_opt_len; 23332 } 23333 23334 if (offset != NULL) { 23335 off = *offset; 23336 /* We use offset as an indicator that end_mp is not NULL. */ 23337 *end_mp = NULL; 23338 } 23339 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23340 /* This could be faster with cooperation from downstream */ 23341 if (mp2 != mp1 && !sendall && 23342 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23343 max_to_send) 23344 /* 23345 * Don't send the next mblk since the whole mblk 23346 * does not fit. 23347 */ 23348 break; 23349 mp2->b_cont = dupb(mp); 23350 mp2 = mp2->b_cont; 23351 if (!mp2) { 23352 freemsg(mp1); 23353 return (NULL); 23354 } 23355 mp2->b_rptr += off; 23356 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23357 (uintptr_t)INT_MAX); 23358 23359 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23360 if (data_length > max_to_send) { 23361 mp2->b_wptr -= data_length - max_to_send; 23362 data_length = max_to_send; 23363 off = mp2->b_wptr - mp->b_rptr; 23364 break; 23365 } else { 23366 off = 0; 23367 } 23368 } 23369 if (offset != NULL) { 23370 *offset = off; 23371 *end_mp = mp; 23372 } 23373 if (seg_len != NULL) { 23374 *seg_len = data_length; 23375 } 23376 23377 /* Update the latest receive window size in TCP header. */ 23378 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23379 tcp->tcp_tcph->th_win); 23380 23381 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23382 mp1->b_rptr = rptr; 23383 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23384 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23385 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23386 U32_TO_ABE32(seq, tcph->th_seq); 23387 23388 /* 23389 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23390 * that this function was called from tcp_wput_data. Thus, when called 23391 * to retransmit data the setting of the PUSH bit may appear some 23392 * what random in that it might get set when it should not. This 23393 * should not pose any performance issues. 23394 */ 23395 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23396 tcp->tcp_unsent == data_length)) { 23397 flags = TH_ACK | TH_PUSH; 23398 } else { 23399 flags = TH_ACK; 23400 } 23401 23402 if (tcp->tcp_ecn_ok) { 23403 if (tcp->tcp_ecn_echo_on) 23404 flags |= TH_ECE; 23405 23406 /* 23407 * Only set ECT bit and ECN_CWR if a segment contains new data. 23408 * There is no TCP flow control for non-data segments, and 23409 * only data segment is transmitted reliably. 23410 */ 23411 if (data_length > 0 && !rexmit) { 23412 SET_ECT(tcp, rptr); 23413 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23414 flags |= TH_CWR; 23415 tcp->tcp_ecn_cwr_sent = B_TRUE; 23416 } 23417 } 23418 } 23419 23420 if (tcp->tcp_valid_bits) { 23421 uint32_t u1; 23422 23423 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23424 seq == tcp->tcp_iss) { 23425 uchar_t *wptr; 23426 23427 /* 23428 * If TCP_ISS_VALID and the seq number is tcp_iss, 23429 * TCP can only be in SYN-SENT, SYN-RCVD or 23430 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23431 * our SYN is not ack'ed but the app closes this 23432 * TCP connection. 23433 */ 23434 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23435 tcp->tcp_state == TCPS_SYN_RCVD || 23436 tcp->tcp_state == TCPS_FIN_WAIT_1); 23437 23438 /* 23439 * Tack on the MSS option. It is always needed 23440 * for both active and passive open. 23441 * 23442 * MSS option value should be interface MTU - MIN 23443 * TCP/IP header according to RFC 793 as it means 23444 * the maximum segment size TCP can receive. But 23445 * to get around some broken middle boxes/end hosts 23446 * out there, we allow the option value to be the 23447 * same as the MSS option size on the peer side. 23448 * In this way, the other side will not send 23449 * anything larger than they can receive. 23450 * 23451 * Note that for SYN_SENT state, the ndd param 23452 * tcp_use_smss_as_mss_opt has no effect as we 23453 * don't know the peer's MSS option value. So 23454 * the only case we need to take care of is in 23455 * SYN_RCVD state, which is done later. 23456 */ 23457 wptr = mp1->b_wptr; 23458 wptr[0] = TCPOPT_MAXSEG; 23459 wptr[1] = TCPOPT_MAXSEG_LEN; 23460 wptr += 2; 23461 u1 = tcp->tcp_if_mtu - 23462 (tcp->tcp_ipversion == IPV4_VERSION ? 23463 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23464 TCP_MIN_HEADER_LENGTH; 23465 U16_TO_BE16(u1, wptr); 23466 mp1->b_wptr = wptr + 2; 23467 /* Update the offset to cover the additional word */ 23468 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23469 23470 /* 23471 * Note that the following way of filling in 23472 * TCP options are not optimal. Some NOPs can 23473 * be saved. But there is no need at this time 23474 * to optimize it. When it is needed, we will 23475 * do it. 23476 */ 23477 switch (tcp->tcp_state) { 23478 case TCPS_SYN_SENT: 23479 flags = TH_SYN; 23480 23481 if (tcp->tcp_snd_ts_ok) { 23482 uint32_t llbolt = (uint32_t)lbolt; 23483 23484 wptr = mp1->b_wptr; 23485 wptr[0] = TCPOPT_NOP; 23486 wptr[1] = TCPOPT_NOP; 23487 wptr[2] = TCPOPT_TSTAMP; 23488 wptr[3] = TCPOPT_TSTAMP_LEN; 23489 wptr += 4; 23490 U32_TO_BE32(llbolt, wptr); 23491 wptr += 4; 23492 ASSERT(tcp->tcp_ts_recent == 0); 23493 U32_TO_BE32(0L, wptr); 23494 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23495 tcph->th_offset_and_rsrvd[0] += 23496 (3 << 4); 23497 } 23498 23499 /* 23500 * Set up all the bits to tell other side 23501 * we are ECN capable. 23502 */ 23503 if (tcp->tcp_ecn_ok) { 23504 flags |= (TH_ECE | TH_CWR); 23505 } 23506 break; 23507 case TCPS_SYN_RCVD: 23508 flags |= TH_SYN; 23509 23510 /* 23511 * Reset the MSS option value to be SMSS 23512 * We should probably add back the bytes 23513 * for timestamp option and IPsec. We 23514 * don't do that as this is a workaround 23515 * for broken middle boxes/end hosts, it 23516 * is better for us to be more cautious. 23517 * They may not take these things into 23518 * account in their SMSS calculation. Thus 23519 * the peer's calculated SMSS may be smaller 23520 * than what it can be. This should be OK. 23521 */ 23522 if (tcps->tcps_use_smss_as_mss_opt) { 23523 u1 = tcp->tcp_mss; 23524 U16_TO_BE16(u1, wptr); 23525 } 23526 23527 /* 23528 * If the other side is ECN capable, reply 23529 * that we are also ECN capable. 23530 */ 23531 if (tcp->tcp_ecn_ok) 23532 flags |= TH_ECE; 23533 break; 23534 default: 23535 /* 23536 * The above ASSERT() makes sure that this 23537 * must be FIN-WAIT-1 state. Our SYN has 23538 * not been ack'ed so retransmit it. 23539 */ 23540 flags |= TH_SYN; 23541 break; 23542 } 23543 23544 if (tcp->tcp_snd_ws_ok) { 23545 wptr = mp1->b_wptr; 23546 wptr[0] = TCPOPT_NOP; 23547 wptr[1] = TCPOPT_WSCALE; 23548 wptr[2] = TCPOPT_WS_LEN; 23549 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23550 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23551 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23552 } 23553 23554 if (tcp->tcp_snd_sack_ok) { 23555 wptr = mp1->b_wptr; 23556 wptr[0] = TCPOPT_NOP; 23557 wptr[1] = TCPOPT_NOP; 23558 wptr[2] = TCPOPT_SACK_PERMITTED; 23559 wptr[3] = TCPOPT_SACK_OK_LEN; 23560 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23561 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23562 } 23563 23564 /* allocb() of adequate mblk assures space */ 23565 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23566 (uintptr_t)INT_MAX); 23567 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23568 /* 23569 * Get IP set to checksum on our behalf 23570 * Include the adjustment for a source route if any. 23571 */ 23572 u1 += tcp->tcp_sum; 23573 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23574 U16_TO_BE16(u1, tcph->th_sum); 23575 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23576 } 23577 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23578 (seq + data_length) == tcp->tcp_fss) { 23579 if (!tcp->tcp_fin_acked) { 23580 flags |= TH_FIN; 23581 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23582 } 23583 if (!tcp->tcp_fin_sent) { 23584 tcp->tcp_fin_sent = B_TRUE; 23585 switch (tcp->tcp_state) { 23586 case TCPS_SYN_RCVD: 23587 case TCPS_ESTABLISHED: 23588 tcp->tcp_state = TCPS_FIN_WAIT_1; 23589 break; 23590 case TCPS_CLOSE_WAIT: 23591 tcp->tcp_state = TCPS_LAST_ACK; 23592 break; 23593 } 23594 if (tcp->tcp_suna == tcp->tcp_snxt) 23595 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23596 tcp->tcp_snxt = tcp->tcp_fss + 1; 23597 } 23598 } 23599 /* 23600 * Note the trick here. u1 is unsigned. When tcp_urg 23601 * is smaller than seq, u1 will become a very huge value. 23602 * So the comparison will fail. Also note that tcp_urp 23603 * should be positive, see RFC 793 page 17. 23604 */ 23605 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23606 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23607 u1 < (uint32_t)(64 * 1024)) { 23608 flags |= TH_URG; 23609 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23610 U32_TO_ABE16(u1, tcph->th_urp); 23611 } 23612 } 23613 tcph->th_flags[0] = (uchar_t)flags; 23614 tcp->tcp_rack = tcp->tcp_rnxt; 23615 tcp->tcp_rack_cnt = 0; 23616 23617 if (tcp->tcp_snd_ts_ok) { 23618 if (tcp->tcp_state != TCPS_SYN_SENT) { 23619 uint32_t llbolt = (uint32_t)lbolt; 23620 23621 U32_TO_BE32(llbolt, 23622 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23623 U32_TO_BE32(tcp->tcp_ts_recent, 23624 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23625 } 23626 } 23627 23628 if (num_sack_blk > 0) { 23629 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23630 sack_blk_t *tmp; 23631 int32_t i; 23632 23633 wptr[0] = TCPOPT_NOP; 23634 wptr[1] = TCPOPT_NOP; 23635 wptr[2] = TCPOPT_SACK; 23636 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23637 sizeof (sack_blk_t); 23638 wptr += TCPOPT_REAL_SACK_LEN; 23639 23640 tmp = tcp->tcp_sack_list; 23641 for (i = 0; i < num_sack_blk; i++) { 23642 U32_TO_BE32(tmp[i].begin, wptr); 23643 wptr += sizeof (tcp_seq); 23644 U32_TO_BE32(tmp[i].end, wptr); 23645 wptr += sizeof (tcp_seq); 23646 } 23647 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23648 } 23649 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23650 data_length += (int)(mp1->b_wptr - rptr); 23651 if (tcp->tcp_ipversion == IPV4_VERSION) { 23652 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23653 } else { 23654 ip6_t *ip6 = (ip6_t *)(rptr + 23655 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23656 sizeof (ip6i_t) : 0)); 23657 23658 ip6->ip6_plen = htons(data_length - 23659 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23660 } 23661 23662 /* 23663 * Prime pump for IP 23664 * Include the adjustment for a source route if any. 23665 */ 23666 data_length -= tcp->tcp_ip_hdr_len; 23667 data_length += tcp->tcp_sum; 23668 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23669 U16_TO_ABE16(data_length, tcph->th_sum); 23670 if (tcp->tcp_ip_forward_progress) { 23671 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23672 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23673 tcp->tcp_ip_forward_progress = B_FALSE; 23674 } 23675 return (mp1); 23676 } 23677 23678 /* This function handles the push timeout. */ 23679 void 23680 tcp_push_timer(void *arg) 23681 { 23682 conn_t *connp = (conn_t *)arg; 23683 tcp_t *tcp = connp->conn_tcp; 23684 uint_t flags; 23685 sodirect_t *sodp; 23686 23687 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23688 23689 ASSERT(tcp->tcp_listener == NULL); 23690 23691 ASSERT(!IPCL_IS_NONSTR(connp)); 23692 23693 /* 23694 * We need to plug synchronous streams during our drain to prevent 23695 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23696 */ 23697 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23698 tcp->tcp_push_tid = 0; 23699 23700 SOD_PTR_ENTER(tcp, sodp); 23701 if (sodp != NULL) { 23702 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23703 /* sod_wakeup() does the mutex_exit() */ 23704 } else if (tcp->tcp_rcv_list != NULL) { 23705 flags = tcp_rcv_drain(tcp); 23706 } 23707 if (flags == TH_ACK_NEEDED) 23708 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23709 23710 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23711 } 23712 23713 /* 23714 * This function handles delayed ACK timeout. 23715 */ 23716 static void 23717 tcp_ack_timer(void *arg) 23718 { 23719 conn_t *connp = (conn_t *)arg; 23720 tcp_t *tcp = connp->conn_tcp; 23721 mblk_t *mp; 23722 tcp_stack_t *tcps = tcp->tcp_tcps; 23723 23724 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23725 23726 tcp->tcp_ack_tid = 0; 23727 23728 if (tcp->tcp_fused) 23729 return; 23730 23731 /* 23732 * Do not send ACK if there is no outstanding unack'ed data. 23733 */ 23734 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23735 return; 23736 } 23737 23738 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23739 /* 23740 * Make sure we don't allow deferred ACKs to result in 23741 * timer-based ACKing. If we have held off an ACK 23742 * when there was more than an mss here, and the timer 23743 * goes off, we have to worry about the possibility 23744 * that the sender isn't doing slow-start, or is out 23745 * of step with us for some other reason. We fall 23746 * permanently back in the direction of 23747 * ACK-every-other-packet as suggested in RFC 1122. 23748 */ 23749 if (tcp->tcp_rack_abs_max > 2) 23750 tcp->tcp_rack_abs_max--; 23751 tcp->tcp_rack_cur_max = 2; 23752 } 23753 mp = tcp_ack_mp(tcp); 23754 23755 if (mp != NULL) { 23756 BUMP_LOCAL(tcp->tcp_obsegs); 23757 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23758 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23759 tcp_send_data(tcp, tcp->tcp_wq, mp); 23760 } 23761 } 23762 23763 23764 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23765 static mblk_t * 23766 tcp_ack_mp(tcp_t *tcp) 23767 { 23768 uint32_t seq_no; 23769 tcp_stack_t *tcps = tcp->tcp_tcps; 23770 23771 /* 23772 * There are a few cases to be considered while setting the sequence no. 23773 * Essentially, we can come here while processing an unacceptable pkt 23774 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23775 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23776 * If we are here for a zero window probe, stick with suna. In all 23777 * other cases, we check if suna + swnd encompasses snxt and set 23778 * the sequence number to snxt, if so. If snxt falls outside the 23779 * window (the receiver probably shrunk its window), we will go with 23780 * suna + swnd, otherwise the sequence no will be unacceptable to the 23781 * receiver. 23782 */ 23783 if (tcp->tcp_zero_win_probe) { 23784 seq_no = tcp->tcp_suna; 23785 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23786 ASSERT(tcp->tcp_swnd == 0); 23787 seq_no = tcp->tcp_snxt; 23788 } else { 23789 seq_no = SEQ_GT(tcp->tcp_snxt, 23790 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23791 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23792 } 23793 23794 if (tcp->tcp_valid_bits) { 23795 /* 23796 * For the complex case where we have to send some 23797 * controls (FIN or SYN), let tcp_xmit_mp do it. 23798 */ 23799 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23800 NULL, B_FALSE)); 23801 } else { 23802 /* Generate a simple ACK */ 23803 int data_length; 23804 uchar_t *rptr; 23805 tcph_t *tcph; 23806 mblk_t *mp1; 23807 int32_t tcp_hdr_len; 23808 int32_t tcp_tcp_hdr_len; 23809 int32_t num_sack_blk = 0; 23810 int32_t sack_opt_len; 23811 23812 /* 23813 * Allocate space for TCP + IP headers 23814 * and link-level header 23815 */ 23816 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23817 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23818 tcp->tcp_num_sack_blk); 23819 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23820 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23821 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23822 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23823 } else { 23824 tcp_hdr_len = tcp->tcp_hdr_len; 23825 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23826 } 23827 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23828 if (!mp1) 23829 return (NULL); 23830 23831 /* Update the latest receive window size in TCP header. */ 23832 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23833 tcp->tcp_tcph->th_win); 23834 /* copy in prototype TCP + IP header */ 23835 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23836 mp1->b_rptr = rptr; 23837 mp1->b_wptr = rptr + tcp_hdr_len; 23838 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23839 23840 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23841 23842 /* Set the TCP sequence number. */ 23843 U32_TO_ABE32(seq_no, tcph->th_seq); 23844 23845 /* Set up the TCP flag field. */ 23846 tcph->th_flags[0] = (uchar_t)TH_ACK; 23847 if (tcp->tcp_ecn_echo_on) 23848 tcph->th_flags[0] |= TH_ECE; 23849 23850 tcp->tcp_rack = tcp->tcp_rnxt; 23851 tcp->tcp_rack_cnt = 0; 23852 23853 /* fill in timestamp option if in use */ 23854 if (tcp->tcp_snd_ts_ok) { 23855 uint32_t llbolt = (uint32_t)lbolt; 23856 23857 U32_TO_BE32(llbolt, 23858 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23859 U32_TO_BE32(tcp->tcp_ts_recent, 23860 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23861 } 23862 23863 /* Fill in SACK options */ 23864 if (num_sack_blk > 0) { 23865 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23866 sack_blk_t *tmp; 23867 int32_t i; 23868 23869 wptr[0] = TCPOPT_NOP; 23870 wptr[1] = TCPOPT_NOP; 23871 wptr[2] = TCPOPT_SACK; 23872 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23873 sizeof (sack_blk_t); 23874 wptr += TCPOPT_REAL_SACK_LEN; 23875 23876 tmp = tcp->tcp_sack_list; 23877 for (i = 0; i < num_sack_blk; i++) { 23878 U32_TO_BE32(tmp[i].begin, wptr); 23879 wptr += sizeof (tcp_seq); 23880 U32_TO_BE32(tmp[i].end, wptr); 23881 wptr += sizeof (tcp_seq); 23882 } 23883 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23884 << 4); 23885 } 23886 23887 if (tcp->tcp_ipversion == IPV4_VERSION) { 23888 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23889 } else { 23890 /* Check for ip6i_t header in sticky hdrs */ 23891 ip6_t *ip6 = (ip6_t *)(rptr + 23892 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23893 sizeof (ip6i_t) : 0)); 23894 23895 ip6->ip6_plen = htons(tcp_hdr_len - 23896 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23897 } 23898 23899 /* 23900 * Prime pump for checksum calculation in IP. Include the 23901 * adjustment for a source route if any. 23902 */ 23903 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23904 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23905 U16_TO_ABE16(data_length, tcph->th_sum); 23906 23907 if (tcp->tcp_ip_forward_progress) { 23908 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23909 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23910 tcp->tcp_ip_forward_progress = B_FALSE; 23911 } 23912 return (mp1); 23913 } 23914 } 23915 23916 /* 23917 * Hash list insertion routine for tcp_t structures. Each hash bucket 23918 * contains a list of tcp_t entries, and each entry is bound to a unique 23919 * port. If there are multiple tcp_t's that are bound to the same port, then 23920 * one of them will be linked into the hash bucket list, and the rest will 23921 * hang off of that one entry. For each port, entries bound to a specific IP 23922 * address will be inserted before those those bound to INADDR_ANY. 23923 */ 23924 static void 23925 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23926 { 23927 tcp_t **tcpp; 23928 tcp_t *tcpnext; 23929 tcp_t *tcphash; 23930 23931 if (tcp->tcp_ptpbhn != NULL) { 23932 ASSERT(!caller_holds_lock); 23933 tcp_bind_hash_remove(tcp); 23934 } 23935 tcpp = &tbf->tf_tcp; 23936 if (!caller_holds_lock) { 23937 mutex_enter(&tbf->tf_lock); 23938 } else { 23939 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23940 } 23941 tcphash = tcpp[0]; 23942 tcpnext = NULL; 23943 if (tcphash != NULL) { 23944 /* Look for an entry using the same port */ 23945 while ((tcphash = tcpp[0]) != NULL && 23946 tcp->tcp_lport != tcphash->tcp_lport) 23947 tcpp = &(tcphash->tcp_bind_hash); 23948 23949 /* The port was not found, just add to the end */ 23950 if (tcphash == NULL) 23951 goto insert; 23952 23953 /* 23954 * OK, there already exists an entry bound to the 23955 * same port. 23956 * 23957 * If the new tcp bound to the INADDR_ANY address 23958 * and the first one in the list is not bound to 23959 * INADDR_ANY we skip all entries until we find the 23960 * first one bound to INADDR_ANY. 23961 * This makes sure that applications binding to a 23962 * specific address get preference over those binding to 23963 * INADDR_ANY. 23964 */ 23965 tcpnext = tcphash; 23966 tcphash = NULL; 23967 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23968 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23969 while ((tcpnext = tcpp[0]) != NULL && 23970 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23971 tcpp = &(tcpnext->tcp_bind_hash_port); 23972 23973 if (tcpnext) { 23974 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23975 tcphash = tcpnext->tcp_bind_hash; 23976 if (tcphash != NULL) { 23977 tcphash->tcp_ptpbhn = 23978 &(tcp->tcp_bind_hash); 23979 tcpnext->tcp_bind_hash = NULL; 23980 } 23981 } 23982 } else { 23983 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23984 tcphash = tcpnext->tcp_bind_hash; 23985 if (tcphash != NULL) { 23986 tcphash->tcp_ptpbhn = 23987 &(tcp->tcp_bind_hash); 23988 tcpnext->tcp_bind_hash = NULL; 23989 } 23990 } 23991 } 23992 insert: 23993 tcp->tcp_bind_hash_port = tcpnext; 23994 tcp->tcp_bind_hash = tcphash; 23995 tcp->tcp_ptpbhn = tcpp; 23996 tcpp[0] = tcp; 23997 if (!caller_holds_lock) 23998 mutex_exit(&tbf->tf_lock); 23999 } 24000 24001 /* 24002 * Hash list removal routine for tcp_t structures. 24003 */ 24004 static void 24005 tcp_bind_hash_remove(tcp_t *tcp) 24006 { 24007 tcp_t *tcpnext; 24008 kmutex_t *lockp; 24009 tcp_stack_t *tcps = tcp->tcp_tcps; 24010 24011 if (tcp->tcp_ptpbhn == NULL) 24012 return; 24013 24014 /* 24015 * Extract the lock pointer in case there are concurrent 24016 * hash_remove's for this instance. 24017 */ 24018 ASSERT(tcp->tcp_lport != 0); 24019 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 24020 24021 ASSERT(lockp != NULL); 24022 mutex_enter(lockp); 24023 if (tcp->tcp_ptpbhn) { 24024 tcpnext = tcp->tcp_bind_hash_port; 24025 if (tcpnext != NULL) { 24026 tcp->tcp_bind_hash_port = NULL; 24027 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24028 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 24029 if (tcpnext->tcp_bind_hash != NULL) { 24030 tcpnext->tcp_bind_hash->tcp_ptpbhn = 24031 &(tcpnext->tcp_bind_hash); 24032 tcp->tcp_bind_hash = NULL; 24033 } 24034 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 24035 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 24036 tcp->tcp_bind_hash = NULL; 24037 } 24038 *tcp->tcp_ptpbhn = tcpnext; 24039 tcp->tcp_ptpbhn = NULL; 24040 } 24041 mutex_exit(lockp); 24042 } 24043 24044 24045 /* 24046 * Hash list lookup routine for tcp_t structures. 24047 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 24048 */ 24049 static tcp_t * 24050 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 24051 { 24052 tf_t *tf; 24053 tcp_t *tcp; 24054 24055 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24056 mutex_enter(&tf->tf_lock); 24057 for (tcp = tf->tf_tcp; tcp != NULL; 24058 tcp = tcp->tcp_acceptor_hash) { 24059 if (tcp->tcp_acceptor_id == id) { 24060 CONN_INC_REF(tcp->tcp_connp); 24061 mutex_exit(&tf->tf_lock); 24062 return (tcp); 24063 } 24064 } 24065 mutex_exit(&tf->tf_lock); 24066 return (NULL); 24067 } 24068 24069 24070 /* 24071 * Hash list insertion routine for tcp_t structures. 24072 */ 24073 void 24074 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 24075 { 24076 tf_t *tf; 24077 tcp_t **tcpp; 24078 tcp_t *tcpnext; 24079 tcp_stack_t *tcps = tcp->tcp_tcps; 24080 24081 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 24082 24083 if (tcp->tcp_ptpahn != NULL) 24084 tcp_acceptor_hash_remove(tcp); 24085 tcpp = &tf->tf_tcp; 24086 mutex_enter(&tf->tf_lock); 24087 tcpnext = tcpp[0]; 24088 if (tcpnext) 24089 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 24090 tcp->tcp_acceptor_hash = tcpnext; 24091 tcp->tcp_ptpahn = tcpp; 24092 tcpp[0] = tcp; 24093 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 24094 mutex_exit(&tf->tf_lock); 24095 } 24096 24097 /* 24098 * Hash list removal routine for tcp_t structures. 24099 */ 24100 static void 24101 tcp_acceptor_hash_remove(tcp_t *tcp) 24102 { 24103 tcp_t *tcpnext; 24104 kmutex_t *lockp; 24105 24106 /* 24107 * Extract the lock pointer in case there are concurrent 24108 * hash_remove's for this instance. 24109 */ 24110 lockp = tcp->tcp_acceptor_lockp; 24111 24112 if (tcp->tcp_ptpahn == NULL) 24113 return; 24114 24115 ASSERT(lockp != NULL); 24116 mutex_enter(lockp); 24117 if (tcp->tcp_ptpahn) { 24118 tcpnext = tcp->tcp_acceptor_hash; 24119 if (tcpnext) { 24120 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24121 tcp->tcp_acceptor_hash = NULL; 24122 } 24123 *tcp->tcp_ptpahn = tcpnext; 24124 tcp->tcp_ptpahn = NULL; 24125 } 24126 mutex_exit(lockp); 24127 tcp->tcp_acceptor_lockp = NULL; 24128 } 24129 24130 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24131 24132 static ipaddr_t netmasks[] = { 24133 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24134 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24135 }; 24136 24137 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24138 24139 /* 24140 * XXX This routine should go away and instead we should use the metrics 24141 * associated with the routes to determine the default sndspace and rcvspace. 24142 */ 24143 static tcp_hsp_t * 24144 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24145 { 24146 tcp_hsp_t *hsp = NULL; 24147 24148 /* Quick check without acquiring the lock. */ 24149 if (tcps->tcps_hsp_hash == NULL) 24150 return (NULL); 24151 24152 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24153 24154 /* This routine finds the best-matching HSP for address addr. */ 24155 24156 if (tcps->tcps_hsp_hash) { 24157 int i; 24158 ipaddr_t srchaddr; 24159 tcp_hsp_t *hsp_net; 24160 24161 /* We do three passes: host, network, and subnet. */ 24162 24163 srchaddr = addr; 24164 24165 for (i = 1; i <= 3; i++) { 24166 /* Look for exact match on srchaddr */ 24167 24168 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24169 while (hsp) { 24170 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24171 hsp->tcp_hsp_addr == srchaddr) 24172 break; 24173 hsp = hsp->tcp_hsp_next; 24174 } 24175 ASSERT(hsp == NULL || 24176 hsp->tcp_hsp_vers == IPV4_VERSION); 24177 24178 /* 24179 * If this is the first pass: 24180 * If we found a match, great, return it. 24181 * If not, search for the network on the second pass. 24182 */ 24183 24184 if (i == 1) 24185 if (hsp) 24186 break; 24187 else 24188 { 24189 srchaddr = addr & netmask(addr); 24190 continue; 24191 } 24192 24193 /* 24194 * If this is the second pass: 24195 * If we found a match, but there's a subnet mask, 24196 * save the match but try again using the subnet 24197 * mask on the third pass. 24198 * Otherwise, return whatever we found. 24199 */ 24200 24201 if (i == 2) { 24202 if (hsp && hsp->tcp_hsp_subnet) { 24203 hsp_net = hsp; 24204 srchaddr = addr & hsp->tcp_hsp_subnet; 24205 continue; 24206 } else { 24207 break; 24208 } 24209 } 24210 24211 /* 24212 * This must be the third pass. If we didn't find 24213 * anything, return the saved network HSP instead. 24214 */ 24215 24216 if (!hsp) 24217 hsp = hsp_net; 24218 } 24219 } 24220 24221 rw_exit(&tcps->tcps_hsp_lock); 24222 return (hsp); 24223 } 24224 24225 /* 24226 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24227 * match lookup. 24228 */ 24229 static tcp_hsp_t * 24230 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24231 { 24232 tcp_hsp_t *hsp = NULL; 24233 24234 /* Quick check without acquiring the lock. */ 24235 if (tcps->tcps_hsp_hash == NULL) 24236 return (NULL); 24237 24238 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24239 24240 /* This routine finds the best-matching HSP for address addr. */ 24241 24242 if (tcps->tcps_hsp_hash) { 24243 int i; 24244 in6_addr_t v6srchaddr; 24245 tcp_hsp_t *hsp_net; 24246 24247 /* We do three passes: host, network, and subnet. */ 24248 24249 v6srchaddr = *v6addr; 24250 24251 for (i = 1; i <= 3; i++) { 24252 /* Look for exact match on srchaddr */ 24253 24254 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24255 V4_PART_OF_V6(v6srchaddr))]; 24256 while (hsp) { 24257 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24258 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24259 &v6srchaddr)) 24260 break; 24261 hsp = hsp->tcp_hsp_next; 24262 } 24263 24264 /* 24265 * If this is the first pass: 24266 * If we found a match, great, return it. 24267 * If not, search for the network on the second pass. 24268 */ 24269 24270 if (i == 1) 24271 if (hsp) 24272 break; 24273 else { 24274 /* Assume a 64 bit mask */ 24275 v6srchaddr.s6_addr32[0] = 24276 v6addr->s6_addr32[0]; 24277 v6srchaddr.s6_addr32[1] = 24278 v6addr->s6_addr32[1]; 24279 v6srchaddr.s6_addr32[2] = 0; 24280 v6srchaddr.s6_addr32[3] = 0; 24281 continue; 24282 } 24283 24284 /* 24285 * If this is the second pass: 24286 * If we found a match, but there's a subnet mask, 24287 * save the match but try again using the subnet 24288 * mask on the third pass. 24289 * Otherwise, return whatever we found. 24290 */ 24291 24292 if (i == 2) { 24293 ASSERT(hsp == NULL || 24294 hsp->tcp_hsp_vers == IPV6_VERSION); 24295 if (hsp && 24296 !IN6_IS_ADDR_UNSPECIFIED( 24297 &hsp->tcp_hsp_subnet_v6)) { 24298 hsp_net = hsp; 24299 V6_MASK_COPY(*v6addr, 24300 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24301 continue; 24302 } else { 24303 break; 24304 } 24305 } 24306 24307 /* 24308 * This must be the third pass. If we didn't find 24309 * anything, return the saved network HSP instead. 24310 */ 24311 24312 if (!hsp) 24313 hsp = hsp_net; 24314 } 24315 } 24316 24317 rw_exit(&tcps->tcps_hsp_lock); 24318 return (hsp); 24319 } 24320 24321 /* 24322 * Type three generator adapted from the random() function in 4.4 BSD: 24323 */ 24324 24325 /* 24326 * Copyright (c) 1983, 1993 24327 * The Regents of the University of California. All rights reserved. 24328 * 24329 * Redistribution and use in source and binary forms, with or without 24330 * modification, are permitted provided that the following conditions 24331 * are met: 24332 * 1. Redistributions of source code must retain the above copyright 24333 * notice, this list of conditions and the following disclaimer. 24334 * 2. Redistributions in binary form must reproduce the above copyright 24335 * notice, this list of conditions and the following disclaimer in the 24336 * documentation and/or other materials provided with the distribution. 24337 * 3. All advertising materials mentioning features or use of this software 24338 * must display the following acknowledgement: 24339 * This product includes software developed by the University of 24340 * California, Berkeley and its contributors. 24341 * 4. Neither the name of the University nor the names of its contributors 24342 * may be used to endorse or promote products derived from this software 24343 * without specific prior written permission. 24344 * 24345 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24346 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24347 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24348 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24349 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24350 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24351 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24352 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24353 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24354 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24355 * SUCH DAMAGE. 24356 */ 24357 24358 /* Type 3 -- x**31 + x**3 + 1 */ 24359 #define DEG_3 31 24360 #define SEP_3 3 24361 24362 24363 /* Protected by tcp_random_lock */ 24364 static int tcp_randtbl[DEG_3 + 1]; 24365 24366 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24367 static int *tcp_random_rptr = &tcp_randtbl[1]; 24368 24369 static int *tcp_random_state = &tcp_randtbl[1]; 24370 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24371 24372 kmutex_t tcp_random_lock; 24373 24374 void 24375 tcp_random_init(void) 24376 { 24377 int i; 24378 hrtime_t hrt; 24379 time_t wallclock; 24380 uint64_t result; 24381 24382 /* 24383 * Use high-res timer and current time for seed. Gethrtime() returns 24384 * a longlong, which may contain resolution down to nanoseconds. 24385 * The current time will either be a 32-bit or a 64-bit quantity. 24386 * XOR the two together in a 64-bit result variable. 24387 * Convert the result to a 32-bit value by multiplying the high-order 24388 * 32-bits by the low-order 32-bits. 24389 */ 24390 24391 hrt = gethrtime(); 24392 (void) drv_getparm(TIME, &wallclock); 24393 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24394 mutex_enter(&tcp_random_lock); 24395 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24396 (result & 0xffffffff); 24397 24398 for (i = 1; i < DEG_3; i++) 24399 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24400 + 12345; 24401 tcp_random_fptr = &tcp_random_state[SEP_3]; 24402 tcp_random_rptr = &tcp_random_state[0]; 24403 mutex_exit(&tcp_random_lock); 24404 for (i = 0; i < 10 * DEG_3; i++) 24405 (void) tcp_random(); 24406 } 24407 24408 /* 24409 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24410 * This range is selected to be approximately centered on TCP_ISS / 2, 24411 * and easy to compute. We get this value by generating a 32-bit random 24412 * number, selecting out the high-order 17 bits, and then adding one so 24413 * that we never return zero. 24414 */ 24415 int 24416 tcp_random(void) 24417 { 24418 int i; 24419 24420 mutex_enter(&tcp_random_lock); 24421 *tcp_random_fptr += *tcp_random_rptr; 24422 24423 /* 24424 * The high-order bits are more random than the low-order bits, 24425 * so we select out the high-order 17 bits and add one so that 24426 * we never return zero. 24427 */ 24428 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24429 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24430 tcp_random_fptr = tcp_random_state; 24431 ++tcp_random_rptr; 24432 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24433 tcp_random_rptr = tcp_random_state; 24434 24435 mutex_exit(&tcp_random_lock); 24436 return (i); 24437 } 24438 24439 static int 24440 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24441 int *t_errorp, int *sys_errorp) 24442 { 24443 int error; 24444 int is_absreq_failure; 24445 t_scalar_t *opt_lenp; 24446 t_scalar_t opt_offset; 24447 int prim_type; 24448 struct T_conn_req *tcreqp; 24449 struct T_conn_res *tcresp; 24450 cred_t *cr; 24451 24452 /* 24453 * All Solaris components should pass a db_credp 24454 * for this TPI message, hence we ASSERT. 24455 * But in case there is some other M_PROTO that looks 24456 * like a TPI message sent by some other kernel 24457 * component, we check and return an error. 24458 */ 24459 cr = msg_getcred(mp, NULL); 24460 ASSERT(cr != NULL); 24461 if (cr == NULL) 24462 return (-1); 24463 24464 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24465 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24466 prim_type == T_CONN_RES); 24467 24468 switch (prim_type) { 24469 case T_CONN_REQ: 24470 tcreqp = (struct T_conn_req *)mp->b_rptr; 24471 opt_offset = tcreqp->OPT_offset; 24472 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24473 break; 24474 case O_T_CONN_RES: 24475 case T_CONN_RES: 24476 tcresp = (struct T_conn_res *)mp->b_rptr; 24477 opt_offset = tcresp->OPT_offset; 24478 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24479 break; 24480 } 24481 24482 *t_errorp = 0; 24483 *sys_errorp = 0; 24484 *do_disconnectp = 0; 24485 24486 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24487 opt_offset, cr, &tcp_opt_obj, 24488 NULL, &is_absreq_failure); 24489 24490 switch (error) { 24491 case 0: /* no error */ 24492 ASSERT(is_absreq_failure == 0); 24493 return (0); 24494 case ENOPROTOOPT: 24495 *t_errorp = TBADOPT; 24496 break; 24497 case EACCES: 24498 *t_errorp = TACCES; 24499 break; 24500 default: 24501 *t_errorp = TSYSERR; *sys_errorp = error; 24502 break; 24503 } 24504 if (is_absreq_failure != 0) { 24505 /* 24506 * The connection request should get the local ack 24507 * T_OK_ACK and then a T_DISCON_IND. 24508 */ 24509 *do_disconnectp = 1; 24510 } 24511 return (-1); 24512 } 24513 24514 /* 24515 * Split this function out so that if the secret changes, I'm okay. 24516 * 24517 * Initialize the tcp_iss_cookie and tcp_iss_key. 24518 */ 24519 24520 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24521 24522 static void 24523 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24524 { 24525 struct { 24526 int32_t current_time; 24527 uint32_t randnum; 24528 uint16_t pad; 24529 uint8_t ether[6]; 24530 uint8_t passwd[PASSWD_SIZE]; 24531 } tcp_iss_cookie; 24532 time_t t; 24533 24534 /* 24535 * Start with the current absolute time. 24536 */ 24537 (void) drv_getparm(TIME, &t); 24538 tcp_iss_cookie.current_time = t; 24539 24540 /* 24541 * XXX - Need a more random number per RFC 1750, not this crap. 24542 * OTOH, if what follows is pretty random, then I'm in better shape. 24543 */ 24544 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24545 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24546 24547 /* 24548 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24549 * as a good template. 24550 */ 24551 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24552 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24553 24554 /* 24555 * The pass-phrase. Normally this is supplied by user-called NDD. 24556 */ 24557 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24558 24559 /* 24560 * See 4010593 if this section becomes a problem again, 24561 * but the local ethernet address is useful here. 24562 */ 24563 (void) localetheraddr(NULL, 24564 (struct ether_addr *)&tcp_iss_cookie.ether); 24565 24566 /* 24567 * Hash 'em all together. The MD5Final is called per-connection. 24568 */ 24569 mutex_enter(&tcps->tcps_iss_key_lock); 24570 MD5Init(&tcps->tcps_iss_key); 24571 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24572 sizeof (tcp_iss_cookie)); 24573 mutex_exit(&tcps->tcps_iss_key_lock); 24574 } 24575 24576 /* 24577 * Set the RFC 1948 pass phrase 24578 */ 24579 /* ARGSUSED */ 24580 static int 24581 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24582 cred_t *cr) 24583 { 24584 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24585 24586 /* 24587 * Basically, value contains a new pass phrase. Pass it along! 24588 */ 24589 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24590 return (0); 24591 } 24592 24593 /* ARGSUSED */ 24594 static int 24595 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24596 { 24597 bzero(buf, sizeof (tcp_sack_info_t)); 24598 return (0); 24599 } 24600 24601 /* ARGSUSED */ 24602 static int 24603 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24604 { 24605 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24606 return (0); 24607 } 24608 24609 /* 24610 * Make sure we wait until the default queue is setup, yet allow 24611 * tcp_g_q_create() to open a TCP stream. 24612 * We need to allow tcp_g_q_create() do do an open 24613 * of tcp, hence we compare curhread. 24614 * All others have to wait until the tcps_g_q has been 24615 * setup. 24616 */ 24617 void 24618 tcp_g_q_setup(tcp_stack_t *tcps) 24619 { 24620 mutex_enter(&tcps->tcps_g_q_lock); 24621 if (tcps->tcps_g_q != NULL) { 24622 mutex_exit(&tcps->tcps_g_q_lock); 24623 return; 24624 } 24625 if (tcps->tcps_g_q_creator == NULL) { 24626 /* This thread will set it up */ 24627 tcps->tcps_g_q_creator = curthread; 24628 mutex_exit(&tcps->tcps_g_q_lock); 24629 tcp_g_q_create(tcps); 24630 mutex_enter(&tcps->tcps_g_q_lock); 24631 ASSERT(tcps->tcps_g_q_creator == curthread); 24632 tcps->tcps_g_q_creator = NULL; 24633 cv_signal(&tcps->tcps_g_q_cv); 24634 ASSERT(tcps->tcps_g_q != NULL); 24635 mutex_exit(&tcps->tcps_g_q_lock); 24636 return; 24637 } 24638 /* Everybody but the creator has to wait */ 24639 if (tcps->tcps_g_q_creator != curthread) { 24640 while (tcps->tcps_g_q == NULL) 24641 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24642 } 24643 mutex_exit(&tcps->tcps_g_q_lock); 24644 } 24645 24646 #define IP "ip" 24647 24648 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24649 24650 /* 24651 * Create a default tcp queue here instead of in strplumb 24652 */ 24653 void 24654 tcp_g_q_create(tcp_stack_t *tcps) 24655 { 24656 int error; 24657 ldi_handle_t lh = NULL; 24658 ldi_ident_t li = NULL; 24659 int rval; 24660 cred_t *cr; 24661 major_t IP_MAJ; 24662 24663 #ifdef NS_DEBUG 24664 (void) printf("tcp_g_q_create()\n"); 24665 #endif 24666 24667 IP_MAJ = ddi_name_to_major(IP); 24668 24669 ASSERT(tcps->tcps_g_q_creator == curthread); 24670 24671 error = ldi_ident_from_major(IP_MAJ, &li); 24672 if (error) { 24673 #ifdef DEBUG 24674 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24675 error); 24676 #endif 24677 return; 24678 } 24679 24680 cr = zone_get_kcred(netstackid_to_zoneid( 24681 tcps->tcps_netstack->netstack_stackid)); 24682 ASSERT(cr != NULL); 24683 /* 24684 * We set the tcp default queue to IPv6 because IPv4 falls 24685 * back to IPv6 when it can't find a client, but 24686 * IPv6 does not fall back to IPv4. 24687 */ 24688 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24689 if (error) { 24690 #ifdef DEBUG 24691 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24692 error); 24693 #endif 24694 goto out; 24695 } 24696 24697 /* 24698 * This ioctl causes the tcp framework to cache a pointer to 24699 * this stream, so we don't want to close the stream after 24700 * this operation. 24701 * Use the kernel credentials that are for the zone we're in. 24702 */ 24703 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24704 (intptr_t)0, FKIOCTL, cr, &rval); 24705 if (error) { 24706 #ifdef DEBUG 24707 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24708 "error %d\n", error); 24709 #endif 24710 goto out; 24711 } 24712 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24713 lh = NULL; 24714 out: 24715 /* Close layered handles */ 24716 if (li) 24717 ldi_ident_release(li); 24718 /* Keep cred around until _inactive needs it */ 24719 tcps->tcps_g_q_cr = cr; 24720 } 24721 24722 /* 24723 * We keep tcp_g_q set until all other tcp_t's in the zone 24724 * has gone away, and then when tcp_g_q_inactive() is called 24725 * we clear it. 24726 */ 24727 void 24728 tcp_g_q_destroy(tcp_stack_t *tcps) 24729 { 24730 #ifdef NS_DEBUG 24731 (void) printf("tcp_g_q_destroy()for stack %d\n", 24732 tcps->tcps_netstack->netstack_stackid); 24733 #endif 24734 24735 if (tcps->tcps_g_q == NULL) { 24736 return; /* Nothing to cleanup */ 24737 } 24738 /* 24739 * Drop reference corresponding to the default queue. 24740 * This reference was added from tcp_open when the default queue 24741 * was created, hence we compensate for this extra drop in 24742 * tcp_g_q_close. If the refcnt drops to zero here it means 24743 * the default queue was the last one to be open, in which 24744 * case, then tcp_g_q_inactive will be 24745 * called as a result of the refrele. 24746 */ 24747 TCPS_REFRELE(tcps); 24748 } 24749 24750 /* 24751 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24752 * Run by tcp_q_q_inactive using a taskq. 24753 */ 24754 static void 24755 tcp_g_q_close(void *arg) 24756 { 24757 tcp_stack_t *tcps = arg; 24758 int error; 24759 ldi_handle_t lh = NULL; 24760 ldi_ident_t li = NULL; 24761 cred_t *cr; 24762 major_t IP_MAJ; 24763 24764 IP_MAJ = ddi_name_to_major(IP); 24765 24766 #ifdef NS_DEBUG 24767 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24768 tcps->tcps_netstack->netstack_stackid, 24769 tcps->tcps_netstack->netstack_refcnt); 24770 #endif 24771 lh = tcps->tcps_g_q_lh; 24772 if (lh == NULL) 24773 return; /* Nothing to cleanup */ 24774 24775 ASSERT(tcps->tcps_refcnt == 1); 24776 ASSERT(tcps->tcps_g_q != NULL); 24777 24778 error = ldi_ident_from_major(IP_MAJ, &li); 24779 if (error) { 24780 #ifdef DEBUG 24781 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24782 error); 24783 #endif 24784 return; 24785 } 24786 24787 cr = tcps->tcps_g_q_cr; 24788 tcps->tcps_g_q_cr = NULL; 24789 ASSERT(cr != NULL); 24790 24791 /* 24792 * Make sure we can break the recursion when tcp_close decrements 24793 * the reference count causing g_q_inactive to be called again. 24794 */ 24795 tcps->tcps_g_q_lh = NULL; 24796 24797 /* close the default queue */ 24798 (void) ldi_close(lh, FREAD|FWRITE, cr); 24799 /* 24800 * At this point in time tcps and the rest of netstack_t might 24801 * have been deleted. 24802 */ 24803 tcps = NULL; 24804 24805 /* Close layered handles */ 24806 ldi_ident_release(li); 24807 crfree(cr); 24808 } 24809 24810 /* 24811 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24812 * 24813 * Have to ensure that the ldi routines are not used by an 24814 * interrupt thread by using a taskq. 24815 */ 24816 void 24817 tcp_g_q_inactive(tcp_stack_t *tcps) 24818 { 24819 if (tcps->tcps_g_q_lh == NULL) 24820 return; /* Nothing to cleanup */ 24821 24822 ASSERT(tcps->tcps_refcnt == 0); 24823 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24824 24825 if (servicing_interrupt()) { 24826 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24827 (void *) tcps, TQ_SLEEP); 24828 } else { 24829 tcp_g_q_close(tcps); 24830 } 24831 } 24832 24833 /* 24834 * Called by IP when IP is loaded into the kernel 24835 */ 24836 void 24837 tcp_ddi_g_init(void) 24838 { 24839 tcp_timercache = kmem_cache_create("tcp_timercache", 24840 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24841 NULL, NULL, NULL, NULL, NULL, 0); 24842 24843 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24844 sizeof (tcp_sack_info_t), 0, 24845 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24846 24847 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24848 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24849 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24850 24851 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24852 24853 /* Initialize the random number generator */ 24854 tcp_random_init(); 24855 24856 /* A single callback independently of how many netstacks we have */ 24857 ip_squeue_init(tcp_squeue_add); 24858 24859 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24860 24861 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24862 TASKQ_PREPOPULATE); 24863 24864 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24865 24866 /* 24867 * We want to be informed each time a stack is created or 24868 * destroyed in the kernel, so we can maintain the 24869 * set of tcp_stack_t's. 24870 */ 24871 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24872 tcp_stack_fini); 24873 } 24874 24875 24876 #define INET_NAME "ip" 24877 24878 /* 24879 * Initialize the TCP stack instance. 24880 */ 24881 static void * 24882 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24883 { 24884 tcp_stack_t *tcps; 24885 tcpparam_t *pa; 24886 int i; 24887 int error = 0; 24888 major_t major; 24889 24890 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24891 tcps->tcps_netstack = ns; 24892 24893 /* Initialize locks */ 24894 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24895 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24896 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24897 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24898 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24899 24900 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24901 tcps->tcps_g_epriv_ports[0] = 2049; 24902 tcps->tcps_g_epriv_ports[1] = 4045; 24903 tcps->tcps_min_anonpriv_port = 512; 24904 24905 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24906 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24907 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24908 TCP_FANOUT_SIZE, KM_SLEEP); 24909 24910 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24911 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24912 MUTEX_DEFAULT, NULL); 24913 } 24914 24915 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24916 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24917 MUTEX_DEFAULT, NULL); 24918 } 24919 24920 /* TCP's IPsec code calls the packet dropper. */ 24921 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24922 24923 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24924 tcps->tcps_params = pa; 24925 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24926 24927 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24928 A_CNT(lcl_tcp_param_arr), tcps); 24929 24930 /* 24931 * Note: To really walk the device tree you need the devinfo 24932 * pointer to your device which is only available after probe/attach. 24933 * The following is safe only because it uses ddi_root_node() 24934 */ 24935 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24936 tcp_opt_obj.odb_opt_arr_cnt); 24937 24938 /* 24939 * Initialize RFC 1948 secret values. This will probably be reset once 24940 * by the boot scripts. 24941 * 24942 * Use NULL name, as the name is caught by the new lockstats. 24943 * 24944 * Initialize with some random, non-guessable string, like the global 24945 * T_INFO_ACK. 24946 */ 24947 24948 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24949 sizeof (tcp_g_t_info_ack), tcps); 24950 24951 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24952 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24953 24954 major = mod_name_to_major(INET_NAME); 24955 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24956 ASSERT(error == 0); 24957 return (tcps); 24958 } 24959 24960 /* 24961 * Called when the IP module is about to be unloaded. 24962 */ 24963 void 24964 tcp_ddi_g_destroy(void) 24965 { 24966 tcp_g_kstat_fini(tcp_g_kstat); 24967 tcp_g_kstat = NULL; 24968 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24969 24970 mutex_destroy(&tcp_random_lock); 24971 24972 kmem_cache_destroy(tcp_timercache); 24973 kmem_cache_destroy(tcp_sack_info_cache); 24974 kmem_cache_destroy(tcp_iphc_cache); 24975 24976 netstack_unregister(NS_TCP); 24977 taskq_destroy(tcp_taskq); 24978 } 24979 24980 /* 24981 * Shut down the TCP stack instance. 24982 */ 24983 /* ARGSUSED */ 24984 static void 24985 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24986 { 24987 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24988 24989 tcp_g_q_destroy(tcps); 24990 } 24991 24992 /* 24993 * Free the TCP stack instance. 24994 */ 24995 static void 24996 tcp_stack_fini(netstackid_t stackid, void *arg) 24997 { 24998 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24999 int i; 25000 25001 nd_free(&tcps->tcps_g_nd); 25002 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 25003 tcps->tcps_params = NULL; 25004 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 25005 tcps->tcps_wroff_xtra_param = NULL; 25006 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 25007 tcps->tcps_mdt_head_param = NULL; 25008 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 25009 tcps->tcps_mdt_tail_param = NULL; 25010 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 25011 tcps->tcps_mdt_max_pbufs_param = NULL; 25012 25013 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 25014 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 25015 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 25016 } 25017 25018 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 25019 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 25020 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 25021 } 25022 25023 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 25024 tcps->tcps_bind_fanout = NULL; 25025 25026 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 25027 tcps->tcps_acceptor_fanout = NULL; 25028 25029 mutex_destroy(&tcps->tcps_iss_key_lock); 25030 rw_destroy(&tcps->tcps_hsp_lock); 25031 mutex_destroy(&tcps->tcps_g_q_lock); 25032 cv_destroy(&tcps->tcps_g_q_cv); 25033 mutex_destroy(&tcps->tcps_epriv_port_lock); 25034 25035 ip_drop_unregister(&tcps->tcps_dropper); 25036 25037 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 25038 tcps->tcps_kstat = NULL; 25039 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 25040 25041 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 25042 tcps->tcps_mibkp = NULL; 25043 25044 ldi_ident_release(tcps->tcps_ldi_ident); 25045 kmem_free(tcps, sizeof (*tcps)); 25046 } 25047 25048 /* 25049 * Generate ISS, taking into account NDD changes may happen halfway through. 25050 * (If the iss is not zero, set it.) 25051 */ 25052 25053 static void 25054 tcp_iss_init(tcp_t *tcp) 25055 { 25056 MD5_CTX context; 25057 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 25058 uint32_t answer[4]; 25059 tcp_stack_t *tcps = tcp->tcp_tcps; 25060 25061 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 25062 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 25063 switch (tcps->tcps_strong_iss) { 25064 case 2: 25065 mutex_enter(&tcps->tcps_iss_key_lock); 25066 context = tcps->tcps_iss_key; 25067 mutex_exit(&tcps->tcps_iss_key_lock); 25068 arg.ports = tcp->tcp_ports; 25069 if (tcp->tcp_ipversion == IPV4_VERSION) { 25070 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 25071 &arg.src); 25072 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 25073 &arg.dst); 25074 } else { 25075 arg.src = tcp->tcp_ip6h->ip6_src; 25076 arg.dst = tcp->tcp_ip6h->ip6_dst; 25077 } 25078 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 25079 MD5Final((uchar_t *)answer, &context); 25080 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 25081 /* 25082 * Now that we've hashed into a unique per-connection sequence 25083 * space, add a random increment per strong_iss == 1. So I 25084 * guess we'll have to... 25085 */ 25086 /* FALLTHRU */ 25087 case 1: 25088 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 25089 break; 25090 default: 25091 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25092 break; 25093 } 25094 tcp->tcp_valid_bits = TCP_ISS_VALID; 25095 tcp->tcp_fss = tcp->tcp_iss - 1; 25096 tcp->tcp_suna = tcp->tcp_iss; 25097 tcp->tcp_snxt = tcp->tcp_iss + 1; 25098 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 25099 tcp->tcp_csuna = tcp->tcp_snxt; 25100 } 25101 25102 /* 25103 * Exported routine for extracting active tcp connection status. 25104 * 25105 * This is used by the Solaris Cluster Networking software to 25106 * gather a list of connections that need to be forwarded to 25107 * specific nodes in the cluster when configuration changes occur. 25108 * 25109 * The callback is invoked for each tcp_t structure from all netstacks, 25110 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 25111 * from the netstack with the specified stack_id. Returning 25112 * non-zero from the callback routine terminates the search. 25113 */ 25114 int 25115 cl_tcp_walk_list(netstackid_t stack_id, 25116 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 25117 { 25118 netstack_handle_t nh; 25119 netstack_t *ns; 25120 int ret = 0; 25121 25122 if (stack_id >= 0) { 25123 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 25124 return (EINVAL); 25125 25126 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25127 ns->netstack_tcp); 25128 netstack_rele(ns); 25129 return (ret); 25130 } 25131 25132 netstack_next_init(&nh); 25133 while ((ns = netstack_next(&nh)) != NULL) { 25134 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25135 ns->netstack_tcp); 25136 netstack_rele(ns); 25137 } 25138 netstack_next_fini(&nh); 25139 return (ret); 25140 } 25141 25142 static int 25143 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25144 tcp_stack_t *tcps) 25145 { 25146 tcp_t *tcp; 25147 cl_tcp_info_t cl_tcpi; 25148 connf_t *connfp; 25149 conn_t *connp; 25150 int i; 25151 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25152 25153 ASSERT(callback != NULL); 25154 25155 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25156 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25157 connp = NULL; 25158 25159 while ((connp = 25160 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25161 25162 tcp = connp->conn_tcp; 25163 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25164 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25165 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25166 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25167 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25168 /* 25169 * The macros tcp_laddr and tcp_faddr give the IPv4 25170 * addresses. They are copied implicitly below as 25171 * mapped addresses. 25172 */ 25173 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25174 if (tcp->tcp_ipversion == IPV4_VERSION) { 25175 cl_tcpi.cl_tcpi_faddr = 25176 tcp->tcp_ipha->ipha_dst; 25177 } else { 25178 cl_tcpi.cl_tcpi_faddr_v6 = 25179 tcp->tcp_ip6h->ip6_dst; 25180 } 25181 25182 /* 25183 * If the callback returns non-zero 25184 * we terminate the traversal. 25185 */ 25186 if ((*callback)(&cl_tcpi, arg) != 0) { 25187 CONN_DEC_REF(tcp->tcp_connp); 25188 return (1); 25189 } 25190 } 25191 } 25192 25193 return (0); 25194 } 25195 25196 /* 25197 * Macros used for accessing the different types of sockaddr 25198 * structures inside a tcp_ioc_abort_conn_t. 25199 */ 25200 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25201 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25202 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25203 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25204 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25205 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25206 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25207 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25208 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25209 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25210 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25211 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25212 25213 /* 25214 * Return the correct error code to mimic the behavior 25215 * of a connection reset. 25216 */ 25217 #define TCP_AC_GET_ERRCODE(state, err) { \ 25218 switch ((state)) { \ 25219 case TCPS_SYN_SENT: \ 25220 case TCPS_SYN_RCVD: \ 25221 (err) = ECONNREFUSED; \ 25222 break; \ 25223 case TCPS_ESTABLISHED: \ 25224 case TCPS_FIN_WAIT_1: \ 25225 case TCPS_FIN_WAIT_2: \ 25226 case TCPS_CLOSE_WAIT: \ 25227 (err) = ECONNRESET; \ 25228 break; \ 25229 case TCPS_CLOSING: \ 25230 case TCPS_LAST_ACK: \ 25231 case TCPS_TIME_WAIT: \ 25232 (err) = 0; \ 25233 break; \ 25234 default: \ 25235 (err) = ENXIO; \ 25236 } \ 25237 } 25238 25239 /* 25240 * Check if a tcp structure matches the info in acp. 25241 */ 25242 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25243 (((acp)->ac_local.ss_family == AF_INET) ? \ 25244 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25245 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25246 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25247 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25248 (TCP_AC_V4LPORT((acp)) == 0 || \ 25249 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25250 (TCP_AC_V4RPORT((acp)) == 0 || \ 25251 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25252 (acp)->ac_start <= (tcp)->tcp_state && \ 25253 (acp)->ac_end >= (tcp)->tcp_state) : \ 25254 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25255 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25256 &(tcp)->tcp_ip_src_v6)) && \ 25257 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25258 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25259 &(tcp)->tcp_remote_v6)) && \ 25260 (TCP_AC_V6LPORT((acp)) == 0 || \ 25261 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25262 (TCP_AC_V6RPORT((acp)) == 0 || \ 25263 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25264 (acp)->ac_start <= (tcp)->tcp_state && \ 25265 (acp)->ac_end >= (tcp)->tcp_state)) 25266 25267 #define TCP_AC_MATCH(acp, tcp) \ 25268 (((acp)->ac_zoneid == ALL_ZONES || \ 25269 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25270 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25271 25272 /* 25273 * Build a message containing a tcp_ioc_abort_conn_t structure 25274 * which is filled in with information from acp and tp. 25275 */ 25276 static mblk_t * 25277 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25278 { 25279 mblk_t *mp; 25280 tcp_ioc_abort_conn_t *tacp; 25281 25282 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25283 if (mp == NULL) 25284 return (NULL); 25285 25286 mp->b_datap->db_type = M_CTL; 25287 25288 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25289 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25290 sizeof (uint32_t)); 25291 25292 tacp->ac_start = acp->ac_start; 25293 tacp->ac_end = acp->ac_end; 25294 tacp->ac_zoneid = acp->ac_zoneid; 25295 25296 if (acp->ac_local.ss_family == AF_INET) { 25297 tacp->ac_local.ss_family = AF_INET; 25298 tacp->ac_remote.ss_family = AF_INET; 25299 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25300 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25301 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25302 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25303 } else { 25304 tacp->ac_local.ss_family = AF_INET6; 25305 tacp->ac_remote.ss_family = AF_INET6; 25306 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25307 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25308 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25309 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25310 } 25311 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25312 return (mp); 25313 } 25314 25315 /* 25316 * Print a tcp_ioc_abort_conn_t structure. 25317 */ 25318 static void 25319 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25320 { 25321 char lbuf[128]; 25322 char rbuf[128]; 25323 sa_family_t af; 25324 in_port_t lport, rport; 25325 ushort_t logflags; 25326 25327 af = acp->ac_local.ss_family; 25328 25329 if (af == AF_INET) { 25330 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25331 lbuf, 128); 25332 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25333 rbuf, 128); 25334 lport = ntohs(TCP_AC_V4LPORT(acp)); 25335 rport = ntohs(TCP_AC_V4RPORT(acp)); 25336 } else { 25337 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25338 lbuf, 128); 25339 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25340 rbuf, 128); 25341 lport = ntohs(TCP_AC_V6LPORT(acp)); 25342 rport = ntohs(TCP_AC_V6RPORT(acp)); 25343 } 25344 25345 logflags = SL_TRACE | SL_NOTE; 25346 /* 25347 * Don't print this message to the console if the operation was done 25348 * to a non-global zone. 25349 */ 25350 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25351 logflags |= SL_CONSOLE; 25352 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25353 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25354 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25355 acp->ac_start, acp->ac_end); 25356 } 25357 25358 /* 25359 * Called inside tcp_rput when a message built using 25360 * tcp_ioctl_abort_build_msg is put into a queue. 25361 * Note that when we get here there is no wildcard in acp any more. 25362 */ 25363 static void 25364 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25365 { 25366 tcp_ioc_abort_conn_t *acp; 25367 25368 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25369 if (tcp->tcp_state <= acp->ac_end) { 25370 /* 25371 * If we get here, we are already on the correct 25372 * squeue. This ioctl follows the following path 25373 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25374 * ->tcp_ioctl_abort->squeue_enter (if on a 25375 * different squeue) 25376 */ 25377 int errcode; 25378 25379 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25380 (void) tcp_clean_death(tcp, errcode, 26); 25381 } 25382 freemsg(mp); 25383 } 25384 25385 /* 25386 * Abort all matching connections on a hash chain. 25387 */ 25388 static int 25389 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25390 boolean_t exact, tcp_stack_t *tcps) 25391 { 25392 int nmatch, err = 0; 25393 tcp_t *tcp; 25394 MBLKP mp, last, listhead = NULL; 25395 conn_t *tconnp; 25396 connf_t *connfp; 25397 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25398 25399 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25400 25401 startover: 25402 nmatch = 0; 25403 25404 mutex_enter(&connfp->connf_lock); 25405 for (tconnp = connfp->connf_head; tconnp != NULL; 25406 tconnp = tconnp->conn_next) { 25407 tcp = tconnp->conn_tcp; 25408 if (TCP_AC_MATCH(acp, tcp)) { 25409 CONN_INC_REF(tcp->tcp_connp); 25410 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25411 if (mp == NULL) { 25412 err = ENOMEM; 25413 CONN_DEC_REF(tcp->tcp_connp); 25414 break; 25415 } 25416 mp->b_prev = (mblk_t *)tcp; 25417 25418 if (listhead == NULL) { 25419 listhead = mp; 25420 last = mp; 25421 } else { 25422 last->b_next = mp; 25423 last = mp; 25424 } 25425 nmatch++; 25426 if (exact) 25427 break; 25428 } 25429 25430 /* Avoid holding lock for too long. */ 25431 if (nmatch >= 500) 25432 break; 25433 } 25434 mutex_exit(&connfp->connf_lock); 25435 25436 /* Pass mp into the correct tcp */ 25437 while ((mp = listhead) != NULL) { 25438 listhead = listhead->b_next; 25439 tcp = (tcp_t *)mp->b_prev; 25440 mp->b_next = mp->b_prev = NULL; 25441 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 25442 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 25443 } 25444 25445 *count += nmatch; 25446 if (nmatch >= 500 && err == 0) 25447 goto startover; 25448 return (err); 25449 } 25450 25451 /* 25452 * Abort all connections that matches the attributes specified in acp. 25453 */ 25454 static int 25455 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25456 { 25457 sa_family_t af; 25458 uint32_t ports; 25459 uint16_t *pports; 25460 int err = 0, count = 0; 25461 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25462 int index = -1; 25463 ushort_t logflags; 25464 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25465 25466 af = acp->ac_local.ss_family; 25467 25468 if (af == AF_INET) { 25469 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25470 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25471 pports = (uint16_t *)&ports; 25472 pports[1] = TCP_AC_V4LPORT(acp); 25473 pports[0] = TCP_AC_V4RPORT(acp); 25474 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25475 } 25476 } else { 25477 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25478 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25479 pports = (uint16_t *)&ports; 25480 pports[1] = TCP_AC_V6LPORT(acp); 25481 pports[0] = TCP_AC_V6RPORT(acp); 25482 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25483 } 25484 } 25485 25486 /* 25487 * For cases where remote addr, local port, and remote port are non- 25488 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25489 */ 25490 if (index != -1) { 25491 err = tcp_ioctl_abort_bucket(acp, index, 25492 &count, exact, tcps); 25493 } else { 25494 /* 25495 * loop through all entries for wildcard case 25496 */ 25497 for (index = 0; 25498 index < ipst->ips_ipcl_conn_fanout_size; 25499 index++) { 25500 err = tcp_ioctl_abort_bucket(acp, index, 25501 &count, exact, tcps); 25502 if (err != 0) 25503 break; 25504 } 25505 } 25506 25507 logflags = SL_TRACE | SL_NOTE; 25508 /* 25509 * Don't print this message to the console if the operation was done 25510 * to a non-global zone. 25511 */ 25512 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25513 logflags |= SL_CONSOLE; 25514 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25515 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25516 if (err == 0 && count == 0) 25517 err = ENOENT; 25518 return (err); 25519 } 25520 25521 /* 25522 * Process the TCP_IOC_ABORT_CONN ioctl request. 25523 */ 25524 static void 25525 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25526 { 25527 int err; 25528 IOCP iocp; 25529 MBLKP mp1; 25530 sa_family_t laf, raf; 25531 tcp_ioc_abort_conn_t *acp; 25532 zone_t *zptr; 25533 conn_t *connp = Q_TO_CONN(q); 25534 zoneid_t zoneid = connp->conn_zoneid; 25535 tcp_t *tcp = connp->conn_tcp; 25536 tcp_stack_t *tcps = tcp->tcp_tcps; 25537 25538 iocp = (IOCP)mp->b_rptr; 25539 25540 if ((mp1 = mp->b_cont) == NULL || 25541 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25542 err = EINVAL; 25543 goto out; 25544 } 25545 25546 /* check permissions */ 25547 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25548 err = EPERM; 25549 goto out; 25550 } 25551 25552 if (mp1->b_cont != NULL) { 25553 freemsg(mp1->b_cont); 25554 mp1->b_cont = NULL; 25555 } 25556 25557 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25558 laf = acp->ac_local.ss_family; 25559 raf = acp->ac_remote.ss_family; 25560 25561 /* check that a zone with the supplied zoneid exists */ 25562 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25563 zptr = zone_find_by_id(zoneid); 25564 if (zptr != NULL) { 25565 zone_rele(zptr); 25566 } else { 25567 err = EINVAL; 25568 goto out; 25569 } 25570 } 25571 25572 /* 25573 * For exclusive stacks we set the zoneid to zero 25574 * to make TCP operate as if in the global zone. 25575 */ 25576 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25577 acp->ac_zoneid = GLOBAL_ZONEID; 25578 25579 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25580 acp->ac_start > acp->ac_end || laf != raf || 25581 (laf != AF_INET && laf != AF_INET6)) { 25582 err = EINVAL; 25583 goto out; 25584 } 25585 25586 tcp_ioctl_abort_dump(acp); 25587 err = tcp_ioctl_abort(acp, tcps); 25588 25589 out: 25590 if (mp1 != NULL) { 25591 freemsg(mp1); 25592 mp->b_cont = NULL; 25593 } 25594 25595 if (err != 0) 25596 miocnak(q, mp, 0, err); 25597 else 25598 miocack(q, mp, 0, 0); 25599 } 25600 25601 /* 25602 * tcp_time_wait_processing() handles processing of incoming packets when 25603 * the tcp is in the TIME_WAIT state. 25604 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25605 * on the time wait list. 25606 */ 25607 void 25608 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25609 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25610 { 25611 int32_t bytes_acked; 25612 int32_t gap; 25613 int32_t rgap; 25614 tcp_opt_t tcpopt; 25615 uint_t flags; 25616 uint32_t new_swnd = 0; 25617 conn_t *connp; 25618 tcp_stack_t *tcps = tcp->tcp_tcps; 25619 25620 BUMP_LOCAL(tcp->tcp_ibsegs); 25621 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25622 25623 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25624 new_swnd = BE16_TO_U16(tcph->th_win) << 25625 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25626 if (tcp->tcp_snd_ts_ok) { 25627 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25628 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25629 tcp->tcp_rnxt, TH_ACK); 25630 goto done; 25631 } 25632 } 25633 gap = seg_seq - tcp->tcp_rnxt; 25634 rgap = tcp->tcp_rwnd - (gap + seg_len); 25635 if (gap < 0) { 25636 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25637 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25638 (seg_len > -gap ? -gap : seg_len)); 25639 seg_len += gap; 25640 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25641 if (flags & TH_RST) { 25642 goto done; 25643 } 25644 if ((flags & TH_FIN) && seg_len == -1) { 25645 /* 25646 * When TCP receives a duplicate FIN in 25647 * TIME_WAIT state, restart the 2 MSL timer. 25648 * See page 73 in RFC 793. Make sure this TCP 25649 * is already on the TIME_WAIT list. If not, 25650 * just restart the timer. 25651 */ 25652 if (TCP_IS_DETACHED(tcp)) { 25653 if (tcp_time_wait_remove(tcp, NULL) == 25654 B_TRUE) { 25655 tcp_time_wait_append(tcp); 25656 TCP_DBGSTAT(tcps, 25657 tcp_rput_time_wait); 25658 } 25659 } else { 25660 ASSERT(tcp != NULL); 25661 TCP_TIMER_RESTART(tcp, 25662 tcps->tcps_time_wait_interval); 25663 } 25664 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25665 tcp->tcp_rnxt, TH_ACK); 25666 goto done; 25667 } 25668 flags |= TH_ACK_NEEDED; 25669 seg_len = 0; 25670 goto process_ack; 25671 } 25672 25673 /* Fix seg_seq, and chew the gap off the front. */ 25674 seg_seq = tcp->tcp_rnxt; 25675 } 25676 25677 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25678 /* 25679 * Make sure that when we accept the connection, pick 25680 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25681 * old connection. 25682 * 25683 * The next ISS generated is equal to tcp_iss_incr_extra 25684 * + ISS_INCR/2 + other components depending on the 25685 * value of tcp_strong_iss. We pre-calculate the new 25686 * ISS here and compare with tcp_snxt to determine if 25687 * we need to make adjustment to tcp_iss_incr_extra. 25688 * 25689 * The above calculation is ugly and is a 25690 * waste of CPU cycles... 25691 */ 25692 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25693 int32_t adj; 25694 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25695 25696 switch (tcps->tcps_strong_iss) { 25697 case 2: { 25698 /* Add time and MD5 components. */ 25699 uint32_t answer[4]; 25700 struct { 25701 uint32_t ports; 25702 in6_addr_t src; 25703 in6_addr_t dst; 25704 } arg; 25705 MD5_CTX context; 25706 25707 mutex_enter(&tcps->tcps_iss_key_lock); 25708 context = tcps->tcps_iss_key; 25709 mutex_exit(&tcps->tcps_iss_key_lock); 25710 arg.ports = tcp->tcp_ports; 25711 /* We use MAPPED addresses in tcp_iss_init */ 25712 arg.src = tcp->tcp_ip_src_v6; 25713 if (tcp->tcp_ipversion == IPV4_VERSION) { 25714 IN6_IPADDR_TO_V4MAPPED( 25715 tcp->tcp_ipha->ipha_dst, 25716 &arg.dst); 25717 } else { 25718 arg.dst = 25719 tcp->tcp_ip6h->ip6_dst; 25720 } 25721 MD5Update(&context, (uchar_t *)&arg, 25722 sizeof (arg)); 25723 MD5Final((uchar_t *)answer, &context); 25724 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25725 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25726 break; 25727 } 25728 case 1: 25729 /* Add time component and min random (i.e. 1). */ 25730 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25731 break; 25732 default: 25733 /* Add only time component. */ 25734 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25735 break; 25736 } 25737 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25738 /* 25739 * New ISS not guaranteed to be ISS_INCR/2 25740 * ahead of the current tcp_snxt, so add the 25741 * difference to tcp_iss_incr_extra. 25742 */ 25743 tcps->tcps_iss_incr_extra += adj; 25744 } 25745 /* 25746 * If tcp_clean_death() can not perform the task now, 25747 * drop the SYN packet and let the other side re-xmit. 25748 * Otherwise pass the SYN packet back in, since the 25749 * old tcp state has been cleaned up or freed. 25750 */ 25751 if (tcp_clean_death(tcp, 0, 27) == -1) 25752 goto done; 25753 /* 25754 * We will come back to tcp_rput_data 25755 * on the global queue. Packets destined 25756 * for the global queue will be checked 25757 * with global policy. But the policy for 25758 * this packet has already been checked as 25759 * this was destined for the detached 25760 * connection. We need to bypass policy 25761 * check this time by attaching a dummy 25762 * ipsec_in with ipsec_in_dont_check set. 25763 */ 25764 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25765 if (connp != NULL) { 25766 TCP_STAT(tcps, tcp_time_wait_syn_success); 25767 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25768 return; 25769 } 25770 goto done; 25771 } 25772 25773 /* 25774 * rgap is the amount of stuff received out of window. A negative 25775 * value is the amount out of window. 25776 */ 25777 if (rgap < 0) { 25778 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25779 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25780 /* Fix seg_len and make sure there is something left. */ 25781 seg_len += rgap; 25782 if (seg_len <= 0) { 25783 if (flags & TH_RST) { 25784 goto done; 25785 } 25786 flags |= TH_ACK_NEEDED; 25787 seg_len = 0; 25788 goto process_ack; 25789 } 25790 } 25791 /* 25792 * Check whether we can update tcp_ts_recent. This test is 25793 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25794 * Extensions for High Performance: An Update", Internet Draft. 25795 */ 25796 if (tcp->tcp_snd_ts_ok && 25797 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25798 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25799 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25800 tcp->tcp_last_rcv_lbolt = lbolt64; 25801 } 25802 25803 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25804 /* Always ack out of order packets */ 25805 flags |= TH_ACK_NEEDED; 25806 seg_len = 0; 25807 } else if (seg_len > 0) { 25808 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25809 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25810 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25811 } 25812 if (flags & TH_RST) { 25813 (void) tcp_clean_death(tcp, 0, 28); 25814 goto done; 25815 } 25816 if (flags & TH_SYN) { 25817 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25818 TH_RST|TH_ACK); 25819 /* 25820 * Do not delete the TCP structure if it is in 25821 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25822 */ 25823 goto done; 25824 } 25825 process_ack: 25826 if (flags & TH_ACK) { 25827 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25828 if (bytes_acked <= 0) { 25829 if (bytes_acked == 0 && seg_len == 0 && 25830 new_swnd == tcp->tcp_swnd) 25831 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25832 } else { 25833 /* Acks something not sent */ 25834 flags |= TH_ACK_NEEDED; 25835 } 25836 } 25837 if (flags & TH_ACK_NEEDED) { 25838 /* 25839 * Time to send an ack for some reason. 25840 */ 25841 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25842 tcp->tcp_rnxt, TH_ACK); 25843 } 25844 done: 25845 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25846 DB_CKSUMSTART(mp) = 0; 25847 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25848 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25849 } 25850 freemsg(mp); 25851 } 25852 25853 /* 25854 * TCP Timers Implementation. 25855 */ 25856 timeout_id_t 25857 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25858 { 25859 mblk_t *mp; 25860 tcp_timer_t *tcpt; 25861 tcp_t *tcp = connp->conn_tcp; 25862 25863 ASSERT(connp->conn_sqp != NULL); 25864 25865 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25866 25867 if (tcp->tcp_timercache == NULL) { 25868 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25869 } else { 25870 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25871 mp = tcp->tcp_timercache; 25872 tcp->tcp_timercache = mp->b_next; 25873 mp->b_next = NULL; 25874 ASSERT(mp->b_wptr == NULL); 25875 } 25876 25877 CONN_INC_REF(connp); 25878 tcpt = (tcp_timer_t *)mp->b_rptr; 25879 tcpt->connp = connp; 25880 tcpt->tcpt_proc = f; 25881 /* 25882 * TCP timers are normal timeouts. Plus, they do not require more than 25883 * a 10 millisecond resolution. By choosing a coarser resolution and by 25884 * rounding up the expiration to the next resolution boundary, we can 25885 * batch timers in the callout subsystem to make TCP timers more 25886 * efficient. The roundup also protects short timers from expiring too 25887 * early before they have a chance to be cancelled. 25888 */ 25889 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25890 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25891 25892 return ((timeout_id_t)mp); 25893 } 25894 25895 static void 25896 tcp_timer_callback(void *arg) 25897 { 25898 mblk_t *mp = (mblk_t *)arg; 25899 tcp_timer_t *tcpt; 25900 conn_t *connp; 25901 25902 tcpt = (tcp_timer_t *)mp->b_rptr; 25903 connp = tcpt->connp; 25904 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25905 SQ_FILL, SQTAG_TCP_TIMER); 25906 } 25907 25908 static void 25909 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25910 { 25911 tcp_timer_t *tcpt; 25912 conn_t *connp = (conn_t *)arg; 25913 tcp_t *tcp = connp->conn_tcp; 25914 25915 tcpt = (tcp_timer_t *)mp->b_rptr; 25916 ASSERT(connp == tcpt->connp); 25917 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25918 25919 /* 25920 * If the TCP has reached the closed state, don't proceed any 25921 * further. This TCP logically does not exist on the system. 25922 * tcpt_proc could for example access queues, that have already 25923 * been qprocoff'ed off. Also see comments at the start of tcp_input 25924 */ 25925 if (tcp->tcp_state != TCPS_CLOSED) { 25926 (*tcpt->tcpt_proc)(connp); 25927 } else { 25928 tcp->tcp_timer_tid = 0; 25929 } 25930 tcp_timer_free(connp->conn_tcp, mp); 25931 } 25932 25933 /* 25934 * There is potential race with untimeout and the handler firing at the same 25935 * time. The mblock may be freed by the handler while we are trying to use 25936 * it. But since both should execute on the same squeue, this race should not 25937 * occur. 25938 */ 25939 clock_t 25940 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25941 { 25942 mblk_t *mp = (mblk_t *)id; 25943 tcp_timer_t *tcpt; 25944 clock_t delta; 25945 25946 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25947 25948 if (mp == NULL) 25949 return (-1); 25950 25951 tcpt = (tcp_timer_t *)mp->b_rptr; 25952 ASSERT(tcpt->connp == connp); 25953 25954 delta = untimeout_default(tcpt->tcpt_tid, 0); 25955 25956 if (delta >= 0) { 25957 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25958 tcp_timer_free(connp->conn_tcp, mp); 25959 CONN_DEC_REF(connp); 25960 } 25961 25962 return (delta); 25963 } 25964 25965 /* 25966 * Allocate space for the timer event. The allocation looks like mblk, but it is 25967 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25968 * 25969 * Dealing with failures: If we can't allocate from the timer cache we try 25970 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25971 * points to b_rptr. 25972 * If we can't allocate anything using allocb_tryhard(), we perform a last 25973 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25974 * save the actual allocation size in b_datap. 25975 */ 25976 mblk_t * 25977 tcp_timermp_alloc(int kmflags) 25978 { 25979 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25980 kmflags & ~KM_PANIC); 25981 25982 if (mp != NULL) { 25983 mp->b_next = mp->b_prev = NULL; 25984 mp->b_rptr = (uchar_t *)(&mp[1]); 25985 mp->b_wptr = NULL; 25986 mp->b_datap = NULL; 25987 mp->b_queue = NULL; 25988 mp->b_cont = NULL; 25989 } else if (kmflags & KM_PANIC) { 25990 /* 25991 * Failed to allocate memory for the timer. Try allocating from 25992 * dblock caches. 25993 */ 25994 /* ipclassifier calls this from a constructor - hence no tcps */ 25995 TCP_G_STAT(tcp_timermp_allocfail); 25996 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25997 if (mp == NULL) { 25998 size_t size = 0; 25999 /* 26000 * Memory is really low. Try tryhard allocation. 26001 * 26002 * ipclassifier calls this from a constructor - 26003 * hence no tcps 26004 */ 26005 TCP_G_STAT(tcp_timermp_allocdblfail); 26006 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 26007 sizeof (tcp_timer_t), &size, kmflags); 26008 mp->b_rptr = (uchar_t *)(&mp[1]); 26009 mp->b_next = mp->b_prev = NULL; 26010 mp->b_wptr = (uchar_t *)-1; 26011 mp->b_datap = (dblk_t *)size; 26012 mp->b_queue = NULL; 26013 mp->b_cont = NULL; 26014 } 26015 ASSERT(mp->b_wptr != NULL); 26016 } 26017 /* ipclassifier calls this from a constructor - hence no tcps */ 26018 TCP_G_DBGSTAT(tcp_timermp_alloced); 26019 26020 return (mp); 26021 } 26022 26023 /* 26024 * Free per-tcp timer cache. 26025 * It can only contain entries from tcp_timercache. 26026 */ 26027 void 26028 tcp_timermp_free(tcp_t *tcp) 26029 { 26030 mblk_t *mp; 26031 26032 while ((mp = tcp->tcp_timercache) != NULL) { 26033 ASSERT(mp->b_wptr == NULL); 26034 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 26035 kmem_cache_free(tcp_timercache, mp); 26036 } 26037 } 26038 26039 /* 26040 * Free timer event. Put it on the per-tcp timer cache if there is not too many 26041 * events there already (currently at most two events are cached). 26042 * If the event is not allocated from the timer cache, free it right away. 26043 */ 26044 static void 26045 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 26046 { 26047 mblk_t *mp1 = tcp->tcp_timercache; 26048 26049 if (mp->b_wptr != NULL) { 26050 /* 26051 * This allocation is not from a timer cache, free it right 26052 * away. 26053 */ 26054 if (mp->b_wptr != (uchar_t *)-1) 26055 freeb(mp); 26056 else 26057 kmem_free(mp, (size_t)mp->b_datap); 26058 } else if (mp1 == NULL || mp1->b_next == NULL) { 26059 /* Cache this timer block for future allocations */ 26060 mp->b_rptr = (uchar_t *)(&mp[1]); 26061 mp->b_next = mp1; 26062 tcp->tcp_timercache = mp; 26063 } else { 26064 kmem_cache_free(tcp_timercache, mp); 26065 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 26066 } 26067 } 26068 26069 /* 26070 * End of TCP Timers implementation. 26071 */ 26072 26073 /* 26074 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 26075 * on the specified backing STREAMS q. Note, the caller may make the 26076 * decision to call based on the tcp_t.tcp_flow_stopped value which 26077 * when check outside the q's lock is only an advisory check ... 26078 */ 26079 void 26080 tcp_setqfull(tcp_t *tcp) 26081 { 26082 tcp_stack_t *tcps = tcp->tcp_tcps; 26083 conn_t *connp = tcp->tcp_connp; 26084 26085 if (tcp->tcp_closed) 26086 return; 26087 26088 if (IPCL_IS_NONSTR(connp)) { 26089 (*connp->conn_upcalls->su_txq_full) 26090 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 26091 tcp->tcp_flow_stopped = B_TRUE; 26092 } else { 26093 queue_t *q = tcp->tcp_wq; 26094 26095 if (!(q->q_flag & QFULL)) { 26096 mutex_enter(QLOCK(q)); 26097 if (!(q->q_flag & QFULL)) { 26098 /* still need to set QFULL */ 26099 q->q_flag |= QFULL; 26100 tcp->tcp_flow_stopped = B_TRUE; 26101 mutex_exit(QLOCK(q)); 26102 TCP_STAT(tcps, tcp_flwctl_on); 26103 } else { 26104 mutex_exit(QLOCK(q)); 26105 } 26106 } 26107 } 26108 } 26109 26110 void 26111 tcp_clrqfull(tcp_t *tcp) 26112 { 26113 conn_t *connp = tcp->tcp_connp; 26114 26115 if (tcp->tcp_closed) 26116 return; 26117 26118 if (IPCL_IS_NONSTR(connp)) { 26119 (*connp->conn_upcalls->su_txq_full) 26120 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 26121 tcp->tcp_flow_stopped = B_FALSE; 26122 } else { 26123 queue_t *q = tcp->tcp_wq; 26124 26125 if (q->q_flag & QFULL) { 26126 mutex_enter(QLOCK(q)); 26127 if (q->q_flag & QFULL) { 26128 q->q_flag &= ~QFULL; 26129 tcp->tcp_flow_stopped = B_FALSE; 26130 mutex_exit(QLOCK(q)); 26131 if (q->q_flag & QWANTW) 26132 qbackenable(q, 0); 26133 } else { 26134 mutex_exit(QLOCK(q)); 26135 } 26136 } 26137 } 26138 } 26139 26140 /* 26141 * kstats related to squeues i.e. not per IP instance 26142 */ 26143 static void * 26144 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26145 { 26146 kstat_t *ksp; 26147 26148 tcp_g_stat_t template = { 26149 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26150 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26151 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26152 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26153 }; 26154 26155 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26156 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26157 KSTAT_FLAG_VIRTUAL); 26158 26159 if (ksp == NULL) 26160 return (NULL); 26161 26162 bcopy(&template, tcp_g_statp, sizeof (template)); 26163 ksp->ks_data = (void *)tcp_g_statp; 26164 26165 kstat_install(ksp); 26166 return (ksp); 26167 } 26168 26169 static void 26170 tcp_g_kstat_fini(kstat_t *ksp) 26171 { 26172 if (ksp != NULL) { 26173 kstat_delete(ksp); 26174 } 26175 } 26176 26177 26178 static void * 26179 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26180 { 26181 kstat_t *ksp; 26182 26183 tcp_stat_t template = { 26184 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26185 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26186 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26187 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26188 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26189 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26190 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26191 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26192 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26193 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26194 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26195 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26196 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26197 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26198 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26199 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26200 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26201 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26202 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26203 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26204 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26205 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26206 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26207 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26208 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26209 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26210 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26211 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26212 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26213 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26214 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26215 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26216 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26217 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26218 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26219 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26220 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26221 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26222 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26223 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26224 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26225 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26226 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26227 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26228 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26229 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26230 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26231 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26232 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26233 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26234 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26235 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26236 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26237 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26238 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26239 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26240 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26241 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26242 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26243 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26244 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26245 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26246 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26247 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26248 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26249 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26250 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26251 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26252 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26253 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26254 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26255 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26256 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26257 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26258 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26259 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26260 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26261 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26262 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26263 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26264 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26265 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26266 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26267 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26268 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26269 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26270 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26271 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26272 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26273 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26274 }; 26275 26276 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26277 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26278 KSTAT_FLAG_VIRTUAL, stackid); 26279 26280 if (ksp == NULL) 26281 return (NULL); 26282 26283 bcopy(&template, tcps_statisticsp, sizeof (template)); 26284 ksp->ks_data = (void *)tcps_statisticsp; 26285 ksp->ks_private = (void *)(uintptr_t)stackid; 26286 26287 kstat_install(ksp); 26288 return (ksp); 26289 } 26290 26291 static void 26292 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26293 { 26294 if (ksp != NULL) { 26295 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26296 kstat_delete_netstack(ksp, stackid); 26297 } 26298 } 26299 26300 /* 26301 * TCP Kstats implementation 26302 */ 26303 static void * 26304 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26305 { 26306 kstat_t *ksp; 26307 26308 tcp_named_kstat_t template = { 26309 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26310 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26311 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26312 { "maxConn", KSTAT_DATA_INT32, 0 }, 26313 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26314 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26315 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26316 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26317 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26318 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26319 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26320 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26321 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26322 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26323 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26324 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26325 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26326 { "outAck", KSTAT_DATA_UINT32, 0 }, 26327 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26328 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26329 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26330 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26331 { "outControl", KSTAT_DATA_UINT32, 0 }, 26332 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26333 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26334 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26335 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26336 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26337 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26338 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26339 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26340 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26341 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26342 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26343 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26344 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26345 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26346 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26347 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26348 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26349 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26350 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26351 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26352 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26353 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26354 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26355 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26356 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26357 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26358 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26359 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26360 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26361 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26362 }; 26363 26364 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26365 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26366 26367 if (ksp == NULL) 26368 return (NULL); 26369 26370 template.rtoAlgorithm.value.ui32 = 4; 26371 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26372 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26373 template.maxConn.value.i32 = -1; 26374 26375 bcopy(&template, ksp->ks_data, sizeof (template)); 26376 ksp->ks_update = tcp_kstat_update; 26377 ksp->ks_private = (void *)(uintptr_t)stackid; 26378 26379 kstat_install(ksp); 26380 return (ksp); 26381 } 26382 26383 static void 26384 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26385 { 26386 if (ksp != NULL) { 26387 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26388 kstat_delete_netstack(ksp, stackid); 26389 } 26390 } 26391 26392 static int 26393 tcp_kstat_update(kstat_t *kp, int rw) 26394 { 26395 tcp_named_kstat_t *tcpkp; 26396 tcp_t *tcp; 26397 connf_t *connfp; 26398 conn_t *connp; 26399 int i; 26400 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26401 netstack_t *ns; 26402 tcp_stack_t *tcps; 26403 ip_stack_t *ipst; 26404 26405 if ((kp == NULL) || (kp->ks_data == NULL)) 26406 return (EIO); 26407 26408 if (rw == KSTAT_WRITE) 26409 return (EACCES); 26410 26411 ns = netstack_find_by_stackid(stackid); 26412 if (ns == NULL) 26413 return (-1); 26414 tcps = ns->netstack_tcp; 26415 if (tcps == NULL) { 26416 netstack_rele(ns); 26417 return (-1); 26418 } 26419 26420 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26421 26422 tcpkp->currEstab.value.ui32 = 0; 26423 26424 ipst = ns->netstack_ip; 26425 26426 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26427 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26428 connp = NULL; 26429 while ((connp = 26430 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26431 tcp = connp->conn_tcp; 26432 switch (tcp_snmp_state(tcp)) { 26433 case MIB2_TCP_established: 26434 case MIB2_TCP_closeWait: 26435 tcpkp->currEstab.value.ui32++; 26436 break; 26437 } 26438 } 26439 } 26440 26441 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26442 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26443 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26444 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26445 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26446 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26447 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26448 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26449 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26450 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26451 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26452 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26453 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26454 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26455 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26456 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26457 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26458 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26459 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26460 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26461 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26462 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26463 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26464 tcpkp->inDataInorderSegs.value.ui32 = 26465 tcps->tcps_mib.tcpInDataInorderSegs; 26466 tcpkp->inDataInorderBytes.value.ui32 = 26467 tcps->tcps_mib.tcpInDataInorderBytes; 26468 tcpkp->inDataUnorderSegs.value.ui32 = 26469 tcps->tcps_mib.tcpInDataUnorderSegs; 26470 tcpkp->inDataUnorderBytes.value.ui32 = 26471 tcps->tcps_mib.tcpInDataUnorderBytes; 26472 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26473 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26474 tcpkp->inDataPartDupSegs.value.ui32 = 26475 tcps->tcps_mib.tcpInDataPartDupSegs; 26476 tcpkp->inDataPartDupBytes.value.ui32 = 26477 tcps->tcps_mib.tcpInDataPartDupBytes; 26478 tcpkp->inDataPastWinSegs.value.ui32 = 26479 tcps->tcps_mib.tcpInDataPastWinSegs; 26480 tcpkp->inDataPastWinBytes.value.ui32 = 26481 tcps->tcps_mib.tcpInDataPastWinBytes; 26482 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26483 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26484 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26485 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26486 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26487 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26488 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26489 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26490 tcpkp->timKeepaliveProbe.value.ui32 = 26491 tcps->tcps_mib.tcpTimKeepaliveProbe; 26492 tcpkp->timKeepaliveDrop.value.ui32 = 26493 tcps->tcps_mib.tcpTimKeepaliveDrop; 26494 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26495 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26496 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26497 tcpkp->outSackRetransSegs.value.ui32 = 26498 tcps->tcps_mib.tcpOutSackRetransSegs; 26499 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26500 26501 netstack_rele(ns); 26502 return (0); 26503 } 26504 26505 void 26506 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26507 { 26508 uint16_t hdr_len; 26509 ipha_t *ipha; 26510 uint8_t *nexthdrp; 26511 tcph_t *tcph; 26512 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26513 26514 /* Already has an eager */ 26515 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26516 TCP_STAT(tcps, tcp_reinput_syn); 26517 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26518 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 26519 return; 26520 } 26521 26522 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26523 case IPV4_VERSION: 26524 ipha = (ipha_t *)mp->b_rptr; 26525 hdr_len = IPH_HDR_LENGTH(ipha); 26526 break; 26527 case IPV6_VERSION: 26528 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26529 &hdr_len, &nexthdrp)) { 26530 CONN_DEC_REF(connp); 26531 freemsg(mp); 26532 return; 26533 } 26534 break; 26535 } 26536 26537 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26538 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26539 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26540 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26541 } 26542 26543 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26544 SQ_FILL, SQTAG_TCP_REINPUT); 26545 } 26546 26547 static int 26548 tcp_squeue_switch(int val) 26549 { 26550 int rval = SQ_FILL; 26551 26552 switch (val) { 26553 case 1: 26554 rval = SQ_NODRAIN; 26555 break; 26556 case 2: 26557 rval = SQ_PROCESS; 26558 break; 26559 default: 26560 break; 26561 } 26562 return (rval); 26563 } 26564 26565 /* 26566 * This is called once for each squeue - globally for all stack 26567 * instances. 26568 */ 26569 static void 26570 tcp_squeue_add(squeue_t *sqp) 26571 { 26572 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26573 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26574 26575 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26576 tcp_time_wait->tcp_time_wait_tid = 26577 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26578 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26579 CALLOUT_FLAG_ROUNDUP); 26580 if (tcp_free_list_max_cnt == 0) { 26581 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26582 max_ncpus : boot_max_ncpus); 26583 26584 /* 26585 * Limit number of entries to 1% of availble memory / tcp_ncpus 26586 */ 26587 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26588 (tcp_ncpus * sizeof (tcp_t) * 100); 26589 } 26590 tcp_time_wait->tcp_free_list_cnt = 0; 26591 } 26592 26593 static int 26594 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 26595 { 26596 mblk_t *ire_mp = NULL; 26597 mblk_t *syn_mp; 26598 mblk_t *mdti; 26599 mblk_t *lsoi; 26600 int retval; 26601 tcph_t *tcph; 26602 uint32_t mss; 26603 queue_t *q = tcp->tcp_rq; 26604 conn_t *connp = tcp->tcp_connp; 26605 tcp_stack_t *tcps = tcp->tcp_tcps; 26606 26607 if (error == 0) { 26608 /* 26609 * Adapt Multidata information, if any. The 26610 * following tcp_mdt_update routine will free 26611 * the message. 26612 */ 26613 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26614 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26615 b_rptr)->mdt_capab, B_TRUE); 26616 freemsg(mdti); 26617 } 26618 26619 /* 26620 * Check to update LSO information with tcp, and 26621 * tcp_lso_update routine will free the message. 26622 */ 26623 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26624 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26625 b_rptr)->lso_capab); 26626 freemsg(lsoi); 26627 } 26628 26629 /* Get the IRE, if we had requested for it */ 26630 if (mp != NULL) 26631 ire_mp = tcp_ire_mp(&mp); 26632 26633 if (tcp->tcp_hard_binding) { 26634 tcp->tcp_hard_binding = B_FALSE; 26635 tcp->tcp_hard_bound = B_TRUE; 26636 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26637 if (retval != 0) { 26638 error = EADDRINUSE; 26639 goto bind_failed; 26640 } 26641 } else { 26642 if (ire_mp != NULL) 26643 freeb(ire_mp); 26644 goto after_syn_sent; 26645 } 26646 26647 retval = tcp_adapt_ire(tcp, ire_mp); 26648 if (ire_mp != NULL) 26649 freeb(ire_mp); 26650 if (retval == 0) { 26651 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26652 ENETUNREACH : EADDRNOTAVAIL); 26653 goto ipcl_rm; 26654 } 26655 /* 26656 * Don't let an endpoint connect to itself. 26657 * Also checked in tcp_connect() but that 26658 * check can't handle the case when the 26659 * local IP address is INADDR_ANY. 26660 */ 26661 if (tcp->tcp_ipversion == IPV4_VERSION) { 26662 if ((tcp->tcp_ipha->ipha_dst == 26663 tcp->tcp_ipha->ipha_src) && 26664 (BE16_EQL(tcp->tcp_tcph->th_lport, 26665 tcp->tcp_tcph->th_fport))) { 26666 error = EADDRNOTAVAIL; 26667 goto ipcl_rm; 26668 } 26669 } else { 26670 if (IN6_ARE_ADDR_EQUAL( 26671 &tcp->tcp_ip6h->ip6_dst, 26672 &tcp->tcp_ip6h->ip6_src) && 26673 (BE16_EQL(tcp->tcp_tcph->th_lport, 26674 tcp->tcp_tcph->th_fport))) { 26675 error = EADDRNOTAVAIL; 26676 goto ipcl_rm; 26677 } 26678 } 26679 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26680 /* 26681 * This should not be possible! Just for 26682 * defensive coding... 26683 */ 26684 if (tcp->tcp_state != TCPS_SYN_SENT) 26685 goto after_syn_sent; 26686 26687 if (is_system_labeled() && 26688 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26689 error = EHOSTUNREACH; 26690 goto ipcl_rm; 26691 } 26692 26693 /* 26694 * tcp_adapt_ire() does not adjust 26695 * for TCP/IP header length. 26696 */ 26697 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26698 26699 /* 26700 * Just make sure our rwnd is at 26701 * least tcp_recv_hiwat_mss * MSS 26702 * large, and round up to the nearest 26703 * MSS. 26704 * 26705 * We do the round up here because 26706 * we need to get the interface 26707 * MTU first before we can do the 26708 * round up. 26709 */ 26710 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26711 tcps->tcps_recv_hiwat_minmss * mss); 26712 if (!IPCL_IS_NONSTR(connp)) 26713 q->q_hiwat = tcp->tcp_rwnd; 26714 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26715 tcp_set_ws_value(tcp); 26716 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26717 tcp->tcp_tcph->th_win); 26718 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26719 tcp->tcp_snd_ws_ok = B_TRUE; 26720 26721 /* 26722 * Set tcp_snd_ts_ok to true 26723 * so that tcp_xmit_mp will 26724 * include the timestamp 26725 * option in the SYN segment. 26726 */ 26727 if (tcps->tcps_tstamp_always || 26728 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26729 tcp->tcp_snd_ts_ok = B_TRUE; 26730 } 26731 26732 /* 26733 * tcp_snd_sack_ok can be set in 26734 * tcp_adapt_ire() if the sack metric 26735 * is set. So check it here also. 26736 */ 26737 if (tcps->tcps_sack_permitted == 2 || 26738 tcp->tcp_snd_sack_ok) { 26739 if (tcp->tcp_sack_info == NULL) { 26740 tcp->tcp_sack_info = 26741 kmem_cache_alloc(tcp_sack_info_cache, 26742 KM_SLEEP); 26743 } 26744 tcp->tcp_snd_sack_ok = B_TRUE; 26745 } 26746 26747 /* 26748 * Should we use ECN? Note that the current 26749 * default value (SunOS 5.9) of tcp_ecn_permitted 26750 * is 1. The reason for doing this is that there 26751 * are equipments out there that will drop ECN 26752 * enabled IP packets. Setting it to 1 avoids 26753 * compatibility problems. 26754 */ 26755 if (tcps->tcps_ecn_permitted == 2) 26756 tcp->tcp_ecn_ok = B_TRUE; 26757 26758 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26759 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26760 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26761 if (syn_mp) { 26762 if (cr == NULL) { 26763 cr = tcp->tcp_cred; 26764 pid = tcp->tcp_cpid; 26765 } 26766 mblk_setcred(syn_mp, cr, pid); 26767 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26768 } 26769 after_syn_sent: 26770 if (mp != NULL) { 26771 ASSERT(mp->b_cont == NULL); 26772 freeb(mp); 26773 } 26774 return (error); 26775 } else { 26776 /* error */ 26777 if (tcp->tcp_debug) { 26778 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26779 "tcp_post_ip_bind: error == %d", error); 26780 } 26781 if (mp != NULL) { 26782 freeb(mp); 26783 } 26784 } 26785 26786 ipcl_rm: 26787 /* 26788 * Need to unbind with classifier since we were just 26789 * told that our bind succeeded. a.k.a error == 0 at the entry. 26790 */ 26791 tcp->tcp_hard_bound = B_FALSE; 26792 tcp->tcp_hard_binding = B_FALSE; 26793 26794 ipcl_hash_remove(connp); 26795 26796 bind_failed: 26797 tcp->tcp_state = TCPS_IDLE; 26798 if (tcp->tcp_ipversion == IPV4_VERSION) 26799 tcp->tcp_ipha->ipha_src = 0; 26800 else 26801 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26802 /* 26803 * Copy of the src addr. in tcp_t is needed since 26804 * the lookup funcs. can only look at tcp_t 26805 */ 26806 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26807 26808 tcph = tcp->tcp_tcph; 26809 tcph->th_lport[0] = 0; 26810 tcph->th_lport[1] = 0; 26811 tcp_bind_hash_remove(tcp); 26812 bzero(&connp->u_port, sizeof (connp->u_port)); 26813 /* blow away saved option results if any */ 26814 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26815 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26816 26817 conn_delete_ire(tcp->tcp_connp, NULL); 26818 26819 return (error); 26820 } 26821 26822 static int 26823 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26824 boolean_t bind_to_req_port_only, cred_t *cr) 26825 { 26826 in_port_t mlp_port; 26827 mlp_type_t addrtype, mlptype; 26828 boolean_t user_specified; 26829 in_port_t allocated_port; 26830 in_port_t requested_port = *requested_port_ptr; 26831 conn_t *connp; 26832 zone_t *zone; 26833 tcp_stack_t *tcps = tcp->tcp_tcps; 26834 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26835 26836 /* 26837 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26838 */ 26839 if (cr == NULL) 26840 cr = tcp->tcp_cred; 26841 /* 26842 * Get a valid port (within the anonymous range and should not 26843 * be a privileged one) to use if the user has not given a port. 26844 * If multiple threads are here, they may all start with 26845 * with the same initial port. But, it should be fine as long as 26846 * tcp_bindi will ensure that no two threads will be assigned 26847 * the same port. 26848 * 26849 * NOTE: XXX If a privileged process asks for an anonymous port, we 26850 * still check for ports only in the range > tcp_smallest_non_priv_port, 26851 * unless TCP_ANONPRIVBIND option is set. 26852 */ 26853 mlptype = mlptSingle; 26854 mlp_port = requested_port; 26855 if (requested_port == 0) { 26856 requested_port = tcp->tcp_anon_priv_bind ? 26857 tcp_get_next_priv_port(tcp) : 26858 tcp_update_next_port(tcps->tcps_next_port_to_try, 26859 tcp, B_TRUE); 26860 if (requested_port == 0) { 26861 return (-TNOADDR); 26862 } 26863 user_specified = B_FALSE; 26864 26865 /* 26866 * If the user went through one of the RPC interfaces to create 26867 * this socket and RPC is MLP in this zone, then give him an 26868 * anonymous MLP. 26869 */ 26870 connp = tcp->tcp_connp; 26871 if (connp->conn_anon_mlp && is_system_labeled()) { 26872 zone = crgetzone(cr); 26873 addrtype = tsol_mlp_addr_type(zone->zone_id, 26874 IPV6_VERSION, &v6addr, 26875 tcps->tcps_netstack->netstack_ip); 26876 if (addrtype == mlptSingle) { 26877 return (-TNOADDR); 26878 } 26879 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26880 PMAPPORT, addrtype); 26881 mlp_port = PMAPPORT; 26882 } 26883 } else { 26884 int i; 26885 boolean_t priv = B_FALSE; 26886 26887 /* 26888 * If the requested_port is in the well-known privileged range, 26889 * verify that the stream was opened by a privileged user. 26890 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26891 * but instead the code relies on: 26892 * - the fact that the address of the array and its size never 26893 * changes 26894 * - the atomic assignment of the elements of the array 26895 */ 26896 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26897 priv = B_TRUE; 26898 } else { 26899 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26900 if (requested_port == 26901 tcps->tcps_g_epriv_ports[i]) { 26902 priv = B_TRUE; 26903 break; 26904 } 26905 } 26906 } 26907 if (priv) { 26908 if (secpolicy_net_privaddr(cr, requested_port, 26909 IPPROTO_TCP) != 0) { 26910 if (tcp->tcp_debug) { 26911 (void) strlog(TCP_MOD_ID, 0, 1, 26912 SL_ERROR|SL_TRACE, 26913 "tcp_bind: no priv for port %d", 26914 requested_port); 26915 } 26916 return (-TACCES); 26917 } 26918 } 26919 user_specified = B_TRUE; 26920 26921 connp = tcp->tcp_connp; 26922 if (is_system_labeled()) { 26923 zone = crgetzone(cr); 26924 addrtype = tsol_mlp_addr_type(zone->zone_id, 26925 IPV6_VERSION, &v6addr, 26926 tcps->tcps_netstack->netstack_ip); 26927 if (addrtype == mlptSingle) { 26928 return (-TNOADDR); 26929 } 26930 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26931 requested_port, addrtype); 26932 } 26933 } 26934 26935 if (mlptype != mlptSingle) { 26936 if (secpolicy_net_bindmlp(cr) != 0) { 26937 if (tcp->tcp_debug) { 26938 (void) strlog(TCP_MOD_ID, 0, 1, 26939 SL_ERROR|SL_TRACE, 26940 "tcp_bind: no priv for multilevel port %d", 26941 requested_port); 26942 } 26943 return (-TACCES); 26944 } 26945 26946 /* 26947 * If we're specifically binding a shared IP address and the 26948 * port is MLP on shared addresses, then check to see if this 26949 * zone actually owns the MLP. Reject if not. 26950 */ 26951 if (mlptype == mlptShared && addrtype == mlptShared) { 26952 /* 26953 * No need to handle exclusive-stack zones since 26954 * ALL_ZONES only applies to the shared stack. 26955 */ 26956 zoneid_t mlpzone; 26957 26958 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26959 htons(mlp_port)); 26960 if (connp->conn_zoneid != mlpzone) { 26961 if (tcp->tcp_debug) { 26962 (void) strlog(TCP_MOD_ID, 0, 1, 26963 SL_ERROR|SL_TRACE, 26964 "tcp_bind: attempt to bind port " 26965 "%d on shared addr in zone %d " 26966 "(should be %d)", 26967 mlp_port, connp->conn_zoneid, 26968 mlpzone); 26969 } 26970 return (-TACCES); 26971 } 26972 } 26973 26974 if (!user_specified) { 26975 int err; 26976 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26977 requested_port, B_TRUE); 26978 if (err != 0) { 26979 if (tcp->tcp_debug) { 26980 (void) strlog(TCP_MOD_ID, 0, 1, 26981 SL_ERROR|SL_TRACE, 26982 "tcp_bind: cannot establish anon " 26983 "MLP for port %d", 26984 requested_port); 26985 } 26986 return (err); 26987 } 26988 connp->conn_anon_port = B_TRUE; 26989 } 26990 connp->conn_mlp_type = mlptype; 26991 } 26992 26993 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26994 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26995 26996 if (allocated_port == 0) { 26997 connp->conn_mlp_type = mlptSingle; 26998 if (connp->conn_anon_port) { 26999 connp->conn_anon_port = B_FALSE; 27000 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 27001 requested_port, B_FALSE); 27002 } 27003 if (bind_to_req_port_only) { 27004 if (tcp->tcp_debug) { 27005 (void) strlog(TCP_MOD_ID, 0, 1, 27006 SL_ERROR|SL_TRACE, 27007 "tcp_bind: requested addr busy"); 27008 } 27009 return (-TADDRBUSY); 27010 } else { 27011 /* If we are out of ports, fail the bind. */ 27012 if (tcp->tcp_debug) { 27013 (void) strlog(TCP_MOD_ID, 0, 1, 27014 SL_ERROR|SL_TRACE, 27015 "tcp_bind: out of ports?"); 27016 } 27017 return (-TNOADDR); 27018 } 27019 } 27020 27021 /* Pass the allocated port back */ 27022 *requested_port_ptr = allocated_port; 27023 return (0); 27024 } 27025 27026 static int 27027 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27028 boolean_t bind_to_req_port_only) 27029 { 27030 tcp_t *tcp = connp->conn_tcp; 27031 sin_t *sin; 27032 sin6_t *sin6; 27033 sin6_t sin6addr; 27034 in_port_t requested_port; 27035 ipaddr_t v4addr; 27036 in6_addr_t v6addr; 27037 uint_t origipversion; 27038 int error = 0; 27039 27040 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 27041 27042 if (tcp->tcp_state == TCPS_BOUND) { 27043 return (0); 27044 } else if (tcp->tcp_state > TCPS_BOUND) { 27045 if (tcp->tcp_debug) { 27046 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27047 "tcp_bind: bad state, %d", tcp->tcp_state); 27048 } 27049 return (-TOUTSTATE); 27050 } 27051 origipversion = tcp->tcp_ipversion; 27052 27053 if (sa != NULL && !OK_32PTR((char *)sa)) { 27054 if (tcp->tcp_debug) { 27055 (void) strlog(TCP_MOD_ID, 0, 1, 27056 SL_ERROR|SL_TRACE, 27057 "tcp_bind: bad address parameter, " 27058 "address %p, len %d", 27059 (void *)sa, len); 27060 } 27061 return (-TPROTO); 27062 } 27063 27064 switch (len) { 27065 case 0: /* request for a generic port */ 27066 if (tcp->tcp_family == AF_INET) { 27067 sin = (sin_t *)&sin6addr; 27068 *sin = sin_null; 27069 sin->sin_family = AF_INET; 27070 tcp->tcp_ipversion = IPV4_VERSION; 27071 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 27072 } else { 27073 ASSERT(tcp->tcp_family == AF_INET6); 27074 sin6 = (sin6_t *)&sin6addr; 27075 *sin6 = sin6_null; 27076 sin6->sin6_family = AF_INET6; 27077 tcp->tcp_ipversion = IPV6_VERSION; 27078 V6_SET_ZERO(v6addr); 27079 } 27080 requested_port = 0; 27081 break; 27082 27083 case sizeof (sin_t): /* Complete IPv4 address */ 27084 sin = (sin_t *)sa; 27085 /* 27086 * With sockets sockfs will accept bogus sin_family in 27087 * bind() and replace it with the family used in the socket 27088 * call. 27089 */ 27090 if (sin->sin_family != AF_INET || 27091 tcp->tcp_family != AF_INET) { 27092 return (EAFNOSUPPORT); 27093 } 27094 requested_port = ntohs(sin->sin_port); 27095 tcp->tcp_ipversion = IPV4_VERSION; 27096 v4addr = sin->sin_addr.s_addr; 27097 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 27098 break; 27099 27100 case sizeof (sin6_t): /* Complete IPv6 address */ 27101 sin6 = (sin6_t *)sa; 27102 if (sin6->sin6_family != AF_INET6 || 27103 tcp->tcp_family != AF_INET6) { 27104 return (EAFNOSUPPORT); 27105 } 27106 requested_port = ntohs(sin6->sin6_port); 27107 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 27108 IPV4_VERSION : IPV6_VERSION; 27109 v6addr = sin6->sin6_addr; 27110 break; 27111 27112 default: 27113 if (tcp->tcp_debug) { 27114 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27115 "tcp_bind: bad address length, %d", len); 27116 } 27117 return (EAFNOSUPPORT); 27118 /* return (-TBADADDR); */ 27119 } 27120 27121 tcp->tcp_bound_source_v6 = v6addr; 27122 27123 /* Check for change in ipversion */ 27124 if (origipversion != tcp->tcp_ipversion) { 27125 ASSERT(tcp->tcp_family == AF_INET6); 27126 error = tcp->tcp_ipversion == IPV6_VERSION ? 27127 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 27128 if (error) { 27129 return (ENOMEM); 27130 } 27131 } 27132 27133 /* 27134 * Initialize family specific fields. Copy of the src addr. 27135 * in tcp_t is needed for the lookup funcs. 27136 */ 27137 if (tcp->tcp_ipversion == IPV6_VERSION) { 27138 tcp->tcp_ip6h->ip6_src = v6addr; 27139 } else { 27140 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 27141 } 27142 tcp->tcp_ip_src_v6 = v6addr; 27143 27144 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 27145 27146 error = tcp_bind_select_lport(tcp, &requested_port, 27147 bind_to_req_port_only, cr); 27148 27149 return (error); 27150 } 27151 27152 /* 27153 * Return unix error is tli error is TSYSERR, otherwise return a negative 27154 * tli error. 27155 */ 27156 int 27157 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27158 boolean_t bind_to_req_port_only) 27159 { 27160 int error; 27161 tcp_t *tcp = connp->conn_tcp; 27162 27163 if (tcp->tcp_state >= TCPS_BOUND) { 27164 if (tcp->tcp_debug) { 27165 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27166 "tcp_bind: bad state, %d", tcp->tcp_state); 27167 } 27168 return (-TOUTSTATE); 27169 } 27170 27171 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 27172 if (error != 0) 27173 return (error); 27174 27175 ASSERT(tcp->tcp_state == TCPS_BOUND); 27176 27177 tcp->tcp_conn_req_max = 0; 27178 27179 /* 27180 * We need to make sure that the conn_recv is set to a non-null 27181 * value before we insert the conn into the classifier table. 27182 * This is to avoid a race with an incoming packet which does an 27183 * ipcl_classify(). 27184 */ 27185 connp->conn_recv = tcp_conn_request; 27186 27187 if (tcp->tcp_family == AF_INET6) { 27188 ASSERT(tcp->tcp_connp->conn_af_isv6); 27189 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27190 &tcp->tcp_bound_source_v6, 0, B_FALSE); 27191 } else { 27192 ASSERT(!tcp->tcp_connp->conn_af_isv6); 27193 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 27194 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 27195 } 27196 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27197 } 27198 27199 int 27200 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 27201 socklen_t len, cred_t *cr) 27202 { 27203 int error; 27204 conn_t *connp = (conn_t *)proto_handle; 27205 squeue_t *sqp = connp->conn_sqp; 27206 27207 /* All Solaris components should pass a cred for this operation. */ 27208 ASSERT(cr != NULL); 27209 27210 ASSERT(sqp != NULL); 27211 ASSERT(connp->conn_upper_handle != NULL); 27212 27213 error = squeue_synch_enter(sqp, connp, 0); 27214 if (error != 0) { 27215 /* failed to enter */ 27216 return (ENOSR); 27217 } 27218 27219 /* binding to a NULL address really means unbind */ 27220 if (sa == NULL) { 27221 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 27222 error = tcp_do_unbind(connp); 27223 else 27224 error = EINVAL; 27225 } else { 27226 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 27227 } 27228 27229 squeue_synch_exit(sqp, connp); 27230 27231 if (error < 0) { 27232 if (error == -TOUTSTATE) 27233 error = EINVAL; 27234 else 27235 error = proto_tlitosyserr(-error); 27236 } 27237 27238 return (error); 27239 } 27240 27241 /* 27242 * If the return value from this function is positive, it's a UNIX error. 27243 * Otherwise, if it's negative, then the absolute value is a TLI error. 27244 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 27245 */ 27246 int 27247 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 27248 cred_t *cr, pid_t pid) 27249 { 27250 tcp_t *tcp = connp->conn_tcp; 27251 sin_t *sin = (sin_t *)sa; 27252 sin6_t *sin6 = (sin6_t *)sa; 27253 ipaddr_t *dstaddrp; 27254 in_port_t dstport; 27255 uint_t srcid; 27256 int error = 0; 27257 27258 switch (len) { 27259 default: 27260 /* 27261 * Should never happen 27262 */ 27263 return (EINVAL); 27264 27265 case sizeof (sin_t): 27266 sin = (sin_t *)sa; 27267 if (sin->sin_port == 0) { 27268 return (-TBADADDR); 27269 } 27270 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 27271 return (EAFNOSUPPORT); 27272 } 27273 break; 27274 27275 case sizeof (sin6_t): 27276 sin6 = (sin6_t *)sa; 27277 if (sin6->sin6_port == 0) { 27278 return (-TBADADDR); 27279 } 27280 break; 27281 } 27282 /* 27283 * If we're connecting to an IPv4-mapped IPv6 address, we need to 27284 * make sure that the template IP header in the tcp structure is an 27285 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 27286 * need to this before we call tcp_bindi() so that the port lookup 27287 * code will look for ports in the correct port space (IPv4 and 27288 * IPv6 have separate port spaces). 27289 */ 27290 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 27291 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27292 int err = 0; 27293 27294 err = tcp_header_init_ipv4(tcp); 27295 if (err != 0) { 27296 error = ENOMEM; 27297 goto connect_failed; 27298 } 27299 if (tcp->tcp_lport != 0) 27300 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 27301 } 27302 27303 switch (tcp->tcp_state) { 27304 case TCPS_LISTEN: 27305 /* 27306 * Listening sockets are not allowed to issue connect(). 27307 */ 27308 if (IPCL_IS_NONSTR(connp)) 27309 return (EOPNOTSUPP); 27310 /* FALLTHRU */ 27311 case TCPS_IDLE: 27312 /* 27313 * We support quick connect, refer to comments in 27314 * tcp_connect_*() 27315 */ 27316 /* FALLTHRU */ 27317 case TCPS_BOUND: 27318 /* 27319 * We must bump the generation before the operation start. 27320 * This is done to ensure that any upcall made later on sends 27321 * up the right generation to the socket. 27322 */ 27323 SOCK_CONNID_BUMP(tcp->tcp_connid); 27324 27325 if (tcp->tcp_family == AF_INET6) { 27326 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27327 return (tcp_connect_ipv6(tcp, 27328 &sin6->sin6_addr, 27329 sin6->sin6_port, sin6->sin6_flowinfo, 27330 sin6->__sin6_src_id, sin6->sin6_scope_id, 27331 cr, pid)); 27332 } 27333 /* 27334 * Destination adress is mapped IPv6 address. 27335 * Source bound address should be unspecified or 27336 * IPv6 mapped address as well. 27337 */ 27338 if (!IN6_IS_ADDR_UNSPECIFIED( 27339 &tcp->tcp_bound_source_v6) && 27340 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 27341 return (EADDRNOTAVAIL); 27342 } 27343 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 27344 dstport = sin6->sin6_port; 27345 srcid = sin6->__sin6_src_id; 27346 } else { 27347 dstaddrp = &sin->sin_addr.s_addr; 27348 dstport = sin->sin_port; 27349 srcid = 0; 27350 } 27351 27352 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 27353 pid); 27354 break; 27355 default: 27356 return (-TOUTSTATE); 27357 } 27358 /* 27359 * Note: Code below is the "failure" case 27360 */ 27361 connect_failed: 27362 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 27363 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 27364 return (error); 27365 } 27366 27367 int 27368 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 27369 socklen_t len, sock_connid_t *id, cred_t *cr) 27370 { 27371 conn_t *connp = (conn_t *)proto_handle; 27372 tcp_t *tcp = connp->conn_tcp; 27373 squeue_t *sqp = connp->conn_sqp; 27374 int error; 27375 27376 ASSERT(connp->conn_upper_handle != NULL); 27377 27378 /* All Solaris components should pass a cred for this operation. */ 27379 ASSERT(cr != NULL); 27380 27381 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 27382 if (error != 0) { 27383 return (error); 27384 } 27385 27386 error = squeue_synch_enter(sqp, connp, 0); 27387 if (error != 0) { 27388 /* failed to enter */ 27389 return (ENOSR); 27390 } 27391 27392 /* 27393 * TCP supports quick connect, so no need to do an implicit bind 27394 */ 27395 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 27396 if (error == 0) { 27397 *id = connp->conn_tcp->tcp_connid; 27398 } else if (error < 0) { 27399 if (error == -TOUTSTATE) { 27400 switch (connp->conn_tcp->tcp_state) { 27401 case TCPS_SYN_SENT: 27402 error = EALREADY; 27403 break; 27404 case TCPS_ESTABLISHED: 27405 error = EISCONN; 27406 break; 27407 case TCPS_LISTEN: 27408 error = EOPNOTSUPP; 27409 break; 27410 default: 27411 error = EINVAL; 27412 break; 27413 } 27414 } else { 27415 error = proto_tlitosyserr(-error); 27416 } 27417 } 27418 done: 27419 squeue_synch_exit(sqp, connp); 27420 27421 return ((error == 0) ? EINPROGRESS : error); 27422 } 27423 27424 /* ARGSUSED */ 27425 sock_lower_handle_t 27426 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 27427 uint_t *smodep, int *errorp, int flags, cred_t *credp) 27428 { 27429 conn_t *connp; 27430 boolean_t isv6 = family == AF_INET6; 27431 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 27432 (proto != 0 && proto != IPPROTO_TCP)) { 27433 *errorp = EPROTONOSUPPORT; 27434 return (NULL); 27435 } 27436 27437 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 27438 if (connp == NULL) { 27439 return (NULL); 27440 } 27441 27442 /* 27443 * Put the ref for TCP. Ref for IP was already put 27444 * by ipcl_conn_create. Also Make the conn_t globally 27445 * visible to walkers 27446 */ 27447 mutex_enter(&connp->conn_lock); 27448 CONN_INC_REF_LOCKED(connp); 27449 ASSERT(connp->conn_ref == 2); 27450 connp->conn_state_flags &= ~CONN_INCIPIENT; 27451 27452 connp->conn_flags |= IPCL_NONSTR; 27453 mutex_exit(&connp->conn_lock); 27454 27455 ASSERT(errorp != NULL); 27456 *errorp = 0; 27457 *sock_downcalls = &sock_tcp_downcalls; 27458 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 27459 SM_SENDFILESUPP; 27460 27461 return ((sock_lower_handle_t)connp); 27462 } 27463 27464 /* ARGSUSED */ 27465 void 27466 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 27467 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 27468 { 27469 conn_t *connp = (conn_t *)proto_handle; 27470 struct sock_proto_props sopp; 27471 27472 ASSERT(connp->conn_upper_handle == NULL); 27473 27474 /* All Solaris components should pass a cred for this operation. */ 27475 ASSERT(cr != NULL); 27476 27477 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 27478 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 27479 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 27480 27481 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 27482 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 27483 sopp.sopp_maxpsz = INFPSZ; 27484 sopp.sopp_maxblk = INFPSZ; 27485 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 27486 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 27487 sopp.sopp_maxaddrlen = sizeof (sin6_t); 27488 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 27489 tcp_rinfo.mi_minpsz; 27490 27491 connp->conn_upcalls = sock_upcalls; 27492 connp->conn_upper_handle = sock_handle; 27493 27494 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 27495 } 27496 27497 /* ARGSUSED */ 27498 int 27499 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 27500 { 27501 conn_t *connp = (conn_t *)proto_handle; 27502 27503 ASSERT(connp->conn_upper_handle != NULL); 27504 27505 /* All Solaris components should pass a cred for this operation. */ 27506 ASSERT(cr != NULL); 27507 27508 tcp_close_common(connp, flags); 27509 27510 ip_free_helper_stream(connp); 27511 27512 /* 27513 * Drop IP's reference on the conn. This is the last reference 27514 * on the connp if the state was less than established. If the 27515 * connection has gone into timewait state, then we will have 27516 * one ref for the TCP and one more ref (total of two) for the 27517 * classifier connected hash list (a timewait connections stays 27518 * in connected hash till closed). 27519 * 27520 * We can't assert the references because there might be other 27521 * transient reference places because of some walkers or queued 27522 * packets in squeue for the timewait state. 27523 */ 27524 CONN_DEC_REF(connp); 27525 return (0); 27526 } 27527 27528 /* ARGSUSED */ 27529 int 27530 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 27531 cred_t *cr) 27532 { 27533 tcp_t *tcp; 27534 uint32_t msize; 27535 conn_t *connp = (conn_t *)proto_handle; 27536 int32_t tcpstate; 27537 27538 /* All Solaris components should pass a cred for this operation. */ 27539 ASSERT(cr != NULL); 27540 27541 ASSERT(connp->conn_ref >= 2); 27542 ASSERT(connp->conn_upper_handle != NULL); 27543 27544 if (msg->msg_controllen != 0) { 27545 return (EOPNOTSUPP); 27546 27547 } 27548 switch (DB_TYPE(mp)) { 27549 case M_DATA: 27550 tcp = connp->conn_tcp; 27551 ASSERT(tcp != NULL); 27552 27553 tcpstate = tcp->tcp_state; 27554 if (tcpstate < TCPS_ESTABLISHED) { 27555 freemsg(mp); 27556 return (ENOTCONN); 27557 } else if (tcpstate > TCPS_CLOSE_WAIT) { 27558 freemsg(mp); 27559 return (EPIPE); 27560 } 27561 27562 msize = msgdsize(mp); 27563 27564 mutex_enter(&tcp->tcp_non_sq_lock); 27565 tcp->tcp_squeue_bytes += msize; 27566 /* 27567 * Squeue Flow Control 27568 */ 27569 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 27570 tcp_setqfull(tcp); 27571 } 27572 mutex_exit(&tcp->tcp_non_sq_lock); 27573 27574 /* 27575 * The application may pass in an address in the msghdr, but 27576 * we ignore the address on connection-oriented sockets. 27577 * Just like BSD this code does not generate an error for 27578 * TCP (a CONNREQUIRED socket) when sending to an address 27579 * passed in with sendto/sendmsg. Instead the data is 27580 * delivered on the connection as if no address had been 27581 * supplied. 27582 */ 27583 CONN_INC_REF(connp); 27584 27585 if (msg != NULL && msg->msg_flags & MSG_OOB) { 27586 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 27587 tcp_output_urgent, connp, tcp_squeue_flag, 27588 SQTAG_TCP_OUTPUT); 27589 } else { 27590 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 27591 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 27592 } 27593 27594 return (0); 27595 27596 default: 27597 ASSERT(0); 27598 } 27599 27600 freemsg(mp); 27601 return (0); 27602 } 27603 27604 /* ARGSUSED */ 27605 void 27606 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 27607 { 27608 int len; 27609 uint32_t msize; 27610 conn_t *connp = (conn_t *)arg; 27611 tcp_t *tcp = connp->conn_tcp; 27612 27613 msize = msgdsize(mp); 27614 27615 len = msize - 1; 27616 if (len < 0) { 27617 freemsg(mp); 27618 return; 27619 } 27620 27621 /* 27622 * Try to force urgent data out on the wire. 27623 * Even if we have unsent data this will 27624 * at least send the urgent flag. 27625 * XXX does not handle more flag correctly. 27626 */ 27627 len += tcp->tcp_unsent; 27628 len += tcp->tcp_snxt; 27629 tcp->tcp_urg = len; 27630 tcp->tcp_valid_bits |= TCP_URG_VALID; 27631 27632 /* Bypass tcp protocol for fused tcp loopback */ 27633 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27634 return; 27635 tcp_wput_data(tcp, mp, B_TRUE); 27636 } 27637 27638 /* ARGSUSED */ 27639 int 27640 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27641 socklen_t *addrlenp, cred_t *cr) 27642 { 27643 conn_t *connp = (conn_t *)proto_handle; 27644 tcp_t *tcp = connp->conn_tcp; 27645 27646 ASSERT(connp->conn_upper_handle != NULL); 27647 /* All Solaris components should pass a cred for this operation. */ 27648 ASSERT(cr != NULL); 27649 27650 ASSERT(tcp != NULL); 27651 27652 return (tcp_do_getpeername(tcp, addr, addrlenp)); 27653 } 27654 27655 /* ARGSUSED */ 27656 int 27657 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27658 socklen_t *addrlenp, cred_t *cr) 27659 { 27660 conn_t *connp = (conn_t *)proto_handle; 27661 tcp_t *tcp = connp->conn_tcp; 27662 27663 /* All Solaris components should pass a cred for this operation. */ 27664 ASSERT(cr != NULL); 27665 27666 ASSERT(connp->conn_upper_handle != NULL); 27667 27668 return (tcp_do_getsockname(tcp, addr, addrlenp)); 27669 } 27670 27671 /* 27672 * tcp_fallback 27673 * 27674 * A direct socket is falling back to using STREAMS. Hanging 27675 * off of the queue is a temporary tcp_t, which was created using 27676 * tcp_open(). The tcp_open() was called as part of the regular 27677 * sockfs create path, i.e., the SO_SOCKSTR flag is passed down, 27678 * and therefore the temporary tcp_t is marked to be a socket 27679 * (i.e., IPCL_SOCKET, tcp_issocket). So the optimizations 27680 * introduced by FireEngine will be used. 27681 * 27682 * The tcp_t associated with the socket falling back will 27683 * still be marked as a socket, although the direct socket flag 27684 * (IPCL_NONSTR) is removed. A fall back to true TPI semantics 27685 * will not take place until a _SIOCSOCKFALLBACK ioctl is issued. 27686 * 27687 * If the above mentioned behavior, i.e., the tmp tcp_t is created 27688 * as a STREAMS/TPI endpoint, then we will need to do more work here. 27689 * Such as inserting the direct socket into the acceptor hash. 27690 */ 27691 void 27692 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27693 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27694 { 27695 tcp_t *tcp, *eager; 27696 conn_t *connp = (conn_t *)proto_handle; 27697 int error; 27698 struct T_capability_ack tca; 27699 struct sockaddr_in6 laddr, faddr; 27700 socklen_t laddrlen, faddrlen; 27701 short opts; 27702 struct stroptions *stropt; 27703 mblk_t *stropt_mp; 27704 mblk_t *mp; 27705 mblk_t *conn_ind_head = NULL; 27706 mblk_t *conn_ind_tail = NULL; 27707 mblk_t *ordrel_mp; 27708 mblk_t *fused_sigurp_mp; 27709 27710 tcp = connp->conn_tcp; 27711 /* 27712 * No support for acceptor fallback 27713 */ 27714 ASSERT(q->q_qinfo != &tcp_acceptor_rinit); 27715 27716 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL); 27717 27718 /* Pre-allocate the T_ordrel_ind mblk. */ 27719 ASSERT(tcp->tcp_ordrel_mp == NULL); 27720 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27721 STR_NOSIG, NULL); 27722 ordrel_mp->b_datap->db_type = M_PROTO; 27723 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27724 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27725 27726 /* Pre-allocate the M_PCSIG anyway */ 27727 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27728 27729 /* 27730 * Enter the squeue so that no new packets can come in 27731 */ 27732 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27733 if (error != 0) { 27734 /* failed to enter, free all the pre-allocated messages. */ 27735 freeb(stropt_mp); 27736 freeb(ordrel_mp); 27737 freeb(fused_sigurp_mp); 27738 return; 27739 } 27740 27741 /* Disable I/OAT during fallback */ 27742 tcp->tcp_sodirect = NULL; 27743 27744 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27745 connp->conn_minor_arena = WR(q)->q_ptr; 27746 27747 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27748 27749 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27750 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27751 27752 WR(q)->q_qinfo = &tcp_sock_winit; 27753 27754 if (!direct_sockfs) 27755 tcp_disable_direct_sockfs(tcp); 27756 27757 /* 27758 * free the helper stream 27759 */ 27760 ip_free_helper_stream(connp); 27761 27762 /* 27763 * Notify the STREAM head about options 27764 */ 27765 DB_TYPE(stropt_mp) = M_SETOPTS; 27766 stropt = (struct stroptions *)stropt_mp->b_rptr; 27767 stropt_mp->b_wptr += sizeof (struct stroptions); 27768 stropt = (struct stroptions *)stropt_mp->b_rptr; 27769 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27770 27771 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27772 tcp->tcp_tcps->tcps_wroff_xtra); 27773 if (tcp->tcp_snd_sack_ok) 27774 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27775 stropt->so_hiwat = tcp->tcp_fused ? 27776 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27777 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27778 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27779 27780 putnext(RD(q), stropt_mp); 27781 27782 /* 27783 * Collect the information needed to sync with the sonode 27784 */ 27785 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27786 27787 laddrlen = faddrlen = sizeof (sin6_t); 27788 (void) tcp_getsockname(proto_handle, (struct sockaddr *)&laddr, 27789 &laddrlen, CRED()); 27790 error = tcp_getpeername(proto_handle, (struct sockaddr *)&faddr, 27791 &faddrlen, CRED()); 27792 if (error != 0) 27793 faddrlen = 0; 27794 27795 opts = 0; 27796 if (tcp->tcp_oobinline) 27797 opts |= SO_OOBINLINE; 27798 if (tcp->tcp_dontroute) 27799 opts |= SO_DONTROUTE; 27800 27801 /* 27802 * Notify the socket that the protocol is now quiescent, 27803 * and it's therefore safe move data from the socket 27804 * to the stream head. 27805 */ 27806 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27807 (struct sockaddr *)&laddr, laddrlen, 27808 (struct sockaddr *)&faddr, faddrlen, opts); 27809 27810 while ((mp = tcp->tcp_rcv_list) != NULL) { 27811 tcp->tcp_rcv_list = mp->b_next; 27812 mp->b_next = NULL; 27813 putnext(q, mp); 27814 } 27815 tcp->tcp_rcv_last_head = NULL; 27816 tcp->tcp_rcv_last_tail = NULL; 27817 tcp->tcp_rcv_cnt = 0; 27818 27819 /* 27820 * No longer a direct socket 27821 */ 27822 connp->conn_flags &= ~IPCL_NONSTR; 27823 27824 tcp->tcp_ordrel_mp = ordrel_mp; 27825 27826 if (tcp->tcp_fused) { 27827 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27828 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27829 } else { 27830 freeb(fused_sigurp_mp); 27831 } 27832 27833 /* 27834 * Send T_CONN_IND messages for all ESTABLISHED connections. 27835 */ 27836 mutex_enter(&tcp->tcp_eager_lock); 27837 for (eager = tcp->tcp_eager_next_q; eager != NULL; 27838 eager = eager->tcp_eager_next_q) { 27839 mp = eager->tcp_conn.tcp_eager_conn_ind; 27840 27841 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27842 ASSERT(mp != NULL); 27843 /* 27844 * TLI/XTI applications will get confused by 27845 * sending eager as an option since it violates 27846 * the option semantics. So remove the eager as 27847 * option since TLI/XTI app doesn't need it anyway. 27848 */ 27849 if (!TCP_IS_SOCKET(tcp)) { 27850 struct T_conn_ind *conn_ind; 27851 27852 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27853 conn_ind->OPT_length = 0; 27854 conn_ind->OPT_offset = 0; 27855 } 27856 if (conn_ind_head == NULL) { 27857 conn_ind_head = mp; 27858 } else { 27859 conn_ind_tail->b_next = mp; 27860 } 27861 conn_ind_tail = mp; 27862 } 27863 mutex_exit(&tcp->tcp_eager_lock); 27864 27865 mp = conn_ind_head; 27866 while (mp != NULL) { 27867 mblk_t *nmp = mp->b_next; 27868 mp->b_next = NULL; 27869 27870 putnext(tcp->tcp_rq, mp); 27871 mp = nmp; 27872 } 27873 27874 /* 27875 * There should be atleast two ref's (IP + TCP) 27876 */ 27877 ASSERT(connp->conn_ref >= 2); 27878 squeue_synch_exit(connp->conn_sqp, connp); 27879 } 27880 27881 /* ARGSUSED */ 27882 static void 27883 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27884 { 27885 conn_t *connp = (conn_t *)arg; 27886 tcp_t *tcp = connp->conn_tcp; 27887 27888 freemsg(mp); 27889 27890 if (tcp->tcp_fused) 27891 tcp_unfuse(tcp); 27892 27893 if (tcp_xmit_end(tcp) != 0) { 27894 /* 27895 * We were crossing FINs and got a reset from 27896 * the other side. Just ignore it. 27897 */ 27898 if (tcp->tcp_debug) { 27899 (void) strlog(TCP_MOD_ID, 0, 1, 27900 SL_ERROR|SL_TRACE, 27901 "tcp_shutdown_output() out of state %s", 27902 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27903 } 27904 } 27905 } 27906 27907 /* ARGSUSED */ 27908 int 27909 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27910 { 27911 conn_t *connp = (conn_t *)proto_handle; 27912 tcp_t *tcp = connp->conn_tcp; 27913 27914 ASSERT(connp->conn_upper_handle != NULL); 27915 27916 /* All Solaris components should pass a cred for this operation. */ 27917 ASSERT(cr != NULL); 27918 27919 /* 27920 * X/Open requires that we check the connected state. 27921 */ 27922 if (tcp->tcp_state < TCPS_SYN_SENT) 27923 return (ENOTCONN); 27924 27925 /* shutdown the send side */ 27926 if (how != SHUT_RD) { 27927 mblk_t *bp; 27928 27929 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27930 CONN_INC_REF(connp); 27931 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27932 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27933 27934 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27935 SOCK_OPCTL_SHUT_SEND, 0); 27936 } 27937 27938 /* shutdown the recv side */ 27939 if (how != SHUT_WR) 27940 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27941 SOCK_OPCTL_SHUT_RECV, 0); 27942 27943 return (0); 27944 } 27945 27946 /* 27947 * SOP_LISTEN() calls into tcp_listen(). 27948 */ 27949 /* ARGSUSED */ 27950 int 27951 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27952 { 27953 conn_t *connp = (conn_t *)proto_handle; 27954 int error; 27955 squeue_t *sqp = connp->conn_sqp; 27956 27957 ASSERT(connp->conn_upper_handle != NULL); 27958 27959 /* All Solaris components should pass a cred for this operation. */ 27960 ASSERT(cr != NULL); 27961 27962 error = squeue_synch_enter(sqp, connp, 0); 27963 if (error != 0) { 27964 /* failed to enter */ 27965 return (ENOBUFS); 27966 } 27967 27968 error = tcp_do_listen(connp, backlog, cr); 27969 if (error == 0) { 27970 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27971 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 27972 } else if (error < 0) { 27973 if (error == -TOUTSTATE) 27974 error = EINVAL; 27975 else 27976 error = proto_tlitosyserr(-error); 27977 } 27978 squeue_synch_exit(sqp, connp); 27979 return (error); 27980 } 27981 27982 static int 27983 tcp_do_listen(conn_t *connp, int backlog, cred_t *cr) 27984 { 27985 tcp_t *tcp = connp->conn_tcp; 27986 sin_t *sin; 27987 sin6_t *sin6; 27988 int error = 0; 27989 tcp_stack_t *tcps = tcp->tcp_tcps; 27990 27991 /* All Solaris components should pass a cred for this operation. */ 27992 ASSERT(cr != NULL); 27993 27994 if (tcp->tcp_state >= TCPS_BOUND) { 27995 if ((tcp->tcp_state == TCPS_BOUND || 27996 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 27997 /* 27998 * Handle listen() increasing backlog. 27999 * This is more "liberal" then what the TPI spec 28000 * requires but is needed to avoid a t_unbind 28001 * when handling listen() since the port number 28002 * might be "stolen" between the unbind and bind. 28003 */ 28004 goto do_listen; 28005 } 28006 if (tcp->tcp_debug) { 28007 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 28008 "tcp_listen: bad state, %d", tcp->tcp_state); 28009 } 28010 return (-TOUTSTATE); 28011 } else { 28012 int32_t len; 28013 sin6_t addr; 28014 28015 /* Do an implicit bind: Request for a generic port. */ 28016 if (tcp->tcp_family == AF_INET) { 28017 len = sizeof (sin_t); 28018 sin = (sin_t *)&addr; 28019 *sin = sin_null; 28020 sin->sin_family = AF_INET; 28021 tcp->tcp_ipversion = IPV4_VERSION; 28022 } else { 28023 ASSERT(tcp->tcp_family == AF_INET6); 28024 len = sizeof (sin6_t); 28025 sin6 = (sin6_t *)&addr; 28026 *sin6 = sin6_null; 28027 sin6->sin6_family = AF_INET6; 28028 tcp->tcp_ipversion = IPV6_VERSION; 28029 } 28030 28031 error = tcp_bind_check(connp, (struct sockaddr *)&addr, len, 28032 cr, B_FALSE); 28033 if (error) 28034 return (error); 28035 /* Fall through and do the fanout insertion */ 28036 } 28037 28038 do_listen: 28039 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 28040 tcp->tcp_conn_req_max = backlog; 28041 if (tcp->tcp_conn_req_max) { 28042 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 28043 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 28044 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 28045 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 28046 /* 28047 * If this is a listener, do not reset the eager list 28048 * and other stuffs. Note that we don't check if the 28049 * existing eager list meets the new tcp_conn_req_max 28050 * requirement. 28051 */ 28052 if (tcp->tcp_state != TCPS_LISTEN) { 28053 tcp->tcp_state = TCPS_LISTEN; 28054 /* Initialize the chain. Don't need the eager_lock */ 28055 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 28056 tcp->tcp_eager_next_drop_q0 = tcp; 28057 tcp->tcp_eager_prev_drop_q0 = tcp; 28058 tcp->tcp_second_ctimer_threshold = 28059 tcps->tcps_ip_abort_linterval; 28060 } 28061 } 28062 28063 /* 28064 * We can call ip_bind directly, the processing continues 28065 * in tcp_post_ip_bind(). 28066 * 28067 * We need to make sure that the conn_recv is set to a non-null 28068 * value before we insert the conn into the classifier table. 28069 * This is to avoid a race with an incoming packet which does an 28070 * ipcl_classify(). 28071 */ 28072 connp->conn_recv = tcp_conn_request; 28073 if (tcp->tcp_family == AF_INET) { 28074 error = ip_proto_bind_laddr_v4(connp, NULL, 28075 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 28076 } else { 28077 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 28078 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 28079 } 28080 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 28081 } 28082 28083 void 28084 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 28085 { 28086 conn_t *connp = (conn_t *)proto_handle; 28087 tcp_t *tcp = connp->conn_tcp; 28088 tcp_stack_t *tcps = tcp->tcp_tcps; 28089 uint_t thwin; 28090 28091 ASSERT(connp->conn_upper_handle != NULL); 28092 28093 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 28094 28095 /* Flow control condition has been removed. */ 28096 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 28097 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 28098 << tcp->tcp_rcv_ws; 28099 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 28100 /* 28101 * Send back a window update immediately if TCP is above 28102 * ESTABLISHED state and the increase of the rcv window 28103 * that the other side knows is at least 1 MSS after flow 28104 * control is lifted. 28105 */ 28106 if (tcp->tcp_state >= TCPS_ESTABLISHED && 28107 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 28108 tcp_xmit_ctl(NULL, tcp, 28109 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 28110 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 28111 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 28112 } 28113 28114 squeue_synch_exit(connp->conn_sqp, connp); 28115 } 28116 28117 /* ARGSUSED */ 28118 int 28119 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 28120 int mode, int32_t *rvalp, cred_t *cr) 28121 { 28122 conn_t *connp = (conn_t *)proto_handle; 28123 int error; 28124 28125 ASSERT(connp->conn_upper_handle != NULL); 28126 28127 /* All Solaris components should pass a cred for this operation. */ 28128 ASSERT(cr != NULL); 28129 28130 switch (cmd) { 28131 case ND_SET: 28132 case ND_GET: 28133 case TCP_IOC_DEFAULT_Q: 28134 case _SIOCSOCKFALLBACK: 28135 case TCP_IOC_ABORT_CONN: 28136 case TI_GETPEERNAME: 28137 case TI_GETMYNAME: 28138 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 28139 cmd)); 28140 error = EINVAL; 28141 break; 28142 default: 28143 /* 28144 * Pass on to IP using helper stream 28145 */ 28146 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 28147 cmd, arg, mode, cr, rvalp); 28148 break; 28149 } 28150 return (error); 28151 } 28152 28153 sock_downcalls_t sock_tcp_downcalls = { 28154 tcp_activate, 28155 tcp_accept, 28156 tcp_bind, 28157 tcp_listen, 28158 tcp_connect, 28159 tcp_getpeername, 28160 tcp_getsockname, 28161 tcp_getsockopt, 28162 tcp_setsockopt, 28163 tcp_sendmsg, 28164 NULL, 28165 NULL, 28166 NULL, 28167 tcp_shutdown, 28168 tcp_clr_flowctrl, 28169 tcp_ioctl, 28170 tcp_close, 28171 }; 28172