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 void 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 mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 778 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 779 static void tcp_reinit(tcp_t *tcp); 780 static void tcp_reinit_values(tcp_t *tcp); 781 782 static uint_t tcp_rwnd_reopen(tcp_t *tcp); 783 static uint_t tcp_rcv_drain(tcp_t *tcp); 784 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 785 static boolean_t tcp_send_rst_chk(tcp_stack_t *); 786 static void tcp_ss_rexmit(tcp_t *tcp); 787 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 788 static void tcp_process_options(tcp_t *, tcph_t *); 789 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 790 static void tcp_rsrv(queue_t *q); 791 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 792 static int tcp_snmp_state(tcp_t *tcp); 793 static void tcp_timer(void *arg); 794 static void tcp_timer_callback(void *); 795 static in_port_t tcp_update_next_port(in_port_t port, const tcp_t *tcp, 796 boolean_t random); 797 static in_port_t tcp_get_next_priv_port(const tcp_t *); 798 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 799 static void tcp_wput_fallback(queue_t *q, mblk_t *mp); 800 void tcp_tpi_accept(queue_t *q, mblk_t *mp); 801 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 802 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 803 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 804 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 805 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 806 const int num_sack_blk, int *usable, uint_t *snxt, 807 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 808 const int mdt_thres); 809 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 810 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 811 const int num_sack_blk, int *usable, uint_t *snxt, 812 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 813 const int mdt_thres); 814 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 815 int num_sack_blk); 816 static void tcp_wsrv(queue_t *q); 817 static int tcp_xmit_end(tcp_t *tcp); 818 static void tcp_ack_timer(void *arg); 819 static mblk_t *tcp_ack_mp(tcp_t *tcp); 820 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 821 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len, 822 zoneid_t zoneid, tcp_stack_t *, conn_t *connp); 823 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 824 uint32_t ack, int ctl); 825 static int setmaxps(queue_t *q, int maxpsz); 826 static void tcp_set_rto(tcp_t *, time_t); 827 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 828 boolean_t, boolean_t); 829 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 830 boolean_t ipsec_mctl); 831 static int tcp_build_hdrs(tcp_t *); 832 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 833 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 834 tcph_t *tcph); 835 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 836 static mblk_t *tcp_mdt_info_mp(mblk_t *); 837 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 838 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 839 const boolean_t, const uint32_t, const uint32_t, 840 const uint32_t, const uint32_t, tcp_stack_t *); 841 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 842 const uint_t, const uint_t, boolean_t *); 843 static mblk_t *tcp_lso_info_mp(mblk_t *); 844 static void tcp_lso_update(tcp_t *, ill_lso_capab_t *); 845 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 846 extern mblk_t *tcp_timermp_alloc(int); 847 extern void tcp_timermp_free(tcp_t *); 848 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 849 static void tcp_stop_lingering(tcp_t *tcp); 850 static void tcp_close_linger_timeout(void *arg); 851 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns); 852 static void tcp_stack_shutdown(netstackid_t stackid, void *arg); 853 static void tcp_stack_fini(netstackid_t stackid, void *arg); 854 static void *tcp_g_kstat_init(tcp_g_stat_t *); 855 static void tcp_g_kstat_fini(kstat_t *); 856 static void *tcp_kstat_init(netstackid_t, tcp_stack_t *); 857 static void tcp_kstat_fini(netstackid_t, kstat_t *); 858 static void *tcp_kstat2_init(netstackid_t, tcp_stat_t *); 859 static void tcp_kstat2_fini(netstackid_t, kstat_t *); 860 static int tcp_kstat_update(kstat_t *kp, int rw); 861 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 862 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 863 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 864 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 865 tcph_t *tcph, mblk_t *idmp); 866 static int tcp_squeue_switch(int); 867 868 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t); 869 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *); 870 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *); 871 static int tcp_tpi_close(queue_t *, int); 872 static int tcp_tpi_close_accept(queue_t *); 873 874 static void tcp_squeue_add(squeue_t *); 875 static boolean_t tcp_zcopy_check(tcp_t *); 876 static void tcp_zcopy_notify(tcp_t *); 877 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 878 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 879 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 880 881 extern void tcp_kssl_input(tcp_t *, mblk_t *); 882 883 void tcp_eager_kill(void *arg, mblk_t *mp, void *arg2); 884 void tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2); 885 886 static int tcp_accept(sock_lower_handle_t, sock_lower_handle_t, 887 sock_upper_handle_t, cred_t *); 888 static int tcp_listen(sock_lower_handle_t, int, cred_t *); 889 static int tcp_post_ip_bind(tcp_t *, mblk_t *, int, cred_t *, pid_t); 890 static int tcp_do_listen(conn_t *, struct sockaddr *, socklen_t, int, cred_t *, 891 boolean_t); 892 static int tcp_do_connect(conn_t *, const struct sockaddr *, socklen_t, 893 cred_t *, pid_t); 894 static int tcp_do_bind(conn_t *, struct sockaddr *, socklen_t, cred_t *, 895 boolean_t); 896 static int tcp_do_unbind(conn_t *); 897 static int tcp_bind_check(conn_t *, struct sockaddr *, socklen_t, cred_t *, 898 boolean_t); 899 900 static void tcp_ulp_newconn(conn_t *, conn_t *, mblk_t *); 901 902 /* 903 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 904 * 905 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 906 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 907 * (defined in tcp.h) needs to be filled in and passed into the kernel 908 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 909 * structure contains the four-tuple of a TCP connection and a range of TCP 910 * states (specified by ac_start and ac_end). The use of wildcard addresses 911 * and ports is allowed. Connections with a matching four tuple and a state 912 * within the specified range will be aborted. The valid states for the 913 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 914 * inclusive. 915 * 916 * An application which has its connection aborted by this ioctl will receive 917 * an error that is dependent on the connection state at the time of the abort. 918 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 919 * though a RST packet has been received. If the connection state is equal to 920 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 921 * and all resources associated with the connection will be freed. 922 */ 923 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 924 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 925 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 926 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *, tcp_stack_t *tcps); 927 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 928 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 929 boolean_t, tcp_stack_t *); 930 931 static struct module_info tcp_rinfo = { 932 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 933 }; 934 935 static struct module_info tcp_winfo = { 936 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 937 }; 938 939 /* 940 * Entry points for TCP as a device. The normal case which supports 941 * the TCP functionality. 942 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. 943 */ 944 struct qinit tcp_rinitv4 = { 945 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo 946 }; 947 948 struct qinit tcp_rinitv6 = { 949 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo 950 }; 951 952 struct qinit tcp_winit = { 953 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 954 }; 955 956 /* Initial entry point for TCP in socket mode. */ 957 struct qinit tcp_sock_winit = { 958 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 959 }; 960 961 /* TCP entry point during fallback */ 962 struct qinit tcp_fallback_sock_winit = { 963 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo 964 }; 965 966 /* 967 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 968 * an accept. Avoid allocating data structures since eager has already 969 * been created. 970 */ 971 struct qinit tcp_acceptor_rinit = { 972 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo 973 }; 974 975 struct qinit tcp_acceptor_winit = { 976 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo 977 }; 978 979 /* 980 * Entry points for TCP loopback (read side only) 981 * The open routine is only used for reopens, thus no need to 982 * have a separate one for tcp_openv6. 983 */ 984 struct qinit tcp_loopback_rinit = { 985 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, (pfi_t)0, 986 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 987 }; 988 989 /* For AF_INET aka /dev/tcp */ 990 struct streamtab tcpinfov4 = { 991 &tcp_rinitv4, &tcp_winit 992 }; 993 994 /* For AF_INET6 aka /dev/tcp6 */ 995 struct streamtab tcpinfov6 = { 996 &tcp_rinitv6, &tcp_winit 997 }; 998 999 sock_downcalls_t sock_tcp_downcalls; 1000 1001 /* 1002 * Have to ensure that tcp_g_q_close is not done by an 1003 * interrupt thread. 1004 */ 1005 static taskq_t *tcp_taskq; 1006 1007 /* Setable only in /etc/system. Move to ndd? */ 1008 boolean_t tcp_icmp_source_quench = B_FALSE; 1009 1010 /* 1011 * Following assumes TPI alignment requirements stay along 32 bit 1012 * boundaries 1013 */ 1014 #define ROUNDUP32(x) \ 1015 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1016 1017 /* Template for response to info request. */ 1018 static struct T_info_ack tcp_g_t_info_ack = { 1019 T_INFO_ACK, /* PRIM_type */ 1020 0, /* TSDU_size */ 1021 T_INFINITE, /* ETSDU_size */ 1022 T_INVALID, /* CDATA_size */ 1023 T_INVALID, /* DDATA_size */ 1024 sizeof (sin_t), /* ADDR_size */ 1025 0, /* OPT_size - not initialized here */ 1026 TIDUSZ, /* TIDU_size */ 1027 T_COTS_ORD, /* SERV_type */ 1028 TCPS_IDLE, /* CURRENT_state */ 1029 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1030 }; 1031 1032 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1033 T_INFO_ACK, /* PRIM_type */ 1034 0, /* TSDU_size */ 1035 T_INFINITE, /* ETSDU_size */ 1036 T_INVALID, /* CDATA_size */ 1037 T_INVALID, /* DDATA_size */ 1038 sizeof (sin6_t), /* ADDR_size */ 1039 0, /* OPT_size - not initialized here */ 1040 TIDUSZ, /* TIDU_size */ 1041 T_COTS_ORD, /* SERV_type */ 1042 TCPS_IDLE, /* CURRENT_state */ 1043 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1044 }; 1045 1046 #define MS 1L 1047 #define SECONDS (1000 * MS) 1048 #define MINUTES (60 * SECONDS) 1049 #define HOURS (60 * MINUTES) 1050 #define DAYS (24 * HOURS) 1051 1052 #define PARAM_MAX (~(uint32_t)0) 1053 1054 /* Max size IP datagram is 64k - 1 */ 1055 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1056 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1057 /* Max of the above */ 1058 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1059 1060 /* Largest TCP port number */ 1061 #define TCP_MAX_PORT (64 * 1024 - 1) 1062 1063 /* 1064 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1065 * layer header. It has to be a multiple of 4. 1066 */ 1067 static tcpparam_t lcl_tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1068 #define tcps_wroff_xtra tcps_wroff_xtra_param->tcp_param_val 1069 1070 /* 1071 * All of these are alterable, within the min/max values given, at run time. 1072 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1073 * per the TCP spec. 1074 */ 1075 /* BEGIN CSTYLED */ 1076 static tcpparam_t lcl_tcp_param_arr[] = { 1077 /*min max value name */ 1078 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1079 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1080 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1081 { 1, 1024, 1, "tcp_conn_req_min" }, 1082 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1083 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1084 { 0, 10, 0, "tcp_debug" }, 1085 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1086 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1087 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1088 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1089 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1090 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1091 { 1, 255, 64, "tcp_ipv4_ttl"}, 1092 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1093 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1094 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1095 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1096 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1097 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1098 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1099 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1100 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1101 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1102 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1103 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1104 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1105 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1106 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1107 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1108 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1109 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1110 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1111 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1112 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1113 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1114 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1115 /* 1116 * Question: What default value should I set for tcp_strong_iss? 1117 */ 1118 { 0, 2, 1, "tcp_strong_iss"}, 1119 { 0, 65536, 20, "tcp_rtt_updates"}, 1120 { 0, 1, 1, "tcp_wscale_always"}, 1121 { 0, 1, 0, "tcp_tstamp_always"}, 1122 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1123 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1124 { 0, 16, 2, "tcp_deferred_acks_max"}, 1125 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1126 { 1, 4, 4, "tcp_slow_start_initial"}, 1127 { 0, 2, 2, "tcp_sack_permitted"}, 1128 { 0, 1, 1, "tcp_compression_enabled"}, 1129 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1130 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1131 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1132 { 0, 1, 0, "tcp_rev_src_routes"}, 1133 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1134 { 0, 16, 8, "tcp_local_dacks_max"}, 1135 { 0, 2, 1, "tcp_ecn_permitted"}, 1136 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1137 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1138 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1139 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1140 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1141 }; 1142 /* END CSTYLED */ 1143 1144 /* 1145 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1146 * each header fragment in the header buffer. Each parameter value has 1147 * to be a multiple of 4 (32-bit aligned). 1148 */ 1149 static tcpparam_t lcl_tcp_mdt_head_param = 1150 { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1151 static tcpparam_t lcl_tcp_mdt_tail_param = 1152 { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1153 #define tcps_mdt_hdr_head_min tcps_mdt_head_param->tcp_param_val 1154 #define tcps_mdt_hdr_tail_min tcps_mdt_tail_param->tcp_param_val 1155 1156 /* 1157 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1158 * the maximum number of payload buffers associated per Multidata. 1159 */ 1160 static tcpparam_t lcl_tcp_mdt_max_pbufs_param = 1161 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1162 #define tcps_mdt_max_pbufs tcps_mdt_max_pbufs_param->tcp_param_val 1163 1164 /* Round up the value to the nearest mss. */ 1165 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1166 1167 /* 1168 * Set ECN capable transport (ECT) code point in IP header. 1169 * 1170 * Note that there are 2 ECT code points '01' and '10', which are called 1171 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1172 * point ECT(0) for TCP as described in RFC 2481. 1173 */ 1174 #define SET_ECT(tcp, iph) \ 1175 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1176 /* We need to clear the code point first. */ \ 1177 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1178 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1179 } else { \ 1180 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1181 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1182 } 1183 1184 /* 1185 * The format argument to pass to tcp_display(). 1186 * DISP_PORT_ONLY means that the returned string has only port info. 1187 * DISP_ADDR_AND_PORT means that the returned string also contains the 1188 * remote and local IP address. 1189 */ 1190 #define DISP_PORT_ONLY 1 1191 #define DISP_ADDR_AND_PORT 2 1192 1193 #define IS_VMLOANED_MBLK(mp) \ 1194 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1195 1196 1197 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1198 boolean_t tcp_mdt_chain = B_TRUE; 1199 1200 /* 1201 * MDT threshold in the form of effective send MSS multiplier; we take 1202 * the MDT path if the amount of unsent data exceeds the threshold value 1203 * (default threshold is 1*SMSS). 1204 */ 1205 uint_t tcp_mdt_smss_threshold = 1; 1206 1207 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1208 1209 /* 1210 * Forces all connections to obey the value of the tcps_maxpsz_multiplier 1211 * tunable settable via NDD. Otherwise, the per-connection behavior is 1212 * determined dynamically during tcp_adapt_ire(), which is the default. 1213 */ 1214 boolean_t tcp_static_maxpsz = B_FALSE; 1215 1216 /* Setable in /etc/system */ 1217 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1218 uint32_t tcp_random_anon_port = 1; 1219 1220 /* 1221 * To reach to an eager in Q0 which can be dropped due to an incoming 1222 * new SYN request when Q0 is full, a new doubly linked list is 1223 * introduced. This list allows to select an eager from Q0 in O(1) time. 1224 * This is needed to avoid spending too much time walking through the 1225 * long list of eagers in Q0 when tcp_drop_q0() is called. Each member of 1226 * this new list has to be a member of Q0. 1227 * This list is headed by listener's tcp_t. When the list is empty, 1228 * both the pointers - tcp_eager_next_drop_q0 and tcp_eager_prev_drop_q0, 1229 * of listener's tcp_t point to listener's tcp_t itself. 1230 * 1231 * Given an eager in Q0 and a listener, MAKE_DROPPABLE() puts the eager 1232 * in the list. MAKE_UNDROPPABLE() takes the eager out of the list. 1233 * These macros do not affect the eager's membership to Q0. 1234 */ 1235 1236 1237 #define MAKE_DROPPABLE(listener, eager) \ 1238 if ((eager)->tcp_eager_next_drop_q0 == NULL) { \ 1239 (listener)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0\ 1240 = (eager); \ 1241 (eager)->tcp_eager_prev_drop_q0 = (listener); \ 1242 (eager)->tcp_eager_next_drop_q0 = \ 1243 (listener)->tcp_eager_next_drop_q0; \ 1244 (listener)->tcp_eager_next_drop_q0 = (eager); \ 1245 } 1246 1247 #define MAKE_UNDROPPABLE(eager) \ 1248 if ((eager)->tcp_eager_next_drop_q0 != NULL) { \ 1249 (eager)->tcp_eager_next_drop_q0->tcp_eager_prev_drop_q0 \ 1250 = (eager)->tcp_eager_prev_drop_q0; \ 1251 (eager)->tcp_eager_prev_drop_q0->tcp_eager_next_drop_q0 \ 1252 = (eager)->tcp_eager_next_drop_q0; \ 1253 (eager)->tcp_eager_prev_drop_q0 = NULL; \ 1254 (eager)->tcp_eager_next_drop_q0 = NULL; \ 1255 } 1256 1257 /* 1258 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1259 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1260 * data, TCP will not respond with an ACK. RFC 793 requires that 1261 * TCP responds with an ACK for such a bogus ACK. By not following 1262 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1263 * an attacker successfully spoofs an acceptable segment to our 1264 * peer; or when our peer is "confused." 1265 */ 1266 uint32_t tcp_drop_ack_unsent_cnt = 10; 1267 1268 /* 1269 * Hook functions to enable cluster networking 1270 * On non-clustered systems these vectors must always be NULL. 1271 */ 1272 1273 void (*cl_inet_listen)(netstackid_t stack_id, uint8_t protocol, 1274 sa_family_t addr_family, uint8_t *laddrp, 1275 in_port_t lport, void *args) = NULL; 1276 void (*cl_inet_unlisten)(netstackid_t stack_id, uint8_t protocol, 1277 sa_family_t addr_family, uint8_t *laddrp, 1278 in_port_t lport, void *args) = NULL; 1279 1280 int (*cl_inet_connect2)(netstackid_t stack_id, uint8_t protocol, 1281 boolean_t is_outgoing, 1282 sa_family_t addr_family, 1283 uint8_t *laddrp, in_port_t lport, 1284 uint8_t *faddrp, in_port_t fport, 1285 void *args) = NULL; 1286 1287 void (*cl_inet_disconnect)(netstackid_t stack_id, uint8_t protocol, 1288 sa_family_t addr_family, uint8_t *laddrp, 1289 in_port_t lport, uint8_t *faddrp, 1290 in_port_t fport, void *args) = NULL; 1291 1292 /* 1293 * The following are defined in ip.c 1294 */ 1295 extern int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol, 1296 sa_family_t addr_family, uint8_t *laddrp, 1297 void *args); 1298 extern uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol, 1299 sa_family_t addr_family, uint8_t *laddrp, 1300 uint8_t *faddrp, void *args); 1301 1302 1303 /* 1304 * int CL_INET_CONNECT(conn_t *cp, tcp_t *tcp, boolean_t is_outgoing, int err) 1305 */ 1306 #define CL_INET_CONNECT(connp, tcp, is_outgoing, err) { \ 1307 (err) = 0; \ 1308 if (cl_inet_connect2 != NULL) { \ 1309 /* \ 1310 * Running in cluster mode - register active connection \ 1311 * information \ 1312 */ \ 1313 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1314 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1315 (err) = (*cl_inet_connect2)( \ 1316 (connp)->conn_netstack->netstack_stackid,\ 1317 IPPROTO_TCP, is_outgoing, AF_INET, \ 1318 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1319 (in_port_t)(tcp)->tcp_lport, \ 1320 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1321 (in_port_t)(tcp)->tcp_fport, NULL); \ 1322 } \ 1323 } else { \ 1324 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1325 &(tcp)->tcp_ip6h->ip6_src)) { \ 1326 (err) = (*cl_inet_connect2)( \ 1327 (connp)->conn_netstack->netstack_stackid,\ 1328 IPPROTO_TCP, is_outgoing, AF_INET6, \ 1329 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1330 (in_port_t)(tcp)->tcp_lport, \ 1331 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1332 (in_port_t)(tcp)->tcp_fport, NULL); \ 1333 } \ 1334 } \ 1335 } \ 1336 } 1337 1338 #define CL_INET_DISCONNECT(connp, tcp) { \ 1339 if (cl_inet_disconnect != NULL) { \ 1340 /* \ 1341 * Running in cluster mode - deregister active \ 1342 * connection information \ 1343 */ \ 1344 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1345 if ((tcp)->tcp_ip_src != 0) { \ 1346 (*cl_inet_disconnect)( \ 1347 (connp)->conn_netstack->netstack_stackid,\ 1348 IPPROTO_TCP, AF_INET, \ 1349 (uint8_t *)(&((tcp)->tcp_ip_src)), \ 1350 (in_port_t)(tcp)->tcp_lport, \ 1351 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1352 (in_port_t)(tcp)->tcp_fport, NULL); \ 1353 } \ 1354 } else { \ 1355 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1356 &(tcp)->tcp_ip_src_v6)) { \ 1357 (*cl_inet_disconnect)( \ 1358 (connp)->conn_netstack->netstack_stackid,\ 1359 IPPROTO_TCP, AF_INET6, \ 1360 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1361 (in_port_t)(tcp)->tcp_lport, \ 1362 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1363 (in_port_t)(tcp)->tcp_fport, NULL); \ 1364 } \ 1365 } \ 1366 } \ 1367 } 1368 1369 /* 1370 * Cluster networking hook for traversing current connection list. 1371 * This routine is used to extract the current list of live connections 1372 * which must continue to to be dispatched to this node. 1373 */ 1374 int cl_tcp_walk_list(netstackid_t stack_id, 1375 int (*callback)(cl_tcp_info_t *, void *), void *arg); 1376 1377 static int cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), 1378 void *arg, tcp_stack_t *tcps); 1379 1380 #define DTRACE_IP_FASTPATH(mp, iph, ill, ipha, ip6h) \ 1381 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL, void_ip_t *, \ 1382 iph, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, \ 1383 ip6_t *, ip6h, int, 0); 1384 1385 /* 1386 * Figure out the value of window scale opton. Note that the rwnd is 1387 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1388 * We cannot find the scale value and then do a round up of tcp_rwnd 1389 * because the scale value may not be correct after that. 1390 * 1391 * Set the compiler flag to make this function inline. 1392 */ 1393 static void 1394 tcp_set_ws_value(tcp_t *tcp) 1395 { 1396 int i; 1397 uint32_t rwnd = tcp->tcp_rwnd; 1398 1399 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1400 i++, rwnd >>= 1) 1401 ; 1402 tcp->tcp_rcv_ws = i; 1403 } 1404 1405 /* 1406 * Remove a connection from the list of detached TIME_WAIT connections. 1407 * It returns B_FALSE if it can't remove the connection from the list 1408 * as the connection has already been removed from the list due to an 1409 * earlier call to tcp_time_wait_remove(); otherwise it returns B_TRUE. 1410 */ 1411 static boolean_t 1412 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1413 { 1414 boolean_t locked = B_FALSE; 1415 1416 if (tcp_time_wait == NULL) { 1417 tcp_time_wait = *((tcp_squeue_priv_t **) 1418 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1419 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1420 locked = B_TRUE; 1421 } else { 1422 ASSERT(MUTEX_HELD(&tcp_time_wait->tcp_time_wait_lock)); 1423 } 1424 1425 if (tcp->tcp_time_wait_expire == 0) { 1426 ASSERT(tcp->tcp_time_wait_next == NULL); 1427 ASSERT(tcp->tcp_time_wait_prev == NULL); 1428 if (locked) 1429 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1430 return (B_FALSE); 1431 } 1432 ASSERT(TCP_IS_DETACHED(tcp)); 1433 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1434 1435 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1436 ASSERT(tcp->tcp_time_wait_prev == NULL); 1437 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1438 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1439 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1440 NULL; 1441 } else { 1442 tcp_time_wait->tcp_time_wait_tail = NULL; 1443 } 1444 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1445 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1446 ASSERT(tcp->tcp_time_wait_next == NULL); 1447 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1448 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1449 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1450 } else { 1451 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1452 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1453 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1454 tcp->tcp_time_wait_next; 1455 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1456 tcp->tcp_time_wait_prev; 1457 } 1458 tcp->tcp_time_wait_next = NULL; 1459 tcp->tcp_time_wait_prev = NULL; 1460 tcp->tcp_time_wait_expire = 0; 1461 1462 if (locked) 1463 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1464 return (B_TRUE); 1465 } 1466 1467 /* 1468 * Add a connection to the list of detached TIME_WAIT connections 1469 * and set its time to expire. 1470 */ 1471 static void 1472 tcp_time_wait_append(tcp_t *tcp) 1473 { 1474 tcp_stack_t *tcps = tcp->tcp_tcps; 1475 tcp_squeue_priv_t *tcp_time_wait = 1476 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1477 SQPRIVATE_TCP)); 1478 1479 tcp_timers_stop(tcp); 1480 1481 /* Freed above */ 1482 ASSERT(tcp->tcp_timer_tid == 0); 1483 ASSERT(tcp->tcp_ack_tid == 0); 1484 1485 /* must have happened at the time of detaching the tcp */ 1486 ASSERT(tcp->tcp_ptpahn == NULL); 1487 ASSERT(tcp->tcp_flow_stopped == 0); 1488 ASSERT(tcp->tcp_time_wait_next == NULL); 1489 ASSERT(tcp->tcp_time_wait_prev == NULL); 1490 ASSERT(tcp->tcp_time_wait_expire == NULL); 1491 ASSERT(tcp->tcp_listener == NULL); 1492 1493 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1494 /* 1495 * The value computed below in tcp->tcp_time_wait_expire may 1496 * appear negative or wrap around. That is ok since our 1497 * interest is only in the difference between the current lbolt 1498 * value and tcp->tcp_time_wait_expire. But the value should not 1499 * be zero, since it means the tcp is not in the TIME_WAIT list. 1500 * The corresponding comparison in tcp_time_wait_collector() uses 1501 * modular arithmetic. 1502 */ 1503 tcp->tcp_time_wait_expire += 1504 drv_usectohz(tcps->tcps_time_wait_interval * 1000); 1505 if (tcp->tcp_time_wait_expire == 0) 1506 tcp->tcp_time_wait_expire = 1; 1507 1508 ASSERT(TCP_IS_DETACHED(tcp)); 1509 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1510 ASSERT(tcp->tcp_time_wait_next == NULL); 1511 ASSERT(tcp->tcp_time_wait_prev == NULL); 1512 TCP_DBGSTAT(tcps, tcp_time_wait); 1513 1514 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1515 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1516 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1517 tcp_time_wait->tcp_time_wait_head = tcp; 1518 } else { 1519 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1520 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1521 TCPS_TIME_WAIT); 1522 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1523 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1524 } 1525 tcp_time_wait->tcp_time_wait_tail = tcp; 1526 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1527 } 1528 1529 /* ARGSUSED */ 1530 void 1531 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1532 { 1533 conn_t *connp = (conn_t *)arg; 1534 tcp_t *tcp = connp->conn_tcp; 1535 tcp_stack_t *tcps = tcp->tcp_tcps; 1536 1537 ASSERT(tcp != NULL); 1538 if (tcp->tcp_state == TCPS_CLOSED) { 1539 return; 1540 } 1541 1542 ASSERT((tcp->tcp_family == AF_INET && 1543 tcp->tcp_ipversion == IPV4_VERSION) || 1544 (tcp->tcp_family == AF_INET6 && 1545 (tcp->tcp_ipversion == IPV4_VERSION || 1546 tcp->tcp_ipversion == IPV6_VERSION))); 1547 ASSERT(!tcp->tcp_listener); 1548 1549 TCP_STAT(tcps, tcp_time_wait_reap); 1550 ASSERT(TCP_IS_DETACHED(tcp)); 1551 1552 /* 1553 * Because they have no upstream client to rebind or tcp_close() 1554 * them later, we axe the connection here and now. 1555 */ 1556 tcp_close_detached(tcp); 1557 } 1558 1559 /* 1560 * Remove cached/latched IPsec references. 1561 */ 1562 void 1563 tcp_ipsec_cleanup(tcp_t *tcp) 1564 { 1565 conn_t *connp = tcp->tcp_connp; 1566 1567 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1568 1569 if (connp->conn_latch != NULL) { 1570 IPLATCH_REFRELE(connp->conn_latch, 1571 connp->conn_netstack); 1572 connp->conn_latch = NULL; 1573 } 1574 if (connp->conn_policy != NULL) { 1575 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack); 1576 connp->conn_policy = NULL; 1577 } 1578 } 1579 1580 /* 1581 * Cleaup before placing on free list. 1582 * Disassociate from the netstack/tcp_stack_t since the freelist 1583 * is per squeue and not per netstack. 1584 */ 1585 void 1586 tcp_cleanup(tcp_t *tcp) 1587 { 1588 mblk_t *mp; 1589 char *tcp_iphc; 1590 int tcp_iphc_len; 1591 int tcp_hdr_grown; 1592 tcp_sack_info_t *tcp_sack_info; 1593 conn_t *connp = tcp->tcp_connp; 1594 tcp_stack_t *tcps = tcp->tcp_tcps; 1595 netstack_t *ns = tcps->tcps_netstack; 1596 mblk_t *tcp_rsrv_mp; 1597 1598 tcp_bind_hash_remove(tcp); 1599 1600 /* Cleanup that which needs the netstack first */ 1601 tcp_ipsec_cleanup(tcp); 1602 1603 tcp_free(tcp); 1604 1605 /* Release any SSL context */ 1606 if (tcp->tcp_kssl_ent != NULL) { 1607 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1608 tcp->tcp_kssl_ent = NULL; 1609 } 1610 1611 if (tcp->tcp_kssl_ctx != NULL) { 1612 kssl_release_ctx(tcp->tcp_kssl_ctx); 1613 tcp->tcp_kssl_ctx = NULL; 1614 } 1615 tcp->tcp_kssl_pending = B_FALSE; 1616 1617 conn_delete_ire(connp, NULL); 1618 1619 /* 1620 * Since we will bzero the entire structure, we need to 1621 * remove it and reinsert it in global hash list. We 1622 * know the walkers can't get to this conn because we 1623 * had set CONDEMNED flag earlier and checked reference 1624 * under conn_lock so walker won't pick it and when we 1625 * go the ipcl_globalhash_remove() below, no walker 1626 * can get to it. 1627 */ 1628 ipcl_globalhash_remove(connp); 1629 1630 /* 1631 * Now it is safe to decrement the reference counts. 1632 * This might be the last reference on the netstack and TCPS 1633 * in which case it will cause the tcp_g_q_close and 1634 * the freeing of the IP Instance. 1635 */ 1636 connp->conn_netstack = NULL; 1637 netstack_rele(ns); 1638 ASSERT(tcps != NULL); 1639 tcp->tcp_tcps = NULL; 1640 TCPS_REFRELE(tcps); 1641 1642 /* Save some state */ 1643 mp = tcp->tcp_timercache; 1644 1645 tcp_sack_info = tcp->tcp_sack_info; 1646 tcp_iphc = tcp->tcp_iphc; 1647 tcp_iphc_len = tcp->tcp_iphc_len; 1648 tcp_hdr_grown = tcp->tcp_hdr_grown; 1649 tcp_rsrv_mp = tcp->tcp_rsrv_mp; 1650 1651 if (connp->conn_cred != NULL) { 1652 crfree(connp->conn_cred); 1653 connp->conn_cred = NULL; 1654 } 1655 if (connp->conn_peercred != NULL) { 1656 crfree(connp->conn_peercred); 1657 connp->conn_peercred = NULL; 1658 } 1659 ipcl_conn_cleanup(connp); 1660 connp->conn_flags = IPCL_TCPCONN; 1661 bzero(tcp, sizeof (tcp_t)); 1662 1663 /* restore the state */ 1664 tcp->tcp_timercache = mp; 1665 1666 tcp->tcp_sack_info = tcp_sack_info; 1667 tcp->tcp_iphc = tcp_iphc; 1668 tcp->tcp_iphc_len = tcp_iphc_len; 1669 tcp->tcp_hdr_grown = tcp_hdr_grown; 1670 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 1671 1672 tcp->tcp_connp = connp; 1673 1674 ASSERT(connp->conn_tcp == tcp); 1675 ASSERT(connp->conn_flags & IPCL_TCPCONN); 1676 connp->conn_state_flags = CONN_INCIPIENT; 1677 ASSERT(connp->conn_ulp == IPPROTO_TCP); 1678 ASSERT(connp->conn_ref == 1); 1679 } 1680 1681 /* 1682 * Blows away all tcps whose TIME_WAIT has expired. List traversal 1683 * is done forwards from the head. 1684 * This walks all stack instances since 1685 * tcp_time_wait remains global across all stacks. 1686 */ 1687 /* ARGSUSED */ 1688 void 1689 tcp_time_wait_collector(void *arg) 1690 { 1691 tcp_t *tcp; 1692 clock_t now; 1693 mblk_t *mp; 1694 conn_t *connp; 1695 kmutex_t *lock; 1696 boolean_t removed; 1697 1698 squeue_t *sqp = (squeue_t *)arg; 1699 tcp_squeue_priv_t *tcp_time_wait = 1700 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 1701 1702 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1703 tcp_time_wait->tcp_time_wait_tid = 0; 1704 1705 if (tcp_time_wait->tcp_free_list != NULL && 1706 tcp_time_wait->tcp_free_list->tcp_in_free_list == B_TRUE) { 1707 TCP_G_STAT(tcp_freelist_cleanup); 1708 while ((tcp = tcp_time_wait->tcp_free_list) != NULL) { 1709 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 1710 tcp->tcp_time_wait_next = NULL; 1711 tcp_time_wait->tcp_free_list_cnt--; 1712 ASSERT(tcp->tcp_tcps == NULL); 1713 CONN_DEC_REF(tcp->tcp_connp); 1714 } 1715 ASSERT(tcp_time_wait->tcp_free_list_cnt == 0); 1716 } 1717 1718 /* 1719 * In order to reap time waits reliably, we should use a 1720 * source of time that is not adjustable by the user -- hence 1721 * the call to ddi_get_lbolt(). 1722 */ 1723 now = ddi_get_lbolt(); 1724 while ((tcp = tcp_time_wait->tcp_time_wait_head) != NULL) { 1725 /* 1726 * Compare times using modular arithmetic, since 1727 * lbolt can wrapover. 1728 */ 1729 if ((now - tcp->tcp_time_wait_expire) < 0) { 1730 break; 1731 } 1732 1733 removed = tcp_time_wait_remove(tcp, tcp_time_wait); 1734 ASSERT(removed); 1735 1736 connp = tcp->tcp_connp; 1737 ASSERT(connp->conn_fanout != NULL); 1738 lock = &connp->conn_fanout->connf_lock; 1739 /* 1740 * This is essentially a TW reclaim fast path optimization for 1741 * performance where the timewait collector checks under the 1742 * fanout lock (so that no one else can get access to the 1743 * conn_t) that the refcnt is 2 i.e. one for TCP and one for 1744 * the classifier hash list. If ref count is indeed 2, we can 1745 * just remove the conn under the fanout lock and avoid 1746 * cleaning up the conn under the squeue, provided that 1747 * clustering callbacks are not enabled. If clustering is 1748 * enabled, we need to make the clustering callback before 1749 * setting the CONDEMNED flag and after dropping all locks and 1750 * so we forego this optimization and fall back to the slow 1751 * path. Also please see the comments in tcp_closei_local 1752 * regarding the refcnt logic. 1753 * 1754 * Since we are holding the tcp_time_wait_lock, its better 1755 * not to block on the fanout_lock because other connections 1756 * can't add themselves to time_wait list. So we do a 1757 * tryenter instead of mutex_enter. 1758 */ 1759 if (mutex_tryenter(lock)) { 1760 mutex_enter(&connp->conn_lock); 1761 if ((connp->conn_ref == 2) && 1762 (cl_inet_disconnect == NULL)) { 1763 ipcl_hash_remove_locked(connp, 1764 connp->conn_fanout); 1765 /* 1766 * Set the CONDEMNED flag now itself so that 1767 * the refcnt cannot increase due to any 1768 * walker. But we have still not cleaned up 1769 * conn_ire_cache. This is still ok since 1770 * we are going to clean it up in tcp_cleanup 1771 * immediately and any interface unplumb 1772 * thread will wait till the ire is blown away 1773 */ 1774 connp->conn_state_flags |= CONN_CONDEMNED; 1775 mutex_exit(lock); 1776 mutex_exit(&connp->conn_lock); 1777 if (tcp_time_wait->tcp_free_list_cnt < 1778 tcp_free_list_max_cnt) { 1779 /* Add to head of tcp_free_list */ 1780 mutex_exit( 1781 &tcp_time_wait->tcp_time_wait_lock); 1782 tcp_cleanup(tcp); 1783 ASSERT(connp->conn_latch == NULL); 1784 ASSERT(connp->conn_policy == NULL); 1785 ASSERT(tcp->tcp_tcps == NULL); 1786 ASSERT(connp->conn_netstack == NULL); 1787 1788 mutex_enter( 1789 &tcp_time_wait->tcp_time_wait_lock); 1790 tcp->tcp_time_wait_next = 1791 tcp_time_wait->tcp_free_list; 1792 tcp_time_wait->tcp_free_list = tcp; 1793 tcp_time_wait->tcp_free_list_cnt++; 1794 continue; 1795 } else { 1796 /* Do not add to tcp_free_list */ 1797 mutex_exit( 1798 &tcp_time_wait->tcp_time_wait_lock); 1799 tcp_bind_hash_remove(tcp); 1800 conn_delete_ire(tcp->tcp_connp, NULL); 1801 tcp_ipsec_cleanup(tcp); 1802 CONN_DEC_REF(tcp->tcp_connp); 1803 } 1804 } else { 1805 CONN_INC_REF_LOCKED(connp); 1806 mutex_exit(lock); 1807 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1808 mutex_exit(&connp->conn_lock); 1809 /* 1810 * We can reuse the closemp here since conn has 1811 * detached (otherwise we wouldn't even be in 1812 * time_wait list). tcp_closemp_used can safely 1813 * be changed without taking a lock as no other 1814 * thread can concurrently access it at this 1815 * point in the connection lifecycle. 1816 */ 1817 1818 if (tcp->tcp_closemp.b_prev == NULL) 1819 tcp->tcp_closemp_used = B_TRUE; 1820 else 1821 cmn_err(CE_PANIC, 1822 "tcp_timewait_collector: " 1823 "concurrent use of tcp_closemp: " 1824 "connp %p tcp %p\n", (void *)connp, 1825 (void *)tcp); 1826 1827 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1828 mp = &tcp->tcp_closemp; 1829 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1830 tcp_timewait_output, connp, 1831 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1832 } 1833 } else { 1834 mutex_enter(&connp->conn_lock); 1835 CONN_INC_REF_LOCKED(connp); 1836 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1837 mutex_exit(&connp->conn_lock); 1838 /* 1839 * We can reuse the closemp here since conn has 1840 * detached (otherwise we wouldn't even be in 1841 * time_wait list). tcp_closemp_used can safely 1842 * be changed without taking a lock as no other 1843 * thread can concurrently access it at this 1844 * point in the connection lifecycle. 1845 */ 1846 1847 if (tcp->tcp_closemp.b_prev == NULL) 1848 tcp->tcp_closemp_used = B_TRUE; 1849 else 1850 cmn_err(CE_PANIC, "tcp_timewait_collector: " 1851 "concurrent use of tcp_closemp: " 1852 "connp %p tcp %p\n", (void *)connp, 1853 (void *)tcp); 1854 1855 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 1856 mp = &tcp->tcp_closemp; 1857 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 1858 tcp_timewait_output, connp, 1859 SQ_FILL, SQTAG_TCP_TIMEWAIT); 1860 } 1861 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1862 } 1863 1864 if (tcp_time_wait->tcp_free_list != NULL) 1865 tcp_time_wait->tcp_free_list->tcp_in_free_list = B_TRUE; 1866 1867 tcp_time_wait->tcp_time_wait_tid = 1868 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 1869 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 1870 CALLOUT_FLAG_ROUNDUP); 1871 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1872 } 1873 1874 /* 1875 * Reply to a clients T_CONN_RES TPI message. This function 1876 * is used only for TLI/XTI listener. Sockfs sends T_CONN_RES 1877 * on the acceptor STREAM and processed in tcp_wput_accept(). 1878 * Read the block comment on top of tcp_conn_request(). 1879 */ 1880 static void 1881 tcp_tli_accept(tcp_t *listener, mblk_t *mp) 1882 { 1883 tcp_t *acceptor; 1884 tcp_t *eager; 1885 tcp_t *tcp; 1886 struct T_conn_res *tcr; 1887 t_uscalar_t acceptor_id; 1888 t_scalar_t seqnum; 1889 mblk_t *opt_mp = NULL; /* T_OPTMGMT_REQ messages */ 1890 struct tcp_options *tcpopt; 1891 mblk_t *ok_mp; 1892 mblk_t *mp1; 1893 tcp_stack_t *tcps = listener->tcp_tcps; 1894 1895 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 1896 tcp_err_ack(listener, mp, TPROTO, 0); 1897 return; 1898 } 1899 tcr = (struct T_conn_res *)mp->b_rptr; 1900 1901 /* 1902 * Under ILP32 the stream head points tcr->ACCEPTOR_id at the 1903 * read side queue of the streams device underneath us i.e. the 1904 * read side queue of 'ip'. Since we can't deference QUEUE_ptr we 1905 * look it up in the queue_hash. Under LP64 it sends down the 1906 * minor_t of the accepting endpoint. 1907 * 1908 * Once the acceptor/eager are modified (in tcp_accept_swap) the 1909 * fanout hash lock is held. 1910 * This prevents any thread from entering the acceptor queue from 1911 * below (since it has not been hard bound yet i.e. any inbound 1912 * packets will arrive on the listener or default tcp queue and 1913 * go through tcp_lookup). 1914 * The CONN_INC_REF will prevent the acceptor from closing. 1915 * 1916 * XXX It is still possible for a tli application to send down data 1917 * on the accepting stream while another thread calls t_accept. 1918 * This should not be a problem for well-behaved applications since 1919 * the T_OK_ACK is sent after the queue swapping is completed. 1920 * 1921 * If the accepting fd is the same as the listening fd, avoid 1922 * queue hash lookup since that will return an eager listener in a 1923 * already established state. 1924 */ 1925 acceptor_id = tcr->ACCEPTOR_id; 1926 mutex_enter(&listener->tcp_eager_lock); 1927 if (listener->tcp_acceptor_id == acceptor_id) { 1928 eager = listener->tcp_eager_next_q; 1929 /* only count how many T_CONN_INDs so don't count q0 */ 1930 if ((listener->tcp_conn_req_cnt_q != 1) || 1931 (eager->tcp_conn_req_seqnum != tcr->SEQ_number)) { 1932 mutex_exit(&listener->tcp_eager_lock); 1933 tcp_err_ack(listener, mp, TBADF, 0); 1934 return; 1935 } 1936 if (listener->tcp_conn_req_cnt_q0 != 0) { 1937 /* Throw away all the eagers on q0. */ 1938 tcp_eager_cleanup(listener, 1); 1939 } 1940 if (listener->tcp_syn_defense) { 1941 listener->tcp_syn_defense = B_FALSE; 1942 if (listener->tcp_ip_addr_cache != NULL) { 1943 kmem_free(listener->tcp_ip_addr_cache, 1944 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 1945 listener->tcp_ip_addr_cache = NULL; 1946 } 1947 } 1948 /* 1949 * Transfer tcp_conn_req_max to the eager so that when 1950 * a disconnect occurs we can revert the endpoint to the 1951 * listen state. 1952 */ 1953 eager->tcp_conn_req_max = listener->tcp_conn_req_max; 1954 ASSERT(listener->tcp_conn_req_cnt_q0 == 0); 1955 /* 1956 * Get a reference on the acceptor just like the 1957 * tcp_acceptor_hash_lookup below. 1958 */ 1959 acceptor = listener; 1960 CONN_INC_REF(acceptor->tcp_connp); 1961 } else { 1962 acceptor = tcp_acceptor_hash_lookup(acceptor_id, tcps); 1963 if (acceptor == NULL) { 1964 if (listener->tcp_debug) { 1965 (void) strlog(TCP_MOD_ID, 0, 1, 1966 SL_ERROR|SL_TRACE, 1967 "tcp_accept: did not find acceptor 0x%x\n", 1968 acceptor_id); 1969 } 1970 mutex_exit(&listener->tcp_eager_lock); 1971 tcp_err_ack(listener, mp, TPROVMISMATCH, 0); 1972 return; 1973 } 1974 /* 1975 * Verify acceptor state. The acceptable states for an acceptor 1976 * include TCPS_IDLE and TCPS_BOUND. 1977 */ 1978 switch (acceptor->tcp_state) { 1979 case TCPS_IDLE: 1980 /* FALLTHRU */ 1981 case TCPS_BOUND: 1982 break; 1983 default: 1984 CONN_DEC_REF(acceptor->tcp_connp); 1985 mutex_exit(&listener->tcp_eager_lock); 1986 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1987 return; 1988 } 1989 } 1990 1991 /* The listener must be in TCPS_LISTEN */ 1992 if (listener->tcp_state != TCPS_LISTEN) { 1993 CONN_DEC_REF(acceptor->tcp_connp); 1994 mutex_exit(&listener->tcp_eager_lock); 1995 tcp_err_ack(listener, mp, TOUTSTATE, 0); 1996 return; 1997 } 1998 1999 /* 2000 * Rendezvous with an eager connection request packet hanging off 2001 * 'tcp' that has the 'seqnum' tag. We tagged the detached open 2002 * tcp structure when the connection packet arrived in 2003 * tcp_conn_request(). 2004 */ 2005 seqnum = tcr->SEQ_number; 2006 eager = listener; 2007 do { 2008 eager = eager->tcp_eager_next_q; 2009 if (eager == NULL) { 2010 CONN_DEC_REF(acceptor->tcp_connp); 2011 mutex_exit(&listener->tcp_eager_lock); 2012 tcp_err_ack(listener, mp, TBADSEQ, 0); 2013 return; 2014 } 2015 } while (eager->tcp_conn_req_seqnum != seqnum); 2016 mutex_exit(&listener->tcp_eager_lock); 2017 2018 /* 2019 * At this point, both acceptor and listener have 2 ref 2020 * that they begin with. Acceptor has one additional ref 2021 * we placed in lookup while listener has 3 additional 2022 * ref for being behind the squeue (tcp_accept() is 2023 * done on listener's squeue); being in classifier hash; 2024 * and eager's ref on listener. 2025 */ 2026 ASSERT(listener->tcp_connp->conn_ref >= 5); 2027 ASSERT(acceptor->tcp_connp->conn_ref >= 3); 2028 2029 /* 2030 * The eager at this point is set in its own squeue and 2031 * could easily have been killed (tcp_accept_finish will 2032 * deal with that) because of a TH_RST so we can only 2033 * ASSERT for a single ref. 2034 */ 2035 ASSERT(eager->tcp_connp->conn_ref >= 1); 2036 2037 /* Pre allocate the stroptions mblk also */ 2038 opt_mp = allocb(MAX(sizeof (struct tcp_options), 2039 sizeof (struct T_conn_res)), BPRI_HI); 2040 if (opt_mp == NULL) { 2041 CONN_DEC_REF(acceptor->tcp_connp); 2042 CONN_DEC_REF(eager->tcp_connp); 2043 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2044 return; 2045 } 2046 DB_TYPE(opt_mp) = M_SETOPTS; 2047 opt_mp->b_wptr += sizeof (struct tcp_options); 2048 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 2049 tcpopt->to_flags = 0; 2050 2051 /* 2052 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 2053 * from listener to acceptor. 2054 */ 2055 if (listener->tcp_bound_if != 0) { 2056 tcpopt->to_flags |= TCPOPT_BOUNDIF; 2057 tcpopt->to_boundif = listener->tcp_bound_if; 2058 } 2059 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 2060 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 2061 } 2062 2063 /* Re-use mp1 to hold a copy of mp, in case reallocb fails */ 2064 if ((mp1 = copymsg(mp)) == NULL) { 2065 CONN_DEC_REF(acceptor->tcp_connp); 2066 CONN_DEC_REF(eager->tcp_connp); 2067 freemsg(opt_mp); 2068 tcp_err_ack(listener, mp, TSYSERR, ENOMEM); 2069 return; 2070 } 2071 2072 tcr = (struct T_conn_res *)mp1->b_rptr; 2073 2074 /* 2075 * This is an expanded version of mi_tpi_ok_ack_alloc() 2076 * which allocates a larger mblk and appends the new 2077 * local address to the ok_ack. The address is copied by 2078 * soaccept() for getsockname(). 2079 */ 2080 { 2081 int extra; 2082 2083 extra = (eager->tcp_family == AF_INET) ? 2084 sizeof (sin_t) : sizeof (sin6_t); 2085 2086 /* 2087 * Try to re-use mp, if possible. Otherwise, allocate 2088 * an mblk and return it as ok_mp. In any case, mp 2089 * is no longer usable upon return. 2090 */ 2091 if ((ok_mp = mi_tpi_ok_ack_alloc_extra(mp, extra)) == NULL) { 2092 CONN_DEC_REF(acceptor->tcp_connp); 2093 CONN_DEC_REF(eager->tcp_connp); 2094 freemsg(opt_mp); 2095 /* Original mp has been freed by now, so use mp1 */ 2096 tcp_err_ack(listener, mp1, TSYSERR, ENOMEM); 2097 return; 2098 } 2099 2100 mp = NULL; /* We should never use mp after this point */ 2101 2102 switch (extra) { 2103 case sizeof (sin_t): { 2104 sin_t *sin = (sin_t *)ok_mp->b_wptr; 2105 2106 ok_mp->b_wptr += extra; 2107 sin->sin_family = AF_INET; 2108 sin->sin_port = eager->tcp_lport; 2109 sin->sin_addr.s_addr = 2110 eager->tcp_ipha->ipha_src; 2111 break; 2112 } 2113 case sizeof (sin6_t): { 2114 sin6_t *sin6 = (sin6_t *)ok_mp->b_wptr; 2115 2116 ok_mp->b_wptr += extra; 2117 sin6->sin6_family = AF_INET6; 2118 sin6->sin6_port = eager->tcp_lport; 2119 if (eager->tcp_ipversion == IPV4_VERSION) { 2120 sin6->sin6_flowinfo = 0; 2121 IN6_IPADDR_TO_V4MAPPED( 2122 eager->tcp_ipha->ipha_src, 2123 &sin6->sin6_addr); 2124 } else { 2125 ASSERT(eager->tcp_ip6h != NULL); 2126 sin6->sin6_flowinfo = 2127 eager->tcp_ip6h->ip6_vcf & 2128 ~IPV6_VERS_AND_FLOW_MASK; 2129 sin6->sin6_addr = 2130 eager->tcp_ip6h->ip6_src; 2131 } 2132 sin6->sin6_scope_id = 0; 2133 sin6->__sin6_src_id = 0; 2134 break; 2135 } 2136 default: 2137 break; 2138 } 2139 ASSERT(ok_mp->b_wptr <= ok_mp->b_datap->db_lim); 2140 } 2141 2142 /* 2143 * If there are no options we know that the T_CONN_RES will 2144 * succeed. However, we can't send the T_OK_ACK upstream until 2145 * the tcp_accept_swap is done since it would be dangerous to 2146 * let the application start using the new fd prior to the swap. 2147 */ 2148 tcp_accept_swap(listener, acceptor, eager); 2149 2150 /* 2151 * tcp_accept_swap unlinks eager from listener but does not drop 2152 * the eager's reference on the listener. 2153 */ 2154 ASSERT(eager->tcp_listener == NULL); 2155 ASSERT(listener->tcp_connp->conn_ref >= 5); 2156 2157 /* 2158 * The eager is now associated with its own queue. Insert in 2159 * the hash so that the connection can be reused for a future 2160 * T_CONN_RES. 2161 */ 2162 tcp_acceptor_hash_insert(acceptor_id, eager); 2163 2164 /* 2165 * We now do the processing of options with T_CONN_RES. 2166 * We delay till now since we wanted to have queue to pass to 2167 * option processing routines that points back to the right 2168 * instance structure which does not happen until after 2169 * tcp_accept_swap(). 2170 * 2171 * Note: 2172 * The sanity of the logic here assumes that whatever options 2173 * are appropriate to inherit from listner=>eager are done 2174 * before this point, and whatever were to be overridden (or not) 2175 * in transfer logic from eager=>acceptor in tcp_accept_swap(). 2176 * [ Warning: acceptor endpoint can have T_OPTMGMT_REQ done to it 2177 * before its ACCEPTOR_id comes down in T_CONN_RES ] 2178 * This may not be true at this point in time but can be fixed 2179 * independently. This option processing code starts with 2180 * the instantiated acceptor instance and the final queue at 2181 * this point. 2182 */ 2183 2184 if (tcr->OPT_length != 0) { 2185 /* Options to process */ 2186 int t_error = 0; 2187 int sys_error = 0; 2188 int do_disconnect = 0; 2189 2190 if (tcp_conprim_opt_process(eager, mp1, 2191 &do_disconnect, &t_error, &sys_error) < 0) { 2192 eager->tcp_accept_error = 1; 2193 if (do_disconnect) { 2194 /* 2195 * An option failed which does not allow 2196 * connection to be accepted. 2197 * 2198 * We allow T_CONN_RES to succeed and 2199 * put a T_DISCON_IND on the eager queue. 2200 */ 2201 ASSERT(t_error == 0 && sys_error == 0); 2202 eager->tcp_send_discon_ind = 1; 2203 } else { 2204 ASSERT(t_error != 0); 2205 freemsg(ok_mp); 2206 /* 2207 * Original mp was either freed or set 2208 * to ok_mp above, so use mp1 instead. 2209 */ 2210 tcp_err_ack(listener, mp1, t_error, sys_error); 2211 goto finish; 2212 } 2213 } 2214 /* 2215 * Most likely success in setting options (except if 2216 * eager->tcp_send_discon_ind set). 2217 * mp1 option buffer represented by OPT_length/offset 2218 * potentially modified and contains results of setting 2219 * options at this point 2220 */ 2221 } 2222 2223 /* We no longer need mp1, since all options processing has passed */ 2224 freemsg(mp1); 2225 2226 putnext(listener->tcp_rq, ok_mp); 2227 2228 mutex_enter(&listener->tcp_eager_lock); 2229 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 2230 tcp_t *tail; 2231 mblk_t *conn_ind; 2232 2233 /* 2234 * This path should not be executed if listener and 2235 * acceptor streams are the same. 2236 */ 2237 ASSERT(listener != acceptor); 2238 2239 tcp = listener->tcp_eager_prev_q0; 2240 /* 2241 * listener->tcp_eager_prev_q0 points to the TAIL of the 2242 * deferred T_conn_ind queue. We need to get to the head of 2243 * the queue in order to send up T_conn_ind the same order as 2244 * how the 3WHS is completed. 2245 */ 2246 while (tcp != listener) { 2247 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0) 2248 break; 2249 else 2250 tcp = tcp->tcp_eager_prev_q0; 2251 } 2252 ASSERT(tcp != listener); 2253 conn_ind = tcp->tcp_conn.tcp_eager_conn_ind; 2254 ASSERT(conn_ind != NULL); 2255 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 2256 2257 /* Move from q0 to q */ 2258 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 2259 listener->tcp_conn_req_cnt_q0--; 2260 listener->tcp_conn_req_cnt_q++; 2261 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 2262 tcp->tcp_eager_prev_q0; 2263 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 2264 tcp->tcp_eager_next_q0; 2265 tcp->tcp_eager_prev_q0 = NULL; 2266 tcp->tcp_eager_next_q0 = NULL; 2267 tcp->tcp_conn_def_q0 = B_FALSE; 2268 2269 /* Make sure the tcp isn't in the list of droppables */ 2270 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 2271 tcp->tcp_eager_prev_drop_q0 == NULL); 2272 2273 /* 2274 * Insert at end of the queue because sockfs sends 2275 * down T_CONN_RES in chronological order. Leaving 2276 * the older conn indications at front of the queue 2277 * helps reducing search time. 2278 */ 2279 tail = listener->tcp_eager_last_q; 2280 if (tail != NULL) 2281 tail->tcp_eager_next_q = tcp; 2282 else 2283 listener->tcp_eager_next_q = tcp; 2284 listener->tcp_eager_last_q = tcp; 2285 tcp->tcp_eager_next_q = NULL; 2286 mutex_exit(&listener->tcp_eager_lock); 2287 putnext(tcp->tcp_rq, conn_ind); 2288 } else { 2289 mutex_exit(&listener->tcp_eager_lock); 2290 } 2291 2292 /* 2293 * Done with the acceptor - free it 2294 * 2295 * Note: from this point on, no access to listener should be made 2296 * as listener can be equal to acceptor. 2297 */ 2298 finish: 2299 ASSERT(acceptor->tcp_detached); 2300 ASSERT(tcps->tcps_g_q != NULL); 2301 ASSERT(!IPCL_IS_NONSTR(acceptor->tcp_connp)); 2302 acceptor->tcp_rq = tcps->tcps_g_q; 2303 acceptor->tcp_wq = WR(tcps->tcps_g_q); 2304 (void) tcp_clean_death(acceptor, 0, 2); 2305 CONN_DEC_REF(acceptor->tcp_connp); 2306 2307 /* 2308 * In case we already received a FIN we have to make tcp_rput send 2309 * the ordrel_ind. This will also send up a window update if the window 2310 * has opened up. 2311 * 2312 * In the normal case of a successful connection acceptance 2313 * we give the O_T_BIND_REQ to the read side put procedure as an 2314 * indication that this was just accepted. This tells tcp_rput to 2315 * pass up any data queued in tcp_rcv_list. 2316 * 2317 * In the fringe case where options sent with T_CONN_RES failed and 2318 * we required, we would be indicating a T_DISCON_IND to blow 2319 * away this connection. 2320 */ 2321 2322 /* 2323 * XXX: we currently have a problem if XTI application closes the 2324 * acceptor stream in between. This problem exists in on10-gate also 2325 * and is well know but nothing can be done short of major rewrite 2326 * to fix it. Now it is possible to take care of it by assigning TLI/XTI 2327 * eager same squeue as listener (we can distinguish non socket 2328 * listeners at the time of handling a SYN in tcp_conn_request) 2329 * and do most of the work that tcp_accept_finish does here itself 2330 * and then get behind the acceptor squeue to access the acceptor 2331 * queue. 2332 */ 2333 /* 2334 * We already have a ref on tcp so no need to do one before squeue_enter 2335 */ 2336 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, opt_mp, tcp_accept_finish, 2337 eager->tcp_connp, SQ_FILL, SQTAG_TCP_ACCEPT_FINISH); 2338 } 2339 2340 /* 2341 * Swap information between the eager and acceptor for a TLI/XTI client. 2342 * The sockfs accept is done on the acceptor stream and control goes 2343 * through tcp_wput_accept() and tcp_accept()/tcp_accept_swap() is not 2344 * called. In either case, both the eager and listener are in their own 2345 * perimeter (squeue) and the code has to deal with potential race. 2346 * 2347 * See the block comment on top of tcp_accept() and tcp_wput_accept(). 2348 */ 2349 static void 2350 tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, tcp_t *eager) 2351 { 2352 conn_t *econnp, *aconnp; 2353 2354 ASSERT(eager->tcp_rq == listener->tcp_rq); 2355 ASSERT(eager->tcp_detached && !acceptor->tcp_detached); 2356 ASSERT(!eager->tcp_hard_bound); 2357 ASSERT(!TCP_IS_SOCKET(acceptor)); 2358 ASSERT(!TCP_IS_SOCKET(eager)); 2359 ASSERT(!TCP_IS_SOCKET(listener)); 2360 2361 acceptor->tcp_detached = B_TRUE; 2362 /* 2363 * To permit stream re-use by TLI/XTI, the eager needs a copy of 2364 * the acceptor id. 2365 */ 2366 eager->tcp_acceptor_id = acceptor->tcp_acceptor_id; 2367 2368 /* remove eager from listen list... */ 2369 mutex_enter(&listener->tcp_eager_lock); 2370 tcp_eager_unlink(eager); 2371 ASSERT(eager->tcp_eager_next_q == NULL && 2372 eager->tcp_eager_last_q == NULL); 2373 ASSERT(eager->tcp_eager_next_q0 == NULL && 2374 eager->tcp_eager_prev_q0 == NULL); 2375 mutex_exit(&listener->tcp_eager_lock); 2376 eager->tcp_rq = acceptor->tcp_rq; 2377 eager->tcp_wq = acceptor->tcp_wq; 2378 2379 econnp = eager->tcp_connp; 2380 aconnp = acceptor->tcp_connp; 2381 2382 eager->tcp_rq->q_ptr = econnp; 2383 eager->tcp_wq->q_ptr = econnp; 2384 2385 /* 2386 * In the TLI/XTI loopback case, we are inside the listener's squeue, 2387 * which might be a different squeue from our peer TCP instance. 2388 * For TCP Fusion, the peer expects that whenever tcp_detached is 2389 * clear, our TCP queues point to the acceptor's queues. Thus, use 2390 * membar_producer() to ensure that the assignments of tcp_rq/tcp_wq 2391 * above reach global visibility prior to the clearing of tcp_detached. 2392 */ 2393 membar_producer(); 2394 eager->tcp_detached = B_FALSE; 2395 2396 ASSERT(eager->tcp_ack_tid == 0); 2397 2398 econnp->conn_dev = aconnp->conn_dev; 2399 econnp->conn_minor_arena = aconnp->conn_minor_arena; 2400 ASSERT(econnp->conn_minor_arena != NULL); 2401 if (eager->tcp_cred != NULL) 2402 crfree(eager->tcp_cred); 2403 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2404 ASSERT(econnp->conn_netstack == aconnp->conn_netstack); 2405 ASSERT(eager->tcp_tcps == acceptor->tcp_tcps); 2406 2407 aconnp->conn_cred = NULL; 2408 2409 econnp->conn_zoneid = aconnp->conn_zoneid; 2410 econnp->conn_allzones = aconnp->conn_allzones; 2411 2412 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2413 aconnp->conn_mac_exempt = B_FALSE; 2414 2415 ASSERT(aconnp->conn_peercred == NULL); 2416 2417 /* Do the IPC initialization */ 2418 CONN_INC_REF(econnp); 2419 2420 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2421 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2422 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2423 2424 /* Done with old IPC. Drop its ref on its connp */ 2425 CONN_DEC_REF(aconnp); 2426 } 2427 2428 2429 /* 2430 * Adapt to the information, such as rtt and rtt_sd, provided from the 2431 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2432 * 2433 * Checks for multicast and broadcast destination address. 2434 * Returns zero on failure; non-zero if ok. 2435 * 2436 * Note that the MSS calculation here is based on the info given in 2437 * the IRE. We do not do any calculation based on TCP options. They 2438 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2439 * knows which options to use. 2440 * 2441 * Note on how TCP gets its parameters for a connection. 2442 * 2443 * When a tcp_t structure is allocated, it gets all the default parameters. 2444 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2445 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2446 * default. 2447 * 2448 * An incoming SYN with a multicast or broadcast destination address, is dropped 2449 * in 1 of 2 places. 2450 * 2451 * 1. If the packet was received over the wire it is dropped in 2452 * ip_rput_process_broadcast() 2453 * 2454 * 2. If the packet was received through internal IP loopback, i.e. the packet 2455 * was generated and received on the same machine, it is dropped in 2456 * ip_wput_local() 2457 * 2458 * An incoming SYN with a multicast or broadcast source address is always 2459 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2460 * reject an attempt to connect to a broadcast or multicast (destination) 2461 * address. 2462 */ 2463 static int 2464 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2465 { 2466 ire_t *ire; 2467 ire_t *sire = NULL; 2468 iulp_t *ire_uinfo = NULL; 2469 uint32_t mss_max; 2470 uint32_t mss; 2471 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2472 conn_t *connp = tcp->tcp_connp; 2473 boolean_t ire_cacheable = B_FALSE; 2474 zoneid_t zoneid = connp->conn_zoneid; 2475 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2476 MATCH_IRE_SECATTR; 2477 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2478 ill_t *ill = NULL; 2479 boolean_t incoming = (ire_mp == NULL); 2480 tcp_stack_t *tcps = tcp->tcp_tcps; 2481 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 2482 2483 ASSERT(connp->conn_ire_cache == NULL); 2484 2485 if (tcp->tcp_ipversion == IPV4_VERSION) { 2486 2487 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2488 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 2489 return (0); 2490 } 2491 /* 2492 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2493 * for the destination with the nexthop as gateway. 2494 * ire_ctable_lookup() is used because this particular 2495 * ire, if it exists, will be marked private. 2496 * If that is not available, use the interface ire 2497 * for the nexthop. 2498 * 2499 * TSol: tcp_update_label will detect label mismatches based 2500 * only on the destination's label, but that would not 2501 * detect label mismatches based on the security attributes 2502 * of routes or next hop gateway. Hence we need to pass the 2503 * label to ire_ftable_lookup below in order to locate the 2504 * right prefix (and/or) ire cache. Similarly we also need 2505 * pass the label to the ire_cache_lookup below to locate 2506 * the right ire that also matches on the label. 2507 */ 2508 if (tcp->tcp_connp->conn_nexthop_set) { 2509 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2510 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2511 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, 2512 ipst); 2513 if (ire == NULL) { 2514 ire = ire_ftable_lookup( 2515 tcp->tcp_connp->conn_nexthop_v4, 2516 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2517 tsl, match_flags, ipst); 2518 if (ire == NULL) 2519 return (0); 2520 } else { 2521 ire_uinfo = &ire->ire_uinfo; 2522 } 2523 } else { 2524 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2525 zoneid, tsl, ipst); 2526 if (ire != NULL) { 2527 ire_cacheable = B_TRUE; 2528 ire_uinfo = (ire_mp != NULL) ? 2529 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2530 &ire->ire_uinfo; 2531 2532 } else { 2533 if (ire_mp == NULL) { 2534 ire = ire_ftable_lookup( 2535 tcp->tcp_connp->conn_rem, 2536 0, 0, 0, NULL, &sire, zoneid, 0, 2537 tsl, (MATCH_IRE_RECURSIVE | 2538 MATCH_IRE_DEFAULT), ipst); 2539 if (ire == NULL) 2540 return (0); 2541 ire_uinfo = (sire != NULL) ? 2542 &sire->ire_uinfo : 2543 &ire->ire_uinfo; 2544 } else { 2545 ire = (ire_t *)ire_mp->b_rptr; 2546 ire_uinfo = 2547 &((ire_t *) 2548 ire_mp->b_rptr)->ire_uinfo; 2549 } 2550 } 2551 } 2552 ASSERT(ire != NULL); 2553 2554 if ((ire->ire_src_addr == INADDR_ANY) || 2555 (ire->ire_type & IRE_BROADCAST)) { 2556 /* 2557 * ire->ire_mp is non null when ire_mp passed in is used 2558 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2559 */ 2560 if (ire->ire_mp == NULL) 2561 ire_refrele(ire); 2562 if (sire != NULL) 2563 ire_refrele(sire); 2564 return (0); 2565 } 2566 2567 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2568 ipaddr_t src_addr; 2569 2570 /* 2571 * ip_bind_connected() has stored the correct source 2572 * address in conn_src. 2573 */ 2574 src_addr = tcp->tcp_connp->conn_src; 2575 tcp->tcp_ipha->ipha_src = src_addr; 2576 /* 2577 * Copy of the src addr. in tcp_t is needed 2578 * for the lookup funcs. 2579 */ 2580 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2581 } 2582 /* 2583 * Set the fragment bit so that IP will tell us if the MTU 2584 * should change. IP tells us the latest setting of 2585 * ip_path_mtu_discovery through ire_frag_flag. 2586 */ 2587 if (ipst->ips_ip_path_mtu_discovery) { 2588 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2589 htons(IPH_DF); 2590 } 2591 /* 2592 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2593 * for IP_NEXTHOP. No cache ire has been found for the 2594 * destination and we are working with the nexthop's 2595 * interface ire. Since we need to forward all packets 2596 * to the nexthop first, we "blindly" set tcp_localnet 2597 * to false, eventhough the destination may also be 2598 * onlink. 2599 */ 2600 if (ire_uinfo == NULL) 2601 tcp->tcp_localnet = 0; 2602 else 2603 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2604 } else { 2605 /* 2606 * For incoming connection ire_mp = NULL 2607 * For outgoing connection ire_mp != NULL 2608 * Technically we should check conn_incoming_ill 2609 * when ire_mp is NULL and conn_outgoing_ill when 2610 * ire_mp is non-NULL. But this is performance 2611 * critical path and for IPV*_BOUND_IF, outgoing 2612 * and incoming ill are always set to the same value. 2613 */ 2614 ill_t *dst_ill = NULL; 2615 ipif_t *dst_ipif = NULL; 2616 2617 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2618 2619 if (connp->conn_outgoing_ill != NULL) { 2620 /* Outgoing or incoming path */ 2621 int err; 2622 2623 dst_ill = conn_get_held_ill(connp, 2624 &connp->conn_outgoing_ill, &err); 2625 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2626 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2627 return (0); 2628 } 2629 match_flags |= MATCH_IRE_ILL; 2630 dst_ipif = dst_ill->ill_ipif; 2631 } 2632 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2633 0, 0, dst_ipif, zoneid, tsl, match_flags, ipst); 2634 2635 if (ire != NULL) { 2636 ire_cacheable = B_TRUE; 2637 ire_uinfo = (ire_mp != NULL) ? 2638 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2639 &ire->ire_uinfo; 2640 } else { 2641 if (ire_mp == NULL) { 2642 ire = ire_ftable_lookup_v6( 2643 &tcp->tcp_connp->conn_remv6, 2644 0, 0, 0, dst_ipif, &sire, zoneid, 2645 0, tsl, match_flags, ipst); 2646 if (ire == NULL) { 2647 if (dst_ill != NULL) 2648 ill_refrele(dst_ill); 2649 return (0); 2650 } 2651 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2652 &ire->ire_uinfo; 2653 } else { 2654 ire = (ire_t *)ire_mp->b_rptr; 2655 ire_uinfo = 2656 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2657 } 2658 } 2659 if (dst_ill != NULL) 2660 ill_refrele(dst_ill); 2661 2662 ASSERT(ire != NULL); 2663 ASSERT(ire_uinfo != NULL); 2664 2665 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2666 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2667 /* 2668 * ire->ire_mp is non null when ire_mp passed in is used 2669 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2670 */ 2671 if (ire->ire_mp == NULL) 2672 ire_refrele(ire); 2673 if (sire != NULL) 2674 ire_refrele(sire); 2675 return (0); 2676 } 2677 2678 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2679 in6_addr_t src_addr; 2680 2681 /* 2682 * ip_bind_connected_v6() has stored the correct source 2683 * address per IPv6 addr. selection policy in 2684 * conn_src_v6. 2685 */ 2686 src_addr = tcp->tcp_connp->conn_srcv6; 2687 2688 tcp->tcp_ip6h->ip6_src = src_addr; 2689 /* 2690 * Copy of the src addr. in tcp_t is needed 2691 * for the lookup funcs. 2692 */ 2693 tcp->tcp_ip_src_v6 = src_addr; 2694 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2695 &connp->conn_srcv6)); 2696 } 2697 tcp->tcp_localnet = 2698 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2699 } 2700 2701 /* 2702 * This allows applications to fail quickly when connections are made 2703 * to dead hosts. Hosts can be labeled dead by adding a reject route 2704 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2705 */ 2706 if ((ire->ire_flags & RTF_REJECT) && 2707 (ire->ire_flags & RTF_PRIVATE)) 2708 goto error; 2709 2710 /* 2711 * Make use of the cached rtt and rtt_sd values to calculate the 2712 * initial RTO. Note that they are already initialized in 2713 * tcp_init_values(). 2714 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2715 * IP_NEXTHOP, but instead are using the interface ire for the 2716 * nexthop, then we do not use the ire_uinfo from that ire to 2717 * do any initializations. 2718 */ 2719 if (ire_uinfo != NULL) { 2720 if (ire_uinfo->iulp_rtt != 0) { 2721 clock_t rto; 2722 2723 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2724 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2725 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2726 tcps->tcps_rexmit_interval_extra + 2727 (tcp->tcp_rtt_sa >> 5); 2728 2729 if (rto > tcps->tcps_rexmit_interval_max) { 2730 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 2731 } else if (rto < tcps->tcps_rexmit_interval_min) { 2732 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 2733 } else { 2734 tcp->tcp_rto = rto; 2735 } 2736 } 2737 if (ire_uinfo->iulp_ssthresh != 0) 2738 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2739 else 2740 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2741 if (ire_uinfo->iulp_spipe > 0) { 2742 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2743 tcps->tcps_max_buf); 2744 if (tcps->tcps_snd_lowat_fraction != 0) 2745 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2746 tcps->tcps_snd_lowat_fraction; 2747 (void) tcp_maxpsz_set(tcp, B_TRUE); 2748 } 2749 /* 2750 * Note that up till now, acceptor always inherits receive 2751 * window from the listener. But if there is a metrics 2752 * associated with a host, we should use that instead of 2753 * inheriting it from listener. Thus we need to pass this 2754 * info back to the caller. 2755 */ 2756 if (ire_uinfo->iulp_rpipe > 0) { 2757 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, 2758 tcps->tcps_max_buf); 2759 } 2760 2761 if (ire_uinfo->iulp_rtomax > 0) { 2762 tcp->tcp_second_timer_threshold = 2763 ire_uinfo->iulp_rtomax; 2764 } 2765 2766 /* 2767 * Use the metric option settings, iulp_tstamp_ok and 2768 * iulp_wscale_ok, only for active open. What this means 2769 * is that if the other side uses timestamp or window 2770 * scale option, TCP will also use those options. That 2771 * is for passive open. If the application sets a 2772 * large window, window scale is enabled regardless of 2773 * the value in iulp_wscale_ok. This is the behavior 2774 * since 2.6. So we keep it. 2775 * The only case left in passive open processing is the 2776 * check for SACK. 2777 * For ECN, it should probably be like SACK. But the 2778 * current value is binary, so we treat it like the other 2779 * cases. The metric only controls active open.For passive 2780 * open, the ndd param, tcp_ecn_permitted, controls the 2781 * behavior. 2782 */ 2783 if (!tcp_detached) { 2784 /* 2785 * The if check means that the following can only 2786 * be turned on by the metrics only IRE, but not off. 2787 */ 2788 if (ire_uinfo->iulp_tstamp_ok) 2789 tcp->tcp_snd_ts_ok = B_TRUE; 2790 if (ire_uinfo->iulp_wscale_ok) 2791 tcp->tcp_snd_ws_ok = B_TRUE; 2792 if (ire_uinfo->iulp_sack == 2) 2793 tcp->tcp_snd_sack_ok = B_TRUE; 2794 if (ire_uinfo->iulp_ecn_ok) 2795 tcp->tcp_ecn_ok = B_TRUE; 2796 } else { 2797 /* 2798 * Passive open. 2799 * 2800 * As above, the if check means that SACK can only be 2801 * turned on by the metric only IRE. 2802 */ 2803 if (ire_uinfo->iulp_sack > 0) { 2804 tcp->tcp_snd_sack_ok = B_TRUE; 2805 } 2806 } 2807 } 2808 2809 2810 /* 2811 * XXX: Note that currently, ire_max_frag can be as small as 68 2812 * because of PMTUd. So tcp_mss may go to negative if combined 2813 * length of all those options exceeds 28 bytes. But because 2814 * of the tcp_mss_min check below, we may not have a problem if 2815 * tcp_mss_min is of a reasonable value. The default is 1 so 2816 * the negative problem still exists. And the check defeats PMTUd. 2817 * In fact, if PMTUd finds that the MSS should be smaller than 2818 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2819 * value. 2820 * 2821 * We do not deal with that now. All those problems related to 2822 * PMTUd will be fixed later. 2823 */ 2824 ASSERT(ire->ire_max_frag != 0); 2825 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2826 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2827 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2828 mss = MIN(mss, IPV6_MIN_MTU); 2829 } 2830 } 2831 2832 /* Sanity check for MSS value. */ 2833 if (tcp->tcp_ipversion == IPV4_VERSION) 2834 mss_max = tcps->tcps_mss_max_ipv4; 2835 else 2836 mss_max = tcps->tcps_mss_max_ipv6; 2837 2838 if (tcp->tcp_ipversion == IPV6_VERSION && 2839 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2840 /* 2841 * After receiving an ICMPv6 "packet too big" message with a 2842 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2843 * will insert a 8-byte fragment header in every packet; we 2844 * reduce the MSS by that amount here. 2845 */ 2846 mss -= sizeof (ip6_frag_t); 2847 } 2848 2849 if (tcp->tcp_ipsec_overhead == 0) 2850 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2851 2852 mss -= tcp->tcp_ipsec_overhead; 2853 2854 if (mss < tcps->tcps_mss_min) 2855 mss = tcps->tcps_mss_min; 2856 if (mss > mss_max) 2857 mss = mss_max; 2858 2859 /* Note that this is the maximum MSS, excluding all options. */ 2860 tcp->tcp_mss = mss; 2861 2862 /* 2863 * Initialize the ISS here now that we have the full connection ID. 2864 * The RFC 1948 method of initial sequence number generation requires 2865 * knowledge of the full connection ID before setting the ISS. 2866 */ 2867 2868 tcp_iss_init(tcp); 2869 2870 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2871 tcp->tcp_loopback = B_TRUE; 2872 2873 if (sire != NULL) 2874 IRE_REFRELE(sire); 2875 2876 /* 2877 * If we got an IRE_CACHE and an ILL, go through their properties; 2878 * otherwise, this is deferred until later when we have an IRE_CACHE. 2879 */ 2880 if (tcp->tcp_loopback || 2881 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2882 /* 2883 * For incoming, see if this tcp may be MDT-capable. For 2884 * outgoing, this process has been taken care of through 2885 * tcp_rput_other. 2886 */ 2887 tcp_ire_ill_check(tcp, ire, ill, incoming); 2888 tcp->tcp_ire_ill_check_done = B_TRUE; 2889 } 2890 2891 mutex_enter(&connp->conn_lock); 2892 /* 2893 * Make sure that conn is not marked incipient 2894 * for incoming connections. A blind 2895 * removal of incipient flag is cheaper than 2896 * check and removal. 2897 */ 2898 connp->conn_state_flags &= ~CONN_INCIPIENT; 2899 2900 /* 2901 * Must not cache forwarding table routes 2902 * or recache an IRE after the conn_t has 2903 * had conn_ire_cache cleared and is flagged 2904 * unusable, (see the CONN_CACHE_IRE() macro). 2905 */ 2906 if (ire_cacheable && CONN_CACHE_IRE(connp)) { 2907 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2908 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2909 connp->conn_ire_cache = ire; 2910 IRE_UNTRACE_REF(ire); 2911 rw_exit(&ire->ire_bucket->irb_lock); 2912 mutex_exit(&connp->conn_lock); 2913 return (1); 2914 } 2915 rw_exit(&ire->ire_bucket->irb_lock); 2916 } 2917 mutex_exit(&connp->conn_lock); 2918 2919 if (ire->ire_mp == NULL) 2920 ire_refrele(ire); 2921 return (1); 2922 2923 error: 2924 if (ire->ire_mp == NULL) 2925 ire_refrele(ire); 2926 if (sire != NULL) 2927 ire_refrele(sire); 2928 return (0); 2929 } 2930 2931 static void 2932 tcp_tpi_bind(tcp_t *tcp, mblk_t *mp) 2933 { 2934 int error; 2935 conn_t *connp = tcp->tcp_connp; 2936 struct sockaddr *sa; 2937 mblk_t *mp1; 2938 struct T_bind_req *tbr; 2939 int backlog; 2940 socklen_t len; 2941 sin_t *sin; 2942 sin6_t *sin6; 2943 cred_t *cr; 2944 2945 /* 2946 * All Solaris components should pass a db_credp 2947 * for this TPI message, hence we ASSERT. 2948 * But in case there is some other M_PROTO that looks 2949 * like a TPI message sent by some other kernel 2950 * component, we check and return an error. 2951 */ 2952 cr = msg_getcred(mp, NULL); 2953 ASSERT(cr != NULL); 2954 if (cr == NULL) { 2955 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 2956 return; 2957 } 2958 2959 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2960 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2961 if (tcp->tcp_debug) { 2962 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2963 "tcp_tpi_bind: bad req, len %u", 2964 (uint_t)(mp->b_wptr - mp->b_rptr)); 2965 } 2966 tcp_err_ack(tcp, mp, TPROTO, 0); 2967 return; 2968 } 2969 /* Make sure the largest address fits */ 2970 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2971 if (mp1 == NULL) { 2972 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 2973 return; 2974 } 2975 mp = mp1; 2976 tbr = (struct T_bind_req *)mp->b_rptr; 2977 2978 backlog = tbr->CONIND_number; 2979 len = tbr->ADDR_length; 2980 2981 switch (len) { 2982 case 0: /* request for a generic port */ 2983 tbr->ADDR_offset = sizeof (struct T_bind_req); 2984 if (tcp->tcp_family == AF_INET) { 2985 tbr->ADDR_length = sizeof (sin_t); 2986 sin = (sin_t *)&tbr[1]; 2987 *sin = sin_null; 2988 sin->sin_family = AF_INET; 2989 sa = (struct sockaddr *)sin; 2990 len = sizeof (sin_t); 2991 mp->b_wptr = (uchar_t *)&sin[1]; 2992 } else { 2993 ASSERT(tcp->tcp_family == AF_INET6); 2994 tbr->ADDR_length = sizeof (sin6_t); 2995 sin6 = (sin6_t *)&tbr[1]; 2996 *sin6 = sin6_null; 2997 sin6->sin6_family = AF_INET6; 2998 sa = (struct sockaddr *)sin6; 2999 len = sizeof (sin6_t); 3000 mp->b_wptr = (uchar_t *)&sin6[1]; 3001 } 3002 break; 3003 3004 case sizeof (sin_t): /* Complete IPv4 address */ 3005 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset, 3006 sizeof (sin_t)); 3007 break; 3008 3009 case sizeof (sin6_t): /* Complete IPv6 address */ 3010 sa = (struct sockaddr *)mi_offset_param(mp, 3011 tbr->ADDR_offset, sizeof (sin6_t)); 3012 break; 3013 3014 default: 3015 if (tcp->tcp_debug) { 3016 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3017 "tcp_tpi_bind: bad address length, %d", 3018 tbr->ADDR_length); 3019 } 3020 tcp_err_ack(tcp, mp, TBADADDR, 0); 3021 return; 3022 } 3023 3024 if (backlog > 0) { 3025 error = tcp_do_listen(connp, sa, len, backlog, DB_CRED(mp), 3026 tbr->PRIM_type != O_T_BIND_REQ); 3027 } else { 3028 error = tcp_do_bind(connp, sa, len, DB_CRED(mp), 3029 tbr->PRIM_type != O_T_BIND_REQ); 3030 } 3031 done: 3032 if (error > 0) { 3033 tcp_err_ack(tcp, mp, TSYSERR, error); 3034 } else if (error < 0) { 3035 tcp_err_ack(tcp, mp, -error, 0); 3036 } else { 3037 /* 3038 * Update port information as sockfs/tpi needs it for checking 3039 */ 3040 if (tcp->tcp_family == AF_INET) { 3041 sin = (sin_t *)sa; 3042 sin->sin_port = tcp->tcp_lport; 3043 } else { 3044 sin6 = (sin6_t *)sa; 3045 sin6->sin6_port = tcp->tcp_lport; 3046 } 3047 mp->b_datap->db_type = M_PCPROTO; 3048 tbr->PRIM_type = T_BIND_ACK; 3049 putnext(tcp->tcp_rq, mp); 3050 } 3051 } 3052 3053 /* 3054 * If the "bind_to_req_port_only" parameter is set, if the requested port 3055 * number is available, return it, If not return 0 3056 * 3057 * If "bind_to_req_port_only" parameter is not set and 3058 * If the requested port number is available, return it. If not, return 3059 * the first anonymous port we happen across. If no anonymous ports are 3060 * available, return 0. addr is the requested local address, if any. 3061 * 3062 * In either case, when succeeding update the tcp_t to record the port number 3063 * and insert it in the bind hash table. 3064 * 3065 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3066 * without setting SO_REUSEADDR. This is needed so that they 3067 * can be viewed as two independent transport protocols. 3068 */ 3069 static in_port_t 3070 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3071 int reuseaddr, boolean_t quick_connect, 3072 boolean_t bind_to_req_port_only, boolean_t user_specified) 3073 { 3074 /* number of times we have run around the loop */ 3075 int count = 0; 3076 /* maximum number of times to run around the loop */ 3077 int loopmax; 3078 conn_t *connp = tcp->tcp_connp; 3079 zoneid_t zoneid = connp->conn_zoneid; 3080 tcp_stack_t *tcps = tcp->tcp_tcps; 3081 3082 /* 3083 * Lookup for free addresses is done in a loop and "loopmax" 3084 * influences how long we spin in the loop 3085 */ 3086 if (bind_to_req_port_only) { 3087 /* 3088 * If the requested port is busy, don't bother to look 3089 * for a new one. Setting loop maximum count to 1 has 3090 * that effect. 3091 */ 3092 loopmax = 1; 3093 } else { 3094 /* 3095 * If the requested port is busy, look for a free one 3096 * in the anonymous port range. 3097 * Set loopmax appropriately so that one does not look 3098 * forever in the case all of the anonymous ports are in use. 3099 */ 3100 if (tcp->tcp_anon_priv_bind) { 3101 /* 3102 * loopmax = 3103 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3104 */ 3105 loopmax = IPPORT_RESERVED - 3106 tcps->tcps_min_anonpriv_port; 3107 } else { 3108 loopmax = (tcps->tcps_largest_anon_port - 3109 tcps->tcps_smallest_anon_port + 1); 3110 } 3111 } 3112 do { 3113 uint16_t lport; 3114 tf_t *tbf; 3115 tcp_t *ltcp; 3116 conn_t *lconnp; 3117 3118 lport = htons(port); 3119 3120 /* 3121 * Ensure that the tcp_t is not currently in the bind hash. 3122 * Hold the lock on the hash bucket to ensure that 3123 * the duplicate check plus the insertion is an atomic 3124 * operation. 3125 * 3126 * This function does an inline lookup on the bind hash list 3127 * Make sure that we access only members of tcp_t 3128 * and that we don't look at tcp_tcp, since we are not 3129 * doing a CONN_INC_REF. 3130 */ 3131 tcp_bind_hash_remove(tcp); 3132 tbf = &tcps->tcps_bind_fanout[TCP_BIND_HASH(lport)]; 3133 mutex_enter(&tbf->tf_lock); 3134 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3135 ltcp = ltcp->tcp_bind_hash) { 3136 if (lport == ltcp->tcp_lport) 3137 break; 3138 } 3139 3140 for (; ltcp != NULL; ltcp = ltcp->tcp_bind_hash_port) { 3141 boolean_t not_socket; 3142 boolean_t exclbind; 3143 3144 lconnp = ltcp->tcp_connp; 3145 3146 /* 3147 * On a labeled system, we must treat bindings to ports 3148 * on shared IP addresses by sockets with MAC exemption 3149 * privilege as being in all zones, as there's 3150 * otherwise no way to identify the right receiver. 3151 */ 3152 if (!(IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) || 3153 IPCL_ZONE_MATCH(connp, 3154 ltcp->tcp_connp->conn_zoneid)) && 3155 !lconnp->conn_mac_exempt && 3156 !connp->conn_mac_exempt) 3157 continue; 3158 3159 /* 3160 * If TCP_EXCLBIND is set for either the bound or 3161 * binding endpoint, the semantics of bind 3162 * is changed according to the following. 3163 * 3164 * spec = specified address (v4 or v6) 3165 * unspec = unspecified address (v4 or v6) 3166 * A = specified addresses are different for endpoints 3167 * 3168 * bound bind to allowed 3169 * ------------------------------------- 3170 * unspec unspec no 3171 * unspec spec no 3172 * spec unspec no 3173 * spec spec yes if A 3174 * 3175 * For labeled systems, SO_MAC_EXEMPT behaves the same 3176 * as TCP_EXCLBIND, except that zoneid is ignored. 3177 * 3178 * Note: 3179 * 3180 * 1. Because of TLI semantics, an endpoint can go 3181 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3182 * TCPS_BOUND, depending on whether it is originally 3183 * a listener or not. That is why we need to check 3184 * for states greater than or equal to TCPS_BOUND 3185 * here. 3186 * 3187 * 2. Ideally, we should only check for state equals 3188 * to TCPS_LISTEN. And the following check should be 3189 * added. 3190 * 3191 * if (ltcp->tcp_state == TCPS_LISTEN || 3192 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3193 * ... 3194 * } 3195 * 3196 * The semantics will be changed to this. If the 3197 * endpoint on the list is in state not equal to 3198 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3199 * set, let the bind succeed. 3200 * 3201 * Because of (1), we cannot do that for TLI 3202 * endpoints. But we can do that for socket endpoints. 3203 * If in future, we can change this going back 3204 * semantics, we can use the above check for TLI also. 3205 */ 3206 not_socket = !(TCP_IS_SOCKET(ltcp) && 3207 TCP_IS_SOCKET(tcp)); 3208 exclbind = ltcp->tcp_exclbind || tcp->tcp_exclbind; 3209 3210 if (lconnp->conn_mac_exempt || connp->conn_mac_exempt || 3211 (exclbind && (not_socket || 3212 ltcp->tcp_state <= TCPS_ESTABLISHED))) { 3213 if (V6_OR_V4_INADDR_ANY( 3214 ltcp->tcp_bound_source_v6) || 3215 V6_OR_V4_INADDR_ANY(*laddr) || 3216 IN6_ARE_ADDR_EQUAL(laddr, 3217 <cp->tcp_bound_source_v6)) { 3218 break; 3219 } 3220 continue; 3221 } 3222 3223 /* 3224 * Check ipversion to allow IPv4 and IPv6 sockets to 3225 * have disjoint port number spaces, if *_EXCLBIND 3226 * is not set and only if the application binds to a 3227 * specific port. We use the same autoassigned port 3228 * number space for IPv4 and IPv6 sockets. 3229 */ 3230 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3231 bind_to_req_port_only) 3232 continue; 3233 3234 /* 3235 * Ideally, we should make sure that the source 3236 * address, remote address, and remote port in the 3237 * four tuple for this tcp-connection is unique. 3238 * However, trying to find out the local source 3239 * address would require too much code duplication 3240 * with IP, since IP needs needs to have that code 3241 * to support userland TCP implementations. 3242 */ 3243 if (quick_connect && 3244 (ltcp->tcp_state > TCPS_LISTEN) && 3245 ((tcp->tcp_fport != ltcp->tcp_fport) || 3246 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3247 <cp->tcp_remote_v6))) 3248 continue; 3249 3250 if (!reuseaddr) { 3251 /* 3252 * No socket option SO_REUSEADDR. 3253 * If existing port is bound to 3254 * a non-wildcard IP address 3255 * and the requesting stream is 3256 * bound to a distinct 3257 * different IP addresses 3258 * (non-wildcard, also), keep 3259 * going. 3260 */ 3261 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3262 !V6_OR_V4_INADDR_ANY( 3263 ltcp->tcp_bound_source_v6) && 3264 !IN6_ARE_ADDR_EQUAL(laddr, 3265 <cp->tcp_bound_source_v6)) 3266 continue; 3267 if (ltcp->tcp_state >= TCPS_BOUND) { 3268 /* 3269 * This port is being used and 3270 * its state is >= TCPS_BOUND, 3271 * so we can't bind to it. 3272 */ 3273 break; 3274 } 3275 } else { 3276 /* 3277 * socket option SO_REUSEADDR is set on the 3278 * binding tcp_t. 3279 * 3280 * If two streams are bound to 3281 * same IP address or both addr 3282 * and bound source are wildcards 3283 * (INADDR_ANY), we want to stop 3284 * searching. 3285 * We have found a match of IP source 3286 * address and source port, which is 3287 * refused regardless of the 3288 * SO_REUSEADDR setting, so we break. 3289 */ 3290 if (IN6_ARE_ADDR_EQUAL(laddr, 3291 <cp->tcp_bound_source_v6) && 3292 (ltcp->tcp_state == TCPS_LISTEN || 3293 ltcp->tcp_state == TCPS_BOUND)) 3294 break; 3295 } 3296 } 3297 if (ltcp != NULL) { 3298 /* The port number is busy */ 3299 mutex_exit(&tbf->tf_lock); 3300 } else { 3301 /* 3302 * This port is ours. Insert in fanout and mark as 3303 * bound to prevent others from getting the port 3304 * number. 3305 */ 3306 tcp->tcp_state = TCPS_BOUND; 3307 tcp->tcp_lport = htons(port); 3308 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3309 3310 ASSERT(&tcps->tcps_bind_fanout[TCP_BIND_HASH( 3311 tcp->tcp_lport)] == tbf); 3312 tcp_bind_hash_insert(tbf, tcp, 1); 3313 3314 mutex_exit(&tbf->tf_lock); 3315 3316 /* 3317 * We don't want tcp_next_port_to_try to "inherit" 3318 * a port number supplied by the user in a bind. 3319 */ 3320 if (user_specified) 3321 return (port); 3322 3323 /* 3324 * This is the only place where tcp_next_port_to_try 3325 * is updated. After the update, it may or may not 3326 * be in the valid range. 3327 */ 3328 if (!tcp->tcp_anon_priv_bind) 3329 tcps->tcps_next_port_to_try = port + 1; 3330 return (port); 3331 } 3332 3333 if (tcp->tcp_anon_priv_bind) { 3334 port = tcp_get_next_priv_port(tcp); 3335 } else { 3336 if (count == 0 && user_specified) { 3337 /* 3338 * We may have to return an anonymous port. So 3339 * get one to start with. 3340 */ 3341 port = 3342 tcp_update_next_port( 3343 tcps->tcps_next_port_to_try, 3344 tcp, B_TRUE); 3345 user_specified = B_FALSE; 3346 } else { 3347 port = tcp_update_next_port(port + 1, tcp, 3348 B_FALSE); 3349 } 3350 } 3351 if (port == 0) 3352 break; 3353 3354 /* 3355 * Don't let this loop run forever in the case where 3356 * all of the anonymous ports are in use. 3357 */ 3358 } while (++count < loopmax); 3359 return (0); 3360 } 3361 3362 /* 3363 * tcp_clean_death / tcp_close_detached must not be called more than once 3364 * on a tcp. Thus every function that potentially calls tcp_clean_death 3365 * must check for the tcp state before calling tcp_clean_death. 3366 * Eg. tcp_input, tcp_rput_data, tcp_eager_kill, tcp_clean_death_wrapper, 3367 * tcp_timer_handler, all check for the tcp state. 3368 */ 3369 /* ARGSUSED */ 3370 void 3371 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2) 3372 { 3373 tcp_t *tcp = ((conn_t *)arg)->conn_tcp; 3374 3375 freemsg(mp); 3376 if (tcp->tcp_state > TCPS_BOUND) 3377 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, 3378 ETIMEDOUT, 5); 3379 } 3380 3381 /* 3382 * We are dying for some reason. Try to do it gracefully. (May be called 3383 * as writer.) 3384 * 3385 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3386 * done by a service procedure). 3387 * TBD - Should the return value distinguish between the tcp_t being 3388 * freed and it being reinitialized? 3389 */ 3390 static int 3391 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3392 { 3393 mblk_t *mp; 3394 queue_t *q; 3395 conn_t *connp = tcp->tcp_connp; 3396 tcp_stack_t *tcps = tcp->tcp_tcps; 3397 3398 TCP_CLD_STAT(tag); 3399 3400 #if TCP_TAG_CLEAN_DEATH 3401 tcp->tcp_cleandeathtag = tag; 3402 #endif 3403 3404 if (tcp->tcp_fused) 3405 tcp_unfuse(tcp); 3406 3407 if (tcp->tcp_linger_tid != 0 && 3408 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3409 tcp_stop_lingering(tcp); 3410 } 3411 3412 ASSERT(tcp != NULL); 3413 ASSERT((tcp->tcp_family == AF_INET && 3414 tcp->tcp_ipversion == IPV4_VERSION) || 3415 (tcp->tcp_family == AF_INET6 && 3416 (tcp->tcp_ipversion == IPV4_VERSION || 3417 tcp->tcp_ipversion == IPV6_VERSION))); 3418 3419 if (TCP_IS_DETACHED(tcp)) { 3420 if (tcp->tcp_hard_binding) { 3421 /* 3422 * Its an eager that we are dealing with. We close the 3423 * eager but in case a conn_ind has already gone to the 3424 * listener, let tcp_accept_finish() send a discon_ind 3425 * to the listener and drop the last reference. If the 3426 * listener doesn't even know about the eager i.e. the 3427 * conn_ind hasn't gone up, blow away the eager and drop 3428 * the last reference as well. If the conn_ind has gone 3429 * up, state should be BOUND. tcp_accept_finish 3430 * will figure out that the connection has received a 3431 * RST and will send a DISCON_IND to the application. 3432 */ 3433 tcp_closei_local(tcp); 3434 if (!tcp->tcp_tconnind_started) { 3435 CONN_DEC_REF(connp); 3436 } else { 3437 tcp->tcp_state = TCPS_BOUND; 3438 } 3439 } else { 3440 tcp_close_detached(tcp); 3441 } 3442 return (0); 3443 } 3444 3445 TCP_STAT(tcps, tcp_clean_death_nondetached); 3446 3447 q = tcp->tcp_rq; 3448 3449 /* Trash all inbound data */ 3450 if (!IPCL_IS_NONSTR(connp)) { 3451 ASSERT(q != NULL); 3452 flushq(q, FLUSHALL); 3453 } 3454 3455 /* 3456 * If we are at least part way open and there is error 3457 * (err==0 implies no error) 3458 * notify our client by a T_DISCON_IND. 3459 */ 3460 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3461 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3462 !TCP_IS_SOCKET(tcp)) { 3463 /* 3464 * Send M_FLUSH according to TPI. Because sockets will 3465 * (and must) ignore FLUSHR we do that only for TPI 3466 * endpoints and sockets in STREAMS mode. 3467 */ 3468 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3469 } 3470 if (tcp->tcp_debug) { 3471 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3472 "tcp_clean_death: discon err %d", err); 3473 } 3474 if (IPCL_IS_NONSTR(connp)) { 3475 /* Direct socket, use upcall */ 3476 (*connp->conn_upcalls->su_disconnected)( 3477 connp->conn_upper_handle, tcp->tcp_connid, err); 3478 } else { 3479 mp = mi_tpi_discon_ind(NULL, err, 0); 3480 if (mp != NULL) { 3481 putnext(q, mp); 3482 } else { 3483 if (tcp->tcp_debug) { 3484 (void) strlog(TCP_MOD_ID, 0, 1, 3485 SL_ERROR|SL_TRACE, 3486 "tcp_clean_death, sending M_ERROR"); 3487 } 3488 (void) putnextctl1(q, M_ERROR, EPROTO); 3489 } 3490 } 3491 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3492 /* SYN_SENT or SYN_RCVD */ 3493 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3494 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3495 /* ESTABLISHED or CLOSE_WAIT */ 3496 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3497 } 3498 } 3499 3500 tcp_reinit(tcp); 3501 if (IPCL_IS_NONSTR(connp)) 3502 (void) tcp_do_unbind(connp); 3503 3504 return (-1); 3505 } 3506 3507 /* 3508 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3509 * to expire, stop the wait and finish the close. 3510 */ 3511 static void 3512 tcp_stop_lingering(tcp_t *tcp) 3513 { 3514 clock_t delta = 0; 3515 tcp_stack_t *tcps = tcp->tcp_tcps; 3516 3517 tcp->tcp_linger_tid = 0; 3518 if (tcp->tcp_state > TCPS_LISTEN) { 3519 tcp_acceptor_hash_remove(tcp); 3520 mutex_enter(&tcp->tcp_non_sq_lock); 3521 if (tcp->tcp_flow_stopped) { 3522 tcp_clrqfull(tcp); 3523 } 3524 mutex_exit(&tcp->tcp_non_sq_lock); 3525 3526 if (tcp->tcp_timer_tid != 0) { 3527 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3528 tcp->tcp_timer_tid = 0; 3529 } 3530 /* 3531 * Need to cancel those timers which will not be used when 3532 * TCP is detached. This has to be done before the tcp_wq 3533 * is set to the global queue. 3534 */ 3535 tcp_timers_stop(tcp); 3536 3537 tcp->tcp_detached = B_TRUE; 3538 ASSERT(tcps->tcps_g_q != NULL); 3539 tcp->tcp_rq = tcps->tcps_g_q; 3540 tcp->tcp_wq = WR(tcps->tcps_g_q); 3541 3542 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3543 tcp_time_wait_append(tcp); 3544 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3545 goto finish; 3546 } 3547 3548 /* 3549 * If delta is zero the timer event wasn't executed and was 3550 * successfully canceled. In this case we need to restart it 3551 * with the minimal delta possible. 3552 */ 3553 if (delta >= 0) { 3554 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3555 delta ? delta : 1); 3556 } 3557 } else { 3558 tcp_closei_local(tcp); 3559 CONN_DEC_REF(tcp->tcp_connp); 3560 } 3561 finish: 3562 /* Signal closing thread that it can complete close */ 3563 mutex_enter(&tcp->tcp_closelock); 3564 tcp->tcp_detached = B_TRUE; 3565 ASSERT(tcps->tcps_g_q != NULL); 3566 3567 tcp->tcp_rq = tcps->tcps_g_q; 3568 tcp->tcp_wq = WR(tcps->tcps_g_q); 3569 3570 tcp->tcp_closed = 1; 3571 cv_signal(&tcp->tcp_closecv); 3572 mutex_exit(&tcp->tcp_closelock); 3573 } 3574 3575 /* 3576 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3577 * expires. 3578 */ 3579 static void 3580 tcp_close_linger_timeout(void *arg) 3581 { 3582 conn_t *connp = (conn_t *)arg; 3583 tcp_t *tcp = connp->conn_tcp; 3584 3585 tcp->tcp_client_errno = ETIMEDOUT; 3586 tcp_stop_lingering(tcp); 3587 } 3588 3589 static void 3590 tcp_close_common(conn_t *connp, int flags) 3591 { 3592 tcp_t *tcp = connp->conn_tcp; 3593 mblk_t *mp = &tcp->tcp_closemp; 3594 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3595 mblk_t *bp; 3596 3597 ASSERT(connp->conn_ref >= 2); 3598 3599 /* 3600 * Mark the conn as closing. ill_pending_mp_add will not 3601 * add any mp to the pending mp list, after this conn has 3602 * started closing. Same for sq_pending_mp_add 3603 */ 3604 mutex_enter(&connp->conn_lock); 3605 connp->conn_state_flags |= CONN_CLOSING; 3606 if (connp->conn_oper_pending_ill != NULL) 3607 conn_ioctl_cleanup_reqd = B_TRUE; 3608 CONN_INC_REF_LOCKED(connp); 3609 mutex_exit(&connp->conn_lock); 3610 tcp->tcp_closeflags = (uint8_t)flags; 3611 ASSERT(connp->conn_ref >= 3); 3612 3613 /* 3614 * tcp_closemp_used is used below without any protection of a lock 3615 * as we don't expect any one else to use it concurrently at this 3616 * point otherwise it would be a major defect. 3617 */ 3618 3619 if (mp->b_prev == NULL) 3620 tcp->tcp_closemp_used = B_TRUE; 3621 else 3622 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: " 3623 "connp %p tcp %p\n", (void *)connp, (void *)tcp); 3624 3625 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15); 3626 3627 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp, 3628 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3629 3630 mutex_enter(&tcp->tcp_closelock); 3631 while (!tcp->tcp_closed) { 3632 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) { 3633 /* 3634 * The cv_wait_sig() was interrupted. We now do the 3635 * following: 3636 * 3637 * 1) If the endpoint was lingering, we allow this 3638 * to be interrupted by cancelling the linger timeout 3639 * and closing normally. 3640 * 3641 * 2) Revert to calling cv_wait() 3642 * 3643 * We revert to using cv_wait() to avoid an 3644 * infinite loop which can occur if the calling 3645 * thread is higher priority than the squeue worker 3646 * thread and is bound to the same cpu. 3647 */ 3648 if (tcp->tcp_linger && tcp->tcp_lingertime > 0) { 3649 mutex_exit(&tcp->tcp_closelock); 3650 /* Entering squeue, bump ref count. */ 3651 CONN_INC_REF(connp); 3652 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 3653 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, 3654 tcp_linger_interrupted, connp, 3655 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE); 3656 mutex_enter(&tcp->tcp_closelock); 3657 } 3658 break; 3659 } 3660 } 3661 while (!tcp->tcp_closed) 3662 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3663 mutex_exit(&tcp->tcp_closelock); 3664 3665 /* 3666 * In the case of listener streams that have eagers in the q or q0 3667 * we wait for the eagers to drop their reference to us. tcp_rq and 3668 * tcp_wq of the eagers point to our queues. By waiting for the 3669 * refcnt to drop to 1, we are sure that the eagers have cleaned 3670 * up their queue pointers and also dropped their references to us. 3671 */ 3672 if (tcp->tcp_wait_for_eagers) { 3673 mutex_enter(&connp->conn_lock); 3674 while (connp->conn_ref != 1) { 3675 cv_wait(&connp->conn_cv, &connp->conn_lock); 3676 } 3677 mutex_exit(&connp->conn_lock); 3678 } 3679 /* 3680 * ioctl cleanup. The mp is queued in the 3681 * ill_pending_mp or in the sq_pending_mp. 3682 */ 3683 if (conn_ioctl_cleanup_reqd) 3684 conn_ioctl_cleanup(connp); 3685 3686 tcp->tcp_cpid = -1; 3687 } 3688 3689 static int 3690 tcp_tpi_close(queue_t *q, int flags) 3691 { 3692 conn_t *connp; 3693 3694 ASSERT(WR(q)->q_next == NULL); 3695 3696 if (flags & SO_FALLBACK) { 3697 /* 3698 * stream is being closed while in fallback 3699 * simply free the resources that were allocated 3700 */ 3701 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr)); 3702 qprocsoff(q); 3703 goto done; 3704 } 3705 3706 connp = Q_TO_CONN(q); 3707 /* 3708 * We are being closed as /dev/tcp or /dev/tcp6. 3709 */ 3710 tcp_close_common(connp, flags); 3711 3712 qprocsoff(q); 3713 inet_minor_free(connp->conn_minor_arena, connp->conn_dev); 3714 3715 /* 3716 * Drop IP's reference on the conn. This is the last reference 3717 * on the connp if the state was less than established. If the 3718 * connection has gone into timewait state, then we will have 3719 * one ref for the TCP and one more ref (total of two) for the 3720 * classifier connected hash list (a timewait connections stays 3721 * in connected hash till closed). 3722 * 3723 * We can't assert the references because there might be other 3724 * transient reference places because of some walkers or queued 3725 * packets in squeue for the timewait state. 3726 */ 3727 CONN_DEC_REF(connp); 3728 done: 3729 q->q_ptr = WR(q)->q_ptr = NULL; 3730 return (0); 3731 } 3732 3733 static int 3734 tcp_tpi_close_accept(queue_t *q) 3735 { 3736 vmem_t *minor_arena; 3737 dev_t conn_dev; 3738 3739 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3740 3741 /* 3742 * We had opened an acceptor STREAM for sockfs which is 3743 * now being closed due to some error. 3744 */ 3745 qprocsoff(q); 3746 3747 minor_arena = (vmem_t *)WR(q)->q_ptr; 3748 conn_dev = (dev_t)RD(q)->q_ptr; 3749 ASSERT(minor_arena != NULL); 3750 ASSERT(conn_dev != 0); 3751 inet_minor_free(minor_arena, conn_dev); 3752 q->q_ptr = WR(q)->q_ptr = NULL; 3753 return (0); 3754 } 3755 3756 /* 3757 * Called by tcp_close() routine via squeue when lingering is 3758 * interrupted by a signal. 3759 */ 3760 3761 /* ARGSUSED */ 3762 static void 3763 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2) 3764 { 3765 conn_t *connp = (conn_t *)arg; 3766 tcp_t *tcp = connp->conn_tcp; 3767 3768 freeb(mp); 3769 if (tcp->tcp_linger_tid != 0 && 3770 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3771 tcp_stop_lingering(tcp); 3772 tcp->tcp_client_errno = EINTR; 3773 } 3774 } 3775 3776 /* 3777 * Called by streams close routine via squeues when our client blows off her 3778 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3779 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3780 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3781 * acked. 3782 * 3783 * NOTE: tcp_close potentially returns error when lingering. 3784 * However, the stream head currently does not pass these errors 3785 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3786 * errors to the application (from tsleep()) and not errors 3787 * like ECONNRESET caused by receiving a reset packet. 3788 */ 3789 3790 /* ARGSUSED */ 3791 static void 3792 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3793 { 3794 char *msg; 3795 conn_t *connp = (conn_t *)arg; 3796 tcp_t *tcp = connp->conn_tcp; 3797 clock_t delta = 0; 3798 tcp_stack_t *tcps = tcp->tcp_tcps; 3799 3800 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3801 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3802 3803 mutex_enter(&tcp->tcp_eager_lock); 3804 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3805 /* Cleanup for listener */ 3806 tcp_eager_cleanup(tcp, 0); 3807 tcp->tcp_wait_for_eagers = 1; 3808 } 3809 mutex_exit(&tcp->tcp_eager_lock); 3810 3811 connp->conn_mdt_ok = B_FALSE; 3812 tcp->tcp_mdt = B_FALSE; 3813 3814 connp->conn_lso_ok = B_FALSE; 3815 tcp->tcp_lso = B_FALSE; 3816 3817 msg = NULL; 3818 switch (tcp->tcp_state) { 3819 case TCPS_CLOSED: 3820 case TCPS_IDLE: 3821 case TCPS_BOUND: 3822 case TCPS_LISTEN: 3823 break; 3824 case TCPS_SYN_SENT: 3825 msg = "tcp_close, during connect"; 3826 break; 3827 case TCPS_SYN_RCVD: 3828 /* 3829 * Close during the connect 3-way handshake 3830 * but here there may or may not be pending data 3831 * already on queue. Process almost same as in 3832 * the ESTABLISHED state. 3833 */ 3834 /* FALLTHRU */ 3835 default: 3836 if (tcp->tcp_fused) 3837 tcp_unfuse(tcp); 3838 3839 /* 3840 * If SO_LINGER has set a zero linger time, abort the 3841 * connection with a reset. 3842 */ 3843 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3844 msg = "tcp_close, zero lingertime"; 3845 break; 3846 } 3847 3848 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3849 /* 3850 * Abort connection if there is unread data queued. 3851 */ 3852 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3853 msg = "tcp_close, unread data"; 3854 break; 3855 } 3856 /* 3857 * tcp_hard_bound is now cleared thus all packets go through 3858 * tcp_lookup. This fact is used by tcp_detach below. 3859 * 3860 * We have done a qwait() above which could have possibly 3861 * drained more messages in turn causing transition to a 3862 * different state. Check whether we have to do the rest 3863 * of the processing or not. 3864 */ 3865 if (tcp->tcp_state <= TCPS_LISTEN) 3866 break; 3867 3868 /* 3869 * Transmit the FIN before detaching the tcp_t. 3870 * After tcp_detach returns this queue/perimeter 3871 * no longer owns the tcp_t thus others can modify it. 3872 */ 3873 (void) tcp_xmit_end(tcp); 3874 3875 /* 3876 * If lingering on close then wait until the fin is acked, 3877 * the SO_LINGER time passes, or a reset is sent/received. 3878 */ 3879 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3880 !(tcp->tcp_fin_acked) && 3881 tcp->tcp_state >= TCPS_ESTABLISHED) { 3882 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3883 tcp->tcp_client_errno = EWOULDBLOCK; 3884 } else if (tcp->tcp_client_errno == 0) { 3885 3886 ASSERT(tcp->tcp_linger_tid == 0); 3887 3888 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3889 tcp_close_linger_timeout, 3890 tcp->tcp_lingertime * hz); 3891 3892 /* tcp_close_linger_timeout will finish close */ 3893 if (tcp->tcp_linger_tid == 0) 3894 tcp->tcp_client_errno = ENOSR; 3895 else 3896 return; 3897 } 3898 3899 /* 3900 * Check if we need to detach or just close 3901 * the instance. 3902 */ 3903 if (tcp->tcp_state <= TCPS_LISTEN) 3904 break; 3905 } 3906 3907 /* 3908 * Make sure that no other thread will access the tcp_rq of 3909 * this instance (through lookups etc.) as tcp_rq will go 3910 * away shortly. 3911 */ 3912 tcp_acceptor_hash_remove(tcp); 3913 3914 mutex_enter(&tcp->tcp_non_sq_lock); 3915 if (tcp->tcp_flow_stopped) { 3916 tcp_clrqfull(tcp); 3917 } 3918 mutex_exit(&tcp->tcp_non_sq_lock); 3919 3920 if (tcp->tcp_timer_tid != 0) { 3921 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3922 tcp->tcp_timer_tid = 0; 3923 } 3924 /* 3925 * Need to cancel those timers which will not be used when 3926 * TCP is detached. This has to be done before the tcp_wq 3927 * is set to the global queue. 3928 */ 3929 tcp_timers_stop(tcp); 3930 3931 tcp->tcp_detached = B_TRUE; 3932 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3933 tcp_time_wait_append(tcp); 3934 TCP_DBGSTAT(tcps, tcp_detach_time_wait); 3935 ASSERT(connp->conn_ref >= 3); 3936 goto finish; 3937 } 3938 3939 /* 3940 * If delta is zero the timer event wasn't executed and was 3941 * successfully canceled. In this case we need to restart it 3942 * with the minimal delta possible. 3943 */ 3944 if (delta >= 0) 3945 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3946 delta ? delta : 1); 3947 3948 ASSERT(connp->conn_ref >= 3); 3949 goto finish; 3950 } 3951 3952 /* Detach did not complete. Still need to remove q from stream. */ 3953 if (msg) { 3954 if (tcp->tcp_state == TCPS_ESTABLISHED || 3955 tcp->tcp_state == TCPS_CLOSE_WAIT) 3956 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 3957 if (tcp->tcp_state == TCPS_SYN_SENT || 3958 tcp->tcp_state == TCPS_SYN_RCVD) 3959 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 3960 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 3961 } 3962 3963 tcp_closei_local(tcp); 3964 CONN_DEC_REF(connp); 3965 ASSERT(connp->conn_ref >= 2); 3966 3967 finish: 3968 /* 3969 * Although packets are always processed on the correct 3970 * tcp's perimeter and access is serialized via squeue's, 3971 * IP still needs a queue when sending packets in time_wait 3972 * state so use WR(tcps_g_q) till ip_output() can be 3973 * changed to deal with just connp. For read side, we 3974 * could have set tcp_rq to NULL but there are some cases 3975 * in tcp_rput_data() from early days of this code which 3976 * do a putnext without checking if tcp is closed. Those 3977 * need to be identified before both tcp_rq and tcp_wq 3978 * can be set to NULL and tcps_g_q can disappear forever. 3979 */ 3980 mutex_enter(&tcp->tcp_closelock); 3981 /* 3982 * Don't change the queues in the case of a listener that has 3983 * eagers in its q or q0. It could surprise the eagers. 3984 * Instead wait for the eagers outside the squeue. 3985 */ 3986 if (!tcp->tcp_wait_for_eagers) { 3987 tcp->tcp_detached = B_TRUE; 3988 /* 3989 * When default queue is closing we set tcps_g_q to NULL 3990 * after the close is done. 3991 */ 3992 ASSERT(tcps->tcps_g_q != NULL); 3993 tcp->tcp_rq = tcps->tcps_g_q; 3994 tcp->tcp_wq = WR(tcps->tcps_g_q); 3995 } 3996 3997 /* Signal tcp_close() to finish closing. */ 3998 tcp->tcp_closed = 1; 3999 cv_signal(&tcp->tcp_closecv); 4000 mutex_exit(&tcp->tcp_closelock); 4001 } 4002 4003 /* 4004 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4005 * Some stream heads get upset if they see these later on as anything but NULL. 4006 */ 4007 static void 4008 tcp_close_mpp(mblk_t **mpp) 4009 { 4010 mblk_t *mp; 4011 4012 if ((mp = *mpp) != NULL) { 4013 do { 4014 mp->b_next = NULL; 4015 mp->b_prev = NULL; 4016 } while ((mp = mp->b_cont) != NULL); 4017 4018 mp = *mpp; 4019 *mpp = NULL; 4020 freemsg(mp); 4021 } 4022 } 4023 4024 /* Do detached close. */ 4025 static void 4026 tcp_close_detached(tcp_t *tcp) 4027 { 4028 if (tcp->tcp_fused) 4029 tcp_unfuse(tcp); 4030 4031 /* 4032 * Clustering code serializes TCP disconnect callbacks and 4033 * cluster tcp list walks by blocking a TCP disconnect callback 4034 * if a cluster tcp list walk is in progress. This ensures 4035 * accurate accounting of TCPs in the cluster code even though 4036 * the TCP list walk itself is not atomic. 4037 */ 4038 tcp_closei_local(tcp); 4039 CONN_DEC_REF(tcp->tcp_connp); 4040 } 4041 4042 /* 4043 * Stop all TCP timers, and free the timer mblks if requested. 4044 */ 4045 void 4046 tcp_timers_stop(tcp_t *tcp) 4047 { 4048 if (tcp->tcp_timer_tid != 0) { 4049 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4050 tcp->tcp_timer_tid = 0; 4051 } 4052 if (tcp->tcp_ka_tid != 0) { 4053 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4054 tcp->tcp_ka_tid = 0; 4055 } 4056 if (tcp->tcp_ack_tid != 0) { 4057 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4058 tcp->tcp_ack_tid = 0; 4059 } 4060 if (tcp->tcp_push_tid != 0) { 4061 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4062 tcp->tcp_push_tid = 0; 4063 } 4064 } 4065 4066 /* 4067 * The tcp_t is going away. Remove it from all lists and set it 4068 * to TCPS_CLOSED. The freeing up of memory is deferred until 4069 * tcp_inactive. This is needed since a thread in tcp_rput might have 4070 * done a CONN_INC_REF on this structure before it was removed from the 4071 * hashes. 4072 */ 4073 static void 4074 tcp_closei_local(tcp_t *tcp) 4075 { 4076 ire_t *ire; 4077 conn_t *connp = tcp->tcp_connp; 4078 tcp_stack_t *tcps = tcp->tcp_tcps; 4079 4080 if (!TCP_IS_SOCKET(tcp)) 4081 tcp_acceptor_hash_remove(tcp); 4082 4083 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 4084 tcp->tcp_ibsegs = 0; 4085 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 4086 tcp->tcp_obsegs = 0; 4087 4088 /* 4089 * If we are an eager connection hanging off a listener that 4090 * hasn't formally accepted the connection yet, get off his 4091 * list and blow off any data that we have accumulated. 4092 */ 4093 if (tcp->tcp_listener != NULL) { 4094 tcp_t *listener = tcp->tcp_listener; 4095 mutex_enter(&listener->tcp_eager_lock); 4096 /* 4097 * tcp_tconnind_started == B_TRUE means that the 4098 * conn_ind has already gone to listener. At 4099 * this point, eager will be closed but we 4100 * leave it in listeners eager list so that 4101 * if listener decides to close without doing 4102 * accept, we can clean this up. In tcp_wput_accept 4103 * we take care of the case of accept on closed 4104 * eager. 4105 */ 4106 if (!tcp->tcp_tconnind_started) { 4107 tcp_eager_unlink(tcp); 4108 mutex_exit(&listener->tcp_eager_lock); 4109 /* 4110 * We don't want to have any pointers to the 4111 * listener queue, after we have released our 4112 * reference on the listener 4113 */ 4114 ASSERT(tcps->tcps_g_q != NULL); 4115 tcp->tcp_rq = tcps->tcps_g_q; 4116 tcp->tcp_wq = WR(tcps->tcps_g_q); 4117 CONN_DEC_REF(listener->tcp_connp); 4118 } else { 4119 mutex_exit(&listener->tcp_eager_lock); 4120 } 4121 } 4122 4123 /* Stop all the timers */ 4124 tcp_timers_stop(tcp); 4125 4126 if (tcp->tcp_state == TCPS_LISTEN) { 4127 if (tcp->tcp_ip_addr_cache) { 4128 kmem_free((void *)tcp->tcp_ip_addr_cache, 4129 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4130 tcp->tcp_ip_addr_cache = NULL; 4131 } 4132 } 4133 mutex_enter(&tcp->tcp_non_sq_lock); 4134 if (tcp->tcp_flow_stopped) 4135 tcp_clrqfull(tcp); 4136 mutex_exit(&tcp->tcp_non_sq_lock); 4137 4138 tcp_bind_hash_remove(tcp); 4139 /* 4140 * If the tcp_time_wait_collector (which runs outside the squeue) 4141 * is trying to remove this tcp from the time wait list, we will 4142 * block in tcp_time_wait_remove while trying to acquire the 4143 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4144 * requires the ipcl_hash_remove to be ordered after the 4145 * tcp_time_wait_remove for the refcnt checks to work correctly. 4146 */ 4147 if (tcp->tcp_state == TCPS_TIME_WAIT) 4148 (void) tcp_time_wait_remove(tcp, NULL); 4149 CL_INET_DISCONNECT(connp, tcp); 4150 ipcl_hash_remove(connp); 4151 4152 /* 4153 * Delete the cached ire in conn_ire_cache and also mark 4154 * the conn as CONDEMNED 4155 */ 4156 mutex_enter(&connp->conn_lock); 4157 connp->conn_state_flags |= CONN_CONDEMNED; 4158 ire = connp->conn_ire_cache; 4159 connp->conn_ire_cache = NULL; 4160 mutex_exit(&connp->conn_lock); 4161 if (ire != NULL) 4162 IRE_REFRELE_NOTR(ire); 4163 4164 /* Need to cleanup any pending ioctls */ 4165 ASSERT(tcp->tcp_time_wait_next == NULL); 4166 ASSERT(tcp->tcp_time_wait_prev == NULL); 4167 ASSERT(tcp->tcp_time_wait_expire == 0); 4168 tcp->tcp_state = TCPS_CLOSED; 4169 4170 /* Release any SSL context */ 4171 if (tcp->tcp_kssl_ent != NULL) { 4172 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4173 tcp->tcp_kssl_ent = NULL; 4174 } 4175 if (tcp->tcp_kssl_ctx != NULL) { 4176 kssl_release_ctx(tcp->tcp_kssl_ctx); 4177 tcp->tcp_kssl_ctx = NULL; 4178 } 4179 tcp->tcp_kssl_pending = B_FALSE; 4180 4181 tcp_ipsec_cleanup(tcp); 4182 } 4183 4184 /* 4185 * tcp is dying (called from ipcl_conn_destroy and error cases). 4186 * Free the tcp_t in either case. 4187 */ 4188 void 4189 tcp_free(tcp_t *tcp) 4190 { 4191 mblk_t *mp; 4192 ip6_pkt_t *ipp; 4193 4194 ASSERT(tcp != NULL); 4195 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4196 4197 tcp->tcp_rq = NULL; 4198 tcp->tcp_wq = NULL; 4199 4200 tcp_close_mpp(&tcp->tcp_xmit_head); 4201 tcp_close_mpp(&tcp->tcp_reass_head); 4202 if (tcp->tcp_rcv_list != NULL) { 4203 /* Free b_next chain */ 4204 tcp_close_mpp(&tcp->tcp_rcv_list); 4205 } 4206 if ((mp = tcp->tcp_urp_mp) != NULL) { 4207 freemsg(mp); 4208 } 4209 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4210 freemsg(mp); 4211 } 4212 4213 if (tcp->tcp_fused_sigurg_mp != NULL) { 4214 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4215 freeb(tcp->tcp_fused_sigurg_mp); 4216 tcp->tcp_fused_sigurg_mp = NULL; 4217 } 4218 4219 if (tcp->tcp_ordrel_mp != NULL) { 4220 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 4221 freeb(tcp->tcp_ordrel_mp); 4222 tcp->tcp_ordrel_mp = NULL; 4223 } 4224 4225 if (tcp->tcp_sack_info != NULL) { 4226 if (tcp->tcp_notsack_list != NULL) { 4227 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4228 } 4229 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4230 } 4231 4232 if (tcp->tcp_hopopts != NULL) { 4233 mi_free(tcp->tcp_hopopts); 4234 tcp->tcp_hopopts = NULL; 4235 tcp->tcp_hopoptslen = 0; 4236 } 4237 ASSERT(tcp->tcp_hopoptslen == 0); 4238 if (tcp->tcp_dstopts != NULL) { 4239 mi_free(tcp->tcp_dstopts); 4240 tcp->tcp_dstopts = NULL; 4241 tcp->tcp_dstoptslen = 0; 4242 } 4243 ASSERT(tcp->tcp_dstoptslen == 0); 4244 if (tcp->tcp_rtdstopts != NULL) { 4245 mi_free(tcp->tcp_rtdstopts); 4246 tcp->tcp_rtdstopts = NULL; 4247 tcp->tcp_rtdstoptslen = 0; 4248 } 4249 ASSERT(tcp->tcp_rtdstoptslen == 0); 4250 if (tcp->tcp_rthdr != NULL) { 4251 mi_free(tcp->tcp_rthdr); 4252 tcp->tcp_rthdr = NULL; 4253 tcp->tcp_rthdrlen = 0; 4254 } 4255 ASSERT(tcp->tcp_rthdrlen == 0); 4256 4257 ipp = &tcp->tcp_sticky_ipp; 4258 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4259 IPPF_RTHDR)) 4260 ip6_pkt_free(ipp); 4261 4262 /* 4263 * Free memory associated with the tcp/ip header template. 4264 */ 4265 4266 if (tcp->tcp_iphc != NULL) 4267 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4268 4269 /* 4270 * Following is really a blowing away a union. 4271 * It happens to have exactly two members of identical size 4272 * the following code is enough. 4273 */ 4274 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4275 } 4276 4277 4278 /* 4279 * Put a connection confirmation message upstream built from the 4280 * address information within 'iph' and 'tcph'. Report our success or failure. 4281 */ 4282 static boolean_t 4283 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4284 mblk_t **defermp) 4285 { 4286 sin_t sin; 4287 sin6_t sin6; 4288 mblk_t *mp; 4289 char *optp = NULL; 4290 int optlen = 0; 4291 4292 if (defermp != NULL) 4293 *defermp = NULL; 4294 4295 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4296 /* 4297 * Return in T_CONN_CON results of option negotiation through 4298 * the T_CONN_REQ. Note: If there is an real end-to-end option 4299 * negotiation, then what is received from remote end needs 4300 * to be taken into account but there is no such thing (yet?) 4301 * in our TCP/IP. 4302 * Note: We do not use mi_offset_param() here as 4303 * tcp_opts_conn_req contents do not directly come from 4304 * an application and are either generated in kernel or 4305 * from user input that was already verified. 4306 */ 4307 mp = tcp->tcp_conn.tcp_opts_conn_req; 4308 optp = (char *)(mp->b_rptr + 4309 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4310 optlen = (int) 4311 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4312 } 4313 4314 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4315 ipha_t *ipha = (ipha_t *)iphdr; 4316 4317 /* packet is IPv4 */ 4318 if (tcp->tcp_family == AF_INET) { 4319 sin = sin_null; 4320 sin.sin_addr.s_addr = ipha->ipha_src; 4321 sin.sin_port = *(uint16_t *)tcph->th_lport; 4322 sin.sin_family = AF_INET; 4323 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4324 (int)sizeof (sin_t), optp, optlen); 4325 } else { 4326 sin6 = sin6_null; 4327 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4328 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4329 sin6.sin6_family = AF_INET6; 4330 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4331 (int)sizeof (sin6_t), optp, optlen); 4332 4333 } 4334 } else { 4335 ip6_t *ip6h = (ip6_t *)iphdr; 4336 4337 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4338 ASSERT(tcp->tcp_family == AF_INET6); 4339 sin6 = sin6_null; 4340 sin6.sin6_addr = ip6h->ip6_src; 4341 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4342 sin6.sin6_family = AF_INET6; 4343 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4344 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4345 (int)sizeof (sin6_t), optp, optlen); 4346 } 4347 4348 if (!mp) 4349 return (B_FALSE); 4350 4351 mblk_copycred(mp, idmp); 4352 4353 if (defermp == NULL) { 4354 conn_t *connp = tcp->tcp_connp; 4355 if (IPCL_IS_NONSTR(connp)) { 4356 cred_t *cr; 4357 pid_t cpid; 4358 4359 cr = msg_getcred(mp, &cpid); 4360 (*connp->conn_upcalls->su_connected) 4361 (connp->conn_upper_handle, tcp->tcp_connid, cr, 4362 cpid); 4363 freemsg(mp); 4364 } else { 4365 putnext(tcp->tcp_rq, mp); 4366 } 4367 } else { 4368 *defermp = mp; 4369 } 4370 4371 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4372 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4373 return (B_TRUE); 4374 } 4375 4376 /* 4377 * Defense for the SYN attack - 4378 * 1. When q0 is full, drop from the tail (tcp_eager_prev_drop_q0) the oldest 4379 * one from the list of droppable eagers. This list is a subset of q0. 4380 * see comments before the definition of MAKE_DROPPABLE(). 4381 * 2. Don't drop a SYN request before its first timeout. This gives every 4382 * request at least til the first timeout to complete its 3-way handshake. 4383 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4384 * requests currently on the queue that has timed out. This will be used 4385 * as an indicator of whether an attack is under way, so that appropriate 4386 * actions can be taken. (It's incremented in tcp_timer() and decremented 4387 * either when eager goes into ESTABLISHED, or gets freed up.) 4388 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4389 * # of timeout drops back to <= q0len/32 => SYN alert off 4390 */ 4391 static boolean_t 4392 tcp_drop_q0(tcp_t *tcp) 4393 { 4394 tcp_t *eager; 4395 mblk_t *mp; 4396 tcp_stack_t *tcps = tcp->tcp_tcps; 4397 4398 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4399 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4400 4401 /* Pick oldest eager from the list of droppable eagers */ 4402 eager = tcp->tcp_eager_prev_drop_q0; 4403 4404 /* If list is empty. return B_FALSE */ 4405 if (eager == tcp) { 4406 return (B_FALSE); 4407 } 4408 4409 /* If allocated, the mp will be freed in tcp_clean_death_wrapper() */ 4410 if ((mp = allocb(0, BPRI_HI)) == NULL) 4411 return (B_FALSE); 4412 4413 /* 4414 * Take this eager out from the list of droppable eagers since we are 4415 * going to drop it. 4416 */ 4417 MAKE_UNDROPPABLE(eager); 4418 4419 if (tcp->tcp_debug) { 4420 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4421 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4422 " (%d pending) on %s, drop one", tcps->tcps_conn_req_max_q0, 4423 tcp->tcp_conn_req_cnt_q0, 4424 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4425 } 4426 4427 BUMP_MIB(&tcps->tcps_mib, tcpHalfOpenDrop); 4428 4429 /* Put a reference on the conn as we are enqueueing it in the sqeue */ 4430 CONN_INC_REF(eager->tcp_connp); 4431 4432 /* Mark the IRE created for this SYN request temporary */ 4433 tcp_ip_ire_mark_advice(eager); 4434 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 4435 tcp_clean_death_wrapper, eager->tcp_connp, 4436 SQ_FILL, SQTAG_TCP_DROP_Q0); 4437 4438 return (B_TRUE); 4439 } 4440 4441 int 4442 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4443 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4444 { 4445 tcp_t *ltcp = lconnp->conn_tcp; 4446 tcp_t *tcp = connp->conn_tcp; 4447 mblk_t *tpi_mp; 4448 ipha_t *ipha; 4449 ip6_t *ip6h; 4450 sin6_t sin6; 4451 in6_addr_t v6dst; 4452 int err; 4453 int ifindex = 0; 4454 tcp_stack_t *tcps = tcp->tcp_tcps; 4455 4456 if (ipvers == IPV4_VERSION) { 4457 ipha = (ipha_t *)mp->b_rptr; 4458 4459 connp->conn_send = ip_output; 4460 connp->conn_recv = tcp_input; 4461 4462 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4463 &connp->conn_bound_source_v6); 4464 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4465 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4466 4467 sin6 = sin6_null; 4468 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4469 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4470 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4471 sin6.sin6_family = AF_INET6; 4472 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4473 lconnp->conn_zoneid, tcps->tcps_netstack); 4474 if (tcp->tcp_recvdstaddr) { 4475 sin6_t sin6d; 4476 4477 sin6d = sin6_null; 4478 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4479 &sin6d.sin6_addr); 4480 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4481 sin6d.sin6_family = AF_INET; 4482 tpi_mp = mi_tpi_extconn_ind(NULL, 4483 (char *)&sin6d, sizeof (sin6_t), 4484 (char *)&tcp, 4485 (t_scalar_t)sizeof (intptr_t), 4486 (char *)&sin6d, sizeof (sin6_t), 4487 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4488 } else { 4489 tpi_mp = mi_tpi_conn_ind(NULL, 4490 (char *)&sin6, sizeof (sin6_t), 4491 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4492 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4493 } 4494 } else { 4495 ip6h = (ip6_t *)mp->b_rptr; 4496 4497 connp->conn_send = ip_output_v6; 4498 connp->conn_recv = tcp_input; 4499 4500 connp->conn_bound_source_v6 = ip6h->ip6_dst; 4501 connp->conn_srcv6 = ip6h->ip6_dst; 4502 connp->conn_remv6 = ip6h->ip6_src; 4503 4504 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4505 ifindex = (int)DB_CKSUMSTUFF(mp); 4506 DB_CKSUMSTUFF(mp) = 0; 4507 4508 sin6 = sin6_null; 4509 sin6.sin6_addr = ip6h->ip6_src; 4510 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4511 sin6.sin6_family = AF_INET6; 4512 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4513 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4514 lconnp->conn_zoneid, tcps->tcps_netstack); 4515 4516 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4517 /* Pass up the scope_id of remote addr */ 4518 sin6.sin6_scope_id = ifindex; 4519 } else { 4520 sin6.sin6_scope_id = 0; 4521 } 4522 if (tcp->tcp_recvdstaddr) { 4523 sin6_t sin6d; 4524 4525 sin6d = sin6_null; 4526 sin6.sin6_addr = ip6h->ip6_dst; 4527 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4528 sin6d.sin6_family = AF_INET; 4529 tpi_mp = mi_tpi_extconn_ind(NULL, 4530 (char *)&sin6d, sizeof (sin6_t), 4531 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4532 (char *)&sin6d, sizeof (sin6_t), 4533 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4534 } else { 4535 tpi_mp = mi_tpi_conn_ind(NULL, 4536 (char *)&sin6, sizeof (sin6_t), 4537 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4538 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4539 } 4540 } 4541 4542 if (tpi_mp == NULL) 4543 return (ENOMEM); 4544 4545 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4546 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4547 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4548 connp->conn_fully_bound = B_FALSE; 4549 4550 /* Inherit information from the "parent" */ 4551 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4552 tcp->tcp_family = ltcp->tcp_family; 4553 4554 tcp->tcp_wq = ltcp->tcp_wq; 4555 tcp->tcp_rq = ltcp->tcp_rq; 4556 4557 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 4558 tcp->tcp_detached = B_TRUE; 4559 SOCK_CONNID_INIT(tcp->tcp_connid); 4560 if ((err = tcp_init_values(tcp)) != 0) { 4561 freemsg(tpi_mp); 4562 return (err); 4563 } 4564 4565 if (ipvers == IPV4_VERSION) { 4566 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4567 freemsg(tpi_mp); 4568 return (err); 4569 } 4570 ASSERT(tcp->tcp_ipha != NULL); 4571 } else { 4572 /* ifindex must be already set */ 4573 ASSERT(ifindex != 0); 4574 4575 if (ltcp->tcp_bound_if != 0) 4576 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4577 else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) 4578 tcp->tcp_bound_if = ifindex; 4579 4580 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4581 tcp->tcp_recvifindex = 0; 4582 tcp->tcp_recvhops = 0xffffffffU; 4583 ASSERT(tcp->tcp_ip6h != NULL); 4584 } 4585 4586 tcp->tcp_lport = ltcp->tcp_lport; 4587 4588 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4589 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4590 /* 4591 * Listener had options of some sort; eager inherits. 4592 * Free up the eager template and allocate one 4593 * of the right size. 4594 */ 4595 if (tcp->tcp_hdr_grown) { 4596 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4597 } else { 4598 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4599 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4600 } 4601 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4602 KM_NOSLEEP); 4603 if (tcp->tcp_iphc == NULL) { 4604 tcp->tcp_iphc_len = 0; 4605 freemsg(tpi_mp); 4606 return (ENOMEM); 4607 } 4608 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4609 tcp->tcp_hdr_grown = B_TRUE; 4610 } 4611 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4612 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4613 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4614 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4615 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4616 4617 /* 4618 * Copy the IP+TCP header template from listener to eager 4619 */ 4620 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4621 if (tcp->tcp_ipversion == IPV6_VERSION) { 4622 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4623 IPPROTO_RAW) { 4624 tcp->tcp_ip6h = 4625 (ip6_t *)(tcp->tcp_iphc + 4626 sizeof (ip6i_t)); 4627 } else { 4628 tcp->tcp_ip6h = 4629 (ip6_t *)(tcp->tcp_iphc); 4630 } 4631 tcp->tcp_ipha = NULL; 4632 } else { 4633 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4634 tcp->tcp_ip6h = NULL; 4635 } 4636 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4637 tcp->tcp_ip_hdr_len); 4638 } else { 4639 /* 4640 * only valid case when ipversion of listener and 4641 * eager differ is when listener is IPv6 and 4642 * eager is IPv4. 4643 * Eager header template has been initialized to the 4644 * maximum v4 header sizes, which includes space for 4645 * TCP and IP options. 4646 */ 4647 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4648 (tcp->tcp_ipversion == IPV4_VERSION)); 4649 ASSERT(tcp->tcp_iphc_len >= 4650 TCP_MAX_COMBINED_HEADER_LENGTH); 4651 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4652 /* copy IP header fields individually */ 4653 tcp->tcp_ipha->ipha_ttl = 4654 ltcp->tcp_ip6h->ip6_hops; 4655 bcopy(ltcp->tcp_tcph->th_lport, 4656 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4657 } 4658 4659 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4660 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4661 sizeof (in_port_t)); 4662 4663 if (ltcp->tcp_lport == 0) { 4664 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4665 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4666 sizeof (in_port_t)); 4667 } 4668 4669 if (tcp->tcp_ipversion == IPV4_VERSION) { 4670 ASSERT(ipha != NULL); 4671 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4672 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4673 4674 /* Source routing option copyover (reverse it) */ 4675 if (tcps->tcps_rev_src_routes) 4676 tcp_opt_reverse(tcp, ipha); 4677 } else { 4678 ASSERT(ip6h != NULL); 4679 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4680 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4681 } 4682 4683 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4684 ASSERT(!tcp->tcp_tconnind_started); 4685 /* 4686 * If the SYN contains a credential, it's a loopback packet; attach 4687 * the credential to the TPI message. 4688 */ 4689 mblk_copycred(tpi_mp, idmp); 4690 4691 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4692 4693 /* Inherit the listener's SSL protection state */ 4694 4695 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4696 kssl_hold_ent(tcp->tcp_kssl_ent); 4697 tcp->tcp_kssl_pending = B_TRUE; 4698 } 4699 4700 /* Inherit the listener's non-STREAMS flag */ 4701 if (IPCL_IS_NONSTR(lconnp)) { 4702 connp->conn_flags |= IPCL_NONSTR; 4703 } 4704 4705 return (0); 4706 } 4707 4708 4709 int 4710 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4711 tcph_t *tcph, mblk_t *idmp) 4712 { 4713 tcp_t *ltcp = lconnp->conn_tcp; 4714 tcp_t *tcp = connp->conn_tcp; 4715 sin_t sin; 4716 mblk_t *tpi_mp = NULL; 4717 int err; 4718 tcp_stack_t *tcps = tcp->tcp_tcps; 4719 4720 sin = sin_null; 4721 sin.sin_addr.s_addr = ipha->ipha_src; 4722 sin.sin_port = *(uint16_t *)tcph->th_lport; 4723 sin.sin_family = AF_INET; 4724 if (ltcp->tcp_recvdstaddr) { 4725 sin_t sind; 4726 4727 sind = sin_null; 4728 sind.sin_addr.s_addr = ipha->ipha_dst; 4729 sind.sin_port = *(uint16_t *)tcph->th_fport; 4730 sind.sin_family = AF_INET; 4731 tpi_mp = mi_tpi_extconn_ind(NULL, 4732 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4733 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4734 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4735 } else { 4736 tpi_mp = mi_tpi_conn_ind(NULL, 4737 (char *)&sin, sizeof (sin_t), 4738 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4739 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4740 } 4741 4742 if (tpi_mp == NULL) { 4743 return (ENOMEM); 4744 } 4745 4746 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4747 connp->conn_send = ip_output; 4748 connp->conn_recv = tcp_input; 4749 connp->conn_fully_bound = B_FALSE; 4750 4751 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_bound_source_v6); 4752 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4753 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4754 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4755 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4756 4757 /* Inherit information from the "parent" */ 4758 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4759 tcp->tcp_family = ltcp->tcp_family; 4760 tcp->tcp_wq = ltcp->tcp_wq; 4761 tcp->tcp_rq = ltcp->tcp_rq; 4762 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 4763 tcp->tcp_detached = B_TRUE; 4764 SOCK_CONNID_INIT(tcp->tcp_connid); 4765 if ((err = tcp_init_values(tcp)) != 0) { 4766 freemsg(tpi_mp); 4767 return (err); 4768 } 4769 4770 /* 4771 * Let's make sure that eager tcp template has enough space to 4772 * copy IPv4 listener's tcp template. Since the conn_t structure is 4773 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4774 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4775 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4776 * extension headers or with ip6i_t struct). Note that bcopy() below 4777 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4778 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4779 */ 4780 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4781 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4782 4783 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4784 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4785 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4786 tcp->tcp_ttl = ltcp->tcp_ttl; 4787 tcp->tcp_tos = ltcp->tcp_tos; 4788 4789 /* Copy the IP+TCP header template from listener to eager */ 4790 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4791 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4792 tcp->tcp_ip6h = NULL; 4793 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4794 tcp->tcp_ip_hdr_len); 4795 4796 /* Initialize the IP addresses and Ports */ 4797 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4798 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4799 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4800 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4801 4802 /* Source routing option copyover (reverse it) */ 4803 if (tcps->tcps_rev_src_routes) 4804 tcp_opt_reverse(tcp, ipha); 4805 4806 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4807 ASSERT(!tcp->tcp_tconnind_started); 4808 4809 /* 4810 * If the SYN contains a credential, it's a loopback packet; attach 4811 * the credential to the TPI message. 4812 */ 4813 mblk_copycred(tpi_mp, idmp); 4814 4815 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4816 4817 /* Inherit the listener's SSL protection state */ 4818 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4819 kssl_hold_ent(tcp->tcp_kssl_ent); 4820 tcp->tcp_kssl_pending = B_TRUE; 4821 } 4822 4823 /* Inherit the listener's non-STREAMS flag */ 4824 if (IPCL_IS_NONSTR(lconnp)) { 4825 connp->conn_flags |= IPCL_NONSTR; 4826 } 4827 4828 return (0); 4829 } 4830 4831 /* 4832 * sets up conn for ipsec. 4833 * if the first mblk is M_CTL it is consumed and mpp is updated. 4834 * in case of error mpp is freed. 4835 */ 4836 conn_t * 4837 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4838 { 4839 conn_t *connp = tcp->tcp_connp; 4840 conn_t *econnp; 4841 squeue_t *new_sqp; 4842 mblk_t *first_mp = *mpp; 4843 mblk_t *mp = *mpp; 4844 boolean_t mctl_present = B_FALSE; 4845 uint_t ipvers; 4846 4847 econnp = tcp_get_conn(sqp, tcp->tcp_tcps); 4848 if (econnp == NULL) { 4849 freemsg(first_mp); 4850 return (NULL); 4851 } 4852 if (DB_TYPE(mp) == M_CTL) { 4853 if (mp->b_cont == NULL || 4854 mp->b_cont->b_datap->db_type != M_DATA) { 4855 freemsg(first_mp); 4856 return (NULL); 4857 } 4858 mp = mp->b_cont; 4859 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4860 freemsg(first_mp); 4861 return (NULL); 4862 } 4863 4864 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4865 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4866 mctl_present = B_TRUE; 4867 } else { 4868 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4869 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4870 } 4871 4872 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4873 DB_CKSUMSTART(mp) = 0; 4874 4875 ASSERT(OK_32PTR(mp->b_rptr)); 4876 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4877 if (ipvers == IPV4_VERSION) { 4878 uint16_t *up; 4879 uint32_t ports; 4880 ipha_t *ipha; 4881 4882 ipha = (ipha_t *)mp->b_rptr; 4883 up = (uint16_t *)((uchar_t *)ipha + 4884 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4885 ports = *(uint32_t *)up; 4886 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4887 ipha->ipha_dst, ipha->ipha_src, ports); 4888 } else { 4889 uint16_t *up; 4890 uint32_t ports; 4891 uint16_t ip_hdr_len; 4892 uint8_t *nexthdrp; 4893 ip6_t *ip6h; 4894 tcph_t *tcph; 4895 4896 ip6h = (ip6_t *)mp->b_rptr; 4897 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4898 ip_hdr_len = IPV6_HDR_LEN; 4899 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4900 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4901 CONN_DEC_REF(econnp); 4902 freemsg(first_mp); 4903 return (NULL); 4904 } 4905 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4906 up = (uint16_t *)tcph->th_lport; 4907 ports = *(uint32_t *)up; 4908 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4909 ip6h->ip6_dst, ip6h->ip6_src, ports); 4910 } 4911 4912 /* 4913 * The caller already ensured that there is a sqp present. 4914 */ 4915 econnp->conn_sqp = new_sqp; 4916 econnp->conn_initial_sqp = new_sqp; 4917 4918 if (connp->conn_policy != NULL) { 4919 ipsec_in_t *ii; 4920 ii = (ipsec_in_t *)(first_mp->b_rptr); 4921 ASSERT(ii->ipsec_in_policy == NULL); 4922 IPPH_REFHOLD(connp->conn_policy); 4923 ii->ipsec_in_policy = connp->conn_policy; 4924 4925 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 4926 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 4927 CONN_DEC_REF(econnp); 4928 freemsg(first_mp); 4929 return (NULL); 4930 } 4931 } 4932 4933 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 4934 CONN_DEC_REF(econnp); 4935 freemsg(first_mp); 4936 return (NULL); 4937 } 4938 4939 /* 4940 * If we know we have some policy, pass the "IPSEC" 4941 * options size TCP uses this adjust the MSS. 4942 */ 4943 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 4944 if (mctl_present) { 4945 freeb(first_mp); 4946 *mpp = mp; 4947 } 4948 4949 return (econnp); 4950 } 4951 4952 /* 4953 * tcp_get_conn/tcp_free_conn 4954 * 4955 * tcp_get_conn is used to get a clean tcp connection structure. 4956 * It tries to reuse the connections put on the freelist by the 4957 * time_wait_collector failing which it goes to kmem_cache. This 4958 * way has two benefits compared to just allocating from and 4959 * freeing to kmem_cache. 4960 * 1) The time_wait_collector can free (which includes the cleanup) 4961 * outside the squeue. So when the interrupt comes, we have a clean 4962 * connection sitting in the freelist. Obviously, this buys us 4963 * performance. 4964 * 4965 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 4966 * has multiple disadvantages - tying up the squeue during alloc, and the 4967 * fact that IPSec policy initialization has to happen here which 4968 * requires us sending a M_CTL and checking for it i.e. real ugliness. 4969 * But allocating the conn/tcp in IP land is also not the best since 4970 * we can't check the 'q' and 'q0' which are protected by squeue and 4971 * blindly allocate memory which might have to be freed here if we are 4972 * not allowed to accept the connection. By using the freelist and 4973 * putting the conn/tcp back in freelist, we don't pay a penalty for 4974 * allocating memory without checking 'q/q0' and freeing it if we can't 4975 * accept the connection. 4976 * 4977 * Care should be taken to put the conn back in the same squeue's freelist 4978 * from which it was allocated. Best results are obtained if conn is 4979 * allocated from listener's squeue and freed to the same. Time wait 4980 * collector will free up the freelist is the connection ends up sitting 4981 * there for too long. 4982 */ 4983 void * 4984 tcp_get_conn(void *arg, tcp_stack_t *tcps) 4985 { 4986 tcp_t *tcp = NULL; 4987 conn_t *connp = NULL; 4988 squeue_t *sqp = (squeue_t *)arg; 4989 tcp_squeue_priv_t *tcp_time_wait; 4990 netstack_t *ns; 4991 mblk_t *tcp_rsrv_mp = NULL; 4992 4993 tcp_time_wait = 4994 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 4995 4996 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 4997 tcp = tcp_time_wait->tcp_free_list; 4998 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 4999 if (tcp != NULL) { 5000 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5001 tcp_time_wait->tcp_free_list_cnt--; 5002 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5003 tcp->tcp_time_wait_next = NULL; 5004 connp = tcp->tcp_connp; 5005 connp->conn_flags |= IPCL_REUSED; 5006 5007 ASSERT(tcp->tcp_tcps == NULL); 5008 ASSERT(connp->conn_netstack == NULL); 5009 ASSERT(tcp->tcp_rsrv_mp != NULL); 5010 ns = tcps->tcps_netstack; 5011 netstack_hold(ns); 5012 connp->conn_netstack = ns; 5013 tcp->tcp_tcps = tcps; 5014 TCPS_REFHOLD(tcps); 5015 ipcl_globalhash_insert(connp); 5016 return ((void *)connp); 5017 } 5018 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5019 /* 5020 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until 5021 * this conn_t/tcp_t is freed at ipcl_conn_destroy(). 5022 */ 5023 tcp_rsrv_mp = allocb(0, BPRI_HI); 5024 if (tcp_rsrv_mp == NULL) 5025 return (NULL); 5026 5027 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP, 5028 tcps->tcps_netstack)) == NULL) { 5029 freeb(tcp_rsrv_mp); 5030 return (NULL); 5031 } 5032 5033 tcp = connp->conn_tcp; 5034 tcp->tcp_rsrv_mp = tcp_rsrv_mp; 5035 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL); 5036 5037 tcp->tcp_tcps = tcps; 5038 TCPS_REFHOLD(tcps); 5039 5040 return ((void *)connp); 5041 } 5042 5043 /* 5044 * Update the cached label for the given tcp_t. This should be called once per 5045 * connection, and before any packets are sent or tcp_process_options is 5046 * invoked. Returns B_FALSE if the correct label could not be constructed. 5047 */ 5048 static boolean_t 5049 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5050 { 5051 conn_t *connp = tcp->tcp_connp; 5052 5053 if (tcp->tcp_ipversion == IPV4_VERSION) { 5054 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5055 int added; 5056 5057 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5058 connp->conn_mac_exempt, 5059 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5060 return (B_FALSE); 5061 5062 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5063 if (added == -1) 5064 return (B_FALSE); 5065 tcp->tcp_hdr_len += added; 5066 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5067 tcp->tcp_ip_hdr_len += added; 5068 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5069 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5070 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5071 tcp->tcp_hdr_len); 5072 if (added == -1) 5073 return (B_FALSE); 5074 tcp->tcp_hdr_len += added; 5075 tcp->tcp_tcph = (tcph_t *) 5076 ((uchar_t *)tcp->tcp_tcph + added); 5077 tcp->tcp_ip_hdr_len += added; 5078 } 5079 } else { 5080 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5081 5082 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5083 connp->conn_mac_exempt, 5084 tcp->tcp_tcps->tcps_netstack->netstack_ip) != 0) 5085 return (B_FALSE); 5086 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5087 &tcp->tcp_label_len, optbuf) != 0) 5088 return (B_FALSE); 5089 if (tcp_build_hdrs(tcp) != 0) 5090 return (B_FALSE); 5091 } 5092 5093 connp->conn_ulp_labeled = 1; 5094 5095 return (B_TRUE); 5096 } 5097 5098 /* BEGIN CSTYLED */ 5099 /* 5100 * 5101 * The sockfs ACCEPT path: 5102 * ======================= 5103 * 5104 * The eager is now established in its own perimeter as soon as SYN is 5105 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5106 * completes the accept processing on the acceptor STREAM. The sending 5107 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5108 * listener but a TLI/XTI listener completes the accept processing 5109 * on the listener perimeter. 5110 * 5111 * Common control flow for 3 way handshake: 5112 * ---------------------------------------- 5113 * 5114 * incoming SYN (listener perimeter) -> tcp_rput_data() 5115 * -> tcp_conn_request() 5116 * 5117 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5118 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5119 * 5120 * Sockfs ACCEPT Path: 5121 * ------------------- 5122 * 5123 * open acceptor stream (tcp_open allocates tcp_wput_accept() 5124 * as STREAM entry point) 5125 * 5126 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5127 * 5128 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5129 * association (we are not behind eager's squeue but sockfs is protecting us 5130 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5131 * is changed to point at tcp_wput(). 5132 * 5133 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5134 * listener (done on listener's perimeter). 5135 * 5136 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5137 * accept. 5138 * 5139 * TLI/XTI client ACCEPT path: 5140 * --------------------------- 5141 * 5142 * soaccept() sends T_CONN_RES on the listener STREAM. 5143 * 5144 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5145 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5146 * 5147 * Locks: 5148 * ====== 5149 * 5150 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5151 * and listeners->tcp_eager_next_q. 5152 * 5153 * Referencing: 5154 * ============ 5155 * 5156 * 1) We start out in tcp_conn_request by eager placing a ref on 5157 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5158 * 5159 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5160 * doing so we place a ref on the eager. This ref is finally dropped at the 5161 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5162 * reference is dropped by the squeue framework. 5163 * 5164 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5165 * 5166 * The reference must be released by the same entity that added the reference 5167 * In the above scheme, the eager is the entity that adds and releases the 5168 * references. Note that tcp_accept_finish executes in the squeue of the eager 5169 * (albeit after it is attached to the acceptor stream). Though 1. executes 5170 * in the listener's squeue, the eager is nascent at this point and the 5171 * reference can be considered to have been added on behalf of the eager. 5172 * 5173 * Eager getting a Reset or listener closing: 5174 * ========================================== 5175 * 5176 * Once the listener and eager are linked, the listener never does the unlink. 5177 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5178 * a message on all eager perimeter. The eager then does the unlink, clears 5179 * any pointers to the listener's queue and drops the reference to the 5180 * listener. The listener waits in tcp_close outside the squeue until its 5181 * refcount has dropped to 1. This ensures that the listener has waited for 5182 * all eagers to clear their association with the listener. 5183 * 5184 * Similarly, if eager decides to go away, it can unlink itself and close. 5185 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5186 * the reference to eager is still valid because of the extra ref we put 5187 * in tcp_send_conn_ind. 5188 * 5189 * Listener can always locate the eager under the protection 5190 * of the listener->tcp_eager_lock, and then do a refhold 5191 * on the eager during the accept processing. 5192 * 5193 * The acceptor stream accesses the eager in the accept processing 5194 * based on the ref placed on eager before sending T_conn_ind. 5195 * The only entity that can negate this refhold is a listener close 5196 * which is mutually exclusive with an active acceptor stream. 5197 * 5198 * Eager's reference on the listener 5199 * =================================== 5200 * 5201 * If the accept happens (even on a closed eager) the eager drops its 5202 * reference on the listener at the start of tcp_accept_finish. If the 5203 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5204 * the reference is dropped in tcp_closei_local. If the listener closes, 5205 * the reference is dropped in tcp_eager_kill. In all cases the reference 5206 * is dropped while executing in the eager's context (squeue). 5207 */ 5208 /* END CSTYLED */ 5209 5210 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5211 5212 /* 5213 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5214 * tcp_rput_data will not see any SYN packets. 5215 */ 5216 /* ARGSUSED */ 5217 void 5218 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5219 { 5220 tcph_t *tcph; 5221 uint32_t seg_seq; 5222 tcp_t *eager; 5223 uint_t ipvers; 5224 ipha_t *ipha; 5225 ip6_t *ip6h; 5226 int err; 5227 conn_t *econnp = NULL; 5228 squeue_t *new_sqp; 5229 mblk_t *mp1; 5230 uint_t ip_hdr_len; 5231 conn_t *connp = (conn_t *)arg; 5232 tcp_t *tcp = connp->conn_tcp; 5233 cred_t *credp; 5234 tcp_stack_t *tcps = tcp->tcp_tcps; 5235 ip_stack_t *ipst; 5236 5237 if (tcp->tcp_state != TCPS_LISTEN) 5238 goto error2; 5239 5240 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5241 5242 mutex_enter(&tcp->tcp_eager_lock); 5243 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5244 mutex_exit(&tcp->tcp_eager_lock); 5245 TCP_STAT(tcps, tcp_listendrop); 5246 BUMP_MIB(&tcps->tcps_mib, tcpListenDrop); 5247 if (tcp->tcp_debug) { 5248 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5249 "tcp_conn_request: listen backlog (max=%d) " 5250 "overflow (%d pending) on %s", 5251 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5252 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5253 } 5254 goto error2; 5255 } 5256 5257 if (tcp->tcp_conn_req_cnt_q0 >= 5258 tcp->tcp_conn_req_max + tcps->tcps_conn_req_max_q0) { 5259 /* 5260 * Q0 is full. Drop a pending half-open req from the queue 5261 * to make room for the new SYN req. Also mark the time we 5262 * drop a SYN. 5263 * 5264 * A more aggressive defense against SYN attack will 5265 * be to set the "tcp_syn_defense" flag now. 5266 */ 5267 TCP_STAT(tcps, tcp_listendropq0); 5268 tcp->tcp_last_rcv_lbolt = lbolt64; 5269 if (!tcp_drop_q0(tcp)) { 5270 mutex_exit(&tcp->tcp_eager_lock); 5271 BUMP_MIB(&tcps->tcps_mib, tcpListenDropQ0); 5272 if (tcp->tcp_debug) { 5273 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5274 "tcp_conn_request: listen half-open queue " 5275 "(max=%d) full (%d pending) on %s", 5276 tcps->tcps_conn_req_max_q0, 5277 tcp->tcp_conn_req_cnt_q0, 5278 tcp_display(tcp, NULL, 5279 DISP_PORT_ONLY)); 5280 } 5281 goto error2; 5282 } 5283 } 5284 mutex_exit(&tcp->tcp_eager_lock); 5285 5286 /* 5287 * IP adds STRUIO_EAGER and ensures that the received packet is 5288 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5289 * link local address. If IPSec is enabled, db_struioflag has 5290 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5291 * otherwise an error case if neither of them is set. 5292 */ 5293 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5294 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5295 DB_CKSUMSTART(mp) = 0; 5296 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5297 econnp = (conn_t *)tcp_get_conn(arg2, tcps); 5298 if (econnp == NULL) 5299 goto error2; 5300 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5301 econnp->conn_sqp = new_sqp; 5302 econnp->conn_initial_sqp = new_sqp; 5303 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5304 /* 5305 * mp is updated in tcp_get_ipsec_conn(). 5306 */ 5307 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5308 if (econnp == NULL) { 5309 /* 5310 * mp freed by tcp_get_ipsec_conn. 5311 */ 5312 return; 5313 } 5314 ASSERT(econnp->conn_netstack == connp->conn_netstack); 5315 } else { 5316 goto error2; 5317 } 5318 5319 ASSERT(DB_TYPE(mp) == M_DATA); 5320 5321 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5322 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5323 ASSERT(OK_32PTR(mp->b_rptr)); 5324 if (ipvers == IPV4_VERSION) { 5325 ipha = (ipha_t *)mp->b_rptr; 5326 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5327 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5328 } else { 5329 ip6h = (ip6_t *)mp->b_rptr; 5330 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5331 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5332 } 5333 5334 if (tcp->tcp_family == AF_INET) { 5335 ASSERT(ipvers == IPV4_VERSION); 5336 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5337 } else { 5338 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5339 } 5340 5341 if (err) 5342 goto error3; 5343 5344 eager = econnp->conn_tcp; 5345 ASSERT(eager->tcp_ordrel_mp == NULL); 5346 5347 if (!IPCL_IS_NONSTR(econnp)) { 5348 /* 5349 * Pre-allocate the T_ordrel_ind mblk for TPI socket so that 5350 * at close time, we will always have that to send up. 5351 * Otherwise, we need to do special handling in case the 5352 * allocation fails at that time. 5353 */ 5354 if ((eager->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) 5355 goto error3; 5356 } 5357 /* Inherit various TCP parameters from the listener */ 5358 eager->tcp_naglim = tcp->tcp_naglim; 5359 eager->tcp_first_timer_threshold = tcp->tcp_first_timer_threshold; 5360 eager->tcp_second_timer_threshold = tcp->tcp_second_timer_threshold; 5361 5362 eager->tcp_first_ctimer_threshold = tcp->tcp_first_ctimer_threshold; 5363 eager->tcp_second_ctimer_threshold = tcp->tcp_second_ctimer_threshold; 5364 5365 /* 5366 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5367 * If it does not, the eager's receive window will be set to the 5368 * listener's receive window later in this function. 5369 */ 5370 eager->tcp_rwnd = 0; 5371 5372 /* 5373 * Inherit listener's tcp_init_cwnd. Need to do this before 5374 * calling tcp_process_options() where tcp_mss_set() is called 5375 * to set the initial cwnd. 5376 */ 5377 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5378 5379 /* 5380 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5381 * zone id before the accept is completed in tcp_wput_accept(). 5382 */ 5383 econnp->conn_zoneid = connp->conn_zoneid; 5384 econnp->conn_allzones = connp->conn_allzones; 5385 5386 /* Copy nexthop information from listener to eager */ 5387 if (connp->conn_nexthop_set) { 5388 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5389 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5390 } 5391 5392 /* 5393 * TSOL: tsol_input_proc() needs the eager's cred before the 5394 * eager is accepted 5395 */ 5396 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5397 crhold(credp); 5398 5399 /* 5400 * If the caller has the process-wide flag set, then default to MAC 5401 * exempt mode. This allows read-down to unlabeled hosts. 5402 */ 5403 if (getpflags(NET_MAC_AWARE, credp) != 0) 5404 econnp->conn_mac_exempt = B_TRUE; 5405 5406 if (is_system_labeled()) { 5407 cred_t *cr; 5408 5409 if (connp->conn_mlp_type != mlptSingle) { 5410 cr = econnp->conn_peercred = msg_getcred(mp, NULL); 5411 if (cr != NULL) 5412 crhold(cr); 5413 else 5414 cr = econnp->conn_cred; 5415 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5416 econnp, cred_t *, cr) 5417 } else { 5418 cr = econnp->conn_cred; 5419 DTRACE_PROBE2(syn_accept, conn_t *, 5420 econnp, cred_t *, cr) 5421 } 5422 5423 if (!tcp_update_label(eager, cr)) { 5424 DTRACE_PROBE3( 5425 tx__ip__log__error__connrequest__tcp, 5426 char *, "eager connp(1) label on SYN mp(2) failed", 5427 conn_t *, econnp, mblk_t *, mp); 5428 goto error3; 5429 } 5430 } 5431 5432 eager->tcp_hard_binding = B_TRUE; 5433 5434 tcp_bind_hash_insert(&tcps->tcps_bind_fanout[ 5435 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5436 5437 CL_INET_CONNECT(connp, eager, B_FALSE, err); 5438 if (err != 0) { 5439 tcp_bind_hash_remove(eager); 5440 goto error3; 5441 } 5442 5443 /* 5444 * No need to check for multicast destination since ip will only pass 5445 * up multicasts to those that have expressed interest 5446 * TODO: what about rejecting broadcasts? 5447 * Also check that source is not a multicast or broadcast address. 5448 */ 5449 eager->tcp_state = TCPS_SYN_RCVD; 5450 5451 5452 /* 5453 * There should be no ire in the mp as we are being called after 5454 * receiving the SYN. 5455 */ 5456 ASSERT(tcp_ire_mp(&mp) == NULL); 5457 5458 /* 5459 * Adapt our mss, ttl, ... according to information provided in IRE. 5460 */ 5461 5462 if (tcp_adapt_ire(eager, NULL) == 0) { 5463 /* Undo the bind_hash_insert */ 5464 tcp_bind_hash_remove(eager); 5465 goto error3; 5466 } 5467 5468 /* Process all TCP options. */ 5469 tcp_process_options(eager, tcph); 5470 5471 /* Is the other end ECN capable? */ 5472 if (tcps->tcps_ecn_permitted >= 1 && 5473 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5474 eager->tcp_ecn_ok = B_TRUE; 5475 } 5476 5477 /* 5478 * listener->tcp_rq->q_hiwat should be the default window size or a 5479 * window size changed via SO_RCVBUF option. First round up the 5480 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5481 * scale option value if needed. Call tcp_rwnd_set() to finish the 5482 * setting. 5483 * 5484 * Note if there is a rpipe metric associated with the remote host, 5485 * we should not inherit receive window size from listener. 5486 */ 5487 eager->tcp_rwnd = MSS_ROUNDUP( 5488 (eager->tcp_rwnd == 0 ? tcp->tcp_recv_hiwater: 5489 eager->tcp_rwnd), eager->tcp_mss); 5490 if (eager->tcp_snd_ws_ok) 5491 tcp_set_ws_value(eager); 5492 /* 5493 * Note that this is the only place tcp_rwnd_set() is called for 5494 * accepting a connection. We need to call it here instead of 5495 * after the 3-way handshake because we need to tell the other 5496 * side our rwnd in the SYN-ACK segment. 5497 */ 5498 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5499 5500 /* 5501 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5502 * via soaccept()->soinheritoptions() which essentially applies 5503 * all the listener options to the new STREAM. The options that we 5504 * need to take care of are: 5505 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5506 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5507 * SO_SNDBUF, SO_RCVBUF. 5508 * 5509 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5510 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5511 * tcp_maxpsz_set() gets called later from 5512 * tcp_accept_finish(), the option takes effect. 5513 * 5514 */ 5515 /* Set the TCP options */ 5516 eager->tcp_recv_hiwater = tcp->tcp_recv_hiwater; 5517 eager->tcp_recv_lowater = tcp->tcp_recv_lowater; 5518 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5519 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5520 eager->tcp_oobinline = tcp->tcp_oobinline; 5521 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5522 eager->tcp_broadcast = tcp->tcp_broadcast; 5523 eager->tcp_useloopback = tcp->tcp_useloopback; 5524 eager->tcp_dontroute = tcp->tcp_dontroute; 5525 eager->tcp_debug = tcp->tcp_debug; 5526 eager->tcp_linger = tcp->tcp_linger; 5527 eager->tcp_lingertime = tcp->tcp_lingertime; 5528 if (tcp->tcp_ka_enabled) 5529 eager->tcp_ka_enabled = 1; 5530 5531 /* Set the IP options */ 5532 econnp->conn_broadcast = connp->conn_broadcast; 5533 econnp->conn_loopback = connp->conn_loopback; 5534 econnp->conn_dontroute = connp->conn_dontroute; 5535 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5536 5537 /* Put a ref on the listener for the eager. */ 5538 CONN_INC_REF(connp); 5539 mutex_enter(&tcp->tcp_eager_lock); 5540 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5541 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5542 tcp->tcp_eager_next_q0 = eager; 5543 eager->tcp_eager_prev_q0 = tcp; 5544 5545 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5546 eager->tcp_listener = tcp; 5547 eager->tcp_saved_listener = tcp; 5548 5549 /* 5550 * Tag this detached tcp vector for later retrieval 5551 * by our listener client in tcp_accept(). 5552 */ 5553 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5554 tcp->tcp_conn_req_cnt_q0++; 5555 if (++tcp->tcp_conn_req_seqnum == -1) { 5556 /* 5557 * -1 is "special" and defined in TPI as something 5558 * that should never be used in T_CONN_IND 5559 */ 5560 ++tcp->tcp_conn_req_seqnum; 5561 } 5562 mutex_exit(&tcp->tcp_eager_lock); 5563 5564 if (tcp->tcp_syn_defense) { 5565 /* Don't drop the SYN that comes from a good IP source */ 5566 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5567 if (addr_cache != NULL && eager->tcp_remote == 5568 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5569 eager->tcp_dontdrop = B_TRUE; 5570 } 5571 } 5572 5573 /* 5574 * We need to insert the eager in its own perimeter but as soon 5575 * as we do that, we expose the eager to the classifier and 5576 * should not touch any field outside the eager's perimeter. 5577 * So do all the work necessary before inserting the eager 5578 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5579 * will succeed but undo everything if it fails. 5580 */ 5581 seg_seq = ABE32_TO_U32(tcph->th_seq); 5582 eager->tcp_irs = seg_seq; 5583 eager->tcp_rack = seg_seq; 5584 eager->tcp_rnxt = seg_seq + 1; 5585 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5586 BUMP_MIB(&tcps->tcps_mib, tcpPassiveOpens); 5587 eager->tcp_state = TCPS_SYN_RCVD; 5588 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5589 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5590 if (mp1 == NULL) { 5591 /* 5592 * Increment the ref count as we are going to 5593 * enqueueing an mp in squeue 5594 */ 5595 CONN_INC_REF(econnp); 5596 goto error; 5597 } 5598 5599 /* 5600 * Note that in theory this should use the current pid 5601 * so that getpeerucred on the client returns the actual listener 5602 * that does accept. But accept() hasn't been called yet. We could use 5603 * the pid of the process that did bind/listen on the server. 5604 * However, with common usage like inetd() the bind/listen can be done 5605 * by a different process than the accept(). 5606 * Hence we do the simple thing of using the open pid here. 5607 * Note that db_credp is set later in tcp_send_data(). 5608 */ 5609 mblk_setcred(mp1, credp, tcp->tcp_cpid); 5610 eager->tcp_cpid = tcp->tcp_cpid; 5611 eager->tcp_open_time = lbolt64; 5612 5613 /* 5614 * We need to start the rto timer. In normal case, we start 5615 * the timer after sending the packet on the wire (or at 5616 * least believing that packet was sent by waiting for 5617 * CALL_IP_WPUT() to return). Since this is the first packet 5618 * being sent on the wire for the eager, our initial tcp_rto 5619 * is at least tcp_rexmit_interval_min which is a fairly 5620 * large value to allow the algorithm to adjust slowly to large 5621 * fluctuations of RTT during first few transmissions. 5622 * 5623 * Starting the timer first and then sending the packet in this 5624 * case shouldn't make much difference since tcp_rexmit_interval_min 5625 * is of the order of several 100ms and starting the timer 5626 * first and then sending the packet will result in difference 5627 * of few micro seconds. 5628 * 5629 * Without this optimization, we are forced to hold the fanout 5630 * lock across the ipcl_bind_insert() and sending the packet 5631 * so that we don't race against an incoming packet (maybe RST) 5632 * for this eager. 5633 * 5634 * It is necessary to acquire an extra reference on the eager 5635 * at this point and hold it until after tcp_send_data() to 5636 * ensure against an eager close race. 5637 */ 5638 5639 CONN_INC_REF(eager->tcp_connp); 5640 5641 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5642 5643 /* 5644 * Insert the eager in its own perimeter now. We are ready to deal 5645 * with any packets on eager. 5646 */ 5647 if (eager->tcp_ipversion == IPV4_VERSION) { 5648 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5649 goto error; 5650 } 5651 } else { 5652 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5653 goto error; 5654 } 5655 } 5656 5657 /* mark conn as fully-bound */ 5658 econnp->conn_fully_bound = B_TRUE; 5659 5660 /* Send the SYN-ACK */ 5661 tcp_send_data(eager, eager->tcp_wq, mp1); 5662 CONN_DEC_REF(eager->tcp_connp); 5663 freemsg(mp); 5664 5665 return; 5666 error: 5667 freemsg(mp1); 5668 eager->tcp_closemp_used = B_TRUE; 5669 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 5670 mp1 = &eager->tcp_closemp; 5671 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp1, tcp_eager_kill, 5672 econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_2); 5673 5674 /* 5675 * If a connection already exists, send the mp to that connections so 5676 * that it can be appropriately dealt with. 5677 */ 5678 ipst = tcps->tcps_netstack->netstack_ip; 5679 5680 if ((econnp = ipcl_classify(mp, connp->conn_zoneid, ipst)) != NULL) { 5681 if (!IPCL_IS_CONNECTED(econnp)) { 5682 /* 5683 * Something bad happened. ipcl_conn_insert() 5684 * failed because a connection already existed 5685 * in connected hash but we can't find it 5686 * anymore (someone blew it away). Just 5687 * free this message and hopefully remote 5688 * will retransmit at which time the SYN can be 5689 * treated as a new connection or dealth with 5690 * a TH_RST if a connection already exists. 5691 */ 5692 CONN_DEC_REF(econnp); 5693 freemsg(mp); 5694 } else { 5695 SQUEUE_ENTER_ONE(econnp->conn_sqp, mp, 5696 tcp_input, econnp, SQ_FILL, SQTAG_TCP_CONN_REQ_1); 5697 } 5698 } else { 5699 /* Nobody wants this packet */ 5700 freemsg(mp); 5701 } 5702 return; 5703 error3: 5704 CONN_DEC_REF(econnp); 5705 error2: 5706 freemsg(mp); 5707 } 5708 5709 /* 5710 * In an ideal case of vertical partition in NUMA architecture, its 5711 * beneficial to have the listener and all the incoming connections 5712 * tied to the same squeue. The other constraint is that incoming 5713 * connections should be tied to the squeue attached to interrupted 5714 * CPU for obvious locality reason so this leaves the listener to 5715 * be tied to the same squeue. Our only problem is that when listener 5716 * is binding, the CPU that will get interrupted by the NIC whose 5717 * IP address the listener is binding to is not even known. So 5718 * the code below allows us to change that binding at the time the 5719 * CPU is interrupted by virtue of incoming connection's squeue. 5720 * 5721 * This is usefull only in case of a listener bound to a specific IP 5722 * address. For other kind of listeners, they get bound the 5723 * very first time and there is no attempt to rebind them. 5724 */ 5725 void 5726 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5727 { 5728 conn_t *connp = (conn_t *)arg; 5729 squeue_t *sqp = (squeue_t *)arg2; 5730 squeue_t *new_sqp; 5731 uint32_t conn_flags; 5732 5733 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5734 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5735 } else { 5736 goto done; 5737 } 5738 5739 if (connp->conn_fanout == NULL) 5740 goto done; 5741 5742 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5743 mutex_enter(&connp->conn_fanout->connf_lock); 5744 mutex_enter(&connp->conn_lock); 5745 /* 5746 * No one from read or write side can access us now 5747 * except for already queued packets on this squeue. 5748 * But since we haven't changed the squeue yet, they 5749 * can't execute. If they are processed after we have 5750 * changed the squeue, they are sent back to the 5751 * correct squeue down below. 5752 * But a listner close can race with processing of 5753 * incoming SYN. If incoming SYN processing changes 5754 * the squeue then the listener close which is waiting 5755 * to enter the squeue would operate on the wrong 5756 * squeue. Hence we don't change the squeue here unless 5757 * the refcount is exactly the minimum refcount. The 5758 * minimum refcount of 4 is counted as - 1 each for 5759 * TCP and IP, 1 for being in the classifier hash, and 5760 * 1 for the mblk being processed. 5761 */ 5762 5763 if (connp->conn_ref != 4 || 5764 connp->conn_tcp->tcp_state != TCPS_LISTEN) { 5765 mutex_exit(&connp->conn_lock); 5766 mutex_exit(&connp->conn_fanout->connf_lock); 5767 goto done; 5768 } 5769 if (connp->conn_sqp != new_sqp) { 5770 while (connp->conn_sqp != new_sqp) 5771 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5772 } 5773 5774 do { 5775 conn_flags = connp->conn_flags; 5776 conn_flags |= IPCL_FULLY_BOUND; 5777 (void) cas32(&connp->conn_flags, connp->conn_flags, 5778 conn_flags); 5779 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5780 5781 mutex_exit(&connp->conn_fanout->connf_lock); 5782 mutex_exit(&connp->conn_lock); 5783 } 5784 5785 done: 5786 if (connp->conn_sqp != sqp) { 5787 CONN_INC_REF(connp); 5788 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 5789 SQ_FILL, SQTAG_TCP_CONN_REQ_UNBOUND); 5790 } else { 5791 tcp_conn_request(connp, mp, sqp); 5792 } 5793 } 5794 5795 /* 5796 * Successful connect request processing begins when our client passes 5797 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5798 * our T_OK_ACK reply message upstream. The control flow looks like this: 5799 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_tpi_connect() -> IP 5800 * upstream <- tcp_rput() <- IP 5801 * After various error checks are completed, tcp_tpi_connect() lays 5802 * the target address and port into the composite header template, 5803 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5804 * request followed by an IRE request, and passes the three mblk message 5805 * down to IP looking like this: 5806 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5807 * Processing continues in tcp_rput() when we receive the following message: 5808 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5809 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5810 * to fire off the connection request, and then passes the T_OK_ACK mblk 5811 * upstream that we filled in below. There are, of course, numerous 5812 * error conditions along the way which truncate the processing described 5813 * above. 5814 */ 5815 static void 5816 tcp_tpi_connect(tcp_t *tcp, mblk_t *mp) 5817 { 5818 sin_t *sin; 5819 queue_t *q = tcp->tcp_wq; 5820 struct T_conn_req *tcr; 5821 struct sockaddr *sa; 5822 socklen_t len; 5823 int error; 5824 cred_t *cr; 5825 pid_t cpid; 5826 5827 /* 5828 * All Solaris components should pass a db_credp 5829 * for this TPI message, hence we ASSERT. 5830 * But in case there is some other M_PROTO that looks 5831 * like a TPI message sent by some other kernel 5832 * component, we check and return an error. 5833 */ 5834 cr = msg_getcred(mp, &cpid); 5835 ASSERT(cr != NULL); 5836 if (cr == NULL) { 5837 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5838 return; 5839 } 5840 5841 tcr = (struct T_conn_req *)mp->b_rptr; 5842 5843 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5844 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5845 tcp_err_ack(tcp, mp, TPROTO, 0); 5846 return; 5847 } 5848 5849 /* 5850 * Pre-allocate the T_ordrel_ind mblk so that at close time, we 5851 * will always have that to send up. Otherwise, we need to do 5852 * special handling in case the allocation fails at that time. 5853 * If the end point is TPI, the tcp_t can be reused and the 5854 * tcp_ordrel_mp may be allocated already. 5855 */ 5856 if (tcp->tcp_ordrel_mp == NULL) { 5857 if ((tcp->tcp_ordrel_mp = mi_tpi_ordrel_ind()) == NULL) { 5858 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5859 return; 5860 } 5861 } 5862 5863 /* 5864 * Determine packet type based on type of address passed in 5865 * the request should contain an IPv4 or IPv6 address. 5866 * Make sure that address family matches the type of 5867 * family of the the address passed down 5868 */ 5869 switch (tcr->DEST_length) { 5870 default: 5871 tcp_err_ack(tcp, mp, TBADADDR, 0); 5872 return; 5873 5874 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5875 /* 5876 * XXX: The check for valid DEST_length was not there 5877 * in earlier releases and some buggy 5878 * TLI apps (e.g Sybase) got away with not feeding 5879 * in sin_zero part of address. 5880 * We allow that bug to keep those buggy apps humming. 5881 * Test suites require the check on DEST_length. 5882 * We construct a new mblk with valid DEST_length 5883 * free the original so the rest of the code does 5884 * not have to keep track of this special shorter 5885 * length address case. 5886 */ 5887 mblk_t *nmp; 5888 struct T_conn_req *ntcr; 5889 sin_t *nsin; 5890 5891 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5892 tcr->OPT_length, BPRI_HI); 5893 if (nmp == NULL) { 5894 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5895 return; 5896 } 5897 ntcr = (struct T_conn_req *)nmp->b_rptr; 5898 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5899 ntcr->PRIM_type = T_CONN_REQ; 5900 ntcr->DEST_length = sizeof (sin_t); 5901 ntcr->DEST_offset = sizeof (struct T_conn_req); 5902 5903 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5904 *nsin = sin_null; 5905 /* Get pointer to shorter address to copy from original mp */ 5906 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5907 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5908 if (sin == NULL || !OK_32PTR((char *)sin)) { 5909 freemsg(nmp); 5910 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5911 return; 5912 } 5913 nsin->sin_family = sin->sin_family; 5914 nsin->sin_port = sin->sin_port; 5915 nsin->sin_addr = sin->sin_addr; 5916 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5917 nmp->b_wptr = (uchar_t *)&nsin[1]; 5918 if (tcr->OPT_length != 0) { 5919 ntcr->OPT_length = tcr->OPT_length; 5920 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5921 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5922 (uchar_t *)ntcr + ntcr->OPT_offset, 5923 tcr->OPT_length); 5924 nmp->b_wptr += tcr->OPT_length; 5925 } 5926 freemsg(mp); /* original mp freed */ 5927 mp = nmp; /* re-initialize original variables */ 5928 tcr = ntcr; 5929 } 5930 /* FALLTHRU */ 5931 5932 case sizeof (sin_t): 5933 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5934 sizeof (sin_t)); 5935 len = sizeof (sin_t); 5936 break; 5937 5938 case sizeof (sin6_t): 5939 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset, 5940 sizeof (sin6_t)); 5941 len = sizeof (sin6_t); 5942 break; 5943 } 5944 5945 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 5946 if (error != 0) { 5947 tcp_err_ack(tcp, mp, TSYSERR, error); 5948 return; 5949 } 5950 5951 /* 5952 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 5953 * should key on their sequence number and cut them loose. 5954 */ 5955 5956 /* 5957 * If options passed in, feed it for verification and handling 5958 */ 5959 if (tcr->OPT_length != 0) { 5960 mblk_t *ok_mp; 5961 mblk_t *discon_mp; 5962 mblk_t *conn_opts_mp; 5963 int t_error, sys_error, do_disconnect; 5964 5965 conn_opts_mp = NULL; 5966 5967 if (tcp_conprim_opt_process(tcp, mp, 5968 &do_disconnect, &t_error, &sys_error) < 0) { 5969 if (do_disconnect) { 5970 ASSERT(t_error == 0 && sys_error == 0); 5971 discon_mp = mi_tpi_discon_ind(NULL, 5972 ECONNREFUSED, 0); 5973 if (!discon_mp) { 5974 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 5975 TSYSERR, ENOMEM); 5976 return; 5977 } 5978 ok_mp = mi_tpi_ok_ack_alloc(mp); 5979 if (!ok_mp) { 5980 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 5981 TSYSERR, ENOMEM); 5982 return; 5983 } 5984 qreply(q, ok_mp); 5985 qreply(q, discon_mp); /* no flush! */ 5986 } else { 5987 ASSERT(t_error != 0); 5988 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 5989 sys_error); 5990 } 5991 return; 5992 } 5993 /* 5994 * Success in setting options, the mp option buffer represented 5995 * by OPT_length/offset has been potentially modified and 5996 * contains results of option processing. We copy it in 5997 * another mp to save it for potentially influencing returning 5998 * it in T_CONN_CONN. 5999 */ 6000 if (tcr->OPT_length != 0) { /* there are resulting options */ 6001 conn_opts_mp = copyb(mp); 6002 if (!conn_opts_mp) { 6003 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6004 TSYSERR, ENOMEM); 6005 return; 6006 } 6007 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6008 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6009 /* 6010 * Note: 6011 * These resulting option negotiation can include any 6012 * end-to-end negotiation options but there no such 6013 * thing (yet?) in our TCP/IP. 6014 */ 6015 } 6016 } 6017 6018 /* call the non-TPI version */ 6019 error = tcp_do_connect(tcp->tcp_connp, sa, len, cr, cpid); 6020 if (error < 0) { 6021 mp = mi_tpi_err_ack_alloc(mp, -error, 0); 6022 } else if (error > 0) { 6023 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error); 6024 } else { 6025 mp = mi_tpi_ok_ack_alloc(mp); 6026 } 6027 6028 /* 6029 * Note: Code below is the "failure" case 6030 */ 6031 /* return error ack and blow away saved option results if any */ 6032 connect_failed: 6033 if (mp != NULL) 6034 putnext(tcp->tcp_rq, mp); 6035 else { 6036 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6037 TSYSERR, ENOMEM); 6038 } 6039 } 6040 6041 /* 6042 * Handle connect to IPv4 destinations, including connections for AF_INET6 6043 * sockets connecting to IPv4 mapped IPv6 destinations. 6044 */ 6045 static int 6046 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport, 6047 uint_t srcid, cred_t *cr, pid_t pid) 6048 { 6049 tcph_t *tcph; 6050 mblk_t *mp; 6051 ipaddr_t dstaddr = *dstaddrp; 6052 int32_t oldstate; 6053 uint16_t lport; 6054 int error = 0; 6055 tcp_stack_t *tcps = tcp->tcp_tcps; 6056 6057 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6058 6059 /* Check for attempt to connect to INADDR_ANY */ 6060 if (dstaddr == INADDR_ANY) { 6061 /* 6062 * SunOS 4.x and 4.3 BSD allow an application 6063 * to connect a TCP socket to INADDR_ANY. 6064 * When they do this, the kernel picks the 6065 * address of one interface and uses it 6066 * instead. The kernel usually ends up 6067 * picking the address of the loopback 6068 * interface. This is an undocumented feature. 6069 * However, we provide the same thing here 6070 * in order to have source and binary 6071 * compatibility with SunOS 4.x. 6072 * Update the T_CONN_REQ (sin/sin6) since it is used to 6073 * generate the T_CONN_CON. 6074 */ 6075 dstaddr = htonl(INADDR_LOOPBACK); 6076 *dstaddrp = dstaddr; 6077 } 6078 6079 /* Handle __sin6_src_id if socket not bound to an IP address */ 6080 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6081 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6082 tcp->tcp_connp->conn_zoneid, tcps->tcps_netstack); 6083 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6084 tcp->tcp_ipha->ipha_src); 6085 } 6086 6087 /* 6088 * Don't let an endpoint connect to itself. Note that 6089 * the test here does not catch the case where the 6090 * source IP addr was left unspecified by the user. In 6091 * this case, the source addr is set in tcp_adapt_ire() 6092 * using the reply to the T_BIND message that we send 6093 * down to IP here and the check is repeated in tcp_rput_other. 6094 */ 6095 if (dstaddr == tcp->tcp_ipha->ipha_src && 6096 dstport == tcp->tcp_lport) { 6097 error = -TBADADDR; 6098 goto failed; 6099 } 6100 6101 tcp->tcp_ipha->ipha_dst = dstaddr; 6102 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6103 6104 /* 6105 * Massage a source route if any putting the first hop 6106 * in iph_dst. Compute a starting value for the checksum which 6107 * takes into account that the original iph_dst should be 6108 * included in the checksum but that ip will include the 6109 * first hop in the source route in the tcp checksum. 6110 */ 6111 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha, tcps->tcps_netstack); 6112 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6113 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6114 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6115 if ((int)tcp->tcp_sum < 0) 6116 tcp->tcp_sum--; 6117 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6118 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6119 (tcp->tcp_sum >> 16)); 6120 tcph = tcp->tcp_tcph; 6121 *(uint16_t *)tcph->th_fport = dstport; 6122 tcp->tcp_fport = dstport; 6123 6124 oldstate = tcp->tcp_state; 6125 /* 6126 * At this point the remote destination address and remote port fields 6127 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6128 * have to see which state tcp was in so we can take apropriate action. 6129 */ 6130 if (oldstate == TCPS_IDLE) { 6131 /* 6132 * We support a quick connect capability here, allowing 6133 * clients to transition directly from IDLE to SYN_SENT 6134 * tcp_bindi will pick an unused port, insert the connection 6135 * in the bind hash and transition to BOUND state. 6136 */ 6137 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6138 tcp, B_TRUE); 6139 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6140 B_FALSE, B_FALSE); 6141 if (lport == 0) { 6142 error = -TNOADDR; 6143 goto failed; 6144 } 6145 } 6146 tcp->tcp_state = TCPS_SYN_SENT; 6147 6148 mp = allocb(sizeof (ire_t), BPRI_HI); 6149 if (mp == NULL) { 6150 tcp->tcp_state = oldstate; 6151 error = ENOMEM; 6152 goto failed; 6153 } 6154 6155 mp->b_wptr += sizeof (ire_t); 6156 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6157 tcp->tcp_hard_binding = 1; 6158 6159 /* 6160 * We need to make sure that the conn_recv is set to a non-null 6161 * value before we insert the conn_t into the classifier table. 6162 * This is to avoid a race with an incoming packet which does 6163 * an ipcl_classify(). 6164 */ 6165 tcp->tcp_connp->conn_recv = tcp_input; 6166 6167 if (tcp->tcp_family == AF_INET) { 6168 error = ip_proto_bind_connected_v4(tcp->tcp_connp, &mp, 6169 IPPROTO_TCP, &tcp->tcp_ipha->ipha_src, tcp->tcp_lport, 6170 tcp->tcp_remote, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6171 } else { 6172 in6_addr_t v6src; 6173 if (tcp->tcp_ipversion == IPV4_VERSION) { 6174 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6175 } else { 6176 v6src = tcp->tcp_ip6h->ip6_src; 6177 } 6178 error = ip_proto_bind_connected_v6(tcp->tcp_connp, &mp, 6179 IPPROTO_TCP, &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6180 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6181 } 6182 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6183 tcp->tcp_active_open = 1; 6184 6185 6186 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6187 failed: 6188 /* return error ack and blow away saved option results if any */ 6189 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6190 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6191 return (error); 6192 } 6193 6194 /* 6195 * Handle connect to IPv6 destinations. 6196 */ 6197 static int 6198 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport, 6199 uint32_t flowinfo, uint_t srcid, uint32_t scope_id, cred_t *cr, pid_t pid) 6200 { 6201 tcph_t *tcph; 6202 mblk_t *mp; 6203 ip6_rthdr_t *rth; 6204 int32_t oldstate; 6205 uint16_t lport; 6206 tcp_stack_t *tcps = tcp->tcp_tcps; 6207 int error = 0; 6208 conn_t *connp = tcp->tcp_connp; 6209 6210 ASSERT(tcp->tcp_family == AF_INET6); 6211 6212 /* 6213 * If we're here, it means that the destination address is a native 6214 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6215 * reason why it might not be IPv6 is if the socket was bound to an 6216 * IPv4-mapped IPv6 address. 6217 */ 6218 if (tcp->tcp_ipversion != IPV6_VERSION) { 6219 return (-TBADADDR); 6220 } 6221 6222 /* 6223 * Interpret a zero destination to mean loopback. 6224 * Update the T_CONN_REQ (sin/sin6) since it is used to 6225 * generate the T_CONN_CON. 6226 */ 6227 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6228 *dstaddrp = ipv6_loopback; 6229 } 6230 6231 /* Handle __sin6_src_id if socket not bound to an IP address */ 6232 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6233 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6234 connp->conn_zoneid, tcps->tcps_netstack); 6235 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6236 } 6237 6238 /* 6239 * Take care of the scope_id now and add ip6i_t 6240 * if ip6i_t is not already allocated through TCP 6241 * sticky options. At this point tcp_ip6h does not 6242 * have dst info, thus use dstaddrp. 6243 */ 6244 if (scope_id != 0 && 6245 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6246 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6247 ip6i_t *ip6i; 6248 6249 ipp->ipp_ifindex = scope_id; 6250 ip6i = (ip6i_t *)tcp->tcp_iphc; 6251 6252 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6253 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6254 /* Already allocated */ 6255 ip6i->ip6i_flags |= IP6I_IFINDEX; 6256 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6257 ipp->ipp_fields |= IPPF_SCOPE_ID; 6258 } else { 6259 int reterr; 6260 6261 ipp->ipp_fields |= IPPF_SCOPE_ID; 6262 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6263 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6264 reterr = tcp_build_hdrs(tcp); 6265 if (reterr != 0) 6266 goto failed; 6267 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6268 } 6269 } 6270 6271 /* 6272 * Don't let an endpoint connect to itself. Note that 6273 * the test here does not catch the case where the 6274 * source IP addr was left unspecified by the user. In 6275 * this case, the source addr is set in tcp_adapt_ire() 6276 * using the reply to the T_BIND message that we send 6277 * down to IP here and the check is repeated in tcp_rput_other. 6278 */ 6279 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6280 (dstport == tcp->tcp_lport)) { 6281 error = -TBADADDR; 6282 goto failed; 6283 } 6284 6285 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6286 tcp->tcp_remote_v6 = *dstaddrp; 6287 tcp->tcp_ip6h->ip6_vcf = 6288 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6289 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6290 6291 /* 6292 * Massage a routing header (if present) putting the first hop 6293 * in ip6_dst. Compute a starting value for the checksum which 6294 * takes into account that the original ip6_dst should be 6295 * included in the checksum but that ip will include the 6296 * first hop in the source route in the tcp checksum. 6297 */ 6298 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6299 if (rth != NULL) { 6300 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth, 6301 tcps->tcps_netstack); 6302 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6303 (tcp->tcp_sum >> 16)); 6304 } else { 6305 tcp->tcp_sum = 0; 6306 } 6307 6308 tcph = tcp->tcp_tcph; 6309 *(uint16_t *)tcph->th_fport = dstport; 6310 tcp->tcp_fport = dstport; 6311 6312 oldstate = tcp->tcp_state; 6313 /* 6314 * At this point the remote destination address and remote port fields 6315 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6316 * have to see which state tcp was in so we can take apropriate action. 6317 */ 6318 if (oldstate == TCPS_IDLE) { 6319 /* 6320 * We support a quick connect capability here, allowing 6321 * clients to transition directly from IDLE to SYN_SENT 6322 * tcp_bindi will pick an unused port, insert the connection 6323 * in the bind hash and transition to BOUND state. 6324 */ 6325 lport = tcp_update_next_port(tcps->tcps_next_port_to_try, 6326 tcp, B_TRUE); 6327 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6328 B_FALSE, B_FALSE); 6329 if (lport == 0) { 6330 error = -TNOADDR; 6331 goto failed; 6332 } 6333 } 6334 tcp->tcp_state = TCPS_SYN_SENT; 6335 6336 mp = allocb(sizeof (ire_t), BPRI_HI); 6337 if (mp != NULL) { 6338 in6_addr_t v6src; 6339 6340 mp->b_wptr += sizeof (ire_t); 6341 mp->b_datap->db_type = IRE_DB_REQ_TYPE; 6342 6343 tcp->tcp_hard_binding = 1; 6344 6345 /* 6346 * We need to make sure that the conn_recv is set to a non-null 6347 * value before we insert the conn_t into the classifier table. 6348 * This is to avoid a race with an incoming packet which does 6349 * an ipcl_classify(). 6350 */ 6351 tcp->tcp_connp->conn_recv = tcp_input; 6352 6353 if (tcp->tcp_ipversion == IPV4_VERSION) { 6354 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6src); 6355 } else { 6356 v6src = tcp->tcp_ip6h->ip6_src; 6357 } 6358 error = ip_proto_bind_connected_v6(connp, &mp, IPPROTO_TCP, 6359 &v6src, tcp->tcp_lport, &tcp->tcp_remote_v6, 6360 &tcp->tcp_sticky_ipp, tcp->tcp_fport, B_TRUE, B_TRUE, cr); 6361 BUMP_MIB(&tcps->tcps_mib, tcpActiveOpens); 6362 tcp->tcp_active_open = 1; 6363 6364 return (tcp_post_ip_bind(tcp, mp, error, cr, pid)); 6365 } 6366 /* Error case */ 6367 tcp->tcp_state = oldstate; 6368 error = ENOMEM; 6369 6370 failed: 6371 /* return error ack and blow away saved option results if any */ 6372 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6373 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6374 return (error); 6375 } 6376 6377 /* 6378 * We need a stream q for detached closing tcp connections 6379 * to use. Our client hereby indicates that this q is the 6380 * one to use. 6381 */ 6382 static void 6383 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6384 { 6385 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6386 queue_t *q = tcp->tcp_wq; 6387 tcp_stack_t *tcps = tcp->tcp_tcps; 6388 6389 #ifdef NS_DEBUG 6390 (void) printf("TCP_IOC_DEFAULT_Q for stack %d\n", 6391 tcps->tcps_netstack->netstack_stackid); 6392 #endif 6393 mp->b_datap->db_type = M_IOCACK; 6394 iocp->ioc_count = 0; 6395 mutex_enter(&tcps->tcps_g_q_lock); 6396 if (tcps->tcps_g_q != NULL) { 6397 mutex_exit(&tcps->tcps_g_q_lock); 6398 iocp->ioc_error = EALREADY; 6399 } else { 6400 int error = 0; 6401 conn_t *connp = tcp->tcp_connp; 6402 ip_stack_t *ipst = connp->conn_netstack->netstack_ip; 6403 6404 tcps->tcps_g_q = tcp->tcp_rq; 6405 mutex_exit(&tcps->tcps_g_q_lock); 6406 iocp->ioc_error = 0; 6407 iocp->ioc_rval = 0; 6408 /* 6409 * We are passing tcp_sticky_ipp as NULL 6410 * as it is not useful for tcp_default queue 6411 * 6412 * Set conn_recv just in case. 6413 */ 6414 tcp->tcp_connp->conn_recv = tcp_conn_request; 6415 6416 ASSERT(connp->conn_af_isv6); 6417 connp->conn_ulp = IPPROTO_TCP; 6418 6419 if (ipst->ips_ipcl_proto_fanout_v6[IPPROTO_TCP].connf_head != 6420 NULL || connp->conn_mac_exempt) { 6421 error = -TBADADDR; 6422 } else { 6423 connp->conn_srcv6 = ipv6_all_zeros; 6424 ipcl_proto_insert_v6(connp, IPPROTO_TCP); 6425 } 6426 6427 (void) tcp_post_ip_bind(tcp, NULL, error, NULL, 0); 6428 } 6429 qreply(q, mp); 6430 } 6431 6432 static int 6433 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum) 6434 { 6435 tcp_t *ltcp = NULL; 6436 conn_t *connp; 6437 tcp_stack_t *tcps = tcp->tcp_tcps; 6438 6439 /* 6440 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6441 * when the stream is in BOUND state. Do not send a reset, 6442 * since the destination IP address is not valid, and it can 6443 * be the initialized value of all zeros (broadcast address). 6444 * 6445 * XXX There won't be any pending bind request to IP. 6446 */ 6447 if (tcp->tcp_state <= TCPS_BOUND) { 6448 if (tcp->tcp_debug) { 6449 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6450 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6451 } 6452 return (TOUTSTATE); 6453 } 6454 6455 6456 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6457 6458 /* 6459 * According to TPI, for non-listeners, ignore seqnum 6460 * and disconnect. 6461 * Following interpretation of -1 seqnum is historical 6462 * and implied TPI ? (TPI only states that for T_CONN_IND, 6463 * a valid seqnum should not be -1). 6464 * 6465 * -1 means disconnect everything 6466 * regardless even on a listener. 6467 */ 6468 6469 int old_state = tcp->tcp_state; 6470 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 6471 6472 /* 6473 * The connection can't be on the tcp_time_wait_head list 6474 * since it is not detached. 6475 */ 6476 ASSERT(tcp->tcp_time_wait_next == NULL); 6477 ASSERT(tcp->tcp_time_wait_prev == NULL); 6478 ASSERT(tcp->tcp_time_wait_expire == 0); 6479 ltcp = NULL; 6480 /* 6481 * If it used to be a listener, check to make sure no one else 6482 * has taken the port before switching back to LISTEN state. 6483 */ 6484 if (tcp->tcp_ipversion == IPV4_VERSION) { 6485 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6486 tcp->tcp_ipha->ipha_src, 6487 tcp->tcp_connp->conn_zoneid, ipst); 6488 if (connp != NULL) 6489 ltcp = connp->conn_tcp; 6490 } else { 6491 /* Allow tcp_bound_if listeners? */ 6492 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6493 &tcp->tcp_ip6h->ip6_src, 0, 6494 tcp->tcp_connp->conn_zoneid, ipst); 6495 if (connp != NULL) 6496 ltcp = connp->conn_tcp; 6497 } 6498 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6499 tcp->tcp_state = TCPS_LISTEN; 6500 } else if (old_state > TCPS_BOUND) { 6501 tcp->tcp_conn_req_max = 0; 6502 tcp->tcp_state = TCPS_BOUND; 6503 } 6504 if (ltcp != NULL) 6505 CONN_DEC_REF(ltcp->tcp_connp); 6506 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6507 BUMP_MIB(&tcps->tcps_mib, tcpAttemptFails); 6508 } else if (old_state == TCPS_ESTABLISHED || 6509 old_state == TCPS_CLOSE_WAIT) { 6510 BUMP_MIB(&tcps->tcps_mib, tcpEstabResets); 6511 } 6512 6513 if (tcp->tcp_fused) 6514 tcp_unfuse(tcp); 6515 6516 mutex_enter(&tcp->tcp_eager_lock); 6517 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6518 (tcp->tcp_conn_req_cnt_q != 0)) { 6519 tcp_eager_cleanup(tcp, 0); 6520 } 6521 mutex_exit(&tcp->tcp_eager_lock); 6522 6523 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6524 tcp->tcp_rnxt, TH_RST | TH_ACK); 6525 6526 tcp_reinit(tcp); 6527 6528 return (0); 6529 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6530 return (TBADSEQ); 6531 } 6532 return (0); 6533 } 6534 6535 /* 6536 * Our client hereby directs us to reject the connection request 6537 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6538 * of sending the appropriate RST, not an ICMP error. 6539 */ 6540 static void 6541 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6542 { 6543 t_scalar_t seqnum; 6544 int error; 6545 6546 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6547 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6548 tcp_err_ack(tcp, mp, TPROTO, 0); 6549 return; 6550 } 6551 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6552 error = tcp_disconnect_common(tcp, seqnum); 6553 if (error != 0) 6554 tcp_err_ack(tcp, mp, error, 0); 6555 else { 6556 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6557 /* Send M_FLUSH according to TPI */ 6558 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6559 } 6560 mp = mi_tpi_ok_ack_alloc(mp); 6561 if (mp) 6562 putnext(tcp->tcp_rq, mp); 6563 } 6564 } 6565 6566 /* 6567 * Diagnostic routine used to return a string associated with the tcp state. 6568 * Note that if the caller does not supply a buffer, it will use an internal 6569 * static string. This means that if multiple threads call this function at 6570 * the same time, output can be corrupted... Note also that this function 6571 * does not check the size of the supplied buffer. The caller has to make 6572 * sure that it is big enough. 6573 */ 6574 static char * 6575 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6576 { 6577 char buf1[30]; 6578 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6579 char *buf; 6580 char *cp; 6581 in6_addr_t local, remote; 6582 char local_addrbuf[INET6_ADDRSTRLEN]; 6583 char remote_addrbuf[INET6_ADDRSTRLEN]; 6584 6585 if (sup_buf != NULL) 6586 buf = sup_buf; 6587 else 6588 buf = priv_buf; 6589 6590 if (tcp == NULL) 6591 return ("NULL_TCP"); 6592 switch (tcp->tcp_state) { 6593 case TCPS_CLOSED: 6594 cp = "TCP_CLOSED"; 6595 break; 6596 case TCPS_IDLE: 6597 cp = "TCP_IDLE"; 6598 break; 6599 case TCPS_BOUND: 6600 cp = "TCP_BOUND"; 6601 break; 6602 case TCPS_LISTEN: 6603 cp = "TCP_LISTEN"; 6604 break; 6605 case TCPS_SYN_SENT: 6606 cp = "TCP_SYN_SENT"; 6607 break; 6608 case TCPS_SYN_RCVD: 6609 cp = "TCP_SYN_RCVD"; 6610 break; 6611 case TCPS_ESTABLISHED: 6612 cp = "TCP_ESTABLISHED"; 6613 break; 6614 case TCPS_CLOSE_WAIT: 6615 cp = "TCP_CLOSE_WAIT"; 6616 break; 6617 case TCPS_FIN_WAIT_1: 6618 cp = "TCP_FIN_WAIT_1"; 6619 break; 6620 case TCPS_CLOSING: 6621 cp = "TCP_CLOSING"; 6622 break; 6623 case TCPS_LAST_ACK: 6624 cp = "TCP_LAST_ACK"; 6625 break; 6626 case TCPS_FIN_WAIT_2: 6627 cp = "TCP_FIN_WAIT_2"; 6628 break; 6629 case TCPS_TIME_WAIT: 6630 cp = "TCP_TIME_WAIT"; 6631 break; 6632 default: 6633 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6634 cp = buf1; 6635 break; 6636 } 6637 switch (format) { 6638 case DISP_ADDR_AND_PORT: 6639 if (tcp->tcp_ipversion == IPV4_VERSION) { 6640 /* 6641 * Note that we use the remote address in the tcp_b 6642 * structure. This means that it will print out 6643 * the real destination address, not the next hop's 6644 * address if source routing is used. 6645 */ 6646 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6647 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6648 6649 } else { 6650 local = tcp->tcp_ip_src_v6; 6651 remote = tcp->tcp_remote_v6; 6652 } 6653 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6654 sizeof (local_addrbuf)); 6655 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6656 sizeof (remote_addrbuf)); 6657 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6658 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6659 ntohs(tcp->tcp_fport), cp); 6660 break; 6661 case DISP_PORT_ONLY: 6662 default: 6663 (void) mi_sprintf(buf, "[%u, %u] %s", 6664 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6665 break; 6666 } 6667 6668 return (buf); 6669 } 6670 6671 /* 6672 * Called via squeue to get on to eager's perimeter. It sends a 6673 * TH_RST if eager is in the fanout table. The listener wants the 6674 * eager to disappear either by means of tcp_eager_blowoff() or 6675 * tcp_eager_cleanup() being called. tcp_eager_kill() can also be 6676 * called (via squeue) if the eager cannot be inserted in the 6677 * fanout table in tcp_conn_request(). 6678 */ 6679 /* ARGSUSED */ 6680 void 6681 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6682 { 6683 conn_t *econnp = (conn_t *)arg; 6684 tcp_t *eager = econnp->conn_tcp; 6685 tcp_t *listener = eager->tcp_listener; 6686 tcp_stack_t *tcps = eager->tcp_tcps; 6687 6688 /* 6689 * We could be called because listener is closing. Since 6690 * the eager is using listener's queue's, its not safe. 6691 * Better use the default queue just to send the TH_RST 6692 * out. 6693 */ 6694 ASSERT(tcps->tcps_g_q != NULL); 6695 eager->tcp_rq = tcps->tcps_g_q; 6696 eager->tcp_wq = WR(tcps->tcps_g_q); 6697 6698 /* 6699 * An eager's conn_fanout will be NULL if it's a duplicate 6700 * for an existing 4-tuples in the conn fanout table. 6701 * We don't want to send an RST out in such case. 6702 */ 6703 if (econnp->conn_fanout != NULL && eager->tcp_state > TCPS_LISTEN) { 6704 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6705 eager, eager->tcp_snxt, 0, TH_RST); 6706 } 6707 6708 /* We are here because listener wants this eager gone */ 6709 if (listener != NULL) { 6710 mutex_enter(&listener->tcp_eager_lock); 6711 tcp_eager_unlink(eager); 6712 if (eager->tcp_tconnind_started) { 6713 /* 6714 * The eager has sent a conn_ind up to the 6715 * listener but listener decides to close 6716 * instead. We need to drop the extra ref 6717 * placed on eager in tcp_rput_data() before 6718 * sending the conn_ind to listener. 6719 */ 6720 CONN_DEC_REF(econnp); 6721 } 6722 mutex_exit(&listener->tcp_eager_lock); 6723 CONN_DEC_REF(listener->tcp_connp); 6724 } 6725 6726 if (eager->tcp_state > TCPS_BOUND) 6727 tcp_close_detached(eager); 6728 } 6729 6730 /* 6731 * Reset any eager connection hanging off this listener marked 6732 * with 'seqnum' and then reclaim it's resources. 6733 */ 6734 static boolean_t 6735 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6736 { 6737 tcp_t *eager; 6738 mblk_t *mp; 6739 tcp_stack_t *tcps = listener->tcp_tcps; 6740 6741 TCP_STAT(tcps, tcp_eager_blowoff_calls); 6742 eager = listener; 6743 mutex_enter(&listener->tcp_eager_lock); 6744 do { 6745 eager = eager->tcp_eager_next_q; 6746 if (eager == NULL) { 6747 mutex_exit(&listener->tcp_eager_lock); 6748 return (B_FALSE); 6749 } 6750 } while (eager->tcp_conn_req_seqnum != seqnum); 6751 6752 if (eager->tcp_closemp_used) { 6753 mutex_exit(&listener->tcp_eager_lock); 6754 return (B_TRUE); 6755 } 6756 eager->tcp_closemp_used = B_TRUE; 6757 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6758 CONN_INC_REF(eager->tcp_connp); 6759 mutex_exit(&listener->tcp_eager_lock); 6760 mp = &eager->tcp_closemp; 6761 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6762 eager->tcp_connp, SQ_FILL, SQTAG_TCP_EAGER_BLOWOFF); 6763 return (B_TRUE); 6764 } 6765 6766 /* 6767 * Reset any eager connection hanging off this listener 6768 * and then reclaim it's resources. 6769 */ 6770 static void 6771 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6772 { 6773 tcp_t *eager; 6774 mblk_t *mp; 6775 tcp_stack_t *tcps = listener->tcp_tcps; 6776 6777 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6778 6779 if (!q0_only) { 6780 /* First cleanup q */ 6781 TCP_STAT(tcps, tcp_eager_blowoff_q); 6782 eager = listener->tcp_eager_next_q; 6783 while (eager != NULL) { 6784 if (!eager->tcp_closemp_used) { 6785 eager->tcp_closemp_used = B_TRUE; 6786 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6787 CONN_INC_REF(eager->tcp_connp); 6788 mp = &eager->tcp_closemp; 6789 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6790 tcp_eager_kill, eager->tcp_connp, 6791 SQ_FILL, SQTAG_TCP_EAGER_CLEANUP); 6792 } 6793 eager = eager->tcp_eager_next_q; 6794 } 6795 } 6796 /* Then cleanup q0 */ 6797 TCP_STAT(tcps, tcp_eager_blowoff_q0); 6798 eager = listener->tcp_eager_next_q0; 6799 while (eager != listener) { 6800 if (!eager->tcp_closemp_used) { 6801 eager->tcp_closemp_used = B_TRUE; 6802 TCP_DEBUG_GETPCSTACK(eager->tcmp_stk, 15); 6803 CONN_INC_REF(eager->tcp_connp); 6804 mp = &eager->tcp_closemp; 6805 SQUEUE_ENTER_ONE(eager->tcp_connp->conn_sqp, mp, 6806 tcp_eager_kill, eager->tcp_connp, SQ_FILL, 6807 SQTAG_TCP_EAGER_CLEANUP_Q0); 6808 } 6809 eager = eager->tcp_eager_next_q0; 6810 } 6811 } 6812 6813 /* 6814 * If we are an eager connection hanging off a listener that hasn't 6815 * formally accepted the connection yet, get off his list and blow off 6816 * any data that we have accumulated. 6817 */ 6818 static void 6819 tcp_eager_unlink(tcp_t *tcp) 6820 { 6821 tcp_t *listener = tcp->tcp_listener; 6822 6823 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6824 ASSERT(listener != NULL); 6825 if (tcp->tcp_eager_next_q0 != NULL) { 6826 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6827 6828 /* Remove the eager tcp from q0 */ 6829 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6830 tcp->tcp_eager_prev_q0; 6831 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6832 tcp->tcp_eager_next_q0; 6833 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6834 listener->tcp_conn_req_cnt_q0--; 6835 6836 tcp->tcp_eager_next_q0 = NULL; 6837 tcp->tcp_eager_prev_q0 = NULL; 6838 6839 /* 6840 * Take the eager out, if it is in the list of droppable 6841 * eagers. 6842 */ 6843 MAKE_UNDROPPABLE(tcp); 6844 6845 if (tcp->tcp_syn_rcvd_timeout != 0) { 6846 /* we have timed out before */ 6847 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6848 listener->tcp_syn_rcvd_timeout--; 6849 } 6850 } else { 6851 tcp_t **tcpp = &listener->tcp_eager_next_q; 6852 tcp_t *prev = NULL; 6853 6854 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6855 if (tcpp[0] == tcp) { 6856 if (listener->tcp_eager_last_q == tcp) { 6857 /* 6858 * If we are unlinking the last 6859 * element on the list, adjust 6860 * tail pointer. Set tail pointer 6861 * to nil when list is empty. 6862 */ 6863 ASSERT(tcp->tcp_eager_next_q == NULL); 6864 if (listener->tcp_eager_last_q == 6865 listener->tcp_eager_next_q) { 6866 listener->tcp_eager_last_q = 6867 NULL; 6868 } else { 6869 /* 6870 * We won't get here if there 6871 * is only one eager in the 6872 * list. 6873 */ 6874 ASSERT(prev != NULL); 6875 listener->tcp_eager_last_q = 6876 prev; 6877 } 6878 } 6879 tcpp[0] = tcp->tcp_eager_next_q; 6880 tcp->tcp_eager_next_q = NULL; 6881 tcp->tcp_eager_last_q = NULL; 6882 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6883 listener->tcp_conn_req_cnt_q--; 6884 break; 6885 } 6886 prev = tcpp[0]; 6887 } 6888 } 6889 tcp->tcp_listener = NULL; 6890 } 6891 6892 /* Shorthand to generate and send TPI error acks to our client */ 6893 static void 6894 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6895 { 6896 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6897 putnext(tcp->tcp_rq, mp); 6898 } 6899 6900 /* Shorthand to generate and send TPI error acks to our client */ 6901 static void 6902 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6903 int t_error, int sys_error) 6904 { 6905 struct T_error_ack *teackp; 6906 6907 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6908 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6909 teackp = (struct T_error_ack *)mp->b_rptr; 6910 teackp->ERROR_prim = primitive; 6911 teackp->TLI_error = t_error; 6912 teackp->UNIX_error = sys_error; 6913 putnext(tcp->tcp_rq, mp); 6914 } 6915 } 6916 6917 /* 6918 * Note: No locks are held when inspecting tcp_g_*epriv_ports 6919 * but instead the code relies on: 6920 * - the fact that the address of the array and its size never changes 6921 * - the atomic assignment of the elements of the array 6922 */ 6923 /* ARGSUSED */ 6924 static int 6925 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 6926 { 6927 int i; 6928 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6929 6930 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6931 if (tcps->tcps_g_epriv_ports[i] != 0) 6932 (void) mi_mpprintf(mp, "%d ", 6933 tcps->tcps_g_epriv_ports[i]); 6934 } 6935 return (0); 6936 } 6937 6938 /* 6939 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6940 * threads from changing it at the same time. 6941 */ 6942 /* ARGSUSED */ 6943 static int 6944 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6945 cred_t *cr) 6946 { 6947 long new_value; 6948 int i; 6949 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6950 6951 /* 6952 * Fail the request if the new value does not lie within the 6953 * port number limits. 6954 */ 6955 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 6956 new_value <= 0 || new_value >= 65536) { 6957 return (EINVAL); 6958 } 6959 6960 mutex_enter(&tcps->tcps_epriv_port_lock); 6961 /* Check if the value is already in the list */ 6962 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6963 if (new_value == tcps->tcps_g_epriv_ports[i]) { 6964 mutex_exit(&tcps->tcps_epriv_port_lock); 6965 return (EEXIST); 6966 } 6967 } 6968 /* Find an empty slot */ 6969 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 6970 if (tcps->tcps_g_epriv_ports[i] == 0) 6971 break; 6972 } 6973 if (i == tcps->tcps_g_num_epriv_ports) { 6974 mutex_exit(&tcps->tcps_epriv_port_lock); 6975 return (EOVERFLOW); 6976 } 6977 /* Set the new value */ 6978 tcps->tcps_g_epriv_ports[i] = (uint16_t)new_value; 6979 mutex_exit(&tcps->tcps_epriv_port_lock); 6980 return (0); 6981 } 6982 6983 /* 6984 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6985 * threads from changing it at the same time. 6986 */ 6987 /* ARGSUSED */ 6988 static int 6989 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6990 cred_t *cr) 6991 { 6992 long new_value; 6993 int i; 6994 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 6995 6996 /* 6997 * Fail the request if the new value does not lie within the 6998 * port number limits. 6999 */ 7000 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7001 new_value >= 65536) { 7002 return (EINVAL); 7003 } 7004 7005 mutex_enter(&tcps->tcps_epriv_port_lock); 7006 /* Check that the value is already in the list */ 7007 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 7008 if (tcps->tcps_g_epriv_ports[i] == new_value) 7009 break; 7010 } 7011 if (i == tcps->tcps_g_num_epriv_ports) { 7012 mutex_exit(&tcps->tcps_epriv_port_lock); 7013 return (ESRCH); 7014 } 7015 /* Clear the value */ 7016 tcps->tcps_g_epriv_ports[i] = 0; 7017 mutex_exit(&tcps->tcps_epriv_port_lock); 7018 return (0); 7019 } 7020 7021 /* Return the TPI/TLI equivalent of our current tcp_state */ 7022 static int 7023 tcp_tpistate(tcp_t *tcp) 7024 { 7025 switch (tcp->tcp_state) { 7026 case TCPS_IDLE: 7027 return (TS_UNBND); 7028 case TCPS_LISTEN: 7029 /* 7030 * Return whether there are outstanding T_CONN_IND waiting 7031 * for the matching T_CONN_RES. Therefore don't count q0. 7032 */ 7033 if (tcp->tcp_conn_req_cnt_q > 0) 7034 return (TS_WRES_CIND); 7035 else 7036 return (TS_IDLE); 7037 case TCPS_BOUND: 7038 return (TS_IDLE); 7039 case TCPS_SYN_SENT: 7040 return (TS_WCON_CREQ); 7041 case TCPS_SYN_RCVD: 7042 /* 7043 * Note: assumption: this has to the active open SYN_RCVD. 7044 * The passive instance is detached in SYN_RCVD stage of 7045 * incoming connection processing so we cannot get request 7046 * for T_info_ack on it. 7047 */ 7048 return (TS_WACK_CRES); 7049 case TCPS_ESTABLISHED: 7050 return (TS_DATA_XFER); 7051 case TCPS_CLOSE_WAIT: 7052 return (TS_WREQ_ORDREL); 7053 case TCPS_FIN_WAIT_1: 7054 return (TS_WIND_ORDREL); 7055 case TCPS_FIN_WAIT_2: 7056 return (TS_WIND_ORDREL); 7057 7058 case TCPS_CLOSING: 7059 case TCPS_LAST_ACK: 7060 case TCPS_TIME_WAIT: 7061 case TCPS_CLOSED: 7062 /* 7063 * Following TS_WACK_DREQ7 is a rendition of "not 7064 * yet TS_IDLE" TPI state. There is no best match to any 7065 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7066 * choose a value chosen that will map to TLI/XTI level 7067 * state of TSTATECHNG (state is process of changing) which 7068 * captures what this dummy state represents. 7069 */ 7070 return (TS_WACK_DREQ7); 7071 default: 7072 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7073 tcp->tcp_state, tcp_display(tcp, NULL, 7074 DISP_PORT_ONLY)); 7075 return (TS_UNBND); 7076 } 7077 } 7078 7079 static void 7080 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7081 { 7082 tcp_stack_t *tcps = tcp->tcp_tcps; 7083 7084 if (tcp->tcp_family == AF_INET6) 7085 *tia = tcp_g_t_info_ack_v6; 7086 else 7087 *tia = tcp_g_t_info_ack; 7088 tia->CURRENT_state = tcp_tpistate(tcp); 7089 tia->OPT_size = tcp_max_optsize; 7090 if (tcp->tcp_mss == 0) { 7091 /* Not yet set - tcp_open does not set mss */ 7092 if (tcp->tcp_ipversion == IPV4_VERSION) 7093 tia->TIDU_size = tcps->tcps_mss_def_ipv4; 7094 else 7095 tia->TIDU_size = tcps->tcps_mss_def_ipv6; 7096 } else { 7097 tia->TIDU_size = tcp->tcp_mss; 7098 } 7099 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7100 } 7101 7102 static void 7103 tcp_do_capability_ack(tcp_t *tcp, struct T_capability_ack *tcap, 7104 t_uscalar_t cap_bits1) 7105 { 7106 tcap->CAP_bits1 = 0; 7107 7108 if (cap_bits1 & TC1_INFO) { 7109 tcp_copy_info(&tcap->INFO_ack, tcp); 7110 tcap->CAP_bits1 |= TC1_INFO; 7111 } 7112 7113 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7114 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7115 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7116 } 7117 7118 } 7119 7120 /* 7121 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7122 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7123 * tcp_g_t_info_ack. The current state of the stream is copied from 7124 * tcp_state. 7125 */ 7126 static void 7127 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7128 { 7129 t_uscalar_t cap_bits1; 7130 struct T_capability_ack *tcap; 7131 7132 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7133 freemsg(mp); 7134 return; 7135 } 7136 7137 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7138 7139 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7140 mp->b_datap->db_type, T_CAPABILITY_ACK); 7141 if (mp == NULL) 7142 return; 7143 7144 tcap = (struct T_capability_ack *)mp->b_rptr; 7145 tcp_do_capability_ack(tcp, tcap, cap_bits1); 7146 7147 putnext(tcp->tcp_rq, mp); 7148 } 7149 7150 /* 7151 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7152 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7153 * The current state of the stream is copied from tcp_state. 7154 */ 7155 static void 7156 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7157 { 7158 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7159 T_INFO_ACK); 7160 if (!mp) { 7161 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7162 return; 7163 } 7164 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7165 putnext(tcp->tcp_rq, mp); 7166 } 7167 7168 /* Respond to the TPI addr request */ 7169 static void 7170 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7171 { 7172 sin_t *sin; 7173 mblk_t *ackmp; 7174 struct T_addr_ack *taa; 7175 7176 /* Make it large enough for worst case */ 7177 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7178 2 * sizeof (sin6_t), 1); 7179 if (ackmp == NULL) { 7180 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7181 return; 7182 } 7183 7184 if (tcp->tcp_ipversion == IPV6_VERSION) { 7185 tcp_addr_req_ipv6(tcp, ackmp); 7186 return; 7187 } 7188 taa = (struct T_addr_ack *)ackmp->b_rptr; 7189 7190 bzero(taa, sizeof (struct T_addr_ack)); 7191 ackmp->b_wptr = (uchar_t *)&taa[1]; 7192 7193 taa->PRIM_type = T_ADDR_ACK; 7194 ackmp->b_datap->db_type = M_PCPROTO; 7195 7196 /* 7197 * Note: Following code assumes 32 bit alignment of basic 7198 * data structures like sin_t and struct T_addr_ack. 7199 */ 7200 if (tcp->tcp_state >= TCPS_BOUND) { 7201 /* 7202 * Fill in local address 7203 */ 7204 taa->LOCADDR_length = sizeof (sin_t); 7205 taa->LOCADDR_offset = sizeof (*taa); 7206 7207 sin = (sin_t *)&taa[1]; 7208 7209 /* Fill zeroes and then intialize non-zero fields */ 7210 *sin = sin_null; 7211 7212 sin->sin_family = AF_INET; 7213 7214 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7215 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7216 7217 ackmp->b_wptr = (uchar_t *)&sin[1]; 7218 7219 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7220 /* 7221 * Fill in Remote address 7222 */ 7223 taa->REMADDR_length = sizeof (sin_t); 7224 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7225 taa->LOCADDR_length); 7226 7227 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7228 *sin = sin_null; 7229 sin->sin_family = AF_INET; 7230 sin->sin_addr.s_addr = tcp->tcp_remote; 7231 sin->sin_port = tcp->tcp_fport; 7232 7233 ackmp->b_wptr = (uchar_t *)&sin[1]; 7234 } 7235 } 7236 putnext(tcp->tcp_rq, ackmp); 7237 } 7238 7239 /* Assumes that tcp_addr_req gets enough space and alignment */ 7240 static void 7241 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7242 { 7243 sin6_t *sin6; 7244 struct T_addr_ack *taa; 7245 7246 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7247 ASSERT(OK_32PTR(ackmp->b_rptr)); 7248 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7249 2 * sizeof (sin6_t)); 7250 7251 taa = (struct T_addr_ack *)ackmp->b_rptr; 7252 7253 bzero(taa, sizeof (struct T_addr_ack)); 7254 ackmp->b_wptr = (uchar_t *)&taa[1]; 7255 7256 taa->PRIM_type = T_ADDR_ACK; 7257 ackmp->b_datap->db_type = M_PCPROTO; 7258 7259 /* 7260 * Note: Following code assumes 32 bit alignment of basic 7261 * data structures like sin6_t and struct T_addr_ack. 7262 */ 7263 if (tcp->tcp_state >= TCPS_BOUND) { 7264 /* 7265 * Fill in local address 7266 */ 7267 taa->LOCADDR_length = sizeof (sin6_t); 7268 taa->LOCADDR_offset = sizeof (*taa); 7269 7270 sin6 = (sin6_t *)&taa[1]; 7271 *sin6 = sin6_null; 7272 7273 sin6->sin6_family = AF_INET6; 7274 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7275 sin6->sin6_port = tcp->tcp_lport; 7276 7277 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7278 7279 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7280 /* 7281 * Fill in Remote address 7282 */ 7283 taa->REMADDR_length = sizeof (sin6_t); 7284 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7285 taa->LOCADDR_length); 7286 7287 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7288 *sin6 = sin6_null; 7289 sin6->sin6_family = AF_INET6; 7290 sin6->sin6_flowinfo = 7291 tcp->tcp_ip6h->ip6_vcf & 7292 ~IPV6_VERS_AND_FLOW_MASK; 7293 sin6->sin6_addr = tcp->tcp_remote_v6; 7294 sin6->sin6_port = tcp->tcp_fport; 7295 7296 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7297 } 7298 } 7299 putnext(tcp->tcp_rq, ackmp); 7300 } 7301 7302 /* 7303 * Handle reinitialization of a tcp structure. 7304 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7305 */ 7306 static void 7307 tcp_reinit(tcp_t *tcp) 7308 { 7309 mblk_t *mp; 7310 int err; 7311 tcp_stack_t *tcps = tcp->tcp_tcps; 7312 7313 TCP_STAT(tcps, tcp_reinit_calls); 7314 7315 /* tcp_reinit should never be called for detached tcp_t's */ 7316 ASSERT(tcp->tcp_listener == NULL); 7317 ASSERT((tcp->tcp_family == AF_INET && 7318 tcp->tcp_ipversion == IPV4_VERSION) || 7319 (tcp->tcp_family == AF_INET6 && 7320 (tcp->tcp_ipversion == IPV4_VERSION || 7321 tcp->tcp_ipversion == IPV6_VERSION))); 7322 7323 /* Cancel outstanding timers */ 7324 tcp_timers_stop(tcp); 7325 7326 /* 7327 * Reset everything in the state vector, after updating global 7328 * MIB data from instance counters. 7329 */ 7330 UPDATE_MIB(&tcps->tcps_mib, tcpHCInSegs, tcp->tcp_ibsegs); 7331 tcp->tcp_ibsegs = 0; 7332 UPDATE_MIB(&tcps->tcps_mib, tcpHCOutSegs, tcp->tcp_obsegs); 7333 tcp->tcp_obsegs = 0; 7334 7335 tcp_close_mpp(&tcp->tcp_xmit_head); 7336 if (tcp->tcp_snd_zcopy_aware) 7337 tcp_zcopy_notify(tcp); 7338 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7339 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7340 mutex_enter(&tcp->tcp_non_sq_lock); 7341 if (tcp->tcp_flow_stopped && 7342 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7343 tcp_clrqfull(tcp); 7344 } 7345 mutex_exit(&tcp->tcp_non_sq_lock); 7346 tcp_close_mpp(&tcp->tcp_reass_head); 7347 tcp->tcp_reass_tail = NULL; 7348 if (tcp->tcp_rcv_list != NULL) { 7349 /* Free b_next chain */ 7350 tcp_close_mpp(&tcp->tcp_rcv_list); 7351 tcp->tcp_rcv_last_head = NULL; 7352 tcp->tcp_rcv_last_tail = NULL; 7353 tcp->tcp_rcv_cnt = 0; 7354 } 7355 tcp->tcp_rcv_last_tail = NULL; 7356 7357 if ((mp = tcp->tcp_urp_mp) != NULL) { 7358 freemsg(mp); 7359 tcp->tcp_urp_mp = NULL; 7360 } 7361 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7362 freemsg(mp); 7363 tcp->tcp_urp_mark_mp = NULL; 7364 } 7365 if (tcp->tcp_fused_sigurg_mp != NULL) { 7366 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7367 freeb(tcp->tcp_fused_sigurg_mp); 7368 tcp->tcp_fused_sigurg_mp = NULL; 7369 } 7370 if (tcp->tcp_ordrel_mp != NULL) { 7371 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 7372 freeb(tcp->tcp_ordrel_mp); 7373 tcp->tcp_ordrel_mp = NULL; 7374 } 7375 7376 /* 7377 * Following is a union with two members which are 7378 * identical types and size so the following cleanup 7379 * is enough. 7380 */ 7381 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7382 7383 CL_INET_DISCONNECT(tcp->tcp_connp, tcp); 7384 7385 /* 7386 * The connection can't be on the tcp_time_wait_head list 7387 * since it is not detached. 7388 */ 7389 ASSERT(tcp->tcp_time_wait_next == NULL); 7390 ASSERT(tcp->tcp_time_wait_prev == NULL); 7391 ASSERT(tcp->tcp_time_wait_expire == 0); 7392 7393 if (tcp->tcp_kssl_pending) { 7394 tcp->tcp_kssl_pending = B_FALSE; 7395 7396 /* Don't reset if the initialized by bind. */ 7397 if (tcp->tcp_kssl_ent != NULL) { 7398 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7399 KSSL_NO_PROXY); 7400 } 7401 } 7402 if (tcp->tcp_kssl_ctx != NULL) { 7403 kssl_release_ctx(tcp->tcp_kssl_ctx); 7404 tcp->tcp_kssl_ctx = NULL; 7405 } 7406 7407 /* 7408 * Reset/preserve other values 7409 */ 7410 tcp_reinit_values(tcp); 7411 ipcl_hash_remove(tcp->tcp_connp); 7412 conn_delete_ire(tcp->tcp_connp, NULL); 7413 tcp_ipsec_cleanup(tcp); 7414 7415 if (tcp->tcp_conn_req_max != 0) { 7416 /* 7417 * This is the case when a TLI program uses the same 7418 * transport end point to accept a connection. This 7419 * makes the TCP both a listener and acceptor. When 7420 * this connection is closed, we need to set the state 7421 * back to TCPS_LISTEN. Make sure that the eager list 7422 * is reinitialized. 7423 * 7424 * Note that this stream is still bound to the four 7425 * tuples of the previous connection in IP. If a new 7426 * SYN with different foreign address comes in, IP will 7427 * not find it and will send it to the global queue. In 7428 * the global queue, TCP will do a tcp_lookup_listener() 7429 * to find this stream. This works because this stream 7430 * is only removed from connected hash. 7431 * 7432 */ 7433 tcp->tcp_state = TCPS_LISTEN; 7434 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7435 tcp->tcp_eager_next_drop_q0 = tcp; 7436 tcp->tcp_eager_prev_drop_q0 = tcp; 7437 tcp->tcp_connp->conn_recv = tcp_conn_request; 7438 if (tcp->tcp_family == AF_INET6) { 7439 ASSERT(tcp->tcp_connp->conn_af_isv6); 7440 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7441 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7442 } else { 7443 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7444 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7445 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7446 } 7447 } else { 7448 tcp->tcp_state = TCPS_BOUND; 7449 } 7450 7451 /* 7452 * Initialize to default values 7453 * Can't fail since enough header template space already allocated 7454 * at open(). 7455 */ 7456 err = tcp_init_values(tcp); 7457 ASSERT(err == 0); 7458 /* Restore state in tcp_tcph */ 7459 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7460 if (tcp->tcp_ipversion == IPV4_VERSION) 7461 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7462 else 7463 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7464 /* 7465 * Copy of the src addr. in tcp_t is needed in tcp_t 7466 * since the lookup funcs can only lookup on tcp_t 7467 */ 7468 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7469 7470 ASSERT(tcp->tcp_ptpbhn != NULL); 7471 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 7472 tcp->tcp_rq->q_hiwat = tcps->tcps_recv_hiwat; 7473 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 7474 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 7475 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 7476 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7477 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4; 7478 } 7479 7480 /* 7481 * Force values to zero that need be zero. 7482 * Do not touch values asociated with the BOUND or LISTEN state 7483 * since the connection will end up in that state after the reinit. 7484 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7485 * structure! 7486 */ 7487 static void 7488 tcp_reinit_values(tcp) 7489 tcp_t *tcp; 7490 { 7491 tcp_stack_t *tcps = tcp->tcp_tcps; 7492 7493 #ifndef lint 7494 #define DONTCARE(x) 7495 #define PRESERVE(x) 7496 #else 7497 #define DONTCARE(x) ((x) = (x)) 7498 #define PRESERVE(x) ((x) = (x)) 7499 #endif /* lint */ 7500 7501 PRESERVE(tcp->tcp_bind_hash_port); 7502 PRESERVE(tcp->tcp_bind_hash); 7503 PRESERVE(tcp->tcp_ptpbhn); 7504 PRESERVE(tcp->tcp_acceptor_hash); 7505 PRESERVE(tcp->tcp_ptpahn); 7506 7507 /* Should be ASSERT NULL on these with new code! */ 7508 ASSERT(tcp->tcp_time_wait_next == NULL); 7509 ASSERT(tcp->tcp_time_wait_prev == NULL); 7510 ASSERT(tcp->tcp_time_wait_expire == 0); 7511 PRESERVE(tcp->tcp_state); 7512 PRESERVE(tcp->tcp_rq); 7513 PRESERVE(tcp->tcp_wq); 7514 7515 ASSERT(tcp->tcp_xmit_head == NULL); 7516 ASSERT(tcp->tcp_xmit_last == NULL); 7517 ASSERT(tcp->tcp_unsent == 0); 7518 ASSERT(tcp->tcp_xmit_tail == NULL); 7519 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7520 7521 tcp->tcp_snxt = 0; /* Displayed in mib */ 7522 tcp->tcp_suna = 0; /* Displayed in mib */ 7523 tcp->tcp_swnd = 0; 7524 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7525 7526 ASSERT(tcp->tcp_ibsegs == 0); 7527 ASSERT(tcp->tcp_obsegs == 0); 7528 7529 if (tcp->tcp_iphc != NULL) { 7530 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7531 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7532 } 7533 7534 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7535 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7536 DONTCARE(tcp->tcp_ipha); 7537 DONTCARE(tcp->tcp_ip6h); 7538 DONTCARE(tcp->tcp_ip_hdr_len); 7539 DONTCARE(tcp->tcp_tcph); 7540 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7541 tcp->tcp_valid_bits = 0; 7542 7543 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7544 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7545 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7546 tcp->tcp_last_rcv_lbolt = 0; 7547 7548 tcp->tcp_init_cwnd = 0; 7549 7550 tcp->tcp_urp_last_valid = 0; 7551 tcp->tcp_hard_binding = 0; 7552 tcp->tcp_hard_bound = 0; 7553 PRESERVE(tcp->tcp_cred); 7554 PRESERVE(tcp->tcp_cpid); 7555 PRESERVE(tcp->tcp_open_time); 7556 PRESERVE(tcp->tcp_exclbind); 7557 7558 tcp->tcp_fin_acked = 0; 7559 tcp->tcp_fin_rcvd = 0; 7560 tcp->tcp_fin_sent = 0; 7561 tcp->tcp_ordrel_done = 0; 7562 7563 tcp->tcp_debug = 0; 7564 tcp->tcp_dontroute = 0; 7565 tcp->tcp_broadcast = 0; 7566 7567 tcp->tcp_useloopback = 0; 7568 tcp->tcp_reuseaddr = 0; 7569 tcp->tcp_oobinline = 0; 7570 tcp->tcp_dgram_errind = 0; 7571 7572 tcp->tcp_detached = 0; 7573 tcp->tcp_bind_pending = 0; 7574 tcp->tcp_unbind_pending = 0; 7575 7576 tcp->tcp_snd_ws_ok = B_FALSE; 7577 tcp->tcp_snd_ts_ok = B_FALSE; 7578 tcp->tcp_linger = 0; 7579 tcp->tcp_ka_enabled = 0; 7580 tcp->tcp_zero_win_probe = 0; 7581 7582 tcp->tcp_loopback = 0; 7583 tcp->tcp_refuse = 0; 7584 tcp->tcp_localnet = 0; 7585 tcp->tcp_syn_defense = 0; 7586 tcp->tcp_set_timer = 0; 7587 7588 tcp->tcp_active_open = 0; 7589 tcp->tcp_rexmit = B_FALSE; 7590 tcp->tcp_xmit_zc_clean = B_FALSE; 7591 7592 tcp->tcp_snd_sack_ok = B_FALSE; 7593 PRESERVE(tcp->tcp_recvdstaddr); 7594 tcp->tcp_hwcksum = B_FALSE; 7595 7596 tcp->tcp_ire_ill_check_done = B_FALSE; 7597 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7598 7599 tcp->tcp_mdt = B_FALSE; 7600 tcp->tcp_mdt_hdr_head = 0; 7601 tcp->tcp_mdt_hdr_tail = 0; 7602 7603 tcp->tcp_conn_def_q0 = 0; 7604 tcp->tcp_ip_forward_progress = B_FALSE; 7605 tcp->tcp_anon_priv_bind = 0; 7606 tcp->tcp_ecn_ok = B_FALSE; 7607 7608 tcp->tcp_cwr = B_FALSE; 7609 tcp->tcp_ecn_echo_on = B_FALSE; 7610 7611 if (tcp->tcp_sack_info != NULL) { 7612 if (tcp->tcp_notsack_list != NULL) { 7613 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7614 } 7615 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7616 tcp->tcp_sack_info = NULL; 7617 } 7618 7619 tcp->tcp_rcv_ws = 0; 7620 tcp->tcp_snd_ws = 0; 7621 tcp->tcp_ts_recent = 0; 7622 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7623 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7624 tcp->tcp_if_mtu = 0; 7625 7626 ASSERT(tcp->tcp_reass_head == NULL); 7627 ASSERT(tcp->tcp_reass_tail == NULL); 7628 7629 tcp->tcp_cwnd_cnt = 0; 7630 7631 ASSERT(tcp->tcp_rcv_list == NULL); 7632 ASSERT(tcp->tcp_rcv_last_head == NULL); 7633 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7634 ASSERT(tcp->tcp_rcv_cnt == 0); 7635 7636 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7637 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7638 tcp->tcp_csuna = 0; 7639 7640 tcp->tcp_rto = 0; /* Displayed in MIB */ 7641 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7642 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7643 tcp->tcp_rtt_update = 0; 7644 7645 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7646 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7647 7648 tcp->tcp_rack = 0; /* Displayed in mib */ 7649 tcp->tcp_rack_cnt = 0; 7650 tcp->tcp_rack_cur_max = 0; 7651 tcp->tcp_rack_abs_max = 0; 7652 7653 tcp->tcp_max_swnd = 0; 7654 7655 ASSERT(tcp->tcp_listener == NULL); 7656 7657 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7658 7659 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7660 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7661 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7662 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7663 7664 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7665 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7666 PRESERVE(tcp->tcp_conn_req_max); 7667 PRESERVE(tcp->tcp_conn_req_seqnum); 7668 7669 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7670 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7671 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7672 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7673 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7674 7675 tcp->tcp_lingertime = 0; 7676 7677 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7678 ASSERT(tcp->tcp_urp_mp == NULL); 7679 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7680 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7681 7682 ASSERT(tcp->tcp_eager_next_q == NULL); 7683 ASSERT(tcp->tcp_eager_last_q == NULL); 7684 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7685 tcp->tcp_eager_prev_q0 == NULL) || 7686 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7687 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7688 7689 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL && 7690 tcp->tcp_eager_prev_drop_q0 == NULL) || 7691 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0); 7692 7693 tcp->tcp_client_errno = 0; 7694 7695 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7696 7697 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7698 7699 PRESERVE(tcp->tcp_bound_source_v6); 7700 tcp->tcp_last_sent_len = 0; 7701 tcp->tcp_dupack_cnt = 0; 7702 7703 tcp->tcp_fport = 0; /* Displayed in MIB */ 7704 PRESERVE(tcp->tcp_lport); 7705 7706 PRESERVE(tcp->tcp_acceptor_lockp); 7707 7708 ASSERT(tcp->tcp_ordrel_mp == NULL); 7709 PRESERVE(tcp->tcp_acceptor_id); 7710 DONTCARE(tcp->tcp_ipsec_overhead); 7711 7712 PRESERVE(tcp->tcp_family); 7713 if (tcp->tcp_family == AF_INET6) { 7714 tcp->tcp_ipversion = IPV6_VERSION; 7715 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 7716 } else { 7717 tcp->tcp_ipversion = IPV4_VERSION; 7718 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 7719 } 7720 7721 tcp->tcp_bound_if = 0; 7722 tcp->tcp_ipv6_recvancillary = 0; 7723 tcp->tcp_recvifindex = 0; 7724 tcp->tcp_recvhops = 0; 7725 tcp->tcp_closed = 0; 7726 tcp->tcp_cleandeathtag = 0; 7727 if (tcp->tcp_hopopts != NULL) { 7728 mi_free(tcp->tcp_hopopts); 7729 tcp->tcp_hopopts = NULL; 7730 tcp->tcp_hopoptslen = 0; 7731 } 7732 ASSERT(tcp->tcp_hopoptslen == 0); 7733 if (tcp->tcp_dstopts != NULL) { 7734 mi_free(tcp->tcp_dstopts); 7735 tcp->tcp_dstopts = NULL; 7736 tcp->tcp_dstoptslen = 0; 7737 } 7738 ASSERT(tcp->tcp_dstoptslen == 0); 7739 if (tcp->tcp_rtdstopts != NULL) { 7740 mi_free(tcp->tcp_rtdstopts); 7741 tcp->tcp_rtdstopts = NULL; 7742 tcp->tcp_rtdstoptslen = 0; 7743 } 7744 ASSERT(tcp->tcp_rtdstoptslen == 0); 7745 if (tcp->tcp_rthdr != NULL) { 7746 mi_free(tcp->tcp_rthdr); 7747 tcp->tcp_rthdr = NULL; 7748 tcp->tcp_rthdrlen = 0; 7749 } 7750 ASSERT(tcp->tcp_rthdrlen == 0); 7751 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7752 7753 /* Reset fusion-related fields */ 7754 tcp->tcp_fused = B_FALSE; 7755 tcp->tcp_unfusable = B_FALSE; 7756 tcp->tcp_fused_sigurg = B_FALSE; 7757 tcp->tcp_direct_sockfs = B_FALSE; 7758 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7759 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7760 tcp->tcp_loopback_peer = NULL; 7761 tcp->tcp_fuse_rcv_hiwater = 0; 7762 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7763 tcp->tcp_fuse_rcv_unread_cnt = 0; 7764 7765 tcp->tcp_lso = B_FALSE; 7766 7767 tcp->tcp_in_ack_unsent = 0; 7768 tcp->tcp_cork = B_FALSE; 7769 tcp->tcp_tconnind_started = B_FALSE; 7770 7771 PRESERVE(tcp->tcp_squeue_bytes); 7772 7773 ASSERT(tcp->tcp_kssl_ctx == NULL); 7774 ASSERT(!tcp->tcp_kssl_pending); 7775 PRESERVE(tcp->tcp_kssl_ent); 7776 7777 tcp->tcp_closemp_used = B_FALSE; 7778 7779 PRESERVE(tcp->tcp_rsrv_mp); 7780 PRESERVE(tcp->tcp_rsrv_mp_lock); 7781 7782 #ifdef DEBUG 7783 DONTCARE(tcp->tcmp_stk[0]); 7784 #endif 7785 7786 PRESERVE(tcp->tcp_connid); 7787 7788 7789 #undef DONTCARE 7790 #undef PRESERVE 7791 } 7792 7793 /* 7794 * Allocate necessary resources and initialize state vector. 7795 * Guaranteed not to fail so that when an error is returned, 7796 * the caller doesn't need to do any additional cleanup. 7797 */ 7798 int 7799 tcp_init(tcp_t *tcp, queue_t *q) 7800 { 7801 int err; 7802 7803 tcp->tcp_rq = q; 7804 tcp->tcp_wq = WR(q); 7805 tcp->tcp_state = TCPS_IDLE; 7806 if ((err = tcp_init_values(tcp)) != 0) 7807 tcp_timers_stop(tcp); 7808 return (err); 7809 } 7810 7811 static int 7812 tcp_init_values(tcp_t *tcp) 7813 { 7814 int err; 7815 tcp_stack_t *tcps = tcp->tcp_tcps; 7816 7817 ASSERT((tcp->tcp_family == AF_INET && 7818 tcp->tcp_ipversion == IPV4_VERSION) || 7819 (tcp->tcp_family == AF_INET6 && 7820 (tcp->tcp_ipversion == IPV4_VERSION || 7821 tcp->tcp_ipversion == IPV6_VERSION))); 7822 7823 /* 7824 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7825 * will be close to tcp_rexmit_interval_initial. By doing this, we 7826 * allow the algorithm to adjust slowly to large fluctuations of RTT 7827 * during first few transmissions of a connection as seen in slow 7828 * links. 7829 */ 7830 tcp->tcp_rtt_sa = tcps->tcps_rexmit_interval_initial << 2; 7831 tcp->tcp_rtt_sd = tcps->tcps_rexmit_interval_initial >> 1; 7832 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7833 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7834 tcps->tcps_conn_grace_period; 7835 if (tcp->tcp_rto < tcps->tcps_rexmit_interval_min) 7836 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 7837 tcp->tcp_timer_backoff = 0; 7838 tcp->tcp_ms_we_have_waited = 0; 7839 tcp->tcp_last_recv_time = lbolt; 7840 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_; 7841 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7842 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7843 7844 tcp->tcp_maxpsz = tcps->tcps_maxpsz_multiplier; 7845 7846 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval; 7847 tcp->tcp_first_ctimer_threshold = tcps->tcps_ip_notify_cinterval; 7848 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval; 7849 /* 7850 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7851 * passive open. 7852 */ 7853 tcp->tcp_second_ctimer_threshold = tcps->tcps_ip_abort_cinterval; 7854 7855 tcp->tcp_naglim = tcps->tcps_naglim_def; 7856 7857 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7858 7859 tcp->tcp_mdt_hdr_head = 0; 7860 tcp->tcp_mdt_hdr_tail = 0; 7861 7862 /* Reset fusion-related fields */ 7863 tcp->tcp_fused = B_FALSE; 7864 tcp->tcp_unfusable = B_FALSE; 7865 tcp->tcp_fused_sigurg = B_FALSE; 7866 tcp->tcp_direct_sockfs = B_FALSE; 7867 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7868 tcp->tcp_fuse_syncstr_plugged = B_FALSE; 7869 tcp->tcp_loopback_peer = NULL; 7870 tcp->tcp_fuse_rcv_hiwater = 0; 7871 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7872 tcp->tcp_fuse_rcv_unread_cnt = 0; 7873 7874 /* Initialize the header template */ 7875 if (tcp->tcp_ipversion == IPV4_VERSION) { 7876 err = tcp_header_init_ipv4(tcp); 7877 } else { 7878 err = tcp_header_init_ipv6(tcp); 7879 } 7880 if (err) 7881 return (err); 7882 7883 /* 7884 * Init the window scale to the max so tcp_rwnd_set() won't pare 7885 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7886 */ 7887 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7888 tcp->tcp_xmit_lowater = tcps->tcps_xmit_lowat; 7889 tcp->tcp_xmit_hiwater = tcps->tcps_xmit_hiwat; 7890 7891 tcp->tcp_cork = B_FALSE; 7892 /* 7893 * Init the tcp_debug option. This value determines whether TCP 7894 * calls strlog() to print out debug messages. Doing this 7895 * initialization here means that this value is not inherited thru 7896 * tcp_reinit(). 7897 */ 7898 tcp->tcp_debug = tcps->tcps_dbg; 7899 7900 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval; 7901 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval; 7902 7903 return (0); 7904 } 7905 7906 /* 7907 * Initialize the IPv4 header. Loses any record of any IP options. 7908 */ 7909 static int 7910 tcp_header_init_ipv4(tcp_t *tcp) 7911 { 7912 tcph_t *tcph; 7913 uint32_t sum; 7914 conn_t *connp; 7915 tcp_stack_t *tcps = tcp->tcp_tcps; 7916 7917 /* 7918 * This is a simple initialization. If there's 7919 * already a template, it should never be too small, 7920 * so reuse it. Otherwise, allocate space for the new one. 7921 */ 7922 if (tcp->tcp_iphc == NULL) { 7923 ASSERT(tcp->tcp_iphc_len == 0); 7924 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7925 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7926 if (tcp->tcp_iphc == NULL) { 7927 tcp->tcp_iphc_len = 0; 7928 return (ENOMEM); 7929 } 7930 } 7931 7932 /* options are gone; may need a new label */ 7933 connp = tcp->tcp_connp; 7934 connp->conn_mlp_type = mlptSingle; 7935 connp->conn_ulp_labeled = !is_system_labeled(); 7936 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7937 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 7938 tcp->tcp_ip6h = NULL; 7939 tcp->tcp_ipversion = IPV4_VERSION; 7940 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 7941 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 7942 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 7943 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 7944 tcp->tcp_ipha->ipha_version_and_hdr_length 7945 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 7946 tcp->tcp_ipha->ipha_ident = 0; 7947 7948 tcp->tcp_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7949 tcp->tcp_tos = 0; 7950 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 7951 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 7952 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 7953 7954 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 7955 tcp->tcp_tcph = tcph; 7956 tcph->th_offset_and_rsrvd[0] = (5 << 4); 7957 /* 7958 * IP wants our header length in the checksum field to 7959 * allow it to perform a single pseudo-header+checksum 7960 * calculation on behalf of TCP. 7961 * Include the adjustment for a source route once IP_OPTIONS is set. 7962 */ 7963 sum = sizeof (tcph_t) + tcp->tcp_sum; 7964 sum = (sum >> 16) + (sum & 0xFFFF); 7965 U16_TO_ABE16(sum, tcph->th_sum); 7966 return (0); 7967 } 7968 7969 /* 7970 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 7971 */ 7972 static int 7973 tcp_header_init_ipv6(tcp_t *tcp) 7974 { 7975 tcph_t *tcph; 7976 uint32_t sum; 7977 conn_t *connp; 7978 tcp_stack_t *tcps = tcp->tcp_tcps; 7979 7980 /* 7981 * This is a simple initialization. If there's 7982 * already a template, it should never be too small, 7983 * so reuse it. Otherwise, allocate space for the new one. 7984 * Ensure that there is enough space to "downgrade" the tcp_t 7985 * to an IPv4 tcp_t. This requires having space for a full load 7986 * of IPv4 options, as well as a full load of TCP options 7987 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 7988 * than a v6 header and a TCP header with a full load of TCP options 7989 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 7990 * We want to avoid reallocation in the "downgraded" case when 7991 * processing outbound IPv4 options. 7992 */ 7993 if (tcp->tcp_iphc == NULL) { 7994 ASSERT(tcp->tcp_iphc_len == 0); 7995 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7996 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7997 if (tcp->tcp_iphc == NULL) { 7998 tcp->tcp_iphc_len = 0; 7999 return (ENOMEM); 8000 } 8001 } 8002 8003 /* options are gone; may need a new label */ 8004 connp = tcp->tcp_connp; 8005 connp->conn_mlp_type = mlptSingle; 8006 connp->conn_ulp_labeled = !is_system_labeled(); 8007 8008 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8009 tcp->tcp_ipversion = IPV6_VERSION; 8010 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8011 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8012 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8013 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8014 tcp->tcp_ipha = NULL; 8015 8016 /* Initialize the header template */ 8017 8018 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8019 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8020 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8021 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcps->tcps_ipv6_hoplimit; 8022 8023 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8024 tcp->tcp_tcph = tcph; 8025 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8026 /* 8027 * IP wants our header length in the checksum field to 8028 * allow it to perform a single psuedo-header+checksum 8029 * calculation on behalf of TCP. 8030 * Include the adjustment for a source route when IPV6_RTHDR is set. 8031 */ 8032 sum = sizeof (tcph_t) + tcp->tcp_sum; 8033 sum = (sum >> 16) + (sum & 0xFFFF); 8034 U16_TO_ABE16(sum, tcph->th_sum); 8035 return (0); 8036 } 8037 8038 /* At minimum we need 8 bytes in the TCP header for the lookup */ 8039 #define ICMP_MIN_TCP_HDR 8 8040 8041 /* 8042 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8043 * passed up by IP. The message is always received on the correct tcp_t. 8044 * Assumes that IP has pulled up everything up to and including the ICMP header. 8045 */ 8046 void 8047 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8048 { 8049 icmph_t *icmph; 8050 ipha_t *ipha; 8051 int iph_hdr_length; 8052 tcph_t *tcph; 8053 boolean_t ipsec_mctl = B_FALSE; 8054 boolean_t secure; 8055 mblk_t *first_mp = mp; 8056 int32_t new_mss; 8057 uint32_t ratio; 8058 size_t mp_size = MBLKL(mp); 8059 uint32_t seg_seq; 8060 tcp_stack_t *tcps = tcp->tcp_tcps; 8061 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 8062 8063 /* Assume IP provides aligned packets - otherwise toss */ 8064 if (!OK_32PTR(mp->b_rptr)) { 8065 freemsg(mp); 8066 return; 8067 } 8068 8069 /* 8070 * Since ICMP errors are normal data marked with M_CTL when sent 8071 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8072 * packets starting with an ipsec_info_t, see ipsec_info.h. 8073 */ 8074 if ((mp_size == sizeof (ipsec_info_t)) && 8075 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8076 ASSERT(mp->b_cont != NULL); 8077 mp = mp->b_cont; 8078 /* IP should have done this */ 8079 ASSERT(OK_32PTR(mp->b_rptr)); 8080 mp_size = MBLKL(mp); 8081 ipsec_mctl = B_TRUE; 8082 } 8083 8084 /* 8085 * Verify that we have a complete outer IP header. If not, drop it. 8086 */ 8087 if (mp_size < sizeof (ipha_t)) { 8088 noticmpv4: 8089 freemsg(first_mp); 8090 return; 8091 } 8092 8093 ipha = (ipha_t *)mp->b_rptr; 8094 /* 8095 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8096 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8097 */ 8098 switch (IPH_HDR_VERSION(ipha)) { 8099 case IPV6_VERSION: 8100 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8101 return; 8102 case IPV4_VERSION: 8103 break; 8104 default: 8105 goto noticmpv4; 8106 } 8107 8108 /* Skip past the outer IP and ICMP headers */ 8109 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8110 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8111 /* 8112 * If we don't have the correct outer IP header length or if the ULP 8113 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8114 * send it upstream. 8115 */ 8116 if (iph_hdr_length < sizeof (ipha_t) || 8117 ipha->ipha_protocol != IPPROTO_ICMP || 8118 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8119 goto noticmpv4; 8120 } 8121 ipha = (ipha_t *)&icmph[1]; 8122 8123 /* Skip past the inner IP and find the ULP header */ 8124 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8125 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8126 /* 8127 * If we don't have the correct inner IP header length or if the ULP 8128 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8129 * bytes of TCP header, drop it. 8130 */ 8131 if (iph_hdr_length < sizeof (ipha_t) || 8132 ipha->ipha_protocol != IPPROTO_TCP || 8133 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8134 goto noticmpv4; 8135 } 8136 8137 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8138 if (ipsec_mctl) { 8139 secure = ipsec_in_is_secure(first_mp); 8140 } else { 8141 secure = B_FALSE; 8142 } 8143 if (secure) { 8144 /* 8145 * If we are willing to accept this in clear 8146 * we don't have to verify policy. 8147 */ 8148 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8149 if (!tcp_check_policy(tcp, first_mp, 8150 ipha, NULL, secure, ipsec_mctl)) { 8151 /* 8152 * tcp_check_policy called 8153 * ip_drop_packet() on failure. 8154 */ 8155 return; 8156 } 8157 } 8158 } 8159 } else if (ipsec_mctl) { 8160 /* 8161 * This is a hard_bound connection. IP has already 8162 * verified policy. We don't have to do it again. 8163 */ 8164 freeb(first_mp); 8165 first_mp = mp; 8166 ipsec_mctl = B_FALSE; 8167 } 8168 8169 seg_seq = ABE32_TO_U32(tcph->th_seq); 8170 /* 8171 * TCP SHOULD check that the TCP sequence number contained in 8172 * payload of the ICMP error message is within the range 8173 * SND.UNA <= SEG.SEQ < SND.NXT. 8174 */ 8175 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8176 /* 8177 * The ICMP message is bogus, just drop it. But if this is 8178 * an ICMP too big message, IP has already changed 8179 * the ire_max_frag to the bogus value. We need to change 8180 * it back. 8181 */ 8182 if (icmph->icmph_type == ICMP_DEST_UNREACHABLE && 8183 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) { 8184 conn_t *connp = tcp->tcp_connp; 8185 ire_t *ire; 8186 int flag; 8187 8188 if (tcp->tcp_ipversion == IPV4_VERSION) { 8189 flag = tcp->tcp_ipha-> 8190 ipha_fragment_offset_and_flags; 8191 } else { 8192 flag = 0; 8193 } 8194 mutex_enter(&connp->conn_lock); 8195 if ((ire = connp->conn_ire_cache) != NULL) { 8196 mutex_enter(&ire->ire_lock); 8197 mutex_exit(&connp->conn_lock); 8198 ire->ire_max_frag = tcp->tcp_if_mtu; 8199 ire->ire_frag_flag |= flag; 8200 mutex_exit(&ire->ire_lock); 8201 } else { 8202 mutex_exit(&connp->conn_lock); 8203 } 8204 } 8205 goto noticmpv4; 8206 } 8207 8208 switch (icmph->icmph_type) { 8209 case ICMP_DEST_UNREACHABLE: 8210 switch (icmph->icmph_code) { 8211 case ICMP_FRAGMENTATION_NEEDED: 8212 /* 8213 * Reduce the MSS based on the new MTU. This will 8214 * eliminate any fragmentation locally. 8215 * N.B. There may well be some funny side-effects on 8216 * the local send policy and the remote receive policy. 8217 * Pending further research, we provide 8218 * tcp_ignore_path_mtu just in case this proves 8219 * disastrous somewhere. 8220 * 8221 * After updating the MSS, retransmit part of the 8222 * dropped segment using the new mss by calling 8223 * tcp_wput_data(). Need to adjust all those 8224 * params to make sure tcp_wput_data() work properly. 8225 */ 8226 if (tcps->tcps_ignore_path_mtu || 8227 tcp->tcp_ipha->ipha_fragment_offset_and_flags == 0) 8228 break; 8229 8230 /* 8231 * Decrease the MSS by time stamp options 8232 * IP options and IPSEC options. tcp_hdr_len 8233 * includes time stamp option and IP option 8234 * length. Note that new_mss may be negative 8235 * if tcp_ipsec_overhead is large and the 8236 * icmph_du_mtu is the minimum value, which is 68. 8237 */ 8238 new_mss = ntohs(icmph->icmph_du_mtu) - 8239 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8240 8241 DTRACE_PROBE2(tcp__pmtu__change, tcp_t *, tcp, int, 8242 new_mss); 8243 8244 /* 8245 * Only update the MSS if the new one is 8246 * smaller than the previous one. This is 8247 * to avoid problems when getting multiple 8248 * ICMP errors for the same MTU. 8249 */ 8250 if (new_mss >= tcp->tcp_mss) 8251 break; 8252 8253 /* 8254 * Note that we are using the template header's DF 8255 * bit in the fast path sending. So we need to compare 8256 * the new mss with both tcps_mss_min and ip_pmtu_min. 8257 * And stop doing IPv4 PMTUd if new_mss is less than 8258 * MAX(tcps_mss_min, ip_pmtu_min). 8259 */ 8260 if (new_mss < tcps->tcps_mss_min || 8261 new_mss < ipst->ips_ip_pmtu_min) { 8262 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8263 0; 8264 } 8265 8266 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8267 ASSERT(ratio >= 1); 8268 tcp_mss_set(tcp, new_mss, B_TRUE); 8269 8270 /* 8271 * Make sure we have something to 8272 * send. 8273 */ 8274 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8275 (tcp->tcp_xmit_head != NULL)) { 8276 /* 8277 * Shrink tcp_cwnd in 8278 * proportion to the old MSS/new MSS. 8279 */ 8280 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8281 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8282 (tcp->tcp_unsent == 0)) { 8283 tcp->tcp_rexmit_max = tcp->tcp_fss; 8284 } else { 8285 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8286 } 8287 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8288 tcp->tcp_rexmit = B_TRUE; 8289 tcp->tcp_dupack_cnt = 0; 8290 tcp->tcp_snd_burst = TCP_CWND_SS; 8291 tcp_ss_rexmit(tcp); 8292 } 8293 break; 8294 case ICMP_PORT_UNREACHABLE: 8295 case ICMP_PROTOCOL_UNREACHABLE: 8296 switch (tcp->tcp_state) { 8297 case TCPS_SYN_SENT: 8298 case TCPS_SYN_RCVD: 8299 /* 8300 * ICMP can snipe away incipient 8301 * TCP connections as long as 8302 * seq number is same as initial 8303 * send seq number. 8304 */ 8305 if (seg_seq == tcp->tcp_iss) { 8306 (void) tcp_clean_death(tcp, 8307 ECONNREFUSED, 6); 8308 } 8309 break; 8310 } 8311 break; 8312 case ICMP_HOST_UNREACHABLE: 8313 case ICMP_NET_UNREACHABLE: 8314 /* Record the error in case we finally time out. */ 8315 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8316 tcp->tcp_client_errno = EHOSTUNREACH; 8317 else 8318 tcp->tcp_client_errno = ENETUNREACH; 8319 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8320 if (tcp->tcp_listener != NULL && 8321 tcp->tcp_listener->tcp_syn_defense) { 8322 /* 8323 * Ditch the half-open connection if we 8324 * suspect a SYN attack is under way. 8325 */ 8326 tcp_ip_ire_mark_advice(tcp); 8327 (void) tcp_clean_death(tcp, 8328 tcp->tcp_client_errno, 7); 8329 } 8330 } 8331 break; 8332 default: 8333 break; 8334 } 8335 break; 8336 case ICMP_SOURCE_QUENCH: { 8337 /* 8338 * use a global boolean to control 8339 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8340 * The default is false. 8341 */ 8342 if (tcp_icmp_source_quench) { 8343 /* 8344 * Reduce the sending rate as if we got a 8345 * retransmit timeout 8346 */ 8347 uint32_t npkt; 8348 8349 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8350 tcp->tcp_mss; 8351 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8352 tcp->tcp_cwnd = tcp->tcp_mss; 8353 tcp->tcp_cwnd_cnt = 0; 8354 } 8355 break; 8356 } 8357 } 8358 freemsg(first_mp); 8359 } 8360 8361 /* 8362 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8363 * error messages passed up by IP. 8364 * Assumes that IP has pulled up all the extension headers as well 8365 * as the ICMPv6 header. 8366 */ 8367 static void 8368 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8369 { 8370 icmp6_t *icmp6; 8371 ip6_t *ip6h; 8372 uint16_t iph_hdr_length; 8373 tcpha_t *tcpha; 8374 uint8_t *nexthdrp; 8375 uint32_t new_mss; 8376 uint32_t ratio; 8377 boolean_t secure; 8378 mblk_t *first_mp = mp; 8379 size_t mp_size; 8380 uint32_t seg_seq; 8381 tcp_stack_t *tcps = tcp->tcp_tcps; 8382 8383 /* 8384 * The caller has determined if this is an IPSEC_IN packet and 8385 * set ipsec_mctl appropriately (see tcp_icmp_error). 8386 */ 8387 if (ipsec_mctl) 8388 mp = mp->b_cont; 8389 8390 mp_size = MBLKL(mp); 8391 8392 /* 8393 * Verify that we have a complete IP header. If not, send it upstream. 8394 */ 8395 if (mp_size < sizeof (ip6_t)) { 8396 noticmpv6: 8397 freemsg(first_mp); 8398 return; 8399 } 8400 8401 /* 8402 * Verify this is an ICMPV6 packet, else send it upstream. 8403 */ 8404 ip6h = (ip6_t *)mp->b_rptr; 8405 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8406 iph_hdr_length = IPV6_HDR_LEN; 8407 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8408 &nexthdrp) || 8409 *nexthdrp != IPPROTO_ICMPV6) { 8410 goto noticmpv6; 8411 } 8412 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8413 ip6h = (ip6_t *)&icmp6[1]; 8414 /* 8415 * Verify if we have a complete ICMP and inner IP header. 8416 */ 8417 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8418 goto noticmpv6; 8419 8420 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8421 goto noticmpv6; 8422 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8423 /* 8424 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8425 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8426 * packet. 8427 */ 8428 if ((*nexthdrp != IPPROTO_TCP) || 8429 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8430 goto noticmpv6; 8431 } 8432 8433 /* 8434 * ICMP errors come on the right queue or come on 8435 * listener/global queue for detached connections and 8436 * get switched to the right queue. If it comes on the 8437 * right queue, policy check has already been done by IP 8438 * and thus free the first_mp without verifying the policy. 8439 * If it has come for a non-hard bound connection, we need 8440 * to verify policy as IP may not have done it. 8441 */ 8442 if (!tcp->tcp_hard_bound) { 8443 if (ipsec_mctl) { 8444 secure = ipsec_in_is_secure(first_mp); 8445 } else { 8446 secure = B_FALSE; 8447 } 8448 if (secure) { 8449 /* 8450 * If we are willing to accept this in clear 8451 * we don't have to verify policy. 8452 */ 8453 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8454 if (!tcp_check_policy(tcp, first_mp, 8455 NULL, ip6h, secure, ipsec_mctl)) { 8456 /* 8457 * tcp_check_policy called 8458 * ip_drop_packet() on failure. 8459 */ 8460 return; 8461 } 8462 } 8463 } 8464 } else if (ipsec_mctl) { 8465 /* 8466 * This is a hard_bound connection. IP has already 8467 * verified policy. We don't have to do it again. 8468 */ 8469 freeb(first_mp); 8470 first_mp = mp; 8471 ipsec_mctl = B_FALSE; 8472 } 8473 8474 seg_seq = ntohl(tcpha->tha_seq); 8475 /* 8476 * TCP SHOULD check that the TCP sequence number contained in 8477 * payload of the ICMP error message is within the range 8478 * SND.UNA <= SEG.SEQ < SND.NXT. 8479 */ 8480 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt)) { 8481 /* 8482 * If the ICMP message is bogus, should we kill the 8483 * connection, or should we just drop the bogus ICMP 8484 * message? It would probably make more sense to just 8485 * drop the message so that if this one managed to get 8486 * in, the real connection should not suffer. 8487 */ 8488 goto noticmpv6; 8489 } 8490 8491 switch (icmp6->icmp6_type) { 8492 case ICMP6_PACKET_TOO_BIG: 8493 /* 8494 * Reduce the MSS based on the new MTU. This will 8495 * eliminate any fragmentation locally. 8496 * N.B. There may well be some funny side-effects on 8497 * the local send policy and the remote receive policy. 8498 * Pending further research, we provide 8499 * tcp_ignore_path_mtu just in case this proves 8500 * disastrous somewhere. 8501 * 8502 * After updating the MSS, retransmit part of the 8503 * dropped segment using the new mss by calling 8504 * tcp_wput_data(). Need to adjust all those 8505 * params to make sure tcp_wput_data() work properly. 8506 */ 8507 if (tcps->tcps_ignore_path_mtu) 8508 break; 8509 8510 /* 8511 * Decrease the MSS by time stamp options 8512 * IP options and IPSEC options. tcp_hdr_len 8513 * includes time stamp option and IP option 8514 * length. 8515 */ 8516 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8517 tcp->tcp_ipsec_overhead; 8518 8519 /* 8520 * Only update the MSS if the new one is 8521 * smaller than the previous one. This is 8522 * to avoid problems when getting multiple 8523 * ICMP errors for the same MTU. 8524 */ 8525 if (new_mss >= tcp->tcp_mss) 8526 break; 8527 8528 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8529 ASSERT(ratio >= 1); 8530 tcp_mss_set(tcp, new_mss, B_TRUE); 8531 8532 /* 8533 * Make sure we have something to 8534 * send. 8535 */ 8536 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8537 (tcp->tcp_xmit_head != NULL)) { 8538 /* 8539 * Shrink tcp_cwnd in 8540 * proportion to the old MSS/new MSS. 8541 */ 8542 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8543 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8544 (tcp->tcp_unsent == 0)) { 8545 tcp->tcp_rexmit_max = tcp->tcp_fss; 8546 } else { 8547 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8548 } 8549 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8550 tcp->tcp_rexmit = B_TRUE; 8551 tcp->tcp_dupack_cnt = 0; 8552 tcp->tcp_snd_burst = TCP_CWND_SS; 8553 tcp_ss_rexmit(tcp); 8554 } 8555 break; 8556 8557 case ICMP6_DST_UNREACH: 8558 switch (icmp6->icmp6_code) { 8559 case ICMP6_DST_UNREACH_NOPORT: 8560 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8561 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8562 (seg_seq == tcp->tcp_iss)) { 8563 (void) tcp_clean_death(tcp, 8564 ECONNREFUSED, 8); 8565 } 8566 break; 8567 8568 case ICMP6_DST_UNREACH_ADMIN: 8569 case ICMP6_DST_UNREACH_NOROUTE: 8570 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8571 case ICMP6_DST_UNREACH_ADDR: 8572 /* Record the error in case we finally time out. */ 8573 tcp->tcp_client_errno = EHOSTUNREACH; 8574 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8575 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8576 (seg_seq == tcp->tcp_iss)) { 8577 if (tcp->tcp_listener != NULL && 8578 tcp->tcp_listener->tcp_syn_defense) { 8579 /* 8580 * Ditch the half-open connection if we 8581 * suspect a SYN attack is under way. 8582 */ 8583 tcp_ip_ire_mark_advice(tcp); 8584 (void) tcp_clean_death(tcp, 8585 tcp->tcp_client_errno, 9); 8586 } 8587 } 8588 8589 8590 break; 8591 default: 8592 break; 8593 } 8594 break; 8595 8596 case ICMP6_PARAM_PROB: 8597 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8598 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8599 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8600 (uchar_t *)nexthdrp) { 8601 if (tcp->tcp_state == TCPS_SYN_SENT || 8602 tcp->tcp_state == TCPS_SYN_RCVD) { 8603 (void) tcp_clean_death(tcp, 8604 ECONNREFUSED, 10); 8605 } 8606 break; 8607 } 8608 break; 8609 8610 case ICMP6_TIME_EXCEEDED: 8611 default: 8612 break; 8613 } 8614 freemsg(first_mp); 8615 } 8616 8617 /* 8618 * Notify IP that we are having trouble with this connection. IP should 8619 * blow the IRE away and start over. 8620 */ 8621 static void 8622 tcp_ip_notify(tcp_t *tcp) 8623 { 8624 struct iocblk *iocp; 8625 ipid_t *ipid; 8626 mblk_t *mp; 8627 8628 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8629 if (tcp->tcp_ipversion == IPV6_VERSION) 8630 return; 8631 8632 mp = mkiocb(IP_IOCTL); 8633 if (mp == NULL) 8634 return; 8635 8636 iocp = (struct iocblk *)mp->b_rptr; 8637 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8638 8639 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8640 if (!mp->b_cont) { 8641 freeb(mp); 8642 return; 8643 } 8644 8645 ipid = (ipid_t *)mp->b_cont->b_rptr; 8646 mp->b_cont->b_wptr += iocp->ioc_count; 8647 bzero(ipid, sizeof (*ipid)); 8648 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8649 ipid->ipid_ire_type = IRE_CACHE; 8650 ipid->ipid_addr_offset = sizeof (ipid_t); 8651 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8652 /* 8653 * Note: in the case of source routing we want to blow away the 8654 * route to the first source route hop. 8655 */ 8656 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8657 sizeof (tcp->tcp_ipha->ipha_dst)); 8658 8659 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8660 } 8661 8662 /* Unlink and return any mblk that looks like it contains an ire */ 8663 static mblk_t * 8664 tcp_ire_mp(mblk_t **mpp) 8665 { 8666 mblk_t *mp = *mpp; 8667 mblk_t *prev_mp = NULL; 8668 8669 for (;;) { 8670 switch (DB_TYPE(mp)) { 8671 case IRE_DB_TYPE: 8672 case IRE_DB_REQ_TYPE: 8673 if (mp == *mpp) { 8674 *mpp = mp->b_cont; 8675 } else { 8676 prev_mp->b_cont = mp->b_cont; 8677 } 8678 mp->b_cont = NULL; 8679 return (mp); 8680 default: 8681 break; 8682 } 8683 prev_mp = mp; 8684 mp = mp->b_cont; 8685 if (mp == NULL) 8686 break; 8687 } 8688 return (mp); 8689 } 8690 8691 /* 8692 * Timer callback routine for keepalive probe. We do a fake resend of 8693 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8694 * check to see if we have heard anything from the other end for the last 8695 * RTO period. If we have, set the timer to expire for another 8696 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8697 * RTO << 1 and check again when it expires. Keep exponentially increasing 8698 * the timeout if we have not heard from the other side. If for more than 8699 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8700 * kill the connection unless the keepalive abort threshold is 0. In 8701 * that case, we will probe "forever." 8702 */ 8703 static void 8704 tcp_keepalive_killer(void *arg) 8705 { 8706 mblk_t *mp; 8707 conn_t *connp = (conn_t *)arg; 8708 tcp_t *tcp = connp->conn_tcp; 8709 int32_t firetime; 8710 int32_t idletime; 8711 int32_t ka_intrvl; 8712 tcp_stack_t *tcps = tcp->tcp_tcps; 8713 8714 tcp->tcp_ka_tid = 0; 8715 8716 if (tcp->tcp_fused) 8717 return; 8718 8719 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepalive); 8720 ka_intrvl = tcp->tcp_ka_interval; 8721 8722 /* 8723 * Keepalive probe should only be sent if the application has not 8724 * done a close on the connection. 8725 */ 8726 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8727 return; 8728 } 8729 /* Timer fired too early, restart it. */ 8730 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8731 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8732 MSEC_TO_TICK(ka_intrvl)); 8733 return; 8734 } 8735 8736 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8737 /* 8738 * If we have not heard from the other side for a long 8739 * time, kill the connection unless the keepalive abort 8740 * threshold is 0. In that case, we will probe "forever." 8741 */ 8742 if (tcp->tcp_ka_abort_thres != 0 && 8743 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8744 BUMP_MIB(&tcps->tcps_mib, tcpTimKeepaliveDrop); 8745 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8746 tcp->tcp_client_errno : ETIMEDOUT, 11); 8747 return; 8748 } 8749 8750 if (tcp->tcp_snxt == tcp->tcp_suna && 8751 idletime >= ka_intrvl) { 8752 /* Fake resend of last ACKed byte. */ 8753 mblk_t *mp1 = allocb(1, BPRI_LO); 8754 8755 if (mp1 != NULL) { 8756 *mp1->b_wptr++ = '\0'; 8757 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8758 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8759 freeb(mp1); 8760 /* 8761 * if allocation failed, fall through to start the 8762 * timer back. 8763 */ 8764 if (mp != NULL) { 8765 tcp_send_data(tcp, tcp->tcp_wq, mp); 8766 BUMP_MIB(&tcps->tcps_mib, 8767 tcpTimKeepaliveProbe); 8768 if (tcp->tcp_ka_last_intrvl != 0) { 8769 int max; 8770 /* 8771 * We should probe again at least 8772 * in ka_intrvl, but not more than 8773 * tcp_rexmit_interval_max. 8774 */ 8775 max = tcps->tcps_rexmit_interval_max; 8776 firetime = MIN(ka_intrvl - 1, 8777 tcp->tcp_ka_last_intrvl << 1); 8778 if (firetime > max) 8779 firetime = max; 8780 } else { 8781 firetime = tcp->tcp_rto; 8782 } 8783 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8784 tcp_keepalive_killer, 8785 MSEC_TO_TICK(firetime)); 8786 tcp->tcp_ka_last_intrvl = firetime; 8787 return; 8788 } 8789 } 8790 } else { 8791 tcp->tcp_ka_last_intrvl = 0; 8792 } 8793 8794 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8795 if ((firetime = ka_intrvl - idletime) < 0) { 8796 firetime = ka_intrvl; 8797 } 8798 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8799 MSEC_TO_TICK(firetime)); 8800 } 8801 8802 int 8803 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8804 { 8805 queue_t *q = tcp->tcp_rq; 8806 int32_t mss = tcp->tcp_mss; 8807 int maxpsz; 8808 conn_t *connp = tcp->tcp_connp; 8809 8810 if (TCP_IS_DETACHED(tcp)) 8811 return (mss); 8812 if (tcp->tcp_fused) { 8813 maxpsz = tcp_fuse_maxpsz_set(tcp); 8814 mss = INFPSZ; 8815 } else if (tcp->tcp_mdt || tcp->tcp_lso || tcp->tcp_maxpsz == 0) { 8816 /* 8817 * Set the sd_qn_maxpsz according to the socket send buffer 8818 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8819 * instruct the stream head to copyin user data into contiguous 8820 * kernel-allocated buffers without breaking it up into smaller 8821 * chunks. We round up the buffer size to the nearest SMSS. 8822 */ 8823 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8824 if (tcp->tcp_kssl_ctx == NULL) 8825 mss = INFPSZ; 8826 else 8827 mss = SSL3_MAX_RECORD_LEN; 8828 } else { 8829 /* 8830 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8831 * (and a multiple of the mss). This instructs the stream 8832 * head to break down larger than SMSS writes into SMSS- 8833 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8834 */ 8835 /* XXX tune this with ndd tcp_maxpsz_multiplier */ 8836 maxpsz = tcp->tcp_maxpsz * mss; 8837 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8838 maxpsz = tcp->tcp_xmit_hiwater/2; 8839 /* Round up to nearest mss */ 8840 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8841 } 8842 } 8843 8844 (void) proto_set_maxpsz(q, connp, maxpsz); 8845 if (!(IPCL_IS_NONSTR(connp))) { 8846 /* XXX do it in set_maxpsz()? */ 8847 tcp->tcp_wq->q_maxpsz = maxpsz; 8848 } 8849 8850 if (set_maxblk) 8851 (void) proto_set_tx_maxblk(q, connp, mss); 8852 return (mss); 8853 } 8854 8855 /* 8856 * Extract option values from a tcp header. We put any found values into the 8857 * tcpopt struct and return a bitmask saying which options were found. 8858 */ 8859 static int 8860 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8861 { 8862 uchar_t *endp; 8863 int len; 8864 uint32_t mss; 8865 uchar_t *up = (uchar_t *)tcph; 8866 int found = 0; 8867 int32_t sack_len; 8868 tcp_seq sack_begin, sack_end; 8869 tcp_t *tcp; 8870 8871 endp = up + TCP_HDR_LENGTH(tcph); 8872 up += TCP_MIN_HEADER_LENGTH; 8873 while (up < endp) { 8874 len = endp - up; 8875 switch (*up) { 8876 case TCPOPT_EOL: 8877 break; 8878 8879 case TCPOPT_NOP: 8880 up++; 8881 continue; 8882 8883 case TCPOPT_MAXSEG: 8884 if (len < TCPOPT_MAXSEG_LEN || 8885 up[1] != TCPOPT_MAXSEG_LEN) 8886 break; 8887 8888 mss = BE16_TO_U16(up+2); 8889 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8890 tcpopt->tcp_opt_mss = mss; 8891 found |= TCP_OPT_MSS_PRESENT; 8892 8893 up += TCPOPT_MAXSEG_LEN; 8894 continue; 8895 8896 case TCPOPT_WSCALE: 8897 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8898 break; 8899 8900 if (up[2] > TCP_MAX_WINSHIFT) 8901 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8902 else 8903 tcpopt->tcp_opt_wscale = up[2]; 8904 found |= TCP_OPT_WSCALE_PRESENT; 8905 8906 up += TCPOPT_WS_LEN; 8907 continue; 8908 8909 case TCPOPT_SACK_PERMITTED: 8910 if (len < TCPOPT_SACK_OK_LEN || 8911 up[1] != TCPOPT_SACK_OK_LEN) 8912 break; 8913 found |= TCP_OPT_SACK_OK_PRESENT; 8914 up += TCPOPT_SACK_OK_LEN; 8915 continue; 8916 8917 case TCPOPT_SACK: 8918 if (len <= 2 || up[1] <= 2 || len < up[1]) 8919 break; 8920 8921 /* If TCP is not interested in SACK blks... */ 8922 if ((tcp = tcpopt->tcp) == NULL) { 8923 up += up[1]; 8924 continue; 8925 } 8926 sack_len = up[1] - TCPOPT_HEADER_LEN; 8927 up += TCPOPT_HEADER_LEN; 8928 8929 /* 8930 * If the list is empty, allocate one and assume 8931 * nothing is sack'ed. 8932 */ 8933 ASSERT(tcp->tcp_sack_info != NULL); 8934 if (tcp->tcp_notsack_list == NULL) { 8935 tcp_notsack_update(&(tcp->tcp_notsack_list), 8936 tcp->tcp_suna, tcp->tcp_snxt, 8937 &(tcp->tcp_num_notsack_blk), 8938 &(tcp->tcp_cnt_notsack_list)); 8939 8940 /* 8941 * Make sure tcp_notsack_list is not NULL. 8942 * This happens when kmem_alloc(KM_NOSLEEP) 8943 * returns NULL. 8944 */ 8945 if (tcp->tcp_notsack_list == NULL) { 8946 up += sack_len; 8947 continue; 8948 } 8949 tcp->tcp_fack = tcp->tcp_suna; 8950 } 8951 8952 while (sack_len > 0) { 8953 if (up + 8 > endp) { 8954 up = endp; 8955 break; 8956 } 8957 sack_begin = BE32_TO_U32(up); 8958 up += 4; 8959 sack_end = BE32_TO_U32(up); 8960 up += 4; 8961 sack_len -= 8; 8962 /* 8963 * Bounds checking. Make sure the SACK 8964 * info is within tcp_suna and tcp_snxt. 8965 * If this SACK blk is out of bound, ignore 8966 * it but continue to parse the following 8967 * blks. 8968 */ 8969 if (SEQ_LEQ(sack_end, sack_begin) || 8970 SEQ_LT(sack_begin, tcp->tcp_suna) || 8971 SEQ_GT(sack_end, tcp->tcp_snxt)) { 8972 continue; 8973 } 8974 tcp_notsack_insert(&(tcp->tcp_notsack_list), 8975 sack_begin, sack_end, 8976 &(tcp->tcp_num_notsack_blk), 8977 &(tcp->tcp_cnt_notsack_list)); 8978 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 8979 tcp->tcp_fack = sack_end; 8980 } 8981 } 8982 found |= TCP_OPT_SACK_PRESENT; 8983 continue; 8984 8985 case TCPOPT_TSTAMP: 8986 if (len < TCPOPT_TSTAMP_LEN || 8987 up[1] != TCPOPT_TSTAMP_LEN) 8988 break; 8989 8990 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 8991 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 8992 8993 found |= TCP_OPT_TSTAMP_PRESENT; 8994 8995 up += TCPOPT_TSTAMP_LEN; 8996 continue; 8997 8998 default: 8999 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9000 break; 9001 up += up[1]; 9002 continue; 9003 } 9004 break; 9005 } 9006 return (found); 9007 } 9008 9009 /* 9010 * Set the mss associated with a particular tcp based on its current value, 9011 * and a new one passed in. Observe minimums and maximums, and reset 9012 * other state variables that we want to view as multiples of mss. 9013 * 9014 * This function is called mainly because values like tcp_mss, tcp_cwnd, 9015 * highwater marks etc. need to be initialized or adjusted. 9016 * 1) From tcp_process_options() when the other side's SYN/SYN-ACK 9017 * packet arrives. 9018 * 2) We need to set a new MSS when ICMP_FRAGMENTATION_NEEDED or 9019 * ICMP6_PACKET_TOO_BIG arrives. 9020 * 3) From tcp_paws_check() if the other side stops sending the timestamp, 9021 * to increase the MSS to use the extra bytes available. 9022 * 9023 * Callers except tcp_paws_check() ensure that they only reduce mss. 9024 */ 9025 static void 9026 tcp_mss_set(tcp_t *tcp, uint32_t mss, boolean_t do_ss) 9027 { 9028 uint32_t mss_max; 9029 tcp_stack_t *tcps = tcp->tcp_tcps; 9030 9031 if (tcp->tcp_ipversion == IPV4_VERSION) 9032 mss_max = tcps->tcps_mss_max_ipv4; 9033 else 9034 mss_max = tcps->tcps_mss_max_ipv6; 9035 9036 if (mss < tcps->tcps_mss_min) 9037 mss = tcps->tcps_mss_min; 9038 if (mss > mss_max) 9039 mss = mss_max; 9040 /* 9041 * Unless naglim has been set by our client to 9042 * a non-mss value, force naglim to track mss. 9043 * This can help to aggregate small writes. 9044 */ 9045 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9046 tcp->tcp_naglim = mss; 9047 /* 9048 * TCP should be able to buffer at least 4 MSS data for obvious 9049 * performance reason. 9050 */ 9051 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9052 tcp->tcp_xmit_hiwater = mss << 2; 9053 9054 if (do_ss) { 9055 /* 9056 * Either the tcp_cwnd is as yet uninitialized, or mss is 9057 * changing due to a reduction in MTU, presumably as a 9058 * result of a new path component, reset cwnd to its 9059 * "initial" value, as a multiple of the new mss. 9060 */ 9061 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9062 } else { 9063 /* 9064 * Called by tcp_paws_check(), the mss increased 9065 * marginally to allow use of space previously taken 9066 * by the timestamp option. It would be inappropriate 9067 * to apply slow start or tcp_init_cwnd values to 9068 * tcp_cwnd, simply adjust to a multiple of the new mss. 9069 */ 9070 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9071 tcp->tcp_cwnd_cnt = 0; 9072 } 9073 tcp->tcp_mss = mss; 9074 (void) tcp_maxpsz_set(tcp, B_TRUE); 9075 } 9076 9077 /* For /dev/tcp aka AF_INET open */ 9078 static int 9079 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9080 { 9081 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9082 } 9083 9084 /* For /dev/tcp6 aka AF_INET6 open */ 9085 static int 9086 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9087 { 9088 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9089 } 9090 9091 static conn_t * 9092 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9093 boolean_t issocket, int *errorp) 9094 { 9095 tcp_t *tcp = NULL; 9096 conn_t *connp; 9097 int err; 9098 zoneid_t zoneid; 9099 tcp_stack_t *tcps; 9100 squeue_t *sqp; 9101 9102 ASSERT(errorp != NULL); 9103 /* 9104 * Find the proper zoneid and netstack. 9105 */ 9106 /* 9107 * Special case for install: miniroot needs to be able to 9108 * access files via NFS as though it were always in the 9109 * global zone. 9110 */ 9111 if (credp == kcred && nfs_global_client_only != 0) { 9112 zoneid = GLOBAL_ZONEID; 9113 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9114 netstack_tcp; 9115 ASSERT(tcps != NULL); 9116 } else { 9117 netstack_t *ns; 9118 9119 ns = netstack_find_by_cred(credp); 9120 ASSERT(ns != NULL); 9121 tcps = ns->netstack_tcp; 9122 ASSERT(tcps != NULL); 9123 9124 /* 9125 * For exclusive stacks we set the zoneid to zero 9126 * to make TCP operate as if in the global zone. 9127 */ 9128 if (tcps->tcps_netstack->netstack_stackid != 9129 GLOBAL_NETSTACKID) 9130 zoneid = GLOBAL_ZONEID; 9131 else 9132 zoneid = crgetzoneid(credp); 9133 } 9134 /* 9135 * For stackid zero this is done from strplumb.c, but 9136 * non-zero stackids are handled here. 9137 */ 9138 if (tcps->tcps_g_q == NULL && 9139 tcps->tcps_netstack->netstack_stackid != 9140 GLOBAL_NETSTACKID) { 9141 tcp_g_q_setup(tcps); 9142 } 9143 9144 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9145 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9146 /* 9147 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9148 * so we drop it by one. 9149 */ 9150 netstack_rele(tcps->tcps_netstack); 9151 if (connp == NULL) { 9152 *errorp = ENOSR; 9153 return (NULL); 9154 } 9155 connp->conn_sqp = sqp; 9156 connp->conn_initial_sqp = connp->conn_sqp; 9157 tcp = connp->conn_tcp; 9158 9159 if (isv6) { 9160 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9161 connp->conn_send = ip_output_v6; 9162 connp->conn_af_isv6 = B_TRUE; 9163 connp->conn_pkt_isv6 = B_TRUE; 9164 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9165 tcp->tcp_ipversion = IPV6_VERSION; 9166 tcp->tcp_family = AF_INET6; 9167 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9168 } else { 9169 connp->conn_flags |= IPCL_TCP4; 9170 connp->conn_send = ip_output; 9171 connp->conn_af_isv6 = B_FALSE; 9172 connp->conn_pkt_isv6 = B_FALSE; 9173 tcp->tcp_ipversion = IPV4_VERSION; 9174 tcp->tcp_family = AF_INET; 9175 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9176 } 9177 9178 /* 9179 * TCP keeps a copy of cred for cache locality reasons but 9180 * we put a reference only once. If connp->conn_cred 9181 * becomes invalid, tcp_cred should also be set to NULL. 9182 */ 9183 tcp->tcp_cred = connp->conn_cred = credp; 9184 crhold(connp->conn_cred); 9185 tcp->tcp_cpid = curproc->p_pid; 9186 tcp->tcp_open_time = lbolt64; 9187 connp->conn_zoneid = zoneid; 9188 connp->conn_mlp_type = mlptSingle; 9189 connp->conn_ulp_labeled = !is_system_labeled(); 9190 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9191 ASSERT(tcp->tcp_tcps == tcps); 9192 9193 /* 9194 * If the caller has the process-wide flag set, then default to MAC 9195 * exempt mode. This allows read-down to unlabeled hosts. 9196 */ 9197 if (getpflags(NET_MAC_AWARE, credp) != 0) 9198 connp->conn_mac_exempt = B_TRUE; 9199 9200 connp->conn_dev = NULL; 9201 if (issocket) { 9202 connp->conn_flags |= IPCL_SOCKET; 9203 tcp->tcp_issocket = 1; 9204 } 9205 9206 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9207 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9208 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9209 9210 /* Non-zero default values */ 9211 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9212 9213 if (q == NULL) { 9214 /* 9215 * Create a helper stream for non-STREAMS socket. 9216 */ 9217 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9218 if (err != 0) { 9219 ip1dbg(("tcp_create_common: create of IP helper stream " 9220 "failed\n")); 9221 CONN_DEC_REF(connp); 9222 *errorp = err; 9223 return (NULL); 9224 } 9225 q = connp->conn_rq; 9226 } else { 9227 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9228 } 9229 9230 SOCK_CONNID_INIT(tcp->tcp_connid); 9231 err = tcp_init(tcp, q); 9232 if (err != 0) { 9233 CONN_DEC_REF(connp); 9234 *errorp = err; 9235 return (NULL); 9236 } 9237 9238 return (connp); 9239 } 9240 9241 static int 9242 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9243 boolean_t isv6) 9244 { 9245 tcp_t *tcp = NULL; 9246 conn_t *connp = NULL; 9247 int err; 9248 vmem_t *minor_arena = NULL; 9249 dev_t conn_dev; 9250 boolean_t issocket; 9251 9252 if (q->q_ptr != NULL) 9253 return (0); 9254 9255 if (sflag == MODOPEN) 9256 return (EINVAL); 9257 9258 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9259 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9260 minor_arena = ip_minor_arena_la; 9261 } else { 9262 /* 9263 * Either minor numbers in the large arena were exhausted 9264 * or a non socket application is doing the open. 9265 * Try to allocate from the small arena. 9266 */ 9267 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9268 return (EBUSY); 9269 } 9270 minor_arena = ip_minor_arena_sa; 9271 } 9272 9273 ASSERT(minor_arena != NULL); 9274 9275 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9276 9277 if (flag & SO_FALLBACK) { 9278 /* 9279 * Non streams socket needs a stream to fallback to 9280 */ 9281 RD(q)->q_ptr = (void *)conn_dev; 9282 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9283 WR(q)->q_ptr = (void *)minor_arena; 9284 qprocson(q); 9285 return (0); 9286 } else if (flag & SO_ACCEPTOR) { 9287 q->q_qinfo = &tcp_acceptor_rinit; 9288 /* 9289 * the conn_dev and minor_arena will be subsequently used by 9290 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9291 * the minor device number for this connection from the q_ptr. 9292 */ 9293 RD(q)->q_ptr = (void *)conn_dev; 9294 WR(q)->q_qinfo = &tcp_acceptor_winit; 9295 WR(q)->q_ptr = (void *)minor_arena; 9296 qprocson(q); 9297 return (0); 9298 } 9299 9300 issocket = flag & SO_SOCKSTR; 9301 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9302 9303 if (connp == NULL) { 9304 inet_minor_free(minor_arena, conn_dev); 9305 q->q_ptr = WR(q)->q_ptr = NULL; 9306 return (err); 9307 } 9308 9309 q->q_ptr = WR(q)->q_ptr = connp; 9310 9311 connp->conn_dev = conn_dev; 9312 connp->conn_minor_arena = minor_arena; 9313 9314 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9315 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9316 9317 tcp = connp->conn_tcp; 9318 9319 if (issocket) { 9320 WR(q)->q_qinfo = &tcp_sock_winit; 9321 } else { 9322 #ifdef _ILP32 9323 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9324 #else 9325 tcp->tcp_acceptor_id = conn_dev; 9326 #endif /* _ILP32 */ 9327 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9328 } 9329 9330 /* 9331 * Put the ref for TCP. Ref for IP was already put 9332 * by ipcl_conn_create. Also Make the conn_t globally 9333 * visible to walkers 9334 */ 9335 mutex_enter(&connp->conn_lock); 9336 CONN_INC_REF_LOCKED(connp); 9337 ASSERT(connp->conn_ref == 2); 9338 connp->conn_state_flags &= ~CONN_INCIPIENT; 9339 mutex_exit(&connp->conn_lock); 9340 9341 qprocson(q); 9342 return (0); 9343 } 9344 9345 /* 9346 * Some TCP options can be "set" by requesting them in the option 9347 * buffer. This is needed for XTI feature test though we do not 9348 * allow it in general. We interpret that this mechanism is more 9349 * applicable to OSI protocols and need not be allowed in general. 9350 * This routine filters out options for which it is not allowed (most) 9351 * and lets through those (few) for which it is. [ The XTI interface 9352 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9353 * ever implemented will have to be allowed here ]. 9354 */ 9355 static boolean_t 9356 tcp_allow_connopt_set(int level, int name) 9357 { 9358 9359 switch (level) { 9360 case IPPROTO_TCP: 9361 switch (name) { 9362 case TCP_NODELAY: 9363 return (B_TRUE); 9364 default: 9365 return (B_FALSE); 9366 } 9367 /*NOTREACHED*/ 9368 default: 9369 return (B_FALSE); 9370 } 9371 /*NOTREACHED*/ 9372 } 9373 9374 /* 9375 * this routine gets default values of certain options whose default 9376 * values are maintained by protocol specific code 9377 */ 9378 /* ARGSUSED */ 9379 int 9380 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9381 { 9382 int32_t *i1 = (int32_t *)ptr; 9383 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9384 9385 switch (level) { 9386 case IPPROTO_TCP: 9387 switch (name) { 9388 case TCP_NOTIFY_THRESHOLD: 9389 *i1 = tcps->tcps_ip_notify_interval; 9390 break; 9391 case TCP_ABORT_THRESHOLD: 9392 *i1 = tcps->tcps_ip_abort_interval; 9393 break; 9394 case TCP_CONN_NOTIFY_THRESHOLD: 9395 *i1 = tcps->tcps_ip_notify_cinterval; 9396 break; 9397 case TCP_CONN_ABORT_THRESHOLD: 9398 *i1 = tcps->tcps_ip_abort_cinterval; 9399 break; 9400 default: 9401 return (-1); 9402 } 9403 break; 9404 case IPPROTO_IP: 9405 switch (name) { 9406 case IP_TTL: 9407 *i1 = tcps->tcps_ipv4_ttl; 9408 break; 9409 default: 9410 return (-1); 9411 } 9412 break; 9413 case IPPROTO_IPV6: 9414 switch (name) { 9415 case IPV6_UNICAST_HOPS: 9416 *i1 = tcps->tcps_ipv6_hoplimit; 9417 break; 9418 default: 9419 return (-1); 9420 } 9421 break; 9422 default: 9423 return (-1); 9424 } 9425 return (sizeof (int)); 9426 } 9427 9428 static int 9429 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9430 { 9431 int *i1 = (int *)ptr; 9432 tcp_t *tcp = connp->conn_tcp; 9433 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9434 9435 switch (level) { 9436 case SOL_SOCKET: 9437 switch (name) { 9438 case SO_LINGER: { 9439 struct linger *lgr = (struct linger *)ptr; 9440 9441 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9442 lgr->l_linger = tcp->tcp_lingertime; 9443 } 9444 return (sizeof (struct linger)); 9445 case SO_DEBUG: 9446 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9447 break; 9448 case SO_KEEPALIVE: 9449 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9450 break; 9451 case SO_DONTROUTE: 9452 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9453 break; 9454 case SO_USELOOPBACK: 9455 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9456 break; 9457 case SO_BROADCAST: 9458 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9459 break; 9460 case SO_REUSEADDR: 9461 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9462 break; 9463 case SO_OOBINLINE: 9464 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9465 break; 9466 case SO_DGRAM_ERRIND: 9467 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9468 break; 9469 case SO_TYPE: 9470 *i1 = SOCK_STREAM; 9471 break; 9472 case SO_SNDBUF: 9473 *i1 = tcp->tcp_xmit_hiwater; 9474 break; 9475 case SO_RCVBUF: 9476 *i1 = tcp->tcp_recv_hiwater; 9477 break; 9478 case SO_SND_COPYAVOID: 9479 *i1 = tcp->tcp_snd_zcopy_on ? 9480 SO_SND_COPYAVOID : 0; 9481 break; 9482 case SO_ALLZONES: 9483 *i1 = connp->conn_allzones ? 1 : 0; 9484 break; 9485 case SO_ANON_MLP: 9486 *i1 = connp->conn_anon_mlp; 9487 break; 9488 case SO_MAC_EXEMPT: 9489 *i1 = connp->conn_mac_exempt; 9490 break; 9491 case SO_EXCLBIND: 9492 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9493 break; 9494 case SO_PROTOTYPE: 9495 *i1 = IPPROTO_TCP; 9496 break; 9497 case SO_DOMAIN: 9498 *i1 = tcp->tcp_family; 9499 break; 9500 case SO_ACCEPTCONN: 9501 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9502 default: 9503 return (-1); 9504 } 9505 break; 9506 case IPPROTO_TCP: 9507 switch (name) { 9508 case TCP_NODELAY: 9509 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9510 break; 9511 case TCP_MAXSEG: 9512 *i1 = tcp->tcp_mss; 9513 break; 9514 case TCP_NOTIFY_THRESHOLD: 9515 *i1 = (int)tcp->tcp_first_timer_threshold; 9516 break; 9517 case TCP_ABORT_THRESHOLD: 9518 *i1 = tcp->tcp_second_timer_threshold; 9519 break; 9520 case TCP_CONN_NOTIFY_THRESHOLD: 9521 *i1 = tcp->tcp_first_ctimer_threshold; 9522 break; 9523 case TCP_CONN_ABORT_THRESHOLD: 9524 *i1 = tcp->tcp_second_ctimer_threshold; 9525 break; 9526 case TCP_RECVDSTADDR: 9527 *i1 = tcp->tcp_recvdstaddr; 9528 break; 9529 case TCP_ANONPRIVBIND: 9530 *i1 = tcp->tcp_anon_priv_bind; 9531 break; 9532 case TCP_EXCLBIND: 9533 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9534 break; 9535 case TCP_INIT_CWND: 9536 *i1 = tcp->tcp_init_cwnd; 9537 break; 9538 case TCP_KEEPALIVE_THRESHOLD: 9539 *i1 = tcp->tcp_ka_interval; 9540 break; 9541 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9542 *i1 = tcp->tcp_ka_abort_thres; 9543 break; 9544 case TCP_CORK: 9545 *i1 = tcp->tcp_cork; 9546 break; 9547 default: 9548 return (-1); 9549 } 9550 break; 9551 case IPPROTO_IP: 9552 if (tcp->tcp_family != AF_INET) 9553 return (-1); 9554 switch (name) { 9555 case IP_OPTIONS: 9556 case T_IP_OPTIONS: { 9557 /* 9558 * This is compatible with BSD in that in only return 9559 * the reverse source route with the final destination 9560 * as the last entry. The first 4 bytes of the option 9561 * will contain the final destination. 9562 */ 9563 int opt_len; 9564 9565 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9566 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9567 ASSERT(opt_len >= 0); 9568 /* Caller ensures enough space */ 9569 if (opt_len > 0) { 9570 /* 9571 * TODO: Do we have to handle getsockopt on an 9572 * initiator as well? 9573 */ 9574 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9575 } 9576 return (0); 9577 } 9578 case IP_TOS: 9579 case T_IP_TOS: 9580 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9581 break; 9582 case IP_TTL: 9583 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9584 break; 9585 case IP_NEXTHOP: 9586 /* Handled at IP level */ 9587 return (-EINVAL); 9588 default: 9589 return (-1); 9590 } 9591 break; 9592 case IPPROTO_IPV6: 9593 /* 9594 * IPPROTO_IPV6 options are only supported for sockets 9595 * that are using IPv6 on the wire. 9596 */ 9597 if (tcp->tcp_ipversion != IPV6_VERSION) { 9598 return (-1); 9599 } 9600 switch (name) { 9601 case IPV6_UNICAST_HOPS: 9602 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9603 break; /* goto sizeof (int) option return */ 9604 case IPV6_BOUND_IF: 9605 /* Zero if not set */ 9606 *i1 = tcp->tcp_bound_if; 9607 break; /* goto sizeof (int) option return */ 9608 case IPV6_RECVPKTINFO: 9609 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9610 *i1 = 1; 9611 else 9612 *i1 = 0; 9613 break; /* goto sizeof (int) option return */ 9614 case IPV6_RECVTCLASS: 9615 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9616 *i1 = 1; 9617 else 9618 *i1 = 0; 9619 break; /* goto sizeof (int) option return */ 9620 case IPV6_RECVHOPLIMIT: 9621 if (tcp->tcp_ipv6_recvancillary & 9622 TCP_IPV6_RECVHOPLIMIT) 9623 *i1 = 1; 9624 else 9625 *i1 = 0; 9626 break; /* goto sizeof (int) option return */ 9627 case IPV6_RECVHOPOPTS: 9628 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9629 *i1 = 1; 9630 else 9631 *i1 = 0; 9632 break; /* goto sizeof (int) option return */ 9633 case IPV6_RECVDSTOPTS: 9634 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9635 *i1 = 1; 9636 else 9637 *i1 = 0; 9638 break; /* goto sizeof (int) option return */ 9639 case _OLD_IPV6_RECVDSTOPTS: 9640 if (tcp->tcp_ipv6_recvancillary & 9641 TCP_OLD_IPV6_RECVDSTOPTS) 9642 *i1 = 1; 9643 else 9644 *i1 = 0; 9645 break; /* goto sizeof (int) option return */ 9646 case IPV6_RECVRTHDR: 9647 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9648 *i1 = 1; 9649 else 9650 *i1 = 0; 9651 break; /* goto sizeof (int) option return */ 9652 case IPV6_RECVRTHDRDSTOPTS: 9653 if (tcp->tcp_ipv6_recvancillary & 9654 TCP_IPV6_RECVRTDSTOPTS) 9655 *i1 = 1; 9656 else 9657 *i1 = 0; 9658 break; /* goto sizeof (int) option return */ 9659 case IPV6_PKTINFO: { 9660 /* XXX assumes that caller has room for max size! */ 9661 struct in6_pktinfo *pkti; 9662 9663 pkti = (struct in6_pktinfo *)ptr; 9664 if (ipp->ipp_fields & IPPF_IFINDEX) 9665 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9666 else 9667 pkti->ipi6_ifindex = 0; 9668 if (ipp->ipp_fields & IPPF_ADDR) 9669 pkti->ipi6_addr = ipp->ipp_addr; 9670 else 9671 pkti->ipi6_addr = ipv6_all_zeros; 9672 return (sizeof (struct in6_pktinfo)); 9673 } 9674 case IPV6_TCLASS: 9675 if (ipp->ipp_fields & IPPF_TCLASS) 9676 *i1 = ipp->ipp_tclass; 9677 else 9678 *i1 = IPV6_FLOW_TCLASS( 9679 IPV6_DEFAULT_VERS_AND_FLOW); 9680 break; /* goto sizeof (int) option return */ 9681 case IPV6_NEXTHOP: { 9682 sin6_t *sin6 = (sin6_t *)ptr; 9683 9684 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9685 return (0); 9686 *sin6 = sin6_null; 9687 sin6->sin6_family = AF_INET6; 9688 sin6->sin6_addr = ipp->ipp_nexthop; 9689 return (sizeof (sin6_t)); 9690 } 9691 case IPV6_HOPOPTS: 9692 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9693 return (0); 9694 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9695 return (0); 9696 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9697 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9698 if (tcp->tcp_label_len > 0) { 9699 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9700 ptr[1] = (ipp->ipp_hopoptslen - 9701 tcp->tcp_label_len + 7) / 8 - 1; 9702 } 9703 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9704 case IPV6_RTHDRDSTOPTS: 9705 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9706 return (0); 9707 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9708 return (ipp->ipp_rtdstoptslen); 9709 case IPV6_RTHDR: 9710 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9711 return (0); 9712 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9713 return (ipp->ipp_rthdrlen); 9714 case IPV6_DSTOPTS: 9715 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9716 return (0); 9717 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9718 return (ipp->ipp_dstoptslen); 9719 case IPV6_SRC_PREFERENCES: 9720 return (ip6_get_src_preferences(connp, 9721 (uint32_t *)ptr)); 9722 case IPV6_PATHMTU: { 9723 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9724 9725 if (tcp->tcp_state < TCPS_ESTABLISHED) 9726 return (-1); 9727 9728 return (ip_fill_mtuinfo(&connp->conn_remv6, 9729 connp->conn_fport, mtuinfo, 9730 connp->conn_netstack)); 9731 } 9732 default: 9733 return (-1); 9734 } 9735 break; 9736 default: 9737 return (-1); 9738 } 9739 return (sizeof (int)); 9740 } 9741 9742 /* 9743 * TCP routine to get the values of options. 9744 */ 9745 int 9746 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9747 { 9748 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9749 } 9750 9751 /* returns UNIX error, the optlen is a value-result arg */ 9752 int 9753 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9754 void *optvalp, socklen_t *optlen, cred_t *cr) 9755 { 9756 conn_t *connp = (conn_t *)proto_handle; 9757 squeue_t *sqp = connp->conn_sqp; 9758 int error; 9759 t_uscalar_t max_optbuf_len; 9760 void *optvalp_buf; 9761 int len; 9762 9763 ASSERT(connp->conn_upper_handle != NULL); 9764 9765 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9766 tcp_opt_obj.odb_opt_des_arr, 9767 tcp_opt_obj.odb_opt_arr_cnt, 9768 tcp_opt_obj.odb_topmost_tpiprovider, 9769 B_FALSE, B_TRUE, cr); 9770 if (error != 0) { 9771 if (error < 0) { 9772 error = proto_tlitosyserr(-error); 9773 } 9774 return (error); 9775 } 9776 9777 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9778 9779 error = squeue_synch_enter(sqp, connp, NULL); 9780 if (error == ENOMEM) { 9781 return (ENOMEM); 9782 } 9783 9784 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9785 squeue_synch_exit(sqp, connp); 9786 9787 if (len < 0) { 9788 /* 9789 * Pass on to IP 9790 */ 9791 kmem_free(optvalp_buf, max_optbuf_len); 9792 return (ip_get_options(connp, level, option_name, 9793 optvalp, optlen, cr)); 9794 } else { 9795 /* 9796 * update optlen and copy option value 9797 */ 9798 t_uscalar_t size = MIN(len, *optlen); 9799 bcopy(optvalp_buf, optvalp, size); 9800 bcopy(&size, optlen, sizeof (size)); 9801 9802 kmem_free(optvalp_buf, max_optbuf_len); 9803 return (0); 9804 } 9805 } 9806 9807 /* 9808 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9809 * Parameters are assumed to be verified by the caller. 9810 */ 9811 /* ARGSUSED */ 9812 int 9813 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9814 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9815 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9816 { 9817 tcp_t *tcp = connp->conn_tcp; 9818 int *i1 = (int *)invalp; 9819 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9820 boolean_t checkonly; 9821 int reterr; 9822 tcp_stack_t *tcps = tcp->tcp_tcps; 9823 9824 switch (optset_context) { 9825 case SETFN_OPTCOM_CHECKONLY: 9826 checkonly = B_TRUE; 9827 /* 9828 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9829 * inlen != 0 implies value supplied and 9830 * we have to "pretend" to set it. 9831 * inlen == 0 implies that there is no 9832 * value part in T_CHECK request and just validation 9833 * done elsewhere should be enough, we just return here. 9834 */ 9835 if (inlen == 0) { 9836 *outlenp = 0; 9837 return (0); 9838 } 9839 break; 9840 case SETFN_OPTCOM_NEGOTIATE: 9841 checkonly = B_FALSE; 9842 break; 9843 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9844 case SETFN_CONN_NEGOTIATE: 9845 checkonly = B_FALSE; 9846 /* 9847 * Negotiating local and "association-related" options 9848 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9849 * primitives is allowed by XTI, but we choose 9850 * to not implement this style negotiation for Internet 9851 * protocols (We interpret it is a must for OSI world but 9852 * optional for Internet protocols) for all options. 9853 * [ Will do only for the few options that enable test 9854 * suites that our XTI implementation of this feature 9855 * works for transports that do allow it ] 9856 */ 9857 if (!tcp_allow_connopt_set(level, name)) { 9858 *outlenp = 0; 9859 return (EINVAL); 9860 } 9861 break; 9862 default: 9863 /* 9864 * We should never get here 9865 */ 9866 *outlenp = 0; 9867 return (EINVAL); 9868 } 9869 9870 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9871 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9872 9873 /* 9874 * For TCP, we should have no ancillary data sent down 9875 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9876 * has to be zero. 9877 */ 9878 ASSERT(thisdg_attrs == NULL); 9879 9880 /* 9881 * For fixed length options, no sanity check 9882 * of passed in length is done. It is assumed *_optcom_req() 9883 * routines do the right thing. 9884 */ 9885 switch (level) { 9886 case SOL_SOCKET: 9887 switch (name) { 9888 case SO_LINGER: { 9889 struct linger *lgr = (struct linger *)invalp; 9890 9891 if (!checkonly) { 9892 if (lgr->l_onoff) { 9893 tcp->tcp_linger = 1; 9894 tcp->tcp_lingertime = lgr->l_linger; 9895 } else { 9896 tcp->tcp_linger = 0; 9897 tcp->tcp_lingertime = 0; 9898 } 9899 /* struct copy */ 9900 *(struct linger *)outvalp = *lgr; 9901 } else { 9902 if (!lgr->l_onoff) { 9903 ((struct linger *) 9904 outvalp)->l_onoff = 0; 9905 ((struct linger *) 9906 outvalp)->l_linger = 0; 9907 } else { 9908 /* struct copy */ 9909 *(struct linger *)outvalp = *lgr; 9910 } 9911 } 9912 *outlenp = sizeof (struct linger); 9913 return (0); 9914 } 9915 case SO_DEBUG: 9916 if (!checkonly) 9917 tcp->tcp_debug = onoff; 9918 break; 9919 case SO_KEEPALIVE: 9920 if (checkonly) { 9921 /* check only case */ 9922 break; 9923 } 9924 9925 if (!onoff) { 9926 if (tcp->tcp_ka_enabled) { 9927 if (tcp->tcp_ka_tid != 0) { 9928 (void) TCP_TIMER_CANCEL(tcp, 9929 tcp->tcp_ka_tid); 9930 tcp->tcp_ka_tid = 0; 9931 } 9932 tcp->tcp_ka_enabled = 0; 9933 } 9934 break; 9935 } 9936 if (!tcp->tcp_ka_enabled) { 9937 /* Crank up the keepalive timer */ 9938 tcp->tcp_ka_last_intrvl = 0; 9939 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9940 tcp_keepalive_killer, 9941 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9942 tcp->tcp_ka_enabled = 1; 9943 } 9944 break; 9945 case SO_DONTROUTE: 9946 /* 9947 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 9948 * only of interest to IP. We track them here only so 9949 * that we can report their current value. 9950 */ 9951 if (!checkonly) { 9952 tcp->tcp_dontroute = onoff; 9953 tcp->tcp_connp->conn_dontroute = onoff; 9954 } 9955 break; 9956 case SO_USELOOPBACK: 9957 if (!checkonly) { 9958 tcp->tcp_useloopback = onoff; 9959 tcp->tcp_connp->conn_loopback = onoff; 9960 } 9961 break; 9962 case SO_BROADCAST: 9963 if (!checkonly) { 9964 tcp->tcp_broadcast = onoff; 9965 tcp->tcp_connp->conn_broadcast = onoff; 9966 } 9967 break; 9968 case SO_REUSEADDR: 9969 if (!checkonly) { 9970 tcp->tcp_reuseaddr = onoff; 9971 tcp->tcp_connp->conn_reuseaddr = onoff; 9972 } 9973 break; 9974 case SO_OOBINLINE: 9975 if (!checkonly) { 9976 tcp->tcp_oobinline = onoff; 9977 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 9978 proto_set_rx_oob_opt(connp, onoff); 9979 } 9980 break; 9981 case SO_DGRAM_ERRIND: 9982 if (!checkonly) 9983 tcp->tcp_dgram_errind = onoff; 9984 break; 9985 case SO_SNDBUF: { 9986 if (*i1 > tcps->tcps_max_buf) { 9987 *outlenp = 0; 9988 return (ENOBUFS); 9989 } 9990 if (checkonly) 9991 break; 9992 9993 tcp->tcp_xmit_hiwater = *i1; 9994 if (tcps->tcps_snd_lowat_fraction != 0) 9995 tcp->tcp_xmit_lowater = 9996 tcp->tcp_xmit_hiwater / 9997 tcps->tcps_snd_lowat_fraction; 9998 (void) tcp_maxpsz_set(tcp, B_TRUE); 9999 /* 10000 * If we are flow-controlled, recheck the condition. 10001 * There are apps that increase SO_SNDBUF size when 10002 * flow-controlled (EWOULDBLOCK), and expect the flow 10003 * control condition to be lifted right away. 10004 */ 10005 mutex_enter(&tcp->tcp_non_sq_lock); 10006 if (tcp->tcp_flow_stopped && 10007 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10008 tcp_clrqfull(tcp); 10009 } 10010 mutex_exit(&tcp->tcp_non_sq_lock); 10011 break; 10012 } 10013 case SO_RCVBUF: 10014 if (*i1 > tcps->tcps_max_buf) { 10015 *outlenp = 0; 10016 return (ENOBUFS); 10017 } 10018 /* Silently ignore zero */ 10019 if (!checkonly && *i1 != 0) { 10020 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10021 (void) tcp_rwnd_set(tcp, *i1); 10022 } 10023 /* 10024 * XXX should we return the rwnd here 10025 * and tcp_opt_get ? 10026 */ 10027 break; 10028 case SO_SND_COPYAVOID: 10029 if (!checkonly) { 10030 /* we only allow enable at most once for now */ 10031 if (tcp->tcp_loopback || 10032 (tcp->tcp_kssl_ctx != NULL) || 10033 (!tcp->tcp_snd_zcopy_aware && 10034 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10035 *outlenp = 0; 10036 return (EOPNOTSUPP); 10037 } 10038 tcp->tcp_snd_zcopy_aware = 1; 10039 } 10040 break; 10041 case SO_RCVTIMEO: 10042 case SO_SNDTIMEO: 10043 /* 10044 * Pass these two options in order for third part 10045 * protocol usage. Here just return directly. 10046 */ 10047 return (0); 10048 case SO_ALLZONES: 10049 /* Pass option along to IP level for handling */ 10050 return (-EINVAL); 10051 case SO_ANON_MLP: 10052 /* Pass option along to IP level for handling */ 10053 return (-EINVAL); 10054 case SO_MAC_EXEMPT: 10055 /* Pass option along to IP level for handling */ 10056 return (-EINVAL); 10057 case SO_EXCLBIND: 10058 if (!checkonly) 10059 tcp->tcp_exclbind = onoff; 10060 break; 10061 default: 10062 *outlenp = 0; 10063 return (EINVAL); 10064 } 10065 break; 10066 case IPPROTO_TCP: 10067 switch (name) { 10068 case TCP_NODELAY: 10069 if (!checkonly) 10070 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10071 break; 10072 case TCP_NOTIFY_THRESHOLD: 10073 if (!checkonly) 10074 tcp->tcp_first_timer_threshold = *i1; 10075 break; 10076 case TCP_ABORT_THRESHOLD: 10077 if (!checkonly) 10078 tcp->tcp_second_timer_threshold = *i1; 10079 break; 10080 case TCP_CONN_NOTIFY_THRESHOLD: 10081 if (!checkonly) 10082 tcp->tcp_first_ctimer_threshold = *i1; 10083 break; 10084 case TCP_CONN_ABORT_THRESHOLD: 10085 if (!checkonly) 10086 tcp->tcp_second_ctimer_threshold = *i1; 10087 break; 10088 case TCP_RECVDSTADDR: 10089 if (tcp->tcp_state > TCPS_LISTEN) 10090 return (EOPNOTSUPP); 10091 if (!checkonly) 10092 tcp->tcp_recvdstaddr = onoff; 10093 break; 10094 case TCP_ANONPRIVBIND: 10095 if ((reterr = secpolicy_net_privaddr(cr, 0, 10096 IPPROTO_TCP)) != 0) { 10097 *outlenp = 0; 10098 return (reterr); 10099 } 10100 if (!checkonly) { 10101 tcp->tcp_anon_priv_bind = onoff; 10102 } 10103 break; 10104 case TCP_EXCLBIND: 10105 if (!checkonly) 10106 tcp->tcp_exclbind = onoff; 10107 break; /* goto sizeof (int) option return */ 10108 case TCP_INIT_CWND: { 10109 uint32_t init_cwnd = *((uint32_t *)invalp); 10110 10111 if (checkonly) 10112 break; 10113 10114 /* 10115 * Only allow socket with network configuration 10116 * privilege to set the initial cwnd to be larger 10117 * than allowed by RFC 3390. 10118 */ 10119 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10120 tcp->tcp_init_cwnd = init_cwnd; 10121 break; 10122 } 10123 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10124 *outlenp = 0; 10125 return (reterr); 10126 } 10127 if (init_cwnd > TCP_MAX_INIT_CWND) { 10128 *outlenp = 0; 10129 return (EINVAL); 10130 } 10131 tcp->tcp_init_cwnd = init_cwnd; 10132 break; 10133 } 10134 case TCP_KEEPALIVE_THRESHOLD: 10135 if (checkonly) 10136 break; 10137 10138 if (*i1 < tcps->tcps_keepalive_interval_low || 10139 *i1 > tcps->tcps_keepalive_interval_high) { 10140 *outlenp = 0; 10141 return (EINVAL); 10142 } 10143 if (*i1 != tcp->tcp_ka_interval) { 10144 tcp->tcp_ka_interval = *i1; 10145 /* 10146 * Check if we need to restart the 10147 * keepalive timer. 10148 */ 10149 if (tcp->tcp_ka_tid != 0) { 10150 ASSERT(tcp->tcp_ka_enabled); 10151 (void) TCP_TIMER_CANCEL(tcp, 10152 tcp->tcp_ka_tid); 10153 tcp->tcp_ka_last_intrvl = 0; 10154 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10155 tcp_keepalive_killer, 10156 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10157 } 10158 } 10159 break; 10160 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10161 if (!checkonly) { 10162 if (*i1 < 10163 tcps->tcps_keepalive_abort_interval_low || 10164 *i1 > 10165 tcps->tcps_keepalive_abort_interval_high) { 10166 *outlenp = 0; 10167 return (EINVAL); 10168 } 10169 tcp->tcp_ka_abort_thres = *i1; 10170 } 10171 break; 10172 case TCP_CORK: 10173 if (!checkonly) { 10174 /* 10175 * if tcp->tcp_cork was set and is now 10176 * being unset, we have to make sure that 10177 * the remaining data gets sent out. Also 10178 * unset tcp->tcp_cork so that tcp_wput_data() 10179 * can send data even if it is less than mss 10180 */ 10181 if (tcp->tcp_cork && onoff == 0 && 10182 tcp->tcp_unsent > 0) { 10183 tcp->tcp_cork = B_FALSE; 10184 tcp_wput_data(tcp, NULL, B_FALSE); 10185 } 10186 tcp->tcp_cork = onoff; 10187 } 10188 break; 10189 default: 10190 *outlenp = 0; 10191 return (EINVAL); 10192 } 10193 break; 10194 case IPPROTO_IP: 10195 if (tcp->tcp_family != AF_INET) { 10196 *outlenp = 0; 10197 return (ENOPROTOOPT); 10198 } 10199 switch (name) { 10200 case IP_OPTIONS: 10201 case T_IP_OPTIONS: 10202 reterr = tcp_opt_set_header(tcp, checkonly, 10203 invalp, inlen); 10204 if (reterr) { 10205 *outlenp = 0; 10206 return (reterr); 10207 } 10208 /* OK return - copy input buffer into output buffer */ 10209 if (invalp != outvalp) { 10210 /* don't trust bcopy for identical src/dst */ 10211 bcopy(invalp, outvalp, inlen); 10212 } 10213 *outlenp = inlen; 10214 return (0); 10215 case IP_TOS: 10216 case T_IP_TOS: 10217 if (!checkonly) { 10218 tcp->tcp_ipha->ipha_type_of_service = 10219 (uchar_t)*i1; 10220 tcp->tcp_tos = (uchar_t)*i1; 10221 } 10222 break; 10223 case IP_TTL: 10224 if (!checkonly) { 10225 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10226 tcp->tcp_ttl = (uchar_t)*i1; 10227 } 10228 break; 10229 case IP_BOUND_IF: 10230 case IP_NEXTHOP: 10231 /* Handled at the IP level */ 10232 return (-EINVAL); 10233 case IP_SEC_OPT: 10234 /* 10235 * We should not allow policy setting after 10236 * we start listening for connections. 10237 */ 10238 if (tcp->tcp_state == TCPS_LISTEN) { 10239 return (EINVAL); 10240 } else { 10241 /* Handled at the IP level */ 10242 return (-EINVAL); 10243 } 10244 default: 10245 *outlenp = 0; 10246 return (EINVAL); 10247 } 10248 break; 10249 case IPPROTO_IPV6: { 10250 ip6_pkt_t *ipp; 10251 10252 /* 10253 * IPPROTO_IPV6 options are only supported for sockets 10254 * that are using IPv6 on the wire. 10255 */ 10256 if (tcp->tcp_ipversion != IPV6_VERSION) { 10257 *outlenp = 0; 10258 return (ENOPROTOOPT); 10259 } 10260 /* 10261 * Only sticky options; no ancillary data 10262 */ 10263 ipp = &tcp->tcp_sticky_ipp; 10264 10265 switch (name) { 10266 case IPV6_UNICAST_HOPS: 10267 /* -1 means use default */ 10268 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10269 *outlenp = 0; 10270 return (EINVAL); 10271 } 10272 if (!checkonly) { 10273 if (*i1 == -1) { 10274 tcp->tcp_ip6h->ip6_hops = 10275 ipp->ipp_unicast_hops = 10276 (uint8_t)tcps->tcps_ipv6_hoplimit; 10277 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10278 /* Pass modified value to IP. */ 10279 *i1 = tcp->tcp_ip6h->ip6_hops; 10280 } else { 10281 tcp->tcp_ip6h->ip6_hops = 10282 ipp->ipp_unicast_hops = 10283 (uint8_t)*i1; 10284 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10285 } 10286 reterr = tcp_build_hdrs(tcp); 10287 if (reterr != 0) 10288 return (reterr); 10289 } 10290 break; 10291 case IPV6_BOUND_IF: 10292 if (!checkonly) { 10293 tcp->tcp_bound_if = *i1; 10294 PASS_OPT_TO_IP(connp); 10295 } 10296 break; 10297 /* 10298 * Set boolean switches for ancillary data delivery 10299 */ 10300 case IPV6_RECVPKTINFO: 10301 if (!checkonly) { 10302 if (onoff) 10303 tcp->tcp_ipv6_recvancillary |= 10304 TCP_IPV6_RECVPKTINFO; 10305 else 10306 tcp->tcp_ipv6_recvancillary &= 10307 ~TCP_IPV6_RECVPKTINFO; 10308 /* Force it to be sent up with the next msg */ 10309 tcp->tcp_recvifindex = 0; 10310 PASS_OPT_TO_IP(connp); 10311 } 10312 break; 10313 case IPV6_RECVTCLASS: 10314 if (!checkonly) { 10315 if (onoff) 10316 tcp->tcp_ipv6_recvancillary |= 10317 TCP_IPV6_RECVTCLASS; 10318 else 10319 tcp->tcp_ipv6_recvancillary &= 10320 ~TCP_IPV6_RECVTCLASS; 10321 PASS_OPT_TO_IP(connp); 10322 } 10323 break; 10324 case IPV6_RECVHOPLIMIT: 10325 if (!checkonly) { 10326 if (onoff) 10327 tcp->tcp_ipv6_recvancillary |= 10328 TCP_IPV6_RECVHOPLIMIT; 10329 else 10330 tcp->tcp_ipv6_recvancillary &= 10331 ~TCP_IPV6_RECVHOPLIMIT; 10332 /* Force it to be sent up with the next msg */ 10333 tcp->tcp_recvhops = 0xffffffffU; 10334 PASS_OPT_TO_IP(connp); 10335 } 10336 break; 10337 case IPV6_RECVHOPOPTS: 10338 if (!checkonly) { 10339 if (onoff) 10340 tcp->tcp_ipv6_recvancillary |= 10341 TCP_IPV6_RECVHOPOPTS; 10342 else 10343 tcp->tcp_ipv6_recvancillary &= 10344 ~TCP_IPV6_RECVHOPOPTS; 10345 PASS_OPT_TO_IP(connp); 10346 } 10347 break; 10348 case IPV6_RECVDSTOPTS: 10349 if (!checkonly) { 10350 if (onoff) 10351 tcp->tcp_ipv6_recvancillary |= 10352 TCP_IPV6_RECVDSTOPTS; 10353 else 10354 tcp->tcp_ipv6_recvancillary &= 10355 ~TCP_IPV6_RECVDSTOPTS; 10356 PASS_OPT_TO_IP(connp); 10357 } 10358 break; 10359 case _OLD_IPV6_RECVDSTOPTS: 10360 if (!checkonly) { 10361 if (onoff) 10362 tcp->tcp_ipv6_recvancillary |= 10363 TCP_OLD_IPV6_RECVDSTOPTS; 10364 else 10365 tcp->tcp_ipv6_recvancillary &= 10366 ~TCP_OLD_IPV6_RECVDSTOPTS; 10367 } 10368 break; 10369 case IPV6_RECVRTHDR: 10370 if (!checkonly) { 10371 if (onoff) 10372 tcp->tcp_ipv6_recvancillary |= 10373 TCP_IPV6_RECVRTHDR; 10374 else 10375 tcp->tcp_ipv6_recvancillary &= 10376 ~TCP_IPV6_RECVRTHDR; 10377 PASS_OPT_TO_IP(connp); 10378 } 10379 break; 10380 case IPV6_RECVRTHDRDSTOPTS: 10381 if (!checkonly) { 10382 if (onoff) 10383 tcp->tcp_ipv6_recvancillary |= 10384 TCP_IPV6_RECVRTDSTOPTS; 10385 else 10386 tcp->tcp_ipv6_recvancillary &= 10387 ~TCP_IPV6_RECVRTDSTOPTS; 10388 PASS_OPT_TO_IP(connp); 10389 } 10390 break; 10391 case IPV6_PKTINFO: 10392 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10393 return (EINVAL); 10394 if (checkonly) 10395 break; 10396 10397 if (inlen == 0) { 10398 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10399 } else { 10400 struct in6_pktinfo *pkti; 10401 10402 pkti = (struct in6_pktinfo *)invalp; 10403 /* 10404 * RFC 3542 states that ipi6_addr must be 10405 * the unspecified address when setting the 10406 * IPV6_PKTINFO sticky socket option on a 10407 * TCP socket. 10408 */ 10409 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10410 return (EINVAL); 10411 /* 10412 * IP will validate the source address and 10413 * interface index. 10414 */ 10415 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10416 reterr = ip_set_options(tcp->tcp_connp, 10417 level, name, invalp, inlen, cr); 10418 } else { 10419 reterr = ip6_set_pktinfo(cr, 10420 tcp->tcp_connp, pkti); 10421 } 10422 if (reterr != 0) 10423 return (reterr); 10424 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10425 ipp->ipp_addr = pkti->ipi6_addr; 10426 if (ipp->ipp_ifindex != 0) 10427 ipp->ipp_fields |= IPPF_IFINDEX; 10428 else 10429 ipp->ipp_fields &= ~IPPF_IFINDEX; 10430 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10431 ipp->ipp_fields |= IPPF_ADDR; 10432 else 10433 ipp->ipp_fields &= ~IPPF_ADDR; 10434 } 10435 reterr = tcp_build_hdrs(tcp); 10436 if (reterr != 0) 10437 return (reterr); 10438 break; 10439 case IPV6_TCLASS: 10440 if (inlen != 0 && inlen != sizeof (int)) 10441 return (EINVAL); 10442 if (checkonly) 10443 break; 10444 10445 if (inlen == 0) { 10446 ipp->ipp_fields &= ~IPPF_TCLASS; 10447 } else { 10448 if (*i1 > 255 || *i1 < -1) 10449 return (EINVAL); 10450 if (*i1 == -1) { 10451 ipp->ipp_tclass = 0; 10452 *i1 = 0; 10453 } else { 10454 ipp->ipp_tclass = *i1; 10455 } 10456 ipp->ipp_fields |= IPPF_TCLASS; 10457 } 10458 reterr = tcp_build_hdrs(tcp); 10459 if (reterr != 0) 10460 return (reterr); 10461 break; 10462 case IPV6_NEXTHOP: 10463 /* 10464 * IP will verify that the nexthop is reachable 10465 * and fail for sticky options. 10466 */ 10467 if (inlen != 0 && inlen != sizeof (sin6_t)) 10468 return (EINVAL); 10469 if (checkonly) 10470 break; 10471 10472 if (inlen == 0) { 10473 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10474 } else { 10475 sin6_t *sin6 = (sin6_t *)invalp; 10476 10477 if (sin6->sin6_family != AF_INET6) 10478 return (EAFNOSUPPORT); 10479 if (IN6_IS_ADDR_V4MAPPED( 10480 &sin6->sin6_addr)) 10481 return (EADDRNOTAVAIL); 10482 ipp->ipp_nexthop = sin6->sin6_addr; 10483 if (!IN6_IS_ADDR_UNSPECIFIED( 10484 &ipp->ipp_nexthop)) 10485 ipp->ipp_fields |= IPPF_NEXTHOP; 10486 else 10487 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10488 } 10489 reterr = tcp_build_hdrs(tcp); 10490 if (reterr != 0) 10491 return (reterr); 10492 PASS_OPT_TO_IP(connp); 10493 break; 10494 case IPV6_HOPOPTS: { 10495 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10496 10497 /* 10498 * Sanity checks - minimum size, size a multiple of 10499 * eight bytes, and matching size passed in. 10500 */ 10501 if (inlen != 0 && 10502 inlen != (8 * (hopts->ip6h_len + 1))) 10503 return (EINVAL); 10504 10505 if (checkonly) 10506 break; 10507 10508 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10509 (uchar_t **)&ipp->ipp_hopopts, 10510 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10511 if (reterr != 0) 10512 return (reterr); 10513 if (ipp->ipp_hopoptslen == 0) 10514 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10515 else 10516 ipp->ipp_fields |= IPPF_HOPOPTS; 10517 reterr = tcp_build_hdrs(tcp); 10518 if (reterr != 0) 10519 return (reterr); 10520 break; 10521 } 10522 case IPV6_RTHDRDSTOPTS: { 10523 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10524 10525 /* 10526 * Sanity checks - minimum size, size a multiple of 10527 * eight bytes, and matching size passed in. 10528 */ 10529 if (inlen != 0 && 10530 inlen != (8 * (dopts->ip6d_len + 1))) 10531 return (EINVAL); 10532 10533 if (checkonly) 10534 break; 10535 10536 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10537 (uchar_t **)&ipp->ipp_rtdstopts, 10538 &ipp->ipp_rtdstoptslen, 0); 10539 if (reterr != 0) 10540 return (reterr); 10541 if (ipp->ipp_rtdstoptslen == 0) 10542 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10543 else 10544 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10545 reterr = tcp_build_hdrs(tcp); 10546 if (reterr != 0) 10547 return (reterr); 10548 break; 10549 } 10550 case IPV6_DSTOPTS: { 10551 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10552 10553 /* 10554 * Sanity checks - minimum size, size a multiple of 10555 * eight bytes, and matching size passed in. 10556 */ 10557 if (inlen != 0 && 10558 inlen != (8 * (dopts->ip6d_len + 1))) 10559 return (EINVAL); 10560 10561 if (checkonly) 10562 break; 10563 10564 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10565 (uchar_t **)&ipp->ipp_dstopts, 10566 &ipp->ipp_dstoptslen, 0); 10567 if (reterr != 0) 10568 return (reterr); 10569 if (ipp->ipp_dstoptslen == 0) 10570 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10571 else 10572 ipp->ipp_fields |= IPPF_DSTOPTS; 10573 reterr = tcp_build_hdrs(tcp); 10574 if (reterr != 0) 10575 return (reterr); 10576 break; 10577 } 10578 case IPV6_RTHDR: { 10579 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10580 10581 /* 10582 * Sanity checks - minimum size, size a multiple of 10583 * eight bytes, and matching size passed in. 10584 */ 10585 if (inlen != 0 && 10586 inlen != (8 * (rt->ip6r_len + 1))) 10587 return (EINVAL); 10588 10589 if (checkonly) 10590 break; 10591 10592 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10593 (uchar_t **)&ipp->ipp_rthdr, 10594 &ipp->ipp_rthdrlen, 0); 10595 if (reterr != 0) 10596 return (reterr); 10597 if (ipp->ipp_rthdrlen == 0) 10598 ipp->ipp_fields &= ~IPPF_RTHDR; 10599 else 10600 ipp->ipp_fields |= IPPF_RTHDR; 10601 reterr = tcp_build_hdrs(tcp); 10602 if (reterr != 0) 10603 return (reterr); 10604 break; 10605 } 10606 case IPV6_V6ONLY: 10607 if (!checkonly) { 10608 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10609 } 10610 break; 10611 case IPV6_USE_MIN_MTU: 10612 if (inlen != sizeof (int)) 10613 return (EINVAL); 10614 10615 if (*i1 < -1 || *i1 > 1) 10616 return (EINVAL); 10617 10618 if (checkonly) 10619 break; 10620 10621 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10622 ipp->ipp_use_min_mtu = *i1; 10623 break; 10624 case IPV6_SEC_OPT: 10625 /* 10626 * We should not allow policy setting after 10627 * we start listening for connections. 10628 */ 10629 if (tcp->tcp_state == TCPS_LISTEN) { 10630 return (EINVAL); 10631 } else { 10632 /* Handled at the IP level */ 10633 return (-EINVAL); 10634 } 10635 case IPV6_SRC_PREFERENCES: 10636 if (inlen != sizeof (uint32_t)) 10637 return (EINVAL); 10638 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10639 *(uint32_t *)invalp); 10640 if (reterr != 0) { 10641 *outlenp = 0; 10642 return (reterr); 10643 } 10644 break; 10645 default: 10646 *outlenp = 0; 10647 return (EINVAL); 10648 } 10649 break; 10650 } /* end IPPROTO_IPV6 */ 10651 default: 10652 *outlenp = 0; 10653 return (EINVAL); 10654 } 10655 /* 10656 * Common case of OK return with outval same as inval 10657 */ 10658 if (invalp != outvalp) { 10659 /* don't trust bcopy for identical src/dst */ 10660 (void) bcopy(invalp, outvalp, inlen); 10661 } 10662 *outlenp = inlen; 10663 return (0); 10664 } 10665 10666 /* ARGSUSED */ 10667 int 10668 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10669 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10670 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10671 { 10672 conn_t *connp = Q_TO_CONN(q); 10673 10674 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10675 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10676 } 10677 10678 int 10679 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10680 const void *optvalp, socklen_t optlen, cred_t *cr) 10681 { 10682 conn_t *connp = (conn_t *)proto_handle; 10683 squeue_t *sqp = connp->conn_sqp; 10684 int error; 10685 10686 ASSERT(connp->conn_upper_handle != NULL); 10687 /* 10688 * Entering the squeue synchronously can result in a context switch, 10689 * which can cause a rather sever performance degradation. So we try to 10690 * handle whatever options we can without entering the squeue. 10691 */ 10692 if (level == IPPROTO_TCP) { 10693 switch (option_name) { 10694 case TCP_NODELAY: 10695 if (optlen != sizeof (int32_t)) 10696 return (EINVAL); 10697 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10698 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10699 connp->conn_tcp->tcp_mss; 10700 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10701 return (0); 10702 default: 10703 break; 10704 } 10705 } 10706 10707 error = squeue_synch_enter(sqp, connp, NULL); 10708 if (error == ENOMEM) { 10709 return (ENOMEM); 10710 } 10711 10712 error = proto_opt_check(level, option_name, optlen, NULL, 10713 tcp_opt_obj.odb_opt_des_arr, 10714 tcp_opt_obj.odb_opt_arr_cnt, 10715 tcp_opt_obj.odb_topmost_tpiprovider, 10716 B_TRUE, B_FALSE, cr); 10717 10718 if (error != 0) { 10719 if (error < 0) { 10720 error = proto_tlitosyserr(-error); 10721 } 10722 squeue_synch_exit(sqp, connp); 10723 return (error); 10724 } 10725 10726 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10727 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10728 NULL, cr, NULL); 10729 squeue_synch_exit(sqp, connp); 10730 10731 if (error < 0) { 10732 /* 10733 * Pass on to ip 10734 */ 10735 error = ip_set_options(connp, level, option_name, optvalp, 10736 optlen, cr); 10737 } 10738 return (error); 10739 } 10740 10741 /* 10742 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10743 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10744 * headers, and the maximum size tcp header (to avoid reallocation 10745 * on the fly for additional tcp options). 10746 * Returns failure if can't allocate memory. 10747 */ 10748 static int 10749 tcp_build_hdrs(tcp_t *tcp) 10750 { 10751 char *hdrs; 10752 uint_t hdrs_len; 10753 ip6i_t *ip6i; 10754 char buf[TCP_MAX_HDR_LENGTH]; 10755 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10756 in6_addr_t src, dst; 10757 tcp_stack_t *tcps = tcp->tcp_tcps; 10758 conn_t *connp = tcp->tcp_connp; 10759 10760 /* 10761 * save the existing tcp header and source/dest IP addresses 10762 */ 10763 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10764 src = tcp->tcp_ip6h->ip6_src; 10765 dst = tcp->tcp_ip6h->ip6_dst; 10766 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10767 ASSERT(hdrs_len != 0); 10768 if (hdrs_len > tcp->tcp_iphc_len) { 10769 /* Need to reallocate */ 10770 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10771 if (hdrs == NULL) 10772 return (ENOMEM); 10773 if (tcp->tcp_iphc != NULL) { 10774 if (tcp->tcp_hdr_grown) { 10775 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10776 } else { 10777 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10778 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10779 } 10780 tcp->tcp_iphc_len = 0; 10781 } 10782 ASSERT(tcp->tcp_iphc_len == 0); 10783 tcp->tcp_iphc = hdrs; 10784 tcp->tcp_iphc_len = hdrs_len; 10785 tcp->tcp_hdr_grown = B_TRUE; 10786 } 10787 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10788 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10789 10790 /* Set header fields not in ipp */ 10791 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10792 ip6i = (ip6i_t *)tcp->tcp_iphc; 10793 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10794 } else { 10795 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10796 } 10797 /* 10798 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10799 * 10800 * tcp->tcp_tcp_hdr_len doesn't change here. 10801 */ 10802 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10803 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10804 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10805 10806 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10807 10808 tcp->tcp_ip6h->ip6_src = src; 10809 tcp->tcp_ip6h->ip6_dst = dst; 10810 10811 /* 10812 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10813 * the default value for TCP. 10814 */ 10815 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10816 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10817 10818 /* 10819 * If we're setting extension headers after a connection 10820 * has been established, and if we have a routing header 10821 * among the extension headers, call ip_massage_options_v6 to 10822 * manipulate the routing header/ip6_dst set the checksum 10823 * difference in the tcp header template. 10824 * (This happens in tcp_connect_ipv6 if the routing header 10825 * is set prior to the connect.) 10826 * Set the tcp_sum to zero first in case we've cleared a 10827 * routing header or don't have one at all. 10828 */ 10829 tcp->tcp_sum = 0; 10830 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10831 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10832 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10833 (uint8_t *)tcp->tcp_tcph); 10834 if (rth != NULL) { 10835 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10836 rth, tcps->tcps_netstack); 10837 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10838 (tcp->tcp_sum >> 16)); 10839 } 10840 } 10841 10842 /* Try to get everything in a single mblk */ 10843 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10844 hdrs_len + tcps->tcps_wroff_xtra); 10845 return (0); 10846 } 10847 10848 /* 10849 * Transfer any source route option from ipha to buf/dst in reversed form. 10850 */ 10851 static int 10852 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10853 { 10854 ipoptp_t opts; 10855 uchar_t *opt; 10856 uint8_t optval; 10857 uint8_t optlen; 10858 uint32_t len = 0; 10859 10860 for (optval = ipoptp_first(&opts, ipha); 10861 optval != IPOPT_EOL; 10862 optval = ipoptp_next(&opts)) { 10863 opt = opts.ipoptp_cur; 10864 optlen = opts.ipoptp_len; 10865 switch (optval) { 10866 int off1, off2; 10867 case IPOPT_SSRR: 10868 case IPOPT_LSRR: 10869 10870 /* Reverse source route */ 10871 /* 10872 * First entry should be the next to last one in the 10873 * current source route (the last entry is our 10874 * address.) 10875 * The last entry should be the final destination. 10876 */ 10877 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10878 buf[IPOPT_OLEN] = (uint8_t)optlen; 10879 off1 = IPOPT_MINOFF_SR - 1; 10880 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10881 if (off2 < 0) { 10882 /* No entries in source route */ 10883 break; 10884 } 10885 bcopy(opt + off2, dst, IP_ADDR_LEN); 10886 /* 10887 * Note: use src since ipha has not had its src 10888 * and dst reversed (it is in the state it was 10889 * received. 10890 */ 10891 bcopy(&ipha->ipha_src, buf + off2, 10892 IP_ADDR_LEN); 10893 off2 -= IP_ADDR_LEN; 10894 10895 while (off2 > 0) { 10896 bcopy(opt + off2, buf + off1, 10897 IP_ADDR_LEN); 10898 off1 += IP_ADDR_LEN; 10899 off2 -= IP_ADDR_LEN; 10900 } 10901 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10902 buf += optlen; 10903 len += optlen; 10904 break; 10905 } 10906 } 10907 done: 10908 /* Pad the resulting options */ 10909 while (len & 0x3) { 10910 *buf++ = IPOPT_EOL; 10911 len++; 10912 } 10913 return (len); 10914 } 10915 10916 10917 /* 10918 * Extract and revert a source route from ipha (if any) 10919 * and then update the relevant fields in both tcp_t and the standard header. 10920 */ 10921 static void 10922 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10923 { 10924 char buf[TCP_MAX_HDR_LENGTH]; 10925 uint_t tcph_len; 10926 int len; 10927 10928 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10929 len = IPH_HDR_LENGTH(ipha); 10930 if (len == IP_SIMPLE_HDR_LENGTH) 10931 /* Nothing to do */ 10932 return; 10933 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10934 (len & 0x3)) 10935 return; 10936 10937 tcph_len = tcp->tcp_tcp_hdr_len; 10938 bcopy(tcp->tcp_tcph, buf, tcph_len); 10939 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10940 (tcp->tcp_ipha->ipha_dst & 0xffff); 10941 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10942 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10943 len += IP_SIMPLE_HDR_LENGTH; 10944 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10945 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10946 if ((int)tcp->tcp_sum < 0) 10947 tcp->tcp_sum--; 10948 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10949 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10950 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10951 bcopy(buf, tcp->tcp_tcph, tcph_len); 10952 tcp->tcp_ip_hdr_len = len; 10953 tcp->tcp_ipha->ipha_version_and_hdr_length = 10954 (IP_VERSION << 4) | (len >> 2); 10955 len += tcph_len; 10956 tcp->tcp_hdr_len = len; 10957 } 10958 10959 /* 10960 * Copy the standard header into its new location, 10961 * lay in the new options and then update the relevant 10962 * fields in both tcp_t and the standard header. 10963 */ 10964 static int 10965 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10966 { 10967 uint_t tcph_len; 10968 uint8_t *ip_optp; 10969 tcph_t *new_tcph; 10970 tcp_stack_t *tcps = tcp->tcp_tcps; 10971 conn_t *connp = tcp->tcp_connp; 10972 10973 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10974 return (EINVAL); 10975 10976 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10977 return (EINVAL); 10978 10979 if (checkonly) { 10980 /* 10981 * do not really set, just pretend to - T_CHECK 10982 */ 10983 return (0); 10984 } 10985 10986 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10987 if (tcp->tcp_label_len > 0) { 10988 int padlen; 10989 uint8_t opt; 10990 10991 /* convert list termination to no-ops */ 10992 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 10993 ip_optp += ip_optp[IPOPT_OLEN]; 10994 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 10995 while (--padlen >= 0) 10996 *ip_optp++ = opt; 10997 } 10998 tcph_len = tcp->tcp_tcp_hdr_len; 10999 new_tcph = (tcph_t *)(ip_optp + len); 11000 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11001 tcp->tcp_tcph = new_tcph; 11002 bcopy(ptr, ip_optp, len); 11003 11004 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11005 11006 tcp->tcp_ip_hdr_len = len; 11007 tcp->tcp_ipha->ipha_version_and_hdr_length = 11008 (IP_VERSION << 4) | (len >> 2); 11009 tcp->tcp_hdr_len = len + tcph_len; 11010 if (!TCP_IS_DETACHED(tcp)) { 11011 /* Always allocate room for all options. */ 11012 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11013 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11014 } 11015 return (0); 11016 } 11017 11018 /* Get callback routine passed to nd_load by tcp_param_register */ 11019 /* ARGSUSED */ 11020 static int 11021 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11022 { 11023 tcpparam_t *tcppa = (tcpparam_t *)cp; 11024 11025 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11026 return (0); 11027 } 11028 11029 /* 11030 * Walk through the param array specified registering each element with the 11031 * named dispatch handler. 11032 */ 11033 static boolean_t 11034 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11035 { 11036 for (; cnt-- > 0; tcppa++) { 11037 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11038 if (!nd_load(ndp, tcppa->tcp_param_name, 11039 tcp_param_get, tcp_param_set, 11040 (caddr_t)tcppa)) { 11041 nd_free(ndp); 11042 return (B_FALSE); 11043 } 11044 } 11045 } 11046 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11047 KM_SLEEP); 11048 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11049 sizeof (tcpparam_t)); 11050 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11051 tcp_param_get, tcp_param_set_aligned, 11052 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11053 nd_free(ndp); 11054 return (B_FALSE); 11055 } 11056 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11057 KM_SLEEP); 11058 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11059 sizeof (tcpparam_t)); 11060 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11061 tcp_param_get, tcp_param_set_aligned, 11062 (caddr_t)tcps->tcps_mdt_head_param)) { 11063 nd_free(ndp); 11064 return (B_FALSE); 11065 } 11066 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11067 KM_SLEEP); 11068 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11069 sizeof (tcpparam_t)); 11070 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11071 tcp_param_get, tcp_param_set_aligned, 11072 (caddr_t)tcps->tcps_mdt_tail_param)) { 11073 nd_free(ndp); 11074 return (B_FALSE); 11075 } 11076 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11077 KM_SLEEP); 11078 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11079 sizeof (tcpparam_t)); 11080 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11081 tcp_param_get, tcp_param_set_aligned, 11082 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11083 nd_free(ndp); 11084 return (B_FALSE); 11085 } 11086 if (!nd_load(ndp, "tcp_extra_priv_ports", 11087 tcp_extra_priv_ports_get, NULL, NULL)) { 11088 nd_free(ndp); 11089 return (B_FALSE); 11090 } 11091 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11092 NULL, tcp_extra_priv_ports_add, NULL)) { 11093 nd_free(ndp); 11094 return (B_FALSE); 11095 } 11096 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11097 NULL, tcp_extra_priv_ports_del, NULL)) { 11098 nd_free(ndp); 11099 return (B_FALSE); 11100 } 11101 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11102 tcp_1948_phrase_set, NULL)) { 11103 nd_free(ndp); 11104 return (B_FALSE); 11105 } 11106 /* 11107 * Dummy ndd variables - only to convey obsolescence information 11108 * through printing of their name (no get or set routines) 11109 * XXX Remove in future releases ? 11110 */ 11111 if (!nd_load(ndp, 11112 "tcp_close_wait_interval(obsoleted - " 11113 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11114 nd_free(ndp); 11115 return (B_FALSE); 11116 } 11117 return (B_TRUE); 11118 } 11119 11120 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11121 /* ARGSUSED */ 11122 static int 11123 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11124 cred_t *cr) 11125 { 11126 long new_value; 11127 tcpparam_t *tcppa = (tcpparam_t *)cp; 11128 11129 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11130 new_value < tcppa->tcp_param_min || 11131 new_value > tcppa->tcp_param_max) { 11132 return (EINVAL); 11133 } 11134 /* 11135 * Need to make sure new_value is a multiple of 4. If it is not, 11136 * round it up. For future 64 bit requirement, we actually make it 11137 * a multiple of 8. 11138 */ 11139 if (new_value & 0x7) { 11140 new_value = (new_value & ~0x7) + 0x8; 11141 } 11142 tcppa->tcp_param_val = new_value; 11143 return (0); 11144 } 11145 11146 /* Set callback routine passed to nd_load by tcp_param_register */ 11147 /* ARGSUSED */ 11148 static int 11149 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11150 { 11151 long new_value; 11152 tcpparam_t *tcppa = (tcpparam_t *)cp; 11153 11154 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11155 new_value < tcppa->tcp_param_min || 11156 new_value > tcppa->tcp_param_max) { 11157 return (EINVAL); 11158 } 11159 tcppa->tcp_param_val = new_value; 11160 return (0); 11161 } 11162 11163 /* 11164 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11165 * is filled, return as much as we can. The message passed in may be 11166 * multi-part, chained using b_cont. "start" is the starting sequence 11167 * number for this piece. 11168 */ 11169 static mblk_t * 11170 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11171 { 11172 uint32_t end; 11173 mblk_t *mp1; 11174 mblk_t *mp2; 11175 mblk_t *next_mp; 11176 uint32_t u1; 11177 tcp_stack_t *tcps = tcp->tcp_tcps; 11178 11179 /* Walk through all the new pieces. */ 11180 do { 11181 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11182 (uintptr_t)INT_MAX); 11183 end = start + (int)(mp->b_wptr - mp->b_rptr); 11184 next_mp = mp->b_cont; 11185 if (start == end) { 11186 /* Empty. Blast it. */ 11187 freeb(mp); 11188 continue; 11189 } 11190 mp->b_cont = NULL; 11191 TCP_REASS_SET_SEQ(mp, start); 11192 TCP_REASS_SET_END(mp, end); 11193 mp1 = tcp->tcp_reass_tail; 11194 if (!mp1) { 11195 tcp->tcp_reass_tail = mp; 11196 tcp->tcp_reass_head = mp; 11197 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11198 UPDATE_MIB(&tcps->tcps_mib, 11199 tcpInDataUnorderBytes, end - start); 11200 continue; 11201 } 11202 /* New stuff completely beyond tail? */ 11203 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11204 /* Link it on end. */ 11205 mp1->b_cont = mp; 11206 tcp->tcp_reass_tail = mp; 11207 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11208 UPDATE_MIB(&tcps->tcps_mib, 11209 tcpInDataUnorderBytes, end - start); 11210 continue; 11211 } 11212 mp1 = tcp->tcp_reass_head; 11213 u1 = TCP_REASS_SEQ(mp1); 11214 /* New stuff at the front? */ 11215 if (SEQ_LT(start, u1)) { 11216 /* Yes... Check for overlap. */ 11217 mp->b_cont = mp1; 11218 tcp->tcp_reass_head = mp; 11219 tcp_reass_elim_overlap(tcp, mp); 11220 continue; 11221 } 11222 /* 11223 * The new piece fits somewhere between the head and tail. 11224 * We find our slot, where mp1 precedes us and mp2 trails. 11225 */ 11226 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11227 u1 = TCP_REASS_SEQ(mp2); 11228 if (SEQ_LEQ(start, u1)) 11229 break; 11230 } 11231 /* Link ourselves in */ 11232 mp->b_cont = mp2; 11233 mp1->b_cont = mp; 11234 11235 /* Trim overlap with following mblk(s) first */ 11236 tcp_reass_elim_overlap(tcp, mp); 11237 11238 /* Trim overlap with preceding mblk */ 11239 tcp_reass_elim_overlap(tcp, mp1); 11240 11241 } while (start = end, mp = next_mp); 11242 mp1 = tcp->tcp_reass_head; 11243 /* Anything ready to go? */ 11244 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11245 return (NULL); 11246 /* Eat what we can off the queue */ 11247 for (;;) { 11248 mp = mp1->b_cont; 11249 end = TCP_REASS_END(mp1); 11250 TCP_REASS_SET_SEQ(mp1, 0); 11251 TCP_REASS_SET_END(mp1, 0); 11252 if (!mp) { 11253 tcp->tcp_reass_tail = NULL; 11254 break; 11255 } 11256 if (end != TCP_REASS_SEQ(mp)) { 11257 mp1->b_cont = NULL; 11258 break; 11259 } 11260 mp1 = mp; 11261 } 11262 mp1 = tcp->tcp_reass_head; 11263 tcp->tcp_reass_head = mp; 11264 return (mp1); 11265 } 11266 11267 /* Eliminate any overlap that mp may have over later mblks */ 11268 static void 11269 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11270 { 11271 uint32_t end; 11272 mblk_t *mp1; 11273 uint32_t u1; 11274 tcp_stack_t *tcps = tcp->tcp_tcps; 11275 11276 end = TCP_REASS_END(mp); 11277 while ((mp1 = mp->b_cont) != NULL) { 11278 u1 = TCP_REASS_SEQ(mp1); 11279 if (!SEQ_GT(end, u1)) 11280 break; 11281 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11282 mp->b_wptr -= end - u1; 11283 TCP_REASS_SET_END(mp, u1); 11284 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11285 UPDATE_MIB(&tcps->tcps_mib, 11286 tcpInDataPartDupBytes, end - u1); 11287 break; 11288 } 11289 mp->b_cont = mp1->b_cont; 11290 TCP_REASS_SET_SEQ(mp1, 0); 11291 TCP_REASS_SET_END(mp1, 0); 11292 freeb(mp1); 11293 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11294 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11295 } 11296 if (!mp1) 11297 tcp->tcp_reass_tail = mp; 11298 } 11299 11300 static uint_t 11301 tcp_rwnd_reopen(tcp_t *tcp) 11302 { 11303 uint_t ret = 0; 11304 uint_t thwin; 11305 11306 /* Learn the latest rwnd information that we sent to the other side. */ 11307 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11308 << tcp->tcp_rcv_ws; 11309 /* This is peer's calculated send window (our receive window). */ 11310 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11311 /* 11312 * Increase the receive window to max. But we need to do receiver 11313 * SWS avoidance. This means that we need to check the increase of 11314 * of receive window is at least 1 MSS. 11315 */ 11316 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11317 /* 11318 * If the window that the other side knows is less than max 11319 * deferred acks segments, send an update immediately. 11320 */ 11321 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11322 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11323 ret = TH_ACK_NEEDED; 11324 } 11325 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11326 } 11327 return (ret); 11328 } 11329 11330 /* 11331 * Send up all messages queued on tcp_rcv_list. 11332 */ 11333 static uint_t 11334 tcp_rcv_drain(tcp_t *tcp) 11335 { 11336 mblk_t *mp; 11337 uint_t ret = 0; 11338 #ifdef DEBUG 11339 uint_t cnt = 0; 11340 #endif 11341 queue_t *q = tcp->tcp_rq; 11342 11343 /* Can't drain on an eager connection */ 11344 if (tcp->tcp_listener != NULL) 11345 return (ret); 11346 11347 /* Can't be a non-STREAMS connection */ 11348 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11349 11350 /* No need for the push timer now. */ 11351 if (tcp->tcp_push_tid != 0) { 11352 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11353 tcp->tcp_push_tid = 0; 11354 } 11355 11356 /* 11357 * Handle two cases here: we are currently fused or we were 11358 * previously fused and have some urgent data to be delivered 11359 * upstream. The latter happens because we either ran out of 11360 * memory or were detached and therefore sending the SIGURG was 11361 * deferred until this point. In either case we pass control 11362 * over to tcp_fuse_rcv_drain() since it may need to complete 11363 * some work. 11364 */ 11365 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11366 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11367 tcp->tcp_fused_sigurg_mp != NULL); 11368 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11369 &tcp->tcp_fused_sigurg_mp)) 11370 return (ret); 11371 } 11372 11373 while ((mp = tcp->tcp_rcv_list) != NULL) { 11374 tcp->tcp_rcv_list = mp->b_next; 11375 mp->b_next = NULL; 11376 #ifdef DEBUG 11377 cnt += msgdsize(mp); 11378 #endif 11379 /* Does this need SSL processing first? */ 11380 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11381 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11382 mblk_t *, mp); 11383 tcp_kssl_input(tcp, mp); 11384 continue; 11385 } 11386 putnext(q, mp); 11387 } 11388 #ifdef DEBUG 11389 ASSERT(cnt == tcp->tcp_rcv_cnt); 11390 #endif 11391 tcp->tcp_rcv_last_head = NULL; 11392 tcp->tcp_rcv_last_tail = NULL; 11393 tcp->tcp_rcv_cnt = 0; 11394 11395 if (canputnext(q)) 11396 return (tcp_rwnd_reopen(tcp)); 11397 11398 return (ret); 11399 } 11400 11401 /* 11402 * Queue data on tcp_rcv_list which is a b_next chain. 11403 * tcp_rcv_last_head/tail is the last element of this chain. 11404 * Each element of the chain is a b_cont chain. 11405 * 11406 * M_DATA messages are added to the current element. 11407 * Other messages are added as new (b_next) elements. 11408 */ 11409 void 11410 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11411 { 11412 ASSERT(seg_len == msgdsize(mp)); 11413 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11414 11415 if (tcp->tcp_rcv_list == NULL) { 11416 ASSERT(tcp->tcp_rcv_last_head == NULL); 11417 tcp->tcp_rcv_list = mp; 11418 tcp->tcp_rcv_last_head = mp; 11419 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11420 tcp->tcp_rcv_last_tail->b_cont = mp; 11421 } else { 11422 tcp->tcp_rcv_last_head->b_next = mp; 11423 tcp->tcp_rcv_last_head = mp; 11424 } 11425 11426 while (mp->b_cont) 11427 mp = mp->b_cont; 11428 11429 tcp->tcp_rcv_last_tail = mp; 11430 tcp->tcp_rcv_cnt += seg_len; 11431 tcp->tcp_rwnd -= seg_len; 11432 } 11433 11434 /* 11435 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11436 * 11437 * This is the default entry function into TCP on the read side. TCP is 11438 * always entered via squeue i.e. using squeue's for mutual exclusion. 11439 * When classifier does a lookup to find the tcp, it also puts a reference 11440 * on the conn structure associated so the tcp is guaranteed to exist 11441 * when we come here. We still need to check the state because it might 11442 * as well has been closed. The squeue processing function i.e. squeue_enter, 11443 * is responsible for doing the CONN_DEC_REF. 11444 * 11445 * Apart from the default entry point, IP also sends packets directly to 11446 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11447 * connections. 11448 */ 11449 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11450 void 11451 tcp_input(void *arg, mblk_t *mp, void *arg2) 11452 { 11453 conn_t *connp = (conn_t *)arg; 11454 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11455 11456 /* arg2 is the sqp */ 11457 ASSERT(arg2 != NULL); 11458 ASSERT(mp != NULL); 11459 11460 /* 11461 * Don't accept any input on a closed tcp as this TCP logically does 11462 * not exist on the system. Don't proceed further with this TCP. 11463 * For eg. this packet could trigger another close of this tcp 11464 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11465 * tcp_clean_death / tcp_closei_local must be called at most once 11466 * on a TCP. In this case we need to refeed the packet into the 11467 * classifier and figure out where the packet should go. Need to 11468 * preserve the recv_ill somehow. Until we figure that out, for 11469 * now just drop the packet if we can't classify the packet. 11470 */ 11471 if (tcp->tcp_state == TCPS_CLOSED || 11472 tcp->tcp_state == TCPS_BOUND) { 11473 conn_t *new_connp; 11474 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11475 11476 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11477 if (new_connp != NULL) { 11478 tcp_reinput(new_connp, mp, arg2); 11479 return; 11480 } 11481 /* We failed to classify. For now just drop the packet */ 11482 freemsg(mp); 11483 return; 11484 } 11485 11486 if (DB_TYPE(mp) != M_DATA) { 11487 tcp_rput_common(tcp, mp); 11488 return; 11489 } 11490 11491 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11492 squeue_t *final_sqp; 11493 11494 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11495 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11496 DB_CKSUMSTART(mp) = 0; 11497 if (tcp->tcp_state == TCPS_SYN_SENT && 11498 connp->conn_final_sqp == NULL && 11499 tcp_outbound_squeue_switch) { 11500 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11501 connp->conn_final_sqp = final_sqp; 11502 if (connp->conn_final_sqp != connp->conn_sqp) { 11503 CONN_INC_REF(connp); 11504 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11505 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11506 tcp_rput_data, connp, ip_squeue_flag, 11507 SQTAG_CONNECT_FINISH); 11508 return; 11509 } 11510 } 11511 } 11512 tcp_rput_data(connp, mp, arg2); 11513 } 11514 11515 /* 11516 * The read side put procedure. 11517 * The packets passed up by ip are assume to be aligned according to 11518 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11519 */ 11520 static void 11521 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11522 { 11523 /* 11524 * tcp_rput_data() does not expect M_CTL except for the case 11525 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11526 * type. Need to make sure that any other M_CTLs don't make 11527 * it to tcp_rput_data since it is not expecting any and doesn't 11528 * check for it. 11529 */ 11530 if (DB_TYPE(mp) == M_CTL) { 11531 switch (*(uint32_t *)(mp->b_rptr)) { 11532 case TCP_IOC_ABORT_CONN: 11533 /* 11534 * Handle connection abort request. 11535 */ 11536 tcp_ioctl_abort_handler(tcp, mp); 11537 return; 11538 case IPSEC_IN: 11539 /* 11540 * Only secure icmp arrive in TCP and they 11541 * don't go through data path. 11542 */ 11543 tcp_icmp_error(tcp, mp); 11544 return; 11545 case IN_PKTINFO: 11546 /* 11547 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11548 * sockets that are receiving IPv4 traffic. tcp 11549 */ 11550 ASSERT(tcp->tcp_family == AF_INET6); 11551 ASSERT(tcp->tcp_ipv6_recvancillary & 11552 TCP_IPV6_RECVPKTINFO); 11553 tcp_rput_data(tcp->tcp_connp, mp, 11554 tcp->tcp_connp->conn_sqp); 11555 return; 11556 case MDT_IOC_INFO_UPDATE: 11557 /* 11558 * Handle Multidata information update; the 11559 * following routine will free the message. 11560 */ 11561 if (tcp->tcp_connp->conn_mdt_ok) { 11562 tcp_mdt_update(tcp, 11563 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11564 B_FALSE); 11565 } 11566 freemsg(mp); 11567 return; 11568 case LSO_IOC_INFO_UPDATE: 11569 /* 11570 * Handle LSO information update; the following 11571 * routine will free the message. 11572 */ 11573 if (tcp->tcp_connp->conn_lso_ok) { 11574 tcp_lso_update(tcp, 11575 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11576 } 11577 freemsg(mp); 11578 return; 11579 default: 11580 /* 11581 * tcp_icmp_err() will process the M_CTL packets. 11582 * Non-ICMP packets, if any, will be discarded in 11583 * tcp_icmp_err(). We will process the ICMP packet 11584 * even if we are TCP_IS_DETACHED_NONEAGER as the 11585 * incoming ICMP packet may result in changing 11586 * the tcp_mss, which we would need if we have 11587 * packets to retransmit. 11588 */ 11589 tcp_icmp_error(tcp, mp); 11590 return; 11591 } 11592 } 11593 11594 /* No point processing the message if tcp is already closed */ 11595 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11596 freemsg(mp); 11597 return; 11598 } 11599 11600 tcp_rput_other(tcp, mp); 11601 } 11602 11603 11604 /* The minimum of smoothed mean deviation in RTO calculation. */ 11605 #define TCP_SD_MIN 400 11606 11607 /* 11608 * Set RTO for this connection. The formula is from Jacobson and Karels' 11609 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11610 * are the same as those in Appendix A.2 of that paper. 11611 * 11612 * m = new measurement 11613 * sa = smoothed RTT average (8 * average estimates). 11614 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11615 */ 11616 static void 11617 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11618 { 11619 long m = TICK_TO_MSEC(rtt); 11620 clock_t sa = tcp->tcp_rtt_sa; 11621 clock_t sv = tcp->tcp_rtt_sd; 11622 clock_t rto; 11623 tcp_stack_t *tcps = tcp->tcp_tcps; 11624 11625 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11626 tcp->tcp_rtt_update++; 11627 11628 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11629 if (sa != 0) { 11630 /* 11631 * Update average estimator: 11632 * new rtt = 7/8 old rtt + 1/8 Error 11633 */ 11634 11635 /* m is now Error in estimate. */ 11636 m -= sa >> 3; 11637 if ((sa += m) <= 0) { 11638 /* 11639 * Don't allow the smoothed average to be negative. 11640 * We use 0 to denote reinitialization of the 11641 * variables. 11642 */ 11643 sa = 1; 11644 } 11645 11646 /* 11647 * Update deviation estimator: 11648 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11649 */ 11650 if (m < 0) 11651 m = -m; 11652 m -= sv >> 2; 11653 sv += m; 11654 } else { 11655 /* 11656 * This follows BSD's implementation. So the reinitialized 11657 * RTO is 3 * m. We cannot go less than 2 because if the 11658 * link is bandwidth dominated, doubling the window size 11659 * during slow start means doubling the RTT. We want to be 11660 * more conservative when we reinitialize our estimates. 3 11661 * is just a convenient number. 11662 */ 11663 sa = m << 3; 11664 sv = m << 1; 11665 } 11666 if (sv < TCP_SD_MIN) { 11667 /* 11668 * We do not know that if sa captures the delay ACK 11669 * effect as in a long train of segments, a receiver 11670 * does not delay its ACKs. So set the minimum of sv 11671 * to be TCP_SD_MIN, which is default to 400 ms, twice 11672 * of BSD DATO. That means the minimum of mean 11673 * deviation is 100 ms. 11674 * 11675 */ 11676 sv = TCP_SD_MIN; 11677 } 11678 tcp->tcp_rtt_sa = sa; 11679 tcp->tcp_rtt_sd = sv; 11680 /* 11681 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11682 * 11683 * Add tcp_rexmit_interval extra in case of extreme environment 11684 * where the algorithm fails to work. The default value of 11685 * tcp_rexmit_interval_extra should be 0. 11686 * 11687 * As we use a finer grained clock than BSD and update 11688 * RTO for every ACKs, add in another .25 of RTT to the 11689 * deviation of RTO to accomodate burstiness of 1/4 of 11690 * window size. 11691 */ 11692 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11693 11694 if (rto > tcps->tcps_rexmit_interval_max) { 11695 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11696 } else if (rto < tcps->tcps_rexmit_interval_min) { 11697 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11698 } else { 11699 tcp->tcp_rto = rto; 11700 } 11701 11702 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11703 tcp->tcp_timer_backoff = 0; 11704 } 11705 11706 /* 11707 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11708 * send queue which starts at the given seq. no. 11709 * 11710 * Parameters: 11711 * tcp_t *tcp: the tcp instance pointer. 11712 * uint32_t seq: the starting seq. no of the requested segment. 11713 * int32_t *off: after the execution, *off will be the offset to 11714 * the returned mblk which points to the requested seq no. 11715 * It is the caller's responsibility to send in a non-null off. 11716 * 11717 * Return: 11718 * A mblk_t pointer pointing to the requested segment in send queue. 11719 */ 11720 static mblk_t * 11721 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11722 { 11723 int32_t cnt; 11724 mblk_t *mp; 11725 11726 /* Defensive coding. Make sure we don't send incorrect data. */ 11727 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11728 return (NULL); 11729 11730 cnt = seq - tcp->tcp_suna; 11731 mp = tcp->tcp_xmit_head; 11732 while (cnt > 0 && mp != NULL) { 11733 cnt -= mp->b_wptr - mp->b_rptr; 11734 if (cnt < 0) { 11735 cnt += mp->b_wptr - mp->b_rptr; 11736 break; 11737 } 11738 mp = mp->b_cont; 11739 } 11740 ASSERT(mp != NULL); 11741 *off = cnt; 11742 return (mp); 11743 } 11744 11745 /* 11746 * This function handles all retransmissions if SACK is enabled for this 11747 * connection. First it calculates how many segments can be retransmitted 11748 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11749 * segments. A segment is eligible if sack_cnt for that segment is greater 11750 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11751 * all eligible segments, it checks to see if TCP can send some new segments 11752 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11753 * 11754 * Parameters: 11755 * tcp_t *tcp: the tcp structure of the connection. 11756 * uint_t *flags: in return, appropriate value will be set for 11757 * tcp_rput_data(). 11758 */ 11759 static void 11760 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11761 { 11762 notsack_blk_t *notsack_blk; 11763 int32_t usable_swnd; 11764 int32_t mss; 11765 uint32_t seg_len; 11766 mblk_t *xmit_mp; 11767 tcp_stack_t *tcps = tcp->tcp_tcps; 11768 11769 ASSERT(tcp->tcp_sack_info != NULL); 11770 ASSERT(tcp->tcp_notsack_list != NULL); 11771 ASSERT(tcp->tcp_rexmit == B_FALSE); 11772 11773 /* Defensive coding in case there is a bug... */ 11774 if (tcp->tcp_notsack_list == NULL) { 11775 return; 11776 } 11777 notsack_blk = tcp->tcp_notsack_list; 11778 mss = tcp->tcp_mss; 11779 11780 /* 11781 * Limit the num of outstanding data in the network to be 11782 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11783 */ 11784 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11785 11786 /* At least retransmit 1 MSS of data. */ 11787 if (usable_swnd <= 0) { 11788 usable_swnd = mss; 11789 } 11790 11791 /* Make sure no new RTT samples will be taken. */ 11792 tcp->tcp_csuna = tcp->tcp_snxt; 11793 11794 notsack_blk = tcp->tcp_notsack_list; 11795 while (usable_swnd > 0) { 11796 mblk_t *snxt_mp, *tmp_mp; 11797 tcp_seq begin = tcp->tcp_sack_snxt; 11798 tcp_seq end; 11799 int32_t off; 11800 11801 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11802 if (SEQ_GT(notsack_blk->end, begin) && 11803 (notsack_blk->sack_cnt >= 11804 tcps->tcps_dupack_fast_retransmit)) { 11805 end = notsack_blk->end; 11806 if (SEQ_LT(begin, notsack_blk->begin)) { 11807 begin = notsack_blk->begin; 11808 } 11809 break; 11810 } 11811 } 11812 /* 11813 * All holes are filled. Manipulate tcp_cwnd to send more 11814 * if we can. Note that after the SACK recovery, tcp_cwnd is 11815 * set to tcp_cwnd_ssthresh. 11816 */ 11817 if (notsack_blk == NULL) { 11818 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11819 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11820 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11821 ASSERT(tcp->tcp_cwnd > 0); 11822 return; 11823 } else { 11824 usable_swnd = usable_swnd / mss; 11825 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11826 MAX(usable_swnd * mss, mss); 11827 *flags |= TH_XMIT_NEEDED; 11828 return; 11829 } 11830 } 11831 11832 /* 11833 * Note that we may send more than usable_swnd allows here 11834 * because of round off, but no more than 1 MSS of data. 11835 */ 11836 seg_len = end - begin; 11837 if (seg_len > mss) 11838 seg_len = mss; 11839 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11840 ASSERT(snxt_mp != NULL); 11841 /* This should not happen. Defensive coding again... */ 11842 if (snxt_mp == NULL) { 11843 return; 11844 } 11845 11846 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11847 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11848 if (xmit_mp == NULL) 11849 return; 11850 11851 usable_swnd -= seg_len; 11852 tcp->tcp_pipe += seg_len; 11853 tcp->tcp_sack_snxt = begin + seg_len; 11854 11855 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11856 11857 /* 11858 * Update the send timestamp to avoid false retransmission. 11859 */ 11860 snxt_mp->b_prev = (mblk_t *)lbolt; 11861 11862 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11863 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11864 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11865 /* 11866 * Update tcp_rexmit_max to extend this SACK recovery phase. 11867 * This happens when new data sent during fast recovery is 11868 * also lost. If TCP retransmits those new data, it needs 11869 * to extend SACK recover phase to avoid starting another 11870 * fast retransmit/recovery unnecessarily. 11871 */ 11872 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11873 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11874 } 11875 } 11876 } 11877 11878 /* 11879 * This function handles policy checking at TCP level for non-hard_bound/ 11880 * detached connections. 11881 */ 11882 static boolean_t 11883 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11884 boolean_t secure, boolean_t mctl_present) 11885 { 11886 ipsec_latch_t *ipl = NULL; 11887 ipsec_action_t *act = NULL; 11888 mblk_t *data_mp; 11889 ipsec_in_t *ii; 11890 const char *reason; 11891 kstat_named_t *counter; 11892 tcp_stack_t *tcps = tcp->tcp_tcps; 11893 ipsec_stack_t *ipss; 11894 ip_stack_t *ipst; 11895 11896 ASSERT(mctl_present || !secure); 11897 11898 ASSERT((ipha == NULL && ip6h != NULL) || 11899 (ip6h == NULL && ipha != NULL)); 11900 11901 /* 11902 * We don't necessarily have an ipsec_in_act action to verify 11903 * policy because of assymetrical policy where we have only 11904 * outbound policy and no inbound policy (possible with global 11905 * policy). 11906 */ 11907 if (!secure) { 11908 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11909 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11910 return (B_TRUE); 11911 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 11912 "tcp_check_policy", ipha, ip6h, secure, 11913 tcps->tcps_netstack); 11914 ipss = tcps->tcps_netstack->netstack_ipsec; 11915 11916 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11917 DROPPER(ipss, ipds_tcp_clear), 11918 &tcps->tcps_dropper); 11919 return (B_FALSE); 11920 } 11921 11922 /* 11923 * We have a secure packet. 11924 */ 11925 if (act == NULL) { 11926 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 11927 "tcp_check_policy", ipha, ip6h, secure, 11928 tcps->tcps_netstack); 11929 ipss = tcps->tcps_netstack->netstack_ipsec; 11930 11931 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11932 DROPPER(ipss, ipds_tcp_secure), 11933 &tcps->tcps_dropper); 11934 return (B_FALSE); 11935 } 11936 11937 /* 11938 * XXX This whole routine is currently incorrect. ipl should 11939 * be set to the latch pointer, but is currently not set, so 11940 * we initialize it to NULL to avoid picking up random garbage. 11941 */ 11942 if (ipl == NULL) 11943 return (B_TRUE); 11944 11945 data_mp = first_mp->b_cont; 11946 11947 ii = (ipsec_in_t *)first_mp->b_rptr; 11948 11949 ipst = tcps->tcps_netstack->netstack_ip; 11950 11951 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11952 &counter, tcp->tcp_connp)) { 11953 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 11954 return (B_TRUE); 11955 } 11956 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11957 "tcp inbound policy mismatch: %s, packet dropped\n", 11958 reason); 11959 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 11960 11961 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 11962 &tcps->tcps_dropper); 11963 return (B_FALSE); 11964 } 11965 11966 /* 11967 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11968 * retransmission after a timeout. 11969 * 11970 * To limit the number of duplicate segments, we limit the number of segment 11971 * to be sent in one time to tcp_snd_burst, the burst variable. 11972 */ 11973 static void 11974 tcp_ss_rexmit(tcp_t *tcp) 11975 { 11976 uint32_t snxt; 11977 uint32_t smax; 11978 int32_t win; 11979 int32_t mss; 11980 int32_t off; 11981 int32_t burst = tcp->tcp_snd_burst; 11982 mblk_t *snxt_mp; 11983 tcp_stack_t *tcps = tcp->tcp_tcps; 11984 11985 /* 11986 * Note that tcp_rexmit can be set even though TCP has retransmitted 11987 * all unack'ed segments. 11988 */ 11989 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11990 smax = tcp->tcp_rexmit_max; 11991 snxt = tcp->tcp_rexmit_nxt; 11992 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11993 snxt = tcp->tcp_suna; 11994 } 11995 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 11996 win -= snxt - tcp->tcp_suna; 11997 mss = tcp->tcp_mss; 11998 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 11999 12000 while (SEQ_LT(snxt, smax) && (win > 0) && 12001 (burst > 0) && (snxt_mp != NULL)) { 12002 mblk_t *xmit_mp; 12003 mblk_t *old_snxt_mp = snxt_mp; 12004 uint32_t cnt = mss; 12005 12006 if (win < cnt) { 12007 cnt = win; 12008 } 12009 if (SEQ_GT(snxt + cnt, smax)) { 12010 cnt = smax - snxt; 12011 } 12012 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12013 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12014 if (xmit_mp == NULL) 12015 return; 12016 12017 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12018 12019 snxt += cnt; 12020 win -= cnt; 12021 /* 12022 * Update the send timestamp to avoid false 12023 * retransmission. 12024 */ 12025 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12026 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12027 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12028 12029 tcp->tcp_rexmit_nxt = snxt; 12030 burst--; 12031 } 12032 /* 12033 * If we have transmitted all we have at the time 12034 * we started the retranmission, we can leave 12035 * the rest of the job to tcp_wput_data(). But we 12036 * need to check the send window first. If the 12037 * win is not 0, go on with tcp_wput_data(). 12038 */ 12039 if (SEQ_LT(snxt, smax) || win == 0) { 12040 return; 12041 } 12042 } 12043 /* Only call tcp_wput_data() if there is data to be sent. */ 12044 if (tcp->tcp_unsent) { 12045 tcp_wput_data(tcp, NULL, B_FALSE); 12046 } 12047 } 12048 12049 /* 12050 * Process all TCP option in SYN segment. Note that this function should 12051 * be called after tcp_adapt_ire() is called so that the necessary info 12052 * from IRE is already set in the tcp structure. 12053 * 12054 * This function sets up the correct tcp_mss value according to the 12055 * MSS option value and our header size. It also sets up the window scale 12056 * and timestamp values, and initialize SACK info blocks. But it does not 12057 * change receive window size after setting the tcp_mss value. The caller 12058 * should do the appropriate change. 12059 */ 12060 void 12061 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12062 { 12063 int options; 12064 tcp_opt_t tcpopt; 12065 uint32_t mss_max; 12066 char *tmp_tcph; 12067 tcp_stack_t *tcps = tcp->tcp_tcps; 12068 12069 tcpopt.tcp = NULL; 12070 options = tcp_parse_options(tcph, &tcpopt); 12071 12072 /* 12073 * Process MSS option. Note that MSS option value does not account 12074 * for IP or TCP options. This means that it is equal to MTU - minimum 12075 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12076 * IPv6. 12077 */ 12078 if (!(options & TCP_OPT_MSS_PRESENT)) { 12079 if (tcp->tcp_ipversion == IPV4_VERSION) 12080 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12081 else 12082 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12083 } else { 12084 if (tcp->tcp_ipversion == IPV4_VERSION) 12085 mss_max = tcps->tcps_mss_max_ipv4; 12086 else 12087 mss_max = tcps->tcps_mss_max_ipv6; 12088 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12089 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12090 else if (tcpopt.tcp_opt_mss > mss_max) 12091 tcpopt.tcp_opt_mss = mss_max; 12092 } 12093 12094 /* Process Window Scale option. */ 12095 if (options & TCP_OPT_WSCALE_PRESENT) { 12096 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12097 tcp->tcp_snd_ws_ok = B_TRUE; 12098 } else { 12099 tcp->tcp_snd_ws = B_FALSE; 12100 tcp->tcp_snd_ws_ok = B_FALSE; 12101 tcp->tcp_rcv_ws = B_FALSE; 12102 } 12103 12104 /* Process Timestamp option. */ 12105 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12106 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12107 tmp_tcph = (char *)tcp->tcp_tcph; 12108 12109 tcp->tcp_snd_ts_ok = B_TRUE; 12110 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12111 tcp->tcp_last_rcv_lbolt = lbolt64; 12112 ASSERT(OK_32PTR(tmp_tcph)); 12113 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12114 12115 /* Fill in our template header with basic timestamp option. */ 12116 tmp_tcph += tcp->tcp_tcp_hdr_len; 12117 tmp_tcph[0] = TCPOPT_NOP; 12118 tmp_tcph[1] = TCPOPT_NOP; 12119 tmp_tcph[2] = TCPOPT_TSTAMP; 12120 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12121 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12122 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12123 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12124 } else { 12125 tcp->tcp_snd_ts_ok = B_FALSE; 12126 } 12127 12128 /* 12129 * Process SACK options. If SACK is enabled for this connection, 12130 * then allocate the SACK info structure. Note the following ways 12131 * when tcp_snd_sack_ok is set to true. 12132 * 12133 * For active connection: in tcp_adapt_ire() called in 12134 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12135 * is checked. 12136 * 12137 * For passive connection: in tcp_adapt_ire() called in 12138 * tcp_accept_comm(). 12139 * 12140 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12141 * That check makes sure that if we did not send a SACK OK option, 12142 * we will not enable SACK for this connection even though the other 12143 * side sends us SACK OK option. For active connection, the SACK 12144 * info structure has already been allocated. So we need to free 12145 * it if SACK is disabled. 12146 */ 12147 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12148 (tcp->tcp_snd_sack_ok || 12149 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12150 /* This should be true only in the passive case. */ 12151 if (tcp->tcp_sack_info == NULL) { 12152 ASSERT(TCP_IS_DETACHED(tcp)); 12153 tcp->tcp_sack_info = 12154 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12155 } 12156 if (tcp->tcp_sack_info == NULL) { 12157 tcp->tcp_snd_sack_ok = B_FALSE; 12158 } else { 12159 tcp->tcp_snd_sack_ok = B_TRUE; 12160 if (tcp->tcp_snd_ts_ok) { 12161 tcp->tcp_max_sack_blk = 3; 12162 } else { 12163 tcp->tcp_max_sack_blk = 4; 12164 } 12165 } 12166 } else { 12167 /* 12168 * Resetting tcp_snd_sack_ok to B_FALSE so that 12169 * no SACK info will be used for this 12170 * connection. This assumes that SACK usage 12171 * permission is negotiated. This may need 12172 * to be changed once this is clarified. 12173 */ 12174 if (tcp->tcp_sack_info != NULL) { 12175 ASSERT(tcp->tcp_notsack_list == NULL); 12176 kmem_cache_free(tcp_sack_info_cache, 12177 tcp->tcp_sack_info); 12178 tcp->tcp_sack_info = NULL; 12179 } 12180 tcp->tcp_snd_sack_ok = B_FALSE; 12181 } 12182 12183 /* 12184 * Now we know the exact TCP/IP header length, subtract 12185 * that from tcp_mss to get our side's MSS. 12186 */ 12187 tcp->tcp_mss -= tcp->tcp_hdr_len; 12188 /* 12189 * Here we assume that the other side's header size will be equal to 12190 * our header size. We calculate the real MSS accordingly. Need to 12191 * take into additional stuffs IPsec puts in. 12192 * 12193 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12194 */ 12195 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12196 ((tcp->tcp_ipversion == IPV4_VERSION ? 12197 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12198 12199 /* 12200 * Set MSS to the smaller one of both ends of the connection. 12201 * We should not have called tcp_mss_set() before, but our 12202 * side of the MSS should have been set to a proper value 12203 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12204 * STREAM head parameters properly. 12205 * 12206 * If we have a larger-than-16-bit window but the other side 12207 * didn't want to do window scale, tcp_rwnd_set() will take 12208 * care of that. 12209 */ 12210 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12211 } 12212 12213 /* 12214 * Sends the T_CONN_IND to the listener. The caller calls this 12215 * functions via squeue to get inside the listener's perimeter 12216 * once the 3 way hand shake is done a T_CONN_IND needs to be 12217 * sent. As an optimization, the caller can call this directly 12218 * if listener's perimeter is same as eager's. 12219 */ 12220 /* ARGSUSED */ 12221 void 12222 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12223 { 12224 conn_t *lconnp = (conn_t *)arg; 12225 tcp_t *listener = lconnp->conn_tcp; 12226 tcp_t *tcp; 12227 struct T_conn_ind *conn_ind; 12228 ipaddr_t *addr_cache; 12229 boolean_t need_send_conn_ind = B_FALSE; 12230 tcp_stack_t *tcps = listener->tcp_tcps; 12231 12232 /* retrieve the eager */ 12233 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12234 ASSERT(conn_ind->OPT_offset != 0 && 12235 conn_ind->OPT_length == sizeof (intptr_t)); 12236 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12237 conn_ind->OPT_length); 12238 12239 /* 12240 * TLI/XTI applications will get confused by 12241 * sending eager as an option since it violates 12242 * the option semantics. So remove the eager as 12243 * option since TLI/XTI app doesn't need it anyway. 12244 */ 12245 if (!TCP_IS_SOCKET(listener)) { 12246 conn_ind->OPT_length = 0; 12247 conn_ind->OPT_offset = 0; 12248 } 12249 if (listener->tcp_state == TCPS_CLOSED || 12250 TCP_IS_DETACHED(listener)) { 12251 /* 12252 * If listener has closed, it would have caused a 12253 * a cleanup/blowoff to happen for the eager. We 12254 * just need to return. 12255 */ 12256 freemsg(mp); 12257 return; 12258 } 12259 12260 12261 /* 12262 * if the conn_req_q is full defer passing up the 12263 * T_CONN_IND until space is availabe after t_accept() 12264 * processing 12265 */ 12266 mutex_enter(&listener->tcp_eager_lock); 12267 12268 /* 12269 * Take the eager out, if it is in the list of droppable eagers 12270 * as we are here because the 3W handshake is over. 12271 */ 12272 MAKE_UNDROPPABLE(tcp); 12273 12274 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12275 tcp_t *tail; 12276 12277 /* 12278 * The eager already has an extra ref put in tcp_rput_data 12279 * so that it stays till accept comes back even though it 12280 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12281 */ 12282 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12283 listener->tcp_conn_req_cnt_q0--; 12284 listener->tcp_conn_req_cnt_q++; 12285 12286 /* Move from SYN_RCVD to ESTABLISHED list */ 12287 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12288 tcp->tcp_eager_prev_q0; 12289 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12290 tcp->tcp_eager_next_q0; 12291 tcp->tcp_eager_prev_q0 = NULL; 12292 tcp->tcp_eager_next_q0 = NULL; 12293 12294 /* 12295 * Insert at end of the queue because sockfs 12296 * sends down T_CONN_RES in chronological 12297 * order. Leaving the older conn indications 12298 * at front of the queue helps reducing search 12299 * time. 12300 */ 12301 tail = listener->tcp_eager_last_q; 12302 if (tail != NULL) 12303 tail->tcp_eager_next_q = tcp; 12304 else 12305 listener->tcp_eager_next_q = tcp; 12306 listener->tcp_eager_last_q = tcp; 12307 tcp->tcp_eager_next_q = NULL; 12308 /* 12309 * Delay sending up the T_conn_ind until we are 12310 * done with the eager. Once we have have sent up 12311 * the T_conn_ind, the accept can potentially complete 12312 * any time and release the refhold we have on the eager. 12313 */ 12314 need_send_conn_ind = B_TRUE; 12315 } else { 12316 /* 12317 * Defer connection on q0 and set deferred 12318 * connection bit true 12319 */ 12320 tcp->tcp_conn_def_q0 = B_TRUE; 12321 12322 /* take tcp out of q0 ... */ 12323 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12324 tcp->tcp_eager_next_q0; 12325 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12326 tcp->tcp_eager_prev_q0; 12327 12328 /* ... and place it at the end of q0 */ 12329 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12330 tcp->tcp_eager_next_q0 = listener; 12331 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12332 listener->tcp_eager_prev_q0 = tcp; 12333 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12334 } 12335 12336 /* we have timed out before */ 12337 if (tcp->tcp_syn_rcvd_timeout != 0) { 12338 tcp->tcp_syn_rcvd_timeout = 0; 12339 listener->tcp_syn_rcvd_timeout--; 12340 if (listener->tcp_syn_defense && 12341 listener->tcp_syn_rcvd_timeout <= 12342 (tcps->tcps_conn_req_max_q0 >> 5) && 12343 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12344 listener->tcp_last_rcv_lbolt)) { 12345 /* 12346 * Turn off the defense mode if we 12347 * believe the SYN attack is over. 12348 */ 12349 listener->tcp_syn_defense = B_FALSE; 12350 if (listener->tcp_ip_addr_cache) { 12351 kmem_free((void *)listener->tcp_ip_addr_cache, 12352 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12353 listener->tcp_ip_addr_cache = NULL; 12354 } 12355 } 12356 } 12357 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12358 if (addr_cache != NULL) { 12359 /* 12360 * We have finished a 3-way handshake with this 12361 * remote host. This proves the IP addr is good. 12362 * Cache it! 12363 */ 12364 addr_cache[IP_ADDR_CACHE_HASH( 12365 tcp->tcp_remote)] = tcp->tcp_remote; 12366 } 12367 mutex_exit(&listener->tcp_eager_lock); 12368 if (need_send_conn_ind) 12369 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12370 } 12371 12372 /* 12373 * Send the newconn notification to ulp. The eager is blown off if the 12374 * notification fails. 12375 */ 12376 static void 12377 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12378 { 12379 if (IPCL_IS_NONSTR(lconnp)) { 12380 cred_t *cr; 12381 pid_t cpid; 12382 12383 cr = msg_getcred(mp, &cpid); 12384 12385 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12386 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12387 lconnp->conn_tcp); 12388 12389 /* Keep the message around in case of a fallback to TPI */ 12390 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12391 12392 /* 12393 * Notify the ULP about the newconn. It is guaranteed that no 12394 * tcp_accept() call will be made for the eager if the 12395 * notification fails, so it's safe to blow it off in that 12396 * case. 12397 * 12398 * The upper handle will be assigned when tcp_accept() is 12399 * called. 12400 */ 12401 if ((*lconnp->conn_upcalls->su_newconn) 12402 (lconnp->conn_upper_handle, 12403 (sock_lower_handle_t)econnp, 12404 &sock_tcp_downcalls, cr, cpid, 12405 &econnp->conn_upcalls) == NULL) { 12406 /* Failed to allocate a socket */ 12407 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12408 tcpEstabResets); 12409 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12410 econnp->conn_tcp->tcp_conn_req_seqnum); 12411 } 12412 } else { 12413 putnext(lconnp->conn_tcp->tcp_rq, mp); 12414 } 12415 } 12416 12417 mblk_t * 12418 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12419 uint_t *ifindexp, ip6_pkt_t *ippp) 12420 { 12421 ip_pktinfo_t *pinfo; 12422 ip6_t *ip6h; 12423 uchar_t *rptr; 12424 mblk_t *first_mp = mp; 12425 boolean_t mctl_present = B_FALSE; 12426 uint_t ifindex = 0; 12427 ip6_pkt_t ipp; 12428 uint_t ipvers; 12429 uint_t ip_hdr_len; 12430 tcp_stack_t *tcps = tcp->tcp_tcps; 12431 12432 rptr = mp->b_rptr; 12433 ASSERT(OK_32PTR(rptr)); 12434 ASSERT(tcp != NULL); 12435 ipp.ipp_fields = 0; 12436 12437 switch DB_TYPE(mp) { 12438 case M_CTL: 12439 mp = mp->b_cont; 12440 if (mp == NULL) { 12441 freemsg(first_mp); 12442 return (NULL); 12443 } 12444 if (DB_TYPE(mp) != M_DATA) { 12445 freemsg(first_mp); 12446 return (NULL); 12447 } 12448 mctl_present = B_TRUE; 12449 break; 12450 case M_DATA: 12451 break; 12452 default: 12453 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12454 freemsg(mp); 12455 return (NULL); 12456 } 12457 ipvers = IPH_HDR_VERSION(rptr); 12458 if (ipvers == IPV4_VERSION) { 12459 if (tcp == NULL) { 12460 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12461 goto done; 12462 } 12463 12464 ipp.ipp_fields |= IPPF_HOPLIMIT; 12465 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12466 12467 /* 12468 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12469 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12470 */ 12471 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12472 mctl_present) { 12473 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12474 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12475 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12476 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12477 ipp.ipp_fields |= IPPF_IFINDEX; 12478 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12479 ifindex = pinfo->ip_pkt_ifindex; 12480 } 12481 freeb(first_mp); 12482 mctl_present = B_FALSE; 12483 } 12484 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12485 } else { 12486 ip6h = (ip6_t *)rptr; 12487 12488 ASSERT(ipvers == IPV6_VERSION); 12489 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12490 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12491 ipp.ipp_hoplimit = ip6h->ip6_hops; 12492 12493 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12494 uint8_t nexthdrp; 12495 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12496 12497 /* Look for ifindex information */ 12498 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12499 ip6i_t *ip6i = (ip6i_t *)ip6h; 12500 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12501 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12502 freemsg(first_mp); 12503 return (NULL); 12504 } 12505 12506 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12507 ASSERT(ip6i->ip6i_ifindex != 0); 12508 ipp.ipp_fields |= IPPF_IFINDEX; 12509 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12510 ifindex = ip6i->ip6i_ifindex; 12511 } 12512 rptr = (uchar_t *)&ip6i[1]; 12513 mp->b_rptr = rptr; 12514 if (rptr == mp->b_wptr) { 12515 mblk_t *mp1; 12516 mp1 = mp->b_cont; 12517 freeb(mp); 12518 mp = mp1; 12519 rptr = mp->b_rptr; 12520 } 12521 if (MBLKL(mp) < IPV6_HDR_LEN + 12522 sizeof (tcph_t)) { 12523 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12524 freemsg(first_mp); 12525 return (NULL); 12526 } 12527 ip6h = (ip6_t *)rptr; 12528 } 12529 12530 /* 12531 * Find any potentially interesting extension headers 12532 * as well as the length of the IPv6 + extension 12533 * headers. 12534 */ 12535 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12536 /* Verify if this is a TCP packet */ 12537 if (nexthdrp != IPPROTO_TCP) { 12538 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12539 freemsg(first_mp); 12540 return (NULL); 12541 } 12542 } else { 12543 ip_hdr_len = IPV6_HDR_LEN; 12544 } 12545 } 12546 12547 done: 12548 if (ipversp != NULL) 12549 *ipversp = ipvers; 12550 if (ip_hdr_lenp != NULL) 12551 *ip_hdr_lenp = ip_hdr_len; 12552 if (ippp != NULL) 12553 *ippp = ipp; 12554 if (ifindexp != NULL) 12555 *ifindexp = ifindex; 12556 if (mctl_present) { 12557 freeb(first_mp); 12558 } 12559 return (mp); 12560 } 12561 12562 /* 12563 * Handle M_DATA messages from IP. Its called directly from IP via 12564 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12565 * in this path. 12566 * 12567 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12568 * v4 and v6), we are called through tcp_input() and a M_CTL can 12569 * be present for options but tcp_find_pktinfo() deals with it. We 12570 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12571 * 12572 * The first argument is always the connp/tcp to which the mp belongs. 12573 * There are no exceptions to this rule. The caller has already put 12574 * a reference on this connp/tcp and once tcp_rput_data() returns, 12575 * the squeue will do the refrele. 12576 * 12577 * The TH_SYN for the listener directly go to tcp_conn_request via 12578 * squeue. 12579 * 12580 * sqp: NULL = recursive, sqp != NULL means called from squeue 12581 */ 12582 void 12583 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12584 { 12585 int32_t bytes_acked; 12586 int32_t gap; 12587 mblk_t *mp1; 12588 uint_t flags; 12589 uint32_t new_swnd = 0; 12590 uchar_t *iphdr; 12591 uchar_t *rptr; 12592 int32_t rgap; 12593 uint32_t seg_ack; 12594 int seg_len; 12595 uint_t ip_hdr_len; 12596 uint32_t seg_seq; 12597 tcph_t *tcph; 12598 int urp; 12599 tcp_opt_t tcpopt; 12600 uint_t ipvers; 12601 ip6_pkt_t ipp; 12602 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12603 uint32_t cwnd; 12604 uint32_t add; 12605 int npkt; 12606 int mss; 12607 conn_t *connp = (conn_t *)arg; 12608 squeue_t *sqp = (squeue_t *)arg2; 12609 tcp_t *tcp = connp->conn_tcp; 12610 tcp_stack_t *tcps = tcp->tcp_tcps; 12611 12612 /* 12613 * RST from fused tcp loopback peer should trigger an unfuse. 12614 */ 12615 if (tcp->tcp_fused) { 12616 TCP_STAT(tcps, tcp_fusion_aborted); 12617 tcp_unfuse(tcp); 12618 } 12619 12620 iphdr = mp->b_rptr; 12621 rptr = mp->b_rptr; 12622 ASSERT(OK_32PTR(rptr)); 12623 12624 /* 12625 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12626 * processing here. For rest call tcp_find_pktinfo to fill up the 12627 * necessary information. 12628 */ 12629 if (IPCL_IS_TCP4(connp)) { 12630 ipvers = IPV4_VERSION; 12631 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12632 } else { 12633 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12634 NULL, &ipp); 12635 if (mp == NULL) { 12636 TCP_STAT(tcps, tcp_rput_v6_error); 12637 return; 12638 } 12639 iphdr = mp->b_rptr; 12640 rptr = mp->b_rptr; 12641 } 12642 ASSERT(DB_TYPE(mp) == M_DATA); 12643 ASSERT(mp->b_next == NULL); 12644 12645 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12646 seg_seq = ABE32_TO_U32(tcph->th_seq); 12647 seg_ack = ABE32_TO_U32(tcph->th_ack); 12648 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12649 seg_len = (int)(mp->b_wptr - rptr) - 12650 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12651 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12652 do { 12653 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12654 (uintptr_t)INT_MAX); 12655 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12656 } while ((mp1 = mp1->b_cont) != NULL && 12657 mp1->b_datap->db_type == M_DATA); 12658 } 12659 12660 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12661 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12662 seg_len, tcph); 12663 return; 12664 } 12665 12666 if (sqp != NULL) { 12667 /* 12668 * This is the correct place to update tcp_last_recv_time. Note 12669 * that it is also updated for tcp structure that belongs to 12670 * global and listener queues which do not really need updating. 12671 * But that should not cause any harm. And it is updated for 12672 * all kinds of incoming segments, not only for data segments. 12673 */ 12674 tcp->tcp_last_recv_time = lbolt; 12675 } 12676 12677 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12678 12679 BUMP_LOCAL(tcp->tcp_ibsegs); 12680 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12681 12682 if ((flags & TH_URG) && sqp != NULL) { 12683 /* 12684 * TCP can't handle urgent pointers that arrive before 12685 * the connection has been accept()ed since it can't 12686 * buffer OOB data. Discard segment if this happens. 12687 * 12688 * We can't just rely on a non-null tcp_listener to indicate 12689 * that the accept() has completed since unlinking of the 12690 * eager and completion of the accept are not atomic. 12691 * tcp_detached, when it is not set (B_FALSE) indicates 12692 * that the accept() has completed. 12693 * 12694 * Nor can it reassemble urgent pointers, so discard 12695 * if it's not the next segment expected. 12696 * 12697 * Otherwise, collapse chain into one mblk (discard if 12698 * that fails). This makes sure the headers, retransmitted 12699 * data, and new data all are in the same mblk. 12700 */ 12701 ASSERT(mp != NULL); 12702 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12703 freemsg(mp); 12704 return; 12705 } 12706 /* Update pointers into message */ 12707 iphdr = rptr = mp->b_rptr; 12708 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12709 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12710 /* 12711 * Since we can't handle any data with this urgent 12712 * pointer that is out of sequence, we expunge 12713 * the data. This allows us to still register 12714 * the urgent mark and generate the M_PCSIG, 12715 * which we can do. 12716 */ 12717 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12718 seg_len = 0; 12719 } 12720 } 12721 12722 switch (tcp->tcp_state) { 12723 case TCPS_SYN_SENT: 12724 if (flags & TH_ACK) { 12725 /* 12726 * Note that our stack cannot send data before a 12727 * connection is established, therefore the 12728 * following check is valid. Otherwise, it has 12729 * to be changed. 12730 */ 12731 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12732 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12733 freemsg(mp); 12734 if (flags & TH_RST) 12735 return; 12736 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12737 tcp, seg_ack, 0, TH_RST); 12738 return; 12739 } 12740 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12741 } 12742 if (flags & TH_RST) { 12743 freemsg(mp); 12744 if (flags & TH_ACK) 12745 (void) tcp_clean_death(tcp, 12746 ECONNREFUSED, 13); 12747 return; 12748 } 12749 if (!(flags & TH_SYN)) { 12750 freemsg(mp); 12751 return; 12752 } 12753 12754 /* Process all TCP options. */ 12755 tcp_process_options(tcp, tcph); 12756 /* 12757 * The following changes our rwnd to be a multiple of the 12758 * MIN(peer MSS, our MSS) for performance reason. 12759 */ 12760 (void) tcp_rwnd_set(tcp, 12761 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12762 12763 /* Is the other end ECN capable? */ 12764 if (tcp->tcp_ecn_ok) { 12765 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12766 tcp->tcp_ecn_ok = B_FALSE; 12767 } 12768 } 12769 /* 12770 * Clear ECN flags because it may interfere with later 12771 * processing. 12772 */ 12773 flags &= ~(TH_ECE|TH_CWR); 12774 12775 tcp->tcp_irs = seg_seq; 12776 tcp->tcp_rack = seg_seq; 12777 tcp->tcp_rnxt = seg_seq + 1; 12778 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12779 if (!TCP_IS_DETACHED(tcp)) { 12780 /* Allocate room for SACK options if needed. */ 12781 if (tcp->tcp_snd_sack_ok) { 12782 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12783 tcp->tcp_hdr_len + 12784 TCPOPT_MAX_SACK_LEN + 12785 (tcp->tcp_loopback ? 0 : 12786 tcps->tcps_wroff_xtra)); 12787 } else { 12788 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12789 tcp->tcp_hdr_len + 12790 (tcp->tcp_loopback ? 0 : 12791 tcps->tcps_wroff_xtra)); 12792 } 12793 } 12794 if (flags & TH_ACK) { 12795 /* 12796 * If we can't get the confirmation upstream, pretend 12797 * we didn't even see this one. 12798 * 12799 * XXX: how can we pretend we didn't see it if we 12800 * have updated rnxt et. al. 12801 * 12802 * For loopback we defer sending up the T_CONN_CON 12803 * until after some checks below. 12804 */ 12805 mp1 = NULL; 12806 /* 12807 * tcp_sendmsg() checks tcp_state without entering 12808 * the squeue so tcp_state should be updated before 12809 * sending up connection confirmation 12810 */ 12811 tcp->tcp_state = TCPS_ESTABLISHED; 12812 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12813 tcp->tcp_loopback ? &mp1 : NULL)) { 12814 tcp->tcp_state = TCPS_SYN_SENT; 12815 freemsg(mp); 12816 return; 12817 } 12818 /* SYN was acked - making progress */ 12819 if (tcp->tcp_ipversion == IPV6_VERSION) 12820 tcp->tcp_ip_forward_progress = B_TRUE; 12821 12822 /* One for the SYN */ 12823 tcp->tcp_suna = tcp->tcp_iss + 1; 12824 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12825 12826 /* 12827 * If SYN was retransmitted, need to reset all 12828 * retransmission info. This is because this 12829 * segment will be treated as a dup ACK. 12830 */ 12831 if (tcp->tcp_rexmit) { 12832 tcp->tcp_rexmit = B_FALSE; 12833 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12834 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12835 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12836 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12837 tcp->tcp_ms_we_have_waited = 0; 12838 12839 /* 12840 * Set tcp_cwnd back to 1 MSS, per 12841 * recommendation from 12842 * draft-floyd-incr-init-win-01.txt, 12843 * Increasing TCP's Initial Window. 12844 */ 12845 tcp->tcp_cwnd = tcp->tcp_mss; 12846 } 12847 12848 tcp->tcp_swl1 = seg_seq; 12849 tcp->tcp_swl2 = seg_ack; 12850 12851 new_swnd = BE16_TO_U16(tcph->th_win); 12852 tcp->tcp_swnd = new_swnd; 12853 if (new_swnd > tcp->tcp_max_swnd) 12854 tcp->tcp_max_swnd = new_swnd; 12855 12856 /* 12857 * Always send the three-way handshake ack immediately 12858 * in order to make the connection complete as soon as 12859 * possible on the accepting host. 12860 */ 12861 flags |= TH_ACK_NEEDED; 12862 12863 /* 12864 * Special case for loopback. At this point we have 12865 * received SYN-ACK from the remote endpoint. In 12866 * order to ensure that both endpoints reach the 12867 * fused state prior to any data exchange, the final 12868 * ACK needs to be sent before we indicate T_CONN_CON 12869 * to the module upstream. 12870 */ 12871 if (tcp->tcp_loopback) { 12872 mblk_t *ack_mp; 12873 12874 ASSERT(!tcp->tcp_unfusable); 12875 ASSERT(mp1 != NULL); 12876 /* 12877 * For loopback, we always get a pure SYN-ACK 12878 * and only need to send back the final ACK 12879 * with no data (this is because the other 12880 * tcp is ours and we don't do T/TCP). This 12881 * final ACK triggers the passive side to 12882 * perform fusion in ESTABLISHED state. 12883 */ 12884 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12885 if (tcp->tcp_ack_tid != 0) { 12886 (void) TCP_TIMER_CANCEL(tcp, 12887 tcp->tcp_ack_tid); 12888 tcp->tcp_ack_tid = 0; 12889 } 12890 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12891 BUMP_LOCAL(tcp->tcp_obsegs); 12892 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 12893 12894 if (!IPCL_IS_NONSTR(connp)) { 12895 /* Send up T_CONN_CON */ 12896 putnext(tcp->tcp_rq, mp1); 12897 } else { 12898 cred_t *cr; 12899 pid_t cpid; 12900 12901 cr = msg_getcred(mp1, &cpid); 12902 (*connp->conn_upcalls-> 12903 su_connected) 12904 (connp->conn_upper_handle, 12905 tcp->tcp_connid, cr, cpid); 12906 freemsg(mp1); 12907 } 12908 12909 freemsg(mp); 12910 return; 12911 } 12912 /* 12913 * Forget fusion; we need to handle more 12914 * complex cases below. Send the deferred 12915 * T_CONN_CON message upstream and proceed 12916 * as usual. Mark this tcp as not capable 12917 * of fusion. 12918 */ 12919 TCP_STAT(tcps, tcp_fusion_unfusable); 12920 tcp->tcp_unfusable = B_TRUE; 12921 if (!IPCL_IS_NONSTR(connp)) { 12922 putnext(tcp->tcp_rq, mp1); 12923 } else { 12924 cred_t *cr; 12925 pid_t cpid; 12926 12927 cr = msg_getcred(mp1, &cpid); 12928 (*connp->conn_upcalls->su_connected) 12929 (connp->conn_upper_handle, 12930 tcp->tcp_connid, cr, cpid); 12931 freemsg(mp1); 12932 } 12933 } 12934 12935 /* 12936 * Check to see if there is data to be sent. If 12937 * yes, set the transmit flag. Then check to see 12938 * if received data processing needs to be done. 12939 * If not, go straight to xmit_check. This short 12940 * cut is OK as we don't support T/TCP. 12941 */ 12942 if (tcp->tcp_unsent) 12943 flags |= TH_XMIT_NEEDED; 12944 12945 if (seg_len == 0 && !(flags & TH_URG)) { 12946 freemsg(mp); 12947 goto xmit_check; 12948 } 12949 12950 flags &= ~TH_SYN; 12951 seg_seq++; 12952 break; 12953 } 12954 tcp->tcp_state = TCPS_SYN_RCVD; 12955 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12956 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12957 if (mp1) { 12958 /* 12959 * See comment in tcp_conn_request() for why we use 12960 * the open() time pid here. 12961 */ 12962 DB_CPID(mp1) = tcp->tcp_cpid; 12963 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12964 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12965 } 12966 freemsg(mp); 12967 return; 12968 case TCPS_SYN_RCVD: 12969 if (flags & TH_ACK) { 12970 /* 12971 * In this state, a SYN|ACK packet is either bogus 12972 * because the other side must be ACKing our SYN which 12973 * indicates it has seen the ACK for their SYN and 12974 * shouldn't retransmit it or we're crossing SYNs 12975 * on active open. 12976 */ 12977 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12978 freemsg(mp); 12979 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12980 tcp, seg_ack, 0, TH_RST); 12981 return; 12982 } 12983 /* 12984 * NOTE: RFC 793 pg. 72 says this should be 12985 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12986 * but that would mean we have an ack that ignored 12987 * our SYN. 12988 */ 12989 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12990 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12991 freemsg(mp); 12992 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12993 tcp, seg_ack, 0, TH_RST); 12994 return; 12995 } 12996 } 12997 break; 12998 case TCPS_LISTEN: 12999 /* 13000 * Only a TLI listener can come through this path when a 13001 * acceptor is going back to be a listener and a packet 13002 * for the acceptor hits the classifier. For a socket 13003 * listener, this can never happen because a listener 13004 * can never accept connection on itself and hence a 13005 * socket acceptor can not go back to being a listener. 13006 */ 13007 ASSERT(!TCP_IS_SOCKET(tcp)); 13008 /*FALLTHRU*/ 13009 case TCPS_CLOSED: 13010 case TCPS_BOUND: { 13011 conn_t *new_connp; 13012 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13013 13014 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13015 if (new_connp != NULL) { 13016 tcp_reinput(new_connp, mp, connp->conn_sqp); 13017 return; 13018 } 13019 /* We failed to classify. For now just drop the packet */ 13020 freemsg(mp); 13021 return; 13022 } 13023 case TCPS_IDLE: 13024 /* 13025 * Handle the case where the tcp_clean_death() has happened 13026 * on a connection (application hasn't closed yet) but a packet 13027 * was already queued on squeue before tcp_clean_death() 13028 * was processed. Calling tcp_clean_death() twice on same 13029 * connection can result in weird behaviour. 13030 */ 13031 freemsg(mp); 13032 return; 13033 default: 13034 break; 13035 } 13036 13037 /* 13038 * Already on the correct queue/perimeter. 13039 * If this is a detached connection and not an eager 13040 * connection hanging off a listener then new data 13041 * (past the FIN) will cause a reset. 13042 * We do a special check here where it 13043 * is out of the main line, rather than check 13044 * if we are detached every time we see new 13045 * data down below. 13046 */ 13047 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13048 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13049 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13050 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13051 13052 freemsg(mp); 13053 /* 13054 * This could be an SSL closure alert. We're detached so just 13055 * acknowledge it this last time. 13056 */ 13057 if (tcp->tcp_kssl_ctx != NULL) { 13058 kssl_release_ctx(tcp->tcp_kssl_ctx); 13059 tcp->tcp_kssl_ctx = NULL; 13060 13061 tcp->tcp_rnxt += seg_len; 13062 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13063 flags |= TH_ACK_NEEDED; 13064 goto ack_check; 13065 } 13066 13067 tcp_xmit_ctl("new data when detached", tcp, 13068 tcp->tcp_snxt, 0, TH_RST); 13069 (void) tcp_clean_death(tcp, EPROTO, 12); 13070 return; 13071 } 13072 13073 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13074 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13075 new_swnd = BE16_TO_U16(tcph->th_win) << 13076 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13077 13078 if (tcp->tcp_snd_ts_ok) { 13079 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13080 /* 13081 * This segment is not acceptable. 13082 * Drop it and send back an ACK. 13083 */ 13084 freemsg(mp); 13085 flags |= TH_ACK_NEEDED; 13086 goto ack_check; 13087 } 13088 } else if (tcp->tcp_snd_sack_ok) { 13089 ASSERT(tcp->tcp_sack_info != NULL); 13090 tcpopt.tcp = tcp; 13091 /* 13092 * SACK info in already updated in tcp_parse_options. Ignore 13093 * all other TCP options... 13094 */ 13095 (void) tcp_parse_options(tcph, &tcpopt); 13096 } 13097 try_again:; 13098 mss = tcp->tcp_mss; 13099 gap = seg_seq - tcp->tcp_rnxt; 13100 rgap = tcp->tcp_rwnd - (gap + seg_len); 13101 /* 13102 * gap is the amount of sequence space between what we expect to see 13103 * and what we got for seg_seq. A positive value for gap means 13104 * something got lost. A negative value means we got some old stuff. 13105 */ 13106 if (gap < 0) { 13107 /* Old stuff present. Is the SYN in there? */ 13108 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13109 (seg_len != 0)) { 13110 flags &= ~TH_SYN; 13111 seg_seq++; 13112 urp--; 13113 /* Recompute the gaps after noting the SYN. */ 13114 goto try_again; 13115 } 13116 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13117 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13118 (seg_len > -gap ? -gap : seg_len)); 13119 /* Remove the old stuff from seg_len. */ 13120 seg_len += gap; 13121 /* 13122 * Anything left? 13123 * Make sure to check for unack'd FIN when rest of data 13124 * has been previously ack'd. 13125 */ 13126 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13127 /* 13128 * Resets are only valid if they lie within our offered 13129 * window. If the RST bit is set, we just ignore this 13130 * segment. 13131 */ 13132 if (flags & TH_RST) { 13133 freemsg(mp); 13134 return; 13135 } 13136 13137 /* 13138 * The arriving of dup data packets indicate that we 13139 * may have postponed an ack for too long, or the other 13140 * side's RTT estimate is out of shape. Start acking 13141 * more often. 13142 */ 13143 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13144 tcp->tcp_rack_cnt >= 1 && 13145 tcp->tcp_rack_abs_max > 2) { 13146 tcp->tcp_rack_abs_max--; 13147 } 13148 tcp->tcp_rack_cur_max = 1; 13149 13150 /* 13151 * This segment is "unacceptable". None of its 13152 * sequence space lies within our advertized window. 13153 * 13154 * Adjust seg_len to the original value for tracing. 13155 */ 13156 seg_len -= gap; 13157 if (tcp->tcp_debug) { 13158 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13159 "tcp_rput: unacceptable, gap %d, rgap %d, " 13160 "flags 0x%x, seg_seq %u, seg_ack %u, " 13161 "seg_len %d, rnxt %u, snxt %u, %s", 13162 gap, rgap, flags, seg_seq, seg_ack, 13163 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13164 tcp_display(tcp, NULL, 13165 DISP_ADDR_AND_PORT)); 13166 } 13167 13168 /* 13169 * Arrange to send an ACK in response to the 13170 * unacceptable segment per RFC 793 page 69. There 13171 * is only one small difference between ours and the 13172 * acceptability test in the RFC - we accept ACK-only 13173 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13174 * will be generated. 13175 * 13176 * Note that we have to ACK an ACK-only packet at least 13177 * for stacks that send 0-length keep-alives with 13178 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13179 * section 4.2.3.6. As long as we don't ever generate 13180 * an unacceptable packet in response to an incoming 13181 * packet that is unacceptable, it should not cause 13182 * "ACK wars". 13183 */ 13184 flags |= TH_ACK_NEEDED; 13185 13186 /* 13187 * Continue processing this segment in order to use the 13188 * ACK information it contains, but skip all other 13189 * sequence-number processing. Processing the ACK 13190 * information is necessary in order to 13191 * re-synchronize connections that may have lost 13192 * synchronization. 13193 * 13194 * We clear seg_len and flag fields related to 13195 * sequence number processing as they are not 13196 * to be trusted for an unacceptable segment. 13197 */ 13198 seg_len = 0; 13199 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13200 goto process_ack; 13201 } 13202 13203 /* Fix seg_seq, and chew the gap off the front. */ 13204 seg_seq = tcp->tcp_rnxt; 13205 urp += gap; 13206 do { 13207 mblk_t *mp2; 13208 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13209 (uintptr_t)UINT_MAX); 13210 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13211 if (gap > 0) { 13212 mp->b_rptr = mp->b_wptr - gap; 13213 break; 13214 } 13215 mp2 = mp; 13216 mp = mp->b_cont; 13217 freeb(mp2); 13218 } while (gap < 0); 13219 /* 13220 * If the urgent data has already been acknowledged, we 13221 * should ignore TH_URG below 13222 */ 13223 if (urp < 0) 13224 flags &= ~TH_URG; 13225 } 13226 /* 13227 * rgap is the amount of stuff received out of window. A negative 13228 * value is the amount out of window. 13229 */ 13230 if (rgap < 0) { 13231 mblk_t *mp2; 13232 13233 if (tcp->tcp_rwnd == 0) { 13234 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13235 } else { 13236 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13237 UPDATE_MIB(&tcps->tcps_mib, 13238 tcpInDataPastWinBytes, -rgap); 13239 } 13240 13241 /* 13242 * seg_len does not include the FIN, so if more than 13243 * just the FIN is out of window, we act like we don't 13244 * see it. (If just the FIN is out of window, rgap 13245 * will be zero and we will go ahead and acknowledge 13246 * the FIN.) 13247 */ 13248 flags &= ~TH_FIN; 13249 13250 /* Fix seg_len and make sure there is something left. */ 13251 seg_len += rgap; 13252 if (seg_len <= 0) { 13253 /* 13254 * Resets are only valid if they lie within our offered 13255 * window. If the RST bit is set, we just ignore this 13256 * segment. 13257 */ 13258 if (flags & TH_RST) { 13259 freemsg(mp); 13260 return; 13261 } 13262 13263 /* Per RFC 793, we need to send back an ACK. */ 13264 flags |= TH_ACK_NEEDED; 13265 13266 /* 13267 * Send SIGURG as soon as possible i.e. even 13268 * if the TH_URG was delivered in a window probe 13269 * packet (which will be unacceptable). 13270 * 13271 * We generate a signal if none has been generated 13272 * for this connection or if this is a new urgent 13273 * byte. Also send a zero-length "unmarked" message 13274 * to inform SIOCATMARK that this is not the mark. 13275 * 13276 * tcp_urp_last_valid is cleared when the T_exdata_ind 13277 * is sent up. This plus the check for old data 13278 * (gap >= 0) handles the wraparound of the sequence 13279 * number space without having to always track the 13280 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13281 * this max in its rcv_up variable). 13282 * 13283 * This prevents duplicate SIGURGS due to a "late" 13284 * zero-window probe when the T_EXDATA_IND has already 13285 * been sent up. 13286 */ 13287 if ((flags & TH_URG) && 13288 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13289 tcp->tcp_urp_last))) { 13290 if (IPCL_IS_NONSTR(connp)) { 13291 if (!TCP_IS_DETACHED(tcp)) { 13292 (*connp->conn_upcalls-> 13293 su_signal_oob) 13294 (connp->conn_upper_handle, 13295 urp); 13296 } 13297 } else { 13298 mp1 = allocb(0, BPRI_MED); 13299 if (mp1 == NULL) { 13300 freemsg(mp); 13301 return; 13302 } 13303 if (!TCP_IS_DETACHED(tcp) && 13304 !putnextctl1(tcp->tcp_rq, 13305 M_PCSIG, SIGURG)) { 13306 /* Try again on the rexmit. */ 13307 freemsg(mp1); 13308 freemsg(mp); 13309 return; 13310 } 13311 /* 13312 * If the next byte would be the mark 13313 * then mark with MARKNEXT else mark 13314 * with NOTMARKNEXT. 13315 */ 13316 if (gap == 0 && urp == 0) 13317 mp1->b_flag |= MSGMARKNEXT; 13318 else 13319 mp1->b_flag |= MSGNOTMARKNEXT; 13320 freemsg(tcp->tcp_urp_mark_mp); 13321 tcp->tcp_urp_mark_mp = mp1; 13322 flags |= TH_SEND_URP_MARK; 13323 } 13324 tcp->tcp_urp_last_valid = B_TRUE; 13325 tcp->tcp_urp_last = urp + seg_seq; 13326 } 13327 /* 13328 * If this is a zero window probe, continue to 13329 * process the ACK part. But we need to set seg_len 13330 * to 0 to avoid data processing. Otherwise just 13331 * drop the segment and send back an ACK. 13332 */ 13333 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13334 flags &= ~(TH_SYN | TH_URG); 13335 seg_len = 0; 13336 goto process_ack; 13337 } else { 13338 freemsg(mp); 13339 goto ack_check; 13340 } 13341 } 13342 /* Pitch out of window stuff off the end. */ 13343 rgap = seg_len; 13344 mp2 = mp; 13345 do { 13346 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13347 (uintptr_t)INT_MAX); 13348 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13349 if (rgap < 0) { 13350 mp2->b_wptr += rgap; 13351 if ((mp1 = mp2->b_cont) != NULL) { 13352 mp2->b_cont = NULL; 13353 freemsg(mp1); 13354 } 13355 break; 13356 } 13357 } while ((mp2 = mp2->b_cont) != NULL); 13358 } 13359 ok:; 13360 /* 13361 * TCP should check ECN info for segments inside the window only. 13362 * Therefore the check should be done here. 13363 */ 13364 if (tcp->tcp_ecn_ok) { 13365 if (flags & TH_CWR) { 13366 tcp->tcp_ecn_echo_on = B_FALSE; 13367 } 13368 /* 13369 * Note that both ECN_CE and CWR can be set in the 13370 * same segment. In this case, we once again turn 13371 * on ECN_ECHO. 13372 */ 13373 if (tcp->tcp_ipversion == IPV4_VERSION) { 13374 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13375 13376 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13377 tcp->tcp_ecn_echo_on = B_TRUE; 13378 } 13379 } else { 13380 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13381 13382 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13383 htonl(IPH_ECN_CE << 20)) { 13384 tcp->tcp_ecn_echo_on = B_TRUE; 13385 } 13386 } 13387 } 13388 13389 /* 13390 * Check whether we can update tcp_ts_recent. This test is 13391 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13392 * Extensions for High Performance: An Update", Internet Draft. 13393 */ 13394 if (tcp->tcp_snd_ts_ok && 13395 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13396 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13397 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13398 tcp->tcp_last_rcv_lbolt = lbolt64; 13399 } 13400 13401 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13402 /* 13403 * FIN in an out of order segment. We record this in 13404 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13405 * Clear the FIN so that any check on FIN flag will fail. 13406 * Remember that FIN also counts in the sequence number 13407 * space. So we need to ack out of order FIN only segments. 13408 */ 13409 if (flags & TH_FIN) { 13410 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13411 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13412 flags &= ~TH_FIN; 13413 flags |= TH_ACK_NEEDED; 13414 } 13415 if (seg_len > 0) { 13416 /* Fill in the SACK blk list. */ 13417 if (tcp->tcp_snd_sack_ok) { 13418 ASSERT(tcp->tcp_sack_info != NULL); 13419 tcp_sack_insert(tcp->tcp_sack_list, 13420 seg_seq, seg_seq + seg_len, 13421 &(tcp->tcp_num_sack_blk)); 13422 } 13423 13424 /* 13425 * Attempt reassembly and see if we have something 13426 * ready to go. 13427 */ 13428 mp = tcp_reass(tcp, mp, seg_seq); 13429 /* Always ack out of order packets */ 13430 flags |= TH_ACK_NEEDED | TH_PUSH; 13431 if (mp) { 13432 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13433 (uintptr_t)INT_MAX); 13434 seg_len = mp->b_cont ? msgdsize(mp) : 13435 (int)(mp->b_wptr - mp->b_rptr); 13436 seg_seq = tcp->tcp_rnxt; 13437 /* 13438 * A gap is filled and the seq num and len 13439 * of the gap match that of a previously 13440 * received FIN, put the FIN flag back in. 13441 */ 13442 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13443 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13444 flags |= TH_FIN; 13445 tcp->tcp_valid_bits &= 13446 ~TCP_OFO_FIN_VALID; 13447 } 13448 } else { 13449 /* 13450 * Keep going even with NULL mp. 13451 * There may be a useful ACK or something else 13452 * we don't want to miss. 13453 * 13454 * But TCP should not perform fast retransmit 13455 * because of the ack number. TCP uses 13456 * seg_len == 0 to determine if it is a pure 13457 * ACK. And this is not a pure ACK. 13458 */ 13459 seg_len = 0; 13460 ofo_seg = B_TRUE; 13461 } 13462 } 13463 } else if (seg_len > 0) { 13464 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13465 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13466 /* 13467 * If an out of order FIN was received before, and the seq 13468 * num and len of the new segment match that of the FIN, 13469 * put the FIN flag back in. 13470 */ 13471 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13472 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13473 flags |= TH_FIN; 13474 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13475 } 13476 } 13477 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13478 if (flags & TH_RST) { 13479 freemsg(mp); 13480 switch (tcp->tcp_state) { 13481 case TCPS_SYN_RCVD: 13482 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13483 break; 13484 case TCPS_ESTABLISHED: 13485 case TCPS_FIN_WAIT_1: 13486 case TCPS_FIN_WAIT_2: 13487 case TCPS_CLOSE_WAIT: 13488 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13489 break; 13490 case TCPS_CLOSING: 13491 case TCPS_LAST_ACK: 13492 (void) tcp_clean_death(tcp, 0, 16); 13493 break; 13494 default: 13495 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13496 (void) tcp_clean_death(tcp, ENXIO, 17); 13497 break; 13498 } 13499 return; 13500 } 13501 if (flags & TH_SYN) { 13502 /* 13503 * See RFC 793, Page 71 13504 * 13505 * The seq number must be in the window as it should 13506 * be "fixed" above. If it is outside window, it should 13507 * be already rejected. Note that we allow seg_seq to be 13508 * rnxt + rwnd because we want to accept 0 window probe. 13509 */ 13510 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13511 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13512 freemsg(mp); 13513 /* 13514 * If the ACK flag is not set, just use our snxt as the 13515 * seq number of the RST segment. 13516 */ 13517 if (!(flags & TH_ACK)) { 13518 seg_ack = tcp->tcp_snxt; 13519 } 13520 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13521 TH_RST|TH_ACK); 13522 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13523 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13524 return; 13525 } 13526 /* 13527 * urp could be -1 when the urp field in the packet is 0 13528 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13529 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13530 */ 13531 if (flags & TH_URG && urp >= 0) { 13532 if (!tcp->tcp_urp_last_valid || 13533 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13534 if (IPCL_IS_NONSTR(connp)) { 13535 if (!TCP_IS_DETACHED(tcp)) { 13536 (*connp->conn_upcalls->su_signal_oob) 13537 (connp->conn_upper_handle, urp); 13538 } 13539 } else { 13540 /* 13541 * If we haven't generated the signal yet for 13542 * this urgent pointer value, do it now. Also, 13543 * send up a zero-length M_DATA indicating 13544 * whether or not this is the mark. The latter 13545 * is not needed when a T_EXDATA_IND is sent up. 13546 * However, if there are allocation failures 13547 * this code relies on the sender retransmitting 13548 * and the socket code for determining the mark 13549 * should not block waiting for the peer to 13550 * transmit. Thus, for simplicity we always 13551 * send up the mark indication. 13552 */ 13553 mp1 = allocb(0, BPRI_MED); 13554 if (mp1 == NULL) { 13555 freemsg(mp); 13556 return; 13557 } 13558 if (!TCP_IS_DETACHED(tcp) && 13559 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13560 SIGURG)) { 13561 /* Try again on the rexmit. */ 13562 freemsg(mp1); 13563 freemsg(mp); 13564 return; 13565 } 13566 /* 13567 * Mark with NOTMARKNEXT for now. 13568 * The code below will change this to MARKNEXT 13569 * if we are at the mark. 13570 * 13571 * If there are allocation failures (e.g. in 13572 * dupmsg below) the next time tcp_rput_data 13573 * sees the urgent segment it will send up the 13574 * MSGMARKNEXT message. 13575 */ 13576 mp1->b_flag |= MSGNOTMARKNEXT; 13577 freemsg(tcp->tcp_urp_mark_mp); 13578 tcp->tcp_urp_mark_mp = mp1; 13579 flags |= TH_SEND_URP_MARK; 13580 #ifdef DEBUG 13581 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13582 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13583 "last %x, %s", 13584 seg_seq, urp, tcp->tcp_urp_last, 13585 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13586 #endif /* DEBUG */ 13587 } 13588 tcp->tcp_urp_last_valid = B_TRUE; 13589 tcp->tcp_urp_last = urp + seg_seq; 13590 } else if (tcp->tcp_urp_mark_mp != NULL) { 13591 /* 13592 * An allocation failure prevented the previous 13593 * tcp_rput_data from sending up the allocated 13594 * MSG*MARKNEXT message - send it up this time 13595 * around. 13596 */ 13597 flags |= TH_SEND_URP_MARK; 13598 } 13599 13600 /* 13601 * If the urgent byte is in this segment, make sure that it is 13602 * all by itself. This makes it much easier to deal with the 13603 * possibility of an allocation failure on the T_exdata_ind. 13604 * Note that seg_len is the number of bytes in the segment, and 13605 * urp is the offset into the segment of the urgent byte. 13606 * urp < seg_len means that the urgent byte is in this segment. 13607 */ 13608 if (urp < seg_len) { 13609 if (seg_len != 1) { 13610 uint32_t tmp_rnxt; 13611 /* 13612 * Break it up and feed it back in. 13613 * Re-attach the IP header. 13614 */ 13615 mp->b_rptr = iphdr; 13616 if (urp > 0) { 13617 /* 13618 * There is stuff before the urgent 13619 * byte. 13620 */ 13621 mp1 = dupmsg(mp); 13622 if (!mp1) { 13623 /* 13624 * Trim from urgent byte on. 13625 * The rest will come back. 13626 */ 13627 (void) adjmsg(mp, 13628 urp - seg_len); 13629 tcp_rput_data(connp, 13630 mp, NULL); 13631 return; 13632 } 13633 (void) adjmsg(mp1, urp - seg_len); 13634 /* Feed this piece back in. */ 13635 tmp_rnxt = tcp->tcp_rnxt; 13636 tcp_rput_data(connp, mp1, NULL); 13637 /* 13638 * If the data passed back in was not 13639 * processed (ie: bad ACK) sending 13640 * the remainder back in will cause a 13641 * loop. In this case, drop the 13642 * packet and let the sender try 13643 * sending a good packet. 13644 */ 13645 if (tmp_rnxt == tcp->tcp_rnxt) { 13646 freemsg(mp); 13647 return; 13648 } 13649 } 13650 if (urp != seg_len - 1) { 13651 uint32_t tmp_rnxt; 13652 /* 13653 * There is stuff after the urgent 13654 * byte. 13655 */ 13656 mp1 = dupmsg(mp); 13657 if (!mp1) { 13658 /* 13659 * Trim everything beyond the 13660 * urgent byte. The rest will 13661 * come back. 13662 */ 13663 (void) adjmsg(mp, 13664 urp + 1 - seg_len); 13665 tcp_rput_data(connp, 13666 mp, NULL); 13667 return; 13668 } 13669 (void) adjmsg(mp1, urp + 1 - seg_len); 13670 tmp_rnxt = tcp->tcp_rnxt; 13671 tcp_rput_data(connp, mp1, NULL); 13672 /* 13673 * If the data passed back in was not 13674 * processed (ie: bad ACK) sending 13675 * the remainder back in will cause a 13676 * loop. In this case, drop the 13677 * packet and let the sender try 13678 * sending a good packet. 13679 */ 13680 if (tmp_rnxt == tcp->tcp_rnxt) { 13681 freemsg(mp); 13682 return; 13683 } 13684 } 13685 tcp_rput_data(connp, mp, NULL); 13686 return; 13687 } 13688 /* 13689 * This segment contains only the urgent byte. We 13690 * have to allocate the T_exdata_ind, if we can. 13691 */ 13692 if (IPCL_IS_NONSTR(connp)) { 13693 int error; 13694 13695 (*connp->conn_upcalls->su_recv) 13696 (connp->conn_upper_handle, mp, seg_len, 13697 MSG_OOB, &error, NULL); 13698 /* 13699 * We should never be in middle of a 13700 * fallback, the squeue guarantees that. 13701 */ 13702 ASSERT(error != EOPNOTSUPP); 13703 mp = NULL; 13704 goto update_ack; 13705 } else if (!tcp->tcp_urp_mp) { 13706 struct T_exdata_ind *tei; 13707 mp1 = allocb(sizeof (struct T_exdata_ind), 13708 BPRI_MED); 13709 if (!mp1) { 13710 /* 13711 * Sigh... It'll be back. 13712 * Generate any MSG*MARK message now. 13713 */ 13714 freemsg(mp); 13715 seg_len = 0; 13716 if (flags & TH_SEND_URP_MARK) { 13717 13718 13719 ASSERT(tcp->tcp_urp_mark_mp); 13720 tcp->tcp_urp_mark_mp->b_flag &= 13721 ~MSGNOTMARKNEXT; 13722 tcp->tcp_urp_mark_mp->b_flag |= 13723 MSGMARKNEXT; 13724 } 13725 goto ack_check; 13726 } 13727 mp1->b_datap->db_type = M_PROTO; 13728 tei = (struct T_exdata_ind *)mp1->b_rptr; 13729 tei->PRIM_type = T_EXDATA_IND; 13730 tei->MORE_flag = 0; 13731 mp1->b_wptr = (uchar_t *)&tei[1]; 13732 tcp->tcp_urp_mp = mp1; 13733 #ifdef DEBUG 13734 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13735 "tcp_rput: allocated exdata_ind %s", 13736 tcp_display(tcp, NULL, 13737 DISP_PORT_ONLY)); 13738 #endif /* DEBUG */ 13739 /* 13740 * There is no need to send a separate MSG*MARK 13741 * message since the T_EXDATA_IND will be sent 13742 * now. 13743 */ 13744 flags &= ~TH_SEND_URP_MARK; 13745 freemsg(tcp->tcp_urp_mark_mp); 13746 tcp->tcp_urp_mark_mp = NULL; 13747 } 13748 /* 13749 * Now we are all set. On the next putnext upstream, 13750 * tcp_urp_mp will be non-NULL and will get prepended 13751 * to what has to be this piece containing the urgent 13752 * byte. If for any reason we abort this segment below, 13753 * if it comes back, we will have this ready, or it 13754 * will get blown off in close. 13755 */ 13756 } else if (urp == seg_len) { 13757 /* 13758 * The urgent byte is the next byte after this sequence 13759 * number. If there is data it is marked with 13760 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13761 * since it is not needed. Otherwise, if the code 13762 * above just allocated a zero-length tcp_urp_mark_mp 13763 * message, that message is tagged with MSGMARKNEXT. 13764 * Sending up these MSGMARKNEXT messages makes 13765 * SIOCATMARK work correctly even though 13766 * the T_EXDATA_IND will not be sent up until the 13767 * urgent byte arrives. 13768 */ 13769 if (seg_len != 0) { 13770 flags |= TH_MARKNEXT_NEEDED; 13771 freemsg(tcp->tcp_urp_mark_mp); 13772 tcp->tcp_urp_mark_mp = NULL; 13773 flags &= ~TH_SEND_URP_MARK; 13774 } else if (tcp->tcp_urp_mark_mp != NULL) { 13775 flags |= TH_SEND_URP_MARK; 13776 tcp->tcp_urp_mark_mp->b_flag &= 13777 ~MSGNOTMARKNEXT; 13778 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13779 } 13780 #ifdef DEBUG 13781 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13782 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13783 seg_len, flags, 13784 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13785 #endif /* DEBUG */ 13786 } 13787 #ifdef DEBUG 13788 else { 13789 /* Data left until we hit mark */ 13790 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13791 "tcp_rput: URP %d bytes left, %s", 13792 urp - seg_len, tcp_display(tcp, NULL, 13793 DISP_PORT_ONLY)); 13794 } 13795 #endif /* DEBUG */ 13796 } 13797 13798 process_ack: 13799 if (!(flags & TH_ACK)) { 13800 freemsg(mp); 13801 goto xmit_check; 13802 } 13803 } 13804 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13805 13806 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13807 tcp->tcp_ip_forward_progress = B_TRUE; 13808 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13809 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13810 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13811 /* 3-way handshake complete - pass up the T_CONN_IND */ 13812 tcp_t *listener = tcp->tcp_listener; 13813 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13814 13815 tcp->tcp_tconnind_started = B_TRUE; 13816 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13817 /* 13818 * We are here means eager is fine but it can 13819 * get a TH_RST at any point between now and till 13820 * accept completes and disappear. We need to 13821 * ensure that reference to eager is valid after 13822 * we get out of eager's perimeter. So we do 13823 * an extra refhold. 13824 */ 13825 CONN_INC_REF(connp); 13826 13827 /* 13828 * The listener also exists because of the refhold 13829 * done in tcp_conn_request. Its possible that it 13830 * might have closed. We will check that once we 13831 * get inside listeners context. 13832 */ 13833 CONN_INC_REF(listener->tcp_connp); 13834 if (listener->tcp_connp->conn_sqp == 13835 connp->conn_sqp) { 13836 /* 13837 * We optimize by not calling an SQUEUE_ENTER 13838 * on the listener since we know that the 13839 * listener and eager squeues are the same. 13840 * We are able to make this check safely only 13841 * because neither the eager nor the listener 13842 * can change its squeue. Only an active connect 13843 * can change its squeue 13844 */ 13845 tcp_send_conn_ind(listener->tcp_connp, mp, 13846 listener->tcp_connp->conn_sqp); 13847 CONN_DEC_REF(listener->tcp_connp); 13848 } else if (!tcp->tcp_loopback) { 13849 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13850 mp, tcp_send_conn_ind, 13851 listener->tcp_connp, SQ_FILL, 13852 SQTAG_TCP_CONN_IND); 13853 } else { 13854 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13855 mp, tcp_send_conn_ind, 13856 listener->tcp_connp, SQ_PROCESS, 13857 SQTAG_TCP_CONN_IND); 13858 } 13859 } 13860 13861 /* 13862 * We are seeing the final ack in the three way 13863 * hand shake of a active open'ed connection 13864 * so we must send up a T_CONN_CON 13865 * 13866 * tcp_sendmsg() checks tcp_state without entering 13867 * the squeue so tcp_state should be updated before 13868 * sending up connection confirmation. 13869 */ 13870 tcp->tcp_state = TCPS_ESTABLISHED; 13871 if (tcp->tcp_active_open) { 13872 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13873 freemsg(mp); 13874 tcp->tcp_state = TCPS_SYN_RCVD; 13875 return; 13876 } 13877 /* 13878 * Don't fuse the loopback endpoints for 13879 * simultaneous active opens. 13880 */ 13881 if (tcp->tcp_loopback) { 13882 TCP_STAT(tcps, tcp_fusion_unfusable); 13883 tcp->tcp_unfusable = B_TRUE; 13884 } 13885 } 13886 13887 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13888 bytes_acked--; 13889 /* SYN was acked - making progress */ 13890 if (tcp->tcp_ipversion == IPV6_VERSION) 13891 tcp->tcp_ip_forward_progress = B_TRUE; 13892 13893 /* 13894 * If SYN was retransmitted, need to reset all 13895 * retransmission info as this segment will be 13896 * treated as a dup ACK. 13897 */ 13898 if (tcp->tcp_rexmit) { 13899 tcp->tcp_rexmit = B_FALSE; 13900 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13901 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13902 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13903 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13904 tcp->tcp_ms_we_have_waited = 0; 13905 tcp->tcp_cwnd = mss; 13906 } 13907 13908 /* 13909 * We set the send window to zero here. 13910 * This is needed if there is data to be 13911 * processed already on the queue. 13912 * Later (at swnd_update label), the 13913 * "new_swnd > tcp_swnd" condition is satisfied 13914 * the XMIT_NEEDED flag is set in the current 13915 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13916 * called if there is already data on queue in 13917 * this state. 13918 */ 13919 tcp->tcp_swnd = 0; 13920 13921 if (new_swnd > tcp->tcp_max_swnd) 13922 tcp->tcp_max_swnd = new_swnd; 13923 tcp->tcp_swl1 = seg_seq; 13924 tcp->tcp_swl2 = seg_ack; 13925 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13926 13927 /* Fuse when both sides are in ESTABLISHED state */ 13928 if (tcp->tcp_loopback && do_tcp_fusion) 13929 tcp_fuse(tcp, iphdr, tcph); 13930 13931 } 13932 /* This code follows 4.4BSD-Lite2 mostly. */ 13933 if (bytes_acked < 0) 13934 goto est; 13935 13936 /* 13937 * If TCP is ECN capable and the congestion experience bit is 13938 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13939 * done once per window (or more loosely, per RTT). 13940 */ 13941 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13942 tcp->tcp_cwr = B_FALSE; 13943 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13944 if (!tcp->tcp_cwr) { 13945 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13946 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13947 tcp->tcp_cwnd = npkt * mss; 13948 /* 13949 * If the cwnd is 0, use the timer to clock out 13950 * new segments. This is required by the ECN spec. 13951 */ 13952 if (npkt == 0) { 13953 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13954 /* 13955 * This makes sure that when the ACK comes 13956 * back, we will increase tcp_cwnd by 1 MSS. 13957 */ 13958 tcp->tcp_cwnd_cnt = 0; 13959 } 13960 tcp->tcp_cwr = B_TRUE; 13961 /* 13962 * This marks the end of the current window of in 13963 * flight data. That is why we don't use 13964 * tcp_suna + tcp_swnd. Only data in flight can 13965 * provide ECN info. 13966 */ 13967 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13968 tcp->tcp_ecn_cwr_sent = B_FALSE; 13969 } 13970 } 13971 13972 mp1 = tcp->tcp_xmit_head; 13973 if (bytes_acked == 0) { 13974 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13975 int dupack_cnt; 13976 13977 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 13978 /* 13979 * Fast retransmit. When we have seen exactly three 13980 * identical ACKs while we have unacked data 13981 * outstanding we take it as a hint that our peer 13982 * dropped something. 13983 * 13984 * If TCP is retransmitting, don't do fast retransmit. 13985 */ 13986 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13987 ! tcp->tcp_rexmit) { 13988 /* Do Limited Transmit */ 13989 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13990 tcps->tcps_dupack_fast_retransmit) { 13991 /* 13992 * RFC 3042 13993 * 13994 * What we need to do is temporarily 13995 * increase tcp_cwnd so that new 13996 * data can be sent if it is allowed 13997 * by the receive window (tcp_rwnd). 13998 * tcp_wput_data() will take care of 13999 * the rest. 14000 * 14001 * If the connection is SACK capable, 14002 * only do limited xmit when there 14003 * is SACK info. 14004 * 14005 * Note how tcp_cwnd is incremented. 14006 * The first dup ACK will increase 14007 * it by 1 MSS. The second dup ACK 14008 * will increase it by 2 MSS. This 14009 * means that only 1 new segment will 14010 * be sent for each dup ACK. 14011 */ 14012 if (tcp->tcp_unsent > 0 && 14013 (!tcp->tcp_snd_sack_ok || 14014 (tcp->tcp_snd_sack_ok && 14015 tcp->tcp_notsack_list != NULL))) { 14016 tcp->tcp_cwnd += mss << 14017 (tcp->tcp_dupack_cnt - 1); 14018 flags |= TH_LIMIT_XMIT; 14019 } 14020 } else if (dupack_cnt == 14021 tcps->tcps_dupack_fast_retransmit) { 14022 14023 /* 14024 * If we have reduced tcp_ssthresh 14025 * because of ECN, do not reduce it again 14026 * unless it is already one window of data 14027 * away. After one window of data, tcp_cwr 14028 * should then be cleared. Note that 14029 * for non ECN capable connection, tcp_cwr 14030 * should always be false. 14031 * 14032 * Adjust cwnd since the duplicate 14033 * ack indicates that a packet was 14034 * dropped (due to congestion.) 14035 */ 14036 if (!tcp->tcp_cwr) { 14037 npkt = ((tcp->tcp_snxt - 14038 tcp->tcp_suna) >> 1) / mss; 14039 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14040 mss; 14041 tcp->tcp_cwnd = (npkt + 14042 tcp->tcp_dupack_cnt) * mss; 14043 } 14044 if (tcp->tcp_ecn_ok) { 14045 tcp->tcp_cwr = B_TRUE; 14046 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14047 tcp->tcp_ecn_cwr_sent = B_FALSE; 14048 } 14049 14050 /* 14051 * We do Hoe's algorithm. Refer to her 14052 * paper "Improving the Start-up Behavior 14053 * of a Congestion Control Scheme for TCP," 14054 * appeared in SIGCOMM'96. 14055 * 14056 * Save highest seq no we have sent so far. 14057 * Be careful about the invisible FIN byte. 14058 */ 14059 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14060 (tcp->tcp_unsent == 0)) { 14061 tcp->tcp_rexmit_max = tcp->tcp_fss; 14062 } else { 14063 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14064 } 14065 14066 /* 14067 * Do not allow bursty traffic during. 14068 * fast recovery. Refer to Fall and Floyd's 14069 * paper "Simulation-based Comparisons of 14070 * Tahoe, Reno and SACK TCP" (in CCR?) 14071 * This is a best current practise. 14072 */ 14073 tcp->tcp_snd_burst = TCP_CWND_SS; 14074 14075 /* 14076 * For SACK: 14077 * Calculate tcp_pipe, which is the 14078 * estimated number of bytes in 14079 * network. 14080 * 14081 * tcp_fack is the highest sack'ed seq num 14082 * TCP has received. 14083 * 14084 * tcp_pipe is explained in the above quoted 14085 * Fall and Floyd's paper. tcp_fack is 14086 * explained in Mathis and Mahdavi's 14087 * "Forward Acknowledgment: Refining TCP 14088 * Congestion Control" in SIGCOMM '96. 14089 */ 14090 if (tcp->tcp_snd_sack_ok) { 14091 ASSERT(tcp->tcp_sack_info != NULL); 14092 if (tcp->tcp_notsack_list != NULL) { 14093 tcp->tcp_pipe = tcp->tcp_snxt - 14094 tcp->tcp_fack; 14095 tcp->tcp_sack_snxt = seg_ack; 14096 flags |= TH_NEED_SACK_REXMIT; 14097 } else { 14098 /* 14099 * Always initialize tcp_pipe 14100 * even though we don't have 14101 * any SACK info. If later 14102 * we get SACK info and 14103 * tcp_pipe is not initialized, 14104 * funny things will happen. 14105 */ 14106 tcp->tcp_pipe = 14107 tcp->tcp_cwnd_ssthresh; 14108 } 14109 } else { 14110 flags |= TH_REXMIT_NEEDED; 14111 } /* tcp_snd_sack_ok */ 14112 14113 } else { 14114 /* 14115 * Here we perform congestion 14116 * avoidance, but NOT slow start. 14117 * This is known as the Fast 14118 * Recovery Algorithm. 14119 */ 14120 if (tcp->tcp_snd_sack_ok && 14121 tcp->tcp_notsack_list != NULL) { 14122 flags |= TH_NEED_SACK_REXMIT; 14123 tcp->tcp_pipe -= mss; 14124 if (tcp->tcp_pipe < 0) 14125 tcp->tcp_pipe = 0; 14126 } else { 14127 /* 14128 * We know that one more packet has 14129 * left the pipe thus we can update 14130 * cwnd. 14131 */ 14132 cwnd = tcp->tcp_cwnd + mss; 14133 if (cwnd > tcp->tcp_cwnd_max) 14134 cwnd = tcp->tcp_cwnd_max; 14135 tcp->tcp_cwnd = cwnd; 14136 if (tcp->tcp_unsent > 0) 14137 flags |= TH_XMIT_NEEDED; 14138 } 14139 } 14140 } 14141 } else if (tcp->tcp_zero_win_probe) { 14142 /* 14143 * If the window has opened, need to arrange 14144 * to send additional data. 14145 */ 14146 if (new_swnd != 0) { 14147 /* tcp_suna != tcp_snxt */ 14148 /* Packet contains a window update */ 14149 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14150 tcp->tcp_zero_win_probe = 0; 14151 tcp->tcp_timer_backoff = 0; 14152 tcp->tcp_ms_we_have_waited = 0; 14153 14154 /* 14155 * Transmit starting with tcp_suna since 14156 * the one byte probe is not ack'ed. 14157 * If TCP has sent more than one identical 14158 * probe, tcp_rexmit will be set. That means 14159 * tcp_ss_rexmit() will send out the one 14160 * byte along with new data. Otherwise, 14161 * fake the retransmission. 14162 */ 14163 flags |= TH_XMIT_NEEDED; 14164 if (!tcp->tcp_rexmit) { 14165 tcp->tcp_rexmit = B_TRUE; 14166 tcp->tcp_dupack_cnt = 0; 14167 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14168 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14169 } 14170 } 14171 } 14172 goto swnd_update; 14173 } 14174 14175 /* 14176 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14177 * If the ACK value acks something that we have not yet sent, it might 14178 * be an old duplicate segment. Send an ACK to re-synchronize the 14179 * other side. 14180 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14181 * state is handled above, so we can always just drop the segment and 14182 * send an ACK here. 14183 * 14184 * Should we send ACKs in response to ACK only segments? 14185 */ 14186 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14187 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14188 /* drop the received segment */ 14189 freemsg(mp); 14190 14191 /* 14192 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14193 * greater than 0, check if the number of such 14194 * bogus ACks is greater than that count. If yes, 14195 * don't send back any ACK. This prevents TCP from 14196 * getting into an ACK storm if somehow an attacker 14197 * successfully spoofs an acceptable segment to our 14198 * peer. 14199 */ 14200 if (tcp_drop_ack_unsent_cnt > 0 && 14201 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14202 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14203 return; 14204 } 14205 mp = tcp_ack_mp(tcp); 14206 if (mp != NULL) { 14207 BUMP_LOCAL(tcp->tcp_obsegs); 14208 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14209 tcp_send_data(tcp, tcp->tcp_wq, mp); 14210 } 14211 return; 14212 } 14213 14214 /* 14215 * TCP gets a new ACK, update the notsack'ed list to delete those 14216 * blocks that are covered by this ACK. 14217 */ 14218 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14219 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14220 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14221 } 14222 14223 /* 14224 * If we got an ACK after fast retransmit, check to see 14225 * if it is a partial ACK. If it is not and the congestion 14226 * window was inflated to account for the other side's 14227 * cached packets, retract it. If it is, do Hoe's algorithm. 14228 */ 14229 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14230 ASSERT(tcp->tcp_rexmit == B_FALSE); 14231 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14232 tcp->tcp_dupack_cnt = 0; 14233 /* 14234 * Restore the orig tcp_cwnd_ssthresh after 14235 * fast retransmit phase. 14236 */ 14237 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14238 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14239 } 14240 tcp->tcp_rexmit_max = seg_ack; 14241 tcp->tcp_cwnd_cnt = 0; 14242 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14243 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14244 14245 /* 14246 * Remove all notsack info to avoid confusion with 14247 * the next fast retrasnmit/recovery phase. 14248 */ 14249 if (tcp->tcp_snd_sack_ok && 14250 tcp->tcp_notsack_list != NULL) { 14251 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14252 } 14253 } else { 14254 if (tcp->tcp_snd_sack_ok && 14255 tcp->tcp_notsack_list != NULL) { 14256 flags |= TH_NEED_SACK_REXMIT; 14257 tcp->tcp_pipe -= mss; 14258 if (tcp->tcp_pipe < 0) 14259 tcp->tcp_pipe = 0; 14260 } else { 14261 /* 14262 * Hoe's algorithm: 14263 * 14264 * Retransmit the unack'ed segment and 14265 * restart fast recovery. Note that we 14266 * need to scale back tcp_cwnd to the 14267 * original value when we started fast 14268 * recovery. This is to prevent overly 14269 * aggressive behaviour in sending new 14270 * segments. 14271 */ 14272 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14273 tcps->tcps_dupack_fast_retransmit * mss; 14274 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14275 flags |= TH_REXMIT_NEEDED; 14276 } 14277 } 14278 } else { 14279 tcp->tcp_dupack_cnt = 0; 14280 if (tcp->tcp_rexmit) { 14281 /* 14282 * TCP is retranmitting. If the ACK ack's all 14283 * outstanding data, update tcp_rexmit_max and 14284 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14285 * to the correct value. 14286 * 14287 * Note that SEQ_LEQ() is used. This is to avoid 14288 * unnecessary fast retransmit caused by dup ACKs 14289 * received when TCP does slow start retransmission 14290 * after a time out. During this phase, TCP may 14291 * send out segments which are already received. 14292 * This causes dup ACKs to be sent back. 14293 */ 14294 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14295 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14296 tcp->tcp_rexmit_nxt = seg_ack; 14297 } 14298 if (seg_ack != tcp->tcp_rexmit_max) { 14299 flags |= TH_XMIT_NEEDED; 14300 } 14301 } else { 14302 tcp->tcp_rexmit = B_FALSE; 14303 tcp->tcp_xmit_zc_clean = B_FALSE; 14304 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14305 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14306 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14307 } 14308 tcp->tcp_ms_we_have_waited = 0; 14309 } 14310 } 14311 14312 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14313 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14314 tcp->tcp_suna = seg_ack; 14315 if (tcp->tcp_zero_win_probe != 0) { 14316 tcp->tcp_zero_win_probe = 0; 14317 tcp->tcp_timer_backoff = 0; 14318 } 14319 14320 /* 14321 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14322 * Note that it cannot be the SYN being ack'ed. The code flow 14323 * will not reach here. 14324 */ 14325 if (mp1 == NULL) { 14326 goto fin_acked; 14327 } 14328 14329 /* 14330 * Update the congestion window. 14331 * 14332 * If TCP is not ECN capable or TCP is ECN capable but the 14333 * congestion experience bit is not set, increase the tcp_cwnd as 14334 * usual. 14335 */ 14336 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14337 cwnd = tcp->tcp_cwnd; 14338 add = mss; 14339 14340 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14341 /* 14342 * This is to prevent an increase of less than 1 MSS of 14343 * tcp_cwnd. With partial increase, tcp_wput_data() 14344 * may send out tinygrams in order to preserve mblk 14345 * boundaries. 14346 * 14347 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14348 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14349 * increased by 1 MSS for every RTTs. 14350 */ 14351 if (tcp->tcp_cwnd_cnt <= 0) { 14352 tcp->tcp_cwnd_cnt = cwnd + add; 14353 } else { 14354 tcp->tcp_cwnd_cnt -= add; 14355 add = 0; 14356 } 14357 } 14358 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14359 } 14360 14361 /* See if the latest urgent data has been acknowledged */ 14362 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14363 SEQ_GT(seg_ack, tcp->tcp_urg)) 14364 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14365 14366 /* Can we update the RTT estimates? */ 14367 if (tcp->tcp_snd_ts_ok) { 14368 /* Ignore zero timestamp echo-reply. */ 14369 if (tcpopt.tcp_opt_ts_ecr != 0) { 14370 tcp_set_rto(tcp, (int32_t)lbolt - 14371 (int32_t)tcpopt.tcp_opt_ts_ecr); 14372 } 14373 14374 /* If needed, restart the timer. */ 14375 if (tcp->tcp_set_timer == 1) { 14376 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14377 tcp->tcp_set_timer = 0; 14378 } 14379 /* 14380 * Update tcp_csuna in case the other side stops sending 14381 * us timestamps. 14382 */ 14383 tcp->tcp_csuna = tcp->tcp_snxt; 14384 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14385 /* 14386 * An ACK sequence we haven't seen before, so get the RTT 14387 * and update the RTO. But first check if the timestamp is 14388 * valid to use. 14389 */ 14390 if ((mp1->b_next != NULL) && 14391 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14392 tcp_set_rto(tcp, (int32_t)lbolt - 14393 (int32_t)(intptr_t)mp1->b_prev); 14394 else 14395 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14396 14397 /* Remeber the last sequence to be ACKed */ 14398 tcp->tcp_csuna = seg_ack; 14399 if (tcp->tcp_set_timer == 1) { 14400 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14401 tcp->tcp_set_timer = 0; 14402 } 14403 } else { 14404 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14405 } 14406 14407 /* Eat acknowledged bytes off the xmit queue. */ 14408 for (;;) { 14409 mblk_t *mp2; 14410 uchar_t *wptr; 14411 14412 wptr = mp1->b_wptr; 14413 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14414 bytes_acked -= (int)(wptr - mp1->b_rptr); 14415 if (bytes_acked < 0) { 14416 mp1->b_rptr = wptr + bytes_acked; 14417 /* 14418 * Set a new timestamp if all the bytes timed by the 14419 * old timestamp have been ack'ed. 14420 */ 14421 if (SEQ_GT(seg_ack, 14422 (uint32_t)(uintptr_t)(mp1->b_next))) { 14423 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14424 mp1->b_next = NULL; 14425 } 14426 break; 14427 } 14428 mp1->b_next = NULL; 14429 mp1->b_prev = NULL; 14430 mp2 = mp1; 14431 mp1 = mp1->b_cont; 14432 14433 /* 14434 * This notification is required for some zero-copy 14435 * clients to maintain a copy semantic. After the data 14436 * is ack'ed, client is safe to modify or reuse the buffer. 14437 */ 14438 if (tcp->tcp_snd_zcopy_aware && 14439 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14440 tcp_zcopy_notify(tcp); 14441 freeb(mp2); 14442 if (bytes_acked == 0) { 14443 if (mp1 == NULL) { 14444 /* Everything is ack'ed, clear the tail. */ 14445 tcp->tcp_xmit_tail = NULL; 14446 /* 14447 * Cancel the timer unless we are still 14448 * waiting for an ACK for the FIN packet. 14449 */ 14450 if (tcp->tcp_timer_tid != 0 && 14451 tcp->tcp_snxt == tcp->tcp_suna) { 14452 (void) TCP_TIMER_CANCEL(tcp, 14453 tcp->tcp_timer_tid); 14454 tcp->tcp_timer_tid = 0; 14455 } 14456 goto pre_swnd_update; 14457 } 14458 if (mp2 != tcp->tcp_xmit_tail) 14459 break; 14460 tcp->tcp_xmit_tail = mp1; 14461 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14462 (uintptr_t)INT_MAX); 14463 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14464 mp1->b_rptr); 14465 break; 14466 } 14467 if (mp1 == NULL) { 14468 /* 14469 * More was acked but there is nothing more 14470 * outstanding. This means that the FIN was 14471 * just acked or that we're talking to a clown. 14472 */ 14473 fin_acked: 14474 ASSERT(tcp->tcp_fin_sent); 14475 tcp->tcp_xmit_tail = NULL; 14476 if (tcp->tcp_fin_sent) { 14477 /* FIN was acked - making progress */ 14478 if (tcp->tcp_ipversion == IPV6_VERSION && 14479 !tcp->tcp_fin_acked) 14480 tcp->tcp_ip_forward_progress = B_TRUE; 14481 tcp->tcp_fin_acked = B_TRUE; 14482 if (tcp->tcp_linger_tid != 0 && 14483 TCP_TIMER_CANCEL(tcp, 14484 tcp->tcp_linger_tid) >= 0) { 14485 tcp_stop_lingering(tcp); 14486 freemsg(mp); 14487 mp = NULL; 14488 } 14489 } else { 14490 /* 14491 * We should never get here because 14492 * we have already checked that the 14493 * number of bytes ack'ed should be 14494 * smaller than or equal to what we 14495 * have sent so far (it is the 14496 * acceptability check of the ACK). 14497 * We can only get here if the send 14498 * queue is corrupted. 14499 * 14500 * Terminate the connection and 14501 * panic the system. It is better 14502 * for us to panic instead of 14503 * continuing to avoid other disaster. 14504 */ 14505 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14506 tcp->tcp_rnxt, TH_RST|TH_ACK); 14507 panic("Memory corruption " 14508 "detected for connection %s.", 14509 tcp_display(tcp, NULL, 14510 DISP_ADDR_AND_PORT)); 14511 /*NOTREACHED*/ 14512 } 14513 goto pre_swnd_update; 14514 } 14515 ASSERT(mp2 != tcp->tcp_xmit_tail); 14516 } 14517 if (tcp->tcp_unsent) { 14518 flags |= TH_XMIT_NEEDED; 14519 } 14520 pre_swnd_update: 14521 tcp->tcp_xmit_head = mp1; 14522 swnd_update: 14523 /* 14524 * The following check is different from most other implementations. 14525 * For bi-directional transfer, when segments are dropped, the 14526 * "normal" check will not accept a window update in those 14527 * retransmitted segemnts. Failing to do that, TCP may send out 14528 * segments which are outside receiver's window. As TCP accepts 14529 * the ack in those retransmitted segments, if the window update in 14530 * the same segment is not accepted, TCP will incorrectly calculates 14531 * that it can send more segments. This can create a deadlock 14532 * with the receiver if its window becomes zero. 14533 */ 14534 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14535 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14536 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14537 /* 14538 * The criteria for update is: 14539 * 14540 * 1. the segment acknowledges some data. Or 14541 * 2. the segment is new, i.e. it has a higher seq num. Or 14542 * 3. the segment is not old and the advertised window is 14543 * larger than the previous advertised window. 14544 */ 14545 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14546 flags |= TH_XMIT_NEEDED; 14547 tcp->tcp_swnd = new_swnd; 14548 if (new_swnd > tcp->tcp_max_swnd) 14549 tcp->tcp_max_swnd = new_swnd; 14550 tcp->tcp_swl1 = seg_seq; 14551 tcp->tcp_swl2 = seg_ack; 14552 } 14553 est: 14554 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14555 14556 switch (tcp->tcp_state) { 14557 case TCPS_FIN_WAIT_1: 14558 if (tcp->tcp_fin_acked) { 14559 tcp->tcp_state = TCPS_FIN_WAIT_2; 14560 /* 14561 * We implement the non-standard BSD/SunOS 14562 * FIN_WAIT_2 flushing algorithm. 14563 * If there is no user attached to this 14564 * TCP endpoint, then this TCP struct 14565 * could hang around forever in FIN_WAIT_2 14566 * state if the peer forgets to send us 14567 * a FIN. To prevent this, we wait only 14568 * 2*MSL (a convenient time value) for 14569 * the FIN to arrive. If it doesn't show up, 14570 * we flush the TCP endpoint. This algorithm, 14571 * though a violation of RFC-793, has worked 14572 * for over 10 years in BSD systems. 14573 * Note: SunOS 4.x waits 675 seconds before 14574 * flushing the FIN_WAIT_2 connection. 14575 */ 14576 TCP_TIMER_RESTART(tcp, 14577 tcps->tcps_fin_wait_2_flush_interval); 14578 } 14579 break; 14580 case TCPS_FIN_WAIT_2: 14581 break; /* Shutdown hook? */ 14582 case TCPS_LAST_ACK: 14583 freemsg(mp); 14584 if (tcp->tcp_fin_acked) { 14585 (void) tcp_clean_death(tcp, 0, 19); 14586 return; 14587 } 14588 goto xmit_check; 14589 case TCPS_CLOSING: 14590 if (tcp->tcp_fin_acked) { 14591 tcp->tcp_state = TCPS_TIME_WAIT; 14592 /* 14593 * Unconditionally clear the exclusive binding 14594 * bit so this TIME-WAIT connection won't 14595 * interfere with new ones. 14596 */ 14597 tcp->tcp_exclbind = 0; 14598 if (!TCP_IS_DETACHED(tcp)) { 14599 TCP_TIMER_RESTART(tcp, 14600 tcps->tcps_time_wait_interval); 14601 } else { 14602 tcp_time_wait_append(tcp); 14603 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14604 } 14605 } 14606 /*FALLTHRU*/ 14607 case TCPS_CLOSE_WAIT: 14608 freemsg(mp); 14609 goto xmit_check; 14610 default: 14611 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14612 break; 14613 } 14614 } 14615 if (flags & TH_FIN) { 14616 /* Make sure we ack the fin */ 14617 flags |= TH_ACK_NEEDED; 14618 if (!tcp->tcp_fin_rcvd) { 14619 tcp->tcp_fin_rcvd = B_TRUE; 14620 tcp->tcp_rnxt++; 14621 tcph = tcp->tcp_tcph; 14622 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14623 14624 /* 14625 * Generate the ordrel_ind at the end unless we 14626 * are an eager guy. 14627 * In the eager case tcp_rsrv will do this when run 14628 * after tcp_accept is done. 14629 */ 14630 if (tcp->tcp_listener == NULL && 14631 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14632 flags |= TH_ORDREL_NEEDED; 14633 switch (tcp->tcp_state) { 14634 case TCPS_SYN_RCVD: 14635 case TCPS_ESTABLISHED: 14636 tcp->tcp_state = TCPS_CLOSE_WAIT; 14637 /* Keepalive? */ 14638 break; 14639 case TCPS_FIN_WAIT_1: 14640 if (!tcp->tcp_fin_acked) { 14641 tcp->tcp_state = TCPS_CLOSING; 14642 break; 14643 } 14644 /* FALLTHRU */ 14645 case TCPS_FIN_WAIT_2: 14646 tcp->tcp_state = TCPS_TIME_WAIT; 14647 /* 14648 * Unconditionally clear the exclusive binding 14649 * bit so this TIME-WAIT connection won't 14650 * interfere with new ones. 14651 */ 14652 tcp->tcp_exclbind = 0; 14653 if (!TCP_IS_DETACHED(tcp)) { 14654 TCP_TIMER_RESTART(tcp, 14655 tcps->tcps_time_wait_interval); 14656 } else { 14657 tcp_time_wait_append(tcp); 14658 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14659 } 14660 if (seg_len) { 14661 /* 14662 * implies data piggybacked on FIN. 14663 * break to handle data. 14664 */ 14665 break; 14666 } 14667 freemsg(mp); 14668 goto ack_check; 14669 } 14670 } 14671 } 14672 if (mp == NULL) 14673 goto xmit_check; 14674 if (seg_len == 0) { 14675 freemsg(mp); 14676 goto xmit_check; 14677 } 14678 if (mp->b_rptr == mp->b_wptr) { 14679 /* 14680 * The header has been consumed, so we remove the 14681 * zero-length mblk here. 14682 */ 14683 mp1 = mp; 14684 mp = mp->b_cont; 14685 freeb(mp1); 14686 } 14687 update_ack: 14688 tcph = tcp->tcp_tcph; 14689 tcp->tcp_rack_cnt++; 14690 { 14691 uint32_t cur_max; 14692 14693 cur_max = tcp->tcp_rack_cur_max; 14694 if (tcp->tcp_rack_cnt >= cur_max) { 14695 /* 14696 * We have more unacked data than we should - send 14697 * an ACK now. 14698 */ 14699 flags |= TH_ACK_NEEDED; 14700 cur_max++; 14701 if (cur_max > tcp->tcp_rack_abs_max) 14702 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14703 else 14704 tcp->tcp_rack_cur_max = cur_max; 14705 } else if (TCP_IS_DETACHED(tcp)) { 14706 /* We don't have an ACK timer for detached TCP. */ 14707 flags |= TH_ACK_NEEDED; 14708 } else if (seg_len < mss) { 14709 /* 14710 * If we get a segment that is less than an mss, and we 14711 * already have unacknowledged data, and the amount 14712 * unacknowledged is not a multiple of mss, then we 14713 * better generate an ACK now. Otherwise, this may be 14714 * the tail piece of a transaction, and we would rather 14715 * wait for the response. 14716 */ 14717 uint32_t udif; 14718 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14719 (uintptr_t)INT_MAX); 14720 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14721 if (udif && (udif % mss)) 14722 flags |= TH_ACK_NEEDED; 14723 else 14724 flags |= TH_ACK_TIMER_NEEDED; 14725 } else { 14726 /* Start delayed ack timer */ 14727 flags |= TH_ACK_TIMER_NEEDED; 14728 } 14729 } 14730 tcp->tcp_rnxt += seg_len; 14731 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14732 14733 if (mp == NULL) 14734 goto xmit_check; 14735 14736 /* Update SACK list */ 14737 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14738 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14739 &(tcp->tcp_num_sack_blk)); 14740 } 14741 14742 if (tcp->tcp_urp_mp) { 14743 tcp->tcp_urp_mp->b_cont = mp; 14744 mp = tcp->tcp_urp_mp; 14745 tcp->tcp_urp_mp = NULL; 14746 /* Ready for a new signal. */ 14747 tcp->tcp_urp_last_valid = B_FALSE; 14748 #ifdef DEBUG 14749 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14750 "tcp_rput: sending exdata_ind %s", 14751 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14752 #endif /* DEBUG */ 14753 } 14754 14755 /* 14756 * Check for ancillary data changes compared to last segment. 14757 */ 14758 if (tcp->tcp_ipv6_recvancillary != 0) { 14759 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14760 ASSERT(mp != NULL); 14761 } 14762 14763 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14764 /* 14765 * Side queue inbound data until the accept happens. 14766 * tcp_accept/tcp_rput drains this when the accept happens. 14767 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14768 * T_EXDATA_IND) it is queued on b_next. 14769 * XXX Make urgent data use this. Requires: 14770 * Removing tcp_listener check for TH_URG 14771 * Making M_PCPROTO and MARK messages skip the eager case 14772 */ 14773 14774 if (tcp->tcp_kssl_pending) { 14775 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14776 mblk_t *, mp); 14777 tcp_kssl_input(tcp, mp); 14778 } else { 14779 tcp_rcv_enqueue(tcp, mp, seg_len); 14780 } 14781 } else { 14782 if (mp->b_datap->db_type != M_DATA || 14783 (flags & TH_MARKNEXT_NEEDED)) { 14784 if (IPCL_IS_NONSTR(connp)) { 14785 int error; 14786 14787 if ((*connp->conn_upcalls->su_recv) 14788 (connp->conn_upper_handle, mp, 14789 seg_len, 0, &error, NULL) <= 0) { 14790 /* 14791 * We should never be in middle of a 14792 * fallback, the squeue guarantees that. 14793 */ 14794 ASSERT(error != EOPNOTSUPP); 14795 if (error == ENOSPC) 14796 tcp->tcp_rwnd -= seg_len; 14797 } 14798 } else if (tcp->tcp_rcv_list != NULL) { 14799 flags |= tcp_rcv_drain(tcp); 14800 } 14801 ASSERT(tcp->tcp_rcv_list == NULL || 14802 tcp->tcp_fused_sigurg); 14803 14804 if (flags & TH_MARKNEXT_NEEDED) { 14805 #ifdef DEBUG 14806 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14807 "tcp_rput: sending MSGMARKNEXT %s", 14808 tcp_display(tcp, NULL, 14809 DISP_PORT_ONLY)); 14810 #endif /* DEBUG */ 14811 mp->b_flag |= MSGMARKNEXT; 14812 flags &= ~TH_MARKNEXT_NEEDED; 14813 } 14814 14815 /* Does this need SSL processing first? */ 14816 if ((tcp->tcp_kssl_ctx != NULL) && 14817 (DB_TYPE(mp) == M_DATA)) { 14818 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14819 mblk_t *, mp); 14820 tcp_kssl_input(tcp, mp); 14821 } else if (!IPCL_IS_NONSTR(connp)) { 14822 /* Already handled non-STREAMS case. */ 14823 putnext(tcp->tcp_rq, mp); 14824 if (!canputnext(tcp->tcp_rq)) 14825 tcp->tcp_rwnd -= seg_len; 14826 } 14827 } else if ((tcp->tcp_kssl_ctx != NULL) && 14828 (DB_TYPE(mp) == M_DATA)) { 14829 /* Does this need SSL processing first? */ 14830 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14831 tcp_kssl_input(tcp, mp); 14832 } else if (IPCL_IS_NONSTR(connp)) { 14833 /* Non-STREAMS socket */ 14834 boolean_t push = flags & (TH_PUSH|TH_FIN); 14835 int error; 14836 14837 if ((*connp->conn_upcalls->su_recv)( 14838 connp->conn_upper_handle, 14839 mp, seg_len, 0, &error, &push) <= 0) { 14840 /* 14841 * We should never be in middle of a 14842 * fallback, the squeue guarantees that. 14843 */ 14844 ASSERT(error != EOPNOTSUPP); 14845 if (error == ENOSPC) 14846 tcp->tcp_rwnd -= seg_len; 14847 } else if (push) { 14848 /* 14849 * PUSH bit set and sockfs is not 14850 * flow controlled 14851 */ 14852 flags |= tcp_rwnd_reopen(tcp); 14853 } 14854 } else if ((flags & (TH_PUSH|TH_FIN)) || 14855 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 14856 if (tcp->tcp_rcv_list != NULL) { 14857 /* 14858 * Enqueue the new segment first and then 14859 * call tcp_rcv_drain() to send all data 14860 * up. The other way to do this is to 14861 * send all queued data up and then call 14862 * putnext() to send the new segment up. 14863 * This way can remove the else part later 14864 * on. 14865 * 14866 * We don't do this to avoid one more call to 14867 * canputnext() as tcp_rcv_drain() needs to 14868 * call canputnext(). 14869 */ 14870 tcp_rcv_enqueue(tcp, mp, seg_len); 14871 flags |= tcp_rcv_drain(tcp); 14872 } else { 14873 putnext(tcp->tcp_rq, mp); 14874 if (!canputnext(tcp->tcp_rq)) 14875 tcp->tcp_rwnd -= seg_len; 14876 } 14877 } else { 14878 /* 14879 * Enqueue all packets when processing an mblk 14880 * from the co queue and also enqueue normal packets. 14881 */ 14882 tcp_rcv_enqueue(tcp, mp, seg_len); 14883 } 14884 /* 14885 * Make sure the timer is running if we have data waiting 14886 * for a push bit. This provides resiliency against 14887 * implementations that do not correctly generate push bits. 14888 */ 14889 if (!IPCL_IS_NONSTR(connp) && tcp->tcp_rcv_list != NULL && 14890 tcp->tcp_push_tid == 0) { 14891 /* 14892 * The connection may be closed at this point, so don't 14893 * do anything for a detached tcp. 14894 */ 14895 if (!TCP_IS_DETACHED(tcp)) 14896 tcp->tcp_push_tid = TCP_TIMER(tcp, 14897 tcp_push_timer, 14898 MSEC_TO_TICK( 14899 tcps->tcps_push_timer_interval)); 14900 } 14901 } 14902 14903 xmit_check: 14904 /* Is there anything left to do? */ 14905 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14906 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14907 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14908 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14909 goto done; 14910 14911 /* Any transmit work to do and a non-zero window? */ 14912 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14913 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14914 if (flags & TH_REXMIT_NEEDED) { 14915 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14916 14917 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 14918 if (snd_size > mss) 14919 snd_size = mss; 14920 if (snd_size > tcp->tcp_swnd) 14921 snd_size = tcp->tcp_swnd; 14922 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14923 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14924 B_TRUE); 14925 14926 if (mp1 != NULL) { 14927 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14928 tcp->tcp_csuna = tcp->tcp_snxt; 14929 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 14930 UPDATE_MIB(&tcps->tcps_mib, 14931 tcpRetransBytes, snd_size); 14932 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14933 } 14934 } 14935 if (flags & TH_NEED_SACK_REXMIT) { 14936 tcp_sack_rxmit(tcp, &flags); 14937 } 14938 /* 14939 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14940 * out new segment. Note that tcp_rexmit should not be 14941 * set, otherwise TH_LIMIT_XMIT should not be set. 14942 */ 14943 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14944 if (!tcp->tcp_rexmit) { 14945 tcp_wput_data(tcp, NULL, B_FALSE); 14946 } else { 14947 tcp_ss_rexmit(tcp); 14948 } 14949 } 14950 /* 14951 * Adjust tcp_cwnd back to normal value after sending 14952 * new data segments. 14953 */ 14954 if (flags & TH_LIMIT_XMIT) { 14955 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14956 /* 14957 * This will restart the timer. Restarting the 14958 * timer is used to avoid a timeout before the 14959 * limited transmitted segment's ACK gets back. 14960 */ 14961 if (tcp->tcp_xmit_head != NULL) 14962 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14963 } 14964 14965 /* Anything more to do? */ 14966 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14967 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14968 goto done; 14969 } 14970 ack_check: 14971 if (flags & TH_SEND_URP_MARK) { 14972 ASSERT(tcp->tcp_urp_mark_mp); 14973 ASSERT(!IPCL_IS_NONSTR(connp)); 14974 /* 14975 * Send up any queued data and then send the mark message 14976 */ 14977 if (tcp->tcp_rcv_list != NULL) { 14978 flags |= tcp_rcv_drain(tcp); 14979 14980 } 14981 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14982 mp1 = tcp->tcp_urp_mark_mp; 14983 tcp->tcp_urp_mark_mp = NULL; 14984 putnext(tcp->tcp_rq, mp1); 14985 #ifdef DEBUG 14986 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14987 "tcp_rput: sending zero-length %s %s", 14988 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14989 "MSGNOTMARKNEXT"), 14990 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14991 #endif /* DEBUG */ 14992 flags &= ~TH_SEND_URP_MARK; 14993 } 14994 if (flags & TH_ACK_NEEDED) { 14995 /* 14996 * Time to send an ack for some reason. 14997 */ 14998 mp1 = tcp_ack_mp(tcp); 14999 15000 if (mp1 != NULL) { 15001 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15002 BUMP_LOCAL(tcp->tcp_obsegs); 15003 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15004 } 15005 if (tcp->tcp_ack_tid != 0) { 15006 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15007 tcp->tcp_ack_tid = 0; 15008 } 15009 } 15010 if (flags & TH_ACK_TIMER_NEEDED) { 15011 /* 15012 * Arrange for deferred ACK or push wait timeout. 15013 * Start timer if it is not already running. 15014 */ 15015 if (tcp->tcp_ack_tid == 0) { 15016 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15017 MSEC_TO_TICK(tcp->tcp_localnet ? 15018 (clock_t)tcps->tcps_local_dack_interval : 15019 (clock_t)tcps->tcps_deferred_ack_interval)); 15020 } 15021 } 15022 if (flags & TH_ORDREL_NEEDED) { 15023 /* 15024 * Send up the ordrel_ind unless we are an eager guy. 15025 * In the eager case tcp_rsrv will do this when run 15026 * after tcp_accept is done. 15027 */ 15028 ASSERT(tcp->tcp_listener == NULL); 15029 15030 if (IPCL_IS_NONSTR(connp)) { 15031 ASSERT(tcp->tcp_ordrel_mp == NULL); 15032 tcp->tcp_ordrel_done = B_TRUE; 15033 (*connp->conn_upcalls->su_opctl) 15034 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15035 goto done; 15036 } 15037 15038 if (tcp->tcp_rcv_list != NULL) { 15039 /* 15040 * Push any mblk(s) enqueued from co processing. 15041 */ 15042 flags |= tcp_rcv_drain(tcp); 15043 } 15044 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15045 15046 mp1 = tcp->tcp_ordrel_mp; 15047 tcp->tcp_ordrel_mp = NULL; 15048 tcp->tcp_ordrel_done = B_TRUE; 15049 putnext(tcp->tcp_rq, mp1); 15050 } 15051 done: 15052 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15053 } 15054 15055 /* 15056 * This function does PAWS protection check. Returns B_TRUE if the 15057 * segment passes the PAWS test, else returns B_FALSE. 15058 */ 15059 boolean_t 15060 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15061 { 15062 uint8_t flags; 15063 int options; 15064 uint8_t *up; 15065 15066 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15067 /* 15068 * If timestamp option is aligned nicely, get values inline, 15069 * otherwise call general routine to parse. Only do that 15070 * if timestamp is the only option. 15071 */ 15072 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15073 TCPOPT_REAL_TS_LEN && 15074 OK_32PTR((up = ((uint8_t *)tcph) + 15075 TCP_MIN_HEADER_LENGTH)) && 15076 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15077 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15078 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15079 15080 options = TCP_OPT_TSTAMP_PRESENT; 15081 } else { 15082 if (tcp->tcp_snd_sack_ok) { 15083 tcpoptp->tcp = tcp; 15084 } else { 15085 tcpoptp->tcp = NULL; 15086 } 15087 options = tcp_parse_options(tcph, tcpoptp); 15088 } 15089 15090 if (options & TCP_OPT_TSTAMP_PRESENT) { 15091 /* 15092 * Do PAWS per RFC 1323 section 4.2. Accept RST 15093 * regardless of the timestamp, page 18 RFC 1323.bis. 15094 */ 15095 if ((flags & TH_RST) == 0 && 15096 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15097 tcp->tcp_ts_recent)) { 15098 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15099 PAWS_TIMEOUT)) { 15100 /* This segment is not acceptable. */ 15101 return (B_FALSE); 15102 } else { 15103 /* 15104 * Connection has been idle for 15105 * too long. Reset the timestamp 15106 * and assume the segment is valid. 15107 */ 15108 tcp->tcp_ts_recent = 15109 tcpoptp->tcp_opt_ts_val; 15110 } 15111 } 15112 } else { 15113 /* 15114 * If we don't get a timestamp on every packet, we 15115 * figure we can't really trust 'em, so we stop sending 15116 * and parsing them. 15117 */ 15118 tcp->tcp_snd_ts_ok = B_FALSE; 15119 15120 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15121 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15122 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15123 /* 15124 * Adjust the tcp_mss accordingly. We also need to 15125 * adjust tcp_cwnd here in accordance with the new mss. 15126 * But we avoid doing a slow start here so as to not 15127 * to lose on the transfer rate built up so far. 15128 */ 15129 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15130 if (tcp->tcp_snd_sack_ok) { 15131 ASSERT(tcp->tcp_sack_info != NULL); 15132 tcp->tcp_max_sack_blk = 4; 15133 } 15134 } 15135 return (B_TRUE); 15136 } 15137 15138 /* 15139 * Attach ancillary data to a received TCP segments for the 15140 * ancillary pieces requested by the application that are 15141 * different than they were in the previous data segment. 15142 * 15143 * Save the "current" values once memory allocation is ok so that 15144 * when memory allocation fails we can just wait for the next data segment. 15145 */ 15146 static mblk_t * 15147 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15148 { 15149 struct T_optdata_ind *todi; 15150 int optlen; 15151 uchar_t *optptr; 15152 struct T_opthdr *toh; 15153 uint_t addflag; /* Which pieces to add */ 15154 mblk_t *mp1; 15155 15156 optlen = 0; 15157 addflag = 0; 15158 /* If app asked for pktinfo and the index has changed ... */ 15159 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15160 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15161 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15162 optlen += sizeof (struct T_opthdr) + 15163 sizeof (struct in6_pktinfo); 15164 addflag |= TCP_IPV6_RECVPKTINFO; 15165 } 15166 /* If app asked for hoplimit and it has changed ... */ 15167 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15168 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15169 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15170 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15171 addflag |= TCP_IPV6_RECVHOPLIMIT; 15172 } 15173 /* If app asked for tclass and it has changed ... */ 15174 if ((ipp->ipp_fields & IPPF_TCLASS) && 15175 ipp->ipp_tclass != tcp->tcp_recvtclass && 15176 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15177 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15178 addflag |= TCP_IPV6_RECVTCLASS; 15179 } 15180 /* 15181 * If app asked for hopbyhop headers and it has changed ... 15182 * For security labels, note that (1) security labels can't change on 15183 * a connected socket at all, (2) we're connected to at most one peer, 15184 * (3) if anything changes, then it must be some other extra option. 15185 */ 15186 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15187 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15188 (ipp->ipp_fields & IPPF_HOPOPTS), 15189 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15190 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15191 tcp->tcp_label_len; 15192 addflag |= TCP_IPV6_RECVHOPOPTS; 15193 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15194 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15195 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15196 return (mp); 15197 } 15198 /* If app asked for dst headers before routing headers ... */ 15199 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15200 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15201 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15202 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15203 optlen += sizeof (struct T_opthdr) + 15204 ipp->ipp_rtdstoptslen; 15205 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15206 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15207 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15208 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15209 return (mp); 15210 } 15211 /* If app asked for routing headers and it has changed ... */ 15212 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15213 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15214 (ipp->ipp_fields & IPPF_RTHDR), 15215 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15216 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15217 addflag |= TCP_IPV6_RECVRTHDR; 15218 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15219 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15220 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15221 return (mp); 15222 } 15223 /* If app asked for dest headers and it has changed ... */ 15224 if ((tcp->tcp_ipv6_recvancillary & 15225 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15226 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15227 (ipp->ipp_fields & IPPF_DSTOPTS), 15228 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15229 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15230 addflag |= TCP_IPV6_RECVDSTOPTS; 15231 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15232 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15233 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15234 return (mp); 15235 } 15236 15237 if (optlen == 0) { 15238 /* Nothing to add */ 15239 return (mp); 15240 } 15241 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15242 if (mp1 == NULL) { 15243 /* 15244 * Defer sending ancillary data until the next TCP segment 15245 * arrives. 15246 */ 15247 return (mp); 15248 } 15249 mp1->b_cont = mp; 15250 mp = mp1; 15251 mp->b_wptr += sizeof (*todi) + optlen; 15252 mp->b_datap->db_type = M_PROTO; 15253 todi = (struct T_optdata_ind *)mp->b_rptr; 15254 todi->PRIM_type = T_OPTDATA_IND; 15255 todi->DATA_flag = 1; /* MORE data */ 15256 todi->OPT_length = optlen; 15257 todi->OPT_offset = sizeof (*todi); 15258 optptr = (uchar_t *)&todi[1]; 15259 /* 15260 * If app asked for pktinfo and the index has changed ... 15261 * Note that the local address never changes for the connection. 15262 */ 15263 if (addflag & TCP_IPV6_RECVPKTINFO) { 15264 struct in6_pktinfo *pkti; 15265 15266 toh = (struct T_opthdr *)optptr; 15267 toh->level = IPPROTO_IPV6; 15268 toh->name = IPV6_PKTINFO; 15269 toh->len = sizeof (*toh) + sizeof (*pkti); 15270 toh->status = 0; 15271 optptr += sizeof (*toh); 15272 pkti = (struct in6_pktinfo *)optptr; 15273 if (tcp->tcp_ipversion == IPV6_VERSION) 15274 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15275 else 15276 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15277 &pkti->ipi6_addr); 15278 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15279 optptr += sizeof (*pkti); 15280 ASSERT(OK_32PTR(optptr)); 15281 /* Save as "last" value */ 15282 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15283 } 15284 /* If app asked for hoplimit and it has changed ... */ 15285 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15286 toh = (struct T_opthdr *)optptr; 15287 toh->level = IPPROTO_IPV6; 15288 toh->name = IPV6_HOPLIMIT; 15289 toh->len = sizeof (*toh) + sizeof (uint_t); 15290 toh->status = 0; 15291 optptr += sizeof (*toh); 15292 *(uint_t *)optptr = ipp->ipp_hoplimit; 15293 optptr += sizeof (uint_t); 15294 ASSERT(OK_32PTR(optptr)); 15295 /* Save as "last" value */ 15296 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15297 } 15298 /* If app asked for tclass and it has changed ... */ 15299 if (addflag & TCP_IPV6_RECVTCLASS) { 15300 toh = (struct T_opthdr *)optptr; 15301 toh->level = IPPROTO_IPV6; 15302 toh->name = IPV6_TCLASS; 15303 toh->len = sizeof (*toh) + sizeof (uint_t); 15304 toh->status = 0; 15305 optptr += sizeof (*toh); 15306 *(uint_t *)optptr = ipp->ipp_tclass; 15307 optptr += sizeof (uint_t); 15308 ASSERT(OK_32PTR(optptr)); 15309 /* Save as "last" value */ 15310 tcp->tcp_recvtclass = ipp->ipp_tclass; 15311 } 15312 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15313 toh = (struct T_opthdr *)optptr; 15314 toh->level = IPPROTO_IPV6; 15315 toh->name = IPV6_HOPOPTS; 15316 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15317 tcp->tcp_label_len; 15318 toh->status = 0; 15319 optptr += sizeof (*toh); 15320 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15321 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15322 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15323 ASSERT(OK_32PTR(optptr)); 15324 /* Save as last value */ 15325 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15326 (ipp->ipp_fields & IPPF_HOPOPTS), 15327 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15328 } 15329 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15330 toh = (struct T_opthdr *)optptr; 15331 toh->level = IPPROTO_IPV6; 15332 toh->name = IPV6_RTHDRDSTOPTS; 15333 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15334 toh->status = 0; 15335 optptr += sizeof (*toh); 15336 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15337 optptr += ipp->ipp_rtdstoptslen; 15338 ASSERT(OK_32PTR(optptr)); 15339 /* Save as last value */ 15340 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15341 &tcp->tcp_rtdstoptslen, 15342 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15343 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15344 } 15345 if (addflag & TCP_IPV6_RECVRTHDR) { 15346 toh = (struct T_opthdr *)optptr; 15347 toh->level = IPPROTO_IPV6; 15348 toh->name = IPV6_RTHDR; 15349 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15350 toh->status = 0; 15351 optptr += sizeof (*toh); 15352 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15353 optptr += ipp->ipp_rthdrlen; 15354 ASSERT(OK_32PTR(optptr)); 15355 /* Save as last value */ 15356 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15357 (ipp->ipp_fields & IPPF_RTHDR), 15358 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15359 } 15360 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15361 toh = (struct T_opthdr *)optptr; 15362 toh->level = IPPROTO_IPV6; 15363 toh->name = IPV6_DSTOPTS; 15364 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15365 toh->status = 0; 15366 optptr += sizeof (*toh); 15367 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15368 optptr += ipp->ipp_dstoptslen; 15369 ASSERT(OK_32PTR(optptr)); 15370 /* Save as last value */ 15371 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15372 (ipp->ipp_fields & IPPF_DSTOPTS), 15373 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15374 } 15375 ASSERT(optptr == mp->b_wptr); 15376 return (mp); 15377 } 15378 15379 /* 15380 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15381 * messages. 15382 */ 15383 void 15384 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15385 { 15386 uchar_t *rptr = mp->b_rptr; 15387 queue_t *q = tcp->tcp_rq; 15388 struct T_error_ack *tea; 15389 15390 switch (mp->b_datap->db_type) { 15391 case M_PROTO: 15392 case M_PCPROTO: 15393 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15394 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15395 break; 15396 tea = (struct T_error_ack *)rptr; 15397 ASSERT(tea->PRIM_type != T_BIND_ACK); 15398 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15399 tea->ERROR_prim != T_BIND_REQ); 15400 switch (tea->PRIM_type) { 15401 case T_ERROR_ACK: 15402 if (tcp->tcp_debug) { 15403 (void) strlog(TCP_MOD_ID, 0, 1, 15404 SL_TRACE|SL_ERROR, 15405 "tcp_rput_other: case T_ERROR_ACK, " 15406 "ERROR_prim == %d", 15407 tea->ERROR_prim); 15408 } 15409 switch (tea->ERROR_prim) { 15410 case T_SVR4_OPTMGMT_REQ: 15411 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15412 /* T_OPTMGMT_REQ generated by TCP */ 15413 printf("T_SVR4_OPTMGMT_REQ failed " 15414 "%d/%d - dropped (cnt %d)\n", 15415 tea->TLI_error, tea->UNIX_error, 15416 tcp->tcp_drop_opt_ack_cnt); 15417 freemsg(mp); 15418 tcp->tcp_drop_opt_ack_cnt--; 15419 return; 15420 } 15421 break; 15422 } 15423 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15424 tcp->tcp_drop_opt_ack_cnt > 0) { 15425 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15426 "- dropped (cnt %d)\n", 15427 tea->TLI_error, tea->UNIX_error, 15428 tcp->tcp_drop_opt_ack_cnt); 15429 freemsg(mp); 15430 tcp->tcp_drop_opt_ack_cnt--; 15431 return; 15432 } 15433 break; 15434 case T_OPTMGMT_ACK: 15435 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15436 /* T_OPTMGMT_REQ generated by TCP */ 15437 freemsg(mp); 15438 tcp->tcp_drop_opt_ack_cnt--; 15439 return; 15440 } 15441 break; 15442 default: 15443 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15444 break; 15445 } 15446 break; 15447 case M_FLUSH: 15448 if (*rptr & FLUSHR) 15449 flushq(q, FLUSHDATA); 15450 break; 15451 default: 15452 /* M_CTL will be directly sent to tcp_icmp_error() */ 15453 ASSERT(DB_TYPE(mp) != M_CTL); 15454 break; 15455 } 15456 /* 15457 * Make sure we set this bit before sending the ACK for 15458 * bind. Otherwise accept could possibly run and free 15459 * this tcp struct. 15460 */ 15461 ASSERT(q != NULL); 15462 putnext(q, mp); 15463 } 15464 15465 /* ARGSUSED */ 15466 static void 15467 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15468 { 15469 conn_t *connp = (conn_t *)arg; 15470 tcp_t *tcp = connp->conn_tcp; 15471 queue_t *q = tcp->tcp_rq; 15472 tcp_stack_t *tcps = tcp->tcp_tcps; 15473 15474 ASSERT(!IPCL_IS_NONSTR(connp)); 15475 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15476 tcp->tcp_rsrv_mp = mp; 15477 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15478 15479 TCP_STAT(tcps, tcp_rsrv_calls); 15480 15481 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15482 return; 15483 } 15484 15485 if (tcp->tcp_fused) { 15486 tcp_fuse_backenable(tcp); 15487 return; 15488 } 15489 15490 if (canputnext(q)) { 15491 /* Not flow-controlled, open rwnd */ 15492 tcp->tcp_rwnd = q->q_hiwat; 15493 15494 /* 15495 * Send back a window update immediately if TCP is above 15496 * ESTABLISHED state and the increase of the rcv window 15497 * that the other side knows is at least 1 MSS after flow 15498 * control is lifted. 15499 */ 15500 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15501 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 15502 tcp_xmit_ctl(NULL, tcp, 15503 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15504 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15505 } 15506 } 15507 } 15508 15509 /* 15510 * The read side service routine is called mostly when we get back-enabled as a 15511 * result of flow control relief. Since we don't actually queue anything in 15512 * TCP, we have no data to send out of here. What we do is clear the receive 15513 * window, and send out a window update. 15514 */ 15515 static void 15516 tcp_rsrv(queue_t *q) 15517 { 15518 conn_t *connp = Q_TO_CONN(q); 15519 tcp_t *tcp = connp->conn_tcp; 15520 mblk_t *mp; 15521 tcp_stack_t *tcps = tcp->tcp_tcps; 15522 15523 /* No code does a putq on the read side */ 15524 ASSERT(q->q_first == NULL); 15525 15526 /* Nothing to do for the default queue */ 15527 if (q == tcps->tcps_g_q) { 15528 return; 15529 } 15530 15531 /* 15532 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15533 * been run. So just return. 15534 */ 15535 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15536 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15537 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15538 return; 15539 } 15540 tcp->tcp_rsrv_mp = NULL; 15541 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15542 15543 CONN_INC_REF(connp); 15544 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15545 SQ_PROCESS, SQTAG_TCP_RSRV); 15546 } 15547 15548 /* 15549 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15550 * We do not allow the receive window to shrink. After setting rwnd, 15551 * set the flow control hiwat of the stream. 15552 * 15553 * This function is called in 2 cases: 15554 * 15555 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15556 * connection (passive open) and in tcp_rput_data() for active connect. 15557 * This is called after tcp_mss_set() when the desired MSS value is known. 15558 * This makes sure that our window size is a mutiple of the other side's 15559 * MSS. 15560 * 2) Handling SO_RCVBUF option. 15561 * 15562 * It is ASSUMED that the requested size is a multiple of the current MSS. 15563 * 15564 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15565 * user requests so. 15566 */ 15567 static int 15568 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15569 { 15570 uint32_t mss = tcp->tcp_mss; 15571 uint32_t old_max_rwnd; 15572 uint32_t max_transmittable_rwnd; 15573 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15574 tcp_stack_t *tcps = tcp->tcp_tcps; 15575 15576 if (tcp->tcp_fused) { 15577 size_t sth_hiwat; 15578 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15579 15580 ASSERT(peer_tcp != NULL); 15581 /* 15582 * Record the stream head's high water mark for 15583 * this endpoint; this is used for flow-control 15584 * purposes in tcp_fuse_output(). 15585 */ 15586 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15587 if (!tcp_detached) { 15588 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15589 sth_hiwat); 15590 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15591 conn_t *connp = tcp->tcp_connp; 15592 struct sock_proto_props sopp; 15593 15594 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15595 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15596 15597 (*connp->conn_upcalls->su_set_proto_props) 15598 (connp->conn_upper_handle, &sopp); 15599 } 15600 } 15601 15602 /* 15603 * In the fusion case, the maxpsz stream head value of 15604 * our peer is set according to its send buffer size 15605 * and our receive buffer size; since the latter may 15606 * have changed we need to update the peer's maxpsz. 15607 */ 15608 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15609 return (rwnd); 15610 } 15611 15612 if (tcp_detached) { 15613 old_max_rwnd = tcp->tcp_rwnd; 15614 } else { 15615 old_max_rwnd = tcp->tcp_recv_hiwater; 15616 } 15617 15618 /* 15619 * Insist on a receive window that is at least 15620 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15621 * funny TCP interactions of Nagle algorithm, SWS avoidance 15622 * and delayed acknowledgement. 15623 */ 15624 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15625 15626 /* 15627 * If window size info has already been exchanged, TCP should not 15628 * shrink the window. Shrinking window is doable if done carefully. 15629 * We may add that support later. But so far there is not a real 15630 * need to do that. 15631 */ 15632 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15633 /* MSS may have changed, do a round up again. */ 15634 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15635 } 15636 15637 /* 15638 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15639 * can be applied even before the window scale option is decided. 15640 */ 15641 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15642 if (rwnd > max_transmittable_rwnd) { 15643 rwnd = max_transmittable_rwnd - 15644 (max_transmittable_rwnd % mss); 15645 if (rwnd < mss) 15646 rwnd = max_transmittable_rwnd; 15647 /* 15648 * If we're over the limit we may have to back down tcp_rwnd. 15649 * The increment below won't work for us. So we set all three 15650 * here and the increment below will have no effect. 15651 */ 15652 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15653 } 15654 if (tcp->tcp_localnet) { 15655 tcp->tcp_rack_abs_max = 15656 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15657 } else { 15658 /* 15659 * For a remote host on a different subnet (through a router), 15660 * we ack every other packet to be conforming to RFC1122. 15661 * tcp_deferred_acks_max is default to 2. 15662 */ 15663 tcp->tcp_rack_abs_max = 15664 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15665 } 15666 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15667 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15668 else 15669 tcp->tcp_rack_cur_max = 0; 15670 /* 15671 * Increment the current rwnd by the amount the maximum grew (we 15672 * can not overwrite it since we might be in the middle of a 15673 * connection.) 15674 */ 15675 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15676 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15677 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15678 tcp->tcp_cwnd_max = rwnd; 15679 15680 if (tcp_detached) 15681 return (rwnd); 15682 /* 15683 * We set the maximum receive window into rq->q_hiwat if it is 15684 * a STREAMS socket. 15685 * This is not actually used for flow control. 15686 */ 15687 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15688 tcp->tcp_rq->q_hiwat = rwnd; 15689 tcp->tcp_recv_hiwater = rwnd; 15690 /* 15691 * Set the STREAM head high water mark. This doesn't have to be 15692 * here, since we are simply using default values, but we would 15693 * prefer to choose these values algorithmically, with a likely 15694 * relationship to rwnd. 15695 */ 15696 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15697 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15698 return (rwnd); 15699 } 15700 15701 /* 15702 * Return SNMP stuff in buffer in mpdata. 15703 */ 15704 mblk_t * 15705 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15706 { 15707 mblk_t *mpdata; 15708 mblk_t *mp_conn_ctl = NULL; 15709 mblk_t *mp_conn_tail; 15710 mblk_t *mp_attr_ctl = NULL; 15711 mblk_t *mp_attr_tail; 15712 mblk_t *mp6_conn_ctl = NULL; 15713 mblk_t *mp6_conn_tail; 15714 mblk_t *mp6_attr_ctl = NULL; 15715 mblk_t *mp6_attr_tail; 15716 struct opthdr *optp; 15717 mib2_tcpConnEntry_t tce; 15718 mib2_tcp6ConnEntry_t tce6; 15719 mib2_transportMLPEntry_t mlp; 15720 connf_t *connfp; 15721 int i; 15722 boolean_t ispriv; 15723 zoneid_t zoneid; 15724 int v4_conn_idx; 15725 int v6_conn_idx; 15726 conn_t *connp = Q_TO_CONN(q); 15727 tcp_stack_t *tcps; 15728 ip_stack_t *ipst; 15729 mblk_t *mp2ctl; 15730 15731 /* 15732 * make a copy of the original message 15733 */ 15734 mp2ctl = copymsg(mpctl); 15735 15736 if (mpctl == NULL || 15737 (mpdata = mpctl->b_cont) == NULL || 15738 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15739 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15740 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15741 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15742 freemsg(mp_conn_ctl); 15743 freemsg(mp_attr_ctl); 15744 freemsg(mp6_conn_ctl); 15745 freemsg(mp6_attr_ctl); 15746 freemsg(mpctl); 15747 freemsg(mp2ctl); 15748 return (NULL); 15749 } 15750 15751 ipst = connp->conn_netstack->netstack_ip; 15752 tcps = connp->conn_netstack->netstack_tcp; 15753 15754 /* build table of connections -- need count in fixed part */ 15755 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15756 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15757 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15758 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15759 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15760 15761 ispriv = 15762 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15763 zoneid = Q_TO_CONN(q)->conn_zoneid; 15764 15765 v4_conn_idx = v6_conn_idx = 0; 15766 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15767 15768 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15769 ipst = tcps->tcps_netstack->netstack_ip; 15770 15771 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15772 15773 connp = NULL; 15774 15775 while ((connp = 15776 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15777 tcp_t *tcp; 15778 boolean_t needattr; 15779 15780 if (connp->conn_zoneid != zoneid) 15781 continue; /* not in this zone */ 15782 15783 tcp = connp->conn_tcp; 15784 UPDATE_MIB(&tcps->tcps_mib, 15785 tcpHCInSegs, tcp->tcp_ibsegs); 15786 tcp->tcp_ibsegs = 0; 15787 UPDATE_MIB(&tcps->tcps_mib, 15788 tcpHCOutSegs, tcp->tcp_obsegs); 15789 tcp->tcp_obsegs = 0; 15790 15791 tce6.tcp6ConnState = tce.tcpConnState = 15792 tcp_snmp_state(tcp); 15793 if (tce.tcpConnState == MIB2_TCP_established || 15794 tce.tcpConnState == MIB2_TCP_closeWait) 15795 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15796 15797 needattr = B_FALSE; 15798 bzero(&mlp, sizeof (mlp)); 15799 if (connp->conn_mlp_type != mlptSingle) { 15800 if (connp->conn_mlp_type == mlptShared || 15801 connp->conn_mlp_type == mlptBoth) 15802 mlp.tme_flags |= MIB2_TMEF_SHARED; 15803 if (connp->conn_mlp_type == mlptPrivate || 15804 connp->conn_mlp_type == mlptBoth) 15805 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15806 needattr = B_TRUE; 15807 } 15808 if (connp->conn_peercred != NULL) { 15809 ts_label_t *tsl; 15810 15811 tsl = crgetlabel(connp->conn_peercred); 15812 mlp.tme_doi = label2doi(tsl); 15813 mlp.tme_label = *label2bslabel(tsl); 15814 needattr = B_TRUE; 15815 } 15816 15817 /* Create a message to report on IPv6 entries */ 15818 if (tcp->tcp_ipversion == IPV6_VERSION) { 15819 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15820 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15821 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15822 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15823 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15824 /* Don't want just anybody seeing these... */ 15825 if (ispriv) { 15826 tce6.tcp6ConnEntryInfo.ce_snxt = 15827 tcp->tcp_snxt; 15828 tce6.tcp6ConnEntryInfo.ce_suna = 15829 tcp->tcp_suna; 15830 tce6.tcp6ConnEntryInfo.ce_rnxt = 15831 tcp->tcp_rnxt; 15832 tce6.tcp6ConnEntryInfo.ce_rack = 15833 tcp->tcp_rack; 15834 } else { 15835 /* 15836 * Netstat, unfortunately, uses this to 15837 * get send/receive queue sizes. How to fix? 15838 * Why not compute the difference only? 15839 */ 15840 tce6.tcp6ConnEntryInfo.ce_snxt = 15841 tcp->tcp_snxt - tcp->tcp_suna; 15842 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15843 tce6.tcp6ConnEntryInfo.ce_rnxt = 15844 tcp->tcp_rnxt - tcp->tcp_rack; 15845 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15846 } 15847 15848 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15849 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15850 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15851 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15852 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15853 15854 tce6.tcp6ConnCreationProcess = 15855 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15856 tcp->tcp_cpid; 15857 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 15858 15859 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15860 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15861 15862 mlp.tme_connidx = v6_conn_idx++; 15863 if (needattr) 15864 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15865 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15866 } 15867 /* 15868 * Create an IPv4 table entry for IPv4 entries and also 15869 * for IPv6 entries which are bound to in6addr_any 15870 * but don't have IPV6_V6ONLY set. 15871 * (i.e. anything an IPv4 peer could connect to) 15872 */ 15873 if (tcp->tcp_ipversion == IPV4_VERSION || 15874 (tcp->tcp_state <= TCPS_LISTEN && 15875 !tcp->tcp_connp->conn_ipv6_v6only && 15876 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15877 if (tcp->tcp_ipversion == IPV6_VERSION) { 15878 tce.tcpConnRemAddress = INADDR_ANY; 15879 tce.tcpConnLocalAddress = INADDR_ANY; 15880 } else { 15881 tce.tcpConnRemAddress = 15882 tcp->tcp_remote; 15883 tce.tcpConnLocalAddress = 15884 tcp->tcp_ip_src; 15885 } 15886 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15887 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15888 /* Don't want just anybody seeing these... */ 15889 if (ispriv) { 15890 tce.tcpConnEntryInfo.ce_snxt = 15891 tcp->tcp_snxt; 15892 tce.tcpConnEntryInfo.ce_suna = 15893 tcp->tcp_suna; 15894 tce.tcpConnEntryInfo.ce_rnxt = 15895 tcp->tcp_rnxt; 15896 tce.tcpConnEntryInfo.ce_rack = 15897 tcp->tcp_rack; 15898 } else { 15899 /* 15900 * Netstat, unfortunately, uses this to 15901 * get send/receive queue sizes. How 15902 * to fix? 15903 * Why not compute the difference only? 15904 */ 15905 tce.tcpConnEntryInfo.ce_snxt = 15906 tcp->tcp_snxt - tcp->tcp_suna; 15907 tce.tcpConnEntryInfo.ce_suna = 0; 15908 tce.tcpConnEntryInfo.ce_rnxt = 15909 tcp->tcp_rnxt - tcp->tcp_rack; 15910 tce.tcpConnEntryInfo.ce_rack = 0; 15911 } 15912 15913 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15914 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15915 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15916 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15917 tce.tcpConnEntryInfo.ce_state = 15918 tcp->tcp_state; 15919 15920 tce.tcpConnCreationProcess = 15921 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15922 tcp->tcp_cpid; 15923 tce.tcpConnCreationTime = tcp->tcp_open_time; 15924 15925 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15926 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15927 15928 mlp.tme_connidx = v4_conn_idx++; 15929 if (needattr) 15930 (void) snmp_append_data2( 15931 mp_attr_ctl->b_cont, 15932 &mp_attr_tail, (char *)&mlp, 15933 sizeof (mlp)); 15934 } 15935 } 15936 } 15937 15938 /* fixed length structure for IPv4 and IPv6 counters */ 15939 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15940 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 15941 sizeof (mib2_tcp6ConnEntry_t)); 15942 /* synchronize 32- and 64-bit counters */ 15943 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 15944 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 15945 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15946 optp->level = MIB2_TCP; 15947 optp->name = 0; 15948 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 15949 sizeof (tcps->tcps_mib)); 15950 optp->len = msgdsize(mpdata); 15951 qreply(q, mpctl); 15952 15953 /* table of connections... */ 15954 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 15955 sizeof (struct T_optmgmt_ack)]; 15956 optp->level = MIB2_TCP; 15957 optp->name = MIB2_TCP_CONN; 15958 optp->len = msgdsize(mp_conn_ctl->b_cont); 15959 qreply(q, mp_conn_ctl); 15960 15961 /* table of MLP attributes... */ 15962 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 15963 sizeof (struct T_optmgmt_ack)]; 15964 optp->level = MIB2_TCP; 15965 optp->name = EXPER_XPORT_MLP; 15966 optp->len = msgdsize(mp_attr_ctl->b_cont); 15967 if (optp->len == 0) 15968 freemsg(mp_attr_ctl); 15969 else 15970 qreply(q, mp_attr_ctl); 15971 15972 /* table of IPv6 connections... */ 15973 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 15974 sizeof (struct T_optmgmt_ack)]; 15975 optp->level = MIB2_TCP6; 15976 optp->name = MIB2_TCP6_CONN; 15977 optp->len = msgdsize(mp6_conn_ctl->b_cont); 15978 qreply(q, mp6_conn_ctl); 15979 15980 /* table of IPv6 MLP attributes... */ 15981 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 15982 sizeof (struct T_optmgmt_ack)]; 15983 optp->level = MIB2_TCP6; 15984 optp->name = EXPER_XPORT_MLP; 15985 optp->len = msgdsize(mp6_attr_ctl->b_cont); 15986 if (optp->len == 0) 15987 freemsg(mp6_attr_ctl); 15988 else 15989 qreply(q, mp6_attr_ctl); 15990 return (mp2ctl); 15991 } 15992 15993 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 15994 /* ARGSUSED */ 15995 int 15996 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 15997 { 15998 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 15999 16000 switch (level) { 16001 case MIB2_TCP: 16002 switch (name) { 16003 case 13: 16004 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16005 return (0); 16006 /* TODO: delete entry defined by tce */ 16007 return (1); 16008 default: 16009 return (0); 16010 } 16011 default: 16012 return (1); 16013 } 16014 } 16015 16016 /* Translate TCP state to MIB2 TCP state. */ 16017 static int 16018 tcp_snmp_state(tcp_t *tcp) 16019 { 16020 if (tcp == NULL) 16021 return (0); 16022 16023 switch (tcp->tcp_state) { 16024 case TCPS_CLOSED: 16025 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16026 case TCPS_BOUND: 16027 return (MIB2_TCP_closed); 16028 case TCPS_LISTEN: 16029 return (MIB2_TCP_listen); 16030 case TCPS_SYN_SENT: 16031 return (MIB2_TCP_synSent); 16032 case TCPS_SYN_RCVD: 16033 return (MIB2_TCP_synReceived); 16034 case TCPS_ESTABLISHED: 16035 return (MIB2_TCP_established); 16036 case TCPS_CLOSE_WAIT: 16037 return (MIB2_TCP_closeWait); 16038 case TCPS_FIN_WAIT_1: 16039 return (MIB2_TCP_finWait1); 16040 case TCPS_CLOSING: 16041 return (MIB2_TCP_closing); 16042 case TCPS_LAST_ACK: 16043 return (MIB2_TCP_lastAck); 16044 case TCPS_FIN_WAIT_2: 16045 return (MIB2_TCP_finWait2); 16046 case TCPS_TIME_WAIT: 16047 return (MIB2_TCP_timeWait); 16048 default: 16049 return (0); 16050 } 16051 } 16052 16053 /* 16054 * tcp_timer is the timer service routine. It handles the retransmission, 16055 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16056 * from the state of the tcp instance what kind of action needs to be done 16057 * at the time it is called. 16058 */ 16059 static void 16060 tcp_timer(void *arg) 16061 { 16062 mblk_t *mp; 16063 clock_t first_threshold; 16064 clock_t second_threshold; 16065 clock_t ms; 16066 uint32_t mss; 16067 conn_t *connp = (conn_t *)arg; 16068 tcp_t *tcp = connp->conn_tcp; 16069 tcp_stack_t *tcps = tcp->tcp_tcps; 16070 16071 tcp->tcp_timer_tid = 0; 16072 16073 if (tcp->tcp_fused) 16074 return; 16075 16076 first_threshold = tcp->tcp_first_timer_threshold; 16077 second_threshold = tcp->tcp_second_timer_threshold; 16078 switch (tcp->tcp_state) { 16079 case TCPS_IDLE: 16080 case TCPS_BOUND: 16081 case TCPS_LISTEN: 16082 return; 16083 case TCPS_SYN_RCVD: { 16084 tcp_t *listener = tcp->tcp_listener; 16085 16086 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16087 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16088 /* it's our first timeout */ 16089 tcp->tcp_syn_rcvd_timeout = 1; 16090 mutex_enter(&listener->tcp_eager_lock); 16091 listener->tcp_syn_rcvd_timeout++; 16092 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16093 /* 16094 * Make this eager available for drop if we 16095 * need to drop one to accomodate a new 16096 * incoming SYN request. 16097 */ 16098 MAKE_DROPPABLE(listener, tcp); 16099 } 16100 if (!listener->tcp_syn_defense && 16101 (listener->tcp_syn_rcvd_timeout > 16102 (tcps->tcps_conn_req_max_q0 >> 2)) && 16103 (tcps->tcps_conn_req_max_q0 > 200)) { 16104 /* We may be under attack. Put on a defense. */ 16105 listener->tcp_syn_defense = B_TRUE; 16106 cmn_err(CE_WARN, "High TCP connect timeout " 16107 "rate! System (port %d) may be under a " 16108 "SYN flood attack!", 16109 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16110 16111 listener->tcp_ip_addr_cache = kmem_zalloc( 16112 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16113 KM_NOSLEEP); 16114 } 16115 mutex_exit(&listener->tcp_eager_lock); 16116 } else if (listener != NULL) { 16117 mutex_enter(&listener->tcp_eager_lock); 16118 tcp->tcp_syn_rcvd_timeout++; 16119 if (tcp->tcp_syn_rcvd_timeout > 1 && 16120 !tcp->tcp_closemp_used) { 16121 /* 16122 * This is our second timeout. Put the tcp in 16123 * the list of droppable eagers to allow it to 16124 * be dropped, if needed. We don't check 16125 * whether tcp_dontdrop is set or not to 16126 * protect ourselve from a SYN attack where a 16127 * remote host can spoof itself as one of the 16128 * good IP source and continue to hold 16129 * resources too long. 16130 */ 16131 MAKE_DROPPABLE(listener, tcp); 16132 } 16133 mutex_exit(&listener->tcp_eager_lock); 16134 } 16135 } 16136 /* FALLTHRU */ 16137 case TCPS_SYN_SENT: 16138 first_threshold = tcp->tcp_first_ctimer_threshold; 16139 second_threshold = tcp->tcp_second_ctimer_threshold; 16140 break; 16141 case TCPS_ESTABLISHED: 16142 case TCPS_FIN_WAIT_1: 16143 case TCPS_CLOSING: 16144 case TCPS_CLOSE_WAIT: 16145 case TCPS_LAST_ACK: 16146 /* If we have data to rexmit */ 16147 if (tcp->tcp_suna != tcp->tcp_snxt) { 16148 clock_t time_to_wait; 16149 16150 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16151 if (!tcp->tcp_xmit_head) 16152 break; 16153 time_to_wait = lbolt - 16154 (clock_t)tcp->tcp_xmit_head->b_prev; 16155 time_to_wait = tcp->tcp_rto - 16156 TICK_TO_MSEC(time_to_wait); 16157 /* 16158 * If the timer fires too early, 1 clock tick earlier, 16159 * restart the timer. 16160 */ 16161 if (time_to_wait > msec_per_tick) { 16162 TCP_STAT(tcps, tcp_timer_fire_early); 16163 TCP_TIMER_RESTART(tcp, time_to_wait); 16164 return; 16165 } 16166 /* 16167 * When we probe zero windows, we force the swnd open. 16168 * If our peer acks with a closed window swnd will be 16169 * set to zero by tcp_rput(). As long as we are 16170 * receiving acks tcp_rput will 16171 * reset 'tcp_ms_we_have_waited' so as not to trip the 16172 * first and second interval actions. NOTE: the timer 16173 * interval is allowed to continue its exponential 16174 * backoff. 16175 */ 16176 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16177 if (tcp->tcp_debug) { 16178 (void) strlog(TCP_MOD_ID, 0, 1, 16179 SL_TRACE, "tcp_timer: zero win"); 16180 } 16181 } else { 16182 /* 16183 * After retransmission, we need to do 16184 * slow start. Set the ssthresh to one 16185 * half of current effective window and 16186 * cwnd to one MSS. Also reset 16187 * tcp_cwnd_cnt. 16188 * 16189 * Note that if tcp_ssthresh is reduced because 16190 * of ECN, do not reduce it again unless it is 16191 * already one window of data away (tcp_cwr 16192 * should then be cleared) or this is a 16193 * timeout for a retransmitted segment. 16194 */ 16195 uint32_t npkt; 16196 16197 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16198 npkt = ((tcp->tcp_timer_backoff ? 16199 tcp->tcp_cwnd_ssthresh : 16200 tcp->tcp_snxt - 16201 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16202 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16203 tcp->tcp_mss; 16204 } 16205 tcp->tcp_cwnd = tcp->tcp_mss; 16206 tcp->tcp_cwnd_cnt = 0; 16207 if (tcp->tcp_ecn_ok) { 16208 tcp->tcp_cwr = B_TRUE; 16209 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16210 tcp->tcp_ecn_cwr_sent = B_FALSE; 16211 } 16212 } 16213 break; 16214 } 16215 /* 16216 * We have something to send yet we cannot send. The 16217 * reason can be: 16218 * 16219 * 1. Zero send window: we need to do zero window probe. 16220 * 2. Zero cwnd: because of ECN, we need to "clock out 16221 * segments. 16222 * 3. SWS avoidance: receiver may have shrunk window, 16223 * reset our knowledge. 16224 * 16225 * Note that condition 2 can happen with either 1 or 16226 * 3. But 1 and 3 are exclusive. 16227 */ 16228 if (tcp->tcp_unsent != 0) { 16229 if (tcp->tcp_cwnd == 0) { 16230 /* 16231 * Set tcp_cwnd to 1 MSS so that a 16232 * new segment can be sent out. We 16233 * are "clocking out" new data when 16234 * the network is really congested. 16235 */ 16236 ASSERT(tcp->tcp_ecn_ok); 16237 tcp->tcp_cwnd = tcp->tcp_mss; 16238 } 16239 if (tcp->tcp_swnd == 0) { 16240 /* Extend window for zero window probe */ 16241 tcp->tcp_swnd++; 16242 tcp->tcp_zero_win_probe = B_TRUE; 16243 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16244 } else { 16245 /* 16246 * Handle timeout from sender SWS avoidance. 16247 * Reset our knowledge of the max send window 16248 * since the receiver might have reduced its 16249 * receive buffer. Avoid setting tcp_max_swnd 16250 * to one since that will essentially disable 16251 * the SWS checks. 16252 * 16253 * Note that since we don't have a SWS 16254 * state variable, if the timeout is set 16255 * for ECN but not for SWS, this 16256 * code will also be executed. This is 16257 * fine as tcp_max_swnd is updated 16258 * constantly and it will not affect 16259 * anything. 16260 */ 16261 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16262 } 16263 tcp_wput_data(tcp, NULL, B_FALSE); 16264 return; 16265 } 16266 /* Is there a FIN that needs to be to re retransmitted? */ 16267 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16268 !tcp->tcp_fin_acked) 16269 break; 16270 /* Nothing to do, return without restarting timer. */ 16271 TCP_STAT(tcps, tcp_timer_fire_miss); 16272 return; 16273 case TCPS_FIN_WAIT_2: 16274 /* 16275 * User closed the TCP endpoint and peer ACK'ed our FIN. 16276 * We waited some time for for peer's FIN, but it hasn't 16277 * arrived. We flush the connection now to avoid 16278 * case where the peer has rebooted. 16279 */ 16280 if (TCP_IS_DETACHED(tcp)) { 16281 (void) tcp_clean_death(tcp, 0, 23); 16282 } else { 16283 TCP_TIMER_RESTART(tcp, 16284 tcps->tcps_fin_wait_2_flush_interval); 16285 } 16286 return; 16287 case TCPS_TIME_WAIT: 16288 (void) tcp_clean_death(tcp, 0, 24); 16289 return; 16290 default: 16291 if (tcp->tcp_debug) { 16292 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16293 "tcp_timer: strange state (%d) %s", 16294 tcp->tcp_state, tcp_display(tcp, NULL, 16295 DISP_PORT_ONLY)); 16296 } 16297 return; 16298 } 16299 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16300 /* 16301 * For zero window probe, we need to send indefinitely, 16302 * unless we have not heard from the other side for some 16303 * time... 16304 */ 16305 if ((tcp->tcp_zero_win_probe == 0) || 16306 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16307 second_threshold)) { 16308 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16309 /* 16310 * If TCP is in SYN_RCVD state, send back a 16311 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16312 * should be zero in TCPS_SYN_RCVD state. 16313 */ 16314 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16315 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16316 "in SYN_RCVD", 16317 tcp, tcp->tcp_snxt, 16318 tcp->tcp_rnxt, TH_RST | TH_ACK); 16319 } 16320 (void) tcp_clean_death(tcp, 16321 tcp->tcp_client_errno ? 16322 tcp->tcp_client_errno : ETIMEDOUT, 25); 16323 return; 16324 } else { 16325 /* 16326 * Set tcp_ms_we_have_waited to second_threshold 16327 * so that in next timeout, we will do the above 16328 * check (lbolt - tcp_last_recv_time). This is 16329 * also to avoid overflow. 16330 * 16331 * We don't need to decrement tcp_timer_backoff 16332 * to avoid overflow because it will be decremented 16333 * later if new timeout value is greater than 16334 * tcp_rexmit_interval_max. In the case when 16335 * tcp_rexmit_interval_max is greater than 16336 * second_threshold, it means that we will wait 16337 * longer than second_threshold to send the next 16338 * window probe. 16339 */ 16340 tcp->tcp_ms_we_have_waited = second_threshold; 16341 } 16342 } else if (ms > first_threshold) { 16343 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16344 tcp->tcp_xmit_head != NULL) { 16345 tcp->tcp_xmit_head = 16346 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16347 } 16348 /* 16349 * We have been retransmitting for too long... The RTT 16350 * we calculated is probably incorrect. Reinitialize it. 16351 * Need to compensate for 0 tcp_rtt_sa. Reset 16352 * tcp_rtt_update so that we won't accidentally cache a 16353 * bad value. But only do this if this is not a zero 16354 * window probe. 16355 */ 16356 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16357 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16358 (tcp->tcp_rtt_sa >> 5); 16359 tcp->tcp_rtt_sa = 0; 16360 tcp_ip_notify(tcp); 16361 tcp->tcp_rtt_update = 0; 16362 } 16363 } 16364 tcp->tcp_timer_backoff++; 16365 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16366 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16367 tcps->tcps_rexmit_interval_min) { 16368 /* 16369 * This means the original RTO is tcp_rexmit_interval_min. 16370 * So we will use tcp_rexmit_interval_min as the RTO value 16371 * and do the backoff. 16372 */ 16373 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16374 } else { 16375 ms <<= tcp->tcp_timer_backoff; 16376 } 16377 if (ms > tcps->tcps_rexmit_interval_max) { 16378 ms = tcps->tcps_rexmit_interval_max; 16379 /* 16380 * ms is at max, decrement tcp_timer_backoff to avoid 16381 * overflow. 16382 */ 16383 tcp->tcp_timer_backoff--; 16384 } 16385 tcp->tcp_ms_we_have_waited += ms; 16386 if (tcp->tcp_zero_win_probe == 0) { 16387 tcp->tcp_rto = ms; 16388 } 16389 TCP_TIMER_RESTART(tcp, ms); 16390 /* 16391 * This is after a timeout and tcp_rto is backed off. Set 16392 * tcp_set_timer to 1 so that next time RTO is updated, we will 16393 * restart the timer with a correct value. 16394 */ 16395 tcp->tcp_set_timer = 1; 16396 mss = tcp->tcp_snxt - tcp->tcp_suna; 16397 if (mss > tcp->tcp_mss) 16398 mss = tcp->tcp_mss; 16399 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16400 mss = tcp->tcp_swnd; 16401 16402 if ((mp = tcp->tcp_xmit_head) != NULL) 16403 mp->b_prev = (mblk_t *)lbolt; 16404 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16405 B_TRUE); 16406 16407 /* 16408 * When slow start after retransmission begins, start with 16409 * this seq no. tcp_rexmit_max marks the end of special slow 16410 * start phase. tcp_snd_burst controls how many segments 16411 * can be sent because of an ack. 16412 */ 16413 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16414 tcp->tcp_snd_burst = TCP_CWND_SS; 16415 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16416 (tcp->tcp_unsent == 0)) { 16417 tcp->tcp_rexmit_max = tcp->tcp_fss; 16418 } else { 16419 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16420 } 16421 tcp->tcp_rexmit = B_TRUE; 16422 tcp->tcp_dupack_cnt = 0; 16423 16424 /* 16425 * Remove all rexmit SACK blk to start from fresh. 16426 */ 16427 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16428 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16429 tcp->tcp_num_notsack_blk = 0; 16430 tcp->tcp_cnt_notsack_list = 0; 16431 } 16432 if (mp == NULL) { 16433 return; 16434 } 16435 /* 16436 * Attach credentials to retransmitted initial SYNs. 16437 * In theory we should use the credentials from the connect() 16438 * call to ensure that getpeerucred() on the peer will be correct. 16439 * But we assume that SYN's are not dropped for loopback connections. 16440 */ 16441 if (tcp->tcp_state == TCPS_SYN_SENT) { 16442 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 16443 } 16444 16445 tcp->tcp_csuna = tcp->tcp_snxt; 16446 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16447 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16448 tcp_send_data(tcp, tcp->tcp_wq, mp); 16449 16450 } 16451 16452 static int 16453 tcp_do_unbind(conn_t *connp) 16454 { 16455 tcp_t *tcp = connp->conn_tcp; 16456 int error = 0; 16457 16458 switch (tcp->tcp_state) { 16459 case TCPS_BOUND: 16460 case TCPS_LISTEN: 16461 break; 16462 default: 16463 return (-TOUTSTATE); 16464 } 16465 16466 /* 16467 * Need to clean up all the eagers since after the unbind, segments 16468 * will no longer be delivered to this listener stream. 16469 */ 16470 mutex_enter(&tcp->tcp_eager_lock); 16471 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16472 tcp_eager_cleanup(tcp, 0); 16473 } 16474 mutex_exit(&tcp->tcp_eager_lock); 16475 16476 if (tcp->tcp_ipversion == IPV4_VERSION) { 16477 tcp->tcp_ipha->ipha_src = 0; 16478 } else { 16479 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16480 } 16481 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16482 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16483 tcp_bind_hash_remove(tcp); 16484 tcp->tcp_state = TCPS_IDLE; 16485 tcp->tcp_mdt = B_FALSE; 16486 16487 connp = tcp->tcp_connp; 16488 connp->conn_mdt_ok = B_FALSE; 16489 ipcl_hash_remove(connp); 16490 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16491 16492 return (error); 16493 } 16494 16495 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16496 static void 16497 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16498 { 16499 int error = tcp_do_unbind(tcp->tcp_connp); 16500 16501 if (error > 0) { 16502 tcp_err_ack(tcp, mp, TSYSERR, error); 16503 } else if (error < 0) { 16504 tcp_err_ack(tcp, mp, -error, 0); 16505 } else { 16506 /* Send M_FLUSH according to TPI */ 16507 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16508 16509 mp = mi_tpi_ok_ack_alloc(mp); 16510 putnext(tcp->tcp_rq, mp); 16511 } 16512 } 16513 16514 /* 16515 * Don't let port fall into the privileged range. 16516 * Since the extra privileged ports can be arbitrary we also 16517 * ensure that we exclude those from consideration. 16518 * tcp_g_epriv_ports is not sorted thus we loop over it until 16519 * there are no changes. 16520 * 16521 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16522 * but instead the code relies on: 16523 * - the fact that the address of the array and its size never changes 16524 * - the atomic assignment of the elements of the array 16525 * 16526 * Returns 0 if there are no more ports available. 16527 * 16528 * TS note: skip multilevel ports. 16529 */ 16530 static in_port_t 16531 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16532 { 16533 int i; 16534 boolean_t restart = B_FALSE; 16535 tcp_stack_t *tcps = tcp->tcp_tcps; 16536 16537 if (random && tcp_random_anon_port != 0) { 16538 (void) random_get_pseudo_bytes((uint8_t *)&port, 16539 sizeof (in_port_t)); 16540 /* 16541 * Unless changed by a sys admin, the smallest anon port 16542 * is 32768 and the largest anon port is 65535. It is 16543 * very likely (50%) for the random port to be smaller 16544 * than the smallest anon port. When that happens, 16545 * add port % (anon port range) to the smallest anon 16546 * port to get the random port. It should fall into the 16547 * valid anon port range. 16548 */ 16549 if (port < tcps->tcps_smallest_anon_port) { 16550 port = tcps->tcps_smallest_anon_port + 16551 port % (tcps->tcps_largest_anon_port - 16552 tcps->tcps_smallest_anon_port); 16553 } 16554 } 16555 16556 retry: 16557 if (port < tcps->tcps_smallest_anon_port) 16558 port = (in_port_t)tcps->tcps_smallest_anon_port; 16559 16560 if (port > tcps->tcps_largest_anon_port) { 16561 if (restart) 16562 return (0); 16563 restart = B_TRUE; 16564 port = (in_port_t)tcps->tcps_smallest_anon_port; 16565 } 16566 16567 if (port < tcps->tcps_smallest_nonpriv_port) 16568 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16569 16570 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16571 if (port == tcps->tcps_g_epriv_ports[i]) { 16572 port++; 16573 /* 16574 * Make sure whether the port is in the 16575 * valid range. 16576 */ 16577 goto retry; 16578 } 16579 } 16580 if (is_system_labeled() && 16581 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16582 IPPROTO_TCP, B_TRUE)) != 0) { 16583 port = i; 16584 goto retry; 16585 } 16586 return (port); 16587 } 16588 16589 /* 16590 * Return the next anonymous port in the privileged port range for 16591 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16592 * downwards. This is the same behavior as documented in the userland 16593 * library call rresvport(3N). 16594 * 16595 * TS note: skip multilevel ports. 16596 */ 16597 static in_port_t 16598 tcp_get_next_priv_port(const tcp_t *tcp) 16599 { 16600 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16601 in_port_t nextport; 16602 boolean_t restart = B_FALSE; 16603 tcp_stack_t *tcps = tcp->tcp_tcps; 16604 retry: 16605 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16606 next_priv_port >= IPPORT_RESERVED) { 16607 next_priv_port = IPPORT_RESERVED - 1; 16608 if (restart) 16609 return (0); 16610 restart = B_TRUE; 16611 } 16612 if (is_system_labeled() && 16613 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16614 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16615 next_priv_port = nextport; 16616 goto retry; 16617 } 16618 return (next_priv_port--); 16619 } 16620 16621 /* The write side r/w procedure. */ 16622 16623 #if CCS_STATS 16624 struct { 16625 struct { 16626 int64_t count, bytes; 16627 } tot, hit; 16628 } wrw_stats; 16629 #endif 16630 16631 /* 16632 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16633 * messages. 16634 */ 16635 /* ARGSUSED */ 16636 static void 16637 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16638 { 16639 conn_t *connp = (conn_t *)arg; 16640 tcp_t *tcp = connp->conn_tcp; 16641 queue_t *q = tcp->tcp_wq; 16642 16643 ASSERT(DB_TYPE(mp) != M_IOCTL); 16644 /* 16645 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16646 * Once the close starts, streamhead and sockfs will not let any data 16647 * packets come down (close ensures that there are no threads using the 16648 * queue and no new threads will come down) but since qprocsoff() 16649 * hasn't happened yet, a M_FLUSH or some non data message might 16650 * get reflected back (in response to our own FLUSHRW) and get 16651 * processed after tcp_close() is done. The conn would still be valid 16652 * because a ref would have added but we need to check the state 16653 * before actually processing the packet. 16654 */ 16655 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16656 freemsg(mp); 16657 return; 16658 } 16659 16660 switch (DB_TYPE(mp)) { 16661 case M_IOCDATA: 16662 tcp_wput_iocdata(tcp, mp); 16663 break; 16664 case M_FLUSH: 16665 tcp_wput_flush(tcp, mp); 16666 break; 16667 default: 16668 CALL_IP_WPUT(connp, q, mp); 16669 break; 16670 } 16671 } 16672 16673 /* 16674 * The TCP fast path write put procedure. 16675 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16676 */ 16677 /* ARGSUSED */ 16678 void 16679 tcp_output(void *arg, mblk_t *mp, void *arg2) 16680 { 16681 int len; 16682 int hdrlen; 16683 int plen; 16684 mblk_t *mp1; 16685 uchar_t *rptr; 16686 uint32_t snxt; 16687 tcph_t *tcph; 16688 struct datab *db; 16689 uint32_t suna; 16690 uint32_t mss; 16691 ipaddr_t *dst; 16692 ipaddr_t *src; 16693 uint32_t sum; 16694 int usable; 16695 conn_t *connp = (conn_t *)arg; 16696 tcp_t *tcp = connp->conn_tcp; 16697 uint32_t msize; 16698 tcp_stack_t *tcps = tcp->tcp_tcps; 16699 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16700 16701 /* 16702 * Try and ASSERT the minimum possible references on the 16703 * conn early enough. Since we are executing on write side, 16704 * the connection is obviously not detached and that means 16705 * there is a ref each for TCP and IP. Since we are behind 16706 * the squeue, the minimum references needed are 3. If the 16707 * conn is in classifier hash list, there should be an 16708 * extra ref for that (we check both the possibilities). 16709 */ 16710 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16711 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16712 16713 ASSERT(DB_TYPE(mp) == M_DATA); 16714 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16715 16716 mutex_enter(&tcp->tcp_non_sq_lock); 16717 tcp->tcp_squeue_bytes -= msize; 16718 mutex_exit(&tcp->tcp_non_sq_lock); 16719 16720 /* Check to see if this connection wants to be re-fused. */ 16721 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16722 if (tcp->tcp_ipversion == IPV4_VERSION) { 16723 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16724 &tcp->tcp_saved_tcph); 16725 } else { 16726 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16727 &tcp->tcp_saved_tcph); 16728 } 16729 } 16730 /* Bypass tcp protocol for fused tcp loopback */ 16731 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16732 return; 16733 16734 mss = tcp->tcp_mss; 16735 if (tcp->tcp_xmit_zc_clean) 16736 mp = tcp_zcopy_backoff(tcp, mp, 0); 16737 16738 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16739 len = (int)(mp->b_wptr - mp->b_rptr); 16740 16741 /* 16742 * Criteria for fast path: 16743 * 16744 * 1. no unsent data 16745 * 2. single mblk in request 16746 * 3. connection established 16747 * 4. data in mblk 16748 * 5. len <= mss 16749 * 6. no tcp_valid bits 16750 */ 16751 if ((tcp->tcp_unsent != 0) || 16752 (tcp->tcp_cork) || 16753 (mp->b_cont != NULL) || 16754 (tcp->tcp_state != TCPS_ESTABLISHED) || 16755 (len == 0) || 16756 (len > mss) || 16757 (tcp->tcp_valid_bits != 0)) { 16758 tcp_wput_data(tcp, mp, B_FALSE); 16759 return; 16760 } 16761 16762 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16763 ASSERT(tcp->tcp_fin_sent == 0); 16764 16765 /* queue new packet onto retransmission queue */ 16766 if (tcp->tcp_xmit_head == NULL) { 16767 tcp->tcp_xmit_head = mp; 16768 } else { 16769 tcp->tcp_xmit_last->b_cont = mp; 16770 } 16771 tcp->tcp_xmit_last = mp; 16772 tcp->tcp_xmit_tail = mp; 16773 16774 /* find out how much we can send */ 16775 /* BEGIN CSTYLED */ 16776 /* 16777 * un-acked usable 16778 * |--------------|-----------------| 16779 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16780 */ 16781 /* END CSTYLED */ 16782 16783 /* start sending from tcp_snxt */ 16784 snxt = tcp->tcp_snxt; 16785 16786 /* 16787 * Check to see if this connection has been idled for some 16788 * time and no ACK is expected. If it is, we need to slow 16789 * start again to get back the connection's "self-clock" as 16790 * described in VJ's paper. 16791 * 16792 * Refer to the comment in tcp_mss_set() for the calculation 16793 * of tcp_cwnd after idle. 16794 */ 16795 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16796 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16797 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16798 } 16799 16800 usable = tcp->tcp_swnd; /* tcp window size */ 16801 if (usable > tcp->tcp_cwnd) 16802 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16803 usable -= snxt; /* subtract stuff already sent */ 16804 suna = tcp->tcp_suna; 16805 usable += suna; 16806 /* usable can be < 0 if the congestion window is smaller */ 16807 if (len > usable) { 16808 /* Can't send complete M_DATA in one shot */ 16809 goto slow; 16810 } 16811 16812 mutex_enter(&tcp->tcp_non_sq_lock); 16813 if (tcp->tcp_flow_stopped && 16814 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16815 tcp_clrqfull(tcp); 16816 } 16817 mutex_exit(&tcp->tcp_non_sq_lock); 16818 16819 /* 16820 * determine if anything to send (Nagle). 16821 * 16822 * 1. len < tcp_mss (i.e. small) 16823 * 2. unacknowledged data present 16824 * 3. len < nagle limit 16825 * 4. last packet sent < nagle limit (previous packet sent) 16826 */ 16827 if ((len < mss) && (snxt != suna) && 16828 (len < (int)tcp->tcp_naglim) && 16829 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16830 /* 16831 * This was the first unsent packet and normally 16832 * mss < xmit_hiwater so there is no need to worry 16833 * about flow control. The next packet will go 16834 * through the flow control check in tcp_wput_data(). 16835 */ 16836 /* leftover work from above */ 16837 tcp->tcp_unsent = len; 16838 tcp->tcp_xmit_tail_unsent = len; 16839 16840 return; 16841 } 16842 16843 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 16844 16845 if (snxt == suna) { 16846 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16847 } 16848 16849 /* we have always sent something */ 16850 tcp->tcp_rack_cnt = 0; 16851 16852 tcp->tcp_snxt = snxt + len; 16853 tcp->tcp_rack = tcp->tcp_rnxt; 16854 16855 if ((mp1 = dupb(mp)) == 0) 16856 goto no_memory; 16857 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 16858 mp->b_next = (mblk_t *)(uintptr_t)snxt; 16859 16860 /* adjust tcp header information */ 16861 tcph = tcp->tcp_tcph; 16862 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 16863 16864 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 16865 sum = (sum >> 16) + (sum & 0xFFFF); 16866 U16_TO_ABE16(sum, tcph->th_sum); 16867 16868 U32_TO_ABE32(snxt, tcph->th_seq); 16869 16870 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 16871 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 16872 BUMP_LOCAL(tcp->tcp_obsegs); 16873 16874 /* Update the latest receive window size in TCP header. */ 16875 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 16876 tcph->th_win); 16877 16878 tcp->tcp_last_sent_len = (ushort_t)len; 16879 16880 plen = len + tcp->tcp_hdr_len; 16881 16882 if (tcp->tcp_ipversion == IPV4_VERSION) { 16883 tcp->tcp_ipha->ipha_length = htons(plen); 16884 } else { 16885 tcp->tcp_ip6h->ip6_plen = htons(plen - 16886 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 16887 } 16888 16889 /* see if we need to allocate a mblk for the headers */ 16890 hdrlen = tcp->tcp_hdr_len; 16891 rptr = mp1->b_rptr - hdrlen; 16892 db = mp1->b_datap; 16893 if ((db->db_ref != 2) || rptr < db->db_base || 16894 (!OK_32PTR(rptr))) { 16895 /* NOTE: we assume allocb returns an OK_32PTR */ 16896 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 16897 tcps->tcps_wroff_xtra, BPRI_MED); 16898 if (!mp) { 16899 freemsg(mp1); 16900 goto no_memory; 16901 } 16902 mp->b_cont = mp1; 16903 mp1 = mp; 16904 /* Leave room for Link Level header */ 16905 /* hdrlen = tcp->tcp_hdr_len; */ 16906 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 16907 mp1->b_wptr = &rptr[hdrlen]; 16908 } 16909 mp1->b_rptr = rptr; 16910 16911 /* Fill in the timestamp option. */ 16912 if (tcp->tcp_snd_ts_ok) { 16913 U32_TO_BE32((uint32_t)lbolt, 16914 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 16915 U32_TO_BE32(tcp->tcp_ts_recent, 16916 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 16917 } else { 16918 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 16919 } 16920 16921 /* copy header into outgoing packet */ 16922 dst = (ipaddr_t *)rptr; 16923 src = (ipaddr_t *)tcp->tcp_iphc; 16924 dst[0] = src[0]; 16925 dst[1] = src[1]; 16926 dst[2] = src[2]; 16927 dst[3] = src[3]; 16928 dst[4] = src[4]; 16929 dst[5] = src[5]; 16930 dst[6] = src[6]; 16931 dst[7] = src[7]; 16932 dst[8] = src[8]; 16933 dst[9] = src[9]; 16934 if (hdrlen -= 40) { 16935 hdrlen >>= 2; 16936 dst += 10; 16937 src += 10; 16938 do { 16939 *dst++ = *src++; 16940 } while (--hdrlen); 16941 } 16942 16943 /* 16944 * Set the ECN info in the TCP header. Note that this 16945 * is not the template header. 16946 */ 16947 if (tcp->tcp_ecn_ok) { 16948 SET_ECT(tcp, rptr); 16949 16950 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 16951 if (tcp->tcp_ecn_echo_on) 16952 tcph->th_flags[0] |= TH_ECE; 16953 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 16954 tcph->th_flags[0] |= TH_CWR; 16955 tcp->tcp_ecn_cwr_sent = B_TRUE; 16956 } 16957 } 16958 16959 if (tcp->tcp_ip_forward_progress) { 16960 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 16961 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 16962 tcp->tcp_ip_forward_progress = B_FALSE; 16963 } 16964 tcp_send_data(tcp, tcp->tcp_wq, mp1); 16965 return; 16966 16967 /* 16968 * If we ran out of memory, we pretend to have sent the packet 16969 * and that it was lost on the wire. 16970 */ 16971 no_memory: 16972 return; 16973 16974 slow: 16975 /* leftover work from above */ 16976 tcp->tcp_unsent = len; 16977 tcp->tcp_xmit_tail_unsent = len; 16978 tcp_wput_data(tcp, NULL, B_FALSE); 16979 } 16980 16981 /* ARGSUSED */ 16982 void 16983 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 16984 { 16985 conn_t *connp = (conn_t *)arg; 16986 tcp_t *tcp = connp->conn_tcp; 16987 queue_t *q = tcp->tcp_rq; 16988 struct tcp_options *tcpopt; 16989 tcp_stack_t *tcps = tcp->tcp_tcps; 16990 16991 /* socket options */ 16992 uint_t sopp_flags; 16993 ssize_t sopp_rxhiwat; 16994 ssize_t sopp_maxblk; 16995 ushort_t sopp_wroff; 16996 ushort_t sopp_tail; 16997 ushort_t sopp_copyopt; 16998 16999 tcpopt = (struct tcp_options *)mp->b_rptr; 17000 17001 /* 17002 * Drop the eager's ref on the listener, that was placed when 17003 * this eager began life in tcp_conn_request. 17004 */ 17005 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17006 if (IPCL_IS_NONSTR(connp)) { 17007 /* Safe to free conn_ind message */ 17008 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17009 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17010 } 17011 17012 tcp->tcp_detached = B_FALSE; 17013 17014 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17015 /* 17016 * Someone blewoff the eager before we could finish 17017 * the accept. 17018 * 17019 * The only reason eager exists it because we put in 17020 * a ref on it when conn ind went up. We need to send 17021 * a disconnect indication up while the last reference 17022 * on the eager will be dropped by the squeue when we 17023 * return. 17024 */ 17025 ASSERT(tcp->tcp_listener == NULL); 17026 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17027 if (IPCL_IS_NONSTR(connp)) { 17028 ASSERT(tcp->tcp_issocket); 17029 (*connp->conn_upcalls->su_disconnected)( 17030 connp->conn_upper_handle, tcp->tcp_connid, 17031 ECONNREFUSED); 17032 freemsg(mp); 17033 } else { 17034 struct T_discon_ind *tdi; 17035 17036 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17037 /* 17038 * Let us reuse the incoming mblk to avoid 17039 * memory allocation failure problems. We know 17040 * that the size of the incoming mblk i.e. 17041 * stroptions is greater than sizeof 17042 * T_discon_ind. So the reallocb below can't 17043 * fail. 17044 */ 17045 freemsg(mp->b_cont); 17046 mp->b_cont = NULL; 17047 ASSERT(DB_REF(mp) == 1); 17048 mp = reallocb(mp, sizeof (struct T_discon_ind), 17049 B_FALSE); 17050 ASSERT(mp != NULL); 17051 DB_TYPE(mp) = M_PROTO; 17052 ((union T_primitives *)mp->b_rptr)->type = 17053 T_DISCON_IND; 17054 tdi = (struct T_discon_ind *)mp->b_rptr; 17055 if (tcp->tcp_issocket) { 17056 tdi->DISCON_reason = ECONNREFUSED; 17057 tdi->SEQ_number = 0; 17058 } else { 17059 tdi->DISCON_reason = ENOPROTOOPT; 17060 tdi->SEQ_number = 17061 tcp->tcp_conn_req_seqnum; 17062 } 17063 mp->b_wptr = mp->b_rptr + 17064 sizeof (struct T_discon_ind); 17065 putnext(q, mp); 17066 return; 17067 } 17068 } 17069 if (tcp->tcp_hard_binding) { 17070 tcp->tcp_hard_binding = B_FALSE; 17071 tcp->tcp_hard_bound = B_TRUE; 17072 } 17073 return; 17074 } 17075 17076 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17077 int boundif = tcpopt->to_boundif; 17078 uint_t len = sizeof (int); 17079 17080 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17081 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17082 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17083 } 17084 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17085 uint_t on = 1; 17086 uint_t len = sizeof (uint_t); 17087 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17088 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17089 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17090 } 17091 17092 /* 17093 * For a loopback connection with tcp_direct_sockfs on, note that 17094 * we don't have to protect tcp_rcv_list yet because synchronous 17095 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17096 * possibly race with us. 17097 */ 17098 17099 /* 17100 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17101 * properly. This is the first time we know of the acceptor' 17102 * queue. So we do it here. 17103 * 17104 * XXX 17105 */ 17106 if (tcp->tcp_rcv_list == NULL) { 17107 /* 17108 * Recv queue is empty, tcp_rwnd should not have changed. 17109 * That means it should be equal to the listener's tcp_rwnd. 17110 */ 17111 if (!IPCL_IS_NONSTR(connp)) 17112 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17113 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17114 } else { 17115 #ifdef DEBUG 17116 mblk_t *tmp; 17117 mblk_t *mp1; 17118 uint_t cnt = 0; 17119 17120 mp1 = tcp->tcp_rcv_list; 17121 while ((tmp = mp1) != NULL) { 17122 mp1 = tmp->b_next; 17123 cnt += msgdsize(tmp); 17124 } 17125 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17126 #endif 17127 /* There is some data, add them back to get the max. */ 17128 if (!IPCL_IS_NONSTR(connp)) 17129 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17130 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17131 } 17132 /* 17133 * This is the first time we run on the correct 17134 * queue after tcp_accept. So fix all the q parameters 17135 * here. 17136 */ 17137 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17138 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17139 17140 /* 17141 * Record the stream head's high water mark for this endpoint; 17142 * this is used for flow-control purposes. 17143 */ 17144 sopp_rxhiwat = tcp->tcp_fused ? 17145 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17146 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17147 17148 /* 17149 * Determine what write offset value to use depending on SACK and 17150 * whether the endpoint is fused or not. 17151 */ 17152 if (tcp->tcp_fused) { 17153 ASSERT(tcp->tcp_loopback); 17154 ASSERT(tcp->tcp_loopback_peer != NULL); 17155 /* 17156 * For fused tcp loopback, set the stream head's write 17157 * offset value to zero since we won't be needing any room 17158 * for TCP/IP headers. This would also improve performance 17159 * since it would reduce the amount of work done by kmem. 17160 * Non-fused tcp loopback case is handled separately below. 17161 */ 17162 sopp_wroff = 0; 17163 /* 17164 * Update the peer's transmit parameters according to 17165 * our recently calculated high water mark value. 17166 */ 17167 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17168 } else if (tcp->tcp_snd_sack_ok) { 17169 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17170 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17171 } else { 17172 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17173 tcps->tcps_wroff_xtra); 17174 } 17175 17176 /* 17177 * If this is endpoint is handling SSL, then reserve extra 17178 * offset and space at the end. 17179 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17180 * overriding the previous setting. The extra cost of signing and 17181 * encrypting multiple MSS-size records (12 of them with Ethernet), 17182 * instead of a single contiguous one by the stream head 17183 * largely outweighs the statistical reduction of ACKs, when 17184 * applicable. The peer will also save on decryption and verification 17185 * costs. 17186 */ 17187 if (tcp->tcp_kssl_ctx != NULL) { 17188 sopp_wroff += SSL3_WROFFSET; 17189 17190 sopp_flags |= SOCKOPT_TAIL; 17191 sopp_tail = SSL3_MAX_TAIL_LEN; 17192 17193 sopp_flags |= SOCKOPT_ZCOPY; 17194 sopp_copyopt = ZCVMUNSAFE; 17195 17196 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17197 } 17198 17199 /* Send the options up */ 17200 if (IPCL_IS_NONSTR(connp)) { 17201 struct sock_proto_props sopp; 17202 17203 sopp.sopp_flags = sopp_flags; 17204 sopp.sopp_wroff = sopp_wroff; 17205 sopp.sopp_maxblk = sopp_maxblk; 17206 sopp.sopp_rxhiwat = sopp_rxhiwat; 17207 if (sopp_flags & SOCKOPT_TAIL) { 17208 ASSERT(tcp->tcp_kssl_ctx != NULL); 17209 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17210 sopp.sopp_tail = sopp_tail; 17211 sopp.sopp_zcopyflag = sopp_copyopt; 17212 } 17213 (*connp->conn_upcalls->su_set_proto_props) 17214 (connp->conn_upper_handle, &sopp); 17215 } else { 17216 struct stroptions *stropt; 17217 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17218 if (stropt_mp == NULL) { 17219 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17220 return; 17221 } 17222 DB_TYPE(stropt_mp) = M_SETOPTS; 17223 stropt = (struct stroptions *)stropt_mp->b_rptr; 17224 stropt_mp->b_wptr += sizeof (struct stroptions); 17225 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 17226 stropt->so_hiwat = sopp_rxhiwat; 17227 stropt->so_wroff = sopp_wroff; 17228 stropt->so_maxblk = sopp_maxblk; 17229 17230 if (sopp_flags & SOCKOPT_TAIL) { 17231 ASSERT(tcp->tcp_kssl_ctx != NULL); 17232 17233 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17234 stropt->so_tail = sopp_tail; 17235 stropt->so_copyopt = sopp_copyopt; 17236 } 17237 17238 /* Send the options up */ 17239 putnext(q, stropt_mp); 17240 } 17241 17242 freemsg(mp); 17243 /* 17244 * Pass up any data and/or a fin that has been received. 17245 * 17246 * Adjust receive window in case it had decreased 17247 * (because there is data <=> tcp_rcv_list != NULL) 17248 * while the connection was detached. Note that 17249 * in case the eager was flow-controlled, w/o this 17250 * code, the rwnd may never open up again! 17251 */ 17252 if (tcp->tcp_rcv_list != NULL) { 17253 if (IPCL_IS_NONSTR(connp)) { 17254 mblk_t *mp; 17255 int space_left; 17256 int error; 17257 boolean_t push = B_TRUE; 17258 17259 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17260 (connp->conn_upper_handle, NULL, 0, 0, &error, 17261 &push) >= 0) { 17262 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17263 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17264 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17265 tcp_xmit_ctl(NULL, 17266 tcp, (tcp->tcp_swnd == 0) ? 17267 tcp->tcp_suna : tcp->tcp_snxt, 17268 tcp->tcp_rnxt, TH_ACK); 17269 } 17270 } 17271 while ((mp = tcp->tcp_rcv_list) != NULL) { 17272 push = B_TRUE; 17273 tcp->tcp_rcv_list = mp->b_next; 17274 mp->b_next = NULL; 17275 space_left = (*connp->conn_upcalls->su_recv) 17276 (connp->conn_upper_handle, mp, msgdsize(mp), 17277 0, &error, &push); 17278 if (space_left < 0) { 17279 /* 17280 * We should never be in middle of a 17281 * fallback, the squeue guarantees that. 17282 */ 17283 ASSERT(error != EOPNOTSUPP); 17284 } 17285 } 17286 tcp->tcp_rcv_last_head = NULL; 17287 tcp->tcp_rcv_last_tail = NULL; 17288 tcp->tcp_rcv_cnt = 0; 17289 } else { 17290 /* We drain directly in case of fused tcp loopback */ 17291 17292 if (!tcp->tcp_fused && canputnext(q)) { 17293 tcp->tcp_rwnd = q->q_hiwat; 17294 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17295 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17296 tcp_xmit_ctl(NULL, 17297 tcp, (tcp->tcp_swnd == 0) ? 17298 tcp->tcp_suna : tcp->tcp_snxt, 17299 tcp->tcp_rnxt, TH_ACK); 17300 } 17301 } 17302 17303 (void) tcp_rcv_drain(tcp); 17304 } 17305 17306 /* 17307 * For fused tcp loopback, back-enable peer endpoint 17308 * if it's currently flow-controlled. 17309 */ 17310 if (tcp->tcp_fused) { 17311 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17312 17313 ASSERT(peer_tcp != NULL); 17314 ASSERT(peer_tcp->tcp_fused); 17315 /* 17316 * In order to change the peer's tcp_flow_stopped, 17317 * we need to take locks for both end points. The 17318 * highest address is taken first. 17319 */ 17320 if (peer_tcp > tcp) { 17321 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17322 mutex_enter(&tcp->tcp_non_sq_lock); 17323 } else { 17324 mutex_enter(&tcp->tcp_non_sq_lock); 17325 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17326 } 17327 if (peer_tcp->tcp_flow_stopped) { 17328 tcp_clrqfull(peer_tcp); 17329 TCP_STAT(tcps, tcp_fusion_backenabled); 17330 } 17331 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17332 mutex_exit(&tcp->tcp_non_sq_lock); 17333 } 17334 } 17335 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17336 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17337 tcp->tcp_ordrel_done = B_TRUE; 17338 if (IPCL_IS_NONSTR(connp)) { 17339 ASSERT(tcp->tcp_ordrel_mp == NULL); 17340 (*connp->conn_upcalls->su_opctl)( 17341 connp->conn_upper_handle, 17342 SOCK_OPCTL_SHUT_RECV, 0); 17343 } else { 17344 mp = tcp->tcp_ordrel_mp; 17345 tcp->tcp_ordrel_mp = NULL; 17346 putnext(q, mp); 17347 } 17348 } 17349 if (tcp->tcp_hard_binding) { 17350 tcp->tcp_hard_binding = B_FALSE; 17351 tcp->tcp_hard_bound = B_TRUE; 17352 } 17353 17354 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17355 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17356 tcp->tcp_loopback_peer != NULL && 17357 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17358 tcp_fuse_syncstr_enable_pair(tcp); 17359 } 17360 17361 if (tcp->tcp_ka_enabled) { 17362 tcp->tcp_ka_last_intrvl = 0; 17363 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17364 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17365 } 17366 17367 /* 17368 * At this point, eager is fully established and will 17369 * have the following references - 17370 * 17371 * 2 references for connection to exist (1 for TCP and 1 for IP). 17372 * 1 reference for the squeue which will be dropped by the squeue as 17373 * soon as this function returns. 17374 * There will be 1 additonal reference for being in classifier 17375 * hash list provided something bad hasn't happened. 17376 */ 17377 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17378 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17379 } 17380 17381 /* 17382 * The function called through squeue to get behind listener's perimeter to 17383 * send a deffered conn_ind. 17384 */ 17385 /* ARGSUSED */ 17386 void 17387 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17388 { 17389 conn_t *connp = (conn_t *)arg; 17390 tcp_t *listener = connp->conn_tcp; 17391 struct T_conn_ind *conn_ind; 17392 tcp_t *tcp; 17393 17394 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17395 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17396 conn_ind->OPT_length); 17397 17398 if (listener->tcp_state == TCPS_CLOSED || 17399 TCP_IS_DETACHED(listener)) { 17400 /* 17401 * If listener has closed, it would have caused a 17402 * a cleanup/blowoff to happen for the eager. 17403 * 17404 * We need to drop the ref on eager that was put 17405 * tcp_rput_data() before trying to send the conn_ind 17406 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17407 * and tcp_wput_accept() is sending this deferred conn_ind but 17408 * listener is closed so we drop the ref. 17409 */ 17410 CONN_DEC_REF(tcp->tcp_connp); 17411 freemsg(mp); 17412 return; 17413 } 17414 17415 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17416 } 17417 17418 /* ARGSUSED */ 17419 static int 17420 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17421 { 17422 tcp_t *listener, *eager; 17423 mblk_t *opt_mp; 17424 struct tcp_options *tcpopt; 17425 17426 listener = lconnp->conn_tcp; 17427 ASSERT(listener->tcp_state == TCPS_LISTEN); 17428 eager = econnp->conn_tcp; 17429 ASSERT(eager->tcp_listener != NULL); 17430 17431 ASSERT(eager->tcp_rq != NULL); 17432 17433 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17434 if (opt_mp == NULL) { 17435 return (-TPROTO); 17436 } 17437 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17438 eager->tcp_issocket = B_TRUE; 17439 17440 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17441 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17442 ASSERT(econnp->conn_netstack == 17443 listener->tcp_connp->conn_netstack); 17444 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17445 17446 /* Put the ref for IP */ 17447 CONN_INC_REF(econnp); 17448 17449 /* 17450 * We should have minimum of 3 references on the conn 17451 * at this point. One each for TCP and IP and one for 17452 * the T_conn_ind that was sent up when the 3-way handshake 17453 * completed. In the normal case we would also have another 17454 * reference (making a total of 4) for the conn being in the 17455 * classifier hash list. However the eager could have received 17456 * an RST subsequently and tcp_closei_local could have removed 17457 * the eager from the classifier hash list, hence we can't 17458 * assert that reference. 17459 */ 17460 ASSERT(econnp->conn_ref >= 3); 17461 17462 opt_mp->b_datap->db_type = M_SETOPTS; 17463 opt_mp->b_wptr += sizeof (struct tcp_options); 17464 17465 /* 17466 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17467 * from listener to acceptor. 17468 */ 17469 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17470 tcpopt->to_flags = 0; 17471 17472 if (listener->tcp_bound_if != 0) { 17473 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17474 tcpopt->to_boundif = listener->tcp_bound_if; 17475 } 17476 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17477 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17478 } 17479 17480 mutex_enter(&listener->tcp_eager_lock); 17481 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17482 17483 tcp_t *tail; 17484 tcp_t *tcp; 17485 mblk_t *mp1; 17486 17487 tcp = listener->tcp_eager_prev_q0; 17488 /* 17489 * listener->tcp_eager_prev_q0 points to the TAIL of the 17490 * deferred T_conn_ind queue. We need to get to the head 17491 * of the queue in order to send up T_conn_ind the same 17492 * order as how the 3WHS is completed. 17493 */ 17494 while (tcp != listener) { 17495 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17496 !tcp->tcp_kssl_pending) 17497 break; 17498 else 17499 tcp = tcp->tcp_eager_prev_q0; 17500 } 17501 /* None of the pending eagers can be sent up now */ 17502 if (tcp == listener) 17503 goto no_more_eagers; 17504 17505 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17506 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17507 /* Move from q0 to q */ 17508 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17509 listener->tcp_conn_req_cnt_q0--; 17510 listener->tcp_conn_req_cnt_q++; 17511 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17512 tcp->tcp_eager_prev_q0; 17513 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17514 tcp->tcp_eager_next_q0; 17515 tcp->tcp_eager_prev_q0 = NULL; 17516 tcp->tcp_eager_next_q0 = NULL; 17517 tcp->tcp_conn_def_q0 = B_FALSE; 17518 17519 /* Make sure the tcp isn't in the list of droppables */ 17520 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17521 tcp->tcp_eager_prev_drop_q0 == NULL); 17522 17523 /* 17524 * Insert at end of the queue because sockfs sends 17525 * down T_CONN_RES in chronological order. Leaving 17526 * the older conn indications at front of the queue 17527 * helps reducing search time. 17528 */ 17529 tail = listener->tcp_eager_last_q; 17530 if (tail != NULL) { 17531 tail->tcp_eager_next_q = tcp; 17532 } else { 17533 listener->tcp_eager_next_q = tcp; 17534 } 17535 listener->tcp_eager_last_q = tcp; 17536 tcp->tcp_eager_next_q = NULL; 17537 17538 /* Need to get inside the listener perimeter */ 17539 CONN_INC_REF(listener->tcp_connp); 17540 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17541 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17542 SQTAG_TCP_SEND_PENDING); 17543 } 17544 no_more_eagers: 17545 tcp_eager_unlink(eager); 17546 mutex_exit(&listener->tcp_eager_lock); 17547 17548 /* 17549 * At this point, the eager is detached from the listener 17550 * but we still have an extra refs on eager (apart from the 17551 * usual tcp references). The ref was placed in tcp_rput_data 17552 * before sending the conn_ind in tcp_send_conn_ind. 17553 * The ref will be dropped in tcp_accept_finish(). 17554 */ 17555 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17556 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17557 return (0); 17558 } 17559 17560 int 17561 tcp_accept(sock_lower_handle_t lproto_handle, 17562 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17563 cred_t *cr) 17564 { 17565 conn_t *lconnp, *econnp; 17566 tcp_t *listener, *eager; 17567 tcp_stack_t *tcps; 17568 17569 lconnp = (conn_t *)lproto_handle; 17570 listener = lconnp->conn_tcp; 17571 ASSERT(listener->tcp_state == TCPS_LISTEN); 17572 econnp = (conn_t *)eproto_handle; 17573 eager = econnp->conn_tcp; 17574 ASSERT(eager->tcp_listener != NULL); 17575 tcps = eager->tcp_tcps; 17576 17577 /* 17578 * It is OK to manipulate these fields outside the eager's squeue 17579 * because they will not start being used until tcp_accept_finish 17580 * has been called. 17581 */ 17582 ASSERT(lconnp->conn_upper_handle != NULL); 17583 ASSERT(econnp->conn_upper_handle == NULL); 17584 econnp->conn_upper_handle = sock_handle; 17585 econnp->conn_upcalls = lconnp->conn_upcalls; 17586 ASSERT(IPCL_IS_NONSTR(econnp)); 17587 /* 17588 * Create helper stream if it is a non-TPI TCP connection. 17589 */ 17590 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17591 ip1dbg(("tcp_accept: create of IP helper stream" 17592 " failed\n")); 17593 return (EPROTO); 17594 } 17595 eager->tcp_rq = econnp->conn_rq; 17596 eager->tcp_wq = econnp->conn_wq; 17597 17598 ASSERT(eager->tcp_rq != NULL); 17599 17600 return (tcp_accept_common(lconnp, econnp, cr)); 17601 } 17602 17603 17604 /* 17605 * This is the STREAMS entry point for T_CONN_RES coming down on 17606 * Acceptor STREAM when sockfs listener does accept processing. 17607 * Read the block comment on top of tcp_conn_request(). 17608 */ 17609 void 17610 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17611 { 17612 queue_t *rq = RD(q); 17613 struct T_conn_res *conn_res; 17614 tcp_t *eager; 17615 tcp_t *listener; 17616 struct T_ok_ack *ok; 17617 t_scalar_t PRIM_type; 17618 conn_t *econnp; 17619 cred_t *cr; 17620 17621 ASSERT(DB_TYPE(mp) == M_PROTO); 17622 17623 /* 17624 * All Solaris components should pass a db_credp 17625 * for this TPI message, hence we ASSERT. 17626 * But in case there is some other M_PROTO that looks 17627 * like a TPI message sent by some other kernel 17628 * component, we check and return an error. 17629 */ 17630 cr = msg_getcred(mp, NULL); 17631 ASSERT(cr != NULL); 17632 if (cr == NULL) { 17633 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17634 if (mp != NULL) 17635 putnext(rq, mp); 17636 return; 17637 } 17638 conn_res = (struct T_conn_res *)mp->b_rptr; 17639 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17640 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17641 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17642 if (mp != NULL) 17643 putnext(rq, mp); 17644 return; 17645 } 17646 switch (conn_res->PRIM_type) { 17647 case O_T_CONN_RES: 17648 case T_CONN_RES: 17649 /* 17650 * We pass up an err ack if allocb fails. This will 17651 * cause sockfs to issue a T_DISCON_REQ which will cause 17652 * tcp_eager_blowoff to be called. sockfs will then call 17653 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17654 * we need to do the allocb up here because we have to 17655 * make sure rq->q_qinfo->qi_qclose still points to the 17656 * correct function (tcp_tpi_close_accept) in case allocb 17657 * fails. 17658 */ 17659 bcopy(mp->b_rptr + conn_res->OPT_offset, 17660 &eager, conn_res->OPT_length); 17661 PRIM_type = conn_res->PRIM_type; 17662 mp->b_datap->db_type = M_PCPROTO; 17663 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17664 ok = (struct T_ok_ack *)mp->b_rptr; 17665 ok->PRIM_type = T_OK_ACK; 17666 ok->CORRECT_prim = PRIM_type; 17667 econnp = eager->tcp_connp; 17668 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17669 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17670 eager->tcp_rq = rq; 17671 eager->tcp_wq = q; 17672 rq->q_ptr = econnp; 17673 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17674 q->q_ptr = econnp; 17675 q->q_qinfo = &tcp_winit; 17676 listener = eager->tcp_listener; 17677 17678 if (tcp_accept_common(listener->tcp_connp, 17679 econnp, cr) < 0) { 17680 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17681 if (mp != NULL) 17682 putnext(rq, mp); 17683 return; 17684 } 17685 17686 /* 17687 * Send the new local address also up to sockfs. There 17688 * should already be enough space in the mp that came 17689 * down from soaccept(). 17690 */ 17691 if (eager->tcp_family == AF_INET) { 17692 sin_t *sin; 17693 17694 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17695 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17696 sin = (sin_t *)mp->b_wptr; 17697 mp->b_wptr += sizeof (sin_t); 17698 sin->sin_family = AF_INET; 17699 sin->sin_port = eager->tcp_lport; 17700 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17701 } else { 17702 sin6_t *sin6; 17703 17704 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17705 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17706 sin6 = (sin6_t *)mp->b_wptr; 17707 mp->b_wptr += sizeof (sin6_t); 17708 sin6->sin6_family = AF_INET6; 17709 sin6->sin6_port = eager->tcp_lport; 17710 if (eager->tcp_ipversion == IPV4_VERSION) { 17711 sin6->sin6_flowinfo = 0; 17712 IN6_IPADDR_TO_V4MAPPED( 17713 eager->tcp_ipha->ipha_src, 17714 &sin6->sin6_addr); 17715 } else { 17716 ASSERT(eager->tcp_ip6h != NULL); 17717 sin6->sin6_flowinfo = 17718 eager->tcp_ip6h->ip6_vcf & 17719 ~IPV6_VERS_AND_FLOW_MASK; 17720 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17721 } 17722 sin6->sin6_scope_id = 0; 17723 sin6->__sin6_src_id = 0; 17724 } 17725 17726 putnext(rq, mp); 17727 return; 17728 default: 17729 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17730 if (mp != NULL) 17731 putnext(rq, mp); 17732 return; 17733 } 17734 } 17735 17736 static int 17737 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17738 { 17739 sin_t *sin = (sin_t *)sa; 17740 sin6_t *sin6 = (sin6_t *)sa; 17741 17742 switch (tcp->tcp_family) { 17743 case AF_INET: 17744 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17745 17746 if (*salenp < sizeof (sin_t)) 17747 return (EINVAL); 17748 17749 *sin = sin_null; 17750 sin->sin_family = AF_INET; 17751 if (tcp->tcp_state >= TCPS_BOUND) { 17752 sin->sin_port = tcp->tcp_lport; 17753 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17754 } 17755 *salenp = sizeof (sin_t); 17756 break; 17757 17758 case AF_INET6: 17759 if (*salenp < sizeof (sin6_t)) 17760 return (EINVAL); 17761 17762 *sin6 = sin6_null; 17763 sin6->sin6_family = AF_INET6; 17764 if (tcp->tcp_state >= TCPS_BOUND) { 17765 sin6->sin6_port = tcp->tcp_lport; 17766 if (tcp->tcp_ipversion == IPV4_VERSION) { 17767 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17768 &sin6->sin6_addr); 17769 } else { 17770 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17771 } 17772 } 17773 *salenp = sizeof (sin6_t); 17774 break; 17775 } 17776 17777 return (0); 17778 } 17779 17780 static int 17781 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17782 { 17783 sin_t *sin = (sin_t *)sa; 17784 sin6_t *sin6 = (sin6_t *)sa; 17785 17786 if (tcp->tcp_state < TCPS_SYN_RCVD) 17787 return (ENOTCONN); 17788 17789 switch (tcp->tcp_family) { 17790 case AF_INET: 17791 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17792 17793 if (*salenp < sizeof (sin_t)) 17794 return (EINVAL); 17795 17796 *sin = sin_null; 17797 sin->sin_family = AF_INET; 17798 sin->sin_port = tcp->tcp_fport; 17799 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17800 sin->sin_addr.s_addr); 17801 *salenp = sizeof (sin_t); 17802 break; 17803 17804 case AF_INET6: 17805 if (*salenp < sizeof (sin6_t)) 17806 return (EINVAL); 17807 17808 *sin6 = sin6_null; 17809 sin6->sin6_family = AF_INET6; 17810 sin6->sin6_port = tcp->tcp_fport; 17811 sin6->sin6_addr = tcp->tcp_remote_v6; 17812 if (tcp->tcp_ipversion == IPV6_VERSION) { 17813 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17814 ~IPV6_VERS_AND_FLOW_MASK; 17815 } 17816 *salenp = sizeof (sin6_t); 17817 break; 17818 } 17819 17820 return (0); 17821 } 17822 17823 /* 17824 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17825 */ 17826 static void 17827 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 17828 { 17829 void *data; 17830 mblk_t *datamp = mp->b_cont; 17831 tcp_t *tcp = Q_TO_TCP(q); 17832 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 17833 17834 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 17835 cmdp->cb_error = EPROTO; 17836 qreply(q, mp); 17837 return; 17838 } 17839 17840 data = datamp->b_rptr; 17841 17842 switch (cmdp->cb_cmd) { 17843 case TI_GETPEERNAME: 17844 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 17845 break; 17846 case TI_GETMYNAME: 17847 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 17848 break; 17849 default: 17850 cmdp->cb_error = EINVAL; 17851 break; 17852 } 17853 17854 qreply(q, mp); 17855 } 17856 17857 void 17858 tcp_wput(queue_t *q, mblk_t *mp) 17859 { 17860 conn_t *connp = Q_TO_CONN(q); 17861 tcp_t *tcp; 17862 void (*output_proc)(); 17863 t_scalar_t type; 17864 uchar_t *rptr; 17865 struct iocblk *iocp; 17866 size_t size; 17867 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17868 17869 ASSERT(connp->conn_ref >= 2); 17870 17871 switch (DB_TYPE(mp)) { 17872 case M_DATA: 17873 tcp = connp->conn_tcp; 17874 ASSERT(tcp != NULL); 17875 17876 size = msgdsize(mp); 17877 17878 mutex_enter(&tcp->tcp_non_sq_lock); 17879 tcp->tcp_squeue_bytes += size; 17880 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17881 tcp_setqfull(tcp); 17882 } 17883 mutex_exit(&tcp->tcp_non_sq_lock); 17884 17885 CONN_INC_REF(connp); 17886 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 17887 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 17888 return; 17889 17890 case M_CMD: 17891 tcp_wput_cmdblk(q, mp); 17892 return; 17893 17894 case M_PROTO: 17895 case M_PCPROTO: 17896 /* 17897 * if it is a snmp message, don't get behind the squeue 17898 */ 17899 tcp = connp->conn_tcp; 17900 rptr = mp->b_rptr; 17901 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17902 type = ((union T_primitives *)rptr)->type; 17903 } else { 17904 if (tcp->tcp_debug) { 17905 (void) strlog(TCP_MOD_ID, 0, 1, 17906 SL_ERROR|SL_TRACE, 17907 "tcp_wput_proto, dropping one..."); 17908 } 17909 freemsg(mp); 17910 return; 17911 } 17912 if (type == T_SVR4_OPTMGMT_REQ) { 17913 /* 17914 * All Solaris components should pass a db_credp 17915 * for this TPI message, hence we ASSERT. 17916 * But in case there is some other M_PROTO that looks 17917 * like a TPI message sent by some other kernel 17918 * component, we check and return an error. 17919 */ 17920 cred_t *cr = msg_getcred(mp, NULL); 17921 17922 ASSERT(cr != NULL); 17923 if (cr == NULL) { 17924 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 17925 return; 17926 } 17927 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 17928 cr)) { 17929 /* 17930 * This was a SNMP request 17931 */ 17932 return; 17933 } else { 17934 output_proc = tcp_wput_proto; 17935 } 17936 } else { 17937 output_proc = tcp_wput_proto; 17938 } 17939 break; 17940 case M_IOCTL: 17941 /* 17942 * Most ioctls can be processed right away without going via 17943 * squeues - process them right here. Those that do require 17944 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17945 * are processed by tcp_wput_ioctl(). 17946 */ 17947 iocp = (struct iocblk *)mp->b_rptr; 17948 tcp = connp->conn_tcp; 17949 17950 switch (iocp->ioc_cmd) { 17951 case TCP_IOC_ABORT_CONN: 17952 tcp_ioctl_abort_conn(q, mp); 17953 return; 17954 case TI_GETPEERNAME: 17955 case TI_GETMYNAME: 17956 mi_copyin(q, mp, NULL, 17957 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17958 return; 17959 case ND_SET: 17960 /* nd_getset does the necessary checks */ 17961 case ND_GET: 17962 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 17963 CALL_IP_WPUT(connp, q, mp); 17964 return; 17965 } 17966 qreply(q, mp); 17967 return; 17968 case TCP_IOC_DEFAULT_Q: 17969 /* 17970 * Wants to be the default wq. Check the credentials 17971 * first, the rest is executed via squeue. 17972 */ 17973 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 17974 iocp->ioc_error = EPERM; 17975 iocp->ioc_count = 0; 17976 mp->b_datap->db_type = M_IOCACK; 17977 qreply(q, mp); 17978 return; 17979 } 17980 output_proc = tcp_wput_ioctl; 17981 break; 17982 default: 17983 output_proc = tcp_wput_ioctl; 17984 break; 17985 } 17986 break; 17987 default: 17988 output_proc = tcp_wput_nondata; 17989 break; 17990 } 17991 17992 CONN_INC_REF(connp); 17993 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 17994 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 17995 } 17996 17997 /* 17998 * Initial STREAMS write side put() procedure for sockets. It tries to 17999 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18000 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18001 * are handled by tcp_wput() as usual. 18002 * 18003 * All further messages will also be handled by tcp_wput() because we cannot 18004 * be sure that the above short cut is safe later. 18005 */ 18006 static void 18007 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18008 { 18009 conn_t *connp = Q_TO_CONN(wq); 18010 tcp_t *tcp = connp->conn_tcp; 18011 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18012 18013 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18014 wq->q_qinfo = &tcp_winit; 18015 18016 ASSERT(IPCL_IS_TCP(connp)); 18017 ASSERT(TCP_IS_SOCKET(tcp)); 18018 18019 if (DB_TYPE(mp) == M_PCPROTO && 18020 MBLKL(mp) == sizeof (struct T_capability_req) && 18021 car->PRIM_type == T_CAPABILITY_REQ) { 18022 tcp_capability_req(tcp, mp); 18023 return; 18024 } 18025 18026 tcp_wput(wq, mp); 18027 } 18028 18029 /* ARGSUSED */ 18030 static void 18031 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18032 { 18033 #ifdef DEBUG 18034 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18035 #endif 18036 freemsg(mp); 18037 } 18038 18039 static boolean_t 18040 tcp_zcopy_check(tcp_t *tcp) 18041 { 18042 conn_t *connp = tcp->tcp_connp; 18043 ire_t *ire; 18044 boolean_t zc_enabled = B_FALSE; 18045 tcp_stack_t *tcps = tcp->tcp_tcps; 18046 18047 if (do_tcpzcopy == 2) 18048 zc_enabled = B_TRUE; 18049 else if (tcp->tcp_ipversion == IPV4_VERSION && 18050 IPCL_IS_CONNECTED(connp) && 18051 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18052 connp->conn_dontroute == 0 && 18053 !connp->conn_nexthop_set && 18054 connp->conn_outgoing_ill == NULL && 18055 do_tcpzcopy == 1) { 18056 /* 18057 * the checks above closely resemble the fast path checks 18058 * in tcp_send_data(). 18059 */ 18060 mutex_enter(&connp->conn_lock); 18061 ire = connp->conn_ire_cache; 18062 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18063 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18064 IRE_REFHOLD(ire); 18065 if (ire->ire_stq != NULL) { 18066 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18067 18068 zc_enabled = ill && (ill->ill_capabilities & 18069 ILL_CAPAB_ZEROCOPY) && 18070 (ill->ill_zerocopy_capab-> 18071 ill_zerocopy_flags != 0); 18072 } 18073 IRE_REFRELE(ire); 18074 } 18075 mutex_exit(&connp->conn_lock); 18076 } 18077 tcp->tcp_snd_zcopy_on = zc_enabled; 18078 if (!TCP_IS_DETACHED(tcp)) { 18079 if (zc_enabled) { 18080 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18081 ZCVMSAFE); 18082 TCP_STAT(tcps, tcp_zcopy_on); 18083 } else { 18084 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18085 ZCVMUNSAFE); 18086 TCP_STAT(tcps, tcp_zcopy_off); 18087 } 18088 } 18089 return (zc_enabled); 18090 } 18091 18092 static mblk_t * 18093 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18094 { 18095 tcp_stack_t *tcps = tcp->tcp_tcps; 18096 18097 if (do_tcpzcopy == 2) 18098 return (bp); 18099 else if (tcp->tcp_snd_zcopy_on) { 18100 tcp->tcp_snd_zcopy_on = B_FALSE; 18101 if (!TCP_IS_DETACHED(tcp)) { 18102 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18103 ZCVMUNSAFE); 18104 TCP_STAT(tcps, tcp_zcopy_disable); 18105 } 18106 } 18107 return (tcp_zcopy_backoff(tcp, bp, 0)); 18108 } 18109 18110 /* 18111 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18112 * the original desballoca'ed segmapped mblk. 18113 */ 18114 static mblk_t * 18115 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18116 { 18117 mblk_t *head, *tail, *nbp; 18118 tcp_stack_t *tcps = tcp->tcp_tcps; 18119 18120 if (IS_VMLOANED_MBLK(bp)) { 18121 TCP_STAT(tcps, tcp_zcopy_backoff); 18122 if ((head = copyb(bp)) == NULL) { 18123 /* fail to backoff; leave it for the next backoff */ 18124 tcp->tcp_xmit_zc_clean = B_FALSE; 18125 return (bp); 18126 } 18127 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18128 if (fix_xmitlist) 18129 tcp_zcopy_notify(tcp); 18130 else 18131 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18132 } 18133 nbp = bp->b_cont; 18134 if (fix_xmitlist) { 18135 head->b_prev = bp->b_prev; 18136 head->b_next = bp->b_next; 18137 if (tcp->tcp_xmit_tail == bp) 18138 tcp->tcp_xmit_tail = head; 18139 } 18140 bp->b_next = NULL; 18141 bp->b_prev = NULL; 18142 freeb(bp); 18143 } else { 18144 head = bp; 18145 nbp = bp->b_cont; 18146 } 18147 tail = head; 18148 while (nbp) { 18149 if (IS_VMLOANED_MBLK(nbp)) { 18150 TCP_STAT(tcps, tcp_zcopy_backoff); 18151 if ((tail->b_cont = copyb(nbp)) == NULL) { 18152 tcp->tcp_xmit_zc_clean = B_FALSE; 18153 tail->b_cont = nbp; 18154 return (head); 18155 } 18156 tail = tail->b_cont; 18157 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18158 if (fix_xmitlist) 18159 tcp_zcopy_notify(tcp); 18160 else 18161 tail->b_datap->db_struioflag |= 18162 STRUIO_ZCNOTIFY; 18163 } 18164 bp = nbp; 18165 nbp = nbp->b_cont; 18166 if (fix_xmitlist) { 18167 tail->b_prev = bp->b_prev; 18168 tail->b_next = bp->b_next; 18169 if (tcp->tcp_xmit_tail == bp) 18170 tcp->tcp_xmit_tail = tail; 18171 } 18172 bp->b_next = NULL; 18173 bp->b_prev = NULL; 18174 freeb(bp); 18175 } else { 18176 tail->b_cont = nbp; 18177 tail = nbp; 18178 nbp = nbp->b_cont; 18179 } 18180 } 18181 if (fix_xmitlist) { 18182 tcp->tcp_xmit_last = tail; 18183 tcp->tcp_xmit_zc_clean = B_TRUE; 18184 } 18185 return (head); 18186 } 18187 18188 static void 18189 tcp_zcopy_notify(tcp_t *tcp) 18190 { 18191 struct stdata *stp; 18192 conn_t *connp; 18193 18194 if (tcp->tcp_detached) 18195 return; 18196 connp = tcp->tcp_connp; 18197 if (IPCL_IS_NONSTR(connp)) { 18198 (*connp->conn_upcalls->su_zcopy_notify) 18199 (connp->conn_upper_handle); 18200 return; 18201 } 18202 stp = STREAM(tcp->tcp_rq); 18203 mutex_enter(&stp->sd_lock); 18204 stp->sd_flag |= STZCNOTIFY; 18205 cv_broadcast(&stp->sd_zcopy_wait); 18206 mutex_exit(&stp->sd_lock); 18207 } 18208 18209 static boolean_t 18210 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18211 { 18212 ire_t *ire; 18213 conn_t *connp = tcp->tcp_connp; 18214 tcp_stack_t *tcps = tcp->tcp_tcps; 18215 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18216 18217 mutex_enter(&connp->conn_lock); 18218 ire = connp->conn_ire_cache; 18219 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18220 18221 if ((ire != NULL) && 18222 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18223 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18224 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18225 IRE_REFHOLD(ire); 18226 mutex_exit(&connp->conn_lock); 18227 } else { 18228 boolean_t cached = B_FALSE; 18229 ts_label_t *tsl; 18230 18231 /* force a recheck later on */ 18232 tcp->tcp_ire_ill_check_done = B_FALSE; 18233 18234 TCP_DBGSTAT(tcps, tcp_ire_null1); 18235 connp->conn_ire_cache = NULL; 18236 mutex_exit(&connp->conn_lock); 18237 18238 if (ire != NULL) 18239 IRE_REFRELE_NOTR(ire); 18240 18241 tsl = crgetlabel(CONN_CRED(connp)); 18242 ire = (dst ? 18243 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18244 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18245 connp->conn_zoneid, tsl, ipst)); 18246 18247 if (ire == NULL) { 18248 TCP_STAT(tcps, tcp_ire_null); 18249 return (B_FALSE); 18250 } 18251 18252 IRE_REFHOLD_NOTR(ire); 18253 18254 mutex_enter(&connp->conn_lock); 18255 if (CONN_CACHE_IRE(connp)) { 18256 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18257 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18258 TCP_CHECK_IREINFO(tcp, ire); 18259 connp->conn_ire_cache = ire; 18260 cached = B_TRUE; 18261 } 18262 rw_exit(&ire->ire_bucket->irb_lock); 18263 } 18264 mutex_exit(&connp->conn_lock); 18265 18266 /* 18267 * We can continue to use the ire but since it was 18268 * not cached, we should drop the extra reference. 18269 */ 18270 if (!cached) 18271 IRE_REFRELE_NOTR(ire); 18272 18273 /* 18274 * Rampart note: no need to select a new label here, since 18275 * labels are not allowed to change during the life of a TCP 18276 * connection. 18277 */ 18278 } 18279 18280 *irep = ire; 18281 18282 return (B_TRUE); 18283 } 18284 18285 /* 18286 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18287 * 18288 * 0 = success; 18289 * 1 = failed to find ire and ill. 18290 */ 18291 static boolean_t 18292 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18293 { 18294 ipha_t *ipha; 18295 ipaddr_t dst; 18296 ire_t *ire; 18297 ill_t *ill; 18298 mblk_t *ire_fp_mp; 18299 tcp_stack_t *tcps = tcp->tcp_tcps; 18300 18301 if (mp != NULL) 18302 ipha = (ipha_t *)mp->b_rptr; 18303 else 18304 ipha = tcp->tcp_ipha; 18305 dst = ipha->ipha_dst; 18306 18307 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18308 return (B_FALSE); 18309 18310 if ((ire->ire_flags & RTF_MULTIRT) || 18311 (ire->ire_stq == NULL) || 18312 (ire->ire_nce == NULL) || 18313 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18314 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18315 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18316 TCP_STAT(tcps, tcp_ip_ire_send); 18317 IRE_REFRELE(ire); 18318 return (B_FALSE); 18319 } 18320 18321 ill = ire_to_ill(ire); 18322 ASSERT(ill != NULL); 18323 18324 if (!tcp->tcp_ire_ill_check_done) { 18325 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18326 tcp->tcp_ire_ill_check_done = B_TRUE; 18327 } 18328 18329 *irep = ire; 18330 *illp = ill; 18331 18332 return (B_TRUE); 18333 } 18334 18335 static void 18336 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18337 { 18338 ipha_t *ipha; 18339 ipaddr_t src; 18340 ipaddr_t dst; 18341 uint32_t cksum; 18342 ire_t *ire; 18343 uint16_t *up; 18344 ill_t *ill; 18345 conn_t *connp = tcp->tcp_connp; 18346 uint32_t hcksum_txflags = 0; 18347 mblk_t *ire_fp_mp; 18348 uint_t ire_fp_mp_len; 18349 tcp_stack_t *tcps = tcp->tcp_tcps; 18350 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18351 cred_t *cr; 18352 pid_t cpid; 18353 18354 ASSERT(DB_TYPE(mp) == M_DATA); 18355 18356 /* 18357 * Here we need to handle the overloading of the cred_t for 18358 * both getpeerucred and TX. 18359 * If this is a SYN then the caller already set db_credp so 18360 * that getpeerucred will work. But if TX is in use we might have 18361 * a conn_peercred which is different, and we need to use that cred 18362 * to make TX use the correct label and label dependent route. 18363 */ 18364 if (is_system_labeled()) { 18365 cr = msg_getcred(mp, &cpid); 18366 if (cr == NULL || connp->conn_peercred != NULL) 18367 mblk_setcred(mp, CONN_CRED(connp), cpid); 18368 } 18369 18370 ipha = (ipha_t *)mp->b_rptr; 18371 src = ipha->ipha_src; 18372 dst = ipha->ipha_dst; 18373 18374 ASSERT(q != NULL); 18375 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18376 18377 /* 18378 * Drop off fast path for IPv6 and also if options are present or 18379 * we need to resolve a TS label. 18380 */ 18381 if (tcp->tcp_ipversion != IPV4_VERSION || 18382 !IPCL_IS_CONNECTED(connp) || 18383 !CONN_IS_LSO_MD_FASTPATH(connp) || 18384 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18385 !connp->conn_ulp_labeled || 18386 ipha->ipha_ident == IP_HDR_INCLUDED || 18387 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18388 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18389 if (tcp->tcp_snd_zcopy_aware) 18390 mp = tcp_zcopy_disable(tcp, mp); 18391 TCP_STAT(tcps, tcp_ip_send); 18392 CALL_IP_WPUT(connp, q, mp); 18393 return; 18394 } 18395 18396 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18397 if (tcp->tcp_snd_zcopy_aware) 18398 mp = tcp_zcopy_backoff(tcp, mp, 0); 18399 CALL_IP_WPUT(connp, q, mp); 18400 return; 18401 } 18402 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18403 ire_fp_mp_len = MBLKL(ire_fp_mp); 18404 18405 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18406 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18407 #ifndef _BIG_ENDIAN 18408 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18409 #endif 18410 18411 /* 18412 * Check to see if we need to re-enable LSO/MDT for this connection 18413 * because it was previously disabled due to changes in the ill; 18414 * note that by doing it here, this re-enabling only applies when 18415 * the packet is not dispatched through CALL_IP_WPUT(). 18416 * 18417 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18418 * case, since that's how we ended up here. For IPv6, we do the 18419 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18420 */ 18421 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18422 /* 18423 * Restore LSO for this connection, so that next time around 18424 * it is eligible to go through tcp_lsosend() path again. 18425 */ 18426 TCP_STAT(tcps, tcp_lso_enabled); 18427 tcp->tcp_lso = B_TRUE; 18428 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18429 "interface %s\n", (void *)connp, ill->ill_name)); 18430 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18431 /* 18432 * Restore MDT for this connection, so that next time around 18433 * it is eligible to go through tcp_multisend() path again. 18434 */ 18435 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18436 tcp->tcp_mdt = B_TRUE; 18437 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18438 "interface %s\n", (void *)connp, ill->ill_name)); 18439 } 18440 18441 if (tcp->tcp_snd_zcopy_aware) { 18442 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18443 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18444 mp = tcp_zcopy_disable(tcp, mp); 18445 /* 18446 * we shouldn't need to reset ipha as the mp containing 18447 * ipha should never be a zero-copy mp. 18448 */ 18449 } 18450 18451 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18452 ASSERT(ill->ill_hcksum_capab != NULL); 18453 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18454 } 18455 18456 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18457 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18458 18459 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18460 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18461 18462 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18463 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18464 18465 /* Software checksum? */ 18466 if (DB_CKSUMFLAGS(mp) == 0) { 18467 TCP_STAT(tcps, tcp_out_sw_cksum); 18468 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18469 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18470 } 18471 18472 /* Calculate IP header checksum if hardware isn't capable */ 18473 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18474 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18475 ((uint16_t *)ipha)[4]); 18476 } 18477 18478 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18479 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18480 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18481 18482 UPDATE_OB_PKT_COUNT(ire); 18483 ire->ire_last_used_time = lbolt; 18484 18485 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18486 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18487 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18488 ntohs(ipha->ipha_length)); 18489 18490 DTRACE_PROBE4(ip4__physical__out__start, 18491 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18492 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18493 ipst->ips_ipv4firewall_physical_out, 18494 NULL, ill, ipha, mp, mp, 0, ipst); 18495 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18496 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18497 18498 if (mp != NULL) { 18499 if (ipst->ips_ipobs_enabled) { 18500 zoneid_t szone; 18501 18502 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18503 ipst, ALL_ZONES); 18504 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18505 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18506 } 18507 18508 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18509 } 18510 18511 IRE_REFRELE(ire); 18512 } 18513 18514 /* 18515 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18516 * if the receiver shrinks the window, i.e. moves the right window to the 18517 * left, the we should not send new data, but should retransmit normally the 18518 * old unacked data between suna and suna + swnd. We might has sent data 18519 * that is now outside the new window, pretend that we didn't send it. 18520 */ 18521 static void 18522 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18523 { 18524 uint32_t snxt = tcp->tcp_snxt; 18525 mblk_t *xmit_tail; 18526 int32_t offset; 18527 18528 ASSERT(shrunk_count > 0); 18529 18530 /* Pretend we didn't send the data outside the window */ 18531 snxt -= shrunk_count; 18532 18533 /* Get the mblk and the offset in it per the shrunk window */ 18534 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18535 18536 ASSERT(xmit_tail != NULL); 18537 18538 /* Reset all the values per the now shrunk window */ 18539 tcp->tcp_snxt = snxt; 18540 tcp->tcp_xmit_tail = xmit_tail; 18541 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18542 offset; 18543 tcp->tcp_unsent += shrunk_count; 18544 18545 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18546 /* 18547 * Make sure the timer is running so that we will probe a zero 18548 * window. 18549 */ 18550 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18551 } 18552 18553 18554 /* 18555 * The TCP normal data output path. 18556 * NOTE: the logic of the fast path is duplicated from this function. 18557 */ 18558 static void 18559 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18560 { 18561 int len; 18562 mblk_t *local_time; 18563 mblk_t *mp1; 18564 uint32_t snxt; 18565 int tail_unsent; 18566 int tcpstate; 18567 int usable = 0; 18568 mblk_t *xmit_tail; 18569 queue_t *q = tcp->tcp_wq; 18570 int32_t mss; 18571 int32_t num_sack_blk = 0; 18572 int32_t tcp_hdr_len; 18573 int32_t tcp_tcp_hdr_len; 18574 int mdt_thres; 18575 int rc; 18576 tcp_stack_t *tcps = tcp->tcp_tcps; 18577 ip_stack_t *ipst; 18578 18579 tcpstate = tcp->tcp_state; 18580 if (mp == NULL) { 18581 /* 18582 * tcp_wput_data() with NULL mp should only be called when 18583 * there is unsent data. 18584 */ 18585 ASSERT(tcp->tcp_unsent > 0); 18586 /* Really tacky... but we need this for detached closes. */ 18587 len = tcp->tcp_unsent; 18588 goto data_null; 18589 } 18590 18591 #if CCS_STATS 18592 wrw_stats.tot.count++; 18593 wrw_stats.tot.bytes += msgdsize(mp); 18594 #endif 18595 ASSERT(mp->b_datap->db_type == M_DATA); 18596 /* 18597 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18598 * or before a connection attempt has begun. 18599 */ 18600 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18601 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18602 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18603 #ifdef DEBUG 18604 cmn_err(CE_WARN, 18605 "tcp_wput_data: data after ordrel, %s", 18606 tcp_display(tcp, NULL, 18607 DISP_ADDR_AND_PORT)); 18608 #else 18609 if (tcp->tcp_debug) { 18610 (void) strlog(TCP_MOD_ID, 0, 1, 18611 SL_TRACE|SL_ERROR, 18612 "tcp_wput_data: data after ordrel, %s\n", 18613 tcp_display(tcp, NULL, 18614 DISP_ADDR_AND_PORT)); 18615 } 18616 #endif /* DEBUG */ 18617 } 18618 if (tcp->tcp_snd_zcopy_aware && 18619 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18620 tcp_zcopy_notify(tcp); 18621 freemsg(mp); 18622 mutex_enter(&tcp->tcp_non_sq_lock); 18623 if (tcp->tcp_flow_stopped && 18624 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18625 tcp_clrqfull(tcp); 18626 } 18627 mutex_exit(&tcp->tcp_non_sq_lock); 18628 return; 18629 } 18630 18631 /* Strip empties */ 18632 for (;;) { 18633 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18634 (uintptr_t)INT_MAX); 18635 len = (int)(mp->b_wptr - mp->b_rptr); 18636 if (len > 0) 18637 break; 18638 mp1 = mp; 18639 mp = mp->b_cont; 18640 freeb(mp1); 18641 if (!mp) { 18642 return; 18643 } 18644 } 18645 18646 /* If we are the first on the list ... */ 18647 if (tcp->tcp_xmit_head == NULL) { 18648 tcp->tcp_xmit_head = mp; 18649 tcp->tcp_xmit_tail = mp; 18650 tcp->tcp_xmit_tail_unsent = len; 18651 } else { 18652 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18653 struct datab *dp; 18654 18655 mp1 = tcp->tcp_xmit_last; 18656 if (len < tcp_tx_pull_len && 18657 (dp = mp1->b_datap)->db_ref == 1 && 18658 dp->db_lim - mp1->b_wptr >= len) { 18659 ASSERT(len > 0); 18660 ASSERT(!mp1->b_cont); 18661 if (len == 1) { 18662 *mp1->b_wptr++ = *mp->b_rptr; 18663 } else { 18664 bcopy(mp->b_rptr, mp1->b_wptr, len); 18665 mp1->b_wptr += len; 18666 } 18667 if (mp1 == tcp->tcp_xmit_tail) 18668 tcp->tcp_xmit_tail_unsent += len; 18669 mp1->b_cont = mp->b_cont; 18670 if (tcp->tcp_snd_zcopy_aware && 18671 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18672 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18673 freeb(mp); 18674 mp = mp1; 18675 } else { 18676 tcp->tcp_xmit_last->b_cont = mp; 18677 } 18678 len += tcp->tcp_unsent; 18679 } 18680 18681 /* Tack on however many more positive length mblks we have */ 18682 if ((mp1 = mp->b_cont) != NULL) { 18683 do { 18684 int tlen; 18685 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18686 (uintptr_t)INT_MAX); 18687 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18688 if (tlen <= 0) { 18689 mp->b_cont = mp1->b_cont; 18690 freeb(mp1); 18691 } else { 18692 len += tlen; 18693 mp = mp1; 18694 } 18695 } while ((mp1 = mp->b_cont) != NULL); 18696 } 18697 tcp->tcp_xmit_last = mp; 18698 tcp->tcp_unsent = len; 18699 18700 if (urgent) 18701 usable = 1; 18702 18703 data_null: 18704 snxt = tcp->tcp_snxt; 18705 xmit_tail = tcp->tcp_xmit_tail; 18706 tail_unsent = tcp->tcp_xmit_tail_unsent; 18707 18708 /* 18709 * Note that tcp_mss has been adjusted to take into account the 18710 * timestamp option if applicable. Because SACK options do not 18711 * appear in every TCP segments and they are of variable lengths, 18712 * they cannot be included in tcp_mss. Thus we need to calculate 18713 * the actual segment length when we need to send a segment which 18714 * includes SACK options. 18715 */ 18716 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18717 int32_t opt_len; 18718 18719 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18720 tcp->tcp_num_sack_blk); 18721 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18722 2 + TCPOPT_HEADER_LEN; 18723 mss = tcp->tcp_mss - opt_len; 18724 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18725 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18726 } else { 18727 mss = tcp->tcp_mss; 18728 tcp_hdr_len = tcp->tcp_hdr_len; 18729 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18730 } 18731 18732 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18733 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18734 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18735 } 18736 if (tcpstate == TCPS_SYN_RCVD) { 18737 /* 18738 * The three-way connection establishment handshake is not 18739 * complete yet. We want to queue the data for transmission 18740 * after entering ESTABLISHED state (RFC793). A jump to 18741 * "done" label effectively leaves data on the queue. 18742 */ 18743 goto done; 18744 } else { 18745 int usable_r; 18746 18747 /* 18748 * In the special case when cwnd is zero, which can only 18749 * happen if the connection is ECN capable, return now. 18750 * New segments is sent using tcp_timer(). The timer 18751 * is set in tcp_rput_data(). 18752 */ 18753 if (tcp->tcp_cwnd == 0) { 18754 /* 18755 * Note that tcp_cwnd is 0 before 3-way handshake is 18756 * finished. 18757 */ 18758 ASSERT(tcp->tcp_ecn_ok || 18759 tcp->tcp_state < TCPS_ESTABLISHED); 18760 return; 18761 } 18762 18763 /* NOTE: trouble if xmitting while SYN not acked? */ 18764 usable_r = snxt - tcp->tcp_suna; 18765 usable_r = tcp->tcp_swnd - usable_r; 18766 18767 /* 18768 * Check if the receiver has shrunk the window. If 18769 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18770 * cannot be set as there is unsent data, so FIN cannot 18771 * be sent out. Otherwise, we need to take into account 18772 * of FIN as it consumes an "invisible" sequence number. 18773 */ 18774 ASSERT(tcp->tcp_fin_sent == 0); 18775 if (usable_r < 0) { 18776 /* 18777 * The receiver has shrunk the window and we have sent 18778 * -usable_r date beyond the window, re-adjust. 18779 * 18780 * If TCP window scaling is enabled, there can be 18781 * round down error as the advertised receive window 18782 * is actually right shifted n bits. This means that 18783 * the lower n bits info is wiped out. It will look 18784 * like the window is shrunk. Do a check here to 18785 * see if the shrunk amount is actually within the 18786 * error in window calculation. If it is, just 18787 * return. Note that this check is inside the 18788 * shrunk window check. This makes sure that even 18789 * though tcp_process_shrunk_swnd() is not called, 18790 * we will stop further processing. 18791 */ 18792 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18793 tcp_process_shrunk_swnd(tcp, -usable_r); 18794 } 18795 return; 18796 } 18797 18798 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18799 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18800 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18801 18802 /* usable = MIN(usable, unsent) */ 18803 if (usable_r > len) 18804 usable_r = len; 18805 18806 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18807 if (usable_r > 0) { 18808 usable = usable_r; 18809 } else { 18810 /* Bypass all other unnecessary processing. */ 18811 goto done; 18812 } 18813 } 18814 18815 local_time = (mblk_t *)lbolt; 18816 18817 /* 18818 * "Our" Nagle Algorithm. This is not the same as in the old 18819 * BSD. This is more in line with the true intent of Nagle. 18820 * 18821 * The conditions are: 18822 * 1. The amount of unsent data (or amount of data which can be 18823 * sent, whichever is smaller) is less than Nagle limit. 18824 * 2. The last sent size is also less than Nagle limit. 18825 * 3. There is unack'ed data. 18826 * 4. Urgent pointer is not set. Send urgent data ignoring the 18827 * Nagle algorithm. This reduces the probability that urgent 18828 * bytes get "merged" together. 18829 * 5. The app has not closed the connection. This eliminates the 18830 * wait time of the receiving side waiting for the last piece of 18831 * (small) data. 18832 * 18833 * If all are satisified, exit without sending anything. Note 18834 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18835 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18836 * 4095). 18837 */ 18838 if (usable < (int)tcp->tcp_naglim && 18839 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18840 snxt != tcp->tcp_suna && 18841 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18842 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18843 goto done; 18844 } 18845 18846 if (tcp->tcp_cork) { 18847 /* 18848 * if the tcp->tcp_cork option is set, then we have to force 18849 * TCP not to send partial segment (smaller than MSS bytes). 18850 * We are calculating the usable now based on full mss and 18851 * will save the rest of remaining data for later. 18852 */ 18853 if (usable < mss) 18854 goto done; 18855 usable = (usable / mss) * mss; 18856 } 18857 18858 /* Update the latest receive window size in TCP header. */ 18859 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18860 tcp->tcp_tcph->th_win); 18861 18862 /* 18863 * Determine if it's worthwhile to attempt LSO or MDT, based on: 18864 * 18865 * 1. Simple TCP/IP{v4,v6} (no options). 18866 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18867 * 3. If the TCP connection is in ESTABLISHED state. 18868 * 4. The TCP is not detached. 18869 * 18870 * If any of the above conditions have changed during the 18871 * connection, stop using LSO/MDT and restore the stream head 18872 * parameters accordingly. 18873 */ 18874 ipst = tcps->tcps_netstack->netstack_ip; 18875 18876 if ((tcp->tcp_lso || tcp->tcp_mdt) && 18877 ((tcp->tcp_ipversion == IPV4_VERSION && 18878 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18879 (tcp->tcp_ipversion == IPV6_VERSION && 18880 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18881 tcp->tcp_state != TCPS_ESTABLISHED || 18882 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 18883 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18884 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 18885 if (tcp->tcp_lso) { 18886 tcp->tcp_connp->conn_lso_ok = B_FALSE; 18887 tcp->tcp_lso = B_FALSE; 18888 } else { 18889 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18890 tcp->tcp_mdt = B_FALSE; 18891 } 18892 18893 /* Anything other than detached is considered pathological */ 18894 if (!TCP_IS_DETACHED(tcp)) { 18895 if (tcp->tcp_lso) 18896 TCP_STAT(tcps, tcp_lso_disabled); 18897 else 18898 TCP_STAT(tcps, tcp_mdt_conn_halted1); 18899 (void) tcp_maxpsz_set(tcp, B_TRUE); 18900 } 18901 } 18902 18903 /* Use MDT if sendable amount is greater than the threshold */ 18904 if (tcp->tcp_mdt && 18905 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18906 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18907 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18908 (tcp->tcp_valid_bits == 0 || 18909 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18910 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18911 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18912 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18913 local_time, mdt_thres); 18914 } else { 18915 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18916 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18917 local_time, INT_MAX); 18918 } 18919 18920 /* Pretend that all we were trying to send really got sent */ 18921 if (rc < 0 && tail_unsent < 0) { 18922 do { 18923 xmit_tail = xmit_tail->b_cont; 18924 xmit_tail->b_prev = local_time; 18925 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18926 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18927 tail_unsent += (int)(xmit_tail->b_wptr - 18928 xmit_tail->b_rptr); 18929 } while (tail_unsent < 0); 18930 } 18931 done:; 18932 tcp->tcp_xmit_tail = xmit_tail; 18933 tcp->tcp_xmit_tail_unsent = tail_unsent; 18934 len = tcp->tcp_snxt - snxt; 18935 if (len) { 18936 /* 18937 * If new data was sent, need to update the notsack 18938 * list, which is, afterall, data blocks that have 18939 * not been sack'ed by the receiver. New data is 18940 * not sack'ed. 18941 */ 18942 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18943 /* len is a negative value. */ 18944 tcp->tcp_pipe -= len; 18945 tcp_notsack_update(&(tcp->tcp_notsack_list), 18946 tcp->tcp_snxt, snxt, 18947 &(tcp->tcp_num_notsack_blk), 18948 &(tcp->tcp_cnt_notsack_list)); 18949 } 18950 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18951 tcp->tcp_rack = tcp->tcp_rnxt; 18952 tcp->tcp_rack_cnt = 0; 18953 if ((snxt + len) == tcp->tcp_suna) { 18954 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18955 } 18956 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18957 /* 18958 * Didn't send anything. Make sure the timer is running 18959 * so that we will probe a zero window. 18960 */ 18961 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18962 } 18963 /* Note that len is the amount we just sent but with a negative sign */ 18964 tcp->tcp_unsent += len; 18965 mutex_enter(&tcp->tcp_non_sq_lock); 18966 if (tcp->tcp_flow_stopped) { 18967 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18968 tcp_clrqfull(tcp); 18969 } 18970 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18971 tcp_setqfull(tcp); 18972 } 18973 mutex_exit(&tcp->tcp_non_sq_lock); 18974 } 18975 18976 /* 18977 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18978 * outgoing TCP header with the template header, as well as other 18979 * options such as time-stamp, ECN and/or SACK. 18980 */ 18981 static void 18982 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18983 { 18984 tcph_t *tcp_tmpl, *tcp_h; 18985 uint32_t *dst, *src; 18986 int hdrlen; 18987 18988 ASSERT(OK_32PTR(rptr)); 18989 18990 /* Template header */ 18991 tcp_tmpl = tcp->tcp_tcph; 18992 18993 /* Header of outgoing packet */ 18994 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18995 18996 /* dst and src are opaque 32-bit fields, used for copying */ 18997 dst = (uint32_t *)rptr; 18998 src = (uint32_t *)tcp->tcp_iphc; 18999 hdrlen = tcp->tcp_hdr_len; 19000 19001 /* Fill time-stamp option if needed */ 19002 if (tcp->tcp_snd_ts_ok) { 19003 U32_TO_BE32((uint32_t)now, 19004 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19005 U32_TO_BE32(tcp->tcp_ts_recent, 19006 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19007 } else { 19008 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19009 } 19010 19011 /* 19012 * Copy the template header; is this really more efficient than 19013 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19014 * but perhaps not for other scenarios. 19015 */ 19016 dst[0] = src[0]; 19017 dst[1] = src[1]; 19018 dst[2] = src[2]; 19019 dst[3] = src[3]; 19020 dst[4] = src[4]; 19021 dst[5] = src[5]; 19022 dst[6] = src[6]; 19023 dst[7] = src[7]; 19024 dst[8] = src[8]; 19025 dst[9] = src[9]; 19026 if (hdrlen -= 40) { 19027 hdrlen >>= 2; 19028 dst += 10; 19029 src += 10; 19030 do { 19031 *dst++ = *src++; 19032 } while (--hdrlen); 19033 } 19034 19035 /* 19036 * Set the ECN info in the TCP header if it is not a zero 19037 * window probe. Zero window probe is only sent in 19038 * tcp_wput_data() and tcp_timer(). 19039 */ 19040 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19041 SET_ECT(tcp, rptr); 19042 19043 if (tcp->tcp_ecn_echo_on) 19044 tcp_h->th_flags[0] |= TH_ECE; 19045 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19046 tcp_h->th_flags[0] |= TH_CWR; 19047 tcp->tcp_ecn_cwr_sent = B_TRUE; 19048 } 19049 } 19050 19051 /* Fill in SACK options */ 19052 if (num_sack_blk > 0) { 19053 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19054 sack_blk_t *tmp; 19055 int32_t i; 19056 19057 wptr[0] = TCPOPT_NOP; 19058 wptr[1] = TCPOPT_NOP; 19059 wptr[2] = TCPOPT_SACK; 19060 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19061 sizeof (sack_blk_t); 19062 wptr += TCPOPT_REAL_SACK_LEN; 19063 19064 tmp = tcp->tcp_sack_list; 19065 for (i = 0; i < num_sack_blk; i++) { 19066 U32_TO_BE32(tmp[i].begin, wptr); 19067 wptr += sizeof (tcp_seq); 19068 U32_TO_BE32(tmp[i].end, wptr); 19069 wptr += sizeof (tcp_seq); 19070 } 19071 tcp_h->th_offset_and_rsrvd[0] += 19072 ((num_sack_blk * 2 + 1) << 4); 19073 } 19074 } 19075 19076 /* 19077 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19078 * the destination address and SAP attribute, and if necessary, the 19079 * hardware checksum offload attribute to a Multidata message. 19080 */ 19081 static int 19082 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19083 const uint32_t start, const uint32_t stuff, const uint32_t end, 19084 const uint32_t flags, tcp_stack_t *tcps) 19085 { 19086 /* Add global destination address & SAP attribute */ 19087 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19088 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19089 "destination address+SAP\n")); 19090 19091 if (dlmp != NULL) 19092 TCP_STAT(tcps, tcp_mdt_allocfail); 19093 return (-1); 19094 } 19095 19096 /* Add global hwcksum attribute */ 19097 if (hwcksum && 19098 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19099 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19100 "checksum attribute\n")); 19101 19102 TCP_STAT(tcps, tcp_mdt_allocfail); 19103 return (-1); 19104 } 19105 19106 return (0); 19107 } 19108 19109 /* 19110 * Smaller and private version of pdescinfo_t used specifically for TCP, 19111 * which allows for only two payload spans per packet. 19112 */ 19113 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19114 19115 /* 19116 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19117 * scheme, and returns one the following: 19118 * 19119 * -1 = failed allocation. 19120 * 0 = success; burst count reached, or usable send window is too small, 19121 * and that we'd rather wait until later before sending again. 19122 */ 19123 static int 19124 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19125 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19126 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19127 const int mdt_thres) 19128 { 19129 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19130 multidata_t *mmd; 19131 uint_t obsegs, obbytes, hdr_frag_sz; 19132 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19133 int num_burst_seg, max_pld; 19134 pdesc_t *pkt; 19135 tcp_pdescinfo_t tcp_pkt_info; 19136 pdescinfo_t *pkt_info; 19137 int pbuf_idx, pbuf_idx_nxt; 19138 int seg_len, len, spill, af; 19139 boolean_t add_buffer, zcopy, clusterwide; 19140 boolean_t rconfirm = B_FALSE; 19141 boolean_t done = B_FALSE; 19142 uint32_t cksum; 19143 uint32_t hwcksum_flags; 19144 ire_t *ire = NULL; 19145 ill_t *ill; 19146 ipha_t *ipha; 19147 ip6_t *ip6h; 19148 ipaddr_t src, dst; 19149 ill_zerocopy_capab_t *zc_cap = NULL; 19150 uint16_t *up; 19151 int err; 19152 conn_t *connp; 19153 tcp_stack_t *tcps = tcp->tcp_tcps; 19154 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19155 int usable_mmd, tail_unsent_mmd; 19156 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19157 mblk_t *xmit_tail_mmd; 19158 netstackid_t stack_id; 19159 19160 #ifdef _BIG_ENDIAN 19161 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19162 #else 19163 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19164 #endif 19165 19166 #define PREP_NEW_MULTIDATA() { \ 19167 mmd = NULL; \ 19168 md_mp = md_hbuf = NULL; \ 19169 cur_hdr_off = 0; \ 19170 max_pld = tcp->tcp_mdt_max_pld; \ 19171 pbuf_idx = pbuf_idx_nxt = -1; \ 19172 add_buffer = B_TRUE; \ 19173 zcopy = B_FALSE; \ 19174 } 19175 19176 #define PREP_NEW_PBUF() { \ 19177 md_pbuf = md_pbuf_nxt = NULL; \ 19178 pbuf_idx = pbuf_idx_nxt = -1; \ 19179 cur_pld_off = 0; \ 19180 first_snxt = *snxt; \ 19181 ASSERT(*tail_unsent > 0); \ 19182 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19183 } 19184 19185 ASSERT(mdt_thres >= mss); 19186 ASSERT(*usable > 0 && *usable > mdt_thres); 19187 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19188 ASSERT(!TCP_IS_DETACHED(tcp)); 19189 ASSERT(tcp->tcp_valid_bits == 0 || 19190 tcp->tcp_valid_bits == TCP_FSS_VALID); 19191 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19192 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19193 (tcp->tcp_ipversion == IPV6_VERSION && 19194 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19195 19196 connp = tcp->tcp_connp; 19197 ASSERT(connp != NULL); 19198 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19199 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19200 19201 stack_id = connp->conn_netstack->netstack_stackid; 19202 19203 usable_mmd = tail_unsent_mmd = 0; 19204 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19205 xmit_tail_mmd = NULL; 19206 /* 19207 * Note that tcp will only declare at most 2 payload spans per 19208 * packet, which is much lower than the maximum allowable number 19209 * of packet spans per Multidata. For this reason, we use the 19210 * privately declared and smaller descriptor info structure, in 19211 * order to save some stack space. 19212 */ 19213 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19214 19215 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19216 if (af == AF_INET) { 19217 dst = tcp->tcp_ipha->ipha_dst; 19218 src = tcp->tcp_ipha->ipha_src; 19219 ASSERT(!CLASSD(dst)); 19220 } 19221 ASSERT(af == AF_INET || 19222 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19223 19224 obsegs = obbytes = 0; 19225 num_burst_seg = tcp->tcp_snd_burst; 19226 md_mp_head = NULL; 19227 PREP_NEW_MULTIDATA(); 19228 19229 /* 19230 * Before we go on further, make sure there is an IRE that we can 19231 * use, and that the ILL supports MDT. Otherwise, there's no point 19232 * in proceeding any further, and we should just hand everything 19233 * off to the legacy path. 19234 */ 19235 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19236 goto legacy_send_no_md; 19237 19238 ASSERT(ire != NULL); 19239 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19240 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19241 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19242 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19243 /* 19244 * If we do support loopback for MDT (which requires modifications 19245 * to the receiving paths), the following assertions should go away, 19246 * and we would be sending the Multidata to loopback conn later on. 19247 */ 19248 ASSERT(!IRE_IS_LOCAL(ire)); 19249 ASSERT(ire->ire_stq != NULL); 19250 19251 ill = ire_to_ill(ire); 19252 ASSERT(ill != NULL); 19253 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19254 19255 if (!tcp->tcp_ire_ill_check_done) { 19256 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19257 tcp->tcp_ire_ill_check_done = B_TRUE; 19258 } 19259 19260 /* 19261 * If the underlying interface conditions have changed, or if the 19262 * new interface does not support MDT, go back to legacy path. 19263 */ 19264 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19265 /* don't go through this path anymore for this connection */ 19266 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19267 tcp->tcp_mdt = B_FALSE; 19268 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19269 "interface %s\n", (void *)connp, ill->ill_name)); 19270 /* IRE will be released prior to returning */ 19271 goto legacy_send_no_md; 19272 } 19273 19274 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19275 zc_cap = ill->ill_zerocopy_capab; 19276 19277 /* 19278 * Check if we can take tcp fast-path. Note that "incomplete" 19279 * ire's (where the link-layer for next hop is not resolved 19280 * or where the fast-path header in nce_fp_mp is not available 19281 * yet) are sent down the legacy (slow) path. 19282 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19283 */ 19284 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19285 /* IRE will be released prior to returning */ 19286 goto legacy_send_no_md; 19287 } 19288 19289 /* go to legacy path if interface doesn't support zerocopy */ 19290 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19291 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19292 /* IRE will be released prior to returning */ 19293 goto legacy_send_no_md; 19294 } 19295 19296 /* does the interface support hardware checksum offload? */ 19297 hwcksum_flags = 0; 19298 if (ILL_HCKSUM_CAPABLE(ill) && 19299 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19300 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19301 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19302 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19303 HCKSUM_IPHDRCKSUM) 19304 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19305 19306 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19307 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19308 hwcksum_flags |= HCK_FULLCKSUM; 19309 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19310 HCKSUM_INET_PARTIAL) 19311 hwcksum_flags |= HCK_PARTIALCKSUM; 19312 } 19313 19314 /* 19315 * Each header fragment consists of the leading extra space, 19316 * followed by the TCP/IP header, and the trailing extra space. 19317 * We make sure that each header fragment begins on a 32-bit 19318 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19319 * aligned in tcp_mdt_update). 19320 */ 19321 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19322 tcp->tcp_mdt_hdr_tail), 4); 19323 19324 /* are we starting from the beginning of data block? */ 19325 if (*tail_unsent == 0) { 19326 *xmit_tail = (*xmit_tail)->b_cont; 19327 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19328 *tail_unsent = (int)MBLKL(*xmit_tail); 19329 } 19330 19331 /* 19332 * Here we create one or more Multidata messages, each made up of 19333 * one header buffer and up to N payload buffers. This entire 19334 * operation is done within two loops: 19335 * 19336 * The outer loop mostly deals with creating the Multidata message, 19337 * as well as the header buffer that gets added to it. It also 19338 * links the Multidata messages together such that all of them can 19339 * be sent down to the lower layer in a single putnext call; this 19340 * linking behavior depends on the tcp_mdt_chain tunable. 19341 * 19342 * The inner loop takes an existing Multidata message, and adds 19343 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19344 * packetizes those buffers by filling up the corresponding header 19345 * buffer fragments with the proper IP and TCP headers, and by 19346 * describing the layout of each packet in the packet descriptors 19347 * that get added to the Multidata. 19348 */ 19349 do { 19350 /* 19351 * If usable send window is too small, or data blocks in 19352 * transmit list are smaller than our threshold (i.e. app 19353 * performs large writes followed by small ones), we hand 19354 * off the control over to the legacy path. Note that we'll 19355 * get back the control once it encounters a large block. 19356 */ 19357 if (*usable < mss || (*tail_unsent <= mdt_thres && 19358 (*xmit_tail)->b_cont != NULL && 19359 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19360 /* send down what we've got so far */ 19361 if (md_mp_head != NULL) { 19362 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19363 obsegs, obbytes, &rconfirm); 19364 } 19365 /* 19366 * Pass control over to tcp_send(), but tell it to 19367 * return to us once a large-size transmission is 19368 * possible. 19369 */ 19370 TCP_STAT(tcps, tcp_mdt_legacy_small); 19371 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19372 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19373 tail_unsent, xmit_tail, local_time, 19374 mdt_thres)) <= 0) { 19375 /* burst count reached, or alloc failed */ 19376 IRE_REFRELE(ire); 19377 return (err); 19378 } 19379 19380 /* tcp_send() may have sent everything, so check */ 19381 if (*usable <= 0) { 19382 IRE_REFRELE(ire); 19383 return (0); 19384 } 19385 19386 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19387 /* 19388 * We may have delivered the Multidata, so make sure 19389 * to re-initialize before the next round. 19390 */ 19391 md_mp_head = NULL; 19392 obsegs = obbytes = 0; 19393 num_burst_seg = tcp->tcp_snd_burst; 19394 PREP_NEW_MULTIDATA(); 19395 19396 /* are we starting from the beginning of data block? */ 19397 if (*tail_unsent == 0) { 19398 *xmit_tail = (*xmit_tail)->b_cont; 19399 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19400 (uintptr_t)INT_MAX); 19401 *tail_unsent = (int)MBLKL(*xmit_tail); 19402 } 19403 } 19404 /* 19405 * Record current values for parameters we may need to pass 19406 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19407 * each iteration of the outer loop (each multidata message 19408 * creation). If we have a failure in the inner loop, we send 19409 * any complete multidata messages we have before reverting 19410 * to using the traditional non-md path. 19411 */ 19412 snxt_mmd = *snxt; 19413 usable_mmd = *usable; 19414 xmit_tail_mmd = *xmit_tail; 19415 tail_unsent_mmd = *tail_unsent; 19416 obsegs_mmd = obsegs; 19417 obbytes_mmd = obbytes; 19418 19419 /* 19420 * max_pld limits the number of mblks in tcp's transmit 19421 * queue that can be added to a Multidata message. Once 19422 * this counter reaches zero, no more additional mblks 19423 * can be added to it. What happens afterwards depends 19424 * on whether or not we are set to chain the Multidata 19425 * messages. If we are to link them together, reset 19426 * max_pld to its original value (tcp_mdt_max_pld) and 19427 * prepare to create a new Multidata message which will 19428 * get linked to md_mp_head. Else, leave it alone and 19429 * let the inner loop break on its own. 19430 */ 19431 if (tcp_mdt_chain && max_pld == 0) 19432 PREP_NEW_MULTIDATA(); 19433 19434 /* adding a payload buffer; re-initialize values */ 19435 if (add_buffer) 19436 PREP_NEW_PBUF(); 19437 19438 /* 19439 * If we don't have a Multidata, either because we just 19440 * (re)entered this outer loop, or after we branched off 19441 * to tcp_send above, setup the Multidata and header 19442 * buffer to be used. 19443 */ 19444 if (md_mp == NULL) { 19445 int md_hbuflen; 19446 uint32_t start, stuff; 19447 19448 /* 19449 * Calculate Multidata header buffer size large enough 19450 * to hold all of the headers that can possibly be 19451 * sent at this moment. We'd rather over-estimate 19452 * the size than running out of space; this is okay 19453 * since this buffer is small anyway. 19454 */ 19455 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19456 19457 /* 19458 * Start and stuff offset for partial hardware 19459 * checksum offload; these are currently for IPv4. 19460 * For full checksum offload, they are set to zero. 19461 */ 19462 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19463 if (af == AF_INET) { 19464 start = IP_SIMPLE_HDR_LENGTH; 19465 stuff = IP_SIMPLE_HDR_LENGTH + 19466 TCP_CHECKSUM_OFFSET; 19467 } else { 19468 start = IPV6_HDR_LEN; 19469 stuff = IPV6_HDR_LEN + 19470 TCP_CHECKSUM_OFFSET; 19471 } 19472 } else { 19473 start = stuff = 0; 19474 } 19475 19476 /* 19477 * Create the header buffer, Multidata, as well as 19478 * any necessary attributes (destination address, 19479 * SAP and hardware checksum offload) that should 19480 * be associated with the Multidata message. 19481 */ 19482 ASSERT(cur_hdr_off == 0); 19483 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19484 ((md_hbuf->b_wptr += md_hbuflen), 19485 (mmd = mmd_alloc(md_hbuf, &md_mp, 19486 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19487 /* fastpath mblk */ 19488 ire->ire_nce->nce_res_mp, 19489 /* hardware checksum enabled */ 19490 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19491 /* hardware checksum offsets */ 19492 start, stuff, 0, 19493 /* hardware checksum flag */ 19494 hwcksum_flags, tcps) != 0)) { 19495 legacy_send: 19496 /* 19497 * We arrive here from a failure within the 19498 * inner (packetizer) loop or we fail one of 19499 * the conditionals above. We restore the 19500 * previously checkpointed values for: 19501 * xmit_tail 19502 * usable 19503 * tail_unsent 19504 * snxt 19505 * obbytes 19506 * obsegs 19507 * We should then be able to dispatch any 19508 * complete multidata before reverting to the 19509 * traditional path with consistent parameters 19510 * (the inner loop updates these as it 19511 * iterates). 19512 */ 19513 *xmit_tail = xmit_tail_mmd; 19514 *usable = usable_mmd; 19515 *tail_unsent = tail_unsent_mmd; 19516 *snxt = snxt_mmd; 19517 obbytes = obbytes_mmd; 19518 obsegs = obsegs_mmd; 19519 if (md_mp != NULL) { 19520 /* Unlink message from the chain */ 19521 if (md_mp_head != NULL) { 19522 err = (intptr_t)rmvb(md_mp_head, 19523 md_mp); 19524 /* 19525 * We can't assert that rmvb 19526 * did not return -1, since we 19527 * may get here before linkb 19528 * happens. We do, however, 19529 * check if we just removed the 19530 * only element in the list. 19531 */ 19532 if (err == 0) 19533 md_mp_head = NULL; 19534 } 19535 /* md_hbuf gets freed automatically */ 19536 TCP_STAT(tcps, tcp_mdt_discarded); 19537 freeb(md_mp); 19538 } else { 19539 /* Either allocb or mmd_alloc failed */ 19540 TCP_STAT(tcps, tcp_mdt_allocfail); 19541 if (md_hbuf != NULL) 19542 freeb(md_hbuf); 19543 } 19544 19545 /* send down what we've got so far */ 19546 if (md_mp_head != NULL) { 19547 tcp_multisend_data(tcp, ire, ill, 19548 md_mp_head, obsegs, obbytes, 19549 &rconfirm); 19550 } 19551 legacy_send_no_md: 19552 if (ire != NULL) 19553 IRE_REFRELE(ire); 19554 /* 19555 * Too bad; let the legacy path handle this. 19556 * We specify INT_MAX for the threshold, since 19557 * we gave up with the Multidata processings 19558 * and let the old path have it all. 19559 */ 19560 TCP_STAT(tcps, tcp_mdt_legacy_all); 19561 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19562 tcp_tcp_hdr_len, num_sack_blk, usable, 19563 snxt, tail_unsent, xmit_tail, local_time, 19564 INT_MAX)); 19565 } 19566 19567 /* link to any existing ones, if applicable */ 19568 TCP_STAT(tcps, tcp_mdt_allocd); 19569 if (md_mp_head == NULL) { 19570 md_mp_head = md_mp; 19571 } else if (tcp_mdt_chain) { 19572 TCP_STAT(tcps, tcp_mdt_linked); 19573 linkb(md_mp_head, md_mp); 19574 } 19575 } 19576 19577 ASSERT(md_mp_head != NULL); 19578 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19579 ASSERT(md_mp != NULL && mmd != NULL); 19580 ASSERT(md_hbuf != NULL); 19581 19582 /* 19583 * Packetize the transmittable portion of the data block; 19584 * each data block is essentially added to the Multidata 19585 * as a payload buffer. We also deal with adding more 19586 * than one payload buffers, which happens when the remaining 19587 * packetized portion of the current payload buffer is less 19588 * than MSS, while the next data block in transmit queue 19589 * has enough data to make up for one. This "spillover" 19590 * case essentially creates a split-packet, where portions 19591 * of the packet's payload fragments may span across two 19592 * virtually discontiguous address blocks. 19593 */ 19594 seg_len = mss; 19595 do { 19596 len = seg_len; 19597 19598 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19599 ipha = NULL; 19600 ip6h = NULL; 19601 19602 ASSERT(len > 0); 19603 ASSERT(max_pld >= 0); 19604 ASSERT(!add_buffer || cur_pld_off == 0); 19605 19606 /* 19607 * First time around for this payload buffer; note 19608 * in the case of a spillover, the following has 19609 * been done prior to adding the split-packet 19610 * descriptor to Multidata, and we don't want to 19611 * repeat the process. 19612 */ 19613 if (add_buffer) { 19614 ASSERT(mmd != NULL); 19615 ASSERT(md_pbuf == NULL); 19616 ASSERT(md_pbuf_nxt == NULL); 19617 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19618 19619 /* 19620 * Have we reached the limit? We'd get to 19621 * this case when we're not chaining the 19622 * Multidata messages together, and since 19623 * we're done, terminate this loop. 19624 */ 19625 if (max_pld == 0) 19626 break; /* done */ 19627 19628 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19629 TCP_STAT(tcps, tcp_mdt_allocfail); 19630 goto legacy_send; /* out_of_mem */ 19631 } 19632 19633 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19634 zc_cap != NULL) { 19635 if (!ip_md_zcopy_attr(mmd, NULL, 19636 zc_cap->ill_zerocopy_flags)) { 19637 freeb(md_pbuf); 19638 TCP_STAT(tcps, 19639 tcp_mdt_allocfail); 19640 /* out_of_mem */ 19641 goto legacy_send; 19642 } 19643 zcopy = B_TRUE; 19644 } 19645 19646 md_pbuf->b_rptr += base_pld_off; 19647 19648 /* 19649 * Add a payload buffer to the Multidata; this 19650 * operation must not fail, or otherwise our 19651 * logic in this routine is broken. There 19652 * is no memory allocation done by the 19653 * routine, so any returned failure simply 19654 * tells us that we've done something wrong. 19655 * 19656 * A failure tells us that either we're adding 19657 * the same payload buffer more than once, or 19658 * we're trying to add more buffers than 19659 * allowed (max_pld calculation is wrong). 19660 * None of the above cases should happen, and 19661 * we panic because either there's horrible 19662 * heap corruption, and/or programming mistake. 19663 */ 19664 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19665 if (pbuf_idx < 0) { 19666 cmn_err(CE_PANIC, "tcp_multisend: " 19667 "payload buffer logic error " 19668 "detected for tcp %p mmd %p " 19669 "pbuf %p (%d)\n", 19670 (void *)tcp, (void *)mmd, 19671 (void *)md_pbuf, pbuf_idx); 19672 } 19673 19674 ASSERT(max_pld > 0); 19675 --max_pld; 19676 add_buffer = B_FALSE; 19677 } 19678 19679 ASSERT(md_mp_head != NULL); 19680 ASSERT(md_pbuf != NULL); 19681 ASSERT(md_pbuf_nxt == NULL); 19682 ASSERT(pbuf_idx != -1); 19683 ASSERT(pbuf_idx_nxt == -1); 19684 ASSERT(*usable > 0); 19685 19686 /* 19687 * We spillover to the next payload buffer only 19688 * if all of the following is true: 19689 * 19690 * 1. There is not enough data on the current 19691 * payload buffer to make up `len', 19692 * 2. We are allowed to send `len', 19693 * 3. The next payload buffer length is large 19694 * enough to accomodate `spill'. 19695 */ 19696 if ((spill = len - *tail_unsent) > 0 && 19697 *usable >= len && 19698 MBLKL((*xmit_tail)->b_cont) >= spill && 19699 max_pld > 0) { 19700 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19701 if (md_pbuf_nxt == NULL) { 19702 TCP_STAT(tcps, tcp_mdt_allocfail); 19703 goto legacy_send; /* out_of_mem */ 19704 } 19705 19706 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19707 zc_cap != NULL) { 19708 if (!ip_md_zcopy_attr(mmd, NULL, 19709 zc_cap->ill_zerocopy_flags)) { 19710 freeb(md_pbuf_nxt); 19711 TCP_STAT(tcps, 19712 tcp_mdt_allocfail); 19713 /* out_of_mem */ 19714 goto legacy_send; 19715 } 19716 zcopy = B_TRUE; 19717 } 19718 19719 /* 19720 * See comments above on the first call to 19721 * mmd_addpldbuf for explanation on the panic. 19722 */ 19723 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19724 if (pbuf_idx_nxt < 0) { 19725 panic("tcp_multisend: " 19726 "next payload buffer logic error " 19727 "detected for tcp %p mmd %p " 19728 "pbuf %p (%d)\n", 19729 (void *)tcp, (void *)mmd, 19730 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19731 } 19732 19733 ASSERT(max_pld > 0); 19734 --max_pld; 19735 } else if (spill > 0) { 19736 /* 19737 * If there's a spillover, but the following 19738 * xmit_tail couldn't give us enough octets 19739 * to reach "len", then stop the current 19740 * Multidata creation and let the legacy 19741 * tcp_send() path take over. We don't want 19742 * to send the tiny segment as part of this 19743 * Multidata for performance reasons; instead, 19744 * we let the legacy path deal with grouping 19745 * it with the subsequent small mblks. 19746 */ 19747 if (*usable >= len && 19748 MBLKL((*xmit_tail)->b_cont) < spill) { 19749 max_pld = 0; 19750 break; /* done */ 19751 } 19752 19753 /* 19754 * We can't spillover, and we are near 19755 * the end of the current payload buffer, 19756 * so send what's left. 19757 */ 19758 ASSERT(*tail_unsent > 0); 19759 len = *tail_unsent; 19760 } 19761 19762 /* tail_unsent is negated if there is a spillover */ 19763 *tail_unsent -= len; 19764 *usable -= len; 19765 ASSERT(*usable >= 0); 19766 19767 if (*usable < mss) 19768 seg_len = *usable; 19769 /* 19770 * Sender SWS avoidance; see comments in tcp_send(); 19771 * everything else is the same, except that we only 19772 * do this here if there is no more data to be sent 19773 * following the current xmit_tail. We don't check 19774 * for 1-byte urgent data because we shouldn't get 19775 * here if TCP_URG_VALID is set. 19776 */ 19777 if (*usable > 0 && *usable < mss && 19778 ((md_pbuf_nxt == NULL && 19779 (*xmit_tail)->b_cont == NULL) || 19780 (md_pbuf_nxt != NULL && 19781 (*xmit_tail)->b_cont->b_cont == NULL)) && 19782 seg_len < (tcp->tcp_max_swnd >> 1) && 19783 (tcp->tcp_unsent - 19784 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19785 !tcp->tcp_zero_win_probe) { 19786 if ((*snxt + len) == tcp->tcp_snxt && 19787 (*snxt + len) == tcp->tcp_suna) { 19788 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19789 } 19790 done = B_TRUE; 19791 } 19792 19793 /* 19794 * Prime pump for IP's checksumming on our behalf; 19795 * include the adjustment for a source route if any. 19796 * Do this only for software/partial hardware checksum 19797 * offload, as this field gets zeroed out later for 19798 * the full hardware checksum offload case. 19799 */ 19800 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19801 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19802 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19803 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19804 } 19805 19806 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19807 *snxt += len; 19808 19809 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19810 /* 19811 * We set the PUSH bit only if TCP has no more buffered 19812 * data to be transmitted (or if sender SWS avoidance 19813 * takes place), as opposed to setting it for every 19814 * last packet in the burst. 19815 */ 19816 if (done || 19817 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19818 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19819 19820 /* 19821 * Set FIN bit if this is our last segment; snxt 19822 * already includes its length, and it will not 19823 * be adjusted after this point. 19824 */ 19825 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19826 *snxt == tcp->tcp_fss) { 19827 if (!tcp->tcp_fin_acked) { 19828 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19829 BUMP_MIB(&tcps->tcps_mib, 19830 tcpOutControl); 19831 } 19832 if (!tcp->tcp_fin_sent) { 19833 tcp->tcp_fin_sent = B_TRUE; 19834 /* 19835 * tcp state must be ESTABLISHED 19836 * in order for us to get here in 19837 * the first place. 19838 */ 19839 tcp->tcp_state = TCPS_FIN_WAIT_1; 19840 19841 /* 19842 * Upon returning from this routine, 19843 * tcp_wput_data() will set tcp_snxt 19844 * to be equal to snxt + tcp_fin_sent. 19845 * This is essentially the same as 19846 * setting it to tcp_fss + 1. 19847 */ 19848 } 19849 } 19850 19851 tcp->tcp_last_sent_len = (ushort_t)len; 19852 19853 len += tcp_hdr_len; 19854 if (tcp->tcp_ipversion == IPV4_VERSION) 19855 tcp->tcp_ipha->ipha_length = htons(len); 19856 else 19857 tcp->tcp_ip6h->ip6_plen = htons(len - 19858 ((char *)&tcp->tcp_ip6h[1] - 19859 tcp->tcp_iphc)); 19860 19861 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19862 19863 /* setup header fragment */ 19864 PDESC_HDR_ADD(pkt_info, 19865 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19866 tcp->tcp_mdt_hdr_head, /* head room */ 19867 tcp_hdr_len, /* len */ 19868 tcp->tcp_mdt_hdr_tail); /* tail room */ 19869 19870 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19871 hdr_frag_sz); 19872 ASSERT(MBLKIN(md_hbuf, 19873 (pkt_info->hdr_base - md_hbuf->b_rptr), 19874 PDESC_HDRSIZE(pkt_info))); 19875 19876 /* setup first payload fragment */ 19877 PDESC_PLD_INIT(pkt_info); 19878 PDESC_PLD_SPAN_ADD(pkt_info, 19879 pbuf_idx, /* index */ 19880 md_pbuf->b_rptr + cur_pld_off, /* start */ 19881 tcp->tcp_last_sent_len); /* len */ 19882 19883 /* create a split-packet in case of a spillover */ 19884 if (md_pbuf_nxt != NULL) { 19885 ASSERT(spill > 0); 19886 ASSERT(pbuf_idx_nxt > pbuf_idx); 19887 ASSERT(!add_buffer); 19888 19889 md_pbuf = md_pbuf_nxt; 19890 md_pbuf_nxt = NULL; 19891 pbuf_idx = pbuf_idx_nxt; 19892 pbuf_idx_nxt = -1; 19893 cur_pld_off = spill; 19894 19895 /* trim out first payload fragment */ 19896 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19897 19898 /* setup second payload fragment */ 19899 PDESC_PLD_SPAN_ADD(pkt_info, 19900 pbuf_idx, /* index */ 19901 md_pbuf->b_rptr, /* start */ 19902 spill); /* len */ 19903 19904 if ((*xmit_tail)->b_next == NULL) { 19905 /* 19906 * Store the lbolt used for RTT 19907 * estimation. We can only record one 19908 * timestamp per mblk so we do it when 19909 * we reach the end of the payload 19910 * buffer. Also we only take a new 19911 * timestamp sample when the previous 19912 * timed data from the same mblk has 19913 * been ack'ed. 19914 */ 19915 (*xmit_tail)->b_prev = local_time; 19916 (*xmit_tail)->b_next = 19917 (mblk_t *)(uintptr_t)first_snxt; 19918 } 19919 19920 first_snxt = *snxt - spill; 19921 19922 /* 19923 * Advance xmit_tail; usable could be 0 by 19924 * the time we got here, but we made sure 19925 * above that we would only spillover to 19926 * the next data block if usable includes 19927 * the spilled-over amount prior to the 19928 * subtraction. Therefore, we are sure 19929 * that xmit_tail->b_cont can't be NULL. 19930 */ 19931 ASSERT((*xmit_tail)->b_cont != NULL); 19932 *xmit_tail = (*xmit_tail)->b_cont; 19933 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19934 (uintptr_t)INT_MAX); 19935 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19936 } else { 19937 cur_pld_off += tcp->tcp_last_sent_len; 19938 } 19939 19940 /* 19941 * Fill in the header using the template header, and 19942 * add options such as time-stamp, ECN and/or SACK, 19943 * as needed. 19944 */ 19945 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19946 (clock_t)local_time, num_sack_blk); 19947 19948 /* take care of some IP header businesses */ 19949 if (af == AF_INET) { 19950 ipha = (ipha_t *)pkt_info->hdr_rptr; 19951 19952 ASSERT(OK_32PTR((uchar_t *)ipha)); 19953 ASSERT(PDESC_HDRL(pkt_info) >= 19954 IP_SIMPLE_HDR_LENGTH); 19955 ASSERT(ipha->ipha_version_and_hdr_length == 19956 IP_SIMPLE_HDR_VERSION); 19957 19958 /* 19959 * Assign ident value for current packet; see 19960 * related comments in ip_wput_ire() about the 19961 * contract private interface with clustering 19962 * group. 19963 */ 19964 clusterwide = B_FALSE; 19965 if (cl_inet_ipident != NULL) { 19966 ASSERT(cl_inet_isclusterwide != NULL); 19967 if ((*cl_inet_isclusterwide)(stack_id, 19968 IPPROTO_IP, AF_INET, 19969 (uint8_t *)(uintptr_t)src, NULL)) { 19970 ipha->ipha_ident = 19971 (*cl_inet_ipident)(stack_id, 19972 IPPROTO_IP, AF_INET, 19973 (uint8_t *)(uintptr_t)src, 19974 (uint8_t *)(uintptr_t)dst, 19975 NULL); 19976 clusterwide = B_TRUE; 19977 } 19978 } 19979 19980 if (!clusterwide) { 19981 ipha->ipha_ident = (uint16_t) 19982 atomic_add_32_nv( 19983 &ire->ire_ident, 1); 19984 } 19985 #ifndef _BIG_ENDIAN 19986 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19987 (ipha->ipha_ident >> 8); 19988 #endif 19989 } else { 19990 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19991 19992 ASSERT(OK_32PTR((uchar_t *)ip6h)); 19993 ASSERT(IPVER(ip6h) == IPV6_VERSION); 19994 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 19995 ASSERT(PDESC_HDRL(pkt_info) >= 19996 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 19997 TCP_CHECKSUM_SIZE)); 19998 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 19999 20000 if (tcp->tcp_ip_forward_progress) { 20001 rconfirm = B_TRUE; 20002 tcp->tcp_ip_forward_progress = B_FALSE; 20003 } 20004 } 20005 20006 /* at least one payload span, and at most two */ 20007 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20008 20009 /* add the packet descriptor to Multidata */ 20010 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20011 KM_NOSLEEP)) == NULL) { 20012 /* 20013 * Any failure other than ENOMEM indicates 20014 * that we have passed in invalid pkt_info 20015 * or parameters to mmd_addpdesc, which must 20016 * not happen. 20017 * 20018 * EINVAL is a result of failure on boundary 20019 * checks against the pkt_info contents. It 20020 * should not happen, and we panic because 20021 * either there's horrible heap corruption, 20022 * and/or programming mistake. 20023 */ 20024 if (err != ENOMEM) { 20025 cmn_err(CE_PANIC, "tcp_multisend: " 20026 "pdesc logic error detected for " 20027 "tcp %p mmd %p pinfo %p (%d)\n", 20028 (void *)tcp, (void *)mmd, 20029 (void *)pkt_info, err); 20030 } 20031 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20032 goto legacy_send; /* out_of_mem */ 20033 } 20034 ASSERT(pkt != NULL); 20035 20036 /* calculate IP header and TCP checksums */ 20037 if (af == AF_INET) { 20038 /* calculate pseudo-header checksum */ 20039 cksum = (dst >> 16) + (dst & 0xFFFF) + 20040 (src >> 16) + (src & 0xFFFF); 20041 20042 /* offset for TCP header checksum */ 20043 up = IPH_TCPH_CHECKSUMP(ipha, 20044 IP_SIMPLE_HDR_LENGTH); 20045 } else { 20046 up = (uint16_t *)&ip6h->ip6_src; 20047 20048 /* calculate pseudo-header checksum */ 20049 cksum = up[0] + up[1] + up[2] + up[3] + 20050 up[4] + up[5] + up[6] + up[7] + 20051 up[8] + up[9] + up[10] + up[11] + 20052 up[12] + up[13] + up[14] + up[15]; 20053 20054 /* Fold the initial sum */ 20055 cksum = (cksum & 0xffff) + (cksum >> 16); 20056 20057 up = (uint16_t *)(((uchar_t *)ip6h) + 20058 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20059 } 20060 20061 if (hwcksum_flags & HCK_FULLCKSUM) { 20062 /* clear checksum field for hardware */ 20063 *up = 0; 20064 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20065 uint32_t sum; 20066 20067 /* pseudo-header checksumming */ 20068 sum = *up + cksum + IP_TCP_CSUM_COMP; 20069 sum = (sum & 0xFFFF) + (sum >> 16); 20070 *up = (sum & 0xFFFF) + (sum >> 16); 20071 } else { 20072 /* software checksumming */ 20073 TCP_STAT(tcps, tcp_out_sw_cksum); 20074 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20075 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20076 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20077 cksum + IP_TCP_CSUM_COMP); 20078 if (*up == 0) 20079 *up = 0xFFFF; 20080 } 20081 20082 /* IPv4 header checksum */ 20083 if (af == AF_INET) { 20084 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20085 ipha->ipha_hdr_checksum = 0; 20086 } else { 20087 IP_HDR_CKSUM(ipha, cksum, 20088 ((uint32_t *)ipha)[0], 20089 ((uint16_t *)ipha)[4]); 20090 } 20091 } 20092 20093 if (af == AF_INET && 20094 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20095 af == AF_INET6 && 20096 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20097 mblk_t *mp, *mp1; 20098 uchar_t *hdr_rptr, *hdr_wptr; 20099 uchar_t *pld_rptr, *pld_wptr; 20100 20101 /* 20102 * We reconstruct a pseudo packet for the hooks 20103 * framework using mmd_transform_link(). 20104 * If it is a split packet we pullup the 20105 * payload. FW_HOOKS expects a pkt comprising 20106 * of two mblks: a header and the payload. 20107 */ 20108 if ((mp = mmd_transform_link(pkt)) == NULL) { 20109 TCP_STAT(tcps, tcp_mdt_allocfail); 20110 goto legacy_send; 20111 } 20112 20113 if (pkt_info->pld_cnt > 1) { 20114 /* split payload, more than one pld */ 20115 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20116 NULL) { 20117 freemsg(mp); 20118 TCP_STAT(tcps, 20119 tcp_mdt_allocfail); 20120 goto legacy_send; 20121 } 20122 freemsg(mp->b_cont); 20123 mp->b_cont = mp1; 20124 } else { 20125 mp1 = mp->b_cont; 20126 } 20127 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20128 20129 /* 20130 * Remember the message offsets. This is so we 20131 * can detect changes when we return from the 20132 * FW_HOOKS callbacks. 20133 */ 20134 hdr_rptr = mp->b_rptr; 20135 hdr_wptr = mp->b_wptr; 20136 pld_rptr = mp->b_cont->b_rptr; 20137 pld_wptr = mp->b_cont->b_wptr; 20138 20139 if (af == AF_INET) { 20140 DTRACE_PROBE4( 20141 ip4__physical__out__start, 20142 ill_t *, NULL, 20143 ill_t *, ill, 20144 ipha_t *, ipha, 20145 mblk_t *, mp); 20146 FW_HOOKS( 20147 ipst->ips_ip4_physical_out_event, 20148 ipst->ips_ipv4firewall_physical_out, 20149 NULL, ill, ipha, mp, mp, 0, ipst); 20150 DTRACE_PROBE1( 20151 ip4__physical__out__end, 20152 mblk_t *, mp); 20153 } else { 20154 DTRACE_PROBE4( 20155 ip6__physical__out_start, 20156 ill_t *, NULL, 20157 ill_t *, ill, 20158 ip6_t *, ip6h, 20159 mblk_t *, mp); 20160 FW_HOOKS6( 20161 ipst->ips_ip6_physical_out_event, 20162 ipst->ips_ipv6firewall_physical_out, 20163 NULL, ill, ip6h, mp, mp, 0, ipst); 20164 DTRACE_PROBE1( 20165 ip6__physical__out__end, 20166 mblk_t *, mp); 20167 } 20168 20169 if (mp == NULL || 20170 (mp1 = mp->b_cont) == NULL || 20171 mp->b_rptr != hdr_rptr || 20172 mp->b_wptr != hdr_wptr || 20173 mp1->b_rptr != pld_rptr || 20174 mp1->b_wptr != pld_wptr || 20175 mp1->b_cont != NULL) { 20176 /* 20177 * We abandon multidata processing and 20178 * return to the normal path, either 20179 * when a packet is blocked, or when 20180 * the boundaries of header buffer or 20181 * payload buffer have been changed by 20182 * FW_HOOKS[6]. 20183 */ 20184 if (mp != NULL) 20185 freemsg(mp); 20186 goto legacy_send; 20187 } 20188 /* Finished with the pseudo packet */ 20189 freemsg(mp); 20190 } 20191 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20192 ill, ipha, ip6h); 20193 /* advance header offset */ 20194 cur_hdr_off += hdr_frag_sz; 20195 20196 obbytes += tcp->tcp_last_sent_len; 20197 ++obsegs; 20198 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20199 *tail_unsent > 0); 20200 20201 if ((*xmit_tail)->b_next == NULL) { 20202 /* 20203 * Store the lbolt used for RTT estimation. We can only 20204 * record one timestamp per mblk so we do it when we 20205 * reach the end of the payload buffer. Also we only 20206 * take a new timestamp sample when the previous timed 20207 * data from the same mblk has been ack'ed. 20208 */ 20209 (*xmit_tail)->b_prev = local_time; 20210 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20211 } 20212 20213 ASSERT(*tail_unsent >= 0); 20214 if (*tail_unsent > 0) { 20215 /* 20216 * We got here because we broke out of the above 20217 * loop due to of one of the following cases: 20218 * 20219 * 1. len < adjusted MSS (i.e. small), 20220 * 2. Sender SWS avoidance, 20221 * 3. max_pld is zero. 20222 * 20223 * We are done for this Multidata, so trim our 20224 * last payload buffer (if any) accordingly. 20225 */ 20226 if (md_pbuf != NULL) 20227 md_pbuf->b_wptr -= *tail_unsent; 20228 } else if (*usable > 0) { 20229 *xmit_tail = (*xmit_tail)->b_cont; 20230 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20231 (uintptr_t)INT_MAX); 20232 *tail_unsent = (int)MBLKL(*xmit_tail); 20233 add_buffer = B_TRUE; 20234 } 20235 } while (!done && *usable > 0 && num_burst_seg > 0 && 20236 (tcp_mdt_chain || max_pld > 0)); 20237 20238 if (md_mp_head != NULL) { 20239 /* send everything down */ 20240 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20241 &rconfirm); 20242 } 20243 20244 #undef PREP_NEW_MULTIDATA 20245 #undef PREP_NEW_PBUF 20246 #undef IPVER 20247 20248 IRE_REFRELE(ire); 20249 return (0); 20250 } 20251 20252 /* 20253 * A wrapper function for sending one or more Multidata messages down to 20254 * the module below ip; this routine does not release the reference of the 20255 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20256 */ 20257 static void 20258 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20259 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20260 { 20261 uint64_t delta; 20262 nce_t *nce; 20263 tcp_stack_t *tcps = tcp->tcp_tcps; 20264 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20265 20266 ASSERT(ire != NULL && ill != NULL); 20267 ASSERT(ire->ire_stq != NULL); 20268 ASSERT(md_mp_head != NULL); 20269 ASSERT(rconfirm != NULL); 20270 20271 /* adjust MIBs and IRE timestamp */ 20272 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20273 tcp->tcp_obsegs += obsegs; 20274 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20275 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20276 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20277 20278 if (tcp->tcp_ipversion == IPV4_VERSION) { 20279 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20280 } else { 20281 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20282 } 20283 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20284 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20285 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20286 20287 ire->ire_ob_pkt_count += obsegs; 20288 if (ire->ire_ipif != NULL) 20289 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20290 ire->ire_last_used_time = lbolt; 20291 20292 if (ipst->ips_ipobs_enabled) { 20293 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20294 pdesc_t *dl_pkt; 20295 pdescinfo_t pinfo; 20296 mblk_t *nmp; 20297 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20298 20299 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20300 (dl_pkt != NULL); 20301 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20302 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20303 continue; 20304 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20305 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20306 freemsg(nmp); 20307 } 20308 } 20309 20310 /* send it down */ 20311 putnext(ire->ire_stq, md_mp_head); 20312 20313 /* we're done for TCP/IPv4 */ 20314 if (tcp->tcp_ipversion == IPV4_VERSION) 20315 return; 20316 20317 nce = ire->ire_nce; 20318 20319 ASSERT(nce != NULL); 20320 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20321 ASSERT(nce->nce_state != ND_INCOMPLETE); 20322 20323 /* reachability confirmation? */ 20324 if (*rconfirm) { 20325 nce->nce_last = TICK_TO_MSEC(lbolt64); 20326 if (nce->nce_state != ND_REACHABLE) { 20327 mutex_enter(&nce->nce_lock); 20328 nce->nce_state = ND_REACHABLE; 20329 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20330 mutex_exit(&nce->nce_lock); 20331 (void) untimeout(nce->nce_timeout_id); 20332 if (ip_debug > 2) { 20333 /* ip1dbg */ 20334 pr_addr_dbg("tcp_multisend_data: state " 20335 "for %s changed to REACHABLE\n", 20336 AF_INET6, &ire->ire_addr_v6); 20337 } 20338 } 20339 /* reset transport reachability confirmation */ 20340 *rconfirm = B_FALSE; 20341 } 20342 20343 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20344 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20345 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20346 20347 if (delta > (uint64_t)ill->ill_reachable_time) { 20348 mutex_enter(&nce->nce_lock); 20349 switch (nce->nce_state) { 20350 case ND_REACHABLE: 20351 case ND_STALE: 20352 /* 20353 * ND_REACHABLE is identical to ND_STALE in this 20354 * specific case. If reachable time has expired for 20355 * this neighbor (delta is greater than reachable 20356 * time), conceptually, the neighbor cache is no 20357 * longer in REACHABLE state, but already in STALE 20358 * state. So the correct transition here is to 20359 * ND_DELAY. 20360 */ 20361 nce->nce_state = ND_DELAY; 20362 mutex_exit(&nce->nce_lock); 20363 NDP_RESTART_TIMER(nce, 20364 ipst->ips_delay_first_probe_time); 20365 if (ip_debug > 3) { 20366 /* ip2dbg */ 20367 pr_addr_dbg("tcp_multisend_data: state " 20368 "for %s changed to DELAY\n", 20369 AF_INET6, &ire->ire_addr_v6); 20370 } 20371 break; 20372 case ND_DELAY: 20373 case ND_PROBE: 20374 mutex_exit(&nce->nce_lock); 20375 /* Timers have already started */ 20376 break; 20377 case ND_UNREACHABLE: 20378 /* 20379 * ndp timer has detected that this nce is 20380 * unreachable and initiated deleting this nce 20381 * and all its associated IREs. This is a race 20382 * where we found the ire before it was deleted 20383 * and have just sent out a packet using this 20384 * unreachable nce. 20385 */ 20386 mutex_exit(&nce->nce_lock); 20387 break; 20388 default: 20389 ASSERT(0); 20390 } 20391 } 20392 } 20393 20394 /* 20395 * Derived from tcp_send_data(). 20396 */ 20397 static void 20398 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20399 int num_lso_seg) 20400 { 20401 ipha_t *ipha; 20402 mblk_t *ire_fp_mp; 20403 uint_t ire_fp_mp_len; 20404 uint32_t hcksum_txflags = 0; 20405 ipaddr_t src; 20406 ipaddr_t dst; 20407 uint32_t cksum; 20408 uint16_t *up; 20409 tcp_stack_t *tcps = tcp->tcp_tcps; 20410 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20411 20412 ASSERT(DB_TYPE(mp) == M_DATA); 20413 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20414 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20415 ASSERT(tcp->tcp_connp != NULL); 20416 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20417 20418 ipha = (ipha_t *)mp->b_rptr; 20419 src = ipha->ipha_src; 20420 dst = ipha->ipha_dst; 20421 20422 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20423 20424 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20425 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20426 num_lso_seg); 20427 #ifndef _BIG_ENDIAN 20428 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20429 #endif 20430 if (tcp->tcp_snd_zcopy_aware) { 20431 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20432 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20433 mp = tcp_zcopy_disable(tcp, mp); 20434 } 20435 20436 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20437 ASSERT(ill->ill_hcksum_capab != NULL); 20438 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20439 } 20440 20441 /* 20442 * Since the TCP checksum should be recalculated by h/w, we can just 20443 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20444 * pseudo-header checksum for HCK_PARTIALCKSUM. 20445 * The partial pseudo-header excludes TCP length, that was calculated 20446 * in tcp_send(), so to zero *up before further processing. 20447 */ 20448 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20449 20450 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20451 *up = 0; 20452 20453 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20454 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20455 20456 /* 20457 * Append LSO flags and mss to the mp. 20458 */ 20459 lso_info_set(mp, mss, HW_LSO); 20460 20461 ipha->ipha_fragment_offset_and_flags |= 20462 (uint32_t)htons(ire->ire_frag_flag); 20463 20464 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20465 ire_fp_mp_len = MBLKL(ire_fp_mp); 20466 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20467 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20468 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20469 20470 UPDATE_OB_PKT_COUNT(ire); 20471 ire->ire_last_used_time = lbolt; 20472 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20473 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20474 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20475 ntohs(ipha->ipha_length)); 20476 20477 DTRACE_PROBE4(ip4__physical__out__start, 20478 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20479 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20480 ipst->ips_ipv4firewall_physical_out, NULL, 20481 ill, ipha, mp, mp, 0, ipst); 20482 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20483 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20484 20485 if (mp != NULL) { 20486 if (ipst->ips_ipobs_enabled) { 20487 zoneid_t szone; 20488 20489 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20490 ipst, ALL_ZONES); 20491 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20492 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20493 } 20494 20495 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20496 } 20497 } 20498 20499 /* 20500 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20501 * scheme, and returns one of the following: 20502 * 20503 * -1 = failed allocation. 20504 * 0 = success; burst count reached, or usable send window is too small, 20505 * and that we'd rather wait until later before sending again. 20506 * 1 = success; we are called from tcp_multisend(), and both usable send 20507 * window and tail_unsent are greater than the MDT threshold, and thus 20508 * Multidata Transmit should be used instead. 20509 */ 20510 static int 20511 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20512 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20513 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20514 const int mdt_thres) 20515 { 20516 int num_burst_seg = tcp->tcp_snd_burst; 20517 ire_t *ire = NULL; 20518 ill_t *ill = NULL; 20519 mblk_t *ire_fp_mp = NULL; 20520 uint_t ire_fp_mp_len = 0; 20521 int num_lso_seg = 1; 20522 uint_t lso_usable; 20523 boolean_t do_lso_send = B_FALSE; 20524 tcp_stack_t *tcps = tcp->tcp_tcps; 20525 20526 /* 20527 * Check LSO capability before any further work. And the similar check 20528 * need to be done in for(;;) loop. 20529 * LSO will be deployed when therer is more than one mss of available 20530 * data and a burst transmission is allowed. 20531 */ 20532 if (tcp->tcp_lso && 20533 (tcp->tcp_valid_bits == 0 || 20534 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20535 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20536 /* 20537 * Try to find usable IRE/ILL and do basic check to the ILL. 20538 * Double check LSO usability before going further, since the 20539 * underlying interface could have been changed. In case of any 20540 * change of LSO capability, set tcp_ire_ill_check_done to 20541 * B_FALSE to force to check the ILL with the next send. 20542 */ 20543 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20544 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20545 /* 20546 * Enable LSO with this transmission. 20547 * Since IRE has been hold in tcp_send_find_ire_ill(), 20548 * IRE_REFRELE(ire) should be called before return. 20549 */ 20550 do_lso_send = B_TRUE; 20551 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20552 ire_fp_mp_len = MBLKL(ire_fp_mp); 20553 /* Round up to multiple of 4 */ 20554 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20555 } else { 20556 tcp->tcp_lso = B_FALSE; 20557 tcp->tcp_ire_ill_check_done = B_FALSE; 20558 do_lso_send = B_FALSE; 20559 ill = NULL; 20560 } 20561 } 20562 20563 for (;;) { 20564 struct datab *db; 20565 tcph_t *tcph; 20566 uint32_t sum; 20567 mblk_t *mp, *mp1; 20568 uchar_t *rptr; 20569 int len; 20570 20571 /* 20572 * If we're called by tcp_multisend(), and the amount of 20573 * sendable data as well as the size of current xmit_tail 20574 * is beyond the MDT threshold, return to the caller and 20575 * let the large data transmit be done using MDT. 20576 */ 20577 if (*usable > 0 && *usable > mdt_thres && 20578 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20579 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20580 ASSERT(tcp->tcp_mdt); 20581 return (1); /* success; do large send */ 20582 } 20583 20584 if (num_burst_seg == 0) 20585 break; /* success; burst count reached */ 20586 20587 /* 20588 * Calculate the maximum payload length we can send in *one* 20589 * time. 20590 */ 20591 if (do_lso_send) { 20592 /* 20593 * Check whether need to do LSO any more. 20594 */ 20595 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20596 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20597 lso_usable = MIN(lso_usable, 20598 num_burst_seg * mss); 20599 20600 num_lso_seg = lso_usable / mss; 20601 if (lso_usable % mss) { 20602 num_lso_seg++; 20603 tcp->tcp_last_sent_len = (ushort_t) 20604 (lso_usable % mss); 20605 } else { 20606 tcp->tcp_last_sent_len = (ushort_t)mss; 20607 } 20608 } else { 20609 do_lso_send = B_FALSE; 20610 num_lso_seg = 1; 20611 lso_usable = mss; 20612 } 20613 } 20614 20615 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20616 20617 /* 20618 * Adjust num_burst_seg here. 20619 */ 20620 num_burst_seg -= num_lso_seg; 20621 20622 len = mss; 20623 if (len > *usable) { 20624 ASSERT(do_lso_send == B_FALSE); 20625 20626 len = *usable; 20627 if (len <= 0) { 20628 /* Terminate the loop */ 20629 break; /* success; too small */ 20630 } 20631 /* 20632 * Sender silly-window avoidance. 20633 * Ignore this if we are going to send a 20634 * zero window probe out. 20635 * 20636 * TODO: force data into microscopic window? 20637 * ==> (!pushed || (unsent > usable)) 20638 */ 20639 if (len < (tcp->tcp_max_swnd >> 1) && 20640 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20641 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20642 len == 1) && (! tcp->tcp_zero_win_probe)) { 20643 /* 20644 * If the retransmit timer is not running 20645 * we start it so that we will retransmit 20646 * in the case when the the receiver has 20647 * decremented the window. 20648 */ 20649 if (*snxt == tcp->tcp_snxt && 20650 *snxt == tcp->tcp_suna) { 20651 /* 20652 * We are not supposed to send 20653 * anything. So let's wait a little 20654 * bit longer before breaking SWS 20655 * avoidance. 20656 * 20657 * What should the value be? 20658 * Suggestion: MAX(init rexmit time, 20659 * tcp->tcp_rto) 20660 */ 20661 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20662 } 20663 break; /* success; too small */ 20664 } 20665 } 20666 20667 tcph = tcp->tcp_tcph; 20668 20669 /* 20670 * The reason to adjust len here is that we need to set flags 20671 * and calculate checksum. 20672 */ 20673 if (do_lso_send) 20674 len = lso_usable; 20675 20676 *usable -= len; /* Approximate - can be adjusted later */ 20677 if (*usable > 0) 20678 tcph->th_flags[0] = TH_ACK; 20679 else 20680 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20681 20682 /* 20683 * Prime pump for IP's checksumming on our behalf 20684 * Include the adjustment for a source route if any. 20685 */ 20686 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20687 sum = (sum >> 16) + (sum & 0xFFFF); 20688 U16_TO_ABE16(sum, tcph->th_sum); 20689 20690 U32_TO_ABE32(*snxt, tcph->th_seq); 20691 20692 /* 20693 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20694 * set. For the case when TCP_FSS_VALID is the only valid 20695 * bit (normal active close), branch off only when we think 20696 * that the FIN flag needs to be set. Note for this case, 20697 * that (snxt + len) may not reflect the actual seg_len, 20698 * as len may be further reduced in tcp_xmit_mp(). If len 20699 * gets modified, we will end up here again. 20700 */ 20701 if (tcp->tcp_valid_bits != 0 && 20702 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20703 ((*snxt + len) == tcp->tcp_fss))) { 20704 uchar_t *prev_rptr; 20705 uint32_t prev_snxt = tcp->tcp_snxt; 20706 20707 if (*tail_unsent == 0) { 20708 ASSERT((*xmit_tail)->b_cont != NULL); 20709 *xmit_tail = (*xmit_tail)->b_cont; 20710 prev_rptr = (*xmit_tail)->b_rptr; 20711 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20712 (*xmit_tail)->b_rptr); 20713 } else { 20714 prev_rptr = (*xmit_tail)->b_rptr; 20715 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20716 *tail_unsent; 20717 } 20718 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20719 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20720 /* Restore tcp_snxt so we get amount sent right. */ 20721 tcp->tcp_snxt = prev_snxt; 20722 if (prev_rptr == (*xmit_tail)->b_rptr) { 20723 /* 20724 * If the previous timestamp is still in use, 20725 * don't stomp on it. 20726 */ 20727 if ((*xmit_tail)->b_next == NULL) { 20728 (*xmit_tail)->b_prev = local_time; 20729 (*xmit_tail)->b_next = 20730 (mblk_t *)(uintptr_t)(*snxt); 20731 } 20732 } else 20733 (*xmit_tail)->b_rptr = prev_rptr; 20734 20735 if (mp == NULL) { 20736 if (ire != NULL) 20737 IRE_REFRELE(ire); 20738 return (-1); 20739 } 20740 mp1 = mp->b_cont; 20741 20742 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20743 tcp->tcp_last_sent_len = (ushort_t)len; 20744 while (mp1->b_cont) { 20745 *xmit_tail = (*xmit_tail)->b_cont; 20746 (*xmit_tail)->b_prev = local_time; 20747 (*xmit_tail)->b_next = 20748 (mblk_t *)(uintptr_t)(*snxt); 20749 mp1 = mp1->b_cont; 20750 } 20751 *snxt += len; 20752 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20753 BUMP_LOCAL(tcp->tcp_obsegs); 20754 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20755 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20756 tcp_send_data(tcp, q, mp); 20757 continue; 20758 } 20759 20760 *snxt += len; /* Adjust later if we don't send all of len */ 20761 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20762 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20763 20764 if (*tail_unsent) { 20765 /* Are the bytes above us in flight? */ 20766 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20767 if (rptr != (*xmit_tail)->b_rptr) { 20768 *tail_unsent -= len; 20769 if (len <= mss) /* LSO is unusable */ 20770 tcp->tcp_last_sent_len = (ushort_t)len; 20771 len += tcp_hdr_len; 20772 if (tcp->tcp_ipversion == IPV4_VERSION) 20773 tcp->tcp_ipha->ipha_length = htons(len); 20774 else 20775 tcp->tcp_ip6h->ip6_plen = 20776 htons(len - 20777 ((char *)&tcp->tcp_ip6h[1] - 20778 tcp->tcp_iphc)); 20779 mp = dupb(*xmit_tail); 20780 if (mp == NULL) { 20781 if (ire != NULL) 20782 IRE_REFRELE(ire); 20783 return (-1); /* out_of_mem */ 20784 } 20785 mp->b_rptr = rptr; 20786 /* 20787 * If the old timestamp is no longer in use, 20788 * sample a new timestamp now. 20789 */ 20790 if ((*xmit_tail)->b_next == NULL) { 20791 (*xmit_tail)->b_prev = local_time; 20792 (*xmit_tail)->b_next = 20793 (mblk_t *)(uintptr_t)(*snxt-len); 20794 } 20795 goto must_alloc; 20796 } 20797 } else { 20798 *xmit_tail = (*xmit_tail)->b_cont; 20799 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20800 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20801 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20802 (*xmit_tail)->b_rptr); 20803 } 20804 20805 (*xmit_tail)->b_prev = local_time; 20806 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20807 20808 *tail_unsent -= len; 20809 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20810 tcp->tcp_last_sent_len = (ushort_t)len; 20811 20812 len += tcp_hdr_len; 20813 if (tcp->tcp_ipversion == IPV4_VERSION) 20814 tcp->tcp_ipha->ipha_length = htons(len); 20815 else 20816 tcp->tcp_ip6h->ip6_plen = htons(len - 20817 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20818 20819 mp = dupb(*xmit_tail); 20820 if (mp == NULL) { 20821 if (ire != NULL) 20822 IRE_REFRELE(ire); 20823 return (-1); /* out_of_mem */ 20824 } 20825 20826 len = tcp_hdr_len; 20827 /* 20828 * There are four reasons to allocate a new hdr mblk: 20829 * 1) The bytes above us are in use by another packet 20830 * 2) We don't have good alignment 20831 * 3) The mblk is being shared 20832 * 4) We don't have enough room for a header 20833 */ 20834 rptr = mp->b_rptr - len; 20835 if (!OK_32PTR(rptr) || 20836 ((db = mp->b_datap), db->db_ref != 2) || 20837 rptr < db->db_base + ire_fp_mp_len) { 20838 /* NOTE: we assume allocb returns an OK_32PTR */ 20839 20840 must_alloc:; 20841 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20842 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 20843 if (mp1 == NULL) { 20844 freemsg(mp); 20845 if (ire != NULL) 20846 IRE_REFRELE(ire); 20847 return (-1); /* out_of_mem */ 20848 } 20849 mp1->b_cont = mp; 20850 mp = mp1; 20851 /* Leave room for Link Level header */ 20852 len = tcp_hdr_len; 20853 rptr = 20854 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 20855 mp->b_wptr = &rptr[len]; 20856 } 20857 20858 /* 20859 * Fill in the header using the template header, and add 20860 * options such as time-stamp, ECN and/or SACK, as needed. 20861 */ 20862 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20863 20864 mp->b_rptr = rptr; 20865 20866 if (*tail_unsent) { 20867 int spill = *tail_unsent; 20868 20869 mp1 = mp->b_cont; 20870 if (mp1 == NULL) 20871 mp1 = mp; 20872 20873 /* 20874 * If we're a little short, tack on more mblks until 20875 * there is no more spillover. 20876 */ 20877 while (spill < 0) { 20878 mblk_t *nmp; 20879 int nmpsz; 20880 20881 nmp = (*xmit_tail)->b_cont; 20882 nmpsz = MBLKL(nmp); 20883 20884 /* 20885 * Excess data in mblk; can we split it? 20886 * If MDT is enabled for the connection, 20887 * keep on splitting as this is a transient 20888 * send path. 20889 */ 20890 if (!do_lso_send && !tcp->tcp_mdt && 20891 (spill + nmpsz > 0)) { 20892 /* 20893 * Don't split if stream head was 20894 * told to break up larger writes 20895 * into smaller ones. 20896 */ 20897 if (tcp->tcp_maxpsz > 0) 20898 break; 20899 20900 /* 20901 * Next mblk is less than SMSS/2 20902 * rounded up to nearest 64-byte; 20903 * let it get sent as part of the 20904 * next segment. 20905 */ 20906 if (tcp->tcp_localnet && 20907 !tcp->tcp_cork && 20908 (nmpsz < roundup((mss >> 1), 64))) 20909 break; 20910 } 20911 20912 *xmit_tail = nmp; 20913 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20914 /* Stash for rtt use later */ 20915 (*xmit_tail)->b_prev = local_time; 20916 (*xmit_tail)->b_next = 20917 (mblk_t *)(uintptr_t)(*snxt - len); 20918 mp1->b_cont = dupb(*xmit_tail); 20919 mp1 = mp1->b_cont; 20920 20921 spill += nmpsz; 20922 if (mp1 == NULL) { 20923 *tail_unsent = spill; 20924 freemsg(mp); 20925 if (ire != NULL) 20926 IRE_REFRELE(ire); 20927 return (-1); /* out_of_mem */ 20928 } 20929 } 20930 20931 /* Trim back any surplus on the last mblk */ 20932 if (spill >= 0) { 20933 mp1->b_wptr -= spill; 20934 *tail_unsent = spill; 20935 } else { 20936 /* 20937 * We did not send everything we could in 20938 * order to remain within the b_cont limit. 20939 */ 20940 *usable -= spill; 20941 *snxt += spill; 20942 tcp->tcp_last_sent_len += spill; 20943 UPDATE_MIB(&tcps->tcps_mib, 20944 tcpOutDataBytes, spill); 20945 /* 20946 * Adjust the checksum 20947 */ 20948 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20949 sum += spill; 20950 sum = (sum >> 16) + (sum & 0xFFFF); 20951 U16_TO_ABE16(sum, tcph->th_sum); 20952 if (tcp->tcp_ipversion == IPV4_VERSION) { 20953 sum = ntohs( 20954 ((ipha_t *)rptr)->ipha_length) + 20955 spill; 20956 ((ipha_t *)rptr)->ipha_length = 20957 htons(sum); 20958 } else { 20959 sum = ntohs( 20960 ((ip6_t *)rptr)->ip6_plen) + 20961 spill; 20962 ((ip6_t *)rptr)->ip6_plen = 20963 htons(sum); 20964 } 20965 *tail_unsent = 0; 20966 } 20967 } 20968 if (tcp->tcp_ip_forward_progress) { 20969 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20970 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20971 tcp->tcp_ip_forward_progress = B_FALSE; 20972 } 20973 20974 if (do_lso_send) { 20975 tcp_lsosend_data(tcp, mp, ire, ill, mss, 20976 num_lso_seg); 20977 tcp->tcp_obsegs += num_lso_seg; 20978 20979 TCP_STAT(tcps, tcp_lso_times); 20980 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 20981 } else { 20982 tcp_send_data(tcp, q, mp); 20983 BUMP_LOCAL(tcp->tcp_obsegs); 20984 } 20985 } 20986 20987 if (ire != NULL) 20988 IRE_REFRELE(ire); 20989 return (0); 20990 } 20991 20992 /* Unlink and return any mblk that looks like it contains a MDT info */ 20993 static mblk_t * 20994 tcp_mdt_info_mp(mblk_t *mp) 20995 { 20996 mblk_t *prev_mp; 20997 20998 for (;;) { 20999 prev_mp = mp; 21000 /* no more to process? */ 21001 if ((mp = mp->b_cont) == NULL) 21002 break; 21003 21004 switch (DB_TYPE(mp)) { 21005 case M_CTL: 21006 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21007 continue; 21008 ASSERT(prev_mp != NULL); 21009 prev_mp->b_cont = mp->b_cont; 21010 mp->b_cont = NULL; 21011 return (mp); 21012 default: 21013 break; 21014 } 21015 } 21016 return (mp); 21017 } 21018 21019 /* MDT info update routine, called when IP notifies us about MDT */ 21020 static void 21021 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21022 { 21023 boolean_t prev_state; 21024 tcp_stack_t *tcps = tcp->tcp_tcps; 21025 21026 /* 21027 * IP is telling us to abort MDT on this connection? We know 21028 * this because the capability is only turned off when IP 21029 * encounters some pathological cases, e.g. link-layer change 21030 * where the new driver doesn't support MDT, or in situation 21031 * where MDT usage on the link-layer has been switched off. 21032 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21033 * if the link-layer doesn't support MDT, and if it does, it 21034 * will indicate that the feature is to be turned on. 21035 */ 21036 prev_state = tcp->tcp_mdt; 21037 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21038 if (!tcp->tcp_mdt && !first) { 21039 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21040 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21041 (void *)tcp->tcp_connp)); 21042 } 21043 21044 /* 21045 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21046 * so disable MDT otherwise. The checks are done here 21047 * and in tcp_wput_data(). 21048 */ 21049 if (tcp->tcp_mdt && 21050 (tcp->tcp_ipversion == IPV4_VERSION && 21051 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21052 (tcp->tcp_ipversion == IPV6_VERSION && 21053 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21054 tcp->tcp_mdt = B_FALSE; 21055 21056 if (tcp->tcp_mdt) { 21057 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21058 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21059 "version (%d), expected version is %d", 21060 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21061 tcp->tcp_mdt = B_FALSE; 21062 return; 21063 } 21064 21065 /* 21066 * We need the driver to be able to handle at least three 21067 * spans per packet in order for tcp MDT to be utilized. 21068 * The first is for the header portion, while the rest are 21069 * needed to handle a packet that straddles across two 21070 * virtually non-contiguous buffers; a typical tcp packet 21071 * therefore consists of only two spans. Note that we take 21072 * a zero as "don't care". 21073 */ 21074 if (mdt_capab->ill_mdt_span_limit > 0 && 21075 mdt_capab->ill_mdt_span_limit < 3) { 21076 tcp->tcp_mdt = B_FALSE; 21077 return; 21078 } 21079 21080 /* a zero means driver wants default value */ 21081 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21082 tcps->tcps_mdt_max_pbufs); 21083 if (tcp->tcp_mdt_max_pld == 0) 21084 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21085 21086 /* ensure 32-bit alignment */ 21087 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21088 mdt_capab->ill_mdt_hdr_head), 4); 21089 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21090 mdt_capab->ill_mdt_hdr_tail), 4); 21091 21092 if (!first && !prev_state) { 21093 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21094 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21095 (void *)tcp->tcp_connp)); 21096 } 21097 } 21098 } 21099 21100 /* Unlink and return any mblk that looks like it contains a LSO info */ 21101 static mblk_t * 21102 tcp_lso_info_mp(mblk_t *mp) 21103 { 21104 mblk_t *prev_mp; 21105 21106 for (;;) { 21107 prev_mp = mp; 21108 /* no more to process? */ 21109 if ((mp = mp->b_cont) == NULL) 21110 break; 21111 21112 switch (DB_TYPE(mp)) { 21113 case M_CTL: 21114 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21115 continue; 21116 ASSERT(prev_mp != NULL); 21117 prev_mp->b_cont = mp->b_cont; 21118 mp->b_cont = NULL; 21119 return (mp); 21120 default: 21121 break; 21122 } 21123 } 21124 21125 return (mp); 21126 } 21127 21128 /* LSO info update routine, called when IP notifies us about LSO */ 21129 static void 21130 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21131 { 21132 tcp_stack_t *tcps = tcp->tcp_tcps; 21133 21134 /* 21135 * IP is telling us to abort LSO on this connection? We know 21136 * this because the capability is only turned off when IP 21137 * encounters some pathological cases, e.g. link-layer change 21138 * where the new NIC/driver doesn't support LSO, or in situation 21139 * where LSO usage on the link-layer has been switched off. 21140 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21141 * if the link-layer doesn't support LSO, and if it does, it 21142 * will indicate that the feature is to be turned on. 21143 */ 21144 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21145 TCP_STAT(tcps, tcp_lso_enabled); 21146 21147 /* 21148 * We currently only support LSO on simple TCP/IPv4, 21149 * so disable LSO otherwise. The checks are done here 21150 * and in tcp_wput_data(). 21151 */ 21152 if (tcp->tcp_lso && 21153 (tcp->tcp_ipversion == IPV4_VERSION && 21154 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21155 (tcp->tcp_ipversion == IPV6_VERSION)) { 21156 tcp->tcp_lso = B_FALSE; 21157 TCP_STAT(tcps, tcp_lso_disabled); 21158 } else { 21159 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21160 lso_capab->ill_lso_max); 21161 } 21162 } 21163 21164 static void 21165 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21166 { 21167 conn_t *connp = tcp->tcp_connp; 21168 tcp_stack_t *tcps = tcp->tcp_tcps; 21169 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21170 21171 ASSERT(ire != NULL); 21172 21173 /* 21174 * We may be in the fastpath here, and although we essentially do 21175 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21176 * we try to keep things as brief as possible. After all, these 21177 * are only best-effort checks, and we do more thorough ones prior 21178 * to calling tcp_send()/tcp_multisend(). 21179 */ 21180 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21181 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21182 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21183 !(ire->ire_flags & RTF_MULTIRT) && 21184 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21185 CONN_IS_LSO_MD_FASTPATH(connp)) { 21186 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21187 /* Cache the result */ 21188 connp->conn_lso_ok = B_TRUE; 21189 21190 ASSERT(ill->ill_lso_capab != NULL); 21191 if (!ill->ill_lso_capab->ill_lso_on) { 21192 ill->ill_lso_capab->ill_lso_on = 1; 21193 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21194 "LSO for interface %s\n", (void *)connp, 21195 ill->ill_name)); 21196 } 21197 tcp_lso_update(tcp, ill->ill_lso_capab); 21198 } else if (ipst->ips_ip_multidata_outbound && 21199 ILL_MDT_CAPABLE(ill)) { 21200 /* Cache the result */ 21201 connp->conn_mdt_ok = B_TRUE; 21202 21203 ASSERT(ill->ill_mdt_capab != NULL); 21204 if (!ill->ill_mdt_capab->ill_mdt_on) { 21205 ill->ill_mdt_capab->ill_mdt_on = 1; 21206 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21207 "MDT for interface %s\n", (void *)connp, 21208 ill->ill_name)); 21209 } 21210 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21211 } 21212 } 21213 21214 /* 21215 * The goal is to reduce the number of generated tcp segments by 21216 * setting the maxpsz multiplier to 0; this will have an affect on 21217 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21218 * into each packet, up to SMSS bytes. Doing this reduces the number 21219 * of outbound segments and incoming ACKs, thus allowing for better 21220 * network and system performance. In contrast the legacy behavior 21221 * may result in sending less than SMSS size, because the last mblk 21222 * for some packets may have more data than needed to make up SMSS, 21223 * and the legacy code refused to "split" it. 21224 * 21225 * We apply the new behavior on following situations: 21226 * 21227 * 1) Loopback connections, 21228 * 2) Connections in which the remote peer is not on local subnet, 21229 * 3) Local subnet connections over the bge interface (see below). 21230 * 21231 * Ideally, we would like this behavior to apply for interfaces other 21232 * than bge. However, doing so would negatively impact drivers which 21233 * perform dynamic mapping and unmapping of DMA resources, which are 21234 * increased by setting the maxpsz multiplier to 0 (more mblks per 21235 * packet will be generated by tcp). The bge driver does not suffer 21236 * from this, as it copies the mblks into pre-mapped buffers, and 21237 * therefore does not require more I/O resources than before. 21238 * 21239 * Otherwise, this behavior is present on all network interfaces when 21240 * the destination endpoint is non-local, since reducing the number 21241 * of packets in general is good for the network. 21242 * 21243 * TODO We need to remove this hard-coded conditional for bge once 21244 * a better "self-tuning" mechanism, or a way to comprehend 21245 * the driver transmit strategy is devised. Until the solution 21246 * is found and well understood, we live with this hack. 21247 */ 21248 if (!tcp_static_maxpsz && 21249 (tcp->tcp_loopback || !tcp->tcp_localnet || 21250 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21251 /* override the default value */ 21252 tcp->tcp_maxpsz = 0; 21253 21254 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21255 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21256 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21257 } 21258 21259 /* set the stream head parameters accordingly */ 21260 (void) tcp_maxpsz_set(tcp, B_TRUE); 21261 } 21262 21263 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21264 static void 21265 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21266 { 21267 uchar_t fval = *mp->b_rptr; 21268 mblk_t *tail; 21269 queue_t *q = tcp->tcp_wq; 21270 21271 /* TODO: How should flush interact with urgent data? */ 21272 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21273 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21274 /* 21275 * Flush only data that has not yet been put on the wire. If 21276 * we flush data that we have already transmitted, life, as we 21277 * know it, may come to an end. 21278 */ 21279 tail = tcp->tcp_xmit_tail; 21280 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21281 tcp->tcp_xmit_tail_unsent = 0; 21282 tcp->tcp_unsent = 0; 21283 if (tail->b_wptr != tail->b_rptr) 21284 tail = tail->b_cont; 21285 if (tail) { 21286 mblk_t **excess = &tcp->tcp_xmit_head; 21287 for (;;) { 21288 mblk_t *mp1 = *excess; 21289 if (mp1 == tail) 21290 break; 21291 tcp->tcp_xmit_tail = mp1; 21292 tcp->tcp_xmit_last = mp1; 21293 excess = &mp1->b_cont; 21294 } 21295 *excess = NULL; 21296 tcp_close_mpp(&tail); 21297 if (tcp->tcp_snd_zcopy_aware) 21298 tcp_zcopy_notify(tcp); 21299 } 21300 /* 21301 * We have no unsent data, so unsent must be less than 21302 * tcp_xmit_lowater, so re-enable flow. 21303 */ 21304 mutex_enter(&tcp->tcp_non_sq_lock); 21305 if (tcp->tcp_flow_stopped) { 21306 tcp_clrqfull(tcp); 21307 } 21308 mutex_exit(&tcp->tcp_non_sq_lock); 21309 } 21310 /* 21311 * TODO: you can't just flush these, you have to increase rwnd for one 21312 * thing. For another, how should urgent data interact? 21313 */ 21314 if (fval & FLUSHR) { 21315 *mp->b_rptr = fval & ~FLUSHW; 21316 /* XXX */ 21317 qreply(q, mp); 21318 return; 21319 } 21320 freemsg(mp); 21321 } 21322 21323 /* 21324 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21325 * messages. 21326 */ 21327 static void 21328 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21329 { 21330 mblk_t *mp1; 21331 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21332 STRUCT_HANDLE(strbuf, sb); 21333 queue_t *q = tcp->tcp_wq; 21334 int error; 21335 uint_t addrlen; 21336 21337 /* Make sure it is one of ours. */ 21338 switch (iocp->ioc_cmd) { 21339 case TI_GETMYNAME: 21340 case TI_GETPEERNAME: 21341 break; 21342 default: 21343 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21344 return; 21345 } 21346 switch (mi_copy_state(q, mp, &mp1)) { 21347 case -1: 21348 return; 21349 case MI_COPY_CASE(MI_COPY_IN, 1): 21350 break; 21351 case MI_COPY_CASE(MI_COPY_OUT, 1): 21352 /* Copy out the strbuf. */ 21353 mi_copyout(q, mp); 21354 return; 21355 case MI_COPY_CASE(MI_COPY_OUT, 2): 21356 /* All done. */ 21357 mi_copy_done(q, mp, 0); 21358 return; 21359 default: 21360 mi_copy_done(q, mp, EPROTO); 21361 return; 21362 } 21363 /* Check alignment of the strbuf */ 21364 if (!OK_32PTR(mp1->b_rptr)) { 21365 mi_copy_done(q, mp, EINVAL); 21366 return; 21367 } 21368 21369 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21370 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21371 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21372 mi_copy_done(q, mp, EINVAL); 21373 return; 21374 } 21375 21376 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21377 if (mp1 == NULL) 21378 return; 21379 21380 switch (iocp->ioc_cmd) { 21381 case TI_GETMYNAME: 21382 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21383 break; 21384 case TI_GETPEERNAME: 21385 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21386 break; 21387 } 21388 21389 if (error != 0) { 21390 mi_copy_done(q, mp, error); 21391 } else { 21392 mp1->b_wptr += addrlen; 21393 STRUCT_FSET(sb, len, addrlen); 21394 21395 /* Copy out the address */ 21396 mi_copyout(q, mp); 21397 } 21398 } 21399 21400 static void 21401 tcp_disable_direct_sockfs(tcp_t *tcp) 21402 { 21403 #ifdef _ILP32 21404 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21405 #else 21406 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21407 #endif 21408 /* 21409 * Insert this socket into the acceptor hash. 21410 * We might need it for T_CONN_RES message 21411 */ 21412 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21413 21414 if (tcp->tcp_fused) { 21415 /* 21416 * This is a fused loopback tcp; disable 21417 * read-side synchronous streams interface 21418 * and drain any queued data. It is okay 21419 * to do this for non-synchronous streams 21420 * fused tcp as well. 21421 */ 21422 tcp_fuse_disable_pair(tcp, B_FALSE); 21423 } 21424 tcp->tcp_issocket = B_FALSE; 21425 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21426 } 21427 21428 /* 21429 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21430 * messages. 21431 */ 21432 /* ARGSUSED */ 21433 static void 21434 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21435 { 21436 conn_t *connp = (conn_t *)arg; 21437 tcp_t *tcp = connp->conn_tcp; 21438 queue_t *q = tcp->tcp_wq; 21439 struct iocblk *iocp; 21440 21441 ASSERT(DB_TYPE(mp) == M_IOCTL); 21442 /* 21443 * Try and ASSERT the minimum possible references on the 21444 * conn early enough. Since we are executing on write side, 21445 * the connection is obviously not detached and that means 21446 * there is a ref each for TCP and IP. Since we are behind 21447 * the squeue, the minimum references needed are 3. If the 21448 * conn is in classifier hash list, there should be an 21449 * extra ref for that (we check both the possibilities). 21450 */ 21451 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21452 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21453 21454 iocp = (struct iocblk *)mp->b_rptr; 21455 switch (iocp->ioc_cmd) { 21456 case TCP_IOC_DEFAULT_Q: 21457 /* Wants to be the default wq. */ 21458 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21459 iocp->ioc_error = EPERM; 21460 iocp->ioc_count = 0; 21461 mp->b_datap->db_type = M_IOCACK; 21462 qreply(q, mp); 21463 return; 21464 } 21465 tcp_def_q_set(tcp, mp); 21466 return; 21467 case _SIOCSOCKFALLBACK: 21468 /* 21469 * Either sockmod is about to be popped and the socket 21470 * would now be treated as a plain stream, or a module 21471 * is about to be pushed so we could no longer use read- 21472 * side synchronous streams for fused loopback tcp. 21473 * Drain any queued data and disable direct sockfs 21474 * interface from now on. 21475 */ 21476 if (!tcp->tcp_issocket) { 21477 DB_TYPE(mp) = M_IOCNAK; 21478 iocp->ioc_error = EINVAL; 21479 } else { 21480 tcp_disable_direct_sockfs(tcp); 21481 DB_TYPE(mp) = M_IOCACK; 21482 iocp->ioc_error = 0; 21483 } 21484 iocp->ioc_count = 0; 21485 iocp->ioc_rval = 0; 21486 qreply(q, mp); 21487 return; 21488 } 21489 CALL_IP_WPUT(connp, q, mp); 21490 } 21491 21492 /* 21493 * This routine is called by tcp_wput() to handle all TPI requests. 21494 */ 21495 /* ARGSUSED */ 21496 static void 21497 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21498 { 21499 conn_t *connp = (conn_t *)arg; 21500 tcp_t *tcp = connp->conn_tcp; 21501 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21502 uchar_t *rptr; 21503 t_scalar_t type; 21504 cred_t *cr; 21505 21506 /* 21507 * Try and ASSERT the minimum possible references on the 21508 * conn early enough. Since we are executing on write side, 21509 * the connection is obviously not detached and that means 21510 * there is a ref each for TCP and IP. Since we are behind 21511 * the squeue, the minimum references needed are 3. If the 21512 * conn is in classifier hash list, there should be an 21513 * extra ref for that (we check both the possibilities). 21514 */ 21515 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21516 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21517 21518 rptr = mp->b_rptr; 21519 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21520 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21521 type = ((union T_primitives *)rptr)->type; 21522 if (type == T_EXDATA_REQ) { 21523 tcp_output_urgent(connp, mp->b_cont, arg2); 21524 freeb(mp); 21525 } else if (type != T_DATA_REQ) { 21526 goto non_urgent_data; 21527 } else { 21528 /* TODO: options, flags, ... from user */ 21529 /* Set length to zero for reclamation below */ 21530 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21531 freeb(mp); 21532 } 21533 return; 21534 } else { 21535 if (tcp->tcp_debug) { 21536 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21537 "tcp_wput_proto, dropping one..."); 21538 } 21539 freemsg(mp); 21540 return; 21541 } 21542 21543 non_urgent_data: 21544 21545 switch ((int)tprim->type) { 21546 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21547 /* 21548 * save the kssl_ent_t from the next block, and convert this 21549 * back to a normal bind_req. 21550 */ 21551 if (mp->b_cont != NULL) { 21552 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21553 21554 if (tcp->tcp_kssl_ent != NULL) { 21555 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21556 KSSL_NO_PROXY); 21557 tcp->tcp_kssl_ent = NULL; 21558 } 21559 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21560 sizeof (kssl_ent_t)); 21561 kssl_hold_ent(tcp->tcp_kssl_ent); 21562 freemsg(mp->b_cont); 21563 mp->b_cont = NULL; 21564 } 21565 tprim->type = T_BIND_REQ; 21566 21567 /* FALLTHROUGH */ 21568 case O_T_BIND_REQ: /* bind request */ 21569 case T_BIND_REQ: /* new semantics bind request */ 21570 tcp_tpi_bind(tcp, mp); 21571 break; 21572 case T_UNBIND_REQ: /* unbind request */ 21573 tcp_tpi_unbind(tcp, mp); 21574 break; 21575 case O_T_CONN_RES: /* old connection response XXX */ 21576 case T_CONN_RES: /* connection response */ 21577 tcp_tli_accept(tcp, mp); 21578 break; 21579 case T_CONN_REQ: /* connection request */ 21580 tcp_tpi_connect(tcp, mp); 21581 break; 21582 case T_DISCON_REQ: /* disconnect request */ 21583 tcp_disconnect(tcp, mp); 21584 break; 21585 case T_CAPABILITY_REQ: 21586 tcp_capability_req(tcp, mp); /* capability request */ 21587 break; 21588 case T_INFO_REQ: /* information request */ 21589 tcp_info_req(tcp, mp); 21590 break; 21591 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21592 case T_OPTMGMT_REQ: 21593 /* 21594 * Note: no support for snmpcom_req() through new 21595 * T_OPTMGMT_REQ. See comments in ip.c 21596 */ 21597 21598 /* 21599 * All Solaris components should pass a db_credp 21600 * for this TPI message, hence we ASSERT. 21601 * But in case there is some other M_PROTO that looks 21602 * like a TPI message sent by some other kernel 21603 * component, we check and return an error. 21604 */ 21605 cr = msg_getcred(mp, NULL); 21606 ASSERT(cr != NULL); 21607 if (cr == NULL) { 21608 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21609 return; 21610 } 21611 /* 21612 * If EINPROGRESS is returned, the request has been queued 21613 * for subsequent processing by ip_restart_optmgmt(), which 21614 * will do the CONN_DEC_REF(). 21615 */ 21616 CONN_INC_REF(connp); 21617 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21618 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21619 B_TRUE) != EINPROGRESS) { 21620 CONN_DEC_REF(connp); 21621 } 21622 } else { 21623 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21624 B_TRUE) != EINPROGRESS) { 21625 CONN_DEC_REF(connp); 21626 } 21627 } 21628 break; 21629 21630 case T_UNITDATA_REQ: /* unitdata request */ 21631 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21632 break; 21633 case T_ORDREL_REQ: /* orderly release req */ 21634 freemsg(mp); 21635 21636 if (tcp->tcp_fused) 21637 tcp_unfuse(tcp); 21638 21639 if (tcp_xmit_end(tcp) != 0) { 21640 /* 21641 * We were crossing FINs and got a reset from 21642 * the other side. Just ignore it. 21643 */ 21644 if (tcp->tcp_debug) { 21645 (void) strlog(TCP_MOD_ID, 0, 1, 21646 SL_ERROR|SL_TRACE, 21647 "tcp_wput_proto, T_ORDREL_REQ out of " 21648 "state %s", 21649 tcp_display(tcp, NULL, 21650 DISP_ADDR_AND_PORT)); 21651 } 21652 } 21653 break; 21654 case T_ADDR_REQ: 21655 tcp_addr_req(tcp, mp); 21656 break; 21657 default: 21658 if (tcp->tcp_debug) { 21659 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21660 "tcp_wput_proto, bogus TPI msg, type %d", 21661 tprim->type); 21662 } 21663 /* 21664 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21665 * to recover. 21666 */ 21667 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21668 break; 21669 } 21670 } 21671 21672 /* 21673 * The TCP write service routine should never be called... 21674 */ 21675 /* ARGSUSED */ 21676 static void 21677 tcp_wsrv(queue_t *q) 21678 { 21679 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21680 21681 TCP_STAT(tcps, tcp_wsrv_called); 21682 } 21683 21684 /* Non overlapping byte exchanger */ 21685 static void 21686 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21687 { 21688 uchar_t uch; 21689 21690 while (len-- > 0) { 21691 uch = a[len]; 21692 a[len] = b[len]; 21693 b[len] = uch; 21694 } 21695 } 21696 21697 /* 21698 * Send out a control packet on the tcp connection specified. This routine 21699 * is typically called where we need a simple ACK or RST generated. 21700 */ 21701 static void 21702 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21703 { 21704 uchar_t *rptr; 21705 tcph_t *tcph; 21706 ipha_t *ipha = NULL; 21707 ip6_t *ip6h = NULL; 21708 uint32_t sum; 21709 int tcp_hdr_len; 21710 int tcp_ip_hdr_len; 21711 mblk_t *mp; 21712 tcp_stack_t *tcps = tcp->tcp_tcps; 21713 21714 /* 21715 * Save sum for use in source route later. 21716 */ 21717 ASSERT(tcp != NULL); 21718 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21719 tcp_hdr_len = tcp->tcp_hdr_len; 21720 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21721 21722 /* If a text string is passed in with the request, pass it to strlog. */ 21723 if (str != NULL && tcp->tcp_debug) { 21724 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21725 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21726 str, seq, ack, ctl); 21727 } 21728 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21729 BPRI_MED); 21730 if (mp == NULL) { 21731 return; 21732 } 21733 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21734 mp->b_rptr = rptr; 21735 mp->b_wptr = &rptr[tcp_hdr_len]; 21736 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21737 21738 if (tcp->tcp_ipversion == IPV4_VERSION) { 21739 ipha = (ipha_t *)rptr; 21740 ipha->ipha_length = htons(tcp_hdr_len); 21741 } else { 21742 ip6h = (ip6_t *)rptr; 21743 ASSERT(tcp != NULL); 21744 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21745 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21746 } 21747 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21748 tcph->th_flags[0] = (uint8_t)ctl; 21749 if (ctl & TH_RST) { 21750 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21751 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21752 /* 21753 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21754 */ 21755 if (tcp->tcp_snd_ts_ok && 21756 tcp->tcp_state > TCPS_SYN_SENT) { 21757 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21758 *(mp->b_wptr) = TCPOPT_EOL; 21759 if (tcp->tcp_ipversion == IPV4_VERSION) { 21760 ipha->ipha_length = htons(tcp_hdr_len - 21761 TCPOPT_REAL_TS_LEN); 21762 } else { 21763 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21764 TCPOPT_REAL_TS_LEN); 21765 } 21766 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21767 sum -= TCPOPT_REAL_TS_LEN; 21768 } 21769 } 21770 if (ctl & TH_ACK) { 21771 if (tcp->tcp_snd_ts_ok) { 21772 U32_TO_BE32(lbolt, 21773 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21774 U32_TO_BE32(tcp->tcp_ts_recent, 21775 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21776 } 21777 21778 /* Update the latest receive window size in TCP header. */ 21779 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21780 tcph->th_win); 21781 tcp->tcp_rack = ack; 21782 tcp->tcp_rack_cnt = 0; 21783 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21784 } 21785 BUMP_LOCAL(tcp->tcp_obsegs); 21786 U32_TO_BE32(seq, tcph->th_seq); 21787 U32_TO_BE32(ack, tcph->th_ack); 21788 /* 21789 * Include the adjustment for a source route if any. 21790 */ 21791 sum = (sum >> 16) + (sum & 0xFFFF); 21792 U16_TO_BE16(sum, tcph->th_sum); 21793 tcp_send_data(tcp, tcp->tcp_wq, mp); 21794 } 21795 21796 /* 21797 * If this routine returns B_TRUE, TCP can generate a RST in response 21798 * to a segment. If it returns B_FALSE, TCP should not respond. 21799 */ 21800 static boolean_t 21801 tcp_send_rst_chk(tcp_stack_t *tcps) 21802 { 21803 clock_t now; 21804 21805 /* 21806 * TCP needs to protect itself from generating too many RSTs. 21807 * This can be a DoS attack by sending us random segments 21808 * soliciting RSTs. 21809 * 21810 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21811 * in each 1 second interval. In this way, TCP still generate 21812 * RSTs in normal cases but when under attack, the impact is 21813 * limited. 21814 */ 21815 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21816 now = lbolt; 21817 /* lbolt can wrap around. */ 21818 if ((tcps->tcps_last_rst_intrvl > now) || 21819 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 21820 1*SECONDS)) { 21821 tcps->tcps_last_rst_intrvl = now; 21822 tcps->tcps_rst_cnt = 1; 21823 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 21824 return (B_FALSE); 21825 } 21826 } 21827 return (B_TRUE); 21828 } 21829 21830 /* 21831 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21832 */ 21833 static void 21834 tcp_ip_ire_mark_advice(tcp_t *tcp) 21835 { 21836 mblk_t *mp; 21837 ipic_t *ipic; 21838 21839 if (tcp->tcp_ipversion == IPV4_VERSION) { 21840 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21841 &ipic); 21842 } else { 21843 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21844 &ipic); 21845 } 21846 if (mp == NULL) 21847 return; 21848 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21849 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21850 } 21851 21852 /* 21853 * Return an IP advice ioctl mblk and set ipic to be the pointer 21854 * to the advice structure. 21855 */ 21856 static mblk_t * 21857 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21858 { 21859 struct iocblk *ioc; 21860 mblk_t *mp, *mp1; 21861 21862 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21863 if (mp == NULL) 21864 return (NULL); 21865 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21866 *ipic = (ipic_t *)mp->b_rptr; 21867 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21868 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21869 21870 bcopy(addr, *ipic + 1, addr_len); 21871 21872 (*ipic)->ipic_addr_length = addr_len; 21873 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21874 21875 mp1 = mkiocb(IP_IOCTL); 21876 if (mp1 == NULL) { 21877 freemsg(mp); 21878 return (NULL); 21879 } 21880 mp1->b_cont = mp; 21881 ioc = (struct iocblk *)mp1->b_rptr; 21882 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21883 21884 return (mp1); 21885 } 21886 21887 /* 21888 * Generate a reset based on an inbound packet, connp is set by caller 21889 * when RST is in response to an unexpected inbound packet for which 21890 * there is active tcp state in the system. 21891 * 21892 * IPSEC NOTE : Try to send the reply with the same protection as it came 21893 * in. We still have the ipsec_mp that the packet was attached to. Thus 21894 * the packet will go out at the same level of protection as it came in by 21895 * converting the IPSEC_IN to IPSEC_OUT. 21896 */ 21897 static void 21898 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21899 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 21900 tcp_stack_t *tcps, conn_t *connp) 21901 { 21902 ipha_t *ipha = NULL; 21903 ip6_t *ip6h = NULL; 21904 ushort_t len; 21905 tcph_t *tcph; 21906 int i; 21907 mblk_t *ipsec_mp; 21908 boolean_t mctl_present; 21909 ipic_t *ipic; 21910 ipaddr_t v4addr; 21911 in6_addr_t v6addr; 21912 int addr_len; 21913 void *addr; 21914 queue_t *q = tcps->tcps_g_q; 21915 tcp_t *tcp; 21916 cred_t *cr; 21917 mblk_t *nmp; 21918 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21919 21920 if (tcps->tcps_g_q == NULL) { 21921 /* 21922 * For non-zero stackids the default queue isn't created 21923 * until the first open, thus there can be a need to send 21924 * a reset before then. But we can't do that, hence we just 21925 * drop the packet. Later during boot, when the default queue 21926 * has been setup, a retransmitted packet from the peer 21927 * will result in a reset. 21928 */ 21929 ASSERT(tcps->tcps_netstack->netstack_stackid != 21930 GLOBAL_NETSTACKID); 21931 freemsg(mp); 21932 return; 21933 } 21934 21935 if (connp != NULL) 21936 tcp = connp->conn_tcp; 21937 else 21938 tcp = Q_TO_TCP(q); 21939 21940 if (!tcp_send_rst_chk(tcps)) { 21941 tcps->tcps_rst_unsent++; 21942 freemsg(mp); 21943 return; 21944 } 21945 21946 if (mp->b_datap->db_type == M_CTL) { 21947 ipsec_mp = mp; 21948 mp = mp->b_cont; 21949 mctl_present = B_TRUE; 21950 } else { 21951 ipsec_mp = mp; 21952 mctl_present = B_FALSE; 21953 } 21954 21955 if (str && q && tcps->tcps_dbg) { 21956 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21957 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21958 "flags 0x%x", 21959 str, seq, ack, ctl); 21960 } 21961 if (mp->b_datap->db_ref != 1) { 21962 mblk_t *mp1 = copyb(mp); 21963 freemsg(mp); 21964 mp = mp1; 21965 if (!mp) { 21966 if (mctl_present) 21967 freeb(ipsec_mp); 21968 return; 21969 } else { 21970 if (mctl_present) { 21971 ipsec_mp->b_cont = mp; 21972 } else { 21973 ipsec_mp = mp; 21974 } 21975 } 21976 } else if (mp->b_cont) { 21977 freemsg(mp->b_cont); 21978 mp->b_cont = NULL; 21979 } 21980 /* 21981 * We skip reversing source route here. 21982 * (for now we replace all IP options with EOL) 21983 */ 21984 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21985 ipha = (ipha_t *)mp->b_rptr; 21986 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21987 mp->b_rptr[i] = IPOPT_EOL; 21988 /* 21989 * Make sure that src address isn't flagrantly invalid. 21990 * Not all broadcast address checking for the src address 21991 * is possible, since we don't know the netmask of the src 21992 * addr. No check for destination address is done, since 21993 * IP will not pass up a packet with a broadcast dest 21994 * address to TCP. Similar checks are done below for IPv6. 21995 */ 21996 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21997 CLASSD(ipha->ipha_src)) { 21998 freemsg(ipsec_mp); 21999 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22000 return; 22001 } 22002 } else { 22003 ip6h = (ip6_t *)mp->b_rptr; 22004 22005 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22006 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22007 freemsg(ipsec_mp); 22008 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22009 return; 22010 } 22011 22012 /* Remove any extension headers assuming partial overlay */ 22013 if (ip_hdr_len > IPV6_HDR_LEN) { 22014 uint8_t *to; 22015 22016 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22017 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22018 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22019 ip_hdr_len = IPV6_HDR_LEN; 22020 ip6h = (ip6_t *)mp->b_rptr; 22021 ip6h->ip6_nxt = IPPROTO_TCP; 22022 } 22023 } 22024 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22025 if (tcph->th_flags[0] & TH_RST) { 22026 freemsg(ipsec_mp); 22027 return; 22028 } 22029 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22030 len = ip_hdr_len + sizeof (tcph_t); 22031 mp->b_wptr = &mp->b_rptr[len]; 22032 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22033 ipha->ipha_length = htons(len); 22034 /* Swap addresses */ 22035 v4addr = ipha->ipha_src; 22036 ipha->ipha_src = ipha->ipha_dst; 22037 ipha->ipha_dst = v4addr; 22038 ipha->ipha_ident = 0; 22039 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22040 addr_len = IP_ADDR_LEN; 22041 addr = &v4addr; 22042 } else { 22043 /* No ip6i_t in this case */ 22044 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22045 /* Swap addresses */ 22046 v6addr = ip6h->ip6_src; 22047 ip6h->ip6_src = ip6h->ip6_dst; 22048 ip6h->ip6_dst = v6addr; 22049 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22050 addr_len = IPV6_ADDR_LEN; 22051 addr = &v6addr; 22052 } 22053 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22054 U32_TO_BE32(ack, tcph->th_ack); 22055 U32_TO_BE32(seq, tcph->th_seq); 22056 U16_TO_BE16(0, tcph->th_win); 22057 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22058 tcph->th_flags[0] = (uint8_t)ctl; 22059 if (ctl & TH_RST) { 22060 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22061 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22062 } 22063 22064 /* IP trusts us to set up labels when required. */ 22065 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22066 crgetlabel(cr) != NULL) { 22067 int err; 22068 22069 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22070 err = tsol_check_label(cr, &mp, 22071 tcp->tcp_connp->conn_mac_exempt, 22072 tcps->tcps_netstack->netstack_ip); 22073 else 22074 err = tsol_check_label_v6(cr, &mp, 22075 tcp->tcp_connp->conn_mac_exempt, 22076 tcps->tcps_netstack->netstack_ip); 22077 if (mctl_present) 22078 ipsec_mp->b_cont = mp; 22079 else 22080 ipsec_mp = mp; 22081 if (err != 0) { 22082 freemsg(ipsec_mp); 22083 return; 22084 } 22085 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22086 ipha = (ipha_t *)mp->b_rptr; 22087 } else { 22088 ip6h = (ip6_t *)mp->b_rptr; 22089 } 22090 } 22091 22092 if (mctl_present) { 22093 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22094 22095 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22096 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22097 return; 22098 } 22099 } 22100 if (zoneid == ALL_ZONES) 22101 zoneid = GLOBAL_ZONEID; 22102 22103 /* Add the zoneid so ip_output routes it properly */ 22104 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22105 freemsg(ipsec_mp); 22106 return; 22107 } 22108 ipsec_mp = nmp; 22109 22110 /* 22111 * NOTE: one might consider tracing a TCP packet here, but 22112 * this function has no active TCP state and no tcp structure 22113 * that has a trace buffer. If we traced here, we would have 22114 * to keep a local trace buffer in tcp_record_trace(). 22115 * 22116 * TSol note: The mblk that contains the incoming packet was 22117 * reused by tcp_xmit_listener_reset, so it already contains 22118 * the right credentials and we don't need to call mblk_setcred. 22119 * Also the conn's cred is not right since it is associated 22120 * with tcps_g_q. 22121 */ 22122 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22123 22124 /* 22125 * Tell IP to mark the IRE used for this destination temporary. 22126 * This way, we can limit our exposure to DoS attack because IP 22127 * creates an IRE for each destination. If there are too many, 22128 * the time to do any routing lookup will be extremely long. And 22129 * the lookup can be in interrupt context. 22130 * 22131 * Note that in normal circumstances, this marking should not 22132 * affect anything. It would be nice if only 1 message is 22133 * needed to inform IP that the IRE created for this RST should 22134 * not be added to the cache table. But there is currently 22135 * not such communication mechanism between TCP and IP. So 22136 * the best we can do now is to send the advice ioctl to IP 22137 * to mark the IRE temporary. 22138 */ 22139 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22140 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22141 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22142 } 22143 } 22144 22145 /* 22146 * Initiate closedown sequence on an active connection. (May be called as 22147 * writer.) Return value zero for OK return, non-zero for error return. 22148 */ 22149 static int 22150 tcp_xmit_end(tcp_t *tcp) 22151 { 22152 ipic_t *ipic; 22153 mblk_t *mp; 22154 tcp_stack_t *tcps = tcp->tcp_tcps; 22155 22156 if (tcp->tcp_state < TCPS_SYN_RCVD || 22157 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22158 /* 22159 * Invalid state, only states TCPS_SYN_RCVD, 22160 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22161 */ 22162 return (-1); 22163 } 22164 22165 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22166 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22167 /* 22168 * If there is nothing more unsent, send the FIN now. 22169 * Otherwise, it will go out with the last segment. 22170 */ 22171 if (tcp->tcp_unsent == 0) { 22172 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22173 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22174 22175 if (mp) { 22176 tcp_send_data(tcp, tcp->tcp_wq, mp); 22177 } else { 22178 /* 22179 * Couldn't allocate msg. Pretend we got it out. 22180 * Wait for rexmit timeout. 22181 */ 22182 tcp->tcp_snxt = tcp->tcp_fss + 1; 22183 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22184 } 22185 22186 /* 22187 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22188 * changed. 22189 */ 22190 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22191 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22192 } 22193 } else { 22194 /* 22195 * If tcp->tcp_cork is set, then the data will not get sent, 22196 * so we have to check that and unset it first. 22197 */ 22198 if (tcp->tcp_cork) 22199 tcp->tcp_cork = B_FALSE; 22200 tcp_wput_data(tcp, NULL, B_FALSE); 22201 } 22202 22203 /* 22204 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22205 * is 0, don't update the cache. 22206 */ 22207 if (tcps->tcps_rtt_updates == 0 || 22208 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22209 return (0); 22210 22211 /* 22212 * NOTE: should not update if source routes i.e. if tcp_remote if 22213 * different from the destination. 22214 */ 22215 if (tcp->tcp_ipversion == IPV4_VERSION) { 22216 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22217 return (0); 22218 } 22219 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22220 &ipic); 22221 } else { 22222 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22223 &tcp->tcp_ip6h->ip6_dst))) { 22224 return (0); 22225 } 22226 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22227 &ipic); 22228 } 22229 22230 /* Record route attributes in the IRE for use by future connections. */ 22231 if (mp == NULL) 22232 return (0); 22233 22234 /* 22235 * We do not have a good algorithm to update ssthresh at this time. 22236 * So don't do any update. 22237 */ 22238 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22239 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22240 22241 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22242 22243 return (0); 22244 } 22245 22246 /* ARGSUSED */ 22247 void 22248 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22249 { 22250 conn_t *connp = (conn_t *)arg; 22251 mblk_t *mp1; 22252 tcp_t *tcp = connp->conn_tcp; 22253 tcp_xmit_reset_event_t *eventp; 22254 22255 ASSERT(mp->b_datap->db_type == M_PROTO && 22256 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22257 22258 if (tcp->tcp_state != TCPS_LISTEN) { 22259 freemsg(mp); 22260 return; 22261 } 22262 22263 mp1 = mp->b_cont; 22264 mp->b_cont = NULL; 22265 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22266 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22267 connp->conn_netstack); 22268 22269 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22270 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22271 freemsg(mp); 22272 } 22273 22274 /* 22275 * Generate a "no listener here" RST in response to an "unknown" segment. 22276 * connp is set by caller when RST is in response to an unexpected 22277 * inbound packet for which there is active tcp state in the system. 22278 * Note that we are reusing the incoming mp to construct the outgoing RST. 22279 */ 22280 void 22281 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22282 tcp_stack_t *tcps, conn_t *connp) 22283 { 22284 uchar_t *rptr; 22285 uint32_t seg_len; 22286 tcph_t *tcph; 22287 uint32_t seg_seq; 22288 uint32_t seg_ack; 22289 uint_t flags; 22290 mblk_t *ipsec_mp; 22291 ipha_t *ipha; 22292 ip6_t *ip6h; 22293 boolean_t mctl_present = B_FALSE; 22294 boolean_t check = B_TRUE; 22295 boolean_t policy_present; 22296 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22297 22298 TCP_STAT(tcps, tcp_no_listener); 22299 22300 ipsec_mp = mp; 22301 22302 if (mp->b_datap->db_type == M_CTL) { 22303 ipsec_in_t *ii; 22304 22305 mctl_present = B_TRUE; 22306 mp = mp->b_cont; 22307 22308 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22309 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22310 if (ii->ipsec_in_dont_check) { 22311 check = B_FALSE; 22312 if (!ii->ipsec_in_secure) { 22313 freeb(ipsec_mp); 22314 mctl_present = B_FALSE; 22315 ipsec_mp = mp; 22316 } 22317 } 22318 } 22319 22320 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22321 policy_present = ipss->ipsec_inbound_v4_policy_present; 22322 ipha = (ipha_t *)mp->b_rptr; 22323 ip6h = NULL; 22324 } else { 22325 policy_present = ipss->ipsec_inbound_v6_policy_present; 22326 ipha = NULL; 22327 ip6h = (ip6_t *)mp->b_rptr; 22328 } 22329 22330 if (check && policy_present) { 22331 /* 22332 * The conn_t parameter is NULL because we already know 22333 * nobody's home. 22334 */ 22335 ipsec_mp = ipsec_check_global_policy( 22336 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22337 tcps->tcps_netstack); 22338 if (ipsec_mp == NULL) 22339 return; 22340 } 22341 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22342 DTRACE_PROBE2( 22343 tx__ip__log__error__nolistener__tcp, 22344 char *, "Could not reply with RST to mp(1)", 22345 mblk_t *, mp); 22346 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22347 freemsg(ipsec_mp); 22348 return; 22349 } 22350 22351 rptr = mp->b_rptr; 22352 22353 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22354 seg_seq = BE32_TO_U32(tcph->th_seq); 22355 seg_ack = BE32_TO_U32(tcph->th_ack); 22356 flags = tcph->th_flags[0]; 22357 22358 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22359 if (flags & TH_RST) { 22360 freemsg(ipsec_mp); 22361 } else if (flags & TH_ACK) { 22362 tcp_xmit_early_reset("no tcp, reset", 22363 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22364 connp); 22365 } else { 22366 if (flags & TH_SYN) { 22367 seg_len++; 22368 } else { 22369 /* 22370 * Here we violate the RFC. Note that a normal 22371 * TCP will never send a segment without the ACK 22372 * flag, except for RST or SYN segment. This 22373 * segment is neither. Just drop it on the 22374 * floor. 22375 */ 22376 freemsg(ipsec_mp); 22377 tcps->tcps_rst_unsent++; 22378 return; 22379 } 22380 22381 tcp_xmit_early_reset("no tcp, reset/ack", 22382 ipsec_mp, 0, seg_seq + seg_len, 22383 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22384 } 22385 } 22386 22387 /* 22388 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22389 * ip and tcp header ready to pass down to IP. If the mp passed in is 22390 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22391 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22392 * otherwise it will dup partial mblks.) 22393 * Otherwise, an appropriate ACK packet will be generated. This 22394 * routine is not usually called to send new data for the first time. It 22395 * is mostly called out of the timer for retransmits, and to generate ACKs. 22396 * 22397 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22398 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22399 * of the original mblk chain will be returned in *offset and *end_mp. 22400 */ 22401 mblk_t * 22402 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22403 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22404 boolean_t rexmit) 22405 { 22406 int data_length; 22407 int32_t off = 0; 22408 uint_t flags; 22409 mblk_t *mp1; 22410 mblk_t *mp2; 22411 uchar_t *rptr; 22412 tcph_t *tcph; 22413 int32_t num_sack_blk = 0; 22414 int32_t sack_opt_len = 0; 22415 tcp_stack_t *tcps = tcp->tcp_tcps; 22416 22417 /* Allocate for our maximum TCP header + link-level */ 22418 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22419 tcps->tcps_wroff_xtra, BPRI_MED); 22420 if (!mp1) 22421 return (NULL); 22422 data_length = 0; 22423 22424 /* 22425 * Note that tcp_mss has been adjusted to take into account the 22426 * timestamp option if applicable. Because SACK options do not 22427 * appear in every TCP segments and they are of variable lengths, 22428 * they cannot be included in tcp_mss. Thus we need to calculate 22429 * the actual segment length when we need to send a segment which 22430 * includes SACK options. 22431 */ 22432 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22433 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22434 tcp->tcp_num_sack_blk); 22435 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22436 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22437 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22438 max_to_send -= sack_opt_len; 22439 } 22440 22441 if (offset != NULL) { 22442 off = *offset; 22443 /* We use offset as an indicator that end_mp is not NULL. */ 22444 *end_mp = NULL; 22445 } 22446 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22447 /* This could be faster with cooperation from downstream */ 22448 if (mp2 != mp1 && !sendall && 22449 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22450 max_to_send) 22451 /* 22452 * Don't send the next mblk since the whole mblk 22453 * does not fit. 22454 */ 22455 break; 22456 mp2->b_cont = dupb(mp); 22457 mp2 = mp2->b_cont; 22458 if (!mp2) { 22459 freemsg(mp1); 22460 return (NULL); 22461 } 22462 mp2->b_rptr += off; 22463 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22464 (uintptr_t)INT_MAX); 22465 22466 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22467 if (data_length > max_to_send) { 22468 mp2->b_wptr -= data_length - max_to_send; 22469 data_length = max_to_send; 22470 off = mp2->b_wptr - mp->b_rptr; 22471 break; 22472 } else { 22473 off = 0; 22474 } 22475 } 22476 if (offset != NULL) { 22477 *offset = off; 22478 *end_mp = mp; 22479 } 22480 if (seg_len != NULL) { 22481 *seg_len = data_length; 22482 } 22483 22484 /* Update the latest receive window size in TCP header. */ 22485 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22486 tcp->tcp_tcph->th_win); 22487 22488 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22489 mp1->b_rptr = rptr; 22490 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22491 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22492 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22493 U32_TO_ABE32(seq, tcph->th_seq); 22494 22495 /* 22496 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22497 * that this function was called from tcp_wput_data. Thus, when called 22498 * to retransmit data the setting of the PUSH bit may appear some 22499 * what random in that it might get set when it should not. This 22500 * should not pose any performance issues. 22501 */ 22502 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22503 tcp->tcp_unsent == data_length)) { 22504 flags = TH_ACK | TH_PUSH; 22505 } else { 22506 flags = TH_ACK; 22507 } 22508 22509 if (tcp->tcp_ecn_ok) { 22510 if (tcp->tcp_ecn_echo_on) 22511 flags |= TH_ECE; 22512 22513 /* 22514 * Only set ECT bit and ECN_CWR if a segment contains new data. 22515 * There is no TCP flow control for non-data segments, and 22516 * only data segment is transmitted reliably. 22517 */ 22518 if (data_length > 0 && !rexmit) { 22519 SET_ECT(tcp, rptr); 22520 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22521 flags |= TH_CWR; 22522 tcp->tcp_ecn_cwr_sent = B_TRUE; 22523 } 22524 } 22525 } 22526 22527 if (tcp->tcp_valid_bits) { 22528 uint32_t u1; 22529 22530 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22531 seq == tcp->tcp_iss) { 22532 uchar_t *wptr; 22533 22534 /* 22535 * If TCP_ISS_VALID and the seq number is tcp_iss, 22536 * TCP can only be in SYN-SENT, SYN-RCVD or 22537 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22538 * our SYN is not ack'ed but the app closes this 22539 * TCP connection. 22540 */ 22541 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22542 tcp->tcp_state == TCPS_SYN_RCVD || 22543 tcp->tcp_state == TCPS_FIN_WAIT_1); 22544 22545 /* 22546 * Tack on the MSS option. It is always needed 22547 * for both active and passive open. 22548 * 22549 * MSS option value should be interface MTU - MIN 22550 * TCP/IP header according to RFC 793 as it means 22551 * the maximum segment size TCP can receive. But 22552 * to get around some broken middle boxes/end hosts 22553 * out there, we allow the option value to be the 22554 * same as the MSS option size on the peer side. 22555 * In this way, the other side will not send 22556 * anything larger than they can receive. 22557 * 22558 * Note that for SYN_SENT state, the ndd param 22559 * tcp_use_smss_as_mss_opt has no effect as we 22560 * don't know the peer's MSS option value. So 22561 * the only case we need to take care of is in 22562 * SYN_RCVD state, which is done later. 22563 */ 22564 wptr = mp1->b_wptr; 22565 wptr[0] = TCPOPT_MAXSEG; 22566 wptr[1] = TCPOPT_MAXSEG_LEN; 22567 wptr += 2; 22568 u1 = tcp->tcp_if_mtu - 22569 (tcp->tcp_ipversion == IPV4_VERSION ? 22570 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22571 TCP_MIN_HEADER_LENGTH; 22572 U16_TO_BE16(u1, wptr); 22573 mp1->b_wptr = wptr + 2; 22574 /* Update the offset to cover the additional word */ 22575 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22576 22577 /* 22578 * Note that the following way of filling in 22579 * TCP options are not optimal. Some NOPs can 22580 * be saved. But there is no need at this time 22581 * to optimize it. When it is needed, we will 22582 * do it. 22583 */ 22584 switch (tcp->tcp_state) { 22585 case TCPS_SYN_SENT: 22586 flags = TH_SYN; 22587 22588 if (tcp->tcp_snd_ts_ok) { 22589 uint32_t llbolt = (uint32_t)lbolt; 22590 22591 wptr = mp1->b_wptr; 22592 wptr[0] = TCPOPT_NOP; 22593 wptr[1] = TCPOPT_NOP; 22594 wptr[2] = TCPOPT_TSTAMP; 22595 wptr[3] = TCPOPT_TSTAMP_LEN; 22596 wptr += 4; 22597 U32_TO_BE32(llbolt, wptr); 22598 wptr += 4; 22599 ASSERT(tcp->tcp_ts_recent == 0); 22600 U32_TO_BE32(0L, wptr); 22601 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22602 tcph->th_offset_and_rsrvd[0] += 22603 (3 << 4); 22604 } 22605 22606 /* 22607 * Set up all the bits to tell other side 22608 * we are ECN capable. 22609 */ 22610 if (tcp->tcp_ecn_ok) { 22611 flags |= (TH_ECE | TH_CWR); 22612 } 22613 break; 22614 case TCPS_SYN_RCVD: 22615 flags |= TH_SYN; 22616 22617 /* 22618 * Reset the MSS option value to be SMSS 22619 * We should probably add back the bytes 22620 * for timestamp option and IPsec. We 22621 * don't do that as this is a workaround 22622 * for broken middle boxes/end hosts, it 22623 * is better for us to be more cautious. 22624 * They may not take these things into 22625 * account in their SMSS calculation. Thus 22626 * the peer's calculated SMSS may be smaller 22627 * than what it can be. This should be OK. 22628 */ 22629 if (tcps->tcps_use_smss_as_mss_opt) { 22630 u1 = tcp->tcp_mss; 22631 U16_TO_BE16(u1, wptr); 22632 } 22633 22634 /* 22635 * If the other side is ECN capable, reply 22636 * that we are also ECN capable. 22637 */ 22638 if (tcp->tcp_ecn_ok) 22639 flags |= TH_ECE; 22640 break; 22641 default: 22642 /* 22643 * The above ASSERT() makes sure that this 22644 * must be FIN-WAIT-1 state. Our SYN has 22645 * not been ack'ed so retransmit it. 22646 */ 22647 flags |= TH_SYN; 22648 break; 22649 } 22650 22651 if (tcp->tcp_snd_ws_ok) { 22652 wptr = mp1->b_wptr; 22653 wptr[0] = TCPOPT_NOP; 22654 wptr[1] = TCPOPT_WSCALE; 22655 wptr[2] = TCPOPT_WS_LEN; 22656 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22657 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22658 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22659 } 22660 22661 if (tcp->tcp_snd_sack_ok) { 22662 wptr = mp1->b_wptr; 22663 wptr[0] = TCPOPT_NOP; 22664 wptr[1] = TCPOPT_NOP; 22665 wptr[2] = TCPOPT_SACK_PERMITTED; 22666 wptr[3] = TCPOPT_SACK_OK_LEN; 22667 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22668 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22669 } 22670 22671 /* allocb() of adequate mblk assures space */ 22672 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22673 (uintptr_t)INT_MAX); 22674 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22675 /* 22676 * Get IP set to checksum on our behalf 22677 * Include the adjustment for a source route if any. 22678 */ 22679 u1 += tcp->tcp_sum; 22680 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22681 U16_TO_BE16(u1, tcph->th_sum); 22682 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22683 } 22684 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22685 (seq + data_length) == tcp->tcp_fss) { 22686 if (!tcp->tcp_fin_acked) { 22687 flags |= TH_FIN; 22688 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22689 } 22690 if (!tcp->tcp_fin_sent) { 22691 tcp->tcp_fin_sent = B_TRUE; 22692 switch (tcp->tcp_state) { 22693 case TCPS_SYN_RCVD: 22694 case TCPS_ESTABLISHED: 22695 tcp->tcp_state = TCPS_FIN_WAIT_1; 22696 break; 22697 case TCPS_CLOSE_WAIT: 22698 tcp->tcp_state = TCPS_LAST_ACK; 22699 break; 22700 } 22701 if (tcp->tcp_suna == tcp->tcp_snxt) 22702 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22703 tcp->tcp_snxt = tcp->tcp_fss + 1; 22704 } 22705 } 22706 /* 22707 * Note the trick here. u1 is unsigned. When tcp_urg 22708 * is smaller than seq, u1 will become a very huge value. 22709 * So the comparison will fail. Also note that tcp_urp 22710 * should be positive, see RFC 793 page 17. 22711 */ 22712 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22713 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22714 u1 < (uint32_t)(64 * 1024)) { 22715 flags |= TH_URG; 22716 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22717 U32_TO_ABE16(u1, tcph->th_urp); 22718 } 22719 } 22720 tcph->th_flags[0] = (uchar_t)flags; 22721 tcp->tcp_rack = tcp->tcp_rnxt; 22722 tcp->tcp_rack_cnt = 0; 22723 22724 if (tcp->tcp_snd_ts_ok) { 22725 if (tcp->tcp_state != TCPS_SYN_SENT) { 22726 uint32_t llbolt = (uint32_t)lbolt; 22727 22728 U32_TO_BE32(llbolt, 22729 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22730 U32_TO_BE32(tcp->tcp_ts_recent, 22731 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22732 } 22733 } 22734 22735 if (num_sack_blk > 0) { 22736 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22737 sack_blk_t *tmp; 22738 int32_t i; 22739 22740 wptr[0] = TCPOPT_NOP; 22741 wptr[1] = TCPOPT_NOP; 22742 wptr[2] = TCPOPT_SACK; 22743 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22744 sizeof (sack_blk_t); 22745 wptr += TCPOPT_REAL_SACK_LEN; 22746 22747 tmp = tcp->tcp_sack_list; 22748 for (i = 0; i < num_sack_blk; i++) { 22749 U32_TO_BE32(tmp[i].begin, wptr); 22750 wptr += sizeof (tcp_seq); 22751 U32_TO_BE32(tmp[i].end, wptr); 22752 wptr += sizeof (tcp_seq); 22753 } 22754 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22755 } 22756 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22757 data_length += (int)(mp1->b_wptr - rptr); 22758 if (tcp->tcp_ipversion == IPV4_VERSION) { 22759 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22760 } else { 22761 ip6_t *ip6 = (ip6_t *)(rptr + 22762 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22763 sizeof (ip6i_t) : 0)); 22764 22765 ip6->ip6_plen = htons(data_length - 22766 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22767 } 22768 22769 /* 22770 * Prime pump for IP 22771 * Include the adjustment for a source route if any. 22772 */ 22773 data_length -= tcp->tcp_ip_hdr_len; 22774 data_length += tcp->tcp_sum; 22775 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22776 U16_TO_ABE16(data_length, tcph->th_sum); 22777 if (tcp->tcp_ip_forward_progress) { 22778 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22779 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22780 tcp->tcp_ip_forward_progress = B_FALSE; 22781 } 22782 return (mp1); 22783 } 22784 22785 /* This function handles the push timeout. */ 22786 void 22787 tcp_push_timer(void *arg) 22788 { 22789 conn_t *connp = (conn_t *)arg; 22790 tcp_t *tcp = connp->conn_tcp; 22791 22792 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22793 22794 ASSERT(tcp->tcp_listener == NULL); 22795 22796 ASSERT(!IPCL_IS_NONSTR(connp)); 22797 22798 /* 22799 * We need to plug synchronous streams during our drain to prevent 22800 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22801 */ 22802 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22803 tcp->tcp_push_tid = 0; 22804 22805 if (tcp->tcp_rcv_list != NULL && 22806 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22807 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22808 22809 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22810 } 22811 22812 /* 22813 * This function handles delayed ACK timeout. 22814 */ 22815 static void 22816 tcp_ack_timer(void *arg) 22817 { 22818 conn_t *connp = (conn_t *)arg; 22819 tcp_t *tcp = connp->conn_tcp; 22820 mblk_t *mp; 22821 tcp_stack_t *tcps = tcp->tcp_tcps; 22822 22823 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 22824 22825 tcp->tcp_ack_tid = 0; 22826 22827 if (tcp->tcp_fused) 22828 return; 22829 22830 /* 22831 * Do not send ACK if there is no outstanding unack'ed data. 22832 */ 22833 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22834 return; 22835 } 22836 22837 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22838 /* 22839 * Make sure we don't allow deferred ACKs to result in 22840 * timer-based ACKing. If we have held off an ACK 22841 * when there was more than an mss here, and the timer 22842 * goes off, we have to worry about the possibility 22843 * that the sender isn't doing slow-start, or is out 22844 * of step with us for some other reason. We fall 22845 * permanently back in the direction of 22846 * ACK-every-other-packet as suggested in RFC 1122. 22847 */ 22848 if (tcp->tcp_rack_abs_max > 2) 22849 tcp->tcp_rack_abs_max--; 22850 tcp->tcp_rack_cur_max = 2; 22851 } 22852 mp = tcp_ack_mp(tcp); 22853 22854 if (mp != NULL) { 22855 BUMP_LOCAL(tcp->tcp_obsegs); 22856 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22857 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 22858 tcp_send_data(tcp, tcp->tcp_wq, mp); 22859 } 22860 } 22861 22862 22863 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22864 static mblk_t * 22865 tcp_ack_mp(tcp_t *tcp) 22866 { 22867 uint32_t seq_no; 22868 tcp_stack_t *tcps = tcp->tcp_tcps; 22869 22870 /* 22871 * There are a few cases to be considered while setting the sequence no. 22872 * Essentially, we can come here while processing an unacceptable pkt 22873 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22874 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22875 * If we are here for a zero window probe, stick with suna. In all 22876 * other cases, we check if suna + swnd encompasses snxt and set 22877 * the sequence number to snxt, if so. If snxt falls outside the 22878 * window (the receiver probably shrunk its window), we will go with 22879 * suna + swnd, otherwise the sequence no will be unacceptable to the 22880 * receiver. 22881 */ 22882 if (tcp->tcp_zero_win_probe) { 22883 seq_no = tcp->tcp_suna; 22884 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22885 ASSERT(tcp->tcp_swnd == 0); 22886 seq_no = tcp->tcp_snxt; 22887 } else { 22888 seq_no = SEQ_GT(tcp->tcp_snxt, 22889 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22890 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22891 } 22892 22893 if (tcp->tcp_valid_bits) { 22894 /* 22895 * For the complex case where we have to send some 22896 * controls (FIN or SYN), let tcp_xmit_mp do it. 22897 */ 22898 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22899 NULL, B_FALSE)); 22900 } else { 22901 /* Generate a simple ACK */ 22902 int data_length; 22903 uchar_t *rptr; 22904 tcph_t *tcph; 22905 mblk_t *mp1; 22906 int32_t tcp_hdr_len; 22907 int32_t tcp_tcp_hdr_len; 22908 int32_t num_sack_blk = 0; 22909 int32_t sack_opt_len; 22910 22911 /* 22912 * Allocate space for TCP + IP headers 22913 * and link-level header 22914 */ 22915 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22916 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22917 tcp->tcp_num_sack_blk); 22918 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22919 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22920 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22921 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22922 } else { 22923 tcp_hdr_len = tcp->tcp_hdr_len; 22924 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22925 } 22926 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 22927 if (!mp1) 22928 return (NULL); 22929 22930 /* Update the latest receive window size in TCP header. */ 22931 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22932 tcp->tcp_tcph->th_win); 22933 /* copy in prototype TCP + IP header */ 22934 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22935 mp1->b_rptr = rptr; 22936 mp1->b_wptr = rptr + tcp_hdr_len; 22937 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22938 22939 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22940 22941 /* Set the TCP sequence number. */ 22942 U32_TO_ABE32(seq_no, tcph->th_seq); 22943 22944 /* Set up the TCP flag field. */ 22945 tcph->th_flags[0] = (uchar_t)TH_ACK; 22946 if (tcp->tcp_ecn_echo_on) 22947 tcph->th_flags[0] |= TH_ECE; 22948 22949 tcp->tcp_rack = tcp->tcp_rnxt; 22950 tcp->tcp_rack_cnt = 0; 22951 22952 /* fill in timestamp option if in use */ 22953 if (tcp->tcp_snd_ts_ok) { 22954 uint32_t llbolt = (uint32_t)lbolt; 22955 22956 U32_TO_BE32(llbolt, 22957 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22958 U32_TO_BE32(tcp->tcp_ts_recent, 22959 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22960 } 22961 22962 /* Fill in SACK options */ 22963 if (num_sack_blk > 0) { 22964 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22965 sack_blk_t *tmp; 22966 int32_t i; 22967 22968 wptr[0] = TCPOPT_NOP; 22969 wptr[1] = TCPOPT_NOP; 22970 wptr[2] = TCPOPT_SACK; 22971 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22972 sizeof (sack_blk_t); 22973 wptr += TCPOPT_REAL_SACK_LEN; 22974 22975 tmp = tcp->tcp_sack_list; 22976 for (i = 0; i < num_sack_blk; i++) { 22977 U32_TO_BE32(tmp[i].begin, wptr); 22978 wptr += sizeof (tcp_seq); 22979 U32_TO_BE32(tmp[i].end, wptr); 22980 wptr += sizeof (tcp_seq); 22981 } 22982 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22983 << 4); 22984 } 22985 22986 if (tcp->tcp_ipversion == IPV4_VERSION) { 22987 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22988 } else { 22989 /* Check for ip6i_t header in sticky hdrs */ 22990 ip6_t *ip6 = (ip6_t *)(rptr + 22991 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22992 sizeof (ip6i_t) : 0)); 22993 22994 ip6->ip6_plen = htons(tcp_hdr_len - 22995 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22996 } 22997 22998 /* 22999 * Prime pump for checksum calculation in IP. Include the 23000 * adjustment for a source route if any. 23001 */ 23002 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23003 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23004 U16_TO_ABE16(data_length, tcph->th_sum); 23005 23006 if (tcp->tcp_ip_forward_progress) { 23007 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23008 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23009 tcp->tcp_ip_forward_progress = B_FALSE; 23010 } 23011 return (mp1); 23012 } 23013 } 23014 23015 /* 23016 * Hash list insertion routine for tcp_t structures. Each hash bucket 23017 * contains a list of tcp_t entries, and each entry is bound to a unique 23018 * port. If there are multiple tcp_t's that are bound to the same port, then 23019 * one of them will be linked into the hash bucket list, and the rest will 23020 * hang off of that one entry. For each port, entries bound to a specific IP 23021 * address will be inserted before those those bound to INADDR_ANY. 23022 */ 23023 static void 23024 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23025 { 23026 tcp_t **tcpp; 23027 tcp_t *tcpnext; 23028 tcp_t *tcphash; 23029 23030 if (tcp->tcp_ptpbhn != NULL) { 23031 ASSERT(!caller_holds_lock); 23032 tcp_bind_hash_remove(tcp); 23033 } 23034 tcpp = &tbf->tf_tcp; 23035 if (!caller_holds_lock) { 23036 mutex_enter(&tbf->tf_lock); 23037 } else { 23038 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23039 } 23040 tcphash = tcpp[0]; 23041 tcpnext = NULL; 23042 if (tcphash != NULL) { 23043 /* Look for an entry using the same port */ 23044 while ((tcphash = tcpp[0]) != NULL && 23045 tcp->tcp_lport != tcphash->tcp_lport) 23046 tcpp = &(tcphash->tcp_bind_hash); 23047 23048 /* The port was not found, just add to the end */ 23049 if (tcphash == NULL) 23050 goto insert; 23051 23052 /* 23053 * OK, there already exists an entry bound to the 23054 * same port. 23055 * 23056 * If the new tcp bound to the INADDR_ANY address 23057 * and the first one in the list is not bound to 23058 * INADDR_ANY we skip all entries until we find the 23059 * first one bound to INADDR_ANY. 23060 * This makes sure that applications binding to a 23061 * specific address get preference over those binding to 23062 * INADDR_ANY. 23063 */ 23064 tcpnext = tcphash; 23065 tcphash = NULL; 23066 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23067 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23068 while ((tcpnext = tcpp[0]) != NULL && 23069 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23070 tcpp = &(tcpnext->tcp_bind_hash_port); 23071 23072 if (tcpnext) { 23073 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23074 tcphash = tcpnext->tcp_bind_hash; 23075 if (tcphash != NULL) { 23076 tcphash->tcp_ptpbhn = 23077 &(tcp->tcp_bind_hash); 23078 tcpnext->tcp_bind_hash = NULL; 23079 } 23080 } 23081 } else { 23082 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23083 tcphash = tcpnext->tcp_bind_hash; 23084 if (tcphash != NULL) { 23085 tcphash->tcp_ptpbhn = 23086 &(tcp->tcp_bind_hash); 23087 tcpnext->tcp_bind_hash = NULL; 23088 } 23089 } 23090 } 23091 insert: 23092 tcp->tcp_bind_hash_port = tcpnext; 23093 tcp->tcp_bind_hash = tcphash; 23094 tcp->tcp_ptpbhn = tcpp; 23095 tcpp[0] = tcp; 23096 if (!caller_holds_lock) 23097 mutex_exit(&tbf->tf_lock); 23098 } 23099 23100 /* 23101 * Hash list removal routine for tcp_t structures. 23102 */ 23103 static void 23104 tcp_bind_hash_remove(tcp_t *tcp) 23105 { 23106 tcp_t *tcpnext; 23107 kmutex_t *lockp; 23108 tcp_stack_t *tcps = tcp->tcp_tcps; 23109 23110 if (tcp->tcp_ptpbhn == NULL) 23111 return; 23112 23113 /* 23114 * Extract the lock pointer in case there are concurrent 23115 * hash_remove's for this instance. 23116 */ 23117 ASSERT(tcp->tcp_lport != 0); 23118 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23119 23120 ASSERT(lockp != NULL); 23121 mutex_enter(lockp); 23122 if (tcp->tcp_ptpbhn) { 23123 tcpnext = tcp->tcp_bind_hash_port; 23124 if (tcpnext != NULL) { 23125 tcp->tcp_bind_hash_port = NULL; 23126 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23127 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23128 if (tcpnext->tcp_bind_hash != NULL) { 23129 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23130 &(tcpnext->tcp_bind_hash); 23131 tcp->tcp_bind_hash = NULL; 23132 } 23133 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23134 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23135 tcp->tcp_bind_hash = NULL; 23136 } 23137 *tcp->tcp_ptpbhn = tcpnext; 23138 tcp->tcp_ptpbhn = NULL; 23139 } 23140 mutex_exit(lockp); 23141 } 23142 23143 23144 /* 23145 * Hash list lookup routine for tcp_t structures. 23146 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23147 */ 23148 static tcp_t * 23149 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23150 { 23151 tf_t *tf; 23152 tcp_t *tcp; 23153 23154 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23155 mutex_enter(&tf->tf_lock); 23156 for (tcp = tf->tf_tcp; tcp != NULL; 23157 tcp = tcp->tcp_acceptor_hash) { 23158 if (tcp->tcp_acceptor_id == id) { 23159 CONN_INC_REF(tcp->tcp_connp); 23160 mutex_exit(&tf->tf_lock); 23161 return (tcp); 23162 } 23163 } 23164 mutex_exit(&tf->tf_lock); 23165 return (NULL); 23166 } 23167 23168 23169 /* 23170 * Hash list insertion routine for tcp_t structures. 23171 */ 23172 void 23173 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23174 { 23175 tf_t *tf; 23176 tcp_t **tcpp; 23177 tcp_t *tcpnext; 23178 tcp_stack_t *tcps = tcp->tcp_tcps; 23179 23180 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23181 23182 if (tcp->tcp_ptpahn != NULL) 23183 tcp_acceptor_hash_remove(tcp); 23184 tcpp = &tf->tf_tcp; 23185 mutex_enter(&tf->tf_lock); 23186 tcpnext = tcpp[0]; 23187 if (tcpnext) 23188 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23189 tcp->tcp_acceptor_hash = tcpnext; 23190 tcp->tcp_ptpahn = tcpp; 23191 tcpp[0] = tcp; 23192 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23193 mutex_exit(&tf->tf_lock); 23194 } 23195 23196 /* 23197 * Hash list removal routine for tcp_t structures. 23198 */ 23199 static void 23200 tcp_acceptor_hash_remove(tcp_t *tcp) 23201 { 23202 tcp_t *tcpnext; 23203 kmutex_t *lockp; 23204 23205 /* 23206 * Extract the lock pointer in case there are concurrent 23207 * hash_remove's for this instance. 23208 */ 23209 lockp = tcp->tcp_acceptor_lockp; 23210 23211 if (tcp->tcp_ptpahn == NULL) 23212 return; 23213 23214 ASSERT(lockp != NULL); 23215 mutex_enter(lockp); 23216 if (tcp->tcp_ptpahn) { 23217 tcpnext = tcp->tcp_acceptor_hash; 23218 if (tcpnext) { 23219 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23220 tcp->tcp_acceptor_hash = NULL; 23221 } 23222 *tcp->tcp_ptpahn = tcpnext; 23223 tcp->tcp_ptpahn = NULL; 23224 } 23225 mutex_exit(lockp); 23226 tcp->tcp_acceptor_lockp = NULL; 23227 } 23228 23229 /* 23230 * Type three generator adapted from the random() function in 4.4 BSD: 23231 */ 23232 23233 /* 23234 * Copyright (c) 1983, 1993 23235 * The Regents of the University of California. All rights reserved. 23236 * 23237 * Redistribution and use in source and binary forms, with or without 23238 * modification, are permitted provided that the following conditions 23239 * are met: 23240 * 1. Redistributions of source code must retain the above copyright 23241 * notice, this list of conditions and the following disclaimer. 23242 * 2. Redistributions in binary form must reproduce the above copyright 23243 * notice, this list of conditions and the following disclaimer in the 23244 * documentation and/or other materials provided with the distribution. 23245 * 3. All advertising materials mentioning features or use of this software 23246 * must display the following acknowledgement: 23247 * This product includes software developed by the University of 23248 * California, Berkeley and its contributors. 23249 * 4. Neither the name of the University nor the names of its contributors 23250 * may be used to endorse or promote products derived from this software 23251 * without specific prior written permission. 23252 * 23253 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23254 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23255 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23256 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23257 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23258 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23259 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23260 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23261 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23262 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23263 * SUCH DAMAGE. 23264 */ 23265 23266 /* Type 3 -- x**31 + x**3 + 1 */ 23267 #define DEG_3 31 23268 #define SEP_3 3 23269 23270 23271 /* Protected by tcp_random_lock */ 23272 static int tcp_randtbl[DEG_3 + 1]; 23273 23274 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23275 static int *tcp_random_rptr = &tcp_randtbl[1]; 23276 23277 static int *tcp_random_state = &tcp_randtbl[1]; 23278 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23279 23280 kmutex_t tcp_random_lock; 23281 23282 void 23283 tcp_random_init(void) 23284 { 23285 int i; 23286 hrtime_t hrt; 23287 time_t wallclock; 23288 uint64_t result; 23289 23290 /* 23291 * Use high-res timer and current time for seed. Gethrtime() returns 23292 * a longlong, which may contain resolution down to nanoseconds. 23293 * The current time will either be a 32-bit or a 64-bit quantity. 23294 * XOR the two together in a 64-bit result variable. 23295 * Convert the result to a 32-bit value by multiplying the high-order 23296 * 32-bits by the low-order 32-bits. 23297 */ 23298 23299 hrt = gethrtime(); 23300 (void) drv_getparm(TIME, &wallclock); 23301 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23302 mutex_enter(&tcp_random_lock); 23303 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23304 (result & 0xffffffff); 23305 23306 for (i = 1; i < DEG_3; i++) 23307 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23308 + 12345; 23309 tcp_random_fptr = &tcp_random_state[SEP_3]; 23310 tcp_random_rptr = &tcp_random_state[0]; 23311 mutex_exit(&tcp_random_lock); 23312 for (i = 0; i < 10 * DEG_3; i++) 23313 (void) tcp_random(); 23314 } 23315 23316 /* 23317 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23318 * This range is selected to be approximately centered on TCP_ISS / 2, 23319 * and easy to compute. We get this value by generating a 32-bit random 23320 * number, selecting out the high-order 17 bits, and then adding one so 23321 * that we never return zero. 23322 */ 23323 int 23324 tcp_random(void) 23325 { 23326 int i; 23327 23328 mutex_enter(&tcp_random_lock); 23329 *tcp_random_fptr += *tcp_random_rptr; 23330 23331 /* 23332 * The high-order bits are more random than the low-order bits, 23333 * so we select out the high-order 17 bits and add one so that 23334 * we never return zero. 23335 */ 23336 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23337 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23338 tcp_random_fptr = tcp_random_state; 23339 ++tcp_random_rptr; 23340 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23341 tcp_random_rptr = tcp_random_state; 23342 23343 mutex_exit(&tcp_random_lock); 23344 return (i); 23345 } 23346 23347 static int 23348 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23349 int *t_errorp, int *sys_errorp) 23350 { 23351 int error; 23352 int is_absreq_failure; 23353 t_scalar_t *opt_lenp; 23354 t_scalar_t opt_offset; 23355 int prim_type; 23356 struct T_conn_req *tcreqp; 23357 struct T_conn_res *tcresp; 23358 cred_t *cr; 23359 23360 /* 23361 * All Solaris components should pass a db_credp 23362 * for this TPI message, hence we ASSERT. 23363 * But in case there is some other M_PROTO that looks 23364 * like a TPI message sent by some other kernel 23365 * component, we check and return an error. 23366 */ 23367 cr = msg_getcred(mp, NULL); 23368 ASSERT(cr != NULL); 23369 if (cr == NULL) 23370 return (-1); 23371 23372 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23373 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23374 prim_type == T_CONN_RES); 23375 23376 switch (prim_type) { 23377 case T_CONN_REQ: 23378 tcreqp = (struct T_conn_req *)mp->b_rptr; 23379 opt_offset = tcreqp->OPT_offset; 23380 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23381 break; 23382 case O_T_CONN_RES: 23383 case T_CONN_RES: 23384 tcresp = (struct T_conn_res *)mp->b_rptr; 23385 opt_offset = tcresp->OPT_offset; 23386 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23387 break; 23388 } 23389 23390 *t_errorp = 0; 23391 *sys_errorp = 0; 23392 *do_disconnectp = 0; 23393 23394 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23395 opt_offset, cr, &tcp_opt_obj, 23396 NULL, &is_absreq_failure); 23397 23398 switch (error) { 23399 case 0: /* no error */ 23400 ASSERT(is_absreq_failure == 0); 23401 return (0); 23402 case ENOPROTOOPT: 23403 *t_errorp = TBADOPT; 23404 break; 23405 case EACCES: 23406 *t_errorp = TACCES; 23407 break; 23408 default: 23409 *t_errorp = TSYSERR; *sys_errorp = error; 23410 break; 23411 } 23412 if (is_absreq_failure != 0) { 23413 /* 23414 * The connection request should get the local ack 23415 * T_OK_ACK and then a T_DISCON_IND. 23416 */ 23417 *do_disconnectp = 1; 23418 } 23419 return (-1); 23420 } 23421 23422 /* 23423 * Split this function out so that if the secret changes, I'm okay. 23424 * 23425 * Initialize the tcp_iss_cookie and tcp_iss_key. 23426 */ 23427 23428 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23429 23430 static void 23431 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23432 { 23433 struct { 23434 int32_t current_time; 23435 uint32_t randnum; 23436 uint16_t pad; 23437 uint8_t ether[6]; 23438 uint8_t passwd[PASSWD_SIZE]; 23439 } tcp_iss_cookie; 23440 time_t t; 23441 23442 /* 23443 * Start with the current absolute time. 23444 */ 23445 (void) drv_getparm(TIME, &t); 23446 tcp_iss_cookie.current_time = t; 23447 23448 /* 23449 * XXX - Need a more random number per RFC 1750, not this crap. 23450 * OTOH, if what follows is pretty random, then I'm in better shape. 23451 */ 23452 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23453 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23454 23455 /* 23456 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23457 * as a good template. 23458 */ 23459 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23460 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23461 23462 /* 23463 * The pass-phrase. Normally this is supplied by user-called NDD. 23464 */ 23465 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23466 23467 /* 23468 * See 4010593 if this section becomes a problem again, 23469 * but the local ethernet address is useful here. 23470 */ 23471 (void) localetheraddr(NULL, 23472 (struct ether_addr *)&tcp_iss_cookie.ether); 23473 23474 /* 23475 * Hash 'em all together. The MD5Final is called per-connection. 23476 */ 23477 mutex_enter(&tcps->tcps_iss_key_lock); 23478 MD5Init(&tcps->tcps_iss_key); 23479 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23480 sizeof (tcp_iss_cookie)); 23481 mutex_exit(&tcps->tcps_iss_key_lock); 23482 } 23483 23484 /* 23485 * Set the RFC 1948 pass phrase 23486 */ 23487 /* ARGSUSED */ 23488 static int 23489 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23490 cred_t *cr) 23491 { 23492 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23493 23494 /* 23495 * Basically, value contains a new pass phrase. Pass it along! 23496 */ 23497 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23498 return (0); 23499 } 23500 23501 /* ARGSUSED */ 23502 static int 23503 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23504 { 23505 bzero(buf, sizeof (tcp_sack_info_t)); 23506 return (0); 23507 } 23508 23509 /* ARGSUSED */ 23510 static int 23511 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23512 { 23513 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23514 return (0); 23515 } 23516 23517 /* 23518 * Make sure we wait until the default queue is setup, yet allow 23519 * tcp_g_q_create() to open a TCP stream. 23520 * We need to allow tcp_g_q_create() do do an open 23521 * of tcp, hence we compare curhread. 23522 * All others have to wait until the tcps_g_q has been 23523 * setup. 23524 */ 23525 void 23526 tcp_g_q_setup(tcp_stack_t *tcps) 23527 { 23528 mutex_enter(&tcps->tcps_g_q_lock); 23529 if (tcps->tcps_g_q != NULL) { 23530 mutex_exit(&tcps->tcps_g_q_lock); 23531 return; 23532 } 23533 if (tcps->tcps_g_q_creator == NULL) { 23534 /* This thread will set it up */ 23535 tcps->tcps_g_q_creator = curthread; 23536 mutex_exit(&tcps->tcps_g_q_lock); 23537 tcp_g_q_create(tcps); 23538 mutex_enter(&tcps->tcps_g_q_lock); 23539 ASSERT(tcps->tcps_g_q_creator == curthread); 23540 tcps->tcps_g_q_creator = NULL; 23541 cv_signal(&tcps->tcps_g_q_cv); 23542 ASSERT(tcps->tcps_g_q != NULL); 23543 mutex_exit(&tcps->tcps_g_q_lock); 23544 return; 23545 } 23546 /* Everybody but the creator has to wait */ 23547 if (tcps->tcps_g_q_creator != curthread) { 23548 while (tcps->tcps_g_q == NULL) 23549 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23550 } 23551 mutex_exit(&tcps->tcps_g_q_lock); 23552 } 23553 23554 #define IP "ip" 23555 23556 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23557 23558 /* 23559 * Create a default tcp queue here instead of in strplumb 23560 */ 23561 void 23562 tcp_g_q_create(tcp_stack_t *tcps) 23563 { 23564 int error; 23565 ldi_handle_t lh = NULL; 23566 ldi_ident_t li = NULL; 23567 int rval; 23568 cred_t *cr; 23569 major_t IP_MAJ; 23570 23571 #ifdef NS_DEBUG 23572 (void) printf("tcp_g_q_create()\n"); 23573 #endif 23574 23575 IP_MAJ = ddi_name_to_major(IP); 23576 23577 ASSERT(tcps->tcps_g_q_creator == curthread); 23578 23579 error = ldi_ident_from_major(IP_MAJ, &li); 23580 if (error) { 23581 #ifdef DEBUG 23582 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23583 error); 23584 #endif 23585 return; 23586 } 23587 23588 cr = zone_get_kcred(netstackid_to_zoneid( 23589 tcps->tcps_netstack->netstack_stackid)); 23590 ASSERT(cr != NULL); 23591 /* 23592 * We set the tcp default queue to IPv6 because IPv4 falls 23593 * back to IPv6 when it can't find a client, but 23594 * IPv6 does not fall back to IPv4. 23595 */ 23596 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23597 if (error) { 23598 #ifdef DEBUG 23599 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23600 error); 23601 #endif 23602 goto out; 23603 } 23604 23605 /* 23606 * This ioctl causes the tcp framework to cache a pointer to 23607 * this stream, so we don't want to close the stream after 23608 * this operation. 23609 * Use the kernel credentials that are for the zone we're in. 23610 */ 23611 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23612 (intptr_t)0, FKIOCTL, cr, &rval); 23613 if (error) { 23614 #ifdef DEBUG 23615 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23616 "error %d\n", error); 23617 #endif 23618 goto out; 23619 } 23620 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23621 lh = NULL; 23622 out: 23623 /* Close layered handles */ 23624 if (li) 23625 ldi_ident_release(li); 23626 /* Keep cred around until _inactive needs it */ 23627 tcps->tcps_g_q_cr = cr; 23628 } 23629 23630 /* 23631 * We keep tcp_g_q set until all other tcp_t's in the zone 23632 * has gone away, and then when tcp_g_q_inactive() is called 23633 * we clear it. 23634 */ 23635 void 23636 tcp_g_q_destroy(tcp_stack_t *tcps) 23637 { 23638 #ifdef NS_DEBUG 23639 (void) printf("tcp_g_q_destroy()for stack %d\n", 23640 tcps->tcps_netstack->netstack_stackid); 23641 #endif 23642 23643 if (tcps->tcps_g_q == NULL) { 23644 return; /* Nothing to cleanup */ 23645 } 23646 /* 23647 * Drop reference corresponding to the default queue. 23648 * This reference was added from tcp_open when the default queue 23649 * was created, hence we compensate for this extra drop in 23650 * tcp_g_q_close. If the refcnt drops to zero here it means 23651 * the default queue was the last one to be open, in which 23652 * case, then tcp_g_q_inactive will be 23653 * called as a result of the refrele. 23654 */ 23655 TCPS_REFRELE(tcps); 23656 } 23657 23658 /* 23659 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23660 * Run by tcp_q_q_inactive using a taskq. 23661 */ 23662 static void 23663 tcp_g_q_close(void *arg) 23664 { 23665 tcp_stack_t *tcps = arg; 23666 int error; 23667 ldi_handle_t lh = NULL; 23668 ldi_ident_t li = NULL; 23669 cred_t *cr; 23670 major_t IP_MAJ; 23671 23672 IP_MAJ = ddi_name_to_major(IP); 23673 23674 #ifdef NS_DEBUG 23675 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23676 tcps->tcps_netstack->netstack_stackid, 23677 tcps->tcps_netstack->netstack_refcnt); 23678 #endif 23679 lh = tcps->tcps_g_q_lh; 23680 if (lh == NULL) 23681 return; /* Nothing to cleanup */ 23682 23683 ASSERT(tcps->tcps_refcnt == 1); 23684 ASSERT(tcps->tcps_g_q != NULL); 23685 23686 error = ldi_ident_from_major(IP_MAJ, &li); 23687 if (error) { 23688 #ifdef DEBUG 23689 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23690 error); 23691 #endif 23692 return; 23693 } 23694 23695 cr = tcps->tcps_g_q_cr; 23696 tcps->tcps_g_q_cr = NULL; 23697 ASSERT(cr != NULL); 23698 23699 /* 23700 * Make sure we can break the recursion when tcp_close decrements 23701 * the reference count causing g_q_inactive to be called again. 23702 */ 23703 tcps->tcps_g_q_lh = NULL; 23704 23705 /* close the default queue */ 23706 (void) ldi_close(lh, FREAD|FWRITE, cr); 23707 /* 23708 * At this point in time tcps and the rest of netstack_t might 23709 * have been deleted. 23710 */ 23711 tcps = NULL; 23712 23713 /* Close layered handles */ 23714 ldi_ident_release(li); 23715 crfree(cr); 23716 } 23717 23718 /* 23719 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23720 * 23721 * Have to ensure that the ldi routines are not used by an 23722 * interrupt thread by using a taskq. 23723 */ 23724 void 23725 tcp_g_q_inactive(tcp_stack_t *tcps) 23726 { 23727 if (tcps->tcps_g_q_lh == NULL) 23728 return; /* Nothing to cleanup */ 23729 23730 ASSERT(tcps->tcps_refcnt == 0); 23731 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23732 23733 if (servicing_interrupt()) { 23734 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23735 (void *) tcps, TQ_SLEEP); 23736 } else { 23737 tcp_g_q_close(tcps); 23738 } 23739 } 23740 23741 /* 23742 * Called by IP when IP is loaded into the kernel 23743 */ 23744 void 23745 tcp_ddi_g_init(void) 23746 { 23747 tcp_timercache = kmem_cache_create("tcp_timercache", 23748 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23749 NULL, NULL, NULL, NULL, NULL, 0); 23750 23751 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23752 sizeof (tcp_sack_info_t), 0, 23753 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23754 23755 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23756 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23757 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23758 23759 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23760 23761 /* Initialize the random number generator */ 23762 tcp_random_init(); 23763 23764 /* A single callback independently of how many netstacks we have */ 23765 ip_squeue_init(tcp_squeue_add); 23766 23767 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23768 23769 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23770 TASKQ_PREPOPULATE); 23771 23772 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23773 23774 /* 23775 * We want to be informed each time a stack is created or 23776 * destroyed in the kernel, so we can maintain the 23777 * set of tcp_stack_t's. 23778 */ 23779 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23780 tcp_stack_fini); 23781 } 23782 23783 23784 #define INET_NAME "ip" 23785 23786 /* 23787 * Initialize the TCP stack instance. 23788 */ 23789 static void * 23790 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23791 { 23792 tcp_stack_t *tcps; 23793 tcpparam_t *pa; 23794 int i; 23795 int error = 0; 23796 major_t major; 23797 23798 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23799 tcps->tcps_netstack = ns; 23800 23801 /* Initialize locks */ 23802 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23803 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23804 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23805 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23806 23807 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23808 tcps->tcps_g_epriv_ports[0] = 2049; 23809 tcps->tcps_g_epriv_ports[1] = 4045; 23810 tcps->tcps_min_anonpriv_port = 512; 23811 23812 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23813 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23814 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23815 TCP_FANOUT_SIZE, KM_SLEEP); 23816 23817 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23818 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 23819 MUTEX_DEFAULT, NULL); 23820 } 23821 23822 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23823 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 23824 MUTEX_DEFAULT, NULL); 23825 } 23826 23827 /* TCP's IPsec code calls the packet dropper. */ 23828 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 23829 23830 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 23831 tcps->tcps_params = pa; 23832 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23833 23834 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 23835 A_CNT(lcl_tcp_param_arr), tcps); 23836 23837 /* 23838 * Note: To really walk the device tree you need the devinfo 23839 * pointer to your device which is only available after probe/attach. 23840 * The following is safe only because it uses ddi_root_node() 23841 */ 23842 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23843 tcp_opt_obj.odb_opt_arr_cnt); 23844 23845 /* 23846 * Initialize RFC 1948 secret values. This will probably be reset once 23847 * by the boot scripts. 23848 * 23849 * Use NULL name, as the name is caught by the new lockstats. 23850 * 23851 * Initialize with some random, non-guessable string, like the global 23852 * T_INFO_ACK. 23853 */ 23854 23855 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23856 sizeof (tcp_g_t_info_ack), tcps); 23857 23858 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 23859 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 23860 23861 major = mod_name_to_major(INET_NAME); 23862 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 23863 ASSERT(error == 0); 23864 return (tcps); 23865 } 23866 23867 /* 23868 * Called when the IP module is about to be unloaded. 23869 */ 23870 void 23871 tcp_ddi_g_destroy(void) 23872 { 23873 tcp_g_kstat_fini(tcp_g_kstat); 23874 tcp_g_kstat = NULL; 23875 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 23876 23877 mutex_destroy(&tcp_random_lock); 23878 23879 kmem_cache_destroy(tcp_timercache); 23880 kmem_cache_destroy(tcp_sack_info_cache); 23881 kmem_cache_destroy(tcp_iphc_cache); 23882 23883 netstack_unregister(NS_TCP); 23884 taskq_destroy(tcp_taskq); 23885 } 23886 23887 /* 23888 * Shut down the TCP stack instance. 23889 */ 23890 /* ARGSUSED */ 23891 static void 23892 tcp_stack_shutdown(netstackid_t stackid, void *arg) 23893 { 23894 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23895 23896 tcp_g_q_destroy(tcps); 23897 } 23898 23899 /* 23900 * Free the TCP stack instance. 23901 */ 23902 static void 23903 tcp_stack_fini(netstackid_t stackid, void *arg) 23904 { 23905 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23906 int i; 23907 23908 nd_free(&tcps->tcps_g_nd); 23909 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23910 tcps->tcps_params = NULL; 23911 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 23912 tcps->tcps_wroff_xtra_param = NULL; 23913 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 23914 tcps->tcps_mdt_head_param = NULL; 23915 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 23916 tcps->tcps_mdt_tail_param = NULL; 23917 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 23918 tcps->tcps_mdt_max_pbufs_param = NULL; 23919 23920 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23921 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 23922 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 23923 } 23924 23925 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23926 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 23927 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 23928 } 23929 23930 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 23931 tcps->tcps_bind_fanout = NULL; 23932 23933 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 23934 tcps->tcps_acceptor_fanout = NULL; 23935 23936 mutex_destroy(&tcps->tcps_iss_key_lock); 23937 mutex_destroy(&tcps->tcps_g_q_lock); 23938 cv_destroy(&tcps->tcps_g_q_cv); 23939 mutex_destroy(&tcps->tcps_epriv_port_lock); 23940 23941 ip_drop_unregister(&tcps->tcps_dropper); 23942 23943 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 23944 tcps->tcps_kstat = NULL; 23945 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 23946 23947 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 23948 tcps->tcps_mibkp = NULL; 23949 23950 ldi_ident_release(tcps->tcps_ldi_ident); 23951 kmem_free(tcps, sizeof (*tcps)); 23952 } 23953 23954 /* 23955 * Generate ISS, taking into account NDD changes may happen halfway through. 23956 * (If the iss is not zero, set it.) 23957 */ 23958 23959 static void 23960 tcp_iss_init(tcp_t *tcp) 23961 { 23962 MD5_CTX context; 23963 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23964 uint32_t answer[4]; 23965 tcp_stack_t *tcps = tcp->tcp_tcps; 23966 23967 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 23968 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 23969 switch (tcps->tcps_strong_iss) { 23970 case 2: 23971 mutex_enter(&tcps->tcps_iss_key_lock); 23972 context = tcps->tcps_iss_key; 23973 mutex_exit(&tcps->tcps_iss_key_lock); 23974 arg.ports = tcp->tcp_ports; 23975 if (tcp->tcp_ipversion == IPV4_VERSION) { 23976 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23977 &arg.src); 23978 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23979 &arg.dst); 23980 } else { 23981 arg.src = tcp->tcp_ip6h->ip6_src; 23982 arg.dst = tcp->tcp_ip6h->ip6_dst; 23983 } 23984 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23985 MD5Final((uchar_t *)answer, &context); 23986 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23987 /* 23988 * Now that we've hashed into a unique per-connection sequence 23989 * space, add a random increment per strong_iss == 1. So I 23990 * guess we'll have to... 23991 */ 23992 /* FALLTHRU */ 23993 case 1: 23994 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 23995 break; 23996 default: 23997 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 23998 break; 23999 } 24000 tcp->tcp_valid_bits = TCP_ISS_VALID; 24001 tcp->tcp_fss = tcp->tcp_iss - 1; 24002 tcp->tcp_suna = tcp->tcp_iss; 24003 tcp->tcp_snxt = tcp->tcp_iss + 1; 24004 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24005 tcp->tcp_csuna = tcp->tcp_snxt; 24006 } 24007 24008 /* 24009 * Exported routine for extracting active tcp connection status. 24010 * 24011 * This is used by the Solaris Cluster Networking software to 24012 * gather a list of connections that need to be forwarded to 24013 * specific nodes in the cluster when configuration changes occur. 24014 * 24015 * The callback is invoked for each tcp_t structure from all netstacks, 24016 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24017 * from the netstack with the specified stack_id. Returning 24018 * non-zero from the callback routine terminates the search. 24019 */ 24020 int 24021 cl_tcp_walk_list(netstackid_t stack_id, 24022 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24023 { 24024 netstack_handle_t nh; 24025 netstack_t *ns; 24026 int ret = 0; 24027 24028 if (stack_id >= 0) { 24029 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24030 return (EINVAL); 24031 24032 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24033 ns->netstack_tcp); 24034 netstack_rele(ns); 24035 return (ret); 24036 } 24037 24038 netstack_next_init(&nh); 24039 while ((ns = netstack_next(&nh)) != NULL) { 24040 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24041 ns->netstack_tcp); 24042 netstack_rele(ns); 24043 } 24044 netstack_next_fini(&nh); 24045 return (ret); 24046 } 24047 24048 static int 24049 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24050 tcp_stack_t *tcps) 24051 { 24052 tcp_t *tcp; 24053 cl_tcp_info_t cl_tcpi; 24054 connf_t *connfp; 24055 conn_t *connp; 24056 int i; 24057 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24058 24059 ASSERT(callback != NULL); 24060 24061 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24062 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24063 connp = NULL; 24064 24065 while ((connp = 24066 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24067 24068 tcp = connp->conn_tcp; 24069 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24070 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24071 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24072 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24073 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24074 /* 24075 * The macros tcp_laddr and tcp_faddr give the IPv4 24076 * addresses. They are copied implicitly below as 24077 * mapped addresses. 24078 */ 24079 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24080 if (tcp->tcp_ipversion == IPV4_VERSION) { 24081 cl_tcpi.cl_tcpi_faddr = 24082 tcp->tcp_ipha->ipha_dst; 24083 } else { 24084 cl_tcpi.cl_tcpi_faddr_v6 = 24085 tcp->tcp_ip6h->ip6_dst; 24086 } 24087 24088 /* 24089 * If the callback returns non-zero 24090 * we terminate the traversal. 24091 */ 24092 if ((*callback)(&cl_tcpi, arg) != 0) { 24093 CONN_DEC_REF(tcp->tcp_connp); 24094 return (1); 24095 } 24096 } 24097 } 24098 24099 return (0); 24100 } 24101 24102 /* 24103 * Macros used for accessing the different types of sockaddr 24104 * structures inside a tcp_ioc_abort_conn_t. 24105 */ 24106 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24107 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24108 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24109 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24110 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24111 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24112 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24113 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24114 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24115 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24116 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24117 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24118 24119 /* 24120 * Return the correct error code to mimic the behavior 24121 * of a connection reset. 24122 */ 24123 #define TCP_AC_GET_ERRCODE(state, err) { \ 24124 switch ((state)) { \ 24125 case TCPS_SYN_SENT: \ 24126 case TCPS_SYN_RCVD: \ 24127 (err) = ECONNREFUSED; \ 24128 break; \ 24129 case TCPS_ESTABLISHED: \ 24130 case TCPS_FIN_WAIT_1: \ 24131 case TCPS_FIN_WAIT_2: \ 24132 case TCPS_CLOSE_WAIT: \ 24133 (err) = ECONNRESET; \ 24134 break; \ 24135 case TCPS_CLOSING: \ 24136 case TCPS_LAST_ACK: \ 24137 case TCPS_TIME_WAIT: \ 24138 (err) = 0; \ 24139 break; \ 24140 default: \ 24141 (err) = ENXIO; \ 24142 } \ 24143 } 24144 24145 /* 24146 * Check if a tcp structure matches the info in acp. 24147 */ 24148 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24149 (((acp)->ac_local.ss_family == AF_INET) ? \ 24150 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24151 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24152 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24153 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24154 (TCP_AC_V4LPORT((acp)) == 0 || \ 24155 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24156 (TCP_AC_V4RPORT((acp)) == 0 || \ 24157 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24158 (acp)->ac_start <= (tcp)->tcp_state && \ 24159 (acp)->ac_end >= (tcp)->tcp_state) : \ 24160 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24161 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24162 &(tcp)->tcp_ip_src_v6)) && \ 24163 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24164 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24165 &(tcp)->tcp_remote_v6)) && \ 24166 (TCP_AC_V6LPORT((acp)) == 0 || \ 24167 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24168 (TCP_AC_V6RPORT((acp)) == 0 || \ 24169 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24170 (acp)->ac_start <= (tcp)->tcp_state && \ 24171 (acp)->ac_end >= (tcp)->tcp_state)) 24172 24173 #define TCP_AC_MATCH(acp, tcp) \ 24174 (((acp)->ac_zoneid == ALL_ZONES || \ 24175 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24176 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24177 24178 /* 24179 * Build a message containing a tcp_ioc_abort_conn_t structure 24180 * which is filled in with information from acp and tp. 24181 */ 24182 static mblk_t * 24183 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24184 { 24185 mblk_t *mp; 24186 tcp_ioc_abort_conn_t *tacp; 24187 24188 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24189 if (mp == NULL) 24190 return (NULL); 24191 24192 mp->b_datap->db_type = M_CTL; 24193 24194 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24195 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24196 sizeof (uint32_t)); 24197 24198 tacp->ac_start = acp->ac_start; 24199 tacp->ac_end = acp->ac_end; 24200 tacp->ac_zoneid = acp->ac_zoneid; 24201 24202 if (acp->ac_local.ss_family == AF_INET) { 24203 tacp->ac_local.ss_family = AF_INET; 24204 tacp->ac_remote.ss_family = AF_INET; 24205 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24206 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24207 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24208 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24209 } else { 24210 tacp->ac_local.ss_family = AF_INET6; 24211 tacp->ac_remote.ss_family = AF_INET6; 24212 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24213 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24214 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24215 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24216 } 24217 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24218 return (mp); 24219 } 24220 24221 /* 24222 * Print a tcp_ioc_abort_conn_t structure. 24223 */ 24224 static void 24225 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24226 { 24227 char lbuf[128]; 24228 char rbuf[128]; 24229 sa_family_t af; 24230 in_port_t lport, rport; 24231 ushort_t logflags; 24232 24233 af = acp->ac_local.ss_family; 24234 24235 if (af == AF_INET) { 24236 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24237 lbuf, 128); 24238 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24239 rbuf, 128); 24240 lport = ntohs(TCP_AC_V4LPORT(acp)); 24241 rport = ntohs(TCP_AC_V4RPORT(acp)); 24242 } else { 24243 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24244 lbuf, 128); 24245 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24246 rbuf, 128); 24247 lport = ntohs(TCP_AC_V6LPORT(acp)); 24248 rport = ntohs(TCP_AC_V6RPORT(acp)); 24249 } 24250 24251 logflags = SL_TRACE | SL_NOTE; 24252 /* 24253 * Don't print this message to the console if the operation was done 24254 * to a non-global zone. 24255 */ 24256 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24257 logflags |= SL_CONSOLE; 24258 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24259 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24260 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24261 acp->ac_start, acp->ac_end); 24262 } 24263 24264 /* 24265 * Called inside tcp_rput when a message built using 24266 * tcp_ioctl_abort_build_msg is put into a queue. 24267 * Note that when we get here there is no wildcard in acp any more. 24268 */ 24269 static void 24270 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24271 { 24272 tcp_ioc_abort_conn_t *acp; 24273 24274 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24275 if (tcp->tcp_state <= acp->ac_end) { 24276 /* 24277 * If we get here, we are already on the correct 24278 * squeue. This ioctl follows the following path 24279 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24280 * ->tcp_ioctl_abort->squeue_enter (if on a 24281 * different squeue) 24282 */ 24283 int errcode; 24284 24285 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24286 (void) tcp_clean_death(tcp, errcode, 26); 24287 } 24288 freemsg(mp); 24289 } 24290 24291 /* 24292 * Abort all matching connections on a hash chain. 24293 */ 24294 static int 24295 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24296 boolean_t exact, tcp_stack_t *tcps) 24297 { 24298 int nmatch, err = 0; 24299 tcp_t *tcp; 24300 MBLKP mp, last, listhead = NULL; 24301 conn_t *tconnp; 24302 connf_t *connfp; 24303 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24304 24305 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24306 24307 startover: 24308 nmatch = 0; 24309 24310 mutex_enter(&connfp->connf_lock); 24311 for (tconnp = connfp->connf_head; tconnp != NULL; 24312 tconnp = tconnp->conn_next) { 24313 tcp = tconnp->conn_tcp; 24314 if (TCP_AC_MATCH(acp, tcp)) { 24315 CONN_INC_REF(tcp->tcp_connp); 24316 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24317 if (mp == NULL) { 24318 err = ENOMEM; 24319 CONN_DEC_REF(tcp->tcp_connp); 24320 break; 24321 } 24322 mp->b_prev = (mblk_t *)tcp; 24323 24324 if (listhead == NULL) { 24325 listhead = mp; 24326 last = mp; 24327 } else { 24328 last->b_next = mp; 24329 last = mp; 24330 } 24331 nmatch++; 24332 if (exact) 24333 break; 24334 } 24335 24336 /* Avoid holding lock for too long. */ 24337 if (nmatch >= 500) 24338 break; 24339 } 24340 mutex_exit(&connfp->connf_lock); 24341 24342 /* Pass mp into the correct tcp */ 24343 while ((mp = listhead) != NULL) { 24344 listhead = listhead->b_next; 24345 tcp = (tcp_t *)mp->b_prev; 24346 mp->b_next = mp->b_prev = NULL; 24347 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24348 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24349 } 24350 24351 *count += nmatch; 24352 if (nmatch >= 500 && err == 0) 24353 goto startover; 24354 return (err); 24355 } 24356 24357 /* 24358 * Abort all connections that matches the attributes specified in acp. 24359 */ 24360 static int 24361 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24362 { 24363 sa_family_t af; 24364 uint32_t ports; 24365 uint16_t *pports; 24366 int err = 0, count = 0; 24367 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24368 int index = -1; 24369 ushort_t logflags; 24370 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24371 24372 af = acp->ac_local.ss_family; 24373 24374 if (af == AF_INET) { 24375 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24376 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24377 pports = (uint16_t *)&ports; 24378 pports[1] = TCP_AC_V4LPORT(acp); 24379 pports[0] = TCP_AC_V4RPORT(acp); 24380 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24381 } 24382 } else { 24383 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24384 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24385 pports = (uint16_t *)&ports; 24386 pports[1] = TCP_AC_V6LPORT(acp); 24387 pports[0] = TCP_AC_V6RPORT(acp); 24388 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24389 } 24390 } 24391 24392 /* 24393 * For cases where remote addr, local port, and remote port are non- 24394 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24395 */ 24396 if (index != -1) { 24397 err = tcp_ioctl_abort_bucket(acp, index, 24398 &count, exact, tcps); 24399 } else { 24400 /* 24401 * loop through all entries for wildcard case 24402 */ 24403 for (index = 0; 24404 index < ipst->ips_ipcl_conn_fanout_size; 24405 index++) { 24406 err = tcp_ioctl_abort_bucket(acp, index, 24407 &count, exact, tcps); 24408 if (err != 0) 24409 break; 24410 } 24411 } 24412 24413 logflags = SL_TRACE | SL_NOTE; 24414 /* 24415 * Don't print this message to the console if the operation was done 24416 * to a non-global zone. 24417 */ 24418 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24419 logflags |= SL_CONSOLE; 24420 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24421 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24422 if (err == 0 && count == 0) 24423 err = ENOENT; 24424 return (err); 24425 } 24426 24427 /* 24428 * Process the TCP_IOC_ABORT_CONN ioctl request. 24429 */ 24430 static void 24431 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24432 { 24433 int err; 24434 IOCP iocp; 24435 MBLKP mp1; 24436 sa_family_t laf, raf; 24437 tcp_ioc_abort_conn_t *acp; 24438 zone_t *zptr; 24439 conn_t *connp = Q_TO_CONN(q); 24440 zoneid_t zoneid = connp->conn_zoneid; 24441 tcp_t *tcp = connp->conn_tcp; 24442 tcp_stack_t *tcps = tcp->tcp_tcps; 24443 24444 iocp = (IOCP)mp->b_rptr; 24445 24446 if ((mp1 = mp->b_cont) == NULL || 24447 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24448 err = EINVAL; 24449 goto out; 24450 } 24451 24452 /* check permissions */ 24453 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24454 err = EPERM; 24455 goto out; 24456 } 24457 24458 if (mp1->b_cont != NULL) { 24459 freemsg(mp1->b_cont); 24460 mp1->b_cont = NULL; 24461 } 24462 24463 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24464 laf = acp->ac_local.ss_family; 24465 raf = acp->ac_remote.ss_family; 24466 24467 /* check that a zone with the supplied zoneid exists */ 24468 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24469 zptr = zone_find_by_id(zoneid); 24470 if (zptr != NULL) { 24471 zone_rele(zptr); 24472 } else { 24473 err = EINVAL; 24474 goto out; 24475 } 24476 } 24477 24478 /* 24479 * For exclusive stacks we set the zoneid to zero 24480 * to make TCP operate as if in the global zone. 24481 */ 24482 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24483 acp->ac_zoneid = GLOBAL_ZONEID; 24484 24485 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24486 acp->ac_start > acp->ac_end || laf != raf || 24487 (laf != AF_INET && laf != AF_INET6)) { 24488 err = EINVAL; 24489 goto out; 24490 } 24491 24492 tcp_ioctl_abort_dump(acp); 24493 err = tcp_ioctl_abort(acp, tcps); 24494 24495 out: 24496 if (mp1 != NULL) { 24497 freemsg(mp1); 24498 mp->b_cont = NULL; 24499 } 24500 24501 if (err != 0) 24502 miocnak(q, mp, 0, err); 24503 else 24504 miocack(q, mp, 0, 0); 24505 } 24506 24507 /* 24508 * tcp_time_wait_processing() handles processing of incoming packets when 24509 * the tcp is in the TIME_WAIT state. 24510 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24511 * on the time wait list. 24512 */ 24513 void 24514 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24515 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24516 { 24517 int32_t bytes_acked; 24518 int32_t gap; 24519 int32_t rgap; 24520 tcp_opt_t tcpopt; 24521 uint_t flags; 24522 uint32_t new_swnd = 0; 24523 conn_t *connp; 24524 tcp_stack_t *tcps = tcp->tcp_tcps; 24525 24526 BUMP_LOCAL(tcp->tcp_ibsegs); 24527 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24528 24529 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24530 new_swnd = BE16_TO_U16(tcph->th_win) << 24531 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24532 if (tcp->tcp_snd_ts_ok) { 24533 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24534 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24535 tcp->tcp_rnxt, TH_ACK); 24536 goto done; 24537 } 24538 } 24539 gap = seg_seq - tcp->tcp_rnxt; 24540 rgap = tcp->tcp_rwnd - (gap + seg_len); 24541 if (gap < 0) { 24542 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24543 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24544 (seg_len > -gap ? -gap : seg_len)); 24545 seg_len += gap; 24546 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24547 if (flags & TH_RST) { 24548 goto done; 24549 } 24550 if ((flags & TH_FIN) && seg_len == -1) { 24551 /* 24552 * When TCP receives a duplicate FIN in 24553 * TIME_WAIT state, restart the 2 MSL timer. 24554 * See page 73 in RFC 793. Make sure this TCP 24555 * is already on the TIME_WAIT list. If not, 24556 * just restart the timer. 24557 */ 24558 if (TCP_IS_DETACHED(tcp)) { 24559 if (tcp_time_wait_remove(tcp, NULL) == 24560 B_TRUE) { 24561 tcp_time_wait_append(tcp); 24562 TCP_DBGSTAT(tcps, 24563 tcp_rput_time_wait); 24564 } 24565 } else { 24566 ASSERT(tcp != NULL); 24567 TCP_TIMER_RESTART(tcp, 24568 tcps->tcps_time_wait_interval); 24569 } 24570 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24571 tcp->tcp_rnxt, TH_ACK); 24572 goto done; 24573 } 24574 flags |= TH_ACK_NEEDED; 24575 seg_len = 0; 24576 goto process_ack; 24577 } 24578 24579 /* Fix seg_seq, and chew the gap off the front. */ 24580 seg_seq = tcp->tcp_rnxt; 24581 } 24582 24583 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24584 /* 24585 * Make sure that when we accept the connection, pick 24586 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24587 * old connection. 24588 * 24589 * The next ISS generated is equal to tcp_iss_incr_extra 24590 * + ISS_INCR/2 + other components depending on the 24591 * value of tcp_strong_iss. We pre-calculate the new 24592 * ISS here and compare with tcp_snxt to determine if 24593 * we need to make adjustment to tcp_iss_incr_extra. 24594 * 24595 * The above calculation is ugly and is a 24596 * waste of CPU cycles... 24597 */ 24598 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24599 int32_t adj; 24600 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24601 24602 switch (tcps->tcps_strong_iss) { 24603 case 2: { 24604 /* Add time and MD5 components. */ 24605 uint32_t answer[4]; 24606 struct { 24607 uint32_t ports; 24608 in6_addr_t src; 24609 in6_addr_t dst; 24610 } arg; 24611 MD5_CTX context; 24612 24613 mutex_enter(&tcps->tcps_iss_key_lock); 24614 context = tcps->tcps_iss_key; 24615 mutex_exit(&tcps->tcps_iss_key_lock); 24616 arg.ports = tcp->tcp_ports; 24617 /* We use MAPPED addresses in tcp_iss_init */ 24618 arg.src = tcp->tcp_ip_src_v6; 24619 if (tcp->tcp_ipversion == IPV4_VERSION) { 24620 IN6_IPADDR_TO_V4MAPPED( 24621 tcp->tcp_ipha->ipha_dst, 24622 &arg.dst); 24623 } else { 24624 arg.dst = 24625 tcp->tcp_ip6h->ip6_dst; 24626 } 24627 MD5Update(&context, (uchar_t *)&arg, 24628 sizeof (arg)); 24629 MD5Final((uchar_t *)answer, &context); 24630 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24631 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24632 break; 24633 } 24634 case 1: 24635 /* Add time component and min random (i.e. 1). */ 24636 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24637 break; 24638 default: 24639 /* Add only time component. */ 24640 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24641 break; 24642 } 24643 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24644 /* 24645 * New ISS not guaranteed to be ISS_INCR/2 24646 * ahead of the current tcp_snxt, so add the 24647 * difference to tcp_iss_incr_extra. 24648 */ 24649 tcps->tcps_iss_incr_extra += adj; 24650 } 24651 /* 24652 * If tcp_clean_death() can not perform the task now, 24653 * drop the SYN packet and let the other side re-xmit. 24654 * Otherwise pass the SYN packet back in, since the 24655 * old tcp state has been cleaned up or freed. 24656 */ 24657 if (tcp_clean_death(tcp, 0, 27) == -1) 24658 goto done; 24659 /* 24660 * We will come back to tcp_rput_data 24661 * on the global queue. Packets destined 24662 * for the global queue will be checked 24663 * with global policy. But the policy for 24664 * this packet has already been checked as 24665 * this was destined for the detached 24666 * connection. We need to bypass policy 24667 * check this time by attaching a dummy 24668 * ipsec_in with ipsec_in_dont_check set. 24669 */ 24670 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24671 if (connp != NULL) { 24672 TCP_STAT(tcps, tcp_time_wait_syn_success); 24673 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24674 return; 24675 } 24676 goto done; 24677 } 24678 24679 /* 24680 * rgap is the amount of stuff received out of window. A negative 24681 * value is the amount out of window. 24682 */ 24683 if (rgap < 0) { 24684 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24685 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24686 /* Fix seg_len and make sure there is something left. */ 24687 seg_len += rgap; 24688 if (seg_len <= 0) { 24689 if (flags & TH_RST) { 24690 goto done; 24691 } 24692 flags |= TH_ACK_NEEDED; 24693 seg_len = 0; 24694 goto process_ack; 24695 } 24696 } 24697 /* 24698 * Check whether we can update tcp_ts_recent. This test is 24699 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24700 * Extensions for High Performance: An Update", Internet Draft. 24701 */ 24702 if (tcp->tcp_snd_ts_ok && 24703 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24704 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24705 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24706 tcp->tcp_last_rcv_lbolt = lbolt64; 24707 } 24708 24709 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24710 /* Always ack out of order packets */ 24711 flags |= TH_ACK_NEEDED; 24712 seg_len = 0; 24713 } else if (seg_len > 0) { 24714 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24715 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24716 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24717 } 24718 if (flags & TH_RST) { 24719 (void) tcp_clean_death(tcp, 0, 28); 24720 goto done; 24721 } 24722 if (flags & TH_SYN) { 24723 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24724 TH_RST|TH_ACK); 24725 /* 24726 * Do not delete the TCP structure if it is in 24727 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24728 */ 24729 goto done; 24730 } 24731 process_ack: 24732 if (flags & TH_ACK) { 24733 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24734 if (bytes_acked <= 0) { 24735 if (bytes_acked == 0 && seg_len == 0 && 24736 new_swnd == tcp->tcp_swnd) 24737 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24738 } else { 24739 /* Acks something not sent */ 24740 flags |= TH_ACK_NEEDED; 24741 } 24742 } 24743 if (flags & TH_ACK_NEEDED) { 24744 /* 24745 * Time to send an ack for some reason. 24746 */ 24747 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24748 tcp->tcp_rnxt, TH_ACK); 24749 } 24750 done: 24751 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24752 DB_CKSUMSTART(mp) = 0; 24753 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24754 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24755 } 24756 freemsg(mp); 24757 } 24758 24759 /* 24760 * TCP Timers Implementation. 24761 */ 24762 timeout_id_t 24763 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24764 { 24765 mblk_t *mp; 24766 tcp_timer_t *tcpt; 24767 tcp_t *tcp = connp->conn_tcp; 24768 24769 ASSERT(connp->conn_sqp != NULL); 24770 24771 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24772 24773 if (tcp->tcp_timercache == NULL) { 24774 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24775 } else { 24776 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24777 mp = tcp->tcp_timercache; 24778 tcp->tcp_timercache = mp->b_next; 24779 mp->b_next = NULL; 24780 ASSERT(mp->b_wptr == NULL); 24781 } 24782 24783 CONN_INC_REF(connp); 24784 tcpt = (tcp_timer_t *)mp->b_rptr; 24785 tcpt->connp = connp; 24786 tcpt->tcpt_proc = f; 24787 /* 24788 * TCP timers are normal timeouts. Plus, they do not require more than 24789 * a 10 millisecond resolution. By choosing a coarser resolution and by 24790 * rounding up the expiration to the next resolution boundary, we can 24791 * batch timers in the callout subsystem to make TCP timers more 24792 * efficient. The roundup also protects short timers from expiring too 24793 * early before they have a chance to be cancelled. 24794 */ 24795 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24796 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24797 24798 return ((timeout_id_t)mp); 24799 } 24800 24801 static void 24802 tcp_timer_callback(void *arg) 24803 { 24804 mblk_t *mp = (mblk_t *)arg; 24805 tcp_timer_t *tcpt; 24806 conn_t *connp; 24807 24808 tcpt = (tcp_timer_t *)mp->b_rptr; 24809 connp = tcpt->connp; 24810 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24811 SQ_FILL, SQTAG_TCP_TIMER); 24812 } 24813 24814 static void 24815 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24816 { 24817 tcp_timer_t *tcpt; 24818 conn_t *connp = (conn_t *)arg; 24819 tcp_t *tcp = connp->conn_tcp; 24820 24821 tcpt = (tcp_timer_t *)mp->b_rptr; 24822 ASSERT(connp == tcpt->connp); 24823 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24824 24825 /* 24826 * If the TCP has reached the closed state, don't proceed any 24827 * further. This TCP logically does not exist on the system. 24828 * tcpt_proc could for example access queues, that have already 24829 * been qprocoff'ed off. Also see comments at the start of tcp_input 24830 */ 24831 if (tcp->tcp_state != TCPS_CLOSED) { 24832 (*tcpt->tcpt_proc)(connp); 24833 } else { 24834 tcp->tcp_timer_tid = 0; 24835 } 24836 tcp_timer_free(connp->conn_tcp, mp); 24837 } 24838 24839 /* 24840 * There is potential race with untimeout and the handler firing at the same 24841 * time. The mblock may be freed by the handler while we are trying to use 24842 * it. But since both should execute on the same squeue, this race should not 24843 * occur. 24844 */ 24845 clock_t 24846 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24847 { 24848 mblk_t *mp = (mblk_t *)id; 24849 tcp_timer_t *tcpt; 24850 clock_t delta; 24851 24852 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 24853 24854 if (mp == NULL) 24855 return (-1); 24856 24857 tcpt = (tcp_timer_t *)mp->b_rptr; 24858 ASSERT(tcpt->connp == connp); 24859 24860 delta = untimeout_default(tcpt->tcpt_tid, 0); 24861 24862 if (delta >= 0) { 24863 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 24864 tcp_timer_free(connp->conn_tcp, mp); 24865 CONN_DEC_REF(connp); 24866 } 24867 24868 return (delta); 24869 } 24870 24871 /* 24872 * Allocate space for the timer event. The allocation looks like mblk, but it is 24873 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24874 * 24875 * Dealing with failures: If we can't allocate from the timer cache we try 24876 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24877 * points to b_rptr. 24878 * If we can't allocate anything using allocb_tryhard(), we perform a last 24879 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24880 * save the actual allocation size in b_datap. 24881 */ 24882 mblk_t * 24883 tcp_timermp_alloc(int kmflags) 24884 { 24885 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24886 kmflags & ~KM_PANIC); 24887 24888 if (mp != NULL) { 24889 mp->b_next = mp->b_prev = NULL; 24890 mp->b_rptr = (uchar_t *)(&mp[1]); 24891 mp->b_wptr = NULL; 24892 mp->b_datap = NULL; 24893 mp->b_queue = NULL; 24894 mp->b_cont = NULL; 24895 } else if (kmflags & KM_PANIC) { 24896 /* 24897 * Failed to allocate memory for the timer. Try allocating from 24898 * dblock caches. 24899 */ 24900 /* ipclassifier calls this from a constructor - hence no tcps */ 24901 TCP_G_STAT(tcp_timermp_allocfail); 24902 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24903 if (mp == NULL) { 24904 size_t size = 0; 24905 /* 24906 * Memory is really low. Try tryhard allocation. 24907 * 24908 * ipclassifier calls this from a constructor - 24909 * hence no tcps 24910 */ 24911 TCP_G_STAT(tcp_timermp_allocdblfail); 24912 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24913 sizeof (tcp_timer_t), &size, kmflags); 24914 mp->b_rptr = (uchar_t *)(&mp[1]); 24915 mp->b_next = mp->b_prev = NULL; 24916 mp->b_wptr = (uchar_t *)-1; 24917 mp->b_datap = (dblk_t *)size; 24918 mp->b_queue = NULL; 24919 mp->b_cont = NULL; 24920 } 24921 ASSERT(mp->b_wptr != NULL); 24922 } 24923 /* ipclassifier calls this from a constructor - hence no tcps */ 24924 TCP_G_DBGSTAT(tcp_timermp_alloced); 24925 24926 return (mp); 24927 } 24928 24929 /* 24930 * Free per-tcp timer cache. 24931 * It can only contain entries from tcp_timercache. 24932 */ 24933 void 24934 tcp_timermp_free(tcp_t *tcp) 24935 { 24936 mblk_t *mp; 24937 24938 while ((mp = tcp->tcp_timercache) != NULL) { 24939 ASSERT(mp->b_wptr == NULL); 24940 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24941 kmem_cache_free(tcp_timercache, mp); 24942 } 24943 } 24944 24945 /* 24946 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24947 * events there already (currently at most two events are cached). 24948 * If the event is not allocated from the timer cache, free it right away. 24949 */ 24950 static void 24951 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24952 { 24953 mblk_t *mp1 = tcp->tcp_timercache; 24954 24955 if (mp->b_wptr != NULL) { 24956 /* 24957 * This allocation is not from a timer cache, free it right 24958 * away. 24959 */ 24960 if (mp->b_wptr != (uchar_t *)-1) 24961 freeb(mp); 24962 else 24963 kmem_free(mp, (size_t)mp->b_datap); 24964 } else if (mp1 == NULL || mp1->b_next == NULL) { 24965 /* Cache this timer block for future allocations */ 24966 mp->b_rptr = (uchar_t *)(&mp[1]); 24967 mp->b_next = mp1; 24968 tcp->tcp_timercache = mp; 24969 } else { 24970 kmem_cache_free(tcp_timercache, mp); 24971 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 24972 } 24973 } 24974 24975 /* 24976 * End of TCP Timers implementation. 24977 */ 24978 24979 /* 24980 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24981 * on the specified backing STREAMS q. Note, the caller may make the 24982 * decision to call based on the tcp_t.tcp_flow_stopped value which 24983 * when check outside the q's lock is only an advisory check ... 24984 */ 24985 void 24986 tcp_setqfull(tcp_t *tcp) 24987 { 24988 tcp_stack_t *tcps = tcp->tcp_tcps; 24989 conn_t *connp = tcp->tcp_connp; 24990 24991 if (tcp->tcp_closed) 24992 return; 24993 24994 if (IPCL_IS_NONSTR(connp)) { 24995 (*connp->conn_upcalls->su_txq_full) 24996 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 24997 tcp->tcp_flow_stopped = B_TRUE; 24998 } else { 24999 queue_t *q = tcp->tcp_wq; 25000 25001 if (!(q->q_flag & QFULL)) { 25002 mutex_enter(QLOCK(q)); 25003 if (!(q->q_flag & QFULL)) { 25004 /* still need to set QFULL */ 25005 q->q_flag |= QFULL; 25006 tcp->tcp_flow_stopped = B_TRUE; 25007 mutex_exit(QLOCK(q)); 25008 TCP_STAT(tcps, tcp_flwctl_on); 25009 } else { 25010 mutex_exit(QLOCK(q)); 25011 } 25012 } 25013 } 25014 } 25015 25016 void 25017 tcp_clrqfull(tcp_t *tcp) 25018 { 25019 conn_t *connp = tcp->tcp_connp; 25020 25021 if (tcp->tcp_closed) 25022 return; 25023 25024 if (IPCL_IS_NONSTR(connp)) { 25025 (*connp->conn_upcalls->su_txq_full) 25026 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25027 tcp->tcp_flow_stopped = B_FALSE; 25028 } else { 25029 queue_t *q = tcp->tcp_wq; 25030 25031 if (q->q_flag & QFULL) { 25032 mutex_enter(QLOCK(q)); 25033 if (q->q_flag & QFULL) { 25034 q->q_flag &= ~QFULL; 25035 tcp->tcp_flow_stopped = B_FALSE; 25036 mutex_exit(QLOCK(q)); 25037 if (q->q_flag & QWANTW) 25038 qbackenable(q, 0); 25039 } else { 25040 mutex_exit(QLOCK(q)); 25041 } 25042 } 25043 } 25044 } 25045 25046 /* 25047 * kstats related to squeues i.e. not per IP instance 25048 */ 25049 static void * 25050 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25051 { 25052 kstat_t *ksp; 25053 25054 tcp_g_stat_t template = { 25055 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25056 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25057 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25058 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25059 }; 25060 25061 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25062 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25063 KSTAT_FLAG_VIRTUAL); 25064 25065 if (ksp == NULL) 25066 return (NULL); 25067 25068 bcopy(&template, tcp_g_statp, sizeof (template)); 25069 ksp->ks_data = (void *)tcp_g_statp; 25070 25071 kstat_install(ksp); 25072 return (ksp); 25073 } 25074 25075 static void 25076 tcp_g_kstat_fini(kstat_t *ksp) 25077 { 25078 if (ksp != NULL) { 25079 kstat_delete(ksp); 25080 } 25081 } 25082 25083 25084 static void * 25085 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25086 { 25087 kstat_t *ksp; 25088 25089 tcp_stat_t template = { 25090 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25091 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25092 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25093 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25094 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25095 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25096 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25097 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25098 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25099 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25100 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25101 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25102 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25103 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25104 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25105 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25106 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25107 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25108 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25109 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25110 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25111 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25112 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25113 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25114 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25115 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25116 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25117 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25118 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25119 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25120 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25121 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25122 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25123 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25124 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25125 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25126 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25127 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25128 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25129 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25130 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25131 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25132 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25133 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25134 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25135 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25136 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25137 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25138 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25139 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25140 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25141 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25142 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25143 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25144 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25145 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25146 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25147 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25148 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25149 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25150 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25151 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25152 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25153 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25154 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25155 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25156 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25157 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25158 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25159 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25160 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25161 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25162 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25163 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25164 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25165 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25166 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25167 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25168 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25169 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25170 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25171 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25172 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25173 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25174 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25175 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25176 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25177 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25178 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25179 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25180 }; 25181 25182 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25183 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25184 KSTAT_FLAG_VIRTUAL, stackid); 25185 25186 if (ksp == NULL) 25187 return (NULL); 25188 25189 bcopy(&template, tcps_statisticsp, sizeof (template)); 25190 ksp->ks_data = (void *)tcps_statisticsp; 25191 ksp->ks_private = (void *)(uintptr_t)stackid; 25192 25193 kstat_install(ksp); 25194 return (ksp); 25195 } 25196 25197 static void 25198 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25199 { 25200 if (ksp != NULL) { 25201 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25202 kstat_delete_netstack(ksp, stackid); 25203 } 25204 } 25205 25206 /* 25207 * TCP Kstats implementation 25208 */ 25209 static void * 25210 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25211 { 25212 kstat_t *ksp; 25213 25214 tcp_named_kstat_t template = { 25215 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25216 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25217 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25218 { "maxConn", KSTAT_DATA_INT32, 0 }, 25219 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25220 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25221 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25222 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25223 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25224 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25225 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25226 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25227 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25228 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25229 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25230 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25231 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25232 { "outAck", KSTAT_DATA_UINT32, 0 }, 25233 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25234 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25235 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25236 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25237 { "outControl", KSTAT_DATA_UINT32, 0 }, 25238 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25239 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25240 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25241 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25242 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25243 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25244 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25245 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25246 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25247 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25248 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25249 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25250 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25251 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25252 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25253 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25254 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25255 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25256 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25257 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25258 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25259 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25260 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25261 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25262 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25263 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25264 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25265 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25266 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25267 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25268 }; 25269 25270 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25271 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25272 25273 if (ksp == NULL) 25274 return (NULL); 25275 25276 template.rtoAlgorithm.value.ui32 = 4; 25277 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25278 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25279 template.maxConn.value.i32 = -1; 25280 25281 bcopy(&template, ksp->ks_data, sizeof (template)); 25282 ksp->ks_update = tcp_kstat_update; 25283 ksp->ks_private = (void *)(uintptr_t)stackid; 25284 25285 kstat_install(ksp); 25286 return (ksp); 25287 } 25288 25289 static void 25290 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25291 { 25292 if (ksp != NULL) { 25293 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25294 kstat_delete_netstack(ksp, stackid); 25295 } 25296 } 25297 25298 static int 25299 tcp_kstat_update(kstat_t *kp, int rw) 25300 { 25301 tcp_named_kstat_t *tcpkp; 25302 tcp_t *tcp; 25303 connf_t *connfp; 25304 conn_t *connp; 25305 int i; 25306 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25307 netstack_t *ns; 25308 tcp_stack_t *tcps; 25309 ip_stack_t *ipst; 25310 25311 if ((kp == NULL) || (kp->ks_data == NULL)) 25312 return (EIO); 25313 25314 if (rw == KSTAT_WRITE) 25315 return (EACCES); 25316 25317 ns = netstack_find_by_stackid(stackid); 25318 if (ns == NULL) 25319 return (-1); 25320 tcps = ns->netstack_tcp; 25321 if (tcps == NULL) { 25322 netstack_rele(ns); 25323 return (-1); 25324 } 25325 25326 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25327 25328 tcpkp->currEstab.value.ui32 = 0; 25329 25330 ipst = ns->netstack_ip; 25331 25332 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25333 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25334 connp = NULL; 25335 while ((connp = 25336 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25337 tcp = connp->conn_tcp; 25338 switch (tcp_snmp_state(tcp)) { 25339 case MIB2_TCP_established: 25340 case MIB2_TCP_closeWait: 25341 tcpkp->currEstab.value.ui32++; 25342 break; 25343 } 25344 } 25345 } 25346 25347 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25348 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25349 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25350 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25351 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25352 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25353 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25354 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25355 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25356 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25357 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25358 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25359 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25360 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25361 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25362 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25363 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25364 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25365 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25366 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25367 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25368 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25369 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25370 tcpkp->inDataInorderSegs.value.ui32 = 25371 tcps->tcps_mib.tcpInDataInorderSegs; 25372 tcpkp->inDataInorderBytes.value.ui32 = 25373 tcps->tcps_mib.tcpInDataInorderBytes; 25374 tcpkp->inDataUnorderSegs.value.ui32 = 25375 tcps->tcps_mib.tcpInDataUnorderSegs; 25376 tcpkp->inDataUnorderBytes.value.ui32 = 25377 tcps->tcps_mib.tcpInDataUnorderBytes; 25378 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25379 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25380 tcpkp->inDataPartDupSegs.value.ui32 = 25381 tcps->tcps_mib.tcpInDataPartDupSegs; 25382 tcpkp->inDataPartDupBytes.value.ui32 = 25383 tcps->tcps_mib.tcpInDataPartDupBytes; 25384 tcpkp->inDataPastWinSegs.value.ui32 = 25385 tcps->tcps_mib.tcpInDataPastWinSegs; 25386 tcpkp->inDataPastWinBytes.value.ui32 = 25387 tcps->tcps_mib.tcpInDataPastWinBytes; 25388 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25389 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25390 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25391 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25392 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25393 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25394 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25395 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25396 tcpkp->timKeepaliveProbe.value.ui32 = 25397 tcps->tcps_mib.tcpTimKeepaliveProbe; 25398 tcpkp->timKeepaliveDrop.value.ui32 = 25399 tcps->tcps_mib.tcpTimKeepaliveDrop; 25400 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25401 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25402 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25403 tcpkp->outSackRetransSegs.value.ui32 = 25404 tcps->tcps_mib.tcpOutSackRetransSegs; 25405 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25406 25407 netstack_rele(ns); 25408 return (0); 25409 } 25410 25411 void 25412 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25413 { 25414 uint16_t hdr_len; 25415 ipha_t *ipha; 25416 uint8_t *nexthdrp; 25417 tcph_t *tcph; 25418 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25419 25420 /* Already has an eager */ 25421 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25422 TCP_STAT(tcps, tcp_reinput_syn); 25423 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25424 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25425 return; 25426 } 25427 25428 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25429 case IPV4_VERSION: 25430 ipha = (ipha_t *)mp->b_rptr; 25431 hdr_len = IPH_HDR_LENGTH(ipha); 25432 break; 25433 case IPV6_VERSION: 25434 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25435 &hdr_len, &nexthdrp)) { 25436 CONN_DEC_REF(connp); 25437 freemsg(mp); 25438 return; 25439 } 25440 break; 25441 } 25442 25443 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25444 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25445 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25446 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25447 } 25448 25449 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25450 SQ_FILL, SQTAG_TCP_REINPUT); 25451 } 25452 25453 static int 25454 tcp_squeue_switch(int val) 25455 { 25456 int rval = SQ_FILL; 25457 25458 switch (val) { 25459 case 1: 25460 rval = SQ_NODRAIN; 25461 break; 25462 case 2: 25463 rval = SQ_PROCESS; 25464 break; 25465 default: 25466 break; 25467 } 25468 return (rval); 25469 } 25470 25471 /* 25472 * This is called once for each squeue - globally for all stack 25473 * instances. 25474 */ 25475 static void 25476 tcp_squeue_add(squeue_t *sqp) 25477 { 25478 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25479 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25480 25481 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25482 tcp_time_wait->tcp_time_wait_tid = 25483 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25484 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25485 CALLOUT_FLAG_ROUNDUP); 25486 if (tcp_free_list_max_cnt == 0) { 25487 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25488 max_ncpus : boot_max_ncpus); 25489 25490 /* 25491 * Limit number of entries to 1% of availble memory / tcp_ncpus 25492 */ 25493 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25494 (tcp_ncpus * sizeof (tcp_t) * 100); 25495 } 25496 tcp_time_wait->tcp_free_list_cnt = 0; 25497 } 25498 25499 static int 25500 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25501 { 25502 mblk_t *ire_mp = NULL; 25503 mblk_t *syn_mp; 25504 mblk_t *mdti; 25505 mblk_t *lsoi; 25506 int retval; 25507 tcph_t *tcph; 25508 uint32_t mss; 25509 queue_t *q = tcp->tcp_rq; 25510 conn_t *connp = tcp->tcp_connp; 25511 tcp_stack_t *tcps = tcp->tcp_tcps; 25512 25513 if (error == 0) { 25514 /* 25515 * Adapt Multidata information, if any. The 25516 * following tcp_mdt_update routine will free 25517 * the message. 25518 */ 25519 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25520 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25521 b_rptr)->mdt_capab, B_TRUE); 25522 freemsg(mdti); 25523 } 25524 25525 /* 25526 * Check to update LSO information with tcp, and 25527 * tcp_lso_update routine will free the message. 25528 */ 25529 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25530 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25531 b_rptr)->lso_capab); 25532 freemsg(lsoi); 25533 } 25534 25535 /* Get the IRE, if we had requested for it */ 25536 if (mp != NULL) 25537 ire_mp = tcp_ire_mp(&mp); 25538 25539 if (tcp->tcp_hard_binding) { 25540 tcp->tcp_hard_binding = B_FALSE; 25541 tcp->tcp_hard_bound = B_TRUE; 25542 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25543 if (retval != 0) { 25544 error = EADDRINUSE; 25545 goto bind_failed; 25546 } 25547 } else { 25548 if (ire_mp != NULL) 25549 freeb(ire_mp); 25550 goto after_syn_sent; 25551 } 25552 25553 retval = tcp_adapt_ire(tcp, ire_mp); 25554 if (ire_mp != NULL) 25555 freeb(ire_mp); 25556 if (retval == 0) { 25557 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25558 ENETUNREACH : EADDRNOTAVAIL); 25559 goto ipcl_rm; 25560 } 25561 /* 25562 * Don't let an endpoint connect to itself. 25563 * Also checked in tcp_connect() but that 25564 * check can't handle the case when the 25565 * local IP address is INADDR_ANY. 25566 */ 25567 if (tcp->tcp_ipversion == IPV4_VERSION) { 25568 if ((tcp->tcp_ipha->ipha_dst == 25569 tcp->tcp_ipha->ipha_src) && 25570 (BE16_EQL(tcp->tcp_tcph->th_lport, 25571 tcp->tcp_tcph->th_fport))) { 25572 error = EADDRNOTAVAIL; 25573 goto ipcl_rm; 25574 } 25575 } else { 25576 if (IN6_ARE_ADDR_EQUAL( 25577 &tcp->tcp_ip6h->ip6_dst, 25578 &tcp->tcp_ip6h->ip6_src) && 25579 (BE16_EQL(tcp->tcp_tcph->th_lport, 25580 tcp->tcp_tcph->th_fport))) { 25581 error = EADDRNOTAVAIL; 25582 goto ipcl_rm; 25583 } 25584 } 25585 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25586 /* 25587 * This should not be possible! Just for 25588 * defensive coding... 25589 */ 25590 if (tcp->tcp_state != TCPS_SYN_SENT) 25591 goto after_syn_sent; 25592 25593 if (is_system_labeled() && 25594 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25595 error = EHOSTUNREACH; 25596 goto ipcl_rm; 25597 } 25598 25599 /* 25600 * tcp_adapt_ire() does not adjust 25601 * for TCP/IP header length. 25602 */ 25603 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25604 25605 /* 25606 * Just make sure our rwnd is at 25607 * least tcp_recv_hiwat_mss * MSS 25608 * large, and round up to the nearest 25609 * MSS. 25610 * 25611 * We do the round up here because 25612 * we need to get the interface 25613 * MTU first before we can do the 25614 * round up. 25615 */ 25616 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25617 tcps->tcps_recv_hiwat_minmss * mss); 25618 if (!IPCL_IS_NONSTR(connp)) 25619 q->q_hiwat = tcp->tcp_rwnd; 25620 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25621 tcp_set_ws_value(tcp); 25622 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25623 tcp->tcp_tcph->th_win); 25624 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25625 tcp->tcp_snd_ws_ok = B_TRUE; 25626 25627 /* 25628 * Set tcp_snd_ts_ok to true 25629 * so that tcp_xmit_mp will 25630 * include the timestamp 25631 * option in the SYN segment. 25632 */ 25633 if (tcps->tcps_tstamp_always || 25634 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25635 tcp->tcp_snd_ts_ok = B_TRUE; 25636 } 25637 25638 /* 25639 * tcp_snd_sack_ok can be set in 25640 * tcp_adapt_ire() if the sack metric 25641 * is set. So check it here also. 25642 */ 25643 if (tcps->tcps_sack_permitted == 2 || 25644 tcp->tcp_snd_sack_ok) { 25645 if (tcp->tcp_sack_info == NULL) { 25646 tcp->tcp_sack_info = 25647 kmem_cache_alloc(tcp_sack_info_cache, 25648 KM_SLEEP); 25649 } 25650 tcp->tcp_snd_sack_ok = B_TRUE; 25651 } 25652 25653 /* 25654 * Should we use ECN? Note that the current 25655 * default value (SunOS 5.9) of tcp_ecn_permitted 25656 * is 1. The reason for doing this is that there 25657 * are equipments out there that will drop ECN 25658 * enabled IP packets. Setting it to 1 avoids 25659 * compatibility problems. 25660 */ 25661 if (tcps->tcps_ecn_permitted == 2) 25662 tcp->tcp_ecn_ok = B_TRUE; 25663 25664 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25665 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25666 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25667 if (syn_mp) { 25668 if (cr == NULL) { 25669 cr = tcp->tcp_cred; 25670 pid = tcp->tcp_cpid; 25671 } 25672 mblk_setcred(syn_mp, cr, pid); 25673 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25674 } 25675 after_syn_sent: 25676 if (mp != NULL) { 25677 ASSERT(mp->b_cont == NULL); 25678 freeb(mp); 25679 } 25680 return (error); 25681 } else { 25682 /* error */ 25683 if (tcp->tcp_debug) { 25684 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25685 "tcp_post_ip_bind: error == %d", error); 25686 } 25687 if (mp != NULL) { 25688 freeb(mp); 25689 } 25690 } 25691 25692 ipcl_rm: 25693 /* 25694 * Need to unbind with classifier since we were just 25695 * told that our bind succeeded. a.k.a error == 0 at the entry. 25696 */ 25697 tcp->tcp_hard_bound = B_FALSE; 25698 tcp->tcp_hard_binding = B_FALSE; 25699 25700 ipcl_hash_remove(connp); 25701 25702 bind_failed: 25703 tcp->tcp_state = TCPS_IDLE; 25704 if (tcp->tcp_ipversion == IPV4_VERSION) 25705 tcp->tcp_ipha->ipha_src = 0; 25706 else 25707 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25708 /* 25709 * Copy of the src addr. in tcp_t is needed since 25710 * the lookup funcs. can only look at tcp_t 25711 */ 25712 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25713 25714 tcph = tcp->tcp_tcph; 25715 tcph->th_lport[0] = 0; 25716 tcph->th_lport[1] = 0; 25717 tcp_bind_hash_remove(tcp); 25718 bzero(&connp->u_port, sizeof (connp->u_port)); 25719 /* blow away saved option results if any */ 25720 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25721 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25722 25723 conn_delete_ire(tcp->tcp_connp, NULL); 25724 25725 return (error); 25726 } 25727 25728 static int 25729 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25730 boolean_t bind_to_req_port_only, cred_t *cr) 25731 { 25732 in_port_t mlp_port; 25733 mlp_type_t addrtype, mlptype; 25734 boolean_t user_specified; 25735 in_port_t allocated_port; 25736 in_port_t requested_port = *requested_port_ptr; 25737 conn_t *connp; 25738 zone_t *zone; 25739 tcp_stack_t *tcps = tcp->tcp_tcps; 25740 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25741 25742 /* 25743 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25744 */ 25745 if (cr == NULL) 25746 cr = tcp->tcp_cred; 25747 /* 25748 * Get a valid port (within the anonymous range and should not 25749 * be a privileged one) to use if the user has not given a port. 25750 * If multiple threads are here, they may all start with 25751 * with the same initial port. But, it should be fine as long as 25752 * tcp_bindi will ensure that no two threads will be assigned 25753 * the same port. 25754 * 25755 * NOTE: XXX If a privileged process asks for an anonymous port, we 25756 * still check for ports only in the range > tcp_smallest_non_priv_port, 25757 * unless TCP_ANONPRIVBIND option is set. 25758 */ 25759 mlptype = mlptSingle; 25760 mlp_port = requested_port; 25761 if (requested_port == 0) { 25762 requested_port = tcp->tcp_anon_priv_bind ? 25763 tcp_get_next_priv_port(tcp) : 25764 tcp_update_next_port(tcps->tcps_next_port_to_try, 25765 tcp, B_TRUE); 25766 if (requested_port == 0) { 25767 return (-TNOADDR); 25768 } 25769 user_specified = B_FALSE; 25770 25771 /* 25772 * If the user went through one of the RPC interfaces to create 25773 * this socket and RPC is MLP in this zone, then give him an 25774 * anonymous MLP. 25775 */ 25776 connp = tcp->tcp_connp; 25777 if (connp->conn_anon_mlp && is_system_labeled()) { 25778 zone = crgetzone(cr); 25779 addrtype = tsol_mlp_addr_type(zone->zone_id, 25780 IPV6_VERSION, &v6addr, 25781 tcps->tcps_netstack->netstack_ip); 25782 if (addrtype == mlptSingle) { 25783 return (-TNOADDR); 25784 } 25785 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25786 PMAPPORT, addrtype); 25787 mlp_port = PMAPPORT; 25788 } 25789 } else { 25790 int i; 25791 boolean_t priv = B_FALSE; 25792 25793 /* 25794 * If the requested_port is in the well-known privileged range, 25795 * verify that the stream was opened by a privileged user. 25796 * Note: No locks are held when inspecting tcp_g_*epriv_ports 25797 * but instead the code relies on: 25798 * - the fact that the address of the array and its size never 25799 * changes 25800 * - the atomic assignment of the elements of the array 25801 */ 25802 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 25803 priv = B_TRUE; 25804 } else { 25805 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 25806 if (requested_port == 25807 tcps->tcps_g_epriv_ports[i]) { 25808 priv = B_TRUE; 25809 break; 25810 } 25811 } 25812 } 25813 if (priv) { 25814 if (secpolicy_net_privaddr(cr, requested_port, 25815 IPPROTO_TCP) != 0) { 25816 if (tcp->tcp_debug) { 25817 (void) strlog(TCP_MOD_ID, 0, 1, 25818 SL_ERROR|SL_TRACE, 25819 "tcp_bind: no priv for port %d", 25820 requested_port); 25821 } 25822 return (-TACCES); 25823 } 25824 } 25825 user_specified = B_TRUE; 25826 25827 connp = tcp->tcp_connp; 25828 if (is_system_labeled()) { 25829 zone = crgetzone(cr); 25830 addrtype = tsol_mlp_addr_type(zone->zone_id, 25831 IPV6_VERSION, &v6addr, 25832 tcps->tcps_netstack->netstack_ip); 25833 if (addrtype == mlptSingle) { 25834 return (-TNOADDR); 25835 } 25836 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25837 requested_port, addrtype); 25838 } 25839 } 25840 25841 if (mlptype != mlptSingle) { 25842 if (secpolicy_net_bindmlp(cr) != 0) { 25843 if (tcp->tcp_debug) { 25844 (void) strlog(TCP_MOD_ID, 0, 1, 25845 SL_ERROR|SL_TRACE, 25846 "tcp_bind: no priv for multilevel port %d", 25847 requested_port); 25848 } 25849 return (-TACCES); 25850 } 25851 25852 /* 25853 * If we're specifically binding a shared IP address and the 25854 * port is MLP on shared addresses, then check to see if this 25855 * zone actually owns the MLP. Reject if not. 25856 */ 25857 if (mlptype == mlptShared && addrtype == mlptShared) { 25858 /* 25859 * No need to handle exclusive-stack zones since 25860 * ALL_ZONES only applies to the shared stack. 25861 */ 25862 zoneid_t mlpzone; 25863 25864 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 25865 htons(mlp_port)); 25866 if (connp->conn_zoneid != mlpzone) { 25867 if (tcp->tcp_debug) { 25868 (void) strlog(TCP_MOD_ID, 0, 1, 25869 SL_ERROR|SL_TRACE, 25870 "tcp_bind: attempt to bind port " 25871 "%d on shared addr in zone %d " 25872 "(should be %d)", 25873 mlp_port, connp->conn_zoneid, 25874 mlpzone); 25875 } 25876 return (-TACCES); 25877 } 25878 } 25879 25880 if (!user_specified) { 25881 int err; 25882 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25883 requested_port, B_TRUE); 25884 if (err != 0) { 25885 if (tcp->tcp_debug) { 25886 (void) strlog(TCP_MOD_ID, 0, 1, 25887 SL_ERROR|SL_TRACE, 25888 "tcp_bind: cannot establish anon " 25889 "MLP for port %d", 25890 requested_port); 25891 } 25892 return (err); 25893 } 25894 connp->conn_anon_port = B_TRUE; 25895 } 25896 connp->conn_mlp_type = mlptype; 25897 } 25898 25899 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 25900 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 25901 25902 if (allocated_port == 0) { 25903 connp->conn_mlp_type = mlptSingle; 25904 if (connp->conn_anon_port) { 25905 connp->conn_anon_port = B_FALSE; 25906 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25907 requested_port, B_FALSE); 25908 } 25909 if (bind_to_req_port_only) { 25910 if (tcp->tcp_debug) { 25911 (void) strlog(TCP_MOD_ID, 0, 1, 25912 SL_ERROR|SL_TRACE, 25913 "tcp_bind: requested addr busy"); 25914 } 25915 return (-TADDRBUSY); 25916 } else { 25917 /* If we are out of ports, fail the bind. */ 25918 if (tcp->tcp_debug) { 25919 (void) strlog(TCP_MOD_ID, 0, 1, 25920 SL_ERROR|SL_TRACE, 25921 "tcp_bind: out of ports?"); 25922 } 25923 return (-TNOADDR); 25924 } 25925 } 25926 25927 /* Pass the allocated port back */ 25928 *requested_port_ptr = allocated_port; 25929 return (0); 25930 } 25931 25932 static int 25933 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 25934 boolean_t bind_to_req_port_only) 25935 { 25936 tcp_t *tcp = connp->conn_tcp; 25937 sin_t *sin; 25938 sin6_t *sin6; 25939 in_port_t requested_port; 25940 ipaddr_t v4addr; 25941 in6_addr_t v6addr; 25942 uint_t origipversion; 25943 int error = 0; 25944 25945 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 25946 25947 if (tcp->tcp_state == TCPS_BOUND) { 25948 return (0); 25949 } else if (tcp->tcp_state > TCPS_BOUND) { 25950 if (tcp->tcp_debug) { 25951 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 25952 "tcp_bind: bad state, %d", tcp->tcp_state); 25953 } 25954 return (-TOUTSTATE); 25955 } 25956 origipversion = tcp->tcp_ipversion; 25957 25958 ASSERT(sa != NULL && len != 0); 25959 25960 if (!OK_32PTR((char *)sa)) { 25961 if (tcp->tcp_debug) { 25962 (void) strlog(TCP_MOD_ID, 0, 1, 25963 SL_ERROR|SL_TRACE, 25964 "tcp_bind: bad address parameter, " 25965 "address %p, len %d", 25966 (void *)sa, len); 25967 } 25968 return (-TPROTO); 25969 } 25970 25971 switch (len) { 25972 case sizeof (sin_t): /* Complete IPv4 address */ 25973 sin = (sin_t *)sa; 25974 /* 25975 * With sockets sockfs will accept bogus sin_family in 25976 * bind() and replace it with the family used in the socket 25977 * call. 25978 */ 25979 if (sin->sin_family != AF_INET || 25980 tcp->tcp_family != AF_INET) { 25981 return (EAFNOSUPPORT); 25982 } 25983 requested_port = ntohs(sin->sin_port); 25984 tcp->tcp_ipversion = IPV4_VERSION; 25985 v4addr = sin->sin_addr.s_addr; 25986 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 25987 break; 25988 25989 case sizeof (sin6_t): /* Complete IPv6 address */ 25990 sin6 = (sin6_t *)sa; 25991 if (sin6->sin6_family != AF_INET6 || 25992 tcp->tcp_family != AF_INET6) { 25993 return (EAFNOSUPPORT); 25994 } 25995 requested_port = ntohs(sin6->sin6_port); 25996 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 25997 IPV4_VERSION : IPV6_VERSION; 25998 v6addr = sin6->sin6_addr; 25999 break; 26000 26001 default: 26002 if (tcp->tcp_debug) { 26003 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26004 "tcp_bind: bad address length, %d", len); 26005 } 26006 return (EAFNOSUPPORT); 26007 /* return (-TBADADDR); */ 26008 } 26009 26010 tcp->tcp_bound_source_v6 = v6addr; 26011 26012 /* Check for change in ipversion */ 26013 if (origipversion != tcp->tcp_ipversion) { 26014 ASSERT(tcp->tcp_family == AF_INET6); 26015 error = tcp->tcp_ipversion == IPV6_VERSION ? 26016 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26017 if (error) { 26018 return (ENOMEM); 26019 } 26020 } 26021 26022 /* 26023 * Initialize family specific fields. Copy of the src addr. 26024 * in tcp_t is needed for the lookup funcs. 26025 */ 26026 if (tcp->tcp_ipversion == IPV6_VERSION) { 26027 tcp->tcp_ip6h->ip6_src = v6addr; 26028 } else { 26029 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26030 } 26031 tcp->tcp_ip_src_v6 = v6addr; 26032 26033 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26034 26035 error = tcp_bind_select_lport(tcp, &requested_port, 26036 bind_to_req_port_only, cr); 26037 26038 return (error); 26039 } 26040 26041 /* 26042 * Return unix error is tli error is TSYSERR, otherwise return a negative 26043 * tli error. 26044 */ 26045 int 26046 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26047 boolean_t bind_to_req_port_only) 26048 { 26049 int error; 26050 tcp_t *tcp = connp->conn_tcp; 26051 26052 if (tcp->tcp_state >= TCPS_BOUND) { 26053 if (tcp->tcp_debug) { 26054 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26055 "tcp_bind: bad state, %d", tcp->tcp_state); 26056 } 26057 return (-TOUTSTATE); 26058 } 26059 26060 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26061 if (error != 0) 26062 return (error); 26063 26064 ASSERT(tcp->tcp_state == TCPS_BOUND); 26065 26066 tcp->tcp_conn_req_max = 0; 26067 26068 if (tcp->tcp_family == AF_INET6) { 26069 ASSERT(tcp->tcp_connp->conn_af_isv6); 26070 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26071 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26072 } else { 26073 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26074 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26075 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26076 } 26077 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26078 } 26079 26080 int 26081 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26082 socklen_t len, cred_t *cr) 26083 { 26084 int error; 26085 conn_t *connp = (conn_t *)proto_handle; 26086 squeue_t *sqp = connp->conn_sqp; 26087 26088 /* All Solaris components should pass a cred for this operation. */ 26089 ASSERT(cr != NULL); 26090 26091 ASSERT(sqp != NULL); 26092 ASSERT(connp->conn_upper_handle != NULL); 26093 26094 error = squeue_synch_enter(sqp, connp, NULL); 26095 if (error != 0) { 26096 /* failed to enter */ 26097 return (ENOSR); 26098 } 26099 26100 /* binding to a NULL address really means unbind */ 26101 if (sa == NULL) { 26102 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26103 error = tcp_do_unbind(connp); 26104 else 26105 error = EINVAL; 26106 } else { 26107 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26108 } 26109 26110 squeue_synch_exit(sqp, connp); 26111 26112 if (error < 0) { 26113 if (error == -TOUTSTATE) 26114 error = EINVAL; 26115 else 26116 error = proto_tlitosyserr(-error); 26117 } 26118 26119 return (error); 26120 } 26121 26122 /* 26123 * If the return value from this function is positive, it's a UNIX error. 26124 * Otherwise, if it's negative, then the absolute value is a TLI error. 26125 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26126 */ 26127 int 26128 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26129 cred_t *cr, pid_t pid) 26130 { 26131 tcp_t *tcp = connp->conn_tcp; 26132 sin_t *sin = (sin_t *)sa; 26133 sin6_t *sin6 = (sin6_t *)sa; 26134 ipaddr_t *dstaddrp; 26135 in_port_t dstport; 26136 uint_t srcid; 26137 int error = 0; 26138 26139 switch (len) { 26140 default: 26141 /* 26142 * Should never happen 26143 */ 26144 return (EINVAL); 26145 26146 case sizeof (sin_t): 26147 sin = (sin_t *)sa; 26148 if (sin->sin_port == 0) { 26149 return (-TBADADDR); 26150 } 26151 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26152 return (EAFNOSUPPORT); 26153 } 26154 break; 26155 26156 case sizeof (sin6_t): 26157 sin6 = (sin6_t *)sa; 26158 if (sin6->sin6_port == 0) { 26159 return (-TBADADDR); 26160 } 26161 break; 26162 } 26163 /* 26164 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26165 * make sure that the template IP header in the tcp structure is an 26166 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26167 * need to this before we call tcp_bindi() so that the port lookup 26168 * code will look for ports in the correct port space (IPv4 and 26169 * IPv6 have separate port spaces). 26170 */ 26171 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26172 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26173 int err = 0; 26174 26175 err = tcp_header_init_ipv4(tcp); 26176 if (err != 0) { 26177 error = ENOMEM; 26178 goto connect_failed; 26179 } 26180 if (tcp->tcp_lport != 0) 26181 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26182 } 26183 26184 switch (tcp->tcp_state) { 26185 case TCPS_LISTEN: 26186 /* 26187 * Listening sockets are not allowed to issue connect(). 26188 */ 26189 if (IPCL_IS_NONSTR(connp)) 26190 return (EOPNOTSUPP); 26191 /* FALLTHRU */ 26192 case TCPS_IDLE: 26193 /* 26194 * We support quick connect, refer to comments in 26195 * tcp_connect_*() 26196 */ 26197 /* FALLTHRU */ 26198 case TCPS_BOUND: 26199 /* 26200 * We must bump the generation before the operation start. 26201 * This is done to ensure that any upcall made later on sends 26202 * up the right generation to the socket. 26203 */ 26204 SOCK_CONNID_BUMP(tcp->tcp_connid); 26205 26206 if (tcp->tcp_family == AF_INET6) { 26207 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26208 return (tcp_connect_ipv6(tcp, 26209 &sin6->sin6_addr, 26210 sin6->sin6_port, sin6->sin6_flowinfo, 26211 sin6->__sin6_src_id, sin6->sin6_scope_id, 26212 cr, pid)); 26213 } 26214 /* 26215 * Destination adress is mapped IPv6 address. 26216 * Source bound address should be unspecified or 26217 * IPv6 mapped address as well. 26218 */ 26219 if (!IN6_IS_ADDR_UNSPECIFIED( 26220 &tcp->tcp_bound_source_v6) && 26221 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26222 return (EADDRNOTAVAIL); 26223 } 26224 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26225 dstport = sin6->sin6_port; 26226 srcid = sin6->__sin6_src_id; 26227 } else { 26228 dstaddrp = &sin->sin_addr.s_addr; 26229 dstport = sin->sin_port; 26230 srcid = 0; 26231 } 26232 26233 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26234 pid); 26235 break; 26236 default: 26237 return (-TOUTSTATE); 26238 } 26239 /* 26240 * Note: Code below is the "failure" case 26241 */ 26242 connect_failed: 26243 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26244 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26245 return (error); 26246 } 26247 26248 int 26249 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26250 socklen_t len, sock_connid_t *id, cred_t *cr) 26251 { 26252 conn_t *connp = (conn_t *)proto_handle; 26253 tcp_t *tcp = connp->conn_tcp; 26254 squeue_t *sqp = connp->conn_sqp; 26255 int error; 26256 26257 ASSERT(connp->conn_upper_handle != NULL); 26258 26259 /* All Solaris components should pass a cred for this operation. */ 26260 ASSERT(cr != NULL); 26261 26262 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26263 if (error != 0) { 26264 return (error); 26265 } 26266 26267 error = squeue_synch_enter(sqp, connp, NULL); 26268 if (error != 0) { 26269 /* failed to enter */ 26270 return (ENOSR); 26271 } 26272 26273 /* 26274 * TCP supports quick connect, so no need to do an implicit bind 26275 */ 26276 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26277 if (error == 0) { 26278 *id = connp->conn_tcp->tcp_connid; 26279 } else if (error < 0) { 26280 if (error == -TOUTSTATE) { 26281 switch (connp->conn_tcp->tcp_state) { 26282 case TCPS_SYN_SENT: 26283 error = EALREADY; 26284 break; 26285 case TCPS_ESTABLISHED: 26286 error = EISCONN; 26287 break; 26288 case TCPS_LISTEN: 26289 error = EOPNOTSUPP; 26290 break; 26291 default: 26292 error = EINVAL; 26293 break; 26294 } 26295 } else { 26296 error = proto_tlitosyserr(-error); 26297 } 26298 } 26299 done: 26300 squeue_synch_exit(sqp, connp); 26301 26302 return ((error == 0) ? EINPROGRESS : error); 26303 } 26304 26305 /* ARGSUSED */ 26306 sock_lower_handle_t 26307 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26308 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26309 { 26310 conn_t *connp; 26311 boolean_t isv6 = family == AF_INET6; 26312 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26313 (proto != 0 && proto != IPPROTO_TCP)) { 26314 *errorp = EPROTONOSUPPORT; 26315 return (NULL); 26316 } 26317 26318 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26319 if (connp == NULL) { 26320 return (NULL); 26321 } 26322 26323 /* 26324 * Put the ref for TCP. Ref for IP was already put 26325 * by ipcl_conn_create. Also Make the conn_t globally 26326 * visible to walkers 26327 */ 26328 mutex_enter(&connp->conn_lock); 26329 CONN_INC_REF_LOCKED(connp); 26330 ASSERT(connp->conn_ref == 2); 26331 connp->conn_state_flags &= ~CONN_INCIPIENT; 26332 26333 connp->conn_flags |= IPCL_NONSTR; 26334 mutex_exit(&connp->conn_lock); 26335 26336 ASSERT(errorp != NULL); 26337 *errorp = 0; 26338 *sock_downcalls = &sock_tcp_downcalls; 26339 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26340 SM_SENDFILESUPP; 26341 26342 return ((sock_lower_handle_t)connp); 26343 } 26344 26345 /* ARGSUSED */ 26346 void 26347 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26348 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26349 { 26350 conn_t *connp = (conn_t *)proto_handle; 26351 struct sock_proto_props sopp; 26352 26353 ASSERT(connp->conn_upper_handle == NULL); 26354 26355 /* All Solaris components should pass a cred for this operation. */ 26356 ASSERT(cr != NULL); 26357 26358 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26359 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26360 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26361 26362 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26363 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26364 sopp.sopp_maxpsz = INFPSZ; 26365 sopp.sopp_maxblk = INFPSZ; 26366 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26367 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26368 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26369 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26370 tcp_rinfo.mi_minpsz; 26371 26372 connp->conn_upcalls = sock_upcalls; 26373 connp->conn_upper_handle = sock_handle; 26374 26375 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26376 } 26377 26378 /* ARGSUSED */ 26379 int 26380 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26381 { 26382 conn_t *connp = (conn_t *)proto_handle; 26383 26384 ASSERT(connp->conn_upper_handle != NULL); 26385 26386 /* All Solaris components should pass a cred for this operation. */ 26387 ASSERT(cr != NULL); 26388 26389 tcp_close_common(connp, flags); 26390 26391 ip_free_helper_stream(connp); 26392 26393 /* 26394 * Drop IP's reference on the conn. This is the last reference 26395 * on the connp if the state was less than established. If the 26396 * connection has gone into timewait state, then we will have 26397 * one ref for the TCP and one more ref (total of two) for the 26398 * classifier connected hash list (a timewait connections stays 26399 * in connected hash till closed). 26400 * 26401 * We can't assert the references because there might be other 26402 * transient reference places because of some walkers or queued 26403 * packets in squeue for the timewait state. 26404 */ 26405 CONN_DEC_REF(connp); 26406 return (0); 26407 } 26408 26409 /* ARGSUSED */ 26410 int 26411 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26412 cred_t *cr) 26413 { 26414 tcp_t *tcp; 26415 uint32_t msize; 26416 conn_t *connp = (conn_t *)proto_handle; 26417 int32_t tcpstate; 26418 26419 /* All Solaris components should pass a cred for this operation. */ 26420 ASSERT(cr != NULL); 26421 26422 ASSERT(connp->conn_ref >= 2); 26423 ASSERT(connp->conn_upper_handle != NULL); 26424 26425 if (msg->msg_controllen != 0) { 26426 return (EOPNOTSUPP); 26427 26428 } 26429 switch (DB_TYPE(mp)) { 26430 case M_DATA: 26431 tcp = connp->conn_tcp; 26432 ASSERT(tcp != NULL); 26433 26434 tcpstate = tcp->tcp_state; 26435 if (tcpstate < TCPS_ESTABLISHED) { 26436 freemsg(mp); 26437 return (ENOTCONN); 26438 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26439 freemsg(mp); 26440 return (EPIPE); 26441 } 26442 26443 msize = msgdsize(mp); 26444 26445 mutex_enter(&tcp->tcp_non_sq_lock); 26446 tcp->tcp_squeue_bytes += msize; 26447 /* 26448 * Squeue Flow Control 26449 */ 26450 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26451 tcp_setqfull(tcp); 26452 } 26453 mutex_exit(&tcp->tcp_non_sq_lock); 26454 26455 /* 26456 * The application may pass in an address in the msghdr, but 26457 * we ignore the address on connection-oriented sockets. 26458 * Just like BSD this code does not generate an error for 26459 * TCP (a CONNREQUIRED socket) when sending to an address 26460 * passed in with sendto/sendmsg. Instead the data is 26461 * delivered on the connection as if no address had been 26462 * supplied. 26463 */ 26464 CONN_INC_REF(connp); 26465 26466 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26467 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26468 tcp_output_urgent, connp, tcp_squeue_flag, 26469 SQTAG_TCP_OUTPUT); 26470 } else { 26471 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26472 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26473 } 26474 26475 return (0); 26476 26477 default: 26478 ASSERT(0); 26479 } 26480 26481 freemsg(mp); 26482 return (0); 26483 } 26484 26485 /* ARGSUSED */ 26486 void 26487 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26488 { 26489 int len; 26490 uint32_t msize; 26491 conn_t *connp = (conn_t *)arg; 26492 tcp_t *tcp = connp->conn_tcp; 26493 26494 msize = msgdsize(mp); 26495 26496 len = msize - 1; 26497 if (len < 0) { 26498 freemsg(mp); 26499 return; 26500 } 26501 26502 /* 26503 * Try to force urgent data out on the wire. 26504 * Even if we have unsent data this will 26505 * at least send the urgent flag. 26506 * XXX does not handle more flag correctly. 26507 */ 26508 len += tcp->tcp_unsent; 26509 len += tcp->tcp_snxt; 26510 tcp->tcp_urg = len; 26511 tcp->tcp_valid_bits |= TCP_URG_VALID; 26512 26513 /* Bypass tcp protocol for fused tcp loopback */ 26514 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26515 return; 26516 tcp_wput_data(tcp, mp, B_TRUE); 26517 } 26518 26519 /* ARGSUSED */ 26520 int 26521 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26522 socklen_t *addrlenp, cred_t *cr) 26523 { 26524 conn_t *connp = (conn_t *)proto_handle; 26525 tcp_t *tcp = connp->conn_tcp; 26526 26527 ASSERT(connp->conn_upper_handle != NULL); 26528 /* All Solaris components should pass a cred for this operation. */ 26529 ASSERT(cr != NULL); 26530 26531 ASSERT(tcp != NULL); 26532 26533 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26534 } 26535 26536 /* ARGSUSED */ 26537 int 26538 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26539 socklen_t *addrlenp, cred_t *cr) 26540 { 26541 conn_t *connp = (conn_t *)proto_handle; 26542 tcp_t *tcp = connp->conn_tcp; 26543 26544 /* All Solaris components should pass a cred for this operation. */ 26545 ASSERT(cr != NULL); 26546 26547 ASSERT(connp->conn_upper_handle != NULL); 26548 26549 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26550 } 26551 26552 /* 26553 * tcp_fallback 26554 * 26555 * A direct socket is falling back to using STREAMS. The queue 26556 * that is being passed down was created using tcp_open() with 26557 * the SO_FALLBACK flag set. As a result, the queue is not 26558 * associated with a conn, and the q_ptrs instead contain the 26559 * dev and minor area that should be used. 26560 * 26561 * The 'direct_sockfs' flag indicates whether the FireEngine 26562 * optimizations should be used. The common case would be that 26563 * optimizations are enabled, and they might be subsequently 26564 * disabled using the _SIOCSOCKFALLBACK ioctl. 26565 */ 26566 26567 /* 26568 * An active connection is falling back to TPI. Gather all the information 26569 * required by the STREAM head and TPI sonode and send it up. 26570 */ 26571 void 26572 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26573 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26574 { 26575 conn_t *connp = tcp->tcp_connp; 26576 struct stroptions *stropt; 26577 struct T_capability_ack tca; 26578 struct sockaddr_in6 laddr, faddr; 26579 socklen_t laddrlen, faddrlen; 26580 short opts; 26581 int error; 26582 mblk_t *mp; 26583 26584 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26585 connp->conn_minor_arena = WR(q)->q_ptr; 26586 26587 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26588 26589 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26590 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26591 26592 WR(q)->q_qinfo = &tcp_sock_winit; 26593 26594 if (!direct_sockfs) 26595 tcp_disable_direct_sockfs(tcp); 26596 26597 /* 26598 * free the helper stream 26599 */ 26600 ip_free_helper_stream(connp); 26601 26602 /* 26603 * Notify the STREAM head about options 26604 */ 26605 DB_TYPE(stropt_mp) = M_SETOPTS; 26606 stropt = (struct stroptions *)stropt_mp->b_rptr; 26607 stropt_mp->b_wptr += sizeof (struct stroptions); 26608 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 26609 26610 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26611 tcp->tcp_tcps->tcps_wroff_xtra); 26612 if (tcp->tcp_snd_sack_ok) 26613 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26614 stropt->so_hiwat = tcp->tcp_fused ? 26615 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26616 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26617 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26618 26619 putnext(RD(q), stropt_mp); 26620 26621 /* 26622 * Collect the information needed to sync with the sonode 26623 */ 26624 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26625 26626 laddrlen = faddrlen = sizeof (sin6_t); 26627 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26628 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26629 if (error != 0) 26630 faddrlen = 0; 26631 26632 opts = 0; 26633 if (tcp->tcp_oobinline) 26634 opts |= SO_OOBINLINE; 26635 if (tcp->tcp_dontroute) 26636 opts |= SO_DONTROUTE; 26637 26638 /* 26639 * Notify the socket that the protocol is now quiescent, 26640 * and it's therefore safe move data from the socket 26641 * to the stream head. 26642 */ 26643 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26644 (struct sockaddr *)&laddr, laddrlen, 26645 (struct sockaddr *)&faddr, faddrlen, opts); 26646 26647 while ((mp = tcp->tcp_rcv_list) != NULL) { 26648 tcp->tcp_rcv_list = mp->b_next; 26649 mp->b_next = NULL; 26650 putnext(q, mp); 26651 } 26652 tcp->tcp_rcv_last_head = NULL; 26653 tcp->tcp_rcv_last_tail = NULL; 26654 tcp->tcp_rcv_cnt = 0; 26655 } 26656 26657 /* 26658 * An eager is falling back to TPI. All we have to do is send 26659 * up a T_CONN_IND. 26660 */ 26661 void 26662 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26663 { 26664 tcp_t *listener = eager->tcp_listener; 26665 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26666 26667 ASSERT(listener != NULL); 26668 ASSERT(mp != NULL); 26669 26670 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26671 26672 /* 26673 * TLI/XTI applications will get confused by 26674 * sending eager as an option since it violates 26675 * the option semantics. So remove the eager as 26676 * option since TLI/XTI app doesn't need it anyway. 26677 */ 26678 if (!direct_sockfs) { 26679 struct T_conn_ind *conn_ind; 26680 26681 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26682 conn_ind->OPT_length = 0; 26683 conn_ind->OPT_offset = 0; 26684 } 26685 26686 /* 26687 * Sockfs guarantees that the listener will not be closed 26688 * during fallback. So we can safely use the listener's queue. 26689 */ 26690 putnext(listener->tcp_rq, mp); 26691 } 26692 26693 int 26694 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26695 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26696 { 26697 tcp_t *tcp; 26698 conn_t *connp = (conn_t *)proto_handle; 26699 int error; 26700 mblk_t *stropt_mp; 26701 mblk_t *ordrel_mp; 26702 mblk_t *fused_sigurp_mp; 26703 26704 tcp = connp->conn_tcp; 26705 26706 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26707 NULL); 26708 26709 /* Pre-allocate the T_ordrel_ind mblk. */ 26710 ASSERT(tcp->tcp_ordrel_mp == NULL); 26711 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26712 STR_NOSIG, NULL); 26713 ordrel_mp->b_datap->db_type = M_PROTO; 26714 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26715 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26716 26717 /* Pre-allocate the M_PCSIG used by fusion */ 26718 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 26719 26720 /* 26721 * Enter the squeue so that no new packets can come in 26722 */ 26723 error = squeue_synch_enter(connp->conn_sqp, connp, NULL); 26724 if (error != 0) { 26725 /* failed to enter, free all the pre-allocated messages. */ 26726 freeb(stropt_mp); 26727 freeb(ordrel_mp); 26728 freeb(fused_sigurp_mp); 26729 /* 26730 * We cannot process the eager, so at least send out a 26731 * RST so the peer can reconnect. 26732 */ 26733 if (tcp->tcp_listener != NULL) { 26734 (void) tcp_eager_blowoff(tcp->tcp_listener, 26735 tcp->tcp_conn_req_seqnum); 26736 } 26737 return (ENOMEM); 26738 } 26739 26740 /* 26741 * No longer a direct socket 26742 */ 26743 connp->conn_flags &= ~IPCL_NONSTR; 26744 26745 tcp->tcp_ordrel_mp = ordrel_mp; 26746 26747 if (tcp->tcp_fused) { 26748 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 26749 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 26750 } else { 26751 freeb(fused_sigurp_mp); 26752 } 26753 26754 if (tcp->tcp_listener != NULL) { 26755 /* The eager will deal with opts when accept() is called */ 26756 freeb(stropt_mp); 26757 tcp_fallback_eager(tcp, direct_sockfs); 26758 } else { 26759 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26760 quiesced_cb); 26761 } 26762 26763 /* 26764 * There should be atleast two ref's (IP + TCP) 26765 */ 26766 ASSERT(connp->conn_ref >= 2); 26767 squeue_synch_exit(connp->conn_sqp, connp); 26768 26769 return (0); 26770 } 26771 26772 /* ARGSUSED */ 26773 static void 26774 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26775 { 26776 conn_t *connp = (conn_t *)arg; 26777 tcp_t *tcp = connp->conn_tcp; 26778 26779 freemsg(mp); 26780 26781 if (tcp->tcp_fused) 26782 tcp_unfuse(tcp); 26783 26784 if (tcp_xmit_end(tcp) != 0) { 26785 /* 26786 * We were crossing FINs and got a reset from 26787 * the other side. Just ignore it. 26788 */ 26789 if (tcp->tcp_debug) { 26790 (void) strlog(TCP_MOD_ID, 0, 1, 26791 SL_ERROR|SL_TRACE, 26792 "tcp_shutdown_output() out of state %s", 26793 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 26794 } 26795 } 26796 } 26797 26798 /* ARGSUSED */ 26799 int 26800 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 26801 { 26802 conn_t *connp = (conn_t *)proto_handle; 26803 tcp_t *tcp = connp->conn_tcp; 26804 26805 ASSERT(connp->conn_upper_handle != NULL); 26806 26807 /* All Solaris components should pass a cred for this operation. */ 26808 ASSERT(cr != NULL); 26809 26810 /* 26811 * X/Open requires that we check the connected state. 26812 */ 26813 if (tcp->tcp_state < TCPS_SYN_SENT) 26814 return (ENOTCONN); 26815 26816 /* shutdown the send side */ 26817 if (how != SHUT_RD) { 26818 mblk_t *bp; 26819 26820 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 26821 CONN_INC_REF(connp); 26822 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 26823 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 26824 26825 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26826 SOCK_OPCTL_SHUT_SEND, 0); 26827 } 26828 26829 /* shutdown the recv side */ 26830 if (how != SHUT_WR) 26831 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26832 SOCK_OPCTL_SHUT_RECV, 0); 26833 26834 return (0); 26835 } 26836 26837 /* 26838 * SOP_LISTEN() calls into tcp_listen(). 26839 */ 26840 /* ARGSUSED */ 26841 int 26842 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 26843 { 26844 conn_t *connp = (conn_t *)proto_handle; 26845 int error; 26846 squeue_t *sqp = connp->conn_sqp; 26847 26848 ASSERT(connp->conn_upper_handle != NULL); 26849 26850 /* All Solaris components should pass a cred for this operation. */ 26851 ASSERT(cr != NULL); 26852 26853 error = squeue_synch_enter(sqp, connp, NULL); 26854 if (error != 0) { 26855 /* failed to enter */ 26856 return (ENOBUFS); 26857 } 26858 26859 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 26860 if (error == 0) { 26861 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26862 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 26863 } else if (error < 0) { 26864 if (error == -TOUTSTATE) 26865 error = EINVAL; 26866 else 26867 error = proto_tlitosyserr(-error); 26868 } 26869 squeue_synch_exit(sqp, connp); 26870 return (error); 26871 } 26872 26873 static int 26874 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 26875 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 26876 { 26877 tcp_t *tcp = connp->conn_tcp; 26878 int error = 0; 26879 tcp_stack_t *tcps = tcp->tcp_tcps; 26880 26881 /* All Solaris components should pass a cred for this operation. */ 26882 ASSERT(cr != NULL); 26883 26884 if (tcp->tcp_state >= TCPS_BOUND) { 26885 if ((tcp->tcp_state == TCPS_BOUND || 26886 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 26887 /* 26888 * Handle listen() increasing backlog. 26889 * This is more "liberal" then what the TPI spec 26890 * requires but is needed to avoid a t_unbind 26891 * when handling listen() since the port number 26892 * might be "stolen" between the unbind and bind. 26893 */ 26894 goto do_listen; 26895 } 26896 if (tcp->tcp_debug) { 26897 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26898 "tcp_listen: bad state, %d", tcp->tcp_state); 26899 } 26900 return (-TOUTSTATE); 26901 } else { 26902 if (sa == NULL) { 26903 sin6_t addr; 26904 sin_t *sin; 26905 sin6_t *sin6; 26906 26907 ASSERT(IPCL_IS_NONSTR(connp)); 26908 26909 /* Do an implicit bind: Request for a generic port. */ 26910 if (tcp->tcp_family == AF_INET) { 26911 len = sizeof (sin_t); 26912 sin = (sin_t *)&addr; 26913 *sin = sin_null; 26914 sin->sin_family = AF_INET; 26915 tcp->tcp_ipversion = IPV4_VERSION; 26916 } else { 26917 ASSERT(tcp->tcp_family == AF_INET6); 26918 len = sizeof (sin6_t); 26919 sin6 = (sin6_t *)&addr; 26920 *sin6 = sin6_null; 26921 sin6->sin6_family = AF_INET6; 26922 tcp->tcp_ipversion = IPV6_VERSION; 26923 } 26924 sa = (struct sockaddr *)&addr; 26925 } 26926 26927 error = tcp_bind_check(connp, sa, len, cr, 26928 bind_to_req_port_only); 26929 if (error) 26930 return (error); 26931 /* Fall through and do the fanout insertion */ 26932 } 26933 26934 do_listen: 26935 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 26936 tcp->tcp_conn_req_max = backlog; 26937 if (tcp->tcp_conn_req_max) { 26938 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 26939 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 26940 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 26941 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 26942 /* 26943 * If this is a listener, do not reset the eager list 26944 * and other stuffs. Note that we don't check if the 26945 * existing eager list meets the new tcp_conn_req_max 26946 * requirement. 26947 */ 26948 if (tcp->tcp_state != TCPS_LISTEN) { 26949 tcp->tcp_state = TCPS_LISTEN; 26950 /* Initialize the chain. Don't need the eager_lock */ 26951 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 26952 tcp->tcp_eager_next_drop_q0 = tcp; 26953 tcp->tcp_eager_prev_drop_q0 = tcp; 26954 tcp->tcp_second_ctimer_threshold = 26955 tcps->tcps_ip_abort_linterval; 26956 } 26957 } 26958 26959 /* 26960 * We can call ip_bind directly, the processing continues 26961 * in tcp_post_ip_bind(). 26962 * 26963 * We need to make sure that the conn_recv is set to a non-null 26964 * value before we insert the conn into the classifier table. 26965 * This is to avoid a race with an incoming packet which does an 26966 * ipcl_classify(). 26967 */ 26968 connp->conn_recv = tcp_conn_request; 26969 if (tcp->tcp_family == AF_INET) { 26970 error = ip_proto_bind_laddr_v4(connp, NULL, 26971 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 26972 } else { 26973 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26974 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 26975 } 26976 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26977 } 26978 26979 void 26980 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 26981 { 26982 conn_t *connp = (conn_t *)proto_handle; 26983 tcp_t *tcp = connp->conn_tcp; 26984 mblk_t *mp; 26985 int error; 26986 26987 ASSERT(connp->conn_upper_handle != NULL); 26988 26989 /* 26990 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_clr_flowctrl() 26991 * is currently running. 26992 */ 26993 mutex_enter(&tcp->tcp_rsrv_mp_lock); 26994 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 26995 mutex_exit(&tcp->tcp_rsrv_mp_lock); 26996 return; 26997 } 26998 tcp->tcp_rsrv_mp = NULL; 26999 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27000 27001 error = squeue_synch_enter(connp->conn_sqp, connp, mp); 27002 ASSERT(error == 0); 27003 27004 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27005 tcp->tcp_rsrv_mp = mp; 27006 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27007 27008 if (tcp->tcp_fused) { 27009 tcp_fuse_backenable(tcp); 27010 } else { 27011 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27012 /* 27013 * Send back a window update immediately if TCP is above 27014 * ESTABLISHED state and the increase of the rcv window 27015 * that the other side knows is at least 1 MSS after flow 27016 * control is lifted. 27017 */ 27018 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27019 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 27020 tcp_xmit_ctl(NULL, tcp, 27021 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27022 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27023 } 27024 } 27025 27026 squeue_synch_exit(connp->conn_sqp, connp); 27027 } 27028 27029 /* ARGSUSED */ 27030 int 27031 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27032 int mode, int32_t *rvalp, cred_t *cr) 27033 { 27034 conn_t *connp = (conn_t *)proto_handle; 27035 int error; 27036 27037 ASSERT(connp->conn_upper_handle != NULL); 27038 27039 /* All Solaris components should pass a cred for this operation. */ 27040 ASSERT(cr != NULL); 27041 27042 switch (cmd) { 27043 case ND_SET: 27044 case ND_GET: 27045 case TCP_IOC_DEFAULT_Q: 27046 case _SIOCSOCKFALLBACK: 27047 case TCP_IOC_ABORT_CONN: 27048 case TI_GETPEERNAME: 27049 case TI_GETMYNAME: 27050 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27051 cmd)); 27052 error = EINVAL; 27053 break; 27054 default: 27055 /* 27056 * Pass on to IP using helper stream 27057 */ 27058 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27059 cmd, arg, mode, cr, rvalp); 27060 break; 27061 } 27062 return (error); 27063 } 27064 27065 sock_downcalls_t sock_tcp_downcalls = { 27066 tcp_activate, 27067 tcp_accept, 27068 tcp_bind, 27069 tcp_listen, 27070 tcp_connect, 27071 tcp_getpeername, 27072 tcp_getsockname, 27073 tcp_getsockopt, 27074 tcp_setsockopt, 27075 tcp_sendmsg, 27076 NULL, 27077 NULL, 27078 NULL, 27079 tcp_shutdown, 27080 tcp_clr_flowctrl, 27081 tcp_ioctl, 27082 tcp_close, 27083 }; 27084