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 /* 9055 * Set the xmit_lowater to at least twice of MSS. 9056 */ 9057 if ((mss << 1) > tcp->tcp_xmit_lowater) 9058 tcp->tcp_xmit_lowater = mss << 1; 9059 9060 if (do_ss) { 9061 /* 9062 * Either the tcp_cwnd is as yet uninitialized, or mss is 9063 * changing due to a reduction in MTU, presumably as a 9064 * result of a new path component, reset cwnd to its 9065 * "initial" value, as a multiple of the new mss. 9066 */ 9067 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_initial); 9068 } else { 9069 /* 9070 * Called by tcp_paws_check(), the mss increased 9071 * marginally to allow use of space previously taken 9072 * by the timestamp option. It would be inappropriate 9073 * to apply slow start or tcp_init_cwnd values to 9074 * tcp_cwnd, simply adjust to a multiple of the new mss. 9075 */ 9076 tcp->tcp_cwnd = (tcp->tcp_cwnd / tcp->tcp_mss) * mss; 9077 tcp->tcp_cwnd_cnt = 0; 9078 } 9079 tcp->tcp_mss = mss; 9080 (void) tcp_maxpsz_set(tcp, B_TRUE); 9081 } 9082 9083 /* For /dev/tcp aka AF_INET open */ 9084 static int 9085 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9086 { 9087 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE)); 9088 } 9089 9090 /* For /dev/tcp6 aka AF_INET6 open */ 9091 static int 9092 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9093 { 9094 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE)); 9095 } 9096 9097 static conn_t * 9098 tcp_create_common(queue_t *q, cred_t *credp, boolean_t isv6, 9099 boolean_t issocket, int *errorp) 9100 { 9101 tcp_t *tcp = NULL; 9102 conn_t *connp; 9103 int err; 9104 zoneid_t zoneid; 9105 tcp_stack_t *tcps; 9106 squeue_t *sqp; 9107 9108 ASSERT(errorp != NULL); 9109 /* 9110 * Find the proper zoneid and netstack. 9111 */ 9112 /* 9113 * Special case for install: miniroot needs to be able to 9114 * access files via NFS as though it were always in the 9115 * global zone. 9116 */ 9117 if (credp == kcred && nfs_global_client_only != 0) { 9118 zoneid = GLOBAL_ZONEID; 9119 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)-> 9120 netstack_tcp; 9121 ASSERT(tcps != NULL); 9122 } else { 9123 netstack_t *ns; 9124 9125 ns = netstack_find_by_cred(credp); 9126 ASSERT(ns != NULL); 9127 tcps = ns->netstack_tcp; 9128 ASSERT(tcps != NULL); 9129 9130 /* 9131 * For exclusive stacks we set the zoneid to zero 9132 * to make TCP operate as if in the global zone. 9133 */ 9134 if (tcps->tcps_netstack->netstack_stackid != 9135 GLOBAL_NETSTACKID) 9136 zoneid = GLOBAL_ZONEID; 9137 else 9138 zoneid = crgetzoneid(credp); 9139 } 9140 /* 9141 * For stackid zero this is done from strplumb.c, but 9142 * non-zero stackids are handled here. 9143 */ 9144 if (tcps->tcps_g_q == NULL && 9145 tcps->tcps_netstack->netstack_stackid != 9146 GLOBAL_NETSTACKID) { 9147 tcp_g_q_setup(tcps); 9148 } 9149 9150 sqp = IP_SQUEUE_GET((uint_t)gethrtime()); 9151 connp = (conn_t *)tcp_get_conn(sqp, tcps); 9152 /* 9153 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, 9154 * so we drop it by one. 9155 */ 9156 netstack_rele(tcps->tcps_netstack); 9157 if (connp == NULL) { 9158 *errorp = ENOSR; 9159 return (NULL); 9160 } 9161 connp->conn_sqp = sqp; 9162 connp->conn_initial_sqp = connp->conn_sqp; 9163 tcp = connp->conn_tcp; 9164 9165 if (isv6) { 9166 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9167 connp->conn_send = ip_output_v6; 9168 connp->conn_af_isv6 = B_TRUE; 9169 connp->conn_pkt_isv6 = B_TRUE; 9170 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9171 tcp->tcp_ipversion = IPV6_VERSION; 9172 tcp->tcp_family = AF_INET6; 9173 tcp->tcp_mss = tcps->tcps_mss_def_ipv6; 9174 } else { 9175 connp->conn_flags |= IPCL_TCP4; 9176 connp->conn_send = ip_output; 9177 connp->conn_af_isv6 = B_FALSE; 9178 connp->conn_pkt_isv6 = B_FALSE; 9179 tcp->tcp_ipversion = IPV4_VERSION; 9180 tcp->tcp_family = AF_INET; 9181 tcp->tcp_mss = tcps->tcps_mss_def_ipv4; 9182 } 9183 9184 /* 9185 * TCP keeps a copy of cred for cache locality reasons but 9186 * we put a reference only once. If connp->conn_cred 9187 * becomes invalid, tcp_cred should also be set to NULL. 9188 */ 9189 tcp->tcp_cred = connp->conn_cred = credp; 9190 crhold(connp->conn_cred); 9191 tcp->tcp_cpid = curproc->p_pid; 9192 tcp->tcp_open_time = lbolt64; 9193 connp->conn_zoneid = zoneid; 9194 connp->conn_mlp_type = mlptSingle; 9195 connp->conn_ulp_labeled = !is_system_labeled(); 9196 ASSERT(connp->conn_netstack == tcps->tcps_netstack); 9197 ASSERT(tcp->tcp_tcps == tcps); 9198 9199 /* 9200 * If the caller has the process-wide flag set, then default to MAC 9201 * exempt mode. This allows read-down to unlabeled hosts. 9202 */ 9203 if (getpflags(NET_MAC_AWARE, credp) != 0) 9204 connp->conn_mac_exempt = B_TRUE; 9205 9206 connp->conn_dev = NULL; 9207 if (issocket) { 9208 connp->conn_flags |= IPCL_SOCKET; 9209 tcp->tcp_issocket = 1; 9210 } 9211 9212 tcp->tcp_recv_hiwater = tcps->tcps_recv_hiwat; 9213 tcp->tcp_rwnd = tcps->tcps_recv_hiwat; 9214 tcp->tcp_recv_lowater = tcp_rinfo.mi_lowat; 9215 9216 /* Non-zero default values */ 9217 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9218 9219 if (q == NULL) { 9220 /* 9221 * Create a helper stream for non-STREAMS socket. 9222 */ 9223 err = ip_create_helper_stream(connp, tcps->tcps_ldi_ident); 9224 if (err != 0) { 9225 ip1dbg(("tcp_create_common: create of IP helper stream " 9226 "failed\n")); 9227 CONN_DEC_REF(connp); 9228 *errorp = err; 9229 return (NULL); 9230 } 9231 q = connp->conn_rq; 9232 } else { 9233 RD(q)->q_hiwat = tcps->tcps_recv_hiwat; 9234 } 9235 9236 SOCK_CONNID_INIT(tcp->tcp_connid); 9237 err = tcp_init(tcp, q); 9238 if (err != 0) { 9239 CONN_DEC_REF(connp); 9240 *errorp = err; 9241 return (NULL); 9242 } 9243 9244 return (connp); 9245 } 9246 9247 static int 9248 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp, 9249 boolean_t isv6) 9250 { 9251 tcp_t *tcp = NULL; 9252 conn_t *connp = NULL; 9253 int err; 9254 vmem_t *minor_arena = NULL; 9255 dev_t conn_dev; 9256 boolean_t issocket; 9257 9258 if (q->q_ptr != NULL) 9259 return (0); 9260 9261 if (sflag == MODOPEN) 9262 return (EINVAL); 9263 9264 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) && 9265 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) { 9266 minor_arena = ip_minor_arena_la; 9267 } else { 9268 /* 9269 * Either minor numbers in the large arena were exhausted 9270 * or a non socket application is doing the open. 9271 * Try to allocate from the small arena. 9272 */ 9273 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) { 9274 return (EBUSY); 9275 } 9276 minor_arena = ip_minor_arena_sa; 9277 } 9278 9279 ASSERT(minor_arena != NULL); 9280 9281 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev); 9282 9283 if (flag & SO_FALLBACK) { 9284 /* 9285 * Non streams socket needs a stream to fallback to 9286 */ 9287 RD(q)->q_ptr = (void *)conn_dev; 9288 WR(q)->q_qinfo = &tcp_fallback_sock_winit; 9289 WR(q)->q_ptr = (void *)minor_arena; 9290 qprocson(q); 9291 return (0); 9292 } else if (flag & SO_ACCEPTOR) { 9293 q->q_qinfo = &tcp_acceptor_rinit; 9294 /* 9295 * the conn_dev and minor_arena will be subsequently used by 9296 * tcp_wput_accept() and tcp_tpi_close_accept() to figure out 9297 * the minor device number for this connection from the q_ptr. 9298 */ 9299 RD(q)->q_ptr = (void *)conn_dev; 9300 WR(q)->q_qinfo = &tcp_acceptor_winit; 9301 WR(q)->q_ptr = (void *)minor_arena; 9302 qprocson(q); 9303 return (0); 9304 } 9305 9306 issocket = flag & SO_SOCKSTR; 9307 connp = tcp_create_common(q, credp, isv6, issocket, &err); 9308 9309 if (connp == NULL) { 9310 inet_minor_free(minor_arena, conn_dev); 9311 q->q_ptr = WR(q)->q_ptr = NULL; 9312 return (err); 9313 } 9314 9315 q->q_ptr = WR(q)->q_ptr = connp; 9316 9317 connp->conn_dev = conn_dev; 9318 connp->conn_minor_arena = minor_arena; 9319 9320 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6); 9321 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9322 9323 tcp = connp->conn_tcp; 9324 9325 if (issocket) { 9326 WR(q)->q_qinfo = &tcp_sock_winit; 9327 } else { 9328 #ifdef _ILP32 9329 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9330 #else 9331 tcp->tcp_acceptor_id = conn_dev; 9332 #endif /* _ILP32 */ 9333 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9334 } 9335 9336 /* 9337 * Put the ref for TCP. Ref for IP was already put 9338 * by ipcl_conn_create. Also Make the conn_t globally 9339 * visible to walkers 9340 */ 9341 mutex_enter(&connp->conn_lock); 9342 CONN_INC_REF_LOCKED(connp); 9343 ASSERT(connp->conn_ref == 2); 9344 connp->conn_state_flags &= ~CONN_INCIPIENT; 9345 mutex_exit(&connp->conn_lock); 9346 9347 qprocson(q); 9348 return (0); 9349 } 9350 9351 /* 9352 * Some TCP options can be "set" by requesting them in the option 9353 * buffer. This is needed for XTI feature test though we do not 9354 * allow it in general. We interpret that this mechanism is more 9355 * applicable to OSI protocols and need not be allowed in general. 9356 * This routine filters out options for which it is not allowed (most) 9357 * and lets through those (few) for which it is. [ The XTI interface 9358 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9359 * ever implemented will have to be allowed here ]. 9360 */ 9361 static boolean_t 9362 tcp_allow_connopt_set(int level, int name) 9363 { 9364 9365 switch (level) { 9366 case IPPROTO_TCP: 9367 switch (name) { 9368 case TCP_NODELAY: 9369 return (B_TRUE); 9370 default: 9371 return (B_FALSE); 9372 } 9373 /*NOTREACHED*/ 9374 default: 9375 return (B_FALSE); 9376 } 9377 /*NOTREACHED*/ 9378 } 9379 9380 /* 9381 * this routine gets default values of certain options whose default 9382 * values are maintained by protocol specific code 9383 */ 9384 /* ARGSUSED */ 9385 int 9386 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9387 { 9388 int32_t *i1 = (int32_t *)ptr; 9389 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 9390 9391 switch (level) { 9392 case IPPROTO_TCP: 9393 switch (name) { 9394 case TCP_NOTIFY_THRESHOLD: 9395 *i1 = tcps->tcps_ip_notify_interval; 9396 break; 9397 case TCP_ABORT_THRESHOLD: 9398 *i1 = tcps->tcps_ip_abort_interval; 9399 break; 9400 case TCP_CONN_NOTIFY_THRESHOLD: 9401 *i1 = tcps->tcps_ip_notify_cinterval; 9402 break; 9403 case TCP_CONN_ABORT_THRESHOLD: 9404 *i1 = tcps->tcps_ip_abort_cinterval; 9405 break; 9406 default: 9407 return (-1); 9408 } 9409 break; 9410 case IPPROTO_IP: 9411 switch (name) { 9412 case IP_TTL: 9413 *i1 = tcps->tcps_ipv4_ttl; 9414 break; 9415 default: 9416 return (-1); 9417 } 9418 break; 9419 case IPPROTO_IPV6: 9420 switch (name) { 9421 case IPV6_UNICAST_HOPS: 9422 *i1 = tcps->tcps_ipv6_hoplimit; 9423 break; 9424 default: 9425 return (-1); 9426 } 9427 break; 9428 default: 9429 return (-1); 9430 } 9431 return (sizeof (int)); 9432 } 9433 9434 static int 9435 tcp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr) 9436 { 9437 int *i1 = (int *)ptr; 9438 tcp_t *tcp = connp->conn_tcp; 9439 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9440 9441 switch (level) { 9442 case SOL_SOCKET: 9443 switch (name) { 9444 case SO_LINGER: { 9445 struct linger *lgr = (struct linger *)ptr; 9446 9447 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9448 lgr->l_linger = tcp->tcp_lingertime; 9449 } 9450 return (sizeof (struct linger)); 9451 case SO_DEBUG: 9452 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9453 break; 9454 case SO_KEEPALIVE: 9455 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9456 break; 9457 case SO_DONTROUTE: 9458 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9459 break; 9460 case SO_USELOOPBACK: 9461 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9462 break; 9463 case SO_BROADCAST: 9464 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9465 break; 9466 case SO_REUSEADDR: 9467 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9468 break; 9469 case SO_OOBINLINE: 9470 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9471 break; 9472 case SO_DGRAM_ERRIND: 9473 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9474 break; 9475 case SO_TYPE: 9476 *i1 = SOCK_STREAM; 9477 break; 9478 case SO_SNDBUF: 9479 *i1 = tcp->tcp_xmit_hiwater; 9480 break; 9481 case SO_RCVBUF: 9482 *i1 = tcp->tcp_recv_hiwater; 9483 break; 9484 case SO_SND_COPYAVOID: 9485 *i1 = tcp->tcp_snd_zcopy_on ? 9486 SO_SND_COPYAVOID : 0; 9487 break; 9488 case SO_ALLZONES: 9489 *i1 = connp->conn_allzones ? 1 : 0; 9490 break; 9491 case SO_ANON_MLP: 9492 *i1 = connp->conn_anon_mlp; 9493 break; 9494 case SO_MAC_EXEMPT: 9495 *i1 = connp->conn_mac_exempt; 9496 break; 9497 case SO_EXCLBIND: 9498 *i1 = tcp->tcp_exclbind ? SO_EXCLBIND : 0; 9499 break; 9500 case SO_PROTOTYPE: 9501 *i1 = IPPROTO_TCP; 9502 break; 9503 case SO_DOMAIN: 9504 *i1 = tcp->tcp_family; 9505 break; 9506 case SO_ACCEPTCONN: 9507 *i1 = (tcp->tcp_state == TCPS_LISTEN); 9508 default: 9509 return (-1); 9510 } 9511 break; 9512 case IPPROTO_TCP: 9513 switch (name) { 9514 case TCP_NODELAY: 9515 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9516 break; 9517 case TCP_MAXSEG: 9518 *i1 = tcp->tcp_mss; 9519 break; 9520 case TCP_NOTIFY_THRESHOLD: 9521 *i1 = (int)tcp->tcp_first_timer_threshold; 9522 break; 9523 case TCP_ABORT_THRESHOLD: 9524 *i1 = tcp->tcp_second_timer_threshold; 9525 break; 9526 case TCP_CONN_NOTIFY_THRESHOLD: 9527 *i1 = tcp->tcp_first_ctimer_threshold; 9528 break; 9529 case TCP_CONN_ABORT_THRESHOLD: 9530 *i1 = tcp->tcp_second_ctimer_threshold; 9531 break; 9532 case TCP_RECVDSTADDR: 9533 *i1 = tcp->tcp_recvdstaddr; 9534 break; 9535 case TCP_ANONPRIVBIND: 9536 *i1 = tcp->tcp_anon_priv_bind; 9537 break; 9538 case TCP_EXCLBIND: 9539 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9540 break; 9541 case TCP_INIT_CWND: 9542 *i1 = tcp->tcp_init_cwnd; 9543 break; 9544 case TCP_KEEPALIVE_THRESHOLD: 9545 *i1 = tcp->tcp_ka_interval; 9546 break; 9547 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9548 *i1 = tcp->tcp_ka_abort_thres; 9549 break; 9550 case TCP_CORK: 9551 *i1 = tcp->tcp_cork; 9552 break; 9553 default: 9554 return (-1); 9555 } 9556 break; 9557 case IPPROTO_IP: 9558 if (tcp->tcp_family != AF_INET) 9559 return (-1); 9560 switch (name) { 9561 case IP_OPTIONS: 9562 case T_IP_OPTIONS: { 9563 /* 9564 * This is compatible with BSD in that in only return 9565 * the reverse source route with the final destination 9566 * as the last entry. The first 4 bytes of the option 9567 * will contain the final destination. 9568 */ 9569 int opt_len; 9570 9571 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9572 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9573 ASSERT(opt_len >= 0); 9574 /* Caller ensures enough space */ 9575 if (opt_len > 0) { 9576 /* 9577 * TODO: Do we have to handle getsockopt on an 9578 * initiator as well? 9579 */ 9580 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9581 } 9582 return (0); 9583 } 9584 case IP_TOS: 9585 case T_IP_TOS: 9586 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9587 break; 9588 case IP_TTL: 9589 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9590 break; 9591 case IP_NEXTHOP: 9592 /* Handled at IP level */ 9593 return (-EINVAL); 9594 default: 9595 return (-1); 9596 } 9597 break; 9598 case IPPROTO_IPV6: 9599 /* 9600 * IPPROTO_IPV6 options are only supported for sockets 9601 * that are using IPv6 on the wire. 9602 */ 9603 if (tcp->tcp_ipversion != IPV6_VERSION) { 9604 return (-1); 9605 } 9606 switch (name) { 9607 case IPV6_UNICAST_HOPS: 9608 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9609 break; /* goto sizeof (int) option return */ 9610 case IPV6_BOUND_IF: 9611 /* Zero if not set */ 9612 *i1 = tcp->tcp_bound_if; 9613 break; /* goto sizeof (int) option return */ 9614 case IPV6_RECVPKTINFO: 9615 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9616 *i1 = 1; 9617 else 9618 *i1 = 0; 9619 break; /* goto sizeof (int) option return */ 9620 case IPV6_RECVTCLASS: 9621 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9622 *i1 = 1; 9623 else 9624 *i1 = 0; 9625 break; /* goto sizeof (int) option return */ 9626 case IPV6_RECVHOPLIMIT: 9627 if (tcp->tcp_ipv6_recvancillary & 9628 TCP_IPV6_RECVHOPLIMIT) 9629 *i1 = 1; 9630 else 9631 *i1 = 0; 9632 break; /* goto sizeof (int) option return */ 9633 case IPV6_RECVHOPOPTS: 9634 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9635 *i1 = 1; 9636 else 9637 *i1 = 0; 9638 break; /* goto sizeof (int) option return */ 9639 case IPV6_RECVDSTOPTS: 9640 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9641 *i1 = 1; 9642 else 9643 *i1 = 0; 9644 break; /* goto sizeof (int) option return */ 9645 case _OLD_IPV6_RECVDSTOPTS: 9646 if (tcp->tcp_ipv6_recvancillary & 9647 TCP_OLD_IPV6_RECVDSTOPTS) 9648 *i1 = 1; 9649 else 9650 *i1 = 0; 9651 break; /* goto sizeof (int) option return */ 9652 case IPV6_RECVRTHDR: 9653 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9654 *i1 = 1; 9655 else 9656 *i1 = 0; 9657 break; /* goto sizeof (int) option return */ 9658 case IPV6_RECVRTHDRDSTOPTS: 9659 if (tcp->tcp_ipv6_recvancillary & 9660 TCP_IPV6_RECVRTDSTOPTS) 9661 *i1 = 1; 9662 else 9663 *i1 = 0; 9664 break; /* goto sizeof (int) option return */ 9665 case IPV6_PKTINFO: { 9666 /* XXX assumes that caller has room for max size! */ 9667 struct in6_pktinfo *pkti; 9668 9669 pkti = (struct in6_pktinfo *)ptr; 9670 if (ipp->ipp_fields & IPPF_IFINDEX) 9671 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9672 else 9673 pkti->ipi6_ifindex = 0; 9674 if (ipp->ipp_fields & IPPF_ADDR) 9675 pkti->ipi6_addr = ipp->ipp_addr; 9676 else 9677 pkti->ipi6_addr = ipv6_all_zeros; 9678 return (sizeof (struct in6_pktinfo)); 9679 } 9680 case IPV6_TCLASS: 9681 if (ipp->ipp_fields & IPPF_TCLASS) 9682 *i1 = ipp->ipp_tclass; 9683 else 9684 *i1 = IPV6_FLOW_TCLASS( 9685 IPV6_DEFAULT_VERS_AND_FLOW); 9686 break; /* goto sizeof (int) option return */ 9687 case IPV6_NEXTHOP: { 9688 sin6_t *sin6 = (sin6_t *)ptr; 9689 9690 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9691 return (0); 9692 *sin6 = sin6_null; 9693 sin6->sin6_family = AF_INET6; 9694 sin6->sin6_addr = ipp->ipp_nexthop; 9695 return (sizeof (sin6_t)); 9696 } 9697 case IPV6_HOPOPTS: 9698 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9699 return (0); 9700 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9701 return (0); 9702 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9703 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9704 if (tcp->tcp_label_len > 0) { 9705 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9706 ptr[1] = (ipp->ipp_hopoptslen - 9707 tcp->tcp_label_len + 7) / 8 - 1; 9708 } 9709 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9710 case IPV6_RTHDRDSTOPTS: 9711 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9712 return (0); 9713 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9714 return (ipp->ipp_rtdstoptslen); 9715 case IPV6_RTHDR: 9716 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9717 return (0); 9718 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9719 return (ipp->ipp_rthdrlen); 9720 case IPV6_DSTOPTS: 9721 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9722 return (0); 9723 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9724 return (ipp->ipp_dstoptslen); 9725 case IPV6_SRC_PREFERENCES: 9726 return (ip6_get_src_preferences(connp, 9727 (uint32_t *)ptr)); 9728 case IPV6_PATHMTU: { 9729 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9730 9731 if (tcp->tcp_state < TCPS_ESTABLISHED) 9732 return (-1); 9733 9734 return (ip_fill_mtuinfo(&connp->conn_remv6, 9735 connp->conn_fport, mtuinfo, 9736 connp->conn_netstack)); 9737 } 9738 default: 9739 return (-1); 9740 } 9741 break; 9742 default: 9743 return (-1); 9744 } 9745 return (sizeof (int)); 9746 } 9747 9748 /* 9749 * TCP routine to get the values of options. 9750 */ 9751 int 9752 tcp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9753 { 9754 return (tcp_opt_get(Q_TO_CONN(q), level, name, ptr)); 9755 } 9756 9757 /* returns UNIX error, the optlen is a value-result arg */ 9758 int 9759 tcp_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 9760 void *optvalp, socklen_t *optlen, cred_t *cr) 9761 { 9762 conn_t *connp = (conn_t *)proto_handle; 9763 squeue_t *sqp = connp->conn_sqp; 9764 int error; 9765 t_uscalar_t max_optbuf_len; 9766 void *optvalp_buf; 9767 int len; 9768 9769 ASSERT(connp->conn_upper_handle != NULL); 9770 9771 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len, 9772 tcp_opt_obj.odb_opt_des_arr, 9773 tcp_opt_obj.odb_opt_arr_cnt, 9774 tcp_opt_obj.odb_topmost_tpiprovider, 9775 B_FALSE, B_TRUE, cr); 9776 if (error != 0) { 9777 if (error < 0) { 9778 error = proto_tlitosyserr(-error); 9779 } 9780 return (error); 9781 } 9782 9783 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP); 9784 9785 error = squeue_synch_enter(sqp, connp, NULL); 9786 if (error == ENOMEM) { 9787 return (ENOMEM); 9788 } 9789 9790 len = tcp_opt_get(connp, level, option_name, optvalp_buf); 9791 squeue_synch_exit(sqp, connp); 9792 9793 if (len < 0) { 9794 /* 9795 * Pass on to IP 9796 */ 9797 kmem_free(optvalp_buf, max_optbuf_len); 9798 return (ip_get_options(connp, level, option_name, 9799 optvalp, optlen, cr)); 9800 } else { 9801 /* 9802 * update optlen and copy option value 9803 */ 9804 t_uscalar_t size = MIN(len, *optlen); 9805 bcopy(optvalp_buf, optvalp, size); 9806 bcopy(&size, optlen, sizeof (size)); 9807 9808 kmem_free(optvalp_buf, max_optbuf_len); 9809 return (0); 9810 } 9811 } 9812 9813 /* 9814 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9815 * Parameters are assumed to be verified by the caller. 9816 */ 9817 /* ARGSUSED */ 9818 int 9819 tcp_opt_set(conn_t *connp, uint_t optset_context, int level, int name, 9820 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9821 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9822 { 9823 tcp_t *tcp = connp->conn_tcp; 9824 int *i1 = (int *)invalp; 9825 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9826 boolean_t checkonly; 9827 int reterr; 9828 tcp_stack_t *tcps = tcp->tcp_tcps; 9829 9830 switch (optset_context) { 9831 case SETFN_OPTCOM_CHECKONLY: 9832 checkonly = B_TRUE; 9833 /* 9834 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9835 * inlen != 0 implies value supplied and 9836 * we have to "pretend" to set it. 9837 * inlen == 0 implies that there is no 9838 * value part in T_CHECK request and just validation 9839 * done elsewhere should be enough, we just return here. 9840 */ 9841 if (inlen == 0) { 9842 *outlenp = 0; 9843 return (0); 9844 } 9845 break; 9846 case SETFN_OPTCOM_NEGOTIATE: 9847 checkonly = B_FALSE; 9848 break; 9849 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9850 case SETFN_CONN_NEGOTIATE: 9851 checkonly = B_FALSE; 9852 /* 9853 * Negotiating local and "association-related" options 9854 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9855 * primitives is allowed by XTI, but we choose 9856 * to not implement this style negotiation for Internet 9857 * protocols (We interpret it is a must for OSI world but 9858 * optional for Internet protocols) for all options. 9859 * [ Will do only for the few options that enable test 9860 * suites that our XTI implementation of this feature 9861 * works for transports that do allow it ] 9862 */ 9863 if (!tcp_allow_connopt_set(level, name)) { 9864 *outlenp = 0; 9865 return (EINVAL); 9866 } 9867 break; 9868 default: 9869 /* 9870 * We should never get here 9871 */ 9872 *outlenp = 0; 9873 return (EINVAL); 9874 } 9875 9876 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9877 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9878 9879 /* 9880 * For TCP, we should have no ancillary data sent down 9881 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9882 * has to be zero. 9883 */ 9884 ASSERT(thisdg_attrs == NULL); 9885 9886 /* 9887 * For fixed length options, no sanity check 9888 * of passed in length is done. It is assumed *_optcom_req() 9889 * routines do the right thing. 9890 */ 9891 switch (level) { 9892 case SOL_SOCKET: 9893 switch (name) { 9894 case SO_LINGER: { 9895 struct linger *lgr = (struct linger *)invalp; 9896 9897 if (!checkonly) { 9898 if (lgr->l_onoff) { 9899 tcp->tcp_linger = 1; 9900 tcp->tcp_lingertime = lgr->l_linger; 9901 } else { 9902 tcp->tcp_linger = 0; 9903 tcp->tcp_lingertime = 0; 9904 } 9905 /* struct copy */ 9906 *(struct linger *)outvalp = *lgr; 9907 } else { 9908 if (!lgr->l_onoff) { 9909 ((struct linger *) 9910 outvalp)->l_onoff = 0; 9911 ((struct linger *) 9912 outvalp)->l_linger = 0; 9913 } else { 9914 /* struct copy */ 9915 *(struct linger *)outvalp = *lgr; 9916 } 9917 } 9918 *outlenp = sizeof (struct linger); 9919 return (0); 9920 } 9921 case SO_DEBUG: 9922 if (!checkonly) 9923 tcp->tcp_debug = onoff; 9924 break; 9925 case SO_KEEPALIVE: 9926 if (checkonly) { 9927 /* check only case */ 9928 break; 9929 } 9930 9931 if (!onoff) { 9932 if (tcp->tcp_ka_enabled) { 9933 if (tcp->tcp_ka_tid != 0) { 9934 (void) TCP_TIMER_CANCEL(tcp, 9935 tcp->tcp_ka_tid); 9936 tcp->tcp_ka_tid = 0; 9937 } 9938 tcp->tcp_ka_enabled = 0; 9939 } 9940 break; 9941 } 9942 if (!tcp->tcp_ka_enabled) { 9943 /* Crank up the keepalive timer */ 9944 tcp->tcp_ka_last_intrvl = 0; 9945 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9946 tcp_keepalive_killer, 9947 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9948 tcp->tcp_ka_enabled = 1; 9949 } 9950 break; 9951 case SO_DONTROUTE: 9952 /* 9953 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 9954 * only of interest to IP. We track them here only so 9955 * that we can report their current value. 9956 */ 9957 if (!checkonly) { 9958 tcp->tcp_dontroute = onoff; 9959 tcp->tcp_connp->conn_dontroute = onoff; 9960 } 9961 break; 9962 case SO_USELOOPBACK: 9963 if (!checkonly) { 9964 tcp->tcp_useloopback = onoff; 9965 tcp->tcp_connp->conn_loopback = onoff; 9966 } 9967 break; 9968 case SO_BROADCAST: 9969 if (!checkonly) { 9970 tcp->tcp_broadcast = onoff; 9971 tcp->tcp_connp->conn_broadcast = onoff; 9972 } 9973 break; 9974 case SO_REUSEADDR: 9975 if (!checkonly) { 9976 tcp->tcp_reuseaddr = onoff; 9977 tcp->tcp_connp->conn_reuseaddr = onoff; 9978 } 9979 break; 9980 case SO_OOBINLINE: 9981 if (!checkonly) { 9982 tcp->tcp_oobinline = onoff; 9983 if (IPCL_IS_NONSTR(tcp->tcp_connp)) 9984 proto_set_rx_oob_opt(connp, onoff); 9985 } 9986 break; 9987 case SO_DGRAM_ERRIND: 9988 if (!checkonly) 9989 tcp->tcp_dgram_errind = onoff; 9990 break; 9991 case SO_SNDBUF: { 9992 if (*i1 > tcps->tcps_max_buf) { 9993 *outlenp = 0; 9994 return (ENOBUFS); 9995 } 9996 if (checkonly) 9997 break; 9998 9999 tcp->tcp_xmit_hiwater = *i1; 10000 if (tcps->tcps_snd_lowat_fraction != 0) 10001 tcp->tcp_xmit_lowater = 10002 tcp->tcp_xmit_hiwater / 10003 tcps->tcps_snd_lowat_fraction; 10004 (void) tcp_maxpsz_set(tcp, B_TRUE); 10005 /* 10006 * If we are flow-controlled, recheck the condition. 10007 * There are apps that increase SO_SNDBUF size when 10008 * flow-controlled (EWOULDBLOCK), and expect the flow 10009 * control condition to be lifted right away. 10010 */ 10011 mutex_enter(&tcp->tcp_non_sq_lock); 10012 if (tcp->tcp_flow_stopped && 10013 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10014 tcp_clrqfull(tcp); 10015 } 10016 mutex_exit(&tcp->tcp_non_sq_lock); 10017 break; 10018 } 10019 case SO_RCVBUF: 10020 if (*i1 > tcps->tcps_max_buf) { 10021 *outlenp = 0; 10022 return (ENOBUFS); 10023 } 10024 /* Silently ignore zero */ 10025 if (!checkonly && *i1 != 0) { 10026 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10027 (void) tcp_rwnd_set(tcp, *i1); 10028 } 10029 /* 10030 * XXX should we return the rwnd here 10031 * and tcp_opt_get ? 10032 */ 10033 break; 10034 case SO_SND_COPYAVOID: 10035 if (!checkonly) { 10036 /* we only allow enable at most once for now */ 10037 if (tcp->tcp_loopback || 10038 (tcp->tcp_kssl_ctx != NULL) || 10039 (!tcp->tcp_snd_zcopy_aware && 10040 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10041 *outlenp = 0; 10042 return (EOPNOTSUPP); 10043 } 10044 tcp->tcp_snd_zcopy_aware = 1; 10045 } 10046 break; 10047 case SO_RCVTIMEO: 10048 case SO_SNDTIMEO: 10049 /* 10050 * Pass these two options in order for third part 10051 * protocol usage. Here just return directly. 10052 */ 10053 return (0); 10054 case SO_ALLZONES: 10055 /* Pass option along to IP level for handling */ 10056 return (-EINVAL); 10057 case SO_ANON_MLP: 10058 /* Pass option along to IP level for handling */ 10059 return (-EINVAL); 10060 case SO_MAC_EXEMPT: 10061 /* Pass option along to IP level for handling */ 10062 return (-EINVAL); 10063 case SO_EXCLBIND: 10064 if (!checkonly) 10065 tcp->tcp_exclbind = onoff; 10066 break; 10067 default: 10068 *outlenp = 0; 10069 return (EINVAL); 10070 } 10071 break; 10072 case IPPROTO_TCP: 10073 switch (name) { 10074 case TCP_NODELAY: 10075 if (!checkonly) 10076 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10077 break; 10078 case TCP_NOTIFY_THRESHOLD: 10079 if (!checkonly) 10080 tcp->tcp_first_timer_threshold = *i1; 10081 break; 10082 case TCP_ABORT_THRESHOLD: 10083 if (!checkonly) 10084 tcp->tcp_second_timer_threshold = *i1; 10085 break; 10086 case TCP_CONN_NOTIFY_THRESHOLD: 10087 if (!checkonly) 10088 tcp->tcp_first_ctimer_threshold = *i1; 10089 break; 10090 case TCP_CONN_ABORT_THRESHOLD: 10091 if (!checkonly) 10092 tcp->tcp_second_ctimer_threshold = *i1; 10093 break; 10094 case TCP_RECVDSTADDR: 10095 if (tcp->tcp_state > TCPS_LISTEN) 10096 return (EOPNOTSUPP); 10097 if (!checkonly) 10098 tcp->tcp_recvdstaddr = onoff; 10099 break; 10100 case TCP_ANONPRIVBIND: 10101 if ((reterr = secpolicy_net_privaddr(cr, 0, 10102 IPPROTO_TCP)) != 0) { 10103 *outlenp = 0; 10104 return (reterr); 10105 } 10106 if (!checkonly) { 10107 tcp->tcp_anon_priv_bind = onoff; 10108 } 10109 break; 10110 case TCP_EXCLBIND: 10111 if (!checkonly) 10112 tcp->tcp_exclbind = onoff; 10113 break; /* goto sizeof (int) option return */ 10114 case TCP_INIT_CWND: { 10115 uint32_t init_cwnd = *((uint32_t *)invalp); 10116 10117 if (checkonly) 10118 break; 10119 10120 /* 10121 * Only allow socket with network configuration 10122 * privilege to set the initial cwnd to be larger 10123 * than allowed by RFC 3390. 10124 */ 10125 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10126 tcp->tcp_init_cwnd = init_cwnd; 10127 break; 10128 } 10129 if ((reterr = secpolicy_ip_config(cr, B_TRUE)) != 0) { 10130 *outlenp = 0; 10131 return (reterr); 10132 } 10133 if (init_cwnd > TCP_MAX_INIT_CWND) { 10134 *outlenp = 0; 10135 return (EINVAL); 10136 } 10137 tcp->tcp_init_cwnd = init_cwnd; 10138 break; 10139 } 10140 case TCP_KEEPALIVE_THRESHOLD: 10141 if (checkonly) 10142 break; 10143 10144 if (*i1 < tcps->tcps_keepalive_interval_low || 10145 *i1 > tcps->tcps_keepalive_interval_high) { 10146 *outlenp = 0; 10147 return (EINVAL); 10148 } 10149 if (*i1 != tcp->tcp_ka_interval) { 10150 tcp->tcp_ka_interval = *i1; 10151 /* 10152 * Check if we need to restart the 10153 * keepalive timer. 10154 */ 10155 if (tcp->tcp_ka_tid != 0) { 10156 ASSERT(tcp->tcp_ka_enabled); 10157 (void) TCP_TIMER_CANCEL(tcp, 10158 tcp->tcp_ka_tid); 10159 tcp->tcp_ka_last_intrvl = 0; 10160 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10161 tcp_keepalive_killer, 10162 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10163 } 10164 } 10165 break; 10166 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10167 if (!checkonly) { 10168 if (*i1 < 10169 tcps->tcps_keepalive_abort_interval_low || 10170 *i1 > 10171 tcps->tcps_keepalive_abort_interval_high) { 10172 *outlenp = 0; 10173 return (EINVAL); 10174 } 10175 tcp->tcp_ka_abort_thres = *i1; 10176 } 10177 break; 10178 case TCP_CORK: 10179 if (!checkonly) { 10180 /* 10181 * if tcp->tcp_cork was set and is now 10182 * being unset, we have to make sure that 10183 * the remaining data gets sent out. Also 10184 * unset tcp->tcp_cork so that tcp_wput_data() 10185 * can send data even if it is less than mss 10186 */ 10187 if (tcp->tcp_cork && onoff == 0 && 10188 tcp->tcp_unsent > 0) { 10189 tcp->tcp_cork = B_FALSE; 10190 tcp_wput_data(tcp, NULL, B_FALSE); 10191 } 10192 tcp->tcp_cork = onoff; 10193 } 10194 break; 10195 default: 10196 *outlenp = 0; 10197 return (EINVAL); 10198 } 10199 break; 10200 case IPPROTO_IP: 10201 if (tcp->tcp_family != AF_INET) { 10202 *outlenp = 0; 10203 return (ENOPROTOOPT); 10204 } 10205 switch (name) { 10206 case IP_OPTIONS: 10207 case T_IP_OPTIONS: 10208 reterr = tcp_opt_set_header(tcp, checkonly, 10209 invalp, inlen); 10210 if (reterr) { 10211 *outlenp = 0; 10212 return (reterr); 10213 } 10214 /* OK return - copy input buffer into output buffer */ 10215 if (invalp != outvalp) { 10216 /* don't trust bcopy for identical src/dst */ 10217 bcopy(invalp, outvalp, inlen); 10218 } 10219 *outlenp = inlen; 10220 return (0); 10221 case IP_TOS: 10222 case T_IP_TOS: 10223 if (!checkonly) { 10224 tcp->tcp_ipha->ipha_type_of_service = 10225 (uchar_t)*i1; 10226 tcp->tcp_tos = (uchar_t)*i1; 10227 } 10228 break; 10229 case IP_TTL: 10230 if (!checkonly) { 10231 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10232 tcp->tcp_ttl = (uchar_t)*i1; 10233 } 10234 break; 10235 case IP_BOUND_IF: 10236 case IP_NEXTHOP: 10237 /* Handled at the IP level */ 10238 return (-EINVAL); 10239 case IP_SEC_OPT: 10240 /* 10241 * We should not allow policy setting after 10242 * we start listening for connections. 10243 */ 10244 if (tcp->tcp_state == TCPS_LISTEN) { 10245 return (EINVAL); 10246 } else { 10247 /* Handled at the IP level */ 10248 return (-EINVAL); 10249 } 10250 default: 10251 *outlenp = 0; 10252 return (EINVAL); 10253 } 10254 break; 10255 case IPPROTO_IPV6: { 10256 ip6_pkt_t *ipp; 10257 10258 /* 10259 * IPPROTO_IPV6 options are only supported for sockets 10260 * that are using IPv6 on the wire. 10261 */ 10262 if (tcp->tcp_ipversion != IPV6_VERSION) { 10263 *outlenp = 0; 10264 return (ENOPROTOOPT); 10265 } 10266 /* 10267 * Only sticky options; no ancillary data 10268 */ 10269 ipp = &tcp->tcp_sticky_ipp; 10270 10271 switch (name) { 10272 case IPV6_UNICAST_HOPS: 10273 /* -1 means use default */ 10274 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10275 *outlenp = 0; 10276 return (EINVAL); 10277 } 10278 if (!checkonly) { 10279 if (*i1 == -1) { 10280 tcp->tcp_ip6h->ip6_hops = 10281 ipp->ipp_unicast_hops = 10282 (uint8_t)tcps->tcps_ipv6_hoplimit; 10283 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10284 /* Pass modified value to IP. */ 10285 *i1 = tcp->tcp_ip6h->ip6_hops; 10286 } else { 10287 tcp->tcp_ip6h->ip6_hops = 10288 ipp->ipp_unicast_hops = 10289 (uint8_t)*i1; 10290 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10291 } 10292 reterr = tcp_build_hdrs(tcp); 10293 if (reterr != 0) 10294 return (reterr); 10295 } 10296 break; 10297 case IPV6_BOUND_IF: 10298 if (!checkonly) { 10299 tcp->tcp_bound_if = *i1; 10300 PASS_OPT_TO_IP(connp); 10301 } 10302 break; 10303 /* 10304 * Set boolean switches for ancillary data delivery 10305 */ 10306 case IPV6_RECVPKTINFO: 10307 if (!checkonly) { 10308 if (onoff) 10309 tcp->tcp_ipv6_recvancillary |= 10310 TCP_IPV6_RECVPKTINFO; 10311 else 10312 tcp->tcp_ipv6_recvancillary &= 10313 ~TCP_IPV6_RECVPKTINFO; 10314 /* Force it to be sent up with the next msg */ 10315 tcp->tcp_recvifindex = 0; 10316 PASS_OPT_TO_IP(connp); 10317 } 10318 break; 10319 case IPV6_RECVTCLASS: 10320 if (!checkonly) { 10321 if (onoff) 10322 tcp->tcp_ipv6_recvancillary |= 10323 TCP_IPV6_RECVTCLASS; 10324 else 10325 tcp->tcp_ipv6_recvancillary &= 10326 ~TCP_IPV6_RECVTCLASS; 10327 PASS_OPT_TO_IP(connp); 10328 } 10329 break; 10330 case IPV6_RECVHOPLIMIT: 10331 if (!checkonly) { 10332 if (onoff) 10333 tcp->tcp_ipv6_recvancillary |= 10334 TCP_IPV6_RECVHOPLIMIT; 10335 else 10336 tcp->tcp_ipv6_recvancillary &= 10337 ~TCP_IPV6_RECVHOPLIMIT; 10338 /* Force it to be sent up with the next msg */ 10339 tcp->tcp_recvhops = 0xffffffffU; 10340 PASS_OPT_TO_IP(connp); 10341 } 10342 break; 10343 case IPV6_RECVHOPOPTS: 10344 if (!checkonly) { 10345 if (onoff) 10346 tcp->tcp_ipv6_recvancillary |= 10347 TCP_IPV6_RECVHOPOPTS; 10348 else 10349 tcp->tcp_ipv6_recvancillary &= 10350 ~TCP_IPV6_RECVHOPOPTS; 10351 PASS_OPT_TO_IP(connp); 10352 } 10353 break; 10354 case IPV6_RECVDSTOPTS: 10355 if (!checkonly) { 10356 if (onoff) 10357 tcp->tcp_ipv6_recvancillary |= 10358 TCP_IPV6_RECVDSTOPTS; 10359 else 10360 tcp->tcp_ipv6_recvancillary &= 10361 ~TCP_IPV6_RECVDSTOPTS; 10362 PASS_OPT_TO_IP(connp); 10363 } 10364 break; 10365 case _OLD_IPV6_RECVDSTOPTS: 10366 if (!checkonly) { 10367 if (onoff) 10368 tcp->tcp_ipv6_recvancillary |= 10369 TCP_OLD_IPV6_RECVDSTOPTS; 10370 else 10371 tcp->tcp_ipv6_recvancillary &= 10372 ~TCP_OLD_IPV6_RECVDSTOPTS; 10373 } 10374 break; 10375 case IPV6_RECVRTHDR: 10376 if (!checkonly) { 10377 if (onoff) 10378 tcp->tcp_ipv6_recvancillary |= 10379 TCP_IPV6_RECVRTHDR; 10380 else 10381 tcp->tcp_ipv6_recvancillary &= 10382 ~TCP_IPV6_RECVRTHDR; 10383 PASS_OPT_TO_IP(connp); 10384 } 10385 break; 10386 case IPV6_RECVRTHDRDSTOPTS: 10387 if (!checkonly) { 10388 if (onoff) 10389 tcp->tcp_ipv6_recvancillary |= 10390 TCP_IPV6_RECVRTDSTOPTS; 10391 else 10392 tcp->tcp_ipv6_recvancillary &= 10393 ~TCP_IPV6_RECVRTDSTOPTS; 10394 PASS_OPT_TO_IP(connp); 10395 } 10396 break; 10397 case IPV6_PKTINFO: 10398 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10399 return (EINVAL); 10400 if (checkonly) 10401 break; 10402 10403 if (inlen == 0) { 10404 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10405 } else { 10406 struct in6_pktinfo *pkti; 10407 10408 pkti = (struct in6_pktinfo *)invalp; 10409 /* 10410 * RFC 3542 states that ipi6_addr must be 10411 * the unspecified address when setting the 10412 * IPV6_PKTINFO sticky socket option on a 10413 * TCP socket. 10414 */ 10415 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10416 return (EINVAL); 10417 /* 10418 * IP will validate the source address and 10419 * interface index. 10420 */ 10421 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 10422 reterr = ip_set_options(tcp->tcp_connp, 10423 level, name, invalp, inlen, cr); 10424 } else { 10425 reterr = ip6_set_pktinfo(cr, 10426 tcp->tcp_connp, pkti); 10427 } 10428 if (reterr != 0) 10429 return (reterr); 10430 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10431 ipp->ipp_addr = pkti->ipi6_addr; 10432 if (ipp->ipp_ifindex != 0) 10433 ipp->ipp_fields |= IPPF_IFINDEX; 10434 else 10435 ipp->ipp_fields &= ~IPPF_IFINDEX; 10436 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10437 ipp->ipp_fields |= IPPF_ADDR; 10438 else 10439 ipp->ipp_fields &= ~IPPF_ADDR; 10440 } 10441 reterr = tcp_build_hdrs(tcp); 10442 if (reterr != 0) 10443 return (reterr); 10444 break; 10445 case IPV6_TCLASS: 10446 if (inlen != 0 && inlen != sizeof (int)) 10447 return (EINVAL); 10448 if (checkonly) 10449 break; 10450 10451 if (inlen == 0) { 10452 ipp->ipp_fields &= ~IPPF_TCLASS; 10453 } else { 10454 if (*i1 > 255 || *i1 < -1) 10455 return (EINVAL); 10456 if (*i1 == -1) { 10457 ipp->ipp_tclass = 0; 10458 *i1 = 0; 10459 } else { 10460 ipp->ipp_tclass = *i1; 10461 } 10462 ipp->ipp_fields |= IPPF_TCLASS; 10463 } 10464 reterr = tcp_build_hdrs(tcp); 10465 if (reterr != 0) 10466 return (reterr); 10467 break; 10468 case IPV6_NEXTHOP: 10469 /* 10470 * IP will verify that the nexthop is reachable 10471 * and fail for sticky options. 10472 */ 10473 if (inlen != 0 && inlen != sizeof (sin6_t)) 10474 return (EINVAL); 10475 if (checkonly) 10476 break; 10477 10478 if (inlen == 0) { 10479 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10480 } else { 10481 sin6_t *sin6 = (sin6_t *)invalp; 10482 10483 if (sin6->sin6_family != AF_INET6) 10484 return (EAFNOSUPPORT); 10485 if (IN6_IS_ADDR_V4MAPPED( 10486 &sin6->sin6_addr)) 10487 return (EADDRNOTAVAIL); 10488 ipp->ipp_nexthop = sin6->sin6_addr; 10489 if (!IN6_IS_ADDR_UNSPECIFIED( 10490 &ipp->ipp_nexthop)) 10491 ipp->ipp_fields |= IPPF_NEXTHOP; 10492 else 10493 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10494 } 10495 reterr = tcp_build_hdrs(tcp); 10496 if (reterr != 0) 10497 return (reterr); 10498 PASS_OPT_TO_IP(connp); 10499 break; 10500 case IPV6_HOPOPTS: { 10501 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10502 10503 /* 10504 * Sanity checks - minimum size, size a multiple of 10505 * eight bytes, and matching size passed in. 10506 */ 10507 if (inlen != 0 && 10508 inlen != (8 * (hopts->ip6h_len + 1))) 10509 return (EINVAL); 10510 10511 if (checkonly) 10512 break; 10513 10514 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10515 (uchar_t **)&ipp->ipp_hopopts, 10516 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10517 if (reterr != 0) 10518 return (reterr); 10519 if (ipp->ipp_hopoptslen == 0) 10520 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10521 else 10522 ipp->ipp_fields |= IPPF_HOPOPTS; 10523 reterr = tcp_build_hdrs(tcp); 10524 if (reterr != 0) 10525 return (reterr); 10526 break; 10527 } 10528 case IPV6_RTHDRDSTOPTS: { 10529 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10530 10531 /* 10532 * Sanity checks - minimum size, size a multiple of 10533 * eight bytes, and matching size passed in. 10534 */ 10535 if (inlen != 0 && 10536 inlen != (8 * (dopts->ip6d_len + 1))) 10537 return (EINVAL); 10538 10539 if (checkonly) 10540 break; 10541 10542 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10543 (uchar_t **)&ipp->ipp_rtdstopts, 10544 &ipp->ipp_rtdstoptslen, 0); 10545 if (reterr != 0) 10546 return (reterr); 10547 if (ipp->ipp_rtdstoptslen == 0) 10548 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10549 else 10550 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10551 reterr = tcp_build_hdrs(tcp); 10552 if (reterr != 0) 10553 return (reterr); 10554 break; 10555 } 10556 case IPV6_DSTOPTS: { 10557 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10558 10559 /* 10560 * Sanity checks - minimum size, size a multiple of 10561 * eight bytes, and matching size passed in. 10562 */ 10563 if (inlen != 0 && 10564 inlen != (8 * (dopts->ip6d_len + 1))) 10565 return (EINVAL); 10566 10567 if (checkonly) 10568 break; 10569 10570 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10571 (uchar_t **)&ipp->ipp_dstopts, 10572 &ipp->ipp_dstoptslen, 0); 10573 if (reterr != 0) 10574 return (reterr); 10575 if (ipp->ipp_dstoptslen == 0) 10576 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10577 else 10578 ipp->ipp_fields |= IPPF_DSTOPTS; 10579 reterr = tcp_build_hdrs(tcp); 10580 if (reterr != 0) 10581 return (reterr); 10582 break; 10583 } 10584 case IPV6_RTHDR: { 10585 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10586 10587 /* 10588 * Sanity checks - minimum size, size a multiple of 10589 * eight bytes, and matching size passed in. 10590 */ 10591 if (inlen != 0 && 10592 inlen != (8 * (rt->ip6r_len + 1))) 10593 return (EINVAL); 10594 10595 if (checkonly) 10596 break; 10597 10598 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10599 (uchar_t **)&ipp->ipp_rthdr, 10600 &ipp->ipp_rthdrlen, 0); 10601 if (reterr != 0) 10602 return (reterr); 10603 if (ipp->ipp_rthdrlen == 0) 10604 ipp->ipp_fields &= ~IPPF_RTHDR; 10605 else 10606 ipp->ipp_fields |= IPPF_RTHDR; 10607 reterr = tcp_build_hdrs(tcp); 10608 if (reterr != 0) 10609 return (reterr); 10610 break; 10611 } 10612 case IPV6_V6ONLY: 10613 if (!checkonly) { 10614 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10615 } 10616 break; 10617 case IPV6_USE_MIN_MTU: 10618 if (inlen != sizeof (int)) 10619 return (EINVAL); 10620 10621 if (*i1 < -1 || *i1 > 1) 10622 return (EINVAL); 10623 10624 if (checkonly) 10625 break; 10626 10627 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10628 ipp->ipp_use_min_mtu = *i1; 10629 break; 10630 case IPV6_SEC_OPT: 10631 /* 10632 * We should not allow policy setting after 10633 * we start listening for connections. 10634 */ 10635 if (tcp->tcp_state == TCPS_LISTEN) { 10636 return (EINVAL); 10637 } else { 10638 /* Handled at the IP level */ 10639 return (-EINVAL); 10640 } 10641 case IPV6_SRC_PREFERENCES: 10642 if (inlen != sizeof (uint32_t)) 10643 return (EINVAL); 10644 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10645 *(uint32_t *)invalp); 10646 if (reterr != 0) { 10647 *outlenp = 0; 10648 return (reterr); 10649 } 10650 break; 10651 default: 10652 *outlenp = 0; 10653 return (EINVAL); 10654 } 10655 break; 10656 } /* end IPPROTO_IPV6 */ 10657 default: 10658 *outlenp = 0; 10659 return (EINVAL); 10660 } 10661 /* 10662 * Common case of OK return with outval same as inval 10663 */ 10664 if (invalp != outvalp) { 10665 /* don't trust bcopy for identical src/dst */ 10666 (void) bcopy(invalp, outvalp, inlen); 10667 } 10668 *outlenp = inlen; 10669 return (0); 10670 } 10671 10672 /* ARGSUSED */ 10673 int 10674 tcp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name, 10675 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 10676 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 10677 { 10678 conn_t *connp = Q_TO_CONN(q); 10679 10680 return (tcp_opt_set(connp, optset_context, level, name, inlen, invalp, 10681 outlenp, outvalp, thisdg_attrs, cr, mblk)); 10682 } 10683 10684 int 10685 tcp_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name, 10686 const void *optvalp, socklen_t optlen, cred_t *cr) 10687 { 10688 conn_t *connp = (conn_t *)proto_handle; 10689 squeue_t *sqp = connp->conn_sqp; 10690 int error; 10691 10692 ASSERT(connp->conn_upper_handle != NULL); 10693 /* 10694 * Entering the squeue synchronously can result in a context switch, 10695 * which can cause a rather sever performance degradation. So we try to 10696 * handle whatever options we can without entering the squeue. 10697 */ 10698 if (level == IPPROTO_TCP) { 10699 switch (option_name) { 10700 case TCP_NODELAY: 10701 if (optlen != sizeof (int32_t)) 10702 return (EINVAL); 10703 mutex_enter(&connp->conn_tcp->tcp_non_sq_lock); 10704 connp->conn_tcp->tcp_naglim = *(int *)optvalp ? 1 : 10705 connp->conn_tcp->tcp_mss; 10706 mutex_exit(&connp->conn_tcp->tcp_non_sq_lock); 10707 return (0); 10708 default: 10709 break; 10710 } 10711 } 10712 10713 error = squeue_synch_enter(sqp, connp, NULL); 10714 if (error == ENOMEM) { 10715 return (ENOMEM); 10716 } 10717 10718 error = proto_opt_check(level, option_name, optlen, NULL, 10719 tcp_opt_obj.odb_opt_des_arr, 10720 tcp_opt_obj.odb_opt_arr_cnt, 10721 tcp_opt_obj.odb_topmost_tpiprovider, 10722 B_TRUE, B_FALSE, cr); 10723 10724 if (error != 0) { 10725 if (error < 0) { 10726 error = proto_tlitosyserr(-error); 10727 } 10728 squeue_synch_exit(sqp, connp); 10729 return (error); 10730 } 10731 10732 error = tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level, option_name, 10733 optlen, (uchar_t *)optvalp, (uint_t *)&optlen, (uchar_t *)optvalp, 10734 NULL, cr, NULL); 10735 squeue_synch_exit(sqp, connp); 10736 10737 if (error < 0) { 10738 /* 10739 * Pass on to ip 10740 */ 10741 error = ip_set_options(connp, level, option_name, optvalp, 10742 optlen, cr); 10743 } 10744 return (error); 10745 } 10746 10747 /* 10748 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10749 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10750 * headers, and the maximum size tcp header (to avoid reallocation 10751 * on the fly for additional tcp options). 10752 * Returns failure if can't allocate memory. 10753 */ 10754 static int 10755 tcp_build_hdrs(tcp_t *tcp) 10756 { 10757 char *hdrs; 10758 uint_t hdrs_len; 10759 ip6i_t *ip6i; 10760 char buf[TCP_MAX_HDR_LENGTH]; 10761 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10762 in6_addr_t src, dst; 10763 tcp_stack_t *tcps = tcp->tcp_tcps; 10764 conn_t *connp = tcp->tcp_connp; 10765 10766 /* 10767 * save the existing tcp header and source/dest IP addresses 10768 */ 10769 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10770 src = tcp->tcp_ip6h->ip6_src; 10771 dst = tcp->tcp_ip6h->ip6_dst; 10772 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10773 ASSERT(hdrs_len != 0); 10774 if (hdrs_len > tcp->tcp_iphc_len) { 10775 /* Need to reallocate */ 10776 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10777 if (hdrs == NULL) 10778 return (ENOMEM); 10779 if (tcp->tcp_iphc != NULL) { 10780 if (tcp->tcp_hdr_grown) { 10781 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10782 } else { 10783 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10784 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10785 } 10786 tcp->tcp_iphc_len = 0; 10787 } 10788 ASSERT(tcp->tcp_iphc_len == 0); 10789 tcp->tcp_iphc = hdrs; 10790 tcp->tcp_iphc_len = hdrs_len; 10791 tcp->tcp_hdr_grown = B_TRUE; 10792 } 10793 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10794 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10795 10796 /* Set header fields not in ipp */ 10797 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10798 ip6i = (ip6i_t *)tcp->tcp_iphc; 10799 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10800 } else { 10801 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10802 } 10803 /* 10804 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10805 * 10806 * tcp->tcp_tcp_hdr_len doesn't change here. 10807 */ 10808 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10809 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10810 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10811 10812 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10813 10814 tcp->tcp_ip6h->ip6_src = src; 10815 tcp->tcp_ip6h->ip6_dst = dst; 10816 10817 /* 10818 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10819 * the default value for TCP. 10820 */ 10821 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10822 tcp->tcp_ip6h->ip6_hops = tcps->tcps_ipv6_hoplimit; 10823 10824 /* 10825 * If we're setting extension headers after a connection 10826 * has been established, and if we have a routing header 10827 * among the extension headers, call ip_massage_options_v6 to 10828 * manipulate the routing header/ip6_dst set the checksum 10829 * difference in the tcp header template. 10830 * (This happens in tcp_connect_ipv6 if the routing header 10831 * is set prior to the connect.) 10832 * Set the tcp_sum to zero first in case we've cleared a 10833 * routing header or don't have one at all. 10834 */ 10835 tcp->tcp_sum = 0; 10836 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10837 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10838 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10839 (uint8_t *)tcp->tcp_tcph); 10840 if (rth != NULL) { 10841 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10842 rth, tcps->tcps_netstack); 10843 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10844 (tcp->tcp_sum >> 16)); 10845 } 10846 } 10847 10848 /* Try to get everything in a single mblk */ 10849 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 10850 hdrs_len + tcps->tcps_wroff_xtra); 10851 return (0); 10852 } 10853 10854 /* 10855 * Transfer any source route option from ipha to buf/dst in reversed form. 10856 */ 10857 static int 10858 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10859 { 10860 ipoptp_t opts; 10861 uchar_t *opt; 10862 uint8_t optval; 10863 uint8_t optlen; 10864 uint32_t len = 0; 10865 10866 for (optval = ipoptp_first(&opts, ipha); 10867 optval != IPOPT_EOL; 10868 optval = ipoptp_next(&opts)) { 10869 opt = opts.ipoptp_cur; 10870 optlen = opts.ipoptp_len; 10871 switch (optval) { 10872 int off1, off2; 10873 case IPOPT_SSRR: 10874 case IPOPT_LSRR: 10875 10876 /* Reverse source route */ 10877 /* 10878 * First entry should be the next to last one in the 10879 * current source route (the last entry is our 10880 * address.) 10881 * The last entry should be the final destination. 10882 */ 10883 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10884 buf[IPOPT_OLEN] = (uint8_t)optlen; 10885 off1 = IPOPT_MINOFF_SR - 1; 10886 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10887 if (off2 < 0) { 10888 /* No entries in source route */ 10889 break; 10890 } 10891 bcopy(opt + off2, dst, IP_ADDR_LEN); 10892 /* 10893 * Note: use src since ipha has not had its src 10894 * and dst reversed (it is in the state it was 10895 * received. 10896 */ 10897 bcopy(&ipha->ipha_src, buf + off2, 10898 IP_ADDR_LEN); 10899 off2 -= IP_ADDR_LEN; 10900 10901 while (off2 > 0) { 10902 bcopy(opt + off2, buf + off1, 10903 IP_ADDR_LEN); 10904 off1 += IP_ADDR_LEN; 10905 off2 -= IP_ADDR_LEN; 10906 } 10907 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10908 buf += optlen; 10909 len += optlen; 10910 break; 10911 } 10912 } 10913 done: 10914 /* Pad the resulting options */ 10915 while (len & 0x3) { 10916 *buf++ = IPOPT_EOL; 10917 len++; 10918 } 10919 return (len); 10920 } 10921 10922 10923 /* 10924 * Extract and revert a source route from ipha (if any) 10925 * and then update the relevant fields in both tcp_t and the standard header. 10926 */ 10927 static void 10928 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10929 { 10930 char buf[TCP_MAX_HDR_LENGTH]; 10931 uint_t tcph_len; 10932 int len; 10933 10934 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10935 len = IPH_HDR_LENGTH(ipha); 10936 if (len == IP_SIMPLE_HDR_LENGTH) 10937 /* Nothing to do */ 10938 return; 10939 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10940 (len & 0x3)) 10941 return; 10942 10943 tcph_len = tcp->tcp_tcp_hdr_len; 10944 bcopy(tcp->tcp_tcph, buf, tcph_len); 10945 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10946 (tcp->tcp_ipha->ipha_dst & 0xffff); 10947 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10948 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10949 len += IP_SIMPLE_HDR_LENGTH; 10950 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10951 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10952 if ((int)tcp->tcp_sum < 0) 10953 tcp->tcp_sum--; 10954 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10955 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10956 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10957 bcopy(buf, tcp->tcp_tcph, tcph_len); 10958 tcp->tcp_ip_hdr_len = len; 10959 tcp->tcp_ipha->ipha_version_and_hdr_length = 10960 (IP_VERSION << 4) | (len >> 2); 10961 len += tcph_len; 10962 tcp->tcp_hdr_len = len; 10963 } 10964 10965 /* 10966 * Copy the standard header into its new location, 10967 * lay in the new options and then update the relevant 10968 * fields in both tcp_t and the standard header. 10969 */ 10970 static int 10971 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10972 { 10973 uint_t tcph_len; 10974 uint8_t *ip_optp; 10975 tcph_t *new_tcph; 10976 tcp_stack_t *tcps = tcp->tcp_tcps; 10977 conn_t *connp = tcp->tcp_connp; 10978 10979 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10980 return (EINVAL); 10981 10982 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10983 return (EINVAL); 10984 10985 if (checkonly) { 10986 /* 10987 * do not really set, just pretend to - T_CHECK 10988 */ 10989 return (0); 10990 } 10991 10992 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10993 if (tcp->tcp_label_len > 0) { 10994 int padlen; 10995 uint8_t opt; 10996 10997 /* convert list termination to no-ops */ 10998 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 10999 ip_optp += ip_optp[IPOPT_OLEN]; 11000 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 11001 while (--padlen >= 0) 11002 *ip_optp++ = opt; 11003 } 11004 tcph_len = tcp->tcp_tcp_hdr_len; 11005 new_tcph = (tcph_t *)(ip_optp + len); 11006 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 11007 tcp->tcp_tcph = new_tcph; 11008 bcopy(ptr, ip_optp, len); 11009 11010 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 11011 11012 tcp->tcp_ip_hdr_len = len; 11013 tcp->tcp_ipha->ipha_version_and_hdr_length = 11014 (IP_VERSION << 4) | (len >> 2); 11015 tcp->tcp_hdr_len = len + tcph_len; 11016 if (!TCP_IS_DETACHED(tcp)) { 11017 /* Always allocate room for all options. */ 11018 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 11019 TCP_MAX_COMBINED_HEADER_LENGTH + tcps->tcps_wroff_xtra); 11020 } 11021 return (0); 11022 } 11023 11024 /* Get callback routine passed to nd_load by tcp_param_register */ 11025 /* ARGSUSED */ 11026 static int 11027 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 11028 { 11029 tcpparam_t *tcppa = (tcpparam_t *)cp; 11030 11031 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 11032 return (0); 11033 } 11034 11035 /* 11036 * Walk through the param array specified registering each element with the 11037 * named dispatch handler. 11038 */ 11039 static boolean_t 11040 tcp_param_register(IDP *ndp, tcpparam_t *tcppa, int cnt, tcp_stack_t *tcps) 11041 { 11042 for (; cnt-- > 0; tcppa++) { 11043 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 11044 if (!nd_load(ndp, tcppa->tcp_param_name, 11045 tcp_param_get, tcp_param_set, 11046 (caddr_t)tcppa)) { 11047 nd_free(ndp); 11048 return (B_FALSE); 11049 } 11050 } 11051 } 11052 tcps->tcps_wroff_xtra_param = kmem_zalloc(sizeof (tcpparam_t), 11053 KM_SLEEP); 11054 bcopy(&lcl_tcp_wroff_xtra_param, tcps->tcps_wroff_xtra_param, 11055 sizeof (tcpparam_t)); 11056 if (!nd_load(ndp, tcps->tcps_wroff_xtra_param->tcp_param_name, 11057 tcp_param_get, tcp_param_set_aligned, 11058 (caddr_t)tcps->tcps_wroff_xtra_param)) { 11059 nd_free(ndp); 11060 return (B_FALSE); 11061 } 11062 tcps->tcps_mdt_head_param = kmem_zalloc(sizeof (tcpparam_t), 11063 KM_SLEEP); 11064 bcopy(&lcl_tcp_mdt_head_param, tcps->tcps_mdt_head_param, 11065 sizeof (tcpparam_t)); 11066 if (!nd_load(ndp, tcps->tcps_mdt_head_param->tcp_param_name, 11067 tcp_param_get, tcp_param_set_aligned, 11068 (caddr_t)tcps->tcps_mdt_head_param)) { 11069 nd_free(ndp); 11070 return (B_FALSE); 11071 } 11072 tcps->tcps_mdt_tail_param = kmem_zalloc(sizeof (tcpparam_t), 11073 KM_SLEEP); 11074 bcopy(&lcl_tcp_mdt_tail_param, tcps->tcps_mdt_tail_param, 11075 sizeof (tcpparam_t)); 11076 if (!nd_load(ndp, tcps->tcps_mdt_tail_param->tcp_param_name, 11077 tcp_param_get, tcp_param_set_aligned, 11078 (caddr_t)tcps->tcps_mdt_tail_param)) { 11079 nd_free(ndp); 11080 return (B_FALSE); 11081 } 11082 tcps->tcps_mdt_max_pbufs_param = kmem_zalloc(sizeof (tcpparam_t), 11083 KM_SLEEP); 11084 bcopy(&lcl_tcp_mdt_max_pbufs_param, tcps->tcps_mdt_max_pbufs_param, 11085 sizeof (tcpparam_t)); 11086 if (!nd_load(ndp, tcps->tcps_mdt_max_pbufs_param->tcp_param_name, 11087 tcp_param_get, tcp_param_set_aligned, 11088 (caddr_t)tcps->tcps_mdt_max_pbufs_param)) { 11089 nd_free(ndp); 11090 return (B_FALSE); 11091 } 11092 if (!nd_load(ndp, "tcp_extra_priv_ports", 11093 tcp_extra_priv_ports_get, NULL, NULL)) { 11094 nd_free(ndp); 11095 return (B_FALSE); 11096 } 11097 if (!nd_load(ndp, "tcp_extra_priv_ports_add", 11098 NULL, tcp_extra_priv_ports_add, NULL)) { 11099 nd_free(ndp); 11100 return (B_FALSE); 11101 } 11102 if (!nd_load(ndp, "tcp_extra_priv_ports_del", 11103 NULL, tcp_extra_priv_ports_del, NULL)) { 11104 nd_free(ndp); 11105 return (B_FALSE); 11106 } 11107 if (!nd_load(ndp, "tcp_1948_phrase", NULL, 11108 tcp_1948_phrase_set, NULL)) { 11109 nd_free(ndp); 11110 return (B_FALSE); 11111 } 11112 /* 11113 * Dummy ndd variables - only to convey obsolescence information 11114 * through printing of their name (no get or set routines) 11115 * XXX Remove in future releases ? 11116 */ 11117 if (!nd_load(ndp, 11118 "tcp_close_wait_interval(obsoleted - " 11119 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11120 nd_free(ndp); 11121 return (B_FALSE); 11122 } 11123 return (B_TRUE); 11124 } 11125 11126 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11127 /* ARGSUSED */ 11128 static int 11129 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11130 cred_t *cr) 11131 { 11132 long new_value; 11133 tcpparam_t *tcppa = (tcpparam_t *)cp; 11134 11135 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11136 new_value < tcppa->tcp_param_min || 11137 new_value > tcppa->tcp_param_max) { 11138 return (EINVAL); 11139 } 11140 /* 11141 * Need to make sure new_value is a multiple of 4. If it is not, 11142 * round it up. For future 64 bit requirement, we actually make it 11143 * a multiple of 8. 11144 */ 11145 if (new_value & 0x7) { 11146 new_value = (new_value & ~0x7) + 0x8; 11147 } 11148 tcppa->tcp_param_val = new_value; 11149 return (0); 11150 } 11151 11152 /* Set callback routine passed to nd_load by tcp_param_register */ 11153 /* ARGSUSED */ 11154 static int 11155 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11156 { 11157 long new_value; 11158 tcpparam_t *tcppa = (tcpparam_t *)cp; 11159 11160 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11161 new_value < tcppa->tcp_param_min || 11162 new_value > tcppa->tcp_param_max) { 11163 return (EINVAL); 11164 } 11165 tcppa->tcp_param_val = new_value; 11166 return (0); 11167 } 11168 11169 /* 11170 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11171 * is filled, return as much as we can. The message passed in may be 11172 * multi-part, chained using b_cont. "start" is the starting sequence 11173 * number for this piece. 11174 */ 11175 static mblk_t * 11176 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11177 { 11178 uint32_t end; 11179 mblk_t *mp1; 11180 mblk_t *mp2; 11181 mblk_t *next_mp; 11182 uint32_t u1; 11183 tcp_stack_t *tcps = tcp->tcp_tcps; 11184 11185 /* Walk through all the new pieces. */ 11186 do { 11187 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11188 (uintptr_t)INT_MAX); 11189 end = start + (int)(mp->b_wptr - mp->b_rptr); 11190 next_mp = mp->b_cont; 11191 if (start == end) { 11192 /* Empty. Blast it. */ 11193 freeb(mp); 11194 continue; 11195 } 11196 mp->b_cont = NULL; 11197 TCP_REASS_SET_SEQ(mp, start); 11198 TCP_REASS_SET_END(mp, end); 11199 mp1 = tcp->tcp_reass_tail; 11200 if (!mp1) { 11201 tcp->tcp_reass_tail = mp; 11202 tcp->tcp_reass_head = mp; 11203 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11204 UPDATE_MIB(&tcps->tcps_mib, 11205 tcpInDataUnorderBytes, end - start); 11206 continue; 11207 } 11208 /* New stuff completely beyond tail? */ 11209 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11210 /* Link it on end. */ 11211 mp1->b_cont = mp; 11212 tcp->tcp_reass_tail = mp; 11213 BUMP_MIB(&tcps->tcps_mib, tcpInDataUnorderSegs); 11214 UPDATE_MIB(&tcps->tcps_mib, 11215 tcpInDataUnorderBytes, end - start); 11216 continue; 11217 } 11218 mp1 = tcp->tcp_reass_head; 11219 u1 = TCP_REASS_SEQ(mp1); 11220 /* New stuff at the front? */ 11221 if (SEQ_LT(start, u1)) { 11222 /* Yes... Check for overlap. */ 11223 mp->b_cont = mp1; 11224 tcp->tcp_reass_head = mp; 11225 tcp_reass_elim_overlap(tcp, mp); 11226 continue; 11227 } 11228 /* 11229 * The new piece fits somewhere between the head and tail. 11230 * We find our slot, where mp1 precedes us and mp2 trails. 11231 */ 11232 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11233 u1 = TCP_REASS_SEQ(mp2); 11234 if (SEQ_LEQ(start, u1)) 11235 break; 11236 } 11237 /* Link ourselves in */ 11238 mp->b_cont = mp2; 11239 mp1->b_cont = mp; 11240 11241 /* Trim overlap with following mblk(s) first */ 11242 tcp_reass_elim_overlap(tcp, mp); 11243 11244 /* Trim overlap with preceding mblk */ 11245 tcp_reass_elim_overlap(tcp, mp1); 11246 11247 } while (start = end, mp = next_mp); 11248 mp1 = tcp->tcp_reass_head; 11249 /* Anything ready to go? */ 11250 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11251 return (NULL); 11252 /* Eat what we can off the queue */ 11253 for (;;) { 11254 mp = mp1->b_cont; 11255 end = TCP_REASS_END(mp1); 11256 TCP_REASS_SET_SEQ(mp1, 0); 11257 TCP_REASS_SET_END(mp1, 0); 11258 if (!mp) { 11259 tcp->tcp_reass_tail = NULL; 11260 break; 11261 } 11262 if (end != TCP_REASS_SEQ(mp)) { 11263 mp1->b_cont = NULL; 11264 break; 11265 } 11266 mp1 = mp; 11267 } 11268 mp1 = tcp->tcp_reass_head; 11269 tcp->tcp_reass_head = mp; 11270 return (mp1); 11271 } 11272 11273 /* Eliminate any overlap that mp may have over later mblks */ 11274 static void 11275 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11276 { 11277 uint32_t end; 11278 mblk_t *mp1; 11279 uint32_t u1; 11280 tcp_stack_t *tcps = tcp->tcp_tcps; 11281 11282 end = TCP_REASS_END(mp); 11283 while ((mp1 = mp->b_cont) != NULL) { 11284 u1 = TCP_REASS_SEQ(mp1); 11285 if (!SEQ_GT(end, u1)) 11286 break; 11287 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11288 mp->b_wptr -= end - u1; 11289 TCP_REASS_SET_END(mp, u1); 11290 BUMP_MIB(&tcps->tcps_mib, tcpInDataPartDupSegs); 11291 UPDATE_MIB(&tcps->tcps_mib, 11292 tcpInDataPartDupBytes, end - u1); 11293 break; 11294 } 11295 mp->b_cont = mp1->b_cont; 11296 TCP_REASS_SET_SEQ(mp1, 0); 11297 TCP_REASS_SET_END(mp1, 0); 11298 freeb(mp1); 11299 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 11300 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, end - u1); 11301 } 11302 if (!mp1) 11303 tcp->tcp_reass_tail = mp; 11304 } 11305 11306 static uint_t 11307 tcp_rwnd_reopen(tcp_t *tcp) 11308 { 11309 uint_t ret = 0; 11310 uint_t thwin; 11311 11312 /* Learn the latest rwnd information that we sent to the other side. */ 11313 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11314 << tcp->tcp_rcv_ws; 11315 /* This is peer's calculated send window (our receive window). */ 11316 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11317 /* 11318 * Increase the receive window to max. But we need to do receiver 11319 * SWS avoidance. This means that we need to check the increase of 11320 * of receive window is at least 1 MSS. 11321 */ 11322 if (tcp->tcp_recv_hiwater - thwin >= tcp->tcp_mss) { 11323 /* 11324 * If the window that the other side knows is less than max 11325 * deferred acks segments, send an update immediately. 11326 */ 11327 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11328 BUMP_MIB(&tcp->tcp_tcps->tcps_mib, tcpOutWinUpdate); 11329 ret = TH_ACK_NEEDED; 11330 } 11331 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 11332 } 11333 return (ret); 11334 } 11335 11336 /* 11337 * Send up all messages queued on tcp_rcv_list. 11338 */ 11339 static uint_t 11340 tcp_rcv_drain(tcp_t *tcp) 11341 { 11342 mblk_t *mp; 11343 uint_t ret = 0; 11344 #ifdef DEBUG 11345 uint_t cnt = 0; 11346 #endif 11347 queue_t *q = tcp->tcp_rq; 11348 11349 /* Can't drain on an eager connection */ 11350 if (tcp->tcp_listener != NULL) 11351 return (ret); 11352 11353 /* Can't be a non-STREAMS connection */ 11354 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp)); 11355 11356 /* No need for the push timer now. */ 11357 if (tcp->tcp_push_tid != 0) { 11358 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11359 tcp->tcp_push_tid = 0; 11360 } 11361 11362 /* 11363 * Handle two cases here: we are currently fused or we were 11364 * previously fused and have some urgent data to be delivered 11365 * upstream. The latter happens because we either ran out of 11366 * memory or were detached and therefore sending the SIGURG was 11367 * deferred until this point. In either case we pass control 11368 * over to tcp_fuse_rcv_drain() since it may need to complete 11369 * some work. 11370 */ 11371 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11372 ASSERT(IPCL_IS_NONSTR(tcp->tcp_connp) || 11373 tcp->tcp_fused_sigurg_mp != NULL); 11374 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11375 &tcp->tcp_fused_sigurg_mp)) 11376 return (ret); 11377 } 11378 11379 while ((mp = tcp->tcp_rcv_list) != NULL) { 11380 tcp->tcp_rcv_list = mp->b_next; 11381 mp->b_next = NULL; 11382 #ifdef DEBUG 11383 cnt += msgdsize(mp); 11384 #endif 11385 /* Does this need SSL processing first? */ 11386 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11387 DTRACE_PROBE1(kssl_mblk__ksslinput_rcvdrain, 11388 mblk_t *, mp); 11389 tcp_kssl_input(tcp, mp); 11390 continue; 11391 } 11392 putnext(q, mp); 11393 } 11394 #ifdef DEBUG 11395 ASSERT(cnt == tcp->tcp_rcv_cnt); 11396 #endif 11397 tcp->tcp_rcv_last_head = NULL; 11398 tcp->tcp_rcv_last_tail = NULL; 11399 tcp->tcp_rcv_cnt = 0; 11400 11401 if (canputnext(q)) 11402 return (tcp_rwnd_reopen(tcp)); 11403 11404 return (ret); 11405 } 11406 11407 /* 11408 * Queue data on tcp_rcv_list which is a b_next chain. 11409 * tcp_rcv_last_head/tail is the last element of this chain. 11410 * Each element of the chain is a b_cont chain. 11411 * 11412 * M_DATA messages are added to the current element. 11413 * Other messages are added as new (b_next) elements. 11414 */ 11415 void 11416 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11417 { 11418 ASSERT(seg_len == msgdsize(mp)); 11419 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11420 11421 if (tcp->tcp_rcv_list == NULL) { 11422 ASSERT(tcp->tcp_rcv_last_head == NULL); 11423 tcp->tcp_rcv_list = mp; 11424 tcp->tcp_rcv_last_head = mp; 11425 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11426 tcp->tcp_rcv_last_tail->b_cont = mp; 11427 } else { 11428 tcp->tcp_rcv_last_head->b_next = mp; 11429 tcp->tcp_rcv_last_head = mp; 11430 } 11431 11432 while (mp->b_cont) 11433 mp = mp->b_cont; 11434 11435 tcp->tcp_rcv_last_tail = mp; 11436 tcp->tcp_rcv_cnt += seg_len; 11437 tcp->tcp_rwnd -= seg_len; 11438 } 11439 11440 /* 11441 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11442 * 11443 * This is the default entry function into TCP on the read side. TCP is 11444 * always entered via squeue i.e. using squeue's for mutual exclusion. 11445 * When classifier does a lookup to find the tcp, it also puts a reference 11446 * on the conn structure associated so the tcp is guaranteed to exist 11447 * when we come here. We still need to check the state because it might 11448 * as well has been closed. The squeue processing function i.e. squeue_enter, 11449 * is responsible for doing the CONN_DEC_REF. 11450 * 11451 * Apart from the default entry point, IP also sends packets directly to 11452 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11453 * connections. 11454 */ 11455 boolean_t tcp_outbound_squeue_switch = B_FALSE; 11456 void 11457 tcp_input(void *arg, mblk_t *mp, void *arg2) 11458 { 11459 conn_t *connp = (conn_t *)arg; 11460 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11461 11462 /* arg2 is the sqp */ 11463 ASSERT(arg2 != NULL); 11464 ASSERT(mp != NULL); 11465 11466 /* 11467 * Don't accept any input on a closed tcp as this TCP logically does 11468 * not exist on the system. Don't proceed further with this TCP. 11469 * For eg. this packet could trigger another close of this tcp 11470 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11471 * tcp_clean_death / tcp_closei_local must be called at most once 11472 * on a TCP. In this case we need to refeed the packet into the 11473 * classifier and figure out where the packet should go. Need to 11474 * preserve the recv_ill somehow. Until we figure that out, for 11475 * now just drop the packet if we can't classify the packet. 11476 */ 11477 if (tcp->tcp_state == TCPS_CLOSED || 11478 tcp->tcp_state == TCPS_BOUND) { 11479 conn_t *new_connp; 11480 ip_stack_t *ipst = tcp->tcp_tcps->tcps_netstack->netstack_ip; 11481 11482 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 11483 if (new_connp != NULL) { 11484 tcp_reinput(new_connp, mp, arg2); 11485 return; 11486 } 11487 /* We failed to classify. For now just drop the packet */ 11488 freemsg(mp); 11489 return; 11490 } 11491 11492 if (DB_TYPE(mp) != M_DATA) { 11493 tcp_rput_common(tcp, mp); 11494 return; 11495 } 11496 11497 if (mp->b_datap->db_struioflag & STRUIO_CONNECT) { 11498 squeue_t *final_sqp; 11499 11500 mp->b_datap->db_struioflag &= ~STRUIO_CONNECT; 11501 final_sqp = (squeue_t *)DB_CKSUMSTART(mp); 11502 DB_CKSUMSTART(mp) = 0; 11503 if (tcp->tcp_state == TCPS_SYN_SENT && 11504 connp->conn_final_sqp == NULL && 11505 tcp_outbound_squeue_switch) { 11506 ASSERT(connp->conn_initial_sqp == connp->conn_sqp); 11507 connp->conn_final_sqp = final_sqp; 11508 if (connp->conn_final_sqp != connp->conn_sqp) { 11509 CONN_INC_REF(connp); 11510 SQUEUE_SWITCH(connp, connp->conn_final_sqp); 11511 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 11512 tcp_rput_data, connp, ip_squeue_flag, 11513 SQTAG_CONNECT_FINISH); 11514 return; 11515 } 11516 } 11517 } 11518 tcp_rput_data(connp, mp, arg2); 11519 } 11520 11521 /* 11522 * The read side put procedure. 11523 * The packets passed up by ip are assume to be aligned according to 11524 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11525 */ 11526 static void 11527 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11528 { 11529 /* 11530 * tcp_rput_data() does not expect M_CTL except for the case 11531 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11532 * type. Need to make sure that any other M_CTLs don't make 11533 * it to tcp_rput_data since it is not expecting any and doesn't 11534 * check for it. 11535 */ 11536 if (DB_TYPE(mp) == M_CTL) { 11537 switch (*(uint32_t *)(mp->b_rptr)) { 11538 case TCP_IOC_ABORT_CONN: 11539 /* 11540 * Handle connection abort request. 11541 */ 11542 tcp_ioctl_abort_handler(tcp, mp); 11543 return; 11544 case IPSEC_IN: 11545 /* 11546 * Only secure icmp arrive in TCP and they 11547 * don't go through data path. 11548 */ 11549 tcp_icmp_error(tcp, mp); 11550 return; 11551 case IN_PKTINFO: 11552 /* 11553 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11554 * sockets that are receiving IPv4 traffic. tcp 11555 */ 11556 ASSERT(tcp->tcp_family == AF_INET6); 11557 ASSERT(tcp->tcp_ipv6_recvancillary & 11558 TCP_IPV6_RECVPKTINFO); 11559 tcp_rput_data(tcp->tcp_connp, mp, 11560 tcp->tcp_connp->conn_sqp); 11561 return; 11562 case MDT_IOC_INFO_UPDATE: 11563 /* 11564 * Handle Multidata information update; the 11565 * following routine will free the message. 11566 */ 11567 if (tcp->tcp_connp->conn_mdt_ok) { 11568 tcp_mdt_update(tcp, 11569 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11570 B_FALSE); 11571 } 11572 freemsg(mp); 11573 return; 11574 case LSO_IOC_INFO_UPDATE: 11575 /* 11576 * Handle LSO information update; the following 11577 * routine will free the message. 11578 */ 11579 if (tcp->tcp_connp->conn_lso_ok) { 11580 tcp_lso_update(tcp, 11581 &((ip_lso_info_t *)mp->b_rptr)->lso_capab); 11582 } 11583 freemsg(mp); 11584 return; 11585 default: 11586 /* 11587 * tcp_icmp_err() will process the M_CTL packets. 11588 * Non-ICMP packets, if any, will be discarded in 11589 * tcp_icmp_err(). We will process the ICMP packet 11590 * even if we are TCP_IS_DETACHED_NONEAGER as the 11591 * incoming ICMP packet may result in changing 11592 * the tcp_mss, which we would need if we have 11593 * packets to retransmit. 11594 */ 11595 tcp_icmp_error(tcp, mp); 11596 return; 11597 } 11598 } 11599 11600 /* No point processing the message if tcp is already closed */ 11601 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11602 freemsg(mp); 11603 return; 11604 } 11605 11606 tcp_rput_other(tcp, mp); 11607 } 11608 11609 11610 /* The minimum of smoothed mean deviation in RTO calculation. */ 11611 #define TCP_SD_MIN 400 11612 11613 /* 11614 * Set RTO for this connection. The formula is from Jacobson and Karels' 11615 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11616 * are the same as those in Appendix A.2 of that paper. 11617 * 11618 * m = new measurement 11619 * sa = smoothed RTT average (8 * average estimates). 11620 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11621 */ 11622 static void 11623 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11624 { 11625 long m = TICK_TO_MSEC(rtt); 11626 clock_t sa = tcp->tcp_rtt_sa; 11627 clock_t sv = tcp->tcp_rtt_sd; 11628 clock_t rto; 11629 tcp_stack_t *tcps = tcp->tcp_tcps; 11630 11631 BUMP_MIB(&tcps->tcps_mib, tcpRttUpdate); 11632 tcp->tcp_rtt_update++; 11633 11634 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11635 if (sa != 0) { 11636 /* 11637 * Update average estimator: 11638 * new rtt = 7/8 old rtt + 1/8 Error 11639 */ 11640 11641 /* m is now Error in estimate. */ 11642 m -= sa >> 3; 11643 if ((sa += m) <= 0) { 11644 /* 11645 * Don't allow the smoothed average to be negative. 11646 * We use 0 to denote reinitialization of the 11647 * variables. 11648 */ 11649 sa = 1; 11650 } 11651 11652 /* 11653 * Update deviation estimator: 11654 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11655 */ 11656 if (m < 0) 11657 m = -m; 11658 m -= sv >> 2; 11659 sv += m; 11660 } else { 11661 /* 11662 * This follows BSD's implementation. So the reinitialized 11663 * RTO is 3 * m. We cannot go less than 2 because if the 11664 * link is bandwidth dominated, doubling the window size 11665 * during slow start means doubling the RTT. We want to be 11666 * more conservative when we reinitialize our estimates. 3 11667 * is just a convenient number. 11668 */ 11669 sa = m << 3; 11670 sv = m << 1; 11671 } 11672 if (sv < TCP_SD_MIN) { 11673 /* 11674 * We do not know that if sa captures the delay ACK 11675 * effect as in a long train of segments, a receiver 11676 * does not delay its ACKs. So set the minimum of sv 11677 * to be TCP_SD_MIN, which is default to 400 ms, twice 11678 * of BSD DATO. That means the minimum of mean 11679 * deviation is 100 ms. 11680 * 11681 */ 11682 sv = TCP_SD_MIN; 11683 } 11684 tcp->tcp_rtt_sa = sa; 11685 tcp->tcp_rtt_sd = sv; 11686 /* 11687 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11688 * 11689 * Add tcp_rexmit_interval extra in case of extreme environment 11690 * where the algorithm fails to work. The default value of 11691 * tcp_rexmit_interval_extra should be 0. 11692 * 11693 * As we use a finer grained clock than BSD and update 11694 * RTO for every ACKs, add in another .25 of RTT to the 11695 * deviation of RTO to accomodate burstiness of 1/4 of 11696 * window size. 11697 */ 11698 rto = (sa >> 3) + sv + tcps->tcps_rexmit_interval_extra + (sa >> 5); 11699 11700 if (rto > tcps->tcps_rexmit_interval_max) { 11701 tcp->tcp_rto = tcps->tcps_rexmit_interval_max; 11702 } else if (rto < tcps->tcps_rexmit_interval_min) { 11703 tcp->tcp_rto = tcps->tcps_rexmit_interval_min; 11704 } else { 11705 tcp->tcp_rto = rto; 11706 } 11707 11708 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11709 tcp->tcp_timer_backoff = 0; 11710 } 11711 11712 /* 11713 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11714 * send queue which starts at the given seq. no. 11715 * 11716 * Parameters: 11717 * tcp_t *tcp: the tcp instance pointer. 11718 * uint32_t seq: the starting seq. no of the requested segment. 11719 * int32_t *off: after the execution, *off will be the offset to 11720 * the returned mblk which points to the requested seq no. 11721 * It is the caller's responsibility to send in a non-null off. 11722 * 11723 * Return: 11724 * A mblk_t pointer pointing to the requested segment in send queue. 11725 */ 11726 static mblk_t * 11727 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11728 { 11729 int32_t cnt; 11730 mblk_t *mp; 11731 11732 /* Defensive coding. Make sure we don't send incorrect data. */ 11733 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11734 return (NULL); 11735 11736 cnt = seq - tcp->tcp_suna; 11737 mp = tcp->tcp_xmit_head; 11738 while (cnt > 0 && mp != NULL) { 11739 cnt -= mp->b_wptr - mp->b_rptr; 11740 if (cnt < 0) { 11741 cnt += mp->b_wptr - mp->b_rptr; 11742 break; 11743 } 11744 mp = mp->b_cont; 11745 } 11746 ASSERT(mp != NULL); 11747 *off = cnt; 11748 return (mp); 11749 } 11750 11751 /* 11752 * This function handles all retransmissions if SACK is enabled for this 11753 * connection. First it calculates how many segments can be retransmitted 11754 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11755 * segments. A segment is eligible if sack_cnt for that segment is greater 11756 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11757 * all eligible segments, it checks to see if TCP can send some new segments 11758 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11759 * 11760 * Parameters: 11761 * tcp_t *tcp: the tcp structure of the connection. 11762 * uint_t *flags: in return, appropriate value will be set for 11763 * tcp_rput_data(). 11764 */ 11765 static void 11766 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11767 { 11768 notsack_blk_t *notsack_blk; 11769 int32_t usable_swnd; 11770 int32_t mss; 11771 uint32_t seg_len; 11772 mblk_t *xmit_mp; 11773 tcp_stack_t *tcps = tcp->tcp_tcps; 11774 11775 ASSERT(tcp->tcp_sack_info != NULL); 11776 ASSERT(tcp->tcp_notsack_list != NULL); 11777 ASSERT(tcp->tcp_rexmit == B_FALSE); 11778 11779 /* Defensive coding in case there is a bug... */ 11780 if (tcp->tcp_notsack_list == NULL) { 11781 return; 11782 } 11783 notsack_blk = tcp->tcp_notsack_list; 11784 mss = tcp->tcp_mss; 11785 11786 /* 11787 * Limit the num of outstanding data in the network to be 11788 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11789 */ 11790 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11791 11792 /* At least retransmit 1 MSS of data. */ 11793 if (usable_swnd <= 0) { 11794 usable_swnd = mss; 11795 } 11796 11797 /* Make sure no new RTT samples will be taken. */ 11798 tcp->tcp_csuna = tcp->tcp_snxt; 11799 11800 notsack_blk = tcp->tcp_notsack_list; 11801 while (usable_swnd > 0) { 11802 mblk_t *snxt_mp, *tmp_mp; 11803 tcp_seq begin = tcp->tcp_sack_snxt; 11804 tcp_seq end; 11805 int32_t off; 11806 11807 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11808 if (SEQ_GT(notsack_blk->end, begin) && 11809 (notsack_blk->sack_cnt >= 11810 tcps->tcps_dupack_fast_retransmit)) { 11811 end = notsack_blk->end; 11812 if (SEQ_LT(begin, notsack_blk->begin)) { 11813 begin = notsack_blk->begin; 11814 } 11815 break; 11816 } 11817 } 11818 /* 11819 * All holes are filled. Manipulate tcp_cwnd to send more 11820 * if we can. Note that after the SACK recovery, tcp_cwnd is 11821 * set to tcp_cwnd_ssthresh. 11822 */ 11823 if (notsack_blk == NULL) { 11824 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11825 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11826 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11827 ASSERT(tcp->tcp_cwnd > 0); 11828 return; 11829 } else { 11830 usable_swnd = usable_swnd / mss; 11831 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11832 MAX(usable_swnd * mss, mss); 11833 *flags |= TH_XMIT_NEEDED; 11834 return; 11835 } 11836 } 11837 11838 /* 11839 * Note that we may send more than usable_swnd allows here 11840 * because of round off, but no more than 1 MSS of data. 11841 */ 11842 seg_len = end - begin; 11843 if (seg_len > mss) 11844 seg_len = mss; 11845 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11846 ASSERT(snxt_mp != NULL); 11847 /* This should not happen. Defensive coding again... */ 11848 if (snxt_mp == NULL) { 11849 return; 11850 } 11851 11852 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11853 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11854 if (xmit_mp == NULL) 11855 return; 11856 11857 usable_swnd -= seg_len; 11858 tcp->tcp_pipe += seg_len; 11859 tcp->tcp_sack_snxt = begin + seg_len; 11860 11861 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11862 11863 /* 11864 * Update the send timestamp to avoid false retransmission. 11865 */ 11866 snxt_mp->b_prev = (mblk_t *)lbolt; 11867 11868 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 11869 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, seg_len); 11870 BUMP_MIB(&tcps->tcps_mib, tcpOutSackRetransSegs); 11871 /* 11872 * Update tcp_rexmit_max to extend this SACK recovery phase. 11873 * This happens when new data sent during fast recovery is 11874 * also lost. If TCP retransmits those new data, it needs 11875 * to extend SACK recover phase to avoid starting another 11876 * fast retransmit/recovery unnecessarily. 11877 */ 11878 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11879 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11880 } 11881 } 11882 } 11883 11884 /* 11885 * This function handles policy checking at TCP level for non-hard_bound/ 11886 * detached connections. 11887 */ 11888 static boolean_t 11889 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11890 boolean_t secure, boolean_t mctl_present) 11891 { 11892 ipsec_latch_t *ipl = NULL; 11893 ipsec_action_t *act = NULL; 11894 mblk_t *data_mp; 11895 ipsec_in_t *ii; 11896 const char *reason; 11897 kstat_named_t *counter; 11898 tcp_stack_t *tcps = tcp->tcp_tcps; 11899 ipsec_stack_t *ipss; 11900 ip_stack_t *ipst; 11901 11902 ASSERT(mctl_present || !secure); 11903 11904 ASSERT((ipha == NULL && ip6h != NULL) || 11905 (ip6h == NULL && ipha != NULL)); 11906 11907 /* 11908 * We don't necessarily have an ipsec_in_act action to verify 11909 * policy because of assymetrical policy where we have only 11910 * outbound policy and no inbound policy (possible with global 11911 * policy). 11912 */ 11913 if (!secure) { 11914 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11915 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11916 return (B_TRUE); 11917 ipsec_log_policy_failure(IPSEC_POLICY_MISMATCH, 11918 "tcp_check_policy", ipha, ip6h, secure, 11919 tcps->tcps_netstack); 11920 ipss = tcps->tcps_netstack->netstack_ipsec; 11921 11922 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11923 DROPPER(ipss, ipds_tcp_clear), 11924 &tcps->tcps_dropper); 11925 return (B_FALSE); 11926 } 11927 11928 /* 11929 * We have a secure packet. 11930 */ 11931 if (act == NULL) { 11932 ipsec_log_policy_failure(IPSEC_POLICY_NOT_NEEDED, 11933 "tcp_check_policy", ipha, ip6h, secure, 11934 tcps->tcps_netstack); 11935 ipss = tcps->tcps_netstack->netstack_ipsec; 11936 11937 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11938 DROPPER(ipss, ipds_tcp_secure), 11939 &tcps->tcps_dropper); 11940 return (B_FALSE); 11941 } 11942 11943 /* 11944 * XXX This whole routine is currently incorrect. ipl should 11945 * be set to the latch pointer, but is currently not set, so 11946 * we initialize it to NULL to avoid picking up random garbage. 11947 */ 11948 if (ipl == NULL) 11949 return (B_TRUE); 11950 11951 data_mp = first_mp->b_cont; 11952 11953 ii = (ipsec_in_t *)first_mp->b_rptr; 11954 11955 ipst = tcps->tcps_netstack->netstack_ip; 11956 11957 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11958 &counter, tcp->tcp_connp)) { 11959 BUMP_MIB(&ipst->ips_ip_mib, ipsecInSucceeded); 11960 return (B_TRUE); 11961 } 11962 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11963 "tcp inbound policy mismatch: %s, packet dropped\n", 11964 reason); 11965 BUMP_MIB(&ipst->ips_ip_mib, ipsecInFailed); 11966 11967 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, 11968 &tcps->tcps_dropper); 11969 return (B_FALSE); 11970 } 11971 11972 /* 11973 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11974 * retransmission after a timeout. 11975 * 11976 * To limit the number of duplicate segments, we limit the number of segment 11977 * to be sent in one time to tcp_snd_burst, the burst variable. 11978 */ 11979 static void 11980 tcp_ss_rexmit(tcp_t *tcp) 11981 { 11982 uint32_t snxt; 11983 uint32_t smax; 11984 int32_t win; 11985 int32_t mss; 11986 int32_t off; 11987 int32_t burst = tcp->tcp_snd_burst; 11988 mblk_t *snxt_mp; 11989 tcp_stack_t *tcps = tcp->tcp_tcps; 11990 11991 /* 11992 * Note that tcp_rexmit can be set even though TCP has retransmitted 11993 * all unack'ed segments. 11994 */ 11995 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11996 smax = tcp->tcp_rexmit_max; 11997 snxt = tcp->tcp_rexmit_nxt; 11998 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11999 snxt = tcp->tcp_suna; 12000 } 12001 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 12002 win -= snxt - tcp->tcp_suna; 12003 mss = tcp->tcp_mss; 12004 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 12005 12006 while (SEQ_LT(snxt, smax) && (win > 0) && 12007 (burst > 0) && (snxt_mp != NULL)) { 12008 mblk_t *xmit_mp; 12009 mblk_t *old_snxt_mp = snxt_mp; 12010 uint32_t cnt = mss; 12011 12012 if (win < cnt) { 12013 cnt = win; 12014 } 12015 if (SEQ_GT(snxt + cnt, smax)) { 12016 cnt = smax - snxt; 12017 } 12018 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 12019 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 12020 if (xmit_mp == NULL) 12021 return; 12022 12023 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 12024 12025 snxt += cnt; 12026 win -= cnt; 12027 /* 12028 * Update the send timestamp to avoid false 12029 * retransmission. 12030 */ 12031 old_snxt_mp->b_prev = (mblk_t *)lbolt; 12032 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 12033 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, cnt); 12034 12035 tcp->tcp_rexmit_nxt = snxt; 12036 burst--; 12037 } 12038 /* 12039 * If we have transmitted all we have at the time 12040 * we started the retranmission, we can leave 12041 * the rest of the job to tcp_wput_data(). But we 12042 * need to check the send window first. If the 12043 * win is not 0, go on with tcp_wput_data(). 12044 */ 12045 if (SEQ_LT(snxt, smax) || win == 0) { 12046 return; 12047 } 12048 } 12049 /* Only call tcp_wput_data() if there is data to be sent. */ 12050 if (tcp->tcp_unsent) { 12051 tcp_wput_data(tcp, NULL, B_FALSE); 12052 } 12053 } 12054 12055 /* 12056 * Process all TCP option in SYN segment. Note that this function should 12057 * be called after tcp_adapt_ire() is called so that the necessary info 12058 * from IRE is already set in the tcp structure. 12059 * 12060 * This function sets up the correct tcp_mss value according to the 12061 * MSS option value and our header size. It also sets up the window scale 12062 * and timestamp values, and initialize SACK info blocks. But it does not 12063 * change receive window size after setting the tcp_mss value. The caller 12064 * should do the appropriate change. 12065 */ 12066 void 12067 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 12068 { 12069 int options; 12070 tcp_opt_t tcpopt; 12071 uint32_t mss_max; 12072 char *tmp_tcph; 12073 tcp_stack_t *tcps = tcp->tcp_tcps; 12074 12075 tcpopt.tcp = NULL; 12076 options = tcp_parse_options(tcph, &tcpopt); 12077 12078 /* 12079 * Process MSS option. Note that MSS option value does not account 12080 * for IP or TCP options. This means that it is equal to MTU - minimum 12081 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 12082 * IPv6. 12083 */ 12084 if (!(options & TCP_OPT_MSS_PRESENT)) { 12085 if (tcp->tcp_ipversion == IPV4_VERSION) 12086 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv4; 12087 else 12088 tcpopt.tcp_opt_mss = tcps->tcps_mss_def_ipv6; 12089 } else { 12090 if (tcp->tcp_ipversion == IPV4_VERSION) 12091 mss_max = tcps->tcps_mss_max_ipv4; 12092 else 12093 mss_max = tcps->tcps_mss_max_ipv6; 12094 if (tcpopt.tcp_opt_mss < tcps->tcps_mss_min) 12095 tcpopt.tcp_opt_mss = tcps->tcps_mss_min; 12096 else if (tcpopt.tcp_opt_mss > mss_max) 12097 tcpopt.tcp_opt_mss = mss_max; 12098 } 12099 12100 /* Process Window Scale option. */ 12101 if (options & TCP_OPT_WSCALE_PRESENT) { 12102 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 12103 tcp->tcp_snd_ws_ok = B_TRUE; 12104 } else { 12105 tcp->tcp_snd_ws = B_FALSE; 12106 tcp->tcp_snd_ws_ok = B_FALSE; 12107 tcp->tcp_rcv_ws = B_FALSE; 12108 } 12109 12110 /* Process Timestamp option. */ 12111 if ((options & TCP_OPT_TSTAMP_PRESENT) && 12112 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 12113 tmp_tcph = (char *)tcp->tcp_tcph; 12114 12115 tcp->tcp_snd_ts_ok = B_TRUE; 12116 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12117 tcp->tcp_last_rcv_lbolt = lbolt64; 12118 ASSERT(OK_32PTR(tmp_tcph)); 12119 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12120 12121 /* Fill in our template header with basic timestamp option. */ 12122 tmp_tcph += tcp->tcp_tcp_hdr_len; 12123 tmp_tcph[0] = TCPOPT_NOP; 12124 tmp_tcph[1] = TCPOPT_NOP; 12125 tmp_tcph[2] = TCPOPT_TSTAMP; 12126 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12127 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12128 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12129 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12130 } else { 12131 tcp->tcp_snd_ts_ok = B_FALSE; 12132 } 12133 12134 /* 12135 * Process SACK options. If SACK is enabled for this connection, 12136 * then allocate the SACK info structure. Note the following ways 12137 * when tcp_snd_sack_ok is set to true. 12138 * 12139 * For active connection: in tcp_adapt_ire() called in 12140 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12141 * is checked. 12142 * 12143 * For passive connection: in tcp_adapt_ire() called in 12144 * tcp_accept_comm(). 12145 * 12146 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12147 * That check makes sure that if we did not send a SACK OK option, 12148 * we will not enable SACK for this connection even though the other 12149 * side sends us SACK OK option. For active connection, the SACK 12150 * info structure has already been allocated. So we need to free 12151 * it if SACK is disabled. 12152 */ 12153 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12154 (tcp->tcp_snd_sack_ok || 12155 (tcps->tcps_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12156 /* This should be true only in the passive case. */ 12157 if (tcp->tcp_sack_info == NULL) { 12158 ASSERT(TCP_IS_DETACHED(tcp)); 12159 tcp->tcp_sack_info = 12160 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12161 } 12162 if (tcp->tcp_sack_info == NULL) { 12163 tcp->tcp_snd_sack_ok = B_FALSE; 12164 } else { 12165 tcp->tcp_snd_sack_ok = B_TRUE; 12166 if (tcp->tcp_snd_ts_ok) { 12167 tcp->tcp_max_sack_blk = 3; 12168 } else { 12169 tcp->tcp_max_sack_blk = 4; 12170 } 12171 } 12172 } else { 12173 /* 12174 * Resetting tcp_snd_sack_ok to B_FALSE so that 12175 * no SACK info will be used for this 12176 * connection. This assumes that SACK usage 12177 * permission is negotiated. This may need 12178 * to be changed once this is clarified. 12179 */ 12180 if (tcp->tcp_sack_info != NULL) { 12181 ASSERT(tcp->tcp_notsack_list == NULL); 12182 kmem_cache_free(tcp_sack_info_cache, 12183 tcp->tcp_sack_info); 12184 tcp->tcp_sack_info = NULL; 12185 } 12186 tcp->tcp_snd_sack_ok = B_FALSE; 12187 } 12188 12189 /* 12190 * Now we know the exact TCP/IP header length, subtract 12191 * that from tcp_mss to get our side's MSS. 12192 */ 12193 tcp->tcp_mss -= tcp->tcp_hdr_len; 12194 /* 12195 * Here we assume that the other side's header size will be equal to 12196 * our header size. We calculate the real MSS accordingly. Need to 12197 * take into additional stuffs IPsec puts in. 12198 * 12199 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12200 */ 12201 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12202 ((tcp->tcp_ipversion == IPV4_VERSION ? 12203 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12204 12205 /* 12206 * Set MSS to the smaller one of both ends of the connection. 12207 * We should not have called tcp_mss_set() before, but our 12208 * side of the MSS should have been set to a proper value 12209 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12210 * STREAM head parameters properly. 12211 * 12212 * If we have a larger-than-16-bit window but the other side 12213 * didn't want to do window scale, tcp_rwnd_set() will take 12214 * care of that. 12215 */ 12216 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss), B_TRUE); 12217 } 12218 12219 /* 12220 * Sends the T_CONN_IND to the listener. The caller calls this 12221 * functions via squeue to get inside the listener's perimeter 12222 * once the 3 way hand shake is done a T_CONN_IND needs to be 12223 * sent. As an optimization, the caller can call this directly 12224 * if listener's perimeter is same as eager's. 12225 */ 12226 /* ARGSUSED */ 12227 void 12228 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12229 { 12230 conn_t *lconnp = (conn_t *)arg; 12231 tcp_t *listener = lconnp->conn_tcp; 12232 tcp_t *tcp; 12233 struct T_conn_ind *conn_ind; 12234 ipaddr_t *addr_cache; 12235 boolean_t need_send_conn_ind = B_FALSE; 12236 tcp_stack_t *tcps = listener->tcp_tcps; 12237 12238 /* retrieve the eager */ 12239 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12240 ASSERT(conn_ind->OPT_offset != 0 && 12241 conn_ind->OPT_length == sizeof (intptr_t)); 12242 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12243 conn_ind->OPT_length); 12244 12245 /* 12246 * TLI/XTI applications will get confused by 12247 * sending eager as an option since it violates 12248 * the option semantics. So remove the eager as 12249 * option since TLI/XTI app doesn't need it anyway. 12250 */ 12251 if (!TCP_IS_SOCKET(listener)) { 12252 conn_ind->OPT_length = 0; 12253 conn_ind->OPT_offset = 0; 12254 } 12255 if (listener->tcp_state == TCPS_CLOSED || 12256 TCP_IS_DETACHED(listener)) { 12257 /* 12258 * If listener has closed, it would have caused a 12259 * a cleanup/blowoff to happen for the eager. We 12260 * just need to return. 12261 */ 12262 freemsg(mp); 12263 return; 12264 } 12265 12266 12267 /* 12268 * if the conn_req_q is full defer passing up the 12269 * T_CONN_IND until space is availabe after t_accept() 12270 * processing 12271 */ 12272 mutex_enter(&listener->tcp_eager_lock); 12273 12274 /* 12275 * Take the eager out, if it is in the list of droppable eagers 12276 * as we are here because the 3W handshake is over. 12277 */ 12278 MAKE_UNDROPPABLE(tcp); 12279 12280 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12281 tcp_t *tail; 12282 12283 /* 12284 * The eager already has an extra ref put in tcp_rput_data 12285 * so that it stays till accept comes back even though it 12286 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12287 */ 12288 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12289 listener->tcp_conn_req_cnt_q0--; 12290 listener->tcp_conn_req_cnt_q++; 12291 12292 /* Move from SYN_RCVD to ESTABLISHED list */ 12293 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12294 tcp->tcp_eager_prev_q0; 12295 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12296 tcp->tcp_eager_next_q0; 12297 tcp->tcp_eager_prev_q0 = NULL; 12298 tcp->tcp_eager_next_q0 = NULL; 12299 12300 /* 12301 * Insert at end of the queue because sockfs 12302 * sends down T_CONN_RES in chronological 12303 * order. Leaving the older conn indications 12304 * at front of the queue helps reducing search 12305 * time. 12306 */ 12307 tail = listener->tcp_eager_last_q; 12308 if (tail != NULL) 12309 tail->tcp_eager_next_q = tcp; 12310 else 12311 listener->tcp_eager_next_q = tcp; 12312 listener->tcp_eager_last_q = tcp; 12313 tcp->tcp_eager_next_q = NULL; 12314 /* 12315 * Delay sending up the T_conn_ind until we are 12316 * done with the eager. Once we have have sent up 12317 * the T_conn_ind, the accept can potentially complete 12318 * any time and release the refhold we have on the eager. 12319 */ 12320 need_send_conn_ind = B_TRUE; 12321 } else { 12322 /* 12323 * Defer connection on q0 and set deferred 12324 * connection bit true 12325 */ 12326 tcp->tcp_conn_def_q0 = B_TRUE; 12327 12328 /* take tcp out of q0 ... */ 12329 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12330 tcp->tcp_eager_next_q0; 12331 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12332 tcp->tcp_eager_prev_q0; 12333 12334 /* ... and place it at the end of q0 */ 12335 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12336 tcp->tcp_eager_next_q0 = listener; 12337 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12338 listener->tcp_eager_prev_q0 = tcp; 12339 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12340 } 12341 12342 /* we have timed out before */ 12343 if (tcp->tcp_syn_rcvd_timeout != 0) { 12344 tcp->tcp_syn_rcvd_timeout = 0; 12345 listener->tcp_syn_rcvd_timeout--; 12346 if (listener->tcp_syn_defense && 12347 listener->tcp_syn_rcvd_timeout <= 12348 (tcps->tcps_conn_req_max_q0 >> 5) && 12349 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12350 listener->tcp_last_rcv_lbolt)) { 12351 /* 12352 * Turn off the defense mode if we 12353 * believe the SYN attack is over. 12354 */ 12355 listener->tcp_syn_defense = B_FALSE; 12356 if (listener->tcp_ip_addr_cache) { 12357 kmem_free((void *)listener->tcp_ip_addr_cache, 12358 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12359 listener->tcp_ip_addr_cache = NULL; 12360 } 12361 } 12362 } 12363 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12364 if (addr_cache != NULL) { 12365 /* 12366 * We have finished a 3-way handshake with this 12367 * remote host. This proves the IP addr is good. 12368 * Cache it! 12369 */ 12370 addr_cache[IP_ADDR_CACHE_HASH( 12371 tcp->tcp_remote)] = tcp->tcp_remote; 12372 } 12373 mutex_exit(&listener->tcp_eager_lock); 12374 if (need_send_conn_ind) 12375 tcp_ulp_newconn(lconnp, tcp->tcp_connp, mp); 12376 } 12377 12378 /* 12379 * Send the newconn notification to ulp. The eager is blown off if the 12380 * notification fails. 12381 */ 12382 static void 12383 tcp_ulp_newconn(conn_t *lconnp, conn_t *econnp, mblk_t *mp) 12384 { 12385 if (IPCL_IS_NONSTR(lconnp)) { 12386 cred_t *cr; 12387 pid_t cpid; 12388 12389 cr = msg_getcred(mp, &cpid); 12390 12391 ASSERT(econnp->conn_tcp->tcp_listener == lconnp->conn_tcp); 12392 ASSERT(econnp->conn_tcp->tcp_saved_listener == 12393 lconnp->conn_tcp); 12394 12395 /* Keep the message around in case of a fallback to TPI */ 12396 econnp->conn_tcp->tcp_conn.tcp_eager_conn_ind = mp; 12397 12398 /* 12399 * Notify the ULP about the newconn. It is guaranteed that no 12400 * tcp_accept() call will be made for the eager if the 12401 * notification fails, so it's safe to blow it off in that 12402 * case. 12403 * 12404 * The upper handle will be assigned when tcp_accept() is 12405 * called. 12406 */ 12407 if ((*lconnp->conn_upcalls->su_newconn) 12408 (lconnp->conn_upper_handle, 12409 (sock_lower_handle_t)econnp, 12410 &sock_tcp_downcalls, cr, cpid, 12411 &econnp->conn_upcalls) == NULL) { 12412 /* Failed to allocate a socket */ 12413 BUMP_MIB(&lconnp->conn_tcp->tcp_tcps->tcps_mib, 12414 tcpEstabResets); 12415 (void) tcp_eager_blowoff(lconnp->conn_tcp, 12416 econnp->conn_tcp->tcp_conn_req_seqnum); 12417 } 12418 } else { 12419 putnext(lconnp->conn_tcp->tcp_rq, mp); 12420 } 12421 } 12422 12423 mblk_t * 12424 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12425 uint_t *ifindexp, ip6_pkt_t *ippp) 12426 { 12427 ip_pktinfo_t *pinfo; 12428 ip6_t *ip6h; 12429 uchar_t *rptr; 12430 mblk_t *first_mp = mp; 12431 boolean_t mctl_present = B_FALSE; 12432 uint_t ifindex = 0; 12433 ip6_pkt_t ipp; 12434 uint_t ipvers; 12435 uint_t ip_hdr_len; 12436 tcp_stack_t *tcps = tcp->tcp_tcps; 12437 12438 rptr = mp->b_rptr; 12439 ASSERT(OK_32PTR(rptr)); 12440 ASSERT(tcp != NULL); 12441 ipp.ipp_fields = 0; 12442 12443 switch DB_TYPE(mp) { 12444 case M_CTL: 12445 mp = mp->b_cont; 12446 if (mp == NULL) { 12447 freemsg(first_mp); 12448 return (NULL); 12449 } 12450 if (DB_TYPE(mp) != M_DATA) { 12451 freemsg(first_mp); 12452 return (NULL); 12453 } 12454 mctl_present = B_TRUE; 12455 break; 12456 case M_DATA: 12457 break; 12458 default: 12459 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12460 freemsg(mp); 12461 return (NULL); 12462 } 12463 ipvers = IPH_HDR_VERSION(rptr); 12464 if (ipvers == IPV4_VERSION) { 12465 if (tcp == NULL) { 12466 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12467 goto done; 12468 } 12469 12470 ipp.ipp_fields |= IPPF_HOPLIMIT; 12471 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12472 12473 /* 12474 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12475 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12476 */ 12477 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12478 mctl_present) { 12479 pinfo = (ip_pktinfo_t *)first_mp->b_rptr; 12480 if ((MBLKL(first_mp) == sizeof (ip_pktinfo_t)) && 12481 (pinfo->ip_pkt_ulp_type == IN_PKTINFO) && 12482 (pinfo->ip_pkt_flags & IPF_RECVIF)) { 12483 ipp.ipp_fields |= IPPF_IFINDEX; 12484 ipp.ipp_ifindex = pinfo->ip_pkt_ifindex; 12485 ifindex = pinfo->ip_pkt_ifindex; 12486 } 12487 freeb(first_mp); 12488 mctl_present = B_FALSE; 12489 } 12490 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12491 } else { 12492 ip6h = (ip6_t *)rptr; 12493 12494 ASSERT(ipvers == IPV6_VERSION); 12495 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12496 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12497 ipp.ipp_hoplimit = ip6h->ip6_hops; 12498 12499 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12500 uint8_t nexthdrp; 12501 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 12502 12503 /* Look for ifindex information */ 12504 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12505 ip6i_t *ip6i = (ip6i_t *)ip6h; 12506 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12507 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12508 freemsg(first_mp); 12509 return (NULL); 12510 } 12511 12512 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12513 ASSERT(ip6i->ip6i_ifindex != 0); 12514 ipp.ipp_fields |= IPPF_IFINDEX; 12515 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12516 ifindex = ip6i->ip6i_ifindex; 12517 } 12518 rptr = (uchar_t *)&ip6i[1]; 12519 mp->b_rptr = rptr; 12520 if (rptr == mp->b_wptr) { 12521 mblk_t *mp1; 12522 mp1 = mp->b_cont; 12523 freeb(mp); 12524 mp = mp1; 12525 rptr = mp->b_rptr; 12526 } 12527 if (MBLKL(mp) < IPV6_HDR_LEN + 12528 sizeof (tcph_t)) { 12529 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12530 freemsg(first_mp); 12531 return (NULL); 12532 } 12533 ip6h = (ip6_t *)rptr; 12534 } 12535 12536 /* 12537 * Find any potentially interesting extension headers 12538 * as well as the length of the IPv6 + extension 12539 * headers. 12540 */ 12541 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12542 /* Verify if this is a TCP packet */ 12543 if (nexthdrp != IPPROTO_TCP) { 12544 BUMP_MIB(&ipst->ips_ip_mib, tcpInErrs); 12545 freemsg(first_mp); 12546 return (NULL); 12547 } 12548 } else { 12549 ip_hdr_len = IPV6_HDR_LEN; 12550 } 12551 } 12552 12553 done: 12554 if (ipversp != NULL) 12555 *ipversp = ipvers; 12556 if (ip_hdr_lenp != NULL) 12557 *ip_hdr_lenp = ip_hdr_len; 12558 if (ippp != NULL) 12559 *ippp = ipp; 12560 if (ifindexp != NULL) 12561 *ifindexp = ifindex; 12562 if (mctl_present) { 12563 freeb(first_mp); 12564 } 12565 return (mp); 12566 } 12567 12568 /* 12569 * Handle M_DATA messages from IP. Its called directly from IP via 12570 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12571 * in this path. 12572 * 12573 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12574 * v4 and v6), we are called through tcp_input() and a M_CTL can 12575 * be present for options but tcp_find_pktinfo() deals with it. We 12576 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12577 * 12578 * The first argument is always the connp/tcp to which the mp belongs. 12579 * There are no exceptions to this rule. The caller has already put 12580 * a reference on this connp/tcp and once tcp_rput_data() returns, 12581 * the squeue will do the refrele. 12582 * 12583 * The TH_SYN for the listener directly go to tcp_conn_request via 12584 * squeue. 12585 * 12586 * sqp: NULL = recursive, sqp != NULL means called from squeue 12587 */ 12588 void 12589 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12590 { 12591 int32_t bytes_acked; 12592 int32_t gap; 12593 mblk_t *mp1; 12594 uint_t flags; 12595 uint32_t new_swnd = 0; 12596 uchar_t *iphdr; 12597 uchar_t *rptr; 12598 int32_t rgap; 12599 uint32_t seg_ack; 12600 int seg_len; 12601 uint_t ip_hdr_len; 12602 uint32_t seg_seq; 12603 tcph_t *tcph; 12604 int urp; 12605 tcp_opt_t tcpopt; 12606 uint_t ipvers; 12607 ip6_pkt_t ipp; 12608 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12609 uint32_t cwnd; 12610 uint32_t add; 12611 int npkt; 12612 int mss; 12613 conn_t *connp = (conn_t *)arg; 12614 squeue_t *sqp = (squeue_t *)arg2; 12615 tcp_t *tcp = connp->conn_tcp; 12616 tcp_stack_t *tcps = tcp->tcp_tcps; 12617 12618 /* 12619 * RST from fused tcp loopback peer should trigger an unfuse. 12620 */ 12621 if (tcp->tcp_fused) { 12622 TCP_STAT(tcps, tcp_fusion_aborted); 12623 tcp_unfuse(tcp); 12624 } 12625 12626 iphdr = mp->b_rptr; 12627 rptr = mp->b_rptr; 12628 ASSERT(OK_32PTR(rptr)); 12629 12630 /* 12631 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12632 * processing here. For rest call tcp_find_pktinfo to fill up the 12633 * necessary information. 12634 */ 12635 if (IPCL_IS_TCP4(connp)) { 12636 ipvers = IPV4_VERSION; 12637 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12638 } else { 12639 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12640 NULL, &ipp); 12641 if (mp == NULL) { 12642 TCP_STAT(tcps, tcp_rput_v6_error); 12643 return; 12644 } 12645 iphdr = mp->b_rptr; 12646 rptr = mp->b_rptr; 12647 } 12648 ASSERT(DB_TYPE(mp) == M_DATA); 12649 ASSERT(mp->b_next == NULL); 12650 12651 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12652 seg_seq = ABE32_TO_U32(tcph->th_seq); 12653 seg_ack = ABE32_TO_U32(tcph->th_ack); 12654 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12655 seg_len = (int)(mp->b_wptr - rptr) - 12656 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12657 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12658 do { 12659 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12660 (uintptr_t)INT_MAX); 12661 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12662 } while ((mp1 = mp1->b_cont) != NULL && 12663 mp1->b_datap->db_type == M_DATA); 12664 } 12665 12666 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12667 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12668 seg_len, tcph); 12669 return; 12670 } 12671 12672 if (sqp != NULL) { 12673 /* 12674 * This is the correct place to update tcp_last_recv_time. Note 12675 * that it is also updated for tcp structure that belongs to 12676 * global and listener queues which do not really need updating. 12677 * But that should not cause any harm. And it is updated for 12678 * all kinds of incoming segments, not only for data segments. 12679 */ 12680 tcp->tcp_last_recv_time = lbolt; 12681 } 12682 12683 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12684 12685 BUMP_LOCAL(tcp->tcp_ibsegs); 12686 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 12687 12688 if ((flags & TH_URG) && sqp != NULL) { 12689 /* 12690 * TCP can't handle urgent pointers that arrive before 12691 * the connection has been accept()ed since it can't 12692 * buffer OOB data. Discard segment if this happens. 12693 * 12694 * We can't just rely on a non-null tcp_listener to indicate 12695 * that the accept() has completed since unlinking of the 12696 * eager and completion of the accept are not atomic. 12697 * tcp_detached, when it is not set (B_FALSE) indicates 12698 * that the accept() has completed. 12699 * 12700 * Nor can it reassemble urgent pointers, so discard 12701 * if it's not the next segment expected. 12702 * 12703 * Otherwise, collapse chain into one mblk (discard if 12704 * that fails). This makes sure the headers, retransmitted 12705 * data, and new data all are in the same mblk. 12706 */ 12707 ASSERT(mp != NULL); 12708 if (tcp->tcp_detached || !pullupmsg(mp, -1)) { 12709 freemsg(mp); 12710 return; 12711 } 12712 /* Update pointers into message */ 12713 iphdr = rptr = mp->b_rptr; 12714 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12715 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12716 /* 12717 * Since we can't handle any data with this urgent 12718 * pointer that is out of sequence, we expunge 12719 * the data. This allows us to still register 12720 * the urgent mark and generate the M_PCSIG, 12721 * which we can do. 12722 */ 12723 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12724 seg_len = 0; 12725 } 12726 } 12727 12728 switch (tcp->tcp_state) { 12729 case TCPS_SYN_SENT: 12730 if (flags & TH_ACK) { 12731 /* 12732 * Note that our stack cannot send data before a 12733 * connection is established, therefore the 12734 * following check is valid. Otherwise, it has 12735 * to be changed. 12736 */ 12737 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12738 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12739 freemsg(mp); 12740 if (flags & TH_RST) 12741 return; 12742 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12743 tcp, seg_ack, 0, TH_RST); 12744 return; 12745 } 12746 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12747 } 12748 if (flags & TH_RST) { 12749 freemsg(mp); 12750 if (flags & TH_ACK) 12751 (void) tcp_clean_death(tcp, 12752 ECONNREFUSED, 13); 12753 return; 12754 } 12755 if (!(flags & TH_SYN)) { 12756 freemsg(mp); 12757 return; 12758 } 12759 12760 /* Process all TCP options. */ 12761 tcp_process_options(tcp, tcph); 12762 /* 12763 * The following changes our rwnd to be a multiple of the 12764 * MIN(peer MSS, our MSS) for performance reason. 12765 */ 12766 (void) tcp_rwnd_set(tcp, 12767 MSS_ROUNDUP(tcp->tcp_recv_hiwater, tcp->tcp_mss)); 12768 12769 /* Is the other end ECN capable? */ 12770 if (tcp->tcp_ecn_ok) { 12771 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12772 tcp->tcp_ecn_ok = B_FALSE; 12773 } 12774 } 12775 /* 12776 * Clear ECN flags because it may interfere with later 12777 * processing. 12778 */ 12779 flags &= ~(TH_ECE|TH_CWR); 12780 12781 tcp->tcp_irs = seg_seq; 12782 tcp->tcp_rack = seg_seq; 12783 tcp->tcp_rnxt = seg_seq + 1; 12784 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12785 if (!TCP_IS_DETACHED(tcp)) { 12786 /* Allocate room for SACK options if needed. */ 12787 if (tcp->tcp_snd_sack_ok) { 12788 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12789 tcp->tcp_hdr_len + 12790 TCPOPT_MAX_SACK_LEN + 12791 (tcp->tcp_loopback ? 0 : 12792 tcps->tcps_wroff_xtra)); 12793 } else { 12794 (void) proto_set_tx_wroff(tcp->tcp_rq, connp, 12795 tcp->tcp_hdr_len + 12796 (tcp->tcp_loopback ? 0 : 12797 tcps->tcps_wroff_xtra)); 12798 } 12799 } 12800 if (flags & TH_ACK) { 12801 /* 12802 * If we can't get the confirmation upstream, pretend 12803 * we didn't even see this one. 12804 * 12805 * XXX: how can we pretend we didn't see it if we 12806 * have updated rnxt et. al. 12807 * 12808 * For loopback we defer sending up the T_CONN_CON 12809 * until after some checks below. 12810 */ 12811 mp1 = NULL; 12812 /* 12813 * tcp_sendmsg() checks tcp_state without entering 12814 * the squeue so tcp_state should be updated before 12815 * sending up connection confirmation 12816 */ 12817 tcp->tcp_state = TCPS_ESTABLISHED; 12818 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12819 tcp->tcp_loopback ? &mp1 : NULL)) { 12820 tcp->tcp_state = TCPS_SYN_SENT; 12821 freemsg(mp); 12822 return; 12823 } 12824 /* SYN was acked - making progress */ 12825 if (tcp->tcp_ipversion == IPV6_VERSION) 12826 tcp->tcp_ip_forward_progress = B_TRUE; 12827 12828 /* One for the SYN */ 12829 tcp->tcp_suna = tcp->tcp_iss + 1; 12830 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12831 12832 /* 12833 * If SYN was retransmitted, need to reset all 12834 * retransmission info. This is because this 12835 * segment will be treated as a dup ACK. 12836 */ 12837 if (tcp->tcp_rexmit) { 12838 tcp->tcp_rexmit = B_FALSE; 12839 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12840 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12841 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12842 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12843 tcp->tcp_ms_we_have_waited = 0; 12844 12845 /* 12846 * Set tcp_cwnd back to 1 MSS, per 12847 * recommendation from 12848 * draft-floyd-incr-init-win-01.txt, 12849 * Increasing TCP's Initial Window. 12850 */ 12851 tcp->tcp_cwnd = tcp->tcp_mss; 12852 } 12853 12854 tcp->tcp_swl1 = seg_seq; 12855 tcp->tcp_swl2 = seg_ack; 12856 12857 new_swnd = BE16_TO_U16(tcph->th_win); 12858 tcp->tcp_swnd = new_swnd; 12859 if (new_swnd > tcp->tcp_max_swnd) 12860 tcp->tcp_max_swnd = new_swnd; 12861 12862 /* 12863 * Always send the three-way handshake ack immediately 12864 * in order to make the connection complete as soon as 12865 * possible on the accepting host. 12866 */ 12867 flags |= TH_ACK_NEEDED; 12868 12869 /* 12870 * Special case for loopback. At this point we have 12871 * received SYN-ACK from the remote endpoint. In 12872 * order to ensure that both endpoints reach the 12873 * fused state prior to any data exchange, the final 12874 * ACK needs to be sent before we indicate T_CONN_CON 12875 * to the module upstream. 12876 */ 12877 if (tcp->tcp_loopback) { 12878 mblk_t *ack_mp; 12879 12880 ASSERT(!tcp->tcp_unfusable); 12881 ASSERT(mp1 != NULL); 12882 /* 12883 * For loopback, we always get a pure SYN-ACK 12884 * and only need to send back the final ACK 12885 * with no data (this is because the other 12886 * tcp is ours and we don't do T/TCP). This 12887 * final ACK triggers the passive side to 12888 * perform fusion in ESTABLISHED state. 12889 */ 12890 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12891 if (tcp->tcp_ack_tid != 0) { 12892 (void) TCP_TIMER_CANCEL(tcp, 12893 tcp->tcp_ack_tid); 12894 tcp->tcp_ack_tid = 0; 12895 } 12896 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12897 BUMP_LOCAL(tcp->tcp_obsegs); 12898 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 12899 12900 if (!IPCL_IS_NONSTR(connp)) { 12901 /* Send up T_CONN_CON */ 12902 putnext(tcp->tcp_rq, mp1); 12903 } else { 12904 cred_t *cr; 12905 pid_t cpid; 12906 12907 cr = msg_getcred(mp1, &cpid); 12908 (*connp->conn_upcalls-> 12909 su_connected) 12910 (connp->conn_upper_handle, 12911 tcp->tcp_connid, cr, cpid); 12912 freemsg(mp1); 12913 } 12914 12915 freemsg(mp); 12916 return; 12917 } 12918 /* 12919 * Forget fusion; we need to handle more 12920 * complex cases below. Send the deferred 12921 * T_CONN_CON message upstream and proceed 12922 * as usual. Mark this tcp as not capable 12923 * of fusion. 12924 */ 12925 TCP_STAT(tcps, tcp_fusion_unfusable); 12926 tcp->tcp_unfusable = B_TRUE; 12927 if (!IPCL_IS_NONSTR(connp)) { 12928 putnext(tcp->tcp_rq, mp1); 12929 } else { 12930 cred_t *cr; 12931 pid_t cpid; 12932 12933 cr = msg_getcred(mp1, &cpid); 12934 (*connp->conn_upcalls->su_connected) 12935 (connp->conn_upper_handle, 12936 tcp->tcp_connid, cr, cpid); 12937 freemsg(mp1); 12938 } 12939 } 12940 12941 /* 12942 * Check to see if there is data to be sent. If 12943 * yes, set the transmit flag. Then check to see 12944 * if received data processing needs to be done. 12945 * If not, go straight to xmit_check. This short 12946 * cut is OK as we don't support T/TCP. 12947 */ 12948 if (tcp->tcp_unsent) 12949 flags |= TH_XMIT_NEEDED; 12950 12951 if (seg_len == 0 && !(flags & TH_URG)) { 12952 freemsg(mp); 12953 goto xmit_check; 12954 } 12955 12956 flags &= ~TH_SYN; 12957 seg_seq++; 12958 break; 12959 } 12960 tcp->tcp_state = TCPS_SYN_RCVD; 12961 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12962 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12963 if (mp1) { 12964 /* 12965 * See comment in tcp_conn_request() for why we use 12966 * the open() time pid here. 12967 */ 12968 DB_CPID(mp1) = tcp->tcp_cpid; 12969 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12970 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12971 } 12972 freemsg(mp); 12973 return; 12974 case TCPS_SYN_RCVD: 12975 if (flags & TH_ACK) { 12976 /* 12977 * In this state, a SYN|ACK packet is either bogus 12978 * because the other side must be ACKing our SYN which 12979 * indicates it has seen the ACK for their SYN and 12980 * shouldn't retransmit it or we're crossing SYNs 12981 * on active open. 12982 */ 12983 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12984 freemsg(mp); 12985 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12986 tcp, seg_ack, 0, TH_RST); 12987 return; 12988 } 12989 /* 12990 * NOTE: RFC 793 pg. 72 says this should be 12991 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12992 * but that would mean we have an ack that ignored 12993 * our SYN. 12994 */ 12995 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12996 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12997 freemsg(mp); 12998 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12999 tcp, seg_ack, 0, TH_RST); 13000 return; 13001 } 13002 } 13003 break; 13004 case TCPS_LISTEN: 13005 /* 13006 * Only a TLI listener can come through this path when a 13007 * acceptor is going back to be a listener and a packet 13008 * for the acceptor hits the classifier. For a socket 13009 * listener, this can never happen because a listener 13010 * can never accept connection on itself and hence a 13011 * socket acceptor can not go back to being a listener. 13012 */ 13013 ASSERT(!TCP_IS_SOCKET(tcp)); 13014 /*FALLTHRU*/ 13015 case TCPS_CLOSED: 13016 case TCPS_BOUND: { 13017 conn_t *new_connp; 13018 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 13019 13020 new_connp = ipcl_classify(mp, connp->conn_zoneid, ipst); 13021 if (new_connp != NULL) { 13022 tcp_reinput(new_connp, mp, connp->conn_sqp); 13023 return; 13024 } 13025 /* We failed to classify. For now just drop the packet */ 13026 freemsg(mp); 13027 return; 13028 } 13029 case TCPS_IDLE: 13030 /* 13031 * Handle the case where the tcp_clean_death() has happened 13032 * on a connection (application hasn't closed yet) but a packet 13033 * was already queued on squeue before tcp_clean_death() 13034 * was processed. Calling tcp_clean_death() twice on same 13035 * connection can result in weird behaviour. 13036 */ 13037 freemsg(mp); 13038 return; 13039 default: 13040 break; 13041 } 13042 13043 /* 13044 * Already on the correct queue/perimeter. 13045 * If this is a detached connection and not an eager 13046 * connection hanging off a listener then new data 13047 * (past the FIN) will cause a reset. 13048 * We do a special check here where it 13049 * is out of the main line, rather than check 13050 * if we are detached every time we see new 13051 * data down below. 13052 */ 13053 if (TCP_IS_DETACHED_NONEAGER(tcp) && 13054 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 13055 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 13056 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 13057 13058 freemsg(mp); 13059 /* 13060 * This could be an SSL closure alert. We're detached so just 13061 * acknowledge it this last time. 13062 */ 13063 if (tcp->tcp_kssl_ctx != NULL) { 13064 kssl_release_ctx(tcp->tcp_kssl_ctx); 13065 tcp->tcp_kssl_ctx = NULL; 13066 13067 tcp->tcp_rnxt += seg_len; 13068 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 13069 flags |= TH_ACK_NEEDED; 13070 goto ack_check; 13071 } 13072 13073 tcp_xmit_ctl("new data when detached", tcp, 13074 tcp->tcp_snxt, 0, TH_RST); 13075 (void) tcp_clean_death(tcp, EPROTO, 12); 13076 return; 13077 } 13078 13079 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 13080 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 13081 new_swnd = BE16_TO_U16(tcph->th_win) << 13082 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 13083 13084 if (tcp->tcp_snd_ts_ok) { 13085 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 13086 /* 13087 * This segment is not acceptable. 13088 * Drop it and send back an ACK. 13089 */ 13090 freemsg(mp); 13091 flags |= TH_ACK_NEEDED; 13092 goto ack_check; 13093 } 13094 } else if (tcp->tcp_snd_sack_ok) { 13095 ASSERT(tcp->tcp_sack_info != NULL); 13096 tcpopt.tcp = tcp; 13097 /* 13098 * SACK info in already updated in tcp_parse_options. Ignore 13099 * all other TCP options... 13100 */ 13101 (void) tcp_parse_options(tcph, &tcpopt); 13102 } 13103 try_again:; 13104 mss = tcp->tcp_mss; 13105 gap = seg_seq - tcp->tcp_rnxt; 13106 rgap = tcp->tcp_rwnd - (gap + seg_len); 13107 /* 13108 * gap is the amount of sequence space between what we expect to see 13109 * and what we got for seg_seq. A positive value for gap means 13110 * something got lost. A negative value means we got some old stuff. 13111 */ 13112 if (gap < 0) { 13113 /* Old stuff present. Is the SYN in there? */ 13114 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 13115 (seg_len != 0)) { 13116 flags &= ~TH_SYN; 13117 seg_seq++; 13118 urp--; 13119 /* Recompute the gaps after noting the SYN. */ 13120 goto try_again; 13121 } 13122 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 13123 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 13124 (seg_len > -gap ? -gap : seg_len)); 13125 /* Remove the old stuff from seg_len. */ 13126 seg_len += gap; 13127 /* 13128 * Anything left? 13129 * Make sure to check for unack'd FIN when rest of data 13130 * has been previously ack'd. 13131 */ 13132 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 13133 /* 13134 * Resets are only valid if they lie within our offered 13135 * window. If the RST bit is set, we just ignore this 13136 * segment. 13137 */ 13138 if (flags & TH_RST) { 13139 freemsg(mp); 13140 return; 13141 } 13142 13143 /* 13144 * The arriving of dup data packets indicate that we 13145 * may have postponed an ack for too long, or the other 13146 * side's RTT estimate is out of shape. Start acking 13147 * more often. 13148 */ 13149 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 13150 tcp->tcp_rack_cnt >= 1 && 13151 tcp->tcp_rack_abs_max > 2) { 13152 tcp->tcp_rack_abs_max--; 13153 } 13154 tcp->tcp_rack_cur_max = 1; 13155 13156 /* 13157 * This segment is "unacceptable". None of its 13158 * sequence space lies within our advertized window. 13159 * 13160 * Adjust seg_len to the original value for tracing. 13161 */ 13162 seg_len -= gap; 13163 if (tcp->tcp_debug) { 13164 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13165 "tcp_rput: unacceptable, gap %d, rgap %d, " 13166 "flags 0x%x, seg_seq %u, seg_ack %u, " 13167 "seg_len %d, rnxt %u, snxt %u, %s", 13168 gap, rgap, flags, seg_seq, seg_ack, 13169 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 13170 tcp_display(tcp, NULL, 13171 DISP_ADDR_AND_PORT)); 13172 } 13173 13174 /* 13175 * Arrange to send an ACK in response to the 13176 * unacceptable segment per RFC 793 page 69. There 13177 * is only one small difference between ours and the 13178 * acceptability test in the RFC - we accept ACK-only 13179 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 13180 * will be generated. 13181 * 13182 * Note that we have to ACK an ACK-only packet at least 13183 * for stacks that send 0-length keep-alives with 13184 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 13185 * section 4.2.3.6. As long as we don't ever generate 13186 * an unacceptable packet in response to an incoming 13187 * packet that is unacceptable, it should not cause 13188 * "ACK wars". 13189 */ 13190 flags |= TH_ACK_NEEDED; 13191 13192 /* 13193 * Continue processing this segment in order to use the 13194 * ACK information it contains, but skip all other 13195 * sequence-number processing. Processing the ACK 13196 * information is necessary in order to 13197 * re-synchronize connections that may have lost 13198 * synchronization. 13199 * 13200 * We clear seg_len and flag fields related to 13201 * sequence number processing as they are not 13202 * to be trusted for an unacceptable segment. 13203 */ 13204 seg_len = 0; 13205 flags &= ~(TH_SYN | TH_FIN | TH_URG); 13206 goto process_ack; 13207 } 13208 13209 /* Fix seg_seq, and chew the gap off the front. */ 13210 seg_seq = tcp->tcp_rnxt; 13211 urp += gap; 13212 do { 13213 mblk_t *mp2; 13214 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13215 (uintptr_t)UINT_MAX); 13216 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13217 if (gap > 0) { 13218 mp->b_rptr = mp->b_wptr - gap; 13219 break; 13220 } 13221 mp2 = mp; 13222 mp = mp->b_cont; 13223 freeb(mp2); 13224 } while (gap < 0); 13225 /* 13226 * If the urgent data has already been acknowledged, we 13227 * should ignore TH_URG below 13228 */ 13229 if (urp < 0) 13230 flags &= ~TH_URG; 13231 } 13232 /* 13233 * rgap is the amount of stuff received out of window. A negative 13234 * value is the amount out of window. 13235 */ 13236 if (rgap < 0) { 13237 mblk_t *mp2; 13238 13239 if (tcp->tcp_rwnd == 0) { 13240 BUMP_MIB(&tcps->tcps_mib, tcpInWinProbe); 13241 } else { 13242 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 13243 UPDATE_MIB(&tcps->tcps_mib, 13244 tcpInDataPastWinBytes, -rgap); 13245 } 13246 13247 /* 13248 * seg_len does not include the FIN, so if more than 13249 * just the FIN is out of window, we act like we don't 13250 * see it. (If just the FIN is out of window, rgap 13251 * will be zero and we will go ahead and acknowledge 13252 * the FIN.) 13253 */ 13254 flags &= ~TH_FIN; 13255 13256 /* Fix seg_len and make sure there is something left. */ 13257 seg_len += rgap; 13258 if (seg_len <= 0) { 13259 /* 13260 * Resets are only valid if they lie within our offered 13261 * window. If the RST bit is set, we just ignore this 13262 * segment. 13263 */ 13264 if (flags & TH_RST) { 13265 freemsg(mp); 13266 return; 13267 } 13268 13269 /* Per RFC 793, we need to send back an ACK. */ 13270 flags |= TH_ACK_NEEDED; 13271 13272 /* 13273 * Send SIGURG as soon as possible i.e. even 13274 * if the TH_URG was delivered in a window probe 13275 * packet (which will be unacceptable). 13276 * 13277 * We generate a signal if none has been generated 13278 * for this connection or if this is a new urgent 13279 * byte. Also send a zero-length "unmarked" message 13280 * to inform SIOCATMARK that this is not the mark. 13281 * 13282 * tcp_urp_last_valid is cleared when the T_exdata_ind 13283 * is sent up. This plus the check for old data 13284 * (gap >= 0) handles the wraparound of the sequence 13285 * number space without having to always track the 13286 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13287 * this max in its rcv_up variable). 13288 * 13289 * This prevents duplicate SIGURGS due to a "late" 13290 * zero-window probe when the T_EXDATA_IND has already 13291 * been sent up. 13292 */ 13293 if ((flags & TH_URG) && 13294 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13295 tcp->tcp_urp_last))) { 13296 if (IPCL_IS_NONSTR(connp)) { 13297 if (!TCP_IS_DETACHED(tcp)) { 13298 (*connp->conn_upcalls-> 13299 su_signal_oob) 13300 (connp->conn_upper_handle, 13301 urp); 13302 } 13303 } else { 13304 mp1 = allocb(0, BPRI_MED); 13305 if (mp1 == NULL) { 13306 freemsg(mp); 13307 return; 13308 } 13309 if (!TCP_IS_DETACHED(tcp) && 13310 !putnextctl1(tcp->tcp_rq, 13311 M_PCSIG, SIGURG)) { 13312 /* Try again on the rexmit. */ 13313 freemsg(mp1); 13314 freemsg(mp); 13315 return; 13316 } 13317 /* 13318 * If the next byte would be the mark 13319 * then mark with MARKNEXT else mark 13320 * with NOTMARKNEXT. 13321 */ 13322 if (gap == 0 && urp == 0) 13323 mp1->b_flag |= MSGMARKNEXT; 13324 else 13325 mp1->b_flag |= MSGNOTMARKNEXT; 13326 freemsg(tcp->tcp_urp_mark_mp); 13327 tcp->tcp_urp_mark_mp = mp1; 13328 flags |= TH_SEND_URP_MARK; 13329 } 13330 tcp->tcp_urp_last_valid = B_TRUE; 13331 tcp->tcp_urp_last = urp + seg_seq; 13332 } 13333 /* 13334 * If this is a zero window probe, continue to 13335 * process the ACK part. But we need to set seg_len 13336 * to 0 to avoid data processing. Otherwise just 13337 * drop the segment and send back an ACK. 13338 */ 13339 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13340 flags &= ~(TH_SYN | TH_URG); 13341 seg_len = 0; 13342 goto process_ack; 13343 } else { 13344 freemsg(mp); 13345 goto ack_check; 13346 } 13347 } 13348 /* Pitch out of window stuff off the end. */ 13349 rgap = seg_len; 13350 mp2 = mp; 13351 do { 13352 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13353 (uintptr_t)INT_MAX); 13354 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13355 if (rgap < 0) { 13356 mp2->b_wptr += rgap; 13357 if ((mp1 = mp2->b_cont) != NULL) { 13358 mp2->b_cont = NULL; 13359 freemsg(mp1); 13360 } 13361 break; 13362 } 13363 } while ((mp2 = mp2->b_cont) != NULL); 13364 } 13365 ok:; 13366 /* 13367 * TCP should check ECN info for segments inside the window only. 13368 * Therefore the check should be done here. 13369 */ 13370 if (tcp->tcp_ecn_ok) { 13371 if (flags & TH_CWR) { 13372 tcp->tcp_ecn_echo_on = B_FALSE; 13373 } 13374 /* 13375 * Note that both ECN_CE and CWR can be set in the 13376 * same segment. In this case, we once again turn 13377 * on ECN_ECHO. 13378 */ 13379 if (tcp->tcp_ipversion == IPV4_VERSION) { 13380 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13381 13382 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13383 tcp->tcp_ecn_echo_on = B_TRUE; 13384 } 13385 } else { 13386 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13387 13388 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13389 htonl(IPH_ECN_CE << 20)) { 13390 tcp->tcp_ecn_echo_on = B_TRUE; 13391 } 13392 } 13393 } 13394 13395 /* 13396 * Check whether we can update tcp_ts_recent. This test is 13397 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13398 * Extensions for High Performance: An Update", Internet Draft. 13399 */ 13400 if (tcp->tcp_snd_ts_ok && 13401 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13402 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13403 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13404 tcp->tcp_last_rcv_lbolt = lbolt64; 13405 } 13406 13407 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13408 /* 13409 * FIN in an out of order segment. We record this in 13410 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13411 * Clear the FIN so that any check on FIN flag will fail. 13412 * Remember that FIN also counts in the sequence number 13413 * space. So we need to ack out of order FIN only segments. 13414 */ 13415 if (flags & TH_FIN) { 13416 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13417 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13418 flags &= ~TH_FIN; 13419 flags |= TH_ACK_NEEDED; 13420 } 13421 if (seg_len > 0) { 13422 /* Fill in the SACK blk list. */ 13423 if (tcp->tcp_snd_sack_ok) { 13424 ASSERT(tcp->tcp_sack_info != NULL); 13425 tcp_sack_insert(tcp->tcp_sack_list, 13426 seg_seq, seg_seq + seg_len, 13427 &(tcp->tcp_num_sack_blk)); 13428 } 13429 13430 /* 13431 * Attempt reassembly and see if we have something 13432 * ready to go. 13433 */ 13434 mp = tcp_reass(tcp, mp, seg_seq); 13435 /* Always ack out of order packets */ 13436 flags |= TH_ACK_NEEDED | TH_PUSH; 13437 if (mp) { 13438 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13439 (uintptr_t)INT_MAX); 13440 seg_len = mp->b_cont ? msgdsize(mp) : 13441 (int)(mp->b_wptr - mp->b_rptr); 13442 seg_seq = tcp->tcp_rnxt; 13443 /* 13444 * A gap is filled and the seq num and len 13445 * of the gap match that of a previously 13446 * received FIN, put the FIN flag back in. 13447 */ 13448 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13449 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13450 flags |= TH_FIN; 13451 tcp->tcp_valid_bits &= 13452 ~TCP_OFO_FIN_VALID; 13453 } 13454 } else { 13455 /* 13456 * Keep going even with NULL mp. 13457 * There may be a useful ACK or something else 13458 * we don't want to miss. 13459 * 13460 * But TCP should not perform fast retransmit 13461 * because of the ack number. TCP uses 13462 * seg_len == 0 to determine if it is a pure 13463 * ACK. And this is not a pure ACK. 13464 */ 13465 seg_len = 0; 13466 ofo_seg = B_TRUE; 13467 } 13468 } 13469 } else if (seg_len > 0) { 13470 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 13471 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 13472 /* 13473 * If an out of order FIN was received before, and the seq 13474 * num and len of the new segment match that of the FIN, 13475 * put the FIN flag back in. 13476 */ 13477 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13478 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13479 flags |= TH_FIN; 13480 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13481 } 13482 } 13483 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13484 if (flags & TH_RST) { 13485 freemsg(mp); 13486 switch (tcp->tcp_state) { 13487 case TCPS_SYN_RCVD: 13488 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13489 break; 13490 case TCPS_ESTABLISHED: 13491 case TCPS_FIN_WAIT_1: 13492 case TCPS_FIN_WAIT_2: 13493 case TCPS_CLOSE_WAIT: 13494 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13495 break; 13496 case TCPS_CLOSING: 13497 case TCPS_LAST_ACK: 13498 (void) tcp_clean_death(tcp, 0, 16); 13499 break; 13500 default: 13501 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13502 (void) tcp_clean_death(tcp, ENXIO, 17); 13503 break; 13504 } 13505 return; 13506 } 13507 if (flags & TH_SYN) { 13508 /* 13509 * See RFC 793, Page 71 13510 * 13511 * The seq number must be in the window as it should 13512 * be "fixed" above. If it is outside window, it should 13513 * be already rejected. Note that we allow seg_seq to be 13514 * rnxt + rwnd because we want to accept 0 window probe. 13515 */ 13516 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13517 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13518 freemsg(mp); 13519 /* 13520 * If the ACK flag is not set, just use our snxt as the 13521 * seq number of the RST segment. 13522 */ 13523 if (!(flags & TH_ACK)) { 13524 seg_ack = tcp->tcp_snxt; 13525 } 13526 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13527 TH_RST|TH_ACK); 13528 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13529 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13530 return; 13531 } 13532 /* 13533 * urp could be -1 when the urp field in the packet is 0 13534 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13535 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13536 */ 13537 if (flags & TH_URG && urp >= 0) { 13538 if (!tcp->tcp_urp_last_valid || 13539 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13540 if (IPCL_IS_NONSTR(connp)) { 13541 if (!TCP_IS_DETACHED(tcp)) { 13542 (*connp->conn_upcalls->su_signal_oob) 13543 (connp->conn_upper_handle, urp); 13544 } 13545 } else { 13546 /* 13547 * If we haven't generated the signal yet for 13548 * this urgent pointer value, do it now. Also, 13549 * send up a zero-length M_DATA indicating 13550 * whether or not this is the mark. The latter 13551 * is not needed when a T_EXDATA_IND is sent up. 13552 * However, if there are allocation failures 13553 * this code relies on the sender retransmitting 13554 * and the socket code for determining the mark 13555 * should not block waiting for the peer to 13556 * transmit. Thus, for simplicity we always 13557 * send up the mark indication. 13558 */ 13559 mp1 = allocb(0, BPRI_MED); 13560 if (mp1 == NULL) { 13561 freemsg(mp); 13562 return; 13563 } 13564 if (!TCP_IS_DETACHED(tcp) && 13565 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13566 SIGURG)) { 13567 /* Try again on the rexmit. */ 13568 freemsg(mp1); 13569 freemsg(mp); 13570 return; 13571 } 13572 /* 13573 * Mark with NOTMARKNEXT for now. 13574 * The code below will change this to MARKNEXT 13575 * if we are at the mark. 13576 * 13577 * If there are allocation failures (e.g. in 13578 * dupmsg below) the next time tcp_rput_data 13579 * sees the urgent segment it will send up the 13580 * MSGMARKNEXT message. 13581 */ 13582 mp1->b_flag |= MSGNOTMARKNEXT; 13583 freemsg(tcp->tcp_urp_mark_mp); 13584 tcp->tcp_urp_mark_mp = mp1; 13585 flags |= TH_SEND_URP_MARK; 13586 #ifdef DEBUG 13587 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13588 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13589 "last %x, %s", 13590 seg_seq, urp, tcp->tcp_urp_last, 13591 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13592 #endif /* DEBUG */ 13593 } 13594 tcp->tcp_urp_last_valid = B_TRUE; 13595 tcp->tcp_urp_last = urp + seg_seq; 13596 } else if (tcp->tcp_urp_mark_mp != NULL) { 13597 /* 13598 * An allocation failure prevented the previous 13599 * tcp_rput_data from sending up the allocated 13600 * MSG*MARKNEXT message - send it up this time 13601 * around. 13602 */ 13603 flags |= TH_SEND_URP_MARK; 13604 } 13605 13606 /* 13607 * If the urgent byte is in this segment, make sure that it is 13608 * all by itself. This makes it much easier to deal with the 13609 * possibility of an allocation failure on the T_exdata_ind. 13610 * Note that seg_len is the number of bytes in the segment, and 13611 * urp is the offset into the segment of the urgent byte. 13612 * urp < seg_len means that the urgent byte is in this segment. 13613 */ 13614 if (urp < seg_len) { 13615 if (seg_len != 1) { 13616 uint32_t tmp_rnxt; 13617 /* 13618 * Break it up and feed it back in. 13619 * Re-attach the IP header. 13620 */ 13621 mp->b_rptr = iphdr; 13622 if (urp > 0) { 13623 /* 13624 * There is stuff before the urgent 13625 * byte. 13626 */ 13627 mp1 = dupmsg(mp); 13628 if (!mp1) { 13629 /* 13630 * Trim from urgent byte on. 13631 * The rest will come back. 13632 */ 13633 (void) adjmsg(mp, 13634 urp - seg_len); 13635 tcp_rput_data(connp, 13636 mp, NULL); 13637 return; 13638 } 13639 (void) adjmsg(mp1, urp - seg_len); 13640 /* Feed this piece back in. */ 13641 tmp_rnxt = tcp->tcp_rnxt; 13642 tcp_rput_data(connp, mp1, NULL); 13643 /* 13644 * If the data passed back in was not 13645 * processed (ie: bad ACK) sending 13646 * the remainder back in will cause a 13647 * loop. In this case, drop the 13648 * packet and let the sender try 13649 * sending a good packet. 13650 */ 13651 if (tmp_rnxt == tcp->tcp_rnxt) { 13652 freemsg(mp); 13653 return; 13654 } 13655 } 13656 if (urp != seg_len - 1) { 13657 uint32_t tmp_rnxt; 13658 /* 13659 * There is stuff after the urgent 13660 * byte. 13661 */ 13662 mp1 = dupmsg(mp); 13663 if (!mp1) { 13664 /* 13665 * Trim everything beyond the 13666 * urgent byte. The rest will 13667 * come back. 13668 */ 13669 (void) adjmsg(mp, 13670 urp + 1 - seg_len); 13671 tcp_rput_data(connp, 13672 mp, NULL); 13673 return; 13674 } 13675 (void) adjmsg(mp1, urp + 1 - seg_len); 13676 tmp_rnxt = tcp->tcp_rnxt; 13677 tcp_rput_data(connp, mp1, NULL); 13678 /* 13679 * If the data passed back in was not 13680 * processed (ie: bad ACK) sending 13681 * the remainder back in will cause a 13682 * loop. In this case, drop the 13683 * packet and let the sender try 13684 * sending a good packet. 13685 */ 13686 if (tmp_rnxt == tcp->tcp_rnxt) { 13687 freemsg(mp); 13688 return; 13689 } 13690 } 13691 tcp_rput_data(connp, mp, NULL); 13692 return; 13693 } 13694 /* 13695 * This segment contains only the urgent byte. We 13696 * have to allocate the T_exdata_ind, if we can. 13697 */ 13698 if (IPCL_IS_NONSTR(connp)) { 13699 int error; 13700 13701 (*connp->conn_upcalls->su_recv) 13702 (connp->conn_upper_handle, mp, seg_len, 13703 MSG_OOB, &error, NULL); 13704 /* 13705 * We should never be in middle of a 13706 * fallback, the squeue guarantees that. 13707 */ 13708 ASSERT(error != EOPNOTSUPP); 13709 mp = NULL; 13710 goto update_ack; 13711 } else if (!tcp->tcp_urp_mp) { 13712 struct T_exdata_ind *tei; 13713 mp1 = allocb(sizeof (struct T_exdata_ind), 13714 BPRI_MED); 13715 if (!mp1) { 13716 /* 13717 * Sigh... It'll be back. 13718 * Generate any MSG*MARK message now. 13719 */ 13720 freemsg(mp); 13721 seg_len = 0; 13722 if (flags & TH_SEND_URP_MARK) { 13723 13724 13725 ASSERT(tcp->tcp_urp_mark_mp); 13726 tcp->tcp_urp_mark_mp->b_flag &= 13727 ~MSGNOTMARKNEXT; 13728 tcp->tcp_urp_mark_mp->b_flag |= 13729 MSGMARKNEXT; 13730 } 13731 goto ack_check; 13732 } 13733 mp1->b_datap->db_type = M_PROTO; 13734 tei = (struct T_exdata_ind *)mp1->b_rptr; 13735 tei->PRIM_type = T_EXDATA_IND; 13736 tei->MORE_flag = 0; 13737 mp1->b_wptr = (uchar_t *)&tei[1]; 13738 tcp->tcp_urp_mp = mp1; 13739 #ifdef DEBUG 13740 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13741 "tcp_rput: allocated exdata_ind %s", 13742 tcp_display(tcp, NULL, 13743 DISP_PORT_ONLY)); 13744 #endif /* DEBUG */ 13745 /* 13746 * There is no need to send a separate MSG*MARK 13747 * message since the T_EXDATA_IND will be sent 13748 * now. 13749 */ 13750 flags &= ~TH_SEND_URP_MARK; 13751 freemsg(tcp->tcp_urp_mark_mp); 13752 tcp->tcp_urp_mark_mp = NULL; 13753 } 13754 /* 13755 * Now we are all set. On the next putnext upstream, 13756 * tcp_urp_mp will be non-NULL and will get prepended 13757 * to what has to be this piece containing the urgent 13758 * byte. If for any reason we abort this segment below, 13759 * if it comes back, we will have this ready, or it 13760 * will get blown off in close. 13761 */ 13762 } else if (urp == seg_len) { 13763 /* 13764 * The urgent byte is the next byte after this sequence 13765 * number. If there is data it is marked with 13766 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13767 * since it is not needed. Otherwise, if the code 13768 * above just allocated a zero-length tcp_urp_mark_mp 13769 * message, that message is tagged with MSGMARKNEXT. 13770 * Sending up these MSGMARKNEXT messages makes 13771 * SIOCATMARK work correctly even though 13772 * the T_EXDATA_IND will not be sent up until the 13773 * urgent byte arrives. 13774 */ 13775 if (seg_len != 0) { 13776 flags |= TH_MARKNEXT_NEEDED; 13777 freemsg(tcp->tcp_urp_mark_mp); 13778 tcp->tcp_urp_mark_mp = NULL; 13779 flags &= ~TH_SEND_URP_MARK; 13780 } else if (tcp->tcp_urp_mark_mp != NULL) { 13781 flags |= TH_SEND_URP_MARK; 13782 tcp->tcp_urp_mark_mp->b_flag &= 13783 ~MSGNOTMARKNEXT; 13784 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13785 } 13786 #ifdef DEBUG 13787 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13788 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13789 seg_len, flags, 13790 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13791 #endif /* DEBUG */ 13792 } 13793 #ifdef DEBUG 13794 else { 13795 /* Data left until we hit mark */ 13796 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13797 "tcp_rput: URP %d bytes left, %s", 13798 urp - seg_len, tcp_display(tcp, NULL, 13799 DISP_PORT_ONLY)); 13800 } 13801 #endif /* DEBUG */ 13802 } 13803 13804 process_ack: 13805 if (!(flags & TH_ACK)) { 13806 freemsg(mp); 13807 goto xmit_check; 13808 } 13809 } 13810 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13811 13812 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13813 tcp->tcp_ip_forward_progress = B_TRUE; 13814 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13815 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13816 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13817 /* 3-way handshake complete - pass up the T_CONN_IND */ 13818 tcp_t *listener = tcp->tcp_listener; 13819 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13820 13821 tcp->tcp_tconnind_started = B_TRUE; 13822 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13823 /* 13824 * We are here means eager is fine but it can 13825 * get a TH_RST at any point between now and till 13826 * accept completes and disappear. We need to 13827 * ensure that reference to eager is valid after 13828 * we get out of eager's perimeter. So we do 13829 * an extra refhold. 13830 */ 13831 CONN_INC_REF(connp); 13832 13833 /* 13834 * The listener also exists because of the refhold 13835 * done in tcp_conn_request. Its possible that it 13836 * might have closed. We will check that once we 13837 * get inside listeners context. 13838 */ 13839 CONN_INC_REF(listener->tcp_connp); 13840 if (listener->tcp_connp->conn_sqp == 13841 connp->conn_sqp) { 13842 /* 13843 * We optimize by not calling an SQUEUE_ENTER 13844 * on the listener since we know that the 13845 * listener and eager squeues are the same. 13846 * We are able to make this check safely only 13847 * because neither the eager nor the listener 13848 * can change its squeue. Only an active connect 13849 * can change its squeue 13850 */ 13851 tcp_send_conn_ind(listener->tcp_connp, mp, 13852 listener->tcp_connp->conn_sqp); 13853 CONN_DEC_REF(listener->tcp_connp); 13854 } else if (!tcp->tcp_loopback) { 13855 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13856 mp, tcp_send_conn_ind, 13857 listener->tcp_connp, SQ_FILL, 13858 SQTAG_TCP_CONN_IND); 13859 } else { 13860 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, 13861 mp, tcp_send_conn_ind, 13862 listener->tcp_connp, SQ_PROCESS, 13863 SQTAG_TCP_CONN_IND); 13864 } 13865 } 13866 13867 /* 13868 * We are seeing the final ack in the three way 13869 * hand shake of a active open'ed connection 13870 * so we must send up a T_CONN_CON 13871 * 13872 * tcp_sendmsg() checks tcp_state without entering 13873 * the squeue so tcp_state should be updated before 13874 * sending up connection confirmation. 13875 */ 13876 tcp->tcp_state = TCPS_ESTABLISHED; 13877 if (tcp->tcp_active_open) { 13878 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13879 freemsg(mp); 13880 tcp->tcp_state = TCPS_SYN_RCVD; 13881 return; 13882 } 13883 /* 13884 * Don't fuse the loopback endpoints for 13885 * simultaneous active opens. 13886 */ 13887 if (tcp->tcp_loopback) { 13888 TCP_STAT(tcps, tcp_fusion_unfusable); 13889 tcp->tcp_unfusable = B_TRUE; 13890 } 13891 } 13892 13893 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13894 bytes_acked--; 13895 /* SYN was acked - making progress */ 13896 if (tcp->tcp_ipversion == IPV6_VERSION) 13897 tcp->tcp_ip_forward_progress = B_TRUE; 13898 13899 /* 13900 * If SYN was retransmitted, need to reset all 13901 * retransmission info as this segment will be 13902 * treated as a dup ACK. 13903 */ 13904 if (tcp->tcp_rexmit) { 13905 tcp->tcp_rexmit = B_FALSE; 13906 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13907 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13908 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13909 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13910 tcp->tcp_ms_we_have_waited = 0; 13911 tcp->tcp_cwnd = mss; 13912 } 13913 13914 /* 13915 * We set the send window to zero here. 13916 * This is needed if there is data to be 13917 * processed already on the queue. 13918 * Later (at swnd_update label), the 13919 * "new_swnd > tcp_swnd" condition is satisfied 13920 * the XMIT_NEEDED flag is set in the current 13921 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13922 * called if there is already data on queue in 13923 * this state. 13924 */ 13925 tcp->tcp_swnd = 0; 13926 13927 if (new_swnd > tcp->tcp_max_swnd) 13928 tcp->tcp_max_swnd = new_swnd; 13929 tcp->tcp_swl1 = seg_seq; 13930 tcp->tcp_swl2 = seg_ack; 13931 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13932 13933 /* Fuse when both sides are in ESTABLISHED state */ 13934 if (tcp->tcp_loopback && do_tcp_fusion) 13935 tcp_fuse(tcp, iphdr, tcph); 13936 13937 } 13938 /* This code follows 4.4BSD-Lite2 mostly. */ 13939 if (bytes_acked < 0) 13940 goto est; 13941 13942 /* 13943 * If TCP is ECN capable and the congestion experience bit is 13944 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13945 * done once per window (or more loosely, per RTT). 13946 */ 13947 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13948 tcp->tcp_cwr = B_FALSE; 13949 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13950 if (!tcp->tcp_cwr) { 13951 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13952 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13953 tcp->tcp_cwnd = npkt * mss; 13954 /* 13955 * If the cwnd is 0, use the timer to clock out 13956 * new segments. This is required by the ECN spec. 13957 */ 13958 if (npkt == 0) { 13959 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13960 /* 13961 * This makes sure that when the ACK comes 13962 * back, we will increase tcp_cwnd by 1 MSS. 13963 */ 13964 tcp->tcp_cwnd_cnt = 0; 13965 } 13966 tcp->tcp_cwr = B_TRUE; 13967 /* 13968 * This marks the end of the current window of in 13969 * flight data. That is why we don't use 13970 * tcp_suna + tcp_swnd. Only data in flight can 13971 * provide ECN info. 13972 */ 13973 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13974 tcp->tcp_ecn_cwr_sent = B_FALSE; 13975 } 13976 } 13977 13978 mp1 = tcp->tcp_xmit_head; 13979 if (bytes_acked == 0) { 13980 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13981 int dupack_cnt; 13982 13983 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 13984 /* 13985 * Fast retransmit. When we have seen exactly three 13986 * identical ACKs while we have unacked data 13987 * outstanding we take it as a hint that our peer 13988 * dropped something. 13989 * 13990 * If TCP is retransmitting, don't do fast retransmit. 13991 */ 13992 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13993 ! tcp->tcp_rexmit) { 13994 /* Do Limited Transmit */ 13995 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13996 tcps->tcps_dupack_fast_retransmit) { 13997 /* 13998 * RFC 3042 13999 * 14000 * What we need to do is temporarily 14001 * increase tcp_cwnd so that new 14002 * data can be sent if it is allowed 14003 * by the receive window (tcp_rwnd). 14004 * tcp_wput_data() will take care of 14005 * the rest. 14006 * 14007 * If the connection is SACK capable, 14008 * only do limited xmit when there 14009 * is SACK info. 14010 * 14011 * Note how tcp_cwnd is incremented. 14012 * The first dup ACK will increase 14013 * it by 1 MSS. The second dup ACK 14014 * will increase it by 2 MSS. This 14015 * means that only 1 new segment will 14016 * be sent for each dup ACK. 14017 */ 14018 if (tcp->tcp_unsent > 0 && 14019 (!tcp->tcp_snd_sack_ok || 14020 (tcp->tcp_snd_sack_ok && 14021 tcp->tcp_notsack_list != NULL))) { 14022 tcp->tcp_cwnd += mss << 14023 (tcp->tcp_dupack_cnt - 1); 14024 flags |= TH_LIMIT_XMIT; 14025 } 14026 } else if (dupack_cnt == 14027 tcps->tcps_dupack_fast_retransmit) { 14028 14029 /* 14030 * If we have reduced tcp_ssthresh 14031 * because of ECN, do not reduce it again 14032 * unless it is already one window of data 14033 * away. After one window of data, tcp_cwr 14034 * should then be cleared. Note that 14035 * for non ECN capable connection, tcp_cwr 14036 * should always be false. 14037 * 14038 * Adjust cwnd since the duplicate 14039 * ack indicates that a packet was 14040 * dropped (due to congestion.) 14041 */ 14042 if (!tcp->tcp_cwr) { 14043 npkt = ((tcp->tcp_snxt - 14044 tcp->tcp_suna) >> 1) / mss; 14045 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 14046 mss; 14047 tcp->tcp_cwnd = (npkt + 14048 tcp->tcp_dupack_cnt) * mss; 14049 } 14050 if (tcp->tcp_ecn_ok) { 14051 tcp->tcp_cwr = B_TRUE; 14052 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 14053 tcp->tcp_ecn_cwr_sent = B_FALSE; 14054 } 14055 14056 /* 14057 * We do Hoe's algorithm. Refer to her 14058 * paper "Improving the Start-up Behavior 14059 * of a Congestion Control Scheme for TCP," 14060 * appeared in SIGCOMM'96. 14061 * 14062 * Save highest seq no we have sent so far. 14063 * Be careful about the invisible FIN byte. 14064 */ 14065 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 14066 (tcp->tcp_unsent == 0)) { 14067 tcp->tcp_rexmit_max = tcp->tcp_fss; 14068 } else { 14069 tcp->tcp_rexmit_max = tcp->tcp_snxt; 14070 } 14071 14072 /* 14073 * Do not allow bursty traffic during. 14074 * fast recovery. Refer to Fall and Floyd's 14075 * paper "Simulation-based Comparisons of 14076 * Tahoe, Reno and SACK TCP" (in CCR?) 14077 * This is a best current practise. 14078 */ 14079 tcp->tcp_snd_burst = TCP_CWND_SS; 14080 14081 /* 14082 * For SACK: 14083 * Calculate tcp_pipe, which is the 14084 * estimated number of bytes in 14085 * network. 14086 * 14087 * tcp_fack is the highest sack'ed seq num 14088 * TCP has received. 14089 * 14090 * tcp_pipe is explained in the above quoted 14091 * Fall and Floyd's paper. tcp_fack is 14092 * explained in Mathis and Mahdavi's 14093 * "Forward Acknowledgment: Refining TCP 14094 * Congestion Control" in SIGCOMM '96. 14095 */ 14096 if (tcp->tcp_snd_sack_ok) { 14097 ASSERT(tcp->tcp_sack_info != NULL); 14098 if (tcp->tcp_notsack_list != NULL) { 14099 tcp->tcp_pipe = tcp->tcp_snxt - 14100 tcp->tcp_fack; 14101 tcp->tcp_sack_snxt = seg_ack; 14102 flags |= TH_NEED_SACK_REXMIT; 14103 } else { 14104 /* 14105 * Always initialize tcp_pipe 14106 * even though we don't have 14107 * any SACK info. If later 14108 * we get SACK info and 14109 * tcp_pipe is not initialized, 14110 * funny things will happen. 14111 */ 14112 tcp->tcp_pipe = 14113 tcp->tcp_cwnd_ssthresh; 14114 } 14115 } else { 14116 flags |= TH_REXMIT_NEEDED; 14117 } /* tcp_snd_sack_ok */ 14118 14119 } else { 14120 /* 14121 * Here we perform congestion 14122 * avoidance, but NOT slow start. 14123 * This is known as the Fast 14124 * Recovery Algorithm. 14125 */ 14126 if (tcp->tcp_snd_sack_ok && 14127 tcp->tcp_notsack_list != NULL) { 14128 flags |= TH_NEED_SACK_REXMIT; 14129 tcp->tcp_pipe -= mss; 14130 if (tcp->tcp_pipe < 0) 14131 tcp->tcp_pipe = 0; 14132 } else { 14133 /* 14134 * We know that one more packet has 14135 * left the pipe thus we can update 14136 * cwnd. 14137 */ 14138 cwnd = tcp->tcp_cwnd + mss; 14139 if (cwnd > tcp->tcp_cwnd_max) 14140 cwnd = tcp->tcp_cwnd_max; 14141 tcp->tcp_cwnd = cwnd; 14142 if (tcp->tcp_unsent > 0) 14143 flags |= TH_XMIT_NEEDED; 14144 } 14145 } 14146 } 14147 } else if (tcp->tcp_zero_win_probe) { 14148 /* 14149 * If the window has opened, need to arrange 14150 * to send additional data. 14151 */ 14152 if (new_swnd != 0) { 14153 /* tcp_suna != tcp_snxt */ 14154 /* Packet contains a window update */ 14155 BUMP_MIB(&tcps->tcps_mib, tcpInWinUpdate); 14156 tcp->tcp_zero_win_probe = 0; 14157 tcp->tcp_timer_backoff = 0; 14158 tcp->tcp_ms_we_have_waited = 0; 14159 14160 /* 14161 * Transmit starting with tcp_suna since 14162 * the one byte probe is not ack'ed. 14163 * If TCP has sent more than one identical 14164 * probe, tcp_rexmit will be set. That means 14165 * tcp_ss_rexmit() will send out the one 14166 * byte along with new data. Otherwise, 14167 * fake the retransmission. 14168 */ 14169 flags |= TH_XMIT_NEEDED; 14170 if (!tcp->tcp_rexmit) { 14171 tcp->tcp_rexmit = B_TRUE; 14172 tcp->tcp_dupack_cnt = 0; 14173 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 14174 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 14175 } 14176 } 14177 } 14178 goto swnd_update; 14179 } 14180 14181 /* 14182 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 14183 * If the ACK value acks something that we have not yet sent, it might 14184 * be an old duplicate segment. Send an ACK to re-synchronize the 14185 * other side. 14186 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 14187 * state is handled above, so we can always just drop the segment and 14188 * send an ACK here. 14189 * 14190 * Should we send ACKs in response to ACK only segments? 14191 */ 14192 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 14193 BUMP_MIB(&tcps->tcps_mib, tcpInAckUnsent); 14194 /* drop the received segment */ 14195 freemsg(mp); 14196 14197 /* 14198 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 14199 * greater than 0, check if the number of such 14200 * bogus ACks is greater than that count. If yes, 14201 * don't send back any ACK. This prevents TCP from 14202 * getting into an ACK storm if somehow an attacker 14203 * successfully spoofs an acceptable segment to our 14204 * peer. 14205 */ 14206 if (tcp_drop_ack_unsent_cnt > 0 && 14207 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 14208 TCP_STAT(tcps, tcp_in_ack_unsent_drop); 14209 return; 14210 } 14211 mp = tcp_ack_mp(tcp); 14212 if (mp != NULL) { 14213 BUMP_LOCAL(tcp->tcp_obsegs); 14214 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 14215 tcp_send_data(tcp, tcp->tcp_wq, mp); 14216 } 14217 return; 14218 } 14219 14220 /* 14221 * TCP gets a new ACK, update the notsack'ed list to delete those 14222 * blocks that are covered by this ACK. 14223 */ 14224 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 14225 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 14226 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 14227 } 14228 14229 /* 14230 * If we got an ACK after fast retransmit, check to see 14231 * if it is a partial ACK. If it is not and the congestion 14232 * window was inflated to account for the other side's 14233 * cached packets, retract it. If it is, do Hoe's algorithm. 14234 */ 14235 if (tcp->tcp_dupack_cnt >= tcps->tcps_dupack_fast_retransmit) { 14236 ASSERT(tcp->tcp_rexmit == B_FALSE); 14237 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 14238 tcp->tcp_dupack_cnt = 0; 14239 /* 14240 * Restore the orig tcp_cwnd_ssthresh after 14241 * fast retransmit phase. 14242 */ 14243 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 14244 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 14245 } 14246 tcp->tcp_rexmit_max = seg_ack; 14247 tcp->tcp_cwnd_cnt = 0; 14248 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14249 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14250 14251 /* 14252 * Remove all notsack info to avoid confusion with 14253 * the next fast retrasnmit/recovery phase. 14254 */ 14255 if (tcp->tcp_snd_sack_ok && 14256 tcp->tcp_notsack_list != NULL) { 14257 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 14258 } 14259 } else { 14260 if (tcp->tcp_snd_sack_ok && 14261 tcp->tcp_notsack_list != NULL) { 14262 flags |= TH_NEED_SACK_REXMIT; 14263 tcp->tcp_pipe -= mss; 14264 if (tcp->tcp_pipe < 0) 14265 tcp->tcp_pipe = 0; 14266 } else { 14267 /* 14268 * Hoe's algorithm: 14269 * 14270 * Retransmit the unack'ed segment and 14271 * restart fast recovery. Note that we 14272 * need to scale back tcp_cwnd to the 14273 * original value when we started fast 14274 * recovery. This is to prevent overly 14275 * aggressive behaviour in sending new 14276 * segments. 14277 */ 14278 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14279 tcps->tcps_dupack_fast_retransmit * mss; 14280 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14281 flags |= TH_REXMIT_NEEDED; 14282 } 14283 } 14284 } else { 14285 tcp->tcp_dupack_cnt = 0; 14286 if (tcp->tcp_rexmit) { 14287 /* 14288 * TCP is retranmitting. If the ACK ack's all 14289 * outstanding data, update tcp_rexmit_max and 14290 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14291 * to the correct value. 14292 * 14293 * Note that SEQ_LEQ() is used. This is to avoid 14294 * unnecessary fast retransmit caused by dup ACKs 14295 * received when TCP does slow start retransmission 14296 * after a time out. During this phase, TCP may 14297 * send out segments which are already received. 14298 * This causes dup ACKs to be sent back. 14299 */ 14300 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14301 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14302 tcp->tcp_rexmit_nxt = seg_ack; 14303 } 14304 if (seg_ack != tcp->tcp_rexmit_max) { 14305 flags |= TH_XMIT_NEEDED; 14306 } 14307 } else { 14308 tcp->tcp_rexmit = B_FALSE; 14309 tcp->tcp_xmit_zc_clean = B_FALSE; 14310 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14311 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14312 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14313 } 14314 tcp->tcp_ms_we_have_waited = 0; 14315 } 14316 } 14317 14318 BUMP_MIB(&tcps->tcps_mib, tcpInAckSegs); 14319 UPDATE_MIB(&tcps->tcps_mib, tcpInAckBytes, bytes_acked); 14320 tcp->tcp_suna = seg_ack; 14321 if (tcp->tcp_zero_win_probe != 0) { 14322 tcp->tcp_zero_win_probe = 0; 14323 tcp->tcp_timer_backoff = 0; 14324 } 14325 14326 /* 14327 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14328 * Note that it cannot be the SYN being ack'ed. The code flow 14329 * will not reach here. 14330 */ 14331 if (mp1 == NULL) { 14332 goto fin_acked; 14333 } 14334 14335 /* 14336 * Update the congestion window. 14337 * 14338 * If TCP is not ECN capable or TCP is ECN capable but the 14339 * congestion experience bit is not set, increase the tcp_cwnd as 14340 * usual. 14341 */ 14342 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14343 cwnd = tcp->tcp_cwnd; 14344 add = mss; 14345 14346 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14347 /* 14348 * This is to prevent an increase of less than 1 MSS of 14349 * tcp_cwnd. With partial increase, tcp_wput_data() 14350 * may send out tinygrams in order to preserve mblk 14351 * boundaries. 14352 * 14353 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14354 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14355 * increased by 1 MSS for every RTTs. 14356 */ 14357 if (tcp->tcp_cwnd_cnt <= 0) { 14358 tcp->tcp_cwnd_cnt = cwnd + add; 14359 } else { 14360 tcp->tcp_cwnd_cnt -= add; 14361 add = 0; 14362 } 14363 } 14364 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14365 } 14366 14367 /* See if the latest urgent data has been acknowledged */ 14368 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14369 SEQ_GT(seg_ack, tcp->tcp_urg)) 14370 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14371 14372 /* Can we update the RTT estimates? */ 14373 if (tcp->tcp_snd_ts_ok) { 14374 /* Ignore zero timestamp echo-reply. */ 14375 if (tcpopt.tcp_opt_ts_ecr != 0) { 14376 tcp_set_rto(tcp, (int32_t)lbolt - 14377 (int32_t)tcpopt.tcp_opt_ts_ecr); 14378 } 14379 14380 /* If needed, restart the timer. */ 14381 if (tcp->tcp_set_timer == 1) { 14382 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14383 tcp->tcp_set_timer = 0; 14384 } 14385 /* 14386 * Update tcp_csuna in case the other side stops sending 14387 * us timestamps. 14388 */ 14389 tcp->tcp_csuna = tcp->tcp_snxt; 14390 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14391 /* 14392 * An ACK sequence we haven't seen before, so get the RTT 14393 * and update the RTO. But first check if the timestamp is 14394 * valid to use. 14395 */ 14396 if ((mp1->b_next != NULL) && 14397 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14398 tcp_set_rto(tcp, (int32_t)lbolt - 14399 (int32_t)(intptr_t)mp1->b_prev); 14400 else 14401 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14402 14403 /* Remeber the last sequence to be ACKed */ 14404 tcp->tcp_csuna = seg_ack; 14405 if (tcp->tcp_set_timer == 1) { 14406 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14407 tcp->tcp_set_timer = 0; 14408 } 14409 } else { 14410 BUMP_MIB(&tcps->tcps_mib, tcpRttNoUpdate); 14411 } 14412 14413 /* Eat acknowledged bytes off the xmit queue. */ 14414 for (;;) { 14415 mblk_t *mp2; 14416 uchar_t *wptr; 14417 14418 wptr = mp1->b_wptr; 14419 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14420 bytes_acked -= (int)(wptr - mp1->b_rptr); 14421 if (bytes_acked < 0) { 14422 mp1->b_rptr = wptr + bytes_acked; 14423 /* 14424 * Set a new timestamp if all the bytes timed by the 14425 * old timestamp have been ack'ed. 14426 */ 14427 if (SEQ_GT(seg_ack, 14428 (uint32_t)(uintptr_t)(mp1->b_next))) { 14429 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14430 mp1->b_next = NULL; 14431 } 14432 break; 14433 } 14434 mp1->b_next = NULL; 14435 mp1->b_prev = NULL; 14436 mp2 = mp1; 14437 mp1 = mp1->b_cont; 14438 14439 /* 14440 * This notification is required for some zero-copy 14441 * clients to maintain a copy semantic. After the data 14442 * is ack'ed, client is safe to modify or reuse the buffer. 14443 */ 14444 if (tcp->tcp_snd_zcopy_aware && 14445 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14446 tcp_zcopy_notify(tcp); 14447 freeb(mp2); 14448 if (bytes_acked == 0) { 14449 if (mp1 == NULL) { 14450 /* Everything is ack'ed, clear the tail. */ 14451 tcp->tcp_xmit_tail = NULL; 14452 /* 14453 * Cancel the timer unless we are still 14454 * waiting for an ACK for the FIN packet. 14455 */ 14456 if (tcp->tcp_timer_tid != 0 && 14457 tcp->tcp_snxt == tcp->tcp_suna) { 14458 (void) TCP_TIMER_CANCEL(tcp, 14459 tcp->tcp_timer_tid); 14460 tcp->tcp_timer_tid = 0; 14461 } 14462 goto pre_swnd_update; 14463 } 14464 if (mp2 != tcp->tcp_xmit_tail) 14465 break; 14466 tcp->tcp_xmit_tail = mp1; 14467 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14468 (uintptr_t)INT_MAX); 14469 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14470 mp1->b_rptr); 14471 break; 14472 } 14473 if (mp1 == NULL) { 14474 /* 14475 * More was acked but there is nothing more 14476 * outstanding. This means that the FIN was 14477 * just acked or that we're talking to a clown. 14478 */ 14479 fin_acked: 14480 ASSERT(tcp->tcp_fin_sent); 14481 tcp->tcp_xmit_tail = NULL; 14482 if (tcp->tcp_fin_sent) { 14483 /* FIN was acked - making progress */ 14484 if (tcp->tcp_ipversion == IPV6_VERSION && 14485 !tcp->tcp_fin_acked) 14486 tcp->tcp_ip_forward_progress = B_TRUE; 14487 tcp->tcp_fin_acked = B_TRUE; 14488 if (tcp->tcp_linger_tid != 0 && 14489 TCP_TIMER_CANCEL(tcp, 14490 tcp->tcp_linger_tid) >= 0) { 14491 tcp_stop_lingering(tcp); 14492 freemsg(mp); 14493 mp = NULL; 14494 } 14495 } else { 14496 /* 14497 * We should never get here because 14498 * we have already checked that the 14499 * number of bytes ack'ed should be 14500 * smaller than or equal to what we 14501 * have sent so far (it is the 14502 * acceptability check of the ACK). 14503 * We can only get here if the send 14504 * queue is corrupted. 14505 * 14506 * Terminate the connection and 14507 * panic the system. It is better 14508 * for us to panic instead of 14509 * continuing to avoid other disaster. 14510 */ 14511 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14512 tcp->tcp_rnxt, TH_RST|TH_ACK); 14513 panic("Memory corruption " 14514 "detected for connection %s.", 14515 tcp_display(tcp, NULL, 14516 DISP_ADDR_AND_PORT)); 14517 /*NOTREACHED*/ 14518 } 14519 goto pre_swnd_update; 14520 } 14521 ASSERT(mp2 != tcp->tcp_xmit_tail); 14522 } 14523 if (tcp->tcp_unsent) { 14524 flags |= TH_XMIT_NEEDED; 14525 } 14526 pre_swnd_update: 14527 tcp->tcp_xmit_head = mp1; 14528 swnd_update: 14529 /* 14530 * The following check is different from most other implementations. 14531 * For bi-directional transfer, when segments are dropped, the 14532 * "normal" check will not accept a window update in those 14533 * retransmitted segemnts. Failing to do that, TCP may send out 14534 * segments which are outside receiver's window. As TCP accepts 14535 * the ack in those retransmitted segments, if the window update in 14536 * the same segment is not accepted, TCP will incorrectly calculates 14537 * that it can send more segments. This can create a deadlock 14538 * with the receiver if its window becomes zero. 14539 */ 14540 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14541 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14542 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14543 /* 14544 * The criteria for update is: 14545 * 14546 * 1. the segment acknowledges some data. Or 14547 * 2. the segment is new, i.e. it has a higher seq num. Or 14548 * 3. the segment is not old and the advertised window is 14549 * larger than the previous advertised window. 14550 */ 14551 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14552 flags |= TH_XMIT_NEEDED; 14553 tcp->tcp_swnd = new_swnd; 14554 if (new_swnd > tcp->tcp_max_swnd) 14555 tcp->tcp_max_swnd = new_swnd; 14556 tcp->tcp_swl1 = seg_seq; 14557 tcp->tcp_swl2 = seg_ack; 14558 } 14559 est: 14560 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14561 14562 switch (tcp->tcp_state) { 14563 case TCPS_FIN_WAIT_1: 14564 if (tcp->tcp_fin_acked) { 14565 tcp->tcp_state = TCPS_FIN_WAIT_2; 14566 /* 14567 * We implement the non-standard BSD/SunOS 14568 * FIN_WAIT_2 flushing algorithm. 14569 * If there is no user attached to this 14570 * TCP endpoint, then this TCP struct 14571 * could hang around forever in FIN_WAIT_2 14572 * state if the peer forgets to send us 14573 * a FIN. To prevent this, we wait only 14574 * 2*MSL (a convenient time value) for 14575 * the FIN to arrive. If it doesn't show up, 14576 * we flush the TCP endpoint. This algorithm, 14577 * though a violation of RFC-793, has worked 14578 * for over 10 years in BSD systems. 14579 * Note: SunOS 4.x waits 675 seconds before 14580 * flushing the FIN_WAIT_2 connection. 14581 */ 14582 TCP_TIMER_RESTART(tcp, 14583 tcps->tcps_fin_wait_2_flush_interval); 14584 } 14585 break; 14586 case TCPS_FIN_WAIT_2: 14587 break; /* Shutdown hook? */ 14588 case TCPS_LAST_ACK: 14589 freemsg(mp); 14590 if (tcp->tcp_fin_acked) { 14591 (void) tcp_clean_death(tcp, 0, 19); 14592 return; 14593 } 14594 goto xmit_check; 14595 case TCPS_CLOSING: 14596 if (tcp->tcp_fin_acked) { 14597 tcp->tcp_state = TCPS_TIME_WAIT; 14598 /* 14599 * Unconditionally clear the exclusive binding 14600 * bit so this TIME-WAIT connection won't 14601 * interfere with new ones. 14602 */ 14603 tcp->tcp_exclbind = 0; 14604 if (!TCP_IS_DETACHED(tcp)) { 14605 TCP_TIMER_RESTART(tcp, 14606 tcps->tcps_time_wait_interval); 14607 } else { 14608 tcp_time_wait_append(tcp); 14609 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14610 } 14611 } 14612 /*FALLTHRU*/ 14613 case TCPS_CLOSE_WAIT: 14614 freemsg(mp); 14615 goto xmit_check; 14616 default: 14617 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14618 break; 14619 } 14620 } 14621 if (flags & TH_FIN) { 14622 /* Make sure we ack the fin */ 14623 flags |= TH_ACK_NEEDED; 14624 if (!tcp->tcp_fin_rcvd) { 14625 tcp->tcp_fin_rcvd = B_TRUE; 14626 tcp->tcp_rnxt++; 14627 tcph = tcp->tcp_tcph; 14628 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14629 14630 /* 14631 * Generate the ordrel_ind at the end unless we 14632 * are an eager guy. 14633 * In the eager case tcp_rsrv will do this when run 14634 * after tcp_accept is done. 14635 */ 14636 if (tcp->tcp_listener == NULL && 14637 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14638 flags |= TH_ORDREL_NEEDED; 14639 switch (tcp->tcp_state) { 14640 case TCPS_SYN_RCVD: 14641 case TCPS_ESTABLISHED: 14642 tcp->tcp_state = TCPS_CLOSE_WAIT; 14643 /* Keepalive? */ 14644 break; 14645 case TCPS_FIN_WAIT_1: 14646 if (!tcp->tcp_fin_acked) { 14647 tcp->tcp_state = TCPS_CLOSING; 14648 break; 14649 } 14650 /* FALLTHRU */ 14651 case TCPS_FIN_WAIT_2: 14652 tcp->tcp_state = TCPS_TIME_WAIT; 14653 /* 14654 * Unconditionally clear the exclusive binding 14655 * bit so this TIME-WAIT connection won't 14656 * interfere with new ones. 14657 */ 14658 tcp->tcp_exclbind = 0; 14659 if (!TCP_IS_DETACHED(tcp)) { 14660 TCP_TIMER_RESTART(tcp, 14661 tcps->tcps_time_wait_interval); 14662 } else { 14663 tcp_time_wait_append(tcp); 14664 TCP_DBGSTAT(tcps, tcp_rput_time_wait); 14665 } 14666 if (seg_len) { 14667 /* 14668 * implies data piggybacked on FIN. 14669 * break to handle data. 14670 */ 14671 break; 14672 } 14673 freemsg(mp); 14674 goto ack_check; 14675 } 14676 } 14677 } 14678 if (mp == NULL) 14679 goto xmit_check; 14680 if (seg_len == 0) { 14681 freemsg(mp); 14682 goto xmit_check; 14683 } 14684 if (mp->b_rptr == mp->b_wptr) { 14685 /* 14686 * The header has been consumed, so we remove the 14687 * zero-length mblk here. 14688 */ 14689 mp1 = mp; 14690 mp = mp->b_cont; 14691 freeb(mp1); 14692 } 14693 update_ack: 14694 tcph = tcp->tcp_tcph; 14695 tcp->tcp_rack_cnt++; 14696 { 14697 uint32_t cur_max; 14698 14699 cur_max = tcp->tcp_rack_cur_max; 14700 if (tcp->tcp_rack_cnt >= cur_max) { 14701 /* 14702 * We have more unacked data than we should - send 14703 * an ACK now. 14704 */ 14705 flags |= TH_ACK_NEEDED; 14706 cur_max++; 14707 if (cur_max > tcp->tcp_rack_abs_max) 14708 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14709 else 14710 tcp->tcp_rack_cur_max = cur_max; 14711 } else if (TCP_IS_DETACHED(tcp)) { 14712 /* We don't have an ACK timer for detached TCP. */ 14713 flags |= TH_ACK_NEEDED; 14714 } else if (seg_len < mss) { 14715 /* 14716 * If we get a segment that is less than an mss, and we 14717 * already have unacknowledged data, and the amount 14718 * unacknowledged is not a multiple of mss, then we 14719 * better generate an ACK now. Otherwise, this may be 14720 * the tail piece of a transaction, and we would rather 14721 * wait for the response. 14722 */ 14723 uint32_t udif; 14724 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14725 (uintptr_t)INT_MAX); 14726 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14727 if (udif && (udif % mss)) 14728 flags |= TH_ACK_NEEDED; 14729 else 14730 flags |= TH_ACK_TIMER_NEEDED; 14731 } else { 14732 /* Start delayed ack timer */ 14733 flags |= TH_ACK_TIMER_NEEDED; 14734 } 14735 } 14736 tcp->tcp_rnxt += seg_len; 14737 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14738 14739 if (mp == NULL) 14740 goto xmit_check; 14741 14742 /* Update SACK list */ 14743 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14744 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14745 &(tcp->tcp_num_sack_blk)); 14746 } 14747 14748 if (tcp->tcp_urp_mp) { 14749 tcp->tcp_urp_mp->b_cont = mp; 14750 mp = tcp->tcp_urp_mp; 14751 tcp->tcp_urp_mp = NULL; 14752 /* Ready for a new signal. */ 14753 tcp->tcp_urp_last_valid = B_FALSE; 14754 #ifdef DEBUG 14755 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14756 "tcp_rput: sending exdata_ind %s", 14757 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14758 #endif /* DEBUG */ 14759 } 14760 14761 /* 14762 * Check for ancillary data changes compared to last segment. 14763 */ 14764 if (tcp->tcp_ipv6_recvancillary != 0) { 14765 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14766 ASSERT(mp != NULL); 14767 } 14768 14769 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14770 /* 14771 * Side queue inbound data until the accept happens. 14772 * tcp_accept/tcp_rput drains this when the accept happens. 14773 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14774 * T_EXDATA_IND) it is queued on b_next. 14775 * XXX Make urgent data use this. Requires: 14776 * Removing tcp_listener check for TH_URG 14777 * Making M_PCPROTO and MARK messages skip the eager case 14778 */ 14779 14780 if (tcp->tcp_kssl_pending) { 14781 DTRACE_PROBE1(kssl_mblk__ksslinput_pending, 14782 mblk_t *, mp); 14783 tcp_kssl_input(tcp, mp); 14784 } else { 14785 tcp_rcv_enqueue(tcp, mp, seg_len); 14786 } 14787 } else { 14788 if (mp->b_datap->db_type != M_DATA || 14789 (flags & TH_MARKNEXT_NEEDED)) { 14790 if (IPCL_IS_NONSTR(connp)) { 14791 int error; 14792 14793 if ((*connp->conn_upcalls->su_recv) 14794 (connp->conn_upper_handle, mp, 14795 seg_len, 0, &error, NULL) <= 0) { 14796 /* 14797 * We should never be in middle of a 14798 * fallback, the squeue guarantees that. 14799 */ 14800 ASSERT(error != EOPNOTSUPP); 14801 if (error == ENOSPC) 14802 tcp->tcp_rwnd -= seg_len; 14803 } 14804 } else if (tcp->tcp_rcv_list != NULL) { 14805 flags |= tcp_rcv_drain(tcp); 14806 } 14807 ASSERT(tcp->tcp_rcv_list == NULL || 14808 tcp->tcp_fused_sigurg); 14809 14810 if (flags & TH_MARKNEXT_NEEDED) { 14811 #ifdef DEBUG 14812 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14813 "tcp_rput: sending MSGMARKNEXT %s", 14814 tcp_display(tcp, NULL, 14815 DISP_PORT_ONLY)); 14816 #endif /* DEBUG */ 14817 mp->b_flag |= MSGMARKNEXT; 14818 flags &= ~TH_MARKNEXT_NEEDED; 14819 } 14820 14821 /* Does this need SSL processing first? */ 14822 if ((tcp->tcp_kssl_ctx != NULL) && 14823 (DB_TYPE(mp) == M_DATA)) { 14824 DTRACE_PROBE1(kssl_mblk__ksslinput_data1, 14825 mblk_t *, mp); 14826 tcp_kssl_input(tcp, mp); 14827 } else if (!IPCL_IS_NONSTR(connp)) { 14828 /* Already handled non-STREAMS case. */ 14829 putnext(tcp->tcp_rq, mp); 14830 if (!canputnext(tcp->tcp_rq)) 14831 tcp->tcp_rwnd -= seg_len; 14832 } 14833 } else if ((tcp->tcp_kssl_ctx != NULL) && 14834 (DB_TYPE(mp) == M_DATA)) { 14835 /* Does this need SSL processing first? */ 14836 DTRACE_PROBE1(kssl_mblk__ksslinput_data2, mblk_t *, mp); 14837 tcp_kssl_input(tcp, mp); 14838 } else if (IPCL_IS_NONSTR(connp)) { 14839 /* Non-STREAMS socket */ 14840 boolean_t push = flags & (TH_PUSH|TH_FIN); 14841 int error; 14842 14843 if ((*connp->conn_upcalls->su_recv)( 14844 connp->conn_upper_handle, 14845 mp, seg_len, 0, &error, &push) <= 0) { 14846 /* 14847 * We should never be in middle of a 14848 * fallback, the squeue guarantees that. 14849 */ 14850 ASSERT(error != EOPNOTSUPP); 14851 if (error == ENOSPC) 14852 tcp->tcp_rwnd -= seg_len; 14853 } else if (push) { 14854 /* 14855 * PUSH bit set and sockfs is not 14856 * flow controlled 14857 */ 14858 flags |= tcp_rwnd_reopen(tcp); 14859 } 14860 } else if ((flags & (TH_PUSH|TH_FIN)) || 14861 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_recv_hiwater >> 3) { 14862 if (tcp->tcp_rcv_list != NULL) { 14863 /* 14864 * Enqueue the new segment first and then 14865 * call tcp_rcv_drain() to send all data 14866 * up. The other way to do this is to 14867 * send all queued data up and then call 14868 * putnext() to send the new segment up. 14869 * This way can remove the else part later 14870 * on. 14871 * 14872 * We don't do this to avoid one more call to 14873 * canputnext() as tcp_rcv_drain() needs to 14874 * call canputnext(). 14875 */ 14876 tcp_rcv_enqueue(tcp, mp, seg_len); 14877 flags |= tcp_rcv_drain(tcp); 14878 } else { 14879 putnext(tcp->tcp_rq, mp); 14880 if (!canputnext(tcp->tcp_rq)) 14881 tcp->tcp_rwnd -= seg_len; 14882 } 14883 } else { 14884 /* 14885 * Enqueue all packets when processing an mblk 14886 * from the co queue and also enqueue normal packets. 14887 */ 14888 tcp_rcv_enqueue(tcp, mp, seg_len); 14889 } 14890 /* 14891 * Make sure the timer is running if we have data waiting 14892 * for a push bit. This provides resiliency against 14893 * implementations that do not correctly generate push bits. 14894 */ 14895 if (!IPCL_IS_NONSTR(connp) && tcp->tcp_rcv_list != NULL && 14896 tcp->tcp_push_tid == 0) { 14897 /* 14898 * The connection may be closed at this point, so don't 14899 * do anything for a detached tcp. 14900 */ 14901 if (!TCP_IS_DETACHED(tcp)) 14902 tcp->tcp_push_tid = TCP_TIMER(tcp, 14903 tcp_push_timer, 14904 MSEC_TO_TICK( 14905 tcps->tcps_push_timer_interval)); 14906 } 14907 } 14908 14909 xmit_check: 14910 /* Is there anything left to do? */ 14911 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14912 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14913 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14914 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14915 goto done; 14916 14917 /* Any transmit work to do and a non-zero window? */ 14918 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14919 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14920 if (flags & TH_REXMIT_NEEDED) { 14921 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14922 14923 BUMP_MIB(&tcps->tcps_mib, tcpOutFastRetrans); 14924 if (snd_size > mss) 14925 snd_size = mss; 14926 if (snd_size > tcp->tcp_swnd) 14927 snd_size = tcp->tcp_swnd; 14928 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14929 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14930 B_TRUE); 14931 14932 if (mp1 != NULL) { 14933 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14934 tcp->tcp_csuna = tcp->tcp_snxt; 14935 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 14936 UPDATE_MIB(&tcps->tcps_mib, 14937 tcpRetransBytes, snd_size); 14938 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14939 } 14940 } 14941 if (flags & TH_NEED_SACK_REXMIT) { 14942 tcp_sack_rxmit(tcp, &flags); 14943 } 14944 /* 14945 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14946 * out new segment. Note that tcp_rexmit should not be 14947 * set, otherwise TH_LIMIT_XMIT should not be set. 14948 */ 14949 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14950 if (!tcp->tcp_rexmit) { 14951 tcp_wput_data(tcp, NULL, B_FALSE); 14952 } else { 14953 tcp_ss_rexmit(tcp); 14954 } 14955 } 14956 /* 14957 * Adjust tcp_cwnd back to normal value after sending 14958 * new data segments. 14959 */ 14960 if (flags & TH_LIMIT_XMIT) { 14961 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14962 /* 14963 * This will restart the timer. Restarting the 14964 * timer is used to avoid a timeout before the 14965 * limited transmitted segment's ACK gets back. 14966 */ 14967 if (tcp->tcp_xmit_head != NULL) 14968 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14969 } 14970 14971 /* Anything more to do? */ 14972 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14973 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14974 goto done; 14975 } 14976 ack_check: 14977 if (flags & TH_SEND_URP_MARK) { 14978 ASSERT(tcp->tcp_urp_mark_mp); 14979 ASSERT(!IPCL_IS_NONSTR(connp)); 14980 /* 14981 * Send up any queued data and then send the mark message 14982 */ 14983 if (tcp->tcp_rcv_list != NULL) { 14984 flags |= tcp_rcv_drain(tcp); 14985 14986 } 14987 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14988 mp1 = tcp->tcp_urp_mark_mp; 14989 tcp->tcp_urp_mark_mp = NULL; 14990 putnext(tcp->tcp_rq, mp1); 14991 #ifdef DEBUG 14992 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14993 "tcp_rput: sending zero-length %s %s", 14994 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14995 "MSGNOTMARKNEXT"), 14996 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14997 #endif /* DEBUG */ 14998 flags &= ~TH_SEND_URP_MARK; 14999 } 15000 if (flags & TH_ACK_NEEDED) { 15001 /* 15002 * Time to send an ack for some reason. 15003 */ 15004 mp1 = tcp_ack_mp(tcp); 15005 15006 if (mp1 != NULL) { 15007 tcp_send_data(tcp, tcp->tcp_wq, mp1); 15008 BUMP_LOCAL(tcp->tcp_obsegs); 15009 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 15010 } 15011 if (tcp->tcp_ack_tid != 0) { 15012 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 15013 tcp->tcp_ack_tid = 0; 15014 } 15015 } 15016 if (flags & TH_ACK_TIMER_NEEDED) { 15017 /* 15018 * Arrange for deferred ACK or push wait timeout. 15019 * Start timer if it is not already running. 15020 */ 15021 if (tcp->tcp_ack_tid == 0) { 15022 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 15023 MSEC_TO_TICK(tcp->tcp_localnet ? 15024 (clock_t)tcps->tcps_local_dack_interval : 15025 (clock_t)tcps->tcps_deferred_ack_interval)); 15026 } 15027 } 15028 if (flags & TH_ORDREL_NEEDED) { 15029 /* 15030 * Send up the ordrel_ind unless we are an eager guy. 15031 * In the eager case tcp_rsrv will do this when run 15032 * after tcp_accept is done. 15033 */ 15034 ASSERT(tcp->tcp_listener == NULL); 15035 15036 if (IPCL_IS_NONSTR(connp)) { 15037 ASSERT(tcp->tcp_ordrel_mp == NULL); 15038 tcp->tcp_ordrel_done = B_TRUE; 15039 (*connp->conn_upcalls->su_opctl) 15040 (connp->conn_upper_handle, SOCK_OPCTL_SHUT_RECV, 0); 15041 goto done; 15042 } 15043 15044 if (tcp->tcp_rcv_list != NULL) { 15045 /* 15046 * Push any mblk(s) enqueued from co processing. 15047 */ 15048 flags |= tcp_rcv_drain(tcp); 15049 } 15050 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15051 15052 mp1 = tcp->tcp_ordrel_mp; 15053 tcp->tcp_ordrel_mp = NULL; 15054 tcp->tcp_ordrel_done = B_TRUE; 15055 putnext(tcp->tcp_rq, mp1); 15056 } 15057 done: 15058 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 15059 } 15060 15061 /* 15062 * This function does PAWS protection check. Returns B_TRUE if the 15063 * segment passes the PAWS test, else returns B_FALSE. 15064 */ 15065 boolean_t 15066 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 15067 { 15068 uint8_t flags; 15069 int options; 15070 uint8_t *up; 15071 15072 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 15073 /* 15074 * If timestamp option is aligned nicely, get values inline, 15075 * otherwise call general routine to parse. Only do that 15076 * if timestamp is the only option. 15077 */ 15078 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 15079 TCPOPT_REAL_TS_LEN && 15080 OK_32PTR((up = ((uint8_t *)tcph) + 15081 TCP_MIN_HEADER_LENGTH)) && 15082 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 15083 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 15084 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 15085 15086 options = TCP_OPT_TSTAMP_PRESENT; 15087 } else { 15088 if (tcp->tcp_snd_sack_ok) { 15089 tcpoptp->tcp = tcp; 15090 } else { 15091 tcpoptp->tcp = NULL; 15092 } 15093 options = tcp_parse_options(tcph, tcpoptp); 15094 } 15095 15096 if (options & TCP_OPT_TSTAMP_PRESENT) { 15097 /* 15098 * Do PAWS per RFC 1323 section 4.2. Accept RST 15099 * regardless of the timestamp, page 18 RFC 1323.bis. 15100 */ 15101 if ((flags & TH_RST) == 0 && 15102 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 15103 tcp->tcp_ts_recent)) { 15104 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 15105 PAWS_TIMEOUT)) { 15106 /* This segment is not acceptable. */ 15107 return (B_FALSE); 15108 } else { 15109 /* 15110 * Connection has been idle for 15111 * too long. Reset the timestamp 15112 * and assume the segment is valid. 15113 */ 15114 tcp->tcp_ts_recent = 15115 tcpoptp->tcp_opt_ts_val; 15116 } 15117 } 15118 } else { 15119 /* 15120 * If we don't get a timestamp on every packet, we 15121 * figure we can't really trust 'em, so we stop sending 15122 * and parsing them. 15123 */ 15124 tcp->tcp_snd_ts_ok = B_FALSE; 15125 15126 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15127 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 15128 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 15129 /* 15130 * Adjust the tcp_mss accordingly. We also need to 15131 * adjust tcp_cwnd here in accordance with the new mss. 15132 * But we avoid doing a slow start here so as to not 15133 * to lose on the transfer rate built up so far. 15134 */ 15135 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN, B_FALSE); 15136 if (tcp->tcp_snd_sack_ok) { 15137 ASSERT(tcp->tcp_sack_info != NULL); 15138 tcp->tcp_max_sack_blk = 4; 15139 } 15140 } 15141 return (B_TRUE); 15142 } 15143 15144 /* 15145 * Attach ancillary data to a received TCP segments for the 15146 * ancillary pieces requested by the application that are 15147 * different than they were in the previous data segment. 15148 * 15149 * Save the "current" values once memory allocation is ok so that 15150 * when memory allocation fails we can just wait for the next data segment. 15151 */ 15152 static mblk_t * 15153 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 15154 { 15155 struct T_optdata_ind *todi; 15156 int optlen; 15157 uchar_t *optptr; 15158 struct T_opthdr *toh; 15159 uint_t addflag; /* Which pieces to add */ 15160 mblk_t *mp1; 15161 15162 optlen = 0; 15163 addflag = 0; 15164 /* If app asked for pktinfo and the index has changed ... */ 15165 if ((ipp->ipp_fields & IPPF_IFINDEX) && 15166 ipp->ipp_ifindex != tcp->tcp_recvifindex && 15167 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 15168 optlen += sizeof (struct T_opthdr) + 15169 sizeof (struct in6_pktinfo); 15170 addflag |= TCP_IPV6_RECVPKTINFO; 15171 } 15172 /* If app asked for hoplimit and it has changed ... */ 15173 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 15174 ipp->ipp_hoplimit != tcp->tcp_recvhops && 15175 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 15176 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15177 addflag |= TCP_IPV6_RECVHOPLIMIT; 15178 } 15179 /* If app asked for tclass and it has changed ... */ 15180 if ((ipp->ipp_fields & IPPF_TCLASS) && 15181 ipp->ipp_tclass != tcp->tcp_recvtclass && 15182 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 15183 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 15184 addflag |= TCP_IPV6_RECVTCLASS; 15185 } 15186 /* 15187 * If app asked for hopbyhop headers and it has changed ... 15188 * For security labels, note that (1) security labels can't change on 15189 * a connected socket at all, (2) we're connected to at most one peer, 15190 * (3) if anything changes, then it must be some other extra option. 15191 */ 15192 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 15193 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 15194 (ipp->ipp_fields & IPPF_HOPOPTS), 15195 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 15196 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 15197 tcp->tcp_label_len; 15198 addflag |= TCP_IPV6_RECVHOPOPTS; 15199 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 15200 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 15201 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 15202 return (mp); 15203 } 15204 /* If app asked for dst headers before routing headers ... */ 15205 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 15206 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 15207 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15208 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 15209 optlen += sizeof (struct T_opthdr) + 15210 ipp->ipp_rtdstoptslen; 15211 addflag |= TCP_IPV6_RECVRTDSTOPTS; 15212 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 15213 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 15214 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 15215 return (mp); 15216 } 15217 /* If app asked for routing headers and it has changed ... */ 15218 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 15219 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 15220 (ipp->ipp_fields & IPPF_RTHDR), 15221 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 15222 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 15223 addflag |= TCP_IPV6_RECVRTHDR; 15224 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 15225 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 15226 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 15227 return (mp); 15228 } 15229 /* If app asked for dest headers and it has changed ... */ 15230 if ((tcp->tcp_ipv6_recvancillary & 15231 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 15232 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 15233 (ipp->ipp_fields & IPPF_DSTOPTS), 15234 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 15235 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 15236 addflag |= TCP_IPV6_RECVDSTOPTS; 15237 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 15238 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 15239 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 15240 return (mp); 15241 } 15242 15243 if (optlen == 0) { 15244 /* Nothing to add */ 15245 return (mp); 15246 } 15247 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 15248 if (mp1 == NULL) { 15249 /* 15250 * Defer sending ancillary data until the next TCP segment 15251 * arrives. 15252 */ 15253 return (mp); 15254 } 15255 mp1->b_cont = mp; 15256 mp = mp1; 15257 mp->b_wptr += sizeof (*todi) + optlen; 15258 mp->b_datap->db_type = M_PROTO; 15259 todi = (struct T_optdata_ind *)mp->b_rptr; 15260 todi->PRIM_type = T_OPTDATA_IND; 15261 todi->DATA_flag = 1; /* MORE data */ 15262 todi->OPT_length = optlen; 15263 todi->OPT_offset = sizeof (*todi); 15264 optptr = (uchar_t *)&todi[1]; 15265 /* 15266 * If app asked for pktinfo and the index has changed ... 15267 * Note that the local address never changes for the connection. 15268 */ 15269 if (addflag & TCP_IPV6_RECVPKTINFO) { 15270 struct in6_pktinfo *pkti; 15271 15272 toh = (struct T_opthdr *)optptr; 15273 toh->level = IPPROTO_IPV6; 15274 toh->name = IPV6_PKTINFO; 15275 toh->len = sizeof (*toh) + sizeof (*pkti); 15276 toh->status = 0; 15277 optptr += sizeof (*toh); 15278 pkti = (struct in6_pktinfo *)optptr; 15279 if (tcp->tcp_ipversion == IPV6_VERSION) 15280 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 15281 else 15282 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 15283 &pkti->ipi6_addr); 15284 pkti->ipi6_ifindex = ipp->ipp_ifindex; 15285 optptr += sizeof (*pkti); 15286 ASSERT(OK_32PTR(optptr)); 15287 /* Save as "last" value */ 15288 tcp->tcp_recvifindex = ipp->ipp_ifindex; 15289 } 15290 /* If app asked for hoplimit and it has changed ... */ 15291 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 15292 toh = (struct T_opthdr *)optptr; 15293 toh->level = IPPROTO_IPV6; 15294 toh->name = IPV6_HOPLIMIT; 15295 toh->len = sizeof (*toh) + sizeof (uint_t); 15296 toh->status = 0; 15297 optptr += sizeof (*toh); 15298 *(uint_t *)optptr = ipp->ipp_hoplimit; 15299 optptr += sizeof (uint_t); 15300 ASSERT(OK_32PTR(optptr)); 15301 /* Save as "last" value */ 15302 tcp->tcp_recvhops = ipp->ipp_hoplimit; 15303 } 15304 /* If app asked for tclass and it has changed ... */ 15305 if (addflag & TCP_IPV6_RECVTCLASS) { 15306 toh = (struct T_opthdr *)optptr; 15307 toh->level = IPPROTO_IPV6; 15308 toh->name = IPV6_TCLASS; 15309 toh->len = sizeof (*toh) + sizeof (uint_t); 15310 toh->status = 0; 15311 optptr += sizeof (*toh); 15312 *(uint_t *)optptr = ipp->ipp_tclass; 15313 optptr += sizeof (uint_t); 15314 ASSERT(OK_32PTR(optptr)); 15315 /* Save as "last" value */ 15316 tcp->tcp_recvtclass = ipp->ipp_tclass; 15317 } 15318 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15319 toh = (struct T_opthdr *)optptr; 15320 toh->level = IPPROTO_IPV6; 15321 toh->name = IPV6_HOPOPTS; 15322 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15323 tcp->tcp_label_len; 15324 toh->status = 0; 15325 optptr += sizeof (*toh); 15326 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15327 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15328 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15329 ASSERT(OK_32PTR(optptr)); 15330 /* Save as last value */ 15331 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15332 (ipp->ipp_fields & IPPF_HOPOPTS), 15333 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15334 } 15335 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15336 toh = (struct T_opthdr *)optptr; 15337 toh->level = IPPROTO_IPV6; 15338 toh->name = IPV6_RTHDRDSTOPTS; 15339 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15340 toh->status = 0; 15341 optptr += sizeof (*toh); 15342 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15343 optptr += ipp->ipp_rtdstoptslen; 15344 ASSERT(OK_32PTR(optptr)); 15345 /* Save as last value */ 15346 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15347 &tcp->tcp_rtdstoptslen, 15348 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15349 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15350 } 15351 if (addflag & TCP_IPV6_RECVRTHDR) { 15352 toh = (struct T_opthdr *)optptr; 15353 toh->level = IPPROTO_IPV6; 15354 toh->name = IPV6_RTHDR; 15355 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15356 toh->status = 0; 15357 optptr += sizeof (*toh); 15358 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15359 optptr += ipp->ipp_rthdrlen; 15360 ASSERT(OK_32PTR(optptr)); 15361 /* Save as last value */ 15362 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15363 (ipp->ipp_fields & IPPF_RTHDR), 15364 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15365 } 15366 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15367 toh = (struct T_opthdr *)optptr; 15368 toh->level = IPPROTO_IPV6; 15369 toh->name = IPV6_DSTOPTS; 15370 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15371 toh->status = 0; 15372 optptr += sizeof (*toh); 15373 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15374 optptr += ipp->ipp_dstoptslen; 15375 ASSERT(OK_32PTR(optptr)); 15376 /* Save as last value */ 15377 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15378 (ipp->ipp_fields & IPPF_DSTOPTS), 15379 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15380 } 15381 ASSERT(optptr == mp->b_wptr); 15382 return (mp); 15383 } 15384 15385 /* 15386 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15387 * messages. 15388 */ 15389 void 15390 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15391 { 15392 uchar_t *rptr = mp->b_rptr; 15393 queue_t *q = tcp->tcp_rq; 15394 struct T_error_ack *tea; 15395 15396 switch (mp->b_datap->db_type) { 15397 case M_PROTO: 15398 case M_PCPROTO: 15399 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15400 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15401 break; 15402 tea = (struct T_error_ack *)rptr; 15403 ASSERT(tea->PRIM_type != T_BIND_ACK); 15404 ASSERT(tea->ERROR_prim != O_T_BIND_REQ && 15405 tea->ERROR_prim != T_BIND_REQ); 15406 switch (tea->PRIM_type) { 15407 case T_ERROR_ACK: 15408 if (tcp->tcp_debug) { 15409 (void) strlog(TCP_MOD_ID, 0, 1, 15410 SL_TRACE|SL_ERROR, 15411 "tcp_rput_other: case T_ERROR_ACK, " 15412 "ERROR_prim == %d", 15413 tea->ERROR_prim); 15414 } 15415 switch (tea->ERROR_prim) { 15416 case T_SVR4_OPTMGMT_REQ: 15417 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15418 /* T_OPTMGMT_REQ generated by TCP */ 15419 printf("T_SVR4_OPTMGMT_REQ failed " 15420 "%d/%d - dropped (cnt %d)\n", 15421 tea->TLI_error, tea->UNIX_error, 15422 tcp->tcp_drop_opt_ack_cnt); 15423 freemsg(mp); 15424 tcp->tcp_drop_opt_ack_cnt--; 15425 return; 15426 } 15427 break; 15428 } 15429 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15430 tcp->tcp_drop_opt_ack_cnt > 0) { 15431 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15432 "- dropped (cnt %d)\n", 15433 tea->TLI_error, tea->UNIX_error, 15434 tcp->tcp_drop_opt_ack_cnt); 15435 freemsg(mp); 15436 tcp->tcp_drop_opt_ack_cnt--; 15437 return; 15438 } 15439 break; 15440 case T_OPTMGMT_ACK: 15441 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15442 /* T_OPTMGMT_REQ generated by TCP */ 15443 freemsg(mp); 15444 tcp->tcp_drop_opt_ack_cnt--; 15445 return; 15446 } 15447 break; 15448 default: 15449 ASSERT(tea->ERROR_prim != T_UNBIND_REQ); 15450 break; 15451 } 15452 break; 15453 case M_FLUSH: 15454 if (*rptr & FLUSHR) 15455 flushq(q, FLUSHDATA); 15456 break; 15457 default: 15458 /* M_CTL will be directly sent to tcp_icmp_error() */ 15459 ASSERT(DB_TYPE(mp) != M_CTL); 15460 break; 15461 } 15462 /* 15463 * Make sure we set this bit before sending the ACK for 15464 * bind. Otherwise accept could possibly run and free 15465 * this tcp struct. 15466 */ 15467 ASSERT(q != NULL); 15468 putnext(q, mp); 15469 } 15470 15471 /* ARGSUSED */ 15472 static void 15473 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15474 { 15475 conn_t *connp = (conn_t *)arg; 15476 tcp_t *tcp = connp->conn_tcp; 15477 queue_t *q = tcp->tcp_rq; 15478 tcp_stack_t *tcps = tcp->tcp_tcps; 15479 15480 ASSERT(!IPCL_IS_NONSTR(connp)); 15481 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15482 tcp->tcp_rsrv_mp = mp; 15483 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15484 15485 TCP_STAT(tcps, tcp_rsrv_calls); 15486 15487 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15488 return; 15489 } 15490 15491 if (tcp->tcp_fused) { 15492 tcp_fuse_backenable(tcp); 15493 return; 15494 } 15495 15496 if (canputnext(q)) { 15497 /* Not flow-controlled, open rwnd */ 15498 tcp->tcp_rwnd = q->q_hiwat; 15499 15500 /* 15501 * Send back a window update immediately if TCP is above 15502 * ESTABLISHED state and the increase of the rcv window 15503 * that the other side knows is at least 1 MSS after flow 15504 * control is lifted. 15505 */ 15506 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15507 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 15508 tcp_xmit_ctl(NULL, tcp, 15509 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15510 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15511 } 15512 } 15513 } 15514 15515 /* 15516 * The read side service routine is called mostly when we get back-enabled as a 15517 * result of flow control relief. Since we don't actually queue anything in 15518 * TCP, we have no data to send out of here. What we do is clear the receive 15519 * window, and send out a window update. 15520 */ 15521 static void 15522 tcp_rsrv(queue_t *q) 15523 { 15524 conn_t *connp = Q_TO_CONN(q); 15525 tcp_t *tcp = connp->conn_tcp; 15526 mblk_t *mp; 15527 tcp_stack_t *tcps = tcp->tcp_tcps; 15528 15529 /* No code does a putq on the read side */ 15530 ASSERT(q->q_first == NULL); 15531 15532 /* Nothing to do for the default queue */ 15533 if (q == tcps->tcps_g_q) { 15534 return; 15535 } 15536 15537 /* 15538 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_rsrv() has already 15539 * been run. So just return. 15540 */ 15541 mutex_enter(&tcp->tcp_rsrv_mp_lock); 15542 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 15543 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15544 return; 15545 } 15546 tcp->tcp_rsrv_mp = NULL; 15547 mutex_exit(&tcp->tcp_rsrv_mp_lock); 15548 15549 CONN_INC_REF(connp); 15550 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15551 SQ_PROCESS, SQTAG_TCP_RSRV); 15552 } 15553 15554 /* 15555 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15556 * We do not allow the receive window to shrink. After setting rwnd, 15557 * set the flow control hiwat of the stream. 15558 * 15559 * This function is called in 2 cases: 15560 * 15561 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15562 * connection (passive open) and in tcp_rput_data() for active connect. 15563 * This is called after tcp_mss_set() when the desired MSS value is known. 15564 * This makes sure that our window size is a mutiple of the other side's 15565 * MSS. 15566 * 2) Handling SO_RCVBUF option. 15567 * 15568 * It is ASSUMED that the requested size is a multiple of the current MSS. 15569 * 15570 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15571 * user requests so. 15572 */ 15573 static int 15574 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15575 { 15576 uint32_t mss = tcp->tcp_mss; 15577 uint32_t old_max_rwnd; 15578 uint32_t max_transmittable_rwnd; 15579 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15580 tcp_stack_t *tcps = tcp->tcp_tcps; 15581 15582 if (tcp->tcp_fused) { 15583 size_t sth_hiwat; 15584 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15585 15586 ASSERT(peer_tcp != NULL); 15587 /* 15588 * Record the stream head's high water mark for 15589 * this endpoint; this is used for flow-control 15590 * purposes in tcp_fuse_output(). 15591 */ 15592 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15593 if (!tcp_detached) { 15594 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15595 sth_hiwat); 15596 if (IPCL_IS_NONSTR(tcp->tcp_connp)) { 15597 conn_t *connp = tcp->tcp_connp; 15598 struct sock_proto_props sopp; 15599 15600 sopp.sopp_flags = SOCKOPT_RCVTHRESH; 15601 sopp.sopp_rcvthresh = sth_hiwat >> 3; 15602 15603 (*connp->conn_upcalls->su_set_proto_props) 15604 (connp->conn_upper_handle, &sopp); 15605 } 15606 } 15607 15608 /* 15609 * In the fusion case, the maxpsz stream head value of 15610 * our peer is set according to its send buffer size 15611 * and our receive buffer size; since the latter may 15612 * have changed we need to update the peer's maxpsz. 15613 */ 15614 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15615 return (rwnd); 15616 } 15617 15618 if (tcp_detached) { 15619 old_max_rwnd = tcp->tcp_rwnd; 15620 } else { 15621 old_max_rwnd = tcp->tcp_recv_hiwater; 15622 } 15623 15624 /* 15625 * Insist on a receive window that is at least 15626 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15627 * funny TCP interactions of Nagle algorithm, SWS avoidance 15628 * and delayed acknowledgement. 15629 */ 15630 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss); 15631 15632 /* 15633 * If window size info has already been exchanged, TCP should not 15634 * shrink the window. Shrinking window is doable if done carefully. 15635 * We may add that support later. But so far there is not a real 15636 * need to do that. 15637 */ 15638 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15639 /* MSS may have changed, do a round up again. */ 15640 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15641 } 15642 15643 /* 15644 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15645 * can be applied even before the window scale option is decided. 15646 */ 15647 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15648 if (rwnd > max_transmittable_rwnd) { 15649 rwnd = max_transmittable_rwnd - 15650 (max_transmittable_rwnd % mss); 15651 if (rwnd < mss) 15652 rwnd = max_transmittable_rwnd; 15653 /* 15654 * If we're over the limit we may have to back down tcp_rwnd. 15655 * The increment below won't work for us. So we set all three 15656 * here and the increment below will have no effect. 15657 */ 15658 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15659 } 15660 if (tcp->tcp_localnet) { 15661 tcp->tcp_rack_abs_max = 15662 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2); 15663 } else { 15664 /* 15665 * For a remote host on a different subnet (through a router), 15666 * we ack every other packet to be conforming to RFC1122. 15667 * tcp_deferred_acks_max is default to 2. 15668 */ 15669 tcp->tcp_rack_abs_max = 15670 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2); 15671 } 15672 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15673 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15674 else 15675 tcp->tcp_rack_cur_max = 0; 15676 /* 15677 * Increment the current rwnd by the amount the maximum grew (we 15678 * can not overwrite it since we might be in the middle of a 15679 * connection.) 15680 */ 15681 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15682 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15683 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15684 tcp->tcp_cwnd_max = rwnd; 15685 15686 if (tcp_detached) 15687 return (rwnd); 15688 /* 15689 * We set the maximum receive window into rq->q_hiwat if it is 15690 * a STREAMS socket. 15691 * This is not actually used for flow control. 15692 */ 15693 if (!IPCL_IS_NONSTR(tcp->tcp_connp)) 15694 tcp->tcp_rq->q_hiwat = rwnd; 15695 tcp->tcp_recv_hiwater = rwnd; 15696 /* 15697 * Set the STREAM head high water mark. This doesn't have to be 15698 * here, since we are simply using default values, but we would 15699 * prefer to choose these values algorithmically, with a likely 15700 * relationship to rwnd. 15701 */ 15702 (void) proto_set_rx_hiwat(tcp->tcp_rq, tcp->tcp_connp, 15703 MAX(rwnd, tcps->tcps_sth_rcv_hiwat)); 15704 return (rwnd); 15705 } 15706 15707 /* 15708 * Return SNMP stuff in buffer in mpdata. 15709 */ 15710 mblk_t * 15711 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15712 { 15713 mblk_t *mpdata; 15714 mblk_t *mp_conn_ctl = NULL; 15715 mblk_t *mp_conn_tail; 15716 mblk_t *mp_attr_ctl = NULL; 15717 mblk_t *mp_attr_tail; 15718 mblk_t *mp6_conn_ctl = NULL; 15719 mblk_t *mp6_conn_tail; 15720 mblk_t *mp6_attr_ctl = NULL; 15721 mblk_t *mp6_attr_tail; 15722 struct opthdr *optp; 15723 mib2_tcpConnEntry_t tce; 15724 mib2_tcp6ConnEntry_t tce6; 15725 mib2_transportMLPEntry_t mlp; 15726 connf_t *connfp; 15727 int i; 15728 boolean_t ispriv; 15729 zoneid_t zoneid; 15730 int v4_conn_idx; 15731 int v6_conn_idx; 15732 conn_t *connp = Q_TO_CONN(q); 15733 tcp_stack_t *tcps; 15734 ip_stack_t *ipst; 15735 mblk_t *mp2ctl; 15736 15737 /* 15738 * make a copy of the original message 15739 */ 15740 mp2ctl = copymsg(mpctl); 15741 15742 if (mpctl == NULL || 15743 (mpdata = mpctl->b_cont) == NULL || 15744 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15745 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15746 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15747 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15748 freemsg(mp_conn_ctl); 15749 freemsg(mp_attr_ctl); 15750 freemsg(mp6_conn_ctl); 15751 freemsg(mp6_attr_ctl); 15752 freemsg(mpctl); 15753 freemsg(mp2ctl); 15754 return (NULL); 15755 } 15756 15757 ipst = connp->conn_netstack->netstack_ip; 15758 tcps = connp->conn_netstack->netstack_tcp; 15759 15760 /* build table of connections -- need count in fixed part */ 15761 SET_MIB(tcps->tcps_mib.tcpRtoAlgorithm, 4); /* vanj */ 15762 SET_MIB(tcps->tcps_mib.tcpRtoMin, tcps->tcps_rexmit_interval_min); 15763 SET_MIB(tcps->tcps_mib.tcpRtoMax, tcps->tcps_rexmit_interval_max); 15764 SET_MIB(tcps->tcps_mib.tcpMaxConn, -1); 15765 SET_MIB(tcps->tcps_mib.tcpCurrEstab, 0); 15766 15767 ispriv = 15768 secpolicy_ip_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15769 zoneid = Q_TO_CONN(q)->conn_zoneid; 15770 15771 v4_conn_idx = v6_conn_idx = 0; 15772 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15773 15774 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15775 ipst = tcps->tcps_netstack->netstack_ip; 15776 15777 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 15778 15779 connp = NULL; 15780 15781 while ((connp = 15782 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15783 tcp_t *tcp; 15784 boolean_t needattr; 15785 15786 if (connp->conn_zoneid != zoneid) 15787 continue; /* not in this zone */ 15788 15789 tcp = connp->conn_tcp; 15790 UPDATE_MIB(&tcps->tcps_mib, 15791 tcpHCInSegs, tcp->tcp_ibsegs); 15792 tcp->tcp_ibsegs = 0; 15793 UPDATE_MIB(&tcps->tcps_mib, 15794 tcpHCOutSegs, tcp->tcp_obsegs); 15795 tcp->tcp_obsegs = 0; 15796 15797 tce6.tcp6ConnState = tce.tcpConnState = 15798 tcp_snmp_state(tcp); 15799 if (tce.tcpConnState == MIB2_TCP_established || 15800 tce.tcpConnState == MIB2_TCP_closeWait) 15801 BUMP_MIB(&tcps->tcps_mib, tcpCurrEstab); 15802 15803 needattr = B_FALSE; 15804 bzero(&mlp, sizeof (mlp)); 15805 if (connp->conn_mlp_type != mlptSingle) { 15806 if (connp->conn_mlp_type == mlptShared || 15807 connp->conn_mlp_type == mlptBoth) 15808 mlp.tme_flags |= MIB2_TMEF_SHARED; 15809 if (connp->conn_mlp_type == mlptPrivate || 15810 connp->conn_mlp_type == mlptBoth) 15811 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15812 needattr = B_TRUE; 15813 } 15814 if (connp->conn_peercred != NULL) { 15815 ts_label_t *tsl; 15816 15817 tsl = crgetlabel(connp->conn_peercred); 15818 mlp.tme_doi = label2doi(tsl); 15819 mlp.tme_label = *label2bslabel(tsl); 15820 needattr = B_TRUE; 15821 } 15822 15823 /* Create a message to report on IPv6 entries */ 15824 if (tcp->tcp_ipversion == IPV6_VERSION) { 15825 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15826 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15827 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15828 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15829 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15830 /* Don't want just anybody seeing these... */ 15831 if (ispriv) { 15832 tce6.tcp6ConnEntryInfo.ce_snxt = 15833 tcp->tcp_snxt; 15834 tce6.tcp6ConnEntryInfo.ce_suna = 15835 tcp->tcp_suna; 15836 tce6.tcp6ConnEntryInfo.ce_rnxt = 15837 tcp->tcp_rnxt; 15838 tce6.tcp6ConnEntryInfo.ce_rack = 15839 tcp->tcp_rack; 15840 } else { 15841 /* 15842 * Netstat, unfortunately, uses this to 15843 * get send/receive queue sizes. How to fix? 15844 * Why not compute the difference only? 15845 */ 15846 tce6.tcp6ConnEntryInfo.ce_snxt = 15847 tcp->tcp_snxt - tcp->tcp_suna; 15848 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15849 tce6.tcp6ConnEntryInfo.ce_rnxt = 15850 tcp->tcp_rnxt - tcp->tcp_rack; 15851 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15852 } 15853 15854 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15855 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15856 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15857 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15858 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15859 15860 tce6.tcp6ConnCreationProcess = 15861 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15862 tcp->tcp_cpid; 15863 tce6.tcp6ConnCreationTime = tcp->tcp_open_time; 15864 15865 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15866 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15867 15868 mlp.tme_connidx = v6_conn_idx++; 15869 if (needattr) 15870 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15871 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15872 } 15873 /* 15874 * Create an IPv4 table entry for IPv4 entries and also 15875 * for IPv6 entries which are bound to in6addr_any 15876 * but don't have IPV6_V6ONLY set. 15877 * (i.e. anything an IPv4 peer could connect to) 15878 */ 15879 if (tcp->tcp_ipversion == IPV4_VERSION || 15880 (tcp->tcp_state <= TCPS_LISTEN && 15881 !tcp->tcp_connp->conn_ipv6_v6only && 15882 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15883 if (tcp->tcp_ipversion == IPV6_VERSION) { 15884 tce.tcpConnRemAddress = INADDR_ANY; 15885 tce.tcpConnLocalAddress = INADDR_ANY; 15886 } else { 15887 tce.tcpConnRemAddress = 15888 tcp->tcp_remote; 15889 tce.tcpConnLocalAddress = 15890 tcp->tcp_ip_src; 15891 } 15892 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15893 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15894 /* Don't want just anybody seeing these... */ 15895 if (ispriv) { 15896 tce.tcpConnEntryInfo.ce_snxt = 15897 tcp->tcp_snxt; 15898 tce.tcpConnEntryInfo.ce_suna = 15899 tcp->tcp_suna; 15900 tce.tcpConnEntryInfo.ce_rnxt = 15901 tcp->tcp_rnxt; 15902 tce.tcpConnEntryInfo.ce_rack = 15903 tcp->tcp_rack; 15904 } else { 15905 /* 15906 * Netstat, unfortunately, uses this to 15907 * get send/receive queue sizes. How 15908 * to fix? 15909 * Why not compute the difference only? 15910 */ 15911 tce.tcpConnEntryInfo.ce_snxt = 15912 tcp->tcp_snxt - tcp->tcp_suna; 15913 tce.tcpConnEntryInfo.ce_suna = 0; 15914 tce.tcpConnEntryInfo.ce_rnxt = 15915 tcp->tcp_rnxt - tcp->tcp_rack; 15916 tce.tcpConnEntryInfo.ce_rack = 0; 15917 } 15918 15919 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15920 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15921 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15922 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15923 tce.tcpConnEntryInfo.ce_state = 15924 tcp->tcp_state; 15925 15926 tce.tcpConnCreationProcess = 15927 (tcp->tcp_cpid < 0) ? MIB2_UNKNOWN_PROCESS : 15928 tcp->tcp_cpid; 15929 tce.tcpConnCreationTime = tcp->tcp_open_time; 15930 15931 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15932 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15933 15934 mlp.tme_connidx = v4_conn_idx++; 15935 if (needattr) 15936 (void) snmp_append_data2( 15937 mp_attr_ctl->b_cont, 15938 &mp_attr_tail, (char *)&mlp, 15939 sizeof (mlp)); 15940 } 15941 } 15942 } 15943 15944 /* fixed length structure for IPv4 and IPv6 counters */ 15945 SET_MIB(tcps->tcps_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15946 SET_MIB(tcps->tcps_mib.tcp6ConnTableSize, 15947 sizeof (mib2_tcp6ConnEntry_t)); 15948 /* synchronize 32- and 64-bit counters */ 15949 SYNC32_MIB(&tcps->tcps_mib, tcpInSegs, tcpHCInSegs); 15950 SYNC32_MIB(&tcps->tcps_mib, tcpOutSegs, tcpHCOutSegs); 15951 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15952 optp->level = MIB2_TCP; 15953 optp->name = 0; 15954 (void) snmp_append_data(mpdata, (char *)&tcps->tcps_mib, 15955 sizeof (tcps->tcps_mib)); 15956 optp->len = msgdsize(mpdata); 15957 qreply(q, mpctl); 15958 15959 /* table of connections... */ 15960 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 15961 sizeof (struct T_optmgmt_ack)]; 15962 optp->level = MIB2_TCP; 15963 optp->name = MIB2_TCP_CONN; 15964 optp->len = msgdsize(mp_conn_ctl->b_cont); 15965 qreply(q, mp_conn_ctl); 15966 15967 /* table of MLP attributes... */ 15968 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 15969 sizeof (struct T_optmgmt_ack)]; 15970 optp->level = MIB2_TCP; 15971 optp->name = EXPER_XPORT_MLP; 15972 optp->len = msgdsize(mp_attr_ctl->b_cont); 15973 if (optp->len == 0) 15974 freemsg(mp_attr_ctl); 15975 else 15976 qreply(q, mp_attr_ctl); 15977 15978 /* table of IPv6 connections... */ 15979 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 15980 sizeof (struct T_optmgmt_ack)]; 15981 optp->level = MIB2_TCP6; 15982 optp->name = MIB2_TCP6_CONN; 15983 optp->len = msgdsize(mp6_conn_ctl->b_cont); 15984 qreply(q, mp6_conn_ctl); 15985 15986 /* table of IPv6 MLP attributes... */ 15987 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 15988 sizeof (struct T_optmgmt_ack)]; 15989 optp->level = MIB2_TCP6; 15990 optp->name = EXPER_XPORT_MLP; 15991 optp->len = msgdsize(mp6_attr_ctl->b_cont); 15992 if (optp->len == 0) 15993 freemsg(mp6_attr_ctl); 15994 else 15995 qreply(q, mp6_attr_ctl); 15996 return (mp2ctl); 15997 } 15998 15999 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16000 /* ARGSUSED */ 16001 int 16002 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16003 { 16004 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16005 16006 switch (level) { 16007 case MIB2_TCP: 16008 switch (name) { 16009 case 13: 16010 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16011 return (0); 16012 /* TODO: delete entry defined by tce */ 16013 return (1); 16014 default: 16015 return (0); 16016 } 16017 default: 16018 return (1); 16019 } 16020 } 16021 16022 /* Translate TCP state to MIB2 TCP state. */ 16023 static int 16024 tcp_snmp_state(tcp_t *tcp) 16025 { 16026 if (tcp == NULL) 16027 return (0); 16028 16029 switch (tcp->tcp_state) { 16030 case TCPS_CLOSED: 16031 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16032 case TCPS_BOUND: 16033 return (MIB2_TCP_closed); 16034 case TCPS_LISTEN: 16035 return (MIB2_TCP_listen); 16036 case TCPS_SYN_SENT: 16037 return (MIB2_TCP_synSent); 16038 case TCPS_SYN_RCVD: 16039 return (MIB2_TCP_synReceived); 16040 case TCPS_ESTABLISHED: 16041 return (MIB2_TCP_established); 16042 case TCPS_CLOSE_WAIT: 16043 return (MIB2_TCP_closeWait); 16044 case TCPS_FIN_WAIT_1: 16045 return (MIB2_TCP_finWait1); 16046 case TCPS_CLOSING: 16047 return (MIB2_TCP_closing); 16048 case TCPS_LAST_ACK: 16049 return (MIB2_TCP_lastAck); 16050 case TCPS_FIN_WAIT_2: 16051 return (MIB2_TCP_finWait2); 16052 case TCPS_TIME_WAIT: 16053 return (MIB2_TCP_timeWait); 16054 default: 16055 return (0); 16056 } 16057 } 16058 16059 /* 16060 * tcp_timer is the timer service routine. It handles the retransmission, 16061 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16062 * from the state of the tcp instance what kind of action needs to be done 16063 * at the time it is called. 16064 */ 16065 static void 16066 tcp_timer(void *arg) 16067 { 16068 mblk_t *mp; 16069 clock_t first_threshold; 16070 clock_t second_threshold; 16071 clock_t ms; 16072 uint32_t mss; 16073 conn_t *connp = (conn_t *)arg; 16074 tcp_t *tcp = connp->conn_tcp; 16075 tcp_stack_t *tcps = tcp->tcp_tcps; 16076 16077 tcp->tcp_timer_tid = 0; 16078 16079 if (tcp->tcp_fused) 16080 return; 16081 16082 first_threshold = tcp->tcp_first_timer_threshold; 16083 second_threshold = tcp->tcp_second_timer_threshold; 16084 switch (tcp->tcp_state) { 16085 case TCPS_IDLE: 16086 case TCPS_BOUND: 16087 case TCPS_LISTEN: 16088 return; 16089 case TCPS_SYN_RCVD: { 16090 tcp_t *listener = tcp->tcp_listener; 16091 16092 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16093 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16094 /* it's our first timeout */ 16095 tcp->tcp_syn_rcvd_timeout = 1; 16096 mutex_enter(&listener->tcp_eager_lock); 16097 listener->tcp_syn_rcvd_timeout++; 16098 if (!tcp->tcp_dontdrop && !tcp->tcp_closemp_used) { 16099 /* 16100 * Make this eager available for drop if we 16101 * need to drop one to accomodate a new 16102 * incoming SYN request. 16103 */ 16104 MAKE_DROPPABLE(listener, tcp); 16105 } 16106 if (!listener->tcp_syn_defense && 16107 (listener->tcp_syn_rcvd_timeout > 16108 (tcps->tcps_conn_req_max_q0 >> 2)) && 16109 (tcps->tcps_conn_req_max_q0 > 200)) { 16110 /* We may be under attack. Put on a defense. */ 16111 listener->tcp_syn_defense = B_TRUE; 16112 cmn_err(CE_WARN, "High TCP connect timeout " 16113 "rate! System (port %d) may be under a " 16114 "SYN flood attack!", 16115 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16116 16117 listener->tcp_ip_addr_cache = kmem_zalloc( 16118 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16119 KM_NOSLEEP); 16120 } 16121 mutex_exit(&listener->tcp_eager_lock); 16122 } else if (listener != NULL) { 16123 mutex_enter(&listener->tcp_eager_lock); 16124 tcp->tcp_syn_rcvd_timeout++; 16125 if (tcp->tcp_syn_rcvd_timeout > 1 && 16126 !tcp->tcp_closemp_used) { 16127 /* 16128 * This is our second timeout. Put the tcp in 16129 * the list of droppable eagers to allow it to 16130 * be dropped, if needed. We don't check 16131 * whether tcp_dontdrop is set or not to 16132 * protect ourselve from a SYN attack where a 16133 * remote host can spoof itself as one of the 16134 * good IP source and continue to hold 16135 * resources too long. 16136 */ 16137 MAKE_DROPPABLE(listener, tcp); 16138 } 16139 mutex_exit(&listener->tcp_eager_lock); 16140 } 16141 } 16142 /* FALLTHRU */ 16143 case TCPS_SYN_SENT: 16144 first_threshold = tcp->tcp_first_ctimer_threshold; 16145 second_threshold = tcp->tcp_second_ctimer_threshold; 16146 break; 16147 case TCPS_ESTABLISHED: 16148 case TCPS_FIN_WAIT_1: 16149 case TCPS_CLOSING: 16150 case TCPS_CLOSE_WAIT: 16151 case TCPS_LAST_ACK: 16152 /* If we have data to rexmit */ 16153 if (tcp->tcp_suna != tcp->tcp_snxt) { 16154 clock_t time_to_wait; 16155 16156 BUMP_MIB(&tcps->tcps_mib, tcpTimRetrans); 16157 if (!tcp->tcp_xmit_head) 16158 break; 16159 time_to_wait = lbolt - 16160 (clock_t)tcp->tcp_xmit_head->b_prev; 16161 time_to_wait = tcp->tcp_rto - 16162 TICK_TO_MSEC(time_to_wait); 16163 /* 16164 * If the timer fires too early, 1 clock tick earlier, 16165 * restart the timer. 16166 */ 16167 if (time_to_wait > msec_per_tick) { 16168 TCP_STAT(tcps, tcp_timer_fire_early); 16169 TCP_TIMER_RESTART(tcp, time_to_wait); 16170 return; 16171 } 16172 /* 16173 * When we probe zero windows, we force the swnd open. 16174 * If our peer acks with a closed window swnd will be 16175 * set to zero by tcp_rput(). As long as we are 16176 * receiving acks tcp_rput will 16177 * reset 'tcp_ms_we_have_waited' so as not to trip the 16178 * first and second interval actions. NOTE: the timer 16179 * interval is allowed to continue its exponential 16180 * backoff. 16181 */ 16182 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16183 if (tcp->tcp_debug) { 16184 (void) strlog(TCP_MOD_ID, 0, 1, 16185 SL_TRACE, "tcp_timer: zero win"); 16186 } 16187 } else { 16188 /* 16189 * After retransmission, we need to do 16190 * slow start. Set the ssthresh to one 16191 * half of current effective window and 16192 * cwnd to one MSS. Also reset 16193 * tcp_cwnd_cnt. 16194 * 16195 * Note that if tcp_ssthresh is reduced because 16196 * of ECN, do not reduce it again unless it is 16197 * already one window of data away (tcp_cwr 16198 * should then be cleared) or this is a 16199 * timeout for a retransmitted segment. 16200 */ 16201 uint32_t npkt; 16202 16203 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16204 npkt = ((tcp->tcp_timer_backoff ? 16205 tcp->tcp_cwnd_ssthresh : 16206 tcp->tcp_snxt - 16207 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16208 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16209 tcp->tcp_mss; 16210 } 16211 tcp->tcp_cwnd = tcp->tcp_mss; 16212 tcp->tcp_cwnd_cnt = 0; 16213 if (tcp->tcp_ecn_ok) { 16214 tcp->tcp_cwr = B_TRUE; 16215 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16216 tcp->tcp_ecn_cwr_sent = B_FALSE; 16217 } 16218 } 16219 break; 16220 } 16221 /* 16222 * We have something to send yet we cannot send. The 16223 * reason can be: 16224 * 16225 * 1. Zero send window: we need to do zero window probe. 16226 * 2. Zero cwnd: because of ECN, we need to "clock out 16227 * segments. 16228 * 3. SWS avoidance: receiver may have shrunk window, 16229 * reset our knowledge. 16230 * 16231 * Note that condition 2 can happen with either 1 or 16232 * 3. But 1 and 3 are exclusive. 16233 */ 16234 if (tcp->tcp_unsent != 0) { 16235 if (tcp->tcp_cwnd == 0) { 16236 /* 16237 * Set tcp_cwnd to 1 MSS so that a 16238 * new segment can be sent out. We 16239 * are "clocking out" new data when 16240 * the network is really congested. 16241 */ 16242 ASSERT(tcp->tcp_ecn_ok); 16243 tcp->tcp_cwnd = tcp->tcp_mss; 16244 } 16245 if (tcp->tcp_swnd == 0) { 16246 /* Extend window for zero window probe */ 16247 tcp->tcp_swnd++; 16248 tcp->tcp_zero_win_probe = B_TRUE; 16249 BUMP_MIB(&tcps->tcps_mib, tcpOutWinProbe); 16250 } else { 16251 /* 16252 * Handle timeout from sender SWS avoidance. 16253 * Reset our knowledge of the max send window 16254 * since the receiver might have reduced its 16255 * receive buffer. Avoid setting tcp_max_swnd 16256 * to one since that will essentially disable 16257 * the SWS checks. 16258 * 16259 * Note that since we don't have a SWS 16260 * state variable, if the timeout is set 16261 * for ECN but not for SWS, this 16262 * code will also be executed. This is 16263 * fine as tcp_max_swnd is updated 16264 * constantly and it will not affect 16265 * anything. 16266 */ 16267 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16268 } 16269 tcp_wput_data(tcp, NULL, B_FALSE); 16270 return; 16271 } 16272 /* Is there a FIN that needs to be to re retransmitted? */ 16273 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16274 !tcp->tcp_fin_acked) 16275 break; 16276 /* Nothing to do, return without restarting timer. */ 16277 TCP_STAT(tcps, tcp_timer_fire_miss); 16278 return; 16279 case TCPS_FIN_WAIT_2: 16280 /* 16281 * User closed the TCP endpoint and peer ACK'ed our FIN. 16282 * We waited some time for for peer's FIN, but it hasn't 16283 * arrived. We flush the connection now to avoid 16284 * case where the peer has rebooted. 16285 */ 16286 if (TCP_IS_DETACHED(tcp)) { 16287 (void) tcp_clean_death(tcp, 0, 23); 16288 } else { 16289 TCP_TIMER_RESTART(tcp, 16290 tcps->tcps_fin_wait_2_flush_interval); 16291 } 16292 return; 16293 case TCPS_TIME_WAIT: 16294 (void) tcp_clean_death(tcp, 0, 24); 16295 return; 16296 default: 16297 if (tcp->tcp_debug) { 16298 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16299 "tcp_timer: strange state (%d) %s", 16300 tcp->tcp_state, tcp_display(tcp, NULL, 16301 DISP_PORT_ONLY)); 16302 } 16303 return; 16304 } 16305 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16306 /* 16307 * For zero window probe, we need to send indefinitely, 16308 * unless we have not heard from the other side for some 16309 * time... 16310 */ 16311 if ((tcp->tcp_zero_win_probe == 0) || 16312 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16313 second_threshold)) { 16314 BUMP_MIB(&tcps->tcps_mib, tcpTimRetransDrop); 16315 /* 16316 * If TCP is in SYN_RCVD state, send back a 16317 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16318 * should be zero in TCPS_SYN_RCVD state. 16319 */ 16320 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16321 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16322 "in SYN_RCVD", 16323 tcp, tcp->tcp_snxt, 16324 tcp->tcp_rnxt, TH_RST | TH_ACK); 16325 } 16326 (void) tcp_clean_death(tcp, 16327 tcp->tcp_client_errno ? 16328 tcp->tcp_client_errno : ETIMEDOUT, 25); 16329 return; 16330 } else { 16331 /* 16332 * Set tcp_ms_we_have_waited to second_threshold 16333 * so that in next timeout, we will do the above 16334 * check (lbolt - tcp_last_recv_time). This is 16335 * also to avoid overflow. 16336 * 16337 * We don't need to decrement tcp_timer_backoff 16338 * to avoid overflow because it will be decremented 16339 * later if new timeout value is greater than 16340 * tcp_rexmit_interval_max. In the case when 16341 * tcp_rexmit_interval_max is greater than 16342 * second_threshold, it means that we will wait 16343 * longer than second_threshold to send the next 16344 * window probe. 16345 */ 16346 tcp->tcp_ms_we_have_waited = second_threshold; 16347 } 16348 } else if (ms > first_threshold) { 16349 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16350 tcp->tcp_xmit_head != NULL) { 16351 tcp->tcp_xmit_head = 16352 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16353 } 16354 /* 16355 * We have been retransmitting for too long... The RTT 16356 * we calculated is probably incorrect. Reinitialize it. 16357 * Need to compensate for 0 tcp_rtt_sa. Reset 16358 * tcp_rtt_update so that we won't accidentally cache a 16359 * bad value. But only do this if this is not a zero 16360 * window probe. 16361 */ 16362 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16363 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16364 (tcp->tcp_rtt_sa >> 5); 16365 tcp->tcp_rtt_sa = 0; 16366 tcp_ip_notify(tcp); 16367 tcp->tcp_rtt_update = 0; 16368 } 16369 } 16370 tcp->tcp_timer_backoff++; 16371 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16372 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16373 tcps->tcps_rexmit_interval_min) { 16374 /* 16375 * This means the original RTO is tcp_rexmit_interval_min. 16376 * So we will use tcp_rexmit_interval_min as the RTO value 16377 * and do the backoff. 16378 */ 16379 ms = tcps->tcps_rexmit_interval_min << tcp->tcp_timer_backoff; 16380 } else { 16381 ms <<= tcp->tcp_timer_backoff; 16382 } 16383 if (ms > tcps->tcps_rexmit_interval_max) { 16384 ms = tcps->tcps_rexmit_interval_max; 16385 /* 16386 * ms is at max, decrement tcp_timer_backoff to avoid 16387 * overflow. 16388 */ 16389 tcp->tcp_timer_backoff--; 16390 } 16391 tcp->tcp_ms_we_have_waited += ms; 16392 if (tcp->tcp_zero_win_probe == 0) { 16393 tcp->tcp_rto = ms; 16394 } 16395 TCP_TIMER_RESTART(tcp, ms); 16396 /* 16397 * This is after a timeout and tcp_rto is backed off. Set 16398 * tcp_set_timer to 1 so that next time RTO is updated, we will 16399 * restart the timer with a correct value. 16400 */ 16401 tcp->tcp_set_timer = 1; 16402 mss = tcp->tcp_snxt - tcp->tcp_suna; 16403 if (mss > tcp->tcp_mss) 16404 mss = tcp->tcp_mss; 16405 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16406 mss = tcp->tcp_swnd; 16407 16408 if ((mp = tcp->tcp_xmit_head) != NULL) 16409 mp->b_prev = (mblk_t *)lbolt; 16410 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16411 B_TRUE); 16412 16413 /* 16414 * When slow start after retransmission begins, start with 16415 * this seq no. tcp_rexmit_max marks the end of special slow 16416 * start phase. tcp_snd_burst controls how many segments 16417 * can be sent because of an ack. 16418 */ 16419 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16420 tcp->tcp_snd_burst = TCP_CWND_SS; 16421 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16422 (tcp->tcp_unsent == 0)) { 16423 tcp->tcp_rexmit_max = tcp->tcp_fss; 16424 } else { 16425 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16426 } 16427 tcp->tcp_rexmit = B_TRUE; 16428 tcp->tcp_dupack_cnt = 0; 16429 16430 /* 16431 * Remove all rexmit SACK blk to start from fresh. 16432 */ 16433 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16434 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16435 tcp->tcp_num_notsack_blk = 0; 16436 tcp->tcp_cnt_notsack_list = 0; 16437 } 16438 if (mp == NULL) { 16439 return; 16440 } 16441 /* 16442 * Attach credentials to retransmitted initial SYNs. 16443 * In theory we should use the credentials from the connect() 16444 * call to ensure that getpeerucred() on the peer will be correct. 16445 * But we assume that SYN's are not dropped for loopback connections. 16446 */ 16447 if (tcp->tcp_state == TCPS_SYN_SENT) { 16448 mblk_setcred(mp, tcp->tcp_cred, tcp->tcp_cpid); 16449 } 16450 16451 tcp->tcp_csuna = tcp->tcp_snxt; 16452 BUMP_MIB(&tcps->tcps_mib, tcpRetransSegs); 16453 UPDATE_MIB(&tcps->tcps_mib, tcpRetransBytes, mss); 16454 tcp_send_data(tcp, tcp->tcp_wq, mp); 16455 16456 } 16457 16458 static int 16459 tcp_do_unbind(conn_t *connp) 16460 { 16461 tcp_t *tcp = connp->conn_tcp; 16462 int error = 0; 16463 16464 switch (tcp->tcp_state) { 16465 case TCPS_BOUND: 16466 case TCPS_LISTEN: 16467 break; 16468 default: 16469 return (-TOUTSTATE); 16470 } 16471 16472 /* 16473 * Need to clean up all the eagers since after the unbind, segments 16474 * will no longer be delivered to this listener stream. 16475 */ 16476 mutex_enter(&tcp->tcp_eager_lock); 16477 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16478 tcp_eager_cleanup(tcp, 0); 16479 } 16480 mutex_exit(&tcp->tcp_eager_lock); 16481 16482 if (tcp->tcp_ipversion == IPV4_VERSION) { 16483 tcp->tcp_ipha->ipha_src = 0; 16484 } else { 16485 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16486 } 16487 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16488 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16489 tcp_bind_hash_remove(tcp); 16490 tcp->tcp_state = TCPS_IDLE; 16491 tcp->tcp_mdt = B_FALSE; 16492 16493 connp = tcp->tcp_connp; 16494 connp->conn_mdt_ok = B_FALSE; 16495 ipcl_hash_remove(connp); 16496 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16497 16498 return (error); 16499 } 16500 16501 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16502 static void 16503 tcp_tpi_unbind(tcp_t *tcp, mblk_t *mp) 16504 { 16505 int error = tcp_do_unbind(tcp->tcp_connp); 16506 16507 if (error > 0) { 16508 tcp_err_ack(tcp, mp, TSYSERR, error); 16509 } else if (error < 0) { 16510 tcp_err_ack(tcp, mp, -error, 0); 16511 } else { 16512 /* Send M_FLUSH according to TPI */ 16513 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16514 16515 mp = mi_tpi_ok_ack_alloc(mp); 16516 putnext(tcp->tcp_rq, mp); 16517 } 16518 } 16519 16520 /* 16521 * Don't let port fall into the privileged range. 16522 * Since the extra privileged ports can be arbitrary we also 16523 * ensure that we exclude those from consideration. 16524 * tcp_g_epriv_ports is not sorted thus we loop over it until 16525 * there are no changes. 16526 * 16527 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16528 * but instead the code relies on: 16529 * - the fact that the address of the array and its size never changes 16530 * - the atomic assignment of the elements of the array 16531 * 16532 * Returns 0 if there are no more ports available. 16533 * 16534 * TS note: skip multilevel ports. 16535 */ 16536 static in_port_t 16537 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16538 { 16539 int i; 16540 boolean_t restart = B_FALSE; 16541 tcp_stack_t *tcps = tcp->tcp_tcps; 16542 16543 if (random && tcp_random_anon_port != 0) { 16544 (void) random_get_pseudo_bytes((uint8_t *)&port, 16545 sizeof (in_port_t)); 16546 /* 16547 * Unless changed by a sys admin, the smallest anon port 16548 * is 32768 and the largest anon port is 65535. It is 16549 * very likely (50%) for the random port to be smaller 16550 * than the smallest anon port. When that happens, 16551 * add port % (anon port range) to the smallest anon 16552 * port to get the random port. It should fall into the 16553 * valid anon port range. 16554 */ 16555 if (port < tcps->tcps_smallest_anon_port) { 16556 port = tcps->tcps_smallest_anon_port + 16557 port % (tcps->tcps_largest_anon_port - 16558 tcps->tcps_smallest_anon_port); 16559 } 16560 } 16561 16562 retry: 16563 if (port < tcps->tcps_smallest_anon_port) 16564 port = (in_port_t)tcps->tcps_smallest_anon_port; 16565 16566 if (port > tcps->tcps_largest_anon_port) { 16567 if (restart) 16568 return (0); 16569 restart = B_TRUE; 16570 port = (in_port_t)tcps->tcps_smallest_anon_port; 16571 } 16572 16573 if (port < tcps->tcps_smallest_nonpriv_port) 16574 port = (in_port_t)tcps->tcps_smallest_nonpriv_port; 16575 16576 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 16577 if (port == tcps->tcps_g_epriv_ports[i]) { 16578 port++; 16579 /* 16580 * Make sure whether the port is in the 16581 * valid range. 16582 */ 16583 goto retry; 16584 } 16585 } 16586 if (is_system_labeled() && 16587 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16588 IPPROTO_TCP, B_TRUE)) != 0) { 16589 port = i; 16590 goto retry; 16591 } 16592 return (port); 16593 } 16594 16595 /* 16596 * Return the next anonymous port in the privileged port range for 16597 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16598 * downwards. This is the same behavior as documented in the userland 16599 * library call rresvport(3N). 16600 * 16601 * TS note: skip multilevel ports. 16602 */ 16603 static in_port_t 16604 tcp_get_next_priv_port(const tcp_t *tcp) 16605 { 16606 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16607 in_port_t nextport; 16608 boolean_t restart = B_FALSE; 16609 tcp_stack_t *tcps = tcp->tcp_tcps; 16610 retry: 16611 if (next_priv_port < tcps->tcps_min_anonpriv_port || 16612 next_priv_port >= IPPORT_RESERVED) { 16613 next_priv_port = IPPORT_RESERVED - 1; 16614 if (restart) 16615 return (0); 16616 restart = B_TRUE; 16617 } 16618 if (is_system_labeled() && 16619 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16620 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16621 next_priv_port = nextport; 16622 goto retry; 16623 } 16624 return (next_priv_port--); 16625 } 16626 16627 /* The write side r/w procedure. */ 16628 16629 #if CCS_STATS 16630 struct { 16631 struct { 16632 int64_t count, bytes; 16633 } tot, hit; 16634 } wrw_stats; 16635 #endif 16636 16637 /* 16638 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16639 * messages. 16640 */ 16641 /* ARGSUSED */ 16642 static void 16643 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16644 { 16645 conn_t *connp = (conn_t *)arg; 16646 tcp_t *tcp = connp->conn_tcp; 16647 queue_t *q = tcp->tcp_wq; 16648 16649 ASSERT(DB_TYPE(mp) != M_IOCTL); 16650 /* 16651 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16652 * Once the close starts, streamhead and sockfs will not let any data 16653 * packets come down (close ensures that there are no threads using the 16654 * queue and no new threads will come down) but since qprocsoff() 16655 * hasn't happened yet, a M_FLUSH or some non data message might 16656 * get reflected back (in response to our own FLUSHRW) and get 16657 * processed after tcp_close() is done. The conn would still be valid 16658 * because a ref would have added but we need to check the state 16659 * before actually processing the packet. 16660 */ 16661 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16662 freemsg(mp); 16663 return; 16664 } 16665 16666 switch (DB_TYPE(mp)) { 16667 case M_IOCDATA: 16668 tcp_wput_iocdata(tcp, mp); 16669 break; 16670 case M_FLUSH: 16671 tcp_wput_flush(tcp, mp); 16672 break; 16673 default: 16674 CALL_IP_WPUT(connp, q, mp); 16675 break; 16676 } 16677 } 16678 16679 /* 16680 * The TCP fast path write put procedure. 16681 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16682 */ 16683 /* ARGSUSED */ 16684 void 16685 tcp_output(void *arg, mblk_t *mp, void *arg2) 16686 { 16687 int len; 16688 int hdrlen; 16689 int plen; 16690 mblk_t *mp1; 16691 uchar_t *rptr; 16692 uint32_t snxt; 16693 tcph_t *tcph; 16694 struct datab *db; 16695 uint32_t suna; 16696 uint32_t mss; 16697 ipaddr_t *dst; 16698 ipaddr_t *src; 16699 uint32_t sum; 16700 int usable; 16701 conn_t *connp = (conn_t *)arg; 16702 tcp_t *tcp = connp->conn_tcp; 16703 uint32_t msize; 16704 tcp_stack_t *tcps = tcp->tcp_tcps; 16705 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 16706 16707 /* 16708 * Try and ASSERT the minimum possible references on the 16709 * conn early enough. Since we are executing on write side, 16710 * the connection is obviously not detached and that means 16711 * there is a ref each for TCP and IP. Since we are behind 16712 * the squeue, the minimum references needed are 3. If the 16713 * conn is in classifier hash list, there should be an 16714 * extra ref for that (we check both the possibilities). 16715 */ 16716 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16717 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16718 16719 ASSERT(DB_TYPE(mp) == M_DATA); 16720 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16721 16722 mutex_enter(&tcp->tcp_non_sq_lock); 16723 tcp->tcp_squeue_bytes -= msize; 16724 mutex_exit(&tcp->tcp_non_sq_lock); 16725 16726 /* Check to see if this connection wants to be re-fused. */ 16727 if (tcp->tcp_refuse && !ipst->ips_ipobs_enabled) { 16728 if (tcp->tcp_ipversion == IPV4_VERSION) { 16729 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ipha, 16730 &tcp->tcp_saved_tcph); 16731 } else { 16732 tcp_fuse(tcp, (uchar_t *)&tcp->tcp_saved_ip6h, 16733 &tcp->tcp_saved_tcph); 16734 } 16735 } 16736 /* Bypass tcp protocol for fused tcp loopback */ 16737 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16738 return; 16739 16740 mss = tcp->tcp_mss; 16741 if (tcp->tcp_xmit_zc_clean) 16742 mp = tcp_zcopy_backoff(tcp, mp, 0); 16743 16744 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16745 len = (int)(mp->b_wptr - mp->b_rptr); 16746 16747 /* 16748 * Criteria for fast path: 16749 * 16750 * 1. no unsent data 16751 * 2. single mblk in request 16752 * 3. connection established 16753 * 4. data in mblk 16754 * 5. len <= mss 16755 * 6. no tcp_valid bits 16756 */ 16757 if ((tcp->tcp_unsent != 0) || 16758 (tcp->tcp_cork) || 16759 (mp->b_cont != NULL) || 16760 (tcp->tcp_state != TCPS_ESTABLISHED) || 16761 (len == 0) || 16762 (len > mss) || 16763 (tcp->tcp_valid_bits != 0)) { 16764 tcp_wput_data(tcp, mp, B_FALSE); 16765 return; 16766 } 16767 16768 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16769 ASSERT(tcp->tcp_fin_sent == 0); 16770 16771 /* queue new packet onto retransmission queue */ 16772 if (tcp->tcp_xmit_head == NULL) { 16773 tcp->tcp_xmit_head = mp; 16774 } else { 16775 tcp->tcp_xmit_last->b_cont = mp; 16776 } 16777 tcp->tcp_xmit_last = mp; 16778 tcp->tcp_xmit_tail = mp; 16779 16780 /* find out how much we can send */ 16781 /* BEGIN CSTYLED */ 16782 /* 16783 * un-acked usable 16784 * |--------------|-----------------| 16785 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16786 */ 16787 /* END CSTYLED */ 16788 16789 /* start sending from tcp_snxt */ 16790 snxt = tcp->tcp_snxt; 16791 16792 /* 16793 * Check to see if this connection has been idled for some 16794 * time and no ACK is expected. If it is, we need to slow 16795 * start again to get back the connection's "self-clock" as 16796 * described in VJ's paper. 16797 * 16798 * Refer to the comment in tcp_mss_set() for the calculation 16799 * of tcp_cwnd after idle. 16800 */ 16801 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16802 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16803 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 16804 } 16805 16806 usable = tcp->tcp_swnd; /* tcp window size */ 16807 if (usable > tcp->tcp_cwnd) 16808 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16809 usable -= snxt; /* subtract stuff already sent */ 16810 suna = tcp->tcp_suna; 16811 usable += suna; 16812 /* usable can be < 0 if the congestion window is smaller */ 16813 if (len > usable) { 16814 /* Can't send complete M_DATA in one shot */ 16815 goto slow; 16816 } 16817 16818 mutex_enter(&tcp->tcp_non_sq_lock); 16819 if (tcp->tcp_flow_stopped && 16820 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16821 tcp_clrqfull(tcp); 16822 } 16823 mutex_exit(&tcp->tcp_non_sq_lock); 16824 16825 /* 16826 * determine if anything to send (Nagle). 16827 * 16828 * 1. len < tcp_mss (i.e. small) 16829 * 2. unacknowledged data present 16830 * 3. len < nagle limit 16831 * 4. last packet sent < nagle limit (previous packet sent) 16832 */ 16833 if ((len < mss) && (snxt != suna) && 16834 (len < (int)tcp->tcp_naglim) && 16835 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16836 /* 16837 * This was the first unsent packet and normally 16838 * mss < xmit_hiwater so there is no need to worry 16839 * about flow control. The next packet will go 16840 * through the flow control check in tcp_wput_data(). 16841 */ 16842 /* leftover work from above */ 16843 tcp->tcp_unsent = len; 16844 tcp->tcp_xmit_tail_unsent = len; 16845 16846 return; 16847 } 16848 16849 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 16850 16851 if (snxt == suna) { 16852 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16853 } 16854 16855 /* we have always sent something */ 16856 tcp->tcp_rack_cnt = 0; 16857 16858 tcp->tcp_snxt = snxt + len; 16859 tcp->tcp_rack = tcp->tcp_rnxt; 16860 16861 if ((mp1 = dupb(mp)) == 0) 16862 goto no_memory; 16863 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 16864 mp->b_next = (mblk_t *)(uintptr_t)snxt; 16865 16866 /* adjust tcp header information */ 16867 tcph = tcp->tcp_tcph; 16868 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 16869 16870 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 16871 sum = (sum >> 16) + (sum & 0xFFFF); 16872 U16_TO_ABE16(sum, tcph->th_sum); 16873 16874 U32_TO_ABE32(snxt, tcph->th_seq); 16875 16876 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 16877 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 16878 BUMP_LOCAL(tcp->tcp_obsegs); 16879 16880 /* Update the latest receive window size in TCP header. */ 16881 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 16882 tcph->th_win); 16883 16884 tcp->tcp_last_sent_len = (ushort_t)len; 16885 16886 plen = len + tcp->tcp_hdr_len; 16887 16888 if (tcp->tcp_ipversion == IPV4_VERSION) { 16889 tcp->tcp_ipha->ipha_length = htons(plen); 16890 } else { 16891 tcp->tcp_ip6h->ip6_plen = htons(plen - 16892 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 16893 } 16894 16895 /* see if we need to allocate a mblk for the headers */ 16896 hdrlen = tcp->tcp_hdr_len; 16897 rptr = mp1->b_rptr - hdrlen; 16898 db = mp1->b_datap; 16899 if ((db->db_ref != 2) || rptr < db->db_base || 16900 (!OK_32PTR(rptr))) { 16901 /* NOTE: we assume allocb returns an OK_32PTR */ 16902 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 16903 tcps->tcps_wroff_xtra, BPRI_MED); 16904 if (!mp) { 16905 freemsg(mp1); 16906 goto no_memory; 16907 } 16908 mp->b_cont = mp1; 16909 mp1 = mp; 16910 /* Leave room for Link Level header */ 16911 /* hdrlen = tcp->tcp_hdr_len; */ 16912 rptr = &mp1->b_rptr[tcps->tcps_wroff_xtra]; 16913 mp1->b_wptr = &rptr[hdrlen]; 16914 } 16915 mp1->b_rptr = rptr; 16916 16917 /* Fill in the timestamp option. */ 16918 if (tcp->tcp_snd_ts_ok) { 16919 U32_TO_BE32((uint32_t)lbolt, 16920 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 16921 U32_TO_BE32(tcp->tcp_ts_recent, 16922 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 16923 } else { 16924 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 16925 } 16926 16927 /* copy header into outgoing packet */ 16928 dst = (ipaddr_t *)rptr; 16929 src = (ipaddr_t *)tcp->tcp_iphc; 16930 dst[0] = src[0]; 16931 dst[1] = src[1]; 16932 dst[2] = src[2]; 16933 dst[3] = src[3]; 16934 dst[4] = src[4]; 16935 dst[5] = src[5]; 16936 dst[6] = src[6]; 16937 dst[7] = src[7]; 16938 dst[8] = src[8]; 16939 dst[9] = src[9]; 16940 if (hdrlen -= 40) { 16941 hdrlen >>= 2; 16942 dst += 10; 16943 src += 10; 16944 do { 16945 *dst++ = *src++; 16946 } while (--hdrlen); 16947 } 16948 16949 /* 16950 * Set the ECN info in the TCP header. Note that this 16951 * is not the template header. 16952 */ 16953 if (tcp->tcp_ecn_ok) { 16954 SET_ECT(tcp, rptr); 16955 16956 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 16957 if (tcp->tcp_ecn_echo_on) 16958 tcph->th_flags[0] |= TH_ECE; 16959 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 16960 tcph->th_flags[0] |= TH_CWR; 16961 tcp->tcp_ecn_cwr_sent = B_TRUE; 16962 } 16963 } 16964 16965 if (tcp->tcp_ip_forward_progress) { 16966 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 16967 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 16968 tcp->tcp_ip_forward_progress = B_FALSE; 16969 } 16970 tcp_send_data(tcp, tcp->tcp_wq, mp1); 16971 return; 16972 16973 /* 16974 * If we ran out of memory, we pretend to have sent the packet 16975 * and that it was lost on the wire. 16976 */ 16977 no_memory: 16978 return; 16979 16980 slow: 16981 /* leftover work from above */ 16982 tcp->tcp_unsent = len; 16983 tcp->tcp_xmit_tail_unsent = len; 16984 tcp_wput_data(tcp, NULL, B_FALSE); 16985 } 16986 16987 /* ARGSUSED */ 16988 void 16989 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 16990 { 16991 conn_t *connp = (conn_t *)arg; 16992 tcp_t *tcp = connp->conn_tcp; 16993 queue_t *q = tcp->tcp_rq; 16994 struct tcp_options *tcpopt; 16995 tcp_stack_t *tcps = tcp->tcp_tcps; 16996 16997 /* socket options */ 16998 uint_t sopp_flags; 16999 ssize_t sopp_rxhiwat; 17000 ssize_t sopp_maxblk; 17001 ushort_t sopp_wroff; 17002 ushort_t sopp_tail; 17003 ushort_t sopp_copyopt; 17004 17005 tcpopt = (struct tcp_options *)mp->b_rptr; 17006 17007 /* 17008 * Drop the eager's ref on the listener, that was placed when 17009 * this eager began life in tcp_conn_request. 17010 */ 17011 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17012 if (IPCL_IS_NONSTR(connp)) { 17013 /* Safe to free conn_ind message */ 17014 freemsg(tcp->tcp_conn.tcp_eager_conn_ind); 17015 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17016 } 17017 17018 tcp->tcp_detached = B_FALSE; 17019 17020 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17021 /* 17022 * Someone blewoff the eager before we could finish 17023 * the accept. 17024 * 17025 * The only reason eager exists it because we put in 17026 * a ref on it when conn ind went up. We need to send 17027 * a disconnect indication up while the last reference 17028 * on the eager will be dropped by the squeue when we 17029 * return. 17030 */ 17031 ASSERT(tcp->tcp_listener == NULL); 17032 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17033 if (IPCL_IS_NONSTR(connp)) { 17034 ASSERT(tcp->tcp_issocket); 17035 (*connp->conn_upcalls->su_disconnected)( 17036 connp->conn_upper_handle, tcp->tcp_connid, 17037 ECONNREFUSED); 17038 freemsg(mp); 17039 } else { 17040 struct T_discon_ind *tdi; 17041 17042 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17043 /* 17044 * Let us reuse the incoming mblk to avoid 17045 * memory allocation failure problems. We know 17046 * that the size of the incoming mblk i.e. 17047 * stroptions is greater than sizeof 17048 * T_discon_ind. So the reallocb below can't 17049 * fail. 17050 */ 17051 freemsg(mp->b_cont); 17052 mp->b_cont = NULL; 17053 ASSERT(DB_REF(mp) == 1); 17054 mp = reallocb(mp, sizeof (struct T_discon_ind), 17055 B_FALSE); 17056 ASSERT(mp != NULL); 17057 DB_TYPE(mp) = M_PROTO; 17058 ((union T_primitives *)mp->b_rptr)->type = 17059 T_DISCON_IND; 17060 tdi = (struct T_discon_ind *)mp->b_rptr; 17061 if (tcp->tcp_issocket) { 17062 tdi->DISCON_reason = ECONNREFUSED; 17063 tdi->SEQ_number = 0; 17064 } else { 17065 tdi->DISCON_reason = ENOPROTOOPT; 17066 tdi->SEQ_number = 17067 tcp->tcp_conn_req_seqnum; 17068 } 17069 mp->b_wptr = mp->b_rptr + 17070 sizeof (struct T_discon_ind); 17071 putnext(q, mp); 17072 return; 17073 } 17074 } 17075 if (tcp->tcp_hard_binding) { 17076 tcp->tcp_hard_binding = B_FALSE; 17077 tcp->tcp_hard_bound = B_TRUE; 17078 } 17079 return; 17080 } 17081 17082 if (tcpopt->to_flags & TCPOPT_BOUNDIF) { 17083 int boundif = tcpopt->to_boundif; 17084 uint_t len = sizeof (int); 17085 17086 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17087 IPV6_BOUND_IF, len, (uchar_t *)&boundif, &len, 17088 (uchar_t *)&boundif, NULL, tcp->tcp_cred, NULL); 17089 } 17090 if (tcpopt->to_flags & TCPOPT_RECVPKTINFO) { 17091 uint_t on = 1; 17092 uint_t len = sizeof (uint_t); 17093 (void) tcp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, IPPROTO_IPV6, 17094 IPV6_RECVPKTINFO, len, (uchar_t *)&on, &len, 17095 (uchar_t *)&on, NULL, tcp->tcp_cred, NULL); 17096 } 17097 17098 /* 17099 * For a loopback connection with tcp_direct_sockfs on, note that 17100 * we don't have to protect tcp_rcv_list yet because synchronous 17101 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17102 * possibly race with us. 17103 */ 17104 17105 /* 17106 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17107 * properly. This is the first time we know of the acceptor' 17108 * queue. So we do it here. 17109 * 17110 * XXX 17111 */ 17112 if (tcp->tcp_rcv_list == NULL) { 17113 /* 17114 * Recv queue is empty, tcp_rwnd should not have changed. 17115 * That means it should be equal to the listener's tcp_rwnd. 17116 */ 17117 if (!IPCL_IS_NONSTR(connp)) 17118 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17119 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 17120 } else { 17121 #ifdef DEBUG 17122 mblk_t *tmp; 17123 mblk_t *mp1; 17124 uint_t cnt = 0; 17125 17126 mp1 = tcp->tcp_rcv_list; 17127 while ((tmp = mp1) != NULL) { 17128 mp1 = tmp->b_next; 17129 cnt += msgdsize(tmp); 17130 } 17131 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17132 #endif 17133 /* There is some data, add them back to get the max. */ 17134 if (!IPCL_IS_NONSTR(connp)) 17135 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17136 tcp->tcp_recv_hiwater = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17137 } 17138 /* 17139 * This is the first time we run on the correct 17140 * queue after tcp_accept. So fix all the q parameters 17141 * here. 17142 */ 17143 sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF; 17144 sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17145 17146 /* 17147 * Record the stream head's high water mark for this endpoint; 17148 * this is used for flow-control purposes. 17149 */ 17150 sopp_rxhiwat = tcp->tcp_fused ? 17151 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 17152 MAX(tcp->tcp_recv_hiwater, tcps->tcps_sth_rcv_hiwat); 17153 17154 /* 17155 * Determine what write offset value to use depending on SACK and 17156 * whether the endpoint is fused or not. 17157 */ 17158 if (tcp->tcp_fused) { 17159 ASSERT(tcp->tcp_loopback); 17160 ASSERT(tcp->tcp_loopback_peer != NULL); 17161 /* 17162 * For fused tcp loopback, set the stream head's write 17163 * offset value to zero since we won't be needing any room 17164 * for TCP/IP headers. This would also improve performance 17165 * since it would reduce the amount of work done by kmem. 17166 * Non-fused tcp loopback case is handled separately below. 17167 */ 17168 sopp_wroff = 0; 17169 /* 17170 * Update the peer's transmit parameters according to 17171 * our recently calculated high water mark value. 17172 */ 17173 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17174 } else if (tcp->tcp_snd_sack_ok) { 17175 sopp_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17176 (tcp->tcp_loopback ? 0 : tcps->tcps_wroff_xtra); 17177 } else { 17178 sopp_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17179 tcps->tcps_wroff_xtra); 17180 } 17181 17182 /* 17183 * If this is endpoint is handling SSL, then reserve extra 17184 * offset and space at the end. 17185 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17186 * overriding the previous setting. The extra cost of signing and 17187 * encrypting multiple MSS-size records (12 of them with Ethernet), 17188 * instead of a single contiguous one by the stream head 17189 * largely outweighs the statistical reduction of ACKs, when 17190 * applicable. The peer will also save on decryption and verification 17191 * costs. 17192 */ 17193 if (tcp->tcp_kssl_ctx != NULL) { 17194 sopp_wroff += SSL3_WROFFSET; 17195 17196 sopp_flags |= SOCKOPT_TAIL; 17197 sopp_tail = SSL3_MAX_TAIL_LEN; 17198 17199 sopp_flags |= SOCKOPT_ZCOPY; 17200 sopp_copyopt = ZCVMUNSAFE; 17201 17202 sopp_maxblk = SSL3_MAX_RECORD_LEN; 17203 } 17204 17205 /* Send the options up */ 17206 if (IPCL_IS_NONSTR(connp)) { 17207 struct sock_proto_props sopp; 17208 17209 sopp.sopp_flags = sopp_flags; 17210 sopp.sopp_wroff = sopp_wroff; 17211 sopp.sopp_maxblk = sopp_maxblk; 17212 sopp.sopp_rxhiwat = sopp_rxhiwat; 17213 if (sopp_flags & SOCKOPT_TAIL) { 17214 ASSERT(tcp->tcp_kssl_ctx != NULL); 17215 ASSERT(sopp_flags & SOCKOPT_ZCOPY); 17216 sopp.sopp_tail = sopp_tail; 17217 sopp.sopp_zcopyflag = sopp_copyopt; 17218 } 17219 (*connp->conn_upcalls->su_set_proto_props) 17220 (connp->conn_upper_handle, &sopp); 17221 } else { 17222 struct stroptions *stropt; 17223 mblk_t *stropt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17224 if (stropt_mp == NULL) { 17225 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 17226 return; 17227 } 17228 DB_TYPE(stropt_mp) = M_SETOPTS; 17229 stropt = (struct stroptions *)stropt_mp->b_rptr; 17230 stropt_mp->b_wptr += sizeof (struct stroptions); 17231 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 17232 stropt->so_hiwat = sopp_rxhiwat; 17233 stropt->so_wroff = sopp_wroff; 17234 stropt->so_maxblk = sopp_maxblk; 17235 17236 if (sopp_flags & SOCKOPT_TAIL) { 17237 ASSERT(tcp->tcp_kssl_ctx != NULL); 17238 17239 stropt->so_flags |= SO_TAIL | SO_COPYOPT; 17240 stropt->so_tail = sopp_tail; 17241 stropt->so_copyopt = sopp_copyopt; 17242 } 17243 17244 /* Send the options up */ 17245 putnext(q, stropt_mp); 17246 } 17247 17248 freemsg(mp); 17249 /* 17250 * Pass up any data and/or a fin that has been received. 17251 * 17252 * Adjust receive window in case it had decreased 17253 * (because there is data <=> tcp_rcv_list != NULL) 17254 * while the connection was detached. Note that 17255 * in case the eager was flow-controlled, w/o this 17256 * code, the rwnd may never open up again! 17257 */ 17258 if (tcp->tcp_rcv_list != NULL) { 17259 if (IPCL_IS_NONSTR(connp)) { 17260 mblk_t *mp; 17261 int space_left; 17262 int error; 17263 boolean_t push = B_TRUE; 17264 17265 if (!tcp->tcp_fused && (*connp->conn_upcalls->su_recv) 17266 (connp->conn_upper_handle, NULL, 0, 0, &error, 17267 &push) >= 0) { 17268 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 17269 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17270 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17271 tcp_xmit_ctl(NULL, 17272 tcp, (tcp->tcp_swnd == 0) ? 17273 tcp->tcp_suna : tcp->tcp_snxt, 17274 tcp->tcp_rnxt, TH_ACK); 17275 } 17276 } 17277 while ((mp = tcp->tcp_rcv_list) != NULL) { 17278 push = B_TRUE; 17279 tcp->tcp_rcv_list = mp->b_next; 17280 mp->b_next = NULL; 17281 space_left = (*connp->conn_upcalls->su_recv) 17282 (connp->conn_upper_handle, mp, msgdsize(mp), 17283 0, &error, &push); 17284 if (space_left < 0) { 17285 /* 17286 * We should never be in middle of a 17287 * fallback, the squeue guarantees that. 17288 */ 17289 ASSERT(error != EOPNOTSUPP); 17290 } 17291 } 17292 tcp->tcp_rcv_last_head = NULL; 17293 tcp->tcp_rcv_last_tail = NULL; 17294 tcp->tcp_rcv_cnt = 0; 17295 } else { 17296 /* We drain directly in case of fused tcp loopback */ 17297 17298 if (!tcp->tcp_fused && canputnext(q)) { 17299 tcp->tcp_rwnd = q->q_hiwat; 17300 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17301 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 17302 tcp_xmit_ctl(NULL, 17303 tcp, (tcp->tcp_swnd == 0) ? 17304 tcp->tcp_suna : tcp->tcp_snxt, 17305 tcp->tcp_rnxt, TH_ACK); 17306 } 17307 } 17308 17309 (void) tcp_rcv_drain(tcp); 17310 } 17311 17312 /* 17313 * For fused tcp loopback, back-enable peer endpoint 17314 * if it's currently flow-controlled. 17315 */ 17316 if (tcp->tcp_fused) { 17317 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17318 17319 ASSERT(peer_tcp != NULL); 17320 ASSERT(peer_tcp->tcp_fused); 17321 /* 17322 * In order to change the peer's tcp_flow_stopped, 17323 * we need to take locks for both end points. The 17324 * highest address is taken first. 17325 */ 17326 if (peer_tcp > tcp) { 17327 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17328 mutex_enter(&tcp->tcp_non_sq_lock); 17329 } else { 17330 mutex_enter(&tcp->tcp_non_sq_lock); 17331 mutex_enter(&peer_tcp->tcp_non_sq_lock); 17332 } 17333 if (peer_tcp->tcp_flow_stopped) { 17334 tcp_clrqfull(peer_tcp); 17335 TCP_STAT(tcps, tcp_fusion_backenabled); 17336 } 17337 mutex_exit(&peer_tcp->tcp_non_sq_lock); 17338 mutex_exit(&tcp->tcp_non_sq_lock); 17339 } 17340 } 17341 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17342 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17343 tcp->tcp_ordrel_done = B_TRUE; 17344 if (IPCL_IS_NONSTR(connp)) { 17345 ASSERT(tcp->tcp_ordrel_mp == NULL); 17346 (*connp->conn_upcalls->su_opctl)( 17347 connp->conn_upper_handle, 17348 SOCK_OPCTL_SHUT_RECV, 0); 17349 } else { 17350 mp = tcp->tcp_ordrel_mp; 17351 tcp->tcp_ordrel_mp = NULL; 17352 putnext(q, mp); 17353 } 17354 } 17355 if (tcp->tcp_hard_binding) { 17356 tcp->tcp_hard_binding = B_FALSE; 17357 tcp->tcp_hard_bound = B_TRUE; 17358 } 17359 17360 /* We can enable synchronous streams for STREAMS tcp endpoint now */ 17361 if (tcp->tcp_fused && !IPCL_IS_NONSTR(connp) && 17362 tcp->tcp_loopback_peer != NULL && 17363 !IPCL_IS_NONSTR(tcp->tcp_loopback_peer->tcp_connp)) { 17364 tcp_fuse_syncstr_enable_pair(tcp); 17365 } 17366 17367 if (tcp->tcp_ka_enabled) { 17368 tcp->tcp_ka_last_intrvl = 0; 17369 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17370 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17371 } 17372 17373 /* 17374 * At this point, eager is fully established and will 17375 * have the following references - 17376 * 17377 * 2 references for connection to exist (1 for TCP and 1 for IP). 17378 * 1 reference for the squeue which will be dropped by the squeue as 17379 * soon as this function returns. 17380 * There will be 1 additonal reference for being in classifier 17381 * hash list provided something bad hasn't happened. 17382 */ 17383 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17384 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17385 } 17386 17387 /* 17388 * The function called through squeue to get behind listener's perimeter to 17389 * send a deffered conn_ind. 17390 */ 17391 /* ARGSUSED */ 17392 void 17393 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17394 { 17395 conn_t *connp = (conn_t *)arg; 17396 tcp_t *listener = connp->conn_tcp; 17397 struct T_conn_ind *conn_ind; 17398 tcp_t *tcp; 17399 17400 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17401 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17402 conn_ind->OPT_length); 17403 17404 if (listener->tcp_state == TCPS_CLOSED || 17405 TCP_IS_DETACHED(listener)) { 17406 /* 17407 * If listener has closed, it would have caused a 17408 * a cleanup/blowoff to happen for the eager. 17409 * 17410 * We need to drop the ref on eager that was put 17411 * tcp_rput_data() before trying to send the conn_ind 17412 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17413 * and tcp_wput_accept() is sending this deferred conn_ind but 17414 * listener is closed so we drop the ref. 17415 */ 17416 CONN_DEC_REF(tcp->tcp_connp); 17417 freemsg(mp); 17418 return; 17419 } 17420 17421 tcp_ulp_newconn(connp, tcp->tcp_connp, mp); 17422 } 17423 17424 /* ARGSUSED */ 17425 static int 17426 tcp_accept_common(conn_t *lconnp, conn_t *econnp, cred_t *cr) 17427 { 17428 tcp_t *listener, *eager; 17429 mblk_t *opt_mp; 17430 struct tcp_options *tcpopt; 17431 17432 listener = lconnp->conn_tcp; 17433 ASSERT(listener->tcp_state == TCPS_LISTEN); 17434 eager = econnp->conn_tcp; 17435 ASSERT(eager->tcp_listener != NULL); 17436 17437 ASSERT(eager->tcp_rq != NULL); 17438 17439 opt_mp = allocb(sizeof (struct tcp_options), BPRI_HI); 17440 if (opt_mp == NULL) { 17441 return (-TPROTO); 17442 } 17443 bzero((char *)opt_mp->b_rptr, sizeof (struct tcp_options)); 17444 eager->tcp_issocket = B_TRUE; 17445 17446 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17447 econnp->conn_allzones = listener->tcp_connp->conn_allzones; 17448 ASSERT(econnp->conn_netstack == 17449 listener->tcp_connp->conn_netstack); 17450 ASSERT(eager->tcp_tcps == listener->tcp_tcps); 17451 17452 /* Put the ref for IP */ 17453 CONN_INC_REF(econnp); 17454 17455 /* 17456 * We should have minimum of 3 references on the conn 17457 * at this point. One each for TCP and IP and one for 17458 * the T_conn_ind that was sent up when the 3-way handshake 17459 * completed. In the normal case we would also have another 17460 * reference (making a total of 4) for the conn being in the 17461 * classifier hash list. However the eager could have received 17462 * an RST subsequently and tcp_closei_local could have removed 17463 * the eager from the classifier hash list, hence we can't 17464 * assert that reference. 17465 */ 17466 ASSERT(econnp->conn_ref >= 3); 17467 17468 opt_mp->b_datap->db_type = M_SETOPTS; 17469 opt_mp->b_wptr += sizeof (struct tcp_options); 17470 17471 /* 17472 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17473 * from listener to acceptor. 17474 */ 17475 tcpopt = (struct tcp_options *)opt_mp->b_rptr; 17476 tcpopt->to_flags = 0; 17477 17478 if (listener->tcp_bound_if != 0) { 17479 tcpopt->to_flags |= TCPOPT_BOUNDIF; 17480 tcpopt->to_boundif = listener->tcp_bound_if; 17481 } 17482 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17483 tcpopt->to_flags |= TCPOPT_RECVPKTINFO; 17484 } 17485 17486 mutex_enter(&listener->tcp_eager_lock); 17487 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17488 17489 tcp_t *tail; 17490 tcp_t *tcp; 17491 mblk_t *mp1; 17492 17493 tcp = listener->tcp_eager_prev_q0; 17494 /* 17495 * listener->tcp_eager_prev_q0 points to the TAIL of the 17496 * deferred T_conn_ind queue. We need to get to the head 17497 * of the queue in order to send up T_conn_ind the same 17498 * order as how the 3WHS is completed. 17499 */ 17500 while (tcp != listener) { 17501 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17502 !tcp->tcp_kssl_pending) 17503 break; 17504 else 17505 tcp = tcp->tcp_eager_prev_q0; 17506 } 17507 /* None of the pending eagers can be sent up now */ 17508 if (tcp == listener) 17509 goto no_more_eagers; 17510 17511 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17512 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17513 /* Move from q0 to q */ 17514 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17515 listener->tcp_conn_req_cnt_q0--; 17516 listener->tcp_conn_req_cnt_q++; 17517 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17518 tcp->tcp_eager_prev_q0; 17519 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17520 tcp->tcp_eager_next_q0; 17521 tcp->tcp_eager_prev_q0 = NULL; 17522 tcp->tcp_eager_next_q0 = NULL; 17523 tcp->tcp_conn_def_q0 = B_FALSE; 17524 17525 /* Make sure the tcp isn't in the list of droppables */ 17526 ASSERT(tcp->tcp_eager_next_drop_q0 == NULL && 17527 tcp->tcp_eager_prev_drop_q0 == NULL); 17528 17529 /* 17530 * Insert at end of the queue because sockfs sends 17531 * down T_CONN_RES in chronological order. Leaving 17532 * the older conn indications at front of the queue 17533 * helps reducing search time. 17534 */ 17535 tail = listener->tcp_eager_last_q; 17536 if (tail != NULL) { 17537 tail->tcp_eager_next_q = tcp; 17538 } else { 17539 listener->tcp_eager_next_q = tcp; 17540 } 17541 listener->tcp_eager_last_q = tcp; 17542 tcp->tcp_eager_next_q = NULL; 17543 17544 /* Need to get inside the listener perimeter */ 17545 CONN_INC_REF(listener->tcp_connp); 17546 SQUEUE_ENTER_ONE(listener->tcp_connp->conn_sqp, mp1, 17547 tcp_send_pending, listener->tcp_connp, SQ_FILL, 17548 SQTAG_TCP_SEND_PENDING); 17549 } 17550 no_more_eagers: 17551 tcp_eager_unlink(eager); 17552 mutex_exit(&listener->tcp_eager_lock); 17553 17554 /* 17555 * At this point, the eager is detached from the listener 17556 * but we still have an extra refs on eager (apart from the 17557 * usual tcp references). The ref was placed in tcp_rput_data 17558 * before sending the conn_ind in tcp_send_conn_ind. 17559 * The ref will be dropped in tcp_accept_finish(). 17560 */ 17561 SQUEUE_ENTER_ONE(econnp->conn_sqp, opt_mp, tcp_accept_finish, 17562 econnp, SQ_NODRAIN, SQTAG_TCP_ACCEPT_FINISH_Q0); 17563 return (0); 17564 } 17565 17566 int 17567 tcp_accept(sock_lower_handle_t lproto_handle, 17568 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle, 17569 cred_t *cr) 17570 { 17571 conn_t *lconnp, *econnp; 17572 tcp_t *listener, *eager; 17573 tcp_stack_t *tcps; 17574 17575 lconnp = (conn_t *)lproto_handle; 17576 listener = lconnp->conn_tcp; 17577 ASSERT(listener->tcp_state == TCPS_LISTEN); 17578 econnp = (conn_t *)eproto_handle; 17579 eager = econnp->conn_tcp; 17580 ASSERT(eager->tcp_listener != NULL); 17581 tcps = eager->tcp_tcps; 17582 17583 /* 17584 * It is OK to manipulate these fields outside the eager's squeue 17585 * because they will not start being used until tcp_accept_finish 17586 * has been called. 17587 */ 17588 ASSERT(lconnp->conn_upper_handle != NULL); 17589 ASSERT(econnp->conn_upper_handle == NULL); 17590 econnp->conn_upper_handle = sock_handle; 17591 econnp->conn_upcalls = lconnp->conn_upcalls; 17592 ASSERT(IPCL_IS_NONSTR(econnp)); 17593 /* 17594 * Create helper stream if it is a non-TPI TCP connection. 17595 */ 17596 if (ip_create_helper_stream(econnp, tcps->tcps_ldi_ident)) { 17597 ip1dbg(("tcp_accept: create of IP helper stream" 17598 " failed\n")); 17599 return (EPROTO); 17600 } 17601 eager->tcp_rq = econnp->conn_rq; 17602 eager->tcp_wq = econnp->conn_wq; 17603 17604 ASSERT(eager->tcp_rq != NULL); 17605 17606 return (tcp_accept_common(lconnp, econnp, cr)); 17607 } 17608 17609 17610 /* 17611 * This is the STREAMS entry point for T_CONN_RES coming down on 17612 * Acceptor STREAM when sockfs listener does accept processing. 17613 * Read the block comment on top of tcp_conn_request(). 17614 */ 17615 void 17616 tcp_tpi_accept(queue_t *q, mblk_t *mp) 17617 { 17618 queue_t *rq = RD(q); 17619 struct T_conn_res *conn_res; 17620 tcp_t *eager; 17621 tcp_t *listener; 17622 struct T_ok_ack *ok; 17623 t_scalar_t PRIM_type; 17624 conn_t *econnp; 17625 cred_t *cr; 17626 17627 ASSERT(DB_TYPE(mp) == M_PROTO); 17628 17629 /* 17630 * All Solaris components should pass a db_credp 17631 * for this TPI message, hence we ASSERT. 17632 * But in case there is some other M_PROTO that looks 17633 * like a TPI message sent by some other kernel 17634 * component, we check and return an error. 17635 */ 17636 cr = msg_getcred(mp, NULL); 17637 ASSERT(cr != NULL); 17638 if (cr == NULL) { 17639 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL); 17640 if (mp != NULL) 17641 putnext(rq, mp); 17642 return; 17643 } 17644 conn_res = (struct T_conn_res *)mp->b_rptr; 17645 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17646 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17647 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17648 if (mp != NULL) 17649 putnext(rq, mp); 17650 return; 17651 } 17652 switch (conn_res->PRIM_type) { 17653 case O_T_CONN_RES: 17654 case T_CONN_RES: 17655 /* 17656 * We pass up an err ack if allocb fails. This will 17657 * cause sockfs to issue a T_DISCON_REQ which will cause 17658 * tcp_eager_blowoff to be called. sockfs will then call 17659 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17660 * we need to do the allocb up here because we have to 17661 * make sure rq->q_qinfo->qi_qclose still points to the 17662 * correct function (tcp_tpi_close_accept) in case allocb 17663 * fails. 17664 */ 17665 bcopy(mp->b_rptr + conn_res->OPT_offset, 17666 &eager, conn_res->OPT_length); 17667 PRIM_type = conn_res->PRIM_type; 17668 mp->b_datap->db_type = M_PCPROTO; 17669 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17670 ok = (struct T_ok_ack *)mp->b_rptr; 17671 ok->PRIM_type = T_OK_ACK; 17672 ok->CORRECT_prim = PRIM_type; 17673 econnp = eager->tcp_connp; 17674 econnp->conn_dev = (dev_t)RD(q)->q_ptr; 17675 econnp->conn_minor_arena = (vmem_t *)(WR(q)->q_ptr); 17676 eager->tcp_rq = rq; 17677 eager->tcp_wq = q; 17678 rq->q_ptr = econnp; 17679 rq->q_qinfo = &tcp_rinitv4; /* No open - same as rinitv6 */ 17680 q->q_ptr = econnp; 17681 q->q_qinfo = &tcp_winit; 17682 listener = eager->tcp_listener; 17683 17684 if (tcp_accept_common(listener->tcp_connp, 17685 econnp, cr) < 0) { 17686 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17687 if (mp != NULL) 17688 putnext(rq, mp); 17689 return; 17690 } 17691 17692 /* 17693 * Send the new local address also up to sockfs. There 17694 * should already be enough space in the mp that came 17695 * down from soaccept(). 17696 */ 17697 if (eager->tcp_family == AF_INET) { 17698 sin_t *sin; 17699 17700 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17701 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17702 sin = (sin_t *)mp->b_wptr; 17703 mp->b_wptr += sizeof (sin_t); 17704 sin->sin_family = AF_INET; 17705 sin->sin_port = eager->tcp_lport; 17706 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17707 } else { 17708 sin6_t *sin6; 17709 17710 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17711 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17712 sin6 = (sin6_t *)mp->b_wptr; 17713 mp->b_wptr += sizeof (sin6_t); 17714 sin6->sin6_family = AF_INET6; 17715 sin6->sin6_port = eager->tcp_lport; 17716 if (eager->tcp_ipversion == IPV4_VERSION) { 17717 sin6->sin6_flowinfo = 0; 17718 IN6_IPADDR_TO_V4MAPPED( 17719 eager->tcp_ipha->ipha_src, 17720 &sin6->sin6_addr); 17721 } else { 17722 ASSERT(eager->tcp_ip6h != NULL); 17723 sin6->sin6_flowinfo = 17724 eager->tcp_ip6h->ip6_vcf & 17725 ~IPV6_VERS_AND_FLOW_MASK; 17726 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17727 } 17728 sin6->sin6_scope_id = 0; 17729 sin6->__sin6_src_id = 0; 17730 } 17731 17732 putnext(rq, mp); 17733 return; 17734 default: 17735 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17736 if (mp != NULL) 17737 putnext(rq, mp); 17738 return; 17739 } 17740 } 17741 17742 static int 17743 tcp_do_getsockname(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17744 { 17745 sin_t *sin = (sin_t *)sa; 17746 sin6_t *sin6 = (sin6_t *)sa; 17747 17748 switch (tcp->tcp_family) { 17749 case AF_INET: 17750 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17751 17752 if (*salenp < sizeof (sin_t)) 17753 return (EINVAL); 17754 17755 *sin = sin_null; 17756 sin->sin_family = AF_INET; 17757 if (tcp->tcp_state >= TCPS_BOUND) { 17758 sin->sin_port = tcp->tcp_lport; 17759 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 17760 } 17761 *salenp = sizeof (sin_t); 17762 break; 17763 17764 case AF_INET6: 17765 if (*salenp < sizeof (sin6_t)) 17766 return (EINVAL); 17767 17768 *sin6 = sin6_null; 17769 sin6->sin6_family = AF_INET6; 17770 if (tcp->tcp_state >= TCPS_BOUND) { 17771 sin6->sin6_port = tcp->tcp_lport; 17772 if (tcp->tcp_ipversion == IPV4_VERSION) { 17773 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 17774 &sin6->sin6_addr); 17775 } else { 17776 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 17777 } 17778 } 17779 *salenp = sizeof (sin6_t); 17780 break; 17781 } 17782 17783 return (0); 17784 } 17785 17786 static int 17787 tcp_do_getpeername(tcp_t *tcp, struct sockaddr *sa, uint_t *salenp) 17788 { 17789 sin_t *sin = (sin_t *)sa; 17790 sin6_t *sin6 = (sin6_t *)sa; 17791 17792 if (tcp->tcp_state < TCPS_SYN_RCVD) 17793 return (ENOTCONN); 17794 17795 switch (tcp->tcp_family) { 17796 case AF_INET: 17797 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 17798 17799 if (*salenp < sizeof (sin_t)) 17800 return (EINVAL); 17801 17802 *sin = sin_null; 17803 sin->sin_family = AF_INET; 17804 sin->sin_port = tcp->tcp_fport; 17805 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 17806 sin->sin_addr.s_addr); 17807 *salenp = sizeof (sin_t); 17808 break; 17809 17810 case AF_INET6: 17811 if (*salenp < sizeof (sin6_t)) 17812 return (EINVAL); 17813 17814 *sin6 = sin6_null; 17815 sin6->sin6_family = AF_INET6; 17816 sin6->sin6_port = tcp->tcp_fport; 17817 sin6->sin6_addr = tcp->tcp_remote_v6; 17818 if (tcp->tcp_ipversion == IPV6_VERSION) { 17819 sin6->sin6_flowinfo = tcp->tcp_ip6h->ip6_vcf & 17820 ~IPV6_VERS_AND_FLOW_MASK; 17821 } 17822 *salenp = sizeof (sin6_t); 17823 break; 17824 } 17825 17826 return (0); 17827 } 17828 17829 /* 17830 * Handle special out-of-band ioctl requests (see PSARC/2008/265). 17831 */ 17832 static void 17833 tcp_wput_cmdblk(queue_t *q, mblk_t *mp) 17834 { 17835 void *data; 17836 mblk_t *datamp = mp->b_cont; 17837 tcp_t *tcp = Q_TO_TCP(q); 17838 cmdblk_t *cmdp = (cmdblk_t *)mp->b_rptr; 17839 17840 if (datamp == NULL || MBLKL(datamp) < cmdp->cb_len) { 17841 cmdp->cb_error = EPROTO; 17842 qreply(q, mp); 17843 return; 17844 } 17845 17846 data = datamp->b_rptr; 17847 17848 switch (cmdp->cb_cmd) { 17849 case TI_GETPEERNAME: 17850 cmdp->cb_error = tcp_do_getpeername(tcp, data, &cmdp->cb_len); 17851 break; 17852 case TI_GETMYNAME: 17853 cmdp->cb_error = tcp_do_getsockname(tcp, data, &cmdp->cb_len); 17854 break; 17855 default: 17856 cmdp->cb_error = EINVAL; 17857 break; 17858 } 17859 17860 qreply(q, mp); 17861 } 17862 17863 void 17864 tcp_wput(queue_t *q, mblk_t *mp) 17865 { 17866 conn_t *connp = Q_TO_CONN(q); 17867 tcp_t *tcp; 17868 void (*output_proc)(); 17869 t_scalar_t type; 17870 uchar_t *rptr; 17871 struct iocblk *iocp; 17872 size_t size; 17873 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 17874 17875 ASSERT(connp->conn_ref >= 2); 17876 17877 switch (DB_TYPE(mp)) { 17878 case M_DATA: 17879 tcp = connp->conn_tcp; 17880 ASSERT(tcp != NULL); 17881 17882 size = msgdsize(mp); 17883 17884 mutex_enter(&tcp->tcp_non_sq_lock); 17885 tcp->tcp_squeue_bytes += size; 17886 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17887 tcp_setqfull(tcp); 17888 } 17889 mutex_exit(&tcp->tcp_non_sq_lock); 17890 17891 CONN_INC_REF(connp); 17892 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, connp, 17893 tcp_squeue_flag, SQTAG_TCP_OUTPUT); 17894 return; 17895 17896 case M_CMD: 17897 tcp_wput_cmdblk(q, mp); 17898 return; 17899 17900 case M_PROTO: 17901 case M_PCPROTO: 17902 /* 17903 * if it is a snmp message, don't get behind the squeue 17904 */ 17905 tcp = connp->conn_tcp; 17906 rptr = mp->b_rptr; 17907 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17908 type = ((union T_primitives *)rptr)->type; 17909 } else { 17910 if (tcp->tcp_debug) { 17911 (void) strlog(TCP_MOD_ID, 0, 1, 17912 SL_ERROR|SL_TRACE, 17913 "tcp_wput_proto, dropping one..."); 17914 } 17915 freemsg(mp); 17916 return; 17917 } 17918 if (type == T_SVR4_OPTMGMT_REQ) { 17919 /* 17920 * All Solaris components should pass a db_credp 17921 * for this TPI message, hence we ASSERT. 17922 * But in case there is some other M_PROTO that looks 17923 * like a TPI message sent by some other kernel 17924 * component, we check and return an error. 17925 */ 17926 cred_t *cr = msg_getcred(mp, NULL); 17927 17928 ASSERT(cr != NULL); 17929 if (cr == NULL) { 17930 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 17931 return; 17932 } 17933 if (snmpcom_req(q, mp, tcp_snmp_set, ip_snmp_get, 17934 cr)) { 17935 /* 17936 * This was a SNMP request 17937 */ 17938 return; 17939 } else { 17940 output_proc = tcp_wput_proto; 17941 } 17942 } else { 17943 output_proc = tcp_wput_proto; 17944 } 17945 break; 17946 case M_IOCTL: 17947 /* 17948 * Most ioctls can be processed right away without going via 17949 * squeues - process them right here. Those that do require 17950 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17951 * are processed by tcp_wput_ioctl(). 17952 */ 17953 iocp = (struct iocblk *)mp->b_rptr; 17954 tcp = connp->conn_tcp; 17955 17956 switch (iocp->ioc_cmd) { 17957 case TCP_IOC_ABORT_CONN: 17958 tcp_ioctl_abort_conn(q, mp); 17959 return; 17960 case TI_GETPEERNAME: 17961 case TI_GETMYNAME: 17962 mi_copyin(q, mp, NULL, 17963 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17964 return; 17965 case ND_SET: 17966 /* nd_getset does the necessary checks */ 17967 case ND_GET: 17968 if (!nd_getset(q, tcps->tcps_g_nd, mp)) { 17969 CALL_IP_WPUT(connp, q, mp); 17970 return; 17971 } 17972 qreply(q, mp); 17973 return; 17974 case TCP_IOC_DEFAULT_Q: 17975 /* 17976 * Wants to be the default wq. Check the credentials 17977 * first, the rest is executed via squeue. 17978 */ 17979 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 17980 iocp->ioc_error = EPERM; 17981 iocp->ioc_count = 0; 17982 mp->b_datap->db_type = M_IOCACK; 17983 qreply(q, mp); 17984 return; 17985 } 17986 output_proc = tcp_wput_ioctl; 17987 break; 17988 default: 17989 output_proc = tcp_wput_ioctl; 17990 break; 17991 } 17992 break; 17993 default: 17994 output_proc = tcp_wput_nondata; 17995 break; 17996 } 17997 17998 CONN_INC_REF(connp); 17999 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, output_proc, connp, 18000 tcp_squeue_flag, SQTAG_TCP_WPUT_OTHER); 18001 } 18002 18003 /* 18004 * Initial STREAMS write side put() procedure for sockets. It tries to 18005 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 18006 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 18007 * are handled by tcp_wput() as usual. 18008 * 18009 * All further messages will also be handled by tcp_wput() because we cannot 18010 * be sure that the above short cut is safe later. 18011 */ 18012 static void 18013 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18014 { 18015 conn_t *connp = Q_TO_CONN(wq); 18016 tcp_t *tcp = connp->conn_tcp; 18017 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18018 18019 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18020 wq->q_qinfo = &tcp_winit; 18021 18022 ASSERT(IPCL_IS_TCP(connp)); 18023 ASSERT(TCP_IS_SOCKET(tcp)); 18024 18025 if (DB_TYPE(mp) == M_PCPROTO && 18026 MBLKL(mp) == sizeof (struct T_capability_req) && 18027 car->PRIM_type == T_CAPABILITY_REQ) { 18028 tcp_capability_req(tcp, mp); 18029 return; 18030 } 18031 18032 tcp_wput(wq, mp); 18033 } 18034 18035 /* ARGSUSED */ 18036 static void 18037 tcp_wput_fallback(queue_t *wq, mblk_t *mp) 18038 { 18039 #ifdef DEBUG 18040 cmn_err(CE_CONT, "tcp_wput_fallback: Message during fallback \n"); 18041 #endif 18042 freemsg(mp); 18043 } 18044 18045 static boolean_t 18046 tcp_zcopy_check(tcp_t *tcp) 18047 { 18048 conn_t *connp = tcp->tcp_connp; 18049 ire_t *ire; 18050 boolean_t zc_enabled = B_FALSE; 18051 tcp_stack_t *tcps = tcp->tcp_tcps; 18052 18053 if (do_tcpzcopy == 2) 18054 zc_enabled = B_TRUE; 18055 else if (tcp->tcp_ipversion == IPV4_VERSION && 18056 IPCL_IS_CONNECTED(connp) && 18057 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18058 connp->conn_dontroute == 0 && 18059 !connp->conn_nexthop_set && 18060 connp->conn_outgoing_ill == NULL && 18061 do_tcpzcopy == 1) { 18062 /* 18063 * the checks above closely resemble the fast path checks 18064 * in tcp_send_data(). 18065 */ 18066 mutex_enter(&connp->conn_lock); 18067 ire = connp->conn_ire_cache; 18068 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18069 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18070 IRE_REFHOLD(ire); 18071 if (ire->ire_stq != NULL) { 18072 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18073 18074 zc_enabled = ill && (ill->ill_capabilities & 18075 ILL_CAPAB_ZEROCOPY) && 18076 (ill->ill_zerocopy_capab-> 18077 ill_zerocopy_flags != 0); 18078 } 18079 IRE_REFRELE(ire); 18080 } 18081 mutex_exit(&connp->conn_lock); 18082 } 18083 tcp->tcp_snd_zcopy_on = zc_enabled; 18084 if (!TCP_IS_DETACHED(tcp)) { 18085 if (zc_enabled) { 18086 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18087 ZCVMSAFE); 18088 TCP_STAT(tcps, tcp_zcopy_on); 18089 } else { 18090 (void) proto_set_tx_copyopt(tcp->tcp_rq, connp, 18091 ZCVMUNSAFE); 18092 TCP_STAT(tcps, tcp_zcopy_off); 18093 } 18094 } 18095 return (zc_enabled); 18096 } 18097 18098 static mblk_t * 18099 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18100 { 18101 tcp_stack_t *tcps = tcp->tcp_tcps; 18102 18103 if (do_tcpzcopy == 2) 18104 return (bp); 18105 else if (tcp->tcp_snd_zcopy_on) { 18106 tcp->tcp_snd_zcopy_on = B_FALSE; 18107 if (!TCP_IS_DETACHED(tcp)) { 18108 (void) proto_set_tx_copyopt(tcp->tcp_rq, tcp->tcp_connp, 18109 ZCVMUNSAFE); 18110 TCP_STAT(tcps, tcp_zcopy_disable); 18111 } 18112 } 18113 return (tcp_zcopy_backoff(tcp, bp, 0)); 18114 } 18115 18116 /* 18117 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18118 * the original desballoca'ed segmapped mblk. 18119 */ 18120 static mblk_t * 18121 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18122 { 18123 mblk_t *head, *tail, *nbp; 18124 tcp_stack_t *tcps = tcp->tcp_tcps; 18125 18126 if (IS_VMLOANED_MBLK(bp)) { 18127 TCP_STAT(tcps, tcp_zcopy_backoff); 18128 if ((head = copyb(bp)) == NULL) { 18129 /* fail to backoff; leave it for the next backoff */ 18130 tcp->tcp_xmit_zc_clean = B_FALSE; 18131 return (bp); 18132 } 18133 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18134 if (fix_xmitlist) 18135 tcp_zcopy_notify(tcp); 18136 else 18137 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18138 } 18139 nbp = bp->b_cont; 18140 if (fix_xmitlist) { 18141 head->b_prev = bp->b_prev; 18142 head->b_next = bp->b_next; 18143 if (tcp->tcp_xmit_tail == bp) 18144 tcp->tcp_xmit_tail = head; 18145 } 18146 bp->b_next = NULL; 18147 bp->b_prev = NULL; 18148 freeb(bp); 18149 } else { 18150 head = bp; 18151 nbp = bp->b_cont; 18152 } 18153 tail = head; 18154 while (nbp) { 18155 if (IS_VMLOANED_MBLK(nbp)) { 18156 TCP_STAT(tcps, tcp_zcopy_backoff); 18157 if ((tail->b_cont = copyb(nbp)) == NULL) { 18158 tcp->tcp_xmit_zc_clean = B_FALSE; 18159 tail->b_cont = nbp; 18160 return (head); 18161 } 18162 tail = tail->b_cont; 18163 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18164 if (fix_xmitlist) 18165 tcp_zcopy_notify(tcp); 18166 else 18167 tail->b_datap->db_struioflag |= 18168 STRUIO_ZCNOTIFY; 18169 } 18170 bp = nbp; 18171 nbp = nbp->b_cont; 18172 if (fix_xmitlist) { 18173 tail->b_prev = bp->b_prev; 18174 tail->b_next = bp->b_next; 18175 if (tcp->tcp_xmit_tail == bp) 18176 tcp->tcp_xmit_tail = tail; 18177 } 18178 bp->b_next = NULL; 18179 bp->b_prev = NULL; 18180 freeb(bp); 18181 } else { 18182 tail->b_cont = nbp; 18183 tail = nbp; 18184 nbp = nbp->b_cont; 18185 } 18186 } 18187 if (fix_xmitlist) { 18188 tcp->tcp_xmit_last = tail; 18189 tcp->tcp_xmit_zc_clean = B_TRUE; 18190 } 18191 return (head); 18192 } 18193 18194 static void 18195 tcp_zcopy_notify(tcp_t *tcp) 18196 { 18197 struct stdata *stp; 18198 conn_t *connp; 18199 18200 if (tcp->tcp_detached) 18201 return; 18202 connp = tcp->tcp_connp; 18203 if (IPCL_IS_NONSTR(connp)) { 18204 (*connp->conn_upcalls->su_zcopy_notify) 18205 (connp->conn_upper_handle); 18206 return; 18207 } 18208 stp = STREAM(tcp->tcp_rq); 18209 mutex_enter(&stp->sd_lock); 18210 stp->sd_flag |= STZCNOTIFY; 18211 cv_broadcast(&stp->sd_zcopy_wait); 18212 mutex_exit(&stp->sd_lock); 18213 } 18214 18215 static boolean_t 18216 tcp_send_find_ire(tcp_t *tcp, ipaddr_t *dst, ire_t **irep) 18217 { 18218 ire_t *ire; 18219 conn_t *connp = tcp->tcp_connp; 18220 tcp_stack_t *tcps = tcp->tcp_tcps; 18221 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18222 18223 mutex_enter(&connp->conn_lock); 18224 ire = connp->conn_ire_cache; 18225 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18226 18227 if ((ire != NULL) && 18228 (((dst != NULL) && (ire->ire_addr == *dst)) || ((dst == NULL) && 18229 IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, &tcp->tcp_ip6h->ip6_dst))) && 18230 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18231 IRE_REFHOLD(ire); 18232 mutex_exit(&connp->conn_lock); 18233 } else { 18234 boolean_t cached = B_FALSE; 18235 ts_label_t *tsl; 18236 18237 /* force a recheck later on */ 18238 tcp->tcp_ire_ill_check_done = B_FALSE; 18239 18240 TCP_DBGSTAT(tcps, tcp_ire_null1); 18241 connp->conn_ire_cache = NULL; 18242 mutex_exit(&connp->conn_lock); 18243 18244 if (ire != NULL) 18245 IRE_REFRELE_NOTR(ire); 18246 18247 tsl = crgetlabel(CONN_CRED(connp)); 18248 ire = (dst ? 18249 ire_cache_lookup(*dst, connp->conn_zoneid, tsl, ipst) : 18250 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18251 connp->conn_zoneid, tsl, ipst)); 18252 18253 if (ire == NULL) { 18254 TCP_STAT(tcps, tcp_ire_null); 18255 return (B_FALSE); 18256 } 18257 18258 IRE_REFHOLD_NOTR(ire); 18259 18260 mutex_enter(&connp->conn_lock); 18261 if (CONN_CACHE_IRE(connp)) { 18262 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18263 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18264 TCP_CHECK_IREINFO(tcp, ire); 18265 connp->conn_ire_cache = ire; 18266 cached = B_TRUE; 18267 } 18268 rw_exit(&ire->ire_bucket->irb_lock); 18269 } 18270 mutex_exit(&connp->conn_lock); 18271 18272 /* 18273 * We can continue to use the ire but since it was 18274 * not cached, we should drop the extra reference. 18275 */ 18276 if (!cached) 18277 IRE_REFRELE_NOTR(ire); 18278 18279 /* 18280 * Rampart note: no need to select a new label here, since 18281 * labels are not allowed to change during the life of a TCP 18282 * connection. 18283 */ 18284 } 18285 18286 *irep = ire; 18287 18288 return (B_TRUE); 18289 } 18290 18291 /* 18292 * Called from tcp_send() or tcp_send_data() to find workable IRE. 18293 * 18294 * 0 = success; 18295 * 1 = failed to find ire and ill. 18296 */ 18297 static boolean_t 18298 tcp_send_find_ire_ill(tcp_t *tcp, mblk_t *mp, ire_t **irep, ill_t **illp) 18299 { 18300 ipha_t *ipha; 18301 ipaddr_t dst; 18302 ire_t *ire; 18303 ill_t *ill; 18304 mblk_t *ire_fp_mp; 18305 tcp_stack_t *tcps = tcp->tcp_tcps; 18306 18307 if (mp != NULL) 18308 ipha = (ipha_t *)mp->b_rptr; 18309 else 18310 ipha = tcp->tcp_ipha; 18311 dst = ipha->ipha_dst; 18312 18313 if (!tcp_send_find_ire(tcp, &dst, &ire)) 18314 return (B_FALSE); 18315 18316 if ((ire->ire_flags & RTF_MULTIRT) || 18317 (ire->ire_stq == NULL) || 18318 (ire->ire_nce == NULL) || 18319 ((ire_fp_mp = ire->ire_nce->nce_fp_mp) == NULL) || 18320 ((mp != NULL) && (ire->ire_max_frag < ntohs(ipha->ipha_length) || 18321 MBLKL(ire_fp_mp) > MBLKHEAD(mp)))) { 18322 TCP_STAT(tcps, tcp_ip_ire_send); 18323 IRE_REFRELE(ire); 18324 return (B_FALSE); 18325 } 18326 18327 ill = ire_to_ill(ire); 18328 ASSERT(ill != NULL); 18329 18330 if (!tcp->tcp_ire_ill_check_done) { 18331 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18332 tcp->tcp_ire_ill_check_done = B_TRUE; 18333 } 18334 18335 *irep = ire; 18336 *illp = ill; 18337 18338 return (B_TRUE); 18339 } 18340 18341 static void 18342 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18343 { 18344 ipha_t *ipha; 18345 ipaddr_t src; 18346 ipaddr_t dst; 18347 uint32_t cksum; 18348 ire_t *ire; 18349 uint16_t *up; 18350 ill_t *ill; 18351 conn_t *connp = tcp->tcp_connp; 18352 uint32_t hcksum_txflags = 0; 18353 mblk_t *ire_fp_mp; 18354 uint_t ire_fp_mp_len; 18355 tcp_stack_t *tcps = tcp->tcp_tcps; 18356 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 18357 cred_t *cr; 18358 pid_t cpid; 18359 18360 ASSERT(DB_TYPE(mp) == M_DATA); 18361 18362 /* 18363 * Here we need to handle the overloading of the cred_t for 18364 * both getpeerucred and TX. 18365 * If this is a SYN then the caller already set db_credp so 18366 * that getpeerucred will work. But if TX is in use we might have 18367 * a conn_peercred which is different, and we need to use that cred 18368 * to make TX use the correct label and label dependent route. 18369 */ 18370 if (is_system_labeled()) { 18371 cr = msg_getcred(mp, &cpid); 18372 if (cr == NULL || connp->conn_peercred != NULL) 18373 mblk_setcred(mp, CONN_CRED(connp), cpid); 18374 } 18375 18376 ipha = (ipha_t *)mp->b_rptr; 18377 src = ipha->ipha_src; 18378 dst = ipha->ipha_dst; 18379 18380 ASSERT(q != NULL); 18381 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 18382 18383 /* 18384 * Drop off fast path for IPv6 and also if options are present or 18385 * we need to resolve a TS label. 18386 */ 18387 if (tcp->tcp_ipversion != IPV4_VERSION || 18388 !IPCL_IS_CONNECTED(connp) || 18389 !CONN_IS_LSO_MD_FASTPATH(connp) || 18390 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18391 !connp->conn_ulp_labeled || 18392 ipha->ipha_ident == IP_HDR_INCLUDED || 18393 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18394 IPP_ENABLED(IPP_LOCAL_OUT, ipst)) { 18395 if (tcp->tcp_snd_zcopy_aware) 18396 mp = tcp_zcopy_disable(tcp, mp); 18397 TCP_STAT(tcps, tcp_ip_send); 18398 CALL_IP_WPUT(connp, q, mp); 18399 return; 18400 } 18401 18402 if (!tcp_send_find_ire_ill(tcp, mp, &ire, &ill)) { 18403 if (tcp->tcp_snd_zcopy_aware) 18404 mp = tcp_zcopy_backoff(tcp, mp, 0); 18405 CALL_IP_WPUT(connp, q, mp); 18406 return; 18407 } 18408 ire_fp_mp = ire->ire_nce->nce_fp_mp; 18409 ire_fp_mp_len = MBLKL(ire_fp_mp); 18410 18411 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18412 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18413 #ifndef _BIG_ENDIAN 18414 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18415 #endif 18416 18417 /* 18418 * Check to see if we need to re-enable LSO/MDT for this connection 18419 * because it was previously disabled due to changes in the ill; 18420 * note that by doing it here, this re-enabling only applies when 18421 * the packet is not dispatched through CALL_IP_WPUT(). 18422 * 18423 * That means for IPv4, it is worth re-enabling LSO/MDT for the fastpath 18424 * case, since that's how we ended up here. For IPv6, we do the 18425 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18426 */ 18427 if (connp->conn_lso_ok && !tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 18428 /* 18429 * Restore LSO for this connection, so that next time around 18430 * it is eligible to go through tcp_lsosend() path again. 18431 */ 18432 TCP_STAT(tcps, tcp_lso_enabled); 18433 tcp->tcp_lso = B_TRUE; 18434 ip1dbg(("tcp_send_data: reenabling LSO for connp %p on " 18435 "interface %s\n", (void *)connp, ill->ill_name)); 18436 } else if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18437 /* 18438 * Restore MDT for this connection, so that next time around 18439 * it is eligible to go through tcp_multisend() path again. 18440 */ 18441 TCP_STAT(tcps, tcp_mdt_conn_resumed1); 18442 tcp->tcp_mdt = B_TRUE; 18443 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18444 "interface %s\n", (void *)connp, ill->ill_name)); 18445 } 18446 18447 if (tcp->tcp_snd_zcopy_aware) { 18448 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18449 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18450 mp = tcp_zcopy_disable(tcp, mp); 18451 /* 18452 * we shouldn't need to reset ipha as the mp containing 18453 * ipha should never be a zero-copy mp. 18454 */ 18455 } 18456 18457 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18458 ASSERT(ill->ill_hcksum_capab != NULL); 18459 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18460 } 18461 18462 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18463 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18464 18465 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18466 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18467 18468 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18469 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18470 18471 /* Software checksum? */ 18472 if (DB_CKSUMFLAGS(mp) == 0) { 18473 TCP_STAT(tcps, tcp_out_sw_cksum); 18474 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 18475 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18476 } 18477 18478 /* Calculate IP header checksum if hardware isn't capable */ 18479 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18480 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18481 ((uint16_t *)ipha)[4]); 18482 } 18483 18484 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18485 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18486 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18487 18488 UPDATE_OB_PKT_COUNT(ire); 18489 ire->ire_last_used_time = lbolt; 18490 18491 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 18492 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 18493 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 18494 ntohs(ipha->ipha_length)); 18495 18496 DTRACE_PROBE4(ip4__physical__out__start, 18497 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 18498 FW_HOOKS(ipst->ips_ip4_physical_out_event, 18499 ipst->ips_ipv4firewall_physical_out, 18500 NULL, ill, ipha, mp, mp, 0, ipst); 18501 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 18502 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 18503 18504 if (mp != NULL) { 18505 if (ipst->ips_ipobs_enabled) { 18506 zoneid_t szone; 18507 18508 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 18509 ipst, ALL_ZONES); 18510 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 18511 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 18512 } 18513 18514 ILL_SEND_TX(ill, ire, connp, mp, 0, NULL); 18515 } 18516 18517 IRE_REFRELE(ire); 18518 } 18519 18520 /* 18521 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18522 * if the receiver shrinks the window, i.e. moves the right window to the 18523 * left, the we should not send new data, but should retransmit normally the 18524 * old unacked data between suna and suna + swnd. We might has sent data 18525 * that is now outside the new window, pretend that we didn't send it. 18526 */ 18527 static void 18528 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18529 { 18530 uint32_t snxt = tcp->tcp_snxt; 18531 mblk_t *xmit_tail; 18532 int32_t offset; 18533 18534 ASSERT(shrunk_count > 0); 18535 18536 /* Pretend we didn't send the data outside the window */ 18537 snxt -= shrunk_count; 18538 18539 /* Get the mblk and the offset in it per the shrunk window */ 18540 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18541 18542 ASSERT(xmit_tail != NULL); 18543 18544 /* Reset all the values per the now shrunk window */ 18545 tcp->tcp_snxt = snxt; 18546 tcp->tcp_xmit_tail = xmit_tail; 18547 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18548 offset; 18549 tcp->tcp_unsent += shrunk_count; 18550 18551 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18552 /* 18553 * Make sure the timer is running so that we will probe a zero 18554 * window. 18555 */ 18556 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18557 } 18558 18559 18560 /* 18561 * The TCP normal data output path. 18562 * NOTE: the logic of the fast path is duplicated from this function. 18563 */ 18564 static void 18565 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18566 { 18567 int len; 18568 mblk_t *local_time; 18569 mblk_t *mp1; 18570 uint32_t snxt; 18571 int tail_unsent; 18572 int tcpstate; 18573 int usable = 0; 18574 mblk_t *xmit_tail; 18575 queue_t *q = tcp->tcp_wq; 18576 int32_t mss; 18577 int32_t num_sack_blk = 0; 18578 int32_t tcp_hdr_len; 18579 int32_t tcp_tcp_hdr_len; 18580 int mdt_thres; 18581 int rc; 18582 tcp_stack_t *tcps = tcp->tcp_tcps; 18583 ip_stack_t *ipst; 18584 18585 tcpstate = tcp->tcp_state; 18586 if (mp == NULL) { 18587 /* 18588 * tcp_wput_data() with NULL mp should only be called when 18589 * there is unsent data. 18590 */ 18591 ASSERT(tcp->tcp_unsent > 0); 18592 /* Really tacky... but we need this for detached closes. */ 18593 len = tcp->tcp_unsent; 18594 goto data_null; 18595 } 18596 18597 #if CCS_STATS 18598 wrw_stats.tot.count++; 18599 wrw_stats.tot.bytes += msgdsize(mp); 18600 #endif 18601 ASSERT(mp->b_datap->db_type == M_DATA); 18602 /* 18603 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18604 * or before a connection attempt has begun. 18605 */ 18606 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18607 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18608 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18609 #ifdef DEBUG 18610 cmn_err(CE_WARN, 18611 "tcp_wput_data: data after ordrel, %s", 18612 tcp_display(tcp, NULL, 18613 DISP_ADDR_AND_PORT)); 18614 #else 18615 if (tcp->tcp_debug) { 18616 (void) strlog(TCP_MOD_ID, 0, 1, 18617 SL_TRACE|SL_ERROR, 18618 "tcp_wput_data: data after ordrel, %s\n", 18619 tcp_display(tcp, NULL, 18620 DISP_ADDR_AND_PORT)); 18621 } 18622 #endif /* DEBUG */ 18623 } 18624 if (tcp->tcp_snd_zcopy_aware && 18625 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18626 tcp_zcopy_notify(tcp); 18627 freemsg(mp); 18628 mutex_enter(&tcp->tcp_non_sq_lock); 18629 if (tcp->tcp_flow_stopped && 18630 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18631 tcp_clrqfull(tcp); 18632 } 18633 mutex_exit(&tcp->tcp_non_sq_lock); 18634 return; 18635 } 18636 18637 /* Strip empties */ 18638 for (;;) { 18639 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18640 (uintptr_t)INT_MAX); 18641 len = (int)(mp->b_wptr - mp->b_rptr); 18642 if (len > 0) 18643 break; 18644 mp1 = mp; 18645 mp = mp->b_cont; 18646 freeb(mp1); 18647 if (!mp) { 18648 return; 18649 } 18650 } 18651 18652 /* If we are the first on the list ... */ 18653 if (tcp->tcp_xmit_head == NULL) { 18654 tcp->tcp_xmit_head = mp; 18655 tcp->tcp_xmit_tail = mp; 18656 tcp->tcp_xmit_tail_unsent = len; 18657 } else { 18658 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18659 struct datab *dp; 18660 18661 mp1 = tcp->tcp_xmit_last; 18662 if (len < tcp_tx_pull_len && 18663 (dp = mp1->b_datap)->db_ref == 1 && 18664 dp->db_lim - mp1->b_wptr >= len) { 18665 ASSERT(len > 0); 18666 ASSERT(!mp1->b_cont); 18667 if (len == 1) { 18668 *mp1->b_wptr++ = *mp->b_rptr; 18669 } else { 18670 bcopy(mp->b_rptr, mp1->b_wptr, len); 18671 mp1->b_wptr += len; 18672 } 18673 if (mp1 == tcp->tcp_xmit_tail) 18674 tcp->tcp_xmit_tail_unsent += len; 18675 mp1->b_cont = mp->b_cont; 18676 if (tcp->tcp_snd_zcopy_aware && 18677 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18678 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18679 freeb(mp); 18680 mp = mp1; 18681 } else { 18682 tcp->tcp_xmit_last->b_cont = mp; 18683 } 18684 len += tcp->tcp_unsent; 18685 } 18686 18687 /* Tack on however many more positive length mblks we have */ 18688 if ((mp1 = mp->b_cont) != NULL) { 18689 do { 18690 int tlen; 18691 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18692 (uintptr_t)INT_MAX); 18693 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18694 if (tlen <= 0) { 18695 mp->b_cont = mp1->b_cont; 18696 freeb(mp1); 18697 } else { 18698 len += tlen; 18699 mp = mp1; 18700 } 18701 } while ((mp1 = mp->b_cont) != NULL); 18702 } 18703 tcp->tcp_xmit_last = mp; 18704 tcp->tcp_unsent = len; 18705 18706 if (urgent) 18707 usable = 1; 18708 18709 data_null: 18710 snxt = tcp->tcp_snxt; 18711 xmit_tail = tcp->tcp_xmit_tail; 18712 tail_unsent = tcp->tcp_xmit_tail_unsent; 18713 18714 /* 18715 * Note that tcp_mss has been adjusted to take into account the 18716 * timestamp option if applicable. Because SACK options do not 18717 * appear in every TCP segments and they are of variable lengths, 18718 * they cannot be included in tcp_mss. Thus we need to calculate 18719 * the actual segment length when we need to send a segment which 18720 * includes SACK options. 18721 */ 18722 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18723 int32_t opt_len; 18724 18725 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18726 tcp->tcp_num_sack_blk); 18727 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18728 2 + TCPOPT_HEADER_LEN; 18729 mss = tcp->tcp_mss - opt_len; 18730 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18731 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18732 } else { 18733 mss = tcp->tcp_mss; 18734 tcp_hdr_len = tcp->tcp_hdr_len; 18735 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18736 } 18737 18738 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18739 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18740 SET_TCP_INIT_CWND(tcp, mss, tcps->tcps_slow_start_after_idle); 18741 } 18742 if (tcpstate == TCPS_SYN_RCVD) { 18743 /* 18744 * The three-way connection establishment handshake is not 18745 * complete yet. We want to queue the data for transmission 18746 * after entering ESTABLISHED state (RFC793). A jump to 18747 * "done" label effectively leaves data on the queue. 18748 */ 18749 goto done; 18750 } else { 18751 int usable_r; 18752 18753 /* 18754 * In the special case when cwnd is zero, which can only 18755 * happen if the connection is ECN capable, return now. 18756 * New segments is sent using tcp_timer(). The timer 18757 * is set in tcp_rput_data(). 18758 */ 18759 if (tcp->tcp_cwnd == 0) { 18760 /* 18761 * Note that tcp_cwnd is 0 before 3-way handshake is 18762 * finished. 18763 */ 18764 ASSERT(tcp->tcp_ecn_ok || 18765 tcp->tcp_state < TCPS_ESTABLISHED); 18766 return; 18767 } 18768 18769 /* NOTE: trouble if xmitting while SYN not acked? */ 18770 usable_r = snxt - tcp->tcp_suna; 18771 usable_r = tcp->tcp_swnd - usable_r; 18772 18773 /* 18774 * Check if the receiver has shrunk the window. If 18775 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18776 * cannot be set as there is unsent data, so FIN cannot 18777 * be sent out. Otherwise, we need to take into account 18778 * of FIN as it consumes an "invisible" sequence number. 18779 */ 18780 ASSERT(tcp->tcp_fin_sent == 0); 18781 if (usable_r < 0) { 18782 /* 18783 * The receiver has shrunk the window and we have sent 18784 * -usable_r date beyond the window, re-adjust. 18785 * 18786 * If TCP window scaling is enabled, there can be 18787 * round down error as the advertised receive window 18788 * is actually right shifted n bits. This means that 18789 * the lower n bits info is wiped out. It will look 18790 * like the window is shrunk. Do a check here to 18791 * see if the shrunk amount is actually within the 18792 * error in window calculation. If it is, just 18793 * return. Note that this check is inside the 18794 * shrunk window check. This makes sure that even 18795 * though tcp_process_shrunk_swnd() is not called, 18796 * we will stop further processing. 18797 */ 18798 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18799 tcp_process_shrunk_swnd(tcp, -usable_r); 18800 } 18801 return; 18802 } 18803 18804 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18805 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18806 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18807 18808 /* usable = MIN(usable, unsent) */ 18809 if (usable_r > len) 18810 usable_r = len; 18811 18812 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18813 if (usable_r > 0) { 18814 usable = usable_r; 18815 } else { 18816 /* Bypass all other unnecessary processing. */ 18817 goto done; 18818 } 18819 } 18820 18821 local_time = (mblk_t *)lbolt; 18822 18823 /* 18824 * "Our" Nagle Algorithm. This is not the same as in the old 18825 * BSD. This is more in line with the true intent of Nagle. 18826 * 18827 * The conditions are: 18828 * 1. The amount of unsent data (or amount of data which can be 18829 * sent, whichever is smaller) is less than Nagle limit. 18830 * 2. The last sent size is also less than Nagle limit. 18831 * 3. There is unack'ed data. 18832 * 4. Urgent pointer is not set. Send urgent data ignoring the 18833 * Nagle algorithm. This reduces the probability that urgent 18834 * bytes get "merged" together. 18835 * 5. The app has not closed the connection. This eliminates the 18836 * wait time of the receiving side waiting for the last piece of 18837 * (small) data. 18838 * 18839 * If all are satisified, exit without sending anything. Note 18840 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18841 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18842 * 4095). 18843 */ 18844 if (usable < (int)tcp->tcp_naglim && 18845 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18846 snxt != tcp->tcp_suna && 18847 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18848 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18849 goto done; 18850 } 18851 18852 /* 18853 * If tcp_zero_win_probe is not set and the tcp->tcp_cork option 18854 * is set, then we have to force TCP not to send partial segment 18855 * (smaller than MSS bytes). We are calculating the usable now 18856 * based on full mss and will save the rest of remaining data for 18857 * later. When tcp_zero_win_probe is set, TCP needs to send out 18858 * something to do zero window probe. 18859 */ 18860 if (tcp->tcp_cork && !tcp->tcp_zero_win_probe) { 18861 if (usable < mss) 18862 goto done; 18863 usable = (usable / mss) * mss; 18864 } 18865 18866 /* Update the latest receive window size in TCP header. */ 18867 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18868 tcp->tcp_tcph->th_win); 18869 18870 /* 18871 * Determine if it's worthwhile to attempt LSO or MDT, based on: 18872 * 18873 * 1. Simple TCP/IP{v4,v6} (no options). 18874 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18875 * 3. If the TCP connection is in ESTABLISHED state. 18876 * 4. The TCP is not detached. 18877 * 18878 * If any of the above conditions have changed during the 18879 * connection, stop using LSO/MDT and restore the stream head 18880 * parameters accordingly. 18881 */ 18882 ipst = tcps->tcps_netstack->netstack_ip; 18883 18884 if ((tcp->tcp_lso || tcp->tcp_mdt) && 18885 ((tcp->tcp_ipversion == IPV4_VERSION && 18886 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18887 (tcp->tcp_ipversion == IPV6_VERSION && 18888 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18889 tcp->tcp_state != TCPS_ESTABLISHED || 18890 TCP_IS_DETACHED(tcp) || !CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp) || 18891 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18892 IPP_ENABLED(IPP_LOCAL_OUT, ipst))) { 18893 if (tcp->tcp_lso) { 18894 tcp->tcp_connp->conn_lso_ok = B_FALSE; 18895 tcp->tcp_lso = B_FALSE; 18896 } else { 18897 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18898 tcp->tcp_mdt = B_FALSE; 18899 } 18900 18901 /* Anything other than detached is considered pathological */ 18902 if (!TCP_IS_DETACHED(tcp)) { 18903 if (tcp->tcp_lso) 18904 TCP_STAT(tcps, tcp_lso_disabled); 18905 else 18906 TCP_STAT(tcps, tcp_mdt_conn_halted1); 18907 (void) tcp_maxpsz_set(tcp, B_TRUE); 18908 } 18909 } 18910 18911 /* Use MDT if sendable amount is greater than the threshold */ 18912 if (tcp->tcp_mdt && 18913 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18914 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18915 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18916 (tcp->tcp_valid_bits == 0 || 18917 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18918 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18919 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18920 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18921 local_time, mdt_thres); 18922 } else { 18923 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18924 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18925 local_time, INT_MAX); 18926 } 18927 18928 /* Pretend that all we were trying to send really got sent */ 18929 if (rc < 0 && tail_unsent < 0) { 18930 do { 18931 xmit_tail = xmit_tail->b_cont; 18932 xmit_tail->b_prev = local_time; 18933 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18934 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18935 tail_unsent += (int)(xmit_tail->b_wptr - 18936 xmit_tail->b_rptr); 18937 } while (tail_unsent < 0); 18938 } 18939 done:; 18940 tcp->tcp_xmit_tail = xmit_tail; 18941 tcp->tcp_xmit_tail_unsent = tail_unsent; 18942 len = tcp->tcp_snxt - snxt; 18943 if (len) { 18944 /* 18945 * If new data was sent, need to update the notsack 18946 * list, which is, afterall, data blocks that have 18947 * not been sack'ed by the receiver. New data is 18948 * not sack'ed. 18949 */ 18950 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18951 /* len is a negative value. */ 18952 tcp->tcp_pipe -= len; 18953 tcp_notsack_update(&(tcp->tcp_notsack_list), 18954 tcp->tcp_snxt, snxt, 18955 &(tcp->tcp_num_notsack_blk), 18956 &(tcp->tcp_cnt_notsack_list)); 18957 } 18958 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18959 tcp->tcp_rack = tcp->tcp_rnxt; 18960 tcp->tcp_rack_cnt = 0; 18961 if ((snxt + len) == tcp->tcp_suna) { 18962 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18963 } 18964 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18965 /* 18966 * Didn't send anything. Make sure the timer is running 18967 * so that we will probe a zero window. 18968 */ 18969 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18970 } 18971 /* Note that len is the amount we just sent but with a negative sign */ 18972 tcp->tcp_unsent += len; 18973 mutex_enter(&tcp->tcp_non_sq_lock); 18974 if (tcp->tcp_flow_stopped) { 18975 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18976 tcp_clrqfull(tcp); 18977 } 18978 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18979 tcp_setqfull(tcp); 18980 } 18981 mutex_exit(&tcp->tcp_non_sq_lock); 18982 } 18983 18984 /* 18985 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18986 * outgoing TCP header with the template header, as well as other 18987 * options such as time-stamp, ECN and/or SACK. 18988 */ 18989 static void 18990 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18991 { 18992 tcph_t *tcp_tmpl, *tcp_h; 18993 uint32_t *dst, *src; 18994 int hdrlen; 18995 18996 ASSERT(OK_32PTR(rptr)); 18997 18998 /* Template header */ 18999 tcp_tmpl = tcp->tcp_tcph; 19000 19001 /* Header of outgoing packet */ 19002 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 19003 19004 /* dst and src are opaque 32-bit fields, used for copying */ 19005 dst = (uint32_t *)rptr; 19006 src = (uint32_t *)tcp->tcp_iphc; 19007 hdrlen = tcp->tcp_hdr_len; 19008 19009 /* Fill time-stamp option if needed */ 19010 if (tcp->tcp_snd_ts_ok) { 19011 U32_TO_BE32((uint32_t)now, 19012 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 19013 U32_TO_BE32(tcp->tcp_ts_recent, 19014 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 19015 } else { 19016 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 19017 } 19018 19019 /* 19020 * Copy the template header; is this really more efficient than 19021 * calling bcopy()? For simple IPv4/TCP, it may be the case, 19022 * but perhaps not for other scenarios. 19023 */ 19024 dst[0] = src[0]; 19025 dst[1] = src[1]; 19026 dst[2] = src[2]; 19027 dst[3] = src[3]; 19028 dst[4] = src[4]; 19029 dst[5] = src[5]; 19030 dst[6] = src[6]; 19031 dst[7] = src[7]; 19032 dst[8] = src[8]; 19033 dst[9] = src[9]; 19034 if (hdrlen -= 40) { 19035 hdrlen >>= 2; 19036 dst += 10; 19037 src += 10; 19038 do { 19039 *dst++ = *src++; 19040 } while (--hdrlen); 19041 } 19042 19043 /* 19044 * Set the ECN info in the TCP header if it is not a zero 19045 * window probe. Zero window probe is only sent in 19046 * tcp_wput_data() and tcp_timer(). 19047 */ 19048 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 19049 SET_ECT(tcp, rptr); 19050 19051 if (tcp->tcp_ecn_echo_on) 19052 tcp_h->th_flags[0] |= TH_ECE; 19053 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 19054 tcp_h->th_flags[0] |= TH_CWR; 19055 tcp->tcp_ecn_cwr_sent = B_TRUE; 19056 } 19057 } 19058 19059 /* Fill in SACK options */ 19060 if (num_sack_blk > 0) { 19061 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 19062 sack_blk_t *tmp; 19063 int32_t i; 19064 19065 wptr[0] = TCPOPT_NOP; 19066 wptr[1] = TCPOPT_NOP; 19067 wptr[2] = TCPOPT_SACK; 19068 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 19069 sizeof (sack_blk_t); 19070 wptr += TCPOPT_REAL_SACK_LEN; 19071 19072 tmp = tcp->tcp_sack_list; 19073 for (i = 0; i < num_sack_blk; i++) { 19074 U32_TO_BE32(tmp[i].begin, wptr); 19075 wptr += sizeof (tcp_seq); 19076 U32_TO_BE32(tmp[i].end, wptr); 19077 wptr += sizeof (tcp_seq); 19078 } 19079 tcp_h->th_offset_and_rsrvd[0] += 19080 ((num_sack_blk * 2 + 1) << 4); 19081 } 19082 } 19083 19084 /* 19085 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 19086 * the destination address and SAP attribute, and if necessary, the 19087 * hardware checksum offload attribute to a Multidata message. 19088 */ 19089 static int 19090 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 19091 const uint32_t start, const uint32_t stuff, const uint32_t end, 19092 const uint32_t flags, tcp_stack_t *tcps) 19093 { 19094 /* Add global destination address & SAP attribute */ 19095 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 19096 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 19097 "destination address+SAP\n")); 19098 19099 if (dlmp != NULL) 19100 TCP_STAT(tcps, tcp_mdt_allocfail); 19101 return (-1); 19102 } 19103 19104 /* Add global hwcksum attribute */ 19105 if (hwcksum && 19106 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 19107 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 19108 "checksum attribute\n")); 19109 19110 TCP_STAT(tcps, tcp_mdt_allocfail); 19111 return (-1); 19112 } 19113 19114 return (0); 19115 } 19116 19117 /* 19118 * Smaller and private version of pdescinfo_t used specifically for TCP, 19119 * which allows for only two payload spans per packet. 19120 */ 19121 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 19122 19123 /* 19124 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 19125 * scheme, and returns one the following: 19126 * 19127 * -1 = failed allocation. 19128 * 0 = success; burst count reached, or usable send window is too small, 19129 * and that we'd rather wait until later before sending again. 19130 */ 19131 static int 19132 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19133 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19134 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19135 const int mdt_thres) 19136 { 19137 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 19138 multidata_t *mmd; 19139 uint_t obsegs, obbytes, hdr_frag_sz; 19140 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19141 int num_burst_seg, max_pld; 19142 pdesc_t *pkt; 19143 tcp_pdescinfo_t tcp_pkt_info; 19144 pdescinfo_t *pkt_info; 19145 int pbuf_idx, pbuf_idx_nxt; 19146 int seg_len, len, spill, af; 19147 boolean_t add_buffer, zcopy, clusterwide; 19148 boolean_t rconfirm = B_FALSE; 19149 boolean_t done = B_FALSE; 19150 uint32_t cksum; 19151 uint32_t hwcksum_flags; 19152 ire_t *ire = NULL; 19153 ill_t *ill; 19154 ipha_t *ipha; 19155 ip6_t *ip6h; 19156 ipaddr_t src, dst; 19157 ill_zerocopy_capab_t *zc_cap = NULL; 19158 uint16_t *up; 19159 int err; 19160 conn_t *connp; 19161 tcp_stack_t *tcps = tcp->tcp_tcps; 19162 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 19163 int usable_mmd, tail_unsent_mmd; 19164 uint_t snxt_mmd, obsegs_mmd, obbytes_mmd; 19165 mblk_t *xmit_tail_mmd; 19166 netstackid_t stack_id; 19167 19168 #ifdef _BIG_ENDIAN 19169 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19170 #else 19171 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19172 #endif 19173 19174 #define PREP_NEW_MULTIDATA() { \ 19175 mmd = NULL; \ 19176 md_mp = md_hbuf = NULL; \ 19177 cur_hdr_off = 0; \ 19178 max_pld = tcp->tcp_mdt_max_pld; \ 19179 pbuf_idx = pbuf_idx_nxt = -1; \ 19180 add_buffer = B_TRUE; \ 19181 zcopy = B_FALSE; \ 19182 } 19183 19184 #define PREP_NEW_PBUF() { \ 19185 md_pbuf = md_pbuf_nxt = NULL; \ 19186 pbuf_idx = pbuf_idx_nxt = -1; \ 19187 cur_pld_off = 0; \ 19188 first_snxt = *snxt; \ 19189 ASSERT(*tail_unsent > 0); \ 19190 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19191 } 19192 19193 ASSERT(mdt_thres >= mss); 19194 ASSERT(*usable > 0 && *usable > mdt_thres); 19195 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19196 ASSERT(!TCP_IS_DETACHED(tcp)); 19197 ASSERT(tcp->tcp_valid_bits == 0 || 19198 tcp->tcp_valid_bits == TCP_FSS_VALID); 19199 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19200 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19201 (tcp->tcp_ipversion == IPV6_VERSION && 19202 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19203 19204 connp = tcp->tcp_connp; 19205 ASSERT(connp != NULL); 19206 ASSERT(CONN_IS_LSO_MD_FASTPATH(connp)); 19207 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19208 19209 stack_id = connp->conn_netstack->netstack_stackid; 19210 19211 usable_mmd = tail_unsent_mmd = 0; 19212 snxt_mmd = obsegs_mmd = obbytes_mmd = 0; 19213 xmit_tail_mmd = NULL; 19214 /* 19215 * Note that tcp will only declare at most 2 payload spans per 19216 * packet, which is much lower than the maximum allowable number 19217 * of packet spans per Multidata. For this reason, we use the 19218 * privately declared and smaller descriptor info structure, in 19219 * order to save some stack space. 19220 */ 19221 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19222 19223 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19224 if (af == AF_INET) { 19225 dst = tcp->tcp_ipha->ipha_dst; 19226 src = tcp->tcp_ipha->ipha_src; 19227 ASSERT(!CLASSD(dst)); 19228 } 19229 ASSERT(af == AF_INET || 19230 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19231 19232 obsegs = obbytes = 0; 19233 num_burst_seg = tcp->tcp_snd_burst; 19234 md_mp_head = NULL; 19235 PREP_NEW_MULTIDATA(); 19236 19237 /* 19238 * Before we go on further, make sure there is an IRE that we can 19239 * use, and that the ILL supports MDT. Otherwise, there's no point 19240 * in proceeding any further, and we should just hand everything 19241 * off to the legacy path. 19242 */ 19243 if (!tcp_send_find_ire(tcp, (af == AF_INET) ? &dst : NULL, &ire)) 19244 goto legacy_send_no_md; 19245 19246 ASSERT(ire != NULL); 19247 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19248 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19249 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19250 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19251 /* 19252 * If we do support loopback for MDT (which requires modifications 19253 * to the receiving paths), the following assertions should go away, 19254 * and we would be sending the Multidata to loopback conn later on. 19255 */ 19256 ASSERT(!IRE_IS_LOCAL(ire)); 19257 ASSERT(ire->ire_stq != NULL); 19258 19259 ill = ire_to_ill(ire); 19260 ASSERT(ill != NULL); 19261 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19262 19263 if (!tcp->tcp_ire_ill_check_done) { 19264 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19265 tcp->tcp_ire_ill_check_done = B_TRUE; 19266 } 19267 19268 /* 19269 * If the underlying interface conditions have changed, or if the 19270 * new interface does not support MDT, go back to legacy path. 19271 */ 19272 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19273 /* don't go through this path anymore for this connection */ 19274 TCP_STAT(tcps, tcp_mdt_conn_halted2); 19275 tcp->tcp_mdt = B_FALSE; 19276 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19277 "interface %s\n", (void *)connp, ill->ill_name)); 19278 /* IRE will be released prior to returning */ 19279 goto legacy_send_no_md; 19280 } 19281 19282 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19283 zc_cap = ill->ill_zerocopy_capab; 19284 19285 /* 19286 * Check if we can take tcp fast-path. Note that "incomplete" 19287 * ire's (where the link-layer for next hop is not resolved 19288 * or where the fast-path header in nce_fp_mp is not available 19289 * yet) are sent down the legacy (slow) path. 19290 * NOTE: We should fix ip_xmit_v4 to handle M_MULTIDATA 19291 */ 19292 if (ire->ire_nce && ire->ire_nce->nce_state != ND_REACHABLE) { 19293 /* IRE will be released prior to returning */ 19294 goto legacy_send_no_md; 19295 } 19296 19297 /* go to legacy path if interface doesn't support zerocopy */ 19298 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19299 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19300 /* IRE will be released prior to returning */ 19301 goto legacy_send_no_md; 19302 } 19303 19304 /* does the interface support hardware checksum offload? */ 19305 hwcksum_flags = 0; 19306 if (ILL_HCKSUM_CAPABLE(ill) && 19307 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19308 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19309 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19310 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19311 HCKSUM_IPHDRCKSUM) 19312 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19313 19314 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19315 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19316 hwcksum_flags |= HCK_FULLCKSUM; 19317 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19318 HCKSUM_INET_PARTIAL) 19319 hwcksum_flags |= HCK_PARTIALCKSUM; 19320 } 19321 19322 /* 19323 * Each header fragment consists of the leading extra space, 19324 * followed by the TCP/IP header, and the trailing extra space. 19325 * We make sure that each header fragment begins on a 32-bit 19326 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19327 * aligned in tcp_mdt_update). 19328 */ 19329 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19330 tcp->tcp_mdt_hdr_tail), 4); 19331 19332 /* are we starting from the beginning of data block? */ 19333 if (*tail_unsent == 0) { 19334 *xmit_tail = (*xmit_tail)->b_cont; 19335 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19336 *tail_unsent = (int)MBLKL(*xmit_tail); 19337 } 19338 19339 /* 19340 * Here we create one or more Multidata messages, each made up of 19341 * one header buffer and up to N payload buffers. This entire 19342 * operation is done within two loops: 19343 * 19344 * The outer loop mostly deals with creating the Multidata message, 19345 * as well as the header buffer that gets added to it. It also 19346 * links the Multidata messages together such that all of them can 19347 * be sent down to the lower layer in a single putnext call; this 19348 * linking behavior depends on the tcp_mdt_chain tunable. 19349 * 19350 * The inner loop takes an existing Multidata message, and adds 19351 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19352 * packetizes those buffers by filling up the corresponding header 19353 * buffer fragments with the proper IP and TCP headers, and by 19354 * describing the layout of each packet in the packet descriptors 19355 * that get added to the Multidata. 19356 */ 19357 do { 19358 /* 19359 * If usable send window is too small, or data blocks in 19360 * transmit list are smaller than our threshold (i.e. app 19361 * performs large writes followed by small ones), we hand 19362 * off the control over to the legacy path. Note that we'll 19363 * get back the control once it encounters a large block. 19364 */ 19365 if (*usable < mss || (*tail_unsent <= mdt_thres && 19366 (*xmit_tail)->b_cont != NULL && 19367 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19368 /* send down what we've got so far */ 19369 if (md_mp_head != NULL) { 19370 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19371 obsegs, obbytes, &rconfirm); 19372 } 19373 /* 19374 * Pass control over to tcp_send(), but tell it to 19375 * return to us once a large-size transmission is 19376 * possible. 19377 */ 19378 TCP_STAT(tcps, tcp_mdt_legacy_small); 19379 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19380 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19381 tail_unsent, xmit_tail, local_time, 19382 mdt_thres)) <= 0) { 19383 /* burst count reached, or alloc failed */ 19384 IRE_REFRELE(ire); 19385 return (err); 19386 } 19387 19388 /* tcp_send() may have sent everything, so check */ 19389 if (*usable <= 0) { 19390 IRE_REFRELE(ire); 19391 return (0); 19392 } 19393 19394 TCP_STAT(tcps, tcp_mdt_legacy_ret); 19395 /* 19396 * We may have delivered the Multidata, so make sure 19397 * to re-initialize before the next round. 19398 */ 19399 md_mp_head = NULL; 19400 obsegs = obbytes = 0; 19401 num_burst_seg = tcp->tcp_snd_burst; 19402 PREP_NEW_MULTIDATA(); 19403 19404 /* are we starting from the beginning of data block? */ 19405 if (*tail_unsent == 0) { 19406 *xmit_tail = (*xmit_tail)->b_cont; 19407 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19408 (uintptr_t)INT_MAX); 19409 *tail_unsent = (int)MBLKL(*xmit_tail); 19410 } 19411 } 19412 /* 19413 * Record current values for parameters we may need to pass 19414 * to tcp_send() or tcp_multisend_data(). We checkpoint at 19415 * each iteration of the outer loop (each multidata message 19416 * creation). If we have a failure in the inner loop, we send 19417 * any complete multidata messages we have before reverting 19418 * to using the traditional non-md path. 19419 */ 19420 snxt_mmd = *snxt; 19421 usable_mmd = *usable; 19422 xmit_tail_mmd = *xmit_tail; 19423 tail_unsent_mmd = *tail_unsent; 19424 obsegs_mmd = obsegs; 19425 obbytes_mmd = obbytes; 19426 19427 /* 19428 * max_pld limits the number of mblks in tcp's transmit 19429 * queue that can be added to a Multidata message. Once 19430 * this counter reaches zero, no more additional mblks 19431 * can be added to it. What happens afterwards depends 19432 * on whether or not we are set to chain the Multidata 19433 * messages. If we are to link them together, reset 19434 * max_pld to its original value (tcp_mdt_max_pld) and 19435 * prepare to create a new Multidata message which will 19436 * get linked to md_mp_head. Else, leave it alone and 19437 * let the inner loop break on its own. 19438 */ 19439 if (tcp_mdt_chain && max_pld == 0) 19440 PREP_NEW_MULTIDATA(); 19441 19442 /* adding a payload buffer; re-initialize values */ 19443 if (add_buffer) 19444 PREP_NEW_PBUF(); 19445 19446 /* 19447 * If we don't have a Multidata, either because we just 19448 * (re)entered this outer loop, or after we branched off 19449 * to tcp_send above, setup the Multidata and header 19450 * buffer to be used. 19451 */ 19452 if (md_mp == NULL) { 19453 int md_hbuflen; 19454 uint32_t start, stuff; 19455 19456 /* 19457 * Calculate Multidata header buffer size large enough 19458 * to hold all of the headers that can possibly be 19459 * sent at this moment. We'd rather over-estimate 19460 * the size than running out of space; this is okay 19461 * since this buffer is small anyway. 19462 */ 19463 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19464 19465 /* 19466 * Start and stuff offset for partial hardware 19467 * checksum offload; these are currently for IPv4. 19468 * For full checksum offload, they are set to zero. 19469 */ 19470 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19471 if (af == AF_INET) { 19472 start = IP_SIMPLE_HDR_LENGTH; 19473 stuff = IP_SIMPLE_HDR_LENGTH + 19474 TCP_CHECKSUM_OFFSET; 19475 } else { 19476 start = IPV6_HDR_LEN; 19477 stuff = IPV6_HDR_LEN + 19478 TCP_CHECKSUM_OFFSET; 19479 } 19480 } else { 19481 start = stuff = 0; 19482 } 19483 19484 /* 19485 * Create the header buffer, Multidata, as well as 19486 * any necessary attributes (destination address, 19487 * SAP and hardware checksum offload) that should 19488 * be associated with the Multidata message. 19489 */ 19490 ASSERT(cur_hdr_off == 0); 19491 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19492 ((md_hbuf->b_wptr += md_hbuflen), 19493 (mmd = mmd_alloc(md_hbuf, &md_mp, 19494 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19495 /* fastpath mblk */ 19496 ire->ire_nce->nce_res_mp, 19497 /* hardware checksum enabled */ 19498 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19499 /* hardware checksum offsets */ 19500 start, stuff, 0, 19501 /* hardware checksum flag */ 19502 hwcksum_flags, tcps) != 0)) { 19503 legacy_send: 19504 /* 19505 * We arrive here from a failure within the 19506 * inner (packetizer) loop or we fail one of 19507 * the conditionals above. We restore the 19508 * previously checkpointed values for: 19509 * xmit_tail 19510 * usable 19511 * tail_unsent 19512 * snxt 19513 * obbytes 19514 * obsegs 19515 * We should then be able to dispatch any 19516 * complete multidata before reverting to the 19517 * traditional path with consistent parameters 19518 * (the inner loop updates these as it 19519 * iterates). 19520 */ 19521 *xmit_tail = xmit_tail_mmd; 19522 *usable = usable_mmd; 19523 *tail_unsent = tail_unsent_mmd; 19524 *snxt = snxt_mmd; 19525 obbytes = obbytes_mmd; 19526 obsegs = obsegs_mmd; 19527 if (md_mp != NULL) { 19528 /* Unlink message from the chain */ 19529 if (md_mp_head != NULL) { 19530 err = (intptr_t)rmvb(md_mp_head, 19531 md_mp); 19532 /* 19533 * We can't assert that rmvb 19534 * did not return -1, since we 19535 * may get here before linkb 19536 * happens. We do, however, 19537 * check if we just removed the 19538 * only element in the list. 19539 */ 19540 if (err == 0) 19541 md_mp_head = NULL; 19542 } 19543 /* md_hbuf gets freed automatically */ 19544 TCP_STAT(tcps, tcp_mdt_discarded); 19545 freeb(md_mp); 19546 } else { 19547 /* Either allocb or mmd_alloc failed */ 19548 TCP_STAT(tcps, tcp_mdt_allocfail); 19549 if (md_hbuf != NULL) 19550 freeb(md_hbuf); 19551 } 19552 19553 /* send down what we've got so far */ 19554 if (md_mp_head != NULL) { 19555 tcp_multisend_data(tcp, ire, ill, 19556 md_mp_head, obsegs, obbytes, 19557 &rconfirm); 19558 } 19559 legacy_send_no_md: 19560 if (ire != NULL) 19561 IRE_REFRELE(ire); 19562 /* 19563 * Too bad; let the legacy path handle this. 19564 * We specify INT_MAX for the threshold, since 19565 * we gave up with the Multidata processings 19566 * and let the old path have it all. 19567 */ 19568 TCP_STAT(tcps, tcp_mdt_legacy_all); 19569 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19570 tcp_tcp_hdr_len, num_sack_blk, usable, 19571 snxt, tail_unsent, xmit_tail, local_time, 19572 INT_MAX)); 19573 } 19574 19575 /* link to any existing ones, if applicable */ 19576 TCP_STAT(tcps, tcp_mdt_allocd); 19577 if (md_mp_head == NULL) { 19578 md_mp_head = md_mp; 19579 } else if (tcp_mdt_chain) { 19580 TCP_STAT(tcps, tcp_mdt_linked); 19581 linkb(md_mp_head, md_mp); 19582 } 19583 } 19584 19585 ASSERT(md_mp_head != NULL); 19586 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19587 ASSERT(md_mp != NULL && mmd != NULL); 19588 ASSERT(md_hbuf != NULL); 19589 19590 /* 19591 * Packetize the transmittable portion of the data block; 19592 * each data block is essentially added to the Multidata 19593 * as a payload buffer. We also deal with adding more 19594 * than one payload buffers, which happens when the remaining 19595 * packetized portion of the current payload buffer is less 19596 * than MSS, while the next data block in transmit queue 19597 * has enough data to make up for one. This "spillover" 19598 * case essentially creates a split-packet, where portions 19599 * of the packet's payload fragments may span across two 19600 * virtually discontiguous address blocks. 19601 */ 19602 seg_len = mss; 19603 do { 19604 len = seg_len; 19605 19606 /* one must remain NULL for DTRACE_IP_FASTPATH */ 19607 ipha = NULL; 19608 ip6h = NULL; 19609 19610 ASSERT(len > 0); 19611 ASSERT(max_pld >= 0); 19612 ASSERT(!add_buffer || cur_pld_off == 0); 19613 19614 /* 19615 * First time around for this payload buffer; note 19616 * in the case of a spillover, the following has 19617 * been done prior to adding the split-packet 19618 * descriptor to Multidata, and we don't want to 19619 * repeat the process. 19620 */ 19621 if (add_buffer) { 19622 ASSERT(mmd != NULL); 19623 ASSERT(md_pbuf == NULL); 19624 ASSERT(md_pbuf_nxt == NULL); 19625 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19626 19627 /* 19628 * Have we reached the limit? We'd get to 19629 * this case when we're not chaining the 19630 * Multidata messages together, and since 19631 * we're done, terminate this loop. 19632 */ 19633 if (max_pld == 0) 19634 break; /* done */ 19635 19636 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19637 TCP_STAT(tcps, tcp_mdt_allocfail); 19638 goto legacy_send; /* out_of_mem */ 19639 } 19640 19641 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19642 zc_cap != NULL) { 19643 if (!ip_md_zcopy_attr(mmd, NULL, 19644 zc_cap->ill_zerocopy_flags)) { 19645 freeb(md_pbuf); 19646 TCP_STAT(tcps, 19647 tcp_mdt_allocfail); 19648 /* out_of_mem */ 19649 goto legacy_send; 19650 } 19651 zcopy = B_TRUE; 19652 } 19653 19654 md_pbuf->b_rptr += base_pld_off; 19655 19656 /* 19657 * Add a payload buffer to the Multidata; this 19658 * operation must not fail, or otherwise our 19659 * logic in this routine is broken. There 19660 * is no memory allocation done by the 19661 * routine, so any returned failure simply 19662 * tells us that we've done something wrong. 19663 * 19664 * A failure tells us that either we're adding 19665 * the same payload buffer more than once, or 19666 * we're trying to add more buffers than 19667 * allowed (max_pld calculation is wrong). 19668 * None of the above cases should happen, and 19669 * we panic because either there's horrible 19670 * heap corruption, and/or programming mistake. 19671 */ 19672 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19673 if (pbuf_idx < 0) { 19674 cmn_err(CE_PANIC, "tcp_multisend: " 19675 "payload buffer logic error " 19676 "detected for tcp %p mmd %p " 19677 "pbuf %p (%d)\n", 19678 (void *)tcp, (void *)mmd, 19679 (void *)md_pbuf, pbuf_idx); 19680 } 19681 19682 ASSERT(max_pld > 0); 19683 --max_pld; 19684 add_buffer = B_FALSE; 19685 } 19686 19687 ASSERT(md_mp_head != NULL); 19688 ASSERT(md_pbuf != NULL); 19689 ASSERT(md_pbuf_nxt == NULL); 19690 ASSERT(pbuf_idx != -1); 19691 ASSERT(pbuf_idx_nxt == -1); 19692 ASSERT(*usable > 0); 19693 19694 /* 19695 * We spillover to the next payload buffer only 19696 * if all of the following is true: 19697 * 19698 * 1. There is not enough data on the current 19699 * payload buffer to make up `len', 19700 * 2. We are allowed to send `len', 19701 * 3. The next payload buffer length is large 19702 * enough to accomodate `spill'. 19703 */ 19704 if ((spill = len - *tail_unsent) > 0 && 19705 *usable >= len && 19706 MBLKL((*xmit_tail)->b_cont) >= spill && 19707 max_pld > 0) { 19708 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19709 if (md_pbuf_nxt == NULL) { 19710 TCP_STAT(tcps, tcp_mdt_allocfail); 19711 goto legacy_send; /* out_of_mem */ 19712 } 19713 19714 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19715 zc_cap != NULL) { 19716 if (!ip_md_zcopy_attr(mmd, NULL, 19717 zc_cap->ill_zerocopy_flags)) { 19718 freeb(md_pbuf_nxt); 19719 TCP_STAT(tcps, 19720 tcp_mdt_allocfail); 19721 /* out_of_mem */ 19722 goto legacy_send; 19723 } 19724 zcopy = B_TRUE; 19725 } 19726 19727 /* 19728 * See comments above on the first call to 19729 * mmd_addpldbuf for explanation on the panic. 19730 */ 19731 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19732 if (pbuf_idx_nxt < 0) { 19733 panic("tcp_multisend: " 19734 "next payload buffer logic error " 19735 "detected for tcp %p mmd %p " 19736 "pbuf %p (%d)\n", 19737 (void *)tcp, (void *)mmd, 19738 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19739 } 19740 19741 ASSERT(max_pld > 0); 19742 --max_pld; 19743 } else if (spill > 0) { 19744 /* 19745 * If there's a spillover, but the following 19746 * xmit_tail couldn't give us enough octets 19747 * to reach "len", then stop the current 19748 * Multidata creation and let the legacy 19749 * tcp_send() path take over. We don't want 19750 * to send the tiny segment as part of this 19751 * Multidata for performance reasons; instead, 19752 * we let the legacy path deal with grouping 19753 * it with the subsequent small mblks. 19754 */ 19755 if (*usable >= len && 19756 MBLKL((*xmit_tail)->b_cont) < spill) { 19757 max_pld = 0; 19758 break; /* done */ 19759 } 19760 19761 /* 19762 * We can't spillover, and we are near 19763 * the end of the current payload buffer, 19764 * so send what's left. 19765 */ 19766 ASSERT(*tail_unsent > 0); 19767 len = *tail_unsent; 19768 } 19769 19770 /* tail_unsent is negated if there is a spillover */ 19771 *tail_unsent -= len; 19772 *usable -= len; 19773 ASSERT(*usable >= 0); 19774 19775 if (*usable < mss) 19776 seg_len = *usable; 19777 /* 19778 * Sender SWS avoidance; see comments in tcp_send(); 19779 * everything else is the same, except that we only 19780 * do this here if there is no more data to be sent 19781 * following the current xmit_tail. We don't check 19782 * for 1-byte urgent data because we shouldn't get 19783 * here if TCP_URG_VALID is set. 19784 */ 19785 if (*usable > 0 && *usable < mss && 19786 ((md_pbuf_nxt == NULL && 19787 (*xmit_tail)->b_cont == NULL) || 19788 (md_pbuf_nxt != NULL && 19789 (*xmit_tail)->b_cont->b_cont == NULL)) && 19790 seg_len < (tcp->tcp_max_swnd >> 1) && 19791 (tcp->tcp_unsent - 19792 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19793 !tcp->tcp_zero_win_probe) { 19794 if ((*snxt + len) == tcp->tcp_snxt && 19795 (*snxt + len) == tcp->tcp_suna) { 19796 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19797 } 19798 done = B_TRUE; 19799 } 19800 19801 /* 19802 * Prime pump for IP's checksumming on our behalf; 19803 * include the adjustment for a source route if any. 19804 * Do this only for software/partial hardware checksum 19805 * offload, as this field gets zeroed out later for 19806 * the full hardware checksum offload case. 19807 */ 19808 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19809 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19810 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19811 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19812 } 19813 19814 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19815 *snxt += len; 19816 19817 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19818 /* 19819 * We set the PUSH bit only if TCP has no more buffered 19820 * data to be transmitted (or if sender SWS avoidance 19821 * takes place), as opposed to setting it for every 19822 * last packet in the burst. 19823 */ 19824 if (done || 19825 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19826 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19827 19828 /* 19829 * Set FIN bit if this is our last segment; snxt 19830 * already includes its length, and it will not 19831 * be adjusted after this point. 19832 */ 19833 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19834 *snxt == tcp->tcp_fss) { 19835 if (!tcp->tcp_fin_acked) { 19836 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19837 BUMP_MIB(&tcps->tcps_mib, 19838 tcpOutControl); 19839 } 19840 if (!tcp->tcp_fin_sent) { 19841 tcp->tcp_fin_sent = B_TRUE; 19842 /* 19843 * tcp state must be ESTABLISHED 19844 * in order for us to get here in 19845 * the first place. 19846 */ 19847 tcp->tcp_state = TCPS_FIN_WAIT_1; 19848 19849 /* 19850 * Upon returning from this routine, 19851 * tcp_wput_data() will set tcp_snxt 19852 * to be equal to snxt + tcp_fin_sent. 19853 * This is essentially the same as 19854 * setting it to tcp_fss + 1. 19855 */ 19856 } 19857 } 19858 19859 tcp->tcp_last_sent_len = (ushort_t)len; 19860 19861 len += tcp_hdr_len; 19862 if (tcp->tcp_ipversion == IPV4_VERSION) 19863 tcp->tcp_ipha->ipha_length = htons(len); 19864 else 19865 tcp->tcp_ip6h->ip6_plen = htons(len - 19866 ((char *)&tcp->tcp_ip6h[1] - 19867 tcp->tcp_iphc)); 19868 19869 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19870 19871 /* setup header fragment */ 19872 PDESC_HDR_ADD(pkt_info, 19873 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19874 tcp->tcp_mdt_hdr_head, /* head room */ 19875 tcp_hdr_len, /* len */ 19876 tcp->tcp_mdt_hdr_tail); /* tail room */ 19877 19878 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19879 hdr_frag_sz); 19880 ASSERT(MBLKIN(md_hbuf, 19881 (pkt_info->hdr_base - md_hbuf->b_rptr), 19882 PDESC_HDRSIZE(pkt_info))); 19883 19884 /* setup first payload fragment */ 19885 PDESC_PLD_INIT(pkt_info); 19886 PDESC_PLD_SPAN_ADD(pkt_info, 19887 pbuf_idx, /* index */ 19888 md_pbuf->b_rptr + cur_pld_off, /* start */ 19889 tcp->tcp_last_sent_len); /* len */ 19890 19891 /* create a split-packet in case of a spillover */ 19892 if (md_pbuf_nxt != NULL) { 19893 ASSERT(spill > 0); 19894 ASSERT(pbuf_idx_nxt > pbuf_idx); 19895 ASSERT(!add_buffer); 19896 19897 md_pbuf = md_pbuf_nxt; 19898 md_pbuf_nxt = NULL; 19899 pbuf_idx = pbuf_idx_nxt; 19900 pbuf_idx_nxt = -1; 19901 cur_pld_off = spill; 19902 19903 /* trim out first payload fragment */ 19904 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19905 19906 /* setup second payload fragment */ 19907 PDESC_PLD_SPAN_ADD(pkt_info, 19908 pbuf_idx, /* index */ 19909 md_pbuf->b_rptr, /* start */ 19910 spill); /* len */ 19911 19912 if ((*xmit_tail)->b_next == NULL) { 19913 /* 19914 * Store the lbolt used for RTT 19915 * estimation. We can only record one 19916 * timestamp per mblk so we do it when 19917 * we reach the end of the payload 19918 * buffer. Also we only take a new 19919 * timestamp sample when the previous 19920 * timed data from the same mblk has 19921 * been ack'ed. 19922 */ 19923 (*xmit_tail)->b_prev = local_time; 19924 (*xmit_tail)->b_next = 19925 (mblk_t *)(uintptr_t)first_snxt; 19926 } 19927 19928 first_snxt = *snxt - spill; 19929 19930 /* 19931 * Advance xmit_tail; usable could be 0 by 19932 * the time we got here, but we made sure 19933 * above that we would only spillover to 19934 * the next data block if usable includes 19935 * the spilled-over amount prior to the 19936 * subtraction. Therefore, we are sure 19937 * that xmit_tail->b_cont can't be NULL. 19938 */ 19939 ASSERT((*xmit_tail)->b_cont != NULL); 19940 *xmit_tail = (*xmit_tail)->b_cont; 19941 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19942 (uintptr_t)INT_MAX); 19943 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19944 } else { 19945 cur_pld_off += tcp->tcp_last_sent_len; 19946 } 19947 19948 /* 19949 * Fill in the header using the template header, and 19950 * add options such as time-stamp, ECN and/or SACK, 19951 * as needed. 19952 */ 19953 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19954 (clock_t)local_time, num_sack_blk); 19955 19956 /* take care of some IP header businesses */ 19957 if (af == AF_INET) { 19958 ipha = (ipha_t *)pkt_info->hdr_rptr; 19959 19960 ASSERT(OK_32PTR((uchar_t *)ipha)); 19961 ASSERT(PDESC_HDRL(pkt_info) >= 19962 IP_SIMPLE_HDR_LENGTH); 19963 ASSERT(ipha->ipha_version_and_hdr_length == 19964 IP_SIMPLE_HDR_VERSION); 19965 19966 /* 19967 * Assign ident value for current packet; see 19968 * related comments in ip_wput_ire() about the 19969 * contract private interface with clustering 19970 * group. 19971 */ 19972 clusterwide = B_FALSE; 19973 if (cl_inet_ipident != NULL) { 19974 ASSERT(cl_inet_isclusterwide != NULL); 19975 if ((*cl_inet_isclusterwide)(stack_id, 19976 IPPROTO_IP, AF_INET, 19977 (uint8_t *)(uintptr_t)src, NULL)) { 19978 ipha->ipha_ident = 19979 (*cl_inet_ipident)(stack_id, 19980 IPPROTO_IP, AF_INET, 19981 (uint8_t *)(uintptr_t)src, 19982 (uint8_t *)(uintptr_t)dst, 19983 NULL); 19984 clusterwide = B_TRUE; 19985 } 19986 } 19987 19988 if (!clusterwide) { 19989 ipha->ipha_ident = (uint16_t) 19990 atomic_add_32_nv( 19991 &ire->ire_ident, 1); 19992 } 19993 #ifndef _BIG_ENDIAN 19994 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19995 (ipha->ipha_ident >> 8); 19996 #endif 19997 } else { 19998 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19999 20000 ASSERT(OK_32PTR((uchar_t *)ip6h)); 20001 ASSERT(IPVER(ip6h) == IPV6_VERSION); 20002 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 20003 ASSERT(PDESC_HDRL(pkt_info) >= 20004 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 20005 TCP_CHECKSUM_SIZE)); 20006 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20007 20008 if (tcp->tcp_ip_forward_progress) { 20009 rconfirm = B_TRUE; 20010 tcp->tcp_ip_forward_progress = B_FALSE; 20011 } 20012 } 20013 20014 /* at least one payload span, and at most two */ 20015 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 20016 20017 /* add the packet descriptor to Multidata */ 20018 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 20019 KM_NOSLEEP)) == NULL) { 20020 /* 20021 * Any failure other than ENOMEM indicates 20022 * that we have passed in invalid pkt_info 20023 * or parameters to mmd_addpdesc, which must 20024 * not happen. 20025 * 20026 * EINVAL is a result of failure on boundary 20027 * checks against the pkt_info contents. It 20028 * should not happen, and we panic because 20029 * either there's horrible heap corruption, 20030 * and/or programming mistake. 20031 */ 20032 if (err != ENOMEM) { 20033 cmn_err(CE_PANIC, "tcp_multisend: " 20034 "pdesc logic error detected for " 20035 "tcp %p mmd %p pinfo %p (%d)\n", 20036 (void *)tcp, (void *)mmd, 20037 (void *)pkt_info, err); 20038 } 20039 TCP_STAT(tcps, tcp_mdt_addpdescfail); 20040 goto legacy_send; /* out_of_mem */ 20041 } 20042 ASSERT(pkt != NULL); 20043 20044 /* calculate IP header and TCP checksums */ 20045 if (af == AF_INET) { 20046 /* calculate pseudo-header checksum */ 20047 cksum = (dst >> 16) + (dst & 0xFFFF) + 20048 (src >> 16) + (src & 0xFFFF); 20049 20050 /* offset for TCP header checksum */ 20051 up = IPH_TCPH_CHECKSUMP(ipha, 20052 IP_SIMPLE_HDR_LENGTH); 20053 } else { 20054 up = (uint16_t *)&ip6h->ip6_src; 20055 20056 /* calculate pseudo-header checksum */ 20057 cksum = up[0] + up[1] + up[2] + up[3] + 20058 up[4] + up[5] + up[6] + up[7] + 20059 up[8] + up[9] + up[10] + up[11] + 20060 up[12] + up[13] + up[14] + up[15]; 20061 20062 /* Fold the initial sum */ 20063 cksum = (cksum & 0xffff) + (cksum >> 16); 20064 20065 up = (uint16_t *)(((uchar_t *)ip6h) + 20066 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 20067 } 20068 20069 if (hwcksum_flags & HCK_FULLCKSUM) { 20070 /* clear checksum field for hardware */ 20071 *up = 0; 20072 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 20073 uint32_t sum; 20074 20075 /* pseudo-header checksumming */ 20076 sum = *up + cksum + IP_TCP_CSUM_COMP; 20077 sum = (sum & 0xFFFF) + (sum >> 16); 20078 *up = (sum & 0xFFFF) + (sum >> 16); 20079 } else { 20080 /* software checksumming */ 20081 TCP_STAT(tcps, tcp_out_sw_cksum); 20082 TCP_STAT_UPDATE(tcps, tcp_out_sw_cksum_bytes, 20083 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 20084 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 20085 cksum + IP_TCP_CSUM_COMP); 20086 if (*up == 0) 20087 *up = 0xFFFF; 20088 } 20089 20090 /* IPv4 header checksum */ 20091 if (af == AF_INET) { 20092 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 20093 ipha->ipha_hdr_checksum = 0; 20094 } else { 20095 IP_HDR_CKSUM(ipha, cksum, 20096 ((uint32_t *)ipha)[0], 20097 ((uint16_t *)ipha)[4]); 20098 } 20099 } 20100 20101 if (af == AF_INET && 20102 HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) || 20103 af == AF_INET6 && 20104 HOOKS6_INTERESTED_PHYSICAL_OUT(ipst)) { 20105 mblk_t *mp, *mp1; 20106 uchar_t *hdr_rptr, *hdr_wptr; 20107 uchar_t *pld_rptr, *pld_wptr; 20108 20109 /* 20110 * We reconstruct a pseudo packet for the hooks 20111 * framework using mmd_transform_link(). 20112 * If it is a split packet we pullup the 20113 * payload. FW_HOOKS expects a pkt comprising 20114 * of two mblks: a header and the payload. 20115 */ 20116 if ((mp = mmd_transform_link(pkt)) == NULL) { 20117 TCP_STAT(tcps, tcp_mdt_allocfail); 20118 goto legacy_send; 20119 } 20120 20121 if (pkt_info->pld_cnt > 1) { 20122 /* split payload, more than one pld */ 20123 if ((mp1 = msgpullup(mp->b_cont, -1)) == 20124 NULL) { 20125 freemsg(mp); 20126 TCP_STAT(tcps, 20127 tcp_mdt_allocfail); 20128 goto legacy_send; 20129 } 20130 freemsg(mp->b_cont); 20131 mp->b_cont = mp1; 20132 } else { 20133 mp1 = mp->b_cont; 20134 } 20135 ASSERT(mp1 != NULL && mp1->b_cont == NULL); 20136 20137 /* 20138 * Remember the message offsets. This is so we 20139 * can detect changes when we return from the 20140 * FW_HOOKS callbacks. 20141 */ 20142 hdr_rptr = mp->b_rptr; 20143 hdr_wptr = mp->b_wptr; 20144 pld_rptr = mp->b_cont->b_rptr; 20145 pld_wptr = mp->b_cont->b_wptr; 20146 20147 if (af == AF_INET) { 20148 DTRACE_PROBE4( 20149 ip4__physical__out__start, 20150 ill_t *, NULL, 20151 ill_t *, ill, 20152 ipha_t *, ipha, 20153 mblk_t *, mp); 20154 FW_HOOKS( 20155 ipst->ips_ip4_physical_out_event, 20156 ipst->ips_ipv4firewall_physical_out, 20157 NULL, ill, ipha, mp, mp, 0, ipst); 20158 DTRACE_PROBE1( 20159 ip4__physical__out__end, 20160 mblk_t *, mp); 20161 } else { 20162 DTRACE_PROBE4( 20163 ip6__physical__out_start, 20164 ill_t *, NULL, 20165 ill_t *, ill, 20166 ip6_t *, ip6h, 20167 mblk_t *, mp); 20168 FW_HOOKS6( 20169 ipst->ips_ip6_physical_out_event, 20170 ipst->ips_ipv6firewall_physical_out, 20171 NULL, ill, ip6h, mp, mp, 0, ipst); 20172 DTRACE_PROBE1( 20173 ip6__physical__out__end, 20174 mblk_t *, mp); 20175 } 20176 20177 if (mp == NULL || 20178 (mp1 = mp->b_cont) == NULL || 20179 mp->b_rptr != hdr_rptr || 20180 mp->b_wptr != hdr_wptr || 20181 mp1->b_rptr != pld_rptr || 20182 mp1->b_wptr != pld_wptr || 20183 mp1->b_cont != NULL) { 20184 /* 20185 * We abandon multidata processing and 20186 * return to the normal path, either 20187 * when a packet is blocked, or when 20188 * the boundaries of header buffer or 20189 * payload buffer have been changed by 20190 * FW_HOOKS[6]. 20191 */ 20192 if (mp != NULL) 20193 freemsg(mp); 20194 goto legacy_send; 20195 } 20196 /* Finished with the pseudo packet */ 20197 freemsg(mp); 20198 } 20199 DTRACE_IP_FASTPATH(md_hbuf, pkt_info->hdr_rptr, 20200 ill, ipha, ip6h); 20201 /* advance header offset */ 20202 cur_hdr_off += hdr_frag_sz; 20203 20204 obbytes += tcp->tcp_last_sent_len; 20205 ++obsegs; 20206 } while (!done && *usable > 0 && --num_burst_seg > 0 && 20207 *tail_unsent > 0); 20208 20209 if ((*xmit_tail)->b_next == NULL) { 20210 /* 20211 * Store the lbolt used for RTT estimation. We can only 20212 * record one timestamp per mblk so we do it when we 20213 * reach the end of the payload buffer. Also we only 20214 * take a new timestamp sample when the previous timed 20215 * data from the same mblk has been ack'ed. 20216 */ 20217 (*xmit_tail)->b_prev = local_time; 20218 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 20219 } 20220 20221 ASSERT(*tail_unsent >= 0); 20222 if (*tail_unsent > 0) { 20223 /* 20224 * We got here because we broke out of the above 20225 * loop due to of one of the following cases: 20226 * 20227 * 1. len < adjusted MSS (i.e. small), 20228 * 2. Sender SWS avoidance, 20229 * 3. max_pld is zero. 20230 * 20231 * We are done for this Multidata, so trim our 20232 * last payload buffer (if any) accordingly. 20233 */ 20234 if (md_pbuf != NULL) 20235 md_pbuf->b_wptr -= *tail_unsent; 20236 } else if (*usable > 0) { 20237 *xmit_tail = (*xmit_tail)->b_cont; 20238 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 20239 (uintptr_t)INT_MAX); 20240 *tail_unsent = (int)MBLKL(*xmit_tail); 20241 add_buffer = B_TRUE; 20242 } 20243 } while (!done && *usable > 0 && num_burst_seg > 0 && 20244 (tcp_mdt_chain || max_pld > 0)); 20245 20246 if (md_mp_head != NULL) { 20247 /* send everything down */ 20248 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20249 &rconfirm); 20250 } 20251 20252 #undef PREP_NEW_MULTIDATA 20253 #undef PREP_NEW_PBUF 20254 #undef IPVER 20255 20256 IRE_REFRELE(ire); 20257 return (0); 20258 } 20259 20260 /* 20261 * A wrapper function for sending one or more Multidata messages down to 20262 * the module below ip; this routine does not release the reference of the 20263 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20264 */ 20265 static void 20266 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20267 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20268 { 20269 uint64_t delta; 20270 nce_t *nce; 20271 tcp_stack_t *tcps = tcp->tcp_tcps; 20272 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20273 20274 ASSERT(ire != NULL && ill != NULL); 20275 ASSERT(ire->ire_stq != NULL); 20276 ASSERT(md_mp_head != NULL); 20277 ASSERT(rconfirm != NULL); 20278 20279 /* adjust MIBs and IRE timestamp */ 20280 DTRACE_PROBE2(tcp__trace__send, mblk_t *, md_mp_head, tcp_t *, tcp); 20281 tcp->tcp_obsegs += obsegs; 20282 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataSegs, obsegs); 20283 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, obbytes); 20284 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out, obsegs); 20285 20286 if (tcp->tcp_ipversion == IPV4_VERSION) { 20287 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v4, obsegs); 20288 } else { 20289 TCP_STAT_UPDATE(tcps, tcp_mdt_pkt_out_v6, obsegs); 20290 } 20291 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests, obsegs); 20292 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits, obsegs); 20293 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, obbytes); 20294 20295 ire->ire_ob_pkt_count += obsegs; 20296 if (ire->ire_ipif != NULL) 20297 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20298 ire->ire_last_used_time = lbolt; 20299 20300 if (ipst->ips_ipobs_enabled) { 20301 multidata_t *dlmdp = mmd_getmultidata(md_mp_head); 20302 pdesc_t *dl_pkt; 20303 pdescinfo_t pinfo; 20304 mblk_t *nmp; 20305 zoneid_t szone = tcp->tcp_connp->conn_zoneid; 20306 20307 for (dl_pkt = mmd_getfirstpdesc(dlmdp, &pinfo); 20308 (dl_pkt != NULL); 20309 dl_pkt = mmd_getnextpdesc(dl_pkt, &pinfo)) { 20310 if ((nmp = mmd_transform_link(dl_pkt)) == NULL) 20311 continue; 20312 ipobs_hook(nmp, IPOBS_HOOK_OUTBOUND, szone, 20313 ALL_ZONES, ill, tcp->tcp_ipversion, 0, ipst); 20314 freemsg(nmp); 20315 } 20316 } 20317 20318 /* send it down */ 20319 putnext(ire->ire_stq, md_mp_head); 20320 20321 /* we're done for TCP/IPv4 */ 20322 if (tcp->tcp_ipversion == IPV4_VERSION) 20323 return; 20324 20325 nce = ire->ire_nce; 20326 20327 ASSERT(nce != NULL); 20328 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20329 ASSERT(nce->nce_state != ND_INCOMPLETE); 20330 20331 /* reachability confirmation? */ 20332 if (*rconfirm) { 20333 nce->nce_last = TICK_TO_MSEC(lbolt64); 20334 if (nce->nce_state != ND_REACHABLE) { 20335 mutex_enter(&nce->nce_lock); 20336 nce->nce_state = ND_REACHABLE; 20337 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20338 mutex_exit(&nce->nce_lock); 20339 (void) untimeout(nce->nce_timeout_id); 20340 if (ip_debug > 2) { 20341 /* ip1dbg */ 20342 pr_addr_dbg("tcp_multisend_data: state " 20343 "for %s changed to REACHABLE\n", 20344 AF_INET6, &ire->ire_addr_v6); 20345 } 20346 } 20347 /* reset transport reachability confirmation */ 20348 *rconfirm = B_FALSE; 20349 } 20350 20351 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20352 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20353 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20354 20355 if (delta > (uint64_t)ill->ill_reachable_time) { 20356 mutex_enter(&nce->nce_lock); 20357 switch (nce->nce_state) { 20358 case ND_REACHABLE: 20359 case ND_STALE: 20360 /* 20361 * ND_REACHABLE is identical to ND_STALE in this 20362 * specific case. If reachable time has expired for 20363 * this neighbor (delta is greater than reachable 20364 * time), conceptually, the neighbor cache is no 20365 * longer in REACHABLE state, but already in STALE 20366 * state. So the correct transition here is to 20367 * ND_DELAY. 20368 */ 20369 nce->nce_state = ND_DELAY; 20370 mutex_exit(&nce->nce_lock); 20371 NDP_RESTART_TIMER(nce, 20372 ipst->ips_delay_first_probe_time); 20373 if (ip_debug > 3) { 20374 /* ip2dbg */ 20375 pr_addr_dbg("tcp_multisend_data: state " 20376 "for %s changed to DELAY\n", 20377 AF_INET6, &ire->ire_addr_v6); 20378 } 20379 break; 20380 case ND_DELAY: 20381 case ND_PROBE: 20382 mutex_exit(&nce->nce_lock); 20383 /* Timers have already started */ 20384 break; 20385 case ND_UNREACHABLE: 20386 /* 20387 * ndp timer has detected that this nce is 20388 * unreachable and initiated deleting this nce 20389 * and all its associated IREs. This is a race 20390 * where we found the ire before it was deleted 20391 * and have just sent out a packet using this 20392 * unreachable nce. 20393 */ 20394 mutex_exit(&nce->nce_lock); 20395 break; 20396 default: 20397 ASSERT(0); 20398 } 20399 } 20400 } 20401 20402 /* 20403 * Derived from tcp_send_data(). 20404 */ 20405 static void 20406 tcp_lsosend_data(tcp_t *tcp, mblk_t *mp, ire_t *ire, ill_t *ill, const int mss, 20407 int num_lso_seg) 20408 { 20409 ipha_t *ipha; 20410 mblk_t *ire_fp_mp; 20411 uint_t ire_fp_mp_len; 20412 uint32_t hcksum_txflags = 0; 20413 ipaddr_t src; 20414 ipaddr_t dst; 20415 uint32_t cksum; 20416 uint16_t *up; 20417 tcp_stack_t *tcps = tcp->tcp_tcps; 20418 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 20419 20420 ASSERT(DB_TYPE(mp) == M_DATA); 20421 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 20422 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 20423 ASSERT(tcp->tcp_connp != NULL); 20424 ASSERT(CONN_IS_LSO_MD_FASTPATH(tcp->tcp_connp)); 20425 20426 ipha = (ipha_t *)mp->b_rptr; 20427 src = ipha->ipha_src; 20428 dst = ipha->ipha_dst; 20429 20430 DTRACE_PROBE2(tcp__trace__send, mblk_t *, mp, tcp_t *, tcp); 20431 20432 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 20433 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 20434 num_lso_seg); 20435 #ifndef _BIG_ENDIAN 20436 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 20437 #endif 20438 if (tcp->tcp_snd_zcopy_aware) { 20439 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 20440 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 20441 mp = tcp_zcopy_disable(tcp, mp); 20442 } 20443 20444 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 20445 ASSERT(ill->ill_hcksum_capab != NULL); 20446 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 20447 } 20448 20449 /* 20450 * Since the TCP checksum should be recalculated by h/w, we can just 20451 * zero the checksum field for HCK_FULLCKSUM, or calculate partial 20452 * pseudo-header checksum for HCK_PARTIALCKSUM. 20453 * The partial pseudo-header excludes TCP length, that was calculated 20454 * in tcp_send(), so to zero *up before further processing. 20455 */ 20456 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 20457 20458 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 20459 *up = 0; 20460 20461 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 20462 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 20463 20464 /* 20465 * Append LSO flags and mss to the mp. 20466 */ 20467 lso_info_set(mp, mss, HW_LSO); 20468 20469 ipha->ipha_fragment_offset_and_flags |= 20470 (uint32_t)htons(ire->ire_frag_flag); 20471 20472 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20473 ire_fp_mp_len = MBLKL(ire_fp_mp); 20474 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 20475 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 20476 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 20477 20478 UPDATE_OB_PKT_COUNT(ire); 20479 ire->ire_last_used_time = lbolt; 20480 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutRequests); 20481 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits); 20482 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets, 20483 ntohs(ipha->ipha_length)); 20484 20485 DTRACE_PROBE4(ip4__physical__out__start, 20486 ill_t *, NULL, ill_t *, ill, ipha_t *, ipha, mblk_t *, mp); 20487 FW_HOOKS(ipst->ips_ip4_physical_out_event, 20488 ipst->ips_ipv4firewall_physical_out, NULL, 20489 ill, ipha, mp, mp, 0, ipst); 20490 DTRACE_PROBE1(ip4__physical__out__end, mblk_t *, mp); 20491 DTRACE_IP_FASTPATH(mp, ipha, ill, ipha, NULL); 20492 20493 if (mp != NULL) { 20494 if (ipst->ips_ipobs_enabled) { 20495 zoneid_t szone; 20496 20497 szone = ip_get_zoneid_v4(ipha->ipha_src, mp, 20498 ipst, ALL_ZONES); 20499 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, 20500 ALL_ZONES, ill, IPV4_VERSION, ire_fp_mp_len, ipst); 20501 } 20502 20503 ILL_SEND_TX(ill, ire, tcp->tcp_connp, mp, 0, NULL); 20504 } 20505 } 20506 20507 /* 20508 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20509 * scheme, and returns one of the following: 20510 * 20511 * -1 = failed allocation. 20512 * 0 = success; burst count reached, or usable send window is too small, 20513 * and that we'd rather wait until later before sending again. 20514 * 1 = success; we are called from tcp_multisend(), and both usable send 20515 * window and tail_unsent are greater than the MDT threshold, and thus 20516 * Multidata Transmit should be used instead. 20517 */ 20518 static int 20519 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20520 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20521 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20522 const int mdt_thres) 20523 { 20524 int num_burst_seg = tcp->tcp_snd_burst; 20525 ire_t *ire = NULL; 20526 ill_t *ill = NULL; 20527 mblk_t *ire_fp_mp = NULL; 20528 uint_t ire_fp_mp_len = 0; 20529 int num_lso_seg = 1; 20530 uint_t lso_usable; 20531 boolean_t do_lso_send = B_FALSE; 20532 tcp_stack_t *tcps = tcp->tcp_tcps; 20533 20534 /* 20535 * Check LSO capability before any further work. And the similar check 20536 * need to be done in for(;;) loop. 20537 * LSO will be deployed when therer is more than one mss of available 20538 * data and a burst transmission is allowed. 20539 */ 20540 if (tcp->tcp_lso && 20541 (tcp->tcp_valid_bits == 0 || 20542 tcp->tcp_valid_bits == TCP_FSS_VALID) && 20543 num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20544 /* 20545 * Try to find usable IRE/ILL and do basic check to the ILL. 20546 * Double check LSO usability before going further, since the 20547 * underlying interface could have been changed. In case of any 20548 * change of LSO capability, set tcp_ire_ill_check_done to 20549 * B_FALSE to force to check the ILL with the next send. 20550 */ 20551 if (tcp_send_find_ire_ill(tcp, NULL, &ire, &ill) && 20552 tcp->tcp_lso && ILL_LSO_TCP_USABLE(ill)) { 20553 /* 20554 * Enable LSO with this transmission. 20555 * Since IRE has been hold in tcp_send_find_ire_ill(), 20556 * IRE_REFRELE(ire) should be called before return. 20557 */ 20558 do_lso_send = B_TRUE; 20559 ire_fp_mp = ire->ire_nce->nce_fp_mp; 20560 ire_fp_mp_len = MBLKL(ire_fp_mp); 20561 /* Round up to multiple of 4 */ 20562 ire_fp_mp_len = ((ire_fp_mp_len + 3) / 4) * 4; 20563 } else { 20564 tcp->tcp_lso = B_FALSE; 20565 tcp->tcp_ire_ill_check_done = B_FALSE; 20566 do_lso_send = B_FALSE; 20567 ill = NULL; 20568 } 20569 } 20570 20571 for (;;) { 20572 struct datab *db; 20573 tcph_t *tcph; 20574 uint32_t sum; 20575 mblk_t *mp, *mp1; 20576 uchar_t *rptr; 20577 int len; 20578 20579 /* 20580 * If we're called by tcp_multisend(), and the amount of 20581 * sendable data as well as the size of current xmit_tail 20582 * is beyond the MDT threshold, return to the caller and 20583 * let the large data transmit be done using MDT. 20584 */ 20585 if (*usable > 0 && *usable > mdt_thres && 20586 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20587 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20588 ASSERT(tcp->tcp_mdt); 20589 return (1); /* success; do large send */ 20590 } 20591 20592 if (num_burst_seg == 0) 20593 break; /* success; burst count reached */ 20594 20595 /* 20596 * Calculate the maximum payload length we can send in *one* 20597 * time. 20598 */ 20599 if (do_lso_send) { 20600 /* 20601 * Check whether need to do LSO any more. 20602 */ 20603 if (num_burst_seg >= 2 && (*usable - 1) / mss >= 1) { 20604 lso_usable = MIN(tcp->tcp_lso_max, *usable); 20605 lso_usable = MIN(lso_usable, 20606 num_burst_seg * mss); 20607 20608 num_lso_seg = lso_usable / mss; 20609 if (lso_usable % mss) { 20610 num_lso_seg++; 20611 tcp->tcp_last_sent_len = (ushort_t) 20612 (lso_usable % mss); 20613 } else { 20614 tcp->tcp_last_sent_len = (ushort_t)mss; 20615 } 20616 } else { 20617 do_lso_send = B_FALSE; 20618 num_lso_seg = 1; 20619 lso_usable = mss; 20620 } 20621 } 20622 20623 ASSERT(num_lso_seg <= IP_MAXPACKET / mss + 1); 20624 20625 /* 20626 * Adjust num_burst_seg here. 20627 */ 20628 num_burst_seg -= num_lso_seg; 20629 20630 len = mss; 20631 if (len > *usable) { 20632 ASSERT(do_lso_send == B_FALSE); 20633 20634 len = *usable; 20635 if (len <= 0) { 20636 /* Terminate the loop */ 20637 break; /* success; too small */ 20638 } 20639 /* 20640 * Sender silly-window avoidance. 20641 * Ignore this if we are going to send a 20642 * zero window probe out. 20643 * 20644 * TODO: force data into microscopic window? 20645 * ==> (!pushed || (unsent > usable)) 20646 */ 20647 if (len < (tcp->tcp_max_swnd >> 1) && 20648 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20649 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20650 len == 1) && (! tcp->tcp_zero_win_probe)) { 20651 /* 20652 * If the retransmit timer is not running 20653 * we start it so that we will retransmit 20654 * in the case when the the receiver has 20655 * decremented the window. 20656 */ 20657 if (*snxt == tcp->tcp_snxt && 20658 *snxt == tcp->tcp_suna) { 20659 /* 20660 * We are not supposed to send 20661 * anything. So let's wait a little 20662 * bit longer before breaking SWS 20663 * avoidance. 20664 * 20665 * What should the value be? 20666 * Suggestion: MAX(init rexmit time, 20667 * tcp->tcp_rto) 20668 */ 20669 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20670 } 20671 break; /* success; too small */ 20672 } 20673 } 20674 20675 tcph = tcp->tcp_tcph; 20676 20677 /* 20678 * The reason to adjust len here is that we need to set flags 20679 * and calculate checksum. 20680 */ 20681 if (do_lso_send) 20682 len = lso_usable; 20683 20684 *usable -= len; /* Approximate - can be adjusted later */ 20685 if (*usable > 0) 20686 tcph->th_flags[0] = TH_ACK; 20687 else 20688 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20689 20690 /* 20691 * Prime pump for IP's checksumming on our behalf 20692 * Include the adjustment for a source route if any. 20693 */ 20694 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20695 sum = (sum >> 16) + (sum & 0xFFFF); 20696 U16_TO_ABE16(sum, tcph->th_sum); 20697 20698 U32_TO_ABE32(*snxt, tcph->th_seq); 20699 20700 /* 20701 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20702 * set. For the case when TCP_FSS_VALID is the only valid 20703 * bit (normal active close), branch off only when we think 20704 * that the FIN flag needs to be set. Note for this case, 20705 * that (snxt + len) may not reflect the actual seg_len, 20706 * as len may be further reduced in tcp_xmit_mp(). If len 20707 * gets modified, we will end up here again. 20708 */ 20709 if (tcp->tcp_valid_bits != 0 && 20710 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20711 ((*snxt + len) == tcp->tcp_fss))) { 20712 uchar_t *prev_rptr; 20713 uint32_t prev_snxt = tcp->tcp_snxt; 20714 20715 if (*tail_unsent == 0) { 20716 ASSERT((*xmit_tail)->b_cont != NULL); 20717 *xmit_tail = (*xmit_tail)->b_cont; 20718 prev_rptr = (*xmit_tail)->b_rptr; 20719 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20720 (*xmit_tail)->b_rptr); 20721 } else { 20722 prev_rptr = (*xmit_tail)->b_rptr; 20723 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20724 *tail_unsent; 20725 } 20726 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20727 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20728 /* Restore tcp_snxt so we get amount sent right. */ 20729 tcp->tcp_snxt = prev_snxt; 20730 if (prev_rptr == (*xmit_tail)->b_rptr) { 20731 /* 20732 * If the previous timestamp is still in use, 20733 * don't stomp on it. 20734 */ 20735 if ((*xmit_tail)->b_next == NULL) { 20736 (*xmit_tail)->b_prev = local_time; 20737 (*xmit_tail)->b_next = 20738 (mblk_t *)(uintptr_t)(*snxt); 20739 } 20740 } else 20741 (*xmit_tail)->b_rptr = prev_rptr; 20742 20743 if (mp == NULL) { 20744 if (ire != NULL) 20745 IRE_REFRELE(ire); 20746 return (-1); 20747 } 20748 mp1 = mp->b_cont; 20749 20750 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20751 tcp->tcp_last_sent_len = (ushort_t)len; 20752 while (mp1->b_cont) { 20753 *xmit_tail = (*xmit_tail)->b_cont; 20754 (*xmit_tail)->b_prev = local_time; 20755 (*xmit_tail)->b_next = 20756 (mblk_t *)(uintptr_t)(*snxt); 20757 mp1 = mp1->b_cont; 20758 } 20759 *snxt += len; 20760 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20761 BUMP_LOCAL(tcp->tcp_obsegs); 20762 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20763 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20764 tcp_send_data(tcp, q, mp); 20765 continue; 20766 } 20767 20768 *snxt += len; /* Adjust later if we don't send all of len */ 20769 BUMP_MIB(&tcps->tcps_mib, tcpOutDataSegs); 20770 UPDATE_MIB(&tcps->tcps_mib, tcpOutDataBytes, len); 20771 20772 if (*tail_unsent) { 20773 /* Are the bytes above us in flight? */ 20774 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20775 if (rptr != (*xmit_tail)->b_rptr) { 20776 *tail_unsent -= len; 20777 if (len <= mss) /* LSO is unusable */ 20778 tcp->tcp_last_sent_len = (ushort_t)len; 20779 len += tcp_hdr_len; 20780 if (tcp->tcp_ipversion == IPV4_VERSION) 20781 tcp->tcp_ipha->ipha_length = htons(len); 20782 else 20783 tcp->tcp_ip6h->ip6_plen = 20784 htons(len - 20785 ((char *)&tcp->tcp_ip6h[1] - 20786 tcp->tcp_iphc)); 20787 mp = dupb(*xmit_tail); 20788 if (mp == NULL) { 20789 if (ire != NULL) 20790 IRE_REFRELE(ire); 20791 return (-1); /* out_of_mem */ 20792 } 20793 mp->b_rptr = rptr; 20794 /* 20795 * If the old timestamp is no longer in use, 20796 * sample a new timestamp now. 20797 */ 20798 if ((*xmit_tail)->b_next == NULL) { 20799 (*xmit_tail)->b_prev = local_time; 20800 (*xmit_tail)->b_next = 20801 (mblk_t *)(uintptr_t)(*snxt-len); 20802 } 20803 goto must_alloc; 20804 } 20805 } else { 20806 *xmit_tail = (*xmit_tail)->b_cont; 20807 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20808 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20809 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20810 (*xmit_tail)->b_rptr); 20811 } 20812 20813 (*xmit_tail)->b_prev = local_time; 20814 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20815 20816 *tail_unsent -= len; 20817 if (len <= mss) /* LSO is unusable (!do_lso_send) */ 20818 tcp->tcp_last_sent_len = (ushort_t)len; 20819 20820 len += tcp_hdr_len; 20821 if (tcp->tcp_ipversion == IPV4_VERSION) 20822 tcp->tcp_ipha->ipha_length = htons(len); 20823 else 20824 tcp->tcp_ip6h->ip6_plen = htons(len - 20825 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20826 20827 mp = dupb(*xmit_tail); 20828 if (mp == NULL) { 20829 if (ire != NULL) 20830 IRE_REFRELE(ire); 20831 return (-1); /* out_of_mem */ 20832 } 20833 20834 len = tcp_hdr_len; 20835 /* 20836 * There are four reasons to allocate a new hdr mblk: 20837 * 1) The bytes above us are in use by another packet 20838 * 2) We don't have good alignment 20839 * 3) The mblk is being shared 20840 * 4) We don't have enough room for a header 20841 */ 20842 rptr = mp->b_rptr - len; 20843 if (!OK_32PTR(rptr) || 20844 ((db = mp->b_datap), db->db_ref != 2) || 20845 rptr < db->db_base + ire_fp_mp_len) { 20846 /* NOTE: we assume allocb returns an OK_32PTR */ 20847 20848 must_alloc:; 20849 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20850 tcps->tcps_wroff_xtra + ire_fp_mp_len, BPRI_MED); 20851 if (mp1 == NULL) { 20852 freemsg(mp); 20853 if (ire != NULL) 20854 IRE_REFRELE(ire); 20855 return (-1); /* out_of_mem */ 20856 } 20857 mp1->b_cont = mp; 20858 mp = mp1; 20859 /* Leave room for Link Level header */ 20860 len = tcp_hdr_len; 20861 rptr = 20862 &mp->b_rptr[tcps->tcps_wroff_xtra + ire_fp_mp_len]; 20863 mp->b_wptr = &rptr[len]; 20864 } 20865 20866 /* 20867 * Fill in the header using the template header, and add 20868 * options such as time-stamp, ECN and/or SACK, as needed. 20869 */ 20870 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20871 20872 mp->b_rptr = rptr; 20873 20874 if (*tail_unsent) { 20875 int spill = *tail_unsent; 20876 20877 mp1 = mp->b_cont; 20878 if (mp1 == NULL) 20879 mp1 = mp; 20880 20881 /* 20882 * If we're a little short, tack on more mblks until 20883 * there is no more spillover. 20884 */ 20885 while (spill < 0) { 20886 mblk_t *nmp; 20887 int nmpsz; 20888 20889 nmp = (*xmit_tail)->b_cont; 20890 nmpsz = MBLKL(nmp); 20891 20892 /* 20893 * Excess data in mblk; can we split it? 20894 * If MDT is enabled for the connection, 20895 * keep on splitting as this is a transient 20896 * send path. 20897 */ 20898 if (!do_lso_send && !tcp->tcp_mdt && 20899 (spill + nmpsz > 0)) { 20900 /* 20901 * Don't split if stream head was 20902 * told to break up larger writes 20903 * into smaller ones. 20904 */ 20905 if (tcp->tcp_maxpsz > 0) 20906 break; 20907 20908 /* 20909 * Next mblk is less than SMSS/2 20910 * rounded up to nearest 64-byte; 20911 * let it get sent as part of the 20912 * next segment. 20913 */ 20914 if (tcp->tcp_localnet && 20915 !tcp->tcp_cork && 20916 (nmpsz < roundup((mss >> 1), 64))) 20917 break; 20918 } 20919 20920 *xmit_tail = nmp; 20921 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20922 /* Stash for rtt use later */ 20923 (*xmit_tail)->b_prev = local_time; 20924 (*xmit_tail)->b_next = 20925 (mblk_t *)(uintptr_t)(*snxt - len); 20926 mp1->b_cont = dupb(*xmit_tail); 20927 mp1 = mp1->b_cont; 20928 20929 spill += nmpsz; 20930 if (mp1 == NULL) { 20931 *tail_unsent = spill; 20932 freemsg(mp); 20933 if (ire != NULL) 20934 IRE_REFRELE(ire); 20935 return (-1); /* out_of_mem */ 20936 } 20937 } 20938 20939 /* Trim back any surplus on the last mblk */ 20940 if (spill >= 0) { 20941 mp1->b_wptr -= spill; 20942 *tail_unsent = spill; 20943 } else { 20944 /* 20945 * We did not send everything we could in 20946 * order to remain within the b_cont limit. 20947 */ 20948 *usable -= spill; 20949 *snxt += spill; 20950 tcp->tcp_last_sent_len += spill; 20951 UPDATE_MIB(&tcps->tcps_mib, 20952 tcpOutDataBytes, spill); 20953 /* 20954 * Adjust the checksum 20955 */ 20956 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20957 sum += spill; 20958 sum = (sum >> 16) + (sum & 0xFFFF); 20959 U16_TO_ABE16(sum, tcph->th_sum); 20960 if (tcp->tcp_ipversion == IPV4_VERSION) { 20961 sum = ntohs( 20962 ((ipha_t *)rptr)->ipha_length) + 20963 spill; 20964 ((ipha_t *)rptr)->ipha_length = 20965 htons(sum); 20966 } else { 20967 sum = ntohs( 20968 ((ip6_t *)rptr)->ip6_plen) + 20969 spill; 20970 ((ip6_t *)rptr)->ip6_plen = 20971 htons(sum); 20972 } 20973 *tail_unsent = 0; 20974 } 20975 } 20976 if (tcp->tcp_ip_forward_progress) { 20977 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20978 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20979 tcp->tcp_ip_forward_progress = B_FALSE; 20980 } 20981 20982 if (do_lso_send) { 20983 tcp_lsosend_data(tcp, mp, ire, ill, mss, 20984 num_lso_seg); 20985 tcp->tcp_obsegs += num_lso_seg; 20986 20987 TCP_STAT(tcps, tcp_lso_times); 20988 TCP_STAT_UPDATE(tcps, tcp_lso_pkt_out, num_lso_seg); 20989 } else { 20990 tcp_send_data(tcp, q, mp); 20991 BUMP_LOCAL(tcp->tcp_obsegs); 20992 } 20993 } 20994 20995 if (ire != NULL) 20996 IRE_REFRELE(ire); 20997 return (0); 20998 } 20999 21000 /* Unlink and return any mblk that looks like it contains a MDT info */ 21001 static mblk_t * 21002 tcp_mdt_info_mp(mblk_t *mp) 21003 { 21004 mblk_t *prev_mp; 21005 21006 for (;;) { 21007 prev_mp = mp; 21008 /* no more to process? */ 21009 if ((mp = mp->b_cont) == NULL) 21010 break; 21011 21012 switch (DB_TYPE(mp)) { 21013 case M_CTL: 21014 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 21015 continue; 21016 ASSERT(prev_mp != NULL); 21017 prev_mp->b_cont = mp->b_cont; 21018 mp->b_cont = NULL; 21019 return (mp); 21020 default: 21021 break; 21022 } 21023 } 21024 return (mp); 21025 } 21026 21027 /* MDT info update routine, called when IP notifies us about MDT */ 21028 static void 21029 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 21030 { 21031 boolean_t prev_state; 21032 tcp_stack_t *tcps = tcp->tcp_tcps; 21033 21034 /* 21035 * IP is telling us to abort MDT on this connection? We know 21036 * this because the capability is only turned off when IP 21037 * encounters some pathological cases, e.g. link-layer change 21038 * where the new driver doesn't support MDT, or in situation 21039 * where MDT usage on the link-layer has been switched off. 21040 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 21041 * if the link-layer doesn't support MDT, and if it does, it 21042 * will indicate that the feature is to be turned on. 21043 */ 21044 prev_state = tcp->tcp_mdt; 21045 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 21046 if (!tcp->tcp_mdt && !first) { 21047 TCP_STAT(tcps, tcp_mdt_conn_halted3); 21048 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 21049 (void *)tcp->tcp_connp)); 21050 } 21051 21052 /* 21053 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 21054 * so disable MDT otherwise. The checks are done here 21055 * and in tcp_wput_data(). 21056 */ 21057 if (tcp->tcp_mdt && 21058 (tcp->tcp_ipversion == IPV4_VERSION && 21059 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21060 (tcp->tcp_ipversion == IPV6_VERSION && 21061 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 21062 tcp->tcp_mdt = B_FALSE; 21063 21064 if (tcp->tcp_mdt) { 21065 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 21066 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 21067 "version (%d), expected version is %d", 21068 mdt_capab->ill_mdt_version, MDT_VERSION_2); 21069 tcp->tcp_mdt = B_FALSE; 21070 return; 21071 } 21072 21073 /* 21074 * We need the driver to be able to handle at least three 21075 * spans per packet in order for tcp MDT to be utilized. 21076 * The first is for the header portion, while the rest are 21077 * needed to handle a packet that straddles across two 21078 * virtually non-contiguous buffers; a typical tcp packet 21079 * therefore consists of only two spans. Note that we take 21080 * a zero as "don't care". 21081 */ 21082 if (mdt_capab->ill_mdt_span_limit > 0 && 21083 mdt_capab->ill_mdt_span_limit < 3) { 21084 tcp->tcp_mdt = B_FALSE; 21085 return; 21086 } 21087 21088 /* a zero means driver wants default value */ 21089 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 21090 tcps->tcps_mdt_max_pbufs); 21091 if (tcp->tcp_mdt_max_pld == 0) 21092 tcp->tcp_mdt_max_pld = tcps->tcps_mdt_max_pbufs; 21093 21094 /* ensure 32-bit alignment */ 21095 tcp->tcp_mdt_hdr_head = roundup(MAX(tcps->tcps_mdt_hdr_head_min, 21096 mdt_capab->ill_mdt_hdr_head), 4); 21097 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcps->tcps_mdt_hdr_tail_min, 21098 mdt_capab->ill_mdt_hdr_tail), 4); 21099 21100 if (!first && !prev_state) { 21101 TCP_STAT(tcps, tcp_mdt_conn_resumed2); 21102 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 21103 (void *)tcp->tcp_connp)); 21104 } 21105 } 21106 } 21107 21108 /* Unlink and return any mblk that looks like it contains a LSO info */ 21109 static mblk_t * 21110 tcp_lso_info_mp(mblk_t *mp) 21111 { 21112 mblk_t *prev_mp; 21113 21114 for (;;) { 21115 prev_mp = mp; 21116 /* no more to process? */ 21117 if ((mp = mp->b_cont) == NULL) 21118 break; 21119 21120 switch (DB_TYPE(mp)) { 21121 case M_CTL: 21122 if (*(uint32_t *)mp->b_rptr != LSO_IOC_INFO_UPDATE) 21123 continue; 21124 ASSERT(prev_mp != NULL); 21125 prev_mp->b_cont = mp->b_cont; 21126 mp->b_cont = NULL; 21127 return (mp); 21128 default: 21129 break; 21130 } 21131 } 21132 21133 return (mp); 21134 } 21135 21136 /* LSO info update routine, called when IP notifies us about LSO */ 21137 static void 21138 tcp_lso_update(tcp_t *tcp, ill_lso_capab_t *lso_capab) 21139 { 21140 tcp_stack_t *tcps = tcp->tcp_tcps; 21141 21142 /* 21143 * IP is telling us to abort LSO on this connection? We know 21144 * this because the capability is only turned off when IP 21145 * encounters some pathological cases, e.g. link-layer change 21146 * where the new NIC/driver doesn't support LSO, or in situation 21147 * where LSO usage on the link-layer has been switched off. 21148 * IP would not have sent us the initial LSO_IOC_INFO_UPDATE 21149 * if the link-layer doesn't support LSO, and if it does, it 21150 * will indicate that the feature is to be turned on. 21151 */ 21152 tcp->tcp_lso = (lso_capab->ill_lso_on != 0); 21153 TCP_STAT(tcps, tcp_lso_enabled); 21154 21155 /* 21156 * We currently only support LSO on simple TCP/IPv4, 21157 * so disable LSO otherwise. The checks are done here 21158 * and in tcp_wput_data(). 21159 */ 21160 if (tcp->tcp_lso && 21161 (tcp->tcp_ipversion == IPV4_VERSION && 21162 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 21163 (tcp->tcp_ipversion == IPV6_VERSION)) { 21164 tcp->tcp_lso = B_FALSE; 21165 TCP_STAT(tcps, tcp_lso_disabled); 21166 } else { 21167 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, 21168 lso_capab->ill_lso_max); 21169 } 21170 } 21171 21172 static void 21173 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_lso_mdt) 21174 { 21175 conn_t *connp = tcp->tcp_connp; 21176 tcp_stack_t *tcps = tcp->tcp_tcps; 21177 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21178 21179 ASSERT(ire != NULL); 21180 21181 /* 21182 * We may be in the fastpath here, and although we essentially do 21183 * similar checks as in ip_bind_connected{_v6}/ip_xxinfo_return, 21184 * we try to keep things as brief as possible. After all, these 21185 * are only best-effort checks, and we do more thorough ones prior 21186 * to calling tcp_send()/tcp_multisend(). 21187 */ 21188 if ((ipst->ips_ip_lso_outbound || ipst->ips_ip_multidata_outbound) && 21189 check_lso_mdt && !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 21190 ill != NULL && !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 21191 !(ire->ire_flags & RTF_MULTIRT) && 21192 !IPP_ENABLED(IPP_LOCAL_OUT, ipst) && 21193 CONN_IS_LSO_MD_FASTPATH(connp)) { 21194 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) { 21195 /* Cache the result */ 21196 connp->conn_lso_ok = B_TRUE; 21197 21198 ASSERT(ill->ill_lso_capab != NULL); 21199 if (!ill->ill_lso_capab->ill_lso_on) { 21200 ill->ill_lso_capab->ill_lso_on = 1; 21201 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21202 "LSO for interface %s\n", (void *)connp, 21203 ill->ill_name)); 21204 } 21205 tcp_lso_update(tcp, ill->ill_lso_capab); 21206 } else if (ipst->ips_ip_multidata_outbound && 21207 ILL_MDT_CAPABLE(ill)) { 21208 /* Cache the result */ 21209 connp->conn_mdt_ok = B_TRUE; 21210 21211 ASSERT(ill->ill_mdt_capab != NULL); 21212 if (!ill->ill_mdt_capab->ill_mdt_on) { 21213 ill->ill_mdt_capab->ill_mdt_on = 1; 21214 ip1dbg(("tcp_ire_ill_check: connp %p enables " 21215 "MDT for interface %s\n", (void *)connp, 21216 ill->ill_name)); 21217 } 21218 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 21219 } 21220 } 21221 21222 /* 21223 * The goal is to reduce the number of generated tcp segments by 21224 * setting the maxpsz multiplier to 0; this will have an affect on 21225 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 21226 * into each packet, up to SMSS bytes. Doing this reduces the number 21227 * of outbound segments and incoming ACKs, thus allowing for better 21228 * network and system performance. In contrast the legacy behavior 21229 * may result in sending less than SMSS size, because the last mblk 21230 * for some packets may have more data than needed to make up SMSS, 21231 * and the legacy code refused to "split" it. 21232 * 21233 * We apply the new behavior on following situations: 21234 * 21235 * 1) Loopback connections, 21236 * 2) Connections in which the remote peer is not on local subnet, 21237 * 3) Local subnet connections over the bge interface (see below). 21238 * 21239 * Ideally, we would like this behavior to apply for interfaces other 21240 * than bge. However, doing so would negatively impact drivers which 21241 * perform dynamic mapping and unmapping of DMA resources, which are 21242 * increased by setting the maxpsz multiplier to 0 (more mblks per 21243 * packet will be generated by tcp). The bge driver does not suffer 21244 * from this, as it copies the mblks into pre-mapped buffers, and 21245 * therefore does not require more I/O resources than before. 21246 * 21247 * Otherwise, this behavior is present on all network interfaces when 21248 * the destination endpoint is non-local, since reducing the number 21249 * of packets in general is good for the network. 21250 * 21251 * TODO We need to remove this hard-coded conditional for bge once 21252 * a better "self-tuning" mechanism, or a way to comprehend 21253 * the driver transmit strategy is devised. Until the solution 21254 * is found and well understood, we live with this hack. 21255 */ 21256 if (!tcp_static_maxpsz && 21257 (tcp->tcp_loopback || !tcp->tcp_localnet || 21258 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 21259 /* override the default value */ 21260 tcp->tcp_maxpsz = 0; 21261 21262 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 21263 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 21264 ill != NULL ? ill->ill_name : ipif_loopback_name)); 21265 } 21266 21267 /* set the stream head parameters accordingly */ 21268 (void) tcp_maxpsz_set(tcp, B_TRUE); 21269 } 21270 21271 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 21272 static void 21273 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 21274 { 21275 uchar_t fval = *mp->b_rptr; 21276 mblk_t *tail; 21277 queue_t *q = tcp->tcp_wq; 21278 21279 /* TODO: How should flush interact with urgent data? */ 21280 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 21281 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 21282 /* 21283 * Flush only data that has not yet been put on the wire. If 21284 * we flush data that we have already transmitted, life, as we 21285 * know it, may come to an end. 21286 */ 21287 tail = tcp->tcp_xmit_tail; 21288 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 21289 tcp->tcp_xmit_tail_unsent = 0; 21290 tcp->tcp_unsent = 0; 21291 if (tail->b_wptr != tail->b_rptr) 21292 tail = tail->b_cont; 21293 if (tail) { 21294 mblk_t **excess = &tcp->tcp_xmit_head; 21295 for (;;) { 21296 mblk_t *mp1 = *excess; 21297 if (mp1 == tail) 21298 break; 21299 tcp->tcp_xmit_tail = mp1; 21300 tcp->tcp_xmit_last = mp1; 21301 excess = &mp1->b_cont; 21302 } 21303 *excess = NULL; 21304 tcp_close_mpp(&tail); 21305 if (tcp->tcp_snd_zcopy_aware) 21306 tcp_zcopy_notify(tcp); 21307 } 21308 /* 21309 * We have no unsent data, so unsent must be less than 21310 * tcp_xmit_lowater, so re-enable flow. 21311 */ 21312 mutex_enter(&tcp->tcp_non_sq_lock); 21313 if (tcp->tcp_flow_stopped) { 21314 tcp_clrqfull(tcp); 21315 } 21316 mutex_exit(&tcp->tcp_non_sq_lock); 21317 } 21318 /* 21319 * TODO: you can't just flush these, you have to increase rwnd for one 21320 * thing. For another, how should urgent data interact? 21321 */ 21322 if (fval & FLUSHR) { 21323 *mp->b_rptr = fval & ~FLUSHW; 21324 /* XXX */ 21325 qreply(q, mp); 21326 return; 21327 } 21328 freemsg(mp); 21329 } 21330 21331 /* 21332 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 21333 * messages. 21334 */ 21335 static void 21336 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 21337 { 21338 mblk_t *mp1; 21339 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 21340 STRUCT_HANDLE(strbuf, sb); 21341 queue_t *q = tcp->tcp_wq; 21342 int error; 21343 uint_t addrlen; 21344 21345 /* Make sure it is one of ours. */ 21346 switch (iocp->ioc_cmd) { 21347 case TI_GETMYNAME: 21348 case TI_GETPEERNAME: 21349 break; 21350 default: 21351 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 21352 return; 21353 } 21354 switch (mi_copy_state(q, mp, &mp1)) { 21355 case -1: 21356 return; 21357 case MI_COPY_CASE(MI_COPY_IN, 1): 21358 break; 21359 case MI_COPY_CASE(MI_COPY_OUT, 1): 21360 /* Copy out the strbuf. */ 21361 mi_copyout(q, mp); 21362 return; 21363 case MI_COPY_CASE(MI_COPY_OUT, 2): 21364 /* All done. */ 21365 mi_copy_done(q, mp, 0); 21366 return; 21367 default: 21368 mi_copy_done(q, mp, EPROTO); 21369 return; 21370 } 21371 /* Check alignment of the strbuf */ 21372 if (!OK_32PTR(mp1->b_rptr)) { 21373 mi_copy_done(q, mp, EINVAL); 21374 return; 21375 } 21376 21377 STRUCT_SET_HANDLE(sb, iocp->ioc_flag, (void *)mp1->b_rptr); 21378 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 21379 if (STRUCT_FGET(sb, maxlen) < addrlen) { 21380 mi_copy_done(q, mp, EINVAL); 21381 return; 21382 } 21383 21384 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 21385 if (mp1 == NULL) 21386 return; 21387 21388 switch (iocp->ioc_cmd) { 21389 case TI_GETMYNAME: 21390 error = tcp_do_getsockname(tcp, (void *)mp1->b_rptr, &addrlen); 21391 break; 21392 case TI_GETPEERNAME: 21393 error = tcp_do_getpeername(tcp, (void *)mp1->b_rptr, &addrlen); 21394 break; 21395 } 21396 21397 if (error != 0) { 21398 mi_copy_done(q, mp, error); 21399 } else { 21400 mp1->b_wptr += addrlen; 21401 STRUCT_FSET(sb, len, addrlen); 21402 21403 /* Copy out the address */ 21404 mi_copyout(q, mp); 21405 } 21406 } 21407 21408 static void 21409 tcp_disable_direct_sockfs(tcp_t *tcp) 21410 { 21411 #ifdef _ILP32 21412 tcp->tcp_acceptor_id = (t_uscalar_t)tcp->tcp_rq; 21413 #else 21414 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 21415 #endif 21416 /* 21417 * Insert this socket into the acceptor hash. 21418 * We might need it for T_CONN_RES message 21419 */ 21420 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 21421 21422 if (tcp->tcp_fused) { 21423 /* 21424 * This is a fused loopback tcp; disable 21425 * read-side synchronous streams interface 21426 * and drain any queued data. It is okay 21427 * to do this for non-synchronous streams 21428 * fused tcp as well. 21429 */ 21430 tcp_fuse_disable_pair(tcp, B_FALSE); 21431 } 21432 tcp->tcp_issocket = B_FALSE; 21433 TCP_STAT(tcp->tcp_tcps, tcp_sock_fallback); 21434 } 21435 21436 /* 21437 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 21438 * messages. 21439 */ 21440 /* ARGSUSED */ 21441 static void 21442 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 21443 { 21444 conn_t *connp = (conn_t *)arg; 21445 tcp_t *tcp = connp->conn_tcp; 21446 queue_t *q = tcp->tcp_wq; 21447 struct iocblk *iocp; 21448 21449 ASSERT(DB_TYPE(mp) == M_IOCTL); 21450 /* 21451 * Try and ASSERT the minimum possible references on the 21452 * conn early enough. Since we are executing on write side, 21453 * the connection is obviously not detached and that means 21454 * there is a ref each for TCP and IP. Since we are behind 21455 * the squeue, the minimum references needed are 3. If the 21456 * conn is in classifier hash list, there should be an 21457 * extra ref for that (we check both the possibilities). 21458 */ 21459 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21460 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21461 21462 iocp = (struct iocblk *)mp->b_rptr; 21463 switch (iocp->ioc_cmd) { 21464 case TCP_IOC_DEFAULT_Q: 21465 /* Wants to be the default wq. */ 21466 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 21467 iocp->ioc_error = EPERM; 21468 iocp->ioc_count = 0; 21469 mp->b_datap->db_type = M_IOCACK; 21470 qreply(q, mp); 21471 return; 21472 } 21473 tcp_def_q_set(tcp, mp); 21474 return; 21475 case _SIOCSOCKFALLBACK: 21476 /* 21477 * Either sockmod is about to be popped and the socket 21478 * would now be treated as a plain stream, or a module 21479 * is about to be pushed so we could no longer use read- 21480 * side synchronous streams for fused loopback tcp. 21481 * Drain any queued data and disable direct sockfs 21482 * interface from now on. 21483 */ 21484 if (!tcp->tcp_issocket) { 21485 DB_TYPE(mp) = M_IOCNAK; 21486 iocp->ioc_error = EINVAL; 21487 } else { 21488 tcp_disable_direct_sockfs(tcp); 21489 DB_TYPE(mp) = M_IOCACK; 21490 iocp->ioc_error = 0; 21491 } 21492 iocp->ioc_count = 0; 21493 iocp->ioc_rval = 0; 21494 qreply(q, mp); 21495 return; 21496 } 21497 CALL_IP_WPUT(connp, q, mp); 21498 } 21499 21500 /* 21501 * This routine is called by tcp_wput() to handle all TPI requests. 21502 */ 21503 /* ARGSUSED */ 21504 static void 21505 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 21506 { 21507 conn_t *connp = (conn_t *)arg; 21508 tcp_t *tcp = connp->conn_tcp; 21509 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 21510 uchar_t *rptr; 21511 t_scalar_t type; 21512 cred_t *cr; 21513 21514 /* 21515 * Try and ASSERT the minimum possible references on the 21516 * conn early enough. Since we are executing on write side, 21517 * the connection is obviously not detached and that means 21518 * there is a ref each for TCP and IP. Since we are behind 21519 * the squeue, the minimum references needed are 3. If the 21520 * conn is in classifier hash list, there should be an 21521 * extra ref for that (we check both the possibilities). 21522 */ 21523 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21524 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21525 21526 rptr = mp->b_rptr; 21527 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21528 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21529 type = ((union T_primitives *)rptr)->type; 21530 if (type == T_EXDATA_REQ) { 21531 tcp_output_urgent(connp, mp->b_cont, arg2); 21532 freeb(mp); 21533 } else if (type != T_DATA_REQ) { 21534 goto non_urgent_data; 21535 } else { 21536 /* TODO: options, flags, ... from user */ 21537 /* Set length to zero for reclamation below */ 21538 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21539 freeb(mp); 21540 } 21541 return; 21542 } else { 21543 if (tcp->tcp_debug) { 21544 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21545 "tcp_wput_proto, dropping one..."); 21546 } 21547 freemsg(mp); 21548 return; 21549 } 21550 21551 non_urgent_data: 21552 21553 switch ((int)tprim->type) { 21554 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21555 /* 21556 * save the kssl_ent_t from the next block, and convert this 21557 * back to a normal bind_req. 21558 */ 21559 if (mp->b_cont != NULL) { 21560 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21561 21562 if (tcp->tcp_kssl_ent != NULL) { 21563 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21564 KSSL_NO_PROXY); 21565 tcp->tcp_kssl_ent = NULL; 21566 } 21567 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21568 sizeof (kssl_ent_t)); 21569 kssl_hold_ent(tcp->tcp_kssl_ent); 21570 freemsg(mp->b_cont); 21571 mp->b_cont = NULL; 21572 } 21573 tprim->type = T_BIND_REQ; 21574 21575 /* FALLTHROUGH */ 21576 case O_T_BIND_REQ: /* bind request */ 21577 case T_BIND_REQ: /* new semantics bind request */ 21578 tcp_tpi_bind(tcp, mp); 21579 break; 21580 case T_UNBIND_REQ: /* unbind request */ 21581 tcp_tpi_unbind(tcp, mp); 21582 break; 21583 case O_T_CONN_RES: /* old connection response XXX */ 21584 case T_CONN_RES: /* connection response */ 21585 tcp_tli_accept(tcp, mp); 21586 break; 21587 case T_CONN_REQ: /* connection request */ 21588 tcp_tpi_connect(tcp, mp); 21589 break; 21590 case T_DISCON_REQ: /* disconnect request */ 21591 tcp_disconnect(tcp, mp); 21592 break; 21593 case T_CAPABILITY_REQ: 21594 tcp_capability_req(tcp, mp); /* capability request */ 21595 break; 21596 case T_INFO_REQ: /* information request */ 21597 tcp_info_req(tcp, mp); 21598 break; 21599 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21600 case T_OPTMGMT_REQ: 21601 /* 21602 * Note: no support for snmpcom_req() through new 21603 * T_OPTMGMT_REQ. See comments in ip.c 21604 */ 21605 21606 /* 21607 * All Solaris components should pass a db_credp 21608 * for this TPI message, hence we ASSERT. 21609 * But in case there is some other M_PROTO that looks 21610 * like a TPI message sent by some other kernel 21611 * component, we check and return an error. 21612 */ 21613 cr = msg_getcred(mp, NULL); 21614 ASSERT(cr != NULL); 21615 if (cr == NULL) { 21616 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 21617 return; 21618 } 21619 /* 21620 * If EINPROGRESS is returned, the request has been queued 21621 * for subsequent processing by ip_restart_optmgmt(), which 21622 * will do the CONN_DEC_REF(). 21623 */ 21624 CONN_INC_REF(connp); 21625 if ((int)tprim->type == T_SVR4_OPTMGMT_REQ) { 21626 if (svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21627 B_TRUE) != EINPROGRESS) { 21628 CONN_DEC_REF(connp); 21629 } 21630 } else { 21631 if (tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj, 21632 B_TRUE) != EINPROGRESS) { 21633 CONN_DEC_REF(connp); 21634 } 21635 } 21636 break; 21637 21638 case T_UNITDATA_REQ: /* unitdata request */ 21639 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21640 break; 21641 case T_ORDREL_REQ: /* orderly release req */ 21642 freemsg(mp); 21643 21644 if (tcp->tcp_fused) 21645 tcp_unfuse(tcp); 21646 21647 if (tcp_xmit_end(tcp) != 0) { 21648 /* 21649 * We were crossing FINs and got a reset from 21650 * the other side. Just ignore it. 21651 */ 21652 if (tcp->tcp_debug) { 21653 (void) strlog(TCP_MOD_ID, 0, 1, 21654 SL_ERROR|SL_TRACE, 21655 "tcp_wput_proto, T_ORDREL_REQ out of " 21656 "state %s", 21657 tcp_display(tcp, NULL, 21658 DISP_ADDR_AND_PORT)); 21659 } 21660 } 21661 break; 21662 case T_ADDR_REQ: 21663 tcp_addr_req(tcp, mp); 21664 break; 21665 default: 21666 if (tcp->tcp_debug) { 21667 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21668 "tcp_wput_proto, bogus TPI msg, type %d", 21669 tprim->type); 21670 } 21671 /* 21672 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21673 * to recover. 21674 */ 21675 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21676 break; 21677 } 21678 } 21679 21680 /* 21681 * The TCP write service routine should never be called... 21682 */ 21683 /* ARGSUSED */ 21684 static void 21685 tcp_wsrv(queue_t *q) 21686 { 21687 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 21688 21689 TCP_STAT(tcps, tcp_wsrv_called); 21690 } 21691 21692 /* Non overlapping byte exchanger */ 21693 static void 21694 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21695 { 21696 uchar_t uch; 21697 21698 while (len-- > 0) { 21699 uch = a[len]; 21700 a[len] = b[len]; 21701 b[len] = uch; 21702 } 21703 } 21704 21705 /* 21706 * Send out a control packet on the tcp connection specified. This routine 21707 * is typically called where we need a simple ACK or RST generated. 21708 */ 21709 static void 21710 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21711 { 21712 uchar_t *rptr; 21713 tcph_t *tcph; 21714 ipha_t *ipha = NULL; 21715 ip6_t *ip6h = NULL; 21716 uint32_t sum; 21717 int tcp_hdr_len; 21718 int tcp_ip_hdr_len; 21719 mblk_t *mp; 21720 tcp_stack_t *tcps = tcp->tcp_tcps; 21721 21722 /* 21723 * Save sum for use in source route later. 21724 */ 21725 ASSERT(tcp != NULL); 21726 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21727 tcp_hdr_len = tcp->tcp_hdr_len; 21728 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21729 21730 /* If a text string is passed in with the request, pass it to strlog. */ 21731 if (str != NULL && tcp->tcp_debug) { 21732 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21733 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21734 str, seq, ack, ctl); 21735 } 21736 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcps->tcps_wroff_xtra, 21737 BPRI_MED); 21738 if (mp == NULL) { 21739 return; 21740 } 21741 rptr = &mp->b_rptr[tcps->tcps_wroff_xtra]; 21742 mp->b_rptr = rptr; 21743 mp->b_wptr = &rptr[tcp_hdr_len]; 21744 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21745 21746 if (tcp->tcp_ipversion == IPV4_VERSION) { 21747 ipha = (ipha_t *)rptr; 21748 ipha->ipha_length = htons(tcp_hdr_len); 21749 } else { 21750 ip6h = (ip6_t *)rptr; 21751 ASSERT(tcp != NULL); 21752 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21753 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21754 } 21755 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21756 tcph->th_flags[0] = (uint8_t)ctl; 21757 if (ctl & TH_RST) { 21758 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 21759 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 21760 /* 21761 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21762 */ 21763 if (tcp->tcp_snd_ts_ok && 21764 tcp->tcp_state > TCPS_SYN_SENT) { 21765 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21766 *(mp->b_wptr) = TCPOPT_EOL; 21767 if (tcp->tcp_ipversion == IPV4_VERSION) { 21768 ipha->ipha_length = htons(tcp_hdr_len - 21769 TCPOPT_REAL_TS_LEN); 21770 } else { 21771 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21772 TCPOPT_REAL_TS_LEN); 21773 } 21774 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21775 sum -= TCPOPT_REAL_TS_LEN; 21776 } 21777 } 21778 if (ctl & TH_ACK) { 21779 if (tcp->tcp_snd_ts_ok) { 21780 U32_TO_BE32(lbolt, 21781 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21782 U32_TO_BE32(tcp->tcp_ts_recent, 21783 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21784 } 21785 21786 /* Update the latest receive window size in TCP header. */ 21787 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21788 tcph->th_win); 21789 tcp->tcp_rack = ack; 21790 tcp->tcp_rack_cnt = 0; 21791 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 21792 } 21793 BUMP_LOCAL(tcp->tcp_obsegs); 21794 U32_TO_BE32(seq, tcph->th_seq); 21795 U32_TO_BE32(ack, tcph->th_ack); 21796 /* 21797 * Include the adjustment for a source route if any. 21798 */ 21799 sum = (sum >> 16) + (sum & 0xFFFF); 21800 U16_TO_BE16(sum, tcph->th_sum); 21801 tcp_send_data(tcp, tcp->tcp_wq, mp); 21802 } 21803 21804 /* 21805 * If this routine returns B_TRUE, TCP can generate a RST in response 21806 * to a segment. If it returns B_FALSE, TCP should not respond. 21807 */ 21808 static boolean_t 21809 tcp_send_rst_chk(tcp_stack_t *tcps) 21810 { 21811 clock_t now; 21812 21813 /* 21814 * TCP needs to protect itself from generating too many RSTs. 21815 * This can be a DoS attack by sending us random segments 21816 * soliciting RSTs. 21817 * 21818 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21819 * in each 1 second interval. In this way, TCP still generate 21820 * RSTs in normal cases but when under attack, the impact is 21821 * limited. 21822 */ 21823 if (tcps->tcps_rst_sent_rate_enabled != 0) { 21824 now = lbolt; 21825 /* lbolt can wrap around. */ 21826 if ((tcps->tcps_last_rst_intrvl > now) || 21827 (TICK_TO_MSEC(now - tcps->tcps_last_rst_intrvl) > 21828 1*SECONDS)) { 21829 tcps->tcps_last_rst_intrvl = now; 21830 tcps->tcps_rst_cnt = 1; 21831 } else if (++tcps->tcps_rst_cnt > tcps->tcps_rst_sent_rate) { 21832 return (B_FALSE); 21833 } 21834 } 21835 return (B_TRUE); 21836 } 21837 21838 /* 21839 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21840 */ 21841 static void 21842 tcp_ip_ire_mark_advice(tcp_t *tcp) 21843 { 21844 mblk_t *mp; 21845 ipic_t *ipic; 21846 21847 if (tcp->tcp_ipversion == IPV4_VERSION) { 21848 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21849 &ipic); 21850 } else { 21851 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21852 &ipic); 21853 } 21854 if (mp == NULL) 21855 return; 21856 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21857 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21858 } 21859 21860 /* 21861 * Return an IP advice ioctl mblk and set ipic to be the pointer 21862 * to the advice structure. 21863 */ 21864 static mblk_t * 21865 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21866 { 21867 struct iocblk *ioc; 21868 mblk_t *mp, *mp1; 21869 21870 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21871 if (mp == NULL) 21872 return (NULL); 21873 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21874 *ipic = (ipic_t *)mp->b_rptr; 21875 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21876 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21877 21878 bcopy(addr, *ipic + 1, addr_len); 21879 21880 (*ipic)->ipic_addr_length = addr_len; 21881 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21882 21883 mp1 = mkiocb(IP_IOCTL); 21884 if (mp1 == NULL) { 21885 freemsg(mp); 21886 return (NULL); 21887 } 21888 mp1->b_cont = mp; 21889 ioc = (struct iocblk *)mp1->b_rptr; 21890 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21891 21892 return (mp1); 21893 } 21894 21895 /* 21896 * Generate a reset based on an inbound packet, connp is set by caller 21897 * when RST is in response to an unexpected inbound packet for which 21898 * there is active tcp state in the system. 21899 * 21900 * IPSEC NOTE : Try to send the reply with the same protection as it came 21901 * in. We still have the ipsec_mp that the packet was attached to. Thus 21902 * the packet will go out at the same level of protection as it came in by 21903 * converting the IPSEC_IN to IPSEC_OUT. 21904 */ 21905 static void 21906 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21907 uint32_t ack, int ctl, uint_t ip_hdr_len, zoneid_t zoneid, 21908 tcp_stack_t *tcps, conn_t *connp) 21909 { 21910 ipha_t *ipha = NULL; 21911 ip6_t *ip6h = NULL; 21912 ushort_t len; 21913 tcph_t *tcph; 21914 int i; 21915 mblk_t *ipsec_mp; 21916 boolean_t mctl_present; 21917 ipic_t *ipic; 21918 ipaddr_t v4addr; 21919 in6_addr_t v6addr; 21920 int addr_len; 21921 void *addr; 21922 queue_t *q = tcps->tcps_g_q; 21923 tcp_t *tcp; 21924 cred_t *cr; 21925 mblk_t *nmp; 21926 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 21927 21928 if (tcps->tcps_g_q == NULL) { 21929 /* 21930 * For non-zero stackids the default queue isn't created 21931 * until the first open, thus there can be a need to send 21932 * a reset before then. But we can't do that, hence we just 21933 * drop the packet. Later during boot, when the default queue 21934 * has been setup, a retransmitted packet from the peer 21935 * will result in a reset. 21936 */ 21937 ASSERT(tcps->tcps_netstack->netstack_stackid != 21938 GLOBAL_NETSTACKID); 21939 freemsg(mp); 21940 return; 21941 } 21942 21943 if (connp != NULL) 21944 tcp = connp->conn_tcp; 21945 else 21946 tcp = Q_TO_TCP(q); 21947 21948 if (!tcp_send_rst_chk(tcps)) { 21949 tcps->tcps_rst_unsent++; 21950 freemsg(mp); 21951 return; 21952 } 21953 21954 if (mp->b_datap->db_type == M_CTL) { 21955 ipsec_mp = mp; 21956 mp = mp->b_cont; 21957 mctl_present = B_TRUE; 21958 } else { 21959 ipsec_mp = mp; 21960 mctl_present = B_FALSE; 21961 } 21962 21963 if (str && q && tcps->tcps_dbg) { 21964 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21965 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21966 "flags 0x%x", 21967 str, seq, ack, ctl); 21968 } 21969 if (mp->b_datap->db_ref != 1) { 21970 mblk_t *mp1 = copyb(mp); 21971 freemsg(mp); 21972 mp = mp1; 21973 if (!mp) { 21974 if (mctl_present) 21975 freeb(ipsec_mp); 21976 return; 21977 } else { 21978 if (mctl_present) { 21979 ipsec_mp->b_cont = mp; 21980 } else { 21981 ipsec_mp = mp; 21982 } 21983 } 21984 } else if (mp->b_cont) { 21985 freemsg(mp->b_cont); 21986 mp->b_cont = NULL; 21987 } 21988 /* 21989 * We skip reversing source route here. 21990 * (for now we replace all IP options with EOL) 21991 */ 21992 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21993 ipha = (ipha_t *)mp->b_rptr; 21994 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21995 mp->b_rptr[i] = IPOPT_EOL; 21996 /* 21997 * Make sure that src address isn't flagrantly invalid. 21998 * Not all broadcast address checking for the src address 21999 * is possible, since we don't know the netmask of the src 22000 * addr. No check for destination address is done, since 22001 * IP will not pass up a packet with a broadcast dest 22002 * address to TCP. Similar checks are done below for IPv6. 22003 */ 22004 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 22005 CLASSD(ipha->ipha_src)) { 22006 freemsg(ipsec_mp); 22007 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards); 22008 return; 22009 } 22010 } else { 22011 ip6h = (ip6_t *)mp->b_rptr; 22012 22013 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 22014 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 22015 freemsg(ipsec_mp); 22016 BUMP_MIB(&ipst->ips_ip6_mib, ipIfStatsInDiscards); 22017 return; 22018 } 22019 22020 /* Remove any extension headers assuming partial overlay */ 22021 if (ip_hdr_len > IPV6_HDR_LEN) { 22022 uint8_t *to; 22023 22024 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 22025 ovbcopy(ip6h, to, IPV6_HDR_LEN); 22026 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 22027 ip_hdr_len = IPV6_HDR_LEN; 22028 ip6h = (ip6_t *)mp->b_rptr; 22029 ip6h->ip6_nxt = IPPROTO_TCP; 22030 } 22031 } 22032 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 22033 if (tcph->th_flags[0] & TH_RST) { 22034 freemsg(ipsec_mp); 22035 return; 22036 } 22037 tcph->th_offset_and_rsrvd[0] = (5 << 4); 22038 len = ip_hdr_len + sizeof (tcph_t); 22039 mp->b_wptr = &mp->b_rptr[len]; 22040 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22041 ipha->ipha_length = htons(len); 22042 /* Swap addresses */ 22043 v4addr = ipha->ipha_src; 22044 ipha->ipha_src = ipha->ipha_dst; 22045 ipha->ipha_dst = v4addr; 22046 ipha->ipha_ident = 0; 22047 ipha->ipha_ttl = (uchar_t)tcps->tcps_ipv4_ttl; 22048 addr_len = IP_ADDR_LEN; 22049 addr = &v4addr; 22050 } else { 22051 /* No ip6i_t in this case */ 22052 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 22053 /* Swap addresses */ 22054 v6addr = ip6h->ip6_src; 22055 ip6h->ip6_src = ip6h->ip6_dst; 22056 ip6h->ip6_dst = v6addr; 22057 ip6h->ip6_hops = (uchar_t)tcps->tcps_ipv6_hoplimit; 22058 addr_len = IPV6_ADDR_LEN; 22059 addr = &v6addr; 22060 } 22061 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 22062 U32_TO_BE32(ack, tcph->th_ack); 22063 U32_TO_BE32(seq, tcph->th_seq); 22064 U16_TO_BE16(0, tcph->th_win); 22065 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 22066 tcph->th_flags[0] = (uint8_t)ctl; 22067 if (ctl & TH_RST) { 22068 BUMP_MIB(&tcps->tcps_mib, tcpOutRsts); 22069 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22070 } 22071 22072 /* IP trusts us to set up labels when required. */ 22073 if (is_system_labeled() && (cr = msg_getcred(mp, NULL)) != NULL && 22074 crgetlabel(cr) != NULL) { 22075 int err; 22076 22077 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 22078 err = tsol_check_label(cr, &mp, 22079 tcp->tcp_connp->conn_mac_exempt, 22080 tcps->tcps_netstack->netstack_ip); 22081 else 22082 err = tsol_check_label_v6(cr, &mp, 22083 tcp->tcp_connp->conn_mac_exempt, 22084 tcps->tcps_netstack->netstack_ip); 22085 if (mctl_present) 22086 ipsec_mp->b_cont = mp; 22087 else 22088 ipsec_mp = mp; 22089 if (err != 0) { 22090 freemsg(ipsec_mp); 22091 return; 22092 } 22093 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22094 ipha = (ipha_t *)mp->b_rptr; 22095 } else { 22096 ip6h = (ip6_t *)mp->b_rptr; 22097 } 22098 } 22099 22100 if (mctl_present) { 22101 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22102 22103 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22104 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 22105 return; 22106 } 22107 } 22108 if (zoneid == ALL_ZONES) 22109 zoneid = GLOBAL_ZONEID; 22110 22111 /* Add the zoneid so ip_output routes it properly */ 22112 if ((nmp = ip_prepend_zoneid(ipsec_mp, zoneid, ipst)) == NULL) { 22113 freemsg(ipsec_mp); 22114 return; 22115 } 22116 ipsec_mp = nmp; 22117 22118 /* 22119 * NOTE: one might consider tracing a TCP packet here, but 22120 * this function has no active TCP state and no tcp structure 22121 * that has a trace buffer. If we traced here, we would have 22122 * to keep a local trace buffer in tcp_record_trace(). 22123 * 22124 * TSol note: The mblk that contains the incoming packet was 22125 * reused by tcp_xmit_listener_reset, so it already contains 22126 * the right credentials and we don't need to call mblk_setcred. 22127 * Also the conn's cred is not right since it is associated 22128 * with tcps_g_q. 22129 */ 22130 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 22131 22132 /* 22133 * Tell IP to mark the IRE used for this destination temporary. 22134 * This way, we can limit our exposure to DoS attack because IP 22135 * creates an IRE for each destination. If there are too many, 22136 * the time to do any routing lookup will be extremely long. And 22137 * the lookup can be in interrupt context. 22138 * 22139 * Note that in normal circumstances, this marking should not 22140 * affect anything. It would be nice if only 1 message is 22141 * needed to inform IP that the IRE created for this RST should 22142 * not be added to the cache table. But there is currently 22143 * not such communication mechanism between TCP and IP. So 22144 * the best we can do now is to send the advice ioctl to IP 22145 * to mark the IRE temporary. 22146 */ 22147 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 22148 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 22149 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22150 } 22151 } 22152 22153 /* 22154 * Initiate closedown sequence on an active connection. (May be called as 22155 * writer.) Return value zero for OK return, non-zero for error return. 22156 */ 22157 static int 22158 tcp_xmit_end(tcp_t *tcp) 22159 { 22160 ipic_t *ipic; 22161 mblk_t *mp; 22162 tcp_stack_t *tcps = tcp->tcp_tcps; 22163 22164 if (tcp->tcp_state < TCPS_SYN_RCVD || 22165 tcp->tcp_state > TCPS_CLOSE_WAIT) { 22166 /* 22167 * Invalid state, only states TCPS_SYN_RCVD, 22168 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 22169 */ 22170 return (-1); 22171 } 22172 22173 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 22174 tcp->tcp_valid_bits |= TCP_FSS_VALID; 22175 /* 22176 * If there is nothing more unsent, send the FIN now. 22177 * Otherwise, it will go out with the last segment. 22178 */ 22179 if (tcp->tcp_unsent == 0) { 22180 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 22181 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 22182 22183 if (mp) { 22184 tcp_send_data(tcp, tcp->tcp_wq, mp); 22185 } else { 22186 /* 22187 * Couldn't allocate msg. Pretend we got it out. 22188 * Wait for rexmit timeout. 22189 */ 22190 tcp->tcp_snxt = tcp->tcp_fss + 1; 22191 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22192 } 22193 22194 /* 22195 * If needed, update tcp_rexmit_snxt as tcp_snxt is 22196 * changed. 22197 */ 22198 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 22199 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 22200 } 22201 } else { 22202 /* 22203 * If tcp->tcp_cork is set, then the data will not get sent, 22204 * so we have to check that and unset it first. 22205 */ 22206 if (tcp->tcp_cork) 22207 tcp->tcp_cork = B_FALSE; 22208 tcp_wput_data(tcp, NULL, B_FALSE); 22209 } 22210 22211 /* 22212 * If TCP does not get enough samples of RTT or tcp_rtt_updates 22213 * is 0, don't update the cache. 22214 */ 22215 if (tcps->tcps_rtt_updates == 0 || 22216 tcp->tcp_rtt_update < tcps->tcps_rtt_updates) 22217 return (0); 22218 22219 /* 22220 * NOTE: should not update if source routes i.e. if tcp_remote if 22221 * different from the destination. 22222 */ 22223 if (tcp->tcp_ipversion == IPV4_VERSION) { 22224 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 22225 return (0); 22226 } 22227 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 22228 &ipic); 22229 } else { 22230 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 22231 &tcp->tcp_ip6h->ip6_dst))) { 22232 return (0); 22233 } 22234 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 22235 &ipic); 22236 } 22237 22238 /* Record route attributes in the IRE for use by future connections. */ 22239 if (mp == NULL) 22240 return (0); 22241 22242 /* 22243 * We do not have a good algorithm to update ssthresh at this time. 22244 * So don't do any update. 22245 */ 22246 ipic->ipic_rtt = tcp->tcp_rtt_sa; 22247 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 22248 22249 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 22250 22251 return (0); 22252 } 22253 22254 /* ARGSUSED */ 22255 void 22256 tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2) 22257 { 22258 conn_t *connp = (conn_t *)arg; 22259 mblk_t *mp1; 22260 tcp_t *tcp = connp->conn_tcp; 22261 tcp_xmit_reset_event_t *eventp; 22262 22263 ASSERT(mp->b_datap->db_type == M_PROTO && 22264 MBLKL(mp) == sizeof (tcp_xmit_reset_event_t)); 22265 22266 if (tcp->tcp_state != TCPS_LISTEN) { 22267 freemsg(mp); 22268 return; 22269 } 22270 22271 mp1 = mp->b_cont; 22272 mp->b_cont = NULL; 22273 eventp = (tcp_xmit_reset_event_t *)mp->b_rptr; 22274 ASSERT(eventp->tcp_xre_tcps->tcps_netstack == 22275 connp->conn_netstack); 22276 22277 tcp_xmit_listeners_reset(mp1, eventp->tcp_xre_iphdrlen, 22278 eventp->tcp_xre_zoneid, eventp->tcp_xre_tcps, connp); 22279 freemsg(mp); 22280 } 22281 22282 /* 22283 * Generate a "no listener here" RST in response to an "unknown" segment. 22284 * connp is set by caller when RST is in response to an unexpected 22285 * inbound packet for which there is active tcp state in the system. 22286 * Note that we are reusing the incoming mp to construct the outgoing RST. 22287 */ 22288 void 22289 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len, zoneid_t zoneid, 22290 tcp_stack_t *tcps, conn_t *connp) 22291 { 22292 uchar_t *rptr; 22293 uint32_t seg_len; 22294 tcph_t *tcph; 22295 uint32_t seg_seq; 22296 uint32_t seg_ack; 22297 uint_t flags; 22298 mblk_t *ipsec_mp; 22299 ipha_t *ipha; 22300 ip6_t *ip6h; 22301 boolean_t mctl_present = B_FALSE; 22302 boolean_t check = B_TRUE; 22303 boolean_t policy_present; 22304 ipsec_stack_t *ipss = tcps->tcps_netstack->netstack_ipsec; 22305 22306 TCP_STAT(tcps, tcp_no_listener); 22307 22308 ipsec_mp = mp; 22309 22310 if (mp->b_datap->db_type == M_CTL) { 22311 ipsec_in_t *ii; 22312 22313 mctl_present = B_TRUE; 22314 mp = mp->b_cont; 22315 22316 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 22317 ASSERT(ii->ipsec_in_type == IPSEC_IN); 22318 if (ii->ipsec_in_dont_check) { 22319 check = B_FALSE; 22320 if (!ii->ipsec_in_secure) { 22321 freeb(ipsec_mp); 22322 mctl_present = B_FALSE; 22323 ipsec_mp = mp; 22324 } 22325 } 22326 } 22327 22328 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 22329 policy_present = ipss->ipsec_inbound_v4_policy_present; 22330 ipha = (ipha_t *)mp->b_rptr; 22331 ip6h = NULL; 22332 } else { 22333 policy_present = ipss->ipsec_inbound_v6_policy_present; 22334 ipha = NULL; 22335 ip6h = (ip6_t *)mp->b_rptr; 22336 } 22337 22338 if (check && policy_present) { 22339 /* 22340 * The conn_t parameter is NULL because we already know 22341 * nobody's home. 22342 */ 22343 ipsec_mp = ipsec_check_global_policy( 22344 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present, 22345 tcps->tcps_netstack); 22346 if (ipsec_mp == NULL) 22347 return; 22348 } 22349 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 22350 DTRACE_PROBE2( 22351 tx__ip__log__error__nolistener__tcp, 22352 char *, "Could not reply with RST to mp(1)", 22353 mblk_t *, mp); 22354 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 22355 freemsg(ipsec_mp); 22356 return; 22357 } 22358 22359 rptr = mp->b_rptr; 22360 22361 tcph = (tcph_t *)&rptr[ip_hdr_len]; 22362 seg_seq = BE32_TO_U32(tcph->th_seq); 22363 seg_ack = BE32_TO_U32(tcph->th_ack); 22364 flags = tcph->th_flags[0]; 22365 22366 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 22367 if (flags & TH_RST) { 22368 freemsg(ipsec_mp); 22369 } else if (flags & TH_ACK) { 22370 tcp_xmit_early_reset("no tcp, reset", 22371 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len, zoneid, tcps, 22372 connp); 22373 } else { 22374 if (flags & TH_SYN) { 22375 seg_len++; 22376 } else { 22377 /* 22378 * Here we violate the RFC. Note that a normal 22379 * TCP will never send a segment without the ACK 22380 * flag, except for RST or SYN segment. This 22381 * segment is neither. Just drop it on the 22382 * floor. 22383 */ 22384 freemsg(ipsec_mp); 22385 tcps->tcps_rst_unsent++; 22386 return; 22387 } 22388 22389 tcp_xmit_early_reset("no tcp, reset/ack", 22390 ipsec_mp, 0, seg_seq + seg_len, 22391 TH_RST | TH_ACK, ip_hdr_len, zoneid, tcps, connp); 22392 } 22393 } 22394 22395 /* 22396 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 22397 * ip and tcp header ready to pass down to IP. If the mp passed in is 22398 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 22399 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 22400 * otherwise it will dup partial mblks.) 22401 * Otherwise, an appropriate ACK packet will be generated. This 22402 * routine is not usually called to send new data for the first time. It 22403 * is mostly called out of the timer for retransmits, and to generate ACKs. 22404 * 22405 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 22406 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 22407 * of the original mblk chain will be returned in *offset and *end_mp. 22408 */ 22409 mblk_t * 22410 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 22411 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 22412 boolean_t rexmit) 22413 { 22414 int data_length; 22415 int32_t off = 0; 22416 uint_t flags; 22417 mblk_t *mp1; 22418 mblk_t *mp2; 22419 uchar_t *rptr; 22420 tcph_t *tcph; 22421 int32_t num_sack_blk = 0; 22422 int32_t sack_opt_len = 0; 22423 tcp_stack_t *tcps = tcp->tcp_tcps; 22424 22425 /* Allocate for our maximum TCP header + link-level */ 22426 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 22427 tcps->tcps_wroff_xtra, BPRI_MED); 22428 if (!mp1) 22429 return (NULL); 22430 data_length = 0; 22431 22432 /* 22433 * Note that tcp_mss has been adjusted to take into account the 22434 * timestamp option if applicable. Because SACK options do not 22435 * appear in every TCP segments and they are of variable lengths, 22436 * they cannot be included in tcp_mss. Thus we need to calculate 22437 * the actual segment length when we need to send a segment which 22438 * includes SACK options. 22439 */ 22440 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22441 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22442 tcp->tcp_num_sack_blk); 22443 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22444 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22445 if (max_to_send + sack_opt_len > tcp->tcp_mss) 22446 max_to_send -= sack_opt_len; 22447 } 22448 22449 if (offset != NULL) { 22450 off = *offset; 22451 /* We use offset as an indicator that end_mp is not NULL. */ 22452 *end_mp = NULL; 22453 } 22454 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 22455 /* This could be faster with cooperation from downstream */ 22456 if (mp2 != mp1 && !sendall && 22457 data_length + (int)(mp->b_wptr - mp->b_rptr) > 22458 max_to_send) 22459 /* 22460 * Don't send the next mblk since the whole mblk 22461 * does not fit. 22462 */ 22463 break; 22464 mp2->b_cont = dupb(mp); 22465 mp2 = mp2->b_cont; 22466 if (!mp2) { 22467 freemsg(mp1); 22468 return (NULL); 22469 } 22470 mp2->b_rptr += off; 22471 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 22472 (uintptr_t)INT_MAX); 22473 22474 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 22475 if (data_length > max_to_send) { 22476 mp2->b_wptr -= data_length - max_to_send; 22477 data_length = max_to_send; 22478 off = mp2->b_wptr - mp->b_rptr; 22479 break; 22480 } else { 22481 off = 0; 22482 } 22483 } 22484 if (offset != NULL) { 22485 *offset = off; 22486 *end_mp = mp; 22487 } 22488 if (seg_len != NULL) { 22489 *seg_len = data_length; 22490 } 22491 22492 /* Update the latest receive window size in TCP header. */ 22493 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22494 tcp->tcp_tcph->th_win); 22495 22496 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22497 mp1->b_rptr = rptr; 22498 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 22499 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22500 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22501 U32_TO_ABE32(seq, tcph->th_seq); 22502 22503 /* 22504 * Use tcp_unsent to determine if the PUSH bit should be used assumes 22505 * that this function was called from tcp_wput_data. Thus, when called 22506 * to retransmit data the setting of the PUSH bit may appear some 22507 * what random in that it might get set when it should not. This 22508 * should not pose any performance issues. 22509 */ 22510 if (data_length != 0 && (tcp->tcp_unsent == 0 || 22511 tcp->tcp_unsent == data_length)) { 22512 flags = TH_ACK | TH_PUSH; 22513 } else { 22514 flags = TH_ACK; 22515 } 22516 22517 if (tcp->tcp_ecn_ok) { 22518 if (tcp->tcp_ecn_echo_on) 22519 flags |= TH_ECE; 22520 22521 /* 22522 * Only set ECT bit and ECN_CWR if a segment contains new data. 22523 * There is no TCP flow control for non-data segments, and 22524 * only data segment is transmitted reliably. 22525 */ 22526 if (data_length > 0 && !rexmit) { 22527 SET_ECT(tcp, rptr); 22528 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 22529 flags |= TH_CWR; 22530 tcp->tcp_ecn_cwr_sent = B_TRUE; 22531 } 22532 } 22533 } 22534 22535 if (tcp->tcp_valid_bits) { 22536 uint32_t u1; 22537 22538 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 22539 seq == tcp->tcp_iss) { 22540 uchar_t *wptr; 22541 22542 /* 22543 * If TCP_ISS_VALID and the seq number is tcp_iss, 22544 * TCP can only be in SYN-SENT, SYN-RCVD or 22545 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 22546 * our SYN is not ack'ed but the app closes this 22547 * TCP connection. 22548 */ 22549 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 22550 tcp->tcp_state == TCPS_SYN_RCVD || 22551 tcp->tcp_state == TCPS_FIN_WAIT_1); 22552 22553 /* 22554 * Tack on the MSS option. It is always needed 22555 * for both active and passive open. 22556 * 22557 * MSS option value should be interface MTU - MIN 22558 * TCP/IP header according to RFC 793 as it means 22559 * the maximum segment size TCP can receive. But 22560 * to get around some broken middle boxes/end hosts 22561 * out there, we allow the option value to be the 22562 * same as the MSS option size on the peer side. 22563 * In this way, the other side will not send 22564 * anything larger than they can receive. 22565 * 22566 * Note that for SYN_SENT state, the ndd param 22567 * tcp_use_smss_as_mss_opt has no effect as we 22568 * don't know the peer's MSS option value. So 22569 * the only case we need to take care of is in 22570 * SYN_RCVD state, which is done later. 22571 */ 22572 wptr = mp1->b_wptr; 22573 wptr[0] = TCPOPT_MAXSEG; 22574 wptr[1] = TCPOPT_MAXSEG_LEN; 22575 wptr += 2; 22576 u1 = tcp->tcp_if_mtu - 22577 (tcp->tcp_ipversion == IPV4_VERSION ? 22578 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 22579 TCP_MIN_HEADER_LENGTH; 22580 U16_TO_BE16(u1, wptr); 22581 mp1->b_wptr = wptr + 2; 22582 /* Update the offset to cover the additional word */ 22583 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22584 22585 /* 22586 * Note that the following way of filling in 22587 * TCP options are not optimal. Some NOPs can 22588 * be saved. But there is no need at this time 22589 * to optimize it. When it is needed, we will 22590 * do it. 22591 */ 22592 switch (tcp->tcp_state) { 22593 case TCPS_SYN_SENT: 22594 flags = TH_SYN; 22595 22596 if (tcp->tcp_snd_ts_ok) { 22597 uint32_t llbolt = (uint32_t)lbolt; 22598 22599 wptr = mp1->b_wptr; 22600 wptr[0] = TCPOPT_NOP; 22601 wptr[1] = TCPOPT_NOP; 22602 wptr[2] = TCPOPT_TSTAMP; 22603 wptr[3] = TCPOPT_TSTAMP_LEN; 22604 wptr += 4; 22605 U32_TO_BE32(llbolt, wptr); 22606 wptr += 4; 22607 ASSERT(tcp->tcp_ts_recent == 0); 22608 U32_TO_BE32(0L, wptr); 22609 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22610 tcph->th_offset_and_rsrvd[0] += 22611 (3 << 4); 22612 } 22613 22614 /* 22615 * Set up all the bits to tell other side 22616 * we are ECN capable. 22617 */ 22618 if (tcp->tcp_ecn_ok) { 22619 flags |= (TH_ECE | TH_CWR); 22620 } 22621 break; 22622 case TCPS_SYN_RCVD: 22623 flags |= TH_SYN; 22624 22625 /* 22626 * Reset the MSS option value to be SMSS 22627 * We should probably add back the bytes 22628 * for timestamp option and IPsec. We 22629 * don't do that as this is a workaround 22630 * for broken middle boxes/end hosts, it 22631 * is better for us to be more cautious. 22632 * They may not take these things into 22633 * account in their SMSS calculation. Thus 22634 * the peer's calculated SMSS may be smaller 22635 * than what it can be. This should be OK. 22636 */ 22637 if (tcps->tcps_use_smss_as_mss_opt) { 22638 u1 = tcp->tcp_mss; 22639 U16_TO_BE16(u1, wptr); 22640 } 22641 22642 /* 22643 * If the other side is ECN capable, reply 22644 * that we are also ECN capable. 22645 */ 22646 if (tcp->tcp_ecn_ok) 22647 flags |= TH_ECE; 22648 break; 22649 default: 22650 /* 22651 * The above ASSERT() makes sure that this 22652 * must be FIN-WAIT-1 state. Our SYN has 22653 * not been ack'ed so retransmit it. 22654 */ 22655 flags |= TH_SYN; 22656 break; 22657 } 22658 22659 if (tcp->tcp_snd_ws_ok) { 22660 wptr = mp1->b_wptr; 22661 wptr[0] = TCPOPT_NOP; 22662 wptr[1] = TCPOPT_WSCALE; 22663 wptr[2] = TCPOPT_WS_LEN; 22664 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22665 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22666 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22667 } 22668 22669 if (tcp->tcp_snd_sack_ok) { 22670 wptr = mp1->b_wptr; 22671 wptr[0] = TCPOPT_NOP; 22672 wptr[1] = TCPOPT_NOP; 22673 wptr[2] = TCPOPT_SACK_PERMITTED; 22674 wptr[3] = TCPOPT_SACK_OK_LEN; 22675 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22676 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22677 } 22678 22679 /* allocb() of adequate mblk assures space */ 22680 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22681 (uintptr_t)INT_MAX); 22682 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22683 /* 22684 * Get IP set to checksum on our behalf 22685 * Include the adjustment for a source route if any. 22686 */ 22687 u1 += tcp->tcp_sum; 22688 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22689 U16_TO_BE16(u1, tcph->th_sum); 22690 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22691 } 22692 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22693 (seq + data_length) == tcp->tcp_fss) { 22694 if (!tcp->tcp_fin_acked) { 22695 flags |= TH_FIN; 22696 BUMP_MIB(&tcps->tcps_mib, tcpOutControl); 22697 } 22698 if (!tcp->tcp_fin_sent) { 22699 tcp->tcp_fin_sent = B_TRUE; 22700 switch (tcp->tcp_state) { 22701 case TCPS_SYN_RCVD: 22702 case TCPS_ESTABLISHED: 22703 tcp->tcp_state = TCPS_FIN_WAIT_1; 22704 break; 22705 case TCPS_CLOSE_WAIT: 22706 tcp->tcp_state = TCPS_LAST_ACK; 22707 break; 22708 } 22709 if (tcp->tcp_suna == tcp->tcp_snxt) 22710 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22711 tcp->tcp_snxt = tcp->tcp_fss + 1; 22712 } 22713 } 22714 /* 22715 * Note the trick here. u1 is unsigned. When tcp_urg 22716 * is smaller than seq, u1 will become a very huge value. 22717 * So the comparison will fail. Also note that tcp_urp 22718 * should be positive, see RFC 793 page 17. 22719 */ 22720 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22721 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22722 u1 < (uint32_t)(64 * 1024)) { 22723 flags |= TH_URG; 22724 BUMP_MIB(&tcps->tcps_mib, tcpOutUrg); 22725 U32_TO_ABE16(u1, tcph->th_urp); 22726 } 22727 } 22728 tcph->th_flags[0] = (uchar_t)flags; 22729 tcp->tcp_rack = tcp->tcp_rnxt; 22730 tcp->tcp_rack_cnt = 0; 22731 22732 if (tcp->tcp_snd_ts_ok) { 22733 if (tcp->tcp_state != TCPS_SYN_SENT) { 22734 uint32_t llbolt = (uint32_t)lbolt; 22735 22736 U32_TO_BE32(llbolt, 22737 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22738 U32_TO_BE32(tcp->tcp_ts_recent, 22739 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22740 } 22741 } 22742 22743 if (num_sack_blk > 0) { 22744 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22745 sack_blk_t *tmp; 22746 int32_t i; 22747 22748 wptr[0] = TCPOPT_NOP; 22749 wptr[1] = TCPOPT_NOP; 22750 wptr[2] = TCPOPT_SACK; 22751 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22752 sizeof (sack_blk_t); 22753 wptr += TCPOPT_REAL_SACK_LEN; 22754 22755 tmp = tcp->tcp_sack_list; 22756 for (i = 0; i < num_sack_blk; i++) { 22757 U32_TO_BE32(tmp[i].begin, wptr); 22758 wptr += sizeof (tcp_seq); 22759 U32_TO_BE32(tmp[i].end, wptr); 22760 wptr += sizeof (tcp_seq); 22761 } 22762 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22763 } 22764 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22765 data_length += (int)(mp1->b_wptr - rptr); 22766 if (tcp->tcp_ipversion == IPV4_VERSION) { 22767 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22768 } else { 22769 ip6_t *ip6 = (ip6_t *)(rptr + 22770 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22771 sizeof (ip6i_t) : 0)); 22772 22773 ip6->ip6_plen = htons(data_length - 22774 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22775 } 22776 22777 /* 22778 * Prime pump for IP 22779 * Include the adjustment for a source route if any. 22780 */ 22781 data_length -= tcp->tcp_ip_hdr_len; 22782 data_length += tcp->tcp_sum; 22783 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22784 U16_TO_ABE16(data_length, tcph->th_sum); 22785 if (tcp->tcp_ip_forward_progress) { 22786 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22787 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22788 tcp->tcp_ip_forward_progress = B_FALSE; 22789 } 22790 return (mp1); 22791 } 22792 22793 /* This function handles the push timeout. */ 22794 void 22795 tcp_push_timer(void *arg) 22796 { 22797 conn_t *connp = (conn_t *)arg; 22798 tcp_t *tcp = connp->conn_tcp; 22799 22800 TCP_DBGSTAT(tcp->tcp_tcps, tcp_push_timer_cnt); 22801 22802 ASSERT(tcp->tcp_listener == NULL); 22803 22804 ASSERT(!IPCL_IS_NONSTR(connp)); 22805 22806 /* 22807 * We need to plug synchronous streams during our drain to prevent 22808 * a race with tcp_fuse_rrw() or tcp_fusion_rinfop(). 22809 */ 22810 TCP_FUSE_SYNCSTR_PLUG_DRAIN(tcp); 22811 tcp->tcp_push_tid = 0; 22812 22813 if (tcp->tcp_rcv_list != NULL && 22814 tcp_rcv_drain(tcp) == TH_ACK_NEEDED) 22815 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22816 22817 TCP_FUSE_SYNCSTR_UNPLUG_DRAIN(tcp); 22818 } 22819 22820 /* 22821 * This function handles delayed ACK timeout. 22822 */ 22823 static void 22824 tcp_ack_timer(void *arg) 22825 { 22826 conn_t *connp = (conn_t *)arg; 22827 tcp_t *tcp = connp->conn_tcp; 22828 mblk_t *mp; 22829 tcp_stack_t *tcps = tcp->tcp_tcps; 22830 22831 TCP_DBGSTAT(tcps, tcp_ack_timer_cnt); 22832 22833 tcp->tcp_ack_tid = 0; 22834 22835 if (tcp->tcp_fused) 22836 return; 22837 22838 /* 22839 * Do not send ACK if there is no outstanding unack'ed data. 22840 */ 22841 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22842 return; 22843 } 22844 22845 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22846 /* 22847 * Make sure we don't allow deferred ACKs to result in 22848 * timer-based ACKing. If we have held off an ACK 22849 * when there was more than an mss here, and the timer 22850 * goes off, we have to worry about the possibility 22851 * that the sender isn't doing slow-start, or is out 22852 * of step with us for some other reason. We fall 22853 * permanently back in the direction of 22854 * ACK-every-other-packet as suggested in RFC 1122. 22855 */ 22856 if (tcp->tcp_rack_abs_max > 2) 22857 tcp->tcp_rack_abs_max--; 22858 tcp->tcp_rack_cur_max = 2; 22859 } 22860 mp = tcp_ack_mp(tcp); 22861 22862 if (mp != NULL) { 22863 BUMP_LOCAL(tcp->tcp_obsegs); 22864 BUMP_MIB(&tcps->tcps_mib, tcpOutAck); 22865 BUMP_MIB(&tcps->tcps_mib, tcpOutAckDelayed); 22866 tcp_send_data(tcp, tcp->tcp_wq, mp); 22867 } 22868 } 22869 22870 22871 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22872 static mblk_t * 22873 tcp_ack_mp(tcp_t *tcp) 22874 { 22875 uint32_t seq_no; 22876 tcp_stack_t *tcps = tcp->tcp_tcps; 22877 22878 /* 22879 * There are a few cases to be considered while setting the sequence no. 22880 * Essentially, we can come here while processing an unacceptable pkt 22881 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22882 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22883 * If we are here for a zero window probe, stick with suna. In all 22884 * other cases, we check if suna + swnd encompasses snxt and set 22885 * the sequence number to snxt, if so. If snxt falls outside the 22886 * window (the receiver probably shrunk its window), we will go with 22887 * suna + swnd, otherwise the sequence no will be unacceptable to the 22888 * receiver. 22889 */ 22890 if (tcp->tcp_zero_win_probe) { 22891 seq_no = tcp->tcp_suna; 22892 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22893 ASSERT(tcp->tcp_swnd == 0); 22894 seq_no = tcp->tcp_snxt; 22895 } else { 22896 seq_no = SEQ_GT(tcp->tcp_snxt, 22897 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22898 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22899 } 22900 22901 if (tcp->tcp_valid_bits) { 22902 /* 22903 * For the complex case where we have to send some 22904 * controls (FIN or SYN), let tcp_xmit_mp do it. 22905 */ 22906 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22907 NULL, B_FALSE)); 22908 } else { 22909 /* Generate a simple ACK */ 22910 int data_length; 22911 uchar_t *rptr; 22912 tcph_t *tcph; 22913 mblk_t *mp1; 22914 int32_t tcp_hdr_len; 22915 int32_t tcp_tcp_hdr_len; 22916 int32_t num_sack_blk = 0; 22917 int32_t sack_opt_len; 22918 22919 /* 22920 * Allocate space for TCP + IP headers 22921 * and link-level header 22922 */ 22923 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22924 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22925 tcp->tcp_num_sack_blk); 22926 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22927 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22928 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22929 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22930 } else { 22931 tcp_hdr_len = tcp->tcp_hdr_len; 22932 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22933 } 22934 mp1 = allocb(tcp_hdr_len + tcps->tcps_wroff_xtra, BPRI_MED); 22935 if (!mp1) 22936 return (NULL); 22937 22938 /* Update the latest receive window size in TCP header. */ 22939 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22940 tcp->tcp_tcph->th_win); 22941 /* copy in prototype TCP + IP header */ 22942 rptr = mp1->b_rptr + tcps->tcps_wroff_xtra; 22943 mp1->b_rptr = rptr; 22944 mp1->b_wptr = rptr + tcp_hdr_len; 22945 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22946 22947 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22948 22949 /* Set the TCP sequence number. */ 22950 U32_TO_ABE32(seq_no, tcph->th_seq); 22951 22952 /* Set up the TCP flag field. */ 22953 tcph->th_flags[0] = (uchar_t)TH_ACK; 22954 if (tcp->tcp_ecn_echo_on) 22955 tcph->th_flags[0] |= TH_ECE; 22956 22957 tcp->tcp_rack = tcp->tcp_rnxt; 22958 tcp->tcp_rack_cnt = 0; 22959 22960 /* fill in timestamp option if in use */ 22961 if (tcp->tcp_snd_ts_ok) { 22962 uint32_t llbolt = (uint32_t)lbolt; 22963 22964 U32_TO_BE32(llbolt, 22965 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22966 U32_TO_BE32(tcp->tcp_ts_recent, 22967 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22968 } 22969 22970 /* Fill in SACK options */ 22971 if (num_sack_blk > 0) { 22972 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22973 sack_blk_t *tmp; 22974 int32_t i; 22975 22976 wptr[0] = TCPOPT_NOP; 22977 wptr[1] = TCPOPT_NOP; 22978 wptr[2] = TCPOPT_SACK; 22979 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22980 sizeof (sack_blk_t); 22981 wptr += TCPOPT_REAL_SACK_LEN; 22982 22983 tmp = tcp->tcp_sack_list; 22984 for (i = 0; i < num_sack_blk; i++) { 22985 U32_TO_BE32(tmp[i].begin, wptr); 22986 wptr += sizeof (tcp_seq); 22987 U32_TO_BE32(tmp[i].end, wptr); 22988 wptr += sizeof (tcp_seq); 22989 } 22990 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22991 << 4); 22992 } 22993 22994 if (tcp->tcp_ipversion == IPV4_VERSION) { 22995 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22996 } else { 22997 /* Check for ip6i_t header in sticky hdrs */ 22998 ip6_t *ip6 = (ip6_t *)(rptr + 22999 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 23000 sizeof (ip6i_t) : 0)); 23001 23002 ip6->ip6_plen = htons(tcp_hdr_len - 23003 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 23004 } 23005 23006 /* 23007 * Prime pump for checksum calculation in IP. Include the 23008 * adjustment for a source route if any. 23009 */ 23010 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 23011 data_length = (data_length >> 16) + (data_length & 0xFFFF); 23012 U16_TO_ABE16(data_length, tcph->th_sum); 23013 23014 if (tcp->tcp_ip_forward_progress) { 23015 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 23016 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 23017 tcp->tcp_ip_forward_progress = B_FALSE; 23018 } 23019 return (mp1); 23020 } 23021 } 23022 23023 /* 23024 * Hash list insertion routine for tcp_t structures. Each hash bucket 23025 * contains a list of tcp_t entries, and each entry is bound to a unique 23026 * port. If there are multiple tcp_t's that are bound to the same port, then 23027 * one of them will be linked into the hash bucket list, and the rest will 23028 * hang off of that one entry. For each port, entries bound to a specific IP 23029 * address will be inserted before those those bound to INADDR_ANY. 23030 */ 23031 static void 23032 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 23033 { 23034 tcp_t **tcpp; 23035 tcp_t *tcpnext; 23036 tcp_t *tcphash; 23037 23038 if (tcp->tcp_ptpbhn != NULL) { 23039 ASSERT(!caller_holds_lock); 23040 tcp_bind_hash_remove(tcp); 23041 } 23042 tcpp = &tbf->tf_tcp; 23043 if (!caller_holds_lock) { 23044 mutex_enter(&tbf->tf_lock); 23045 } else { 23046 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 23047 } 23048 tcphash = tcpp[0]; 23049 tcpnext = NULL; 23050 if (tcphash != NULL) { 23051 /* Look for an entry using the same port */ 23052 while ((tcphash = tcpp[0]) != NULL && 23053 tcp->tcp_lport != tcphash->tcp_lport) 23054 tcpp = &(tcphash->tcp_bind_hash); 23055 23056 /* The port was not found, just add to the end */ 23057 if (tcphash == NULL) 23058 goto insert; 23059 23060 /* 23061 * OK, there already exists an entry bound to the 23062 * same port. 23063 * 23064 * If the new tcp bound to the INADDR_ANY address 23065 * and the first one in the list is not bound to 23066 * INADDR_ANY we skip all entries until we find the 23067 * first one bound to INADDR_ANY. 23068 * This makes sure that applications binding to a 23069 * specific address get preference over those binding to 23070 * INADDR_ANY. 23071 */ 23072 tcpnext = tcphash; 23073 tcphash = NULL; 23074 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 23075 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 23076 while ((tcpnext = tcpp[0]) != NULL && 23077 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 23078 tcpp = &(tcpnext->tcp_bind_hash_port); 23079 23080 if (tcpnext) { 23081 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23082 tcphash = tcpnext->tcp_bind_hash; 23083 if (tcphash != NULL) { 23084 tcphash->tcp_ptpbhn = 23085 &(tcp->tcp_bind_hash); 23086 tcpnext->tcp_bind_hash = NULL; 23087 } 23088 } 23089 } else { 23090 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash_port; 23091 tcphash = tcpnext->tcp_bind_hash; 23092 if (tcphash != NULL) { 23093 tcphash->tcp_ptpbhn = 23094 &(tcp->tcp_bind_hash); 23095 tcpnext->tcp_bind_hash = NULL; 23096 } 23097 } 23098 } 23099 insert: 23100 tcp->tcp_bind_hash_port = tcpnext; 23101 tcp->tcp_bind_hash = tcphash; 23102 tcp->tcp_ptpbhn = tcpp; 23103 tcpp[0] = tcp; 23104 if (!caller_holds_lock) 23105 mutex_exit(&tbf->tf_lock); 23106 } 23107 23108 /* 23109 * Hash list removal routine for tcp_t structures. 23110 */ 23111 static void 23112 tcp_bind_hash_remove(tcp_t *tcp) 23113 { 23114 tcp_t *tcpnext; 23115 kmutex_t *lockp; 23116 tcp_stack_t *tcps = tcp->tcp_tcps; 23117 23118 if (tcp->tcp_ptpbhn == NULL) 23119 return; 23120 23121 /* 23122 * Extract the lock pointer in case there are concurrent 23123 * hash_remove's for this instance. 23124 */ 23125 ASSERT(tcp->tcp_lport != 0); 23126 lockp = &tcps->tcps_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 23127 23128 ASSERT(lockp != NULL); 23129 mutex_enter(lockp); 23130 if (tcp->tcp_ptpbhn) { 23131 tcpnext = tcp->tcp_bind_hash_port; 23132 if (tcpnext != NULL) { 23133 tcp->tcp_bind_hash_port = NULL; 23134 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23135 tcpnext->tcp_bind_hash = tcp->tcp_bind_hash; 23136 if (tcpnext->tcp_bind_hash != NULL) { 23137 tcpnext->tcp_bind_hash->tcp_ptpbhn = 23138 &(tcpnext->tcp_bind_hash); 23139 tcp->tcp_bind_hash = NULL; 23140 } 23141 } else if ((tcpnext = tcp->tcp_bind_hash) != NULL) { 23142 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 23143 tcp->tcp_bind_hash = NULL; 23144 } 23145 *tcp->tcp_ptpbhn = tcpnext; 23146 tcp->tcp_ptpbhn = NULL; 23147 } 23148 mutex_exit(lockp); 23149 } 23150 23151 23152 /* 23153 * Hash list lookup routine for tcp_t structures. 23154 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 23155 */ 23156 static tcp_t * 23157 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps) 23158 { 23159 tf_t *tf; 23160 tcp_t *tcp; 23161 23162 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23163 mutex_enter(&tf->tf_lock); 23164 for (tcp = tf->tf_tcp; tcp != NULL; 23165 tcp = tcp->tcp_acceptor_hash) { 23166 if (tcp->tcp_acceptor_id == id) { 23167 CONN_INC_REF(tcp->tcp_connp); 23168 mutex_exit(&tf->tf_lock); 23169 return (tcp); 23170 } 23171 } 23172 mutex_exit(&tf->tf_lock); 23173 return (NULL); 23174 } 23175 23176 23177 /* 23178 * Hash list insertion routine for tcp_t structures. 23179 */ 23180 void 23181 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 23182 { 23183 tf_t *tf; 23184 tcp_t **tcpp; 23185 tcp_t *tcpnext; 23186 tcp_stack_t *tcps = tcp->tcp_tcps; 23187 23188 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 23189 23190 if (tcp->tcp_ptpahn != NULL) 23191 tcp_acceptor_hash_remove(tcp); 23192 tcpp = &tf->tf_tcp; 23193 mutex_enter(&tf->tf_lock); 23194 tcpnext = tcpp[0]; 23195 if (tcpnext) 23196 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 23197 tcp->tcp_acceptor_hash = tcpnext; 23198 tcp->tcp_ptpahn = tcpp; 23199 tcpp[0] = tcp; 23200 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 23201 mutex_exit(&tf->tf_lock); 23202 } 23203 23204 /* 23205 * Hash list removal routine for tcp_t structures. 23206 */ 23207 static void 23208 tcp_acceptor_hash_remove(tcp_t *tcp) 23209 { 23210 tcp_t *tcpnext; 23211 kmutex_t *lockp; 23212 23213 /* 23214 * Extract the lock pointer in case there are concurrent 23215 * hash_remove's for this instance. 23216 */ 23217 lockp = tcp->tcp_acceptor_lockp; 23218 23219 if (tcp->tcp_ptpahn == NULL) 23220 return; 23221 23222 ASSERT(lockp != NULL); 23223 mutex_enter(lockp); 23224 if (tcp->tcp_ptpahn) { 23225 tcpnext = tcp->tcp_acceptor_hash; 23226 if (tcpnext) { 23227 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 23228 tcp->tcp_acceptor_hash = NULL; 23229 } 23230 *tcp->tcp_ptpahn = tcpnext; 23231 tcp->tcp_ptpahn = NULL; 23232 } 23233 mutex_exit(lockp); 23234 tcp->tcp_acceptor_lockp = NULL; 23235 } 23236 23237 /* 23238 * Type three generator adapted from the random() function in 4.4 BSD: 23239 */ 23240 23241 /* 23242 * Copyright (c) 1983, 1993 23243 * The Regents of the University of California. All rights reserved. 23244 * 23245 * Redistribution and use in source and binary forms, with or without 23246 * modification, are permitted provided that the following conditions 23247 * are met: 23248 * 1. Redistributions of source code must retain the above copyright 23249 * notice, this list of conditions and the following disclaimer. 23250 * 2. Redistributions in binary form must reproduce the above copyright 23251 * notice, this list of conditions and the following disclaimer in the 23252 * documentation and/or other materials provided with the distribution. 23253 * 3. All advertising materials mentioning features or use of this software 23254 * must display the following acknowledgement: 23255 * This product includes software developed by the University of 23256 * California, Berkeley and its contributors. 23257 * 4. Neither the name of the University nor the names of its contributors 23258 * may be used to endorse or promote products derived from this software 23259 * without specific prior written permission. 23260 * 23261 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23262 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23263 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23264 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23265 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23266 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23267 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23268 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23269 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23270 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23271 * SUCH DAMAGE. 23272 */ 23273 23274 /* Type 3 -- x**31 + x**3 + 1 */ 23275 #define DEG_3 31 23276 #define SEP_3 3 23277 23278 23279 /* Protected by tcp_random_lock */ 23280 static int tcp_randtbl[DEG_3 + 1]; 23281 23282 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23283 static int *tcp_random_rptr = &tcp_randtbl[1]; 23284 23285 static int *tcp_random_state = &tcp_randtbl[1]; 23286 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23287 23288 kmutex_t tcp_random_lock; 23289 23290 void 23291 tcp_random_init(void) 23292 { 23293 int i; 23294 hrtime_t hrt; 23295 time_t wallclock; 23296 uint64_t result; 23297 23298 /* 23299 * Use high-res timer and current time for seed. Gethrtime() returns 23300 * a longlong, which may contain resolution down to nanoseconds. 23301 * The current time will either be a 32-bit or a 64-bit quantity. 23302 * XOR the two together in a 64-bit result variable. 23303 * Convert the result to a 32-bit value by multiplying the high-order 23304 * 32-bits by the low-order 32-bits. 23305 */ 23306 23307 hrt = gethrtime(); 23308 (void) drv_getparm(TIME, &wallclock); 23309 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23310 mutex_enter(&tcp_random_lock); 23311 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23312 (result & 0xffffffff); 23313 23314 for (i = 1; i < DEG_3; i++) 23315 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23316 + 12345; 23317 tcp_random_fptr = &tcp_random_state[SEP_3]; 23318 tcp_random_rptr = &tcp_random_state[0]; 23319 mutex_exit(&tcp_random_lock); 23320 for (i = 0; i < 10 * DEG_3; i++) 23321 (void) tcp_random(); 23322 } 23323 23324 /* 23325 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23326 * This range is selected to be approximately centered on TCP_ISS / 2, 23327 * and easy to compute. We get this value by generating a 32-bit random 23328 * number, selecting out the high-order 17 bits, and then adding one so 23329 * that we never return zero. 23330 */ 23331 int 23332 tcp_random(void) 23333 { 23334 int i; 23335 23336 mutex_enter(&tcp_random_lock); 23337 *tcp_random_fptr += *tcp_random_rptr; 23338 23339 /* 23340 * The high-order bits are more random than the low-order bits, 23341 * so we select out the high-order 17 bits and add one so that 23342 * we never return zero. 23343 */ 23344 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23345 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23346 tcp_random_fptr = tcp_random_state; 23347 ++tcp_random_rptr; 23348 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23349 tcp_random_rptr = tcp_random_state; 23350 23351 mutex_exit(&tcp_random_lock); 23352 return (i); 23353 } 23354 23355 static int 23356 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23357 int *t_errorp, int *sys_errorp) 23358 { 23359 int error; 23360 int is_absreq_failure; 23361 t_scalar_t *opt_lenp; 23362 t_scalar_t opt_offset; 23363 int prim_type; 23364 struct T_conn_req *tcreqp; 23365 struct T_conn_res *tcresp; 23366 cred_t *cr; 23367 23368 /* 23369 * All Solaris components should pass a db_credp 23370 * for this TPI message, hence we ASSERT. 23371 * But in case there is some other M_PROTO that looks 23372 * like a TPI message sent by some other kernel 23373 * component, we check and return an error. 23374 */ 23375 cr = msg_getcred(mp, NULL); 23376 ASSERT(cr != NULL); 23377 if (cr == NULL) 23378 return (-1); 23379 23380 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23381 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23382 prim_type == T_CONN_RES); 23383 23384 switch (prim_type) { 23385 case T_CONN_REQ: 23386 tcreqp = (struct T_conn_req *)mp->b_rptr; 23387 opt_offset = tcreqp->OPT_offset; 23388 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23389 break; 23390 case O_T_CONN_RES: 23391 case T_CONN_RES: 23392 tcresp = (struct T_conn_res *)mp->b_rptr; 23393 opt_offset = tcresp->OPT_offset; 23394 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23395 break; 23396 } 23397 23398 *t_errorp = 0; 23399 *sys_errorp = 0; 23400 *do_disconnectp = 0; 23401 23402 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23403 opt_offset, cr, &tcp_opt_obj, 23404 NULL, &is_absreq_failure); 23405 23406 switch (error) { 23407 case 0: /* no error */ 23408 ASSERT(is_absreq_failure == 0); 23409 return (0); 23410 case ENOPROTOOPT: 23411 *t_errorp = TBADOPT; 23412 break; 23413 case EACCES: 23414 *t_errorp = TACCES; 23415 break; 23416 default: 23417 *t_errorp = TSYSERR; *sys_errorp = error; 23418 break; 23419 } 23420 if (is_absreq_failure != 0) { 23421 /* 23422 * The connection request should get the local ack 23423 * T_OK_ACK and then a T_DISCON_IND. 23424 */ 23425 *do_disconnectp = 1; 23426 } 23427 return (-1); 23428 } 23429 23430 /* 23431 * Split this function out so that if the secret changes, I'm okay. 23432 * 23433 * Initialize the tcp_iss_cookie and tcp_iss_key. 23434 */ 23435 23436 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23437 23438 static void 23439 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps) 23440 { 23441 struct { 23442 int32_t current_time; 23443 uint32_t randnum; 23444 uint16_t pad; 23445 uint8_t ether[6]; 23446 uint8_t passwd[PASSWD_SIZE]; 23447 } tcp_iss_cookie; 23448 time_t t; 23449 23450 /* 23451 * Start with the current absolute time. 23452 */ 23453 (void) drv_getparm(TIME, &t); 23454 tcp_iss_cookie.current_time = t; 23455 23456 /* 23457 * XXX - Need a more random number per RFC 1750, not this crap. 23458 * OTOH, if what follows is pretty random, then I'm in better shape. 23459 */ 23460 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23461 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23462 23463 /* 23464 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23465 * as a good template. 23466 */ 23467 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23468 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23469 23470 /* 23471 * The pass-phrase. Normally this is supplied by user-called NDD. 23472 */ 23473 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23474 23475 /* 23476 * See 4010593 if this section becomes a problem again, 23477 * but the local ethernet address is useful here. 23478 */ 23479 (void) localetheraddr(NULL, 23480 (struct ether_addr *)&tcp_iss_cookie.ether); 23481 23482 /* 23483 * Hash 'em all together. The MD5Final is called per-connection. 23484 */ 23485 mutex_enter(&tcps->tcps_iss_key_lock); 23486 MD5Init(&tcps->tcps_iss_key); 23487 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie, 23488 sizeof (tcp_iss_cookie)); 23489 mutex_exit(&tcps->tcps_iss_key_lock); 23490 } 23491 23492 /* 23493 * Set the RFC 1948 pass phrase 23494 */ 23495 /* ARGSUSED */ 23496 static int 23497 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23498 cred_t *cr) 23499 { 23500 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps; 23501 23502 /* 23503 * Basically, value contains a new pass phrase. Pass it along! 23504 */ 23505 tcp_iss_key_init((uint8_t *)value, strlen(value), tcps); 23506 return (0); 23507 } 23508 23509 /* ARGSUSED */ 23510 static int 23511 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23512 { 23513 bzero(buf, sizeof (tcp_sack_info_t)); 23514 return (0); 23515 } 23516 23517 /* ARGSUSED */ 23518 static int 23519 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23520 { 23521 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23522 return (0); 23523 } 23524 23525 /* 23526 * Make sure we wait until the default queue is setup, yet allow 23527 * tcp_g_q_create() to open a TCP stream. 23528 * We need to allow tcp_g_q_create() do do an open 23529 * of tcp, hence we compare curhread. 23530 * All others have to wait until the tcps_g_q has been 23531 * setup. 23532 */ 23533 void 23534 tcp_g_q_setup(tcp_stack_t *tcps) 23535 { 23536 mutex_enter(&tcps->tcps_g_q_lock); 23537 if (tcps->tcps_g_q != NULL) { 23538 mutex_exit(&tcps->tcps_g_q_lock); 23539 return; 23540 } 23541 if (tcps->tcps_g_q_creator == NULL) { 23542 /* This thread will set it up */ 23543 tcps->tcps_g_q_creator = curthread; 23544 mutex_exit(&tcps->tcps_g_q_lock); 23545 tcp_g_q_create(tcps); 23546 mutex_enter(&tcps->tcps_g_q_lock); 23547 ASSERT(tcps->tcps_g_q_creator == curthread); 23548 tcps->tcps_g_q_creator = NULL; 23549 cv_signal(&tcps->tcps_g_q_cv); 23550 ASSERT(tcps->tcps_g_q != NULL); 23551 mutex_exit(&tcps->tcps_g_q_lock); 23552 return; 23553 } 23554 /* Everybody but the creator has to wait */ 23555 if (tcps->tcps_g_q_creator != curthread) { 23556 while (tcps->tcps_g_q == NULL) 23557 cv_wait(&tcps->tcps_g_q_cv, &tcps->tcps_g_q_lock); 23558 } 23559 mutex_exit(&tcps->tcps_g_q_lock); 23560 } 23561 23562 #define IP "ip" 23563 23564 #define TCP6DEV "/devices/pseudo/tcp6@0:tcp6" 23565 23566 /* 23567 * Create a default tcp queue here instead of in strplumb 23568 */ 23569 void 23570 tcp_g_q_create(tcp_stack_t *tcps) 23571 { 23572 int error; 23573 ldi_handle_t lh = NULL; 23574 ldi_ident_t li = NULL; 23575 int rval; 23576 cred_t *cr; 23577 major_t IP_MAJ; 23578 23579 #ifdef NS_DEBUG 23580 (void) printf("tcp_g_q_create()\n"); 23581 #endif 23582 23583 IP_MAJ = ddi_name_to_major(IP); 23584 23585 ASSERT(tcps->tcps_g_q_creator == curthread); 23586 23587 error = ldi_ident_from_major(IP_MAJ, &li); 23588 if (error) { 23589 #ifdef DEBUG 23590 printf("tcp_g_q_create: lyr ident get failed error %d\n", 23591 error); 23592 #endif 23593 return; 23594 } 23595 23596 cr = zone_get_kcred(netstackid_to_zoneid( 23597 tcps->tcps_netstack->netstack_stackid)); 23598 ASSERT(cr != NULL); 23599 /* 23600 * We set the tcp default queue to IPv6 because IPv4 falls 23601 * back to IPv6 when it can't find a client, but 23602 * IPv6 does not fall back to IPv4. 23603 */ 23604 error = ldi_open_by_name(TCP6DEV, FREAD|FWRITE, cr, &lh, li); 23605 if (error) { 23606 #ifdef DEBUG 23607 printf("tcp_g_q_create: open of TCP6DEV failed error %d\n", 23608 error); 23609 #endif 23610 goto out; 23611 } 23612 23613 /* 23614 * This ioctl causes the tcp framework to cache a pointer to 23615 * this stream, so we don't want to close the stream after 23616 * this operation. 23617 * Use the kernel credentials that are for the zone we're in. 23618 */ 23619 error = ldi_ioctl(lh, TCP_IOC_DEFAULT_Q, 23620 (intptr_t)0, FKIOCTL, cr, &rval); 23621 if (error) { 23622 #ifdef DEBUG 23623 printf("tcp_g_q_create: ioctl TCP_IOC_DEFAULT_Q failed " 23624 "error %d\n", error); 23625 #endif 23626 goto out; 23627 } 23628 tcps->tcps_g_q_lh = lh; /* For tcp_g_q_close */ 23629 lh = NULL; 23630 out: 23631 /* Close layered handles */ 23632 if (li) 23633 ldi_ident_release(li); 23634 /* Keep cred around until _inactive needs it */ 23635 tcps->tcps_g_q_cr = cr; 23636 } 23637 23638 /* 23639 * We keep tcp_g_q set until all other tcp_t's in the zone 23640 * has gone away, and then when tcp_g_q_inactive() is called 23641 * we clear it. 23642 */ 23643 void 23644 tcp_g_q_destroy(tcp_stack_t *tcps) 23645 { 23646 #ifdef NS_DEBUG 23647 (void) printf("tcp_g_q_destroy()for stack %d\n", 23648 tcps->tcps_netstack->netstack_stackid); 23649 #endif 23650 23651 if (tcps->tcps_g_q == NULL) { 23652 return; /* Nothing to cleanup */ 23653 } 23654 /* 23655 * Drop reference corresponding to the default queue. 23656 * This reference was added from tcp_open when the default queue 23657 * was created, hence we compensate for this extra drop in 23658 * tcp_g_q_close. If the refcnt drops to zero here it means 23659 * the default queue was the last one to be open, in which 23660 * case, then tcp_g_q_inactive will be 23661 * called as a result of the refrele. 23662 */ 23663 TCPS_REFRELE(tcps); 23664 } 23665 23666 /* 23667 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23668 * Run by tcp_q_q_inactive using a taskq. 23669 */ 23670 static void 23671 tcp_g_q_close(void *arg) 23672 { 23673 tcp_stack_t *tcps = arg; 23674 int error; 23675 ldi_handle_t lh = NULL; 23676 ldi_ident_t li = NULL; 23677 cred_t *cr; 23678 major_t IP_MAJ; 23679 23680 IP_MAJ = ddi_name_to_major(IP); 23681 23682 #ifdef NS_DEBUG 23683 (void) printf("tcp_g_q_inactive() for stack %d refcnt %d\n", 23684 tcps->tcps_netstack->netstack_stackid, 23685 tcps->tcps_netstack->netstack_refcnt); 23686 #endif 23687 lh = tcps->tcps_g_q_lh; 23688 if (lh == NULL) 23689 return; /* Nothing to cleanup */ 23690 23691 ASSERT(tcps->tcps_refcnt == 1); 23692 ASSERT(tcps->tcps_g_q != NULL); 23693 23694 error = ldi_ident_from_major(IP_MAJ, &li); 23695 if (error) { 23696 #ifdef DEBUG 23697 printf("tcp_g_q_inactive: lyr ident get failed error %d\n", 23698 error); 23699 #endif 23700 return; 23701 } 23702 23703 cr = tcps->tcps_g_q_cr; 23704 tcps->tcps_g_q_cr = NULL; 23705 ASSERT(cr != NULL); 23706 23707 /* 23708 * Make sure we can break the recursion when tcp_close decrements 23709 * the reference count causing g_q_inactive to be called again. 23710 */ 23711 tcps->tcps_g_q_lh = NULL; 23712 23713 /* close the default queue */ 23714 (void) ldi_close(lh, FREAD|FWRITE, cr); 23715 /* 23716 * At this point in time tcps and the rest of netstack_t might 23717 * have been deleted. 23718 */ 23719 tcps = NULL; 23720 23721 /* Close layered handles */ 23722 ldi_ident_release(li); 23723 crfree(cr); 23724 } 23725 23726 /* 23727 * Called when last tcp_t drops reference count using TCPS_REFRELE. 23728 * 23729 * Have to ensure that the ldi routines are not used by an 23730 * interrupt thread by using a taskq. 23731 */ 23732 void 23733 tcp_g_q_inactive(tcp_stack_t *tcps) 23734 { 23735 if (tcps->tcps_g_q_lh == NULL) 23736 return; /* Nothing to cleanup */ 23737 23738 ASSERT(tcps->tcps_refcnt == 0); 23739 TCPS_REFHOLD(tcps); /* Compensate for what g_q_destroy did */ 23740 23741 if (servicing_interrupt()) { 23742 (void) taskq_dispatch(tcp_taskq, tcp_g_q_close, 23743 (void *) tcps, TQ_SLEEP); 23744 } else { 23745 tcp_g_q_close(tcps); 23746 } 23747 } 23748 23749 /* 23750 * Called by IP when IP is loaded into the kernel 23751 */ 23752 void 23753 tcp_ddi_g_init(void) 23754 { 23755 tcp_timercache = kmem_cache_create("tcp_timercache", 23756 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23757 NULL, NULL, NULL, NULL, NULL, 0); 23758 23759 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23760 sizeof (tcp_sack_info_t), 0, 23761 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23762 23763 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23764 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23765 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23766 23767 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23768 23769 /* Initialize the random number generator */ 23770 tcp_random_init(); 23771 23772 /* A single callback independently of how many netstacks we have */ 23773 ip_squeue_init(tcp_squeue_add); 23774 23775 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics); 23776 23777 tcp_taskq = taskq_create("tcp_taskq", 1, minclsyspri, 1, 1, 23778 TASKQ_PREPOPULATE); 23779 23780 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput); 23781 23782 /* 23783 * We want to be informed each time a stack is created or 23784 * destroyed in the kernel, so we can maintain the 23785 * set of tcp_stack_t's. 23786 */ 23787 netstack_register(NS_TCP, tcp_stack_init, tcp_stack_shutdown, 23788 tcp_stack_fini); 23789 } 23790 23791 23792 #define INET_NAME "ip" 23793 23794 /* 23795 * Initialize the TCP stack instance. 23796 */ 23797 static void * 23798 tcp_stack_init(netstackid_t stackid, netstack_t *ns) 23799 { 23800 tcp_stack_t *tcps; 23801 tcpparam_t *pa; 23802 int i; 23803 int error = 0; 23804 major_t major; 23805 23806 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP); 23807 tcps->tcps_netstack = ns; 23808 23809 /* Initialize locks */ 23810 mutex_init(&tcps->tcps_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23811 cv_init(&tcps->tcps_g_q_cv, NULL, CV_DEFAULT, NULL); 23812 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23813 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23814 23815 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 23816 tcps->tcps_g_epriv_ports[0] = 2049; 23817 tcps->tcps_g_epriv_ports[1] = 4045; 23818 tcps->tcps_min_anonpriv_port = 512; 23819 23820 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) * 23821 TCP_BIND_FANOUT_SIZE, KM_SLEEP); 23822 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) * 23823 TCP_FANOUT_SIZE, KM_SLEEP); 23824 23825 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23826 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL, 23827 MUTEX_DEFAULT, NULL); 23828 } 23829 23830 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23831 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL, 23832 MUTEX_DEFAULT, NULL); 23833 } 23834 23835 /* TCP's IPsec code calls the packet dropper. */ 23836 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement"); 23837 23838 pa = (tcpparam_t *)kmem_alloc(sizeof (lcl_tcp_param_arr), KM_SLEEP); 23839 tcps->tcps_params = pa; 23840 bcopy(lcl_tcp_param_arr, tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23841 23842 (void) tcp_param_register(&tcps->tcps_g_nd, tcps->tcps_params, 23843 A_CNT(lcl_tcp_param_arr), tcps); 23844 23845 /* 23846 * Note: To really walk the device tree you need the devinfo 23847 * pointer to your device which is only available after probe/attach. 23848 * The following is safe only because it uses ddi_root_node() 23849 */ 23850 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23851 tcp_opt_obj.odb_opt_arr_cnt); 23852 23853 /* 23854 * Initialize RFC 1948 secret values. This will probably be reset once 23855 * by the boot scripts. 23856 * 23857 * Use NULL name, as the name is caught by the new lockstats. 23858 * 23859 * Initialize with some random, non-guessable string, like the global 23860 * T_INFO_ACK. 23861 */ 23862 23863 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23864 sizeof (tcp_g_t_info_ack), tcps); 23865 23866 tcps->tcps_kstat = tcp_kstat2_init(stackid, &tcps->tcps_statistics); 23867 tcps->tcps_mibkp = tcp_kstat_init(stackid, tcps); 23868 23869 major = mod_name_to_major(INET_NAME); 23870 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident); 23871 ASSERT(error == 0); 23872 return (tcps); 23873 } 23874 23875 /* 23876 * Called when the IP module is about to be unloaded. 23877 */ 23878 void 23879 tcp_ddi_g_destroy(void) 23880 { 23881 tcp_g_kstat_fini(tcp_g_kstat); 23882 tcp_g_kstat = NULL; 23883 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics)); 23884 23885 mutex_destroy(&tcp_random_lock); 23886 23887 kmem_cache_destroy(tcp_timercache); 23888 kmem_cache_destroy(tcp_sack_info_cache); 23889 kmem_cache_destroy(tcp_iphc_cache); 23890 23891 netstack_unregister(NS_TCP); 23892 taskq_destroy(tcp_taskq); 23893 } 23894 23895 /* 23896 * Shut down the TCP stack instance. 23897 */ 23898 /* ARGSUSED */ 23899 static void 23900 tcp_stack_shutdown(netstackid_t stackid, void *arg) 23901 { 23902 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23903 23904 tcp_g_q_destroy(tcps); 23905 } 23906 23907 /* 23908 * Free the TCP stack instance. 23909 */ 23910 static void 23911 tcp_stack_fini(netstackid_t stackid, void *arg) 23912 { 23913 tcp_stack_t *tcps = (tcp_stack_t *)arg; 23914 int i; 23915 23916 nd_free(&tcps->tcps_g_nd); 23917 kmem_free(tcps->tcps_params, sizeof (lcl_tcp_param_arr)); 23918 tcps->tcps_params = NULL; 23919 kmem_free(tcps->tcps_wroff_xtra_param, sizeof (tcpparam_t)); 23920 tcps->tcps_wroff_xtra_param = NULL; 23921 kmem_free(tcps->tcps_mdt_head_param, sizeof (tcpparam_t)); 23922 tcps->tcps_mdt_head_param = NULL; 23923 kmem_free(tcps->tcps_mdt_tail_param, sizeof (tcpparam_t)); 23924 tcps->tcps_mdt_tail_param = NULL; 23925 kmem_free(tcps->tcps_mdt_max_pbufs_param, sizeof (tcpparam_t)); 23926 tcps->tcps_mdt_max_pbufs_param = NULL; 23927 23928 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) { 23929 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL); 23930 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock); 23931 } 23932 23933 for (i = 0; i < TCP_FANOUT_SIZE; i++) { 23934 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL); 23935 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock); 23936 } 23937 23938 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE); 23939 tcps->tcps_bind_fanout = NULL; 23940 23941 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) * TCP_FANOUT_SIZE); 23942 tcps->tcps_acceptor_fanout = NULL; 23943 23944 mutex_destroy(&tcps->tcps_iss_key_lock); 23945 mutex_destroy(&tcps->tcps_g_q_lock); 23946 cv_destroy(&tcps->tcps_g_q_cv); 23947 mutex_destroy(&tcps->tcps_epriv_port_lock); 23948 23949 ip_drop_unregister(&tcps->tcps_dropper); 23950 23951 tcp_kstat2_fini(stackid, tcps->tcps_kstat); 23952 tcps->tcps_kstat = NULL; 23953 bzero(&tcps->tcps_statistics, sizeof (tcps->tcps_statistics)); 23954 23955 tcp_kstat_fini(stackid, tcps->tcps_mibkp); 23956 tcps->tcps_mibkp = NULL; 23957 23958 ldi_ident_release(tcps->tcps_ldi_ident); 23959 kmem_free(tcps, sizeof (*tcps)); 23960 } 23961 23962 /* 23963 * Generate ISS, taking into account NDD changes may happen halfway through. 23964 * (If the iss is not zero, set it.) 23965 */ 23966 23967 static void 23968 tcp_iss_init(tcp_t *tcp) 23969 { 23970 MD5_CTX context; 23971 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23972 uint32_t answer[4]; 23973 tcp_stack_t *tcps = tcp->tcp_tcps; 23974 23975 tcps->tcps_iss_incr_extra += (ISS_INCR >> 1); 23976 tcp->tcp_iss = tcps->tcps_iss_incr_extra; 23977 switch (tcps->tcps_strong_iss) { 23978 case 2: 23979 mutex_enter(&tcps->tcps_iss_key_lock); 23980 context = tcps->tcps_iss_key; 23981 mutex_exit(&tcps->tcps_iss_key_lock); 23982 arg.ports = tcp->tcp_ports; 23983 if (tcp->tcp_ipversion == IPV4_VERSION) { 23984 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23985 &arg.src); 23986 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23987 &arg.dst); 23988 } else { 23989 arg.src = tcp->tcp_ip6h->ip6_src; 23990 arg.dst = tcp->tcp_ip6h->ip6_dst; 23991 } 23992 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23993 MD5Final((uchar_t *)answer, &context); 23994 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23995 /* 23996 * Now that we've hashed into a unique per-connection sequence 23997 * space, add a random increment per strong_iss == 1. So I 23998 * guess we'll have to... 23999 */ 24000 /* FALLTHRU */ 24001 case 1: 24002 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 24003 break; 24004 default: 24005 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24006 break; 24007 } 24008 tcp->tcp_valid_bits = TCP_ISS_VALID; 24009 tcp->tcp_fss = tcp->tcp_iss - 1; 24010 tcp->tcp_suna = tcp->tcp_iss; 24011 tcp->tcp_snxt = tcp->tcp_iss + 1; 24012 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 24013 tcp->tcp_csuna = tcp->tcp_snxt; 24014 } 24015 24016 /* 24017 * Exported routine for extracting active tcp connection status. 24018 * 24019 * This is used by the Solaris Cluster Networking software to 24020 * gather a list of connections that need to be forwarded to 24021 * specific nodes in the cluster when configuration changes occur. 24022 * 24023 * The callback is invoked for each tcp_t structure from all netstacks, 24024 * if 'stack_id' is less than 0. Otherwise, only for tcp_t structures 24025 * from the netstack with the specified stack_id. Returning 24026 * non-zero from the callback routine terminates the search. 24027 */ 24028 int 24029 cl_tcp_walk_list(netstackid_t stack_id, 24030 int (*cl_callback)(cl_tcp_info_t *, void *), void *arg) 24031 { 24032 netstack_handle_t nh; 24033 netstack_t *ns; 24034 int ret = 0; 24035 24036 if (stack_id >= 0) { 24037 if ((ns = netstack_find_by_stackid(stack_id)) == NULL) 24038 return (EINVAL); 24039 24040 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24041 ns->netstack_tcp); 24042 netstack_rele(ns); 24043 return (ret); 24044 } 24045 24046 netstack_next_init(&nh); 24047 while ((ns = netstack_next(&nh)) != NULL) { 24048 ret = cl_tcp_walk_list_stack(cl_callback, arg, 24049 ns->netstack_tcp); 24050 netstack_rele(ns); 24051 } 24052 netstack_next_fini(&nh); 24053 return (ret); 24054 } 24055 24056 static int 24057 cl_tcp_walk_list_stack(int (*callback)(cl_tcp_info_t *, void *), void *arg, 24058 tcp_stack_t *tcps) 24059 { 24060 tcp_t *tcp; 24061 cl_tcp_info_t cl_tcpi; 24062 connf_t *connfp; 24063 conn_t *connp; 24064 int i; 24065 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24066 24067 ASSERT(callback != NULL); 24068 24069 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24070 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 24071 connp = NULL; 24072 24073 while ((connp = 24074 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24075 24076 tcp = connp->conn_tcp; 24077 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 24078 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 24079 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 24080 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 24081 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 24082 /* 24083 * The macros tcp_laddr and tcp_faddr give the IPv4 24084 * addresses. They are copied implicitly below as 24085 * mapped addresses. 24086 */ 24087 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 24088 if (tcp->tcp_ipversion == IPV4_VERSION) { 24089 cl_tcpi.cl_tcpi_faddr = 24090 tcp->tcp_ipha->ipha_dst; 24091 } else { 24092 cl_tcpi.cl_tcpi_faddr_v6 = 24093 tcp->tcp_ip6h->ip6_dst; 24094 } 24095 24096 /* 24097 * If the callback returns non-zero 24098 * we terminate the traversal. 24099 */ 24100 if ((*callback)(&cl_tcpi, arg) != 0) { 24101 CONN_DEC_REF(tcp->tcp_connp); 24102 return (1); 24103 } 24104 } 24105 } 24106 24107 return (0); 24108 } 24109 24110 /* 24111 * Macros used for accessing the different types of sockaddr 24112 * structures inside a tcp_ioc_abort_conn_t. 24113 */ 24114 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 24115 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 24116 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 24117 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 24118 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 24119 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 24120 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 24121 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 24122 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 24123 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 24124 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 24125 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 24126 24127 /* 24128 * Return the correct error code to mimic the behavior 24129 * of a connection reset. 24130 */ 24131 #define TCP_AC_GET_ERRCODE(state, err) { \ 24132 switch ((state)) { \ 24133 case TCPS_SYN_SENT: \ 24134 case TCPS_SYN_RCVD: \ 24135 (err) = ECONNREFUSED; \ 24136 break; \ 24137 case TCPS_ESTABLISHED: \ 24138 case TCPS_FIN_WAIT_1: \ 24139 case TCPS_FIN_WAIT_2: \ 24140 case TCPS_CLOSE_WAIT: \ 24141 (err) = ECONNRESET; \ 24142 break; \ 24143 case TCPS_CLOSING: \ 24144 case TCPS_LAST_ACK: \ 24145 case TCPS_TIME_WAIT: \ 24146 (err) = 0; \ 24147 break; \ 24148 default: \ 24149 (err) = ENXIO; \ 24150 } \ 24151 } 24152 24153 /* 24154 * Check if a tcp structure matches the info in acp. 24155 */ 24156 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 24157 (((acp)->ac_local.ss_family == AF_INET) ? \ 24158 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 24159 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 24160 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 24161 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 24162 (TCP_AC_V4LPORT((acp)) == 0 || \ 24163 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 24164 (TCP_AC_V4RPORT((acp)) == 0 || \ 24165 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24166 (acp)->ac_start <= (tcp)->tcp_state && \ 24167 (acp)->ac_end >= (tcp)->tcp_state) : \ 24168 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24169 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24170 &(tcp)->tcp_ip_src_v6)) && \ 24171 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24172 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24173 &(tcp)->tcp_remote_v6)) && \ 24174 (TCP_AC_V6LPORT((acp)) == 0 || \ 24175 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24176 (TCP_AC_V6RPORT((acp)) == 0 || \ 24177 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24178 (acp)->ac_start <= (tcp)->tcp_state && \ 24179 (acp)->ac_end >= (tcp)->tcp_state)) 24180 24181 #define TCP_AC_MATCH(acp, tcp) \ 24182 (((acp)->ac_zoneid == ALL_ZONES || \ 24183 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24184 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24185 24186 /* 24187 * Build a message containing a tcp_ioc_abort_conn_t structure 24188 * which is filled in with information from acp and tp. 24189 */ 24190 static mblk_t * 24191 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24192 { 24193 mblk_t *mp; 24194 tcp_ioc_abort_conn_t *tacp; 24195 24196 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24197 if (mp == NULL) 24198 return (NULL); 24199 24200 mp->b_datap->db_type = M_CTL; 24201 24202 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24203 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24204 sizeof (uint32_t)); 24205 24206 tacp->ac_start = acp->ac_start; 24207 tacp->ac_end = acp->ac_end; 24208 tacp->ac_zoneid = acp->ac_zoneid; 24209 24210 if (acp->ac_local.ss_family == AF_INET) { 24211 tacp->ac_local.ss_family = AF_INET; 24212 tacp->ac_remote.ss_family = AF_INET; 24213 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24214 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24215 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24216 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24217 } else { 24218 tacp->ac_local.ss_family = AF_INET6; 24219 tacp->ac_remote.ss_family = AF_INET6; 24220 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24221 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24222 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24223 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24224 } 24225 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24226 return (mp); 24227 } 24228 24229 /* 24230 * Print a tcp_ioc_abort_conn_t structure. 24231 */ 24232 static void 24233 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24234 { 24235 char lbuf[128]; 24236 char rbuf[128]; 24237 sa_family_t af; 24238 in_port_t lport, rport; 24239 ushort_t logflags; 24240 24241 af = acp->ac_local.ss_family; 24242 24243 if (af == AF_INET) { 24244 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24245 lbuf, 128); 24246 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24247 rbuf, 128); 24248 lport = ntohs(TCP_AC_V4LPORT(acp)); 24249 rport = ntohs(TCP_AC_V4RPORT(acp)); 24250 } else { 24251 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24252 lbuf, 128); 24253 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24254 rbuf, 128); 24255 lport = ntohs(TCP_AC_V6LPORT(acp)); 24256 rport = ntohs(TCP_AC_V6RPORT(acp)); 24257 } 24258 24259 logflags = SL_TRACE | SL_NOTE; 24260 /* 24261 * Don't print this message to the console if the operation was done 24262 * to a non-global zone. 24263 */ 24264 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24265 logflags |= SL_CONSOLE; 24266 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24267 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24268 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24269 acp->ac_start, acp->ac_end); 24270 } 24271 24272 /* 24273 * Called inside tcp_rput when a message built using 24274 * tcp_ioctl_abort_build_msg is put into a queue. 24275 * Note that when we get here there is no wildcard in acp any more. 24276 */ 24277 static void 24278 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24279 { 24280 tcp_ioc_abort_conn_t *acp; 24281 24282 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24283 if (tcp->tcp_state <= acp->ac_end) { 24284 /* 24285 * If we get here, we are already on the correct 24286 * squeue. This ioctl follows the following path 24287 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24288 * ->tcp_ioctl_abort->squeue_enter (if on a 24289 * different squeue) 24290 */ 24291 int errcode; 24292 24293 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24294 (void) tcp_clean_death(tcp, errcode, 26); 24295 } 24296 freemsg(mp); 24297 } 24298 24299 /* 24300 * Abort all matching connections on a hash chain. 24301 */ 24302 static int 24303 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24304 boolean_t exact, tcp_stack_t *tcps) 24305 { 24306 int nmatch, err = 0; 24307 tcp_t *tcp; 24308 MBLKP mp, last, listhead = NULL; 24309 conn_t *tconnp; 24310 connf_t *connfp; 24311 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24312 24313 connfp = &ipst->ips_ipcl_conn_fanout[index]; 24314 24315 startover: 24316 nmatch = 0; 24317 24318 mutex_enter(&connfp->connf_lock); 24319 for (tconnp = connfp->connf_head; tconnp != NULL; 24320 tconnp = tconnp->conn_next) { 24321 tcp = tconnp->conn_tcp; 24322 if (TCP_AC_MATCH(acp, tcp)) { 24323 CONN_INC_REF(tcp->tcp_connp); 24324 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24325 if (mp == NULL) { 24326 err = ENOMEM; 24327 CONN_DEC_REF(tcp->tcp_connp); 24328 break; 24329 } 24330 mp->b_prev = (mblk_t *)tcp; 24331 24332 if (listhead == NULL) { 24333 listhead = mp; 24334 last = mp; 24335 } else { 24336 last->b_next = mp; 24337 last = mp; 24338 } 24339 nmatch++; 24340 if (exact) 24341 break; 24342 } 24343 24344 /* Avoid holding lock for too long. */ 24345 if (nmatch >= 500) 24346 break; 24347 } 24348 mutex_exit(&connfp->connf_lock); 24349 24350 /* Pass mp into the correct tcp */ 24351 while ((mp = listhead) != NULL) { 24352 listhead = listhead->b_next; 24353 tcp = (tcp_t *)mp->b_prev; 24354 mp->b_next = mp->b_prev = NULL; 24355 SQUEUE_ENTER_ONE(tcp->tcp_connp->conn_sqp, mp, tcp_input, 24356 tcp->tcp_connp, SQ_FILL, SQTAG_TCP_ABORT_BUCKET); 24357 } 24358 24359 *count += nmatch; 24360 if (nmatch >= 500 && err == 0) 24361 goto startover; 24362 return (err); 24363 } 24364 24365 /* 24366 * Abort all connections that matches the attributes specified in acp. 24367 */ 24368 static int 24369 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp, tcp_stack_t *tcps) 24370 { 24371 sa_family_t af; 24372 uint32_t ports; 24373 uint16_t *pports; 24374 int err = 0, count = 0; 24375 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24376 int index = -1; 24377 ushort_t logflags; 24378 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24379 24380 af = acp->ac_local.ss_family; 24381 24382 if (af == AF_INET) { 24383 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24384 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24385 pports = (uint16_t *)&ports; 24386 pports[1] = TCP_AC_V4LPORT(acp); 24387 pports[0] = TCP_AC_V4RPORT(acp); 24388 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24389 } 24390 } else { 24391 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24392 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24393 pports = (uint16_t *)&ports; 24394 pports[1] = TCP_AC_V6LPORT(acp); 24395 pports[0] = TCP_AC_V6RPORT(acp); 24396 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24397 } 24398 } 24399 24400 /* 24401 * For cases where remote addr, local port, and remote port are non- 24402 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24403 */ 24404 if (index != -1) { 24405 err = tcp_ioctl_abort_bucket(acp, index, 24406 &count, exact, tcps); 24407 } else { 24408 /* 24409 * loop through all entries for wildcard case 24410 */ 24411 for (index = 0; 24412 index < ipst->ips_ipcl_conn_fanout_size; 24413 index++) { 24414 err = tcp_ioctl_abort_bucket(acp, index, 24415 &count, exact, tcps); 24416 if (err != 0) 24417 break; 24418 } 24419 } 24420 24421 logflags = SL_TRACE | SL_NOTE; 24422 /* 24423 * Don't print this message to the console if the operation was done 24424 * to a non-global zone. 24425 */ 24426 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24427 logflags |= SL_CONSOLE; 24428 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24429 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24430 if (err == 0 && count == 0) 24431 err = ENOENT; 24432 return (err); 24433 } 24434 24435 /* 24436 * Process the TCP_IOC_ABORT_CONN ioctl request. 24437 */ 24438 static void 24439 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24440 { 24441 int err; 24442 IOCP iocp; 24443 MBLKP mp1; 24444 sa_family_t laf, raf; 24445 tcp_ioc_abort_conn_t *acp; 24446 zone_t *zptr; 24447 conn_t *connp = Q_TO_CONN(q); 24448 zoneid_t zoneid = connp->conn_zoneid; 24449 tcp_t *tcp = connp->conn_tcp; 24450 tcp_stack_t *tcps = tcp->tcp_tcps; 24451 24452 iocp = (IOCP)mp->b_rptr; 24453 24454 if ((mp1 = mp->b_cont) == NULL || 24455 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24456 err = EINVAL; 24457 goto out; 24458 } 24459 24460 /* check permissions */ 24461 if (secpolicy_ip_config(iocp->ioc_cr, B_FALSE) != 0) { 24462 err = EPERM; 24463 goto out; 24464 } 24465 24466 if (mp1->b_cont != NULL) { 24467 freemsg(mp1->b_cont); 24468 mp1->b_cont = NULL; 24469 } 24470 24471 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24472 laf = acp->ac_local.ss_family; 24473 raf = acp->ac_remote.ss_family; 24474 24475 /* check that a zone with the supplied zoneid exists */ 24476 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24477 zptr = zone_find_by_id(zoneid); 24478 if (zptr != NULL) { 24479 zone_rele(zptr); 24480 } else { 24481 err = EINVAL; 24482 goto out; 24483 } 24484 } 24485 24486 /* 24487 * For exclusive stacks we set the zoneid to zero 24488 * to make TCP operate as if in the global zone. 24489 */ 24490 if (tcps->tcps_netstack->netstack_stackid != GLOBAL_NETSTACKID) 24491 acp->ac_zoneid = GLOBAL_ZONEID; 24492 24493 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24494 acp->ac_start > acp->ac_end || laf != raf || 24495 (laf != AF_INET && laf != AF_INET6)) { 24496 err = EINVAL; 24497 goto out; 24498 } 24499 24500 tcp_ioctl_abort_dump(acp); 24501 err = tcp_ioctl_abort(acp, tcps); 24502 24503 out: 24504 if (mp1 != NULL) { 24505 freemsg(mp1); 24506 mp->b_cont = NULL; 24507 } 24508 24509 if (err != 0) 24510 miocnak(q, mp, 0, err); 24511 else 24512 miocack(q, mp, 0, 0); 24513 } 24514 24515 /* 24516 * tcp_time_wait_processing() handles processing of incoming packets when 24517 * the tcp is in the TIME_WAIT state. 24518 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24519 * on the time wait list. 24520 */ 24521 void 24522 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24523 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24524 { 24525 int32_t bytes_acked; 24526 int32_t gap; 24527 int32_t rgap; 24528 tcp_opt_t tcpopt; 24529 uint_t flags; 24530 uint32_t new_swnd = 0; 24531 conn_t *connp; 24532 tcp_stack_t *tcps = tcp->tcp_tcps; 24533 24534 BUMP_LOCAL(tcp->tcp_ibsegs); 24535 DTRACE_PROBE2(tcp__trace__recv, mblk_t *, mp, tcp_t *, tcp); 24536 24537 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24538 new_swnd = BE16_TO_U16(tcph->th_win) << 24539 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24540 if (tcp->tcp_snd_ts_ok) { 24541 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24542 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24543 tcp->tcp_rnxt, TH_ACK); 24544 goto done; 24545 } 24546 } 24547 gap = seg_seq - tcp->tcp_rnxt; 24548 rgap = tcp->tcp_rwnd - (gap + seg_len); 24549 if (gap < 0) { 24550 BUMP_MIB(&tcps->tcps_mib, tcpInDataDupSegs); 24551 UPDATE_MIB(&tcps->tcps_mib, tcpInDataDupBytes, 24552 (seg_len > -gap ? -gap : seg_len)); 24553 seg_len += gap; 24554 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24555 if (flags & TH_RST) { 24556 goto done; 24557 } 24558 if ((flags & TH_FIN) && seg_len == -1) { 24559 /* 24560 * When TCP receives a duplicate FIN in 24561 * TIME_WAIT state, restart the 2 MSL timer. 24562 * See page 73 in RFC 793. Make sure this TCP 24563 * is already on the TIME_WAIT list. If not, 24564 * just restart the timer. 24565 */ 24566 if (TCP_IS_DETACHED(tcp)) { 24567 if (tcp_time_wait_remove(tcp, NULL) == 24568 B_TRUE) { 24569 tcp_time_wait_append(tcp); 24570 TCP_DBGSTAT(tcps, 24571 tcp_rput_time_wait); 24572 } 24573 } else { 24574 ASSERT(tcp != NULL); 24575 TCP_TIMER_RESTART(tcp, 24576 tcps->tcps_time_wait_interval); 24577 } 24578 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24579 tcp->tcp_rnxt, TH_ACK); 24580 goto done; 24581 } 24582 flags |= TH_ACK_NEEDED; 24583 seg_len = 0; 24584 goto process_ack; 24585 } 24586 24587 /* Fix seg_seq, and chew the gap off the front. */ 24588 seg_seq = tcp->tcp_rnxt; 24589 } 24590 24591 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24592 /* 24593 * Make sure that when we accept the connection, pick 24594 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24595 * old connection. 24596 * 24597 * The next ISS generated is equal to tcp_iss_incr_extra 24598 * + ISS_INCR/2 + other components depending on the 24599 * value of tcp_strong_iss. We pre-calculate the new 24600 * ISS here and compare with tcp_snxt to determine if 24601 * we need to make adjustment to tcp_iss_incr_extra. 24602 * 24603 * The above calculation is ugly and is a 24604 * waste of CPU cycles... 24605 */ 24606 uint32_t new_iss = tcps->tcps_iss_incr_extra; 24607 int32_t adj; 24608 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip; 24609 24610 switch (tcps->tcps_strong_iss) { 24611 case 2: { 24612 /* Add time and MD5 components. */ 24613 uint32_t answer[4]; 24614 struct { 24615 uint32_t ports; 24616 in6_addr_t src; 24617 in6_addr_t dst; 24618 } arg; 24619 MD5_CTX context; 24620 24621 mutex_enter(&tcps->tcps_iss_key_lock); 24622 context = tcps->tcps_iss_key; 24623 mutex_exit(&tcps->tcps_iss_key_lock); 24624 arg.ports = tcp->tcp_ports; 24625 /* We use MAPPED addresses in tcp_iss_init */ 24626 arg.src = tcp->tcp_ip_src_v6; 24627 if (tcp->tcp_ipversion == IPV4_VERSION) { 24628 IN6_IPADDR_TO_V4MAPPED( 24629 tcp->tcp_ipha->ipha_dst, 24630 &arg.dst); 24631 } else { 24632 arg.dst = 24633 tcp->tcp_ip6h->ip6_dst; 24634 } 24635 MD5Update(&context, (uchar_t *)&arg, 24636 sizeof (arg)); 24637 MD5Final((uchar_t *)answer, &context); 24638 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24639 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24640 break; 24641 } 24642 case 1: 24643 /* Add time component and min random (i.e. 1). */ 24644 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24645 break; 24646 default: 24647 /* Add only time component. */ 24648 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24649 break; 24650 } 24651 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24652 /* 24653 * New ISS not guaranteed to be ISS_INCR/2 24654 * ahead of the current tcp_snxt, so add the 24655 * difference to tcp_iss_incr_extra. 24656 */ 24657 tcps->tcps_iss_incr_extra += adj; 24658 } 24659 /* 24660 * If tcp_clean_death() can not perform the task now, 24661 * drop the SYN packet and let the other side re-xmit. 24662 * Otherwise pass the SYN packet back in, since the 24663 * old tcp state has been cleaned up or freed. 24664 */ 24665 if (tcp_clean_death(tcp, 0, 27) == -1) 24666 goto done; 24667 /* 24668 * We will come back to tcp_rput_data 24669 * on the global queue. Packets destined 24670 * for the global queue will be checked 24671 * with global policy. But the policy for 24672 * this packet has already been checked as 24673 * this was destined for the detached 24674 * connection. We need to bypass policy 24675 * check this time by attaching a dummy 24676 * ipsec_in with ipsec_in_dont_check set. 24677 */ 24678 connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid, ipst); 24679 if (connp != NULL) { 24680 TCP_STAT(tcps, tcp_time_wait_syn_success); 24681 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24682 return; 24683 } 24684 goto done; 24685 } 24686 24687 /* 24688 * rgap is the amount of stuff received out of window. A negative 24689 * value is the amount out of window. 24690 */ 24691 if (rgap < 0) { 24692 BUMP_MIB(&tcps->tcps_mib, tcpInDataPastWinSegs); 24693 UPDATE_MIB(&tcps->tcps_mib, tcpInDataPastWinBytes, -rgap); 24694 /* Fix seg_len and make sure there is something left. */ 24695 seg_len += rgap; 24696 if (seg_len <= 0) { 24697 if (flags & TH_RST) { 24698 goto done; 24699 } 24700 flags |= TH_ACK_NEEDED; 24701 seg_len = 0; 24702 goto process_ack; 24703 } 24704 } 24705 /* 24706 * Check whether we can update tcp_ts_recent. This test is 24707 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24708 * Extensions for High Performance: An Update", Internet Draft. 24709 */ 24710 if (tcp->tcp_snd_ts_ok && 24711 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24712 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24713 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24714 tcp->tcp_last_rcv_lbolt = lbolt64; 24715 } 24716 24717 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24718 /* Always ack out of order packets */ 24719 flags |= TH_ACK_NEEDED; 24720 seg_len = 0; 24721 } else if (seg_len > 0) { 24722 BUMP_MIB(&tcps->tcps_mib, tcpInClosed); 24723 BUMP_MIB(&tcps->tcps_mib, tcpInDataInorderSegs); 24724 UPDATE_MIB(&tcps->tcps_mib, tcpInDataInorderBytes, seg_len); 24725 } 24726 if (flags & TH_RST) { 24727 (void) tcp_clean_death(tcp, 0, 28); 24728 goto done; 24729 } 24730 if (flags & TH_SYN) { 24731 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24732 TH_RST|TH_ACK); 24733 /* 24734 * Do not delete the TCP structure if it is in 24735 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24736 */ 24737 goto done; 24738 } 24739 process_ack: 24740 if (flags & TH_ACK) { 24741 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24742 if (bytes_acked <= 0) { 24743 if (bytes_acked == 0 && seg_len == 0 && 24744 new_swnd == tcp->tcp_swnd) 24745 BUMP_MIB(&tcps->tcps_mib, tcpInDupAck); 24746 } else { 24747 /* Acks something not sent */ 24748 flags |= TH_ACK_NEEDED; 24749 } 24750 } 24751 if (flags & TH_ACK_NEEDED) { 24752 /* 24753 * Time to send an ack for some reason. 24754 */ 24755 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24756 tcp->tcp_rnxt, TH_ACK); 24757 } 24758 done: 24759 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24760 DB_CKSUMSTART(mp) = 0; 24761 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24762 TCP_STAT(tcps, tcp_time_wait_syn_fail); 24763 } 24764 freemsg(mp); 24765 } 24766 24767 /* 24768 * TCP Timers Implementation. 24769 */ 24770 timeout_id_t 24771 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24772 { 24773 mblk_t *mp; 24774 tcp_timer_t *tcpt; 24775 tcp_t *tcp = connp->conn_tcp; 24776 24777 ASSERT(connp->conn_sqp != NULL); 24778 24779 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_calls); 24780 24781 if (tcp->tcp_timercache == NULL) { 24782 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24783 } else { 24784 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timeout_cached_alloc); 24785 mp = tcp->tcp_timercache; 24786 tcp->tcp_timercache = mp->b_next; 24787 mp->b_next = NULL; 24788 ASSERT(mp->b_wptr == NULL); 24789 } 24790 24791 CONN_INC_REF(connp); 24792 tcpt = (tcp_timer_t *)mp->b_rptr; 24793 tcpt->connp = connp; 24794 tcpt->tcpt_proc = f; 24795 /* 24796 * TCP timers are normal timeouts. Plus, they do not require more than 24797 * a 10 millisecond resolution. By choosing a coarser resolution and by 24798 * rounding up the expiration to the next resolution boundary, we can 24799 * batch timers in the callout subsystem to make TCP timers more 24800 * efficient. The roundup also protects short timers from expiring too 24801 * early before they have a chance to be cancelled. 24802 */ 24803 tcpt->tcpt_tid = timeout_generic(CALLOUT_NORMAL, tcp_timer_callback, mp, 24804 TICK_TO_NSEC(tim), CALLOUT_TCP_RESOLUTION, CALLOUT_FLAG_ROUNDUP); 24805 24806 return ((timeout_id_t)mp); 24807 } 24808 24809 static void 24810 tcp_timer_callback(void *arg) 24811 { 24812 mblk_t *mp = (mblk_t *)arg; 24813 tcp_timer_t *tcpt; 24814 conn_t *connp; 24815 24816 tcpt = (tcp_timer_t *)mp->b_rptr; 24817 connp = tcpt->connp; 24818 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_timer_handler, connp, 24819 SQ_FILL, SQTAG_TCP_TIMER); 24820 } 24821 24822 static void 24823 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24824 { 24825 tcp_timer_t *tcpt; 24826 conn_t *connp = (conn_t *)arg; 24827 tcp_t *tcp = connp->conn_tcp; 24828 24829 tcpt = (tcp_timer_t *)mp->b_rptr; 24830 ASSERT(connp == tcpt->connp); 24831 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24832 24833 /* 24834 * If the TCP has reached the closed state, don't proceed any 24835 * further. This TCP logically does not exist on the system. 24836 * tcpt_proc could for example access queues, that have already 24837 * been qprocoff'ed off. Also see comments at the start of tcp_input 24838 */ 24839 if (tcp->tcp_state != TCPS_CLOSED) { 24840 (*tcpt->tcpt_proc)(connp); 24841 } else { 24842 tcp->tcp_timer_tid = 0; 24843 } 24844 tcp_timer_free(connp->conn_tcp, mp); 24845 } 24846 24847 /* 24848 * There is potential race with untimeout and the handler firing at the same 24849 * time. The mblock may be freed by the handler while we are trying to use 24850 * it. But since both should execute on the same squeue, this race should not 24851 * occur. 24852 */ 24853 clock_t 24854 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24855 { 24856 mblk_t *mp = (mblk_t *)id; 24857 tcp_timer_t *tcpt; 24858 clock_t delta; 24859 24860 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_cancel_reqs); 24861 24862 if (mp == NULL) 24863 return (-1); 24864 24865 tcpt = (tcp_timer_t *)mp->b_rptr; 24866 ASSERT(tcpt->connp == connp); 24867 24868 delta = untimeout_default(tcpt->tcpt_tid, 0); 24869 24870 if (delta >= 0) { 24871 TCP_DBGSTAT(connp->conn_tcp->tcp_tcps, tcp_timeout_canceled); 24872 tcp_timer_free(connp->conn_tcp, mp); 24873 CONN_DEC_REF(connp); 24874 } 24875 24876 return (delta); 24877 } 24878 24879 /* 24880 * Allocate space for the timer event. The allocation looks like mblk, but it is 24881 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24882 * 24883 * Dealing with failures: If we can't allocate from the timer cache we try 24884 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24885 * points to b_rptr. 24886 * If we can't allocate anything using allocb_tryhard(), we perform a last 24887 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24888 * save the actual allocation size in b_datap. 24889 */ 24890 mblk_t * 24891 tcp_timermp_alloc(int kmflags) 24892 { 24893 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24894 kmflags & ~KM_PANIC); 24895 24896 if (mp != NULL) { 24897 mp->b_next = mp->b_prev = NULL; 24898 mp->b_rptr = (uchar_t *)(&mp[1]); 24899 mp->b_wptr = NULL; 24900 mp->b_datap = NULL; 24901 mp->b_queue = NULL; 24902 mp->b_cont = NULL; 24903 } else if (kmflags & KM_PANIC) { 24904 /* 24905 * Failed to allocate memory for the timer. Try allocating from 24906 * dblock caches. 24907 */ 24908 /* ipclassifier calls this from a constructor - hence no tcps */ 24909 TCP_G_STAT(tcp_timermp_allocfail); 24910 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24911 if (mp == NULL) { 24912 size_t size = 0; 24913 /* 24914 * Memory is really low. Try tryhard allocation. 24915 * 24916 * ipclassifier calls this from a constructor - 24917 * hence no tcps 24918 */ 24919 TCP_G_STAT(tcp_timermp_allocdblfail); 24920 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24921 sizeof (tcp_timer_t), &size, kmflags); 24922 mp->b_rptr = (uchar_t *)(&mp[1]); 24923 mp->b_next = mp->b_prev = NULL; 24924 mp->b_wptr = (uchar_t *)-1; 24925 mp->b_datap = (dblk_t *)size; 24926 mp->b_queue = NULL; 24927 mp->b_cont = NULL; 24928 } 24929 ASSERT(mp->b_wptr != NULL); 24930 } 24931 /* ipclassifier calls this from a constructor - hence no tcps */ 24932 TCP_G_DBGSTAT(tcp_timermp_alloced); 24933 24934 return (mp); 24935 } 24936 24937 /* 24938 * Free per-tcp timer cache. 24939 * It can only contain entries from tcp_timercache. 24940 */ 24941 void 24942 tcp_timermp_free(tcp_t *tcp) 24943 { 24944 mblk_t *mp; 24945 24946 while ((mp = tcp->tcp_timercache) != NULL) { 24947 ASSERT(mp->b_wptr == NULL); 24948 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24949 kmem_cache_free(tcp_timercache, mp); 24950 } 24951 } 24952 24953 /* 24954 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24955 * events there already (currently at most two events are cached). 24956 * If the event is not allocated from the timer cache, free it right away. 24957 */ 24958 static void 24959 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24960 { 24961 mblk_t *mp1 = tcp->tcp_timercache; 24962 24963 if (mp->b_wptr != NULL) { 24964 /* 24965 * This allocation is not from a timer cache, free it right 24966 * away. 24967 */ 24968 if (mp->b_wptr != (uchar_t *)-1) 24969 freeb(mp); 24970 else 24971 kmem_free(mp, (size_t)mp->b_datap); 24972 } else if (mp1 == NULL || mp1->b_next == NULL) { 24973 /* Cache this timer block for future allocations */ 24974 mp->b_rptr = (uchar_t *)(&mp[1]); 24975 mp->b_next = mp1; 24976 tcp->tcp_timercache = mp; 24977 } else { 24978 kmem_cache_free(tcp_timercache, mp); 24979 TCP_DBGSTAT(tcp->tcp_tcps, tcp_timermp_freed); 24980 } 24981 } 24982 24983 /* 24984 * End of TCP Timers implementation. 24985 */ 24986 24987 /* 24988 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24989 * on the specified backing STREAMS q. Note, the caller may make the 24990 * decision to call based on the tcp_t.tcp_flow_stopped value which 24991 * when check outside the q's lock is only an advisory check ... 24992 */ 24993 void 24994 tcp_setqfull(tcp_t *tcp) 24995 { 24996 tcp_stack_t *tcps = tcp->tcp_tcps; 24997 conn_t *connp = tcp->tcp_connp; 24998 24999 if (tcp->tcp_closed) 25000 return; 25001 25002 if (IPCL_IS_NONSTR(connp)) { 25003 (*connp->conn_upcalls->su_txq_full) 25004 (tcp->tcp_connp->conn_upper_handle, B_TRUE); 25005 tcp->tcp_flow_stopped = B_TRUE; 25006 } else { 25007 queue_t *q = tcp->tcp_wq; 25008 25009 if (!(q->q_flag & QFULL)) { 25010 mutex_enter(QLOCK(q)); 25011 if (!(q->q_flag & QFULL)) { 25012 /* still need to set QFULL */ 25013 q->q_flag |= QFULL; 25014 tcp->tcp_flow_stopped = B_TRUE; 25015 mutex_exit(QLOCK(q)); 25016 TCP_STAT(tcps, tcp_flwctl_on); 25017 } else { 25018 mutex_exit(QLOCK(q)); 25019 } 25020 } 25021 } 25022 } 25023 25024 void 25025 tcp_clrqfull(tcp_t *tcp) 25026 { 25027 conn_t *connp = tcp->tcp_connp; 25028 25029 if (tcp->tcp_closed) 25030 return; 25031 25032 if (IPCL_IS_NONSTR(connp)) { 25033 (*connp->conn_upcalls->su_txq_full) 25034 (tcp->tcp_connp->conn_upper_handle, B_FALSE); 25035 tcp->tcp_flow_stopped = B_FALSE; 25036 } else { 25037 queue_t *q = tcp->tcp_wq; 25038 25039 if (q->q_flag & QFULL) { 25040 mutex_enter(QLOCK(q)); 25041 if (q->q_flag & QFULL) { 25042 q->q_flag &= ~QFULL; 25043 tcp->tcp_flow_stopped = B_FALSE; 25044 mutex_exit(QLOCK(q)); 25045 if (q->q_flag & QWANTW) 25046 qbackenable(q, 0); 25047 } else { 25048 mutex_exit(QLOCK(q)); 25049 } 25050 } 25051 } 25052 } 25053 25054 /* 25055 * kstats related to squeues i.e. not per IP instance 25056 */ 25057 static void * 25058 tcp_g_kstat_init(tcp_g_stat_t *tcp_g_statp) 25059 { 25060 kstat_t *ksp; 25061 25062 tcp_g_stat_t template = { 25063 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 25064 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 25065 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 25066 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 25067 }; 25068 25069 ksp = kstat_create(TCP_MOD_NAME, 0, "tcpstat_g", "net", 25070 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25071 KSTAT_FLAG_VIRTUAL); 25072 25073 if (ksp == NULL) 25074 return (NULL); 25075 25076 bcopy(&template, tcp_g_statp, sizeof (template)); 25077 ksp->ks_data = (void *)tcp_g_statp; 25078 25079 kstat_install(ksp); 25080 return (ksp); 25081 } 25082 25083 static void 25084 tcp_g_kstat_fini(kstat_t *ksp) 25085 { 25086 if (ksp != NULL) { 25087 kstat_delete(ksp); 25088 } 25089 } 25090 25091 25092 static void * 25093 tcp_kstat2_init(netstackid_t stackid, tcp_stat_t *tcps_statisticsp) 25094 { 25095 kstat_t *ksp; 25096 25097 tcp_stat_t template = { 25098 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 25099 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 25100 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 25101 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 25102 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 25103 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 25104 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 25105 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 25106 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 25107 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 25108 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 25109 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 25110 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 25111 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 25112 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 25113 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 25114 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 25115 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 25116 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 25117 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 25118 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 25119 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 25120 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 25121 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 25122 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 25123 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 25124 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 25125 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 25126 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 25127 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 25128 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 25129 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 25130 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 25131 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 25132 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 25133 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 25134 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 25135 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 25136 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 25137 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 25138 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 25139 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 25140 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 25141 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 25142 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 25143 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 25144 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 25145 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 25146 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 25147 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 25148 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 25149 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 25150 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 25151 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 25152 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 25153 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 25154 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 25155 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 25156 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 25157 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 25158 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 25159 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 25160 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 25161 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 25162 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 25163 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 25164 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 25165 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 25166 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 25167 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 25168 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 25169 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 25170 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 25171 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 25172 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 25173 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 25174 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 25175 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 25176 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 25177 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 25178 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 25179 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 25180 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 25181 { "tcp_fusion_rrw_plugged", KSTAT_DATA_UINT64 }, 25182 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 25183 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 25184 { "tcp_lso_enabled", KSTAT_DATA_UINT64 }, 25185 { "tcp_lso_disabled", KSTAT_DATA_UINT64 }, 25186 { "tcp_lso_times", KSTAT_DATA_UINT64 }, 25187 { "tcp_lso_pkt_out", KSTAT_DATA_UINT64 }, 25188 }; 25189 25190 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, "tcpstat", "net", 25191 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t), 25192 KSTAT_FLAG_VIRTUAL, stackid); 25193 25194 if (ksp == NULL) 25195 return (NULL); 25196 25197 bcopy(&template, tcps_statisticsp, sizeof (template)); 25198 ksp->ks_data = (void *)tcps_statisticsp; 25199 ksp->ks_private = (void *)(uintptr_t)stackid; 25200 25201 kstat_install(ksp); 25202 return (ksp); 25203 } 25204 25205 static void 25206 tcp_kstat2_fini(netstackid_t stackid, kstat_t *ksp) 25207 { 25208 if (ksp != NULL) { 25209 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25210 kstat_delete_netstack(ksp, stackid); 25211 } 25212 } 25213 25214 /* 25215 * TCP Kstats implementation 25216 */ 25217 static void * 25218 tcp_kstat_init(netstackid_t stackid, tcp_stack_t *tcps) 25219 { 25220 kstat_t *ksp; 25221 25222 tcp_named_kstat_t template = { 25223 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 25224 { "rtoMin", KSTAT_DATA_INT32, 0 }, 25225 { "rtoMax", KSTAT_DATA_INT32, 0 }, 25226 { "maxConn", KSTAT_DATA_INT32, 0 }, 25227 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 25228 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 25229 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 25230 { "estabResets", KSTAT_DATA_UINT32, 0 }, 25231 { "currEstab", KSTAT_DATA_UINT32, 0 }, 25232 { "inSegs", KSTAT_DATA_UINT64, 0 }, 25233 { "outSegs", KSTAT_DATA_UINT64, 0 }, 25234 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 25235 { "connTableSize", KSTAT_DATA_INT32, 0 }, 25236 { "outRsts", KSTAT_DATA_UINT32, 0 }, 25237 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 25238 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 25239 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 25240 { "outAck", KSTAT_DATA_UINT32, 0 }, 25241 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 25242 { "outUrg", KSTAT_DATA_UINT32, 0 }, 25243 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 25244 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 25245 { "outControl", KSTAT_DATA_UINT32, 0 }, 25246 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 25247 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 25248 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 25249 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 25250 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 25251 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 25252 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 25253 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 25254 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 25255 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 25256 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 25257 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 25258 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 25259 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 25260 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 25261 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 25262 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 25263 { "inClosed", KSTAT_DATA_UINT32, 0 }, 25264 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 25265 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 25266 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 25267 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 25268 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 25269 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 25270 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 25271 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 25272 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 25273 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 25274 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 25275 { "connTableSize6", KSTAT_DATA_INT32, 0 } 25276 }; 25277 25278 ksp = kstat_create_netstack(TCP_MOD_NAME, 0, TCP_MOD_NAME, "mib2", 25279 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0, stackid); 25280 25281 if (ksp == NULL) 25282 return (NULL); 25283 25284 template.rtoAlgorithm.value.ui32 = 4; 25285 template.rtoMin.value.ui32 = tcps->tcps_rexmit_interval_min; 25286 template.rtoMax.value.ui32 = tcps->tcps_rexmit_interval_max; 25287 template.maxConn.value.i32 = -1; 25288 25289 bcopy(&template, ksp->ks_data, sizeof (template)); 25290 ksp->ks_update = tcp_kstat_update; 25291 ksp->ks_private = (void *)(uintptr_t)stackid; 25292 25293 kstat_install(ksp); 25294 return (ksp); 25295 } 25296 25297 static void 25298 tcp_kstat_fini(netstackid_t stackid, kstat_t *ksp) 25299 { 25300 if (ksp != NULL) { 25301 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private); 25302 kstat_delete_netstack(ksp, stackid); 25303 } 25304 } 25305 25306 static int 25307 tcp_kstat_update(kstat_t *kp, int rw) 25308 { 25309 tcp_named_kstat_t *tcpkp; 25310 tcp_t *tcp; 25311 connf_t *connfp; 25312 conn_t *connp; 25313 int i; 25314 netstackid_t stackid = (netstackid_t)(uintptr_t)kp->ks_private; 25315 netstack_t *ns; 25316 tcp_stack_t *tcps; 25317 ip_stack_t *ipst; 25318 25319 if ((kp == NULL) || (kp->ks_data == NULL)) 25320 return (EIO); 25321 25322 if (rw == KSTAT_WRITE) 25323 return (EACCES); 25324 25325 ns = netstack_find_by_stackid(stackid); 25326 if (ns == NULL) 25327 return (-1); 25328 tcps = ns->netstack_tcp; 25329 if (tcps == NULL) { 25330 netstack_rele(ns); 25331 return (-1); 25332 } 25333 25334 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 25335 25336 tcpkp->currEstab.value.ui32 = 0; 25337 25338 ipst = ns->netstack_ip; 25339 25340 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 25341 connfp = &ipst->ips_ipcl_globalhash_fanout[i]; 25342 connp = NULL; 25343 while ((connp = 25344 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 25345 tcp = connp->conn_tcp; 25346 switch (tcp_snmp_state(tcp)) { 25347 case MIB2_TCP_established: 25348 case MIB2_TCP_closeWait: 25349 tcpkp->currEstab.value.ui32++; 25350 break; 25351 } 25352 } 25353 } 25354 25355 tcpkp->activeOpens.value.ui32 = tcps->tcps_mib.tcpActiveOpens; 25356 tcpkp->passiveOpens.value.ui32 = tcps->tcps_mib.tcpPassiveOpens; 25357 tcpkp->attemptFails.value.ui32 = tcps->tcps_mib.tcpAttemptFails; 25358 tcpkp->estabResets.value.ui32 = tcps->tcps_mib.tcpEstabResets; 25359 tcpkp->inSegs.value.ui64 = tcps->tcps_mib.tcpHCInSegs; 25360 tcpkp->outSegs.value.ui64 = tcps->tcps_mib.tcpHCOutSegs; 25361 tcpkp->retransSegs.value.ui32 = tcps->tcps_mib.tcpRetransSegs; 25362 tcpkp->connTableSize.value.i32 = tcps->tcps_mib.tcpConnTableSize; 25363 tcpkp->outRsts.value.ui32 = tcps->tcps_mib.tcpOutRsts; 25364 tcpkp->outDataSegs.value.ui32 = tcps->tcps_mib.tcpOutDataSegs; 25365 tcpkp->outDataBytes.value.ui32 = tcps->tcps_mib.tcpOutDataBytes; 25366 tcpkp->retransBytes.value.ui32 = tcps->tcps_mib.tcpRetransBytes; 25367 tcpkp->outAck.value.ui32 = tcps->tcps_mib.tcpOutAck; 25368 tcpkp->outAckDelayed.value.ui32 = tcps->tcps_mib.tcpOutAckDelayed; 25369 tcpkp->outUrg.value.ui32 = tcps->tcps_mib.tcpOutUrg; 25370 tcpkp->outWinUpdate.value.ui32 = tcps->tcps_mib.tcpOutWinUpdate; 25371 tcpkp->outWinProbe.value.ui32 = tcps->tcps_mib.tcpOutWinProbe; 25372 tcpkp->outControl.value.ui32 = tcps->tcps_mib.tcpOutControl; 25373 tcpkp->outFastRetrans.value.ui32 = tcps->tcps_mib.tcpOutFastRetrans; 25374 tcpkp->inAckSegs.value.ui32 = tcps->tcps_mib.tcpInAckSegs; 25375 tcpkp->inAckBytes.value.ui32 = tcps->tcps_mib.tcpInAckBytes; 25376 tcpkp->inDupAck.value.ui32 = tcps->tcps_mib.tcpInDupAck; 25377 tcpkp->inAckUnsent.value.ui32 = tcps->tcps_mib.tcpInAckUnsent; 25378 tcpkp->inDataInorderSegs.value.ui32 = 25379 tcps->tcps_mib.tcpInDataInorderSegs; 25380 tcpkp->inDataInorderBytes.value.ui32 = 25381 tcps->tcps_mib.tcpInDataInorderBytes; 25382 tcpkp->inDataUnorderSegs.value.ui32 = 25383 tcps->tcps_mib.tcpInDataUnorderSegs; 25384 tcpkp->inDataUnorderBytes.value.ui32 = 25385 tcps->tcps_mib.tcpInDataUnorderBytes; 25386 tcpkp->inDataDupSegs.value.ui32 = tcps->tcps_mib.tcpInDataDupSegs; 25387 tcpkp->inDataDupBytes.value.ui32 = tcps->tcps_mib.tcpInDataDupBytes; 25388 tcpkp->inDataPartDupSegs.value.ui32 = 25389 tcps->tcps_mib.tcpInDataPartDupSegs; 25390 tcpkp->inDataPartDupBytes.value.ui32 = 25391 tcps->tcps_mib.tcpInDataPartDupBytes; 25392 tcpkp->inDataPastWinSegs.value.ui32 = 25393 tcps->tcps_mib.tcpInDataPastWinSegs; 25394 tcpkp->inDataPastWinBytes.value.ui32 = 25395 tcps->tcps_mib.tcpInDataPastWinBytes; 25396 tcpkp->inWinProbe.value.ui32 = tcps->tcps_mib.tcpInWinProbe; 25397 tcpkp->inWinUpdate.value.ui32 = tcps->tcps_mib.tcpInWinUpdate; 25398 tcpkp->inClosed.value.ui32 = tcps->tcps_mib.tcpInClosed; 25399 tcpkp->rttNoUpdate.value.ui32 = tcps->tcps_mib.tcpRttNoUpdate; 25400 tcpkp->rttUpdate.value.ui32 = tcps->tcps_mib.tcpRttUpdate; 25401 tcpkp->timRetrans.value.ui32 = tcps->tcps_mib.tcpTimRetrans; 25402 tcpkp->timRetransDrop.value.ui32 = tcps->tcps_mib.tcpTimRetransDrop; 25403 tcpkp->timKeepalive.value.ui32 = tcps->tcps_mib.tcpTimKeepalive; 25404 tcpkp->timKeepaliveProbe.value.ui32 = 25405 tcps->tcps_mib.tcpTimKeepaliveProbe; 25406 tcpkp->timKeepaliveDrop.value.ui32 = 25407 tcps->tcps_mib.tcpTimKeepaliveDrop; 25408 tcpkp->listenDrop.value.ui32 = tcps->tcps_mib.tcpListenDrop; 25409 tcpkp->listenDropQ0.value.ui32 = tcps->tcps_mib.tcpListenDropQ0; 25410 tcpkp->halfOpenDrop.value.ui32 = tcps->tcps_mib.tcpHalfOpenDrop; 25411 tcpkp->outSackRetransSegs.value.ui32 = 25412 tcps->tcps_mib.tcpOutSackRetransSegs; 25413 tcpkp->connTableSize6.value.i32 = tcps->tcps_mib.tcp6ConnTableSize; 25414 25415 netstack_rele(ns); 25416 return (0); 25417 } 25418 25419 void 25420 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25421 { 25422 uint16_t hdr_len; 25423 ipha_t *ipha; 25424 uint8_t *nexthdrp; 25425 tcph_t *tcph; 25426 tcp_stack_t *tcps = connp->conn_tcp->tcp_tcps; 25427 25428 /* Already has an eager */ 25429 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25430 TCP_STAT(tcps, tcp_reinput_syn); 25431 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25432 SQ_PROCESS, SQTAG_TCP_REINPUT_EAGER); 25433 return; 25434 } 25435 25436 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25437 case IPV4_VERSION: 25438 ipha = (ipha_t *)mp->b_rptr; 25439 hdr_len = IPH_HDR_LENGTH(ipha); 25440 break; 25441 case IPV6_VERSION: 25442 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25443 &hdr_len, &nexthdrp)) { 25444 CONN_DEC_REF(connp); 25445 freemsg(mp); 25446 return; 25447 } 25448 break; 25449 } 25450 25451 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25452 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25453 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25454 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25455 } 25456 25457 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv, connp, 25458 SQ_FILL, SQTAG_TCP_REINPUT); 25459 } 25460 25461 static int 25462 tcp_squeue_switch(int val) 25463 { 25464 int rval = SQ_FILL; 25465 25466 switch (val) { 25467 case 1: 25468 rval = SQ_NODRAIN; 25469 break; 25470 case 2: 25471 rval = SQ_PROCESS; 25472 break; 25473 default: 25474 break; 25475 } 25476 return (rval); 25477 } 25478 25479 /* 25480 * This is called once for each squeue - globally for all stack 25481 * instances. 25482 */ 25483 static void 25484 tcp_squeue_add(squeue_t *sqp) 25485 { 25486 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25487 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25488 25489 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25490 tcp_time_wait->tcp_time_wait_tid = 25491 timeout_generic(CALLOUT_NORMAL, tcp_time_wait_collector, sqp, 25492 TICK_TO_NSEC(TCP_TIME_WAIT_DELAY), CALLOUT_TCP_RESOLUTION, 25493 CALLOUT_FLAG_ROUNDUP); 25494 if (tcp_free_list_max_cnt == 0) { 25495 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25496 max_ncpus : boot_max_ncpus); 25497 25498 /* 25499 * Limit number of entries to 1% of availble memory / tcp_ncpus 25500 */ 25501 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25502 (tcp_ncpus * sizeof (tcp_t) * 100); 25503 } 25504 tcp_time_wait->tcp_free_list_cnt = 0; 25505 } 25506 25507 static int 25508 tcp_post_ip_bind(tcp_t *tcp, mblk_t *mp, int error, cred_t *cr, pid_t pid) 25509 { 25510 mblk_t *ire_mp = NULL; 25511 mblk_t *syn_mp; 25512 mblk_t *mdti; 25513 mblk_t *lsoi; 25514 int retval; 25515 tcph_t *tcph; 25516 uint32_t mss; 25517 queue_t *q = tcp->tcp_rq; 25518 conn_t *connp = tcp->tcp_connp; 25519 tcp_stack_t *tcps = tcp->tcp_tcps; 25520 25521 if (error == 0) { 25522 /* 25523 * Adapt Multidata information, if any. The 25524 * following tcp_mdt_update routine will free 25525 * the message. 25526 */ 25527 if (mp != NULL && ((mdti = tcp_mdt_info_mp(mp)) != NULL)) { 25528 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 25529 b_rptr)->mdt_capab, B_TRUE); 25530 freemsg(mdti); 25531 } 25532 25533 /* 25534 * Check to update LSO information with tcp, and 25535 * tcp_lso_update routine will free the message. 25536 */ 25537 if (mp != NULL && ((lsoi = tcp_lso_info_mp(mp)) != NULL)) { 25538 tcp_lso_update(tcp, &((ip_lso_info_t *)lsoi-> 25539 b_rptr)->lso_capab); 25540 freemsg(lsoi); 25541 } 25542 25543 /* Get the IRE, if we had requested for it */ 25544 if (mp != NULL) 25545 ire_mp = tcp_ire_mp(&mp); 25546 25547 if (tcp->tcp_hard_binding) { 25548 tcp->tcp_hard_binding = B_FALSE; 25549 tcp->tcp_hard_bound = B_TRUE; 25550 CL_INET_CONNECT(tcp->tcp_connp, tcp, B_TRUE, retval); 25551 if (retval != 0) { 25552 error = EADDRINUSE; 25553 goto bind_failed; 25554 } 25555 } else { 25556 if (ire_mp != NULL) 25557 freeb(ire_mp); 25558 goto after_syn_sent; 25559 } 25560 25561 retval = tcp_adapt_ire(tcp, ire_mp); 25562 if (ire_mp != NULL) 25563 freeb(ire_mp); 25564 if (retval == 0) { 25565 error = (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 25566 ENETUNREACH : EADDRNOTAVAIL); 25567 goto ipcl_rm; 25568 } 25569 /* 25570 * Don't let an endpoint connect to itself. 25571 * Also checked in tcp_connect() but that 25572 * check can't handle the case when the 25573 * local IP address is INADDR_ANY. 25574 */ 25575 if (tcp->tcp_ipversion == IPV4_VERSION) { 25576 if ((tcp->tcp_ipha->ipha_dst == 25577 tcp->tcp_ipha->ipha_src) && 25578 (BE16_EQL(tcp->tcp_tcph->th_lport, 25579 tcp->tcp_tcph->th_fport))) { 25580 error = EADDRNOTAVAIL; 25581 goto ipcl_rm; 25582 } 25583 } else { 25584 if (IN6_ARE_ADDR_EQUAL( 25585 &tcp->tcp_ip6h->ip6_dst, 25586 &tcp->tcp_ip6h->ip6_src) && 25587 (BE16_EQL(tcp->tcp_tcph->th_lport, 25588 tcp->tcp_tcph->th_fport))) { 25589 error = EADDRNOTAVAIL; 25590 goto ipcl_rm; 25591 } 25592 } 25593 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 25594 /* 25595 * This should not be possible! Just for 25596 * defensive coding... 25597 */ 25598 if (tcp->tcp_state != TCPS_SYN_SENT) 25599 goto after_syn_sent; 25600 25601 if (is_system_labeled() && 25602 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 25603 error = EHOSTUNREACH; 25604 goto ipcl_rm; 25605 } 25606 25607 /* 25608 * tcp_adapt_ire() does not adjust 25609 * for TCP/IP header length. 25610 */ 25611 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 25612 25613 /* 25614 * Just make sure our rwnd is at 25615 * least tcp_recv_hiwat_mss * MSS 25616 * large, and round up to the nearest 25617 * MSS. 25618 * 25619 * We do the round up here because 25620 * we need to get the interface 25621 * MTU first before we can do the 25622 * round up. 25623 */ 25624 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 25625 tcps->tcps_recv_hiwat_minmss * mss); 25626 if (!IPCL_IS_NONSTR(connp)) 25627 q->q_hiwat = tcp->tcp_rwnd; 25628 tcp->tcp_recv_hiwater = tcp->tcp_rwnd; 25629 tcp_set_ws_value(tcp); 25630 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 25631 tcp->tcp_tcph->th_win); 25632 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always) 25633 tcp->tcp_snd_ws_ok = B_TRUE; 25634 25635 /* 25636 * Set tcp_snd_ts_ok to true 25637 * so that tcp_xmit_mp will 25638 * include the timestamp 25639 * option in the SYN segment. 25640 */ 25641 if (tcps->tcps_tstamp_always || 25642 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) { 25643 tcp->tcp_snd_ts_ok = B_TRUE; 25644 } 25645 25646 /* 25647 * tcp_snd_sack_ok can be set in 25648 * tcp_adapt_ire() if the sack metric 25649 * is set. So check it here also. 25650 */ 25651 if (tcps->tcps_sack_permitted == 2 || 25652 tcp->tcp_snd_sack_ok) { 25653 if (tcp->tcp_sack_info == NULL) { 25654 tcp->tcp_sack_info = 25655 kmem_cache_alloc(tcp_sack_info_cache, 25656 KM_SLEEP); 25657 } 25658 tcp->tcp_snd_sack_ok = B_TRUE; 25659 } 25660 25661 /* 25662 * Should we use ECN? Note that the current 25663 * default value (SunOS 5.9) of tcp_ecn_permitted 25664 * is 1. The reason for doing this is that there 25665 * are equipments out there that will drop ECN 25666 * enabled IP packets. Setting it to 1 avoids 25667 * compatibility problems. 25668 */ 25669 if (tcps->tcps_ecn_permitted == 2) 25670 tcp->tcp_ecn_ok = B_TRUE; 25671 25672 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 25673 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 25674 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 25675 if (syn_mp) { 25676 if (cr == NULL) { 25677 cr = tcp->tcp_cred; 25678 pid = tcp->tcp_cpid; 25679 } 25680 mblk_setcred(syn_mp, cr, pid); 25681 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 25682 } 25683 after_syn_sent: 25684 if (mp != NULL) { 25685 ASSERT(mp->b_cont == NULL); 25686 freeb(mp); 25687 } 25688 return (error); 25689 } else { 25690 /* error */ 25691 if (tcp->tcp_debug) { 25692 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 25693 "tcp_post_ip_bind: error == %d", error); 25694 } 25695 if (mp != NULL) { 25696 freeb(mp); 25697 } 25698 } 25699 25700 ipcl_rm: 25701 /* 25702 * Need to unbind with classifier since we were just 25703 * told that our bind succeeded. a.k.a error == 0 at the entry. 25704 */ 25705 tcp->tcp_hard_bound = B_FALSE; 25706 tcp->tcp_hard_binding = B_FALSE; 25707 25708 ipcl_hash_remove(connp); 25709 25710 bind_failed: 25711 tcp->tcp_state = TCPS_IDLE; 25712 if (tcp->tcp_ipversion == IPV4_VERSION) 25713 tcp->tcp_ipha->ipha_src = 0; 25714 else 25715 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 25716 /* 25717 * Copy of the src addr. in tcp_t is needed since 25718 * the lookup funcs. can only look at tcp_t 25719 */ 25720 V6_SET_ZERO(tcp->tcp_ip_src_v6); 25721 25722 tcph = tcp->tcp_tcph; 25723 tcph->th_lport[0] = 0; 25724 tcph->th_lport[1] = 0; 25725 tcp_bind_hash_remove(tcp); 25726 bzero(&connp->u_port, sizeof (connp->u_port)); 25727 /* blow away saved option results if any */ 25728 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 25729 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 25730 25731 conn_delete_ire(tcp->tcp_connp, NULL); 25732 25733 return (error); 25734 } 25735 25736 static int 25737 tcp_bind_select_lport(tcp_t *tcp, in_port_t *requested_port_ptr, 25738 boolean_t bind_to_req_port_only, cred_t *cr) 25739 { 25740 in_port_t mlp_port; 25741 mlp_type_t addrtype, mlptype; 25742 boolean_t user_specified; 25743 in_port_t allocated_port; 25744 in_port_t requested_port = *requested_port_ptr; 25745 conn_t *connp; 25746 zone_t *zone; 25747 tcp_stack_t *tcps = tcp->tcp_tcps; 25748 in6_addr_t v6addr = tcp->tcp_ip_src_v6; 25749 25750 /* 25751 * XXX It's up to the caller to specify bind_to_req_port_only or not. 25752 */ 25753 if (cr == NULL) 25754 cr = tcp->tcp_cred; 25755 /* 25756 * Get a valid port (within the anonymous range and should not 25757 * be a privileged one) to use if the user has not given a port. 25758 * If multiple threads are here, they may all start with 25759 * with the same initial port. But, it should be fine as long as 25760 * tcp_bindi will ensure that no two threads will be assigned 25761 * the same port. 25762 * 25763 * NOTE: XXX If a privileged process asks for an anonymous port, we 25764 * still check for ports only in the range > tcp_smallest_non_priv_port, 25765 * unless TCP_ANONPRIVBIND option is set. 25766 */ 25767 mlptype = mlptSingle; 25768 mlp_port = requested_port; 25769 if (requested_port == 0) { 25770 requested_port = tcp->tcp_anon_priv_bind ? 25771 tcp_get_next_priv_port(tcp) : 25772 tcp_update_next_port(tcps->tcps_next_port_to_try, 25773 tcp, B_TRUE); 25774 if (requested_port == 0) { 25775 return (-TNOADDR); 25776 } 25777 user_specified = B_FALSE; 25778 25779 /* 25780 * If the user went through one of the RPC interfaces to create 25781 * this socket and RPC is MLP in this zone, then give him an 25782 * anonymous MLP. 25783 */ 25784 connp = tcp->tcp_connp; 25785 if (connp->conn_anon_mlp && is_system_labeled()) { 25786 zone = crgetzone(cr); 25787 addrtype = tsol_mlp_addr_type(zone->zone_id, 25788 IPV6_VERSION, &v6addr, 25789 tcps->tcps_netstack->netstack_ip); 25790 if (addrtype == mlptSingle) { 25791 return (-TNOADDR); 25792 } 25793 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25794 PMAPPORT, addrtype); 25795 mlp_port = PMAPPORT; 25796 } 25797 } else { 25798 int i; 25799 boolean_t priv = B_FALSE; 25800 25801 /* 25802 * If the requested_port is in the well-known privileged range, 25803 * verify that the stream was opened by a privileged user. 25804 * Note: No locks are held when inspecting tcp_g_*epriv_ports 25805 * but instead the code relies on: 25806 * - the fact that the address of the array and its size never 25807 * changes 25808 * - the atomic assignment of the elements of the array 25809 */ 25810 if (requested_port < tcps->tcps_smallest_nonpriv_port) { 25811 priv = B_TRUE; 25812 } else { 25813 for (i = 0; i < tcps->tcps_g_num_epriv_ports; i++) { 25814 if (requested_port == 25815 tcps->tcps_g_epriv_ports[i]) { 25816 priv = B_TRUE; 25817 break; 25818 } 25819 } 25820 } 25821 if (priv) { 25822 if (secpolicy_net_privaddr(cr, requested_port, 25823 IPPROTO_TCP) != 0) { 25824 if (tcp->tcp_debug) { 25825 (void) strlog(TCP_MOD_ID, 0, 1, 25826 SL_ERROR|SL_TRACE, 25827 "tcp_bind: no priv for port %d", 25828 requested_port); 25829 } 25830 return (-TACCES); 25831 } 25832 } 25833 user_specified = B_TRUE; 25834 25835 connp = tcp->tcp_connp; 25836 if (is_system_labeled()) { 25837 zone = crgetzone(cr); 25838 addrtype = tsol_mlp_addr_type(zone->zone_id, 25839 IPV6_VERSION, &v6addr, 25840 tcps->tcps_netstack->netstack_ip); 25841 if (addrtype == mlptSingle) { 25842 return (-TNOADDR); 25843 } 25844 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 25845 requested_port, addrtype); 25846 } 25847 } 25848 25849 if (mlptype != mlptSingle) { 25850 if (secpolicy_net_bindmlp(cr) != 0) { 25851 if (tcp->tcp_debug) { 25852 (void) strlog(TCP_MOD_ID, 0, 1, 25853 SL_ERROR|SL_TRACE, 25854 "tcp_bind: no priv for multilevel port %d", 25855 requested_port); 25856 } 25857 return (-TACCES); 25858 } 25859 25860 /* 25861 * If we're specifically binding a shared IP address and the 25862 * port is MLP on shared addresses, then check to see if this 25863 * zone actually owns the MLP. Reject if not. 25864 */ 25865 if (mlptype == mlptShared && addrtype == mlptShared) { 25866 /* 25867 * No need to handle exclusive-stack zones since 25868 * ALL_ZONES only applies to the shared stack. 25869 */ 25870 zoneid_t mlpzone; 25871 25872 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 25873 htons(mlp_port)); 25874 if (connp->conn_zoneid != mlpzone) { 25875 if (tcp->tcp_debug) { 25876 (void) strlog(TCP_MOD_ID, 0, 1, 25877 SL_ERROR|SL_TRACE, 25878 "tcp_bind: attempt to bind port " 25879 "%d on shared addr in zone %d " 25880 "(should be %d)", 25881 mlp_port, connp->conn_zoneid, 25882 mlpzone); 25883 } 25884 return (-TACCES); 25885 } 25886 } 25887 25888 if (!user_specified) { 25889 int err; 25890 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25891 requested_port, B_TRUE); 25892 if (err != 0) { 25893 if (tcp->tcp_debug) { 25894 (void) strlog(TCP_MOD_ID, 0, 1, 25895 SL_ERROR|SL_TRACE, 25896 "tcp_bind: cannot establish anon " 25897 "MLP for port %d", 25898 requested_port); 25899 } 25900 return (err); 25901 } 25902 connp->conn_anon_port = B_TRUE; 25903 } 25904 connp->conn_mlp_type = mlptype; 25905 } 25906 25907 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 25908 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 25909 25910 if (allocated_port == 0) { 25911 connp->conn_mlp_type = mlptSingle; 25912 if (connp->conn_anon_port) { 25913 connp->conn_anon_port = B_FALSE; 25914 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 25915 requested_port, B_FALSE); 25916 } 25917 if (bind_to_req_port_only) { 25918 if (tcp->tcp_debug) { 25919 (void) strlog(TCP_MOD_ID, 0, 1, 25920 SL_ERROR|SL_TRACE, 25921 "tcp_bind: requested addr busy"); 25922 } 25923 return (-TADDRBUSY); 25924 } else { 25925 /* If we are out of ports, fail the bind. */ 25926 if (tcp->tcp_debug) { 25927 (void) strlog(TCP_MOD_ID, 0, 1, 25928 SL_ERROR|SL_TRACE, 25929 "tcp_bind: out of ports?"); 25930 } 25931 return (-TNOADDR); 25932 } 25933 } 25934 25935 /* Pass the allocated port back */ 25936 *requested_port_ptr = allocated_port; 25937 return (0); 25938 } 25939 25940 static int 25941 tcp_bind_check(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 25942 boolean_t bind_to_req_port_only) 25943 { 25944 tcp_t *tcp = connp->conn_tcp; 25945 sin_t *sin; 25946 sin6_t *sin6; 25947 in_port_t requested_port; 25948 ipaddr_t v4addr; 25949 in6_addr_t v6addr; 25950 uint_t origipversion; 25951 int error = 0; 25952 25953 ASSERT((uintptr_t)len <= (uintptr_t)INT_MAX); 25954 25955 if (tcp->tcp_state == TCPS_BOUND) { 25956 return (0); 25957 } else if (tcp->tcp_state > TCPS_BOUND) { 25958 if (tcp->tcp_debug) { 25959 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 25960 "tcp_bind: bad state, %d", tcp->tcp_state); 25961 } 25962 return (-TOUTSTATE); 25963 } 25964 origipversion = tcp->tcp_ipversion; 25965 25966 ASSERT(sa != NULL && len != 0); 25967 25968 if (!OK_32PTR((char *)sa)) { 25969 if (tcp->tcp_debug) { 25970 (void) strlog(TCP_MOD_ID, 0, 1, 25971 SL_ERROR|SL_TRACE, 25972 "tcp_bind: bad address parameter, " 25973 "address %p, len %d", 25974 (void *)sa, len); 25975 } 25976 return (-TPROTO); 25977 } 25978 25979 switch (len) { 25980 case sizeof (sin_t): /* Complete IPv4 address */ 25981 sin = (sin_t *)sa; 25982 /* 25983 * With sockets sockfs will accept bogus sin_family in 25984 * bind() and replace it with the family used in the socket 25985 * call. 25986 */ 25987 if (sin->sin_family != AF_INET || 25988 tcp->tcp_family != AF_INET) { 25989 return (EAFNOSUPPORT); 25990 } 25991 requested_port = ntohs(sin->sin_port); 25992 tcp->tcp_ipversion = IPV4_VERSION; 25993 v4addr = sin->sin_addr.s_addr; 25994 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 25995 break; 25996 25997 case sizeof (sin6_t): /* Complete IPv6 address */ 25998 sin6 = (sin6_t *)sa; 25999 if (sin6->sin6_family != AF_INET6 || 26000 tcp->tcp_family != AF_INET6) { 26001 return (EAFNOSUPPORT); 26002 } 26003 requested_port = ntohs(sin6->sin6_port); 26004 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 26005 IPV4_VERSION : IPV6_VERSION; 26006 v6addr = sin6->sin6_addr; 26007 break; 26008 26009 default: 26010 if (tcp->tcp_debug) { 26011 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26012 "tcp_bind: bad address length, %d", len); 26013 } 26014 return (EAFNOSUPPORT); 26015 /* return (-TBADADDR); */ 26016 } 26017 26018 tcp->tcp_bound_source_v6 = v6addr; 26019 26020 /* Check for change in ipversion */ 26021 if (origipversion != tcp->tcp_ipversion) { 26022 ASSERT(tcp->tcp_family == AF_INET6); 26023 error = tcp->tcp_ipversion == IPV6_VERSION ? 26024 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 26025 if (error) { 26026 return (ENOMEM); 26027 } 26028 } 26029 26030 /* 26031 * Initialize family specific fields. Copy of the src addr. 26032 * in tcp_t is needed for the lookup funcs. 26033 */ 26034 if (tcp->tcp_ipversion == IPV6_VERSION) { 26035 tcp->tcp_ip6h->ip6_src = v6addr; 26036 } else { 26037 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 26038 } 26039 tcp->tcp_ip_src_v6 = v6addr; 26040 26041 bind_to_req_port_only = requested_port != 0 && bind_to_req_port_only; 26042 26043 error = tcp_bind_select_lport(tcp, &requested_port, 26044 bind_to_req_port_only, cr); 26045 26046 return (error); 26047 } 26048 26049 /* 26050 * Return unix error is tli error is TSYSERR, otherwise return a negative 26051 * tli error. 26052 */ 26053 int 26054 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr, 26055 boolean_t bind_to_req_port_only) 26056 { 26057 int error; 26058 tcp_t *tcp = connp->conn_tcp; 26059 26060 if (tcp->tcp_state >= TCPS_BOUND) { 26061 if (tcp->tcp_debug) { 26062 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26063 "tcp_bind: bad state, %d", tcp->tcp_state); 26064 } 26065 return (-TOUTSTATE); 26066 } 26067 26068 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only); 26069 if (error != 0) 26070 return (error); 26071 26072 ASSERT(tcp->tcp_state == TCPS_BOUND); 26073 26074 tcp->tcp_conn_req_max = 0; 26075 26076 if (tcp->tcp_family == AF_INET6) { 26077 ASSERT(tcp->tcp_connp->conn_af_isv6); 26078 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26079 &tcp->tcp_bound_source_v6, 0, B_FALSE); 26080 } else { 26081 ASSERT(!tcp->tcp_connp->conn_af_isv6); 26082 error = ip_proto_bind_laddr_v4(connp, NULL, IPPROTO_TCP, 26083 tcp->tcp_ipha->ipha_src, 0, B_FALSE); 26084 } 26085 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26086 } 26087 26088 int 26089 tcp_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa, 26090 socklen_t len, cred_t *cr) 26091 { 26092 int error; 26093 conn_t *connp = (conn_t *)proto_handle; 26094 squeue_t *sqp = connp->conn_sqp; 26095 26096 /* All Solaris components should pass a cred for this operation. */ 26097 ASSERT(cr != NULL); 26098 26099 ASSERT(sqp != NULL); 26100 ASSERT(connp->conn_upper_handle != NULL); 26101 26102 error = squeue_synch_enter(sqp, connp, NULL); 26103 if (error != 0) { 26104 /* failed to enter */ 26105 return (ENOSR); 26106 } 26107 26108 /* binding to a NULL address really means unbind */ 26109 if (sa == NULL) { 26110 if (connp->conn_tcp->tcp_state < TCPS_LISTEN) 26111 error = tcp_do_unbind(connp); 26112 else 26113 error = EINVAL; 26114 } else { 26115 error = tcp_do_bind(connp, sa, len, cr, B_TRUE); 26116 } 26117 26118 squeue_synch_exit(sqp, connp); 26119 26120 if (error < 0) { 26121 if (error == -TOUTSTATE) 26122 error = EINVAL; 26123 else 26124 error = proto_tlitosyserr(-error); 26125 } 26126 26127 return (error); 26128 } 26129 26130 /* 26131 * If the return value from this function is positive, it's a UNIX error. 26132 * Otherwise, if it's negative, then the absolute value is a TLI error. 26133 * the TPI routine tcp_tpi_connect() is a wrapper function for this. 26134 */ 26135 int 26136 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len, 26137 cred_t *cr, pid_t pid) 26138 { 26139 tcp_t *tcp = connp->conn_tcp; 26140 sin_t *sin = (sin_t *)sa; 26141 sin6_t *sin6 = (sin6_t *)sa; 26142 ipaddr_t *dstaddrp; 26143 in_port_t dstport; 26144 uint_t srcid; 26145 int error = 0; 26146 26147 switch (len) { 26148 default: 26149 /* 26150 * Should never happen 26151 */ 26152 return (EINVAL); 26153 26154 case sizeof (sin_t): 26155 sin = (sin_t *)sa; 26156 if (sin->sin_port == 0) { 26157 return (-TBADADDR); 26158 } 26159 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 26160 return (EAFNOSUPPORT); 26161 } 26162 break; 26163 26164 case sizeof (sin6_t): 26165 sin6 = (sin6_t *)sa; 26166 if (sin6->sin6_port == 0) { 26167 return (-TBADADDR); 26168 } 26169 break; 26170 } 26171 /* 26172 * If we're connecting to an IPv4-mapped IPv6 address, we need to 26173 * make sure that the template IP header in the tcp structure is an 26174 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 26175 * need to this before we call tcp_bindi() so that the port lookup 26176 * code will look for ports in the correct port space (IPv4 and 26177 * IPv6 have separate port spaces). 26178 */ 26179 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 26180 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26181 int err = 0; 26182 26183 err = tcp_header_init_ipv4(tcp); 26184 if (err != 0) { 26185 error = ENOMEM; 26186 goto connect_failed; 26187 } 26188 if (tcp->tcp_lport != 0) 26189 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 26190 } 26191 26192 switch (tcp->tcp_state) { 26193 case TCPS_LISTEN: 26194 /* 26195 * Listening sockets are not allowed to issue connect(). 26196 */ 26197 if (IPCL_IS_NONSTR(connp)) 26198 return (EOPNOTSUPP); 26199 /* FALLTHRU */ 26200 case TCPS_IDLE: 26201 /* 26202 * We support quick connect, refer to comments in 26203 * tcp_connect_*() 26204 */ 26205 /* FALLTHRU */ 26206 case TCPS_BOUND: 26207 /* 26208 * We must bump the generation before the operation start. 26209 * This is done to ensure that any upcall made later on sends 26210 * up the right generation to the socket. 26211 */ 26212 SOCK_CONNID_BUMP(tcp->tcp_connid); 26213 26214 if (tcp->tcp_family == AF_INET6) { 26215 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 26216 return (tcp_connect_ipv6(tcp, 26217 &sin6->sin6_addr, 26218 sin6->sin6_port, sin6->sin6_flowinfo, 26219 sin6->__sin6_src_id, sin6->sin6_scope_id, 26220 cr, pid)); 26221 } 26222 /* 26223 * Destination adress is mapped IPv6 address. 26224 * Source bound address should be unspecified or 26225 * IPv6 mapped address as well. 26226 */ 26227 if (!IN6_IS_ADDR_UNSPECIFIED( 26228 &tcp->tcp_bound_source_v6) && 26229 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 26230 return (EADDRNOTAVAIL); 26231 } 26232 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 26233 dstport = sin6->sin6_port; 26234 srcid = sin6->__sin6_src_id; 26235 } else { 26236 dstaddrp = &sin->sin_addr.s_addr; 26237 dstport = sin->sin_port; 26238 srcid = 0; 26239 } 26240 26241 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid, cr, 26242 pid); 26243 break; 26244 default: 26245 return (-TOUTSTATE); 26246 } 26247 /* 26248 * Note: Code below is the "failure" case 26249 */ 26250 connect_failed: 26251 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 26252 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 26253 return (error); 26254 } 26255 26256 int 26257 tcp_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa, 26258 socklen_t len, sock_connid_t *id, cred_t *cr) 26259 { 26260 conn_t *connp = (conn_t *)proto_handle; 26261 tcp_t *tcp = connp->conn_tcp; 26262 squeue_t *sqp = connp->conn_sqp; 26263 int error; 26264 26265 ASSERT(connp->conn_upper_handle != NULL); 26266 26267 /* All Solaris components should pass a cred for this operation. */ 26268 ASSERT(cr != NULL); 26269 26270 error = proto_verify_ip_addr(tcp->tcp_family, sa, len); 26271 if (error != 0) { 26272 return (error); 26273 } 26274 26275 error = squeue_synch_enter(sqp, connp, NULL); 26276 if (error != 0) { 26277 /* failed to enter */ 26278 return (ENOSR); 26279 } 26280 26281 /* 26282 * TCP supports quick connect, so no need to do an implicit bind 26283 */ 26284 error = tcp_do_connect(connp, sa, len, cr, curproc->p_pid); 26285 if (error == 0) { 26286 *id = connp->conn_tcp->tcp_connid; 26287 } else if (error < 0) { 26288 if (error == -TOUTSTATE) { 26289 switch (connp->conn_tcp->tcp_state) { 26290 case TCPS_SYN_SENT: 26291 error = EALREADY; 26292 break; 26293 case TCPS_ESTABLISHED: 26294 error = EISCONN; 26295 break; 26296 case TCPS_LISTEN: 26297 error = EOPNOTSUPP; 26298 break; 26299 default: 26300 error = EINVAL; 26301 break; 26302 } 26303 } else { 26304 error = proto_tlitosyserr(-error); 26305 } 26306 } 26307 done: 26308 squeue_synch_exit(sqp, connp); 26309 26310 return ((error == 0) ? EINPROGRESS : error); 26311 } 26312 26313 /* ARGSUSED */ 26314 sock_lower_handle_t 26315 tcp_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls, 26316 uint_t *smodep, int *errorp, int flags, cred_t *credp) 26317 { 26318 conn_t *connp; 26319 boolean_t isv6 = family == AF_INET6; 26320 if (type != SOCK_STREAM || (family != AF_INET && family != AF_INET6) || 26321 (proto != 0 && proto != IPPROTO_TCP)) { 26322 *errorp = EPROTONOSUPPORT; 26323 return (NULL); 26324 } 26325 26326 connp = tcp_create_common(NULL, credp, isv6, B_TRUE, errorp); 26327 if (connp == NULL) { 26328 return (NULL); 26329 } 26330 26331 /* 26332 * Put the ref for TCP. Ref for IP was already put 26333 * by ipcl_conn_create. Also Make the conn_t globally 26334 * visible to walkers 26335 */ 26336 mutex_enter(&connp->conn_lock); 26337 CONN_INC_REF_LOCKED(connp); 26338 ASSERT(connp->conn_ref == 2); 26339 connp->conn_state_flags &= ~CONN_INCIPIENT; 26340 26341 connp->conn_flags |= IPCL_NONSTR; 26342 mutex_exit(&connp->conn_lock); 26343 26344 ASSERT(errorp != NULL); 26345 *errorp = 0; 26346 *sock_downcalls = &sock_tcp_downcalls; 26347 *smodep = SM_CONNREQUIRED | SM_EXDATA | SM_ACCEPTSUPP | 26348 SM_SENDFILESUPP; 26349 26350 return ((sock_lower_handle_t)connp); 26351 } 26352 26353 /* ARGSUSED */ 26354 void 26355 tcp_activate(sock_lower_handle_t proto_handle, sock_upper_handle_t sock_handle, 26356 sock_upcalls_t *sock_upcalls, int flags, cred_t *cr) 26357 { 26358 conn_t *connp = (conn_t *)proto_handle; 26359 struct sock_proto_props sopp; 26360 26361 ASSERT(connp->conn_upper_handle == NULL); 26362 26363 /* All Solaris components should pass a cred for this operation. */ 26364 ASSERT(cr != NULL); 26365 26366 sopp.sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT | 26367 SOCKOPT_MAXPSZ | SOCKOPT_MAXBLK | SOCKOPT_RCVTIMER | 26368 SOCKOPT_RCVTHRESH | SOCKOPT_MAXADDRLEN | SOCKOPT_MINPSZ; 26369 26370 sopp.sopp_rxhiwat = SOCKET_RECVHIWATER; 26371 sopp.sopp_rxlowat = SOCKET_RECVLOWATER; 26372 sopp.sopp_maxpsz = INFPSZ; 26373 sopp.sopp_maxblk = INFPSZ; 26374 sopp.sopp_rcvtimer = SOCKET_TIMER_INTERVAL; 26375 sopp.sopp_rcvthresh = SOCKET_RECVHIWATER >> 3; 26376 sopp.sopp_maxaddrlen = sizeof (sin6_t); 26377 sopp.sopp_minpsz = (tcp_rinfo.mi_minpsz == 1) ? 0 : 26378 tcp_rinfo.mi_minpsz; 26379 26380 connp->conn_upcalls = sock_upcalls; 26381 connp->conn_upper_handle = sock_handle; 26382 26383 (*sock_upcalls->su_set_proto_props)(sock_handle, &sopp); 26384 } 26385 26386 /* ARGSUSED */ 26387 int 26388 tcp_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr) 26389 { 26390 conn_t *connp = (conn_t *)proto_handle; 26391 26392 ASSERT(connp->conn_upper_handle != NULL); 26393 26394 /* All Solaris components should pass a cred for this operation. */ 26395 ASSERT(cr != NULL); 26396 26397 tcp_close_common(connp, flags); 26398 26399 ip_free_helper_stream(connp); 26400 26401 /* 26402 * Drop IP's reference on the conn. This is the last reference 26403 * on the connp if the state was less than established. If the 26404 * connection has gone into timewait state, then we will have 26405 * one ref for the TCP and one more ref (total of two) for the 26406 * classifier connected hash list (a timewait connections stays 26407 * in connected hash till closed). 26408 * 26409 * We can't assert the references because there might be other 26410 * transient reference places because of some walkers or queued 26411 * packets in squeue for the timewait state. 26412 */ 26413 CONN_DEC_REF(connp); 26414 return (0); 26415 } 26416 26417 /* ARGSUSED */ 26418 int 26419 tcp_sendmsg(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg, 26420 cred_t *cr) 26421 { 26422 tcp_t *tcp; 26423 uint32_t msize; 26424 conn_t *connp = (conn_t *)proto_handle; 26425 int32_t tcpstate; 26426 26427 /* All Solaris components should pass a cred for this operation. */ 26428 ASSERT(cr != NULL); 26429 26430 ASSERT(connp->conn_ref >= 2); 26431 ASSERT(connp->conn_upper_handle != NULL); 26432 26433 if (msg->msg_controllen != 0) { 26434 return (EOPNOTSUPP); 26435 26436 } 26437 switch (DB_TYPE(mp)) { 26438 case M_DATA: 26439 tcp = connp->conn_tcp; 26440 ASSERT(tcp != NULL); 26441 26442 tcpstate = tcp->tcp_state; 26443 if (tcpstate < TCPS_ESTABLISHED) { 26444 freemsg(mp); 26445 return (ENOTCONN); 26446 } else if (tcpstate > TCPS_CLOSE_WAIT) { 26447 freemsg(mp); 26448 return (EPIPE); 26449 } 26450 26451 msize = msgdsize(mp); 26452 26453 mutex_enter(&tcp->tcp_non_sq_lock); 26454 tcp->tcp_squeue_bytes += msize; 26455 /* 26456 * Squeue Flow Control 26457 */ 26458 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 26459 tcp_setqfull(tcp); 26460 } 26461 mutex_exit(&tcp->tcp_non_sq_lock); 26462 26463 /* 26464 * The application may pass in an address in the msghdr, but 26465 * we ignore the address on connection-oriented sockets. 26466 * Just like BSD this code does not generate an error for 26467 * TCP (a CONNREQUIRED socket) when sending to an address 26468 * passed in with sendto/sendmsg. Instead the data is 26469 * delivered on the connection as if no address had been 26470 * supplied. 26471 */ 26472 CONN_INC_REF(connp); 26473 26474 if (msg != NULL && msg->msg_flags & MSG_OOB) { 26475 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, 26476 tcp_output_urgent, connp, tcp_squeue_flag, 26477 SQTAG_TCP_OUTPUT); 26478 } else { 26479 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_output, 26480 connp, tcp_squeue_flag, SQTAG_TCP_OUTPUT); 26481 } 26482 26483 return (0); 26484 26485 default: 26486 ASSERT(0); 26487 } 26488 26489 freemsg(mp); 26490 return (0); 26491 } 26492 26493 /* ARGSUSED */ 26494 void 26495 tcp_output_urgent(void *arg, mblk_t *mp, void *arg2) 26496 { 26497 int len; 26498 uint32_t msize; 26499 conn_t *connp = (conn_t *)arg; 26500 tcp_t *tcp = connp->conn_tcp; 26501 26502 msize = msgdsize(mp); 26503 26504 len = msize - 1; 26505 if (len < 0) { 26506 freemsg(mp); 26507 return; 26508 } 26509 26510 /* 26511 * Try to force urgent data out on the wire. 26512 * Even if we have unsent data this will 26513 * at least send the urgent flag. 26514 * XXX does not handle more flag correctly. 26515 */ 26516 len += tcp->tcp_unsent; 26517 len += tcp->tcp_snxt; 26518 tcp->tcp_urg = len; 26519 tcp->tcp_valid_bits |= TCP_URG_VALID; 26520 26521 /* Bypass tcp protocol for fused tcp loopback */ 26522 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 26523 return; 26524 tcp_wput_data(tcp, mp, B_TRUE); 26525 } 26526 26527 /* ARGSUSED */ 26528 int 26529 tcp_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26530 socklen_t *addrlenp, cred_t *cr) 26531 { 26532 conn_t *connp = (conn_t *)proto_handle; 26533 tcp_t *tcp = connp->conn_tcp; 26534 26535 ASSERT(connp->conn_upper_handle != NULL); 26536 /* All Solaris components should pass a cred for this operation. */ 26537 ASSERT(cr != NULL); 26538 26539 ASSERT(tcp != NULL); 26540 26541 return (tcp_do_getpeername(tcp, addr, addrlenp)); 26542 } 26543 26544 /* ARGSUSED */ 26545 int 26546 tcp_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *addr, 26547 socklen_t *addrlenp, cred_t *cr) 26548 { 26549 conn_t *connp = (conn_t *)proto_handle; 26550 tcp_t *tcp = connp->conn_tcp; 26551 26552 /* All Solaris components should pass a cred for this operation. */ 26553 ASSERT(cr != NULL); 26554 26555 ASSERT(connp->conn_upper_handle != NULL); 26556 26557 return (tcp_do_getsockname(tcp, addr, addrlenp)); 26558 } 26559 26560 /* 26561 * tcp_fallback 26562 * 26563 * A direct socket is falling back to using STREAMS. The queue 26564 * that is being passed down was created using tcp_open() with 26565 * the SO_FALLBACK flag set. As a result, the queue is not 26566 * associated with a conn, and the q_ptrs instead contain the 26567 * dev and minor area that should be used. 26568 * 26569 * The 'direct_sockfs' flag indicates whether the FireEngine 26570 * optimizations should be used. The common case would be that 26571 * optimizations are enabled, and they might be subsequently 26572 * disabled using the _SIOCSOCKFALLBACK ioctl. 26573 */ 26574 26575 /* 26576 * An active connection is falling back to TPI. Gather all the information 26577 * required by the STREAM head and TPI sonode and send it up. 26578 */ 26579 void 26580 tcp_fallback_noneager(tcp_t *tcp, mblk_t *stropt_mp, queue_t *q, 26581 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26582 { 26583 conn_t *connp = tcp->tcp_connp; 26584 struct stroptions *stropt; 26585 struct T_capability_ack tca; 26586 struct sockaddr_in6 laddr, faddr; 26587 socklen_t laddrlen, faddrlen; 26588 short opts; 26589 int error; 26590 mblk_t *mp; 26591 26592 connp->conn_dev = (dev_t)RD(q)->q_ptr; 26593 connp->conn_minor_arena = WR(q)->q_ptr; 26594 26595 RD(q)->q_ptr = WR(q)->q_ptr = connp; 26596 26597 connp->conn_tcp->tcp_rq = connp->conn_rq = RD(q); 26598 connp->conn_tcp->tcp_wq = connp->conn_wq = WR(q); 26599 26600 WR(q)->q_qinfo = &tcp_sock_winit; 26601 26602 if (!direct_sockfs) 26603 tcp_disable_direct_sockfs(tcp); 26604 26605 /* 26606 * free the helper stream 26607 */ 26608 ip_free_helper_stream(connp); 26609 26610 /* 26611 * Notify the STREAM head about options 26612 */ 26613 DB_TYPE(stropt_mp) = M_SETOPTS; 26614 stropt = (struct stroptions *)stropt_mp->b_rptr; 26615 stropt_mp->b_wptr += sizeof (struct stroptions); 26616 stropt->so_flags = SO_HIWAT | SO_WROFF | SO_MAXBLK; 26617 26618 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 26619 tcp->tcp_tcps->tcps_wroff_xtra); 26620 if (tcp->tcp_snd_sack_ok) 26621 stropt->so_wroff += TCPOPT_MAX_SACK_LEN; 26622 stropt->so_hiwat = tcp->tcp_fused ? 26623 tcp_fuse_set_rcv_hiwat(tcp, tcp->tcp_recv_hiwater) : 26624 MAX(tcp->tcp_recv_hiwater, tcp->tcp_tcps->tcps_sth_rcv_hiwat); 26625 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 26626 26627 putnext(RD(q), stropt_mp); 26628 26629 /* 26630 * Collect the information needed to sync with the sonode 26631 */ 26632 tcp_do_capability_ack(tcp, &tca, TC1_INFO|TC1_ACCEPTOR_ID); 26633 26634 laddrlen = faddrlen = sizeof (sin6_t); 26635 (void) tcp_do_getsockname(tcp, (struct sockaddr *)&laddr, &laddrlen); 26636 error = tcp_do_getpeername(tcp, (struct sockaddr *)&faddr, &faddrlen); 26637 if (error != 0) 26638 faddrlen = 0; 26639 26640 opts = 0; 26641 if (tcp->tcp_oobinline) 26642 opts |= SO_OOBINLINE; 26643 if (tcp->tcp_dontroute) 26644 opts |= SO_DONTROUTE; 26645 26646 /* 26647 * Notify the socket that the protocol is now quiescent, 26648 * and it's therefore safe move data from the socket 26649 * to the stream head. 26650 */ 26651 (*quiesced_cb)(connp->conn_upper_handle, q, &tca, 26652 (struct sockaddr *)&laddr, laddrlen, 26653 (struct sockaddr *)&faddr, faddrlen, opts); 26654 26655 while ((mp = tcp->tcp_rcv_list) != NULL) { 26656 tcp->tcp_rcv_list = mp->b_next; 26657 mp->b_next = NULL; 26658 putnext(q, mp); 26659 } 26660 tcp->tcp_rcv_last_head = NULL; 26661 tcp->tcp_rcv_last_tail = NULL; 26662 tcp->tcp_rcv_cnt = 0; 26663 } 26664 26665 /* 26666 * An eager is falling back to TPI. All we have to do is send 26667 * up a T_CONN_IND. 26668 */ 26669 void 26670 tcp_fallback_eager(tcp_t *eager, boolean_t direct_sockfs) 26671 { 26672 tcp_t *listener = eager->tcp_listener; 26673 mblk_t *mp = eager->tcp_conn.tcp_eager_conn_ind; 26674 26675 ASSERT(listener != NULL); 26676 ASSERT(mp != NULL); 26677 26678 eager->tcp_conn.tcp_eager_conn_ind = NULL; 26679 26680 /* 26681 * TLI/XTI applications will get confused by 26682 * sending eager as an option since it violates 26683 * the option semantics. So remove the eager as 26684 * option since TLI/XTI app doesn't need it anyway. 26685 */ 26686 if (!direct_sockfs) { 26687 struct T_conn_ind *conn_ind; 26688 26689 conn_ind = (struct T_conn_ind *)mp->b_rptr; 26690 conn_ind->OPT_length = 0; 26691 conn_ind->OPT_offset = 0; 26692 } 26693 26694 /* 26695 * Sockfs guarantees that the listener will not be closed 26696 * during fallback. So we can safely use the listener's queue. 26697 */ 26698 putnext(listener->tcp_rq, mp); 26699 } 26700 26701 int 26702 tcp_fallback(sock_lower_handle_t proto_handle, queue_t *q, 26703 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb) 26704 { 26705 tcp_t *tcp; 26706 conn_t *connp = (conn_t *)proto_handle; 26707 int error; 26708 mblk_t *stropt_mp; 26709 mblk_t *ordrel_mp; 26710 mblk_t *fused_sigurp_mp; 26711 26712 tcp = connp->conn_tcp; 26713 26714 stropt_mp = allocb_wait(sizeof (struct stroptions), BPRI_HI, STR_NOSIG, 26715 NULL); 26716 26717 /* Pre-allocate the T_ordrel_ind mblk. */ 26718 ASSERT(tcp->tcp_ordrel_mp == NULL); 26719 ordrel_mp = allocb_wait(sizeof (struct T_ordrel_ind), BPRI_HI, 26720 STR_NOSIG, NULL); 26721 ordrel_mp->b_datap->db_type = M_PROTO; 26722 ((struct T_ordrel_ind *)ordrel_mp->b_rptr)->PRIM_type = T_ORDREL_IND; 26723 ordrel_mp->b_wptr += sizeof (struct T_ordrel_ind); 26724 26725 /* Pre-allocate the M_PCSIG used by fusion */ 26726 fused_sigurp_mp = allocb_wait(1, BPRI_HI, STR_NOSIG, NULL); 26727 26728 /* 26729 * Enter the squeue so that no new packets can come in 26730 */ 26731 error = squeue_synch_enter(connp->conn_sqp, connp, NULL); 26732 if (error != 0) { 26733 /* failed to enter, free all the pre-allocated messages. */ 26734 freeb(stropt_mp); 26735 freeb(ordrel_mp); 26736 freeb(fused_sigurp_mp); 26737 /* 26738 * We cannot process the eager, so at least send out a 26739 * RST so the peer can reconnect. 26740 */ 26741 if (tcp->tcp_listener != NULL) { 26742 (void) tcp_eager_blowoff(tcp->tcp_listener, 26743 tcp->tcp_conn_req_seqnum); 26744 } 26745 return (ENOMEM); 26746 } 26747 26748 /* 26749 * No longer a direct socket 26750 */ 26751 connp->conn_flags &= ~IPCL_NONSTR; 26752 26753 tcp->tcp_ordrel_mp = ordrel_mp; 26754 26755 if (tcp->tcp_fused) { 26756 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 26757 tcp->tcp_fused_sigurg_mp = fused_sigurp_mp; 26758 } else { 26759 freeb(fused_sigurp_mp); 26760 } 26761 26762 if (tcp->tcp_listener != NULL) { 26763 /* The eager will deal with opts when accept() is called */ 26764 freeb(stropt_mp); 26765 tcp_fallback_eager(tcp, direct_sockfs); 26766 } else { 26767 tcp_fallback_noneager(tcp, stropt_mp, q, direct_sockfs, 26768 quiesced_cb); 26769 } 26770 26771 /* 26772 * There should be atleast two ref's (IP + TCP) 26773 */ 26774 ASSERT(connp->conn_ref >= 2); 26775 squeue_synch_exit(connp->conn_sqp, connp); 26776 26777 return (0); 26778 } 26779 26780 /* ARGSUSED */ 26781 static void 26782 tcp_shutdown_output(void *arg, mblk_t *mp, void *arg2) 26783 { 26784 conn_t *connp = (conn_t *)arg; 26785 tcp_t *tcp = connp->conn_tcp; 26786 26787 freemsg(mp); 26788 26789 if (tcp->tcp_fused) 26790 tcp_unfuse(tcp); 26791 26792 if (tcp_xmit_end(tcp) != 0) { 26793 /* 26794 * We were crossing FINs and got a reset from 26795 * the other side. Just ignore it. 26796 */ 26797 if (tcp->tcp_debug) { 26798 (void) strlog(TCP_MOD_ID, 0, 1, 26799 SL_ERROR|SL_TRACE, 26800 "tcp_shutdown_output() out of state %s", 26801 tcp_display(tcp, NULL, DISP_ADDR_AND_PORT)); 26802 } 26803 } 26804 } 26805 26806 /* ARGSUSED */ 26807 int 26808 tcp_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr) 26809 { 26810 conn_t *connp = (conn_t *)proto_handle; 26811 tcp_t *tcp = connp->conn_tcp; 26812 26813 ASSERT(connp->conn_upper_handle != NULL); 26814 26815 /* All Solaris components should pass a cred for this operation. */ 26816 ASSERT(cr != NULL); 26817 26818 /* 26819 * X/Open requires that we check the connected state. 26820 */ 26821 if (tcp->tcp_state < TCPS_SYN_SENT) 26822 return (ENOTCONN); 26823 26824 /* shutdown the send side */ 26825 if (how != SHUT_RD) { 26826 mblk_t *bp; 26827 26828 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL); 26829 CONN_INC_REF(connp); 26830 SQUEUE_ENTER_ONE(connp->conn_sqp, bp, tcp_shutdown_output, 26831 connp, SQ_NODRAIN, SQTAG_TCP_SHUTDOWN_OUTPUT); 26832 26833 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26834 SOCK_OPCTL_SHUT_SEND, 0); 26835 } 26836 26837 /* shutdown the recv side */ 26838 if (how != SHUT_WR) 26839 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26840 SOCK_OPCTL_SHUT_RECV, 0); 26841 26842 return (0); 26843 } 26844 26845 /* 26846 * SOP_LISTEN() calls into tcp_listen(). 26847 */ 26848 /* ARGSUSED */ 26849 int 26850 tcp_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr) 26851 { 26852 conn_t *connp = (conn_t *)proto_handle; 26853 int error; 26854 squeue_t *sqp = connp->conn_sqp; 26855 26856 ASSERT(connp->conn_upper_handle != NULL); 26857 26858 /* All Solaris components should pass a cred for this operation. */ 26859 ASSERT(cr != NULL); 26860 26861 error = squeue_synch_enter(sqp, connp, NULL); 26862 if (error != 0) { 26863 /* failed to enter */ 26864 return (ENOBUFS); 26865 } 26866 26867 error = tcp_do_listen(connp, NULL, 0, backlog, cr, FALSE); 26868 if (error == 0) { 26869 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle, 26870 SOCK_OPCTL_ENAB_ACCEPT, (uintptr_t)backlog); 26871 } else if (error < 0) { 26872 if (error == -TOUTSTATE) 26873 error = EINVAL; 26874 else 26875 error = proto_tlitosyserr(-error); 26876 } 26877 squeue_synch_exit(sqp, connp); 26878 return (error); 26879 } 26880 26881 static int 26882 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len, 26883 int backlog, cred_t *cr, boolean_t bind_to_req_port_only) 26884 { 26885 tcp_t *tcp = connp->conn_tcp; 26886 int error = 0; 26887 tcp_stack_t *tcps = tcp->tcp_tcps; 26888 26889 /* All Solaris components should pass a cred for this operation. */ 26890 ASSERT(cr != NULL); 26891 26892 if (tcp->tcp_state >= TCPS_BOUND) { 26893 if ((tcp->tcp_state == TCPS_BOUND || 26894 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) { 26895 /* 26896 * Handle listen() increasing backlog. 26897 * This is more "liberal" then what the TPI spec 26898 * requires but is needed to avoid a t_unbind 26899 * when handling listen() since the port number 26900 * might be "stolen" between the unbind and bind. 26901 */ 26902 goto do_listen; 26903 } 26904 if (tcp->tcp_debug) { 26905 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 26906 "tcp_listen: bad state, %d", tcp->tcp_state); 26907 } 26908 return (-TOUTSTATE); 26909 } else { 26910 if (sa == NULL) { 26911 sin6_t addr; 26912 sin_t *sin; 26913 sin6_t *sin6; 26914 26915 ASSERT(IPCL_IS_NONSTR(connp)); 26916 26917 /* Do an implicit bind: Request for a generic port. */ 26918 if (tcp->tcp_family == AF_INET) { 26919 len = sizeof (sin_t); 26920 sin = (sin_t *)&addr; 26921 *sin = sin_null; 26922 sin->sin_family = AF_INET; 26923 tcp->tcp_ipversion = IPV4_VERSION; 26924 } else { 26925 ASSERT(tcp->tcp_family == AF_INET6); 26926 len = sizeof (sin6_t); 26927 sin6 = (sin6_t *)&addr; 26928 *sin6 = sin6_null; 26929 sin6->sin6_family = AF_INET6; 26930 tcp->tcp_ipversion = IPV6_VERSION; 26931 } 26932 sa = (struct sockaddr *)&addr; 26933 } 26934 26935 error = tcp_bind_check(connp, sa, len, cr, 26936 bind_to_req_port_only); 26937 if (error) 26938 return (error); 26939 /* Fall through and do the fanout insertion */ 26940 } 26941 26942 do_listen: 26943 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN); 26944 tcp->tcp_conn_req_max = backlog; 26945 if (tcp->tcp_conn_req_max) { 26946 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min) 26947 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min; 26948 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q) 26949 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q; 26950 /* 26951 * If this is a listener, do not reset the eager list 26952 * and other stuffs. Note that we don't check if the 26953 * existing eager list meets the new tcp_conn_req_max 26954 * requirement. 26955 */ 26956 if (tcp->tcp_state != TCPS_LISTEN) { 26957 tcp->tcp_state = TCPS_LISTEN; 26958 /* Initialize the chain. Don't need the eager_lock */ 26959 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 26960 tcp->tcp_eager_next_drop_q0 = tcp; 26961 tcp->tcp_eager_prev_drop_q0 = tcp; 26962 tcp->tcp_second_ctimer_threshold = 26963 tcps->tcps_ip_abort_linterval; 26964 } 26965 } 26966 26967 /* 26968 * We can call ip_bind directly, the processing continues 26969 * in tcp_post_ip_bind(). 26970 * 26971 * We need to make sure that the conn_recv is set to a non-null 26972 * value before we insert the conn into the classifier table. 26973 * This is to avoid a race with an incoming packet which does an 26974 * ipcl_classify(). 26975 */ 26976 connp->conn_recv = tcp_conn_request; 26977 if (tcp->tcp_family == AF_INET) { 26978 error = ip_proto_bind_laddr_v4(connp, NULL, 26979 IPPROTO_TCP, tcp->tcp_bound_source, tcp->tcp_lport, B_TRUE); 26980 } else { 26981 error = ip_proto_bind_laddr_v6(connp, NULL, IPPROTO_TCP, 26982 &tcp->tcp_bound_source_v6, tcp->tcp_lport, B_TRUE); 26983 } 26984 return (tcp_post_ip_bind(tcp, NULL, error, NULL, 0)); 26985 } 26986 26987 void 26988 tcp_clr_flowctrl(sock_lower_handle_t proto_handle) 26989 { 26990 conn_t *connp = (conn_t *)proto_handle; 26991 tcp_t *tcp = connp->conn_tcp; 26992 mblk_t *mp; 26993 int error; 26994 26995 ASSERT(connp->conn_upper_handle != NULL); 26996 26997 /* 26998 * If tcp->tcp_rsrv_mp == NULL, it means that tcp_clr_flowctrl() 26999 * is currently running. 27000 */ 27001 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27002 if ((mp = tcp->tcp_rsrv_mp) == NULL) { 27003 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27004 return; 27005 } 27006 tcp->tcp_rsrv_mp = NULL; 27007 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27008 27009 error = squeue_synch_enter(connp->conn_sqp, connp, mp); 27010 ASSERT(error == 0); 27011 27012 mutex_enter(&tcp->tcp_rsrv_mp_lock); 27013 tcp->tcp_rsrv_mp = mp; 27014 mutex_exit(&tcp->tcp_rsrv_mp_lock); 27015 27016 if (tcp->tcp_fused) { 27017 tcp_fuse_backenable(tcp); 27018 } else { 27019 tcp->tcp_rwnd = tcp->tcp_recv_hiwater; 27020 /* 27021 * Send back a window update immediately if TCP is above 27022 * ESTABLISHED state and the increase of the rcv window 27023 * that the other side knows is at least 1 MSS after flow 27024 * control is lifted. 27025 */ 27026 if (tcp->tcp_state >= TCPS_ESTABLISHED && 27027 tcp_rwnd_reopen(tcp) == TH_ACK_NEEDED) { 27028 tcp_xmit_ctl(NULL, tcp, 27029 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 27030 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 27031 } 27032 } 27033 27034 squeue_synch_exit(connp->conn_sqp, connp); 27035 } 27036 27037 /* ARGSUSED */ 27038 int 27039 tcp_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg, 27040 int mode, int32_t *rvalp, cred_t *cr) 27041 { 27042 conn_t *connp = (conn_t *)proto_handle; 27043 int error; 27044 27045 ASSERT(connp->conn_upper_handle != NULL); 27046 27047 /* All Solaris components should pass a cred for this operation. */ 27048 ASSERT(cr != NULL); 27049 27050 switch (cmd) { 27051 case ND_SET: 27052 case ND_GET: 27053 case TCP_IOC_DEFAULT_Q: 27054 case _SIOCSOCKFALLBACK: 27055 case TCP_IOC_ABORT_CONN: 27056 case TI_GETPEERNAME: 27057 case TI_GETMYNAME: 27058 ip1dbg(("tcp_ioctl: cmd 0x%x on non sreams socket", 27059 cmd)); 27060 error = EINVAL; 27061 break; 27062 default: 27063 /* 27064 * Pass on to IP using helper stream 27065 */ 27066 error = ldi_ioctl(connp->conn_helper_info->iphs_handle, 27067 cmd, arg, mode, cr, rvalp); 27068 break; 27069 } 27070 return (error); 27071 } 27072 27073 sock_downcalls_t sock_tcp_downcalls = { 27074 tcp_activate, 27075 tcp_accept, 27076 tcp_bind, 27077 tcp_listen, 27078 tcp_connect, 27079 tcp_getpeername, 27080 tcp_getsockname, 27081 tcp_getsockopt, 27082 tcp_setsockopt, 27083 tcp_sendmsg, 27084 NULL, 27085 NULL, 27086 NULL, 27087 tcp_shutdown, 27088 tcp_clr_flowctrl, 27089 tcp_ioctl, 27090 tcp_close, 27091 }; 27092