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/uio.h> 66 #include <sys/systm.h> 67 #include <netinet/in.h> 68 #include <netinet/tcp.h> 69 #include <netinet/ip6.h> 70 #include <netinet/icmp6.h> 71 #include <net/if.h> 72 #include <net/route.h> 73 #include <inet/ipsec_impl.h> 74 75 #include <inet/common.h> 76 #include <inet/ip.h> 77 #include <inet/ip_impl.h> 78 #include <inet/ip6.h> 79 #include <inet/ip_ndp.h> 80 #include <inet/proto_set.h> 81 #include <inet/mib2.h> 82 #include <inet/nd.h> 83 #include <inet/optcom.h> 84 #include <inet/snmpcom.h> 85 #include <inet/kstatcom.h> 86 #include <inet/tcp.h> 87 #include <inet/tcp_impl.h> 88 #include <inet/udp_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 * IPsec notes : 220 * 221 * Since a packet is always executed on the correct TCP perimeter 222 * all IPsec processing is defered to IP including checking new 223 * connections and setting IPSEC policies for new connection. The 224 * only exception is tcp_xmit_listeners_reset() which is called 225 * directly from IP and needs to policy check to see if TH_RST 226 * can be sent out. 227 * 228 * PFHooks notes : 229 * 230 * For mdt case, one meta buffer contains multiple packets. Mblks for every 231 * packet are assembled and passed to the hooks. When packets are blocked, 232 * or boundary of any packet is changed, the mdt processing is stopped, and 233 * packets of the meta buffer are send to the IP path one by one. 234 */ 235 236 /* 237 * Values for squeue switch: 238 * 1: SQ_NODRAIN 239 * 2: SQ_PROCESS 240 * 3: SQ_FILL 241 */ 242 int tcp_squeue_wput = 2; /* /etc/systems */ 243 int tcp_squeue_flag; 244 245 /* 246 * This controls how tiny a write must be before we try to copy it 247 * into the the mblk on the tail of the transmit queue. Not much 248 * speedup is observed for values larger than sixteen. Zero will 249 * disable the optimisation. 250 */ 251 int tcp_tx_pull_len = 16; 252 253 /* 254 * TCP Statistics. 255 * 256 * How TCP statistics work. 257 * 258 * There are two types of statistics invoked by two macros. 259 * 260 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 261 * supposed to be used in non MT-hot paths of the code. 262 * 263 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 264 * supposed to be used for DEBUG purposes and may be used on a hot path. 265 * 266 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 267 * (use "kstat tcp" to get them). 268 * 269 * There is also additional debugging facility that marks tcp_clean_death() 270 * instances and saves them in tcp_t structure. It is triggered by 271 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 272 * tcp_clean_death() calls that counts the number of times each tag was hit. It 273 * is triggered by TCP_CLD_COUNTERS define. 274 * 275 * How to add new counters. 276 * 277 * 1) Add a field in the tcp_stat structure describing your counter. 278 * 2) Add a line in the template in tcp_kstat2_init() with the name 279 * of the counter. 280 * 281 * IMPORTANT!! - make sure that both are in sync !! 282 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 283 * 284 * Please avoid using private counters which are not kstat-exported. 285 * 286 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 287 * in tcp_t structure. 288 * 289 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 290 */ 291 292 #ifndef TCP_DEBUG_COUNTER 293 #ifdef DEBUG 294 #define TCP_DEBUG_COUNTER 1 295 #else 296 #define TCP_DEBUG_COUNTER 0 297 #endif 298 #endif 299 300 #define TCP_CLD_COUNTERS 0 301 302 #define TCP_TAG_CLEAN_DEATH 1 303 #define TCP_MAX_CLEAN_DEATH_TAG 32 304 305 #ifdef lint 306 static int _lint_dummy_; 307 #endif 308 309 #if TCP_CLD_COUNTERS 310 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 311 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 312 #elif defined(lint) 313 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 314 #else 315 #define TCP_CLD_STAT(x) 316 #endif 317 318 #if TCP_DEBUG_COUNTER 319 #define TCP_DBGSTAT(tcps, x) \ 320 atomic_add_64(&((tcps)->tcps_statistics.x.value.ui64), 1) 321 #define TCP_G_DBGSTAT(x) \ 322 atomic_add_64(&(tcp_g_statistics.x.value.ui64), 1) 323 #elif defined(lint) 324 #define TCP_DBGSTAT(tcps, x) ASSERT(_lint_dummy_ == 0); 325 #define TCP_G_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 326 #else 327 #define TCP_DBGSTAT(tcps, x) 328 #define TCP_G_DBGSTAT(x) 329 #endif 330 331 #define TCP_G_STAT(x) (tcp_g_statistics.x.value.ui64++) 332 333 tcp_g_stat_t tcp_g_statistics; 334 kstat_t *tcp_g_kstat; 335 336 /* 337 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 338 * tcp write side. 339 */ 340 #define CALL_IP_WPUT(connp, q, mp) { \ 341 ASSERT(((q)->q_flag & QREADR) == 0); \ 342 TCP_DBGSTAT(connp->conn_netstack->netstack_tcp, tcp_ip_output); \ 343 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 344 } 345 346 /* Macros for timestamp comparisons */ 347 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 348 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 349 350 /* 351 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 352 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 353 * by adding three components: a time component which grows by 1 every 4096 354 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 355 * a per-connection component which grows by 125000 for every new connection; 356 * and an "extra" component that grows by a random amount centered 357 * approximately on 64000. This causes the the ISS generator to cycle every 358 * 4.89 hours if no TCP connections are made, and faster if connections are 359 * made. 360 * 361 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 362 * components: a time component which grows by 250000 every second; and 363 * a per-connection component which grows by 125000 for every new connections. 364 * 365 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 366 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 367 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 368 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 369 * password. 370 */ 371 #define ISS_INCR 250000 372 #define ISS_NSEC_SHT 12 373 374 static sin_t sin_null; /* Zero address for quick clears */ 375 static sin6_t sin6_null; /* Zero address for quick clears */ 376 377 /* 378 * This implementation follows the 4.3BSD interpretation of the urgent 379 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 380 * incompatible changes in protocols like telnet and rlogin. 381 */ 382 #define TCP_OLD_URP_INTERPRETATION 1 383 384 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 385 (TCP_IS_DETACHED(tcp) && \ 386 (!(tcp)->tcp_hard_binding)) 387 388 /* 389 * TCP reassembly macros. We hide starting and ending sequence numbers in 390 * b_next and b_prev of messages on the reassembly queue. The messages are 391 * chained using b_cont. These macros are used in tcp_reass() so we don't 392 * have to see the ugly casts and assignments. 393 */ 394 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 395 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 396 (mblk_t *)(uintptr_t)(u)) 397 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 398 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 399 (mblk_t *)(uintptr_t)(u)) 400 401 /* 402 * Implementation of TCP Timers. 403 * ============================= 404 * 405 * INTERFACE: 406 * 407 * There are two basic functions dealing with tcp timers: 408 * 409 * timeout_id_t tcp_timeout(connp, func, time) 410 * clock_t tcp_timeout_cancel(connp, timeout_id) 411 * TCP_TIMER_RESTART(tcp, intvl) 412 * 413 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 414 * after 'time' ticks passed. The function called by timeout() must adhere to 415 * the same restrictions as a driver soft interrupt handler - it must not sleep 416 * or call other functions that might sleep. The value returned is the opaque 417 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 418 * cancel the request. The call to tcp_timeout() may fail in which case it 419 * returns zero. This is different from the timeout(9F) function which never 420 * fails. 421 * 422 * The call-back function 'func' always receives 'connp' as its single 423 * argument. It is always executed in the squeue corresponding to the tcp 424 * structure. The tcp structure is guaranteed to be present at the time the 425 * call-back is called. 426 * 427 * NOTE: The call-back function 'func' is never called if tcp is in 428 * the TCPS_CLOSED state. 429 * 430 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 431 * request. locks acquired by the call-back routine should not be held across 432 * the call to tcp_timeout_cancel() or a deadlock may result. 433 * 434 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 435 * Otherwise, it returns an integer value greater than or equal to 0. In 436 * particular, if the call-back function is already placed on the squeue, it can 437 * not be canceled. 438 * 439 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 440 * within squeue context corresponding to the tcp instance. Since the 441 * call-back is also called via the same squeue, there are no race 442 * conditions described in untimeout(9F) manual page since all calls are 443 * strictly serialized. 444 * 445 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 446 * stored in tcp_timer_tid and starts a new one using 447 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 448 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 449 * field. 450 * 451 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 452 * call-back may still be called, so it is possible tcp_timer() will be 453 * called several times. This should not be a problem since tcp_timer() 454 * should always check the tcp instance state. 455 * 456 * 457 * IMPLEMENTATION: 458 * 459 * TCP timers are implemented using three-stage process. The call to 460 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 461 * when the timer expires. The tcp_timer_callback() arranges the call of the 462 * tcp_timer_handler() function via squeue corresponding to the tcp 463 * instance. The tcp_timer_handler() calls actual requested timeout call-back 464 * and passes tcp instance as an argument to it. Information is passed between 465 * stages using the tcp_timer_t structure which contains the connp pointer, the 466 * tcp call-back to call and the timeout id returned by the timeout(9F). 467 * 468 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 469 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 470 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 471 * returns the pointer to this mblk. 472 * 473 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 474 * looks like a normal mblk without actual dblk attached to it. 475 * 476 * To optimize performance each tcp instance holds a small cache of timer 477 * mblocks. In the current implementation it caches up to two timer mblocks per 478 * tcp instance. The cache is preserved over tcp frees and is only freed when 479 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 480 * timer processing happens on a corresponding squeue, the cache manipulation 481 * does not require any locks. Experiments show that majority of timer mblocks 482 * allocations are satisfied from the tcp cache and do not involve kmem calls. 483 * 484 * The tcp_timeout() places a refhold on the connp instance which guarantees 485 * that it will be present at the time the call-back function fires. The 486 * tcp_timer_handler() drops the reference after calling the call-back, so the 487 * call-back function does not need to manipulate the references explicitly. 488 */ 489 490 typedef struct tcp_timer_s { 491 conn_t *connp; 492 void (*tcpt_proc)(void *); 493 callout_id_t tcpt_tid; 494 } tcp_timer_t; 495 496 static kmem_cache_t *tcp_timercache; 497 kmem_cache_t *tcp_sack_info_cache; 498 kmem_cache_t *tcp_iphc_cache; 499 500 /* 501 * For scalability, we must not run a timer for every TCP connection 502 * in TIME_WAIT state. To see why, consider (for time wait interval of 503 * 4 minutes): 504 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 505 * 506 * This list is ordered by time, so you need only delete from the head 507 * until you get to entries which aren't old enough to delete yet. 508 * The list consists of only the detached TIME_WAIT connections. 509 * 510 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 511 * becomes detached TIME_WAIT (either by changing the state and already 512 * being detached or the other way around). This means that the TIME_WAIT 513 * state can be extended (up to doubled) if the connection doesn't become 514 * detached for a long time. 515 * 516 * The list manipulations (including tcp_time_wait_next/prev) 517 * are protected by the tcp_time_wait_lock. The content of the 518 * detached TIME_WAIT connections is protected by the normal perimeters. 519 * 520 * This list is per squeue and squeues are shared across the tcp_stack_t's. 521 * Things on tcp_time_wait_head remain associated with the tcp_stack_t 522 * and conn_netstack. 523 * The tcp_t's that are added to tcp_free_list are disassociated and 524 * have NULL tcp_tcps and conn_netstack pointers. 525 */ 526 typedef struct tcp_squeue_priv_s { 527 kmutex_t tcp_time_wait_lock; 528 callout_id_t tcp_time_wait_tid; 529 tcp_t *tcp_time_wait_head; 530 tcp_t *tcp_time_wait_tail; 531 tcp_t *tcp_free_list; 532 uint_t tcp_free_list_cnt; 533 } tcp_squeue_priv_t; 534 535 /* 536 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 537 * Running it every 5 seconds seems to give the best results. 538 */ 539 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 540 541 /* 542 * To prevent memory hog, limit the number of entries in tcp_free_list 543 * to 1% of available memory / number of cpus 544 */ 545 uint_t tcp_free_list_max_cnt = 0; 546 547 #define TCP_XMIT_LOWATER 4096 548 #define TCP_XMIT_HIWATER 49152 549 #define TCP_RECV_LOWATER 2048 550 #define TCP_RECV_HIWATER 49152 551 552 /* 553 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 554 */ 555 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 556 557 #define TIDUSZ 4096 /* transport interface data unit size */ 558 559 /* 560 * Bind hash list size and has function. It has to be a power of 2 for 561 * hashing. 562 */ 563 #define TCP_BIND_FANOUT_SIZE 512 564 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 565 /* 566 * Size of listen and acceptor hash list. It has to be a power of 2 for 567 * hashing. 568 */ 569 #define TCP_FANOUT_SIZE 256 570 571 #ifdef _ILP32 572 #define TCP_ACCEPTOR_HASH(accid) \ 573 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 574 #else 575 #define TCP_ACCEPTOR_HASH(accid) \ 576 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 577 #endif /* _ILP32 */ 578 579 #define IP_ADDR_CACHE_SIZE 2048 580 #define IP_ADDR_CACHE_HASH(faddr) \ 581 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 582 583 /* 584 * TCP options struct returned from tcp_parse_options. 585 */ 586 typedef struct tcp_opt_s { 587 uint32_t tcp_opt_mss; 588 uint32_t tcp_opt_wscale; 589 uint32_t tcp_opt_ts_val; 590 uint32_t tcp_opt_ts_ecr; 591 tcp_t *tcp; 592 } tcp_opt_t; 593 594 /* 595 * TCP option struct passing information b/w lisenter and eager. 596 */ 597 struct tcp_options { 598 uint_t to_flags; 599 ssize_t to_boundif; /* IPV6_BOUND_IF */ 600 }; 601 602 #define TCPOPT_BOUNDIF 0x00000001 /* set IPV6_BOUND_IF */ 603 #define TCPOPT_RECVPKTINFO 0x00000002 /* set IPV6_RECVPKTINFO */ 604 605 /* 606 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 607 */ 608 609 #ifdef _BIG_ENDIAN 610 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 611 (TCPOPT_TSTAMP << 8) | 10) 612 #else 613 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 614 (TCPOPT_NOP << 8) | TCPOPT_NOP) 615 #endif 616 617 /* 618 * Flags returned from tcp_parse_options. 619 */ 620 #define TCP_OPT_MSS_PRESENT 1 621 #define TCP_OPT_WSCALE_PRESENT 2 622 #define TCP_OPT_TSTAMP_PRESENT 4 623 #define TCP_OPT_SACK_OK_PRESENT 8 624 #define TCP_OPT_SACK_PRESENT 16 625 626 /* TCP option length */ 627 #define TCPOPT_NOP_LEN 1 628 #define TCPOPT_MAXSEG_LEN 4 629 #define TCPOPT_WS_LEN 3 630 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 631 #define TCPOPT_TSTAMP_LEN 10 632 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 633 #define TCPOPT_SACK_OK_LEN 2 634 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 635 #define TCPOPT_REAL_SACK_LEN 4 636 #define TCPOPT_MAX_SACK_LEN 36 637 #define TCPOPT_HEADER_LEN 2 638 639 /* TCP cwnd burst factor. */ 640 #define TCP_CWND_INFINITE 65535 641 #define TCP_CWND_SS 3 642 #define TCP_CWND_NORMAL 5 643 644 /* Maximum TCP initial cwin (start/restart). */ 645 #define TCP_MAX_INIT_CWND 8 646 647 /* 648 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 649 * either tcp_slow_start_initial or tcp_slow_start_after idle 650 * depending on the caller. If the upper layer has not used the 651 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 652 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 653 * If the upper layer has changed set the tcp_init_cwnd, just use 654 * it to calculate the tcp_cwnd. 655 */ 656 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 657 { \ 658 if ((tcp)->tcp_init_cwnd == 0) { \ 659 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 660 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 661 } else { \ 662 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 663 } \ 664 tcp->tcp_cwnd_cnt = 0; \ 665 } 666 667 /* TCP Timer control structure */ 668 typedef struct tcpt_s { 669 pfv_t tcpt_pfv; /* The routine we are to call */ 670 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 671 } tcpt_t; 672 673 /* 674 * Functions called directly via squeue having a prototype of edesc_t. 675 */ 676 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 677 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 678 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 679 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 680 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 681 void tcp_input(void *arg, mblk_t *mp, void *arg2); 682 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 683 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 684 void tcp_output(void *arg, mblk_t *mp, void *arg2); 685 void tcp_output_urgent(void *arg, mblk_t *mp, void *arg2); 686 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 687 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 688 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2); 689 690 691 /* Prototype for TCP functions */ 692 static void tcp_random_init(void); 693 int tcp_random(void); 694 static void tcp_tli_accept(tcp_t *tcp, mblk_t *mp); 695 static int tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 696 tcp_t *eager); 697 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 698 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 699 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 700 boolean_t user_specified); 701 static void tcp_closei_local(tcp_t *tcp); 702 static void tcp_close_detached(tcp_t *tcp); 703 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 704 mblk_t *idmp, mblk_t **defermp); 705 static void tcp_tpi_connect(tcp_t *tcp, mblk_t *mp); 706 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, 707 in_port_t dstport, uint_t srcid, cred_t *cr, pid_t pid); 708 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, 709 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 710 uint32_t scope_id, cred_t *cr, pid_t pid); 711 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 712 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 713 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 714 static char *tcp_display(tcp_t *tcp, char *, char); 715 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 716 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 717 static void tcp_eager_unlink(tcp_t *tcp); 718 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 719 int unixerr); 720 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 721 int tlierr, int unixerr); 722 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 723 cred_t *cr); 724 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 725 char *value, caddr_t cp, cred_t *cr); 726 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 727 char *value, caddr_t cp, cred_t *cr); 728 static int tcp_tpistate(tcp_t *tcp); 729 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 730 int caller_holds_lock); 731 static void tcp_bind_hash_remove(tcp_t *tcp); 732 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *); 733 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 734 static void tcp_acceptor_hash_remove(tcp_t *tcp); 735 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 736 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 737 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 738 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 739 void tcp_g_q_setup(tcp_stack_t *); 740 void tcp_g_q_create(tcp_stack_t *); 741 void tcp_g_q_destroy(tcp_stack_t *); 742 static int tcp_header_init_ipv4(tcp_t *tcp); 743 static int tcp_header_init_ipv6(tcp_t *tcp); 744 int tcp_init(tcp_t *tcp, queue_t *q); 745 static int tcp_init_values(tcp_t *tcp); 746 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 747 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 748 static void tcp_ip_notify(tcp_t *tcp); 749 static mblk_t *tcp_ire_mp(mblk_t **mpp); 750 static void tcp_iss_init(tcp_t *tcp); 751 static void tcp_keepalive_killer(void *arg); 752 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 753 static void tcp_mss_set(tcp_t *tcp, uint32_t size, boolean_t do_ss); 754 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 755 int *do_disconnectp, int *t_errorp, int *sys_errorp); 756 static boolean_t tcp_allow_connopt_set(int level, int name); 757 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 758 int tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 759 int tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, 760 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 761 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 762 mblk_t *mblk); 763 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 764 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 765 uchar_t *ptr, uint_t len); 766 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 767 static boolean_t tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, 768 tcp_stack_t *); 769 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 770 caddr_t cp, cred_t *cr); 771 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 772 caddr_t cp, cred_t *cr); 773 static void tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *); 774 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 775 caddr_t cp, cred_t *cr); 776 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 777 static void tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt); 778 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 779 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 780 static void tcp_reinit(tcp_t *tcp); 781 static void tcp_reinit_values(tcp_t *tcp); 782 783 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 784 static uint_t tcp_rcv_drain(tcp_t *tcp); 785 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 786 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 787 static void tcp_ss_rexmit(tcp_t *tcp); 788 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 789 static void tcp_process_options(tcp_t *, tcph_t *); 790 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 791 static void tcp_rsrv(queue_t *q); 792 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 793 static int tcp_snmp_state(tcp_t *tcp); 794 static void tcp_timer(void *arg); 795 static void tcp_timer_callback(void *); 796 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 797 boolean_t random); 798 static in_port_t tcp_get_next_priv_port(const tcp_t *); 799 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 800 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 801 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 802 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 803 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 804 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 805 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 806 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 807 const int num_sack_blk, int *usable, uint_t *snxt, 808 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 809 const int mdt_thres); 810 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 811 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 812 const int num_sack_blk, int *usable, uint_t *snxt, 813 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 814 const int mdt_thres); 815 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 816 int num_sack_blk); 817 static void tcp_wsrv(queue_t *q); 818 static int tcp_xmit_end(tcp_t *tcp); 819 static void tcp_ack_timer(void *arg); 820 static mblk_t *tcp_ack_mp(tcp_t *tcp); 821 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 822 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 823 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 824 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 825 uint32_t ack, int ctl); 826 static int setmaxps(queue_t *q, int maxpsz); 827 static void tcp_set_rto(tcp_t *, time_t); 828 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 829 boolean_t, boolean_t); 830 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 831 boolean_t ipsec_mctl); 832 static int tcp_build_hdrs(tcp_t *); 833 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 834 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 835 tcph_t *tcph); 836 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 837 static mblk_t *tcp_mdt_info_mp(mblk_t *); 838 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 839 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 840 const boolean_t, const uint32_t, const uint32_t, 841 const uint32_t, const uint32_t, tcp_stack_t *); 842 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 843 const uint_t, const uint_t, boolean_t *); 844 static mblk_t *tcp_lso_info_mp(mblk_t *); 845 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 846 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 847 extern mblk_t *tcp_timermp_alloc(int); 848 extern void tcp_timermp_free(tcp_t *); 849 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 850 static void tcp_stop_lingering(tcp_t *tcp); 851 static void tcp_close_linger_timeout(void *arg); 852 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 853 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 854 static void tcp_stack_fini(netstackid_t stackid, void *arg); 855 static void *tcp_g_kstat_init(tcp_g_stat_t *); 856 static void tcp_g_kstat_fini(kstat_t *); 857 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 858 static void tcp_kstat_fini(netstackid_t, kstat_t *); 859 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 860 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 861 static int tcp_kstat_update(kstat_t *kp, int rw); 862 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 863 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 864 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 865 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 866 tcph_t *tcph, mblk_t *idmp); 867 static int tcp_squeue_switch(int); 868 869 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 870 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 871 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 872 static int tcp_tpi_close(queue_t *, int); 873 static int tcp_tpi_close_accept(queue_t *); 874 875 static void tcp_squeue_add(squeue_t *); 876 static boolean_t tcp_zcopy_check(tcp_t *); 877 static void tcp_zcopy_notify(tcp_t *); 878 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 879 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 880 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 881 882 extern void tcp_kssl_input(tcp_t *, mblk_t *); 883 884 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 885 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 886 887 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 888 sock_upper_handle_t, cred_t *); 889 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 890 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 891 static int tcp_do_listen(conn_t *, struct sockaddr *, socklen_t, int, cred_t *, 892 boolean_t); 893 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 894 cred_t *, pid_t); 895 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 896 boolean_t); 897 static int tcp_do_unbind(conn_t *); 898 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 899 boolean_t); 900 901 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 902 903 /* 904 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 905 * 906 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 907 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 908 * (defined in tcp.h) needs to be filled in and passed into the kernel 909 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 910 * structure contains the four-tuple of a TCP connection and a range of TCP 911 * states (specified by ac_start and ac_end). The use of wildcard addresses 912 * and ports is allowed. Connections with a matching four tuple and a state 913 * within the specified range will be aborted. The valid states for the 914 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 915 * inclusive. 916 * 917 * An application which has its connection aborted by this ioctl will receive 918 * an error that is dependent on the connection state at the time of the abort. 919 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 920 * though a RST packet has been received. If the connection state is equal to 921 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 922 * and all resources associated with the connection will be freed. 923 */ 924 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 925 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 926 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 927 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 928 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 929 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 930 boolean_t, tcp_stack_t *); 931 932 static struct module_info tcp_rinfo = { 933 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 934 }; 935 936 static struct module_info tcp_winfo = { 937 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 938 }; 939 940 /* 941 * Entry points for TCP as a device. The normal case which supports 942 * the TCP functionality. 943 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 944 */ 945 struct qinit tcp_rinitv4 = { 946 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 947 }; 948 949 struct qinit tcp_rinitv6 = { 950 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 951 }; 952 953 struct qinit tcp_winit = { 954 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 955 }; 956 957 /* Initial entry point for TCP in socket mode. */ 958 struct qinit tcp_sock_winit = { 959 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 960 }; 961 962 /* TCP entry point during fallback */ 963 struct qinit tcp_fallback_sock_winit = { 964 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 965 }; 966 967 /* 968 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 969 * an accept. Avoid allocating data structures since eager has already 970 * been created. 971 */ 972 struct qinit tcp_acceptor_rinit = { 973 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo 974 }; 975 976 struct qinit tcp_acceptor_winit = { 977 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 978 }; 979 980 /* 981 * Entry points for TCP loopback (read side only) 982 * The open routine is only used for reopens, thus no need to 983 * have a separate one for tcp_openv6. 984 */ 985 struct qinit tcp_loopback_rinit = { 986 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 987 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 988 }; 989 990 /* For AF_INET aka /dev/tcp */ 991 struct streamtab tcpinfov4 = { 992 &tcp_rinitv4, &tcp_winit 993 }; 994 995 /* For AF_INET6 aka /dev/tcp6 */ 996 struct streamtab tcpinfov6 = { 997 &tcp_rinitv6, &tcp_winit 998 }; 999 1000 sock_downcalls_t sock_tcp_downcalls; 1001 1002 /* 1003 * Have to ensure that tcp_g_q_close is not done by an 1004 * interrupt thread. 1005 */ 1006 static taskq_t *tcp_taskq; 1007 1008 /* Setable only in /etc/system. Move to ndd? */ 1009 boolean_t tcp_icmp_source_quench = B_FALSE; 1010 1011 /* 1012 * Following assumes TPI alignment requirements stay along 32 bit 1013 * boundaries 1014 */ 1015 #define ROUNDUP32(x) \ 1016 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1017 1018 /* Template for response to info request. */ 1019 static struct T_info_ack tcp_g_t_info_ack = { 1020 T_INFO_ACK, /* PRIM_type */ 1021 0, /* TSDU_size */ 1022 T_INFINITE, /* ETSDU_size */ 1023 T_INVALID, /* CDATA_size */ 1024 T_INVALID, /* DDATA_size */ 1025 sizeof (sin_t), /* ADDR_size */ 1026 0, /* OPT_size - not initialized here */ 1027 TIDUSZ, /* TIDU_size */ 1028 T_COTS_ORD, /* SERV_type */ 1029 TCPS_IDLE, /* CURRENT_state */ 1030 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1031 }; 1032 1033 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1034 T_INFO_ACK, /* PRIM_type */ 1035 0, /* TSDU_size */ 1036 T_INFINITE, /* ETSDU_size */ 1037 T_INVALID, /* CDATA_size */ 1038 T_INVALID, /* DDATA_size */ 1039 sizeof (sin6_t), /* ADDR_size */ 1040 0, /* OPT_size - not initialized here */ 1041 TIDUSZ, /* TIDU_size */ 1042 T_COTS_ORD, /* SERV_type */ 1043 TCPS_IDLE, /* CURRENT_state */ 1044 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1045 }; 1046 1047 #define MS 1L 1048 #define SECONDS (1000 * MS) 1049 #define MINUTES (60 * SECONDS) 1050 #define HOURS (60 * MINUTES) 1051 #define DAYS (24 * HOURS) 1052 1053 #define PARAM_MAX (~(uint32_t)0) 1054 1055 /* Max size IP datagram is 64k - 1 */ 1056 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1057 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1058 /* Max of the above */ 1059 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1060 1061 /* Largest TCP port number */ 1062 #define TCP_MAX_PORT (64 * 1024 - 1) 1063 1064 /* 1065 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1066 * layer header. It has to be a multiple of 4. 1067 */ 1068 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1069 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1070 1071 /* 1072 * All of these are alterable, within the min/max values given, at run time. 1073 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1074 * per the TCP spec. 1075 */ 1076 /* BEGIN CSTYLED */ 1077 static tcpparam_t lcl_tcp_param_arr[] = { 1078 /*min max value name */ 1079 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1080 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1081 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1082 { 1, 1024, 1, "tcp_conn_req_min" }, 1083 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1084 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1085 { 0, 10, 0, "tcp_debug" }, 1086 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1087 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1088 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1089 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1090 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1091 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1092 { 1, 255, 64, "tcp_ipv4_ttl"}, 1093 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1094 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1095 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1096 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1097 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1098 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1099 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1100 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1101 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1102 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1103 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1104 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1105 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1106 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1107 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1108 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1109 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1110 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1111 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1112 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1113 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1114 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1115 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1116 /* 1117 * Question: What default value should I set for tcp_strong_iss? 1118 */ 1119 { 0, 2, 1, "tcp_strong_iss"}, 1120 { 0, 65536, 20, "tcp_rtt_updates"}, 1121 { 0, 1, 1, "tcp_wscale_always"}, 1122 { 0, 1, 0, "tcp_tstamp_always"}, 1123 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1124 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1125 { 0, 16, 2, "tcp_deferred_acks_max"}, 1126 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1127 { 1, 4, 4, "tcp_slow_start_initial"}, 1128 { 0, 2, 2, "tcp_sack_permitted"}, 1129 { 0, 1, 1, "tcp_compression_enabled"}, 1130 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1131 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1132 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1133 { 0, 1, 0, "tcp_rev_src_routes"}, 1134 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1135 { 0, 16, 8, "tcp_local_dacks_max"}, 1136 { 0, 2, 1, "tcp_ecn_permitted"}, 1137 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1138 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1139 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1140 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1141 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1142 }; 1143 /* END CSTYLED */ 1144 1145 /* 1146 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1147 * each header fragment in the header buffer. Each parameter value has 1148 * to be a multiple of 4 (32-bit aligned). 1149 */ 1150 static tcpparam_t lcl_tcp_mdt_head_param = 1151 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1152 static tcpparam_t lcl_tcp_mdt_tail_param = 1153 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1154 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1155 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1156 1157 /* 1158 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1159 * the maximum number of payload buffers associated per Multidata. 1160 */ 1161 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1162 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1163 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1164 1165 /* Round up the value to the nearest mss. */ 1166 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1167 1168 /* 1169 * Set ECN capable transport (ECT) code point in IP header. 1170 * 1171 * Note that there are 2 ECT code points '01' and '10', which are called 1172 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1173 * point ECT(0) for TCP as described in RFC 2481. 1174 */ 1175 #define SET_ECT(tcp, iph) \ 1176 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1177 /* We need to clear the code point first. */ \ 1178 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1179 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1180 } else { \ 1181 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1182 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1183 } 1184 1185 /* 1186 * The format argument to pass to tcp_display(). 1187 * DISP_PORT_ONLY means that the returned string has only port info. 1188 * DISP_ADDR_AND_PORT means that the returned string also contains the 1189 * remote and local IP address. 1190 */ 1191 #define DISP_PORT_ONLY 1 1192 #define DISP_ADDR_AND_PORT 2 1193 1194 #define IS_VMLOANED_MBLK(mp) \ 1195 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1196 1197 1198 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1199 boolean_t tcp_mdt_chain = B_TRUE; 1200 1201 /* 1202 * MDT threshold in the form of effective send MSS multiplier; we take 1203 * the MDT path if the amount of unsent data exceeds the threshold value 1204 * (default threshold is 1*SMSS). 1205 */ 1206 uint_t tcp_mdt_smss_threshold = 1; 1207 1208 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1209 1210 /* 1211 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1212 * tunable settable via NDD. Otherwise, the per-connection behavior is 1213 * determined dynamically during tcp_adapt_ire(), which is the default. 1214 */ 1215 boolean_t tcp_static_maxpsz = B_FALSE; 1216 1217 /* Setable in /etc/system */ 1218 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1219 uint32_t tcp_random_anon_port = 1; 1220 1221 /* 1222 * To reach to an eager in Q0 which can be dropped due to an incoming 1223 * new SYN request when Q0 is full, a new doubly linked list is 1224 * introduced. This list allows to select an eager from Q0 in O(1) time. 1225 * This is needed to avoid spending too much time walking through the 1226 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1227 * this new list has to be a member of Q0. 1228 * This list is headed by listener's tcp_t. When the list is empty, 1229 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1230 * of listener's tcp_t point to listener's tcp_t itself. 1231 * 1232 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1233 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1234 * These macros do not affect the eager's membership to Q0. 1235 */ 1236 1237 1238 #define MAKE_DROPPABLE(listener, eager) \ 1239 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1240 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1241 = (eager); \ 1242 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1243 (eager)->tcp_eager_next_drop_q0 = \ 1244 (listener)->tcp_eager_next_drop_q0; \ 1245 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1246 } 1247 1248 #define MAKE_UNDROPPABLE(eager) \ 1249 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1250 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1251 = (eager)->tcp_eager_prev_drop_q0; \ 1252 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1253 = (eager)->tcp_eager_next_drop_q0; \ 1254 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1255 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1256 } 1257 1258 /* 1259 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1260 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1261 * data, TCP will not respond with an ACK. RFC 793 requires that 1262 * TCP responds with an ACK for such a bogus ACK. By not following 1263 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1264 * an attacker successfully spoofs an acceptable segment to our 1265 * peer; or when our peer is "confused." 1266 */ 1267 uint32_t tcp_drop_ack_unsent_cnt = 10; 1268 1269 /* 1270 * Hook functions to enable cluster networking 1271 * On non-clustered systems these vectors must always be NULL. 1272 */ 1273 1274 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1275 sa_family_t addr_family, uint8_t *laddrp, 1276 in_port_t lport, void *args) = NULL; 1277 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1278 sa_family_t addr_family, uint8_t *laddrp, 1279 in_port_t lport, void *args) = NULL; 1280 1281 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1282 boolean_t is_outgoing, 1283 sa_family_t addr_family, 1284 uint8_t *laddrp, in_port_t lport, 1285 uint8_t *faddrp, in_port_t fport, 1286 void *args) = NULL; 1287 1288 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1289 sa_family_t addr_family, uint8_t *laddrp, 1290 in_port_t lport, uint8_t *faddrp, 1291 in_port_t fport, void *args) = NULL; 1292 1293 /* 1294 * The following are defined in ip.c 1295 */ 1296 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1297 sa_family_t addr_family, uint8_t *laddrp, 1298 void *args); 1299 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1300 sa_family_t addr_family, uint8_t *laddrp, 1301 uint8_t *faddrp, void *args); 1302 1303 1304 /* 1305 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1306 */ 1307 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1308 (err) = 0; \ 1309 if (cl_inet_connect2 != NULL) { \ 1310 /* \ 1311 * Running in cluster mode - register active connection \ 1312 * information \ 1313 */ \ 1314 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1315 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1316 (err) = (*cl_inet_connect2)( \ 1317 (connp)->conn_netstack->netstack_stackid,\ 1318 IPPROTO_TCP, is_outgoing, AF_INET, \ 1319 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1320 (in_port_t)(tcp)->tcp_lport, \ 1321 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1322 (in_port_t)(tcp)->tcp_fport, NULL); \ 1323 } \ 1324 } else { \ 1325 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1326 &(tcp)->tcp_ip6h->ip6_src)) { \ 1327 (err) = (*cl_inet_connect2)( \ 1328 (connp)->conn_netstack->netstack_stackid,\ 1329 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1330 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1331 (in_port_t)(tcp)->tcp_lport, \ 1332 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1333 (in_port_t)(tcp)->tcp_fport, NULL); \ 1334 } \ 1335 } \ 1336 } \ 1337 } 1338 1339 #define CL_INET_DISCONNECT(connp, tcp) { \ 1340 if (cl_inet_disconnect != NULL) { \ 1341 /* \ 1342 * Running in cluster mode - deregister active \ 1343 * connection information \ 1344 */ \ 1345 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1346 if ((tcp)->tcp_ip_src != 0) { \ 1347 (*cl_inet_disconnect)( \ 1348 (connp)->conn_netstack->netstack_stackid,\ 1349 IPPROTO_TCP, AF_INET, \ 1350 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1351 (in_port_t)(tcp)->tcp_lport, \ 1352 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1353 (in_port_t)(tcp)->tcp_fport, NULL); \ 1354 } \ 1355 } else { \ 1356 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1357 &(tcp)->tcp_ip_src_v6)) { \ 1358 (*cl_inet_disconnect)( \ 1359 (connp)->conn_netstack->netstack_stackid,\ 1360 IPPROTO_TCP, AF_INET6, \ 1361 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1362 (in_port_t)(tcp)->tcp_lport, \ 1363 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1364 (in_port_t)(tcp)->tcp_fport, NULL); \ 1365 } \ 1366 } \ 1367 } \ 1368 } 1369 1370 /* 1371 * Cluster networking hook for traversing current connection list. 1372 * This routine is used to extract the current list of live connections 1373 * which must continue to to be dispatched to this node. 1374 */ 1375 int cl_tcp_walk_list(netstackid_t stack_id, 1376 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1377 1378 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1379 void *arg, tcp_stack_t *tcps); 1380 1381 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1382 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1383 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1384 ip6_t *, ip6h, int, 0); 1385 1386 /* 1387 * Figure out the value of window scale opton. Note that the rwnd is 1388 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1389 * We cannot find the scale value and then do a round up of tcp_rwnd 1390 * because the scale value may not be correct after that. 1391 * 1392 * Set the compiler flag to make this function inline. 1393 */ 1394 static void 1395 tcp_set_ws_value(tcp_t *tcp) 1396 { 1397 int i; 1398 uint32_t rwnd = tcp->tcp_rwnd; 1399 1400 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1401 i++, rwnd >>= 1) 1402 ; 1403 tcp->tcp_rcv_ws = i; 1404 } 1405 1406 /* 1407 * Remove a connection from the list of detached TIME_WAIT connections. 1408 * It returns B_FALSE if it can't remove the connection from the list 1409 * as the connection has already been removed from the list due to an 1410 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1411 */ 1412 static boolean_t 1413 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1414 { 1415 boolean_t locked = B_FALSE; 1416 1417 if (tcp_time_wait == NULL) { 1418 tcp_time_wait = *((tcp_squeue_priv_t **) 1419 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1420 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1421 locked = B_TRUE; 1422 } else { 1423 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1424 } 1425 1426 if (tcp->tcp_time_wait_expire == 0) { 1427 ASSERT(tcp->tcp_time_wait_next == NULL); 1428 ASSERT(tcp->tcp_time_wait_prev == NULL); 1429 if (locked) 1430 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1431 return (B_FALSE); 1432 } 1433 ASSERT(TCP_IS_DETACHED(tcp)); 1434 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1435 1436 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1437 ASSERT(tcp->tcp_time_wait_prev == NULL); 1438 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1439 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1440 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1441 NULL; 1442 } else { 1443 tcp_time_wait->tcp_time_wait_tail = NULL; 1444 } 1445 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1446 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1447 ASSERT(tcp->tcp_time_wait_next == NULL); 1448 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1449 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1450 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1451 } else { 1452 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1453 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1454 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1455 tcp->tcp_time_wait_next; 1456 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1457 tcp->tcp_time_wait_prev; 1458 } 1459 tcp->tcp_time_wait_next = NULL; 1460 tcp->tcp_time_wait_prev = NULL; 1461 tcp->tcp_time_wait_expire = 0; 1462 1463 if (locked) 1464 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1465 return (B_TRUE); 1466 } 1467 1468 /* 1469 * Add a connection to the list of detached TIME_WAIT connections 1470 * and set its time to expire. 1471 */ 1472 static void 1473 tcp_time_wait_append(tcp_t *tcp) 1474 { 1475 tcp_stack_t *tcps = tcp->tcp_tcps; 1476 tcp_squeue_priv_t *tcp_time_wait = 1477 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1478 SQPRIVATE_TCP)); 1479 1480 tcp_timers_stop(tcp); 1481 1482 /* Freed above */ 1483 ASSERT(tcp->tcp_timer_tid == 0); 1484 ASSERT(tcp->tcp_ack_tid == 0); 1485 1486 /* must have happened at the time of detaching the tcp */ 1487 ASSERT(tcp->tcp_ptpahn == NULL); 1488 ASSERT(tcp->tcp_flow_stopped == 0); 1489 ASSERT(tcp->tcp_time_wait_next == NULL); 1490 ASSERT(tcp->tcp_time_wait_prev == NULL); 1491 ASSERT(tcp->tcp_time_wait_expire == NULL); 1492 ASSERT(tcp->tcp_listener == NULL); 1493 1494 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1495 /* 1496 * The value computed below in tcp->tcp_time_wait_expire may 1497 * appear negative or wrap around. That is ok since our 1498 * interest is only in the difference between the current lbolt 1499 * value and tcp->tcp_time_wait_expire. But the value should not 1500 * be zero, since it means the tcp is not in the TIME_WAIT list. 1501 * The corresponding comparison in tcp_time_wait_collector() uses 1502 * modular arithmetic. 1503 */ 1504 tcp->tcp_time_wait_expire += 1505 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1506 if (tcp->tcp_time_wait_expire == 0) 1507 tcp->tcp_time_wait_expire = 1; 1508 1509 ASSERT(TCP_IS_DETACHED(tcp)); 1510 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1511 ASSERT(tcp->tcp_time_wait_next == NULL); 1512 ASSERT(tcp->tcp_time_wait_prev == NULL); 1513 TCP_DBGSTAT(tcps, tcp_time_wait); 1514 1515 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1516 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1517 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1518 tcp_time_wait->tcp_time_wait_head = tcp; 1519 } else { 1520 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1521 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1522 TCPS_TIME_WAIT); 1523 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1524 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1525 } 1526 tcp_time_wait->tcp_time_wait_tail = tcp; 1527 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1528 } 1529 1530 /* ARGSUSED */ 1531 void 1532 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1533 { 1534 conn_t *connp = (conn_t *)arg; 1535 tcp_t *tcp = connp->conn_tcp; 1536 tcp_stack_t *tcps = tcp->tcp_tcps; 1537 1538 ASSERT(tcp != NULL); 1539 if (tcp->tcp_state == TCPS_CLOSED) { 1540 return; 1541 } 1542 1543 ASSERT((tcp->tcp_family == AF_INET && 1544 tcp->tcp_ipversion == IPV4_VERSION) || 1545 (tcp->tcp_family == AF_INET6 && 1546 (tcp->tcp_ipversion == IPV4_VERSION || 1547 tcp->tcp_ipversion == IPV6_VERSION))); 1548 ASSERT(!tcp->tcp_listener); 1549 1550 TCP_STAT(tcps, tcp_time_wait_reap); 1551 ASSERT(TCP_IS_DETACHED(tcp)); 1552 1553 /* 1554 * Because they have no upstream client to rebind or tcp_close() 1555 * them later, we axe the connection here and now. 1556 */ 1557 tcp_close_detached(tcp); 1558 } 1559 1560 /* 1561 * Remove cached/latched IPsec references. 1562 */ 1563 void 1564 tcp_ipsec_cleanup(tcp_t *tcp) 1565 { 1566 conn_t *connp = tcp->tcp_connp; 1567 1568 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1569 1570 if (connp->conn_latch != NULL) { 1571 IPLATCH_REFRELE(connp->conn_latch, 1572 connp->conn_netstack); 1573 connp->conn_latch = NULL; 1574 } 1575 if (connp->conn_policy != NULL) { 1576 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1577 connp->conn_policy = NULL; 1578 } 1579 } 1580 1581 /* 1582 * Cleaup before placing on free list. 1583 * Disassociate from the netstack/tcp_stack_t since the freelist 1584 * is per squeue and not per netstack. 1585 */ 1586 void 1587 tcp_cleanup(tcp_t *tcp) 1588 { 1589 mblk_t *mp; 1590 char *tcp_iphc; 1591 int tcp_iphc_len; 1592 int tcp_hdr_grown; 1593 tcp_sack_info_t *tcp_sack_info; 1594 conn_t *connp = tcp->tcp_connp; 1595 tcp_stack_t *tcps = tcp->tcp_tcps; 1596 netstack_t *ns = tcps->tcps_netstack; 1597 mblk_t *tcp_rsrv_mp; 1598 1599 tcp_bind_hash_remove(tcp); 1600 1601 /* Cleanup that which needs the netstack first */ 1602 tcp_ipsec_cleanup(tcp); 1603 1604 tcp_free(tcp); 1605 1606 /* Release any SSL context */ 1607 if (tcp->tcp_kssl_ent != NULL) { 1608 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1609 tcp->tcp_kssl_ent = NULL; 1610 } 1611 1612 if (tcp->tcp_kssl_ctx != NULL) { 1613 kssl_release_ctx(tcp->tcp_kssl_ctx); 1614 tcp->tcp_kssl_ctx = NULL; 1615 } 1616 tcp->tcp_kssl_pending = B_FALSE; 1617 1618 conn_delete_ire(connp, NULL); 1619 1620 /* 1621 * Since we will bzero the entire structure, we need to 1622 * remove it and reinsert it in global hash list. We 1623 * know the walkers can't get to this conn because we 1624 * had set CONDEMNED flag earlier and checked reference 1625 * under conn_lock so walker won't pick it and when we 1626 * go the ipcl_globalhash_remove() below, no walker 1627 * can get to it. 1628 */ 1629 ipcl_globalhash_remove(connp); 1630 1631 /* 1632 * Now it is safe to decrement the reference counts. 1633 * This might be the last reference on the netstack and TCPS 1634 * in which case it will cause the tcp_g_q_close and 1635 * the freeing of the IP Instance. 1636 */ 1637 connp->conn_netstack = NULL; 1638 netstack_rele(ns); 1639 ASSERT(tcps != NULL); 1640 tcp->tcp_tcps = NULL; 1641 TCPS_REFRELE(tcps); 1642 1643 /* Save some state */ 1644 mp = tcp->tcp_timercache; 1645 1646 tcp_sack_info = tcp->tcp_sack_info; 1647 tcp_iphc = tcp->tcp_iphc; 1648 tcp_iphc_len = tcp->tcp_iphc_len; 1649 tcp_hdr_grown = tcp->tcp_hdr_grown; 1650 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1651 1652 if (connp->conn_cred != NULL) { 1653 crfree(connp->conn_cred); 1654 connp->conn_cred = NULL; 1655 } 1656 if (connp->conn_effective_cred != NULL) { 1657 crfree(connp->conn_effective_cred); 1658 connp->conn_effective_cred = NULL; 1659 } 1660 ipcl_conn_cleanup(connp); 1661 connp->conn_flags = IPCL_TCPCONN; 1662 bzero(tcp, sizeof (tcp_t)); 1663 1664 /* restore the state */ 1665 tcp->tcp_timercache = mp; 1666 1667 tcp->tcp_sack_info = tcp_sack_info; 1668 tcp->tcp_iphc = tcp_iphc; 1669 tcp->tcp_iphc_len = tcp_iphc_len; 1670 tcp->tcp_hdr_grown = tcp_hdr_grown; 1671 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1672 1673 tcp->tcp_connp = connp; 1674 1675 ASSERT(connp->conn_tcp == tcp); 1676 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1677 connp->conn_state_flags = CONN_INCIPIENT; 1678 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1679 ASSERT(connp->conn_ref == 1); 1680 } 1681 1682 /* 1683 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1684 * is done forwards from the head. 1685 * This walks all stack instances since 1686 * tcp_time_wait remains global across all stacks. 1687 */ 1688 /* ARGSUSED */ 1689 void 1690 tcp_time_wait_collector(void *arg) 1691 { 1692 tcp_t *tcp; 1693 clock_t now; 1694 mblk_t *mp; 1695 conn_t *connp; 1696 kmutex_t *lock; 1697 boolean_t removed; 1698 1699 squeue_t *sqp = (squeue_t *)arg; 1700 tcp_squeue_priv_t *tcp_time_wait = 1701 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1702 1703 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1704 tcp_time_wait->tcp_time_wait_tid = 0; 1705 1706 if (tcp_time_wait->tcp_free_list != NULL && 1707 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1708 TCP_G_STAT(tcp_freelist_cleanup); 1709 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1710 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1711 tcp->tcp_time_wait_next = NULL; 1712 tcp_time_wait->tcp_free_list_cnt--; 1713 ASSERT(tcp->tcp_tcps == NULL); 1714 CONN_DEC_REF(tcp->tcp_connp); 1715 } 1716 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1717 } 1718 1719 /* 1720 * In order to reap time waits reliably, we should use a 1721 * source of time that is not adjustable by the user -- hence 1722 * the call to ddi_get_lbolt(). 1723 */ 1724 now = ddi_get_lbolt(); 1725 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1726 /* 1727 * Compare times using modular arithmetic, since 1728 * lbolt can wrapover. 1729 */ 1730 if ((now - tcp->tcp_time_wait_expire) < 0) { 1731 break; 1732 } 1733 1734 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1735 ASSERT(removed); 1736 1737 connp = tcp->tcp_connp; 1738 ASSERT(connp->conn_fanout != NULL); 1739 lock = &connp->conn_fanout->connf_lock; 1740 /* 1741 * This is essentially a TW reclaim fast path optimization for 1742 * performance where the timewait collector checks under the 1743 * fanout lock (so that no one else can get access to the 1744 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1745 * the classifier hash list. If ref count is indeed 2, we can 1746 * just remove the conn under the fanout lock and avoid 1747 * cleaning up the conn under the squeue, provided that 1748 * clustering callbacks are not enabled. If clustering is 1749 * enabled, we need to make the clustering callback before 1750 * setting the CONDEMNED flag and after dropping all locks and 1751 * so we forego this optimization and fall back to the slow 1752 * path. Also please see the comments in tcp_closei_local 1753 * regarding the refcnt logic. 1754 * 1755 * Since we are holding the tcp_time_wait_lock, its better 1756 * not to block on the fanout_lock because other connections 1757 * can't add themselves to time_wait list. So we do a 1758 * tryenter instead of mutex_enter. 1759 */ 1760 if (mutex_tryenter(lock)) { 1761 mutex_enter(&connp->conn_lock); 1762 if ((connp->conn_ref == 2) && 1763 (cl_inet_disconnect == NULL)) { 1764 ipcl_hash_remove_locked(connp, 1765 connp->conn_fanout); 1766 /* 1767 * Set the CONDEMNED flag now itself so that 1768 * the refcnt cannot increase due to any 1769 * walker. But we have still not cleaned up 1770 * conn_ire_cache. This is still ok since 1771 * we are going to clean it up in tcp_cleanup 1772 * immediately and any interface unplumb 1773 * thread will wait till the ire is blown away 1774 */ 1775 connp->conn_state_flags |= CONN_CONDEMNED; 1776 mutex_exit(lock); 1777 mutex_exit(&connp->conn_lock); 1778 if (tcp_time_wait->tcp_free_list_cnt < 1779 tcp_free_list_max_cnt) { 1780 /* Add to head of tcp_free_list */ 1781 mutex_exit( 1782 &tcp_time_wait->tcp_time_wait_lock); 1783 tcp_cleanup(tcp); 1784 ASSERT(connp->conn_latch == NULL); 1785 ASSERT(connp->conn_policy == NULL); 1786 ASSERT(tcp->tcp_tcps == NULL); 1787 ASSERT(connp->conn_netstack == NULL); 1788 1789 mutex_enter( 1790 &tcp_time_wait->tcp_time_wait_lock); 1791 tcp->tcp_time_wait_next = 1792 tcp_time_wait->tcp_free_list; 1793 tcp_time_wait->tcp_free_list = tcp; 1794 tcp_time_wait->tcp_free_list_cnt++; 1795 continue; 1796 } else { 1797 /* Do not add to tcp_free_list */ 1798 mutex_exit( 1799 &tcp_time_wait->tcp_time_wait_lock); 1800 tcp_bind_hash_remove(tcp); 1801 conn_delete_ire(tcp->tcp_connp, NULL); 1802 tcp_ipsec_cleanup(tcp); 1803 CONN_DEC_REF(tcp->tcp_connp); 1804 } 1805 } else { 1806 CONN_INC_REF_LOCKED(connp); 1807 mutex_exit(lock); 1808 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1809 mutex_exit(&connp->conn_lock); 1810 /* 1811 * We can reuse the closemp here since conn has 1812 * detached (otherwise we wouldn't even be in 1813 * time_wait list). tcp_closemp_used can safely 1814 * be changed without taking a lock as no other 1815 * thread can concurrently access it at this 1816 * point in the connection lifecycle. 1817 */ 1818 1819 if (tcp->tcp_closemp.b_prev == NULL) 1820 tcp->tcp_closemp_used = B_TRUE; 1821 else 1822 cmn_err(CE_PANIC, 1823 "tcp_timewait_collector: " 1824 "concurrent use of tcp_closemp: " 1825 "connp %p tcp %p\n", (void *)connp, 1826 (void *)tcp); 1827 1828 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1829 mp = &tcp->tcp_closemp; 1830 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1831 tcp_timewait_output, connp, 1832 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1833 } 1834 } else { 1835 mutex_enter(&connp->conn_lock); 1836 CONN_INC_REF_LOCKED(connp); 1837 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1838 mutex_exit(&connp->conn_lock); 1839 /* 1840 * We can reuse the closemp here since conn has 1841 * detached (otherwise we wouldn't even be in 1842 * time_wait list). tcp_closemp_used can safely 1843 * be changed without taking a lock as no other 1844 * thread can concurrently access it at this 1845 * point in the connection lifecycle. 1846 */ 1847 1848 if (tcp->tcp_closemp.b_prev == NULL) 1849 tcp->tcp_closemp_used = B_TRUE; 1850 else 1851 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1852 "concurrent use of tcp_closemp: " 1853 "connp %p tcp %p\n", (void *)connp, 1854 (void *)tcp); 1855 1856 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1857 mp = &tcp->tcp_closemp; 1858 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1859 tcp_timewait_output, connp, 1860 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1861 } 1862 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1863 } 1864 1865 if (tcp_time_wait->tcp_free_list != NULL) 1866 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1867 1868 tcp_time_wait->tcp_time_wait_tid = 1869 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1870 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1871 CALLOUT_FLAG_ROUNDUP); 1872 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1873 } 1874 1875 /* 1876 * Reply to a clients T_CONN_RES TPI message. This function 1877 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1878 * on the acceptor STREAM and processed in tcp_wput_accept(). 1879 * Read the block comment on top of tcp_conn_request(). 1880 */ 1881 static void 1882 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1883 { 1884 tcp_t *acceptor; 1885 tcp_t *eager; 1886 tcp_t *tcp; 1887 struct T_conn_res *tcr; 1888 t_uscalar_t acceptor_id; 1889 t_scalar_t seqnum; 1890 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1891 struct tcp_options *tcpopt; 1892 mblk_t *ok_mp; 1893 mblk_t *mp1; 1894 tcp_stack_t *tcps = listener->tcp_tcps; 1895 int error; 1896 1897 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1898 tcp_err_ack(listener, mp, TPROTO, 0); 1899 return; 1900 } 1901 tcr = (struct T_conn_res *)mp->b_rptr; 1902 1903 /* 1904 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1905 * read side queue of the streams device underneath us i.e. the 1906 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1907 * look it up in the queue_hash. Under LP64 it sends down the 1908 * minor_t of the accepting endpoint. 1909 * 1910 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1911 * fanout hash lock is held. 1912 * This prevents any thread from entering the acceptor queue from 1913 * below (since it has not been hard bound yet i.e. any inbound 1914 * packets will arrive on the listener or default tcp queue and 1915 * go through tcp_lookup). 1916 * The CONN_INC_REF will prevent the acceptor from closing. 1917 * 1918 * XXX It is still possible for a tli application to send down data 1919 * on the accepting stream while another thread calls t_accept. 1920 * This should not be a problem for well-behaved applications since 1921 * the T_OK_ACK is sent after the queue swapping is completed. 1922 * 1923 * If the accepting fd is the same as the listening fd, avoid 1924 * queue hash lookup since that will return an eager listener in a 1925 * already established state. 1926 */ 1927 acceptor_id = tcr->ACCEPTOR_id; 1928 mutex_enter(&listener->tcp_eager_lock); 1929 if (listener->tcp_acceptor_id == acceptor_id) { 1930 eager = listener->tcp_eager_next_q; 1931 /* only count how many T_CONN_INDs so don't count q0 */ 1932 if ((listener->tcp_conn_req_cnt_q != 1) || 1933 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1934 mutex_exit(&listener->tcp_eager_lock); 1935 tcp_err_ack(listener, mp, TBADF, 0); 1936 return; 1937 } 1938 if (listener->tcp_conn_req_cnt_q0 != 0) { 1939 /* Throw away all the eagers on q0. */ 1940 tcp_eager_cleanup(listener, 1); 1941 } 1942 if (listener->tcp_syn_defense) { 1943 listener->tcp_syn_defense = B_FALSE; 1944 if (listener->tcp_ip_addr_cache != NULL) { 1945 kmem_free(listener->tcp_ip_addr_cache, 1946 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1947 listener->tcp_ip_addr_cache = NULL; 1948 } 1949 } 1950 /* 1951 * Transfer tcp_conn_req_max to the eager so that when 1952 * a disconnect occurs we can revert the endpoint to the 1953 * listen state. 1954 */ 1955 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1956 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1957 /* 1958 * Get a reference on the acceptor just like the 1959 * tcp_acceptor_hash_lookup below. 1960 */ 1961 acceptor = listener; 1962 CONN_INC_REF(acceptor->tcp_connp); 1963 } else { 1964 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1965 if (acceptor == NULL) { 1966 if (listener->tcp_debug) { 1967 (void) strlog(TCP_MOD_ID, 0, 1, 1968 SL_ERROR|SL_TRACE, 1969 "tcp_accept: did not find acceptor 0x%x\n", 1970 acceptor_id); 1971 } 1972 mutex_exit(&listener->tcp_eager_lock); 1973 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1974 return; 1975 } 1976 /* 1977 * Verify acceptor state. The acceptable states for an acceptor 1978 * include TCPS_IDLE and TCPS_BOUND. 1979 */ 1980 switch (acceptor->tcp_state) { 1981 case TCPS_IDLE: 1982 /* FALLTHRU */ 1983 case TCPS_BOUND: 1984 break; 1985 default: 1986 CONN_DEC_REF(acceptor->tcp_connp); 1987 mutex_exit(&listener->tcp_eager_lock); 1988 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1989 return; 1990 } 1991 } 1992 1993 /* The listener must be in TCPS_LISTEN */ 1994 if (listener->tcp_state != TCPS_LISTEN) { 1995 CONN_DEC_REF(acceptor->tcp_connp); 1996 mutex_exit(&listener->tcp_eager_lock); 1997 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1998 return; 1999 } 2000 2001 /* 2002 * Rendezvous with an eager connection request packet hanging off 2003 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2004 * tcp structure when the connection packet arrived in 2005 * tcp_conn_request(). 2006 */ 2007 seqnum = tcr->SEQ_number; 2008 eager = listener; 2009 do { 2010 eager = eager->tcp_eager_next_q; 2011 if (eager == NULL) { 2012 CONN_DEC_REF(acceptor->tcp_connp); 2013 mutex_exit(&listener->tcp_eager_lock); 2014 tcp_err_ack(listener, mp, TBADSEQ, 0); 2015 return; 2016 } 2017 } while (eager->tcp_conn_req_seqnum != seqnum); 2018 mutex_exit(&listener->tcp_eager_lock); 2019 2020 /* 2021 * At this point, both acceptor and listener have 2 ref 2022 * that they begin with. Acceptor has one additional ref 2023 * we placed in lookup while listener has 3 additional 2024 * ref for being behind the squeue (tcp_accept() is 2025 * done on listener's squeue); being in classifier hash; 2026 * and eager's ref on listener. 2027 */ 2028 ASSERT(listener->tcp_connp->conn_ref >= 5); 2029 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2030 2031 /* 2032 * The eager at this point is set in its own squeue and 2033 * could easily have been killed (tcp_accept_finish will 2034 * deal with that) because of a TH_RST so we can only 2035 * ASSERT for a single ref. 2036 */ 2037 ASSERT(eager->tcp_connp->conn_ref >= 1); 2038 2039 /* Pre allocate the stroptions mblk also */ 2040 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2041 sizeof (struct T_conn_res)), BPRI_HI); 2042 if (opt_mp == NULL) { 2043 CONN_DEC_REF(acceptor->tcp_connp); 2044 CONN_DEC_REF(eager->tcp_connp); 2045 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2046 return; 2047 } 2048 DB_TYPE(opt_mp) = M_SETOPTS; 2049 opt_mp->b_wptr += sizeof (struct tcp_options); 2050 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2051 tcpopt->to_flags = 0; 2052 2053 /* 2054 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2055 * from listener to acceptor. 2056 */ 2057 if (listener->tcp_bound_if != 0) { 2058 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2059 tcpopt->to_boundif = listener->tcp_bound_if; 2060 } 2061 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2062 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2063 } 2064 2065 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2066 if ((mp1 = copymsg(mp)) == NULL) { 2067 CONN_DEC_REF(acceptor->tcp_connp); 2068 CONN_DEC_REF(eager->tcp_connp); 2069 freemsg(opt_mp); 2070 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2071 return; 2072 } 2073 2074 tcr = (struct T_conn_res *)mp1->b_rptr; 2075 2076 /* 2077 * This is an expanded version of mi_tpi_ok_ack_alloc() 2078 * which allocates a larger mblk and appends the new 2079 * local address to the ok_ack. The address is copied by 2080 * soaccept() for getsockname(). 2081 */ 2082 { 2083 int extra; 2084 2085 extra = (eager->tcp_family == AF_INET) ? 2086 sizeof (sin_t) : sizeof (sin6_t); 2087 2088 /* 2089 * Try to re-use mp, if possible. Otherwise, allocate 2090 * an mblk and return it as ok_mp. In any case, mp 2091 * is no longer usable upon return. 2092 */ 2093 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2094 CONN_DEC_REF(acceptor->tcp_connp); 2095 CONN_DEC_REF(eager->tcp_connp); 2096 freemsg(opt_mp); 2097 /* Original mp has been freed by now, so use mp1 */ 2098 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2099 return; 2100 } 2101 2102 mp = NULL; /* We should never use mp after this point */ 2103 2104 switch (extra) { 2105 case sizeof (sin_t): { 2106 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2107 2108 ok_mp->b_wptr += extra; 2109 sin->sin_family = AF_INET; 2110 sin->sin_port = eager->tcp_lport; 2111 sin->sin_addr.s_addr = 2112 eager->tcp_ipha->ipha_src; 2113 break; 2114 } 2115 case sizeof (sin6_t): { 2116 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2117 2118 ok_mp->b_wptr += extra; 2119 sin6->sin6_family = AF_INET6; 2120 sin6->sin6_port = eager->tcp_lport; 2121 if (eager->tcp_ipversion == IPV4_VERSION) { 2122 sin6->sin6_flowinfo = 0; 2123 IN6_IPADDR_TO_V4MAPPED( 2124 eager->tcp_ipha->ipha_src, 2125 &sin6->sin6_addr); 2126 } else { 2127 ASSERT(eager->tcp_ip6h != NULL); 2128 sin6->sin6_flowinfo = 2129 eager->tcp_ip6h->ip6_vcf & 2130 ~IPV6_VERS_AND_FLOW_MASK; 2131 sin6->sin6_addr = 2132 eager->tcp_ip6h->ip6_src; 2133 } 2134 sin6->sin6_scope_id = 0; 2135 sin6->__sin6_src_id = 0; 2136 break; 2137 } 2138 default: 2139 break; 2140 } 2141 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2142 } 2143 2144 /* 2145 * If there are no options we know that the T_CONN_RES will 2146 * succeed. However, we can't send the T_OK_ACK upstream until 2147 * the tcp_accept_swap is done since it would be dangerous to 2148 * let the application start using the new fd prior to the swap. 2149 */ 2150 error = tcp_accept_swap(listener, acceptor, eager); 2151 if (error != 0) { 2152 CONN_DEC_REF(acceptor->tcp_connp); 2153 CONN_DEC_REF(eager->tcp_connp); 2154 freemsg(ok_mp); 2155 /* Original mp has been freed by now, so use mp1 */ 2156 tcp_err_ack(listener, mp1, TSYSERR, error); 2157 return; 2158 } 2159 2160 /* 2161 * tcp_accept_swap unlinks eager from listener but does not drop 2162 * the eager's reference on the listener. 2163 */ 2164 ASSERT(eager->tcp_listener == NULL); 2165 ASSERT(listener->tcp_connp->conn_ref >= 5); 2166 2167 /* 2168 * The eager is now associated with its own queue. Insert in 2169 * the hash so that the connection can be reused for a future 2170 * T_CONN_RES. 2171 */ 2172 tcp_acceptor_hash_insert(acceptor_id, eager); 2173 2174 /* 2175 * We now do the processing of options with T_CONN_RES. 2176 * We delay till now since we wanted to have queue to pass to 2177 * option processing routines that points back to the right 2178 * instance structure which does not happen until after 2179 * tcp_accept_swap(). 2180 * 2181 * Note: 2182 * The sanity of the logic here assumes that whatever options 2183 * are appropriate to inherit from listner=>eager are done 2184 * before this point, and whatever were to be overridden (or not) 2185 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2186 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2187 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2188 * This may not be true at this point in time but can be fixed 2189 * independently. This option processing code starts with 2190 * the instantiated acceptor instance and the final queue at 2191 * this point. 2192 */ 2193 2194 if (tcr->OPT_length != 0) { 2195 /* Options to process */ 2196 int t_error = 0; 2197 int sys_error = 0; 2198 int do_disconnect = 0; 2199 2200 if (tcp_conprim_opt_process(eager, mp1, 2201 &do_disconnect, &t_error, &sys_error) < 0) { 2202 eager->tcp_accept_error = 1; 2203 if (do_disconnect) { 2204 /* 2205 * An option failed which does not allow 2206 * connection to be accepted. 2207 * 2208 * We allow T_CONN_RES to succeed and 2209 * put a T_DISCON_IND on the eager queue. 2210 */ 2211 ASSERT(t_error == 0 && sys_error == 0); 2212 eager->tcp_send_discon_ind = 1; 2213 } else { 2214 ASSERT(t_error != 0); 2215 freemsg(ok_mp); 2216 /* 2217 * Original mp was either freed or set 2218 * to ok_mp above, so use mp1 instead. 2219 */ 2220 tcp_err_ack(listener, mp1, t_error, sys_error); 2221 goto finish; 2222 } 2223 } 2224 /* 2225 * Most likely success in setting options (except if 2226 * eager->tcp_send_discon_ind set). 2227 * mp1 option buffer represented by OPT_length/offset 2228 * potentially modified and contains results of setting 2229 * options at this point 2230 */ 2231 } 2232 2233 /* We no longer need mp1, since all options processing has passed */ 2234 freemsg(mp1); 2235 2236 putnext(listener->tcp_rq, ok_mp); 2237 2238 mutex_enter(&listener->tcp_eager_lock); 2239 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2240 tcp_t *tail; 2241 mblk_t *conn_ind; 2242 2243 /* 2244 * This path should not be executed if listener and 2245 * acceptor streams are the same. 2246 */ 2247 ASSERT(listener != acceptor); 2248 2249 tcp = listener->tcp_eager_prev_q0; 2250 /* 2251 * listener->tcp_eager_prev_q0 points to the TAIL of the 2252 * deferred T_conn_ind queue. We need to get to the head of 2253 * the queue in order to send up T_conn_ind the same order as 2254 * how the 3WHS is completed. 2255 */ 2256 while (tcp != listener) { 2257 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2258 break; 2259 else 2260 tcp = tcp->tcp_eager_prev_q0; 2261 } 2262 ASSERT(tcp != listener); 2263 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2264 ASSERT(conn_ind != NULL); 2265 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2266 2267 /* Move from q0 to q */ 2268 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2269 listener->tcp_conn_req_cnt_q0--; 2270 listener->tcp_conn_req_cnt_q++; 2271 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2272 tcp->tcp_eager_prev_q0; 2273 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2274 tcp->tcp_eager_next_q0; 2275 tcp->tcp_eager_prev_q0 = NULL; 2276 tcp->tcp_eager_next_q0 = NULL; 2277 tcp->tcp_conn_def_q0 = B_FALSE; 2278 2279 /* Make sure the tcp isn't in the list of droppables */ 2280 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2281 tcp->tcp_eager_prev_drop_q0 == NULL); 2282 2283 /* 2284 * Insert at end of the queue because sockfs sends 2285 * down T_CONN_RES in chronological order. Leaving 2286 * the older conn indications at front of the queue 2287 * helps reducing search time. 2288 */ 2289 tail = listener->tcp_eager_last_q; 2290 if (tail != NULL) 2291 tail->tcp_eager_next_q = tcp; 2292 else 2293 listener->tcp_eager_next_q = tcp; 2294 listener->tcp_eager_last_q = tcp; 2295 tcp->tcp_eager_next_q = NULL; 2296 mutex_exit(&listener->tcp_eager_lock); 2297 putnext(tcp->tcp_rq, conn_ind); 2298 } else { 2299 mutex_exit(&listener->tcp_eager_lock); 2300 } 2301 2302 /* 2303 * Done with the acceptor - free it 2304 * 2305 * Note: from this point on, no access to listener should be made 2306 * as listener can be equal to acceptor. 2307 */ 2308 finish: 2309 ASSERT(acceptor->tcp_detached); 2310 ASSERT(tcps->tcps_g_q != NULL); 2311 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2312 acceptor->tcp_rq = tcps->tcps_g_q; 2313 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2314 (void) tcp_clean_death(acceptor, 0, 2); 2315 CONN_DEC_REF(acceptor->tcp_connp); 2316 2317 /* 2318 * In case we already received a FIN we have to make tcp_rput send 2319 * the ordrel_ind. This will also send up a window update if the window 2320 * has opened up. 2321 * 2322 * In the normal case of a successful connection acceptance 2323 * we give the O_T_BIND_REQ to the read side put procedure as an 2324 * indication that this was just accepted. This tells tcp_rput to 2325 * pass up any data queued in tcp_rcv_list. 2326 * 2327 * In the fringe case where options sent with T_CONN_RES failed and 2328 * we required, we would be indicating a T_DISCON_IND to blow 2329 * away this connection. 2330 */ 2331 2332 /* 2333 * XXX: we currently have a problem if XTI application closes the 2334 * acceptor stream in between. This problem exists in on10-gate also 2335 * and is well know but nothing can be done short of major rewrite 2336 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2337 * eager same squeue as listener (we can distinguish non socket 2338 * listeners at the time of handling a SYN in tcp_conn_request) 2339 * and do most of the work that tcp_accept_finish does here itself 2340 * and then get behind the acceptor squeue to access the acceptor 2341 * queue. 2342 */ 2343 /* 2344 * We already have a ref on tcp so no need to do one before squeue_enter 2345 */ 2346 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2347 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2348 } 2349 2350 /* 2351 * Swap information between the eager and acceptor for a TLI/XTI client. 2352 * The sockfs accept is done on the acceptor stream and control goes 2353 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2354 * called. In either case, both the eager and listener are in their own 2355 * perimeter (squeue) and the code has to deal with potential race. 2356 * 2357 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2358 */ 2359 static int 2360 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2361 { 2362 conn_t *econnp, *aconnp; 2363 cred_t *effective_cred = NULL; 2364 2365 ASSERT(eager->tcp_rq == listener->tcp_rq); 2366 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2367 ASSERT(!eager->tcp_hard_bound); 2368 ASSERT(!TCP_IS_SOCKET(acceptor)); 2369 ASSERT(!TCP_IS_SOCKET(eager)); 2370 ASSERT(!TCP_IS_SOCKET(listener)); 2371 2372 econnp = eager->tcp_connp; 2373 aconnp = acceptor->tcp_connp; 2374 2375 /* 2376 * Trusted Extensions may need to use a security label that is 2377 * different from the acceptor's label on MLP and MAC-Exempt 2378 * sockets. If this is the case, the required security label 2379 * already exists in econnp->conn_effective_cred. Use this label 2380 * to generate a new effective cred for the acceptor. 2381 * 2382 * We allow for potential application level retry attempts by 2383 * checking for transient errors before modifying eager. 2384 */ 2385 if (is_system_labeled() && 2386 aconnp->conn_cred != NULL && econnp->conn_effective_cred != NULL) { 2387 effective_cred = copycred_from_tslabel(aconnp->conn_cred, 2388 crgetlabel(econnp->conn_effective_cred), KM_NOSLEEP); 2389 if (effective_cred == NULL) 2390 return (ENOMEM); 2391 } 2392 2393 acceptor->tcp_detached = B_TRUE; 2394 /* 2395 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2396 * the acceptor id. 2397 */ 2398 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2399 2400 /* remove eager from listen list... */ 2401 mutex_enter(&listener->tcp_eager_lock); 2402 tcp_eager_unlink(eager); 2403 ASSERT(eager->tcp_eager_next_q == NULL && 2404 eager->tcp_eager_last_q == NULL); 2405 ASSERT(eager->tcp_eager_next_q0 == NULL && 2406 eager->tcp_eager_prev_q0 == NULL); 2407 mutex_exit(&listener->tcp_eager_lock); 2408 eager->tcp_rq = acceptor->tcp_rq; 2409 eager->tcp_wq = acceptor->tcp_wq; 2410 2411 eager->tcp_rq->q_ptr = econnp; 2412 eager->tcp_wq->q_ptr = econnp; 2413 2414 /* 2415 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2416 * which might be a different squeue from our peer TCP instance. 2417 * For TCP Fusion, the peer expects that whenever tcp_detached is 2418 * clear, our TCP queues point to the acceptor's queues. Thus, use 2419 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2420 * above reach global visibility prior to the clearing of tcp_detached. 2421 */ 2422 membar_producer(); 2423 eager->tcp_detached = B_FALSE; 2424 2425 ASSERT(eager->tcp_ack_tid == 0); 2426 2427 econnp->conn_dev = aconnp->conn_dev; 2428 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2429 2430 ASSERT(econnp->conn_minor_arena != NULL); 2431 if (eager->tcp_cred != NULL) 2432 crfree(eager->tcp_cred); 2433 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2434 if (econnp->conn_effective_cred != NULL) 2435 crfree(econnp->conn_effective_cred); 2436 econnp->conn_effective_cred = effective_cred; 2437 aconnp->conn_cred = NULL; 2438 ASSERT(aconnp->conn_effective_cred == NULL); 2439 2440 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2441 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2442 2443 econnp->conn_zoneid = aconnp->conn_zoneid; 2444 econnp->conn_allzones = aconnp->conn_allzones; 2445 2446 aconnp->conn_mac_exempt = B_FALSE; 2447 2448 /* Do the IPC initialization */ 2449 CONN_INC_REF(econnp); 2450 2451 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2452 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2453 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2454 2455 /* Done with old IPC. Drop its ref on its connp */ 2456 CONN_DEC_REF(aconnp); 2457 return (0); 2458 } 2459 2460 2461 /* 2462 * Adapt to the information, such as rtt and rtt_sd, provided from the 2463 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2464 * 2465 * Checks for multicast and broadcast destination address. 2466 * Returns zero on failure; non-zero if ok. 2467 * 2468 * Note that the MSS calculation here is based on the info given in 2469 * the IRE. We do not do any calculation based on TCP options. They 2470 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2471 * knows which options to use. 2472 * 2473 * Note on how TCP gets its parameters for a connection. 2474 * 2475 * When a tcp_t structure is allocated, it gets all the default parameters. 2476 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2477 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2478 * default. 2479 * 2480 * An incoming SYN with a multicast or broadcast destination address, is dropped 2481 * in 1 of 2 places. 2482 * 2483 * 1. If the packet was received over the wire it is dropped in 2484 * ip_rput_process_broadcast() 2485 * 2486 * 2. If the packet was received through internal IP loopback, i.e. the packet 2487 * was generated and received on the same machine, it is dropped in 2488 * ip_wput_local() 2489 * 2490 * An incoming SYN with a multicast or broadcast source address is always 2491 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2492 * reject an attempt to connect to a broadcast or multicast (destination) 2493 * address. 2494 */ 2495 static int 2496 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2497 { 2498 ire_t *ire; 2499 ire_t *sire = NULL; 2500 iulp_t *ire_uinfo = NULL; 2501 uint32_t mss_max; 2502 uint32_t mss; 2503 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2504 conn_t *connp = tcp->tcp_connp; 2505 boolean_t ire_cacheable = B_FALSE; 2506 zoneid_t zoneid = connp->conn_zoneid; 2507 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2508 MATCH_IRE_SECATTR; 2509 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2510 ill_t *ill = NULL; 2511 boolean_t incoming = (ire_mp == NULL); 2512 tcp_stack_t *tcps = tcp->tcp_tcps; 2513 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2514 2515 ASSERT(connp->conn_ire_cache == NULL); 2516 2517 if (tcp->tcp_ipversion == IPV4_VERSION) { 2518 2519 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2520 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2521 return (0); 2522 } 2523 /* 2524 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2525 * for the destination with the nexthop as gateway. 2526 * ire_ctable_lookup() is used because this particular 2527 * ire, if it exists, will be marked private. 2528 * If that is not available, use the interface ire 2529 * for the nexthop. 2530 * 2531 * TSol: tcp_update_label will detect label mismatches based 2532 * only on the destination's label, but that would not 2533 * detect label mismatches based on the security attributes 2534 * of routes or next hop gateway. Hence we need to pass the 2535 * label to ire_ftable_lookup below in order to locate the 2536 * right prefix (and/or) ire cache. Similarly we also need 2537 * pass the label to the ire_cache_lookup below to locate 2538 * the right ire that also matches on the label. 2539 */ 2540 if (tcp->tcp_connp->conn_nexthop_set) { 2541 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2542 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2543 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2544 ipst); 2545 if (ire == NULL) { 2546 ire = ire_ftable_lookup( 2547 tcp->tcp_connp->conn_nexthop_v4, 2548 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2549 tsl, match_flags, ipst); 2550 if (ire == NULL) 2551 return (0); 2552 } else { 2553 ire_uinfo = &ire->ire_uinfo; 2554 } 2555 } else { 2556 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2557 zoneid, tsl, ipst); 2558 if (ire != NULL) { 2559 ire_cacheable = B_TRUE; 2560 ire_uinfo = (ire_mp != NULL) ? 2561 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2562 &ire->ire_uinfo; 2563 2564 } else { 2565 if (ire_mp == NULL) { 2566 ire = ire_ftable_lookup( 2567 tcp->tcp_connp->conn_rem, 2568 0, 0, 0, NULL, &sire, zoneid, 0, 2569 tsl, (MATCH_IRE_RECURSIVE | 2570 MATCH_IRE_DEFAULT), ipst); 2571 if (ire == NULL) 2572 return (0); 2573 ire_uinfo = (sire != NULL) ? 2574 &sire->ire_uinfo : 2575 &ire->ire_uinfo; 2576 } else { 2577 ire = (ire_t *)ire_mp->b_rptr; 2578 ire_uinfo = 2579 &((ire_t *) 2580 ire_mp->b_rptr)->ire_uinfo; 2581 } 2582 } 2583 } 2584 ASSERT(ire != NULL); 2585 2586 if ((ire->ire_src_addr == INADDR_ANY) || 2587 (ire->ire_type & IRE_BROADCAST)) { 2588 /* 2589 * ire->ire_mp is non null when ire_mp passed in is used 2590 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2591 */ 2592 if (ire->ire_mp == NULL) 2593 ire_refrele(ire); 2594 if (sire != NULL) 2595 ire_refrele(sire); 2596 return (0); 2597 } 2598 2599 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2600 ipaddr_t src_addr; 2601 2602 /* 2603 * ip_bind_connected() has stored the correct source 2604 * address in conn_src. 2605 */ 2606 src_addr = tcp->tcp_connp->conn_src; 2607 tcp->tcp_ipha->ipha_src = src_addr; 2608 /* 2609 * Copy of the src addr. in tcp_t is needed 2610 * for the lookup funcs. 2611 */ 2612 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2613 } 2614 /* 2615 * Set the fragment bit so that IP will tell us if the MTU 2616 * should change. IP tells us the latest setting of 2617 * ip_path_mtu_discovery through ire_frag_flag. 2618 */ 2619 if (ipst->ips_ip_path_mtu_discovery) { 2620 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2621 htons(IPH_DF); 2622 } 2623 /* 2624 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2625 * for IP_NEXTHOP. No cache ire has been found for the 2626 * destination and we are working with the nexthop's 2627 * interface ire. Since we need to forward all packets 2628 * to the nexthop first, we "blindly" set tcp_localnet 2629 * to false, eventhough the destination may also be 2630 * onlink. 2631 */ 2632 if (ire_uinfo == NULL) 2633 tcp->tcp_localnet = 0; 2634 else 2635 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2636 } else { 2637 /* 2638 * For incoming connection ire_mp = NULL 2639 * For outgoing connection ire_mp != NULL 2640 * Technically we should check conn_incoming_ill 2641 * when ire_mp is NULL and conn_outgoing_ill when 2642 * ire_mp is non-NULL. But this is performance 2643 * critical path and for IPV*_BOUND_IF, outgoing 2644 * and incoming ill are always set to the same value. 2645 */ 2646 ill_t *dst_ill = NULL; 2647 ipif_t *dst_ipif = NULL; 2648 2649 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2650 2651 if (connp->conn_outgoing_ill != NULL) { 2652 /* Outgoing or incoming path */ 2653 int err; 2654 2655 dst_ill = conn_get_held_ill(connp, 2656 &connp->conn_outgoing_ill, &err); 2657 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2658 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2659 return (0); 2660 } 2661 match_flags |= MATCH_IRE_ILL; 2662 dst_ipif = dst_ill->ill_ipif; 2663 } 2664 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2665 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2666 2667 if (ire != NULL) { 2668 ire_cacheable = B_TRUE; 2669 ire_uinfo = (ire_mp != NULL) ? 2670 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2671 &ire->ire_uinfo; 2672 } else { 2673 if (ire_mp == NULL) { 2674 ire = ire_ftable_lookup_v6( 2675 &tcp->tcp_connp->conn_remv6, 2676 0, 0, 0, dst_ipif, &sire, zoneid, 2677 0, tsl, match_flags, ipst); 2678 if (ire == NULL) { 2679 if (dst_ill != NULL) 2680 ill_refrele(dst_ill); 2681 return (0); 2682 } 2683 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2684 &ire->ire_uinfo; 2685 } else { 2686 ire = (ire_t *)ire_mp->b_rptr; 2687 ire_uinfo = 2688 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2689 } 2690 } 2691 if (dst_ill != NULL) 2692 ill_refrele(dst_ill); 2693 2694 ASSERT(ire != NULL); 2695 ASSERT(ire_uinfo != NULL); 2696 2697 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2698 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2699 /* 2700 * ire->ire_mp is non null when ire_mp passed in is used 2701 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2702 */ 2703 if (ire->ire_mp == NULL) 2704 ire_refrele(ire); 2705 if (sire != NULL) 2706 ire_refrele(sire); 2707 return (0); 2708 } 2709 2710 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2711 in6_addr_t src_addr; 2712 2713 /* 2714 * ip_bind_connected_v6() has stored the correct source 2715 * address per IPv6 addr. selection policy in 2716 * conn_src_v6. 2717 */ 2718 src_addr = tcp->tcp_connp->conn_srcv6; 2719 2720 tcp->tcp_ip6h->ip6_src = src_addr; 2721 /* 2722 * Copy of the src addr. in tcp_t is needed 2723 * for the lookup funcs. 2724 */ 2725 tcp->tcp_ip_src_v6 = src_addr; 2726 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2727 &connp->conn_srcv6)); 2728 } 2729 tcp->tcp_localnet = 2730 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2731 } 2732 2733 /* 2734 * This allows applications to fail quickly when connections are made 2735 * to dead hosts. Hosts can be labeled dead by adding a reject route 2736 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2737 */ 2738 if ((ire->ire_flags & RTF_REJECT) && 2739 (ire->ire_flags & RTF_PRIVATE)) 2740 goto error; 2741 2742 /* 2743 * Make use of the cached rtt and rtt_sd values to calculate the 2744 * initial RTO. Note that they are already initialized in 2745 * tcp_init_values(). 2746 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2747 * IP_NEXTHOP, but instead are using the interface ire for the 2748 * nexthop, then we do not use the ire_uinfo from that ire to 2749 * do any initializations. 2750 */ 2751 if (ire_uinfo != NULL) { 2752 if (ire_uinfo->iulp_rtt != 0) { 2753 clock_t rto; 2754 2755 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2756 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2757 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2758 tcps->tcps_rexmit_interval_extra + 2759 (tcp->tcp_rtt_sa >> 5); 2760 2761 if (rto > tcps->tcps_rexmit_interval_max) { 2762 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2763 } else if (rto < tcps->tcps_rexmit_interval_min) { 2764 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2765 } else { 2766 tcp->tcp_rto = rto; 2767 } 2768 } 2769 if (ire_uinfo->iulp_ssthresh != 0) 2770 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2771 else 2772 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2773 if (ire_uinfo->iulp_spipe > 0) { 2774 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2775 tcps->tcps_max_buf); 2776 if (tcps->tcps_snd_lowat_fraction != 0) 2777 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2778 tcps->tcps_snd_lowat_fraction; 2779 (void) tcp_maxpsz_set(tcp, B_TRUE); 2780 } 2781 /* 2782 * Note that up till now, acceptor always inherits receive 2783 * window from the listener. But if there is a metrics 2784 * associated with a host, we should use that instead of 2785 * inheriting it from listener. Thus we need to pass this 2786 * info back to the caller. 2787 */ 2788 if (ire_uinfo->iulp_rpipe > 0) { 2789 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2790 tcps->tcps_max_buf); 2791 } 2792 2793 if (ire_uinfo->iulp_rtomax > 0) { 2794 tcp->tcp_second_timer_threshold = 2795 ire_uinfo->iulp_rtomax; 2796 } 2797 2798 /* 2799 * Use the metric option settings, iulp_tstamp_ok and 2800 * iulp_wscale_ok, only for active open. What this means 2801 * is that if the other side uses timestamp or window 2802 * scale option, TCP will also use those options. That 2803 * is for passive open. If the application sets a 2804 * large window, window scale is enabled regardless of 2805 * the value in iulp_wscale_ok. This is the behavior 2806 * since 2.6. So we keep it. 2807 * The only case left in passive open processing is the 2808 * check for SACK. 2809 * For ECN, it should probably be like SACK. But the 2810 * current value is binary, so we treat it like the other 2811 * cases. The metric only controls active open.For passive 2812 * open, the ndd param, tcp_ecn_permitted, controls the 2813 * behavior. 2814 */ 2815 if (!tcp_detached) { 2816 /* 2817 * The if check means that the following can only 2818 * be turned on by the metrics only IRE, but not off. 2819 */ 2820 if (ire_uinfo->iulp_tstamp_ok) 2821 tcp->tcp_snd_ts_ok = B_TRUE; 2822 if (ire_uinfo->iulp_wscale_ok) 2823 tcp->tcp_snd_ws_ok = B_TRUE; 2824 if (ire_uinfo->iulp_sack == 2) 2825 tcp->tcp_snd_sack_ok = B_TRUE; 2826 if (ire_uinfo->iulp_ecn_ok) 2827 tcp->tcp_ecn_ok = B_TRUE; 2828 } else { 2829 /* 2830 * Passive open. 2831 * 2832 * As above, the if check means that SACK can only be 2833 * turned on by the metric only IRE. 2834 */ 2835 if (ire_uinfo->iulp_sack > 0) { 2836 tcp->tcp_snd_sack_ok = B_TRUE; 2837 } 2838 } 2839 } 2840 2841 2842 /* 2843 * XXX: Note that currently, ire_max_frag can be as small as 68 2844 * because of PMTUd. So tcp_mss may go to negative if combined 2845 * length of all those options exceeds 28 bytes. But because 2846 * of the tcp_mss_min check below, we may not have a problem if 2847 * tcp_mss_min is of a reasonable value. The default is 1 so 2848 * the negative problem still exists. And the check defeats PMTUd. 2849 * In fact, if PMTUd finds that the MSS should be smaller than 2850 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2851 * value. 2852 * 2853 * We do not deal with that now. All those problems related to 2854 * PMTUd will be fixed later. 2855 */ 2856 ASSERT(ire->ire_max_frag != 0); 2857 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2858 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2859 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2860 mss = MIN(mss, IPV6_MIN_MTU); 2861 } 2862 } 2863 2864 /* Sanity check for MSS value. */ 2865 if (tcp->tcp_ipversion == IPV4_VERSION) 2866 mss_max = tcps->tcps_mss_max_ipv4; 2867 else 2868 mss_max = tcps->tcps_mss_max_ipv6; 2869 2870 if (tcp->tcp_ipversion == IPV6_VERSION && 2871 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2872 /* 2873 * After receiving an ICMPv6 "packet too big" message with a 2874 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2875 * will insert a 8-byte fragment header in every packet; we 2876 * reduce the MSS by that amount here. 2877 */ 2878 mss -= sizeof (ip6_frag_t); 2879 } 2880 2881 if (tcp->tcp_ipsec_overhead == 0) 2882 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2883 2884 mss -= tcp->tcp_ipsec_overhead; 2885 2886 if (mss < tcps->tcps_mss_min) 2887 mss = tcps->tcps_mss_min; 2888 if (mss > mss_max) 2889 mss = mss_max; 2890 2891 /* Note that this is the maximum MSS, excluding all options. */ 2892 tcp->tcp_mss = mss; 2893 2894 /* 2895 * Initialize the ISS here now that we have the full connection ID. 2896 * The RFC 1948 method of initial sequence number generation requires 2897 * knowledge of the full connection ID before setting the ISS. 2898 */ 2899 2900 tcp_iss_init(tcp); 2901 2902 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2903 tcp->tcp_loopback = B_TRUE; 2904 2905 if (sire != NULL) 2906 IRE_REFRELE(sire); 2907 2908 /* 2909 * If we got an IRE_CACHE and an ILL, go through their properties; 2910 * otherwise, this is deferred until later when we have an IRE_CACHE. 2911 */ 2912 if (tcp->tcp_loopback || 2913 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2914 /* 2915 * For incoming, see if this tcp may be MDT-capable. For 2916 * outgoing, this process has been taken care of through 2917 * tcp_rput_other. 2918 */ 2919 tcp_ire_ill_check(tcp, ire, ill, incoming); 2920 tcp->tcp_ire_ill_check_done = B_TRUE; 2921 } 2922 2923 mutex_enter(&connp->conn_lock); 2924 /* 2925 * Make sure that conn is not marked incipient 2926 * for incoming connections. A blind 2927 * removal of incipient flag is cheaper than 2928 * check and removal. 2929 */ 2930 connp->conn_state_flags &= ~CONN_INCIPIENT; 2931 2932 /* 2933 * Must not cache forwarding table routes 2934 * or recache an IRE after the conn_t has 2935 * had conn_ire_cache cleared and is flagged 2936 * unusable, (see the CONN_CACHE_IRE() macro). 2937 */ 2938 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2939 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2940 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2941 connp->conn_ire_cache = ire; 2942 IRE_UNTRACE_REF(ire); 2943 rw_exit(&ire->ire_bucket->irb_lock); 2944 mutex_exit(&connp->conn_lock); 2945 return (1); 2946 } 2947 rw_exit(&ire->ire_bucket->irb_lock); 2948 } 2949 mutex_exit(&connp->conn_lock); 2950 2951 if (ire->ire_mp == NULL) 2952 ire_refrele(ire); 2953 return (1); 2954 2955 error: 2956 if (ire->ire_mp == NULL) 2957 ire_refrele(ire); 2958 if (sire != NULL) 2959 ire_refrele(sire); 2960 return (0); 2961 } 2962 2963 static void 2964 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 2965 { 2966 int error; 2967 conn_t *connp = tcp->tcp_connp; 2968 struct sockaddr *sa; 2969 mblk_t *mp1; 2970 struct T_bind_req *tbr; 2971 int backlog; 2972 socklen_t len; 2973 sin_t *sin; 2974 sin6_t *sin6; 2975 cred_t *cr; 2976 2977 /* 2978 * All Solaris components should pass a db_credp 2979 * for this TPI message, hence we ASSERT. 2980 * But in case there is some other M_PROTO that looks 2981 * like a TPI message sent by some other kernel 2982 * component, we check and return an error. 2983 */ 2984 cr = msg_getcred(mp, NULL); 2985 ASSERT(cr != NULL); 2986 if (cr == NULL) { 2987 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 2988 return; 2989 } 2990 2991 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2992 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2993 if (tcp->tcp_debug) { 2994 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2995 "tcp_tpi_bind: bad req, len %u", 2996 (uint_t)(mp->b_wptr - mp->b_rptr)); 2997 } 2998 tcp_err_ack(tcp, mp, TPROTO, 0); 2999 return; 3000 } 3001 /* Make sure the largest address fits */ 3002 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 3003 if (mp1 == NULL) { 3004 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3005 return; 3006 } 3007 mp = mp1; 3008 tbr = (struct T_bind_req *)mp->b_rptr; 3009 3010 backlog = tbr->CONIND_number; 3011 len = tbr->ADDR_length; 3012 3013 switch (len) { 3014 case 0: /* request for a generic port */ 3015 tbr->ADDR_offset = sizeof (struct T_bind_req); 3016 if (tcp->tcp_family == AF_INET) { 3017 tbr->ADDR_length = sizeof (sin_t); 3018 sin = (sin_t *)&tbr[1]; 3019 *sin = sin_null; 3020 sin->sin_family = AF_INET; 3021 sa = (struct sockaddr *)sin; 3022 len = sizeof (sin_t); 3023 mp->b_wptr = (uchar_t *)&sin[1]; 3024 } else { 3025 ASSERT(tcp->tcp_family == AF_INET6); 3026 tbr->ADDR_length = sizeof (sin6_t); 3027 sin6 = (sin6_t *)&tbr[1]; 3028 *sin6 = sin6_null; 3029 sin6->sin6_family = AF_INET6; 3030 sa = (struct sockaddr *)sin6; 3031 len = sizeof (sin6_t); 3032 mp->b_wptr = (uchar_t *)&sin6[1]; 3033 } 3034 break; 3035 3036 case sizeof (sin_t): /* Complete IPv4 address */ 3037 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3038 sizeof (sin_t)); 3039 break; 3040 3041 case sizeof (sin6_t): /* Complete IPv6 address */ 3042 sa = (struct sockaddr *)mi_offset_param(mp, 3043 tbr->ADDR_offset, sizeof (sin6_t)); 3044 break; 3045 3046 default: 3047 if (tcp->tcp_debug) { 3048 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3049 "tcp_tpi_bind: bad address length, %d", 3050 tbr->ADDR_length); 3051 } 3052 tcp_err_ack(tcp, mp, TBADADDR, 0); 3053 return; 3054 } 3055 3056 if (backlog > 0) { 3057 error = tcp_do_listen(connp, sa, len, backlog, DB_CRED(mp), 3058 tbr->PRIM_type != O_T_BIND_REQ); 3059 } else { 3060 error = tcp_do_bind(connp, sa, len, DB_CRED(mp), 3061 tbr->PRIM_type != O_T_BIND_REQ); 3062 } 3063 done: 3064 if (error > 0) { 3065 tcp_err_ack(tcp, mp, TSYSERR, error); 3066 } else if (error < 0) { 3067 tcp_err_ack(tcp, mp, -error, 0); 3068 } else { 3069 /* 3070 * Update port information as sockfs/tpi needs it for checking 3071 */ 3072 if (tcp->tcp_family == AF_INET) { 3073 sin = (sin_t *)sa; 3074 sin->sin_port = tcp->tcp_lport; 3075 } else { 3076 sin6 = (sin6_t *)sa; 3077 sin6->sin6_port = tcp->tcp_lport; 3078 } 3079 mp->b_datap->db_type = M_PCPROTO; 3080 tbr->PRIM_type = T_BIND_ACK; 3081 putnext(tcp->tcp_rq, mp); 3082 } 3083 } 3084 3085 /* 3086 * If the "bind_to_req_port_only" parameter is set, if the requested port 3087 * number is available, return it, If not return 0 3088 * 3089 * If "bind_to_req_port_only" parameter is not set and 3090 * If the requested port number is available, return it. If not, return 3091 * the first anonymous port we happen across. If no anonymous ports are 3092 * available, return 0. addr is the requested local address, if any. 3093 * 3094 * In either case, when succeeding update the tcp_t to record the port number 3095 * and insert it in the bind hash table. 3096 * 3097 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3098 * without setting SO_REUSEADDR. This is needed so that they 3099 * can be viewed as two independent transport protocols. 3100 */ 3101 static in_port_t 3102 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3103 int reuseaddr, boolean_t quick_connect, 3104 boolean_t bind_to_req_port_only, boolean_t user_specified) 3105 { 3106 /* number of times we have run around the loop */ 3107 int count = 0; 3108 /* maximum number of times to run around the loop */ 3109 int loopmax; 3110 conn_t *connp = tcp->tcp_connp; 3111 zoneid_t zoneid = connp->conn_zoneid; 3112 tcp_stack_t *tcps = tcp->tcp_tcps; 3113 3114 /* 3115 * Lookup for free addresses is done in a loop and "loopmax" 3116 * influences how long we spin in the loop 3117 */ 3118 if (bind_to_req_port_only) { 3119 /* 3120 * If the requested port is busy, don't bother to look 3121 * for a new one. Setting loop maximum count to 1 has 3122 * that effect. 3123 */ 3124 loopmax = 1; 3125 } else { 3126 /* 3127 * If the requested port is busy, look for a free one 3128 * in the anonymous port range. 3129 * Set loopmax appropriately so that one does not look 3130 * forever in the case all of the anonymous ports are in use. 3131 */ 3132 if (tcp->tcp_anon_priv_bind) { 3133 /* 3134 * loopmax = 3135 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3136 */ 3137 loopmax = IPPORT_RESERVED - 3138 tcps->tcps_min_anonpriv_port; 3139 } else { 3140 loopmax = (tcps->tcps_largest_anon_port - 3141 tcps->tcps_smallest_anon_port + 1); 3142 } 3143 } 3144 do { 3145 uint16_t lport; 3146 tf_t *tbf; 3147 tcp_t *ltcp; 3148 conn_t *lconnp; 3149 3150 lport = htons(port); 3151 3152 /* 3153 * Ensure that the tcp_t is not currently in the bind hash. 3154 * Hold the lock on the hash bucket to ensure that 3155 * the duplicate check plus the insertion is an atomic 3156 * operation. 3157 * 3158 * This function does an inline lookup on the bind hash list 3159 * Make sure that we access only members of tcp_t 3160 * and that we don't look at tcp_tcp, since we are not 3161 * doing a CONN_INC_REF. 3162 */ 3163 tcp_bind_hash_remove(tcp); 3164 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3165 mutex_enter(&tbf->tf_lock); 3166 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3167 ltcp = ltcp->tcp_bind_hash) { 3168 if (lport == ltcp->tcp_lport) 3169 break; 3170 } 3171 3172 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3173 boolean_t not_socket; 3174 boolean_t exclbind; 3175 3176 lconnp = ltcp->tcp_connp; 3177 3178 /* 3179 * On a labeled system, we must treat bindings to ports 3180 * on shared IP addresses by sockets with MAC exemption 3181 * privilege as being in all zones, as there's 3182 * otherwise no way to identify the right receiver. 3183 */ 3184 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3185 IPCL_ZONE_MATCH(connp, 3186 ltcp->tcp_connp->conn_zoneid)) && 3187 !lconnp->conn_mac_exempt && 3188 !connp->conn_mac_exempt) 3189 continue; 3190 3191 /* 3192 * If TCP_EXCLBIND is set for either the bound or 3193 * binding endpoint, the semantics of bind 3194 * is changed according to the following. 3195 * 3196 * spec = specified address (v4 or v6) 3197 * unspec = unspecified address (v4 or v6) 3198 * A = specified addresses are different for endpoints 3199 * 3200 * bound bind to allowed 3201 * ------------------------------------- 3202 * unspec unspec no 3203 * unspec spec no 3204 * spec unspec no 3205 * spec spec yes if A 3206 * 3207 * For labeled systems, SO_MAC_EXEMPT behaves the same 3208 * as TCP_EXCLBIND, except that zoneid is ignored. 3209 * 3210 * Note: 3211 * 3212 * 1. Because of TLI semantics, an endpoint can go 3213 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3214 * TCPS_BOUND, depending on whether it is originally 3215 * a listener or not. That is why we need to check 3216 * for states greater than or equal to TCPS_BOUND 3217 * here. 3218 * 3219 * 2. Ideally, we should only check for state equals 3220 * to TCPS_LISTEN. And the following check should be 3221 * added. 3222 * 3223 * if (ltcp->tcp_state == TCPS_LISTEN || 3224 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3225 * ... 3226 * } 3227 * 3228 * The semantics will be changed to this. If the 3229 * endpoint on the list is in state not equal to 3230 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3231 * set, let the bind succeed. 3232 * 3233 * Because of (1), we cannot do that for TLI 3234 * endpoints. But we can do that for socket endpoints. 3235 * If in future, we can change this going back 3236 * semantics, we can use the above check for TLI also. 3237 */ 3238 not_socket = !(TCP_IS_SOCKET(ltcp) && 3239 TCP_IS_SOCKET(tcp)); 3240 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3241 3242 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3243 (exclbind && (not_socket || 3244 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3245 if (V6_OR_V4_INADDR_ANY( 3246 ltcp->tcp_bound_source_v6) || 3247 V6_OR_V4_INADDR_ANY(*laddr) || 3248 IN6_ARE_ADDR_EQUAL(laddr, 3249 <cp->tcp_bound_source_v6)) { 3250 break; 3251 } 3252 continue; 3253 } 3254 3255 /* 3256 * Check ipversion to allow IPv4 and IPv6 sockets to 3257 * have disjoint port number spaces, if *_EXCLBIND 3258 * is not set and only if the application binds to a 3259 * specific port. We use the same autoassigned port 3260 * number space for IPv4 and IPv6 sockets. 3261 */ 3262 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3263 bind_to_req_port_only) 3264 continue; 3265 3266 /* 3267 * Ideally, we should make sure that the source 3268 * address, remote address, and remote port in the 3269 * four tuple for this tcp-connection is unique. 3270 * However, trying to find out the local source 3271 * address would require too much code duplication 3272 * with IP, since IP needs needs to have that code 3273 * to support userland TCP implementations. 3274 */ 3275 if (quick_connect && 3276 (ltcp->tcp_state > TCPS_LISTEN) && 3277 ((tcp->tcp_fport != ltcp->tcp_fport) || 3278 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3279 <cp->tcp_remote_v6))) 3280 continue; 3281 3282 if (!reuseaddr) { 3283 /* 3284 * No socket option SO_REUSEADDR. 3285 * If existing port is bound to 3286 * a non-wildcard IP address 3287 * and the requesting stream is 3288 * bound to a distinct 3289 * different IP addresses 3290 * (non-wildcard, also), keep 3291 * going. 3292 */ 3293 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3294 !V6_OR_V4_INADDR_ANY( 3295 ltcp->tcp_bound_source_v6) && 3296 !IN6_ARE_ADDR_EQUAL(laddr, 3297 <cp->tcp_bound_source_v6)) 3298 continue; 3299 if (ltcp->tcp_state >= TCPS_BOUND) { 3300 /* 3301 * This port is being used and 3302 * its state is >= TCPS_BOUND, 3303 * so we can't bind to it. 3304 */ 3305 break; 3306 } 3307 } else { 3308 /* 3309 * socket option SO_REUSEADDR is set on the 3310 * binding tcp_t. 3311 * 3312 * If two streams are bound to 3313 * same IP address or both addr 3314 * and bound source are wildcards 3315 * (INADDR_ANY), we want to stop 3316 * searching. 3317 * We have found a match of IP source 3318 * address and source port, which is 3319 * refused regardless of the 3320 * SO_REUSEADDR setting, so we break. 3321 */ 3322 if (IN6_ARE_ADDR_EQUAL(laddr, 3323 <cp->tcp_bound_source_v6) && 3324 (ltcp->tcp_state == TCPS_LISTEN || 3325 ltcp->tcp_state == TCPS_BOUND)) 3326 break; 3327 } 3328 } 3329 if (ltcp != NULL) { 3330 /* The port number is busy */ 3331 mutex_exit(&tbf->tf_lock); 3332 } else { 3333 /* 3334 * This port is ours. Insert in fanout and mark as 3335 * bound to prevent others from getting the port 3336 * number. 3337 */ 3338 tcp->tcp_state = TCPS_BOUND; 3339 tcp->tcp_lport = htons(port); 3340 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3341 3342 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3343 tcp->tcp_lport)] == tbf); 3344 tcp_bind_hash_insert(tbf, tcp, 1); 3345 3346 mutex_exit(&tbf->tf_lock); 3347 3348 /* 3349 * We don't want tcp_next_port_to_try to "inherit" 3350 * a port number supplied by the user in a bind. 3351 */ 3352 if (user_specified) 3353 return (port); 3354 3355 /* 3356 * This is the only place where tcp_next_port_to_try 3357 * is updated. After the update, it may or may not 3358 * be in the valid range. 3359 */ 3360 if (!tcp->tcp_anon_priv_bind) 3361 tcps->tcps_next_port_to_try = port + 1; 3362 return (port); 3363 } 3364 3365 if (tcp->tcp_anon_priv_bind) { 3366 port = tcp_get_next_priv_port(tcp); 3367 } else { 3368 if (count == 0 && user_specified) { 3369 /* 3370 * We may have to return an anonymous port. So 3371 * get one to start with. 3372 */ 3373 port = 3374 tcp_update_next_port( 3375 tcps->tcps_next_port_to_try, 3376 tcp, B_TRUE); 3377 user_specified = B_FALSE; 3378 } else { 3379 port = tcp_update_next_port(port + 1, tcp, 3380 B_FALSE); 3381 } 3382 } 3383 if (port == 0) 3384 break; 3385 3386 /* 3387 * Don't let this loop run forever in the case where 3388 * all of the anonymous ports are in use. 3389 */ 3390 } while (++count < loopmax); 3391 return (0); 3392 } 3393 3394 /* 3395 * tcp_clean_death / tcp_close_detached must not be called more than once 3396 * on a tcp. Thus every function that potentially calls tcp_clean_death 3397 * must check for the tcp state before calling tcp_clean_death. 3398 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3399 * tcp_timer_handler, all check for the tcp state. 3400 */ 3401 /* ARGSUSED */ 3402 void 3403 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3404 { 3405 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3406 3407 freemsg(mp); 3408 if (tcp->tcp_state > TCPS_BOUND) 3409 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3410 ETIMEDOUT, 5); 3411 } 3412 3413 /* 3414 * We are dying for some reason. Try to do it gracefully. (May be called 3415 * as writer.) 3416 * 3417 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3418 * done by a service procedure). 3419 * TBD - Should the return value distinguish between the tcp_t being 3420 * freed and it being reinitialized? 3421 */ 3422 static int 3423 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3424 { 3425 mblk_t *mp; 3426 queue_t *q; 3427 conn_t *connp = tcp->tcp_connp; 3428 tcp_stack_t *tcps = tcp->tcp_tcps; 3429 3430 TCP_CLD_STAT(tag); 3431 3432 #if TCP_TAG_CLEAN_DEATH 3433 tcp->tcp_cleandeathtag = tag; 3434 #endif 3435 3436 if (tcp->tcp_fused) 3437 tcp_unfuse(tcp); 3438 3439 if (tcp->tcp_linger_tid != 0 && 3440 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3441 tcp_stop_lingering(tcp); 3442 } 3443 3444 ASSERT(tcp != NULL); 3445 ASSERT((tcp->tcp_family == AF_INET && 3446 tcp->tcp_ipversion == IPV4_VERSION) || 3447 (tcp->tcp_family == AF_INET6 && 3448 (tcp->tcp_ipversion == IPV4_VERSION || 3449 tcp->tcp_ipversion == IPV6_VERSION))); 3450 3451 if (TCP_IS_DETACHED(tcp)) { 3452 if (tcp->tcp_hard_binding) { 3453 /* 3454 * Its an eager that we are dealing with. We close the 3455 * eager but in case a conn_ind has already gone to the 3456 * listener, let tcp_accept_finish() send a discon_ind 3457 * to the listener and drop the last reference. If the 3458 * listener doesn't even know about the eager i.e. the 3459 * conn_ind hasn't gone up, blow away the eager and drop 3460 * the last reference as well. If the conn_ind has gone 3461 * up, state should be BOUND. tcp_accept_finish 3462 * will figure out that the connection has received a 3463 * RST and will send a DISCON_IND to the application. 3464 */ 3465 tcp_closei_local(tcp); 3466 if (!tcp->tcp_tconnind_started) { 3467 CONN_DEC_REF(connp); 3468 } else { 3469 tcp->tcp_state = TCPS_BOUND; 3470 } 3471 } else { 3472 tcp_close_detached(tcp); 3473 } 3474 return (0); 3475 } 3476 3477 TCP_STAT(tcps, tcp_clean_death_nondetached); 3478 3479 q = tcp->tcp_rq; 3480 3481 /* Trash all inbound data */ 3482 if (!IPCL_IS_NONSTR(connp)) { 3483 ASSERT(q != NULL); 3484 flushq(q, FLUSHALL); 3485 } 3486 3487 /* 3488 * If we are at least part way open and there is error 3489 * (err==0 implies no error) 3490 * notify our client by a T_DISCON_IND. 3491 */ 3492 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3493 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3494 !TCP_IS_SOCKET(tcp)) { 3495 /* 3496 * Send M_FLUSH according to TPI. Because sockets will 3497 * (and must) ignore FLUSHR we do that only for TPI 3498 * endpoints and sockets in STREAMS mode. 3499 */ 3500 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3501 } 3502 if (tcp->tcp_debug) { 3503 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3504 "tcp_clean_death: discon err %d", err); 3505 } 3506 if (IPCL_IS_NONSTR(connp)) { 3507 /* Direct socket, use upcall */ 3508 (*connp->conn_upcalls->su_disconnected)( 3509 connp->conn_upper_handle, tcp->tcp_connid, err); 3510 } else { 3511 mp = mi_tpi_discon_ind(NULL, err, 0); 3512 if (mp != NULL) { 3513 putnext(q, mp); 3514 } else { 3515 if (tcp->tcp_debug) { 3516 (void) strlog(TCP_MOD_ID, 0, 1, 3517 SL_ERROR|SL_TRACE, 3518 "tcp_clean_death, sending M_ERROR"); 3519 } 3520 (void) putnextctl1(q, M_ERROR, EPROTO); 3521 } 3522 } 3523 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3524 /* SYN_SENT or SYN_RCVD */ 3525 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3526 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3527 /* ESTABLISHED or CLOSE_WAIT */ 3528 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3529 } 3530 } 3531 3532 tcp_reinit(tcp); 3533 if (IPCL_IS_NONSTR(connp)) 3534 (void) tcp_do_unbind(connp); 3535 3536 return (-1); 3537 } 3538 3539 /* 3540 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3541 * to expire, stop the wait and finish the close. 3542 */ 3543 static void 3544 tcp_stop_lingering(tcp_t *tcp) 3545 { 3546 clock_t delta = 0; 3547 tcp_stack_t *tcps = tcp->tcp_tcps; 3548 3549 tcp->tcp_linger_tid = 0; 3550 if (tcp->tcp_state > TCPS_LISTEN) { 3551 tcp_acceptor_hash_remove(tcp); 3552 mutex_enter(&tcp->tcp_non_sq_lock); 3553 if (tcp->tcp_flow_stopped) { 3554 tcp_clrqfull(tcp); 3555 } 3556 mutex_exit(&tcp->tcp_non_sq_lock); 3557 3558 if (tcp->tcp_timer_tid != 0) { 3559 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3560 tcp->tcp_timer_tid = 0; 3561 } 3562 /* 3563 * Need to cancel those timers which will not be used when 3564 * TCP is detached. This has to be done before the tcp_wq 3565 * is set to the global queue. 3566 */ 3567 tcp_timers_stop(tcp); 3568 3569 tcp->tcp_detached = B_TRUE; 3570 ASSERT(tcps->tcps_g_q != NULL); 3571 tcp->tcp_rq = tcps->tcps_g_q; 3572 tcp->tcp_wq = WR(tcps->tcps_g_q); 3573 3574 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3575 tcp_time_wait_append(tcp); 3576 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3577 goto finish; 3578 } 3579 3580 /* 3581 * If delta is zero the timer event wasn't executed and was 3582 * successfully canceled. In this case we need to restart it 3583 * with the minimal delta possible. 3584 */ 3585 if (delta >= 0) { 3586 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3587 delta ? delta : 1); 3588 } 3589 } else { 3590 tcp_closei_local(tcp); 3591 CONN_DEC_REF(tcp->tcp_connp); 3592 } 3593 finish: 3594 /* Signal closing thread that it can complete close */ 3595 mutex_enter(&tcp->tcp_closelock); 3596 tcp->tcp_detached = B_TRUE; 3597 ASSERT(tcps->tcps_g_q != NULL); 3598 3599 tcp->tcp_rq = tcps->tcps_g_q; 3600 tcp->tcp_wq = WR(tcps->tcps_g_q); 3601 3602 tcp->tcp_closed = 1; 3603 cv_signal(&tcp->tcp_closecv); 3604 mutex_exit(&tcp->tcp_closelock); 3605 } 3606 3607 /* 3608 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3609 * expires. 3610 */ 3611 static void 3612 tcp_close_linger_timeout(void *arg) 3613 { 3614 conn_t *connp = (conn_t *)arg; 3615 tcp_t *tcp = connp->conn_tcp; 3616 3617 tcp->tcp_client_errno = ETIMEDOUT; 3618 tcp_stop_lingering(tcp); 3619 } 3620 3621 static void 3622 tcp_close_common(conn_t *connp, int flags) 3623 { 3624 tcp_t *tcp = connp->conn_tcp; 3625 mblk_t *mp = &tcp->tcp_closemp; 3626 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3627 mblk_t *bp; 3628 3629 ASSERT(connp->conn_ref >= 2); 3630 3631 /* 3632 * Mark the conn as closing. ill_pending_mp_add will not 3633 * add any mp to the pending mp list, after this conn has 3634 * started closing. Same for sq_pending_mp_add 3635 */ 3636 mutex_enter(&connp->conn_lock); 3637 connp->conn_state_flags |= CONN_CLOSING; 3638 if (connp->conn_oper_pending_ill != NULL) 3639 conn_ioctl_cleanup_reqd = B_TRUE; 3640 CONN_INC_REF_LOCKED(connp); 3641 mutex_exit(&connp->conn_lock); 3642 tcp->tcp_closeflags = (uint8_t)flags; 3643 ASSERT(connp->conn_ref >= 3); 3644 3645 /* 3646 * tcp_closemp_used is used below without any protection of a lock 3647 * as we don't expect any one else to use it concurrently at this 3648 * point otherwise it would be a major defect. 3649 */ 3650 3651 if (mp->b_prev == NULL) 3652 tcp->tcp_closemp_used = B_TRUE; 3653 else 3654 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3655 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3656 3657 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3658 3659 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3660 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3661 3662 mutex_enter(&tcp->tcp_closelock); 3663 while (!tcp->tcp_closed) { 3664 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3665 /* 3666 * The cv_wait_sig() was interrupted. We now do the 3667 * following: 3668 * 3669 * 1) If the endpoint was lingering, we allow this 3670 * to be interrupted by cancelling the linger timeout 3671 * and closing normally. 3672 * 3673 * 2) Revert to calling cv_wait() 3674 * 3675 * We revert to using cv_wait() to avoid an 3676 * infinite loop which can occur if the calling 3677 * thread is higher priority than the squeue worker 3678 * thread and is bound to the same cpu. 3679 */ 3680 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3681 mutex_exit(&tcp->tcp_closelock); 3682 /* Entering squeue, bump ref count. */ 3683 CONN_INC_REF(connp); 3684 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3685 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3686 tcp_linger_interrupted, connp, 3687 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3688 mutex_enter(&tcp->tcp_closelock); 3689 } 3690 break; 3691 } 3692 } 3693 while (!tcp->tcp_closed) 3694 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3695 mutex_exit(&tcp->tcp_closelock); 3696 3697 /* 3698 * In the case of listener streams that have eagers in the q or q0 3699 * we wait for the eagers to drop their reference to us. tcp_rq and 3700 * tcp_wq of the eagers point to our queues. By waiting for the 3701 * refcnt to drop to 1, we are sure that the eagers have cleaned 3702 * up their queue pointers and also dropped their references to us. 3703 */ 3704 if (tcp->tcp_wait_for_eagers) { 3705 mutex_enter(&connp->conn_lock); 3706 while (connp->conn_ref != 1) { 3707 cv_wait(&connp->conn_cv, &connp->conn_lock); 3708 } 3709 mutex_exit(&connp->conn_lock); 3710 } 3711 /* 3712 * ioctl cleanup. The mp is queued in the 3713 * ill_pending_mp or in the sq_pending_mp. 3714 */ 3715 if (conn_ioctl_cleanup_reqd) 3716 conn_ioctl_cleanup(connp); 3717 3718 tcp->tcp_cpid = -1; 3719 } 3720 3721 static int 3722 tcp_tpi_close(queue_t *q, int flags) 3723 { 3724 conn_t *connp; 3725 3726 ASSERT(WR(q)->q_next == NULL); 3727 3728 if (flags & SO_FALLBACK) { 3729 /* 3730 * stream is being closed while in fallback 3731 * simply free the resources that were allocated 3732 */ 3733 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3734 qprocsoff(q); 3735 goto done; 3736 } 3737 3738 connp = Q_TO_CONN(q); 3739 /* 3740 * We are being closed as /dev/tcp or /dev/tcp6. 3741 */ 3742 tcp_close_common(connp, flags); 3743 3744 qprocsoff(q); 3745 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3746 3747 /* 3748 * Drop IP's reference on the conn. This is the last reference 3749 * on the connp if the state was less than established. If the 3750 * connection has gone into timewait state, then we will have 3751 * one ref for the TCP and one more ref (total of two) for the 3752 * classifier connected hash list (a timewait connections stays 3753 * in connected hash till closed). 3754 * 3755 * We can't assert the references because there might be other 3756 * transient reference places because of some walkers or queued 3757 * packets in squeue for the timewait state. 3758 */ 3759 CONN_DEC_REF(connp); 3760 done: 3761 q->q_ptr = WR(q)->q_ptr = NULL; 3762 return (0); 3763 } 3764 3765 static int 3766 tcp_tpi_close_accept(queue_t *q) 3767 { 3768 vmem_t *minor_arena; 3769 dev_t conn_dev; 3770 3771 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3772 3773 /* 3774 * We had opened an acceptor STREAM for sockfs which is 3775 * now being closed due to some error. 3776 */ 3777 qprocsoff(q); 3778 3779 minor_arena = (vmem_t *)WR(q)->q_ptr; 3780 conn_dev = (dev_t)RD(q)->q_ptr; 3781 ASSERT(minor_arena != NULL); 3782 ASSERT(conn_dev != 0); 3783 inet_minor_free(minor_arena, conn_dev); 3784 q->q_ptr = WR(q)->q_ptr = NULL; 3785 return (0); 3786 } 3787 3788 /* 3789 * Called by tcp_close() routine via squeue when lingering is 3790 * interrupted by a signal. 3791 */ 3792 3793 /* ARGSUSED */ 3794 static void 3795 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3796 { 3797 conn_t *connp = (conn_t *)arg; 3798 tcp_t *tcp = connp->conn_tcp; 3799 3800 freeb(mp); 3801 if (tcp->tcp_linger_tid != 0 && 3802 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3803 tcp_stop_lingering(tcp); 3804 tcp->tcp_client_errno = EINTR; 3805 } 3806 } 3807 3808 /* 3809 * Called by streams close routine via squeues when our client blows off her 3810 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3811 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3812 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3813 * acked. 3814 * 3815 * NOTE: tcp_close potentially returns error when lingering. 3816 * However, the stream head currently does not pass these errors 3817 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3818 * errors to the application (from tsleep()) and not errors 3819 * like ECONNRESET caused by receiving a reset packet. 3820 */ 3821 3822 /* ARGSUSED */ 3823 static void 3824 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3825 { 3826 char *msg; 3827 conn_t *connp = (conn_t *)arg; 3828 tcp_t *tcp = connp->conn_tcp; 3829 clock_t delta = 0; 3830 tcp_stack_t *tcps = tcp->tcp_tcps; 3831 3832 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3833 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3834 3835 mutex_enter(&tcp->tcp_eager_lock); 3836 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3837 /* Cleanup for listener */ 3838 tcp_eager_cleanup(tcp, 0); 3839 tcp->tcp_wait_for_eagers = 1; 3840 } 3841 mutex_exit(&tcp->tcp_eager_lock); 3842 3843 connp->conn_mdt_ok = B_FALSE; 3844 tcp->tcp_mdt = B_FALSE; 3845 3846 connp->conn_lso_ok = B_FALSE; 3847 tcp->tcp_lso = B_FALSE; 3848 3849 msg = NULL; 3850 switch (tcp->tcp_state) { 3851 case TCPS_CLOSED: 3852 case TCPS_IDLE: 3853 case TCPS_BOUND: 3854 case TCPS_LISTEN: 3855 break; 3856 case TCPS_SYN_SENT: 3857 msg = "tcp_close, during connect"; 3858 break; 3859 case TCPS_SYN_RCVD: 3860 /* 3861 * Close during the connect 3-way handshake 3862 * but here there may or may not be pending data 3863 * already on queue. Process almost same as in 3864 * the ESTABLISHED state. 3865 */ 3866 /* FALLTHRU */ 3867 default: 3868 if (tcp->tcp_fused) 3869 tcp_unfuse(tcp); 3870 3871 /* 3872 * If SO_LINGER has set a zero linger time, abort the 3873 * connection with a reset. 3874 */ 3875 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3876 msg = "tcp_close, zero lingertime"; 3877 break; 3878 } 3879 3880 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3881 /* 3882 * Abort connection if there is unread data queued. 3883 */ 3884 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3885 msg = "tcp_close, unread data"; 3886 break; 3887 } 3888 /* 3889 * tcp_hard_bound is now cleared thus all packets go through 3890 * tcp_lookup. This fact is used by tcp_detach below. 3891 * 3892 * We have done a qwait() above which could have possibly 3893 * drained more messages in turn causing transition to a 3894 * different state. Check whether we have to do the rest 3895 * of the processing or not. 3896 */ 3897 if (tcp->tcp_state <= TCPS_LISTEN) 3898 break; 3899 3900 /* 3901 * Transmit the FIN before detaching the tcp_t. 3902 * After tcp_detach returns this queue/perimeter 3903 * no longer owns the tcp_t thus others can modify it. 3904 */ 3905 (void) tcp_xmit_end(tcp); 3906 3907 /* 3908 * If lingering on close then wait until the fin is acked, 3909 * the SO_LINGER time passes, or a reset is sent/received. 3910 */ 3911 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3912 !(tcp->tcp_fin_acked) && 3913 tcp->tcp_state >= TCPS_ESTABLISHED) { 3914 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3915 tcp->tcp_client_errno = EWOULDBLOCK; 3916 } else if (tcp->tcp_client_errno == 0) { 3917 3918 ASSERT(tcp->tcp_linger_tid == 0); 3919 3920 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3921 tcp_close_linger_timeout, 3922 tcp->tcp_lingertime * hz); 3923 3924 /* tcp_close_linger_timeout will finish close */ 3925 if (tcp->tcp_linger_tid == 0) 3926 tcp->tcp_client_errno = ENOSR; 3927 else 3928 return; 3929 } 3930 3931 /* 3932 * Check if we need to detach or just close 3933 * the instance. 3934 */ 3935 if (tcp->tcp_state <= TCPS_LISTEN) 3936 break; 3937 } 3938 3939 /* 3940 * Make sure that no other thread will access the tcp_rq of 3941 * this instance (through lookups etc.) as tcp_rq will go 3942 * away shortly. 3943 */ 3944 tcp_acceptor_hash_remove(tcp); 3945 3946 mutex_enter(&tcp->tcp_non_sq_lock); 3947 if (tcp->tcp_flow_stopped) { 3948 tcp_clrqfull(tcp); 3949 } 3950 mutex_exit(&tcp->tcp_non_sq_lock); 3951 3952 if (tcp->tcp_timer_tid != 0) { 3953 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3954 tcp->tcp_timer_tid = 0; 3955 } 3956 /* 3957 * Need to cancel those timers which will not be used when 3958 * TCP is detached. This has to be done before the tcp_wq 3959 * is set to the global queue. 3960 */ 3961 tcp_timers_stop(tcp); 3962 3963 tcp->tcp_detached = B_TRUE; 3964 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3965 tcp_time_wait_append(tcp); 3966 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3967 ASSERT(connp->conn_ref >= 3); 3968 goto finish; 3969 } 3970 3971 /* 3972 * If delta is zero the timer event wasn't executed and was 3973 * successfully canceled. In this case we need to restart it 3974 * with the minimal delta possible. 3975 */ 3976 if (delta >= 0) 3977 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3978 delta ? delta : 1); 3979 3980 ASSERT(connp->conn_ref >= 3); 3981 goto finish; 3982 } 3983 3984 /* Detach did not complete. Still need to remove q from stream. */ 3985 if (msg) { 3986 if (tcp->tcp_state == TCPS_ESTABLISHED || 3987 tcp->tcp_state == TCPS_CLOSE_WAIT) 3988 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3989 if (tcp->tcp_state == TCPS_SYN_SENT || 3990 tcp->tcp_state == TCPS_SYN_RCVD) 3991 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3992 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 3993 } 3994 3995 tcp_closei_local(tcp); 3996 CONN_DEC_REF(connp); 3997 ASSERT(connp->conn_ref >= 2); 3998 3999 finish: 4000 /* 4001 * Although packets are always processed on the correct 4002 * tcp's perimeter and access is serialized via squeue's, 4003 * IP still needs a queue when sending packets in time_wait 4004 * state so use WR(tcps_g_q) till ip_output() can be 4005 * changed to deal with just connp. For read side, we 4006 * could have set tcp_rq to NULL but there are some cases 4007 * in tcp_rput_data() from early days of this code which 4008 * do a putnext without checking if tcp is closed. Those 4009 * need to be identified before both tcp_rq and tcp_wq 4010 * can be set to NULL and tcps_g_q can disappear forever. 4011 */ 4012 mutex_enter(&tcp->tcp_closelock); 4013 /* 4014 * Don't change the queues in the case of a listener that has 4015 * eagers in its q or q0. It could surprise the eagers. 4016 * Instead wait for the eagers outside the squeue. 4017 */ 4018 if (!tcp->tcp_wait_for_eagers) { 4019 tcp->tcp_detached = B_TRUE; 4020 /* 4021 * When default queue is closing we set tcps_g_q to NULL 4022 * after the close is done. 4023 */ 4024 ASSERT(tcps->tcps_g_q != NULL); 4025 tcp->tcp_rq = tcps->tcps_g_q; 4026 tcp->tcp_wq = WR(tcps->tcps_g_q); 4027 } 4028 4029 /* Signal tcp_close() to finish closing. */ 4030 tcp->tcp_closed = 1; 4031 cv_signal(&tcp->tcp_closecv); 4032 mutex_exit(&tcp->tcp_closelock); 4033 } 4034 4035 /* 4036 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4037 * Some stream heads get upset if they see these later on as anything but NULL. 4038 */ 4039 static void 4040 tcp_close_mpp(mblk_t **mpp) 4041 { 4042 mblk_t *mp; 4043 4044 if ((mp = *mpp) != NULL) { 4045 do { 4046 mp->b_next = NULL; 4047 mp->b_prev = NULL; 4048 } while ((mp = mp->b_cont) != NULL); 4049 4050 mp = *mpp; 4051 *mpp = NULL; 4052 freemsg(mp); 4053 } 4054 } 4055 4056 /* Do detached close. */ 4057 static void 4058 tcp_close_detached(tcp_t *tcp) 4059 { 4060 if (tcp->tcp_fused) 4061 tcp_unfuse(tcp); 4062 4063 /* 4064 * Clustering code serializes TCP disconnect callbacks and 4065 * cluster tcp list walks by blocking a TCP disconnect callback 4066 * if a cluster tcp list walk is in progress. This ensures 4067 * accurate accounting of TCPs in the cluster code even though 4068 * the TCP list walk itself is not atomic. 4069 */ 4070 tcp_closei_local(tcp); 4071 CONN_DEC_REF(tcp->tcp_connp); 4072 } 4073 4074 /* 4075 * Stop all TCP timers, and free the timer mblks if requested. 4076 */ 4077 void 4078 tcp_timers_stop(tcp_t *tcp) 4079 { 4080 if (tcp->tcp_timer_tid != 0) { 4081 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4082 tcp->tcp_timer_tid = 0; 4083 } 4084 if (tcp->tcp_ka_tid != 0) { 4085 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4086 tcp->tcp_ka_tid = 0; 4087 } 4088 if (tcp->tcp_ack_tid != 0) { 4089 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4090 tcp->tcp_ack_tid = 0; 4091 } 4092 if (tcp->tcp_push_tid != 0) { 4093 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4094 tcp->tcp_push_tid = 0; 4095 } 4096 } 4097 4098 /* 4099 * The tcp_t is going away. Remove it from all lists and set it 4100 * to TCPS_CLOSED. The freeing up of memory is deferred until 4101 * tcp_inactive. This is needed since a thread in tcp_rput might have 4102 * done a CONN_INC_REF on this structure before it was removed from the 4103 * hashes. 4104 */ 4105 static void 4106 tcp_closei_local(tcp_t *tcp) 4107 { 4108 ire_t *ire; 4109 conn_t *connp = tcp->tcp_connp; 4110 tcp_stack_t *tcps = tcp->tcp_tcps; 4111 4112 if (!TCP_IS_SOCKET(tcp)) 4113 tcp_acceptor_hash_remove(tcp); 4114 4115 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4116 tcp->tcp_ibsegs = 0; 4117 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4118 tcp->tcp_obsegs = 0; 4119 4120 /* 4121 * If we are an eager connection hanging off a listener that 4122 * hasn't formally accepted the connection yet, get off his 4123 * list and blow off any data that we have accumulated. 4124 */ 4125 if (tcp->tcp_listener != NULL) { 4126 tcp_t *listener = tcp->tcp_listener; 4127 mutex_enter(&listener->tcp_eager_lock); 4128 /* 4129 * tcp_tconnind_started == B_TRUE means that the 4130 * conn_ind has already gone to listener. At 4131 * this point, eager will be closed but we 4132 * leave it in listeners eager list so that 4133 * if listener decides to close without doing 4134 * accept, we can clean this up. In tcp_wput_accept 4135 * we take care of the case of accept on closed 4136 * eager. 4137 */ 4138 if (!tcp->tcp_tconnind_started) { 4139 tcp_eager_unlink(tcp); 4140 mutex_exit(&listener->tcp_eager_lock); 4141 /* 4142 * We don't want to have any pointers to the 4143 * listener queue, after we have released our 4144 * reference on the listener 4145 */ 4146 ASSERT(tcps->tcps_g_q != NULL); 4147 tcp->tcp_rq = tcps->tcps_g_q; 4148 tcp->tcp_wq = WR(tcps->tcps_g_q); 4149 CONN_DEC_REF(listener->tcp_connp); 4150 } else { 4151 mutex_exit(&listener->tcp_eager_lock); 4152 } 4153 } 4154 4155 /* Stop all the timers */ 4156 tcp_timers_stop(tcp); 4157 4158 if (tcp->tcp_state == TCPS_LISTEN) { 4159 if (tcp->tcp_ip_addr_cache) { 4160 kmem_free((void *)tcp->tcp_ip_addr_cache, 4161 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4162 tcp->tcp_ip_addr_cache = NULL; 4163 } 4164 } 4165 mutex_enter(&tcp->tcp_non_sq_lock); 4166 if (tcp->tcp_flow_stopped) 4167 tcp_clrqfull(tcp); 4168 mutex_exit(&tcp->tcp_non_sq_lock); 4169 4170 tcp_bind_hash_remove(tcp); 4171 /* 4172 * If the tcp_time_wait_collector (which runs outside the squeue) 4173 * is trying to remove this tcp from the time wait list, we will 4174 * block in tcp_time_wait_remove while trying to acquire the 4175 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4176 * requires the ipcl_hash_remove to be ordered after the 4177 * tcp_time_wait_remove for the refcnt checks to work correctly. 4178 */ 4179 if (tcp->tcp_state == TCPS_TIME_WAIT) 4180 (void) tcp_time_wait_remove(tcp, NULL); 4181 CL_INET_DISCONNECT(connp, tcp); 4182 ipcl_hash_remove(connp); 4183 4184 /* 4185 * Delete the cached ire in conn_ire_cache and also mark 4186 * the conn as CONDEMNED 4187 */ 4188 mutex_enter(&connp->conn_lock); 4189 connp->conn_state_flags |= CONN_CONDEMNED; 4190 ire = connp->conn_ire_cache; 4191 connp->conn_ire_cache = NULL; 4192 mutex_exit(&connp->conn_lock); 4193 if (ire != NULL) 4194 IRE_REFRELE_NOTR(ire); 4195 4196 /* Need to cleanup any pending ioctls */ 4197 ASSERT(tcp->tcp_time_wait_next == NULL); 4198 ASSERT(tcp->tcp_time_wait_prev == NULL); 4199 ASSERT(tcp->tcp_time_wait_expire == 0); 4200 tcp->tcp_state = TCPS_CLOSED; 4201 4202 /* Release any SSL context */ 4203 if (tcp->tcp_kssl_ent != NULL) { 4204 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4205 tcp->tcp_kssl_ent = NULL; 4206 } 4207 if (tcp->tcp_kssl_ctx != NULL) { 4208 kssl_release_ctx(tcp->tcp_kssl_ctx); 4209 tcp->tcp_kssl_ctx = NULL; 4210 } 4211 tcp->tcp_kssl_pending = B_FALSE; 4212 4213 tcp_ipsec_cleanup(tcp); 4214 } 4215 4216 /* 4217 * tcp is dying (called from ipcl_conn_destroy and error cases). 4218 * Free the tcp_t in either case. 4219 */ 4220 void 4221 tcp_free(tcp_t *tcp) 4222 { 4223 mblk_t *mp; 4224 ip6_pkt_t *ipp; 4225 4226 ASSERT(tcp != NULL); 4227 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4228 4229 tcp->tcp_rq = NULL; 4230 tcp->tcp_wq = NULL; 4231 4232 tcp_close_mpp(&tcp->tcp_xmit_head); 4233 tcp_close_mpp(&tcp->tcp_reass_head); 4234 if (tcp->tcp_rcv_list != NULL) { 4235 /* Free b_next chain */ 4236 tcp_close_mpp(&tcp->tcp_rcv_list); 4237 } 4238 if ((mp = tcp->tcp_urp_mp) != NULL) { 4239 freemsg(mp); 4240 } 4241 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4242 freemsg(mp); 4243 } 4244 4245 if (tcp->tcp_fused_sigurg_mp != NULL) { 4246 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4247 freeb(tcp->tcp_fused_sigurg_mp); 4248 tcp->tcp_fused_sigurg_mp = NULL; 4249 } 4250 4251 if (tcp->tcp_ordrel_mp != NULL) { 4252 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4253 freeb(tcp->tcp_ordrel_mp); 4254 tcp->tcp_ordrel_mp = NULL; 4255 } 4256 4257 if (tcp->tcp_sack_info != NULL) { 4258 if (tcp->tcp_notsack_list != NULL) { 4259 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 4260 tcp); 4261 } 4262 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4263 } 4264 4265 if (tcp->tcp_hopopts != NULL) { 4266 mi_free(tcp->tcp_hopopts); 4267 tcp->tcp_hopopts = NULL; 4268 tcp->tcp_hopoptslen = 0; 4269 } 4270 ASSERT(tcp->tcp_hopoptslen == 0); 4271 if (tcp->tcp_dstopts != NULL) { 4272 mi_free(tcp->tcp_dstopts); 4273 tcp->tcp_dstopts = NULL; 4274 tcp->tcp_dstoptslen = 0; 4275 } 4276 ASSERT(tcp->tcp_dstoptslen == 0); 4277 if (tcp->tcp_rtdstopts != NULL) { 4278 mi_free(tcp->tcp_rtdstopts); 4279 tcp->tcp_rtdstopts = NULL; 4280 tcp->tcp_rtdstoptslen = 0; 4281 } 4282 ASSERT(tcp->tcp_rtdstoptslen == 0); 4283 if (tcp->tcp_rthdr != NULL) { 4284 mi_free(tcp->tcp_rthdr); 4285 tcp->tcp_rthdr = NULL; 4286 tcp->tcp_rthdrlen = 0; 4287 } 4288 ASSERT(tcp->tcp_rthdrlen == 0); 4289 4290 ipp = &tcp->tcp_sticky_ipp; 4291 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4292 IPPF_RTHDR)) 4293 ip6_pkt_free(ipp); 4294 4295 /* 4296 * Free memory associated with the tcp/ip header template. 4297 */ 4298 4299 if (tcp->tcp_iphc != NULL) 4300 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4301 4302 /* 4303 * Following is really a blowing away a union. 4304 * It happens to have exactly two members of identical size 4305 * the following code is enough. 4306 */ 4307 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4308 } 4309 4310 4311 /* 4312 * Put a connection confirmation message upstream built from the 4313 * address information within 'iph' and 'tcph'. Report our success or failure. 4314 */ 4315 static boolean_t 4316 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4317 mblk_t **defermp) 4318 { 4319 sin_t sin; 4320 sin6_t sin6; 4321 mblk_t *mp; 4322 char *optp = NULL; 4323 int optlen = 0; 4324 4325 if (defermp != NULL) 4326 *defermp = NULL; 4327 4328 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4329 /* 4330 * Return in T_CONN_CON results of option negotiation through 4331 * the T_CONN_REQ. Note: If there is an real end-to-end option 4332 * negotiation, then what is received from remote end needs 4333 * to be taken into account but there is no such thing (yet?) 4334 * in our TCP/IP. 4335 * Note: We do not use mi_offset_param() here as 4336 * tcp_opts_conn_req contents do not directly come from 4337 * an application and are either generated in kernel or 4338 * from user input that was already verified. 4339 */ 4340 mp = tcp->tcp_conn.tcp_opts_conn_req; 4341 optp = (char *)(mp->b_rptr + 4342 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4343 optlen = (int) 4344 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4345 } 4346 4347 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4348 ipha_t *ipha = (ipha_t *)iphdr; 4349 4350 /* packet is IPv4 */ 4351 if (tcp->tcp_family == AF_INET) { 4352 sin = sin_null; 4353 sin.sin_addr.s_addr = ipha->ipha_src; 4354 sin.sin_port = *(uint16_t *)tcph->th_lport; 4355 sin.sin_family = AF_INET; 4356 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4357 (int)sizeof (sin_t), optp, optlen); 4358 } else { 4359 sin6 = sin6_null; 4360 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4361 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4362 sin6.sin6_family = AF_INET6; 4363 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4364 (int)sizeof (sin6_t), optp, optlen); 4365 4366 } 4367 } else { 4368 ip6_t *ip6h = (ip6_t *)iphdr; 4369 4370 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4371 ASSERT(tcp->tcp_family == AF_INET6); 4372 sin6 = sin6_null; 4373 sin6.sin6_addr = ip6h->ip6_src; 4374 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4375 sin6.sin6_family = AF_INET6; 4376 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4377 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4378 (int)sizeof (sin6_t), optp, optlen); 4379 } 4380 4381 if (!mp) 4382 return (B_FALSE); 4383 4384 mblk_copycred(mp, idmp); 4385 4386 if (defermp == NULL) { 4387 conn_t *connp = tcp->tcp_connp; 4388 if (IPCL_IS_NONSTR(connp)) { 4389 cred_t *cr; 4390 pid_t cpid; 4391 4392 cr = msg_getcred(mp, &cpid); 4393 (*connp->conn_upcalls->su_connected) 4394 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4395 cpid); 4396 freemsg(mp); 4397 } else { 4398 putnext(tcp->tcp_rq, mp); 4399 } 4400 } else { 4401 *defermp = mp; 4402 } 4403 4404 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4405 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4406 return (B_TRUE); 4407 } 4408 4409 /* 4410 * Defense for the SYN attack - 4411 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4412 * one from the list of droppable eagers. This list is a subset of q0. 4413 * see comments before the definition of MAKE_DROPPABLE(). 4414 * 2. Don't drop a SYN request before its first timeout. This gives every 4415 * request at least til the first timeout to complete its 3-way handshake. 4416 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4417 * requests currently on the queue that has timed out. This will be used 4418 * as an indicator of whether an attack is under way, so that appropriate 4419 * actions can be taken. (It's incremented in tcp_timer() and decremented 4420 * either when eager goes into ESTABLISHED, or gets freed up.) 4421 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4422 * # of timeout drops back to <= q0len/32 => SYN alert off 4423 */ 4424 static boolean_t 4425 tcp_drop_q0(tcp_t *tcp) 4426 { 4427 tcp_t *eager; 4428 mblk_t *mp; 4429 tcp_stack_t *tcps = tcp->tcp_tcps; 4430 4431 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4432 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4433 4434 /* Pick oldest eager from the list of droppable eagers */ 4435 eager = tcp->tcp_eager_prev_drop_q0; 4436 4437 /* If list is empty. return B_FALSE */ 4438 if (eager == tcp) { 4439 return (B_FALSE); 4440 } 4441 4442 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4443 if ((mp = allocb(0, BPRI_HI)) == NULL) 4444 return (B_FALSE); 4445 4446 /* 4447 * Take this eager out from the list of droppable eagers since we are 4448 * going to drop it. 4449 */ 4450 MAKE_UNDROPPABLE(eager); 4451 4452 if (tcp->tcp_debug) { 4453 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4454 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4455 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4456 tcp->tcp_conn_req_cnt_q0, 4457 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4458 } 4459 4460 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4461 4462 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4463 CONN_INC_REF(eager->tcp_connp); 4464 4465 /* Mark the IRE created for this SYN request temporary */ 4466 tcp_ip_ire_mark_advice(eager); 4467 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4468 tcp_clean_death_wrapper, eager->tcp_connp, 4469 SQ_FILL, SQTAG_TCP_DROP_Q0); 4470 4471 return (B_TRUE); 4472 } 4473 4474 int 4475 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4476 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4477 { 4478 tcp_t *ltcp = lconnp->conn_tcp; 4479 tcp_t *tcp = connp->conn_tcp; 4480 mblk_t *tpi_mp; 4481 ipha_t *ipha; 4482 ip6_t *ip6h; 4483 sin6_t sin6; 4484 in6_addr_t v6dst; 4485 int err; 4486 int ifindex = 0; 4487 tcp_stack_t *tcps = tcp->tcp_tcps; 4488 4489 if (ipvers == IPV4_VERSION) { 4490 ipha = (ipha_t *)mp->b_rptr; 4491 4492 connp->conn_send = ip_output; 4493 connp->conn_recv = tcp_input; 4494 4495 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4496 &connp->conn_bound_source_v6); 4497 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4498 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4499 4500 sin6 = sin6_null; 4501 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4502 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4503 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4504 sin6.sin6_family = AF_INET6; 4505 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4506 lconnp->conn_zoneid, tcps->tcps_netstack); 4507 if (tcp->tcp_recvdstaddr) { 4508 sin6_t sin6d; 4509 4510 sin6d = sin6_null; 4511 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4512 &sin6d.sin6_addr); 4513 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4514 sin6d.sin6_family = AF_INET; 4515 tpi_mp = mi_tpi_extconn_ind(NULL, 4516 (char *)&sin6d, sizeof (sin6_t), 4517 (char *)&tcp, 4518 (t_scalar_t)sizeof (intptr_t), 4519 (char *)&sin6d, sizeof (sin6_t), 4520 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4521 } else { 4522 tpi_mp = mi_tpi_conn_ind(NULL, 4523 (char *)&sin6, sizeof (sin6_t), 4524 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4525 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4526 } 4527 } else { 4528 ip6h = (ip6_t *)mp->b_rptr; 4529 4530 connp->conn_send = ip_output_v6; 4531 connp->conn_recv = tcp_input; 4532 4533 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4534 connp->conn_srcv6 = ip6h->ip6_dst; 4535 connp->conn_remv6 = ip6h->ip6_src; 4536 4537 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4538 ifindex = (int)DB_CKSUMSTUFF(mp); 4539 DB_CKSUMSTUFF(mp) = 0; 4540 4541 sin6 = sin6_null; 4542 sin6.sin6_addr = ip6h->ip6_src; 4543 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4544 sin6.sin6_family = AF_INET6; 4545 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4546 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4547 lconnp->conn_zoneid, tcps->tcps_netstack); 4548 4549 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4550 /* Pass up the scope_id of remote addr */ 4551 sin6.sin6_scope_id = ifindex; 4552 } else { 4553 sin6.sin6_scope_id = 0; 4554 } 4555 if (tcp->tcp_recvdstaddr) { 4556 sin6_t sin6d; 4557 4558 sin6d = sin6_null; 4559 sin6.sin6_addr = ip6h->ip6_dst; 4560 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4561 sin6d.sin6_family = AF_INET; 4562 tpi_mp = mi_tpi_extconn_ind(NULL, 4563 (char *)&sin6d, sizeof (sin6_t), 4564 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4565 (char *)&sin6d, sizeof (sin6_t), 4566 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4567 } else { 4568 tpi_mp = mi_tpi_conn_ind(NULL, 4569 (char *)&sin6, sizeof (sin6_t), 4570 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4571 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4572 } 4573 } 4574 4575 if (tpi_mp == NULL) 4576 return (ENOMEM); 4577 4578 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4579 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4580 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4581 connp->conn_fully_bound = B_FALSE; 4582 4583 /* Inherit information from the "parent" */ 4584 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4585 tcp->tcp_family = ltcp->tcp_family; 4586 4587 tcp->tcp_wq = ltcp->tcp_wq; 4588 tcp->tcp_rq = ltcp->tcp_rq; 4589 4590 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4591 tcp->tcp_detached = B_TRUE; 4592 SOCK_CONNID_INIT(tcp->tcp_connid); 4593 if ((err = tcp_init_values(tcp)) != 0) { 4594 freemsg(tpi_mp); 4595 return (err); 4596 } 4597 4598 if (ipvers == IPV4_VERSION) { 4599 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4600 freemsg(tpi_mp); 4601 return (err); 4602 } 4603 ASSERT(tcp->tcp_ipha != NULL); 4604 } else { 4605 /* ifindex must be already set */ 4606 ASSERT(ifindex != 0); 4607 4608 if (ltcp->tcp_bound_if != 0) 4609 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4610 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4611 tcp->tcp_bound_if = ifindex; 4612 4613 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4614 tcp->tcp_recvifindex = 0; 4615 tcp->tcp_recvhops = 0xffffffffU; 4616 ASSERT(tcp->tcp_ip6h != NULL); 4617 } 4618 4619 tcp->tcp_lport = ltcp->tcp_lport; 4620 4621 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4622 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4623 /* 4624 * Listener had options of some sort; eager inherits. 4625 * Free up the eager template and allocate one 4626 * of the right size. 4627 */ 4628 if (tcp->tcp_hdr_grown) { 4629 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4630 } else { 4631 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4632 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4633 } 4634 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4635 KM_NOSLEEP); 4636 if (tcp->tcp_iphc == NULL) { 4637 tcp->tcp_iphc_len = 0; 4638 freemsg(tpi_mp); 4639 return (ENOMEM); 4640 } 4641 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4642 tcp->tcp_hdr_grown = B_TRUE; 4643 } 4644 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4645 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4646 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4647 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4648 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4649 4650 /* 4651 * Copy the IP+TCP header template from listener to eager 4652 */ 4653 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4654 if (tcp->tcp_ipversion == IPV6_VERSION) { 4655 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4656 IPPROTO_RAW) { 4657 tcp->tcp_ip6h = 4658 (ip6_t *)(tcp->tcp_iphc + 4659 sizeof (ip6i_t)); 4660 } else { 4661 tcp->tcp_ip6h = 4662 (ip6_t *)(tcp->tcp_iphc); 4663 } 4664 tcp->tcp_ipha = NULL; 4665 } else { 4666 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4667 tcp->tcp_ip6h = NULL; 4668 } 4669 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4670 tcp->tcp_ip_hdr_len); 4671 } else { 4672 /* 4673 * only valid case when ipversion of listener and 4674 * eager differ is when listener is IPv6 and 4675 * eager is IPv4. 4676 * Eager header template has been initialized to the 4677 * maximum v4 header sizes, which includes space for 4678 * TCP and IP options. 4679 */ 4680 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4681 (tcp->tcp_ipversion == IPV4_VERSION)); 4682 ASSERT(tcp->tcp_iphc_len >= 4683 TCP_MAX_COMBINED_HEADER_LENGTH); 4684 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4685 /* copy IP header fields individually */ 4686 tcp->tcp_ipha->ipha_ttl = 4687 ltcp->tcp_ip6h->ip6_hops; 4688 bcopy(ltcp->tcp_tcph->th_lport, 4689 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4690 } 4691 4692 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4693 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4694 sizeof (in_port_t)); 4695 4696 if (ltcp->tcp_lport == 0) { 4697 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4698 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4699 sizeof (in_port_t)); 4700 } 4701 4702 if (tcp->tcp_ipversion == IPV4_VERSION) { 4703 ASSERT(ipha != NULL); 4704 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4705 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4706 4707 /* Source routing option copyover (reverse it) */ 4708 if (tcps->tcps_rev_src_routes) 4709 tcp_opt_reverse(tcp, ipha); 4710 } else { 4711 ASSERT(ip6h != NULL); 4712 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4713 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4714 } 4715 4716 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4717 ASSERT(!tcp->tcp_tconnind_started); 4718 /* 4719 * If the SYN contains a credential, it's a loopback packet; attach 4720 * the credential to the TPI message. 4721 */ 4722 mblk_copycred(tpi_mp, idmp); 4723 4724 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4725 4726 /* Inherit the listener's SSL protection state */ 4727 4728 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4729 kssl_hold_ent(tcp->tcp_kssl_ent); 4730 tcp->tcp_kssl_pending = B_TRUE; 4731 } 4732 4733 /* Inherit the listener's non-STREAMS flag */ 4734 if (IPCL_IS_NONSTR(lconnp)) { 4735 connp->conn_flags |= IPCL_NONSTR; 4736 } 4737 4738 return (0); 4739 } 4740 4741 4742 int 4743 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4744 tcph_t *tcph, mblk_t *idmp) 4745 { 4746 tcp_t *ltcp = lconnp->conn_tcp; 4747 tcp_t *tcp = connp->conn_tcp; 4748 sin_t sin; 4749 mblk_t *tpi_mp = NULL; 4750 int err; 4751 tcp_stack_t *tcps = tcp->tcp_tcps; 4752 4753 sin = sin_null; 4754 sin.sin_addr.s_addr = ipha->ipha_src; 4755 sin.sin_port = *(uint16_t *)tcph->th_lport; 4756 sin.sin_family = AF_INET; 4757 if (ltcp->tcp_recvdstaddr) { 4758 sin_t sind; 4759 4760 sind = sin_null; 4761 sind.sin_addr.s_addr = ipha->ipha_dst; 4762 sind.sin_port = *(uint16_t *)tcph->th_fport; 4763 sind.sin_family = AF_INET; 4764 tpi_mp = mi_tpi_extconn_ind(NULL, 4765 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4766 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4767 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4768 } else { 4769 tpi_mp = mi_tpi_conn_ind(NULL, 4770 (char *)&sin, sizeof (sin_t), 4771 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4772 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4773 } 4774 4775 if (tpi_mp == NULL) { 4776 return (ENOMEM); 4777 } 4778 4779 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4780 connp->conn_send = ip_output; 4781 connp->conn_recv = tcp_input; 4782 connp->conn_fully_bound = B_FALSE; 4783 4784 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4785 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4786 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4787 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4788 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4789 4790 /* Inherit information from the "parent" */ 4791 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4792 tcp->tcp_family = ltcp->tcp_family; 4793 tcp->tcp_wq = ltcp->tcp_wq; 4794 tcp->tcp_rq = ltcp->tcp_rq; 4795 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4796 tcp->tcp_detached = B_TRUE; 4797 SOCK_CONNID_INIT(tcp->tcp_connid); 4798 if ((err = tcp_init_values(tcp)) != 0) { 4799 freemsg(tpi_mp); 4800 return (err); 4801 } 4802 4803 /* 4804 * Let's make sure that eager tcp template has enough space to 4805 * copy IPv4 listener's tcp template. Since the conn_t structure is 4806 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4807 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4808 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4809 * extension headers or with ip6i_t struct). Note that bcopy() below 4810 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4811 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4812 */ 4813 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4814 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4815 4816 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4817 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4818 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4819 tcp->tcp_ttl = ltcp->tcp_ttl; 4820 tcp->tcp_tos = ltcp->tcp_tos; 4821 4822 /* Copy the IP+TCP header template from listener to eager */ 4823 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4824 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4825 tcp->tcp_ip6h = NULL; 4826 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4827 tcp->tcp_ip_hdr_len); 4828 4829 /* Initialize the IP addresses and Ports */ 4830 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4831 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4832 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4833 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4834 4835 /* Source routing option copyover (reverse it) */ 4836 if (tcps->tcps_rev_src_routes) 4837 tcp_opt_reverse(tcp, ipha); 4838 4839 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4840 ASSERT(!tcp->tcp_tconnind_started); 4841 4842 /* 4843 * If the SYN contains a credential, it's a loopback packet; attach 4844 * the credential to the TPI message. 4845 */ 4846 mblk_copycred(tpi_mp, idmp); 4847 4848 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4849 4850 /* Inherit the listener's SSL protection state */ 4851 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4852 kssl_hold_ent(tcp->tcp_kssl_ent); 4853 tcp->tcp_kssl_pending = B_TRUE; 4854 } 4855 4856 /* Inherit the listener's non-STREAMS flag */ 4857 if (IPCL_IS_NONSTR(lconnp)) { 4858 connp->conn_flags |= IPCL_NONSTR; 4859 } 4860 4861 return (0); 4862 } 4863 4864 /* 4865 * sets up conn for ipsec. 4866 * if the first mblk is M_CTL it is consumed and mpp is updated. 4867 * in case of error mpp is freed. 4868 */ 4869 conn_t * 4870 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4871 { 4872 conn_t *connp = tcp->tcp_connp; 4873 conn_t *econnp; 4874 squeue_t *new_sqp; 4875 mblk_t *first_mp = *mpp; 4876 mblk_t *mp = *mpp; 4877 boolean_t mctl_present = B_FALSE; 4878 uint_t ipvers; 4879 4880 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4881 if (econnp == NULL) { 4882 freemsg(first_mp); 4883 return (NULL); 4884 } 4885 if (DB_TYPE(mp) == M_CTL) { 4886 if (mp->b_cont == NULL || 4887 mp->b_cont->b_datap->db_type != M_DATA) { 4888 freemsg(first_mp); 4889 return (NULL); 4890 } 4891 mp = mp->b_cont; 4892 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4893 freemsg(first_mp); 4894 return (NULL); 4895 } 4896 4897 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4898 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4899 mctl_present = B_TRUE; 4900 } else { 4901 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4902 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4903 } 4904 4905 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4906 DB_CKSUMSTART(mp) = 0; 4907 4908 ASSERT(OK_32PTR(mp->b_rptr)); 4909 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4910 if (ipvers == IPV4_VERSION) { 4911 uint16_t *up; 4912 uint32_t ports; 4913 ipha_t *ipha; 4914 4915 ipha = (ipha_t *)mp->b_rptr; 4916 up = (uint16_t *)((uchar_t *)ipha + 4917 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4918 ports = *(uint32_t *)up; 4919 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4920 ipha->ipha_dst, ipha->ipha_src, ports); 4921 } else { 4922 uint16_t *up; 4923 uint32_t ports; 4924 uint16_t ip_hdr_len; 4925 uint8_t *nexthdrp; 4926 ip6_t *ip6h; 4927 tcph_t *tcph; 4928 4929 ip6h = (ip6_t *)mp->b_rptr; 4930 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4931 ip_hdr_len = IPV6_HDR_LEN; 4932 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4933 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4934 CONN_DEC_REF(econnp); 4935 freemsg(first_mp); 4936 return (NULL); 4937 } 4938 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4939 up = (uint16_t *)tcph->th_lport; 4940 ports = *(uint32_t *)up; 4941 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4942 ip6h->ip6_dst, ip6h->ip6_src, ports); 4943 } 4944 4945 /* 4946 * The caller already ensured that there is a sqp present. 4947 */ 4948 econnp->conn_sqp = new_sqp; 4949 econnp->conn_initial_sqp = new_sqp; 4950 4951 if (connp->conn_policy != NULL) { 4952 ipsec_in_t *ii; 4953 ii = (ipsec_in_t *)(first_mp->b_rptr); 4954 ASSERT(ii->ipsec_in_policy == NULL); 4955 IPPH_REFHOLD(connp->conn_policy); 4956 ii->ipsec_in_policy = connp->conn_policy; 4957 4958 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4959 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4960 CONN_DEC_REF(econnp); 4961 freemsg(first_mp); 4962 return (NULL); 4963 } 4964 } 4965 4966 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4967 CONN_DEC_REF(econnp); 4968 freemsg(first_mp); 4969 return (NULL); 4970 } 4971 4972 /* 4973 * If we know we have some policy, pass the "IPSEC" 4974 * options size TCP uses this adjust the MSS. 4975 */ 4976 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 4977 if (mctl_present) { 4978 freeb(first_mp); 4979 *mpp = mp; 4980 } 4981 4982 return (econnp); 4983 } 4984 4985 /* 4986 * tcp_get_conn/tcp_free_conn 4987 * 4988 * tcp_get_conn is used to get a clean tcp connection structure. 4989 * It tries to reuse the connections put on the freelist by the 4990 * time_wait_collector failing which it goes to kmem_cache. This 4991 * way has two benefits compared to just allocating from and 4992 * freeing to kmem_cache. 4993 * 1) The time_wait_collector can free (which includes the cleanup) 4994 * outside the squeue. So when the interrupt comes, we have a clean 4995 * connection sitting in the freelist. Obviously, this buys us 4996 * performance. 4997 * 4998 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 4999 * has multiple disadvantages - tying up the squeue during alloc, and the 5000 * fact that IPSec policy initialization has to happen here which 5001 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5002 * But allocating the conn/tcp in IP land is also not the best since 5003 * we can't check the 'q' and 'q0' which are protected by squeue and 5004 * blindly allocate memory which might have to be freed here if we are 5005 * not allowed to accept the connection. By using the freelist and 5006 * putting the conn/tcp back in freelist, we don't pay a penalty for 5007 * allocating memory without checking 'q/q0' and freeing it if we can't 5008 * accept the connection. 5009 * 5010 * Care should be taken to put the conn back in the same squeue's freelist 5011 * from which it was allocated. Best results are obtained if conn is 5012 * allocated from listener's squeue and freed to the same. Time wait 5013 * collector will free up the freelist is the connection ends up sitting 5014 * there for too long. 5015 */ 5016 void * 5017 tcp_get_conn(void *arg, tcp_stack_t *tcps) 5018 { 5019 tcp_t *tcp = NULL; 5020 conn_t *connp = NULL; 5021 squeue_t *sqp = (squeue_t *)arg; 5022 tcp_squeue_priv_t *tcp_time_wait; 5023 netstack_t *ns; 5024 mblk_t *tcp_rsrv_mp = NULL; 5025 5026 tcp_time_wait = 5027 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5028 5029 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5030 tcp = tcp_time_wait->tcp_free_list; 5031 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5032 if (tcp != NULL) { 5033 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5034 tcp_time_wait->tcp_free_list_cnt--; 5035 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5036 tcp->tcp_time_wait_next = NULL; 5037 connp = tcp->tcp_connp; 5038 connp->conn_flags |= IPCL_REUSED; 5039 5040 ASSERT(tcp->tcp_tcps == NULL); 5041 ASSERT(connp->conn_netstack == NULL); 5042 ASSERT(tcp->tcp_rsrv_mp != NULL); 5043 ns = tcps->tcps_netstack; 5044 netstack_hold(ns); 5045 connp->conn_netstack = ns; 5046 tcp->tcp_tcps = tcps; 5047 TCPS_REFHOLD(tcps); 5048 ipcl_globalhash_insert(connp); 5049 return ((void *)connp); 5050 } 5051 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5052 /* 5053 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until 5054 * this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5055 */ 5056 tcp_rsrv_mp = allocb(0, BPRI_HI); 5057 if (tcp_rsrv_mp == NULL) 5058 return (NULL); 5059 5060 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5061 tcps->tcps_netstack)) == NULL) { 5062 freeb(tcp_rsrv_mp); 5063 return (NULL); 5064 } 5065 5066 tcp = connp->conn_tcp; 5067 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5068 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5069 5070 tcp->tcp_tcps = tcps; 5071 TCPS_REFHOLD(tcps); 5072 5073 return ((void *)connp); 5074 } 5075 5076 /* 5077 * Update the cached label for the given tcp_t. This should be called once per 5078 * connection, and before any packets are sent or tcp_process_options is 5079 * invoked. Returns B_FALSE if the correct label could not be constructed. 5080 */ 5081 static boolean_t 5082 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5083 { 5084 conn_t *connp = tcp->tcp_connp; 5085 5086 if (tcp->tcp_ipversion == IPV4_VERSION) { 5087 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5088 int added; 5089 5090 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5091 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5092 return (B_FALSE); 5093 5094 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5095 if (added == -1) 5096 return (B_FALSE); 5097 tcp->tcp_hdr_len += added; 5098 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5099 tcp->tcp_ip_hdr_len += added; 5100 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5101 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5102 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5103 tcp->tcp_hdr_len); 5104 if (added == -1) 5105 return (B_FALSE); 5106 tcp->tcp_hdr_len += added; 5107 tcp->tcp_tcph = (tcph_t *) 5108 ((uchar_t *)tcp->tcp_tcph + added); 5109 tcp->tcp_ip_hdr_len += added; 5110 } 5111 } else { 5112 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5113 5114 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5115 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5116 return (B_FALSE); 5117 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5118 &tcp->tcp_label_len, optbuf) != 0) 5119 return (B_FALSE); 5120 if (tcp_build_hdrs(tcp) != 0) 5121 return (B_FALSE); 5122 } 5123 5124 connp->conn_ulp_labeled = 1; 5125 5126 return (B_TRUE); 5127 } 5128 5129 /* BEGIN CSTYLED */ 5130 /* 5131 * 5132 * The sockfs ACCEPT path: 5133 * ======================= 5134 * 5135 * The eager is now established in its own perimeter as soon as SYN is 5136 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5137 * completes the accept processing on the acceptor STREAM. The sending 5138 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5139 * listener but a TLI/XTI listener completes the accept processing 5140 * on the listener perimeter. 5141 * 5142 * Common control flow for 3 way handshake: 5143 * ---------------------------------------- 5144 * 5145 * incoming SYN (listener perimeter) -> tcp_rput_data() 5146 * -> tcp_conn_request() 5147 * 5148 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5149 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5150 * 5151 * Sockfs ACCEPT Path: 5152 * ------------------- 5153 * 5154 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5155 * as STREAM entry point) 5156 * 5157 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5158 * 5159 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5160 * association (we are not behind eager's squeue but sockfs is protecting us 5161 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5162 * is changed to point at tcp_wput(). 5163 * 5164 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5165 * listener (done on listener's perimeter). 5166 * 5167 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5168 * accept. 5169 * 5170 * TLI/XTI client ACCEPT path: 5171 * --------------------------- 5172 * 5173 * soaccept() sends T_CONN_RES on the listener STREAM. 5174 * 5175 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5176 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5177 * 5178 * Locks: 5179 * ====== 5180 * 5181 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5182 * and listeners->tcp_eager_next_q. 5183 * 5184 * Referencing: 5185 * ============ 5186 * 5187 * 1) We start out in tcp_conn_request by eager placing a ref on 5188 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5189 * 5190 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5191 * doing so we place a ref on the eager. This ref is finally dropped at the 5192 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5193 * reference is dropped by the squeue framework. 5194 * 5195 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5196 * 5197 * The reference must be released by the same entity that added the reference 5198 * In the above scheme, the eager is the entity that adds and releases the 5199 * references. Note that tcp_accept_finish executes in the squeue of the eager 5200 * (albeit after it is attached to the acceptor stream). Though 1. executes 5201 * in the listener's squeue, the eager is nascent at this point and the 5202 * reference can be considered to have been added on behalf of the eager. 5203 * 5204 * Eager getting a Reset or listener closing: 5205 * ========================================== 5206 * 5207 * Once the listener and eager are linked, the listener never does the unlink. 5208 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5209 * a message on all eager perimeter. The eager then does the unlink, clears 5210 * any pointers to the listener's queue and drops the reference to the 5211 * listener. The listener waits in tcp_close outside the squeue until its 5212 * refcount has dropped to 1. This ensures that the listener has waited for 5213 * all eagers to clear their association with the listener. 5214 * 5215 * Similarly, if eager decides to go away, it can unlink itself and close. 5216 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5217 * the reference to eager is still valid because of the extra ref we put 5218 * in tcp_send_conn_ind. 5219 * 5220 * Listener can always locate the eager under the protection 5221 * of the listener->tcp_eager_lock, and then do a refhold 5222 * on the eager during the accept processing. 5223 * 5224 * The acceptor stream accesses the eager in the accept processing 5225 * based on the ref placed on eager before sending T_conn_ind. 5226 * The only entity that can negate this refhold is a listener close 5227 * which is mutually exclusive with an active acceptor stream. 5228 * 5229 * Eager's reference on the listener 5230 * =================================== 5231 * 5232 * If the accept happens (even on a closed eager) the eager drops its 5233 * reference on the listener at the start of tcp_accept_finish. If the 5234 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5235 * the reference is dropped in tcp_closei_local. If the listener closes, 5236 * the reference is dropped in tcp_eager_kill. In all cases the reference 5237 * is dropped while executing in the eager's context (squeue). 5238 */ 5239 /* END CSTYLED */ 5240 5241 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5242 5243 /* 5244 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5245 * tcp_rput_data will not see any SYN packets. 5246 */ 5247 /* ARGSUSED */ 5248 void 5249 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5250 { 5251 tcph_t *tcph; 5252 uint32_t seg_seq; 5253 tcp_t *eager; 5254 uint_t ipvers; 5255 ipha_t *ipha; 5256 ip6_t *ip6h; 5257 int err; 5258 conn_t *econnp = NULL; 5259 squeue_t *new_sqp; 5260 mblk_t *mp1; 5261 uint_t ip_hdr_len; 5262 conn_t *connp = (conn_t *)arg; 5263 tcp_t *tcp = connp->conn_tcp; 5264 cred_t *credp; 5265 tcp_stack_t *tcps = tcp->tcp_tcps; 5266 ip_stack_t *ipst; 5267 5268 if (tcp->tcp_state != TCPS_LISTEN) 5269 goto error2; 5270 5271 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5272 5273 mutex_enter(&tcp->tcp_eager_lock); 5274 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5275 mutex_exit(&tcp->tcp_eager_lock); 5276 TCP_STAT(tcps, tcp_listendrop); 5277 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5278 if (tcp->tcp_debug) { 5279 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5280 "tcp_conn_request: listen backlog (max=%d) " 5281 "overflow (%d pending) on %s", 5282 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5283 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5284 } 5285 goto error2; 5286 } 5287 5288 if (tcp->tcp_conn_req_cnt_q0 >= 5289 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5290 /* 5291 * Q0 is full. Drop a pending half-open req from the queue 5292 * to make room for the new SYN req. Also mark the time we 5293 * drop a SYN. 5294 * 5295 * A more aggressive defense against SYN attack will 5296 * be to set the "tcp_syn_defense" flag now. 5297 */ 5298 TCP_STAT(tcps, tcp_listendropq0); 5299 tcp->tcp_last_rcv_lbolt = lbolt64; 5300 if (!tcp_drop_q0(tcp)) { 5301 mutex_exit(&tcp->tcp_eager_lock); 5302 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5303 if (tcp->tcp_debug) { 5304 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5305 "tcp_conn_request: listen half-open queue " 5306 "(max=%d) full (%d pending) on %s", 5307 tcps->tcps_conn_req_max_q0, 5308 tcp->tcp_conn_req_cnt_q0, 5309 tcp_display(tcp, NULL, 5310 DISP_PORT_ONLY)); 5311 } 5312 goto error2; 5313 } 5314 } 5315 mutex_exit(&tcp->tcp_eager_lock); 5316 5317 /* 5318 * IP adds STRUIO_EAGER and ensures that the received packet is 5319 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5320 * link local address. If IPSec is enabled, db_struioflag has 5321 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5322 * otherwise an error case if neither of them is set. 5323 */ 5324 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5325 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5326 DB_CKSUMSTART(mp) = 0; 5327 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5328 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5329 if (econnp == NULL) 5330 goto error2; 5331 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5332 econnp->conn_sqp = new_sqp; 5333 econnp->conn_initial_sqp = new_sqp; 5334 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5335 /* 5336 * mp is updated in tcp_get_ipsec_conn(). 5337 */ 5338 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5339 if (econnp == NULL) { 5340 /* 5341 * mp freed by tcp_get_ipsec_conn. 5342 */ 5343 return; 5344 } 5345 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5346 } else { 5347 goto error2; 5348 } 5349 5350 ASSERT(DB_TYPE(mp) == M_DATA); 5351 5352 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5353 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5354 ASSERT(OK_32PTR(mp->b_rptr)); 5355 if (ipvers == IPV4_VERSION) { 5356 ipha = (ipha_t *)mp->b_rptr; 5357 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5358 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5359 } else { 5360 ip6h = (ip6_t *)mp->b_rptr; 5361 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5362 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5363 } 5364 5365 if (tcp->tcp_family == AF_INET) { 5366 ASSERT(ipvers == IPV4_VERSION); 5367 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5368 } else { 5369 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5370 } 5371 5372 if (err) 5373 goto error3; 5374 5375 eager = econnp->conn_tcp; 5376 ASSERT(eager->tcp_ordrel_mp == NULL); 5377 5378 if (!IPCL_IS_NONSTR(econnp)) { 5379 /* 5380 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5381 * at close time, we will always have that to send up. 5382 * Otherwise, we need to do special handling in case the 5383 * allocation fails at that time. 5384 */ 5385 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5386 goto error3; 5387 } 5388 /* Inherit various TCP parameters from the listener */ 5389 eager->tcp_naglim = tcp->tcp_naglim; 5390 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5391 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5392 5393 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5394 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5395 5396 /* 5397 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5398 * If it does not, the eager's receive window will be set to the 5399 * listener's receive window later in this function. 5400 */ 5401 eager->tcp_rwnd = 0; 5402 5403 /* 5404 * Inherit listener's tcp_init_cwnd. Need to do this before 5405 * calling tcp_process_options() where tcp_mss_set() is called 5406 * to set the initial cwnd. 5407 */ 5408 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5409 5410 /* 5411 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5412 * zone id before the accept is completed in tcp_wput_accept(). 5413 */ 5414 econnp->conn_zoneid = connp->conn_zoneid; 5415 econnp->conn_allzones = connp->conn_allzones; 5416 5417 /* Copy nexthop information from listener to eager */ 5418 if (connp->conn_nexthop_set) { 5419 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5420 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5421 } 5422 5423 /* 5424 * TSOL: tsol_input_proc() needs the eager's cred before the 5425 * eager is accepted 5426 */ 5427 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5428 crhold(credp); 5429 5430 ASSERT(econnp->conn_effective_cred == NULL); 5431 if (is_system_labeled()) { 5432 cred_t *cr; 5433 ts_label_t *tsl; 5434 5435 /* 5436 * If this is an MLP connection or a MAC-Exempt connection 5437 * with an unlabeled node, packets are to be 5438 * exchanged using the security label of the received 5439 * SYN packet instead of the server application's label. 5440 */ 5441 if ((cr = msg_getcred(mp, NULL)) != NULL && 5442 (tsl = crgetlabel(cr)) != NULL && 5443 (connp->conn_mlp_type != mlptSingle || 5444 (connp->conn_mac_exempt == B_TRUE && 5445 (tsl->tsl_flags & TSLF_UNLABELED)))) { 5446 if ((econnp->conn_effective_cred = 5447 copycred_from_tslabel(econnp->conn_cred, 5448 tsl, KM_NOSLEEP)) != NULL) { 5449 DTRACE_PROBE2( 5450 syn_accept_peerlabel, 5451 conn_t *, econnp, cred_t *, 5452 econnp->conn_effective_cred); 5453 } else { 5454 DTRACE_PROBE3( 5455 tx__ip__log__error__set__eagercred__tcp, 5456 char *, 5457 "SYN mp(1) label on eager connp(2) failed", 5458 mblk_t *, mp, conn_t *, econnp); 5459 goto error3; 5460 } 5461 } else { 5462 DTRACE_PROBE2(syn_accept, conn_t *, 5463 econnp, cred_t *, econnp->conn_cred) 5464 } 5465 5466 /* 5467 * Verify the destination is allowed to receive packets 5468 * at the security label of the SYN-ACK we are generating. 5469 * tsol_check_dest() may create a new effective cred for 5470 * this connection with a modified label or label flags. 5471 */ 5472 if (IN6_IS_ADDR_V4MAPPED(&econnp->conn_remv6)) { 5473 uint32_t dst; 5474 IN6_V4MAPPED_TO_IPADDR(&econnp->conn_remv6, dst); 5475 err = tsol_check_dest(CONN_CRED(econnp), &dst, 5476 IPV4_VERSION, B_FALSE, &cr); 5477 } else { 5478 err = tsol_check_dest(CONN_CRED(econnp), 5479 &econnp->conn_remv6, IPV6_VERSION, 5480 B_FALSE, &cr); 5481 } 5482 if (err != 0) 5483 goto error3; 5484 if (cr != NULL) { 5485 if (econnp->conn_effective_cred != NULL) 5486 crfree(econnp->conn_effective_cred); 5487 econnp->conn_effective_cred = cr; 5488 } 5489 5490 /* 5491 * Generate the security label to be used in the text of 5492 * this connection's outgoing packets. 5493 */ 5494 if (!tcp_update_label(eager, CONN_CRED(econnp))) { 5495 DTRACE_PROBE3( 5496 tx__ip__log__error__connrequest__tcp, 5497 char *, "eager connp(1) label on SYN mp(2) failed", 5498 conn_t *, econnp, mblk_t *, mp); 5499 goto error3; 5500 } 5501 } 5502 5503 eager->tcp_hard_binding = B_TRUE; 5504 5505 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5506 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5507 5508 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5509 if (err != 0) { 5510 tcp_bind_hash_remove(eager); 5511 goto error3; 5512 } 5513 5514 /* 5515 * No need to check for multicast destination since ip will only pass 5516 * up multicasts to those that have expressed interest 5517 * TODO: what about rejecting broadcasts? 5518 * Also check that source is not a multicast or broadcast address. 5519 */ 5520 eager->tcp_state = TCPS_SYN_RCVD; 5521 5522 5523 /* 5524 * There should be no ire in the mp as we are being called after 5525 * receiving the SYN. 5526 */ 5527 ASSERT(tcp_ire_mp(&mp) == NULL); 5528 5529 /* 5530 * Adapt our mss, ttl, ... according to information provided in IRE. 5531 */ 5532 5533 if (tcp_adapt_ire(eager, NULL) == 0) { 5534 /* Undo the bind_hash_insert */ 5535 tcp_bind_hash_remove(eager); 5536 goto error3; 5537 } 5538 5539 /* Process all TCP options. */ 5540 tcp_process_options(eager, tcph); 5541 5542 /* Is the other end ECN capable? */ 5543 if (tcps->tcps_ecn_permitted >= 1 && 5544 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5545 eager->tcp_ecn_ok = B_TRUE; 5546 } 5547 5548 /* 5549 * listener->tcp_rq->q_hiwat should be the default window size or a 5550 * window size changed via SO_RCVBUF option. First round up the 5551 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5552 * scale option value if needed. Call tcp_rwnd_set() to finish the 5553 * setting. 5554 * 5555 * Note if there is a rpipe metric associated with the remote host, 5556 * we should not inherit receive window size from listener. 5557 */ 5558 eager->tcp_rwnd = MSS_ROUNDUP( 5559 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5560 eager->tcp_rwnd), eager->tcp_mss); 5561 if (eager->tcp_snd_ws_ok) 5562 tcp_set_ws_value(eager); 5563 /* 5564 * Note that this is the only place tcp_rwnd_set() is called for 5565 * accepting a connection. We need to call it here instead of 5566 * after the 3-way handshake because we need to tell the other 5567 * side our rwnd in the SYN-ACK segment. 5568 */ 5569 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5570 5571 /* 5572 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5573 * via soaccept()->soinheritoptions() which essentially applies 5574 * all the listener options to the new STREAM. The options that we 5575 * need to take care of are: 5576 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5577 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5578 * SO_SNDBUF, SO_RCVBUF. 5579 * 5580 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5581 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5582 * tcp_maxpsz_set() gets called later from 5583 * tcp_accept_finish(), the option takes effect. 5584 * 5585 */ 5586 /* Set the TCP options */ 5587 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5588 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5589 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5590 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5591 eager->tcp_oobinline = tcp->tcp_oobinline; 5592 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5593 eager->tcp_broadcast = tcp->tcp_broadcast; 5594 eager->tcp_useloopback = tcp->tcp_useloopback; 5595 eager->tcp_dontroute = tcp->tcp_dontroute; 5596 eager->tcp_debug = tcp->tcp_debug; 5597 eager->tcp_linger = tcp->tcp_linger; 5598 eager->tcp_lingertime = tcp->tcp_lingertime; 5599 if (tcp->tcp_ka_enabled) 5600 eager->tcp_ka_enabled = 1; 5601 5602 /* Set the IP options */ 5603 econnp->conn_broadcast = connp->conn_broadcast; 5604 econnp->conn_loopback = connp->conn_loopback; 5605 econnp->conn_dontroute = connp->conn_dontroute; 5606 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5607 5608 /* Put a ref on the listener for the eager. */ 5609 CONN_INC_REF(connp); 5610 mutex_enter(&tcp->tcp_eager_lock); 5611 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5612 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5613 tcp->tcp_eager_next_q0 = eager; 5614 eager->tcp_eager_prev_q0 = tcp; 5615 5616 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5617 eager->tcp_listener = tcp; 5618 eager->tcp_saved_listener = tcp; 5619 5620 /* 5621 * Tag this detached tcp vector for later retrieval 5622 * by our listener client in tcp_accept(). 5623 */ 5624 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5625 tcp->tcp_conn_req_cnt_q0++; 5626 if (++tcp->tcp_conn_req_seqnum == -1) { 5627 /* 5628 * -1 is "special" and defined in TPI as something 5629 * that should never be used in T_CONN_IND 5630 */ 5631 ++tcp->tcp_conn_req_seqnum; 5632 } 5633 mutex_exit(&tcp->tcp_eager_lock); 5634 5635 if (tcp->tcp_syn_defense) { 5636 /* Don't drop the SYN that comes from a good IP source */ 5637 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5638 if (addr_cache != NULL && eager->tcp_remote == 5639 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5640 eager->tcp_dontdrop = B_TRUE; 5641 } 5642 } 5643 5644 /* 5645 * We need to insert the eager in its own perimeter but as soon 5646 * as we do that, we expose the eager to the classifier and 5647 * should not touch any field outside the eager's perimeter. 5648 * So do all the work necessary before inserting the eager 5649 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5650 * will succeed but undo everything if it fails. 5651 */ 5652 seg_seq = ABE32_TO_U32(tcph->th_seq); 5653 eager->tcp_irs = seg_seq; 5654 eager->tcp_rack = seg_seq; 5655 eager->tcp_rnxt = seg_seq + 1; 5656 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5657 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5658 eager->tcp_state = TCPS_SYN_RCVD; 5659 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5660 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5661 if (mp1 == NULL) { 5662 /* 5663 * Increment the ref count as we are going to 5664 * enqueueing an mp in squeue 5665 */ 5666 CONN_INC_REF(econnp); 5667 goto error; 5668 } 5669 5670 /* 5671 * Note that in theory this should use the current pid 5672 * so that getpeerucred on the client returns the actual listener 5673 * that does accept. But accept() hasn't been called yet. We could use 5674 * the pid of the process that did bind/listen on the server. 5675 * However, with common usage like inetd() the bind/listen can be done 5676 * by a different process than the accept(). 5677 * Hence we do the simple thing of using the open pid here. 5678 * Note that db_credp is set later in tcp_send_data(). 5679 */ 5680 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5681 eager->tcp_cpid = tcp->tcp_cpid; 5682 eager->tcp_open_time = lbolt64; 5683 5684 /* 5685 * We need to start the rto timer. In normal case, we start 5686 * the timer after sending the packet on the wire (or at 5687 * least believing that packet was sent by waiting for 5688 * CALL_IP_WPUT() to return). Since this is the first packet 5689 * being sent on the wire for the eager, our initial tcp_rto 5690 * is at least tcp_rexmit_interval_min which is a fairly 5691 * large value to allow the algorithm to adjust slowly to large 5692 * fluctuations of RTT during first few transmissions. 5693 * 5694 * Starting the timer first and then sending the packet in this 5695 * case shouldn't make much difference since tcp_rexmit_interval_min 5696 * is of the order of several 100ms and starting the timer 5697 * first and then sending the packet will result in difference 5698 * of few micro seconds. 5699 * 5700 * Without this optimization, we are forced to hold the fanout 5701 * lock across the ipcl_bind_insert() and sending the packet 5702 * so that we don't race against an incoming packet (maybe RST) 5703 * for this eager. 5704 * 5705 * It is necessary to acquire an extra reference on the eager 5706 * at this point and hold it until after tcp_send_data() to 5707 * ensure against an eager close race. 5708 */ 5709 5710 CONN_INC_REF(eager->tcp_connp); 5711 5712 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5713 5714 /* 5715 * Insert the eager in its own perimeter now. We are ready to deal 5716 * with any packets on eager. 5717 */ 5718 if (eager->tcp_ipversion == IPV4_VERSION) { 5719 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5720 goto error; 5721 } 5722 } else { 5723 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5724 goto error; 5725 } 5726 } 5727 5728 /* mark conn as fully-bound */ 5729 econnp->conn_fully_bound = B_TRUE; 5730 5731 /* Send the SYN-ACK */ 5732 tcp_send_data(eager, eager->tcp_wq, mp1); 5733 CONN_DEC_REF(eager->tcp_connp); 5734 freemsg(mp); 5735 5736 return; 5737 error: 5738 freemsg(mp1); 5739 eager->tcp_closemp_used = B_TRUE; 5740 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5741 mp1 = &eager->tcp_closemp; 5742 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5743 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5744 5745 /* 5746 * If a connection already exists, send the mp to that connections so 5747 * that it can be appropriately dealt with. 5748 */ 5749 ipst = tcps->tcps_netstack->netstack_ip; 5750 5751 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5752 if (!IPCL_IS_CONNECTED(econnp)) { 5753 /* 5754 * Something bad happened. ipcl_conn_insert() 5755 * failed because a connection already existed 5756 * in connected hash but we can't find it 5757 * anymore (someone blew it away). Just 5758 * free this message and hopefully remote 5759 * will retransmit at which time the SYN can be 5760 * treated as a new connection or dealth with 5761 * a TH_RST if a connection already exists. 5762 */ 5763 CONN_DEC_REF(econnp); 5764 freemsg(mp); 5765 } else { 5766 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5767 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5768 } 5769 } else { 5770 /* Nobody wants this packet */ 5771 freemsg(mp); 5772 } 5773 return; 5774 error3: 5775 CONN_DEC_REF(econnp); 5776 error2: 5777 freemsg(mp); 5778 } 5779 5780 /* 5781 * In an ideal case of vertical partition in NUMA architecture, its 5782 * beneficial to have the listener and all the incoming connections 5783 * tied to the same squeue. The other constraint is that incoming 5784 * connections should be tied to the squeue attached to interrupted 5785 * CPU for obvious locality reason so this leaves the listener to 5786 * be tied to the same squeue. Our only problem is that when listener 5787 * is binding, the CPU that will get interrupted by the NIC whose 5788 * IP address the listener is binding to is not even known. So 5789 * the code below allows us to change that binding at the time the 5790 * CPU is interrupted by virtue of incoming connection's squeue. 5791 * 5792 * This is usefull only in case of a listener bound to a specific IP 5793 * address. For other kind of listeners, they get bound the 5794 * very first time and there is no attempt to rebind them. 5795 */ 5796 void 5797 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5798 { 5799 conn_t *connp = (conn_t *)arg; 5800 squeue_t *sqp = (squeue_t *)arg2; 5801 squeue_t *new_sqp; 5802 uint32_t conn_flags; 5803 5804 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5805 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5806 } else { 5807 goto done; 5808 } 5809 5810 if (connp->conn_fanout == NULL) 5811 goto done; 5812 5813 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5814 mutex_enter(&connp->conn_fanout->connf_lock); 5815 mutex_enter(&connp->conn_lock); 5816 /* 5817 * No one from read or write side can access us now 5818 * except for already queued packets on this squeue. 5819 * But since we haven't changed the squeue yet, they 5820 * can't execute. If they are processed after we have 5821 * changed the squeue, they are sent back to the 5822 * correct squeue down below. 5823 * But a listner close can race with processing of 5824 * incoming SYN. If incoming SYN processing changes 5825 * the squeue then the listener close which is waiting 5826 * to enter the squeue would operate on the wrong 5827 * squeue. Hence we don't change the squeue here unless 5828 * the refcount is exactly the minimum refcount. The 5829 * minimum refcount of 4 is counted as - 1 each for 5830 * TCP and IP, 1 for being in the classifier hash, and 5831 * 1 for the mblk being processed. 5832 */ 5833 5834 if (connp->conn_ref != 4 || 5835 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5836 mutex_exit(&connp->conn_lock); 5837 mutex_exit(&connp->conn_fanout->connf_lock); 5838 goto done; 5839 } 5840 if (connp->conn_sqp != new_sqp) { 5841 while (connp->conn_sqp != new_sqp) 5842 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5843 } 5844 5845 do { 5846 conn_flags = connp->conn_flags; 5847 conn_flags |= IPCL_FULLY_BOUND; 5848 (void) cas32(&connp->conn_flags, connp->conn_flags, 5849 conn_flags); 5850 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5851 5852 mutex_exit(&connp->conn_fanout->connf_lock); 5853 mutex_exit(&connp->conn_lock); 5854 } 5855 5856 done: 5857 if (connp->conn_sqp != sqp) { 5858 CONN_INC_REF(connp); 5859 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5860 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5861 } else { 5862 tcp_conn_request(connp, mp, sqp); 5863 } 5864 } 5865 5866 /* 5867 * Successful connect request processing begins when our client passes 5868 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5869 * our T_OK_ACK reply message upstream. The control flow looks like this: 5870 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5871 * upstream <- tcp_rput() <- IP 5872 * After various error checks are completed, tcp_tpi_connect() lays 5873 * the target address and port into the composite header template, 5874 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5875 * request followed by an IRE request, and passes the three mblk message 5876 * down to IP looking like this: 5877 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5878 * Processing continues in tcp_rput() when we receive the following message: 5879 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5880 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5881 * to fire off the connection request, and then passes the T_OK_ACK mblk 5882 * upstream that we filled in below. There are, of course, numerous 5883 * error conditions along the way which truncate the processing described 5884 * above. 5885 */ 5886 static void 5887 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5888 { 5889 sin_t *sin; 5890 queue_t *q = tcp->tcp_wq; 5891 struct T_conn_req *tcr; 5892 struct sockaddr *sa; 5893 socklen_t len; 5894 int error; 5895 cred_t *cr; 5896 pid_t cpid; 5897 5898 /* 5899 * All Solaris components should pass a db_credp 5900 * for this TPI message, hence we ASSERT. 5901 * But in case there is some other M_PROTO that looks 5902 * like a TPI message sent by some other kernel 5903 * component, we check and return an error. 5904 */ 5905 cr = msg_getcred(mp, &cpid); 5906 ASSERT(cr != NULL); 5907 if (cr == NULL) { 5908 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5909 return; 5910 } 5911 5912 tcr = (struct T_conn_req *)mp->b_rptr; 5913 5914 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5915 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5916 tcp_err_ack(tcp, mp, TPROTO, 0); 5917 return; 5918 } 5919 5920 /* 5921 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5922 * will always have that to send up. Otherwise, we need to do 5923 * special handling in case the allocation fails at that time. 5924 * If the end point is TPI, the tcp_t can be reused and the 5925 * tcp_ordrel_mp may be allocated already. 5926 */ 5927 if (tcp->tcp_ordrel_mp == NULL) { 5928 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5929 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5930 return; 5931 } 5932 } 5933 5934 /* 5935 * Determine packet type based on type of address passed in 5936 * the request should contain an IPv4 or IPv6 address. 5937 * Make sure that address family matches the type of 5938 * family of the the address passed down 5939 */ 5940 switch (tcr->DEST_length) { 5941 default: 5942 tcp_err_ack(tcp, mp, TBADADDR, 0); 5943 return; 5944 5945 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5946 /* 5947 * XXX: The check for valid DEST_length was not there 5948 * in earlier releases and some buggy 5949 * TLI apps (e.g Sybase) got away with not feeding 5950 * in sin_zero part of address. 5951 * We allow that bug to keep those buggy apps humming. 5952 * Test suites require the check on DEST_length. 5953 * We construct a new mblk with valid DEST_length 5954 * free the original so the rest of the code does 5955 * not have to keep track of this special shorter 5956 * length address case. 5957 */ 5958 mblk_t *nmp; 5959 struct T_conn_req *ntcr; 5960 sin_t *nsin; 5961 5962 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5963 tcr->OPT_length, BPRI_HI); 5964 if (nmp == NULL) { 5965 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5966 return; 5967 } 5968 ntcr = (struct T_conn_req *)nmp->b_rptr; 5969 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5970 ntcr->PRIM_type = T_CONN_REQ; 5971 ntcr->DEST_length = sizeof (sin_t); 5972 ntcr->DEST_offset = sizeof (struct T_conn_req); 5973 5974 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5975 *nsin = sin_null; 5976 /* Get pointer to shorter address to copy from original mp */ 5977 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5978 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5979 if (sin == NULL || !OK_32PTR((char *)sin)) { 5980 freemsg(nmp); 5981 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5982 return; 5983 } 5984 nsin->sin_family = sin->sin_family; 5985 nsin->sin_port = sin->sin_port; 5986 nsin->sin_addr = sin->sin_addr; 5987 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5988 nmp->b_wptr = (uchar_t *)&nsin[1]; 5989 if (tcr->OPT_length != 0) { 5990 ntcr->OPT_length = tcr->OPT_length; 5991 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5992 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5993 (uchar_t *)ntcr + ntcr->OPT_offset, 5994 tcr->OPT_length); 5995 nmp->b_wptr += tcr->OPT_length; 5996 } 5997 freemsg(mp); /* original mp freed */ 5998 mp = nmp; /* re-initialize original variables */ 5999 tcr = ntcr; 6000 } 6001 /* FALLTHRU */ 6002 6003 case sizeof (sin_t): 6004 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6005 sizeof (sin_t)); 6006 len = sizeof (sin_t); 6007 break; 6008 6009 case sizeof (sin6_t): 6010 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 6011 sizeof (sin6_t)); 6012 len = sizeof (sin6_t); 6013 break; 6014 } 6015 6016 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 6017 if (error != 0) { 6018 tcp_err_ack(tcp, mp, TSYSERR, error); 6019 return; 6020 } 6021 6022 /* 6023 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6024 * should key on their sequence number and cut them loose. 6025 */ 6026 6027 /* 6028 * If options passed in, feed it for verification and handling 6029 */ 6030 if (tcr->OPT_length != 0) { 6031 mblk_t *ok_mp; 6032 mblk_t *discon_mp; 6033 mblk_t *conn_opts_mp; 6034 int t_error, sys_error, do_disconnect; 6035 6036 conn_opts_mp = NULL; 6037 6038 if (tcp_conprim_opt_process(tcp, mp, 6039 &do_disconnect, &t_error, &sys_error) < 0) { 6040 if (do_disconnect) { 6041 ASSERT(t_error == 0 && sys_error == 0); 6042 discon_mp = mi_tpi_discon_ind(NULL, 6043 ECONNREFUSED, 0); 6044 if (!discon_mp) { 6045 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6046 TSYSERR, ENOMEM); 6047 return; 6048 } 6049 ok_mp = mi_tpi_ok_ack_alloc(mp); 6050 if (!ok_mp) { 6051 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6052 TSYSERR, ENOMEM); 6053 return; 6054 } 6055 qreply(q, ok_mp); 6056 qreply(q, discon_mp); /* no flush! */ 6057 } else { 6058 ASSERT(t_error != 0); 6059 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6060 sys_error); 6061 } 6062 return; 6063 } 6064 /* 6065 * Success in setting options, the mp option buffer represented 6066 * by OPT_length/offset has been potentially modified and 6067 * contains results of option processing. We copy it in 6068 * another mp to save it for potentially influencing returning 6069 * it in T_CONN_CONN. 6070 */ 6071 if (tcr->OPT_length != 0) { /* there are resulting options */ 6072 conn_opts_mp = copyb(mp); 6073 if (!conn_opts_mp) { 6074 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6075 TSYSERR, ENOMEM); 6076 return; 6077 } 6078 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6079 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6080 /* 6081 * Note: 6082 * These resulting option negotiation can include any 6083 * end-to-end negotiation options but there no such 6084 * thing (yet?) in our TCP/IP. 6085 */ 6086 } 6087 } 6088 6089 /* call the non-TPI version */ 6090 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6091 if (error < 0) { 6092 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6093 } else if (error > 0) { 6094 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6095 } else { 6096 mp = mi_tpi_ok_ack_alloc(mp); 6097 } 6098 6099 /* 6100 * Note: Code below is the "failure" case 6101 */ 6102 /* return error ack and blow away saved option results if any */ 6103 connect_failed: 6104 if (mp != NULL) 6105 putnext(tcp->tcp_rq, mp); 6106 else { 6107 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6108 TSYSERR, ENOMEM); 6109 } 6110 } 6111 6112 /* 6113 * Handle connect to IPv4 destinations, including connections for AF_INET6 6114 * sockets connecting to IPv4 mapped IPv6 destinations. 6115 */ 6116 static int 6117 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6118 uint_t srcid, cred_t *cr, pid_t pid) 6119 { 6120 tcph_t *tcph; 6121 mblk_t *mp; 6122 ipaddr_t dstaddr = *dstaddrp; 6123 int32_t oldstate; 6124 uint16_t lport; 6125 int error = 0; 6126 tcp_stack_t *tcps = tcp->tcp_tcps; 6127 6128 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6129 6130 /* Check for attempt to connect to INADDR_ANY */ 6131 if (dstaddr == INADDR_ANY) { 6132 /* 6133 * SunOS 4.x and 4.3 BSD allow an application 6134 * to connect a TCP socket to INADDR_ANY. 6135 * When they do this, the kernel picks the 6136 * address of one interface and uses it 6137 * instead. The kernel usually ends up 6138 * picking the address of the loopback 6139 * interface. This is an undocumented feature. 6140 * However, we provide the same thing here 6141 * in order to have source and binary 6142 * compatibility with SunOS 4.x. 6143 * Update the T_CONN_REQ (sin/sin6) since it is used to 6144 * generate the T_CONN_CON. 6145 */ 6146 dstaddr = htonl(INADDR_LOOPBACK); 6147 *dstaddrp = dstaddr; 6148 } 6149 6150 /* Handle __sin6_src_id if socket not bound to an IP address */ 6151 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6152 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6153 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6154 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6155 tcp->tcp_ipha->ipha_src); 6156 } 6157 6158 /* 6159 * Don't let an endpoint connect to itself. Note that 6160 * the test here does not catch the case where the 6161 * source IP addr was left unspecified by the user. In 6162 * this case, the source addr is set in tcp_adapt_ire() 6163 * using the reply to the T_BIND message that we send 6164 * down to IP here and the check is repeated in tcp_rput_other. 6165 */ 6166 if (dstaddr == tcp->tcp_ipha->ipha_src && 6167 dstport == tcp->tcp_lport) { 6168 error = -TBADADDR; 6169 goto failed; 6170 } 6171 6172 /* 6173 * Verify the destination is allowed to receive packets 6174 * at the security label of the connection we are initiating. 6175 * tsol_check_dest() may create a new effective cred for this 6176 * connection with a modified label or label flags. 6177 */ 6178 if (is_system_labeled()) { 6179 ASSERT(tcp->tcp_connp->conn_effective_cred == NULL); 6180 if ((error = tsol_check_dest(CONN_CRED(tcp->tcp_connp), 6181 &dstaddr, IPV4_VERSION, tcp->tcp_connp->conn_mac_exempt, 6182 &tcp->tcp_connp->conn_effective_cred)) != 0) { 6183 if (error != EHOSTUNREACH) 6184 error = -TSYSERR; 6185 goto failed; 6186 } 6187 } 6188 6189 tcp->tcp_ipha->ipha_dst = dstaddr; 6190 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6191 6192 /* 6193 * Massage a source route if any putting the first hop 6194 * in iph_dst. Compute a starting value for the checksum which 6195 * takes into account that the original iph_dst should be 6196 * included in the checksum but that ip will include the 6197 * first hop in the source route in the tcp checksum. 6198 */ 6199 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6200 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6201 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6202 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6203 if ((int)tcp->tcp_sum < 0) 6204 tcp->tcp_sum--; 6205 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6206 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6207 (tcp->tcp_sum >> 16)); 6208 tcph = tcp->tcp_tcph; 6209 *(uint16_t *)tcph->th_fport = dstport; 6210 tcp->tcp_fport = dstport; 6211 6212 oldstate = tcp->tcp_state; 6213 /* 6214 * At this point the remote destination address and remote port fields 6215 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6216 * have to see which state tcp was in so we can take apropriate action. 6217 */ 6218 if (oldstate == TCPS_IDLE) { 6219 /* 6220 * We support a quick connect capability here, allowing 6221 * clients to transition directly from IDLE to SYN_SENT 6222 * tcp_bindi will pick an unused port, insert the connection 6223 * in the bind hash and transition to BOUND state. 6224 */ 6225 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6226 tcp, B_TRUE); 6227 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6228 B_FALSE, B_FALSE); 6229 if (lport == 0) { 6230 error = -TNOADDR; 6231 goto failed; 6232 } 6233 } 6234 tcp->tcp_state = TCPS_SYN_SENT; 6235 6236 mp = allocb(sizeof (ire_t), BPRI_HI); 6237 if (mp == NULL) { 6238 tcp->tcp_state = oldstate; 6239 error = ENOMEM; 6240 goto failed; 6241 } 6242 6243 mp->b_wptr += sizeof (ire_t); 6244 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6245 tcp->tcp_hard_binding = 1; 6246 6247 /* 6248 * We need to make sure that the conn_recv is set to a non-null 6249 * value before we insert the conn_t into the classifier table. 6250 * This is to avoid a race with an incoming packet which does 6251 * an ipcl_classify(). 6252 */ 6253 tcp->tcp_connp->conn_recv = tcp_input; 6254 6255 if (tcp->tcp_family == AF_INET) { 6256 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6257 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6258 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6259 } else { 6260 in6_addr_t v6src; 6261 if (tcp->tcp_ipversion == IPV4_VERSION) { 6262 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6263 } else { 6264 v6src = tcp->tcp_ip6h->ip6_src; 6265 } 6266 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6267 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6268 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6269 } 6270 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6271 tcp->tcp_active_open = 1; 6272 6273 6274 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6275 failed: 6276 /* return error ack and blow away saved option results if any */ 6277 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6278 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6279 return (error); 6280 } 6281 6282 /* 6283 * Handle connect to IPv6 destinations. 6284 */ 6285 static int 6286 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6287 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6288 { 6289 tcph_t *tcph; 6290 mblk_t *mp; 6291 ip6_rthdr_t *rth; 6292 int32_t oldstate; 6293 uint16_t lport; 6294 tcp_stack_t *tcps = tcp->tcp_tcps; 6295 int error = 0; 6296 conn_t *connp = tcp->tcp_connp; 6297 6298 ASSERT(tcp->tcp_family == AF_INET6); 6299 6300 /* 6301 * If we're here, it means that the destination address is a native 6302 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6303 * reason why it might not be IPv6 is if the socket was bound to an 6304 * IPv4-mapped IPv6 address. 6305 */ 6306 if (tcp->tcp_ipversion != IPV6_VERSION) { 6307 return (-TBADADDR); 6308 } 6309 6310 /* 6311 * Interpret a zero destination to mean loopback. 6312 * Update the T_CONN_REQ (sin/sin6) since it is used to 6313 * generate the T_CONN_CON. 6314 */ 6315 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6316 *dstaddrp = ipv6_loopback; 6317 } 6318 6319 /* Handle __sin6_src_id if socket not bound to an IP address */ 6320 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6321 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6322 connp->conn_zoneid, tcps->tcps_netstack); 6323 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6324 } 6325 6326 /* 6327 * Take care of the scope_id now and add ip6i_t 6328 * if ip6i_t is not already allocated through TCP 6329 * sticky options. At this point tcp_ip6h does not 6330 * have dst info, thus use dstaddrp. 6331 */ 6332 if (scope_id != 0 && 6333 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6334 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6335 ip6i_t *ip6i; 6336 6337 ipp->ipp_ifindex = scope_id; 6338 ip6i = (ip6i_t *)tcp->tcp_iphc; 6339 6340 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6341 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6342 /* Already allocated */ 6343 ip6i->ip6i_flags |= IP6I_IFINDEX; 6344 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6345 ipp->ipp_fields |= IPPF_SCOPE_ID; 6346 } else { 6347 int reterr; 6348 6349 ipp->ipp_fields |= IPPF_SCOPE_ID; 6350 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6351 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6352 reterr = tcp_build_hdrs(tcp); 6353 if (reterr != 0) 6354 goto failed; 6355 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6356 } 6357 } 6358 6359 /* 6360 * Don't let an endpoint connect to itself. Note that 6361 * the test here does not catch the case where the 6362 * source IP addr was left unspecified by the user. In 6363 * this case, the source addr is set in tcp_adapt_ire() 6364 * using the reply to the T_BIND message that we send 6365 * down to IP here and the check is repeated in tcp_rput_other. 6366 */ 6367 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6368 (dstport == tcp->tcp_lport)) { 6369 error = -TBADADDR; 6370 goto failed; 6371 } 6372 6373 /* 6374 * Verify the destination is allowed to receive packets 6375 * at the security label of the connection we are initiating. 6376 * check_dest may create a new effective cred for this 6377 * connection with a modified label or label flags. 6378 */ 6379 if (is_system_labeled()) { 6380 ASSERT(tcp->tcp_connp->conn_effective_cred == NULL); 6381 if ((error = tsol_check_dest(CONN_CRED(tcp->tcp_connp), 6382 dstaddrp, IPV6_VERSION, tcp->tcp_connp->conn_mac_exempt, 6383 &tcp->tcp_connp->conn_effective_cred)) != 0) { 6384 if (error != EHOSTUNREACH) 6385 error = -TSYSERR; 6386 goto failed; 6387 } 6388 } 6389 6390 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6391 tcp->tcp_remote_v6 = *dstaddrp; 6392 tcp->tcp_ip6h->ip6_vcf = 6393 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6394 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6395 6396 /* 6397 * Massage a routing header (if present) putting the first hop 6398 * in ip6_dst. Compute a starting value for the checksum which 6399 * takes into account that the original ip6_dst should be 6400 * included in the checksum but that ip will include the 6401 * first hop in the source route in the tcp checksum. 6402 */ 6403 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6404 if (rth != NULL) { 6405 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6406 tcps->tcps_netstack); 6407 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6408 (tcp->tcp_sum >> 16)); 6409 } else { 6410 tcp->tcp_sum = 0; 6411 } 6412 6413 tcph = tcp->tcp_tcph; 6414 *(uint16_t *)tcph->th_fport = dstport; 6415 tcp->tcp_fport = dstport; 6416 6417 oldstate = tcp->tcp_state; 6418 /* 6419 * At this point the remote destination address and remote port fields 6420 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6421 * have to see which state tcp was in so we can take apropriate action. 6422 */ 6423 if (oldstate == TCPS_IDLE) { 6424 /* 6425 * We support a quick connect capability here, allowing 6426 * clients to transition directly from IDLE to SYN_SENT 6427 * tcp_bindi will pick an unused port, insert the connection 6428 * in the bind hash and transition to BOUND state. 6429 */ 6430 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6431 tcp, B_TRUE); 6432 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6433 B_FALSE, B_FALSE); 6434 if (lport == 0) { 6435 error = -TNOADDR; 6436 goto failed; 6437 } 6438 } 6439 tcp->tcp_state = TCPS_SYN_SENT; 6440 6441 mp = allocb(sizeof (ire_t), BPRI_HI); 6442 if (mp != NULL) { 6443 in6_addr_t v6src; 6444 6445 mp->b_wptr += sizeof (ire_t); 6446 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6447 6448 tcp->tcp_hard_binding = 1; 6449 6450 /* 6451 * We need to make sure that the conn_recv is set to a non-null 6452 * value before we insert the conn_t into the classifier table. 6453 * This is to avoid a race with an incoming packet which does 6454 * an ipcl_classify(). 6455 */ 6456 tcp->tcp_connp->conn_recv = tcp_input; 6457 6458 if (tcp->tcp_ipversion == IPV4_VERSION) { 6459 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6460 } else { 6461 v6src = tcp->tcp_ip6h->ip6_src; 6462 } 6463 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6464 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6465 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6466 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6467 tcp->tcp_active_open = 1; 6468 6469 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6470 } 6471 /* Error case */ 6472 tcp->tcp_state = oldstate; 6473 error = ENOMEM; 6474 6475 failed: 6476 /* return error ack and blow away saved option results if any */ 6477 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6478 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6479 return (error); 6480 } 6481 6482 /* 6483 * We need a stream q for detached closing tcp connections 6484 * to use. Our client hereby indicates that this q is the 6485 * one to use. 6486 */ 6487 static void 6488 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6489 { 6490 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6491 queue_t *q = tcp->tcp_wq; 6492 tcp_stack_t *tcps = tcp->tcp_tcps; 6493 6494 #ifdef NS_DEBUG 6495 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6496 tcps->tcps_netstack->netstack_stackid); 6497 #endif 6498 mp->b_datap->db_type = M_IOCACK; 6499 iocp->ioc_count = 0; 6500 mutex_enter(&tcps->tcps_g_q_lock); 6501 if (tcps->tcps_g_q != NULL) { 6502 mutex_exit(&tcps->tcps_g_q_lock); 6503 iocp->ioc_error = EALREADY; 6504 } else { 6505 int error = 0; 6506 conn_t *connp = tcp->tcp_connp; 6507 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6508 6509 tcps->tcps_g_q = tcp->tcp_rq; 6510 mutex_exit(&tcps->tcps_g_q_lock); 6511 iocp->ioc_error = 0; 6512 iocp->ioc_rval = 0; 6513 /* 6514 * We are passing tcp_sticky_ipp as NULL 6515 * as it is not useful for tcp_default queue 6516 * 6517 * Set conn_recv just in case. 6518 */ 6519 tcp->tcp_connp->conn_recv = tcp_conn_request; 6520 6521 ASSERT(connp->conn_af_isv6); 6522 connp->conn_ulp = IPPROTO_TCP; 6523 6524 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6525 NULL || connp->conn_mac_exempt) { 6526 error = -TBADADDR; 6527 } else { 6528 connp->conn_srcv6 = ipv6_all_zeros; 6529 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6530 } 6531 6532 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6533 } 6534 qreply(q, mp); 6535 } 6536 6537 static int 6538 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6539 { 6540 tcp_t *ltcp = NULL; 6541 conn_t *connp; 6542 tcp_stack_t *tcps = tcp->tcp_tcps; 6543 6544 /* 6545 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6546 * when the stream is in BOUND state. Do not send a reset, 6547 * since the destination IP address is not valid, and it can 6548 * be the initialized value of all zeros (broadcast address). 6549 * 6550 * XXX There won't be any pending bind request to IP. 6551 */ 6552 if (tcp->tcp_state <= TCPS_BOUND) { 6553 if (tcp->tcp_debug) { 6554 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6555 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6556 } 6557 return (TOUTSTATE); 6558 } 6559 6560 6561 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6562 6563 /* 6564 * According to TPI, for non-listeners, ignore seqnum 6565 * and disconnect. 6566 * Following interpretation of -1 seqnum is historical 6567 * and implied TPI ? (TPI only states that for T_CONN_IND, 6568 * a valid seqnum should not be -1). 6569 * 6570 * -1 means disconnect everything 6571 * regardless even on a listener. 6572 */ 6573 6574 int old_state = tcp->tcp_state; 6575 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6576 6577 /* 6578 * The connection can't be on the tcp_time_wait_head list 6579 * since it is not detached. 6580 */ 6581 ASSERT(tcp->tcp_time_wait_next == NULL); 6582 ASSERT(tcp->tcp_time_wait_prev == NULL); 6583 ASSERT(tcp->tcp_time_wait_expire == 0); 6584 ltcp = NULL; 6585 /* 6586 * If it used to be a listener, check to make sure no one else 6587 * has taken the port before switching back to LISTEN state. 6588 */ 6589 if (tcp->tcp_ipversion == IPV4_VERSION) { 6590 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6591 tcp->tcp_ipha->ipha_src, 6592 tcp->tcp_connp->conn_zoneid, ipst); 6593 if (connp != NULL) 6594 ltcp = connp->conn_tcp; 6595 } else { 6596 /* Allow tcp_bound_if listeners? */ 6597 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6598 &tcp->tcp_ip6h->ip6_src, 0, 6599 tcp->tcp_connp->conn_zoneid, ipst); 6600 if (connp != NULL) 6601 ltcp = connp->conn_tcp; 6602 } 6603 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6604 tcp->tcp_state = TCPS_LISTEN; 6605 } else if (old_state > TCPS_BOUND) { 6606 tcp->tcp_conn_req_max = 0; 6607 tcp->tcp_state = TCPS_BOUND; 6608 } 6609 if (ltcp != NULL) 6610 CONN_DEC_REF(ltcp->tcp_connp); 6611 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6612 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6613 } else if (old_state == TCPS_ESTABLISHED || 6614 old_state == TCPS_CLOSE_WAIT) { 6615 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6616 } 6617 6618 if (tcp->tcp_fused) 6619 tcp_unfuse(tcp); 6620 6621 mutex_enter(&tcp->tcp_eager_lock); 6622 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6623 (tcp->tcp_conn_req_cnt_q != 0)) { 6624 tcp_eager_cleanup(tcp, 0); 6625 } 6626 mutex_exit(&tcp->tcp_eager_lock); 6627 6628 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6629 tcp->tcp_rnxt, TH_RST | TH_ACK); 6630 6631 tcp_reinit(tcp); 6632 6633 return (0); 6634 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6635 return (TBADSEQ); 6636 } 6637 return (0); 6638 } 6639 6640 /* 6641 * Our client hereby directs us to reject the connection request 6642 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6643 * of sending the appropriate RST, not an ICMP error. 6644 */ 6645 static void 6646 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6647 { 6648 t_scalar_t seqnum; 6649 int error; 6650 6651 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6652 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6653 tcp_err_ack(tcp, mp, TPROTO, 0); 6654 return; 6655 } 6656 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6657 error = tcp_disconnect_common(tcp, seqnum); 6658 if (error != 0) 6659 tcp_err_ack(tcp, mp, error, 0); 6660 else { 6661 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6662 /* Send M_FLUSH according to TPI */ 6663 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6664 } 6665 mp = mi_tpi_ok_ack_alloc(mp); 6666 if (mp) 6667 putnext(tcp->tcp_rq, mp); 6668 } 6669 } 6670 6671 /* 6672 * Diagnostic routine used to return a string associated with the tcp state. 6673 * Note that if the caller does not supply a buffer, it will use an internal 6674 * static string. This means that if multiple threads call this function at 6675 * the same time, output can be corrupted... Note also that this function 6676 * does not check the size of the supplied buffer. The caller has to make 6677 * sure that it is big enough. 6678 */ 6679 static char * 6680 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6681 { 6682 char buf1[30]; 6683 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6684 char *buf; 6685 char *cp; 6686 in6_addr_t local, remote; 6687 char local_addrbuf[INET6_ADDRSTRLEN]; 6688 char remote_addrbuf[INET6_ADDRSTRLEN]; 6689 6690 if (sup_buf != NULL) 6691 buf = sup_buf; 6692 else 6693 buf = priv_buf; 6694 6695 if (tcp == NULL) 6696 return ("NULL_TCP"); 6697 switch (tcp->tcp_state) { 6698 case TCPS_CLOSED: 6699 cp = "TCP_CLOSED"; 6700 break; 6701 case TCPS_IDLE: 6702 cp = "TCP_IDLE"; 6703 break; 6704 case TCPS_BOUND: 6705 cp = "TCP_BOUND"; 6706 break; 6707 case TCPS_LISTEN: 6708 cp = "TCP_LISTEN"; 6709 break; 6710 case TCPS_SYN_SENT: 6711 cp = "TCP_SYN_SENT"; 6712 break; 6713 case TCPS_SYN_RCVD: 6714 cp = "TCP_SYN_RCVD"; 6715 break; 6716 case TCPS_ESTABLISHED: 6717 cp = "TCP_ESTABLISHED"; 6718 break; 6719 case TCPS_CLOSE_WAIT: 6720 cp = "TCP_CLOSE_WAIT"; 6721 break; 6722 case TCPS_FIN_WAIT_1: 6723 cp = "TCP_FIN_WAIT_1"; 6724 break; 6725 case TCPS_CLOSING: 6726 cp = "TCP_CLOSING"; 6727 break; 6728 case TCPS_LAST_ACK: 6729 cp = "TCP_LAST_ACK"; 6730 break; 6731 case TCPS_FIN_WAIT_2: 6732 cp = "TCP_FIN_WAIT_2"; 6733 break; 6734 case TCPS_TIME_WAIT: 6735 cp = "TCP_TIME_WAIT"; 6736 break; 6737 default: 6738 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6739 cp = buf1; 6740 break; 6741 } 6742 switch (format) { 6743 case DISP_ADDR_AND_PORT: 6744 if (tcp->tcp_ipversion == IPV4_VERSION) { 6745 /* 6746 * Note that we use the remote address in the tcp_b 6747 * structure. This means that it will print out 6748 * the real destination address, not the next hop's 6749 * address if source routing is used. 6750 */ 6751 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6752 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6753 6754 } else { 6755 local = tcp->tcp_ip_src_v6; 6756 remote = tcp->tcp_remote_v6; 6757 } 6758 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6759 sizeof (local_addrbuf)); 6760 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6761 sizeof (remote_addrbuf)); 6762 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6763 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6764 ntohs(tcp->tcp_fport), cp); 6765 break; 6766 case DISP_PORT_ONLY: 6767 default: 6768 (void) mi_sprintf(buf, "[%u, %u] %s", 6769 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6770 break; 6771 } 6772 6773 return (buf); 6774 } 6775 6776 /* 6777 * Called via squeue to get on to eager's perimeter. It sends a 6778 * TH_RST if eager is in the fanout table. The listener wants the 6779 * eager to disappear either by means of tcp_eager_blowoff() or 6780 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6781 * called (via squeue) if the eager cannot be inserted in the 6782 * fanout table in tcp_conn_request(). 6783 */ 6784 /* ARGSUSED */ 6785 void 6786 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6787 { 6788 conn_t *econnp = (conn_t *)arg; 6789 tcp_t *eager = econnp->conn_tcp; 6790 tcp_t *listener = eager->tcp_listener; 6791 tcp_stack_t *tcps = eager->tcp_tcps; 6792 6793 /* 6794 * We could be called because listener is closing. Since 6795 * the eager is using listener's queue's, its not safe. 6796 * Better use the default queue just to send the TH_RST 6797 * out. 6798 */ 6799 ASSERT(tcps->tcps_g_q != NULL); 6800 eager->tcp_rq = tcps->tcps_g_q; 6801 eager->tcp_wq = WR(tcps->tcps_g_q); 6802 6803 /* 6804 * An eager's conn_fanout will be NULL if it's a duplicate 6805 * for an existing 4-tuples in the conn fanout table. 6806 * We don't want to send an RST out in such case. 6807 */ 6808 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6809 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6810 eager, eager->tcp_snxt, 0, TH_RST); 6811 } 6812 6813 /* We are here because listener wants this eager gone */ 6814 if (listener != NULL) { 6815 mutex_enter(&listener->tcp_eager_lock); 6816 tcp_eager_unlink(eager); 6817 if (eager->tcp_tconnind_started) { 6818 /* 6819 * The eager has sent a conn_ind up to the 6820 * listener but listener decides to close 6821 * instead. We need to drop the extra ref 6822 * placed on eager in tcp_rput_data() before 6823 * sending the conn_ind to listener. 6824 */ 6825 CONN_DEC_REF(econnp); 6826 } 6827 mutex_exit(&listener->tcp_eager_lock); 6828 CONN_DEC_REF(listener->tcp_connp); 6829 } 6830 6831 if (eager->tcp_state != TCPS_CLOSED) 6832 tcp_close_detached(eager); 6833 } 6834 6835 /* 6836 * Reset any eager connection hanging off this listener marked 6837 * with 'seqnum' and then reclaim it's resources. 6838 */ 6839 static boolean_t 6840 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6841 { 6842 tcp_t *eager; 6843 mblk_t *mp; 6844 tcp_stack_t *tcps = listener->tcp_tcps; 6845 6846 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6847 eager = listener; 6848 mutex_enter(&listener->tcp_eager_lock); 6849 do { 6850 eager = eager->tcp_eager_next_q; 6851 if (eager == NULL) { 6852 mutex_exit(&listener->tcp_eager_lock); 6853 return (B_FALSE); 6854 } 6855 } while (eager->tcp_conn_req_seqnum != seqnum); 6856 6857 if (eager->tcp_closemp_used) { 6858 mutex_exit(&listener->tcp_eager_lock); 6859 return (B_TRUE); 6860 } 6861 eager->tcp_closemp_used = B_TRUE; 6862 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6863 CONN_INC_REF(eager->tcp_connp); 6864 mutex_exit(&listener->tcp_eager_lock); 6865 mp = &eager->tcp_closemp; 6866 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6867 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6868 return (B_TRUE); 6869 } 6870 6871 /* 6872 * Reset any eager connection hanging off this listener 6873 * and then reclaim it's resources. 6874 */ 6875 static void 6876 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6877 { 6878 tcp_t *eager; 6879 mblk_t *mp; 6880 tcp_stack_t *tcps = listener->tcp_tcps; 6881 6882 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6883 6884 if (!q0_only) { 6885 /* First cleanup q */ 6886 TCP_STAT(tcps, tcp_eager_blowoff_q); 6887 eager = listener->tcp_eager_next_q; 6888 while (eager != NULL) { 6889 if (!eager->tcp_closemp_used) { 6890 eager->tcp_closemp_used = B_TRUE; 6891 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6892 CONN_INC_REF(eager->tcp_connp); 6893 mp = &eager->tcp_closemp; 6894 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6895 tcp_eager_kill, eager->tcp_connp, 6896 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6897 } 6898 eager = eager->tcp_eager_next_q; 6899 } 6900 } 6901 /* Then cleanup q0 */ 6902 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6903 eager = listener->tcp_eager_next_q0; 6904 while (eager != listener) { 6905 if (!eager->tcp_closemp_used) { 6906 eager->tcp_closemp_used = B_TRUE; 6907 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6908 CONN_INC_REF(eager->tcp_connp); 6909 mp = &eager->tcp_closemp; 6910 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6911 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6912 SQTAG_TCP_EAGER_CLEANUP_Q0); 6913 } 6914 eager = eager->tcp_eager_next_q0; 6915 } 6916 } 6917 6918 /* 6919 * If we are an eager connection hanging off a listener that hasn't 6920 * formally accepted the connection yet, get off his list and blow off 6921 * any data that we have accumulated. 6922 */ 6923 static void 6924 tcp_eager_unlink(tcp_t *tcp) 6925 { 6926 tcp_t *listener = tcp->tcp_listener; 6927 6928 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6929 ASSERT(listener != NULL); 6930 if (tcp->tcp_eager_next_q0 != NULL) { 6931 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6932 6933 /* Remove the eager tcp from q0 */ 6934 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6935 tcp->tcp_eager_prev_q0; 6936 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6937 tcp->tcp_eager_next_q0; 6938 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6939 listener->tcp_conn_req_cnt_q0--; 6940 6941 tcp->tcp_eager_next_q0 = NULL; 6942 tcp->tcp_eager_prev_q0 = NULL; 6943 6944 /* 6945 * Take the eager out, if it is in the list of droppable 6946 * eagers. 6947 */ 6948 MAKE_UNDROPPABLE(tcp); 6949 6950 if (tcp->tcp_syn_rcvd_timeout != 0) { 6951 /* we have timed out before */ 6952 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6953 listener->tcp_syn_rcvd_timeout--; 6954 } 6955 } else { 6956 tcp_t **tcpp = &listener->tcp_eager_next_q; 6957 tcp_t *prev = NULL; 6958 6959 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6960 if (tcpp[0] == tcp) { 6961 if (listener->tcp_eager_last_q == tcp) { 6962 /* 6963 * If we are unlinking the last 6964 * element on the list, adjust 6965 * tail pointer. Set tail pointer 6966 * to nil when list is empty. 6967 */ 6968 ASSERT(tcp->tcp_eager_next_q == NULL); 6969 if (listener->tcp_eager_last_q == 6970 listener->tcp_eager_next_q) { 6971 listener->tcp_eager_last_q = 6972 NULL; 6973 } else { 6974 /* 6975 * We won't get here if there 6976 * is only one eager in the 6977 * list. 6978 */ 6979 ASSERT(prev != NULL); 6980 listener->tcp_eager_last_q = 6981 prev; 6982 } 6983 } 6984 tcpp[0] = tcp->tcp_eager_next_q; 6985 tcp->tcp_eager_next_q = NULL; 6986 tcp->tcp_eager_last_q = NULL; 6987 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6988 listener->tcp_conn_req_cnt_q--; 6989 break; 6990 } 6991 prev = tcpp[0]; 6992 } 6993 } 6994 tcp->tcp_listener = NULL; 6995 } 6996 6997 /* Shorthand to generate and send TPI error acks to our client */ 6998 static void 6999 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7000 { 7001 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7002 putnext(tcp->tcp_rq, mp); 7003 } 7004 7005 /* Shorthand to generate and send TPI error acks to our client */ 7006 static void 7007 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7008 int t_error, int sys_error) 7009 { 7010 struct T_error_ack *teackp; 7011 7012 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7013 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7014 teackp = (struct T_error_ack *)mp->b_rptr; 7015 teackp->ERROR_prim = primitive; 7016 teackp->TLI_error = t_error; 7017 teackp->UNIX_error = sys_error; 7018 putnext(tcp->tcp_rq, mp); 7019 } 7020 } 7021 7022 /* 7023 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7024 * but instead the code relies on: 7025 * - the fact that the address of the array and its size never changes 7026 * - the atomic assignment of the elements of the array 7027 */ 7028 /* ARGSUSED */ 7029 static int 7030 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7031 { 7032 int i; 7033 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7034 7035 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7036 if (tcps->tcps_g_epriv_ports[i] != 0) 7037 (void) mi_mpprintf(mp, "%d ", 7038 tcps->tcps_g_epriv_ports[i]); 7039 } 7040 return (0); 7041 } 7042 7043 /* 7044 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7045 * threads from changing it at the same time. 7046 */ 7047 /* ARGSUSED */ 7048 static int 7049 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7050 cred_t *cr) 7051 { 7052 long new_value; 7053 int i; 7054 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7055 7056 /* 7057 * Fail the request if the new value does not lie within the 7058 * port number limits. 7059 */ 7060 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7061 new_value <= 0 || new_value >= 65536) { 7062 return (EINVAL); 7063 } 7064 7065 mutex_enter(&tcps->tcps_epriv_port_lock); 7066 /* Check if the value is already in the list */ 7067 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7068 if (new_value == tcps->tcps_g_epriv_ports[i]) { 7069 mutex_exit(&tcps->tcps_epriv_port_lock); 7070 return (EEXIST); 7071 } 7072 } 7073 /* Find an empty slot */ 7074 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7075 if (tcps->tcps_g_epriv_ports[i] == 0) 7076 break; 7077 } 7078 if (i == tcps->tcps_g_num_epriv_ports) { 7079 mutex_exit(&tcps->tcps_epriv_port_lock); 7080 return (EOVERFLOW); 7081 } 7082 /* Set the new value */ 7083 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 7084 mutex_exit(&tcps->tcps_epriv_port_lock); 7085 return (0); 7086 } 7087 7088 /* 7089 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7090 * threads from changing it at the same time. 7091 */ 7092 /* ARGSUSED */ 7093 static int 7094 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7095 cred_t *cr) 7096 { 7097 long new_value; 7098 int i; 7099 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 7100 7101 /* 7102 * Fail the request if the new value does not lie within the 7103 * port number limits. 7104 */ 7105 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7106 new_value >= 65536) { 7107 return (EINVAL); 7108 } 7109 7110 mutex_enter(&tcps->tcps_epriv_port_lock); 7111 /* Check that the value is already in the list */ 7112 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7113 if (tcps->tcps_g_epriv_ports[i] == new_value) 7114 break; 7115 } 7116 if (i == tcps->tcps_g_num_epriv_ports) { 7117 mutex_exit(&tcps->tcps_epriv_port_lock); 7118 return (ESRCH); 7119 } 7120 /* Clear the value */ 7121 tcps->tcps_g_epriv_ports[i] = 0; 7122 mutex_exit(&tcps->tcps_epriv_port_lock); 7123 return (0); 7124 } 7125 7126 /* Return the TPI/TLI equivalent of our current tcp_state */ 7127 static int 7128 tcp_tpistate(tcp_t *tcp) 7129 { 7130 switch (tcp->tcp_state) { 7131 case TCPS_IDLE: 7132 return (TS_UNBND); 7133 case TCPS_LISTEN: 7134 /* 7135 * Return whether there are outstanding T_CONN_IND waiting 7136 * for the matching T_CONN_RES. Therefore don't count q0. 7137 */ 7138 if (tcp->tcp_conn_req_cnt_q > 0) 7139 return (TS_WRES_CIND); 7140 else 7141 return (TS_IDLE); 7142 case TCPS_BOUND: 7143 return (TS_IDLE); 7144 case TCPS_SYN_SENT: 7145 return (TS_WCON_CREQ); 7146 case TCPS_SYN_RCVD: 7147 /* 7148 * Note: assumption: this has to the active open SYN_RCVD. 7149 * The passive instance is detached in SYN_RCVD stage of 7150 * incoming connection processing so we cannot get request 7151 * for T_info_ack on it. 7152 */ 7153 return (TS_WACK_CRES); 7154 case TCPS_ESTABLISHED: 7155 return (TS_DATA_XFER); 7156 case TCPS_CLOSE_WAIT: 7157 return (TS_WREQ_ORDREL); 7158 case TCPS_FIN_WAIT_1: 7159 return (TS_WIND_ORDREL); 7160 case TCPS_FIN_WAIT_2: 7161 return (TS_WIND_ORDREL); 7162 7163 case TCPS_CLOSING: 7164 case TCPS_LAST_ACK: 7165 case TCPS_TIME_WAIT: 7166 case TCPS_CLOSED: 7167 /* 7168 * Following TS_WACK_DREQ7 is a rendition of "not 7169 * yet TS_IDLE" TPI state. There is no best match to any 7170 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7171 * choose a value chosen that will map to TLI/XTI level 7172 * state of TSTATECHNG (state is process of changing) which 7173 * captures what this dummy state represents. 7174 */ 7175 return (TS_WACK_DREQ7); 7176 default: 7177 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7178 tcp->tcp_state, tcp_display(tcp, NULL, 7179 DISP_PORT_ONLY)); 7180 return (TS_UNBND); 7181 } 7182 } 7183 7184 static void 7185 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7186 { 7187 tcp_stack_t *tcps = tcp->tcp_tcps; 7188 7189 if (tcp->tcp_family == AF_INET6) 7190 *tia = tcp_g_t_info_ack_v6; 7191 else 7192 *tia = tcp_g_t_info_ack; 7193 tia->CURRENT_state = tcp_tpistate(tcp); 7194 tia->OPT_size = tcp_max_optsize; 7195 if (tcp->tcp_mss == 0) { 7196 /* Not yet set - tcp_open does not set mss */ 7197 if (tcp->tcp_ipversion == IPV4_VERSION) 7198 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7199 else 7200 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7201 } else { 7202 tia->TIDU_size = tcp->tcp_mss; 7203 } 7204 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7205 } 7206 7207 static void 7208 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7209 t_uscalar_t cap_bits1) 7210 { 7211 tcap->CAP_bits1 = 0; 7212 7213 if (cap_bits1 & TC1_INFO) { 7214 tcp_copy_info(&tcap->INFO_ack, tcp); 7215 tcap->CAP_bits1 |= TC1_INFO; 7216 } 7217 7218 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7219 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7220 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7221 } 7222 7223 } 7224 7225 /* 7226 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7227 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7228 * tcp_g_t_info_ack. The current state of the stream is copied from 7229 * tcp_state. 7230 */ 7231 static void 7232 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7233 { 7234 t_uscalar_t cap_bits1; 7235 struct T_capability_ack *tcap; 7236 7237 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7238 freemsg(mp); 7239 return; 7240 } 7241 7242 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7243 7244 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7245 mp->b_datap->db_type, T_CAPABILITY_ACK); 7246 if (mp == NULL) 7247 return; 7248 7249 tcap = (struct T_capability_ack *)mp->b_rptr; 7250 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7251 7252 putnext(tcp->tcp_rq, mp); 7253 } 7254 7255 /* 7256 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7257 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7258 * The current state of the stream is copied from tcp_state. 7259 */ 7260 static void 7261 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7262 { 7263 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7264 T_INFO_ACK); 7265 if (!mp) { 7266 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7267 return; 7268 } 7269 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7270 putnext(tcp->tcp_rq, mp); 7271 } 7272 7273 /* Respond to the TPI addr request */ 7274 static void 7275 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7276 { 7277 sin_t *sin; 7278 mblk_t *ackmp; 7279 struct T_addr_ack *taa; 7280 7281 /* Make it large enough for worst case */ 7282 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7283 2 * sizeof (sin6_t), 1); 7284 if (ackmp == NULL) { 7285 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7286 return; 7287 } 7288 7289 if (tcp->tcp_ipversion == IPV6_VERSION) { 7290 tcp_addr_req_ipv6(tcp, ackmp); 7291 return; 7292 } 7293 taa = (struct T_addr_ack *)ackmp->b_rptr; 7294 7295 bzero(taa, sizeof (struct T_addr_ack)); 7296 ackmp->b_wptr = (uchar_t *)&taa[1]; 7297 7298 taa->PRIM_type = T_ADDR_ACK; 7299 ackmp->b_datap->db_type = M_PCPROTO; 7300 7301 /* 7302 * Note: Following code assumes 32 bit alignment of basic 7303 * data structures like sin_t and struct T_addr_ack. 7304 */ 7305 if (tcp->tcp_state >= TCPS_BOUND) { 7306 /* 7307 * Fill in local address 7308 */ 7309 taa->LOCADDR_length = sizeof (sin_t); 7310 taa->LOCADDR_offset = sizeof (*taa); 7311 7312 sin = (sin_t *)&taa[1]; 7313 7314 /* Fill zeroes and then intialize non-zero fields */ 7315 *sin = sin_null; 7316 7317 sin->sin_family = AF_INET; 7318 7319 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7320 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7321 7322 ackmp->b_wptr = (uchar_t *)&sin[1]; 7323 7324 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7325 /* 7326 * Fill in Remote address 7327 */ 7328 taa->REMADDR_length = sizeof (sin_t); 7329 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7330 taa->LOCADDR_length); 7331 7332 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7333 *sin = sin_null; 7334 sin->sin_family = AF_INET; 7335 sin->sin_addr.s_addr = tcp->tcp_remote; 7336 sin->sin_port = tcp->tcp_fport; 7337 7338 ackmp->b_wptr = (uchar_t *)&sin[1]; 7339 } 7340 } 7341 putnext(tcp->tcp_rq, ackmp); 7342 } 7343 7344 /* Assumes that tcp_addr_req gets enough space and alignment */ 7345 static void 7346 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7347 { 7348 sin6_t *sin6; 7349 struct T_addr_ack *taa; 7350 7351 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7352 ASSERT(OK_32PTR(ackmp->b_rptr)); 7353 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7354 2 * sizeof (sin6_t)); 7355 7356 taa = (struct T_addr_ack *)ackmp->b_rptr; 7357 7358 bzero(taa, sizeof (struct T_addr_ack)); 7359 ackmp->b_wptr = (uchar_t *)&taa[1]; 7360 7361 taa->PRIM_type = T_ADDR_ACK; 7362 ackmp->b_datap->db_type = M_PCPROTO; 7363 7364 /* 7365 * Note: Following code assumes 32 bit alignment of basic 7366 * data structures like sin6_t and struct T_addr_ack. 7367 */ 7368 if (tcp->tcp_state >= TCPS_BOUND) { 7369 /* 7370 * Fill in local address 7371 */ 7372 taa->LOCADDR_length = sizeof (sin6_t); 7373 taa->LOCADDR_offset = sizeof (*taa); 7374 7375 sin6 = (sin6_t *)&taa[1]; 7376 *sin6 = sin6_null; 7377 7378 sin6->sin6_family = AF_INET6; 7379 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7380 sin6->sin6_port = tcp->tcp_lport; 7381 7382 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7383 7384 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7385 /* 7386 * Fill in Remote address 7387 */ 7388 taa->REMADDR_length = sizeof (sin6_t); 7389 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7390 taa->LOCADDR_length); 7391 7392 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7393 *sin6 = sin6_null; 7394 sin6->sin6_family = AF_INET6; 7395 sin6->sin6_flowinfo = 7396 tcp->tcp_ip6h->ip6_vcf & 7397 ~IPV6_VERS_AND_FLOW_MASK; 7398 sin6->sin6_addr = tcp->tcp_remote_v6; 7399 sin6->sin6_port = tcp->tcp_fport; 7400 7401 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7402 } 7403 } 7404 putnext(tcp->tcp_rq, ackmp); 7405 } 7406 7407 /* 7408 * Handle reinitialization of a tcp structure. 7409 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7410 */ 7411 static void 7412 tcp_reinit(tcp_t *tcp) 7413 { 7414 mblk_t *mp; 7415 int err; 7416 tcp_stack_t *tcps = tcp->tcp_tcps; 7417 7418 TCP_STAT(tcps, tcp_reinit_calls); 7419 7420 /* tcp_reinit should never be called for detached tcp_t's */ 7421 ASSERT(tcp->tcp_listener == NULL); 7422 ASSERT((tcp->tcp_family == AF_INET && 7423 tcp->tcp_ipversion == IPV4_VERSION) || 7424 (tcp->tcp_family == AF_INET6 && 7425 (tcp->tcp_ipversion == IPV4_VERSION || 7426 tcp->tcp_ipversion == IPV6_VERSION))); 7427 7428 /* Cancel outstanding timers */ 7429 tcp_timers_stop(tcp); 7430 7431 /* 7432 * Reset everything in the state vector, after updating global 7433 * MIB data from instance counters. 7434 */ 7435 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7436 tcp->tcp_ibsegs = 0; 7437 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7438 tcp->tcp_obsegs = 0; 7439 7440 tcp_close_mpp(&tcp->tcp_xmit_head); 7441 if (tcp->tcp_snd_zcopy_aware) 7442 tcp_zcopy_notify(tcp); 7443 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7444 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7445 mutex_enter(&tcp->tcp_non_sq_lock); 7446 if (tcp->tcp_flow_stopped && 7447 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7448 tcp_clrqfull(tcp); 7449 } 7450 mutex_exit(&tcp->tcp_non_sq_lock); 7451 tcp_close_mpp(&tcp->tcp_reass_head); 7452 tcp->tcp_reass_tail = NULL; 7453 if (tcp->tcp_rcv_list != NULL) { 7454 /* Free b_next chain */ 7455 tcp_close_mpp(&tcp->tcp_rcv_list); 7456 tcp->tcp_rcv_last_head = NULL; 7457 tcp->tcp_rcv_last_tail = NULL; 7458 tcp->tcp_rcv_cnt = 0; 7459 } 7460 tcp->tcp_rcv_last_tail = NULL; 7461 7462 if ((mp = tcp->tcp_urp_mp) != NULL) { 7463 freemsg(mp); 7464 tcp->tcp_urp_mp = NULL; 7465 } 7466 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7467 freemsg(mp); 7468 tcp->tcp_urp_mark_mp = NULL; 7469 } 7470 if (tcp->tcp_fused_sigurg_mp != NULL) { 7471 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7472 freeb(tcp->tcp_fused_sigurg_mp); 7473 tcp->tcp_fused_sigurg_mp = NULL; 7474 } 7475 if (tcp->tcp_ordrel_mp != NULL) { 7476 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7477 freeb(tcp->tcp_ordrel_mp); 7478 tcp->tcp_ordrel_mp = NULL; 7479 } 7480 7481 /* 7482 * Following is a union with two members which are 7483 * identical types and size so the following cleanup 7484 * is enough. 7485 */ 7486 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7487 7488 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7489 7490 /* 7491 * The connection can't be on the tcp_time_wait_head list 7492 * since it is not detached. 7493 */ 7494 ASSERT(tcp->tcp_time_wait_next == NULL); 7495 ASSERT(tcp->tcp_time_wait_prev == NULL); 7496 ASSERT(tcp->tcp_time_wait_expire == 0); 7497 7498 if (tcp->tcp_kssl_pending) { 7499 tcp->tcp_kssl_pending = B_FALSE; 7500 7501 /* Don't reset if the initialized by bind. */ 7502 if (tcp->tcp_kssl_ent != NULL) { 7503 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7504 KSSL_NO_PROXY); 7505 } 7506 } 7507 if (tcp->tcp_kssl_ctx != NULL) { 7508 kssl_release_ctx(tcp->tcp_kssl_ctx); 7509 tcp->tcp_kssl_ctx = NULL; 7510 } 7511 7512 /* 7513 * Reset/preserve other values 7514 */ 7515 tcp_reinit_values(tcp); 7516 ipcl_hash_remove(tcp->tcp_connp); 7517 conn_delete_ire(tcp->tcp_connp, NULL); 7518 tcp_ipsec_cleanup(tcp); 7519 7520 if (tcp->tcp_connp->conn_effective_cred != NULL) { 7521 crfree(tcp->tcp_connp->conn_effective_cred); 7522 tcp->tcp_connp->conn_effective_cred = NULL; 7523 } 7524 7525 if (tcp->tcp_conn_req_max != 0) { 7526 /* 7527 * This is the case when a TLI program uses the same 7528 * transport end point to accept a connection. This 7529 * makes the TCP both a listener and acceptor. When 7530 * this connection is closed, we need to set the state 7531 * back to TCPS_LISTEN. Make sure that the eager list 7532 * is reinitialized. 7533 * 7534 * Note that this stream is still bound to the four 7535 * tuples of the previous connection in IP. If a new 7536 * SYN with different foreign address comes in, IP will 7537 * not find it and will send it to the global queue. In 7538 * the global queue, TCP will do a tcp_lookup_listener() 7539 * to find this stream. This works because this stream 7540 * is only removed from connected hash. 7541 * 7542 */ 7543 tcp->tcp_state = TCPS_LISTEN; 7544 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7545 tcp->tcp_eager_next_drop_q0 = tcp; 7546 tcp->tcp_eager_prev_drop_q0 = tcp; 7547 tcp->tcp_connp->conn_recv = tcp_conn_request; 7548 if (tcp->tcp_family == AF_INET6) { 7549 ASSERT(tcp->tcp_connp->conn_af_isv6); 7550 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7551 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7552 } else { 7553 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7554 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7555 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7556 } 7557 } else { 7558 tcp->tcp_state = TCPS_BOUND; 7559 } 7560 7561 /* 7562 * Initialize to default values 7563 * Can't fail since enough header template space already allocated 7564 * at open(). 7565 */ 7566 err = tcp_init_values(tcp); 7567 ASSERT(err == 0); 7568 /* Restore state in tcp_tcph */ 7569 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7570 if (tcp->tcp_ipversion == IPV4_VERSION) 7571 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7572 else 7573 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7574 /* 7575 * Copy of the src addr. in tcp_t is needed in tcp_t 7576 * since the lookup funcs can only lookup on tcp_t 7577 */ 7578 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7579 7580 ASSERT(tcp->tcp_ptpbhn != NULL); 7581 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7582 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7583 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7584 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7585 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7586 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7587 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7588 } 7589 7590 /* 7591 * Force values to zero that need be zero. 7592 * Do not touch values asociated with the BOUND or LISTEN state 7593 * since the connection will end up in that state after the reinit. 7594 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7595 * structure! 7596 */ 7597 static void 7598 tcp_reinit_values(tcp) 7599 tcp_t *tcp; 7600 { 7601 tcp_stack_t *tcps = tcp->tcp_tcps; 7602 7603 #ifndef lint 7604 #define DONTCARE(x) 7605 #define PRESERVE(x) 7606 #else 7607 #define DONTCARE(x) ((x) = (x)) 7608 #define PRESERVE(x) ((x) = (x)) 7609 #endif /* lint */ 7610 7611 PRESERVE(tcp->tcp_bind_hash_port); 7612 PRESERVE(tcp->tcp_bind_hash); 7613 PRESERVE(tcp->tcp_ptpbhn); 7614 PRESERVE(tcp->tcp_acceptor_hash); 7615 PRESERVE(tcp->tcp_ptpahn); 7616 7617 /* Should be ASSERT NULL on these with new code! */ 7618 ASSERT(tcp->tcp_time_wait_next == NULL); 7619 ASSERT(tcp->tcp_time_wait_prev == NULL); 7620 ASSERT(tcp->tcp_time_wait_expire == 0); 7621 PRESERVE(tcp->tcp_state); 7622 PRESERVE(tcp->tcp_rq); 7623 PRESERVE(tcp->tcp_wq); 7624 7625 ASSERT(tcp->tcp_xmit_head == NULL); 7626 ASSERT(tcp->tcp_xmit_last == NULL); 7627 ASSERT(tcp->tcp_unsent == 0); 7628 ASSERT(tcp->tcp_xmit_tail == NULL); 7629 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7630 7631 tcp->tcp_snxt = 0; /* Displayed in mib */ 7632 tcp->tcp_suna = 0; /* Displayed in mib */ 7633 tcp->tcp_swnd = 0; 7634 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7635 7636 ASSERT(tcp->tcp_ibsegs == 0); 7637 ASSERT(tcp->tcp_obsegs == 0); 7638 7639 if (tcp->tcp_iphc != NULL) { 7640 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7641 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7642 } 7643 7644 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7645 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7646 DONTCARE(tcp->tcp_ipha); 7647 DONTCARE(tcp->tcp_ip6h); 7648 DONTCARE(tcp->tcp_ip_hdr_len); 7649 DONTCARE(tcp->tcp_tcph); 7650 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7651 tcp->tcp_valid_bits = 0; 7652 7653 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7654 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7655 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7656 tcp->tcp_last_rcv_lbolt = 0; 7657 7658 tcp->tcp_init_cwnd = 0; 7659 7660 tcp->tcp_urp_last_valid = 0; 7661 tcp->tcp_hard_binding = 0; 7662 tcp->tcp_hard_bound = 0; 7663 PRESERVE(tcp->tcp_cred); 7664 PRESERVE(tcp->tcp_cpid); 7665 PRESERVE(tcp->tcp_open_time); 7666 PRESERVE(tcp->tcp_exclbind); 7667 7668 tcp->tcp_fin_acked = 0; 7669 tcp->tcp_fin_rcvd = 0; 7670 tcp->tcp_fin_sent = 0; 7671 tcp->tcp_ordrel_done = 0; 7672 7673 tcp->tcp_debug = 0; 7674 tcp->tcp_dontroute = 0; 7675 tcp->tcp_broadcast = 0; 7676 7677 tcp->tcp_useloopback = 0; 7678 tcp->tcp_reuseaddr = 0; 7679 tcp->tcp_oobinline = 0; 7680 tcp->tcp_dgram_errind = 0; 7681 7682 tcp->tcp_detached = 0; 7683 tcp->tcp_bind_pending = 0; 7684 tcp->tcp_unbind_pending = 0; 7685 7686 tcp->tcp_snd_ws_ok = B_FALSE; 7687 tcp->tcp_snd_ts_ok = B_FALSE; 7688 tcp->tcp_linger = 0; 7689 tcp->tcp_ka_enabled = 0; 7690 tcp->tcp_zero_win_probe = 0; 7691 7692 tcp->tcp_loopback = 0; 7693 tcp->tcp_refuse = 0; 7694 tcp->tcp_localnet = 0; 7695 tcp->tcp_syn_defense = 0; 7696 tcp->tcp_set_timer = 0; 7697 7698 tcp->tcp_active_open = 0; 7699 tcp->tcp_rexmit = B_FALSE; 7700 tcp->tcp_xmit_zc_clean = B_FALSE; 7701 7702 tcp->tcp_snd_sack_ok = B_FALSE; 7703 PRESERVE(tcp->tcp_recvdstaddr); 7704 tcp->tcp_hwcksum = B_FALSE; 7705 7706 tcp->tcp_ire_ill_check_done = B_FALSE; 7707 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7708 7709 tcp->tcp_mdt = B_FALSE; 7710 tcp->tcp_mdt_hdr_head = 0; 7711 tcp->tcp_mdt_hdr_tail = 0; 7712 7713 tcp->tcp_conn_def_q0 = 0; 7714 tcp->tcp_ip_forward_progress = B_FALSE; 7715 tcp->tcp_anon_priv_bind = 0; 7716 tcp->tcp_ecn_ok = B_FALSE; 7717 7718 tcp->tcp_cwr = B_FALSE; 7719 tcp->tcp_ecn_echo_on = B_FALSE; 7720 tcp->tcp_is_wnd_shrnk = B_FALSE; 7721 7722 if (tcp->tcp_sack_info != NULL) { 7723 if (tcp->tcp_notsack_list != NULL) { 7724 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 7725 tcp); 7726 } 7727 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7728 tcp->tcp_sack_info = NULL; 7729 } 7730 7731 tcp->tcp_rcv_ws = 0; 7732 tcp->tcp_snd_ws = 0; 7733 tcp->tcp_ts_recent = 0; 7734 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7735 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7736 tcp->tcp_if_mtu = 0; 7737 7738 ASSERT(tcp->tcp_reass_head == NULL); 7739 ASSERT(tcp->tcp_reass_tail == NULL); 7740 7741 tcp->tcp_cwnd_cnt = 0; 7742 7743 ASSERT(tcp->tcp_rcv_list == NULL); 7744 ASSERT(tcp->tcp_rcv_last_head == NULL); 7745 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7746 ASSERT(tcp->tcp_rcv_cnt == 0); 7747 7748 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7749 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7750 tcp->tcp_csuna = 0; 7751 7752 tcp->tcp_rto = 0; /* Displayed in MIB */ 7753 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7754 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7755 tcp->tcp_rtt_update = 0; 7756 7757 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7758 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7759 7760 tcp->tcp_rack = 0; /* Displayed in mib */ 7761 tcp->tcp_rack_cnt = 0; 7762 tcp->tcp_rack_cur_max = 0; 7763 tcp->tcp_rack_abs_max = 0; 7764 7765 tcp->tcp_max_swnd = 0; 7766 7767 ASSERT(tcp->tcp_listener == NULL); 7768 7769 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7770 7771 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7772 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7773 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7774 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7775 7776 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7777 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7778 PRESERVE(tcp->tcp_conn_req_max); 7779 PRESERVE(tcp->tcp_conn_req_seqnum); 7780 7781 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7782 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7783 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7784 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7785 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7786 7787 tcp->tcp_lingertime = 0; 7788 7789 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7790 ASSERT(tcp->tcp_urp_mp == NULL); 7791 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7792 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7793 7794 ASSERT(tcp->tcp_eager_next_q == NULL); 7795 ASSERT(tcp->tcp_eager_last_q == NULL); 7796 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7797 tcp->tcp_eager_prev_q0 == NULL) || 7798 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7799 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7800 7801 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7802 tcp->tcp_eager_prev_drop_q0 == NULL) || 7803 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7804 7805 tcp->tcp_client_errno = 0; 7806 7807 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7808 7809 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7810 7811 PRESERVE(tcp->tcp_bound_source_v6); 7812 tcp->tcp_last_sent_len = 0; 7813 tcp->tcp_dupack_cnt = 0; 7814 7815 tcp->tcp_fport = 0; /* Displayed in MIB */ 7816 PRESERVE(tcp->tcp_lport); 7817 7818 PRESERVE(tcp->tcp_acceptor_lockp); 7819 7820 ASSERT(tcp->tcp_ordrel_mp == NULL); 7821 PRESERVE(tcp->tcp_acceptor_id); 7822 DONTCARE(tcp->tcp_ipsec_overhead); 7823 7824 PRESERVE(tcp->tcp_family); 7825 if (tcp->tcp_family == AF_INET6) { 7826 tcp->tcp_ipversion = IPV6_VERSION; 7827 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7828 } else { 7829 tcp->tcp_ipversion = IPV4_VERSION; 7830 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7831 } 7832 7833 tcp->tcp_bound_if = 0; 7834 tcp->tcp_ipv6_recvancillary = 0; 7835 tcp->tcp_recvifindex = 0; 7836 tcp->tcp_recvhops = 0; 7837 tcp->tcp_closed = 0; 7838 tcp->tcp_cleandeathtag = 0; 7839 if (tcp->tcp_hopopts != NULL) { 7840 mi_free(tcp->tcp_hopopts); 7841 tcp->tcp_hopopts = NULL; 7842 tcp->tcp_hopoptslen = 0; 7843 } 7844 ASSERT(tcp->tcp_hopoptslen == 0); 7845 if (tcp->tcp_dstopts != NULL) { 7846 mi_free(tcp->tcp_dstopts); 7847 tcp->tcp_dstopts = NULL; 7848 tcp->tcp_dstoptslen = 0; 7849 } 7850 ASSERT(tcp->tcp_dstoptslen == 0); 7851 if (tcp->tcp_rtdstopts != NULL) { 7852 mi_free(tcp->tcp_rtdstopts); 7853 tcp->tcp_rtdstopts = NULL; 7854 tcp->tcp_rtdstoptslen = 0; 7855 } 7856 ASSERT(tcp->tcp_rtdstoptslen == 0); 7857 if (tcp->tcp_rthdr != NULL) { 7858 mi_free(tcp->tcp_rthdr); 7859 tcp->tcp_rthdr = NULL; 7860 tcp->tcp_rthdrlen = 0; 7861 } 7862 ASSERT(tcp->tcp_rthdrlen == 0); 7863 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7864 7865 /* Reset fusion-related fields */ 7866 tcp->tcp_fused = B_FALSE; 7867 tcp->tcp_unfusable = B_FALSE; 7868 tcp->tcp_fused_sigurg = B_FALSE; 7869 tcp->tcp_direct_sockfs = B_FALSE; 7870 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7871 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7872 tcp->tcp_loopback_peer = NULL; 7873 tcp->tcp_fuse_rcv_hiwater = 0; 7874 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7875 tcp->tcp_fuse_rcv_unread_cnt = 0; 7876 7877 tcp->tcp_lso = B_FALSE; 7878 7879 tcp->tcp_in_ack_unsent = 0; 7880 tcp->tcp_cork = B_FALSE; 7881 tcp->tcp_tconnind_started = B_FALSE; 7882 7883 PRESERVE(tcp->tcp_squeue_bytes); 7884 7885 ASSERT(tcp->tcp_kssl_ctx == NULL); 7886 ASSERT(!tcp->tcp_kssl_pending); 7887 PRESERVE(tcp->tcp_kssl_ent); 7888 7889 tcp->tcp_closemp_used = B_FALSE; 7890 7891 PRESERVE(tcp->tcp_rsrv_mp); 7892 PRESERVE(tcp->tcp_rsrv_mp_lock); 7893 7894 #ifdef DEBUG 7895 DONTCARE(tcp->tcmp_stk[0]); 7896 #endif 7897 7898 PRESERVE(tcp->tcp_connid); 7899 7900 7901 #undef DONTCARE 7902 #undef PRESERVE 7903 } 7904 7905 /* 7906 * Allocate necessary resources and initialize state vector. 7907 * Guaranteed not to fail so that when an error is returned, 7908 * the caller doesn't need to do any additional cleanup. 7909 */ 7910 int 7911 tcp_init(tcp_t *tcp, queue_t *q) 7912 { 7913 int err; 7914 7915 tcp->tcp_rq = q; 7916 tcp->tcp_wq = WR(q); 7917 tcp->tcp_state = TCPS_IDLE; 7918 if ((err = tcp_init_values(tcp)) != 0) 7919 tcp_timers_stop(tcp); 7920 return (err); 7921 } 7922 7923 static int 7924 tcp_init_values(tcp_t *tcp) 7925 { 7926 int err; 7927 tcp_stack_t *tcps = tcp->tcp_tcps; 7928 7929 ASSERT((tcp->tcp_family == AF_INET && 7930 tcp->tcp_ipversion == IPV4_VERSION) || 7931 (tcp->tcp_family == AF_INET6 && 7932 (tcp->tcp_ipversion == IPV4_VERSION || 7933 tcp->tcp_ipversion == IPV6_VERSION))); 7934 7935 /* 7936 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7937 * will be close to tcp_rexmit_interval_initial. By doing this, we 7938 * allow the algorithm to adjust slowly to large fluctuations of RTT 7939 * during first few transmissions of a connection as seen in slow 7940 * links. 7941 */ 7942 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7943 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7944 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7945 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7946 tcps->tcps_conn_grace_period; 7947 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7948 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7949 tcp->tcp_timer_backoff = 0; 7950 tcp->tcp_ms_we_have_waited = 0; 7951 tcp->tcp_last_recv_time = lbolt; 7952 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7953 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7954 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7955 7956 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7957 7958 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7959 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7960 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7961 /* 7962 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7963 * passive open. 7964 */ 7965 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7966 7967 tcp->tcp_naglim = tcps->tcps_naglim_def; 7968 7969 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7970 7971 tcp->tcp_mdt_hdr_head = 0; 7972 tcp->tcp_mdt_hdr_tail = 0; 7973 7974 /* Reset fusion-related fields */ 7975 tcp->tcp_fused = B_FALSE; 7976 tcp->tcp_unfusable = B_FALSE; 7977 tcp->tcp_fused_sigurg = B_FALSE; 7978 tcp->tcp_direct_sockfs = B_FALSE; 7979 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7980 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7981 tcp->tcp_loopback_peer = NULL; 7982 tcp->tcp_fuse_rcv_hiwater = 0; 7983 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7984 tcp->tcp_fuse_rcv_unread_cnt = 0; 7985 7986 /* Initialize the header template */ 7987 if (tcp->tcp_ipversion == IPV4_VERSION) { 7988 err = tcp_header_init_ipv4(tcp); 7989 } else { 7990 err = tcp_header_init_ipv6(tcp); 7991 } 7992 if (err) 7993 return (err); 7994 7995 /* 7996 * Init the window scale to the max so tcp_rwnd_set() won't pare 7997 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7998 */ 7999 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 8000 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 8001 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 8002 8003 tcp->tcp_cork = B_FALSE; 8004 /* 8005 * Init the tcp_debug option. This value determines whether TCP 8006 * calls strlog() to print out debug messages. Doing this 8007 * initialization here means that this value is not inherited thru 8008 * tcp_reinit(). 8009 */ 8010 tcp->tcp_debug = tcps->tcps_dbg; 8011 8012 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 8013 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 8014 8015 return (0); 8016 } 8017 8018 /* 8019 * Initialize the IPv4 header. Loses any record of any IP options. 8020 */ 8021 static int 8022 tcp_header_init_ipv4(tcp_t *tcp) 8023 { 8024 tcph_t *tcph; 8025 uint32_t sum; 8026 conn_t *connp; 8027 tcp_stack_t *tcps = tcp->tcp_tcps; 8028 8029 /* 8030 * This is a simple initialization. If there's 8031 * already a template, it should never be too small, 8032 * so reuse it. Otherwise, allocate space for the new one. 8033 */ 8034 if (tcp->tcp_iphc == NULL) { 8035 ASSERT(tcp->tcp_iphc_len == 0); 8036 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8037 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8038 if (tcp->tcp_iphc == NULL) { 8039 tcp->tcp_iphc_len = 0; 8040 return (ENOMEM); 8041 } 8042 } 8043 8044 /* options are gone; may need a new label */ 8045 connp = tcp->tcp_connp; 8046 connp->conn_mlp_type = mlptSingle; 8047 connp->conn_ulp_labeled = !is_system_labeled(); 8048 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8049 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8050 tcp->tcp_ip6h = NULL; 8051 tcp->tcp_ipversion = IPV4_VERSION; 8052 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8053 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8054 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8055 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8056 tcp->tcp_ipha->ipha_version_and_hdr_length 8057 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8058 tcp->tcp_ipha->ipha_ident = 0; 8059 8060 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8061 tcp->tcp_tos = 0; 8062 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8063 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 8064 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8065 8066 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8067 tcp->tcp_tcph = tcph; 8068 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8069 /* 8070 * IP wants our header length in the checksum field to 8071 * allow it to perform a single pseudo-header+checksum 8072 * calculation on behalf of TCP. 8073 * Include the adjustment for a source route once IP_OPTIONS is set. 8074 */ 8075 sum = sizeof (tcph_t) + tcp->tcp_sum; 8076 sum = (sum >> 16) + (sum & 0xFFFF); 8077 U16_TO_ABE16(sum, tcph->th_sum); 8078 return (0); 8079 } 8080 8081 /* 8082 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8083 */ 8084 static int 8085 tcp_header_init_ipv6(tcp_t *tcp) 8086 { 8087 tcph_t *tcph; 8088 uint32_t sum; 8089 conn_t *connp; 8090 tcp_stack_t *tcps = tcp->tcp_tcps; 8091 8092 /* 8093 * This is a simple initialization. If there's 8094 * already a template, it should never be too small, 8095 * so reuse it. Otherwise, allocate space for the new one. 8096 * Ensure that there is enough space to "downgrade" the tcp_t 8097 * to an IPv4 tcp_t. This requires having space for a full load 8098 * of IPv4 options, as well as a full load of TCP options 8099 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8100 * than a v6 header and a TCP header with a full load of TCP options 8101 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8102 * We want to avoid reallocation in the "downgraded" case when 8103 * processing outbound IPv4 options. 8104 */ 8105 if (tcp->tcp_iphc == NULL) { 8106 ASSERT(tcp->tcp_iphc_len == 0); 8107 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8108 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8109 if (tcp->tcp_iphc == NULL) { 8110 tcp->tcp_iphc_len = 0; 8111 return (ENOMEM); 8112 } 8113 } 8114 8115 /* options are gone; may need a new label */ 8116 connp = tcp->tcp_connp; 8117 connp->conn_mlp_type = mlptSingle; 8118 connp->conn_ulp_labeled = !is_system_labeled(); 8119 8120 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8121 tcp->tcp_ipversion = IPV6_VERSION; 8122 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8123 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8124 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8125 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8126 tcp->tcp_ipha = NULL; 8127 8128 /* Initialize the header template */ 8129 8130 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8131 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8132 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8133 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8134 8135 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8136 tcp->tcp_tcph = tcph; 8137 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8138 /* 8139 * IP wants our header length in the checksum field to 8140 * allow it to perform a single psuedo-header+checksum 8141 * calculation on behalf of TCP. 8142 * Include the adjustment for a source route when IPV6_RTHDR is set. 8143 */ 8144 sum = sizeof (tcph_t) + tcp->tcp_sum; 8145 sum = (sum >> 16) + (sum & 0xFFFF); 8146 U16_TO_ABE16(sum, tcph->th_sum); 8147 return (0); 8148 } 8149 8150 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8151 #define ICMP_MIN_TCP_HDR 8 8152 8153 /* 8154 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8155 * passed up by IP. The message is always received on the correct tcp_t. 8156 * Assumes that IP has pulled up everything up to and including the ICMP header. 8157 */ 8158 void 8159 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8160 { 8161 icmph_t *icmph; 8162 ipha_t *ipha; 8163 int iph_hdr_length; 8164 tcph_t *tcph; 8165 boolean_t ipsec_mctl = B_FALSE; 8166 boolean_t secure; 8167 mblk_t *first_mp = mp; 8168 int32_t new_mss; 8169 uint32_t ratio; 8170 size_t mp_size = MBLKL(mp); 8171 uint32_t seg_seq; 8172 tcp_stack_t *tcps = tcp->tcp_tcps; 8173 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8174 8175 /* Assume IP provides aligned packets - otherwise toss */ 8176 if (!OK_32PTR(mp->b_rptr)) { 8177 freemsg(mp); 8178 return; 8179 } 8180 8181 /* 8182 * Since ICMP errors are normal data marked with M_CTL when sent 8183 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8184 * packets starting with an ipsec_info_t, see ipsec_info.h. 8185 */ 8186 if ((mp_size == sizeof (ipsec_info_t)) && 8187 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8188 ASSERT(mp->b_cont != NULL); 8189 mp = mp->b_cont; 8190 /* IP should have done this */ 8191 ASSERT(OK_32PTR(mp->b_rptr)); 8192 mp_size = MBLKL(mp); 8193 ipsec_mctl = B_TRUE; 8194 } 8195 8196 /* 8197 * Verify that we have a complete outer IP header. If not, drop it. 8198 */ 8199 if (mp_size < sizeof (ipha_t)) { 8200 noticmpv4: 8201 freemsg(first_mp); 8202 return; 8203 } 8204 8205 ipha = (ipha_t *)mp->b_rptr; 8206 /* 8207 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8208 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8209 */ 8210 switch (IPH_HDR_VERSION(ipha)) { 8211 case IPV6_VERSION: 8212 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8213 return; 8214 case IPV4_VERSION: 8215 break; 8216 default: 8217 goto noticmpv4; 8218 } 8219 8220 /* Skip past the outer IP and ICMP headers */ 8221 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8222 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8223 /* 8224 * If we don't have the correct outer IP header length or if the ULP 8225 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8226 * send it upstream. 8227 */ 8228 if (iph_hdr_length < sizeof (ipha_t) || 8229 ipha->ipha_protocol != IPPROTO_ICMP || 8230 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8231 goto noticmpv4; 8232 } 8233 ipha = (ipha_t *)&icmph[1]; 8234 8235 /* Skip past the inner IP and find the ULP header */ 8236 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8237 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8238 /* 8239 * If we don't have the correct inner IP header length or if the ULP 8240 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8241 * bytes of TCP header, drop it. 8242 */ 8243 if (iph_hdr_length < sizeof (ipha_t) || 8244 ipha->ipha_protocol != IPPROTO_TCP || 8245 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8246 goto noticmpv4; 8247 } 8248 8249 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8250 if (ipsec_mctl) { 8251 secure = ipsec_in_is_secure(first_mp); 8252 } else { 8253 secure = B_FALSE; 8254 } 8255 if (secure) { 8256 /* 8257 * If we are willing to accept this in clear 8258 * we don't have to verify policy. 8259 */ 8260 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8261 if (!tcp_check_policy(tcp, first_mp, 8262 ipha, NULL, secure, ipsec_mctl)) { 8263 /* 8264 * tcp_check_policy called 8265 * ip_drop_packet() on failure. 8266 */ 8267 return; 8268 } 8269 } 8270 } 8271 } else if (ipsec_mctl) { 8272 /* 8273 * This is a hard_bound connection. IP has already 8274 * verified policy. We don't have to do it again. 8275 */ 8276 freeb(first_mp); 8277 first_mp = mp; 8278 ipsec_mctl = B_FALSE; 8279 } 8280 8281 seg_seq = ABE32_TO_U32(tcph->th_seq); 8282 /* 8283 * TCP SHOULD check that the TCP sequence number contained in 8284 * payload of the ICMP error message is within the range 8285 * SND.UNA <= SEG.SEQ < SND.NXT. 8286 */ 8287 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8288 /* 8289 * The ICMP message is bogus, just drop it. But if this is 8290 * an ICMP too big message, IP has already changed 8291 * the ire_max_frag to the bogus value. We need to change 8292 * it back. 8293 */ 8294 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8295 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8296 conn_t *connp = tcp->tcp_connp; 8297 ire_t *ire; 8298 int flag; 8299 8300 if (tcp->tcp_ipversion == IPV4_VERSION) { 8301 flag = tcp->tcp_ipha-> 8302 ipha_fragment_offset_and_flags; 8303 } else { 8304 flag = 0; 8305 } 8306 mutex_enter(&connp->conn_lock); 8307 if ((ire = connp->conn_ire_cache) != NULL) { 8308 mutex_enter(&ire->ire_lock); 8309 mutex_exit(&connp->conn_lock); 8310 ire->ire_max_frag = tcp->tcp_if_mtu; 8311 ire->ire_frag_flag |= flag; 8312 mutex_exit(&ire->ire_lock); 8313 } else { 8314 mutex_exit(&connp->conn_lock); 8315 } 8316 } 8317 goto noticmpv4; 8318 } 8319 8320 switch (icmph->icmph_type) { 8321 case ICMP_DEST_UNREACHABLE: 8322 switch (icmph->icmph_code) { 8323 case ICMP_FRAGMENTATION_NEEDED: 8324 /* 8325 * Reduce the MSS based on the new MTU. This will 8326 * eliminate any fragmentation locally. 8327 * N.B. There may well be some funny side-effects on 8328 * the local send policy and the remote receive policy. 8329 * Pending further research, we provide 8330 * tcp_ignore_path_mtu just in case this proves 8331 * disastrous somewhere. 8332 * 8333 * After updating the MSS, retransmit part of the 8334 * dropped segment using the new mss by calling 8335 * tcp_wput_data(). Need to adjust all those 8336 * params to make sure tcp_wput_data() work properly. 8337 */ 8338 if (tcps->tcps_ignore_path_mtu || 8339 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8340 break; 8341 8342 /* 8343 * Decrease the MSS by time stamp options 8344 * IP options and IPSEC options. tcp_hdr_len 8345 * includes time stamp option and IP option 8346 * length. Note that new_mss may be negative 8347 * if tcp_ipsec_overhead is large and the 8348 * icmph_du_mtu is the minimum value, which is 68. 8349 */ 8350 new_mss = ntohs(icmph->icmph_du_mtu) - 8351 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8352 8353 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8354 new_mss); 8355 8356 /* 8357 * Only update the MSS if the new one is 8358 * smaller than the previous one. This is 8359 * to avoid problems when getting multiple 8360 * ICMP errors for the same MTU. 8361 */ 8362 if (new_mss >= tcp->tcp_mss) 8363 break; 8364 8365 /* 8366 * Note that we are using the template header's DF 8367 * bit in the fast path sending. So we need to compare 8368 * the new mss with both tcps_mss_min and ip_pmtu_min. 8369 * And stop doing IPv4 PMTUd if new_mss is less than 8370 * MAX(tcps_mss_min, ip_pmtu_min). 8371 */ 8372 if (new_mss < tcps->tcps_mss_min || 8373 new_mss < ipst->ips_ip_pmtu_min) { 8374 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8375 0; 8376 } 8377 8378 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8379 ASSERT(ratio >= 1); 8380 tcp_mss_set(tcp, new_mss, B_TRUE); 8381 8382 /* 8383 * Make sure we have something to 8384 * send. 8385 */ 8386 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8387 (tcp->tcp_xmit_head != NULL)) { 8388 /* 8389 * Shrink tcp_cwnd in 8390 * proportion to the old MSS/new MSS. 8391 */ 8392 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8393 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8394 (tcp->tcp_unsent == 0)) { 8395 tcp->tcp_rexmit_max = tcp->tcp_fss; 8396 } else { 8397 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8398 } 8399 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8400 tcp->tcp_rexmit = B_TRUE; 8401 tcp->tcp_dupack_cnt = 0; 8402 tcp->tcp_snd_burst = TCP_CWND_SS; 8403 tcp_ss_rexmit(tcp); 8404 } 8405 break; 8406 case ICMP_PORT_UNREACHABLE: 8407 case ICMP_PROTOCOL_UNREACHABLE: 8408 switch (tcp->tcp_state) { 8409 case TCPS_SYN_SENT: 8410 case TCPS_SYN_RCVD: 8411 /* 8412 * ICMP can snipe away incipient 8413 * TCP connections as long as 8414 * seq number is same as initial 8415 * send seq number. 8416 */ 8417 if (seg_seq == tcp->tcp_iss) { 8418 (void) tcp_clean_death(tcp, 8419 ECONNREFUSED, 6); 8420 } 8421 break; 8422 } 8423 break; 8424 case ICMP_HOST_UNREACHABLE: 8425 case ICMP_NET_UNREACHABLE: 8426 /* Record the error in case we finally time out. */ 8427 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8428 tcp->tcp_client_errno = EHOSTUNREACH; 8429 else 8430 tcp->tcp_client_errno = ENETUNREACH; 8431 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8432 if (tcp->tcp_listener != NULL && 8433 tcp->tcp_listener->tcp_syn_defense) { 8434 /* 8435 * Ditch the half-open connection if we 8436 * suspect a SYN attack is under way. 8437 */ 8438 tcp_ip_ire_mark_advice(tcp); 8439 (void) tcp_clean_death(tcp, 8440 tcp->tcp_client_errno, 7); 8441 } 8442 } 8443 break; 8444 default: 8445 break; 8446 } 8447 break; 8448 case ICMP_SOURCE_QUENCH: { 8449 /* 8450 * use a global boolean to control 8451 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8452 * The default is false. 8453 */ 8454 if (tcp_icmp_source_quench) { 8455 /* 8456 * Reduce the sending rate as if we got a 8457 * retransmit timeout 8458 */ 8459 uint32_t npkt; 8460 8461 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8462 tcp->tcp_mss; 8463 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8464 tcp->tcp_cwnd = tcp->tcp_mss; 8465 tcp->tcp_cwnd_cnt = 0; 8466 } 8467 break; 8468 } 8469 } 8470 freemsg(first_mp); 8471 } 8472 8473 /* 8474 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8475 * error messages passed up by IP. 8476 * Assumes that IP has pulled up all the extension headers as well 8477 * as the ICMPv6 header. 8478 */ 8479 static void 8480 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8481 { 8482 icmp6_t *icmp6; 8483 ip6_t *ip6h; 8484 uint16_t iph_hdr_length; 8485 tcpha_t *tcpha; 8486 uint8_t *nexthdrp; 8487 uint32_t new_mss; 8488 uint32_t ratio; 8489 boolean_t secure; 8490 mblk_t *first_mp = mp; 8491 size_t mp_size; 8492 uint32_t seg_seq; 8493 tcp_stack_t *tcps = tcp->tcp_tcps; 8494 8495 /* 8496 * The caller has determined if this is an IPSEC_IN packet and 8497 * set ipsec_mctl appropriately (see tcp_icmp_error). 8498 */ 8499 if (ipsec_mctl) 8500 mp = mp->b_cont; 8501 8502 mp_size = MBLKL(mp); 8503 8504 /* 8505 * Verify that we have a complete IP header. If not, send it upstream. 8506 */ 8507 if (mp_size < sizeof (ip6_t)) { 8508 noticmpv6: 8509 freemsg(first_mp); 8510 return; 8511 } 8512 8513 /* 8514 * Verify this is an ICMPV6 packet, else send it upstream. 8515 */ 8516 ip6h = (ip6_t *)mp->b_rptr; 8517 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8518 iph_hdr_length = IPV6_HDR_LEN; 8519 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8520 &nexthdrp) || 8521 *nexthdrp != IPPROTO_ICMPV6) { 8522 goto noticmpv6; 8523 } 8524 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8525 ip6h = (ip6_t *)&icmp6[1]; 8526 /* 8527 * Verify if we have a complete ICMP and inner IP header. 8528 */ 8529 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8530 goto noticmpv6; 8531 8532 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8533 goto noticmpv6; 8534 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8535 /* 8536 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8537 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8538 * packet. 8539 */ 8540 if ((*nexthdrp != IPPROTO_TCP) || 8541 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8542 goto noticmpv6; 8543 } 8544 8545 /* 8546 * ICMP errors come on the right queue or come on 8547 * listener/global queue for detached connections and 8548 * get switched to the right queue. If it comes on the 8549 * right queue, policy check has already been done by IP 8550 * and thus free the first_mp without verifying the policy. 8551 * If it has come for a non-hard bound connection, we need 8552 * to verify policy as IP may not have done it. 8553 */ 8554 if (!tcp->tcp_hard_bound) { 8555 if (ipsec_mctl) { 8556 secure = ipsec_in_is_secure(first_mp); 8557 } else { 8558 secure = B_FALSE; 8559 } 8560 if (secure) { 8561 /* 8562 * If we are willing to accept this in clear 8563 * we don't have to verify policy. 8564 */ 8565 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8566 if (!tcp_check_policy(tcp, first_mp, 8567 NULL, ip6h, secure, ipsec_mctl)) { 8568 /* 8569 * tcp_check_policy called 8570 * ip_drop_packet() on failure. 8571 */ 8572 return; 8573 } 8574 } 8575 } 8576 } else if (ipsec_mctl) { 8577 /* 8578 * This is a hard_bound connection. IP has already 8579 * verified policy. We don't have to do it again. 8580 */ 8581 freeb(first_mp); 8582 first_mp = mp; 8583 ipsec_mctl = B_FALSE; 8584 } 8585 8586 seg_seq = ntohl(tcpha->tha_seq); 8587 /* 8588 * TCP SHOULD check that the TCP sequence number contained in 8589 * payload of the ICMP error message is within the range 8590 * SND.UNA <= SEG.SEQ < SND.NXT. 8591 */ 8592 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8593 /* 8594 * If the ICMP message is bogus, should we kill the 8595 * connection, or should we just drop the bogus ICMP 8596 * message? It would probably make more sense to just 8597 * drop the message so that if this one managed to get 8598 * in, the real connection should not suffer. 8599 */ 8600 goto noticmpv6; 8601 } 8602 8603 switch (icmp6->icmp6_type) { 8604 case ICMP6_PACKET_TOO_BIG: 8605 /* 8606 * Reduce the MSS based on the new MTU. This will 8607 * eliminate any fragmentation locally. 8608 * N.B. There may well be some funny side-effects on 8609 * the local send policy and the remote receive policy. 8610 * Pending further research, we provide 8611 * tcp_ignore_path_mtu just in case this proves 8612 * disastrous somewhere. 8613 * 8614 * After updating the MSS, retransmit part of the 8615 * dropped segment using the new mss by calling 8616 * tcp_wput_data(). Need to adjust all those 8617 * params to make sure tcp_wput_data() work properly. 8618 */ 8619 if (tcps->tcps_ignore_path_mtu) 8620 break; 8621 8622 /* 8623 * Decrease the MSS by time stamp options 8624 * IP options and IPSEC options. tcp_hdr_len 8625 * includes time stamp option and IP option 8626 * length. 8627 */ 8628 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8629 tcp->tcp_ipsec_overhead; 8630 8631 /* 8632 * Only update the MSS if the new one is 8633 * smaller than the previous one. This is 8634 * to avoid problems when getting multiple 8635 * ICMP errors for the same MTU. 8636 */ 8637 if (new_mss >= tcp->tcp_mss) 8638 break; 8639 8640 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8641 ASSERT(ratio >= 1); 8642 tcp_mss_set(tcp, new_mss, B_TRUE); 8643 8644 /* 8645 * Make sure we have something to 8646 * send. 8647 */ 8648 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8649 (tcp->tcp_xmit_head != NULL)) { 8650 /* 8651 * Shrink tcp_cwnd in 8652 * proportion to the old MSS/new MSS. 8653 */ 8654 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8655 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8656 (tcp->tcp_unsent == 0)) { 8657 tcp->tcp_rexmit_max = tcp->tcp_fss; 8658 } else { 8659 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8660 } 8661 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8662 tcp->tcp_rexmit = B_TRUE; 8663 tcp->tcp_dupack_cnt = 0; 8664 tcp->tcp_snd_burst = TCP_CWND_SS; 8665 tcp_ss_rexmit(tcp); 8666 } 8667 break; 8668 8669 case ICMP6_DST_UNREACH: 8670 switch (icmp6->icmp6_code) { 8671 case ICMP6_DST_UNREACH_NOPORT: 8672 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8673 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8674 (seg_seq == tcp->tcp_iss)) { 8675 (void) tcp_clean_death(tcp, 8676 ECONNREFUSED, 8); 8677 } 8678 break; 8679 8680 case ICMP6_DST_UNREACH_ADMIN: 8681 case ICMP6_DST_UNREACH_NOROUTE: 8682 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8683 case ICMP6_DST_UNREACH_ADDR: 8684 /* Record the error in case we finally time out. */ 8685 tcp->tcp_client_errno = EHOSTUNREACH; 8686 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8687 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8688 (seg_seq == tcp->tcp_iss)) { 8689 if (tcp->tcp_listener != NULL && 8690 tcp->tcp_listener->tcp_syn_defense) { 8691 /* 8692 * Ditch the half-open connection if we 8693 * suspect a SYN attack is under way. 8694 */ 8695 tcp_ip_ire_mark_advice(tcp); 8696 (void) tcp_clean_death(tcp, 8697 tcp->tcp_client_errno, 9); 8698 } 8699 } 8700 8701 8702 break; 8703 default: 8704 break; 8705 } 8706 break; 8707 8708 case ICMP6_PARAM_PROB: 8709 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8710 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8711 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8712 (uchar_t *)nexthdrp) { 8713 if (tcp->tcp_state == TCPS_SYN_SENT || 8714 tcp->tcp_state == TCPS_SYN_RCVD) { 8715 (void) tcp_clean_death(tcp, 8716 ECONNREFUSED, 10); 8717 } 8718 break; 8719 } 8720 break; 8721 8722 case ICMP6_TIME_EXCEEDED: 8723 default: 8724 break; 8725 } 8726 freemsg(first_mp); 8727 } 8728 8729 /* 8730 * Notify IP that we are having trouble with this connection. IP should 8731 * blow the IRE away and start over. 8732 */ 8733 static void 8734 tcp_ip_notify(tcp_t *tcp) 8735 { 8736 struct iocblk *iocp; 8737 ipid_t *ipid; 8738 mblk_t *mp; 8739 8740 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8741 if (tcp->tcp_ipversion == IPV6_VERSION) 8742 return; 8743 8744 mp = mkiocb(IP_IOCTL); 8745 if (mp == NULL) 8746 return; 8747 8748 iocp = (struct iocblk *)mp->b_rptr; 8749 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8750 8751 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8752 if (!mp->b_cont) { 8753 freeb(mp); 8754 return; 8755 } 8756 8757 ipid = (ipid_t *)mp->b_cont->b_rptr; 8758 mp->b_cont->b_wptr += iocp->ioc_count; 8759 bzero(ipid, sizeof (*ipid)); 8760 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8761 ipid->ipid_ire_type = IRE_CACHE; 8762 ipid->ipid_addr_offset = sizeof (ipid_t); 8763 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8764 /* 8765 * Note: in the case of source routing we want to blow away the 8766 * route to the first source route hop. 8767 */ 8768 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8769 sizeof (tcp->tcp_ipha->ipha_dst)); 8770 8771 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8772 } 8773 8774 /* Unlink and return any mblk that looks like it contains an ire */ 8775 static mblk_t * 8776 tcp_ire_mp(mblk_t **mpp) 8777 { 8778 mblk_t *mp = *mpp; 8779 mblk_t *prev_mp = NULL; 8780 8781 for (;;) { 8782 switch (DB_TYPE(mp)) { 8783 case IRE_DB_TYPE: 8784 case IRE_DB_REQ_TYPE: 8785 if (mp == *mpp) { 8786 *mpp = mp->b_cont; 8787 } else { 8788 prev_mp->b_cont = mp->b_cont; 8789 } 8790 mp->b_cont = NULL; 8791 return (mp); 8792 default: 8793 break; 8794 } 8795 prev_mp = mp; 8796 mp = mp->b_cont; 8797 if (mp == NULL) 8798 break; 8799 } 8800 return (mp); 8801 } 8802 8803 /* 8804 * Timer callback routine for keepalive probe. We do a fake resend of 8805 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8806 * check to see if we have heard anything from the other end for the last 8807 * RTO period. If we have, set the timer to expire for another 8808 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8809 * RTO << 1 and check again when it expires. Keep exponentially increasing 8810 * the timeout if we have not heard from the other side. If for more than 8811 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8812 * kill the connection unless the keepalive abort threshold is 0. In 8813 * that case, we will probe "forever." 8814 */ 8815 static void 8816 tcp_keepalive_killer(void *arg) 8817 { 8818 mblk_t *mp; 8819 conn_t *connp = (conn_t *)arg; 8820 tcp_t *tcp = connp->conn_tcp; 8821 int32_t firetime; 8822 int32_t idletime; 8823 int32_t ka_intrvl; 8824 tcp_stack_t *tcps = tcp->tcp_tcps; 8825 8826 tcp->tcp_ka_tid = 0; 8827 8828 if (tcp->tcp_fused) 8829 return; 8830 8831 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8832 ka_intrvl = tcp->tcp_ka_interval; 8833 8834 /* 8835 * Keepalive probe should only be sent if the application has not 8836 * done a close on the connection. 8837 */ 8838 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8839 return; 8840 } 8841 /* Timer fired too early, restart it. */ 8842 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8843 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8844 MSEC_TO_TICK(ka_intrvl)); 8845 return; 8846 } 8847 8848 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8849 /* 8850 * If we have not heard from the other side for a long 8851 * time, kill the connection unless the keepalive abort 8852 * threshold is 0. In that case, we will probe "forever." 8853 */ 8854 if (tcp->tcp_ka_abort_thres != 0 && 8855 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8856 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8857 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8858 tcp->tcp_client_errno : ETIMEDOUT, 11); 8859 return; 8860 } 8861 8862 if (tcp->tcp_snxt == tcp->tcp_suna && 8863 idletime >= ka_intrvl) { 8864 /* Fake resend of last ACKed byte. */ 8865 mblk_t *mp1 = allocb(1, BPRI_LO); 8866 8867 if (mp1 != NULL) { 8868 *mp1->b_wptr++ = '\0'; 8869 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8870 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8871 freeb(mp1); 8872 /* 8873 * if allocation failed, fall through to start the 8874 * timer back. 8875 */ 8876 if (mp != NULL) { 8877 tcp_send_data(tcp, tcp->tcp_wq, mp); 8878 BUMP_MIB(&tcps->tcps_mib, 8879 tcpTimKeepaliveProbe); 8880 if (tcp->tcp_ka_last_intrvl != 0) { 8881 int max; 8882 /* 8883 * We should probe again at least 8884 * in ka_intrvl, but not more than 8885 * tcp_rexmit_interval_max. 8886 */ 8887 max = tcps->tcps_rexmit_interval_max; 8888 firetime = MIN(ka_intrvl - 1, 8889 tcp->tcp_ka_last_intrvl << 1); 8890 if (firetime > max) 8891 firetime = max; 8892 } else { 8893 firetime = tcp->tcp_rto; 8894 } 8895 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8896 tcp_keepalive_killer, 8897 MSEC_TO_TICK(firetime)); 8898 tcp->tcp_ka_last_intrvl = firetime; 8899 return; 8900 } 8901 } 8902 } else { 8903 tcp->tcp_ka_last_intrvl = 0; 8904 } 8905 8906 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8907 if ((firetime = ka_intrvl - idletime) < 0) { 8908 firetime = ka_intrvl; 8909 } 8910 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8911 MSEC_TO_TICK(firetime)); 8912 } 8913 8914 int 8915 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8916 { 8917 queue_t *q = tcp->tcp_rq; 8918 int32_t mss = tcp->tcp_mss; 8919 int maxpsz; 8920 conn_t *connp = tcp->tcp_connp; 8921 8922 if (TCP_IS_DETACHED(tcp)) 8923 return (mss); 8924 if (tcp->tcp_fused) { 8925 maxpsz = tcp_fuse_maxpsz_set(tcp); 8926 mss = INFPSZ; 8927 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8928 /* 8929 * Set the sd_qn_maxpsz according to the socket send buffer 8930 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8931 * instruct the stream head to copyin user data into contiguous 8932 * kernel-allocated buffers without breaking it up into smaller 8933 * chunks. We round up the buffer size to the nearest SMSS. 8934 */ 8935 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8936 if (tcp->tcp_kssl_ctx == NULL) 8937 mss = INFPSZ; 8938 else 8939 mss = SSL3_MAX_RECORD_LEN; 8940 } else { 8941 /* 8942 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8943 * (and a multiple of the mss). This instructs the stream 8944 * head to break down larger than SMSS writes into SMSS- 8945 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8946 */ 8947 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8948 maxpsz = tcp->tcp_maxpsz * mss; 8949 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8950 maxpsz = tcp->tcp_xmit_hiwater/2; 8951 /* Round up to nearest mss */ 8952 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8953 } 8954 } 8955 8956 (void) proto_set_maxpsz(q, connp, maxpsz); 8957 if (!(IPCL_IS_NONSTR(connp))) { 8958 /* XXX do it in set_maxpsz()? */ 8959 tcp->tcp_wq->q_maxpsz = maxpsz; 8960 } 8961 8962 if (set_maxblk) 8963 (void) proto_set_tx_maxblk(q, connp, mss); 8964 return (mss); 8965 } 8966 8967 /* 8968 * Extract option values from a tcp header. We put any found values into the 8969 * tcpopt struct and return a bitmask saying which options were found. 8970 */ 8971 static int 8972 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8973 { 8974 uchar_t *endp; 8975 int len; 8976 uint32_t mss; 8977 uchar_t *up = (uchar_t *)tcph; 8978 int found = 0; 8979 int32_t sack_len; 8980 tcp_seq sack_begin, sack_end; 8981 tcp_t *tcp; 8982 8983 endp = up + TCP_HDR_LENGTH(tcph); 8984 up += TCP_MIN_HEADER_LENGTH; 8985 while (up < endp) { 8986 len = endp - up; 8987 switch (*up) { 8988 case TCPOPT_EOL: 8989 break; 8990 8991 case TCPOPT_NOP: 8992 up++; 8993 continue; 8994 8995 case TCPOPT_MAXSEG: 8996 if (len < TCPOPT_MAXSEG_LEN || 8997 up[1] != TCPOPT_MAXSEG_LEN) 8998 break; 8999 9000 mss = BE16_TO_U16(up+2); 9001 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9002 tcpopt->tcp_opt_mss = mss; 9003 found |= TCP_OPT_MSS_PRESENT; 9004 9005 up += TCPOPT_MAXSEG_LEN; 9006 continue; 9007 9008 case TCPOPT_WSCALE: 9009 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9010 break; 9011 9012 if (up[2] > TCP_MAX_WINSHIFT) 9013 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9014 else 9015 tcpopt->tcp_opt_wscale = up[2]; 9016 found |= TCP_OPT_WSCALE_PRESENT; 9017 9018 up += TCPOPT_WS_LEN; 9019 continue; 9020 9021 case TCPOPT_SACK_PERMITTED: 9022 if (len < TCPOPT_SACK_OK_LEN || 9023 up[1] != TCPOPT_SACK_OK_LEN) 9024 break; 9025 found |= TCP_OPT_SACK_OK_PRESENT; 9026 up += TCPOPT_SACK_OK_LEN; 9027 continue; 9028 9029 case TCPOPT_SACK: 9030 if (len <= 2 || up[1] <= 2 || len < up[1]) 9031 break; 9032 9033 /* If TCP is not interested in SACK blks... */ 9034 if ((tcp = tcpopt->tcp) == NULL) { 9035 up += up[1]; 9036 continue; 9037 } 9038 sack_len = up[1] - TCPOPT_HEADER_LEN; 9039 up += TCPOPT_HEADER_LEN; 9040 9041 /* 9042 * If the list is empty, allocate one and assume 9043 * nothing is sack'ed. 9044 */ 9045 ASSERT(tcp->tcp_sack_info != NULL); 9046 if (tcp->tcp_notsack_list == NULL) { 9047 tcp_notsack_update(&(tcp->tcp_notsack_list), 9048 tcp->tcp_suna, tcp->tcp_snxt, 9049 &(tcp->tcp_num_notsack_blk), 9050 &(tcp->tcp_cnt_notsack_list)); 9051 9052 /* 9053 * Make sure tcp_notsack_list is not NULL. 9054 * This happens when kmem_alloc(KM_NOSLEEP) 9055 * returns NULL. 9056 */ 9057 if (tcp->tcp_notsack_list == NULL) { 9058 up += sack_len; 9059 continue; 9060 } 9061 tcp->tcp_fack = tcp->tcp_suna; 9062 } 9063 9064 while (sack_len > 0) { 9065 if (up + 8 > endp) { 9066 up = endp; 9067 break; 9068 } 9069 sack_begin = BE32_TO_U32(up); 9070 up += 4; 9071 sack_end = BE32_TO_U32(up); 9072 up += 4; 9073 sack_len -= 8; 9074 /* 9075 * Bounds checking. Make sure the SACK 9076 * info is within tcp_suna and tcp_snxt. 9077 * If this SACK blk is out of bound, ignore 9078 * it but continue to parse the following 9079 * blks. 9080 */ 9081 if (SEQ_LEQ(sack_end, sack_begin) || 9082 SEQ_LT(sack_begin, tcp->tcp_suna) || 9083 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9084 continue; 9085 } 9086 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9087 sack_begin, sack_end, 9088 &(tcp->tcp_num_notsack_blk), 9089 &(tcp->tcp_cnt_notsack_list)); 9090 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9091 tcp->tcp_fack = sack_end; 9092 } 9093 } 9094 found |= TCP_OPT_SACK_PRESENT; 9095 continue; 9096 9097 case TCPOPT_TSTAMP: 9098 if (len < TCPOPT_TSTAMP_LEN || 9099 up[1] != TCPOPT_TSTAMP_LEN) 9100 break; 9101 9102 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9103 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9104 9105 found |= TCP_OPT_TSTAMP_PRESENT; 9106 9107 up += TCPOPT_TSTAMP_LEN; 9108 continue; 9109 9110 default: 9111 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9112 break; 9113 up += up[1]; 9114 continue; 9115 } 9116 break; 9117 } 9118 return (found); 9119 } 9120 9121 /* 9122 * Set the mss associated with a particular tcp based on its current value, 9123 * and a new one passed in. Observe minimums and maximums, and reset 9124 * other state variables that we want to view as multiples of mss. 9125 * 9126 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9127 * highwater marks etc. need to be initialized or adjusted. 9128 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9129 * packet arrives. 9130 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9131 * ICMP6_PACKET_TOO_BIG arrives. 9132 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9133 * to increase the MSS to use the extra bytes available. 9134 * 9135 * Callers except tcp_paws_check() ensure that they only reduce mss. 9136 */ 9137 static void 9138 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9139 { 9140 uint32_t mss_max; 9141 tcp_stack_t *tcps = tcp->tcp_tcps; 9142 9143 if (tcp->tcp_ipversion == IPV4_VERSION) 9144 mss_max = tcps->tcps_mss_max_ipv4; 9145 else 9146 mss_max = tcps->tcps_mss_max_ipv6; 9147 9148 if (mss < tcps->tcps_mss_min) 9149 mss = tcps->tcps_mss_min; 9150 if (mss > mss_max) 9151 mss = mss_max; 9152 /* 9153 * Unless naglim has been set by our client to 9154 * a non-mss value, force naglim to track mss. 9155 * This can help to aggregate small writes. 9156 */ 9157 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9158 tcp->tcp_naglim = mss; 9159 /* 9160 * TCP should be able to buffer at least 4 MSS data for obvious 9161 * performance reason. 9162 */ 9163 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9164 tcp->tcp_xmit_hiwater = mss << 2; 9165 9166 /* 9167 * Set the xmit_lowater to at least twice of MSS. 9168 */ 9169 if ((mss << 1) > tcp->tcp_xmit_lowater) 9170 tcp->tcp_xmit_lowater = mss << 1; 9171 9172 if (do_ss) { 9173 /* 9174 * Either the tcp_cwnd is as yet uninitialized, or mss is 9175 * changing due to a reduction in MTU, presumably as a 9176 * result of a new path component, reset cwnd to its 9177 * "initial" value, as a multiple of the new mss. 9178 */ 9179 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9180 } else { 9181 /* 9182 * Called by tcp_paws_check(), the mss increased 9183 * marginally to allow use of space previously taken 9184 * by the timestamp option. It would be inappropriate 9185 * to apply slow start or tcp_init_cwnd values to 9186 * tcp_cwnd, simply adjust to a multiple of the new mss. 9187 */ 9188 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9189 tcp->tcp_cwnd_cnt = 0; 9190 } 9191 tcp->tcp_mss = mss; 9192 (void) tcp_maxpsz_set(tcp, B_TRUE); 9193 } 9194 9195 /* For /dev/tcp aka AF_INET open */ 9196 static int 9197 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9198 { 9199 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9200 } 9201 9202 /* For /dev/tcp6 aka AF_INET6 open */ 9203 static int 9204 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9205 { 9206 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9207 } 9208 9209 static conn_t * 9210 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9211 boolean_t issocket, int *errorp) 9212 { 9213 tcp_t *tcp = NULL; 9214 conn_t *connp; 9215 int err; 9216 zoneid_t zoneid; 9217 tcp_stack_t *tcps; 9218 squeue_t *sqp; 9219 9220 ASSERT(errorp != NULL); 9221 /* 9222 * Find the proper zoneid and netstack. 9223 */ 9224 /* 9225 * Special case for install: miniroot needs to be able to 9226 * access files via NFS as though it were always in the 9227 * global zone. 9228 */ 9229 if (credp == kcred && nfs_global_client_only != 0) { 9230 zoneid = GLOBAL_ZONEID; 9231 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9232 netstack_tcp; 9233 ASSERT(tcps != NULL); 9234 } else { 9235 netstack_t *ns; 9236 9237 ns = netstack_find_by_cred(credp); 9238 ASSERT(ns != NULL); 9239 tcps = ns->netstack_tcp; 9240 ASSERT(tcps != NULL); 9241 9242 /* 9243 * For exclusive stacks we set the zoneid to zero 9244 * to make TCP operate as if in the global zone. 9245 */ 9246 if (tcps->tcps_netstack->netstack_stackid != 9247 GLOBAL_NETSTACKID) 9248 zoneid = GLOBAL_ZONEID; 9249 else 9250 zoneid = crgetzoneid(credp); 9251 } 9252 /* 9253 * For stackid zero this is done from strplumb.c, but 9254 * non-zero stackids are handled here. 9255 */ 9256 if (tcps->tcps_g_q == NULL && 9257 tcps->tcps_netstack->netstack_stackid != 9258 GLOBAL_NETSTACKID) { 9259 tcp_g_q_setup(tcps); 9260 } 9261 9262 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9263 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9264 /* 9265 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9266 * so we drop it by one. 9267 */ 9268 netstack_rele(tcps->tcps_netstack); 9269 if (connp == NULL) { 9270 *errorp = ENOSR; 9271 return (NULL); 9272 } 9273 connp->conn_sqp = sqp; 9274 connp->conn_initial_sqp = connp->conn_sqp; 9275 tcp = connp->conn_tcp; 9276 9277 if (isv6) { 9278 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9279 connp->conn_send = ip_output_v6; 9280 connp->conn_af_isv6 = B_TRUE; 9281 connp->conn_pkt_isv6 = B_TRUE; 9282 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9283 tcp->tcp_ipversion = IPV6_VERSION; 9284 tcp->tcp_family = AF_INET6; 9285 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9286 } else { 9287 connp->conn_flags |= IPCL_TCP4; 9288 connp->conn_send = ip_output; 9289 connp->conn_af_isv6 = B_FALSE; 9290 connp->conn_pkt_isv6 = B_FALSE; 9291 tcp->tcp_ipversion = IPV4_VERSION; 9292 tcp->tcp_family = AF_INET; 9293 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9294 } 9295 9296 /* 9297 * TCP keeps a copy of cred for cache locality reasons but 9298 * we put a reference only once. If connp->conn_cred 9299 * becomes invalid, tcp_cred should also be set to NULL. 9300 */ 9301 tcp->tcp_cred = connp->conn_cred = credp; 9302 crhold(connp->conn_cred); 9303 tcp->tcp_cpid = curproc->p_pid; 9304 tcp->tcp_open_time = lbolt64; 9305 connp->conn_zoneid = zoneid; 9306 connp->conn_mlp_type = mlptSingle; 9307 connp->conn_ulp_labeled = !is_system_labeled(); 9308 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9309 ASSERT(tcp->tcp_tcps == tcps); 9310 9311 /* 9312 * If the caller has the process-wide flag set, then default to MAC 9313 * exempt mode. This allows read-down to unlabeled hosts. 9314 */ 9315 if (getpflags(NET_MAC_AWARE, credp) != 0) 9316 connp->conn_mac_exempt = B_TRUE; 9317 9318 connp->conn_dev = NULL; 9319 if (issocket) { 9320 connp->conn_flags |= IPCL_SOCKET; 9321 tcp->tcp_issocket = 1; 9322 } 9323 9324 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9325 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9326 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9327 9328 /* Non-zero default values */ 9329 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9330 9331 if (q == NULL) { 9332 /* 9333 * Create a helper stream for non-STREAMS socket. 9334 */ 9335 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9336 if (err != 0) { 9337 ip1dbg(("tcp_create_common: create of IP helper stream " 9338 "failed\n")); 9339 CONN_DEC_REF(connp); 9340 *errorp = err; 9341 return (NULL); 9342 } 9343 q = connp->conn_rq; 9344 } else { 9345 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9346 } 9347 9348 SOCK_CONNID_INIT(tcp->tcp_connid); 9349 err = tcp_init(tcp, q); 9350 if (err != 0) { 9351 CONN_DEC_REF(connp); 9352 *errorp = err; 9353 return (NULL); 9354 } 9355 9356 return (connp); 9357 } 9358 9359 static int 9360 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9361 boolean_t isv6) 9362 { 9363 tcp_t *tcp = NULL; 9364 conn_t *connp = NULL; 9365 int err; 9366 vmem_t *minor_arena = NULL; 9367 dev_t conn_dev; 9368 boolean_t issocket; 9369 9370 if (q->q_ptr != NULL) 9371 return (0); 9372 9373 if (sflag == MODOPEN) 9374 return (EINVAL); 9375 9376 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9377 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9378 minor_arena = ip_minor_arena_la; 9379 } else { 9380 /* 9381 * Either minor numbers in the large arena were exhausted 9382 * or a non socket application is doing the open. 9383 * Try to allocate from the small arena. 9384 */ 9385 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9386 return (EBUSY); 9387 } 9388 minor_arena = ip_minor_arena_sa; 9389 } 9390 9391 ASSERT(minor_arena != NULL); 9392 9393 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9394 9395 if (flag & SO_FALLBACK) { 9396 /* 9397 * Non streams socket needs a stream to fallback to 9398 */ 9399 RD(q)->q_ptr = (void *)conn_dev; 9400 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9401 WR(q)->q_ptr = (void *)minor_arena; 9402 qprocson(q); 9403 return (0); 9404 } else if (flag & SO_ACCEPTOR) { 9405 q->q_qinfo = &tcp_acceptor_rinit; 9406 /* 9407 * the conn_dev and minor_arena will be subsequently used by 9408 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9409 * the minor device number for this connection from the q_ptr. 9410 */ 9411 RD(q)->q_ptr = (void *)conn_dev; 9412 WR(q)->q_qinfo = &tcp_acceptor_winit; 9413 WR(q)->q_ptr = (void *)minor_arena; 9414 qprocson(q); 9415 return (0); 9416 } 9417 9418 issocket = flag & SO_SOCKSTR; 9419 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9420 9421 if (connp == NULL) { 9422 inet_minor_free(minor_arena, conn_dev); 9423 q->q_ptr = WR(q)->q_ptr = NULL; 9424 return (err); 9425 } 9426 9427 q->q_ptr = WR(q)->q_ptr = connp; 9428 9429 connp->conn_dev = conn_dev; 9430 connp->conn_minor_arena = minor_arena; 9431 9432 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9433 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9434 9435 tcp = connp->conn_tcp; 9436 9437 if (issocket) { 9438 WR(q)->q_qinfo = &tcp_sock_winit; 9439 } else { 9440 #ifdef _ILP32 9441 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9442 #else 9443 tcp->tcp_acceptor_id = conn_dev; 9444 #endif /* _ILP32 */ 9445 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9446 } 9447 9448 /* 9449 * Put the ref for TCP. Ref for IP was already put 9450 * by ipcl_conn_create. Also Make the conn_t globally 9451 * visible to walkers 9452 */ 9453 mutex_enter(&connp->conn_lock); 9454 CONN_INC_REF_LOCKED(connp); 9455 ASSERT(connp->conn_ref == 2); 9456 connp->conn_state_flags &= ~CONN_INCIPIENT; 9457 mutex_exit(&connp->conn_lock); 9458 9459 qprocson(q); 9460 return (0); 9461 } 9462 9463 /* 9464 * Some TCP options can be "set" by requesting them in the option 9465 * buffer. This is needed for XTI feature test though we do not 9466 * allow it in general. We interpret that this mechanism is more 9467 * applicable to OSI protocols and need not be allowed in general. 9468 * This routine filters out options for which it is not allowed (most) 9469 * and lets through those (few) for which it is. [ The XTI interface 9470 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9471 * ever implemented will have to be allowed here ]. 9472 */ 9473 static boolean_t 9474 tcp_allow_connopt_set(int level, int name) 9475 { 9476 9477 switch (level) { 9478 case IPPROTO_TCP: 9479 switch (name) { 9480 case TCP_NODELAY: 9481 return (B_TRUE); 9482 default: 9483 return (B_FALSE); 9484 } 9485 /*NOTREACHED*/ 9486 default: 9487 return (B_FALSE); 9488 } 9489 /*NOTREACHED*/ 9490 } 9491 9492 /* 9493 * this routine gets default values of certain options whose default 9494 * values are maintained by protocol specific code 9495 */ 9496 /* ARGSUSED */ 9497 int 9498 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9499 { 9500 int32_t *i1 = (int32_t *)ptr; 9501 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9502 9503 switch (level) { 9504 case IPPROTO_TCP: 9505 switch (name) { 9506 case TCP_NOTIFY_THRESHOLD: 9507 *i1 = tcps->tcps_ip_notify_interval; 9508 break; 9509 case TCP_ABORT_THRESHOLD: 9510 *i1 = tcps->tcps_ip_abort_interval; 9511 break; 9512 case TCP_CONN_NOTIFY_THRESHOLD: 9513 *i1 = tcps->tcps_ip_notify_cinterval; 9514 break; 9515 case TCP_CONN_ABORT_THRESHOLD: 9516 *i1 = tcps->tcps_ip_abort_cinterval; 9517 break; 9518 default: 9519 return (-1); 9520 } 9521 break; 9522 case IPPROTO_IP: 9523 switch (name) { 9524 case IP_TTL: 9525 *i1 = tcps->tcps_ipv4_ttl; 9526 break; 9527 default: 9528 return (-1); 9529 } 9530 break; 9531 case IPPROTO_IPV6: 9532 switch (name) { 9533 case IPV6_UNICAST_HOPS: 9534 *i1 = tcps->tcps_ipv6_hoplimit; 9535 break; 9536 default: 9537 return (-1); 9538 } 9539 break; 9540 default: 9541 return (-1); 9542 } 9543 return (sizeof (int)); 9544 } 9545 9546 static int 9547 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9548 { 9549 int *i1 = (int *)ptr; 9550 tcp_t *tcp = connp->conn_tcp; 9551 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9552 9553 switch (level) { 9554 case SOL_SOCKET: 9555 switch (name) { 9556 case SO_LINGER: { 9557 struct linger *lgr = (struct linger *)ptr; 9558 9559 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9560 lgr->l_linger = tcp->tcp_lingertime; 9561 } 9562 return (sizeof (struct linger)); 9563 case SO_DEBUG: 9564 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9565 break; 9566 case SO_KEEPALIVE: 9567 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9568 break; 9569 case SO_DONTROUTE: 9570 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9571 break; 9572 case SO_USELOOPBACK: 9573 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9574 break; 9575 case SO_BROADCAST: 9576 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9577 break; 9578 case SO_REUSEADDR: 9579 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9580 break; 9581 case SO_OOBINLINE: 9582 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9583 break; 9584 case SO_DGRAM_ERRIND: 9585 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9586 break; 9587 case SO_TYPE: 9588 *i1 = SOCK_STREAM; 9589 break; 9590 case SO_SNDBUF: 9591 *i1 = tcp->tcp_xmit_hiwater; 9592 break; 9593 case SO_RCVBUF: 9594 *i1 = tcp->tcp_recv_hiwater; 9595 break; 9596 case SO_SND_COPYAVOID: 9597 *i1 = tcp->tcp_snd_zcopy_on ? 9598 SO_SND_COPYAVOID : 0; 9599 break; 9600 case SO_ALLZONES: 9601 *i1 = connp->conn_allzones ? 1 : 0; 9602 break; 9603 case SO_ANON_MLP: 9604 *i1 = connp->conn_anon_mlp; 9605 break; 9606 case SO_MAC_EXEMPT: 9607 *i1 = connp->conn_mac_exempt; 9608 break; 9609 case SO_EXCLBIND: 9610 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9611 break; 9612 case SO_PROTOTYPE: 9613 *i1 = IPPROTO_TCP; 9614 break; 9615 case SO_DOMAIN: 9616 *i1 = tcp->tcp_family; 9617 break; 9618 case SO_ACCEPTCONN: 9619 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9620 default: 9621 return (-1); 9622 } 9623 break; 9624 case IPPROTO_TCP: 9625 switch (name) { 9626 case TCP_NODELAY: 9627 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9628 break; 9629 case TCP_MAXSEG: 9630 *i1 = tcp->tcp_mss; 9631 break; 9632 case TCP_NOTIFY_THRESHOLD: 9633 *i1 = (int)tcp->tcp_first_timer_threshold; 9634 break; 9635 case TCP_ABORT_THRESHOLD: 9636 *i1 = tcp->tcp_second_timer_threshold; 9637 break; 9638 case TCP_CONN_NOTIFY_THRESHOLD: 9639 *i1 = tcp->tcp_first_ctimer_threshold; 9640 break; 9641 case TCP_CONN_ABORT_THRESHOLD: 9642 *i1 = tcp->tcp_second_ctimer_threshold; 9643 break; 9644 case TCP_RECVDSTADDR: 9645 *i1 = tcp->tcp_recvdstaddr; 9646 break; 9647 case TCP_ANONPRIVBIND: 9648 *i1 = tcp->tcp_anon_priv_bind; 9649 break; 9650 case TCP_EXCLBIND: 9651 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9652 break; 9653 case TCP_INIT_CWND: 9654 *i1 = tcp->tcp_init_cwnd; 9655 break; 9656 case TCP_KEEPALIVE_THRESHOLD: 9657 *i1 = tcp->tcp_ka_interval; 9658 break; 9659 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9660 *i1 = tcp->tcp_ka_abort_thres; 9661 break; 9662 case TCP_CORK: 9663 *i1 = tcp->tcp_cork; 9664 break; 9665 default: 9666 return (-1); 9667 } 9668 break; 9669 case IPPROTO_IP: 9670 if (tcp->tcp_family != AF_INET) 9671 return (-1); 9672 switch (name) { 9673 case IP_OPTIONS: 9674 case T_IP_OPTIONS: { 9675 /* 9676 * This is compatible with BSD in that in only return 9677 * the reverse source route with the final destination 9678 * as the last entry. The first 4 bytes of the option 9679 * will contain the final destination. 9680 */ 9681 int opt_len; 9682 9683 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9684 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9685 ASSERT(opt_len >= 0); 9686 /* Caller ensures enough space */ 9687 if (opt_len > 0) { 9688 /* 9689 * TODO: Do we have to handle getsockopt on an 9690 * initiator as well? 9691 */ 9692 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9693 } 9694 return (0); 9695 } 9696 case IP_TOS: 9697 case T_IP_TOS: 9698 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9699 break; 9700 case IP_TTL: 9701 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9702 break; 9703 case IP_NEXTHOP: 9704 /* Handled at IP level */ 9705 return (-EINVAL); 9706 default: 9707 return (-1); 9708 } 9709 break; 9710 case IPPROTO_IPV6: 9711 /* 9712 * IPPROTO_IPV6 options are only supported for sockets 9713 * that are using IPv6 on the wire. 9714 */ 9715 if (tcp->tcp_ipversion != IPV6_VERSION) { 9716 return (-1); 9717 } 9718 switch (name) { 9719 case IPV6_UNICAST_HOPS: 9720 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9721 break; /* goto sizeof (int) option return */ 9722 case IPV6_BOUND_IF: 9723 /* Zero if not set */ 9724 *i1 = tcp->tcp_bound_if; 9725 break; /* goto sizeof (int) option return */ 9726 case IPV6_RECVPKTINFO: 9727 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9728 *i1 = 1; 9729 else 9730 *i1 = 0; 9731 break; /* goto sizeof (int) option return */ 9732 case IPV6_RECVTCLASS: 9733 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9734 *i1 = 1; 9735 else 9736 *i1 = 0; 9737 break; /* goto sizeof (int) option return */ 9738 case IPV6_RECVHOPLIMIT: 9739 if (tcp->tcp_ipv6_recvancillary & 9740 TCP_IPV6_RECVHOPLIMIT) 9741 *i1 = 1; 9742 else 9743 *i1 = 0; 9744 break; /* goto sizeof (int) option return */ 9745 case IPV6_RECVHOPOPTS: 9746 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9747 *i1 = 1; 9748 else 9749 *i1 = 0; 9750 break; /* goto sizeof (int) option return */ 9751 case IPV6_RECVDSTOPTS: 9752 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9753 *i1 = 1; 9754 else 9755 *i1 = 0; 9756 break; /* goto sizeof (int) option return */ 9757 case _OLD_IPV6_RECVDSTOPTS: 9758 if (tcp->tcp_ipv6_recvancillary & 9759 TCP_OLD_IPV6_RECVDSTOPTS) 9760 *i1 = 1; 9761 else 9762 *i1 = 0; 9763 break; /* goto sizeof (int) option return */ 9764 case IPV6_RECVRTHDR: 9765 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9766 *i1 = 1; 9767 else 9768 *i1 = 0; 9769 break; /* goto sizeof (int) option return */ 9770 case IPV6_RECVRTHDRDSTOPTS: 9771 if (tcp->tcp_ipv6_recvancillary & 9772 TCP_IPV6_RECVRTDSTOPTS) 9773 *i1 = 1; 9774 else 9775 *i1 = 0; 9776 break; /* goto sizeof (int) option return */ 9777 case IPV6_PKTINFO: { 9778 /* XXX assumes that caller has room for max size! */ 9779 struct in6_pktinfo *pkti; 9780 9781 pkti = (struct in6_pktinfo *)ptr; 9782 if (ipp->ipp_fields & IPPF_IFINDEX) 9783 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9784 else 9785 pkti->ipi6_ifindex = 0; 9786 if (ipp->ipp_fields & IPPF_ADDR) 9787 pkti->ipi6_addr = ipp->ipp_addr; 9788 else 9789 pkti->ipi6_addr = ipv6_all_zeros; 9790 return (sizeof (struct in6_pktinfo)); 9791 } 9792 case IPV6_TCLASS: 9793 if (ipp->ipp_fields & IPPF_TCLASS) 9794 *i1 = ipp->ipp_tclass; 9795 else 9796 *i1 = IPV6_FLOW_TCLASS( 9797 IPV6_DEFAULT_VERS_AND_FLOW); 9798 break; /* goto sizeof (int) option return */ 9799 case IPV6_NEXTHOP: { 9800 sin6_t *sin6 = (sin6_t *)ptr; 9801 9802 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9803 return (0); 9804 *sin6 = sin6_null; 9805 sin6->sin6_family = AF_INET6; 9806 sin6->sin6_addr = ipp->ipp_nexthop; 9807 return (sizeof (sin6_t)); 9808 } 9809 case IPV6_HOPOPTS: 9810 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9811 return (0); 9812 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9813 return (0); 9814 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9815 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9816 if (tcp->tcp_label_len > 0) { 9817 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9818 ptr[1] = (ipp->ipp_hopoptslen - 9819 tcp->tcp_label_len + 7) / 8 - 1; 9820 } 9821 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9822 case IPV6_RTHDRDSTOPTS: 9823 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9824 return (0); 9825 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9826 return (ipp->ipp_rtdstoptslen); 9827 case IPV6_RTHDR: 9828 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9829 return (0); 9830 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9831 return (ipp->ipp_rthdrlen); 9832 case IPV6_DSTOPTS: 9833 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9834 return (0); 9835 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9836 return (ipp->ipp_dstoptslen); 9837 case IPV6_SRC_PREFERENCES: 9838 return (ip6_get_src_preferences(connp, 9839 (uint32_t *)ptr)); 9840 case IPV6_PATHMTU: { 9841 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9842 9843 if (tcp->tcp_state < TCPS_ESTABLISHED) 9844 return (-1); 9845 9846 return (ip_fill_mtuinfo(&connp->conn_remv6, 9847 connp->conn_fport, mtuinfo, 9848 connp->conn_netstack)); 9849 } 9850 default: 9851 return (-1); 9852 } 9853 break; 9854 default: 9855 return (-1); 9856 } 9857 return (sizeof (int)); 9858 } 9859 9860 /* 9861 * TCP routine to get the values of options. 9862 */ 9863 int 9864 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9865 { 9866 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9867 } 9868 9869 /* returns UNIX error, the optlen is a value-result arg */ 9870 int 9871 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9872 void *optvalp, socklen_t *optlen, cred_t *cr) 9873 { 9874 conn_t *connp = (conn_t *)proto_handle; 9875 squeue_t *sqp = connp->conn_sqp; 9876 int error; 9877 t_uscalar_t max_optbuf_len; 9878 void *optvalp_buf; 9879 int len; 9880 9881 ASSERT(connp->conn_upper_handle != NULL); 9882 9883 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9884 tcp_opt_obj.odb_opt_des_arr, 9885 tcp_opt_obj.odb_opt_arr_cnt, 9886 tcp_opt_obj.odb_topmost_tpiprovider, 9887 B_FALSE, B_TRUE, cr); 9888 if (error != 0) { 9889 if (error < 0) { 9890 error = proto_tlitosyserr(-error); 9891 } 9892 return (error); 9893 } 9894 9895 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9896 9897 error = squeue_synch_enter(sqp, connp, NULL); 9898 if (error == ENOMEM) { 9899 return (ENOMEM); 9900 } 9901 9902 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9903 squeue_synch_exit(sqp, connp); 9904 9905 if (len < 0) { 9906 /* 9907 * Pass on to IP 9908 */ 9909 kmem_free(optvalp_buf, max_optbuf_len); 9910 return (ip_get_options(connp, level, option_name, 9911 optvalp, optlen, cr)); 9912 } else { 9913 /* 9914 * update optlen and copy option value 9915 */ 9916 t_uscalar_t size = MIN(len, *optlen); 9917 bcopy(optvalp_buf, optvalp, size); 9918 bcopy(&size, optlen, sizeof (size)); 9919 9920 kmem_free(optvalp_buf, max_optbuf_len); 9921 return (0); 9922 } 9923 } 9924 9925 /* 9926 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9927 * Parameters are assumed to be verified by the caller. 9928 */ 9929 /* ARGSUSED */ 9930 int 9931 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9932 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9933 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9934 { 9935 tcp_t *tcp = connp->conn_tcp; 9936 int *i1 = (int *)invalp; 9937 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9938 boolean_t checkonly; 9939 int reterr; 9940 tcp_stack_t *tcps = tcp->tcp_tcps; 9941 9942 switch (optset_context) { 9943 case SETFN_OPTCOM_CHECKONLY: 9944 checkonly = B_TRUE; 9945 /* 9946 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9947 * inlen != 0 implies value supplied and 9948 * we have to "pretend" to set it. 9949 * inlen == 0 implies that there is no 9950 * value part in T_CHECK request and just validation 9951 * done elsewhere should be enough, we just return here. 9952 */ 9953 if (inlen == 0) { 9954 *outlenp = 0; 9955 return (0); 9956 } 9957 break; 9958 case SETFN_OPTCOM_NEGOTIATE: 9959 checkonly = B_FALSE; 9960 break; 9961 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9962 case SETFN_CONN_NEGOTIATE: 9963 checkonly = B_FALSE; 9964 /* 9965 * Negotiating local and "association-related" options 9966 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9967 * primitives is allowed by XTI, but we choose 9968 * to not implement this style negotiation for Internet 9969 * protocols (We interpret it is a must for OSI world but 9970 * optional for Internet protocols) for all options. 9971 * [ Will do only for the few options that enable test 9972 * suites that our XTI implementation of this feature 9973 * works for transports that do allow it ] 9974 */ 9975 if (!tcp_allow_connopt_set(level, name)) { 9976 *outlenp = 0; 9977 return (EINVAL); 9978 } 9979 break; 9980 default: 9981 /* 9982 * We should never get here 9983 */ 9984 *outlenp = 0; 9985 return (EINVAL); 9986 } 9987 9988 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9989 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9990 9991 /* 9992 * For TCP, we should have no ancillary data sent down 9993 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9994 * has to be zero. 9995 */ 9996 ASSERT(thisdg_attrs == NULL); 9997 9998 /* 9999 * For fixed length options, no sanity check 10000 * of passed in length is done. It is assumed *_optcom_req() 10001 * routines do the right thing. 10002 */ 10003 switch (level) { 10004 case SOL_SOCKET: 10005 switch (name) { 10006 case SO_LINGER: { 10007 struct linger *lgr = (struct linger *)invalp; 10008 10009 if (!checkonly) { 10010 if (lgr->l_onoff) { 10011 tcp->tcp_linger = 1; 10012 tcp->tcp_lingertime = lgr->l_linger; 10013 } else { 10014 tcp->tcp_linger = 0; 10015 tcp->tcp_lingertime = 0; 10016 } 10017 /* struct copy */ 10018 *(struct linger *)outvalp = *lgr; 10019 } else { 10020 if (!lgr->l_onoff) { 10021 ((struct linger *) 10022 outvalp)->l_onoff = 0; 10023 ((struct linger *) 10024 outvalp)->l_linger = 0; 10025 } else { 10026 /* struct copy */ 10027 *(struct linger *)outvalp = *lgr; 10028 } 10029 } 10030 *outlenp = sizeof (struct linger); 10031 return (0); 10032 } 10033 case SO_DEBUG: 10034 if (!checkonly) 10035 tcp->tcp_debug = onoff; 10036 break; 10037 case SO_KEEPALIVE: 10038 if (checkonly) { 10039 /* check only case */ 10040 break; 10041 } 10042 10043 if (!onoff) { 10044 if (tcp->tcp_ka_enabled) { 10045 if (tcp->tcp_ka_tid != 0) { 10046 (void) TCP_TIMER_CANCEL(tcp, 10047 tcp->tcp_ka_tid); 10048 tcp->tcp_ka_tid = 0; 10049 } 10050 tcp->tcp_ka_enabled = 0; 10051 } 10052 break; 10053 } 10054 if (!tcp->tcp_ka_enabled) { 10055 /* Crank up the keepalive timer */ 10056 tcp->tcp_ka_last_intrvl = 0; 10057 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10058 tcp_keepalive_killer, 10059 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10060 tcp->tcp_ka_enabled = 1; 10061 } 10062 break; 10063 case SO_DONTROUTE: 10064 /* 10065 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 10066 * only of interest to IP. We track them here only so 10067 * that we can report their current value. 10068 */ 10069 if (!checkonly) { 10070 tcp->tcp_dontroute = onoff; 10071 tcp->tcp_connp->conn_dontroute = onoff; 10072 } 10073 break; 10074 case SO_USELOOPBACK: 10075 if (!checkonly) { 10076 tcp->tcp_useloopback = onoff; 10077 tcp->tcp_connp->conn_loopback = onoff; 10078 } 10079 break; 10080 case SO_BROADCAST: 10081 if (!checkonly) { 10082 tcp->tcp_broadcast = onoff; 10083 tcp->tcp_connp->conn_broadcast = onoff; 10084 } 10085 break; 10086 case SO_REUSEADDR: 10087 if (!checkonly) { 10088 tcp->tcp_reuseaddr = onoff; 10089 tcp->tcp_connp->conn_reuseaddr = onoff; 10090 } 10091 break; 10092 case SO_OOBINLINE: 10093 if (!checkonly) { 10094 tcp->tcp_oobinline = onoff; 10095 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 10096 proto_set_rx_oob_opt(connp, onoff); 10097 } 10098 break; 10099 case SO_DGRAM_ERRIND: 10100 if (!checkonly) 10101 tcp->tcp_dgram_errind = onoff; 10102 break; 10103 case SO_SNDBUF: { 10104 if (*i1 > tcps->tcps_max_buf) { 10105 *outlenp = 0; 10106 return (ENOBUFS); 10107 } 10108 if (checkonly) 10109 break; 10110 10111 tcp->tcp_xmit_hiwater = *i1; 10112 if (tcps->tcps_snd_lowat_fraction != 0) 10113 tcp->tcp_xmit_lowater = 10114 tcp->tcp_xmit_hiwater / 10115 tcps->tcps_snd_lowat_fraction; 10116 (void) tcp_maxpsz_set(tcp, B_TRUE); 10117 /* 10118 * If we are flow-controlled, recheck the condition. 10119 * There are apps that increase SO_SNDBUF size when 10120 * flow-controlled (EWOULDBLOCK), and expect the flow 10121 * control condition to be lifted right away. 10122 */ 10123 mutex_enter(&tcp->tcp_non_sq_lock); 10124 if (tcp->tcp_flow_stopped && 10125 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10126 tcp_clrqfull(tcp); 10127 } 10128 mutex_exit(&tcp->tcp_non_sq_lock); 10129 break; 10130 } 10131 case SO_RCVBUF: 10132 if (*i1 > tcps->tcps_max_buf) { 10133 *outlenp = 0; 10134 return (ENOBUFS); 10135 } 10136 /* Silently ignore zero */ 10137 if (!checkonly && *i1 != 0) { 10138 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10139 (void) tcp_rwnd_set(tcp, *i1); 10140 } 10141 /* 10142 * XXX should we return the rwnd here 10143 * and tcp_opt_get ? 10144 */ 10145 break; 10146 case SO_SND_COPYAVOID: 10147 if (!checkonly) { 10148 /* we only allow enable at most once for now */ 10149 if (tcp->tcp_loopback || 10150 (tcp->tcp_kssl_ctx != NULL) || 10151 (!tcp->tcp_snd_zcopy_aware && 10152 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10153 *outlenp = 0; 10154 return (EOPNOTSUPP); 10155 } 10156 tcp->tcp_snd_zcopy_aware = 1; 10157 } 10158 break; 10159 case SO_RCVTIMEO: 10160 case SO_SNDTIMEO: 10161 /* 10162 * Pass these two options in order for third part 10163 * protocol usage. Here just return directly. 10164 */ 10165 return (0); 10166 case SO_ALLZONES: 10167 /* Pass option along to IP level for handling */ 10168 return (-EINVAL); 10169 case SO_ANON_MLP: 10170 /* Pass option along to IP level for handling */ 10171 return (-EINVAL); 10172 case SO_MAC_EXEMPT: 10173 /* Pass option along to IP level for handling */ 10174 return (-EINVAL); 10175 case SO_EXCLBIND: 10176 if (!checkonly) 10177 tcp->tcp_exclbind = onoff; 10178 break; 10179 default: 10180 *outlenp = 0; 10181 return (EINVAL); 10182 } 10183 break; 10184 case IPPROTO_TCP: 10185 switch (name) { 10186 case TCP_NODELAY: 10187 if (!checkonly) 10188 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10189 break; 10190 case TCP_NOTIFY_THRESHOLD: 10191 if (!checkonly) 10192 tcp->tcp_first_timer_threshold = *i1; 10193 break; 10194 case TCP_ABORT_THRESHOLD: 10195 if (!checkonly) 10196 tcp->tcp_second_timer_threshold = *i1; 10197 break; 10198 case TCP_CONN_NOTIFY_THRESHOLD: 10199 if (!checkonly) 10200 tcp->tcp_first_ctimer_threshold = *i1; 10201 break; 10202 case TCP_CONN_ABORT_THRESHOLD: 10203 if (!checkonly) 10204 tcp->tcp_second_ctimer_threshold = *i1; 10205 break; 10206 case TCP_RECVDSTADDR: 10207 if (tcp->tcp_state > TCPS_LISTEN) 10208 return (EOPNOTSUPP); 10209 if (!checkonly) 10210 tcp->tcp_recvdstaddr = onoff; 10211 break; 10212 case TCP_ANONPRIVBIND: 10213 if ((reterr = secpolicy_net_privaddr(cr, 0, 10214 IPPROTO_TCP)) != 0) { 10215 *outlenp = 0; 10216 return (reterr); 10217 } 10218 if (!checkonly) { 10219 tcp->tcp_anon_priv_bind = onoff; 10220 } 10221 break; 10222 case TCP_EXCLBIND: 10223 if (!checkonly) 10224 tcp->tcp_exclbind = onoff; 10225 break; /* goto sizeof (int) option return */ 10226 case TCP_INIT_CWND: { 10227 uint32_t init_cwnd = *((uint32_t *)invalp); 10228 10229 if (checkonly) 10230 break; 10231 10232 /* 10233 * Only allow socket with network configuration 10234 * privilege to set the initial cwnd to be larger 10235 * than allowed by RFC 3390. 10236 */ 10237 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10238 tcp->tcp_init_cwnd = init_cwnd; 10239 break; 10240 } 10241 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10242 *outlenp = 0; 10243 return (reterr); 10244 } 10245 if (init_cwnd > TCP_MAX_INIT_CWND) { 10246 *outlenp = 0; 10247 return (EINVAL); 10248 } 10249 tcp->tcp_init_cwnd = init_cwnd; 10250 break; 10251 } 10252 case TCP_KEEPALIVE_THRESHOLD: 10253 if (checkonly) 10254 break; 10255 10256 if (*i1 < tcps->tcps_keepalive_interval_low || 10257 *i1 > tcps->tcps_keepalive_interval_high) { 10258 *outlenp = 0; 10259 return (EINVAL); 10260 } 10261 if (*i1 != tcp->tcp_ka_interval) { 10262 tcp->tcp_ka_interval = *i1; 10263 /* 10264 * Check if we need to restart the 10265 * keepalive timer. 10266 */ 10267 if (tcp->tcp_ka_tid != 0) { 10268 ASSERT(tcp->tcp_ka_enabled); 10269 (void) TCP_TIMER_CANCEL(tcp, 10270 tcp->tcp_ka_tid); 10271 tcp->tcp_ka_last_intrvl = 0; 10272 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10273 tcp_keepalive_killer, 10274 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10275 } 10276 } 10277 break; 10278 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10279 if (!checkonly) { 10280 if (*i1 < 10281 tcps->tcps_keepalive_abort_interval_low || 10282 *i1 > 10283 tcps->tcps_keepalive_abort_interval_high) { 10284 *outlenp = 0; 10285 return (EINVAL); 10286 } 10287 tcp->tcp_ka_abort_thres = *i1; 10288 } 10289 break; 10290 case TCP_CORK: 10291 if (!checkonly) { 10292 /* 10293 * if tcp->tcp_cork was set and is now 10294 * being unset, we have to make sure that 10295 * the remaining data gets sent out. Also 10296 * unset tcp->tcp_cork so that tcp_wput_data() 10297 * can send data even if it is less than mss 10298 */ 10299 if (tcp->tcp_cork && onoff == 0 && 10300 tcp->tcp_unsent > 0) { 10301 tcp->tcp_cork = B_FALSE; 10302 tcp_wput_data(tcp, NULL, B_FALSE); 10303 } 10304 tcp->tcp_cork = onoff; 10305 } 10306 break; 10307 default: 10308 *outlenp = 0; 10309 return (EINVAL); 10310 } 10311 break; 10312 case IPPROTO_IP: 10313 if (tcp->tcp_family != AF_INET) { 10314 *outlenp = 0; 10315 return (ENOPROTOOPT); 10316 } 10317 switch (name) { 10318 case IP_OPTIONS: 10319 case T_IP_OPTIONS: 10320 reterr = tcp_opt_set_header(tcp, checkonly, 10321 invalp, inlen); 10322 if (reterr) { 10323 *outlenp = 0; 10324 return (reterr); 10325 } 10326 /* OK return - copy input buffer into output buffer */ 10327 if (invalp != outvalp) { 10328 /* don't trust bcopy for identical src/dst */ 10329 bcopy(invalp, outvalp, inlen); 10330 } 10331 *outlenp = inlen; 10332 return (0); 10333 case IP_TOS: 10334 case T_IP_TOS: 10335 if (!checkonly) { 10336 tcp->tcp_ipha->ipha_type_of_service = 10337 (uchar_t)*i1; 10338 tcp->tcp_tos = (uchar_t)*i1; 10339 } 10340 break; 10341 case IP_TTL: 10342 if (!checkonly) { 10343 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10344 tcp->tcp_ttl = (uchar_t)*i1; 10345 } 10346 break; 10347 case IP_BOUND_IF: 10348 case IP_NEXTHOP: 10349 /* Handled at the IP level */ 10350 return (-EINVAL); 10351 case IP_SEC_OPT: 10352 /* 10353 * We should not allow policy setting after 10354 * we start listening for connections. 10355 */ 10356 if (tcp->tcp_state == TCPS_LISTEN) { 10357 return (EINVAL); 10358 } else { 10359 /* Handled at the IP level */ 10360 return (-EINVAL); 10361 } 10362 default: 10363 *outlenp = 0; 10364 return (EINVAL); 10365 } 10366 break; 10367 case IPPROTO_IPV6: { 10368 ip6_pkt_t *ipp; 10369 10370 /* 10371 * IPPROTO_IPV6 options are only supported for sockets 10372 * that are using IPv6 on the wire. 10373 */ 10374 if (tcp->tcp_ipversion != IPV6_VERSION) { 10375 *outlenp = 0; 10376 return (ENOPROTOOPT); 10377 } 10378 /* 10379 * Only sticky options; no ancillary data 10380 */ 10381 ipp = &tcp->tcp_sticky_ipp; 10382 10383 switch (name) { 10384 case IPV6_UNICAST_HOPS: 10385 /* -1 means use default */ 10386 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10387 *outlenp = 0; 10388 return (EINVAL); 10389 } 10390 if (!checkonly) { 10391 if (*i1 == -1) { 10392 tcp->tcp_ip6h->ip6_hops = 10393 ipp->ipp_unicast_hops = 10394 (uint8_t)tcps->tcps_ipv6_hoplimit; 10395 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10396 /* Pass modified value to IP. */ 10397 *i1 = tcp->tcp_ip6h->ip6_hops; 10398 } else { 10399 tcp->tcp_ip6h->ip6_hops = 10400 ipp->ipp_unicast_hops = 10401 (uint8_t)*i1; 10402 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10403 } 10404 reterr = tcp_build_hdrs(tcp); 10405 if (reterr != 0) 10406 return (reterr); 10407 } 10408 break; 10409 case IPV6_BOUND_IF: 10410 if (!checkonly) { 10411 tcp->tcp_bound_if = *i1; 10412 PASS_OPT_TO_IP(connp); 10413 } 10414 break; 10415 /* 10416 * Set boolean switches for ancillary data delivery 10417 */ 10418 case IPV6_RECVPKTINFO: 10419 if (!checkonly) { 10420 if (onoff) 10421 tcp->tcp_ipv6_recvancillary |= 10422 TCP_IPV6_RECVPKTINFO; 10423 else 10424 tcp->tcp_ipv6_recvancillary &= 10425 ~TCP_IPV6_RECVPKTINFO; 10426 /* Force it to be sent up with the next msg */ 10427 tcp->tcp_recvifindex = 0; 10428 PASS_OPT_TO_IP(connp); 10429 } 10430 break; 10431 case IPV6_RECVTCLASS: 10432 if (!checkonly) { 10433 if (onoff) 10434 tcp->tcp_ipv6_recvancillary |= 10435 TCP_IPV6_RECVTCLASS; 10436 else 10437 tcp->tcp_ipv6_recvancillary &= 10438 ~TCP_IPV6_RECVTCLASS; 10439 PASS_OPT_TO_IP(connp); 10440 } 10441 break; 10442 case IPV6_RECVHOPLIMIT: 10443 if (!checkonly) { 10444 if (onoff) 10445 tcp->tcp_ipv6_recvancillary |= 10446 TCP_IPV6_RECVHOPLIMIT; 10447 else 10448 tcp->tcp_ipv6_recvancillary &= 10449 ~TCP_IPV6_RECVHOPLIMIT; 10450 /* Force it to be sent up with the next msg */ 10451 tcp->tcp_recvhops = 0xffffffffU; 10452 PASS_OPT_TO_IP(connp); 10453 } 10454 break; 10455 case IPV6_RECVHOPOPTS: 10456 if (!checkonly) { 10457 if (onoff) 10458 tcp->tcp_ipv6_recvancillary |= 10459 TCP_IPV6_RECVHOPOPTS; 10460 else 10461 tcp->tcp_ipv6_recvancillary &= 10462 ~TCP_IPV6_RECVHOPOPTS; 10463 PASS_OPT_TO_IP(connp); 10464 } 10465 break; 10466 case IPV6_RECVDSTOPTS: 10467 if (!checkonly) { 10468 if (onoff) 10469 tcp->tcp_ipv6_recvancillary |= 10470 TCP_IPV6_RECVDSTOPTS; 10471 else 10472 tcp->tcp_ipv6_recvancillary &= 10473 ~TCP_IPV6_RECVDSTOPTS; 10474 PASS_OPT_TO_IP(connp); 10475 } 10476 break; 10477 case _OLD_IPV6_RECVDSTOPTS: 10478 if (!checkonly) { 10479 if (onoff) 10480 tcp->tcp_ipv6_recvancillary |= 10481 TCP_OLD_IPV6_RECVDSTOPTS; 10482 else 10483 tcp->tcp_ipv6_recvancillary &= 10484 ~TCP_OLD_IPV6_RECVDSTOPTS; 10485 } 10486 break; 10487 case IPV6_RECVRTHDR: 10488 if (!checkonly) { 10489 if (onoff) 10490 tcp->tcp_ipv6_recvancillary |= 10491 TCP_IPV6_RECVRTHDR; 10492 else 10493 tcp->tcp_ipv6_recvancillary &= 10494 ~TCP_IPV6_RECVRTHDR; 10495 PASS_OPT_TO_IP(connp); 10496 } 10497 break; 10498 case IPV6_RECVRTHDRDSTOPTS: 10499 if (!checkonly) { 10500 if (onoff) 10501 tcp->tcp_ipv6_recvancillary |= 10502 TCP_IPV6_RECVRTDSTOPTS; 10503 else 10504 tcp->tcp_ipv6_recvancillary &= 10505 ~TCP_IPV6_RECVRTDSTOPTS; 10506 PASS_OPT_TO_IP(connp); 10507 } 10508 break; 10509 case IPV6_PKTINFO: 10510 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10511 return (EINVAL); 10512 if (checkonly) 10513 break; 10514 10515 if (inlen == 0) { 10516 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10517 } else { 10518 struct in6_pktinfo *pkti; 10519 10520 pkti = (struct in6_pktinfo *)invalp; 10521 /* 10522 * RFC 3542 states that ipi6_addr must be 10523 * the unspecified address when setting the 10524 * IPV6_PKTINFO sticky socket option on a 10525 * TCP socket. 10526 */ 10527 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10528 return (EINVAL); 10529 /* 10530 * IP will validate the source address and 10531 * interface index. 10532 */ 10533 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10534 reterr = ip_set_options(tcp->tcp_connp, 10535 level, name, invalp, inlen, cr); 10536 } else { 10537 reterr = ip6_set_pktinfo(cr, 10538 tcp->tcp_connp, pkti); 10539 } 10540 if (reterr != 0) 10541 return (reterr); 10542 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10543 ipp->ipp_addr = pkti->ipi6_addr; 10544 if (ipp->ipp_ifindex != 0) 10545 ipp->ipp_fields |= IPPF_IFINDEX; 10546 else 10547 ipp->ipp_fields &= ~IPPF_IFINDEX; 10548 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10549 ipp->ipp_fields |= IPPF_ADDR; 10550 else 10551 ipp->ipp_fields &= ~IPPF_ADDR; 10552 } 10553 reterr = tcp_build_hdrs(tcp); 10554 if (reterr != 0) 10555 return (reterr); 10556 break; 10557 case IPV6_TCLASS: 10558 if (inlen != 0 && inlen != sizeof (int)) 10559 return (EINVAL); 10560 if (checkonly) 10561 break; 10562 10563 if (inlen == 0) { 10564 ipp->ipp_fields &= ~IPPF_TCLASS; 10565 } else { 10566 if (*i1 > 255 || *i1 < -1) 10567 return (EINVAL); 10568 if (*i1 == -1) { 10569 ipp->ipp_tclass = 0; 10570 *i1 = 0; 10571 } else { 10572 ipp->ipp_tclass = *i1; 10573 } 10574 ipp->ipp_fields |= IPPF_TCLASS; 10575 } 10576 reterr = tcp_build_hdrs(tcp); 10577 if (reterr != 0) 10578 return (reterr); 10579 break; 10580 case IPV6_NEXTHOP: 10581 /* 10582 * IP will verify that the nexthop is reachable 10583 * and fail for sticky options. 10584 */ 10585 if (inlen != 0 && inlen != sizeof (sin6_t)) 10586 return (EINVAL); 10587 if (checkonly) 10588 break; 10589 10590 if (inlen == 0) { 10591 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10592 } else { 10593 sin6_t *sin6 = (sin6_t *)invalp; 10594 10595 if (sin6->sin6_family != AF_INET6) 10596 return (EAFNOSUPPORT); 10597 if (IN6_IS_ADDR_V4MAPPED( 10598 &sin6->sin6_addr)) 10599 return (EADDRNOTAVAIL); 10600 ipp->ipp_nexthop = sin6->sin6_addr; 10601 if (!IN6_IS_ADDR_UNSPECIFIED( 10602 &ipp->ipp_nexthop)) 10603 ipp->ipp_fields |= IPPF_NEXTHOP; 10604 else 10605 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10606 } 10607 reterr = tcp_build_hdrs(tcp); 10608 if (reterr != 0) 10609 return (reterr); 10610 PASS_OPT_TO_IP(connp); 10611 break; 10612 case IPV6_HOPOPTS: { 10613 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10614 10615 /* 10616 * Sanity checks - minimum size, size a multiple of 10617 * eight bytes, and matching size passed in. 10618 */ 10619 if (inlen != 0 && 10620 inlen != (8 * (hopts->ip6h_len + 1))) 10621 return (EINVAL); 10622 10623 if (checkonly) 10624 break; 10625 10626 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10627 (uchar_t **)&ipp->ipp_hopopts, 10628 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10629 if (reterr != 0) 10630 return (reterr); 10631 if (ipp->ipp_hopoptslen == 0) 10632 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10633 else 10634 ipp->ipp_fields |= IPPF_HOPOPTS; 10635 reterr = tcp_build_hdrs(tcp); 10636 if (reterr != 0) 10637 return (reterr); 10638 break; 10639 } 10640 case IPV6_RTHDRDSTOPTS: { 10641 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10642 10643 /* 10644 * Sanity checks - minimum size, size a multiple of 10645 * eight bytes, and matching size passed in. 10646 */ 10647 if (inlen != 0 && 10648 inlen != (8 * (dopts->ip6d_len + 1))) 10649 return (EINVAL); 10650 10651 if (checkonly) 10652 break; 10653 10654 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10655 (uchar_t **)&ipp->ipp_rtdstopts, 10656 &ipp->ipp_rtdstoptslen, 0); 10657 if (reterr != 0) 10658 return (reterr); 10659 if (ipp->ipp_rtdstoptslen == 0) 10660 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10661 else 10662 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10663 reterr = tcp_build_hdrs(tcp); 10664 if (reterr != 0) 10665 return (reterr); 10666 break; 10667 } 10668 case IPV6_DSTOPTS: { 10669 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10670 10671 /* 10672 * Sanity checks - minimum size, size a multiple of 10673 * eight bytes, and matching size passed in. 10674 */ 10675 if (inlen != 0 && 10676 inlen != (8 * (dopts->ip6d_len + 1))) 10677 return (EINVAL); 10678 10679 if (checkonly) 10680 break; 10681 10682 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10683 (uchar_t **)&ipp->ipp_dstopts, 10684 &ipp->ipp_dstoptslen, 0); 10685 if (reterr != 0) 10686 return (reterr); 10687 if (ipp->ipp_dstoptslen == 0) 10688 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10689 else 10690 ipp->ipp_fields |= IPPF_DSTOPTS; 10691 reterr = tcp_build_hdrs(tcp); 10692 if (reterr != 0) 10693 return (reterr); 10694 break; 10695 } 10696 case IPV6_RTHDR: { 10697 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10698 10699 /* 10700 * Sanity checks - minimum size, size a multiple of 10701 * eight bytes, and matching size passed in. 10702 */ 10703 if (inlen != 0 && 10704 inlen != (8 * (rt->ip6r_len + 1))) 10705 return (EINVAL); 10706 10707 if (checkonly) 10708 break; 10709 10710 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10711 (uchar_t **)&ipp->ipp_rthdr, 10712 &ipp->ipp_rthdrlen, 0); 10713 if (reterr != 0) 10714 return (reterr); 10715 if (ipp->ipp_rthdrlen == 0) 10716 ipp->ipp_fields &= ~IPPF_RTHDR; 10717 else 10718 ipp->ipp_fields |= IPPF_RTHDR; 10719 reterr = tcp_build_hdrs(tcp); 10720 if (reterr != 0) 10721 return (reterr); 10722 break; 10723 } 10724 case IPV6_V6ONLY: 10725 if (!checkonly) { 10726 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10727 } 10728 break; 10729 case IPV6_USE_MIN_MTU: 10730 if (inlen != sizeof (int)) 10731 return (EINVAL); 10732 10733 if (*i1 < -1 || *i1 > 1) 10734 return (EINVAL); 10735 10736 if (checkonly) 10737 break; 10738 10739 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10740 ipp->ipp_use_min_mtu = *i1; 10741 break; 10742 case IPV6_SEC_OPT: 10743 /* 10744 * We should not allow policy setting after 10745 * we start listening for connections. 10746 */ 10747 if (tcp->tcp_state == TCPS_LISTEN) { 10748 return (EINVAL); 10749 } else { 10750 /* Handled at the IP level */ 10751 return (-EINVAL); 10752 } 10753 case IPV6_SRC_PREFERENCES: 10754 if (inlen != sizeof (uint32_t)) 10755 return (EINVAL); 10756 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10757 *(uint32_t *)invalp); 10758 if (reterr != 0) { 10759 *outlenp = 0; 10760 return (reterr); 10761 } 10762 break; 10763 default: 10764 *outlenp = 0; 10765 return (EINVAL); 10766 } 10767 break; 10768 } /* end IPPROTO_IPV6 */ 10769 default: 10770 *outlenp = 0; 10771 return (EINVAL); 10772 } 10773 /* 10774 * Common case of OK return with outval same as inval 10775 */ 10776 if (invalp != outvalp) { 10777 /* don't trust bcopy for identical src/dst */ 10778 (void) bcopy(invalp, outvalp, inlen); 10779 } 10780 *outlenp = inlen; 10781 return (0); 10782 } 10783 10784 /* ARGSUSED */ 10785 int 10786 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10787 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10788 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10789 { 10790 conn_t *connp = Q_TO_CONN(q); 10791 10792 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10793 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10794 } 10795 10796 int 10797 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10798 const void *optvalp, socklen_t optlen, cred_t *cr) 10799 { 10800 conn_t *connp = (conn_t *)proto_handle; 10801 squeue_t *sqp = connp->conn_sqp; 10802 int error; 10803 10804 ASSERT(connp->conn_upper_handle != NULL); 10805 /* 10806 * Entering the squeue synchronously can result in a context switch, 10807 * which can cause a rather sever performance degradation. So we try to 10808 * handle whatever options we can without entering the squeue. 10809 */ 10810 if (level == IPPROTO_TCP) { 10811 switch (option_name) { 10812 case TCP_NODELAY: 10813 if (optlen != sizeof (int32_t)) 10814 return (EINVAL); 10815 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10816 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10817 connp->conn_tcp->tcp_mss; 10818 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10819 return (0); 10820 default: 10821 break; 10822 } 10823 } 10824 10825 error = squeue_synch_enter(sqp, connp, NULL); 10826 if (error == ENOMEM) { 10827 return (ENOMEM); 10828 } 10829 10830 error = proto_opt_check(level, option_name, optlen, NULL, 10831 tcp_opt_obj.odb_opt_des_arr, 10832 tcp_opt_obj.odb_opt_arr_cnt, 10833 tcp_opt_obj.odb_topmost_tpiprovider, 10834 B_TRUE, B_FALSE, cr); 10835 10836 if (error != 0) { 10837 if (error < 0) { 10838 error = proto_tlitosyserr(-error); 10839 } 10840 squeue_synch_exit(sqp, connp); 10841 return (error); 10842 } 10843 10844 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10845 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10846 NULL, cr, NULL); 10847 squeue_synch_exit(sqp, connp); 10848 10849 if (error < 0) { 10850 /* 10851 * Pass on to ip 10852 */ 10853 error = ip_set_options(connp, level, option_name, optvalp, 10854 optlen, cr); 10855 } 10856 return (error); 10857 } 10858 10859 /* 10860 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10861 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10862 * headers, and the maximum size tcp header (to avoid reallocation 10863 * on the fly for additional tcp options). 10864 * Returns failure if can't allocate memory. 10865 */ 10866 static int 10867 tcp_build_hdrs(tcp_t *tcp) 10868 { 10869 char *hdrs; 10870 uint_t hdrs_len; 10871 ip6i_t *ip6i; 10872 char buf[TCP_MAX_HDR_LENGTH]; 10873 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10874 in6_addr_t src, dst; 10875 tcp_stack_t *tcps = tcp->tcp_tcps; 10876 conn_t *connp = tcp->tcp_connp; 10877 10878 /* 10879 * save the existing tcp header and source/dest IP addresses 10880 */ 10881 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10882 src = tcp->tcp_ip6h->ip6_src; 10883 dst = tcp->tcp_ip6h->ip6_dst; 10884 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10885 ASSERT(hdrs_len != 0); 10886 if (hdrs_len > tcp->tcp_iphc_len) { 10887 /* Need to reallocate */ 10888 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10889 if (hdrs == NULL) 10890 return (ENOMEM); 10891 if (tcp->tcp_iphc != NULL) { 10892 if (tcp->tcp_hdr_grown) { 10893 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10894 } else { 10895 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10896 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10897 } 10898 tcp->tcp_iphc_len = 0; 10899 } 10900 ASSERT(tcp->tcp_iphc_len == 0); 10901 tcp->tcp_iphc = hdrs; 10902 tcp->tcp_iphc_len = hdrs_len; 10903 tcp->tcp_hdr_grown = B_TRUE; 10904 } 10905 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10906 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10907 10908 /* Set header fields not in ipp */ 10909 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10910 ip6i = (ip6i_t *)tcp->tcp_iphc; 10911 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10912 } else { 10913 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10914 } 10915 /* 10916 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10917 * 10918 * tcp->tcp_tcp_hdr_len doesn't change here. 10919 */ 10920 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10921 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10922 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10923 10924 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10925 10926 tcp->tcp_ip6h->ip6_src = src; 10927 tcp->tcp_ip6h->ip6_dst = dst; 10928 10929 /* 10930 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10931 * the default value for TCP. 10932 */ 10933 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10934 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10935 10936 /* 10937 * If we're setting extension headers after a connection 10938 * has been established, and if we have a routing header 10939 * among the extension headers, call ip_massage_options_v6 to 10940 * manipulate the routing header/ip6_dst set the checksum 10941 * difference in the tcp header template. 10942 * (This happens in tcp_connect_ipv6 if the routing header 10943 * is set prior to the connect.) 10944 * Set the tcp_sum to zero first in case we've cleared a 10945 * routing header or don't have one at all. 10946 */ 10947 tcp->tcp_sum = 0; 10948 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10949 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10950 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10951 (uint8_t *)tcp->tcp_tcph); 10952 if (rth != NULL) { 10953 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10954 rth, tcps->tcps_netstack); 10955 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10956 (tcp->tcp_sum >> 16)); 10957 } 10958 } 10959 10960 /* Try to get everything in a single mblk */ 10961 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10962 hdrs_len + tcps->tcps_wroff_xtra); 10963 return (0); 10964 } 10965 10966 /* 10967 * Transfer any source route option from ipha to buf/dst in reversed form. 10968 */ 10969 static int 10970 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10971 { 10972 ipoptp_t opts; 10973 uchar_t *opt; 10974 uint8_t optval; 10975 uint8_t optlen; 10976 uint32_t len = 0; 10977 10978 for (optval = ipoptp_first(&opts, ipha); 10979 optval != IPOPT_EOL; 10980 optval = ipoptp_next(&opts)) { 10981 opt = opts.ipoptp_cur; 10982 optlen = opts.ipoptp_len; 10983 switch (optval) { 10984 int off1, off2; 10985 case IPOPT_SSRR: 10986 case IPOPT_LSRR: 10987 10988 /* Reverse source route */ 10989 /* 10990 * First entry should be the next to last one in the 10991 * current source route (the last entry is our 10992 * address.) 10993 * The last entry should be the final destination. 10994 */ 10995 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10996 buf[IPOPT_OLEN] = (uint8_t)optlen; 10997 off1 = IPOPT_MINOFF_SR - 1; 10998 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10999 if (off2 < 0) { 11000 /* No entries in source route */ 11001 break; 11002 } 11003 bcopy(opt + off2, dst, IP_ADDR_LEN); 11004 /* 11005 * Note: use src since ipha has not had its src 11006 * and dst reversed (it is in the state it was 11007 * received. 11008 */ 11009 bcopy(&ipha->ipha_src, buf + off2, 11010 IP_ADDR_LEN); 11011 off2 -= IP_ADDR_LEN; 11012 11013 while (off2 > 0) { 11014 bcopy(opt + off2, buf + off1, 11015 IP_ADDR_LEN); 11016 off1 += IP_ADDR_LEN; 11017 off2 -= IP_ADDR_LEN; 11018 } 11019 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 11020 buf += optlen; 11021 len += optlen; 11022 break; 11023 } 11024 } 11025 done: 11026 /* Pad the resulting options */ 11027 while (len & 0x3) { 11028 *buf++ = IPOPT_EOL; 11029 len++; 11030 } 11031 return (len); 11032 } 11033 11034 11035 /* 11036 * Extract and revert a source route from ipha (if any) 11037 * and then update the relevant fields in both tcp_t and the standard header. 11038 */ 11039 static void 11040 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 11041 { 11042 char buf[TCP_MAX_HDR_LENGTH]; 11043 uint_t tcph_len; 11044 int len; 11045 11046 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 11047 len = IPH_HDR_LENGTH(ipha); 11048 if (len == IP_SIMPLE_HDR_LENGTH) 11049 /* Nothing to do */ 11050 return; 11051 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 11052 (len & 0x3)) 11053 return; 11054 11055 tcph_len = tcp->tcp_tcp_hdr_len; 11056 bcopy(tcp->tcp_tcph, buf, tcph_len); 11057 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 11058 (tcp->tcp_ipha->ipha_dst & 0xffff); 11059 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 11060 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 11061 len += IP_SIMPLE_HDR_LENGTH; 11062 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 11063 (tcp->tcp_ipha->ipha_dst & 0xffff)); 11064 if ((int)tcp->tcp_sum < 0) 11065 tcp->tcp_sum--; 11066 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 11067 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 11068 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 11069 bcopy(buf, tcp->tcp_tcph, tcph_len); 11070 tcp->tcp_ip_hdr_len = len; 11071 tcp->tcp_ipha->ipha_version_and_hdr_length = 11072 (IP_VERSION << 4) | (len >> 2); 11073 len += tcph_len; 11074 tcp->tcp_hdr_len = len; 11075 } 11076 11077 /* 11078 * Copy the standard header into its new location, 11079 * lay in the new options and then update the relevant 11080 * fields in both tcp_t and the standard header. 11081 */ 11082 static int 11083 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 11084 { 11085 uint_t tcph_len; 11086 uint8_t *ip_optp; 11087 tcph_t *new_tcph; 11088 tcp_stack_t *tcps = tcp->tcp_tcps; 11089 conn_t *connp = tcp->tcp_connp; 11090 11091 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 11092 return (EINVAL); 11093 11094 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 11095 return (EINVAL); 11096 11097 if (checkonly) { 11098 /* 11099 * do not really set, just pretend to - T_CHECK 11100 */ 11101 return (0); 11102 } 11103 11104 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 11105 if (tcp->tcp_label_len > 0) { 11106 int padlen; 11107 uint8_t opt; 11108 11109 /* convert list termination to no-ops */ 11110 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 11111 ip_optp += ip_optp[IPOPT_OLEN]; 11112 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11113 while (--padlen >= 0) 11114 *ip_optp++ = opt; 11115 } 11116 tcph_len = tcp->tcp_tcp_hdr_len; 11117 new_tcph = (tcph_t *)(ip_optp + len); 11118 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11119 tcp->tcp_tcph = new_tcph; 11120 bcopy(ptr, ip_optp, len); 11121 11122 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11123 11124 tcp->tcp_ip_hdr_len = len; 11125 tcp->tcp_ipha->ipha_version_and_hdr_length = 11126 (IP_VERSION << 4) | (len >> 2); 11127 tcp->tcp_hdr_len = len + tcph_len; 11128 if (!TCP_IS_DETACHED(tcp)) { 11129 /* Always allocate room for all options. */ 11130 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11131 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11132 } 11133 return (0); 11134 } 11135 11136 /* Get callback routine passed to nd_load by tcp_param_register */ 11137 /* ARGSUSED */ 11138 static int 11139 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11140 { 11141 tcpparam_t *tcppa = (tcpparam_t *)cp; 11142 11143 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11144 return (0); 11145 } 11146 11147 /* 11148 * Walk through the param array specified registering each element with the 11149 * named dispatch handler. 11150 */ 11151 static boolean_t 11152 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11153 { 11154 for (; cnt-- > 0; tcppa++) { 11155 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11156 if (!nd_load(ndp, tcppa->tcp_param_name, 11157 tcp_param_get, tcp_param_set, 11158 (caddr_t)tcppa)) { 11159 nd_free(ndp); 11160 return (B_FALSE); 11161 } 11162 } 11163 } 11164 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11165 KM_SLEEP); 11166 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11167 sizeof (tcpparam_t)); 11168 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11169 tcp_param_get, tcp_param_set_aligned, 11170 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11171 nd_free(ndp); 11172 return (B_FALSE); 11173 } 11174 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11175 KM_SLEEP); 11176 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11177 sizeof (tcpparam_t)); 11178 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11179 tcp_param_get, tcp_param_set_aligned, 11180 (caddr_t)tcps->tcps_mdt_head_param)) { 11181 nd_free(ndp); 11182 return (B_FALSE); 11183 } 11184 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11185 KM_SLEEP); 11186 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11187 sizeof (tcpparam_t)); 11188 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11189 tcp_param_get, tcp_param_set_aligned, 11190 (caddr_t)tcps->tcps_mdt_tail_param)) { 11191 nd_free(ndp); 11192 return (B_FALSE); 11193 } 11194 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11195 KM_SLEEP); 11196 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11197 sizeof (tcpparam_t)); 11198 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11199 tcp_param_get, tcp_param_set_aligned, 11200 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11201 nd_free(ndp); 11202 return (B_FALSE); 11203 } 11204 if (!nd_load(ndp, "tcp_extra_priv_ports", 11205 tcp_extra_priv_ports_get, NULL, NULL)) { 11206 nd_free(ndp); 11207 return (B_FALSE); 11208 } 11209 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11210 NULL, tcp_extra_priv_ports_add, NULL)) { 11211 nd_free(ndp); 11212 return (B_FALSE); 11213 } 11214 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11215 NULL, tcp_extra_priv_ports_del, NULL)) { 11216 nd_free(ndp); 11217 return (B_FALSE); 11218 } 11219 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11220 tcp_1948_phrase_set, NULL)) { 11221 nd_free(ndp); 11222 return (B_FALSE); 11223 } 11224 /* 11225 * Dummy ndd variables - only to convey obsolescence information 11226 * through printing of their name (no get or set routines) 11227 * XXX Remove in future releases ? 11228 */ 11229 if (!nd_load(ndp, 11230 "tcp_close_wait_interval(obsoleted - " 11231 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11232 nd_free(ndp); 11233 return (B_FALSE); 11234 } 11235 return (B_TRUE); 11236 } 11237 11238 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11239 /* ARGSUSED */ 11240 static int 11241 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11242 cred_t *cr) 11243 { 11244 long new_value; 11245 tcpparam_t *tcppa = (tcpparam_t *)cp; 11246 11247 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11248 new_value < tcppa->tcp_param_min || 11249 new_value > tcppa->tcp_param_max) { 11250 return (EINVAL); 11251 } 11252 /* 11253 * Need to make sure new_value is a multiple of 4. If it is not, 11254 * round it up. For future 64 bit requirement, we actually make it 11255 * a multiple of 8. 11256 */ 11257 if (new_value & 0x7) { 11258 new_value = (new_value & ~0x7) + 0x8; 11259 } 11260 tcppa->tcp_param_val = new_value; 11261 return (0); 11262 } 11263 11264 /* Set callback routine passed to nd_load by tcp_param_register */ 11265 /* ARGSUSED */ 11266 static int 11267 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11268 { 11269 long new_value; 11270 tcpparam_t *tcppa = (tcpparam_t *)cp; 11271 11272 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11273 new_value < tcppa->tcp_param_min || 11274 new_value > tcppa->tcp_param_max) { 11275 return (EINVAL); 11276 } 11277 tcppa->tcp_param_val = new_value; 11278 return (0); 11279 } 11280 11281 /* 11282 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11283 * is filled, return as much as we can. The message passed in may be 11284 * multi-part, chained using b_cont. "start" is the starting sequence 11285 * number for this piece. 11286 */ 11287 static mblk_t * 11288 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11289 { 11290 uint32_t end; 11291 mblk_t *mp1; 11292 mblk_t *mp2; 11293 mblk_t *next_mp; 11294 uint32_t u1; 11295 tcp_stack_t *tcps = tcp->tcp_tcps; 11296 11297 /* Walk through all the new pieces. */ 11298 do { 11299 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11300 (uintptr_t)INT_MAX); 11301 end = start + (int)(mp->b_wptr - mp->b_rptr); 11302 next_mp = mp->b_cont; 11303 if (start == end) { 11304 /* Empty. Blast it. */ 11305 freeb(mp); 11306 continue; 11307 } 11308 mp->b_cont = NULL; 11309 TCP_REASS_SET_SEQ(mp, start); 11310 TCP_REASS_SET_END(mp, end); 11311 mp1 = tcp->tcp_reass_tail; 11312 if (!mp1) { 11313 tcp->tcp_reass_tail = mp; 11314 tcp->tcp_reass_head = mp; 11315 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11316 UPDATE_MIB(&tcps->tcps_mib, 11317 tcpInDataUnorderBytes, end - start); 11318 continue; 11319 } 11320 /* New stuff completely beyond tail? */ 11321 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11322 /* Link it on end. */ 11323 mp1->b_cont = mp; 11324 tcp->tcp_reass_tail = mp; 11325 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11326 UPDATE_MIB(&tcps->tcps_mib, 11327 tcpInDataUnorderBytes, end - start); 11328 continue; 11329 } 11330 mp1 = tcp->tcp_reass_head; 11331 u1 = TCP_REASS_SEQ(mp1); 11332 /* New stuff at the front? */ 11333 if (SEQ_LT(start, u1)) { 11334 /* Yes... Check for overlap. */ 11335 mp->b_cont = mp1; 11336 tcp->tcp_reass_head = mp; 11337 tcp_reass_elim_overlap(tcp, mp); 11338 continue; 11339 } 11340 /* 11341 * The new piece fits somewhere between the head and tail. 11342 * We find our slot, where mp1 precedes us and mp2 trails. 11343 */ 11344 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11345 u1 = TCP_REASS_SEQ(mp2); 11346 if (SEQ_LEQ(start, u1)) 11347 break; 11348 } 11349 /* Link ourselves in */ 11350 mp->b_cont = mp2; 11351 mp1->b_cont = mp; 11352 11353 /* Trim overlap with following mblk(s) first */ 11354 tcp_reass_elim_overlap(tcp, mp); 11355 11356 /* Trim overlap with preceding mblk */ 11357 tcp_reass_elim_overlap(tcp, mp1); 11358 11359 } while (start = end, mp = next_mp); 11360 mp1 = tcp->tcp_reass_head; 11361 /* Anything ready to go? */ 11362 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11363 return (NULL); 11364 /* Eat what we can off the queue */ 11365 for (;;) { 11366 mp = mp1->b_cont; 11367 end = TCP_REASS_END(mp1); 11368 TCP_REASS_SET_SEQ(mp1, 0); 11369 TCP_REASS_SET_END(mp1, 0); 11370 if (!mp) { 11371 tcp->tcp_reass_tail = NULL; 11372 break; 11373 } 11374 if (end != TCP_REASS_SEQ(mp)) { 11375 mp1->b_cont = NULL; 11376 break; 11377 } 11378 mp1 = mp; 11379 } 11380 mp1 = tcp->tcp_reass_head; 11381 tcp->tcp_reass_head = mp; 11382 return (mp1); 11383 } 11384 11385 /* Eliminate any overlap that mp may have over later mblks */ 11386 static void 11387 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11388 { 11389 uint32_t end; 11390 mblk_t *mp1; 11391 uint32_t u1; 11392 tcp_stack_t *tcps = tcp->tcp_tcps; 11393 11394 end = TCP_REASS_END(mp); 11395 while ((mp1 = mp->b_cont) != NULL) { 11396 u1 = TCP_REASS_SEQ(mp1); 11397 if (!SEQ_GT(end, u1)) 11398 break; 11399 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11400 mp->b_wptr -= end - u1; 11401 TCP_REASS_SET_END(mp, u1); 11402 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11403 UPDATE_MIB(&tcps->tcps_mib, 11404 tcpInDataPartDupBytes, end - u1); 11405 break; 11406 } 11407 mp->b_cont = mp1->b_cont; 11408 TCP_REASS_SET_SEQ(mp1, 0); 11409 TCP_REASS_SET_END(mp1, 0); 11410 freeb(mp1); 11411 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11412 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11413 } 11414 if (!mp1) 11415 tcp->tcp_reass_tail = mp; 11416 } 11417 11418 static uint_t 11419 tcp_rwnd_reopen(tcp_t *tcp) 11420 { 11421 uint_t ret = 0; 11422 uint_t thwin; 11423 11424 /* Learn the latest rwnd information that we sent to the other side. */ 11425 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11426 << tcp->tcp_rcv_ws; 11427 /* This is peer's calculated send window (our receive window). */ 11428 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11429 /* 11430 * Increase the receive window to max. But we need to do receiver 11431 * SWS avoidance. This means that we need to check the increase of 11432 * of receive window is at least 1 MSS. 11433 */ 11434 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11435 /* 11436 * If the window that the other side knows is less than max 11437 * deferred acks segments, send an update immediately. 11438 */ 11439 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11440 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11441 ret = TH_ACK_NEEDED; 11442 } 11443 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11444 } 11445 return (ret); 11446 } 11447 11448 /* 11449 * Send up all messages queued on tcp_rcv_list. 11450 */ 11451 static uint_t 11452 tcp_rcv_drain(tcp_t *tcp) 11453 { 11454 mblk_t *mp; 11455 uint_t ret = 0; 11456 #ifdef DEBUG 11457 uint_t cnt = 0; 11458 #endif 11459 queue_t *q = tcp->tcp_rq; 11460 11461 /* Can't drain on an eager connection */ 11462 if (tcp->tcp_listener != NULL) 11463 return (ret); 11464 11465 /* Can't be a non-STREAMS connection */ 11466 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11467 11468 /* No need for the push timer now. */ 11469 if (tcp->tcp_push_tid != 0) { 11470 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11471 tcp->tcp_push_tid = 0; 11472 } 11473 11474 /* 11475 * Handle two cases here: we are currently fused or we were 11476 * previously fused and have some urgent data to be delivered 11477 * upstream. The latter happens because we either ran out of 11478 * memory or were detached and therefore sending the SIGURG was 11479 * deferred until this point. In either case we pass control 11480 * over to tcp_fuse_rcv_drain() since it may need to complete 11481 * some work. 11482 */ 11483 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11484 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11485 tcp->tcp_fused_sigurg_mp != NULL); 11486 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11487 &tcp->tcp_fused_sigurg_mp)) 11488 return (ret); 11489 } 11490 11491 while ((mp = tcp->tcp_rcv_list) != NULL) { 11492 tcp->tcp_rcv_list = mp->b_next; 11493 mp->b_next = NULL; 11494 #ifdef DEBUG 11495 cnt += msgdsize(mp); 11496 #endif 11497 /* Does this need SSL processing first? */ 11498 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11499 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11500 mblk_t *, mp); 11501 tcp_kssl_input(tcp, mp); 11502 continue; 11503 } 11504 putnext(q, mp); 11505 } 11506 #ifdef DEBUG 11507 ASSERT(cnt == tcp->tcp_rcv_cnt); 11508 #endif 11509 tcp->tcp_rcv_last_head = NULL; 11510 tcp->tcp_rcv_last_tail = NULL; 11511 tcp->tcp_rcv_cnt = 0; 11512 11513 if (canputnext(q)) 11514 return (tcp_rwnd_reopen(tcp)); 11515 11516 return (ret); 11517 } 11518 11519 /* 11520 * Queue data on tcp_rcv_list which is a b_next chain. 11521 * tcp_rcv_last_head/tail is the last element of this chain. 11522 * Each element of the chain is a b_cont chain. 11523 * 11524 * M_DATA messages are added to the current element. 11525 * Other messages are added as new (b_next) elements. 11526 */ 11527 void 11528 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11529 { 11530 ASSERT(seg_len == msgdsize(mp)); 11531 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11532 11533 if (tcp->tcp_rcv_list == NULL) { 11534 ASSERT(tcp->tcp_rcv_last_head == NULL); 11535 tcp->tcp_rcv_list = mp; 11536 tcp->tcp_rcv_last_head = mp; 11537 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11538 tcp->tcp_rcv_last_tail->b_cont = mp; 11539 } else { 11540 tcp->tcp_rcv_last_head->b_next = mp; 11541 tcp->tcp_rcv_last_head = mp; 11542 } 11543 11544 while (mp->b_cont) 11545 mp = mp->b_cont; 11546 11547 tcp->tcp_rcv_last_tail = mp; 11548 tcp->tcp_rcv_cnt += seg_len; 11549 tcp->tcp_rwnd -= seg_len; 11550 } 11551 11552 /* 11553 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11554 * 11555 * This is the default entry function into TCP on the read side. TCP is 11556 * always entered via squeue i.e. using squeue's for mutual exclusion. 11557 * When classifier does a lookup to find the tcp, it also puts a reference 11558 * on the conn structure associated so the tcp is guaranteed to exist 11559 * when we come here. We still need to check the state because it might 11560 * as well has been closed. The squeue processing function i.e. squeue_enter, 11561 * is responsible for doing the CONN_DEC_REF. 11562 * 11563 * Apart from the default entry point, IP also sends packets directly to 11564 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11565 * connections. 11566 */ 11567 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11568 void 11569 tcp_input(void *arg, mblk_t *mp, void *arg2) 11570 { 11571 conn_t *connp = (conn_t *)arg; 11572 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11573 11574 /* arg2 is the sqp */ 11575 ASSERT(arg2 != NULL); 11576 ASSERT(mp != NULL); 11577 11578 /* 11579 * Don't accept any input on a closed tcp as this TCP logically does 11580 * not exist on the system. Don't proceed further with this TCP. 11581 * For eg. this packet could trigger another close of this tcp 11582 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11583 * tcp_clean_death / tcp_closei_local must be called at most once 11584 * on a TCP. In this case we need to refeed the packet into the 11585 * classifier and figure out where the packet should go. Need to 11586 * preserve the recv_ill somehow. Until we figure that out, for 11587 * now just drop the packet if we can't classify the packet. 11588 */ 11589 if (tcp->tcp_state == TCPS_CLOSED || 11590 tcp->tcp_state == TCPS_BOUND) { 11591 conn_t *new_connp; 11592 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11593 11594 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11595 if (new_connp != NULL) { 11596 tcp_reinput(new_connp, mp, arg2); 11597 return; 11598 } 11599 /* We failed to classify. For now just drop the packet */ 11600 freemsg(mp); 11601 return; 11602 } 11603 11604 if (DB_TYPE(mp) != M_DATA) { 11605 tcp_rput_common(tcp, mp); 11606 return; 11607 } 11608 11609 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11610 squeue_t *final_sqp; 11611 11612 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11613 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11614 DB_CKSUMSTART(mp) = 0; 11615 if (tcp->tcp_state == TCPS_SYN_SENT && 11616 connp->conn_final_sqp == NULL && 11617 tcp_outbound_squeue_switch) { 11618 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11619 connp->conn_final_sqp = final_sqp; 11620 if (connp->conn_final_sqp != connp->conn_sqp) { 11621 CONN_INC_REF(connp); 11622 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11623 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11624 tcp_rput_data, connp, ip_squeue_flag, 11625 SQTAG_CONNECT_FINISH); 11626 return; 11627 } 11628 } 11629 } 11630 tcp_rput_data(connp, mp, arg2); 11631 } 11632 11633 /* 11634 * The read side put procedure. 11635 * The packets passed up by ip are assume to be aligned according to 11636 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11637 */ 11638 static void 11639 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11640 { 11641 /* 11642 * tcp_rput_data() does not expect M_CTL except for the case 11643 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11644 * type. Need to make sure that any other M_CTLs don't make 11645 * it to tcp_rput_data since it is not expecting any and doesn't 11646 * check for it. 11647 */ 11648 if (DB_TYPE(mp) == M_CTL) { 11649 switch (*(uint32_t *)(mp->b_rptr)) { 11650 case TCP_IOC_ABORT_CONN: 11651 /* 11652 * Handle connection abort request. 11653 */ 11654 tcp_ioctl_abort_handler(tcp, mp); 11655 return; 11656 case IPSEC_IN: 11657 /* 11658 * Only secure icmp arrive in TCP and they 11659 * don't go through data path. 11660 */ 11661 tcp_icmp_error(tcp, mp); 11662 return; 11663 case IN_PKTINFO: 11664 /* 11665 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11666 * sockets that are receiving IPv4 traffic. tcp 11667 */ 11668 ASSERT(tcp->tcp_family == AF_INET6); 11669 ASSERT(tcp->tcp_ipv6_recvancillary & 11670 TCP_IPV6_RECVPKTINFO); 11671 tcp_rput_data(tcp->tcp_connp, mp, 11672 tcp->tcp_connp->conn_sqp); 11673 return; 11674 case MDT_IOC_INFO_UPDATE: 11675 /* 11676 * Handle Multidata information update; the 11677 * following routine will free the message. 11678 */ 11679 if (tcp->tcp_connp->conn_mdt_ok) { 11680 tcp_mdt_update(tcp, 11681 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11682 B_FALSE); 11683 } 11684 freemsg(mp); 11685 return; 11686 case LSO_IOC_INFO_UPDATE: 11687 /* 11688 * Handle LSO information update; the following 11689 * routine will free the message. 11690 */ 11691 if (tcp->tcp_connp->conn_lso_ok) { 11692 tcp_lso_update(tcp, 11693 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11694 } 11695 freemsg(mp); 11696 return; 11697 default: 11698 /* 11699 * tcp_icmp_err() will process the M_CTL packets. 11700 * Non-ICMP packets, if any, will be discarded in 11701 * tcp_icmp_err(). We will process the ICMP packet 11702 * even if we are TCP_IS_DETACHED_NONEAGER as the 11703 * incoming ICMP packet may result in changing 11704 * the tcp_mss, which we would need if we have 11705 * packets to retransmit. 11706 */ 11707 tcp_icmp_error(tcp, mp); 11708 return; 11709 } 11710 } 11711 11712 /* No point processing the message if tcp is already closed */ 11713 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11714 freemsg(mp); 11715 return; 11716 } 11717 11718 tcp_rput_other(tcp, mp); 11719 } 11720 11721 11722 /* The minimum of smoothed mean deviation in RTO calculation. */ 11723 #define TCP_SD_MIN 400 11724 11725 /* 11726 * Set RTO for this connection. The formula is from Jacobson and Karels' 11727 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11728 * are the same as those in Appendix A.2 of that paper. 11729 * 11730 * m = new measurement 11731 * sa = smoothed RTT average (8 * average estimates). 11732 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11733 */ 11734 static void 11735 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11736 { 11737 long m = TICK_TO_MSEC(rtt); 11738 clock_t sa = tcp->tcp_rtt_sa; 11739 clock_t sv = tcp->tcp_rtt_sd; 11740 clock_t rto; 11741 tcp_stack_t *tcps = tcp->tcp_tcps; 11742 11743 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11744 tcp->tcp_rtt_update++; 11745 11746 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11747 if (sa != 0) { 11748 /* 11749 * Update average estimator: 11750 * new rtt = 7/8 old rtt + 1/8 Error 11751 */ 11752 11753 /* m is now Error in estimate. */ 11754 m -= sa >> 3; 11755 if ((sa += m) <= 0) { 11756 /* 11757 * Don't allow the smoothed average to be negative. 11758 * We use 0 to denote reinitialization of the 11759 * variables. 11760 */ 11761 sa = 1; 11762 } 11763 11764 /* 11765 * Update deviation estimator: 11766 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11767 */ 11768 if (m < 0) 11769 m = -m; 11770 m -= sv >> 2; 11771 sv += m; 11772 } else { 11773 /* 11774 * This follows BSD's implementation. So the reinitialized 11775 * RTO is 3 * m. We cannot go less than 2 because if the 11776 * link is bandwidth dominated, doubling the window size 11777 * during slow start means doubling the RTT. We want to be 11778 * more conservative when we reinitialize our estimates. 3 11779 * is just a convenient number. 11780 */ 11781 sa = m << 3; 11782 sv = m << 1; 11783 } 11784 if (sv < TCP_SD_MIN) { 11785 /* 11786 * We do not know that if sa captures the delay ACK 11787 * effect as in a long train of segments, a receiver 11788 * does not delay its ACKs. So set the minimum of sv 11789 * to be TCP_SD_MIN, which is default to 400 ms, twice 11790 * of BSD DATO. That means the minimum of mean 11791 * deviation is 100 ms. 11792 * 11793 */ 11794 sv = TCP_SD_MIN; 11795 } 11796 tcp->tcp_rtt_sa = sa; 11797 tcp->tcp_rtt_sd = sv; 11798 /* 11799 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11800 * 11801 * Add tcp_rexmit_interval extra in case of extreme environment 11802 * where the algorithm fails to work. The default value of 11803 * tcp_rexmit_interval_extra should be 0. 11804 * 11805 * As we use a finer grained clock than BSD and update 11806 * RTO for every ACKs, add in another .25 of RTT to the 11807 * deviation of RTO to accomodate burstiness of 1/4 of 11808 * window size. 11809 */ 11810 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11811 11812 if (rto > tcps->tcps_rexmit_interval_max) { 11813 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11814 } else if (rto < tcps->tcps_rexmit_interval_min) { 11815 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11816 } else { 11817 tcp->tcp_rto = rto; 11818 } 11819 11820 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11821 tcp->tcp_timer_backoff = 0; 11822 } 11823 11824 /* 11825 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11826 * send queue which starts at the given sequence number. If the given 11827 * sequence number is equal to last valid sequence number (tcp_snxt), the 11828 * returned mblk is the last valid mblk, and off is set to the length of 11829 * that mblk. 11830 * 11831 * send queue which starts at the given seq. no. 11832 * 11833 * Parameters: 11834 * tcp_t *tcp: the tcp instance pointer. 11835 * uint32_t seq: the starting seq. no of the requested segment. 11836 * int32_t *off: after the execution, *off will be the offset to 11837 * the returned mblk which points to the requested seq no. 11838 * It is the caller's responsibility to send in a non-null off. 11839 * 11840 * Return: 11841 * A mblk_t pointer pointing to the requested segment in send queue. 11842 */ 11843 static mblk_t * 11844 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11845 { 11846 int32_t cnt; 11847 mblk_t *mp; 11848 11849 /* Defensive coding. Make sure we don't send incorrect data. */ 11850 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GT(seq, tcp->tcp_snxt)) 11851 return (NULL); 11852 11853 cnt = seq - tcp->tcp_suna; 11854 mp = tcp->tcp_xmit_head; 11855 while (cnt > 0 && mp != NULL) { 11856 cnt -= mp->b_wptr - mp->b_rptr; 11857 if (cnt <= 0) { 11858 cnt += mp->b_wptr - mp->b_rptr; 11859 break; 11860 } 11861 mp = mp->b_cont; 11862 } 11863 ASSERT(mp != NULL); 11864 *off = cnt; 11865 return (mp); 11866 } 11867 11868 /* 11869 * This function handles all retransmissions if SACK is enabled for this 11870 * connection. First it calculates how many segments can be retransmitted 11871 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11872 * segments. A segment is eligible if sack_cnt for that segment is greater 11873 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11874 * all eligible segments, it checks to see if TCP can send some new segments 11875 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11876 * 11877 * Parameters: 11878 * tcp_t *tcp: the tcp structure of the connection. 11879 * uint_t *flags: in return, appropriate value will be set for 11880 * tcp_rput_data(). 11881 */ 11882 static void 11883 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11884 { 11885 notsack_blk_t *notsack_blk; 11886 int32_t usable_swnd; 11887 int32_t mss; 11888 uint32_t seg_len; 11889 mblk_t *xmit_mp; 11890 tcp_stack_t *tcps = tcp->tcp_tcps; 11891 11892 ASSERT(tcp->tcp_sack_info != NULL); 11893 ASSERT(tcp->tcp_notsack_list != NULL); 11894 ASSERT(tcp->tcp_rexmit == B_FALSE); 11895 11896 /* Defensive coding in case there is a bug... */ 11897 if (tcp->tcp_notsack_list == NULL) { 11898 return; 11899 } 11900 notsack_blk = tcp->tcp_notsack_list; 11901 mss = tcp->tcp_mss; 11902 11903 /* 11904 * Limit the num of outstanding data in the network to be 11905 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11906 */ 11907 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11908 11909 /* At least retransmit 1 MSS of data. */ 11910 if (usable_swnd <= 0) { 11911 usable_swnd = mss; 11912 } 11913 11914 /* Make sure no new RTT samples will be taken. */ 11915 tcp->tcp_csuna = tcp->tcp_snxt; 11916 11917 notsack_blk = tcp->tcp_notsack_list; 11918 while (usable_swnd > 0) { 11919 mblk_t *snxt_mp, *tmp_mp; 11920 tcp_seq begin = tcp->tcp_sack_snxt; 11921 tcp_seq end; 11922 int32_t off; 11923 11924 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11925 if (SEQ_GT(notsack_blk->end, begin) && 11926 (notsack_blk->sack_cnt >= 11927 tcps->tcps_dupack_fast_retransmit)) { 11928 end = notsack_blk->end; 11929 if (SEQ_LT(begin, notsack_blk->begin)) { 11930 begin = notsack_blk->begin; 11931 } 11932 break; 11933 } 11934 } 11935 /* 11936 * All holes are filled. Manipulate tcp_cwnd to send more 11937 * if we can. Note that after the SACK recovery, tcp_cwnd is 11938 * set to tcp_cwnd_ssthresh. 11939 */ 11940 if (notsack_blk == NULL) { 11941 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11942 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11943 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11944 ASSERT(tcp->tcp_cwnd > 0); 11945 return; 11946 } else { 11947 usable_swnd = usable_swnd / mss; 11948 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11949 MAX(usable_swnd * mss, mss); 11950 *flags |= TH_XMIT_NEEDED; 11951 return; 11952 } 11953 } 11954 11955 /* 11956 * Note that we may send more than usable_swnd allows here 11957 * because of round off, but no more than 1 MSS of data. 11958 */ 11959 seg_len = end - begin; 11960 if (seg_len > mss) 11961 seg_len = mss; 11962 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11963 ASSERT(snxt_mp != NULL); 11964 /* This should not happen. Defensive coding again... */ 11965 if (snxt_mp == NULL) { 11966 return; 11967 } 11968 11969 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11970 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11971 if (xmit_mp == NULL) 11972 return; 11973 11974 usable_swnd -= seg_len; 11975 tcp->tcp_pipe += seg_len; 11976 tcp->tcp_sack_snxt = begin + seg_len; 11977 11978 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11979 11980 /* 11981 * Update the send timestamp to avoid false retransmission. 11982 */ 11983 snxt_mp->b_prev = (mblk_t *)lbolt; 11984 11985 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11986 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11987 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11988 /* 11989 * Update tcp_rexmit_max to extend this SACK recovery phase. 11990 * This happens when new data sent during fast recovery is 11991 * also lost. If TCP retransmits those new data, it needs 11992 * to extend SACK recover phase to avoid starting another 11993 * fast retransmit/recovery unnecessarily. 11994 */ 11995 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11996 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11997 } 11998 } 11999 } 12000 12001 /* 12002 * This function handles policy checking at TCP level for non-hard_bound/ 12003 * detached connections. 12004 */ 12005 static boolean_t 12006 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 12007 boolean_t secure, boolean_t mctl_present) 12008 { 12009 ipsec_latch_t *ipl = NULL; 12010 ipsec_action_t *act = NULL; 12011 mblk_t *data_mp; 12012 ipsec_in_t *ii; 12013 const char *reason; 12014 kstat_named_t *counter; 12015 tcp_stack_t *tcps = tcp->tcp_tcps; 12016 ipsec_stack_t *ipss; 12017 ip_stack_t *ipst; 12018 12019 ASSERT(mctl_present || !secure); 12020 12021 ASSERT((ipha == NULL && ip6h != NULL) || 12022 (ip6h == NULL && ipha != NULL)); 12023 12024 /* 12025 * We don't necessarily have an ipsec_in_act action to verify 12026 * policy because of assymetrical policy where we have only 12027 * outbound policy and no inbound policy (possible with global 12028 * policy). 12029 */ 12030 if (!secure) { 12031 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 12032 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 12033 return (B_TRUE); 12034 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 12035 "tcp_check_policy", ipha, ip6h, secure, 12036 tcps->tcps_netstack); 12037 ipss = tcps->tcps_netstack->netstack_ipsec; 12038 12039 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12040 DROPPER(ipss, ipds_tcp_clear), 12041 &tcps->tcps_dropper); 12042 return (B_FALSE); 12043 } 12044 12045 /* 12046 * We have a secure packet. 12047 */ 12048 if (act == NULL) { 12049 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 12050 "tcp_check_policy", ipha, ip6h, secure, 12051 tcps->tcps_netstack); 12052 ipss = tcps->tcps_netstack->netstack_ipsec; 12053 12054 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 12055 DROPPER(ipss, ipds_tcp_secure), 12056 &tcps->tcps_dropper); 12057 return (B_FALSE); 12058 } 12059 12060 /* 12061 * XXX This whole routine is currently incorrect. ipl should 12062 * be set to the latch pointer, but is currently not set, so 12063 * we initialize it to NULL to avoid picking up random garbage. 12064 */ 12065 if (ipl == NULL) 12066 return (B_TRUE); 12067 12068 data_mp = first_mp->b_cont; 12069 12070 ii = (ipsec_in_t *)first_mp->b_rptr; 12071 12072 ipst = tcps->tcps_netstack->netstack_ip; 12073 12074 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 12075 &counter, tcp->tcp_connp)) { 12076 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 12077 return (B_TRUE); 12078 } 12079 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 12080 "tcp inbound policy mismatch: %s, packet dropped\n", 12081 reason); 12082 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 12083 12084 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 12085 &tcps->tcps_dropper); 12086 return (B_FALSE); 12087 } 12088 12089 /* 12090 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 12091 * retransmission after a timeout. 12092 * 12093 * To limit the number of duplicate segments, we limit the number of segment 12094 * to be sent in one time to tcp_snd_burst, the burst variable. 12095 */ 12096 static void 12097 tcp_ss_rexmit(tcp_t *tcp) 12098 { 12099 uint32_t snxt; 12100 uint32_t smax; 12101 int32_t win; 12102 int32_t mss; 12103 int32_t off; 12104 int32_t burst = tcp->tcp_snd_burst; 12105 mblk_t *snxt_mp; 12106 tcp_stack_t *tcps = tcp->tcp_tcps; 12107 12108 /* 12109 * Note that tcp_rexmit can be set even though TCP has retransmitted 12110 * all unack'ed segments. 12111 */ 12112 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 12113 smax = tcp->tcp_rexmit_max; 12114 snxt = tcp->tcp_rexmit_nxt; 12115 if (SEQ_LT(snxt, tcp->tcp_suna)) { 12116 snxt = tcp->tcp_suna; 12117 } 12118 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12119 win -= snxt - tcp->tcp_suna; 12120 mss = tcp->tcp_mss; 12121 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12122 12123 while (SEQ_LT(snxt, smax) && (win > 0) && 12124 (burst > 0) && (snxt_mp != NULL)) { 12125 mblk_t *xmit_mp; 12126 mblk_t *old_snxt_mp = snxt_mp; 12127 uint32_t cnt = mss; 12128 12129 if (win < cnt) { 12130 cnt = win; 12131 } 12132 if (SEQ_GT(snxt + cnt, smax)) { 12133 cnt = smax - snxt; 12134 } 12135 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12136 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12137 if (xmit_mp == NULL) 12138 return; 12139 12140 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12141 12142 snxt += cnt; 12143 win -= cnt; 12144 /* 12145 * Update the send timestamp to avoid false 12146 * retransmission. 12147 */ 12148 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12149 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12150 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12151 12152 tcp->tcp_rexmit_nxt = snxt; 12153 burst--; 12154 } 12155 /* 12156 * If we have transmitted all we have at the time 12157 * we started the retranmission, we can leave 12158 * the rest of the job to tcp_wput_data(). But we 12159 * need to check the send window first. If the 12160 * win is not 0, go on with tcp_wput_data(). 12161 */ 12162 if (SEQ_LT(snxt, smax) || win == 0) { 12163 return; 12164 } 12165 } 12166 /* Only call tcp_wput_data() if there is data to be sent. */ 12167 if (tcp->tcp_unsent) { 12168 tcp_wput_data(tcp, NULL, B_FALSE); 12169 } 12170 } 12171 12172 /* 12173 * Process all TCP option in SYN segment. Note that this function should 12174 * be called after tcp_adapt_ire() is called so that the necessary info 12175 * from IRE is already set in the tcp structure. 12176 * 12177 * This function sets up the correct tcp_mss value according to the 12178 * MSS option value and our header size. It also sets up the window scale 12179 * and timestamp values, and initialize SACK info blocks. But it does not 12180 * change receive window size after setting the tcp_mss value. The caller 12181 * should do the appropriate change. 12182 */ 12183 void 12184 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12185 { 12186 int options; 12187 tcp_opt_t tcpopt; 12188 uint32_t mss_max; 12189 char *tmp_tcph; 12190 tcp_stack_t *tcps = tcp->tcp_tcps; 12191 12192 tcpopt.tcp = NULL; 12193 options = tcp_parse_options(tcph, &tcpopt); 12194 12195 /* 12196 * Process MSS option. Note that MSS option value does not account 12197 * for IP or TCP options. This means that it is equal to MTU - minimum 12198 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12199 * IPv6. 12200 */ 12201 if (!(options & TCP_OPT_MSS_PRESENT)) { 12202 if (tcp->tcp_ipversion == IPV4_VERSION) 12203 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12204 else 12205 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12206 } else { 12207 if (tcp->tcp_ipversion == IPV4_VERSION) 12208 mss_max = tcps->tcps_mss_max_ipv4; 12209 else 12210 mss_max = tcps->tcps_mss_max_ipv6; 12211 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12212 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12213 else if (tcpopt.tcp_opt_mss > mss_max) 12214 tcpopt.tcp_opt_mss = mss_max; 12215 } 12216 12217 /* Process Window Scale option. */ 12218 if (options & TCP_OPT_WSCALE_PRESENT) { 12219 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12220 tcp->tcp_snd_ws_ok = B_TRUE; 12221 } else { 12222 tcp->tcp_snd_ws = B_FALSE; 12223 tcp->tcp_snd_ws_ok = B_FALSE; 12224 tcp->tcp_rcv_ws = B_FALSE; 12225 } 12226 12227 /* Process Timestamp option. */ 12228 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12229 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12230 tmp_tcph = (char *)tcp->tcp_tcph; 12231 12232 tcp->tcp_snd_ts_ok = B_TRUE; 12233 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12234 tcp->tcp_last_rcv_lbolt = lbolt64; 12235 ASSERT(OK_32PTR(tmp_tcph)); 12236 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12237 12238 /* Fill in our template header with basic timestamp option. */ 12239 tmp_tcph += tcp->tcp_tcp_hdr_len; 12240 tmp_tcph[0] = TCPOPT_NOP; 12241 tmp_tcph[1] = TCPOPT_NOP; 12242 tmp_tcph[2] = TCPOPT_TSTAMP; 12243 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12244 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12245 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12246 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12247 } else { 12248 tcp->tcp_snd_ts_ok = B_FALSE; 12249 } 12250 12251 /* 12252 * Process SACK options. If SACK is enabled for this connection, 12253 * then allocate the SACK info structure. Note the following ways 12254 * when tcp_snd_sack_ok is set to true. 12255 * 12256 * For active connection: in tcp_adapt_ire() called in 12257 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12258 * is checked. 12259 * 12260 * For passive connection: in tcp_adapt_ire() called in 12261 * tcp_accept_comm(). 12262 * 12263 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12264 * That check makes sure that if we did not send a SACK OK option, 12265 * we will not enable SACK for this connection even though the other 12266 * side sends us SACK OK option. For active connection, the SACK 12267 * info structure has already been allocated. So we need to free 12268 * it if SACK is disabled. 12269 */ 12270 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12271 (tcp->tcp_snd_sack_ok || 12272 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12273 /* This should be true only in the passive case. */ 12274 if (tcp->tcp_sack_info == NULL) { 12275 ASSERT(TCP_IS_DETACHED(tcp)); 12276 tcp->tcp_sack_info = 12277 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12278 } 12279 if (tcp->tcp_sack_info == NULL) { 12280 tcp->tcp_snd_sack_ok = B_FALSE; 12281 } else { 12282 tcp->tcp_snd_sack_ok = B_TRUE; 12283 if (tcp->tcp_snd_ts_ok) { 12284 tcp->tcp_max_sack_blk = 3; 12285 } else { 12286 tcp->tcp_max_sack_blk = 4; 12287 } 12288 } 12289 } else { 12290 /* 12291 * Resetting tcp_snd_sack_ok to B_FALSE so that 12292 * no SACK info will be used for this 12293 * connection. This assumes that SACK usage 12294 * permission is negotiated. This may need 12295 * to be changed once this is clarified. 12296 */ 12297 if (tcp->tcp_sack_info != NULL) { 12298 ASSERT(tcp->tcp_notsack_list == NULL); 12299 kmem_cache_free(tcp_sack_info_cache, 12300 tcp->tcp_sack_info); 12301 tcp->tcp_sack_info = NULL; 12302 } 12303 tcp->tcp_snd_sack_ok = B_FALSE; 12304 } 12305 12306 /* 12307 * Now we know the exact TCP/IP header length, subtract 12308 * that from tcp_mss to get our side's MSS. 12309 */ 12310 tcp->tcp_mss -= tcp->tcp_hdr_len; 12311 /* 12312 * Here we assume that the other side's header size will be equal to 12313 * our header size. We calculate the real MSS accordingly. Need to 12314 * take into additional stuffs IPsec puts in. 12315 * 12316 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12317 */ 12318 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12319 ((tcp->tcp_ipversion == IPV4_VERSION ? 12320 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12321 12322 /* 12323 * Set MSS to the smaller one of both ends of the connection. 12324 * We should not have called tcp_mss_set() before, but our 12325 * side of the MSS should have been set to a proper value 12326 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12327 * STREAM head parameters properly. 12328 * 12329 * If we have a larger-than-16-bit window but the other side 12330 * didn't want to do window scale, tcp_rwnd_set() will take 12331 * care of that. 12332 */ 12333 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12334 } 12335 12336 /* 12337 * Sends the T_CONN_IND to the listener. The caller calls this 12338 * functions via squeue to get inside the listener's perimeter 12339 * once the 3 way hand shake is done a T_CONN_IND needs to be 12340 * sent. As an optimization, the caller can call this directly 12341 * if listener's perimeter is same as eager's. 12342 */ 12343 /* ARGSUSED */ 12344 void 12345 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12346 { 12347 conn_t *lconnp = (conn_t *)arg; 12348 tcp_t *listener = lconnp->conn_tcp; 12349 tcp_t *tcp; 12350 struct T_conn_ind *conn_ind; 12351 ipaddr_t *addr_cache; 12352 boolean_t need_send_conn_ind = B_FALSE; 12353 tcp_stack_t *tcps = listener->tcp_tcps; 12354 12355 /* retrieve the eager */ 12356 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12357 ASSERT(conn_ind->OPT_offset != 0 && 12358 conn_ind->OPT_length == sizeof (intptr_t)); 12359 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12360 conn_ind->OPT_length); 12361 12362 /* 12363 * TLI/XTI applications will get confused by 12364 * sending eager as an option since it violates 12365 * the option semantics. So remove the eager as 12366 * option since TLI/XTI app doesn't need it anyway. 12367 */ 12368 if (!TCP_IS_SOCKET(listener)) { 12369 conn_ind->OPT_length = 0; 12370 conn_ind->OPT_offset = 0; 12371 } 12372 if (listener->tcp_state != TCPS_LISTEN) { 12373 /* 12374 * If listener has closed, it would have caused a 12375 * a cleanup/blowoff to happen for the eager. We 12376 * just need to return. 12377 */ 12378 freemsg(mp); 12379 return; 12380 } 12381 12382 12383 /* 12384 * if the conn_req_q is full defer passing up the 12385 * T_CONN_IND until space is availabe after t_accept() 12386 * processing 12387 */ 12388 mutex_enter(&listener->tcp_eager_lock); 12389 12390 /* 12391 * Take the eager out, if it is in the list of droppable eagers 12392 * as we are here because the 3W handshake is over. 12393 */ 12394 MAKE_UNDROPPABLE(tcp); 12395 12396 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12397 tcp_t *tail; 12398 12399 /* 12400 * The eager already has an extra ref put in tcp_rput_data 12401 * so that it stays till accept comes back even though it 12402 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12403 */ 12404 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12405 listener->tcp_conn_req_cnt_q0--; 12406 listener->tcp_conn_req_cnt_q++; 12407 12408 /* Move from SYN_RCVD to ESTABLISHED list */ 12409 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12410 tcp->tcp_eager_prev_q0; 12411 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12412 tcp->tcp_eager_next_q0; 12413 tcp->tcp_eager_prev_q0 = NULL; 12414 tcp->tcp_eager_next_q0 = NULL; 12415 12416 /* 12417 * Insert at end of the queue because sockfs 12418 * sends down T_CONN_RES in chronological 12419 * order. Leaving the older conn indications 12420 * at front of the queue helps reducing search 12421 * time. 12422 */ 12423 tail = listener->tcp_eager_last_q; 12424 if (tail != NULL) 12425 tail->tcp_eager_next_q = tcp; 12426 else 12427 listener->tcp_eager_next_q = tcp; 12428 listener->tcp_eager_last_q = tcp; 12429 tcp->tcp_eager_next_q = NULL; 12430 /* 12431 * Delay sending up the T_conn_ind until we are 12432 * done with the eager. Once we have have sent up 12433 * the T_conn_ind, the accept can potentially complete 12434 * any time and release the refhold we have on the eager. 12435 */ 12436 need_send_conn_ind = B_TRUE; 12437 } else { 12438 /* 12439 * Defer connection on q0 and set deferred 12440 * connection bit true 12441 */ 12442 tcp->tcp_conn_def_q0 = B_TRUE; 12443 12444 /* take tcp out of q0 ... */ 12445 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12446 tcp->tcp_eager_next_q0; 12447 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12448 tcp->tcp_eager_prev_q0; 12449 12450 /* ... and place it at the end of q0 */ 12451 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12452 tcp->tcp_eager_next_q0 = listener; 12453 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12454 listener->tcp_eager_prev_q0 = tcp; 12455 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12456 } 12457 12458 /* we have timed out before */ 12459 if (tcp->tcp_syn_rcvd_timeout != 0) { 12460 tcp->tcp_syn_rcvd_timeout = 0; 12461 listener->tcp_syn_rcvd_timeout--; 12462 if (listener->tcp_syn_defense && 12463 listener->tcp_syn_rcvd_timeout <= 12464 (tcps->tcps_conn_req_max_q0 >> 5) && 12465 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12466 listener->tcp_last_rcv_lbolt)) { 12467 /* 12468 * Turn off the defense mode if we 12469 * believe the SYN attack is over. 12470 */ 12471 listener->tcp_syn_defense = B_FALSE; 12472 if (listener->tcp_ip_addr_cache) { 12473 kmem_free((void *)listener->tcp_ip_addr_cache, 12474 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12475 listener->tcp_ip_addr_cache = NULL; 12476 } 12477 } 12478 } 12479 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12480 if (addr_cache != NULL) { 12481 /* 12482 * We have finished a 3-way handshake with this 12483 * remote host. This proves the IP addr is good. 12484 * Cache it! 12485 */ 12486 addr_cache[IP_ADDR_CACHE_HASH( 12487 tcp->tcp_remote)] = tcp->tcp_remote; 12488 } 12489 mutex_exit(&listener->tcp_eager_lock); 12490 if (need_send_conn_ind) 12491 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12492 } 12493 12494 /* 12495 * Send the newconn notification to ulp. The eager is blown off if the 12496 * notification fails. 12497 */ 12498 static void 12499 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12500 { 12501 if (IPCL_IS_NONSTR(lconnp)) { 12502 cred_t *cr; 12503 pid_t cpid; 12504 12505 cr = msg_getcred(mp, &cpid); 12506 12507 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12508 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12509 lconnp->conn_tcp); 12510 12511 /* Keep the message around in case of a fallback to TPI */ 12512 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12513 12514 /* 12515 * Notify the ULP about the newconn. It is guaranteed that no 12516 * tcp_accept() call will be made for the eager if the 12517 * notification fails, so it's safe to blow it off in that 12518 * case. 12519 * 12520 * The upper handle will be assigned when tcp_accept() is 12521 * called. 12522 */ 12523 if ((*lconnp->conn_upcalls->su_newconn) 12524 (lconnp->conn_upper_handle, 12525 (sock_lower_handle_t)econnp, 12526 &sock_tcp_downcalls, cr, cpid, 12527 &econnp->conn_upcalls) == NULL) { 12528 /* Failed to allocate a socket */ 12529 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12530 tcpEstabResets); 12531 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12532 econnp->conn_tcp->tcp_conn_req_seqnum); 12533 } 12534 } else { 12535 putnext(lconnp->conn_tcp->tcp_rq, mp); 12536 } 12537 } 12538 12539 mblk_t * 12540 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12541 uint_t *ifindexp, ip6_pkt_t *ippp) 12542 { 12543 ip_pktinfo_t *pinfo; 12544 ip6_t *ip6h; 12545 uchar_t *rptr; 12546 mblk_t *first_mp = mp; 12547 boolean_t mctl_present = B_FALSE; 12548 uint_t ifindex = 0; 12549 ip6_pkt_t ipp; 12550 uint_t ipvers; 12551 uint_t ip_hdr_len; 12552 tcp_stack_t *tcps = tcp->tcp_tcps; 12553 12554 rptr = mp->b_rptr; 12555 ASSERT(OK_32PTR(rptr)); 12556 ASSERT(tcp != NULL); 12557 ipp.ipp_fields = 0; 12558 12559 switch DB_TYPE(mp) { 12560 case M_CTL: 12561 mp = mp->b_cont; 12562 if (mp == NULL) { 12563 freemsg(first_mp); 12564 return (NULL); 12565 } 12566 if (DB_TYPE(mp) != M_DATA) { 12567 freemsg(first_mp); 12568 return (NULL); 12569 } 12570 mctl_present = B_TRUE; 12571 break; 12572 case M_DATA: 12573 break; 12574 default: 12575 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12576 freemsg(mp); 12577 return (NULL); 12578 } 12579 ipvers = IPH_HDR_VERSION(rptr); 12580 if (ipvers == IPV4_VERSION) { 12581 if (tcp == NULL) { 12582 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12583 goto done; 12584 } 12585 12586 ipp.ipp_fields |= IPPF_HOPLIMIT; 12587 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12588 12589 /* 12590 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12591 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12592 */ 12593 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12594 mctl_present) { 12595 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12596 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12597 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12598 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12599 ipp.ipp_fields |= IPPF_IFINDEX; 12600 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12601 ifindex = pinfo->ip_pkt_ifindex; 12602 } 12603 freeb(first_mp); 12604 mctl_present = B_FALSE; 12605 } 12606 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12607 } else { 12608 ip6h = (ip6_t *)rptr; 12609 12610 ASSERT(ipvers == IPV6_VERSION); 12611 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12612 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12613 ipp.ipp_hoplimit = ip6h->ip6_hops; 12614 12615 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12616 uint8_t nexthdrp; 12617 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12618 12619 /* Look for ifindex information */ 12620 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12621 ip6i_t *ip6i = (ip6i_t *)ip6h; 12622 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12623 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12624 freemsg(first_mp); 12625 return (NULL); 12626 } 12627 12628 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12629 ASSERT(ip6i->ip6i_ifindex != 0); 12630 ipp.ipp_fields |= IPPF_IFINDEX; 12631 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12632 ifindex = ip6i->ip6i_ifindex; 12633 } 12634 rptr = (uchar_t *)&ip6i[1]; 12635 mp->b_rptr = rptr; 12636 if (rptr == mp->b_wptr) { 12637 mblk_t *mp1; 12638 mp1 = mp->b_cont; 12639 freeb(mp); 12640 mp = mp1; 12641 rptr = mp->b_rptr; 12642 } 12643 if (MBLKL(mp) < IPV6_HDR_LEN + 12644 sizeof (tcph_t)) { 12645 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12646 freemsg(first_mp); 12647 return (NULL); 12648 } 12649 ip6h = (ip6_t *)rptr; 12650 } 12651 12652 /* 12653 * Find any potentially interesting extension headers 12654 * as well as the length of the IPv6 + extension 12655 * headers. 12656 */ 12657 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12658 /* Verify if this is a TCP packet */ 12659 if (nexthdrp != IPPROTO_TCP) { 12660 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12661 freemsg(first_mp); 12662 return (NULL); 12663 } 12664 } else { 12665 ip_hdr_len = IPV6_HDR_LEN; 12666 } 12667 } 12668 12669 done: 12670 if (ipversp != NULL) 12671 *ipversp = ipvers; 12672 if (ip_hdr_lenp != NULL) 12673 *ip_hdr_lenp = ip_hdr_len; 12674 if (ippp != NULL) 12675 *ippp = ipp; 12676 if (ifindexp != NULL) 12677 *ifindexp = ifindex; 12678 if (mctl_present) { 12679 freeb(first_mp); 12680 } 12681 return (mp); 12682 } 12683 12684 /* 12685 * Handle M_DATA messages from IP. Its called directly from IP via 12686 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12687 * in this path. 12688 * 12689 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12690 * v4 and v6), we are called through tcp_input() and a M_CTL can 12691 * be present for options but tcp_find_pktinfo() deals with it. We 12692 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12693 * 12694 * The first argument is always the connp/tcp to which the mp belongs. 12695 * There are no exceptions to this rule. The caller has already put 12696 * a reference on this connp/tcp and once tcp_rput_data() returns, 12697 * the squeue will do the refrele. 12698 * 12699 * The TH_SYN for the listener directly go to tcp_conn_request via 12700 * squeue. 12701 * 12702 * sqp: NULL = recursive, sqp != NULL means called from squeue 12703 */ 12704 void 12705 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12706 { 12707 int32_t bytes_acked; 12708 int32_t gap; 12709 mblk_t *mp1; 12710 uint_t flags; 12711 uint32_t new_swnd = 0; 12712 uchar_t *iphdr; 12713 uchar_t *rptr; 12714 int32_t rgap; 12715 uint32_t seg_ack; 12716 int seg_len; 12717 uint_t ip_hdr_len; 12718 uint32_t seg_seq; 12719 tcph_t *tcph; 12720 int urp; 12721 tcp_opt_t tcpopt; 12722 uint_t ipvers; 12723 ip6_pkt_t ipp; 12724 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12725 uint32_t cwnd; 12726 uint32_t add; 12727 int npkt; 12728 int mss; 12729 conn_t *connp = (conn_t *)arg; 12730 squeue_t *sqp = (squeue_t *)arg2; 12731 tcp_t *tcp = connp->conn_tcp; 12732 tcp_stack_t *tcps = tcp->tcp_tcps; 12733 12734 /* 12735 * RST from fused tcp loopback peer should trigger an unfuse. 12736 */ 12737 if (tcp->tcp_fused) { 12738 TCP_STAT(tcps, tcp_fusion_aborted); 12739 tcp_unfuse(tcp); 12740 } 12741 12742 iphdr = mp->b_rptr; 12743 rptr = mp->b_rptr; 12744 ASSERT(OK_32PTR(rptr)); 12745 12746 /* 12747 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12748 * processing here. For rest call tcp_find_pktinfo to fill up the 12749 * necessary information. 12750 */ 12751 if (IPCL_IS_TCP4(connp)) { 12752 ipvers = IPV4_VERSION; 12753 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12754 } else { 12755 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12756 NULL, &ipp); 12757 if (mp == NULL) { 12758 TCP_STAT(tcps, tcp_rput_v6_error); 12759 return; 12760 } 12761 iphdr = mp->b_rptr; 12762 rptr = mp->b_rptr; 12763 } 12764 ASSERT(DB_TYPE(mp) == M_DATA); 12765 ASSERT(mp->b_next == NULL); 12766 12767 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12768 seg_seq = ABE32_TO_U32(tcph->th_seq); 12769 seg_ack = ABE32_TO_U32(tcph->th_ack); 12770 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12771 seg_len = (int)(mp->b_wptr - rptr) - 12772 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12773 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12774 do { 12775 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12776 (uintptr_t)INT_MAX); 12777 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12778 } while ((mp1 = mp1->b_cont) != NULL && 12779 mp1->b_datap->db_type == M_DATA); 12780 } 12781 12782 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12783 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12784 seg_len, tcph); 12785 return; 12786 } 12787 12788 if (sqp != NULL) { 12789 /* 12790 * This is the correct place to update tcp_last_recv_time. Note 12791 * that it is also updated for tcp structure that belongs to 12792 * global and listener queues which do not really need updating. 12793 * But that should not cause any harm. And it is updated for 12794 * all kinds of incoming segments, not only for data segments. 12795 */ 12796 tcp->tcp_last_recv_time = lbolt; 12797 } 12798 12799 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12800 12801 BUMP_LOCAL(tcp->tcp_ibsegs); 12802 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12803 12804 if ((flags & TH_URG) && sqp != NULL) { 12805 /* 12806 * TCP can't handle urgent pointers that arrive before 12807 * the connection has been accept()ed since it can't 12808 * buffer OOB data. Discard segment if this happens. 12809 * 12810 * We can't just rely on a non-null tcp_listener to indicate 12811 * that the accept() has completed since unlinking of the 12812 * eager and completion of the accept are not atomic. 12813 * tcp_detached, when it is not set (B_FALSE) indicates 12814 * that the accept() has completed. 12815 * 12816 * Nor can it reassemble urgent pointers, so discard 12817 * if it's not the next segment expected. 12818 * 12819 * Otherwise, collapse chain into one mblk (discard if 12820 * that fails). This makes sure the headers, retransmitted 12821 * data, and new data all are in the same mblk. 12822 */ 12823 ASSERT(mp != NULL); 12824 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12825 freemsg(mp); 12826 return; 12827 } 12828 /* Update pointers into message */ 12829 iphdr = rptr = mp->b_rptr; 12830 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12831 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12832 /* 12833 * Since we can't handle any data with this urgent 12834 * pointer that is out of sequence, we expunge 12835 * the data. This allows us to still register 12836 * the urgent mark and generate the M_PCSIG, 12837 * which we can do. 12838 */ 12839 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12840 seg_len = 0; 12841 } 12842 } 12843 12844 switch (tcp->tcp_state) { 12845 case TCPS_SYN_SENT: 12846 if (flags & TH_ACK) { 12847 /* 12848 * Note that our stack cannot send data before a 12849 * connection is established, therefore the 12850 * following check is valid. Otherwise, it has 12851 * to be changed. 12852 */ 12853 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12854 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12855 freemsg(mp); 12856 if (flags & TH_RST) 12857 return; 12858 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12859 tcp, seg_ack, 0, TH_RST); 12860 return; 12861 } 12862 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12863 } 12864 if (flags & TH_RST) { 12865 freemsg(mp); 12866 if (flags & TH_ACK) 12867 (void) tcp_clean_death(tcp, 12868 ECONNREFUSED, 13); 12869 return; 12870 } 12871 if (!(flags & TH_SYN)) { 12872 freemsg(mp); 12873 return; 12874 } 12875 12876 /* Process all TCP options. */ 12877 tcp_process_options(tcp, tcph); 12878 /* 12879 * The following changes our rwnd to be a multiple of the 12880 * MIN(peer MSS, our MSS) for performance reason. 12881 */ 12882 (void) tcp_rwnd_set(tcp, 12883 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12884 12885 /* Is the other end ECN capable? */ 12886 if (tcp->tcp_ecn_ok) { 12887 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12888 tcp->tcp_ecn_ok = B_FALSE; 12889 } 12890 } 12891 /* 12892 * Clear ECN flags because it may interfere with later 12893 * processing. 12894 */ 12895 flags &= ~(TH_ECE|TH_CWR); 12896 12897 tcp->tcp_irs = seg_seq; 12898 tcp->tcp_rack = seg_seq; 12899 tcp->tcp_rnxt = seg_seq + 1; 12900 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12901 if (!TCP_IS_DETACHED(tcp)) { 12902 /* Allocate room for SACK options if needed. */ 12903 if (tcp->tcp_snd_sack_ok) { 12904 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12905 tcp->tcp_hdr_len + 12906 TCPOPT_MAX_SACK_LEN + 12907 (tcp->tcp_loopback ? 0 : 12908 tcps->tcps_wroff_xtra)); 12909 } else { 12910 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12911 tcp->tcp_hdr_len + 12912 (tcp->tcp_loopback ? 0 : 12913 tcps->tcps_wroff_xtra)); 12914 } 12915 } 12916 if (flags & TH_ACK) { 12917 /* 12918 * If we can't get the confirmation upstream, pretend 12919 * we didn't even see this one. 12920 * 12921 * XXX: how can we pretend we didn't see it if we 12922 * have updated rnxt et. al. 12923 * 12924 * For loopback we defer sending up the T_CONN_CON 12925 * until after some checks below. 12926 */ 12927 mp1 = NULL; 12928 /* 12929 * tcp_sendmsg() checks tcp_state without entering 12930 * the squeue so tcp_state should be updated before 12931 * sending up connection confirmation 12932 */ 12933 tcp->tcp_state = TCPS_ESTABLISHED; 12934 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12935 tcp->tcp_loopback ? &mp1 : NULL)) { 12936 tcp->tcp_state = TCPS_SYN_SENT; 12937 freemsg(mp); 12938 return; 12939 } 12940 /* SYN was acked - making progress */ 12941 if (tcp->tcp_ipversion == IPV6_VERSION) 12942 tcp->tcp_ip_forward_progress = B_TRUE; 12943 12944 /* One for the SYN */ 12945 tcp->tcp_suna = tcp->tcp_iss + 1; 12946 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12947 12948 /* 12949 * If SYN was retransmitted, need to reset all 12950 * retransmission info. This is because this 12951 * segment will be treated as a dup ACK. 12952 */ 12953 if (tcp->tcp_rexmit) { 12954 tcp->tcp_rexmit = B_FALSE; 12955 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12956 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12957 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12958 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12959 tcp->tcp_ms_we_have_waited = 0; 12960 12961 /* 12962 * Set tcp_cwnd back to 1 MSS, per 12963 * recommendation from 12964 * draft-floyd-incr-init-win-01.txt, 12965 * Increasing TCP's Initial Window. 12966 */ 12967 tcp->tcp_cwnd = tcp->tcp_mss; 12968 } 12969 12970 tcp->tcp_swl1 = seg_seq; 12971 tcp->tcp_swl2 = seg_ack; 12972 12973 new_swnd = BE16_TO_U16(tcph->th_win); 12974 tcp->tcp_swnd = new_swnd; 12975 if (new_swnd > tcp->tcp_max_swnd) 12976 tcp->tcp_max_swnd = new_swnd; 12977 12978 /* 12979 * Always send the three-way handshake ack immediately 12980 * in order to make the connection complete as soon as 12981 * possible on the accepting host. 12982 */ 12983 flags |= TH_ACK_NEEDED; 12984 12985 /* 12986 * Special case for loopback. At this point we have 12987 * received SYN-ACK from the remote endpoint. In 12988 * order to ensure that both endpoints reach the 12989 * fused state prior to any data exchange, the final 12990 * ACK needs to be sent before we indicate T_CONN_CON 12991 * to the module upstream. 12992 */ 12993 if (tcp->tcp_loopback) { 12994 mblk_t *ack_mp; 12995 12996 ASSERT(!tcp->tcp_unfusable); 12997 ASSERT(mp1 != NULL); 12998 /* 12999 * For loopback, we always get a pure SYN-ACK 13000 * and only need to send back the final ACK 13001 * with no data (this is because the other 13002 * tcp is ours and we don't do T/TCP). This 13003 * final ACK triggers the passive side to 13004 * perform fusion in ESTABLISHED state. 13005 */ 13006 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 13007 if (tcp->tcp_ack_tid != 0) { 13008 (void) TCP_TIMER_CANCEL(tcp, 13009 tcp->tcp_ack_tid); 13010 tcp->tcp_ack_tid = 0; 13011 } 13012 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 13013 BUMP_LOCAL(tcp->tcp_obsegs); 13014 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 13015 13016 if (!IPCL_IS_NONSTR(connp)) { 13017 /* Send up T_CONN_CON */ 13018 putnext(tcp->tcp_rq, mp1); 13019 } else { 13020 cred_t *cr; 13021 pid_t cpid; 13022 13023 cr = msg_getcred(mp1, &cpid); 13024 (*connp->conn_upcalls-> 13025 su_connected) 13026 (connp->conn_upper_handle, 13027 tcp->tcp_connid, cr, cpid); 13028 freemsg(mp1); 13029 } 13030 13031 freemsg(mp); 13032 return; 13033 } 13034 /* 13035 * Forget fusion; we need to handle more 13036 * complex cases below. Send the deferred 13037 * T_CONN_CON message upstream and proceed 13038 * as usual. Mark this tcp as not capable 13039 * of fusion. 13040 */ 13041 TCP_STAT(tcps, tcp_fusion_unfusable); 13042 tcp->tcp_unfusable = B_TRUE; 13043 if (!IPCL_IS_NONSTR(connp)) { 13044 putnext(tcp->tcp_rq, mp1); 13045 } else { 13046 cred_t *cr; 13047 pid_t cpid; 13048 13049 cr = msg_getcred(mp1, &cpid); 13050 (*connp->conn_upcalls->su_connected) 13051 (connp->conn_upper_handle, 13052 tcp->tcp_connid, cr, cpid); 13053 freemsg(mp1); 13054 } 13055 } 13056 13057 /* 13058 * Check to see if there is data to be sent. If 13059 * yes, set the transmit flag. Then check to see 13060 * if received data processing needs to be done. 13061 * If not, go straight to xmit_check. This short 13062 * cut is OK as we don't support T/TCP. 13063 */ 13064 if (tcp->tcp_unsent) 13065 flags |= TH_XMIT_NEEDED; 13066 13067 if (seg_len == 0 && !(flags & TH_URG)) { 13068 freemsg(mp); 13069 goto xmit_check; 13070 } 13071 13072 flags &= ~TH_SYN; 13073 seg_seq++; 13074 break; 13075 } 13076 tcp->tcp_state = TCPS_SYN_RCVD; 13077 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 13078 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 13079 if (mp1) { 13080 /* 13081 * See comment in tcp_conn_request() for why we use 13082 * the open() time pid here. 13083 */ 13084 DB_CPID(mp1) = tcp->tcp_cpid; 13085 tcp_send_data(tcp, tcp->tcp_wq, mp1); 13086 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13087 } 13088 freemsg(mp); 13089 return; 13090 case TCPS_SYN_RCVD: 13091 if (flags & TH_ACK) { 13092 /* 13093 * In this state, a SYN|ACK packet is either bogus 13094 * because the other side must be ACKing our SYN which 13095 * indicates it has seen the ACK for their SYN and 13096 * shouldn't retransmit it or we're crossing SYNs 13097 * on active open. 13098 */ 13099 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 13100 freemsg(mp); 13101 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 13102 tcp, seg_ack, 0, TH_RST); 13103 return; 13104 } 13105 /* 13106 * NOTE: RFC 793 pg. 72 says this should be 13107 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 13108 * but that would mean we have an ack that ignored 13109 * our SYN. 13110 */ 13111 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 13112 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13113 freemsg(mp); 13114 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 13115 tcp, seg_ack, 0, TH_RST); 13116 return; 13117 } 13118 } 13119 break; 13120 case TCPS_LISTEN: 13121 /* 13122 * Only a TLI listener can come through this path when a 13123 * acceptor is going back to be a listener and a packet 13124 * for the acceptor hits the classifier. For a socket 13125 * listener, this can never happen because a listener 13126 * can never accept connection on itself and hence a 13127 * socket acceptor can not go back to being a listener. 13128 */ 13129 ASSERT(!TCP_IS_SOCKET(tcp)); 13130 /*FALLTHRU*/ 13131 case TCPS_CLOSED: 13132 case TCPS_BOUND: { 13133 conn_t *new_connp; 13134 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13135 13136 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13137 if (new_connp != NULL) { 13138 tcp_reinput(new_connp, mp, connp->conn_sqp); 13139 return; 13140 } 13141 /* We failed to classify. For now just drop the packet */ 13142 freemsg(mp); 13143 return; 13144 } 13145 case TCPS_IDLE: 13146 /* 13147 * Handle the case where the tcp_clean_death() has happened 13148 * on a connection (application hasn't closed yet) but a packet 13149 * was already queued on squeue before tcp_clean_death() 13150 * was processed. Calling tcp_clean_death() twice on same 13151 * connection can result in weird behaviour. 13152 */ 13153 freemsg(mp); 13154 return; 13155 default: 13156 break; 13157 } 13158 13159 /* 13160 * Already on the correct queue/perimeter. 13161 * If this is a detached connection and not an eager 13162 * connection hanging off a listener then new data 13163 * (past the FIN) will cause a reset. 13164 * We do a special check here where it 13165 * is out of the main line, rather than check 13166 * if we are detached every time we see new 13167 * data down below. 13168 */ 13169 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13170 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13171 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13172 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13173 13174 freemsg(mp); 13175 /* 13176 * This could be an SSL closure alert. We're detached so just 13177 * acknowledge it this last time. 13178 */ 13179 if (tcp->tcp_kssl_ctx != NULL) { 13180 kssl_release_ctx(tcp->tcp_kssl_ctx); 13181 tcp->tcp_kssl_ctx = NULL; 13182 13183 tcp->tcp_rnxt += seg_len; 13184 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13185 flags |= TH_ACK_NEEDED; 13186 goto ack_check; 13187 } 13188 13189 tcp_xmit_ctl("new data when detached", tcp, 13190 tcp->tcp_snxt, 0, TH_RST); 13191 (void) tcp_clean_death(tcp, EPROTO, 12); 13192 return; 13193 } 13194 13195 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13196 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13197 new_swnd = BE16_TO_U16(tcph->th_win) << 13198 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13199 13200 if (tcp->tcp_snd_ts_ok) { 13201 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13202 /* 13203 * This segment is not acceptable. 13204 * Drop it and send back an ACK. 13205 */ 13206 freemsg(mp); 13207 flags |= TH_ACK_NEEDED; 13208 goto ack_check; 13209 } 13210 } else if (tcp->tcp_snd_sack_ok) { 13211 ASSERT(tcp->tcp_sack_info != NULL); 13212 tcpopt.tcp = tcp; 13213 /* 13214 * SACK info in already updated in tcp_parse_options. Ignore 13215 * all other TCP options... 13216 */ 13217 (void) tcp_parse_options(tcph, &tcpopt); 13218 } 13219 try_again:; 13220 mss = tcp->tcp_mss; 13221 gap = seg_seq - tcp->tcp_rnxt; 13222 rgap = tcp->tcp_rwnd - (gap + seg_len); 13223 /* 13224 * gap is the amount of sequence space between what we expect to see 13225 * and what we got for seg_seq. A positive value for gap means 13226 * something got lost. A negative value means we got some old stuff. 13227 */ 13228 if (gap < 0) { 13229 /* Old stuff present. Is the SYN in there? */ 13230 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13231 (seg_len != 0)) { 13232 flags &= ~TH_SYN; 13233 seg_seq++; 13234 urp--; 13235 /* Recompute the gaps after noting the SYN. */ 13236 goto try_again; 13237 } 13238 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13239 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13240 (seg_len > -gap ? -gap : seg_len)); 13241 /* Remove the old stuff from seg_len. */ 13242 seg_len += gap; 13243 /* 13244 * Anything left? 13245 * Make sure to check for unack'd FIN when rest of data 13246 * has been previously ack'd. 13247 */ 13248 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13249 /* 13250 * Resets are only valid if they lie within our offered 13251 * window. If the RST bit is set, we just ignore this 13252 * segment. 13253 */ 13254 if (flags & TH_RST) { 13255 freemsg(mp); 13256 return; 13257 } 13258 13259 /* 13260 * The arriving of dup data packets indicate that we 13261 * may have postponed an ack for too long, or the other 13262 * side's RTT estimate is out of shape. Start acking 13263 * more often. 13264 */ 13265 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13266 tcp->tcp_rack_cnt >= 1 && 13267 tcp->tcp_rack_abs_max > 2) { 13268 tcp->tcp_rack_abs_max--; 13269 } 13270 tcp->tcp_rack_cur_max = 1; 13271 13272 /* 13273 * This segment is "unacceptable". None of its 13274 * sequence space lies within our advertized window. 13275 * 13276 * Adjust seg_len to the original value for tracing. 13277 */ 13278 seg_len -= gap; 13279 if (tcp->tcp_debug) { 13280 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13281 "tcp_rput: unacceptable, gap %d, rgap %d, " 13282 "flags 0x%x, seg_seq %u, seg_ack %u, " 13283 "seg_len %d, rnxt %u, snxt %u, %s", 13284 gap, rgap, flags, seg_seq, seg_ack, 13285 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13286 tcp_display(tcp, NULL, 13287 DISP_ADDR_AND_PORT)); 13288 } 13289 13290 /* 13291 * Arrange to send an ACK in response to the 13292 * unacceptable segment per RFC 793 page 69. There 13293 * is only one small difference between ours and the 13294 * acceptability test in the RFC - we accept ACK-only 13295 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13296 * will be generated. 13297 * 13298 * Note that we have to ACK an ACK-only packet at least 13299 * for stacks that send 0-length keep-alives with 13300 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13301 * section 4.2.3.6. As long as we don't ever generate 13302 * an unacceptable packet in response to an incoming 13303 * packet that is unacceptable, it should not cause 13304 * "ACK wars". 13305 */ 13306 flags |= TH_ACK_NEEDED; 13307 13308 /* 13309 * Continue processing this segment in order to use the 13310 * ACK information it contains, but skip all other 13311 * sequence-number processing. Processing the ACK 13312 * information is necessary in order to 13313 * re-synchronize connections that may have lost 13314 * synchronization. 13315 * 13316 * We clear seg_len and flag fields related to 13317 * sequence number processing as they are not 13318 * to be trusted for an unacceptable segment. 13319 */ 13320 seg_len = 0; 13321 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13322 goto process_ack; 13323 } 13324 13325 /* Fix seg_seq, and chew the gap off the front. */ 13326 seg_seq = tcp->tcp_rnxt; 13327 urp += gap; 13328 do { 13329 mblk_t *mp2; 13330 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13331 (uintptr_t)UINT_MAX); 13332 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13333 if (gap > 0) { 13334 mp->b_rptr = mp->b_wptr - gap; 13335 break; 13336 } 13337 mp2 = mp; 13338 mp = mp->b_cont; 13339 freeb(mp2); 13340 } while (gap < 0); 13341 /* 13342 * If the urgent data has already been acknowledged, we 13343 * should ignore TH_URG below 13344 */ 13345 if (urp < 0) 13346 flags &= ~TH_URG; 13347 } 13348 /* 13349 * rgap is the amount of stuff received out of window. A negative 13350 * value is the amount out of window. 13351 */ 13352 if (rgap < 0) { 13353 mblk_t *mp2; 13354 13355 if (tcp->tcp_rwnd == 0) { 13356 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13357 } else { 13358 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13359 UPDATE_MIB(&tcps->tcps_mib, 13360 tcpInDataPastWinBytes, -rgap); 13361 } 13362 13363 /* 13364 * seg_len does not include the FIN, so if more than 13365 * just the FIN is out of window, we act like we don't 13366 * see it. (If just the FIN is out of window, rgap 13367 * will be zero and we will go ahead and acknowledge 13368 * the FIN.) 13369 */ 13370 flags &= ~TH_FIN; 13371 13372 /* Fix seg_len and make sure there is something left. */ 13373 seg_len += rgap; 13374 if (seg_len <= 0) { 13375 /* 13376 * Resets are only valid if they lie within our offered 13377 * window. If the RST bit is set, we just ignore this 13378 * segment. 13379 */ 13380 if (flags & TH_RST) { 13381 freemsg(mp); 13382 return; 13383 } 13384 13385 /* Per RFC 793, we need to send back an ACK. */ 13386 flags |= TH_ACK_NEEDED; 13387 13388 /* 13389 * Send SIGURG as soon as possible i.e. even 13390 * if the TH_URG was delivered in a window probe 13391 * packet (which will be unacceptable). 13392 * 13393 * We generate a signal if none has been generated 13394 * for this connection or if this is a new urgent 13395 * byte. Also send a zero-length "unmarked" message 13396 * to inform SIOCATMARK that this is not the mark. 13397 * 13398 * tcp_urp_last_valid is cleared when the T_exdata_ind 13399 * is sent up. This plus the check for old data 13400 * (gap >= 0) handles the wraparound of the sequence 13401 * number space without having to always track the 13402 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13403 * this max in its rcv_up variable). 13404 * 13405 * This prevents duplicate SIGURGS due to a "late" 13406 * zero-window probe when the T_EXDATA_IND has already 13407 * been sent up. 13408 */ 13409 if ((flags & TH_URG) && 13410 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13411 tcp->tcp_urp_last))) { 13412 if (IPCL_IS_NONSTR(connp)) { 13413 if (!TCP_IS_DETACHED(tcp)) { 13414 (*connp->conn_upcalls-> 13415 su_signal_oob) 13416 (connp->conn_upper_handle, 13417 urp); 13418 } 13419 } else { 13420 mp1 = allocb(0, BPRI_MED); 13421 if (mp1 == NULL) { 13422 freemsg(mp); 13423 return; 13424 } 13425 if (!TCP_IS_DETACHED(tcp) && 13426 !putnextctl1(tcp->tcp_rq, 13427 M_PCSIG, SIGURG)) { 13428 /* Try again on the rexmit. */ 13429 freemsg(mp1); 13430 freemsg(mp); 13431 return; 13432 } 13433 /* 13434 * If the next byte would be the mark 13435 * then mark with MARKNEXT else mark 13436 * with NOTMARKNEXT. 13437 */ 13438 if (gap == 0 && urp == 0) 13439 mp1->b_flag |= MSGMARKNEXT; 13440 else 13441 mp1->b_flag |= MSGNOTMARKNEXT; 13442 freemsg(tcp->tcp_urp_mark_mp); 13443 tcp->tcp_urp_mark_mp = mp1; 13444 flags |= TH_SEND_URP_MARK; 13445 } 13446 tcp->tcp_urp_last_valid = B_TRUE; 13447 tcp->tcp_urp_last = urp + seg_seq; 13448 } 13449 /* 13450 * If this is a zero window probe, continue to 13451 * process the ACK part. But we need to set seg_len 13452 * to 0 to avoid data processing. Otherwise just 13453 * drop the segment and send back an ACK. 13454 */ 13455 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13456 flags &= ~(TH_SYN | TH_URG); 13457 seg_len = 0; 13458 goto process_ack; 13459 } else { 13460 freemsg(mp); 13461 goto ack_check; 13462 } 13463 } 13464 /* Pitch out of window stuff off the end. */ 13465 rgap = seg_len; 13466 mp2 = mp; 13467 do { 13468 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13469 (uintptr_t)INT_MAX); 13470 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13471 if (rgap < 0) { 13472 mp2->b_wptr += rgap; 13473 if ((mp1 = mp2->b_cont) != NULL) { 13474 mp2->b_cont = NULL; 13475 freemsg(mp1); 13476 } 13477 break; 13478 } 13479 } while ((mp2 = mp2->b_cont) != NULL); 13480 } 13481 ok:; 13482 /* 13483 * TCP should check ECN info for segments inside the window only. 13484 * Therefore the check should be done here. 13485 */ 13486 if (tcp->tcp_ecn_ok) { 13487 if (flags & TH_CWR) { 13488 tcp->tcp_ecn_echo_on = B_FALSE; 13489 } 13490 /* 13491 * Note that both ECN_CE and CWR can be set in the 13492 * same segment. In this case, we once again turn 13493 * on ECN_ECHO. 13494 */ 13495 if (tcp->tcp_ipversion == IPV4_VERSION) { 13496 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13497 13498 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13499 tcp->tcp_ecn_echo_on = B_TRUE; 13500 } 13501 } else { 13502 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13503 13504 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13505 htonl(IPH_ECN_CE << 20)) { 13506 tcp->tcp_ecn_echo_on = B_TRUE; 13507 } 13508 } 13509 } 13510 13511 /* 13512 * Check whether we can update tcp_ts_recent. This test is 13513 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13514 * Extensions for High Performance: An Update", Internet Draft. 13515 */ 13516 if (tcp->tcp_snd_ts_ok && 13517 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13518 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13519 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13520 tcp->tcp_last_rcv_lbolt = lbolt64; 13521 } 13522 13523 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13524 /* 13525 * FIN in an out of order segment. We record this in 13526 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13527 * Clear the FIN so that any check on FIN flag will fail. 13528 * Remember that FIN also counts in the sequence number 13529 * space. So we need to ack out of order FIN only segments. 13530 */ 13531 if (flags & TH_FIN) { 13532 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13533 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13534 flags &= ~TH_FIN; 13535 flags |= TH_ACK_NEEDED; 13536 } 13537 if (seg_len > 0) { 13538 /* Fill in the SACK blk list. */ 13539 if (tcp->tcp_snd_sack_ok) { 13540 ASSERT(tcp->tcp_sack_info != NULL); 13541 tcp_sack_insert(tcp->tcp_sack_list, 13542 seg_seq, seg_seq + seg_len, 13543 &(tcp->tcp_num_sack_blk)); 13544 } 13545 13546 /* 13547 * Attempt reassembly and see if we have something 13548 * ready to go. 13549 */ 13550 mp = tcp_reass(tcp, mp, seg_seq); 13551 /* Always ack out of order packets */ 13552 flags |= TH_ACK_NEEDED | TH_PUSH; 13553 if (mp) { 13554 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13555 (uintptr_t)INT_MAX); 13556 seg_len = mp->b_cont ? msgdsize(mp) : 13557 (int)(mp->b_wptr - mp->b_rptr); 13558 seg_seq = tcp->tcp_rnxt; 13559 /* 13560 * A gap is filled and the seq num and len 13561 * of the gap match that of a previously 13562 * received FIN, put the FIN flag back in. 13563 */ 13564 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13565 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13566 flags |= TH_FIN; 13567 tcp->tcp_valid_bits &= 13568 ~TCP_OFO_FIN_VALID; 13569 } 13570 } else { 13571 /* 13572 * Keep going even with NULL mp. 13573 * There may be a useful ACK or something else 13574 * we don't want to miss. 13575 * 13576 * But TCP should not perform fast retransmit 13577 * because of the ack number. TCP uses 13578 * seg_len == 0 to determine if it is a pure 13579 * ACK. And this is not a pure ACK. 13580 */ 13581 seg_len = 0; 13582 ofo_seg = B_TRUE; 13583 } 13584 } 13585 } else if (seg_len > 0) { 13586 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13587 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13588 /* 13589 * If an out of order FIN was received before, and the seq 13590 * num and len of the new segment match that of the FIN, 13591 * put the FIN flag back in. 13592 */ 13593 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13594 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13595 flags |= TH_FIN; 13596 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13597 } 13598 } 13599 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13600 if (flags & TH_RST) { 13601 freemsg(mp); 13602 switch (tcp->tcp_state) { 13603 case TCPS_SYN_RCVD: 13604 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13605 break; 13606 case TCPS_ESTABLISHED: 13607 case TCPS_FIN_WAIT_1: 13608 case TCPS_FIN_WAIT_2: 13609 case TCPS_CLOSE_WAIT: 13610 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13611 break; 13612 case TCPS_CLOSING: 13613 case TCPS_LAST_ACK: 13614 (void) tcp_clean_death(tcp, 0, 16); 13615 break; 13616 default: 13617 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13618 (void) tcp_clean_death(tcp, ENXIO, 17); 13619 break; 13620 } 13621 return; 13622 } 13623 if (flags & TH_SYN) { 13624 /* 13625 * See RFC 793, Page 71 13626 * 13627 * The seq number must be in the window as it should 13628 * be "fixed" above. If it is outside window, it should 13629 * be already rejected. Note that we allow seg_seq to be 13630 * rnxt + rwnd because we want to accept 0 window probe. 13631 */ 13632 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13633 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13634 freemsg(mp); 13635 /* 13636 * If the ACK flag is not set, just use our snxt as the 13637 * seq number of the RST segment. 13638 */ 13639 if (!(flags & TH_ACK)) { 13640 seg_ack = tcp->tcp_snxt; 13641 } 13642 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13643 TH_RST|TH_ACK); 13644 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13645 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13646 return; 13647 } 13648 /* 13649 * urp could be -1 when the urp field in the packet is 0 13650 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13651 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13652 */ 13653 if (flags & TH_URG && urp >= 0) { 13654 if (!tcp->tcp_urp_last_valid || 13655 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13656 if (IPCL_IS_NONSTR(connp)) { 13657 if (!TCP_IS_DETACHED(tcp)) { 13658 (*connp->conn_upcalls->su_signal_oob) 13659 (connp->conn_upper_handle, urp); 13660 } 13661 } else { 13662 /* 13663 * If we haven't generated the signal yet for 13664 * this urgent pointer value, do it now. Also, 13665 * send up a zero-length M_DATA indicating 13666 * whether or not this is the mark. The latter 13667 * is not needed when a T_EXDATA_IND is sent up. 13668 * However, if there are allocation failures 13669 * this code relies on the sender retransmitting 13670 * and the socket code for determining the mark 13671 * should not block waiting for the peer to 13672 * transmit. Thus, for simplicity we always 13673 * send up the mark indication. 13674 */ 13675 mp1 = allocb(0, BPRI_MED); 13676 if (mp1 == NULL) { 13677 freemsg(mp); 13678 return; 13679 } 13680 if (!TCP_IS_DETACHED(tcp) && 13681 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13682 SIGURG)) { 13683 /* Try again on the rexmit. */ 13684 freemsg(mp1); 13685 freemsg(mp); 13686 return; 13687 } 13688 /* 13689 * Mark with NOTMARKNEXT for now. 13690 * The code below will change this to MARKNEXT 13691 * if we are at the mark. 13692 * 13693 * If there are allocation failures (e.g. in 13694 * dupmsg below) the next time tcp_rput_data 13695 * sees the urgent segment it will send up the 13696 * MSGMARKNEXT message. 13697 */ 13698 mp1->b_flag |= MSGNOTMARKNEXT; 13699 freemsg(tcp->tcp_urp_mark_mp); 13700 tcp->tcp_urp_mark_mp = mp1; 13701 flags |= TH_SEND_URP_MARK; 13702 #ifdef DEBUG 13703 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13704 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13705 "last %x, %s", 13706 seg_seq, urp, tcp->tcp_urp_last, 13707 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13708 #endif /* DEBUG */ 13709 } 13710 tcp->tcp_urp_last_valid = B_TRUE; 13711 tcp->tcp_urp_last = urp + seg_seq; 13712 } else if (tcp->tcp_urp_mark_mp != NULL) { 13713 /* 13714 * An allocation failure prevented the previous 13715 * tcp_rput_data from sending up the allocated 13716 * MSG*MARKNEXT message - send it up this time 13717 * around. 13718 */ 13719 flags |= TH_SEND_URP_MARK; 13720 } 13721 13722 /* 13723 * If the urgent byte is in this segment, make sure that it is 13724 * all by itself. This makes it much easier to deal with the 13725 * possibility of an allocation failure on the T_exdata_ind. 13726 * Note that seg_len is the number of bytes in the segment, and 13727 * urp is the offset into the segment of the urgent byte. 13728 * urp < seg_len means that the urgent byte is in this segment. 13729 */ 13730 if (urp < seg_len) { 13731 if (seg_len != 1) { 13732 uint32_t tmp_rnxt; 13733 /* 13734 * Break it up and feed it back in. 13735 * Re-attach the IP header. 13736 */ 13737 mp->b_rptr = iphdr; 13738 if (urp > 0) { 13739 /* 13740 * There is stuff before the urgent 13741 * byte. 13742 */ 13743 mp1 = dupmsg(mp); 13744 if (!mp1) { 13745 /* 13746 * Trim from urgent byte on. 13747 * The rest will come back. 13748 */ 13749 (void) adjmsg(mp, 13750 urp - seg_len); 13751 tcp_rput_data(connp, 13752 mp, NULL); 13753 return; 13754 } 13755 (void) adjmsg(mp1, urp - seg_len); 13756 /* Feed this piece back in. */ 13757 tmp_rnxt = tcp->tcp_rnxt; 13758 tcp_rput_data(connp, mp1, NULL); 13759 /* 13760 * If the data passed back in was not 13761 * processed (ie: bad ACK) sending 13762 * the remainder back in will cause a 13763 * loop. In this case, drop the 13764 * packet and let the sender try 13765 * sending a good packet. 13766 */ 13767 if (tmp_rnxt == tcp->tcp_rnxt) { 13768 freemsg(mp); 13769 return; 13770 } 13771 } 13772 if (urp != seg_len - 1) { 13773 uint32_t tmp_rnxt; 13774 /* 13775 * There is stuff after the urgent 13776 * byte. 13777 */ 13778 mp1 = dupmsg(mp); 13779 if (!mp1) { 13780 /* 13781 * Trim everything beyond the 13782 * urgent byte. The rest will 13783 * come back. 13784 */ 13785 (void) adjmsg(mp, 13786 urp + 1 - seg_len); 13787 tcp_rput_data(connp, 13788 mp, NULL); 13789 return; 13790 } 13791 (void) adjmsg(mp1, urp + 1 - seg_len); 13792 tmp_rnxt = tcp->tcp_rnxt; 13793 tcp_rput_data(connp, mp1, NULL); 13794 /* 13795 * If the data passed back in was not 13796 * processed (ie: bad ACK) sending 13797 * the remainder back in will cause a 13798 * loop. In this case, drop the 13799 * packet and let the sender try 13800 * sending a good packet. 13801 */ 13802 if (tmp_rnxt == tcp->tcp_rnxt) { 13803 freemsg(mp); 13804 return; 13805 } 13806 } 13807 tcp_rput_data(connp, mp, NULL); 13808 return; 13809 } 13810 /* 13811 * This segment contains only the urgent byte. We 13812 * have to allocate the T_exdata_ind, if we can. 13813 */ 13814 if (IPCL_IS_NONSTR(connp)) { 13815 int error; 13816 13817 (*connp->conn_upcalls->su_recv) 13818 (connp->conn_upper_handle, mp, seg_len, 13819 MSG_OOB, &error, NULL); 13820 /* 13821 * We should never be in middle of a 13822 * fallback, the squeue guarantees that. 13823 */ 13824 ASSERT(error != EOPNOTSUPP); 13825 mp = NULL; 13826 goto update_ack; 13827 } else if (!tcp->tcp_urp_mp) { 13828 struct T_exdata_ind *tei; 13829 mp1 = allocb(sizeof (struct T_exdata_ind), 13830 BPRI_MED); 13831 if (!mp1) { 13832 /* 13833 * Sigh... It'll be back. 13834 * Generate any MSG*MARK message now. 13835 */ 13836 freemsg(mp); 13837 seg_len = 0; 13838 if (flags & TH_SEND_URP_MARK) { 13839 13840 13841 ASSERT(tcp->tcp_urp_mark_mp); 13842 tcp->tcp_urp_mark_mp->b_flag &= 13843 ~MSGNOTMARKNEXT; 13844 tcp->tcp_urp_mark_mp->b_flag |= 13845 MSGMARKNEXT; 13846 } 13847 goto ack_check; 13848 } 13849 mp1->b_datap->db_type = M_PROTO; 13850 tei = (struct T_exdata_ind *)mp1->b_rptr; 13851 tei->PRIM_type = T_EXDATA_IND; 13852 tei->MORE_flag = 0; 13853 mp1->b_wptr = (uchar_t *)&tei[1]; 13854 tcp->tcp_urp_mp = mp1; 13855 #ifdef DEBUG 13856 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13857 "tcp_rput: allocated exdata_ind %s", 13858 tcp_display(tcp, NULL, 13859 DISP_PORT_ONLY)); 13860 #endif /* DEBUG */ 13861 /* 13862 * There is no need to send a separate MSG*MARK 13863 * message since the T_EXDATA_IND will be sent 13864 * now. 13865 */ 13866 flags &= ~TH_SEND_URP_MARK; 13867 freemsg(tcp->tcp_urp_mark_mp); 13868 tcp->tcp_urp_mark_mp = NULL; 13869 } 13870 /* 13871 * Now we are all set. On the next putnext upstream, 13872 * tcp_urp_mp will be non-NULL and will get prepended 13873 * to what has to be this piece containing the urgent 13874 * byte. If for any reason we abort this segment below, 13875 * if it comes back, we will have this ready, or it 13876 * will get blown off in close. 13877 */ 13878 } else if (urp == seg_len) { 13879 /* 13880 * The urgent byte is the next byte after this sequence 13881 * number. If there is data it is marked with 13882 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13883 * since it is not needed. Otherwise, if the code 13884 * above just allocated a zero-length tcp_urp_mark_mp 13885 * message, that message is tagged with MSGMARKNEXT. 13886 * Sending up these MSGMARKNEXT messages makes 13887 * SIOCATMARK work correctly even though 13888 * the T_EXDATA_IND will not be sent up until the 13889 * urgent byte arrives. 13890 */ 13891 if (seg_len != 0) { 13892 flags |= TH_MARKNEXT_NEEDED; 13893 freemsg(tcp->tcp_urp_mark_mp); 13894 tcp->tcp_urp_mark_mp = NULL; 13895 flags &= ~TH_SEND_URP_MARK; 13896 } else if (tcp->tcp_urp_mark_mp != NULL) { 13897 flags |= TH_SEND_URP_MARK; 13898 tcp->tcp_urp_mark_mp->b_flag &= 13899 ~MSGNOTMARKNEXT; 13900 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13901 } 13902 #ifdef DEBUG 13903 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13904 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13905 seg_len, flags, 13906 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13907 #endif /* DEBUG */ 13908 } 13909 #ifdef DEBUG 13910 else { 13911 /* Data left until we hit mark */ 13912 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13913 "tcp_rput: URP %d bytes left, %s", 13914 urp - seg_len, tcp_display(tcp, NULL, 13915 DISP_PORT_ONLY)); 13916 } 13917 #endif /* DEBUG */ 13918 } 13919 13920 process_ack: 13921 if (!(flags & TH_ACK)) { 13922 freemsg(mp); 13923 goto xmit_check; 13924 } 13925 } 13926 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13927 13928 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13929 tcp->tcp_ip_forward_progress = B_TRUE; 13930 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13931 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13932 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13933 /* 3-way handshake complete - pass up the T_CONN_IND */ 13934 tcp_t *listener = tcp->tcp_listener; 13935 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13936 13937 tcp->tcp_tconnind_started = B_TRUE; 13938 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13939 /* 13940 * We are here means eager is fine but it can 13941 * get a TH_RST at any point between now and till 13942 * accept completes and disappear. We need to 13943 * ensure that reference to eager is valid after 13944 * we get out of eager's perimeter. So we do 13945 * an extra refhold. 13946 */ 13947 CONN_INC_REF(connp); 13948 13949 /* 13950 * The listener also exists because of the refhold 13951 * done in tcp_conn_request. Its possible that it 13952 * might have closed. We will check that once we 13953 * get inside listeners context. 13954 */ 13955 CONN_INC_REF(listener->tcp_connp); 13956 if (listener->tcp_connp->conn_sqp == 13957 connp->conn_sqp) { 13958 /* 13959 * We optimize by not calling an SQUEUE_ENTER 13960 * on the listener since we know that the 13961 * listener and eager squeues are the same. 13962 * We are able to make this check safely only 13963 * because neither the eager nor the listener 13964 * can change its squeue. Only an active connect 13965 * can change its squeue 13966 */ 13967 tcp_send_conn_ind(listener->tcp_connp, mp, 13968 listener->tcp_connp->conn_sqp); 13969 CONN_DEC_REF(listener->tcp_connp); 13970 } else if (!tcp->tcp_loopback) { 13971 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13972 mp, tcp_send_conn_ind, 13973 listener->tcp_connp, SQ_FILL, 13974 SQTAG_TCP_CONN_IND); 13975 } else { 13976 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13977 mp, tcp_send_conn_ind, 13978 listener->tcp_connp, SQ_PROCESS, 13979 SQTAG_TCP_CONN_IND); 13980 } 13981 } 13982 13983 /* 13984 * We are seeing the final ack in the three way 13985 * hand shake of a active open'ed connection 13986 * so we must send up a T_CONN_CON 13987 * 13988 * tcp_sendmsg() checks tcp_state without entering 13989 * the squeue so tcp_state should be updated before 13990 * sending up connection confirmation. 13991 */ 13992 tcp->tcp_state = TCPS_ESTABLISHED; 13993 if (tcp->tcp_active_open) { 13994 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13995 freemsg(mp); 13996 tcp->tcp_state = TCPS_SYN_RCVD; 13997 return; 13998 } 13999 /* 14000 * Don't fuse the loopback endpoints for 14001 * simultaneous active opens. 14002 */ 14003 if (tcp->tcp_loopback) { 14004 TCP_STAT(tcps, tcp_fusion_unfusable); 14005 tcp->tcp_unfusable = B_TRUE; 14006 } 14007 } 14008 14009 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 14010 bytes_acked--; 14011 /* SYN was acked - making progress */ 14012 if (tcp->tcp_ipversion == IPV6_VERSION) 14013 tcp->tcp_ip_forward_progress = B_TRUE; 14014 14015 /* 14016 * If SYN was retransmitted, need to reset all 14017 * retransmission info as this segment will be 14018 * treated as a dup ACK. 14019 */ 14020 if (tcp->tcp_rexmit) { 14021 tcp->tcp_rexmit = B_FALSE; 14022 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14023 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14024 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14025 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14026 tcp->tcp_ms_we_have_waited = 0; 14027 tcp->tcp_cwnd = mss; 14028 } 14029 14030 /* 14031 * We set the send window to zero here. 14032 * This is needed if there is data to be 14033 * processed already on the queue. 14034 * Later (at swnd_update label), the 14035 * "new_swnd > tcp_swnd" condition is satisfied 14036 * the XMIT_NEEDED flag is set in the current 14037 * (SYN_RCVD) state. This ensures tcp_wput_data() is 14038 * called if there is already data on queue in 14039 * this state. 14040 */ 14041 tcp->tcp_swnd = 0; 14042 14043 if (new_swnd > tcp->tcp_max_swnd) 14044 tcp->tcp_max_swnd = new_swnd; 14045 tcp->tcp_swl1 = seg_seq; 14046 tcp->tcp_swl2 = seg_ack; 14047 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 14048 14049 /* Fuse when both sides are in ESTABLISHED state */ 14050 if (tcp->tcp_loopback && do_tcp_fusion) 14051 tcp_fuse(tcp, iphdr, tcph); 14052 14053 } 14054 /* This code follows 4.4BSD-Lite2 mostly. */ 14055 if (bytes_acked < 0) 14056 goto est; 14057 14058 /* 14059 * If TCP is ECN capable and the congestion experience bit is 14060 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 14061 * done once per window (or more loosely, per RTT). 14062 */ 14063 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 14064 tcp->tcp_cwr = B_FALSE; 14065 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 14066 if (!tcp->tcp_cwr) { 14067 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 14068 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 14069 tcp->tcp_cwnd = npkt * mss; 14070 /* 14071 * If the cwnd is 0, use the timer to clock out 14072 * new segments. This is required by the ECN spec. 14073 */ 14074 if (npkt == 0) { 14075 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14076 /* 14077 * This makes sure that when the ACK comes 14078 * back, we will increase tcp_cwnd by 1 MSS. 14079 */ 14080 tcp->tcp_cwnd_cnt = 0; 14081 } 14082 tcp->tcp_cwr = B_TRUE; 14083 /* 14084 * This marks the end of the current window of in 14085 * flight data. That is why we don't use 14086 * tcp_suna + tcp_swnd. Only data in flight can 14087 * provide ECN info. 14088 */ 14089 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14090 tcp->tcp_ecn_cwr_sent = B_FALSE; 14091 } 14092 } 14093 14094 mp1 = tcp->tcp_xmit_head; 14095 if (bytes_acked == 0) { 14096 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 14097 int dupack_cnt; 14098 14099 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 14100 /* 14101 * Fast retransmit. When we have seen exactly three 14102 * identical ACKs while we have unacked data 14103 * outstanding we take it as a hint that our peer 14104 * dropped something. 14105 * 14106 * If TCP is retransmitting, don't do fast retransmit. 14107 */ 14108 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 14109 ! tcp->tcp_rexmit) { 14110 /* Do Limited Transmit */ 14111 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 14112 tcps->tcps_dupack_fast_retransmit) { 14113 /* 14114 * RFC 3042 14115 * 14116 * What we need to do is temporarily 14117 * increase tcp_cwnd so that new 14118 * data can be sent if it is allowed 14119 * by the receive window (tcp_rwnd). 14120 * tcp_wput_data() will take care of 14121 * the rest. 14122 * 14123 * If the connection is SACK capable, 14124 * only do limited xmit when there 14125 * is SACK info. 14126 * 14127 * Note how tcp_cwnd is incremented. 14128 * The first dup ACK will increase 14129 * it by 1 MSS. The second dup ACK 14130 * will increase it by 2 MSS. This 14131 * means that only 1 new segment will 14132 * be sent for each dup ACK. 14133 */ 14134 if (tcp->tcp_unsent > 0 && 14135 (!tcp->tcp_snd_sack_ok || 14136 (tcp->tcp_snd_sack_ok && 14137 tcp->tcp_notsack_list != NULL))) { 14138 tcp->tcp_cwnd += mss << 14139 (tcp->tcp_dupack_cnt - 1); 14140 flags |= TH_LIMIT_XMIT; 14141 } 14142 } else if (dupack_cnt == 14143 tcps->tcps_dupack_fast_retransmit) { 14144 14145 /* 14146 * If we have reduced tcp_ssthresh 14147 * because of ECN, do not reduce it again 14148 * unless it is already one window of data 14149 * away. After one window of data, tcp_cwr 14150 * should then be cleared. Note that 14151 * for non ECN capable connection, tcp_cwr 14152 * should always be false. 14153 * 14154 * Adjust cwnd since the duplicate 14155 * ack indicates that a packet was 14156 * dropped (due to congestion.) 14157 */ 14158 if (!tcp->tcp_cwr) { 14159 npkt = ((tcp->tcp_snxt - 14160 tcp->tcp_suna) >> 1) / mss; 14161 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14162 mss; 14163 tcp->tcp_cwnd = (npkt + 14164 tcp->tcp_dupack_cnt) * mss; 14165 } 14166 if (tcp->tcp_ecn_ok) { 14167 tcp->tcp_cwr = B_TRUE; 14168 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14169 tcp->tcp_ecn_cwr_sent = B_FALSE; 14170 } 14171 14172 /* 14173 * We do Hoe's algorithm. Refer to her 14174 * paper "Improving the Start-up Behavior 14175 * of a Congestion Control Scheme for TCP," 14176 * appeared in SIGCOMM'96. 14177 * 14178 * Save highest seq no we have sent so far. 14179 * Be careful about the invisible FIN byte. 14180 */ 14181 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14182 (tcp->tcp_unsent == 0)) { 14183 tcp->tcp_rexmit_max = tcp->tcp_fss; 14184 } else { 14185 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14186 } 14187 14188 /* 14189 * Do not allow bursty traffic during. 14190 * fast recovery. Refer to Fall and Floyd's 14191 * paper "Simulation-based Comparisons of 14192 * Tahoe, Reno and SACK TCP" (in CCR?) 14193 * This is a best current practise. 14194 */ 14195 tcp->tcp_snd_burst = TCP_CWND_SS; 14196 14197 /* 14198 * For SACK: 14199 * Calculate tcp_pipe, which is the 14200 * estimated number of bytes in 14201 * network. 14202 * 14203 * tcp_fack is the highest sack'ed seq num 14204 * TCP has received. 14205 * 14206 * tcp_pipe is explained in the above quoted 14207 * Fall and Floyd's paper. tcp_fack is 14208 * explained in Mathis and Mahdavi's 14209 * "Forward Acknowledgment: Refining TCP 14210 * Congestion Control" in SIGCOMM '96. 14211 */ 14212 if (tcp->tcp_snd_sack_ok) { 14213 ASSERT(tcp->tcp_sack_info != NULL); 14214 if (tcp->tcp_notsack_list != NULL) { 14215 tcp->tcp_pipe = tcp->tcp_snxt - 14216 tcp->tcp_fack; 14217 tcp->tcp_sack_snxt = seg_ack; 14218 flags |= TH_NEED_SACK_REXMIT; 14219 } else { 14220 /* 14221 * Always initialize tcp_pipe 14222 * even though we don't have 14223 * any SACK info. If later 14224 * we get SACK info and 14225 * tcp_pipe is not initialized, 14226 * funny things will happen. 14227 */ 14228 tcp->tcp_pipe = 14229 tcp->tcp_cwnd_ssthresh; 14230 } 14231 } else { 14232 flags |= TH_REXMIT_NEEDED; 14233 } /* tcp_snd_sack_ok */ 14234 14235 } else { 14236 /* 14237 * Here we perform congestion 14238 * avoidance, but NOT slow start. 14239 * This is known as the Fast 14240 * Recovery Algorithm. 14241 */ 14242 if (tcp->tcp_snd_sack_ok && 14243 tcp->tcp_notsack_list != NULL) { 14244 flags |= TH_NEED_SACK_REXMIT; 14245 tcp->tcp_pipe -= mss; 14246 if (tcp->tcp_pipe < 0) 14247 tcp->tcp_pipe = 0; 14248 } else { 14249 /* 14250 * We know that one more packet has 14251 * left the pipe thus we can update 14252 * cwnd. 14253 */ 14254 cwnd = tcp->tcp_cwnd + mss; 14255 if (cwnd > tcp->tcp_cwnd_max) 14256 cwnd = tcp->tcp_cwnd_max; 14257 tcp->tcp_cwnd = cwnd; 14258 if (tcp->tcp_unsent > 0) 14259 flags |= TH_XMIT_NEEDED; 14260 } 14261 } 14262 } 14263 } else if (tcp->tcp_zero_win_probe) { 14264 /* 14265 * If the window has opened, need to arrange 14266 * to send additional data. 14267 */ 14268 if (new_swnd != 0) { 14269 /* tcp_suna != tcp_snxt */ 14270 /* Packet contains a window update */ 14271 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14272 tcp->tcp_zero_win_probe = 0; 14273 tcp->tcp_timer_backoff = 0; 14274 tcp->tcp_ms_we_have_waited = 0; 14275 14276 /* 14277 * Transmit starting with tcp_suna since 14278 * the one byte probe is not ack'ed. 14279 * If TCP has sent more than one identical 14280 * probe, tcp_rexmit will be set. That means 14281 * tcp_ss_rexmit() will send out the one 14282 * byte along with new data. Otherwise, 14283 * fake the retransmission. 14284 */ 14285 flags |= TH_XMIT_NEEDED; 14286 if (!tcp->tcp_rexmit) { 14287 tcp->tcp_rexmit = B_TRUE; 14288 tcp->tcp_dupack_cnt = 0; 14289 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14290 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14291 } 14292 } 14293 } 14294 goto swnd_update; 14295 } 14296 14297 /* 14298 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14299 * If the ACK value acks something that we have not yet sent, it might 14300 * be an old duplicate segment. Send an ACK to re-synchronize the 14301 * other side. 14302 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14303 * state is handled above, so we can always just drop the segment and 14304 * send an ACK here. 14305 * 14306 * In the case where the peer shrinks the window, we see the new window 14307 * update, but all the data sent previously is queued up by the peer. 14308 * To account for this, in tcp_process_shrunk_swnd(), the sequence 14309 * number, which was already sent, and within window, is recorded. 14310 * tcp_snxt is then updated. 14311 * 14312 * If the window has previously shrunk, and an ACK for data not yet 14313 * sent, according to tcp_snxt is recieved, it may still be valid. If 14314 * the ACK is for data within the window at the time the window was 14315 * shrunk, then the ACK is acceptable. In this case tcp_snxt is set to 14316 * the sequence number ACK'ed. 14317 * 14318 * If the ACK covers all the data sent at the time the window was 14319 * shrunk, we can now set tcp_is_wnd_shrnk to B_FALSE. 14320 * 14321 * Should we send ACKs in response to ACK only segments? 14322 */ 14323 14324 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14325 if ((tcp->tcp_is_wnd_shrnk) && 14326 (SEQ_LEQ(seg_ack, tcp->tcp_snxt_shrunk))) { 14327 uint32_t data_acked_ahead_snxt; 14328 14329 data_acked_ahead_snxt = seg_ack - tcp->tcp_snxt; 14330 tcp_update_xmit_tail(tcp, seg_ack); 14331 tcp->tcp_unsent -= data_acked_ahead_snxt; 14332 } else { 14333 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14334 /* drop the received segment */ 14335 freemsg(mp); 14336 14337 /* 14338 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14339 * greater than 0, check if the number of such 14340 * bogus ACks is greater than that count. If yes, 14341 * don't send back any ACK. This prevents TCP from 14342 * getting into an ACK storm if somehow an attacker 14343 * successfully spoofs an acceptable segment to our 14344 * peer. 14345 */ 14346 if (tcp_drop_ack_unsent_cnt > 0 && 14347 ++tcp->tcp_in_ack_unsent > 14348 tcp_drop_ack_unsent_cnt) { 14349 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14350 return; 14351 } 14352 mp = tcp_ack_mp(tcp); 14353 if (mp != NULL) { 14354 BUMP_LOCAL(tcp->tcp_obsegs); 14355 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14356 tcp_send_data(tcp, tcp->tcp_wq, mp); 14357 } 14358 return; 14359 } 14360 } else if (tcp->tcp_is_wnd_shrnk && SEQ_GEQ(seg_ack, 14361 tcp->tcp_snxt_shrunk)) { 14362 tcp->tcp_is_wnd_shrnk = B_FALSE; 14363 } 14364 14365 /* 14366 * TCP gets a new ACK, update the notsack'ed list to delete those 14367 * blocks that are covered by this ACK. 14368 */ 14369 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14370 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14371 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14372 } 14373 14374 /* 14375 * If we got an ACK after fast retransmit, check to see 14376 * if it is a partial ACK. If it is not and the congestion 14377 * window was inflated to account for the other side's 14378 * cached packets, retract it. If it is, do Hoe's algorithm. 14379 */ 14380 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14381 ASSERT(tcp->tcp_rexmit == B_FALSE); 14382 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14383 tcp->tcp_dupack_cnt = 0; 14384 /* 14385 * Restore the orig tcp_cwnd_ssthresh after 14386 * fast retransmit phase. 14387 */ 14388 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14389 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14390 } 14391 tcp->tcp_rexmit_max = seg_ack; 14392 tcp->tcp_cwnd_cnt = 0; 14393 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14394 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14395 14396 /* 14397 * Remove all notsack info to avoid confusion with 14398 * the next fast retrasnmit/recovery phase. 14399 */ 14400 if (tcp->tcp_snd_sack_ok && 14401 tcp->tcp_notsack_list != NULL) { 14402 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, 14403 tcp); 14404 } 14405 } else { 14406 if (tcp->tcp_snd_sack_ok && 14407 tcp->tcp_notsack_list != NULL) { 14408 flags |= TH_NEED_SACK_REXMIT; 14409 tcp->tcp_pipe -= mss; 14410 if (tcp->tcp_pipe < 0) 14411 tcp->tcp_pipe = 0; 14412 } else { 14413 /* 14414 * Hoe's algorithm: 14415 * 14416 * Retransmit the unack'ed segment and 14417 * restart fast recovery. Note that we 14418 * need to scale back tcp_cwnd to the 14419 * original value when we started fast 14420 * recovery. This is to prevent overly 14421 * aggressive behaviour in sending new 14422 * segments. 14423 */ 14424 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14425 tcps->tcps_dupack_fast_retransmit * mss; 14426 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14427 flags |= TH_REXMIT_NEEDED; 14428 } 14429 } 14430 } else { 14431 tcp->tcp_dupack_cnt = 0; 14432 if (tcp->tcp_rexmit) { 14433 /* 14434 * TCP is retranmitting. If the ACK ack's all 14435 * outstanding data, update tcp_rexmit_max and 14436 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14437 * to the correct value. 14438 * 14439 * Note that SEQ_LEQ() is used. This is to avoid 14440 * unnecessary fast retransmit caused by dup ACKs 14441 * received when TCP does slow start retransmission 14442 * after a time out. During this phase, TCP may 14443 * send out segments which are already received. 14444 * This causes dup ACKs to be sent back. 14445 */ 14446 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14447 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14448 tcp->tcp_rexmit_nxt = seg_ack; 14449 } 14450 if (seg_ack != tcp->tcp_rexmit_max) { 14451 flags |= TH_XMIT_NEEDED; 14452 } 14453 } else { 14454 tcp->tcp_rexmit = B_FALSE; 14455 tcp->tcp_xmit_zc_clean = B_FALSE; 14456 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14457 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14458 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14459 } 14460 tcp->tcp_ms_we_have_waited = 0; 14461 } 14462 } 14463 14464 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14465 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14466 tcp->tcp_suna = seg_ack; 14467 if (tcp->tcp_zero_win_probe != 0) { 14468 tcp->tcp_zero_win_probe = 0; 14469 tcp->tcp_timer_backoff = 0; 14470 } 14471 14472 /* 14473 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14474 * Note that it cannot be the SYN being ack'ed. The code flow 14475 * will not reach here. 14476 */ 14477 if (mp1 == NULL) { 14478 goto fin_acked; 14479 } 14480 14481 /* 14482 * Update the congestion window. 14483 * 14484 * If TCP is not ECN capable or TCP is ECN capable but the 14485 * congestion experience bit is not set, increase the tcp_cwnd as 14486 * usual. 14487 */ 14488 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14489 cwnd = tcp->tcp_cwnd; 14490 add = mss; 14491 14492 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14493 /* 14494 * This is to prevent an increase of less than 1 MSS of 14495 * tcp_cwnd. With partial increase, tcp_wput_data() 14496 * may send out tinygrams in order to preserve mblk 14497 * boundaries. 14498 * 14499 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14500 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14501 * increased by 1 MSS for every RTTs. 14502 */ 14503 if (tcp->tcp_cwnd_cnt <= 0) { 14504 tcp->tcp_cwnd_cnt = cwnd + add; 14505 } else { 14506 tcp->tcp_cwnd_cnt -= add; 14507 add = 0; 14508 } 14509 } 14510 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14511 } 14512 14513 /* See if the latest urgent data has been acknowledged */ 14514 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14515 SEQ_GT(seg_ack, tcp->tcp_urg)) 14516 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14517 14518 /* Can we update the RTT estimates? */ 14519 if (tcp->tcp_snd_ts_ok) { 14520 /* Ignore zero timestamp echo-reply. */ 14521 if (tcpopt.tcp_opt_ts_ecr != 0) { 14522 tcp_set_rto(tcp, (int32_t)lbolt - 14523 (int32_t)tcpopt.tcp_opt_ts_ecr); 14524 } 14525 14526 /* If needed, restart the timer. */ 14527 if (tcp->tcp_set_timer == 1) { 14528 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14529 tcp->tcp_set_timer = 0; 14530 } 14531 /* 14532 * Update tcp_csuna in case the other side stops sending 14533 * us timestamps. 14534 */ 14535 tcp->tcp_csuna = tcp->tcp_snxt; 14536 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14537 /* 14538 * An ACK sequence we haven't seen before, so get the RTT 14539 * and update the RTO. But first check if the timestamp is 14540 * valid to use. 14541 */ 14542 if ((mp1->b_next != NULL) && 14543 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14544 tcp_set_rto(tcp, (int32_t)lbolt - 14545 (int32_t)(intptr_t)mp1->b_prev); 14546 else 14547 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14548 14549 /* Remeber the last sequence to be ACKed */ 14550 tcp->tcp_csuna = seg_ack; 14551 if (tcp->tcp_set_timer == 1) { 14552 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14553 tcp->tcp_set_timer = 0; 14554 } 14555 } else { 14556 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14557 } 14558 14559 /* Eat acknowledged bytes off the xmit queue. */ 14560 for (;;) { 14561 mblk_t *mp2; 14562 uchar_t *wptr; 14563 14564 wptr = mp1->b_wptr; 14565 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14566 bytes_acked -= (int)(wptr - mp1->b_rptr); 14567 if (bytes_acked < 0) { 14568 mp1->b_rptr = wptr + bytes_acked; 14569 /* 14570 * Set a new timestamp if all the bytes timed by the 14571 * old timestamp have been ack'ed. 14572 */ 14573 if (SEQ_GT(seg_ack, 14574 (uint32_t)(uintptr_t)(mp1->b_next))) { 14575 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14576 mp1->b_next = NULL; 14577 } 14578 break; 14579 } 14580 mp1->b_next = NULL; 14581 mp1->b_prev = NULL; 14582 mp2 = mp1; 14583 mp1 = mp1->b_cont; 14584 14585 /* 14586 * This notification is required for some zero-copy 14587 * clients to maintain a copy semantic. After the data 14588 * is ack'ed, client is safe to modify or reuse the buffer. 14589 */ 14590 if (tcp->tcp_snd_zcopy_aware && 14591 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14592 tcp_zcopy_notify(tcp); 14593 freeb(mp2); 14594 if (bytes_acked == 0) { 14595 if (mp1 == NULL) { 14596 /* Everything is ack'ed, clear the tail. */ 14597 tcp->tcp_xmit_tail = NULL; 14598 /* 14599 * Cancel the timer unless we are still 14600 * waiting for an ACK for the FIN packet. 14601 */ 14602 if (tcp->tcp_timer_tid != 0 && 14603 tcp->tcp_snxt == tcp->tcp_suna) { 14604 (void) TCP_TIMER_CANCEL(tcp, 14605 tcp->tcp_timer_tid); 14606 tcp->tcp_timer_tid = 0; 14607 } 14608 goto pre_swnd_update; 14609 } 14610 if (mp2 != tcp->tcp_xmit_tail) 14611 break; 14612 tcp->tcp_xmit_tail = mp1; 14613 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14614 (uintptr_t)INT_MAX); 14615 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14616 mp1->b_rptr); 14617 break; 14618 } 14619 if (mp1 == NULL) { 14620 /* 14621 * More was acked but there is nothing more 14622 * outstanding. This means that the FIN was 14623 * just acked or that we're talking to a clown. 14624 */ 14625 fin_acked: 14626 ASSERT(tcp->tcp_fin_sent); 14627 tcp->tcp_xmit_tail = NULL; 14628 if (tcp->tcp_fin_sent) { 14629 /* FIN was acked - making progress */ 14630 if (tcp->tcp_ipversion == IPV6_VERSION && 14631 !tcp->tcp_fin_acked) 14632 tcp->tcp_ip_forward_progress = B_TRUE; 14633 tcp->tcp_fin_acked = B_TRUE; 14634 if (tcp->tcp_linger_tid != 0 && 14635 TCP_TIMER_CANCEL(tcp, 14636 tcp->tcp_linger_tid) >= 0) { 14637 tcp_stop_lingering(tcp); 14638 freemsg(mp); 14639 mp = NULL; 14640 } 14641 } else { 14642 /* 14643 * We should never get here because 14644 * we have already checked that the 14645 * number of bytes ack'ed should be 14646 * smaller than or equal to what we 14647 * have sent so far (it is the 14648 * acceptability check of the ACK). 14649 * We can only get here if the send 14650 * queue is corrupted. 14651 * 14652 * Terminate the connection and 14653 * panic the system. It is better 14654 * for us to panic instead of 14655 * continuing to avoid other disaster. 14656 */ 14657 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14658 tcp->tcp_rnxt, TH_RST|TH_ACK); 14659 panic("Memory corruption " 14660 "detected for connection %s.", 14661 tcp_display(tcp, NULL, 14662 DISP_ADDR_AND_PORT)); 14663 /*NOTREACHED*/ 14664 } 14665 goto pre_swnd_update; 14666 } 14667 ASSERT(mp2 != tcp->tcp_xmit_tail); 14668 } 14669 if (tcp->tcp_unsent) { 14670 flags |= TH_XMIT_NEEDED; 14671 } 14672 pre_swnd_update: 14673 tcp->tcp_xmit_head = mp1; 14674 swnd_update: 14675 /* 14676 * The following check is different from most other implementations. 14677 * For bi-directional transfer, when segments are dropped, the 14678 * "normal" check will not accept a window update in those 14679 * retransmitted segemnts. Failing to do that, TCP may send out 14680 * segments which are outside receiver's window. As TCP accepts 14681 * the ack in those retransmitted segments, if the window update in 14682 * the same segment is not accepted, TCP will incorrectly calculates 14683 * that it can send more segments. This can create a deadlock 14684 * with the receiver if its window becomes zero. 14685 */ 14686 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14687 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14688 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14689 /* 14690 * The criteria for update is: 14691 * 14692 * 1. the segment acknowledges some data. Or 14693 * 2. the segment is new, i.e. it has a higher seq num. Or 14694 * 3. the segment is not old and the advertised window is 14695 * larger than the previous advertised window. 14696 */ 14697 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14698 flags |= TH_XMIT_NEEDED; 14699 tcp->tcp_swnd = new_swnd; 14700 if (new_swnd > tcp->tcp_max_swnd) 14701 tcp->tcp_max_swnd = new_swnd; 14702 tcp->tcp_swl1 = seg_seq; 14703 tcp->tcp_swl2 = seg_ack; 14704 } 14705 est: 14706 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14707 14708 switch (tcp->tcp_state) { 14709 case TCPS_FIN_WAIT_1: 14710 if (tcp->tcp_fin_acked) { 14711 tcp->tcp_state = TCPS_FIN_WAIT_2; 14712 /* 14713 * We implement the non-standard BSD/SunOS 14714 * FIN_WAIT_2 flushing algorithm. 14715 * If there is no user attached to this 14716 * TCP endpoint, then this TCP struct 14717 * could hang around forever in FIN_WAIT_2 14718 * state if the peer forgets to send us 14719 * a FIN. To prevent this, we wait only 14720 * 2*MSL (a convenient time value) for 14721 * the FIN to arrive. If it doesn't show up, 14722 * we flush the TCP endpoint. This algorithm, 14723 * though a violation of RFC-793, has worked 14724 * for over 10 years in BSD systems. 14725 * Note: SunOS 4.x waits 675 seconds before 14726 * flushing the FIN_WAIT_2 connection. 14727 */ 14728 TCP_TIMER_RESTART(tcp, 14729 tcps->tcps_fin_wait_2_flush_interval); 14730 } 14731 break; 14732 case TCPS_FIN_WAIT_2: 14733 break; /* Shutdown hook? */ 14734 case TCPS_LAST_ACK: 14735 freemsg(mp); 14736 if (tcp->tcp_fin_acked) { 14737 (void) tcp_clean_death(tcp, 0, 19); 14738 return; 14739 } 14740 goto xmit_check; 14741 case TCPS_CLOSING: 14742 if (tcp->tcp_fin_acked) { 14743 tcp->tcp_state = TCPS_TIME_WAIT; 14744 /* 14745 * Unconditionally clear the exclusive binding 14746 * bit so this TIME-WAIT connection won't 14747 * interfere with new ones. 14748 */ 14749 tcp->tcp_exclbind = 0; 14750 if (!TCP_IS_DETACHED(tcp)) { 14751 TCP_TIMER_RESTART(tcp, 14752 tcps->tcps_time_wait_interval); 14753 } else { 14754 tcp_time_wait_append(tcp); 14755 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14756 } 14757 } 14758 /*FALLTHRU*/ 14759 case TCPS_CLOSE_WAIT: 14760 freemsg(mp); 14761 goto xmit_check; 14762 default: 14763 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14764 break; 14765 } 14766 } 14767 if (flags & TH_FIN) { 14768 /* Make sure we ack the fin */ 14769 flags |= TH_ACK_NEEDED; 14770 if (!tcp->tcp_fin_rcvd) { 14771 tcp->tcp_fin_rcvd = B_TRUE; 14772 tcp->tcp_rnxt++; 14773 tcph = tcp->tcp_tcph; 14774 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14775 14776 /* 14777 * Generate the ordrel_ind at the end unless we 14778 * are an eager guy. 14779 * In the eager case tcp_rsrv will do this when run 14780 * after tcp_accept is done. 14781 */ 14782 if (tcp->tcp_listener == NULL && 14783 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14784 flags |= TH_ORDREL_NEEDED; 14785 switch (tcp->tcp_state) { 14786 case TCPS_SYN_RCVD: 14787 case TCPS_ESTABLISHED: 14788 tcp->tcp_state = TCPS_CLOSE_WAIT; 14789 /* Keepalive? */ 14790 break; 14791 case TCPS_FIN_WAIT_1: 14792 if (!tcp->tcp_fin_acked) { 14793 tcp->tcp_state = TCPS_CLOSING; 14794 break; 14795 } 14796 /* FALLTHRU */ 14797 case TCPS_FIN_WAIT_2: 14798 tcp->tcp_state = TCPS_TIME_WAIT; 14799 /* 14800 * Unconditionally clear the exclusive binding 14801 * bit so this TIME-WAIT connection won't 14802 * interfere with new ones. 14803 */ 14804 tcp->tcp_exclbind = 0; 14805 if (!TCP_IS_DETACHED(tcp)) { 14806 TCP_TIMER_RESTART(tcp, 14807 tcps->tcps_time_wait_interval); 14808 } else { 14809 tcp_time_wait_append(tcp); 14810 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14811 } 14812 if (seg_len) { 14813 /* 14814 * implies data piggybacked on FIN. 14815 * break to handle data. 14816 */ 14817 break; 14818 } 14819 freemsg(mp); 14820 goto ack_check; 14821 } 14822 } 14823 } 14824 if (mp == NULL) 14825 goto xmit_check; 14826 if (seg_len == 0) { 14827 freemsg(mp); 14828 goto xmit_check; 14829 } 14830 if (mp->b_rptr == mp->b_wptr) { 14831 /* 14832 * The header has been consumed, so we remove the 14833 * zero-length mblk here. 14834 */ 14835 mp1 = mp; 14836 mp = mp->b_cont; 14837 freeb(mp1); 14838 } 14839 update_ack: 14840 tcph = tcp->tcp_tcph; 14841 tcp->tcp_rack_cnt++; 14842 { 14843 uint32_t cur_max; 14844 14845 cur_max = tcp->tcp_rack_cur_max; 14846 if (tcp->tcp_rack_cnt >= cur_max) { 14847 /* 14848 * We have more unacked data than we should - send 14849 * an ACK now. 14850 */ 14851 flags |= TH_ACK_NEEDED; 14852 cur_max++; 14853 if (cur_max > tcp->tcp_rack_abs_max) 14854 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14855 else 14856 tcp->tcp_rack_cur_max = cur_max; 14857 } else if (TCP_IS_DETACHED(tcp)) { 14858 /* We don't have an ACK timer for detached TCP. */ 14859 flags |= TH_ACK_NEEDED; 14860 } else if (seg_len < mss) { 14861 /* 14862 * If we get a segment that is less than an mss, and we 14863 * already have unacknowledged data, and the amount 14864 * unacknowledged is not a multiple of mss, then we 14865 * better generate an ACK now. Otherwise, this may be 14866 * the tail piece of a transaction, and we would rather 14867 * wait for the response. 14868 */ 14869 uint32_t udif; 14870 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14871 (uintptr_t)INT_MAX); 14872 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14873 if (udif && (udif % mss)) 14874 flags |= TH_ACK_NEEDED; 14875 else 14876 flags |= TH_ACK_TIMER_NEEDED; 14877 } else { 14878 /* Start delayed ack timer */ 14879 flags |= TH_ACK_TIMER_NEEDED; 14880 } 14881 } 14882 tcp->tcp_rnxt += seg_len; 14883 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14884 14885 if (mp == NULL) 14886 goto xmit_check; 14887 14888 /* Update SACK list */ 14889 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14890 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14891 &(tcp->tcp_num_sack_blk)); 14892 } 14893 14894 if (tcp->tcp_urp_mp) { 14895 tcp->tcp_urp_mp->b_cont = mp; 14896 mp = tcp->tcp_urp_mp; 14897 tcp->tcp_urp_mp = NULL; 14898 /* Ready for a new signal. */ 14899 tcp->tcp_urp_last_valid = B_FALSE; 14900 #ifdef DEBUG 14901 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14902 "tcp_rput: sending exdata_ind %s", 14903 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14904 #endif /* DEBUG */ 14905 } 14906 14907 /* 14908 * Check for ancillary data changes compared to last segment. 14909 */ 14910 if (tcp->tcp_ipv6_recvancillary != 0) { 14911 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14912 ASSERT(mp != NULL); 14913 } 14914 14915 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14916 /* 14917 * Side queue inbound data until the accept happens. 14918 * tcp_accept/tcp_rput drains this when the accept happens. 14919 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14920 * T_EXDATA_IND) it is queued on b_next. 14921 * XXX Make urgent data use this. Requires: 14922 * Removing tcp_listener check for TH_URG 14923 * Making M_PCPROTO and MARK messages skip the eager case 14924 */ 14925 14926 if (tcp->tcp_kssl_pending) { 14927 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14928 mblk_t *, mp); 14929 tcp_kssl_input(tcp, mp); 14930 } else { 14931 tcp_rcv_enqueue(tcp, mp, seg_len); 14932 } 14933 } else { 14934 if (mp->b_datap->db_type != M_DATA || 14935 (flags & TH_MARKNEXT_NEEDED)) { 14936 if (IPCL_IS_NONSTR(connp)) { 14937 int error; 14938 14939 if ((*connp->conn_upcalls->su_recv) 14940 (connp->conn_upper_handle, mp, 14941 seg_len, 0, &error, NULL) <= 0) { 14942 /* 14943 * We should never be in middle of a 14944 * fallback, the squeue guarantees that. 14945 */ 14946 ASSERT(error != EOPNOTSUPP); 14947 if (error == ENOSPC) 14948 tcp->tcp_rwnd -= seg_len; 14949 } 14950 } else if (tcp->tcp_rcv_list != NULL) { 14951 flags |= tcp_rcv_drain(tcp); 14952 } 14953 ASSERT(tcp->tcp_rcv_list == NULL || 14954 tcp->tcp_fused_sigurg); 14955 14956 if (flags & TH_MARKNEXT_NEEDED) { 14957 #ifdef DEBUG 14958 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14959 "tcp_rput: sending MSGMARKNEXT %s", 14960 tcp_display(tcp, NULL, 14961 DISP_PORT_ONLY)); 14962 #endif /* DEBUG */ 14963 mp->b_flag |= MSGMARKNEXT; 14964 flags &= ~TH_MARKNEXT_NEEDED; 14965 } 14966 14967 /* Does this need SSL processing first? */ 14968 if ((tcp->tcp_kssl_ctx != NULL) && 14969 (DB_TYPE(mp) == M_DATA)) { 14970 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14971 mblk_t *, mp); 14972 tcp_kssl_input(tcp, mp); 14973 } else if (!IPCL_IS_NONSTR(connp)) { 14974 /* Already handled non-STREAMS case. */ 14975 putnext(tcp->tcp_rq, mp); 14976 if (!canputnext(tcp->tcp_rq)) 14977 tcp->tcp_rwnd -= seg_len; 14978 } 14979 } else if ((tcp->tcp_kssl_ctx != NULL) && 14980 (DB_TYPE(mp) == M_DATA)) { 14981 /* Does this need SSL processing first? */ 14982 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14983 tcp_kssl_input(tcp, mp); 14984 } else if (IPCL_IS_NONSTR(connp)) { 14985 /* Non-STREAMS socket */ 14986 boolean_t push = flags & (TH_PUSH|TH_FIN); 14987 int error; 14988 14989 if ((*connp->conn_upcalls->su_recv)( 14990 connp->conn_upper_handle, 14991 mp, seg_len, 0, &error, &push) <= 0) { 14992 /* 14993 * We should never be in middle of a 14994 * fallback, the squeue guarantees that. 14995 */ 14996 ASSERT(error != EOPNOTSUPP); 14997 if (error == ENOSPC) 14998 tcp->tcp_rwnd -= seg_len; 14999 } else if (push) { 15000 /* 15001 * PUSH bit set and sockfs is not 15002 * flow controlled 15003 */ 15004 flags |= tcp_rwnd_reopen(tcp); 15005 } 15006 } else if ((flags & (TH_PUSH|TH_FIN)) || 15007 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 15008 if (tcp->tcp_rcv_list != NULL) { 15009 /* 15010 * Enqueue the new segment first and then 15011 * call tcp_rcv_drain() to send all data 15012 * up. The other way to do this is to 15013 * send all queued data up and then call 15014 * putnext() to send the new segment up. 15015 * This way can remove the else part later 15016 * on. 15017 * 15018 * We don't do this to avoid one more call to 15019 * canputnext() as tcp_rcv_drain() needs to 15020 * call canputnext(). 15021 */ 15022 tcp_rcv_enqueue(tcp, mp, seg_len); 15023 flags |= tcp_rcv_drain(tcp); 15024 } else { 15025 putnext(tcp->tcp_rq, mp); 15026 if (!canputnext(tcp->tcp_rq)) 15027 tcp->tcp_rwnd -= seg_len; 15028 } 15029 } else { 15030 /* 15031 * Enqueue all packets when processing an mblk 15032 * from the co queue and also enqueue normal packets. 15033 */ 15034 tcp_rcv_enqueue(tcp, mp, seg_len); 15035 } 15036 /* 15037 * Make sure the timer is running if we have data waiting 15038 * for a push bit. This provides resiliency against 15039 * implementations that do not correctly generate push bits. 15040 */ 15041 if (!IPCL_IS_NONSTR(connp) && tcp->tcp_rcv_list != NULL && 15042 tcp->tcp_push_tid == 0) { 15043 /* 15044 * The connection may be closed at this point, so don't 15045 * do anything for a detached tcp. 15046 */ 15047 if (!TCP_IS_DETACHED(tcp)) 15048 tcp->tcp_push_tid = TCP_TIMER(tcp, 15049 tcp_push_timer, 15050 MSEC_TO_TICK( 15051 tcps->tcps_push_timer_interval)); 15052 } 15053 } 15054 15055 xmit_check: 15056 /* Is there anything left to do? */ 15057 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15058 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 15059 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 15060 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15061 goto done; 15062 15063 /* Any transmit work to do and a non-zero window? */ 15064 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 15065 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 15066 if (flags & TH_REXMIT_NEEDED) { 15067 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 15068 15069 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 15070 if (snd_size > mss) 15071 snd_size = mss; 15072 if (snd_size > tcp->tcp_swnd) 15073 snd_size = tcp->tcp_swnd; 15074 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 15075 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 15076 B_TRUE); 15077 15078 if (mp1 != NULL) { 15079 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15080 tcp->tcp_csuna = tcp->tcp_snxt; 15081 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 15082 UPDATE_MIB(&tcps->tcps_mib, 15083 tcpRetransBytes, snd_size); 15084 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15085 } 15086 } 15087 if (flags & TH_NEED_SACK_REXMIT) { 15088 tcp_sack_rxmit(tcp, &flags); 15089 } 15090 /* 15091 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 15092 * out new segment. Note that tcp_rexmit should not be 15093 * set, otherwise TH_LIMIT_XMIT should not be set. 15094 */ 15095 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 15096 if (!tcp->tcp_rexmit) { 15097 tcp_wput_data(tcp, NULL, B_FALSE); 15098 } else { 15099 tcp_ss_rexmit(tcp); 15100 } 15101 } 15102 /* 15103 * Adjust tcp_cwnd back to normal value after sending 15104 * new data segments. 15105 */ 15106 if (flags & TH_LIMIT_XMIT) { 15107 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 15108 /* 15109 * This will restart the timer. Restarting the 15110 * timer is used to avoid a timeout before the 15111 * limited transmitted segment's ACK gets back. 15112 */ 15113 if (tcp->tcp_xmit_head != NULL) 15114 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 15115 } 15116 15117 /* Anything more to do? */ 15118 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 15119 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 15120 goto done; 15121 } 15122 ack_check: 15123 if (flags & TH_SEND_URP_MARK) { 15124 ASSERT(tcp->tcp_urp_mark_mp); 15125 ASSERT(!IPCL_IS_NONSTR(connp)); 15126 /* 15127 * Send up any queued data and then send the mark message 15128 */ 15129 if (tcp->tcp_rcv_list != NULL) { 15130 flags |= tcp_rcv_drain(tcp); 15131 15132 } 15133 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15134 mp1 = tcp->tcp_urp_mark_mp; 15135 tcp->tcp_urp_mark_mp = NULL; 15136 putnext(tcp->tcp_rq, mp1); 15137 #ifdef DEBUG 15138 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 15139 "tcp_rput: sending zero-length %s %s", 15140 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 15141 "MSGNOTMARKNEXT"), 15142 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 15143 #endif /* DEBUG */ 15144 flags &= ~TH_SEND_URP_MARK; 15145 } 15146 if (flags & TH_ACK_NEEDED) { 15147 /* 15148 * Time to send an ack for some reason. 15149 */ 15150 mp1 = tcp_ack_mp(tcp); 15151 15152 if (mp1 != NULL) { 15153 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15154 BUMP_LOCAL(tcp->tcp_obsegs); 15155 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15156 } 15157 if (tcp->tcp_ack_tid != 0) { 15158 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15159 tcp->tcp_ack_tid = 0; 15160 } 15161 } 15162 if (flags & TH_ACK_TIMER_NEEDED) { 15163 /* 15164 * Arrange for deferred ACK or push wait timeout. 15165 * Start timer if it is not already running. 15166 */ 15167 if (tcp->tcp_ack_tid == 0) { 15168 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15169 MSEC_TO_TICK(tcp->tcp_localnet ? 15170 (clock_t)tcps->tcps_local_dack_interval : 15171 (clock_t)tcps->tcps_deferred_ack_interval)); 15172 } 15173 } 15174 if (flags & TH_ORDREL_NEEDED) { 15175 /* 15176 * Send up the ordrel_ind unless we are an eager guy. 15177 * In the eager case tcp_rsrv will do this when run 15178 * after tcp_accept is done. 15179 */ 15180 ASSERT(tcp->tcp_listener == NULL); 15181 15182 if (IPCL_IS_NONSTR(connp)) { 15183 ASSERT(tcp->tcp_ordrel_mp == NULL); 15184 tcp->tcp_ordrel_done = B_TRUE; 15185 (*connp->conn_upcalls->su_opctl) 15186 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15187 goto done; 15188 } 15189 15190 if (tcp->tcp_rcv_list != NULL) { 15191 /* 15192 * Push any mblk(s) enqueued from co processing. 15193 */ 15194 flags |= tcp_rcv_drain(tcp); 15195 } 15196 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15197 15198 mp1 = tcp->tcp_ordrel_mp; 15199 tcp->tcp_ordrel_mp = NULL; 15200 tcp->tcp_ordrel_done = B_TRUE; 15201 putnext(tcp->tcp_rq, mp1); 15202 } 15203 done: 15204 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15205 } 15206 15207 /* 15208 * This routine adjusts next-to-send sequence number variables, in the 15209 * case where the reciever has shrunk it's window. 15210 */ 15211 static void 15212 tcp_update_xmit_tail(tcp_t *tcp, uint32_t snxt) 15213 { 15214 mblk_t *xmit_tail; 15215 int32_t offset; 15216 15217 tcp->tcp_snxt = snxt; 15218 15219 /* Get the mblk, and the offset in it, as per the shrunk window */ 15220 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 15221 ASSERT(xmit_tail != NULL); 15222 tcp->tcp_xmit_tail = xmit_tail; 15223 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - 15224 xmit_tail->b_rptr - offset; 15225 } 15226 15227 /* 15228 * This function does PAWS protection check. Returns B_TRUE if the 15229 * segment passes the PAWS test, else returns B_FALSE. 15230 */ 15231 boolean_t 15232 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15233 { 15234 uint8_t flags; 15235 int options; 15236 uint8_t *up; 15237 15238 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15239 /* 15240 * If timestamp option is aligned nicely, get values inline, 15241 * otherwise call general routine to parse. Only do that 15242 * if timestamp is the only option. 15243 */ 15244 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15245 TCPOPT_REAL_TS_LEN && 15246 OK_32PTR((up = ((uint8_t *)tcph) + 15247 TCP_MIN_HEADER_LENGTH)) && 15248 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15249 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15250 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15251 15252 options = TCP_OPT_TSTAMP_PRESENT; 15253 } else { 15254 if (tcp->tcp_snd_sack_ok) { 15255 tcpoptp->tcp = tcp; 15256 } else { 15257 tcpoptp->tcp = NULL; 15258 } 15259 options = tcp_parse_options(tcph, tcpoptp); 15260 } 15261 15262 if (options & TCP_OPT_TSTAMP_PRESENT) { 15263 /* 15264 * Do PAWS per RFC 1323 section 4.2. Accept RST 15265 * regardless of the timestamp, page 18 RFC 1323.bis. 15266 */ 15267 if ((flags & TH_RST) == 0 && 15268 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15269 tcp->tcp_ts_recent)) { 15270 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15271 PAWS_TIMEOUT)) { 15272 /* This segment is not acceptable. */ 15273 return (B_FALSE); 15274 } else { 15275 /* 15276 * Connection has been idle for 15277 * too long. Reset the timestamp 15278 * and assume the segment is valid. 15279 */ 15280 tcp->tcp_ts_recent = 15281 tcpoptp->tcp_opt_ts_val; 15282 } 15283 } 15284 } else { 15285 /* 15286 * If we don't get a timestamp on every packet, we 15287 * figure we can't really trust 'em, so we stop sending 15288 * and parsing them. 15289 */ 15290 tcp->tcp_snd_ts_ok = B_FALSE; 15291 15292 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15293 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15294 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15295 /* 15296 * Adjust the tcp_mss accordingly. We also need to 15297 * adjust tcp_cwnd here in accordance with the new mss. 15298 * But we avoid doing a slow start here so as to not 15299 * to lose on the transfer rate built up so far. 15300 */ 15301 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15302 if (tcp->tcp_snd_sack_ok) { 15303 ASSERT(tcp->tcp_sack_info != NULL); 15304 tcp->tcp_max_sack_blk = 4; 15305 } 15306 } 15307 return (B_TRUE); 15308 } 15309 15310 /* 15311 * Attach ancillary data to a received TCP segments for the 15312 * ancillary pieces requested by the application that are 15313 * different than they were in the previous data segment. 15314 * 15315 * Save the "current" values once memory allocation is ok so that 15316 * when memory allocation fails we can just wait for the next data segment. 15317 */ 15318 static mblk_t * 15319 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15320 { 15321 struct T_optdata_ind *todi; 15322 int optlen; 15323 uchar_t *optptr; 15324 struct T_opthdr *toh; 15325 uint_t addflag; /* Which pieces to add */ 15326 mblk_t *mp1; 15327 15328 optlen = 0; 15329 addflag = 0; 15330 /* If app asked for pktinfo and the index has changed ... */ 15331 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15332 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15333 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15334 optlen += sizeof (struct T_opthdr) + 15335 sizeof (struct in6_pktinfo); 15336 addflag |= TCP_IPV6_RECVPKTINFO; 15337 } 15338 /* If app asked for hoplimit and it has changed ... */ 15339 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15340 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15341 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15342 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15343 addflag |= TCP_IPV6_RECVHOPLIMIT; 15344 } 15345 /* If app asked for tclass and it has changed ... */ 15346 if ((ipp->ipp_fields & IPPF_TCLASS) && 15347 ipp->ipp_tclass != tcp->tcp_recvtclass && 15348 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15349 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15350 addflag |= TCP_IPV6_RECVTCLASS; 15351 } 15352 /* 15353 * If app asked for hopbyhop headers and it has changed ... 15354 * For security labels, note that (1) security labels can't change on 15355 * a connected socket at all, (2) we're connected to at most one peer, 15356 * (3) if anything changes, then it must be some other extra option. 15357 */ 15358 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15359 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15360 (ipp->ipp_fields & IPPF_HOPOPTS), 15361 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15362 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15363 tcp->tcp_label_len; 15364 addflag |= TCP_IPV6_RECVHOPOPTS; 15365 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15366 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15367 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15368 return (mp); 15369 } 15370 /* If app asked for dst headers before routing headers ... */ 15371 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15372 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15373 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15374 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15375 optlen += sizeof (struct T_opthdr) + 15376 ipp->ipp_rtdstoptslen; 15377 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15378 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15379 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15380 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15381 return (mp); 15382 } 15383 /* If app asked for routing headers and it has changed ... */ 15384 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15385 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15386 (ipp->ipp_fields & IPPF_RTHDR), 15387 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15388 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15389 addflag |= TCP_IPV6_RECVRTHDR; 15390 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15391 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15392 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15393 return (mp); 15394 } 15395 /* If app asked for dest headers and it has changed ... */ 15396 if ((tcp->tcp_ipv6_recvancillary & 15397 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15398 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15399 (ipp->ipp_fields & IPPF_DSTOPTS), 15400 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15401 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15402 addflag |= TCP_IPV6_RECVDSTOPTS; 15403 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15404 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15405 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15406 return (mp); 15407 } 15408 15409 if (optlen == 0) { 15410 /* Nothing to add */ 15411 return (mp); 15412 } 15413 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15414 if (mp1 == NULL) { 15415 /* 15416 * Defer sending ancillary data until the next TCP segment 15417 * arrives. 15418 */ 15419 return (mp); 15420 } 15421 mp1->b_cont = mp; 15422 mp = mp1; 15423 mp->b_wptr += sizeof (*todi) + optlen; 15424 mp->b_datap->db_type = M_PROTO; 15425 todi = (struct T_optdata_ind *)mp->b_rptr; 15426 todi->PRIM_type = T_OPTDATA_IND; 15427 todi->DATA_flag = 1; /* MORE data */ 15428 todi->OPT_length = optlen; 15429 todi->OPT_offset = sizeof (*todi); 15430 optptr = (uchar_t *)&todi[1]; 15431 /* 15432 * If app asked for pktinfo and the index has changed ... 15433 * Note that the local address never changes for the connection. 15434 */ 15435 if (addflag & TCP_IPV6_RECVPKTINFO) { 15436 struct in6_pktinfo *pkti; 15437 15438 toh = (struct T_opthdr *)optptr; 15439 toh->level = IPPROTO_IPV6; 15440 toh->name = IPV6_PKTINFO; 15441 toh->len = sizeof (*toh) + sizeof (*pkti); 15442 toh->status = 0; 15443 optptr += sizeof (*toh); 15444 pkti = (struct in6_pktinfo *)optptr; 15445 if (tcp->tcp_ipversion == IPV6_VERSION) 15446 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15447 else 15448 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15449 &pkti->ipi6_addr); 15450 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15451 optptr += sizeof (*pkti); 15452 ASSERT(OK_32PTR(optptr)); 15453 /* Save as "last" value */ 15454 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15455 } 15456 /* If app asked for hoplimit and it has changed ... */ 15457 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15458 toh = (struct T_opthdr *)optptr; 15459 toh->level = IPPROTO_IPV6; 15460 toh->name = IPV6_HOPLIMIT; 15461 toh->len = sizeof (*toh) + sizeof (uint_t); 15462 toh->status = 0; 15463 optptr += sizeof (*toh); 15464 *(uint_t *)optptr = ipp->ipp_hoplimit; 15465 optptr += sizeof (uint_t); 15466 ASSERT(OK_32PTR(optptr)); 15467 /* Save as "last" value */ 15468 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15469 } 15470 /* If app asked for tclass and it has changed ... */ 15471 if (addflag & TCP_IPV6_RECVTCLASS) { 15472 toh = (struct T_opthdr *)optptr; 15473 toh->level = IPPROTO_IPV6; 15474 toh->name = IPV6_TCLASS; 15475 toh->len = sizeof (*toh) + sizeof (uint_t); 15476 toh->status = 0; 15477 optptr += sizeof (*toh); 15478 *(uint_t *)optptr = ipp->ipp_tclass; 15479 optptr += sizeof (uint_t); 15480 ASSERT(OK_32PTR(optptr)); 15481 /* Save as "last" value */ 15482 tcp->tcp_recvtclass = ipp->ipp_tclass; 15483 } 15484 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15485 toh = (struct T_opthdr *)optptr; 15486 toh->level = IPPROTO_IPV6; 15487 toh->name = IPV6_HOPOPTS; 15488 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15489 tcp->tcp_label_len; 15490 toh->status = 0; 15491 optptr += sizeof (*toh); 15492 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15493 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15494 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15495 ASSERT(OK_32PTR(optptr)); 15496 /* Save as last value */ 15497 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15498 (ipp->ipp_fields & IPPF_HOPOPTS), 15499 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15500 } 15501 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15502 toh = (struct T_opthdr *)optptr; 15503 toh->level = IPPROTO_IPV6; 15504 toh->name = IPV6_RTHDRDSTOPTS; 15505 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15506 toh->status = 0; 15507 optptr += sizeof (*toh); 15508 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15509 optptr += ipp->ipp_rtdstoptslen; 15510 ASSERT(OK_32PTR(optptr)); 15511 /* Save as last value */ 15512 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15513 &tcp->tcp_rtdstoptslen, 15514 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15515 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15516 } 15517 if (addflag & TCP_IPV6_RECVRTHDR) { 15518 toh = (struct T_opthdr *)optptr; 15519 toh->level = IPPROTO_IPV6; 15520 toh->name = IPV6_RTHDR; 15521 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15522 toh->status = 0; 15523 optptr += sizeof (*toh); 15524 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15525 optptr += ipp->ipp_rthdrlen; 15526 ASSERT(OK_32PTR(optptr)); 15527 /* Save as last value */ 15528 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15529 (ipp->ipp_fields & IPPF_RTHDR), 15530 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15531 } 15532 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15533 toh = (struct T_opthdr *)optptr; 15534 toh->level = IPPROTO_IPV6; 15535 toh->name = IPV6_DSTOPTS; 15536 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15537 toh->status = 0; 15538 optptr += sizeof (*toh); 15539 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15540 optptr += ipp->ipp_dstoptslen; 15541 ASSERT(OK_32PTR(optptr)); 15542 /* Save as last value */ 15543 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15544 (ipp->ipp_fields & IPPF_DSTOPTS), 15545 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15546 } 15547 ASSERT(optptr == mp->b_wptr); 15548 return (mp); 15549 } 15550 15551 /* 15552 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15553 * messages. 15554 */ 15555 void 15556 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15557 { 15558 uchar_t *rptr = mp->b_rptr; 15559 queue_t *q = tcp->tcp_rq; 15560 struct T_error_ack *tea; 15561 15562 switch (mp->b_datap->db_type) { 15563 case M_PROTO: 15564 case M_PCPROTO: 15565 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15566 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15567 break; 15568 tea = (struct T_error_ack *)rptr; 15569 ASSERT(tea->PRIM_type != T_BIND_ACK); 15570 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15571 tea->ERROR_prim != T_BIND_REQ); 15572 switch (tea->PRIM_type) { 15573 case T_ERROR_ACK: 15574 if (tcp->tcp_debug) { 15575 (void) strlog(TCP_MOD_ID, 0, 1, 15576 SL_TRACE|SL_ERROR, 15577 "tcp_rput_other: case T_ERROR_ACK, " 15578 "ERROR_prim == %d", 15579 tea->ERROR_prim); 15580 } 15581 switch (tea->ERROR_prim) { 15582 case T_SVR4_OPTMGMT_REQ: 15583 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15584 /* T_OPTMGMT_REQ generated by TCP */ 15585 printf("T_SVR4_OPTMGMT_REQ failed " 15586 "%d/%d - dropped (cnt %d)\n", 15587 tea->TLI_error, tea->UNIX_error, 15588 tcp->tcp_drop_opt_ack_cnt); 15589 freemsg(mp); 15590 tcp->tcp_drop_opt_ack_cnt--; 15591 return; 15592 } 15593 break; 15594 } 15595 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15596 tcp->tcp_drop_opt_ack_cnt > 0) { 15597 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15598 "- dropped (cnt %d)\n", 15599 tea->TLI_error, tea->UNIX_error, 15600 tcp->tcp_drop_opt_ack_cnt); 15601 freemsg(mp); 15602 tcp->tcp_drop_opt_ack_cnt--; 15603 return; 15604 } 15605 break; 15606 case T_OPTMGMT_ACK: 15607 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15608 /* T_OPTMGMT_REQ generated by TCP */ 15609 freemsg(mp); 15610 tcp->tcp_drop_opt_ack_cnt--; 15611 return; 15612 } 15613 break; 15614 default: 15615 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15616 break; 15617 } 15618 break; 15619 case M_FLUSH: 15620 if (*rptr & FLUSHR) 15621 flushq(q, FLUSHDATA); 15622 break; 15623 default: 15624 /* M_CTL will be directly sent to tcp_icmp_error() */ 15625 ASSERT(DB_TYPE(mp) != M_CTL); 15626 break; 15627 } 15628 /* 15629 * Make sure we set this bit before sending the ACK for 15630 * bind. Otherwise accept could possibly run and free 15631 * this tcp struct. 15632 */ 15633 ASSERT(q != NULL); 15634 putnext(q, mp); 15635 } 15636 15637 /* ARGSUSED */ 15638 static void 15639 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15640 { 15641 conn_t *connp = (conn_t *)arg; 15642 tcp_t *tcp = connp->conn_tcp; 15643 queue_t *q = tcp->tcp_rq; 15644 tcp_stack_t *tcps = tcp->tcp_tcps; 15645 15646 ASSERT(!IPCL_IS_NONSTR(connp)); 15647 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15648 tcp->tcp_rsrv_mp = mp; 15649 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15650 15651 TCP_STAT(tcps, tcp_rsrv_calls); 15652 15653 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15654 return; 15655 } 15656 15657 if (tcp->tcp_fused) { 15658 tcp_fuse_backenable(tcp); 15659 return; 15660 } 15661 15662 if (canputnext(q)) { 15663 /* Not flow-controlled, open rwnd */ 15664 tcp->tcp_rwnd = q->q_hiwat; 15665 15666 /* 15667 * Send back a window update immediately if TCP is above 15668 * ESTABLISHED state and the increase of the rcv window 15669 * that the other side knows is at least 1 MSS after flow 15670 * control is lifted. 15671 */ 15672 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15673 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 15674 tcp_xmit_ctl(NULL, tcp, 15675 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15676 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15677 } 15678 } 15679 } 15680 15681 /* 15682 * The read side service routine is called mostly when we get back-enabled as a 15683 * result of flow control relief. Since we don't actually queue anything in 15684 * TCP, we have no data to send out of here. What we do is clear the receive 15685 * window, and send out a window update. 15686 */ 15687 static void 15688 tcp_rsrv(queue_t *q) 15689 { 15690 conn_t *connp = Q_TO_CONN(q); 15691 tcp_t *tcp = connp->conn_tcp; 15692 mblk_t *mp; 15693 tcp_stack_t *tcps = tcp->tcp_tcps; 15694 15695 /* No code does a putq on the read side */ 15696 ASSERT(q->q_first == NULL); 15697 15698 /* Nothing to do for the default queue */ 15699 if (q == tcps->tcps_g_q) { 15700 return; 15701 } 15702 15703 /* 15704 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15705 * been run. So just return. 15706 */ 15707 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15708 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15709 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15710 return; 15711 } 15712 tcp->tcp_rsrv_mp = NULL; 15713 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15714 15715 CONN_INC_REF(connp); 15716 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15717 SQ_PROCESS, SQTAG_TCP_RSRV); 15718 } 15719 15720 /* 15721 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15722 * We do not allow the receive window to shrink. After setting rwnd, 15723 * set the flow control hiwat of the stream. 15724 * 15725 * This function is called in 2 cases: 15726 * 15727 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15728 * connection (passive open) and in tcp_rput_data() for active connect. 15729 * This is called after tcp_mss_set() when the desired MSS value is known. 15730 * This makes sure that our window size is a mutiple of the other side's 15731 * MSS. 15732 * 2) Handling SO_RCVBUF option. 15733 * 15734 * It is ASSUMED that the requested size is a multiple of the current MSS. 15735 * 15736 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15737 * user requests so. 15738 */ 15739 static int 15740 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15741 { 15742 uint32_t mss = tcp->tcp_mss; 15743 uint32_t old_max_rwnd; 15744 uint32_t max_transmittable_rwnd; 15745 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15746 tcp_stack_t *tcps = tcp->tcp_tcps; 15747 15748 if (tcp->tcp_fused) { 15749 size_t sth_hiwat; 15750 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15751 15752 ASSERT(peer_tcp != NULL); 15753 /* 15754 * Record the stream head's high water mark for 15755 * this endpoint; this is used for flow-control 15756 * purposes in tcp_fuse_output(). 15757 */ 15758 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15759 if (!tcp_detached) { 15760 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15761 sth_hiwat); 15762 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15763 conn_t *connp = tcp->tcp_connp; 15764 struct sock_proto_props sopp; 15765 15766 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15767 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15768 15769 (*connp->conn_upcalls->su_set_proto_props) 15770 (connp->conn_upper_handle, &sopp); 15771 } 15772 } 15773 15774 /* 15775 * In the fusion case, the maxpsz stream head value of 15776 * our peer is set according to its send buffer size 15777 * and our receive buffer size; since the latter may 15778 * have changed we need to update the peer's maxpsz. 15779 */ 15780 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15781 return (rwnd); 15782 } 15783 15784 if (tcp_detached) { 15785 old_max_rwnd = tcp->tcp_rwnd; 15786 } else { 15787 old_max_rwnd = tcp->tcp_recv_hiwater; 15788 } 15789 15790 /* 15791 * Insist on a receive window that is at least 15792 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15793 * funny TCP interactions of Nagle algorithm, SWS avoidance 15794 * and delayed acknowledgement. 15795 */ 15796 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15797 15798 /* 15799 * If window size info has already been exchanged, TCP should not 15800 * shrink the window. Shrinking window is doable if done carefully. 15801 * We may add that support later. But so far there is not a real 15802 * need to do that. 15803 */ 15804 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15805 /* MSS may have changed, do a round up again. */ 15806 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15807 } 15808 15809 /* 15810 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15811 * can be applied even before the window scale option is decided. 15812 */ 15813 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15814 if (rwnd > max_transmittable_rwnd) { 15815 rwnd = max_transmittable_rwnd - 15816 (max_transmittable_rwnd % mss); 15817 if (rwnd < mss) 15818 rwnd = max_transmittable_rwnd; 15819 /* 15820 * If we're over the limit we may have to back down tcp_rwnd. 15821 * The increment below won't work for us. So we set all three 15822 * here and the increment below will have no effect. 15823 */ 15824 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15825 } 15826 if (tcp->tcp_localnet) { 15827 tcp->tcp_rack_abs_max = 15828 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15829 } else { 15830 /* 15831 * For a remote host on a different subnet (through a router), 15832 * we ack every other packet to be conforming to RFC1122. 15833 * tcp_deferred_acks_max is default to 2. 15834 */ 15835 tcp->tcp_rack_abs_max = 15836 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15837 } 15838 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15839 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15840 else 15841 tcp->tcp_rack_cur_max = 0; 15842 /* 15843 * Increment the current rwnd by the amount the maximum grew (we 15844 * can not overwrite it since we might be in the middle of a 15845 * connection.) 15846 */ 15847 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15848 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15849 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15850 tcp->tcp_cwnd_max = rwnd; 15851 15852 if (tcp_detached) 15853 return (rwnd); 15854 /* 15855 * We set the maximum receive window into rq->q_hiwat if it is 15856 * a STREAMS socket. 15857 * This is not actually used for flow control. 15858 */ 15859 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15860 tcp->tcp_rq->q_hiwat = rwnd; 15861 tcp->tcp_recv_hiwater = rwnd; 15862 /* 15863 * Set the STREAM head high water mark. This doesn't have to be 15864 * here, since we are simply using default values, but we would 15865 * prefer to choose these values algorithmically, with a likely 15866 * relationship to rwnd. 15867 */ 15868 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15869 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15870 return (rwnd); 15871 } 15872 15873 /* 15874 * Return SNMP stuff in buffer in mpdata. 15875 */ 15876 mblk_t * 15877 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15878 { 15879 mblk_t *mpdata; 15880 mblk_t *mp_conn_ctl = NULL; 15881 mblk_t *mp_conn_tail; 15882 mblk_t *mp_attr_ctl = NULL; 15883 mblk_t *mp_attr_tail; 15884 mblk_t *mp6_conn_ctl = NULL; 15885 mblk_t *mp6_conn_tail; 15886 mblk_t *mp6_attr_ctl = NULL; 15887 mblk_t *mp6_attr_tail; 15888 struct opthdr *optp; 15889 mib2_tcpConnEntry_t tce; 15890 mib2_tcp6ConnEntry_t tce6; 15891 mib2_transportMLPEntry_t mlp; 15892 connf_t *connfp; 15893 int i; 15894 boolean_t ispriv; 15895 zoneid_t zoneid; 15896 int v4_conn_idx; 15897 int v6_conn_idx; 15898 conn_t *connp = Q_TO_CONN(q); 15899 tcp_stack_t *tcps; 15900 ip_stack_t *ipst; 15901 mblk_t *mp2ctl; 15902 15903 /* 15904 * make a copy of the original message 15905 */ 15906 mp2ctl = copymsg(mpctl); 15907 15908 if (mpctl == NULL || 15909 (mpdata = mpctl->b_cont) == NULL || 15910 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15911 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15912 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15913 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15914 freemsg(mp_conn_ctl); 15915 freemsg(mp_attr_ctl); 15916 freemsg(mp6_conn_ctl); 15917 freemsg(mp6_attr_ctl); 15918 freemsg(mpctl); 15919 freemsg(mp2ctl); 15920 return (NULL); 15921 } 15922 15923 ipst = connp->conn_netstack->netstack_ip; 15924 tcps = connp->conn_netstack->netstack_tcp; 15925 15926 /* build table of connections -- need count in fixed part */ 15927 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15928 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15929 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15930 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15931 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15932 15933 ispriv = 15934 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15935 zoneid = Q_TO_CONN(q)->conn_zoneid; 15936 15937 v4_conn_idx = v6_conn_idx = 0; 15938 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15939 15940 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15941 ipst = tcps->tcps_netstack->netstack_ip; 15942 15943 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15944 15945 connp = NULL; 15946 15947 while ((connp = 15948 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15949 tcp_t *tcp; 15950 boolean_t needattr; 15951 15952 if (connp->conn_zoneid != zoneid) 15953 continue; /* not in this zone */ 15954 15955 tcp = connp->conn_tcp; 15956 UPDATE_MIB(&tcps->tcps_mib, 15957 tcpHCInSegs, tcp->tcp_ibsegs); 15958 tcp->tcp_ibsegs = 0; 15959 UPDATE_MIB(&tcps->tcps_mib, 15960 tcpHCOutSegs, tcp->tcp_obsegs); 15961 tcp->tcp_obsegs = 0; 15962 15963 tce6.tcp6ConnState = tce.tcpConnState = 15964 tcp_snmp_state(tcp); 15965 if (tce.tcpConnState == MIB2_TCP_established || 15966 tce.tcpConnState == MIB2_TCP_closeWait) 15967 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15968 15969 needattr = B_FALSE; 15970 bzero(&mlp, sizeof (mlp)); 15971 if (connp->conn_mlp_type != mlptSingle) { 15972 if (connp->conn_mlp_type == mlptShared || 15973 connp->conn_mlp_type == mlptBoth) 15974 mlp.tme_flags |= MIB2_TMEF_SHARED; 15975 if (connp->conn_mlp_type == mlptPrivate || 15976 connp->conn_mlp_type == mlptBoth) 15977 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15978 needattr = B_TRUE; 15979 } 15980 if (connp->conn_anon_mlp) { 15981 mlp.tme_flags |= MIB2_TMEF_ANONMLP; 15982 needattr = B_TRUE; 15983 } 15984 if (connp->conn_mac_exempt) { 15985 mlp.tme_flags |= MIB2_TMEF_MACEXEMPT; 15986 needattr = B_TRUE; 15987 } 15988 if (connp->conn_fully_bound && 15989 connp->conn_effective_cred != NULL) { 15990 ts_label_t *tsl; 15991 15992 tsl = crgetlabel(connp->conn_effective_cred); 15993 mlp.tme_flags |= MIB2_TMEF_IS_LABELED; 15994 mlp.tme_doi = label2doi(tsl); 15995 mlp.tme_label = *label2bslabel(tsl); 15996 needattr = B_TRUE; 15997 } 15998 15999 /* Create a message to report on IPv6 entries */ 16000 if (tcp->tcp_ipversion == IPV6_VERSION) { 16001 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 16002 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 16003 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 16004 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 16005 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 16006 /* Don't want just anybody seeing these... */ 16007 if (ispriv) { 16008 tce6.tcp6ConnEntryInfo.ce_snxt = 16009 tcp->tcp_snxt; 16010 tce6.tcp6ConnEntryInfo.ce_suna = 16011 tcp->tcp_suna; 16012 tce6.tcp6ConnEntryInfo.ce_rnxt = 16013 tcp->tcp_rnxt; 16014 tce6.tcp6ConnEntryInfo.ce_rack = 16015 tcp->tcp_rack; 16016 } else { 16017 /* 16018 * Netstat, unfortunately, uses this to 16019 * get send/receive queue sizes. How to fix? 16020 * Why not compute the difference only? 16021 */ 16022 tce6.tcp6ConnEntryInfo.ce_snxt = 16023 tcp->tcp_snxt - tcp->tcp_suna; 16024 tce6.tcp6ConnEntryInfo.ce_suna = 0; 16025 tce6.tcp6ConnEntryInfo.ce_rnxt = 16026 tcp->tcp_rnxt - tcp->tcp_rack; 16027 tce6.tcp6ConnEntryInfo.ce_rack = 0; 16028 } 16029 16030 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16031 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16032 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 16033 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 16034 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 16035 16036 tce6.tcp6ConnCreationProcess = 16037 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16038 tcp->tcp_cpid; 16039 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 16040 16041 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 16042 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 16043 16044 mlp.tme_connidx = v6_conn_idx++; 16045 if (needattr) 16046 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 16047 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 16048 } 16049 /* 16050 * Create an IPv4 table entry for IPv4 entries and also 16051 * for IPv6 entries which are bound to in6addr_any 16052 * but don't have IPV6_V6ONLY set. 16053 * (i.e. anything an IPv4 peer could connect to) 16054 */ 16055 if (tcp->tcp_ipversion == IPV4_VERSION || 16056 (tcp->tcp_state <= TCPS_LISTEN && 16057 !tcp->tcp_connp->conn_ipv6_v6only && 16058 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 16059 if (tcp->tcp_ipversion == IPV6_VERSION) { 16060 tce.tcpConnRemAddress = INADDR_ANY; 16061 tce.tcpConnLocalAddress = INADDR_ANY; 16062 } else { 16063 tce.tcpConnRemAddress = 16064 tcp->tcp_remote; 16065 tce.tcpConnLocalAddress = 16066 tcp->tcp_ip_src; 16067 } 16068 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 16069 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 16070 /* Don't want just anybody seeing these... */ 16071 if (ispriv) { 16072 tce.tcpConnEntryInfo.ce_snxt = 16073 tcp->tcp_snxt; 16074 tce.tcpConnEntryInfo.ce_suna = 16075 tcp->tcp_suna; 16076 tce.tcpConnEntryInfo.ce_rnxt = 16077 tcp->tcp_rnxt; 16078 tce.tcpConnEntryInfo.ce_rack = 16079 tcp->tcp_rack; 16080 } else { 16081 /* 16082 * Netstat, unfortunately, uses this to 16083 * get send/receive queue sizes. How 16084 * to fix? 16085 * Why not compute the difference only? 16086 */ 16087 tce.tcpConnEntryInfo.ce_snxt = 16088 tcp->tcp_snxt - tcp->tcp_suna; 16089 tce.tcpConnEntryInfo.ce_suna = 0; 16090 tce.tcpConnEntryInfo.ce_rnxt = 16091 tcp->tcp_rnxt - tcp->tcp_rack; 16092 tce.tcpConnEntryInfo.ce_rack = 0; 16093 } 16094 16095 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 16096 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 16097 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 16098 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 16099 tce.tcpConnEntryInfo.ce_state = 16100 tcp->tcp_state; 16101 16102 tce.tcpConnCreationProcess = 16103 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 16104 tcp->tcp_cpid; 16105 tce.tcpConnCreationTime = tcp->tcp_open_time; 16106 16107 (void) snmp_append_data2(mp_conn_ctl->b_cont, 16108 &mp_conn_tail, (char *)&tce, sizeof (tce)); 16109 16110 mlp.tme_connidx = v4_conn_idx++; 16111 if (needattr) 16112 (void) snmp_append_data2( 16113 mp_attr_ctl->b_cont, 16114 &mp_attr_tail, (char *)&mlp, 16115 sizeof (mlp)); 16116 } 16117 } 16118 } 16119 16120 /* fixed length structure for IPv4 and IPv6 counters */ 16121 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 16122 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 16123 sizeof (mib2_tcp6ConnEntry_t)); 16124 /* synchronize 32- and 64-bit counters */ 16125 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 16126 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 16127 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 16128 optp->level = MIB2_TCP; 16129 optp->name = 0; 16130 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 16131 sizeof (tcps->tcps_mib)); 16132 optp->len = msgdsize(mpdata); 16133 qreply(q, mpctl); 16134 16135 /* table of connections... */ 16136 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 16137 sizeof (struct T_optmgmt_ack)]; 16138 optp->level = MIB2_TCP; 16139 optp->name = MIB2_TCP_CONN; 16140 optp->len = msgdsize(mp_conn_ctl->b_cont); 16141 qreply(q, mp_conn_ctl); 16142 16143 /* table of MLP attributes... */ 16144 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 16145 sizeof (struct T_optmgmt_ack)]; 16146 optp->level = MIB2_TCP; 16147 optp->name = EXPER_XPORT_MLP; 16148 optp->len = msgdsize(mp_attr_ctl->b_cont); 16149 if (optp->len == 0) 16150 freemsg(mp_attr_ctl); 16151 else 16152 qreply(q, mp_attr_ctl); 16153 16154 /* table of IPv6 connections... */ 16155 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 16156 sizeof (struct T_optmgmt_ack)]; 16157 optp->level = MIB2_TCP6; 16158 optp->name = MIB2_TCP6_CONN; 16159 optp->len = msgdsize(mp6_conn_ctl->b_cont); 16160 qreply(q, mp6_conn_ctl); 16161 16162 /* table of IPv6 MLP attributes... */ 16163 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 16164 sizeof (struct T_optmgmt_ack)]; 16165 optp->level = MIB2_TCP6; 16166 optp->name = EXPER_XPORT_MLP; 16167 optp->len = msgdsize(mp6_attr_ctl->b_cont); 16168 if (optp->len == 0) 16169 freemsg(mp6_attr_ctl); 16170 else 16171 qreply(q, mp6_attr_ctl); 16172 return (mp2ctl); 16173 } 16174 16175 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16176 /* ARGSUSED */ 16177 int 16178 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16179 { 16180 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16181 16182 switch (level) { 16183 case MIB2_TCP: 16184 switch (name) { 16185 case 13: 16186 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16187 return (0); 16188 /* TODO: delete entry defined by tce */ 16189 return (1); 16190 default: 16191 return (0); 16192 } 16193 default: 16194 return (1); 16195 } 16196 } 16197 16198 /* Translate TCP state to MIB2 TCP state. */ 16199 static int 16200 tcp_snmp_state(tcp_t *tcp) 16201 { 16202 if (tcp == NULL) 16203 return (0); 16204 16205 switch (tcp->tcp_state) { 16206 case TCPS_CLOSED: 16207 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16208 case TCPS_BOUND: 16209 return (MIB2_TCP_closed); 16210 case TCPS_LISTEN: 16211 return (MIB2_TCP_listen); 16212 case TCPS_SYN_SENT: 16213 return (MIB2_TCP_synSent); 16214 case TCPS_SYN_RCVD: 16215 return (MIB2_TCP_synReceived); 16216 case TCPS_ESTABLISHED: 16217 return (MIB2_TCP_established); 16218 case TCPS_CLOSE_WAIT: 16219 return (MIB2_TCP_closeWait); 16220 case TCPS_FIN_WAIT_1: 16221 return (MIB2_TCP_finWait1); 16222 case TCPS_CLOSING: 16223 return (MIB2_TCP_closing); 16224 case TCPS_LAST_ACK: 16225 return (MIB2_TCP_lastAck); 16226 case TCPS_FIN_WAIT_2: 16227 return (MIB2_TCP_finWait2); 16228 case TCPS_TIME_WAIT: 16229 return (MIB2_TCP_timeWait); 16230 default: 16231 return (0); 16232 } 16233 } 16234 16235 /* 16236 * tcp_timer is the timer service routine. It handles the retransmission, 16237 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16238 * from the state of the tcp instance what kind of action needs to be done 16239 * at the time it is called. 16240 */ 16241 static void 16242 tcp_timer(void *arg) 16243 { 16244 mblk_t *mp; 16245 clock_t first_threshold; 16246 clock_t second_threshold; 16247 clock_t ms; 16248 uint32_t mss; 16249 conn_t *connp = (conn_t *)arg; 16250 tcp_t *tcp = connp->conn_tcp; 16251 tcp_stack_t *tcps = tcp->tcp_tcps; 16252 16253 tcp->tcp_timer_tid = 0; 16254 16255 if (tcp->tcp_fused) 16256 return; 16257 16258 first_threshold = tcp->tcp_first_timer_threshold; 16259 second_threshold = tcp->tcp_second_timer_threshold; 16260 switch (tcp->tcp_state) { 16261 case TCPS_IDLE: 16262 case TCPS_BOUND: 16263 case TCPS_LISTEN: 16264 return; 16265 case TCPS_SYN_RCVD: { 16266 tcp_t *listener = tcp->tcp_listener; 16267 16268 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16269 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16270 /* it's our first timeout */ 16271 tcp->tcp_syn_rcvd_timeout = 1; 16272 mutex_enter(&listener->tcp_eager_lock); 16273 listener->tcp_syn_rcvd_timeout++; 16274 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16275 /* 16276 * Make this eager available for drop if we 16277 * need to drop one to accomodate a new 16278 * incoming SYN request. 16279 */ 16280 MAKE_DROPPABLE(listener, tcp); 16281 } 16282 if (!listener->tcp_syn_defense && 16283 (listener->tcp_syn_rcvd_timeout > 16284 (tcps->tcps_conn_req_max_q0 >> 2)) && 16285 (tcps->tcps_conn_req_max_q0 > 200)) { 16286 /* We may be under attack. Put on a defense. */ 16287 listener->tcp_syn_defense = B_TRUE; 16288 cmn_err(CE_WARN, "High TCP connect timeout " 16289 "rate! System (port %d) may be under a " 16290 "SYN flood attack!", 16291 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16292 16293 listener->tcp_ip_addr_cache = kmem_zalloc( 16294 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16295 KM_NOSLEEP); 16296 } 16297 mutex_exit(&listener->tcp_eager_lock); 16298 } else if (listener != NULL) { 16299 mutex_enter(&listener->tcp_eager_lock); 16300 tcp->tcp_syn_rcvd_timeout++; 16301 if (tcp->tcp_syn_rcvd_timeout > 1 && 16302 !tcp->tcp_closemp_used) { 16303 /* 16304 * This is our second timeout. Put the tcp in 16305 * the list of droppable eagers to allow it to 16306 * be dropped, if needed. We don't check 16307 * whether tcp_dontdrop is set or not to 16308 * protect ourselve from a SYN attack where a 16309 * remote host can spoof itself as one of the 16310 * good IP source and continue to hold 16311 * resources too long. 16312 */ 16313 MAKE_DROPPABLE(listener, tcp); 16314 } 16315 mutex_exit(&listener->tcp_eager_lock); 16316 } 16317 } 16318 /* FALLTHRU */ 16319 case TCPS_SYN_SENT: 16320 first_threshold = tcp->tcp_first_ctimer_threshold; 16321 second_threshold = tcp->tcp_second_ctimer_threshold; 16322 break; 16323 case TCPS_ESTABLISHED: 16324 case TCPS_FIN_WAIT_1: 16325 case TCPS_CLOSING: 16326 case TCPS_CLOSE_WAIT: 16327 case TCPS_LAST_ACK: 16328 /* If we have data to rexmit */ 16329 if (tcp->tcp_suna != tcp->tcp_snxt) { 16330 clock_t time_to_wait; 16331 16332 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16333 if (!tcp->tcp_xmit_head) 16334 break; 16335 time_to_wait = lbolt - 16336 (clock_t)tcp->tcp_xmit_head->b_prev; 16337 time_to_wait = tcp->tcp_rto - 16338 TICK_TO_MSEC(time_to_wait); 16339 /* 16340 * If the timer fires too early, 1 clock tick earlier, 16341 * restart the timer. 16342 */ 16343 if (time_to_wait > msec_per_tick) { 16344 TCP_STAT(tcps, tcp_timer_fire_early); 16345 TCP_TIMER_RESTART(tcp, time_to_wait); 16346 return; 16347 } 16348 /* 16349 * When we probe zero windows, we force the swnd open. 16350 * If our peer acks with a closed window swnd will be 16351 * set to zero by tcp_rput(). As long as we are 16352 * receiving acks tcp_rput will 16353 * reset 'tcp_ms_we_have_waited' so as not to trip the 16354 * first and second interval actions. NOTE: the timer 16355 * interval is allowed to continue its exponential 16356 * backoff. 16357 */ 16358 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16359 if (tcp->tcp_debug) { 16360 (void) strlog(TCP_MOD_ID, 0, 1, 16361 SL_TRACE, "tcp_timer: zero win"); 16362 } 16363 } else { 16364 /* 16365 * After retransmission, we need to do 16366 * slow start. Set the ssthresh to one 16367 * half of current effective window and 16368 * cwnd to one MSS. Also reset 16369 * tcp_cwnd_cnt. 16370 * 16371 * Note that if tcp_ssthresh is reduced because 16372 * of ECN, do not reduce it again unless it is 16373 * already one window of data away (tcp_cwr 16374 * should then be cleared) or this is a 16375 * timeout for a retransmitted segment. 16376 */ 16377 uint32_t npkt; 16378 16379 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16380 npkt = ((tcp->tcp_timer_backoff ? 16381 tcp->tcp_cwnd_ssthresh : 16382 tcp->tcp_snxt - 16383 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16384 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16385 tcp->tcp_mss; 16386 } 16387 tcp->tcp_cwnd = tcp->tcp_mss; 16388 tcp->tcp_cwnd_cnt = 0; 16389 if (tcp->tcp_ecn_ok) { 16390 tcp->tcp_cwr = B_TRUE; 16391 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16392 tcp->tcp_ecn_cwr_sent = B_FALSE; 16393 } 16394 } 16395 break; 16396 } 16397 /* 16398 * We have something to send yet we cannot send. The 16399 * reason can be: 16400 * 16401 * 1. Zero send window: we need to do zero window probe. 16402 * 2. Zero cwnd: because of ECN, we need to "clock out 16403 * segments. 16404 * 3. SWS avoidance: receiver may have shrunk window, 16405 * reset our knowledge. 16406 * 16407 * Note that condition 2 can happen with either 1 or 16408 * 3. But 1 and 3 are exclusive. 16409 */ 16410 if (tcp->tcp_unsent != 0) { 16411 if (tcp->tcp_cwnd == 0) { 16412 /* 16413 * Set tcp_cwnd to 1 MSS so that a 16414 * new segment can be sent out. We 16415 * are "clocking out" new data when 16416 * the network is really congested. 16417 */ 16418 ASSERT(tcp->tcp_ecn_ok); 16419 tcp->tcp_cwnd = tcp->tcp_mss; 16420 } 16421 if (tcp->tcp_swnd == 0) { 16422 /* Extend window for zero window probe */ 16423 tcp->tcp_swnd++; 16424 tcp->tcp_zero_win_probe = B_TRUE; 16425 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16426 } else { 16427 /* 16428 * Handle timeout from sender SWS avoidance. 16429 * Reset our knowledge of the max send window 16430 * since the receiver might have reduced its 16431 * receive buffer. Avoid setting tcp_max_swnd 16432 * to one since that will essentially disable 16433 * the SWS checks. 16434 * 16435 * Note that since we don't have a SWS 16436 * state variable, if the timeout is set 16437 * for ECN but not for SWS, this 16438 * code will also be executed. This is 16439 * fine as tcp_max_swnd is updated 16440 * constantly and it will not affect 16441 * anything. 16442 */ 16443 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16444 } 16445 tcp_wput_data(tcp, NULL, B_FALSE); 16446 return; 16447 } 16448 /* Is there a FIN that needs to be to re retransmitted? */ 16449 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16450 !tcp->tcp_fin_acked) 16451 break; 16452 /* Nothing to do, return without restarting timer. */ 16453 TCP_STAT(tcps, tcp_timer_fire_miss); 16454 return; 16455 case TCPS_FIN_WAIT_2: 16456 /* 16457 * User closed the TCP endpoint and peer ACK'ed our FIN. 16458 * We waited some time for for peer's FIN, but it hasn't 16459 * arrived. We flush the connection now to avoid 16460 * case where the peer has rebooted. 16461 */ 16462 if (TCP_IS_DETACHED(tcp)) { 16463 (void) tcp_clean_death(tcp, 0, 23); 16464 } else { 16465 TCP_TIMER_RESTART(tcp, 16466 tcps->tcps_fin_wait_2_flush_interval); 16467 } 16468 return; 16469 case TCPS_TIME_WAIT: 16470 (void) tcp_clean_death(tcp, 0, 24); 16471 return; 16472 default: 16473 if (tcp->tcp_debug) { 16474 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16475 "tcp_timer: strange state (%d) %s", 16476 tcp->tcp_state, tcp_display(tcp, NULL, 16477 DISP_PORT_ONLY)); 16478 } 16479 return; 16480 } 16481 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16482 /* 16483 * For zero window probe, we need to send indefinitely, 16484 * unless we have not heard from the other side for some 16485 * time... 16486 */ 16487 if ((tcp->tcp_zero_win_probe == 0) || 16488 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16489 second_threshold)) { 16490 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16491 /* 16492 * If TCP is in SYN_RCVD state, send back a 16493 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16494 * should be zero in TCPS_SYN_RCVD state. 16495 */ 16496 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16497 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16498 "in SYN_RCVD", 16499 tcp, tcp->tcp_snxt, 16500 tcp->tcp_rnxt, TH_RST | TH_ACK); 16501 } 16502 (void) tcp_clean_death(tcp, 16503 tcp->tcp_client_errno ? 16504 tcp->tcp_client_errno : ETIMEDOUT, 25); 16505 return; 16506 } else { 16507 /* 16508 * Set tcp_ms_we_have_waited to second_threshold 16509 * so that in next timeout, we will do the above 16510 * check (lbolt - tcp_last_recv_time). This is 16511 * also to avoid overflow. 16512 * 16513 * We don't need to decrement tcp_timer_backoff 16514 * to avoid overflow because it will be decremented 16515 * later if new timeout value is greater than 16516 * tcp_rexmit_interval_max. In the case when 16517 * tcp_rexmit_interval_max is greater than 16518 * second_threshold, it means that we will wait 16519 * longer than second_threshold to send the next 16520 * window probe. 16521 */ 16522 tcp->tcp_ms_we_have_waited = second_threshold; 16523 } 16524 } else if (ms > first_threshold) { 16525 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16526 tcp->tcp_xmit_head != NULL) { 16527 tcp->tcp_xmit_head = 16528 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16529 } 16530 /* 16531 * We have been retransmitting for too long... The RTT 16532 * we calculated is probably incorrect. Reinitialize it. 16533 * Need to compensate for 0 tcp_rtt_sa. Reset 16534 * tcp_rtt_update so that we won't accidentally cache a 16535 * bad value. But only do this if this is not a zero 16536 * window probe. 16537 */ 16538 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16539 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16540 (tcp->tcp_rtt_sa >> 5); 16541 tcp->tcp_rtt_sa = 0; 16542 tcp_ip_notify(tcp); 16543 tcp->tcp_rtt_update = 0; 16544 } 16545 } 16546 tcp->tcp_timer_backoff++; 16547 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16548 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16549 tcps->tcps_rexmit_interval_min) { 16550 /* 16551 * This means the original RTO is tcp_rexmit_interval_min. 16552 * So we will use tcp_rexmit_interval_min as the RTO value 16553 * and do the backoff. 16554 */ 16555 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16556 } else { 16557 ms <<= tcp->tcp_timer_backoff; 16558 } 16559 if (ms > tcps->tcps_rexmit_interval_max) { 16560 ms = tcps->tcps_rexmit_interval_max; 16561 /* 16562 * ms is at max, decrement tcp_timer_backoff to avoid 16563 * overflow. 16564 */ 16565 tcp->tcp_timer_backoff--; 16566 } 16567 tcp->tcp_ms_we_have_waited += ms; 16568 if (tcp->tcp_zero_win_probe == 0) { 16569 tcp->tcp_rto = ms; 16570 } 16571 TCP_TIMER_RESTART(tcp, ms); 16572 /* 16573 * This is after a timeout and tcp_rto is backed off. Set 16574 * tcp_set_timer to 1 so that next time RTO is updated, we will 16575 * restart the timer with a correct value. 16576 */ 16577 tcp->tcp_set_timer = 1; 16578 mss = tcp->tcp_snxt - tcp->tcp_suna; 16579 if (mss > tcp->tcp_mss) 16580 mss = tcp->tcp_mss; 16581 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16582 mss = tcp->tcp_swnd; 16583 16584 if ((mp = tcp->tcp_xmit_head) != NULL) 16585 mp->b_prev = (mblk_t *)lbolt; 16586 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16587 B_TRUE); 16588 16589 /* 16590 * When slow start after retransmission begins, start with 16591 * this seq no. tcp_rexmit_max marks the end of special slow 16592 * start phase. tcp_snd_burst controls how many segments 16593 * can be sent because of an ack. 16594 */ 16595 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16596 tcp->tcp_snd_burst = TCP_CWND_SS; 16597 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16598 (tcp->tcp_unsent == 0)) { 16599 tcp->tcp_rexmit_max = tcp->tcp_fss; 16600 } else { 16601 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16602 } 16603 tcp->tcp_rexmit = B_TRUE; 16604 tcp->tcp_dupack_cnt = 0; 16605 16606 /* 16607 * Remove all rexmit SACK blk to start from fresh. 16608 */ 16609 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) 16610 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp); 16611 if (mp == NULL) { 16612 return; 16613 } 16614 /* 16615 * Attach credentials to retransmitted initial SYNs. 16616 * In theory we should use the credentials from the connect() 16617 * call to ensure that getpeerucred() on the peer will be correct. 16618 * But we assume that SYN's are not dropped for loopback connections. 16619 */ 16620 if (tcp->tcp_state == TCPS_SYN_SENT) { 16621 mblk_setcred(mp, CONN_CRED(tcp->tcp_connp), tcp->tcp_cpid); 16622 } 16623 16624 tcp->tcp_csuna = tcp->tcp_snxt; 16625 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16626 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16627 tcp_send_data(tcp, tcp->tcp_wq, mp); 16628 16629 } 16630 16631 static int 16632 tcp_do_unbind(conn_t *connp) 16633 { 16634 tcp_t *tcp = connp->conn_tcp; 16635 int error = 0; 16636 16637 switch (tcp->tcp_state) { 16638 case TCPS_BOUND: 16639 case TCPS_LISTEN: 16640 break; 16641 default: 16642 return (-TOUTSTATE); 16643 } 16644 16645 /* 16646 * Need to clean up all the eagers since after the unbind, segments 16647 * will no longer be delivered to this listener stream. 16648 */ 16649 mutex_enter(&tcp->tcp_eager_lock); 16650 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16651 tcp_eager_cleanup(tcp, 0); 16652 } 16653 mutex_exit(&tcp->tcp_eager_lock); 16654 16655 if (tcp->tcp_ipversion == IPV4_VERSION) { 16656 tcp->tcp_ipha->ipha_src = 0; 16657 } else { 16658 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16659 } 16660 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16661 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16662 tcp_bind_hash_remove(tcp); 16663 tcp->tcp_state = TCPS_IDLE; 16664 tcp->tcp_mdt = B_FALSE; 16665 16666 connp = tcp->tcp_connp; 16667 connp->conn_mdt_ok = B_FALSE; 16668 ipcl_hash_remove(connp); 16669 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16670 16671 return (error); 16672 } 16673 16674 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16675 static void 16676 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16677 { 16678 int error = tcp_do_unbind(tcp->tcp_connp); 16679 16680 if (error > 0) { 16681 tcp_err_ack(tcp, mp, TSYSERR, error); 16682 } else if (error < 0) { 16683 tcp_err_ack(tcp, mp, -error, 0); 16684 } else { 16685 /* Send M_FLUSH according to TPI */ 16686 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16687 16688 mp = mi_tpi_ok_ack_alloc(mp); 16689 putnext(tcp->tcp_rq, mp); 16690 } 16691 } 16692 16693 /* 16694 * Don't let port fall into the privileged range. 16695 * Since the extra privileged ports can be arbitrary we also 16696 * ensure that we exclude those from consideration. 16697 * tcp_g_epriv_ports is not sorted thus we loop over it until 16698 * there are no changes. 16699 * 16700 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16701 * but instead the code relies on: 16702 * - the fact that the address of the array and its size never changes 16703 * - the atomic assignment of the elements of the array 16704 * 16705 * Returns 0 if there are no more ports available. 16706 * 16707 * TS note: skip multilevel ports. 16708 */ 16709 static in_port_t 16710 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16711 { 16712 int i; 16713 boolean_t restart = B_FALSE; 16714 tcp_stack_t *tcps = tcp->tcp_tcps; 16715 16716 if (random && tcp_random_anon_port != 0) { 16717 (void) random_get_pseudo_bytes((uint8_t *)&port, 16718 sizeof (in_port_t)); 16719 /* 16720 * Unless changed by a sys admin, the smallest anon port 16721 * is 32768 and the largest anon port is 65535. It is 16722 * very likely (50%) for the random port to be smaller 16723 * than the smallest anon port. When that happens, 16724 * add port % (anon port range) to the smallest anon 16725 * port to get the random port. It should fall into the 16726 * valid anon port range. 16727 */ 16728 if (port < tcps->tcps_smallest_anon_port) { 16729 port = tcps->tcps_smallest_anon_port + 16730 port % (tcps->tcps_largest_anon_port - 16731 tcps->tcps_smallest_anon_port); 16732 } 16733 } 16734 16735 retry: 16736 if (port < tcps->tcps_smallest_anon_port) 16737 port = (in_port_t)tcps->tcps_smallest_anon_port; 16738 16739 if (port > tcps->tcps_largest_anon_port) { 16740 if (restart) 16741 return (0); 16742 restart = B_TRUE; 16743 port = (in_port_t)tcps->tcps_smallest_anon_port; 16744 } 16745 16746 if (port < tcps->tcps_smallest_nonpriv_port) 16747 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16748 16749 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16750 if (port == tcps->tcps_g_epriv_ports[i]) { 16751 port++; 16752 /* 16753 * Make sure whether the port is in the 16754 * valid range. 16755 */ 16756 goto retry; 16757 } 16758 } 16759 if (is_system_labeled() && 16760 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16761 IPPROTO_TCP, B_TRUE)) != 0) { 16762 port = i; 16763 goto retry; 16764 } 16765 return (port); 16766 } 16767 16768 /* 16769 * Return the next anonymous port in the privileged port range for 16770 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16771 * downwards. This is the same behavior as documented in the userland 16772 * library call rresvport(3N). 16773 * 16774 * TS note: skip multilevel ports. 16775 */ 16776 static in_port_t 16777 tcp_get_next_priv_port(const tcp_t *tcp) 16778 { 16779 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16780 in_port_t nextport; 16781 boolean_t restart = B_FALSE; 16782 tcp_stack_t *tcps = tcp->tcp_tcps; 16783 retry: 16784 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16785 next_priv_port >= IPPORT_RESERVED) { 16786 next_priv_port = IPPORT_RESERVED - 1; 16787 if (restart) 16788 return (0); 16789 restart = B_TRUE; 16790 } 16791 if (is_system_labeled() && 16792 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16793 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16794 next_priv_port = nextport; 16795 goto retry; 16796 } 16797 return (next_priv_port--); 16798 } 16799 16800 /* The write side r/w procedure. */ 16801 16802 #if CCS_STATS 16803 struct { 16804 struct { 16805 int64_t count, bytes; 16806 } tot, hit; 16807 } wrw_stats; 16808 #endif 16809 16810 /* 16811 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16812 * messages. 16813 */ 16814 /* ARGSUSED */ 16815 static void 16816 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16817 { 16818 conn_t *connp = (conn_t *)arg; 16819 tcp_t *tcp = connp->conn_tcp; 16820 queue_t *q = tcp->tcp_wq; 16821 16822 ASSERT(DB_TYPE(mp) != M_IOCTL); 16823 /* 16824 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16825 * Once the close starts, streamhead and sockfs will not let any data 16826 * packets come down (close ensures that there are no threads using the 16827 * queue and no new threads will come down) but since qprocsoff() 16828 * hasn't happened yet, a M_FLUSH or some non data message might 16829 * get reflected back (in response to our own FLUSHRW) and get 16830 * processed after tcp_close() is done. The conn would still be valid 16831 * because a ref would have added but we need to check the state 16832 * before actually processing the packet. 16833 */ 16834 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16835 freemsg(mp); 16836 return; 16837 } 16838 16839 switch (DB_TYPE(mp)) { 16840 case M_IOCDATA: 16841 tcp_wput_iocdata(tcp, mp); 16842 break; 16843 case M_FLUSH: 16844 tcp_wput_flush(tcp, mp); 16845 break; 16846 default: 16847 CALL_IP_WPUT(connp, q, mp); 16848 break; 16849 } 16850 } 16851 16852 /* 16853 * The TCP fast path write put procedure. 16854 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16855 */ 16856 /* ARGSUSED */ 16857 void 16858 tcp_output(void *arg, mblk_t *mp, void *arg2) 16859 { 16860 int len; 16861 int hdrlen; 16862 int plen; 16863 mblk_t *mp1; 16864 uchar_t *rptr; 16865 uint32_t snxt; 16866 tcph_t *tcph; 16867 struct datab *db; 16868 uint32_t suna; 16869 uint32_t mss; 16870 ipaddr_t *dst; 16871 ipaddr_t *src; 16872 uint32_t sum; 16873 int usable; 16874 conn_t *connp = (conn_t *)arg; 16875 tcp_t *tcp = connp->conn_tcp; 16876 uint32_t msize; 16877 tcp_stack_t *tcps = tcp->tcp_tcps; 16878 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16879 16880 /* 16881 * Try and ASSERT the minimum possible references on the 16882 * conn early enough. Since we are executing on write side, 16883 * the connection is obviously not detached and that means 16884 * there is a ref each for TCP and IP. Since we are behind 16885 * the squeue, the minimum references needed are 3. If the 16886 * conn is in classifier hash list, there should be an 16887 * extra ref for that (we check both the possibilities). 16888 */ 16889 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16890 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16891 16892 ASSERT(DB_TYPE(mp) == M_DATA); 16893 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16894 16895 mutex_enter(&tcp->tcp_non_sq_lock); 16896 tcp->tcp_squeue_bytes -= msize; 16897 mutex_exit(&tcp->tcp_non_sq_lock); 16898 16899 /* Check to see if this connection wants to be re-fused. */ 16900 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16901 if (tcp->tcp_ipversion == IPV4_VERSION) { 16902 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16903 &tcp->tcp_saved_tcph); 16904 } else { 16905 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16906 &tcp->tcp_saved_tcph); 16907 } 16908 } 16909 /* Bypass tcp protocol for fused tcp loopback */ 16910 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16911 return; 16912 16913 mss = tcp->tcp_mss; 16914 if (tcp->tcp_xmit_zc_clean) 16915 mp = tcp_zcopy_backoff(tcp, mp, 0); 16916 16917 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16918 len = (int)(mp->b_wptr - mp->b_rptr); 16919 16920 /* 16921 * Criteria for fast path: 16922 * 16923 * 1. no unsent data 16924 * 2. single mblk in request 16925 * 3. connection established 16926 * 4. data in mblk 16927 * 5. len <= mss 16928 * 6. no tcp_valid bits 16929 */ 16930 if ((tcp->tcp_unsent != 0) || 16931 (tcp->tcp_cork) || 16932 (mp->b_cont != NULL) || 16933 (tcp->tcp_state != TCPS_ESTABLISHED) || 16934 (len == 0) || 16935 (len > mss) || 16936 (tcp->tcp_valid_bits != 0)) { 16937 tcp_wput_data(tcp, mp, B_FALSE); 16938 return; 16939 } 16940 16941 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16942 ASSERT(tcp->tcp_fin_sent == 0); 16943 16944 /* queue new packet onto retransmission queue */ 16945 if (tcp->tcp_xmit_head == NULL) { 16946 tcp->tcp_xmit_head = mp; 16947 } else { 16948 tcp->tcp_xmit_last->b_cont = mp; 16949 } 16950 tcp->tcp_xmit_last = mp; 16951 tcp->tcp_xmit_tail = mp; 16952 16953 /* find out how much we can send */ 16954 /* BEGIN CSTYLED */ 16955 /* 16956 * un-acked usable 16957 * |--------------|-----------------| 16958 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16959 */ 16960 /* END CSTYLED */ 16961 16962 /* start sending from tcp_snxt */ 16963 snxt = tcp->tcp_snxt; 16964 16965 /* 16966 * Check to see if this connection has been idled for some 16967 * time and no ACK is expected. If it is, we need to slow 16968 * start again to get back the connection's "self-clock" as 16969 * described in VJ's paper. 16970 * 16971 * Refer to the comment in tcp_mss_set() for the calculation 16972 * of tcp_cwnd after idle. 16973 */ 16974 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16975 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16976 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16977 } 16978 16979 usable = tcp->tcp_swnd; /* tcp window size */ 16980 if (usable > tcp->tcp_cwnd) 16981 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16982 usable -= snxt; /* subtract stuff already sent */ 16983 suna = tcp->tcp_suna; 16984 usable += suna; 16985 /* usable can be < 0 if the congestion window is smaller */ 16986 if (len > usable) { 16987 /* Can't send complete M_DATA in one shot */ 16988 goto slow; 16989 } 16990 16991 mutex_enter(&tcp->tcp_non_sq_lock); 16992 if (tcp->tcp_flow_stopped && 16993 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16994 tcp_clrqfull(tcp); 16995 } 16996 mutex_exit(&tcp->tcp_non_sq_lock); 16997 16998 /* 16999 * determine if anything to send (Nagle). 17000 * 17001 * 1. len < tcp_mss (i.e. small) 17002 * 2. unacknowledged data present 17003 * 3. len < nagle limit 17004 * 4. last packet sent < nagle limit (previous packet sent) 17005 */ 17006 if ((len < mss) && (snxt != suna) && 17007 (len < (int)tcp->tcp_naglim) && 17008 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17009 /* 17010 * This was the first unsent packet and normally 17011 * mss < xmit_hiwater so there is no need to worry 17012 * about flow control. The next packet will go 17013 * through the flow control check in tcp_wput_data(). 17014 */ 17015 /* leftover work from above */ 17016 tcp->tcp_unsent = len; 17017 tcp->tcp_xmit_tail_unsent = len; 17018 17019 return; 17020 } 17021 17022 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17023 17024 if (snxt == suna) { 17025 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17026 } 17027 17028 /* we have always sent something */ 17029 tcp->tcp_rack_cnt = 0; 17030 17031 tcp->tcp_snxt = snxt + len; 17032 tcp->tcp_rack = tcp->tcp_rnxt; 17033 17034 if ((mp1 = dupb(mp)) == 0) 17035 goto no_memory; 17036 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17037 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17038 17039 /* adjust tcp header information */ 17040 tcph = tcp->tcp_tcph; 17041 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17042 17043 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17044 sum = (sum >> 16) + (sum & 0xFFFF); 17045 U16_TO_ABE16(sum, tcph->th_sum); 17046 17047 U32_TO_ABE32(snxt, tcph->th_seq); 17048 17049 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 17050 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 17051 BUMP_LOCAL(tcp->tcp_obsegs); 17052 17053 /* Update the latest receive window size in TCP header. */ 17054 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17055 tcph->th_win); 17056 17057 tcp->tcp_last_sent_len = (ushort_t)len; 17058 17059 plen = len + tcp->tcp_hdr_len; 17060 17061 if (tcp->tcp_ipversion == IPV4_VERSION) { 17062 tcp->tcp_ipha->ipha_length = htons(plen); 17063 } else { 17064 tcp->tcp_ip6h->ip6_plen = htons(plen - 17065 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17066 } 17067 17068 /* see if we need to allocate a mblk for the headers */ 17069 hdrlen = tcp->tcp_hdr_len; 17070 rptr = mp1->b_rptr - hdrlen; 17071 db = mp1->b_datap; 17072 if ((db->db_ref != 2) || rptr < db->db_base || 17073 (!OK_32PTR(rptr))) { 17074 /* NOTE: we assume allocb returns an OK_32PTR */ 17075 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17076 tcps->tcps_wroff_xtra, BPRI_MED); 17077 if (!mp) { 17078 freemsg(mp1); 17079 goto no_memory; 17080 } 17081 mp->b_cont = mp1; 17082 mp1 = mp; 17083 /* Leave room for Link Level header */ 17084 /* hdrlen = tcp->tcp_hdr_len; */ 17085 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 17086 mp1->b_wptr = &rptr[hdrlen]; 17087 } 17088 mp1->b_rptr = rptr; 17089 17090 /* Fill in the timestamp option. */ 17091 if (tcp->tcp_snd_ts_ok) { 17092 U32_TO_BE32((uint32_t)lbolt, 17093 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17094 U32_TO_BE32(tcp->tcp_ts_recent, 17095 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17096 } else { 17097 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17098 } 17099 17100 /* copy header into outgoing packet */ 17101 dst = (ipaddr_t *)rptr; 17102 src = (ipaddr_t *)tcp->tcp_iphc; 17103 dst[0] = src[0]; 17104 dst[1] = src[1]; 17105 dst[2] = src[2]; 17106 dst[3] = src[3]; 17107 dst[4] = src[4]; 17108 dst[5] = src[5]; 17109 dst[6] = src[6]; 17110 dst[7] = src[7]; 17111 dst[8] = src[8]; 17112 dst[9] = src[9]; 17113 if (hdrlen -= 40) { 17114 hdrlen >>= 2; 17115 dst += 10; 17116 src += 10; 17117 do { 17118 *dst++ = *src++; 17119 } while (--hdrlen); 17120 } 17121 17122 /* 17123 * Set the ECN info in the TCP header. Note that this 17124 * is not the template header. 17125 */ 17126 if (tcp->tcp_ecn_ok) { 17127 SET_ECT(tcp, rptr); 17128 17129 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17130 if (tcp->tcp_ecn_echo_on) 17131 tcph->th_flags[0] |= TH_ECE; 17132 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17133 tcph->th_flags[0] |= TH_CWR; 17134 tcp->tcp_ecn_cwr_sent = B_TRUE; 17135 } 17136 } 17137 17138 if (tcp->tcp_ip_forward_progress) { 17139 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17140 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17141 tcp->tcp_ip_forward_progress = B_FALSE; 17142 } 17143 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17144 return; 17145 17146 /* 17147 * If we ran out of memory, we pretend to have sent the packet 17148 * and that it was lost on the wire. 17149 */ 17150 no_memory: 17151 return; 17152 17153 slow: 17154 /* leftover work from above */ 17155 tcp->tcp_unsent = len; 17156 tcp->tcp_xmit_tail_unsent = len; 17157 tcp_wput_data(tcp, NULL, B_FALSE); 17158 } 17159 17160 /* ARGSUSED */ 17161 void 17162 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17163 { 17164 conn_t *connp = (conn_t *)arg; 17165 tcp_t *tcp = connp->conn_tcp; 17166 queue_t *q = tcp->tcp_rq; 17167 struct tcp_options *tcpopt; 17168 tcp_stack_t *tcps = tcp->tcp_tcps; 17169 17170 /* socket options */ 17171 uint_t sopp_flags; 17172 ssize_t sopp_rxhiwat; 17173 ssize_t sopp_maxblk; 17174 ushort_t sopp_wroff; 17175 ushort_t sopp_tail; 17176 ushort_t sopp_copyopt; 17177 17178 tcpopt = (struct tcp_options *)mp->b_rptr; 17179 17180 /* 17181 * Drop the eager's ref on the listener, that was placed when 17182 * this eager began life in tcp_conn_request. 17183 */ 17184 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17185 if (IPCL_IS_NONSTR(connp)) { 17186 /* Safe to free conn_ind message */ 17187 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17188 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17189 } 17190 17191 tcp->tcp_detached = B_FALSE; 17192 17193 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17194 /* 17195 * Someone blewoff the eager before we could finish 17196 * the accept. 17197 * 17198 * The only reason eager exists it because we put in 17199 * a ref on it when conn ind went up. We need to send 17200 * a disconnect indication up while the last reference 17201 * on the eager will be dropped by the squeue when we 17202 * return. 17203 */ 17204 ASSERT(tcp->tcp_listener == NULL); 17205 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17206 if (IPCL_IS_NONSTR(connp)) { 17207 ASSERT(tcp->tcp_issocket); 17208 (*connp->conn_upcalls->su_disconnected)( 17209 connp->conn_upper_handle, tcp->tcp_connid, 17210 ECONNREFUSED); 17211 freemsg(mp); 17212 } else { 17213 struct T_discon_ind *tdi; 17214 17215 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17216 /* 17217 * Let us reuse the incoming mblk to avoid 17218 * memory allocation failure problems. We know 17219 * that the size of the incoming mblk i.e. 17220 * stroptions is greater than sizeof 17221 * T_discon_ind. So the reallocb below can't 17222 * fail. 17223 */ 17224 freemsg(mp->b_cont); 17225 mp->b_cont = NULL; 17226 ASSERT(DB_REF(mp) == 1); 17227 mp = reallocb(mp, sizeof (struct T_discon_ind), 17228 B_FALSE); 17229 ASSERT(mp != NULL); 17230 DB_TYPE(mp) = M_PROTO; 17231 ((union T_primitives *)mp->b_rptr)->type = 17232 T_DISCON_IND; 17233 tdi = (struct T_discon_ind *)mp->b_rptr; 17234 if (tcp->tcp_issocket) { 17235 tdi->DISCON_reason = ECONNREFUSED; 17236 tdi->SEQ_number = 0; 17237 } else { 17238 tdi->DISCON_reason = ENOPROTOOPT; 17239 tdi->SEQ_number = 17240 tcp->tcp_conn_req_seqnum; 17241 } 17242 mp->b_wptr = mp->b_rptr + 17243 sizeof (struct T_discon_ind); 17244 putnext(q, mp); 17245 return; 17246 } 17247 } 17248 if (tcp->tcp_hard_binding) { 17249 tcp->tcp_hard_binding = B_FALSE; 17250 tcp->tcp_hard_bound = B_TRUE; 17251 } 17252 return; 17253 } 17254 17255 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17256 int boundif = tcpopt->to_boundif; 17257 uint_t len = sizeof (int); 17258 17259 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17260 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17261 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17262 } 17263 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17264 uint_t on = 1; 17265 uint_t len = sizeof (uint_t); 17266 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17267 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17268 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17269 } 17270 17271 /* 17272 * For a loopback connection with tcp_direct_sockfs on, note that 17273 * we don't have to protect tcp_rcv_list yet because synchronous 17274 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17275 * possibly race with us. 17276 */ 17277 17278 /* 17279 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17280 * properly. This is the first time we know of the acceptor' 17281 * queue. So we do it here. 17282 * 17283 * XXX 17284 */ 17285 if (tcp->tcp_rcv_list == NULL) { 17286 /* 17287 * Recv queue is empty, tcp_rwnd should not have changed. 17288 * That means it should be equal to the listener's tcp_rwnd. 17289 */ 17290 if (!IPCL_IS_NONSTR(connp)) 17291 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17292 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17293 } else { 17294 #ifdef DEBUG 17295 mblk_t *tmp; 17296 mblk_t *mp1; 17297 uint_t cnt = 0; 17298 17299 mp1 = tcp->tcp_rcv_list; 17300 while ((tmp = mp1) != NULL) { 17301 mp1 = tmp->b_next; 17302 cnt += msgdsize(tmp); 17303 } 17304 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17305 #endif 17306 /* There is some data, add them back to get the max. */ 17307 if (!IPCL_IS_NONSTR(connp)) 17308 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17309 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17310 } 17311 /* 17312 * This is the first time we run on the correct 17313 * queue after tcp_accept. So fix all the q parameters 17314 * here. 17315 */ 17316 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17317 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17318 17319 /* 17320 * Record the stream head's high water mark for this endpoint; 17321 * this is used for flow-control purposes. 17322 */ 17323 sopp_rxhiwat = tcp->tcp_fused ? 17324 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17325 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17326 17327 /* 17328 * Determine what write offset value to use depending on SACK and 17329 * whether the endpoint is fused or not. 17330 */ 17331 if (tcp->tcp_fused) { 17332 ASSERT(tcp->tcp_loopback); 17333 ASSERT(tcp->tcp_loopback_peer != NULL); 17334 /* 17335 * For fused tcp loopback, set the stream head's write 17336 * offset value to zero since we won't be needing any room 17337 * for TCP/IP headers. This would also improve performance 17338 * since it would reduce the amount of work done by kmem. 17339 * Non-fused tcp loopback case is handled separately below. 17340 */ 17341 sopp_wroff = 0; 17342 /* 17343 * Update the peer's transmit parameters according to 17344 * our recently calculated high water mark value. 17345 */ 17346 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17347 } else if (tcp->tcp_snd_sack_ok) { 17348 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17349 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17350 } else { 17351 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17352 tcps->tcps_wroff_xtra); 17353 } 17354 17355 /* 17356 * If this is endpoint is handling SSL, then reserve extra 17357 * offset and space at the end. 17358 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17359 * overriding the previous setting. The extra cost of signing and 17360 * encrypting multiple MSS-size records (12 of them with Ethernet), 17361 * instead of a single contiguous one by the stream head 17362 * largely outweighs the statistical reduction of ACKs, when 17363 * applicable. The peer will also save on decryption and verification 17364 * costs. 17365 */ 17366 if (tcp->tcp_kssl_ctx != NULL) { 17367 sopp_wroff += SSL3_WROFFSET; 17368 17369 sopp_flags |= SOCKOPT_TAIL; 17370 sopp_tail = SSL3_MAX_TAIL_LEN; 17371 17372 sopp_flags |= SOCKOPT_ZCOPY; 17373 sopp_copyopt = ZCVMUNSAFE; 17374 17375 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17376 } 17377 17378 /* Send the options up */ 17379 if (IPCL_IS_NONSTR(connp)) { 17380 struct sock_proto_props sopp; 17381 17382 sopp.sopp_flags = sopp_flags; 17383 sopp.sopp_wroff = sopp_wroff; 17384 sopp.sopp_maxblk = sopp_maxblk; 17385 sopp.sopp_rxhiwat = sopp_rxhiwat; 17386 if (sopp_flags & SOCKOPT_TAIL) { 17387 ASSERT(tcp->tcp_kssl_ctx != NULL); 17388 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17389 sopp.sopp_tail = sopp_tail; 17390 sopp.sopp_zcopyflag = sopp_copyopt; 17391 } 17392 (*connp->conn_upcalls->su_set_proto_props) 17393 (connp->conn_upper_handle, &sopp); 17394 } else { 17395 struct stroptions *stropt; 17396 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17397 if (stropt_mp == NULL) { 17398 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17399 return; 17400 } 17401 DB_TYPE(stropt_mp) = M_SETOPTS; 17402 stropt = (struct stroptions *)stropt_mp->b_rptr; 17403 stropt_mp->b_wptr += sizeof (struct stroptions); 17404 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 17405 stropt->so_hiwat = sopp_rxhiwat; 17406 stropt->so_wroff = sopp_wroff; 17407 stropt->so_maxblk = sopp_maxblk; 17408 17409 if (sopp_flags & SOCKOPT_TAIL) { 17410 ASSERT(tcp->tcp_kssl_ctx != NULL); 17411 17412 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17413 stropt->so_tail = sopp_tail; 17414 stropt->so_copyopt = sopp_copyopt; 17415 } 17416 17417 /* Send the options up */ 17418 putnext(q, stropt_mp); 17419 } 17420 17421 freemsg(mp); 17422 /* 17423 * Pass up any data and/or a fin that has been received. 17424 * 17425 * Adjust receive window in case it had decreased 17426 * (because there is data <=> tcp_rcv_list != NULL) 17427 * while the connection was detached. Note that 17428 * in case the eager was flow-controlled, w/o this 17429 * code, the rwnd may never open up again! 17430 */ 17431 if (tcp->tcp_rcv_list != NULL) { 17432 if (IPCL_IS_NONSTR(connp)) { 17433 mblk_t *mp; 17434 int space_left; 17435 int error; 17436 boolean_t push = B_TRUE; 17437 17438 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17439 (connp->conn_upper_handle, NULL, 0, 0, &error, 17440 &push) >= 0) { 17441 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17442 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17443 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17444 tcp_xmit_ctl(NULL, 17445 tcp, (tcp->tcp_swnd == 0) ? 17446 tcp->tcp_suna : tcp->tcp_snxt, 17447 tcp->tcp_rnxt, TH_ACK); 17448 } 17449 } 17450 while ((mp = tcp->tcp_rcv_list) != NULL) { 17451 push = B_TRUE; 17452 tcp->tcp_rcv_list = mp->b_next; 17453 mp->b_next = NULL; 17454 space_left = (*connp->conn_upcalls->su_recv) 17455 (connp->conn_upper_handle, mp, msgdsize(mp), 17456 0, &error, &push); 17457 if (space_left < 0) { 17458 /* 17459 * We should never be in middle of a 17460 * fallback, the squeue guarantees that. 17461 */ 17462 ASSERT(error != EOPNOTSUPP); 17463 } 17464 } 17465 tcp->tcp_rcv_last_head = NULL; 17466 tcp->tcp_rcv_last_tail = NULL; 17467 tcp->tcp_rcv_cnt = 0; 17468 } else { 17469 /* We drain directly in case of fused tcp loopback */ 17470 17471 if (!tcp->tcp_fused && canputnext(q)) { 17472 tcp->tcp_rwnd = q->q_hiwat; 17473 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17474 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17475 tcp_xmit_ctl(NULL, 17476 tcp, (tcp->tcp_swnd == 0) ? 17477 tcp->tcp_suna : tcp->tcp_snxt, 17478 tcp->tcp_rnxt, TH_ACK); 17479 } 17480 } 17481 17482 (void) tcp_rcv_drain(tcp); 17483 } 17484 17485 /* 17486 * For fused tcp loopback, back-enable peer endpoint 17487 * if it's currently flow-controlled. 17488 */ 17489 if (tcp->tcp_fused) { 17490 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17491 17492 ASSERT(peer_tcp != NULL); 17493 ASSERT(peer_tcp->tcp_fused); 17494 /* 17495 * In order to change the peer's tcp_flow_stopped, 17496 * we need to take locks for both end points. The 17497 * highest address is taken first. 17498 */ 17499 if (peer_tcp > tcp) { 17500 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17501 mutex_enter(&tcp->tcp_non_sq_lock); 17502 } else { 17503 mutex_enter(&tcp->tcp_non_sq_lock); 17504 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17505 } 17506 if (peer_tcp->tcp_flow_stopped) { 17507 tcp_clrqfull(peer_tcp); 17508 TCP_STAT(tcps, tcp_fusion_backenabled); 17509 } 17510 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17511 mutex_exit(&tcp->tcp_non_sq_lock); 17512 } 17513 } 17514 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17515 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17516 tcp->tcp_ordrel_done = B_TRUE; 17517 if (IPCL_IS_NONSTR(connp)) { 17518 ASSERT(tcp->tcp_ordrel_mp == NULL); 17519 (*connp->conn_upcalls->su_opctl)( 17520 connp->conn_upper_handle, 17521 SOCK_OPCTL_SHUT_RECV, 0); 17522 } else { 17523 mp = tcp->tcp_ordrel_mp; 17524 tcp->tcp_ordrel_mp = NULL; 17525 putnext(q, mp); 17526 } 17527 } 17528 if (tcp->tcp_hard_binding) { 17529 tcp->tcp_hard_binding = B_FALSE; 17530 tcp->tcp_hard_bound = B_TRUE; 17531 } 17532 17533 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17534 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17535 tcp->tcp_loopback_peer != NULL && 17536 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17537 tcp_fuse_syncstr_enable_pair(tcp); 17538 } 17539 17540 if (tcp->tcp_ka_enabled) { 17541 tcp->tcp_ka_last_intrvl = 0; 17542 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17543 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17544 } 17545 17546 /* 17547 * At this point, eager is fully established and will 17548 * have the following references - 17549 * 17550 * 2 references for connection to exist (1 for TCP and 1 for IP). 17551 * 1 reference for the squeue which will be dropped by the squeue as 17552 * soon as this function returns. 17553 * There will be 1 additonal reference for being in classifier 17554 * hash list provided something bad hasn't happened. 17555 */ 17556 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17557 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17558 } 17559 17560 /* 17561 * The function called through squeue to get behind listener's perimeter to 17562 * send a deffered conn_ind. 17563 */ 17564 /* ARGSUSED */ 17565 void 17566 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17567 { 17568 conn_t *connp = (conn_t *)arg; 17569 tcp_t *listener = connp->conn_tcp; 17570 struct T_conn_ind *conn_ind; 17571 tcp_t *tcp; 17572 17573 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17574 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17575 conn_ind->OPT_length); 17576 17577 if (listener->tcp_state != TCPS_LISTEN) { 17578 /* 17579 * If listener has closed, it would have caused a 17580 * a cleanup/blowoff to happen for the eager, so 17581 * we don't need to do anything more. 17582 */ 17583 freemsg(mp); 17584 return; 17585 } 17586 17587 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17588 } 17589 17590 /* ARGSUSED */ 17591 static int 17592 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17593 { 17594 tcp_t *listener, *eager; 17595 mblk_t *opt_mp; 17596 struct tcp_options *tcpopt; 17597 17598 listener = lconnp->conn_tcp; 17599 ASSERT(listener->tcp_state == TCPS_LISTEN); 17600 eager = econnp->conn_tcp; 17601 ASSERT(eager->tcp_listener != NULL); 17602 17603 ASSERT(eager->tcp_rq != NULL); 17604 17605 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17606 if (opt_mp == NULL) { 17607 return (-TPROTO); 17608 } 17609 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17610 eager->tcp_issocket = B_TRUE; 17611 17612 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17613 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17614 ASSERT(econnp->conn_netstack == 17615 listener->tcp_connp->conn_netstack); 17616 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17617 17618 /* Put the ref for IP */ 17619 CONN_INC_REF(econnp); 17620 17621 /* 17622 * We should have minimum of 3 references on the conn 17623 * at this point. One each for TCP and IP and one for 17624 * the T_conn_ind that was sent up when the 3-way handshake 17625 * completed. In the normal case we would also have another 17626 * reference (making a total of 4) for the conn being in the 17627 * classifier hash list. However the eager could have received 17628 * an RST subsequently and tcp_closei_local could have removed 17629 * the eager from the classifier hash list, hence we can't 17630 * assert that reference. 17631 */ 17632 ASSERT(econnp->conn_ref >= 3); 17633 17634 opt_mp->b_datap->db_type = M_SETOPTS; 17635 opt_mp->b_wptr += sizeof (struct tcp_options); 17636 17637 /* 17638 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17639 * from listener to acceptor. 17640 */ 17641 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17642 tcpopt->to_flags = 0; 17643 17644 if (listener->tcp_bound_if != 0) { 17645 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17646 tcpopt->to_boundif = listener->tcp_bound_if; 17647 } 17648 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17649 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17650 } 17651 17652 mutex_enter(&listener->tcp_eager_lock); 17653 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17654 17655 tcp_t *tail; 17656 tcp_t *tcp; 17657 mblk_t *mp1; 17658 17659 tcp = listener->tcp_eager_prev_q0; 17660 /* 17661 * listener->tcp_eager_prev_q0 points to the TAIL of the 17662 * deferred T_conn_ind queue. We need to get to the head 17663 * of the queue in order to send up T_conn_ind the same 17664 * order as how the 3WHS is completed. 17665 */ 17666 while (tcp != listener) { 17667 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17668 !tcp->tcp_kssl_pending) 17669 break; 17670 else 17671 tcp = tcp->tcp_eager_prev_q0; 17672 } 17673 /* None of the pending eagers can be sent up now */ 17674 if (tcp == listener) 17675 goto no_more_eagers; 17676 17677 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17678 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17679 /* Move from q0 to q */ 17680 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17681 listener->tcp_conn_req_cnt_q0--; 17682 listener->tcp_conn_req_cnt_q++; 17683 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17684 tcp->tcp_eager_prev_q0; 17685 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17686 tcp->tcp_eager_next_q0; 17687 tcp->tcp_eager_prev_q0 = NULL; 17688 tcp->tcp_eager_next_q0 = NULL; 17689 tcp->tcp_conn_def_q0 = B_FALSE; 17690 17691 /* Make sure the tcp isn't in the list of droppables */ 17692 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17693 tcp->tcp_eager_prev_drop_q0 == NULL); 17694 17695 /* 17696 * Insert at end of the queue because sockfs sends 17697 * down T_CONN_RES in chronological order. Leaving 17698 * the older conn indications at front of the queue 17699 * helps reducing search time. 17700 */ 17701 tail = listener->tcp_eager_last_q; 17702 if (tail != NULL) { 17703 tail->tcp_eager_next_q = tcp; 17704 } else { 17705 listener->tcp_eager_next_q = tcp; 17706 } 17707 listener->tcp_eager_last_q = tcp; 17708 tcp->tcp_eager_next_q = NULL; 17709 17710 /* Need to get inside the listener perimeter */ 17711 CONN_INC_REF(listener->tcp_connp); 17712 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17713 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17714 SQTAG_TCP_SEND_PENDING); 17715 } 17716 no_more_eagers: 17717 tcp_eager_unlink(eager); 17718 mutex_exit(&listener->tcp_eager_lock); 17719 17720 /* 17721 * At this point, the eager is detached from the listener 17722 * but we still have an extra refs on eager (apart from the 17723 * usual tcp references). The ref was placed in tcp_rput_data 17724 * before sending the conn_ind in tcp_send_conn_ind. 17725 * The ref will be dropped in tcp_accept_finish(). 17726 */ 17727 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17728 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17729 return (0); 17730 } 17731 17732 int 17733 tcp_accept(sock_lower_handle_t lproto_handle, 17734 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17735 cred_t *cr) 17736 { 17737 conn_t *lconnp, *econnp; 17738 tcp_t *listener, *eager; 17739 tcp_stack_t *tcps; 17740 17741 lconnp = (conn_t *)lproto_handle; 17742 listener = lconnp->conn_tcp; 17743 ASSERT(listener->tcp_state == TCPS_LISTEN); 17744 econnp = (conn_t *)eproto_handle; 17745 eager = econnp->conn_tcp; 17746 ASSERT(eager->tcp_listener != NULL); 17747 tcps = eager->tcp_tcps; 17748 17749 /* 17750 * It is OK to manipulate these fields outside the eager's squeue 17751 * because they will not start being used until tcp_accept_finish 17752 * has been called. 17753 */ 17754 ASSERT(lconnp->conn_upper_handle != NULL); 17755 ASSERT(econnp->conn_upper_handle == NULL); 17756 econnp->conn_upper_handle = sock_handle; 17757 econnp->conn_upcalls = lconnp->conn_upcalls; 17758 ASSERT(IPCL_IS_NONSTR(econnp)); 17759 /* 17760 * Create helper stream if it is a non-TPI TCP connection. 17761 */ 17762 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17763 ip1dbg(("tcp_accept: create of IP helper stream" 17764 " failed\n")); 17765 return (EPROTO); 17766 } 17767 eager->tcp_rq = econnp->conn_rq; 17768 eager->tcp_wq = econnp->conn_wq; 17769 17770 ASSERT(eager->tcp_rq != NULL); 17771 17772 return (tcp_accept_common(lconnp, econnp, cr)); 17773 } 17774 17775 17776 /* 17777 * This is the STREAMS entry point for T_CONN_RES coming down on 17778 * Acceptor STREAM when sockfs listener does accept processing. 17779 * Read the block comment on top of tcp_conn_request(). 17780 */ 17781 void 17782 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17783 { 17784 queue_t *rq = RD(q); 17785 struct T_conn_res *conn_res; 17786 tcp_t *eager; 17787 tcp_t *listener; 17788 struct T_ok_ack *ok; 17789 t_scalar_t PRIM_type; 17790 conn_t *econnp; 17791 cred_t *cr; 17792 17793 ASSERT(DB_TYPE(mp) == M_PROTO); 17794 17795 /* 17796 * All Solaris components should pass a db_credp 17797 * for this TPI message, hence we ASSERT. 17798 * But in case there is some other M_PROTO that looks 17799 * like a TPI message sent by some other kernel 17800 * component, we check and return an error. 17801 */ 17802 cr = msg_getcred(mp, NULL); 17803 ASSERT(cr != NULL); 17804 if (cr == NULL) { 17805 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17806 if (mp != NULL) 17807 putnext(rq, mp); 17808 return; 17809 } 17810 conn_res = (struct T_conn_res *)mp->b_rptr; 17811 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17812 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17813 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17814 if (mp != NULL) 17815 putnext(rq, mp); 17816 return; 17817 } 17818 switch (conn_res->PRIM_type) { 17819 case O_T_CONN_RES: 17820 case T_CONN_RES: 17821 /* 17822 * We pass up an err ack if allocb fails. This will 17823 * cause sockfs to issue a T_DISCON_REQ which will cause 17824 * tcp_eager_blowoff to be called. sockfs will then call 17825 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17826 * we need to do the allocb up here because we have to 17827 * make sure rq->q_qinfo->qi_qclose still points to the 17828 * correct function (tcp_tpi_close_accept) in case allocb 17829 * fails. 17830 */ 17831 bcopy(mp->b_rptr + conn_res->OPT_offset, 17832 &eager, conn_res->OPT_length); 17833 PRIM_type = conn_res->PRIM_type; 17834 mp->b_datap->db_type = M_PCPROTO; 17835 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17836 ok = (struct T_ok_ack *)mp->b_rptr; 17837 ok->PRIM_type = T_OK_ACK; 17838 ok->CORRECT_prim = PRIM_type; 17839 econnp = eager->tcp_connp; 17840 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17841 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17842 eager->tcp_rq = rq; 17843 eager->tcp_wq = q; 17844 rq->q_ptr = econnp; 17845 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17846 q->q_ptr = econnp; 17847 q->q_qinfo = &tcp_winit; 17848 listener = eager->tcp_listener; 17849 17850 if (tcp_accept_common(listener->tcp_connp, 17851 econnp, cr) < 0) { 17852 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17853 if (mp != NULL) 17854 putnext(rq, mp); 17855 return; 17856 } 17857 17858 /* 17859 * Send the new local address also up to sockfs. There 17860 * should already be enough space in the mp that came 17861 * down from soaccept(). 17862 */ 17863 if (eager->tcp_family == AF_INET) { 17864 sin_t *sin; 17865 17866 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17867 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17868 sin = (sin_t *)mp->b_wptr; 17869 mp->b_wptr += sizeof (sin_t); 17870 sin->sin_family = AF_INET; 17871 sin->sin_port = eager->tcp_lport; 17872 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17873 } else { 17874 sin6_t *sin6; 17875 17876 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17877 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17878 sin6 = (sin6_t *)mp->b_wptr; 17879 mp->b_wptr += sizeof (sin6_t); 17880 sin6->sin6_family = AF_INET6; 17881 sin6->sin6_port = eager->tcp_lport; 17882 if (eager->tcp_ipversion == IPV4_VERSION) { 17883 sin6->sin6_flowinfo = 0; 17884 IN6_IPADDR_TO_V4MAPPED( 17885 eager->tcp_ipha->ipha_src, 17886 &sin6->sin6_addr); 17887 } else { 17888 ASSERT(eager->tcp_ip6h != NULL); 17889 sin6->sin6_flowinfo = 17890 eager->tcp_ip6h->ip6_vcf & 17891 ~IPV6_VERS_AND_FLOW_MASK; 17892 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17893 } 17894 sin6->sin6_scope_id = 0; 17895 sin6->__sin6_src_id = 0; 17896 } 17897 17898 putnext(rq, mp); 17899 return; 17900 default: 17901 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17902 if (mp != NULL) 17903 putnext(rq, mp); 17904 return; 17905 } 17906 } 17907 17908 static int 17909 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17910 { 17911 sin_t *sin = (sin_t *)sa; 17912 sin6_t *sin6 = (sin6_t *)sa; 17913 17914 switch (tcp->tcp_family) { 17915 case AF_INET: 17916 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17917 17918 if (*salenp < sizeof (sin_t)) 17919 return (EINVAL); 17920 17921 *sin = sin_null; 17922 sin->sin_family = AF_INET; 17923 if (tcp->tcp_state >= TCPS_BOUND) { 17924 sin->sin_port = tcp->tcp_lport; 17925 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17926 } 17927 *salenp = sizeof (sin_t); 17928 break; 17929 17930 case AF_INET6: 17931 if (*salenp < sizeof (sin6_t)) 17932 return (EINVAL); 17933 17934 *sin6 = sin6_null; 17935 sin6->sin6_family = AF_INET6; 17936 if (tcp->tcp_state >= TCPS_BOUND) { 17937 sin6->sin6_port = tcp->tcp_lport; 17938 if (tcp->tcp_ipversion == IPV4_VERSION) { 17939 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17940 &sin6->sin6_addr); 17941 } else { 17942 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17943 } 17944 } 17945 *salenp = sizeof (sin6_t); 17946 break; 17947 } 17948 17949 return (0); 17950 } 17951 17952 static int 17953 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17954 { 17955 sin_t *sin = (sin_t *)sa; 17956 sin6_t *sin6 = (sin6_t *)sa; 17957 17958 if (tcp->tcp_state < TCPS_SYN_RCVD) 17959 return (ENOTCONN); 17960 17961 switch (tcp->tcp_family) { 17962 case AF_INET: 17963 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17964 17965 if (*salenp < sizeof (sin_t)) 17966 return (EINVAL); 17967 17968 *sin = sin_null; 17969 sin->sin_family = AF_INET; 17970 sin->sin_port = tcp->tcp_fport; 17971 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17972 sin->sin_addr.s_addr); 17973 *salenp = sizeof (sin_t); 17974 break; 17975 17976 case AF_INET6: 17977 if (*salenp < sizeof (sin6_t)) 17978 return (EINVAL); 17979 17980 *sin6 = sin6_null; 17981 sin6->sin6_family = AF_INET6; 17982 sin6->sin6_port = tcp->tcp_fport; 17983 sin6->sin6_addr = tcp->tcp_remote_v6; 17984 if (tcp->tcp_ipversion == IPV6_VERSION) { 17985 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17986 ~IPV6_VERS_AND_FLOW_MASK; 17987 } 17988 *salenp = sizeof (sin6_t); 17989 break; 17990 } 17991 17992 return (0); 17993 } 17994 17995 /* 17996 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17997 */ 17998 static void 17999 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 18000 { 18001 void *data; 18002 mblk_t *datamp = mp->b_cont; 18003 tcp_t *tcp = Q_TO_TCP(q); 18004 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 18005 18006 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 18007 cmdp->cb_error = EPROTO; 18008 qreply(q, mp); 18009 return; 18010 } 18011 18012 data = datamp->b_rptr; 18013 18014 switch (cmdp->cb_cmd) { 18015 case TI_GETPEERNAME: 18016 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 18017 break; 18018 case TI_GETMYNAME: 18019 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 18020 break; 18021 default: 18022 cmdp->cb_error = EINVAL; 18023 break; 18024 } 18025 18026 qreply(q, mp); 18027 } 18028 18029 void 18030 tcp_wput(queue_t *q, mblk_t *mp) 18031 { 18032 conn_t *connp = Q_TO_CONN(q); 18033 tcp_t *tcp; 18034 void (*output_proc)(); 18035 t_scalar_t type; 18036 uchar_t *rptr; 18037 struct iocblk *iocp; 18038 size_t size; 18039 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 18040 18041 ASSERT(connp->conn_ref >= 2); 18042 18043 switch (DB_TYPE(mp)) { 18044 case M_DATA: 18045 tcp = connp->conn_tcp; 18046 ASSERT(tcp != NULL); 18047 18048 size = msgdsize(mp); 18049 18050 mutex_enter(&tcp->tcp_non_sq_lock); 18051 tcp->tcp_squeue_bytes += size; 18052 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 18053 tcp_setqfull(tcp); 18054 } 18055 mutex_exit(&tcp->tcp_non_sq_lock); 18056 18057 CONN_INC_REF(connp); 18058 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 18059 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 18060 return; 18061 18062 case M_CMD: 18063 tcp_wput_cmdblk(q, mp); 18064 return; 18065 18066 case M_PROTO: 18067 case M_PCPROTO: 18068 /* 18069 * if it is a snmp message, don't get behind the squeue 18070 */ 18071 tcp = connp->conn_tcp; 18072 rptr = mp->b_rptr; 18073 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 18074 type = ((union T_primitives *)rptr)->type; 18075 } else { 18076 if (tcp->tcp_debug) { 18077 (void) strlog(TCP_MOD_ID, 0, 1, 18078 SL_ERROR|SL_TRACE, 18079 "tcp_wput_proto, dropping one..."); 18080 } 18081 freemsg(mp); 18082 return; 18083 } 18084 if (type == T_SVR4_OPTMGMT_REQ) { 18085 /* 18086 * All Solaris components should pass a db_credp 18087 * for this TPI message, hence we ASSERT. 18088 * But in case there is some other M_PROTO that looks 18089 * like a TPI message sent by some other kernel 18090 * component, we check and return an error. 18091 */ 18092 cred_t *cr = msg_getcred(mp, NULL); 18093 18094 ASSERT(cr != NULL); 18095 if (cr == NULL) { 18096 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 18097 return; 18098 } 18099 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 18100 cr)) { 18101 /* 18102 * This was a SNMP request 18103 */ 18104 return; 18105 } else { 18106 output_proc = tcp_wput_proto; 18107 } 18108 } else { 18109 output_proc = tcp_wput_proto; 18110 } 18111 break; 18112 case M_IOCTL: 18113 /* 18114 * Most ioctls can be processed right away without going via 18115 * squeues - process them right here. Those that do require 18116 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 18117 * are processed by tcp_wput_ioctl(). 18118 */ 18119 iocp = (struct iocblk *)mp->b_rptr; 18120 tcp = connp->conn_tcp; 18121 18122 switch (iocp->ioc_cmd) { 18123 case TCP_IOC_ABORT_CONN: 18124 tcp_ioctl_abort_conn(q, mp); 18125 return; 18126 case TI_GETPEERNAME: 18127 case TI_GETMYNAME: 18128 mi_copyin(q, mp, NULL, 18129 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 18130 return; 18131 case ND_SET: 18132 /* nd_getset does the necessary checks */ 18133 case ND_GET: 18134 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 18135 CALL_IP_WPUT(connp, q, mp); 18136 return; 18137 } 18138 qreply(q, mp); 18139 return; 18140 case TCP_IOC_DEFAULT_Q: 18141 /* 18142 * Wants to be the default wq. Check the credentials 18143 * first, the rest is executed via squeue. 18144 */ 18145 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 18146 iocp->ioc_error = EPERM; 18147 iocp->ioc_count = 0; 18148 mp->b_datap->db_type = M_IOCACK; 18149 qreply(q, mp); 18150 return; 18151 } 18152 output_proc = tcp_wput_ioctl; 18153 break; 18154 default: 18155 output_proc = tcp_wput_ioctl; 18156 break; 18157 } 18158 break; 18159 default: 18160 output_proc = tcp_wput_nondata; 18161 break; 18162 } 18163 18164 CONN_INC_REF(connp); 18165 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18166 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18167 } 18168 18169 /* 18170 * Initial STREAMS write side put() procedure for sockets. It tries to 18171 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18172 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18173 * are handled by tcp_wput() as usual. 18174 * 18175 * All further messages will also be handled by tcp_wput() because we cannot 18176 * be sure that the above short cut is safe later. 18177 */ 18178 static void 18179 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18180 { 18181 conn_t *connp = Q_TO_CONN(wq); 18182 tcp_t *tcp = connp->conn_tcp; 18183 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18184 18185 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18186 wq->q_qinfo = &tcp_winit; 18187 18188 ASSERT(IPCL_IS_TCP(connp)); 18189 ASSERT(TCP_IS_SOCKET(tcp)); 18190 18191 if (DB_TYPE(mp) == M_PCPROTO && 18192 MBLKL(mp) == sizeof (struct T_capability_req) && 18193 car->PRIM_type == T_CAPABILITY_REQ) { 18194 tcp_capability_req(tcp, mp); 18195 return; 18196 } 18197 18198 tcp_wput(wq, mp); 18199 } 18200 18201 /* ARGSUSED */ 18202 static void 18203 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18204 { 18205 #ifdef DEBUG 18206 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18207 #endif 18208 freemsg(mp); 18209 } 18210 18211 static boolean_t 18212 tcp_zcopy_check(tcp_t *tcp) 18213 { 18214 conn_t *connp = tcp->tcp_connp; 18215 ire_t *ire; 18216 boolean_t zc_enabled = B_FALSE; 18217 tcp_stack_t *tcps = tcp->tcp_tcps; 18218 18219 if (do_tcpzcopy == 2) 18220 zc_enabled = B_TRUE; 18221 else if (tcp->tcp_ipversion == IPV4_VERSION && 18222 IPCL_IS_CONNECTED(connp) && 18223 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18224 connp->conn_dontroute == 0 && 18225 !connp->conn_nexthop_set && 18226 connp->conn_outgoing_ill == NULL && 18227 do_tcpzcopy == 1) { 18228 /* 18229 * the checks above closely resemble the fast path checks 18230 * in tcp_send_data(). 18231 */ 18232 mutex_enter(&connp->conn_lock); 18233 ire = connp->conn_ire_cache; 18234 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18235 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18236 IRE_REFHOLD(ire); 18237 if (ire->ire_stq != NULL) { 18238 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18239 18240 zc_enabled = ill && (ill->ill_capabilities & 18241 ILL_CAPAB_ZEROCOPY) && 18242 (ill->ill_zerocopy_capab-> 18243 ill_zerocopy_flags != 0); 18244 } 18245 IRE_REFRELE(ire); 18246 } 18247 mutex_exit(&connp->conn_lock); 18248 } 18249 tcp->tcp_snd_zcopy_on = zc_enabled; 18250 if (!TCP_IS_DETACHED(tcp)) { 18251 if (zc_enabled) { 18252 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18253 ZCVMSAFE); 18254 TCP_STAT(tcps, tcp_zcopy_on); 18255 } else { 18256 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18257 ZCVMUNSAFE); 18258 TCP_STAT(tcps, tcp_zcopy_off); 18259 } 18260 } 18261 return (zc_enabled); 18262 } 18263 18264 static mblk_t * 18265 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18266 { 18267 tcp_stack_t *tcps = tcp->tcp_tcps; 18268 18269 if (do_tcpzcopy == 2) 18270 return (bp); 18271 else if (tcp->tcp_snd_zcopy_on) { 18272 tcp->tcp_snd_zcopy_on = B_FALSE; 18273 if (!TCP_IS_DETACHED(tcp)) { 18274 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18275 ZCVMUNSAFE); 18276 TCP_STAT(tcps, tcp_zcopy_disable); 18277 } 18278 } 18279 return (tcp_zcopy_backoff(tcp, bp, 0)); 18280 } 18281 18282 /* 18283 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18284 * the original desballoca'ed segmapped mblk. 18285 */ 18286 static mblk_t * 18287 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18288 { 18289 mblk_t *head, *tail, *nbp; 18290 tcp_stack_t *tcps = tcp->tcp_tcps; 18291 18292 if (IS_VMLOANED_MBLK(bp)) { 18293 TCP_STAT(tcps, tcp_zcopy_backoff); 18294 if ((head = copyb(bp)) == NULL) { 18295 /* fail to backoff; leave it for the next backoff */ 18296 tcp->tcp_xmit_zc_clean = B_FALSE; 18297 return (bp); 18298 } 18299 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18300 if (fix_xmitlist) 18301 tcp_zcopy_notify(tcp); 18302 else 18303 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18304 } 18305 nbp = bp->b_cont; 18306 if (fix_xmitlist) { 18307 head->b_prev = bp->b_prev; 18308 head->b_next = bp->b_next; 18309 if (tcp->tcp_xmit_tail == bp) 18310 tcp->tcp_xmit_tail = head; 18311 } 18312 bp->b_next = NULL; 18313 bp->b_prev = NULL; 18314 freeb(bp); 18315 } else { 18316 head = bp; 18317 nbp = bp->b_cont; 18318 } 18319 tail = head; 18320 while (nbp) { 18321 if (IS_VMLOANED_MBLK(nbp)) { 18322 TCP_STAT(tcps, tcp_zcopy_backoff); 18323 if ((tail->b_cont = copyb(nbp)) == NULL) { 18324 tcp->tcp_xmit_zc_clean = B_FALSE; 18325 tail->b_cont = nbp; 18326 return (head); 18327 } 18328 tail = tail->b_cont; 18329 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18330 if (fix_xmitlist) 18331 tcp_zcopy_notify(tcp); 18332 else 18333 tail->b_datap->db_struioflag |= 18334 STRUIO_ZCNOTIFY; 18335 } 18336 bp = nbp; 18337 nbp = nbp->b_cont; 18338 if (fix_xmitlist) { 18339 tail->b_prev = bp->b_prev; 18340 tail->b_next = bp->b_next; 18341 if (tcp->tcp_xmit_tail == bp) 18342 tcp->tcp_xmit_tail = tail; 18343 } 18344 bp->b_next = NULL; 18345 bp->b_prev = NULL; 18346 freeb(bp); 18347 } else { 18348 tail->b_cont = nbp; 18349 tail = nbp; 18350 nbp = nbp->b_cont; 18351 } 18352 } 18353 if (fix_xmitlist) { 18354 tcp->tcp_xmit_last = tail; 18355 tcp->tcp_xmit_zc_clean = B_TRUE; 18356 } 18357 return (head); 18358 } 18359 18360 static void 18361 tcp_zcopy_notify(tcp_t *tcp) 18362 { 18363 struct stdata *stp; 18364 conn_t *connp; 18365 18366 if (tcp->tcp_detached) 18367 return; 18368 connp = tcp->tcp_connp; 18369 if (IPCL_IS_NONSTR(connp)) { 18370 (*connp->conn_upcalls->su_zcopy_notify) 18371 (connp->conn_upper_handle); 18372 return; 18373 } 18374 stp = STREAM(tcp->tcp_rq); 18375 mutex_enter(&stp->sd_lock); 18376 stp->sd_flag |= STZCNOTIFY; 18377 cv_broadcast(&stp->sd_zcopy_wait); 18378 mutex_exit(&stp->sd_lock); 18379 } 18380 18381 static boolean_t 18382 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18383 { 18384 ire_t *ire; 18385 conn_t *connp = tcp->tcp_connp; 18386 tcp_stack_t *tcps = tcp->tcp_tcps; 18387 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18388 18389 mutex_enter(&connp->conn_lock); 18390 ire = connp->conn_ire_cache; 18391 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18392 18393 if ((ire != NULL) && 18394 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18395 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18396 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18397 IRE_REFHOLD(ire); 18398 mutex_exit(&connp->conn_lock); 18399 } else { 18400 boolean_t cached = B_FALSE; 18401 ts_label_t *tsl; 18402 18403 /* force a recheck later on */ 18404 tcp->tcp_ire_ill_check_done = B_FALSE; 18405 18406 TCP_DBGSTAT(tcps, tcp_ire_null1); 18407 connp->conn_ire_cache = NULL; 18408 mutex_exit(&connp->conn_lock); 18409 18410 if (ire != NULL) 18411 IRE_REFRELE_NOTR(ire); 18412 18413 tsl = crgetlabel(CONN_CRED(connp)); 18414 ire = (dst ? 18415 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18416 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18417 connp->conn_zoneid, tsl, ipst)); 18418 18419 if (ire == NULL) { 18420 TCP_STAT(tcps, tcp_ire_null); 18421 return (B_FALSE); 18422 } 18423 18424 IRE_REFHOLD_NOTR(ire); 18425 18426 mutex_enter(&connp->conn_lock); 18427 if (CONN_CACHE_IRE(connp)) { 18428 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18429 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18430 TCP_CHECK_IREINFO(tcp, ire); 18431 connp->conn_ire_cache = ire; 18432 cached = B_TRUE; 18433 } 18434 rw_exit(&ire->ire_bucket->irb_lock); 18435 } 18436 mutex_exit(&connp->conn_lock); 18437 18438 /* 18439 * We can continue to use the ire but since it was 18440 * not cached, we should drop the extra reference. 18441 */ 18442 if (!cached) 18443 IRE_REFRELE_NOTR(ire); 18444 18445 /* 18446 * Rampart note: no need to select a new label here, since 18447 * labels are not allowed to change during the life of a TCP 18448 * connection. 18449 */ 18450 } 18451 18452 *irep = ire; 18453 18454 return (B_TRUE); 18455 } 18456 18457 /* 18458 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18459 * 18460 * 0 = success; 18461 * 1 = failed to find ire and ill. 18462 */ 18463 static boolean_t 18464 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18465 { 18466 ipha_t *ipha; 18467 ipaddr_t dst; 18468 ire_t *ire; 18469 ill_t *ill; 18470 mblk_t *ire_fp_mp; 18471 tcp_stack_t *tcps = tcp->tcp_tcps; 18472 18473 if (mp != NULL) 18474 ipha = (ipha_t *)mp->b_rptr; 18475 else 18476 ipha = tcp->tcp_ipha; 18477 dst = ipha->ipha_dst; 18478 18479 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18480 return (B_FALSE); 18481 18482 if ((ire->ire_flags & RTF_MULTIRT) || 18483 (ire->ire_stq == NULL) || 18484 (ire->ire_nce == NULL) || 18485 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18486 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18487 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18488 TCP_STAT(tcps, tcp_ip_ire_send); 18489 IRE_REFRELE(ire); 18490 return (B_FALSE); 18491 } 18492 18493 ill = ire_to_ill(ire); 18494 ASSERT(ill != NULL); 18495 18496 if (!tcp->tcp_ire_ill_check_done) { 18497 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18498 tcp->tcp_ire_ill_check_done = B_TRUE; 18499 } 18500 18501 *irep = ire; 18502 *illp = ill; 18503 18504 return (B_TRUE); 18505 } 18506 18507 static void 18508 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18509 { 18510 ipha_t *ipha; 18511 ipaddr_t src; 18512 ipaddr_t dst; 18513 uint32_t cksum; 18514 ire_t *ire; 18515 uint16_t *up; 18516 ill_t *ill; 18517 conn_t *connp = tcp->tcp_connp; 18518 uint32_t hcksum_txflags = 0; 18519 mblk_t *ire_fp_mp; 18520 uint_t ire_fp_mp_len; 18521 tcp_stack_t *tcps = tcp->tcp_tcps; 18522 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18523 cred_t *cr; 18524 pid_t cpid; 18525 18526 ASSERT(DB_TYPE(mp) == M_DATA); 18527 18528 /* 18529 * Here we need to handle the overloading of the cred_t for 18530 * both getpeerucred and TX. 18531 * If this is a SYN then the caller already set db_credp so 18532 * that getpeerucred will work. But if TX is in use we might have 18533 * a conn_effective_cred which is different, and we need to use that 18534 * cred to make TX use the correct label and label dependent route. 18535 */ 18536 if (is_system_labeled()) { 18537 cr = msg_getcred(mp, &cpid); 18538 if (cr == NULL || connp->conn_effective_cred != NULL) 18539 mblk_setcred(mp, CONN_CRED(connp), cpid); 18540 } 18541 18542 ipha = (ipha_t *)mp->b_rptr; 18543 src = ipha->ipha_src; 18544 dst = ipha->ipha_dst; 18545 18546 ASSERT(q != NULL); 18547 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18548 18549 /* 18550 * Drop off fast path for IPv6 and also if options are present or 18551 * we need to resolve a TS label. 18552 */ 18553 if (tcp->tcp_ipversion != IPV4_VERSION || 18554 !IPCL_IS_CONNECTED(connp) || 18555 !CONN_IS_LSO_MD_FASTPATH(connp) || 18556 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18557 !connp->conn_ulp_labeled || 18558 ipha->ipha_ident == IP_HDR_INCLUDED || 18559 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18560 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18561 if (tcp->tcp_snd_zcopy_aware) 18562 mp = tcp_zcopy_disable(tcp, mp); 18563 TCP_STAT(tcps, tcp_ip_send); 18564 CALL_IP_WPUT(connp, q, mp); 18565 return; 18566 } 18567 18568 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18569 if (tcp->tcp_snd_zcopy_aware) 18570 mp = tcp_zcopy_backoff(tcp, mp, 0); 18571 CALL_IP_WPUT(connp, q, mp); 18572 return; 18573 } 18574 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18575 ire_fp_mp_len = MBLKL(ire_fp_mp); 18576 18577 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18578 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18579 #ifndef _BIG_ENDIAN 18580 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18581 #endif 18582 18583 /* 18584 * Check to see if we need to re-enable LSO/MDT for this connection 18585 * because it was previously disabled due to changes in the ill; 18586 * note that by doing it here, this re-enabling only applies when 18587 * the packet is not dispatched through CALL_IP_WPUT(). 18588 * 18589 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18590 * case, since that's how we ended up here. For IPv6, we do the 18591 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18592 */ 18593 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18594 /* 18595 * Restore LSO for this connection, so that next time around 18596 * it is eligible to go through tcp_lsosend() path again. 18597 */ 18598 TCP_STAT(tcps, tcp_lso_enabled); 18599 tcp->tcp_lso = B_TRUE; 18600 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18601 "interface %s\n", (void *)connp, ill->ill_name)); 18602 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18603 /* 18604 * Restore MDT for this connection, so that next time around 18605 * it is eligible to go through tcp_multisend() path again. 18606 */ 18607 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18608 tcp->tcp_mdt = B_TRUE; 18609 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18610 "interface %s\n", (void *)connp, ill->ill_name)); 18611 } 18612 18613 if (tcp->tcp_snd_zcopy_aware) { 18614 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18615 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18616 mp = tcp_zcopy_disable(tcp, mp); 18617 /* 18618 * we shouldn't need to reset ipha as the mp containing 18619 * ipha should never be a zero-copy mp. 18620 */ 18621 } 18622 18623 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18624 ASSERT(ill->ill_hcksum_capab != NULL); 18625 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18626 } 18627 18628 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18629 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18630 18631 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18632 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18633 18634 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18635 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18636 18637 /* Software checksum? */ 18638 if (DB_CKSUMFLAGS(mp) == 0) { 18639 TCP_STAT(tcps, tcp_out_sw_cksum); 18640 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18641 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18642 } 18643 18644 /* Calculate IP header checksum if hardware isn't capable */ 18645 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18646 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18647 ((uint16_t *)ipha)[4]); 18648 } 18649 18650 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18651 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18652 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18653 18654 UPDATE_OB_PKT_COUNT(ire); 18655 ire->ire_last_used_time = lbolt; 18656 18657 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18658 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18659 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18660 ntohs(ipha->ipha_length)); 18661 18662 DTRACE_PROBE4(ip4__physical__out__start, 18663 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18664 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18665 ipst->ips_ipv4firewall_physical_out, 18666 NULL, ill, ipha, mp, mp, 0, ipst); 18667 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18668 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18669 18670 if (mp != NULL) { 18671 if (ipst->ips_ipobs_enabled) { 18672 zoneid_t szone; 18673 18674 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18675 ipst, ALL_ZONES); 18676 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18677 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18678 } 18679 18680 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18681 } 18682 18683 IRE_REFRELE(ire); 18684 } 18685 18686 /* 18687 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18688 * if the receiver shrinks the window, i.e. moves the right window to the 18689 * left, the we should not send new data, but should retransmit normally the 18690 * old unacked data between suna and suna + swnd. We might has sent data 18691 * that is now outside the new window, pretend that we didn't send it. 18692 */ 18693 static void 18694 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18695 { 18696 uint32_t snxt = tcp->tcp_snxt; 18697 18698 ASSERT(shrunk_count > 0); 18699 18700 if (!tcp->tcp_is_wnd_shrnk) { 18701 tcp->tcp_snxt_shrunk = snxt; 18702 tcp->tcp_is_wnd_shrnk = B_TRUE; 18703 } else if (SEQ_GT(snxt, tcp->tcp_snxt_shrunk)) { 18704 tcp->tcp_snxt_shrunk = snxt; 18705 } 18706 18707 /* Pretend we didn't send the data outside the window */ 18708 snxt -= shrunk_count; 18709 18710 /* Reset all the values per the now shrunk window */ 18711 tcp_update_xmit_tail(tcp, snxt); 18712 tcp->tcp_unsent += shrunk_count; 18713 18714 /* 18715 * If the SACK option is set, delete the entire list of 18716 * notsack'ed blocks. 18717 */ 18718 if (tcp->tcp_sack_info != NULL) { 18719 if (tcp->tcp_notsack_list != NULL) 18720 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp); 18721 } 18722 18723 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18724 /* 18725 * Make sure the timer is running so that we will probe a zero 18726 * window. 18727 */ 18728 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18729 } 18730 18731 18732 /* 18733 * The TCP normal data output path. 18734 * NOTE: the logic of the fast path is duplicated from this function. 18735 */ 18736 static void 18737 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18738 { 18739 int len; 18740 mblk_t *local_time; 18741 mblk_t *mp1; 18742 uint32_t snxt; 18743 int tail_unsent; 18744 int tcpstate; 18745 int usable = 0; 18746 mblk_t *xmit_tail; 18747 queue_t *q = tcp->tcp_wq; 18748 int32_t mss; 18749 int32_t num_sack_blk = 0; 18750 int32_t tcp_hdr_len; 18751 int32_t tcp_tcp_hdr_len; 18752 int mdt_thres; 18753 int rc; 18754 tcp_stack_t *tcps = tcp->tcp_tcps; 18755 ip_stack_t *ipst; 18756 18757 tcpstate = tcp->tcp_state; 18758 if (mp == NULL) { 18759 /* 18760 * tcp_wput_data() with NULL mp should only be called when 18761 * there is unsent data. 18762 */ 18763 ASSERT(tcp->tcp_unsent > 0); 18764 /* Really tacky... but we need this for detached closes. */ 18765 len = tcp->tcp_unsent; 18766 goto data_null; 18767 } 18768 18769 #if CCS_STATS 18770 wrw_stats.tot.count++; 18771 wrw_stats.tot.bytes += msgdsize(mp); 18772 #endif 18773 ASSERT(mp->b_datap->db_type == M_DATA); 18774 /* 18775 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18776 * or before a connection attempt has begun. 18777 */ 18778 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18779 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18780 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18781 #ifdef DEBUG 18782 cmn_err(CE_WARN, 18783 "tcp_wput_data: data after ordrel, %s", 18784 tcp_display(tcp, NULL, 18785 DISP_ADDR_AND_PORT)); 18786 #else 18787 if (tcp->tcp_debug) { 18788 (void) strlog(TCP_MOD_ID, 0, 1, 18789 SL_TRACE|SL_ERROR, 18790 "tcp_wput_data: data after ordrel, %s\n", 18791 tcp_display(tcp, NULL, 18792 DISP_ADDR_AND_PORT)); 18793 } 18794 #endif /* DEBUG */ 18795 } 18796 if (tcp->tcp_snd_zcopy_aware && 18797 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18798 tcp_zcopy_notify(tcp); 18799 freemsg(mp); 18800 mutex_enter(&tcp->tcp_non_sq_lock); 18801 if (tcp->tcp_flow_stopped && 18802 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18803 tcp_clrqfull(tcp); 18804 } 18805 mutex_exit(&tcp->tcp_non_sq_lock); 18806 return; 18807 } 18808 18809 /* Strip empties */ 18810 for (;;) { 18811 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18812 (uintptr_t)INT_MAX); 18813 len = (int)(mp->b_wptr - mp->b_rptr); 18814 if (len > 0) 18815 break; 18816 mp1 = mp; 18817 mp = mp->b_cont; 18818 freeb(mp1); 18819 if (!mp) { 18820 return; 18821 } 18822 } 18823 18824 /* If we are the first on the list ... */ 18825 if (tcp->tcp_xmit_head == NULL) { 18826 tcp->tcp_xmit_head = mp; 18827 tcp->tcp_xmit_tail = mp; 18828 tcp->tcp_xmit_tail_unsent = len; 18829 } else { 18830 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18831 struct datab *dp; 18832 18833 mp1 = tcp->tcp_xmit_last; 18834 if (len < tcp_tx_pull_len && 18835 (dp = mp1->b_datap)->db_ref == 1 && 18836 dp->db_lim - mp1->b_wptr >= len) { 18837 ASSERT(len > 0); 18838 ASSERT(!mp1->b_cont); 18839 if (len == 1) { 18840 *mp1->b_wptr++ = *mp->b_rptr; 18841 } else { 18842 bcopy(mp->b_rptr, mp1->b_wptr, len); 18843 mp1->b_wptr += len; 18844 } 18845 if (mp1 == tcp->tcp_xmit_tail) 18846 tcp->tcp_xmit_tail_unsent += len; 18847 mp1->b_cont = mp->b_cont; 18848 if (tcp->tcp_snd_zcopy_aware && 18849 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18850 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18851 freeb(mp); 18852 mp = mp1; 18853 } else { 18854 tcp->tcp_xmit_last->b_cont = mp; 18855 } 18856 len += tcp->tcp_unsent; 18857 } 18858 18859 /* Tack on however many more positive length mblks we have */ 18860 if ((mp1 = mp->b_cont) != NULL) { 18861 do { 18862 int tlen; 18863 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18864 (uintptr_t)INT_MAX); 18865 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18866 if (tlen <= 0) { 18867 mp->b_cont = mp1->b_cont; 18868 freeb(mp1); 18869 } else { 18870 len += tlen; 18871 mp = mp1; 18872 } 18873 } while ((mp1 = mp->b_cont) != NULL); 18874 } 18875 tcp->tcp_xmit_last = mp; 18876 tcp->tcp_unsent = len; 18877 18878 if (urgent) 18879 usable = 1; 18880 18881 data_null: 18882 snxt = tcp->tcp_snxt; 18883 xmit_tail = tcp->tcp_xmit_tail; 18884 tail_unsent = tcp->tcp_xmit_tail_unsent; 18885 18886 /* 18887 * Note that tcp_mss has been adjusted to take into account the 18888 * timestamp option if applicable. Because SACK options do not 18889 * appear in every TCP segments and they are of variable lengths, 18890 * they cannot be included in tcp_mss. Thus we need to calculate 18891 * the actual segment length when we need to send a segment which 18892 * includes SACK options. 18893 */ 18894 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18895 int32_t opt_len; 18896 18897 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18898 tcp->tcp_num_sack_blk); 18899 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18900 2 + TCPOPT_HEADER_LEN; 18901 mss = tcp->tcp_mss - opt_len; 18902 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18903 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18904 } else { 18905 mss = tcp->tcp_mss; 18906 tcp_hdr_len = tcp->tcp_hdr_len; 18907 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18908 } 18909 18910 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18911 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18912 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18913 } 18914 if (tcpstate == TCPS_SYN_RCVD) { 18915 /* 18916 * The three-way connection establishment handshake is not 18917 * complete yet. We want to queue the data for transmission 18918 * after entering ESTABLISHED state (RFC793). A jump to 18919 * "done" label effectively leaves data on the queue. 18920 */ 18921 goto done; 18922 } else { 18923 int usable_r; 18924 18925 /* 18926 * In the special case when cwnd is zero, which can only 18927 * happen if the connection is ECN capable, return now. 18928 * New segments is sent using tcp_timer(). The timer 18929 * is set in tcp_rput_data(). 18930 */ 18931 if (tcp->tcp_cwnd == 0) { 18932 /* 18933 * Note that tcp_cwnd is 0 before 3-way handshake is 18934 * finished. 18935 */ 18936 ASSERT(tcp->tcp_ecn_ok || 18937 tcp->tcp_state < TCPS_ESTABLISHED); 18938 return; 18939 } 18940 18941 /* NOTE: trouble if xmitting while SYN not acked? */ 18942 usable_r = snxt - tcp->tcp_suna; 18943 usable_r = tcp->tcp_swnd - usable_r; 18944 18945 /* 18946 * Check if the receiver has shrunk the window. If 18947 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18948 * cannot be set as there is unsent data, so FIN cannot 18949 * be sent out. Otherwise, we need to take into account 18950 * of FIN as it consumes an "invisible" sequence number. 18951 */ 18952 ASSERT(tcp->tcp_fin_sent == 0); 18953 if (usable_r < 0) { 18954 /* 18955 * The receiver has shrunk the window and we have sent 18956 * -usable_r date beyond the window, re-adjust. 18957 * 18958 * If TCP window scaling is enabled, there can be 18959 * round down error as the advertised receive window 18960 * is actually right shifted n bits. This means that 18961 * the lower n bits info is wiped out. It will look 18962 * like the window is shrunk. Do a check here to 18963 * see if the shrunk amount is actually within the 18964 * error in window calculation. If it is, just 18965 * return. Note that this check is inside the 18966 * shrunk window check. This makes sure that even 18967 * though tcp_process_shrunk_swnd() is not called, 18968 * we will stop further processing. 18969 */ 18970 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18971 tcp_process_shrunk_swnd(tcp, -usable_r); 18972 } 18973 return; 18974 } 18975 18976 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18977 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18978 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18979 18980 /* usable = MIN(usable, unsent) */ 18981 if (usable_r > len) 18982 usable_r = len; 18983 18984 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18985 if (usable_r > 0) { 18986 usable = usable_r; 18987 } else { 18988 /* Bypass all other unnecessary processing. */ 18989 goto done; 18990 } 18991 } 18992 18993 local_time = (mblk_t *)lbolt; 18994 18995 /* 18996 * "Our" Nagle Algorithm. This is not the same as in the old 18997 * BSD. This is more in line with the true intent of Nagle. 18998 * 18999 * The conditions are: 19000 * 1. The amount of unsent data (or amount of data which can be 19001 * sent, whichever is smaller) is less than Nagle limit. 19002 * 2. The last sent size is also less than Nagle limit. 19003 * 3. There is unack'ed data. 19004 * 4. Urgent pointer is not set. Send urgent data ignoring the 19005 * Nagle algorithm. This reduces the probability that urgent 19006 * bytes get "merged" together. 19007 * 5. The app has not closed the connection. This eliminates the 19008 * wait time of the receiving side waiting for the last piece of 19009 * (small) data. 19010 * 19011 * If all are satisified, exit without sending anything. Note 19012 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 19013 * the smaller of 1 MSS and global tcp_naglim_def (default to be 19014 * 4095). 19015 */ 19016 if (usable < (int)tcp->tcp_naglim && 19017 tcp->tcp_naglim > tcp->tcp_last_sent_len && 19018 snxt != tcp->tcp_suna && 19019 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 19020 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 19021 goto done; 19022 } 19023 19024 /* 19025 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option 19026 * is set, then we have to force TCP not to send partial segment 19027 * (smaller than MSS bytes). We are calculating the usable now 19028 * based on full mss and will save the rest of remaining data for 19029 * later. When tcp_zero_win_probe is set, TCP needs to send out 19030 * something to do zero window probe. 19031 */ 19032 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) { 19033 if (usable < mss) 19034 goto done; 19035 usable = (usable / mss) * mss; 19036 } 19037 19038 /* Update the latest receive window size in TCP header. */ 19039 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 19040 tcp->tcp_tcph->th_win); 19041 19042 /* 19043 * Determine if it's worthwhile to attempt LSO or MDT, based on: 19044 * 19045 * 1. Simple TCP/IP{v4,v6} (no options). 19046 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 19047 * 3. If the TCP connection is in ESTABLISHED state. 19048 * 4. The TCP is not detached. 19049 * 19050 * If any of the above conditions have changed during the 19051 * connection, stop using LSO/MDT and restore the stream head 19052 * parameters accordingly. 19053 */ 19054 ipst = tcps->tcps_netstack->netstack_ip; 19055 19056 if ((tcp->tcp_lso || tcp->tcp_mdt) && 19057 ((tcp->tcp_ipversion == IPV4_VERSION && 19058 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 19059 (tcp->tcp_ipversion == IPV6_VERSION && 19060 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 19061 tcp->tcp_state != TCPS_ESTABLISHED || 19062 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 19063 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 19064 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 19065 if (tcp->tcp_lso) { 19066 tcp->tcp_connp->conn_lso_ok = B_FALSE; 19067 tcp->tcp_lso = B_FALSE; 19068 } else { 19069 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 19070 tcp->tcp_mdt = B_FALSE; 19071 } 19072 19073 /* Anything other than detached is considered pathological */ 19074 if (!TCP_IS_DETACHED(tcp)) { 19075 if (tcp->tcp_lso) 19076 TCP_STAT(tcps, tcp_lso_disabled); 19077 else 19078 TCP_STAT(tcps, tcp_mdt_conn_halted1); 19079 (void) tcp_maxpsz_set(tcp, B_TRUE); 19080 } 19081 } 19082 19083 /* Use MDT if sendable amount is greater than the threshold */ 19084 if (tcp->tcp_mdt && 19085 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 19086 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 19087 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 19088 (tcp->tcp_valid_bits == 0 || 19089 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 19090 ASSERT(tcp->tcp_connp->conn_mdt_ok); 19091 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19092 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19093 local_time, mdt_thres); 19094 } else { 19095 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 19096 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 19097 local_time, INT_MAX); 19098 } 19099 19100 /* Pretend that all we were trying to send really got sent */ 19101 if (rc < 0 && tail_unsent < 0) { 19102 do { 19103 xmit_tail = xmit_tail->b_cont; 19104 xmit_tail->b_prev = local_time; 19105 ASSERT((uintptr_t)(xmit_tail->b_wptr - 19106 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 19107 tail_unsent += (int)(xmit_tail->b_wptr - 19108 xmit_tail->b_rptr); 19109 } while (tail_unsent < 0); 19110 } 19111 done:; 19112 tcp->tcp_xmit_tail = xmit_tail; 19113 tcp->tcp_xmit_tail_unsent = tail_unsent; 19114 len = tcp->tcp_snxt - snxt; 19115 if (len) { 19116 /* 19117 * If new data was sent, need to update the notsack 19118 * list, which is, afterall, data blocks that have 19119 * not been sack'ed by the receiver. New data is 19120 * not sack'ed. 19121 */ 19122 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 19123 /* len is a negative value. */ 19124 tcp->tcp_pipe -= len; 19125 tcp_notsack_update(&(tcp->tcp_notsack_list), 19126 tcp->tcp_snxt, snxt, 19127 &(tcp->tcp_num_notsack_blk), 19128 &(tcp->tcp_cnt_notsack_list)); 19129 } 19130 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 19131 tcp->tcp_rack = tcp->tcp_rnxt; 19132 tcp->tcp_rack_cnt = 0; 19133 if ((snxt + len) == tcp->tcp_suna) { 19134 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19135 } 19136 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 19137 /* 19138 * Didn't send anything. Make sure the timer is running 19139 * so that we will probe a zero window. 19140 */ 19141 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19142 } 19143 /* Note that len is the amount we just sent but with a negative sign */ 19144 tcp->tcp_unsent += len; 19145 mutex_enter(&tcp->tcp_non_sq_lock); 19146 if (tcp->tcp_flow_stopped) { 19147 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 19148 tcp_clrqfull(tcp); 19149 } 19150 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 19151 tcp_setqfull(tcp); 19152 } 19153 mutex_exit(&tcp->tcp_non_sq_lock); 19154 } 19155 19156 /* 19157 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 19158 * outgoing TCP header with the template header, as well as other 19159 * options such as time-stamp, ECN and/or SACK. 19160 */ 19161 static void 19162 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 19163 { 19164 tcph_t *tcp_tmpl, *tcp_h; 19165 uint32_t *dst, *src; 19166 int hdrlen; 19167 19168 ASSERT(OK_32PTR(rptr)); 19169 19170 /* Template header */ 19171 tcp_tmpl = tcp->tcp_tcph; 19172 19173 /* Header of outgoing packet */ 19174 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19175 19176 /* dst and src are opaque 32-bit fields, used for copying */ 19177 dst = (uint32_t *)rptr; 19178 src = (uint32_t *)tcp->tcp_iphc; 19179 hdrlen = tcp->tcp_hdr_len; 19180 19181 /* Fill time-stamp option if needed */ 19182 if (tcp->tcp_snd_ts_ok) { 19183 U32_TO_BE32((uint32_t)now, 19184 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19185 U32_TO_BE32(tcp->tcp_ts_recent, 19186 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19187 } else { 19188 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19189 } 19190 19191 /* 19192 * Copy the template header; is this really more efficient than 19193 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19194 * but perhaps not for other scenarios. 19195 */ 19196 dst[0] = src[0]; 19197 dst[1] = src[1]; 19198 dst[2] = src[2]; 19199 dst[3] = src[3]; 19200 dst[4] = src[4]; 19201 dst[5] = src[5]; 19202 dst[6] = src[6]; 19203 dst[7] = src[7]; 19204 dst[8] = src[8]; 19205 dst[9] = src[9]; 19206 if (hdrlen -= 40) { 19207 hdrlen >>= 2; 19208 dst += 10; 19209 src += 10; 19210 do { 19211 *dst++ = *src++; 19212 } while (--hdrlen); 19213 } 19214 19215 /* 19216 * Set the ECN info in the TCP header if it is not a zero 19217 * window probe. Zero window probe is only sent in 19218 * tcp_wput_data() and tcp_timer(). 19219 */ 19220 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19221 SET_ECT(tcp, rptr); 19222 19223 if (tcp->tcp_ecn_echo_on) 19224 tcp_h->th_flags[0] |= TH_ECE; 19225 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19226 tcp_h->th_flags[0] |= TH_CWR; 19227 tcp->tcp_ecn_cwr_sent = B_TRUE; 19228 } 19229 } 19230 19231 /* Fill in SACK options */ 19232 if (num_sack_blk > 0) { 19233 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19234 sack_blk_t *tmp; 19235 int32_t i; 19236 19237 wptr[0] = TCPOPT_NOP; 19238 wptr[1] = TCPOPT_NOP; 19239 wptr[2] = TCPOPT_SACK; 19240 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19241 sizeof (sack_blk_t); 19242 wptr += TCPOPT_REAL_SACK_LEN; 19243 19244 tmp = tcp->tcp_sack_list; 19245 for (i = 0; i < num_sack_blk; i++) { 19246 U32_TO_BE32(tmp[i].begin, wptr); 19247 wptr += sizeof (tcp_seq); 19248 U32_TO_BE32(tmp[i].end, wptr); 19249 wptr += sizeof (tcp_seq); 19250 } 19251 tcp_h->th_offset_and_rsrvd[0] += 19252 ((num_sack_blk * 2 + 1) << 4); 19253 } 19254 } 19255 19256 /* 19257 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19258 * the destination address and SAP attribute, and if necessary, the 19259 * hardware checksum offload attribute to a Multidata message. 19260 */ 19261 static int 19262 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19263 const uint32_t start, const uint32_t stuff, const uint32_t end, 19264 const uint32_t flags, tcp_stack_t *tcps) 19265 { 19266 /* Add global destination address & SAP attribute */ 19267 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19268 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19269 "destination address+SAP\n")); 19270 19271 if (dlmp != NULL) 19272 TCP_STAT(tcps, tcp_mdt_allocfail); 19273 return (-1); 19274 } 19275 19276 /* Add global hwcksum attribute */ 19277 if (hwcksum && 19278 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19279 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19280 "checksum attribute\n")); 19281 19282 TCP_STAT(tcps, tcp_mdt_allocfail); 19283 return (-1); 19284 } 19285 19286 return (0); 19287 } 19288 19289 /* 19290 * Smaller and private version of pdescinfo_t used specifically for TCP, 19291 * which allows for only two payload spans per packet. 19292 */ 19293 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19294 19295 /* 19296 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19297 * scheme, and returns one the following: 19298 * 19299 * -1 = failed allocation. 19300 * 0 = success; burst count reached, or usable send window is too small, 19301 * and that we'd rather wait until later before sending again. 19302 */ 19303 static int 19304 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19305 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19306 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19307 const int mdt_thres) 19308 { 19309 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19310 multidata_t *mmd; 19311 uint_t obsegs, obbytes, hdr_frag_sz; 19312 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19313 int num_burst_seg, max_pld; 19314 pdesc_t *pkt; 19315 tcp_pdescinfo_t tcp_pkt_info; 19316 pdescinfo_t *pkt_info; 19317 int pbuf_idx, pbuf_idx_nxt; 19318 int seg_len, len, spill, af; 19319 boolean_t add_buffer, zcopy, clusterwide; 19320 boolean_t rconfirm = B_FALSE; 19321 boolean_t done = B_FALSE; 19322 uint32_t cksum; 19323 uint32_t hwcksum_flags; 19324 ire_t *ire = NULL; 19325 ill_t *ill; 19326 ipha_t *ipha; 19327 ip6_t *ip6h; 19328 ipaddr_t src, dst; 19329 ill_zerocopy_capab_t *zc_cap = NULL; 19330 uint16_t *up; 19331 int err; 19332 conn_t *connp; 19333 tcp_stack_t *tcps = tcp->tcp_tcps; 19334 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19335 int usable_mmd, tail_unsent_mmd; 19336 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19337 mblk_t *xmit_tail_mmd; 19338 netstackid_t stack_id; 19339 19340 #ifdef _BIG_ENDIAN 19341 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19342 #else 19343 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19344 #endif 19345 19346 #define PREP_NEW_MULTIDATA() { \ 19347 mmd = NULL; \ 19348 md_mp = md_hbuf = NULL; \ 19349 cur_hdr_off = 0; \ 19350 max_pld = tcp->tcp_mdt_max_pld; \ 19351 pbuf_idx = pbuf_idx_nxt = -1; \ 19352 add_buffer = B_TRUE; \ 19353 zcopy = B_FALSE; \ 19354 } 19355 19356 #define PREP_NEW_PBUF() { \ 19357 md_pbuf = md_pbuf_nxt = NULL; \ 19358 pbuf_idx = pbuf_idx_nxt = -1; \ 19359 cur_pld_off = 0; \ 19360 first_snxt = *snxt; \ 19361 ASSERT(*tail_unsent > 0); \ 19362 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19363 } 19364 19365 ASSERT(mdt_thres >= mss); 19366 ASSERT(*usable > 0 && *usable > mdt_thres); 19367 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19368 ASSERT(!TCP_IS_DETACHED(tcp)); 19369 ASSERT(tcp->tcp_valid_bits == 0 || 19370 tcp->tcp_valid_bits == TCP_FSS_VALID); 19371 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19372 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19373 (tcp->tcp_ipversion == IPV6_VERSION && 19374 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19375 19376 connp = tcp->tcp_connp; 19377 ASSERT(connp != NULL); 19378 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19379 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19380 19381 stack_id = connp->conn_netstack->netstack_stackid; 19382 19383 usable_mmd = tail_unsent_mmd = 0; 19384 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19385 xmit_tail_mmd = NULL; 19386 /* 19387 * Note that tcp will only declare at most 2 payload spans per 19388 * packet, which is much lower than the maximum allowable number 19389 * of packet spans per Multidata. For this reason, we use the 19390 * privately declared and smaller descriptor info structure, in 19391 * order to save some stack space. 19392 */ 19393 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19394 19395 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19396 if (af == AF_INET) { 19397 dst = tcp->tcp_ipha->ipha_dst; 19398 src = tcp->tcp_ipha->ipha_src; 19399 ASSERT(!CLASSD(dst)); 19400 } 19401 ASSERT(af == AF_INET || 19402 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19403 19404 obsegs = obbytes = 0; 19405 num_burst_seg = tcp->tcp_snd_burst; 19406 md_mp_head = NULL; 19407 PREP_NEW_MULTIDATA(); 19408 19409 /* 19410 * Before we go on further, make sure there is an IRE that we can 19411 * use, and that the ILL supports MDT. Otherwise, there's no point 19412 * in proceeding any further, and we should just hand everything 19413 * off to the legacy path. 19414 */ 19415 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19416 goto legacy_send_no_md; 19417 19418 ASSERT(ire != NULL); 19419 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19420 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19421 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19422 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19423 /* 19424 * If we do support loopback for MDT (which requires modifications 19425 * to the receiving paths), the following assertions should go away, 19426 * and we would be sending the Multidata to loopback conn later on. 19427 */ 19428 ASSERT(!IRE_IS_LOCAL(ire)); 19429 ASSERT(ire->ire_stq != NULL); 19430 19431 ill = ire_to_ill(ire); 19432 ASSERT(ill != NULL); 19433 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19434 19435 if (!tcp->tcp_ire_ill_check_done) { 19436 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19437 tcp->tcp_ire_ill_check_done = B_TRUE; 19438 } 19439 19440 /* 19441 * If the underlying interface conditions have changed, or if the 19442 * new interface does not support MDT, go back to legacy path. 19443 */ 19444 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19445 /* don't go through this path anymore for this connection */ 19446 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19447 tcp->tcp_mdt = B_FALSE; 19448 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19449 "interface %s\n", (void *)connp, ill->ill_name)); 19450 /* IRE will be released prior to returning */ 19451 goto legacy_send_no_md; 19452 } 19453 19454 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19455 zc_cap = ill->ill_zerocopy_capab; 19456 19457 /* 19458 * Check if we can take tcp fast-path. Note that "incomplete" 19459 * ire's (where the link-layer for next hop is not resolved 19460 * or where the fast-path header in nce_fp_mp is not available 19461 * yet) are sent down the legacy (slow) path. 19462 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19463 */ 19464 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19465 /* IRE will be released prior to returning */ 19466 goto legacy_send_no_md; 19467 } 19468 19469 /* go to legacy path if interface doesn't support zerocopy */ 19470 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19471 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19472 /* IRE will be released prior to returning */ 19473 goto legacy_send_no_md; 19474 } 19475 19476 /* does the interface support hardware checksum offload? */ 19477 hwcksum_flags = 0; 19478 if (ILL_HCKSUM_CAPABLE(ill) && 19479 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19480 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19481 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19482 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19483 HCKSUM_IPHDRCKSUM) 19484 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19485 19486 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19487 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19488 hwcksum_flags |= HCK_FULLCKSUM; 19489 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19490 HCKSUM_INET_PARTIAL) 19491 hwcksum_flags |= HCK_PARTIALCKSUM; 19492 } 19493 19494 /* 19495 * Each header fragment consists of the leading extra space, 19496 * followed by the TCP/IP header, and the trailing extra space. 19497 * We make sure that each header fragment begins on a 32-bit 19498 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19499 * aligned in tcp_mdt_update). 19500 */ 19501 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19502 tcp->tcp_mdt_hdr_tail), 4); 19503 19504 /* are we starting from the beginning of data block? */ 19505 if (*tail_unsent == 0) { 19506 *xmit_tail = (*xmit_tail)->b_cont; 19507 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19508 *tail_unsent = (int)MBLKL(*xmit_tail); 19509 } 19510 19511 /* 19512 * Here we create one or more Multidata messages, each made up of 19513 * one header buffer and up to N payload buffers. This entire 19514 * operation is done within two loops: 19515 * 19516 * The outer loop mostly deals with creating the Multidata message, 19517 * as well as the header buffer that gets added to it. It also 19518 * links the Multidata messages together such that all of them can 19519 * be sent down to the lower layer in a single putnext call; this 19520 * linking behavior depends on the tcp_mdt_chain tunable. 19521 * 19522 * The inner loop takes an existing Multidata message, and adds 19523 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19524 * packetizes those buffers by filling up the corresponding header 19525 * buffer fragments with the proper IP and TCP headers, and by 19526 * describing the layout of each packet in the packet descriptors 19527 * that get added to the Multidata. 19528 */ 19529 do { 19530 /* 19531 * If usable send window is too small, or data blocks in 19532 * transmit list are smaller than our threshold (i.e. app 19533 * performs large writes followed by small ones), we hand 19534 * off the control over to the legacy path. Note that we'll 19535 * get back the control once it encounters a large block. 19536 */ 19537 if (*usable < mss || (*tail_unsent <= mdt_thres && 19538 (*xmit_tail)->b_cont != NULL && 19539 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19540 /* send down what we've got so far */ 19541 if (md_mp_head != NULL) { 19542 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19543 obsegs, obbytes, &rconfirm); 19544 } 19545 /* 19546 * Pass control over to tcp_send(), but tell it to 19547 * return to us once a large-size transmission is 19548 * possible. 19549 */ 19550 TCP_STAT(tcps, tcp_mdt_legacy_small); 19551 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19552 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19553 tail_unsent, xmit_tail, local_time, 19554 mdt_thres)) <= 0) { 19555 /* burst count reached, or alloc failed */ 19556 IRE_REFRELE(ire); 19557 return (err); 19558 } 19559 19560 /* tcp_send() may have sent everything, so check */ 19561 if (*usable <= 0) { 19562 IRE_REFRELE(ire); 19563 return (0); 19564 } 19565 19566 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19567 /* 19568 * We may have delivered the Multidata, so make sure 19569 * to re-initialize before the next round. 19570 */ 19571 md_mp_head = NULL; 19572 obsegs = obbytes = 0; 19573 num_burst_seg = tcp->tcp_snd_burst; 19574 PREP_NEW_MULTIDATA(); 19575 19576 /* are we starting from the beginning of data block? */ 19577 if (*tail_unsent == 0) { 19578 *xmit_tail = (*xmit_tail)->b_cont; 19579 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19580 (uintptr_t)INT_MAX); 19581 *tail_unsent = (int)MBLKL(*xmit_tail); 19582 } 19583 } 19584 /* 19585 * Record current values for parameters we may need to pass 19586 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19587 * each iteration of the outer loop (each multidata message 19588 * creation). If we have a failure in the inner loop, we send 19589 * any complete multidata messages we have before reverting 19590 * to using the traditional non-md path. 19591 */ 19592 snxt_mmd = *snxt; 19593 usable_mmd = *usable; 19594 xmit_tail_mmd = *xmit_tail; 19595 tail_unsent_mmd = *tail_unsent; 19596 obsegs_mmd = obsegs; 19597 obbytes_mmd = obbytes; 19598 19599 /* 19600 * max_pld limits the number of mblks in tcp's transmit 19601 * queue that can be added to a Multidata message. Once 19602 * this counter reaches zero, no more additional mblks 19603 * can be added to it. What happens afterwards depends 19604 * on whether or not we are set to chain the Multidata 19605 * messages. If we are to link them together, reset 19606 * max_pld to its original value (tcp_mdt_max_pld) and 19607 * prepare to create a new Multidata message which will 19608 * get linked to md_mp_head. Else, leave it alone and 19609 * let the inner loop break on its own. 19610 */ 19611 if (tcp_mdt_chain && max_pld == 0) 19612 PREP_NEW_MULTIDATA(); 19613 19614 /* adding a payload buffer; re-initialize values */ 19615 if (add_buffer) 19616 PREP_NEW_PBUF(); 19617 19618 /* 19619 * If we don't have a Multidata, either because we just 19620 * (re)entered this outer loop, or after we branched off 19621 * to tcp_send above, setup the Multidata and header 19622 * buffer to be used. 19623 */ 19624 if (md_mp == NULL) { 19625 int md_hbuflen; 19626 uint32_t start, stuff; 19627 19628 /* 19629 * Calculate Multidata header buffer size large enough 19630 * to hold all of the headers that can possibly be 19631 * sent at this moment. We'd rather over-estimate 19632 * the size than running out of space; this is okay 19633 * since this buffer is small anyway. 19634 */ 19635 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19636 19637 /* 19638 * Start and stuff offset for partial hardware 19639 * checksum offload; these are currently for IPv4. 19640 * For full checksum offload, they are set to zero. 19641 */ 19642 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19643 if (af == AF_INET) { 19644 start = IP_SIMPLE_HDR_LENGTH; 19645 stuff = IP_SIMPLE_HDR_LENGTH + 19646 TCP_CHECKSUM_OFFSET; 19647 } else { 19648 start = IPV6_HDR_LEN; 19649 stuff = IPV6_HDR_LEN + 19650 TCP_CHECKSUM_OFFSET; 19651 } 19652 } else { 19653 start = stuff = 0; 19654 } 19655 19656 /* 19657 * Create the header buffer, Multidata, as well as 19658 * any necessary attributes (destination address, 19659 * SAP and hardware checksum offload) that should 19660 * be associated with the Multidata message. 19661 */ 19662 ASSERT(cur_hdr_off == 0); 19663 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19664 ((md_hbuf->b_wptr += md_hbuflen), 19665 (mmd = mmd_alloc(md_hbuf, &md_mp, 19666 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19667 /* fastpath mblk */ 19668 ire->ire_nce->nce_res_mp, 19669 /* hardware checksum enabled */ 19670 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19671 /* hardware checksum offsets */ 19672 start, stuff, 0, 19673 /* hardware checksum flag */ 19674 hwcksum_flags, tcps) != 0)) { 19675 legacy_send: 19676 /* 19677 * We arrive here from a failure within the 19678 * inner (packetizer) loop or we fail one of 19679 * the conditionals above. We restore the 19680 * previously checkpointed values for: 19681 * xmit_tail 19682 * usable 19683 * tail_unsent 19684 * snxt 19685 * obbytes 19686 * obsegs 19687 * We should then be able to dispatch any 19688 * complete multidata before reverting to the 19689 * traditional path with consistent parameters 19690 * (the inner loop updates these as it 19691 * iterates). 19692 */ 19693 *xmit_tail = xmit_tail_mmd; 19694 *usable = usable_mmd; 19695 *tail_unsent = tail_unsent_mmd; 19696 *snxt = snxt_mmd; 19697 obbytes = obbytes_mmd; 19698 obsegs = obsegs_mmd; 19699 if (md_mp != NULL) { 19700 /* Unlink message from the chain */ 19701 if (md_mp_head != NULL) { 19702 err = (intptr_t)rmvb(md_mp_head, 19703 md_mp); 19704 /* 19705 * We can't assert that rmvb 19706 * did not return -1, since we 19707 * may get here before linkb 19708 * happens. We do, however, 19709 * check if we just removed the 19710 * only element in the list. 19711 */ 19712 if (err == 0) 19713 md_mp_head = NULL; 19714 } 19715 /* md_hbuf gets freed automatically */ 19716 TCP_STAT(tcps, tcp_mdt_discarded); 19717 freeb(md_mp); 19718 } else { 19719 /* Either allocb or mmd_alloc failed */ 19720 TCP_STAT(tcps, tcp_mdt_allocfail); 19721 if (md_hbuf != NULL) 19722 freeb(md_hbuf); 19723 } 19724 19725 /* send down what we've got so far */ 19726 if (md_mp_head != NULL) { 19727 tcp_multisend_data(tcp, ire, ill, 19728 md_mp_head, obsegs, obbytes, 19729 &rconfirm); 19730 } 19731 legacy_send_no_md: 19732 if (ire != NULL) 19733 IRE_REFRELE(ire); 19734 /* 19735 * Too bad; let the legacy path handle this. 19736 * We specify INT_MAX for the threshold, since 19737 * we gave up with the Multidata processings 19738 * and let the old path have it all. 19739 */ 19740 TCP_STAT(tcps, tcp_mdt_legacy_all); 19741 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19742 tcp_tcp_hdr_len, num_sack_blk, usable, 19743 snxt, tail_unsent, xmit_tail, local_time, 19744 INT_MAX)); 19745 } 19746 19747 /* link to any existing ones, if applicable */ 19748 TCP_STAT(tcps, tcp_mdt_allocd); 19749 if (md_mp_head == NULL) { 19750 md_mp_head = md_mp; 19751 } else if (tcp_mdt_chain) { 19752 TCP_STAT(tcps, tcp_mdt_linked); 19753 linkb(md_mp_head, md_mp); 19754 } 19755 } 19756 19757 ASSERT(md_mp_head != NULL); 19758 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19759 ASSERT(md_mp != NULL && mmd != NULL); 19760 ASSERT(md_hbuf != NULL); 19761 19762 /* 19763 * Packetize the transmittable portion of the data block; 19764 * each data block is essentially added to the Multidata 19765 * as a payload buffer. We also deal with adding more 19766 * than one payload buffers, which happens when the remaining 19767 * packetized portion of the current payload buffer is less 19768 * than MSS, while the next data block in transmit queue 19769 * has enough data to make up for one. This "spillover" 19770 * case essentially creates a split-packet, where portions 19771 * of the packet's payload fragments may span across two 19772 * virtually discontiguous address blocks. 19773 */ 19774 seg_len = mss; 19775 do { 19776 len = seg_len; 19777 19778 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19779 ipha = NULL; 19780 ip6h = NULL; 19781 19782 ASSERT(len > 0); 19783 ASSERT(max_pld >= 0); 19784 ASSERT(!add_buffer || cur_pld_off == 0); 19785 19786 /* 19787 * First time around for this payload buffer; note 19788 * in the case of a spillover, the following has 19789 * been done prior to adding the split-packet 19790 * descriptor to Multidata, and we don't want to 19791 * repeat the process. 19792 */ 19793 if (add_buffer) { 19794 ASSERT(mmd != NULL); 19795 ASSERT(md_pbuf == NULL); 19796 ASSERT(md_pbuf_nxt == NULL); 19797 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19798 19799 /* 19800 * Have we reached the limit? We'd get to 19801 * this case when we're not chaining the 19802 * Multidata messages together, and since 19803 * we're done, terminate this loop. 19804 */ 19805 if (max_pld == 0) 19806 break; /* done */ 19807 19808 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19809 TCP_STAT(tcps, tcp_mdt_allocfail); 19810 goto legacy_send; /* out_of_mem */ 19811 } 19812 19813 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19814 zc_cap != NULL) { 19815 if (!ip_md_zcopy_attr(mmd, NULL, 19816 zc_cap->ill_zerocopy_flags)) { 19817 freeb(md_pbuf); 19818 TCP_STAT(tcps, 19819 tcp_mdt_allocfail); 19820 /* out_of_mem */ 19821 goto legacy_send; 19822 } 19823 zcopy = B_TRUE; 19824 } 19825 19826 md_pbuf->b_rptr += base_pld_off; 19827 19828 /* 19829 * Add a payload buffer to the Multidata; this 19830 * operation must not fail, or otherwise our 19831 * logic in this routine is broken. There 19832 * is no memory allocation done by the 19833 * routine, so any returned failure simply 19834 * tells us that we've done something wrong. 19835 * 19836 * A failure tells us that either we're adding 19837 * the same payload buffer more than once, or 19838 * we're trying to add more buffers than 19839 * allowed (max_pld calculation is wrong). 19840 * None of the above cases should happen, and 19841 * we panic because either there's horrible 19842 * heap corruption, and/or programming mistake. 19843 */ 19844 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19845 if (pbuf_idx < 0) { 19846 cmn_err(CE_PANIC, "tcp_multisend: " 19847 "payload buffer logic error " 19848 "detected for tcp %p mmd %p " 19849 "pbuf %p (%d)\n", 19850 (void *)tcp, (void *)mmd, 19851 (void *)md_pbuf, pbuf_idx); 19852 } 19853 19854 ASSERT(max_pld > 0); 19855 --max_pld; 19856 add_buffer = B_FALSE; 19857 } 19858 19859 ASSERT(md_mp_head != NULL); 19860 ASSERT(md_pbuf != NULL); 19861 ASSERT(md_pbuf_nxt == NULL); 19862 ASSERT(pbuf_idx != -1); 19863 ASSERT(pbuf_idx_nxt == -1); 19864 ASSERT(*usable > 0); 19865 19866 /* 19867 * We spillover to the next payload buffer only 19868 * if all of the following is true: 19869 * 19870 * 1. There is not enough data on the current 19871 * payload buffer to make up `len', 19872 * 2. We are allowed to send `len', 19873 * 3. The next payload buffer length is large 19874 * enough to accomodate `spill'. 19875 */ 19876 if ((spill = len - *tail_unsent) > 0 && 19877 *usable >= len && 19878 MBLKL((*xmit_tail)->b_cont) >= spill && 19879 max_pld > 0) { 19880 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19881 if (md_pbuf_nxt == NULL) { 19882 TCP_STAT(tcps, tcp_mdt_allocfail); 19883 goto legacy_send; /* out_of_mem */ 19884 } 19885 19886 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19887 zc_cap != NULL) { 19888 if (!ip_md_zcopy_attr(mmd, NULL, 19889 zc_cap->ill_zerocopy_flags)) { 19890 freeb(md_pbuf_nxt); 19891 TCP_STAT(tcps, 19892 tcp_mdt_allocfail); 19893 /* out_of_mem */ 19894 goto legacy_send; 19895 } 19896 zcopy = B_TRUE; 19897 } 19898 19899 /* 19900 * See comments above on the first call to 19901 * mmd_addpldbuf for explanation on the panic. 19902 */ 19903 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19904 if (pbuf_idx_nxt < 0) { 19905 panic("tcp_multisend: " 19906 "next payload buffer logic error " 19907 "detected for tcp %p mmd %p " 19908 "pbuf %p (%d)\n", 19909 (void *)tcp, (void *)mmd, 19910 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19911 } 19912 19913 ASSERT(max_pld > 0); 19914 --max_pld; 19915 } else if (spill > 0) { 19916 /* 19917 * If there's a spillover, but the following 19918 * xmit_tail couldn't give us enough octets 19919 * to reach "len", then stop the current 19920 * Multidata creation and let the legacy 19921 * tcp_send() path take over. We don't want 19922 * to send the tiny segment as part of this 19923 * Multidata for performance reasons; instead, 19924 * we let the legacy path deal with grouping 19925 * it with the subsequent small mblks. 19926 */ 19927 if (*usable >= len && 19928 MBLKL((*xmit_tail)->b_cont) < spill) { 19929 max_pld = 0; 19930 break; /* done */ 19931 } 19932 19933 /* 19934 * We can't spillover, and we are near 19935 * the end of the current payload buffer, 19936 * so send what's left. 19937 */ 19938 ASSERT(*tail_unsent > 0); 19939 len = *tail_unsent; 19940 } 19941 19942 /* tail_unsent is negated if there is a spillover */ 19943 *tail_unsent -= len; 19944 *usable -= len; 19945 ASSERT(*usable >= 0); 19946 19947 if (*usable < mss) 19948 seg_len = *usable; 19949 /* 19950 * Sender SWS avoidance; see comments in tcp_send(); 19951 * everything else is the same, except that we only 19952 * do this here if there is no more data to be sent 19953 * following the current xmit_tail. We don't check 19954 * for 1-byte urgent data because we shouldn't get 19955 * here if TCP_URG_VALID is set. 19956 */ 19957 if (*usable > 0 && *usable < mss && 19958 ((md_pbuf_nxt == NULL && 19959 (*xmit_tail)->b_cont == NULL) || 19960 (md_pbuf_nxt != NULL && 19961 (*xmit_tail)->b_cont->b_cont == NULL)) && 19962 seg_len < (tcp->tcp_max_swnd >> 1) && 19963 (tcp->tcp_unsent - 19964 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19965 !tcp->tcp_zero_win_probe) { 19966 if ((*snxt + len) == tcp->tcp_snxt && 19967 (*snxt + len) == tcp->tcp_suna) { 19968 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19969 } 19970 done = B_TRUE; 19971 } 19972 19973 /* 19974 * Prime pump for IP's checksumming on our behalf; 19975 * include the adjustment for a source route if any. 19976 * Do this only for software/partial hardware checksum 19977 * offload, as this field gets zeroed out later for 19978 * the full hardware checksum offload case. 19979 */ 19980 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19981 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19982 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19983 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19984 } 19985 19986 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19987 *snxt += len; 19988 19989 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19990 /* 19991 * We set the PUSH bit only if TCP has no more buffered 19992 * data to be transmitted (or if sender SWS avoidance 19993 * takes place), as opposed to setting it for every 19994 * last packet in the burst. 19995 */ 19996 if (done || 19997 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19998 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19999 20000 /* 20001 * Set FIN bit if this is our last segment; snxt 20002 * already includes its length, and it will not 20003 * be adjusted after this point. 20004 */ 20005 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 20006 *snxt == tcp->tcp_fss) { 20007 if (!tcp->tcp_fin_acked) { 20008 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 20009 BUMP_MIB(&tcps->tcps_mib, 20010 tcpOutControl); 20011 } 20012 if (!tcp->tcp_fin_sent) { 20013 tcp->tcp_fin_sent = B_TRUE; 20014 /* 20015 * tcp state must be ESTABLISHED 20016 * in order for us to get here in 20017 * the first place. 20018 */ 20019 tcp->tcp_state = TCPS_FIN_WAIT_1; 20020 20021 /* 20022 * Upon returning from this routine, 20023 * tcp_wput_data() will set tcp_snxt 20024 * to be equal to snxt + tcp_fin_sent. 20025 * This is essentially the same as 20026 * setting it to tcp_fss + 1. 20027 */ 20028 } 20029 } 20030 20031 tcp->tcp_last_sent_len = (ushort_t)len; 20032 20033 len += tcp_hdr_len; 20034 if (tcp->tcp_ipversion == IPV4_VERSION) 20035 tcp->tcp_ipha->ipha_length = htons(len); 20036 else 20037 tcp->tcp_ip6h->ip6_plen = htons(len - 20038 ((char *)&tcp->tcp_ip6h[1] - 20039 tcp->tcp_iphc)); 20040 20041 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 20042 20043 /* setup header fragment */ 20044 PDESC_HDR_ADD(pkt_info, 20045 md_hbuf->b_rptr + cur_hdr_off, /* base */ 20046 tcp->tcp_mdt_hdr_head, /* head room */ 20047 tcp_hdr_len, /* len */ 20048 tcp->tcp_mdt_hdr_tail); /* tail room */ 20049 20050 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 20051 hdr_frag_sz); 20052 ASSERT(MBLKIN(md_hbuf, 20053 (pkt_info->hdr_base - md_hbuf->b_rptr), 20054 PDESC_HDRSIZE(pkt_info))); 20055 20056 /* setup first payload fragment */ 20057 PDESC_PLD_INIT(pkt_info); 20058 PDESC_PLD_SPAN_ADD(pkt_info, 20059 pbuf_idx, /* index */ 20060 md_pbuf->b_rptr + cur_pld_off, /* start */ 20061 tcp->tcp_last_sent_len); /* len */ 20062 20063 /* create a split-packet in case of a spillover */ 20064 if (md_pbuf_nxt != NULL) { 20065 ASSERT(spill > 0); 20066 ASSERT(pbuf_idx_nxt > pbuf_idx); 20067 ASSERT(!add_buffer); 20068 20069 md_pbuf = md_pbuf_nxt; 20070 md_pbuf_nxt = NULL; 20071 pbuf_idx = pbuf_idx_nxt; 20072 pbuf_idx_nxt = -1; 20073 cur_pld_off = spill; 20074 20075 /* trim out first payload fragment */ 20076 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 20077 20078 /* setup second payload fragment */ 20079 PDESC_PLD_SPAN_ADD(pkt_info, 20080 pbuf_idx, /* index */ 20081 md_pbuf->b_rptr, /* start */ 20082 spill); /* len */ 20083 20084 if ((*xmit_tail)->b_next == NULL) { 20085 /* 20086 * Store the lbolt used for RTT 20087 * estimation. We can only record one 20088 * timestamp per mblk so we do it when 20089 * we reach the end of the payload 20090 * buffer. Also we only take a new 20091 * timestamp sample when the previous 20092 * timed data from the same mblk has 20093 * been ack'ed. 20094 */ 20095 (*xmit_tail)->b_prev = local_time; 20096 (*xmit_tail)->b_next = 20097 (mblk_t *)(uintptr_t)first_snxt; 20098 } 20099 20100 first_snxt = *snxt - spill; 20101 20102 /* 20103 * Advance xmit_tail; usable could be 0 by 20104 * the time we got here, but we made sure 20105 * above that we would only spillover to 20106 * the next data block if usable includes 20107 * the spilled-over amount prior to the 20108 * subtraction. Therefore, we are sure 20109 * that xmit_tail->b_cont can't be NULL. 20110 */ 20111 ASSERT((*xmit_tail)->b_cont != NULL); 20112 *xmit_tail = (*xmit_tail)->b_cont; 20113 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20114 (uintptr_t)INT_MAX); 20115 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 20116 } else { 20117 cur_pld_off += tcp->tcp_last_sent_len; 20118 } 20119 20120 /* 20121 * Fill in the header using the template header, and 20122 * add options such as time-stamp, ECN and/or SACK, 20123 * as needed. 20124 */ 20125 tcp_fill_header(tcp, pkt_info->hdr_rptr, 20126 (clock_t)local_time, num_sack_blk); 20127 20128 /* take care of some IP header businesses */ 20129 if (af == AF_INET) { 20130 ipha = (ipha_t *)pkt_info->hdr_rptr; 20131 20132 ASSERT(OK_32PTR((uchar_t *)ipha)); 20133 ASSERT(PDESC_HDRL(pkt_info) >= 20134 IP_SIMPLE_HDR_LENGTH); 20135 ASSERT(ipha->ipha_version_and_hdr_length == 20136 IP_SIMPLE_HDR_VERSION); 20137 20138 /* 20139 * Assign ident value for current packet; see 20140 * related comments in ip_wput_ire() about the 20141 * contract private interface with clustering 20142 * group. 20143 */ 20144 clusterwide = B_FALSE; 20145 if (cl_inet_ipident != NULL) { 20146 ASSERT(cl_inet_isclusterwide != NULL); 20147 if ((*cl_inet_isclusterwide)(stack_id, 20148 IPPROTO_IP, AF_INET, 20149 (uint8_t *)(uintptr_t)src, NULL)) { 20150 ipha->ipha_ident = 20151 (*cl_inet_ipident)(stack_id, 20152 IPPROTO_IP, AF_INET, 20153 (uint8_t *)(uintptr_t)src, 20154 (uint8_t *)(uintptr_t)dst, 20155 NULL); 20156 clusterwide = B_TRUE; 20157 } 20158 } 20159 20160 if (!clusterwide) { 20161 ipha->ipha_ident = (uint16_t) 20162 atomic_add_32_nv( 20163 &ire->ire_ident, 1); 20164 } 20165 #ifndef _BIG_ENDIAN 20166 ipha->ipha_ident = (ipha->ipha_ident << 8) | 20167 (ipha->ipha_ident >> 8); 20168 #endif 20169 } else { 20170 ip6h = (ip6_t *)pkt_info->hdr_rptr; 20171 20172 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20173 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20174 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20175 ASSERT(PDESC_HDRL(pkt_info) >= 20176 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20177 TCP_CHECKSUM_SIZE)); 20178 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20179 20180 if (tcp->tcp_ip_forward_progress) { 20181 rconfirm = B_TRUE; 20182 tcp->tcp_ip_forward_progress = B_FALSE; 20183 } 20184 } 20185 20186 /* at least one payload span, and at most two */ 20187 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20188 20189 /* add the packet descriptor to Multidata */ 20190 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20191 KM_NOSLEEP)) == NULL) { 20192 /* 20193 * Any failure other than ENOMEM indicates 20194 * that we have passed in invalid pkt_info 20195 * or parameters to mmd_addpdesc, which must 20196 * not happen. 20197 * 20198 * EINVAL is a result of failure on boundary 20199 * checks against the pkt_info contents. It 20200 * should not happen, and we panic because 20201 * either there's horrible heap corruption, 20202 * and/or programming mistake. 20203 */ 20204 if (err != ENOMEM) { 20205 cmn_err(CE_PANIC, "tcp_multisend: " 20206 "pdesc logic error detected for " 20207 "tcp %p mmd %p pinfo %p (%d)\n", 20208 (void *)tcp, (void *)mmd, 20209 (void *)pkt_info, err); 20210 } 20211 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20212 goto legacy_send; /* out_of_mem */ 20213 } 20214 ASSERT(pkt != NULL); 20215 20216 /* calculate IP header and TCP checksums */ 20217 if (af == AF_INET) { 20218 /* calculate pseudo-header checksum */ 20219 cksum = (dst >> 16) + (dst & 0xFFFF) + 20220 (src >> 16) + (src & 0xFFFF); 20221 20222 /* offset for TCP header checksum */ 20223 up = IPH_TCPH_CHECKSUMP(ipha, 20224 IP_SIMPLE_HDR_LENGTH); 20225 } else { 20226 up = (uint16_t *)&ip6h->ip6_src; 20227 20228 /* calculate pseudo-header checksum */ 20229 cksum = up[0] + up[1] + up[2] + up[3] + 20230 up[4] + up[5] + up[6] + up[7] + 20231 up[8] + up[9] + up[10] + up[11] + 20232 up[12] + up[13] + up[14] + up[15]; 20233 20234 /* Fold the initial sum */ 20235 cksum = (cksum & 0xffff) + (cksum >> 16); 20236 20237 up = (uint16_t *)(((uchar_t *)ip6h) + 20238 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20239 } 20240 20241 if (hwcksum_flags & HCK_FULLCKSUM) { 20242 /* clear checksum field for hardware */ 20243 *up = 0; 20244 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20245 uint32_t sum; 20246 20247 /* pseudo-header checksumming */ 20248 sum = *up + cksum + IP_TCP_CSUM_COMP; 20249 sum = (sum & 0xFFFF) + (sum >> 16); 20250 *up = (sum & 0xFFFF) + (sum >> 16); 20251 } else { 20252 /* software checksumming */ 20253 TCP_STAT(tcps, tcp_out_sw_cksum); 20254 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20255 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20256 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20257 cksum + IP_TCP_CSUM_COMP); 20258 if (*up == 0) 20259 *up = 0xFFFF; 20260 } 20261 20262 /* IPv4 header checksum */ 20263 if (af == AF_INET) { 20264 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20265 ipha->ipha_hdr_checksum = 0; 20266 } else { 20267 IP_HDR_CKSUM(ipha, cksum, 20268 ((uint32_t *)ipha)[0], 20269 ((uint16_t *)ipha)[4]); 20270 } 20271 } 20272 20273 if (af == AF_INET && 20274 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20275 af == AF_INET6 && 20276 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20277 mblk_t *mp, *mp1; 20278 uchar_t *hdr_rptr, *hdr_wptr; 20279 uchar_t *pld_rptr, *pld_wptr; 20280 20281 /* 20282 * We reconstruct a pseudo packet for the hooks 20283 * framework using mmd_transform_link(). 20284 * If it is a split packet we pullup the 20285 * payload. FW_HOOKS expects a pkt comprising 20286 * of two mblks: a header and the payload. 20287 */ 20288 if ((mp = mmd_transform_link(pkt)) == NULL) { 20289 TCP_STAT(tcps, tcp_mdt_allocfail); 20290 goto legacy_send; 20291 } 20292 20293 if (pkt_info->pld_cnt > 1) { 20294 /* split payload, more than one pld */ 20295 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20296 NULL) { 20297 freemsg(mp); 20298 TCP_STAT(tcps, 20299 tcp_mdt_allocfail); 20300 goto legacy_send; 20301 } 20302 freemsg(mp->b_cont); 20303 mp->b_cont = mp1; 20304 } else { 20305 mp1 = mp->b_cont; 20306 } 20307 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20308 20309 /* 20310 * Remember the message offsets. This is so we 20311 * can detect changes when we return from the 20312 * FW_HOOKS callbacks. 20313 */ 20314 hdr_rptr = mp->b_rptr; 20315 hdr_wptr = mp->b_wptr; 20316 pld_rptr = mp->b_cont->b_rptr; 20317 pld_wptr = mp->b_cont->b_wptr; 20318 20319 if (af == AF_INET) { 20320 DTRACE_PROBE4( 20321 ip4__physical__out__start, 20322 ill_t *, NULL, 20323 ill_t *, ill, 20324 ipha_t *, ipha, 20325 mblk_t *, mp); 20326 FW_HOOKS( 20327 ipst->ips_ip4_physical_out_event, 20328 ipst->ips_ipv4firewall_physical_out, 20329 NULL, ill, ipha, mp, mp, 0, ipst); 20330 DTRACE_PROBE1( 20331 ip4__physical__out__end, 20332 mblk_t *, mp); 20333 } else { 20334 DTRACE_PROBE4( 20335 ip6__physical__out_start, 20336 ill_t *, NULL, 20337 ill_t *, ill, 20338 ip6_t *, ip6h, 20339 mblk_t *, mp); 20340 FW_HOOKS6( 20341 ipst->ips_ip6_physical_out_event, 20342 ipst->ips_ipv6firewall_physical_out, 20343 NULL, ill, ip6h, mp, mp, 0, ipst); 20344 DTRACE_PROBE1( 20345 ip6__physical__out__end, 20346 mblk_t *, mp); 20347 } 20348 20349 if (mp == NULL || 20350 (mp1 = mp->b_cont) == NULL || 20351 mp->b_rptr != hdr_rptr || 20352 mp->b_wptr != hdr_wptr || 20353 mp1->b_rptr != pld_rptr || 20354 mp1->b_wptr != pld_wptr || 20355 mp1->b_cont != NULL) { 20356 /* 20357 * We abandon multidata processing and 20358 * return to the normal path, either 20359 * when a packet is blocked, or when 20360 * the boundaries of header buffer or 20361 * payload buffer have been changed by 20362 * FW_HOOKS[6]. 20363 */ 20364 if (mp != NULL) 20365 freemsg(mp); 20366 goto legacy_send; 20367 } 20368 /* Finished with the pseudo packet */ 20369 freemsg(mp); 20370 } 20371 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20372 ill, ipha, ip6h); 20373 /* advance header offset */ 20374 cur_hdr_off += hdr_frag_sz; 20375 20376 obbytes += tcp->tcp_last_sent_len; 20377 ++obsegs; 20378 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20379 *tail_unsent > 0); 20380 20381 if ((*xmit_tail)->b_next == NULL) { 20382 /* 20383 * Store the lbolt used for RTT estimation. We can only 20384 * record one timestamp per mblk so we do it when we 20385 * reach the end of the payload buffer. Also we only 20386 * take a new timestamp sample when the previous timed 20387 * data from the same mblk has been ack'ed. 20388 */ 20389 (*xmit_tail)->b_prev = local_time; 20390 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20391 } 20392 20393 ASSERT(*tail_unsent >= 0); 20394 if (*tail_unsent > 0) { 20395 /* 20396 * We got here because we broke out of the above 20397 * loop due to of one of the following cases: 20398 * 20399 * 1. len < adjusted MSS (i.e. small), 20400 * 2. Sender SWS avoidance, 20401 * 3. max_pld is zero. 20402 * 20403 * We are done for this Multidata, so trim our 20404 * last payload buffer (if any) accordingly. 20405 */ 20406 if (md_pbuf != NULL) 20407 md_pbuf->b_wptr -= *tail_unsent; 20408 } else if (*usable > 0) { 20409 *xmit_tail = (*xmit_tail)->b_cont; 20410 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20411 (uintptr_t)INT_MAX); 20412 *tail_unsent = (int)MBLKL(*xmit_tail); 20413 add_buffer = B_TRUE; 20414 } 20415 } while (!done && *usable > 0 && num_burst_seg > 0 && 20416 (tcp_mdt_chain || max_pld > 0)); 20417 20418 if (md_mp_head != NULL) { 20419 /* send everything down */ 20420 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20421 &rconfirm); 20422 } 20423 20424 #undef PREP_NEW_MULTIDATA 20425 #undef PREP_NEW_PBUF 20426 #undef IPVER 20427 20428 IRE_REFRELE(ire); 20429 return (0); 20430 } 20431 20432 /* 20433 * A wrapper function for sending one or more Multidata messages down to 20434 * the module below ip; this routine does not release the reference of the 20435 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20436 */ 20437 static void 20438 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20439 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20440 { 20441 uint64_t delta; 20442 nce_t *nce; 20443 tcp_stack_t *tcps = tcp->tcp_tcps; 20444 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20445 20446 ASSERT(ire != NULL && ill != NULL); 20447 ASSERT(ire->ire_stq != NULL); 20448 ASSERT(md_mp_head != NULL); 20449 ASSERT(rconfirm != NULL); 20450 20451 /* adjust MIBs and IRE timestamp */ 20452 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20453 tcp->tcp_obsegs += obsegs; 20454 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20455 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20456 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20457 20458 if (tcp->tcp_ipversion == IPV4_VERSION) { 20459 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20460 } else { 20461 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20462 } 20463 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20464 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20465 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20466 20467 ire->ire_ob_pkt_count += obsegs; 20468 if (ire->ire_ipif != NULL) 20469 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20470 ire->ire_last_used_time = lbolt; 20471 20472 if (ipst->ips_ipobs_enabled) { 20473 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20474 pdesc_t *dl_pkt; 20475 pdescinfo_t pinfo; 20476 mblk_t *nmp; 20477 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20478 20479 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20480 (dl_pkt != NULL); 20481 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20482 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20483 continue; 20484 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20485 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20486 freemsg(nmp); 20487 } 20488 } 20489 20490 /* send it down */ 20491 putnext(ire->ire_stq, md_mp_head); 20492 20493 /* we're done for TCP/IPv4 */ 20494 if (tcp->tcp_ipversion == IPV4_VERSION) 20495 return; 20496 20497 nce = ire->ire_nce; 20498 20499 ASSERT(nce != NULL); 20500 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20501 ASSERT(nce->nce_state != ND_INCOMPLETE); 20502 20503 /* reachability confirmation? */ 20504 if (*rconfirm) { 20505 nce->nce_last = TICK_TO_MSEC(lbolt64); 20506 if (nce->nce_state != ND_REACHABLE) { 20507 mutex_enter(&nce->nce_lock); 20508 nce->nce_state = ND_REACHABLE; 20509 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20510 mutex_exit(&nce->nce_lock); 20511 (void) untimeout(nce->nce_timeout_id); 20512 if (ip_debug > 2) { 20513 /* ip1dbg */ 20514 pr_addr_dbg("tcp_multisend_data: state " 20515 "for %s changed to REACHABLE\n", 20516 AF_INET6, &ire->ire_addr_v6); 20517 } 20518 } 20519 /* reset transport reachability confirmation */ 20520 *rconfirm = B_FALSE; 20521 } 20522 20523 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20524 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20525 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20526 20527 if (delta > (uint64_t)ill->ill_reachable_time) { 20528 mutex_enter(&nce->nce_lock); 20529 switch (nce->nce_state) { 20530 case ND_REACHABLE: 20531 case ND_STALE: 20532 /* 20533 * ND_REACHABLE is identical to ND_STALE in this 20534 * specific case. If reachable time has expired for 20535 * this neighbor (delta is greater than reachable 20536 * time), conceptually, the neighbor cache is no 20537 * longer in REACHABLE state, but already in STALE 20538 * state. So the correct transition here is to 20539 * ND_DELAY. 20540 */ 20541 nce->nce_state = ND_DELAY; 20542 mutex_exit(&nce->nce_lock); 20543 NDP_RESTART_TIMER(nce, 20544 ipst->ips_delay_first_probe_time); 20545 if (ip_debug > 3) { 20546 /* ip2dbg */ 20547 pr_addr_dbg("tcp_multisend_data: state " 20548 "for %s changed to DELAY\n", 20549 AF_INET6, &ire->ire_addr_v6); 20550 } 20551 break; 20552 case ND_DELAY: 20553 case ND_PROBE: 20554 mutex_exit(&nce->nce_lock); 20555 /* Timers have already started */ 20556 break; 20557 case ND_UNREACHABLE: 20558 /* 20559 * ndp timer has detected that this nce is 20560 * unreachable and initiated deleting this nce 20561 * and all its associated IREs. This is a race 20562 * where we found the ire before it was deleted 20563 * and have just sent out a packet using this 20564 * unreachable nce. 20565 */ 20566 mutex_exit(&nce->nce_lock); 20567 break; 20568 default: 20569 ASSERT(0); 20570 } 20571 } 20572 } 20573 20574 /* 20575 * Derived from tcp_send_data(). 20576 */ 20577 static void 20578 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20579 int num_lso_seg) 20580 { 20581 ipha_t *ipha; 20582 mblk_t *ire_fp_mp; 20583 uint_t ire_fp_mp_len; 20584 uint32_t hcksum_txflags = 0; 20585 ipaddr_t src; 20586 ipaddr_t dst; 20587 uint32_t cksum; 20588 uint16_t *up; 20589 tcp_stack_t *tcps = tcp->tcp_tcps; 20590 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20591 20592 ASSERT(DB_TYPE(mp) == M_DATA); 20593 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20594 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20595 ASSERT(tcp->tcp_connp != NULL); 20596 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20597 20598 ipha = (ipha_t *)mp->b_rptr; 20599 src = ipha->ipha_src; 20600 dst = ipha->ipha_dst; 20601 20602 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20603 20604 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20605 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20606 num_lso_seg); 20607 #ifndef _BIG_ENDIAN 20608 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20609 #endif 20610 if (tcp->tcp_snd_zcopy_aware) { 20611 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20612 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20613 mp = tcp_zcopy_disable(tcp, mp); 20614 } 20615 20616 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20617 ASSERT(ill->ill_hcksum_capab != NULL); 20618 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20619 } 20620 20621 /* 20622 * Since the TCP checksum should be recalculated by h/w, we can just 20623 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20624 * pseudo-header checksum for HCK_PARTIALCKSUM. 20625 * The partial pseudo-header excludes TCP length, that was calculated 20626 * in tcp_send(), so to zero *up before further processing. 20627 */ 20628 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20629 20630 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20631 *up = 0; 20632 20633 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20634 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20635 20636 /* 20637 * Append LSO flags and mss to the mp. 20638 */ 20639 lso_info_set(mp, mss, HW_LSO); 20640 20641 ipha->ipha_fragment_offset_and_flags |= 20642 (uint32_t)htons(ire->ire_frag_flag); 20643 20644 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20645 ire_fp_mp_len = MBLKL(ire_fp_mp); 20646 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20647 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20648 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20649 20650 UPDATE_OB_PKT_COUNT(ire); 20651 ire->ire_last_used_time = lbolt; 20652 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20653 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20654 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20655 ntohs(ipha->ipha_length)); 20656 20657 DTRACE_PROBE4(ip4__physical__out__start, 20658 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20659 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20660 ipst->ips_ipv4firewall_physical_out, NULL, 20661 ill, ipha, mp, mp, 0, ipst); 20662 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20663 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20664 20665 if (mp != NULL) { 20666 if (ipst->ips_ipobs_enabled) { 20667 zoneid_t szone; 20668 20669 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20670 ipst, ALL_ZONES); 20671 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20672 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20673 } 20674 20675 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20676 } 20677 } 20678 20679 /* 20680 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20681 * scheme, and returns one of the following: 20682 * 20683 * -1 = failed allocation. 20684 * 0 = success; burst count reached, or usable send window is too small, 20685 * and that we'd rather wait until later before sending again. 20686 * 1 = success; we are called from tcp_multisend(), and both usable send 20687 * window and tail_unsent are greater than the MDT threshold, and thus 20688 * Multidata Transmit should be used instead. 20689 */ 20690 static int 20691 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20692 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20693 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20694 const int mdt_thres) 20695 { 20696 int num_burst_seg = tcp->tcp_snd_burst; 20697 ire_t *ire = NULL; 20698 ill_t *ill = NULL; 20699 mblk_t *ire_fp_mp = NULL; 20700 uint_t ire_fp_mp_len = 0; 20701 int num_lso_seg = 1; 20702 uint_t lso_usable; 20703 boolean_t do_lso_send = B_FALSE; 20704 tcp_stack_t *tcps = tcp->tcp_tcps; 20705 20706 /* 20707 * Check LSO capability before any further work. And the similar check 20708 * need to be done in for(;;) loop. 20709 * LSO will be deployed when therer is more than one mss of available 20710 * data and a burst transmission is allowed. 20711 */ 20712 if (tcp->tcp_lso && 20713 (tcp->tcp_valid_bits == 0 || 20714 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20715 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20716 /* 20717 * Try to find usable IRE/ILL and do basic check to the ILL. 20718 * Double check LSO usability before going further, since the 20719 * underlying interface could have been changed. In case of any 20720 * change of LSO capability, set tcp_ire_ill_check_done to 20721 * B_FALSE to force to check the ILL with the next send. 20722 */ 20723 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20724 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20725 /* 20726 * Enable LSO with this transmission. 20727 * Since IRE has been hold in tcp_send_find_ire_ill(), 20728 * IRE_REFRELE(ire) should be called before return. 20729 */ 20730 do_lso_send = B_TRUE; 20731 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20732 ire_fp_mp_len = MBLKL(ire_fp_mp); 20733 /* Round up to multiple of 4 */ 20734 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20735 } else { 20736 tcp->tcp_lso = B_FALSE; 20737 tcp->tcp_ire_ill_check_done = B_FALSE; 20738 do_lso_send = B_FALSE; 20739 ill = NULL; 20740 } 20741 } 20742 20743 for (;;) { 20744 struct datab *db; 20745 tcph_t *tcph; 20746 uint32_t sum; 20747 mblk_t *mp, *mp1; 20748 uchar_t *rptr; 20749 int len; 20750 20751 /* 20752 * If we're called by tcp_multisend(), and the amount of 20753 * sendable data as well as the size of current xmit_tail 20754 * is beyond the MDT threshold, return to the caller and 20755 * let the large data transmit be done using MDT. 20756 */ 20757 if (*usable > 0 && *usable > mdt_thres && 20758 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20759 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20760 ASSERT(tcp->tcp_mdt); 20761 return (1); /* success; do large send */ 20762 } 20763 20764 if (num_burst_seg == 0) 20765 break; /* success; burst count reached */ 20766 20767 /* 20768 * Calculate the maximum payload length we can send in *one* 20769 * time. 20770 */ 20771 if (do_lso_send) { 20772 /* 20773 * Check whether need to do LSO any more. 20774 */ 20775 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20776 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20777 lso_usable = MIN(lso_usable, 20778 num_burst_seg * mss); 20779 20780 num_lso_seg = lso_usable / mss; 20781 if (lso_usable % mss) { 20782 num_lso_seg++; 20783 tcp->tcp_last_sent_len = (ushort_t) 20784 (lso_usable % mss); 20785 } else { 20786 tcp->tcp_last_sent_len = (ushort_t)mss; 20787 } 20788 } else { 20789 do_lso_send = B_FALSE; 20790 num_lso_seg = 1; 20791 lso_usable = mss; 20792 } 20793 } 20794 20795 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20796 20797 /* 20798 * Adjust num_burst_seg here. 20799 */ 20800 num_burst_seg -= num_lso_seg; 20801 20802 len = mss; 20803 if (len > *usable) { 20804 ASSERT(do_lso_send == B_FALSE); 20805 20806 len = *usable; 20807 if (len <= 0) { 20808 /* Terminate the loop */ 20809 break; /* success; too small */ 20810 } 20811 /* 20812 * Sender silly-window avoidance. 20813 * Ignore this if we are going to send a 20814 * zero window probe out. 20815 * 20816 * TODO: force data into microscopic window? 20817 * ==> (!pushed || (unsent > usable)) 20818 */ 20819 if (len < (tcp->tcp_max_swnd >> 1) && 20820 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20821 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20822 len == 1) && (! tcp->tcp_zero_win_probe)) { 20823 /* 20824 * If the retransmit timer is not running 20825 * we start it so that we will retransmit 20826 * in the case when the the receiver has 20827 * decremented the window. 20828 */ 20829 if (*snxt == tcp->tcp_snxt && 20830 *snxt == tcp->tcp_suna) { 20831 /* 20832 * We are not supposed to send 20833 * anything. So let's wait a little 20834 * bit longer before breaking SWS 20835 * avoidance. 20836 * 20837 * What should the value be? 20838 * Suggestion: MAX(init rexmit time, 20839 * tcp->tcp_rto) 20840 */ 20841 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20842 } 20843 break; /* success; too small */ 20844 } 20845 } 20846 20847 tcph = tcp->tcp_tcph; 20848 20849 /* 20850 * The reason to adjust len here is that we need to set flags 20851 * and calculate checksum. 20852 */ 20853 if (do_lso_send) 20854 len = lso_usable; 20855 20856 *usable -= len; /* Approximate - can be adjusted later */ 20857 if (*usable > 0) 20858 tcph->th_flags[0] = TH_ACK; 20859 else 20860 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20861 20862 /* 20863 * Prime pump for IP's checksumming on our behalf 20864 * Include the adjustment for a source route if any. 20865 */ 20866 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20867 sum = (sum >> 16) + (sum & 0xFFFF); 20868 U16_TO_ABE16(sum, tcph->th_sum); 20869 20870 U32_TO_ABE32(*snxt, tcph->th_seq); 20871 20872 /* 20873 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20874 * set. For the case when TCP_FSS_VALID is the only valid 20875 * bit (normal active close), branch off only when we think 20876 * that the FIN flag needs to be set. Note for this case, 20877 * that (snxt + len) may not reflect the actual seg_len, 20878 * as len may be further reduced in tcp_xmit_mp(). If len 20879 * gets modified, we will end up here again. 20880 */ 20881 if (tcp->tcp_valid_bits != 0 && 20882 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20883 ((*snxt + len) == tcp->tcp_fss))) { 20884 uchar_t *prev_rptr; 20885 uint32_t prev_snxt = tcp->tcp_snxt; 20886 20887 if (*tail_unsent == 0) { 20888 ASSERT((*xmit_tail)->b_cont != NULL); 20889 *xmit_tail = (*xmit_tail)->b_cont; 20890 prev_rptr = (*xmit_tail)->b_rptr; 20891 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20892 (*xmit_tail)->b_rptr); 20893 } else { 20894 prev_rptr = (*xmit_tail)->b_rptr; 20895 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20896 *tail_unsent; 20897 } 20898 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20899 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20900 /* Restore tcp_snxt so we get amount sent right. */ 20901 tcp->tcp_snxt = prev_snxt; 20902 if (prev_rptr == (*xmit_tail)->b_rptr) { 20903 /* 20904 * If the previous timestamp is still in use, 20905 * don't stomp on it. 20906 */ 20907 if ((*xmit_tail)->b_next == NULL) { 20908 (*xmit_tail)->b_prev = local_time; 20909 (*xmit_tail)->b_next = 20910 (mblk_t *)(uintptr_t)(*snxt); 20911 } 20912 } else 20913 (*xmit_tail)->b_rptr = prev_rptr; 20914 20915 if (mp == NULL) { 20916 if (ire != NULL) 20917 IRE_REFRELE(ire); 20918 return (-1); 20919 } 20920 mp1 = mp->b_cont; 20921 20922 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20923 tcp->tcp_last_sent_len = (ushort_t)len; 20924 while (mp1->b_cont) { 20925 *xmit_tail = (*xmit_tail)->b_cont; 20926 (*xmit_tail)->b_prev = local_time; 20927 (*xmit_tail)->b_next = 20928 (mblk_t *)(uintptr_t)(*snxt); 20929 mp1 = mp1->b_cont; 20930 } 20931 *snxt += len; 20932 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20933 BUMP_LOCAL(tcp->tcp_obsegs); 20934 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20935 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20936 tcp_send_data(tcp, q, mp); 20937 continue; 20938 } 20939 20940 *snxt += len; /* Adjust later if we don't send all of len */ 20941 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20942 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20943 20944 if (*tail_unsent) { 20945 /* Are the bytes above us in flight? */ 20946 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20947 if (rptr != (*xmit_tail)->b_rptr) { 20948 *tail_unsent -= len; 20949 if (len <= mss) /* LSO is unusable */ 20950 tcp->tcp_last_sent_len = (ushort_t)len; 20951 len += tcp_hdr_len; 20952 if (tcp->tcp_ipversion == IPV4_VERSION) 20953 tcp->tcp_ipha->ipha_length = htons(len); 20954 else 20955 tcp->tcp_ip6h->ip6_plen = 20956 htons(len - 20957 ((char *)&tcp->tcp_ip6h[1] - 20958 tcp->tcp_iphc)); 20959 mp = dupb(*xmit_tail); 20960 if (mp == NULL) { 20961 if (ire != NULL) 20962 IRE_REFRELE(ire); 20963 return (-1); /* out_of_mem */ 20964 } 20965 mp->b_rptr = rptr; 20966 /* 20967 * If the old timestamp is no longer in use, 20968 * sample a new timestamp now. 20969 */ 20970 if ((*xmit_tail)->b_next == NULL) { 20971 (*xmit_tail)->b_prev = local_time; 20972 (*xmit_tail)->b_next = 20973 (mblk_t *)(uintptr_t)(*snxt-len); 20974 } 20975 goto must_alloc; 20976 } 20977 } else { 20978 *xmit_tail = (*xmit_tail)->b_cont; 20979 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20980 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20981 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20982 (*xmit_tail)->b_rptr); 20983 } 20984 20985 (*xmit_tail)->b_prev = local_time; 20986 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20987 20988 *tail_unsent -= len; 20989 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20990 tcp->tcp_last_sent_len = (ushort_t)len; 20991 20992 len += tcp_hdr_len; 20993 if (tcp->tcp_ipversion == IPV4_VERSION) 20994 tcp->tcp_ipha->ipha_length = htons(len); 20995 else 20996 tcp->tcp_ip6h->ip6_plen = htons(len - 20997 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20998 20999 mp = dupb(*xmit_tail); 21000 if (mp == NULL) { 21001 if (ire != NULL) 21002 IRE_REFRELE(ire); 21003 return (-1); /* out_of_mem */ 21004 } 21005 21006 len = tcp_hdr_len; 21007 /* 21008 * There are four reasons to allocate a new hdr mblk: 21009 * 1) The bytes above us are in use by another packet 21010 * 2) We don't have good alignment 21011 * 3) The mblk is being shared 21012 * 4) We don't have enough room for a header 21013 */ 21014 rptr = mp->b_rptr - len; 21015 if (!OK_32PTR(rptr) || 21016 ((db = mp->b_datap), db->db_ref != 2) || 21017 rptr < db->db_base + ire_fp_mp_len) { 21018 /* NOTE: we assume allocb returns an OK_32PTR */ 21019 21020 must_alloc:; 21021 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 21022 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 21023 if (mp1 == NULL) { 21024 freemsg(mp); 21025 if (ire != NULL) 21026 IRE_REFRELE(ire); 21027 return (-1); /* out_of_mem */ 21028 } 21029 mp1->b_cont = mp; 21030 mp = mp1; 21031 /* Leave room for Link Level header */ 21032 len = tcp_hdr_len; 21033 rptr = 21034 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 21035 mp->b_wptr = &rptr[len]; 21036 } 21037 21038 /* 21039 * Fill in the header using the template header, and add 21040 * options such as time-stamp, ECN and/or SACK, as needed. 21041 */ 21042 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 21043 21044 mp->b_rptr = rptr; 21045 21046 if (*tail_unsent) { 21047 int spill = *tail_unsent; 21048 21049 mp1 = mp->b_cont; 21050 if (mp1 == NULL) 21051 mp1 = mp; 21052 21053 /* 21054 * If we're a little short, tack on more mblks until 21055 * there is no more spillover. 21056 */ 21057 while (spill < 0) { 21058 mblk_t *nmp; 21059 int nmpsz; 21060 21061 nmp = (*xmit_tail)->b_cont; 21062 nmpsz = MBLKL(nmp); 21063 21064 /* 21065 * Excess data in mblk; can we split it? 21066 * If MDT is enabled for the connection, 21067 * keep on splitting as this is a transient 21068 * send path. 21069 */ 21070 if (!do_lso_send && !tcp->tcp_mdt && 21071 (spill + nmpsz > 0)) { 21072 /* 21073 * Don't split if stream head was 21074 * told to break up larger writes 21075 * into smaller ones. 21076 */ 21077 if (tcp->tcp_maxpsz > 0) 21078 break; 21079 21080 /* 21081 * Next mblk is less than SMSS/2 21082 * rounded up to nearest 64-byte; 21083 * let it get sent as part of the 21084 * next segment. 21085 */ 21086 if (tcp->tcp_localnet && 21087 !tcp->tcp_cork && 21088 (nmpsz < roundup((mss >> 1), 64))) 21089 break; 21090 } 21091 21092 *xmit_tail = nmp; 21093 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 21094 /* Stash for rtt use later */ 21095 (*xmit_tail)->b_prev = local_time; 21096 (*xmit_tail)->b_next = 21097 (mblk_t *)(uintptr_t)(*snxt - len); 21098 mp1->b_cont = dupb(*xmit_tail); 21099 mp1 = mp1->b_cont; 21100 21101 spill += nmpsz; 21102 if (mp1 == NULL) { 21103 *tail_unsent = spill; 21104 freemsg(mp); 21105 if (ire != NULL) 21106 IRE_REFRELE(ire); 21107 return (-1); /* out_of_mem */ 21108 } 21109 } 21110 21111 /* Trim back any surplus on the last mblk */ 21112 if (spill >= 0) { 21113 mp1->b_wptr -= spill; 21114 *tail_unsent = spill; 21115 } else { 21116 /* 21117 * We did not send everything we could in 21118 * order to remain within the b_cont limit. 21119 */ 21120 *usable -= spill; 21121 *snxt += spill; 21122 tcp->tcp_last_sent_len += spill; 21123 UPDATE_MIB(&tcps->tcps_mib, 21124 tcpOutDataBytes, spill); 21125 /* 21126 * Adjust the checksum 21127 */ 21128 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 21129 sum += spill; 21130 sum = (sum >> 16) + (sum & 0xFFFF); 21131 U16_TO_ABE16(sum, tcph->th_sum); 21132 if (tcp->tcp_ipversion == IPV4_VERSION) { 21133 sum = ntohs( 21134 ((ipha_t *)rptr)->ipha_length) + 21135 spill; 21136 ((ipha_t *)rptr)->ipha_length = 21137 htons(sum); 21138 } else { 21139 sum = ntohs( 21140 ((ip6_t *)rptr)->ip6_plen) + 21141 spill; 21142 ((ip6_t *)rptr)->ip6_plen = 21143 htons(sum); 21144 } 21145 *tail_unsent = 0; 21146 } 21147 } 21148 if (tcp->tcp_ip_forward_progress) { 21149 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21150 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 21151 tcp->tcp_ip_forward_progress = B_FALSE; 21152 } 21153 21154 if (do_lso_send) { 21155 tcp_lsosend_data(tcp, mp, ire, ill, mss, 21156 num_lso_seg); 21157 tcp->tcp_obsegs += num_lso_seg; 21158 21159 TCP_STAT(tcps, tcp_lso_times); 21160 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 21161 } else { 21162 tcp_send_data(tcp, q, mp); 21163 BUMP_LOCAL(tcp->tcp_obsegs); 21164 } 21165 } 21166 21167 if (ire != NULL) 21168 IRE_REFRELE(ire); 21169 return (0); 21170 } 21171 21172 /* Unlink and return any mblk that looks like it contains a MDT info */ 21173 static mblk_t * 21174 tcp_mdt_info_mp(mblk_t *mp) 21175 { 21176 mblk_t *prev_mp; 21177 21178 for (;;) { 21179 prev_mp = mp; 21180 /* no more to process? */ 21181 if ((mp = mp->b_cont) == NULL) 21182 break; 21183 21184 switch (DB_TYPE(mp)) { 21185 case M_CTL: 21186 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21187 continue; 21188 ASSERT(prev_mp != NULL); 21189 prev_mp->b_cont = mp->b_cont; 21190 mp->b_cont = NULL; 21191 return (mp); 21192 default: 21193 break; 21194 } 21195 } 21196 return (mp); 21197 } 21198 21199 /* MDT info update routine, called when IP notifies us about MDT */ 21200 static void 21201 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21202 { 21203 boolean_t prev_state; 21204 tcp_stack_t *tcps = tcp->tcp_tcps; 21205 21206 /* 21207 * IP is telling us to abort MDT on this connection? We know 21208 * this because the capability is only turned off when IP 21209 * encounters some pathological cases, e.g. link-layer change 21210 * where the new driver doesn't support MDT, or in situation 21211 * where MDT usage on the link-layer has been switched off. 21212 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21213 * if the link-layer doesn't support MDT, and if it does, it 21214 * will indicate that the feature is to be turned on. 21215 */ 21216 prev_state = tcp->tcp_mdt; 21217 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21218 if (!tcp->tcp_mdt && !first) { 21219 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21220 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21221 (void *)tcp->tcp_connp)); 21222 } 21223 21224 /* 21225 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21226 * so disable MDT otherwise. The checks are done here 21227 * and in tcp_wput_data(). 21228 */ 21229 if (tcp->tcp_mdt && 21230 (tcp->tcp_ipversion == IPV4_VERSION && 21231 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21232 (tcp->tcp_ipversion == IPV6_VERSION && 21233 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21234 tcp->tcp_mdt = B_FALSE; 21235 21236 if (tcp->tcp_mdt) { 21237 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21238 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21239 "version (%d), expected version is %d", 21240 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21241 tcp->tcp_mdt = B_FALSE; 21242 return; 21243 } 21244 21245 /* 21246 * We need the driver to be able to handle at least three 21247 * spans per packet in order for tcp MDT to be utilized. 21248 * The first is for the header portion, while the rest are 21249 * needed to handle a packet that straddles across two 21250 * virtually non-contiguous buffers; a typical tcp packet 21251 * therefore consists of only two spans. Note that we take 21252 * a zero as "don't care". 21253 */ 21254 if (mdt_capab->ill_mdt_span_limit > 0 && 21255 mdt_capab->ill_mdt_span_limit < 3) { 21256 tcp->tcp_mdt = B_FALSE; 21257 return; 21258 } 21259 21260 /* a zero means driver wants default value */ 21261 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21262 tcps->tcps_mdt_max_pbufs); 21263 if (tcp->tcp_mdt_max_pld == 0) 21264 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21265 21266 /* ensure 32-bit alignment */ 21267 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21268 mdt_capab->ill_mdt_hdr_head), 4); 21269 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21270 mdt_capab->ill_mdt_hdr_tail), 4); 21271 21272 if (!first && !prev_state) { 21273 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21274 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21275 (void *)tcp->tcp_connp)); 21276 } 21277 } 21278 } 21279 21280 /* Unlink and return any mblk that looks like it contains a LSO info */ 21281 static mblk_t * 21282 tcp_lso_info_mp(mblk_t *mp) 21283 { 21284 mblk_t *prev_mp; 21285 21286 for (;;) { 21287 prev_mp = mp; 21288 /* no more to process? */ 21289 if ((mp = mp->b_cont) == NULL) 21290 break; 21291 21292 switch (DB_TYPE(mp)) { 21293 case M_CTL: 21294 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21295 continue; 21296 ASSERT(prev_mp != NULL); 21297 prev_mp->b_cont = mp->b_cont; 21298 mp->b_cont = NULL; 21299 return (mp); 21300 default: 21301 break; 21302 } 21303 } 21304 21305 return (mp); 21306 } 21307 21308 /* LSO info update routine, called when IP notifies us about LSO */ 21309 static void 21310 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21311 { 21312 tcp_stack_t *tcps = tcp->tcp_tcps; 21313 21314 /* 21315 * IP is telling us to abort LSO on this connection? We know 21316 * this because the capability is only turned off when IP 21317 * encounters some pathological cases, e.g. link-layer change 21318 * where the new NIC/driver doesn't support LSO, or in situation 21319 * where LSO usage on the link-layer has been switched off. 21320 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21321 * if the link-layer doesn't support LSO, and if it does, it 21322 * will indicate that the feature is to be turned on. 21323 */ 21324 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21325 TCP_STAT(tcps, tcp_lso_enabled); 21326 21327 /* 21328 * We currently only support LSO on simple TCP/IPv4, 21329 * so disable LSO otherwise. The checks are done here 21330 * and in tcp_wput_data(). 21331 */ 21332 if (tcp->tcp_lso && 21333 (tcp->tcp_ipversion == IPV4_VERSION && 21334 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21335 (tcp->tcp_ipversion == IPV6_VERSION)) { 21336 tcp->tcp_lso = B_FALSE; 21337 TCP_STAT(tcps, tcp_lso_disabled); 21338 } else { 21339 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21340 lso_capab->ill_lso_max); 21341 } 21342 } 21343 21344 static void 21345 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21346 { 21347 conn_t *connp = tcp->tcp_connp; 21348 tcp_stack_t *tcps = tcp->tcp_tcps; 21349 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21350 21351 ASSERT(ire != NULL); 21352 21353 /* 21354 * We may be in the fastpath here, and although we essentially do 21355 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21356 * we try to keep things as brief as possible. After all, these 21357 * are only best-effort checks, and we do more thorough ones prior 21358 * to calling tcp_send()/tcp_multisend(). 21359 */ 21360 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21361 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21362 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21363 !(ire->ire_flags & RTF_MULTIRT) && 21364 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21365 CONN_IS_LSO_MD_FASTPATH(connp)) { 21366 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21367 /* Cache the result */ 21368 connp->conn_lso_ok = B_TRUE; 21369 21370 ASSERT(ill->ill_lso_capab != NULL); 21371 if (!ill->ill_lso_capab->ill_lso_on) { 21372 ill->ill_lso_capab->ill_lso_on = 1; 21373 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21374 "LSO for interface %s\n", (void *)connp, 21375 ill->ill_name)); 21376 } 21377 tcp_lso_update(tcp, ill->ill_lso_capab); 21378 } else if (ipst->ips_ip_multidata_outbound && 21379 ILL_MDT_CAPABLE(ill)) { 21380 /* Cache the result */ 21381 connp->conn_mdt_ok = B_TRUE; 21382 21383 ASSERT(ill->ill_mdt_capab != NULL); 21384 if (!ill->ill_mdt_capab->ill_mdt_on) { 21385 ill->ill_mdt_capab->ill_mdt_on = 1; 21386 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21387 "MDT for interface %s\n", (void *)connp, 21388 ill->ill_name)); 21389 } 21390 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21391 } 21392 } 21393 21394 /* 21395 * The goal is to reduce the number of generated tcp segments by 21396 * setting the maxpsz multiplier to 0; this will have an affect on 21397 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21398 * into each packet, up to SMSS bytes. Doing this reduces the number 21399 * of outbound segments and incoming ACKs, thus allowing for better 21400 * network and system performance. In contrast the legacy behavior 21401 * may result in sending less than SMSS size, because the last mblk 21402 * for some packets may have more data than needed to make up SMSS, 21403 * and the legacy code refused to "split" it. 21404 * 21405 * We apply the new behavior on following situations: 21406 * 21407 * 1) Loopback connections, 21408 * 2) Connections in which the remote peer is not on local subnet, 21409 * 3) Local subnet connections over the bge interface (see below). 21410 * 21411 * Ideally, we would like this behavior to apply for interfaces other 21412 * than bge. However, doing so would negatively impact drivers which 21413 * perform dynamic mapping and unmapping of DMA resources, which are 21414 * increased by setting the maxpsz multiplier to 0 (more mblks per 21415 * packet will be generated by tcp). The bge driver does not suffer 21416 * from this, as it copies the mblks into pre-mapped buffers, and 21417 * therefore does not require more I/O resources than before. 21418 * 21419 * Otherwise, this behavior is present on all network interfaces when 21420 * the destination endpoint is non-local, since reducing the number 21421 * of packets in general is good for the network. 21422 * 21423 * TODO We need to remove this hard-coded conditional for bge once 21424 * a better "self-tuning" mechanism, or a way to comprehend 21425 * the driver transmit strategy is devised. Until the solution 21426 * is found and well understood, we live with this hack. 21427 */ 21428 if (!tcp_static_maxpsz && 21429 (tcp->tcp_loopback || !tcp->tcp_localnet || 21430 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21431 /* override the default value */ 21432 tcp->tcp_maxpsz = 0; 21433 21434 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21435 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21436 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21437 } 21438 21439 /* set the stream head parameters accordingly */ 21440 (void) tcp_maxpsz_set(tcp, B_TRUE); 21441 } 21442 21443 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21444 static void 21445 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21446 { 21447 uchar_t fval = *mp->b_rptr; 21448 mblk_t *tail; 21449 queue_t *q = tcp->tcp_wq; 21450 21451 /* TODO: How should flush interact with urgent data? */ 21452 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21453 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21454 /* 21455 * Flush only data that has not yet been put on the wire. If 21456 * we flush data that we have already transmitted, life, as we 21457 * know it, may come to an end. 21458 */ 21459 tail = tcp->tcp_xmit_tail; 21460 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21461 tcp->tcp_xmit_tail_unsent = 0; 21462 tcp->tcp_unsent = 0; 21463 if (tail->b_wptr != tail->b_rptr) 21464 tail = tail->b_cont; 21465 if (tail) { 21466 mblk_t **excess = &tcp->tcp_xmit_head; 21467 for (;;) { 21468 mblk_t *mp1 = *excess; 21469 if (mp1 == tail) 21470 break; 21471 tcp->tcp_xmit_tail = mp1; 21472 tcp->tcp_xmit_last = mp1; 21473 excess = &mp1->b_cont; 21474 } 21475 *excess = NULL; 21476 tcp_close_mpp(&tail); 21477 if (tcp->tcp_snd_zcopy_aware) 21478 tcp_zcopy_notify(tcp); 21479 } 21480 /* 21481 * We have no unsent data, so unsent must be less than 21482 * tcp_xmit_lowater, so re-enable flow. 21483 */ 21484 mutex_enter(&tcp->tcp_non_sq_lock); 21485 if (tcp->tcp_flow_stopped) { 21486 tcp_clrqfull(tcp); 21487 } 21488 mutex_exit(&tcp->tcp_non_sq_lock); 21489 } 21490 /* 21491 * TODO: you can't just flush these, you have to increase rwnd for one 21492 * thing. For another, how should urgent data interact? 21493 */ 21494 if (fval & FLUSHR) { 21495 *mp->b_rptr = fval & ~FLUSHW; 21496 /* XXX */ 21497 qreply(q, mp); 21498 return; 21499 } 21500 freemsg(mp); 21501 } 21502 21503 /* 21504 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21505 * messages. 21506 */ 21507 static void 21508 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21509 { 21510 mblk_t *mp1; 21511 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21512 STRUCT_HANDLE(strbuf, sb); 21513 queue_t *q = tcp->tcp_wq; 21514 int error; 21515 uint_t addrlen; 21516 21517 /* Make sure it is one of ours. */ 21518 switch (iocp->ioc_cmd) { 21519 case TI_GETMYNAME: 21520 case TI_GETPEERNAME: 21521 break; 21522 default: 21523 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21524 return; 21525 } 21526 switch (mi_copy_state(q, mp, &mp1)) { 21527 case -1: 21528 return; 21529 case MI_COPY_CASE(MI_COPY_IN, 1): 21530 break; 21531 case MI_COPY_CASE(MI_COPY_OUT, 1): 21532 /* Copy out the strbuf. */ 21533 mi_copyout(q, mp); 21534 return; 21535 case MI_COPY_CASE(MI_COPY_OUT, 2): 21536 /* All done. */ 21537 mi_copy_done(q, mp, 0); 21538 return; 21539 default: 21540 mi_copy_done(q, mp, EPROTO); 21541 return; 21542 } 21543 /* Check alignment of the strbuf */ 21544 if (!OK_32PTR(mp1->b_rptr)) { 21545 mi_copy_done(q, mp, EINVAL); 21546 return; 21547 } 21548 21549 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21550 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21551 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21552 mi_copy_done(q, mp, EINVAL); 21553 return; 21554 } 21555 21556 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21557 if (mp1 == NULL) 21558 return; 21559 21560 switch (iocp->ioc_cmd) { 21561 case TI_GETMYNAME: 21562 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21563 break; 21564 case TI_GETPEERNAME: 21565 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21566 break; 21567 } 21568 21569 if (error != 0) { 21570 mi_copy_done(q, mp, error); 21571 } else { 21572 mp1->b_wptr += addrlen; 21573 STRUCT_FSET(sb, len, addrlen); 21574 21575 /* Copy out the address */ 21576 mi_copyout(q, mp); 21577 } 21578 } 21579 21580 static void 21581 tcp_disable_direct_sockfs(tcp_t *tcp) 21582 { 21583 #ifdef _ILP32 21584 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21585 #else 21586 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21587 #endif 21588 /* 21589 * Insert this socket into the acceptor hash. 21590 * We might need it for T_CONN_RES message 21591 */ 21592 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21593 21594 if (tcp->tcp_fused) { 21595 /* 21596 * This is a fused loopback tcp; disable 21597 * read-side synchronous streams interface 21598 * and drain any queued data. It is okay 21599 * to do this for non-synchronous streams 21600 * fused tcp as well. 21601 */ 21602 tcp_fuse_disable_pair(tcp, B_FALSE); 21603 } 21604 tcp->tcp_issocket = B_FALSE; 21605 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21606 } 21607 21608 /* 21609 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21610 * messages. 21611 */ 21612 /* ARGSUSED */ 21613 static void 21614 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21615 { 21616 conn_t *connp = (conn_t *)arg; 21617 tcp_t *tcp = connp->conn_tcp; 21618 queue_t *q = tcp->tcp_wq; 21619 struct iocblk *iocp; 21620 21621 ASSERT(DB_TYPE(mp) == M_IOCTL); 21622 /* 21623 * Try and ASSERT the minimum possible references on the 21624 * conn early enough. Since we are executing on write side, 21625 * the connection is obviously not detached and that means 21626 * there is a ref each for TCP and IP. Since we are behind 21627 * the squeue, the minimum references needed are 3. If the 21628 * conn is in classifier hash list, there should be an 21629 * extra ref for that (we check both the possibilities). 21630 */ 21631 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21632 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21633 21634 iocp = (struct iocblk *)mp->b_rptr; 21635 switch (iocp->ioc_cmd) { 21636 case TCP_IOC_DEFAULT_Q: 21637 /* Wants to be the default wq. */ 21638 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21639 iocp->ioc_error = EPERM; 21640 iocp->ioc_count = 0; 21641 mp->b_datap->db_type = M_IOCACK; 21642 qreply(q, mp); 21643 return; 21644 } 21645 tcp_def_q_set(tcp, mp); 21646 return; 21647 case _SIOCSOCKFALLBACK: 21648 /* 21649 * Either sockmod is about to be popped and the socket 21650 * would now be treated as a plain stream, or a module 21651 * is about to be pushed so we could no longer use read- 21652 * side synchronous streams for fused loopback tcp. 21653 * Drain any queued data and disable direct sockfs 21654 * interface from now on. 21655 */ 21656 if (!tcp->tcp_issocket) { 21657 DB_TYPE(mp) = M_IOCNAK; 21658 iocp->ioc_error = EINVAL; 21659 } else { 21660 tcp_disable_direct_sockfs(tcp); 21661 DB_TYPE(mp) = M_IOCACK; 21662 iocp->ioc_error = 0; 21663 } 21664 iocp->ioc_count = 0; 21665 iocp->ioc_rval = 0; 21666 qreply(q, mp); 21667 return; 21668 } 21669 CALL_IP_WPUT(connp, q, mp); 21670 } 21671 21672 /* 21673 * This routine is called by tcp_wput() to handle all TPI requests. 21674 */ 21675 /* ARGSUSED */ 21676 static void 21677 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21678 { 21679 conn_t *connp = (conn_t *)arg; 21680 tcp_t *tcp = connp->conn_tcp; 21681 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21682 uchar_t *rptr; 21683 t_scalar_t type; 21684 cred_t *cr; 21685 21686 /* 21687 * Try and ASSERT the minimum possible references on the 21688 * conn early enough. Since we are executing on write side, 21689 * the connection is obviously not detached and that means 21690 * there is a ref each for TCP and IP. Since we are behind 21691 * the squeue, the minimum references needed are 3. If the 21692 * conn is in classifier hash list, there should be an 21693 * extra ref for that (we check both the possibilities). 21694 */ 21695 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21696 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21697 21698 rptr = mp->b_rptr; 21699 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21700 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21701 type = ((union T_primitives *)rptr)->type; 21702 if (type == T_EXDATA_REQ) { 21703 tcp_output_urgent(connp, mp->b_cont, arg2); 21704 freeb(mp); 21705 } else if (type != T_DATA_REQ) { 21706 goto non_urgent_data; 21707 } else { 21708 /* TODO: options, flags, ... from user */ 21709 /* Set length to zero for reclamation below */ 21710 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21711 freeb(mp); 21712 } 21713 return; 21714 } else { 21715 if (tcp->tcp_debug) { 21716 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21717 "tcp_wput_proto, dropping one..."); 21718 } 21719 freemsg(mp); 21720 return; 21721 } 21722 21723 non_urgent_data: 21724 21725 switch ((int)tprim->type) { 21726 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21727 /* 21728 * save the kssl_ent_t from the next block, and convert this 21729 * back to a normal bind_req. 21730 */ 21731 if (mp->b_cont != NULL) { 21732 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21733 21734 if (tcp->tcp_kssl_ent != NULL) { 21735 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21736 KSSL_NO_PROXY); 21737 tcp->tcp_kssl_ent = NULL; 21738 } 21739 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21740 sizeof (kssl_ent_t)); 21741 kssl_hold_ent(tcp->tcp_kssl_ent); 21742 freemsg(mp->b_cont); 21743 mp->b_cont = NULL; 21744 } 21745 tprim->type = T_BIND_REQ; 21746 21747 /* FALLTHROUGH */ 21748 case O_T_BIND_REQ: /* bind request */ 21749 case T_BIND_REQ: /* new semantics bind request */ 21750 tcp_tpi_bind(tcp, mp); 21751 break; 21752 case T_UNBIND_REQ: /* unbind request */ 21753 tcp_tpi_unbind(tcp, mp); 21754 break; 21755 case O_T_CONN_RES: /* old connection response XXX */ 21756 case T_CONN_RES: /* connection response */ 21757 tcp_tli_accept(tcp, mp); 21758 break; 21759 case T_CONN_REQ: /* connection request */ 21760 tcp_tpi_connect(tcp, mp); 21761 break; 21762 case T_DISCON_REQ: /* disconnect request */ 21763 tcp_disconnect(tcp, mp); 21764 break; 21765 case T_CAPABILITY_REQ: 21766 tcp_capability_req(tcp, mp); /* capability request */ 21767 break; 21768 case T_INFO_REQ: /* information request */ 21769 tcp_info_req(tcp, mp); 21770 break; 21771 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21772 case T_OPTMGMT_REQ: 21773 /* 21774 * Note: no support for snmpcom_req() through new 21775 * T_OPTMGMT_REQ. See comments in ip.c 21776 */ 21777 21778 /* 21779 * All Solaris components should pass a db_credp 21780 * for this TPI message, hence we ASSERT. 21781 * But in case there is some other M_PROTO that looks 21782 * like a TPI message sent by some other kernel 21783 * component, we check and return an error. 21784 */ 21785 cr = msg_getcred(mp, NULL); 21786 ASSERT(cr != NULL); 21787 if (cr == NULL) { 21788 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21789 return; 21790 } 21791 /* 21792 * If EINPROGRESS is returned, the request has been queued 21793 * for subsequent processing by ip_restart_optmgmt(), which 21794 * will do the CONN_DEC_REF(). 21795 */ 21796 CONN_INC_REF(connp); 21797 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21798 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21799 B_TRUE) != EINPROGRESS) { 21800 CONN_DEC_REF(connp); 21801 } 21802 } else { 21803 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21804 B_TRUE) != EINPROGRESS) { 21805 CONN_DEC_REF(connp); 21806 } 21807 } 21808 break; 21809 21810 case T_UNITDATA_REQ: /* unitdata request */ 21811 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21812 break; 21813 case T_ORDREL_REQ: /* orderly release req */ 21814 freemsg(mp); 21815 21816 if (tcp->tcp_fused) 21817 tcp_unfuse(tcp); 21818 21819 if (tcp_xmit_end(tcp) != 0) { 21820 /* 21821 * We were crossing FINs and got a reset from 21822 * the other side. Just ignore it. 21823 */ 21824 if (tcp->tcp_debug) { 21825 (void) strlog(TCP_MOD_ID, 0, 1, 21826 SL_ERROR|SL_TRACE, 21827 "tcp_wput_proto, T_ORDREL_REQ out of " 21828 "state %s", 21829 tcp_display(tcp, NULL, 21830 DISP_ADDR_AND_PORT)); 21831 } 21832 } 21833 break; 21834 case T_ADDR_REQ: 21835 tcp_addr_req(tcp, mp); 21836 break; 21837 default: 21838 if (tcp->tcp_debug) { 21839 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21840 "tcp_wput_proto, bogus TPI msg, type %d", 21841 tprim->type); 21842 } 21843 /* 21844 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21845 * to recover. 21846 */ 21847 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21848 break; 21849 } 21850 } 21851 21852 /* 21853 * The TCP write service routine should never be called... 21854 */ 21855 /* ARGSUSED */ 21856 static void 21857 tcp_wsrv(queue_t *q) 21858 { 21859 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21860 21861 TCP_STAT(tcps, tcp_wsrv_called); 21862 } 21863 21864 /* Non overlapping byte exchanger */ 21865 static void 21866 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21867 { 21868 uchar_t uch; 21869 21870 while (len-- > 0) { 21871 uch = a[len]; 21872 a[len] = b[len]; 21873 b[len] = uch; 21874 } 21875 } 21876 21877 /* 21878 * Send out a control packet on the tcp connection specified. This routine 21879 * is typically called where we need a simple ACK or RST generated. 21880 */ 21881 static void 21882 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21883 { 21884 uchar_t *rptr; 21885 tcph_t *tcph; 21886 ipha_t *ipha = NULL; 21887 ip6_t *ip6h = NULL; 21888 uint32_t sum; 21889 int tcp_hdr_len; 21890 int tcp_ip_hdr_len; 21891 mblk_t *mp; 21892 tcp_stack_t *tcps = tcp->tcp_tcps; 21893 21894 /* 21895 * Save sum for use in source route later. 21896 */ 21897 ASSERT(tcp != NULL); 21898 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21899 tcp_hdr_len = tcp->tcp_hdr_len; 21900 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21901 21902 /* If a text string is passed in with the request, pass it to strlog. */ 21903 if (str != NULL && tcp->tcp_debug) { 21904 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21905 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21906 str, seq, ack, ctl); 21907 } 21908 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21909 BPRI_MED); 21910 if (mp == NULL) { 21911 return; 21912 } 21913 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21914 mp->b_rptr = rptr; 21915 mp->b_wptr = &rptr[tcp_hdr_len]; 21916 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21917 21918 if (tcp->tcp_ipversion == IPV4_VERSION) { 21919 ipha = (ipha_t *)rptr; 21920 ipha->ipha_length = htons(tcp_hdr_len); 21921 } else { 21922 ip6h = (ip6_t *)rptr; 21923 ASSERT(tcp != NULL); 21924 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21925 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21926 } 21927 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21928 tcph->th_flags[0] = (uint8_t)ctl; 21929 if (ctl & TH_RST) { 21930 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21931 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21932 /* 21933 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21934 */ 21935 if (tcp->tcp_snd_ts_ok && 21936 tcp->tcp_state > TCPS_SYN_SENT) { 21937 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21938 *(mp->b_wptr) = TCPOPT_EOL; 21939 if (tcp->tcp_ipversion == IPV4_VERSION) { 21940 ipha->ipha_length = htons(tcp_hdr_len - 21941 TCPOPT_REAL_TS_LEN); 21942 } else { 21943 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21944 TCPOPT_REAL_TS_LEN); 21945 } 21946 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21947 sum -= TCPOPT_REAL_TS_LEN; 21948 } 21949 } 21950 if (ctl & TH_ACK) { 21951 if (tcp->tcp_snd_ts_ok) { 21952 U32_TO_BE32(lbolt, 21953 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21954 U32_TO_BE32(tcp->tcp_ts_recent, 21955 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21956 } 21957 21958 /* Update the latest receive window size in TCP header. */ 21959 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21960 tcph->th_win); 21961 tcp->tcp_rack = ack; 21962 tcp->tcp_rack_cnt = 0; 21963 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21964 } 21965 BUMP_LOCAL(tcp->tcp_obsegs); 21966 U32_TO_BE32(seq, tcph->th_seq); 21967 U32_TO_BE32(ack, tcph->th_ack); 21968 /* 21969 * Include the adjustment for a source route if any. 21970 */ 21971 sum = (sum >> 16) + (sum & 0xFFFF); 21972 U16_TO_BE16(sum, tcph->th_sum); 21973 tcp_send_data(tcp, tcp->tcp_wq, mp); 21974 } 21975 21976 /* 21977 * If this routine returns B_TRUE, TCP can generate a RST in response 21978 * to a segment. If it returns B_FALSE, TCP should not respond. 21979 */ 21980 static boolean_t 21981 tcp_send_rst_chk(tcp_stack_t *tcps) 21982 { 21983 clock_t now; 21984 21985 /* 21986 * TCP needs to protect itself from generating too many RSTs. 21987 * This can be a DoS attack by sending us random segments 21988 * soliciting RSTs. 21989 * 21990 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21991 * in each 1 second interval. In this way, TCP still generate 21992 * RSTs in normal cases but when under attack, the impact is 21993 * limited. 21994 */ 21995 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21996 now = lbolt; 21997 /* lbolt can wrap around. */ 21998 if ((tcps->tcps_last_rst_intrvl > now) || 21999 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 22000 1*SECONDS)) { 22001 tcps->tcps_last_rst_intrvl = now; 22002 tcps->tcps_rst_cnt = 1; 22003 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 22004 return (B_FALSE); 22005 } 22006 } 22007 return (B_TRUE); 22008 } 22009 22010 /* 22011 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 22012 */ 22013 static void 22014 tcp_ip_ire_mark_advice(tcp_t *tcp) 22015 { 22016 mblk_t *mp; 22017 ipic_t *ipic; 22018 22019 if (tcp->tcp_ipversion == IPV4_VERSION) { 22020 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22021 &ipic); 22022 } else { 22023 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22024 &ipic); 22025 } 22026 if (mp == NULL) 22027 return; 22028 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22029 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22030 } 22031 22032 /* 22033 * Return an IP advice ioctl mblk and set ipic to be the pointer 22034 * to the advice structure. 22035 */ 22036 static mblk_t * 22037 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 22038 { 22039 struct iocblk *ioc; 22040 mblk_t *mp, *mp1; 22041 22042 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 22043 if (mp == NULL) 22044 return (NULL); 22045 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 22046 *ipic = (ipic_t *)mp->b_rptr; 22047 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 22048 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 22049 22050 bcopy(addr, *ipic + 1, addr_len); 22051 22052 (*ipic)->ipic_addr_length = addr_len; 22053 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 22054 22055 mp1 = mkiocb(IP_IOCTL); 22056 if (mp1 == NULL) { 22057 freemsg(mp); 22058 return (NULL); 22059 } 22060 mp1->b_cont = mp; 22061 ioc = (struct iocblk *)mp1->b_rptr; 22062 ioc->ioc_count = sizeof (ipic_t) + addr_len; 22063 22064 return (mp1); 22065 } 22066 22067 /* 22068 * Generate a reset based on an inbound packet, connp is set by caller 22069 * when RST is in response to an unexpected inbound packet for which 22070 * there is active tcp state in the system. 22071 * 22072 * IPSEC NOTE : Try to send the reply with the same protection as it came 22073 * in. We still have the ipsec_mp that the packet was attached to. Thus 22074 * the packet will go out at the same level of protection as it came in by 22075 * converting the IPSEC_IN to IPSEC_OUT. 22076 */ 22077 static void 22078 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 22079 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 22080 tcp_stack_t *tcps, conn_t *connp) 22081 { 22082 ipha_t *ipha = NULL; 22083 ip6_t *ip6h = NULL; 22084 ushort_t len; 22085 tcph_t *tcph; 22086 int i; 22087 mblk_t *ipsec_mp; 22088 boolean_t mctl_present; 22089 ipic_t *ipic; 22090 ipaddr_t v4addr; 22091 in6_addr_t v6addr; 22092 int addr_len; 22093 void *addr; 22094 queue_t *q = tcps->tcps_g_q; 22095 tcp_t *tcp; 22096 cred_t *cr; 22097 pid_t pid; 22098 mblk_t *nmp; 22099 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 22100 22101 if (tcps->tcps_g_q == NULL) { 22102 /* 22103 * For non-zero stackids the default queue isn't created 22104 * until the first open, thus there can be a need to send 22105 * a reset before then. But we can't do that, hence we just 22106 * drop the packet. Later during boot, when the default queue 22107 * has been setup, a retransmitted packet from the peer 22108 * will result in a reset. 22109 */ 22110 ASSERT(tcps->tcps_netstack->netstack_stackid != 22111 GLOBAL_NETSTACKID); 22112 freemsg(mp); 22113 return; 22114 } 22115 22116 if (connp != NULL) 22117 tcp = connp->conn_tcp; 22118 else 22119 tcp = Q_TO_TCP(q); 22120 22121 if (!tcp_send_rst_chk(tcps)) { 22122 tcps->tcps_rst_unsent++; 22123 freemsg(mp); 22124 return; 22125 } 22126 22127 if (mp->b_datap->db_type == M_CTL) { 22128 ipsec_mp = mp; 22129 mp = mp->b_cont; 22130 mctl_present = B_TRUE; 22131 } else { 22132 ipsec_mp = mp; 22133 mctl_present = B_FALSE; 22134 } 22135 22136 if (str && q && tcps->tcps_dbg) { 22137 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 22138 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 22139 "flags 0x%x", 22140 str, seq, ack, ctl); 22141 } 22142 if (mp->b_datap->db_ref != 1) { 22143 mblk_t *mp1 = copyb(mp); 22144 freemsg(mp); 22145 mp = mp1; 22146 if (!mp) { 22147 if (mctl_present) 22148 freeb(ipsec_mp); 22149 return; 22150 } else { 22151 if (mctl_present) { 22152 ipsec_mp->b_cont = mp; 22153 } else { 22154 ipsec_mp = mp; 22155 } 22156 } 22157 } else if (mp->b_cont) { 22158 freemsg(mp->b_cont); 22159 mp->b_cont = NULL; 22160 } 22161 /* 22162 * We skip reversing source route here. 22163 * (for now we replace all IP options with EOL) 22164 */ 22165 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22166 ipha = (ipha_t *)mp->b_rptr; 22167 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 22168 mp->b_rptr[i] = IPOPT_EOL; 22169 /* 22170 * Make sure that src address isn't flagrantly invalid. 22171 * Not all broadcast address checking for the src address 22172 * is possible, since we don't know the netmask of the src 22173 * addr. No check for destination address is done, since 22174 * IP will not pass up a packet with a broadcast dest 22175 * address to TCP. Similar checks are done below for IPv6. 22176 */ 22177 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22178 CLASSD(ipha->ipha_src)) { 22179 freemsg(ipsec_mp); 22180 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22181 return; 22182 } 22183 } else { 22184 ip6h = (ip6_t *)mp->b_rptr; 22185 22186 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22187 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22188 freemsg(ipsec_mp); 22189 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22190 return; 22191 } 22192 22193 /* Remove any extension headers assuming partial overlay */ 22194 if (ip_hdr_len > IPV6_HDR_LEN) { 22195 uint8_t *to; 22196 22197 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22198 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22199 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22200 ip_hdr_len = IPV6_HDR_LEN; 22201 ip6h = (ip6_t *)mp->b_rptr; 22202 ip6h->ip6_nxt = IPPROTO_TCP; 22203 } 22204 } 22205 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22206 if (tcph->th_flags[0] & TH_RST) { 22207 freemsg(ipsec_mp); 22208 return; 22209 } 22210 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22211 len = ip_hdr_len + sizeof (tcph_t); 22212 mp->b_wptr = &mp->b_rptr[len]; 22213 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22214 ipha->ipha_length = htons(len); 22215 /* Swap addresses */ 22216 v4addr = ipha->ipha_src; 22217 ipha->ipha_src = ipha->ipha_dst; 22218 ipha->ipha_dst = v4addr; 22219 ipha->ipha_ident = 0; 22220 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22221 addr_len = IP_ADDR_LEN; 22222 addr = &v4addr; 22223 } else { 22224 /* No ip6i_t in this case */ 22225 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22226 /* Swap addresses */ 22227 v6addr = ip6h->ip6_src; 22228 ip6h->ip6_src = ip6h->ip6_dst; 22229 ip6h->ip6_dst = v6addr; 22230 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22231 addr_len = IPV6_ADDR_LEN; 22232 addr = &v6addr; 22233 } 22234 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22235 U32_TO_BE32(ack, tcph->th_ack); 22236 U32_TO_BE32(seq, tcph->th_seq); 22237 U16_TO_BE16(0, tcph->th_win); 22238 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22239 tcph->th_flags[0] = (uint8_t)ctl; 22240 if (ctl & TH_RST) { 22241 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22242 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22243 } 22244 22245 /* IP trusts us to set up labels when required. */ 22246 if (is_system_labeled() && (cr = msg_getcred(mp, &pid)) != NULL && 22247 crgetlabel(cr) != NULL) { 22248 int err; 22249 22250 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22251 err = tsol_check_label(cr, &mp, 22252 tcp->tcp_connp->conn_mac_exempt, 22253 tcps->tcps_netstack->netstack_ip, pid); 22254 else 22255 err = tsol_check_label_v6(cr, &mp, 22256 tcp->tcp_connp->conn_mac_exempt, 22257 tcps->tcps_netstack->netstack_ip, pid); 22258 if (mctl_present) 22259 ipsec_mp->b_cont = mp; 22260 else 22261 ipsec_mp = mp; 22262 if (err != 0) { 22263 freemsg(ipsec_mp); 22264 return; 22265 } 22266 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22267 ipha = (ipha_t *)mp->b_rptr; 22268 } else { 22269 ip6h = (ip6_t *)mp->b_rptr; 22270 } 22271 } 22272 22273 if (mctl_present) { 22274 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22275 22276 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22277 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22278 return; 22279 } 22280 } 22281 if (zoneid == ALL_ZONES) 22282 zoneid = GLOBAL_ZONEID; 22283 22284 /* Add the zoneid so ip_output routes it properly */ 22285 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22286 freemsg(ipsec_mp); 22287 return; 22288 } 22289 ipsec_mp = nmp; 22290 22291 /* 22292 * NOTE: one might consider tracing a TCP packet here, but 22293 * this function has no active TCP state and no tcp structure 22294 * that has a trace buffer. If we traced here, we would have 22295 * to keep a local trace buffer in tcp_record_trace(). 22296 * 22297 * TSol note: The mblk that contains the incoming packet was 22298 * reused by tcp_xmit_listener_reset, so it already contains 22299 * the right credentials and we don't need to call mblk_setcred. 22300 * Also the conn's cred is not right since it is associated 22301 * with tcps_g_q. 22302 */ 22303 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22304 22305 /* 22306 * Tell IP to mark the IRE used for this destination temporary. 22307 * This way, we can limit our exposure to DoS attack because IP 22308 * creates an IRE for each destination. If there are too many, 22309 * the time to do any routing lookup will be extremely long. And 22310 * the lookup can be in interrupt context. 22311 * 22312 * Note that in normal circumstances, this marking should not 22313 * affect anything. It would be nice if only 1 message is 22314 * needed to inform IP that the IRE created for this RST should 22315 * not be added to the cache table. But there is currently 22316 * not such communication mechanism between TCP and IP. So 22317 * the best we can do now is to send the advice ioctl to IP 22318 * to mark the IRE temporary. 22319 */ 22320 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22321 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22322 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22323 } 22324 } 22325 22326 /* 22327 * Initiate closedown sequence on an active connection. (May be called as 22328 * writer.) Return value zero for OK return, non-zero for error return. 22329 */ 22330 static int 22331 tcp_xmit_end(tcp_t *tcp) 22332 { 22333 ipic_t *ipic; 22334 mblk_t *mp; 22335 tcp_stack_t *tcps = tcp->tcp_tcps; 22336 22337 if (tcp->tcp_state < TCPS_SYN_RCVD || 22338 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22339 /* 22340 * Invalid state, only states TCPS_SYN_RCVD, 22341 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22342 */ 22343 return (-1); 22344 } 22345 22346 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22347 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22348 /* 22349 * If there is nothing more unsent, send the FIN now. 22350 * Otherwise, it will go out with the last segment. 22351 */ 22352 if (tcp->tcp_unsent == 0) { 22353 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22354 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22355 22356 if (mp) { 22357 tcp_send_data(tcp, tcp->tcp_wq, mp); 22358 } else { 22359 /* 22360 * Couldn't allocate msg. Pretend we got it out. 22361 * Wait for rexmit timeout. 22362 */ 22363 tcp->tcp_snxt = tcp->tcp_fss + 1; 22364 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22365 } 22366 22367 /* 22368 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22369 * changed. 22370 */ 22371 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22372 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22373 } 22374 } else { 22375 /* 22376 * If tcp->tcp_cork is set, then the data will not get sent, 22377 * so we have to check that and unset it first. 22378 */ 22379 if (tcp->tcp_cork) 22380 tcp->tcp_cork = B_FALSE; 22381 tcp_wput_data(tcp, NULL, B_FALSE); 22382 } 22383 22384 /* 22385 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22386 * is 0, don't update the cache. 22387 */ 22388 if (tcps->tcps_rtt_updates == 0 || 22389 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22390 return (0); 22391 22392 /* 22393 * NOTE: should not update if source routes i.e. if tcp_remote if 22394 * different from the destination. 22395 */ 22396 if (tcp->tcp_ipversion == IPV4_VERSION) { 22397 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22398 return (0); 22399 } 22400 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22401 &ipic); 22402 } else { 22403 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22404 &tcp->tcp_ip6h->ip6_dst))) { 22405 return (0); 22406 } 22407 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22408 &ipic); 22409 } 22410 22411 /* Record route attributes in the IRE for use by future connections. */ 22412 if (mp == NULL) 22413 return (0); 22414 22415 /* 22416 * We do not have a good algorithm to update ssthresh at this time. 22417 * So don't do any update. 22418 */ 22419 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22420 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22421 22422 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22423 22424 return (0); 22425 } 22426 22427 /* ARGSUSED */ 22428 void 22429 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22430 { 22431 conn_t *connp = (conn_t *)arg; 22432 mblk_t *mp1; 22433 tcp_t *tcp = connp->conn_tcp; 22434 tcp_xmit_reset_event_t *eventp; 22435 22436 ASSERT(mp->b_datap->db_type == M_PROTO && 22437 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22438 22439 if (tcp->tcp_state != TCPS_LISTEN) { 22440 freemsg(mp); 22441 return; 22442 } 22443 22444 mp1 = mp->b_cont; 22445 mp->b_cont = NULL; 22446 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22447 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22448 connp->conn_netstack); 22449 22450 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22451 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22452 freemsg(mp); 22453 } 22454 22455 /* 22456 * Generate a "no listener here" RST in response to an "unknown" segment. 22457 * connp is set by caller when RST is in response to an unexpected 22458 * inbound packet for which there is active tcp state in the system. 22459 * Note that we are reusing the incoming mp to construct the outgoing RST. 22460 */ 22461 void 22462 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22463 tcp_stack_t *tcps, conn_t *connp) 22464 { 22465 uchar_t *rptr; 22466 uint32_t seg_len; 22467 tcph_t *tcph; 22468 uint32_t seg_seq; 22469 uint32_t seg_ack; 22470 uint_t flags; 22471 mblk_t *ipsec_mp; 22472 ipha_t *ipha; 22473 ip6_t *ip6h; 22474 boolean_t mctl_present = B_FALSE; 22475 boolean_t check = B_TRUE; 22476 boolean_t policy_present; 22477 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22478 22479 TCP_STAT(tcps, tcp_no_listener); 22480 22481 ipsec_mp = mp; 22482 22483 if (mp->b_datap->db_type == M_CTL) { 22484 ipsec_in_t *ii; 22485 22486 mctl_present = B_TRUE; 22487 mp = mp->b_cont; 22488 22489 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22490 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22491 if (ii->ipsec_in_dont_check) { 22492 check = B_FALSE; 22493 if (!ii->ipsec_in_secure) { 22494 freeb(ipsec_mp); 22495 mctl_present = B_FALSE; 22496 ipsec_mp = mp; 22497 } 22498 } 22499 } 22500 22501 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22502 policy_present = ipss->ipsec_inbound_v4_policy_present; 22503 ipha = (ipha_t *)mp->b_rptr; 22504 ip6h = NULL; 22505 } else { 22506 policy_present = ipss->ipsec_inbound_v6_policy_present; 22507 ipha = NULL; 22508 ip6h = (ip6_t *)mp->b_rptr; 22509 } 22510 22511 if (check && policy_present) { 22512 /* 22513 * The conn_t parameter is NULL because we already know 22514 * nobody's home. 22515 */ 22516 ipsec_mp = ipsec_check_global_policy( 22517 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22518 tcps->tcps_netstack); 22519 if (ipsec_mp == NULL) 22520 return; 22521 } 22522 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22523 DTRACE_PROBE2( 22524 tx__ip__log__error__nolistener__tcp, 22525 char *, "Could not reply with RST to mp(1)", 22526 mblk_t *, mp); 22527 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22528 freemsg(ipsec_mp); 22529 return; 22530 } 22531 22532 rptr = mp->b_rptr; 22533 22534 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22535 seg_seq = BE32_TO_U32(tcph->th_seq); 22536 seg_ack = BE32_TO_U32(tcph->th_ack); 22537 flags = tcph->th_flags[0]; 22538 22539 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22540 if (flags & TH_RST) { 22541 freemsg(ipsec_mp); 22542 } else if (flags & TH_ACK) { 22543 tcp_xmit_early_reset("no tcp, reset", 22544 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22545 connp); 22546 } else { 22547 if (flags & TH_SYN) { 22548 seg_len++; 22549 } else { 22550 /* 22551 * Here we violate the RFC. Note that a normal 22552 * TCP will never send a segment without the ACK 22553 * flag, except for RST or SYN segment. This 22554 * segment is neither. Just drop it on the 22555 * floor. 22556 */ 22557 freemsg(ipsec_mp); 22558 tcps->tcps_rst_unsent++; 22559 return; 22560 } 22561 22562 tcp_xmit_early_reset("no tcp, reset/ack", 22563 ipsec_mp, 0, seg_seq + seg_len, 22564 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22565 } 22566 } 22567 22568 /* 22569 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22570 * ip and tcp header ready to pass down to IP. If the mp passed in is 22571 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22572 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22573 * otherwise it will dup partial mblks.) 22574 * Otherwise, an appropriate ACK packet will be generated. This 22575 * routine is not usually called to send new data for the first time. It 22576 * is mostly called out of the timer for retransmits, and to generate ACKs. 22577 * 22578 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22579 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22580 * of the original mblk chain will be returned in *offset and *end_mp. 22581 */ 22582 mblk_t * 22583 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22584 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22585 boolean_t rexmit) 22586 { 22587 int data_length; 22588 int32_t off = 0; 22589 uint_t flags; 22590 mblk_t *mp1; 22591 mblk_t *mp2; 22592 uchar_t *rptr; 22593 tcph_t *tcph; 22594 int32_t num_sack_blk = 0; 22595 int32_t sack_opt_len = 0; 22596 tcp_stack_t *tcps = tcp->tcp_tcps; 22597 22598 /* Allocate for our maximum TCP header + link-level */ 22599 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22600 tcps->tcps_wroff_xtra, BPRI_MED); 22601 if (!mp1) 22602 return (NULL); 22603 data_length = 0; 22604 22605 /* 22606 * Note that tcp_mss has been adjusted to take into account the 22607 * timestamp option if applicable. Because SACK options do not 22608 * appear in every TCP segments and they are of variable lengths, 22609 * they cannot be included in tcp_mss. Thus we need to calculate 22610 * the actual segment length when we need to send a segment which 22611 * includes SACK options. 22612 */ 22613 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22614 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22615 tcp->tcp_num_sack_blk); 22616 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22617 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22618 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22619 max_to_send -= sack_opt_len; 22620 } 22621 22622 if (offset != NULL) { 22623 off = *offset; 22624 /* We use offset as an indicator that end_mp is not NULL. */ 22625 *end_mp = NULL; 22626 } 22627 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22628 /* This could be faster with cooperation from downstream */ 22629 if (mp2 != mp1 && !sendall && 22630 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22631 max_to_send) 22632 /* 22633 * Don't send the next mblk since the whole mblk 22634 * does not fit. 22635 */ 22636 break; 22637 mp2->b_cont = dupb(mp); 22638 mp2 = mp2->b_cont; 22639 if (!mp2) { 22640 freemsg(mp1); 22641 return (NULL); 22642 } 22643 mp2->b_rptr += off; 22644 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22645 (uintptr_t)INT_MAX); 22646 22647 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22648 if (data_length > max_to_send) { 22649 mp2->b_wptr -= data_length - max_to_send; 22650 data_length = max_to_send; 22651 off = mp2->b_wptr - mp->b_rptr; 22652 break; 22653 } else { 22654 off = 0; 22655 } 22656 } 22657 if (offset != NULL) { 22658 *offset = off; 22659 *end_mp = mp; 22660 } 22661 if (seg_len != NULL) { 22662 *seg_len = data_length; 22663 } 22664 22665 /* Update the latest receive window size in TCP header. */ 22666 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22667 tcp->tcp_tcph->th_win); 22668 22669 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22670 mp1->b_rptr = rptr; 22671 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22672 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22673 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22674 U32_TO_ABE32(seq, tcph->th_seq); 22675 22676 /* 22677 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22678 * that this function was called from tcp_wput_data. Thus, when called 22679 * to retransmit data the setting of the PUSH bit may appear some 22680 * what random in that it might get set when it should not. This 22681 * should not pose any performance issues. 22682 */ 22683 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22684 tcp->tcp_unsent == data_length)) { 22685 flags = TH_ACK | TH_PUSH; 22686 } else { 22687 flags = TH_ACK; 22688 } 22689 22690 if (tcp->tcp_ecn_ok) { 22691 if (tcp->tcp_ecn_echo_on) 22692 flags |= TH_ECE; 22693 22694 /* 22695 * Only set ECT bit and ECN_CWR if a segment contains new data. 22696 * There is no TCP flow control for non-data segments, and 22697 * only data segment is transmitted reliably. 22698 */ 22699 if (data_length > 0 && !rexmit) { 22700 SET_ECT(tcp, rptr); 22701 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22702 flags |= TH_CWR; 22703 tcp->tcp_ecn_cwr_sent = B_TRUE; 22704 } 22705 } 22706 } 22707 22708 if (tcp->tcp_valid_bits) { 22709 uint32_t u1; 22710 22711 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22712 seq == tcp->tcp_iss) { 22713 uchar_t *wptr; 22714 22715 /* 22716 * If TCP_ISS_VALID and the seq number is tcp_iss, 22717 * TCP can only be in SYN-SENT, SYN-RCVD or 22718 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22719 * our SYN is not ack'ed but the app closes this 22720 * TCP connection. 22721 */ 22722 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22723 tcp->tcp_state == TCPS_SYN_RCVD || 22724 tcp->tcp_state == TCPS_FIN_WAIT_1); 22725 22726 /* 22727 * Tack on the MSS option. It is always needed 22728 * for both active and passive open. 22729 * 22730 * MSS option value should be interface MTU - MIN 22731 * TCP/IP header according to RFC 793 as it means 22732 * the maximum segment size TCP can receive. But 22733 * to get around some broken middle boxes/end hosts 22734 * out there, we allow the option value to be the 22735 * same as the MSS option size on the peer side. 22736 * In this way, the other side will not send 22737 * anything larger than they can receive. 22738 * 22739 * Note that for SYN_SENT state, the ndd param 22740 * tcp_use_smss_as_mss_opt has no effect as we 22741 * don't know the peer's MSS option value. So 22742 * the only case we need to take care of is in 22743 * SYN_RCVD state, which is done later. 22744 */ 22745 wptr = mp1->b_wptr; 22746 wptr[0] = TCPOPT_MAXSEG; 22747 wptr[1] = TCPOPT_MAXSEG_LEN; 22748 wptr += 2; 22749 u1 = tcp->tcp_if_mtu - 22750 (tcp->tcp_ipversion == IPV4_VERSION ? 22751 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22752 TCP_MIN_HEADER_LENGTH; 22753 U16_TO_BE16(u1, wptr); 22754 mp1->b_wptr = wptr + 2; 22755 /* Update the offset to cover the additional word */ 22756 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22757 22758 /* 22759 * Note that the following way of filling in 22760 * TCP options are not optimal. Some NOPs can 22761 * be saved. But there is no need at this time 22762 * to optimize it. When it is needed, we will 22763 * do it. 22764 */ 22765 switch (tcp->tcp_state) { 22766 case TCPS_SYN_SENT: 22767 flags = TH_SYN; 22768 22769 if (tcp->tcp_snd_ts_ok) { 22770 uint32_t llbolt = (uint32_t)lbolt; 22771 22772 wptr = mp1->b_wptr; 22773 wptr[0] = TCPOPT_NOP; 22774 wptr[1] = TCPOPT_NOP; 22775 wptr[2] = TCPOPT_TSTAMP; 22776 wptr[3] = TCPOPT_TSTAMP_LEN; 22777 wptr += 4; 22778 U32_TO_BE32(llbolt, wptr); 22779 wptr += 4; 22780 ASSERT(tcp->tcp_ts_recent == 0); 22781 U32_TO_BE32(0L, wptr); 22782 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22783 tcph->th_offset_and_rsrvd[0] += 22784 (3 << 4); 22785 } 22786 22787 /* 22788 * Set up all the bits to tell other side 22789 * we are ECN capable. 22790 */ 22791 if (tcp->tcp_ecn_ok) { 22792 flags |= (TH_ECE | TH_CWR); 22793 } 22794 break; 22795 case TCPS_SYN_RCVD: 22796 flags |= TH_SYN; 22797 22798 /* 22799 * Reset the MSS option value to be SMSS 22800 * We should probably add back the bytes 22801 * for timestamp option and IPsec. We 22802 * don't do that as this is a workaround 22803 * for broken middle boxes/end hosts, it 22804 * is better for us to be more cautious. 22805 * They may not take these things into 22806 * account in their SMSS calculation. Thus 22807 * the peer's calculated SMSS may be smaller 22808 * than what it can be. This should be OK. 22809 */ 22810 if (tcps->tcps_use_smss_as_mss_opt) { 22811 u1 = tcp->tcp_mss; 22812 U16_TO_BE16(u1, wptr); 22813 } 22814 22815 /* 22816 * If the other side is ECN capable, reply 22817 * that we are also ECN capable. 22818 */ 22819 if (tcp->tcp_ecn_ok) 22820 flags |= TH_ECE; 22821 break; 22822 default: 22823 /* 22824 * The above ASSERT() makes sure that this 22825 * must be FIN-WAIT-1 state. Our SYN has 22826 * not been ack'ed so retransmit it. 22827 */ 22828 flags |= TH_SYN; 22829 break; 22830 } 22831 22832 if (tcp->tcp_snd_ws_ok) { 22833 wptr = mp1->b_wptr; 22834 wptr[0] = TCPOPT_NOP; 22835 wptr[1] = TCPOPT_WSCALE; 22836 wptr[2] = TCPOPT_WS_LEN; 22837 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22838 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22839 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22840 } 22841 22842 if (tcp->tcp_snd_sack_ok) { 22843 wptr = mp1->b_wptr; 22844 wptr[0] = TCPOPT_NOP; 22845 wptr[1] = TCPOPT_NOP; 22846 wptr[2] = TCPOPT_SACK_PERMITTED; 22847 wptr[3] = TCPOPT_SACK_OK_LEN; 22848 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22849 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22850 } 22851 22852 /* allocb() of adequate mblk assures space */ 22853 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22854 (uintptr_t)INT_MAX); 22855 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22856 /* 22857 * Get IP set to checksum on our behalf 22858 * Include the adjustment for a source route if any. 22859 */ 22860 u1 += tcp->tcp_sum; 22861 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22862 U16_TO_BE16(u1, tcph->th_sum); 22863 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22864 } 22865 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22866 (seq + data_length) == tcp->tcp_fss) { 22867 if (!tcp->tcp_fin_acked) { 22868 flags |= TH_FIN; 22869 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22870 } 22871 if (!tcp->tcp_fin_sent) { 22872 tcp->tcp_fin_sent = B_TRUE; 22873 switch (tcp->tcp_state) { 22874 case TCPS_SYN_RCVD: 22875 case TCPS_ESTABLISHED: 22876 tcp->tcp_state = TCPS_FIN_WAIT_1; 22877 break; 22878 case TCPS_CLOSE_WAIT: 22879 tcp->tcp_state = TCPS_LAST_ACK; 22880 break; 22881 } 22882 if (tcp->tcp_suna == tcp->tcp_snxt) 22883 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22884 tcp->tcp_snxt = tcp->tcp_fss + 1; 22885 } 22886 } 22887 /* 22888 * Note the trick here. u1 is unsigned. When tcp_urg 22889 * is smaller than seq, u1 will become a very huge value. 22890 * So the comparison will fail. Also note that tcp_urp 22891 * should be positive, see RFC 793 page 17. 22892 */ 22893 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22894 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22895 u1 < (uint32_t)(64 * 1024)) { 22896 flags |= TH_URG; 22897 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22898 U32_TO_ABE16(u1, tcph->th_urp); 22899 } 22900 } 22901 tcph->th_flags[0] = (uchar_t)flags; 22902 tcp->tcp_rack = tcp->tcp_rnxt; 22903 tcp->tcp_rack_cnt = 0; 22904 22905 if (tcp->tcp_snd_ts_ok) { 22906 if (tcp->tcp_state != TCPS_SYN_SENT) { 22907 uint32_t llbolt = (uint32_t)lbolt; 22908 22909 U32_TO_BE32(llbolt, 22910 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22911 U32_TO_BE32(tcp->tcp_ts_recent, 22912 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22913 } 22914 } 22915 22916 if (num_sack_blk > 0) { 22917 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22918 sack_blk_t *tmp; 22919 int32_t i; 22920 22921 wptr[0] = TCPOPT_NOP; 22922 wptr[1] = TCPOPT_NOP; 22923 wptr[2] = TCPOPT_SACK; 22924 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22925 sizeof (sack_blk_t); 22926 wptr += TCPOPT_REAL_SACK_LEN; 22927 22928 tmp = tcp->tcp_sack_list; 22929 for (i = 0; i < num_sack_blk; i++) { 22930 U32_TO_BE32(tmp[i].begin, wptr); 22931 wptr += sizeof (tcp_seq); 22932 U32_TO_BE32(tmp[i].end, wptr); 22933 wptr += sizeof (tcp_seq); 22934 } 22935 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22936 } 22937 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22938 data_length += (int)(mp1->b_wptr - rptr); 22939 if (tcp->tcp_ipversion == IPV4_VERSION) { 22940 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22941 } else { 22942 ip6_t *ip6 = (ip6_t *)(rptr + 22943 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22944 sizeof (ip6i_t) : 0)); 22945 22946 ip6->ip6_plen = htons(data_length - 22947 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22948 } 22949 22950 /* 22951 * Prime pump for IP 22952 * Include the adjustment for a source route if any. 22953 */ 22954 data_length -= tcp->tcp_ip_hdr_len; 22955 data_length += tcp->tcp_sum; 22956 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22957 U16_TO_ABE16(data_length, tcph->th_sum); 22958 if (tcp->tcp_ip_forward_progress) { 22959 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22960 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22961 tcp->tcp_ip_forward_progress = B_FALSE; 22962 } 22963 return (mp1); 22964 } 22965 22966 /* This function handles the push timeout. */ 22967 void 22968 tcp_push_timer(void *arg) 22969 { 22970 conn_t *connp = (conn_t *)arg; 22971 tcp_t *tcp = connp->conn_tcp; 22972 22973 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22974 22975 ASSERT(tcp->tcp_listener == NULL); 22976 22977 ASSERT(!IPCL_IS_NONSTR(connp)); 22978 22979 /* 22980 * We need to plug synchronous streams during our drain to prevent 22981 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22982 */ 22983 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22984 tcp->tcp_push_tid = 0; 22985 22986 if (tcp->tcp_rcv_list != NULL && 22987 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22988 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22989 22990 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22991 } 22992 22993 /* 22994 * This function handles delayed ACK timeout. 22995 */ 22996 static void 22997 tcp_ack_timer(void *arg) 22998 { 22999 conn_t *connp = (conn_t *)arg; 23000 tcp_t *tcp = connp->conn_tcp; 23001 mblk_t *mp; 23002 tcp_stack_t *tcps = tcp->tcp_tcps; 23003 23004 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 23005 23006 tcp->tcp_ack_tid = 0; 23007 23008 if (tcp->tcp_fused) 23009 return; 23010 23011 /* 23012 * Do not send ACK if there is no outstanding unack'ed data. 23013 */ 23014 if (tcp->tcp_rnxt == tcp->tcp_rack) { 23015 return; 23016 } 23017 23018 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 23019 /* 23020 * Make sure we don't allow deferred ACKs to result in 23021 * timer-based ACKing. If we have held off an ACK 23022 * when there was more than an mss here, and the timer 23023 * goes off, we have to worry about the possibility 23024 * that the sender isn't doing slow-start, or is out 23025 * of step with us for some other reason. We fall 23026 * permanently back in the direction of 23027 * ACK-every-other-packet as suggested in RFC 1122. 23028 */ 23029 if (tcp->tcp_rack_abs_max > 2) 23030 tcp->tcp_rack_abs_max--; 23031 tcp->tcp_rack_cur_max = 2; 23032 } 23033 mp = tcp_ack_mp(tcp); 23034 23035 if (mp != NULL) { 23036 BUMP_LOCAL(tcp->tcp_obsegs); 23037 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 23038 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 23039 tcp_send_data(tcp, tcp->tcp_wq, mp); 23040 } 23041 } 23042 23043 23044 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 23045 static mblk_t * 23046 tcp_ack_mp(tcp_t *tcp) 23047 { 23048 uint32_t seq_no; 23049 tcp_stack_t *tcps = tcp->tcp_tcps; 23050 23051 /* 23052 * There are a few cases to be considered while setting the sequence no. 23053 * Essentially, we can come here while processing an unacceptable pkt 23054 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 23055 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 23056 * If we are here for a zero window probe, stick with suna. In all 23057 * other cases, we check if suna + swnd encompasses snxt and set 23058 * the sequence number to snxt, if so. If snxt falls outside the 23059 * window (the receiver probably shrunk its window), we will go with 23060 * suna + swnd, otherwise the sequence no will be unacceptable to the 23061 * receiver. 23062 */ 23063 if (tcp->tcp_zero_win_probe) { 23064 seq_no = tcp->tcp_suna; 23065 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 23066 ASSERT(tcp->tcp_swnd == 0); 23067 seq_no = tcp->tcp_snxt; 23068 } else { 23069 seq_no = SEQ_GT(tcp->tcp_snxt, 23070 (tcp->tcp_suna + tcp->tcp_swnd)) ? 23071 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 23072 } 23073 23074 if (tcp->tcp_valid_bits) { 23075 /* 23076 * For the complex case where we have to send some 23077 * controls (FIN or SYN), let tcp_xmit_mp do it. 23078 */ 23079 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 23080 NULL, B_FALSE)); 23081 } else { 23082 /* Generate a simple ACK */ 23083 int data_length; 23084 uchar_t *rptr; 23085 tcph_t *tcph; 23086 mblk_t *mp1; 23087 int32_t tcp_hdr_len; 23088 int32_t tcp_tcp_hdr_len; 23089 int32_t num_sack_blk = 0; 23090 int32_t sack_opt_len; 23091 23092 /* 23093 * Allocate space for TCP + IP headers 23094 * and link-level header 23095 */ 23096 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 23097 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 23098 tcp->tcp_num_sack_blk); 23099 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 23100 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 23101 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 23102 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 23103 } else { 23104 tcp_hdr_len = tcp->tcp_hdr_len; 23105 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 23106 } 23107 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 23108 if (!mp1) 23109 return (NULL); 23110 23111 /* Update the latest receive window size in TCP header. */ 23112 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 23113 tcp->tcp_tcph->th_win); 23114 /* copy in prototype TCP + IP header */ 23115 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 23116 mp1->b_rptr = rptr; 23117 mp1->b_wptr = rptr + tcp_hdr_len; 23118 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 23119 23120 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 23121 23122 /* Set the TCP sequence number. */ 23123 U32_TO_ABE32(seq_no, tcph->th_seq); 23124 23125 /* Set up the TCP flag field. */ 23126 tcph->th_flags[0] = (uchar_t)TH_ACK; 23127 if (tcp->tcp_ecn_echo_on) 23128 tcph->th_flags[0] |= TH_ECE; 23129 23130 tcp->tcp_rack = tcp->tcp_rnxt; 23131 tcp->tcp_rack_cnt = 0; 23132 23133 /* fill in timestamp option if in use */ 23134 if (tcp->tcp_snd_ts_ok) { 23135 uint32_t llbolt = (uint32_t)lbolt; 23136 23137 U32_TO_BE32(llbolt, 23138 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 23139 U32_TO_BE32(tcp->tcp_ts_recent, 23140 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 23141 } 23142 23143 /* Fill in SACK options */ 23144 if (num_sack_blk > 0) { 23145 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 23146 sack_blk_t *tmp; 23147 int32_t i; 23148 23149 wptr[0] = TCPOPT_NOP; 23150 wptr[1] = TCPOPT_NOP; 23151 wptr[2] = TCPOPT_SACK; 23152 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 23153 sizeof (sack_blk_t); 23154 wptr += TCPOPT_REAL_SACK_LEN; 23155 23156 tmp = tcp->tcp_sack_list; 23157 for (i = 0; i < num_sack_blk; i++) { 23158 U32_TO_BE32(tmp[i].begin, wptr); 23159 wptr += sizeof (tcp_seq); 23160 U32_TO_BE32(tmp[i].end, wptr); 23161 wptr += sizeof (tcp_seq); 23162 } 23163 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 23164 << 4); 23165 } 23166 23167 if (tcp->tcp_ipversion == IPV4_VERSION) { 23168 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 23169 } else { 23170 /* Check for ip6i_t header in sticky hdrs */ 23171 ip6_t *ip6 = (ip6_t *)(rptr + 23172 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23173 sizeof (ip6i_t) : 0)); 23174 23175 ip6->ip6_plen = htons(tcp_hdr_len - 23176 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23177 } 23178 23179 /* 23180 * Prime pump for checksum calculation in IP. Include the 23181 * adjustment for a source route if any. 23182 */ 23183 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23184 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23185 U16_TO_ABE16(data_length, tcph->th_sum); 23186 23187 if (tcp->tcp_ip_forward_progress) { 23188 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23189 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23190 tcp->tcp_ip_forward_progress = B_FALSE; 23191 } 23192 return (mp1); 23193 } 23194 } 23195 23196 /* 23197 * Hash list insertion routine for tcp_t structures. Each hash bucket 23198 * contains a list of tcp_t entries, and each entry is bound to a unique 23199 * port. If there are multiple tcp_t's that are bound to the same port, then 23200 * one of them will be linked into the hash bucket list, and the rest will 23201 * hang off of that one entry. For each port, entries bound to a specific IP 23202 * address will be inserted before those those bound to INADDR_ANY. 23203 */ 23204 static void 23205 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23206 { 23207 tcp_t **tcpp; 23208 tcp_t *tcpnext; 23209 tcp_t *tcphash; 23210 23211 if (tcp->tcp_ptpbhn != NULL) { 23212 ASSERT(!caller_holds_lock); 23213 tcp_bind_hash_remove(tcp); 23214 } 23215 tcpp = &tbf->tf_tcp; 23216 if (!caller_holds_lock) { 23217 mutex_enter(&tbf->tf_lock); 23218 } else { 23219 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23220 } 23221 tcphash = tcpp[0]; 23222 tcpnext = NULL; 23223 if (tcphash != NULL) { 23224 /* Look for an entry using the same port */ 23225 while ((tcphash = tcpp[0]) != NULL && 23226 tcp->tcp_lport != tcphash->tcp_lport) 23227 tcpp = &(tcphash->tcp_bind_hash); 23228 23229 /* The port was not found, just add to the end */ 23230 if (tcphash == NULL) 23231 goto insert; 23232 23233 /* 23234 * OK, there already exists an entry bound to the 23235 * same port. 23236 * 23237 * If the new tcp bound to the INADDR_ANY address 23238 * and the first one in the list is not bound to 23239 * INADDR_ANY we skip all entries until we find the 23240 * first one bound to INADDR_ANY. 23241 * This makes sure that applications binding to a 23242 * specific address get preference over those binding to 23243 * INADDR_ANY. 23244 */ 23245 tcpnext = tcphash; 23246 tcphash = NULL; 23247 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23248 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23249 while ((tcpnext = tcpp[0]) != NULL && 23250 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23251 tcpp = &(tcpnext->tcp_bind_hash_port); 23252 23253 if (tcpnext) { 23254 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23255 tcphash = tcpnext->tcp_bind_hash; 23256 if (tcphash != NULL) { 23257 tcphash->tcp_ptpbhn = 23258 &(tcp->tcp_bind_hash); 23259 tcpnext->tcp_bind_hash = NULL; 23260 } 23261 } 23262 } else { 23263 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23264 tcphash = tcpnext->tcp_bind_hash; 23265 if (tcphash != NULL) { 23266 tcphash->tcp_ptpbhn = 23267 &(tcp->tcp_bind_hash); 23268 tcpnext->tcp_bind_hash = NULL; 23269 } 23270 } 23271 } 23272 insert: 23273 tcp->tcp_bind_hash_port = tcpnext; 23274 tcp->tcp_bind_hash = tcphash; 23275 tcp->tcp_ptpbhn = tcpp; 23276 tcpp[0] = tcp; 23277 if (!caller_holds_lock) 23278 mutex_exit(&tbf->tf_lock); 23279 } 23280 23281 /* 23282 * Hash list removal routine for tcp_t structures. 23283 */ 23284 static void 23285 tcp_bind_hash_remove(tcp_t *tcp) 23286 { 23287 tcp_t *tcpnext; 23288 kmutex_t *lockp; 23289 tcp_stack_t *tcps = tcp->tcp_tcps; 23290 23291 if (tcp->tcp_ptpbhn == NULL) 23292 return; 23293 23294 /* 23295 * Extract the lock pointer in case there are concurrent 23296 * hash_remove's for this instance. 23297 */ 23298 ASSERT(tcp->tcp_lport != 0); 23299 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23300 23301 ASSERT(lockp != NULL); 23302 mutex_enter(lockp); 23303 if (tcp->tcp_ptpbhn) { 23304 tcpnext = tcp->tcp_bind_hash_port; 23305 if (tcpnext != NULL) { 23306 tcp->tcp_bind_hash_port = NULL; 23307 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23308 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23309 if (tcpnext->tcp_bind_hash != NULL) { 23310 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23311 &(tcpnext->tcp_bind_hash); 23312 tcp->tcp_bind_hash = NULL; 23313 } 23314 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23315 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23316 tcp->tcp_bind_hash = NULL; 23317 } 23318 *tcp->tcp_ptpbhn = tcpnext; 23319 tcp->tcp_ptpbhn = NULL; 23320 } 23321 mutex_exit(lockp); 23322 } 23323 23324 23325 /* 23326 * Hash list lookup routine for tcp_t structures. 23327 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23328 */ 23329 static tcp_t * 23330 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23331 { 23332 tf_t *tf; 23333 tcp_t *tcp; 23334 23335 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23336 mutex_enter(&tf->tf_lock); 23337 for (tcp = tf->tf_tcp; tcp != NULL; 23338 tcp = tcp->tcp_acceptor_hash) { 23339 if (tcp->tcp_acceptor_id == id) { 23340 CONN_INC_REF(tcp->tcp_connp); 23341 mutex_exit(&tf->tf_lock); 23342 return (tcp); 23343 } 23344 } 23345 mutex_exit(&tf->tf_lock); 23346 return (NULL); 23347 } 23348 23349 23350 /* 23351 * Hash list insertion routine for tcp_t structures. 23352 */ 23353 void 23354 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23355 { 23356 tf_t *tf; 23357 tcp_t **tcpp; 23358 tcp_t *tcpnext; 23359 tcp_stack_t *tcps = tcp->tcp_tcps; 23360 23361 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23362 23363 if (tcp->tcp_ptpahn != NULL) 23364 tcp_acceptor_hash_remove(tcp); 23365 tcpp = &tf->tf_tcp; 23366 mutex_enter(&tf->tf_lock); 23367 tcpnext = tcpp[0]; 23368 if (tcpnext) 23369 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23370 tcp->tcp_acceptor_hash = tcpnext; 23371 tcp->tcp_ptpahn = tcpp; 23372 tcpp[0] = tcp; 23373 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23374 mutex_exit(&tf->tf_lock); 23375 } 23376 23377 /* 23378 * Hash list removal routine for tcp_t structures. 23379 */ 23380 static void 23381 tcp_acceptor_hash_remove(tcp_t *tcp) 23382 { 23383 tcp_t *tcpnext; 23384 kmutex_t *lockp; 23385 23386 /* 23387 * Extract the lock pointer in case there are concurrent 23388 * hash_remove's for this instance. 23389 */ 23390 lockp = tcp->tcp_acceptor_lockp; 23391 23392 if (tcp->tcp_ptpahn == NULL) 23393 return; 23394 23395 ASSERT(lockp != NULL); 23396 mutex_enter(lockp); 23397 if (tcp->tcp_ptpahn) { 23398 tcpnext = tcp->tcp_acceptor_hash; 23399 if (tcpnext) { 23400 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23401 tcp->tcp_acceptor_hash = NULL; 23402 } 23403 *tcp->tcp_ptpahn = tcpnext; 23404 tcp->tcp_ptpahn = NULL; 23405 } 23406 mutex_exit(lockp); 23407 tcp->tcp_acceptor_lockp = NULL; 23408 } 23409 23410 /* 23411 * Type three generator adapted from the random() function in 4.4 BSD: 23412 */ 23413 23414 /* 23415 * Copyright (c) 1983, 1993 23416 * The Regents of the University of California. All rights reserved. 23417 * 23418 * Redistribution and use in source and binary forms, with or without 23419 * modification, are permitted provided that the following conditions 23420 * are met: 23421 * 1. Redistributions of source code must retain the above copyright 23422 * notice, this list of conditions and the following disclaimer. 23423 * 2. Redistributions in binary form must reproduce the above copyright 23424 * notice, this list of conditions and the following disclaimer in the 23425 * documentation and/or other materials provided with the distribution. 23426 * 3. All advertising materials mentioning features or use of this software 23427 * must display the following acknowledgement: 23428 * This product includes software developed by the University of 23429 * California, Berkeley and its contributors. 23430 * 4. Neither the name of the University nor the names of its contributors 23431 * may be used to endorse or promote products derived from this software 23432 * without specific prior written permission. 23433 * 23434 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23435 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23436 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23437 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23438 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23439 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23440 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23441 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23442 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23443 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23444 * SUCH DAMAGE. 23445 */ 23446 23447 /* Type 3 -- x**31 + x**3 + 1 */ 23448 #define DEG_3 31 23449 #define SEP_3 3 23450 23451 23452 /* Protected by tcp_random_lock */ 23453 static int tcp_randtbl[DEG_3 + 1]; 23454 23455 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23456 static int *tcp_random_rptr = &tcp_randtbl[1]; 23457 23458 static int *tcp_random_state = &tcp_randtbl[1]; 23459 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23460 23461 kmutex_t tcp_random_lock; 23462 23463 void 23464 tcp_random_init(void) 23465 { 23466 int i; 23467 hrtime_t hrt; 23468 time_t wallclock; 23469 uint64_t result; 23470 23471 /* 23472 * Use high-res timer and current time for seed. Gethrtime() returns 23473 * a longlong, which may contain resolution down to nanoseconds. 23474 * The current time will either be a 32-bit or a 64-bit quantity. 23475 * XOR the two together in a 64-bit result variable. 23476 * Convert the result to a 32-bit value by multiplying the high-order 23477 * 32-bits by the low-order 32-bits. 23478 */ 23479 23480 hrt = gethrtime(); 23481 (void) drv_getparm(TIME, &wallclock); 23482 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23483 mutex_enter(&tcp_random_lock); 23484 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23485 (result & 0xffffffff); 23486 23487 for (i = 1; i < DEG_3; i++) 23488 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23489 + 12345; 23490 tcp_random_fptr = &tcp_random_state[SEP_3]; 23491 tcp_random_rptr = &tcp_random_state[0]; 23492 mutex_exit(&tcp_random_lock); 23493 for (i = 0; i < 10 * DEG_3; i++) 23494 (void) tcp_random(); 23495 } 23496 23497 /* 23498 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23499 * This range is selected to be approximately centered on TCP_ISS / 2, 23500 * and easy to compute. We get this value by generating a 32-bit random 23501 * number, selecting out the high-order 17 bits, and then adding one so 23502 * that we never return zero. 23503 */ 23504 int 23505 tcp_random(void) 23506 { 23507 int i; 23508 23509 mutex_enter(&tcp_random_lock); 23510 *tcp_random_fptr += *tcp_random_rptr; 23511 23512 /* 23513 * The high-order bits are more random than the low-order bits, 23514 * so we select out the high-order 17 bits and add one so that 23515 * we never return zero. 23516 */ 23517 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23518 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23519 tcp_random_fptr = tcp_random_state; 23520 ++tcp_random_rptr; 23521 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23522 tcp_random_rptr = tcp_random_state; 23523 23524 mutex_exit(&tcp_random_lock); 23525 return (i); 23526 } 23527 23528 static int 23529 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23530 int *t_errorp, int *sys_errorp) 23531 { 23532 int error; 23533 int is_absreq_failure; 23534 t_scalar_t *opt_lenp; 23535 t_scalar_t opt_offset; 23536 int prim_type; 23537 struct T_conn_req *tcreqp; 23538 struct T_conn_res *tcresp; 23539 cred_t *cr; 23540 23541 /* 23542 * All Solaris components should pass a db_credp 23543 * for this TPI message, hence we ASSERT. 23544 * But in case there is some other M_PROTO that looks 23545 * like a TPI message sent by some other kernel 23546 * component, we check and return an error. 23547 */ 23548 cr = msg_getcred(mp, NULL); 23549 ASSERT(cr != NULL); 23550 if (cr == NULL) 23551 return (-1); 23552 23553 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23554 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23555 prim_type == T_CONN_RES); 23556 23557 switch (prim_type) { 23558 case T_CONN_REQ: 23559 tcreqp = (struct T_conn_req *)mp->b_rptr; 23560 opt_offset = tcreqp->OPT_offset; 23561 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23562 break; 23563 case O_T_CONN_RES: 23564 case T_CONN_RES: 23565 tcresp = (struct T_conn_res *)mp->b_rptr; 23566 opt_offset = tcresp->OPT_offset; 23567 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23568 break; 23569 } 23570 23571 *t_errorp = 0; 23572 *sys_errorp = 0; 23573 *do_disconnectp = 0; 23574 23575 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23576 opt_offset, cr, &tcp_opt_obj, 23577 NULL, &is_absreq_failure); 23578 23579 switch (error) { 23580 case 0: /* no error */ 23581 ASSERT(is_absreq_failure == 0); 23582 return (0); 23583 case ENOPROTOOPT: 23584 *t_errorp = TBADOPT; 23585 break; 23586 case EACCES: 23587 *t_errorp = TACCES; 23588 break; 23589 default: 23590 *t_errorp = TSYSERR; *sys_errorp = error; 23591 break; 23592 } 23593 if (is_absreq_failure != 0) { 23594 /* 23595 * The connection request should get the local ack 23596 * T_OK_ACK and then a T_DISCON_IND. 23597 */ 23598 *do_disconnectp = 1; 23599 } 23600 return (-1); 23601 } 23602 23603 /* 23604 * Split this function out so that if the secret changes, I'm okay. 23605 * 23606 * Initialize the tcp_iss_cookie and tcp_iss_key. 23607 */ 23608 23609 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23610 23611 static void 23612 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23613 { 23614 struct { 23615 int32_t current_time; 23616 uint32_t randnum; 23617 uint16_t pad; 23618 uint8_t ether[6]; 23619 uint8_t passwd[PASSWD_SIZE]; 23620 } tcp_iss_cookie; 23621 time_t t; 23622 23623 /* 23624 * Start with the current absolute time. 23625 */ 23626 (void) drv_getparm(TIME, &t); 23627 tcp_iss_cookie.current_time = t; 23628 23629 /* 23630 * XXX - Need a more random number per RFC 1750, not this crap. 23631 * OTOH, if what follows is pretty random, then I'm in better shape. 23632 */ 23633 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23634 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23635 23636 /* 23637 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23638 * as a good template. 23639 */ 23640 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23641 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23642 23643 /* 23644 * The pass-phrase. Normally this is supplied by user-called NDD. 23645 */ 23646 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23647 23648 /* 23649 * See 4010593 if this section becomes a problem again, 23650 * but the local ethernet address is useful here. 23651 */ 23652 (void) localetheraddr(NULL, 23653 (struct ether_addr *)&tcp_iss_cookie.ether); 23654 23655 /* 23656 * Hash 'em all together. The MD5Final is called per-connection. 23657 */ 23658 mutex_enter(&tcps->tcps_iss_key_lock); 23659 MD5Init(&tcps->tcps_iss_key); 23660 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23661 sizeof (tcp_iss_cookie)); 23662 mutex_exit(&tcps->tcps_iss_key_lock); 23663 } 23664 23665 /* 23666 * Set the RFC 1948 pass phrase 23667 */ 23668 /* ARGSUSED */ 23669 static int 23670 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23671 cred_t *cr) 23672 { 23673 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23674 23675 /* 23676 * Basically, value contains a new pass phrase. Pass it along! 23677 */ 23678 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23679 return (0); 23680 } 23681 23682 /* ARGSUSED */ 23683 static int 23684 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23685 { 23686 bzero(buf, sizeof (tcp_sack_info_t)); 23687 return (0); 23688 } 23689 23690 /* ARGSUSED */ 23691 static int 23692 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23693 { 23694 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23695 return (0); 23696 } 23697 23698 /* 23699 * Make sure we wait until the default queue is setup, yet allow 23700 * tcp_g_q_create() to open a TCP stream. 23701 * We need to allow tcp_g_q_create() do do an open 23702 * of tcp, hence we compare curhread. 23703 * All others have to wait until the tcps_g_q has been 23704 * setup. 23705 */ 23706 void 23707 tcp_g_q_setup(tcp_stack_t *tcps) 23708 { 23709 mutex_enter(&tcps->tcps_g_q_lock); 23710 if (tcps->tcps_g_q != NULL) { 23711 mutex_exit(&tcps->tcps_g_q_lock); 23712 return; 23713 } 23714 if (tcps->tcps_g_q_creator == NULL) { 23715 /* This thread will set it up */ 23716 tcps->tcps_g_q_creator = curthread; 23717 mutex_exit(&tcps->tcps_g_q_lock); 23718 tcp_g_q_create(tcps); 23719 mutex_enter(&tcps->tcps_g_q_lock); 23720 ASSERT(tcps->tcps_g_q_creator == curthread); 23721 tcps->tcps_g_q_creator = NULL; 23722 cv_signal(&tcps->tcps_g_q_cv); 23723 ASSERT(tcps->tcps_g_q != NULL); 23724 mutex_exit(&tcps->tcps_g_q_lock); 23725 return; 23726 } 23727 /* Everybody but the creator has to wait */ 23728 if (tcps->tcps_g_q_creator != curthread) { 23729 while (tcps->tcps_g_q == NULL) 23730 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23731 } 23732 mutex_exit(&tcps->tcps_g_q_lock); 23733 } 23734 23735 #define IP "ip" 23736 23737 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23738 23739 /* 23740 * Create a default tcp queue here instead of in strplumb 23741 */ 23742 void 23743 tcp_g_q_create(tcp_stack_t *tcps) 23744 { 23745 int error; 23746 ldi_handle_t lh = NULL; 23747 ldi_ident_t li = NULL; 23748 int rval; 23749 cred_t *cr; 23750 major_t IP_MAJ; 23751 23752 #ifdef NS_DEBUG 23753 (void) printf("tcp_g_q_create()\n"); 23754 #endif 23755 23756 IP_MAJ = ddi_name_to_major(IP); 23757 23758 ASSERT(tcps->tcps_g_q_creator == curthread); 23759 23760 error = ldi_ident_from_major(IP_MAJ, &li); 23761 if (error) { 23762 #ifdef DEBUG 23763 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23764 error); 23765 #endif 23766 return; 23767 } 23768 23769 cr = zone_get_kcred(netstackid_to_zoneid( 23770 tcps->tcps_netstack->netstack_stackid)); 23771 ASSERT(cr != NULL); 23772 /* 23773 * We set the tcp default queue to IPv6 because IPv4 falls 23774 * back to IPv6 when it can't find a client, but 23775 * IPv6 does not fall back to IPv4. 23776 */ 23777 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23778 if (error) { 23779 #ifdef DEBUG 23780 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23781 error); 23782 #endif 23783 goto out; 23784 } 23785 23786 /* 23787 * This ioctl causes the tcp framework to cache a pointer to 23788 * this stream, so we don't want to close the stream after 23789 * this operation. 23790 * Use the kernel credentials that are for the zone we're in. 23791 */ 23792 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23793 (intptr_t)0, FKIOCTL, cr, &rval); 23794 if (error) { 23795 #ifdef DEBUG 23796 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23797 "error %d\n", error); 23798 #endif 23799 goto out; 23800 } 23801 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23802 lh = NULL; 23803 out: 23804 /* Close layered handles */ 23805 if (li) 23806 ldi_ident_release(li); 23807 /* Keep cred around until _inactive needs it */ 23808 tcps->tcps_g_q_cr = cr; 23809 } 23810 23811 /* 23812 * We keep tcp_g_q set until all other tcp_t's in the zone 23813 * has gone away, and then when tcp_g_q_inactive() is called 23814 * we clear it. 23815 */ 23816 void 23817 tcp_g_q_destroy(tcp_stack_t *tcps) 23818 { 23819 #ifdef NS_DEBUG 23820 (void) printf("tcp_g_q_destroy()for stack %d\n", 23821 tcps->tcps_netstack->netstack_stackid); 23822 #endif 23823 23824 if (tcps->tcps_g_q == NULL) { 23825 return; /* Nothing to cleanup */ 23826 } 23827 /* 23828 * Drop reference corresponding to the default queue. 23829 * This reference was added from tcp_open when the default queue 23830 * was created, hence we compensate for this extra drop in 23831 * tcp_g_q_close. If the refcnt drops to zero here it means 23832 * the default queue was the last one to be open, in which 23833 * case, then tcp_g_q_inactive will be 23834 * called as a result of the refrele. 23835 */ 23836 TCPS_REFRELE(tcps); 23837 } 23838 23839 /* 23840 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23841 * Run by tcp_q_q_inactive using a taskq. 23842 */ 23843 static void 23844 tcp_g_q_close(void *arg) 23845 { 23846 tcp_stack_t *tcps = arg; 23847 int error; 23848 ldi_handle_t lh = NULL; 23849 ldi_ident_t li = NULL; 23850 cred_t *cr; 23851 major_t IP_MAJ; 23852 23853 IP_MAJ = ddi_name_to_major(IP); 23854 23855 #ifdef NS_DEBUG 23856 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23857 tcps->tcps_netstack->netstack_stackid, 23858 tcps->tcps_netstack->netstack_refcnt); 23859 #endif 23860 lh = tcps->tcps_g_q_lh; 23861 if (lh == NULL) 23862 return; /* Nothing to cleanup */ 23863 23864 ASSERT(tcps->tcps_refcnt == 1); 23865 ASSERT(tcps->tcps_g_q != NULL); 23866 23867 error = ldi_ident_from_major(IP_MAJ, &li); 23868 if (error) { 23869 #ifdef DEBUG 23870 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23871 error); 23872 #endif 23873 return; 23874 } 23875 23876 cr = tcps->tcps_g_q_cr; 23877 tcps->tcps_g_q_cr = NULL; 23878 ASSERT(cr != NULL); 23879 23880 /* 23881 * Make sure we can break the recursion when tcp_close decrements 23882 * the reference count causing g_q_inactive to be called again. 23883 */ 23884 tcps->tcps_g_q_lh = NULL; 23885 23886 /* close the default queue */ 23887 (void) ldi_close(lh, FREAD|FWRITE, cr); 23888 /* 23889 * At this point in time tcps and the rest of netstack_t might 23890 * have been deleted. 23891 */ 23892 tcps = NULL; 23893 23894 /* Close layered handles */ 23895 ldi_ident_release(li); 23896 crfree(cr); 23897 } 23898 23899 /* 23900 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23901 * 23902 * Have to ensure that the ldi routines are not used by an 23903 * interrupt thread by using a taskq. 23904 */ 23905 void 23906 tcp_g_q_inactive(tcp_stack_t *tcps) 23907 { 23908 if (tcps->tcps_g_q_lh == NULL) 23909 return; /* Nothing to cleanup */ 23910 23911 ASSERT(tcps->tcps_refcnt == 0); 23912 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23913 23914 if (servicing_interrupt()) { 23915 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23916 (void *) tcps, TQ_SLEEP); 23917 } else { 23918 tcp_g_q_close(tcps); 23919 } 23920 } 23921 23922 /* 23923 * Called by IP when IP is loaded into the kernel 23924 */ 23925 void 23926 tcp_ddi_g_init(void) 23927 { 23928 tcp_timercache = kmem_cache_create("tcp_timercache", 23929 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23930 NULL, NULL, NULL, NULL, NULL, 0); 23931 23932 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23933 sizeof (tcp_sack_info_t), 0, 23934 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23935 23936 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23937 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23938 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23939 23940 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23941 23942 /* Initialize the random number generator */ 23943 tcp_random_init(); 23944 23945 /* A single callback independently of how many netstacks we have */ 23946 ip_squeue_init(tcp_squeue_add); 23947 23948 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23949 23950 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23951 TASKQ_PREPOPULATE); 23952 23953 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23954 23955 /* 23956 * We want to be informed each time a stack is created or 23957 * destroyed in the kernel, so we can maintain the 23958 * set of tcp_stack_t's. 23959 */ 23960 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23961 tcp_stack_fini); 23962 } 23963 23964 23965 #define INET_NAME "ip" 23966 23967 /* 23968 * Initialize the TCP stack instance. 23969 */ 23970 static void * 23971 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23972 { 23973 tcp_stack_t *tcps; 23974 tcpparam_t *pa; 23975 int i; 23976 int error = 0; 23977 major_t major; 23978 23979 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23980 tcps->tcps_netstack = ns; 23981 23982 /* Initialize locks */ 23983 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23984 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23985 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23986 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23987 23988 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23989 tcps->tcps_g_epriv_ports[0] = 2049; 23990 tcps->tcps_g_epriv_ports[1] = 4045; 23991 tcps->tcps_min_anonpriv_port = 512; 23992 23993 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23994 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23995 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23996 TCP_FANOUT_SIZE, KM_SLEEP); 23997 23998 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23999 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 24000 MUTEX_DEFAULT, NULL); 24001 } 24002 24003 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24004 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 24005 MUTEX_DEFAULT, NULL); 24006 } 24007 24008 /* TCP's IPsec code calls the packet dropper. */ 24009 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 24010 24011 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 24012 tcps->tcps_params = pa; 24013 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24014 24015 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 24016 A_CNT(lcl_tcp_param_arr), tcps); 24017 24018 /* 24019 * Note: To really walk the device tree you need the devinfo 24020 * pointer to your device which is only available after probe/attach. 24021 * The following is safe only because it uses ddi_root_node() 24022 */ 24023 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 24024 tcp_opt_obj.odb_opt_arr_cnt); 24025 24026 /* 24027 * Initialize RFC 1948 secret values. This will probably be reset once 24028 * by the boot scripts. 24029 * 24030 * Use NULL name, as the name is caught by the new lockstats. 24031 * 24032 * Initialize with some random, non-guessable string, like the global 24033 * T_INFO_ACK. 24034 */ 24035 24036 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 24037 sizeof (tcp_g_t_info_ack), tcps); 24038 24039 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 24040 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 24041 24042 major = mod_name_to_major(INET_NAME); 24043 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 24044 ASSERT(error == 0); 24045 return (tcps); 24046 } 24047 24048 /* 24049 * Called when the IP module is about to be unloaded. 24050 */ 24051 void 24052 tcp_ddi_g_destroy(void) 24053 { 24054 tcp_g_kstat_fini(tcp_g_kstat); 24055 tcp_g_kstat = NULL; 24056 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 24057 24058 mutex_destroy(&tcp_random_lock); 24059 24060 kmem_cache_destroy(tcp_timercache); 24061 kmem_cache_destroy(tcp_sack_info_cache); 24062 kmem_cache_destroy(tcp_iphc_cache); 24063 24064 netstack_unregister(NS_TCP); 24065 taskq_destroy(tcp_taskq); 24066 } 24067 24068 /* 24069 * Shut down the TCP stack instance. 24070 */ 24071 /* ARGSUSED */ 24072 static void 24073 tcp_stack_shutdown(netstackid_t stackid, void *arg) 24074 { 24075 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24076 24077 tcp_g_q_destroy(tcps); 24078 } 24079 24080 /* 24081 * Free the TCP stack instance. 24082 */ 24083 static void 24084 tcp_stack_fini(netstackid_t stackid, void *arg) 24085 { 24086 tcp_stack_t *tcps = (tcp_stack_t *)arg; 24087 int i; 24088 24089 nd_free(&tcps->tcps_g_nd); 24090 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 24091 tcps->tcps_params = NULL; 24092 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 24093 tcps->tcps_wroff_xtra_param = NULL; 24094 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 24095 tcps->tcps_mdt_head_param = NULL; 24096 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 24097 tcps->tcps_mdt_tail_param = NULL; 24098 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 24099 tcps->tcps_mdt_max_pbufs_param = NULL; 24100 24101 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 24102 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 24103 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 24104 } 24105 24106 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 24107 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 24108 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 24109 } 24110 24111 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 24112 tcps->tcps_bind_fanout = NULL; 24113 24114 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 24115 tcps->tcps_acceptor_fanout = NULL; 24116 24117 mutex_destroy(&tcps->tcps_iss_key_lock); 24118 mutex_destroy(&tcps->tcps_g_q_lock); 24119 cv_destroy(&tcps->tcps_g_q_cv); 24120 mutex_destroy(&tcps->tcps_epriv_port_lock); 24121 24122 ip_drop_unregister(&tcps->tcps_dropper); 24123 24124 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 24125 tcps->tcps_kstat = NULL; 24126 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 24127 24128 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 24129 tcps->tcps_mibkp = NULL; 24130 24131 ldi_ident_release(tcps->tcps_ldi_ident); 24132 kmem_free(tcps, sizeof (*tcps)); 24133 } 24134 24135 /* 24136 * Generate ISS, taking into account NDD changes may happen halfway through. 24137 * (If the iss is not zero, set it.) 24138 */ 24139 24140 static void 24141 tcp_iss_init(tcp_t *tcp) 24142 { 24143 MD5_CTX context; 24144 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 24145 uint32_t answer[4]; 24146 tcp_stack_t *tcps = tcp->tcp_tcps; 24147 24148 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 24149 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 24150 switch (tcps->tcps_strong_iss) { 24151 case 2: 24152 mutex_enter(&tcps->tcps_iss_key_lock); 24153 context = tcps->tcps_iss_key; 24154 mutex_exit(&tcps->tcps_iss_key_lock); 24155 arg.ports = tcp->tcp_ports; 24156 if (tcp->tcp_ipversion == IPV4_VERSION) { 24157 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 24158 &arg.src); 24159 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 24160 &arg.dst); 24161 } else { 24162 arg.src = tcp->tcp_ip6h->ip6_src; 24163 arg.dst = tcp->tcp_ip6h->ip6_dst; 24164 } 24165 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 24166 MD5Final((uchar_t *)answer, &context); 24167 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 24168 /* 24169 * Now that we've hashed into a unique per-connection sequence 24170 * space, add a random increment per strong_iss == 1. So I 24171 * guess we'll have to... 24172 */ 24173 /* FALLTHRU */ 24174 case 1: 24175 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24176 break; 24177 default: 24178 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24179 break; 24180 } 24181 tcp->tcp_valid_bits = TCP_ISS_VALID; 24182 tcp->tcp_fss = tcp->tcp_iss - 1; 24183 tcp->tcp_suna = tcp->tcp_iss; 24184 tcp->tcp_snxt = tcp->tcp_iss + 1; 24185 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24186 tcp->tcp_csuna = tcp->tcp_snxt; 24187 } 24188 24189 /* 24190 * Exported routine for extracting active tcp connection status. 24191 * 24192 * This is used by the Solaris Cluster Networking software to 24193 * gather a list of connections that need to be forwarded to 24194 * specific nodes in the cluster when configuration changes occur. 24195 * 24196 * The callback is invoked for each tcp_t structure from all netstacks, 24197 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24198 * from the netstack with the specified stack_id. Returning 24199 * non-zero from the callback routine terminates the search. 24200 */ 24201 int 24202 cl_tcp_walk_list(netstackid_t stack_id, 24203 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24204 { 24205 netstack_handle_t nh; 24206 netstack_t *ns; 24207 int ret = 0; 24208 24209 if (stack_id >= 0) { 24210 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24211 return (EINVAL); 24212 24213 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24214 ns->netstack_tcp); 24215 netstack_rele(ns); 24216 return (ret); 24217 } 24218 24219 netstack_next_init(&nh); 24220 while ((ns = netstack_next(&nh)) != NULL) { 24221 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24222 ns->netstack_tcp); 24223 netstack_rele(ns); 24224 } 24225 netstack_next_fini(&nh); 24226 return (ret); 24227 } 24228 24229 static int 24230 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24231 tcp_stack_t *tcps) 24232 { 24233 tcp_t *tcp; 24234 cl_tcp_info_t cl_tcpi; 24235 connf_t *connfp; 24236 conn_t *connp; 24237 int i; 24238 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24239 24240 ASSERT(callback != NULL); 24241 24242 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24243 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24244 connp = NULL; 24245 24246 while ((connp = 24247 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24248 24249 tcp = connp->conn_tcp; 24250 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24251 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24252 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24253 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24254 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24255 /* 24256 * The macros tcp_laddr and tcp_faddr give the IPv4 24257 * addresses. They are copied implicitly below as 24258 * mapped addresses. 24259 */ 24260 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24261 if (tcp->tcp_ipversion == IPV4_VERSION) { 24262 cl_tcpi.cl_tcpi_faddr = 24263 tcp->tcp_ipha->ipha_dst; 24264 } else { 24265 cl_tcpi.cl_tcpi_faddr_v6 = 24266 tcp->tcp_ip6h->ip6_dst; 24267 } 24268 24269 /* 24270 * If the callback returns non-zero 24271 * we terminate the traversal. 24272 */ 24273 if ((*callback)(&cl_tcpi, arg) != 0) { 24274 CONN_DEC_REF(tcp->tcp_connp); 24275 return (1); 24276 } 24277 } 24278 } 24279 24280 return (0); 24281 } 24282 24283 /* 24284 * Macros used for accessing the different types of sockaddr 24285 * structures inside a tcp_ioc_abort_conn_t. 24286 */ 24287 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24288 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24289 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24290 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24291 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24292 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24293 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24294 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24295 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24296 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24297 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24298 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24299 24300 /* 24301 * Return the correct error code to mimic the behavior 24302 * of a connection reset. 24303 */ 24304 #define TCP_AC_GET_ERRCODE(state, err) { \ 24305 switch ((state)) { \ 24306 case TCPS_SYN_SENT: \ 24307 case TCPS_SYN_RCVD: \ 24308 (err) = ECONNREFUSED; \ 24309 break; \ 24310 case TCPS_ESTABLISHED: \ 24311 case TCPS_FIN_WAIT_1: \ 24312 case TCPS_FIN_WAIT_2: \ 24313 case TCPS_CLOSE_WAIT: \ 24314 (err) = ECONNRESET; \ 24315 break; \ 24316 case TCPS_CLOSING: \ 24317 case TCPS_LAST_ACK: \ 24318 case TCPS_TIME_WAIT: \ 24319 (err) = 0; \ 24320 break; \ 24321 default: \ 24322 (err) = ENXIO; \ 24323 } \ 24324 } 24325 24326 /* 24327 * Check if a tcp structure matches the info in acp. 24328 */ 24329 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24330 (((acp)->ac_local.ss_family == AF_INET) ? \ 24331 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24332 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24333 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24334 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24335 (TCP_AC_V4LPORT((acp)) == 0 || \ 24336 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24337 (TCP_AC_V4RPORT((acp)) == 0 || \ 24338 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24339 (acp)->ac_start <= (tcp)->tcp_state && \ 24340 (acp)->ac_end >= (tcp)->tcp_state) : \ 24341 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24342 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24343 &(tcp)->tcp_ip_src_v6)) && \ 24344 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24345 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24346 &(tcp)->tcp_remote_v6)) && \ 24347 (TCP_AC_V6LPORT((acp)) == 0 || \ 24348 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24349 (TCP_AC_V6RPORT((acp)) == 0 || \ 24350 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24351 (acp)->ac_start <= (tcp)->tcp_state && \ 24352 (acp)->ac_end >= (tcp)->tcp_state)) 24353 24354 #define TCP_AC_MATCH(acp, tcp) \ 24355 (((acp)->ac_zoneid == ALL_ZONES || \ 24356 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24357 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24358 24359 /* 24360 * Build a message containing a tcp_ioc_abort_conn_t structure 24361 * which is filled in with information from acp and tp. 24362 */ 24363 static mblk_t * 24364 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24365 { 24366 mblk_t *mp; 24367 tcp_ioc_abort_conn_t *tacp; 24368 24369 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24370 if (mp == NULL) 24371 return (NULL); 24372 24373 mp->b_datap->db_type = M_CTL; 24374 24375 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24376 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24377 sizeof (uint32_t)); 24378 24379 tacp->ac_start = acp->ac_start; 24380 tacp->ac_end = acp->ac_end; 24381 tacp->ac_zoneid = acp->ac_zoneid; 24382 24383 if (acp->ac_local.ss_family == AF_INET) { 24384 tacp->ac_local.ss_family = AF_INET; 24385 tacp->ac_remote.ss_family = AF_INET; 24386 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24387 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24388 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24389 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24390 } else { 24391 tacp->ac_local.ss_family = AF_INET6; 24392 tacp->ac_remote.ss_family = AF_INET6; 24393 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24394 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24395 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24396 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24397 } 24398 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24399 return (mp); 24400 } 24401 24402 /* 24403 * Print a tcp_ioc_abort_conn_t structure. 24404 */ 24405 static void 24406 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24407 { 24408 char lbuf[128]; 24409 char rbuf[128]; 24410 sa_family_t af; 24411 in_port_t lport, rport; 24412 ushort_t logflags; 24413 24414 af = acp->ac_local.ss_family; 24415 24416 if (af == AF_INET) { 24417 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24418 lbuf, 128); 24419 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24420 rbuf, 128); 24421 lport = ntohs(TCP_AC_V4LPORT(acp)); 24422 rport = ntohs(TCP_AC_V4RPORT(acp)); 24423 } else { 24424 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24425 lbuf, 128); 24426 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24427 rbuf, 128); 24428 lport = ntohs(TCP_AC_V6LPORT(acp)); 24429 rport = ntohs(TCP_AC_V6RPORT(acp)); 24430 } 24431 24432 logflags = SL_TRACE | SL_NOTE; 24433 /* 24434 * Don't print this message to the console if the operation was done 24435 * to a non-global zone. 24436 */ 24437 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24438 logflags |= SL_CONSOLE; 24439 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24440 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24441 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24442 acp->ac_start, acp->ac_end); 24443 } 24444 24445 /* 24446 * Called inside tcp_rput when a message built using 24447 * tcp_ioctl_abort_build_msg is put into a queue. 24448 * Note that when we get here there is no wildcard in acp any more. 24449 */ 24450 static void 24451 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24452 { 24453 tcp_ioc_abort_conn_t *acp; 24454 24455 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24456 if (tcp->tcp_state <= acp->ac_end) { 24457 /* 24458 * If we get here, we are already on the correct 24459 * squeue. This ioctl follows the following path 24460 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24461 * ->tcp_ioctl_abort->squeue_enter (if on a 24462 * different squeue) 24463 */ 24464 int errcode; 24465 24466 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24467 (void) tcp_clean_death(tcp, errcode, 26); 24468 } 24469 freemsg(mp); 24470 } 24471 24472 /* 24473 * Abort all matching connections on a hash chain. 24474 */ 24475 static int 24476 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24477 boolean_t exact, tcp_stack_t *tcps) 24478 { 24479 int nmatch, err = 0; 24480 tcp_t *tcp; 24481 MBLKP mp, last, listhead = NULL; 24482 conn_t *tconnp; 24483 connf_t *connfp; 24484 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24485 24486 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24487 24488 startover: 24489 nmatch = 0; 24490 24491 mutex_enter(&connfp->connf_lock); 24492 for (tconnp = connfp->connf_head; tconnp != NULL; 24493 tconnp = tconnp->conn_next) { 24494 tcp = tconnp->conn_tcp; 24495 if (TCP_AC_MATCH(acp, tcp)) { 24496 CONN_INC_REF(tcp->tcp_connp); 24497 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24498 if (mp == NULL) { 24499 err = ENOMEM; 24500 CONN_DEC_REF(tcp->tcp_connp); 24501 break; 24502 } 24503 mp->b_prev = (mblk_t *)tcp; 24504 24505 if (listhead == NULL) { 24506 listhead = mp; 24507 last = mp; 24508 } else { 24509 last->b_next = mp; 24510 last = mp; 24511 } 24512 nmatch++; 24513 if (exact) 24514 break; 24515 } 24516 24517 /* Avoid holding lock for too long. */ 24518 if (nmatch >= 500) 24519 break; 24520 } 24521 mutex_exit(&connfp->connf_lock); 24522 24523 /* Pass mp into the correct tcp */ 24524 while ((mp = listhead) != NULL) { 24525 listhead = listhead->b_next; 24526 tcp = (tcp_t *)mp->b_prev; 24527 mp->b_next = mp->b_prev = NULL; 24528 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24529 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24530 } 24531 24532 *count += nmatch; 24533 if (nmatch >= 500 && err == 0) 24534 goto startover; 24535 return (err); 24536 } 24537 24538 /* 24539 * Abort all connections that matches the attributes specified in acp. 24540 */ 24541 static int 24542 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24543 { 24544 sa_family_t af; 24545 uint32_t ports; 24546 uint16_t *pports; 24547 int err = 0, count = 0; 24548 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24549 int index = -1; 24550 ushort_t logflags; 24551 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24552 24553 af = acp->ac_local.ss_family; 24554 24555 if (af == AF_INET) { 24556 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24557 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24558 pports = (uint16_t *)&ports; 24559 pports[1] = TCP_AC_V4LPORT(acp); 24560 pports[0] = TCP_AC_V4RPORT(acp); 24561 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24562 } 24563 } else { 24564 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24565 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24566 pports = (uint16_t *)&ports; 24567 pports[1] = TCP_AC_V6LPORT(acp); 24568 pports[0] = TCP_AC_V6RPORT(acp); 24569 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24570 } 24571 } 24572 24573 /* 24574 * For cases where remote addr, local port, and remote port are non- 24575 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24576 */ 24577 if (index != -1) { 24578 err = tcp_ioctl_abort_bucket(acp, index, 24579 &count, exact, tcps); 24580 } else { 24581 /* 24582 * loop through all entries for wildcard case 24583 */ 24584 for (index = 0; 24585 index < ipst->ips_ipcl_conn_fanout_size; 24586 index++) { 24587 err = tcp_ioctl_abort_bucket(acp, index, 24588 &count, exact, tcps); 24589 if (err != 0) 24590 break; 24591 } 24592 } 24593 24594 logflags = SL_TRACE | SL_NOTE; 24595 /* 24596 * Don't print this message to the console if the operation was done 24597 * to a non-global zone. 24598 */ 24599 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24600 logflags |= SL_CONSOLE; 24601 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24602 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24603 if (err == 0 && count == 0) 24604 err = ENOENT; 24605 return (err); 24606 } 24607 24608 /* 24609 * Process the TCP_IOC_ABORT_CONN ioctl request. 24610 */ 24611 static void 24612 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24613 { 24614 int err; 24615 IOCP iocp; 24616 MBLKP mp1; 24617 sa_family_t laf, raf; 24618 tcp_ioc_abort_conn_t *acp; 24619 zone_t *zptr; 24620 conn_t *connp = Q_TO_CONN(q); 24621 zoneid_t zoneid = connp->conn_zoneid; 24622 tcp_t *tcp = connp->conn_tcp; 24623 tcp_stack_t *tcps = tcp->tcp_tcps; 24624 24625 iocp = (IOCP)mp->b_rptr; 24626 24627 if ((mp1 = mp->b_cont) == NULL || 24628 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24629 err = EINVAL; 24630 goto out; 24631 } 24632 24633 /* check permissions */ 24634 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24635 err = EPERM; 24636 goto out; 24637 } 24638 24639 if (mp1->b_cont != NULL) { 24640 freemsg(mp1->b_cont); 24641 mp1->b_cont = NULL; 24642 } 24643 24644 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24645 laf = acp->ac_local.ss_family; 24646 raf = acp->ac_remote.ss_family; 24647 24648 /* check that a zone with the supplied zoneid exists */ 24649 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24650 zptr = zone_find_by_id(zoneid); 24651 if (zptr != NULL) { 24652 zone_rele(zptr); 24653 } else { 24654 err = EINVAL; 24655 goto out; 24656 } 24657 } 24658 24659 /* 24660 * For exclusive stacks we set the zoneid to zero 24661 * to make TCP operate as if in the global zone. 24662 */ 24663 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24664 acp->ac_zoneid = GLOBAL_ZONEID; 24665 24666 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24667 acp->ac_start > acp->ac_end || laf != raf || 24668 (laf != AF_INET && laf != AF_INET6)) { 24669 err = EINVAL; 24670 goto out; 24671 } 24672 24673 tcp_ioctl_abort_dump(acp); 24674 err = tcp_ioctl_abort(acp, tcps); 24675 24676 out: 24677 if (mp1 != NULL) { 24678 freemsg(mp1); 24679 mp->b_cont = NULL; 24680 } 24681 24682 if (err != 0) 24683 miocnak(q, mp, 0, err); 24684 else 24685 miocack(q, mp, 0, 0); 24686 } 24687 24688 /* 24689 * tcp_time_wait_processing() handles processing of incoming packets when 24690 * the tcp is in the TIME_WAIT state. 24691 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24692 * on the time wait list. 24693 */ 24694 void 24695 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24696 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24697 { 24698 int32_t bytes_acked; 24699 int32_t gap; 24700 int32_t rgap; 24701 tcp_opt_t tcpopt; 24702 uint_t flags; 24703 uint32_t new_swnd = 0; 24704 conn_t *connp; 24705 tcp_stack_t *tcps = tcp->tcp_tcps; 24706 24707 BUMP_LOCAL(tcp->tcp_ibsegs); 24708 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24709 24710 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24711 new_swnd = BE16_TO_U16(tcph->th_win) << 24712 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24713 if (tcp->tcp_snd_ts_ok) { 24714 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24715 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24716 tcp->tcp_rnxt, TH_ACK); 24717 goto done; 24718 } 24719 } 24720 gap = seg_seq - tcp->tcp_rnxt; 24721 rgap = tcp->tcp_rwnd - (gap + seg_len); 24722 if (gap < 0) { 24723 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24724 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24725 (seg_len > -gap ? -gap : seg_len)); 24726 seg_len += gap; 24727 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24728 if (flags & TH_RST) { 24729 goto done; 24730 } 24731 if ((flags & TH_FIN) && seg_len == -1) { 24732 /* 24733 * When TCP receives a duplicate FIN in 24734 * TIME_WAIT state, restart the 2 MSL timer. 24735 * See page 73 in RFC 793. Make sure this TCP 24736 * is already on the TIME_WAIT list. If not, 24737 * just restart the timer. 24738 */ 24739 if (TCP_IS_DETACHED(tcp)) { 24740 if (tcp_time_wait_remove(tcp, NULL) == 24741 B_TRUE) { 24742 tcp_time_wait_append(tcp); 24743 TCP_DBGSTAT(tcps, 24744 tcp_rput_time_wait); 24745 } 24746 } else { 24747 ASSERT(tcp != NULL); 24748 TCP_TIMER_RESTART(tcp, 24749 tcps->tcps_time_wait_interval); 24750 } 24751 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24752 tcp->tcp_rnxt, TH_ACK); 24753 goto done; 24754 } 24755 flags |= TH_ACK_NEEDED; 24756 seg_len = 0; 24757 goto process_ack; 24758 } 24759 24760 /* Fix seg_seq, and chew the gap off the front. */ 24761 seg_seq = tcp->tcp_rnxt; 24762 } 24763 24764 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24765 /* 24766 * Make sure that when we accept the connection, pick 24767 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24768 * old connection. 24769 * 24770 * The next ISS generated is equal to tcp_iss_incr_extra 24771 * + ISS_INCR/2 + other components depending on the 24772 * value of tcp_strong_iss. We pre-calculate the new 24773 * ISS here and compare with tcp_snxt to determine if 24774 * we need to make adjustment to tcp_iss_incr_extra. 24775 * 24776 * The above calculation is ugly and is a 24777 * waste of CPU cycles... 24778 */ 24779 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24780 int32_t adj; 24781 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24782 24783 switch (tcps->tcps_strong_iss) { 24784 case 2: { 24785 /* Add time and MD5 components. */ 24786 uint32_t answer[4]; 24787 struct { 24788 uint32_t ports; 24789 in6_addr_t src; 24790 in6_addr_t dst; 24791 } arg; 24792 MD5_CTX context; 24793 24794 mutex_enter(&tcps->tcps_iss_key_lock); 24795 context = tcps->tcps_iss_key; 24796 mutex_exit(&tcps->tcps_iss_key_lock); 24797 arg.ports = tcp->tcp_ports; 24798 /* We use MAPPED addresses in tcp_iss_init */ 24799 arg.src = tcp->tcp_ip_src_v6; 24800 if (tcp->tcp_ipversion == IPV4_VERSION) { 24801 IN6_IPADDR_TO_V4MAPPED( 24802 tcp->tcp_ipha->ipha_dst, 24803 &arg.dst); 24804 } else { 24805 arg.dst = 24806 tcp->tcp_ip6h->ip6_dst; 24807 } 24808 MD5Update(&context, (uchar_t *)&arg, 24809 sizeof (arg)); 24810 MD5Final((uchar_t *)answer, &context); 24811 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24812 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24813 break; 24814 } 24815 case 1: 24816 /* Add time component and min random (i.e. 1). */ 24817 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24818 break; 24819 default: 24820 /* Add only time component. */ 24821 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24822 break; 24823 } 24824 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24825 /* 24826 * New ISS not guaranteed to be ISS_INCR/2 24827 * ahead of the current tcp_snxt, so add the 24828 * difference to tcp_iss_incr_extra. 24829 */ 24830 tcps->tcps_iss_incr_extra += adj; 24831 } 24832 /* 24833 * If tcp_clean_death() can not perform the task now, 24834 * drop the SYN packet and let the other side re-xmit. 24835 * Otherwise pass the SYN packet back in, since the 24836 * old tcp state has been cleaned up or freed. 24837 */ 24838 if (tcp_clean_death(tcp, 0, 27) == -1) 24839 goto done; 24840 /* 24841 * We will come back to tcp_rput_data 24842 * on the global queue. Packets destined 24843 * for the global queue will be checked 24844 * with global policy. But the policy for 24845 * this packet has already been checked as 24846 * this was destined for the detached 24847 * connection. We need to bypass policy 24848 * check this time by attaching a dummy 24849 * ipsec_in with ipsec_in_dont_check set. 24850 */ 24851 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24852 if (connp != NULL) { 24853 TCP_STAT(tcps, tcp_time_wait_syn_success); 24854 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24855 return; 24856 } 24857 goto done; 24858 } 24859 24860 /* 24861 * rgap is the amount of stuff received out of window. A negative 24862 * value is the amount out of window. 24863 */ 24864 if (rgap < 0) { 24865 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24866 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24867 /* Fix seg_len and make sure there is something left. */ 24868 seg_len += rgap; 24869 if (seg_len <= 0) { 24870 if (flags & TH_RST) { 24871 goto done; 24872 } 24873 flags |= TH_ACK_NEEDED; 24874 seg_len = 0; 24875 goto process_ack; 24876 } 24877 } 24878 /* 24879 * Check whether we can update tcp_ts_recent. This test is 24880 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24881 * Extensions for High Performance: An Update", Internet Draft. 24882 */ 24883 if (tcp->tcp_snd_ts_ok && 24884 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24885 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24886 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24887 tcp->tcp_last_rcv_lbolt = lbolt64; 24888 } 24889 24890 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24891 /* Always ack out of order packets */ 24892 flags |= TH_ACK_NEEDED; 24893 seg_len = 0; 24894 } else if (seg_len > 0) { 24895 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24896 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24897 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24898 } 24899 if (flags & TH_RST) { 24900 (void) tcp_clean_death(tcp, 0, 28); 24901 goto done; 24902 } 24903 if (flags & TH_SYN) { 24904 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24905 TH_RST|TH_ACK); 24906 /* 24907 * Do not delete the TCP structure if it is in 24908 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24909 */ 24910 goto done; 24911 } 24912 process_ack: 24913 if (flags & TH_ACK) { 24914 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24915 if (bytes_acked <= 0) { 24916 if (bytes_acked == 0 && seg_len == 0 && 24917 new_swnd == tcp->tcp_swnd) 24918 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24919 } else { 24920 /* Acks something not sent */ 24921 flags |= TH_ACK_NEEDED; 24922 } 24923 } 24924 if (flags & TH_ACK_NEEDED) { 24925 /* 24926 * Time to send an ack for some reason. 24927 */ 24928 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24929 tcp->tcp_rnxt, TH_ACK); 24930 } 24931 done: 24932 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24933 DB_CKSUMSTART(mp) = 0; 24934 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24935 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24936 } 24937 freemsg(mp); 24938 } 24939 24940 /* 24941 * TCP Timers Implementation. 24942 */ 24943 timeout_id_t 24944 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24945 { 24946 mblk_t *mp; 24947 tcp_timer_t *tcpt; 24948 tcp_t *tcp = connp->conn_tcp; 24949 24950 ASSERT(connp->conn_sqp != NULL); 24951 24952 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24953 24954 if (tcp->tcp_timercache == NULL) { 24955 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24956 } else { 24957 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24958 mp = tcp->tcp_timercache; 24959 tcp->tcp_timercache = mp->b_next; 24960 mp->b_next = NULL; 24961 ASSERT(mp->b_wptr == NULL); 24962 } 24963 24964 CONN_INC_REF(connp); 24965 tcpt = (tcp_timer_t *)mp->b_rptr; 24966 tcpt->connp = connp; 24967 tcpt->tcpt_proc = f; 24968 /* 24969 * TCP timers are normal timeouts. Plus, they do not require more than 24970 * a 10 millisecond resolution. By choosing a coarser resolution and by 24971 * rounding up the expiration to the next resolution boundary, we can 24972 * batch timers in the callout subsystem to make TCP timers more 24973 * efficient. The roundup also protects short timers from expiring too 24974 * early before they have a chance to be cancelled. 24975 */ 24976 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24977 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24978 24979 return ((timeout_id_t)mp); 24980 } 24981 24982 static void 24983 tcp_timer_callback(void *arg) 24984 { 24985 mblk_t *mp = (mblk_t *)arg; 24986 tcp_timer_t *tcpt; 24987 conn_t *connp; 24988 24989 tcpt = (tcp_timer_t *)mp->b_rptr; 24990 connp = tcpt->connp; 24991 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24992 SQ_FILL, SQTAG_TCP_TIMER); 24993 } 24994 24995 static void 24996 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24997 { 24998 tcp_timer_t *tcpt; 24999 conn_t *connp = (conn_t *)arg; 25000 tcp_t *tcp = connp->conn_tcp; 25001 25002 tcpt = (tcp_timer_t *)mp->b_rptr; 25003 ASSERT(connp == tcpt->connp); 25004 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 25005 25006 /* 25007 * If the TCP has reached the closed state, don't proceed any 25008 * further. This TCP logically does not exist on the system. 25009 * tcpt_proc could for example access queues, that have already 25010 * been qprocoff'ed off. Also see comments at the start of tcp_input 25011 */ 25012 if (tcp->tcp_state != TCPS_CLOSED) { 25013 (*tcpt->tcpt_proc)(connp); 25014 } else { 25015 tcp->tcp_timer_tid = 0; 25016 } 25017 tcp_timer_free(connp->conn_tcp, mp); 25018 } 25019 25020 /* 25021 * There is potential race with untimeout and the handler firing at the same 25022 * time. The mblock may be freed by the handler while we are trying to use 25023 * it. But since both should execute on the same squeue, this race should not 25024 * occur. 25025 */ 25026 clock_t 25027 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 25028 { 25029 mblk_t *mp = (mblk_t *)id; 25030 tcp_timer_t *tcpt; 25031 clock_t delta; 25032 25033 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 25034 25035 if (mp == NULL) 25036 return (-1); 25037 25038 tcpt = (tcp_timer_t *)mp->b_rptr; 25039 ASSERT(tcpt->connp == connp); 25040 25041 delta = untimeout_default(tcpt->tcpt_tid, 0); 25042 25043 if (delta >= 0) { 25044 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 25045 tcp_timer_free(connp->conn_tcp, mp); 25046 CONN_DEC_REF(connp); 25047 } 25048 25049 return (delta); 25050 } 25051 25052 /* 25053 * Allocate space for the timer event. The allocation looks like mblk, but it is 25054 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 25055 * 25056 * Dealing with failures: If we can't allocate from the timer cache we try 25057 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 25058 * points to b_rptr. 25059 * If we can't allocate anything using allocb_tryhard(), we perform a last 25060 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 25061 * save the actual allocation size in b_datap. 25062 */ 25063 mblk_t * 25064 tcp_timermp_alloc(int kmflags) 25065 { 25066 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 25067 kmflags & ~KM_PANIC); 25068 25069 if (mp != NULL) { 25070 mp->b_next = mp->b_prev = NULL; 25071 mp->b_rptr = (uchar_t *)(&mp[1]); 25072 mp->b_wptr = NULL; 25073 mp->b_datap = NULL; 25074 mp->b_queue = NULL; 25075 mp->b_cont = NULL; 25076 } else if (kmflags & KM_PANIC) { 25077 /* 25078 * Failed to allocate memory for the timer. Try allocating from 25079 * dblock caches. 25080 */ 25081 /* ipclassifier calls this from a constructor - hence no tcps */ 25082 TCP_G_STAT(tcp_timermp_allocfail); 25083 mp = allocb_tryhard(sizeof (tcp_timer_t)); 25084 if (mp == NULL) { 25085 size_t size = 0; 25086 /* 25087 * Memory is really low. Try tryhard allocation. 25088 * 25089 * ipclassifier calls this from a constructor - 25090 * hence no tcps 25091 */ 25092 TCP_G_STAT(tcp_timermp_allocdblfail); 25093 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 25094 sizeof (tcp_timer_t), &size, kmflags); 25095 mp->b_rptr = (uchar_t *)(&mp[1]); 25096 mp->b_next = mp->b_prev = NULL; 25097 mp->b_wptr = (uchar_t *)-1; 25098 mp->b_datap = (dblk_t *)size; 25099 mp->b_queue = NULL; 25100 mp->b_cont = NULL; 25101 } 25102 ASSERT(mp->b_wptr != NULL); 25103 } 25104 /* ipclassifier calls this from a constructor - hence no tcps */ 25105 TCP_G_DBGSTAT(tcp_timermp_alloced); 25106 25107 return (mp); 25108 } 25109 25110 /* 25111 * Free per-tcp timer cache. 25112 * It can only contain entries from tcp_timercache. 25113 */ 25114 void 25115 tcp_timermp_free(tcp_t *tcp) 25116 { 25117 mblk_t *mp; 25118 25119 while ((mp = tcp->tcp_timercache) != NULL) { 25120 ASSERT(mp->b_wptr == NULL); 25121 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 25122 kmem_cache_free(tcp_timercache, mp); 25123 } 25124 } 25125 25126 /* 25127 * Free timer event. Put it on the per-tcp timer cache if there is not too many 25128 * events there already (currently at most two events are cached). 25129 * If the event is not allocated from the timer cache, free it right away. 25130 */ 25131 static void 25132 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 25133 { 25134 mblk_t *mp1 = tcp->tcp_timercache; 25135 25136 if (mp->b_wptr != NULL) { 25137 /* 25138 * This allocation is not from a timer cache, free it right 25139 * away. 25140 */ 25141 if (mp->b_wptr != (uchar_t *)-1) 25142 freeb(mp); 25143 else 25144 kmem_free(mp, (size_t)mp->b_datap); 25145 } else if (mp1 == NULL || mp1->b_next == NULL) { 25146 /* Cache this timer block for future allocations */ 25147 mp->b_rptr = (uchar_t *)(&mp[1]); 25148 mp->b_next = mp1; 25149 tcp->tcp_timercache = mp; 25150 } else { 25151 kmem_cache_free(tcp_timercache, mp); 25152 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 25153 } 25154 } 25155 25156 /* 25157 * End of TCP Timers implementation. 25158 */ 25159 25160 /* 25161 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 25162 * on the specified backing STREAMS q. Note, the caller may make the 25163 * decision to call based on the tcp_t.tcp_flow_stopped value which 25164 * when check outside the q's lock is only an advisory check ... 25165 */ 25166 void 25167 tcp_setqfull(tcp_t *tcp) 25168 { 25169 tcp_stack_t *tcps = tcp->tcp_tcps; 25170 conn_t *connp = tcp->tcp_connp; 25171 25172 if (tcp->tcp_closed) 25173 return; 25174 25175 if (IPCL_IS_NONSTR(connp)) { 25176 (*connp->conn_upcalls->su_txq_full) 25177 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25178 tcp->tcp_flow_stopped = B_TRUE; 25179 } else { 25180 queue_t *q = tcp->tcp_wq; 25181 25182 if (!(q->q_flag & QFULL)) { 25183 mutex_enter(QLOCK(q)); 25184 if (!(q->q_flag & QFULL)) { 25185 /* still need to set QFULL */ 25186 q->q_flag |= QFULL; 25187 tcp->tcp_flow_stopped = B_TRUE; 25188 mutex_exit(QLOCK(q)); 25189 TCP_STAT(tcps, tcp_flwctl_on); 25190 } else { 25191 mutex_exit(QLOCK(q)); 25192 } 25193 } 25194 } 25195 } 25196 25197 void 25198 tcp_clrqfull(tcp_t *tcp) 25199 { 25200 conn_t *connp = tcp->tcp_connp; 25201 25202 if (tcp->tcp_closed) 25203 return; 25204 25205 if (IPCL_IS_NONSTR(connp)) { 25206 (*connp->conn_upcalls->su_txq_full) 25207 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25208 tcp->tcp_flow_stopped = B_FALSE; 25209 } else { 25210 queue_t *q = tcp->tcp_wq; 25211 25212 if (q->q_flag & QFULL) { 25213 mutex_enter(QLOCK(q)); 25214 if (q->q_flag & QFULL) { 25215 q->q_flag &= ~QFULL; 25216 tcp->tcp_flow_stopped = B_FALSE; 25217 mutex_exit(QLOCK(q)); 25218 if (q->q_flag & QWANTW) 25219 qbackenable(q, 0); 25220 } else { 25221 mutex_exit(QLOCK(q)); 25222 } 25223 } 25224 } 25225 } 25226 25227 /* 25228 * kstats related to squeues i.e. not per IP instance 25229 */ 25230 static void * 25231 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25232 { 25233 kstat_t *ksp; 25234 25235 tcp_g_stat_t template = { 25236 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25237 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25238 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25239 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25240 }; 25241 25242 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25243 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25244 KSTAT_FLAG_VIRTUAL); 25245 25246 if (ksp == NULL) 25247 return (NULL); 25248 25249 bcopy(&template, tcp_g_statp, sizeof (template)); 25250 ksp->ks_data = (void *)tcp_g_statp; 25251 25252 kstat_install(ksp); 25253 return (ksp); 25254 } 25255 25256 static void 25257 tcp_g_kstat_fini(kstat_t *ksp) 25258 { 25259 if (ksp != NULL) { 25260 kstat_delete(ksp); 25261 } 25262 } 25263 25264 25265 static void * 25266 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25267 { 25268 kstat_t *ksp; 25269 25270 tcp_stat_t template = { 25271 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25272 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25273 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25274 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25275 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25276 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25277 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25278 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25279 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25280 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25281 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25282 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25283 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25284 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25285 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25286 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25287 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25288 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25289 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25290 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25291 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25292 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25293 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25294 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25295 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25296 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25297 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25298 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25299 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25300 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25301 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25302 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25303 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25304 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25305 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25306 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25307 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25308 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25309 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25310 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25311 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25312 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25313 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25314 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25315 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25316 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25317 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25318 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25319 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25320 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25321 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25322 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25323 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25324 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25325 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25326 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25327 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25328 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25329 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25330 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25331 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25332 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25333 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25334 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25335 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25336 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25337 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25338 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25339 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25340 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25341 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25342 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25343 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25344 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25345 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25346 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25347 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25348 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25349 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25350 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25351 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25352 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25353 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25354 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25355 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25356 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25357 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25358 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25359 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25360 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25361 }; 25362 25363 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25364 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25365 KSTAT_FLAG_VIRTUAL, stackid); 25366 25367 if (ksp == NULL) 25368 return (NULL); 25369 25370 bcopy(&template, tcps_statisticsp, sizeof (template)); 25371 ksp->ks_data = (void *)tcps_statisticsp; 25372 ksp->ks_private = (void *)(uintptr_t)stackid; 25373 25374 kstat_install(ksp); 25375 return (ksp); 25376 } 25377 25378 static void 25379 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25380 { 25381 if (ksp != NULL) { 25382 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25383 kstat_delete_netstack(ksp, stackid); 25384 } 25385 } 25386 25387 /* 25388 * TCP Kstats implementation 25389 */ 25390 static void * 25391 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25392 { 25393 kstat_t *ksp; 25394 25395 tcp_named_kstat_t template = { 25396 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25397 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25398 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25399 { "maxConn", KSTAT_DATA_INT32, 0 }, 25400 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25401 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25402 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25403 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25404 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25405 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25406 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25407 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25408 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25409 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25410 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25411 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25412 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25413 { "outAck", KSTAT_DATA_UINT32, 0 }, 25414 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25415 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25416 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25417 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25418 { "outControl", KSTAT_DATA_UINT32, 0 }, 25419 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25420 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25421 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25422 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25423 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25424 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25425 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25426 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25427 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25428 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25429 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25430 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25431 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25432 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25433 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25434 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25435 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25436 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25437 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25438 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25439 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25440 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25441 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25442 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25443 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25444 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25445 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25446 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25447 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25448 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25449 }; 25450 25451 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25452 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25453 25454 if (ksp == NULL) 25455 return (NULL); 25456 25457 template.rtoAlgorithm.value.ui32 = 4; 25458 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25459 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25460 template.maxConn.value.i32 = -1; 25461 25462 bcopy(&template, ksp->ks_data, sizeof (template)); 25463 ksp->ks_update = tcp_kstat_update; 25464 ksp->ks_private = (void *)(uintptr_t)stackid; 25465 25466 kstat_install(ksp); 25467 return (ksp); 25468 } 25469 25470 static void 25471 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25472 { 25473 if (ksp != NULL) { 25474 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25475 kstat_delete_netstack(ksp, stackid); 25476 } 25477 } 25478 25479 static int 25480 tcp_kstat_update(kstat_t *kp, int rw) 25481 { 25482 tcp_named_kstat_t *tcpkp; 25483 tcp_t *tcp; 25484 connf_t *connfp; 25485 conn_t *connp; 25486 int i; 25487 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25488 netstack_t *ns; 25489 tcp_stack_t *tcps; 25490 ip_stack_t *ipst; 25491 25492 if ((kp == NULL) || (kp->ks_data == NULL)) 25493 return (EIO); 25494 25495 if (rw == KSTAT_WRITE) 25496 return (EACCES); 25497 25498 ns = netstack_find_by_stackid(stackid); 25499 if (ns == NULL) 25500 return (-1); 25501 tcps = ns->netstack_tcp; 25502 if (tcps == NULL) { 25503 netstack_rele(ns); 25504 return (-1); 25505 } 25506 25507 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25508 25509 tcpkp->currEstab.value.ui32 = 0; 25510 25511 ipst = ns->netstack_ip; 25512 25513 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25514 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25515 connp = NULL; 25516 while ((connp = 25517 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25518 tcp = connp->conn_tcp; 25519 switch (tcp_snmp_state(tcp)) { 25520 case MIB2_TCP_established: 25521 case MIB2_TCP_closeWait: 25522 tcpkp->currEstab.value.ui32++; 25523 break; 25524 } 25525 } 25526 } 25527 25528 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25529 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25530 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25531 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25532 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25533 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25534 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25535 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25536 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25537 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25538 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25539 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25540 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25541 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25542 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25543 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25544 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25545 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25546 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25547 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25548 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25549 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25550 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25551 tcpkp->inDataInorderSegs.value.ui32 = 25552 tcps->tcps_mib.tcpInDataInorderSegs; 25553 tcpkp->inDataInorderBytes.value.ui32 = 25554 tcps->tcps_mib.tcpInDataInorderBytes; 25555 tcpkp->inDataUnorderSegs.value.ui32 = 25556 tcps->tcps_mib.tcpInDataUnorderSegs; 25557 tcpkp->inDataUnorderBytes.value.ui32 = 25558 tcps->tcps_mib.tcpInDataUnorderBytes; 25559 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25560 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25561 tcpkp->inDataPartDupSegs.value.ui32 = 25562 tcps->tcps_mib.tcpInDataPartDupSegs; 25563 tcpkp->inDataPartDupBytes.value.ui32 = 25564 tcps->tcps_mib.tcpInDataPartDupBytes; 25565 tcpkp->inDataPastWinSegs.value.ui32 = 25566 tcps->tcps_mib.tcpInDataPastWinSegs; 25567 tcpkp->inDataPastWinBytes.value.ui32 = 25568 tcps->tcps_mib.tcpInDataPastWinBytes; 25569 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25570 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25571 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25572 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25573 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25574 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25575 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25576 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25577 tcpkp->timKeepaliveProbe.value.ui32 = 25578 tcps->tcps_mib.tcpTimKeepaliveProbe; 25579 tcpkp->timKeepaliveDrop.value.ui32 = 25580 tcps->tcps_mib.tcpTimKeepaliveDrop; 25581 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25582 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25583 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25584 tcpkp->outSackRetransSegs.value.ui32 = 25585 tcps->tcps_mib.tcpOutSackRetransSegs; 25586 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25587 25588 netstack_rele(ns); 25589 return (0); 25590 } 25591 25592 void 25593 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25594 { 25595 uint16_t hdr_len; 25596 ipha_t *ipha; 25597 uint8_t *nexthdrp; 25598 tcph_t *tcph; 25599 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25600 25601 /* Already has an eager */ 25602 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25603 TCP_STAT(tcps, tcp_reinput_syn); 25604 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25605 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25606 return; 25607 } 25608 25609 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25610 case IPV4_VERSION: 25611 ipha = (ipha_t *)mp->b_rptr; 25612 hdr_len = IPH_HDR_LENGTH(ipha); 25613 break; 25614 case IPV6_VERSION: 25615 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25616 &hdr_len, &nexthdrp)) { 25617 CONN_DEC_REF(connp); 25618 freemsg(mp); 25619 return; 25620 } 25621 break; 25622 } 25623 25624 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25625 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25626 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25627 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25628 } 25629 25630 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25631 SQ_FILL, SQTAG_TCP_REINPUT); 25632 } 25633 25634 static int 25635 tcp_squeue_switch(int val) 25636 { 25637 int rval = SQ_FILL; 25638 25639 switch (val) { 25640 case 1: 25641 rval = SQ_NODRAIN; 25642 break; 25643 case 2: 25644 rval = SQ_PROCESS; 25645 break; 25646 default: 25647 break; 25648 } 25649 return (rval); 25650 } 25651 25652 /* 25653 * This is called once for each squeue - globally for all stack 25654 * instances. 25655 */ 25656 static void 25657 tcp_squeue_add(squeue_t *sqp) 25658 { 25659 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25660 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25661 25662 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25663 tcp_time_wait->tcp_time_wait_tid = 25664 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25665 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25666 CALLOUT_FLAG_ROUNDUP); 25667 if (tcp_free_list_max_cnt == 0) { 25668 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25669 max_ncpus : boot_max_ncpus); 25670 25671 /* 25672 * Limit number of entries to 1% of availble memory / tcp_ncpus 25673 */ 25674 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25675 (tcp_ncpus * sizeof (tcp_t) * 100); 25676 } 25677 tcp_time_wait->tcp_free_list_cnt = 0; 25678 } 25679 25680 static int 25681 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25682 { 25683 mblk_t *ire_mp = NULL; 25684 mblk_t *syn_mp; 25685 mblk_t *mdti; 25686 mblk_t *lsoi; 25687 int retval; 25688 tcph_t *tcph; 25689 cred_t *ecr; 25690 ts_label_t *tsl; 25691 uint32_t mss; 25692 queue_t *q = tcp->tcp_rq; 25693 conn_t *connp = tcp->tcp_connp; 25694 tcp_stack_t *tcps = tcp->tcp_tcps; 25695 25696 if (error == 0) { 25697 /* 25698 * Adapt Multidata information, if any. The 25699 * following tcp_mdt_update routine will free 25700 * the message. 25701 */ 25702 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25703 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25704 b_rptr)->mdt_capab, B_TRUE); 25705 freemsg(mdti); 25706 } 25707 25708 /* 25709 * Check to update LSO information with tcp, and 25710 * tcp_lso_update routine will free the message. 25711 */ 25712 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25713 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25714 b_rptr)->lso_capab); 25715 freemsg(lsoi); 25716 } 25717 25718 /* Get the IRE, if we had requested for it */ 25719 if (mp != NULL) 25720 ire_mp = tcp_ire_mp(&mp); 25721 25722 if (tcp->tcp_hard_binding) { 25723 tcp->tcp_hard_binding = B_FALSE; 25724 tcp->tcp_hard_bound = B_TRUE; 25725 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25726 if (retval != 0) { 25727 error = EADDRINUSE; 25728 goto bind_failed; 25729 } 25730 } else { 25731 if (ire_mp != NULL) 25732 freeb(ire_mp); 25733 goto after_syn_sent; 25734 } 25735 25736 retval = tcp_adapt_ire(tcp, ire_mp); 25737 if (ire_mp != NULL) 25738 freeb(ire_mp); 25739 if (retval == 0) { 25740 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25741 ENETUNREACH : EADDRNOTAVAIL); 25742 goto ipcl_rm; 25743 } 25744 /* 25745 * Don't let an endpoint connect to itself. 25746 * Also checked in tcp_connect() but that 25747 * check can't handle the case when the 25748 * local IP address is INADDR_ANY. 25749 */ 25750 if (tcp->tcp_ipversion == IPV4_VERSION) { 25751 if ((tcp->tcp_ipha->ipha_dst == 25752 tcp->tcp_ipha->ipha_src) && 25753 (BE16_EQL(tcp->tcp_tcph->th_lport, 25754 tcp->tcp_tcph->th_fport))) { 25755 error = EADDRNOTAVAIL; 25756 goto ipcl_rm; 25757 } 25758 } else { 25759 if (IN6_ARE_ADDR_EQUAL( 25760 &tcp->tcp_ip6h->ip6_dst, 25761 &tcp->tcp_ip6h->ip6_src) && 25762 (BE16_EQL(tcp->tcp_tcph->th_lport, 25763 tcp->tcp_tcph->th_fport))) { 25764 error = EADDRNOTAVAIL; 25765 goto ipcl_rm; 25766 } 25767 } 25768 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25769 /* 25770 * This should not be possible! Just for 25771 * defensive coding... 25772 */ 25773 if (tcp->tcp_state != TCPS_SYN_SENT) 25774 goto after_syn_sent; 25775 25776 if (is_system_labeled() && 25777 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25778 error = EHOSTUNREACH; 25779 goto ipcl_rm; 25780 } 25781 25782 /* 25783 * tcp_adapt_ire() does not adjust 25784 * for TCP/IP header length. 25785 */ 25786 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25787 25788 /* 25789 * Just make sure our rwnd is at 25790 * least tcp_recv_hiwat_mss * MSS 25791 * large, and round up to the nearest 25792 * MSS. 25793 * 25794 * We do the round up here because 25795 * we need to get the interface 25796 * MTU first before we can do the 25797 * round up. 25798 */ 25799 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25800 tcps->tcps_recv_hiwat_minmss * mss); 25801 if (!IPCL_IS_NONSTR(connp)) 25802 q->q_hiwat = tcp->tcp_rwnd; 25803 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25804 tcp_set_ws_value(tcp); 25805 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25806 tcp->tcp_tcph->th_win); 25807 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25808 tcp->tcp_snd_ws_ok = B_TRUE; 25809 25810 /* 25811 * Set tcp_snd_ts_ok to true 25812 * so that tcp_xmit_mp will 25813 * include the timestamp 25814 * option in the SYN segment. 25815 */ 25816 if (tcps->tcps_tstamp_always || 25817 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25818 tcp->tcp_snd_ts_ok = B_TRUE; 25819 } 25820 25821 /* 25822 * tcp_snd_sack_ok can be set in 25823 * tcp_adapt_ire() if the sack metric 25824 * is set. So check it here also. 25825 */ 25826 if (tcps->tcps_sack_permitted == 2 || 25827 tcp->tcp_snd_sack_ok) { 25828 if (tcp->tcp_sack_info == NULL) { 25829 tcp->tcp_sack_info = 25830 kmem_cache_alloc(tcp_sack_info_cache, 25831 KM_SLEEP); 25832 } 25833 tcp->tcp_snd_sack_ok = B_TRUE; 25834 } 25835 25836 /* 25837 * Should we use ECN? Note that the current 25838 * default value (SunOS 5.9) of tcp_ecn_permitted 25839 * is 1. The reason for doing this is that there 25840 * are equipments out there that will drop ECN 25841 * enabled IP packets. Setting it to 1 avoids 25842 * compatibility problems. 25843 */ 25844 if (tcps->tcps_ecn_permitted == 2) 25845 tcp->tcp_ecn_ok = B_TRUE; 25846 25847 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25848 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25849 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25850 if (syn_mp) { 25851 /* 25852 * cr contains the cred from the thread calling 25853 * connect(). 25854 * 25855 * If no thread cred is available, use the 25856 * socket creator's cred instead. If still no 25857 * cred, drop the request rather than risk a 25858 * panic on production systems. 25859 */ 25860 if (cr == NULL) { 25861 cr = CONN_CRED(connp); 25862 pid = tcp->tcp_cpid; 25863 ASSERT(cr != NULL); 25864 if (cr != NULL) { 25865 mblk_setcred(syn_mp, cr, pid); 25866 } else { 25867 error = ECONNABORTED; 25868 goto ipcl_rm; 25869 } 25870 25871 /* 25872 * If an effective security label exists for 25873 * the connection, create a copy of the thread's 25874 * cred but with the effective label attached. 25875 */ 25876 } else if (is_system_labeled() && 25877 connp->conn_effective_cred != NULL && 25878 (tsl = crgetlabel(connp-> 25879 conn_effective_cred)) != NULL) { 25880 if ((ecr = copycred_from_tslabel(cr, 25881 tsl, KM_NOSLEEP)) == NULL) { 25882 error = ENOMEM; 25883 goto ipcl_rm; 25884 } 25885 mblk_setcred(syn_mp, ecr, pid); 25886 crfree(ecr); 25887 25888 /* 25889 * Default to using the thread's cred unchanged. 25890 */ 25891 } else { 25892 mblk_setcred(syn_mp, cr, pid); 25893 } 25894 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25895 } 25896 after_syn_sent: 25897 if (mp != NULL) { 25898 ASSERT(mp->b_cont == NULL); 25899 freeb(mp); 25900 } 25901 return (error); 25902 } else { 25903 /* error */ 25904 if (tcp->tcp_debug) { 25905 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25906 "tcp_post_ip_bind: error == %d", error); 25907 } 25908 if (mp != NULL) { 25909 freeb(mp); 25910 } 25911 } 25912 25913 ipcl_rm: 25914 /* 25915 * Need to unbind with classifier since we were just 25916 * told that our bind succeeded. a.k.a error == 0 at the entry. 25917 */ 25918 tcp->tcp_hard_bound = B_FALSE; 25919 tcp->tcp_hard_binding = B_FALSE; 25920 25921 ipcl_hash_remove(connp); 25922 25923 bind_failed: 25924 tcp->tcp_state = TCPS_IDLE; 25925 if (tcp->tcp_ipversion == IPV4_VERSION) 25926 tcp->tcp_ipha->ipha_src = 0; 25927 else 25928 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25929 /* 25930 * Copy of the src addr. in tcp_t is needed since 25931 * the lookup funcs. can only look at tcp_t 25932 */ 25933 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25934 25935 tcph = tcp->tcp_tcph; 25936 tcph->th_lport[0] = 0; 25937 tcph->th_lport[1] = 0; 25938 tcp_bind_hash_remove(tcp); 25939 bzero(&connp->u_port, sizeof (connp->u_port)); 25940 /* blow away saved option results if any */ 25941 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25942 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25943 25944 conn_delete_ire(tcp->tcp_connp, NULL); 25945 25946 return (error); 25947 } 25948 25949 static int 25950 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25951 boolean_t bind_to_req_port_only, cred_t *cr) 25952 { 25953 in_port_t mlp_port; 25954 mlp_type_t addrtype, mlptype; 25955 boolean_t user_specified; 25956 in_port_t allocated_port; 25957 in_port_t requested_port = *requested_port_ptr; 25958 conn_t *connp; 25959 zone_t *zone; 25960 tcp_stack_t *tcps = tcp->tcp_tcps; 25961 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25962 25963 /* 25964 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25965 */ 25966 if (cr == NULL) 25967 cr = tcp->tcp_cred; 25968 /* 25969 * Get a valid port (within the anonymous range and should not 25970 * be a privileged one) to use if the user has not given a port. 25971 * If multiple threads are here, they may all start with 25972 * with the same initial port. But, it should be fine as long as 25973 * tcp_bindi will ensure that no two threads will be assigned 25974 * the same port. 25975 * 25976 * NOTE: XXX If a privileged process asks for an anonymous port, we 25977 * still check for ports only in the range > tcp_smallest_non_priv_port, 25978 * unless TCP_ANONPRIVBIND option is set. 25979 */ 25980 mlptype = mlptSingle; 25981 mlp_port = requested_port; 25982 if (requested_port == 0) { 25983 requested_port = tcp->tcp_anon_priv_bind ? 25984 tcp_get_next_priv_port(tcp) : 25985 tcp_update_next_port(tcps->tcps_next_port_to_try, 25986 tcp, B_TRUE); 25987 if (requested_port == 0) { 25988 return (-TNOADDR); 25989 } 25990 user_specified = B_FALSE; 25991 25992 /* 25993 * If the user went through one of the RPC interfaces to create 25994 * this socket and RPC is MLP in this zone, then give him an 25995 * anonymous MLP. 25996 */ 25997 connp = tcp->tcp_connp; 25998 if (connp->conn_anon_mlp && is_system_labeled()) { 25999 zone = crgetzone(cr); 26000 addrtype = tsol_mlp_addr_type(zone->zone_id, 26001 IPV6_VERSION, &v6addr, 26002 tcps->tcps_netstack->netstack_ip); 26003 if (addrtype == mlptSingle) { 26004 return (-TNOADDR); 26005 } 26006 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26007 PMAPPORT, addrtype); 26008 mlp_port = PMAPPORT; 26009 } 26010 } else { 26011 int i; 26012 boolean_t priv = B_FALSE; 26013 26014 /* 26015 * If the requested_port is in the well-known privileged range, 26016 * verify that the stream was opened by a privileged user. 26017 * Note: No locks are held when inspecting tcp_g_*epriv_ports 26018 * but instead the code relies on: 26019 * - the fact that the address of the array and its size never 26020 * changes 26021 * - the atomic assignment of the elements of the array 26022 */ 26023 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 26024 priv = B_TRUE; 26025 } else { 26026 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 26027 if (requested_port == 26028 tcps->tcps_g_epriv_ports[i]) { 26029 priv = B_TRUE; 26030 break; 26031 } 26032 } 26033 } 26034 if (priv) { 26035 if (secpolicy_net_privaddr(cr, requested_port, 26036 IPPROTO_TCP) != 0) { 26037 if (tcp->tcp_debug) { 26038 (void) strlog(TCP_MOD_ID, 0, 1, 26039 SL_ERROR|SL_TRACE, 26040 "tcp_bind: no priv for port %d", 26041 requested_port); 26042 } 26043 return (-TACCES); 26044 } 26045 } 26046 user_specified = B_TRUE; 26047 26048 connp = tcp->tcp_connp; 26049 if (is_system_labeled()) { 26050 zone = crgetzone(cr); 26051 addrtype = tsol_mlp_addr_type(zone->zone_id, 26052 IPV6_VERSION, &v6addr, 26053 tcps->tcps_netstack->netstack_ip); 26054 if (addrtype == mlptSingle) { 26055 return (-TNOADDR); 26056 } 26057 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 26058 requested_port, addrtype); 26059 } 26060 } 26061 26062 if (mlptype != mlptSingle) { 26063 if (secpolicy_net_bindmlp(cr) != 0) { 26064 if (tcp->tcp_debug) { 26065 (void) strlog(TCP_MOD_ID, 0, 1, 26066 SL_ERROR|SL_TRACE, 26067 "tcp_bind: no priv for multilevel port %d", 26068 requested_port); 26069 } 26070 return (-TACCES); 26071 } 26072 26073 /* 26074 * If we're specifically binding a shared IP address and the 26075 * port is MLP on shared addresses, then check to see if this 26076 * zone actually owns the MLP. Reject if not. 26077 */ 26078 if (mlptype == mlptShared && addrtype == mlptShared) { 26079 /* 26080 * No need to handle exclusive-stack zones since 26081 * ALL_ZONES only applies to the shared stack. 26082 */ 26083 zoneid_t mlpzone; 26084 26085 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 26086 htons(mlp_port)); 26087 if (connp->conn_zoneid != mlpzone) { 26088 if (tcp->tcp_debug) { 26089 (void) strlog(TCP_MOD_ID, 0, 1, 26090 SL_ERROR|SL_TRACE, 26091 "tcp_bind: attempt to bind port " 26092 "%d on shared addr in zone %d " 26093 "(should be %d)", 26094 mlp_port, connp->conn_zoneid, 26095 mlpzone); 26096 } 26097 return (-TACCES); 26098 } 26099 } 26100 26101 if (!user_specified) { 26102 int err; 26103 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26104 requested_port, B_TRUE); 26105 if (err != 0) { 26106 if (tcp->tcp_debug) { 26107 (void) strlog(TCP_MOD_ID, 0, 1, 26108 SL_ERROR|SL_TRACE, 26109 "tcp_bind: cannot establish anon " 26110 "MLP for port %d", 26111 requested_port); 26112 } 26113 return (err); 26114 } 26115 connp->conn_anon_port = B_TRUE; 26116 } 26117 connp->conn_mlp_type = mlptype; 26118 } 26119 26120 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 26121 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 26122 26123 if (allocated_port == 0) { 26124 connp->conn_mlp_type = mlptSingle; 26125 if (connp->conn_anon_port) { 26126 connp->conn_anon_port = B_FALSE; 26127 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 26128 requested_port, B_FALSE); 26129 } 26130 if (bind_to_req_port_only) { 26131 if (tcp->tcp_debug) { 26132 (void) strlog(TCP_MOD_ID, 0, 1, 26133 SL_ERROR|SL_TRACE, 26134 "tcp_bind: requested addr busy"); 26135 } 26136 return (-TADDRBUSY); 26137 } else { 26138 /* If we are out of ports, fail the bind. */ 26139 if (tcp->tcp_debug) { 26140 (void) strlog(TCP_MOD_ID, 0, 1, 26141 SL_ERROR|SL_TRACE, 26142 "tcp_bind: out of ports?"); 26143 } 26144 return (-TNOADDR); 26145 } 26146 } 26147 26148 /* Pass the allocated port back */ 26149 *requested_port_ptr = allocated_port; 26150 return (0); 26151 } 26152 26153 static int 26154 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26155 boolean_t bind_to_req_port_only) 26156 { 26157 tcp_t *tcp = connp->conn_tcp; 26158 sin_t *sin; 26159 sin6_t *sin6; 26160 in_port_t requested_port; 26161 ipaddr_t v4addr; 26162 in6_addr_t v6addr; 26163 uint_t origipversion; 26164 int error = 0; 26165 26166 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 26167 26168 if (tcp->tcp_state == TCPS_BOUND) { 26169 return (0); 26170 } else if (tcp->tcp_state > TCPS_BOUND) { 26171 if (tcp->tcp_debug) { 26172 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26173 "tcp_bind: bad state, %d", tcp->tcp_state); 26174 } 26175 return (-TOUTSTATE); 26176 } 26177 origipversion = tcp->tcp_ipversion; 26178 26179 ASSERT(sa != NULL && len != 0); 26180 26181 if (!OK_32PTR((char *)sa)) { 26182 if (tcp->tcp_debug) { 26183 (void) strlog(TCP_MOD_ID, 0, 1, 26184 SL_ERROR|SL_TRACE, 26185 "tcp_bind: bad address parameter, " 26186 "address %p, len %d", 26187 (void *)sa, len); 26188 } 26189 return (-TPROTO); 26190 } 26191 26192 switch (len) { 26193 case sizeof (sin_t): /* Complete IPv4 address */ 26194 sin = (sin_t *)sa; 26195 /* 26196 * With sockets sockfs will accept bogus sin_family in 26197 * bind() and replace it with the family used in the socket 26198 * call. 26199 */ 26200 if (sin->sin_family != AF_INET || 26201 tcp->tcp_family != AF_INET) { 26202 return (EAFNOSUPPORT); 26203 } 26204 requested_port = ntohs(sin->sin_port); 26205 tcp->tcp_ipversion = IPV4_VERSION; 26206 v4addr = sin->sin_addr.s_addr; 26207 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 26208 break; 26209 26210 case sizeof (sin6_t): /* Complete IPv6 address */ 26211 sin6 = (sin6_t *)sa; 26212 if (sin6->sin6_family != AF_INET6 || 26213 tcp->tcp_family != AF_INET6) { 26214 return (EAFNOSUPPORT); 26215 } 26216 requested_port = ntohs(sin6->sin6_port); 26217 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26218 IPV4_VERSION : IPV6_VERSION; 26219 v6addr = sin6->sin6_addr; 26220 break; 26221 26222 default: 26223 if (tcp->tcp_debug) { 26224 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26225 "tcp_bind: bad address length, %d", len); 26226 } 26227 return (EAFNOSUPPORT); 26228 /* return (-TBADADDR); */ 26229 } 26230 26231 tcp->tcp_bound_source_v6 = v6addr; 26232 26233 /* Check for change in ipversion */ 26234 if (origipversion != tcp->tcp_ipversion) { 26235 ASSERT(tcp->tcp_family == AF_INET6); 26236 error = tcp->tcp_ipversion == IPV6_VERSION ? 26237 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26238 if (error) { 26239 return (ENOMEM); 26240 } 26241 } 26242 26243 /* 26244 * Initialize family specific fields. Copy of the src addr. 26245 * in tcp_t is needed for the lookup funcs. 26246 */ 26247 if (tcp->tcp_ipversion == IPV6_VERSION) { 26248 tcp->tcp_ip6h->ip6_src = v6addr; 26249 } else { 26250 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26251 } 26252 tcp->tcp_ip_src_v6 = v6addr; 26253 26254 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26255 26256 error = tcp_bind_select_lport(tcp, &requested_port, 26257 bind_to_req_port_only, cr); 26258 26259 return (error); 26260 } 26261 26262 /* 26263 * Return unix error is tli error is TSYSERR, otherwise return a negative 26264 * tli error. 26265 */ 26266 int 26267 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26268 boolean_t bind_to_req_port_only) 26269 { 26270 int error; 26271 tcp_t *tcp = connp->conn_tcp; 26272 26273 if (tcp->tcp_state >= TCPS_BOUND) { 26274 if (tcp->tcp_debug) { 26275 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26276 "tcp_bind: bad state, %d", tcp->tcp_state); 26277 } 26278 return (-TOUTSTATE); 26279 } 26280 26281 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26282 if (error != 0) 26283 return (error); 26284 26285 ASSERT(tcp->tcp_state == TCPS_BOUND); 26286 26287 tcp->tcp_conn_req_max = 0; 26288 26289 if (tcp->tcp_family == AF_INET6) { 26290 ASSERT(tcp->tcp_connp->conn_af_isv6); 26291 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26292 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26293 } else { 26294 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26295 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26296 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26297 } 26298 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26299 } 26300 26301 int 26302 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26303 socklen_t len, cred_t *cr) 26304 { 26305 int error; 26306 conn_t *connp = (conn_t *)proto_handle; 26307 squeue_t *sqp = connp->conn_sqp; 26308 26309 /* All Solaris components should pass a cred for this operation. */ 26310 ASSERT(cr != NULL); 26311 26312 ASSERT(sqp != NULL); 26313 ASSERT(connp->conn_upper_handle != NULL); 26314 26315 error = squeue_synch_enter(sqp, connp, NULL); 26316 if (error != 0) { 26317 /* failed to enter */ 26318 return (ENOSR); 26319 } 26320 26321 /* binding to a NULL address really means unbind */ 26322 if (sa == NULL) { 26323 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26324 error = tcp_do_unbind(connp); 26325 else 26326 error = EINVAL; 26327 } else { 26328 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26329 } 26330 26331 squeue_synch_exit(sqp, connp); 26332 26333 if (error < 0) { 26334 if (error == -TOUTSTATE) 26335 error = EINVAL; 26336 else 26337 error = proto_tlitosyserr(-error); 26338 } 26339 26340 return (error); 26341 } 26342 26343 /* 26344 * If the return value from this function is positive, it's a UNIX error. 26345 * Otherwise, if it's negative, then the absolute value is a TLI error. 26346 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26347 */ 26348 int 26349 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26350 cred_t *cr, pid_t pid) 26351 { 26352 tcp_t *tcp = connp->conn_tcp; 26353 sin_t *sin = (sin_t *)sa; 26354 sin6_t *sin6 = (sin6_t *)sa; 26355 ipaddr_t *dstaddrp; 26356 in_port_t dstport; 26357 uint_t srcid; 26358 int error = 0; 26359 26360 switch (len) { 26361 default: 26362 /* 26363 * Should never happen 26364 */ 26365 return (EINVAL); 26366 26367 case sizeof (sin_t): 26368 sin = (sin_t *)sa; 26369 if (sin->sin_port == 0) { 26370 return (-TBADADDR); 26371 } 26372 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26373 return (EAFNOSUPPORT); 26374 } 26375 break; 26376 26377 case sizeof (sin6_t): 26378 sin6 = (sin6_t *)sa; 26379 if (sin6->sin6_port == 0) { 26380 return (-TBADADDR); 26381 } 26382 break; 26383 } 26384 /* 26385 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26386 * make sure that the template IP header in the tcp structure is an 26387 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26388 * need to this before we call tcp_bindi() so that the port lookup 26389 * code will look for ports in the correct port space (IPv4 and 26390 * IPv6 have separate port spaces). 26391 */ 26392 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26393 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26394 int err = 0; 26395 26396 err = tcp_header_init_ipv4(tcp); 26397 if (err != 0) { 26398 error = ENOMEM; 26399 goto connect_failed; 26400 } 26401 if (tcp->tcp_lport != 0) 26402 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26403 } 26404 26405 switch (tcp->tcp_state) { 26406 case TCPS_LISTEN: 26407 /* 26408 * Listening sockets are not allowed to issue connect(). 26409 */ 26410 if (IPCL_IS_NONSTR(connp)) 26411 return (EOPNOTSUPP); 26412 /* FALLTHRU */ 26413 case TCPS_IDLE: 26414 /* 26415 * We support quick connect, refer to comments in 26416 * tcp_connect_*() 26417 */ 26418 /* FALLTHRU */ 26419 case TCPS_BOUND: 26420 /* 26421 * We must bump the generation before the operation start. 26422 * This is done to ensure that any upcall made later on sends 26423 * up the right generation to the socket. 26424 */ 26425 SOCK_CONNID_BUMP(tcp->tcp_connid); 26426 26427 if (tcp->tcp_family == AF_INET6) { 26428 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26429 return (tcp_connect_ipv6(tcp, 26430 &sin6->sin6_addr, 26431 sin6->sin6_port, sin6->sin6_flowinfo, 26432 sin6->__sin6_src_id, sin6->sin6_scope_id, 26433 cr, pid)); 26434 } 26435 /* 26436 * Destination adress is mapped IPv6 address. 26437 * Source bound address should be unspecified or 26438 * IPv6 mapped address as well. 26439 */ 26440 if (!IN6_IS_ADDR_UNSPECIFIED( 26441 &tcp->tcp_bound_source_v6) && 26442 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26443 return (EADDRNOTAVAIL); 26444 } 26445 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26446 dstport = sin6->sin6_port; 26447 srcid = sin6->__sin6_src_id; 26448 } else { 26449 dstaddrp = &sin->sin_addr.s_addr; 26450 dstport = sin->sin_port; 26451 srcid = 0; 26452 } 26453 26454 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26455 pid); 26456 break; 26457 default: 26458 return (-TOUTSTATE); 26459 } 26460 /* 26461 * Note: Code below is the "failure" case 26462 */ 26463 connect_failed: 26464 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26465 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26466 return (error); 26467 } 26468 26469 int 26470 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26471 socklen_t len, sock_connid_t *id, cred_t *cr) 26472 { 26473 conn_t *connp = (conn_t *)proto_handle; 26474 tcp_t *tcp = connp->conn_tcp; 26475 squeue_t *sqp = connp->conn_sqp; 26476 int error; 26477 26478 ASSERT(connp->conn_upper_handle != NULL); 26479 26480 /* All Solaris components should pass a cred for this operation. */ 26481 ASSERT(cr != NULL); 26482 26483 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26484 if (error != 0) { 26485 return (error); 26486 } 26487 26488 error = squeue_synch_enter(sqp, connp, NULL); 26489 if (error != 0) { 26490 /* failed to enter */ 26491 return (ENOSR); 26492 } 26493 26494 /* 26495 * TCP supports quick connect, so no need to do an implicit bind 26496 */ 26497 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26498 if (error == 0) { 26499 *id = connp->conn_tcp->tcp_connid; 26500 } else if (error < 0) { 26501 if (error == -TOUTSTATE) { 26502 switch (connp->conn_tcp->tcp_state) { 26503 case TCPS_SYN_SENT: 26504 error = EALREADY; 26505 break; 26506 case TCPS_ESTABLISHED: 26507 error = EISCONN; 26508 break; 26509 case TCPS_LISTEN: 26510 error = EOPNOTSUPP; 26511 break; 26512 default: 26513 error = EINVAL; 26514 break; 26515 } 26516 } else { 26517 error = proto_tlitosyserr(-error); 26518 } 26519 } 26520 done: 26521 squeue_synch_exit(sqp, connp); 26522 26523 return ((error == 0) ? EINPROGRESS : error); 26524 } 26525 26526 /* ARGSUSED */ 26527 sock_lower_handle_t 26528 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26529 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26530 { 26531 conn_t *connp; 26532 boolean_t isv6 = family == AF_INET6; 26533 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26534 (proto != 0 && proto != IPPROTO_TCP)) { 26535 *errorp = EPROTONOSUPPORT; 26536 return (NULL); 26537 } 26538 26539 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26540 if (connp == NULL) { 26541 return (NULL); 26542 } 26543 26544 /* 26545 * Put the ref for TCP. Ref for IP was already put 26546 * by ipcl_conn_create. Also Make the conn_t globally 26547 * visible to walkers 26548 */ 26549 mutex_enter(&connp->conn_lock); 26550 CONN_INC_REF_LOCKED(connp); 26551 ASSERT(connp->conn_ref == 2); 26552 connp->conn_state_flags &= ~CONN_INCIPIENT; 26553 26554 connp->conn_flags |= IPCL_NONSTR; 26555 mutex_exit(&connp->conn_lock); 26556 26557 ASSERT(errorp != NULL); 26558 *errorp = 0; 26559 *sock_downcalls = &sock_tcp_downcalls; 26560 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26561 SM_SENDFILESUPP; 26562 26563 return ((sock_lower_handle_t)connp); 26564 } 26565 26566 /* ARGSUSED */ 26567 void 26568 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26569 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26570 { 26571 conn_t *connp = (conn_t *)proto_handle; 26572 struct sock_proto_props sopp; 26573 26574 ASSERT(connp->conn_upper_handle == NULL); 26575 26576 /* All Solaris components should pass a cred for this operation. */ 26577 ASSERT(cr != NULL); 26578 26579 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26580 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26581 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26582 26583 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26584 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26585 sopp.sopp_maxpsz = INFPSZ; 26586 sopp.sopp_maxblk = INFPSZ; 26587 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26588 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26589 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26590 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26591 tcp_rinfo.mi_minpsz; 26592 26593 connp->conn_upcalls = sock_upcalls; 26594 connp->conn_upper_handle = sock_handle; 26595 26596 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26597 } 26598 26599 /* ARGSUSED */ 26600 int 26601 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26602 { 26603 conn_t *connp = (conn_t *)proto_handle; 26604 26605 ASSERT(connp->conn_upper_handle != NULL); 26606 26607 /* All Solaris components should pass a cred for this operation. */ 26608 ASSERT(cr != NULL); 26609 26610 tcp_close_common(connp, flags); 26611 26612 ip_free_helper_stream(connp); 26613 26614 /* 26615 * Drop IP's reference on the conn. This is the last reference 26616 * on the connp if the state was less than established. If the 26617 * connection has gone into timewait state, then we will have 26618 * one ref for the TCP and one more ref (total of two) for the 26619 * classifier connected hash list (a timewait connections stays 26620 * in connected hash till closed). 26621 * 26622 * We can't assert the references because there might be other 26623 * transient reference places because of some walkers or queued 26624 * packets in squeue for the timewait state. 26625 */ 26626 CONN_DEC_REF(connp); 26627 return (0); 26628 } 26629 26630 /* ARGSUSED */ 26631 int 26632 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26633 cred_t *cr) 26634 { 26635 tcp_t *tcp; 26636 uint32_t msize; 26637 conn_t *connp = (conn_t *)proto_handle; 26638 int32_t tcpstate; 26639 26640 /* All Solaris components should pass a cred for this operation. */ 26641 ASSERT(cr != NULL); 26642 26643 ASSERT(connp->conn_ref >= 2); 26644 ASSERT(connp->conn_upper_handle != NULL); 26645 26646 if (msg->msg_controllen != 0) { 26647 return (EOPNOTSUPP); 26648 26649 } 26650 switch (DB_TYPE(mp)) { 26651 case M_DATA: 26652 tcp = connp->conn_tcp; 26653 ASSERT(tcp != NULL); 26654 26655 tcpstate = tcp->tcp_state; 26656 if (tcpstate < TCPS_ESTABLISHED) { 26657 freemsg(mp); 26658 return (ENOTCONN); 26659 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26660 freemsg(mp); 26661 return (EPIPE); 26662 } 26663 26664 msize = msgdsize(mp); 26665 26666 mutex_enter(&tcp->tcp_non_sq_lock); 26667 tcp->tcp_squeue_bytes += msize; 26668 /* 26669 * Squeue Flow Control 26670 */ 26671 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26672 tcp_setqfull(tcp); 26673 } 26674 mutex_exit(&tcp->tcp_non_sq_lock); 26675 26676 /* 26677 * The application may pass in an address in the msghdr, but 26678 * we ignore the address on connection-oriented sockets. 26679 * Just like BSD this code does not generate an error for 26680 * TCP (a CONNREQUIRED socket) when sending to an address 26681 * passed in with sendto/sendmsg. Instead the data is 26682 * delivered on the connection as if no address had been 26683 * supplied. 26684 */ 26685 CONN_INC_REF(connp); 26686 26687 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26688 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26689 tcp_output_urgent, connp, tcp_squeue_flag, 26690 SQTAG_TCP_OUTPUT); 26691 } else { 26692 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26693 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26694 } 26695 26696 return (0); 26697 26698 default: 26699 ASSERT(0); 26700 } 26701 26702 freemsg(mp); 26703 return (0); 26704 } 26705 26706 /* ARGSUSED */ 26707 void 26708 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26709 { 26710 int len; 26711 uint32_t msize; 26712 conn_t *connp = (conn_t *)arg; 26713 tcp_t *tcp = connp->conn_tcp; 26714 26715 msize = msgdsize(mp); 26716 26717 len = msize - 1; 26718 if (len < 0) { 26719 freemsg(mp); 26720 return; 26721 } 26722 26723 /* 26724 * Try to force urgent data out on the wire. 26725 * Even if we have unsent data this will 26726 * at least send the urgent flag. 26727 * XXX does not handle more flag correctly. 26728 */ 26729 len += tcp->tcp_unsent; 26730 len += tcp->tcp_snxt; 26731 tcp->tcp_urg = len; 26732 tcp->tcp_valid_bits |= TCP_URG_VALID; 26733 26734 /* Bypass tcp protocol for fused tcp loopback */ 26735 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26736 return; 26737 tcp_wput_data(tcp, mp, B_TRUE); 26738 } 26739 26740 /* ARGSUSED */ 26741 int 26742 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26743 socklen_t *addrlenp, cred_t *cr) 26744 { 26745 conn_t *connp = (conn_t *)proto_handle; 26746 tcp_t *tcp = connp->conn_tcp; 26747 26748 ASSERT(connp->conn_upper_handle != NULL); 26749 /* All Solaris components should pass a cred for this operation. */ 26750 ASSERT(cr != NULL); 26751 26752 ASSERT(tcp != NULL); 26753 26754 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26755 } 26756 26757 /* ARGSUSED */ 26758 int 26759 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26760 socklen_t *addrlenp, cred_t *cr) 26761 { 26762 conn_t *connp = (conn_t *)proto_handle; 26763 tcp_t *tcp = connp->conn_tcp; 26764 26765 /* All Solaris components should pass a cred for this operation. */ 26766 ASSERT(cr != NULL); 26767 26768 ASSERT(connp->conn_upper_handle != NULL); 26769 26770 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26771 } 26772 26773 /* 26774 * tcp_fallback 26775 * 26776 * A direct socket is falling back to using STREAMS. The queue 26777 * that is being passed down was created using tcp_open() with 26778 * the SO_FALLBACK flag set. As a result, the queue is not 26779 * associated with a conn, and the q_ptrs instead contain the 26780 * dev and minor area that should be used. 26781 * 26782 * The 'direct_sockfs' flag indicates whether the FireEngine 26783 * optimizations should be used. The common case would be that 26784 * optimizations are enabled, and they might be subsequently 26785 * disabled using the _SIOCSOCKFALLBACK ioctl. 26786 */ 26787 26788 /* 26789 * An active connection is falling back to TPI. Gather all the information 26790 * required by the STREAM head and TPI sonode and send it up. 26791 */ 26792 void 26793 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26794 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26795 { 26796 conn_t *connp = tcp->tcp_connp; 26797 struct stroptions *stropt; 26798 struct T_capability_ack tca; 26799 struct sockaddr_in6 laddr, faddr; 26800 socklen_t laddrlen, faddrlen; 26801 short opts; 26802 int error; 26803 mblk_t *mp; 26804 26805 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26806 connp->conn_minor_arena = WR(q)->q_ptr; 26807 26808 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26809 26810 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26811 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26812 26813 WR(q)->q_qinfo = &tcp_sock_winit; 26814 26815 if (!direct_sockfs) 26816 tcp_disable_direct_sockfs(tcp); 26817 26818 /* 26819 * free the helper stream 26820 */ 26821 ip_free_helper_stream(connp); 26822 26823 /* 26824 * Notify the STREAM head about options 26825 */ 26826 DB_TYPE(stropt_mp) = M_SETOPTS; 26827 stropt = (struct stroptions *)stropt_mp->b_rptr; 26828 stropt_mp->b_wptr += sizeof (struct stroptions); 26829 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 26830 26831 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26832 tcp->tcp_tcps->tcps_wroff_xtra); 26833 if (tcp->tcp_snd_sack_ok) 26834 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26835 stropt->so_hiwat = tcp->tcp_fused ? 26836 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26837 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26838 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26839 26840 putnext(RD(q), stropt_mp); 26841 26842 /* 26843 * Collect the information needed to sync with the sonode 26844 */ 26845 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26846 26847 laddrlen = faddrlen = sizeof (sin6_t); 26848 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26849 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26850 if (error != 0) 26851 faddrlen = 0; 26852 26853 opts = 0; 26854 if (tcp->tcp_oobinline) 26855 opts |= SO_OOBINLINE; 26856 if (tcp->tcp_dontroute) 26857 opts |= SO_DONTROUTE; 26858 26859 /* 26860 * Notify the socket that the protocol is now quiescent, 26861 * and it's therefore safe move data from the socket 26862 * to the stream head. 26863 */ 26864 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26865 (struct sockaddr *)&laddr, laddrlen, 26866 (struct sockaddr *)&faddr, faddrlen, opts); 26867 26868 while ((mp = tcp->tcp_rcv_list) != NULL) { 26869 tcp->tcp_rcv_list = mp->b_next; 26870 mp->b_next = NULL; 26871 putnext(q, mp); 26872 } 26873 tcp->tcp_rcv_last_head = NULL; 26874 tcp->tcp_rcv_last_tail = NULL; 26875 tcp->tcp_rcv_cnt = 0; 26876 } 26877 26878 /* 26879 * An eager is falling back to TPI. All we have to do is send 26880 * up a T_CONN_IND. 26881 */ 26882 void 26883 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26884 { 26885 tcp_t *listener = eager->tcp_listener; 26886 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26887 26888 ASSERT(listener != NULL); 26889 ASSERT(mp != NULL); 26890 26891 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26892 26893 /* 26894 * TLI/XTI applications will get confused by 26895 * sending eager as an option since it violates 26896 * the option semantics. So remove the eager as 26897 * option since TLI/XTI app doesn't need it anyway. 26898 */ 26899 if (!direct_sockfs) { 26900 struct T_conn_ind *conn_ind; 26901 26902 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26903 conn_ind->OPT_length = 0; 26904 conn_ind->OPT_offset = 0; 26905 } 26906 26907 /* 26908 * Sockfs guarantees that the listener will not be closed 26909 * during fallback. So we can safely use the listener's queue. 26910 */ 26911 putnext(listener->tcp_rq, mp); 26912 } 26913 26914 int 26915 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26916 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26917 { 26918 tcp_t *tcp; 26919 conn_t *connp = (conn_t *)proto_handle; 26920 int error; 26921 mblk_t *stropt_mp; 26922 mblk_t *ordrel_mp; 26923 mblk_t *fused_sigurp_mp; 26924 26925 tcp = connp->conn_tcp; 26926 26927 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26928 NULL); 26929 26930 /* Pre-allocate the T_ordrel_ind mblk. */ 26931 ASSERT(tcp->tcp_ordrel_mp == NULL); 26932 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26933 STR_NOSIG, NULL); 26934 ordrel_mp->b_datap->db_type = M_PROTO; 26935 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26936 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26937 26938 /* Pre-allocate the M_PCSIG used by fusion */ 26939 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 26940 26941 /* 26942 * Enter the squeue so that no new packets can come in 26943 */ 26944 error = squeue_synch_enter(connp->conn_sqp, connp, NULL); 26945 if (error != 0) { 26946 /* failed to enter, free all the pre-allocated messages. */ 26947 freeb(stropt_mp); 26948 freeb(ordrel_mp); 26949 freeb(fused_sigurp_mp); 26950 /* 26951 * We cannot process the eager, so at least send out a 26952 * RST so the peer can reconnect. 26953 */ 26954 if (tcp->tcp_listener != NULL) { 26955 (void) tcp_eager_blowoff(tcp->tcp_listener, 26956 tcp->tcp_conn_req_seqnum); 26957 } 26958 return (ENOMEM); 26959 } 26960 26961 /* 26962 * No longer a direct socket 26963 */ 26964 connp->conn_flags &= ~IPCL_NONSTR; 26965 26966 tcp->tcp_ordrel_mp = ordrel_mp; 26967 26968 if (tcp->tcp_fused) { 26969 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 26970 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 26971 } else { 26972 freeb(fused_sigurp_mp); 26973 } 26974 26975 if (tcp->tcp_listener != NULL) { 26976 /* The eager will deal with opts when accept() is called */ 26977 freeb(stropt_mp); 26978 tcp_fallback_eager(tcp, direct_sockfs); 26979 } else { 26980 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26981 quiesced_cb); 26982 } 26983 26984 /* 26985 * There should be atleast two ref's (IP + TCP) 26986 */ 26987 ASSERT(connp->conn_ref >= 2); 26988 squeue_synch_exit(connp->conn_sqp, connp); 26989 26990 return (0); 26991 } 26992 26993 /* ARGSUSED */ 26994 static void 26995 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26996 { 26997 conn_t *connp = (conn_t *)arg; 26998 tcp_t *tcp = connp->conn_tcp; 26999 27000 freemsg(mp); 27001 27002 if (tcp->tcp_fused) 27003 tcp_unfuse(tcp); 27004 27005 if (tcp_xmit_end(tcp) != 0) { 27006 /* 27007 * We were crossing FINs and got a reset from 27008 * the other side. Just ignore it. 27009 */ 27010 if (tcp->tcp_debug) { 27011 (void) strlog(TCP_MOD_ID, 0, 1, 27012 SL_ERROR|SL_TRACE, 27013 "tcp_shutdown_output() out of state %s", 27014 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 27015 } 27016 } 27017 } 27018 27019 /* ARGSUSED */ 27020 int 27021 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 27022 { 27023 conn_t *connp = (conn_t *)proto_handle; 27024 tcp_t *tcp = connp->conn_tcp; 27025 27026 ASSERT(connp->conn_upper_handle != NULL); 27027 27028 /* All Solaris components should pass a cred for this operation. */ 27029 ASSERT(cr != NULL); 27030 27031 /* 27032 * X/Open requires that we check the connected state. 27033 */ 27034 if (tcp->tcp_state < TCPS_SYN_SENT) 27035 return (ENOTCONN); 27036 27037 /* shutdown the send side */ 27038 if (how != SHUT_RD) { 27039 mblk_t *bp; 27040 27041 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 27042 CONN_INC_REF(connp); 27043 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 27044 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 27045 27046 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27047 SOCK_OPCTL_SHUT_SEND, 0); 27048 } 27049 27050 /* shutdown the recv side */ 27051 if (how != SHUT_WR) 27052 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27053 SOCK_OPCTL_SHUT_RECV, 0); 27054 27055 return (0); 27056 } 27057 27058 /* 27059 * SOP_LISTEN() calls into tcp_listen(). 27060 */ 27061 /* ARGSUSED */ 27062 int 27063 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 27064 { 27065 conn_t *connp = (conn_t *)proto_handle; 27066 int error; 27067 squeue_t *sqp = connp->conn_sqp; 27068 27069 ASSERT(connp->conn_upper_handle != NULL); 27070 27071 /* All Solaris components should pass a cred for this operation. */ 27072 ASSERT(cr != NULL); 27073 27074 error = squeue_synch_enter(sqp, connp, NULL); 27075 if (error != 0) { 27076 /* failed to enter */ 27077 return (ENOBUFS); 27078 } 27079 27080 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 27081 if (error == 0) { 27082 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 27083 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 27084 } else if (error < 0) { 27085 if (error == -TOUTSTATE) 27086 error = EINVAL; 27087 else 27088 error = proto_tlitosyserr(-error); 27089 } 27090 squeue_synch_exit(sqp, connp); 27091 return (error); 27092 } 27093 27094 static int 27095 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 27096 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 27097 { 27098 tcp_t *tcp = connp->conn_tcp; 27099 int error = 0; 27100 tcp_stack_t *tcps = tcp->tcp_tcps; 27101 27102 /* All Solaris components should pass a cred for this operation. */ 27103 ASSERT(cr != NULL); 27104 27105 if (tcp->tcp_state >= TCPS_BOUND) { 27106 if ((tcp->tcp_state == TCPS_BOUND || 27107 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 27108 /* 27109 * Handle listen() increasing backlog. 27110 * This is more "liberal" then what the TPI spec 27111 * requires but is needed to avoid a t_unbind 27112 * when handling listen() since the port number 27113 * might be "stolen" between the unbind and bind. 27114 */ 27115 goto do_listen; 27116 } 27117 if (tcp->tcp_debug) { 27118 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 27119 "tcp_listen: bad state, %d", tcp->tcp_state); 27120 } 27121 return (-TOUTSTATE); 27122 } else { 27123 if (sa == NULL) { 27124 sin6_t addr; 27125 sin_t *sin; 27126 sin6_t *sin6; 27127 27128 ASSERT(IPCL_IS_NONSTR(connp)); 27129 27130 /* Do an implicit bind: Request for a generic port. */ 27131 if (tcp->tcp_family == AF_INET) { 27132 len = sizeof (sin_t); 27133 sin = (sin_t *)&addr; 27134 *sin = sin_null; 27135 sin->sin_family = AF_INET; 27136 tcp->tcp_ipversion = IPV4_VERSION; 27137 } else { 27138 ASSERT(tcp->tcp_family == AF_INET6); 27139 len = sizeof (sin6_t); 27140 sin6 = (sin6_t *)&addr; 27141 *sin6 = sin6_null; 27142 sin6->sin6_family = AF_INET6; 27143 tcp->tcp_ipversion = IPV6_VERSION; 27144 } 27145 sa = (struct sockaddr *)&addr; 27146 } 27147 27148 error = tcp_bind_check(connp, sa, len, cr, 27149 bind_to_req_port_only); 27150 if (error) 27151 return (error); 27152 /* Fall through and do the fanout insertion */ 27153 } 27154 27155 do_listen: 27156 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 27157 tcp->tcp_conn_req_max = backlog; 27158 if (tcp->tcp_conn_req_max) { 27159 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 27160 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 27161 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 27162 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 27163 /* 27164 * If this is a listener, do not reset the eager list 27165 * and other stuffs. Note that we don't check if the 27166 * existing eager list meets the new tcp_conn_req_max 27167 * requirement. 27168 */ 27169 if (tcp->tcp_state != TCPS_LISTEN) { 27170 tcp->tcp_state = TCPS_LISTEN; 27171 /* Initialize the chain. Don't need the eager_lock */ 27172 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 27173 tcp->tcp_eager_next_drop_q0 = tcp; 27174 tcp->tcp_eager_prev_drop_q0 = tcp; 27175 tcp->tcp_second_ctimer_threshold = 27176 tcps->tcps_ip_abort_linterval; 27177 } 27178 } 27179 27180 /* 27181 * We can call ip_bind directly, the processing continues 27182 * in tcp_post_ip_bind(). 27183 * 27184 * We need to make sure that the conn_recv is set to a non-null 27185 * value before we insert the conn into the classifier table. 27186 * This is to avoid a race with an incoming packet which does an 27187 * ipcl_classify(). 27188 */ 27189 connp->conn_recv = tcp_conn_request; 27190 if (tcp->tcp_family == AF_INET) { 27191 error = ip_proto_bind_laddr_v4(connp, NULL, 27192 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 27193 } else { 27194 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 27195 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 27196 } 27197 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 27198 } 27199 27200 void 27201 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 27202 { 27203 conn_t *connp = (conn_t *)proto_handle; 27204 tcp_t *tcp = connp->conn_tcp; 27205 mblk_t *mp; 27206 int error; 27207 27208 ASSERT(connp->conn_upper_handle != NULL); 27209 27210 /* 27211 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_clr_flowctrl() 27212 * is currently running. 27213 */ 27214 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27215 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 27216 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27217 return; 27218 } 27219 tcp->tcp_rsrv_mp = NULL; 27220 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27221 27222 error = squeue_synch_enter(connp->conn_sqp, connp, mp); 27223 ASSERT(error == 0); 27224 27225 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27226 tcp->tcp_rsrv_mp = mp; 27227 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27228 27229 if (tcp->tcp_fused) { 27230 tcp_fuse_backenable(tcp); 27231 } else { 27232 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27233 /* 27234 * Send back a window update immediately if TCP is above 27235 * ESTABLISHED state and the increase of the rcv window 27236 * that the other side knows is at least 1 MSS after flow 27237 * control is lifted. 27238 */ 27239 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27240 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 27241 tcp_xmit_ctl(NULL, tcp, 27242 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27243 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27244 } 27245 } 27246 27247 squeue_synch_exit(connp->conn_sqp, connp); 27248 } 27249 27250 /* ARGSUSED */ 27251 int 27252 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27253 int mode, int32_t *rvalp, cred_t *cr) 27254 { 27255 conn_t *connp = (conn_t *)proto_handle; 27256 int error; 27257 27258 ASSERT(connp->conn_upper_handle != NULL); 27259 27260 /* All Solaris components should pass a cred for this operation. */ 27261 ASSERT(cr != NULL); 27262 27263 switch (cmd) { 27264 case ND_SET: 27265 case ND_GET: 27266 case TCP_IOC_DEFAULT_Q: 27267 case _SIOCSOCKFALLBACK: 27268 case TCP_IOC_ABORT_CONN: 27269 case TI_GETPEERNAME: 27270 case TI_GETMYNAME: 27271 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27272 cmd)); 27273 error = EINVAL; 27274 break; 27275 default: 27276 /* 27277 * Pass on to IP using helper stream 27278 */ 27279 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27280 cmd, arg, mode, cr, rvalp); 27281 break; 27282 } 27283 return (error); 27284 } 27285 27286 sock_downcalls_t sock_tcp_downcalls = { 27287 tcp_activate, 27288 tcp_accept, 27289 tcp_bind, 27290 tcp_listen, 27291 tcp_connect, 27292 tcp_getpeername, 27293 tcp_getsockname, 27294 tcp_getsockopt, 27295 tcp_setsockopt, 27296 tcp_sendmsg, 27297 NULL, 27298 NULL, 27299 NULL, 27300 tcp_shutdown, 27301 tcp_clr_flowctrl, 27302 tcp_ioctl, 27303 tcp_close, 27304 }; 27305