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 sock_upper_handle_t to_handle; 656 }; 657 658 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 659 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 660 #define TCPOPT_UPPERHANDLE 0x00000004 /* set upper handle */ 661 662 /* 663 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 664 */ 665 666 #ifdef _BIG_ENDIAN 667 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 668 (TCPOPT_TSTAMP << 8) | 10) 669 #else 670 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 671 (TCPOPT_NOP << 8) | TCPOPT_NOP) 672 #endif 673 674 /* 675 * Flags returned from tcp_parse_options. 676 */ 677 #define TCP_OPT_MSS_PRESENT 1 678 #define TCP_OPT_WSCALE_PRESENT 2 679 #define TCP_OPT_TSTAMP_PRESENT 4 680 #define TCP_OPT_SACK_OK_PRESENT 8 681 #define TCP_OPT_SACK_PRESENT 16 682 683 /* TCP option length */ 684 #define TCPOPT_NOP_LEN 1 685 #define TCPOPT_MAXSEG_LEN 4 686 #define TCPOPT_WS_LEN 3 687 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 688 #define TCPOPT_TSTAMP_LEN 10 689 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 690 #define TCPOPT_SACK_OK_LEN 2 691 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 692 #define TCPOPT_REAL_SACK_LEN 4 693 #define TCPOPT_MAX_SACK_LEN 36 694 #define TCPOPT_HEADER_LEN 2 695 696 /* TCP cwnd burst factor. */ 697 #define TCP_CWND_INFINITE 65535 698 #define TCP_CWND_SS 3 699 #define TCP_CWND_NORMAL 5 700 701 /* Maximum TCP initial cwin (start/restart). */ 702 #define TCP_MAX_INIT_CWND 8 703 704 /* 705 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 706 * either tcp_slow_start_initial or tcp_slow_start_after idle 707 * depending on the caller. If the upper layer has not used the 708 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 709 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 710 * If the upper layer has changed set the tcp_init_cwnd, just use 711 * it to calculate the tcp_cwnd. 712 */ 713 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 714 { \ 715 if ((tcp)->tcp_init_cwnd == 0) { \ 716 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 717 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 718 } else { \ 719 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 720 } \ 721 tcp->tcp_cwnd_cnt = 0; \ 722 } 723 724 /* TCP Timer control structure */ 725 typedef struct tcpt_s { 726 pfv_t tcpt_pfv; /* The routine we are to call */ 727 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 728 } tcpt_t; 729 730 /* Host Specific Parameter structure */ 731 typedef struct tcp_hsp { 732 struct tcp_hsp *tcp_hsp_next; 733 in6_addr_t tcp_hsp_addr_v6; 734 in6_addr_t tcp_hsp_subnet_v6; 735 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 736 int32_t tcp_hsp_sendspace; 737 int32_t tcp_hsp_recvspace; 738 int32_t tcp_hsp_tstamp; 739 } tcp_hsp_t; 740 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 741 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 742 743 /* 744 * Functions called directly via squeue having a prototype of edesc_t. 745 */ 746 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 747 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 748 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 749 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 750 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 751 void tcp_input(void *arg, mblk_t *mp, void *arg2); 752 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 753 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 754 void tcp_output(void *arg, mblk_t *mp, void *arg2); 755 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 756 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 757 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 758 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 759 760 761 /* Prototype for TCP functions */ 762 static void tcp_random_init(void); 763 int tcp_random(void); 764 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 765 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 766 tcp_t *eager); 767 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 768 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 769 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 770 boolean_t user_specified); 771 static void tcp_closei_local(tcp_t *tcp); 772 static void tcp_close_detached(tcp_t *tcp); 773 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 774 mblk_t *idmp, mblk_t **defermp); 775 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 776 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 777 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 778 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 779 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 780 uint32_t scope_id, cred_t *cr, pid_t pid); 781 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 782 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 783 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 784 static char *tcp_display(tcp_t *tcp, char *, char); 785 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 786 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 787 static void tcp_eager_unlink(tcp_t *tcp); 788 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 789 int unixerr); 790 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 791 int tlierr, int unixerr); 792 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 793 cred_t *cr); 794 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 795 char *value, caddr_t cp, cred_t *cr); 796 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 797 char *value, caddr_t cp, cred_t *cr); 798 static int tcp_tpistate(tcp_t *tcp); 799 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 800 int caller_holds_lock); 801 static void tcp_bind_hash_remove(tcp_t *tcp); 802 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 803 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 804 static void tcp_acceptor_hash_remove(tcp_t *tcp); 805 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 806 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 807 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 808 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 809 void tcp_g_q_setup(tcp_stack_t *); 810 void tcp_g_q_create(tcp_stack_t *); 811 void tcp_g_q_destroy(tcp_stack_t *); 812 static int tcp_header_init_ipv4(tcp_t *tcp); 813 static int tcp_header_init_ipv6(tcp_t *tcp); 814 int tcp_init(tcp_t *tcp, queue_t *q); 815 static int tcp_init_values(tcp_t *tcp); 816 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 817 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 818 static void tcp_ip_notify(tcp_t *tcp); 819 static mblk_t *tcp_ire_mp(mblk_t **mpp); 820 static void tcp_iss_init(tcp_t *tcp); 821 static void tcp_keepalive_killer(void *arg); 822 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 823 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 824 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 825 int *do_disconnectp, int *t_errorp, int *sys_errorp); 826 static boolean_t tcp_allow_connopt_set(int level, int name); 827 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 828 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 829 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 830 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 831 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 832 mblk_t *mblk); 833 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 834 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 835 uchar_t *ptr, uint_t len); 836 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 837 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 838 tcp_stack_t *); 839 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 840 caddr_t cp, cred_t *cr); 841 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 842 caddr_t cp, cred_t *cr); 843 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 844 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 845 caddr_t cp, cred_t *cr); 846 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 847 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 848 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 849 static void tcp_reinit(tcp_t *tcp); 850 static void tcp_reinit_values(tcp_t *tcp); 851 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 852 tcp_t *thisstream, cred_t *cr); 853 854 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 855 static uint_t tcp_rcv_drain(tcp_t *tcp); 856 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 857 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 858 static void tcp_ss_rexmit(tcp_t *tcp); 859 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 860 static void tcp_process_options(tcp_t *, tcph_t *); 861 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 862 static void tcp_rsrv(queue_t *q); 863 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 864 static int tcp_snmp_state(tcp_t *tcp); 865 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 866 cred_t *cr); 867 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 868 cred_t *cr); 869 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 870 cred_t *cr); 871 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 872 cred_t *cr); 873 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 874 cred_t *cr); 875 static void tcp_timer(void *arg); 876 static void tcp_timer_callback(void *); 877 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 878 boolean_t random); 879 static in_port_t tcp_get_next_priv_port(const tcp_t *); 880 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 881 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 882 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 883 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 884 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 885 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 886 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 887 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 888 const int num_sack_blk, int *usable, uint_t *snxt, 889 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 890 const int mdt_thres); 891 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 892 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 893 const int num_sack_blk, int *usable, uint_t *snxt, 894 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 895 const int mdt_thres); 896 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 897 int num_sack_blk); 898 static void tcp_wsrv(queue_t *q); 899 static int tcp_xmit_end(tcp_t *tcp); 900 static void tcp_ack_timer(void *arg); 901 static mblk_t *tcp_ack_mp(tcp_t *tcp); 902 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 903 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 904 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 905 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 906 uint32_t ack, int ctl); 907 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *); 908 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr, tcp_stack_t *); 909 static int setmaxps(queue_t *q, int maxpsz); 910 static void tcp_set_rto(tcp_t *, time_t); 911 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 912 boolean_t, boolean_t); 913 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 914 boolean_t ipsec_mctl); 915 static int tcp_build_hdrs(tcp_t *); 916 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 917 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 918 tcph_t *tcph); 919 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 920 static mblk_t *tcp_mdt_info_mp(mblk_t *); 921 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 922 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 923 const boolean_t, const uint32_t, const uint32_t, 924 const uint32_t, const uint32_t, tcp_stack_t *); 925 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 926 const uint_t, const uint_t, boolean_t *); 927 static mblk_t *tcp_lso_info_mp(mblk_t *); 928 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 929 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 930 extern mblk_t *tcp_timermp_alloc(int); 931 extern void tcp_timermp_free(tcp_t *); 932 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 933 static void tcp_stop_lingering(tcp_t *tcp); 934 static void tcp_close_linger_timeout(void *arg); 935 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 936 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 937 static void tcp_stack_fini(netstackid_t stackid, void *arg); 938 static void *tcp_g_kstat_init(tcp_g_stat_t *); 939 static void tcp_g_kstat_fini(kstat_t *); 940 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 941 static void tcp_kstat_fini(netstackid_t, kstat_t *); 942 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 943 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 944 static int tcp_kstat_update(kstat_t *kp, int rw); 945 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 946 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 947 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 948 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 949 tcph_t *tcph, mblk_t *idmp); 950 static int tcp_squeue_switch(int); 951 952 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 953 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 954 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 955 static int tcp_tpi_close(queue_t *, int); 956 static int tcpclose_accept(queue_t *); 957 958 static void tcp_squeue_add(squeue_t *); 959 static boolean_t tcp_zcopy_check(tcp_t *); 960 static void tcp_zcopy_notify(tcp_t *); 961 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 962 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 963 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 964 965 extern void tcp_kssl_input(tcp_t *, mblk_t *); 966 967 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 968 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 969 970 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 971 sock_upper_handle_t, cred_t *); 972 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 973 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 974 static int tcp_do_listen(conn_t *, int, cred_t *); 975 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 976 cred_t *, pid_t); 977 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 978 boolean_t); 979 static int tcp_do_unbind(conn_t *); 980 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 981 boolean_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 3056 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 3057 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 3058 if (tcp->tcp_debug) { 3059 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3060 "tcp_tpi_bind: bad req, len %u", 3061 (uint_t)(mp->b_wptr - mp->b_rptr)); 3062 } 3063 tcp_err_ack(tcp, mp, TPROTO, 0); 3064 return; 3065 } 3066 /* Make sure the largest address fits */ 3067 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3068 if (mp1 == NULL) { 3069 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3070 return; 3071 } 3072 mp = mp1; 3073 tbr = (struct T_bind_req *)mp->b_rptr; 3074 3075 backlog = tbr->CONIND_number; 3076 len = tbr->ADDR_length; 3077 3078 switch (len) { 3079 case 0: /* request for a generic port */ 3080 tbr->ADDR_offset = sizeof (struct T_bind_req); 3081 if (tcp->tcp_family == AF_INET) { 3082 tbr->ADDR_length = sizeof (sin_t); 3083 sin = (sin_t *)&tbr[1]; 3084 *sin = sin_null; 3085 sin->sin_family = AF_INET; 3086 sa = (struct sockaddr *)sin; 3087 len = sizeof (sin_t); 3088 mp->b_wptr = (uchar_t *)&sin[1]; 3089 } else { 3090 ASSERT(tcp->tcp_family == AF_INET6); 3091 tbr->ADDR_length = sizeof (sin6_t); 3092 sin6 = (sin6_t *)&tbr[1]; 3093 *sin6 = sin6_null; 3094 sin6->sin6_family = AF_INET6; 3095 sa = (struct sockaddr *)sin6; 3096 len = sizeof (sin6_t); 3097 mp->b_wptr = (uchar_t *)&sin6[1]; 3098 } 3099 break; 3100 3101 case sizeof (sin_t): /* Complete IPv4 address */ 3102 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3103 sizeof (sin_t)); 3104 break; 3105 3106 case sizeof (sin6_t): /* Complete IPv6 address */ 3107 sa = (struct sockaddr *)mi_offset_param(mp, 3108 tbr->ADDR_offset, sizeof (sin6_t)); 3109 break; 3110 3111 default: 3112 if (tcp->tcp_debug) { 3113 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3114 "tcp_tpi_bind: bad address length, %d", 3115 tbr->ADDR_length); 3116 } 3117 tcp_err_ack(tcp, mp, TBADADDR, 0); 3118 return; 3119 } 3120 3121 error = tcp_bind_check(connp, sa, len, DB_CRED(mp), 3122 tbr->PRIM_type != O_T_BIND_REQ); 3123 if (error == 0) { 3124 if (tcp->tcp_family == AF_INET) { 3125 sin = (sin_t *)sa; 3126 sin->sin_port = tcp->tcp_lport; 3127 } else { 3128 sin6 = (sin6_t *)sa; 3129 sin6->sin6_port = tcp->tcp_lport; 3130 } 3131 3132 if (backlog > 0) { 3133 error = tcp_do_listen(connp, backlog, DB_CRED(mp)); 3134 } 3135 } 3136 done: 3137 if (error > 0) { 3138 tcp_err_ack(tcp, mp, TSYSERR, error); 3139 } else if (error < 0) { 3140 tcp_err_ack(tcp, mp, -error, 0); 3141 } else { 3142 mp->b_datap->db_type = M_PCPROTO; 3143 tbr->PRIM_type = T_BIND_ACK; 3144 putnext(tcp->tcp_rq, mp); 3145 } 3146 } 3147 3148 /* 3149 * If the "bind_to_req_port_only" parameter is set, if the requested port 3150 * number is available, return it, If not return 0 3151 * 3152 * If "bind_to_req_port_only" parameter is not set and 3153 * If the requested port number is available, return it. If not, return 3154 * the first anonymous port we happen across. If no anonymous ports are 3155 * available, return 0. addr is the requested local address, if any. 3156 * 3157 * In either case, when succeeding update the tcp_t to record the port number 3158 * and insert it in the bind hash table. 3159 * 3160 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3161 * without setting SO_REUSEADDR. This is needed so that they 3162 * can be viewed as two independent transport protocols. 3163 */ 3164 static in_port_t 3165 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3166 int reuseaddr, boolean_t quick_connect, 3167 boolean_t bind_to_req_port_only, boolean_t user_specified) 3168 { 3169 /* number of times we have run around the loop */ 3170 int count = 0; 3171 /* maximum number of times to run around the loop */ 3172 int loopmax; 3173 conn_t *connp = tcp->tcp_connp; 3174 zoneid_t zoneid = connp->conn_zoneid; 3175 tcp_stack_t *tcps = tcp->tcp_tcps; 3176 3177 /* 3178 * Lookup for free addresses is done in a loop and "loopmax" 3179 * influences how long we spin in the loop 3180 */ 3181 if (bind_to_req_port_only) { 3182 /* 3183 * If the requested port is busy, don't bother to look 3184 * for a new one. Setting loop maximum count to 1 has 3185 * that effect. 3186 */ 3187 loopmax = 1; 3188 } else { 3189 /* 3190 * If the requested port is busy, look for a free one 3191 * in the anonymous port range. 3192 * Set loopmax appropriately so that one does not look 3193 * forever in the case all of the anonymous ports are in use. 3194 */ 3195 if (tcp->tcp_anon_priv_bind) { 3196 /* 3197 * loopmax = 3198 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3199 */ 3200 loopmax = IPPORT_RESERVED - 3201 tcps->tcps_min_anonpriv_port; 3202 } else { 3203 loopmax = (tcps->tcps_largest_anon_port - 3204 tcps->tcps_smallest_anon_port + 1); 3205 } 3206 } 3207 do { 3208 uint16_t lport; 3209 tf_t *tbf; 3210 tcp_t *ltcp; 3211 conn_t *lconnp; 3212 3213 lport = htons(port); 3214 3215 /* 3216 * Ensure that the tcp_t is not currently in the bind hash. 3217 * Hold the lock on the hash bucket to ensure that 3218 * the duplicate check plus the insertion is an atomic 3219 * operation. 3220 * 3221 * This function does an inline lookup on the bind hash list 3222 * Make sure that we access only members of tcp_t 3223 * and that we don't look at tcp_tcp, since we are not 3224 * doing a CONN_INC_REF. 3225 */ 3226 tcp_bind_hash_remove(tcp); 3227 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3228 mutex_enter(&tbf->tf_lock); 3229 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3230 ltcp = ltcp->tcp_bind_hash) { 3231 if (lport == ltcp->tcp_lport) 3232 break; 3233 } 3234 3235 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3236 boolean_t not_socket; 3237 boolean_t exclbind; 3238 3239 lconnp = ltcp->tcp_connp; 3240 3241 /* 3242 * On a labeled system, we must treat bindings to ports 3243 * on shared IP addresses by sockets with MAC exemption 3244 * privilege as being in all zones, as there's 3245 * otherwise no way to identify the right receiver. 3246 */ 3247 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3248 IPCL_ZONE_MATCH(connp, 3249 ltcp->tcp_connp->conn_zoneid)) && 3250 !lconnp->conn_mac_exempt && 3251 !connp->conn_mac_exempt) 3252 continue; 3253 3254 /* 3255 * If TCP_EXCLBIND is set for either the bound or 3256 * binding endpoint, the semantics of bind 3257 * is changed according to the following. 3258 * 3259 * spec = specified address (v4 or v6) 3260 * unspec = unspecified address (v4 or v6) 3261 * A = specified addresses are different for endpoints 3262 * 3263 * bound bind to allowed 3264 * ------------------------------------- 3265 * unspec unspec no 3266 * unspec spec no 3267 * spec unspec no 3268 * spec spec yes if A 3269 * 3270 * For labeled systems, SO_MAC_EXEMPT behaves the same 3271 * as TCP_EXCLBIND, except that zoneid is ignored. 3272 * 3273 * Note: 3274 * 3275 * 1. Because of TLI semantics, an endpoint can go 3276 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3277 * TCPS_BOUND, depending on whether it is originally 3278 * a listener or not. That is why we need to check 3279 * for states greater than or equal to TCPS_BOUND 3280 * here. 3281 * 3282 * 2. Ideally, we should only check for state equals 3283 * to TCPS_LISTEN. And the following check should be 3284 * added. 3285 * 3286 * if (ltcp->tcp_state == TCPS_LISTEN || 3287 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3288 * ... 3289 * } 3290 * 3291 * The semantics will be changed to this. If the 3292 * endpoint on the list is in state not equal to 3293 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3294 * set, let the bind succeed. 3295 * 3296 * Because of (1), we cannot do that for TLI 3297 * endpoints. But we can do that for socket endpoints. 3298 * If in future, we can change this going back 3299 * semantics, we can use the above check for TLI also. 3300 */ 3301 not_socket = !(TCP_IS_SOCKET(ltcp) && 3302 TCP_IS_SOCKET(tcp)); 3303 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3304 3305 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3306 (exclbind && (not_socket || 3307 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3308 if (V6_OR_V4_INADDR_ANY( 3309 ltcp->tcp_bound_source_v6) || 3310 V6_OR_V4_INADDR_ANY(*laddr) || 3311 IN6_ARE_ADDR_EQUAL(laddr, 3312 <cp->tcp_bound_source_v6)) { 3313 break; 3314 } 3315 continue; 3316 } 3317 3318 /* 3319 * Check ipversion to allow IPv4 and IPv6 sockets to 3320 * have disjoint port number spaces, if *_EXCLBIND 3321 * is not set and only if the application binds to a 3322 * specific port. We use the same autoassigned port 3323 * number space for IPv4 and IPv6 sockets. 3324 */ 3325 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3326 bind_to_req_port_only) 3327 continue; 3328 3329 /* 3330 * Ideally, we should make sure that the source 3331 * address, remote address, and remote port in the 3332 * four tuple for this tcp-connection is unique. 3333 * However, trying to find out the local source 3334 * address would require too much code duplication 3335 * with IP, since IP needs needs to have that code 3336 * to support userland TCP implementations. 3337 */ 3338 if (quick_connect && 3339 (ltcp->tcp_state > TCPS_LISTEN) && 3340 ((tcp->tcp_fport != ltcp->tcp_fport) || 3341 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3342 <cp->tcp_remote_v6))) 3343 continue; 3344 3345 if (!reuseaddr) { 3346 /* 3347 * No socket option SO_REUSEADDR. 3348 * If existing port is bound to 3349 * a non-wildcard IP address 3350 * and the requesting stream is 3351 * bound to a distinct 3352 * different IP addresses 3353 * (non-wildcard, also), keep 3354 * going. 3355 */ 3356 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3357 !V6_OR_V4_INADDR_ANY( 3358 ltcp->tcp_bound_source_v6) && 3359 !IN6_ARE_ADDR_EQUAL(laddr, 3360 <cp->tcp_bound_source_v6)) 3361 continue; 3362 if (ltcp->tcp_state >= TCPS_BOUND) { 3363 /* 3364 * This port is being used and 3365 * its state is >= TCPS_BOUND, 3366 * so we can't bind to it. 3367 */ 3368 break; 3369 } 3370 } else { 3371 /* 3372 * socket option SO_REUSEADDR is set on the 3373 * binding tcp_t. 3374 * 3375 * If two streams are bound to 3376 * same IP address or both addr 3377 * and bound source are wildcards 3378 * (INADDR_ANY), we want to stop 3379 * searching. 3380 * We have found a match of IP source 3381 * address and source port, which is 3382 * refused regardless of the 3383 * SO_REUSEADDR setting, so we break. 3384 */ 3385 if (IN6_ARE_ADDR_EQUAL(laddr, 3386 <cp->tcp_bound_source_v6) && 3387 (ltcp->tcp_state == TCPS_LISTEN || 3388 ltcp->tcp_state == TCPS_BOUND)) 3389 break; 3390 } 3391 } 3392 if (ltcp != NULL) { 3393 /* The port number is busy */ 3394 mutex_exit(&tbf->tf_lock); 3395 } else { 3396 /* 3397 * This port is ours. Insert in fanout and mark as 3398 * bound to prevent others from getting the port 3399 * number. 3400 */ 3401 tcp->tcp_state = TCPS_BOUND; 3402 tcp->tcp_lport = htons(port); 3403 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3404 3405 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3406 tcp->tcp_lport)] == tbf); 3407 tcp_bind_hash_insert(tbf, tcp, 1); 3408 3409 mutex_exit(&tbf->tf_lock); 3410 3411 /* 3412 * We don't want tcp_next_port_to_try to "inherit" 3413 * a port number supplied by the user in a bind. 3414 */ 3415 if (user_specified) 3416 return (port); 3417 3418 /* 3419 * This is the only place where tcp_next_port_to_try 3420 * is updated. After the update, it may or may not 3421 * be in the valid range. 3422 */ 3423 if (!tcp->tcp_anon_priv_bind) 3424 tcps->tcps_next_port_to_try = port + 1; 3425 return (port); 3426 } 3427 3428 if (tcp->tcp_anon_priv_bind) { 3429 port = tcp_get_next_priv_port(tcp); 3430 } else { 3431 if (count == 0 && user_specified) { 3432 /* 3433 * We may have to return an anonymous port. So 3434 * get one to start with. 3435 */ 3436 port = 3437 tcp_update_next_port( 3438 tcps->tcps_next_port_to_try, 3439 tcp, B_TRUE); 3440 user_specified = B_FALSE; 3441 } else { 3442 port = tcp_update_next_port(port + 1, tcp, 3443 B_FALSE); 3444 } 3445 } 3446 if (port == 0) 3447 break; 3448 3449 /* 3450 * Don't let this loop run forever in the case where 3451 * all of the anonymous ports are in use. 3452 */ 3453 } while (++count < loopmax); 3454 return (0); 3455 } 3456 3457 /* 3458 * tcp_clean_death / tcp_close_detached must not be called more than once 3459 * on a tcp. Thus every function that potentially calls tcp_clean_death 3460 * must check for the tcp state before calling tcp_clean_death. 3461 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3462 * tcp_timer_handler, all check for the tcp state. 3463 */ 3464 /* ARGSUSED */ 3465 void 3466 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3467 { 3468 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3469 3470 freemsg(mp); 3471 if (tcp->tcp_state > TCPS_BOUND) 3472 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3473 ETIMEDOUT, 5); 3474 } 3475 3476 /* 3477 * We are dying for some reason. Try to do it gracefully. (May be called 3478 * as writer.) 3479 * 3480 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3481 * done by a service procedure). 3482 * TBD - Should the return value distinguish between the tcp_t being 3483 * freed and it being reinitialized? 3484 */ 3485 static int 3486 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3487 { 3488 mblk_t *mp; 3489 queue_t *q; 3490 conn_t *connp = tcp->tcp_connp; 3491 tcp_stack_t *tcps = tcp->tcp_tcps; 3492 sodirect_t *sodp; 3493 3494 TCP_CLD_STAT(tag); 3495 3496 #if TCP_TAG_CLEAN_DEATH 3497 tcp->tcp_cleandeathtag = tag; 3498 #endif 3499 3500 if (tcp->tcp_fused) 3501 tcp_unfuse(tcp); 3502 3503 if (tcp->tcp_linger_tid != 0 && 3504 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3505 tcp_stop_lingering(tcp); 3506 } 3507 3508 ASSERT(tcp != NULL); 3509 ASSERT((tcp->tcp_family == AF_INET && 3510 tcp->tcp_ipversion == IPV4_VERSION) || 3511 (tcp->tcp_family == AF_INET6 && 3512 (tcp->tcp_ipversion == IPV4_VERSION || 3513 tcp->tcp_ipversion == IPV6_VERSION))); 3514 3515 if (TCP_IS_DETACHED(tcp)) { 3516 if (tcp->tcp_hard_binding) { 3517 /* 3518 * Its an eager that we are dealing with. We close the 3519 * eager but in case a conn_ind has already gone to the 3520 * listener, let tcp_accept_finish() send a discon_ind 3521 * to the listener and drop the last reference. If the 3522 * listener doesn't even know about the eager i.e. the 3523 * conn_ind hasn't gone up, blow away the eager and drop 3524 * the last reference as well. If the conn_ind has gone 3525 * up, state should be BOUND. tcp_accept_finish 3526 * will figure out that the connection has received a 3527 * RST and will send a DISCON_IND to the application. 3528 */ 3529 tcp_closei_local(tcp); 3530 if (!tcp->tcp_tconnind_started) { 3531 CONN_DEC_REF(connp); 3532 } else { 3533 tcp->tcp_state = TCPS_BOUND; 3534 } 3535 } else { 3536 tcp_close_detached(tcp); 3537 } 3538 return (0); 3539 } 3540 3541 TCP_STAT(tcps, tcp_clean_death_nondetached); 3542 3543 /* If sodirect, not anymore */ 3544 SOD_PTR_ENTER(tcp, sodp); 3545 if (sodp != NULL) { 3546 tcp->tcp_sodirect = NULL; 3547 mutex_exit(sodp->sod_lockp); 3548 } 3549 3550 q = tcp->tcp_rq; 3551 3552 /* Trash all inbound data */ 3553 if (!IPCL_IS_NONSTR(connp)) { 3554 ASSERT(q != NULL); 3555 flushq(q, FLUSHALL); 3556 } 3557 3558 /* 3559 * If we are at least part way open and there is error 3560 * (err==0 implies no error) 3561 * notify our client by a T_DISCON_IND. 3562 */ 3563 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3564 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3565 !TCP_IS_SOCKET(tcp)) { 3566 /* 3567 * Send M_FLUSH according to TPI. Because sockets will 3568 * (and must) ignore FLUSHR we do that only for TPI 3569 * endpoints and sockets in STREAMS mode. 3570 */ 3571 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3572 } 3573 if (tcp->tcp_debug) { 3574 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3575 "tcp_clean_death: discon err %d", err); 3576 } 3577 if (IPCL_IS_NONSTR(connp)) { 3578 /* Direct socket, use upcall */ 3579 (*connp->conn_upcalls->su_disconnected)( 3580 connp->conn_upper_handle, tcp->tcp_connid, err); 3581 } else { 3582 mp = mi_tpi_discon_ind(NULL, err, 0); 3583 if (mp != NULL) { 3584 putnext(q, mp); 3585 } else { 3586 if (tcp->tcp_debug) { 3587 (void) strlog(TCP_MOD_ID, 0, 1, 3588 SL_ERROR|SL_TRACE, 3589 "tcp_clean_death, sending M_ERROR"); 3590 } 3591 (void) putnextctl1(q, M_ERROR, EPROTO); 3592 } 3593 } 3594 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3595 /* SYN_SENT or SYN_RCVD */ 3596 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3597 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3598 /* ESTABLISHED or CLOSE_WAIT */ 3599 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3600 } 3601 } 3602 3603 tcp_reinit(tcp); 3604 if (IPCL_IS_NONSTR(connp)) 3605 (void) tcp_do_unbind(connp); 3606 3607 return (-1); 3608 } 3609 3610 /* 3611 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3612 * to expire, stop the wait and finish the close. 3613 */ 3614 static void 3615 tcp_stop_lingering(tcp_t *tcp) 3616 { 3617 clock_t delta = 0; 3618 tcp_stack_t *tcps = tcp->tcp_tcps; 3619 3620 tcp->tcp_linger_tid = 0; 3621 if (tcp->tcp_state > TCPS_LISTEN) { 3622 tcp_acceptor_hash_remove(tcp); 3623 mutex_enter(&tcp->tcp_non_sq_lock); 3624 if (tcp->tcp_flow_stopped) { 3625 tcp_clrqfull(tcp); 3626 } 3627 mutex_exit(&tcp->tcp_non_sq_lock); 3628 3629 if (tcp->tcp_timer_tid != 0) { 3630 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3631 tcp->tcp_timer_tid = 0; 3632 } 3633 /* 3634 * Need to cancel those timers which will not be used when 3635 * TCP is detached. This has to be done before the tcp_wq 3636 * is set to the global queue. 3637 */ 3638 tcp_timers_stop(tcp); 3639 3640 tcp->tcp_detached = B_TRUE; 3641 ASSERT(tcps->tcps_g_q != NULL); 3642 tcp->tcp_rq = tcps->tcps_g_q; 3643 tcp->tcp_wq = WR(tcps->tcps_g_q); 3644 3645 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3646 tcp_time_wait_append(tcp); 3647 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3648 goto finish; 3649 } 3650 3651 /* 3652 * If delta is zero the timer event wasn't executed and was 3653 * successfully canceled. In this case we need to restart it 3654 * with the minimal delta possible. 3655 */ 3656 if (delta >= 0) { 3657 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3658 delta ? delta : 1); 3659 } 3660 } else { 3661 tcp_closei_local(tcp); 3662 CONN_DEC_REF(tcp->tcp_connp); 3663 } 3664 finish: 3665 /* Signal closing thread that it can complete close */ 3666 mutex_enter(&tcp->tcp_closelock); 3667 tcp->tcp_detached = B_TRUE; 3668 ASSERT(tcps->tcps_g_q != NULL); 3669 3670 tcp->tcp_rq = tcps->tcps_g_q; 3671 tcp->tcp_wq = WR(tcps->tcps_g_q); 3672 3673 tcp->tcp_closed = 1; 3674 cv_signal(&tcp->tcp_closecv); 3675 mutex_exit(&tcp->tcp_closelock); 3676 } 3677 3678 /* 3679 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3680 * expires. 3681 */ 3682 static void 3683 tcp_close_linger_timeout(void *arg) 3684 { 3685 conn_t *connp = (conn_t *)arg; 3686 tcp_t *tcp = connp->conn_tcp; 3687 3688 tcp->tcp_client_errno = ETIMEDOUT; 3689 tcp_stop_lingering(tcp); 3690 } 3691 3692 static void 3693 tcp_close_common(conn_t *connp, int flags) 3694 { 3695 tcp_t *tcp = connp->conn_tcp; 3696 mblk_t *mp = &tcp->tcp_closemp; 3697 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3698 mblk_t *bp; 3699 3700 ASSERT(connp->conn_ref >= 2); 3701 3702 /* 3703 * Mark the conn as closing. ill_pending_mp_add will not 3704 * add any mp to the pending mp list, after this conn has 3705 * started closing. Same for sq_pending_mp_add 3706 */ 3707 mutex_enter(&connp->conn_lock); 3708 connp->conn_state_flags |= CONN_CLOSING; 3709 if (connp->conn_oper_pending_ill != NULL) 3710 conn_ioctl_cleanup_reqd = B_TRUE; 3711 CONN_INC_REF_LOCKED(connp); 3712 mutex_exit(&connp->conn_lock); 3713 tcp->tcp_closeflags = (uint8_t)flags; 3714 ASSERT(connp->conn_ref >= 3); 3715 3716 /* 3717 * tcp_closemp_used is used below without any protection of a lock 3718 * as we don't expect any one else to use it concurrently at this 3719 * point otherwise it would be a major defect. 3720 */ 3721 3722 if (mp->b_prev == NULL) 3723 tcp->tcp_closemp_used = B_TRUE; 3724 else 3725 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3726 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3727 3728 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3729 3730 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3731 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3732 3733 mutex_enter(&tcp->tcp_closelock); 3734 while (!tcp->tcp_closed) { 3735 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3736 /* 3737 * The cv_wait_sig() was interrupted. We now do the 3738 * following: 3739 * 3740 * 1) If the endpoint was lingering, we allow this 3741 * to be interrupted by cancelling the linger timeout 3742 * and closing normally. 3743 * 3744 * 2) Revert to calling cv_wait() 3745 * 3746 * We revert to using cv_wait() to avoid an 3747 * infinite loop which can occur if the calling 3748 * thread is higher priority than the squeue worker 3749 * thread and is bound to the same cpu. 3750 */ 3751 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3752 mutex_exit(&tcp->tcp_closelock); 3753 /* Entering squeue, bump ref count. */ 3754 CONN_INC_REF(connp); 3755 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3756 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3757 tcp_linger_interrupted, connp, 3758 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3759 mutex_enter(&tcp->tcp_closelock); 3760 } 3761 break; 3762 } 3763 } 3764 while (!tcp->tcp_closed) 3765 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3766 mutex_exit(&tcp->tcp_closelock); 3767 3768 /* 3769 * In the case of listener streams that have eagers in the q or q0 3770 * we wait for the eagers to drop their reference to us. tcp_rq and 3771 * tcp_wq of the eagers point to our queues. By waiting for the 3772 * refcnt to drop to 1, we are sure that the eagers have cleaned 3773 * up their queue pointers and also dropped their references to us. 3774 */ 3775 if (tcp->tcp_wait_for_eagers) { 3776 mutex_enter(&connp->conn_lock); 3777 while (connp->conn_ref != 1) { 3778 cv_wait(&connp->conn_cv, &connp->conn_lock); 3779 } 3780 mutex_exit(&connp->conn_lock); 3781 } 3782 /* 3783 * ioctl cleanup. The mp is queued in the 3784 * ill_pending_mp or in the sq_pending_mp. 3785 */ 3786 if (conn_ioctl_cleanup_reqd) 3787 conn_ioctl_cleanup(connp); 3788 3789 tcp->tcp_cpid = -1; 3790 } 3791 3792 static int 3793 tcp_tpi_close(queue_t *q, int flags) 3794 { 3795 conn_t *connp; 3796 3797 ASSERT(WR(q)->q_next == NULL); 3798 3799 if (flags & SO_FALLBACK) { 3800 /* 3801 * stream is being closed while in fallback 3802 * simply free the resources that were allocated 3803 */ 3804 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3805 qprocsoff(q); 3806 goto done; 3807 } 3808 3809 connp = Q_TO_CONN(q); 3810 /* 3811 * We are being closed as /dev/tcp or /dev/tcp6. 3812 */ 3813 tcp_close_common(connp, flags); 3814 3815 qprocsoff(q); 3816 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3817 3818 /* 3819 * Drop IP's reference on the conn. This is the last reference 3820 * on the connp if the state was less than established. If the 3821 * connection has gone into timewait state, then we will have 3822 * one ref for the TCP and one more ref (total of two) for the 3823 * classifier connected hash list (a timewait connections stays 3824 * in connected hash till closed). 3825 * 3826 * We can't assert the references because there might be other 3827 * transient reference places because of some walkers or queued 3828 * packets in squeue for the timewait state. 3829 */ 3830 CONN_DEC_REF(connp); 3831 done: 3832 q->q_ptr = WR(q)->q_ptr = NULL; 3833 return (0); 3834 } 3835 3836 static int 3837 tcpclose_accept(queue_t *q) 3838 { 3839 vmem_t *minor_arena; 3840 dev_t conn_dev; 3841 3842 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3843 3844 /* 3845 * We had opened an acceptor STREAM for sockfs which is 3846 * now being closed due to some error. 3847 */ 3848 qprocsoff(q); 3849 3850 minor_arena = (vmem_t *)WR(q)->q_ptr; 3851 conn_dev = (dev_t)RD(q)->q_ptr; 3852 ASSERT(minor_arena != NULL); 3853 ASSERT(conn_dev != 0); 3854 inet_minor_free(minor_arena, conn_dev); 3855 q->q_ptr = WR(q)->q_ptr = NULL; 3856 return (0); 3857 } 3858 3859 /* 3860 * Called by tcp_close() routine via squeue when lingering is 3861 * interrupted by a signal. 3862 */ 3863 3864 /* ARGSUSED */ 3865 static void 3866 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3867 { 3868 conn_t *connp = (conn_t *)arg; 3869 tcp_t *tcp = connp->conn_tcp; 3870 3871 freeb(mp); 3872 if (tcp->tcp_linger_tid != 0 && 3873 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3874 tcp_stop_lingering(tcp); 3875 tcp->tcp_client_errno = EINTR; 3876 } 3877 } 3878 3879 /* 3880 * Called by streams close routine via squeues when our client blows off her 3881 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3882 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3883 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3884 * acked. 3885 * 3886 * NOTE: tcp_close potentially returns error when lingering. 3887 * However, the stream head currently does not pass these errors 3888 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3889 * errors to the application (from tsleep()) and not errors 3890 * like ECONNRESET caused by receiving a reset packet. 3891 */ 3892 3893 /* ARGSUSED */ 3894 static void 3895 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3896 { 3897 char *msg; 3898 conn_t *connp = (conn_t *)arg; 3899 tcp_t *tcp = connp->conn_tcp; 3900 clock_t delta = 0; 3901 tcp_stack_t *tcps = tcp->tcp_tcps; 3902 3903 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3904 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3905 3906 mutex_enter(&tcp->tcp_eager_lock); 3907 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3908 /* Cleanup for listener */ 3909 tcp_eager_cleanup(tcp, 0); 3910 tcp->tcp_wait_for_eagers = 1; 3911 } 3912 mutex_exit(&tcp->tcp_eager_lock); 3913 3914 connp->conn_mdt_ok = B_FALSE; 3915 tcp->tcp_mdt = B_FALSE; 3916 3917 connp->conn_lso_ok = B_FALSE; 3918 tcp->tcp_lso = B_FALSE; 3919 3920 msg = NULL; 3921 switch (tcp->tcp_state) { 3922 case TCPS_CLOSED: 3923 case TCPS_IDLE: 3924 case TCPS_BOUND: 3925 case TCPS_LISTEN: 3926 break; 3927 case TCPS_SYN_SENT: 3928 msg = "tcp_close, during connect"; 3929 break; 3930 case TCPS_SYN_RCVD: 3931 /* 3932 * Close during the connect 3-way handshake 3933 * but here there may or may not be pending data 3934 * already on queue. Process almost same as in 3935 * the ESTABLISHED state. 3936 */ 3937 /* FALLTHRU */ 3938 default: 3939 if (tcp->tcp_sodirect != NULL) { 3940 /* Ok, no more sodirect */ 3941 tcp->tcp_sodirect = NULL; 3942 } 3943 3944 if (tcp->tcp_fused) 3945 tcp_unfuse(tcp); 3946 3947 /* 3948 * If SO_LINGER has set a zero linger time, abort the 3949 * connection with a reset. 3950 */ 3951 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3952 msg = "tcp_close, zero lingertime"; 3953 break; 3954 } 3955 3956 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3957 /* 3958 * Abort connection if there is unread data queued. 3959 */ 3960 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3961 msg = "tcp_close, unread data"; 3962 break; 3963 } 3964 /* 3965 * tcp_hard_bound is now cleared thus all packets go through 3966 * tcp_lookup. This fact is used by tcp_detach below. 3967 * 3968 * We have done a qwait() above which could have possibly 3969 * drained more messages in turn causing transition to a 3970 * different state. Check whether we have to do the rest 3971 * of the processing or not. 3972 */ 3973 if (tcp->tcp_state <= TCPS_LISTEN) 3974 break; 3975 3976 /* 3977 * Transmit the FIN before detaching the tcp_t. 3978 * After tcp_detach returns this queue/perimeter 3979 * no longer owns the tcp_t thus others can modify it. 3980 */ 3981 (void) tcp_xmit_end(tcp); 3982 3983 /* 3984 * If lingering on close then wait until the fin is acked, 3985 * the SO_LINGER time passes, or a reset is sent/received. 3986 */ 3987 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3988 !(tcp->tcp_fin_acked) && 3989 tcp->tcp_state >= TCPS_ESTABLISHED) { 3990 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3991 tcp->tcp_client_errno = EWOULDBLOCK; 3992 } else if (tcp->tcp_client_errno == 0) { 3993 3994 ASSERT(tcp->tcp_linger_tid == 0); 3995 3996 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3997 tcp_close_linger_timeout, 3998 tcp->tcp_lingertime * hz); 3999 4000 /* tcp_close_linger_timeout will finish close */ 4001 if (tcp->tcp_linger_tid == 0) 4002 tcp->tcp_client_errno = ENOSR; 4003 else 4004 return; 4005 } 4006 4007 /* 4008 * Check if we need to detach or just close 4009 * the instance. 4010 */ 4011 if (tcp->tcp_state <= TCPS_LISTEN) 4012 break; 4013 } 4014 4015 /* 4016 * Make sure that no other thread will access the tcp_rq of 4017 * this instance (through lookups etc.) as tcp_rq will go 4018 * away shortly. 4019 */ 4020 tcp_acceptor_hash_remove(tcp); 4021 4022 mutex_enter(&tcp->tcp_non_sq_lock); 4023 if (tcp->tcp_flow_stopped) { 4024 tcp_clrqfull(tcp); 4025 } 4026 mutex_exit(&tcp->tcp_non_sq_lock); 4027 4028 if (tcp->tcp_timer_tid != 0) { 4029 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4030 tcp->tcp_timer_tid = 0; 4031 } 4032 /* 4033 * Need to cancel those timers which will not be used when 4034 * TCP is detached. This has to be done before the tcp_wq 4035 * is set to the global queue. 4036 */ 4037 tcp_timers_stop(tcp); 4038 4039 tcp->tcp_detached = B_TRUE; 4040 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4041 tcp_time_wait_append(tcp); 4042 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 4043 ASSERT(connp->conn_ref >= 3); 4044 goto finish; 4045 } 4046 4047 /* 4048 * If delta is zero the timer event wasn't executed and was 4049 * successfully canceled. In this case we need to restart it 4050 * with the minimal delta possible. 4051 */ 4052 if (delta >= 0) 4053 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4054 delta ? delta : 1); 4055 4056 ASSERT(connp->conn_ref >= 3); 4057 goto finish; 4058 } 4059 4060 /* Detach did not complete. Still need to remove q from stream. */ 4061 if (msg) { 4062 if (tcp->tcp_state == TCPS_ESTABLISHED || 4063 tcp->tcp_state == TCPS_CLOSE_WAIT) 4064 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 4065 if (tcp->tcp_state == TCPS_SYN_SENT || 4066 tcp->tcp_state == TCPS_SYN_RCVD) 4067 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 4068 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4069 } 4070 4071 tcp_closei_local(tcp); 4072 CONN_DEC_REF(connp); 4073 ASSERT(connp->conn_ref >= 2); 4074 4075 finish: 4076 /* 4077 * Although packets are always processed on the correct 4078 * tcp's perimeter and access is serialized via squeue's, 4079 * IP still needs a queue when sending packets in time_wait 4080 * state so use WR(tcps_g_q) till ip_output() can be 4081 * changed to deal with just connp. For read side, we 4082 * could have set tcp_rq to NULL but there are some cases 4083 * in tcp_rput_data() from early days of this code which 4084 * do a putnext without checking if tcp is closed. Those 4085 * need to be identified before both tcp_rq and tcp_wq 4086 * can be set to NULL and tcps_g_q can disappear forever. 4087 */ 4088 mutex_enter(&tcp->tcp_closelock); 4089 /* 4090 * Don't change the queues in the case of a listener that has 4091 * eagers in its q or q0. It could surprise the eagers. 4092 * Instead wait for the eagers outside the squeue. 4093 */ 4094 if (!tcp->tcp_wait_for_eagers) { 4095 tcp->tcp_detached = B_TRUE; 4096 /* 4097 * When default queue is closing we set tcps_g_q to NULL 4098 * after the close is done. 4099 */ 4100 ASSERT(tcps->tcps_g_q != NULL); 4101 tcp->tcp_rq = tcps->tcps_g_q; 4102 tcp->tcp_wq = WR(tcps->tcps_g_q); 4103 } 4104 4105 /* Signal tcp_close() to finish closing. */ 4106 tcp->tcp_closed = 1; 4107 cv_signal(&tcp->tcp_closecv); 4108 mutex_exit(&tcp->tcp_closelock); 4109 } 4110 4111 4112 /* 4113 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4114 * Some stream heads get upset if they see these later on as anything but NULL. 4115 */ 4116 static void 4117 tcp_close_mpp(mblk_t **mpp) 4118 { 4119 mblk_t *mp; 4120 4121 if ((mp = *mpp) != NULL) { 4122 do { 4123 mp->b_next = NULL; 4124 mp->b_prev = NULL; 4125 } while ((mp = mp->b_cont) != NULL); 4126 4127 mp = *mpp; 4128 *mpp = NULL; 4129 freemsg(mp); 4130 } 4131 } 4132 4133 /* Do detached close. */ 4134 static void 4135 tcp_close_detached(tcp_t *tcp) 4136 { 4137 if (tcp->tcp_fused) 4138 tcp_unfuse(tcp); 4139 4140 /* 4141 * Clustering code serializes TCP disconnect callbacks and 4142 * cluster tcp list walks by blocking a TCP disconnect callback 4143 * if a cluster tcp list walk is in progress. This ensures 4144 * accurate accounting of TCPs in the cluster code even though 4145 * the TCP list walk itself is not atomic. 4146 */ 4147 tcp_closei_local(tcp); 4148 CONN_DEC_REF(tcp->tcp_connp); 4149 } 4150 4151 /* 4152 * Stop all TCP timers, and free the timer mblks if requested. 4153 */ 4154 void 4155 tcp_timers_stop(tcp_t *tcp) 4156 { 4157 if (tcp->tcp_timer_tid != 0) { 4158 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4159 tcp->tcp_timer_tid = 0; 4160 } 4161 if (tcp->tcp_ka_tid != 0) { 4162 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4163 tcp->tcp_ka_tid = 0; 4164 } 4165 if (tcp->tcp_ack_tid != 0) { 4166 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4167 tcp->tcp_ack_tid = 0; 4168 } 4169 if (tcp->tcp_push_tid != 0) { 4170 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4171 tcp->tcp_push_tid = 0; 4172 } 4173 } 4174 4175 /* 4176 * The tcp_t is going away. Remove it from all lists and set it 4177 * to TCPS_CLOSED. The freeing up of memory is deferred until 4178 * tcp_inactive. This is needed since a thread in tcp_rput might have 4179 * done a CONN_INC_REF on this structure before it was removed from the 4180 * hashes. 4181 */ 4182 static void 4183 tcp_closei_local(tcp_t *tcp) 4184 { 4185 ire_t *ire; 4186 conn_t *connp = tcp->tcp_connp; 4187 tcp_stack_t *tcps = tcp->tcp_tcps; 4188 4189 if (!TCP_IS_SOCKET(tcp)) 4190 tcp_acceptor_hash_remove(tcp); 4191 4192 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4193 tcp->tcp_ibsegs = 0; 4194 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4195 tcp->tcp_obsegs = 0; 4196 4197 /* 4198 * If we are an eager connection hanging off a listener that 4199 * hasn't formally accepted the connection yet, get off his 4200 * list and blow off any data that we have accumulated. 4201 */ 4202 if (tcp->tcp_listener != NULL) { 4203 tcp_t *listener = tcp->tcp_listener; 4204 mutex_enter(&listener->tcp_eager_lock); 4205 /* 4206 * tcp_tconnind_started == B_TRUE means that the 4207 * conn_ind has already gone to listener. At 4208 * this point, eager will be closed but we 4209 * leave it in listeners eager list so that 4210 * if listener decides to close without doing 4211 * accept, we can clean this up. In tcp_wput_accept 4212 * we take care of the case of accept on closed 4213 * eager. 4214 */ 4215 if (!tcp->tcp_tconnind_started) { 4216 tcp_eager_unlink(tcp); 4217 mutex_exit(&listener->tcp_eager_lock); 4218 /* 4219 * We don't want to have any pointers to the 4220 * listener queue, after we have released our 4221 * reference on the listener 4222 */ 4223 ASSERT(tcps->tcps_g_q != NULL); 4224 tcp->tcp_rq = tcps->tcps_g_q; 4225 tcp->tcp_wq = WR(tcps->tcps_g_q); 4226 CONN_DEC_REF(listener->tcp_connp); 4227 } else { 4228 mutex_exit(&listener->tcp_eager_lock); 4229 } 4230 } 4231 4232 /* Stop all the timers */ 4233 tcp_timers_stop(tcp); 4234 4235 if (tcp->tcp_state == TCPS_LISTEN) { 4236 if (tcp->tcp_ip_addr_cache) { 4237 kmem_free((void *)tcp->tcp_ip_addr_cache, 4238 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4239 tcp->tcp_ip_addr_cache = NULL; 4240 } 4241 } 4242 mutex_enter(&tcp->tcp_non_sq_lock); 4243 if (tcp->tcp_flow_stopped) 4244 tcp_clrqfull(tcp); 4245 mutex_exit(&tcp->tcp_non_sq_lock); 4246 4247 tcp_bind_hash_remove(tcp); 4248 /* 4249 * If the tcp_time_wait_collector (which runs outside the squeue) 4250 * is trying to remove this tcp from the time wait list, we will 4251 * block in tcp_time_wait_remove while trying to acquire the 4252 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4253 * requires the ipcl_hash_remove to be ordered after the 4254 * tcp_time_wait_remove for the refcnt checks to work correctly. 4255 */ 4256 if (tcp->tcp_state == TCPS_TIME_WAIT) 4257 (void) tcp_time_wait_remove(tcp, NULL); 4258 CL_INET_DISCONNECT(connp, tcp); 4259 ipcl_hash_remove(connp); 4260 4261 /* 4262 * Delete the cached ire in conn_ire_cache and also mark 4263 * the conn as CONDEMNED 4264 */ 4265 mutex_enter(&connp->conn_lock); 4266 connp->conn_state_flags |= CONN_CONDEMNED; 4267 ire = connp->conn_ire_cache; 4268 connp->conn_ire_cache = NULL; 4269 mutex_exit(&connp->conn_lock); 4270 if (ire != NULL) 4271 IRE_REFRELE_NOTR(ire); 4272 4273 /* Need to cleanup any pending ioctls */ 4274 ASSERT(tcp->tcp_time_wait_next == NULL); 4275 ASSERT(tcp->tcp_time_wait_prev == NULL); 4276 ASSERT(tcp->tcp_time_wait_expire == 0); 4277 tcp->tcp_state = TCPS_CLOSED; 4278 4279 /* Release any SSL context */ 4280 if (tcp->tcp_kssl_ent != NULL) { 4281 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4282 tcp->tcp_kssl_ent = NULL; 4283 } 4284 if (tcp->tcp_kssl_ctx != NULL) { 4285 kssl_release_ctx(tcp->tcp_kssl_ctx); 4286 tcp->tcp_kssl_ctx = NULL; 4287 } 4288 tcp->tcp_kssl_pending = B_FALSE; 4289 4290 tcp_ipsec_cleanup(tcp); 4291 } 4292 4293 /* 4294 * tcp is dying (called from ipcl_conn_destroy and error cases). 4295 * Free the tcp_t in either case. 4296 */ 4297 void 4298 tcp_free(tcp_t *tcp) 4299 { 4300 mblk_t *mp; 4301 ip6_pkt_t *ipp; 4302 4303 ASSERT(tcp != NULL); 4304 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4305 4306 tcp->tcp_rq = NULL; 4307 tcp->tcp_wq = NULL; 4308 4309 tcp_close_mpp(&tcp->tcp_xmit_head); 4310 tcp_close_mpp(&tcp->tcp_reass_head); 4311 if (tcp->tcp_rcv_list != NULL) { 4312 /* Free b_next chain */ 4313 tcp_close_mpp(&tcp->tcp_rcv_list); 4314 } 4315 if ((mp = tcp->tcp_urp_mp) != NULL) { 4316 freemsg(mp); 4317 } 4318 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4319 freemsg(mp); 4320 } 4321 4322 if (tcp->tcp_fused_sigurg_mp != NULL) { 4323 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4324 freeb(tcp->tcp_fused_sigurg_mp); 4325 tcp->tcp_fused_sigurg_mp = NULL; 4326 } 4327 4328 if (tcp->tcp_ordrel_mp != NULL) { 4329 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4330 freeb(tcp->tcp_ordrel_mp); 4331 tcp->tcp_ordrel_mp = NULL; 4332 } 4333 4334 if (tcp->tcp_sack_info != NULL) { 4335 if (tcp->tcp_notsack_list != NULL) { 4336 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4337 } 4338 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4339 } 4340 4341 if (tcp->tcp_hopopts != NULL) { 4342 mi_free(tcp->tcp_hopopts); 4343 tcp->tcp_hopopts = NULL; 4344 tcp->tcp_hopoptslen = 0; 4345 } 4346 ASSERT(tcp->tcp_hopoptslen == 0); 4347 if (tcp->tcp_dstopts != NULL) { 4348 mi_free(tcp->tcp_dstopts); 4349 tcp->tcp_dstopts = NULL; 4350 tcp->tcp_dstoptslen = 0; 4351 } 4352 ASSERT(tcp->tcp_dstoptslen == 0); 4353 if (tcp->tcp_rtdstopts != NULL) { 4354 mi_free(tcp->tcp_rtdstopts); 4355 tcp->tcp_rtdstopts = NULL; 4356 tcp->tcp_rtdstoptslen = 0; 4357 } 4358 ASSERT(tcp->tcp_rtdstoptslen == 0); 4359 if (tcp->tcp_rthdr != NULL) { 4360 mi_free(tcp->tcp_rthdr); 4361 tcp->tcp_rthdr = NULL; 4362 tcp->tcp_rthdrlen = 0; 4363 } 4364 ASSERT(tcp->tcp_rthdrlen == 0); 4365 4366 ipp = &tcp->tcp_sticky_ipp; 4367 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4368 IPPF_RTHDR)) 4369 ip6_pkt_free(ipp); 4370 4371 /* 4372 * Free memory associated with the tcp/ip header template. 4373 */ 4374 4375 if (tcp->tcp_iphc != NULL) 4376 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4377 4378 /* 4379 * Following is really a blowing away a union. 4380 * It happens to have exactly two members of identical size 4381 * the following code is enough. 4382 */ 4383 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4384 } 4385 4386 4387 /* 4388 * Put a connection confirmation message upstream built from the 4389 * address information within 'iph' and 'tcph'. Report our success or failure. 4390 */ 4391 static boolean_t 4392 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4393 mblk_t **defermp) 4394 { 4395 sin_t sin; 4396 sin6_t sin6; 4397 mblk_t *mp; 4398 char *optp = NULL; 4399 int optlen = 0; 4400 cred_t *cr; 4401 4402 if (defermp != NULL) 4403 *defermp = NULL; 4404 4405 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4406 /* 4407 * Return in T_CONN_CON results of option negotiation through 4408 * the T_CONN_REQ. Note: If there is an real end-to-end option 4409 * negotiation, then what is received from remote end needs 4410 * to be taken into account but there is no such thing (yet?) 4411 * in our TCP/IP. 4412 * Note: We do not use mi_offset_param() here as 4413 * tcp_opts_conn_req contents do not directly come from 4414 * an application and are either generated in kernel or 4415 * from user input that was already verified. 4416 */ 4417 mp = tcp->tcp_conn.tcp_opts_conn_req; 4418 optp = (char *)(mp->b_rptr + 4419 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4420 optlen = (int) 4421 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4422 } 4423 4424 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4425 ipha_t *ipha = (ipha_t *)iphdr; 4426 4427 /* packet is IPv4 */ 4428 if (tcp->tcp_family == AF_INET) { 4429 sin = sin_null; 4430 sin.sin_addr.s_addr = ipha->ipha_src; 4431 sin.sin_port = *(uint16_t *)tcph->th_lport; 4432 sin.sin_family = AF_INET; 4433 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4434 (int)sizeof (sin_t), optp, optlen); 4435 } else { 4436 sin6 = sin6_null; 4437 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4438 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4439 sin6.sin6_family = AF_INET6; 4440 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4441 (int)sizeof (sin6_t), optp, optlen); 4442 4443 } 4444 } else { 4445 ip6_t *ip6h = (ip6_t *)iphdr; 4446 4447 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4448 ASSERT(tcp->tcp_family == AF_INET6); 4449 sin6 = sin6_null; 4450 sin6.sin6_addr = ip6h->ip6_src; 4451 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4452 sin6.sin6_family = AF_INET6; 4453 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4454 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4455 (int)sizeof (sin6_t), optp, optlen); 4456 } 4457 4458 if (!mp) 4459 return (B_FALSE); 4460 4461 if ((cr = DB_CRED(idmp)) != NULL) { 4462 mblk_setcred(mp, cr); 4463 DB_CPID(mp) = DB_CPID(idmp); 4464 } 4465 4466 if (defermp == NULL) { 4467 conn_t *connp = tcp->tcp_connp; 4468 if (IPCL_IS_NONSTR(connp)) { 4469 (*connp->conn_upcalls->su_connected) 4470 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4471 DB_CPID(mp)); 4472 freemsg(mp); 4473 } else { 4474 putnext(tcp->tcp_rq, mp); 4475 } 4476 } else { 4477 *defermp = mp; 4478 } 4479 4480 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4481 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4482 return (B_TRUE); 4483 } 4484 4485 /* 4486 * Defense for the SYN attack - 4487 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4488 * one from the list of droppable eagers. This list is a subset of q0. 4489 * see comments before the definition of MAKE_DROPPABLE(). 4490 * 2. Don't drop a SYN request before its first timeout. This gives every 4491 * request at least til the first timeout to complete its 3-way handshake. 4492 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4493 * requests currently on the queue that has timed out. This will be used 4494 * as an indicator of whether an attack is under way, so that appropriate 4495 * actions can be taken. (It's incremented in tcp_timer() and decremented 4496 * either when eager goes into ESTABLISHED, or gets freed up.) 4497 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4498 * # of timeout drops back to <= q0len/32 => SYN alert off 4499 */ 4500 static boolean_t 4501 tcp_drop_q0(tcp_t *tcp) 4502 { 4503 tcp_t *eager; 4504 mblk_t *mp; 4505 tcp_stack_t *tcps = tcp->tcp_tcps; 4506 4507 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4508 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4509 4510 /* Pick oldest eager from the list of droppable eagers */ 4511 eager = tcp->tcp_eager_prev_drop_q0; 4512 4513 /* If list is empty. return B_FALSE */ 4514 if (eager == tcp) { 4515 return (B_FALSE); 4516 } 4517 4518 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4519 if ((mp = allocb(0, BPRI_HI)) == NULL) 4520 return (B_FALSE); 4521 4522 /* 4523 * Take this eager out from the list of droppable eagers since we are 4524 * going to drop it. 4525 */ 4526 MAKE_UNDROPPABLE(eager); 4527 4528 if (tcp->tcp_debug) { 4529 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4530 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4531 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4532 tcp->tcp_conn_req_cnt_q0, 4533 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4534 } 4535 4536 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4537 4538 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4539 CONN_INC_REF(eager->tcp_connp); 4540 4541 /* Mark the IRE created for this SYN request temporary */ 4542 tcp_ip_ire_mark_advice(eager); 4543 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4544 tcp_clean_death_wrapper, eager->tcp_connp, 4545 SQ_FILL, SQTAG_TCP_DROP_Q0); 4546 4547 return (B_TRUE); 4548 } 4549 4550 int 4551 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4552 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4553 { 4554 tcp_t *ltcp = lconnp->conn_tcp; 4555 tcp_t *tcp = connp->conn_tcp; 4556 mblk_t *tpi_mp; 4557 ipha_t *ipha; 4558 ip6_t *ip6h; 4559 sin6_t sin6; 4560 in6_addr_t v6dst; 4561 int err; 4562 int ifindex = 0; 4563 cred_t *cr; 4564 tcp_stack_t *tcps = tcp->tcp_tcps; 4565 4566 if (ipvers == IPV4_VERSION) { 4567 ipha = (ipha_t *)mp->b_rptr; 4568 4569 connp->conn_send = ip_output; 4570 connp->conn_recv = tcp_input; 4571 4572 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4573 &connp->conn_bound_source_v6); 4574 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4575 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4576 4577 sin6 = sin6_null; 4578 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4579 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4580 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4581 sin6.sin6_family = AF_INET6; 4582 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4583 lconnp->conn_zoneid, tcps->tcps_netstack); 4584 if (tcp->tcp_recvdstaddr) { 4585 sin6_t sin6d; 4586 4587 sin6d = sin6_null; 4588 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4589 &sin6d.sin6_addr); 4590 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4591 sin6d.sin6_family = AF_INET; 4592 tpi_mp = mi_tpi_extconn_ind(NULL, 4593 (char *)&sin6d, sizeof (sin6_t), 4594 (char *)&tcp, 4595 (t_scalar_t)sizeof (intptr_t), 4596 (char *)&sin6d, sizeof (sin6_t), 4597 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4598 } else { 4599 tpi_mp = mi_tpi_conn_ind(NULL, 4600 (char *)&sin6, sizeof (sin6_t), 4601 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4602 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4603 } 4604 } else { 4605 ip6h = (ip6_t *)mp->b_rptr; 4606 4607 connp->conn_send = ip_output_v6; 4608 connp->conn_recv = tcp_input; 4609 4610 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4611 connp->conn_srcv6 = ip6h->ip6_dst; 4612 connp->conn_remv6 = ip6h->ip6_src; 4613 4614 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4615 ifindex = (int)DB_CKSUMSTUFF(mp); 4616 DB_CKSUMSTUFF(mp) = 0; 4617 4618 sin6 = sin6_null; 4619 sin6.sin6_addr = ip6h->ip6_src; 4620 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4621 sin6.sin6_family = AF_INET6; 4622 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4623 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4624 lconnp->conn_zoneid, tcps->tcps_netstack); 4625 4626 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4627 /* Pass up the scope_id of remote addr */ 4628 sin6.sin6_scope_id = ifindex; 4629 } else { 4630 sin6.sin6_scope_id = 0; 4631 } 4632 if (tcp->tcp_recvdstaddr) { 4633 sin6_t sin6d; 4634 4635 sin6d = sin6_null; 4636 sin6.sin6_addr = ip6h->ip6_dst; 4637 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4638 sin6d.sin6_family = AF_INET; 4639 tpi_mp = mi_tpi_extconn_ind(NULL, 4640 (char *)&sin6d, sizeof (sin6_t), 4641 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4642 (char *)&sin6d, sizeof (sin6_t), 4643 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4644 } else { 4645 tpi_mp = mi_tpi_conn_ind(NULL, 4646 (char *)&sin6, sizeof (sin6_t), 4647 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4648 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4649 } 4650 } 4651 4652 if (tpi_mp == NULL) 4653 return (ENOMEM); 4654 4655 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4656 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4657 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4658 connp->conn_fully_bound = B_FALSE; 4659 4660 /* Inherit information from the "parent" */ 4661 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4662 tcp->tcp_family = ltcp->tcp_family; 4663 4664 tcp->tcp_wq = ltcp->tcp_wq; 4665 tcp->tcp_rq = ltcp->tcp_rq; 4666 4667 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4668 tcp->tcp_detached = B_TRUE; 4669 SOCK_CONNID_INIT(tcp->tcp_connid); 4670 if ((err = tcp_init_values(tcp)) != 0) { 4671 freemsg(tpi_mp); 4672 return (err); 4673 } 4674 4675 if (ipvers == IPV4_VERSION) { 4676 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4677 freemsg(tpi_mp); 4678 return (err); 4679 } 4680 ASSERT(tcp->tcp_ipha != NULL); 4681 } else { 4682 /* ifindex must be already set */ 4683 ASSERT(ifindex != 0); 4684 4685 if (ltcp->tcp_bound_if != 0) 4686 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4687 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4688 tcp->tcp_bound_if = ifindex; 4689 4690 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4691 tcp->tcp_recvifindex = 0; 4692 tcp->tcp_recvhops = 0xffffffffU; 4693 ASSERT(tcp->tcp_ip6h != NULL); 4694 } 4695 4696 tcp->tcp_lport = ltcp->tcp_lport; 4697 4698 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4699 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4700 /* 4701 * Listener had options of some sort; eager inherits. 4702 * Free up the eager template and allocate one 4703 * of the right size. 4704 */ 4705 if (tcp->tcp_hdr_grown) { 4706 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4707 } else { 4708 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4709 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4710 } 4711 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4712 KM_NOSLEEP); 4713 if (tcp->tcp_iphc == NULL) { 4714 tcp->tcp_iphc_len = 0; 4715 freemsg(tpi_mp); 4716 return (ENOMEM); 4717 } 4718 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4719 tcp->tcp_hdr_grown = B_TRUE; 4720 } 4721 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4722 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4723 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4724 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4725 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4726 4727 /* 4728 * Copy the IP+TCP header template from listener to eager 4729 */ 4730 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4731 if (tcp->tcp_ipversion == IPV6_VERSION) { 4732 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4733 IPPROTO_RAW) { 4734 tcp->tcp_ip6h = 4735 (ip6_t *)(tcp->tcp_iphc + 4736 sizeof (ip6i_t)); 4737 } else { 4738 tcp->tcp_ip6h = 4739 (ip6_t *)(tcp->tcp_iphc); 4740 } 4741 tcp->tcp_ipha = NULL; 4742 } else { 4743 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4744 tcp->tcp_ip6h = NULL; 4745 } 4746 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4747 tcp->tcp_ip_hdr_len); 4748 } else { 4749 /* 4750 * only valid case when ipversion of listener and 4751 * eager differ is when listener is IPv6 and 4752 * eager is IPv4. 4753 * Eager header template has been initialized to the 4754 * maximum v4 header sizes, which includes space for 4755 * TCP and IP options. 4756 */ 4757 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4758 (tcp->tcp_ipversion == IPV4_VERSION)); 4759 ASSERT(tcp->tcp_iphc_len >= 4760 TCP_MAX_COMBINED_HEADER_LENGTH); 4761 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4762 /* copy IP header fields individually */ 4763 tcp->tcp_ipha->ipha_ttl = 4764 ltcp->tcp_ip6h->ip6_hops; 4765 bcopy(ltcp->tcp_tcph->th_lport, 4766 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4767 } 4768 4769 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4770 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4771 sizeof (in_port_t)); 4772 4773 if (ltcp->tcp_lport == 0) { 4774 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4775 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4776 sizeof (in_port_t)); 4777 } 4778 4779 if (tcp->tcp_ipversion == IPV4_VERSION) { 4780 ASSERT(ipha != NULL); 4781 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4782 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4783 4784 /* Source routing option copyover (reverse it) */ 4785 if (tcps->tcps_rev_src_routes) 4786 tcp_opt_reverse(tcp, ipha); 4787 } else { 4788 ASSERT(ip6h != NULL); 4789 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4790 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4791 } 4792 4793 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4794 ASSERT(!tcp->tcp_tconnind_started); 4795 /* 4796 * If the SYN contains a credential, it's a loopback packet; attach 4797 * the credential to the TPI message. 4798 */ 4799 if ((cr = DB_CRED(idmp)) != NULL) { 4800 mblk_setcred(tpi_mp, cr); 4801 DB_CPID(tpi_mp) = DB_CPID(idmp); 4802 } 4803 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4804 4805 /* Inherit the listener's SSL protection state */ 4806 4807 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4808 kssl_hold_ent(tcp->tcp_kssl_ent); 4809 tcp->tcp_kssl_pending = B_TRUE; 4810 } 4811 4812 /* Inherit the listener's non-STREAMS flag */ 4813 if (IPCL_IS_NONSTR(lconnp)) { 4814 connp->conn_flags |= IPCL_NONSTR; 4815 connp->conn_upcalls = lconnp->conn_upcalls; 4816 } 4817 4818 return (0); 4819 } 4820 4821 4822 int 4823 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4824 tcph_t *tcph, mblk_t *idmp) 4825 { 4826 tcp_t *ltcp = lconnp->conn_tcp; 4827 tcp_t *tcp = connp->conn_tcp; 4828 sin_t sin; 4829 mblk_t *tpi_mp = NULL; 4830 int err; 4831 cred_t *cr; 4832 tcp_stack_t *tcps = tcp->tcp_tcps; 4833 4834 sin = sin_null; 4835 sin.sin_addr.s_addr = ipha->ipha_src; 4836 sin.sin_port = *(uint16_t *)tcph->th_lport; 4837 sin.sin_family = AF_INET; 4838 if (ltcp->tcp_recvdstaddr) { 4839 sin_t sind; 4840 4841 sind = sin_null; 4842 sind.sin_addr.s_addr = ipha->ipha_dst; 4843 sind.sin_port = *(uint16_t *)tcph->th_fport; 4844 sind.sin_family = AF_INET; 4845 tpi_mp = mi_tpi_extconn_ind(NULL, 4846 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4847 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4848 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4849 } else { 4850 tpi_mp = mi_tpi_conn_ind(NULL, 4851 (char *)&sin, sizeof (sin_t), 4852 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4853 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4854 } 4855 4856 if (tpi_mp == NULL) { 4857 return (ENOMEM); 4858 } 4859 4860 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4861 connp->conn_send = ip_output; 4862 connp->conn_recv = tcp_input; 4863 connp->conn_fully_bound = B_FALSE; 4864 4865 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4866 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4867 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4868 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4869 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4870 4871 /* Inherit information from the "parent" */ 4872 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4873 tcp->tcp_family = ltcp->tcp_family; 4874 tcp->tcp_wq = ltcp->tcp_wq; 4875 tcp->tcp_rq = ltcp->tcp_rq; 4876 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4877 tcp->tcp_detached = B_TRUE; 4878 SOCK_CONNID_INIT(tcp->tcp_connid); 4879 if ((err = tcp_init_values(tcp)) != 0) { 4880 freemsg(tpi_mp); 4881 return (err); 4882 } 4883 4884 /* 4885 * Let's make sure that eager tcp template has enough space to 4886 * copy IPv4 listener's tcp template. Since the conn_t structure is 4887 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4888 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4889 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4890 * extension headers or with ip6i_t struct). Note that bcopy() below 4891 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4892 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4893 */ 4894 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4895 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4896 4897 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4898 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4899 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4900 tcp->tcp_ttl = ltcp->tcp_ttl; 4901 tcp->tcp_tos = ltcp->tcp_tos; 4902 4903 /* Copy the IP+TCP header template from listener to eager */ 4904 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4905 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4906 tcp->tcp_ip6h = NULL; 4907 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4908 tcp->tcp_ip_hdr_len); 4909 4910 /* Initialize the IP addresses and Ports */ 4911 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4912 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4913 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4914 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4915 4916 /* Source routing option copyover (reverse it) */ 4917 if (tcps->tcps_rev_src_routes) 4918 tcp_opt_reverse(tcp, ipha); 4919 4920 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4921 ASSERT(!tcp->tcp_tconnind_started); 4922 4923 /* 4924 * If the SYN contains a credential, it's a loopback packet; attach 4925 * the credential to the TPI message. 4926 */ 4927 if ((cr = DB_CRED(idmp)) != NULL) { 4928 mblk_setcred(tpi_mp, cr); 4929 DB_CPID(tpi_mp) = DB_CPID(idmp); 4930 } 4931 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4932 4933 /* Inherit the listener's SSL protection state */ 4934 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4935 kssl_hold_ent(tcp->tcp_kssl_ent); 4936 tcp->tcp_kssl_pending = B_TRUE; 4937 } 4938 4939 /* Inherit the listener's non-STREAMS flag */ 4940 if (IPCL_IS_NONSTR(lconnp)) { 4941 connp->conn_flags |= IPCL_NONSTR; 4942 connp->conn_upcalls = lconnp->conn_upcalls; 4943 } 4944 4945 return (0); 4946 } 4947 4948 /* 4949 * sets up conn for ipsec. 4950 * if the first mblk is M_CTL it is consumed and mpp is updated. 4951 * in case of error mpp is freed. 4952 */ 4953 conn_t * 4954 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4955 { 4956 conn_t *connp = tcp->tcp_connp; 4957 conn_t *econnp; 4958 squeue_t *new_sqp; 4959 mblk_t *first_mp = *mpp; 4960 mblk_t *mp = *mpp; 4961 boolean_t mctl_present = B_FALSE; 4962 uint_t ipvers; 4963 4964 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4965 if (econnp == NULL) { 4966 freemsg(first_mp); 4967 return (NULL); 4968 } 4969 if (DB_TYPE(mp) == M_CTL) { 4970 if (mp->b_cont == NULL || 4971 mp->b_cont->b_datap->db_type != M_DATA) { 4972 freemsg(first_mp); 4973 return (NULL); 4974 } 4975 mp = mp->b_cont; 4976 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4977 freemsg(first_mp); 4978 return (NULL); 4979 } 4980 4981 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4982 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4983 mctl_present = B_TRUE; 4984 } else { 4985 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4986 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4987 } 4988 4989 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4990 DB_CKSUMSTART(mp) = 0; 4991 4992 ASSERT(OK_32PTR(mp->b_rptr)); 4993 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4994 if (ipvers == IPV4_VERSION) { 4995 uint16_t *up; 4996 uint32_t ports; 4997 ipha_t *ipha; 4998 4999 ipha = (ipha_t *)mp->b_rptr; 5000 up = (uint16_t *)((uchar_t *)ipha + 5001 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5002 ports = *(uint32_t *)up; 5003 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5004 ipha->ipha_dst, ipha->ipha_src, ports); 5005 } else { 5006 uint16_t *up; 5007 uint32_t ports; 5008 uint16_t ip_hdr_len; 5009 uint8_t *nexthdrp; 5010 ip6_t *ip6h; 5011 tcph_t *tcph; 5012 5013 ip6h = (ip6_t *)mp->b_rptr; 5014 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5015 ip_hdr_len = IPV6_HDR_LEN; 5016 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5017 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5018 CONN_DEC_REF(econnp); 5019 freemsg(first_mp); 5020 return (NULL); 5021 } 5022 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5023 up = (uint16_t *)tcph->th_lport; 5024 ports = *(uint32_t *)up; 5025 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5026 ip6h->ip6_dst, ip6h->ip6_src, ports); 5027 } 5028 5029 /* 5030 * The caller already ensured that there is a sqp present. 5031 */ 5032 econnp->conn_sqp = new_sqp; 5033 econnp->conn_initial_sqp = new_sqp; 5034 5035 if (connp->conn_policy != NULL) { 5036 ipsec_in_t *ii; 5037 ii = (ipsec_in_t *)(first_mp->b_rptr); 5038 ASSERT(ii->ipsec_in_policy == NULL); 5039 IPPH_REFHOLD(connp->conn_policy); 5040 ii->ipsec_in_policy = connp->conn_policy; 5041 5042 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5043 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5044 CONN_DEC_REF(econnp); 5045 freemsg(first_mp); 5046 return (NULL); 5047 } 5048 } 5049 5050 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5051 CONN_DEC_REF(econnp); 5052 freemsg(first_mp); 5053 return (NULL); 5054 } 5055 5056 /* 5057 * If we know we have some policy, pass the "IPSEC" 5058 * options size TCP uses this adjust the MSS. 5059 */ 5060 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5061 if (mctl_present) { 5062 freeb(first_mp); 5063 *mpp = mp; 5064 } 5065 5066 return (econnp); 5067 } 5068 5069 /* 5070 * tcp_get_conn/tcp_free_conn 5071 * 5072 * tcp_get_conn is used to get a clean tcp connection structure. 5073 * It tries to reuse the connections put on the freelist by the 5074 * time_wait_collector failing which it goes to kmem_cache. This 5075 * way has two benefits compared to just allocating from and 5076 * freeing to kmem_cache. 5077 * 1) The time_wait_collector can free (which includes the cleanup) 5078 * outside the squeue. So when the interrupt comes, we have a clean 5079 * connection sitting in the freelist. Obviously, this buys us 5080 * performance. 5081 * 5082 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5083 * has multiple disadvantages - tying up the squeue during alloc, and the 5084 * fact that IPSec policy initialization has to happen here which 5085 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5086 * But allocating the conn/tcp in IP land is also not the best since 5087 * we can't check the 'q' and 'q0' which are protected by squeue and 5088 * blindly allocate memory which might have to be freed here if we are 5089 * not allowed to accept the connection. By using the freelist and 5090 * putting the conn/tcp back in freelist, we don't pay a penalty for 5091 * allocating memory without checking 'q/q0' and freeing it if we can't 5092 * accept the connection. 5093 * 5094 * Care should be taken to put the conn back in the same squeue's freelist 5095 * from which it was allocated. Best results are obtained if conn is 5096 * allocated from listener's squeue and freed to the same. Time wait 5097 * collector will free up the freelist is the connection ends up sitting 5098 * there for too long. 5099 */ 5100 void * 5101 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5102 { 5103 tcp_t *tcp = NULL; 5104 conn_t *connp = NULL; 5105 squeue_t *sqp = (squeue_t *)arg; 5106 tcp_squeue_priv_t *tcp_time_wait; 5107 netstack_t *ns; 5108 5109 tcp_time_wait = 5110 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5111 5112 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5113 tcp = tcp_time_wait->tcp_free_list; 5114 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5115 if (tcp != NULL) { 5116 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5117 tcp_time_wait->tcp_free_list_cnt--; 5118 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5119 tcp->tcp_time_wait_next = NULL; 5120 connp = tcp->tcp_connp; 5121 connp->conn_flags |= IPCL_REUSED; 5122 5123 ASSERT(tcp->tcp_tcps == NULL); 5124 ASSERT(connp->conn_netstack == NULL); 5125 ASSERT(tcp->tcp_rsrv_mp != NULL); 5126 ns = tcps->tcps_netstack; 5127 netstack_hold(ns); 5128 connp->conn_netstack = ns; 5129 tcp->tcp_tcps = tcps; 5130 TCPS_REFHOLD(tcps); 5131 ipcl_globalhash_insert(connp); 5132 return ((void *)connp); 5133 } 5134 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5135 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5136 tcps->tcps_netstack)) == NULL) 5137 return (NULL); 5138 tcp = connp->conn_tcp; 5139 /* 5140 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed 5141 * until this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5142 */ 5143 if ((tcp->tcp_rsrv_mp = allocb(0, BPRI_HI)) == NULL) { 5144 ipcl_conn_destroy(connp); 5145 return (NULL); 5146 } 5147 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5148 tcp->tcp_tcps = tcps; 5149 TCPS_REFHOLD(tcps); 5150 5151 return ((void *)connp); 5152 } 5153 5154 /* 5155 * Update the cached label for the given tcp_t. This should be called once per 5156 * connection, and before any packets are sent or tcp_process_options is 5157 * invoked. Returns B_FALSE if the correct label could not be constructed. 5158 */ 5159 static boolean_t 5160 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5161 { 5162 conn_t *connp = tcp->tcp_connp; 5163 5164 if (tcp->tcp_ipversion == IPV4_VERSION) { 5165 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5166 int added; 5167 5168 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5169 connp->conn_mac_exempt, 5170 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5171 return (B_FALSE); 5172 5173 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5174 if (added == -1) 5175 return (B_FALSE); 5176 tcp->tcp_hdr_len += added; 5177 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5178 tcp->tcp_ip_hdr_len += added; 5179 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5180 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5181 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5182 tcp->tcp_hdr_len); 5183 if (added == -1) 5184 return (B_FALSE); 5185 tcp->tcp_hdr_len += added; 5186 tcp->tcp_tcph = (tcph_t *) 5187 ((uchar_t *)tcp->tcp_tcph + added); 5188 tcp->tcp_ip_hdr_len += added; 5189 } 5190 } else { 5191 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5192 5193 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5194 connp->conn_mac_exempt, 5195 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5196 return (B_FALSE); 5197 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5198 &tcp->tcp_label_len, optbuf) != 0) 5199 return (B_FALSE); 5200 if (tcp_build_hdrs(tcp) != 0) 5201 return (B_FALSE); 5202 } 5203 5204 connp->conn_ulp_labeled = 1; 5205 5206 return (B_TRUE); 5207 } 5208 5209 /* BEGIN CSTYLED */ 5210 /* 5211 * 5212 * The sockfs ACCEPT path: 5213 * ======================= 5214 * 5215 * The eager is now established in its own perimeter as soon as SYN is 5216 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5217 * completes the accept processing on the acceptor STREAM. The sending 5218 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5219 * listener but a TLI/XTI listener completes the accept processing 5220 * on the listener perimeter. 5221 * 5222 * Common control flow for 3 way handshake: 5223 * ---------------------------------------- 5224 * 5225 * incoming SYN (listener perimeter) -> tcp_rput_data() 5226 * -> tcp_conn_request() 5227 * 5228 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5229 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5230 * 5231 * Sockfs ACCEPT Path: 5232 * ------------------- 5233 * 5234 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5235 * as STREAM entry point) 5236 * 5237 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5238 * 5239 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5240 * association (we are not behind eager's squeue but sockfs is protecting us 5241 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5242 * is changed to point at tcp_wput(). 5243 * 5244 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5245 * listener (done on listener's perimeter). 5246 * 5247 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5248 * accept. 5249 * 5250 * TLI/XTI client ACCEPT path: 5251 * --------------------------- 5252 * 5253 * soaccept() sends T_CONN_RES on the listener STREAM. 5254 * 5255 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5256 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5257 * 5258 * Locks: 5259 * ====== 5260 * 5261 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5262 * and listeners->tcp_eager_next_q. 5263 * 5264 * Referencing: 5265 * ============ 5266 * 5267 * 1) We start out in tcp_conn_request by eager placing a ref on 5268 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5269 * 5270 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5271 * doing so we place a ref on the eager. This ref is finally dropped at the 5272 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5273 * reference is dropped by the squeue framework. 5274 * 5275 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5276 * 5277 * The reference must be released by the same entity that added the reference 5278 * In the above scheme, the eager is the entity that adds and releases the 5279 * references. Note that tcp_accept_finish executes in the squeue of the eager 5280 * (albeit after it is attached to the acceptor stream). Though 1. executes 5281 * in the listener's squeue, the eager is nascent at this point and the 5282 * reference can be considered to have been added on behalf of the eager. 5283 * 5284 * Eager getting a Reset or listener closing: 5285 * ========================================== 5286 * 5287 * Once the listener and eager are linked, the listener never does the unlink. 5288 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5289 * a message on all eager perimeter. The eager then does the unlink, clears 5290 * any pointers to the listener's queue and drops the reference to the 5291 * listener. The listener waits in tcp_close outside the squeue until its 5292 * refcount has dropped to 1. This ensures that the listener has waited for 5293 * all eagers to clear their association with the listener. 5294 * 5295 * Similarly, if eager decides to go away, it can unlink itself and close. 5296 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5297 * the reference to eager is still valid because of the extra ref we put 5298 * in tcp_send_conn_ind. 5299 * 5300 * Listener can always locate the eager under the protection 5301 * of the listener->tcp_eager_lock, and then do a refhold 5302 * on the eager during the accept processing. 5303 * 5304 * The acceptor stream accesses the eager in the accept processing 5305 * based on the ref placed on eager before sending T_conn_ind. 5306 * The only entity that can negate this refhold is a listener close 5307 * which is mutually exclusive with an active acceptor stream. 5308 * 5309 * Eager's reference on the listener 5310 * =================================== 5311 * 5312 * If the accept happens (even on a closed eager) the eager drops its 5313 * reference on the listener at the start of tcp_accept_finish. If the 5314 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5315 * the reference is dropped in tcp_closei_local. If the listener closes, 5316 * the reference is dropped in tcp_eager_kill. In all cases the reference 5317 * is dropped while executing in the eager's context (squeue). 5318 */ 5319 /* END CSTYLED */ 5320 5321 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5322 5323 /* 5324 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5325 * tcp_rput_data will not see any SYN packets. 5326 */ 5327 /* ARGSUSED */ 5328 void 5329 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5330 { 5331 tcph_t *tcph; 5332 uint32_t seg_seq; 5333 tcp_t *eager; 5334 uint_t ipvers; 5335 ipha_t *ipha; 5336 ip6_t *ip6h; 5337 int err; 5338 conn_t *econnp = NULL; 5339 squeue_t *new_sqp; 5340 mblk_t *mp1; 5341 uint_t ip_hdr_len; 5342 conn_t *connp = (conn_t *)arg; 5343 tcp_t *tcp = connp->conn_tcp; 5344 cred_t *credp; 5345 tcp_stack_t *tcps = tcp->tcp_tcps; 5346 ip_stack_t *ipst; 5347 5348 if (tcp->tcp_state != TCPS_LISTEN) 5349 goto error2; 5350 5351 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5352 5353 mutex_enter(&tcp->tcp_eager_lock); 5354 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5355 mutex_exit(&tcp->tcp_eager_lock); 5356 TCP_STAT(tcps, tcp_listendrop); 5357 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5358 if (tcp->tcp_debug) { 5359 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5360 "tcp_conn_request: listen backlog (max=%d) " 5361 "overflow (%d pending) on %s", 5362 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5363 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5364 } 5365 goto error2; 5366 } 5367 5368 if (tcp->tcp_conn_req_cnt_q0 >= 5369 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5370 /* 5371 * Q0 is full. Drop a pending half-open req from the queue 5372 * to make room for the new SYN req. Also mark the time we 5373 * drop a SYN. 5374 * 5375 * A more aggressive defense against SYN attack will 5376 * be to set the "tcp_syn_defense" flag now. 5377 */ 5378 TCP_STAT(tcps, tcp_listendropq0); 5379 tcp->tcp_last_rcv_lbolt = lbolt64; 5380 if (!tcp_drop_q0(tcp)) { 5381 mutex_exit(&tcp->tcp_eager_lock); 5382 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5383 if (tcp->tcp_debug) { 5384 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5385 "tcp_conn_request: listen half-open queue " 5386 "(max=%d) full (%d pending) on %s", 5387 tcps->tcps_conn_req_max_q0, 5388 tcp->tcp_conn_req_cnt_q0, 5389 tcp_display(tcp, NULL, 5390 DISP_PORT_ONLY)); 5391 } 5392 goto error2; 5393 } 5394 } 5395 mutex_exit(&tcp->tcp_eager_lock); 5396 5397 /* 5398 * IP adds STRUIO_EAGER and ensures that the received packet is 5399 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5400 * link local address. If IPSec is enabled, db_struioflag has 5401 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5402 * otherwise an error case if neither of them is set. 5403 */ 5404 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5405 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5406 DB_CKSUMSTART(mp) = 0; 5407 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5408 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5409 if (econnp == NULL) 5410 goto error2; 5411 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5412 econnp->conn_sqp = new_sqp; 5413 econnp->conn_initial_sqp = new_sqp; 5414 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5415 /* 5416 * mp is updated in tcp_get_ipsec_conn(). 5417 */ 5418 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5419 if (econnp == NULL) { 5420 /* 5421 * mp freed by tcp_get_ipsec_conn. 5422 */ 5423 return; 5424 } 5425 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5426 } else { 5427 goto error2; 5428 } 5429 5430 ASSERT(DB_TYPE(mp) == M_DATA); 5431 5432 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5433 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5434 ASSERT(OK_32PTR(mp->b_rptr)); 5435 if (ipvers == IPV4_VERSION) { 5436 ipha = (ipha_t *)mp->b_rptr; 5437 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5438 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5439 } else { 5440 ip6h = (ip6_t *)mp->b_rptr; 5441 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5442 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5443 } 5444 5445 if (tcp->tcp_family == AF_INET) { 5446 ASSERT(ipvers == IPV4_VERSION); 5447 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5448 } else { 5449 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5450 } 5451 5452 if (err) 5453 goto error3; 5454 5455 eager = econnp->conn_tcp; 5456 5457 /* 5458 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that at close 5459 * time, we will always have that to send up. Otherwise, we need to do 5460 * special handling in case the allocation fails at that time. 5461 */ 5462 ASSERT(eager->tcp_ordrel_mp == NULL); 5463 if (!IPCL_IS_NONSTR(econnp) && 5464 (eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5465 goto error3; 5466 5467 /* Inherit various TCP parameters from the listener */ 5468 eager->tcp_naglim = tcp->tcp_naglim; 5469 eager->tcp_first_timer_threshold = 5470 tcp->tcp_first_timer_threshold; 5471 eager->tcp_second_timer_threshold = 5472 tcp->tcp_second_timer_threshold; 5473 5474 eager->tcp_first_ctimer_threshold = 5475 tcp->tcp_first_ctimer_threshold; 5476 eager->tcp_second_ctimer_threshold = 5477 tcp->tcp_second_ctimer_threshold; 5478 5479 /* 5480 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5481 * If it does not, the eager's receive window will be set to the 5482 * listener's receive window later in this function. 5483 */ 5484 eager->tcp_rwnd = 0; 5485 5486 /* 5487 * Inherit listener's tcp_init_cwnd. Need to do this before 5488 * calling tcp_process_options() where tcp_mss_set() is called 5489 * to set the initial cwnd. 5490 */ 5491 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5492 5493 /* 5494 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5495 * zone id before the accept is completed in tcp_wput_accept(). 5496 */ 5497 econnp->conn_zoneid = connp->conn_zoneid; 5498 econnp->conn_allzones = connp->conn_allzones; 5499 5500 /* Copy nexthop information from listener to eager */ 5501 if (connp->conn_nexthop_set) { 5502 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5503 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5504 } 5505 5506 /* 5507 * TSOL: tsol_input_proc() needs the eager's cred before the 5508 * eager is accepted 5509 */ 5510 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5511 crhold(credp); 5512 5513 /* 5514 * If the caller has the process-wide flag set, then default to MAC 5515 * exempt mode. This allows read-down to unlabeled hosts. 5516 */ 5517 if (getpflags(NET_MAC_AWARE, credp) != 0) 5518 econnp->conn_mac_exempt = B_TRUE; 5519 5520 if (is_system_labeled()) { 5521 cred_t *cr; 5522 5523 if (connp->conn_mlp_type != mlptSingle) { 5524 cr = econnp->conn_peercred = DB_CRED(mp); 5525 if (cr != NULL) 5526 crhold(cr); 5527 else 5528 cr = econnp->conn_cred; 5529 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5530 econnp, cred_t *, cr) 5531 } else { 5532 cr = econnp->conn_cred; 5533 DTRACE_PROBE2(syn_accept, conn_t *, 5534 econnp, cred_t *, cr) 5535 } 5536 5537 if (!tcp_update_label(eager, cr)) { 5538 DTRACE_PROBE3( 5539 tx__ip__log__error__connrequest__tcp, 5540 char *, "eager connp(1) label on SYN mp(2) failed", 5541 conn_t *, econnp, mblk_t *, mp); 5542 goto error3; 5543 } 5544 } 5545 5546 eager->tcp_hard_binding = B_TRUE; 5547 5548 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5549 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5550 5551 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5552 if (err != 0) { 5553 tcp_bind_hash_remove(eager); 5554 goto error3; 5555 } 5556 5557 /* 5558 * No need to check for multicast destination since ip will only pass 5559 * up multicasts to those that have expressed interest 5560 * TODO: what about rejecting broadcasts? 5561 * Also check that source is not a multicast or broadcast address. 5562 */ 5563 eager->tcp_state = TCPS_SYN_RCVD; 5564 5565 5566 /* 5567 * There should be no ire in the mp as we are being called after 5568 * receiving the SYN. 5569 */ 5570 ASSERT(tcp_ire_mp(&mp) == NULL); 5571 5572 /* 5573 * Adapt our mss, ttl, ... according to information provided in IRE. 5574 */ 5575 5576 if (tcp_adapt_ire(eager, NULL) == 0) { 5577 /* Undo the bind_hash_insert */ 5578 tcp_bind_hash_remove(eager); 5579 goto error3; 5580 } 5581 5582 /* Process all TCP options. */ 5583 tcp_process_options(eager, tcph); 5584 5585 /* Is the other end ECN capable? */ 5586 if (tcps->tcps_ecn_permitted >= 1 && 5587 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5588 eager->tcp_ecn_ok = B_TRUE; 5589 } 5590 5591 /* 5592 * listener->tcp_rq->q_hiwat should be the default window size or a 5593 * window size changed via SO_RCVBUF option. First round up the 5594 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5595 * scale option value if needed. Call tcp_rwnd_set() to finish the 5596 * setting. 5597 * 5598 * Note if there is a rpipe metric associated with the remote host, 5599 * we should not inherit receive window size from listener. 5600 */ 5601 eager->tcp_rwnd = MSS_ROUNDUP( 5602 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5603 eager->tcp_rwnd), eager->tcp_mss); 5604 if (eager->tcp_snd_ws_ok) 5605 tcp_set_ws_value(eager); 5606 /* 5607 * Note that this is the only place tcp_rwnd_set() is called for 5608 * accepting a connection. We need to call it here instead of 5609 * after the 3-way handshake because we need to tell the other 5610 * side our rwnd in the SYN-ACK segment. 5611 */ 5612 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5613 5614 /* 5615 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5616 * via soaccept()->soinheritoptions() which essentially applies 5617 * all the listener options to the new STREAM. The options that we 5618 * need to take care of are: 5619 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5620 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5621 * SO_SNDBUF, SO_RCVBUF. 5622 * 5623 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5624 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5625 * tcp_maxpsz_set() gets called later from 5626 * tcp_accept_finish(), the option takes effect. 5627 * 5628 */ 5629 /* Set the TCP options */ 5630 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5631 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5632 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5633 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5634 eager->tcp_oobinline = tcp->tcp_oobinline; 5635 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5636 eager->tcp_broadcast = tcp->tcp_broadcast; 5637 eager->tcp_useloopback = tcp->tcp_useloopback; 5638 eager->tcp_dontroute = tcp->tcp_dontroute; 5639 eager->tcp_debug = tcp->tcp_debug; 5640 eager->tcp_linger = tcp->tcp_linger; 5641 eager->tcp_lingertime = tcp->tcp_lingertime; 5642 if (tcp->tcp_ka_enabled) 5643 eager->tcp_ka_enabled = 1; 5644 5645 /* Set the IP options */ 5646 econnp->conn_broadcast = connp->conn_broadcast; 5647 econnp->conn_loopback = connp->conn_loopback; 5648 econnp->conn_dontroute = connp->conn_dontroute; 5649 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5650 5651 /* Put a ref on the listener for the eager. */ 5652 CONN_INC_REF(connp); 5653 mutex_enter(&tcp->tcp_eager_lock); 5654 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5655 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5656 tcp->tcp_eager_next_q0 = eager; 5657 eager->tcp_eager_prev_q0 = tcp; 5658 5659 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5660 eager->tcp_listener = tcp; 5661 eager->tcp_saved_listener = tcp; 5662 5663 /* 5664 * Tag this detached tcp vector for later retrieval 5665 * by our listener client in tcp_accept(). 5666 */ 5667 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5668 tcp->tcp_conn_req_cnt_q0++; 5669 if (++tcp->tcp_conn_req_seqnum == -1) { 5670 /* 5671 * -1 is "special" and defined in TPI as something 5672 * that should never be used in T_CONN_IND 5673 */ 5674 ++tcp->tcp_conn_req_seqnum; 5675 } 5676 mutex_exit(&tcp->tcp_eager_lock); 5677 5678 if (tcp->tcp_syn_defense) { 5679 /* Don't drop the SYN that comes from a good IP source */ 5680 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5681 if (addr_cache != NULL && eager->tcp_remote == 5682 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5683 eager->tcp_dontdrop = B_TRUE; 5684 } 5685 } 5686 5687 /* 5688 * We need to insert the eager in its own perimeter but as soon 5689 * as we do that, we expose the eager to the classifier and 5690 * should not touch any field outside the eager's perimeter. 5691 * So do all the work necessary before inserting the eager 5692 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5693 * will succeed but undo everything if it fails. 5694 */ 5695 seg_seq = ABE32_TO_U32(tcph->th_seq); 5696 eager->tcp_irs = seg_seq; 5697 eager->tcp_rack = seg_seq; 5698 eager->tcp_rnxt = seg_seq + 1; 5699 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5700 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5701 eager->tcp_state = TCPS_SYN_RCVD; 5702 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5703 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5704 if (mp1 == NULL) { 5705 /* 5706 * Increment the ref count as we are going to 5707 * enqueueing an mp in squeue 5708 */ 5709 CONN_INC_REF(econnp); 5710 goto error; 5711 } 5712 5713 DB_CPID(mp1) = tcp->tcp_cpid; 5714 mblk_setcred(mp1, CONN_CRED(eager->tcp_connp)); 5715 eager->tcp_cpid = tcp->tcp_cpid; 5716 eager->tcp_open_time = lbolt64; 5717 5718 /* 5719 * We need to start the rto timer. In normal case, we start 5720 * the timer after sending the packet on the wire (or at 5721 * least believing that packet was sent by waiting for 5722 * CALL_IP_WPUT() to return). Since this is the first packet 5723 * being sent on the wire for the eager, our initial tcp_rto 5724 * is at least tcp_rexmit_interval_min which is a fairly 5725 * large value to allow the algorithm to adjust slowly to large 5726 * fluctuations of RTT during first few transmissions. 5727 * 5728 * Starting the timer first and then sending the packet in this 5729 * case shouldn't make much difference since tcp_rexmit_interval_min 5730 * is of the order of several 100ms and starting the timer 5731 * first and then sending the packet will result in difference 5732 * of few micro seconds. 5733 * 5734 * Without this optimization, we are forced to hold the fanout 5735 * lock across the ipcl_bind_insert() and sending the packet 5736 * so that we don't race against an incoming packet (maybe RST) 5737 * for this eager. 5738 * 5739 * It is necessary to acquire an extra reference on the eager 5740 * at this point and hold it until after tcp_send_data() to 5741 * ensure against an eager close race. 5742 */ 5743 5744 CONN_INC_REF(eager->tcp_connp); 5745 5746 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5747 5748 /* 5749 * Insert the eager in its own perimeter now. We are ready to deal 5750 * with any packets on eager. 5751 */ 5752 if (eager->tcp_ipversion == IPV4_VERSION) { 5753 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5754 goto error; 5755 } 5756 } else { 5757 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5758 goto error; 5759 } 5760 } 5761 5762 /* mark conn as fully-bound */ 5763 econnp->conn_fully_bound = B_TRUE; 5764 5765 /* Send the SYN-ACK */ 5766 tcp_send_data(eager, eager->tcp_wq, mp1); 5767 CONN_DEC_REF(eager->tcp_connp); 5768 freemsg(mp); 5769 5770 return; 5771 error: 5772 freemsg(mp1); 5773 eager->tcp_closemp_used = B_TRUE; 5774 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5775 mp1 = &eager->tcp_closemp; 5776 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5777 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5778 5779 /* 5780 * If a connection already exists, send the mp to that connections so 5781 * that it can be appropriately dealt with. 5782 */ 5783 ipst = tcps->tcps_netstack->netstack_ip; 5784 5785 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5786 if (!IPCL_IS_CONNECTED(econnp)) { 5787 /* 5788 * Something bad happened. ipcl_conn_insert() 5789 * failed because a connection already existed 5790 * in connected hash but we can't find it 5791 * anymore (someone blew it away). Just 5792 * free this message and hopefully remote 5793 * will retransmit at which time the SYN can be 5794 * treated as a new connection or dealth with 5795 * a TH_RST if a connection already exists. 5796 */ 5797 CONN_DEC_REF(econnp); 5798 freemsg(mp); 5799 } else { 5800 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5801 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5802 } 5803 } else { 5804 /* Nobody wants this packet */ 5805 freemsg(mp); 5806 } 5807 return; 5808 error3: 5809 CONN_DEC_REF(econnp); 5810 error2: 5811 freemsg(mp); 5812 } 5813 5814 /* 5815 * In an ideal case of vertical partition in NUMA architecture, its 5816 * beneficial to have the listener and all the incoming connections 5817 * tied to the same squeue. The other constraint is that incoming 5818 * connections should be tied to the squeue attached to interrupted 5819 * CPU for obvious locality reason so this leaves the listener to 5820 * be tied to the same squeue. Our only problem is that when listener 5821 * is binding, the CPU that will get interrupted by the NIC whose 5822 * IP address the listener is binding to is not even known. So 5823 * the code below allows us to change that binding at the time the 5824 * CPU is interrupted by virtue of incoming connection's squeue. 5825 * 5826 * This is usefull only in case of a listener bound to a specific IP 5827 * address. For other kind of listeners, they get bound the 5828 * very first time and there is no attempt to rebind them. 5829 */ 5830 void 5831 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5832 { 5833 conn_t *connp = (conn_t *)arg; 5834 squeue_t *sqp = (squeue_t *)arg2; 5835 squeue_t *new_sqp; 5836 uint32_t conn_flags; 5837 5838 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5839 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5840 } else { 5841 goto done; 5842 } 5843 5844 if (connp->conn_fanout == NULL) 5845 goto done; 5846 5847 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5848 mutex_enter(&connp->conn_fanout->connf_lock); 5849 mutex_enter(&connp->conn_lock); 5850 /* 5851 * No one from read or write side can access us now 5852 * except for already queued packets on this squeue. 5853 * But since we haven't changed the squeue yet, they 5854 * can't execute. If they are processed after we have 5855 * changed the squeue, they are sent back to the 5856 * correct squeue down below. 5857 * But a listner close can race with processing of 5858 * incoming SYN. If incoming SYN processing changes 5859 * the squeue then the listener close which is waiting 5860 * to enter the squeue would operate on the wrong 5861 * squeue. Hence we don't change the squeue here unless 5862 * the refcount is exactly the minimum refcount. The 5863 * minimum refcount of 4 is counted as - 1 each for 5864 * TCP and IP, 1 for being in the classifier hash, and 5865 * 1 for the mblk being processed. 5866 */ 5867 5868 if (connp->conn_ref != 4 || 5869 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5870 mutex_exit(&connp->conn_lock); 5871 mutex_exit(&connp->conn_fanout->connf_lock); 5872 goto done; 5873 } 5874 if (connp->conn_sqp != new_sqp) { 5875 while (connp->conn_sqp != new_sqp) 5876 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5877 } 5878 5879 do { 5880 conn_flags = connp->conn_flags; 5881 conn_flags |= IPCL_FULLY_BOUND; 5882 (void) cas32(&connp->conn_flags, connp->conn_flags, 5883 conn_flags); 5884 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5885 5886 mutex_exit(&connp->conn_fanout->connf_lock); 5887 mutex_exit(&connp->conn_lock); 5888 } 5889 5890 done: 5891 if (connp->conn_sqp != sqp) { 5892 CONN_INC_REF(connp); 5893 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5894 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5895 } else { 5896 tcp_conn_request(connp, mp, sqp); 5897 } 5898 } 5899 5900 /* 5901 * Successful connect request processing begins when our client passes 5902 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5903 * our T_OK_ACK reply message upstream. The control flow looks like this: 5904 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5905 * upstream <- tcp_rput() <- IP 5906 * After various error checks are completed, tcp_tpi_connect() lays 5907 * the target address and port into the composite header template, 5908 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5909 * request followed by an IRE request, and passes the three mblk message 5910 * down to IP looking like this: 5911 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5912 * Processing continues in tcp_rput() when we receive the following message: 5913 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5914 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5915 * to fire off the connection request, and then passes the T_OK_ACK mblk 5916 * upstream that we filled in below. There are, of course, numerous 5917 * error conditions along the way which truncate the processing described 5918 * above. 5919 */ 5920 static void 5921 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5922 { 5923 sin_t *sin; 5924 queue_t *q = tcp->tcp_wq; 5925 struct T_conn_req *tcr; 5926 struct sockaddr *sa; 5927 socklen_t len; 5928 int error; 5929 5930 tcr = (struct T_conn_req *)mp->b_rptr; 5931 5932 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5933 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5934 tcp_err_ack(tcp, mp, TPROTO, 0); 5935 return; 5936 } 5937 5938 /* 5939 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5940 * will always have that to send up. Otherwise, we need to do 5941 * special handling in case the allocation fails at that time. 5942 * If the end point is TPI, the tcp_t can be reused and the 5943 * tcp_ordrel_mp may be allocated already. 5944 */ 5945 if (tcp->tcp_ordrel_mp == NULL) { 5946 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5947 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5948 return; 5949 } 5950 } 5951 5952 /* 5953 * Determine packet type based on type of address passed in 5954 * the request should contain an IPv4 or IPv6 address. 5955 * Make sure that address family matches the type of 5956 * family of the the address passed down 5957 */ 5958 switch (tcr->DEST_length) { 5959 default: 5960 tcp_err_ack(tcp, mp, TBADADDR, 0); 5961 return; 5962 5963 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5964 /* 5965 * XXX: The check for valid DEST_length was not there 5966 * in earlier releases and some buggy 5967 * TLI apps (e.g Sybase) got away with not feeding 5968 * in sin_zero part of address. 5969 * We allow that bug to keep those buggy apps humming. 5970 * Test suites require the check on DEST_length. 5971 * We construct a new mblk with valid DEST_length 5972 * free the original so the rest of the code does 5973 * not have to keep track of this special shorter 5974 * length address case. 5975 */ 5976 mblk_t *nmp; 5977 struct T_conn_req *ntcr; 5978 sin_t *nsin; 5979 5980 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5981 tcr->OPT_length, BPRI_HI); 5982 if (nmp == NULL) { 5983 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5984 return; 5985 } 5986 ntcr = (struct T_conn_req *)nmp->b_rptr; 5987 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5988 ntcr->PRIM_type = T_CONN_REQ; 5989 ntcr->DEST_length = sizeof (sin_t); 5990 ntcr->DEST_offset = sizeof (struct T_conn_req); 5991 5992 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5993 *nsin = sin_null; 5994 /* Get pointer to shorter address to copy from original mp */ 5995 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5996 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5997 if (sin == NULL || !OK_32PTR((char *)sin)) { 5998 freemsg(nmp); 5999 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6000 return; 6001 } 6002 nsin->sin_family = sin->sin_family; 6003 nsin->sin_port = sin->sin_port; 6004 nsin->sin_addr = sin->sin_addr; 6005 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6006 nmp->b_wptr = (uchar_t *)&nsin[1]; 6007 if (tcr->OPT_length != 0) { 6008 ntcr->OPT_length = tcr->OPT_length; 6009 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6010 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6011 (uchar_t *)ntcr + ntcr->OPT_offset, 6012 tcr->OPT_length); 6013 nmp->b_wptr += tcr->OPT_length; 6014 } 6015 freemsg(mp); /* original mp freed */ 6016 mp = nmp; /* re-initialize original variables */ 6017 tcr = ntcr; 6018 } 6019 /* FALLTHRU */ 6020 6021 case sizeof (sin_t): 6022 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6023 sizeof (sin_t)); 6024 len = sizeof (sin_t); 6025 break; 6026 6027 case sizeof (sin6_t): 6028 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6029 sizeof (sin6_t)); 6030 len = sizeof (sin6_t); 6031 break; 6032 } 6033 6034 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6035 if (error != 0) { 6036 tcp_err_ack(tcp, mp, TSYSERR, error); 6037 return; 6038 } 6039 6040 /* 6041 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6042 * should key on their sequence number and cut them loose. 6043 */ 6044 6045 /* 6046 * If options passed in, feed it for verification and handling 6047 */ 6048 if (tcr->OPT_length != 0) { 6049 mblk_t *ok_mp; 6050 mblk_t *discon_mp; 6051 mblk_t *conn_opts_mp; 6052 int t_error, sys_error, do_disconnect; 6053 6054 conn_opts_mp = NULL; 6055 6056 if (tcp_conprim_opt_process(tcp, mp, 6057 &do_disconnect, &t_error, &sys_error) < 0) { 6058 if (do_disconnect) { 6059 ASSERT(t_error == 0 && sys_error == 0); 6060 discon_mp = mi_tpi_discon_ind(NULL, 6061 ECONNREFUSED, 0); 6062 if (!discon_mp) { 6063 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6064 TSYSERR, ENOMEM); 6065 return; 6066 } 6067 ok_mp = mi_tpi_ok_ack_alloc(mp); 6068 if (!ok_mp) { 6069 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6070 TSYSERR, ENOMEM); 6071 return; 6072 } 6073 qreply(q, ok_mp); 6074 qreply(q, discon_mp); /* no flush! */ 6075 } else { 6076 ASSERT(t_error != 0); 6077 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6078 sys_error); 6079 } 6080 return; 6081 } 6082 /* 6083 * Success in setting options, the mp option buffer represented 6084 * by OPT_length/offset has been potentially modified and 6085 * contains results of option processing. We copy it in 6086 * another mp to save it for potentially influencing returning 6087 * it in T_CONN_CONN. 6088 */ 6089 if (tcr->OPT_length != 0) { /* there are resulting options */ 6090 conn_opts_mp = copyb(mp); 6091 if (!conn_opts_mp) { 6092 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6093 TSYSERR, ENOMEM); 6094 return; 6095 } 6096 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6097 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6098 /* 6099 * Note: 6100 * These resulting option negotiation can include any 6101 * end-to-end negotiation options but there no such 6102 * thing (yet?) in our TCP/IP. 6103 */ 6104 } 6105 } 6106 6107 /* call the non-TPI version */ 6108 error = tcp_do_connect(tcp->tcp_connp, sa, len, DB_CRED(mp), 6109 DB_CPID(mp)); 6110 if (error < 0) { 6111 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6112 } else if (error > 0) { 6113 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6114 } else { 6115 mp = mi_tpi_ok_ack_alloc(mp); 6116 } 6117 6118 /* 6119 * Note: Code below is the "failure" case 6120 */ 6121 /* return error ack and blow away saved option results if any */ 6122 connect_failed: 6123 if (mp != NULL) 6124 putnext(tcp->tcp_rq, mp); 6125 else { 6126 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6127 TSYSERR, ENOMEM); 6128 } 6129 } 6130 6131 /* 6132 * Handle connect to IPv4 destinations, including connections for AF_INET6 6133 * sockets connecting to IPv4 mapped IPv6 destinations. 6134 */ 6135 static int 6136 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6137 uint_t srcid, cred_t *cr, pid_t pid) 6138 { 6139 tcph_t *tcph; 6140 mblk_t *mp; 6141 ipaddr_t dstaddr = *dstaddrp; 6142 int32_t oldstate; 6143 uint16_t lport; 6144 int error = 0; 6145 tcp_stack_t *tcps = tcp->tcp_tcps; 6146 6147 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6148 6149 /* Check for attempt to connect to INADDR_ANY */ 6150 if (dstaddr == INADDR_ANY) { 6151 /* 6152 * SunOS 4.x and 4.3 BSD allow an application 6153 * to connect a TCP socket to INADDR_ANY. 6154 * When they do this, the kernel picks the 6155 * address of one interface and uses it 6156 * instead. The kernel usually ends up 6157 * picking the address of the loopback 6158 * interface. This is an undocumented feature. 6159 * However, we provide the same thing here 6160 * in order to have source and binary 6161 * compatibility with SunOS 4.x. 6162 * Update the T_CONN_REQ (sin/sin6) since it is used to 6163 * generate the T_CONN_CON. 6164 */ 6165 dstaddr = htonl(INADDR_LOOPBACK); 6166 *dstaddrp = dstaddr; 6167 } 6168 6169 /* Handle __sin6_src_id if socket not bound to an IP address */ 6170 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6171 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6172 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6173 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6174 tcp->tcp_ipha->ipha_src); 6175 } 6176 6177 /* 6178 * Don't let an endpoint connect to itself. Note that 6179 * the test here does not catch the case where the 6180 * source IP addr was left unspecified by the user. In 6181 * this case, the source addr is set in tcp_adapt_ire() 6182 * using the reply to the T_BIND message that we send 6183 * down to IP here and the check is repeated in tcp_rput_other. 6184 */ 6185 if (dstaddr == tcp->tcp_ipha->ipha_src && 6186 dstport == tcp->tcp_lport) { 6187 error = -TBADADDR; 6188 goto failed; 6189 } 6190 6191 tcp->tcp_ipha->ipha_dst = dstaddr; 6192 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6193 6194 /* 6195 * Massage a source route if any putting the first hop 6196 * in iph_dst. Compute a starting value for the checksum which 6197 * takes into account that the original iph_dst should be 6198 * included in the checksum but that ip will include the 6199 * first hop in the source route in the tcp checksum. 6200 */ 6201 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6202 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6203 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6204 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6205 if ((int)tcp->tcp_sum < 0) 6206 tcp->tcp_sum--; 6207 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6208 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6209 (tcp->tcp_sum >> 16)); 6210 tcph = tcp->tcp_tcph; 6211 *(uint16_t *)tcph->th_fport = dstport; 6212 tcp->tcp_fport = dstport; 6213 6214 oldstate = tcp->tcp_state; 6215 /* 6216 * At this point the remote destination address and remote port fields 6217 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6218 * have to see which state tcp was in so we can take apropriate action. 6219 */ 6220 if (oldstate == TCPS_IDLE) { 6221 /* 6222 * We support a quick connect capability here, allowing 6223 * clients to transition directly from IDLE to SYN_SENT 6224 * tcp_bindi will pick an unused port, insert the connection 6225 * in the bind hash and transition to BOUND state. 6226 */ 6227 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6228 tcp, B_TRUE); 6229 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6230 B_FALSE, B_FALSE); 6231 if (lport == 0) { 6232 error = -TNOADDR; 6233 goto failed; 6234 } 6235 } 6236 tcp->tcp_state = TCPS_SYN_SENT; 6237 6238 mp = allocb(sizeof (ire_t), BPRI_HI); 6239 if (mp == NULL) { 6240 tcp->tcp_state = oldstate; 6241 error = ENOMEM; 6242 goto failed; 6243 } 6244 6245 mp->b_wptr += sizeof (ire_t); 6246 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6247 tcp->tcp_hard_binding = 1; 6248 6249 /* 6250 * We need to make sure that the conn_recv is set to a non-null 6251 * value before we insert the conn_t into the classifier table. 6252 * This is to avoid a race with an incoming packet which does 6253 * an ipcl_classify(). 6254 */ 6255 tcp->tcp_connp->conn_recv = tcp_input; 6256 6257 if (tcp->tcp_family == AF_INET) { 6258 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6259 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6260 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE); 6261 } else { 6262 in6_addr_t v6src; 6263 if (tcp->tcp_ipversion == IPV4_VERSION) { 6264 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6265 } else { 6266 v6src = tcp->tcp_ip6h->ip6_src; 6267 } 6268 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6269 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6270 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE); 6271 } 6272 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6273 tcp->tcp_active_open = 1; 6274 6275 6276 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6277 failed: 6278 /* return error ack and blow away saved option results if any */ 6279 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6280 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6281 return (error); 6282 } 6283 6284 /* 6285 * Handle connect to IPv6 destinations. 6286 */ 6287 static int 6288 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6289 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6290 { 6291 tcph_t *tcph; 6292 mblk_t *mp; 6293 ip6_rthdr_t *rth; 6294 int32_t oldstate; 6295 uint16_t lport; 6296 tcp_stack_t *tcps = tcp->tcp_tcps; 6297 int error = 0; 6298 conn_t *connp = tcp->tcp_connp; 6299 6300 ASSERT(tcp->tcp_family == AF_INET6); 6301 6302 /* 6303 * If we're here, it means that the destination address is a native 6304 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6305 * reason why it might not be IPv6 is if the socket was bound to an 6306 * IPv4-mapped IPv6 address. 6307 */ 6308 if (tcp->tcp_ipversion != IPV6_VERSION) { 6309 return (-TBADADDR); 6310 } 6311 6312 /* 6313 * Interpret a zero destination to mean loopback. 6314 * Update the T_CONN_REQ (sin/sin6) since it is used to 6315 * generate the T_CONN_CON. 6316 */ 6317 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6318 *dstaddrp = ipv6_loopback; 6319 } 6320 6321 /* Handle __sin6_src_id if socket not bound to an IP address */ 6322 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6323 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6324 connp->conn_zoneid, tcps->tcps_netstack); 6325 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6326 } 6327 6328 /* 6329 * Take care of the scope_id now and add ip6i_t 6330 * if ip6i_t is not already allocated through TCP 6331 * sticky options. At this point tcp_ip6h does not 6332 * have dst info, thus use dstaddrp. 6333 */ 6334 if (scope_id != 0 && 6335 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6336 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6337 ip6i_t *ip6i; 6338 6339 ipp->ipp_ifindex = scope_id; 6340 ip6i = (ip6i_t *)tcp->tcp_iphc; 6341 6342 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6343 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6344 /* Already allocated */ 6345 ip6i->ip6i_flags |= IP6I_IFINDEX; 6346 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6347 ipp->ipp_fields |= IPPF_SCOPE_ID; 6348 } else { 6349 int reterr; 6350 6351 ipp->ipp_fields |= IPPF_SCOPE_ID; 6352 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6353 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6354 reterr = tcp_build_hdrs(tcp); 6355 if (reterr != 0) 6356 goto failed; 6357 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6358 } 6359 } 6360 6361 /* 6362 * Don't let an endpoint connect to itself. Note that 6363 * the test here does not catch the case where the 6364 * source IP addr was left unspecified by the user. In 6365 * this case, the source addr is set in tcp_adapt_ire() 6366 * using the reply to the T_BIND message that we send 6367 * down to IP here and the check is repeated in tcp_rput_other. 6368 */ 6369 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6370 (dstport == tcp->tcp_lport)) { 6371 error = -TBADADDR; 6372 goto failed; 6373 } 6374 6375 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6376 tcp->tcp_remote_v6 = *dstaddrp; 6377 tcp->tcp_ip6h->ip6_vcf = 6378 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6379 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6380 6381 /* 6382 * Massage a routing header (if present) putting the first hop 6383 * in ip6_dst. Compute a starting value for the checksum which 6384 * takes into account that the original ip6_dst should be 6385 * included in the checksum but that ip will include the 6386 * first hop in the source route in the tcp checksum. 6387 */ 6388 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6389 if (rth != NULL) { 6390 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6391 tcps->tcps_netstack); 6392 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6393 (tcp->tcp_sum >> 16)); 6394 } else { 6395 tcp->tcp_sum = 0; 6396 } 6397 6398 tcph = tcp->tcp_tcph; 6399 *(uint16_t *)tcph->th_fport = dstport; 6400 tcp->tcp_fport = dstport; 6401 6402 oldstate = tcp->tcp_state; 6403 /* 6404 * At this point the remote destination address and remote port fields 6405 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6406 * have to see which state tcp was in so we can take apropriate action. 6407 */ 6408 if (oldstate == TCPS_IDLE) { 6409 /* 6410 * We support a quick connect capability here, allowing 6411 * clients to transition directly from IDLE to SYN_SENT 6412 * tcp_bindi will pick an unused port, insert the connection 6413 * in the bind hash and transition to BOUND state. 6414 */ 6415 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6416 tcp, B_TRUE); 6417 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6418 B_FALSE, B_FALSE); 6419 if (lport == 0) { 6420 error = -TNOADDR; 6421 goto failed; 6422 } 6423 } 6424 tcp->tcp_state = TCPS_SYN_SENT; 6425 6426 mp = allocb(sizeof (ire_t), BPRI_HI); 6427 if (mp != NULL) { 6428 in6_addr_t v6src; 6429 6430 mp->b_wptr += sizeof (ire_t); 6431 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6432 6433 tcp->tcp_hard_binding = 1; 6434 6435 /* 6436 * We need to make sure that the conn_recv is set to a non-null 6437 * value before we insert the conn_t into the classifier table. 6438 * This is to avoid a race with an incoming packet which does 6439 * an ipcl_classify(). 6440 */ 6441 tcp->tcp_connp->conn_recv = tcp_input; 6442 6443 if (tcp->tcp_ipversion == IPV4_VERSION) { 6444 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6445 } else { 6446 v6src = tcp->tcp_ip6h->ip6_src; 6447 } 6448 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6449 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6450 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE); 6451 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6452 tcp->tcp_active_open = 1; 6453 6454 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6455 } 6456 /* Error case */ 6457 tcp->tcp_state = oldstate; 6458 error = ENOMEM; 6459 6460 failed: 6461 /* return error ack and blow away saved option results if any */ 6462 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6463 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6464 return (error); 6465 } 6466 6467 /* 6468 * We need a stream q for detached closing tcp connections 6469 * to use. Our client hereby indicates that this q is the 6470 * one to use. 6471 */ 6472 static void 6473 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6474 { 6475 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6476 queue_t *q = tcp->tcp_wq; 6477 tcp_stack_t *tcps = tcp->tcp_tcps; 6478 6479 #ifdef NS_DEBUG 6480 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6481 tcps->tcps_netstack->netstack_stackid); 6482 #endif 6483 mp->b_datap->db_type = M_IOCACK; 6484 iocp->ioc_count = 0; 6485 mutex_enter(&tcps->tcps_g_q_lock); 6486 if (tcps->tcps_g_q != NULL) { 6487 mutex_exit(&tcps->tcps_g_q_lock); 6488 iocp->ioc_error = EALREADY; 6489 } else { 6490 int error = 0; 6491 conn_t *connp = tcp->tcp_connp; 6492 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6493 6494 tcps->tcps_g_q = tcp->tcp_rq; 6495 mutex_exit(&tcps->tcps_g_q_lock); 6496 iocp->ioc_error = 0; 6497 iocp->ioc_rval = 0; 6498 /* 6499 * We are passing tcp_sticky_ipp as NULL 6500 * as it is not useful for tcp_default queue 6501 * 6502 * Set conn_recv just in case. 6503 */ 6504 tcp->tcp_connp->conn_recv = tcp_conn_request; 6505 6506 ASSERT(connp->conn_af_isv6); 6507 connp->conn_ulp = IPPROTO_TCP; 6508 6509 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6510 NULL || connp->conn_mac_exempt) { 6511 error = -TBADADDR; 6512 } else { 6513 connp->conn_srcv6 = ipv6_all_zeros; 6514 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6515 } 6516 6517 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6518 } 6519 qreply(q, mp); 6520 } 6521 6522 static int 6523 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6524 { 6525 tcp_t *ltcp = NULL; 6526 conn_t *connp; 6527 tcp_stack_t *tcps = tcp->tcp_tcps; 6528 6529 /* 6530 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6531 * when the stream is in BOUND state. Do not send a reset, 6532 * since the destination IP address is not valid, and it can 6533 * be the initialized value of all zeros (broadcast address). 6534 * 6535 * XXX There won't be any pending bind request to IP. 6536 */ 6537 if (tcp->tcp_state <= TCPS_BOUND) { 6538 if (tcp->tcp_debug) { 6539 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6540 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6541 } 6542 return (TOUTSTATE); 6543 } 6544 6545 6546 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6547 6548 /* 6549 * According to TPI, for non-listeners, ignore seqnum 6550 * and disconnect. 6551 * Following interpretation of -1 seqnum is historical 6552 * and implied TPI ? (TPI only states that for T_CONN_IND, 6553 * a valid seqnum should not be -1). 6554 * 6555 * -1 means disconnect everything 6556 * regardless even on a listener. 6557 */ 6558 6559 int old_state = tcp->tcp_state; 6560 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6561 6562 /* 6563 * The connection can't be on the tcp_time_wait_head list 6564 * since it is not detached. 6565 */ 6566 ASSERT(tcp->tcp_time_wait_next == NULL); 6567 ASSERT(tcp->tcp_time_wait_prev == NULL); 6568 ASSERT(tcp->tcp_time_wait_expire == 0); 6569 ltcp = NULL; 6570 /* 6571 * If it used to be a listener, check to make sure no one else 6572 * has taken the port before switching back to LISTEN state. 6573 */ 6574 if (tcp->tcp_ipversion == IPV4_VERSION) { 6575 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6576 tcp->tcp_ipha->ipha_src, 6577 tcp->tcp_connp->conn_zoneid, ipst); 6578 if (connp != NULL) 6579 ltcp = connp->conn_tcp; 6580 } else { 6581 /* Allow tcp_bound_if listeners? */ 6582 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6583 &tcp->tcp_ip6h->ip6_src, 0, 6584 tcp->tcp_connp->conn_zoneid, ipst); 6585 if (connp != NULL) 6586 ltcp = connp->conn_tcp; 6587 } 6588 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6589 tcp->tcp_state = TCPS_LISTEN; 6590 } else if (old_state > TCPS_BOUND) { 6591 tcp->tcp_conn_req_max = 0; 6592 tcp->tcp_state = TCPS_BOUND; 6593 } 6594 if (ltcp != NULL) 6595 CONN_DEC_REF(ltcp->tcp_connp); 6596 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6597 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6598 } else if (old_state == TCPS_ESTABLISHED || 6599 old_state == TCPS_CLOSE_WAIT) { 6600 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6601 } 6602 6603 if (tcp->tcp_fused) 6604 tcp_unfuse(tcp); 6605 6606 mutex_enter(&tcp->tcp_eager_lock); 6607 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6608 (tcp->tcp_conn_req_cnt_q != 0)) { 6609 tcp_eager_cleanup(tcp, 0); 6610 } 6611 mutex_exit(&tcp->tcp_eager_lock); 6612 6613 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6614 tcp->tcp_rnxt, TH_RST | TH_ACK); 6615 6616 tcp_reinit(tcp); 6617 6618 return (0); 6619 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6620 return (TBADSEQ); 6621 } 6622 return (0); 6623 } 6624 6625 /* 6626 * Our client hereby directs us to reject the connection request 6627 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6628 * of sending the appropriate RST, not an ICMP error. 6629 */ 6630 static void 6631 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6632 { 6633 t_scalar_t seqnum; 6634 int error; 6635 6636 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6637 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6638 tcp_err_ack(tcp, mp, TPROTO, 0); 6639 return; 6640 } 6641 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6642 error = tcp_disconnect_common(tcp, seqnum); 6643 if (error != 0) 6644 tcp_err_ack(tcp, mp, error, 0); 6645 else { 6646 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6647 /* Send M_FLUSH according to TPI */ 6648 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6649 } 6650 mp = mi_tpi_ok_ack_alloc(mp); 6651 if (mp) 6652 putnext(tcp->tcp_rq, mp); 6653 } 6654 } 6655 6656 /* 6657 * Diagnostic routine used to return a string associated with the tcp state. 6658 * Note that if the caller does not supply a buffer, it will use an internal 6659 * static string. This means that if multiple threads call this function at 6660 * the same time, output can be corrupted... Note also that this function 6661 * does not check the size of the supplied buffer. The caller has to make 6662 * sure that it is big enough. 6663 */ 6664 static char * 6665 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6666 { 6667 char buf1[30]; 6668 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6669 char *buf; 6670 char *cp; 6671 in6_addr_t local, remote; 6672 char local_addrbuf[INET6_ADDRSTRLEN]; 6673 char remote_addrbuf[INET6_ADDRSTRLEN]; 6674 6675 if (sup_buf != NULL) 6676 buf = sup_buf; 6677 else 6678 buf = priv_buf; 6679 6680 if (tcp == NULL) 6681 return ("NULL_TCP"); 6682 switch (tcp->tcp_state) { 6683 case TCPS_CLOSED: 6684 cp = "TCP_CLOSED"; 6685 break; 6686 case TCPS_IDLE: 6687 cp = "TCP_IDLE"; 6688 break; 6689 case TCPS_BOUND: 6690 cp = "TCP_BOUND"; 6691 break; 6692 case TCPS_LISTEN: 6693 cp = "TCP_LISTEN"; 6694 break; 6695 case TCPS_SYN_SENT: 6696 cp = "TCP_SYN_SENT"; 6697 break; 6698 case TCPS_SYN_RCVD: 6699 cp = "TCP_SYN_RCVD"; 6700 break; 6701 case TCPS_ESTABLISHED: 6702 cp = "TCP_ESTABLISHED"; 6703 break; 6704 case TCPS_CLOSE_WAIT: 6705 cp = "TCP_CLOSE_WAIT"; 6706 break; 6707 case TCPS_FIN_WAIT_1: 6708 cp = "TCP_FIN_WAIT_1"; 6709 break; 6710 case TCPS_CLOSING: 6711 cp = "TCP_CLOSING"; 6712 break; 6713 case TCPS_LAST_ACK: 6714 cp = "TCP_LAST_ACK"; 6715 break; 6716 case TCPS_FIN_WAIT_2: 6717 cp = "TCP_FIN_WAIT_2"; 6718 break; 6719 case TCPS_TIME_WAIT: 6720 cp = "TCP_TIME_WAIT"; 6721 break; 6722 default: 6723 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6724 cp = buf1; 6725 break; 6726 } 6727 switch (format) { 6728 case DISP_ADDR_AND_PORT: 6729 if (tcp->tcp_ipversion == IPV4_VERSION) { 6730 /* 6731 * Note that we use the remote address in the tcp_b 6732 * structure. This means that it will print out 6733 * the real destination address, not the next hop's 6734 * address if source routing is used. 6735 */ 6736 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6737 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6738 6739 } else { 6740 local = tcp->tcp_ip_src_v6; 6741 remote = tcp->tcp_remote_v6; 6742 } 6743 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6744 sizeof (local_addrbuf)); 6745 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6746 sizeof (remote_addrbuf)); 6747 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6748 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6749 ntohs(tcp->tcp_fport), cp); 6750 break; 6751 case DISP_PORT_ONLY: 6752 default: 6753 (void) mi_sprintf(buf, "[%u, %u] %s", 6754 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6755 break; 6756 } 6757 6758 return (buf); 6759 } 6760 6761 /* 6762 * Called via squeue to get on to eager's perimeter. It sends a 6763 * TH_RST if eager is in the fanout table. The listener wants the 6764 * eager to disappear either by means of tcp_eager_blowoff() or 6765 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6766 * called (via squeue) if the eager cannot be inserted in the 6767 * fanout table in tcp_conn_request(). 6768 */ 6769 /* ARGSUSED */ 6770 void 6771 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6772 { 6773 conn_t *econnp = (conn_t *)arg; 6774 tcp_t *eager = econnp->conn_tcp; 6775 tcp_t *listener = eager->tcp_listener; 6776 tcp_stack_t *tcps = eager->tcp_tcps; 6777 6778 /* 6779 * We could be called because listener is closing. Since 6780 * the eager is using listener's queue's, its not safe. 6781 * Better use the default queue just to send the TH_RST 6782 * out. 6783 */ 6784 ASSERT(tcps->tcps_g_q != NULL); 6785 eager->tcp_rq = tcps->tcps_g_q; 6786 eager->tcp_wq = WR(tcps->tcps_g_q); 6787 6788 /* 6789 * An eager's conn_fanout will be NULL if it's a duplicate 6790 * for an existing 4-tuples in the conn fanout table. 6791 * We don't want to send an RST out in such case. 6792 */ 6793 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6794 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6795 eager, eager->tcp_snxt, 0, TH_RST); 6796 } 6797 6798 /* We are here because listener wants this eager gone */ 6799 if (listener != NULL) { 6800 mutex_enter(&listener->tcp_eager_lock); 6801 tcp_eager_unlink(eager); 6802 if (eager->tcp_tconnind_started) { 6803 /* 6804 * The eager has sent a conn_ind up to the 6805 * listener but listener decides to close 6806 * instead. We need to drop the extra ref 6807 * placed on eager in tcp_rput_data() before 6808 * sending the conn_ind to listener. 6809 */ 6810 CONN_DEC_REF(econnp); 6811 } 6812 mutex_exit(&listener->tcp_eager_lock); 6813 CONN_DEC_REF(listener->tcp_connp); 6814 } 6815 6816 if (eager->tcp_state > TCPS_BOUND) 6817 tcp_close_detached(eager); 6818 } 6819 6820 /* 6821 * Reset any eager connection hanging off this listener marked 6822 * with 'seqnum' and then reclaim it's resources. 6823 */ 6824 static boolean_t 6825 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6826 { 6827 tcp_t *eager; 6828 mblk_t *mp; 6829 tcp_stack_t *tcps = listener->tcp_tcps; 6830 6831 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6832 eager = listener; 6833 mutex_enter(&listener->tcp_eager_lock); 6834 do { 6835 eager = eager->tcp_eager_next_q; 6836 if (eager == NULL) { 6837 mutex_exit(&listener->tcp_eager_lock); 6838 return (B_FALSE); 6839 } 6840 } while (eager->tcp_conn_req_seqnum != seqnum); 6841 6842 if (eager->tcp_closemp_used) { 6843 mutex_exit(&listener->tcp_eager_lock); 6844 return (B_TRUE); 6845 } 6846 eager->tcp_closemp_used = B_TRUE; 6847 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6848 CONN_INC_REF(eager->tcp_connp); 6849 mutex_exit(&listener->tcp_eager_lock); 6850 mp = &eager->tcp_closemp; 6851 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6852 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6853 return (B_TRUE); 6854 } 6855 6856 /* 6857 * Reset any eager connection hanging off this listener 6858 * and then reclaim it's resources. 6859 */ 6860 static void 6861 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6862 { 6863 tcp_t *eager; 6864 mblk_t *mp; 6865 tcp_stack_t *tcps = listener->tcp_tcps; 6866 6867 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6868 6869 if (!q0_only) { 6870 /* First cleanup q */ 6871 TCP_STAT(tcps, tcp_eager_blowoff_q); 6872 eager = listener->tcp_eager_next_q; 6873 while (eager != NULL) { 6874 if (!eager->tcp_closemp_used) { 6875 eager->tcp_closemp_used = B_TRUE; 6876 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6877 CONN_INC_REF(eager->tcp_connp); 6878 mp = &eager->tcp_closemp; 6879 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6880 tcp_eager_kill, eager->tcp_connp, 6881 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6882 } 6883 eager = eager->tcp_eager_next_q; 6884 } 6885 } 6886 /* Then cleanup q0 */ 6887 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6888 eager = listener->tcp_eager_next_q0; 6889 while (eager != listener) { 6890 if (!eager->tcp_closemp_used) { 6891 eager->tcp_closemp_used = B_TRUE; 6892 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6893 CONN_INC_REF(eager->tcp_connp); 6894 mp = &eager->tcp_closemp; 6895 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6896 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6897 SQTAG_TCP_EAGER_CLEANUP_Q0); 6898 } 6899 eager = eager->tcp_eager_next_q0; 6900 } 6901 } 6902 6903 /* 6904 * If we are an eager connection hanging off a listener that hasn't 6905 * formally accepted the connection yet, get off his list and blow off 6906 * any data that we have accumulated. 6907 */ 6908 static void 6909 tcp_eager_unlink(tcp_t *tcp) 6910 { 6911 tcp_t *listener = tcp->tcp_listener; 6912 6913 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6914 ASSERT(listener != NULL); 6915 if (tcp->tcp_eager_next_q0 != NULL) { 6916 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6917 6918 /* Remove the eager tcp from q0 */ 6919 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6920 tcp->tcp_eager_prev_q0; 6921 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6922 tcp->tcp_eager_next_q0; 6923 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6924 listener->tcp_conn_req_cnt_q0--; 6925 6926 tcp->tcp_eager_next_q0 = NULL; 6927 tcp->tcp_eager_prev_q0 = NULL; 6928 6929 /* 6930 * Take the eager out, if it is in the list of droppable 6931 * eagers. 6932 */ 6933 MAKE_UNDROPPABLE(tcp); 6934 6935 if (tcp->tcp_syn_rcvd_timeout != 0) { 6936 /* we have timed out before */ 6937 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6938 listener->tcp_syn_rcvd_timeout--; 6939 } 6940 } else { 6941 tcp_t **tcpp = &listener->tcp_eager_next_q; 6942 tcp_t *prev = NULL; 6943 6944 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6945 if (tcpp[0] == tcp) { 6946 if (listener->tcp_eager_last_q == tcp) { 6947 /* 6948 * If we are unlinking the last 6949 * element on the list, adjust 6950 * tail pointer. Set tail pointer 6951 * to nil when list is empty. 6952 */ 6953 ASSERT(tcp->tcp_eager_next_q == NULL); 6954 if (listener->tcp_eager_last_q == 6955 listener->tcp_eager_next_q) { 6956 listener->tcp_eager_last_q = 6957 NULL; 6958 } else { 6959 /* 6960 * We won't get here if there 6961 * is only one eager in the 6962 * list. 6963 */ 6964 ASSERT(prev != NULL); 6965 listener->tcp_eager_last_q = 6966 prev; 6967 } 6968 } 6969 tcpp[0] = tcp->tcp_eager_next_q; 6970 tcp->tcp_eager_next_q = NULL; 6971 tcp->tcp_eager_last_q = NULL; 6972 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6973 listener->tcp_conn_req_cnt_q--; 6974 break; 6975 } 6976 prev = tcpp[0]; 6977 } 6978 } 6979 tcp->tcp_listener = NULL; 6980 } 6981 6982 /* Shorthand to generate and send TPI error acks to our client */ 6983 static void 6984 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6985 { 6986 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6987 putnext(tcp->tcp_rq, mp); 6988 } 6989 6990 /* Shorthand to generate and send TPI error acks to our client */ 6991 static void 6992 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6993 int t_error, int sys_error) 6994 { 6995 struct T_error_ack *teackp; 6996 6997 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6998 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6999 teackp = (struct T_error_ack *)mp->b_rptr; 7000 teackp->ERROR_prim = primitive; 7001 teackp->TLI_error = t_error; 7002 teackp->UNIX_error = sys_error; 7003 putnext(tcp->tcp_rq, mp); 7004 } 7005 } 7006 7007 /* 7008 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7009 * but instead the code relies on: 7010 * - the fact that the address of the array and its size never changes 7011 * - the atomic assignment of the elements of the array 7012 */ 7013 /* ARGSUSED */ 7014 static int 7015 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7016 { 7017 int i; 7018 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7019 7020 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7021 if (tcps->tcps_g_epriv_ports[i] != 0) 7022 (void) mi_mpprintf(mp, "%d ", 7023 tcps->tcps_g_epriv_ports[i]); 7024 } 7025 return (0); 7026 } 7027 7028 /* 7029 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7030 * threads from changing it at the same time. 7031 */ 7032 /* ARGSUSED */ 7033 static int 7034 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7035 cred_t *cr) 7036 { 7037 long new_value; 7038 int i; 7039 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7040 7041 /* 7042 * Fail the request if the new value does not lie within the 7043 * port number limits. 7044 */ 7045 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7046 new_value <= 0 || new_value >= 65536) { 7047 return (EINVAL); 7048 } 7049 7050 mutex_enter(&tcps->tcps_epriv_port_lock); 7051 /* Check if the value is already in the list */ 7052 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7053 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7054 mutex_exit(&tcps->tcps_epriv_port_lock); 7055 return (EEXIST); 7056 } 7057 } 7058 /* Find an empty slot */ 7059 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7060 if (tcps->tcps_g_epriv_ports[i] == 0) 7061 break; 7062 } 7063 if (i == tcps->tcps_g_num_epriv_ports) { 7064 mutex_exit(&tcps->tcps_epriv_port_lock); 7065 return (EOVERFLOW); 7066 } 7067 /* Set the new value */ 7068 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7069 mutex_exit(&tcps->tcps_epriv_port_lock); 7070 return (0); 7071 } 7072 7073 /* 7074 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7075 * threads from changing it at the same time. 7076 */ 7077 /* ARGSUSED */ 7078 static int 7079 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7080 cred_t *cr) 7081 { 7082 long new_value; 7083 int i; 7084 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7085 7086 /* 7087 * Fail the request if the new value does not lie within the 7088 * port number limits. 7089 */ 7090 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7091 new_value >= 65536) { 7092 return (EINVAL); 7093 } 7094 7095 mutex_enter(&tcps->tcps_epriv_port_lock); 7096 /* Check that the value is already in the list */ 7097 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7098 if (tcps->tcps_g_epriv_ports[i] == new_value) 7099 break; 7100 } 7101 if (i == tcps->tcps_g_num_epriv_ports) { 7102 mutex_exit(&tcps->tcps_epriv_port_lock); 7103 return (ESRCH); 7104 } 7105 /* Clear the value */ 7106 tcps->tcps_g_epriv_ports[i] = 0; 7107 mutex_exit(&tcps->tcps_epriv_port_lock); 7108 return (0); 7109 } 7110 7111 /* Return the TPI/TLI equivalent of our current tcp_state */ 7112 static int 7113 tcp_tpistate(tcp_t *tcp) 7114 { 7115 switch (tcp->tcp_state) { 7116 case TCPS_IDLE: 7117 return (TS_UNBND); 7118 case TCPS_LISTEN: 7119 /* 7120 * Return whether there are outstanding T_CONN_IND waiting 7121 * for the matching T_CONN_RES. Therefore don't count q0. 7122 */ 7123 if (tcp->tcp_conn_req_cnt_q > 0) 7124 return (TS_WRES_CIND); 7125 else 7126 return (TS_IDLE); 7127 case TCPS_BOUND: 7128 return (TS_IDLE); 7129 case TCPS_SYN_SENT: 7130 return (TS_WCON_CREQ); 7131 case TCPS_SYN_RCVD: 7132 /* 7133 * Note: assumption: this has to the active open SYN_RCVD. 7134 * The passive instance is detached in SYN_RCVD stage of 7135 * incoming connection processing so we cannot get request 7136 * for T_info_ack on it. 7137 */ 7138 return (TS_WACK_CRES); 7139 case TCPS_ESTABLISHED: 7140 return (TS_DATA_XFER); 7141 case TCPS_CLOSE_WAIT: 7142 return (TS_WREQ_ORDREL); 7143 case TCPS_FIN_WAIT_1: 7144 return (TS_WIND_ORDREL); 7145 case TCPS_FIN_WAIT_2: 7146 return (TS_WIND_ORDREL); 7147 7148 case TCPS_CLOSING: 7149 case TCPS_LAST_ACK: 7150 case TCPS_TIME_WAIT: 7151 case TCPS_CLOSED: 7152 /* 7153 * Following TS_WACK_DREQ7 is a rendition of "not 7154 * yet TS_IDLE" TPI state. There is no best match to any 7155 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7156 * choose a value chosen that will map to TLI/XTI level 7157 * state of TSTATECHNG (state is process of changing) which 7158 * captures what this dummy state represents. 7159 */ 7160 return (TS_WACK_DREQ7); 7161 default: 7162 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7163 tcp->tcp_state, tcp_display(tcp, NULL, 7164 DISP_PORT_ONLY)); 7165 return (TS_UNBND); 7166 } 7167 } 7168 7169 static void 7170 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7171 { 7172 tcp_stack_t *tcps = tcp->tcp_tcps; 7173 7174 if (tcp->tcp_family == AF_INET6) 7175 *tia = tcp_g_t_info_ack_v6; 7176 else 7177 *tia = tcp_g_t_info_ack; 7178 tia->CURRENT_state = tcp_tpistate(tcp); 7179 tia->OPT_size = tcp_max_optsize; 7180 if (tcp->tcp_mss == 0) { 7181 /* Not yet set - tcp_open does not set mss */ 7182 if (tcp->tcp_ipversion == IPV4_VERSION) 7183 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7184 else 7185 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7186 } else { 7187 tia->TIDU_size = tcp->tcp_mss; 7188 } 7189 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7190 } 7191 7192 static void 7193 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7194 t_uscalar_t cap_bits1) 7195 { 7196 tcap->CAP_bits1 = 0; 7197 7198 if (cap_bits1 & TC1_INFO) { 7199 tcp_copy_info(&tcap->INFO_ack, tcp); 7200 tcap->CAP_bits1 |= TC1_INFO; 7201 } 7202 7203 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7204 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7205 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7206 } 7207 7208 } 7209 7210 /* 7211 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7212 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7213 * tcp_g_t_info_ack. The current state of the stream is copied from 7214 * tcp_state. 7215 */ 7216 static void 7217 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7218 { 7219 t_uscalar_t cap_bits1; 7220 struct T_capability_ack *tcap; 7221 7222 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7223 freemsg(mp); 7224 return; 7225 } 7226 7227 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7228 7229 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7230 mp->b_datap->db_type, T_CAPABILITY_ACK); 7231 if (mp == NULL) 7232 return; 7233 7234 tcap = (struct T_capability_ack *)mp->b_rptr; 7235 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7236 7237 putnext(tcp->tcp_rq, mp); 7238 } 7239 7240 /* 7241 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7242 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7243 * The current state of the stream is copied from tcp_state. 7244 */ 7245 static void 7246 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7247 { 7248 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7249 T_INFO_ACK); 7250 if (!mp) { 7251 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7252 return; 7253 } 7254 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7255 putnext(tcp->tcp_rq, mp); 7256 } 7257 7258 /* Respond to the TPI addr request */ 7259 static void 7260 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7261 { 7262 sin_t *sin; 7263 mblk_t *ackmp; 7264 struct T_addr_ack *taa; 7265 7266 /* Make it large enough for worst case */ 7267 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7268 2 * sizeof (sin6_t), 1); 7269 if (ackmp == NULL) { 7270 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7271 return; 7272 } 7273 7274 if (tcp->tcp_ipversion == IPV6_VERSION) { 7275 tcp_addr_req_ipv6(tcp, ackmp); 7276 return; 7277 } 7278 taa = (struct T_addr_ack *)ackmp->b_rptr; 7279 7280 bzero(taa, sizeof (struct T_addr_ack)); 7281 ackmp->b_wptr = (uchar_t *)&taa[1]; 7282 7283 taa->PRIM_type = T_ADDR_ACK; 7284 ackmp->b_datap->db_type = M_PCPROTO; 7285 7286 /* 7287 * Note: Following code assumes 32 bit alignment of basic 7288 * data structures like sin_t and struct T_addr_ack. 7289 */ 7290 if (tcp->tcp_state >= TCPS_BOUND) { 7291 /* 7292 * Fill in local address 7293 */ 7294 taa->LOCADDR_length = sizeof (sin_t); 7295 taa->LOCADDR_offset = sizeof (*taa); 7296 7297 sin = (sin_t *)&taa[1]; 7298 7299 /* Fill zeroes and then intialize non-zero fields */ 7300 *sin = sin_null; 7301 7302 sin->sin_family = AF_INET; 7303 7304 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7305 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7306 7307 ackmp->b_wptr = (uchar_t *)&sin[1]; 7308 7309 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7310 /* 7311 * Fill in Remote address 7312 */ 7313 taa->REMADDR_length = sizeof (sin_t); 7314 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7315 taa->LOCADDR_length); 7316 7317 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7318 *sin = sin_null; 7319 sin->sin_family = AF_INET; 7320 sin->sin_addr.s_addr = tcp->tcp_remote; 7321 sin->sin_port = tcp->tcp_fport; 7322 7323 ackmp->b_wptr = (uchar_t *)&sin[1]; 7324 } 7325 } 7326 putnext(tcp->tcp_rq, ackmp); 7327 } 7328 7329 /* Assumes that tcp_addr_req gets enough space and alignment */ 7330 static void 7331 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7332 { 7333 sin6_t *sin6; 7334 struct T_addr_ack *taa; 7335 7336 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7337 ASSERT(OK_32PTR(ackmp->b_rptr)); 7338 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7339 2 * sizeof (sin6_t)); 7340 7341 taa = (struct T_addr_ack *)ackmp->b_rptr; 7342 7343 bzero(taa, sizeof (struct T_addr_ack)); 7344 ackmp->b_wptr = (uchar_t *)&taa[1]; 7345 7346 taa->PRIM_type = T_ADDR_ACK; 7347 ackmp->b_datap->db_type = M_PCPROTO; 7348 7349 /* 7350 * Note: Following code assumes 32 bit alignment of basic 7351 * data structures like sin6_t and struct T_addr_ack. 7352 */ 7353 if (tcp->tcp_state >= TCPS_BOUND) { 7354 /* 7355 * Fill in local address 7356 */ 7357 taa->LOCADDR_length = sizeof (sin6_t); 7358 taa->LOCADDR_offset = sizeof (*taa); 7359 7360 sin6 = (sin6_t *)&taa[1]; 7361 *sin6 = sin6_null; 7362 7363 sin6->sin6_family = AF_INET6; 7364 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7365 sin6->sin6_port = tcp->tcp_lport; 7366 7367 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7368 7369 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7370 /* 7371 * Fill in Remote address 7372 */ 7373 taa->REMADDR_length = sizeof (sin6_t); 7374 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7375 taa->LOCADDR_length); 7376 7377 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7378 *sin6 = sin6_null; 7379 sin6->sin6_family = AF_INET6; 7380 sin6->sin6_flowinfo = 7381 tcp->tcp_ip6h->ip6_vcf & 7382 ~IPV6_VERS_AND_FLOW_MASK; 7383 sin6->sin6_addr = tcp->tcp_remote_v6; 7384 sin6->sin6_port = tcp->tcp_fport; 7385 7386 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7387 } 7388 } 7389 putnext(tcp->tcp_rq, ackmp); 7390 } 7391 7392 /* 7393 * Handle reinitialization of a tcp structure. 7394 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7395 */ 7396 static void 7397 tcp_reinit(tcp_t *tcp) 7398 { 7399 mblk_t *mp; 7400 int err; 7401 tcp_stack_t *tcps = tcp->tcp_tcps; 7402 7403 TCP_STAT(tcps, tcp_reinit_calls); 7404 7405 /* tcp_reinit should never be called for detached tcp_t's */ 7406 ASSERT(tcp->tcp_listener == NULL); 7407 ASSERT((tcp->tcp_family == AF_INET && 7408 tcp->tcp_ipversion == IPV4_VERSION) || 7409 (tcp->tcp_family == AF_INET6 && 7410 (tcp->tcp_ipversion == IPV4_VERSION || 7411 tcp->tcp_ipversion == IPV6_VERSION))); 7412 7413 /* Cancel outstanding timers */ 7414 tcp_timers_stop(tcp); 7415 7416 /* 7417 * Reset everything in the state vector, after updating global 7418 * MIB data from instance counters. 7419 */ 7420 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7421 tcp->tcp_ibsegs = 0; 7422 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7423 tcp->tcp_obsegs = 0; 7424 7425 tcp_close_mpp(&tcp->tcp_xmit_head); 7426 if (tcp->tcp_snd_zcopy_aware) 7427 tcp_zcopy_notify(tcp); 7428 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7429 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7430 mutex_enter(&tcp->tcp_non_sq_lock); 7431 if (tcp->tcp_flow_stopped && 7432 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7433 tcp_clrqfull(tcp); 7434 } 7435 mutex_exit(&tcp->tcp_non_sq_lock); 7436 tcp_close_mpp(&tcp->tcp_reass_head); 7437 tcp->tcp_reass_tail = NULL; 7438 if (tcp->tcp_rcv_list != NULL) { 7439 /* Free b_next chain */ 7440 tcp_close_mpp(&tcp->tcp_rcv_list); 7441 tcp->tcp_rcv_last_head = NULL; 7442 tcp->tcp_rcv_last_tail = NULL; 7443 tcp->tcp_rcv_cnt = 0; 7444 } 7445 tcp->tcp_rcv_last_tail = NULL; 7446 7447 if ((mp = tcp->tcp_urp_mp) != NULL) { 7448 freemsg(mp); 7449 tcp->tcp_urp_mp = NULL; 7450 } 7451 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7452 freemsg(mp); 7453 tcp->tcp_urp_mark_mp = NULL; 7454 } 7455 if (tcp->tcp_fused_sigurg_mp != NULL) { 7456 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7457 freeb(tcp->tcp_fused_sigurg_mp); 7458 tcp->tcp_fused_sigurg_mp = NULL; 7459 } 7460 if (tcp->tcp_ordrel_mp != NULL) { 7461 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7462 freeb(tcp->tcp_ordrel_mp); 7463 tcp->tcp_ordrel_mp = NULL; 7464 } 7465 7466 /* 7467 * Following is a union with two members which are 7468 * identical types and size so the following cleanup 7469 * is enough. 7470 */ 7471 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7472 7473 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7474 7475 /* 7476 * The connection can't be on the tcp_time_wait_head list 7477 * since it is not detached. 7478 */ 7479 ASSERT(tcp->tcp_time_wait_next == NULL); 7480 ASSERT(tcp->tcp_time_wait_prev == NULL); 7481 ASSERT(tcp->tcp_time_wait_expire == 0); 7482 7483 if (tcp->tcp_kssl_pending) { 7484 tcp->tcp_kssl_pending = B_FALSE; 7485 7486 /* Don't reset if the initialized by bind. */ 7487 if (tcp->tcp_kssl_ent != NULL) { 7488 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7489 KSSL_NO_PROXY); 7490 } 7491 } 7492 if (tcp->tcp_kssl_ctx != NULL) { 7493 kssl_release_ctx(tcp->tcp_kssl_ctx); 7494 tcp->tcp_kssl_ctx = NULL; 7495 } 7496 7497 /* 7498 * Reset/preserve other values 7499 */ 7500 tcp_reinit_values(tcp); 7501 ipcl_hash_remove(tcp->tcp_connp); 7502 conn_delete_ire(tcp->tcp_connp, NULL); 7503 tcp_ipsec_cleanup(tcp); 7504 7505 if (tcp->tcp_conn_req_max != 0) { 7506 /* 7507 * This is the case when a TLI program uses the same 7508 * transport end point to accept a connection. This 7509 * makes the TCP both a listener and acceptor. When 7510 * this connection is closed, we need to set the state 7511 * back to TCPS_LISTEN. Make sure that the eager list 7512 * is reinitialized. 7513 * 7514 * Note that this stream is still bound to the four 7515 * tuples of the previous connection in IP. If a new 7516 * SYN with different foreign address comes in, IP will 7517 * not find it and will send it to the global queue. In 7518 * the global queue, TCP will do a tcp_lookup_listener() 7519 * to find this stream. This works because this stream 7520 * is only removed from connected hash. 7521 * 7522 */ 7523 tcp->tcp_state = TCPS_LISTEN; 7524 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7525 tcp->tcp_eager_next_drop_q0 = tcp; 7526 tcp->tcp_eager_prev_drop_q0 = tcp; 7527 tcp->tcp_connp->conn_recv = tcp_conn_request; 7528 if (tcp->tcp_family == AF_INET6) { 7529 ASSERT(tcp->tcp_connp->conn_af_isv6); 7530 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7531 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7532 } else { 7533 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7534 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7535 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7536 } 7537 } else { 7538 tcp->tcp_state = TCPS_BOUND; 7539 } 7540 7541 /* 7542 * Initialize to default values 7543 * Can't fail since enough header template space already allocated 7544 * at open(). 7545 */ 7546 err = tcp_init_values(tcp); 7547 ASSERT(err == 0); 7548 /* Restore state in tcp_tcph */ 7549 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7550 if (tcp->tcp_ipversion == IPV4_VERSION) 7551 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7552 else 7553 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7554 /* 7555 * Copy of the src addr. in tcp_t is needed in tcp_t 7556 * since the lookup funcs can only lookup on tcp_t 7557 */ 7558 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7559 7560 ASSERT(tcp->tcp_ptpbhn != NULL); 7561 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7562 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7563 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7564 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7565 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7566 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7567 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7568 } 7569 7570 /* 7571 * Force values to zero that need be zero. 7572 * Do not touch values asociated with the BOUND or LISTEN state 7573 * since the connection will end up in that state after the reinit. 7574 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7575 * structure! 7576 */ 7577 static void 7578 tcp_reinit_values(tcp) 7579 tcp_t *tcp; 7580 { 7581 tcp_stack_t *tcps = tcp->tcp_tcps; 7582 7583 #ifndef lint 7584 #define DONTCARE(x) 7585 #define PRESERVE(x) 7586 #else 7587 #define DONTCARE(x) ((x) = (x)) 7588 #define PRESERVE(x) ((x) = (x)) 7589 #endif /* lint */ 7590 7591 PRESERVE(tcp->tcp_bind_hash_port); 7592 PRESERVE(tcp->tcp_bind_hash); 7593 PRESERVE(tcp->tcp_ptpbhn); 7594 PRESERVE(tcp->tcp_acceptor_hash); 7595 PRESERVE(tcp->tcp_ptpahn); 7596 7597 /* Should be ASSERT NULL on these with new code! */ 7598 ASSERT(tcp->tcp_time_wait_next == NULL); 7599 ASSERT(tcp->tcp_time_wait_prev == NULL); 7600 ASSERT(tcp->tcp_time_wait_expire == 0); 7601 PRESERVE(tcp->tcp_state); 7602 PRESERVE(tcp->tcp_rq); 7603 PRESERVE(tcp->tcp_wq); 7604 7605 ASSERT(tcp->tcp_xmit_head == NULL); 7606 ASSERT(tcp->tcp_xmit_last == NULL); 7607 ASSERT(tcp->tcp_unsent == 0); 7608 ASSERT(tcp->tcp_xmit_tail == NULL); 7609 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7610 7611 tcp->tcp_snxt = 0; /* Displayed in mib */ 7612 tcp->tcp_suna = 0; /* Displayed in mib */ 7613 tcp->tcp_swnd = 0; 7614 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7615 7616 ASSERT(tcp->tcp_ibsegs == 0); 7617 ASSERT(tcp->tcp_obsegs == 0); 7618 7619 if (tcp->tcp_iphc != NULL) { 7620 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7621 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7622 } 7623 7624 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7625 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7626 DONTCARE(tcp->tcp_ipha); 7627 DONTCARE(tcp->tcp_ip6h); 7628 DONTCARE(tcp->tcp_ip_hdr_len); 7629 DONTCARE(tcp->tcp_tcph); 7630 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7631 tcp->tcp_valid_bits = 0; 7632 7633 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7634 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7635 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7636 tcp->tcp_last_rcv_lbolt = 0; 7637 7638 tcp->tcp_init_cwnd = 0; 7639 7640 tcp->tcp_urp_last_valid = 0; 7641 tcp->tcp_hard_binding = 0; 7642 tcp->tcp_hard_bound = 0; 7643 PRESERVE(tcp->tcp_cred); 7644 PRESERVE(tcp->tcp_cpid); 7645 PRESERVE(tcp->tcp_open_time); 7646 PRESERVE(tcp->tcp_exclbind); 7647 7648 tcp->tcp_fin_acked = 0; 7649 tcp->tcp_fin_rcvd = 0; 7650 tcp->tcp_fin_sent = 0; 7651 tcp->tcp_ordrel_done = 0; 7652 7653 tcp->tcp_debug = 0; 7654 tcp->tcp_dontroute = 0; 7655 tcp->tcp_broadcast = 0; 7656 7657 tcp->tcp_useloopback = 0; 7658 tcp->tcp_reuseaddr = 0; 7659 tcp->tcp_oobinline = 0; 7660 tcp->tcp_dgram_errind = 0; 7661 7662 tcp->tcp_detached = 0; 7663 tcp->tcp_bind_pending = 0; 7664 tcp->tcp_unbind_pending = 0; 7665 7666 tcp->tcp_snd_ws_ok = B_FALSE; 7667 tcp->tcp_snd_ts_ok = B_FALSE; 7668 tcp->tcp_linger = 0; 7669 tcp->tcp_ka_enabled = 0; 7670 tcp->tcp_zero_win_probe = 0; 7671 7672 tcp->tcp_loopback = 0; 7673 tcp->tcp_refuse = 0; 7674 tcp->tcp_localnet = 0; 7675 tcp->tcp_syn_defense = 0; 7676 tcp->tcp_set_timer = 0; 7677 7678 tcp->tcp_active_open = 0; 7679 tcp->tcp_rexmit = B_FALSE; 7680 tcp->tcp_xmit_zc_clean = B_FALSE; 7681 7682 tcp->tcp_snd_sack_ok = B_FALSE; 7683 PRESERVE(tcp->tcp_recvdstaddr); 7684 tcp->tcp_hwcksum = B_FALSE; 7685 7686 tcp->tcp_ire_ill_check_done = B_FALSE; 7687 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7688 7689 tcp->tcp_mdt = B_FALSE; 7690 tcp->tcp_mdt_hdr_head = 0; 7691 tcp->tcp_mdt_hdr_tail = 0; 7692 7693 tcp->tcp_conn_def_q0 = 0; 7694 tcp->tcp_ip_forward_progress = B_FALSE; 7695 tcp->tcp_anon_priv_bind = 0; 7696 tcp->tcp_ecn_ok = B_FALSE; 7697 7698 tcp->tcp_cwr = B_FALSE; 7699 tcp->tcp_ecn_echo_on = B_FALSE; 7700 7701 if (tcp->tcp_sack_info != NULL) { 7702 if (tcp->tcp_notsack_list != NULL) { 7703 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7704 } 7705 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7706 tcp->tcp_sack_info = NULL; 7707 } 7708 7709 tcp->tcp_rcv_ws = 0; 7710 tcp->tcp_snd_ws = 0; 7711 tcp->tcp_ts_recent = 0; 7712 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7713 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7714 tcp->tcp_if_mtu = 0; 7715 7716 ASSERT(tcp->tcp_reass_head == NULL); 7717 ASSERT(tcp->tcp_reass_tail == NULL); 7718 7719 tcp->tcp_cwnd_cnt = 0; 7720 7721 ASSERT(tcp->tcp_rcv_list == NULL); 7722 ASSERT(tcp->tcp_rcv_last_head == NULL); 7723 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7724 ASSERT(tcp->tcp_rcv_cnt == 0); 7725 7726 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7727 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7728 tcp->tcp_csuna = 0; 7729 7730 tcp->tcp_rto = 0; /* Displayed in MIB */ 7731 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7732 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7733 tcp->tcp_rtt_update = 0; 7734 7735 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7736 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7737 7738 tcp->tcp_rack = 0; /* Displayed in mib */ 7739 tcp->tcp_rack_cnt = 0; 7740 tcp->tcp_rack_cur_max = 0; 7741 tcp->tcp_rack_abs_max = 0; 7742 7743 tcp->tcp_max_swnd = 0; 7744 7745 ASSERT(tcp->tcp_listener == NULL); 7746 7747 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7748 7749 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7750 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7751 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7752 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7753 7754 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7755 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7756 PRESERVE(tcp->tcp_conn_req_max); 7757 PRESERVE(tcp->tcp_conn_req_seqnum); 7758 7759 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7760 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7761 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7762 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7763 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7764 7765 tcp->tcp_lingertime = 0; 7766 7767 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7768 ASSERT(tcp->tcp_urp_mp == NULL); 7769 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7770 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7771 7772 ASSERT(tcp->tcp_eager_next_q == NULL); 7773 ASSERT(tcp->tcp_eager_last_q == NULL); 7774 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7775 tcp->tcp_eager_prev_q0 == NULL) || 7776 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7777 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7778 7779 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7780 tcp->tcp_eager_prev_drop_q0 == NULL) || 7781 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7782 7783 tcp->tcp_client_errno = 0; 7784 7785 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7786 7787 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7788 7789 PRESERVE(tcp->tcp_bound_source_v6); 7790 tcp->tcp_last_sent_len = 0; 7791 tcp->tcp_dupack_cnt = 0; 7792 7793 tcp->tcp_fport = 0; /* Displayed in MIB */ 7794 PRESERVE(tcp->tcp_lport); 7795 7796 PRESERVE(tcp->tcp_acceptor_lockp); 7797 7798 ASSERT(tcp->tcp_ordrel_mp == NULL); 7799 PRESERVE(tcp->tcp_acceptor_id); 7800 DONTCARE(tcp->tcp_ipsec_overhead); 7801 7802 PRESERVE(tcp->tcp_family); 7803 if (tcp->tcp_family == AF_INET6) { 7804 tcp->tcp_ipversion = IPV6_VERSION; 7805 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7806 } else { 7807 tcp->tcp_ipversion = IPV4_VERSION; 7808 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7809 } 7810 7811 tcp->tcp_bound_if = 0; 7812 tcp->tcp_ipv6_recvancillary = 0; 7813 tcp->tcp_recvifindex = 0; 7814 tcp->tcp_recvhops = 0; 7815 tcp->tcp_closed = 0; 7816 tcp->tcp_cleandeathtag = 0; 7817 if (tcp->tcp_hopopts != NULL) { 7818 mi_free(tcp->tcp_hopopts); 7819 tcp->tcp_hopopts = NULL; 7820 tcp->tcp_hopoptslen = 0; 7821 } 7822 ASSERT(tcp->tcp_hopoptslen == 0); 7823 if (tcp->tcp_dstopts != NULL) { 7824 mi_free(tcp->tcp_dstopts); 7825 tcp->tcp_dstopts = NULL; 7826 tcp->tcp_dstoptslen = 0; 7827 } 7828 ASSERT(tcp->tcp_dstoptslen == 0); 7829 if (tcp->tcp_rtdstopts != NULL) { 7830 mi_free(tcp->tcp_rtdstopts); 7831 tcp->tcp_rtdstopts = NULL; 7832 tcp->tcp_rtdstoptslen = 0; 7833 } 7834 ASSERT(tcp->tcp_rtdstoptslen == 0); 7835 if (tcp->tcp_rthdr != NULL) { 7836 mi_free(tcp->tcp_rthdr); 7837 tcp->tcp_rthdr = NULL; 7838 tcp->tcp_rthdrlen = 0; 7839 } 7840 ASSERT(tcp->tcp_rthdrlen == 0); 7841 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7842 7843 /* Reset fusion-related fields */ 7844 tcp->tcp_fused = B_FALSE; 7845 tcp->tcp_unfusable = B_FALSE; 7846 tcp->tcp_fused_sigurg = B_FALSE; 7847 tcp->tcp_direct_sockfs = B_FALSE; 7848 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7849 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7850 tcp->tcp_loopback_peer = NULL; 7851 tcp->tcp_fuse_rcv_hiwater = 0; 7852 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7853 tcp->tcp_fuse_rcv_unread_cnt = 0; 7854 7855 tcp->tcp_lso = B_FALSE; 7856 7857 tcp->tcp_in_ack_unsent = 0; 7858 tcp->tcp_cork = B_FALSE; 7859 tcp->tcp_tconnind_started = B_FALSE; 7860 7861 PRESERVE(tcp->tcp_squeue_bytes); 7862 7863 ASSERT(tcp->tcp_kssl_ctx == NULL); 7864 ASSERT(!tcp->tcp_kssl_pending); 7865 PRESERVE(tcp->tcp_kssl_ent); 7866 7867 /* Sodirect */ 7868 tcp->tcp_sodirect = NULL; 7869 7870 tcp->tcp_closemp_used = B_FALSE; 7871 7872 PRESERVE(tcp->tcp_rsrv_mp); 7873 PRESERVE(tcp->tcp_rsrv_mp_lock); 7874 7875 #ifdef DEBUG 7876 DONTCARE(tcp->tcmp_stk[0]); 7877 #endif 7878 7879 PRESERVE(tcp->tcp_connid); 7880 7881 7882 #undef DONTCARE 7883 #undef PRESERVE 7884 } 7885 7886 /* 7887 * Allocate necessary resources and initialize state vector. 7888 * Guaranteed not to fail so that when an error is returned, 7889 * the caller doesn't need to do any additional cleanup. 7890 */ 7891 int 7892 tcp_init(tcp_t *tcp, queue_t *q) 7893 { 7894 int err; 7895 7896 tcp->tcp_rq = q; 7897 tcp->tcp_wq = WR(q); 7898 tcp->tcp_state = TCPS_IDLE; 7899 if ((err = tcp_init_values(tcp)) != 0) 7900 tcp_timers_stop(tcp); 7901 return (err); 7902 } 7903 7904 static int 7905 tcp_init_values(tcp_t *tcp) 7906 { 7907 int err; 7908 tcp_stack_t *tcps = tcp->tcp_tcps; 7909 7910 ASSERT((tcp->tcp_family == AF_INET && 7911 tcp->tcp_ipversion == IPV4_VERSION) || 7912 (tcp->tcp_family == AF_INET6 && 7913 (tcp->tcp_ipversion == IPV4_VERSION || 7914 tcp->tcp_ipversion == IPV6_VERSION))); 7915 7916 /* 7917 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7918 * will be close to tcp_rexmit_interval_initial. By doing this, we 7919 * allow the algorithm to adjust slowly to large fluctuations of RTT 7920 * during first few transmissions of a connection as seen in slow 7921 * links. 7922 */ 7923 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7924 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7925 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7926 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7927 tcps->tcps_conn_grace_period; 7928 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7929 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7930 tcp->tcp_timer_backoff = 0; 7931 tcp->tcp_ms_we_have_waited = 0; 7932 tcp->tcp_last_recv_time = lbolt; 7933 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7934 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7935 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7936 7937 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7938 7939 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7940 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7941 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7942 /* 7943 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7944 * passive open. 7945 */ 7946 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7947 7948 tcp->tcp_naglim = tcps->tcps_naglim_def; 7949 7950 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7951 7952 tcp->tcp_mdt_hdr_head = 0; 7953 tcp->tcp_mdt_hdr_tail = 0; 7954 7955 /* Reset fusion-related fields */ 7956 tcp->tcp_fused = B_FALSE; 7957 tcp->tcp_unfusable = B_FALSE; 7958 tcp->tcp_fused_sigurg = B_FALSE; 7959 tcp->tcp_direct_sockfs = B_FALSE; 7960 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7961 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7962 tcp->tcp_loopback_peer = NULL; 7963 tcp->tcp_fuse_rcv_hiwater = 0; 7964 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7965 tcp->tcp_fuse_rcv_unread_cnt = 0; 7966 7967 /* Sodirect */ 7968 tcp->tcp_sodirect = NULL; 7969 7970 /* Initialize the header template */ 7971 if (tcp->tcp_ipversion == IPV4_VERSION) { 7972 err = tcp_header_init_ipv4(tcp); 7973 } else { 7974 err = tcp_header_init_ipv6(tcp); 7975 } 7976 if (err) 7977 return (err); 7978 7979 /* 7980 * Init the window scale to the max so tcp_rwnd_set() won't pare 7981 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7982 */ 7983 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7984 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7985 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7986 7987 tcp->tcp_cork = B_FALSE; 7988 /* 7989 * Init the tcp_debug option. This value determines whether TCP 7990 * calls strlog() to print out debug messages. Doing this 7991 * initialization here means that this value is not inherited thru 7992 * tcp_reinit(). 7993 */ 7994 tcp->tcp_debug = tcps->tcps_dbg; 7995 7996 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7997 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7998 7999 return (0); 8000 } 8001 8002 /* 8003 * Initialize the IPv4 header. Loses any record of any IP options. 8004 */ 8005 static int 8006 tcp_header_init_ipv4(tcp_t *tcp) 8007 { 8008 tcph_t *tcph; 8009 uint32_t sum; 8010 conn_t *connp; 8011 tcp_stack_t *tcps = tcp->tcp_tcps; 8012 8013 /* 8014 * This is a simple initialization. If there's 8015 * already a template, it should never be too small, 8016 * so reuse it. Otherwise, allocate space for the new one. 8017 */ 8018 if (tcp->tcp_iphc == NULL) { 8019 ASSERT(tcp->tcp_iphc_len == 0); 8020 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8021 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8022 if (tcp->tcp_iphc == NULL) { 8023 tcp->tcp_iphc_len = 0; 8024 return (ENOMEM); 8025 } 8026 } 8027 8028 /* options are gone; may need a new label */ 8029 connp = tcp->tcp_connp; 8030 connp->conn_mlp_type = mlptSingle; 8031 connp->conn_ulp_labeled = !is_system_labeled(); 8032 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8033 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8034 tcp->tcp_ip6h = NULL; 8035 tcp->tcp_ipversion = IPV4_VERSION; 8036 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8037 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8038 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8039 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8040 tcp->tcp_ipha->ipha_version_and_hdr_length 8041 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8042 tcp->tcp_ipha->ipha_ident = 0; 8043 8044 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8045 tcp->tcp_tos = 0; 8046 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8047 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8048 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8049 8050 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8051 tcp->tcp_tcph = tcph; 8052 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8053 /* 8054 * IP wants our header length in the checksum field to 8055 * allow it to perform a single pseudo-header+checksum 8056 * calculation on behalf of TCP. 8057 * Include the adjustment for a source route once IP_OPTIONS is set. 8058 */ 8059 sum = sizeof (tcph_t) + tcp->tcp_sum; 8060 sum = (sum >> 16) + (sum & 0xFFFF); 8061 U16_TO_ABE16(sum, tcph->th_sum); 8062 return (0); 8063 } 8064 8065 /* 8066 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8067 */ 8068 static int 8069 tcp_header_init_ipv6(tcp_t *tcp) 8070 { 8071 tcph_t *tcph; 8072 uint32_t sum; 8073 conn_t *connp; 8074 tcp_stack_t *tcps = tcp->tcp_tcps; 8075 8076 /* 8077 * This is a simple initialization. If there's 8078 * already a template, it should never be too small, 8079 * so reuse it. Otherwise, allocate space for the new one. 8080 * Ensure that there is enough space to "downgrade" the tcp_t 8081 * to an IPv4 tcp_t. This requires having space for a full load 8082 * of IPv4 options, as well as a full load of TCP options 8083 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8084 * than a v6 header and a TCP header with a full load of TCP options 8085 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8086 * We want to avoid reallocation in the "downgraded" case when 8087 * processing outbound IPv4 options. 8088 */ 8089 if (tcp->tcp_iphc == NULL) { 8090 ASSERT(tcp->tcp_iphc_len == 0); 8091 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8092 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8093 if (tcp->tcp_iphc == NULL) { 8094 tcp->tcp_iphc_len = 0; 8095 return (ENOMEM); 8096 } 8097 } 8098 8099 /* options are gone; may need a new label */ 8100 connp = tcp->tcp_connp; 8101 connp->conn_mlp_type = mlptSingle; 8102 connp->conn_ulp_labeled = !is_system_labeled(); 8103 8104 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8105 tcp->tcp_ipversion = IPV6_VERSION; 8106 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8107 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8108 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8109 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8110 tcp->tcp_ipha = NULL; 8111 8112 /* Initialize the header template */ 8113 8114 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8115 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8116 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8117 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8118 8119 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8120 tcp->tcp_tcph = tcph; 8121 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8122 /* 8123 * IP wants our header length in the checksum field to 8124 * allow it to perform a single psuedo-header+checksum 8125 * calculation on behalf of TCP. 8126 * Include the adjustment for a source route when IPV6_RTHDR is set. 8127 */ 8128 sum = sizeof (tcph_t) + tcp->tcp_sum; 8129 sum = (sum >> 16) + (sum & 0xFFFF); 8130 U16_TO_ABE16(sum, tcph->th_sum); 8131 return (0); 8132 } 8133 8134 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8135 #define ICMP_MIN_TCP_HDR 8 8136 8137 /* 8138 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8139 * passed up by IP. The message is always received on the correct tcp_t. 8140 * Assumes that IP has pulled up everything up to and including the ICMP header. 8141 */ 8142 void 8143 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8144 { 8145 icmph_t *icmph; 8146 ipha_t *ipha; 8147 int iph_hdr_length; 8148 tcph_t *tcph; 8149 boolean_t ipsec_mctl = B_FALSE; 8150 boolean_t secure; 8151 mblk_t *first_mp = mp; 8152 int32_t new_mss; 8153 uint32_t ratio; 8154 size_t mp_size = MBLKL(mp); 8155 uint32_t seg_seq; 8156 tcp_stack_t *tcps = tcp->tcp_tcps; 8157 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8158 8159 /* Assume IP provides aligned packets - otherwise toss */ 8160 if (!OK_32PTR(mp->b_rptr)) { 8161 freemsg(mp); 8162 return; 8163 } 8164 8165 /* 8166 * Since ICMP errors are normal data marked with M_CTL when sent 8167 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8168 * packets starting with an ipsec_info_t, see ipsec_info.h. 8169 */ 8170 if ((mp_size == sizeof (ipsec_info_t)) && 8171 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8172 ASSERT(mp->b_cont != NULL); 8173 mp = mp->b_cont; 8174 /* IP should have done this */ 8175 ASSERT(OK_32PTR(mp->b_rptr)); 8176 mp_size = MBLKL(mp); 8177 ipsec_mctl = B_TRUE; 8178 } 8179 8180 /* 8181 * Verify that we have a complete outer IP header. If not, drop it. 8182 */ 8183 if (mp_size < sizeof (ipha_t)) { 8184 noticmpv4: 8185 freemsg(first_mp); 8186 return; 8187 } 8188 8189 ipha = (ipha_t *)mp->b_rptr; 8190 /* 8191 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8192 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8193 */ 8194 switch (IPH_HDR_VERSION(ipha)) { 8195 case IPV6_VERSION: 8196 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8197 return; 8198 case IPV4_VERSION: 8199 break; 8200 default: 8201 goto noticmpv4; 8202 } 8203 8204 /* Skip past the outer IP and ICMP headers */ 8205 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8206 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8207 /* 8208 * If we don't have the correct outer IP header length or if the ULP 8209 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8210 * send it upstream. 8211 */ 8212 if (iph_hdr_length < sizeof (ipha_t) || 8213 ipha->ipha_protocol != IPPROTO_ICMP || 8214 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8215 goto noticmpv4; 8216 } 8217 ipha = (ipha_t *)&icmph[1]; 8218 8219 /* Skip past the inner IP and find the ULP header */ 8220 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8221 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8222 /* 8223 * If we don't have the correct inner IP header length or if the ULP 8224 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8225 * bytes of TCP header, drop it. 8226 */ 8227 if (iph_hdr_length < sizeof (ipha_t) || 8228 ipha->ipha_protocol != IPPROTO_TCP || 8229 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8230 goto noticmpv4; 8231 } 8232 8233 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8234 if (ipsec_mctl) { 8235 secure = ipsec_in_is_secure(first_mp); 8236 } else { 8237 secure = B_FALSE; 8238 } 8239 if (secure) { 8240 /* 8241 * If we are willing to accept this in clear 8242 * we don't have to verify policy. 8243 */ 8244 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8245 if (!tcp_check_policy(tcp, first_mp, 8246 ipha, NULL, secure, ipsec_mctl)) { 8247 /* 8248 * tcp_check_policy called 8249 * ip_drop_packet() on failure. 8250 */ 8251 return; 8252 } 8253 } 8254 } 8255 } else if (ipsec_mctl) { 8256 /* 8257 * This is a hard_bound connection. IP has already 8258 * verified policy. We don't have to do it again. 8259 */ 8260 freeb(first_mp); 8261 first_mp = mp; 8262 ipsec_mctl = B_FALSE; 8263 } 8264 8265 seg_seq = ABE32_TO_U32(tcph->th_seq); 8266 /* 8267 * TCP SHOULD check that the TCP sequence number contained in 8268 * payload of the ICMP error message is within the range 8269 * SND.UNA <= SEG.SEQ < SND.NXT. 8270 */ 8271 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8272 /* 8273 * The ICMP message is bogus, just drop it. But if this is 8274 * an ICMP too big message, IP has already changed 8275 * the ire_max_frag to the bogus value. We need to change 8276 * it back. 8277 */ 8278 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8279 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8280 conn_t *connp = tcp->tcp_connp; 8281 ire_t *ire; 8282 int flag; 8283 8284 if (tcp->tcp_ipversion == IPV4_VERSION) { 8285 flag = tcp->tcp_ipha-> 8286 ipha_fragment_offset_and_flags; 8287 } else { 8288 flag = 0; 8289 } 8290 mutex_enter(&connp->conn_lock); 8291 if ((ire = connp->conn_ire_cache) != NULL) { 8292 mutex_enter(&ire->ire_lock); 8293 mutex_exit(&connp->conn_lock); 8294 ire->ire_max_frag = tcp->tcp_if_mtu; 8295 ire->ire_frag_flag |= flag; 8296 mutex_exit(&ire->ire_lock); 8297 } else { 8298 mutex_exit(&connp->conn_lock); 8299 } 8300 } 8301 goto noticmpv4; 8302 } 8303 8304 switch (icmph->icmph_type) { 8305 case ICMP_DEST_UNREACHABLE: 8306 switch (icmph->icmph_code) { 8307 case ICMP_FRAGMENTATION_NEEDED: 8308 /* 8309 * Reduce the MSS based on the new MTU. This will 8310 * eliminate any fragmentation locally. 8311 * N.B. There may well be some funny side-effects on 8312 * the local send policy and the remote receive policy. 8313 * Pending further research, we provide 8314 * tcp_ignore_path_mtu just in case this proves 8315 * disastrous somewhere. 8316 * 8317 * After updating the MSS, retransmit part of the 8318 * dropped segment using the new mss by calling 8319 * tcp_wput_data(). Need to adjust all those 8320 * params to make sure tcp_wput_data() work properly. 8321 */ 8322 if (tcps->tcps_ignore_path_mtu || 8323 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8324 break; 8325 8326 /* 8327 * Decrease the MSS by time stamp options 8328 * IP options and IPSEC options. tcp_hdr_len 8329 * includes time stamp option and IP option 8330 * length. Note that new_mss may be negative 8331 * if tcp_ipsec_overhead is large and the 8332 * icmph_du_mtu is the minimum value, which is 68. 8333 */ 8334 new_mss = ntohs(icmph->icmph_du_mtu) - 8335 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8336 8337 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8338 new_mss); 8339 8340 /* 8341 * Only update the MSS if the new one is 8342 * smaller than the previous one. This is 8343 * to avoid problems when getting multiple 8344 * ICMP errors for the same MTU. 8345 */ 8346 if (new_mss >= tcp->tcp_mss) 8347 break; 8348 8349 /* 8350 * Note that we are using the template header's DF 8351 * bit in the fast path sending. So we need to compare 8352 * the new mss with both tcps_mss_min and ip_pmtu_min. 8353 * And stop doing IPv4 PMTUd if new_mss is less than 8354 * MAX(tcps_mss_min, ip_pmtu_min). 8355 */ 8356 if (new_mss < tcps->tcps_mss_min || 8357 new_mss < ipst->ips_ip_pmtu_min) { 8358 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8359 0; 8360 } 8361 8362 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8363 ASSERT(ratio >= 1); 8364 tcp_mss_set(tcp, new_mss, B_TRUE); 8365 8366 /* 8367 * Make sure we have something to 8368 * send. 8369 */ 8370 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8371 (tcp->tcp_xmit_head != NULL)) { 8372 /* 8373 * Shrink tcp_cwnd in 8374 * proportion to the old MSS/new MSS. 8375 */ 8376 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8377 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8378 (tcp->tcp_unsent == 0)) { 8379 tcp->tcp_rexmit_max = tcp->tcp_fss; 8380 } else { 8381 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8382 } 8383 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8384 tcp->tcp_rexmit = B_TRUE; 8385 tcp->tcp_dupack_cnt = 0; 8386 tcp->tcp_snd_burst = TCP_CWND_SS; 8387 tcp_ss_rexmit(tcp); 8388 } 8389 break; 8390 case ICMP_PORT_UNREACHABLE: 8391 case ICMP_PROTOCOL_UNREACHABLE: 8392 switch (tcp->tcp_state) { 8393 case TCPS_SYN_SENT: 8394 case TCPS_SYN_RCVD: 8395 /* 8396 * ICMP can snipe away incipient 8397 * TCP connections as long as 8398 * seq number is same as initial 8399 * send seq number. 8400 */ 8401 if (seg_seq == tcp->tcp_iss) { 8402 (void) tcp_clean_death(tcp, 8403 ECONNREFUSED, 6); 8404 } 8405 break; 8406 } 8407 break; 8408 case ICMP_HOST_UNREACHABLE: 8409 case ICMP_NET_UNREACHABLE: 8410 /* Record the error in case we finally time out. */ 8411 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8412 tcp->tcp_client_errno = EHOSTUNREACH; 8413 else 8414 tcp->tcp_client_errno = ENETUNREACH; 8415 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8416 if (tcp->tcp_listener != NULL && 8417 tcp->tcp_listener->tcp_syn_defense) { 8418 /* 8419 * Ditch the half-open connection if we 8420 * suspect a SYN attack is under way. 8421 */ 8422 tcp_ip_ire_mark_advice(tcp); 8423 (void) tcp_clean_death(tcp, 8424 tcp->tcp_client_errno, 7); 8425 } 8426 } 8427 break; 8428 default: 8429 break; 8430 } 8431 break; 8432 case ICMP_SOURCE_QUENCH: { 8433 /* 8434 * use a global boolean to control 8435 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8436 * The default is false. 8437 */ 8438 if (tcp_icmp_source_quench) { 8439 /* 8440 * Reduce the sending rate as if we got a 8441 * retransmit timeout 8442 */ 8443 uint32_t npkt; 8444 8445 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8446 tcp->tcp_mss; 8447 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8448 tcp->tcp_cwnd = tcp->tcp_mss; 8449 tcp->tcp_cwnd_cnt = 0; 8450 } 8451 break; 8452 } 8453 } 8454 freemsg(first_mp); 8455 } 8456 8457 /* 8458 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8459 * error messages passed up by IP. 8460 * Assumes that IP has pulled up all the extension headers as well 8461 * as the ICMPv6 header. 8462 */ 8463 static void 8464 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8465 { 8466 icmp6_t *icmp6; 8467 ip6_t *ip6h; 8468 uint16_t iph_hdr_length; 8469 tcpha_t *tcpha; 8470 uint8_t *nexthdrp; 8471 uint32_t new_mss; 8472 uint32_t ratio; 8473 boolean_t secure; 8474 mblk_t *first_mp = mp; 8475 size_t mp_size; 8476 uint32_t seg_seq; 8477 tcp_stack_t *tcps = tcp->tcp_tcps; 8478 8479 /* 8480 * The caller has determined if this is an IPSEC_IN packet and 8481 * set ipsec_mctl appropriately (see tcp_icmp_error). 8482 */ 8483 if (ipsec_mctl) 8484 mp = mp->b_cont; 8485 8486 mp_size = MBLKL(mp); 8487 8488 /* 8489 * Verify that we have a complete IP header. If not, send it upstream. 8490 */ 8491 if (mp_size < sizeof (ip6_t)) { 8492 noticmpv6: 8493 freemsg(first_mp); 8494 return; 8495 } 8496 8497 /* 8498 * Verify this is an ICMPV6 packet, else send it upstream. 8499 */ 8500 ip6h = (ip6_t *)mp->b_rptr; 8501 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8502 iph_hdr_length = IPV6_HDR_LEN; 8503 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8504 &nexthdrp) || 8505 *nexthdrp != IPPROTO_ICMPV6) { 8506 goto noticmpv6; 8507 } 8508 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8509 ip6h = (ip6_t *)&icmp6[1]; 8510 /* 8511 * Verify if we have a complete ICMP and inner IP header. 8512 */ 8513 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8514 goto noticmpv6; 8515 8516 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8517 goto noticmpv6; 8518 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8519 /* 8520 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8521 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8522 * packet. 8523 */ 8524 if ((*nexthdrp != IPPROTO_TCP) || 8525 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8526 goto noticmpv6; 8527 } 8528 8529 /* 8530 * ICMP errors come on the right queue or come on 8531 * listener/global queue for detached connections and 8532 * get switched to the right queue. If it comes on the 8533 * right queue, policy check has already been done by IP 8534 * and thus free the first_mp without verifying the policy. 8535 * If it has come for a non-hard bound connection, we need 8536 * to verify policy as IP may not have done it. 8537 */ 8538 if (!tcp->tcp_hard_bound) { 8539 if (ipsec_mctl) { 8540 secure = ipsec_in_is_secure(first_mp); 8541 } else { 8542 secure = B_FALSE; 8543 } 8544 if (secure) { 8545 /* 8546 * If we are willing to accept this in clear 8547 * we don't have to verify policy. 8548 */ 8549 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8550 if (!tcp_check_policy(tcp, first_mp, 8551 NULL, ip6h, secure, ipsec_mctl)) { 8552 /* 8553 * tcp_check_policy called 8554 * ip_drop_packet() on failure. 8555 */ 8556 return; 8557 } 8558 } 8559 } 8560 } else if (ipsec_mctl) { 8561 /* 8562 * This is a hard_bound connection. IP has already 8563 * verified policy. We don't have to do it again. 8564 */ 8565 freeb(first_mp); 8566 first_mp = mp; 8567 ipsec_mctl = B_FALSE; 8568 } 8569 8570 seg_seq = ntohl(tcpha->tha_seq); 8571 /* 8572 * TCP SHOULD check that the TCP sequence number contained in 8573 * payload of the ICMP error message is within the range 8574 * SND.UNA <= SEG.SEQ < SND.NXT. 8575 */ 8576 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8577 /* 8578 * If the ICMP message is bogus, should we kill the 8579 * connection, or should we just drop the bogus ICMP 8580 * message? It would probably make more sense to just 8581 * drop the message so that if this one managed to get 8582 * in, the real connection should not suffer. 8583 */ 8584 goto noticmpv6; 8585 } 8586 8587 switch (icmp6->icmp6_type) { 8588 case ICMP6_PACKET_TOO_BIG: 8589 /* 8590 * Reduce the MSS based on the new MTU. This will 8591 * eliminate any fragmentation locally. 8592 * N.B. There may well be some funny side-effects on 8593 * the local send policy and the remote receive policy. 8594 * Pending further research, we provide 8595 * tcp_ignore_path_mtu just in case this proves 8596 * disastrous somewhere. 8597 * 8598 * After updating the MSS, retransmit part of the 8599 * dropped segment using the new mss by calling 8600 * tcp_wput_data(). Need to adjust all those 8601 * params to make sure tcp_wput_data() work properly. 8602 */ 8603 if (tcps->tcps_ignore_path_mtu) 8604 break; 8605 8606 /* 8607 * Decrease the MSS by time stamp options 8608 * IP options and IPSEC options. tcp_hdr_len 8609 * includes time stamp option and IP option 8610 * length. 8611 */ 8612 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8613 tcp->tcp_ipsec_overhead; 8614 8615 /* 8616 * Only update the MSS if the new one is 8617 * smaller than the previous one. This is 8618 * to avoid problems when getting multiple 8619 * ICMP errors for the same MTU. 8620 */ 8621 if (new_mss >= tcp->tcp_mss) 8622 break; 8623 8624 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8625 ASSERT(ratio >= 1); 8626 tcp_mss_set(tcp, new_mss, B_TRUE); 8627 8628 /* 8629 * Make sure we have something to 8630 * send. 8631 */ 8632 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8633 (tcp->tcp_xmit_head != NULL)) { 8634 /* 8635 * Shrink tcp_cwnd in 8636 * proportion to the old MSS/new MSS. 8637 */ 8638 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8639 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8640 (tcp->tcp_unsent == 0)) { 8641 tcp->tcp_rexmit_max = tcp->tcp_fss; 8642 } else { 8643 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8644 } 8645 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8646 tcp->tcp_rexmit = B_TRUE; 8647 tcp->tcp_dupack_cnt = 0; 8648 tcp->tcp_snd_burst = TCP_CWND_SS; 8649 tcp_ss_rexmit(tcp); 8650 } 8651 break; 8652 8653 case ICMP6_DST_UNREACH: 8654 switch (icmp6->icmp6_code) { 8655 case ICMP6_DST_UNREACH_NOPORT: 8656 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8657 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8658 (seg_seq == tcp->tcp_iss)) { 8659 (void) tcp_clean_death(tcp, 8660 ECONNREFUSED, 8); 8661 } 8662 break; 8663 8664 case ICMP6_DST_UNREACH_ADMIN: 8665 case ICMP6_DST_UNREACH_NOROUTE: 8666 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8667 case ICMP6_DST_UNREACH_ADDR: 8668 /* Record the error in case we finally time out. */ 8669 tcp->tcp_client_errno = EHOSTUNREACH; 8670 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8671 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8672 (seg_seq == tcp->tcp_iss)) { 8673 if (tcp->tcp_listener != NULL && 8674 tcp->tcp_listener->tcp_syn_defense) { 8675 /* 8676 * Ditch the half-open connection if we 8677 * suspect a SYN attack is under way. 8678 */ 8679 tcp_ip_ire_mark_advice(tcp); 8680 (void) tcp_clean_death(tcp, 8681 tcp->tcp_client_errno, 9); 8682 } 8683 } 8684 8685 8686 break; 8687 default: 8688 break; 8689 } 8690 break; 8691 8692 case ICMP6_PARAM_PROB: 8693 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8694 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8695 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8696 (uchar_t *)nexthdrp) { 8697 if (tcp->tcp_state == TCPS_SYN_SENT || 8698 tcp->tcp_state == TCPS_SYN_RCVD) { 8699 (void) tcp_clean_death(tcp, 8700 ECONNREFUSED, 10); 8701 } 8702 break; 8703 } 8704 break; 8705 8706 case ICMP6_TIME_EXCEEDED: 8707 default: 8708 break; 8709 } 8710 freemsg(first_mp); 8711 } 8712 8713 /* 8714 * Notify IP that we are having trouble with this connection. IP should 8715 * blow the IRE away and start over. 8716 */ 8717 static void 8718 tcp_ip_notify(tcp_t *tcp) 8719 { 8720 struct iocblk *iocp; 8721 ipid_t *ipid; 8722 mblk_t *mp; 8723 8724 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8725 if (tcp->tcp_ipversion == IPV6_VERSION) 8726 return; 8727 8728 mp = mkiocb(IP_IOCTL); 8729 if (mp == NULL) 8730 return; 8731 8732 iocp = (struct iocblk *)mp->b_rptr; 8733 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8734 8735 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8736 if (!mp->b_cont) { 8737 freeb(mp); 8738 return; 8739 } 8740 8741 ipid = (ipid_t *)mp->b_cont->b_rptr; 8742 mp->b_cont->b_wptr += iocp->ioc_count; 8743 bzero(ipid, sizeof (*ipid)); 8744 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8745 ipid->ipid_ire_type = IRE_CACHE; 8746 ipid->ipid_addr_offset = sizeof (ipid_t); 8747 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8748 /* 8749 * Note: in the case of source routing we want to blow away the 8750 * route to the first source route hop. 8751 */ 8752 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8753 sizeof (tcp->tcp_ipha->ipha_dst)); 8754 8755 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8756 } 8757 8758 /* Unlink and return any mblk that looks like it contains an ire */ 8759 static mblk_t * 8760 tcp_ire_mp(mblk_t **mpp) 8761 { 8762 mblk_t *mp = *mpp; 8763 mblk_t *prev_mp = NULL; 8764 8765 for (;;) { 8766 switch (DB_TYPE(mp)) { 8767 case IRE_DB_TYPE: 8768 case IRE_DB_REQ_TYPE: 8769 if (mp == *mpp) { 8770 *mpp = mp->b_cont; 8771 } else { 8772 prev_mp->b_cont = mp->b_cont; 8773 } 8774 mp->b_cont = NULL; 8775 return (mp); 8776 default: 8777 break; 8778 } 8779 prev_mp = mp; 8780 mp = mp->b_cont; 8781 if (mp == NULL) 8782 break; 8783 } 8784 return (mp); 8785 } 8786 8787 /* 8788 * Timer callback routine for keepalive probe. We do a fake resend of 8789 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8790 * check to see if we have heard anything from the other end for the last 8791 * RTO period. If we have, set the timer to expire for another 8792 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8793 * RTO << 1 and check again when it expires. Keep exponentially increasing 8794 * the timeout if we have not heard from the other side. If for more than 8795 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8796 * kill the connection unless the keepalive abort threshold is 0. In 8797 * that case, we will probe "forever." 8798 */ 8799 static void 8800 tcp_keepalive_killer(void *arg) 8801 { 8802 mblk_t *mp; 8803 conn_t *connp = (conn_t *)arg; 8804 tcp_t *tcp = connp->conn_tcp; 8805 int32_t firetime; 8806 int32_t idletime; 8807 int32_t ka_intrvl; 8808 tcp_stack_t *tcps = tcp->tcp_tcps; 8809 8810 tcp->tcp_ka_tid = 0; 8811 8812 if (tcp->tcp_fused) 8813 return; 8814 8815 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8816 ka_intrvl = tcp->tcp_ka_interval; 8817 8818 /* 8819 * Keepalive probe should only be sent if the application has not 8820 * done a close on the connection. 8821 */ 8822 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8823 return; 8824 } 8825 /* Timer fired too early, restart it. */ 8826 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8827 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8828 MSEC_TO_TICK(ka_intrvl)); 8829 return; 8830 } 8831 8832 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8833 /* 8834 * If we have not heard from the other side for a long 8835 * time, kill the connection unless the keepalive abort 8836 * threshold is 0. In that case, we will probe "forever." 8837 */ 8838 if (tcp->tcp_ka_abort_thres != 0 && 8839 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8840 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8841 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8842 tcp->tcp_client_errno : ETIMEDOUT, 11); 8843 return; 8844 } 8845 8846 if (tcp->tcp_snxt == tcp->tcp_suna && 8847 idletime >= ka_intrvl) { 8848 /* Fake resend of last ACKed byte. */ 8849 mblk_t *mp1 = allocb(1, BPRI_LO); 8850 8851 if (mp1 != NULL) { 8852 *mp1->b_wptr++ = '\0'; 8853 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8854 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8855 freeb(mp1); 8856 /* 8857 * if allocation failed, fall through to start the 8858 * timer back. 8859 */ 8860 if (mp != NULL) { 8861 tcp_send_data(tcp, tcp->tcp_wq, mp); 8862 BUMP_MIB(&tcps->tcps_mib, 8863 tcpTimKeepaliveProbe); 8864 if (tcp->tcp_ka_last_intrvl != 0) { 8865 int max; 8866 /* 8867 * We should probe again at least 8868 * in ka_intrvl, but not more than 8869 * tcp_rexmit_interval_max. 8870 */ 8871 max = tcps->tcps_rexmit_interval_max; 8872 firetime = MIN(ka_intrvl - 1, 8873 tcp->tcp_ka_last_intrvl << 1); 8874 if (firetime > max) 8875 firetime = max; 8876 } else { 8877 firetime = tcp->tcp_rto; 8878 } 8879 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8880 tcp_keepalive_killer, 8881 MSEC_TO_TICK(firetime)); 8882 tcp->tcp_ka_last_intrvl = firetime; 8883 return; 8884 } 8885 } 8886 } else { 8887 tcp->tcp_ka_last_intrvl = 0; 8888 } 8889 8890 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8891 if ((firetime = ka_intrvl - idletime) < 0) { 8892 firetime = ka_intrvl; 8893 } 8894 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8895 MSEC_TO_TICK(firetime)); 8896 } 8897 8898 int 8899 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8900 { 8901 queue_t *q = tcp->tcp_rq; 8902 int32_t mss = tcp->tcp_mss; 8903 int maxpsz; 8904 conn_t *connp = tcp->tcp_connp; 8905 8906 if (TCP_IS_DETACHED(tcp)) 8907 return (mss); 8908 if (tcp->tcp_fused) { 8909 maxpsz = tcp_fuse_maxpsz_set(tcp); 8910 mss = INFPSZ; 8911 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8912 /* 8913 * Set the sd_qn_maxpsz according to the socket send buffer 8914 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8915 * instruct the stream head to copyin user data into contiguous 8916 * kernel-allocated buffers without breaking it up into smaller 8917 * chunks. We round up the buffer size to the nearest SMSS. 8918 */ 8919 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8920 if (tcp->tcp_kssl_ctx == NULL) 8921 mss = INFPSZ; 8922 else 8923 mss = SSL3_MAX_RECORD_LEN; 8924 } else { 8925 /* 8926 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8927 * (and a multiple of the mss). This instructs the stream 8928 * head to break down larger than SMSS writes into SMSS- 8929 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8930 */ 8931 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8932 maxpsz = tcp->tcp_maxpsz * mss; 8933 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8934 maxpsz = tcp->tcp_xmit_hiwater/2; 8935 /* Round up to nearest mss */ 8936 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8937 } 8938 } 8939 8940 (void) proto_set_maxpsz(q, connp, maxpsz); 8941 if (!(IPCL_IS_NONSTR(connp))) { 8942 /* XXX do it in set_maxpsz()? */ 8943 tcp->tcp_wq->q_maxpsz = maxpsz; 8944 } 8945 8946 if (set_maxblk) 8947 (void) proto_set_tx_maxblk(q, connp, mss); 8948 return (mss); 8949 } 8950 8951 /* 8952 * Extract option values from a tcp header. We put any found values into the 8953 * tcpopt struct and return a bitmask saying which options were found. 8954 */ 8955 static int 8956 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8957 { 8958 uchar_t *endp; 8959 int len; 8960 uint32_t mss; 8961 uchar_t *up = (uchar_t *)tcph; 8962 int found = 0; 8963 int32_t sack_len; 8964 tcp_seq sack_begin, sack_end; 8965 tcp_t *tcp; 8966 8967 endp = up + TCP_HDR_LENGTH(tcph); 8968 up += TCP_MIN_HEADER_LENGTH; 8969 while (up < endp) { 8970 len = endp - up; 8971 switch (*up) { 8972 case TCPOPT_EOL: 8973 break; 8974 8975 case TCPOPT_NOP: 8976 up++; 8977 continue; 8978 8979 case TCPOPT_MAXSEG: 8980 if (len < TCPOPT_MAXSEG_LEN || 8981 up[1] != TCPOPT_MAXSEG_LEN) 8982 break; 8983 8984 mss = BE16_TO_U16(up+2); 8985 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8986 tcpopt->tcp_opt_mss = mss; 8987 found |= TCP_OPT_MSS_PRESENT; 8988 8989 up += TCPOPT_MAXSEG_LEN; 8990 continue; 8991 8992 case TCPOPT_WSCALE: 8993 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8994 break; 8995 8996 if (up[2] > TCP_MAX_WINSHIFT) 8997 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8998 else 8999 tcpopt->tcp_opt_wscale = up[2]; 9000 found |= TCP_OPT_WSCALE_PRESENT; 9001 9002 up += TCPOPT_WS_LEN; 9003 continue; 9004 9005 case TCPOPT_SACK_PERMITTED: 9006 if (len < TCPOPT_SACK_OK_LEN || 9007 up[1] != TCPOPT_SACK_OK_LEN) 9008 break; 9009 found |= TCP_OPT_SACK_OK_PRESENT; 9010 up += TCPOPT_SACK_OK_LEN; 9011 continue; 9012 9013 case TCPOPT_SACK: 9014 if (len <= 2 || up[1] <= 2 || len < up[1]) 9015 break; 9016 9017 /* If TCP is not interested in SACK blks... */ 9018 if ((tcp = tcpopt->tcp) == NULL) { 9019 up += up[1]; 9020 continue; 9021 } 9022 sack_len = up[1] - TCPOPT_HEADER_LEN; 9023 up += TCPOPT_HEADER_LEN; 9024 9025 /* 9026 * If the list is empty, allocate one and assume 9027 * nothing is sack'ed. 9028 */ 9029 ASSERT(tcp->tcp_sack_info != NULL); 9030 if (tcp->tcp_notsack_list == NULL) { 9031 tcp_notsack_update(&(tcp->tcp_notsack_list), 9032 tcp->tcp_suna, tcp->tcp_snxt, 9033 &(tcp->tcp_num_notsack_blk), 9034 &(tcp->tcp_cnt_notsack_list)); 9035 9036 /* 9037 * Make sure tcp_notsack_list is not NULL. 9038 * This happens when kmem_alloc(KM_NOSLEEP) 9039 * returns NULL. 9040 */ 9041 if (tcp->tcp_notsack_list == NULL) { 9042 up += sack_len; 9043 continue; 9044 } 9045 tcp->tcp_fack = tcp->tcp_suna; 9046 } 9047 9048 while (sack_len > 0) { 9049 if (up + 8 > endp) { 9050 up = endp; 9051 break; 9052 } 9053 sack_begin = BE32_TO_U32(up); 9054 up += 4; 9055 sack_end = BE32_TO_U32(up); 9056 up += 4; 9057 sack_len -= 8; 9058 /* 9059 * Bounds checking. Make sure the SACK 9060 * info is within tcp_suna and tcp_snxt. 9061 * If this SACK blk is out of bound, ignore 9062 * it but continue to parse the following 9063 * blks. 9064 */ 9065 if (SEQ_LEQ(sack_end, sack_begin) || 9066 SEQ_LT(sack_begin, tcp->tcp_suna) || 9067 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9068 continue; 9069 } 9070 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9071 sack_begin, sack_end, 9072 &(tcp->tcp_num_notsack_blk), 9073 &(tcp->tcp_cnt_notsack_list)); 9074 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9075 tcp->tcp_fack = sack_end; 9076 } 9077 } 9078 found |= TCP_OPT_SACK_PRESENT; 9079 continue; 9080 9081 case TCPOPT_TSTAMP: 9082 if (len < TCPOPT_TSTAMP_LEN || 9083 up[1] != TCPOPT_TSTAMP_LEN) 9084 break; 9085 9086 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9087 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9088 9089 found |= TCP_OPT_TSTAMP_PRESENT; 9090 9091 up += TCPOPT_TSTAMP_LEN; 9092 continue; 9093 9094 default: 9095 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9096 break; 9097 up += up[1]; 9098 continue; 9099 } 9100 break; 9101 } 9102 return (found); 9103 } 9104 9105 /* 9106 * Set the mss associated with a particular tcp based on its current value, 9107 * and a new one passed in. Observe minimums and maximums, and reset 9108 * other state variables that we want to view as multiples of mss. 9109 * 9110 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9111 * highwater marks etc. need to be initialized or adjusted. 9112 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9113 * packet arrives. 9114 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9115 * ICMP6_PACKET_TOO_BIG arrives. 9116 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9117 * to increase the MSS to use the extra bytes available. 9118 * 9119 * Callers except tcp_paws_check() ensure that they only reduce mss. 9120 */ 9121 static void 9122 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9123 { 9124 uint32_t mss_max; 9125 tcp_stack_t *tcps = tcp->tcp_tcps; 9126 9127 if (tcp->tcp_ipversion == IPV4_VERSION) 9128 mss_max = tcps->tcps_mss_max_ipv4; 9129 else 9130 mss_max = tcps->tcps_mss_max_ipv6; 9131 9132 if (mss < tcps->tcps_mss_min) 9133 mss = tcps->tcps_mss_min; 9134 if (mss > mss_max) 9135 mss = mss_max; 9136 /* 9137 * Unless naglim has been set by our client to 9138 * a non-mss value, force naglim to track mss. 9139 * This can help to aggregate small writes. 9140 */ 9141 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9142 tcp->tcp_naglim = mss; 9143 /* 9144 * TCP should be able to buffer at least 4 MSS data for obvious 9145 * performance reason. 9146 */ 9147 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9148 tcp->tcp_xmit_hiwater = mss << 2; 9149 9150 if (do_ss) { 9151 /* 9152 * Either the tcp_cwnd is as yet uninitialized, or mss is 9153 * changing due to a reduction in MTU, presumably as a 9154 * result of a new path component, reset cwnd to its 9155 * "initial" value, as a multiple of the new mss. 9156 */ 9157 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9158 } else { 9159 /* 9160 * Called by tcp_paws_check(), the mss increased 9161 * marginally to allow use of space previously taken 9162 * by the timestamp option. It would be inappropriate 9163 * to apply slow start or tcp_init_cwnd values to 9164 * tcp_cwnd, simply adjust to a multiple of the new mss. 9165 */ 9166 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9167 tcp->tcp_cwnd_cnt = 0; 9168 } 9169 tcp->tcp_mss = mss; 9170 (void) tcp_maxpsz_set(tcp, B_TRUE); 9171 } 9172 9173 /* For /dev/tcp aka AF_INET open */ 9174 static int 9175 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9176 { 9177 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9178 } 9179 9180 /* For /dev/tcp6 aka AF_INET6 open */ 9181 static int 9182 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9183 { 9184 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9185 } 9186 9187 static conn_t * 9188 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9189 boolean_t issocket, int *errorp) 9190 { 9191 tcp_t *tcp = NULL; 9192 conn_t *connp; 9193 int err; 9194 zoneid_t zoneid; 9195 tcp_stack_t *tcps; 9196 squeue_t *sqp; 9197 9198 ASSERT(errorp != NULL); 9199 /* 9200 * Find the proper zoneid and netstack. 9201 */ 9202 /* 9203 * Special case for install: miniroot needs to be able to 9204 * access files via NFS as though it were always in the 9205 * global zone. 9206 */ 9207 if (credp == kcred && nfs_global_client_only != 0) { 9208 zoneid = GLOBAL_ZONEID; 9209 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9210 netstack_tcp; 9211 ASSERT(tcps != NULL); 9212 } else { 9213 netstack_t *ns; 9214 9215 ns = netstack_find_by_cred(credp); 9216 ASSERT(ns != NULL); 9217 tcps = ns->netstack_tcp; 9218 ASSERT(tcps != NULL); 9219 9220 /* 9221 * For exclusive stacks we set the zoneid to zero 9222 * to make TCP operate as if in the global zone. 9223 */ 9224 if (tcps->tcps_netstack->netstack_stackid != 9225 GLOBAL_NETSTACKID) 9226 zoneid = GLOBAL_ZONEID; 9227 else 9228 zoneid = crgetzoneid(credp); 9229 } 9230 /* 9231 * For stackid zero this is done from strplumb.c, but 9232 * non-zero stackids are handled here. 9233 */ 9234 if (tcps->tcps_g_q == NULL && 9235 tcps->tcps_netstack->netstack_stackid != 9236 GLOBAL_NETSTACKID) { 9237 tcp_g_q_setup(tcps); 9238 } 9239 9240 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9241 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9242 /* 9243 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9244 * so we drop it by one. 9245 */ 9246 netstack_rele(tcps->tcps_netstack); 9247 if (connp == NULL) { 9248 *errorp = ENOSR; 9249 return (NULL); 9250 } 9251 connp->conn_sqp = sqp; 9252 connp->conn_initial_sqp = connp->conn_sqp; 9253 tcp = connp->conn_tcp; 9254 9255 if (isv6) { 9256 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9257 connp->conn_send = ip_output_v6; 9258 connp->conn_af_isv6 = B_TRUE; 9259 connp->conn_pkt_isv6 = B_TRUE; 9260 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9261 tcp->tcp_ipversion = IPV6_VERSION; 9262 tcp->tcp_family = AF_INET6; 9263 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9264 } else { 9265 connp->conn_flags |= IPCL_TCP4; 9266 connp->conn_send = ip_output; 9267 connp->conn_af_isv6 = B_FALSE; 9268 connp->conn_pkt_isv6 = B_FALSE; 9269 tcp->tcp_ipversion = IPV4_VERSION; 9270 tcp->tcp_family = AF_INET; 9271 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9272 } 9273 9274 /* 9275 * TCP keeps a copy of cred for cache locality reasons but 9276 * we put a reference only once. If connp->conn_cred 9277 * becomes invalid, tcp_cred should also be set to NULL. 9278 */ 9279 tcp->tcp_cred = connp->conn_cred = credp; 9280 crhold(connp->conn_cred); 9281 tcp->tcp_cpid = curproc->p_pid; 9282 tcp->tcp_open_time = lbolt64; 9283 connp->conn_zoneid = zoneid; 9284 connp->conn_mlp_type = mlptSingle; 9285 connp->conn_ulp_labeled = !is_system_labeled(); 9286 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9287 ASSERT(tcp->tcp_tcps == tcps); 9288 9289 /* 9290 * If the caller has the process-wide flag set, then default to MAC 9291 * exempt mode. This allows read-down to unlabeled hosts. 9292 */ 9293 if (getpflags(NET_MAC_AWARE, credp) != 0) 9294 connp->conn_mac_exempt = B_TRUE; 9295 9296 connp->conn_dev = NULL; 9297 if (issocket) { 9298 connp->conn_flags |= IPCL_SOCKET; 9299 tcp->tcp_issocket = 1; 9300 } 9301 9302 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9303 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9304 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9305 9306 /* Non-zero default values */ 9307 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9308 9309 if (q == NULL) { 9310 /* 9311 * Create a helper stream for non-STREAMS socket. 9312 */ 9313 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9314 if (err != 0) { 9315 ip1dbg(("tcp_create_common: create of IP helper stream " 9316 "failed\n")); 9317 CONN_DEC_REF(connp); 9318 *errorp = err; 9319 return (NULL); 9320 } 9321 q = connp->conn_rq; 9322 } else { 9323 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9324 } 9325 9326 SOCK_CONNID_INIT(tcp->tcp_connid); 9327 err = tcp_init(tcp, q); 9328 if (err != 0) { 9329 CONN_DEC_REF(connp); 9330 *errorp = err; 9331 return (NULL); 9332 } 9333 9334 return (connp); 9335 } 9336 9337 static int 9338 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9339 boolean_t isv6) 9340 { 9341 tcp_t *tcp = NULL; 9342 conn_t *connp = NULL; 9343 int err; 9344 vmem_t *minor_arena = NULL; 9345 dev_t conn_dev; 9346 boolean_t issocket; 9347 9348 if (q->q_ptr != NULL) 9349 return (0); 9350 9351 if (sflag == MODOPEN) 9352 return (EINVAL); 9353 9354 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9355 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9356 minor_arena = ip_minor_arena_la; 9357 } else { 9358 /* 9359 * Either minor numbers in the large arena were exhausted 9360 * or a non socket application is doing the open. 9361 * Try to allocate from the small arena. 9362 */ 9363 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9364 return (EBUSY); 9365 } 9366 minor_arena = ip_minor_arena_sa; 9367 } 9368 9369 ASSERT(minor_arena != NULL); 9370 9371 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9372 9373 if (flag & SO_FALLBACK) { 9374 /* 9375 * Non streams socket needs a stream to fallback to 9376 */ 9377 RD(q)->q_ptr = (void *)conn_dev; 9378 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9379 WR(q)->q_ptr = (void *)minor_arena; 9380 qprocson(q); 9381 return (0); 9382 } else if (flag & SO_ACCEPTOR) { 9383 q->q_qinfo = &tcp_acceptor_rinit; 9384 /* 9385 * the conn_dev and minor_arena will be subsequently used by 9386 * tcp_wput_accept() and tcpclose_accept() to figure out the 9387 * minor device number for this connection from the q_ptr. 9388 */ 9389 RD(q)->q_ptr = (void *)conn_dev; 9390 WR(q)->q_qinfo = &tcp_acceptor_winit; 9391 WR(q)->q_ptr = (void *)minor_arena; 9392 qprocson(q); 9393 return (0); 9394 } 9395 9396 issocket = flag & SO_SOCKSTR; 9397 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9398 9399 if (connp == NULL) { 9400 inet_minor_free(minor_arena, conn_dev); 9401 q->q_ptr = WR(q)->q_ptr = NULL; 9402 return (err); 9403 } 9404 9405 q->q_ptr = WR(q)->q_ptr = connp; 9406 9407 connp->conn_dev = conn_dev; 9408 connp->conn_minor_arena = minor_arena; 9409 9410 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9411 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9412 9413 if (issocket) { 9414 WR(q)->q_qinfo = &tcp_sock_winit; 9415 } else { 9416 tcp = connp->conn_tcp; 9417 #ifdef _ILP32 9418 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9419 #else 9420 tcp->tcp_acceptor_id = conn_dev; 9421 #endif /* _ILP32 */ 9422 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9423 } 9424 9425 /* 9426 * Put the ref for TCP. Ref for IP was already put 9427 * by ipcl_conn_create. Also Make the conn_t globally 9428 * visible to walkers 9429 */ 9430 mutex_enter(&connp->conn_lock); 9431 CONN_INC_REF_LOCKED(connp); 9432 ASSERT(connp->conn_ref == 2); 9433 connp->conn_state_flags &= ~CONN_INCIPIENT; 9434 mutex_exit(&connp->conn_lock); 9435 9436 qprocson(q); 9437 return (0); 9438 } 9439 9440 /* 9441 * Some TCP options can be "set" by requesting them in the option 9442 * buffer. This is needed for XTI feature test though we do not 9443 * allow it in general. We interpret that this mechanism is more 9444 * applicable to OSI protocols and need not be allowed in general. 9445 * This routine filters out options for which it is not allowed (most) 9446 * and lets through those (few) for which it is. [ The XTI interface 9447 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9448 * ever implemented will have to be allowed here ]. 9449 */ 9450 static boolean_t 9451 tcp_allow_connopt_set(int level, int name) 9452 { 9453 9454 switch (level) { 9455 case IPPROTO_TCP: 9456 switch (name) { 9457 case TCP_NODELAY: 9458 return (B_TRUE); 9459 default: 9460 return (B_FALSE); 9461 } 9462 /*NOTREACHED*/ 9463 default: 9464 return (B_FALSE); 9465 } 9466 /*NOTREACHED*/ 9467 } 9468 9469 /* 9470 * this routine gets default values of certain options whose default 9471 * values are maintained by protocol specific code 9472 */ 9473 /* ARGSUSED */ 9474 int 9475 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9476 { 9477 int32_t *i1 = (int32_t *)ptr; 9478 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9479 9480 switch (level) { 9481 case IPPROTO_TCP: 9482 switch (name) { 9483 case TCP_NOTIFY_THRESHOLD: 9484 *i1 = tcps->tcps_ip_notify_interval; 9485 break; 9486 case TCP_ABORT_THRESHOLD: 9487 *i1 = tcps->tcps_ip_abort_interval; 9488 break; 9489 case TCP_CONN_NOTIFY_THRESHOLD: 9490 *i1 = tcps->tcps_ip_notify_cinterval; 9491 break; 9492 case TCP_CONN_ABORT_THRESHOLD: 9493 *i1 = tcps->tcps_ip_abort_cinterval; 9494 break; 9495 default: 9496 return (-1); 9497 } 9498 break; 9499 case IPPROTO_IP: 9500 switch (name) { 9501 case IP_TTL: 9502 *i1 = tcps->tcps_ipv4_ttl; 9503 break; 9504 default: 9505 return (-1); 9506 } 9507 break; 9508 case IPPROTO_IPV6: 9509 switch (name) { 9510 case IPV6_UNICAST_HOPS: 9511 *i1 = tcps->tcps_ipv6_hoplimit; 9512 break; 9513 default: 9514 return (-1); 9515 } 9516 break; 9517 default: 9518 return (-1); 9519 } 9520 return (sizeof (int)); 9521 } 9522 9523 static int 9524 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9525 { 9526 int *i1 = (int *)ptr; 9527 tcp_t *tcp = connp->conn_tcp; 9528 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9529 9530 switch (level) { 9531 case SOL_SOCKET: 9532 switch (name) { 9533 case SO_LINGER: { 9534 struct linger *lgr = (struct linger *)ptr; 9535 9536 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9537 lgr->l_linger = tcp->tcp_lingertime; 9538 } 9539 return (sizeof (struct linger)); 9540 case SO_DEBUG: 9541 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9542 break; 9543 case SO_KEEPALIVE: 9544 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9545 break; 9546 case SO_DONTROUTE: 9547 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9548 break; 9549 case SO_USELOOPBACK: 9550 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9551 break; 9552 case SO_BROADCAST: 9553 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9554 break; 9555 case SO_REUSEADDR: 9556 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9557 break; 9558 case SO_OOBINLINE: 9559 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9560 break; 9561 case SO_DGRAM_ERRIND: 9562 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9563 break; 9564 case SO_TYPE: 9565 *i1 = SOCK_STREAM; 9566 break; 9567 case SO_SNDBUF: 9568 *i1 = tcp->tcp_xmit_hiwater; 9569 break; 9570 case SO_RCVBUF: 9571 *i1 = tcp->tcp_recv_hiwater; 9572 break; 9573 case SO_SND_COPYAVOID: 9574 *i1 = tcp->tcp_snd_zcopy_on ? 9575 SO_SND_COPYAVOID : 0; 9576 break; 9577 case SO_ALLZONES: 9578 *i1 = connp->conn_allzones ? 1 : 0; 9579 break; 9580 case SO_ANON_MLP: 9581 *i1 = connp->conn_anon_mlp; 9582 break; 9583 case SO_MAC_EXEMPT: 9584 *i1 = connp->conn_mac_exempt; 9585 break; 9586 case SO_EXCLBIND: 9587 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9588 break; 9589 case SO_PROTOTYPE: 9590 *i1 = IPPROTO_TCP; 9591 break; 9592 case SO_DOMAIN: 9593 *i1 = tcp->tcp_family; 9594 break; 9595 case SO_ACCEPTCONN: 9596 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9597 default: 9598 return (-1); 9599 } 9600 break; 9601 case IPPROTO_TCP: 9602 switch (name) { 9603 case TCP_NODELAY: 9604 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9605 break; 9606 case TCP_MAXSEG: 9607 *i1 = tcp->tcp_mss; 9608 break; 9609 case TCP_NOTIFY_THRESHOLD: 9610 *i1 = (int)tcp->tcp_first_timer_threshold; 9611 break; 9612 case TCP_ABORT_THRESHOLD: 9613 *i1 = tcp->tcp_second_timer_threshold; 9614 break; 9615 case TCP_CONN_NOTIFY_THRESHOLD: 9616 *i1 = tcp->tcp_first_ctimer_threshold; 9617 break; 9618 case TCP_CONN_ABORT_THRESHOLD: 9619 *i1 = tcp->tcp_second_ctimer_threshold; 9620 break; 9621 case TCP_RECVDSTADDR: 9622 *i1 = tcp->tcp_recvdstaddr; 9623 break; 9624 case TCP_ANONPRIVBIND: 9625 *i1 = tcp->tcp_anon_priv_bind; 9626 break; 9627 case TCP_EXCLBIND: 9628 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9629 break; 9630 case TCP_INIT_CWND: 9631 *i1 = tcp->tcp_init_cwnd; 9632 break; 9633 case TCP_KEEPALIVE_THRESHOLD: 9634 *i1 = tcp->tcp_ka_interval; 9635 break; 9636 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9637 *i1 = tcp->tcp_ka_abort_thres; 9638 break; 9639 case TCP_CORK: 9640 *i1 = tcp->tcp_cork; 9641 break; 9642 default: 9643 return (-1); 9644 } 9645 break; 9646 case IPPROTO_IP: 9647 if (tcp->tcp_family != AF_INET) 9648 return (-1); 9649 switch (name) { 9650 case IP_OPTIONS: 9651 case T_IP_OPTIONS: { 9652 /* 9653 * This is compatible with BSD in that in only return 9654 * the reverse source route with the final destination 9655 * as the last entry. The first 4 bytes of the option 9656 * will contain the final destination. 9657 */ 9658 int opt_len; 9659 9660 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9661 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9662 ASSERT(opt_len >= 0); 9663 /* Caller ensures enough space */ 9664 if (opt_len > 0) { 9665 /* 9666 * TODO: Do we have to handle getsockopt on an 9667 * initiator as well? 9668 */ 9669 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9670 } 9671 return (0); 9672 } 9673 case IP_TOS: 9674 case T_IP_TOS: 9675 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9676 break; 9677 case IP_TTL: 9678 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9679 break; 9680 case IP_NEXTHOP: 9681 /* Handled at IP level */ 9682 return (-EINVAL); 9683 default: 9684 return (-1); 9685 } 9686 break; 9687 case IPPROTO_IPV6: 9688 /* 9689 * IPPROTO_IPV6 options are only supported for sockets 9690 * that are using IPv6 on the wire. 9691 */ 9692 if (tcp->tcp_ipversion != IPV6_VERSION) { 9693 return (-1); 9694 } 9695 switch (name) { 9696 case IPV6_UNICAST_HOPS: 9697 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9698 break; /* goto sizeof (int) option return */ 9699 case IPV6_BOUND_IF: 9700 /* Zero if not set */ 9701 *i1 = tcp->tcp_bound_if; 9702 break; /* goto sizeof (int) option return */ 9703 case IPV6_RECVPKTINFO: 9704 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9705 *i1 = 1; 9706 else 9707 *i1 = 0; 9708 break; /* goto sizeof (int) option return */ 9709 case IPV6_RECVTCLASS: 9710 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9711 *i1 = 1; 9712 else 9713 *i1 = 0; 9714 break; /* goto sizeof (int) option return */ 9715 case IPV6_RECVHOPLIMIT: 9716 if (tcp->tcp_ipv6_recvancillary & 9717 TCP_IPV6_RECVHOPLIMIT) 9718 *i1 = 1; 9719 else 9720 *i1 = 0; 9721 break; /* goto sizeof (int) option return */ 9722 case IPV6_RECVHOPOPTS: 9723 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9724 *i1 = 1; 9725 else 9726 *i1 = 0; 9727 break; /* goto sizeof (int) option return */ 9728 case IPV6_RECVDSTOPTS: 9729 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9730 *i1 = 1; 9731 else 9732 *i1 = 0; 9733 break; /* goto sizeof (int) option return */ 9734 case _OLD_IPV6_RECVDSTOPTS: 9735 if (tcp->tcp_ipv6_recvancillary & 9736 TCP_OLD_IPV6_RECVDSTOPTS) 9737 *i1 = 1; 9738 else 9739 *i1 = 0; 9740 break; /* goto sizeof (int) option return */ 9741 case IPV6_RECVRTHDR: 9742 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9743 *i1 = 1; 9744 else 9745 *i1 = 0; 9746 break; /* goto sizeof (int) option return */ 9747 case IPV6_RECVRTHDRDSTOPTS: 9748 if (tcp->tcp_ipv6_recvancillary & 9749 TCP_IPV6_RECVRTDSTOPTS) 9750 *i1 = 1; 9751 else 9752 *i1 = 0; 9753 break; /* goto sizeof (int) option return */ 9754 case IPV6_PKTINFO: { 9755 /* XXX assumes that caller has room for max size! */ 9756 struct in6_pktinfo *pkti; 9757 9758 pkti = (struct in6_pktinfo *)ptr; 9759 if (ipp->ipp_fields & IPPF_IFINDEX) 9760 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9761 else 9762 pkti->ipi6_ifindex = 0; 9763 if (ipp->ipp_fields & IPPF_ADDR) 9764 pkti->ipi6_addr = ipp->ipp_addr; 9765 else 9766 pkti->ipi6_addr = ipv6_all_zeros; 9767 return (sizeof (struct in6_pktinfo)); 9768 } 9769 case IPV6_TCLASS: 9770 if (ipp->ipp_fields & IPPF_TCLASS) 9771 *i1 = ipp->ipp_tclass; 9772 else 9773 *i1 = IPV6_FLOW_TCLASS( 9774 IPV6_DEFAULT_VERS_AND_FLOW); 9775 break; /* goto sizeof (int) option return */ 9776 case IPV6_NEXTHOP: { 9777 sin6_t *sin6 = (sin6_t *)ptr; 9778 9779 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9780 return (0); 9781 *sin6 = sin6_null; 9782 sin6->sin6_family = AF_INET6; 9783 sin6->sin6_addr = ipp->ipp_nexthop; 9784 return (sizeof (sin6_t)); 9785 } 9786 case IPV6_HOPOPTS: 9787 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9788 return (0); 9789 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9790 return (0); 9791 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9792 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9793 if (tcp->tcp_label_len > 0) { 9794 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9795 ptr[1] = (ipp->ipp_hopoptslen - 9796 tcp->tcp_label_len + 7) / 8 - 1; 9797 } 9798 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9799 case IPV6_RTHDRDSTOPTS: 9800 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9801 return (0); 9802 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9803 return (ipp->ipp_rtdstoptslen); 9804 case IPV6_RTHDR: 9805 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9806 return (0); 9807 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9808 return (ipp->ipp_rthdrlen); 9809 case IPV6_DSTOPTS: 9810 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9811 return (0); 9812 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9813 return (ipp->ipp_dstoptslen); 9814 case IPV6_SRC_PREFERENCES: 9815 return (ip6_get_src_preferences(connp, 9816 (uint32_t *)ptr)); 9817 case IPV6_PATHMTU: { 9818 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9819 9820 if (tcp->tcp_state < TCPS_ESTABLISHED) 9821 return (-1); 9822 9823 return (ip_fill_mtuinfo(&connp->conn_remv6, 9824 connp->conn_fport, mtuinfo, 9825 connp->conn_netstack)); 9826 } 9827 default: 9828 return (-1); 9829 } 9830 break; 9831 default: 9832 return (-1); 9833 } 9834 return (sizeof (int)); 9835 } 9836 9837 /* 9838 * TCP routine to get the values of options. 9839 */ 9840 int 9841 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9842 { 9843 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9844 } 9845 9846 /* returns UNIX error, the optlen is a value-result arg */ 9847 int 9848 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9849 void *optvalp, socklen_t *optlen, cred_t *cr) 9850 { 9851 conn_t *connp = (conn_t *)proto_handle; 9852 squeue_t *sqp = connp->conn_sqp; 9853 int error; 9854 t_uscalar_t max_optbuf_len; 9855 void *optvalp_buf; 9856 int len; 9857 9858 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9859 tcp_opt_obj.odb_opt_des_arr, 9860 tcp_opt_obj.odb_opt_arr_cnt, 9861 tcp_opt_obj.odb_topmost_tpiprovider, 9862 B_FALSE, B_TRUE, cr); 9863 if (error != 0) { 9864 if (error < 0) { 9865 error = proto_tlitosyserr(-error); 9866 } 9867 return (error); 9868 } 9869 9870 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9871 9872 error = squeue_synch_enter(sqp, connp, 0); 9873 if (error == ENOMEM) { 9874 return (ENOMEM); 9875 } 9876 9877 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9878 squeue_synch_exit(sqp, connp); 9879 9880 if (len < 0) { 9881 /* 9882 * Pass on to IP 9883 */ 9884 kmem_free(optvalp_buf, max_optbuf_len); 9885 return (ip_get_options(connp, level, option_name, 9886 optvalp, optlen, cr)); 9887 } else { 9888 /* 9889 * update optlen and copy option value 9890 */ 9891 t_uscalar_t size = MIN(len, *optlen); 9892 bcopy(optvalp_buf, optvalp, size); 9893 bcopy(&size, optlen, sizeof (size)); 9894 9895 kmem_free(optvalp_buf, max_optbuf_len); 9896 return (0); 9897 } 9898 } 9899 9900 /* 9901 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9902 * Parameters are assumed to be verified by the caller. 9903 */ 9904 /* ARGSUSED */ 9905 int 9906 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9907 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9908 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9909 { 9910 tcp_t *tcp = connp->conn_tcp; 9911 int *i1 = (int *)invalp; 9912 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9913 boolean_t checkonly; 9914 int reterr; 9915 tcp_stack_t *tcps = tcp->tcp_tcps; 9916 9917 switch (optset_context) { 9918 case SETFN_OPTCOM_CHECKONLY: 9919 checkonly = B_TRUE; 9920 /* 9921 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9922 * inlen != 0 implies value supplied and 9923 * we have to "pretend" to set it. 9924 * inlen == 0 implies that there is no 9925 * value part in T_CHECK request and just validation 9926 * done elsewhere should be enough, we just return here. 9927 */ 9928 if (inlen == 0) { 9929 *outlenp = 0; 9930 return (0); 9931 } 9932 break; 9933 case SETFN_OPTCOM_NEGOTIATE: 9934 checkonly = B_FALSE; 9935 break; 9936 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9937 case SETFN_CONN_NEGOTIATE: 9938 checkonly = B_FALSE; 9939 /* 9940 * Negotiating local and "association-related" options 9941 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9942 * primitives is allowed by XTI, but we choose 9943 * to not implement this style negotiation for Internet 9944 * protocols (We interpret it is a must for OSI world but 9945 * optional for Internet protocols) for all options. 9946 * [ Will do only for the few options that enable test 9947 * suites that our XTI implementation of this feature 9948 * works for transports that do allow it ] 9949 */ 9950 if (!tcp_allow_connopt_set(level, name)) { 9951 *outlenp = 0; 9952 return (EINVAL); 9953 } 9954 break; 9955 default: 9956 /* 9957 * We should never get here 9958 */ 9959 *outlenp = 0; 9960 return (EINVAL); 9961 } 9962 9963 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9964 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9965 9966 /* 9967 * For TCP, we should have no ancillary data sent down 9968 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9969 * has to be zero. 9970 */ 9971 ASSERT(thisdg_attrs == NULL); 9972 9973 /* 9974 * For fixed length options, no sanity check 9975 * of passed in length is done. It is assumed *_optcom_req() 9976 * routines do the right thing. 9977 */ 9978 switch (level) { 9979 case SOL_SOCKET: 9980 switch (name) { 9981 case SO_LINGER: { 9982 struct linger *lgr = (struct linger *)invalp; 9983 9984 if (!checkonly) { 9985 if (lgr->l_onoff) { 9986 tcp->tcp_linger = 1; 9987 tcp->tcp_lingertime = lgr->l_linger; 9988 } else { 9989 tcp->tcp_linger = 0; 9990 tcp->tcp_lingertime = 0; 9991 } 9992 /* struct copy */ 9993 *(struct linger *)outvalp = *lgr; 9994 } else { 9995 if (!lgr->l_onoff) { 9996 ((struct linger *) 9997 outvalp)->l_onoff = 0; 9998 ((struct linger *) 9999 outvalp)->l_linger = 0; 10000 } else { 10001 /* struct copy */ 10002 *(struct linger *)outvalp = *lgr; 10003 } 10004 } 10005 *outlenp = sizeof (struct linger); 10006 return (0); 10007 } 10008 case SO_DEBUG: 10009 if (!checkonly) 10010 tcp->tcp_debug = onoff; 10011 break; 10012 case SO_KEEPALIVE: 10013 if (checkonly) { 10014 /* check only case */ 10015 break; 10016 } 10017 10018 if (!onoff) { 10019 if (tcp->tcp_ka_enabled) { 10020 if (tcp->tcp_ka_tid != 0) { 10021 (void) TCP_TIMER_CANCEL(tcp, 10022 tcp->tcp_ka_tid); 10023 tcp->tcp_ka_tid = 0; 10024 } 10025 tcp->tcp_ka_enabled = 0; 10026 } 10027 break; 10028 } 10029 if (!tcp->tcp_ka_enabled) { 10030 /* Crank up the keepalive timer */ 10031 tcp->tcp_ka_last_intrvl = 0; 10032 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10033 tcp_keepalive_killer, 10034 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10035 tcp->tcp_ka_enabled = 1; 10036 } 10037 break; 10038 case SO_DONTROUTE: 10039 /* 10040 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10041 * only of interest to IP. We track them here only so 10042 * that we can report their current value. 10043 */ 10044 if (!checkonly) { 10045 tcp->tcp_dontroute = onoff; 10046 tcp->tcp_connp->conn_dontroute = onoff; 10047 } 10048 break; 10049 case SO_USELOOPBACK: 10050 if (!checkonly) { 10051 tcp->tcp_useloopback = onoff; 10052 tcp->tcp_connp->conn_loopback = onoff; 10053 } 10054 break; 10055 case SO_BROADCAST: 10056 if (!checkonly) { 10057 tcp->tcp_broadcast = onoff; 10058 tcp->tcp_connp->conn_broadcast = onoff; 10059 } 10060 break; 10061 case SO_REUSEADDR: 10062 if (!checkonly) { 10063 tcp->tcp_reuseaddr = onoff; 10064 tcp->tcp_connp->conn_reuseaddr = onoff; 10065 } 10066 break; 10067 case SO_OOBINLINE: 10068 if (!checkonly) { 10069 tcp->tcp_oobinline = onoff; 10070 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10071 proto_set_rx_oob_opt(connp, onoff); 10072 } 10073 break; 10074 case SO_DGRAM_ERRIND: 10075 if (!checkonly) 10076 tcp->tcp_dgram_errind = onoff; 10077 break; 10078 case SO_SNDBUF: { 10079 if (*i1 > tcps->tcps_max_buf) { 10080 *outlenp = 0; 10081 return (ENOBUFS); 10082 } 10083 if (checkonly) 10084 break; 10085 10086 tcp->tcp_xmit_hiwater = *i1; 10087 if (tcps->tcps_snd_lowat_fraction != 0) 10088 tcp->tcp_xmit_lowater = 10089 tcp->tcp_xmit_hiwater / 10090 tcps->tcps_snd_lowat_fraction; 10091 (void) tcp_maxpsz_set(tcp, B_TRUE); 10092 /* 10093 * If we are flow-controlled, recheck the condition. 10094 * There are apps that increase SO_SNDBUF size when 10095 * flow-controlled (EWOULDBLOCK), and expect the flow 10096 * control condition to be lifted right away. 10097 */ 10098 mutex_enter(&tcp->tcp_non_sq_lock); 10099 if (tcp->tcp_flow_stopped && 10100 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10101 tcp_clrqfull(tcp); 10102 } 10103 mutex_exit(&tcp->tcp_non_sq_lock); 10104 break; 10105 } 10106 case SO_RCVBUF: 10107 if (*i1 > tcps->tcps_max_buf) { 10108 *outlenp = 0; 10109 return (ENOBUFS); 10110 } 10111 /* Silently ignore zero */ 10112 if (!checkonly && *i1 != 0) { 10113 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10114 (void) tcp_rwnd_set(tcp, *i1); 10115 } 10116 /* 10117 * XXX should we return the rwnd here 10118 * and tcp_opt_get ? 10119 */ 10120 break; 10121 case SO_SND_COPYAVOID: 10122 if (!checkonly) { 10123 /* we only allow enable at most once for now */ 10124 if (tcp->tcp_loopback || 10125 (tcp->tcp_kssl_ctx != NULL) || 10126 (!tcp->tcp_snd_zcopy_aware && 10127 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10128 *outlenp = 0; 10129 return (EOPNOTSUPP); 10130 } 10131 tcp->tcp_snd_zcopy_aware = 1; 10132 } 10133 break; 10134 case SO_RCVTIMEO: 10135 case SO_SNDTIMEO: 10136 /* 10137 * Pass these two options in order for third part 10138 * protocol usage. Here just return directly. 10139 */ 10140 return (0); 10141 case SO_ALLZONES: 10142 /* Pass option along to IP level for handling */ 10143 return (-EINVAL); 10144 case SO_ANON_MLP: 10145 /* Pass option along to IP level for handling */ 10146 return (-EINVAL); 10147 case SO_MAC_EXEMPT: 10148 /* Pass option along to IP level for handling */ 10149 return (-EINVAL); 10150 case SO_EXCLBIND: 10151 if (!checkonly) 10152 tcp->tcp_exclbind = onoff; 10153 break; 10154 default: 10155 *outlenp = 0; 10156 return (EINVAL); 10157 } 10158 break; 10159 case IPPROTO_TCP: 10160 switch (name) { 10161 case TCP_NODELAY: 10162 if (!checkonly) 10163 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10164 break; 10165 case TCP_NOTIFY_THRESHOLD: 10166 if (!checkonly) 10167 tcp->tcp_first_timer_threshold = *i1; 10168 break; 10169 case TCP_ABORT_THRESHOLD: 10170 if (!checkonly) 10171 tcp->tcp_second_timer_threshold = *i1; 10172 break; 10173 case TCP_CONN_NOTIFY_THRESHOLD: 10174 if (!checkonly) 10175 tcp->tcp_first_ctimer_threshold = *i1; 10176 break; 10177 case TCP_CONN_ABORT_THRESHOLD: 10178 if (!checkonly) 10179 tcp->tcp_second_ctimer_threshold = *i1; 10180 break; 10181 case TCP_RECVDSTADDR: 10182 if (tcp->tcp_state > TCPS_LISTEN) 10183 return (EOPNOTSUPP); 10184 if (!checkonly) 10185 tcp->tcp_recvdstaddr = onoff; 10186 break; 10187 case TCP_ANONPRIVBIND: 10188 if ((reterr = secpolicy_net_privaddr(cr, 0, 10189 IPPROTO_TCP)) != 0) { 10190 *outlenp = 0; 10191 return (reterr); 10192 } 10193 if (!checkonly) { 10194 tcp->tcp_anon_priv_bind = onoff; 10195 } 10196 break; 10197 case TCP_EXCLBIND: 10198 if (!checkonly) 10199 tcp->tcp_exclbind = onoff; 10200 break; /* goto sizeof (int) option return */ 10201 case TCP_INIT_CWND: { 10202 uint32_t init_cwnd = *((uint32_t *)invalp); 10203 10204 if (checkonly) 10205 break; 10206 10207 /* 10208 * Only allow socket with network configuration 10209 * privilege to set the initial cwnd to be larger 10210 * than allowed by RFC 3390. 10211 */ 10212 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10213 tcp->tcp_init_cwnd = init_cwnd; 10214 break; 10215 } 10216 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10217 *outlenp = 0; 10218 return (reterr); 10219 } 10220 if (init_cwnd > TCP_MAX_INIT_CWND) { 10221 *outlenp = 0; 10222 return (EINVAL); 10223 } 10224 tcp->tcp_init_cwnd = init_cwnd; 10225 break; 10226 } 10227 case TCP_KEEPALIVE_THRESHOLD: 10228 if (checkonly) 10229 break; 10230 10231 if (*i1 < tcps->tcps_keepalive_interval_low || 10232 *i1 > tcps->tcps_keepalive_interval_high) { 10233 *outlenp = 0; 10234 return (EINVAL); 10235 } 10236 if (*i1 != tcp->tcp_ka_interval) { 10237 tcp->tcp_ka_interval = *i1; 10238 /* 10239 * Check if we need to restart the 10240 * keepalive timer. 10241 */ 10242 if (tcp->tcp_ka_tid != 0) { 10243 ASSERT(tcp->tcp_ka_enabled); 10244 (void) TCP_TIMER_CANCEL(tcp, 10245 tcp->tcp_ka_tid); 10246 tcp->tcp_ka_last_intrvl = 0; 10247 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10248 tcp_keepalive_killer, 10249 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10250 } 10251 } 10252 break; 10253 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10254 if (!checkonly) { 10255 if (*i1 < 10256 tcps->tcps_keepalive_abort_interval_low || 10257 *i1 > 10258 tcps->tcps_keepalive_abort_interval_high) { 10259 *outlenp = 0; 10260 return (EINVAL); 10261 } 10262 tcp->tcp_ka_abort_thres = *i1; 10263 } 10264 break; 10265 case TCP_CORK: 10266 if (!checkonly) { 10267 /* 10268 * if tcp->tcp_cork was set and is now 10269 * being unset, we have to make sure that 10270 * the remaining data gets sent out. Also 10271 * unset tcp->tcp_cork so that tcp_wput_data() 10272 * can send data even if it is less than mss 10273 */ 10274 if (tcp->tcp_cork && onoff == 0 && 10275 tcp->tcp_unsent > 0) { 10276 tcp->tcp_cork = B_FALSE; 10277 tcp_wput_data(tcp, NULL, B_FALSE); 10278 } 10279 tcp->tcp_cork = onoff; 10280 } 10281 break; 10282 default: 10283 *outlenp = 0; 10284 return (EINVAL); 10285 } 10286 break; 10287 case IPPROTO_IP: 10288 if (tcp->tcp_family != AF_INET) { 10289 *outlenp = 0; 10290 return (ENOPROTOOPT); 10291 } 10292 switch (name) { 10293 case IP_OPTIONS: 10294 case T_IP_OPTIONS: 10295 reterr = tcp_opt_set_header(tcp, checkonly, 10296 invalp, inlen); 10297 if (reterr) { 10298 *outlenp = 0; 10299 return (reterr); 10300 } 10301 /* OK return - copy input buffer into output buffer */ 10302 if (invalp != outvalp) { 10303 /* don't trust bcopy for identical src/dst */ 10304 bcopy(invalp, outvalp, inlen); 10305 } 10306 *outlenp = inlen; 10307 return (0); 10308 case IP_TOS: 10309 case T_IP_TOS: 10310 if (!checkonly) { 10311 tcp->tcp_ipha->ipha_type_of_service = 10312 (uchar_t)*i1; 10313 tcp->tcp_tos = (uchar_t)*i1; 10314 } 10315 break; 10316 case IP_TTL: 10317 if (!checkonly) { 10318 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10319 tcp->tcp_ttl = (uchar_t)*i1; 10320 } 10321 break; 10322 case IP_BOUND_IF: 10323 case IP_NEXTHOP: 10324 /* Handled at the IP level */ 10325 return (-EINVAL); 10326 case IP_SEC_OPT: 10327 /* 10328 * We should not allow policy setting after 10329 * we start listening for connections. 10330 */ 10331 if (tcp->tcp_state == TCPS_LISTEN) { 10332 return (EINVAL); 10333 } else { 10334 /* Handled at the IP level */ 10335 return (-EINVAL); 10336 } 10337 default: 10338 *outlenp = 0; 10339 return (EINVAL); 10340 } 10341 break; 10342 case IPPROTO_IPV6: { 10343 ip6_pkt_t *ipp; 10344 10345 /* 10346 * IPPROTO_IPV6 options are only supported for sockets 10347 * that are using IPv6 on the wire. 10348 */ 10349 if (tcp->tcp_ipversion != IPV6_VERSION) { 10350 *outlenp = 0; 10351 return (ENOPROTOOPT); 10352 } 10353 /* 10354 * Only sticky options; no ancillary data 10355 */ 10356 ipp = &tcp->tcp_sticky_ipp; 10357 10358 switch (name) { 10359 case IPV6_UNICAST_HOPS: 10360 /* -1 means use default */ 10361 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10362 *outlenp = 0; 10363 return (EINVAL); 10364 } 10365 if (!checkonly) { 10366 if (*i1 == -1) { 10367 tcp->tcp_ip6h->ip6_hops = 10368 ipp->ipp_unicast_hops = 10369 (uint8_t)tcps->tcps_ipv6_hoplimit; 10370 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10371 /* Pass modified value to IP. */ 10372 *i1 = tcp->tcp_ip6h->ip6_hops; 10373 } else { 10374 tcp->tcp_ip6h->ip6_hops = 10375 ipp->ipp_unicast_hops = 10376 (uint8_t)*i1; 10377 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10378 } 10379 reterr = tcp_build_hdrs(tcp); 10380 if (reterr != 0) 10381 return (reterr); 10382 } 10383 break; 10384 case IPV6_BOUND_IF: 10385 if (!checkonly) { 10386 tcp->tcp_bound_if = *i1; 10387 PASS_OPT_TO_IP(connp); 10388 } 10389 break; 10390 /* 10391 * Set boolean switches for ancillary data delivery 10392 */ 10393 case IPV6_RECVPKTINFO: 10394 if (!checkonly) { 10395 if (onoff) 10396 tcp->tcp_ipv6_recvancillary |= 10397 TCP_IPV6_RECVPKTINFO; 10398 else 10399 tcp->tcp_ipv6_recvancillary &= 10400 ~TCP_IPV6_RECVPKTINFO; 10401 /* Force it to be sent up with the next msg */ 10402 tcp->tcp_recvifindex = 0; 10403 PASS_OPT_TO_IP(connp); 10404 } 10405 break; 10406 case IPV6_RECVTCLASS: 10407 if (!checkonly) { 10408 if (onoff) 10409 tcp->tcp_ipv6_recvancillary |= 10410 TCP_IPV6_RECVTCLASS; 10411 else 10412 tcp->tcp_ipv6_recvancillary &= 10413 ~TCP_IPV6_RECVTCLASS; 10414 PASS_OPT_TO_IP(connp); 10415 } 10416 break; 10417 case IPV6_RECVHOPLIMIT: 10418 if (!checkonly) { 10419 if (onoff) 10420 tcp->tcp_ipv6_recvancillary |= 10421 TCP_IPV6_RECVHOPLIMIT; 10422 else 10423 tcp->tcp_ipv6_recvancillary &= 10424 ~TCP_IPV6_RECVHOPLIMIT; 10425 /* Force it to be sent up with the next msg */ 10426 tcp->tcp_recvhops = 0xffffffffU; 10427 PASS_OPT_TO_IP(connp); 10428 } 10429 break; 10430 case IPV6_RECVHOPOPTS: 10431 if (!checkonly) { 10432 if (onoff) 10433 tcp->tcp_ipv6_recvancillary |= 10434 TCP_IPV6_RECVHOPOPTS; 10435 else 10436 tcp->tcp_ipv6_recvancillary &= 10437 ~TCP_IPV6_RECVHOPOPTS; 10438 PASS_OPT_TO_IP(connp); 10439 } 10440 break; 10441 case IPV6_RECVDSTOPTS: 10442 if (!checkonly) { 10443 if (onoff) 10444 tcp->tcp_ipv6_recvancillary |= 10445 TCP_IPV6_RECVDSTOPTS; 10446 else 10447 tcp->tcp_ipv6_recvancillary &= 10448 ~TCP_IPV6_RECVDSTOPTS; 10449 PASS_OPT_TO_IP(connp); 10450 } 10451 break; 10452 case _OLD_IPV6_RECVDSTOPTS: 10453 if (!checkonly) { 10454 if (onoff) 10455 tcp->tcp_ipv6_recvancillary |= 10456 TCP_OLD_IPV6_RECVDSTOPTS; 10457 else 10458 tcp->tcp_ipv6_recvancillary &= 10459 ~TCP_OLD_IPV6_RECVDSTOPTS; 10460 } 10461 break; 10462 case IPV6_RECVRTHDR: 10463 if (!checkonly) { 10464 if (onoff) 10465 tcp->tcp_ipv6_recvancillary |= 10466 TCP_IPV6_RECVRTHDR; 10467 else 10468 tcp->tcp_ipv6_recvancillary &= 10469 ~TCP_IPV6_RECVRTHDR; 10470 PASS_OPT_TO_IP(connp); 10471 } 10472 break; 10473 case IPV6_RECVRTHDRDSTOPTS: 10474 if (!checkonly) { 10475 if (onoff) 10476 tcp->tcp_ipv6_recvancillary |= 10477 TCP_IPV6_RECVRTDSTOPTS; 10478 else 10479 tcp->tcp_ipv6_recvancillary &= 10480 ~TCP_IPV6_RECVRTDSTOPTS; 10481 PASS_OPT_TO_IP(connp); 10482 } 10483 break; 10484 case IPV6_PKTINFO: 10485 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10486 return (EINVAL); 10487 if (checkonly) 10488 break; 10489 10490 if (inlen == 0) { 10491 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10492 } else { 10493 struct in6_pktinfo *pkti; 10494 10495 pkti = (struct in6_pktinfo *)invalp; 10496 /* 10497 * RFC 3542 states that ipi6_addr must be 10498 * the unspecified address when setting the 10499 * IPV6_PKTINFO sticky socket option on a 10500 * TCP socket. 10501 */ 10502 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10503 return (EINVAL); 10504 /* 10505 * IP will validate the source address and 10506 * interface index. 10507 */ 10508 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10509 reterr = ip_set_options(tcp->tcp_connp, 10510 level, name, invalp, inlen, cr); 10511 } else { 10512 reterr = ip6_set_pktinfo(cr, 10513 tcp->tcp_connp, pkti); 10514 } 10515 if (reterr != 0) 10516 return (reterr); 10517 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10518 ipp->ipp_addr = pkti->ipi6_addr; 10519 if (ipp->ipp_ifindex != 0) 10520 ipp->ipp_fields |= IPPF_IFINDEX; 10521 else 10522 ipp->ipp_fields &= ~IPPF_IFINDEX; 10523 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10524 ipp->ipp_fields |= IPPF_ADDR; 10525 else 10526 ipp->ipp_fields &= ~IPPF_ADDR; 10527 } 10528 reterr = tcp_build_hdrs(tcp); 10529 if (reterr != 0) 10530 return (reterr); 10531 break; 10532 case IPV6_TCLASS: 10533 if (inlen != 0 && inlen != sizeof (int)) 10534 return (EINVAL); 10535 if (checkonly) 10536 break; 10537 10538 if (inlen == 0) { 10539 ipp->ipp_fields &= ~IPPF_TCLASS; 10540 } else { 10541 if (*i1 > 255 || *i1 < -1) 10542 return (EINVAL); 10543 if (*i1 == -1) { 10544 ipp->ipp_tclass = 0; 10545 *i1 = 0; 10546 } else { 10547 ipp->ipp_tclass = *i1; 10548 } 10549 ipp->ipp_fields |= IPPF_TCLASS; 10550 } 10551 reterr = tcp_build_hdrs(tcp); 10552 if (reterr != 0) 10553 return (reterr); 10554 break; 10555 case IPV6_NEXTHOP: 10556 /* 10557 * IP will verify that the nexthop is reachable 10558 * and fail for sticky options. 10559 */ 10560 if (inlen != 0 && inlen != sizeof (sin6_t)) 10561 return (EINVAL); 10562 if (checkonly) 10563 break; 10564 10565 if (inlen == 0) { 10566 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10567 } else { 10568 sin6_t *sin6 = (sin6_t *)invalp; 10569 10570 if (sin6->sin6_family != AF_INET6) 10571 return (EAFNOSUPPORT); 10572 if (IN6_IS_ADDR_V4MAPPED( 10573 &sin6->sin6_addr)) 10574 return (EADDRNOTAVAIL); 10575 ipp->ipp_nexthop = sin6->sin6_addr; 10576 if (!IN6_IS_ADDR_UNSPECIFIED( 10577 &ipp->ipp_nexthop)) 10578 ipp->ipp_fields |= IPPF_NEXTHOP; 10579 else 10580 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10581 } 10582 reterr = tcp_build_hdrs(tcp); 10583 if (reterr != 0) 10584 return (reterr); 10585 PASS_OPT_TO_IP(connp); 10586 break; 10587 case IPV6_HOPOPTS: { 10588 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10589 10590 /* 10591 * Sanity checks - minimum size, size a multiple of 10592 * eight bytes, and matching size passed in. 10593 */ 10594 if (inlen != 0 && 10595 inlen != (8 * (hopts->ip6h_len + 1))) 10596 return (EINVAL); 10597 10598 if (checkonly) 10599 break; 10600 10601 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10602 (uchar_t **)&ipp->ipp_hopopts, 10603 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10604 if (reterr != 0) 10605 return (reterr); 10606 if (ipp->ipp_hopoptslen == 0) 10607 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10608 else 10609 ipp->ipp_fields |= IPPF_HOPOPTS; 10610 reterr = tcp_build_hdrs(tcp); 10611 if (reterr != 0) 10612 return (reterr); 10613 break; 10614 } 10615 case IPV6_RTHDRDSTOPTS: { 10616 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10617 10618 /* 10619 * Sanity checks - minimum size, size a multiple of 10620 * eight bytes, and matching size passed in. 10621 */ 10622 if (inlen != 0 && 10623 inlen != (8 * (dopts->ip6d_len + 1))) 10624 return (EINVAL); 10625 10626 if (checkonly) 10627 break; 10628 10629 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10630 (uchar_t **)&ipp->ipp_rtdstopts, 10631 &ipp->ipp_rtdstoptslen, 0); 10632 if (reterr != 0) 10633 return (reterr); 10634 if (ipp->ipp_rtdstoptslen == 0) 10635 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10636 else 10637 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10638 reterr = tcp_build_hdrs(tcp); 10639 if (reterr != 0) 10640 return (reterr); 10641 break; 10642 } 10643 case IPV6_DSTOPTS: { 10644 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10645 10646 /* 10647 * Sanity checks - minimum size, size a multiple of 10648 * eight bytes, and matching size passed in. 10649 */ 10650 if (inlen != 0 && 10651 inlen != (8 * (dopts->ip6d_len + 1))) 10652 return (EINVAL); 10653 10654 if (checkonly) 10655 break; 10656 10657 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10658 (uchar_t **)&ipp->ipp_dstopts, 10659 &ipp->ipp_dstoptslen, 0); 10660 if (reterr != 0) 10661 return (reterr); 10662 if (ipp->ipp_dstoptslen == 0) 10663 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10664 else 10665 ipp->ipp_fields |= IPPF_DSTOPTS; 10666 reterr = tcp_build_hdrs(tcp); 10667 if (reterr != 0) 10668 return (reterr); 10669 break; 10670 } 10671 case IPV6_RTHDR: { 10672 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10673 10674 /* 10675 * Sanity checks - minimum size, size a multiple of 10676 * eight bytes, and matching size passed in. 10677 */ 10678 if (inlen != 0 && 10679 inlen != (8 * (rt->ip6r_len + 1))) 10680 return (EINVAL); 10681 10682 if (checkonly) 10683 break; 10684 10685 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10686 (uchar_t **)&ipp->ipp_rthdr, 10687 &ipp->ipp_rthdrlen, 0); 10688 if (reterr != 0) 10689 return (reterr); 10690 if (ipp->ipp_rthdrlen == 0) 10691 ipp->ipp_fields &= ~IPPF_RTHDR; 10692 else 10693 ipp->ipp_fields |= IPPF_RTHDR; 10694 reterr = tcp_build_hdrs(tcp); 10695 if (reterr != 0) 10696 return (reterr); 10697 break; 10698 } 10699 case IPV6_V6ONLY: 10700 if (!checkonly) { 10701 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10702 } 10703 break; 10704 case IPV6_USE_MIN_MTU: 10705 if (inlen != sizeof (int)) 10706 return (EINVAL); 10707 10708 if (*i1 < -1 || *i1 > 1) 10709 return (EINVAL); 10710 10711 if (checkonly) 10712 break; 10713 10714 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10715 ipp->ipp_use_min_mtu = *i1; 10716 break; 10717 case IPV6_SEC_OPT: 10718 /* 10719 * We should not allow policy setting after 10720 * we start listening for connections. 10721 */ 10722 if (tcp->tcp_state == TCPS_LISTEN) { 10723 return (EINVAL); 10724 } else { 10725 /* Handled at the IP level */ 10726 return (-EINVAL); 10727 } 10728 case IPV6_SRC_PREFERENCES: 10729 if (inlen != sizeof (uint32_t)) 10730 return (EINVAL); 10731 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10732 *(uint32_t *)invalp); 10733 if (reterr != 0) { 10734 *outlenp = 0; 10735 return (reterr); 10736 } 10737 break; 10738 default: 10739 *outlenp = 0; 10740 return (EINVAL); 10741 } 10742 break; 10743 } /* end IPPROTO_IPV6 */ 10744 default: 10745 *outlenp = 0; 10746 return (EINVAL); 10747 } 10748 /* 10749 * Common case of OK return with outval same as inval 10750 */ 10751 if (invalp != outvalp) { 10752 /* don't trust bcopy for identical src/dst */ 10753 (void) bcopy(invalp, outvalp, inlen); 10754 } 10755 *outlenp = inlen; 10756 return (0); 10757 } 10758 10759 /* ARGSUSED */ 10760 int 10761 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10762 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10763 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10764 { 10765 conn_t *connp = Q_TO_CONN(q); 10766 10767 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10768 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10769 } 10770 10771 int 10772 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10773 const void *optvalp, socklen_t optlen, cred_t *cr) 10774 { 10775 conn_t *connp = (conn_t *)proto_handle; 10776 squeue_t *sqp = connp->conn_sqp; 10777 int error; 10778 10779 /* 10780 * Entering the squeue synchronously can result in a context switch, 10781 * which can cause a rather sever performance degradation. So we try to 10782 * handle whatever options we can without entering the squeue. 10783 */ 10784 if (level == IPPROTO_TCP) { 10785 switch (option_name) { 10786 case TCP_NODELAY: 10787 if (optlen != sizeof (int32_t)) 10788 return (EINVAL); 10789 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10790 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10791 connp->conn_tcp->tcp_mss; 10792 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10793 return (0); 10794 default: 10795 break; 10796 } 10797 } 10798 10799 error = squeue_synch_enter(sqp, connp, 0); 10800 if (error == ENOMEM) { 10801 return (ENOMEM); 10802 } 10803 10804 error = proto_opt_check(level, option_name, optlen, NULL, 10805 tcp_opt_obj.odb_opt_des_arr, 10806 tcp_opt_obj.odb_opt_arr_cnt, 10807 tcp_opt_obj.odb_topmost_tpiprovider, 10808 B_TRUE, B_FALSE, cr); 10809 10810 if (error != 0) { 10811 if (error < 0) { 10812 error = proto_tlitosyserr(-error); 10813 } 10814 squeue_synch_exit(sqp, connp); 10815 return (error); 10816 } 10817 10818 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10819 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10820 NULL, cr, NULL); 10821 squeue_synch_exit(sqp, connp); 10822 10823 if (error < 0) { 10824 /* 10825 * Pass on to ip 10826 */ 10827 error = ip_set_options(connp, level, option_name, optvalp, 10828 optlen, cr); 10829 } 10830 return (error); 10831 } 10832 10833 /* 10834 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10835 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10836 * headers, and the maximum size tcp header (to avoid reallocation 10837 * on the fly for additional tcp options). 10838 * Returns failure if can't allocate memory. 10839 */ 10840 static int 10841 tcp_build_hdrs(tcp_t *tcp) 10842 { 10843 char *hdrs; 10844 uint_t hdrs_len; 10845 ip6i_t *ip6i; 10846 char buf[TCP_MAX_HDR_LENGTH]; 10847 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10848 in6_addr_t src, dst; 10849 tcp_stack_t *tcps = tcp->tcp_tcps; 10850 conn_t *connp = tcp->tcp_connp; 10851 10852 /* 10853 * save the existing tcp header and source/dest IP addresses 10854 */ 10855 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10856 src = tcp->tcp_ip6h->ip6_src; 10857 dst = tcp->tcp_ip6h->ip6_dst; 10858 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10859 ASSERT(hdrs_len != 0); 10860 if (hdrs_len > tcp->tcp_iphc_len) { 10861 /* Need to reallocate */ 10862 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10863 if (hdrs == NULL) 10864 return (ENOMEM); 10865 if (tcp->tcp_iphc != NULL) { 10866 if (tcp->tcp_hdr_grown) { 10867 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10868 } else { 10869 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10870 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10871 } 10872 tcp->tcp_iphc_len = 0; 10873 } 10874 ASSERT(tcp->tcp_iphc_len == 0); 10875 tcp->tcp_iphc = hdrs; 10876 tcp->tcp_iphc_len = hdrs_len; 10877 tcp->tcp_hdr_grown = B_TRUE; 10878 } 10879 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10880 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10881 10882 /* Set header fields not in ipp */ 10883 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10884 ip6i = (ip6i_t *)tcp->tcp_iphc; 10885 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10886 } else { 10887 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10888 } 10889 /* 10890 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10891 * 10892 * tcp->tcp_tcp_hdr_len doesn't change here. 10893 */ 10894 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10895 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10896 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10897 10898 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10899 10900 tcp->tcp_ip6h->ip6_src = src; 10901 tcp->tcp_ip6h->ip6_dst = dst; 10902 10903 /* 10904 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10905 * the default value for TCP. 10906 */ 10907 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10908 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10909 10910 /* 10911 * If we're setting extension headers after a connection 10912 * has been established, and if we have a routing header 10913 * among the extension headers, call ip_massage_options_v6 to 10914 * manipulate the routing header/ip6_dst set the checksum 10915 * difference in the tcp header template. 10916 * (This happens in tcp_connect_ipv6 if the routing header 10917 * is set prior to the connect.) 10918 * Set the tcp_sum to zero first in case we've cleared a 10919 * routing header or don't have one at all. 10920 */ 10921 tcp->tcp_sum = 0; 10922 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10923 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10924 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10925 (uint8_t *)tcp->tcp_tcph); 10926 if (rth != NULL) { 10927 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10928 rth, tcps->tcps_netstack); 10929 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10930 (tcp->tcp_sum >> 16)); 10931 } 10932 } 10933 10934 /* Try to get everything in a single mblk */ 10935 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10936 hdrs_len + tcps->tcps_wroff_xtra); 10937 return (0); 10938 } 10939 10940 /* 10941 * Transfer any source route option from ipha to buf/dst in reversed form. 10942 */ 10943 static int 10944 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10945 { 10946 ipoptp_t opts; 10947 uchar_t *opt; 10948 uint8_t optval; 10949 uint8_t optlen; 10950 uint32_t len = 0; 10951 10952 for (optval = ipoptp_first(&opts, ipha); 10953 optval != IPOPT_EOL; 10954 optval = ipoptp_next(&opts)) { 10955 opt = opts.ipoptp_cur; 10956 optlen = opts.ipoptp_len; 10957 switch (optval) { 10958 int off1, off2; 10959 case IPOPT_SSRR: 10960 case IPOPT_LSRR: 10961 10962 /* Reverse source route */ 10963 /* 10964 * First entry should be the next to last one in the 10965 * current source route (the last entry is our 10966 * address.) 10967 * The last entry should be the final destination. 10968 */ 10969 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10970 buf[IPOPT_OLEN] = (uint8_t)optlen; 10971 off1 = IPOPT_MINOFF_SR - 1; 10972 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10973 if (off2 < 0) { 10974 /* No entries in source route */ 10975 break; 10976 } 10977 bcopy(opt + off2, dst, IP_ADDR_LEN); 10978 /* 10979 * Note: use src since ipha has not had its src 10980 * and dst reversed (it is in the state it was 10981 * received. 10982 */ 10983 bcopy(&ipha->ipha_src, buf + off2, 10984 IP_ADDR_LEN); 10985 off2 -= IP_ADDR_LEN; 10986 10987 while (off2 > 0) { 10988 bcopy(opt + off2, buf + off1, 10989 IP_ADDR_LEN); 10990 off1 += IP_ADDR_LEN; 10991 off2 -= IP_ADDR_LEN; 10992 } 10993 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10994 buf += optlen; 10995 len += optlen; 10996 break; 10997 } 10998 } 10999 done: 11000 /* Pad the resulting options */ 11001 while (len & 0x3) { 11002 *buf++ = IPOPT_EOL; 11003 len++; 11004 } 11005 return (len); 11006 } 11007 11008 11009 /* 11010 * Extract and revert a source route from ipha (if any) 11011 * and then update the relevant fields in both tcp_t and the standard header. 11012 */ 11013 static void 11014 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11015 { 11016 char buf[TCP_MAX_HDR_LENGTH]; 11017 uint_t tcph_len; 11018 int len; 11019 11020 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11021 len = IPH_HDR_LENGTH(ipha); 11022 if (len == IP_SIMPLE_HDR_LENGTH) 11023 /* Nothing to do */ 11024 return; 11025 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11026 (len & 0x3)) 11027 return; 11028 11029 tcph_len = tcp->tcp_tcp_hdr_len; 11030 bcopy(tcp->tcp_tcph, buf, tcph_len); 11031 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11032 (tcp->tcp_ipha->ipha_dst & 0xffff); 11033 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11034 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11035 len += IP_SIMPLE_HDR_LENGTH; 11036 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11037 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11038 if ((int)tcp->tcp_sum < 0) 11039 tcp->tcp_sum--; 11040 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11041 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11042 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11043 bcopy(buf, tcp->tcp_tcph, tcph_len); 11044 tcp->tcp_ip_hdr_len = len; 11045 tcp->tcp_ipha->ipha_version_and_hdr_length = 11046 (IP_VERSION << 4) | (len >> 2); 11047 len += tcph_len; 11048 tcp->tcp_hdr_len = len; 11049 } 11050 11051 /* 11052 * Copy the standard header into its new location, 11053 * lay in the new options and then update the relevant 11054 * fields in both tcp_t and the standard header. 11055 */ 11056 static int 11057 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11058 { 11059 uint_t tcph_len; 11060 uint8_t *ip_optp; 11061 tcph_t *new_tcph; 11062 tcp_stack_t *tcps = tcp->tcp_tcps; 11063 conn_t *connp = tcp->tcp_connp; 11064 11065 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11066 return (EINVAL); 11067 11068 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11069 return (EINVAL); 11070 11071 if (checkonly) { 11072 /* 11073 * do not really set, just pretend to - T_CHECK 11074 */ 11075 return (0); 11076 } 11077 11078 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11079 if (tcp->tcp_label_len > 0) { 11080 int padlen; 11081 uint8_t opt; 11082 11083 /* convert list termination to no-ops */ 11084 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11085 ip_optp += ip_optp[IPOPT_OLEN]; 11086 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11087 while (--padlen >= 0) 11088 *ip_optp++ = opt; 11089 } 11090 tcph_len = tcp->tcp_tcp_hdr_len; 11091 new_tcph = (tcph_t *)(ip_optp + len); 11092 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11093 tcp->tcp_tcph = new_tcph; 11094 bcopy(ptr, ip_optp, len); 11095 11096 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11097 11098 tcp->tcp_ip_hdr_len = len; 11099 tcp->tcp_ipha->ipha_version_and_hdr_length = 11100 (IP_VERSION << 4) | (len >> 2); 11101 tcp->tcp_hdr_len = len + tcph_len; 11102 if (!TCP_IS_DETACHED(tcp)) { 11103 /* Always allocate room for all options. */ 11104 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11105 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11106 } 11107 return (0); 11108 } 11109 11110 /* Get callback routine passed to nd_load by tcp_param_register */ 11111 /* ARGSUSED */ 11112 static int 11113 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11114 { 11115 tcpparam_t *tcppa = (tcpparam_t *)cp; 11116 11117 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11118 return (0); 11119 } 11120 11121 /* 11122 * Walk through the param array specified registering each element with the 11123 * named dispatch handler. 11124 */ 11125 static boolean_t 11126 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11127 { 11128 for (; cnt-- > 0; tcppa++) { 11129 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11130 if (!nd_load(ndp, tcppa->tcp_param_name, 11131 tcp_param_get, tcp_param_set, 11132 (caddr_t)tcppa)) { 11133 nd_free(ndp); 11134 return (B_FALSE); 11135 } 11136 } 11137 } 11138 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11139 KM_SLEEP); 11140 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11141 sizeof (tcpparam_t)); 11142 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11143 tcp_param_get, tcp_param_set_aligned, 11144 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11145 nd_free(ndp); 11146 return (B_FALSE); 11147 } 11148 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11149 KM_SLEEP); 11150 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11151 sizeof (tcpparam_t)); 11152 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11153 tcp_param_get, tcp_param_set_aligned, 11154 (caddr_t)tcps->tcps_mdt_head_param)) { 11155 nd_free(ndp); 11156 return (B_FALSE); 11157 } 11158 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11159 KM_SLEEP); 11160 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11161 sizeof (tcpparam_t)); 11162 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11163 tcp_param_get, tcp_param_set_aligned, 11164 (caddr_t)tcps->tcps_mdt_tail_param)) { 11165 nd_free(ndp); 11166 return (B_FALSE); 11167 } 11168 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11169 KM_SLEEP); 11170 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11171 sizeof (tcpparam_t)); 11172 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11173 tcp_param_get, tcp_param_set_aligned, 11174 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11175 nd_free(ndp); 11176 return (B_FALSE); 11177 } 11178 if (!nd_load(ndp, "tcp_extra_priv_ports", 11179 tcp_extra_priv_ports_get, NULL, NULL)) { 11180 nd_free(ndp); 11181 return (B_FALSE); 11182 } 11183 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11184 NULL, tcp_extra_priv_ports_add, NULL)) { 11185 nd_free(ndp); 11186 return (B_FALSE); 11187 } 11188 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11189 NULL, tcp_extra_priv_ports_del, NULL)) { 11190 nd_free(ndp); 11191 return (B_FALSE); 11192 } 11193 if (!nd_load(ndp, "tcp_status", tcp_status_report, NULL, 11194 NULL)) { 11195 nd_free(ndp); 11196 return (B_FALSE); 11197 } 11198 if (!nd_load(ndp, "tcp_bind_hash", tcp_bind_hash_report, 11199 NULL, NULL)) { 11200 nd_free(ndp); 11201 return (B_FALSE); 11202 } 11203 if (!nd_load(ndp, "tcp_listen_hash", 11204 tcp_listen_hash_report, NULL, NULL)) { 11205 nd_free(ndp); 11206 return (B_FALSE); 11207 } 11208 if (!nd_load(ndp, "tcp_conn_hash", tcp_conn_hash_report, 11209 NULL, NULL)) { 11210 nd_free(ndp); 11211 return (B_FALSE); 11212 } 11213 if (!nd_load(ndp, "tcp_acceptor_hash", 11214 tcp_acceptor_hash_report, NULL, NULL)) { 11215 nd_free(ndp); 11216 return (B_FALSE); 11217 } 11218 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11219 tcp_1948_phrase_set, NULL)) { 11220 nd_free(ndp); 11221 return (B_FALSE); 11222 } 11223 /* 11224 * Dummy ndd variables - only to convey obsolescence information 11225 * through printing of their name (no get or set routines) 11226 * XXX Remove in future releases ? 11227 */ 11228 if (!nd_load(ndp, 11229 "tcp_close_wait_interval(obsoleted - " 11230 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11231 nd_free(ndp); 11232 return (B_FALSE); 11233 } 11234 return (B_TRUE); 11235 } 11236 11237 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11238 /* ARGSUSED */ 11239 static int 11240 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11241 cred_t *cr) 11242 { 11243 long new_value; 11244 tcpparam_t *tcppa = (tcpparam_t *)cp; 11245 11246 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11247 new_value < tcppa->tcp_param_min || 11248 new_value > tcppa->tcp_param_max) { 11249 return (EINVAL); 11250 } 11251 /* 11252 * Need to make sure new_value is a multiple of 4. If it is not, 11253 * round it up. For future 64 bit requirement, we actually make it 11254 * a multiple of 8. 11255 */ 11256 if (new_value & 0x7) { 11257 new_value = (new_value & ~0x7) + 0x8; 11258 } 11259 tcppa->tcp_param_val = new_value; 11260 return (0); 11261 } 11262 11263 /* Set callback routine passed to nd_load by tcp_param_register */ 11264 /* ARGSUSED */ 11265 static int 11266 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11267 { 11268 long new_value; 11269 tcpparam_t *tcppa = (tcpparam_t *)cp; 11270 11271 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11272 new_value < tcppa->tcp_param_min || 11273 new_value > tcppa->tcp_param_max) { 11274 return (EINVAL); 11275 } 11276 tcppa->tcp_param_val = new_value; 11277 return (0); 11278 } 11279 11280 /* 11281 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11282 * is filled, return as much as we can. The message passed in may be 11283 * multi-part, chained using b_cont. "start" is the starting sequence 11284 * number for this piece. 11285 */ 11286 static mblk_t * 11287 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11288 { 11289 uint32_t end; 11290 mblk_t *mp1; 11291 mblk_t *mp2; 11292 mblk_t *next_mp; 11293 uint32_t u1; 11294 tcp_stack_t *tcps = tcp->tcp_tcps; 11295 11296 /* Walk through all the new pieces. */ 11297 do { 11298 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11299 (uintptr_t)INT_MAX); 11300 end = start + (int)(mp->b_wptr - mp->b_rptr); 11301 next_mp = mp->b_cont; 11302 if (start == end) { 11303 /* Empty. Blast it. */ 11304 freeb(mp); 11305 continue; 11306 } 11307 mp->b_cont = NULL; 11308 TCP_REASS_SET_SEQ(mp, start); 11309 TCP_REASS_SET_END(mp, end); 11310 mp1 = tcp->tcp_reass_tail; 11311 if (!mp1) { 11312 tcp->tcp_reass_tail = mp; 11313 tcp->tcp_reass_head = mp; 11314 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11315 UPDATE_MIB(&tcps->tcps_mib, 11316 tcpInDataUnorderBytes, end - start); 11317 continue; 11318 } 11319 /* New stuff completely beyond tail? */ 11320 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11321 /* Link it on end. */ 11322 mp1->b_cont = mp; 11323 tcp->tcp_reass_tail = mp; 11324 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11325 UPDATE_MIB(&tcps->tcps_mib, 11326 tcpInDataUnorderBytes, end - start); 11327 continue; 11328 } 11329 mp1 = tcp->tcp_reass_head; 11330 u1 = TCP_REASS_SEQ(mp1); 11331 /* New stuff at the front? */ 11332 if (SEQ_LT(start, u1)) { 11333 /* Yes... Check for overlap. */ 11334 mp->b_cont = mp1; 11335 tcp->tcp_reass_head = mp; 11336 tcp_reass_elim_overlap(tcp, mp); 11337 continue; 11338 } 11339 /* 11340 * The new piece fits somewhere between the head and tail. 11341 * We find our slot, where mp1 precedes us and mp2 trails. 11342 */ 11343 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11344 u1 = TCP_REASS_SEQ(mp2); 11345 if (SEQ_LEQ(start, u1)) 11346 break; 11347 } 11348 /* Link ourselves in */ 11349 mp->b_cont = mp2; 11350 mp1->b_cont = mp; 11351 11352 /* Trim overlap with following mblk(s) first */ 11353 tcp_reass_elim_overlap(tcp, mp); 11354 11355 /* Trim overlap with preceding mblk */ 11356 tcp_reass_elim_overlap(tcp, mp1); 11357 11358 } while (start = end, mp = next_mp); 11359 mp1 = tcp->tcp_reass_head; 11360 /* Anything ready to go? */ 11361 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11362 return (NULL); 11363 /* Eat what we can off the queue */ 11364 for (;;) { 11365 mp = mp1->b_cont; 11366 end = TCP_REASS_END(mp1); 11367 TCP_REASS_SET_SEQ(mp1, 0); 11368 TCP_REASS_SET_END(mp1, 0); 11369 if (!mp) { 11370 tcp->tcp_reass_tail = NULL; 11371 break; 11372 } 11373 if (end != TCP_REASS_SEQ(mp)) { 11374 mp1->b_cont = NULL; 11375 break; 11376 } 11377 mp1 = mp; 11378 } 11379 mp1 = tcp->tcp_reass_head; 11380 tcp->tcp_reass_head = mp; 11381 return (mp1); 11382 } 11383 11384 /* Eliminate any overlap that mp may have over later mblks */ 11385 static void 11386 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11387 { 11388 uint32_t end; 11389 mblk_t *mp1; 11390 uint32_t u1; 11391 tcp_stack_t *tcps = tcp->tcp_tcps; 11392 11393 end = TCP_REASS_END(mp); 11394 while ((mp1 = mp->b_cont) != NULL) { 11395 u1 = TCP_REASS_SEQ(mp1); 11396 if (!SEQ_GT(end, u1)) 11397 break; 11398 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11399 mp->b_wptr -= end - u1; 11400 TCP_REASS_SET_END(mp, u1); 11401 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11402 UPDATE_MIB(&tcps->tcps_mib, 11403 tcpInDataPartDupBytes, end - u1); 11404 break; 11405 } 11406 mp->b_cont = mp1->b_cont; 11407 TCP_REASS_SET_SEQ(mp1, 0); 11408 TCP_REASS_SET_END(mp1, 0); 11409 freeb(mp1); 11410 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11411 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11412 } 11413 if (!mp1) 11414 tcp->tcp_reass_tail = mp; 11415 } 11416 11417 static uint_t 11418 tcp_rwnd_reopen(tcp_t *tcp) 11419 { 11420 uint_t ret = 0; 11421 uint_t thwin; 11422 11423 /* Learn the latest rwnd information that we sent to the other side. */ 11424 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11425 << tcp->tcp_rcv_ws; 11426 /* This is peer's calculated send window (our receive window). */ 11427 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11428 /* 11429 * Increase the receive window to max. But we need to do receiver 11430 * SWS avoidance. This means that we need to check the increase of 11431 * of receive window is at least 1 MSS. 11432 */ 11433 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11434 /* 11435 * If the window that the other side knows is less than max 11436 * deferred acks segments, send an update immediately. 11437 */ 11438 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11439 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11440 ret = TH_ACK_NEEDED; 11441 } 11442 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11443 } 11444 return (ret); 11445 } 11446 11447 /* 11448 * Send up all messages queued on tcp_rcv_list. 11449 */ 11450 static uint_t 11451 tcp_rcv_drain(tcp_t *tcp) 11452 { 11453 mblk_t *mp; 11454 uint_t ret = 0; 11455 #ifdef DEBUG 11456 uint_t cnt = 0; 11457 #endif 11458 queue_t *q = tcp->tcp_rq; 11459 11460 /* Can't drain on an eager connection */ 11461 if (tcp->tcp_listener != NULL) 11462 return (ret); 11463 11464 /* Can't be a non-STREAMS connection or sodirect enabled */ 11465 ASSERT((!IPCL_IS_NONSTR(tcp->tcp_connp)) && SOD_NOT_ENABLED(tcp)); 11466 11467 /* No need for the push timer now. */ 11468 if (tcp->tcp_push_tid != 0) { 11469 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11470 tcp->tcp_push_tid = 0; 11471 } 11472 11473 /* 11474 * Handle two cases here: we are currently fused or we were 11475 * previously fused and have some urgent data to be delivered 11476 * upstream. The latter happens because we either ran out of 11477 * memory or were detached and therefore sending the SIGURG was 11478 * deferred until this point. In either case we pass control 11479 * over to tcp_fuse_rcv_drain() since it may need to complete 11480 * some work. 11481 */ 11482 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11483 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11484 tcp->tcp_fused_sigurg_mp != NULL); 11485 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11486 &tcp->tcp_fused_sigurg_mp)) 11487 return (ret); 11488 } 11489 11490 while ((mp = tcp->tcp_rcv_list) != NULL) { 11491 tcp->tcp_rcv_list = mp->b_next; 11492 mp->b_next = NULL; 11493 #ifdef DEBUG 11494 cnt += msgdsize(mp); 11495 #endif 11496 /* Does this need SSL processing first? */ 11497 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11498 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11499 mblk_t *, mp); 11500 tcp_kssl_input(tcp, mp); 11501 continue; 11502 } 11503 putnext(q, mp); 11504 } 11505 #ifdef DEBUG 11506 ASSERT(cnt == tcp->tcp_rcv_cnt); 11507 #endif 11508 tcp->tcp_rcv_last_head = NULL; 11509 tcp->tcp_rcv_last_tail = NULL; 11510 tcp->tcp_rcv_cnt = 0; 11511 11512 if (canputnext(q)) 11513 return (tcp_rwnd_reopen(tcp)); 11514 11515 return (ret); 11516 } 11517 11518 /* 11519 * Queue data on tcp_rcv_list which is a b_next chain. 11520 * tcp_rcv_last_head/tail is the last element of this chain. 11521 * Each element of the chain is a b_cont chain. 11522 * 11523 * M_DATA messages are added to the current element. 11524 * Other messages are added as new (b_next) elements. 11525 */ 11526 void 11527 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11528 { 11529 ASSERT(seg_len == msgdsize(mp)); 11530 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11531 11532 if (tcp->tcp_rcv_list == NULL) { 11533 ASSERT(tcp->tcp_rcv_last_head == NULL); 11534 tcp->tcp_rcv_list = mp; 11535 tcp->tcp_rcv_last_head = mp; 11536 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11537 tcp->tcp_rcv_last_tail->b_cont = mp; 11538 } else { 11539 tcp->tcp_rcv_last_head->b_next = mp; 11540 tcp->tcp_rcv_last_head = mp; 11541 } 11542 11543 while (mp->b_cont) 11544 mp = mp->b_cont; 11545 11546 tcp->tcp_rcv_last_tail = mp; 11547 tcp->tcp_rcv_cnt += seg_len; 11548 tcp->tcp_rwnd -= seg_len; 11549 } 11550 11551 /* 11552 * The tcp_rcv_sod_XXX() functions enqueue data directly to the socket 11553 * above, in addition when uioa is enabled schedule an asynchronous uio 11554 * prior to enqueuing. They implement the combinhed semantics of the 11555 * tcp_rcv_XXX() functions, tcp_rcv_list push logic, and STREAMS putnext() 11556 * canputnext(), i.e. flow-control with backenable. 11557 * 11558 * tcp_sod_wakeup() is called where tcp_rcv_drain() would be called in the 11559 * non sodirect connection but as there are no tcp_tcv_list mblk_t's we deal 11560 * with the rcv_wnd and push timer and call the sodirect wakeup function. 11561 * 11562 * Must be called with sodp->sod_lockp held and will return with the lock 11563 * released. 11564 */ 11565 static uint_t 11566 tcp_rcv_sod_wakeup(tcp_t *tcp, sodirect_t *sodp) 11567 { 11568 queue_t *q = tcp->tcp_rq; 11569 uint_t thwin; 11570 tcp_stack_t *tcps = tcp->tcp_tcps; 11571 uint_t ret = 0; 11572 11573 /* Can't be an eager connection */ 11574 ASSERT(tcp->tcp_listener == NULL); 11575 11576 /* Caller must have lock held */ 11577 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11578 11579 /* Sodirect mode so must not be a tcp_rcv_list */ 11580 ASSERT(tcp->tcp_rcv_list == NULL); 11581 11582 if (SOD_QFULL(sodp)) { 11583 /* Q is full, mark Q for need backenable */ 11584 SOD_QSETBE(sodp); 11585 } 11586 /* Last advertised rwnd, i.e. rwnd last sent in a packet */ 11587 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11588 << tcp->tcp_rcv_ws; 11589 /* This is peer's calculated send window (our available rwnd). */ 11590 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11591 /* 11592 * Increase the receive window to max. But we need to do receiver 11593 * SWS avoidance. This means that we need to check the increase of 11594 * of receive window is at least 1 MSS. 11595 */ 11596 if (!SOD_QFULL(sodp) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11597 /* 11598 * If the window that the other side knows is less than max 11599 * deferred acks segments, send an update immediately. 11600 */ 11601 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11602 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 11603 ret = TH_ACK_NEEDED; 11604 } 11605 tcp->tcp_rwnd = q->q_hiwat; 11606 } 11607 11608 if (!SOD_QEMPTY(sodp)) { 11609 /* Wakeup to socket */ 11610 sodp->sod_state &= SOD_WAKE_CLR; 11611 sodp->sod_state |= SOD_WAKE_DONE; 11612 (sodp->sod_wakeup)(sodp); 11613 /* wakeup() does the mutex_ext() */ 11614 } else { 11615 /* Q is empty, no need to wake */ 11616 sodp->sod_state &= SOD_WAKE_CLR; 11617 sodp->sod_state |= SOD_WAKE_NOT; 11618 mutex_exit(sodp->sod_lockp); 11619 } 11620 11621 /* No need for the push timer now. */ 11622 if (tcp->tcp_push_tid != 0) { 11623 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11624 tcp->tcp_push_tid = 0; 11625 } 11626 11627 return (ret); 11628 } 11629 11630 /* 11631 * Called where tcp_rcv_enqueue()/putnext(RD(q)) would be. For M_DATA 11632 * mblk_t's if uioa enabled then start a uioa asynchronous copy directly 11633 * to the user-land buffer and flag the mblk_t as such. 11634 * 11635 * Also, handle tcp_rwnd. 11636 */ 11637 uint_t 11638 tcp_rcv_sod_enqueue(tcp_t *tcp, sodirect_t *sodp, mblk_t *mp, uint_t seg_len) 11639 { 11640 uioa_t *uioap = &sodp->sod_uioa; 11641 boolean_t qfull; 11642 uint_t thwin; 11643 11644 /* Can't be an eager connection */ 11645 ASSERT(tcp->tcp_listener == NULL); 11646 11647 /* Caller must have lock held */ 11648 ASSERT(MUTEX_HELD(sodp->sod_lockp)); 11649 11650 /* Sodirect mode so must not be a tcp_rcv_list */ 11651 ASSERT(tcp->tcp_rcv_list == NULL); 11652 11653 /* Passed in segment length must be equal to mblk_t chain data size */ 11654 ASSERT(seg_len == msgdsize(mp)); 11655 11656 if (DB_TYPE(mp) != M_DATA) { 11657 /* Only process M_DATA mblk_t's */ 11658 goto enq; 11659 } 11660 if (uioap->uioa_state & UIOA_ENABLED) { 11661 /* Uioa is enabled */ 11662 mblk_t *mp1 = mp; 11663 mblk_t *lmp = NULL; 11664 11665 if (seg_len > uioap->uio_resid) { 11666 /* 11667 * There isn't enough uio space for the mblk_t chain 11668 * so disable uioa such that this and any additional 11669 * mblk_t data is handled by the socket and schedule 11670 * the socket for wakeup to finish this uioa. 11671 */ 11672 uioap->uioa_state &= UIOA_CLR; 11673 uioap->uioa_state |= UIOA_FINI; 11674 if (sodp->sod_state & SOD_WAKE_NOT) { 11675 sodp->sod_state &= SOD_WAKE_CLR; 11676 sodp->sod_state |= SOD_WAKE_NEED; 11677 } 11678 goto enq; 11679 } 11680 do { 11681 uint32_t len = MBLKL(mp1); 11682 11683 if (!uioamove(mp1->b_rptr, len, UIO_READ, uioap)) { 11684 /* Scheduled, mark dblk_t as such */ 11685 DB_FLAGS(mp1) |= DBLK_UIOA; 11686 } else { 11687 /* Error, turn off async processing */ 11688 uioap->uioa_state &= UIOA_CLR; 11689 uioap->uioa_state |= UIOA_FINI; 11690 break; 11691 } 11692 lmp = mp1; 11693 } while ((mp1 = mp1->b_cont) != NULL); 11694 11695 if (mp1 != NULL || uioap->uio_resid == 0) { 11696 /* 11697 * Not all mblk_t(s) uioamoved (error) or all uio 11698 * space has been consumed so schedule the socket 11699 * for wakeup to finish this uio. 11700 */ 11701 sodp->sod_state &= SOD_WAKE_CLR; 11702 sodp->sod_state |= SOD_WAKE_NEED; 11703 11704 /* Break the mblk chain if neccessary. */ 11705 if (mp1 != NULL && lmp != NULL) { 11706 mp->b_next = mp1; 11707 lmp->b_cont = NULL; 11708 } 11709 } 11710 } else if (uioap->uioa_state & UIOA_FINI) { 11711 /* 11712 * Post UIO_ENABLED waiting for socket to finish processing 11713 * so just enqueue and update tcp_rwnd. 11714 */ 11715 if (SOD_QFULL(sodp)) 11716 tcp->tcp_rwnd -= seg_len; 11717 } else if (sodp->sod_want > 0) { 11718 /* 11719 * Uioa isn't enabled but sodirect has a pending read(). 11720 */ 11721 if (SOD_QCNT(sodp) + seg_len >= sodp->sod_want) { 11722 if (sodp->sod_state & SOD_WAKE_NOT) { 11723 /* Schedule socket for wakeup */ 11724 sodp->sod_state &= SOD_WAKE_CLR; 11725 sodp->sod_state |= SOD_WAKE_NEED; 11726 } 11727 tcp->tcp_rwnd -= seg_len; 11728 } 11729 } else if (SOD_QCNT(sodp) + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 11730 /* 11731 * No pending sodirect read() so used the default 11732 * TCP push logic to guess that a push is needed. 11733 */ 11734 if (sodp->sod_state & SOD_WAKE_NOT) { 11735 /* Schedule socket for wakeup */ 11736 sodp->sod_state &= SOD_WAKE_CLR; 11737 sodp->sod_state |= SOD_WAKE_NEED; 11738 } 11739 tcp->tcp_rwnd -= seg_len; 11740 } else { 11741 /* Just update tcp_rwnd */ 11742 tcp->tcp_rwnd -= seg_len; 11743 } 11744 enq: 11745 qfull = SOD_QFULL(sodp); 11746 11747 (sodp->sod_enqueue)(sodp, mp); 11748 11749 if (! qfull && SOD_QFULL(sodp)) { 11750 /* Wasn't QFULL, now QFULL, need back-enable */ 11751 SOD_QSETBE(sodp); 11752 } 11753 11754 /* 11755 * Check to see if remote avail swnd < mss due to delayed ACK, 11756 * first get advertised rwnd. 11757 */ 11758 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)); 11759 /* Minus delayed ACK count */ 11760 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11761 if (thwin < tcp->tcp_mss) { 11762 /* Remote avail swnd < mss, need ACK now */ 11763 return (TH_ACK_NEEDED); 11764 } 11765 11766 return (0); 11767 } 11768 11769 /* 11770 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11771 * 11772 * This is the default entry function into TCP on the read side. TCP is 11773 * always entered via squeue i.e. using squeue's for mutual exclusion. 11774 * When classifier does a lookup to find the tcp, it also puts a reference 11775 * on the conn structure associated so the tcp is guaranteed to exist 11776 * when we come here. We still need to check the state because it might 11777 * as well has been closed. The squeue processing function i.e. squeue_enter, 11778 * is responsible for doing the CONN_DEC_REF. 11779 * 11780 * Apart from the default entry point, IP also sends packets directly to 11781 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11782 * connections. 11783 */ 11784 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11785 void 11786 tcp_input(void *arg, mblk_t *mp, void *arg2) 11787 { 11788 conn_t *connp = (conn_t *)arg; 11789 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11790 11791 /* arg2 is the sqp */ 11792 ASSERT(arg2 != NULL); 11793 ASSERT(mp != NULL); 11794 11795 /* 11796 * Don't accept any input on a closed tcp as this TCP logically does 11797 * not exist on the system. Don't proceed further with this TCP. 11798 * For eg. this packet could trigger another close of this tcp 11799 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11800 * tcp_clean_death / tcp_closei_local must be called at most once 11801 * on a TCP. In this case we need to refeed the packet into the 11802 * classifier and figure out where the packet should go. Need to 11803 * preserve the recv_ill somehow. Until we figure that out, for 11804 * now just drop the packet if we can't classify the packet. 11805 */ 11806 if (tcp->tcp_state == TCPS_CLOSED || 11807 tcp->tcp_state == TCPS_BOUND) { 11808 conn_t *new_connp; 11809 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11810 11811 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11812 if (new_connp != NULL) { 11813 tcp_reinput(new_connp, mp, arg2); 11814 return; 11815 } 11816 /* We failed to classify. For now just drop the packet */ 11817 freemsg(mp); 11818 return; 11819 } 11820 11821 if (DB_TYPE(mp) != M_DATA) { 11822 tcp_rput_common(tcp, mp); 11823 return; 11824 } 11825 11826 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11827 squeue_t *final_sqp; 11828 11829 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11830 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11831 DB_CKSUMSTART(mp) = 0; 11832 if (tcp->tcp_state == TCPS_SYN_SENT && 11833 connp->conn_final_sqp == NULL && 11834 tcp_outbound_squeue_switch) { 11835 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11836 connp->conn_final_sqp = final_sqp; 11837 if (connp->conn_final_sqp != connp->conn_sqp) { 11838 CONN_INC_REF(connp); 11839 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11840 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11841 tcp_rput_data, connp, ip_squeue_flag, 11842 SQTAG_CONNECT_FINISH); 11843 return; 11844 } 11845 } 11846 } 11847 tcp_rput_data(connp, mp, arg2); 11848 } 11849 11850 /* 11851 * The read side put procedure. 11852 * The packets passed up by ip are assume to be aligned according to 11853 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11854 */ 11855 static void 11856 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11857 { 11858 /* 11859 * tcp_rput_data() does not expect M_CTL except for the case 11860 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11861 * type. Need to make sure that any other M_CTLs don't make 11862 * it to tcp_rput_data since it is not expecting any and doesn't 11863 * check for it. 11864 */ 11865 if (DB_TYPE(mp) == M_CTL) { 11866 switch (*(uint32_t *)(mp->b_rptr)) { 11867 case TCP_IOC_ABORT_CONN: 11868 /* 11869 * Handle connection abort request. 11870 */ 11871 tcp_ioctl_abort_handler(tcp, mp); 11872 return; 11873 case IPSEC_IN: 11874 /* 11875 * Only secure icmp arrive in TCP and they 11876 * don't go through data path. 11877 */ 11878 tcp_icmp_error(tcp, mp); 11879 return; 11880 case IN_PKTINFO: 11881 /* 11882 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11883 * sockets that are receiving IPv4 traffic. tcp 11884 */ 11885 ASSERT(tcp->tcp_family == AF_INET6); 11886 ASSERT(tcp->tcp_ipv6_recvancillary & 11887 TCP_IPV6_RECVPKTINFO); 11888 tcp_rput_data(tcp->tcp_connp, mp, 11889 tcp->tcp_connp->conn_sqp); 11890 return; 11891 case MDT_IOC_INFO_UPDATE: 11892 /* 11893 * Handle Multidata information update; the 11894 * following routine will free the message. 11895 */ 11896 if (tcp->tcp_connp->conn_mdt_ok) { 11897 tcp_mdt_update(tcp, 11898 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11899 B_FALSE); 11900 } 11901 freemsg(mp); 11902 return; 11903 case LSO_IOC_INFO_UPDATE: 11904 /* 11905 * Handle LSO information update; the following 11906 * routine will free the message. 11907 */ 11908 if (tcp->tcp_connp->conn_lso_ok) { 11909 tcp_lso_update(tcp, 11910 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11911 } 11912 freemsg(mp); 11913 return; 11914 default: 11915 /* 11916 * tcp_icmp_err() will process the M_CTL packets. 11917 * Non-ICMP packets, if any, will be discarded in 11918 * tcp_icmp_err(). We will process the ICMP packet 11919 * even if we are TCP_IS_DETACHED_NONEAGER as the 11920 * incoming ICMP packet may result in changing 11921 * the tcp_mss, which we would need if we have 11922 * packets to retransmit. 11923 */ 11924 tcp_icmp_error(tcp, mp); 11925 return; 11926 } 11927 } 11928 11929 /* No point processing the message if tcp is already closed */ 11930 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11931 freemsg(mp); 11932 return; 11933 } 11934 11935 tcp_rput_other(tcp, mp); 11936 } 11937 11938 11939 /* The minimum of smoothed mean deviation in RTO calculation. */ 11940 #define TCP_SD_MIN 400 11941 11942 /* 11943 * Set RTO for this connection. The formula is from Jacobson and Karels' 11944 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11945 * are the same as those in Appendix A.2 of that paper. 11946 * 11947 * m = new measurement 11948 * sa = smoothed RTT average (8 * average estimates). 11949 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11950 */ 11951 static void 11952 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11953 { 11954 long m = TICK_TO_MSEC(rtt); 11955 clock_t sa = tcp->tcp_rtt_sa; 11956 clock_t sv = tcp->tcp_rtt_sd; 11957 clock_t rto; 11958 tcp_stack_t *tcps = tcp->tcp_tcps; 11959 11960 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11961 tcp->tcp_rtt_update++; 11962 11963 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11964 if (sa != 0) { 11965 /* 11966 * Update average estimator: 11967 * new rtt = 7/8 old rtt + 1/8 Error 11968 */ 11969 11970 /* m is now Error in estimate. */ 11971 m -= sa >> 3; 11972 if ((sa += m) <= 0) { 11973 /* 11974 * Don't allow the smoothed average to be negative. 11975 * We use 0 to denote reinitialization of the 11976 * variables. 11977 */ 11978 sa = 1; 11979 } 11980 11981 /* 11982 * Update deviation estimator: 11983 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11984 */ 11985 if (m < 0) 11986 m = -m; 11987 m -= sv >> 2; 11988 sv += m; 11989 } else { 11990 /* 11991 * This follows BSD's implementation. So the reinitialized 11992 * RTO is 3 * m. We cannot go less than 2 because if the 11993 * link is bandwidth dominated, doubling the window size 11994 * during slow start means doubling the RTT. We want to be 11995 * more conservative when we reinitialize our estimates. 3 11996 * is just a convenient number. 11997 */ 11998 sa = m << 3; 11999 sv = m << 1; 12000 } 12001 if (sv < TCP_SD_MIN) { 12002 /* 12003 * We do not know that if sa captures the delay ACK 12004 * effect as in a long train of segments, a receiver 12005 * does not delay its ACKs. So set the minimum of sv 12006 * to be TCP_SD_MIN, which is default to 400 ms, twice 12007 * of BSD DATO. That means the minimum of mean 12008 * deviation is 100 ms. 12009 * 12010 */ 12011 sv = TCP_SD_MIN; 12012 } 12013 tcp->tcp_rtt_sa = sa; 12014 tcp->tcp_rtt_sd = sv; 12015 /* 12016 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 12017 * 12018 * Add tcp_rexmit_interval extra in case of extreme environment 12019 * where the algorithm fails to work. The default value of 12020 * tcp_rexmit_interval_extra should be 0. 12021 * 12022 * As we use a finer grained clock than BSD and update 12023 * RTO for every ACKs, add in another .25 of RTT to the 12024 * deviation of RTO to accomodate burstiness of 1/4 of 12025 * window size. 12026 */ 12027 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 12028 12029 if (rto > tcps->tcps_rexmit_interval_max) { 12030 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 12031 } else if (rto < tcps->tcps_rexmit_interval_min) { 12032 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 12033 } else { 12034 tcp->tcp_rto = rto; 12035 } 12036 12037 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 12038 tcp->tcp_timer_backoff = 0; 12039 } 12040 12041 /* 12042 * tcp_get_seg_mp() is called to get the pointer to a segment in the 12043 * send queue which starts at the given seq. no. 12044 * 12045 * Parameters: 12046 * tcp_t *tcp: the tcp instance pointer. 12047 * uint32_t seq: the starting seq. no of the requested segment. 12048 * int32_t *off: after the execution, *off will be the offset to 12049 * the returned mblk which points to the requested seq no. 12050 * It is the caller's responsibility to send in a non-null off. 12051 * 12052 * Return: 12053 * A mblk_t pointer pointing to the requested segment in send queue. 12054 */ 12055 static mblk_t * 12056 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 12057 { 12058 int32_t cnt; 12059 mblk_t *mp; 12060 12061 /* Defensive coding. Make sure we don't send incorrect data. */ 12062 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 12063 return (NULL); 12064 12065 cnt = seq - tcp->tcp_suna; 12066 mp = tcp->tcp_xmit_head; 12067 while (cnt > 0 && mp != NULL) { 12068 cnt -= mp->b_wptr - mp->b_rptr; 12069 if (cnt < 0) { 12070 cnt += mp->b_wptr - mp->b_rptr; 12071 break; 12072 } 12073 mp = mp->b_cont; 12074 } 12075 ASSERT(mp != NULL); 12076 *off = cnt; 12077 return (mp); 12078 } 12079 12080 /* 12081 * This function handles all retransmissions if SACK is enabled for this 12082 * connection. First it calculates how many segments can be retransmitted 12083 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 12084 * segments. A segment is eligible if sack_cnt for that segment is greater 12085 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 12086 * all eligible segments, it checks to see if TCP can send some new segments 12087 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 12088 * 12089 * Parameters: 12090 * tcp_t *tcp: the tcp structure of the connection. 12091 * uint_t *flags: in return, appropriate value will be set for 12092 * tcp_rput_data(). 12093 */ 12094 static void 12095 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 12096 { 12097 notsack_blk_t *notsack_blk; 12098 int32_t usable_swnd; 12099 int32_t mss; 12100 uint32_t seg_len; 12101 mblk_t *xmit_mp; 12102 tcp_stack_t *tcps = tcp->tcp_tcps; 12103 12104 ASSERT(tcp->tcp_sack_info != NULL); 12105 ASSERT(tcp->tcp_notsack_list != NULL); 12106 ASSERT(tcp->tcp_rexmit == B_FALSE); 12107 12108 /* Defensive coding in case there is a bug... */ 12109 if (tcp->tcp_notsack_list == NULL) { 12110 return; 12111 } 12112 notsack_blk = tcp->tcp_notsack_list; 12113 mss = tcp->tcp_mss; 12114 12115 /* 12116 * Limit the num of outstanding data in the network to be 12117 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 12118 */ 12119 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12120 12121 /* At least retransmit 1 MSS of data. */ 12122 if (usable_swnd <= 0) { 12123 usable_swnd = mss; 12124 } 12125 12126 /* Make sure no new RTT samples will be taken. */ 12127 tcp->tcp_csuna = tcp->tcp_snxt; 12128 12129 notsack_blk = tcp->tcp_notsack_list; 12130 while (usable_swnd > 0) { 12131 mblk_t *snxt_mp, *tmp_mp; 12132 tcp_seq begin = tcp->tcp_sack_snxt; 12133 tcp_seq end; 12134 int32_t off; 12135 12136 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 12137 if (SEQ_GT(notsack_blk->end, begin) && 12138 (notsack_blk->sack_cnt >= 12139 tcps->tcps_dupack_fast_retransmit)) { 12140 end = notsack_blk->end; 12141 if (SEQ_LT(begin, notsack_blk->begin)) { 12142 begin = notsack_blk->begin; 12143 } 12144 break; 12145 } 12146 } 12147 /* 12148 * All holes are filled. Manipulate tcp_cwnd to send more 12149 * if we can. Note that after the SACK recovery, tcp_cwnd is 12150 * set to tcp_cwnd_ssthresh. 12151 */ 12152 if (notsack_blk == NULL) { 12153 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 12154 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 12155 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 12156 ASSERT(tcp->tcp_cwnd > 0); 12157 return; 12158 } else { 12159 usable_swnd = usable_swnd / mss; 12160 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 12161 MAX(usable_swnd * mss, mss); 12162 *flags |= TH_XMIT_NEEDED; 12163 return; 12164 } 12165 } 12166 12167 /* 12168 * Note that we may send more than usable_swnd allows here 12169 * because of round off, but no more than 1 MSS of data. 12170 */ 12171 seg_len = end - begin; 12172 if (seg_len > mss) 12173 seg_len = mss; 12174 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 12175 ASSERT(snxt_mp != NULL); 12176 /* This should not happen. Defensive coding again... */ 12177 if (snxt_mp == NULL) { 12178 return; 12179 } 12180 12181 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 12182 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 12183 if (xmit_mp == NULL) 12184 return; 12185 12186 usable_swnd -= seg_len; 12187 tcp->tcp_pipe += seg_len; 12188 tcp->tcp_sack_snxt = begin + seg_len; 12189 12190 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12191 12192 /* 12193 * Update the send timestamp to avoid false retransmission. 12194 */ 12195 snxt_mp->b_prev = (mblk_t *)lbolt; 12196 12197 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12198 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 12199 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 12200 /* 12201 * Update tcp_rexmit_max to extend this SACK recovery phase. 12202 * This happens when new data sent during fast recovery is 12203 * also lost. If TCP retransmits those new data, it needs 12204 * to extend SACK recover phase to avoid starting another 12205 * fast retransmit/recovery unnecessarily. 12206 */ 12207 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 12208 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 12209 } 12210 } 12211 } 12212 12213 /* 12214 * This function handles policy checking at TCP level for non-hard_bound/ 12215 * detached connections. 12216 */ 12217 static boolean_t 12218 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12219 boolean_t secure, boolean_t mctl_present) 12220 { 12221 ipsec_latch_t *ipl = NULL; 12222 ipsec_action_t *act = NULL; 12223 mblk_t *data_mp; 12224 ipsec_in_t *ii; 12225 const char *reason; 12226 kstat_named_t *counter; 12227 tcp_stack_t *tcps = tcp->tcp_tcps; 12228 ipsec_stack_t *ipss; 12229 ip_stack_t *ipst; 12230 12231 ASSERT(mctl_present || !secure); 12232 12233 ASSERT((ipha == NULL && ip6h != NULL) || 12234 (ip6h == NULL && ipha != NULL)); 12235 12236 /* 12237 * We don't necessarily have an ipsec_in_act action to verify 12238 * policy because of assymetrical policy where we have only 12239 * outbound policy and no inbound policy (possible with global 12240 * policy). 12241 */ 12242 if (!secure) { 12243 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12244 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12245 return (B_TRUE); 12246 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12247 "tcp_check_policy", ipha, ip6h, secure, 12248 tcps->tcps_netstack); 12249 ipss = tcps->tcps_netstack->netstack_ipsec; 12250 12251 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12252 DROPPER(ipss, ipds_tcp_clear), 12253 &tcps->tcps_dropper); 12254 return (B_FALSE); 12255 } 12256 12257 /* 12258 * We have a secure packet. 12259 */ 12260 if (act == NULL) { 12261 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12262 "tcp_check_policy", ipha, ip6h, secure, 12263 tcps->tcps_netstack); 12264 ipss = tcps->tcps_netstack->netstack_ipsec; 12265 12266 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12267 DROPPER(ipss, ipds_tcp_secure), 12268 &tcps->tcps_dropper); 12269 return (B_FALSE); 12270 } 12271 12272 /* 12273 * XXX This whole routine is currently incorrect. ipl should 12274 * be set to the latch pointer, but is currently not set, so 12275 * we initialize it to NULL to avoid picking up random garbage. 12276 */ 12277 if (ipl == NULL) 12278 return (B_TRUE); 12279 12280 data_mp = first_mp->b_cont; 12281 12282 ii = (ipsec_in_t *)first_mp->b_rptr; 12283 12284 ipst = tcps->tcps_netstack->netstack_ip; 12285 12286 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12287 &counter, tcp->tcp_connp)) { 12288 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12289 return (B_TRUE); 12290 } 12291 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12292 "tcp inbound policy mismatch: %s, packet dropped\n", 12293 reason); 12294 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12295 12296 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12297 &tcps->tcps_dropper); 12298 return (B_FALSE); 12299 } 12300 12301 /* 12302 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12303 * retransmission after a timeout. 12304 * 12305 * To limit the number of duplicate segments, we limit the number of segment 12306 * to be sent in one time to tcp_snd_burst, the burst variable. 12307 */ 12308 static void 12309 tcp_ss_rexmit(tcp_t *tcp) 12310 { 12311 uint32_t snxt; 12312 uint32_t smax; 12313 int32_t win; 12314 int32_t mss; 12315 int32_t off; 12316 int32_t burst = tcp->tcp_snd_burst; 12317 mblk_t *snxt_mp; 12318 tcp_stack_t *tcps = tcp->tcp_tcps; 12319 12320 /* 12321 * Note that tcp_rexmit can be set even though TCP has retransmitted 12322 * all unack'ed segments. 12323 */ 12324 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12325 smax = tcp->tcp_rexmit_max; 12326 snxt = tcp->tcp_rexmit_nxt; 12327 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12328 snxt = tcp->tcp_suna; 12329 } 12330 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12331 win -= snxt - tcp->tcp_suna; 12332 mss = tcp->tcp_mss; 12333 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12334 12335 while (SEQ_LT(snxt, smax) && (win > 0) && 12336 (burst > 0) && (snxt_mp != NULL)) { 12337 mblk_t *xmit_mp; 12338 mblk_t *old_snxt_mp = snxt_mp; 12339 uint32_t cnt = mss; 12340 12341 if (win < cnt) { 12342 cnt = win; 12343 } 12344 if (SEQ_GT(snxt + cnt, smax)) { 12345 cnt = smax - snxt; 12346 } 12347 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12348 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12349 if (xmit_mp == NULL) 12350 return; 12351 12352 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12353 12354 snxt += cnt; 12355 win -= cnt; 12356 /* 12357 * Update the send timestamp to avoid false 12358 * retransmission. 12359 */ 12360 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12361 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12362 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12363 12364 tcp->tcp_rexmit_nxt = snxt; 12365 burst--; 12366 } 12367 /* 12368 * If we have transmitted all we have at the time 12369 * we started the retranmission, we can leave 12370 * the rest of the job to tcp_wput_data(). But we 12371 * need to check the send window first. If the 12372 * win is not 0, go on with tcp_wput_data(). 12373 */ 12374 if (SEQ_LT(snxt, smax) || win == 0) { 12375 return; 12376 } 12377 } 12378 /* Only call tcp_wput_data() if there is data to be sent. */ 12379 if (tcp->tcp_unsent) { 12380 tcp_wput_data(tcp, NULL, B_FALSE); 12381 } 12382 } 12383 12384 /* 12385 * Process all TCP option in SYN segment. Note that this function should 12386 * be called after tcp_adapt_ire() is called so that the necessary info 12387 * from IRE is already set in the tcp structure. 12388 * 12389 * This function sets up the correct tcp_mss value according to the 12390 * MSS option value and our header size. It also sets up the window scale 12391 * and timestamp values, and initialize SACK info blocks. But it does not 12392 * change receive window size after setting the tcp_mss value. The caller 12393 * should do the appropriate change. 12394 */ 12395 void 12396 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12397 { 12398 int options; 12399 tcp_opt_t tcpopt; 12400 uint32_t mss_max; 12401 char *tmp_tcph; 12402 tcp_stack_t *tcps = tcp->tcp_tcps; 12403 12404 tcpopt.tcp = NULL; 12405 options = tcp_parse_options(tcph, &tcpopt); 12406 12407 /* 12408 * Process MSS option. Note that MSS option value does not account 12409 * for IP or TCP options. This means that it is equal to MTU - minimum 12410 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12411 * IPv6. 12412 */ 12413 if (!(options & TCP_OPT_MSS_PRESENT)) { 12414 if (tcp->tcp_ipversion == IPV4_VERSION) 12415 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12416 else 12417 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12418 } else { 12419 if (tcp->tcp_ipversion == IPV4_VERSION) 12420 mss_max = tcps->tcps_mss_max_ipv4; 12421 else 12422 mss_max = tcps->tcps_mss_max_ipv6; 12423 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12424 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12425 else if (tcpopt.tcp_opt_mss > mss_max) 12426 tcpopt.tcp_opt_mss = mss_max; 12427 } 12428 12429 /* Process Window Scale option. */ 12430 if (options & TCP_OPT_WSCALE_PRESENT) { 12431 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12432 tcp->tcp_snd_ws_ok = B_TRUE; 12433 } else { 12434 tcp->tcp_snd_ws = B_FALSE; 12435 tcp->tcp_snd_ws_ok = B_FALSE; 12436 tcp->tcp_rcv_ws = B_FALSE; 12437 } 12438 12439 /* Process Timestamp option. */ 12440 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12441 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12442 tmp_tcph = (char *)tcp->tcp_tcph; 12443 12444 tcp->tcp_snd_ts_ok = B_TRUE; 12445 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12446 tcp->tcp_last_rcv_lbolt = lbolt64; 12447 ASSERT(OK_32PTR(tmp_tcph)); 12448 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12449 12450 /* Fill in our template header with basic timestamp option. */ 12451 tmp_tcph += tcp->tcp_tcp_hdr_len; 12452 tmp_tcph[0] = TCPOPT_NOP; 12453 tmp_tcph[1] = TCPOPT_NOP; 12454 tmp_tcph[2] = TCPOPT_TSTAMP; 12455 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12456 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12457 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12458 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12459 } else { 12460 tcp->tcp_snd_ts_ok = B_FALSE; 12461 } 12462 12463 /* 12464 * Process SACK options. If SACK is enabled for this connection, 12465 * then allocate the SACK info structure. Note the following ways 12466 * when tcp_snd_sack_ok is set to true. 12467 * 12468 * For active connection: in tcp_adapt_ire() called in 12469 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12470 * is checked. 12471 * 12472 * For passive connection: in tcp_adapt_ire() called in 12473 * tcp_accept_comm(). 12474 * 12475 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12476 * That check makes sure that if we did not send a SACK OK option, 12477 * we will not enable SACK for this connection even though the other 12478 * side sends us SACK OK option. For active connection, the SACK 12479 * info structure has already been allocated. So we need to free 12480 * it if SACK is disabled. 12481 */ 12482 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12483 (tcp->tcp_snd_sack_ok || 12484 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12485 /* This should be true only in the passive case. */ 12486 if (tcp->tcp_sack_info == NULL) { 12487 ASSERT(TCP_IS_DETACHED(tcp)); 12488 tcp->tcp_sack_info = 12489 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12490 } 12491 if (tcp->tcp_sack_info == NULL) { 12492 tcp->tcp_snd_sack_ok = B_FALSE; 12493 } else { 12494 tcp->tcp_snd_sack_ok = B_TRUE; 12495 if (tcp->tcp_snd_ts_ok) { 12496 tcp->tcp_max_sack_blk = 3; 12497 } else { 12498 tcp->tcp_max_sack_blk = 4; 12499 } 12500 } 12501 } else { 12502 /* 12503 * Resetting tcp_snd_sack_ok to B_FALSE so that 12504 * no SACK info will be used for this 12505 * connection. This assumes that SACK usage 12506 * permission is negotiated. This may need 12507 * to be changed once this is clarified. 12508 */ 12509 if (tcp->tcp_sack_info != NULL) { 12510 ASSERT(tcp->tcp_notsack_list == NULL); 12511 kmem_cache_free(tcp_sack_info_cache, 12512 tcp->tcp_sack_info); 12513 tcp->tcp_sack_info = NULL; 12514 } 12515 tcp->tcp_snd_sack_ok = B_FALSE; 12516 } 12517 12518 /* 12519 * Now we know the exact TCP/IP header length, subtract 12520 * that from tcp_mss to get our side's MSS. 12521 */ 12522 tcp->tcp_mss -= tcp->tcp_hdr_len; 12523 /* 12524 * Here we assume that the other side's header size will be equal to 12525 * our header size. We calculate the real MSS accordingly. Need to 12526 * take into additional stuffs IPsec puts in. 12527 * 12528 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12529 */ 12530 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12531 ((tcp->tcp_ipversion == IPV4_VERSION ? 12532 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12533 12534 /* 12535 * Set MSS to the smaller one of both ends of the connection. 12536 * We should not have called tcp_mss_set() before, but our 12537 * side of the MSS should have been set to a proper value 12538 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12539 * STREAM head parameters properly. 12540 * 12541 * If we have a larger-than-16-bit window but the other side 12542 * didn't want to do window scale, tcp_rwnd_set() will take 12543 * care of that. 12544 */ 12545 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12546 } 12547 12548 /* 12549 * Sends the T_CONN_IND to the listener. The caller calls this 12550 * functions via squeue to get inside the listener's perimeter 12551 * once the 3 way hand shake is done a T_CONN_IND needs to be 12552 * sent. As an optimization, the caller can call this directly 12553 * if listener's perimeter is same as eager's. 12554 */ 12555 /* ARGSUSED */ 12556 void 12557 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12558 { 12559 conn_t *lconnp = (conn_t *)arg; 12560 tcp_t *listener = lconnp->conn_tcp; 12561 tcp_t *tcp; 12562 struct T_conn_ind *conn_ind; 12563 ipaddr_t *addr_cache; 12564 boolean_t need_send_conn_ind = B_FALSE; 12565 tcp_stack_t *tcps = listener->tcp_tcps; 12566 12567 /* retrieve the eager */ 12568 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12569 ASSERT(conn_ind->OPT_offset != 0 && 12570 conn_ind->OPT_length == sizeof (intptr_t)); 12571 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12572 conn_ind->OPT_length); 12573 12574 /* 12575 * TLI/XTI applications will get confused by 12576 * sending eager as an option since it violates 12577 * the option semantics. So remove the eager as 12578 * option since TLI/XTI app doesn't need it anyway. 12579 */ 12580 if (!TCP_IS_SOCKET(listener)) { 12581 conn_ind->OPT_length = 0; 12582 conn_ind->OPT_offset = 0; 12583 } 12584 if (listener->tcp_state == TCPS_CLOSED || 12585 TCP_IS_DETACHED(listener)) { 12586 /* 12587 * If listener has closed, it would have caused a 12588 * a cleanup/blowoff to happen for the eager. We 12589 * just need to return. 12590 */ 12591 freemsg(mp); 12592 return; 12593 } 12594 12595 12596 /* 12597 * if the conn_req_q is full defer passing up the 12598 * T_CONN_IND until space is availabe after t_accept() 12599 * processing 12600 */ 12601 mutex_enter(&listener->tcp_eager_lock); 12602 12603 /* 12604 * Take the eager out, if it is in the list of droppable eagers 12605 * as we are here because the 3W handshake is over. 12606 */ 12607 MAKE_UNDROPPABLE(tcp); 12608 12609 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12610 tcp_t *tail; 12611 12612 /* 12613 * The eager already has an extra ref put in tcp_rput_data 12614 * so that it stays till accept comes back even though it 12615 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12616 */ 12617 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12618 listener->tcp_conn_req_cnt_q0--; 12619 listener->tcp_conn_req_cnt_q++; 12620 12621 /* Move from SYN_RCVD to ESTABLISHED list */ 12622 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12623 tcp->tcp_eager_prev_q0; 12624 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12625 tcp->tcp_eager_next_q0; 12626 tcp->tcp_eager_prev_q0 = NULL; 12627 tcp->tcp_eager_next_q0 = NULL; 12628 12629 /* 12630 * Insert at end of the queue because sockfs 12631 * sends down T_CONN_RES in chronological 12632 * order. Leaving the older conn indications 12633 * at front of the queue helps reducing search 12634 * time. 12635 */ 12636 tail = listener->tcp_eager_last_q; 12637 if (tail != NULL) 12638 tail->tcp_eager_next_q = tcp; 12639 else 12640 listener->tcp_eager_next_q = tcp; 12641 listener->tcp_eager_last_q = tcp; 12642 tcp->tcp_eager_next_q = NULL; 12643 /* 12644 * Delay sending up the T_conn_ind until we are 12645 * done with the eager. Once we have have sent up 12646 * the T_conn_ind, the accept can potentially complete 12647 * any time and release the refhold we have on the eager. 12648 */ 12649 need_send_conn_ind = B_TRUE; 12650 } else { 12651 /* 12652 * Defer connection on q0 and set deferred 12653 * connection bit true 12654 */ 12655 tcp->tcp_conn_def_q0 = B_TRUE; 12656 12657 /* take tcp out of q0 ... */ 12658 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12659 tcp->tcp_eager_next_q0; 12660 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12661 tcp->tcp_eager_prev_q0; 12662 12663 /* ... and place it at the end of q0 */ 12664 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12665 tcp->tcp_eager_next_q0 = listener; 12666 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12667 listener->tcp_eager_prev_q0 = tcp; 12668 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12669 } 12670 12671 /* we have timed out before */ 12672 if (tcp->tcp_syn_rcvd_timeout != 0) { 12673 tcp->tcp_syn_rcvd_timeout = 0; 12674 listener->tcp_syn_rcvd_timeout--; 12675 if (listener->tcp_syn_defense && 12676 listener->tcp_syn_rcvd_timeout <= 12677 (tcps->tcps_conn_req_max_q0 >> 5) && 12678 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12679 listener->tcp_last_rcv_lbolt)) { 12680 /* 12681 * Turn off the defense mode if we 12682 * believe the SYN attack is over. 12683 */ 12684 listener->tcp_syn_defense = B_FALSE; 12685 if (listener->tcp_ip_addr_cache) { 12686 kmem_free((void *)listener->tcp_ip_addr_cache, 12687 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12688 listener->tcp_ip_addr_cache = NULL; 12689 } 12690 } 12691 } 12692 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12693 if (addr_cache != NULL) { 12694 /* 12695 * We have finished a 3-way handshake with this 12696 * remote host. This proves the IP addr is good. 12697 * Cache it! 12698 */ 12699 addr_cache[IP_ADDR_CACHE_HASH( 12700 tcp->tcp_remote)] = tcp->tcp_remote; 12701 } 12702 mutex_exit(&listener->tcp_eager_lock); 12703 if (need_send_conn_ind) { 12704 if (IPCL_IS_NONSTR(lconnp)) { 12705 ASSERT(tcp->tcp_listener == listener); 12706 ASSERT(tcp->tcp_saved_listener == listener); 12707 if ((*lconnp->conn_upcalls->su_newconn) 12708 (lconnp->conn_upper_handle, 12709 (sock_lower_handle_t)tcp->tcp_connp, 12710 &sock_tcp_downcalls, DB_CRED(mp), DB_CPID(mp), 12711 &tcp->tcp_connp->conn_upcalls) != NULL) { 12712 /* 12713 * Keep the message around 12714 * in case of fallback 12715 */ 12716 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12717 } else { 12718 freemsg(mp); 12719 } 12720 } else { 12721 putnext(listener->tcp_rq, mp); 12722 } 12723 } 12724 } 12725 12726 mblk_t * 12727 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12728 uint_t *ifindexp, ip6_pkt_t *ippp) 12729 { 12730 ip_pktinfo_t *pinfo; 12731 ip6_t *ip6h; 12732 uchar_t *rptr; 12733 mblk_t *first_mp = mp; 12734 boolean_t mctl_present = B_FALSE; 12735 uint_t ifindex = 0; 12736 ip6_pkt_t ipp; 12737 uint_t ipvers; 12738 uint_t ip_hdr_len; 12739 tcp_stack_t *tcps = tcp->tcp_tcps; 12740 12741 rptr = mp->b_rptr; 12742 ASSERT(OK_32PTR(rptr)); 12743 ASSERT(tcp != NULL); 12744 ipp.ipp_fields = 0; 12745 12746 switch DB_TYPE(mp) { 12747 case M_CTL: 12748 mp = mp->b_cont; 12749 if (mp == NULL) { 12750 freemsg(first_mp); 12751 return (NULL); 12752 } 12753 if (DB_TYPE(mp) != M_DATA) { 12754 freemsg(first_mp); 12755 return (NULL); 12756 } 12757 mctl_present = B_TRUE; 12758 break; 12759 case M_DATA: 12760 break; 12761 default: 12762 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12763 freemsg(mp); 12764 return (NULL); 12765 } 12766 ipvers = IPH_HDR_VERSION(rptr); 12767 if (ipvers == IPV4_VERSION) { 12768 if (tcp == NULL) { 12769 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12770 goto done; 12771 } 12772 12773 ipp.ipp_fields |= IPPF_HOPLIMIT; 12774 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12775 12776 /* 12777 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12778 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12779 */ 12780 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12781 mctl_present) { 12782 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12783 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12784 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12785 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12786 ipp.ipp_fields |= IPPF_IFINDEX; 12787 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12788 ifindex = pinfo->ip_pkt_ifindex; 12789 } 12790 freeb(first_mp); 12791 mctl_present = B_FALSE; 12792 } 12793 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12794 } else { 12795 ip6h = (ip6_t *)rptr; 12796 12797 ASSERT(ipvers == IPV6_VERSION); 12798 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12799 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12800 ipp.ipp_hoplimit = ip6h->ip6_hops; 12801 12802 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12803 uint8_t nexthdrp; 12804 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12805 12806 /* Look for ifindex information */ 12807 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12808 ip6i_t *ip6i = (ip6i_t *)ip6h; 12809 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12810 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12811 freemsg(first_mp); 12812 return (NULL); 12813 } 12814 12815 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12816 ASSERT(ip6i->ip6i_ifindex != 0); 12817 ipp.ipp_fields |= IPPF_IFINDEX; 12818 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12819 ifindex = ip6i->ip6i_ifindex; 12820 } 12821 rptr = (uchar_t *)&ip6i[1]; 12822 mp->b_rptr = rptr; 12823 if (rptr == mp->b_wptr) { 12824 mblk_t *mp1; 12825 mp1 = mp->b_cont; 12826 freeb(mp); 12827 mp = mp1; 12828 rptr = mp->b_rptr; 12829 } 12830 if (MBLKL(mp) < IPV6_HDR_LEN + 12831 sizeof (tcph_t)) { 12832 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12833 freemsg(first_mp); 12834 return (NULL); 12835 } 12836 ip6h = (ip6_t *)rptr; 12837 } 12838 12839 /* 12840 * Find any potentially interesting extension headers 12841 * as well as the length of the IPv6 + extension 12842 * headers. 12843 */ 12844 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12845 /* Verify if this is a TCP packet */ 12846 if (nexthdrp != IPPROTO_TCP) { 12847 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12848 freemsg(first_mp); 12849 return (NULL); 12850 } 12851 } else { 12852 ip_hdr_len = IPV6_HDR_LEN; 12853 } 12854 } 12855 12856 done: 12857 if (ipversp != NULL) 12858 *ipversp = ipvers; 12859 if (ip_hdr_lenp != NULL) 12860 *ip_hdr_lenp = ip_hdr_len; 12861 if (ippp != NULL) 12862 *ippp = ipp; 12863 if (ifindexp != NULL) 12864 *ifindexp = ifindex; 12865 if (mctl_present) { 12866 freeb(first_mp); 12867 } 12868 return (mp); 12869 } 12870 12871 /* 12872 * Handle M_DATA messages from IP. Its called directly from IP via 12873 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12874 * in this path. 12875 * 12876 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12877 * v4 and v6), we are called through tcp_input() and a M_CTL can 12878 * be present for options but tcp_find_pktinfo() deals with it. We 12879 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12880 * 12881 * The first argument is always the connp/tcp to which the mp belongs. 12882 * There are no exceptions to this rule. The caller has already put 12883 * a reference on this connp/tcp and once tcp_rput_data() returns, 12884 * the squeue will do the refrele. 12885 * 12886 * The TH_SYN for the listener directly go to tcp_conn_request via 12887 * squeue. 12888 * 12889 * sqp: NULL = recursive, sqp != NULL means called from squeue 12890 */ 12891 void 12892 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12893 { 12894 int32_t bytes_acked; 12895 int32_t gap; 12896 mblk_t *mp1; 12897 uint_t flags; 12898 uint32_t new_swnd = 0; 12899 uchar_t *iphdr; 12900 uchar_t *rptr; 12901 int32_t rgap; 12902 uint32_t seg_ack; 12903 int seg_len; 12904 uint_t ip_hdr_len; 12905 uint32_t seg_seq; 12906 tcph_t *tcph; 12907 int urp; 12908 tcp_opt_t tcpopt; 12909 uint_t ipvers; 12910 ip6_pkt_t ipp; 12911 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12912 uint32_t cwnd; 12913 uint32_t add; 12914 int npkt; 12915 int mss; 12916 conn_t *connp = (conn_t *)arg; 12917 squeue_t *sqp = (squeue_t *)arg2; 12918 tcp_t *tcp = connp->conn_tcp; 12919 tcp_stack_t *tcps = tcp->tcp_tcps; 12920 12921 /* 12922 * RST from fused tcp loopback peer should trigger an unfuse. 12923 */ 12924 if (tcp->tcp_fused) { 12925 TCP_STAT(tcps, tcp_fusion_aborted); 12926 tcp_unfuse(tcp); 12927 } 12928 12929 iphdr = mp->b_rptr; 12930 rptr = mp->b_rptr; 12931 ASSERT(OK_32PTR(rptr)); 12932 12933 /* 12934 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12935 * processing here. For rest call tcp_find_pktinfo to fill up the 12936 * necessary information. 12937 */ 12938 if (IPCL_IS_TCP4(connp)) { 12939 ipvers = IPV4_VERSION; 12940 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12941 } else { 12942 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12943 NULL, &ipp); 12944 if (mp == NULL) { 12945 TCP_STAT(tcps, tcp_rput_v6_error); 12946 return; 12947 } 12948 iphdr = mp->b_rptr; 12949 rptr = mp->b_rptr; 12950 } 12951 ASSERT(DB_TYPE(mp) == M_DATA); 12952 ASSERT(mp->b_next == NULL); 12953 12954 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12955 seg_seq = ABE32_TO_U32(tcph->th_seq); 12956 seg_ack = ABE32_TO_U32(tcph->th_ack); 12957 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12958 seg_len = (int)(mp->b_wptr - rptr) - 12959 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12960 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12961 do { 12962 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12963 (uintptr_t)INT_MAX); 12964 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12965 } while ((mp1 = mp1->b_cont) != NULL && 12966 mp1->b_datap->db_type == M_DATA); 12967 } 12968 12969 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12970 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12971 seg_len, tcph); 12972 return; 12973 } 12974 12975 if (sqp != NULL) { 12976 /* 12977 * This is the correct place to update tcp_last_recv_time. Note 12978 * that it is also updated for tcp structure that belongs to 12979 * global and listener queues which do not really need updating. 12980 * But that should not cause any harm. And it is updated for 12981 * all kinds of incoming segments, not only for data segments. 12982 */ 12983 tcp->tcp_last_recv_time = lbolt; 12984 } 12985 12986 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12987 12988 BUMP_LOCAL(tcp->tcp_ibsegs); 12989 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12990 12991 if ((flags & TH_URG) && sqp != NULL) { 12992 /* 12993 * TCP can't handle urgent pointers that arrive before 12994 * the connection has been accept()ed since it can't 12995 * buffer OOB data. Discard segment if this happens. 12996 * 12997 * We can't just rely on a non-null tcp_listener to indicate 12998 * that the accept() has completed since unlinking of the 12999 * eager and completion of the accept are not atomic. 13000 * tcp_detached, when it is not set (B_FALSE) indicates 13001 * that the accept() has completed. 13002 * 13003 * Nor can it reassemble urgent pointers, so discard 13004 * if it's not the next segment expected. 13005 * 13006 * Otherwise, collapse chain into one mblk (discard if 13007 * that fails). This makes sure the headers, retransmitted 13008 * data, and new data all are in the same mblk. 13009 */ 13010 ASSERT(mp != NULL); 13011 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 13012 freemsg(mp); 13013 return; 13014 } 13015 /* Update pointers into message */ 13016 iphdr = rptr = mp->b_rptr; 13017 tcph = (tcph_t *)&rptr[ip_hdr_len]; 13018 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 13019 /* 13020 * Since we can't handle any data with this urgent 13021 * pointer that is out of sequence, we expunge 13022 * the data. This allows us to still register 13023 * the urgent mark and generate the M_PCSIG, 13024 * which we can do. 13025 */ 13026 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13027 seg_len = 0; 13028 } 13029 } 13030 13031 switch (tcp->tcp_state) { 13032 case TCPS_SYN_SENT: 13033 if (flags & TH_ACK) { 13034 /* 13035 * Note that our stack cannot send data before a 13036 * connection is established, therefore the 13037 * following check is valid. Otherwise, it has 13038 * to be changed. 13039 */ 13040 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 13041 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13042 freemsg(mp); 13043 if (flags & TH_RST) 13044 return; 13045 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 13046 tcp, seg_ack, 0, TH_RST); 13047 return; 13048 } 13049 ASSERT(tcp->tcp_suna + 1 == seg_ack); 13050 } 13051 if (flags & TH_RST) { 13052 freemsg(mp); 13053 if (flags & TH_ACK) 13054 (void) tcp_clean_death(tcp, 13055 ECONNREFUSED, 13); 13056 return; 13057 } 13058 if (!(flags & TH_SYN)) { 13059 freemsg(mp); 13060 return; 13061 } 13062 13063 /* Process all TCP options. */ 13064 tcp_process_options(tcp, tcph); 13065 /* 13066 * The following changes our rwnd to be a multiple of the 13067 * MIN(peer MSS, our MSS) for performance reason. 13068 */ 13069 (void) tcp_rwnd_set(tcp, 13070 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 13071 13072 /* Is the other end ECN capable? */ 13073 if (tcp->tcp_ecn_ok) { 13074 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 13075 tcp->tcp_ecn_ok = B_FALSE; 13076 } 13077 } 13078 /* 13079 * Clear ECN flags because it may interfere with later 13080 * processing. 13081 */ 13082 flags &= ~(TH_ECE|TH_CWR); 13083 13084 tcp->tcp_irs = seg_seq; 13085 tcp->tcp_rack = seg_seq; 13086 tcp->tcp_rnxt = seg_seq + 1; 13087 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13088 if (!TCP_IS_DETACHED(tcp)) { 13089 /* Allocate room for SACK options if needed. */ 13090 if (tcp->tcp_snd_sack_ok) { 13091 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13092 tcp->tcp_hdr_len + 13093 TCPOPT_MAX_SACK_LEN + 13094 (tcp->tcp_loopback ? 0 : 13095 tcps->tcps_wroff_xtra)); 13096 } else { 13097 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 13098 tcp->tcp_hdr_len + 13099 (tcp->tcp_loopback ? 0 : 13100 tcps->tcps_wroff_xtra)); 13101 } 13102 } 13103 if (flags & TH_ACK) { 13104 /* 13105 * If we can't get the confirmation upstream, pretend 13106 * we didn't even see this one. 13107 * 13108 * XXX: how can we pretend we didn't see it if we 13109 * have updated rnxt et. al. 13110 * 13111 * For loopback we defer sending up the T_CONN_CON 13112 * until after some checks below. 13113 */ 13114 mp1 = NULL; 13115 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 13116 tcp->tcp_loopback ? &mp1 : NULL)) { 13117 freemsg(mp); 13118 return; 13119 } 13120 /* SYN was acked - making progress */ 13121 if (tcp->tcp_ipversion == IPV6_VERSION) 13122 tcp->tcp_ip_forward_progress = B_TRUE; 13123 13124 /* One for the SYN */ 13125 tcp->tcp_suna = tcp->tcp_iss + 1; 13126 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13127 tcp->tcp_state = TCPS_ESTABLISHED; 13128 13129 /* 13130 * If SYN was retransmitted, need to reset all 13131 * retransmission info. This is because this 13132 * segment will be treated as a dup ACK. 13133 */ 13134 if (tcp->tcp_rexmit) { 13135 tcp->tcp_rexmit = B_FALSE; 13136 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13137 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13138 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13139 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13140 tcp->tcp_ms_we_have_waited = 0; 13141 13142 /* 13143 * Set tcp_cwnd back to 1 MSS, per 13144 * recommendation from 13145 * draft-floyd-incr-init-win-01.txt, 13146 * Increasing TCP's Initial Window. 13147 */ 13148 tcp->tcp_cwnd = tcp->tcp_mss; 13149 } 13150 13151 tcp->tcp_swl1 = seg_seq; 13152 tcp->tcp_swl2 = seg_ack; 13153 13154 new_swnd = BE16_TO_U16(tcph->th_win); 13155 tcp->tcp_swnd = new_swnd; 13156 if (new_swnd > tcp->tcp_max_swnd) 13157 tcp->tcp_max_swnd = new_swnd; 13158 13159 /* 13160 * Always send the three-way handshake ack immediately 13161 * in order to make the connection complete as soon as 13162 * possible on the accepting host. 13163 */ 13164 flags |= TH_ACK_NEEDED; 13165 13166 /* 13167 * Special case for loopback. At this point we have 13168 * received SYN-ACK from the remote endpoint. In 13169 * order to ensure that both endpoints reach the 13170 * fused state prior to any data exchange, the final 13171 * ACK needs to be sent before we indicate T_CONN_CON 13172 * to the module upstream. 13173 */ 13174 if (tcp->tcp_loopback) { 13175 mblk_t *ack_mp; 13176 13177 ASSERT(!tcp->tcp_unfusable); 13178 ASSERT(mp1 != NULL); 13179 /* 13180 * For loopback, we always get a pure SYN-ACK 13181 * and only need to send back the final ACK 13182 * with no data (this is because the other 13183 * tcp is ours and we don't do T/TCP). This 13184 * final ACK triggers the passive side to 13185 * perform fusion in ESTABLISHED state. 13186 */ 13187 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13188 if (tcp->tcp_ack_tid != 0) { 13189 (void) TCP_TIMER_CANCEL(tcp, 13190 tcp->tcp_ack_tid); 13191 tcp->tcp_ack_tid = 0; 13192 } 13193 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13194 BUMP_LOCAL(tcp->tcp_obsegs); 13195 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13196 13197 if (!IPCL_IS_NONSTR(connp)) { 13198 /* Send up T_CONN_CON */ 13199 putnext(tcp->tcp_rq, mp1); 13200 } else { 13201 (*connp->conn_upcalls-> 13202 su_connected) 13203 (connp->conn_upper_handle, 13204 tcp->tcp_connid, 13205 DB_CRED(mp1), 13206 DB_CPID(mp1)); 13207 freemsg(mp1); 13208 } 13209 13210 freemsg(mp); 13211 return; 13212 } 13213 /* 13214 * Forget fusion; we need to handle more 13215 * complex cases below. Send the deferred 13216 * T_CONN_CON message upstream and proceed 13217 * as usual. Mark this tcp as not capable 13218 * of fusion. 13219 */ 13220 TCP_STAT(tcps, tcp_fusion_unfusable); 13221 tcp->tcp_unfusable = B_TRUE; 13222 if (!IPCL_IS_NONSTR(connp)) { 13223 putnext(tcp->tcp_rq, mp1); 13224 } else { 13225 (*connp->conn_upcalls->su_connected) 13226 (connp->conn_upper_handle, 13227 tcp->tcp_connid, DB_CRED(mp1), 13228 DB_CPID(mp1)); 13229 freemsg(mp1); 13230 } 13231 } 13232 13233 /* 13234 * Check to see if there is data to be sent. If 13235 * yes, set the transmit flag. Then check to see 13236 * if received data processing needs to be done. 13237 * If not, go straight to xmit_check. This short 13238 * cut is OK as we don't support T/TCP. 13239 */ 13240 if (tcp->tcp_unsent) 13241 flags |= TH_XMIT_NEEDED; 13242 13243 if (seg_len == 0 && !(flags & TH_URG)) { 13244 freemsg(mp); 13245 goto xmit_check; 13246 } 13247 13248 flags &= ~TH_SYN; 13249 seg_seq++; 13250 break; 13251 } 13252 tcp->tcp_state = TCPS_SYN_RCVD; 13253 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13254 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13255 if (mp1) { 13256 DB_CPID(mp1) = tcp->tcp_cpid; 13257 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13258 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13259 } 13260 freemsg(mp); 13261 return; 13262 case TCPS_SYN_RCVD: 13263 if (flags & TH_ACK) { 13264 /* 13265 * In this state, a SYN|ACK packet is either bogus 13266 * because the other side must be ACKing our SYN which 13267 * indicates it has seen the ACK for their SYN and 13268 * shouldn't retransmit it or we're crossing SYNs 13269 * on active open. 13270 */ 13271 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13272 freemsg(mp); 13273 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13274 tcp, seg_ack, 0, TH_RST); 13275 return; 13276 } 13277 /* 13278 * NOTE: RFC 793 pg. 72 says this should be 13279 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13280 * but that would mean we have an ack that ignored 13281 * our SYN. 13282 */ 13283 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13284 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13285 freemsg(mp); 13286 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13287 tcp, seg_ack, 0, TH_RST); 13288 return; 13289 } 13290 } 13291 break; 13292 case TCPS_LISTEN: 13293 /* 13294 * Only a TLI listener can come through this path when a 13295 * acceptor is going back to be a listener and a packet 13296 * for the acceptor hits the classifier. For a socket 13297 * listener, this can never happen because a listener 13298 * can never accept connection on itself and hence a 13299 * socket acceptor can not go back to being a listener. 13300 */ 13301 ASSERT(!TCP_IS_SOCKET(tcp)); 13302 /*FALLTHRU*/ 13303 case TCPS_CLOSED: 13304 case TCPS_BOUND: { 13305 conn_t *new_connp; 13306 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13307 13308 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13309 if (new_connp != NULL) { 13310 tcp_reinput(new_connp, mp, connp->conn_sqp); 13311 return; 13312 } 13313 /* We failed to classify. For now just drop the packet */ 13314 freemsg(mp); 13315 return; 13316 } 13317 case TCPS_IDLE: 13318 /* 13319 * Handle the case where the tcp_clean_death() has happened 13320 * on a connection (application hasn't closed yet) but a packet 13321 * was already queued on squeue before tcp_clean_death() 13322 * was processed. Calling tcp_clean_death() twice on same 13323 * connection can result in weird behaviour. 13324 */ 13325 freemsg(mp); 13326 return; 13327 default: 13328 break; 13329 } 13330 13331 /* 13332 * Already on the correct queue/perimeter. 13333 * If this is a detached connection and not an eager 13334 * connection hanging off a listener then new data 13335 * (past the FIN) will cause a reset. 13336 * We do a special check here where it 13337 * is out of the main line, rather than check 13338 * if we are detached every time we see new 13339 * data down below. 13340 */ 13341 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13342 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13343 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13344 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13345 13346 freemsg(mp); 13347 /* 13348 * This could be an SSL closure alert. We're detached so just 13349 * acknowledge it this last time. 13350 */ 13351 if (tcp->tcp_kssl_ctx != NULL) { 13352 kssl_release_ctx(tcp->tcp_kssl_ctx); 13353 tcp->tcp_kssl_ctx = NULL; 13354 13355 tcp->tcp_rnxt += seg_len; 13356 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13357 flags |= TH_ACK_NEEDED; 13358 goto ack_check; 13359 } 13360 13361 tcp_xmit_ctl("new data when detached", tcp, 13362 tcp->tcp_snxt, 0, TH_RST); 13363 (void) tcp_clean_death(tcp, EPROTO, 12); 13364 return; 13365 } 13366 13367 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13368 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13369 new_swnd = BE16_TO_U16(tcph->th_win) << 13370 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13371 13372 if (tcp->tcp_snd_ts_ok) { 13373 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13374 /* 13375 * This segment is not acceptable. 13376 * Drop it and send back an ACK. 13377 */ 13378 freemsg(mp); 13379 flags |= TH_ACK_NEEDED; 13380 goto ack_check; 13381 } 13382 } else if (tcp->tcp_snd_sack_ok) { 13383 ASSERT(tcp->tcp_sack_info != NULL); 13384 tcpopt.tcp = tcp; 13385 /* 13386 * SACK info in already updated in tcp_parse_options. Ignore 13387 * all other TCP options... 13388 */ 13389 (void) tcp_parse_options(tcph, &tcpopt); 13390 } 13391 try_again:; 13392 mss = tcp->tcp_mss; 13393 gap = seg_seq - tcp->tcp_rnxt; 13394 rgap = tcp->tcp_rwnd - (gap + seg_len); 13395 /* 13396 * gap is the amount of sequence space between what we expect to see 13397 * and what we got for seg_seq. A positive value for gap means 13398 * something got lost. A negative value means we got some old stuff. 13399 */ 13400 if (gap < 0) { 13401 /* Old stuff present. Is the SYN in there? */ 13402 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13403 (seg_len != 0)) { 13404 flags &= ~TH_SYN; 13405 seg_seq++; 13406 urp--; 13407 /* Recompute the gaps after noting the SYN. */ 13408 goto try_again; 13409 } 13410 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13411 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13412 (seg_len > -gap ? -gap : seg_len)); 13413 /* Remove the old stuff from seg_len. */ 13414 seg_len += gap; 13415 /* 13416 * Anything left? 13417 * Make sure to check for unack'd FIN when rest of data 13418 * has been previously ack'd. 13419 */ 13420 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13421 /* 13422 * Resets are only valid if they lie within our offered 13423 * window. If the RST bit is set, we just ignore this 13424 * segment. 13425 */ 13426 if (flags & TH_RST) { 13427 freemsg(mp); 13428 return; 13429 } 13430 13431 /* 13432 * The arriving of dup data packets indicate that we 13433 * may have postponed an ack for too long, or the other 13434 * side's RTT estimate is out of shape. Start acking 13435 * more often. 13436 */ 13437 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13438 tcp->tcp_rack_cnt >= 1 && 13439 tcp->tcp_rack_abs_max > 2) { 13440 tcp->tcp_rack_abs_max--; 13441 } 13442 tcp->tcp_rack_cur_max = 1; 13443 13444 /* 13445 * This segment is "unacceptable". None of its 13446 * sequence space lies within our advertized window. 13447 * 13448 * Adjust seg_len to the original value for tracing. 13449 */ 13450 seg_len -= gap; 13451 if (tcp->tcp_debug) { 13452 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13453 "tcp_rput: unacceptable, gap %d, rgap %d, " 13454 "flags 0x%x, seg_seq %u, seg_ack %u, " 13455 "seg_len %d, rnxt %u, snxt %u, %s", 13456 gap, rgap, flags, seg_seq, seg_ack, 13457 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13458 tcp_display(tcp, NULL, 13459 DISP_ADDR_AND_PORT)); 13460 } 13461 13462 /* 13463 * Arrange to send an ACK in response to the 13464 * unacceptable segment per RFC 793 page 69. There 13465 * is only one small difference between ours and the 13466 * acceptability test in the RFC - we accept ACK-only 13467 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13468 * will be generated. 13469 * 13470 * Note that we have to ACK an ACK-only packet at least 13471 * for stacks that send 0-length keep-alives with 13472 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13473 * section 4.2.3.6. As long as we don't ever generate 13474 * an unacceptable packet in response to an incoming 13475 * packet that is unacceptable, it should not cause 13476 * "ACK wars". 13477 */ 13478 flags |= TH_ACK_NEEDED; 13479 13480 /* 13481 * Continue processing this segment in order to use the 13482 * ACK information it contains, but skip all other 13483 * sequence-number processing. Processing the ACK 13484 * information is necessary in order to 13485 * re-synchronize connections that may have lost 13486 * synchronization. 13487 * 13488 * We clear seg_len and flag fields related to 13489 * sequence number processing as they are not 13490 * to be trusted for an unacceptable segment. 13491 */ 13492 seg_len = 0; 13493 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13494 goto process_ack; 13495 } 13496 13497 /* Fix seg_seq, and chew the gap off the front. */ 13498 seg_seq = tcp->tcp_rnxt; 13499 urp += gap; 13500 do { 13501 mblk_t *mp2; 13502 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13503 (uintptr_t)UINT_MAX); 13504 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13505 if (gap > 0) { 13506 mp->b_rptr = mp->b_wptr - gap; 13507 break; 13508 } 13509 mp2 = mp; 13510 mp = mp->b_cont; 13511 freeb(mp2); 13512 } while (gap < 0); 13513 /* 13514 * If the urgent data has already been acknowledged, we 13515 * should ignore TH_URG below 13516 */ 13517 if (urp < 0) 13518 flags &= ~TH_URG; 13519 } 13520 /* 13521 * rgap is the amount of stuff received out of window. A negative 13522 * value is the amount out of window. 13523 */ 13524 if (rgap < 0) { 13525 mblk_t *mp2; 13526 13527 if (tcp->tcp_rwnd == 0) { 13528 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13529 } else { 13530 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13531 UPDATE_MIB(&tcps->tcps_mib, 13532 tcpInDataPastWinBytes, -rgap); 13533 } 13534 13535 /* 13536 * seg_len does not include the FIN, so if more than 13537 * just the FIN is out of window, we act like we don't 13538 * see it. (If just the FIN is out of window, rgap 13539 * will be zero and we will go ahead and acknowledge 13540 * the FIN.) 13541 */ 13542 flags &= ~TH_FIN; 13543 13544 /* Fix seg_len and make sure there is something left. */ 13545 seg_len += rgap; 13546 if (seg_len <= 0) { 13547 /* 13548 * Resets are only valid if they lie within our offered 13549 * window. If the RST bit is set, we just ignore this 13550 * segment. 13551 */ 13552 if (flags & TH_RST) { 13553 freemsg(mp); 13554 return; 13555 } 13556 13557 /* Per RFC 793, we need to send back an ACK. */ 13558 flags |= TH_ACK_NEEDED; 13559 13560 /* 13561 * Send SIGURG as soon as possible i.e. even 13562 * if the TH_URG was delivered in a window probe 13563 * packet (which will be unacceptable). 13564 * 13565 * We generate a signal if none has been generated 13566 * for this connection or if this is a new urgent 13567 * byte. Also send a zero-length "unmarked" message 13568 * to inform SIOCATMARK that this is not the mark. 13569 * 13570 * tcp_urp_last_valid is cleared when the T_exdata_ind 13571 * is sent up. This plus the check for old data 13572 * (gap >= 0) handles the wraparound of the sequence 13573 * number space without having to always track the 13574 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13575 * this max in its rcv_up variable). 13576 * 13577 * This prevents duplicate SIGURGS due to a "late" 13578 * zero-window probe when the T_EXDATA_IND has already 13579 * been sent up. 13580 */ 13581 if ((flags & TH_URG) && 13582 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13583 tcp->tcp_urp_last))) { 13584 if (IPCL_IS_NONSTR(connp)) { 13585 if (!TCP_IS_DETACHED(tcp)) { 13586 (*connp->conn_upcalls-> 13587 su_signal_oob) 13588 (connp->conn_upper_handle, 13589 urp); 13590 } 13591 } else { 13592 mp1 = allocb(0, BPRI_MED); 13593 if (mp1 == NULL) { 13594 freemsg(mp); 13595 return; 13596 } 13597 if (!TCP_IS_DETACHED(tcp) && 13598 !putnextctl1(tcp->tcp_rq, 13599 M_PCSIG, SIGURG)) { 13600 /* Try again on the rexmit. */ 13601 freemsg(mp1); 13602 freemsg(mp); 13603 return; 13604 } 13605 /* 13606 * If the next byte would be the mark 13607 * then mark with MARKNEXT else mark 13608 * with NOTMARKNEXT. 13609 */ 13610 if (gap == 0 && urp == 0) 13611 mp1->b_flag |= MSGMARKNEXT; 13612 else 13613 mp1->b_flag |= MSGNOTMARKNEXT; 13614 freemsg(tcp->tcp_urp_mark_mp); 13615 tcp->tcp_urp_mark_mp = mp1; 13616 flags |= TH_SEND_URP_MARK; 13617 } 13618 tcp->tcp_urp_last_valid = B_TRUE; 13619 tcp->tcp_urp_last = urp + seg_seq; 13620 } 13621 /* 13622 * If this is a zero window probe, continue to 13623 * process the ACK part. But we need to set seg_len 13624 * to 0 to avoid data processing. Otherwise just 13625 * drop the segment and send back an ACK. 13626 */ 13627 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13628 flags &= ~(TH_SYN | TH_URG); 13629 seg_len = 0; 13630 goto process_ack; 13631 } else { 13632 freemsg(mp); 13633 goto ack_check; 13634 } 13635 } 13636 /* Pitch out of window stuff off the end. */ 13637 rgap = seg_len; 13638 mp2 = mp; 13639 do { 13640 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13641 (uintptr_t)INT_MAX); 13642 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13643 if (rgap < 0) { 13644 mp2->b_wptr += rgap; 13645 if ((mp1 = mp2->b_cont) != NULL) { 13646 mp2->b_cont = NULL; 13647 freemsg(mp1); 13648 } 13649 break; 13650 } 13651 } while ((mp2 = mp2->b_cont) != NULL); 13652 } 13653 ok:; 13654 /* 13655 * TCP should check ECN info for segments inside the window only. 13656 * Therefore the check should be done here. 13657 */ 13658 if (tcp->tcp_ecn_ok) { 13659 if (flags & TH_CWR) { 13660 tcp->tcp_ecn_echo_on = B_FALSE; 13661 } 13662 /* 13663 * Note that both ECN_CE and CWR can be set in the 13664 * same segment. In this case, we once again turn 13665 * on ECN_ECHO. 13666 */ 13667 if (tcp->tcp_ipversion == IPV4_VERSION) { 13668 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13669 13670 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13671 tcp->tcp_ecn_echo_on = B_TRUE; 13672 } 13673 } else { 13674 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13675 13676 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13677 htonl(IPH_ECN_CE << 20)) { 13678 tcp->tcp_ecn_echo_on = B_TRUE; 13679 } 13680 } 13681 } 13682 13683 /* 13684 * Check whether we can update tcp_ts_recent. This test is 13685 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13686 * Extensions for High Performance: An Update", Internet Draft. 13687 */ 13688 if (tcp->tcp_snd_ts_ok && 13689 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13690 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13691 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13692 tcp->tcp_last_rcv_lbolt = lbolt64; 13693 } 13694 13695 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13696 /* 13697 * FIN in an out of order segment. We record this in 13698 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13699 * Clear the FIN so that any check on FIN flag will fail. 13700 * Remember that FIN also counts in the sequence number 13701 * space. So we need to ack out of order FIN only segments. 13702 */ 13703 if (flags & TH_FIN) { 13704 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13705 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13706 flags &= ~TH_FIN; 13707 flags |= TH_ACK_NEEDED; 13708 } 13709 if (seg_len > 0) { 13710 /* Fill in the SACK blk list. */ 13711 if (tcp->tcp_snd_sack_ok) { 13712 ASSERT(tcp->tcp_sack_info != NULL); 13713 tcp_sack_insert(tcp->tcp_sack_list, 13714 seg_seq, seg_seq + seg_len, 13715 &(tcp->tcp_num_sack_blk)); 13716 } 13717 13718 /* 13719 * Attempt reassembly and see if we have something 13720 * ready to go. 13721 */ 13722 mp = tcp_reass(tcp, mp, seg_seq); 13723 /* Always ack out of order packets */ 13724 flags |= TH_ACK_NEEDED | TH_PUSH; 13725 if (mp) { 13726 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13727 (uintptr_t)INT_MAX); 13728 seg_len = mp->b_cont ? msgdsize(mp) : 13729 (int)(mp->b_wptr - mp->b_rptr); 13730 seg_seq = tcp->tcp_rnxt; 13731 /* 13732 * A gap is filled and the seq num and len 13733 * of the gap match that of a previously 13734 * received FIN, put the FIN flag back in. 13735 */ 13736 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13737 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13738 flags |= TH_FIN; 13739 tcp->tcp_valid_bits &= 13740 ~TCP_OFO_FIN_VALID; 13741 } 13742 } else { 13743 /* 13744 * Keep going even with NULL mp. 13745 * There may be a useful ACK or something else 13746 * we don't want to miss. 13747 * 13748 * But TCP should not perform fast retransmit 13749 * because of the ack number. TCP uses 13750 * seg_len == 0 to determine if it is a pure 13751 * ACK. And this is not a pure ACK. 13752 */ 13753 seg_len = 0; 13754 ofo_seg = B_TRUE; 13755 } 13756 } 13757 } else if (seg_len > 0) { 13758 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13759 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13760 /* 13761 * If an out of order FIN was received before, and the seq 13762 * num and len of the new segment match that of the FIN, 13763 * put the FIN flag back in. 13764 */ 13765 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13766 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13767 flags |= TH_FIN; 13768 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13769 } 13770 } 13771 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13772 if (flags & TH_RST) { 13773 freemsg(mp); 13774 switch (tcp->tcp_state) { 13775 case TCPS_SYN_RCVD: 13776 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13777 break; 13778 case TCPS_ESTABLISHED: 13779 case TCPS_FIN_WAIT_1: 13780 case TCPS_FIN_WAIT_2: 13781 case TCPS_CLOSE_WAIT: 13782 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13783 break; 13784 case TCPS_CLOSING: 13785 case TCPS_LAST_ACK: 13786 (void) tcp_clean_death(tcp, 0, 16); 13787 break; 13788 default: 13789 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13790 (void) tcp_clean_death(tcp, ENXIO, 17); 13791 break; 13792 } 13793 return; 13794 } 13795 if (flags & TH_SYN) { 13796 /* 13797 * See RFC 793, Page 71 13798 * 13799 * The seq number must be in the window as it should 13800 * be "fixed" above. If it is outside window, it should 13801 * be already rejected. Note that we allow seg_seq to be 13802 * rnxt + rwnd because we want to accept 0 window probe. 13803 */ 13804 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13805 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13806 freemsg(mp); 13807 /* 13808 * If the ACK flag is not set, just use our snxt as the 13809 * seq number of the RST segment. 13810 */ 13811 if (!(flags & TH_ACK)) { 13812 seg_ack = tcp->tcp_snxt; 13813 } 13814 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13815 TH_RST|TH_ACK); 13816 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13817 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13818 return; 13819 } 13820 /* 13821 * urp could be -1 when the urp field in the packet is 0 13822 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13823 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13824 */ 13825 if (flags & TH_URG && urp >= 0) { 13826 if (!tcp->tcp_urp_last_valid || 13827 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13828 if (IPCL_IS_NONSTR(connp)) { 13829 if (!TCP_IS_DETACHED(tcp)) { 13830 (*connp->conn_upcalls->su_signal_oob) 13831 (connp->conn_upper_handle, urp); 13832 } 13833 } else { 13834 /* 13835 * If we haven't generated the signal yet for 13836 * this urgent pointer value, do it now. Also, 13837 * send up a zero-length M_DATA indicating 13838 * whether or not this is the mark. The latter 13839 * is not needed when a T_EXDATA_IND is sent up. 13840 * However, if there are allocation failures 13841 * this code relies on the sender retransmitting 13842 * and the socket code for determining the mark 13843 * should not block waiting for the peer to 13844 * transmit. Thus, for simplicity we always 13845 * send up the mark indication. 13846 */ 13847 mp1 = allocb(0, BPRI_MED); 13848 if (mp1 == NULL) { 13849 freemsg(mp); 13850 return; 13851 } 13852 if (!TCP_IS_DETACHED(tcp) && 13853 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13854 SIGURG)) { 13855 /* Try again on the rexmit. */ 13856 freemsg(mp1); 13857 freemsg(mp); 13858 return; 13859 } 13860 /* 13861 * Mark with NOTMARKNEXT for now. 13862 * The code below will change this to MARKNEXT 13863 * if we are at the mark. 13864 * 13865 * If there are allocation failures (e.g. in 13866 * dupmsg below) the next time tcp_rput_data 13867 * sees the urgent segment it will send up the 13868 * MSGMARKNEXT message. 13869 */ 13870 mp1->b_flag |= MSGNOTMARKNEXT; 13871 freemsg(tcp->tcp_urp_mark_mp); 13872 tcp->tcp_urp_mark_mp = mp1; 13873 flags |= TH_SEND_URP_MARK; 13874 #ifdef DEBUG 13875 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13876 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13877 "last %x, %s", 13878 seg_seq, urp, tcp->tcp_urp_last, 13879 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13880 #endif /* DEBUG */ 13881 } 13882 tcp->tcp_urp_last_valid = B_TRUE; 13883 tcp->tcp_urp_last = urp + seg_seq; 13884 } else if (tcp->tcp_urp_mark_mp != NULL) { 13885 /* 13886 * An allocation failure prevented the previous 13887 * tcp_rput_data from sending up the allocated 13888 * MSG*MARKNEXT message - send it up this time 13889 * around. 13890 */ 13891 flags |= TH_SEND_URP_MARK; 13892 } 13893 13894 /* 13895 * If the urgent byte is in this segment, make sure that it is 13896 * all by itself. This makes it much easier to deal with the 13897 * possibility of an allocation failure on the T_exdata_ind. 13898 * Note that seg_len is the number of bytes in the segment, and 13899 * urp is the offset into the segment of the urgent byte. 13900 * urp < seg_len means that the urgent byte is in this segment. 13901 */ 13902 if (urp < seg_len) { 13903 if (seg_len != 1) { 13904 uint32_t tmp_rnxt; 13905 /* 13906 * Break it up and feed it back in. 13907 * Re-attach the IP header. 13908 */ 13909 mp->b_rptr = iphdr; 13910 if (urp > 0) { 13911 /* 13912 * There is stuff before the urgent 13913 * byte. 13914 */ 13915 mp1 = dupmsg(mp); 13916 if (!mp1) { 13917 /* 13918 * Trim from urgent byte on. 13919 * The rest will come back. 13920 */ 13921 (void) adjmsg(mp, 13922 urp - seg_len); 13923 tcp_rput_data(connp, 13924 mp, NULL); 13925 return; 13926 } 13927 (void) adjmsg(mp1, urp - seg_len); 13928 /* Feed this piece back in. */ 13929 tmp_rnxt = tcp->tcp_rnxt; 13930 tcp_rput_data(connp, mp1, NULL); 13931 /* 13932 * If the data passed back in was not 13933 * processed (ie: bad ACK) sending 13934 * the remainder back in will cause a 13935 * loop. In this case, drop the 13936 * packet and let the sender try 13937 * sending a good packet. 13938 */ 13939 if (tmp_rnxt == tcp->tcp_rnxt) { 13940 freemsg(mp); 13941 return; 13942 } 13943 } 13944 if (urp != seg_len - 1) { 13945 uint32_t tmp_rnxt; 13946 /* 13947 * There is stuff after the urgent 13948 * byte. 13949 */ 13950 mp1 = dupmsg(mp); 13951 if (!mp1) { 13952 /* 13953 * Trim everything beyond the 13954 * urgent byte. The rest will 13955 * come back. 13956 */ 13957 (void) adjmsg(mp, 13958 urp + 1 - seg_len); 13959 tcp_rput_data(connp, 13960 mp, NULL); 13961 return; 13962 } 13963 (void) adjmsg(mp1, urp + 1 - seg_len); 13964 tmp_rnxt = tcp->tcp_rnxt; 13965 tcp_rput_data(connp, mp1, NULL); 13966 /* 13967 * If the data passed back in was not 13968 * processed (ie: bad ACK) sending 13969 * the remainder back in will cause a 13970 * loop. In this case, drop the 13971 * packet and let the sender try 13972 * sending a good packet. 13973 */ 13974 if (tmp_rnxt == tcp->tcp_rnxt) { 13975 freemsg(mp); 13976 return; 13977 } 13978 } 13979 tcp_rput_data(connp, mp, NULL); 13980 return; 13981 } 13982 /* 13983 * This segment contains only the urgent byte. We 13984 * have to allocate the T_exdata_ind, if we can. 13985 */ 13986 if (IPCL_IS_NONSTR(connp)) { 13987 int error; 13988 13989 (*connp->conn_upcalls->su_recv) 13990 (connp->conn_upper_handle, mp, seg_len, 13991 MSG_OOB, &error, NULL); 13992 mp = NULL; 13993 goto update_ack; 13994 } else if (!tcp->tcp_urp_mp) { 13995 struct T_exdata_ind *tei; 13996 mp1 = allocb(sizeof (struct T_exdata_ind), 13997 BPRI_MED); 13998 if (!mp1) { 13999 /* 14000 * Sigh... It'll be back. 14001 * Generate any MSG*MARK message now. 14002 */ 14003 freemsg(mp); 14004 seg_len = 0; 14005 if (flags & TH_SEND_URP_MARK) { 14006 14007 14008 ASSERT(tcp->tcp_urp_mark_mp); 14009 tcp->tcp_urp_mark_mp->b_flag &= 14010 ~MSGNOTMARKNEXT; 14011 tcp->tcp_urp_mark_mp->b_flag |= 14012 MSGMARKNEXT; 14013 } 14014 goto ack_check; 14015 } 14016 mp1->b_datap->db_type = M_PROTO; 14017 tei = (struct T_exdata_ind *)mp1->b_rptr; 14018 tei->PRIM_type = T_EXDATA_IND; 14019 tei->MORE_flag = 0; 14020 mp1->b_wptr = (uchar_t *)&tei[1]; 14021 tcp->tcp_urp_mp = mp1; 14022 #ifdef DEBUG 14023 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14024 "tcp_rput: allocated exdata_ind %s", 14025 tcp_display(tcp, NULL, 14026 DISP_PORT_ONLY)); 14027 #endif /* DEBUG */ 14028 /* 14029 * There is no need to send a separate MSG*MARK 14030 * message since the T_EXDATA_IND will be sent 14031 * now. 14032 */ 14033 flags &= ~TH_SEND_URP_MARK; 14034 freemsg(tcp->tcp_urp_mark_mp); 14035 tcp->tcp_urp_mark_mp = NULL; 14036 } 14037 /* 14038 * Now we are all set. On the next putnext upstream, 14039 * tcp_urp_mp will be non-NULL and will get prepended 14040 * to what has to be this piece containing the urgent 14041 * byte. If for any reason we abort this segment below, 14042 * if it comes back, we will have this ready, or it 14043 * will get blown off in close. 14044 */ 14045 } else if (urp == seg_len) { 14046 /* 14047 * The urgent byte is the next byte after this sequence 14048 * number. If there is data it is marked with 14049 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 14050 * since it is not needed. Otherwise, if the code 14051 * above just allocated a zero-length tcp_urp_mark_mp 14052 * message, that message is tagged with MSGMARKNEXT. 14053 * Sending up these MSGMARKNEXT messages makes 14054 * SIOCATMARK work correctly even though 14055 * the T_EXDATA_IND will not be sent up until the 14056 * urgent byte arrives. 14057 */ 14058 if (seg_len != 0) { 14059 flags |= TH_MARKNEXT_NEEDED; 14060 freemsg(tcp->tcp_urp_mark_mp); 14061 tcp->tcp_urp_mark_mp = NULL; 14062 flags &= ~TH_SEND_URP_MARK; 14063 } else if (tcp->tcp_urp_mark_mp != NULL) { 14064 flags |= TH_SEND_URP_MARK; 14065 tcp->tcp_urp_mark_mp->b_flag &= 14066 ~MSGNOTMARKNEXT; 14067 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 14068 } 14069 #ifdef DEBUG 14070 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14071 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 14072 seg_len, flags, 14073 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14074 #endif /* DEBUG */ 14075 } 14076 #ifdef DEBUG 14077 else { 14078 /* Data left until we hit mark */ 14079 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14080 "tcp_rput: URP %d bytes left, %s", 14081 urp - seg_len, tcp_display(tcp, NULL, 14082 DISP_PORT_ONLY)); 14083 } 14084 #endif /* DEBUG */ 14085 } 14086 14087 process_ack: 14088 if (!(flags & TH_ACK)) { 14089 freemsg(mp); 14090 goto xmit_check; 14091 } 14092 } 14093 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 14094 14095 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 14096 tcp->tcp_ip_forward_progress = B_TRUE; 14097 if (tcp->tcp_state == TCPS_SYN_RCVD) { 14098 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 14099 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 14100 /* 3-way handshake complete - pass up the T_CONN_IND */ 14101 tcp_t *listener = tcp->tcp_listener; 14102 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 14103 14104 tcp->tcp_tconnind_started = B_TRUE; 14105 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 14106 /* 14107 * We are here means eager is fine but it can 14108 * get a TH_RST at any point between now and till 14109 * accept completes and disappear. We need to 14110 * ensure that reference to eager is valid after 14111 * we get out of eager's perimeter. So we do 14112 * an extra refhold. 14113 */ 14114 CONN_INC_REF(connp); 14115 14116 /* 14117 * The listener also exists because of the refhold 14118 * done in tcp_conn_request. Its possible that it 14119 * might have closed. We will check that once we 14120 * get inside listeners context. 14121 */ 14122 CONN_INC_REF(listener->tcp_connp); 14123 if (listener->tcp_connp->conn_sqp == 14124 connp->conn_sqp) { 14125 /* 14126 * We optimize by not calling an SQUEUE_ENTER 14127 * on the listener since we know that the 14128 * listener and eager squeues are the same. 14129 * We are able to make this check safely only 14130 * because neither the eager nor the listener 14131 * can change its squeue. Only an active connect 14132 * can change its squeue 14133 */ 14134 tcp_send_conn_ind(listener->tcp_connp, mp, 14135 listener->tcp_connp->conn_sqp); 14136 CONN_DEC_REF(listener->tcp_connp); 14137 } else if (!tcp->tcp_loopback) { 14138 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14139 mp, tcp_send_conn_ind, 14140 listener->tcp_connp, SQ_FILL, 14141 SQTAG_TCP_CONN_IND); 14142 } else { 14143 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 14144 mp, tcp_send_conn_ind, 14145 listener->tcp_connp, SQ_PROCESS, 14146 SQTAG_TCP_CONN_IND); 14147 } 14148 } 14149 14150 if (tcp->tcp_active_open) { 14151 /* 14152 * We are seeing the final ack in the three way 14153 * hand shake of a active open'ed connection 14154 * so we must send up a T_CONN_CON 14155 */ 14156 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 14157 freemsg(mp); 14158 return; 14159 } 14160 /* 14161 * Don't fuse the loopback endpoints for 14162 * simultaneous active opens. 14163 */ 14164 if (tcp->tcp_loopback) { 14165 TCP_STAT(tcps, tcp_fusion_unfusable); 14166 tcp->tcp_unfusable = B_TRUE; 14167 } 14168 } 14169 14170 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14171 bytes_acked--; 14172 /* SYN was acked - making progress */ 14173 if (tcp->tcp_ipversion == IPV6_VERSION) 14174 tcp->tcp_ip_forward_progress = B_TRUE; 14175 14176 /* 14177 * If SYN was retransmitted, need to reset all 14178 * retransmission info as this segment will be 14179 * treated as a dup ACK. 14180 */ 14181 if (tcp->tcp_rexmit) { 14182 tcp->tcp_rexmit = B_FALSE; 14183 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14184 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14185 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14186 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14187 tcp->tcp_ms_we_have_waited = 0; 14188 tcp->tcp_cwnd = mss; 14189 } 14190 14191 /* 14192 * We set the send window to zero here. 14193 * This is needed if there is data to be 14194 * processed already on the queue. 14195 * Later (at swnd_update label), the 14196 * "new_swnd > tcp_swnd" condition is satisfied 14197 * the XMIT_NEEDED flag is set in the current 14198 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14199 * called if there is already data on queue in 14200 * this state. 14201 */ 14202 tcp->tcp_swnd = 0; 14203 14204 if (new_swnd > tcp->tcp_max_swnd) 14205 tcp->tcp_max_swnd = new_swnd; 14206 tcp->tcp_swl1 = seg_seq; 14207 tcp->tcp_swl2 = seg_ack; 14208 tcp->tcp_state = TCPS_ESTABLISHED; 14209 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14210 14211 /* Fuse when both sides are in ESTABLISHED state */ 14212 if (tcp->tcp_loopback && do_tcp_fusion) 14213 tcp_fuse(tcp, iphdr, tcph); 14214 14215 } 14216 /* This code follows 4.4BSD-Lite2 mostly. */ 14217 if (bytes_acked < 0) 14218 goto est; 14219 14220 /* 14221 * If TCP is ECN capable and the congestion experience bit is 14222 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14223 * done once per window (or more loosely, per RTT). 14224 */ 14225 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14226 tcp->tcp_cwr = B_FALSE; 14227 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14228 if (!tcp->tcp_cwr) { 14229 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14230 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14231 tcp->tcp_cwnd = npkt * mss; 14232 /* 14233 * If the cwnd is 0, use the timer to clock out 14234 * new segments. This is required by the ECN spec. 14235 */ 14236 if (npkt == 0) { 14237 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14238 /* 14239 * This makes sure that when the ACK comes 14240 * back, we will increase tcp_cwnd by 1 MSS. 14241 */ 14242 tcp->tcp_cwnd_cnt = 0; 14243 } 14244 tcp->tcp_cwr = B_TRUE; 14245 /* 14246 * This marks the end of the current window of in 14247 * flight data. That is why we don't use 14248 * tcp_suna + tcp_swnd. Only data in flight can 14249 * provide ECN info. 14250 */ 14251 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14252 tcp->tcp_ecn_cwr_sent = B_FALSE; 14253 } 14254 } 14255 14256 mp1 = tcp->tcp_xmit_head; 14257 if (bytes_acked == 0) { 14258 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14259 int dupack_cnt; 14260 14261 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14262 /* 14263 * Fast retransmit. When we have seen exactly three 14264 * identical ACKs while we have unacked data 14265 * outstanding we take it as a hint that our peer 14266 * dropped something. 14267 * 14268 * If TCP is retransmitting, don't do fast retransmit. 14269 */ 14270 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14271 ! tcp->tcp_rexmit) { 14272 /* Do Limited Transmit */ 14273 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14274 tcps->tcps_dupack_fast_retransmit) { 14275 /* 14276 * RFC 3042 14277 * 14278 * What we need to do is temporarily 14279 * increase tcp_cwnd so that new 14280 * data can be sent if it is allowed 14281 * by the receive window (tcp_rwnd). 14282 * tcp_wput_data() will take care of 14283 * the rest. 14284 * 14285 * If the connection is SACK capable, 14286 * only do limited xmit when there 14287 * is SACK info. 14288 * 14289 * Note how tcp_cwnd is incremented. 14290 * The first dup ACK will increase 14291 * it by 1 MSS. The second dup ACK 14292 * will increase it by 2 MSS. This 14293 * means that only 1 new segment will 14294 * be sent for each dup ACK. 14295 */ 14296 if (tcp->tcp_unsent > 0 && 14297 (!tcp->tcp_snd_sack_ok || 14298 (tcp->tcp_snd_sack_ok && 14299 tcp->tcp_notsack_list != NULL))) { 14300 tcp->tcp_cwnd += mss << 14301 (tcp->tcp_dupack_cnt - 1); 14302 flags |= TH_LIMIT_XMIT; 14303 } 14304 } else if (dupack_cnt == 14305 tcps->tcps_dupack_fast_retransmit) { 14306 14307 /* 14308 * If we have reduced tcp_ssthresh 14309 * because of ECN, do not reduce it again 14310 * unless it is already one window of data 14311 * away. After one window of data, tcp_cwr 14312 * should then be cleared. Note that 14313 * for non ECN capable connection, tcp_cwr 14314 * should always be false. 14315 * 14316 * Adjust cwnd since the duplicate 14317 * ack indicates that a packet was 14318 * dropped (due to congestion.) 14319 */ 14320 if (!tcp->tcp_cwr) { 14321 npkt = ((tcp->tcp_snxt - 14322 tcp->tcp_suna) >> 1) / mss; 14323 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14324 mss; 14325 tcp->tcp_cwnd = (npkt + 14326 tcp->tcp_dupack_cnt) * mss; 14327 } 14328 if (tcp->tcp_ecn_ok) { 14329 tcp->tcp_cwr = B_TRUE; 14330 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14331 tcp->tcp_ecn_cwr_sent = B_FALSE; 14332 } 14333 14334 /* 14335 * We do Hoe's algorithm. Refer to her 14336 * paper "Improving the Start-up Behavior 14337 * of a Congestion Control Scheme for TCP," 14338 * appeared in SIGCOMM'96. 14339 * 14340 * Save highest seq no we have sent so far. 14341 * Be careful about the invisible FIN byte. 14342 */ 14343 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14344 (tcp->tcp_unsent == 0)) { 14345 tcp->tcp_rexmit_max = tcp->tcp_fss; 14346 } else { 14347 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14348 } 14349 14350 /* 14351 * Do not allow bursty traffic during. 14352 * fast recovery. Refer to Fall and Floyd's 14353 * paper "Simulation-based Comparisons of 14354 * Tahoe, Reno and SACK TCP" (in CCR?) 14355 * This is a best current practise. 14356 */ 14357 tcp->tcp_snd_burst = TCP_CWND_SS; 14358 14359 /* 14360 * For SACK: 14361 * Calculate tcp_pipe, which is the 14362 * estimated number of bytes in 14363 * network. 14364 * 14365 * tcp_fack is the highest sack'ed seq num 14366 * TCP has received. 14367 * 14368 * tcp_pipe is explained in the above quoted 14369 * Fall and Floyd's paper. tcp_fack is 14370 * explained in Mathis and Mahdavi's 14371 * "Forward Acknowledgment: Refining TCP 14372 * Congestion Control" in SIGCOMM '96. 14373 */ 14374 if (tcp->tcp_snd_sack_ok) { 14375 ASSERT(tcp->tcp_sack_info != NULL); 14376 if (tcp->tcp_notsack_list != NULL) { 14377 tcp->tcp_pipe = tcp->tcp_snxt - 14378 tcp->tcp_fack; 14379 tcp->tcp_sack_snxt = seg_ack; 14380 flags |= TH_NEED_SACK_REXMIT; 14381 } else { 14382 /* 14383 * Always initialize tcp_pipe 14384 * even though we don't have 14385 * any SACK info. If later 14386 * we get SACK info and 14387 * tcp_pipe is not initialized, 14388 * funny things will happen. 14389 */ 14390 tcp->tcp_pipe = 14391 tcp->tcp_cwnd_ssthresh; 14392 } 14393 } else { 14394 flags |= TH_REXMIT_NEEDED; 14395 } /* tcp_snd_sack_ok */ 14396 14397 } else { 14398 /* 14399 * Here we perform congestion 14400 * avoidance, but NOT slow start. 14401 * This is known as the Fast 14402 * Recovery Algorithm. 14403 */ 14404 if (tcp->tcp_snd_sack_ok && 14405 tcp->tcp_notsack_list != NULL) { 14406 flags |= TH_NEED_SACK_REXMIT; 14407 tcp->tcp_pipe -= mss; 14408 if (tcp->tcp_pipe < 0) 14409 tcp->tcp_pipe = 0; 14410 } else { 14411 /* 14412 * We know that one more packet has 14413 * left the pipe thus we can update 14414 * cwnd. 14415 */ 14416 cwnd = tcp->tcp_cwnd + mss; 14417 if (cwnd > tcp->tcp_cwnd_max) 14418 cwnd = tcp->tcp_cwnd_max; 14419 tcp->tcp_cwnd = cwnd; 14420 if (tcp->tcp_unsent > 0) 14421 flags |= TH_XMIT_NEEDED; 14422 } 14423 } 14424 } 14425 } else if (tcp->tcp_zero_win_probe) { 14426 /* 14427 * If the window has opened, need to arrange 14428 * to send additional data. 14429 */ 14430 if (new_swnd != 0) { 14431 /* tcp_suna != tcp_snxt */ 14432 /* Packet contains a window update */ 14433 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14434 tcp->tcp_zero_win_probe = 0; 14435 tcp->tcp_timer_backoff = 0; 14436 tcp->tcp_ms_we_have_waited = 0; 14437 14438 /* 14439 * Transmit starting with tcp_suna since 14440 * the one byte probe is not ack'ed. 14441 * If TCP has sent more than one identical 14442 * probe, tcp_rexmit will be set. That means 14443 * tcp_ss_rexmit() will send out the one 14444 * byte along with new data. Otherwise, 14445 * fake the retransmission. 14446 */ 14447 flags |= TH_XMIT_NEEDED; 14448 if (!tcp->tcp_rexmit) { 14449 tcp->tcp_rexmit = B_TRUE; 14450 tcp->tcp_dupack_cnt = 0; 14451 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14452 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14453 } 14454 } 14455 } 14456 goto swnd_update; 14457 } 14458 14459 /* 14460 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14461 * If the ACK value acks something that we have not yet sent, it might 14462 * be an old duplicate segment. Send an ACK to re-synchronize the 14463 * other side. 14464 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14465 * state is handled above, so we can always just drop the segment and 14466 * send an ACK here. 14467 * 14468 * Should we send ACKs in response to ACK only segments? 14469 */ 14470 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14471 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14472 /* drop the received segment */ 14473 freemsg(mp); 14474 14475 /* 14476 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14477 * greater than 0, check if the number of such 14478 * bogus ACks is greater than that count. If yes, 14479 * don't send back any ACK. This prevents TCP from 14480 * getting into an ACK storm if somehow an attacker 14481 * successfully spoofs an acceptable segment to our 14482 * peer. 14483 */ 14484 if (tcp_drop_ack_unsent_cnt > 0 && 14485 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14486 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14487 return; 14488 } 14489 mp = tcp_ack_mp(tcp); 14490 if (mp != NULL) { 14491 BUMP_LOCAL(tcp->tcp_obsegs); 14492 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14493 tcp_send_data(tcp, tcp->tcp_wq, mp); 14494 } 14495 return; 14496 } 14497 14498 /* 14499 * TCP gets a new ACK, update the notsack'ed list to delete those 14500 * blocks that are covered by this ACK. 14501 */ 14502 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14503 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14504 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14505 } 14506 14507 /* 14508 * If we got an ACK after fast retransmit, check to see 14509 * if it is a partial ACK. If it is not and the congestion 14510 * window was inflated to account for the other side's 14511 * cached packets, retract it. If it is, do Hoe's algorithm. 14512 */ 14513 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14514 ASSERT(tcp->tcp_rexmit == B_FALSE); 14515 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14516 tcp->tcp_dupack_cnt = 0; 14517 /* 14518 * Restore the orig tcp_cwnd_ssthresh after 14519 * fast retransmit phase. 14520 */ 14521 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14522 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14523 } 14524 tcp->tcp_rexmit_max = seg_ack; 14525 tcp->tcp_cwnd_cnt = 0; 14526 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14527 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14528 14529 /* 14530 * Remove all notsack info to avoid confusion with 14531 * the next fast retrasnmit/recovery phase. 14532 */ 14533 if (tcp->tcp_snd_sack_ok && 14534 tcp->tcp_notsack_list != NULL) { 14535 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14536 } 14537 } else { 14538 if (tcp->tcp_snd_sack_ok && 14539 tcp->tcp_notsack_list != NULL) { 14540 flags |= TH_NEED_SACK_REXMIT; 14541 tcp->tcp_pipe -= mss; 14542 if (tcp->tcp_pipe < 0) 14543 tcp->tcp_pipe = 0; 14544 } else { 14545 /* 14546 * Hoe's algorithm: 14547 * 14548 * Retransmit the unack'ed segment and 14549 * restart fast recovery. Note that we 14550 * need to scale back tcp_cwnd to the 14551 * original value when we started fast 14552 * recovery. This is to prevent overly 14553 * aggressive behaviour in sending new 14554 * segments. 14555 */ 14556 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14557 tcps->tcps_dupack_fast_retransmit * mss; 14558 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14559 flags |= TH_REXMIT_NEEDED; 14560 } 14561 } 14562 } else { 14563 tcp->tcp_dupack_cnt = 0; 14564 if (tcp->tcp_rexmit) { 14565 /* 14566 * TCP is retranmitting. If the ACK ack's all 14567 * outstanding data, update tcp_rexmit_max and 14568 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14569 * to the correct value. 14570 * 14571 * Note that SEQ_LEQ() is used. This is to avoid 14572 * unnecessary fast retransmit caused by dup ACKs 14573 * received when TCP does slow start retransmission 14574 * after a time out. During this phase, TCP may 14575 * send out segments which are already received. 14576 * This causes dup ACKs to be sent back. 14577 */ 14578 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14579 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14580 tcp->tcp_rexmit_nxt = seg_ack; 14581 } 14582 if (seg_ack != tcp->tcp_rexmit_max) { 14583 flags |= TH_XMIT_NEEDED; 14584 } 14585 } else { 14586 tcp->tcp_rexmit = B_FALSE; 14587 tcp->tcp_xmit_zc_clean = B_FALSE; 14588 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14589 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14590 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14591 } 14592 tcp->tcp_ms_we_have_waited = 0; 14593 } 14594 } 14595 14596 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14597 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14598 tcp->tcp_suna = seg_ack; 14599 if (tcp->tcp_zero_win_probe != 0) { 14600 tcp->tcp_zero_win_probe = 0; 14601 tcp->tcp_timer_backoff = 0; 14602 } 14603 14604 /* 14605 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14606 * Note that it cannot be the SYN being ack'ed. The code flow 14607 * will not reach here. 14608 */ 14609 if (mp1 == NULL) { 14610 goto fin_acked; 14611 } 14612 14613 /* 14614 * Update the congestion window. 14615 * 14616 * If TCP is not ECN capable or TCP is ECN capable but the 14617 * congestion experience bit is not set, increase the tcp_cwnd as 14618 * usual. 14619 */ 14620 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14621 cwnd = tcp->tcp_cwnd; 14622 add = mss; 14623 14624 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14625 /* 14626 * This is to prevent an increase of less than 1 MSS of 14627 * tcp_cwnd. With partial increase, tcp_wput_data() 14628 * may send out tinygrams in order to preserve mblk 14629 * boundaries. 14630 * 14631 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14632 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14633 * increased by 1 MSS for every RTTs. 14634 */ 14635 if (tcp->tcp_cwnd_cnt <= 0) { 14636 tcp->tcp_cwnd_cnt = cwnd + add; 14637 } else { 14638 tcp->tcp_cwnd_cnt -= add; 14639 add = 0; 14640 } 14641 } 14642 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14643 } 14644 14645 /* See if the latest urgent data has been acknowledged */ 14646 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14647 SEQ_GT(seg_ack, tcp->tcp_urg)) 14648 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14649 14650 /* Can we update the RTT estimates? */ 14651 if (tcp->tcp_snd_ts_ok) { 14652 /* Ignore zero timestamp echo-reply. */ 14653 if (tcpopt.tcp_opt_ts_ecr != 0) { 14654 tcp_set_rto(tcp, (int32_t)lbolt - 14655 (int32_t)tcpopt.tcp_opt_ts_ecr); 14656 } 14657 14658 /* If needed, restart the timer. */ 14659 if (tcp->tcp_set_timer == 1) { 14660 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14661 tcp->tcp_set_timer = 0; 14662 } 14663 /* 14664 * Update tcp_csuna in case the other side stops sending 14665 * us timestamps. 14666 */ 14667 tcp->tcp_csuna = tcp->tcp_snxt; 14668 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14669 /* 14670 * An ACK sequence we haven't seen before, so get the RTT 14671 * and update the RTO. But first check if the timestamp is 14672 * valid to use. 14673 */ 14674 if ((mp1->b_next != NULL) && 14675 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14676 tcp_set_rto(tcp, (int32_t)lbolt - 14677 (int32_t)(intptr_t)mp1->b_prev); 14678 else 14679 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14680 14681 /* Remeber the last sequence to be ACKed */ 14682 tcp->tcp_csuna = seg_ack; 14683 if (tcp->tcp_set_timer == 1) { 14684 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14685 tcp->tcp_set_timer = 0; 14686 } 14687 } else { 14688 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14689 } 14690 14691 /* Eat acknowledged bytes off the xmit queue. */ 14692 for (;;) { 14693 mblk_t *mp2; 14694 uchar_t *wptr; 14695 14696 wptr = mp1->b_wptr; 14697 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14698 bytes_acked -= (int)(wptr - mp1->b_rptr); 14699 if (bytes_acked < 0) { 14700 mp1->b_rptr = wptr + bytes_acked; 14701 /* 14702 * Set a new timestamp if all the bytes timed by the 14703 * old timestamp have been ack'ed. 14704 */ 14705 if (SEQ_GT(seg_ack, 14706 (uint32_t)(uintptr_t)(mp1->b_next))) { 14707 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14708 mp1->b_next = NULL; 14709 } 14710 break; 14711 } 14712 mp1->b_next = NULL; 14713 mp1->b_prev = NULL; 14714 mp2 = mp1; 14715 mp1 = mp1->b_cont; 14716 14717 /* 14718 * This notification is required for some zero-copy 14719 * clients to maintain a copy semantic. After the data 14720 * is ack'ed, client is safe to modify or reuse the buffer. 14721 */ 14722 if (tcp->tcp_snd_zcopy_aware && 14723 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14724 tcp_zcopy_notify(tcp); 14725 freeb(mp2); 14726 if (bytes_acked == 0) { 14727 if (mp1 == NULL) { 14728 /* Everything is ack'ed, clear the tail. */ 14729 tcp->tcp_xmit_tail = NULL; 14730 /* 14731 * Cancel the timer unless we are still 14732 * waiting for an ACK for the FIN packet. 14733 */ 14734 if (tcp->tcp_timer_tid != 0 && 14735 tcp->tcp_snxt == tcp->tcp_suna) { 14736 (void) TCP_TIMER_CANCEL(tcp, 14737 tcp->tcp_timer_tid); 14738 tcp->tcp_timer_tid = 0; 14739 } 14740 goto pre_swnd_update; 14741 } 14742 if (mp2 != tcp->tcp_xmit_tail) 14743 break; 14744 tcp->tcp_xmit_tail = mp1; 14745 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14746 (uintptr_t)INT_MAX); 14747 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14748 mp1->b_rptr); 14749 break; 14750 } 14751 if (mp1 == NULL) { 14752 /* 14753 * More was acked but there is nothing more 14754 * outstanding. This means that the FIN was 14755 * just acked or that we're talking to a clown. 14756 */ 14757 fin_acked: 14758 ASSERT(tcp->tcp_fin_sent); 14759 tcp->tcp_xmit_tail = NULL; 14760 if (tcp->tcp_fin_sent) { 14761 /* FIN was acked - making progress */ 14762 if (tcp->tcp_ipversion == IPV6_VERSION && 14763 !tcp->tcp_fin_acked) 14764 tcp->tcp_ip_forward_progress = B_TRUE; 14765 tcp->tcp_fin_acked = B_TRUE; 14766 if (tcp->tcp_linger_tid != 0 && 14767 TCP_TIMER_CANCEL(tcp, 14768 tcp->tcp_linger_tid) >= 0) { 14769 tcp_stop_lingering(tcp); 14770 freemsg(mp); 14771 mp = NULL; 14772 } 14773 } else { 14774 /* 14775 * We should never get here because 14776 * we have already checked that the 14777 * number of bytes ack'ed should be 14778 * smaller than or equal to what we 14779 * have sent so far (it is the 14780 * acceptability check of the ACK). 14781 * We can only get here if the send 14782 * queue is corrupted. 14783 * 14784 * Terminate the connection and 14785 * panic the system. It is better 14786 * for us to panic instead of 14787 * continuing to avoid other disaster. 14788 */ 14789 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14790 tcp->tcp_rnxt, TH_RST|TH_ACK); 14791 panic("Memory corruption " 14792 "detected for connection %s.", 14793 tcp_display(tcp, NULL, 14794 DISP_ADDR_AND_PORT)); 14795 /*NOTREACHED*/ 14796 } 14797 goto pre_swnd_update; 14798 } 14799 ASSERT(mp2 != tcp->tcp_xmit_tail); 14800 } 14801 if (tcp->tcp_unsent) { 14802 flags |= TH_XMIT_NEEDED; 14803 } 14804 pre_swnd_update: 14805 tcp->tcp_xmit_head = mp1; 14806 swnd_update: 14807 /* 14808 * The following check is different from most other implementations. 14809 * For bi-directional transfer, when segments are dropped, the 14810 * "normal" check will not accept a window update in those 14811 * retransmitted segemnts. Failing to do that, TCP may send out 14812 * segments which are outside receiver's window. As TCP accepts 14813 * the ack in those retransmitted segments, if the window update in 14814 * the same segment is not accepted, TCP will incorrectly calculates 14815 * that it can send more segments. This can create a deadlock 14816 * with the receiver if its window becomes zero. 14817 */ 14818 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14819 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14820 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14821 /* 14822 * The criteria for update is: 14823 * 14824 * 1. the segment acknowledges some data. Or 14825 * 2. the segment is new, i.e. it has a higher seq num. Or 14826 * 3. the segment is not old and the advertised window is 14827 * larger than the previous advertised window. 14828 */ 14829 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14830 flags |= TH_XMIT_NEEDED; 14831 tcp->tcp_swnd = new_swnd; 14832 if (new_swnd > tcp->tcp_max_swnd) 14833 tcp->tcp_max_swnd = new_swnd; 14834 tcp->tcp_swl1 = seg_seq; 14835 tcp->tcp_swl2 = seg_ack; 14836 } 14837 est: 14838 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14839 14840 switch (tcp->tcp_state) { 14841 case TCPS_FIN_WAIT_1: 14842 if (tcp->tcp_fin_acked) { 14843 tcp->tcp_state = TCPS_FIN_WAIT_2; 14844 /* 14845 * We implement the non-standard BSD/SunOS 14846 * FIN_WAIT_2 flushing algorithm. 14847 * If there is no user attached to this 14848 * TCP endpoint, then this TCP struct 14849 * could hang around forever in FIN_WAIT_2 14850 * state if the peer forgets to send us 14851 * a FIN. To prevent this, we wait only 14852 * 2*MSL (a convenient time value) for 14853 * the FIN to arrive. If it doesn't show up, 14854 * we flush the TCP endpoint. This algorithm, 14855 * though a violation of RFC-793, has worked 14856 * for over 10 years in BSD systems. 14857 * Note: SunOS 4.x waits 675 seconds before 14858 * flushing the FIN_WAIT_2 connection. 14859 */ 14860 TCP_TIMER_RESTART(tcp, 14861 tcps->tcps_fin_wait_2_flush_interval); 14862 } 14863 break; 14864 case TCPS_FIN_WAIT_2: 14865 break; /* Shutdown hook? */ 14866 case TCPS_LAST_ACK: 14867 freemsg(mp); 14868 if (tcp->tcp_fin_acked) { 14869 (void) tcp_clean_death(tcp, 0, 19); 14870 return; 14871 } 14872 goto xmit_check; 14873 case TCPS_CLOSING: 14874 if (tcp->tcp_fin_acked) { 14875 tcp->tcp_state = TCPS_TIME_WAIT; 14876 /* 14877 * Unconditionally clear the exclusive binding 14878 * bit so this TIME-WAIT connection won't 14879 * interfere with new ones. 14880 */ 14881 tcp->tcp_exclbind = 0; 14882 if (!TCP_IS_DETACHED(tcp)) { 14883 TCP_TIMER_RESTART(tcp, 14884 tcps->tcps_time_wait_interval); 14885 } else { 14886 tcp_time_wait_append(tcp); 14887 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14888 } 14889 } 14890 /*FALLTHRU*/ 14891 case TCPS_CLOSE_WAIT: 14892 freemsg(mp); 14893 goto xmit_check; 14894 default: 14895 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14896 break; 14897 } 14898 } 14899 if (flags & TH_FIN) { 14900 /* Make sure we ack the fin */ 14901 flags |= TH_ACK_NEEDED; 14902 if (!tcp->tcp_fin_rcvd) { 14903 tcp->tcp_fin_rcvd = B_TRUE; 14904 tcp->tcp_rnxt++; 14905 tcph = tcp->tcp_tcph; 14906 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14907 14908 /* 14909 * Generate the ordrel_ind at the end unless we 14910 * are an eager guy. 14911 * In the eager case tcp_rsrv will do this when run 14912 * after tcp_accept is done. 14913 */ 14914 if (tcp->tcp_listener == NULL && 14915 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14916 flags |= TH_ORDREL_NEEDED; 14917 switch (tcp->tcp_state) { 14918 case TCPS_SYN_RCVD: 14919 case TCPS_ESTABLISHED: 14920 tcp->tcp_state = TCPS_CLOSE_WAIT; 14921 /* Keepalive? */ 14922 break; 14923 case TCPS_FIN_WAIT_1: 14924 if (!tcp->tcp_fin_acked) { 14925 tcp->tcp_state = TCPS_CLOSING; 14926 break; 14927 } 14928 /* FALLTHRU */ 14929 case TCPS_FIN_WAIT_2: 14930 tcp->tcp_state = TCPS_TIME_WAIT; 14931 /* 14932 * Unconditionally clear the exclusive binding 14933 * bit so this TIME-WAIT connection won't 14934 * interfere with new ones. 14935 */ 14936 tcp->tcp_exclbind = 0; 14937 if (!TCP_IS_DETACHED(tcp)) { 14938 TCP_TIMER_RESTART(tcp, 14939 tcps->tcps_time_wait_interval); 14940 } else { 14941 tcp_time_wait_append(tcp); 14942 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14943 } 14944 if (seg_len) { 14945 /* 14946 * implies data piggybacked on FIN. 14947 * break to handle data. 14948 */ 14949 break; 14950 } 14951 freemsg(mp); 14952 goto ack_check; 14953 } 14954 } 14955 } 14956 if (mp == NULL) 14957 goto xmit_check; 14958 if (seg_len == 0) { 14959 freemsg(mp); 14960 goto xmit_check; 14961 } 14962 if (mp->b_rptr == mp->b_wptr) { 14963 /* 14964 * The header has been consumed, so we remove the 14965 * zero-length mblk here. 14966 */ 14967 mp1 = mp; 14968 mp = mp->b_cont; 14969 freeb(mp1); 14970 } 14971 update_ack: 14972 tcph = tcp->tcp_tcph; 14973 tcp->tcp_rack_cnt++; 14974 { 14975 uint32_t cur_max; 14976 14977 cur_max = tcp->tcp_rack_cur_max; 14978 if (tcp->tcp_rack_cnt >= cur_max) { 14979 /* 14980 * We have more unacked data than we should - send 14981 * an ACK now. 14982 */ 14983 flags |= TH_ACK_NEEDED; 14984 cur_max++; 14985 if (cur_max > tcp->tcp_rack_abs_max) 14986 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14987 else 14988 tcp->tcp_rack_cur_max = cur_max; 14989 } else if (TCP_IS_DETACHED(tcp)) { 14990 /* We don't have an ACK timer for detached TCP. */ 14991 flags |= TH_ACK_NEEDED; 14992 } else if (seg_len < mss) { 14993 /* 14994 * If we get a segment that is less than an mss, and we 14995 * already have unacknowledged data, and the amount 14996 * unacknowledged is not a multiple of mss, then we 14997 * better generate an ACK now. Otherwise, this may be 14998 * the tail piece of a transaction, and we would rather 14999 * wait for the response. 15000 */ 15001 uint32_t udif; 15002 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 15003 (uintptr_t)INT_MAX); 15004 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 15005 if (udif && (udif % mss)) 15006 flags |= TH_ACK_NEEDED; 15007 else 15008 flags |= TH_ACK_TIMER_NEEDED; 15009 } else { 15010 /* Start delayed ack timer */ 15011 flags |= TH_ACK_TIMER_NEEDED; 15012 } 15013 } 15014 tcp->tcp_rnxt += seg_len; 15015 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 15016 15017 if (mp == NULL) 15018 goto xmit_check; 15019 15020 /* Update SACK list */ 15021 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 15022 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 15023 &(tcp->tcp_num_sack_blk)); 15024 } 15025 15026 if (tcp->tcp_urp_mp) { 15027 tcp->tcp_urp_mp->b_cont = mp; 15028 mp = tcp->tcp_urp_mp; 15029 tcp->tcp_urp_mp = NULL; 15030 /* Ready for a new signal. */ 15031 tcp->tcp_urp_last_valid = B_FALSE; 15032 #ifdef DEBUG 15033 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15034 "tcp_rput: sending exdata_ind %s", 15035 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15036 #endif /* DEBUG */ 15037 } 15038 15039 /* 15040 * Check for ancillary data changes compared to last segment. 15041 */ 15042 if (tcp->tcp_ipv6_recvancillary != 0) { 15043 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 15044 ASSERT(mp != NULL); 15045 } 15046 15047 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 15048 /* 15049 * Side queue inbound data until the accept happens. 15050 * tcp_accept/tcp_rput drains this when the accept happens. 15051 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 15052 * T_EXDATA_IND) it is queued on b_next. 15053 * XXX Make urgent data use this. Requires: 15054 * Removing tcp_listener check for TH_URG 15055 * Making M_PCPROTO and MARK messages skip the eager case 15056 */ 15057 15058 if (tcp->tcp_kssl_pending) { 15059 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 15060 mblk_t *, mp); 15061 tcp_kssl_input(tcp, mp); 15062 } else { 15063 tcp_rcv_enqueue(tcp, mp, seg_len); 15064 } 15065 } else { 15066 sodirect_t *sodp = tcp->tcp_sodirect; 15067 15068 /* 15069 * If an sodirect connection and an enabled sodirect_t then 15070 * sodp will be set to point to the tcp_t/sonode_t shared 15071 * sodirect_t and the sodirect_t's lock will be held. 15072 */ 15073 if (sodp != NULL) { 15074 mutex_enter(sodp->sod_lockp); 15075 if (!(sodp->sod_state & SOD_ENABLED) || 15076 (tcp->tcp_kssl_ctx != NULL && 15077 DB_TYPE(mp) == M_DATA)) { 15078 sodp = NULL; 15079 } 15080 mutex_exit(sodp->sod_lockp); 15081 } 15082 if (mp->b_datap->db_type != M_DATA || 15083 (flags & TH_MARKNEXT_NEEDED)) { 15084 if (IPCL_IS_NONSTR(connp)) { 15085 int error; 15086 15087 if ((*connp->conn_upcalls->su_recv) 15088 (connp->conn_upper_handle, mp, 15089 seg_len, 0, &error, NULL) <= 0) { 15090 if (error == ENOSPC) { 15091 tcp->tcp_rwnd -= seg_len; 15092 } else if (error == EOPNOTSUPP) { 15093 tcp_rcv_enqueue(tcp, mp, 15094 seg_len); 15095 } 15096 } 15097 } else if (sodp != NULL) { 15098 mutex_enter(sodp->sod_lockp); 15099 SOD_UIOAFINI(sodp); 15100 if (!SOD_QEMPTY(sodp) && 15101 (sodp->sod_state & SOD_WAKE_NOT)) { 15102 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15103 /* sod_wakeup() did the mutex_exit() */ 15104 } else { 15105 mutex_exit(sodp->sod_lockp); 15106 } 15107 } else if (tcp->tcp_rcv_list != NULL) { 15108 flags |= tcp_rcv_drain(tcp); 15109 } 15110 ASSERT(tcp->tcp_rcv_list == NULL || 15111 tcp->tcp_fused_sigurg); 15112 15113 if (flags & TH_MARKNEXT_NEEDED) { 15114 #ifdef DEBUG 15115 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15116 "tcp_rput: sending MSGMARKNEXT %s", 15117 tcp_display(tcp, NULL, 15118 DISP_PORT_ONLY)); 15119 #endif /* DEBUG */ 15120 mp->b_flag |= MSGMARKNEXT; 15121 flags &= ~TH_MARKNEXT_NEEDED; 15122 } 15123 15124 /* Does this need SSL processing first? */ 15125 if ((tcp->tcp_kssl_ctx != NULL) && 15126 (DB_TYPE(mp) == M_DATA)) { 15127 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 15128 mblk_t *, mp); 15129 tcp_kssl_input(tcp, mp); 15130 } else if (!IPCL_IS_NONSTR(connp)) { 15131 /* Already handled non-STREAMS case. */ 15132 putnext(tcp->tcp_rq, mp); 15133 if (!canputnext(tcp->tcp_rq)) 15134 tcp->tcp_rwnd -= seg_len; 15135 } 15136 } else if ((tcp->tcp_kssl_ctx != NULL) && 15137 (DB_TYPE(mp) == M_DATA)) { 15138 /* Does this need SSL processing first? */ 15139 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 15140 tcp_kssl_input(tcp, mp); 15141 } else if (IPCL_IS_NONSTR(connp)) { 15142 /* Non-STREAMS socket */ 15143 boolean_t push = flags & (TH_PUSH|TH_FIN); 15144 int error; 15145 15146 if ((*connp->conn_upcalls->su_recv)( 15147 connp->conn_upper_handle, 15148 mp, seg_len, 0, &error, &push) <= 0) { 15149 if (error == ENOSPC) { 15150 tcp->tcp_rwnd -= seg_len; 15151 } else if (error == EOPNOTSUPP) { 15152 tcp_rcv_enqueue(tcp, mp, seg_len); 15153 } 15154 } else if (push) { 15155 /* 15156 * PUSH bit set and sockfs is not 15157 * flow controlled 15158 */ 15159 flags |= tcp_rwnd_reopen(tcp); 15160 } 15161 } else if (sodp != NULL) { 15162 /* 15163 * Sodirect so all mblk_t's are queued on the 15164 * socket directly, check for wakeup of blocked 15165 * reader (if any), and last if flow-controled. 15166 */ 15167 mutex_enter(sodp->sod_lockp); 15168 flags |= tcp_rcv_sod_enqueue(tcp, sodp, mp, seg_len); 15169 if ((sodp->sod_state & SOD_WAKE_NEED) || 15170 (flags & (TH_PUSH|TH_FIN))) { 15171 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15172 /* sod_wakeup() did the mutex_exit() */ 15173 } else { 15174 if (SOD_QFULL(sodp)) { 15175 /* Q is full, need backenable */ 15176 SOD_QSETBE(sodp); 15177 } 15178 mutex_exit(sodp->sod_lockp); 15179 } 15180 } else if ((flags & (TH_PUSH|TH_FIN)) || 15181 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15182 if (tcp->tcp_rcv_list != NULL) { 15183 /* 15184 * Enqueue the new segment first and then 15185 * call tcp_rcv_drain() to send all data 15186 * up. The other way to do this is to 15187 * send all queued data up and then call 15188 * putnext() to send the new segment up. 15189 * This way can remove the else part later 15190 * on. 15191 * 15192 * We don't do this to avoid one more call to 15193 * canputnext() as tcp_rcv_drain() needs to 15194 * call canputnext(). 15195 */ 15196 tcp_rcv_enqueue(tcp, mp, seg_len); 15197 flags |= tcp_rcv_drain(tcp); 15198 } else { 15199 putnext(tcp->tcp_rq, mp); 15200 if (!canputnext(tcp->tcp_rq)) 15201 tcp->tcp_rwnd -= seg_len; 15202 } 15203 } else { 15204 /* 15205 * Enqueue all packets when processing an mblk 15206 * from the co queue and also enqueue normal packets. 15207 * For packets which belong to SSL stream do SSL 15208 * processing first. 15209 */ 15210 tcp_rcv_enqueue(tcp, mp, seg_len); 15211 } 15212 /* 15213 * Make sure the timer is running if we have data waiting 15214 * for a push bit. This provides resiliency against 15215 * implementations that do not correctly generate push bits. 15216 * 15217 * Note, for sodirect if Q isn't empty and there's not a 15218 * pending wakeup then we need a timer. Also note that sodp 15219 * is assumed to be still valid after exit()ing the sod_lockp 15220 * above and while the SOD state can change it can only change 15221 * such that the Q is empty now even though data was added 15222 * above. 15223 */ 15224 if (!IPCL_IS_NONSTR(connp) && 15225 ((sodp != NULL && !SOD_QEMPTY(sodp) && 15226 (sodp->sod_state & SOD_WAKE_NOT)) || 15227 (sodp == NULL && tcp->tcp_rcv_list != NULL)) && 15228 tcp->tcp_push_tid == 0) { 15229 /* 15230 * The connection may be closed at this point, so don't 15231 * do anything for a detached tcp. 15232 */ 15233 if (!TCP_IS_DETACHED(tcp)) 15234 tcp->tcp_push_tid = TCP_TIMER(tcp, 15235 tcp_push_timer, 15236 MSEC_TO_TICK( 15237 tcps->tcps_push_timer_interval)); 15238 } 15239 } 15240 15241 xmit_check: 15242 /* Is there anything left to do? */ 15243 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15244 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15245 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15246 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15247 goto done; 15248 15249 /* Any transmit work to do and a non-zero window? */ 15250 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15251 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15252 if (flags & TH_REXMIT_NEEDED) { 15253 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15254 15255 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15256 if (snd_size > mss) 15257 snd_size = mss; 15258 if (snd_size > tcp->tcp_swnd) 15259 snd_size = tcp->tcp_swnd; 15260 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15261 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15262 B_TRUE); 15263 15264 if (mp1 != NULL) { 15265 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15266 tcp->tcp_csuna = tcp->tcp_snxt; 15267 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15268 UPDATE_MIB(&tcps->tcps_mib, 15269 tcpRetransBytes, snd_size); 15270 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15271 } 15272 } 15273 if (flags & TH_NEED_SACK_REXMIT) { 15274 tcp_sack_rxmit(tcp, &flags); 15275 } 15276 /* 15277 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15278 * out new segment. Note that tcp_rexmit should not be 15279 * set, otherwise TH_LIMIT_XMIT should not be set. 15280 */ 15281 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15282 if (!tcp->tcp_rexmit) { 15283 tcp_wput_data(tcp, NULL, B_FALSE); 15284 } else { 15285 tcp_ss_rexmit(tcp); 15286 } 15287 } 15288 /* 15289 * Adjust tcp_cwnd back to normal value after sending 15290 * new data segments. 15291 */ 15292 if (flags & TH_LIMIT_XMIT) { 15293 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15294 /* 15295 * This will restart the timer. Restarting the 15296 * timer is used to avoid a timeout before the 15297 * limited transmitted segment's ACK gets back. 15298 */ 15299 if (tcp->tcp_xmit_head != NULL) 15300 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15301 } 15302 15303 /* Anything more to do? */ 15304 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15305 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15306 goto done; 15307 } 15308 ack_check: 15309 if (flags & TH_SEND_URP_MARK) { 15310 ASSERT(tcp->tcp_urp_mark_mp); 15311 ASSERT(!IPCL_IS_NONSTR(connp)); 15312 /* 15313 * Send up any queued data and then send the mark message 15314 */ 15315 sodirect_t *sodp; 15316 15317 SOD_PTR_ENTER(tcp, sodp); 15318 15319 mp1 = tcp->tcp_urp_mark_mp; 15320 tcp->tcp_urp_mark_mp = NULL; 15321 if (sodp != NULL) { 15322 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15323 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15324 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15325 } 15326 ASSERT(tcp->tcp_rcv_list == NULL); 15327 15328 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15329 /* sod_wakeup() does the mutex_exit() */ 15330 } else if (tcp->tcp_rcv_list != NULL) { 15331 flags |= tcp_rcv_drain(tcp); 15332 15333 ASSERT(tcp->tcp_rcv_list == NULL || 15334 tcp->tcp_fused_sigurg); 15335 15336 } 15337 putnext(tcp->tcp_rq, mp1); 15338 #ifdef DEBUG 15339 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15340 "tcp_rput: sending zero-length %s %s", 15341 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15342 "MSGNOTMARKNEXT"), 15343 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15344 #endif /* DEBUG */ 15345 flags &= ~TH_SEND_URP_MARK; 15346 } 15347 if (flags & TH_ACK_NEEDED) { 15348 /* 15349 * Time to send an ack for some reason. 15350 */ 15351 mp1 = tcp_ack_mp(tcp); 15352 15353 if (mp1 != NULL) { 15354 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15355 BUMP_LOCAL(tcp->tcp_obsegs); 15356 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15357 } 15358 if (tcp->tcp_ack_tid != 0) { 15359 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15360 tcp->tcp_ack_tid = 0; 15361 } 15362 } 15363 if (flags & TH_ACK_TIMER_NEEDED) { 15364 /* 15365 * Arrange for deferred ACK or push wait timeout. 15366 * Start timer if it is not already running. 15367 */ 15368 if (tcp->tcp_ack_tid == 0) { 15369 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15370 MSEC_TO_TICK(tcp->tcp_localnet ? 15371 (clock_t)tcps->tcps_local_dack_interval : 15372 (clock_t)tcps->tcps_deferred_ack_interval)); 15373 } 15374 } 15375 if (flags & TH_ORDREL_NEEDED) { 15376 /* 15377 * Send up the ordrel_ind unless we are an eager guy. 15378 * In the eager case tcp_rsrv will do this when run 15379 * after tcp_accept is done. 15380 */ 15381 sodirect_t *sodp; 15382 15383 ASSERT(tcp->tcp_listener == NULL); 15384 15385 if (IPCL_IS_NONSTR(connp)) { 15386 ASSERT(tcp->tcp_ordrel_mp == NULL); 15387 tcp->tcp_ordrel_done = B_TRUE; 15388 (*connp->conn_upcalls->su_opctl) 15389 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15390 goto done; 15391 } 15392 15393 SOD_PTR_ENTER(tcp, sodp); 15394 if (sodp != NULL) { 15395 if (sodp->sod_uioa.uioa_state & UIOA_ENABLED) { 15396 sodp->sod_uioa.uioa_state &= UIOA_CLR; 15397 sodp->sod_uioa.uioa_state |= UIOA_FINI; 15398 } 15399 /* No more sodirect */ 15400 tcp->tcp_sodirect = NULL; 15401 if (!SOD_QEMPTY(sodp)) { 15402 /* Mblk(s) to process, notify */ 15403 flags |= tcp_rcv_sod_wakeup(tcp, sodp); 15404 /* sod_wakeup() does the mutex_exit() */ 15405 } else { 15406 /* Nothing to process */ 15407 mutex_exit(sodp->sod_lockp); 15408 } 15409 } else if (tcp->tcp_rcv_list != NULL) { 15410 /* 15411 * Push any mblk(s) enqueued from co processing. 15412 */ 15413 flags |= tcp_rcv_drain(tcp); 15414 15415 ASSERT(tcp->tcp_rcv_list == NULL || 15416 tcp->tcp_fused_sigurg); 15417 } 15418 15419 mp1 = tcp->tcp_ordrel_mp; 15420 tcp->tcp_ordrel_mp = NULL; 15421 tcp->tcp_ordrel_done = B_TRUE; 15422 putnext(tcp->tcp_rq, mp1); 15423 } 15424 done: 15425 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15426 } 15427 15428 /* 15429 * This function does PAWS protection check. Returns B_TRUE if the 15430 * segment passes the PAWS test, else returns B_FALSE. 15431 */ 15432 boolean_t 15433 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15434 { 15435 uint8_t flags; 15436 int options; 15437 uint8_t *up; 15438 15439 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15440 /* 15441 * If timestamp option is aligned nicely, get values inline, 15442 * otherwise call general routine to parse. Only do that 15443 * if timestamp is the only option. 15444 */ 15445 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15446 TCPOPT_REAL_TS_LEN && 15447 OK_32PTR((up = ((uint8_t *)tcph) + 15448 TCP_MIN_HEADER_LENGTH)) && 15449 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15450 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15451 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15452 15453 options = TCP_OPT_TSTAMP_PRESENT; 15454 } else { 15455 if (tcp->tcp_snd_sack_ok) { 15456 tcpoptp->tcp = tcp; 15457 } else { 15458 tcpoptp->tcp = NULL; 15459 } 15460 options = tcp_parse_options(tcph, tcpoptp); 15461 } 15462 15463 if (options & TCP_OPT_TSTAMP_PRESENT) { 15464 /* 15465 * Do PAWS per RFC 1323 section 4.2. Accept RST 15466 * regardless of the timestamp, page 18 RFC 1323.bis. 15467 */ 15468 if ((flags & TH_RST) == 0 && 15469 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15470 tcp->tcp_ts_recent)) { 15471 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15472 PAWS_TIMEOUT)) { 15473 /* This segment is not acceptable. */ 15474 return (B_FALSE); 15475 } else { 15476 /* 15477 * Connection has been idle for 15478 * too long. Reset the timestamp 15479 * and assume the segment is valid. 15480 */ 15481 tcp->tcp_ts_recent = 15482 tcpoptp->tcp_opt_ts_val; 15483 } 15484 } 15485 } else { 15486 /* 15487 * If we don't get a timestamp on every packet, we 15488 * figure we can't really trust 'em, so we stop sending 15489 * and parsing them. 15490 */ 15491 tcp->tcp_snd_ts_ok = B_FALSE; 15492 15493 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15494 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15495 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15496 /* 15497 * Adjust the tcp_mss accordingly. We also need to 15498 * adjust tcp_cwnd here in accordance with the new mss. 15499 * But we avoid doing a slow start here so as to not 15500 * to lose on the transfer rate built up so far. 15501 */ 15502 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15503 if (tcp->tcp_snd_sack_ok) { 15504 ASSERT(tcp->tcp_sack_info != NULL); 15505 tcp->tcp_max_sack_blk = 4; 15506 } 15507 } 15508 return (B_TRUE); 15509 } 15510 15511 /* 15512 * Attach ancillary data to a received TCP segments for the 15513 * ancillary pieces requested by the application that are 15514 * different than they were in the previous data segment. 15515 * 15516 * Save the "current" values once memory allocation is ok so that 15517 * when memory allocation fails we can just wait for the next data segment. 15518 */ 15519 static mblk_t * 15520 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15521 { 15522 struct T_optdata_ind *todi; 15523 int optlen; 15524 uchar_t *optptr; 15525 struct T_opthdr *toh; 15526 uint_t addflag; /* Which pieces to add */ 15527 mblk_t *mp1; 15528 15529 optlen = 0; 15530 addflag = 0; 15531 /* If app asked for pktinfo and the index has changed ... */ 15532 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15533 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15534 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15535 optlen += sizeof (struct T_opthdr) + 15536 sizeof (struct in6_pktinfo); 15537 addflag |= TCP_IPV6_RECVPKTINFO; 15538 } 15539 /* If app asked for hoplimit and it has changed ... */ 15540 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15541 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15542 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15543 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15544 addflag |= TCP_IPV6_RECVHOPLIMIT; 15545 } 15546 /* If app asked for tclass and it has changed ... */ 15547 if ((ipp->ipp_fields & IPPF_TCLASS) && 15548 ipp->ipp_tclass != tcp->tcp_recvtclass && 15549 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15550 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15551 addflag |= TCP_IPV6_RECVTCLASS; 15552 } 15553 /* 15554 * If app asked for hopbyhop headers and it has changed ... 15555 * For security labels, note that (1) security labels can't change on 15556 * a connected socket at all, (2) we're connected to at most one peer, 15557 * (3) if anything changes, then it must be some other extra option. 15558 */ 15559 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15560 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15561 (ipp->ipp_fields & IPPF_HOPOPTS), 15562 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15563 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15564 tcp->tcp_label_len; 15565 addflag |= TCP_IPV6_RECVHOPOPTS; 15566 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15567 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15568 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15569 return (mp); 15570 } 15571 /* If app asked for dst headers before routing headers ... */ 15572 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15573 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15574 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15575 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15576 optlen += sizeof (struct T_opthdr) + 15577 ipp->ipp_rtdstoptslen; 15578 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15579 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15580 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15581 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15582 return (mp); 15583 } 15584 /* If app asked for routing headers and it has changed ... */ 15585 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15586 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15587 (ipp->ipp_fields & IPPF_RTHDR), 15588 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15589 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15590 addflag |= TCP_IPV6_RECVRTHDR; 15591 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15592 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15593 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15594 return (mp); 15595 } 15596 /* If app asked for dest headers and it has changed ... */ 15597 if ((tcp->tcp_ipv6_recvancillary & 15598 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15599 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15600 (ipp->ipp_fields & IPPF_DSTOPTS), 15601 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15602 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15603 addflag |= TCP_IPV6_RECVDSTOPTS; 15604 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15605 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15606 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15607 return (mp); 15608 } 15609 15610 if (optlen == 0) { 15611 /* Nothing to add */ 15612 return (mp); 15613 } 15614 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15615 if (mp1 == NULL) { 15616 /* 15617 * Defer sending ancillary data until the next TCP segment 15618 * arrives. 15619 */ 15620 return (mp); 15621 } 15622 mp1->b_cont = mp; 15623 mp = mp1; 15624 mp->b_wptr += sizeof (*todi) + optlen; 15625 mp->b_datap->db_type = M_PROTO; 15626 todi = (struct T_optdata_ind *)mp->b_rptr; 15627 todi->PRIM_type = T_OPTDATA_IND; 15628 todi->DATA_flag = 1; /* MORE data */ 15629 todi->OPT_length = optlen; 15630 todi->OPT_offset = sizeof (*todi); 15631 optptr = (uchar_t *)&todi[1]; 15632 /* 15633 * If app asked for pktinfo and the index has changed ... 15634 * Note that the local address never changes for the connection. 15635 */ 15636 if (addflag & TCP_IPV6_RECVPKTINFO) { 15637 struct in6_pktinfo *pkti; 15638 15639 toh = (struct T_opthdr *)optptr; 15640 toh->level = IPPROTO_IPV6; 15641 toh->name = IPV6_PKTINFO; 15642 toh->len = sizeof (*toh) + sizeof (*pkti); 15643 toh->status = 0; 15644 optptr += sizeof (*toh); 15645 pkti = (struct in6_pktinfo *)optptr; 15646 if (tcp->tcp_ipversion == IPV6_VERSION) 15647 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15648 else 15649 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15650 &pkti->ipi6_addr); 15651 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15652 optptr += sizeof (*pkti); 15653 ASSERT(OK_32PTR(optptr)); 15654 /* Save as "last" value */ 15655 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15656 } 15657 /* If app asked for hoplimit and it has changed ... */ 15658 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15659 toh = (struct T_opthdr *)optptr; 15660 toh->level = IPPROTO_IPV6; 15661 toh->name = IPV6_HOPLIMIT; 15662 toh->len = sizeof (*toh) + sizeof (uint_t); 15663 toh->status = 0; 15664 optptr += sizeof (*toh); 15665 *(uint_t *)optptr = ipp->ipp_hoplimit; 15666 optptr += sizeof (uint_t); 15667 ASSERT(OK_32PTR(optptr)); 15668 /* Save as "last" value */ 15669 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15670 } 15671 /* If app asked for tclass and it has changed ... */ 15672 if (addflag & TCP_IPV6_RECVTCLASS) { 15673 toh = (struct T_opthdr *)optptr; 15674 toh->level = IPPROTO_IPV6; 15675 toh->name = IPV6_TCLASS; 15676 toh->len = sizeof (*toh) + sizeof (uint_t); 15677 toh->status = 0; 15678 optptr += sizeof (*toh); 15679 *(uint_t *)optptr = ipp->ipp_tclass; 15680 optptr += sizeof (uint_t); 15681 ASSERT(OK_32PTR(optptr)); 15682 /* Save as "last" value */ 15683 tcp->tcp_recvtclass = ipp->ipp_tclass; 15684 } 15685 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15686 toh = (struct T_opthdr *)optptr; 15687 toh->level = IPPROTO_IPV6; 15688 toh->name = IPV6_HOPOPTS; 15689 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15690 tcp->tcp_label_len; 15691 toh->status = 0; 15692 optptr += sizeof (*toh); 15693 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15694 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15695 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15696 ASSERT(OK_32PTR(optptr)); 15697 /* Save as last value */ 15698 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15699 (ipp->ipp_fields & IPPF_HOPOPTS), 15700 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15701 } 15702 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15703 toh = (struct T_opthdr *)optptr; 15704 toh->level = IPPROTO_IPV6; 15705 toh->name = IPV6_RTHDRDSTOPTS; 15706 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15707 toh->status = 0; 15708 optptr += sizeof (*toh); 15709 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15710 optptr += ipp->ipp_rtdstoptslen; 15711 ASSERT(OK_32PTR(optptr)); 15712 /* Save as last value */ 15713 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15714 &tcp->tcp_rtdstoptslen, 15715 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15716 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15717 } 15718 if (addflag & TCP_IPV6_RECVRTHDR) { 15719 toh = (struct T_opthdr *)optptr; 15720 toh->level = IPPROTO_IPV6; 15721 toh->name = IPV6_RTHDR; 15722 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15723 toh->status = 0; 15724 optptr += sizeof (*toh); 15725 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15726 optptr += ipp->ipp_rthdrlen; 15727 ASSERT(OK_32PTR(optptr)); 15728 /* Save as last value */ 15729 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15730 (ipp->ipp_fields & IPPF_RTHDR), 15731 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15732 } 15733 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15734 toh = (struct T_opthdr *)optptr; 15735 toh->level = IPPROTO_IPV6; 15736 toh->name = IPV6_DSTOPTS; 15737 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15738 toh->status = 0; 15739 optptr += sizeof (*toh); 15740 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15741 optptr += ipp->ipp_dstoptslen; 15742 ASSERT(OK_32PTR(optptr)); 15743 /* Save as last value */ 15744 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15745 (ipp->ipp_fields & IPPF_DSTOPTS), 15746 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15747 } 15748 ASSERT(optptr == mp->b_wptr); 15749 return (mp); 15750 } 15751 15752 /* 15753 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15754 * messages. 15755 */ 15756 void 15757 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15758 { 15759 uchar_t *rptr = mp->b_rptr; 15760 queue_t *q = tcp->tcp_rq; 15761 struct T_error_ack *tea; 15762 15763 switch (mp->b_datap->db_type) { 15764 case M_PROTO: 15765 case M_PCPROTO: 15766 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15767 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15768 break; 15769 tea = (struct T_error_ack *)rptr; 15770 ASSERT(tea->PRIM_type != T_BIND_ACK); 15771 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15772 tea->ERROR_prim != T_BIND_REQ); 15773 switch (tea->PRIM_type) { 15774 case T_ERROR_ACK: 15775 if (tcp->tcp_debug) { 15776 (void) strlog(TCP_MOD_ID, 0, 1, 15777 SL_TRACE|SL_ERROR, 15778 "tcp_rput_other: case T_ERROR_ACK, " 15779 "ERROR_prim == %d", 15780 tea->ERROR_prim); 15781 } 15782 switch (tea->ERROR_prim) { 15783 case T_SVR4_OPTMGMT_REQ: 15784 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15785 /* T_OPTMGMT_REQ generated by TCP */ 15786 printf("T_SVR4_OPTMGMT_REQ failed " 15787 "%d/%d - dropped (cnt %d)\n", 15788 tea->TLI_error, tea->UNIX_error, 15789 tcp->tcp_drop_opt_ack_cnt); 15790 freemsg(mp); 15791 tcp->tcp_drop_opt_ack_cnt--; 15792 return; 15793 } 15794 break; 15795 } 15796 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15797 tcp->tcp_drop_opt_ack_cnt > 0) { 15798 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15799 "- dropped (cnt %d)\n", 15800 tea->TLI_error, tea->UNIX_error, 15801 tcp->tcp_drop_opt_ack_cnt); 15802 freemsg(mp); 15803 tcp->tcp_drop_opt_ack_cnt--; 15804 return; 15805 } 15806 break; 15807 case T_OPTMGMT_ACK: 15808 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15809 /* T_OPTMGMT_REQ generated by TCP */ 15810 freemsg(mp); 15811 tcp->tcp_drop_opt_ack_cnt--; 15812 return; 15813 } 15814 break; 15815 default: 15816 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15817 break; 15818 } 15819 break; 15820 case M_FLUSH: 15821 if (*rptr & FLUSHR) 15822 flushq(q, FLUSHDATA); 15823 break; 15824 default: 15825 /* M_CTL will be directly sent to tcp_icmp_error() */ 15826 ASSERT(DB_TYPE(mp) != M_CTL); 15827 break; 15828 } 15829 /* 15830 * Make sure we set this bit before sending the ACK for 15831 * bind. Otherwise accept could possibly run and free 15832 * this tcp struct. 15833 */ 15834 ASSERT(q != NULL); 15835 putnext(q, mp); 15836 } 15837 15838 /* ARGSUSED */ 15839 static void 15840 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15841 { 15842 conn_t *connp = (conn_t *)arg; 15843 tcp_t *tcp = connp->conn_tcp; 15844 queue_t *q = tcp->tcp_rq; 15845 uint_t thwin; 15846 tcp_stack_t *tcps = tcp->tcp_tcps; 15847 sodirect_t *sodp; 15848 boolean_t fc; 15849 15850 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15851 tcp->tcp_rsrv_mp = mp; 15852 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15853 15854 TCP_STAT(tcps, tcp_rsrv_calls); 15855 15856 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15857 return; 15858 } 15859 15860 if (tcp->tcp_fused) { 15861 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15862 15863 ASSERT(tcp->tcp_fused); 15864 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15865 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15866 ASSERT(!TCP_IS_DETACHED(tcp)); 15867 ASSERT(tcp->tcp_connp->conn_sqp == 15868 peer_tcp->tcp_connp->conn_sqp); 15869 15870 /* 15871 * Normally we would not get backenabled in synchronous 15872 * streams mode, but in case this happens, we need to plug 15873 * synchronous streams during our drain to prevent a race 15874 * with tcp_fuse_rrw() or tcp_fuse_rinfop(). 15875 */ 15876 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 15877 if (tcp->tcp_rcv_list != NULL) 15878 (void) tcp_rcv_drain(tcp); 15879 15880 if (peer_tcp > tcp) { 15881 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15882 mutex_enter(&tcp->tcp_non_sq_lock); 15883 } else { 15884 mutex_enter(&tcp->tcp_non_sq_lock); 15885 mutex_enter(&peer_tcp->tcp_non_sq_lock); 15886 } 15887 15888 if (peer_tcp->tcp_flow_stopped && 15889 (TCP_UNSENT_BYTES(peer_tcp) <= 15890 peer_tcp->tcp_xmit_lowater)) { 15891 tcp_clrqfull(peer_tcp); 15892 } 15893 mutex_exit(&peer_tcp->tcp_non_sq_lock); 15894 mutex_exit(&tcp->tcp_non_sq_lock); 15895 15896 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 15897 TCP_STAT(tcps, tcp_fusion_backenabled); 15898 return; 15899 } 15900 15901 SOD_PTR_ENTER(tcp, sodp); 15902 if (sodp != NULL) { 15903 /* An sodirect connection */ 15904 if (SOD_QFULL(sodp)) { 15905 /* Flow-controlled, need another back-enable */ 15906 fc = B_TRUE; 15907 SOD_QSETBE(sodp); 15908 } else { 15909 /* Not flow-controlled */ 15910 fc = B_FALSE; 15911 } 15912 mutex_exit(sodp->sod_lockp); 15913 } else if (canputnext(q)) { 15914 /* STREAMS, not flow-controlled */ 15915 fc = B_FALSE; 15916 } else { 15917 /* STREAMS, flow-controlled */ 15918 fc = B_TRUE; 15919 } 15920 if (!fc) { 15921 /* Not flow-controlled, open rwnd */ 15922 tcp->tcp_rwnd = q->q_hiwat; 15923 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15924 << tcp->tcp_rcv_ws; 15925 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15926 /* 15927 * Send back a window update immediately if TCP is above 15928 * ESTABLISHED state and the increase of the rcv window 15929 * that the other side knows is at least 1 MSS after flow 15930 * control is lifted. 15931 */ 15932 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15933 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15934 tcp_xmit_ctl(NULL, tcp, 15935 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15936 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15937 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 15938 } 15939 } 15940 } 15941 15942 /* 15943 * The read side service routine is called mostly when we get back-enabled as a 15944 * result of flow control relief. Since we don't actually queue anything in 15945 * TCP, we have no data to send out of here. What we do is clear the receive 15946 * window, and send out a window update. 15947 */ 15948 static void 15949 tcp_rsrv(queue_t *q) 15950 { 15951 conn_t *connp = Q_TO_CONN(q); 15952 tcp_t *tcp = connp->conn_tcp; 15953 mblk_t *mp; 15954 tcp_stack_t *tcps = tcp->tcp_tcps; 15955 15956 /* No code does a putq on the read side */ 15957 ASSERT(q->q_first == NULL); 15958 15959 /* Nothing to do for the default queue */ 15960 if (q == tcps->tcps_g_q) { 15961 return; 15962 } 15963 15964 /* 15965 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15966 * been run. So just return. 15967 */ 15968 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15969 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15970 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15971 return; 15972 } 15973 tcp->tcp_rsrv_mp = NULL; 15974 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15975 15976 CONN_INC_REF(connp); 15977 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15978 SQ_PROCESS, SQTAG_TCP_RSRV); 15979 } 15980 15981 /* 15982 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15983 * We do not allow the receive window to shrink. After setting rwnd, 15984 * set the flow control hiwat of the stream. 15985 * 15986 * This function is called in 2 cases: 15987 * 15988 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15989 * connection (passive open) and in tcp_rput_data() for active connect. 15990 * This is called after tcp_mss_set() when the desired MSS value is known. 15991 * This makes sure that our window size is a mutiple of the other side's 15992 * MSS. 15993 * 2) Handling SO_RCVBUF option. 15994 * 15995 * It is ASSUMED that the requested size is a multiple of the current MSS. 15996 * 15997 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15998 * user requests so. 15999 */ 16000 static int 16001 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 16002 { 16003 uint32_t mss = tcp->tcp_mss; 16004 uint32_t old_max_rwnd; 16005 uint32_t max_transmittable_rwnd; 16006 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 16007 tcp_stack_t *tcps = tcp->tcp_tcps; 16008 16009 if (tcp->tcp_fused) { 16010 size_t sth_hiwat; 16011 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 16012 16013 ASSERT(peer_tcp != NULL); 16014 /* 16015 * Record the stream head's high water mark for 16016 * this endpoint; this is used for flow-control 16017 * purposes in tcp_fuse_output(). 16018 */ 16019 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 16020 if (!tcp_detached) { 16021 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16022 sth_hiwat); 16023 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 16024 conn_t *connp = tcp->tcp_connp; 16025 struct sock_proto_props sopp; 16026 16027 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 16028 sopp.sopp_rcvthresh = sth_hiwat >> 3; 16029 16030 (*connp->conn_upcalls->su_set_proto_props) 16031 (connp->conn_upper_handle, &sopp); 16032 } 16033 } 16034 16035 /* 16036 * In the fusion case, the maxpsz stream head value of 16037 * our peer is set according to its send buffer size 16038 * and our receive buffer size; since the latter may 16039 * have changed we need to update the peer's maxpsz. 16040 */ 16041 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 16042 return (rwnd); 16043 } 16044 16045 if (tcp_detached) { 16046 old_max_rwnd = tcp->tcp_rwnd; 16047 } else { 16048 old_max_rwnd = tcp->tcp_recv_hiwater; 16049 } 16050 16051 /* 16052 * Insist on a receive window that is at least 16053 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 16054 * funny TCP interactions of Nagle algorithm, SWS avoidance 16055 * and delayed acknowledgement. 16056 */ 16057 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 16058 16059 /* 16060 * If window size info has already been exchanged, TCP should not 16061 * shrink the window. Shrinking window is doable if done carefully. 16062 * We may add that support later. But so far there is not a real 16063 * need to do that. 16064 */ 16065 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 16066 /* MSS may have changed, do a round up again. */ 16067 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 16068 } 16069 16070 /* 16071 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 16072 * can be applied even before the window scale option is decided. 16073 */ 16074 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 16075 if (rwnd > max_transmittable_rwnd) { 16076 rwnd = max_transmittable_rwnd - 16077 (max_transmittable_rwnd % mss); 16078 if (rwnd < mss) 16079 rwnd = max_transmittable_rwnd; 16080 /* 16081 * If we're over the limit we may have to back down tcp_rwnd. 16082 * The increment below won't work for us. So we set all three 16083 * here and the increment below will have no effect. 16084 */ 16085 tcp->tcp_rwnd = old_max_rwnd = rwnd; 16086 } 16087 if (tcp->tcp_localnet) { 16088 tcp->tcp_rack_abs_max = 16089 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 16090 } else { 16091 /* 16092 * For a remote host on a different subnet (through a router), 16093 * we ack every other packet to be conforming to RFC1122. 16094 * tcp_deferred_acks_max is default to 2. 16095 */ 16096 tcp->tcp_rack_abs_max = 16097 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 16098 } 16099 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 16100 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 16101 else 16102 tcp->tcp_rack_cur_max = 0; 16103 /* 16104 * Increment the current rwnd by the amount the maximum grew (we 16105 * can not overwrite it since we might be in the middle of a 16106 * connection.) 16107 */ 16108 tcp->tcp_rwnd += rwnd - old_max_rwnd; 16109 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 16110 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 16111 tcp->tcp_cwnd_max = rwnd; 16112 16113 if (tcp_detached) 16114 return (rwnd); 16115 /* 16116 * We set the maximum receive window into rq->q_hiwat if it is 16117 * a STREAMS socket. 16118 * This is not actually used for flow control. 16119 */ 16120 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 16121 tcp->tcp_rq->q_hiwat = rwnd; 16122 tcp->tcp_recv_hiwater = rwnd; 16123 /* 16124 * Set the STREAM head high water mark. This doesn't have to be 16125 * here, since we are simply using default values, but we would 16126 * prefer to choose these values algorithmically, with a likely 16127 * relationship to rwnd. 16128 */ 16129 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 16130 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 16131 return (rwnd); 16132 } 16133 16134 /* 16135 * Return SNMP stuff in buffer in mpdata. 16136 */ 16137 mblk_t * 16138 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 16139 { 16140 mblk_t *mpdata; 16141 mblk_t *mp_conn_ctl = NULL; 16142 mblk_t *mp_conn_tail; 16143 mblk_t *mp_attr_ctl = NULL; 16144 mblk_t *mp_attr_tail; 16145 mblk_t *mp6_conn_ctl = NULL; 16146 mblk_t *mp6_conn_tail; 16147 mblk_t *mp6_attr_ctl = NULL; 16148 mblk_t *mp6_attr_tail; 16149 struct opthdr *optp; 16150 mib2_tcpConnEntry_t tce; 16151 mib2_tcp6ConnEntry_t tce6; 16152 mib2_transportMLPEntry_t mlp; 16153 connf_t *connfp; 16154 int i; 16155 boolean_t ispriv; 16156 zoneid_t zoneid; 16157 int v4_conn_idx; 16158 int v6_conn_idx; 16159 conn_t *connp = Q_TO_CONN(q); 16160 tcp_stack_t *tcps; 16161 ip_stack_t *ipst; 16162 mblk_t *mp2ctl; 16163 16164 /* 16165 * make a copy of the original message 16166 */ 16167 mp2ctl = copymsg(mpctl); 16168 16169 if (mpctl == NULL || 16170 (mpdata = mpctl->b_cont) == NULL || 16171 (mp_conn_ctl = copymsg(mpctl)) == NULL || 16172 (mp_attr_ctl = copymsg(mpctl)) == NULL || 16173 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 16174 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 16175 freemsg(mp_conn_ctl); 16176 freemsg(mp_attr_ctl); 16177 freemsg(mp6_conn_ctl); 16178 freemsg(mp6_attr_ctl); 16179 freemsg(mpctl); 16180 freemsg(mp2ctl); 16181 return (NULL); 16182 } 16183 16184 ipst = connp->conn_netstack->netstack_ip; 16185 tcps = connp->conn_netstack->netstack_tcp; 16186 16187 /* build table of connections -- need count in fixed part */ 16188 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 16189 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 16190 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 16191 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 16192 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 16193 16194 ispriv = 16195 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 16196 zoneid = Q_TO_CONN(q)->conn_zoneid; 16197 16198 v4_conn_idx = v6_conn_idx = 0; 16199 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 16200 16201 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16202 ipst = tcps->tcps_netstack->netstack_ip; 16203 16204 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16205 16206 connp = NULL; 16207 16208 while ((connp = 16209 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16210 tcp_t *tcp; 16211 boolean_t needattr; 16212 16213 if (connp->conn_zoneid != zoneid) 16214 continue; /* not in this zone */ 16215 16216 tcp = connp->conn_tcp; 16217 UPDATE_MIB(&tcps->tcps_mib, 16218 tcpHCInSegs, tcp->tcp_ibsegs); 16219 tcp->tcp_ibsegs = 0; 16220 UPDATE_MIB(&tcps->tcps_mib, 16221 tcpHCOutSegs, tcp->tcp_obsegs); 16222 tcp->tcp_obsegs = 0; 16223 16224 tce6.tcp6ConnState = tce.tcpConnState = 16225 tcp_snmp_state(tcp); 16226 if (tce.tcpConnState == MIB2_TCP_established || 16227 tce.tcpConnState == MIB2_TCP_closeWait) 16228 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 16229 16230 needattr = B_FALSE; 16231 bzero(&mlp, sizeof (mlp)); 16232 if (connp->conn_mlp_type != mlptSingle) { 16233 if (connp->conn_mlp_type == mlptShared || 16234 connp->conn_mlp_type == mlptBoth) 16235 mlp.tme_flags |= MIB2_TMEF_SHARED; 16236 if (connp->conn_mlp_type == mlptPrivate || 16237 connp->conn_mlp_type == mlptBoth) 16238 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 16239 needattr = B_TRUE; 16240 } 16241 if (connp->conn_peercred != NULL) { 16242 ts_label_t *tsl; 16243 16244 tsl = crgetlabel(connp->conn_peercred); 16245 mlp.tme_doi = label2doi(tsl); 16246 mlp.tme_label = *label2bslabel(tsl); 16247 needattr = B_TRUE; 16248 } 16249 16250 /* Create a message to report on IPv6 entries */ 16251 if (tcp->tcp_ipversion == IPV6_VERSION) { 16252 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16253 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16254 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16255 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16256 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16257 /* Don't want just anybody seeing these... */ 16258 if (ispriv) { 16259 tce6.tcp6ConnEntryInfo.ce_snxt = 16260 tcp->tcp_snxt; 16261 tce6.tcp6ConnEntryInfo.ce_suna = 16262 tcp->tcp_suna; 16263 tce6.tcp6ConnEntryInfo.ce_rnxt = 16264 tcp->tcp_rnxt; 16265 tce6.tcp6ConnEntryInfo.ce_rack = 16266 tcp->tcp_rack; 16267 } else { 16268 /* 16269 * Netstat, unfortunately, uses this to 16270 * get send/receive queue sizes. How to fix? 16271 * Why not compute the difference only? 16272 */ 16273 tce6.tcp6ConnEntryInfo.ce_snxt = 16274 tcp->tcp_snxt - tcp->tcp_suna; 16275 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16276 tce6.tcp6ConnEntryInfo.ce_rnxt = 16277 tcp->tcp_rnxt - tcp->tcp_rack; 16278 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16279 } 16280 16281 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16282 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16283 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16284 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16285 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16286 16287 tce6.tcp6ConnCreationProcess = 16288 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16289 tcp->tcp_cpid; 16290 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16291 16292 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16293 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16294 16295 mlp.tme_connidx = v6_conn_idx++; 16296 if (needattr) 16297 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16298 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16299 } 16300 /* 16301 * Create an IPv4 table entry for IPv4 entries and also 16302 * for IPv6 entries which are bound to in6addr_any 16303 * but don't have IPV6_V6ONLY set. 16304 * (i.e. anything an IPv4 peer could connect to) 16305 */ 16306 if (tcp->tcp_ipversion == IPV4_VERSION || 16307 (tcp->tcp_state <= TCPS_LISTEN && 16308 !tcp->tcp_connp->conn_ipv6_v6only && 16309 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16310 if (tcp->tcp_ipversion == IPV6_VERSION) { 16311 tce.tcpConnRemAddress = INADDR_ANY; 16312 tce.tcpConnLocalAddress = INADDR_ANY; 16313 } else { 16314 tce.tcpConnRemAddress = 16315 tcp->tcp_remote; 16316 tce.tcpConnLocalAddress = 16317 tcp->tcp_ip_src; 16318 } 16319 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16320 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16321 /* Don't want just anybody seeing these... */ 16322 if (ispriv) { 16323 tce.tcpConnEntryInfo.ce_snxt = 16324 tcp->tcp_snxt; 16325 tce.tcpConnEntryInfo.ce_suna = 16326 tcp->tcp_suna; 16327 tce.tcpConnEntryInfo.ce_rnxt = 16328 tcp->tcp_rnxt; 16329 tce.tcpConnEntryInfo.ce_rack = 16330 tcp->tcp_rack; 16331 } else { 16332 /* 16333 * Netstat, unfortunately, uses this to 16334 * get send/receive queue sizes. How 16335 * to fix? 16336 * Why not compute the difference only? 16337 */ 16338 tce.tcpConnEntryInfo.ce_snxt = 16339 tcp->tcp_snxt - tcp->tcp_suna; 16340 tce.tcpConnEntryInfo.ce_suna = 0; 16341 tce.tcpConnEntryInfo.ce_rnxt = 16342 tcp->tcp_rnxt - tcp->tcp_rack; 16343 tce.tcpConnEntryInfo.ce_rack = 0; 16344 } 16345 16346 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16347 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16348 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16349 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16350 tce.tcpConnEntryInfo.ce_state = 16351 tcp->tcp_state; 16352 16353 tce.tcpConnCreationProcess = 16354 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16355 tcp->tcp_cpid; 16356 tce.tcpConnCreationTime = tcp->tcp_open_time; 16357 16358 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16359 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16360 16361 mlp.tme_connidx = v4_conn_idx++; 16362 if (needattr) 16363 (void) snmp_append_data2( 16364 mp_attr_ctl->b_cont, 16365 &mp_attr_tail, (char *)&mlp, 16366 sizeof (mlp)); 16367 } 16368 } 16369 } 16370 16371 /* fixed length structure for IPv4 and IPv6 counters */ 16372 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16373 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16374 sizeof (mib2_tcp6ConnEntry_t)); 16375 /* synchronize 32- and 64-bit counters */ 16376 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16377 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16378 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16379 optp->level = MIB2_TCP; 16380 optp->name = 0; 16381 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16382 sizeof (tcps->tcps_mib)); 16383 optp->len = msgdsize(mpdata); 16384 qreply(q, mpctl); 16385 16386 /* table of connections... */ 16387 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16388 sizeof (struct T_optmgmt_ack)]; 16389 optp->level = MIB2_TCP; 16390 optp->name = MIB2_TCP_CONN; 16391 optp->len = msgdsize(mp_conn_ctl->b_cont); 16392 qreply(q, mp_conn_ctl); 16393 16394 /* table of MLP attributes... */ 16395 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16396 sizeof (struct T_optmgmt_ack)]; 16397 optp->level = MIB2_TCP; 16398 optp->name = EXPER_XPORT_MLP; 16399 optp->len = msgdsize(mp_attr_ctl->b_cont); 16400 if (optp->len == 0) 16401 freemsg(mp_attr_ctl); 16402 else 16403 qreply(q, mp_attr_ctl); 16404 16405 /* table of IPv6 connections... */ 16406 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16407 sizeof (struct T_optmgmt_ack)]; 16408 optp->level = MIB2_TCP6; 16409 optp->name = MIB2_TCP6_CONN; 16410 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16411 qreply(q, mp6_conn_ctl); 16412 16413 /* table of IPv6 MLP attributes... */ 16414 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16415 sizeof (struct T_optmgmt_ack)]; 16416 optp->level = MIB2_TCP6; 16417 optp->name = EXPER_XPORT_MLP; 16418 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16419 if (optp->len == 0) 16420 freemsg(mp6_attr_ctl); 16421 else 16422 qreply(q, mp6_attr_ctl); 16423 return (mp2ctl); 16424 } 16425 16426 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16427 /* ARGSUSED */ 16428 int 16429 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16430 { 16431 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16432 16433 switch (level) { 16434 case MIB2_TCP: 16435 switch (name) { 16436 case 13: 16437 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16438 return (0); 16439 /* TODO: delete entry defined by tce */ 16440 return (1); 16441 default: 16442 return (0); 16443 } 16444 default: 16445 return (1); 16446 } 16447 } 16448 16449 /* Translate TCP state to MIB2 TCP state. */ 16450 static int 16451 tcp_snmp_state(tcp_t *tcp) 16452 { 16453 if (tcp == NULL) 16454 return (0); 16455 16456 switch (tcp->tcp_state) { 16457 case TCPS_CLOSED: 16458 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16459 case TCPS_BOUND: 16460 return (MIB2_TCP_closed); 16461 case TCPS_LISTEN: 16462 return (MIB2_TCP_listen); 16463 case TCPS_SYN_SENT: 16464 return (MIB2_TCP_synSent); 16465 case TCPS_SYN_RCVD: 16466 return (MIB2_TCP_synReceived); 16467 case TCPS_ESTABLISHED: 16468 return (MIB2_TCP_established); 16469 case TCPS_CLOSE_WAIT: 16470 return (MIB2_TCP_closeWait); 16471 case TCPS_FIN_WAIT_1: 16472 return (MIB2_TCP_finWait1); 16473 case TCPS_CLOSING: 16474 return (MIB2_TCP_closing); 16475 case TCPS_LAST_ACK: 16476 return (MIB2_TCP_lastAck); 16477 case TCPS_FIN_WAIT_2: 16478 return (MIB2_TCP_finWait2); 16479 case TCPS_TIME_WAIT: 16480 return (MIB2_TCP_timeWait); 16481 default: 16482 return (0); 16483 } 16484 } 16485 16486 static char tcp_report_header[] = 16487 "TCP " MI_COL_HDRPAD_STR 16488 "zone dest snxt suna " 16489 "swnd rnxt rack rwnd rto mss w sw rw t " 16490 "recent [lport,fport] state"; 16491 16492 /* 16493 * TCP status report triggered via the Named Dispatch mechanism. 16494 */ 16495 /* ARGSUSED */ 16496 static void 16497 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16498 cred_t *cr) 16499 { 16500 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16501 boolean_t ispriv = secpolicy_ip_config(cr, B_TRUE) == 0; 16502 char cflag; 16503 in6_addr_t v6dst; 16504 char buf[80]; 16505 uint_t print_len, buf_len; 16506 16507 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16508 if (buf_len <= 0) 16509 return; 16510 16511 if (hashval >= 0) 16512 (void) sprintf(hash, "%03d ", hashval); 16513 else 16514 hash[0] = '\0'; 16515 16516 /* 16517 * Note that we use the remote address in the tcp_b structure. 16518 * This means that it will print out the real destination address, 16519 * not the next hop's address if source routing is used. This 16520 * avoid the confusion on the output because user may not 16521 * know that source routing is used for a connection. 16522 */ 16523 if (tcp->tcp_ipversion == IPV4_VERSION) { 16524 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16525 } else { 16526 v6dst = tcp->tcp_remote_v6; 16527 } 16528 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16529 /* 16530 * the ispriv checks are so that normal users cannot determine 16531 * sequence number information using NDD. 16532 */ 16533 16534 if (TCP_IS_DETACHED(tcp)) 16535 cflag = '*'; 16536 else 16537 cflag = ' '; 16538 print_len = snprintf((char *)mp->b_wptr, buf_len, 16539 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16540 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16541 hash, 16542 (void *)tcp, 16543 tcp->tcp_connp->conn_zoneid, 16544 addrbuf, 16545 (ispriv) ? tcp->tcp_snxt : 0, 16546 (ispriv) ? tcp->tcp_suna : 0, 16547 tcp->tcp_swnd, 16548 (ispriv) ? tcp->tcp_rnxt : 0, 16549 (ispriv) ? tcp->tcp_rack : 0, 16550 tcp->tcp_rwnd, 16551 tcp->tcp_rto, 16552 tcp->tcp_mss, 16553 tcp->tcp_snd_ws_ok, 16554 tcp->tcp_snd_ws, 16555 tcp->tcp_rcv_ws, 16556 tcp->tcp_snd_ts_ok, 16557 tcp->tcp_ts_recent, 16558 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16559 if (print_len < buf_len) { 16560 ((mblk_t *)mp)->b_wptr += print_len; 16561 } else { 16562 ((mblk_t *)mp)->b_wptr += buf_len; 16563 } 16564 } 16565 16566 /* 16567 * TCP status report (for listeners only) triggered via the Named Dispatch 16568 * mechanism. 16569 */ 16570 /* ARGSUSED */ 16571 static void 16572 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16573 { 16574 char addrbuf[INET6_ADDRSTRLEN]; 16575 in6_addr_t v6dst; 16576 uint_t print_len, buf_len; 16577 16578 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16579 if (buf_len <= 0) 16580 return; 16581 16582 if (tcp->tcp_ipversion == IPV4_VERSION) { 16583 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16584 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16585 } else { 16586 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16587 addrbuf, sizeof (addrbuf)); 16588 } 16589 print_len = snprintf((char *)mp->b_wptr, buf_len, 16590 "%03d " 16591 MI_COL_PTRFMT_STR 16592 "%d %s %05u %08u %d/%d/%d%c\n", 16593 hashval, (void *)tcp, 16594 tcp->tcp_connp->conn_zoneid, 16595 addrbuf, 16596 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16597 tcp->tcp_conn_req_seqnum, 16598 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16599 tcp->tcp_conn_req_max, 16600 tcp->tcp_syn_defense ? '*' : ' '); 16601 if (print_len < buf_len) { 16602 ((mblk_t *)mp)->b_wptr += print_len; 16603 } else { 16604 ((mblk_t *)mp)->b_wptr += buf_len; 16605 } 16606 } 16607 16608 /* TCP status report triggered via the Named Dispatch mechanism. */ 16609 /* ARGSUSED */ 16610 static int 16611 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16612 { 16613 tcp_t *tcp; 16614 int i; 16615 conn_t *connp; 16616 connf_t *connfp; 16617 zoneid_t zoneid; 16618 tcp_stack_t *tcps; 16619 ip_stack_t *ipst; 16620 16621 zoneid = Q_TO_CONN(q)->conn_zoneid; 16622 tcps = Q_TO_TCP(q)->tcp_tcps; 16623 16624 /* 16625 * Because of the ndd constraint, at most we can have 64K buffer 16626 * to put in all TCP info. So to be more efficient, just 16627 * allocate a 64K buffer here, assuming we need that large buffer. 16628 * This may be a problem as any user can read tcp_status. Therefore 16629 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16630 * This should be OK as normal users should not do this too often. 16631 */ 16632 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16633 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16634 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16635 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16636 return (0); 16637 } 16638 } 16639 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16640 /* The following may work even if we cannot get a large buf. */ 16641 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16642 return (0); 16643 } 16644 16645 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16646 16647 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16648 16649 ipst = tcps->tcps_netstack->netstack_ip; 16650 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 16651 16652 connp = NULL; 16653 16654 while ((connp = 16655 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16656 tcp = connp->conn_tcp; 16657 if (zoneid != GLOBAL_ZONEID && 16658 zoneid != connp->conn_zoneid) 16659 continue; 16660 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16661 cr); 16662 } 16663 16664 } 16665 16666 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16667 return (0); 16668 } 16669 16670 /* TCP status report triggered via the Named Dispatch mechanism. */ 16671 /* ARGSUSED */ 16672 static int 16673 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16674 { 16675 tf_t *tbf; 16676 tcp_t *tcp, *ltcp; 16677 int i; 16678 zoneid_t zoneid; 16679 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 16680 16681 zoneid = Q_TO_CONN(q)->conn_zoneid; 16682 16683 /* Refer to comments in tcp_status_report(). */ 16684 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16685 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16686 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16687 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16688 return (0); 16689 } 16690 } 16691 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16692 /* The following may work even if we cannot get a large buf. */ 16693 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16694 return (0); 16695 } 16696 16697 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16698 16699 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 16700 tbf = &tcps->tcps_bind_fanout[i]; 16701 mutex_enter(&tbf->tf_lock); 16702 for (ltcp = tbf->tf_tcp; ltcp != NULL; 16703 ltcp = ltcp->tcp_bind_hash) { 16704 for (tcp = ltcp; tcp != NULL; 16705 tcp = tcp->tcp_bind_hash_port) { 16706 if (zoneid != GLOBAL_ZONEID && 16707 zoneid != tcp->tcp_connp->conn_zoneid) 16708 continue; 16709 CONN_INC_REF(tcp->tcp_connp); 16710 tcp_report_item(mp->b_cont, tcp, i, 16711 Q_TO_TCP(q), cr); 16712 CONN_DEC_REF(tcp->tcp_connp); 16713 } 16714 } 16715 mutex_exit(&tbf->tf_lock); 16716 } 16717 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16718 return (0); 16719 } 16720 16721 /* TCP status report triggered via the Named Dispatch mechanism. */ 16722 /* ARGSUSED */ 16723 static int 16724 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16725 { 16726 connf_t *connfp; 16727 conn_t *connp; 16728 tcp_t *tcp; 16729 int i; 16730 zoneid_t zoneid; 16731 tcp_stack_t *tcps; 16732 ip_stack_t *ipst; 16733 16734 zoneid = Q_TO_CONN(q)->conn_zoneid; 16735 tcps = Q_TO_TCP(q)->tcp_tcps; 16736 16737 /* Refer to comments in tcp_status_report(). */ 16738 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16739 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16740 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16741 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16742 return (0); 16743 } 16744 } 16745 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16746 /* The following may work even if we cannot get a large buf. */ 16747 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16748 return (0); 16749 } 16750 16751 (void) mi_mpprintf(mp, 16752 " TCP " MI_COL_HDRPAD_STR 16753 "zone IP addr port seqnum backlog (q0/q/max)"); 16754 16755 ipst = tcps->tcps_netstack->netstack_ip; 16756 16757 for (i = 0; i < ipst->ips_ipcl_bind_fanout_size; i++) { 16758 connfp = &ipst->ips_ipcl_bind_fanout[i]; 16759 connp = NULL; 16760 while ((connp = 16761 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16762 tcp = connp->conn_tcp; 16763 if (zoneid != GLOBAL_ZONEID && 16764 zoneid != connp->conn_zoneid) 16765 continue; 16766 tcp_report_listener(mp->b_cont, tcp, i); 16767 } 16768 } 16769 16770 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16771 return (0); 16772 } 16773 16774 /* TCP status report triggered via the Named Dispatch mechanism. */ 16775 /* ARGSUSED */ 16776 static int 16777 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16778 { 16779 connf_t *connfp; 16780 conn_t *connp; 16781 tcp_t *tcp; 16782 int i; 16783 zoneid_t zoneid; 16784 tcp_stack_t *tcps; 16785 ip_stack_t *ipst; 16786 16787 zoneid = Q_TO_CONN(q)->conn_zoneid; 16788 tcps = Q_TO_TCP(q)->tcp_tcps; 16789 ipst = tcps->tcps_netstack->netstack_ip; 16790 16791 /* Refer to comments in tcp_status_report(). */ 16792 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16793 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16794 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16795 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16796 return (0); 16797 } 16798 } 16799 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16800 /* The following may work even if we cannot get a large buf. */ 16801 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16802 return (0); 16803 } 16804 16805 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16806 ipst->ips_ipcl_conn_fanout_size); 16807 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16808 16809 for (i = 0; i < ipst->ips_ipcl_conn_fanout_size; i++) { 16810 connfp = &ipst->ips_ipcl_conn_fanout[i]; 16811 connp = NULL; 16812 while ((connp = 16813 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16814 tcp = connp->conn_tcp; 16815 if (zoneid != GLOBAL_ZONEID && 16816 zoneid != connp->conn_zoneid) 16817 continue; 16818 tcp_report_item(mp->b_cont, tcp, i, 16819 Q_TO_TCP(q), cr); 16820 } 16821 } 16822 16823 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16824 return (0); 16825 } 16826 16827 /* TCP status report triggered via the Named Dispatch mechanism. */ 16828 /* ARGSUSED */ 16829 static int 16830 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16831 { 16832 tf_t *tf; 16833 tcp_t *tcp; 16834 int i; 16835 zoneid_t zoneid; 16836 tcp_stack_t *tcps; 16837 16838 zoneid = Q_TO_CONN(q)->conn_zoneid; 16839 tcps = Q_TO_TCP(q)->tcp_tcps; 16840 16841 /* Refer to comments in tcp_status_report(). */ 16842 if (cr == NULL || secpolicy_ip_config(cr, B_TRUE) != 0) { 16843 if (ddi_get_lbolt() - tcps->tcps_last_ndd_get_info_time < 16844 drv_usectohz(tcps->tcps_ndd_get_info_interval * 1000)) { 16845 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16846 return (0); 16847 } 16848 } 16849 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16850 /* The following may work even if we cannot get a large buf. */ 16851 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16852 return (0); 16853 } 16854 16855 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16856 16857 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 16858 tf = &tcps->tcps_acceptor_fanout[i]; 16859 mutex_enter(&tf->tf_lock); 16860 for (tcp = tf->tf_tcp; tcp != NULL; 16861 tcp = tcp->tcp_acceptor_hash) { 16862 if (zoneid != GLOBAL_ZONEID && 16863 zoneid != tcp->tcp_connp->conn_zoneid) 16864 continue; 16865 tcp_report_item(mp->b_cont, tcp, i, 16866 Q_TO_TCP(q), cr); 16867 } 16868 mutex_exit(&tf->tf_lock); 16869 } 16870 tcps->tcps_last_ndd_get_info_time = ddi_get_lbolt(); 16871 return (0); 16872 } 16873 16874 /* 16875 * tcp_timer is the timer service routine. It handles the retransmission, 16876 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16877 * from the state of the tcp instance what kind of action needs to be done 16878 * at the time it is called. 16879 */ 16880 static void 16881 tcp_timer(void *arg) 16882 { 16883 mblk_t *mp; 16884 clock_t first_threshold; 16885 clock_t second_threshold; 16886 clock_t ms; 16887 uint32_t mss; 16888 conn_t *connp = (conn_t *)arg; 16889 tcp_t *tcp = connp->conn_tcp; 16890 tcp_stack_t *tcps = tcp->tcp_tcps; 16891 16892 tcp->tcp_timer_tid = 0; 16893 16894 if (tcp->tcp_fused) 16895 return; 16896 16897 first_threshold = tcp->tcp_first_timer_threshold; 16898 second_threshold = tcp->tcp_second_timer_threshold; 16899 switch (tcp->tcp_state) { 16900 case TCPS_IDLE: 16901 case TCPS_BOUND: 16902 case TCPS_LISTEN: 16903 return; 16904 case TCPS_SYN_RCVD: { 16905 tcp_t *listener = tcp->tcp_listener; 16906 16907 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16908 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16909 /* it's our first timeout */ 16910 tcp->tcp_syn_rcvd_timeout = 1; 16911 mutex_enter(&listener->tcp_eager_lock); 16912 listener->tcp_syn_rcvd_timeout++; 16913 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16914 /* 16915 * Make this eager available for drop if we 16916 * need to drop one to accomodate a new 16917 * incoming SYN request. 16918 */ 16919 MAKE_DROPPABLE(listener, tcp); 16920 } 16921 if (!listener->tcp_syn_defense && 16922 (listener->tcp_syn_rcvd_timeout > 16923 (tcps->tcps_conn_req_max_q0 >> 2)) && 16924 (tcps->tcps_conn_req_max_q0 > 200)) { 16925 /* We may be under attack. Put on a defense. */ 16926 listener->tcp_syn_defense = B_TRUE; 16927 cmn_err(CE_WARN, "High TCP connect timeout " 16928 "rate! System (port %d) may be under a " 16929 "SYN flood attack!", 16930 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16931 16932 listener->tcp_ip_addr_cache = kmem_zalloc( 16933 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16934 KM_NOSLEEP); 16935 } 16936 mutex_exit(&listener->tcp_eager_lock); 16937 } else if (listener != NULL) { 16938 mutex_enter(&listener->tcp_eager_lock); 16939 tcp->tcp_syn_rcvd_timeout++; 16940 if (tcp->tcp_syn_rcvd_timeout > 1 && 16941 !tcp->tcp_closemp_used) { 16942 /* 16943 * This is our second timeout. Put the tcp in 16944 * the list of droppable eagers to allow it to 16945 * be dropped, if needed. We don't check 16946 * whether tcp_dontdrop is set or not to 16947 * protect ourselve from a SYN attack where a 16948 * remote host can spoof itself as one of the 16949 * good IP source and continue to hold 16950 * resources too long. 16951 */ 16952 MAKE_DROPPABLE(listener, tcp); 16953 } 16954 mutex_exit(&listener->tcp_eager_lock); 16955 } 16956 } 16957 /* FALLTHRU */ 16958 case TCPS_SYN_SENT: 16959 first_threshold = tcp->tcp_first_ctimer_threshold; 16960 second_threshold = tcp->tcp_second_ctimer_threshold; 16961 break; 16962 case TCPS_ESTABLISHED: 16963 case TCPS_FIN_WAIT_1: 16964 case TCPS_CLOSING: 16965 case TCPS_CLOSE_WAIT: 16966 case TCPS_LAST_ACK: 16967 /* If we have data to rexmit */ 16968 if (tcp->tcp_suna != tcp->tcp_snxt) { 16969 clock_t time_to_wait; 16970 16971 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16972 if (!tcp->tcp_xmit_head) 16973 break; 16974 time_to_wait = lbolt - 16975 (clock_t)tcp->tcp_xmit_head->b_prev; 16976 time_to_wait = tcp->tcp_rto - 16977 TICK_TO_MSEC(time_to_wait); 16978 /* 16979 * If the timer fires too early, 1 clock tick earlier, 16980 * restart the timer. 16981 */ 16982 if (time_to_wait > msec_per_tick) { 16983 TCP_STAT(tcps, tcp_timer_fire_early); 16984 TCP_TIMER_RESTART(tcp, time_to_wait); 16985 return; 16986 } 16987 /* 16988 * When we probe zero windows, we force the swnd open. 16989 * If our peer acks with a closed window swnd will be 16990 * set to zero by tcp_rput(). As long as we are 16991 * receiving acks tcp_rput will 16992 * reset 'tcp_ms_we_have_waited' so as not to trip the 16993 * first and second interval actions. NOTE: the timer 16994 * interval is allowed to continue its exponential 16995 * backoff. 16996 */ 16997 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16998 if (tcp->tcp_debug) { 16999 (void) strlog(TCP_MOD_ID, 0, 1, 17000 SL_TRACE, "tcp_timer: zero win"); 17001 } 17002 } else { 17003 /* 17004 * After retransmission, we need to do 17005 * slow start. Set the ssthresh to one 17006 * half of current effective window and 17007 * cwnd to one MSS. Also reset 17008 * tcp_cwnd_cnt. 17009 * 17010 * Note that if tcp_ssthresh is reduced because 17011 * of ECN, do not reduce it again unless it is 17012 * already one window of data away (tcp_cwr 17013 * should then be cleared) or this is a 17014 * timeout for a retransmitted segment. 17015 */ 17016 uint32_t npkt; 17017 17018 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 17019 npkt = ((tcp->tcp_timer_backoff ? 17020 tcp->tcp_cwnd_ssthresh : 17021 tcp->tcp_snxt - 17022 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 17023 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 17024 tcp->tcp_mss; 17025 } 17026 tcp->tcp_cwnd = tcp->tcp_mss; 17027 tcp->tcp_cwnd_cnt = 0; 17028 if (tcp->tcp_ecn_ok) { 17029 tcp->tcp_cwr = B_TRUE; 17030 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 17031 tcp->tcp_ecn_cwr_sent = B_FALSE; 17032 } 17033 } 17034 break; 17035 } 17036 /* 17037 * We have something to send yet we cannot send. The 17038 * reason can be: 17039 * 17040 * 1. Zero send window: we need to do zero window probe. 17041 * 2. Zero cwnd: because of ECN, we need to "clock out 17042 * segments. 17043 * 3. SWS avoidance: receiver may have shrunk window, 17044 * reset our knowledge. 17045 * 17046 * Note that condition 2 can happen with either 1 or 17047 * 3. But 1 and 3 are exclusive. 17048 */ 17049 if (tcp->tcp_unsent != 0) { 17050 if (tcp->tcp_cwnd == 0) { 17051 /* 17052 * Set tcp_cwnd to 1 MSS so that a 17053 * new segment can be sent out. We 17054 * are "clocking out" new data when 17055 * the network is really congested. 17056 */ 17057 ASSERT(tcp->tcp_ecn_ok); 17058 tcp->tcp_cwnd = tcp->tcp_mss; 17059 } 17060 if (tcp->tcp_swnd == 0) { 17061 /* Extend window for zero window probe */ 17062 tcp->tcp_swnd++; 17063 tcp->tcp_zero_win_probe = B_TRUE; 17064 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 17065 } else { 17066 /* 17067 * Handle timeout from sender SWS avoidance. 17068 * Reset our knowledge of the max send window 17069 * since the receiver might have reduced its 17070 * receive buffer. Avoid setting tcp_max_swnd 17071 * to one since that will essentially disable 17072 * the SWS checks. 17073 * 17074 * Note that since we don't have a SWS 17075 * state variable, if the timeout is set 17076 * for ECN but not for SWS, this 17077 * code will also be executed. This is 17078 * fine as tcp_max_swnd is updated 17079 * constantly and it will not affect 17080 * anything. 17081 */ 17082 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 17083 } 17084 tcp_wput_data(tcp, NULL, B_FALSE); 17085 return; 17086 } 17087 /* Is there a FIN that needs to be to re retransmitted? */ 17088 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17089 !tcp->tcp_fin_acked) 17090 break; 17091 /* Nothing to do, return without restarting timer. */ 17092 TCP_STAT(tcps, tcp_timer_fire_miss); 17093 return; 17094 case TCPS_FIN_WAIT_2: 17095 /* 17096 * User closed the TCP endpoint and peer ACK'ed our FIN. 17097 * We waited some time for for peer's FIN, but it hasn't 17098 * arrived. We flush the connection now to avoid 17099 * case where the peer has rebooted. 17100 */ 17101 if (TCP_IS_DETACHED(tcp)) { 17102 (void) tcp_clean_death(tcp, 0, 23); 17103 } else { 17104 TCP_TIMER_RESTART(tcp, 17105 tcps->tcps_fin_wait_2_flush_interval); 17106 } 17107 return; 17108 case TCPS_TIME_WAIT: 17109 (void) tcp_clean_death(tcp, 0, 24); 17110 return; 17111 default: 17112 if (tcp->tcp_debug) { 17113 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 17114 "tcp_timer: strange state (%d) %s", 17115 tcp->tcp_state, tcp_display(tcp, NULL, 17116 DISP_PORT_ONLY)); 17117 } 17118 return; 17119 } 17120 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 17121 /* 17122 * For zero window probe, we need to send indefinitely, 17123 * unless we have not heard from the other side for some 17124 * time... 17125 */ 17126 if ((tcp->tcp_zero_win_probe == 0) || 17127 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 17128 second_threshold)) { 17129 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 17130 /* 17131 * If TCP is in SYN_RCVD state, send back a 17132 * RST|ACK as BSD does. Note that tcp_zero_win_probe 17133 * should be zero in TCPS_SYN_RCVD state. 17134 */ 17135 if (tcp->tcp_state == TCPS_SYN_RCVD) { 17136 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 17137 "in SYN_RCVD", 17138 tcp, tcp->tcp_snxt, 17139 tcp->tcp_rnxt, TH_RST | TH_ACK); 17140 } 17141 (void) tcp_clean_death(tcp, 17142 tcp->tcp_client_errno ? 17143 tcp->tcp_client_errno : ETIMEDOUT, 25); 17144 return; 17145 } else { 17146 /* 17147 * Set tcp_ms_we_have_waited to second_threshold 17148 * so that in next timeout, we will do the above 17149 * check (lbolt - tcp_last_recv_time). This is 17150 * also to avoid overflow. 17151 * 17152 * We don't need to decrement tcp_timer_backoff 17153 * to avoid overflow because it will be decremented 17154 * later if new timeout value is greater than 17155 * tcp_rexmit_interval_max. In the case when 17156 * tcp_rexmit_interval_max is greater than 17157 * second_threshold, it means that we will wait 17158 * longer than second_threshold to send the next 17159 * window probe. 17160 */ 17161 tcp->tcp_ms_we_have_waited = second_threshold; 17162 } 17163 } else if (ms > first_threshold) { 17164 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 17165 tcp->tcp_xmit_head != NULL) { 17166 tcp->tcp_xmit_head = 17167 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 17168 } 17169 /* 17170 * We have been retransmitting for too long... The RTT 17171 * we calculated is probably incorrect. Reinitialize it. 17172 * Need to compensate for 0 tcp_rtt_sa. Reset 17173 * tcp_rtt_update so that we won't accidentally cache a 17174 * bad value. But only do this if this is not a zero 17175 * window probe. 17176 */ 17177 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 17178 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 17179 (tcp->tcp_rtt_sa >> 5); 17180 tcp->tcp_rtt_sa = 0; 17181 tcp_ip_notify(tcp); 17182 tcp->tcp_rtt_update = 0; 17183 } 17184 } 17185 tcp->tcp_timer_backoff++; 17186 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 17187 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 17188 tcps->tcps_rexmit_interval_min) { 17189 /* 17190 * This means the original RTO is tcp_rexmit_interval_min. 17191 * So we will use tcp_rexmit_interval_min as the RTO value 17192 * and do the backoff. 17193 */ 17194 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 17195 } else { 17196 ms <<= tcp->tcp_timer_backoff; 17197 } 17198 if (ms > tcps->tcps_rexmit_interval_max) { 17199 ms = tcps->tcps_rexmit_interval_max; 17200 /* 17201 * ms is at max, decrement tcp_timer_backoff to avoid 17202 * overflow. 17203 */ 17204 tcp->tcp_timer_backoff--; 17205 } 17206 tcp->tcp_ms_we_have_waited += ms; 17207 if (tcp->tcp_zero_win_probe == 0) { 17208 tcp->tcp_rto = ms; 17209 } 17210 TCP_TIMER_RESTART(tcp, ms); 17211 /* 17212 * This is after a timeout and tcp_rto is backed off. Set 17213 * tcp_set_timer to 1 so that next time RTO is updated, we will 17214 * restart the timer with a correct value. 17215 */ 17216 tcp->tcp_set_timer = 1; 17217 mss = tcp->tcp_snxt - tcp->tcp_suna; 17218 if (mss > tcp->tcp_mss) 17219 mss = tcp->tcp_mss; 17220 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 17221 mss = tcp->tcp_swnd; 17222 17223 if ((mp = tcp->tcp_xmit_head) != NULL) 17224 mp->b_prev = (mblk_t *)lbolt; 17225 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 17226 B_TRUE); 17227 17228 /* 17229 * When slow start after retransmission begins, start with 17230 * this seq no. tcp_rexmit_max marks the end of special slow 17231 * start phase. tcp_snd_burst controls how many segments 17232 * can be sent because of an ack. 17233 */ 17234 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 17235 tcp->tcp_snd_burst = TCP_CWND_SS; 17236 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 17237 (tcp->tcp_unsent == 0)) { 17238 tcp->tcp_rexmit_max = tcp->tcp_fss; 17239 } else { 17240 tcp->tcp_rexmit_max = tcp->tcp_snxt; 17241 } 17242 tcp->tcp_rexmit = B_TRUE; 17243 tcp->tcp_dupack_cnt = 0; 17244 17245 /* 17246 * Remove all rexmit SACK blk to start from fresh. 17247 */ 17248 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 17249 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 17250 tcp->tcp_num_notsack_blk = 0; 17251 tcp->tcp_cnt_notsack_list = 0; 17252 } 17253 if (mp == NULL) { 17254 return; 17255 } 17256 /* Attach credentials to retransmitted initial SYNs. */ 17257 if (tcp->tcp_state == TCPS_SYN_SENT) { 17258 mblk_setcred(mp, tcp->tcp_cred); 17259 DB_CPID(mp) = tcp->tcp_cpid; 17260 } 17261 17262 tcp->tcp_csuna = tcp->tcp_snxt; 17263 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 17264 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 17265 tcp_send_data(tcp, tcp->tcp_wq, mp); 17266 17267 } 17268 17269 static int 17270 tcp_do_unbind(conn_t *connp) 17271 { 17272 tcp_t *tcp = connp->conn_tcp; 17273 int error = 0; 17274 17275 switch (tcp->tcp_state) { 17276 case TCPS_BOUND: 17277 case TCPS_LISTEN: 17278 break; 17279 default: 17280 return (-TOUTSTATE); 17281 } 17282 17283 /* 17284 * Need to clean up all the eagers since after the unbind, segments 17285 * will no longer be delivered to this listener stream. 17286 */ 17287 mutex_enter(&tcp->tcp_eager_lock); 17288 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 17289 tcp_eager_cleanup(tcp, 0); 17290 } 17291 mutex_exit(&tcp->tcp_eager_lock); 17292 17293 if (tcp->tcp_ipversion == IPV4_VERSION) { 17294 tcp->tcp_ipha->ipha_src = 0; 17295 } else { 17296 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 17297 } 17298 V6_SET_ZERO(tcp->tcp_ip_src_v6); 17299 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 17300 tcp_bind_hash_remove(tcp); 17301 tcp->tcp_state = TCPS_IDLE; 17302 tcp->tcp_mdt = B_FALSE; 17303 17304 connp = tcp->tcp_connp; 17305 connp->conn_mdt_ok = B_FALSE; 17306 ipcl_hash_remove(connp); 17307 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 17308 17309 return (error); 17310 } 17311 17312 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 17313 static void 17314 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 17315 { 17316 int error = tcp_do_unbind(tcp->tcp_connp); 17317 17318 if (error > 0) { 17319 tcp_err_ack(tcp, mp, TSYSERR, error); 17320 } else if (error < 0) { 17321 tcp_err_ack(tcp, mp, -error, 0); 17322 } else { 17323 /* Send M_FLUSH according to TPI */ 17324 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 17325 17326 mp = mi_tpi_ok_ack_alloc(mp); 17327 putnext(tcp->tcp_rq, mp); 17328 } 17329 } 17330 17331 /* 17332 * Don't let port fall into the privileged range. 17333 * Since the extra privileged ports can be arbitrary we also 17334 * ensure that we exclude those from consideration. 17335 * tcp_g_epriv_ports is not sorted thus we loop over it until 17336 * there are no changes. 17337 * 17338 * Note: No locks are held when inspecting tcp_g_*epriv_ports 17339 * but instead the code relies on: 17340 * - the fact that the address of the array and its size never changes 17341 * - the atomic assignment of the elements of the array 17342 * 17343 * Returns 0 if there are no more ports available. 17344 * 17345 * TS note: skip multilevel ports. 17346 */ 17347 static in_port_t 17348 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 17349 { 17350 int i; 17351 boolean_t restart = B_FALSE; 17352 tcp_stack_t *tcps = tcp->tcp_tcps; 17353 17354 if (random && tcp_random_anon_port != 0) { 17355 (void) random_get_pseudo_bytes((uint8_t *)&port, 17356 sizeof (in_port_t)); 17357 /* 17358 * Unless changed by a sys admin, the smallest anon port 17359 * is 32768 and the largest anon port is 65535. It is 17360 * very likely (50%) for the random port to be smaller 17361 * than the smallest anon port. When that happens, 17362 * add port % (anon port range) to the smallest anon 17363 * port to get the random port. It should fall into the 17364 * valid anon port range. 17365 */ 17366 if (port < tcps->tcps_smallest_anon_port) { 17367 port = tcps->tcps_smallest_anon_port + 17368 port % (tcps->tcps_largest_anon_port - 17369 tcps->tcps_smallest_anon_port); 17370 } 17371 } 17372 17373 retry: 17374 if (port < tcps->tcps_smallest_anon_port) 17375 port = (in_port_t)tcps->tcps_smallest_anon_port; 17376 17377 if (port > tcps->tcps_largest_anon_port) { 17378 if (restart) 17379 return (0); 17380 restart = B_TRUE; 17381 port = (in_port_t)tcps->tcps_smallest_anon_port; 17382 } 17383 17384 if (port < tcps->tcps_smallest_nonpriv_port) 17385 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 17386 17387 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 17388 if (port == tcps->tcps_g_epriv_ports[i]) { 17389 port++; 17390 /* 17391 * Make sure whether the port is in the 17392 * valid range. 17393 */ 17394 goto retry; 17395 } 17396 } 17397 if (is_system_labeled() && 17398 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 17399 IPPROTO_TCP, B_TRUE)) != 0) { 17400 port = i; 17401 goto retry; 17402 } 17403 return (port); 17404 } 17405 17406 /* 17407 * Return the next anonymous port in the privileged port range for 17408 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 17409 * downwards. This is the same behavior as documented in the userland 17410 * library call rresvport(3N). 17411 * 17412 * TS note: skip multilevel ports. 17413 */ 17414 static in_port_t 17415 tcp_get_next_priv_port(const tcp_t *tcp) 17416 { 17417 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 17418 in_port_t nextport; 17419 boolean_t restart = B_FALSE; 17420 tcp_stack_t *tcps = tcp->tcp_tcps; 17421 retry: 17422 if (next_priv_port < tcps->tcps_min_anonpriv_port || 17423 next_priv_port >= IPPORT_RESERVED) { 17424 next_priv_port = IPPORT_RESERVED - 1; 17425 if (restart) 17426 return (0); 17427 restart = B_TRUE; 17428 } 17429 if (is_system_labeled() && 17430 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 17431 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 17432 next_priv_port = nextport; 17433 goto retry; 17434 } 17435 return (next_priv_port--); 17436 } 17437 17438 /* The write side r/w procedure. */ 17439 17440 #if CCS_STATS 17441 struct { 17442 struct { 17443 int64_t count, bytes; 17444 } tot, hit; 17445 } wrw_stats; 17446 #endif 17447 17448 /* 17449 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 17450 * messages. 17451 */ 17452 /* ARGSUSED */ 17453 static void 17454 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 17455 { 17456 conn_t *connp = (conn_t *)arg; 17457 tcp_t *tcp = connp->conn_tcp; 17458 queue_t *q = tcp->tcp_wq; 17459 17460 ASSERT(DB_TYPE(mp) != M_IOCTL); 17461 /* 17462 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 17463 * Once the close starts, streamhead and sockfs will not let any data 17464 * packets come down (close ensures that there are no threads using the 17465 * queue and no new threads will come down) but since qprocsoff() 17466 * hasn't happened yet, a M_FLUSH or some non data message might 17467 * get reflected back (in response to our own FLUSHRW) and get 17468 * processed after tcp_close() is done. The conn would still be valid 17469 * because a ref would have added but we need to check the state 17470 * before actually processing the packet. 17471 */ 17472 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 17473 freemsg(mp); 17474 return; 17475 } 17476 17477 switch (DB_TYPE(mp)) { 17478 case M_IOCDATA: 17479 tcp_wput_iocdata(tcp, mp); 17480 break; 17481 case M_FLUSH: 17482 tcp_wput_flush(tcp, mp); 17483 break; 17484 default: 17485 CALL_IP_WPUT(connp, q, mp); 17486 break; 17487 } 17488 } 17489 17490 /* 17491 * The TCP fast path write put procedure. 17492 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17493 */ 17494 /* ARGSUSED */ 17495 void 17496 tcp_output(void *arg, mblk_t *mp, void *arg2) 17497 { 17498 int len; 17499 int hdrlen; 17500 int plen; 17501 mblk_t *mp1; 17502 uchar_t *rptr; 17503 uint32_t snxt; 17504 tcph_t *tcph; 17505 struct datab *db; 17506 uint32_t suna; 17507 uint32_t mss; 17508 ipaddr_t *dst; 17509 ipaddr_t *src; 17510 uint32_t sum; 17511 int usable; 17512 conn_t *connp = (conn_t *)arg; 17513 tcp_t *tcp = connp->conn_tcp; 17514 uint32_t msize; 17515 tcp_stack_t *tcps = tcp->tcp_tcps; 17516 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 17517 17518 /* 17519 * Try and ASSERT the minimum possible references on the 17520 * conn early enough. Since we are executing on write side, 17521 * the connection is obviously not detached and that means 17522 * there is a ref each for TCP and IP. Since we are behind 17523 * the squeue, the minimum references needed are 3. If the 17524 * conn is in classifier hash list, there should be an 17525 * extra ref for that (we check both the possibilities). 17526 */ 17527 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17528 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17529 17530 ASSERT(DB_TYPE(mp) == M_DATA); 17531 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17532 17533 mutex_enter(&tcp->tcp_non_sq_lock); 17534 tcp->tcp_squeue_bytes -= msize; 17535 mutex_exit(&tcp->tcp_non_sq_lock); 17536 17537 /* Check to see if this connection wants to be re-fused. */ 17538 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 17539 if (tcp->tcp_ipversion == IPV4_VERSION) { 17540 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 17541 &tcp->tcp_saved_tcph); 17542 } else { 17543 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 17544 &tcp->tcp_saved_tcph); 17545 } 17546 } 17547 /* Bypass tcp protocol for fused tcp loopback */ 17548 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17549 return; 17550 17551 mss = tcp->tcp_mss; 17552 if (tcp->tcp_xmit_zc_clean) 17553 mp = tcp_zcopy_backoff(tcp, mp, 0); 17554 17555 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17556 len = (int)(mp->b_wptr - mp->b_rptr); 17557 17558 /* 17559 * Criteria for fast path: 17560 * 17561 * 1. no unsent data 17562 * 2. single mblk in request 17563 * 3. connection established 17564 * 4. data in mblk 17565 * 5. len <= mss 17566 * 6. no tcp_valid bits 17567 */ 17568 if ((tcp->tcp_unsent != 0) || 17569 (tcp->tcp_cork) || 17570 (mp->b_cont != NULL) || 17571 (tcp->tcp_state != TCPS_ESTABLISHED) || 17572 (len == 0) || 17573 (len > mss) || 17574 (tcp->tcp_valid_bits != 0)) { 17575 tcp_wput_data(tcp, mp, B_FALSE); 17576 return; 17577 } 17578 17579 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17580 ASSERT(tcp->tcp_fin_sent == 0); 17581 17582 /* queue new packet onto retransmission queue */ 17583 if (tcp->tcp_xmit_head == NULL) { 17584 tcp->tcp_xmit_head = mp; 17585 } else { 17586 tcp->tcp_xmit_last->b_cont = mp; 17587 } 17588 tcp->tcp_xmit_last = mp; 17589 tcp->tcp_xmit_tail = mp; 17590 17591 /* find out how much we can send */ 17592 /* BEGIN CSTYLED */ 17593 /* 17594 * un-acked usable 17595 * |--------------|-----------------| 17596 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17597 */ 17598 /* END CSTYLED */ 17599 17600 /* start sending from tcp_snxt */ 17601 snxt = tcp->tcp_snxt; 17602 17603 /* 17604 * Check to see if this connection has been idled for some 17605 * time and no ACK is expected. If it is, we need to slow 17606 * start again to get back the connection's "self-clock" as 17607 * described in VJ's paper. 17608 * 17609 * Refer to the comment in tcp_mss_set() for the calculation 17610 * of tcp_cwnd after idle. 17611 */ 17612 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17613 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17614 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 17615 } 17616 17617 usable = tcp->tcp_swnd; /* tcp window size */ 17618 if (usable > tcp->tcp_cwnd) 17619 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17620 usable -= snxt; /* subtract stuff already sent */ 17621 suna = tcp->tcp_suna; 17622 usable += suna; 17623 /* usable can be < 0 if the congestion window is smaller */ 17624 if (len > usable) { 17625 /* Can't send complete M_DATA in one shot */ 17626 goto slow; 17627 } 17628 17629 mutex_enter(&tcp->tcp_non_sq_lock); 17630 if (tcp->tcp_flow_stopped && 17631 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17632 tcp_clrqfull(tcp); 17633 } 17634 mutex_exit(&tcp->tcp_non_sq_lock); 17635 17636 /* 17637 * determine if anything to send (Nagle). 17638 * 17639 * 1. len < tcp_mss (i.e. small) 17640 * 2. unacknowledged data present 17641 * 3. len < nagle limit 17642 * 4. last packet sent < nagle limit (previous packet sent) 17643 */ 17644 if ((len < mss) && (snxt != suna) && 17645 (len < (int)tcp->tcp_naglim) && 17646 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17647 /* 17648 * This was the first unsent packet and normally 17649 * mss < xmit_hiwater so there is no need to worry 17650 * about flow control. The next packet will go 17651 * through the flow control check in tcp_wput_data(). 17652 */ 17653 /* leftover work from above */ 17654 tcp->tcp_unsent = len; 17655 tcp->tcp_xmit_tail_unsent = len; 17656 17657 return; 17658 } 17659 17660 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17661 17662 if (snxt == suna) { 17663 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17664 } 17665 17666 /* we have always sent something */ 17667 tcp->tcp_rack_cnt = 0; 17668 17669 tcp->tcp_snxt = snxt + len; 17670 tcp->tcp_rack = tcp->tcp_rnxt; 17671 17672 if ((mp1 = dupb(mp)) == 0) 17673 goto no_memory; 17674 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17675 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17676 17677 /* adjust tcp header information */ 17678 tcph = tcp->tcp_tcph; 17679 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17680 17681 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17682 sum = (sum >> 16) + (sum & 0xFFFF); 17683 U16_TO_ABE16(sum, tcph->th_sum); 17684 17685 U32_TO_ABE32(snxt, tcph->th_seq); 17686 17687 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17688 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17689 BUMP_LOCAL(tcp->tcp_obsegs); 17690 17691 /* Update the latest receive window size in TCP header. */ 17692 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17693 tcph->th_win); 17694 17695 tcp->tcp_last_sent_len = (ushort_t)len; 17696 17697 plen = len + tcp->tcp_hdr_len; 17698 17699 if (tcp->tcp_ipversion == IPV4_VERSION) { 17700 tcp->tcp_ipha->ipha_length = htons(plen); 17701 } else { 17702 tcp->tcp_ip6h->ip6_plen = htons(plen - 17703 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17704 } 17705 17706 /* see if we need to allocate a mblk for the headers */ 17707 hdrlen = tcp->tcp_hdr_len; 17708 rptr = mp1->b_rptr - hdrlen; 17709 db = mp1->b_datap; 17710 if ((db->db_ref != 2) || rptr < db->db_base || 17711 (!OK_32PTR(rptr))) { 17712 /* NOTE: we assume allocb returns an OK_32PTR */ 17713 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17714 tcps->tcps_wroff_xtra, BPRI_MED); 17715 if (!mp) { 17716 freemsg(mp1); 17717 goto no_memory; 17718 } 17719 mp->b_cont = mp1; 17720 mp1 = mp; 17721 /* Leave room for Link Level header */ 17722 /* hdrlen = tcp->tcp_hdr_len; */ 17723 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17724 mp1->b_wptr = &rptr[hdrlen]; 17725 } 17726 mp1->b_rptr = rptr; 17727 17728 /* Fill in the timestamp option. */ 17729 if (tcp->tcp_snd_ts_ok) { 17730 U32_TO_BE32((uint32_t)lbolt, 17731 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17732 U32_TO_BE32(tcp->tcp_ts_recent, 17733 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17734 } else { 17735 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17736 } 17737 17738 /* copy header into outgoing packet */ 17739 dst = (ipaddr_t *)rptr; 17740 src = (ipaddr_t *)tcp->tcp_iphc; 17741 dst[0] = src[0]; 17742 dst[1] = src[1]; 17743 dst[2] = src[2]; 17744 dst[3] = src[3]; 17745 dst[4] = src[4]; 17746 dst[5] = src[5]; 17747 dst[6] = src[6]; 17748 dst[7] = src[7]; 17749 dst[8] = src[8]; 17750 dst[9] = src[9]; 17751 if (hdrlen -= 40) { 17752 hdrlen >>= 2; 17753 dst += 10; 17754 src += 10; 17755 do { 17756 *dst++ = *src++; 17757 } while (--hdrlen); 17758 } 17759 17760 /* 17761 * Set the ECN info in the TCP header. Note that this 17762 * is not the template header. 17763 */ 17764 if (tcp->tcp_ecn_ok) { 17765 SET_ECT(tcp, rptr); 17766 17767 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17768 if (tcp->tcp_ecn_echo_on) 17769 tcph->th_flags[0] |= TH_ECE; 17770 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17771 tcph->th_flags[0] |= TH_CWR; 17772 tcp->tcp_ecn_cwr_sent = B_TRUE; 17773 } 17774 } 17775 17776 if (tcp->tcp_ip_forward_progress) { 17777 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17778 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17779 tcp->tcp_ip_forward_progress = B_FALSE; 17780 } 17781 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17782 return; 17783 17784 /* 17785 * If we ran out of memory, we pretend to have sent the packet 17786 * and that it was lost on the wire. 17787 */ 17788 no_memory: 17789 return; 17790 17791 slow: 17792 /* leftover work from above */ 17793 tcp->tcp_unsent = len; 17794 tcp->tcp_xmit_tail_unsent = len; 17795 tcp_wput_data(tcp, NULL, B_FALSE); 17796 } 17797 17798 /* ARGSUSED */ 17799 void 17800 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17801 { 17802 conn_t *connp = (conn_t *)arg; 17803 tcp_t *tcp = connp->conn_tcp; 17804 queue_t *q = tcp->tcp_rq; 17805 struct tcp_options *tcpopt; 17806 tcp_stack_t *tcps = tcp->tcp_tcps; 17807 17808 /* socket options */ 17809 uint_t sopp_flags; 17810 ssize_t sopp_rxhiwat; 17811 ssize_t sopp_maxblk; 17812 ushort_t sopp_wroff; 17813 ushort_t sopp_tail; 17814 ushort_t sopp_copyopt; 17815 17816 tcpopt = (struct tcp_options *)mp->b_rptr; 17817 17818 /* 17819 * Drop the eager's ref on the listener, that was placed when 17820 * this eager began life in tcp_conn_request. 17821 */ 17822 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17823 if (IPCL_IS_NONSTR(connp)) { 17824 /* Safe to free conn_ind message */ 17825 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17826 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17827 17828 /* The listener tells us which upper handle to use */ 17829 ASSERT(tcpopt->to_flags & TCPOPT_UPPERHANDLE); 17830 connp->conn_upper_handle = tcpopt->to_handle; 17831 } 17832 17833 tcp->tcp_detached = B_FALSE; 17834 17835 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17836 /* 17837 * Someone blewoff the eager before we could finish 17838 * the accept. 17839 * 17840 * The only reason eager exists it because we put in 17841 * a ref on it when conn ind went up. We need to send 17842 * a disconnect indication up while the last reference 17843 * on the eager will be dropped by the squeue when we 17844 * return. 17845 */ 17846 ASSERT(tcp->tcp_listener == NULL); 17847 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17848 if (IPCL_IS_NONSTR(connp)) { 17849 ASSERT(tcp->tcp_issocket); 17850 (*connp->conn_upcalls->su_disconnected)( 17851 connp->conn_upper_handle, tcp->tcp_connid, 17852 ECONNREFUSED); 17853 freemsg(mp); 17854 } else { 17855 struct T_discon_ind *tdi; 17856 17857 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17858 /* 17859 * Let us reuse the incoming mblk to avoid 17860 * memory allocation failure problems. We know 17861 * that the size of the incoming mblk i.e. 17862 * stroptions is greater than sizeof 17863 * T_discon_ind. So the reallocb below can't 17864 * fail. 17865 */ 17866 freemsg(mp->b_cont); 17867 mp->b_cont = NULL; 17868 ASSERT(DB_REF(mp) == 1); 17869 mp = reallocb(mp, sizeof (struct T_discon_ind), 17870 B_FALSE); 17871 ASSERT(mp != NULL); 17872 DB_TYPE(mp) = M_PROTO; 17873 ((union T_primitives *)mp->b_rptr)->type = 17874 T_DISCON_IND; 17875 tdi = (struct T_discon_ind *)mp->b_rptr; 17876 if (tcp->tcp_issocket) { 17877 tdi->DISCON_reason = ECONNREFUSED; 17878 tdi->SEQ_number = 0; 17879 } else { 17880 tdi->DISCON_reason = ENOPROTOOPT; 17881 tdi->SEQ_number = 17882 tcp->tcp_conn_req_seqnum; 17883 } 17884 mp->b_wptr = mp->b_rptr + 17885 sizeof (struct T_discon_ind); 17886 putnext(q, mp); 17887 return; 17888 } 17889 } 17890 if (tcp->tcp_hard_binding) { 17891 tcp->tcp_hard_binding = B_FALSE; 17892 tcp->tcp_hard_bound = B_TRUE; 17893 } 17894 return; 17895 } 17896 17897 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17898 int boundif = tcpopt->to_boundif; 17899 uint_t len = sizeof (int); 17900 17901 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17902 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17903 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17904 } 17905 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17906 uint_t on = 1; 17907 uint_t len = sizeof (uint_t); 17908 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17909 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17910 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17911 } 17912 17913 /* 17914 * For a loopback connection with tcp_direct_sockfs on, note that 17915 * we don't have to protect tcp_rcv_list yet because synchronous 17916 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17917 * possibly race with us. 17918 */ 17919 17920 /* 17921 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17922 * properly. This is the first time we know of the acceptor' 17923 * queue. So we do it here. 17924 * 17925 * XXX 17926 */ 17927 if (tcp->tcp_rcv_list == NULL) { 17928 /* 17929 * Recv queue is empty, tcp_rwnd should not have changed. 17930 * That means it should be equal to the listener's tcp_rwnd. 17931 */ 17932 if (!IPCL_IS_NONSTR(connp)) 17933 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17934 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17935 } else { 17936 #ifdef DEBUG 17937 mblk_t *tmp; 17938 mblk_t *mp1; 17939 uint_t cnt = 0; 17940 17941 mp1 = tcp->tcp_rcv_list; 17942 while ((tmp = mp1) != NULL) { 17943 mp1 = tmp->b_next; 17944 cnt += msgdsize(tmp); 17945 } 17946 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17947 #endif 17948 /* There is some data, add them back to get the max. */ 17949 if (!IPCL_IS_NONSTR(connp)) 17950 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17951 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17952 } 17953 /* 17954 * This is the first time we run on the correct 17955 * queue after tcp_accept. So fix all the q parameters 17956 * here. 17957 */ 17958 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17959 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17960 17961 /* 17962 * Record the stream head's high water mark for this endpoint; 17963 * this is used for flow-control purposes. 17964 */ 17965 sopp_rxhiwat = tcp->tcp_fused ? 17966 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17967 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17968 17969 /* 17970 * Determine what write offset value to use depending on SACK and 17971 * whether the endpoint is fused or not. 17972 */ 17973 if (tcp->tcp_fused) { 17974 ASSERT(tcp->tcp_loopback); 17975 ASSERT(tcp->tcp_loopback_peer != NULL); 17976 /* 17977 * For fused tcp loopback, set the stream head's write 17978 * offset value to zero since we won't be needing any room 17979 * for TCP/IP headers. This would also improve performance 17980 * since it would reduce the amount of work done by kmem. 17981 * Non-fused tcp loopback case is handled separately below. 17982 */ 17983 sopp_wroff = 0; 17984 /* 17985 * Update the peer's transmit parameters according to 17986 * our recently calculated high water mark value. 17987 */ 17988 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17989 } else if (tcp->tcp_snd_sack_ok) { 17990 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17991 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17992 } else { 17993 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17994 tcps->tcps_wroff_xtra); 17995 } 17996 17997 /* 17998 * If this is endpoint is handling SSL, then reserve extra 17999 * offset and space at the end. 18000 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 18001 * overriding the previous setting. The extra cost of signing and 18002 * encrypting multiple MSS-size records (12 of them with Ethernet), 18003 * instead of a single contiguous one by the stream head 18004 * largely outweighs the statistical reduction of ACKs, when 18005 * applicable. The peer will also save on decryption and verification 18006 * costs. 18007 */ 18008 if (tcp->tcp_kssl_ctx != NULL) { 18009 sopp_wroff += SSL3_WROFFSET; 18010 18011 sopp_flags |= SOCKOPT_TAIL; 18012 sopp_tail = SSL3_MAX_TAIL_LEN; 18013 18014 sopp_flags |= SOCKOPT_ZCOPY; 18015 sopp_copyopt = ZCVMUNSAFE; 18016 18017 sopp_maxblk = SSL3_MAX_RECORD_LEN; 18018 } 18019 18020 /* Send the options up */ 18021 if (IPCL_IS_NONSTR(connp)) { 18022 struct sock_proto_props sopp; 18023 18024 sopp.sopp_flags = sopp_flags; 18025 sopp.sopp_wroff = sopp_wroff; 18026 sopp.sopp_maxblk = sopp_maxblk; 18027 sopp.sopp_rxhiwat = sopp_rxhiwat; 18028 if (sopp_flags & SOCKOPT_TAIL) { 18029 ASSERT(tcp->tcp_kssl_ctx != NULL); 18030 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 18031 sopp.sopp_tail = sopp_tail; 18032 sopp.sopp_zcopyflag = sopp_copyopt; 18033 } 18034 (*connp->conn_upcalls->su_set_proto_props) 18035 (connp->conn_upper_handle, &sopp); 18036 } else { 18037 struct stroptions *stropt; 18038 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 18039 if (stropt_mp == NULL) { 18040 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 18041 return; 18042 } 18043 DB_TYPE(stropt_mp) = M_SETOPTS; 18044 stropt = (struct stroptions *)stropt_mp->b_rptr; 18045 stropt_mp->b_wptr += sizeof (struct stroptions); 18046 stropt = (struct stroptions *)stropt_mp->b_rptr; 18047 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 18048 stropt->so_hiwat = sopp_rxhiwat; 18049 stropt->so_wroff = sopp_wroff; 18050 stropt->so_maxblk = sopp_maxblk; 18051 18052 if (sopp_flags & SOCKOPT_TAIL) { 18053 ASSERT(tcp->tcp_kssl_ctx != NULL); 18054 18055 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 18056 stropt->so_tail = sopp_tail; 18057 stropt->so_copyopt = sopp_copyopt; 18058 } 18059 18060 /* Send the options up */ 18061 putnext(q, stropt_mp); 18062 } 18063 18064 freemsg(mp); 18065 /* 18066 * Pass up any data and/or a fin that has been received. 18067 * 18068 * Adjust receive window in case it had decreased 18069 * (because there is data <=> tcp_rcv_list != NULL) 18070 * while the connection was detached. Note that 18071 * in case the eager was flow-controlled, w/o this 18072 * code, the rwnd may never open up again! 18073 */ 18074 if (tcp->tcp_rcv_list != NULL) { 18075 if (IPCL_IS_NONSTR(connp)) { 18076 mblk_t *mp; 18077 int space_left; 18078 int error; 18079 boolean_t push = B_TRUE; 18080 18081 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 18082 (connp->conn_upper_handle, NULL, 0, 0, &error, 18083 &push) >= 0) { 18084 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 18085 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18086 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18087 tcp_xmit_ctl(NULL, 18088 tcp, (tcp->tcp_swnd == 0) ? 18089 tcp->tcp_suna : tcp->tcp_snxt, 18090 tcp->tcp_rnxt, TH_ACK); 18091 } 18092 } 18093 while ((mp = tcp->tcp_rcv_list) != NULL) { 18094 push = B_TRUE; 18095 tcp->tcp_rcv_list = mp->b_next; 18096 mp->b_next = NULL; 18097 space_left = (*connp->conn_upcalls->su_recv) 18098 (connp->conn_upper_handle, mp, msgdsize(mp), 18099 0, &error, &push); 18100 if (space_left < 0) { 18101 /* 18102 * At this point the eager is not 18103 * visible to anyone, so fallback 18104 * can not happen. 18105 */ 18106 ASSERT(error != EOPNOTSUPP); 18107 } 18108 } 18109 tcp->tcp_rcv_last_head = NULL; 18110 tcp->tcp_rcv_last_tail = NULL; 18111 tcp->tcp_rcv_cnt = 0; 18112 } else { 18113 /* We drain directly in case of fused tcp loopback */ 18114 sodirect_t *sodp; 18115 18116 if (!tcp->tcp_fused && canputnext(q)) { 18117 tcp->tcp_rwnd = q->q_hiwat; 18118 if (tcp->tcp_state >= TCPS_ESTABLISHED && 18119 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 18120 tcp_xmit_ctl(NULL, 18121 tcp, (tcp->tcp_swnd == 0) ? 18122 tcp->tcp_suna : tcp->tcp_snxt, 18123 tcp->tcp_rnxt, TH_ACK); 18124 } 18125 } 18126 18127 SOD_PTR_ENTER(tcp, sodp); 18128 if (sodp != NULL) { 18129 /* Sodirect, move from rcv_list */ 18130 ASSERT(!tcp->tcp_fused); 18131 while ((mp = tcp->tcp_rcv_list) != NULL) { 18132 tcp->tcp_rcv_list = mp->b_next; 18133 mp->b_next = NULL; 18134 (void) tcp_rcv_sod_enqueue(tcp, sodp, 18135 mp, msgdsize(mp)); 18136 } 18137 tcp->tcp_rcv_last_head = NULL; 18138 tcp->tcp_rcv_last_tail = NULL; 18139 tcp->tcp_rcv_cnt = 0; 18140 (void) tcp_rcv_sod_wakeup(tcp, sodp); 18141 /* sod_wakeup() did the mutex_exit() */ 18142 } else { 18143 /* Not sodirect, drain */ 18144 (void) tcp_rcv_drain(tcp); 18145 } 18146 } 18147 18148 /* 18149 * For fused tcp loopback, back-enable peer endpoint 18150 * if it's currently flow-controlled. 18151 */ 18152 if (tcp->tcp_fused) { 18153 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 18154 18155 ASSERT(peer_tcp != NULL); 18156 ASSERT(peer_tcp->tcp_fused); 18157 /* 18158 * In order to change the peer's tcp_flow_stopped, 18159 * we need to take locks for both end points. The 18160 * highest address is taken first. 18161 */ 18162 if (peer_tcp > tcp) { 18163 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18164 mutex_enter(&tcp->tcp_non_sq_lock); 18165 } else { 18166 mutex_enter(&tcp->tcp_non_sq_lock); 18167 mutex_enter(&peer_tcp->tcp_non_sq_lock); 18168 } 18169 if (peer_tcp->tcp_flow_stopped) { 18170 tcp_clrqfull(peer_tcp); 18171 TCP_STAT(tcps, tcp_fusion_backenabled); 18172 } 18173 mutex_exit(&peer_tcp->tcp_non_sq_lock); 18174 mutex_exit(&tcp->tcp_non_sq_lock); 18175 } 18176 } 18177 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 18178 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 18179 tcp->tcp_ordrel_done = B_TRUE; 18180 if (IPCL_IS_NONSTR(connp)) { 18181 ASSERT(tcp->tcp_ordrel_mp == NULL); 18182 (*connp->conn_upcalls->su_opctl)( 18183 connp->conn_upper_handle, 18184 SOCK_OPCTL_SHUT_RECV, 0); 18185 } else { 18186 mp = tcp->tcp_ordrel_mp; 18187 tcp->tcp_ordrel_mp = NULL; 18188 putnext(q, mp); 18189 } 18190 } 18191 if (tcp->tcp_hard_binding) { 18192 tcp->tcp_hard_binding = B_FALSE; 18193 tcp->tcp_hard_bound = B_TRUE; 18194 } 18195 18196 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 18197 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 18198 tcp->tcp_loopback_peer != NULL && 18199 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 18200 tcp_fuse_syncstr_enable_pair(tcp); 18201 } 18202 18203 if (tcp->tcp_ka_enabled) { 18204 tcp->tcp_ka_last_intrvl = 0; 18205 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 18206 MSEC_TO_TICK(tcp->tcp_ka_interval)); 18207 } 18208 18209 /* 18210 * At this point, eager is fully established and will 18211 * have the following references - 18212 * 18213 * 2 references for connection to exist (1 for TCP and 1 for IP). 18214 * 1 reference for the squeue which will be dropped by the squeue as 18215 * soon as this function returns. 18216 * There will be 1 additonal reference for being in classifier 18217 * hash list provided something bad hasn't happened. 18218 */ 18219 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 18220 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 18221 } 18222 18223 /* 18224 * The function called through squeue to get behind listener's perimeter to 18225 * send a deffered conn_ind. 18226 */ 18227 /* ARGSUSED */ 18228 void 18229 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 18230 { 18231 conn_t *connp = (conn_t *)arg; 18232 tcp_t *listener = connp->conn_tcp; 18233 struct T_conn_ind *conn_ind; 18234 tcp_t *tcp; 18235 18236 if (listener->tcp_state == TCPS_CLOSED || 18237 TCP_IS_DETACHED(listener)) { 18238 /* 18239 * If listener has closed, it would have caused a 18240 * a cleanup/blowoff to happen for the eager. 18241 */ 18242 18243 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18244 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18245 conn_ind->OPT_length); 18246 /* 18247 * We need to drop the ref on eager that was put 18248 * tcp_rput_data() before trying to send the conn_ind 18249 * to listener. The conn_ind was deferred in tcp_send_conn_ind 18250 * and tcp_wput_accept() is sending this deferred conn_ind but 18251 * listener is closed so we drop the ref. 18252 */ 18253 CONN_DEC_REF(tcp->tcp_connp); 18254 freemsg(mp); 18255 return; 18256 } 18257 if (IPCL_IS_NONSTR(connp)) { 18258 conn_ind = (struct T_conn_ind *)mp->b_rptr; 18259 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 18260 conn_ind->OPT_length); 18261 18262 if ((*connp->conn_upcalls->su_newconn) 18263 (connp->conn_upper_handle, 18264 (sock_lower_handle_t)tcp->tcp_connp, 18265 &sock_tcp_downcalls, DB_CRED(mp), DB_CPID(mp), 18266 &tcp->tcp_connp->conn_upcalls) != NULL) { 18267 /* Keep the message around in case of fallback */ 18268 tcp->tcp_conn.tcp_eager_conn_ind = mp; 18269 } else { 18270 freemsg(mp); 18271 } 18272 } else { 18273 putnext(listener->tcp_rq, mp); 18274 } 18275 } 18276 18277 /* ARGSUSED */ 18278 static int 18279 tcp_accept_common(conn_t *lconnp, conn_t *econnp, 18280 sock_upper_handle_t sock_handle, cred_t *cr) 18281 { 18282 tcp_t *listener, *eager; 18283 mblk_t *opt_mp; 18284 struct tcp_options *tcpopt; 18285 18286 listener = lconnp->conn_tcp; 18287 ASSERT(listener->tcp_state == TCPS_LISTEN); 18288 eager = econnp->conn_tcp; 18289 ASSERT(eager->tcp_listener != NULL); 18290 18291 ASSERT(eager->tcp_rq != NULL); 18292 18293 /* If tcp_fused and sodirect enabled disable it */ 18294 if (eager->tcp_fused && eager->tcp_sodirect != NULL) { 18295 /* Fused, disable sodirect */ 18296 mutex_enter(eager->tcp_sodirect->sod_lockp); 18297 SOD_DISABLE(eager->tcp_sodirect); 18298 mutex_exit(eager->tcp_sodirect->sod_lockp); 18299 eager->tcp_sodirect = NULL; 18300 } 18301 18302 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 18303 if (opt_mp == NULL) { 18304 return (-TPROTO); 18305 } 18306 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 18307 eager->tcp_issocket = B_TRUE; 18308 18309 econnp->conn_upcalls = lconnp->conn_upcalls; 18310 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 18311 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 18312 ASSERT(econnp->conn_netstack == 18313 listener->tcp_connp->conn_netstack); 18314 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 18315 18316 /* Put the ref for IP */ 18317 CONN_INC_REF(econnp); 18318 18319 /* 18320 * We should have minimum of 3 references on the conn 18321 * at this point. One each for TCP and IP and one for 18322 * the T_conn_ind that was sent up when the 3-way handshake 18323 * completed. In the normal case we would also have another 18324 * reference (making a total of 4) for the conn being in the 18325 * classifier hash list. However the eager could have received 18326 * an RST subsequently and tcp_closei_local could have removed 18327 * the eager from the classifier hash list, hence we can't 18328 * assert that reference. 18329 */ 18330 ASSERT(econnp->conn_ref >= 3); 18331 18332 opt_mp->b_datap->db_type = M_SETOPTS; 18333 opt_mp->b_wptr += sizeof (struct tcp_options); 18334 18335 /* 18336 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 18337 * from listener to acceptor. In case of non-STREAMS sockets, 18338 * we also need to pass the upper handle along. 18339 */ 18340 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 18341 tcpopt->to_flags = 0; 18342 18343 if (IPCL_IS_NONSTR(econnp)) { 18344 ASSERT(sock_handle != NULL); 18345 tcpopt->to_flags |= TCPOPT_UPPERHANDLE; 18346 tcpopt->to_handle = sock_handle; 18347 } 18348 if (listener->tcp_bound_if != 0) { 18349 tcpopt->to_flags |= TCPOPT_BOUNDIF; 18350 tcpopt->to_boundif = listener->tcp_bound_if; 18351 } 18352 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 18353 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 18354 } 18355 18356 mutex_enter(&listener->tcp_eager_lock); 18357 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 18358 18359 tcp_t *tail; 18360 tcp_t *tcp; 18361 mblk_t *mp1; 18362 18363 tcp = listener->tcp_eager_prev_q0; 18364 /* 18365 * listener->tcp_eager_prev_q0 points to the TAIL of the 18366 * deferred T_conn_ind queue. We need to get to the head 18367 * of the queue in order to send up T_conn_ind the same 18368 * order as how the 3WHS is completed. 18369 */ 18370 while (tcp != listener) { 18371 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 18372 !tcp->tcp_kssl_pending) 18373 break; 18374 else 18375 tcp = tcp->tcp_eager_prev_q0; 18376 } 18377 /* None of the pending eagers can be sent up now */ 18378 if (tcp == listener) 18379 goto no_more_eagers; 18380 18381 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 18382 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 18383 /* Move from q0 to q */ 18384 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 18385 listener->tcp_conn_req_cnt_q0--; 18386 listener->tcp_conn_req_cnt_q++; 18387 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 18388 tcp->tcp_eager_prev_q0; 18389 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 18390 tcp->tcp_eager_next_q0; 18391 tcp->tcp_eager_prev_q0 = NULL; 18392 tcp->tcp_eager_next_q0 = NULL; 18393 tcp->tcp_conn_def_q0 = B_FALSE; 18394 18395 /* Make sure the tcp isn't in the list of droppables */ 18396 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 18397 tcp->tcp_eager_prev_drop_q0 == NULL); 18398 18399 /* 18400 * Insert at end of the queue because sockfs sends 18401 * down T_CONN_RES in chronological order. Leaving 18402 * the older conn indications at front of the queue 18403 * helps reducing search time. 18404 */ 18405 tail = listener->tcp_eager_last_q; 18406 if (tail != NULL) { 18407 tail->tcp_eager_next_q = tcp; 18408 } else { 18409 listener->tcp_eager_next_q = tcp; 18410 } 18411 listener->tcp_eager_last_q = tcp; 18412 tcp->tcp_eager_next_q = NULL; 18413 18414 /* Need to get inside the listener perimeter */ 18415 CONN_INC_REF(listener->tcp_connp); 18416 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 18417 tcp_send_pending, listener->tcp_connp, SQ_FILL, 18418 SQTAG_TCP_SEND_PENDING); 18419 } 18420 no_more_eagers: 18421 tcp_eager_unlink(eager); 18422 mutex_exit(&listener->tcp_eager_lock); 18423 18424 /* 18425 * At this point, the eager is detached from the listener 18426 * but we still have an extra refs on eager (apart from the 18427 * usual tcp references). The ref was placed in tcp_rput_data 18428 * before sending the conn_ind in tcp_send_conn_ind. 18429 * The ref will be dropped in tcp_accept_finish(). 18430 */ 18431 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 18432 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 18433 return (0); 18434 } 18435 18436 int 18437 tcp_accept(sock_lower_handle_t lproto_handle, 18438 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 18439 cred_t *cr) 18440 { 18441 conn_t *lconnp, *econnp; 18442 tcp_t *listener, *eager; 18443 tcp_stack_t *tcps; 18444 18445 lconnp = (conn_t *)lproto_handle; 18446 listener = lconnp->conn_tcp; 18447 ASSERT(listener->tcp_state == TCPS_LISTEN); 18448 econnp = (conn_t *)eproto_handle; 18449 eager = econnp->conn_tcp; 18450 ASSERT(eager->tcp_listener != NULL); 18451 tcps = eager->tcp_tcps; 18452 18453 ASSERT(IPCL_IS_NONSTR(econnp)); 18454 /* 18455 * Create helper stream if it is a non-TPI TCP connection. 18456 */ 18457 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 18458 ip1dbg(("tcp_accept: create of IP helper stream" 18459 " failed\n")); 18460 return (EPROTO); 18461 } 18462 eager->tcp_rq = econnp->conn_rq; 18463 eager->tcp_wq = econnp->conn_wq; 18464 18465 ASSERT(eager->tcp_rq != NULL); 18466 18467 eager->tcp_sodirect = SOD_SOTOSODP(sock_handle); 18468 return (tcp_accept_common(lconnp, econnp, sock_handle, cr)); 18469 } 18470 18471 18472 /* 18473 * This is the STREAMS entry point for T_CONN_RES coming down on 18474 * Acceptor STREAM when sockfs listener does accept processing. 18475 * Read the block comment on top of tcp_conn_request(). 18476 */ 18477 void 18478 tcp_tpi_accept(queue_t *q, mblk_t *mp) 18479 { 18480 queue_t *rq = RD(q); 18481 struct T_conn_res *conn_res; 18482 tcp_t *eager; 18483 tcp_t *listener; 18484 struct T_ok_ack *ok; 18485 t_scalar_t PRIM_type; 18486 conn_t *econnp; 18487 18488 ASSERT(DB_TYPE(mp) == M_PROTO); 18489 18490 conn_res = (struct T_conn_res *)mp->b_rptr; 18491 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 18492 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 18493 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18494 if (mp != NULL) 18495 putnext(rq, mp); 18496 return; 18497 } 18498 switch (conn_res->PRIM_type) { 18499 case O_T_CONN_RES: 18500 case T_CONN_RES: 18501 /* 18502 * We pass up an err ack if allocb fails. This will 18503 * cause sockfs to issue a T_DISCON_REQ which will cause 18504 * tcp_eager_blowoff to be called. sockfs will then call 18505 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 18506 * we need to do the allocb up here because we have to 18507 * make sure rq->q_qinfo->qi_qclose still points to the 18508 * correct function (tcpclose_accept) in case allocb 18509 * fails. 18510 */ 18511 bcopy(mp->b_rptr + conn_res->OPT_offset, 18512 &eager, conn_res->OPT_length); 18513 PRIM_type = conn_res->PRIM_type; 18514 mp->b_datap->db_type = M_PCPROTO; 18515 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 18516 ok = (struct T_ok_ack *)mp->b_rptr; 18517 ok->PRIM_type = T_OK_ACK; 18518 ok->CORRECT_prim = PRIM_type; 18519 econnp = eager->tcp_connp; 18520 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 18521 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 18522 eager->tcp_rq = rq; 18523 eager->tcp_wq = q; 18524 rq->q_ptr = econnp; 18525 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 18526 q->q_ptr = econnp; 18527 q->q_qinfo = &tcp_winit; 18528 listener = eager->tcp_listener; 18529 18530 /* 18531 * TCP is _D_SODIRECT and sockfs is directly above so 18532 * save shared sodirect_t pointer (if any). 18533 */ 18534 eager->tcp_sodirect = SOD_QTOSODP(eager->tcp_rq); 18535 if (tcp_accept_common(listener->tcp_connp, 18536 econnp, NULL, CRED()) < 0) { 18537 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 18538 if (mp != NULL) 18539 putnext(rq, mp); 18540 return; 18541 } 18542 18543 /* 18544 * Send the new local address also up to sockfs. There 18545 * should already be enough space in the mp that came 18546 * down from soaccept(). 18547 */ 18548 if (eager->tcp_family == AF_INET) { 18549 sin_t *sin; 18550 18551 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18552 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 18553 sin = (sin_t *)mp->b_wptr; 18554 mp->b_wptr += sizeof (sin_t); 18555 sin->sin_family = AF_INET; 18556 sin->sin_port = eager->tcp_lport; 18557 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 18558 } else { 18559 sin6_t *sin6; 18560 18561 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 18562 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 18563 sin6 = (sin6_t *)mp->b_wptr; 18564 mp->b_wptr += sizeof (sin6_t); 18565 sin6->sin6_family = AF_INET6; 18566 sin6->sin6_port = eager->tcp_lport; 18567 if (eager->tcp_ipversion == IPV4_VERSION) { 18568 sin6->sin6_flowinfo = 0; 18569 IN6_IPADDR_TO_V4MAPPED( 18570 eager->tcp_ipha->ipha_src, 18571 &sin6->sin6_addr); 18572 } else { 18573 ASSERT(eager->tcp_ip6h != NULL); 18574 sin6->sin6_flowinfo = 18575 eager->tcp_ip6h->ip6_vcf & 18576 ~IPV6_VERS_AND_FLOW_MASK; 18577 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 18578 } 18579 sin6->sin6_scope_id = 0; 18580 sin6->__sin6_src_id = 0; 18581 } 18582 18583 putnext(rq, mp); 18584 return; 18585 default: 18586 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 18587 if (mp != NULL) 18588 putnext(rq, mp); 18589 return; 18590 } 18591 } 18592 18593 static int 18594 tcp_getmyname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18595 { 18596 sin_t *sin = (sin_t *)sa; 18597 sin6_t *sin6 = (sin6_t *)sa; 18598 18599 switch (tcp->tcp_family) { 18600 case AF_INET: 18601 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18602 18603 if (*salenp < sizeof (sin_t)) 18604 return (EINVAL); 18605 18606 *sin = sin_null; 18607 sin->sin_family = AF_INET; 18608 sin->sin_port = tcp->tcp_lport; 18609 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 18610 break; 18611 18612 case AF_INET6: 18613 if (*salenp < sizeof (sin6_t)) 18614 return (EINVAL); 18615 18616 *sin6 = sin6_null; 18617 sin6->sin6_family = AF_INET6; 18618 sin6->sin6_port = tcp->tcp_lport; 18619 if (tcp->tcp_ipversion == IPV4_VERSION) { 18620 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 18621 &sin6->sin6_addr); 18622 } else { 18623 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 18624 } 18625 break; 18626 } 18627 18628 return (0); 18629 } 18630 18631 static int 18632 i_tcp_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 18633 { 18634 sin_t *sin = (sin_t *)sa; 18635 sin6_t *sin6 = (sin6_t *)sa; 18636 18637 if (tcp->tcp_state < TCPS_SYN_RCVD) 18638 return (ENOTCONN); 18639 18640 switch (tcp->tcp_family) { 18641 case AF_INET: 18642 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 18643 18644 if (*salenp < sizeof (sin_t)) 18645 return (EINVAL); 18646 18647 *sin = sin_null; 18648 sin->sin_family = AF_INET; 18649 sin->sin_port = tcp->tcp_fport; 18650 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 18651 sin->sin_addr.s_addr); 18652 *salenp = sizeof (sin_t); 18653 break; 18654 18655 case AF_INET6: 18656 if (*salenp < sizeof (sin6_t)) 18657 return (EINVAL); 18658 18659 *sin6 = sin6_null; 18660 sin6->sin6_family = AF_INET6; 18661 sin6->sin6_port = tcp->tcp_fport; 18662 sin6->sin6_addr = tcp->tcp_remote_v6; 18663 if (tcp->tcp_ipversion == IPV6_VERSION) { 18664 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 18665 ~IPV6_VERS_AND_FLOW_MASK; 18666 } 18667 *salenp = sizeof (sin6_t); 18668 break; 18669 } 18670 18671 return (0); 18672 } 18673 18674 /* 18675 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 18676 */ 18677 static void 18678 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18679 { 18680 void *data; 18681 mblk_t *datamp = mp->b_cont; 18682 tcp_t *tcp = Q_TO_TCP(q); 18683 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18684 18685 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18686 cmdp->cb_error = EPROTO; 18687 qreply(q, mp); 18688 return; 18689 } 18690 18691 data = datamp->b_rptr; 18692 18693 switch (cmdp->cb_cmd) { 18694 case TI_GETPEERNAME: 18695 cmdp->cb_error = i_tcp_getpeername(tcp, data, &cmdp->cb_len); 18696 break; 18697 case TI_GETMYNAME: 18698 cmdp->cb_error = tcp_getmyname(tcp, data, &cmdp->cb_len); 18699 break; 18700 default: 18701 cmdp->cb_error = EINVAL; 18702 break; 18703 } 18704 18705 qreply(q, mp); 18706 } 18707 18708 void 18709 tcp_wput(queue_t *q, mblk_t *mp) 18710 { 18711 conn_t *connp = Q_TO_CONN(q); 18712 tcp_t *tcp; 18713 void (*output_proc)(); 18714 t_scalar_t type; 18715 uchar_t *rptr; 18716 struct iocblk *iocp; 18717 size_t size; 18718 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18719 18720 ASSERT(connp->conn_ref >= 2); 18721 18722 switch (DB_TYPE(mp)) { 18723 case M_DATA: 18724 tcp = connp->conn_tcp; 18725 ASSERT(tcp != NULL); 18726 18727 size = msgdsize(mp); 18728 18729 mutex_enter(&tcp->tcp_non_sq_lock); 18730 tcp->tcp_squeue_bytes += size; 18731 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18732 tcp_setqfull(tcp); 18733 } 18734 mutex_exit(&tcp->tcp_non_sq_lock); 18735 18736 CONN_INC_REF(connp); 18737 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18738 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18739 return; 18740 18741 case M_CMD: 18742 tcp_wput_cmdblk(q, mp); 18743 return; 18744 18745 case M_PROTO: 18746 case M_PCPROTO: 18747 /* 18748 * if it is a snmp message, don't get behind the squeue 18749 */ 18750 tcp = connp->conn_tcp; 18751 rptr = mp->b_rptr; 18752 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18753 type = ((union T_primitives *)rptr)->type; 18754 } else { 18755 if (tcp->tcp_debug) { 18756 (void) strlog(TCP_MOD_ID, 0, 1, 18757 SL_ERROR|SL_TRACE, 18758 "tcp_wput_proto, dropping one..."); 18759 } 18760 freemsg(mp); 18761 return; 18762 } 18763 if (type == T_SVR4_OPTMGMT_REQ) { 18764 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 18765 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18766 cr)) { 18767 /* 18768 * This was a SNMP request 18769 */ 18770 return; 18771 } else { 18772 output_proc = tcp_wput_proto; 18773 } 18774 } else { 18775 output_proc = tcp_wput_proto; 18776 } 18777 break; 18778 case M_IOCTL: 18779 /* 18780 * Most ioctls can be processed right away without going via 18781 * squeues - process them right here. Those that do require 18782 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18783 * are processed by tcp_wput_ioctl(). 18784 */ 18785 iocp = (struct iocblk *)mp->b_rptr; 18786 tcp = connp->conn_tcp; 18787 18788 switch (iocp->ioc_cmd) { 18789 case TCP_IOC_ABORT_CONN: 18790 tcp_ioctl_abort_conn(q, mp); 18791 return; 18792 case TI_GETPEERNAME: 18793 case TI_GETMYNAME: 18794 mi_copyin(q, mp, NULL, 18795 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18796 return; 18797 case ND_SET: 18798 /* nd_getset does the necessary checks */ 18799 case ND_GET: 18800 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18801 CALL_IP_WPUT(connp, q, mp); 18802 return; 18803 } 18804 qreply(q, mp); 18805 return; 18806 case TCP_IOC_DEFAULT_Q: 18807 /* 18808 * Wants to be the default wq. Check the credentials 18809 * first, the rest is executed via squeue. 18810 */ 18811 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18812 iocp->ioc_error = EPERM; 18813 iocp->ioc_count = 0; 18814 mp->b_datap->db_type = M_IOCACK; 18815 qreply(q, mp); 18816 return; 18817 } 18818 output_proc = tcp_wput_ioctl; 18819 break; 18820 default: 18821 output_proc = tcp_wput_ioctl; 18822 break; 18823 } 18824 break; 18825 default: 18826 output_proc = tcp_wput_nondata; 18827 break; 18828 } 18829 18830 CONN_INC_REF(connp); 18831 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18832 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18833 } 18834 18835 /* 18836 * Initial STREAMS write side put() procedure for sockets. It tries to 18837 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18838 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18839 * are handled by tcp_wput() as usual. 18840 * 18841 * All further messages will also be handled by tcp_wput() because we cannot 18842 * be sure that the above short cut is safe later. 18843 */ 18844 static void 18845 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18846 { 18847 conn_t *connp = Q_TO_CONN(wq); 18848 tcp_t *tcp = connp->conn_tcp; 18849 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18850 18851 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18852 wq->q_qinfo = &tcp_winit; 18853 18854 ASSERT(IPCL_IS_TCP(connp)); 18855 ASSERT(TCP_IS_SOCKET(tcp)); 18856 18857 if (DB_TYPE(mp) == M_PCPROTO && 18858 MBLKL(mp) == sizeof (struct T_capability_req) && 18859 car->PRIM_type == T_CAPABILITY_REQ) { 18860 tcp_capability_req(tcp, mp); 18861 return; 18862 } 18863 18864 tcp_wput(wq, mp); 18865 } 18866 18867 /* ARGSUSED */ 18868 static void 18869 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18870 { 18871 #ifdef DEBUG 18872 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18873 #endif 18874 freemsg(mp); 18875 } 18876 18877 static boolean_t 18878 tcp_zcopy_check(tcp_t *tcp) 18879 { 18880 conn_t *connp = tcp->tcp_connp; 18881 ire_t *ire; 18882 boolean_t zc_enabled = B_FALSE; 18883 tcp_stack_t *tcps = tcp->tcp_tcps; 18884 18885 if (do_tcpzcopy == 2) 18886 zc_enabled = B_TRUE; 18887 else if (tcp->tcp_ipversion == IPV4_VERSION && 18888 IPCL_IS_CONNECTED(connp) && 18889 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18890 connp->conn_dontroute == 0 && 18891 !connp->conn_nexthop_set && 18892 connp->conn_outgoing_ill == NULL && 18893 do_tcpzcopy == 1) { 18894 /* 18895 * the checks above closely resemble the fast path checks 18896 * in tcp_send_data(). 18897 */ 18898 mutex_enter(&connp->conn_lock); 18899 ire = connp->conn_ire_cache; 18900 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18901 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18902 IRE_REFHOLD(ire); 18903 if (ire->ire_stq != NULL) { 18904 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18905 18906 zc_enabled = ill && (ill->ill_capabilities & 18907 ILL_CAPAB_ZEROCOPY) && 18908 (ill->ill_zerocopy_capab-> 18909 ill_zerocopy_flags != 0); 18910 } 18911 IRE_REFRELE(ire); 18912 } 18913 mutex_exit(&connp->conn_lock); 18914 } 18915 tcp->tcp_snd_zcopy_on = zc_enabled; 18916 if (!TCP_IS_DETACHED(tcp)) { 18917 if (zc_enabled) { 18918 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18919 ZCVMSAFE); 18920 TCP_STAT(tcps, tcp_zcopy_on); 18921 } else { 18922 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18923 ZCVMUNSAFE); 18924 TCP_STAT(tcps, tcp_zcopy_off); 18925 } 18926 } 18927 return (zc_enabled); 18928 } 18929 18930 static mblk_t * 18931 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18932 { 18933 tcp_stack_t *tcps = tcp->tcp_tcps; 18934 18935 if (do_tcpzcopy == 2) 18936 return (bp); 18937 else if (tcp->tcp_snd_zcopy_on) { 18938 tcp->tcp_snd_zcopy_on = B_FALSE; 18939 if (!TCP_IS_DETACHED(tcp)) { 18940 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18941 ZCVMUNSAFE); 18942 TCP_STAT(tcps, tcp_zcopy_disable); 18943 } 18944 } 18945 return (tcp_zcopy_backoff(tcp, bp, 0)); 18946 } 18947 18948 /* 18949 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18950 * the original desballoca'ed segmapped mblk. 18951 */ 18952 static mblk_t * 18953 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18954 { 18955 mblk_t *head, *tail, *nbp; 18956 tcp_stack_t *tcps = tcp->tcp_tcps; 18957 18958 if (IS_VMLOANED_MBLK(bp)) { 18959 TCP_STAT(tcps, tcp_zcopy_backoff); 18960 if ((head = copyb(bp)) == NULL) { 18961 /* fail to backoff; leave it for the next backoff */ 18962 tcp->tcp_xmit_zc_clean = B_FALSE; 18963 return (bp); 18964 } 18965 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18966 if (fix_xmitlist) 18967 tcp_zcopy_notify(tcp); 18968 else 18969 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18970 } 18971 nbp = bp->b_cont; 18972 if (fix_xmitlist) { 18973 head->b_prev = bp->b_prev; 18974 head->b_next = bp->b_next; 18975 if (tcp->tcp_xmit_tail == bp) 18976 tcp->tcp_xmit_tail = head; 18977 } 18978 bp->b_next = NULL; 18979 bp->b_prev = NULL; 18980 freeb(bp); 18981 } else { 18982 head = bp; 18983 nbp = bp->b_cont; 18984 } 18985 tail = head; 18986 while (nbp) { 18987 if (IS_VMLOANED_MBLK(nbp)) { 18988 TCP_STAT(tcps, tcp_zcopy_backoff); 18989 if ((tail->b_cont = copyb(nbp)) == NULL) { 18990 tcp->tcp_xmit_zc_clean = B_FALSE; 18991 tail->b_cont = nbp; 18992 return (head); 18993 } 18994 tail = tail->b_cont; 18995 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18996 if (fix_xmitlist) 18997 tcp_zcopy_notify(tcp); 18998 else 18999 tail->b_datap->db_struioflag |= 19000 STRUIO_ZCNOTIFY; 19001 } 19002 bp = nbp; 19003 nbp = nbp->b_cont; 19004 if (fix_xmitlist) { 19005 tail->b_prev = bp->b_prev; 19006 tail->b_next = bp->b_next; 19007 if (tcp->tcp_xmit_tail == bp) 19008 tcp->tcp_xmit_tail = tail; 19009 } 19010 bp->b_next = NULL; 19011 bp->b_prev = NULL; 19012 freeb(bp); 19013 } else { 19014 tail->b_cont = nbp; 19015 tail = nbp; 19016 nbp = nbp->b_cont; 19017 } 19018 } 19019 if (fix_xmitlist) { 19020 tcp->tcp_xmit_last = tail; 19021 tcp->tcp_xmit_zc_clean = B_TRUE; 19022 } 19023 return (head); 19024 } 19025 19026 static void 19027 tcp_zcopy_notify(tcp_t *tcp) 19028 { 19029 struct stdata *stp; 19030 conn_t *connp; 19031 19032 if (tcp->tcp_detached) 19033 return; 19034 connp = tcp->tcp_connp; 19035 if (IPCL_IS_NONSTR(connp)) { 19036 (*connp->conn_upcalls->su_zcopy_notify) 19037 (connp->conn_upper_handle); 19038 return; 19039 } 19040 stp = STREAM(tcp->tcp_rq); 19041 mutex_enter(&stp->sd_lock); 19042 stp->sd_flag |= STZCNOTIFY; 19043 cv_broadcast(&stp->sd_zcopy_wait); 19044 mutex_exit(&stp->sd_lock); 19045 } 19046 19047 static boolean_t 19048 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 19049 { 19050 ire_t *ire; 19051 conn_t *connp = tcp->tcp_connp; 19052 tcp_stack_t *tcps = tcp->tcp_tcps; 19053 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19054 19055 mutex_enter(&connp->conn_lock); 19056 ire = connp->conn_ire_cache; 19057 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19058 19059 if ((ire != NULL) && 19060 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 19061 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 19062 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19063 IRE_REFHOLD(ire); 19064 mutex_exit(&connp->conn_lock); 19065 } else { 19066 boolean_t cached = B_FALSE; 19067 ts_label_t *tsl; 19068 19069 /* force a recheck later on */ 19070 tcp->tcp_ire_ill_check_done = B_FALSE; 19071 19072 TCP_DBGSTAT(tcps, tcp_ire_null1); 19073 connp->conn_ire_cache = NULL; 19074 mutex_exit(&connp->conn_lock); 19075 19076 if (ire != NULL) 19077 IRE_REFRELE_NOTR(ire); 19078 19079 tsl = crgetlabel(CONN_CRED(connp)); 19080 ire = (dst ? 19081 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 19082 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19083 connp->conn_zoneid, tsl, ipst)); 19084 19085 if (ire == NULL) { 19086 TCP_STAT(tcps, tcp_ire_null); 19087 return (B_FALSE); 19088 } 19089 19090 IRE_REFHOLD_NOTR(ire); 19091 19092 mutex_enter(&connp->conn_lock); 19093 if (CONN_CACHE_IRE(connp)) { 19094 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19095 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19096 TCP_CHECK_IREINFO(tcp, ire); 19097 connp->conn_ire_cache = ire; 19098 cached = B_TRUE; 19099 } 19100 rw_exit(&ire->ire_bucket->irb_lock); 19101 } 19102 mutex_exit(&connp->conn_lock); 19103 19104 /* 19105 * We can continue to use the ire but since it was 19106 * not cached, we should drop the extra reference. 19107 */ 19108 if (!cached) 19109 IRE_REFRELE_NOTR(ire); 19110 19111 /* 19112 * Rampart note: no need to select a new label here, since 19113 * labels are not allowed to change during the life of a TCP 19114 * connection. 19115 */ 19116 } 19117 19118 *irep = ire; 19119 19120 return (B_TRUE); 19121 } 19122 19123 /* 19124 * Called from tcp_send() or tcp_send_data() to find workable IRE. 19125 * 19126 * 0 = success; 19127 * 1 = failed to find ire and ill. 19128 */ 19129 static boolean_t 19130 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 19131 { 19132 ipha_t *ipha; 19133 ipaddr_t dst; 19134 ire_t *ire; 19135 ill_t *ill; 19136 mblk_t *ire_fp_mp; 19137 tcp_stack_t *tcps = tcp->tcp_tcps; 19138 19139 if (mp != NULL) 19140 ipha = (ipha_t *)mp->b_rptr; 19141 else 19142 ipha = tcp->tcp_ipha; 19143 dst = ipha->ipha_dst; 19144 19145 if (!tcp_send_find_ire(tcp, &dst, &ire)) 19146 return (B_FALSE); 19147 19148 if ((ire->ire_flags & RTF_MULTIRT) || 19149 (ire->ire_stq == NULL) || 19150 (ire->ire_nce == NULL) || 19151 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 19152 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 19153 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 19154 TCP_STAT(tcps, tcp_ip_ire_send); 19155 IRE_REFRELE(ire); 19156 return (B_FALSE); 19157 } 19158 19159 ill = ire_to_ill(ire); 19160 ASSERT(ill != NULL); 19161 19162 if (!tcp->tcp_ire_ill_check_done) { 19163 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19164 tcp->tcp_ire_ill_check_done = B_TRUE; 19165 } 19166 19167 *irep = ire; 19168 *illp = ill; 19169 19170 return (B_TRUE); 19171 } 19172 19173 static void 19174 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 19175 { 19176 ipha_t *ipha; 19177 ipaddr_t src; 19178 ipaddr_t dst; 19179 uint32_t cksum; 19180 ire_t *ire; 19181 uint16_t *up; 19182 ill_t *ill; 19183 conn_t *connp = tcp->tcp_connp; 19184 uint32_t hcksum_txflags = 0; 19185 mblk_t *ire_fp_mp; 19186 uint_t ire_fp_mp_len; 19187 tcp_stack_t *tcps = tcp->tcp_tcps; 19188 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19189 19190 ASSERT(DB_TYPE(mp) == M_DATA); 19191 19192 if (is_system_labeled() && DB_CRED(mp) == NULL) 19193 mblk_setcred(mp, CONN_CRED(tcp->tcp_connp)); 19194 19195 ipha = (ipha_t *)mp->b_rptr; 19196 src = ipha->ipha_src; 19197 dst = ipha->ipha_dst; 19198 19199 ASSERT(q != NULL); 19200 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 19201 19202 /* 19203 * Drop off fast path for IPv6 and also if options are present or 19204 * we need to resolve a TS label. 19205 */ 19206 if (tcp->tcp_ipversion != IPV4_VERSION || 19207 !IPCL_IS_CONNECTED(connp) || 19208 !CONN_IS_LSO_MD_FASTPATH(connp) || 19209 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 19210 !connp->conn_ulp_labeled || 19211 ipha->ipha_ident == IP_HDR_INCLUDED || 19212 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 19213 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 19214 if (tcp->tcp_snd_zcopy_aware) 19215 mp = tcp_zcopy_disable(tcp, mp); 19216 TCP_STAT(tcps, tcp_ip_send); 19217 CALL_IP_WPUT(connp, q, mp); 19218 return; 19219 } 19220 19221 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 19222 if (tcp->tcp_snd_zcopy_aware) 19223 mp = tcp_zcopy_backoff(tcp, mp, 0); 19224 CALL_IP_WPUT(connp, q, mp); 19225 return; 19226 } 19227 ire_fp_mp = ire->ire_nce->nce_fp_mp; 19228 ire_fp_mp_len = MBLKL(ire_fp_mp); 19229 19230 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 19231 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 19232 #ifndef _BIG_ENDIAN 19233 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 19234 #endif 19235 19236 /* 19237 * Check to see if we need to re-enable LSO/MDT for this connection 19238 * because it was previously disabled due to changes in the ill; 19239 * note that by doing it here, this re-enabling only applies when 19240 * the packet is not dispatched through CALL_IP_WPUT(). 19241 * 19242 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 19243 * case, since that's how we ended up here. For IPv6, we do the 19244 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 19245 */ 19246 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 19247 /* 19248 * Restore LSO for this connection, so that next time around 19249 * it is eligible to go through tcp_lsosend() path again. 19250 */ 19251 TCP_STAT(tcps, tcp_lso_enabled); 19252 tcp->tcp_lso = B_TRUE; 19253 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 19254 "interface %s\n", (void *)connp, ill->ill_name)); 19255 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 19256 /* 19257 * Restore MDT for this connection, so that next time around 19258 * it is eligible to go through tcp_multisend() path again. 19259 */ 19260 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 19261 tcp->tcp_mdt = B_TRUE; 19262 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 19263 "interface %s\n", (void *)connp, ill->ill_name)); 19264 } 19265 19266 if (tcp->tcp_snd_zcopy_aware) { 19267 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 19268 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 19269 mp = tcp_zcopy_disable(tcp, mp); 19270 /* 19271 * we shouldn't need to reset ipha as the mp containing 19272 * ipha should never be a zero-copy mp. 19273 */ 19274 } 19275 19276 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 19277 ASSERT(ill->ill_hcksum_capab != NULL); 19278 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 19279 } 19280 19281 /* pseudo-header checksum (do it in parts for IP header checksum) */ 19282 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 19283 19284 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 19285 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 19286 19287 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 19288 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 19289 19290 /* Software checksum? */ 19291 if (DB_CKSUMFLAGS(mp) == 0) { 19292 TCP_STAT(tcps, tcp_out_sw_cksum); 19293 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 19294 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 19295 } 19296 19297 /* Calculate IP header checksum if hardware isn't capable */ 19298 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 19299 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 19300 ((uint16_t *)ipha)[4]); 19301 } 19302 19303 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 19304 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 19305 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 19306 19307 UPDATE_OB_PKT_COUNT(ire); 19308 ire->ire_last_used_time = lbolt; 19309 19310 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 19311 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 19312 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 19313 ntohs(ipha->ipha_length)); 19314 19315 DTRACE_PROBE4(ip4__physical__out__start, 19316 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 19317 FW_HOOKS(ipst->ips_ip4_physical_out_event, 19318 ipst->ips_ipv4firewall_physical_out, 19319 NULL, ill, ipha, mp, mp, 0, ipst); 19320 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 19321 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 19322 19323 if (mp != NULL) { 19324 if (ipst->ips_ipobs_enabled) { 19325 zoneid_t szone; 19326 19327 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 19328 ipst, ALL_ZONES); 19329 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 19330 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 19331 } 19332 19333 ILL_SEND_TX(ill, ire, connp, mp, 0); 19334 } 19335 19336 IRE_REFRELE(ire); 19337 } 19338 19339 /* 19340 * This handles the case when the receiver has shrunk its win. Per RFC 1122 19341 * if the receiver shrinks the window, i.e. moves the right window to the 19342 * left, the we should not send new data, but should retransmit normally the 19343 * old unacked data between suna and suna + swnd. We might has sent data 19344 * that is now outside the new window, pretend that we didn't send it. 19345 */ 19346 static void 19347 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 19348 { 19349 uint32_t snxt = tcp->tcp_snxt; 19350 mblk_t *xmit_tail; 19351 int32_t offset; 19352 19353 ASSERT(shrunk_count > 0); 19354 19355 /* Pretend we didn't send the data outside the window */ 19356 snxt -= shrunk_count; 19357 19358 /* Get the mblk and the offset in it per the shrunk window */ 19359 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 19360 19361 ASSERT(xmit_tail != NULL); 19362 19363 /* Reset all the values per the now shrunk window */ 19364 tcp->tcp_snxt = snxt; 19365 tcp->tcp_xmit_tail = xmit_tail; 19366 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 19367 offset; 19368 tcp->tcp_unsent += shrunk_count; 19369 19370 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 19371 /* 19372 * Make sure the timer is running so that we will probe a zero 19373 * window. 19374 */ 19375 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19376 } 19377 19378 19379 /* 19380 * The TCP normal data output path. 19381 * NOTE: the logic of the fast path is duplicated from this function. 19382 */ 19383 static void 19384 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 19385 { 19386 int len; 19387 mblk_t *local_time; 19388 mblk_t *mp1; 19389 uint32_t snxt; 19390 int tail_unsent; 19391 int tcpstate; 19392 int usable = 0; 19393 mblk_t *xmit_tail; 19394 queue_t *q = tcp->tcp_wq; 19395 int32_t mss; 19396 int32_t num_sack_blk = 0; 19397 int32_t tcp_hdr_len; 19398 int32_t tcp_tcp_hdr_len; 19399 int mdt_thres; 19400 int rc; 19401 tcp_stack_t *tcps = tcp->tcp_tcps; 19402 ip_stack_t *ipst; 19403 19404 tcpstate = tcp->tcp_state; 19405 if (mp == NULL) { 19406 /* 19407 * tcp_wput_data() with NULL mp should only be called when 19408 * there is unsent data. 19409 */ 19410 ASSERT(tcp->tcp_unsent > 0); 19411 /* Really tacky... but we need this for detached closes. */ 19412 len = tcp->tcp_unsent; 19413 goto data_null; 19414 } 19415 19416 #if CCS_STATS 19417 wrw_stats.tot.count++; 19418 wrw_stats.tot.bytes += msgdsize(mp); 19419 #endif 19420 ASSERT(mp->b_datap->db_type == M_DATA); 19421 /* 19422 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 19423 * or before a connection attempt has begun. 19424 */ 19425 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 19426 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19427 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 19428 #ifdef DEBUG 19429 cmn_err(CE_WARN, 19430 "tcp_wput_data: data after ordrel, %s", 19431 tcp_display(tcp, NULL, 19432 DISP_ADDR_AND_PORT)); 19433 #else 19434 if (tcp->tcp_debug) { 19435 (void) strlog(TCP_MOD_ID, 0, 1, 19436 SL_TRACE|SL_ERROR, 19437 "tcp_wput_data: data after ordrel, %s\n", 19438 tcp_display(tcp, NULL, 19439 DISP_ADDR_AND_PORT)); 19440 } 19441 #endif /* DEBUG */ 19442 } 19443 if (tcp->tcp_snd_zcopy_aware && 19444 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 19445 tcp_zcopy_notify(tcp); 19446 freemsg(mp); 19447 mutex_enter(&tcp->tcp_non_sq_lock); 19448 if (tcp->tcp_flow_stopped && 19449 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19450 tcp_clrqfull(tcp); 19451 } 19452 mutex_exit(&tcp->tcp_non_sq_lock); 19453 return; 19454 } 19455 19456 /* Strip empties */ 19457 for (;;) { 19458 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 19459 (uintptr_t)INT_MAX); 19460 len = (int)(mp->b_wptr - mp->b_rptr); 19461 if (len > 0) 19462 break; 19463 mp1 = mp; 19464 mp = mp->b_cont; 19465 freeb(mp1); 19466 if (!mp) { 19467 return; 19468 } 19469 } 19470 19471 /* If we are the first on the list ... */ 19472 if (tcp->tcp_xmit_head == NULL) { 19473 tcp->tcp_xmit_head = mp; 19474 tcp->tcp_xmit_tail = mp; 19475 tcp->tcp_xmit_tail_unsent = len; 19476 } else { 19477 /* If tiny tx and room in txq tail, pullup to save mblks. */ 19478 struct datab *dp; 19479 19480 mp1 = tcp->tcp_xmit_last; 19481 if (len < tcp_tx_pull_len && 19482 (dp = mp1->b_datap)->db_ref == 1 && 19483 dp->db_lim - mp1->b_wptr >= len) { 19484 ASSERT(len > 0); 19485 ASSERT(!mp1->b_cont); 19486 if (len == 1) { 19487 *mp1->b_wptr++ = *mp->b_rptr; 19488 } else { 19489 bcopy(mp->b_rptr, mp1->b_wptr, len); 19490 mp1->b_wptr += len; 19491 } 19492 if (mp1 == tcp->tcp_xmit_tail) 19493 tcp->tcp_xmit_tail_unsent += len; 19494 mp1->b_cont = mp->b_cont; 19495 if (tcp->tcp_snd_zcopy_aware && 19496 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 19497 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 19498 freeb(mp); 19499 mp = mp1; 19500 } else { 19501 tcp->tcp_xmit_last->b_cont = mp; 19502 } 19503 len += tcp->tcp_unsent; 19504 } 19505 19506 /* Tack on however many more positive length mblks we have */ 19507 if ((mp1 = mp->b_cont) != NULL) { 19508 do { 19509 int tlen; 19510 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 19511 (uintptr_t)INT_MAX); 19512 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 19513 if (tlen <= 0) { 19514 mp->b_cont = mp1->b_cont; 19515 freeb(mp1); 19516 } else { 19517 len += tlen; 19518 mp = mp1; 19519 } 19520 } while ((mp1 = mp->b_cont) != NULL); 19521 } 19522 tcp->tcp_xmit_last = mp; 19523 tcp->tcp_unsent = len; 19524 19525 if (urgent) 19526 usable = 1; 19527 19528 data_null: 19529 snxt = tcp->tcp_snxt; 19530 xmit_tail = tcp->tcp_xmit_tail; 19531 tail_unsent = tcp->tcp_xmit_tail_unsent; 19532 19533 /* 19534 * Note that tcp_mss has been adjusted to take into account the 19535 * timestamp option if applicable. Because SACK options do not 19536 * appear in every TCP segments and they are of variable lengths, 19537 * they cannot be included in tcp_mss. Thus we need to calculate 19538 * the actual segment length when we need to send a segment which 19539 * includes SACK options. 19540 */ 19541 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 19542 int32_t opt_len; 19543 19544 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 19545 tcp->tcp_num_sack_blk); 19546 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 19547 2 + TCPOPT_HEADER_LEN; 19548 mss = tcp->tcp_mss - opt_len; 19549 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 19550 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 19551 } else { 19552 mss = tcp->tcp_mss; 19553 tcp_hdr_len = tcp->tcp_hdr_len; 19554 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 19555 } 19556 19557 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 19558 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 19559 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 19560 } 19561 if (tcpstate == TCPS_SYN_RCVD) { 19562 /* 19563 * The three-way connection establishment handshake is not 19564 * complete yet. We want to queue the data for transmission 19565 * after entering ESTABLISHED state (RFC793). A jump to 19566 * "done" label effectively leaves data on the queue. 19567 */ 19568 goto done; 19569 } else { 19570 int usable_r; 19571 19572 /* 19573 * In the special case when cwnd is zero, which can only 19574 * happen if the connection is ECN capable, return now. 19575 * New segments is sent using tcp_timer(). The timer 19576 * is set in tcp_rput_data(). 19577 */ 19578 if (tcp->tcp_cwnd == 0) { 19579 /* 19580 * Note that tcp_cwnd is 0 before 3-way handshake is 19581 * finished. 19582 */ 19583 ASSERT(tcp->tcp_ecn_ok || 19584 tcp->tcp_state < TCPS_ESTABLISHED); 19585 return; 19586 } 19587 19588 /* NOTE: trouble if xmitting while SYN not acked? */ 19589 usable_r = snxt - tcp->tcp_suna; 19590 usable_r = tcp->tcp_swnd - usable_r; 19591 19592 /* 19593 * Check if the receiver has shrunk the window. If 19594 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 19595 * cannot be set as there is unsent data, so FIN cannot 19596 * be sent out. Otherwise, we need to take into account 19597 * of FIN as it consumes an "invisible" sequence number. 19598 */ 19599 ASSERT(tcp->tcp_fin_sent == 0); 19600 if (usable_r < 0) { 19601 /* 19602 * The receiver has shrunk the window and we have sent 19603 * -usable_r date beyond the window, re-adjust. 19604 * 19605 * If TCP window scaling is enabled, there can be 19606 * round down error as the advertised receive window 19607 * is actually right shifted n bits. This means that 19608 * the lower n bits info is wiped out. It will look 19609 * like the window is shrunk. Do a check here to 19610 * see if the shrunk amount is actually within the 19611 * error in window calculation. If it is, just 19612 * return. Note that this check is inside the 19613 * shrunk window check. This makes sure that even 19614 * though tcp_process_shrunk_swnd() is not called, 19615 * we will stop further processing. 19616 */ 19617 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 19618 tcp_process_shrunk_swnd(tcp, -usable_r); 19619 } 19620 return; 19621 } 19622 19623 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 19624 if (tcp->tcp_swnd > tcp->tcp_cwnd) 19625 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 19626 19627 /* usable = MIN(usable, unsent) */ 19628 if (usable_r > len) 19629 usable_r = len; 19630 19631 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 19632 if (usable_r > 0) { 19633 usable = usable_r; 19634 } else { 19635 /* Bypass all other unnecessary processing. */ 19636 goto done; 19637 } 19638 } 19639 19640 local_time = (mblk_t *)lbolt; 19641 19642 /* 19643 * "Our" Nagle Algorithm. This is not the same as in the old 19644 * BSD. This is more in line with the true intent of Nagle. 19645 * 19646 * The conditions are: 19647 * 1. The amount of unsent data (or amount of data which can be 19648 * sent, whichever is smaller) is less than Nagle limit. 19649 * 2. The last sent size is also less than Nagle limit. 19650 * 3. There is unack'ed data. 19651 * 4. Urgent pointer is not set. Send urgent data ignoring the 19652 * Nagle algorithm. This reduces the probability that urgent 19653 * bytes get "merged" together. 19654 * 5. The app has not closed the connection. This eliminates the 19655 * wait time of the receiving side waiting for the last piece of 19656 * (small) data. 19657 * 19658 * If all are satisified, exit without sending anything. Note 19659 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19660 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19661 * 4095). 19662 */ 19663 if (usable < (int)tcp->tcp_naglim && 19664 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19665 snxt != tcp->tcp_suna && 19666 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19667 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19668 goto done; 19669 } 19670 19671 if (tcp->tcp_cork) { 19672 /* 19673 * if the tcp->tcp_cork option is set, then we have to force 19674 * TCP not to send partial segment (smaller than MSS bytes). 19675 * We are calculating the usable now based on full mss and 19676 * will save the rest of remaining data for later. 19677 */ 19678 if (usable < mss) 19679 goto done; 19680 usable = (usable / mss) * mss; 19681 } 19682 19683 /* Update the latest receive window size in TCP header. */ 19684 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19685 tcp->tcp_tcph->th_win); 19686 19687 /* 19688 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19689 * 19690 * 1. Simple TCP/IP{v4,v6} (no options). 19691 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19692 * 3. If the TCP connection is in ESTABLISHED state. 19693 * 4. The TCP is not detached. 19694 * 19695 * If any of the above conditions have changed during the 19696 * connection, stop using LSO/MDT and restore the stream head 19697 * parameters accordingly. 19698 */ 19699 ipst = tcps->tcps_netstack->netstack_ip; 19700 19701 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19702 ((tcp->tcp_ipversion == IPV4_VERSION && 19703 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19704 (tcp->tcp_ipversion == IPV6_VERSION && 19705 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19706 tcp->tcp_state != TCPS_ESTABLISHED || 19707 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19708 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19709 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19710 if (tcp->tcp_lso) { 19711 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19712 tcp->tcp_lso = B_FALSE; 19713 } else { 19714 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19715 tcp->tcp_mdt = B_FALSE; 19716 } 19717 19718 /* Anything other than detached is considered pathological */ 19719 if (!TCP_IS_DETACHED(tcp)) { 19720 if (tcp->tcp_lso) 19721 TCP_STAT(tcps, tcp_lso_disabled); 19722 else 19723 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19724 (void) tcp_maxpsz_set(tcp, B_TRUE); 19725 } 19726 } 19727 19728 /* Use MDT if sendable amount is greater than the threshold */ 19729 if (tcp->tcp_mdt && 19730 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19731 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19732 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19733 (tcp->tcp_valid_bits == 0 || 19734 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19735 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19736 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19737 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19738 local_time, mdt_thres); 19739 } else { 19740 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19741 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19742 local_time, INT_MAX); 19743 } 19744 19745 /* Pretend that all we were trying to send really got sent */ 19746 if (rc < 0 && tail_unsent < 0) { 19747 do { 19748 xmit_tail = xmit_tail->b_cont; 19749 xmit_tail->b_prev = local_time; 19750 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19751 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19752 tail_unsent += (int)(xmit_tail->b_wptr - 19753 xmit_tail->b_rptr); 19754 } while (tail_unsent < 0); 19755 } 19756 done:; 19757 tcp->tcp_xmit_tail = xmit_tail; 19758 tcp->tcp_xmit_tail_unsent = tail_unsent; 19759 len = tcp->tcp_snxt - snxt; 19760 if (len) { 19761 /* 19762 * If new data was sent, need to update the notsack 19763 * list, which is, afterall, data blocks that have 19764 * not been sack'ed by the receiver. New data is 19765 * not sack'ed. 19766 */ 19767 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19768 /* len is a negative value. */ 19769 tcp->tcp_pipe -= len; 19770 tcp_notsack_update(&(tcp->tcp_notsack_list), 19771 tcp->tcp_snxt, snxt, 19772 &(tcp->tcp_num_notsack_blk), 19773 &(tcp->tcp_cnt_notsack_list)); 19774 } 19775 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19776 tcp->tcp_rack = tcp->tcp_rnxt; 19777 tcp->tcp_rack_cnt = 0; 19778 if ((snxt + len) == tcp->tcp_suna) { 19779 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19780 } 19781 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19782 /* 19783 * Didn't send anything. Make sure the timer is running 19784 * so that we will probe a zero window. 19785 */ 19786 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19787 } 19788 /* Note that len is the amount we just sent but with a negative sign */ 19789 tcp->tcp_unsent += len; 19790 mutex_enter(&tcp->tcp_non_sq_lock); 19791 if (tcp->tcp_flow_stopped) { 19792 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19793 tcp_clrqfull(tcp); 19794 } 19795 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19796 tcp_setqfull(tcp); 19797 } 19798 mutex_exit(&tcp->tcp_non_sq_lock); 19799 } 19800 19801 /* 19802 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19803 * outgoing TCP header with the template header, as well as other 19804 * options such as time-stamp, ECN and/or SACK. 19805 */ 19806 static void 19807 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19808 { 19809 tcph_t *tcp_tmpl, *tcp_h; 19810 uint32_t *dst, *src; 19811 int hdrlen; 19812 19813 ASSERT(OK_32PTR(rptr)); 19814 19815 /* Template header */ 19816 tcp_tmpl = tcp->tcp_tcph; 19817 19818 /* Header of outgoing packet */ 19819 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19820 19821 /* dst and src are opaque 32-bit fields, used for copying */ 19822 dst = (uint32_t *)rptr; 19823 src = (uint32_t *)tcp->tcp_iphc; 19824 hdrlen = tcp->tcp_hdr_len; 19825 19826 /* Fill time-stamp option if needed */ 19827 if (tcp->tcp_snd_ts_ok) { 19828 U32_TO_BE32((uint32_t)now, 19829 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19830 U32_TO_BE32(tcp->tcp_ts_recent, 19831 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19832 } else { 19833 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19834 } 19835 19836 /* 19837 * Copy the template header; is this really more efficient than 19838 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19839 * but perhaps not for other scenarios. 19840 */ 19841 dst[0] = src[0]; 19842 dst[1] = src[1]; 19843 dst[2] = src[2]; 19844 dst[3] = src[3]; 19845 dst[4] = src[4]; 19846 dst[5] = src[5]; 19847 dst[6] = src[6]; 19848 dst[7] = src[7]; 19849 dst[8] = src[8]; 19850 dst[9] = src[9]; 19851 if (hdrlen -= 40) { 19852 hdrlen >>= 2; 19853 dst += 10; 19854 src += 10; 19855 do { 19856 *dst++ = *src++; 19857 } while (--hdrlen); 19858 } 19859 19860 /* 19861 * Set the ECN info in the TCP header if it is not a zero 19862 * window probe. Zero window probe is only sent in 19863 * tcp_wput_data() and tcp_timer(). 19864 */ 19865 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19866 SET_ECT(tcp, rptr); 19867 19868 if (tcp->tcp_ecn_echo_on) 19869 tcp_h->th_flags[0] |= TH_ECE; 19870 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19871 tcp_h->th_flags[0] |= TH_CWR; 19872 tcp->tcp_ecn_cwr_sent = B_TRUE; 19873 } 19874 } 19875 19876 /* Fill in SACK options */ 19877 if (num_sack_blk > 0) { 19878 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19879 sack_blk_t *tmp; 19880 int32_t i; 19881 19882 wptr[0] = TCPOPT_NOP; 19883 wptr[1] = TCPOPT_NOP; 19884 wptr[2] = TCPOPT_SACK; 19885 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19886 sizeof (sack_blk_t); 19887 wptr += TCPOPT_REAL_SACK_LEN; 19888 19889 tmp = tcp->tcp_sack_list; 19890 for (i = 0; i < num_sack_blk; i++) { 19891 U32_TO_BE32(tmp[i].begin, wptr); 19892 wptr += sizeof (tcp_seq); 19893 U32_TO_BE32(tmp[i].end, wptr); 19894 wptr += sizeof (tcp_seq); 19895 } 19896 tcp_h->th_offset_and_rsrvd[0] += 19897 ((num_sack_blk * 2 + 1) << 4); 19898 } 19899 } 19900 19901 /* 19902 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19903 * the destination address and SAP attribute, and if necessary, the 19904 * hardware checksum offload attribute to a Multidata message. 19905 */ 19906 static int 19907 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19908 const uint32_t start, const uint32_t stuff, const uint32_t end, 19909 const uint32_t flags, tcp_stack_t *tcps) 19910 { 19911 /* Add global destination address & SAP attribute */ 19912 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19913 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19914 "destination address+SAP\n")); 19915 19916 if (dlmp != NULL) 19917 TCP_STAT(tcps, tcp_mdt_allocfail); 19918 return (-1); 19919 } 19920 19921 /* Add global hwcksum attribute */ 19922 if (hwcksum && 19923 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19924 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19925 "checksum attribute\n")); 19926 19927 TCP_STAT(tcps, tcp_mdt_allocfail); 19928 return (-1); 19929 } 19930 19931 return (0); 19932 } 19933 19934 /* 19935 * Smaller and private version of pdescinfo_t used specifically for TCP, 19936 * which allows for only two payload spans per packet. 19937 */ 19938 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19939 19940 /* 19941 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19942 * scheme, and returns one the following: 19943 * 19944 * -1 = failed allocation. 19945 * 0 = success; burst count reached, or usable send window is too small, 19946 * and that we'd rather wait until later before sending again. 19947 */ 19948 static int 19949 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19950 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19951 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19952 const int mdt_thres) 19953 { 19954 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19955 multidata_t *mmd; 19956 uint_t obsegs, obbytes, hdr_frag_sz; 19957 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19958 int num_burst_seg, max_pld; 19959 pdesc_t *pkt; 19960 tcp_pdescinfo_t tcp_pkt_info; 19961 pdescinfo_t *pkt_info; 19962 int pbuf_idx, pbuf_idx_nxt; 19963 int seg_len, len, spill, af; 19964 boolean_t add_buffer, zcopy, clusterwide; 19965 boolean_t rconfirm = B_FALSE; 19966 boolean_t done = B_FALSE; 19967 uint32_t cksum; 19968 uint32_t hwcksum_flags; 19969 ire_t *ire = NULL; 19970 ill_t *ill; 19971 ipha_t *ipha; 19972 ip6_t *ip6h; 19973 ipaddr_t src, dst; 19974 ill_zerocopy_capab_t *zc_cap = NULL; 19975 uint16_t *up; 19976 int err; 19977 conn_t *connp; 19978 tcp_stack_t *tcps = tcp->tcp_tcps; 19979 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19980 int usable_mmd, tail_unsent_mmd; 19981 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19982 mblk_t *xmit_tail_mmd; 19983 netstackid_t stack_id; 19984 19985 #ifdef _BIG_ENDIAN 19986 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19987 #else 19988 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19989 #endif 19990 19991 #define PREP_NEW_MULTIDATA() { \ 19992 mmd = NULL; \ 19993 md_mp = md_hbuf = NULL; \ 19994 cur_hdr_off = 0; \ 19995 max_pld = tcp->tcp_mdt_max_pld; \ 19996 pbuf_idx = pbuf_idx_nxt = -1; \ 19997 add_buffer = B_TRUE; \ 19998 zcopy = B_FALSE; \ 19999 } 20000 20001 #define PREP_NEW_PBUF() { \ 20002 md_pbuf = md_pbuf_nxt = NULL; \ 20003 pbuf_idx = pbuf_idx_nxt = -1; \ 20004 cur_pld_off = 0; \ 20005 first_snxt = *snxt; \ 20006 ASSERT(*tail_unsent > 0); \ 20007 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 20008 } 20009 20010 ASSERT(mdt_thres >= mss); 20011 ASSERT(*usable > 0 && *usable > mdt_thres); 20012 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20013 ASSERT(!TCP_IS_DETACHED(tcp)); 20014 ASSERT(tcp->tcp_valid_bits == 0 || 20015 tcp->tcp_valid_bits == TCP_FSS_VALID); 20016 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 20017 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 20018 (tcp->tcp_ipversion == IPV6_VERSION && 20019 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 20020 20021 connp = tcp->tcp_connp; 20022 ASSERT(connp != NULL); 20023 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 20024 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 20025 20026 stack_id = connp->conn_netstack->netstack_stackid; 20027 20028 usable_mmd = tail_unsent_mmd = 0; 20029 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 20030 xmit_tail_mmd = NULL; 20031 /* 20032 * Note that tcp will only declare at most 2 payload spans per 20033 * packet, which is much lower than the maximum allowable number 20034 * of packet spans per Multidata. For this reason, we use the 20035 * privately declared and smaller descriptor info structure, in 20036 * order to save some stack space. 20037 */ 20038 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 20039 20040 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 20041 if (af == AF_INET) { 20042 dst = tcp->tcp_ipha->ipha_dst; 20043 src = tcp->tcp_ipha->ipha_src; 20044 ASSERT(!CLASSD(dst)); 20045 } 20046 ASSERT(af == AF_INET || 20047 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 20048 20049 obsegs = obbytes = 0; 20050 num_burst_seg = tcp->tcp_snd_burst; 20051 md_mp_head = NULL; 20052 PREP_NEW_MULTIDATA(); 20053 20054 /* 20055 * Before we go on further, make sure there is an IRE that we can 20056 * use, and that the ILL supports MDT. Otherwise, there's no point 20057 * in proceeding any further, and we should just hand everything 20058 * off to the legacy path. 20059 */ 20060 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 20061 goto legacy_send_no_md; 20062 20063 ASSERT(ire != NULL); 20064 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 20065 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 20066 ASSERT(af == AF_INET || ire->ire_nce != NULL); 20067 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 20068 /* 20069 * If we do support loopback for MDT (which requires modifications 20070 * to the receiving paths), the following assertions should go away, 20071 * and we would be sending the Multidata to loopback conn later on. 20072 */ 20073 ASSERT(!IRE_IS_LOCAL(ire)); 20074 ASSERT(ire->ire_stq != NULL); 20075 20076 ill = ire_to_ill(ire); 20077 ASSERT(ill != NULL); 20078 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 20079 20080 if (!tcp->tcp_ire_ill_check_done) { 20081 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 20082 tcp->tcp_ire_ill_check_done = B_TRUE; 20083 } 20084 20085 /* 20086 * If the underlying interface conditions have changed, or if the 20087 * new interface does not support MDT, go back to legacy path. 20088 */ 20089 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 20090 /* don't go through this path anymore for this connection */ 20091 TCP_STAT(tcps, tcp_mdt_conn_halted2); 20092 tcp->tcp_mdt = B_FALSE; 20093 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 20094 "interface %s\n", (void *)connp, ill->ill_name)); 20095 /* IRE will be released prior to returning */ 20096 goto legacy_send_no_md; 20097 } 20098 20099 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 20100 zc_cap = ill->ill_zerocopy_capab; 20101 20102 /* 20103 * Check if we can take tcp fast-path. Note that "incomplete" 20104 * ire's (where the link-layer for next hop is not resolved 20105 * or where the fast-path header in nce_fp_mp is not available 20106 * yet) are sent down the legacy (slow) path. 20107 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 20108 */ 20109 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 20110 /* IRE will be released prior to returning */ 20111 goto legacy_send_no_md; 20112 } 20113 20114 /* go to legacy path if interface doesn't support zerocopy */ 20115 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 20116 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 20117 /* IRE will be released prior to returning */ 20118 goto legacy_send_no_md; 20119 } 20120 20121 /* does the interface support hardware checksum offload? */ 20122 hwcksum_flags = 0; 20123 if (ILL_HCKSUM_CAPABLE(ill) && 20124 (ill->ill_hcksum_capab->ill_hcksum_txflags & 20125 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 20126 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 20127 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20128 HCKSUM_IPHDRCKSUM) 20129 hwcksum_flags = HCK_IPV4_HDRCKSUM; 20130 20131 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20132 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 20133 hwcksum_flags |= HCK_FULLCKSUM; 20134 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 20135 HCKSUM_INET_PARTIAL) 20136 hwcksum_flags |= HCK_PARTIALCKSUM; 20137 } 20138 20139 /* 20140 * Each header fragment consists of the leading extra space, 20141 * followed by the TCP/IP header, and the trailing extra space. 20142 * We make sure that each header fragment begins on a 32-bit 20143 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 20144 * aligned in tcp_mdt_update). 20145 */ 20146 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 20147 tcp->tcp_mdt_hdr_tail), 4); 20148 20149 /* are we starting from the beginning of data block? */ 20150 if (*tail_unsent == 0) { 20151 *xmit_tail = (*xmit_tail)->b_cont; 20152 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 20153 *tail_unsent = (int)MBLKL(*xmit_tail); 20154 } 20155 20156 /* 20157 * Here we create one or more Multidata messages, each made up of 20158 * one header buffer and up to N payload buffers. This entire 20159 * operation is done within two loops: 20160 * 20161 * The outer loop mostly deals with creating the Multidata message, 20162 * as well as the header buffer that gets added to it. It also 20163 * links the Multidata messages together such that all of them can 20164 * be sent down to the lower layer in a single putnext call; this 20165 * linking behavior depends on the tcp_mdt_chain tunable. 20166 * 20167 * The inner loop takes an existing Multidata message, and adds 20168 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 20169 * packetizes those buffers by filling up the corresponding header 20170 * buffer fragments with the proper IP and TCP headers, and by 20171 * describing the layout of each packet in the packet descriptors 20172 * that get added to the Multidata. 20173 */ 20174 do { 20175 /* 20176 * If usable send window is too small, or data blocks in 20177 * transmit list are smaller than our threshold (i.e. app 20178 * performs large writes followed by small ones), we hand 20179 * off the control over to the legacy path. Note that we'll 20180 * get back the control once it encounters a large block. 20181 */ 20182 if (*usable < mss || (*tail_unsent <= mdt_thres && 20183 (*xmit_tail)->b_cont != NULL && 20184 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 20185 /* send down what we've got so far */ 20186 if (md_mp_head != NULL) { 20187 tcp_multisend_data(tcp, ire, ill, md_mp_head, 20188 obsegs, obbytes, &rconfirm); 20189 } 20190 /* 20191 * Pass control over to tcp_send(), but tell it to 20192 * return to us once a large-size transmission is 20193 * possible. 20194 */ 20195 TCP_STAT(tcps, tcp_mdt_legacy_small); 20196 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 20197 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 20198 tail_unsent, xmit_tail, local_time, 20199 mdt_thres)) <= 0) { 20200 /* burst count reached, or alloc failed */ 20201 IRE_REFRELE(ire); 20202 return (err); 20203 } 20204 20205 /* tcp_send() may have sent everything, so check */ 20206 if (*usable <= 0) { 20207 IRE_REFRELE(ire); 20208 return (0); 20209 } 20210 20211 TCP_STAT(tcps, tcp_mdt_legacy_ret); 20212 /* 20213 * We may have delivered the Multidata, so make sure 20214 * to re-initialize before the next round. 20215 */ 20216 md_mp_head = NULL; 20217 obsegs = obbytes = 0; 20218 num_burst_seg = tcp->tcp_snd_burst; 20219 PREP_NEW_MULTIDATA(); 20220 20221 /* are we starting from the beginning of data block? */ 20222 if (*tail_unsent == 0) { 20223 *xmit_tail = (*xmit_tail)->b_cont; 20224 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20225 (uintptr_t)INT_MAX); 20226 *tail_unsent = (int)MBLKL(*xmit_tail); 20227 } 20228 } 20229 /* 20230 * Record current values for parameters we may need to pass 20231 * to tcp_send() or tcp_multisend_data(). We checkpoint at 20232 * each iteration of the outer loop (each multidata message 20233 * creation). If we have a failure in the inner loop, we send 20234 * any complete multidata messages we have before reverting 20235 * to using the traditional non-md path. 20236 */ 20237 snxt_mmd = *snxt; 20238 usable_mmd = *usable; 20239 xmit_tail_mmd = *xmit_tail; 20240 tail_unsent_mmd = *tail_unsent; 20241 obsegs_mmd = obsegs; 20242 obbytes_mmd = obbytes; 20243 20244 /* 20245 * max_pld limits the number of mblks in tcp's transmit 20246 * queue that can be added to a Multidata message. Once 20247 * this counter reaches zero, no more additional mblks 20248 * can be added to it. What happens afterwards depends 20249 * on whether or not we are set to chain the Multidata 20250 * messages. If we are to link them together, reset 20251 * max_pld to its original value (tcp_mdt_max_pld) and 20252 * prepare to create a new Multidata message which will 20253 * get linked to md_mp_head. Else, leave it alone and 20254 * let the inner loop break on its own. 20255 */ 20256 if (tcp_mdt_chain && max_pld == 0) 20257 PREP_NEW_MULTIDATA(); 20258 20259 /* adding a payload buffer; re-initialize values */ 20260 if (add_buffer) 20261 PREP_NEW_PBUF(); 20262 20263 /* 20264 * If we don't have a Multidata, either because we just 20265 * (re)entered this outer loop, or after we branched off 20266 * to tcp_send above, setup the Multidata and header 20267 * buffer to be used. 20268 */ 20269 if (md_mp == NULL) { 20270 int md_hbuflen; 20271 uint32_t start, stuff; 20272 20273 /* 20274 * Calculate Multidata header buffer size large enough 20275 * to hold all of the headers that can possibly be 20276 * sent at this moment. We'd rather over-estimate 20277 * the size than running out of space; this is okay 20278 * since this buffer is small anyway. 20279 */ 20280 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 20281 20282 /* 20283 * Start and stuff offset for partial hardware 20284 * checksum offload; these are currently for IPv4. 20285 * For full checksum offload, they are set to zero. 20286 */ 20287 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 20288 if (af == AF_INET) { 20289 start = IP_SIMPLE_HDR_LENGTH; 20290 stuff = IP_SIMPLE_HDR_LENGTH + 20291 TCP_CHECKSUM_OFFSET; 20292 } else { 20293 start = IPV6_HDR_LEN; 20294 stuff = IPV6_HDR_LEN + 20295 TCP_CHECKSUM_OFFSET; 20296 } 20297 } else { 20298 start = stuff = 0; 20299 } 20300 20301 /* 20302 * Create the header buffer, Multidata, as well as 20303 * any necessary attributes (destination address, 20304 * SAP and hardware checksum offload) that should 20305 * be associated with the Multidata message. 20306 */ 20307 ASSERT(cur_hdr_off == 0); 20308 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 20309 ((md_hbuf->b_wptr += md_hbuflen), 20310 (mmd = mmd_alloc(md_hbuf, &md_mp, 20311 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 20312 /* fastpath mblk */ 20313 ire->ire_nce->nce_res_mp, 20314 /* hardware checksum enabled */ 20315 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 20316 /* hardware checksum offsets */ 20317 start, stuff, 0, 20318 /* hardware checksum flag */ 20319 hwcksum_flags, tcps) != 0)) { 20320 legacy_send: 20321 /* 20322 * We arrive here from a failure within the 20323 * inner (packetizer) loop or we fail one of 20324 * the conditionals above. We restore the 20325 * previously checkpointed values for: 20326 * xmit_tail 20327 * usable 20328 * tail_unsent 20329 * snxt 20330 * obbytes 20331 * obsegs 20332 * We should then be able to dispatch any 20333 * complete multidata before reverting to the 20334 * traditional path with consistent parameters 20335 * (the inner loop updates these as it 20336 * iterates). 20337 */ 20338 *xmit_tail = xmit_tail_mmd; 20339 *usable = usable_mmd; 20340 *tail_unsent = tail_unsent_mmd; 20341 *snxt = snxt_mmd; 20342 obbytes = obbytes_mmd; 20343 obsegs = obsegs_mmd; 20344 if (md_mp != NULL) { 20345 /* Unlink message from the chain */ 20346 if (md_mp_head != NULL) { 20347 err = (intptr_t)rmvb(md_mp_head, 20348 md_mp); 20349 /* 20350 * We can't assert that rmvb 20351 * did not return -1, since we 20352 * may get here before linkb 20353 * happens. We do, however, 20354 * check if we just removed the 20355 * only element in the list. 20356 */ 20357 if (err == 0) 20358 md_mp_head = NULL; 20359 } 20360 /* md_hbuf gets freed automatically */ 20361 TCP_STAT(tcps, tcp_mdt_discarded); 20362 freeb(md_mp); 20363 } else { 20364 /* Either allocb or mmd_alloc failed */ 20365 TCP_STAT(tcps, tcp_mdt_allocfail); 20366 if (md_hbuf != NULL) 20367 freeb(md_hbuf); 20368 } 20369 20370 /* send down what we've got so far */ 20371 if (md_mp_head != NULL) { 20372 tcp_multisend_data(tcp, ire, ill, 20373 md_mp_head, obsegs, obbytes, 20374 &rconfirm); 20375 } 20376 legacy_send_no_md: 20377 if (ire != NULL) 20378 IRE_REFRELE(ire); 20379 /* 20380 * Too bad; let the legacy path handle this. 20381 * We specify INT_MAX for the threshold, since 20382 * we gave up with the Multidata processings 20383 * and let the old path have it all. 20384 */ 20385 TCP_STAT(tcps, tcp_mdt_legacy_all); 20386 return (tcp_send(q, tcp, mss, tcp_hdr_len, 20387 tcp_tcp_hdr_len, num_sack_blk, usable, 20388 snxt, tail_unsent, xmit_tail, local_time, 20389 INT_MAX)); 20390 } 20391 20392 /* link to any existing ones, if applicable */ 20393 TCP_STAT(tcps, tcp_mdt_allocd); 20394 if (md_mp_head == NULL) { 20395 md_mp_head = md_mp; 20396 } else if (tcp_mdt_chain) { 20397 TCP_STAT(tcps, tcp_mdt_linked); 20398 linkb(md_mp_head, md_mp); 20399 } 20400 } 20401 20402 ASSERT(md_mp_head != NULL); 20403 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 20404 ASSERT(md_mp != NULL && mmd != NULL); 20405 ASSERT(md_hbuf != NULL); 20406 20407 /* 20408 * Packetize the transmittable portion of the data block; 20409 * each data block is essentially added to the Multidata 20410 * as a payload buffer. We also deal with adding more 20411 * than one payload buffers, which happens when the remaining 20412 * packetized portion of the current payload buffer is less 20413 * than MSS, while the next data block in transmit queue 20414 * has enough data to make up for one. This "spillover" 20415 * case essentially creates a split-packet, where portions 20416 * of the packet's payload fragments may span across two 20417 * virtually discontiguous address blocks. 20418 */ 20419 seg_len = mss; 20420 do { 20421 len = seg_len; 20422 20423 /* one must remain NULL for DTRACE_IP_FASTPATH */ 20424 ipha = NULL; 20425 ip6h = NULL; 20426 20427 ASSERT(len > 0); 20428 ASSERT(max_pld >= 0); 20429 ASSERT(!add_buffer || cur_pld_off == 0); 20430 20431 /* 20432 * First time around for this payload buffer; note 20433 * in the case of a spillover, the following has 20434 * been done prior to adding the split-packet 20435 * descriptor to Multidata, and we don't want to 20436 * repeat the process. 20437 */ 20438 if (add_buffer) { 20439 ASSERT(mmd != NULL); 20440 ASSERT(md_pbuf == NULL); 20441 ASSERT(md_pbuf_nxt == NULL); 20442 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 20443 20444 /* 20445 * Have we reached the limit? We'd get to 20446 * this case when we're not chaining the 20447 * Multidata messages together, and since 20448 * we're done, terminate this loop. 20449 */ 20450 if (max_pld == 0) 20451 break; /* done */ 20452 20453 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 20454 TCP_STAT(tcps, tcp_mdt_allocfail); 20455 goto legacy_send; /* out_of_mem */ 20456 } 20457 20458 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 20459 zc_cap != NULL) { 20460 if (!ip_md_zcopy_attr(mmd, NULL, 20461 zc_cap->ill_zerocopy_flags)) { 20462 freeb(md_pbuf); 20463 TCP_STAT(tcps, 20464 tcp_mdt_allocfail); 20465 /* out_of_mem */ 20466 goto legacy_send; 20467 } 20468 zcopy = B_TRUE; 20469 } 20470 20471 md_pbuf->b_rptr += base_pld_off; 20472 20473 /* 20474 * Add a payload buffer to the Multidata; this 20475 * operation must not fail, or otherwise our 20476 * logic in this routine is broken. There 20477 * is no memory allocation done by the 20478 * routine, so any returned failure simply 20479 * tells us that we've done something wrong. 20480 * 20481 * A failure tells us that either we're adding 20482 * the same payload buffer more than once, or 20483 * we're trying to add more buffers than 20484 * allowed (max_pld calculation is wrong). 20485 * None of the above cases should happen, and 20486 * we panic because either there's horrible 20487 * heap corruption, and/or programming mistake. 20488 */ 20489 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 20490 if (pbuf_idx < 0) { 20491 cmn_err(CE_PANIC, "tcp_multisend: " 20492 "payload buffer logic error " 20493 "detected for tcp %p mmd %p " 20494 "pbuf %p (%d)\n", 20495 (void *)tcp, (void *)mmd, 20496 (void *)md_pbuf, pbuf_idx); 20497 } 20498 20499 ASSERT(max_pld > 0); 20500 --max_pld; 20501 add_buffer = B_FALSE; 20502 } 20503 20504 ASSERT(md_mp_head != NULL); 20505 ASSERT(md_pbuf != NULL); 20506 ASSERT(md_pbuf_nxt == NULL); 20507 ASSERT(pbuf_idx != -1); 20508 ASSERT(pbuf_idx_nxt == -1); 20509 ASSERT(*usable > 0); 20510 20511 /* 20512 * We spillover to the next payload buffer only 20513 * if all of the following is true: 20514 * 20515 * 1. There is not enough data on the current 20516 * payload buffer to make up `len', 20517 * 2. We are allowed to send `len', 20518 * 3. The next payload buffer length is large 20519 * enough to accomodate `spill'. 20520 */ 20521 if ((spill = len - *tail_unsent) > 0 && 20522 *usable >= len && 20523 MBLKL((*xmit_tail)->b_cont) >= spill && 20524 max_pld > 0) { 20525 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 20526 if (md_pbuf_nxt == NULL) { 20527 TCP_STAT(tcps, tcp_mdt_allocfail); 20528 goto legacy_send; /* out_of_mem */ 20529 } 20530 20531 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 20532 zc_cap != NULL) { 20533 if (!ip_md_zcopy_attr(mmd, NULL, 20534 zc_cap->ill_zerocopy_flags)) { 20535 freeb(md_pbuf_nxt); 20536 TCP_STAT(tcps, 20537 tcp_mdt_allocfail); 20538 /* out_of_mem */ 20539 goto legacy_send; 20540 } 20541 zcopy = B_TRUE; 20542 } 20543 20544 /* 20545 * See comments above on the first call to 20546 * mmd_addpldbuf for explanation on the panic. 20547 */ 20548 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 20549 if (pbuf_idx_nxt < 0) { 20550 panic("tcp_multisend: " 20551 "next payload buffer logic error " 20552 "detected for tcp %p mmd %p " 20553 "pbuf %p (%d)\n", 20554 (void *)tcp, (void *)mmd, 20555 (void *)md_pbuf_nxt, pbuf_idx_nxt); 20556 } 20557 20558 ASSERT(max_pld > 0); 20559 --max_pld; 20560 } else if (spill > 0) { 20561 /* 20562 * If there's a spillover, but the following 20563 * xmit_tail couldn't give us enough octets 20564 * to reach "len", then stop the current 20565 * Multidata creation and let the legacy 20566 * tcp_send() path take over. We don't want 20567 * to send the tiny segment as part of this 20568 * Multidata for performance reasons; instead, 20569 * we let the legacy path deal with grouping 20570 * it with the subsequent small mblks. 20571 */ 20572 if (*usable >= len && 20573 MBLKL((*xmit_tail)->b_cont) < spill) { 20574 max_pld = 0; 20575 break; /* done */ 20576 } 20577 20578 /* 20579 * We can't spillover, and we are near 20580 * the end of the current payload buffer, 20581 * so send what's left. 20582 */ 20583 ASSERT(*tail_unsent > 0); 20584 len = *tail_unsent; 20585 } 20586 20587 /* tail_unsent is negated if there is a spillover */ 20588 *tail_unsent -= len; 20589 *usable -= len; 20590 ASSERT(*usable >= 0); 20591 20592 if (*usable < mss) 20593 seg_len = *usable; 20594 /* 20595 * Sender SWS avoidance; see comments in tcp_send(); 20596 * everything else is the same, except that we only 20597 * do this here if there is no more data to be sent 20598 * following the current xmit_tail. We don't check 20599 * for 1-byte urgent data because we shouldn't get 20600 * here if TCP_URG_VALID is set. 20601 */ 20602 if (*usable > 0 && *usable < mss && 20603 ((md_pbuf_nxt == NULL && 20604 (*xmit_tail)->b_cont == NULL) || 20605 (md_pbuf_nxt != NULL && 20606 (*xmit_tail)->b_cont->b_cont == NULL)) && 20607 seg_len < (tcp->tcp_max_swnd >> 1) && 20608 (tcp->tcp_unsent - 20609 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 20610 !tcp->tcp_zero_win_probe) { 20611 if ((*snxt + len) == tcp->tcp_snxt && 20612 (*snxt + len) == tcp->tcp_suna) { 20613 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20614 } 20615 done = B_TRUE; 20616 } 20617 20618 /* 20619 * Prime pump for IP's checksumming on our behalf; 20620 * include the adjustment for a source route if any. 20621 * Do this only for software/partial hardware checksum 20622 * offload, as this field gets zeroed out later for 20623 * the full hardware checksum offload case. 20624 */ 20625 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 20626 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20627 cksum = (cksum >> 16) + (cksum & 0xFFFF); 20628 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 20629 } 20630 20631 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 20632 *snxt += len; 20633 20634 tcp->tcp_tcph->th_flags[0] = TH_ACK; 20635 /* 20636 * We set the PUSH bit only if TCP has no more buffered 20637 * data to be transmitted (or if sender SWS avoidance 20638 * takes place), as opposed to setting it for every 20639 * last packet in the burst. 20640 */ 20641 if (done || 20642 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 20643 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 20644 20645 /* 20646 * Set FIN bit if this is our last segment; snxt 20647 * already includes its length, and it will not 20648 * be adjusted after this point. 20649 */ 20650 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20651 *snxt == tcp->tcp_fss) { 20652 if (!tcp->tcp_fin_acked) { 20653 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20654 BUMP_MIB(&tcps->tcps_mib, 20655 tcpOutControl); 20656 } 20657 if (!tcp->tcp_fin_sent) { 20658 tcp->tcp_fin_sent = B_TRUE; 20659 /* 20660 * tcp state must be ESTABLISHED 20661 * in order for us to get here in 20662 * the first place. 20663 */ 20664 tcp->tcp_state = TCPS_FIN_WAIT_1; 20665 20666 /* 20667 * Upon returning from this routine, 20668 * tcp_wput_data() will set tcp_snxt 20669 * to be equal to snxt + tcp_fin_sent. 20670 * This is essentially the same as 20671 * setting it to tcp_fss + 1. 20672 */ 20673 } 20674 } 20675 20676 tcp->tcp_last_sent_len = (ushort_t)len; 20677 20678 len += tcp_hdr_len; 20679 if (tcp->tcp_ipversion == IPV4_VERSION) 20680 tcp->tcp_ipha->ipha_length = htons(len); 20681 else 20682 tcp->tcp_ip6h->ip6_plen = htons(len - 20683 ((char *)&tcp->tcp_ip6h[1] - 20684 tcp->tcp_iphc)); 20685 20686 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20687 20688 /* setup header fragment */ 20689 PDESC_HDR_ADD(pkt_info, 20690 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20691 tcp->tcp_mdt_hdr_head, /* head room */ 20692 tcp_hdr_len, /* len */ 20693 tcp->tcp_mdt_hdr_tail); /* tail room */ 20694 20695 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20696 hdr_frag_sz); 20697 ASSERT(MBLKIN(md_hbuf, 20698 (pkt_info->hdr_base - md_hbuf->b_rptr), 20699 PDESC_HDRSIZE(pkt_info))); 20700 20701 /* setup first payload fragment */ 20702 PDESC_PLD_INIT(pkt_info); 20703 PDESC_PLD_SPAN_ADD(pkt_info, 20704 pbuf_idx, /* index */ 20705 md_pbuf->b_rptr + cur_pld_off, /* start */ 20706 tcp->tcp_last_sent_len); /* len */ 20707 20708 /* create a split-packet in case of a spillover */ 20709 if (md_pbuf_nxt != NULL) { 20710 ASSERT(spill > 0); 20711 ASSERT(pbuf_idx_nxt > pbuf_idx); 20712 ASSERT(!add_buffer); 20713 20714 md_pbuf = md_pbuf_nxt; 20715 md_pbuf_nxt = NULL; 20716 pbuf_idx = pbuf_idx_nxt; 20717 pbuf_idx_nxt = -1; 20718 cur_pld_off = spill; 20719 20720 /* trim out first payload fragment */ 20721 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20722 20723 /* setup second payload fragment */ 20724 PDESC_PLD_SPAN_ADD(pkt_info, 20725 pbuf_idx, /* index */ 20726 md_pbuf->b_rptr, /* start */ 20727 spill); /* len */ 20728 20729 if ((*xmit_tail)->b_next == NULL) { 20730 /* 20731 * Store the lbolt used for RTT 20732 * estimation. We can only record one 20733 * timestamp per mblk so we do it when 20734 * we reach the end of the payload 20735 * buffer. Also we only take a new 20736 * timestamp sample when the previous 20737 * timed data from the same mblk has 20738 * been ack'ed. 20739 */ 20740 (*xmit_tail)->b_prev = local_time; 20741 (*xmit_tail)->b_next = 20742 (mblk_t *)(uintptr_t)first_snxt; 20743 } 20744 20745 first_snxt = *snxt - spill; 20746 20747 /* 20748 * Advance xmit_tail; usable could be 0 by 20749 * the time we got here, but we made sure 20750 * above that we would only spillover to 20751 * the next data block if usable includes 20752 * the spilled-over amount prior to the 20753 * subtraction. Therefore, we are sure 20754 * that xmit_tail->b_cont can't be NULL. 20755 */ 20756 ASSERT((*xmit_tail)->b_cont != NULL); 20757 *xmit_tail = (*xmit_tail)->b_cont; 20758 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20759 (uintptr_t)INT_MAX); 20760 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20761 } else { 20762 cur_pld_off += tcp->tcp_last_sent_len; 20763 } 20764 20765 /* 20766 * Fill in the header using the template header, and 20767 * add options such as time-stamp, ECN and/or SACK, 20768 * as needed. 20769 */ 20770 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20771 (clock_t)local_time, num_sack_blk); 20772 20773 /* take care of some IP header businesses */ 20774 if (af == AF_INET) { 20775 ipha = (ipha_t *)pkt_info->hdr_rptr; 20776 20777 ASSERT(OK_32PTR((uchar_t *)ipha)); 20778 ASSERT(PDESC_HDRL(pkt_info) >= 20779 IP_SIMPLE_HDR_LENGTH); 20780 ASSERT(ipha->ipha_version_and_hdr_length == 20781 IP_SIMPLE_HDR_VERSION); 20782 20783 /* 20784 * Assign ident value for current packet; see 20785 * related comments in ip_wput_ire() about the 20786 * contract private interface with clustering 20787 * group. 20788 */ 20789 clusterwide = B_FALSE; 20790 if (cl_inet_ipident != NULL) { 20791 ASSERT(cl_inet_isclusterwide != NULL); 20792 if ((*cl_inet_isclusterwide)(stack_id, 20793 IPPROTO_IP, AF_INET, 20794 (uint8_t *)(uintptr_t)src, NULL)) { 20795 ipha->ipha_ident = 20796 (*cl_inet_ipident)(stack_id, 20797 IPPROTO_IP, AF_INET, 20798 (uint8_t *)(uintptr_t)src, 20799 (uint8_t *)(uintptr_t)dst, 20800 NULL); 20801 clusterwide = B_TRUE; 20802 } 20803 } 20804 20805 if (!clusterwide) { 20806 ipha->ipha_ident = (uint16_t) 20807 atomic_add_32_nv( 20808 &ire->ire_ident, 1); 20809 } 20810 #ifndef _BIG_ENDIAN 20811 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20812 (ipha->ipha_ident >> 8); 20813 #endif 20814 } else { 20815 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20816 20817 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20818 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20819 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20820 ASSERT(PDESC_HDRL(pkt_info) >= 20821 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20822 TCP_CHECKSUM_SIZE)); 20823 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20824 20825 if (tcp->tcp_ip_forward_progress) { 20826 rconfirm = B_TRUE; 20827 tcp->tcp_ip_forward_progress = B_FALSE; 20828 } 20829 } 20830 20831 /* at least one payload span, and at most two */ 20832 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20833 20834 /* add the packet descriptor to Multidata */ 20835 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20836 KM_NOSLEEP)) == NULL) { 20837 /* 20838 * Any failure other than ENOMEM indicates 20839 * that we have passed in invalid pkt_info 20840 * or parameters to mmd_addpdesc, which must 20841 * not happen. 20842 * 20843 * EINVAL is a result of failure on boundary 20844 * checks against the pkt_info contents. It 20845 * should not happen, and we panic because 20846 * either there's horrible heap corruption, 20847 * and/or programming mistake. 20848 */ 20849 if (err != ENOMEM) { 20850 cmn_err(CE_PANIC, "tcp_multisend: " 20851 "pdesc logic error detected for " 20852 "tcp %p mmd %p pinfo %p (%d)\n", 20853 (void *)tcp, (void *)mmd, 20854 (void *)pkt_info, err); 20855 } 20856 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20857 goto legacy_send; /* out_of_mem */ 20858 } 20859 ASSERT(pkt != NULL); 20860 20861 /* calculate IP header and TCP checksums */ 20862 if (af == AF_INET) { 20863 /* calculate pseudo-header checksum */ 20864 cksum = (dst >> 16) + (dst & 0xFFFF) + 20865 (src >> 16) + (src & 0xFFFF); 20866 20867 /* offset for TCP header checksum */ 20868 up = IPH_TCPH_CHECKSUMP(ipha, 20869 IP_SIMPLE_HDR_LENGTH); 20870 } else { 20871 up = (uint16_t *)&ip6h->ip6_src; 20872 20873 /* calculate pseudo-header checksum */ 20874 cksum = up[0] + up[1] + up[2] + up[3] + 20875 up[4] + up[5] + up[6] + up[7] + 20876 up[8] + up[9] + up[10] + up[11] + 20877 up[12] + up[13] + up[14] + up[15]; 20878 20879 /* Fold the initial sum */ 20880 cksum = (cksum & 0xffff) + (cksum >> 16); 20881 20882 up = (uint16_t *)(((uchar_t *)ip6h) + 20883 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20884 } 20885 20886 if (hwcksum_flags & HCK_FULLCKSUM) { 20887 /* clear checksum field for hardware */ 20888 *up = 0; 20889 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20890 uint32_t sum; 20891 20892 /* pseudo-header checksumming */ 20893 sum = *up + cksum + IP_TCP_CSUM_COMP; 20894 sum = (sum & 0xFFFF) + (sum >> 16); 20895 *up = (sum & 0xFFFF) + (sum >> 16); 20896 } else { 20897 /* software checksumming */ 20898 TCP_STAT(tcps, tcp_out_sw_cksum); 20899 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20900 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20901 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20902 cksum + IP_TCP_CSUM_COMP); 20903 if (*up == 0) 20904 *up = 0xFFFF; 20905 } 20906 20907 /* IPv4 header checksum */ 20908 if (af == AF_INET) { 20909 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20910 ipha->ipha_hdr_checksum = 0; 20911 } else { 20912 IP_HDR_CKSUM(ipha, cksum, 20913 ((uint32_t *)ipha)[0], 20914 ((uint16_t *)ipha)[4]); 20915 } 20916 } 20917 20918 if (af == AF_INET && 20919 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20920 af == AF_INET6 && 20921 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20922 mblk_t *mp, *mp1; 20923 uchar_t *hdr_rptr, *hdr_wptr; 20924 uchar_t *pld_rptr, *pld_wptr; 20925 20926 /* 20927 * We reconstruct a pseudo packet for the hooks 20928 * framework using mmd_transform_link(). 20929 * If it is a split packet we pullup the 20930 * payload. FW_HOOKS expects a pkt comprising 20931 * of two mblks: a header and the payload. 20932 */ 20933 if ((mp = mmd_transform_link(pkt)) == NULL) { 20934 TCP_STAT(tcps, tcp_mdt_allocfail); 20935 goto legacy_send; 20936 } 20937 20938 if (pkt_info->pld_cnt > 1) { 20939 /* split payload, more than one pld */ 20940 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20941 NULL) { 20942 freemsg(mp); 20943 TCP_STAT(tcps, 20944 tcp_mdt_allocfail); 20945 goto legacy_send; 20946 } 20947 freemsg(mp->b_cont); 20948 mp->b_cont = mp1; 20949 } else { 20950 mp1 = mp->b_cont; 20951 } 20952 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20953 20954 /* 20955 * Remember the message offsets. This is so we 20956 * can detect changes when we return from the 20957 * FW_HOOKS callbacks. 20958 */ 20959 hdr_rptr = mp->b_rptr; 20960 hdr_wptr = mp->b_wptr; 20961 pld_rptr = mp->b_cont->b_rptr; 20962 pld_wptr = mp->b_cont->b_wptr; 20963 20964 if (af == AF_INET) { 20965 DTRACE_PROBE4( 20966 ip4__physical__out__start, 20967 ill_t *, NULL, 20968 ill_t *, ill, 20969 ipha_t *, ipha, 20970 mblk_t *, mp); 20971 FW_HOOKS( 20972 ipst->ips_ip4_physical_out_event, 20973 ipst->ips_ipv4firewall_physical_out, 20974 NULL, ill, ipha, mp, mp, 0, ipst); 20975 DTRACE_PROBE1( 20976 ip4__physical__out__end, 20977 mblk_t *, mp); 20978 } else { 20979 DTRACE_PROBE4( 20980 ip6__physical__out_start, 20981 ill_t *, NULL, 20982 ill_t *, ill, 20983 ip6_t *, ip6h, 20984 mblk_t *, mp); 20985 FW_HOOKS6( 20986 ipst->ips_ip6_physical_out_event, 20987 ipst->ips_ipv6firewall_physical_out, 20988 NULL, ill, ip6h, mp, mp, 0, ipst); 20989 DTRACE_PROBE1( 20990 ip6__physical__out__end, 20991 mblk_t *, mp); 20992 } 20993 20994 if (mp == NULL || 20995 (mp1 = mp->b_cont) == NULL || 20996 mp->b_rptr != hdr_rptr || 20997 mp->b_wptr != hdr_wptr || 20998 mp1->b_rptr != pld_rptr || 20999 mp1->b_wptr != pld_wptr || 21000 mp1->b_cont != NULL) { 21001 /* 21002 * We abandon multidata processing and 21003 * return to the normal path, either 21004 * when a packet is blocked, or when 21005 * the boundaries of header buffer or 21006 * payload buffer have been changed by 21007 * FW_HOOKS[6]. 21008 */ 21009 if (mp != NULL) 21010 freemsg(mp); 21011 goto legacy_send; 21012 } 21013 /* Finished with the pseudo packet */ 21014 freemsg(mp); 21015 } 21016 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 21017 ill, ipha, ip6h); 21018 /* advance header offset */ 21019 cur_hdr_off += hdr_frag_sz; 21020 21021 obbytes += tcp->tcp_last_sent_len; 21022 ++obsegs; 21023 } while (!done && *usable > 0 && --num_burst_seg > 0 && 21024 *tail_unsent > 0); 21025 21026 if ((*xmit_tail)->b_next == NULL) { 21027 /* 21028 * Store the lbolt used for RTT estimation. We can only 21029 * record one timestamp per mblk so we do it when we 21030 * reach the end of the payload buffer. Also we only 21031 * take a new timestamp sample when the previous timed 21032 * data from the same mblk has been ack'ed. 21033 */ 21034 (*xmit_tail)->b_prev = local_time; 21035 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 21036 } 21037 21038 ASSERT(*tail_unsent >= 0); 21039 if (*tail_unsent > 0) { 21040 /* 21041 * We got here because we broke out of the above 21042 * loop due to of one of the following cases: 21043 * 21044 * 1. len < adjusted MSS (i.e. small), 21045 * 2. Sender SWS avoidance, 21046 * 3. max_pld is zero. 21047 * 21048 * We are done for this Multidata, so trim our 21049 * last payload buffer (if any) accordingly. 21050 */ 21051 if (md_pbuf != NULL) 21052 md_pbuf->b_wptr -= *tail_unsent; 21053 } else if (*usable > 0) { 21054 *xmit_tail = (*xmit_tail)->b_cont; 21055 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 21056 (uintptr_t)INT_MAX); 21057 *tail_unsent = (int)MBLKL(*xmit_tail); 21058 add_buffer = B_TRUE; 21059 } 21060 } while (!done && *usable > 0 && num_burst_seg > 0 && 21061 (tcp_mdt_chain || max_pld > 0)); 21062 21063 if (md_mp_head != NULL) { 21064 /* send everything down */ 21065 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 21066 &rconfirm); 21067 } 21068 21069 #undef PREP_NEW_MULTIDATA 21070 #undef PREP_NEW_PBUF 21071 #undef IPVER 21072 21073 IRE_REFRELE(ire); 21074 return (0); 21075 } 21076 21077 /* 21078 * A wrapper function for sending one or more Multidata messages down to 21079 * the module below ip; this routine does not release the reference of the 21080 * IRE (caller does that). This routine is analogous to tcp_send_data(). 21081 */ 21082 static void 21083 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 21084 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 21085 { 21086 uint64_t delta; 21087 nce_t *nce; 21088 tcp_stack_t *tcps = tcp->tcp_tcps; 21089 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21090 21091 ASSERT(ire != NULL && ill != NULL); 21092 ASSERT(ire->ire_stq != NULL); 21093 ASSERT(md_mp_head != NULL); 21094 ASSERT(rconfirm != NULL); 21095 21096 /* adjust MIBs and IRE timestamp */ 21097 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 21098 tcp->tcp_obsegs += obsegs; 21099 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 21100 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 21101 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 21102 21103 if (tcp->tcp_ipversion == IPV4_VERSION) { 21104 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 21105 } else { 21106 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 21107 } 21108 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 21109 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 21110 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 21111 21112 ire->ire_ob_pkt_count += obsegs; 21113 if (ire->ire_ipif != NULL) 21114 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 21115 ire->ire_last_used_time = lbolt; 21116 21117 if (ipst->ips_ipobs_enabled) { 21118 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 21119 pdesc_t *dl_pkt; 21120 pdescinfo_t pinfo; 21121 mblk_t *nmp; 21122 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 21123 21124 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 21125 (dl_pkt != NULL); 21126 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 21127 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 21128 continue; 21129 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 21130 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 21131 freemsg(nmp); 21132 } 21133 } 21134 21135 /* send it down */ 21136 putnext(ire->ire_stq, md_mp_head); 21137 21138 /* we're done for TCP/IPv4 */ 21139 if (tcp->tcp_ipversion == IPV4_VERSION) 21140 return; 21141 21142 nce = ire->ire_nce; 21143 21144 ASSERT(nce != NULL); 21145 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 21146 ASSERT(nce->nce_state != ND_INCOMPLETE); 21147 21148 /* reachability confirmation? */ 21149 if (*rconfirm) { 21150 nce->nce_last = TICK_TO_MSEC(lbolt64); 21151 if (nce->nce_state != ND_REACHABLE) { 21152 mutex_enter(&nce->nce_lock); 21153 nce->nce_state = ND_REACHABLE; 21154 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 21155 mutex_exit(&nce->nce_lock); 21156 (void) untimeout(nce->nce_timeout_id); 21157 if (ip_debug > 2) { 21158 /* ip1dbg */ 21159 pr_addr_dbg("tcp_multisend_data: state " 21160 "for %s changed to REACHABLE\n", 21161 AF_INET6, &ire->ire_addr_v6); 21162 } 21163 } 21164 /* reset transport reachability confirmation */ 21165 *rconfirm = B_FALSE; 21166 } 21167 21168 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 21169 ip1dbg(("tcp_multisend_data: delta = %" PRId64 21170 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 21171 21172 if (delta > (uint64_t)ill->ill_reachable_time) { 21173 mutex_enter(&nce->nce_lock); 21174 switch (nce->nce_state) { 21175 case ND_REACHABLE: 21176 case ND_STALE: 21177 /* 21178 * ND_REACHABLE is identical to ND_STALE in this 21179 * specific case. If reachable time has expired for 21180 * this neighbor (delta is greater than reachable 21181 * time), conceptually, the neighbor cache is no 21182 * longer in REACHABLE state, but already in STALE 21183 * state. So the correct transition here is to 21184 * ND_DELAY. 21185 */ 21186 nce->nce_state = ND_DELAY; 21187 mutex_exit(&nce->nce_lock); 21188 NDP_RESTART_TIMER(nce, 21189 ipst->ips_delay_first_probe_time); 21190 if (ip_debug > 3) { 21191 /* ip2dbg */ 21192 pr_addr_dbg("tcp_multisend_data: state " 21193 "for %s changed to DELAY\n", 21194 AF_INET6, &ire->ire_addr_v6); 21195 } 21196 break; 21197 case ND_DELAY: 21198 case ND_PROBE: 21199 mutex_exit(&nce->nce_lock); 21200 /* Timers have already started */ 21201 break; 21202 case ND_UNREACHABLE: 21203 /* 21204 * ndp timer has detected that this nce is 21205 * unreachable and initiated deleting this nce 21206 * and all its associated IREs. This is a race 21207 * where we found the ire before it was deleted 21208 * and have just sent out a packet using this 21209 * unreachable nce. 21210 */ 21211 mutex_exit(&nce->nce_lock); 21212 break; 21213 default: 21214 ASSERT(0); 21215 } 21216 } 21217 } 21218 21219 /* 21220 * Derived from tcp_send_data(). 21221 */ 21222 static void 21223 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 21224 int num_lso_seg) 21225 { 21226 ipha_t *ipha; 21227 mblk_t *ire_fp_mp; 21228 uint_t ire_fp_mp_len; 21229 uint32_t hcksum_txflags = 0; 21230 ipaddr_t src; 21231 ipaddr_t dst; 21232 uint32_t cksum; 21233 uint16_t *up; 21234 tcp_stack_t *tcps = tcp->tcp_tcps; 21235 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21236 21237 ASSERT(DB_TYPE(mp) == M_DATA); 21238 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 21239 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 21240 ASSERT(tcp->tcp_connp != NULL); 21241 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 21242 21243 ipha = (ipha_t *)mp->b_rptr; 21244 src = ipha->ipha_src; 21245 dst = ipha->ipha_dst; 21246 21247 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 21248 21249 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 21250 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 21251 num_lso_seg); 21252 #ifndef _BIG_ENDIAN 21253 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 21254 #endif 21255 if (tcp->tcp_snd_zcopy_aware) { 21256 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 21257 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 21258 mp = tcp_zcopy_disable(tcp, mp); 21259 } 21260 21261 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 21262 ASSERT(ill->ill_hcksum_capab != NULL); 21263 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 21264 } 21265 21266 /* 21267 * Since the TCP checksum should be recalculated by h/w, we can just 21268 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 21269 * pseudo-header checksum for HCK_PARTIALCKSUM. 21270 * The partial pseudo-header excludes TCP length, that was calculated 21271 * in tcp_send(), so to zero *up before further processing. 21272 */ 21273 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 21274 21275 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 21276 *up = 0; 21277 21278 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 21279 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 21280 21281 /* 21282 * Append LSO flags and mss to the mp. 21283 */ 21284 lso_info_set(mp, mss, HW_LSO); 21285 21286 ipha->ipha_fragment_offset_and_flags |= 21287 (uint32_t)htons(ire->ire_frag_flag); 21288 21289 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21290 ire_fp_mp_len = MBLKL(ire_fp_mp); 21291 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 21292 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 21293 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 21294 21295 UPDATE_OB_PKT_COUNT(ire); 21296 ire->ire_last_used_time = lbolt; 21297 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 21298 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 21299 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 21300 ntohs(ipha->ipha_length)); 21301 21302 DTRACE_PROBE4(ip4__physical__out__start, 21303 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 21304 FW_HOOKS(ipst->ips_ip4_physical_out_event, 21305 ipst->ips_ipv4firewall_physical_out, NULL, 21306 ill, ipha, mp, mp, 0, ipst); 21307 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 21308 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 21309 21310 if (mp != NULL) { 21311 if (ipst->ips_ipobs_enabled) { 21312 zoneid_t szone; 21313 21314 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 21315 ipst, ALL_ZONES); 21316 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 21317 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 21318 } 21319 21320 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0); 21321 } 21322 } 21323 21324 /* 21325 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 21326 * scheme, and returns one of the following: 21327 * 21328 * -1 = failed allocation. 21329 * 0 = success; burst count reached, or usable send window is too small, 21330 * and that we'd rather wait until later before sending again. 21331 * 1 = success; we are called from tcp_multisend(), and both usable send 21332 * window and tail_unsent are greater than the MDT threshold, and thus 21333 * Multidata Transmit should be used instead. 21334 */ 21335 static int 21336 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 21337 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 21338 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 21339 const int mdt_thres) 21340 { 21341 int num_burst_seg = tcp->tcp_snd_burst; 21342 ire_t *ire = NULL; 21343 ill_t *ill = NULL; 21344 mblk_t *ire_fp_mp = NULL; 21345 uint_t ire_fp_mp_len = 0; 21346 int num_lso_seg = 1; 21347 uint_t lso_usable; 21348 boolean_t do_lso_send = B_FALSE; 21349 tcp_stack_t *tcps = tcp->tcp_tcps; 21350 21351 /* 21352 * Check LSO capability before any further work. And the similar check 21353 * need to be done in for(;;) loop. 21354 * LSO will be deployed when therer is more than one mss of available 21355 * data and a burst transmission is allowed. 21356 */ 21357 if (tcp->tcp_lso && 21358 (tcp->tcp_valid_bits == 0 || 21359 tcp->tcp_valid_bits == TCP_FSS_VALID) && 21360 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21361 /* 21362 * Try to find usable IRE/ILL and do basic check to the ILL. 21363 */ 21364 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill)) { 21365 /* 21366 * Enable LSO with this transmission. 21367 * Since IRE has been hold in 21368 * tcp_send_find_ire_ill(), IRE_REFRELE(ire) 21369 * should be called before return. 21370 */ 21371 do_lso_send = B_TRUE; 21372 ire_fp_mp = ire->ire_nce->nce_fp_mp; 21373 ire_fp_mp_len = MBLKL(ire_fp_mp); 21374 /* Round up to multiple of 4 */ 21375 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 21376 } else { 21377 do_lso_send = B_FALSE; 21378 ill = NULL; 21379 } 21380 } 21381 21382 for (;;) { 21383 struct datab *db; 21384 tcph_t *tcph; 21385 uint32_t sum; 21386 mblk_t *mp, *mp1; 21387 uchar_t *rptr; 21388 int len; 21389 21390 /* 21391 * If we're called by tcp_multisend(), and the amount of 21392 * sendable data as well as the size of current xmit_tail 21393 * is beyond the MDT threshold, return to the caller and 21394 * let the large data transmit be done using MDT. 21395 */ 21396 if (*usable > 0 && *usable > mdt_thres && 21397 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 21398 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 21399 ASSERT(tcp->tcp_mdt); 21400 return (1); /* success; do large send */ 21401 } 21402 21403 if (num_burst_seg == 0) 21404 break; /* success; burst count reached */ 21405 21406 /* 21407 * Calculate the maximum payload length we can send in *one* 21408 * time. 21409 */ 21410 if (do_lso_send) { 21411 /* 21412 * Check whether need to do LSO any more. 21413 */ 21414 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 21415 lso_usable = MIN(tcp->tcp_lso_max, *usable); 21416 lso_usable = MIN(lso_usable, 21417 num_burst_seg * mss); 21418 21419 num_lso_seg = lso_usable / mss; 21420 if (lso_usable % mss) { 21421 num_lso_seg++; 21422 tcp->tcp_last_sent_len = (ushort_t) 21423 (lso_usable % mss); 21424 } else { 21425 tcp->tcp_last_sent_len = (ushort_t)mss; 21426 } 21427 } else { 21428 do_lso_send = B_FALSE; 21429 num_lso_seg = 1; 21430 lso_usable = mss; 21431 } 21432 } 21433 21434 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 21435 21436 /* 21437 * Adjust num_burst_seg here. 21438 */ 21439 num_burst_seg -= num_lso_seg; 21440 21441 len = mss; 21442 if (len > *usable) { 21443 ASSERT(do_lso_send == B_FALSE); 21444 21445 len = *usable; 21446 if (len <= 0) { 21447 /* Terminate the loop */ 21448 break; /* success; too small */ 21449 } 21450 /* 21451 * Sender silly-window avoidance. 21452 * Ignore this if we are going to send a 21453 * zero window probe out. 21454 * 21455 * TODO: force data into microscopic window? 21456 * ==> (!pushed || (unsent > usable)) 21457 */ 21458 if (len < (tcp->tcp_max_swnd >> 1) && 21459 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 21460 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 21461 len == 1) && (! tcp->tcp_zero_win_probe)) { 21462 /* 21463 * If the retransmit timer is not running 21464 * we start it so that we will retransmit 21465 * in the case when the the receiver has 21466 * decremented the window. 21467 */ 21468 if (*snxt == tcp->tcp_snxt && 21469 *snxt == tcp->tcp_suna) { 21470 /* 21471 * We are not supposed to send 21472 * anything. So let's wait a little 21473 * bit longer before breaking SWS 21474 * avoidance. 21475 * 21476 * What should the value be? 21477 * Suggestion: MAX(init rexmit time, 21478 * tcp->tcp_rto) 21479 */ 21480 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21481 } 21482 break; /* success; too small */ 21483 } 21484 } 21485 21486 tcph = tcp->tcp_tcph; 21487 21488 /* 21489 * The reason to adjust len here is that we need to set flags 21490 * and calculate checksum. 21491 */ 21492 if (do_lso_send) 21493 len = lso_usable; 21494 21495 *usable -= len; /* Approximate - can be adjusted later */ 21496 if (*usable > 0) 21497 tcph->th_flags[0] = TH_ACK; 21498 else 21499 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 21500 21501 /* 21502 * Prime pump for IP's checksumming on our behalf 21503 * Include the adjustment for a source route if any. 21504 */ 21505 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 21506 sum = (sum >> 16) + (sum & 0xFFFF); 21507 U16_TO_ABE16(sum, tcph->th_sum); 21508 21509 U32_TO_ABE32(*snxt, tcph->th_seq); 21510 21511 /* 21512 * Branch off to tcp_xmit_mp() if any of the VALID bits is 21513 * set. For the case when TCP_FSS_VALID is the only valid 21514 * bit (normal active close), branch off only when we think 21515 * that the FIN flag needs to be set. Note for this case, 21516 * that (snxt + len) may not reflect the actual seg_len, 21517 * as len may be further reduced in tcp_xmit_mp(). If len 21518 * gets modified, we will end up here again. 21519 */ 21520 if (tcp->tcp_valid_bits != 0 && 21521 (tcp->tcp_valid_bits != TCP_FSS_VALID || 21522 ((*snxt + len) == tcp->tcp_fss))) { 21523 uchar_t *prev_rptr; 21524 uint32_t prev_snxt = tcp->tcp_snxt; 21525 21526 if (*tail_unsent == 0) { 21527 ASSERT((*xmit_tail)->b_cont != NULL); 21528 *xmit_tail = (*xmit_tail)->b_cont; 21529 prev_rptr = (*xmit_tail)->b_rptr; 21530 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21531 (*xmit_tail)->b_rptr); 21532 } else { 21533 prev_rptr = (*xmit_tail)->b_rptr; 21534 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 21535 *tail_unsent; 21536 } 21537 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 21538 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 21539 /* Restore tcp_snxt so we get amount sent right. */ 21540 tcp->tcp_snxt = prev_snxt; 21541 if (prev_rptr == (*xmit_tail)->b_rptr) { 21542 /* 21543 * If the previous timestamp is still in use, 21544 * don't stomp on it. 21545 */ 21546 if ((*xmit_tail)->b_next == NULL) { 21547 (*xmit_tail)->b_prev = local_time; 21548 (*xmit_tail)->b_next = 21549 (mblk_t *)(uintptr_t)(*snxt); 21550 } 21551 } else 21552 (*xmit_tail)->b_rptr = prev_rptr; 21553 21554 if (mp == NULL) { 21555 if (ire != NULL) 21556 IRE_REFRELE(ire); 21557 return (-1); 21558 } 21559 mp1 = mp->b_cont; 21560 21561 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21562 tcp->tcp_last_sent_len = (ushort_t)len; 21563 while (mp1->b_cont) { 21564 *xmit_tail = (*xmit_tail)->b_cont; 21565 (*xmit_tail)->b_prev = local_time; 21566 (*xmit_tail)->b_next = 21567 (mblk_t *)(uintptr_t)(*snxt); 21568 mp1 = mp1->b_cont; 21569 } 21570 *snxt += len; 21571 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 21572 BUMP_LOCAL(tcp->tcp_obsegs); 21573 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21574 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21575 tcp_send_data(tcp, q, mp); 21576 continue; 21577 } 21578 21579 *snxt += len; /* Adjust later if we don't send all of len */ 21580 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 21581 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 21582 21583 if (*tail_unsent) { 21584 /* Are the bytes above us in flight? */ 21585 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 21586 if (rptr != (*xmit_tail)->b_rptr) { 21587 *tail_unsent -= len; 21588 if (len <= mss) /* LSO is unusable */ 21589 tcp->tcp_last_sent_len = (ushort_t)len; 21590 len += tcp_hdr_len; 21591 if (tcp->tcp_ipversion == IPV4_VERSION) 21592 tcp->tcp_ipha->ipha_length = htons(len); 21593 else 21594 tcp->tcp_ip6h->ip6_plen = 21595 htons(len - 21596 ((char *)&tcp->tcp_ip6h[1] - 21597 tcp->tcp_iphc)); 21598 mp = dupb(*xmit_tail); 21599 if (mp == NULL) { 21600 if (ire != NULL) 21601 IRE_REFRELE(ire); 21602 return (-1); /* out_of_mem */ 21603 } 21604 mp->b_rptr = rptr; 21605 /* 21606 * If the old timestamp is no longer in use, 21607 * sample a new timestamp now. 21608 */ 21609 if ((*xmit_tail)->b_next == NULL) { 21610 (*xmit_tail)->b_prev = local_time; 21611 (*xmit_tail)->b_next = 21612 (mblk_t *)(uintptr_t)(*snxt-len); 21613 } 21614 goto must_alloc; 21615 } 21616 } else { 21617 *xmit_tail = (*xmit_tail)->b_cont; 21618 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 21619 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 21620 *tail_unsent = (int)((*xmit_tail)->b_wptr - 21621 (*xmit_tail)->b_rptr); 21622 } 21623 21624 (*xmit_tail)->b_prev = local_time; 21625 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 21626 21627 *tail_unsent -= len; 21628 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 21629 tcp->tcp_last_sent_len = (ushort_t)len; 21630 21631 len += tcp_hdr_len; 21632 if (tcp->tcp_ipversion == IPV4_VERSION) 21633 tcp->tcp_ipha->ipha_length = htons(len); 21634 else 21635 tcp->tcp_ip6h->ip6_plen = htons(len - 21636 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21637 21638 mp = dupb(*xmit_tail); 21639 if (mp == NULL) { 21640 if (ire != NULL) 21641 IRE_REFRELE(ire); 21642 return (-1); /* out_of_mem */ 21643 } 21644 21645 len = tcp_hdr_len; 21646 /* 21647 * There are four reasons to allocate a new hdr mblk: 21648 * 1) The bytes above us are in use by another packet 21649 * 2) We don't have good alignment 21650 * 3) The mblk is being shared 21651 * 4) We don't have enough room for a header 21652 */ 21653 rptr = mp->b_rptr - len; 21654 if (!OK_32PTR(rptr) || 21655 ((db = mp->b_datap), db->db_ref != 2) || 21656 rptr < db->db_base + ire_fp_mp_len) { 21657 /* NOTE: we assume allocb returns an OK_32PTR */ 21658 21659 must_alloc:; 21660 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21661 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21662 if (mp1 == NULL) { 21663 freemsg(mp); 21664 if (ire != NULL) 21665 IRE_REFRELE(ire); 21666 return (-1); /* out_of_mem */ 21667 } 21668 mp1->b_cont = mp; 21669 mp = mp1; 21670 /* Leave room for Link Level header */ 21671 len = tcp_hdr_len; 21672 rptr = 21673 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21674 mp->b_wptr = &rptr[len]; 21675 } 21676 21677 /* 21678 * Fill in the header using the template header, and add 21679 * options such as time-stamp, ECN and/or SACK, as needed. 21680 */ 21681 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21682 21683 mp->b_rptr = rptr; 21684 21685 if (*tail_unsent) { 21686 int spill = *tail_unsent; 21687 21688 mp1 = mp->b_cont; 21689 if (mp1 == NULL) 21690 mp1 = mp; 21691 21692 /* 21693 * If we're a little short, tack on more mblks until 21694 * there is no more spillover. 21695 */ 21696 while (spill < 0) { 21697 mblk_t *nmp; 21698 int nmpsz; 21699 21700 nmp = (*xmit_tail)->b_cont; 21701 nmpsz = MBLKL(nmp); 21702 21703 /* 21704 * Excess data in mblk; can we split it? 21705 * If MDT is enabled for the connection, 21706 * keep on splitting as this is a transient 21707 * send path. 21708 */ 21709 if (!do_lso_send && !tcp->tcp_mdt && 21710 (spill + nmpsz > 0)) { 21711 /* 21712 * Don't split if stream head was 21713 * told to break up larger writes 21714 * into smaller ones. 21715 */ 21716 if (tcp->tcp_maxpsz > 0) 21717 break; 21718 21719 /* 21720 * Next mblk is less than SMSS/2 21721 * rounded up to nearest 64-byte; 21722 * let it get sent as part of the 21723 * next segment. 21724 */ 21725 if (tcp->tcp_localnet && 21726 !tcp->tcp_cork && 21727 (nmpsz < roundup((mss >> 1), 64))) 21728 break; 21729 } 21730 21731 *xmit_tail = nmp; 21732 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21733 /* Stash for rtt use later */ 21734 (*xmit_tail)->b_prev = local_time; 21735 (*xmit_tail)->b_next = 21736 (mblk_t *)(uintptr_t)(*snxt - len); 21737 mp1->b_cont = dupb(*xmit_tail); 21738 mp1 = mp1->b_cont; 21739 21740 spill += nmpsz; 21741 if (mp1 == NULL) { 21742 *tail_unsent = spill; 21743 freemsg(mp); 21744 if (ire != NULL) 21745 IRE_REFRELE(ire); 21746 return (-1); /* out_of_mem */ 21747 } 21748 } 21749 21750 /* Trim back any surplus on the last mblk */ 21751 if (spill >= 0) { 21752 mp1->b_wptr -= spill; 21753 *tail_unsent = spill; 21754 } else { 21755 /* 21756 * We did not send everything we could in 21757 * order to remain within the b_cont limit. 21758 */ 21759 *usable -= spill; 21760 *snxt += spill; 21761 tcp->tcp_last_sent_len += spill; 21762 UPDATE_MIB(&tcps->tcps_mib, 21763 tcpOutDataBytes, spill); 21764 /* 21765 * Adjust the checksum 21766 */ 21767 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21768 sum += spill; 21769 sum = (sum >> 16) + (sum & 0xFFFF); 21770 U16_TO_ABE16(sum, tcph->th_sum); 21771 if (tcp->tcp_ipversion == IPV4_VERSION) { 21772 sum = ntohs( 21773 ((ipha_t *)rptr)->ipha_length) + 21774 spill; 21775 ((ipha_t *)rptr)->ipha_length = 21776 htons(sum); 21777 } else { 21778 sum = ntohs( 21779 ((ip6_t *)rptr)->ip6_plen) + 21780 spill; 21781 ((ip6_t *)rptr)->ip6_plen = 21782 htons(sum); 21783 } 21784 *tail_unsent = 0; 21785 } 21786 } 21787 if (tcp->tcp_ip_forward_progress) { 21788 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21789 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21790 tcp->tcp_ip_forward_progress = B_FALSE; 21791 } 21792 21793 if (do_lso_send) { 21794 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21795 num_lso_seg); 21796 tcp->tcp_obsegs += num_lso_seg; 21797 21798 TCP_STAT(tcps, tcp_lso_times); 21799 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21800 } else { 21801 tcp_send_data(tcp, q, mp); 21802 BUMP_LOCAL(tcp->tcp_obsegs); 21803 } 21804 } 21805 21806 if (ire != NULL) 21807 IRE_REFRELE(ire); 21808 return (0); 21809 } 21810 21811 /* Unlink and return any mblk that looks like it contains a MDT info */ 21812 static mblk_t * 21813 tcp_mdt_info_mp(mblk_t *mp) 21814 { 21815 mblk_t *prev_mp; 21816 21817 for (;;) { 21818 prev_mp = mp; 21819 /* no more to process? */ 21820 if ((mp = mp->b_cont) == NULL) 21821 break; 21822 21823 switch (DB_TYPE(mp)) { 21824 case M_CTL: 21825 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21826 continue; 21827 ASSERT(prev_mp != NULL); 21828 prev_mp->b_cont = mp->b_cont; 21829 mp->b_cont = NULL; 21830 return (mp); 21831 default: 21832 break; 21833 } 21834 } 21835 return (mp); 21836 } 21837 21838 /* MDT info update routine, called when IP notifies us about MDT */ 21839 static void 21840 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21841 { 21842 boolean_t prev_state; 21843 tcp_stack_t *tcps = tcp->tcp_tcps; 21844 21845 /* 21846 * IP is telling us to abort MDT on this connection? We know 21847 * this because the capability is only turned off when IP 21848 * encounters some pathological cases, e.g. link-layer change 21849 * where the new driver doesn't support MDT, or in situation 21850 * where MDT usage on the link-layer has been switched off. 21851 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21852 * if the link-layer doesn't support MDT, and if it does, it 21853 * will indicate that the feature is to be turned on. 21854 */ 21855 prev_state = tcp->tcp_mdt; 21856 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21857 if (!tcp->tcp_mdt && !first) { 21858 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21859 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21860 (void *)tcp->tcp_connp)); 21861 } 21862 21863 /* 21864 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21865 * so disable MDT otherwise. The checks are done here 21866 * and in tcp_wput_data(). 21867 */ 21868 if (tcp->tcp_mdt && 21869 (tcp->tcp_ipversion == IPV4_VERSION && 21870 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21871 (tcp->tcp_ipversion == IPV6_VERSION && 21872 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21873 tcp->tcp_mdt = B_FALSE; 21874 21875 if (tcp->tcp_mdt) { 21876 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21877 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21878 "version (%d), expected version is %d", 21879 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21880 tcp->tcp_mdt = B_FALSE; 21881 return; 21882 } 21883 21884 /* 21885 * We need the driver to be able to handle at least three 21886 * spans per packet in order for tcp MDT to be utilized. 21887 * The first is for the header portion, while the rest are 21888 * needed to handle a packet that straddles across two 21889 * virtually non-contiguous buffers; a typical tcp packet 21890 * therefore consists of only two spans. Note that we take 21891 * a zero as "don't care". 21892 */ 21893 if (mdt_capab->ill_mdt_span_limit > 0 && 21894 mdt_capab->ill_mdt_span_limit < 3) { 21895 tcp->tcp_mdt = B_FALSE; 21896 return; 21897 } 21898 21899 /* a zero means driver wants default value */ 21900 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21901 tcps->tcps_mdt_max_pbufs); 21902 if (tcp->tcp_mdt_max_pld == 0) 21903 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21904 21905 /* ensure 32-bit alignment */ 21906 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21907 mdt_capab->ill_mdt_hdr_head), 4); 21908 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21909 mdt_capab->ill_mdt_hdr_tail), 4); 21910 21911 if (!first && !prev_state) { 21912 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21913 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21914 (void *)tcp->tcp_connp)); 21915 } 21916 } 21917 } 21918 21919 /* Unlink and return any mblk that looks like it contains a LSO info */ 21920 static mblk_t * 21921 tcp_lso_info_mp(mblk_t *mp) 21922 { 21923 mblk_t *prev_mp; 21924 21925 for (;;) { 21926 prev_mp = mp; 21927 /* no more to process? */ 21928 if ((mp = mp->b_cont) == NULL) 21929 break; 21930 21931 switch (DB_TYPE(mp)) { 21932 case M_CTL: 21933 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21934 continue; 21935 ASSERT(prev_mp != NULL); 21936 prev_mp->b_cont = mp->b_cont; 21937 mp->b_cont = NULL; 21938 return (mp); 21939 default: 21940 break; 21941 } 21942 } 21943 21944 return (mp); 21945 } 21946 21947 /* LSO info update routine, called when IP notifies us about LSO */ 21948 static void 21949 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21950 { 21951 tcp_stack_t *tcps = tcp->tcp_tcps; 21952 21953 /* 21954 * IP is telling us to abort LSO on this connection? We know 21955 * this because the capability is only turned off when IP 21956 * encounters some pathological cases, e.g. link-layer change 21957 * where the new NIC/driver doesn't support LSO, or in situation 21958 * where LSO usage on the link-layer has been switched off. 21959 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21960 * if the link-layer doesn't support LSO, and if it does, it 21961 * will indicate that the feature is to be turned on. 21962 */ 21963 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21964 TCP_STAT(tcps, tcp_lso_enabled); 21965 21966 /* 21967 * We currently only support LSO on simple TCP/IPv4, 21968 * so disable LSO otherwise. The checks are done here 21969 * and in tcp_wput_data(). 21970 */ 21971 if (tcp->tcp_lso && 21972 (tcp->tcp_ipversion == IPV4_VERSION && 21973 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21974 (tcp->tcp_ipversion == IPV6_VERSION)) { 21975 tcp->tcp_lso = B_FALSE; 21976 TCP_STAT(tcps, tcp_lso_disabled); 21977 } else { 21978 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21979 lso_capab->ill_lso_max); 21980 } 21981 } 21982 21983 static void 21984 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21985 { 21986 conn_t *connp = tcp->tcp_connp; 21987 tcp_stack_t *tcps = tcp->tcp_tcps; 21988 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21989 21990 ASSERT(ire != NULL); 21991 21992 /* 21993 * We may be in the fastpath here, and although we essentially do 21994 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21995 * we try to keep things as brief as possible. After all, these 21996 * are only best-effort checks, and we do more thorough ones prior 21997 * to calling tcp_send()/tcp_multisend(). 21998 */ 21999 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 22000 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 22001 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 22002 !(ire->ire_flags & RTF_MULTIRT) && 22003 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 22004 CONN_IS_LSO_MD_FASTPATH(connp)) { 22005 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 22006 /* Cache the result */ 22007 connp->conn_lso_ok = B_TRUE; 22008 22009 ASSERT(ill->ill_lso_capab != NULL); 22010 if (!ill->ill_lso_capab->ill_lso_on) { 22011 ill->ill_lso_capab->ill_lso_on = 1; 22012 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22013 "LSO for interface %s\n", (void *)connp, 22014 ill->ill_name)); 22015 } 22016 tcp_lso_update(tcp, ill->ill_lso_capab); 22017 } else if (ipst->ips_ip_multidata_outbound && 22018 ILL_MDT_CAPABLE(ill)) { 22019 /* Cache the result */ 22020 connp->conn_mdt_ok = B_TRUE; 22021 22022 ASSERT(ill->ill_mdt_capab != NULL); 22023 if (!ill->ill_mdt_capab->ill_mdt_on) { 22024 ill->ill_mdt_capab->ill_mdt_on = 1; 22025 ip1dbg(("tcp_ire_ill_check: connp %p enables " 22026 "MDT for interface %s\n", (void *)connp, 22027 ill->ill_name)); 22028 } 22029 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 22030 } 22031 } 22032 22033 /* 22034 * The goal is to reduce the number of generated tcp segments by 22035 * setting the maxpsz multiplier to 0; this will have an affect on 22036 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 22037 * into each packet, up to SMSS bytes. Doing this reduces the number 22038 * of outbound segments and incoming ACKs, thus allowing for better 22039 * network and system performance. In contrast the legacy behavior 22040 * may result in sending less than SMSS size, because the last mblk 22041 * for some packets may have more data than needed to make up SMSS, 22042 * and the legacy code refused to "split" it. 22043 * 22044 * We apply the new behavior on following situations: 22045 * 22046 * 1) Loopback connections, 22047 * 2) Connections in which the remote peer is not on local subnet, 22048 * 3) Local subnet connections over the bge interface (see below). 22049 * 22050 * Ideally, we would like this behavior to apply for interfaces other 22051 * than bge. However, doing so would negatively impact drivers which 22052 * perform dynamic mapping and unmapping of DMA resources, which are 22053 * increased by setting the maxpsz multiplier to 0 (more mblks per 22054 * packet will be generated by tcp). The bge driver does not suffer 22055 * from this, as it copies the mblks into pre-mapped buffers, and 22056 * therefore does not require more I/O resources than before. 22057 * 22058 * Otherwise, this behavior is present on all network interfaces when 22059 * the destination endpoint is non-local, since reducing the number 22060 * of packets in general is good for the network. 22061 * 22062 * TODO We need to remove this hard-coded conditional for bge once 22063 * a better "self-tuning" mechanism, or a way to comprehend 22064 * the driver transmit strategy is devised. Until the solution 22065 * is found and well understood, we live with this hack. 22066 */ 22067 if (!tcp_static_maxpsz && 22068 (tcp->tcp_loopback || !tcp->tcp_localnet || 22069 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 22070 /* override the default value */ 22071 tcp->tcp_maxpsz = 0; 22072 22073 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 22074 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 22075 ill != NULL ? ill->ill_name : ipif_loopback_name)); 22076 } 22077 22078 /* set the stream head parameters accordingly */ 22079 (void) tcp_maxpsz_set(tcp, B_TRUE); 22080 } 22081 22082 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 22083 static void 22084 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 22085 { 22086 uchar_t fval = *mp->b_rptr; 22087 mblk_t *tail; 22088 queue_t *q = tcp->tcp_wq; 22089 22090 /* TODO: How should flush interact with urgent data? */ 22091 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 22092 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 22093 /* 22094 * Flush only data that has not yet been put on the wire. If 22095 * we flush data that we have already transmitted, life, as we 22096 * know it, may come to an end. 22097 */ 22098 tail = tcp->tcp_xmit_tail; 22099 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 22100 tcp->tcp_xmit_tail_unsent = 0; 22101 tcp->tcp_unsent = 0; 22102 if (tail->b_wptr != tail->b_rptr) 22103 tail = tail->b_cont; 22104 if (tail) { 22105 mblk_t **excess = &tcp->tcp_xmit_head; 22106 for (;;) { 22107 mblk_t *mp1 = *excess; 22108 if (mp1 == tail) 22109 break; 22110 tcp->tcp_xmit_tail = mp1; 22111 tcp->tcp_xmit_last = mp1; 22112 excess = &mp1->b_cont; 22113 } 22114 *excess = NULL; 22115 tcp_close_mpp(&tail); 22116 if (tcp->tcp_snd_zcopy_aware) 22117 tcp_zcopy_notify(tcp); 22118 } 22119 /* 22120 * We have no unsent data, so unsent must be less than 22121 * tcp_xmit_lowater, so re-enable flow. 22122 */ 22123 mutex_enter(&tcp->tcp_non_sq_lock); 22124 if (tcp->tcp_flow_stopped) { 22125 tcp_clrqfull(tcp); 22126 } 22127 mutex_exit(&tcp->tcp_non_sq_lock); 22128 } 22129 /* 22130 * TODO: you can't just flush these, you have to increase rwnd for one 22131 * thing. For another, how should urgent data interact? 22132 */ 22133 if (fval & FLUSHR) { 22134 *mp->b_rptr = fval & ~FLUSHW; 22135 /* XXX */ 22136 qreply(q, mp); 22137 return; 22138 } 22139 freemsg(mp); 22140 } 22141 22142 /* 22143 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 22144 * messages. 22145 */ 22146 static void 22147 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 22148 { 22149 mblk_t *mp1; 22150 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 22151 STRUCT_HANDLE(strbuf, sb); 22152 queue_t *q = tcp->tcp_wq; 22153 int error; 22154 uint_t addrlen; 22155 22156 /* Make sure it is one of ours. */ 22157 switch (iocp->ioc_cmd) { 22158 case TI_GETMYNAME: 22159 case TI_GETPEERNAME: 22160 break; 22161 default: 22162 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 22163 return; 22164 } 22165 switch (mi_copy_state(q, mp, &mp1)) { 22166 case -1: 22167 return; 22168 case MI_COPY_CASE(MI_COPY_IN, 1): 22169 break; 22170 case MI_COPY_CASE(MI_COPY_OUT, 1): 22171 /* Copy out the strbuf. */ 22172 mi_copyout(q, mp); 22173 return; 22174 case MI_COPY_CASE(MI_COPY_OUT, 2): 22175 /* All done. */ 22176 mi_copy_done(q, mp, 0); 22177 return; 22178 default: 22179 mi_copy_done(q, mp, EPROTO); 22180 return; 22181 } 22182 /* Check alignment of the strbuf */ 22183 if (!OK_32PTR(mp1->b_rptr)) { 22184 mi_copy_done(q, mp, EINVAL); 22185 return; 22186 } 22187 22188 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 22189 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 22190 if (STRUCT_FGET(sb, maxlen) < addrlen) { 22191 mi_copy_done(q, mp, EINVAL); 22192 return; 22193 } 22194 22195 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 22196 if (mp1 == NULL) 22197 return; 22198 22199 switch (iocp->ioc_cmd) { 22200 case TI_GETMYNAME: 22201 error = tcp_getmyname(tcp, (void *)mp1->b_rptr, &addrlen); 22202 break; 22203 case TI_GETPEERNAME: 22204 error = i_tcp_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 22205 break; 22206 } 22207 22208 if (error != 0) { 22209 mi_copy_done(q, mp, error); 22210 } else { 22211 mp1->b_wptr += addrlen; 22212 STRUCT_FSET(sb, len, addrlen); 22213 22214 /* Copy out the address */ 22215 mi_copyout(q, mp); 22216 } 22217 } 22218 22219 static void 22220 tcp_disable_direct_sockfs(tcp_t *tcp) 22221 { 22222 #ifdef _ILP32 22223 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 22224 #else 22225 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 22226 #endif 22227 /* 22228 * Insert this socket into the acceptor hash. 22229 * We might need it for T_CONN_RES message 22230 */ 22231 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 22232 22233 if (tcp->tcp_fused) { 22234 /* 22235 * This is a fused loopback tcp; disable 22236 * read-side synchronous streams interface 22237 * and drain any queued data. It is okay 22238 * to do this for non-synchronous streams 22239 * fused tcp as well. 22240 */ 22241 tcp_fuse_disable_pair(tcp, B_FALSE); 22242 } 22243 tcp->tcp_issocket = B_FALSE; 22244 tcp->tcp_sodirect = NULL; 22245 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 22246 } 22247 22248 /* 22249 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 22250 * messages. 22251 */ 22252 /* ARGSUSED */ 22253 static void 22254 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 22255 { 22256 conn_t *connp = (conn_t *)arg; 22257 tcp_t *tcp = connp->conn_tcp; 22258 queue_t *q = tcp->tcp_wq; 22259 struct iocblk *iocp; 22260 22261 ASSERT(DB_TYPE(mp) == M_IOCTL); 22262 /* 22263 * Try and ASSERT the minimum possible references on the 22264 * conn early enough. Since we are executing on write side, 22265 * the connection is obviously not detached and that means 22266 * there is a ref each for TCP and IP. Since we are behind 22267 * the squeue, the minimum references needed are 3. If the 22268 * conn is in classifier hash list, there should be an 22269 * extra ref for that (we check both the possibilities). 22270 */ 22271 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22272 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22273 22274 iocp = (struct iocblk *)mp->b_rptr; 22275 switch (iocp->ioc_cmd) { 22276 case TCP_IOC_DEFAULT_Q: 22277 /* Wants to be the default wq. */ 22278 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 22279 iocp->ioc_error = EPERM; 22280 iocp->ioc_count = 0; 22281 mp->b_datap->db_type = M_IOCACK; 22282 qreply(q, mp); 22283 return; 22284 } 22285 tcp_def_q_set(tcp, mp); 22286 return; 22287 case _SIOCSOCKFALLBACK: 22288 /* 22289 * Either sockmod is about to be popped and the socket 22290 * would now be treated as a plain stream, or a module 22291 * is about to be pushed so we could no longer use read- 22292 * side synchronous streams for fused loopback tcp. 22293 * Drain any queued data and disable direct sockfs 22294 * interface from now on. 22295 */ 22296 if (!tcp->tcp_issocket) { 22297 DB_TYPE(mp) = M_IOCNAK; 22298 iocp->ioc_error = EINVAL; 22299 } else { 22300 tcp_disable_direct_sockfs(tcp); 22301 DB_TYPE(mp) = M_IOCACK; 22302 iocp->ioc_error = 0; 22303 } 22304 iocp->ioc_count = 0; 22305 iocp->ioc_rval = 0; 22306 qreply(q, mp); 22307 return; 22308 } 22309 CALL_IP_WPUT(connp, q, mp); 22310 } 22311 22312 /* 22313 * This routine is called by tcp_wput() to handle all TPI requests. 22314 */ 22315 /* ARGSUSED */ 22316 static void 22317 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 22318 { 22319 conn_t *connp = (conn_t *)arg; 22320 tcp_t *tcp = connp->conn_tcp; 22321 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 22322 uchar_t *rptr; 22323 t_scalar_t type; 22324 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 22325 22326 /* 22327 * Try and ASSERT the minimum possible references on the 22328 * conn early enough. Since we are executing on write side, 22329 * the connection is obviously not detached and that means 22330 * there is a ref each for TCP and IP. Since we are behind 22331 * the squeue, the minimum references needed are 3. If the 22332 * conn is in classifier hash list, there should be an 22333 * extra ref for that (we check both the possibilities). 22334 */ 22335 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 22336 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 22337 22338 rptr = mp->b_rptr; 22339 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22340 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 22341 type = ((union T_primitives *)rptr)->type; 22342 if (type == T_EXDATA_REQ) { 22343 tcp_output_urgent(connp, mp->b_cont, arg2); 22344 freeb(mp); 22345 } else if (type != T_DATA_REQ) { 22346 goto non_urgent_data; 22347 } else { 22348 /* TODO: options, flags, ... from user */ 22349 /* Set length to zero for reclamation below */ 22350 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 22351 freeb(mp); 22352 } 22353 return; 22354 } else { 22355 if (tcp->tcp_debug) { 22356 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22357 "tcp_wput_proto, dropping one..."); 22358 } 22359 freemsg(mp); 22360 return; 22361 } 22362 22363 non_urgent_data: 22364 22365 switch ((int)tprim->type) { 22366 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 22367 /* 22368 * save the kssl_ent_t from the next block, and convert this 22369 * back to a normal bind_req. 22370 */ 22371 if (mp->b_cont != NULL) { 22372 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 22373 22374 if (tcp->tcp_kssl_ent != NULL) { 22375 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 22376 KSSL_NO_PROXY); 22377 tcp->tcp_kssl_ent = NULL; 22378 } 22379 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 22380 sizeof (kssl_ent_t)); 22381 kssl_hold_ent(tcp->tcp_kssl_ent); 22382 freemsg(mp->b_cont); 22383 mp->b_cont = NULL; 22384 } 22385 tprim->type = T_BIND_REQ; 22386 22387 /* FALLTHROUGH */ 22388 case O_T_BIND_REQ: /* bind request */ 22389 case T_BIND_REQ: /* new semantics bind request */ 22390 tcp_tpi_bind(tcp, mp); 22391 break; 22392 case T_UNBIND_REQ: /* unbind request */ 22393 tcp_tpi_unbind(tcp, mp); 22394 break; 22395 case O_T_CONN_RES: /* old connection response XXX */ 22396 case T_CONN_RES: /* connection response */ 22397 tcp_tli_accept(tcp, mp); 22398 break; 22399 case T_CONN_REQ: /* connection request */ 22400 tcp_tpi_connect(tcp, mp); 22401 break; 22402 case T_DISCON_REQ: /* disconnect request */ 22403 tcp_disconnect(tcp, mp); 22404 break; 22405 case T_CAPABILITY_REQ: 22406 tcp_capability_req(tcp, mp); /* capability request */ 22407 break; 22408 case T_INFO_REQ: /* information request */ 22409 tcp_info_req(tcp, mp); 22410 break; 22411 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 22412 /* 22413 * If EINPROGRESS is returned, the request has been queued 22414 * for subsequent processing by ip_restart_optmgmt(), which 22415 * will do the CONN_DEC_REF(). 22416 */ 22417 CONN_INC_REF(connp); 22418 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22419 B_TRUE) != EINPROGRESS) { 22420 CONN_DEC_REF(connp); 22421 } 22422 break; 22423 case T_OPTMGMT_REQ: 22424 /* 22425 * Note: no support for snmpcom_req() through new 22426 * T_OPTMGMT_REQ. See comments in ip.c 22427 * 22428 * see comments above in T_SVR4_OPTMGMT_REQ for conn 22429 * reference changes. 22430 */ 22431 CONN_INC_REF(connp); 22432 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 22433 B_TRUE) != EINPROGRESS) { 22434 CONN_DEC_REF(connp); 22435 } 22436 break; 22437 22438 case T_UNITDATA_REQ: /* unitdata request */ 22439 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22440 break; 22441 case T_ORDREL_REQ: /* orderly release req */ 22442 freemsg(mp); 22443 22444 if (tcp->tcp_fused) 22445 tcp_unfuse(tcp); 22446 22447 if (tcp_xmit_end(tcp) != 0) { 22448 /* 22449 * We were crossing FINs and got a reset from 22450 * the other side. Just ignore it. 22451 */ 22452 if (tcp->tcp_debug) { 22453 (void) strlog(TCP_MOD_ID, 0, 1, 22454 SL_ERROR|SL_TRACE, 22455 "tcp_wput_proto, T_ORDREL_REQ out of " 22456 "state %s", 22457 tcp_display(tcp, NULL, 22458 DISP_ADDR_AND_PORT)); 22459 } 22460 } 22461 break; 22462 case T_ADDR_REQ: 22463 tcp_addr_req(tcp, mp); 22464 break; 22465 default: 22466 if (tcp->tcp_debug) { 22467 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 22468 "tcp_wput_proto, bogus TPI msg, type %d", 22469 tprim->type); 22470 } 22471 /* 22472 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 22473 * to recover. 22474 */ 22475 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 22476 break; 22477 } 22478 } 22479 22480 /* 22481 * The TCP write service routine should never be called... 22482 */ 22483 /* ARGSUSED */ 22484 static void 22485 tcp_wsrv(queue_t *q) 22486 { 22487 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 22488 22489 TCP_STAT(tcps, tcp_wsrv_called); 22490 } 22491 22492 /* Non overlapping byte exchanger */ 22493 static void 22494 tcp_xchg(uchar_t *a, uchar_t *b, int len) 22495 { 22496 uchar_t uch; 22497 22498 while (len-- > 0) { 22499 uch = a[len]; 22500 a[len] = b[len]; 22501 b[len] = uch; 22502 } 22503 } 22504 22505 /* 22506 * Send out a control packet on the tcp connection specified. This routine 22507 * is typically called where we need a simple ACK or RST generated. 22508 */ 22509 static void 22510 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 22511 { 22512 uchar_t *rptr; 22513 tcph_t *tcph; 22514 ipha_t *ipha = NULL; 22515 ip6_t *ip6h = NULL; 22516 uint32_t sum; 22517 int tcp_hdr_len; 22518 int tcp_ip_hdr_len; 22519 mblk_t *mp; 22520 tcp_stack_t *tcps = tcp->tcp_tcps; 22521 22522 /* 22523 * Save sum for use in source route later. 22524 */ 22525 ASSERT(tcp != NULL); 22526 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 22527 tcp_hdr_len = tcp->tcp_hdr_len; 22528 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 22529 22530 /* If a text string is passed in with the request, pass it to strlog. */ 22531 if (str != NULL && tcp->tcp_debug) { 22532 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22533 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 22534 str, seq, ack, ctl); 22535 } 22536 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 22537 BPRI_MED); 22538 if (mp == NULL) { 22539 return; 22540 } 22541 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 22542 mp->b_rptr = rptr; 22543 mp->b_wptr = &rptr[tcp_hdr_len]; 22544 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 22545 22546 if (tcp->tcp_ipversion == IPV4_VERSION) { 22547 ipha = (ipha_t *)rptr; 22548 ipha->ipha_length = htons(tcp_hdr_len); 22549 } else { 22550 ip6h = (ip6_t *)rptr; 22551 ASSERT(tcp != NULL); 22552 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 22553 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22554 } 22555 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 22556 tcph->th_flags[0] = (uint8_t)ctl; 22557 if (ctl & TH_RST) { 22558 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22559 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22560 /* 22561 * Don't send TSopt w/ TH_RST packets per RFC 1323. 22562 */ 22563 if (tcp->tcp_snd_ts_ok && 22564 tcp->tcp_state > TCPS_SYN_SENT) { 22565 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 22566 *(mp->b_wptr) = TCPOPT_EOL; 22567 if (tcp->tcp_ipversion == IPV4_VERSION) { 22568 ipha->ipha_length = htons(tcp_hdr_len - 22569 TCPOPT_REAL_TS_LEN); 22570 } else { 22571 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 22572 TCPOPT_REAL_TS_LEN); 22573 } 22574 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 22575 sum -= TCPOPT_REAL_TS_LEN; 22576 } 22577 } 22578 if (ctl & TH_ACK) { 22579 if (tcp->tcp_snd_ts_ok) { 22580 U32_TO_BE32(lbolt, 22581 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22582 U32_TO_BE32(tcp->tcp_ts_recent, 22583 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22584 } 22585 22586 /* Update the latest receive window size in TCP header. */ 22587 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22588 tcph->th_win); 22589 tcp->tcp_rack = ack; 22590 tcp->tcp_rack_cnt = 0; 22591 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22592 } 22593 BUMP_LOCAL(tcp->tcp_obsegs); 22594 U32_TO_BE32(seq, tcph->th_seq); 22595 U32_TO_BE32(ack, tcph->th_ack); 22596 /* 22597 * Include the adjustment for a source route if any. 22598 */ 22599 sum = (sum >> 16) + (sum & 0xFFFF); 22600 U16_TO_BE16(sum, tcph->th_sum); 22601 tcp_send_data(tcp, tcp->tcp_wq, mp); 22602 } 22603 22604 /* 22605 * If this routine returns B_TRUE, TCP can generate a RST in response 22606 * to a segment. If it returns B_FALSE, TCP should not respond. 22607 */ 22608 static boolean_t 22609 tcp_send_rst_chk(tcp_stack_t *tcps) 22610 { 22611 clock_t now; 22612 22613 /* 22614 * TCP needs to protect itself from generating too many RSTs. 22615 * This can be a DoS attack by sending us random segments 22616 * soliciting RSTs. 22617 * 22618 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 22619 * in each 1 second interval. In this way, TCP still generate 22620 * RSTs in normal cases but when under attack, the impact is 22621 * limited. 22622 */ 22623 if (tcps->tcps_rst_sent_rate_enabled != 0) { 22624 now = lbolt; 22625 /* lbolt can wrap around. */ 22626 if ((tcps->tcps_last_rst_intrvl > now) || 22627 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22628 1*SECONDS)) { 22629 tcps->tcps_last_rst_intrvl = now; 22630 tcps->tcps_rst_cnt = 1; 22631 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22632 return (B_FALSE); 22633 } 22634 } 22635 return (B_TRUE); 22636 } 22637 22638 /* 22639 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22640 */ 22641 static void 22642 tcp_ip_ire_mark_advice(tcp_t *tcp) 22643 { 22644 mblk_t *mp; 22645 ipic_t *ipic; 22646 22647 if (tcp->tcp_ipversion == IPV4_VERSION) { 22648 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22649 &ipic); 22650 } else { 22651 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22652 &ipic); 22653 } 22654 if (mp == NULL) 22655 return; 22656 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22657 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22658 } 22659 22660 /* 22661 * Return an IP advice ioctl mblk and set ipic to be the pointer 22662 * to the advice structure. 22663 */ 22664 static mblk_t * 22665 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22666 { 22667 struct iocblk *ioc; 22668 mblk_t *mp, *mp1; 22669 22670 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22671 if (mp == NULL) 22672 return (NULL); 22673 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22674 *ipic = (ipic_t *)mp->b_rptr; 22675 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22676 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22677 22678 bcopy(addr, *ipic + 1, addr_len); 22679 22680 (*ipic)->ipic_addr_length = addr_len; 22681 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22682 22683 mp1 = mkiocb(IP_IOCTL); 22684 if (mp1 == NULL) { 22685 freemsg(mp); 22686 return (NULL); 22687 } 22688 mp1->b_cont = mp; 22689 ioc = (struct iocblk *)mp1->b_rptr; 22690 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22691 22692 return (mp1); 22693 } 22694 22695 /* 22696 * Generate a reset based on an inbound packet, connp is set by caller 22697 * when RST is in response to an unexpected inbound packet for which 22698 * there is active tcp state in the system. 22699 * 22700 * IPSEC NOTE : Try to send the reply with the same protection as it came 22701 * in. We still have the ipsec_mp that the packet was attached to. Thus 22702 * the packet will go out at the same level of protection as it came in by 22703 * converting the IPSEC_IN to IPSEC_OUT. 22704 */ 22705 static void 22706 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22707 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22708 tcp_stack_t *tcps, conn_t *connp) 22709 { 22710 ipha_t *ipha = NULL; 22711 ip6_t *ip6h = NULL; 22712 ushort_t len; 22713 tcph_t *tcph; 22714 int i; 22715 mblk_t *ipsec_mp; 22716 boolean_t mctl_present; 22717 ipic_t *ipic; 22718 ipaddr_t v4addr; 22719 in6_addr_t v6addr; 22720 int addr_len; 22721 void *addr; 22722 queue_t *q = tcps->tcps_g_q; 22723 tcp_t *tcp; 22724 cred_t *cr; 22725 mblk_t *nmp; 22726 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22727 22728 if (tcps->tcps_g_q == NULL) { 22729 /* 22730 * For non-zero stackids the default queue isn't created 22731 * until the first open, thus there can be a need to send 22732 * a reset before then. But we can't do that, hence we just 22733 * drop the packet. Later during boot, when the default queue 22734 * has been setup, a retransmitted packet from the peer 22735 * will result in a reset. 22736 */ 22737 ASSERT(tcps->tcps_netstack->netstack_stackid != 22738 GLOBAL_NETSTACKID); 22739 freemsg(mp); 22740 return; 22741 } 22742 22743 if (connp != NULL) 22744 tcp = connp->conn_tcp; 22745 else 22746 tcp = Q_TO_TCP(q); 22747 22748 if (!tcp_send_rst_chk(tcps)) { 22749 tcps->tcps_rst_unsent++; 22750 freemsg(mp); 22751 return; 22752 } 22753 22754 if (mp->b_datap->db_type == M_CTL) { 22755 ipsec_mp = mp; 22756 mp = mp->b_cont; 22757 mctl_present = B_TRUE; 22758 } else { 22759 ipsec_mp = mp; 22760 mctl_present = B_FALSE; 22761 } 22762 22763 if (str && q && tcps->tcps_dbg) { 22764 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22765 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22766 "flags 0x%x", 22767 str, seq, ack, ctl); 22768 } 22769 if (mp->b_datap->db_ref != 1) { 22770 mblk_t *mp1 = copyb(mp); 22771 freemsg(mp); 22772 mp = mp1; 22773 if (!mp) { 22774 if (mctl_present) 22775 freeb(ipsec_mp); 22776 return; 22777 } else { 22778 if (mctl_present) { 22779 ipsec_mp->b_cont = mp; 22780 } else { 22781 ipsec_mp = mp; 22782 } 22783 } 22784 } else if (mp->b_cont) { 22785 freemsg(mp->b_cont); 22786 mp->b_cont = NULL; 22787 } 22788 /* 22789 * We skip reversing source route here. 22790 * (for now we replace all IP options with EOL) 22791 */ 22792 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22793 ipha = (ipha_t *)mp->b_rptr; 22794 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22795 mp->b_rptr[i] = IPOPT_EOL; 22796 /* 22797 * Make sure that src address isn't flagrantly invalid. 22798 * Not all broadcast address checking for the src address 22799 * is possible, since we don't know the netmask of the src 22800 * addr. No check for destination address is done, since 22801 * IP will not pass up a packet with a broadcast dest 22802 * address to TCP. Similar checks are done below for IPv6. 22803 */ 22804 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22805 CLASSD(ipha->ipha_src)) { 22806 freemsg(ipsec_mp); 22807 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22808 return; 22809 } 22810 } else { 22811 ip6h = (ip6_t *)mp->b_rptr; 22812 22813 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22814 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22815 freemsg(ipsec_mp); 22816 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22817 return; 22818 } 22819 22820 /* Remove any extension headers assuming partial overlay */ 22821 if (ip_hdr_len > IPV6_HDR_LEN) { 22822 uint8_t *to; 22823 22824 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22825 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22826 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22827 ip_hdr_len = IPV6_HDR_LEN; 22828 ip6h = (ip6_t *)mp->b_rptr; 22829 ip6h->ip6_nxt = IPPROTO_TCP; 22830 } 22831 } 22832 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22833 if (tcph->th_flags[0] & TH_RST) { 22834 freemsg(ipsec_mp); 22835 return; 22836 } 22837 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22838 len = ip_hdr_len + sizeof (tcph_t); 22839 mp->b_wptr = &mp->b_rptr[len]; 22840 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22841 ipha->ipha_length = htons(len); 22842 /* Swap addresses */ 22843 v4addr = ipha->ipha_src; 22844 ipha->ipha_src = ipha->ipha_dst; 22845 ipha->ipha_dst = v4addr; 22846 ipha->ipha_ident = 0; 22847 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22848 addr_len = IP_ADDR_LEN; 22849 addr = &v4addr; 22850 } else { 22851 /* No ip6i_t in this case */ 22852 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22853 /* Swap addresses */ 22854 v6addr = ip6h->ip6_src; 22855 ip6h->ip6_src = ip6h->ip6_dst; 22856 ip6h->ip6_dst = v6addr; 22857 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22858 addr_len = IPV6_ADDR_LEN; 22859 addr = &v6addr; 22860 } 22861 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22862 U32_TO_BE32(ack, tcph->th_ack); 22863 U32_TO_BE32(seq, tcph->th_seq); 22864 U16_TO_BE16(0, tcph->th_win); 22865 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22866 tcph->th_flags[0] = (uint8_t)ctl; 22867 if (ctl & TH_RST) { 22868 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22869 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22870 } 22871 22872 /* IP trusts us to set up labels when required. */ 22873 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 22874 crgetlabel(cr) != NULL) { 22875 int err; 22876 22877 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22878 err = tsol_check_label(cr, &mp, 22879 tcp->tcp_connp->conn_mac_exempt, 22880 tcps->tcps_netstack->netstack_ip); 22881 else 22882 err = tsol_check_label_v6(cr, &mp, 22883 tcp->tcp_connp->conn_mac_exempt, 22884 tcps->tcps_netstack->netstack_ip); 22885 if (mctl_present) 22886 ipsec_mp->b_cont = mp; 22887 else 22888 ipsec_mp = mp; 22889 if (err != 0) { 22890 freemsg(ipsec_mp); 22891 return; 22892 } 22893 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22894 ipha = (ipha_t *)mp->b_rptr; 22895 } else { 22896 ip6h = (ip6_t *)mp->b_rptr; 22897 } 22898 } 22899 22900 if (mctl_present) { 22901 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22902 22903 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22904 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22905 return; 22906 } 22907 } 22908 if (zoneid == ALL_ZONES) 22909 zoneid = GLOBAL_ZONEID; 22910 22911 /* Add the zoneid so ip_output routes it properly */ 22912 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22913 freemsg(ipsec_mp); 22914 return; 22915 } 22916 ipsec_mp = nmp; 22917 22918 /* 22919 * NOTE: one might consider tracing a TCP packet here, but 22920 * this function has no active TCP state and no tcp structure 22921 * that has a trace buffer. If we traced here, we would have 22922 * to keep a local trace buffer in tcp_record_trace(). 22923 * 22924 * TSol note: The mblk that contains the incoming packet was 22925 * reused by tcp_xmit_listener_reset, so it already contains 22926 * the right credentials and we don't need to call mblk_setcred. 22927 * Also the conn's cred is not right since it is associated 22928 * with tcps_g_q. 22929 */ 22930 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22931 22932 /* 22933 * Tell IP to mark the IRE used for this destination temporary. 22934 * This way, we can limit our exposure to DoS attack because IP 22935 * creates an IRE for each destination. If there are too many, 22936 * the time to do any routing lookup will be extremely long. And 22937 * the lookup can be in interrupt context. 22938 * 22939 * Note that in normal circumstances, this marking should not 22940 * affect anything. It would be nice if only 1 message is 22941 * needed to inform IP that the IRE created for this RST should 22942 * not be added to the cache table. But there is currently 22943 * not such communication mechanism between TCP and IP. So 22944 * the best we can do now is to send the advice ioctl to IP 22945 * to mark the IRE temporary. 22946 */ 22947 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22948 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22949 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22950 } 22951 } 22952 22953 /* 22954 * Initiate closedown sequence on an active connection. (May be called as 22955 * writer.) Return value zero for OK return, non-zero for error return. 22956 */ 22957 static int 22958 tcp_xmit_end(tcp_t *tcp) 22959 { 22960 ipic_t *ipic; 22961 mblk_t *mp; 22962 tcp_stack_t *tcps = tcp->tcp_tcps; 22963 22964 if (tcp->tcp_state < TCPS_SYN_RCVD || 22965 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22966 /* 22967 * Invalid state, only states TCPS_SYN_RCVD, 22968 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22969 */ 22970 return (-1); 22971 } 22972 22973 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22974 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22975 /* 22976 * If there is nothing more unsent, send the FIN now. 22977 * Otherwise, it will go out with the last segment. 22978 */ 22979 if (tcp->tcp_unsent == 0) { 22980 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22981 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22982 22983 if (mp) { 22984 tcp_send_data(tcp, tcp->tcp_wq, mp); 22985 } else { 22986 /* 22987 * Couldn't allocate msg. Pretend we got it out. 22988 * Wait for rexmit timeout. 22989 */ 22990 tcp->tcp_snxt = tcp->tcp_fss + 1; 22991 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22992 } 22993 22994 /* 22995 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22996 * changed. 22997 */ 22998 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22999 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23000 } 23001 } else { 23002 /* 23003 * If tcp->tcp_cork is set, then the data will not get sent, 23004 * so we have to check that and unset it first. 23005 */ 23006 if (tcp->tcp_cork) 23007 tcp->tcp_cork = B_FALSE; 23008 tcp_wput_data(tcp, NULL, B_FALSE); 23009 } 23010 23011 /* 23012 * If TCP does not get enough samples of RTT or tcp_rtt_updates 23013 * is 0, don't update the cache. 23014 */ 23015 if (tcps->tcps_rtt_updates == 0 || 23016 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 23017 return (0); 23018 23019 /* 23020 * NOTE: should not update if source routes i.e. if tcp_remote if 23021 * different from the destination. 23022 */ 23023 if (tcp->tcp_ipversion == IPV4_VERSION) { 23024 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 23025 return (0); 23026 } 23027 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 23028 &ipic); 23029 } else { 23030 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 23031 &tcp->tcp_ip6h->ip6_dst))) { 23032 return (0); 23033 } 23034 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 23035 &ipic); 23036 } 23037 23038 /* Record route attributes in the IRE for use by future connections. */ 23039 if (mp == NULL) 23040 return (0); 23041 23042 /* 23043 * We do not have a good algorithm to update ssthresh at this time. 23044 * So don't do any update. 23045 */ 23046 ipic->ipic_rtt = tcp->tcp_rtt_sa; 23047 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 23048 23049 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 23050 23051 return (0); 23052 } 23053 23054 /* 23055 * Generate a "no listener here" RST in response to an "unknown" segment. 23056 * connp is set by caller when RST is in response to an unexpected 23057 * inbound packet for which there is active tcp state in the system. 23058 * Note that we are reusing the incoming mp to construct the outgoing RST. 23059 */ 23060 void 23061 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 23062 tcp_stack_t *tcps, conn_t *connp) 23063 { 23064 uchar_t *rptr; 23065 uint32_t seg_len; 23066 tcph_t *tcph; 23067 uint32_t seg_seq; 23068 uint32_t seg_ack; 23069 uint_t flags; 23070 mblk_t *ipsec_mp; 23071 ipha_t *ipha; 23072 ip6_t *ip6h; 23073 boolean_t mctl_present = B_FALSE; 23074 boolean_t check = B_TRUE; 23075 boolean_t policy_present; 23076 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 23077 23078 TCP_STAT(tcps, tcp_no_listener); 23079 23080 ipsec_mp = mp; 23081 23082 if (mp->b_datap->db_type == M_CTL) { 23083 ipsec_in_t *ii; 23084 23085 mctl_present = B_TRUE; 23086 mp = mp->b_cont; 23087 23088 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 23089 ASSERT(ii->ipsec_in_type == IPSEC_IN); 23090 if (ii->ipsec_in_dont_check) { 23091 check = B_FALSE; 23092 if (!ii->ipsec_in_secure) { 23093 freeb(ipsec_mp); 23094 mctl_present = B_FALSE; 23095 ipsec_mp = mp; 23096 } 23097 } 23098 } 23099 23100 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 23101 policy_present = ipss->ipsec_inbound_v4_policy_present; 23102 ipha = (ipha_t *)mp->b_rptr; 23103 ip6h = NULL; 23104 } else { 23105 policy_present = ipss->ipsec_inbound_v6_policy_present; 23106 ipha = NULL; 23107 ip6h = (ip6_t *)mp->b_rptr; 23108 } 23109 23110 if (check && policy_present) { 23111 /* 23112 * The conn_t parameter is NULL because we already know 23113 * nobody's home. 23114 */ 23115 ipsec_mp = ipsec_check_global_policy( 23116 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 23117 tcps->tcps_netstack); 23118 if (ipsec_mp == NULL) 23119 return; 23120 } 23121 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 23122 DTRACE_PROBE2( 23123 tx__ip__log__error__nolistener__tcp, 23124 char *, "Could not reply with RST to mp(1)", 23125 mblk_t *, mp); 23126 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 23127 freemsg(ipsec_mp); 23128 return; 23129 } 23130 23131 rptr = mp->b_rptr; 23132 23133 tcph = (tcph_t *)&rptr[ip_hdr_len]; 23134 seg_seq = BE32_TO_U32(tcph->th_seq); 23135 seg_ack = BE32_TO_U32(tcph->th_ack); 23136 flags = tcph->th_flags[0]; 23137 23138 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 23139 if (flags & TH_RST) { 23140 freemsg(ipsec_mp); 23141 } else if (flags & TH_ACK) { 23142 tcp_xmit_early_reset("no tcp, reset", 23143 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 23144 connp); 23145 } else { 23146 if (flags & TH_SYN) { 23147 seg_len++; 23148 } else { 23149 /* 23150 * Here we violate the RFC. Note that a normal 23151 * TCP will never send a segment without the ACK 23152 * flag, except for RST or SYN segment. This 23153 * segment is neither. Just drop it on the 23154 * floor. 23155 */ 23156 freemsg(ipsec_mp); 23157 tcps->tcps_rst_unsent++; 23158 return; 23159 } 23160 23161 tcp_xmit_early_reset("no tcp, reset/ack", 23162 ipsec_mp, 0, seg_seq + seg_len, 23163 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 23164 } 23165 } 23166 23167 /* 23168 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 23169 * ip and tcp header ready to pass down to IP. If the mp passed in is 23170 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 23171 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 23172 * otherwise it will dup partial mblks.) 23173 * Otherwise, an appropriate ACK packet will be generated. This 23174 * routine is not usually called to send new data for the first time. It 23175 * is mostly called out of the timer for retransmits, and to generate ACKs. 23176 * 23177 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 23178 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 23179 * of the original mblk chain will be returned in *offset and *end_mp. 23180 */ 23181 mblk_t * 23182 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 23183 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 23184 boolean_t rexmit) 23185 { 23186 int data_length; 23187 int32_t off = 0; 23188 uint_t flags; 23189 mblk_t *mp1; 23190 mblk_t *mp2; 23191 uchar_t *rptr; 23192 tcph_t *tcph; 23193 int32_t num_sack_blk = 0; 23194 int32_t sack_opt_len = 0; 23195 tcp_stack_t *tcps = tcp->tcp_tcps; 23196 23197 /* Allocate for our maximum TCP header + link-level */ 23198 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 23199 tcps->tcps_wroff_xtra, BPRI_MED); 23200 if (!mp1) 23201 return (NULL); 23202 data_length = 0; 23203 23204 /* 23205 * Note that tcp_mss has been adjusted to take into account the 23206 * timestamp option if applicable. Because SACK options do not 23207 * appear in every TCP segments and they are of variable lengths, 23208 * they cannot be included in tcp_mss. Thus we need to calculate 23209 * the actual segment length when we need to send a segment which 23210 * includes SACK options. 23211 */ 23212 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23213 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23214 tcp->tcp_num_sack_blk); 23215 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23216 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23217 if (max_to_send + sack_opt_len > tcp->tcp_mss) 23218 max_to_send -= sack_opt_len; 23219 } 23220 23221 if (offset != NULL) { 23222 off = *offset; 23223 /* We use offset as an indicator that end_mp is not NULL. */ 23224 *end_mp = NULL; 23225 } 23226 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 23227 /* This could be faster with cooperation from downstream */ 23228 if (mp2 != mp1 && !sendall && 23229 data_length + (int)(mp->b_wptr - mp->b_rptr) > 23230 max_to_send) 23231 /* 23232 * Don't send the next mblk since the whole mblk 23233 * does not fit. 23234 */ 23235 break; 23236 mp2->b_cont = dupb(mp); 23237 mp2 = mp2->b_cont; 23238 if (!mp2) { 23239 freemsg(mp1); 23240 return (NULL); 23241 } 23242 mp2->b_rptr += off; 23243 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 23244 (uintptr_t)INT_MAX); 23245 23246 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 23247 if (data_length > max_to_send) { 23248 mp2->b_wptr -= data_length - max_to_send; 23249 data_length = max_to_send; 23250 off = mp2->b_wptr - mp->b_rptr; 23251 break; 23252 } else { 23253 off = 0; 23254 } 23255 } 23256 if (offset != NULL) { 23257 *offset = off; 23258 *end_mp = mp; 23259 } 23260 if (seg_len != NULL) { 23261 *seg_len = data_length; 23262 } 23263 23264 /* Update the latest receive window size in TCP header. */ 23265 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23266 tcp->tcp_tcph->th_win); 23267 23268 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23269 mp1->b_rptr = rptr; 23270 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 23271 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23272 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23273 U32_TO_ABE32(seq, tcph->th_seq); 23274 23275 /* 23276 * Use tcp_unsent to determine if the PUSH bit should be used assumes 23277 * that this function was called from tcp_wput_data. Thus, when called 23278 * to retransmit data the setting of the PUSH bit may appear some 23279 * what random in that it might get set when it should not. This 23280 * should not pose any performance issues. 23281 */ 23282 if (data_length != 0 && (tcp->tcp_unsent == 0 || 23283 tcp->tcp_unsent == data_length)) { 23284 flags = TH_ACK | TH_PUSH; 23285 } else { 23286 flags = TH_ACK; 23287 } 23288 23289 if (tcp->tcp_ecn_ok) { 23290 if (tcp->tcp_ecn_echo_on) 23291 flags |= TH_ECE; 23292 23293 /* 23294 * Only set ECT bit and ECN_CWR if a segment contains new data. 23295 * There is no TCP flow control for non-data segments, and 23296 * only data segment is transmitted reliably. 23297 */ 23298 if (data_length > 0 && !rexmit) { 23299 SET_ECT(tcp, rptr); 23300 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 23301 flags |= TH_CWR; 23302 tcp->tcp_ecn_cwr_sent = B_TRUE; 23303 } 23304 } 23305 } 23306 23307 if (tcp->tcp_valid_bits) { 23308 uint32_t u1; 23309 23310 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 23311 seq == tcp->tcp_iss) { 23312 uchar_t *wptr; 23313 23314 /* 23315 * If TCP_ISS_VALID and the seq number is tcp_iss, 23316 * TCP can only be in SYN-SENT, SYN-RCVD or 23317 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 23318 * our SYN is not ack'ed but the app closes this 23319 * TCP connection. 23320 */ 23321 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 23322 tcp->tcp_state == TCPS_SYN_RCVD || 23323 tcp->tcp_state == TCPS_FIN_WAIT_1); 23324 23325 /* 23326 * Tack on the MSS option. It is always needed 23327 * for both active and passive open. 23328 * 23329 * MSS option value should be interface MTU - MIN 23330 * TCP/IP header according to RFC 793 as it means 23331 * the maximum segment size TCP can receive. But 23332 * to get around some broken middle boxes/end hosts 23333 * out there, we allow the option value to be the 23334 * same as the MSS option size on the peer side. 23335 * In this way, the other side will not send 23336 * anything larger than they can receive. 23337 * 23338 * Note that for SYN_SENT state, the ndd param 23339 * tcp_use_smss_as_mss_opt has no effect as we 23340 * don't know the peer's MSS option value. So 23341 * the only case we need to take care of is in 23342 * SYN_RCVD state, which is done later. 23343 */ 23344 wptr = mp1->b_wptr; 23345 wptr[0] = TCPOPT_MAXSEG; 23346 wptr[1] = TCPOPT_MAXSEG_LEN; 23347 wptr += 2; 23348 u1 = tcp->tcp_if_mtu - 23349 (tcp->tcp_ipversion == IPV4_VERSION ? 23350 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 23351 TCP_MIN_HEADER_LENGTH; 23352 U16_TO_BE16(u1, wptr); 23353 mp1->b_wptr = wptr + 2; 23354 /* Update the offset to cover the additional word */ 23355 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23356 23357 /* 23358 * Note that the following way of filling in 23359 * TCP options are not optimal. Some NOPs can 23360 * be saved. But there is no need at this time 23361 * to optimize it. When it is needed, we will 23362 * do it. 23363 */ 23364 switch (tcp->tcp_state) { 23365 case TCPS_SYN_SENT: 23366 flags = TH_SYN; 23367 23368 if (tcp->tcp_snd_ts_ok) { 23369 uint32_t llbolt = (uint32_t)lbolt; 23370 23371 wptr = mp1->b_wptr; 23372 wptr[0] = TCPOPT_NOP; 23373 wptr[1] = TCPOPT_NOP; 23374 wptr[2] = TCPOPT_TSTAMP; 23375 wptr[3] = TCPOPT_TSTAMP_LEN; 23376 wptr += 4; 23377 U32_TO_BE32(llbolt, wptr); 23378 wptr += 4; 23379 ASSERT(tcp->tcp_ts_recent == 0); 23380 U32_TO_BE32(0L, wptr); 23381 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 23382 tcph->th_offset_and_rsrvd[0] += 23383 (3 << 4); 23384 } 23385 23386 /* 23387 * Set up all the bits to tell other side 23388 * we are ECN capable. 23389 */ 23390 if (tcp->tcp_ecn_ok) { 23391 flags |= (TH_ECE | TH_CWR); 23392 } 23393 break; 23394 case TCPS_SYN_RCVD: 23395 flags |= TH_SYN; 23396 23397 /* 23398 * Reset the MSS option value to be SMSS 23399 * We should probably add back the bytes 23400 * for timestamp option and IPsec. We 23401 * don't do that as this is a workaround 23402 * for broken middle boxes/end hosts, it 23403 * is better for us to be more cautious. 23404 * They may not take these things into 23405 * account in their SMSS calculation. Thus 23406 * the peer's calculated SMSS may be smaller 23407 * than what it can be. This should be OK. 23408 */ 23409 if (tcps->tcps_use_smss_as_mss_opt) { 23410 u1 = tcp->tcp_mss; 23411 U16_TO_BE16(u1, wptr); 23412 } 23413 23414 /* 23415 * If the other side is ECN capable, reply 23416 * that we are also ECN capable. 23417 */ 23418 if (tcp->tcp_ecn_ok) 23419 flags |= TH_ECE; 23420 break; 23421 default: 23422 /* 23423 * The above ASSERT() makes sure that this 23424 * must be FIN-WAIT-1 state. Our SYN has 23425 * not been ack'ed so retransmit it. 23426 */ 23427 flags |= TH_SYN; 23428 break; 23429 } 23430 23431 if (tcp->tcp_snd_ws_ok) { 23432 wptr = mp1->b_wptr; 23433 wptr[0] = TCPOPT_NOP; 23434 wptr[1] = TCPOPT_WSCALE; 23435 wptr[2] = TCPOPT_WS_LEN; 23436 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 23437 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 23438 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23439 } 23440 23441 if (tcp->tcp_snd_sack_ok) { 23442 wptr = mp1->b_wptr; 23443 wptr[0] = TCPOPT_NOP; 23444 wptr[1] = TCPOPT_NOP; 23445 wptr[2] = TCPOPT_SACK_PERMITTED; 23446 wptr[3] = TCPOPT_SACK_OK_LEN; 23447 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 23448 tcph->th_offset_and_rsrvd[0] += (1 << 4); 23449 } 23450 23451 /* allocb() of adequate mblk assures space */ 23452 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 23453 (uintptr_t)INT_MAX); 23454 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 23455 /* 23456 * Get IP set to checksum on our behalf 23457 * Include the adjustment for a source route if any. 23458 */ 23459 u1 += tcp->tcp_sum; 23460 u1 = (u1 >> 16) + (u1 & 0xFFFF); 23461 U16_TO_BE16(u1, tcph->th_sum); 23462 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23463 } 23464 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 23465 (seq + data_length) == tcp->tcp_fss) { 23466 if (!tcp->tcp_fin_acked) { 23467 flags |= TH_FIN; 23468 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 23469 } 23470 if (!tcp->tcp_fin_sent) { 23471 tcp->tcp_fin_sent = B_TRUE; 23472 switch (tcp->tcp_state) { 23473 case TCPS_SYN_RCVD: 23474 case TCPS_ESTABLISHED: 23475 tcp->tcp_state = TCPS_FIN_WAIT_1; 23476 break; 23477 case TCPS_CLOSE_WAIT: 23478 tcp->tcp_state = TCPS_LAST_ACK; 23479 break; 23480 } 23481 if (tcp->tcp_suna == tcp->tcp_snxt) 23482 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 23483 tcp->tcp_snxt = tcp->tcp_fss + 1; 23484 } 23485 } 23486 /* 23487 * Note the trick here. u1 is unsigned. When tcp_urg 23488 * is smaller than seq, u1 will become a very huge value. 23489 * So the comparison will fail. Also note that tcp_urp 23490 * should be positive, see RFC 793 page 17. 23491 */ 23492 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 23493 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 23494 u1 < (uint32_t)(64 * 1024)) { 23495 flags |= TH_URG; 23496 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 23497 U32_TO_ABE16(u1, tcph->th_urp); 23498 } 23499 } 23500 tcph->th_flags[0] = (uchar_t)flags; 23501 tcp->tcp_rack = tcp->tcp_rnxt; 23502 tcp->tcp_rack_cnt = 0; 23503 23504 if (tcp->tcp_snd_ts_ok) { 23505 if (tcp->tcp_state != TCPS_SYN_SENT) { 23506 uint32_t llbolt = (uint32_t)lbolt; 23507 23508 U32_TO_BE32(llbolt, 23509 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23510 U32_TO_BE32(tcp->tcp_ts_recent, 23511 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23512 } 23513 } 23514 23515 if (num_sack_blk > 0) { 23516 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23517 sack_blk_t *tmp; 23518 int32_t i; 23519 23520 wptr[0] = TCPOPT_NOP; 23521 wptr[1] = TCPOPT_NOP; 23522 wptr[2] = TCPOPT_SACK; 23523 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23524 sizeof (sack_blk_t); 23525 wptr += TCPOPT_REAL_SACK_LEN; 23526 23527 tmp = tcp->tcp_sack_list; 23528 for (i = 0; i < num_sack_blk; i++) { 23529 U32_TO_BE32(tmp[i].begin, wptr); 23530 wptr += sizeof (tcp_seq); 23531 U32_TO_BE32(tmp[i].end, wptr); 23532 wptr += sizeof (tcp_seq); 23533 } 23534 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 23535 } 23536 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 23537 data_length += (int)(mp1->b_wptr - rptr); 23538 if (tcp->tcp_ipversion == IPV4_VERSION) { 23539 ((ipha_t *)rptr)->ipha_length = htons(data_length); 23540 } else { 23541 ip6_t *ip6 = (ip6_t *)(rptr + 23542 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23543 sizeof (ip6i_t) : 0)); 23544 23545 ip6->ip6_plen = htons(data_length - 23546 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23547 } 23548 23549 /* 23550 * Prime pump for IP 23551 * Include the adjustment for a source route if any. 23552 */ 23553 data_length -= tcp->tcp_ip_hdr_len; 23554 data_length += tcp->tcp_sum; 23555 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23556 U16_TO_ABE16(data_length, tcph->th_sum); 23557 if (tcp->tcp_ip_forward_progress) { 23558 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23559 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23560 tcp->tcp_ip_forward_progress = B_FALSE; 23561 } 23562 return (mp1); 23563 } 23564 23565 /* This function handles the push timeout. */ 23566 void 23567 tcp_push_timer(void *arg) 23568 { 23569 conn_t *connp = (conn_t *)arg; 23570 tcp_t *tcp = connp->conn_tcp; 23571 uint_t flags; 23572 sodirect_t *sodp; 23573 23574 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 23575 23576 ASSERT(tcp->tcp_listener == NULL); 23577 23578 ASSERT(!IPCL_IS_NONSTR(connp)); 23579 23580 /* 23581 * We need to plug synchronous streams during our drain to prevent 23582 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 23583 */ 23584 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 23585 tcp->tcp_push_tid = 0; 23586 23587 SOD_PTR_ENTER(tcp, sodp); 23588 if (sodp != NULL) { 23589 flags = tcp_rcv_sod_wakeup(tcp, sodp); 23590 /* sod_wakeup() does the mutex_exit() */ 23591 } else if (tcp->tcp_rcv_list != NULL) { 23592 flags = tcp_rcv_drain(tcp); 23593 } 23594 if (flags == TH_ACK_NEEDED) 23595 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 23596 23597 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 23598 } 23599 23600 /* 23601 * This function handles delayed ACK timeout. 23602 */ 23603 static void 23604 tcp_ack_timer(void *arg) 23605 { 23606 conn_t *connp = (conn_t *)arg; 23607 tcp_t *tcp = connp->conn_tcp; 23608 mblk_t *mp; 23609 tcp_stack_t *tcps = tcp->tcp_tcps; 23610 23611 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23612 23613 tcp->tcp_ack_tid = 0; 23614 23615 if (tcp->tcp_fused) 23616 return; 23617 23618 /* 23619 * Do not send ACK if there is no outstanding unack'ed data. 23620 */ 23621 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23622 return; 23623 } 23624 23625 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23626 /* 23627 * Make sure we don't allow deferred ACKs to result in 23628 * timer-based ACKing. If we have held off an ACK 23629 * when there was more than an mss here, and the timer 23630 * goes off, we have to worry about the possibility 23631 * that the sender isn't doing slow-start, or is out 23632 * of step with us for some other reason. We fall 23633 * permanently back in the direction of 23634 * ACK-every-other-packet as suggested in RFC 1122. 23635 */ 23636 if (tcp->tcp_rack_abs_max > 2) 23637 tcp->tcp_rack_abs_max--; 23638 tcp->tcp_rack_cur_max = 2; 23639 } 23640 mp = tcp_ack_mp(tcp); 23641 23642 if (mp != NULL) { 23643 BUMP_LOCAL(tcp->tcp_obsegs); 23644 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23645 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23646 tcp_send_data(tcp, tcp->tcp_wq, mp); 23647 } 23648 } 23649 23650 23651 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23652 static mblk_t * 23653 tcp_ack_mp(tcp_t *tcp) 23654 { 23655 uint32_t seq_no; 23656 tcp_stack_t *tcps = tcp->tcp_tcps; 23657 23658 /* 23659 * There are a few cases to be considered while setting the sequence no. 23660 * Essentially, we can come here while processing an unacceptable pkt 23661 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23662 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23663 * If we are here for a zero window probe, stick with suna. In all 23664 * other cases, we check if suna + swnd encompasses snxt and set 23665 * the sequence number to snxt, if so. If snxt falls outside the 23666 * window (the receiver probably shrunk its window), we will go with 23667 * suna + swnd, otherwise the sequence no will be unacceptable to the 23668 * receiver. 23669 */ 23670 if (tcp->tcp_zero_win_probe) { 23671 seq_no = tcp->tcp_suna; 23672 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23673 ASSERT(tcp->tcp_swnd == 0); 23674 seq_no = tcp->tcp_snxt; 23675 } else { 23676 seq_no = SEQ_GT(tcp->tcp_snxt, 23677 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23678 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23679 } 23680 23681 if (tcp->tcp_valid_bits) { 23682 /* 23683 * For the complex case where we have to send some 23684 * controls (FIN or SYN), let tcp_xmit_mp do it. 23685 */ 23686 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23687 NULL, B_FALSE)); 23688 } else { 23689 /* Generate a simple ACK */ 23690 int data_length; 23691 uchar_t *rptr; 23692 tcph_t *tcph; 23693 mblk_t *mp1; 23694 int32_t tcp_hdr_len; 23695 int32_t tcp_tcp_hdr_len; 23696 int32_t num_sack_blk = 0; 23697 int32_t sack_opt_len; 23698 23699 /* 23700 * Allocate space for TCP + IP headers 23701 * and link-level header 23702 */ 23703 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23704 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23705 tcp->tcp_num_sack_blk); 23706 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23707 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23708 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23709 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23710 } else { 23711 tcp_hdr_len = tcp->tcp_hdr_len; 23712 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23713 } 23714 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23715 if (!mp1) 23716 return (NULL); 23717 23718 /* Update the latest receive window size in TCP header. */ 23719 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23720 tcp->tcp_tcph->th_win); 23721 /* copy in prototype TCP + IP header */ 23722 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23723 mp1->b_rptr = rptr; 23724 mp1->b_wptr = rptr + tcp_hdr_len; 23725 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23726 23727 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23728 23729 /* Set the TCP sequence number. */ 23730 U32_TO_ABE32(seq_no, tcph->th_seq); 23731 23732 /* Set up the TCP flag field. */ 23733 tcph->th_flags[0] = (uchar_t)TH_ACK; 23734 if (tcp->tcp_ecn_echo_on) 23735 tcph->th_flags[0] |= TH_ECE; 23736 23737 tcp->tcp_rack = tcp->tcp_rnxt; 23738 tcp->tcp_rack_cnt = 0; 23739 23740 /* fill in timestamp option if in use */ 23741 if (tcp->tcp_snd_ts_ok) { 23742 uint32_t llbolt = (uint32_t)lbolt; 23743 23744 U32_TO_BE32(llbolt, 23745 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23746 U32_TO_BE32(tcp->tcp_ts_recent, 23747 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23748 } 23749 23750 /* Fill in SACK options */ 23751 if (num_sack_blk > 0) { 23752 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23753 sack_blk_t *tmp; 23754 int32_t i; 23755 23756 wptr[0] = TCPOPT_NOP; 23757 wptr[1] = TCPOPT_NOP; 23758 wptr[2] = TCPOPT_SACK; 23759 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23760 sizeof (sack_blk_t); 23761 wptr += TCPOPT_REAL_SACK_LEN; 23762 23763 tmp = tcp->tcp_sack_list; 23764 for (i = 0; i < num_sack_blk; i++) { 23765 U32_TO_BE32(tmp[i].begin, wptr); 23766 wptr += sizeof (tcp_seq); 23767 U32_TO_BE32(tmp[i].end, wptr); 23768 wptr += sizeof (tcp_seq); 23769 } 23770 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23771 << 4); 23772 } 23773 23774 if (tcp->tcp_ipversion == IPV4_VERSION) { 23775 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23776 } else { 23777 /* Check for ip6i_t header in sticky hdrs */ 23778 ip6_t *ip6 = (ip6_t *)(rptr + 23779 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23780 sizeof (ip6i_t) : 0)); 23781 23782 ip6->ip6_plen = htons(tcp_hdr_len - 23783 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23784 } 23785 23786 /* 23787 * Prime pump for checksum calculation in IP. Include the 23788 * adjustment for a source route if any. 23789 */ 23790 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23791 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23792 U16_TO_ABE16(data_length, tcph->th_sum); 23793 23794 if (tcp->tcp_ip_forward_progress) { 23795 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23796 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23797 tcp->tcp_ip_forward_progress = B_FALSE; 23798 } 23799 return (mp1); 23800 } 23801 } 23802 23803 /* 23804 * Hash list insertion routine for tcp_t structures. Each hash bucket 23805 * contains a list of tcp_t entries, and each entry is bound to a unique 23806 * port. If there are multiple tcp_t's that are bound to the same port, then 23807 * one of them will be linked into the hash bucket list, and the rest will 23808 * hang off of that one entry. For each port, entries bound to a specific IP 23809 * address will be inserted before those those bound to INADDR_ANY. 23810 */ 23811 static void 23812 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23813 { 23814 tcp_t **tcpp; 23815 tcp_t *tcpnext; 23816 tcp_t *tcphash; 23817 23818 if (tcp->tcp_ptpbhn != NULL) { 23819 ASSERT(!caller_holds_lock); 23820 tcp_bind_hash_remove(tcp); 23821 } 23822 tcpp = &tbf->tf_tcp; 23823 if (!caller_holds_lock) { 23824 mutex_enter(&tbf->tf_lock); 23825 } else { 23826 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23827 } 23828 tcphash = tcpp[0]; 23829 tcpnext = NULL; 23830 if (tcphash != NULL) { 23831 /* Look for an entry using the same port */ 23832 while ((tcphash = tcpp[0]) != NULL && 23833 tcp->tcp_lport != tcphash->tcp_lport) 23834 tcpp = &(tcphash->tcp_bind_hash); 23835 23836 /* The port was not found, just add to the end */ 23837 if (tcphash == NULL) 23838 goto insert; 23839 23840 /* 23841 * OK, there already exists an entry bound to the 23842 * same port. 23843 * 23844 * If the new tcp bound to the INADDR_ANY address 23845 * and the first one in the list is not bound to 23846 * INADDR_ANY we skip all entries until we find the 23847 * first one bound to INADDR_ANY. 23848 * This makes sure that applications binding to a 23849 * specific address get preference over those binding to 23850 * INADDR_ANY. 23851 */ 23852 tcpnext = tcphash; 23853 tcphash = NULL; 23854 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23855 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23856 while ((tcpnext = tcpp[0]) != NULL && 23857 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23858 tcpp = &(tcpnext->tcp_bind_hash_port); 23859 23860 if (tcpnext) { 23861 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23862 tcphash = tcpnext->tcp_bind_hash; 23863 if (tcphash != NULL) { 23864 tcphash->tcp_ptpbhn = 23865 &(tcp->tcp_bind_hash); 23866 tcpnext->tcp_bind_hash = NULL; 23867 } 23868 } 23869 } else { 23870 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23871 tcphash = tcpnext->tcp_bind_hash; 23872 if (tcphash != NULL) { 23873 tcphash->tcp_ptpbhn = 23874 &(tcp->tcp_bind_hash); 23875 tcpnext->tcp_bind_hash = NULL; 23876 } 23877 } 23878 } 23879 insert: 23880 tcp->tcp_bind_hash_port = tcpnext; 23881 tcp->tcp_bind_hash = tcphash; 23882 tcp->tcp_ptpbhn = tcpp; 23883 tcpp[0] = tcp; 23884 if (!caller_holds_lock) 23885 mutex_exit(&tbf->tf_lock); 23886 } 23887 23888 /* 23889 * Hash list removal routine for tcp_t structures. 23890 */ 23891 static void 23892 tcp_bind_hash_remove(tcp_t *tcp) 23893 { 23894 tcp_t *tcpnext; 23895 kmutex_t *lockp; 23896 tcp_stack_t *tcps = tcp->tcp_tcps; 23897 23898 if (tcp->tcp_ptpbhn == NULL) 23899 return; 23900 23901 /* 23902 * Extract the lock pointer in case there are concurrent 23903 * hash_remove's for this instance. 23904 */ 23905 ASSERT(tcp->tcp_lport != 0); 23906 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23907 23908 ASSERT(lockp != NULL); 23909 mutex_enter(lockp); 23910 if (tcp->tcp_ptpbhn) { 23911 tcpnext = tcp->tcp_bind_hash_port; 23912 if (tcpnext != NULL) { 23913 tcp->tcp_bind_hash_port = NULL; 23914 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23915 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23916 if (tcpnext->tcp_bind_hash != NULL) { 23917 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23918 &(tcpnext->tcp_bind_hash); 23919 tcp->tcp_bind_hash = NULL; 23920 } 23921 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23922 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23923 tcp->tcp_bind_hash = NULL; 23924 } 23925 *tcp->tcp_ptpbhn = tcpnext; 23926 tcp->tcp_ptpbhn = NULL; 23927 } 23928 mutex_exit(lockp); 23929 } 23930 23931 23932 /* 23933 * Hash list lookup routine for tcp_t structures. 23934 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23935 */ 23936 static tcp_t * 23937 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23938 { 23939 tf_t *tf; 23940 tcp_t *tcp; 23941 23942 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23943 mutex_enter(&tf->tf_lock); 23944 for (tcp = tf->tf_tcp; tcp != NULL; 23945 tcp = tcp->tcp_acceptor_hash) { 23946 if (tcp->tcp_acceptor_id == id) { 23947 CONN_INC_REF(tcp->tcp_connp); 23948 mutex_exit(&tf->tf_lock); 23949 return (tcp); 23950 } 23951 } 23952 mutex_exit(&tf->tf_lock); 23953 return (NULL); 23954 } 23955 23956 23957 /* 23958 * Hash list insertion routine for tcp_t structures. 23959 */ 23960 void 23961 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23962 { 23963 tf_t *tf; 23964 tcp_t **tcpp; 23965 tcp_t *tcpnext; 23966 tcp_stack_t *tcps = tcp->tcp_tcps; 23967 23968 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23969 23970 if (tcp->tcp_ptpahn != NULL) 23971 tcp_acceptor_hash_remove(tcp); 23972 tcpp = &tf->tf_tcp; 23973 mutex_enter(&tf->tf_lock); 23974 tcpnext = tcpp[0]; 23975 if (tcpnext) 23976 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23977 tcp->tcp_acceptor_hash = tcpnext; 23978 tcp->tcp_ptpahn = tcpp; 23979 tcpp[0] = tcp; 23980 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23981 mutex_exit(&tf->tf_lock); 23982 } 23983 23984 /* 23985 * Hash list removal routine for tcp_t structures. 23986 */ 23987 static void 23988 tcp_acceptor_hash_remove(tcp_t *tcp) 23989 { 23990 tcp_t *tcpnext; 23991 kmutex_t *lockp; 23992 23993 /* 23994 * Extract the lock pointer in case there are concurrent 23995 * hash_remove's for this instance. 23996 */ 23997 lockp = tcp->tcp_acceptor_lockp; 23998 23999 if (tcp->tcp_ptpahn == NULL) 24000 return; 24001 24002 ASSERT(lockp != NULL); 24003 mutex_enter(lockp); 24004 if (tcp->tcp_ptpahn) { 24005 tcpnext = tcp->tcp_acceptor_hash; 24006 if (tcpnext) { 24007 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 24008 tcp->tcp_acceptor_hash = NULL; 24009 } 24010 *tcp->tcp_ptpahn = tcpnext; 24011 tcp->tcp_ptpahn = NULL; 24012 } 24013 mutex_exit(lockp); 24014 tcp->tcp_acceptor_lockp = NULL; 24015 } 24016 24017 /* Data for fast netmask macro used by tcp_hsp_lookup */ 24018 24019 static ipaddr_t netmasks[] = { 24020 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 24021 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 24022 }; 24023 24024 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 24025 24026 /* 24027 * XXX This routine should go away and instead we should use the metrics 24028 * associated with the routes to determine the default sndspace and rcvspace. 24029 */ 24030 static tcp_hsp_t * 24031 tcp_hsp_lookup(ipaddr_t addr, tcp_stack_t *tcps) 24032 { 24033 tcp_hsp_t *hsp = NULL; 24034 24035 /* Quick check without acquiring the lock. */ 24036 if (tcps->tcps_hsp_hash == NULL) 24037 return (NULL); 24038 24039 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24040 24041 /* This routine finds the best-matching HSP for address addr. */ 24042 24043 if (tcps->tcps_hsp_hash) { 24044 int i; 24045 ipaddr_t srchaddr; 24046 tcp_hsp_t *hsp_net; 24047 24048 /* We do three passes: host, network, and subnet. */ 24049 24050 srchaddr = addr; 24051 24052 for (i = 1; i <= 3; i++) { 24053 /* Look for exact match on srchaddr */ 24054 24055 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH(srchaddr)]; 24056 while (hsp) { 24057 if (hsp->tcp_hsp_vers == IPV4_VERSION && 24058 hsp->tcp_hsp_addr == srchaddr) 24059 break; 24060 hsp = hsp->tcp_hsp_next; 24061 } 24062 ASSERT(hsp == NULL || 24063 hsp->tcp_hsp_vers == IPV4_VERSION); 24064 24065 /* 24066 * If this is the first pass: 24067 * If we found a match, great, return it. 24068 * If not, search for the network on the second pass. 24069 */ 24070 24071 if (i == 1) 24072 if (hsp) 24073 break; 24074 else 24075 { 24076 srchaddr = addr & netmask(addr); 24077 continue; 24078 } 24079 24080 /* 24081 * If this is the second pass: 24082 * If we found a match, but there's a subnet mask, 24083 * save the match but try again using the subnet 24084 * mask on the third pass. 24085 * Otherwise, return whatever we found. 24086 */ 24087 24088 if (i == 2) { 24089 if (hsp && hsp->tcp_hsp_subnet) { 24090 hsp_net = hsp; 24091 srchaddr = addr & hsp->tcp_hsp_subnet; 24092 continue; 24093 } else { 24094 break; 24095 } 24096 } 24097 24098 /* 24099 * This must be the third pass. If we didn't find 24100 * anything, return the saved network HSP instead. 24101 */ 24102 24103 if (!hsp) 24104 hsp = hsp_net; 24105 } 24106 } 24107 24108 rw_exit(&tcps->tcps_hsp_lock); 24109 return (hsp); 24110 } 24111 24112 /* 24113 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 24114 * match lookup. 24115 */ 24116 static tcp_hsp_t * 24117 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr, tcp_stack_t *tcps) 24118 { 24119 tcp_hsp_t *hsp = NULL; 24120 24121 /* Quick check without acquiring the lock. */ 24122 if (tcps->tcps_hsp_hash == NULL) 24123 return (NULL); 24124 24125 rw_enter(&tcps->tcps_hsp_lock, RW_READER); 24126 24127 /* This routine finds the best-matching HSP for address addr. */ 24128 24129 if (tcps->tcps_hsp_hash) { 24130 int i; 24131 in6_addr_t v6srchaddr; 24132 tcp_hsp_t *hsp_net; 24133 24134 /* We do three passes: host, network, and subnet. */ 24135 24136 v6srchaddr = *v6addr; 24137 24138 for (i = 1; i <= 3; i++) { 24139 /* Look for exact match on srchaddr */ 24140 24141 hsp = tcps->tcps_hsp_hash[TCP_HSP_HASH( 24142 V4_PART_OF_V6(v6srchaddr))]; 24143 while (hsp) { 24144 if (hsp->tcp_hsp_vers == IPV6_VERSION && 24145 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 24146 &v6srchaddr)) 24147 break; 24148 hsp = hsp->tcp_hsp_next; 24149 } 24150 24151 /* 24152 * If this is the first pass: 24153 * If we found a match, great, return it. 24154 * If not, search for the network on the second pass. 24155 */ 24156 24157 if (i == 1) 24158 if (hsp) 24159 break; 24160 else { 24161 /* Assume a 64 bit mask */ 24162 v6srchaddr.s6_addr32[0] = 24163 v6addr->s6_addr32[0]; 24164 v6srchaddr.s6_addr32[1] = 24165 v6addr->s6_addr32[1]; 24166 v6srchaddr.s6_addr32[2] = 0; 24167 v6srchaddr.s6_addr32[3] = 0; 24168 continue; 24169 } 24170 24171 /* 24172 * If this is the second pass: 24173 * If we found a match, but there's a subnet mask, 24174 * save the match but try again using the subnet 24175 * mask on the third pass. 24176 * Otherwise, return whatever we found. 24177 */ 24178 24179 if (i == 2) { 24180 ASSERT(hsp == NULL || 24181 hsp->tcp_hsp_vers == IPV6_VERSION); 24182 if (hsp && 24183 !IN6_IS_ADDR_UNSPECIFIED( 24184 &hsp->tcp_hsp_subnet_v6)) { 24185 hsp_net = hsp; 24186 V6_MASK_COPY(*v6addr, 24187 hsp->tcp_hsp_subnet_v6, v6srchaddr); 24188 continue; 24189 } else { 24190 break; 24191 } 24192 } 24193 24194 /* 24195 * This must be the third pass. If we didn't find 24196 * anything, return the saved network HSP instead. 24197 */ 24198 24199 if (!hsp) 24200 hsp = hsp_net; 24201 } 24202 } 24203 24204 rw_exit(&tcps->tcps_hsp_lock); 24205 return (hsp); 24206 } 24207 24208 /* 24209 * Type three generator adapted from the random() function in 4.4 BSD: 24210 */ 24211 24212 /* 24213 * Copyright (c) 1983, 1993 24214 * The Regents of the University of California. All rights reserved. 24215 * 24216 * Redistribution and use in source and binary forms, with or without 24217 * modification, are permitted provided that the following conditions 24218 * are met: 24219 * 1. Redistributions of source code must retain the above copyright 24220 * notice, this list of conditions and the following disclaimer. 24221 * 2. Redistributions in binary form must reproduce the above copyright 24222 * notice, this list of conditions and the following disclaimer in the 24223 * documentation and/or other materials provided with the distribution. 24224 * 3. All advertising materials mentioning features or use of this software 24225 * must display the following acknowledgement: 24226 * This product includes software developed by the University of 24227 * California, Berkeley and its contributors. 24228 * 4. Neither the name of the University nor the names of its contributors 24229 * may be used to endorse or promote products derived from this software 24230 * without specific prior written permission. 24231 * 24232 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24233 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24234 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24235 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24236 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24237 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24238 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24239 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24240 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24241 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24242 * SUCH DAMAGE. 24243 */ 24244 24245 /* Type 3 -- x**31 + x**3 + 1 */ 24246 #define DEG_3 31 24247 #define SEP_3 3 24248 24249 24250 /* Protected by tcp_random_lock */ 24251 static int tcp_randtbl[DEG_3 + 1]; 24252 24253 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 24254 static int *tcp_random_rptr = &tcp_randtbl[1]; 24255 24256 static int *tcp_random_state = &tcp_randtbl[1]; 24257 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 24258 24259 kmutex_t tcp_random_lock; 24260 24261 void 24262 tcp_random_init(void) 24263 { 24264 int i; 24265 hrtime_t hrt; 24266 time_t wallclock; 24267 uint64_t result; 24268 24269 /* 24270 * Use high-res timer and current time for seed. Gethrtime() returns 24271 * a longlong, which may contain resolution down to nanoseconds. 24272 * The current time will either be a 32-bit or a 64-bit quantity. 24273 * XOR the two together in a 64-bit result variable. 24274 * Convert the result to a 32-bit value by multiplying the high-order 24275 * 32-bits by the low-order 32-bits. 24276 */ 24277 24278 hrt = gethrtime(); 24279 (void) drv_getparm(TIME, &wallclock); 24280 result = (uint64_t)wallclock ^ (uint64_t)hrt; 24281 mutex_enter(&tcp_random_lock); 24282 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 24283 (result & 0xffffffff); 24284 24285 for (i = 1; i < DEG_3; i++) 24286 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 24287 + 12345; 24288 tcp_random_fptr = &tcp_random_state[SEP_3]; 24289 tcp_random_rptr = &tcp_random_state[0]; 24290 mutex_exit(&tcp_random_lock); 24291 for (i = 0; i < 10 * DEG_3; i++) 24292 (void) tcp_random(); 24293 } 24294 24295 /* 24296 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 24297 * This range is selected to be approximately centered on TCP_ISS / 2, 24298 * and easy to compute. We get this value by generating a 32-bit random 24299 * number, selecting out the high-order 17 bits, and then adding one so 24300 * that we never return zero. 24301 */ 24302 int 24303 tcp_random(void) 24304 { 24305 int i; 24306 24307 mutex_enter(&tcp_random_lock); 24308 *tcp_random_fptr += *tcp_random_rptr; 24309 24310 /* 24311 * The high-order bits are more random than the low-order bits, 24312 * so we select out the high-order 17 bits and add one so that 24313 * we never return zero. 24314 */ 24315 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 24316 if (++tcp_random_fptr >= tcp_random_end_ptr) { 24317 tcp_random_fptr = tcp_random_state; 24318 ++tcp_random_rptr; 24319 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 24320 tcp_random_rptr = tcp_random_state; 24321 24322 mutex_exit(&tcp_random_lock); 24323 return (i); 24324 } 24325 24326 static int 24327 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 24328 int *t_errorp, int *sys_errorp) 24329 { 24330 int error; 24331 int is_absreq_failure; 24332 t_scalar_t *opt_lenp; 24333 t_scalar_t opt_offset; 24334 int prim_type; 24335 struct T_conn_req *tcreqp; 24336 struct T_conn_res *tcresp; 24337 cred_t *cr; 24338 24339 cr = DB_CREDDEF(mp, tcp->tcp_cred); 24340 24341 prim_type = ((union T_primitives *)mp->b_rptr)->type; 24342 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 24343 prim_type == T_CONN_RES); 24344 24345 switch (prim_type) { 24346 case T_CONN_REQ: 24347 tcreqp = (struct T_conn_req *)mp->b_rptr; 24348 opt_offset = tcreqp->OPT_offset; 24349 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 24350 break; 24351 case O_T_CONN_RES: 24352 case T_CONN_RES: 24353 tcresp = (struct T_conn_res *)mp->b_rptr; 24354 opt_offset = tcresp->OPT_offset; 24355 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 24356 break; 24357 } 24358 24359 *t_errorp = 0; 24360 *sys_errorp = 0; 24361 *do_disconnectp = 0; 24362 24363 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 24364 opt_offset, cr, &tcp_opt_obj, 24365 NULL, &is_absreq_failure); 24366 24367 switch (error) { 24368 case 0: /* no error */ 24369 ASSERT(is_absreq_failure == 0); 24370 return (0); 24371 case ENOPROTOOPT: 24372 *t_errorp = TBADOPT; 24373 break; 24374 case EACCES: 24375 *t_errorp = TACCES; 24376 break; 24377 default: 24378 *t_errorp = TSYSERR; *sys_errorp = error; 24379 break; 24380 } 24381 if (is_absreq_failure != 0) { 24382 /* 24383 * The connection request should get the local ack 24384 * T_OK_ACK and then a T_DISCON_IND. 24385 */ 24386 *do_disconnectp = 1; 24387 } 24388 return (-1); 24389 } 24390 24391 /* 24392 * Split this function out so that if the secret changes, I'm okay. 24393 * 24394 * Initialize the tcp_iss_cookie and tcp_iss_key. 24395 */ 24396 24397 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 24398 24399 static void 24400 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 24401 { 24402 struct { 24403 int32_t current_time; 24404 uint32_t randnum; 24405 uint16_t pad; 24406 uint8_t ether[6]; 24407 uint8_t passwd[PASSWD_SIZE]; 24408 } tcp_iss_cookie; 24409 time_t t; 24410 24411 /* 24412 * Start with the current absolute time. 24413 */ 24414 (void) drv_getparm(TIME, &t); 24415 tcp_iss_cookie.current_time = t; 24416 24417 /* 24418 * XXX - Need a more random number per RFC 1750, not this crap. 24419 * OTOH, if what follows is pretty random, then I'm in better shape. 24420 */ 24421 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 24422 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 24423 24424 /* 24425 * The cpu_type_info is pretty non-random. Ugggh. It does serve 24426 * as a good template. 24427 */ 24428 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 24429 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 24430 24431 /* 24432 * The pass-phrase. Normally this is supplied by user-called NDD. 24433 */ 24434 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 24435 24436 /* 24437 * See 4010593 if this section becomes a problem again, 24438 * but the local ethernet address is useful here. 24439 */ 24440 (void) localetheraddr(NULL, 24441 (struct ether_addr *)&tcp_iss_cookie.ether); 24442 24443 /* 24444 * Hash 'em all together. The MD5Final is called per-connection. 24445 */ 24446 mutex_enter(&tcps->tcps_iss_key_lock); 24447 MD5Init(&tcps->tcps_iss_key); 24448 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 24449 sizeof (tcp_iss_cookie)); 24450 mutex_exit(&tcps->tcps_iss_key_lock); 24451 } 24452 24453 /* 24454 * Set the RFC 1948 pass phrase 24455 */ 24456 /* ARGSUSED */ 24457 static int 24458 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 24459 cred_t *cr) 24460 { 24461 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 24462 24463 /* 24464 * Basically, value contains a new pass phrase. Pass it along! 24465 */ 24466 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 24467 return (0); 24468 } 24469 24470 /* ARGSUSED */ 24471 static int 24472 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 24473 { 24474 bzero(buf, sizeof (tcp_sack_info_t)); 24475 return (0); 24476 } 24477 24478 /* ARGSUSED */ 24479 static int 24480 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 24481 { 24482 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 24483 return (0); 24484 } 24485 24486 /* 24487 * Make sure we wait until the default queue is setup, yet allow 24488 * tcp_g_q_create() to open a TCP stream. 24489 * We need to allow tcp_g_q_create() do do an open 24490 * of tcp, hence we compare curhread. 24491 * All others have to wait until the tcps_g_q has been 24492 * setup. 24493 */ 24494 void 24495 tcp_g_q_setup(tcp_stack_t *tcps) 24496 { 24497 mutex_enter(&tcps->tcps_g_q_lock); 24498 if (tcps->tcps_g_q != NULL) { 24499 mutex_exit(&tcps->tcps_g_q_lock); 24500 return; 24501 } 24502 if (tcps->tcps_g_q_creator == NULL) { 24503 /* This thread will set it up */ 24504 tcps->tcps_g_q_creator = curthread; 24505 mutex_exit(&tcps->tcps_g_q_lock); 24506 tcp_g_q_create(tcps); 24507 mutex_enter(&tcps->tcps_g_q_lock); 24508 ASSERT(tcps->tcps_g_q_creator == curthread); 24509 tcps->tcps_g_q_creator = NULL; 24510 cv_signal(&tcps->tcps_g_q_cv); 24511 ASSERT(tcps->tcps_g_q != NULL); 24512 mutex_exit(&tcps->tcps_g_q_lock); 24513 return; 24514 } 24515 /* Everybody but the creator has to wait */ 24516 if (tcps->tcps_g_q_creator != curthread) { 24517 while (tcps->tcps_g_q == NULL) 24518 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 24519 } 24520 mutex_exit(&tcps->tcps_g_q_lock); 24521 } 24522 24523 #define IP "ip" 24524 24525 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 24526 24527 /* 24528 * Create a default tcp queue here instead of in strplumb 24529 */ 24530 void 24531 tcp_g_q_create(tcp_stack_t *tcps) 24532 { 24533 int error; 24534 ldi_handle_t lh = NULL; 24535 ldi_ident_t li = NULL; 24536 int rval; 24537 cred_t *cr; 24538 major_t IP_MAJ; 24539 24540 #ifdef NS_DEBUG 24541 (void) printf("tcp_g_q_create()\n"); 24542 #endif 24543 24544 IP_MAJ = ddi_name_to_major(IP); 24545 24546 ASSERT(tcps->tcps_g_q_creator == curthread); 24547 24548 error = ldi_ident_from_major(IP_MAJ, &li); 24549 if (error) { 24550 #ifdef DEBUG 24551 printf("tcp_g_q_create: lyr ident get failed error %d\n", 24552 error); 24553 #endif 24554 return; 24555 } 24556 24557 cr = zone_get_kcred(netstackid_to_zoneid( 24558 tcps->tcps_netstack->netstack_stackid)); 24559 ASSERT(cr != NULL); 24560 /* 24561 * We set the tcp default queue to IPv6 because IPv4 falls 24562 * back to IPv6 when it can't find a client, but 24563 * IPv6 does not fall back to IPv4. 24564 */ 24565 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 24566 if (error) { 24567 #ifdef DEBUG 24568 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 24569 error); 24570 #endif 24571 goto out; 24572 } 24573 24574 /* 24575 * This ioctl causes the tcp framework to cache a pointer to 24576 * this stream, so we don't want to close the stream after 24577 * this operation. 24578 * Use the kernel credentials that are for the zone we're in. 24579 */ 24580 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 24581 (intptr_t)0, FKIOCTL, cr, &rval); 24582 if (error) { 24583 #ifdef DEBUG 24584 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 24585 "error %d\n", error); 24586 #endif 24587 goto out; 24588 } 24589 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 24590 lh = NULL; 24591 out: 24592 /* Close layered handles */ 24593 if (li) 24594 ldi_ident_release(li); 24595 /* Keep cred around until _inactive needs it */ 24596 tcps->tcps_g_q_cr = cr; 24597 } 24598 24599 /* 24600 * We keep tcp_g_q set until all other tcp_t's in the zone 24601 * has gone away, and then when tcp_g_q_inactive() is called 24602 * we clear it. 24603 */ 24604 void 24605 tcp_g_q_destroy(tcp_stack_t *tcps) 24606 { 24607 #ifdef NS_DEBUG 24608 (void) printf("tcp_g_q_destroy()for stack %d\n", 24609 tcps->tcps_netstack->netstack_stackid); 24610 #endif 24611 24612 if (tcps->tcps_g_q == NULL) { 24613 return; /* Nothing to cleanup */ 24614 } 24615 /* 24616 * Drop reference corresponding to the default queue. 24617 * This reference was added from tcp_open when the default queue 24618 * was created, hence we compensate for this extra drop in 24619 * tcp_g_q_close. If the refcnt drops to zero here it means 24620 * the default queue was the last one to be open, in which 24621 * case, then tcp_g_q_inactive will be 24622 * called as a result of the refrele. 24623 */ 24624 TCPS_REFRELE(tcps); 24625 } 24626 24627 /* 24628 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24629 * Run by tcp_q_q_inactive using a taskq. 24630 */ 24631 static void 24632 tcp_g_q_close(void *arg) 24633 { 24634 tcp_stack_t *tcps = arg; 24635 int error; 24636 ldi_handle_t lh = NULL; 24637 ldi_ident_t li = NULL; 24638 cred_t *cr; 24639 major_t IP_MAJ; 24640 24641 IP_MAJ = ddi_name_to_major(IP); 24642 24643 #ifdef NS_DEBUG 24644 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 24645 tcps->tcps_netstack->netstack_stackid, 24646 tcps->tcps_netstack->netstack_refcnt); 24647 #endif 24648 lh = tcps->tcps_g_q_lh; 24649 if (lh == NULL) 24650 return; /* Nothing to cleanup */ 24651 24652 ASSERT(tcps->tcps_refcnt == 1); 24653 ASSERT(tcps->tcps_g_q != NULL); 24654 24655 error = ldi_ident_from_major(IP_MAJ, &li); 24656 if (error) { 24657 #ifdef DEBUG 24658 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 24659 error); 24660 #endif 24661 return; 24662 } 24663 24664 cr = tcps->tcps_g_q_cr; 24665 tcps->tcps_g_q_cr = NULL; 24666 ASSERT(cr != NULL); 24667 24668 /* 24669 * Make sure we can break the recursion when tcp_close decrements 24670 * the reference count causing g_q_inactive to be called again. 24671 */ 24672 tcps->tcps_g_q_lh = NULL; 24673 24674 /* close the default queue */ 24675 (void) ldi_close(lh, FREAD|FWRITE, cr); 24676 /* 24677 * At this point in time tcps and the rest of netstack_t might 24678 * have been deleted. 24679 */ 24680 tcps = NULL; 24681 24682 /* Close layered handles */ 24683 ldi_ident_release(li); 24684 crfree(cr); 24685 } 24686 24687 /* 24688 * Called when last tcp_t drops reference count using TCPS_REFRELE. 24689 * 24690 * Have to ensure that the ldi routines are not used by an 24691 * interrupt thread by using a taskq. 24692 */ 24693 void 24694 tcp_g_q_inactive(tcp_stack_t *tcps) 24695 { 24696 if (tcps->tcps_g_q_lh == NULL) 24697 return; /* Nothing to cleanup */ 24698 24699 ASSERT(tcps->tcps_refcnt == 0); 24700 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 24701 24702 if (servicing_interrupt()) { 24703 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 24704 (void *) tcps, TQ_SLEEP); 24705 } else { 24706 tcp_g_q_close(tcps); 24707 } 24708 } 24709 24710 /* 24711 * Called by IP when IP is loaded into the kernel 24712 */ 24713 void 24714 tcp_ddi_g_init(void) 24715 { 24716 tcp_timercache = kmem_cache_create("tcp_timercache", 24717 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 24718 NULL, NULL, NULL, NULL, NULL, 0); 24719 24720 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 24721 sizeof (tcp_sack_info_t), 0, 24722 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 24723 24724 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 24725 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 24726 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 24727 24728 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 24729 24730 /* Initialize the random number generator */ 24731 tcp_random_init(); 24732 24733 /* A single callback independently of how many netstacks we have */ 24734 ip_squeue_init(tcp_squeue_add); 24735 24736 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 24737 24738 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 24739 TASKQ_PREPOPULATE); 24740 24741 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 24742 24743 /* 24744 * We want to be informed each time a stack is created or 24745 * destroyed in the kernel, so we can maintain the 24746 * set of tcp_stack_t's. 24747 */ 24748 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 24749 tcp_stack_fini); 24750 } 24751 24752 24753 #define INET_NAME "ip" 24754 24755 /* 24756 * Initialize the TCP stack instance. 24757 */ 24758 static void * 24759 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 24760 { 24761 tcp_stack_t *tcps; 24762 tcpparam_t *pa; 24763 int i; 24764 int error = 0; 24765 major_t major; 24766 24767 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 24768 tcps->tcps_netstack = ns; 24769 24770 /* Initialize locks */ 24771 rw_init(&tcps->tcps_hsp_lock, NULL, RW_DEFAULT, NULL); 24772 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 24773 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 24774 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 24775 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 24776 24777 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 24778 tcps->tcps_g_epriv_ports[0] = 2049; 24779 tcps->tcps_g_epriv_ports[1] = 4045; 24780 tcps->tcps_min_anonpriv_port = 512; 24781 24782 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 24783 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 24784 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 24785 TCP_FANOUT_SIZE, KM_SLEEP); 24786 24787 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24788 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24789 MUTEX_DEFAULT, NULL); 24790 } 24791 24792 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24793 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24794 MUTEX_DEFAULT, NULL); 24795 } 24796 24797 /* TCP's IPsec code calls the packet dropper. */ 24798 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24799 24800 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24801 tcps->tcps_params = pa; 24802 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24803 24804 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24805 A_CNT(lcl_tcp_param_arr), tcps); 24806 24807 /* 24808 * Note: To really walk the device tree you need the devinfo 24809 * pointer to your device which is only available after probe/attach. 24810 * The following is safe only because it uses ddi_root_node() 24811 */ 24812 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24813 tcp_opt_obj.odb_opt_arr_cnt); 24814 24815 /* 24816 * Initialize RFC 1948 secret values. This will probably be reset once 24817 * by the boot scripts. 24818 * 24819 * Use NULL name, as the name is caught by the new lockstats. 24820 * 24821 * Initialize with some random, non-guessable string, like the global 24822 * T_INFO_ACK. 24823 */ 24824 24825 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24826 sizeof (tcp_g_t_info_ack), tcps); 24827 24828 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24829 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24830 24831 major = mod_name_to_major(INET_NAME); 24832 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24833 ASSERT(error == 0); 24834 return (tcps); 24835 } 24836 24837 /* 24838 * Called when the IP module is about to be unloaded. 24839 */ 24840 void 24841 tcp_ddi_g_destroy(void) 24842 { 24843 tcp_g_kstat_fini(tcp_g_kstat); 24844 tcp_g_kstat = NULL; 24845 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24846 24847 mutex_destroy(&tcp_random_lock); 24848 24849 kmem_cache_destroy(tcp_timercache); 24850 kmem_cache_destroy(tcp_sack_info_cache); 24851 kmem_cache_destroy(tcp_iphc_cache); 24852 24853 netstack_unregister(NS_TCP); 24854 taskq_destroy(tcp_taskq); 24855 } 24856 24857 /* 24858 * Shut down the TCP stack instance. 24859 */ 24860 /* ARGSUSED */ 24861 static void 24862 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24863 { 24864 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24865 24866 tcp_g_q_destroy(tcps); 24867 } 24868 24869 /* 24870 * Free the TCP stack instance. 24871 */ 24872 static void 24873 tcp_stack_fini(netstackid_t stackid, void *arg) 24874 { 24875 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24876 int i; 24877 24878 nd_free(&tcps->tcps_g_nd); 24879 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24880 tcps->tcps_params = NULL; 24881 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24882 tcps->tcps_wroff_xtra_param = NULL; 24883 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24884 tcps->tcps_mdt_head_param = NULL; 24885 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24886 tcps->tcps_mdt_tail_param = NULL; 24887 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 24888 tcps->tcps_mdt_max_pbufs_param = NULL; 24889 24890 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24891 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 24892 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 24893 } 24894 24895 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24896 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 24897 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 24898 } 24899 24900 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 24901 tcps->tcps_bind_fanout = NULL; 24902 24903 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 24904 tcps->tcps_acceptor_fanout = NULL; 24905 24906 mutex_destroy(&tcps->tcps_iss_key_lock); 24907 rw_destroy(&tcps->tcps_hsp_lock); 24908 mutex_destroy(&tcps->tcps_g_q_lock); 24909 cv_destroy(&tcps->tcps_g_q_cv); 24910 mutex_destroy(&tcps->tcps_epriv_port_lock); 24911 24912 ip_drop_unregister(&tcps->tcps_dropper); 24913 24914 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 24915 tcps->tcps_kstat = NULL; 24916 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 24917 24918 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 24919 tcps->tcps_mibkp = NULL; 24920 24921 ldi_ident_release(tcps->tcps_ldi_ident); 24922 kmem_free(tcps, sizeof (*tcps)); 24923 } 24924 24925 /* 24926 * Generate ISS, taking into account NDD changes may happen halfway through. 24927 * (If the iss is not zero, set it.) 24928 */ 24929 24930 static void 24931 tcp_iss_init(tcp_t *tcp) 24932 { 24933 MD5_CTX context; 24934 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24935 uint32_t answer[4]; 24936 tcp_stack_t *tcps = tcp->tcp_tcps; 24937 24938 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 24939 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 24940 switch (tcps->tcps_strong_iss) { 24941 case 2: 24942 mutex_enter(&tcps->tcps_iss_key_lock); 24943 context = tcps->tcps_iss_key; 24944 mutex_exit(&tcps->tcps_iss_key_lock); 24945 arg.ports = tcp->tcp_ports; 24946 if (tcp->tcp_ipversion == IPV4_VERSION) { 24947 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24948 &arg.src); 24949 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24950 &arg.dst); 24951 } else { 24952 arg.src = tcp->tcp_ip6h->ip6_src; 24953 arg.dst = tcp->tcp_ip6h->ip6_dst; 24954 } 24955 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24956 MD5Final((uchar_t *)answer, &context); 24957 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24958 /* 24959 * Now that we've hashed into a unique per-connection sequence 24960 * space, add a random increment per strong_iss == 1. So I 24961 * guess we'll have to... 24962 */ 24963 /* FALLTHRU */ 24964 case 1: 24965 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24966 break; 24967 default: 24968 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24969 break; 24970 } 24971 tcp->tcp_valid_bits = TCP_ISS_VALID; 24972 tcp->tcp_fss = tcp->tcp_iss - 1; 24973 tcp->tcp_suna = tcp->tcp_iss; 24974 tcp->tcp_snxt = tcp->tcp_iss + 1; 24975 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24976 tcp->tcp_csuna = tcp->tcp_snxt; 24977 } 24978 24979 /* 24980 * Exported routine for extracting active tcp connection status. 24981 * 24982 * This is used by the Solaris Cluster Networking software to 24983 * gather a list of connections that need to be forwarded to 24984 * specific nodes in the cluster when configuration changes occur. 24985 * 24986 * The callback is invoked for each tcp_t structure from all netstacks, 24987 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24988 * from the netstack with the specified stack_id. Returning 24989 * non-zero from the callback routine terminates the search. 24990 */ 24991 int 24992 cl_tcp_walk_list(netstackid_t stack_id, 24993 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24994 { 24995 netstack_handle_t nh; 24996 netstack_t *ns; 24997 int ret = 0; 24998 24999 if (stack_id >= 0) { 25000 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 25001 return (EINVAL); 25002 25003 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25004 ns->netstack_tcp); 25005 netstack_rele(ns); 25006 return (ret); 25007 } 25008 25009 netstack_next_init(&nh); 25010 while ((ns = netstack_next(&nh)) != NULL) { 25011 ret = cl_tcp_walk_list_stack(cl_callback, arg, 25012 ns->netstack_tcp); 25013 netstack_rele(ns); 25014 } 25015 netstack_next_fini(&nh); 25016 return (ret); 25017 } 25018 25019 static int 25020 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 25021 tcp_stack_t *tcps) 25022 { 25023 tcp_t *tcp; 25024 cl_tcp_info_t cl_tcpi; 25025 connf_t *connfp; 25026 conn_t *connp; 25027 int i; 25028 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25029 25030 ASSERT(callback != NULL); 25031 25032 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25033 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25034 connp = NULL; 25035 25036 while ((connp = 25037 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25038 25039 tcp = connp->conn_tcp; 25040 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 25041 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 25042 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 25043 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 25044 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 25045 /* 25046 * The macros tcp_laddr and tcp_faddr give the IPv4 25047 * addresses. They are copied implicitly below as 25048 * mapped addresses. 25049 */ 25050 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 25051 if (tcp->tcp_ipversion == IPV4_VERSION) { 25052 cl_tcpi.cl_tcpi_faddr = 25053 tcp->tcp_ipha->ipha_dst; 25054 } else { 25055 cl_tcpi.cl_tcpi_faddr_v6 = 25056 tcp->tcp_ip6h->ip6_dst; 25057 } 25058 25059 /* 25060 * If the callback returns non-zero 25061 * we terminate the traversal. 25062 */ 25063 if ((*callback)(&cl_tcpi, arg) != 0) { 25064 CONN_DEC_REF(tcp->tcp_connp); 25065 return (1); 25066 } 25067 } 25068 } 25069 25070 return (0); 25071 } 25072 25073 /* 25074 * Macros used for accessing the different types of sockaddr 25075 * structures inside a tcp_ioc_abort_conn_t. 25076 */ 25077 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 25078 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 25079 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 25080 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 25081 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 25082 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 25083 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 25084 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 25085 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 25086 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 25087 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 25088 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 25089 25090 /* 25091 * Return the correct error code to mimic the behavior 25092 * of a connection reset. 25093 */ 25094 #define TCP_AC_GET_ERRCODE(state, err) { \ 25095 switch ((state)) { \ 25096 case TCPS_SYN_SENT: \ 25097 case TCPS_SYN_RCVD: \ 25098 (err) = ECONNREFUSED; \ 25099 break; \ 25100 case TCPS_ESTABLISHED: \ 25101 case TCPS_FIN_WAIT_1: \ 25102 case TCPS_FIN_WAIT_2: \ 25103 case TCPS_CLOSE_WAIT: \ 25104 (err) = ECONNRESET; \ 25105 break; \ 25106 case TCPS_CLOSING: \ 25107 case TCPS_LAST_ACK: \ 25108 case TCPS_TIME_WAIT: \ 25109 (err) = 0; \ 25110 break; \ 25111 default: \ 25112 (err) = ENXIO; \ 25113 } \ 25114 } 25115 25116 /* 25117 * Check if a tcp structure matches the info in acp. 25118 */ 25119 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 25120 (((acp)->ac_local.ss_family == AF_INET) ? \ 25121 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 25122 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 25123 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 25124 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 25125 (TCP_AC_V4LPORT((acp)) == 0 || \ 25126 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 25127 (TCP_AC_V4RPORT((acp)) == 0 || \ 25128 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 25129 (acp)->ac_start <= (tcp)->tcp_state && \ 25130 (acp)->ac_end >= (tcp)->tcp_state) : \ 25131 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 25132 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 25133 &(tcp)->tcp_ip_src_v6)) && \ 25134 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 25135 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 25136 &(tcp)->tcp_remote_v6)) && \ 25137 (TCP_AC_V6LPORT((acp)) == 0 || \ 25138 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 25139 (TCP_AC_V6RPORT((acp)) == 0 || \ 25140 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 25141 (acp)->ac_start <= (tcp)->tcp_state && \ 25142 (acp)->ac_end >= (tcp)->tcp_state)) 25143 25144 #define TCP_AC_MATCH(acp, tcp) \ 25145 (((acp)->ac_zoneid == ALL_ZONES || \ 25146 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 25147 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 25148 25149 /* 25150 * Build a message containing a tcp_ioc_abort_conn_t structure 25151 * which is filled in with information from acp and tp. 25152 */ 25153 static mblk_t * 25154 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 25155 { 25156 mblk_t *mp; 25157 tcp_ioc_abort_conn_t *tacp; 25158 25159 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 25160 if (mp == NULL) 25161 return (NULL); 25162 25163 mp->b_datap->db_type = M_CTL; 25164 25165 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 25166 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 25167 sizeof (uint32_t)); 25168 25169 tacp->ac_start = acp->ac_start; 25170 tacp->ac_end = acp->ac_end; 25171 tacp->ac_zoneid = acp->ac_zoneid; 25172 25173 if (acp->ac_local.ss_family == AF_INET) { 25174 tacp->ac_local.ss_family = AF_INET; 25175 tacp->ac_remote.ss_family = AF_INET; 25176 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 25177 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 25178 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 25179 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 25180 } else { 25181 tacp->ac_local.ss_family = AF_INET6; 25182 tacp->ac_remote.ss_family = AF_INET6; 25183 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 25184 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 25185 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 25186 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 25187 } 25188 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 25189 return (mp); 25190 } 25191 25192 /* 25193 * Print a tcp_ioc_abort_conn_t structure. 25194 */ 25195 static void 25196 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 25197 { 25198 char lbuf[128]; 25199 char rbuf[128]; 25200 sa_family_t af; 25201 in_port_t lport, rport; 25202 ushort_t logflags; 25203 25204 af = acp->ac_local.ss_family; 25205 25206 if (af == AF_INET) { 25207 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 25208 lbuf, 128); 25209 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 25210 rbuf, 128); 25211 lport = ntohs(TCP_AC_V4LPORT(acp)); 25212 rport = ntohs(TCP_AC_V4RPORT(acp)); 25213 } else { 25214 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 25215 lbuf, 128); 25216 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 25217 rbuf, 128); 25218 lport = ntohs(TCP_AC_V6LPORT(acp)); 25219 rport = ntohs(TCP_AC_V6RPORT(acp)); 25220 } 25221 25222 logflags = SL_TRACE | SL_NOTE; 25223 /* 25224 * Don't print this message to the console if the operation was done 25225 * to a non-global zone. 25226 */ 25227 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25228 logflags |= SL_CONSOLE; 25229 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 25230 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 25231 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 25232 acp->ac_start, acp->ac_end); 25233 } 25234 25235 /* 25236 * Called inside tcp_rput when a message built using 25237 * tcp_ioctl_abort_build_msg is put into a queue. 25238 * Note that when we get here there is no wildcard in acp any more. 25239 */ 25240 static void 25241 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 25242 { 25243 tcp_ioc_abort_conn_t *acp; 25244 25245 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 25246 if (tcp->tcp_state <= acp->ac_end) { 25247 /* 25248 * If we get here, we are already on the correct 25249 * squeue. This ioctl follows the following path 25250 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 25251 * ->tcp_ioctl_abort->squeue_enter (if on a 25252 * different squeue) 25253 */ 25254 int errcode; 25255 25256 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 25257 (void) tcp_clean_death(tcp, errcode, 26); 25258 } 25259 freemsg(mp); 25260 } 25261 25262 /* 25263 * Abort all matching connections on a hash chain. 25264 */ 25265 static int 25266 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 25267 boolean_t exact, tcp_stack_t *tcps) 25268 { 25269 int nmatch, err = 0; 25270 tcp_t *tcp; 25271 MBLKP mp, last, listhead = NULL; 25272 conn_t *tconnp; 25273 connf_t *connfp; 25274 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25275 25276 connfp = &ipst->ips_ipcl_conn_fanout[index]; 25277 25278 startover: 25279 nmatch = 0; 25280 25281 mutex_enter(&connfp->connf_lock); 25282 for (tconnp = connfp->connf_head; tconnp != NULL; 25283 tconnp = tconnp->conn_next) { 25284 tcp = tconnp->conn_tcp; 25285 if (TCP_AC_MATCH(acp, tcp)) { 25286 CONN_INC_REF(tcp->tcp_connp); 25287 mp = tcp_ioctl_abort_build_msg(acp, tcp); 25288 if (mp == NULL) { 25289 err = ENOMEM; 25290 CONN_DEC_REF(tcp->tcp_connp); 25291 break; 25292 } 25293 mp->b_prev = (mblk_t *)tcp; 25294 25295 if (listhead == NULL) { 25296 listhead = mp; 25297 last = mp; 25298 } else { 25299 last->b_next = mp; 25300 last = mp; 25301 } 25302 nmatch++; 25303 if (exact) 25304 break; 25305 } 25306 25307 /* Avoid holding lock for too long. */ 25308 if (nmatch >= 500) 25309 break; 25310 } 25311 mutex_exit(&connfp->connf_lock); 25312 25313 /* Pass mp into the correct tcp */ 25314 while ((mp = listhead) != NULL) { 25315 listhead = listhead->b_next; 25316 tcp = (tcp_t *)mp->b_prev; 25317 mp->b_next = mp->b_prev = NULL; 25318 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 25319 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 25320 } 25321 25322 *count += nmatch; 25323 if (nmatch >= 500 && err == 0) 25324 goto startover; 25325 return (err); 25326 } 25327 25328 /* 25329 * Abort all connections that matches the attributes specified in acp. 25330 */ 25331 static int 25332 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 25333 { 25334 sa_family_t af; 25335 uint32_t ports; 25336 uint16_t *pports; 25337 int err = 0, count = 0; 25338 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 25339 int index = -1; 25340 ushort_t logflags; 25341 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25342 25343 af = acp->ac_local.ss_family; 25344 25345 if (af == AF_INET) { 25346 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 25347 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 25348 pports = (uint16_t *)&ports; 25349 pports[1] = TCP_AC_V4LPORT(acp); 25350 pports[0] = TCP_AC_V4RPORT(acp); 25351 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 25352 } 25353 } else { 25354 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 25355 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 25356 pports = (uint16_t *)&ports; 25357 pports[1] = TCP_AC_V6LPORT(acp); 25358 pports[0] = TCP_AC_V6RPORT(acp); 25359 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 25360 } 25361 } 25362 25363 /* 25364 * For cases where remote addr, local port, and remote port are non- 25365 * wildcards, tcp_ioctl_abort_bucket will only be called once. 25366 */ 25367 if (index != -1) { 25368 err = tcp_ioctl_abort_bucket(acp, index, 25369 &count, exact, tcps); 25370 } else { 25371 /* 25372 * loop through all entries for wildcard case 25373 */ 25374 for (index = 0; 25375 index < ipst->ips_ipcl_conn_fanout_size; 25376 index++) { 25377 err = tcp_ioctl_abort_bucket(acp, index, 25378 &count, exact, tcps); 25379 if (err != 0) 25380 break; 25381 } 25382 } 25383 25384 logflags = SL_TRACE | SL_NOTE; 25385 /* 25386 * Don't print this message to the console if the operation was done 25387 * to a non-global zone. 25388 */ 25389 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 25390 logflags |= SL_CONSOLE; 25391 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 25392 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 25393 if (err == 0 && count == 0) 25394 err = ENOENT; 25395 return (err); 25396 } 25397 25398 /* 25399 * Process the TCP_IOC_ABORT_CONN ioctl request. 25400 */ 25401 static void 25402 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 25403 { 25404 int err; 25405 IOCP iocp; 25406 MBLKP mp1; 25407 sa_family_t laf, raf; 25408 tcp_ioc_abort_conn_t *acp; 25409 zone_t *zptr; 25410 conn_t *connp = Q_TO_CONN(q); 25411 zoneid_t zoneid = connp->conn_zoneid; 25412 tcp_t *tcp = connp->conn_tcp; 25413 tcp_stack_t *tcps = tcp->tcp_tcps; 25414 25415 iocp = (IOCP)mp->b_rptr; 25416 25417 if ((mp1 = mp->b_cont) == NULL || 25418 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 25419 err = EINVAL; 25420 goto out; 25421 } 25422 25423 /* check permissions */ 25424 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 25425 err = EPERM; 25426 goto out; 25427 } 25428 25429 if (mp1->b_cont != NULL) { 25430 freemsg(mp1->b_cont); 25431 mp1->b_cont = NULL; 25432 } 25433 25434 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 25435 laf = acp->ac_local.ss_family; 25436 raf = acp->ac_remote.ss_family; 25437 25438 /* check that a zone with the supplied zoneid exists */ 25439 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 25440 zptr = zone_find_by_id(zoneid); 25441 if (zptr != NULL) { 25442 zone_rele(zptr); 25443 } else { 25444 err = EINVAL; 25445 goto out; 25446 } 25447 } 25448 25449 /* 25450 * For exclusive stacks we set the zoneid to zero 25451 * to make TCP operate as if in the global zone. 25452 */ 25453 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 25454 acp->ac_zoneid = GLOBAL_ZONEID; 25455 25456 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 25457 acp->ac_start > acp->ac_end || laf != raf || 25458 (laf != AF_INET && laf != AF_INET6)) { 25459 err = EINVAL; 25460 goto out; 25461 } 25462 25463 tcp_ioctl_abort_dump(acp); 25464 err = tcp_ioctl_abort(acp, tcps); 25465 25466 out: 25467 if (mp1 != NULL) { 25468 freemsg(mp1); 25469 mp->b_cont = NULL; 25470 } 25471 25472 if (err != 0) 25473 miocnak(q, mp, 0, err); 25474 else 25475 miocack(q, mp, 0, 0); 25476 } 25477 25478 /* 25479 * tcp_time_wait_processing() handles processing of incoming packets when 25480 * the tcp is in the TIME_WAIT state. 25481 * A TIME_WAIT tcp that has an associated open TCP stream is never put 25482 * on the time wait list. 25483 */ 25484 void 25485 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 25486 uint32_t seg_ack, int seg_len, tcph_t *tcph) 25487 { 25488 int32_t bytes_acked; 25489 int32_t gap; 25490 int32_t rgap; 25491 tcp_opt_t tcpopt; 25492 uint_t flags; 25493 uint32_t new_swnd = 0; 25494 conn_t *connp; 25495 tcp_stack_t *tcps = tcp->tcp_tcps; 25496 25497 BUMP_LOCAL(tcp->tcp_ibsegs); 25498 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 25499 25500 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 25501 new_swnd = BE16_TO_U16(tcph->th_win) << 25502 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 25503 if (tcp->tcp_snd_ts_ok) { 25504 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 25505 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25506 tcp->tcp_rnxt, TH_ACK); 25507 goto done; 25508 } 25509 } 25510 gap = seg_seq - tcp->tcp_rnxt; 25511 rgap = tcp->tcp_rwnd - (gap + seg_len); 25512 if (gap < 0) { 25513 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 25514 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 25515 (seg_len > -gap ? -gap : seg_len)); 25516 seg_len += gap; 25517 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 25518 if (flags & TH_RST) { 25519 goto done; 25520 } 25521 if ((flags & TH_FIN) && seg_len == -1) { 25522 /* 25523 * When TCP receives a duplicate FIN in 25524 * TIME_WAIT state, restart the 2 MSL timer. 25525 * See page 73 in RFC 793. Make sure this TCP 25526 * is already on the TIME_WAIT list. If not, 25527 * just restart the timer. 25528 */ 25529 if (TCP_IS_DETACHED(tcp)) { 25530 if (tcp_time_wait_remove(tcp, NULL) == 25531 B_TRUE) { 25532 tcp_time_wait_append(tcp); 25533 TCP_DBGSTAT(tcps, 25534 tcp_rput_time_wait); 25535 } 25536 } else { 25537 ASSERT(tcp != NULL); 25538 TCP_TIMER_RESTART(tcp, 25539 tcps->tcps_time_wait_interval); 25540 } 25541 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25542 tcp->tcp_rnxt, TH_ACK); 25543 goto done; 25544 } 25545 flags |= TH_ACK_NEEDED; 25546 seg_len = 0; 25547 goto process_ack; 25548 } 25549 25550 /* Fix seg_seq, and chew the gap off the front. */ 25551 seg_seq = tcp->tcp_rnxt; 25552 } 25553 25554 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 25555 /* 25556 * Make sure that when we accept the connection, pick 25557 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 25558 * old connection. 25559 * 25560 * The next ISS generated is equal to tcp_iss_incr_extra 25561 * + ISS_INCR/2 + other components depending on the 25562 * value of tcp_strong_iss. We pre-calculate the new 25563 * ISS here and compare with tcp_snxt to determine if 25564 * we need to make adjustment to tcp_iss_incr_extra. 25565 * 25566 * The above calculation is ugly and is a 25567 * waste of CPU cycles... 25568 */ 25569 uint32_t new_iss = tcps->tcps_iss_incr_extra; 25570 int32_t adj; 25571 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 25572 25573 switch (tcps->tcps_strong_iss) { 25574 case 2: { 25575 /* Add time and MD5 components. */ 25576 uint32_t answer[4]; 25577 struct { 25578 uint32_t ports; 25579 in6_addr_t src; 25580 in6_addr_t dst; 25581 } arg; 25582 MD5_CTX context; 25583 25584 mutex_enter(&tcps->tcps_iss_key_lock); 25585 context = tcps->tcps_iss_key; 25586 mutex_exit(&tcps->tcps_iss_key_lock); 25587 arg.ports = tcp->tcp_ports; 25588 /* We use MAPPED addresses in tcp_iss_init */ 25589 arg.src = tcp->tcp_ip_src_v6; 25590 if (tcp->tcp_ipversion == IPV4_VERSION) { 25591 IN6_IPADDR_TO_V4MAPPED( 25592 tcp->tcp_ipha->ipha_dst, 25593 &arg.dst); 25594 } else { 25595 arg.dst = 25596 tcp->tcp_ip6h->ip6_dst; 25597 } 25598 MD5Update(&context, (uchar_t *)&arg, 25599 sizeof (arg)); 25600 MD5Final((uchar_t *)answer, &context); 25601 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 25602 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 25603 break; 25604 } 25605 case 1: 25606 /* Add time component and min random (i.e. 1). */ 25607 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 25608 break; 25609 default: 25610 /* Add only time component. */ 25611 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 25612 break; 25613 } 25614 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 25615 /* 25616 * New ISS not guaranteed to be ISS_INCR/2 25617 * ahead of the current tcp_snxt, so add the 25618 * difference to tcp_iss_incr_extra. 25619 */ 25620 tcps->tcps_iss_incr_extra += adj; 25621 } 25622 /* 25623 * If tcp_clean_death() can not perform the task now, 25624 * drop the SYN packet and let the other side re-xmit. 25625 * Otherwise pass the SYN packet back in, since the 25626 * old tcp state has been cleaned up or freed. 25627 */ 25628 if (tcp_clean_death(tcp, 0, 27) == -1) 25629 goto done; 25630 /* 25631 * We will come back to tcp_rput_data 25632 * on the global queue. Packets destined 25633 * for the global queue will be checked 25634 * with global policy. But the policy for 25635 * this packet has already been checked as 25636 * this was destined for the detached 25637 * connection. We need to bypass policy 25638 * check this time by attaching a dummy 25639 * ipsec_in with ipsec_in_dont_check set. 25640 */ 25641 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 25642 if (connp != NULL) { 25643 TCP_STAT(tcps, tcp_time_wait_syn_success); 25644 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 25645 return; 25646 } 25647 goto done; 25648 } 25649 25650 /* 25651 * rgap is the amount of stuff received out of window. A negative 25652 * value is the amount out of window. 25653 */ 25654 if (rgap < 0) { 25655 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 25656 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 25657 /* Fix seg_len and make sure there is something left. */ 25658 seg_len += rgap; 25659 if (seg_len <= 0) { 25660 if (flags & TH_RST) { 25661 goto done; 25662 } 25663 flags |= TH_ACK_NEEDED; 25664 seg_len = 0; 25665 goto process_ack; 25666 } 25667 } 25668 /* 25669 * Check whether we can update tcp_ts_recent. This test is 25670 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 25671 * Extensions for High Performance: An Update", Internet Draft. 25672 */ 25673 if (tcp->tcp_snd_ts_ok && 25674 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 25675 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 25676 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 25677 tcp->tcp_last_rcv_lbolt = lbolt64; 25678 } 25679 25680 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 25681 /* Always ack out of order packets */ 25682 flags |= TH_ACK_NEEDED; 25683 seg_len = 0; 25684 } else if (seg_len > 0) { 25685 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 25686 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 25687 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 25688 } 25689 if (flags & TH_RST) { 25690 (void) tcp_clean_death(tcp, 0, 28); 25691 goto done; 25692 } 25693 if (flags & TH_SYN) { 25694 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 25695 TH_RST|TH_ACK); 25696 /* 25697 * Do not delete the TCP structure if it is in 25698 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 25699 */ 25700 goto done; 25701 } 25702 process_ack: 25703 if (flags & TH_ACK) { 25704 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 25705 if (bytes_acked <= 0) { 25706 if (bytes_acked == 0 && seg_len == 0 && 25707 new_swnd == tcp->tcp_swnd) 25708 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 25709 } else { 25710 /* Acks something not sent */ 25711 flags |= TH_ACK_NEEDED; 25712 } 25713 } 25714 if (flags & TH_ACK_NEEDED) { 25715 /* 25716 * Time to send an ack for some reason. 25717 */ 25718 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 25719 tcp->tcp_rnxt, TH_ACK); 25720 } 25721 done: 25722 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25723 DB_CKSUMSTART(mp) = 0; 25724 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 25725 TCP_STAT(tcps, tcp_time_wait_syn_fail); 25726 } 25727 freemsg(mp); 25728 } 25729 25730 /* 25731 * TCP Timers Implementation. 25732 */ 25733 timeout_id_t 25734 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 25735 { 25736 mblk_t *mp; 25737 tcp_timer_t *tcpt; 25738 tcp_t *tcp = connp->conn_tcp; 25739 25740 ASSERT(connp->conn_sqp != NULL); 25741 25742 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 25743 25744 if (tcp->tcp_timercache == NULL) { 25745 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 25746 } else { 25747 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 25748 mp = tcp->tcp_timercache; 25749 tcp->tcp_timercache = mp->b_next; 25750 mp->b_next = NULL; 25751 ASSERT(mp->b_wptr == NULL); 25752 } 25753 25754 CONN_INC_REF(connp); 25755 tcpt = (tcp_timer_t *)mp->b_rptr; 25756 tcpt->connp = connp; 25757 tcpt->tcpt_proc = f; 25758 /* 25759 * TCP timers are normal timeouts. Plus, they do not require more than 25760 * a 10 millisecond resolution. By choosing a coarser resolution and by 25761 * rounding up the expiration to the next resolution boundary, we can 25762 * batch timers in the callout subsystem to make TCP timers more 25763 * efficient. The roundup also protects short timers from expiring too 25764 * early before they have a chance to be cancelled. 25765 */ 25766 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 25767 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 25768 25769 return ((timeout_id_t)mp); 25770 } 25771 25772 static void 25773 tcp_timer_callback(void *arg) 25774 { 25775 mblk_t *mp = (mblk_t *)arg; 25776 tcp_timer_t *tcpt; 25777 conn_t *connp; 25778 25779 tcpt = (tcp_timer_t *)mp->b_rptr; 25780 connp = tcpt->connp; 25781 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 25782 SQ_FILL, SQTAG_TCP_TIMER); 25783 } 25784 25785 static void 25786 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 25787 { 25788 tcp_timer_t *tcpt; 25789 conn_t *connp = (conn_t *)arg; 25790 tcp_t *tcp = connp->conn_tcp; 25791 25792 tcpt = (tcp_timer_t *)mp->b_rptr; 25793 ASSERT(connp == tcpt->connp); 25794 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25795 25796 /* 25797 * If the TCP has reached the closed state, don't proceed any 25798 * further. This TCP logically does not exist on the system. 25799 * tcpt_proc could for example access queues, that have already 25800 * been qprocoff'ed off. Also see comments at the start of tcp_input 25801 */ 25802 if (tcp->tcp_state != TCPS_CLOSED) { 25803 (*tcpt->tcpt_proc)(connp); 25804 } else { 25805 tcp->tcp_timer_tid = 0; 25806 } 25807 tcp_timer_free(connp->conn_tcp, mp); 25808 } 25809 25810 /* 25811 * There is potential race with untimeout and the handler firing at the same 25812 * time. The mblock may be freed by the handler while we are trying to use 25813 * it. But since both should execute on the same squeue, this race should not 25814 * occur. 25815 */ 25816 clock_t 25817 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25818 { 25819 mblk_t *mp = (mblk_t *)id; 25820 tcp_timer_t *tcpt; 25821 clock_t delta; 25822 25823 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25824 25825 if (mp == NULL) 25826 return (-1); 25827 25828 tcpt = (tcp_timer_t *)mp->b_rptr; 25829 ASSERT(tcpt->connp == connp); 25830 25831 delta = untimeout_default(tcpt->tcpt_tid, 0); 25832 25833 if (delta >= 0) { 25834 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25835 tcp_timer_free(connp->conn_tcp, mp); 25836 CONN_DEC_REF(connp); 25837 } 25838 25839 return (delta); 25840 } 25841 25842 /* 25843 * Allocate space for the timer event. The allocation looks like mblk, but it is 25844 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25845 * 25846 * Dealing with failures: If we can't allocate from the timer cache we try 25847 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25848 * points to b_rptr. 25849 * If we can't allocate anything using allocb_tryhard(), we perform a last 25850 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25851 * save the actual allocation size in b_datap. 25852 */ 25853 mblk_t * 25854 tcp_timermp_alloc(int kmflags) 25855 { 25856 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25857 kmflags & ~KM_PANIC); 25858 25859 if (mp != NULL) { 25860 mp->b_next = mp->b_prev = NULL; 25861 mp->b_rptr = (uchar_t *)(&mp[1]); 25862 mp->b_wptr = NULL; 25863 mp->b_datap = NULL; 25864 mp->b_queue = NULL; 25865 mp->b_cont = NULL; 25866 } else if (kmflags & KM_PANIC) { 25867 /* 25868 * Failed to allocate memory for the timer. Try allocating from 25869 * dblock caches. 25870 */ 25871 /* ipclassifier calls this from a constructor - hence no tcps */ 25872 TCP_G_STAT(tcp_timermp_allocfail); 25873 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25874 if (mp == NULL) { 25875 size_t size = 0; 25876 /* 25877 * Memory is really low. Try tryhard allocation. 25878 * 25879 * ipclassifier calls this from a constructor - 25880 * hence no tcps 25881 */ 25882 TCP_G_STAT(tcp_timermp_allocdblfail); 25883 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25884 sizeof (tcp_timer_t), &size, kmflags); 25885 mp->b_rptr = (uchar_t *)(&mp[1]); 25886 mp->b_next = mp->b_prev = NULL; 25887 mp->b_wptr = (uchar_t *)-1; 25888 mp->b_datap = (dblk_t *)size; 25889 mp->b_queue = NULL; 25890 mp->b_cont = NULL; 25891 } 25892 ASSERT(mp->b_wptr != NULL); 25893 } 25894 /* ipclassifier calls this from a constructor - hence no tcps */ 25895 TCP_G_DBGSTAT(tcp_timermp_alloced); 25896 25897 return (mp); 25898 } 25899 25900 /* 25901 * Free per-tcp timer cache. 25902 * It can only contain entries from tcp_timercache. 25903 */ 25904 void 25905 tcp_timermp_free(tcp_t *tcp) 25906 { 25907 mblk_t *mp; 25908 25909 while ((mp = tcp->tcp_timercache) != NULL) { 25910 ASSERT(mp->b_wptr == NULL); 25911 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25912 kmem_cache_free(tcp_timercache, mp); 25913 } 25914 } 25915 25916 /* 25917 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25918 * events there already (currently at most two events are cached). 25919 * If the event is not allocated from the timer cache, free it right away. 25920 */ 25921 static void 25922 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25923 { 25924 mblk_t *mp1 = tcp->tcp_timercache; 25925 25926 if (mp->b_wptr != NULL) { 25927 /* 25928 * This allocation is not from a timer cache, free it right 25929 * away. 25930 */ 25931 if (mp->b_wptr != (uchar_t *)-1) 25932 freeb(mp); 25933 else 25934 kmem_free(mp, (size_t)mp->b_datap); 25935 } else if (mp1 == NULL || mp1->b_next == NULL) { 25936 /* Cache this timer block for future allocations */ 25937 mp->b_rptr = (uchar_t *)(&mp[1]); 25938 mp->b_next = mp1; 25939 tcp->tcp_timercache = mp; 25940 } else { 25941 kmem_cache_free(tcp_timercache, mp); 25942 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 25943 } 25944 } 25945 25946 /* 25947 * End of TCP Timers implementation. 25948 */ 25949 25950 /* 25951 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25952 * on the specified backing STREAMS q. Note, the caller may make the 25953 * decision to call based on the tcp_t.tcp_flow_stopped value which 25954 * when check outside the q's lock is only an advisory check ... 25955 */ 25956 void 25957 tcp_setqfull(tcp_t *tcp) 25958 { 25959 tcp_stack_t *tcps = tcp->tcp_tcps; 25960 conn_t *connp = tcp->tcp_connp; 25961 25962 if (tcp->tcp_closed) 25963 return; 25964 25965 if (IPCL_IS_NONSTR(connp)) { 25966 (*connp->conn_upcalls->su_txq_full) 25967 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25968 tcp->tcp_flow_stopped = B_TRUE; 25969 } else { 25970 queue_t *q = tcp->tcp_wq; 25971 25972 if (!(q->q_flag & QFULL)) { 25973 mutex_enter(QLOCK(q)); 25974 if (!(q->q_flag & QFULL)) { 25975 /* still need to set QFULL */ 25976 q->q_flag |= QFULL; 25977 tcp->tcp_flow_stopped = B_TRUE; 25978 mutex_exit(QLOCK(q)); 25979 TCP_STAT(tcps, tcp_flwctl_on); 25980 } else { 25981 mutex_exit(QLOCK(q)); 25982 } 25983 } 25984 } 25985 } 25986 25987 void 25988 tcp_clrqfull(tcp_t *tcp) 25989 { 25990 conn_t *connp = tcp->tcp_connp; 25991 25992 if (tcp->tcp_closed) 25993 return; 25994 25995 if (IPCL_IS_NONSTR(connp)) { 25996 (*connp->conn_upcalls->su_txq_full) 25997 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25998 tcp->tcp_flow_stopped = B_FALSE; 25999 } else { 26000 queue_t *q = tcp->tcp_wq; 26001 26002 if (q->q_flag & QFULL) { 26003 mutex_enter(QLOCK(q)); 26004 if (q->q_flag & QFULL) { 26005 q->q_flag &= ~QFULL; 26006 tcp->tcp_flow_stopped = B_FALSE; 26007 mutex_exit(QLOCK(q)); 26008 if (q->q_flag & QWANTW) 26009 qbackenable(q, 0); 26010 } else { 26011 mutex_exit(QLOCK(q)); 26012 } 26013 } 26014 } 26015 } 26016 26017 /* 26018 * kstats related to squeues i.e. not per IP instance 26019 */ 26020 static void * 26021 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 26022 { 26023 kstat_t *ksp; 26024 26025 tcp_g_stat_t template = { 26026 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 26027 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 26028 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 26029 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 26030 }; 26031 26032 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 26033 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26034 KSTAT_FLAG_VIRTUAL); 26035 26036 if (ksp == NULL) 26037 return (NULL); 26038 26039 bcopy(&template, tcp_g_statp, sizeof (template)); 26040 ksp->ks_data = (void *)tcp_g_statp; 26041 26042 kstat_install(ksp); 26043 return (ksp); 26044 } 26045 26046 static void 26047 tcp_g_kstat_fini(kstat_t *ksp) 26048 { 26049 if (ksp != NULL) { 26050 kstat_delete(ksp); 26051 } 26052 } 26053 26054 26055 static void * 26056 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 26057 { 26058 kstat_t *ksp; 26059 26060 tcp_stat_t template = { 26061 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 26062 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 26063 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 26064 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 26065 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 26066 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 26067 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 26068 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 26069 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 26070 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 26071 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 26072 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 26073 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 26074 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 26075 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 26076 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 26077 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 26078 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 26079 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 26080 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 26081 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 26082 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 26083 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 26084 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 26085 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 26086 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 26087 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 26088 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 26089 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 26090 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 26091 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 26092 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 26093 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 26094 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 26095 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 26096 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 26097 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 26098 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 26099 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 26100 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 26101 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 26102 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 26103 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 26104 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 26105 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 26106 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 26107 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 26108 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 26109 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 26110 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 26111 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 26112 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 26113 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 26114 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 26115 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 26116 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 26117 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 26118 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 26119 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 26120 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 26121 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 26122 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 26123 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 26124 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 26125 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 26126 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 26127 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 26128 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 26129 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 26130 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 26131 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 26132 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 26133 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 26134 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 26135 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 26136 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 26137 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 26138 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 26139 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 26140 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 26141 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 26142 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 26143 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 26144 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 26145 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 26146 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 26147 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 26148 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 26149 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 26150 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 26151 }; 26152 26153 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 26154 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 26155 KSTAT_FLAG_VIRTUAL, stackid); 26156 26157 if (ksp == NULL) 26158 return (NULL); 26159 26160 bcopy(&template, tcps_statisticsp, sizeof (template)); 26161 ksp->ks_data = (void *)tcps_statisticsp; 26162 ksp->ks_private = (void *)(uintptr_t)stackid; 26163 26164 kstat_install(ksp); 26165 return (ksp); 26166 } 26167 26168 static void 26169 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 26170 { 26171 if (ksp != NULL) { 26172 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26173 kstat_delete_netstack(ksp, stackid); 26174 } 26175 } 26176 26177 /* 26178 * TCP Kstats implementation 26179 */ 26180 static void * 26181 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 26182 { 26183 kstat_t *ksp; 26184 26185 tcp_named_kstat_t template = { 26186 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 26187 { "rtoMin", KSTAT_DATA_INT32, 0 }, 26188 { "rtoMax", KSTAT_DATA_INT32, 0 }, 26189 { "maxConn", KSTAT_DATA_INT32, 0 }, 26190 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 26191 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 26192 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 26193 { "estabResets", KSTAT_DATA_UINT32, 0 }, 26194 { "currEstab", KSTAT_DATA_UINT32, 0 }, 26195 { "inSegs", KSTAT_DATA_UINT64, 0 }, 26196 { "outSegs", KSTAT_DATA_UINT64, 0 }, 26197 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 26198 { "connTableSize", KSTAT_DATA_INT32, 0 }, 26199 { "outRsts", KSTAT_DATA_UINT32, 0 }, 26200 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 26201 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 26202 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 26203 { "outAck", KSTAT_DATA_UINT32, 0 }, 26204 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 26205 { "outUrg", KSTAT_DATA_UINT32, 0 }, 26206 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 26207 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 26208 { "outControl", KSTAT_DATA_UINT32, 0 }, 26209 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 26210 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 26211 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 26212 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 26213 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 26214 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 26215 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 26216 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 26217 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 26218 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 26219 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 26220 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 26221 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 26222 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 26223 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 26224 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 26225 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 26226 { "inClosed", KSTAT_DATA_UINT32, 0 }, 26227 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 26228 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 26229 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 26230 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 26231 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 26232 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 26233 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 26234 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 26235 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 26236 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 26237 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 26238 { "connTableSize6", KSTAT_DATA_INT32, 0 } 26239 }; 26240 26241 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 26242 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 26243 26244 if (ksp == NULL) 26245 return (NULL); 26246 26247 template.rtoAlgorithm.value.ui32 = 4; 26248 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 26249 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 26250 template.maxConn.value.i32 = -1; 26251 26252 bcopy(&template, ksp->ks_data, sizeof (template)); 26253 ksp->ks_update = tcp_kstat_update; 26254 ksp->ks_private = (void *)(uintptr_t)stackid; 26255 26256 kstat_install(ksp); 26257 return (ksp); 26258 } 26259 26260 static void 26261 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 26262 { 26263 if (ksp != NULL) { 26264 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 26265 kstat_delete_netstack(ksp, stackid); 26266 } 26267 } 26268 26269 static int 26270 tcp_kstat_update(kstat_t *kp, int rw) 26271 { 26272 tcp_named_kstat_t *tcpkp; 26273 tcp_t *tcp; 26274 connf_t *connfp; 26275 conn_t *connp; 26276 int i; 26277 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 26278 netstack_t *ns; 26279 tcp_stack_t *tcps; 26280 ip_stack_t *ipst; 26281 26282 if ((kp == NULL) || (kp->ks_data == NULL)) 26283 return (EIO); 26284 26285 if (rw == KSTAT_WRITE) 26286 return (EACCES); 26287 26288 ns = netstack_find_by_stackid(stackid); 26289 if (ns == NULL) 26290 return (-1); 26291 tcps = ns->netstack_tcp; 26292 if (tcps == NULL) { 26293 netstack_rele(ns); 26294 return (-1); 26295 } 26296 26297 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 26298 26299 tcpkp->currEstab.value.ui32 = 0; 26300 26301 ipst = ns->netstack_ip; 26302 26303 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 26304 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 26305 connp = NULL; 26306 while ((connp = 26307 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 26308 tcp = connp->conn_tcp; 26309 switch (tcp_snmp_state(tcp)) { 26310 case MIB2_TCP_established: 26311 case MIB2_TCP_closeWait: 26312 tcpkp->currEstab.value.ui32++; 26313 break; 26314 } 26315 } 26316 } 26317 26318 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 26319 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 26320 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 26321 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 26322 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 26323 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 26324 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 26325 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 26326 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 26327 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 26328 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 26329 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 26330 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 26331 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 26332 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 26333 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 26334 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 26335 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 26336 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 26337 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 26338 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 26339 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 26340 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 26341 tcpkp->inDataInorderSegs.value.ui32 = 26342 tcps->tcps_mib.tcpInDataInorderSegs; 26343 tcpkp->inDataInorderBytes.value.ui32 = 26344 tcps->tcps_mib.tcpInDataInorderBytes; 26345 tcpkp->inDataUnorderSegs.value.ui32 = 26346 tcps->tcps_mib.tcpInDataUnorderSegs; 26347 tcpkp->inDataUnorderBytes.value.ui32 = 26348 tcps->tcps_mib.tcpInDataUnorderBytes; 26349 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 26350 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 26351 tcpkp->inDataPartDupSegs.value.ui32 = 26352 tcps->tcps_mib.tcpInDataPartDupSegs; 26353 tcpkp->inDataPartDupBytes.value.ui32 = 26354 tcps->tcps_mib.tcpInDataPartDupBytes; 26355 tcpkp->inDataPastWinSegs.value.ui32 = 26356 tcps->tcps_mib.tcpInDataPastWinSegs; 26357 tcpkp->inDataPastWinBytes.value.ui32 = 26358 tcps->tcps_mib.tcpInDataPastWinBytes; 26359 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 26360 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 26361 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 26362 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 26363 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 26364 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 26365 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 26366 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 26367 tcpkp->timKeepaliveProbe.value.ui32 = 26368 tcps->tcps_mib.tcpTimKeepaliveProbe; 26369 tcpkp->timKeepaliveDrop.value.ui32 = 26370 tcps->tcps_mib.tcpTimKeepaliveDrop; 26371 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 26372 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 26373 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 26374 tcpkp->outSackRetransSegs.value.ui32 = 26375 tcps->tcps_mib.tcpOutSackRetransSegs; 26376 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 26377 26378 netstack_rele(ns); 26379 return (0); 26380 } 26381 26382 void 26383 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 26384 { 26385 uint16_t hdr_len; 26386 ipha_t *ipha; 26387 uint8_t *nexthdrp; 26388 tcph_t *tcph; 26389 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 26390 26391 /* Already has an eager */ 26392 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 26393 TCP_STAT(tcps, tcp_reinput_syn); 26394 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26395 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 26396 return; 26397 } 26398 26399 switch (IPH_HDR_VERSION(mp->b_rptr)) { 26400 case IPV4_VERSION: 26401 ipha = (ipha_t *)mp->b_rptr; 26402 hdr_len = IPH_HDR_LENGTH(ipha); 26403 break; 26404 case IPV6_VERSION: 26405 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 26406 &hdr_len, &nexthdrp)) { 26407 CONN_DEC_REF(connp); 26408 freemsg(mp); 26409 return; 26410 } 26411 break; 26412 } 26413 26414 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 26415 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 26416 mp->b_datap->db_struioflag |= STRUIO_EAGER; 26417 DB_CKSUMSTART(mp) = (intptr_t)sqp; 26418 } 26419 26420 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 26421 SQ_FILL, SQTAG_TCP_REINPUT); 26422 } 26423 26424 static int 26425 tcp_squeue_switch(int val) 26426 { 26427 int rval = SQ_FILL; 26428 26429 switch (val) { 26430 case 1: 26431 rval = SQ_NODRAIN; 26432 break; 26433 case 2: 26434 rval = SQ_PROCESS; 26435 break; 26436 default: 26437 break; 26438 } 26439 return (rval); 26440 } 26441 26442 /* 26443 * This is called once for each squeue - globally for all stack 26444 * instances. 26445 */ 26446 static void 26447 tcp_squeue_add(squeue_t *sqp) 26448 { 26449 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 26450 sizeof (tcp_squeue_priv_t), KM_SLEEP); 26451 26452 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 26453 tcp_time_wait->tcp_time_wait_tid = 26454 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 26455 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 26456 CALLOUT_FLAG_ROUNDUP); 26457 if (tcp_free_list_max_cnt == 0) { 26458 int tcp_ncpus = ((boot_max_ncpus == -1) ? 26459 max_ncpus : boot_max_ncpus); 26460 26461 /* 26462 * Limit number of entries to 1% of availble memory / tcp_ncpus 26463 */ 26464 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 26465 (tcp_ncpus * sizeof (tcp_t) * 100); 26466 } 26467 tcp_time_wait->tcp_free_list_cnt = 0; 26468 } 26469 26470 static int 26471 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 26472 { 26473 mblk_t *ire_mp = NULL; 26474 mblk_t *syn_mp; 26475 mblk_t *mdti; 26476 mblk_t *lsoi; 26477 int retval; 26478 tcph_t *tcph; 26479 uint32_t mss; 26480 queue_t *q = tcp->tcp_rq; 26481 conn_t *connp = tcp->tcp_connp; 26482 tcp_stack_t *tcps = tcp->tcp_tcps; 26483 26484 if (error == 0) { 26485 /* 26486 * Adapt Multidata information, if any. The 26487 * following tcp_mdt_update routine will free 26488 * the message. 26489 */ 26490 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 26491 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 26492 b_rptr)->mdt_capab, B_TRUE); 26493 freemsg(mdti); 26494 } 26495 26496 /* 26497 * Check to update LSO information with tcp, and 26498 * tcp_lso_update routine will free the message. 26499 */ 26500 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 26501 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 26502 b_rptr)->lso_capab); 26503 freemsg(lsoi); 26504 } 26505 26506 /* Get the IRE, if we had requested for it */ 26507 if (mp != NULL) 26508 ire_mp = tcp_ire_mp(&mp); 26509 26510 if (tcp->tcp_hard_binding) { 26511 tcp->tcp_hard_binding = B_FALSE; 26512 tcp->tcp_hard_bound = B_TRUE; 26513 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 26514 if (retval != 0) { 26515 error = EADDRINUSE; 26516 goto bind_failed; 26517 } 26518 } else { 26519 if (ire_mp != NULL) 26520 freeb(ire_mp); 26521 goto after_syn_sent; 26522 } 26523 26524 retval = tcp_adapt_ire(tcp, ire_mp); 26525 if (ire_mp != NULL) 26526 freeb(ire_mp); 26527 if (retval == 0) { 26528 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 26529 ENETUNREACH : EADDRNOTAVAIL); 26530 goto ipcl_rm; 26531 } 26532 /* 26533 * Don't let an endpoint connect to itself. 26534 * Also checked in tcp_connect() but that 26535 * check can't handle the case when the 26536 * local IP address is INADDR_ANY. 26537 */ 26538 if (tcp->tcp_ipversion == IPV4_VERSION) { 26539 if ((tcp->tcp_ipha->ipha_dst == 26540 tcp->tcp_ipha->ipha_src) && 26541 (BE16_EQL(tcp->tcp_tcph->th_lport, 26542 tcp->tcp_tcph->th_fport))) { 26543 error = EADDRNOTAVAIL; 26544 goto ipcl_rm; 26545 } 26546 } else { 26547 if (IN6_ARE_ADDR_EQUAL( 26548 &tcp->tcp_ip6h->ip6_dst, 26549 &tcp->tcp_ip6h->ip6_src) && 26550 (BE16_EQL(tcp->tcp_tcph->th_lport, 26551 tcp->tcp_tcph->th_fport))) { 26552 error = EADDRNOTAVAIL; 26553 goto ipcl_rm; 26554 } 26555 } 26556 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 26557 /* 26558 * This should not be possible! Just for 26559 * defensive coding... 26560 */ 26561 if (tcp->tcp_state != TCPS_SYN_SENT) 26562 goto after_syn_sent; 26563 26564 if (is_system_labeled() && 26565 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 26566 error = EHOSTUNREACH; 26567 goto ipcl_rm; 26568 } 26569 26570 /* 26571 * tcp_adapt_ire() does not adjust 26572 * for TCP/IP header length. 26573 */ 26574 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 26575 26576 /* 26577 * Just make sure our rwnd is at 26578 * least tcp_recv_hiwat_mss * MSS 26579 * large, and round up to the nearest 26580 * MSS. 26581 * 26582 * We do the round up here because 26583 * we need to get the interface 26584 * MTU first before we can do the 26585 * round up. 26586 */ 26587 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 26588 tcps->tcps_recv_hiwat_minmss * mss); 26589 if (!IPCL_IS_NONSTR(connp)) 26590 q->q_hiwat = tcp->tcp_rwnd; 26591 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 26592 tcp_set_ws_value(tcp); 26593 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 26594 tcp->tcp_tcph->th_win); 26595 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 26596 tcp->tcp_snd_ws_ok = B_TRUE; 26597 26598 /* 26599 * Set tcp_snd_ts_ok to true 26600 * so that tcp_xmit_mp will 26601 * include the timestamp 26602 * option in the SYN segment. 26603 */ 26604 if (tcps->tcps_tstamp_always || 26605 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 26606 tcp->tcp_snd_ts_ok = B_TRUE; 26607 } 26608 26609 /* 26610 * tcp_snd_sack_ok can be set in 26611 * tcp_adapt_ire() if the sack metric 26612 * is set. So check it here also. 26613 */ 26614 if (tcps->tcps_sack_permitted == 2 || 26615 tcp->tcp_snd_sack_ok) { 26616 if (tcp->tcp_sack_info == NULL) { 26617 tcp->tcp_sack_info = 26618 kmem_cache_alloc(tcp_sack_info_cache, 26619 KM_SLEEP); 26620 } 26621 tcp->tcp_snd_sack_ok = B_TRUE; 26622 } 26623 26624 /* 26625 * Should we use ECN? Note that the current 26626 * default value (SunOS 5.9) of tcp_ecn_permitted 26627 * is 1. The reason for doing this is that there 26628 * are equipments out there that will drop ECN 26629 * enabled IP packets. Setting it to 1 avoids 26630 * compatibility problems. 26631 */ 26632 if (tcps->tcps_ecn_permitted == 2) 26633 tcp->tcp_ecn_ok = B_TRUE; 26634 26635 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 26636 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 26637 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 26638 if (syn_mp) { 26639 if (cr == NULL) { 26640 cr = tcp->tcp_cred; 26641 pid = tcp->tcp_cpid; 26642 } 26643 mblk_setcred(syn_mp, cr); 26644 DB_CPID(syn_mp) = pid; 26645 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 26646 } 26647 after_syn_sent: 26648 if (mp != NULL) { 26649 ASSERT(mp->b_cont == NULL); 26650 freeb(mp); 26651 } 26652 return (error); 26653 } else { 26654 /* error */ 26655 if (tcp->tcp_debug) { 26656 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 26657 "tcp_post_ip_bind: error == %d", error); 26658 } 26659 if (mp != NULL) { 26660 freeb(mp); 26661 } 26662 } 26663 26664 ipcl_rm: 26665 /* 26666 * Need to unbind with classifier since we were just 26667 * told that our bind succeeded. a.k.a error == 0 at the entry. 26668 */ 26669 tcp->tcp_hard_bound = B_FALSE; 26670 tcp->tcp_hard_binding = B_FALSE; 26671 26672 ipcl_hash_remove(connp); 26673 26674 bind_failed: 26675 tcp->tcp_state = TCPS_IDLE; 26676 if (tcp->tcp_ipversion == IPV4_VERSION) 26677 tcp->tcp_ipha->ipha_src = 0; 26678 else 26679 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 26680 /* 26681 * Copy of the src addr. in tcp_t is needed since 26682 * the lookup funcs. can only look at tcp_t 26683 */ 26684 V6_SET_ZERO(tcp->tcp_ip_src_v6); 26685 26686 tcph = tcp->tcp_tcph; 26687 tcph->th_lport[0] = 0; 26688 tcph->th_lport[1] = 0; 26689 tcp_bind_hash_remove(tcp); 26690 bzero(&connp->u_port, sizeof (connp->u_port)); 26691 /* blow away saved option results if any */ 26692 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26693 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26694 26695 conn_delete_ire(tcp->tcp_connp, NULL); 26696 26697 return (error); 26698 } 26699 26700 static int 26701 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 26702 boolean_t bind_to_req_port_only, cred_t *cr) 26703 { 26704 in_port_t mlp_port; 26705 mlp_type_t addrtype, mlptype; 26706 boolean_t user_specified; 26707 in_port_t allocated_port; 26708 in_port_t requested_port = *requested_port_ptr; 26709 conn_t *connp; 26710 zone_t *zone; 26711 tcp_stack_t *tcps = tcp->tcp_tcps; 26712 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 26713 26714 /* 26715 * XXX It's up to the caller to specify bind_to_req_port_only or not. 26716 */ 26717 if (cr == NULL) 26718 cr = tcp->tcp_cred; 26719 /* 26720 * Get a valid port (within the anonymous range and should not 26721 * be a privileged one) to use if the user has not given a port. 26722 * If multiple threads are here, they may all start with 26723 * with the same initial port. But, it should be fine as long as 26724 * tcp_bindi will ensure that no two threads will be assigned 26725 * the same port. 26726 * 26727 * NOTE: XXX If a privileged process asks for an anonymous port, we 26728 * still check for ports only in the range > tcp_smallest_non_priv_port, 26729 * unless TCP_ANONPRIVBIND option is set. 26730 */ 26731 mlptype = mlptSingle; 26732 mlp_port = requested_port; 26733 if (requested_port == 0) { 26734 requested_port = tcp->tcp_anon_priv_bind ? 26735 tcp_get_next_priv_port(tcp) : 26736 tcp_update_next_port(tcps->tcps_next_port_to_try, 26737 tcp, B_TRUE); 26738 if (requested_port == 0) { 26739 return (-TNOADDR); 26740 } 26741 user_specified = B_FALSE; 26742 26743 /* 26744 * If the user went through one of the RPC interfaces to create 26745 * this socket and RPC is MLP in this zone, then give him an 26746 * anonymous MLP. 26747 */ 26748 connp = tcp->tcp_connp; 26749 if (connp->conn_anon_mlp && is_system_labeled()) { 26750 zone = crgetzone(cr); 26751 addrtype = tsol_mlp_addr_type(zone->zone_id, 26752 IPV6_VERSION, &v6addr, 26753 tcps->tcps_netstack->netstack_ip); 26754 if (addrtype == mlptSingle) { 26755 return (-TNOADDR); 26756 } 26757 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26758 PMAPPORT, addrtype); 26759 mlp_port = PMAPPORT; 26760 } 26761 } else { 26762 int i; 26763 boolean_t priv = B_FALSE; 26764 26765 /* 26766 * If the requested_port is in the well-known privileged range, 26767 * verify that the stream was opened by a privileged user. 26768 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26769 * but instead the code relies on: 26770 * - the fact that the address of the array and its size never 26771 * changes 26772 * - the atomic assignment of the elements of the array 26773 */ 26774 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26775 priv = B_TRUE; 26776 } else { 26777 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26778 if (requested_port == 26779 tcps->tcps_g_epriv_ports[i]) { 26780 priv = B_TRUE; 26781 break; 26782 } 26783 } 26784 } 26785 if (priv) { 26786 if (secpolicy_net_privaddr(cr, requested_port, 26787 IPPROTO_TCP) != 0) { 26788 if (tcp->tcp_debug) { 26789 (void) strlog(TCP_MOD_ID, 0, 1, 26790 SL_ERROR|SL_TRACE, 26791 "tcp_bind: no priv for port %d", 26792 requested_port); 26793 } 26794 return (-TACCES); 26795 } 26796 } 26797 user_specified = B_TRUE; 26798 26799 connp = tcp->tcp_connp; 26800 if (is_system_labeled()) { 26801 zone = crgetzone(cr); 26802 addrtype = tsol_mlp_addr_type(zone->zone_id, 26803 IPV6_VERSION, &v6addr, 26804 tcps->tcps_netstack->netstack_ip); 26805 if (addrtype == mlptSingle) { 26806 return (-TNOADDR); 26807 } 26808 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26809 requested_port, addrtype); 26810 } 26811 } 26812 26813 if (mlptype != mlptSingle) { 26814 if (secpolicy_net_bindmlp(cr) != 0) { 26815 if (tcp->tcp_debug) { 26816 (void) strlog(TCP_MOD_ID, 0, 1, 26817 SL_ERROR|SL_TRACE, 26818 "tcp_bind: no priv for multilevel port %d", 26819 requested_port); 26820 } 26821 return (-TACCES); 26822 } 26823 26824 /* 26825 * If we're specifically binding a shared IP address and the 26826 * port is MLP on shared addresses, then check to see if this 26827 * zone actually owns the MLP. Reject if not. 26828 */ 26829 if (mlptype == mlptShared && addrtype == mlptShared) { 26830 /* 26831 * No need to handle exclusive-stack zones since 26832 * ALL_ZONES only applies to the shared stack. 26833 */ 26834 zoneid_t mlpzone; 26835 26836 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26837 htons(mlp_port)); 26838 if (connp->conn_zoneid != mlpzone) { 26839 if (tcp->tcp_debug) { 26840 (void) strlog(TCP_MOD_ID, 0, 1, 26841 SL_ERROR|SL_TRACE, 26842 "tcp_bind: attempt to bind port " 26843 "%d on shared addr in zone %d " 26844 "(should be %d)", 26845 mlp_port, connp->conn_zoneid, 26846 mlpzone); 26847 } 26848 return (-TACCES); 26849 } 26850 } 26851 26852 if (!user_specified) { 26853 int err; 26854 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26855 requested_port, B_TRUE); 26856 if (err != 0) { 26857 if (tcp->tcp_debug) { 26858 (void) strlog(TCP_MOD_ID, 0, 1, 26859 SL_ERROR|SL_TRACE, 26860 "tcp_bind: cannot establish anon " 26861 "MLP for port %d", 26862 requested_port); 26863 } 26864 return (err); 26865 } 26866 connp->conn_anon_port = B_TRUE; 26867 } 26868 connp->conn_mlp_type = mlptype; 26869 } 26870 26871 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26872 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26873 26874 if (allocated_port == 0) { 26875 connp->conn_mlp_type = mlptSingle; 26876 if (connp->conn_anon_port) { 26877 connp->conn_anon_port = B_FALSE; 26878 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26879 requested_port, B_FALSE); 26880 } 26881 if (bind_to_req_port_only) { 26882 if (tcp->tcp_debug) { 26883 (void) strlog(TCP_MOD_ID, 0, 1, 26884 SL_ERROR|SL_TRACE, 26885 "tcp_bind: requested addr busy"); 26886 } 26887 return (-TADDRBUSY); 26888 } else { 26889 /* If we are out of ports, fail the bind. */ 26890 if (tcp->tcp_debug) { 26891 (void) strlog(TCP_MOD_ID, 0, 1, 26892 SL_ERROR|SL_TRACE, 26893 "tcp_bind: out of ports?"); 26894 } 26895 return (-TNOADDR); 26896 } 26897 } 26898 26899 /* Pass the allocated port back */ 26900 *requested_port_ptr = allocated_port; 26901 return (0); 26902 } 26903 26904 static int 26905 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26906 boolean_t bind_to_req_port_only) 26907 { 26908 tcp_t *tcp = connp->conn_tcp; 26909 26910 sin_t *sin; 26911 sin6_t *sin6; 26912 sin6_t sin6addr; 26913 in_port_t requested_port; 26914 ipaddr_t v4addr; 26915 in6_addr_t v6addr; 26916 uint_t origipversion; 26917 int error = 0; 26918 26919 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 26920 26921 if (tcp->tcp_state == TCPS_BOUND) { 26922 return (0); 26923 } else if (tcp->tcp_state > TCPS_BOUND) { 26924 if (tcp->tcp_debug) { 26925 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26926 "tcp_bind: bad state, %d", tcp->tcp_state); 26927 } 26928 return (-TOUTSTATE); 26929 } 26930 origipversion = tcp->tcp_ipversion; 26931 26932 if (sa != NULL && !OK_32PTR((char *)sa)) { 26933 if (tcp->tcp_debug) { 26934 (void) strlog(TCP_MOD_ID, 0, 1, 26935 SL_ERROR|SL_TRACE, 26936 "tcp_bind: bad address parameter, " 26937 "address %p, len %d", 26938 (void *)sa, len); 26939 } 26940 return (-TPROTO); 26941 } 26942 26943 switch (len) { 26944 case 0: /* request for a generic port */ 26945 if (tcp->tcp_family == AF_INET) { 26946 sin = (sin_t *)&sin6addr; 26947 *sin = sin_null; 26948 sin->sin_family = AF_INET; 26949 tcp->tcp_ipversion = IPV4_VERSION; 26950 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 26951 } else { 26952 ASSERT(tcp->tcp_family == AF_INET6); 26953 sin6 = (sin6_t *)&sin6addr; 26954 *sin6 = sin6_null; 26955 sin6->sin6_family = AF_INET6; 26956 tcp->tcp_ipversion = IPV6_VERSION; 26957 V6_SET_ZERO(v6addr); 26958 } 26959 requested_port = 0; 26960 break; 26961 26962 case sizeof (sin_t): /* Complete IPv4 address */ 26963 sin = (sin_t *)sa; 26964 /* 26965 * With sockets sockfs will accept bogus sin_family in 26966 * bind() and replace it with the family used in the socket 26967 * call. 26968 */ 26969 if (sin->sin_family != AF_INET || 26970 tcp->tcp_family != AF_INET) { 26971 return (EAFNOSUPPORT); 26972 } 26973 requested_port = ntohs(sin->sin_port); 26974 tcp->tcp_ipversion = IPV4_VERSION; 26975 v4addr = sin->sin_addr.s_addr; 26976 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 26977 break; 26978 26979 case sizeof (sin6_t): /* Complete IPv6 address */ 26980 sin6 = (sin6_t *)sa; 26981 if (sin6->sin6_family != AF_INET6 || 26982 tcp->tcp_family != AF_INET6) { 26983 return (EAFNOSUPPORT); 26984 } 26985 requested_port = ntohs(sin6->sin6_port); 26986 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26987 IPV4_VERSION : IPV6_VERSION; 26988 v6addr = sin6->sin6_addr; 26989 break; 26990 26991 default: 26992 if (tcp->tcp_debug) { 26993 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26994 "tcp_bind: bad address length, %d", len); 26995 } 26996 return (EAFNOSUPPORT); 26997 /* return (-TBADADDR); */ 26998 } 26999 27000 tcp->tcp_bound_source_v6 = v6addr; 27001 27002 /* Check for change in ipversion */ 27003 if (origipversion != tcp->tcp_ipversion) { 27004 ASSERT(tcp->tcp_family == AF_INET6); 27005 error = tcp->tcp_ipversion == IPV6_VERSION ? 27006 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 27007 if (error) { 27008 return (ENOMEM); 27009 } 27010 } 27011 27012 /* 27013 * Initialize family specific fields. Copy of the src addr. 27014 * in tcp_t is needed for the lookup funcs. 27015 */ 27016 if (tcp->tcp_ipversion == IPV6_VERSION) { 27017 tcp->tcp_ip6h->ip6_src = v6addr; 27018 } else { 27019 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 27020 } 27021 tcp->tcp_ip_src_v6 = v6addr; 27022 27023 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 27024 27025 error = tcp_bind_select_lport(tcp, &requested_port, 27026 bind_to_req_port_only, cr); 27027 27028 return (error); 27029 } 27030 27031 /* 27032 * Return unix error is tli error is TSYSERR, otherwise return a negative 27033 * tli error. 27034 */ 27035 int 27036 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 27037 boolean_t bind_to_req_port_only) 27038 { 27039 int error; 27040 tcp_t *tcp = connp->conn_tcp; 27041 27042 if (tcp->tcp_state >= TCPS_BOUND) { 27043 if (tcp->tcp_debug) { 27044 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27045 "tcp_bind: bad state, %d", tcp->tcp_state); 27046 } 27047 return (-TOUTSTATE); 27048 } 27049 27050 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 27051 if (error != 0) 27052 return (error); 27053 27054 ASSERT(tcp->tcp_state == TCPS_BOUND); 27055 27056 tcp->tcp_conn_req_max = 0; 27057 27058 /* 27059 * We need to make sure that the conn_recv is set to a non-null 27060 * value before we insert the conn into the classifier table. 27061 * This is to avoid a race with an incoming packet which does an 27062 * ipcl_classify(). 27063 */ 27064 connp->conn_recv = tcp_conn_request; 27065 27066 if (tcp->tcp_family == AF_INET6) { 27067 ASSERT(tcp->tcp_connp->conn_af_isv6); 27068 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27069 &tcp->tcp_bound_source_v6, 0, B_FALSE); 27070 } else { 27071 ASSERT(!tcp->tcp_connp->conn_af_isv6); 27072 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 27073 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 27074 } 27075 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27076 } 27077 27078 int 27079 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 27080 socklen_t len, cred_t *cr) 27081 { 27082 int error; 27083 conn_t *connp = (conn_t *)proto_handle; 27084 squeue_t *sqp = connp->conn_sqp; 27085 27086 ASSERT(sqp != NULL); 27087 27088 error = squeue_synch_enter(sqp, connp, 0); 27089 if (error != 0) { 27090 /* failed to enter */ 27091 return (ENOSR); 27092 } 27093 27094 /* binding to a NULL address really means unbind */ 27095 if (sa == NULL) { 27096 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 27097 error = tcp_do_unbind(connp); 27098 else 27099 error = EINVAL; 27100 } else { 27101 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 27102 } 27103 27104 squeue_synch_exit(sqp, connp); 27105 27106 if (error < 0) { 27107 if (error == -TOUTSTATE) 27108 error = EINVAL; 27109 else 27110 error = proto_tlitosyserr(-error); 27111 } 27112 27113 return (error); 27114 } 27115 27116 /* 27117 * If the return value from this function is positive, it's a UNIX error. 27118 * Otherwise, if it's negative, then the absolute value is a TLI error. 27119 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 27120 */ 27121 int 27122 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 27123 cred_t *cr, pid_t pid) 27124 { 27125 tcp_t *tcp = connp->conn_tcp; 27126 sin_t *sin = (sin_t *)sa; 27127 sin6_t *sin6 = (sin6_t *)sa; 27128 ipaddr_t *dstaddrp; 27129 in_port_t dstport; 27130 uint_t srcid; 27131 int error = 0; 27132 27133 switch (len) { 27134 default: 27135 /* 27136 * Should never happen 27137 */ 27138 return (EINVAL); 27139 27140 case sizeof (sin_t): 27141 sin = (sin_t *)sa; 27142 if (sin->sin_port == 0) { 27143 return (-TBADADDR); 27144 } 27145 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 27146 return (EAFNOSUPPORT); 27147 } 27148 break; 27149 27150 case sizeof (sin6_t): 27151 sin6 = (sin6_t *)sa; 27152 if (sin6->sin6_port == 0) { 27153 return (-TBADADDR); 27154 } 27155 break; 27156 } 27157 /* 27158 * If we're connecting to an IPv4-mapped IPv6 address, we need to 27159 * make sure that the template IP header in the tcp structure is an 27160 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 27161 * need to this before we call tcp_bindi() so that the port lookup 27162 * code will look for ports in the correct port space (IPv4 and 27163 * IPv6 have separate port spaces). 27164 */ 27165 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 27166 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27167 int err = 0; 27168 27169 err = tcp_header_init_ipv4(tcp); 27170 if (err != 0) { 27171 error = ENOMEM; 27172 goto connect_failed; 27173 } 27174 if (tcp->tcp_lport != 0) 27175 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 27176 } 27177 27178 switch (tcp->tcp_state) { 27179 case TCPS_LISTEN: 27180 /* 27181 * Listening sockets are not allowed to issue connect(). 27182 */ 27183 if (IPCL_IS_NONSTR(connp)) 27184 return (EOPNOTSUPP); 27185 /* FALLTHRU */ 27186 case TCPS_IDLE: 27187 /* 27188 * We support quick connect, refer to comments in 27189 * tcp_connect_*() 27190 */ 27191 /* FALLTHRU */ 27192 case TCPS_BOUND: 27193 /* 27194 * We must bump the generation before the operation start. 27195 * This is done to ensure that any upcall made later on sends 27196 * up the right generation to the socket. 27197 */ 27198 SOCK_CONNID_BUMP(tcp->tcp_connid); 27199 27200 if (tcp->tcp_family == AF_INET6) { 27201 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 27202 return (tcp_connect_ipv6(tcp, 27203 &sin6->sin6_addr, 27204 sin6->sin6_port, sin6->sin6_flowinfo, 27205 sin6->__sin6_src_id, sin6->sin6_scope_id, 27206 cr, pid)); 27207 } 27208 /* 27209 * Destination adress is mapped IPv6 address. 27210 * Source bound address should be unspecified or 27211 * IPv6 mapped address as well. 27212 */ 27213 if (!IN6_IS_ADDR_UNSPECIFIED( 27214 &tcp->tcp_bound_source_v6) && 27215 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 27216 return (EADDRNOTAVAIL); 27217 } 27218 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 27219 dstport = sin6->sin6_port; 27220 srcid = sin6->__sin6_src_id; 27221 } else { 27222 dstaddrp = &sin->sin_addr.s_addr; 27223 dstport = sin->sin_port; 27224 srcid = 0; 27225 } 27226 27227 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 27228 pid); 27229 break; 27230 default: 27231 return (-TOUTSTATE); 27232 } 27233 /* 27234 * Note: Code below is the "failure" case 27235 */ 27236 connect_failed: 27237 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 27238 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 27239 return (error); 27240 } 27241 27242 int 27243 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 27244 socklen_t len, sock_connid_t *id, cred_t *cr) 27245 { 27246 conn_t *connp = (conn_t *)proto_handle; 27247 tcp_t *tcp = connp->conn_tcp; 27248 squeue_t *sqp = connp->conn_sqp; 27249 int error; 27250 27251 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 27252 if (error != 0) { 27253 return (error); 27254 } 27255 27256 error = squeue_synch_enter(sqp, connp, 0); 27257 if (error != 0) { 27258 /* failed to enter */ 27259 return (ENOSR); 27260 } 27261 27262 /* 27263 * TCP supports quick connect, so no need to do an implicit bind 27264 */ 27265 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 27266 if (error == 0) { 27267 *id = connp->conn_tcp->tcp_connid; 27268 } else if (error < 0) { 27269 if (error == -TOUTSTATE) { 27270 switch (connp->conn_tcp->tcp_state) { 27271 case TCPS_SYN_SENT: 27272 error = EALREADY; 27273 break; 27274 case TCPS_ESTABLISHED: 27275 error = EISCONN; 27276 break; 27277 case TCPS_LISTEN: 27278 error = EOPNOTSUPP; 27279 break; 27280 default: 27281 error = EINVAL; 27282 break; 27283 } 27284 } else { 27285 error = proto_tlitosyserr(-error); 27286 } 27287 } 27288 done: 27289 squeue_synch_exit(sqp, connp); 27290 27291 return ((error == 0) ? EINPROGRESS : error); 27292 } 27293 27294 /* ARGSUSED */ 27295 sock_lower_handle_t 27296 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 27297 uint_t *smodep, int *errorp, int flags, cred_t *credp) 27298 { 27299 conn_t *connp; 27300 boolean_t isv6 = family == AF_INET6; 27301 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 27302 (proto != 0 && proto != IPPROTO_TCP)) { 27303 *errorp = EPROTONOSUPPORT; 27304 return (NULL); 27305 } 27306 27307 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 27308 if (connp == NULL) { 27309 return (NULL); 27310 } 27311 27312 /* 27313 * Put the ref for TCP. Ref for IP was already put 27314 * by ipcl_conn_create. Also Make the conn_t globally 27315 * visible to walkers 27316 */ 27317 mutex_enter(&connp->conn_lock); 27318 CONN_INC_REF_LOCKED(connp); 27319 ASSERT(connp->conn_ref == 2); 27320 connp->conn_state_flags &= ~CONN_INCIPIENT; 27321 27322 connp->conn_flags |= IPCL_NONSTR; 27323 mutex_exit(&connp->conn_lock); 27324 27325 ASSERT(errorp != NULL); 27326 *errorp = 0; 27327 *sock_downcalls = &sock_tcp_downcalls; 27328 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 27329 SM_SENDFILESUPP; 27330 27331 return ((sock_lower_handle_t)connp); 27332 } 27333 27334 /* ARGSUSED */ 27335 void 27336 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 27337 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 27338 { 27339 conn_t *connp = (conn_t *)proto_handle; 27340 struct sock_proto_props sopp; 27341 27342 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 27343 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 27344 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 27345 27346 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 27347 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 27348 sopp.sopp_maxpsz = INFPSZ; 27349 sopp.sopp_maxblk = INFPSZ; 27350 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 27351 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 27352 sopp.sopp_maxaddrlen = sizeof (sin6_t); 27353 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 27354 tcp_rinfo.mi_minpsz; 27355 27356 connp->conn_upcalls = sock_upcalls; 27357 connp->conn_upper_handle = sock_handle; 27358 27359 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 27360 } 27361 27362 /* ARGSUSED */ 27363 int 27364 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 27365 { 27366 conn_t *connp = (conn_t *)proto_handle; 27367 27368 tcp_close_common(connp, flags); 27369 27370 ip_free_helper_stream(connp); 27371 27372 /* 27373 * Drop IP's reference on the conn. This is the last reference 27374 * on the connp if the state was less than established. If the 27375 * connection has gone into timewait state, then we will have 27376 * one ref for the TCP and one more ref (total of two) for the 27377 * classifier connected hash list (a timewait connections stays 27378 * in connected hash till closed). 27379 * 27380 * We can't assert the references because there might be other 27381 * transient reference places because of some walkers or queued 27382 * packets in squeue for the timewait state. 27383 */ 27384 CONN_DEC_REF(connp); 27385 return (0); 27386 } 27387 27388 /* ARGSUSED */ 27389 int 27390 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 27391 cred_t *cr) 27392 { 27393 tcp_t *tcp; 27394 uint32_t msize; 27395 conn_t *connp = (conn_t *)proto_handle; 27396 int32_t tcpstate; 27397 27398 ASSERT(connp->conn_ref >= 2); 27399 27400 if (msg->msg_controllen != 0) { 27401 return (EOPNOTSUPP); 27402 27403 } 27404 switch (DB_TYPE(mp)) { 27405 case M_DATA: 27406 tcp = connp->conn_tcp; 27407 ASSERT(tcp != NULL); 27408 27409 tcpstate = tcp->tcp_state; 27410 if (tcpstate < TCPS_ESTABLISHED) { 27411 freemsg(mp); 27412 return (ENOTCONN); 27413 } else if (tcpstate > TCPS_CLOSE_WAIT) { 27414 freemsg(mp); 27415 return (EPIPE); 27416 } 27417 27418 msize = msgdsize(mp); 27419 27420 mutex_enter(&tcp->tcp_non_sq_lock); 27421 tcp->tcp_squeue_bytes += msize; 27422 /* 27423 * Squeue Flow Control 27424 */ 27425 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 27426 tcp_setqfull(tcp); 27427 } 27428 mutex_exit(&tcp->tcp_non_sq_lock); 27429 27430 /* 27431 * The application may pass in an address in the msghdr, but 27432 * we ignore the address on connection-oriented sockets. 27433 * Just like BSD this code does not generate an error for 27434 * TCP (a CONNREQUIRED socket) when sending to an address 27435 * passed in with sendto/sendmsg. Instead the data is 27436 * delivered on the connection as if no address had been 27437 * supplied. 27438 */ 27439 CONN_INC_REF(connp); 27440 27441 if (msg != NULL && msg->msg_flags & MSG_OOB) { 27442 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 27443 tcp_output_urgent, connp, tcp_squeue_flag, 27444 SQTAG_TCP_OUTPUT); 27445 } else { 27446 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 27447 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 27448 } 27449 27450 return (0); 27451 27452 default: 27453 ASSERT(0); 27454 } 27455 27456 freemsg(mp); 27457 return (0); 27458 } 27459 27460 /* ARGSUSED */ 27461 void 27462 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 27463 { 27464 int len; 27465 uint32_t msize; 27466 conn_t *connp = (conn_t *)arg; 27467 tcp_t *tcp = connp->conn_tcp; 27468 27469 msize = msgdsize(mp); 27470 27471 len = msize - 1; 27472 if (len < 0) { 27473 freemsg(mp); 27474 return; 27475 } 27476 27477 /* 27478 * Try to force urgent data out on the wire. 27479 * Even if we have unsent data this will 27480 * at least send the urgent flag. 27481 * XXX does not handle more flag correctly. 27482 */ 27483 len += tcp->tcp_unsent; 27484 len += tcp->tcp_snxt; 27485 tcp->tcp_urg = len; 27486 tcp->tcp_valid_bits |= TCP_URG_VALID; 27487 27488 /* Bypass tcp protocol for fused tcp loopback */ 27489 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 27490 return; 27491 tcp_wput_data(tcp, mp, B_TRUE); 27492 } 27493 27494 /* ARGSUSED */ 27495 int 27496 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27497 socklen_t *addrlen, cred_t *cr) 27498 { 27499 sin_t *sin; 27500 sin6_t *sin6; 27501 conn_t *connp = (conn_t *)proto_handle; 27502 tcp_t *tcp = connp->conn_tcp; 27503 27504 ASSERT(tcp != NULL); 27505 if (tcp->tcp_state < TCPS_SYN_RCVD) 27506 return (ENOTCONN); 27507 27508 addr->sa_family = tcp->tcp_family; 27509 switch (tcp->tcp_family) { 27510 case AF_INET: 27511 if (*addrlen < sizeof (sin_t)) 27512 return (EINVAL); 27513 27514 sin = (sin_t *)addr; 27515 *sin = sin_null; 27516 sin->sin_family = AF_INET; 27517 if (tcp->tcp_ipversion == IPV4_VERSION) { 27518 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 27519 sin->sin_addr.s_addr); 27520 } 27521 sin->sin_port = tcp->tcp_fport; 27522 *addrlen = sizeof (struct sockaddr_in); 27523 break; 27524 case AF_INET6: 27525 sin6 = (sin6_t *)addr; 27526 *sin6 = sin6_null; 27527 sin6->sin6_family = AF_INET6; 27528 27529 if (*addrlen < sizeof (struct sockaddr_in6)) 27530 return (EINVAL); 27531 27532 if (tcp->tcp_ipversion == IPV6_VERSION) { 27533 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 27534 ~IPV6_VERS_AND_FLOW_MASK; 27535 } 27536 27537 sin6->sin6_addr = tcp->tcp_remote_v6; 27538 sin6->sin6_port = tcp->tcp_fport; 27539 *addrlen = sizeof (struct sockaddr_in6); 27540 break; 27541 } 27542 return (0); 27543 } 27544 27545 /* ARGSUSED */ 27546 int 27547 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 27548 socklen_t *addrlenp, cred_t *cr) 27549 { 27550 sin_t *sin; 27551 sin6_t *sin6; 27552 conn_t *connp = (conn_t *)proto_handle; 27553 tcp_t *tcp = connp->conn_tcp; 27554 27555 switch (tcp->tcp_family) { 27556 case AF_INET: 27557 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 27558 if (*addrlenp < sizeof (sin_t)) 27559 return (EINVAL); 27560 sin = (sin_t *)addr; 27561 *sin = sin_null; 27562 sin->sin_family = AF_INET; 27563 *addrlenp = sizeof (sin_t); 27564 if (tcp->tcp_state >= TCPS_BOUND) { 27565 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 27566 sin->sin_port = tcp->tcp_lport; 27567 } 27568 break; 27569 27570 case AF_INET6: 27571 if (*addrlenp < sizeof (sin6_t)) 27572 return (EINVAL); 27573 sin6 = (sin6_t *)addr; 27574 *sin6 = sin6_null; 27575 sin6->sin6_family = AF_INET6; 27576 *addrlenp = sizeof (sin6_t); 27577 if (tcp->tcp_state >= TCPS_BOUND) { 27578 sin6->sin6_port = tcp->tcp_lport; 27579 if (tcp->tcp_ipversion == IPV4_VERSION) { 27580 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 27581 &sin6->sin6_addr); 27582 } else { 27583 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 27584 } 27585 } 27586 break; 27587 } 27588 return (0); 27589 } 27590 27591 /* 27592 * tcp_fallback 27593 * 27594 * A direct socket is falling back to using STREAMS. Hanging 27595 * off of the queue is a temporary tcp_t, which was created using 27596 * tcp_open(). The tcp_open() was called as part of the regular 27597 * sockfs create path, i.e., the SO_SOCKSTR flag is passed down, 27598 * and therefore the temporary tcp_t is marked to be a socket 27599 * (i.e., IPCL_SOCKET, tcp_issocket). So the optimizations 27600 * introduced by FireEngine will be used. 27601 * 27602 * The tcp_t associated with the socket falling back will 27603 * still be marked as a socket, although the direct socket flag 27604 * (IPCL_NONSTR) is removed. A fall back to true TPI semantics 27605 * will not take place until a _SIOCSOCKFALLBACK ioctl is issued. 27606 * 27607 * If the above mentioned behavior, i.e., the tmp tcp_t is created 27608 * as a STREAMS/TPI endpoint, then we will need to do more work here. 27609 * Such as inserting the direct socket into the acceptor hash. 27610 */ 27611 void 27612 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 27613 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 27614 { 27615 tcp_t *tcp, *eager; 27616 conn_t *connp = (conn_t *)proto_handle; 27617 int error; 27618 struct T_capability_ack tca; 27619 struct sockaddr_in6 laddr, faddr; 27620 socklen_t laddrlen, faddrlen; 27621 short opts; 27622 struct stroptions *stropt; 27623 mblk_t *stropt_mp; 27624 mblk_t *mp; 27625 mblk_t *conn_ind_head = NULL; 27626 mblk_t *conn_ind_tail = NULL; 27627 mblk_t *ordrel_mp; 27628 mblk_t *fused_sigurp_mp; 27629 27630 tcp = connp->conn_tcp; 27631 /* 27632 * No support for acceptor fallback 27633 */ 27634 ASSERT(q->q_qinfo != &tcp_acceptor_rinit); 27635 27636 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL); 27637 27638 /* Pre-allocate the T_ordrel_ind mblk. */ 27639 ASSERT(tcp->tcp_ordrel_mp == NULL); 27640 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 27641 STR_NOSIG, NULL); 27642 ordrel_mp->b_datap->db_type = M_PROTO; 27643 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 27644 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 27645 27646 /* Pre-allocate the M_PCSIG anyway */ 27647 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 27648 27649 /* 27650 * Enter the squeue so that no new packets can come in 27651 */ 27652 error = squeue_synch_enter(connp->conn_sqp, connp, 0); 27653 if (error != 0) { 27654 /* failed to enter, free all the pre-allocated messages. */ 27655 freeb(stropt_mp); 27656 freeb(ordrel_mp); 27657 freeb(fused_sigurp_mp); 27658 return; 27659 } 27660 27661 /* Disable I/OAT during fallback */ 27662 tcp->tcp_sodirect = NULL; 27663 27664 connp->conn_dev = (dev_t)RD(q)->q_ptr; 27665 connp->conn_minor_arena = WR(q)->q_ptr; 27666 27667 RD(q)->q_ptr = WR(q)->q_ptr = connp; 27668 27669 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 27670 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 27671 27672 WR(q)->q_qinfo = &tcp_sock_winit; 27673 27674 if (!direct_sockfs) 27675 tcp_disable_direct_sockfs(tcp); 27676 27677 /* 27678 * free the helper stream 27679 */ 27680 ip_free_helper_stream(connp); 27681 27682 /* 27683 * Notify the STREAM head about options 27684 */ 27685 DB_TYPE(stropt_mp) = M_SETOPTS; 27686 stropt = (struct stroptions *)stropt_mp->b_rptr; 27687 stropt_mp->b_wptr += sizeof (struct stroptions); 27688 stropt = (struct stroptions *)stropt_mp->b_rptr; 27689 stropt->so_flags |= SO_HIWAT | SO_WROFF | SO_MAXBLK; 27690 27691 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 27692 tcp->tcp_tcps->tcps_wroff_xtra); 27693 if (tcp->tcp_snd_sack_ok) 27694 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 27695 stropt->so_hiwat = tcp->tcp_fused ? 27696 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 27697 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 27698 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 27699 27700 putnext(RD(q), stropt_mp); 27701 27702 /* 27703 * Collect the information needed to sync with the sonode 27704 */ 27705 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 27706 27707 laddrlen = faddrlen = sizeof (sin6_t); 27708 (void) tcp_getsockname(proto_handle, (struct sockaddr *)&laddr, 27709 &laddrlen, CRED()); 27710 error = tcp_getpeername(proto_handle, (struct sockaddr *)&faddr, 27711 &faddrlen, CRED()); 27712 if (error != 0) 27713 faddrlen = 0; 27714 27715 opts = 0; 27716 if (tcp->tcp_oobinline) 27717 opts |= SO_OOBINLINE; 27718 if (tcp->tcp_dontroute) 27719 opts |= SO_DONTROUTE; 27720 27721 /* 27722 * Notify the socket that the protocol is now quiescent, 27723 * and it's therefore safe move data from the socket 27724 * to the stream head. 27725 */ 27726 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 27727 (struct sockaddr *)&laddr, laddrlen, 27728 (struct sockaddr *)&faddr, faddrlen, opts); 27729 27730 while ((mp = tcp->tcp_rcv_list) != NULL) { 27731 tcp->tcp_rcv_list = mp->b_next; 27732 mp->b_next = NULL; 27733 putnext(q, mp); 27734 } 27735 tcp->tcp_rcv_last_head = NULL; 27736 tcp->tcp_rcv_last_tail = NULL; 27737 tcp->tcp_rcv_cnt = 0; 27738 27739 /* 27740 * No longer a direct socket 27741 */ 27742 connp->conn_flags &= ~IPCL_NONSTR; 27743 27744 tcp->tcp_ordrel_mp = ordrel_mp; 27745 27746 if (tcp->tcp_fused) { 27747 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 27748 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 27749 } else { 27750 freeb(fused_sigurp_mp); 27751 } 27752 27753 /* 27754 * Send T_CONN_IND messages for all ESTABLISHED connections. 27755 */ 27756 mutex_enter(&tcp->tcp_eager_lock); 27757 for (eager = tcp->tcp_eager_next_q; eager != NULL; 27758 eager = eager->tcp_eager_next_q) { 27759 mp = eager->tcp_conn.tcp_eager_conn_ind; 27760 27761 eager->tcp_conn.tcp_eager_conn_ind = NULL; 27762 ASSERT(mp != NULL); 27763 /* 27764 * TLI/XTI applications will get confused by 27765 * sending eager as an option since it violates 27766 * the option semantics. So remove the eager as 27767 * option since TLI/XTI app doesn't need it anyway. 27768 */ 27769 if (!TCP_IS_SOCKET(tcp)) { 27770 struct T_conn_ind *conn_ind; 27771 27772 conn_ind = (struct T_conn_ind *)mp->b_rptr; 27773 conn_ind->OPT_length = 0; 27774 conn_ind->OPT_offset = 0; 27775 } 27776 if (conn_ind_head == NULL) { 27777 conn_ind_head = mp; 27778 } else { 27779 conn_ind_tail->b_next = mp; 27780 } 27781 conn_ind_tail = mp; 27782 } 27783 mutex_exit(&tcp->tcp_eager_lock); 27784 27785 mp = conn_ind_head; 27786 while (mp != NULL) { 27787 mblk_t *nmp = mp->b_next; 27788 mp->b_next = NULL; 27789 27790 putnext(tcp->tcp_rq, mp); 27791 mp = nmp; 27792 } 27793 27794 /* 27795 * There should be atleast two ref's (IP + TCP) 27796 */ 27797 ASSERT(connp->conn_ref >= 2); 27798 squeue_synch_exit(connp->conn_sqp, connp); 27799 } 27800 27801 /* ARGSUSED */ 27802 static void 27803 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 27804 { 27805 conn_t *connp = (conn_t *)arg; 27806 tcp_t *tcp = connp->conn_tcp; 27807 27808 freemsg(mp); 27809 27810 if (tcp->tcp_fused) 27811 tcp_unfuse(tcp); 27812 27813 if (tcp_xmit_end(tcp) != 0) { 27814 /* 27815 * We were crossing FINs and got a reset from 27816 * the other side. Just ignore it. 27817 */ 27818 if (tcp->tcp_debug) { 27819 (void) strlog(TCP_MOD_ID, 0, 1, 27820 SL_ERROR|SL_TRACE, 27821 "tcp_shutdown_output() out of state %s", 27822 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27823 } 27824 } 27825 } 27826 27827 /* ARGSUSED */ 27828 int 27829 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27830 { 27831 conn_t *connp = (conn_t *)proto_handle; 27832 tcp_t *tcp = connp->conn_tcp; 27833 27834 /* 27835 * X/Open requires that we check the connected state. 27836 */ 27837 if (tcp->tcp_state < TCPS_SYN_SENT) 27838 return (ENOTCONN); 27839 27840 /* shutdown the send side */ 27841 if (how != SHUT_RD) { 27842 mblk_t *bp; 27843 27844 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27845 CONN_INC_REF(connp); 27846 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27847 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27848 27849 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27850 SOCK_OPCTL_SHUT_SEND, 0); 27851 } 27852 27853 /* shutdown the recv side */ 27854 if (how != SHUT_WR) 27855 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27856 SOCK_OPCTL_SHUT_RECV, 0); 27857 27858 return (0); 27859 } 27860 27861 /* 27862 * SOP_LISTEN() calls into tcp_listen(). 27863 */ 27864 /* ARGSUSED */ 27865 int 27866 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27867 { 27868 conn_t *connp = (conn_t *)proto_handle; 27869 int error; 27870 squeue_t *sqp = connp->conn_sqp; 27871 27872 error = squeue_synch_enter(sqp, connp, 0); 27873 if (error != 0) { 27874 /* failed to enter */ 27875 return (ENOBUFS); 27876 } 27877 27878 error = tcp_do_listen(connp, backlog, cr); 27879 if (error == 0) { 27880 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27881 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 27882 } else if (error < 0) { 27883 if (error == -TOUTSTATE) 27884 error = EINVAL; 27885 else 27886 error = proto_tlitosyserr(-error); 27887 } 27888 squeue_synch_exit(sqp, connp); 27889 return (error); 27890 } 27891 27892 static int 27893 tcp_do_listen(conn_t *connp, int backlog, cred_t *cr) 27894 { 27895 tcp_t *tcp = connp->conn_tcp; 27896 sin_t *sin; 27897 sin6_t *sin6; 27898 int error = 0; 27899 tcp_stack_t *tcps = tcp->tcp_tcps; 27900 27901 if (tcp->tcp_state >= TCPS_BOUND) { 27902 if ((tcp->tcp_state == TCPS_BOUND || 27903 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 27904 /* 27905 * Handle listen() increasing backlog. 27906 * This is more "liberal" then what the TPI spec 27907 * requires but is needed to avoid a t_unbind 27908 * when handling listen() since the port number 27909 * might be "stolen" between the unbind and bind. 27910 */ 27911 goto do_listen; 27912 } 27913 if (tcp->tcp_debug) { 27914 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27915 "tcp_listen: bad state, %d", tcp->tcp_state); 27916 } 27917 return (-TOUTSTATE); 27918 } else { 27919 int32_t len; 27920 sin6_t addr; 27921 27922 /* Do an implicit bind: Request for a generic port. */ 27923 if (tcp->tcp_family == AF_INET) { 27924 len = sizeof (sin_t); 27925 sin = (sin_t *)&addr; 27926 *sin = sin_null; 27927 sin->sin_family = AF_INET; 27928 tcp->tcp_ipversion = IPV4_VERSION; 27929 } else { 27930 ASSERT(tcp->tcp_family == AF_INET6); 27931 len = sizeof (sin6_t); 27932 sin6 = (sin6_t *)&addr; 27933 *sin6 = sin6_null; 27934 sin6->sin6_family = AF_INET6; 27935 tcp->tcp_ipversion = IPV6_VERSION; 27936 } 27937 27938 error = tcp_bind_check(connp, (struct sockaddr *)&addr, len, 27939 cr, B_FALSE); 27940 if (error) 27941 return (error); 27942 /* Fall through and do the fanout insertion */ 27943 } 27944 27945 do_listen: 27946 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 27947 tcp->tcp_conn_req_max = backlog; 27948 if (tcp->tcp_conn_req_max) { 27949 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 27950 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 27951 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 27952 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 27953 /* 27954 * If this is a listener, do not reset the eager list 27955 * and other stuffs. Note that we don't check if the 27956 * existing eager list meets the new tcp_conn_req_max 27957 * requirement. 27958 */ 27959 if (tcp->tcp_state != TCPS_LISTEN) { 27960 tcp->tcp_state = TCPS_LISTEN; 27961 /* Initialize the chain. Don't need the eager_lock */ 27962 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 27963 tcp->tcp_eager_next_drop_q0 = tcp; 27964 tcp->tcp_eager_prev_drop_q0 = tcp; 27965 tcp->tcp_second_ctimer_threshold = 27966 tcps->tcps_ip_abort_linterval; 27967 } 27968 } 27969 27970 /* 27971 * We can call ip_bind directly, the processing continues 27972 * in tcp_post_ip_bind(). 27973 * 27974 * We need to make sure that the conn_recv is set to a non-null 27975 * value before we insert the conn into the classifier table. 27976 * This is to avoid a race with an incoming packet which does an 27977 * ipcl_classify(). 27978 */ 27979 connp->conn_recv = tcp_conn_request; 27980 if (tcp->tcp_family == AF_INET) { 27981 error = ip_proto_bind_laddr_v4(connp, NULL, 27982 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 27983 } else { 27984 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27985 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 27986 } 27987 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27988 } 27989 27990 void 27991 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 27992 { 27993 conn_t *connp = (conn_t *)proto_handle; 27994 tcp_t *tcp = connp->conn_tcp; 27995 tcp_stack_t *tcps = tcp->tcp_tcps; 27996 uint_t thwin; 27997 27998 (void) squeue_synch_enter(connp->conn_sqp, connp, 0); 27999 28000 /* Flow control condition has been removed. */ 28001 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 28002 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 28003 << tcp->tcp_rcv_ws; 28004 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 28005 /* 28006 * Send back a window update immediately if TCP is above 28007 * ESTABLISHED state and the increase of the rcv window 28008 * that the other side knows is at least 1 MSS after flow 28009 * control is lifted. 28010 */ 28011 if (tcp->tcp_state >= TCPS_ESTABLISHED && 28012 (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss)) { 28013 tcp_xmit_ctl(NULL, tcp, 28014 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 28015 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 28016 BUMP_MIB(&tcps->tcps_mib, tcpOutWinUpdate); 28017 } 28018 28019 squeue_synch_exit(connp->conn_sqp, connp); 28020 } 28021 28022 /* ARGSUSED */ 28023 int 28024 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 28025 int mode, int32_t *rvalp, cred_t *cr) 28026 { 28027 conn_t *connp = (conn_t *)proto_handle; 28028 int error; 28029 28030 switch (cmd) { 28031 case ND_SET: 28032 case ND_GET: 28033 case TCP_IOC_DEFAULT_Q: 28034 case _SIOCSOCKFALLBACK: 28035 case TCP_IOC_ABORT_CONN: 28036 case TI_GETPEERNAME: 28037 case TI_GETMYNAME: 28038 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 28039 cmd)); 28040 error = EINVAL; 28041 break; 28042 default: 28043 /* 28044 * Pass on to IP using helper stream 28045 */ 28046 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 28047 cmd, arg, mode, cr, rvalp); 28048 break; 28049 } 28050 return (error); 28051 } 28052 28053 sock_downcalls_t sock_tcp_downcalls = { 28054 tcp_activate, 28055 tcp_accept, 28056 tcp_bind, 28057 tcp_listen, 28058 tcp_connect, 28059 tcp_getpeername, 28060 tcp_getsockname, 28061 tcp_getsockopt, 28062 tcp_setsockopt, 28063 tcp_sendmsg, 28064 NULL, 28065 NULL, 28066 NULL, 28067 tcp_shutdown, 28068 tcp_clr_flowctrl, 28069 tcp_ioctl, 28070 tcp_close, 28071 }; 28072